Arducam_OV5642_Library

Dependents:   DigitalCamera_OV5642_WIZwiki-W7500 RFID-RC522_buffer Prelude_OV5642_dev

Committer:
justinkim
Date:
Thu Oct 29 06:26:27 2015 +0000
Revision:
0:fc90ec271280
Child:
1:f2126346d524
spi test failed

Who changed what in which revision?

UserRevisionLine numberNew contents of line
justinkim 0:fc90ec271280 1 #ifndef MBED_ARDUCAM_H
justinkim 0:fc90ec271280 2 #define MBED_ARDUCAM_H
justinkim 0:fc90ec271280 3
justinkim 0:fc90ec271280 4 #include "mbed.h"
justinkim 0:fc90ec271280 5
justinkim 0:fc90ec271280 6 //******************************************************************************
justinkim 0:fc90ec271280 7 // Macros
justinkim 0:fc90ec271280 8 //******************************************************************************
justinkim 0:fc90ec271280 9 #define I2C_SEND_STOP 0x01
justinkim 0:fc90ec271280 10 #define I2C_SEND_START 0x02
justinkim 0:fc90ec271280 11 #define sensor_addr 0x78
justinkim 0:fc90ec271280 12 #define SPAWN_TASK_PRIORITY 9//9
justinkim 0:fc90ec271280 13 #define HTTP_SERVER_APP_TASK_PRIORITY 3//1
justinkim 0:fc90ec271280 14 #define CAMERA_SERVICE_PRIORITY 3
justinkim 0:fc90ec271280 15 #define OSI_STACK_SIZE 8 * 1024
justinkim 0:fc90ec271280 16
justinkim 0:fc90ec271280 17 #define cbi(reg, bitmask) *reg &= ~bitmask
justinkim 0:fc90ec271280 18 #define sbi(reg, bitmask) *reg |= bitmask
justinkim 0:fc90ec271280 19 #define pulse_high(reg, bitmask) sbi(reg, bitmask); cbi(reg, bitmask);
justinkim 0:fc90ec271280 20 #define pulse_low(reg, bitmask) cbi(reg, bitmask); sbi(reg, bitmask);
justinkim 0:fc90ec271280 21
justinkim 0:fc90ec271280 22 #define cport(port, data) port &= data
justinkim 0:fc90ec271280 23 #define sport(port, data) port |= data
justinkim 0:fc90ec271280 24
justinkim 0:fc90ec271280 25 #define swap(type, i, j) {type t = i; i = j; j = t;}
justinkim 0:fc90ec271280 26
justinkim 0:fc90ec271280 27 #define fontbyte(x) cfont.font[x]
justinkim 0:fc90ec271280 28
justinkim 0:fc90ec271280 29 #define regtype volatile uint32_t
justinkim 0:fc90ec271280 30 #define regsize uint32_t
justinkim 0:fc90ec271280 31 #define PROGMEM
justinkim 0:fc90ec271280 32
justinkim 0:fc90ec271280 33
justinkim 0:fc90ec271280 34 #define pgm_read_byte(x) (*((char *)x))
justinkim 0:fc90ec271280 35 // #define pgm_read_word(x) (*((short *)(x & 0xfffffffe)))
justinkim 0:fc90ec271280 36 #define pgm_read_word(x) ( ((*((unsigned char *)x + 1)) << 8) + (*((unsigned char *)x)))
justinkim 0:fc90ec271280 37 #define pgm_read_byte_near(x) (*((char *)x))
justinkim 0:fc90ec271280 38 #define pgm_read_byte_far(x) (*((char *)x))
justinkim 0:fc90ec271280 39 // #define pgm_read_word_near(x) (*((short *)(x & 0xfffffffe))
justinkim 0:fc90ec271280 40 // #define pgm_read_word_far(x) (*((short *)(x & 0xfffffffe)))
justinkim 0:fc90ec271280 41 #define pgm_read_word_near(x) ( ((*((unsigned char *)x + 1)) << 8) + (*((unsigned char *)x)))
justinkim 0:fc90ec271280 42 #define pgm_read_word_far(x) ( ((*((unsigned char *)x + 1)) << 8) + (*((unsigned char *)x))))
justinkim 0:fc90ec271280 43 #define PSTR(x) x
justinkim 0:fc90ec271280 44 #define F(X) (X)
justinkim 0:fc90ec271280 45
justinkim 0:fc90ec271280 46 #define BMP 0
justinkim 0:fc90ec271280 47 #define JPEG 1
justinkim 0:fc90ec271280 48
justinkim 0:fc90ec271280 49 #define OV5642 3
justinkim 0:fc90ec271280 50
justinkim 0:fc90ec271280 51 #define OV5642_320x240 1 //320x240
justinkim 0:fc90ec271280 52 #define OV5642_640x480 2 //640x480
justinkim 0:fc90ec271280 53 #define OV5642_1280x720 3 //1280x720
justinkim 0:fc90ec271280 54 #define OV5642_1920x1080 4 //1920x1080
justinkim 0:fc90ec271280 55 #define OV5642_2048x1563 5 //2048x1563
justinkim 0:fc90ec271280 56 #define OV5642_2592x1944 6 //2592x1944
justinkim 0:fc90ec271280 57
justinkim 0:fc90ec271280 58 /****************************************************/
justinkim 0:fc90ec271280 59 /* I2C Control Definition */
justinkim 0:fc90ec271280 60 /****************************************************/
justinkim 0:fc90ec271280 61 #define I2C_ADDR_8BIT 0
justinkim 0:fc90ec271280 62 #define I2C_ADDR_16BIT 1
justinkim 0:fc90ec271280 63 #define I2C_REG_8BIT 0
justinkim 0:fc90ec271280 64 #define I2C_REG_16BIT 1
justinkim 0:fc90ec271280 65 #define I2C_DAT_8BIT 0
justinkim 0:fc90ec271280 66 #define I2C_DAT_16BIT 1
justinkim 0:fc90ec271280 67
justinkim 0:fc90ec271280 68 /* Register initialization tables for SENSORs */
justinkim 0:fc90ec271280 69 /* Terminating list entry for reg */
justinkim 0:fc90ec271280 70 #define SENSOR_REG_TERM_8BIT 0xFF
justinkim 0:fc90ec271280 71 #define SENSOR_REG_TERM_16BIT 0xFFFF
justinkim 0:fc90ec271280 72 /* Terminating list entry for val */
justinkim 0:fc90ec271280 73 #define SENSOR_VAL_TERM_8BIT 0xFF
justinkim 0:fc90ec271280 74 #define SENSOR_VAL_TERM_16BIT 0xFFFF
justinkim 0:fc90ec271280 75
justinkim 0:fc90ec271280 76 /****************************************************/
justinkim 0:fc90ec271280 77 /* ArduChip related definition */
justinkim 0:fc90ec271280 78 /****************************************************/
justinkim 0:fc90ec271280 79 #define RWBIT 0x80 //READ AND WRITE BIT IS BIT[7]
justinkim 0:fc90ec271280 80
justinkim 0:fc90ec271280 81 #define ARDUCHIP_TEST1 0x00 //TEST register
justinkim 0:fc90ec271280 82 #define ARDUCHIP_TEST2 0x01 //TEST register
justinkim 0:fc90ec271280 83
justinkim 0:fc90ec271280 84 #define ARDUCHIP_FRAMES 0x01 //Bit[2:0]Number of frames to be captured
justinkim 0:fc90ec271280 85
justinkim 0:fc90ec271280 86 #define ARDUCHIP_MODE 0x02 //Mode register
justinkim 0:fc90ec271280 87 #define MCU2LCD_MODE 0x00
justinkim 0:fc90ec271280 88 #define CAM2LCD_MODE 0x01
justinkim 0:fc90ec271280 89 #define LCD2MCU_MODE 0x02
justinkim 0:fc90ec271280 90
justinkim 0:fc90ec271280 91 #define ARDUCHIP_TIM 0x03 //Timming control
justinkim 0:fc90ec271280 92 #define HREF_LEVEL_MASK 0x01 //0 = High active , 1 = Low active
justinkim 0:fc90ec271280 93 #define VSYNC_LEVEL_MASK 0x02 //0 = High active , 1 = Low active
justinkim 0:fc90ec271280 94 #define LCD_BKEN_MASK 0x04 //0 = Enable, 1 = Disable
justinkim 0:fc90ec271280 95 #define DELAY_MASK 0x08 //0 = no delay, 1 = delay one clock
justinkim 0:fc90ec271280 96 #define MODE_MASK 0x10 //0 = LCD mode, 1 = FIFO mode
justinkim 0:fc90ec271280 97 #define FIFO_PWRDN_MASK 0x20 //0 = Normal operation, 1 = FIFO power down
justinkim 0:fc90ec271280 98 #define LOW_POWER_MODE 0x40 //0 = Normal mode, 1 = Low power mode
justinkim 0:fc90ec271280 99
justinkim 0:fc90ec271280 100 #define ARDUCHIP_FIFO 0x04 //FIFO and I2C control
justinkim 0:fc90ec271280 101 #define FIFO_CLEAR_MASK 0x01
justinkim 0:fc90ec271280 102 #define FIFO_START_MASK 0x02
justinkim 0:fc90ec271280 103 #define FIFO_RDPTR_RST_MASK 0x10
justinkim 0:fc90ec271280 104 #define FIFO_WRPTR_RST_MASK 0x20
justinkim 0:fc90ec271280 105
justinkim 0:fc90ec271280 106 #define ARDUCHIP_GPIO 0x06 //GPIO Write Register
justinkim 0:fc90ec271280 107 #define GPIO_RESET_MASK 0x01 //0 = default state, 1 = Sensor reset IO value
justinkim 0:fc90ec271280 108 #define GPIO_PWDN_MASK 0x02 //0 = Sensor power down IO value, 1 = Sensor power enable IO value
justinkim 0:fc90ec271280 109
justinkim 0:fc90ec271280 110 #define BURST_FIFO_READ 0x3C //Burst FIFO read operation
justinkim 0:fc90ec271280 111 #define SINGLE_FIFO_READ 0x3D //Single FIFO read operation
justinkim 0:fc90ec271280 112
justinkim 0:fc90ec271280 113 #define ARDUCHIP_REV 0x40 //ArduCHIP revision
justinkim 0:fc90ec271280 114 #define VER_LOW_MASK 0x3F
justinkim 0:fc90ec271280 115 #define VER_HIGH_MASK 0xC0
justinkim 0:fc90ec271280 116
justinkim 0:fc90ec271280 117 #define ARDUCHIP_TRIG 0x41 //Trigger source
justinkim 0:fc90ec271280 118 #define VSYNC_MASK 0x01
justinkim 0:fc90ec271280 119 #define SHUTTER_MASK 0x02
justinkim 0:fc90ec271280 120 #define CAP_DONE_MASK 0x08
justinkim 0:fc90ec271280 121
justinkim 0:fc90ec271280 122 #define FIFO_SIZE1 0x42 //Camera write FIFO size[7:0] for burst to read
justinkim 0:fc90ec271280 123 #define FIFO_SIZE2 0x43 //Camera write FIFO size[15:8]
justinkim 0:fc90ec271280 124 #define FIFO_SIZE3 0x44 //Camera write FIFO size[18:16
justinkim 0:fc90ec271280 125
justinkim 0:fc90ec271280 126 class ArduCAM
justinkim 0:fc90ec271280 127 {
justinkim 0:fc90ec271280 128 public:
justinkim 0:fc90ec271280 129 ArduCAM(PinName mosi, PinName miso, PinName sck, PinName cs, PinName sda, PinName scl);
justinkim 0:fc90ec271280 130 void InitCAM(void);
justinkim 0:fc90ec271280 131
justinkim 0:fc90ec271280 132 void CS_HIGH(void);
justinkim 0:fc90ec271280 133 void CS_LOW(void);
justinkim 0:fc90ec271280 134
justinkim 0:fc90ec271280 135 void flush_fifo(void);
justinkim 0:fc90ec271280 136 void start_capture(void);
justinkim 0:fc90ec271280 137 void clear_fifo_flag(void);
justinkim 0:fc90ec271280 138 uint8_t read_fifo(void);
justinkim 0:fc90ec271280 139
justinkim 0:fc90ec271280 140 uint8_t read_reg(uint8_t addr);
justinkim 0:fc90ec271280 141 void write_reg(uint8_t addr, uint8_t data);
justinkim 0:fc90ec271280 142
justinkim 0:fc90ec271280 143 uint32_t read_fifo_length(void);
justinkim 0:fc90ec271280 144 void set_fifo_burst(void);
justinkim 0:fc90ec271280 145 void set_bit(uint8_t addr, uint8_t bit);
justinkim 0:fc90ec271280 146 void clear_bit(uint8_t addr, uint8_t bit);
justinkim 0:fc90ec271280 147 uint8_t get_bit(uint8_t addr, uint8_t bit);
justinkim 0:fc90ec271280 148 void set_mode(uint8_t mode);
justinkim 0:fc90ec271280 149
justinkim 0:fc90ec271280 150 int wrSensorRegs(const struct sensor_reg*);
justinkim 0:fc90ec271280 151 int wrSensorRegs8_8(const struct sensor_reg*);
justinkim 0:fc90ec271280 152 int wrSensorRegs8_16(const struct sensor_reg*);
justinkim 0:fc90ec271280 153 int wrSensorRegs16_8(const struct sensor_reg*);
justinkim 0:fc90ec271280 154 int wrSensorRegs16_16(const struct sensor_reg*);
justinkim 0:fc90ec271280 155
justinkim 0:fc90ec271280 156 uint8_t wrSensorReg(int regID, int regDat);
justinkim 0:fc90ec271280 157 uint8_t wrSensorReg8_8(int regID, int regDat);
justinkim 0:fc90ec271280 158 uint8_t wrSensorReg8_16(int regID, int regDat);
justinkim 0:fc90ec271280 159 uint8_t wrSensorReg16_8(int regID, int regDat);
justinkim 0:fc90ec271280 160 uint8_t wrSensorReg16_16(int regID, int regDat);
justinkim 0:fc90ec271280 161
justinkim 0:fc90ec271280 162 uint8_t rdSensorReg8_8(uint8_t regID, uint8_t* regDat);
justinkim 0:fc90ec271280 163 uint8_t rdSensorReg16_8(uint16_t regID, uint8_t* regDat);
justinkim 0:fc90ec271280 164 uint8_t rdSensorReg8_16(uint8_t regID, uint16_t* regDat);
justinkim 0:fc90ec271280 165 uint8_t rdSensorReg16_16(uint16_t regID, uint16_t* regDat);
justinkim 0:fc90ec271280 166
justinkim 0:fc90ec271280 167 void OV5642_set_JPEG_size(void);
justinkim 0:fc90ec271280 168 void set_format(uint8_t fmt);
justinkim 0:fc90ec271280 169
justinkim 0:fc90ec271280 170 int bus_write(int address, int value);
justinkim 0:fc90ec271280 171 uint8_t bus_read(int address);
justinkim 0:fc90ec271280 172
justinkim 0:fc90ec271280 173 protected:
justinkim 0:fc90ec271280 174 SPI spi; // does SPI MOSI, MISO and SCK
justinkim 0:fc90ec271280 175 DigitalOut _cs; // does SPI CE
justinkim 0:fc90ec271280 176 I2C i2c; // does I2C SDA, SCL
justinkim 0:fc90ec271280 177 regtype *P_CS;
justinkim 0:fc90ec271280 178 regsize B_CS;
justinkim 0:fc90ec271280 179 uint8_t m_fmt;
justinkim 0:fc90ec271280 180 uint8_t sensor_model;
justinkim 0:fc90ec271280 181
justinkim 0:fc90ec271280 182 uint32_t I2CInit(void);
justinkim 0:fc90ec271280 183 int32_t I2CBufferRead(int32_t ucDevAddr, uint8_t *ucBuffer, int32_t ulSize,unsigned char ucFlags);
justinkim 0:fc90ec271280 184 int32_t I2CBufferWrite(int32_t ucDevAddr, uint8_t *ucBuffer, int32_t ulSize,unsigned char ucFlags);
justinkim 0:fc90ec271280 185 };
justinkim 0:fc90ec271280 186
justinkim 0:fc90ec271280 187 #endif