Skip to content

Commit fbb0481

Browse files
authored
Merge pull request #14 from adafruit/seesawify
Seesawify & add a re-awb resetter
2 parents 5079b56 + 171db33 commit fbb0481

File tree

1 file changed

+198
-21
lines changed

1 file changed

+198
-21
lines changed

adafruit_ov5640.py

Lines changed: 198 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@
3030
import time
3131
import imagecapture
3232
import pwmio
33-
import digitalio
3433
from adafruit_bus_device.i2c_device import I2CDevice
3534

3635
try:
3736
from typing import Optional, Sequence, List, Union
3837
from busio import I2C
3938
from microcontroller import Pin
39+
from digitalio import DigitalInOut
4040
except ImportError:
4141
pass
4242

@@ -51,6 +51,37 @@
5151
OV5640_COLOR_JPEG = 3
5252

5353
# fmt: off
54+
55+
_SYSTEM_RESET00 = const(0x3000) # Reset for Individual Block
56+
# (0: enable block; 1: reset block)
57+
# Bit[7]: Reset BIST
58+
# Bit[6]: Reset MCU program memory
59+
# Bit[5]: Reset MCU
60+
# Bit[4]: Reset OTP
61+
# Bit[3]: Reset STB
62+
# Bit[2]: Reset d5060
63+
# Bit[1]: Reset timing control
64+
# Bit[0]: Reset array control
65+
66+
_SYSTEM_RESET02 = const(0x3002) # Reset for Individual Block
67+
# (0: enable block; 1: reset block)
68+
# Bit[7]: Reset VFIFO
69+
# Bit[5]: Reset format
70+
# Bit[4]: Reset JFIFO
71+
# Bit[3]: Reset SFIFO
72+
# Bit[2]: Reset JPG
73+
# Bit[1]: Reset format MUX
74+
# Bit[0]: Reset average
75+
76+
_CLOCK_ENABLE02 = const(0x3006) # Clock Enable Control
77+
# (0: disable clock; 1: enable clock)
78+
# Bit[7]: Enable PSRAM clock
79+
# Bit[6]: Enable FMT clock
80+
# Bit[5]: Enable JPEG 2x clock
81+
# Bit[3]: Enable JPEG clock
82+
# Bit[1]: Enable format MUX clock
83+
# Bit[0]: Enable average clock
84+
5485
_SYSTEM_CTROL0 = const(0x3008)
5586
# Bit[7]: Software reset
5687
# Bit[6]: Software power down
@@ -397,11 +428,11 @@
397428
0x4713, 0x02, # jpg mode select
398429
_ISP_CONTROL_01, 0x83, # turn color matrix, awb and SDE
399430
# sys reset
400-
0x3000, 0x00,
401-
0x3002, 0x1C,
431+
_SYSTEM_RESET00, 0x00, # enable all blocks
432+
_SYSTEM_RESET02, 0x1C, # reset jfifo, sfifo, jpg, fmux, avg
402433
# clock enable
403434
0x3004, 0xFF,
404-
0x3006, 0xC3,
435+
_CLOCK_ENABLE02, 0xC3,
405436
# isp control
406437
0x5000, 0xA7,
407438
_ISP_CONTROL_01, 0xA3, # +scaling?
@@ -537,14 +568,156 @@
537568
0x3008, 0x02,
538569
# 50Hz
539570
0x3C00, 0x04,
540-
_REG_DLY, 300,
571+
#_REG_DLY, 300,
541572
]
542573

574+
575+
576+
_reset_awb = [
577+
_ISP_CONTROL_01, 0x83, # turn color matrix, awb and SDE
578+
# sys reset
579+
_SYSTEM_RESET00, 0x00, # enable all blocks
580+
_SYSTEM_RESET02, 0x1C, # reset jfifo, sfifo, jpg, fmux, avg
581+
# clock enable
582+
#0x3004, 0xFF,
583+
#_CLOCK_ENABLE02, 0xC3,
584+
# isp control
585+
0x5000, 0xA7,
586+
_ISP_CONTROL_01, 0xA3, # +scaling?
587+
0x5003, 0x08, # special_effect
588+
# unknown
589+
0x370C, 0x02, #!!IMPORTANT
590+
0x3634, 0x40, #!!IMPORTANT
591+
# AEC/AGC
592+
0x3A02, 0x03,
593+
0x3A03, 0xD8,
594+
0x3A08, 0x01,
595+
0x3A09, 0x27,
596+
0x3A0A, 0x00,
597+
0x3A0B, 0xF6,
598+
0x3A0D, 0x04,
599+
0x3A0E, 0x03,
600+
0x3A0F, 0x30, # ae_level
601+
0x3A10, 0x28, # ae_level
602+
0x3A11, 0x60, # ae_level
603+
0x3A13, 0x43,
604+
0x3A14, 0x03,
605+
0x3A15, 0xD8,
606+
0x3A18, 0x00, # gainceiling
607+
0x3A19, 0xF8, # gainceiling
608+
0x3A1B, 0x30, # ae_level
609+
0x3A1E, 0x26, # ae_level
610+
0x3A1F, 0x14, # ae_level
611+
# vcm debug
612+
0x3600, 0x08,
613+
0x3601, 0x33,
614+
# 50/60Hz
615+
0x3C01, 0xA4,
616+
0x3C04, 0x28,
617+
0x3C05, 0x98,
618+
0x3C06, 0x00,
619+
0x3C07, 0x08,
620+
0x3C08, 0x00,
621+
0x3C09, 0x1C,
622+
0x3C0A, 0x9C,
623+
0x3C0B, 0x40,
624+
0x460C, 0x22, # disable jpeg footer
625+
# BLC
626+
0x4001, 0x02,
627+
0x4004, 0x02,
628+
# AWB
629+
0x5180, 0xFF,
630+
0x5181, 0xF2,
631+
0x5182, 0x00,
632+
0x5183, 0x14,
633+
0x5184, 0x25,
634+
0x5185, 0x24,
635+
0x5186, 0x09,
636+
0x5187, 0x09,
637+
0x5188, 0x09,
638+
0x5189, 0x75,
639+
0x518A, 0x54,
640+
0x518B, 0xE0,
641+
0x518C, 0xB2,
642+
0x518D, 0x42,
643+
0x518E, 0x3D,
644+
0x518F, 0x56,
645+
0x5190, 0x46,
646+
0x5191, 0xF8,
647+
0x5192, 0x04,
648+
0x5193, 0x70,
649+
0x5194, 0xF0,
650+
0x5195, 0xF0,
651+
0x5196, 0x03,
652+
0x5197, 0x01,
653+
0x5198, 0x04,
654+
0x5199, 0x12,
655+
0x519A, 0x04,
656+
0x519B, 0x00,
657+
0x519C, 0x06,
658+
0x519D, 0x82,
659+
0x519E, 0x38,
660+
# color matrix (Saturation)
661+
0x5381, 0x1E,
662+
0x5382, 0x5B,
663+
0x5383, 0x08,
664+
0x5384, 0x0A,
665+
0x5385, 0x7E,
666+
0x5386, 0x88,
667+
0x5387, 0x7C,
668+
0x5388, 0x6C,
669+
0x5389, 0x10,
670+
0x538A, 0x01,
671+
0x538B, 0x98,
672+
# CIP control (Sharpness)
673+
0x5300, 0x10, # sharpness
674+
0x5301, 0x10, # sharpness
675+
0x5302, 0x18, # sharpness
676+
0x5303, 0x19, # sharpness
677+
0x5304, 0x10,
678+
0x5305, 0x10,
679+
0x5306, 0x08, # denoise
680+
0x5307, 0x16,
681+
0x5308, 0x40,
682+
0x5309, 0x10, # sharpness
683+
0x530A, 0x10, # sharpness
684+
0x530B, 0x04, # sharpness
685+
0x530C, 0x06, # sharpness
686+
# GAMMA
687+
0x5480, 0x01,
688+
0x5481, 0x00,
689+
0x5482, 0x1E,
690+
0x5483, 0x3B,
691+
0x5484, 0x58,
692+
0x5485, 0x66,
693+
0x5486, 0x71,
694+
0x5487, 0x7D,
695+
0x5488, 0x83,
696+
0x5489, 0x8F,
697+
0x548A, 0x98,
698+
0x548B, 0xA6,
699+
0x548C, 0xB8,
700+
0x548D, 0xCA,
701+
0x548E, 0xD7,
702+
0x548F, 0xE3,
703+
0x5490, 0x1D,
704+
# Special Digital Effects (SDE) (UV adjust)
705+
0x5580, 0x06, # enable brightness and contrast
706+
0x5583, 0x40, # special_effect
707+
0x5584, 0x10, # special_effect
708+
0x5586, 0x20, # contrast
709+
0x5587, 0x00, # brightness
710+
0x5588, 0x00, # brightness
711+
0x5589, 0x10,
712+
0x558A, 0x00,
713+
0x558B, 0xF8,
714+
0x501D, 0x40, # enable manual offset of contrast
715+
]
543716
_sensor_format_jpeg = [
544717
_FORMAT_CTRL, 0x00, # YUV422
545718
_FORMAT_CTRL00, 0x30, # YUYV
546-
0x3002, 0x00, # 0x1c to 0x00 !!!
547-
0x3006, 0xFF, # 0xc3 to 0xff !!!
719+
_SYSTEM_RESET02, 0x00, # enable everything
720+
_CLOCK_ENABLE02, 0xFF, # enable all clocks
548721
0x471C, 0x50, # 0xd0 to 0x50 !!!
549722
]
550723

@@ -566,6 +739,9 @@
566739
_sensor_format_rgb565 = [
567740
_FORMAT_CTRL, 0x01, # RGB
568741
_FORMAT_CTRL00, 0x61, # RGB565 (BGR)
742+
_SYSTEM_RESET02, 0x1C, # reset jfifo, sfifo, jpg, fmux, avg
743+
_CLOCK_ENABLE02, 0xC3, # reset to how it was before (no jpg clock)
744+
569745
]
570746

571747
_ov5640_color_settings = {
@@ -822,8 +998,8 @@ def __init__(
822998
clock: Pin,
823999
vsync: Pin,
8241000
href: Pin,
825-
shutdown: Optional[Pin] = None,
826-
reset: Optional[Pin] = None,
1001+
shutdown: Optional[DigitalInOut] = None,
1002+
reset: Optional[DigitalInOut] = None,
8271003
mclk: Optional[Pin] = None,
8281004
mclk_frequency: int = 20_000_000,
8291005
i2c_address: int = 0x3C,
@@ -837,9 +1013,9 @@ def __init__(
8371013
vsync (microcontroller.Pin): The vsync signal from the OV5640.
8381014
href (microcontroller.Pin): The href signal from the OV5640, \
8391015
sometimes inaccurately called hsync.
840-
shutdown (Optional[microcontroller.Pin]): If not None, the shutdown
1016+
shutdown (Optional[digitalio.DigitalInOut]): If not None, the shutdown
8411017
signal to the camera, also called the powerdown or enable pin.
842-
reset (Optional[microcontroller.Pin]): If not None, the reset signal
1018+
reset (Optional[digitalio.DigitalInOut]): If not None, the reset signal
8431019
to the camera.
8441020
mclk (Optional[microcontroller.Pin]): The pin on which to create a
8451021
master clock signal, or None if the master clock signal is
@@ -861,23 +1037,24 @@ def __init__(
8611037
else:
8621038
self._mclk_pwm = None
8631039

1040+
if reset:
1041+
self._reset = reset
1042+
self._reset.switch_to_output(False)
1043+
else:
1044+
self._reset = None
1045+
8641046
if shutdown:
865-
self._shutdown = digitalio.DigitalInOut(shutdown)
1047+
self._shutdown = shutdown
8661048
self._shutdown.switch_to_output(True)
867-
time.sleep(0.1)
1049+
time.sleep(0.005) # t2, 5ms stability
8681050
self._shutdown.switch_to_output(False)
869-
time.sleep(0.3)
8701051
else:
8711052
self._shutdown = None
8721053

873-
if reset:
874-
self._reset = digitalio.DigitalInOut(reset)
875-
self._reset.switch_to_output(False)
876-
time.sleep(0.1)
1054+
if self._reset:
1055+
time.sleep(0.001) # t3, 1ms delay from pwdn
8771056
self._reset.switch_to_output(True)
878-
time.sleep(0.1)
879-
else:
880-
self._reset = None
1057+
time.sleep(0.02)
8811058

8821059
# Now that the master clock is running, we can initialize i2c comms
8831060
super().__init__(i2c_bus, i2c_address)

0 commit comments

Comments
 (0)