Tedd OKANO / MARY_CAMERA
Committer:
okano
Date:
Wed Mar 05 06:35:35 2014 +0000
Revision:
7:942d8d0a1760
Parent:
6:7363c7bc620e
Child:
8:23d14d5254d2
camera status detection by whole data byte

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 const char param[2][PARAM_NUM] = {
okano 0:f4584dba3bac 8 {
okano 0:f4584dba3bac 9 0x01,0x02,0x03,0x0c,0x0e,0x0f,0x11,0x12,0x15,0x16,0x17,0x18,0x19,0x1a,0x1e,0x21,0x22,
okano 0:f4584dba3bac 10 0x29,0x32,0x33,0x34,0x35,0x37,0x38,0x39,0x3b,0x3c,0x3d,0x3e,0x3f,0x40,0x41,0x41,
okano 0:f4584dba3bac 11 0x43,0x44,0x45,0x46,0x47,0x48,0x4b,0x4c,0x4d,0x4e,0x4f,0x50,0x51,0x52,0x53,
okano 0:f4584dba3bac 12 0x54,0x56,0x58,0x59,0x5a,0x5b,0x5c,0x5d,0x5e,0x69,0x6a,0x6b,0x6c,0x6d,0x6e,
okano 0:f4584dba3bac 13 0x6f,0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x8d,0x8e,0x8f,0x90,
okano 0:f4584dba3bac 14 0x91,0x96,0x96,0x97,0x98,0x99,0x9a,0x9a,0x9b,0x9c,0x9d,0x9e,0xa2,0xa4,0xb0,
okano 0:f4584dba3bac 15 0xb1,0xb2,0xb3,0xb8,0xc8,0xc9
okano 0:f4584dba3bac 16 },
okano 0:f4584dba3bac 17 {
okano 0:f4584dba3bac 18 0x40,0x60,0x02,0x0c,0x61,0x4b,0x81,0x04 ,0x00,0x02,0x39,0x03,0x03,0x7b,0x37,0x02,0x91,
okano 0:f4584dba3bac 19 0x07,0x80,0x0b,0x11,0x0b,0x1d,0x71,0x2a,0x12,0x78,0xc3,0x11,0x00,0xd0,0x08,0x38,
okano 0:f4584dba3bac 20 0x0a,0xf0,0x34,0x58,0x28,0x3a,0x09,0x00,0x40,0x20,0x80,0x80,0x00,0x22,0x5e,
okano 0:f4584dba3bac 21 0x80,0x40,0x9e,0x88,0x88,0x44,0x67,0x49,0x0e,0x00,0x40,0x0a,0x0a,0x55,0x11,
okano 0:f4584dba3bac 22 0x9f,0x3a,0x35,0x11,0xf1,0x10,0x05,0xe1,0x01,0x04,0x01,0x4f,0x00,0x00,0x00,
okano 0:f4584dba3bac 23 0x00,0x00,0x00,0x30,0x20,0x30,0x00,0x84,0x29,0x03,0x4c,0x3f,0x52,0x88,0x84,
okano 0:f4584dba3bac 24 0x0c,0x0e,0x82,0x0a,0xf0,0x60
okano 0:f4584dba3bac 25 }
okano 0:f4584dba3bac 26 };
okano 0:f4584dba3bac 27
okano 0:f4584dba3bac 28 #define RESET_PULSE_WIDTH 100 // mili-seconds
okano 0:f4584dba3bac 29 #define RESET_RECOVERY_TIME 100 // mili-seconds
okano 0:f4584dba3bac 30
okano 2:ee71ffdf317e 31 #define COMMAND_WRITE 0x00
okano 2:ee71ffdf317e 32 #define COMMAND_READ 0x80
okano 2:ee71ffdf317e 33 #define COMMAND_ADDR_INCREMENT 0x20
okano 0:f4584dba3bac 34
okano 2:ee71ffdf317e 35 #define MEMORY_ADDR_LOW__REGISTER 0x0
okano 2:ee71ffdf317e 36 #define MEMORY_ADDR_MID__REGISTER 0x1
okano 2:ee71ffdf317e 37 #define MEMORY_ADDR_HIGH_REGISTER 0x2
okano 2:ee71ffdf317e 38 #define CAMERA_DATA_REGISTER 0x8
okano 2:ee71ffdf317e 39 #define CONTROL_DATA_REGISTER 0x3
okano 2:ee71ffdf317e 40 #define STATUS_REGISTER 0x4
okano 2:ee71ffdf317e 41
okano 2:ee71ffdf317e 42 #define CONTROL__PAUSE_BUFFER_UPDATE 0x01
okano 2:ee71ffdf317e 43 #define CONTROL__RESUME_BUFFER_UPDATE 0x00
okano 0:f4584dba3bac 44
okano 0:f4584dba3bac 45
okano 0:f4584dba3bac 46
okano 6:7363c7bc620e 47
okano 6:7363c7bc620e 48
okano 6:7363c7bc620e 49
okano 6:7363c7bc620e 50
okano 6:7363c7bc620e 51
okano 6:7363c7bc620e 52
okano 6:7363c7bc620e 53
okano 6:7363c7bc620e 54 DigitalIn ready( p25 );
okano 7:942d8d0a1760 55 DigitalOut syncout( p21 );
okano 6:7363c7bc620e 56
okano 6:7363c7bc620e 57
okano 6:7363c7bc620e 58
okano 6:7363c7bc620e 59
okano 6:7363c7bc620e 60
okano 6:7363c7bc620e 61
okano 6:7363c7bc620e 62
okano 6:7363c7bc620e 63
okano 0:f4584dba3bac 64 MARY_CAMERA::MARY_CAMERA(
okano 4:cb0ef3fd89c9 65 PinName SPI_mosi,
okano 4:cb0ef3fd89c9 66 PinName SPI_miso,
okano 4:cb0ef3fd89c9 67 PinName SPI_sck,
okano 4:cb0ef3fd89c9 68 PinName SPI_cs,
okano 0:f4584dba3bac 69 PinName cam_reset,
okano 4:cb0ef3fd89c9 70 PinName I2C_sda,
okano 4:cb0ef3fd89c9 71 PinName I2C_scl
okano 2:ee71ffdf317e 72 ) :
okano 4:cb0ef3fd89c9 73 _spi( SPI_mosi, SPI_miso, SPI_sck ),
okano 4:cb0ef3fd89c9 74 _cs( SPI_cs ),
okano 4:cb0ef3fd89c9 75 _reset( cam_reset ),
okano 4:cb0ef3fd89c9 76 _i2c( I2C_sda, I2C_scl )
okano 0:f4584dba3bac 77 {
okano 0:f4584dba3bac 78 init();
okano 0:f4584dba3bac 79 }
okano 0:f4584dba3bac 80
okano 0:f4584dba3bac 81
okano 0:f4584dba3bac 82
okano 0:f4584dba3bac 83 /** initialiation
okano 0:f4584dba3bac 84 *
okano 0:f4584dba3bac 85 * Performs reset and initialization
okano 0:f4584dba3bac 86 */
okano 0:f4584dba3bac 87 void MARY_CAMERA::init( void )
okano 0:f4584dba3bac 88 {
okano 4:cb0ef3fd89c9 89 _cs = 1;
okano 0:f4584dba3bac 90
okano 4:cb0ef3fd89c9 91 _reset = 0;
okano 0:f4584dba3bac 92 wait_ms( RESET_PULSE_WIDTH );
okano 0:f4584dba3bac 93
okano 4:cb0ef3fd89c9 94 _reset = 1;
okano 0:f4584dba3bac 95 wait_ms( RESET_RECOVERY_TIME );
okano 0:f4584dba3bac 96
okano 0:f4584dba3bac 97
okano 0:f4584dba3bac 98 for ( int i = 0; i < PARAM_NUM; i++ ) {
okano 2:ee71ffdf317e 99 #if 1
okano 4:cb0ef3fd89c9 100 _i2c.start();
okano 4:cb0ef3fd89c9 101 _i2c.write( CAM_I2C_ADDR );
okano 4:cb0ef3fd89c9 102 _i2c.write( param[ 0 ][ i ] );
okano 4:cb0ef3fd89c9 103 _i2c.write( param[ 1 ][ i ] );
okano 4:cb0ef3fd89c9 104 _i2c.stop();
okano 2:ee71ffdf317e 105 #else
okano 2:ee71ffdf317e 106
okano 2:ee71ffdf317e 107 char s[ 2 ];
okano 2:ee71ffdf317e 108
okano 2:ee71ffdf317e 109 s[ 0 ] = param[ 0 ][ i ];
okano 2:ee71ffdf317e 110 s[ 1 ] = param[ 1 ][ i ];
okano 2:ee71ffdf317e 111
okano 4:cb0ef3fd89c9 112 _i2c.write( CAM_I2C_ADDR, s, 2 );
okano 2:ee71ffdf317e 113
okano 2:ee71ffdf317e 114 #endif
okano 0:f4584dba3bac 115 wait_ms( 20 );
okano 0:f4584dba3bac 116 }
okano 0:f4584dba3bac 117
okano 4:cb0ef3fd89c9 118 _spi.format( 8 );
okano 4:cb0ef3fd89c9 119 _spi.frequency( SPI_FREQUENCY );
okano 0:f4584dba3bac 120 }
okano 0:f4584dba3bac 121
okano 0:f4584dba3bac 122 void MARY_CAMERA::transfer_a_line( short *p, int line_number, int x_offset, int n_of_pixels )
okano 0:f4584dba3bac 123 {
okano 0:f4584dba3bac 124 set_address( line_number * BYTE_PER_LINE + x_offset * BYTE_PER_PIXEL );
okano 0:f4584dba3bac 125
okano 0:f4584dba3bac 126 read_register( CAMERA_DATA_REGISTER );
okano 0:f4584dba3bac 127
okano 0:f4584dba3bac 128 for( int x = 0; x < n_of_pixels; x++ )
okano 0:f4584dba3bac 129 *p++ = (read_register( CAMERA_DATA_REGISTER ) << 8) | (read_register( CAMERA_DATA_REGISTER ) << 0);
okano 0:f4584dba3bac 130 }
okano 0:f4584dba3bac 131
okano 0:f4584dba3bac 132 void MARY_CAMERA::write_register( char reg, char value )
okano 0:f4584dba3bac 133 {
okano 0:f4584dba3bac 134 send_spi( COMMAND_WRITE | reg );
okano 0:f4584dba3bac 135 send_spi( value );
okano 0:f4584dba3bac 136 }
okano 0:f4584dba3bac 137
okano 0:f4584dba3bac 138 int MARY_CAMERA::read_register( char reg )
okano 0:f4584dba3bac 139 {
okano 0:f4584dba3bac 140 return ( send_spi( COMMAND_READ | reg | ((reg == CAMERA_DATA_REGISTER) ? COMMAND_ADDR_INCREMENT : 0x00) ) );
okano 0:f4584dba3bac 141 }
okano 0:f4584dba3bac 142
okano 7:942d8d0a1760 143 //#define USE_READY_SIGNAL
okano 7:942d8d0a1760 144
okano 0:f4584dba3bac 145 void MARY_CAMERA::open_transfer( void )
okano 0:f4584dba3bac 146 {
okano 0:f4584dba3bac 147 write_register( CONTROL_DATA_REGISTER, CONTROL__PAUSE_BUFFER_UPDATE );
okano 0:f4584dba3bac 148
okano 0:f4584dba3bac 149 read_register( STATUS_REGISTER );
okano 7:942d8d0a1760 150
okano 7:942d8d0a1760 151 syncout = 0;
okano 7:942d8d0a1760 152 #ifdef USE_READY_SIGNAL
okano 6:7363c7bc620e 153 while ( !ready );
okano 7:942d8d0a1760 154 #else
okano 7:942d8d0a1760 155 //while ( !(read_register( STATUS_REGISTER ) & 0x1) );
okano 7:942d8d0a1760 156 while ( 0x51 != read_register( STATUS_REGISTER ) );
okano 7:942d8d0a1760 157 #endif
okano 7:942d8d0a1760 158 syncout = 1;
okano 0:f4584dba3bac 159 }
okano 0:f4584dba3bac 160
okano 0:f4584dba3bac 161 void MARY_CAMERA::close_transfer( void )
okano 0:f4584dba3bac 162 {
okano 0:f4584dba3bac 163 write_register( CONTROL_DATA_REGISTER, CONTROL__RESUME_BUFFER_UPDATE );
okano 0:f4584dba3bac 164
okano 0:f4584dba3bac 165 read_register( STATUS_REGISTER );
okano 7:942d8d0a1760 166
okano 7:942d8d0a1760 167 #ifdef USE_READY_SIGNAL
okano 7:942d8d0a1760 168 while ( ready );
okano 7:942d8d0a1760 169 #else
okano 6:7363c7bc620e 170 // while ( read_register( STATUS_REGISTER ) & 0x1 );
okano 7:942d8d0a1760 171 while ( 0x50 != read_register( STATUS_REGISTER ) );
okano 7:942d8d0a1760 172
okano 7:942d8d0a1760 173 #endif
okano 7:942d8d0a1760 174
okano 0:f4584dba3bac 175 }
okano 0:f4584dba3bac 176
okano 0:f4584dba3bac 177
okano 0:f4584dba3bac 178 void MARY_CAMERA::set_address( int address )
okano 0:f4584dba3bac 179 {
okano 0:f4584dba3bac 180 write_register( MEMORY_ADDR_LOW__REGISTER, (address >> 0) & 0xFF );
okano 0:f4584dba3bac 181 write_register( MEMORY_ADDR_MID__REGISTER, (address >> 8) & 0xFF );
okano 0:f4584dba3bac 182 write_register( MEMORY_ADDR_HIGH_REGISTER, (address >> 16) & 0xFF );
okano 0:f4584dba3bac 183 }
okano 0:f4584dba3bac 184
okano 0:f4584dba3bac 185 int MARY_CAMERA::send_spi( char data )
okano 0:f4584dba3bac 186 {
okano 0:f4584dba3bac 187 int tmp;
okano 0:f4584dba3bac 188
okano 4:cb0ef3fd89c9 189 _cs = 0;
okano 4:cb0ef3fd89c9 190 tmp = _spi.write( data );
okano 4:cb0ef3fd89c9 191 _cs = 1;
okano 4:cb0ef3fd89c9 192
okano 0:f4584dba3bac 193 return ( tmp );
okano 0:f4584dba3bac 194 }