diff --git a/libraries/GC2145/gc2145.cpp b/libraries/GC2145/gc2145.cpp index 5c1ec4c0a..0d4115c05 100644 --- a/libraries/GC2145/gc2145.cpp +++ b/libraries/GC2145/gc2145.cpp @@ -707,8 +707,16 @@ static const uint8_t default_regs[][2] = { {0x00, 0x00}, }; +GC2145::GC2145(arduino::MbedI2C &i2c) : + _i2c(&i2c) +{ +} + int GC2145::Init() { + _i2c->begin(); + _i2c->setClock(100000); + int ret = 0; // Write default regsiters @@ -853,32 +861,32 @@ int GC2145::SetPixelFormat(int32_t pixformat) int GC2145::reg_write(uint8_t dev_addr, uint16_t reg_addr, uint8_t reg_data, bool wide_addr) { - Wire2.beginTransmission(dev_addr); + _i2c->beginTransmission(dev_addr); uint8_t buf[3] = {(uint8_t) (reg_addr >> 8), (uint8_t) (reg_addr & 0xFF), reg_data}; if (wide_addr == true) { - Wire2.write(buf, 1); + _i2c->write(buf, 1); } - Wire2.write(&buf[1], 2); - return Wire2.endTransmission(); + _i2c->write(&buf[1], 2); + return _i2c->endTransmission(); } uint8_t GC2145::reg_read(uint8_t dev_addr, uint16_t reg_addr, bool wide_addr) { uint8_t reg_data = 0; uint8_t buf[2] = {(uint8_t) (reg_addr >> 8), (uint8_t) (reg_addr & 0xFF)}; - Wire2.beginTransmission(dev_addr); + _i2c->beginTransmission(dev_addr); if (wide_addr) { - Wire2.write(buf, 2); + _i2c->write(buf, 2); } else { - Wire2.write(&buf[1], 1); + _i2c->write(&buf[1], 1); } - Wire2.endTransmission(false); - Wire2.requestFrom(dev_addr, 1); - if (Wire2.available()) { - reg_data = Wire2.read(); + _i2c->endTransmission(false); + _i2c->requestFrom(dev_addr, 1); + if (_i2c->available()) { + reg_data = _i2c->read(); } - while (Wire2.available()) { - Wire2.read(); + while (_i2c->available()) { + _i2c->read(); } return reg_data; } diff --git a/libraries/GC2145/gc2145.h b/libraries/GC2145/gc2145.h index 91d843842..dffac4962 100644 --- a/libraries/GC2145/gc2145.h +++ b/libraries/GC2145/gc2145.h @@ -25,8 +25,10 @@ class GC2145: public ImageSensor { private: int SetWindow(uint16_t reg, uint16_t x, uint16_t y, uint16_t w, uint16_t h); Stream *_debug; + arduino::MbedI2C *_i2c; public: + GC2145(arduino::MbedI2C &i2c = CameraWire); int Init(); int Reset(); int GetID() { return GC2145_I2C_ADDR; }; diff --git a/libraries/Himax_HM01B0/himax.cpp b/libraries/Himax_HM01B0/himax.cpp index 9b78d9a20..9a79e4949 100644 --- a/libraries/Himax_HM01B0/himax.cpp +++ b/libraries/Himax_HM01B0/himax.cpp @@ -293,8 +293,16 @@ static const uint16_t himax_qqvga_regs[][2] = { {0x0000, 0x00}, // EOF }; +HM01B0::HM01B0(arduino::MbedI2C &i2c) : + _i2c(&i2c) +{ +} + int HM01B0::Init() { + _i2c->begin(); + _i2c->setClock(100000); + if (Reset() != 0 ) { return -1; } @@ -461,32 +469,32 @@ uint8_t HM01B0::PrintRegs() int HM01B0::reg_write(uint8_t dev_addr, uint16_t reg_addr, uint8_t reg_data, bool wide_addr) { - Wire.beginTransmission(dev_addr); + _i2c->beginTransmission(dev_addr); uint8_t buf[3] = {(uint8_t) (reg_addr >> 8), (uint8_t) (reg_addr & 0xFF), reg_data}; if (wide_addr == true) { - Wire.write(buf, 1); + _i2c->write(buf, 1); } - Wire.write(&buf[1], 2); - return Wire.endTransmission(); + _i2c->write(&buf[1], 2); + return _i2c->endTransmission(); } uint8_t HM01B0::reg_read(uint8_t dev_addr, uint16_t reg_addr, bool wide_addr) { uint8_t reg_data = 0; uint8_t buf[2] = {(uint8_t) (reg_addr >> 8), (uint8_t) (reg_addr & 0xFF)}; - Wire.beginTransmission(dev_addr); + _i2c->beginTransmission(dev_addr); if (wide_addr) { - Wire.write(buf, 2); + _i2c->write(buf, 2); } else { - Wire.write(&buf[1], 1); + _i2c->write(&buf[1], 1); } - Wire.endTransmission(false); - Wire.requestFrom(dev_addr, 1); - if (Wire.available()) { - reg_data = Wire.read(); + _i2c->endTransmission(false); + _i2c->requestFrom(dev_addr, 1); + if (_i2c->available()) { + reg_data = _i2c->read(); } - while (Wire.available()) { - Wire.read(); + while (_i2c->available()) { + _i2c->read(); } return reg_data; } diff --git a/libraries/Himax_HM01B0/himax.h b/libraries/Himax_HM01B0/himax.h index 572d9f145..da5637c25 100644 --- a/libraries/Himax_HM01B0/himax.h +++ b/libraries/Himax_HM01B0/himax.h @@ -24,7 +24,9 @@ class HM01B0: public ImageSensor { private: Stream *_debug; + arduino::MbedI2C *_i2c; public: + HM01B0(arduino::MbedI2C &i2c = CameraWire); int Init(); int Reset(); int GetID() { return HM01B0_I2C_ADDR; }; diff --git a/libraries/Portenta_Camera/examples/CameraCaptureRawBytes/CameraCaptureRawBytes.ino b/libraries/Portenta_Camera/examples/CameraCaptureRawBytes/CameraCaptureRawBytes.ino index 8a339f761..841f1d198 100644 --- a/libraries/Portenta_Camera/examples/CameraCaptureRawBytes/CameraCaptureRawBytes.ino +++ b/libraries/Portenta_Camera/examples/CameraCaptureRawBytes/CameraCaptureRawBytes.ino @@ -3,13 +3,7 @@ //REDIRECT_STDOUT_TO(Serial); -// Uncomment for HM01B0 -#include "himax.h" -Camera cam(new HM01B0()); - -// Uncomment to use GC2145 instead -//#include "gc2145.h" -//Camera cam(new GC2145()); +Camera cam; uint8_t *fb = (uint8_t*) SDRAM_START_ADDRESS; diff --git a/libraries/Portenta_Camera/src/camera.cpp b/libraries/Portenta_Camera/src/camera.cpp index 8309e04fa..d103d7cb9 100644 --- a/libraries/Portenta_Camera/src/camera.cpp +++ b/libraries/Portenta_Camera/src/camera.cpp @@ -27,7 +27,6 @@ // Include all image sensor drivers here. #ifdef ARDUINO_PORTENTA_H7_M7 -#include "himax.h" #define DCMI_TIM (TIM1) #define DCMI_TIM_PIN (GPIO_PIN_1) @@ -38,10 +37,10 @@ #define DCMI_TIM_CLK_DISABLE() __TIM1_CLK_DISABLE() #define DCMI_TIM_PCLK_FREQ() HAL_RCC_GetPCLK2Freq() #define DCMI_TIM_FREQUENCY (6000000) -#endif +arduino::MbedI2C CameraWire(I2C_SDA, I2C_SCL); + +#elif defined(ARDUINO_NICLA_VISION) -#ifdef ARDUINO_NICLA_VISION -#include "gc2145.h" #define DCMI_TIM (TIM3) #define DCMI_TIM_PIN (GPIO_PIN_7) #define DCMI_TIM_PORT (GPIOA) @@ -51,6 +50,8 @@ #define DCMI_TIM_CLK_DISABLE() __TIM3_CLK_DISABLE() #define DCMI_TIM_PCLK_FREQ() HAL_RCC_GetPCLK1Freq() #define DCMI_TIM_FREQUENCY (12000000) +arduino::MbedI2C CameraWire(I2C_SDA2, I2C_SCL2); + #endif #define DCMI_IRQ_PRI NVIC_EncodePriority(NVIC_PRIORITYGROUP_4, 2, 0) @@ -299,6 +300,15 @@ void HAL_DCMI_FrameEventCallback(DCMI_HandleTypeDef *hdcmi) } // extern "C" +Camera::Camera(ImageSensor &sensor) : + pixformat(-1), + resolution(-1), + framerate(-1), + sensor(&sensor), + _debug(NULL) +{ +} + int Camera::Reset() { #ifdef ARDUINO_PORTENTA_H7_M7 @@ -311,7 +321,7 @@ int Camera::Reset() #endif return 0; } - +/* int Camera::ProbeSensor() { uint8_t addr; @@ -337,7 +347,7 @@ int Camera::ProbeSensor() #endif //ARDUINO_NICLA_VISION return addr; } - +*/ int Camera::begin(int32_t resolution, int32_t pixformat, int32_t framerate) { if (resolution >= CAMERA_RMAX || pixformat >= CAMERA_PMAX) { @@ -351,24 +361,15 @@ int Camera::begin(int32_t resolution, int32_t pixformat, int32_t framerate) // Reset the image sensor. Reset(); - // Start I2C -#ifdef ARDUINO_PORTENTA_H7_M7 - Wire.begin(); - Wire.setClock(400000); -#endif - -#ifdef ARDUINO_NICLA_VISION - Wire2.begin(); - Wire2.setClock(100000); -#endif - - //if (ProbeSensor() != this->sensor->GetID()) { + /* + if (ProbeSensor() != this->sensor->GetID()) { if (_debug) { _debug->print("SensorID: 0x"); _debug->println(this->sensor->GetID(), HEX); } - //return -1; - //} + return -1; + } + */ if (sensor->GetClockFrequency() != DCMI_TIM_FREQUENCY) { // Reconfigure the sensor clock frequency. diff --git a/libraries/Portenta_Camera/src/camera.h b/libraries/Portenta_Camera/src/camera.h index 5ba959c08..bc3f9e08f 100644 --- a/libraries/Portenta_Camera/src/camera.h +++ b/libraries/Portenta_Camera/src/camera.h @@ -45,6 +45,7 @@ extern const uint32_t restab[CAMERA_RMAX][2]; class ImageSensor { public: + virtual ~ImageSensor() { } virtual int Init() = 0; virtual int Reset() = 0; virtual int GetID() = 0; @@ -78,8 +79,9 @@ class Camera { int Reset(); int ProbeSensor(); Stream *_debug; + arduino::MbedI2C *_i2c; public: - Camera(ImageSensor *sensor): pixformat(-1), resolution(-1), framerate(-1), sensor(sensor) {} + Camera(ImageSensor &sensor); int begin(int32_t resolution=CAMERA_R320x240, int32_t pixformat=CAMERA_GRAYSCALE, int32_t framerate=30); int GetID(); int SetFrameRate(int32_t framerate); @@ -91,4 +93,7 @@ class Camera { int GrabFrame(uint8_t *buffer, uint32_t timeout=5000); void debug(Stream &stream); }; + #endif // __CAMERA_H + +extern arduino::MbedI2C CameraWire;