Tedd OKANO / MARY_CAMERA
Committer:
okano
Date:
Tue Mar 11 03:49:45 2014 +0000
Revision:
14:64ea2901540c
Parent:
13:210f4bbd0cd6
Child:
15:50562b6b23b8
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 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 13:210f4bbd0cd6 50 { 0x01, 0x40 },
okano 13:210f4bbd0cd6 51 { 0x02, 0x60 },
okano 13:210f4bbd0cd6 52 { 0x03, 0x02 },
okano 13:210f4bbd0cd6 53 { 0x0C, 0x0C },
okano 13:210f4bbd0cd6 54 { 0x0E, 0x61 },
okano 13:210f4bbd0cd6 55 { 0x0F, 0x4B },
okano 13:210f4bbd0cd6 56 { 0x11, 0x81 },
okano 13:210f4bbd0cd6 57 { 0x12, 0x04 },
okano 13:210f4bbd0cd6 58 { 0x15, 0x00 },
okano 13:210f4bbd0cd6 59 { 0x16, 0x02 },
okano 13:210f4bbd0cd6 60 { 0x17, 0x39 },
okano 13:210f4bbd0cd6 61 { 0x18, 0x03 },
okano 13:210f4bbd0cd6 62 { 0x19, 0x03 },
okano 13:210f4bbd0cd6 63 { 0x1A, 0x7B },
okano 13:210f4bbd0cd6 64 { 0x1E, 0x37 },
okano 13:210f4bbd0cd6 65 { 0x21, 0x02 },
okano 13:210f4bbd0cd6 66 { 0x22, 0x91 },
okano 13:210f4bbd0cd6 67 { 0x29, 0x07 },
okano 13:210f4bbd0cd6 68 { 0x32, 0x80 },
okano 13:210f4bbd0cd6 69 { 0x33, 0x0B },
okano 13:210f4bbd0cd6 70 { 0x34, 0x11 },
okano 13:210f4bbd0cd6 71 { 0x35, 0x0B },
okano 13:210f4bbd0cd6 72 { 0x37, 0x1D },
okano 13:210f4bbd0cd6 73 { 0x38, 0x71 },
okano 13:210f4bbd0cd6 74 { 0x39, 0x2A },
okano 13:210f4bbd0cd6 75 { 0x3B, 0x12 },
okano 13:210f4bbd0cd6 76 { 0x3C, 0x78 },
okano 13:210f4bbd0cd6 77 { 0x3D, 0xC3 },
okano 13:210f4bbd0cd6 78 { 0x3E, 0x11 },
okano 13:210f4bbd0cd6 79 { 0x3F, 0x00 },
okano 13:210f4bbd0cd6 80 { 0x40, 0xD0 },
okano 13:210f4bbd0cd6 81 { 0x41, 0x08 },
okano 13:210f4bbd0cd6 82 { 0x41, 0x38 },
okano 13:210f4bbd0cd6 83 { 0x43, 0x0A },
okano 13:210f4bbd0cd6 84 { 0x44, 0xF0 },
okano 13:210f4bbd0cd6 85 { 0x45, 0x34 },
okano 13:210f4bbd0cd6 86 { 0x46, 0x58 },
okano 13:210f4bbd0cd6 87 { 0x47, 0x28 },
okano 13:210f4bbd0cd6 88 { 0x48, 0x3A },
okano 13:210f4bbd0cd6 89 { 0x4B, 0x09 },
okano 13:210f4bbd0cd6 90 { 0x4C, 0x00 },
okano 13:210f4bbd0cd6 91 { 0x4D, 0x40 },
okano 13:210f4bbd0cd6 92 { 0x4E, 0x20 },
okano 13:210f4bbd0cd6 93 { 0x4F, 0x80 },
okano 13:210f4bbd0cd6 94 { 0x50, 0x80 },
okano 13:210f4bbd0cd6 95 { 0x51, 0x00 },
okano 13:210f4bbd0cd6 96 { 0x52, 0x22 },
okano 13:210f4bbd0cd6 97 { 0x53, 0x5E },
okano 13:210f4bbd0cd6 98 { 0x54, 0x80 },
okano 13:210f4bbd0cd6 99 { 0x56, 0x40 },
okano 13:210f4bbd0cd6 100 { 0x58, 0x9E },
okano 13:210f4bbd0cd6 101 { 0x59, 0x88 },
okano 13:210f4bbd0cd6 102 { 0x5A, 0x88 },
okano 13:210f4bbd0cd6 103 { 0x5B, 0x44 },
okano 13:210f4bbd0cd6 104 { 0x5C, 0x67 },
okano 13:210f4bbd0cd6 105 { 0x5D, 0x49 },
okano 13:210f4bbd0cd6 106 { 0x5E, 0x0E },
okano 13:210f4bbd0cd6 107 { 0x69, 0x00 },
okano 13:210f4bbd0cd6 108 { 0x6A, 0x40 },
okano 13:210f4bbd0cd6 109 { 0x6B, 0x0A },
okano 13:210f4bbd0cd6 110 { 0x6C, 0x0A },
okano 13:210f4bbd0cd6 111 { 0x6D, 0x55 },
okano 13:210f4bbd0cd6 112 { 0x6E, 0x11 },
okano 13:210f4bbd0cd6 113 { 0x6F, 0x9F },
okano 13:210f4bbd0cd6 114 { 0x70, 0x3A },
okano 13:210f4bbd0cd6 115 { 0x71, 0x35 },
okano 13:210f4bbd0cd6 116 { 0x72, 0x11 },
okano 13:210f4bbd0cd6 117 { 0x73, 0xF1 },
okano 13:210f4bbd0cd6 118 { 0x74, 0x10 },
okano 13:210f4bbd0cd6 119 { 0x75, 0x05 },
okano 13:210f4bbd0cd6 120 { 0x76, 0xE1 },
okano 13:210f4bbd0cd6 121 { 0x77, 0x01 },
okano 13:210f4bbd0cd6 122 { 0x78, 0x04 },
okano 13:210f4bbd0cd6 123 { 0x79, 0x01 },
okano 13:210f4bbd0cd6 124 { 0x8D, 0x4F },
okano 13:210f4bbd0cd6 125 { 0x8E, 0x00 },
okano 13:210f4bbd0cd6 126 { 0x8F, 0x00 },
okano 13:210f4bbd0cd6 127 { 0x90, 0x00 },
okano 13:210f4bbd0cd6 128 { 0x91, 0x00 },
okano 13:210f4bbd0cd6 129 { 0x96, 0x00 },
okano 13:210f4bbd0cd6 130 { 0x96, 0x00 },
okano 13:210f4bbd0cd6 131 { 0x97, 0x30 },
okano 13:210f4bbd0cd6 132 { 0x98, 0x20 },
okano 13:210f4bbd0cd6 133 { 0x99, 0x30 },
okano 13:210f4bbd0cd6 134 { 0x9A, 0x00 },
okano 13:210f4bbd0cd6 135 { 0x9A, 0x84 },
okano 13:210f4bbd0cd6 136 { 0x9B, 0x29 },
okano 13:210f4bbd0cd6 137 { 0x9C, 0x03 },
okano 13:210f4bbd0cd6 138 { 0x9D, 0x4C },
okano 13:210f4bbd0cd6 139 { 0x9E, 0x3F },
okano 13:210f4bbd0cd6 140 { 0xA2, 0x52 },
okano 13:210f4bbd0cd6 141 { 0xA4, 0x88 },
okano 13:210f4bbd0cd6 142 { 0xB0, 0x84 },
okano 13:210f4bbd0cd6 143 { 0xB1, 0x0C },
okano 13:210f4bbd0cd6 144 { 0xB2, 0x0E },
okano 13:210f4bbd0cd6 145 { 0xB3, 0x82 },
okano 13:210f4bbd0cd6 146 { 0xB8, 0x0A },
okano 13:210f4bbd0cd6 147 { 0xC8, 0xF0 },
okano 13:210f4bbd0cd6 148 { 0xC9, 0x60 },
okano 13:210f4bbd0cd6 149 };
okano 13:210f4bbd0cd6 150 const char camera_register_setting2[ PARAM_NUM ][ 2 ] = {
okano 13:210f4bbd0cd6 151 { 0x01, 0x40 },
okano 13:210f4bbd0cd6 152 { 0x02, 0x60 },
okano 13:210f4bbd0cd6 153 { 0x03, 0x0A }, //
okano 13:210f4bbd0cd6 154 { 0x0C, 0x00 }, //
okano 13:210f4bbd0cd6 155 { 0x0E, 0x61 },
okano 13:210f4bbd0cd6 156 { 0x0F, 0x4B },
okano 13:210f4bbd0cd6 157 { 0x11, 0x81 },
okano 13:210f4bbd0cd6 158 { 0x12, 0x04 }, //
okano 13:210f4bbd0cd6 159 { 0x15, 0x00 },
okano 13:210f4bbd0cd6 160 { 0x16, 0x02 },
okano 13:210f4bbd0cd6 161 { 0x17, 0x13 }, //
okano 13:210f4bbd0cd6 162 { 0x18, 0x01 }, //
okano 13:210f4bbd0cd6 163 { 0x19, 0x02 }, //
okano 13:210f4bbd0cd6 164 { 0x1A, 0x7A }, //
okano 13:210f4bbd0cd6 165 { 0x1E, 0x37 },
okano 13:210f4bbd0cd6 166 { 0x21, 0x02 },
okano 13:210f4bbd0cd6 167 { 0x22, 0x91 },
okano 13:210f4bbd0cd6 168 { 0x29, 0x07 },
okano 13:210f4bbd0cd6 169 { 0x32, 0xB6 }, //
okano 13:210f4bbd0cd6 170 { 0x33, 0x0B },
okano 13:210f4bbd0cd6 171 { 0x34, 0x11 },
okano 13:210f4bbd0cd6 172 { 0x35, 0x0B },
okano 13:210f4bbd0cd6 173 { 0x37, 0x1D },
okano 13:210f4bbd0cd6 174 { 0x38, 0x71 },
okano 13:210f4bbd0cd6 175 { 0x39, 0x2A },
okano 13:210f4bbd0cd6 176 { 0x3B, 0x12 },
okano 13:210f4bbd0cd6 177 { 0x3C, 0x78 },
okano 13:210f4bbd0cd6 178 { 0x3D, 0xC3 },
okano 13:210f4bbd0cd6 179 { 0x3E, 0x00 }, //
okano 13:210f4bbd0cd6 180 { 0x3F, 0x00 },
okano 13:210f4bbd0cd6 181 { 0x40, 0xD0 },
okano 13:210f4bbd0cd6 182 { 0x41, 0x08 },
okano 13:210f4bbd0cd6 183 { 0x41, 0x38 },
okano 13:210f4bbd0cd6 184 { 0x43, 0x0A },
okano 13:210f4bbd0cd6 185 { 0x44, 0xF0 },
okano 13:210f4bbd0cd6 186 { 0x45, 0x34 },
okano 13:210f4bbd0cd6 187 { 0x46, 0x58 },
okano 13:210f4bbd0cd6 188 { 0x47, 0x28 },
okano 13:210f4bbd0cd6 189 { 0x48, 0x3A },
okano 13:210f4bbd0cd6 190 { 0x4B, 0x09 },
okano 13:210f4bbd0cd6 191 { 0x4C, 0x00 },
okano 13:210f4bbd0cd6 192 { 0x4D, 0x40 },
okano 13:210f4bbd0cd6 193 { 0x4E, 0x20 },
okano 13:210f4bbd0cd6 194 { 0x4F, 0x80 },
okano 13:210f4bbd0cd6 195 { 0x50, 0x80 },
okano 13:210f4bbd0cd6 196 { 0x51, 0x00 },
okano 13:210f4bbd0cd6 197 { 0x52, 0x22 },
okano 13:210f4bbd0cd6 198 { 0x53, 0x5E },
okano 13:210f4bbd0cd6 199 { 0x54, 0x80 },
okano 13:210f4bbd0cd6 200 { 0x56, 0x40 },
okano 13:210f4bbd0cd6 201 { 0x58, 0x9E },
okano 13:210f4bbd0cd6 202 { 0x59, 0x88 },
okano 13:210f4bbd0cd6 203 { 0x5A, 0x88 },
okano 13:210f4bbd0cd6 204 { 0x5B, 0x44 },
okano 13:210f4bbd0cd6 205 { 0x5C, 0x67 },
okano 13:210f4bbd0cd6 206 { 0x5D, 0x49 },
okano 13:210f4bbd0cd6 207 { 0x5E, 0x0E },
okano 13:210f4bbd0cd6 208 { 0x69, 0x00 },
okano 13:210f4bbd0cd6 209 { 0x6A, 0x40 },
okano 13:210f4bbd0cd6 210 { 0x6B, 0x0A },
okano 13:210f4bbd0cd6 211 { 0x6C, 0x0A },
okano 13:210f4bbd0cd6 212 { 0x6D, 0x55 },
okano 13:210f4bbd0cd6 213 { 0x6E, 0x11 },
okano 13:210f4bbd0cd6 214 { 0x6F, 0x9F },
okano 13:210f4bbd0cd6 215 { 0x70, 0x3A }, //
okano 13:210f4bbd0cd6 216 { 0x71, 0x35 }, //
okano 13:210f4bbd0cd6 217 { 0x72, 0x11 }, //
okano 13:210f4bbd0cd6 218 { 0x73, 0xF0 }, //
okano 13:210f4bbd0cd6 219 { 0x74, 0x10 },
okano 13:210f4bbd0cd6 220 { 0x75, 0x05 },
okano 13:210f4bbd0cd6 221 { 0x76, 0xE1 },
okano 13:210f4bbd0cd6 222 { 0x77, 0x01 },
okano 13:210f4bbd0cd6 223 { 0x78, 0x04 },
okano 13:210f4bbd0cd6 224 { 0x79, 0x01 },
okano 13:210f4bbd0cd6 225 { 0x8D, 0x4F },
okano 13:210f4bbd0cd6 226 { 0x8E, 0x00 },
okano 13:210f4bbd0cd6 227 { 0x8F, 0x00 },
okano 13:210f4bbd0cd6 228 { 0x90, 0x00 },
okano 13:210f4bbd0cd6 229 { 0x91, 0x00 },
okano 13:210f4bbd0cd6 230 { 0x96, 0x00 },
okano 13:210f4bbd0cd6 231 { 0x96, 0x00 },
okano 13:210f4bbd0cd6 232 { 0x97, 0x30 },
okano 13:210f4bbd0cd6 233 { 0x98, 0x20 },
okano 13:210f4bbd0cd6 234 { 0x99, 0x30 },
okano 13:210f4bbd0cd6 235 { 0x9A, 0x00 },
okano 13:210f4bbd0cd6 236 { 0x9A, 0x84 },
okano 13:210f4bbd0cd6 237 { 0x9B, 0x29 },
okano 13:210f4bbd0cd6 238 { 0x9C, 0x03 },
okano 13:210f4bbd0cd6 239 { 0x9D, 0x4C },
okano 13:210f4bbd0cd6 240 { 0x9E, 0x3F },
okano 13:210f4bbd0cd6 241 { 0xA2, 0x02 }, //
okano 13:210f4bbd0cd6 242 { 0xA4, 0x88 },
okano 13:210f4bbd0cd6 243 { 0xB0, 0x84 },
okano 13:210f4bbd0cd6 244 { 0xB1, 0x0C },
okano 13:210f4bbd0cd6 245 { 0xB2, 0x0E },
okano 13:210f4bbd0cd6 246 { 0xB3, 0x82 },
okano 13:210f4bbd0cd6 247 { 0xB8, 0x0A },
okano 13:210f4bbd0cd6 248 { 0xC8, 0xF0 },
okano 13:210f4bbd0cd6 249 { 0xC9, 0x60 },
okano 13:210f4bbd0cd6 250 };
okano 13:210f4bbd0cd6 251
okano 13:210f4bbd0cd6 252 const char owreg[] = {
okano 13:210f4bbd0cd6 253 0x12,
okano 13:210f4bbd0cd6 254 0x17,
okano 13:210f4bbd0cd6 255 0x18,
okano 13:210f4bbd0cd6 256 0x32,
okano 13:210f4bbd0cd6 257 0x19,
okano 13:210f4bbd0cd6 258 0x1a,
okano 13:210f4bbd0cd6 259 0x03,
okano 13:210f4bbd0cd6 260 0x0C,
okano 13:210f4bbd0cd6 261 0x3E,
okano 13:210f4bbd0cd6 262 0x70,
okano 13:210f4bbd0cd6 263 0x71,
okano 13:210f4bbd0cd6 264 0x72,
okano 13:210f4bbd0cd6 265 0x73,
okano 13:210f4bbd0cd6 266 0xA2,
okano 13:210f4bbd0cd6 267 };
okano 13:210f4bbd0cd6 268 const char vga[] = {
okano 13:210f4bbd0cd6 269 0x04, 0x13, 0x01, 0xb6, 0x02, 0x7a, 0x0a, 0x00, 0x00, 0x3a, 0x35, 0x11, 0xf0, 0x02
okano 13:210f4bbd0cd6 270 };
okano 13:210f4bbd0cd6 271
okano 13:210f4bbd0cd6 272
okano 0:f4584dba3bac 273
okano 11:61a025e8ab68 274 // SPI settings
okano 11:61a025e8ab68 275
okano 11:61a025e8ab68 276 _cs = 1; // set ChipSelect signal HIGH
okano 11:61a025e8ab68 277 _spi.format( 8 ); // camera SPI : 8bits/transfer
okano 11:61a025e8ab68 278 _spi.frequency( SPI_FREQUENCY ); // SPI frequency setting
okano 11:61a025e8ab68 279
okano 11:61a025e8ab68 280 // reset
okano 0:f4584dba3bac 281
okano 4:cb0ef3fd89c9 282 _reset = 0;
okano 11:61a025e8ab68 283 wait_ms( RESET_PULSE_WIDTH ); // assert RESET signal
okano 4:cb0ef3fd89c9 284 _reset = 1;
okano 11:61a025e8ab68 285 wait_ms( RESET_RECOVERY_TIME ); // deassert RESET signal
okano 0:f4584dba3bac 286
okano 11:61a025e8ab68 287 // set camera registers
okano 0:f4584dba3bac 288
okano 13:210f4bbd0cd6 289 #if 1
okano 13:210f4bbd0cd6 290 for ( int i = 0; i < PARAM_NUM; i++ ) {
okano 14:64ea2901540c 291 if ( 0 != (_error_state = _i2c.write( CAM_I2C_ADDR, camera_register_setting2[ i ], 2 )) )
okano 13:210f4bbd0cd6 292 break;
okano 12:614be3290c47 293
okano 13:210f4bbd0cd6 294 wait_ms( 20 ); // camera register writing requires this interval
okano 13:210f4bbd0cd6 295 }
okano 13:210f4bbd0cd6 296 #else
okano 13:210f4bbd0cd6 297 for ( int i = (PARAM_NUM - 1); i >= 0; i-- ) {
okano 13:210f4bbd0cd6 298 if ( 0 != (_error_state = _i2c.write( CAM_I2C_ADDR, camera_register_setting[ i ], 2 )) )
okano 13:210f4bbd0cd6 299 break;
okano 13:210f4bbd0cd6 300
okano 13:210f4bbd0cd6 301 wait_ms( 20 ); // camera register writing requires this interval
okano 13:210f4bbd0cd6 302 }
okano 13:210f4bbd0cd6 303 #endif
okano 13:210f4bbd0cd6 304
okano 13:210f4bbd0cd6 305
okano 13:210f4bbd0cd6 306
okano 13:210f4bbd0cd6 307
okano 13:210f4bbd0cd6 308
okano 13:210f4bbd0cd6 309
okano 13:210f4bbd0cd6 310 #if 0
okano 0:f4584dba3bac 311 for ( int i = 0; i < PARAM_NUM; i++ ) {
okano 13:210f4bbd0cd6 312 if ( 0 != (_error_state = _i2c.write( CAM_I2C_ADDR, camera_register_settin2[ i ], 2 )) )
okano 13:210f4bbd0cd6 313 break;
okano 13:210f4bbd0cd6 314
okano 13:210f4bbd0cd6 315 wait_ms( 20 ); // camera register writing requires this interval
okano 13:210f4bbd0cd6 316 }
okano 13:210f4bbd0cd6 317 #endif
okano 13:210f4bbd0cd6 318
okano 13:210f4bbd0cd6 319
okano 13:210f4bbd0cd6 320
okano 13:210f4bbd0cd6 321
okano 13:210f4bbd0cd6 322
okano 13:210f4bbd0cd6 323
okano 13:210f4bbd0cd6 324
okano 14:64ea2901540c 325 #if 0
okano 13:210f4bbd0cd6 326 for ( int i = 0; i < sizeof( owreg ); i++ ) {
okano 13:210f4bbd0cd6 327 char s[ 2 ];
okano 13:210f4bbd0cd6 328 s[ 0 ] = owreg[ i ];
okano 13:210f4bbd0cd6 329 s[ 1 ] = vga[ i ];
okano 13:210f4bbd0cd6 330
okano 12:614be3290c47 331 if ( 0 != (_error_state = _i2c.write( CAM_I2C_ADDR, camera_register_setting[ i ], 2 )) )
okano 12:614be3290c47 332 break;
okano 2:ee71ffdf317e 333
okano 11:61a025e8ab68 334 wait_ms( 20 ); // camera register writing requires this interval
okano 0:f4584dba3bac 335 }
okano 14:64ea2901540c 336 #endif
okano 0:f4584dba3bac 337
okano 11:61a025e8ab68 338 return _error_state; // return non-zero if I2C access failed
okano 0:f4584dba3bac 339 }
okano 0:f4584dba3bac 340
okano 13:210f4bbd0cd6 341 int MARY_CAMERA::ready( void )
okano 13:210f4bbd0cd6 342 {
okano 11:61a025e8ab68 343 return _error_state; // return last state of I2C access
okano 8:23d14d5254d2 344 }
okano 8:23d14d5254d2 345
okano 13:210f4bbd0cd6 346 void MARY_CAMERA::transfer_a_line( short *p, int line_number, int x_offset, int n_of_pixels )
okano 13:210f4bbd0cd6 347 {
okano 9:c1e24f1bec19 348 char tmp;
okano 10:82394d226c74 349
okano 11:61a025e8ab68 350 // set camera module's buffer address
okano 0:f4584dba3bac 351 set_address( line_number * BYTE_PER_LINE + x_offset * BYTE_PER_PIXEL );
okano 0:f4584dba3bac 352
okano 11:61a025e8ab68 353 // put a read command, first return byte should be ignored
okano 0:f4584dba3bac 354 read_register( CAMERA_DATA_REGISTER );
okano 11:61a025e8ab68 355
okano 10:82394d226c74 356 for( int x = 0; x < n_of_pixels; x++ ) {
okano 11:61a025e8ab68 357 // perform 2 bytes read. a pixel data is in RGB565 format (16bits)
okano 11:61a025e8ab68 358 tmp = read_register( CAMERA_DATA_REGISTER ); // read lower byte
okano 11:61a025e8ab68 359 *p++ = (read_register( CAMERA_DATA_REGISTER ) << 8) | tmp; // read upper byte
okano 9:c1e24f1bec19 360 }
okano 0:f4584dba3bac 361 }
okano 0:f4584dba3bac 362
okano 13:210f4bbd0cd6 363 void MARY_CAMERA::open_transfer( void )
okano 13:210f4bbd0cd6 364 {
okano 11:61a025e8ab68 365 // send command to pause the camera buffer update
okano 0:f4584dba3bac 366 write_register( CONTROL_DATA_REGISTER, CONTROL__PAUSE_BUFFER_UPDATE );
okano 0:f4584dba3bac 367
okano 11:61a025e8ab68 368 // read status register (first return byte should be ignored)
okano 0:f4584dba3bac 369 read_register( STATUS_REGISTER );
okano 7:942d8d0a1760 370
okano 11:61a025e8ab68 371 // wait until the status register become 0x51(ready to transfer data)
okano 11:61a025e8ab68 372 while ( 0x51 != read_register( STATUS_REGISTER ) )
okano 11:61a025e8ab68 373 ;
okano 0:f4584dba3bac 374 }
okano 0:f4584dba3bac 375
okano 13:210f4bbd0cd6 376 void MARY_CAMERA::close_transfer( void )
okano 13:210f4bbd0cd6 377 {
okano 11:61a025e8ab68 378 // send command to resume the camera buffer update
okano 0:f4584dba3bac 379 write_register( CONTROL_DATA_REGISTER, CONTROL__RESUME_BUFFER_UPDATE );
okano 0:f4584dba3bac 380
okano 11:61a025e8ab68 381 // read status register (first return byte should be ignored)
okano 0:f4584dba3bac 382 read_register( STATUS_REGISTER );
okano 7:942d8d0a1760 383
okano 11:61a025e8ab68 384 // wait until the status register become 0x50(camera updating the buffer)
okano 11:61a025e8ab68 385 while ( 0x50 != read_register( STATUS_REGISTER ) )
okano 11:61a025e8ab68 386 ;
okano 0:f4584dba3bac 387 }
okano 0:f4584dba3bac 388
okano 13:210f4bbd0cd6 389 void MARY_CAMERA::set_address( int address )
okano 13:210f4bbd0cd6 390 {
okano 11:61a025e8ab68 391 // set memory address (3 bytes)
okano 12:614be3290c47 392
okano 0:f4584dba3bac 393 write_register( MEMORY_ADDR_LOW__REGISTER, (address >> 0) & 0xFF );
okano 0:f4584dba3bac 394 write_register( MEMORY_ADDR_MID__REGISTER, (address >> 8) & 0xFF );
okano 0:f4584dba3bac 395 write_register( MEMORY_ADDR_HIGH_REGISTER, (address >> 16) & 0xFF );
okano 0:f4584dba3bac 396 }
okano 0:f4584dba3bac 397
okano 13:210f4bbd0cd6 398 void MARY_CAMERA::write_register( char reg, char value )
okano 13:210f4bbd0cd6 399 {
okano 11:61a025e8ab68 400 // camera register write
okano 12:614be3290c47 401
okano 11:61a025e8ab68 402 send_spi( COMMAND_WRITE | reg ); // send command and register number
okano 11:61a025e8ab68 403 send_spi( value ); // send register value
okano 11:61a025e8ab68 404 }
okano 11:61a025e8ab68 405
okano 13:210f4bbd0cd6 406 int MARY_CAMERA::read_register( char reg )
okano 13:210f4bbd0cd6 407 {
okano 11:61a025e8ab68 408 // camera register read
okano 11:61a025e8ab68 409 // returning current data in SPI buffer (data returned by previous command)
okano 11:61a025e8ab68 410
okano 11:61a025e8ab68 411 return ( send_spi( COMMAND_READ | reg | ((reg == CAMERA_DATA_REGISTER) ? COMMAND_ADDR_INCREMENT : 0x00) ) );
okano 11:61a025e8ab68 412 }
okano 10:82394d226c74 413
okano 13:210f4bbd0cd6 414 int MARY_CAMERA::send_spi( char data )
okano 13:210f4bbd0cd6 415 {
okano 0:f4584dba3bac 416 int tmp;
okano 12:614be3290c47 417
okano 11:61a025e8ab68 418 // SPI access
okano 0:f4584dba3bac 419
okano 4:cb0ef3fd89c9 420 _cs = 0;
okano 4:cb0ef3fd89c9 421 tmp = _spi.write( data );
okano 4:cb0ef3fd89c9 422 _cs = 1;
okano 12:614be3290c47 423
okano 0:f4584dba3bac 424 return ( tmp );
okano 0:f4584dba3bac 425 }