summaryrefslogtreecommitdiff
path: root/fw/fe310/eos/dev/drv/ov2640.c
diff options
context:
space:
mode:
Diffstat (limited to 'fw/fe310/eos/dev/drv/ov2640.c')
-rw-r--r--fw/fe310/eos/dev/drv/ov2640.c90
1 files changed, 39 insertions, 51 deletions
diff --git a/fw/fe310/eos/dev/drv/ov2640.c b/fw/fe310/eos/dev/drv/ov2640.c
index b801e20..5ed0b3e 100644
--- a/fw/fe310/eos/dev/drv/ov2640.c
+++ b/fw/fe310/eos/dev/drv/ov2640.c
@@ -1,17 +1,17 @@
+/*
+ * Sensor driver adapted from OpenMV project.
+*/
+
#include <stdlib.h>
#include <stdint.h>
#include <math.h>
-#include "eos.h"
-#include "soc/timer.h"
-#include "soc/i2c.h"
-
+#include "platform.h"
+#include "ov2640_platform.h"
#include "ov2640_regs.h"
+#include "ov2640_imlib.h"
#include "ov2640.h"
-#define XCLK_FREQ 24000000
-//#define XCLK_FREQ 12000000
-
#define CIF_WIDTH (400)
#define CIF_HEIGHT (296)
@@ -21,18 +21,6 @@
#define UXGA_WIDTH (1600)
#define UXGA_HEIGHT (1200)
-#define IM_LOG2_2(x) (((x) & 0x2ULL) ? ( 2 ) : 1) // NO ({ ... }) !
-#define IM_LOG2_4(x) (((x) & 0xCULL) ? ( 2 + IM_LOG2_2((x) >> 2)) : IM_LOG2_2(x)) // NO ({ ... }) !
-#define IM_LOG2_8(x) (((x) & 0xF0ULL) ? ( 4 + IM_LOG2_4((x) >> 4)) : IM_LOG2_4(x)) // NO ({ ... }) !
-#define IM_LOG2_16(x) (((x) & 0xFF00ULL) ? ( 8 + IM_LOG2_8((x) >> 8)) : IM_LOG2_8(x)) // NO ({ ... }) !
-#define IM_LOG2_32(x) (((x) & 0xFFFF0000ULL) ? (16 + IM_LOG2_16((x) >> 16)) : IM_LOG2_16(x)) // NO ({ ... }) !
-#define IM_LOG2(x) (((x) & 0xFFFFFFFF00000000ULL) ? (32 + IM_LOG2_32((x) >> 32)) : IM_LOG2_32(x)) // NO ({ ... }) !
-
-#define IM_MAX(a,b) ({ __typeof__ (a) _a = (a); __typeof__ (b) _b = (b); _a > _b ? _a : _b; })
-#define IM_MIN(a,b) ({ __typeof__ (a) _a = (a); __typeof__ (b) _b = (b); _a < _b ? _a : _b; })
-#define IM_DIV(a,b) ({ __typeof__ (a) _a = (a); __typeof__ (b) _b = (b); _b ? (_a / _b) : 0; })
-#define IM_MOD(a,b) ({ __typeof__ (a) _a = (a); __typeof__ (b) _b = (b); _b ? (_a % _b) : 0; })
-
static const int resolution_arr[][2] = {
{0, 0 },
// C/SIF Resolutions
@@ -391,18 +379,18 @@ static const uint8_t saturation_regs[NUM_SATURATION_LEVELS + 1][5] = {
};
static int reg_read(int8_t reg, uint8_t *data) {
- return eos_i2c_read8(OV2640_ADDR, reg, data, 1);
+ return drv_i2c_read8(OV2640_ADDR, reg, data, 1);
}
static int reg_write(uint8_t reg, uint8_t data) {
- return eos_i2c_write8(OV2640_ADDR, reg, &data, 1);
+ return drv_i2c_write8(OV2640_ADDR, reg, &data, 1);
}
static int regarr_write(const uint8_t (*regs)[2]) {
int i, rv;
i = 0;
- rv = EOS_OK;
+ rv = DRV_OK;
while ((regs[i][0] != 0xff) || (regs[i][1] != 0xff)) {
if (!rv) rv = reg_write(regs[i][0], regs[i][1]);
@@ -423,16 +411,16 @@ int ov2640_init(void) {
if (rv) return rv;
// Delay 5 ms
- eos_sleep(5);
+ drv_sleep(5);
// Write default regsiters
rv = regarr_write(default_regs);
if (rv) return rv;
// Delay 300 ms
- eos_sleep(300);
+ drv_sleep(300);
- return EOS_OK;
+ return DRV_OK;
}
int ov2640_sleep(int enable) {
@@ -473,7 +461,7 @@ int ov2640_set_pixfmt(cam_pixformat_t fmt) {
regs = jpeg_regs;
break;
default:
- return EOS_ERR;
+ return DRV_ERR;
}
return regarr_write(regs);
@@ -488,7 +476,7 @@ int ov2640_set_framesize(cam_framesize_t framesize) {
int rv;
if ((w % 4) || (h % 4) || (w > UXGA_WIDTH) || (h > UXGA_HEIGHT)) { // w/h must be divisble by 4
- return EOS_ERR;
+ return DRV_ERR;
}
// Looks really bad.
@@ -518,7 +506,7 @@ int ov2640_set_framesize(cam_framesize_t framesize) {
uint16_t x_off = (sensor_w - w_mul) / 2;
uint16_t y_off = (sensor_h - h_mul) / 2;
- rv = EOS_OK;
+ rv = DRV_OK;
if (!rv) rv = reg_write(CTRLI, CTRLI_LP_DP | CTRLI_V_DIV_SET(log_div) | CTRLI_H_DIV_SET(log_div));
if (!rv) rv = reg_write(HSIZE, HSIZE_SET(w_mul));
if (!rv) rv = reg_write(VSIZE, VSIZE_SET(h_mul));
@@ -536,11 +524,11 @@ int ov2640_set_framesize(cam_framesize_t framesize) {
}
int ov2640_set_contrast(int level) {
- int rv = EOS_OK;
+ int rv = DRV_OK;
level += (NUM_CONTRAST_LEVELS / 2) + 1;
if (level <= 0 || level > NUM_CONTRAST_LEVELS) {
- return EOS_ERR;
+ return DRV_ERR;
}
/* Switch to DSP register bank */
@@ -555,11 +543,11 @@ int ov2640_set_contrast(int level) {
}
int ov2640_set_brightness(int level) {
- int rv = EOS_OK;
+ int rv = DRV_OK;
level += (NUM_BRIGHTNESS_LEVELS / 2) + 1;
if (level <= 0 || level > NUM_BRIGHTNESS_LEVELS) {
- return EOS_ERR;
+ return DRV_ERR;
}
/* Switch to DSP register bank */
@@ -574,11 +562,11 @@ int ov2640_set_brightness(int level) {
}
int ov2640_set_saturation(int level) {
- int rv = EOS_OK;
+ int rv = DRV_OK;
level += (NUM_SATURATION_LEVELS / 2) + 1;
if (level <= 0 || level > NUM_SATURATION_LEVELS) {
- return EOS_ERR;
+ return DRV_ERR;
}
/* Switch to DSP register bank */
@@ -593,7 +581,7 @@ int ov2640_set_saturation(int level) {
}
int ov2640_set_gainceiling(cam_gainceiling_t gainceiling) {
- int rv = EOS_OK;
+ int rv = DRV_OK;
/* Switch to SENSOR register bank */
rv = reg_write(BANK_SEL, BANK_SEL_SENSOR);
@@ -605,7 +593,7 @@ int ov2640_set_gainceiling(cam_gainceiling_t gainceiling) {
}
int ov2640_set_quality(int qs) {
- int rv = EOS_OK;
+ int rv = DRV_OK;
/* Switch to DSP register bank */
rv = reg_write(BANK_SEL, BANK_SEL_DSP);
@@ -617,7 +605,7 @@ int ov2640_set_quality(int qs) {
}
int ov2640_set_colorbar(int enable) {
- int rv = EOS_OK;
+ int rv = DRV_OK;
uint8_t reg;
rv = reg_write(BANK_SEL, BANK_SEL_SENSOR);
@@ -636,7 +624,7 @@ int ov2640_set_colorbar(int enable) {
}
int ov2640_set_auto_gain(int enable, float gain_db, float gain_db_ceiling) {
- int rv = EOS_OK;
+ int rv = DRV_OK;
uint8_t reg;
rv = reg_write(BANK_SEL, BANK_SEL_SENSOR);
@@ -648,7 +636,7 @@ int ov2640_set_auto_gain(int enable, float gain_db, float gain_db_ceiling) {
rv = reg_write(COM8, (reg & (~COM8_AGC_EN)) | (enable ? COM8_AGC_EN : 0));
if (rv) return rv;
- rv = EOS_OK;
+ rv = DRV_OK;
if (enable && (!isnanf(gain_db_ceiling)) && (!isinff(gain_db_ceiling))) {
float gain_ceiling = IM_MAX(IM_MIN(expf((gain_db_ceiling / 20.0) * logf(10.0)), 128.0), 2.0);
@@ -670,7 +658,7 @@ int ov2640_set_auto_gain(int enable, float gain_db, float gain_db_ceiling) {
}
int ov2640_get_gain_db(float *gain_db) {
- int rv = EOS_OK;
+ int rv = DRV_OK;
uint8_t reg, gain;
rv = reg_write(BANK_SEL, BANK_SEL_SENSOR);
@@ -700,11 +688,11 @@ int ov2640_get_gain_db(float *gain_db) {
float lo_gain = 1.0 + (((gain >> 0) & 0xF) / 16.0);
*gain_db = 20.0 * (logf(hi_gain * lo_gain) / logf(10.0));
- return EOS_OK;
+ return DRV_OK;
}
int ov2640_set_auto_exposure(int enable, int exposure_us) {
- int rv = EOS_OK;
+ int rv = DRV_OK;
uint8_t reg;
rv = reg_write(BANK_SEL, BANK_SEL_SENSOR);
@@ -744,7 +732,7 @@ int ov2640_set_auto_exposure(int enable, int exposure_us) {
if (IMAGE_MODE_GET_FMT(reg) == IMAGE_MODE_RAW10) t_pclk = 1;
if (IMAGE_MODE_GET_FMT(reg) == IMAGE_MODE_RGB565) t_pclk = 2;
- int exposure = IM_MAX(IM_MIN(((exposure_us*(((XCLK_FREQ/clk_rc)*pll_mult)/1000000))/t_pclk)/t_line,0xFFFF),0x0000);
+ int exposure = IM_MAX(IM_MIN(((exposure_us*(((OV2640_CLK_FREQ/clk_rc)*pll_mult)/1000000))/t_pclk)/t_line,0xFFFF),0x0000);
if (!rv) rv = reg_write(BANK_SEL, BANK_SEL_SENSOR);
@@ -762,7 +750,7 @@ int ov2640_set_auto_exposure(int enable, int exposure_us) {
}
int ov2640_get_exposure_us(int *exposure_us) {
- int rv = EOS_OK;
+ int rv = DRV_OK;
uint8_t reg, aec_10, aec_92, aec_1510;
rv = reg_write(BANK_SEL, BANK_SEL_SENSOR);
@@ -822,13 +810,13 @@ int ov2640_get_exposure_us(int *exposure_us) {
if (IMAGE_MODE_GET_FMT(reg) == IMAGE_MODE_RGB565) t_pclk = 2;
uint16_t exposure = ((aec_1510 & 0x3F) << 10) + ((aec_92 & 0xFF) << 2) + ((aec_10 & 0x3) << 0);
- *exposure_us = (exposure*t_line*t_pclk)/(((XCLK_FREQ/clk_rc)*pll_mult)/1000000);
+ *exposure_us = (exposure*t_line*t_pclk)/(((OV2640_CLK_FREQ/clk_rc)*pll_mult)/1000000);
- return EOS_OK;
+ return DRV_OK;
}
int ov2640_set_auto_whitebal(int enable, float r_gain_db, float g_gain_db, float b_gain_db) {
- int rv = EOS_OK;
+ int rv = DRV_OK;
uint8_t reg;
rv = reg_write(BANK_SEL, BANK_SEL_DSP);
@@ -848,7 +836,7 @@ int ov2640_set_auto_whitebal(int enable, float r_gain_db, float g_gain_db, float
}
int ov2640_set_hmirror(int enable) {
- int rv = EOS_OK;
+ int rv = DRV_OK;
uint8_t reg;
rv = reg_write(BANK_SEL, BANK_SEL_SENSOR);
@@ -867,7 +855,7 @@ int ov2640_set_hmirror(int enable) {
}
int ov2640_set_vflip(int enable) {
- int rv = EOS_OK;
+ int rv = DRV_OK;
uint8_t reg;
rv = reg_write(BANK_SEL, BANK_SEL_SENSOR);
@@ -886,7 +874,7 @@ int ov2640_set_vflip(int enable) {
}
int ov2640_set_effect(cam_sde_t sde) {
- int rv = EOS_OK;
+ int rv = DRV_OK;
switch (sde) {
case CAM_SDE_NEGATIVE:
@@ -906,7 +894,7 @@ int ov2640_set_effect(cam_sde_t sde) {
if (!rv) rv = reg_write(BPDATA, 0x80);
break;
default:
- return EOS_ERR;
+ return DRV_ERR;
}
return rv;