test
Dependencies: mbed Prelude_OV5642 Arducam_UTFT_SPI Arducam_OV5642 Arduino WIZnetInterface_Ricky
main.cpp@3:23d439c8526b, 2015-10-29 (annotated)
- Committer:
- justinkim
- Date:
- Thu Oct 29 10:09:41 2015 +0000
- Revision:
- 3:23d439c8526b
- Parent:
- 2:3a12c5e8b030
- Child:
- 4:63042b865304
camera test success!
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
justinkim | 0:e01f64037748 | 1 | #include "mbed.h" |
justinkim | 0:e01f64037748 | 2 | #include "UTFT_SPI.h" |
justinkim | 0:e01f64037748 | 3 | #include "OV5642.h" |
justinkim | 0:e01f64037748 | 4 | #include "OV5642_regs.h" |
justinkim | 3:23d439c8526b | 5 | #include "SDFileSystem.h" |
justinkim | 0:e01f64037748 | 6 | #include "Arduino.h" |
justinkim | 0:e01f64037748 | 7 | |
justinkim | 0:e01f64037748 | 8 | void setup(); |
justinkim | 0:e01f64037748 | 9 | void loop(); |
justinkim | 3:23d439c8526b | 10 | void itoa( unsigned long long int value, char *str); |
justinkim | 0:e01f64037748 | 11 | |
justinkim | 0:e01f64037748 | 12 | ArduCAM myCAM(D11, D12, D13, D10, D14, D15); |
justinkim | 1:3a14a4c84db2 | 13 | ArduLCD myGLCD(D11, D12, D13, D10); |
justinkim | 3:23d439c8526b | 14 | SDFileSystem sd(D11, D12, D13, D9, "sd"); |
justinkim | 0:e01f64037748 | 15 | Serial pc(USBTX, USBRX); |
justinkim | 3:23d439c8526b | 16 | bool isShowFlag = true; |
justinkim | 3:23d439c8526b | 17 | char fname[32]; |
justinkim | 3:23d439c8526b | 18 | char fnamecnt=0; |
justinkim | 0:e01f64037748 | 19 | |
justinkim | 0:e01f64037748 | 20 | int main() |
justinkim | 0:e01f64037748 | 21 | { |
justinkim | 3:23d439c8526b | 22 | *(volatile uint32_t *)(0x41001014) = 0x0060200; //clock 48MHz |
justinkim | 0:e01f64037748 | 23 | |
justinkim | 0:e01f64037748 | 24 | setup(); |
justinkim | 0:e01f64037748 | 25 | |
justinkim | 0:e01f64037748 | 26 | while(1) |
justinkim | 0:e01f64037748 | 27 | { |
justinkim | 0:e01f64037748 | 28 | loop(); |
justinkim | 0:e01f64037748 | 29 | } |
justinkim | 0:e01f64037748 | 30 | } |
justinkim | 0:e01f64037748 | 31 | |
justinkim | 0:e01f64037748 | 32 | void setup() |
justinkim | 0:e01f64037748 | 33 | { |
justinkim | 0:e01f64037748 | 34 | uint8_t vid,pid; |
justinkim | 0:e01f64037748 | 35 | uint8_t temp; |
justinkim | 0:e01f64037748 | 36 | |
justinkim | 0:e01f64037748 | 37 | pc.baud(115200); |
justinkim | 0:e01f64037748 | 38 | pc.printf("ArduCAM Start!\r\n"); |
justinkim | 1:3a14a4c84db2 | 39 | |
justinkim | 1:3a14a4c84db2 | 40 | uint8_t temp1,temp2; |
justinkim | 1:3a14a4c84db2 | 41 | myCAM.write_reg(ARDUCHIP_TEST1, 0x55); //Write to test1 register by 0x55 |
justinkim | 1:3a14a4c84db2 | 42 | myCAM.write_reg(ARDUCHIP_TEST2, 0xAA); //Write to test1 register by 0xaa |
justinkim | 1:3a14a4c84db2 | 43 | wait_ms(1000); |
justinkim | 1:3a14a4c84db2 | 44 | temp1 = myCAM.read_reg(ARDUCHIP_TEST1); //Read from test1 register |
justinkim | 1:3a14a4c84db2 | 45 | temp2 = myCAM.read_reg(ARDUCHIP_TEST2); //Read from test1 register |
justinkim | 1:3a14a4c84db2 | 46 | pc.printf("temp1 : %d\r\n",temp1); |
justinkim | 1:3a14a4c84db2 | 47 | pc.printf("temp2 : %d\r\n",temp2); |
justinkim | 1:3a14a4c84db2 | 48 | wait_ms(1000); |
justinkim | 1:3a14a4c84db2 | 49 | |
justinkim | 0:e01f64037748 | 50 | myCAM.write_reg(ARDUCHIP_TEST1, 0x55); |
justinkim | 0:e01f64037748 | 51 | temp = myCAM.read_reg(ARDUCHIP_TEST1); |
justinkim | 1:3a14a4c84db2 | 52 | |
justinkim | 0:e01f64037748 | 53 | if(temp != 0x55) |
justinkim | 0:e01f64037748 | 54 | { |
justinkim | 0:e01f64037748 | 55 | pc.printf("SPI interface Error!\r\n"); |
justinkim | 0:e01f64037748 | 56 | while(1); |
justinkim | 0:e01f64037748 | 57 | } |
justinkim | 0:e01f64037748 | 58 | |
justinkim | 0:e01f64037748 | 59 | //Change MCU mode |
justinkim | 0:e01f64037748 | 60 | myCAM.set_mode(MCU2LCD_MODE); |
justinkim | 0:e01f64037748 | 61 | |
justinkim | 0:e01f64037748 | 62 | //Initialize the LCD Module |
justinkim | 1:3a14a4c84db2 | 63 | myGLCD.InitLCD(); |
justinkim | 0:e01f64037748 | 64 | |
justinkim | 0:e01f64037748 | 65 | //Check if the camera module type is OV5642 |
justinkim | 0:e01f64037748 | 66 | myCAM.rdSensorReg16_8(OV5642_CHIPID_HIGH, &vid); |
justinkim | 0:e01f64037748 | 67 | myCAM.rdSensorReg16_8(OV5642_CHIPID_LOW, &pid); |
justinkim | 0:e01f64037748 | 68 | if((vid != 0x56) || (pid != 0x42)) |
justinkim | 0:e01f64037748 | 69 | pc.printf("Can't find OV5642 module!\r\n"); |
justinkim | 0:e01f64037748 | 70 | else |
justinkim | 0:e01f64037748 | 71 | pc.printf("OV5642 detected\r\n"); |
justinkim | 0:e01f64037748 | 72 | |
justinkim | 3:23d439c8526b | 73 | //Change to BMP capture mode and initialize the OV5642 module |
justinkim | 3:23d439c8526b | 74 | myCAM.set_format(BMP); |
justinkim | 3:23d439c8526b | 75 | |
justinkim | 0:e01f64037748 | 76 | myCAM.InitCAM(); |
justinkim | 0:e01f64037748 | 77 | } |
justinkim | 3:23d439c8526b | 78 | |
justinkim | 0:e01f64037748 | 79 | void loop() |
justinkim | 0:e01f64037748 | 80 | { |
justinkim | 3:23d439c8526b | 81 | FILE *outFile; |
justinkim | 3:23d439c8526b | 82 | uint8_t buf[256]; |
justinkim | 3:23d439c8526b | 83 | static int i = 0; |
justinkim | 3:23d439c8526b | 84 | uint8_t temp,temp_last; |
justinkim | 3:23d439c8526b | 85 | uint8_t start_capture = 0; |
justinkim | 3:23d439c8526b | 86 | |
justinkim | 3:23d439c8526b | 87 | //Wait trigger from shutter buttom |
justinkim | 3:23d439c8526b | 88 | if(myCAM.get_bit(ARDUCHIP_TRIG , SHUTTER_MASK)) |
justinkim | 3:23d439c8526b | 89 | { |
justinkim | 3:23d439c8526b | 90 | isShowFlag = false; |
justinkim | 3:23d439c8526b | 91 | myCAM.set_mode(MCU2LCD_MODE); |
justinkim | 3:23d439c8526b | 92 | myCAM.set_format(JPEG); |
justinkim | 3:23d439c8526b | 93 | myCAM.InitCAM(); |
justinkim | 3:23d439c8526b | 94 | myCAM.OV5642_set_JPEG_size(); |
justinkim | 3:23d439c8526b | 95 | myCAM.write_reg(ARDUCHIP_TIM, VSYNC_LEVEL_MASK); //VSYNC is active HIGH |
justinkim | 3:23d439c8526b | 96 | |
justinkim | 3:23d439c8526b | 97 | //Wait until buttom released |
justinkim | 3:23d439c8526b | 98 | while(myCAM.get_bit(ARDUCHIP_TRIG, SHUTTER_MASK)); |
justinkim | 3:23d439c8526b | 99 | wait_ms(1000); |
justinkim | 3:23d439c8526b | 100 | start_capture = 1; |
justinkim | 3:23d439c8526b | 101 | } |
justinkim | 3:23d439c8526b | 102 | else |
justinkim | 3:23d439c8526b | 103 | { |
justinkim | 3:23d439c8526b | 104 | if(isShowFlag ) |
justinkim | 3:23d439c8526b | 105 | { |
justinkim | 0:e01f64037748 | 106 | |
justinkim | 3:23d439c8526b | 107 | if(!myCAM.get_bit(ARDUCHIP_TRIG,VSYNC_MASK)) //New Frame is coming |
justinkim | 3:23d439c8526b | 108 | { |
justinkim | 3:23d439c8526b | 109 | myCAM.set_mode(MCU2LCD_MODE); //Switch to MCU |
justinkim | 3:23d439c8526b | 110 | myGLCD.resetXY(); |
justinkim | 3:23d439c8526b | 111 | myCAM.set_mode(CAM2LCD_MODE); //Switch to CAM |
justinkim | 3:23d439c8526b | 112 | while(!myCAM.get_bit(ARDUCHIP_TRIG,VSYNC_MASK)); //Wait for VSYNC is gone |
justinkim | 3:23d439c8526b | 113 | } |
justinkim | 3:23d439c8526b | 114 | } |
justinkim | 3:23d439c8526b | 115 | } |
justinkim | 3:23d439c8526b | 116 | if(start_capture) |
justinkim | 3:23d439c8526b | 117 | { |
justinkim | 3:23d439c8526b | 118 | //Flush the FIFO |
justinkim | 3:23d439c8526b | 119 | myCAM.flush_fifo(); |
justinkim | 3:23d439c8526b | 120 | //Clear the capture done flag |
justinkim | 3:23d439c8526b | 121 | myCAM.clear_fifo_flag(); |
justinkim | 3:23d439c8526b | 122 | //Start capture |
justinkim | 3:23d439c8526b | 123 | myCAM.start_capture(); |
justinkim | 3:23d439c8526b | 124 | pc.printf("Start Capture\r\n"); |
justinkim | 3:23d439c8526b | 125 | } |
justinkim | 3:23d439c8526b | 126 | |
justinkim | 3:23d439c8526b | 127 | if(myCAM.get_bit(ARDUCHIP_TRIG ,CAP_DONE_MASK)) |
justinkim | 0:e01f64037748 | 128 | { |
justinkim | 0:e01f64037748 | 129 | |
justinkim | 3:23d439c8526b | 130 | pc.printf("Capture Done!\r\n"); |
justinkim | 3:23d439c8526b | 131 | //Construct a file name |
justinkim | 3:23d439c8526b | 132 | snprintf(fname, sizeof(fname), "/sd/jpss%04d.jpg", fnamecnt); |
justinkim | 3:23d439c8526b | 133 | fnamecnt++; |
justinkim | 3:23d439c8526b | 134 | //Open the new file |
justinkim | 3:23d439c8526b | 135 | outFile = fopen(fname,"w"); |
justinkim | 3:23d439c8526b | 136 | if (! outFile) |
justinkim | 3:23d439c8526b | 137 | { |
justinkim | 3:23d439c8526b | 138 | pc.printf("open file failed\r\n"); |
justinkim | 3:23d439c8526b | 139 | return; |
justinkim | 3:23d439c8526b | 140 | } |
justinkim | 3:23d439c8526b | 141 | //Read first dummy byte |
justinkim | 3:23d439c8526b | 142 | //myCAM.read_fifo(); |
justinkim | 3:23d439c8526b | 143 | |
justinkim | 3:23d439c8526b | 144 | i = 0; |
justinkim | 3:23d439c8526b | 145 | temp = myCAM.read_fifo(); |
justinkim | 3:23d439c8526b | 146 | //Write first image data to buffer |
justinkim | 3:23d439c8526b | 147 | buf[i++] = temp; |
justinkim | 3:23d439c8526b | 148 | |
justinkim | 3:23d439c8526b | 149 | //Read JPEG data from FIFO |
justinkim | 3:23d439c8526b | 150 | while( (temp != 0xD9) | (temp_last != 0xFF) ) |
justinkim | 0:e01f64037748 | 151 | { |
justinkim | 3:23d439c8526b | 152 | temp_last = temp; |
justinkim | 3:23d439c8526b | 153 | temp = myCAM.read_fifo(); |
justinkim | 3:23d439c8526b | 154 | //Write image data to buffer if not full |
justinkim | 3:23d439c8526b | 155 | if(i < 256) |
justinkim | 3:23d439c8526b | 156 | buf[i++] = temp; |
justinkim | 3:23d439c8526b | 157 | else |
justinkim | 3:23d439c8526b | 158 | { |
justinkim | 3:23d439c8526b | 159 | //Write 256 bytes image data to file |
justinkim | 3:23d439c8526b | 160 | fwrite(buf,256,1,outFile); |
justinkim | 3:23d439c8526b | 161 | i = 0; |
justinkim | 3:23d439c8526b | 162 | buf[i++] = temp; |
justinkim | 3:23d439c8526b | 163 | } |
justinkim | 0:e01f64037748 | 164 | } |
justinkim | 3:23d439c8526b | 165 | //Write the remain bytes in the buffer |
justinkim | 3:23d439c8526b | 166 | if(i > 0) |
justinkim | 3:23d439c8526b | 167 | fwrite(buf,i,1,outFile); |
justinkim | 3:23d439c8526b | 168 | |
justinkim | 3:23d439c8526b | 169 | //Close the file |
justinkim | 3:23d439c8526b | 170 | fclose(outFile); |
justinkim | 3:23d439c8526b | 171 | //Clear the capture done flag |
justinkim | 3:23d439c8526b | 172 | myCAM.clear_fifo_flag(); |
justinkim | 3:23d439c8526b | 173 | //Clear the start capture flag |
justinkim | 3:23d439c8526b | 174 | start_capture = 0; |
justinkim | 3:23d439c8526b | 175 | |
justinkim | 3:23d439c8526b | 176 | myCAM.set_format(BMP); |
justinkim | 3:23d439c8526b | 177 | myCAM.InitCAM(); |
justinkim | 3:23d439c8526b | 178 | isShowFlag = true; |
justinkim | 0:e01f64037748 | 179 | } |
justinkim | 0:e01f64037748 | 180 | } |
justinkim | 3:23d439c8526b | 181 | |
justinkim | 3:23d439c8526b | 182 | /* itoa: convert n to characters in s */ |
justinkim | 3:23d439c8526b | 183 | void itoa( unsigned long long int value, char *str) |
justinkim | 3:23d439c8526b | 184 | { |
justinkim | 3:23d439c8526b | 185 | int i,j; |
justinkim | 3:23d439c8526b | 186 | char temp[30]; |
justinkim | 3:23d439c8526b | 187 | for(i=0; value > 0; i++){ |
justinkim | 3:23d439c8526b | 188 | str[i] = value%10+'0'; |
justinkim | 3:23d439c8526b | 189 | value=value/10; |
justinkim | 3:23d439c8526b | 190 | } |
justinkim | 3:23d439c8526b | 191 | for(j=0;i>=0;j++,i--){ |
justinkim | 3:23d439c8526b | 192 | temp[j]=str[i-1]; |
justinkim | 3:23d439c8526b | 193 | } |
justinkim | 3:23d439c8526b | 194 | for(i=0;i<j;i++){ |
justinkim | 3:23d439c8526b | 195 | str[i]=temp[i]; |
justinkim | 3:23d439c8526b | 196 | } |
justinkim | 3:23d439c8526b | 197 | } |