Arducam_OV5642_Library
Dependents: DigitalCamera_OV5642_WIZwiki-W7500 RFID-RC522_buffer Prelude_OV5642_dev
OV5642.cpp@2:8cb6d03dccb8, 2015-10-29 (annotated)
- Committer:
- justinkim
- Date:
- Thu Oct 29 07:56:55 2015 +0000
- Revision:
- 2:8cb6d03dccb8
- Parent:
- 0:fc90ec271280
- Child:
- 3:ee782b77485e
version0.1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
justinkim | 0:fc90ec271280 | 1 | #include "mbed.h" |
justinkim | 0:fc90ec271280 | 2 | #include "OV5642_regs.h" |
justinkim | 0:fc90ec271280 | 3 | #include "OV5642.h" |
justinkim | 0:fc90ec271280 | 4 | |
justinkim | 0:fc90ec271280 | 5 | #define ENABLE_JPEG |
justinkim | 0:fc90ec271280 | 6 | |
justinkim | 0:fc90ec271280 | 7 | ArduCAM::ArduCAM(PinName mosi, PinName miso, PinName sck, PinName cs, PinName sda, PinName scl) |
justinkim | 0:fc90ec271280 | 8 | : spi(mosi,miso,sck), _cs(cs), i2c(sda, scl) |
justinkim | 0:fc90ec271280 | 9 | { |
justinkim | 0:fc90ec271280 | 10 | _cs = 1; |
justinkim | 0:fc90ec271280 | 11 | } |
justinkim | 0:fc90ec271280 | 12 | |
justinkim | 0:fc90ec271280 | 13 | //Assert CS signal |
justinkim | 0:fc90ec271280 | 14 | void ArduCAM::CS_LOW(void) |
justinkim | 0:fc90ec271280 | 15 | { |
justinkim | 0:fc90ec271280 | 16 | _cs = 0; |
justinkim | 0:fc90ec271280 | 17 | } |
justinkim | 0:fc90ec271280 | 18 | |
justinkim | 0:fc90ec271280 | 19 | //Disable CS signal |
justinkim | 0:fc90ec271280 | 20 | void ArduCAM::CS_HIGH(void) |
justinkim | 0:fc90ec271280 | 21 | { |
justinkim | 0:fc90ec271280 | 22 | _cs = 1; |
justinkim | 0:fc90ec271280 | 23 | } |
justinkim | 0:fc90ec271280 | 24 | |
justinkim | 0:fc90ec271280 | 25 | //Reset the FIFO pointer to ZERO |
justinkim | 0:fc90ec271280 | 26 | void ArduCAM::flush_fifo(void) |
justinkim | 0:fc90ec271280 | 27 | { |
justinkim | 0:fc90ec271280 | 28 | write_reg(ARDUCHIP_FIFO, FIFO_CLEAR_MASK); |
justinkim | 0:fc90ec271280 | 29 | } |
justinkim | 0:fc90ec271280 | 30 | |
justinkim | 0:fc90ec271280 | 31 | //Send capture command |
justinkim | 0:fc90ec271280 | 32 | void ArduCAM::start_capture(void) |
justinkim | 0:fc90ec271280 | 33 | { |
justinkim | 0:fc90ec271280 | 34 | write_reg(ARDUCHIP_FIFO, FIFO_START_MASK); |
justinkim | 0:fc90ec271280 | 35 | } |
justinkim | 0:fc90ec271280 | 36 | |
justinkim | 0:fc90ec271280 | 37 | //Clear FIFO Complete flag |
justinkim | 0:fc90ec271280 | 38 | void ArduCAM::clear_fifo_flag(void) |
justinkim | 0:fc90ec271280 | 39 | { |
justinkim | 0:fc90ec271280 | 40 | write_reg(ARDUCHIP_FIFO, FIFO_CLEAR_MASK); |
justinkim | 0:fc90ec271280 | 41 | } |
justinkim | 0:fc90ec271280 | 42 | |
justinkim | 0:fc90ec271280 | 43 | //Read FIFO single |
justinkim | 0:fc90ec271280 | 44 | uint8_t ArduCAM::read_fifo(void) |
justinkim | 0:fc90ec271280 | 45 | { |
justinkim | 0:fc90ec271280 | 46 | uint8_t data; |
justinkim | 0:fc90ec271280 | 47 | data = bus_read(SINGLE_FIFO_READ); |
justinkim | 0:fc90ec271280 | 48 | return data; |
justinkim | 0:fc90ec271280 | 49 | } |
justinkim | 0:fc90ec271280 | 50 | |
justinkim | 0:fc90ec271280 | 51 | //Read Write FIFO length |
justinkim | 0:fc90ec271280 | 52 | //Support ArduCAM Mini only |
justinkim | 0:fc90ec271280 | 53 | uint32_t ArduCAM::read_fifo_length(void) |
justinkim | 0:fc90ec271280 | 54 | { |
justinkim | 0:fc90ec271280 | 55 | uint32_t len1,len2,len3,length=0; |
justinkim | 0:fc90ec271280 | 56 | len1 = read_reg(FIFO_SIZE1); |
justinkim | 0:fc90ec271280 | 57 | len2 = read_reg(FIFO_SIZE2); |
justinkim | 0:fc90ec271280 | 58 | len3 = read_reg(FIFO_SIZE3) & 0x07; |
justinkim | 0:fc90ec271280 | 59 | length = ((len3 << 16) | (len2 << 8) | len1) & 0x07ffff; |
justinkim | 0:fc90ec271280 | 60 | return length; |
justinkim | 0:fc90ec271280 | 61 | } |
justinkim | 0:fc90ec271280 | 62 | |
justinkim | 0:fc90ec271280 | 63 | //Send read fifo burst command |
justinkim | 0:fc90ec271280 | 64 | //Support ArduCAM Mini only |
justinkim | 0:fc90ec271280 | 65 | void ArduCAM::set_fifo_burst() |
justinkim | 0:fc90ec271280 | 66 | { |
justinkim | 0:fc90ec271280 | 67 | spi.write(BURST_FIFO_READ); |
justinkim | 0:fc90ec271280 | 68 | } |
justinkim | 0:fc90ec271280 | 69 | |
justinkim | 2:8cb6d03dccb8 | 70 | void ArduCAM::InitCAM() |
justinkim | 2:8cb6d03dccb8 | 71 | { |
justinkim | 2:8cb6d03dccb8 | 72 | uint8_t reg_val; |
justinkim | 2:8cb6d03dccb8 | 73 | |
justinkim | 2:8cb6d03dccb8 | 74 | wrSensorReg16_8(0x3008, 0x80); |
justinkim | 2:8cb6d03dccb8 | 75 | |
justinkim | 2:8cb6d03dccb8 | 76 | wait_ms(100); |
justinkim | 2:8cb6d03dccb8 | 77 | if(m_fmt == JPEG) |
justinkim | 2:8cb6d03dccb8 | 78 | { |
justinkim | 2:8cb6d03dccb8 | 79 | wrSensorRegs16_8(ov5642_dvp_fmt_global_init); |
justinkim | 2:8cb6d03dccb8 | 80 | wait_ms(100); |
justinkim | 2:8cb6d03dccb8 | 81 | wrSensorRegs16_8(ov5642_dvp_fmt_jpeg_qvga); |
justinkim | 2:8cb6d03dccb8 | 82 | wrSensorReg16_8(0x4407,0x0C); |
justinkim | 2:8cb6d03dccb8 | 83 | } |
justinkim | 2:8cb6d03dccb8 | 84 | else |
justinkim | 2:8cb6d03dccb8 | 85 | { |
justinkim | 2:8cb6d03dccb8 | 86 | wrSensorRegs16_8(OV5642_RGB_QVGA); |
justinkim | 2:8cb6d03dccb8 | 87 | rdSensorReg16_8(0x3818,®_val); |
justinkim | 2:8cb6d03dccb8 | 88 | wrSensorReg16_8(0x3818, (reg_val | 0x60) & 0xff); |
justinkim | 2:8cb6d03dccb8 | 89 | rdSensorReg16_8(0x3621,®_val); |
justinkim | 2:8cb6d03dccb8 | 90 | wrSensorReg16_8(0x3621, reg_val & 0xdf); |
justinkim | 2:8cb6d03dccb8 | 91 | } |
justinkim | 0:fc90ec271280 | 92 | } |
justinkim | 0:fc90ec271280 | 93 | |
justinkim | 0:fc90ec271280 | 94 | //Write ArduChip internal registers |
justinkim | 0:fc90ec271280 | 95 | void ArduCAM::write_reg(uint8_t addr, uint8_t data) |
justinkim | 0:fc90ec271280 | 96 | { |
justinkim | 0:fc90ec271280 | 97 | bus_write(addr | 0x80, data); |
justinkim | 0:fc90ec271280 | 98 | } |
justinkim | 0:fc90ec271280 | 99 | |
justinkim | 0:fc90ec271280 | 100 | //Read ArduChip internal registers |
justinkim | 0:fc90ec271280 | 101 | uint8_t ArduCAM::read_reg(uint8_t addr) |
justinkim | 0:fc90ec271280 | 102 | { |
justinkim | 0:fc90ec271280 | 103 | uint8_t data; |
justinkim | 0:fc90ec271280 | 104 | data = bus_read(addr & 0x7F); |
justinkim | 0:fc90ec271280 | 105 | return data; |
justinkim | 0:fc90ec271280 | 106 | } |
justinkim | 0:fc90ec271280 | 107 | |
justinkim | 0:fc90ec271280 | 108 | //Set corresponding bit |
justinkim | 0:fc90ec271280 | 109 | void ArduCAM::set_bit(uint8_t addr, uint8_t bit) |
justinkim | 0:fc90ec271280 | 110 | { |
justinkim | 0:fc90ec271280 | 111 | uint8_t temp; |
justinkim | 0:fc90ec271280 | 112 | temp = read_reg(addr); |
justinkim | 0:fc90ec271280 | 113 | write_reg(addr, temp | bit); |
justinkim | 0:fc90ec271280 | 114 | |
justinkim | 0:fc90ec271280 | 115 | } |
justinkim | 0:fc90ec271280 | 116 | |
justinkim | 0:fc90ec271280 | 117 | //Clear corresponding bit |
justinkim | 0:fc90ec271280 | 118 | void ArduCAM::clear_bit(uint8_t addr, uint8_t bit) |
justinkim | 0:fc90ec271280 | 119 | { |
justinkim | 0:fc90ec271280 | 120 | uint8_t temp; |
justinkim | 0:fc90ec271280 | 121 | temp = read_reg(addr); |
justinkim | 0:fc90ec271280 | 122 | write_reg(addr, temp & (~bit)); |
justinkim | 0:fc90ec271280 | 123 | } |
justinkim | 0:fc90ec271280 | 124 | |
justinkim | 0:fc90ec271280 | 125 | //Get corresponding bit status |
justinkim | 0:fc90ec271280 | 126 | uint8_t ArduCAM::get_bit(uint8_t addr, uint8_t bit) |
justinkim | 0:fc90ec271280 | 127 | { |
justinkim | 0:fc90ec271280 | 128 | uint8_t temp; |
justinkim | 0:fc90ec271280 | 129 | temp = read_reg(addr); |
justinkim | 0:fc90ec271280 | 130 | temp = temp & bit; |
justinkim | 0:fc90ec271280 | 131 | return temp; |
justinkim | 0:fc90ec271280 | 132 | } |
justinkim | 0:fc90ec271280 | 133 | |
justinkim | 0:fc90ec271280 | 134 | //Set ArduCAM working mode |
justinkim | 0:fc90ec271280 | 135 | //MCU2LCD_MODE: MCU writes the LCD screen GRAM |
justinkim | 0:fc90ec271280 | 136 | //CAM2LCD_MODE: Camera takes control of the LCD screen |
justinkim | 0:fc90ec271280 | 137 | //LCD2MCU_MODE: MCU read the LCD screen GRAM |
justinkim | 0:fc90ec271280 | 138 | void ArduCAM::set_mode(uint8_t mode) |
justinkim | 0:fc90ec271280 | 139 | { |
justinkim | 0:fc90ec271280 | 140 | switch(mode) |
justinkim | 0:fc90ec271280 | 141 | { |
justinkim | 0:fc90ec271280 | 142 | case MCU2LCD_MODE: |
justinkim | 0:fc90ec271280 | 143 | write_reg(ARDUCHIP_MODE, MCU2LCD_MODE); |
justinkim | 0:fc90ec271280 | 144 | break; |
justinkim | 0:fc90ec271280 | 145 | case CAM2LCD_MODE: |
justinkim | 0:fc90ec271280 | 146 | write_reg(ARDUCHIP_MODE, CAM2LCD_MODE); |
justinkim | 0:fc90ec271280 | 147 | break; |
justinkim | 0:fc90ec271280 | 148 | case LCD2MCU_MODE: |
justinkim | 0:fc90ec271280 | 149 | write_reg(ARDUCHIP_MODE, LCD2MCU_MODE); |
justinkim | 0:fc90ec271280 | 150 | break; |
justinkim | 0:fc90ec271280 | 151 | default: |
justinkim | 0:fc90ec271280 | 152 | write_reg(ARDUCHIP_MODE, MCU2LCD_MODE); |
justinkim | 0:fc90ec271280 | 153 | break; |
justinkim | 0:fc90ec271280 | 154 | } |
justinkim | 0:fc90ec271280 | 155 | } |
justinkim | 0:fc90ec271280 | 156 | |
justinkim | 0:fc90ec271280 | 157 | |
justinkim | 0:fc90ec271280 | 158 | //I2C Write 8bit address, 8bit data |
justinkim | 0:fc90ec271280 | 159 | uint8_t ArduCAM::wrSensorReg8_8(int regID, int regDat) |
justinkim | 0:fc90ec271280 | 160 | { |
justinkim | 0:fc90ec271280 | 161 | uint8_t buff[20]; |
justinkim | 0:fc90ec271280 | 162 | buff[0] = regID; |
justinkim | 0:fc90ec271280 | 163 | buff[1] = regDat; |
justinkim | 0:fc90ec271280 | 164 | I2CBufferWrite(sensor_addr,buff,2, I2C_SEND_STOP); |
justinkim | 0:fc90ec271280 | 165 | |
justinkim | 0:fc90ec271280 | 166 | // wait_ms(1); |
justinkim | 0:fc90ec271280 | 167 | return(1); |
justinkim | 0:fc90ec271280 | 168 | } |
justinkim | 0:fc90ec271280 | 169 | |
justinkim | 0:fc90ec271280 | 170 | //I2C Read 8bit address, 8bit data |
justinkim | 0:fc90ec271280 | 171 | uint8_t ArduCAM::rdSensorReg8_8(uint8_t regID, uint8_t* regDat) |
justinkim | 0:fc90ec271280 | 172 | { |
justinkim | 0:fc90ec271280 | 173 | uint8_t buff[20]; |
justinkim | 0:fc90ec271280 | 174 | buff[0] = regID; |
justinkim | 0:fc90ec271280 | 175 | |
justinkim | 0:fc90ec271280 | 176 | I2CBufferWrite(sensor_addr,buff,1, I2C_SEND_STOP); |
justinkim | 0:fc90ec271280 | 177 | I2CBufferRead(sensor_addr+1,buff,1, I2C_SEND_STOP); |
justinkim | 0:fc90ec271280 | 178 | *regDat = buff[0]; |
justinkim | 0:fc90ec271280 | 179 | |
justinkim | 0:fc90ec271280 | 180 | // wait_ms(1); |
justinkim | 0:fc90ec271280 | 181 | return(1); |
justinkim | 0:fc90ec271280 | 182 | } |
justinkim | 0:fc90ec271280 | 183 | |
justinkim | 0:fc90ec271280 | 184 | //I2C Write 8bit address, 16bit data |
justinkim | 0:fc90ec271280 | 185 | uint8_t ArduCAM::wrSensorReg8_16(int regID, int regDat) |
justinkim | 0:fc90ec271280 | 186 | { |
justinkim | 0:fc90ec271280 | 187 | uint8_t buff[20]; |
justinkim | 0:fc90ec271280 | 188 | buff[0] = regID; |
justinkim | 0:fc90ec271280 | 189 | buff[1] = regDat >> 8; |
justinkim | 0:fc90ec271280 | 190 | buff[2] = regDat & 0x00FF; |
justinkim | 0:fc90ec271280 | 191 | I2CBufferWrite(sensor_addr,buff,3, I2C_SEND_STOP); |
justinkim | 0:fc90ec271280 | 192 | |
justinkim | 0:fc90ec271280 | 193 | // wait_ms(1); |
justinkim | 0:fc90ec271280 | 194 | return(1); |
justinkim | 0:fc90ec271280 | 195 | } |
justinkim | 0:fc90ec271280 | 196 | |
justinkim | 0:fc90ec271280 | 197 | //I2C Read 8bit address, 16bit data |
justinkim | 0:fc90ec271280 | 198 | uint8_t ArduCAM::rdSensorReg8_16(uint8_t regID, uint16_t* regDat) |
justinkim | 0:fc90ec271280 | 199 | { |
justinkim | 0:fc90ec271280 | 200 | // uint8_t temp; |
justinkim | 0:fc90ec271280 | 201 | uint8_t buff[20]; |
justinkim | 0:fc90ec271280 | 202 | buff[0] = regID; |
justinkim | 0:fc90ec271280 | 203 | |
justinkim | 0:fc90ec271280 | 204 | I2CBufferWrite(sensor_addr,buff,1, I2C_SEND_STOP); |
justinkim | 0:fc90ec271280 | 205 | I2CBufferRead(sensor_addr+1,buff, 2, I2C_SEND_STOP); |
justinkim | 0:fc90ec271280 | 206 | *regDat = (buff[0] << 8) | buff[1]; |
justinkim | 0:fc90ec271280 | 207 | |
justinkim | 0:fc90ec271280 | 208 | // wait_ms(1); |
justinkim | 0:fc90ec271280 | 209 | return(1); |
justinkim | 0:fc90ec271280 | 210 | } |
justinkim | 0:fc90ec271280 | 211 | |
justinkim | 0:fc90ec271280 | 212 | //I2C Write 16bit address, 8bit data |
justinkim | 0:fc90ec271280 | 213 | uint8_t ArduCAM::wrSensorReg16_8(int regID, int regDat) |
justinkim | 0:fc90ec271280 | 214 | { |
justinkim | 0:fc90ec271280 | 215 | uint8_t buff[20]; |
justinkim | 0:fc90ec271280 | 216 | buff[0] = regID >> 8; |
justinkim | 0:fc90ec271280 | 217 | buff[1] = regID & 0x00FF; |
justinkim | 0:fc90ec271280 | 218 | buff[2] = regDat; |
justinkim | 0:fc90ec271280 | 219 | I2CBufferWrite(sensor_addr,buff,3, I2C_SEND_STOP); |
justinkim | 0:fc90ec271280 | 220 | |
justinkim | 0:fc90ec271280 | 221 | // wait_ms(1); |
justinkim | 0:fc90ec271280 | 222 | return(1); |
justinkim | 0:fc90ec271280 | 223 | } |
justinkim | 0:fc90ec271280 | 224 | |
justinkim | 0:fc90ec271280 | 225 | //I2C Read 16bit address, 8bit data |
justinkim | 0:fc90ec271280 | 226 | uint8_t ArduCAM::rdSensorReg16_8(uint16_t regID, uint8_t* regDat) |
justinkim | 0:fc90ec271280 | 227 | { |
justinkim | 0:fc90ec271280 | 228 | uint8_t buff[20]; |
justinkim | 0:fc90ec271280 | 229 | buff[0] = regID >> 8; |
justinkim | 0:fc90ec271280 | 230 | buff[1] = regID & 0x00FF; |
justinkim | 0:fc90ec271280 | 231 | I2CBufferWrite(sensor_addr,buff,2, I2C_SEND_STOP); |
justinkim | 0:fc90ec271280 | 232 | I2CBufferRead(sensor_addr+1,buff, 1, I2C_SEND_STOP); |
justinkim | 0:fc90ec271280 | 233 | *regDat = buff[0]; |
justinkim | 0:fc90ec271280 | 234 | |
justinkim | 0:fc90ec271280 | 235 | // wait_ms(1); |
justinkim | 0:fc90ec271280 | 236 | return(1); |
justinkim | 0:fc90ec271280 | 237 | } |
justinkim | 0:fc90ec271280 | 238 | |
justinkim | 0:fc90ec271280 | 239 | //I2C Write 16bit address, 16bit data |
justinkim | 0:fc90ec271280 | 240 | uint8_t ArduCAM::wrSensorReg16_16(int regID, int regDat) |
justinkim | 0:fc90ec271280 | 241 | { |
justinkim | 0:fc90ec271280 | 242 | uint8_t buff[20]; |
justinkim | 0:fc90ec271280 | 243 | buff[0] = regID >> 8; |
justinkim | 0:fc90ec271280 | 244 | buff[1] = regID & 0x00FF; |
justinkim | 0:fc90ec271280 | 245 | buff[2] = regDat >> 8; |
justinkim | 0:fc90ec271280 | 246 | buff[3] = regID & 0x00FF; |
justinkim | 0:fc90ec271280 | 247 | I2CBufferWrite(sensor_addr,buff,4, I2C_SEND_STOP); |
justinkim | 0:fc90ec271280 | 248 | |
justinkim | 0:fc90ec271280 | 249 | // wait_ms(1); |
justinkim | 0:fc90ec271280 | 250 | return(1); |
justinkim | 0:fc90ec271280 | 251 | } |
justinkim | 0:fc90ec271280 | 252 | |
justinkim | 0:fc90ec271280 | 253 | //I2C Read 16bit address, 16bit data |
justinkim | 0:fc90ec271280 | 254 | uint8_t ArduCAM::rdSensorReg16_16(uint16_t regID, uint16_t* regDat) |
justinkim | 0:fc90ec271280 | 255 | { |
justinkim | 0:fc90ec271280 | 256 | // uint8_t temp; |
justinkim | 0:fc90ec271280 | 257 | uint8_t buff[20]; |
justinkim | 0:fc90ec271280 | 258 | buff[0] = regID >> 8; |
justinkim | 0:fc90ec271280 | 259 | buff[1] = regID & 0x00FF; |
justinkim | 0:fc90ec271280 | 260 | I2CBufferWrite(sensor_addr,buff,2, I2C_SEND_STOP); |
justinkim | 0:fc90ec271280 | 261 | I2CBufferRead(sensor_addr+1,buff, 1, I2C_SEND_STOP); |
justinkim | 0:fc90ec271280 | 262 | *regDat = (buff[0] << 8) | buff[1]; |
justinkim | 0:fc90ec271280 | 263 | |
justinkim | 0:fc90ec271280 | 264 | // wait_ms(1); |
justinkim | 0:fc90ec271280 | 265 | return(1); |
justinkim | 0:fc90ec271280 | 266 | } |
justinkim | 0:fc90ec271280 | 267 | |
justinkim | 0:fc90ec271280 | 268 | //I2C Array Write 8bit address, 8bit data |
justinkim | 0:fc90ec271280 | 269 | int ArduCAM::wrSensorRegs8_8(const struct sensor_reg reglist[]) |
justinkim | 0:fc90ec271280 | 270 | { |
justinkim | 0:fc90ec271280 | 271 | // int err = 0; |
justinkim | 0:fc90ec271280 | 272 | uint16_t reg_addr = 0; |
justinkim | 0:fc90ec271280 | 273 | uint16_t reg_val = 0; |
justinkim | 0:fc90ec271280 | 274 | const struct sensor_reg *next = reglist; |
justinkim | 0:fc90ec271280 | 275 | |
justinkim | 0:fc90ec271280 | 276 | while ((reg_addr != 0xff) | (reg_val != 0xff)) |
justinkim | 0:fc90ec271280 | 277 | { |
justinkim | 0:fc90ec271280 | 278 | reg_addr = next->reg; |
justinkim | 0:fc90ec271280 | 279 | reg_val = next->val; |
justinkim | 0:fc90ec271280 | 280 | wrSensorReg8_8(reg_addr, reg_val); |
justinkim | 0:fc90ec271280 | 281 | next++; |
justinkim | 0:fc90ec271280 | 282 | |
justinkim | 0:fc90ec271280 | 283 | } |
justinkim | 0:fc90ec271280 | 284 | |
justinkim | 0:fc90ec271280 | 285 | return 1; |
justinkim | 0:fc90ec271280 | 286 | } |
justinkim | 0:fc90ec271280 | 287 | |
justinkim | 0:fc90ec271280 | 288 | //I2C Array Write 8bit address, 16bit data |
justinkim | 0:fc90ec271280 | 289 | int ArduCAM::wrSensorRegs8_16(const struct sensor_reg reglist[]) |
justinkim | 0:fc90ec271280 | 290 | { |
justinkim | 0:fc90ec271280 | 291 | // int err = 0; |
justinkim | 0:fc90ec271280 | 292 | |
justinkim | 0:fc90ec271280 | 293 | uint32_t reg_addr = 0; |
justinkim | 0:fc90ec271280 | 294 | uint32_t reg_val = 0; |
justinkim | 0:fc90ec271280 | 295 | const struct sensor_reg *next = reglist; |
justinkim | 0:fc90ec271280 | 296 | |
justinkim | 0:fc90ec271280 | 297 | while ((reg_addr != 0xff) | (reg_val != 0xffff)) |
justinkim | 0:fc90ec271280 | 298 | { |
justinkim | 0:fc90ec271280 | 299 | reg_addr = next->reg; |
justinkim | 0:fc90ec271280 | 300 | reg_val = next->val; |
justinkim | 0:fc90ec271280 | 301 | wrSensorReg8_16(reg_addr, reg_val); |
justinkim | 0:fc90ec271280 | 302 | // if (!err) |
justinkim | 0:fc90ec271280 | 303 | //return err; |
justinkim | 0:fc90ec271280 | 304 | next++; |
justinkim | 0:fc90ec271280 | 305 | } |
justinkim | 0:fc90ec271280 | 306 | |
justinkim | 0:fc90ec271280 | 307 | return 1; |
justinkim | 0:fc90ec271280 | 308 | } |
justinkim | 0:fc90ec271280 | 309 | |
justinkim | 0:fc90ec271280 | 310 | //I2C Array Write 16bit address, 8bit data |
justinkim | 0:fc90ec271280 | 311 | int ArduCAM::wrSensorRegs16_8(const struct sensor_reg reglist[]) |
justinkim | 0:fc90ec271280 | 312 | { |
justinkim | 0:fc90ec271280 | 313 | // int err = 0; |
justinkim | 0:fc90ec271280 | 314 | |
justinkim | 0:fc90ec271280 | 315 | uint32_t reg_addr = 0; |
justinkim | 0:fc90ec271280 | 316 | uint8_t reg_val = 0; |
justinkim | 0:fc90ec271280 | 317 | const struct sensor_reg *next = reglist; |
justinkim | 0:fc90ec271280 | 318 | |
justinkim | 0:fc90ec271280 | 319 | while ((reg_addr != 0xffff) | (reg_val != 0xff)) |
justinkim | 0:fc90ec271280 | 320 | { |
justinkim | 0:fc90ec271280 | 321 | reg_addr = next->reg; |
justinkim | 0:fc90ec271280 | 322 | reg_val = next->val; |
justinkim | 0:fc90ec271280 | 323 | wrSensorReg16_8(reg_addr, reg_val); |
justinkim | 0:fc90ec271280 | 324 | //if (!err) |
justinkim | 0:fc90ec271280 | 325 | //return err; |
justinkim | 0:fc90ec271280 | 326 | next++; |
justinkim | 0:fc90ec271280 | 327 | } |
justinkim | 0:fc90ec271280 | 328 | |
justinkim | 0:fc90ec271280 | 329 | return 1; |
justinkim | 0:fc90ec271280 | 330 | } |
justinkim | 0:fc90ec271280 | 331 | |
justinkim | 0:fc90ec271280 | 332 | //I2C Array Write 16bit address, 16bit data |
justinkim | 0:fc90ec271280 | 333 | int ArduCAM::wrSensorRegs16_16(const struct sensor_reg reglist[]) |
justinkim | 0:fc90ec271280 | 334 | { |
justinkim | 0:fc90ec271280 | 335 | // int err = 0; |
justinkim | 0:fc90ec271280 | 336 | |
justinkim | 0:fc90ec271280 | 337 | uint32_t reg_addr = 0; |
justinkim | 0:fc90ec271280 | 338 | uint32_t reg_val = 0; |
justinkim | 0:fc90ec271280 | 339 | const struct sensor_reg *next = reglist; |
justinkim | 0:fc90ec271280 | 340 | |
justinkim | 0:fc90ec271280 | 341 | while ((reg_addr != 0xffff) | (reg_val != 0xffff)) |
justinkim | 0:fc90ec271280 | 342 | { |
justinkim | 0:fc90ec271280 | 343 | reg_addr = next->reg; |
justinkim | 0:fc90ec271280 | 344 | reg_val = next->val; |
justinkim | 0:fc90ec271280 | 345 | wrSensorReg16_16(reg_addr, reg_val); |
justinkim | 0:fc90ec271280 | 346 | //if (!err) |
justinkim | 0:fc90ec271280 | 347 | // return err; |
justinkim | 0:fc90ec271280 | 348 | next++; |
justinkim | 0:fc90ec271280 | 349 | } |
justinkim | 0:fc90ec271280 | 350 | |
justinkim | 0:fc90ec271280 | 351 | return 1; |
justinkim | 0:fc90ec271280 | 352 | } |
justinkim | 0:fc90ec271280 | 353 | |
justinkim | 0:fc90ec271280 | 354 | |
justinkim | 0:fc90ec271280 | 355 | void ArduCAM::OV5642_set_JPEG_size(){ |
justinkim | 0:fc90ec271280 | 356 | |
justinkim | 0:fc90ec271280 | 357 | wrSensorRegs16_8(ov5642_dvp_fmt_global_init); |
justinkim | 0:fc90ec271280 | 358 | wait_ms(100); |
justinkim | 0:fc90ec271280 | 359 | wrSensorRegs16_8(ov5642_dvp_fmt_jpeg_qvga); |
justinkim | 0:fc90ec271280 | 360 | wrSensorReg16_8(0x4407, 0x04); |
justinkim | 0:fc90ec271280 | 361 | wrSensorReg16_8(0x3818, 0xA8); |
justinkim | 0:fc90ec271280 | 362 | wrSensorReg16_8(0x3621, 0x10); |
justinkim | 0:fc90ec271280 | 363 | wrSensorReg16_8(0x3801, 0x98); |
justinkim | 0:fc90ec271280 | 364 | wait_ms(100); |
justinkim | 0:fc90ec271280 | 365 | } |
justinkim | 0:fc90ec271280 | 366 | |
justinkim | 0:fc90ec271280 | 367 | void ArduCAM::set_format(uint8_t fmt) |
justinkim | 0:fc90ec271280 | 368 | { |
justinkim | 0:fc90ec271280 | 369 | if(fmt == BMP) |
justinkim | 0:fc90ec271280 | 370 | m_fmt = BMP; |
justinkim | 0:fc90ec271280 | 371 | else |
justinkim | 0:fc90ec271280 | 372 | m_fmt = JPEG; |
justinkim | 0:fc90ec271280 | 373 | } |
justinkim | 0:fc90ec271280 | 374 | |
justinkim | 0:fc90ec271280 | 375 | int ArduCAM::bus_write(int address, int value) |
justinkim | 0:fc90ec271280 | 376 | { |
justinkim | 0:fc90ec271280 | 377 | // take the SS pin low to select the chip: |
justinkim | 0:fc90ec271280 | 378 | _cs = 0; |
justinkim | 0:fc90ec271280 | 379 | // send in the address and value via SPI: |
justinkim | 0:fc90ec271280 | 380 | spi.write(address); |
justinkim | 0:fc90ec271280 | 381 | spi.write(value); |
justinkim | 0:fc90ec271280 | 382 | // take the SS pin high to de-select the chip: |
justinkim | 0:fc90ec271280 | 383 | _cs = 1; |
justinkim | 0:fc90ec271280 | 384 | |
justinkim | 0:fc90ec271280 | 385 | return value; |
justinkim | 0:fc90ec271280 | 386 | } |
justinkim | 0:fc90ec271280 | 387 | |
justinkim | 0:fc90ec271280 | 388 | uint8_t ArduCAM::bus_read(int address) |
justinkim | 0:fc90ec271280 | 389 | { |
justinkim | 0:fc90ec271280 | 390 | uint8_t value = 0; |
justinkim | 0:fc90ec271280 | 391 | // take the SS pin low to select the chip: |
justinkim | 0:fc90ec271280 | 392 | _cs = 0; |
justinkim | 0:fc90ec271280 | 393 | // send in the address and value via SPI: |
justinkim | 0:fc90ec271280 | 394 | spi.write(address); |
justinkim | 0:fc90ec271280 | 395 | value = spi.write(0x00); |
justinkim | 0:fc90ec271280 | 396 | // take the SS pin high to de-select the chip: |
justinkim | 0:fc90ec271280 | 397 | _cs = 1; |
justinkim | 0:fc90ec271280 | 398 | return value; |
justinkim | 0:fc90ec271280 | 399 | } |
justinkim | 0:fc90ec271280 | 400 | |
justinkim | 0:fc90ec271280 | 401 | uint32_t ArduCAM::I2CInit() |
justinkim | 0:fc90ec271280 | 402 | { |
justinkim | 0:fc90ec271280 | 403 | |
justinkim | 0:fc90ec271280 | 404 | i2c.frequency (100000); |
justinkim | 0:fc90ec271280 | 405 | |
justinkim | 0:fc90ec271280 | 406 | return 0; |
justinkim | 0:fc90ec271280 | 407 | } |
justinkim | 0:fc90ec271280 | 408 | |
justinkim | 0:fc90ec271280 | 409 | int32_t ArduCAM::I2CBufferRead(int32_t ucDevAddr, uint8_t *ucBuffer, |
justinkim | 0:fc90ec271280 | 410 | int32_t ulSize, unsigned char ucFlags) |
justinkim | 0:fc90ec271280 | 411 | { |
justinkim | 0:fc90ec271280 | 412 | // Uart_Write((uint8_t*)"I2CBufferRead \n\r"); |
justinkim | 0:fc90ec271280 | 413 | |
justinkim | 0:fc90ec271280 | 414 | i2c.start(); |
justinkim | 0:fc90ec271280 | 415 | |
justinkim | 0:fc90ec271280 | 416 | // Set I2C slave read address |
justinkim | 0:fc90ec271280 | 417 | i2c.write(ucDevAddr); |
justinkim | 0:fc90ec271280 | 418 | if(ulSize == 1){ |
justinkim | 0:fc90ec271280 | 419 | ucBuffer[0] = i2c.read(0); |
justinkim | 0:fc90ec271280 | 420 | }else{ |
justinkim | 0:fc90ec271280 | 421 | for(int i=0;i<ulSize;i++){ |
justinkim | 0:fc90ec271280 | 422 | ucBuffer[i] = i2c.read(0); |
justinkim | 0:fc90ec271280 | 423 | } |
justinkim | 0:fc90ec271280 | 424 | } |
justinkim | 0:fc90ec271280 | 425 | |
justinkim | 0:fc90ec271280 | 426 | i2c.stop(); |
justinkim | 0:fc90ec271280 | 427 | |
justinkim | 0:fc90ec271280 | 428 | /* int32_t err = 0; |
justinkim | 0:fc90ec271280 | 429 | |
justinkim | 0:fc90ec271280 | 430 | // RTOS_MUTEX_ACQUIRE(&g_i2cLock); |
justinkim | 0:fc90ec271280 | 431 | |
justinkim | 0:fc90ec271280 | 432 | err = i2c.read(ucDevAddr,(char*)ucBuffer,ulSize); |
justinkim | 0:fc90ec271280 | 433 | if(err == 1){ |
justinkim | 0:fc90ec271280 | 434 | Uart_Write((uint8_t*)"Return error I2C read\n\r"); |
justinkim | 0:fc90ec271280 | 435 | // RTOS_MUTEX_RELEASE(&g_i2cLock); |
justinkim | 0:fc90ec271280 | 436 | return -1; |
justinkim | 0:fc90ec271280 | 437 | } |
justinkim | 0:fc90ec271280 | 438 | wait_ms(1); |
justinkim | 0:fc90ec271280 | 439 | // RTOS_MUTEX_RELEASE(&g_i2cLock); */ |
justinkim | 0:fc90ec271280 | 440 | return 0; |
justinkim | 0:fc90ec271280 | 441 | } |
justinkim | 0:fc90ec271280 | 442 | |
justinkim | 0:fc90ec271280 | 443 | int32_t ArduCAM::I2CBufferWrite(int32_t ucDevAddr, uint8_t *ucBuffer, |
justinkim | 0:fc90ec271280 | 444 | int32_t ulSize,unsigned char ucFlags) |
justinkim | 0:fc90ec271280 | 445 | { |
justinkim | 0:fc90ec271280 | 446 | // Uart_Write((uint8_t*)"I2CBufferWrite \n\r"); |
justinkim | 0:fc90ec271280 | 447 | |
justinkim | 0:fc90ec271280 | 448 | i2c.start(); |
justinkim | 0:fc90ec271280 | 449 | |
justinkim | 0:fc90ec271280 | 450 | // Set I2C slave write address |
justinkim | 0:fc90ec271280 | 451 | i2c.write(ucDevAddr); |
justinkim | 0:fc90ec271280 | 452 | |
justinkim | 0:fc90ec271280 | 453 | if(ulSize == 1){ |
justinkim | 0:fc90ec271280 | 454 | i2c.write(ucBuffer[0]); |
justinkim | 0:fc90ec271280 | 455 | }else{ |
justinkim | 0:fc90ec271280 | 456 | for(int i=0;i<ulSize;i++){ |
justinkim | 0:fc90ec271280 | 457 | i2c.write(ucBuffer[i]); |
justinkim | 0:fc90ec271280 | 458 | } |
justinkim | 0:fc90ec271280 | 459 | } |
justinkim | 0:fc90ec271280 | 460 | |
justinkim | 0:fc90ec271280 | 461 | i2c.stop(); |
justinkim | 0:fc90ec271280 | 462 | /* |
justinkim | 0:fc90ec271280 | 463 | int32_t err = 0; |
justinkim | 0:fc90ec271280 | 464 | |
justinkim | 0:fc90ec271280 | 465 | // RTOS_MUTEX_ACQUIRE(&g_i2cLock); |
justinkim | 0:fc90ec271280 | 466 | err = i2c.write(ucDevAddr,(char*)ucBuffer,ulSize); |
justinkim | 0:fc90ec271280 | 467 | if(err == 1){ |
justinkim | 0:fc90ec271280 | 468 | Uart_Write((uint8_t*)"Return error I2C write\n\r"); |
justinkim | 0:fc90ec271280 | 469 | // RTOS_MUTEX_RELEASE(&g_i2cLock); |
justinkim | 0:fc90ec271280 | 470 | return -1; |
justinkim | 0:fc90ec271280 | 471 | } |
justinkim | 0:fc90ec271280 | 472 | wait_ms(1); |
justinkim | 0:fc90ec271280 | 473 | // RTOS_MUTEX_RELEASE(&g_i2cLock); */ |
justinkim | 0:fc90ec271280 | 474 | return 0; |
justinkim | 0:fc90ec271280 | 475 | } |