diff --git a/src/omv/mutex.c b/src/omv/mutex.c index c5b54dbc8..413196d2b 100644 --- a/src/omv/mutex.c +++ b/src/omv/mutex.c @@ -7,10 +7,11 @@ * */ #include -#include -#include #include "mutex.h" +// This is a standard implementation of mutexs on ARM processors following the ARM guide. +// http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dai0321a/BIHEJCHB.html + void mutex_init(mutex_t *mutex) { mutex_unlock(mutex); diff --git a/src/omv/ringbuf.c b/src/omv/ringbuf.c index 329f46de5..e66330e67 100644 --- a/src/omv/ringbuf.c +++ b/src/omv/ringbuf.c @@ -7,7 +7,6 @@ * */ #include -#include #include "ringbuf.h" void ring_buf_init(ring_buf_t *buf) @@ -26,9 +25,9 @@ void ring_buf_put(ring_buf_t *buf, uint8_t c) /*buffer is full*/ return; } - + buf->data[buf->tail] = c; - buf->tail = (buf->tail + 1) % BUFFER_SIZE; + buf->tail = (buf->tail + 1) % BUFFER_SIZE; } uint8_t ring_buf_get(ring_buf_t *buf) @@ -38,7 +37,7 @@ uint8_t ring_buf_get(ring_buf_t *buf) /*buffer is empty*/ return 0; } - + c = buf->data[buf->head]; buf->head = (buf->head + 1) % BUFFER_SIZE; return c; diff --git a/src/omv/ringbuf.h b/src/omv/ringbuf.h index 3b0a7920c..f7a37663e 100644 --- a/src/omv/ringbuf.h +++ b/src/omv/ringbuf.h @@ -12,7 +12,7 @@ #define BUFFER_SIZE (1024) typedef struct ring_buffer { - volatile uint32_t head; + volatile uint32_t head; volatile uint32_t tail; uint8_t data[BUFFER_SIZE]; } ring_buf_t; diff --git a/src/omv/sccb.c b/src/omv/sccb.c index c6c6fa6ca..4c230649c 100644 --- a/src/omv/sccb.c +++ b/src/omv/sccb.c @@ -6,14 +6,14 @@ * SCCB (I2C like) driver. * */ +#include #include -#include "sccb.h" -#include "stdbool.h" -#include "systick.h" +#include #include "omv_boardconfig.h" -#define SCCB_FREQ (100000) -#define TIMEOUT (10000) - +#include "sccb.h" +#define SCCB_FREQ (100000) // We don't need fast I2C. 100KHz is fine here. +#define TIMEOUT (1000) /* Can't be sure when I2C routines return. Interrupts +while polling hardware may result in unknown delays. */ static I2C_HandleTypeDef I2CHandle; int SCCB_Init() @@ -24,7 +24,7 @@ int SCCB_Init() I2CHandle.Init.ClockSpeed = SCCB_FREQ; I2CHandle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED; I2CHandle.Init.DutyCycle = I2C_DUTYCYCLE_2; - I2CHandle.Init.GeneralCallMode = I2C_GENERALCALL_ENABLED; + I2CHandle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED; I2CHandle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLED; I2CHandle.Init.OwnAddress1 = 0xFE; I2CHandle.Init.OwnAddress2 = 0xFE; @@ -46,37 +46,35 @@ uint8_t SCCB_Probe() slv_addr = i; break; } - systick_sleep(1); + if (i!=126) { + systick_sleep(1); // Necessary for OV7725 camera (not for OV2640). + } } return slv_addr; } +uint8_t SCCB_Read(uint8_t slv_addr, uint8_t reg) +{ + uint8_t data=0; + + __disable_irq(); + if((HAL_I2C_Master_Transmit(&I2CHandle, slv_addr, ®, 1, TIMEOUT) != HAL_OK) + || (HAL_I2C_Master_Receive(&I2CHandle, slv_addr, &data, 1, TIMEOUT) != HAL_OK)) { + data=0xFF; + } + __enable_irq(); + return data; +} + uint8_t SCCB_Write(uint8_t slv_addr, uint8_t reg, uint8_t data) { uint8_t ret=0; uint8_t buf[] = {reg, data}; __disable_irq(); - if (HAL_I2C_Master_Transmit(&I2CHandle, slv_addr, buf, 2, TIMEOUT) != HAL_OK) { + if(HAL_I2C_Master_Transmit(&I2CHandle, slv_addr, buf, 2, TIMEOUT) != HAL_OK) { ret=0xFF; } __enable_irq(); return ret; } - -uint8_t SCCB_Read(uint8_t slv_addr, uint8_t reg) -{ - uint8_t data=0; - - __disable_irq(); - if (HAL_I2C_Master_Transmit(&I2CHandle, slv_addr, ®, 1, TIMEOUT) != HAL_OK) { - data = 0xFF; - goto error_w; - } - if (HAL_I2C_Master_Receive(&I2CHandle, slv_addr, &data, 1, TIMEOUT) != HAL_OK) { - data = 0xFF; - } -error_w: - __enable_irq(); - return data; -}