Skip to content

Commit

Permalink
Merge pull request #14 from adafruit/seesawify
Browse files Browse the repository at this point in the history
Seesawify & add a re-awb resetter
  • Loading branch information
jepler committed Mar 23, 2022
2 parents 5079b56 + 171db33 commit fbb0481
Showing 1 changed file with 198 additions and 21 deletions.
219 changes: 198 additions & 21 deletions adafruit_ov5640.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@
import time
import imagecapture
import pwmio
import digitalio
from adafruit_bus_device.i2c_device import I2CDevice

try:
from typing import Optional, Sequence, List, Union
from busio import I2C
from microcontroller import Pin
from digitalio import DigitalInOut
except ImportError:
pass

Expand All @@ -51,6 +51,37 @@
OV5640_COLOR_JPEG = 3

# fmt: off

_SYSTEM_RESET00 = const(0x3000) # Reset for Individual Block
# (0: enable block; 1: reset block)
# Bit[7]: Reset BIST
# Bit[6]: Reset MCU program memory
# Bit[5]: Reset MCU
# Bit[4]: Reset OTP
# Bit[3]: Reset STB
# Bit[2]: Reset d5060
# Bit[1]: Reset timing control
# Bit[0]: Reset array control

_SYSTEM_RESET02 = const(0x3002) # Reset for Individual Block
# (0: enable block; 1: reset block)
# Bit[7]: Reset VFIFO
# Bit[5]: Reset format
# Bit[4]: Reset JFIFO
# Bit[3]: Reset SFIFO
# Bit[2]: Reset JPG
# Bit[1]: Reset format MUX
# Bit[0]: Reset average

_CLOCK_ENABLE02 = const(0x3006) # Clock Enable Control
# (0: disable clock; 1: enable clock)
# Bit[7]: Enable PSRAM clock
# Bit[6]: Enable FMT clock
# Bit[5]: Enable JPEG 2x clock
# Bit[3]: Enable JPEG clock
# Bit[1]: Enable format MUX clock
# Bit[0]: Enable average clock

_SYSTEM_CTROL0 = const(0x3008)
# Bit[7]: Software reset
# Bit[6]: Software power down
Expand Down Expand Up @@ -397,11 +428,11 @@
0x4713, 0x02, # jpg mode select
_ISP_CONTROL_01, 0x83, # turn color matrix, awb and SDE
# sys reset
0x3000, 0x00,
0x3002, 0x1C,
_SYSTEM_RESET00, 0x00, # enable all blocks
_SYSTEM_RESET02, 0x1C, # reset jfifo, sfifo, jpg, fmux, avg
# clock enable
0x3004, 0xFF,
0x3006, 0xC3,
_CLOCK_ENABLE02, 0xC3,
# isp control
0x5000, 0xA7,
_ISP_CONTROL_01, 0xA3, # +scaling?
Expand Down Expand Up @@ -537,14 +568,156 @@
0x3008, 0x02,
# 50Hz
0x3C00, 0x04,
_REG_DLY, 300,
#_REG_DLY, 300,
]



_reset_awb = [
_ISP_CONTROL_01, 0x83, # turn color matrix, awb and SDE
# sys reset
_SYSTEM_RESET00, 0x00, # enable all blocks
_SYSTEM_RESET02, 0x1C, # reset jfifo, sfifo, jpg, fmux, avg
# clock enable
#0x3004, 0xFF,
#_CLOCK_ENABLE02, 0xC3,
# isp control
0x5000, 0xA7,
_ISP_CONTROL_01, 0xA3, # +scaling?
0x5003, 0x08, # special_effect
# unknown
0x370C, 0x02, #!!IMPORTANT
0x3634, 0x40, #!!IMPORTANT
# AEC/AGC
0x3A02, 0x03,
0x3A03, 0xD8,
0x3A08, 0x01,
0x3A09, 0x27,
0x3A0A, 0x00,
0x3A0B, 0xF6,
0x3A0D, 0x04,
0x3A0E, 0x03,
0x3A0F, 0x30, # ae_level
0x3A10, 0x28, # ae_level
0x3A11, 0x60, # ae_level
0x3A13, 0x43,
0x3A14, 0x03,
0x3A15, 0xD8,
0x3A18, 0x00, # gainceiling
0x3A19, 0xF8, # gainceiling
0x3A1B, 0x30, # ae_level
0x3A1E, 0x26, # ae_level
0x3A1F, 0x14, # ae_level
# vcm debug
0x3600, 0x08,
0x3601, 0x33,
# 50/60Hz
0x3C01, 0xA4,
0x3C04, 0x28,
0x3C05, 0x98,
0x3C06, 0x00,
0x3C07, 0x08,
0x3C08, 0x00,
0x3C09, 0x1C,
0x3C0A, 0x9C,
0x3C0B, 0x40,
0x460C, 0x22, # disable jpeg footer
# BLC
0x4001, 0x02,
0x4004, 0x02,
# AWB
0x5180, 0xFF,
0x5181, 0xF2,
0x5182, 0x00,
0x5183, 0x14,
0x5184, 0x25,
0x5185, 0x24,
0x5186, 0x09,
0x5187, 0x09,
0x5188, 0x09,
0x5189, 0x75,
0x518A, 0x54,
0x518B, 0xE0,
0x518C, 0xB2,
0x518D, 0x42,
0x518E, 0x3D,
0x518F, 0x56,
0x5190, 0x46,
0x5191, 0xF8,
0x5192, 0x04,
0x5193, 0x70,
0x5194, 0xF0,
0x5195, 0xF0,
0x5196, 0x03,
0x5197, 0x01,
0x5198, 0x04,
0x5199, 0x12,
0x519A, 0x04,
0x519B, 0x00,
0x519C, 0x06,
0x519D, 0x82,
0x519E, 0x38,
# color matrix (Saturation)
0x5381, 0x1E,
0x5382, 0x5B,
0x5383, 0x08,
0x5384, 0x0A,
0x5385, 0x7E,
0x5386, 0x88,
0x5387, 0x7C,
0x5388, 0x6C,
0x5389, 0x10,
0x538A, 0x01,
0x538B, 0x98,
# CIP control (Sharpness)
0x5300, 0x10, # sharpness
0x5301, 0x10, # sharpness
0x5302, 0x18, # sharpness
0x5303, 0x19, # sharpness
0x5304, 0x10,
0x5305, 0x10,
0x5306, 0x08, # denoise
0x5307, 0x16,
0x5308, 0x40,
0x5309, 0x10, # sharpness
0x530A, 0x10, # sharpness
0x530B, 0x04, # sharpness
0x530C, 0x06, # sharpness
# GAMMA
0x5480, 0x01,
0x5481, 0x00,
0x5482, 0x1E,
0x5483, 0x3B,
0x5484, 0x58,
0x5485, 0x66,
0x5486, 0x71,
0x5487, 0x7D,
0x5488, 0x83,
0x5489, 0x8F,
0x548A, 0x98,
0x548B, 0xA6,
0x548C, 0xB8,
0x548D, 0xCA,
0x548E, 0xD7,
0x548F, 0xE3,
0x5490, 0x1D,
# Special Digital Effects (SDE) (UV adjust)
0x5580, 0x06, # enable brightness and contrast
0x5583, 0x40, # special_effect
0x5584, 0x10, # special_effect
0x5586, 0x20, # contrast
0x5587, 0x00, # brightness
0x5588, 0x00, # brightness
0x5589, 0x10,
0x558A, 0x00,
0x558B, 0xF8,
0x501D, 0x40, # enable manual offset of contrast
]
_sensor_format_jpeg = [
_FORMAT_CTRL, 0x00, # YUV422
_FORMAT_CTRL00, 0x30, # YUYV
0x3002, 0x00, # 0x1c to 0x00 !!!
0x3006, 0xFF, # 0xc3 to 0xff !!!
_SYSTEM_RESET02, 0x00, # enable everything
_CLOCK_ENABLE02, 0xFF, # enable all clocks
0x471C, 0x50, # 0xd0 to 0x50 !!!
]

Expand All @@ -566,6 +739,9 @@
_sensor_format_rgb565 = [
_FORMAT_CTRL, 0x01, # RGB
_FORMAT_CTRL00, 0x61, # RGB565 (BGR)
_SYSTEM_RESET02, 0x1C, # reset jfifo, sfifo, jpg, fmux, avg
_CLOCK_ENABLE02, 0xC3, # reset to how it was before (no jpg clock)

]

_ov5640_color_settings = {
Expand Down Expand Up @@ -822,8 +998,8 @@ def __init__(
clock: Pin,
vsync: Pin,
href: Pin,
shutdown: Optional[Pin] = None,
reset: Optional[Pin] = None,
shutdown: Optional[DigitalInOut] = None,
reset: Optional[DigitalInOut] = None,
mclk: Optional[Pin] = None,
mclk_frequency: int = 20_000_000,
i2c_address: int = 0x3C,
Expand All @@ -837,9 +1013,9 @@ def __init__(
vsync (microcontroller.Pin): The vsync signal from the OV5640.
href (microcontroller.Pin): The href signal from the OV5640, \
sometimes inaccurately called hsync.
shutdown (Optional[microcontroller.Pin]): If not None, the shutdown
shutdown (Optional[digitalio.DigitalInOut]): If not None, the shutdown
signal to the camera, also called the powerdown or enable pin.
reset (Optional[microcontroller.Pin]): If not None, the reset signal
reset (Optional[digitalio.DigitalInOut]): If not None, the reset signal
to the camera.
mclk (Optional[microcontroller.Pin]): The pin on which to create a
master clock signal, or None if the master clock signal is
Expand All @@ -861,23 +1037,24 @@ def __init__(
else:
self._mclk_pwm = None

if reset:
self._reset = reset
self._reset.switch_to_output(False)
else:
self._reset = None

if shutdown:
self._shutdown = digitalio.DigitalInOut(shutdown)
self._shutdown = shutdown
self._shutdown.switch_to_output(True)
time.sleep(0.1)
time.sleep(0.005) # t2, 5ms stability
self._shutdown.switch_to_output(False)
time.sleep(0.3)
else:
self._shutdown = None

if reset:
self._reset = digitalio.DigitalInOut(reset)
self._reset.switch_to_output(False)
time.sleep(0.1)
if self._reset:
time.sleep(0.001) # t3, 1ms delay from pwdn
self._reset.switch_to_output(True)
time.sleep(0.1)
else:
self._reset = None
time.sleep(0.02)

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

0 comments on commit fbb0481

Please sign in to comment.