Tedd OKANO / MARY_CAMERA
Committer:
okano
Date:
Mon Mar 10 01:41:23 2014 +0000
Revision:
12:614be3290c47
Parent:
11:61a025e8ab68
Child:
13:210f4bbd0cd6
camera register init array row-col swapped

Who changed what in which revision?

UserRevisionLine numberNew contents of line
okano 0:f4584dba3bac 1 #include "mbed.h"
okano 0:f4584dba3bac 2 #include "MARY_CAMERA.h"
okano 0:f4584dba3bac 3
okano 2:ee71ffdf317e 4 #define PARAM_NUM 99
okano 2:ee71ffdf317e 5 #define CAM_I2C_ADDR 0x42
okano 2:ee71ffdf317e 6
okano 0:f4584dba3bac 7 #define RESET_PULSE_WIDTH 100 // mili-seconds
okano 0:f4584dba3bac 8 #define RESET_RECOVERY_TIME 100 // mili-seconds
okano 0:f4584dba3bac 9
okano 2:ee71ffdf317e 10 #define COMMAND_WRITE 0x00
okano 2:ee71ffdf317e 11 #define COMMAND_READ 0x80
okano 2:ee71ffdf317e 12 #define COMMAND_ADDR_INCREMENT 0x20
okano 0:f4584dba3bac 13
okano 2:ee71ffdf317e 14 #define MEMORY_ADDR_LOW__REGISTER 0x0
okano 2:ee71ffdf317e 15 #define MEMORY_ADDR_MID__REGISTER 0x1
okano 2:ee71ffdf317e 16 #define MEMORY_ADDR_HIGH_REGISTER 0x2
okano 2:ee71ffdf317e 17 #define CAMERA_DATA_REGISTER 0x8
okano 2:ee71ffdf317e 18 #define CONTROL_DATA_REGISTER 0x3
okano 2:ee71ffdf317e 19 #define STATUS_REGISTER 0x4
okano 2:ee71ffdf317e 20
okano 2:ee71ffdf317e 21 #define CONTROL__PAUSE_BUFFER_UPDATE 0x01
okano 2:ee71ffdf317e 22 #define CONTROL__RESUME_BUFFER_UPDATE 0x00
okano 0:f4584dba3bac 23
okano 0:f4584dba3bac 24
okano 0:f4584dba3bac 25 MARY_CAMERA::MARY_CAMERA(
okano 4:cb0ef3fd89c9 26 PinName SPI_mosi,
okano 4:cb0ef3fd89c9 27 PinName SPI_miso,
okano 4:cb0ef3fd89c9 28 PinName SPI_sck,
okano 4:cb0ef3fd89c9 29 PinName SPI_cs,
okano 0:f4584dba3bac 30 PinName cam_reset,
okano 4:cb0ef3fd89c9 31 PinName I2C_sda,
okano 4:cb0ef3fd89c9 32 PinName I2C_scl
okano 2:ee71ffdf317e 33 ) :
okano 4:cb0ef3fd89c9 34 _spi( SPI_mosi, SPI_miso, SPI_sck ),
okano 4:cb0ef3fd89c9 35 _cs( SPI_cs ),
okano 4:cb0ef3fd89c9 36 _reset( cam_reset ),
okano 4:cb0ef3fd89c9 37 _i2c( I2C_sda, I2C_scl )
okano 0:f4584dba3bac 38 {
okano 11:61a025e8ab68 39 #ifdef IGNORE_INITIALIZATION_ERROR
okano 0:f4584dba3bac 40 init();
okano 11:61a025e8ab68 41 #else
okano 11:61a025e8ab68 42 if ( 0 != init() )
okano 11:61a025e8ab68 43 error( "camera initialization failed." );
okano 11:61a025e8ab68 44 #endif
okano 0:f4584dba3bac 45 }
okano 0:f4584dba3bac 46
okano 11:61a025e8ab68 47 int MARY_CAMERA::init( void )
okano 11:61a025e8ab68 48 {
okano 12:614be3290c47 49 const char camera_register_setting[ PARAM_NUM ][ 2 ] = {
okano 12:614be3290c47 50 { 0x01, 0x40 },
okano 12:614be3290c47 51 { 0x02, 0x60 },
okano 12:614be3290c47 52 { 0x03, 0x02 },
okano 12:614be3290c47 53 { 0x0C, 0x0C },
okano 12:614be3290c47 54 { 0x0E, 0x61 },
okano 12:614be3290c47 55 { 0x0F, 0x4B },
okano 12:614be3290c47 56 { 0x11, 0x81 },
okano 12:614be3290c47 57 { 0x12, 0x04 },
okano 12:614be3290c47 58 { 0x15, 0x00 },
okano 12:614be3290c47 59 { 0x16, 0x02 },
okano 12:614be3290c47 60 { 0x17, 0x39 },
okano 12:614be3290c47 61 { 0x18, 0x03 },
okano 12:614be3290c47 62 { 0x19, 0x03 },
okano 12:614be3290c47 63 { 0x1A, 0x7B },
okano 12:614be3290c47 64 { 0x1E, 0x37 },
okano 12:614be3290c47 65 { 0x21, 0x02 },
okano 12:614be3290c47 66 { 0x22, 0x91 },
okano 12:614be3290c47 67 { 0x29, 0x07 },
okano 12:614be3290c47 68 { 0x32, 0x80 },
okano 12:614be3290c47 69 { 0x33, 0x0B },
okano 12:614be3290c47 70 { 0x34, 0x11 },
okano 12:614be3290c47 71 { 0x35, 0x0B },
okano 12:614be3290c47 72 { 0x37, 0x1D },
okano 12:614be3290c47 73 { 0x38, 0x71 },
okano 12:614be3290c47 74 { 0x39, 0x2A },
okano 12:614be3290c47 75 { 0x3B, 0x12 },
okano 12:614be3290c47 76 { 0x3C, 0x78 },
okano 12:614be3290c47 77 { 0x3D, 0xC3 },
okano 12:614be3290c47 78 { 0x3E, 0x11 },
okano 12:614be3290c47 79 { 0x3F, 0x00 },
okano 12:614be3290c47 80 { 0x40, 0xD0 },
okano 12:614be3290c47 81 { 0x41, 0x08 },
okano 12:614be3290c47 82 { 0x41, 0x38 },
okano 12:614be3290c47 83 { 0x43, 0x0A },
okano 12:614be3290c47 84 { 0x44, 0xF0 },
okano 12:614be3290c47 85 { 0x45, 0x34 },
okano 12:614be3290c47 86 { 0x46, 0x58 },
okano 12:614be3290c47 87 { 0x47, 0x28 },
okano 12:614be3290c47 88 { 0x48, 0x3A },
okano 12:614be3290c47 89 { 0x4B, 0x09 },
okano 12:614be3290c47 90 { 0x4C, 0x00 },
okano 12:614be3290c47 91 { 0x4D, 0x40 },
okano 12:614be3290c47 92 { 0x4E, 0x20 },
okano 12:614be3290c47 93 { 0x4F, 0x80 },
okano 12:614be3290c47 94 { 0x50, 0x80 },
okano 12:614be3290c47 95 { 0x51, 0x00 },
okano 12:614be3290c47 96 { 0x52, 0x22 },
okano 12:614be3290c47 97 { 0x53, 0x5E },
okano 12:614be3290c47 98 { 0x54, 0x80 },
okano 12:614be3290c47 99 { 0x56, 0x40 },
okano 12:614be3290c47 100 { 0x58, 0x9E },
okano 12:614be3290c47 101 { 0x59, 0x88 },
okano 12:614be3290c47 102 { 0x5A, 0x88 },
okano 12:614be3290c47 103 { 0x5B, 0x44 },
okano 12:614be3290c47 104 { 0x5C, 0x67 },
okano 12:614be3290c47 105 { 0x5D, 0x49 },
okano 12:614be3290c47 106 { 0x5E, 0x0E },
okano 12:614be3290c47 107 { 0x69, 0x00 },
okano 12:614be3290c47 108 { 0x6A, 0x40 },
okano 12:614be3290c47 109 { 0x6B, 0x0A },
okano 12:614be3290c47 110 { 0x6C, 0x0A },
okano 12:614be3290c47 111 { 0x6D, 0x55 },
okano 12:614be3290c47 112 { 0x6E, 0x11 },
okano 12:614be3290c47 113 { 0x6F, 0x9F },
okano 12:614be3290c47 114 { 0x70, 0x3A },
okano 12:614be3290c47 115 { 0x71, 0x35 },
okano 12:614be3290c47 116 { 0x72, 0x11 },
okano 12:614be3290c47 117 { 0x73, 0xF1 },
okano 12:614be3290c47 118 { 0x74, 0x10 },
okano 12:614be3290c47 119 { 0x75, 0x05 },
okano 12:614be3290c47 120 { 0x76, 0xE1 },
okano 12:614be3290c47 121 { 0x77, 0x01 },
okano 12:614be3290c47 122 { 0x78, 0x04 },
okano 12:614be3290c47 123 { 0x79, 0x01 },
okano 12:614be3290c47 124 { 0x8D, 0x4F },
okano 12:614be3290c47 125 { 0x8E, 0x00 },
okano 12:614be3290c47 126 { 0x8F, 0x00 },
okano 12:614be3290c47 127 { 0x90, 0x00 },
okano 12:614be3290c47 128 { 0x91, 0x00 },
okano 12:614be3290c47 129 { 0x96, 0x00 },
okano 12:614be3290c47 130 { 0x96, 0x00 },
okano 12:614be3290c47 131 { 0x97, 0x30 },
okano 12:614be3290c47 132 { 0x98, 0x20 },
okano 12:614be3290c47 133 { 0x99, 0x30 },
okano 12:614be3290c47 134 { 0x9A, 0x00 },
okano 12:614be3290c47 135 { 0x9A, 0x84 },
okano 12:614be3290c47 136 { 0x9B, 0x29 },
okano 12:614be3290c47 137 { 0x9C, 0x03 },
okano 12:614be3290c47 138 { 0x9D, 0x4C },
okano 12:614be3290c47 139 { 0x9E, 0x3F },
okano 12:614be3290c47 140 { 0xA2, 0x52 },
okano 12:614be3290c47 141 { 0xA4, 0x88 },
okano 12:614be3290c47 142 { 0xB0, 0x84 },
okano 12:614be3290c47 143 { 0xB1, 0x0C },
okano 12:614be3290c47 144 { 0xB2, 0x0E },
okano 12:614be3290c47 145 { 0xB3, 0x82 },
okano 12:614be3290c47 146 { 0xB8, 0x0A },
okano 12:614be3290c47 147 { 0xC8, 0xF0 },
okano 12:614be3290c47 148 { 0xC9, 0x60 },
okano 12:614be3290c47 149 };
okano 0:f4584dba3bac 150
okano 11:61a025e8ab68 151 // SPI settings
okano 11:61a025e8ab68 152
okano 11:61a025e8ab68 153 _cs = 1; // set ChipSelect signal HIGH
okano 11:61a025e8ab68 154 _spi.format( 8 ); // camera SPI : 8bits/transfer
okano 11:61a025e8ab68 155 _spi.frequency( SPI_FREQUENCY ); // SPI frequency setting
okano 11:61a025e8ab68 156
okano 11:61a025e8ab68 157 // reset
okano 0:f4584dba3bac 158
okano 4:cb0ef3fd89c9 159 _reset = 0;
okano 11:61a025e8ab68 160 wait_ms( RESET_PULSE_WIDTH ); // assert RESET signal
okano 4:cb0ef3fd89c9 161 _reset = 1;
okano 11:61a025e8ab68 162 wait_ms( RESET_RECOVERY_TIME ); // deassert RESET signal
okano 0:f4584dba3bac 163
okano 11:61a025e8ab68 164 // set camera registers
okano 0:f4584dba3bac 165
okano 12:614be3290c47 166
okano 0:f4584dba3bac 167 for ( int i = 0; i < PARAM_NUM; i++ ) {
okano 12:614be3290c47 168 if ( 0 != (_error_state = _i2c.write( CAM_I2C_ADDR, camera_register_setting[ i ], 2 )) )
okano 12:614be3290c47 169 break;
okano 2:ee71ffdf317e 170
okano 11:61a025e8ab68 171 wait_ms( 20 ); // camera register writing requires this interval
okano 0:f4584dba3bac 172 }
okano 0:f4584dba3bac 173
okano 11:61a025e8ab68 174 return _error_state; // return non-zero if I2C access failed
okano 0:f4584dba3bac 175 }
okano 0:f4584dba3bac 176
okano 12:614be3290c47 177 int MARY_CAMERA::ready( void ) {
okano 11:61a025e8ab68 178 return _error_state; // return last state of I2C access
okano 8:23d14d5254d2 179 }
okano 8:23d14d5254d2 180
okano 12:614be3290c47 181 void MARY_CAMERA::transfer_a_line( short *p, int line_number, int x_offset, int n_of_pixels ) {
okano 9:c1e24f1bec19 182 char tmp;
okano 10:82394d226c74 183
okano 11:61a025e8ab68 184 // set camera module's buffer address
okano 0:f4584dba3bac 185 set_address( line_number * BYTE_PER_LINE + x_offset * BYTE_PER_PIXEL );
okano 0:f4584dba3bac 186
okano 11:61a025e8ab68 187 // put a read command, first return byte should be ignored
okano 0:f4584dba3bac 188 read_register( CAMERA_DATA_REGISTER );
okano 11:61a025e8ab68 189
okano 10:82394d226c74 190 for( int x = 0; x < n_of_pixels; x++ ) {
okano 11:61a025e8ab68 191 // perform 2 bytes read. a pixel data is in RGB565 format (16bits)
okano 11:61a025e8ab68 192 tmp = read_register( CAMERA_DATA_REGISTER ); // read lower byte
okano 11:61a025e8ab68 193 *p++ = (read_register( CAMERA_DATA_REGISTER ) << 8) | tmp; // read upper byte
okano 9:c1e24f1bec19 194 }
okano 0:f4584dba3bac 195 }
okano 0:f4584dba3bac 196
okano 12:614be3290c47 197 void MARY_CAMERA::open_transfer( void ) {
okano 11:61a025e8ab68 198 // send command to pause the camera buffer update
okano 0:f4584dba3bac 199 write_register( CONTROL_DATA_REGISTER, CONTROL__PAUSE_BUFFER_UPDATE );
okano 0:f4584dba3bac 200
okano 11:61a025e8ab68 201 // read status register (first return byte should be ignored)
okano 0:f4584dba3bac 202 read_register( STATUS_REGISTER );
okano 7:942d8d0a1760 203
okano 11:61a025e8ab68 204 // wait until the status register become 0x51(ready to transfer data)
okano 11:61a025e8ab68 205 while ( 0x51 != read_register( STATUS_REGISTER ) )
okano 11:61a025e8ab68 206 ;
okano 0:f4584dba3bac 207 }
okano 0:f4584dba3bac 208
okano 12:614be3290c47 209 void MARY_CAMERA::close_transfer( void ) {
okano 11:61a025e8ab68 210 // send command to resume the camera buffer update
okano 0:f4584dba3bac 211 write_register( CONTROL_DATA_REGISTER, CONTROL__RESUME_BUFFER_UPDATE );
okano 0:f4584dba3bac 212
okano 11:61a025e8ab68 213 // read status register (first return byte should be ignored)
okano 0:f4584dba3bac 214 read_register( STATUS_REGISTER );
okano 7:942d8d0a1760 215
okano 11:61a025e8ab68 216 // wait until the status register become 0x50(camera updating the buffer)
okano 11:61a025e8ab68 217 while ( 0x50 != read_register( STATUS_REGISTER ) )
okano 11:61a025e8ab68 218 ;
okano 0:f4584dba3bac 219 }
okano 0:f4584dba3bac 220
okano 12:614be3290c47 221 void MARY_CAMERA::set_address( int address ) {
okano 11:61a025e8ab68 222 // set memory address (3 bytes)
okano 12:614be3290c47 223
okano 0:f4584dba3bac 224 write_register( MEMORY_ADDR_LOW__REGISTER, (address >> 0) & 0xFF );
okano 0:f4584dba3bac 225 write_register( MEMORY_ADDR_MID__REGISTER, (address >> 8) & 0xFF );
okano 0:f4584dba3bac 226 write_register( MEMORY_ADDR_HIGH_REGISTER, (address >> 16) & 0xFF );
okano 0:f4584dba3bac 227 }
okano 0:f4584dba3bac 228
okano 12:614be3290c47 229 void MARY_CAMERA::write_register( char reg, char value ) {
okano 11:61a025e8ab68 230 // camera register write
okano 12:614be3290c47 231
okano 11:61a025e8ab68 232 send_spi( COMMAND_WRITE | reg ); // send command and register number
okano 11:61a025e8ab68 233 send_spi( value ); // send register value
okano 11:61a025e8ab68 234 }
okano 11:61a025e8ab68 235
okano 12:614be3290c47 236 int MARY_CAMERA::read_register( char reg ) {
okano 11:61a025e8ab68 237 // camera register read
okano 11:61a025e8ab68 238 // returning current data in SPI buffer (data returned by previous command)
okano 11:61a025e8ab68 239
okano 11:61a025e8ab68 240 return ( send_spi( COMMAND_READ | reg | ((reg == CAMERA_DATA_REGISTER) ? COMMAND_ADDR_INCREMENT : 0x00) ) );
okano 11:61a025e8ab68 241 }
okano 10:82394d226c74 242
okano 12:614be3290c47 243 int MARY_CAMERA::send_spi( char data ) {
okano 0:f4584dba3bac 244 int tmp;
okano 12:614be3290c47 245
okano 11:61a025e8ab68 246 // SPI access
okano 0:f4584dba3bac 247
okano 4:cb0ef3fd89c9 248 _cs = 0;
okano 4:cb0ef3fd89c9 249 tmp = _spi.write( data );
okano 4:cb0ef3fd89c9 250 _cs = 1;
okano 12:614be3290c47 251
okano 0:f4584dba3bac 252 return ( tmp );
okano 0:f4584dba3bac 253 }