Tedd OKANO / MARY_CAMERA
Committer:
okano
Date:
Sat Mar 22 01:09:04 2014 +0000
Revision:
27:f31bff7335ae
Parent:
24:cc4271d1545f
Child:
28:b5d5a2f7f0d0
test code

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 22:1a923c255be6 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 22:1a923c255be6 47 int MARY_CAMERA::init( void )
okano 11:61a025e8ab68 48 {
okano 22:1a923c255be6 49 const char camera_register_setting[ PARAM_NUM ][ 2 ] = {
okano 23:214896356355 50 { 0x01, 0x40 }, { 0x02, 0x60 }, { 0x03, 0x02 }, { 0x0C, 0x0C },
okano 23:214896356355 51 { 0x0E, 0x61 }, { 0x0F, 0x4B }, { 0x11, 0x81 }, { 0x12, 0x04 },
okano 23:214896356355 52 { 0x15, 0x00 }, { 0x16, 0x02 }, { 0x17, 0x39 }, { 0x18, 0x03 },
okano 23:214896356355 53 { 0x19, 0x03 }, { 0x1A, 0x7B }, { 0x1E, 0x37 }, { 0x21, 0x02 },
okano 23:214896356355 54 { 0x22, 0x91 }, { 0x29, 0x07 }, { 0x32, 0x80 }, { 0x33, 0x0B },
okano 23:214896356355 55 { 0x34, 0x11 }, { 0x35, 0x0B }, { 0x37, 0x1D }, { 0x38, 0x71 },
okano 23:214896356355 56 { 0x39, 0x2A }, { 0x3B, 0x12 }, { 0x3C, 0x78 }, { 0x3D, 0xC3 },
okano 23:214896356355 57 { 0x3E, 0x11 }, { 0x3F, 0x00 }, { 0x40, 0xD0 }, { 0x41, 0x08 },
okano 23:214896356355 58 { 0x41, 0x38 }, { 0x43, 0x0A }, { 0x44, 0xF0 }, { 0x45, 0x34 },
okano 23:214896356355 59 { 0x46, 0x58 }, { 0x47, 0x28 }, { 0x48, 0x3A }, { 0x4B, 0x09 },
okano 23:214896356355 60 { 0x4C, 0x00 }, { 0x4D, 0x40 }, { 0x4E, 0x20 }, { 0x4F, 0x80 },
okano 23:214896356355 61 { 0x50, 0x80 }, { 0x51, 0x00 }, { 0x52, 0x22 }, { 0x53, 0x5E },
okano 23:214896356355 62 { 0x54, 0x80 }, { 0x56, 0x40 }, { 0x58, 0x9E }, { 0x59, 0x88 },
okano 23:214896356355 63 { 0x5A, 0x88 }, { 0x5B, 0x44 }, { 0x5C, 0x67 }, { 0x5D, 0x49 },
okano 23:214896356355 64 { 0x5E, 0x0E }, { 0x69, 0x00 }, { 0x6A, 0x40 }, { 0x6B, 0x0A },
okano 23:214896356355 65 { 0x6C, 0x0A }, { 0x6D, 0x55 }, { 0x6E, 0x11 }, { 0x6F, 0x9F },
okano 23:214896356355 66 { 0x70, 0x3A }, { 0x71, 0x35 }, { 0x72, 0x11 }, { 0x73, 0xF1 },
okano 23:214896356355 67 { 0x74, 0x10 }, { 0x75, 0x05 }, { 0x76, 0xE1 }, { 0x77, 0x01 },
okano 23:214896356355 68 { 0x78, 0x04 }, { 0x79, 0x01 }, { 0x8D, 0x4F }, { 0x8E, 0x00 },
okano 23:214896356355 69 { 0x8F, 0x00 }, { 0x90, 0x00 }, { 0x91, 0x00 }, { 0x96, 0x00 },
okano 23:214896356355 70 { 0x96, 0x00 }, { 0x97, 0x30 }, { 0x98, 0x20 }, { 0x99, 0x30 },
okano 23:214896356355 71 { 0x9A, 0x00 }, { 0x9A, 0x84 }, { 0x9B, 0x29 }, { 0x9C, 0x03 },
okano 23:214896356355 72 { 0x9D, 0x4C }, { 0x9E, 0x3F }, { 0xA2, 0x52 }, { 0xA4, 0x88 },
okano 23:214896356355 73 { 0xB0, 0x84 }, { 0xB1, 0x0C }, { 0xB2, 0x0E }, { 0xB3, 0x82 },
okano 23:214896356355 74 { 0xB8, 0x0A }, { 0xC8, 0xF0 }, { 0xC9, 0x60 },
okano 13:210f4bbd0cd6 75 };
okano 27:f31bff7335ae 76 const char camera_reset_command[] = { 0x12, 0x80 };
okano 19:1d07d6d762a9 77
okano 11:61a025e8ab68 78 // SPI settings
okano 11:61a025e8ab68 79
okano 11:61a025e8ab68 80 _cs = 1; // set ChipSelect signal HIGH
okano 11:61a025e8ab68 81 _spi.format( 8 ); // camera SPI : 8bits/transfer
okano 11:61a025e8ab68 82 _spi.frequency( SPI_FREQUENCY ); // SPI frequency setting
okano 11:61a025e8ab68 83
okano 11:61a025e8ab68 84 // reset
okano 0:f4584dba3bac 85
okano 4:cb0ef3fd89c9 86 _reset = 0;
okano 11:61a025e8ab68 87 wait_ms( RESET_PULSE_WIDTH ); // assert RESET signal
okano 4:cb0ef3fd89c9 88 _reset = 1;
okano 27:f31bff7335ae 89
okano 27:f31bff7335ae 90 if ( 0 != (_error_state = _i2c.write( CAM_I2C_ADDR, camera_reset_command, 2 )) )
okano 27:f31bff7335ae 91 return _error_state; // return non-zero if I2C access failed
okano 27:f31bff7335ae 92
okano 11:61a025e8ab68 93 wait_ms( RESET_RECOVERY_TIME ); // deassert RESET signal
okano 0:f4584dba3bac 94
okano 22:1a923c255be6 95 _horizontal_size = QCIF_PIXEL_PER_LINE;
okano 22:1a923c255be6 96 _vertical_size = QCIF_LINE_PER_FRAME;
okano 19:1d07d6d762a9 97
okano 22:1a923c255be6 98 for ( int i = 0; i < PARAM_NUM; i++ ) {
okano 22:1a923c255be6 99 if ( 0 != (_error_state = _i2c.write( CAM_I2C_ADDR, camera_register_setting[ i ], 2 )) )
okano 22:1a923c255be6 100 break;
okano 22:1a923c255be6 101
okano 22:1a923c255be6 102 wait_ms( 20 ); // camera register writing requires this interval
okano 22:1a923c255be6 103 }
okano 22:1a923c255be6 104
okano 22:1a923c255be6 105 return _error_state; // return non-zero if I2C access failed
okano 22:1a923c255be6 106 }
okano 22:1a923c255be6 107
okano 22:1a923c255be6 108 void MARY_CAMERA::resolution( CameraResolution res )
okano 22:1a923c255be6 109 {
okano 22:1a923c255be6 110 #define OVERWRITE_PARAM_NUM 12
okano 22:1a923c255be6 111 const char over_write_param[ 5 ][ OVERWRITE_PARAM_NUM ] = {
okano 22:1a923c255be6 112 { 0x17, 0x18, 0x32, 0x19, 0x1a, 0x03, 0x0c, 0x3e, 0x71, 0x72, 0x73, 0xa2 }, // register addr
okano 22:1a923c255be6 113 { 0x39, 0x03, 0x80, 0x03, 0x7b, 0x02, 0x0c, 0x11, 0x35, 0x11, 0xf1, 0x52 }, // QSIF
okano 22:1a923c255be6 114 { 0x13, 0x01, 0xb6, 0x02, 0x7a, 0x0a, 0x00, 0x00, 0x35, 0x11, 0xf0, 0x02 }, // VGA
okano 22:1a923c255be6 115 { 0x16, 0x04, 0x80, 0x02, 0x7a, 0x0a, 0x04, 0x19, 0x35, 0x11, 0xf1, 0x02 }, // QVGA
okano 22:1a923c255be6 116 { 0x16, 0x04, 0xa4, 0x02, 0x7a, 0x0a, 0x04, 0x1a, 0x35, 0x22, 0xf2, 0x02 }, // QQVGA
okano 22:1a923c255be6 117 };
okano 22:1a923c255be6 118 char d[ 2 ];
okano 27:f31bff7335ae 119
okano 27:f31bff7335ae 120 if ( init() )
okano 27:f31bff7335ae 121 return;
okano 22:1a923c255be6 122
okano 22:1a923c255be6 123 switch ( res ) {
okano 19:1d07d6d762a9 124 case QCIF:
okano 19:1d07d6d762a9 125 _horizontal_size = QCIF_PIXEL_PER_LINE;
okano 19:1d07d6d762a9 126 _vertical_size = QCIF_LINE_PER_FRAME;
okano 19:1d07d6d762a9 127 break;
okano 19:1d07d6d762a9 128 case VGA:
okano 19:1d07d6d762a9 129 _horizontal_size = VGA_PIXEL_PER_LINE;
okano 19:1d07d6d762a9 130 _vertical_size = VGA_LINE_PER_FRAME;
okano 19:1d07d6d762a9 131 break;
okano 19:1d07d6d762a9 132 case QVGA:
okano 19:1d07d6d762a9 133 _horizontal_size = VGA_PIXEL_PER_LINE / 2;
okano 19:1d07d6d762a9 134 _vertical_size = VGA_LINE_PER_FRAME / 2;
okano 19:1d07d6d762a9 135 break;
okano 19:1d07d6d762a9 136 case QQVGA:
okano 19:1d07d6d762a9 137 _horizontal_size = VGA_PIXEL_PER_LINE / 4;
okano 19:1d07d6d762a9 138 _vertical_size = VGA_LINE_PER_FRAME / 4;
okano 19:1d07d6d762a9 139 break;
okano 19:1d07d6d762a9 140 }
okano 19:1d07d6d762a9 141
okano 11:61a025e8ab68 142 // set camera registers
okano 0:f4584dba3bac 143
okano 22:1a923c255be6 144 for ( int i = 0; i < OVERWRITE_PARAM_NUM; i++ ) {
okano 22:1a923c255be6 145 d[ 0 ] = over_write_param[ 0 ][ i ];
okano 22:1a923c255be6 146 d[ 1 ] = over_write_param[ res ][ i ];
okano 23:214896356355 147
okano 22:1a923c255be6 148 if ( 0 != (_error_state = _i2c.write( CAM_I2C_ADDR, d, 2 )) )
okano 13:210f4bbd0cd6 149 break;
okano 23:214896356355 150
okano 13:210f4bbd0cd6 151 wait_ms( 20 ); // camera register writing requires this interval
okano 13:210f4bbd0cd6 152 }
okano 23:214896356355 153
okano 23:214896356355 154 // wait 1 frame buffer update
okano 23:214896356355 155 open_transfer();
okano 23:214896356355 156 close_transfer();
okano 19:1d07d6d762a9 157 }
okano 13:210f4bbd0cd6 158
okano 20:fa4a54e25fc4 159 void MARY_CAMERA::colorbar( SwitchState sw )
okano 20:fa4a54e25fc4 160 {
okano 20:fa4a54e25fc4 161 char s[ 2 ];
okano 22:1a923c255be6 162
okano 20:fa4a54e25fc4 163 s[ 0 ] = 0x12;
okano 20:fa4a54e25fc4 164 s[ 1 ] = sw ? 0x06 : 0x04;
okano 22:1a923c255be6 165
okano 20:fa4a54e25fc4 166 _error_state = _i2c.write( CAM_I2C_ADDR, s, 2 );
okano 20:fa4a54e25fc4 167
okano 20:fa4a54e25fc4 168 }
okano 20:fa4a54e25fc4 169
okano 19:1d07d6d762a9 170 int MARY_CAMERA::horizontal_size( void )
okano 19:1d07d6d762a9 171 {
okano 19:1d07d6d762a9 172 return _horizontal_size; // return last state of I2C access
okano 19:1d07d6d762a9 173 }
okano 2:ee71ffdf317e 174
okano 19:1d07d6d762a9 175 int MARY_CAMERA::vertical_size( void )
okano 19:1d07d6d762a9 176 {
okano 19:1d07d6d762a9 177 return _vertical_size; // return last state of I2C access
okano 0:f4584dba3bac 178 }
okano 0:f4584dba3bac 179
okano 13:210f4bbd0cd6 180 int MARY_CAMERA::ready( void )
okano 13:210f4bbd0cd6 181 {
okano 11:61a025e8ab68 182 return _error_state; // return last state of I2C access
okano 8:23d14d5254d2 183 }
okano 8:23d14d5254d2 184
okano 24:cc4271d1545f 185 extern int read_order_change;
okano 24:cc4271d1545f 186
okano 13:210f4bbd0cd6 187 void MARY_CAMERA::transfer_a_line( short *p, int line_number, int x_offset, int n_of_pixels )
okano 13:210f4bbd0cd6 188 {
okano 24:cc4271d1545f 189 #if 0
okano 24:cc4271d1545f 190
okano 9:c1e24f1bec19 191 char tmp;
okano 24:cc4271d1545f 192
okano 23:214896356355 193 if ( line_number < 0 )
okano 23:214896356355 194 return;
okano 10:82394d226c74 195
okano 11:61a025e8ab68 196 // set camera module's buffer address
okano 19:1d07d6d762a9 197 set_address( line_number * horizontal_size() * BYTE_PER_PIXEL + x_offset * BYTE_PER_PIXEL );
okano 0:f4584dba3bac 198
okano 11:61a025e8ab68 199 // put a read command, first return byte should be ignored
okano 0:f4584dba3bac 200 read_register( CAMERA_DATA_REGISTER );
okano 11:61a025e8ab68 201
okano 10:82394d226c74 202 for( int x = 0; x < n_of_pixels; x++ ) {
okano 11:61a025e8ab68 203 // perform 2 bytes read. a pixel data is in RGB565 format (16bits)
okano 11:61a025e8ab68 204 tmp = read_register( CAMERA_DATA_REGISTER ); // read lower byte
okano 11:61a025e8ab68 205 *p++ = (read_register( CAMERA_DATA_REGISTER ) << 8) | tmp; // read upper byte
okano 9:c1e24f1bec19 206 }
okano 24:cc4271d1545f 207
okano 24:cc4271d1545f 208 #else
okano 24:cc4271d1545f 209
okano 24:cc4271d1545f 210
okano 24:cc4271d1545f 211 short tmp;
okano 24:cc4271d1545f 212
okano 24:cc4271d1545f 213 if ( line_number < 0 )
okano 24:cc4271d1545f 214 return;
okano 24:cc4271d1545f 215
okano 24:cc4271d1545f 216 // set camera module's buffer address
okano 24:cc4271d1545f 217 set_address( line_number * horizontal_size() * BYTE_PER_PIXEL + x_offset * BYTE_PER_PIXEL );
okano 24:cc4271d1545f 218
okano 24:cc4271d1545f 219 // put a read command, first return byte should be ignored
okano 24:cc4271d1545f 220 read_register( CAMERA_DATA_REGISTER );
okano 24:cc4271d1545f 221
okano 24:cc4271d1545f 222
okano 24:cc4271d1545f 223 if ( read_order_change ) {
okano 24:cc4271d1545f 224
okano 24:cc4271d1545f 225 read_register( CAMERA_DATA_REGISTER );
okano 24:cc4271d1545f 226
okano 24:cc4271d1545f 227 for( int x = 0; x < n_of_pixels; x++ ) {
okano 24:cc4271d1545f 228 // perform 2 bytes read. a pixel data is in RGB565 format (16bits)
okano 24:cc4271d1545f 229 tmp = read_register( CAMERA_DATA_REGISTER ) << 8; // read lower byte
okano 24:cc4271d1545f 230 *p++ = (read_register( CAMERA_DATA_REGISTER ) << 0) | tmp; // read upper byte
okano 24:cc4271d1545f 231 }
okano 24:cc4271d1545f 232
okano 24:cc4271d1545f 233 } else {
okano 24:cc4271d1545f 234
okano 24:cc4271d1545f 235 for( int x = 0; x < n_of_pixels; x++ ) {
okano 24:cc4271d1545f 236 // perform 2 bytes read. a pixel data is in RGB565 format (16bits)
okano 24:cc4271d1545f 237 tmp = read_register( CAMERA_DATA_REGISTER ); // read lower byte
okano 24:cc4271d1545f 238 *p++ = (read_register( CAMERA_DATA_REGISTER ) << 8) | tmp; // read upper byte
okano 24:cc4271d1545f 239 }
okano 24:cc4271d1545f 240
okano 24:cc4271d1545f 241 }
okano 24:cc4271d1545f 242
okano 24:cc4271d1545f 243 #endif
okano 0:f4584dba3bac 244 }
okano 0:f4584dba3bac 245
okano 13:210f4bbd0cd6 246 void MARY_CAMERA::open_transfer( void )
okano 13:210f4bbd0cd6 247 {
okano 11:61a025e8ab68 248 // send command to pause the camera buffer update
okano 0:f4584dba3bac 249 write_register( CONTROL_DATA_REGISTER, CONTROL__PAUSE_BUFFER_UPDATE );
okano 0:f4584dba3bac 250
okano 11:61a025e8ab68 251 // read status register (first return byte should be ignored)
okano 0:f4584dba3bac 252 read_register( STATUS_REGISTER );
okano 7:942d8d0a1760 253
okano 11:61a025e8ab68 254 // wait until the status register become 0x51(ready to transfer data)
okano 11:61a025e8ab68 255 while ( 0x51 != read_register( STATUS_REGISTER ) )
okano 11:61a025e8ab68 256 ;
okano 0:f4584dba3bac 257 }
okano 0:f4584dba3bac 258
okano 13:210f4bbd0cd6 259 void MARY_CAMERA::close_transfer( void )
okano 13:210f4bbd0cd6 260 {
okano 11:61a025e8ab68 261 // send command to resume the camera buffer update
okano 0:f4584dba3bac 262 write_register( CONTROL_DATA_REGISTER, CONTROL__RESUME_BUFFER_UPDATE );
okano 0:f4584dba3bac 263
okano 11:61a025e8ab68 264 // read status register (first return byte should be ignored)
okano 0:f4584dba3bac 265 read_register( STATUS_REGISTER );
okano 7:942d8d0a1760 266
okano 11:61a025e8ab68 267 // wait until the status register become 0x50(camera updating the buffer)
okano 11:61a025e8ab68 268 while ( 0x50 != read_register( STATUS_REGISTER ) )
okano 11:61a025e8ab68 269 ;
okano 0:f4584dba3bac 270 }
okano 0:f4584dba3bac 271
okano 13:210f4bbd0cd6 272 void MARY_CAMERA::set_address( int address )
okano 13:210f4bbd0cd6 273 {
okano 11:61a025e8ab68 274 // set memory address (3 bytes)
okano 12:614be3290c47 275
okano 0:f4584dba3bac 276 write_register( MEMORY_ADDR_LOW__REGISTER, (address >> 0) & 0xFF );
okano 0:f4584dba3bac 277 write_register( MEMORY_ADDR_MID__REGISTER, (address >> 8) & 0xFF );
okano 0:f4584dba3bac 278 write_register( MEMORY_ADDR_HIGH_REGISTER, (address >> 16) & 0xFF );
okano 0:f4584dba3bac 279 }
okano 0:f4584dba3bac 280
okano 13:210f4bbd0cd6 281 void MARY_CAMERA::write_register( char reg, char value )
okano 13:210f4bbd0cd6 282 {
okano 11:61a025e8ab68 283 // camera register write
okano 12:614be3290c47 284
okano 11:61a025e8ab68 285 send_spi( COMMAND_WRITE | reg ); // send command and register number
okano 11:61a025e8ab68 286 send_spi( value ); // send register value
okano 11:61a025e8ab68 287 }
okano 11:61a025e8ab68 288
okano 13:210f4bbd0cd6 289 int MARY_CAMERA::read_register( char reg )
okano 13:210f4bbd0cd6 290 {
okano 11:61a025e8ab68 291 // camera register read
okano 11:61a025e8ab68 292 // returning current data in SPI buffer (data returned by previous command)
okano 11:61a025e8ab68 293
okano 11:61a025e8ab68 294 return ( send_spi( COMMAND_READ | reg | ((reg == CAMERA_DATA_REGISTER) ? COMMAND_ADDR_INCREMENT : 0x00) ) );
okano 11:61a025e8ab68 295 }
okano 10:82394d226c74 296
okano 13:210f4bbd0cd6 297 int MARY_CAMERA::send_spi( char data )
okano 13:210f4bbd0cd6 298 {
okano 0:f4584dba3bac 299 int tmp;
okano 12:614be3290c47 300
okano 11:61a025e8ab68 301 // SPI access
okano 0:f4584dba3bac 302
okano 4:cb0ef3fd89c9 303 _cs = 0;
okano 4:cb0ef3fd89c9 304 tmp = _spi.write( data );
okano 4:cb0ef3fd89c9 305 _cs = 1;
okano 12:614be3290c47 306
okano 0:f4584dba3bac 307 return ( tmp );
okano 0:f4584dba3bac 308 }