Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of BSP_DISCO_F746NG by
ov7670.c
00001 /** 00002 ****************************************************************************** 00003 * @file ov7670.c 00004 * @author MCD Application Team 00005 * @version V1.0.0 00006 * @date 25-June-2015 00007 * @brief This file provides the OV7670 camera driver 00008 ****************************************************************************** 00009 * @attention 00010 * 00011 * <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2> 00012 * 00013 * Redistribution and use in source and binary forms, with or without modification, 00014 * are permitted provided that the following conditions are met: 00015 * 1. Redistributions of source code must retain the above copyright notice, 00016 * this list of conditions and the following disclaimer. 00017 * 2. Redistributions in binary form must reproduce the above copyright notice, 00018 * this list of conditions and the following disclaimer in the documentation 00019 * and/or other materials provided with the distribution. 00020 * 3. Neither the name of STMicroelectronics nor the names of its contributors 00021 * may be used to endorse or promote products derived from this software 00022 * without specific prior written permission. 00023 * 00024 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00025 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00026 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00027 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 00028 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00029 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 00030 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00031 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 00032 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00033 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00034 * 00035 ****************************************************************************** 00036 */ 00037 00038 /* Includes ------------------------------------------------------------------*/ 00039 #include "ov7670.h" 00040 00041 /** @addtogroup BSP 00042 * @{ 00043 */ 00044 00045 /** @addtogroup Components 00046 * @{ 00047 */ 00048 00049 /** @addtogroup OV7670 00050 * @brief This file provides a set of functions needed to drive the 00051 * OV7670 Camera module. 00052 * @{ 00053 */ 00054 00055 /** @defgroup OV7670_Private_TypesDefinitions 00056 * @{ 00057 */ 00058 00059 /** 00060 * @} 00061 */ 00062 00063 /** @defgroup OV7670_Private_Defines 00064 * @{ 00065 */ 00066 00067 /** 00068 * @} 00069 */ 00070 00071 /** @defgroup OV7670_Private_Macros 00072 * @{ 00073 */ 00074 00075 /** 00076 * @} 00077 */ 00078 00079 /** @defgroup OV7670_Private_FunctionPrototypes 00080 * @{ 00081 */ 00082 static uint64_t ov7670_ConvertValue(uint32_t feature, uint32_t value); 00083 /** 00084 * @} 00085 */ 00086 00087 /** @defgroup OV7670_Private_Variables 00088 * @{ 00089 */ 00090 00091 CAMERA_DrvTypeDef ov7670_drv = 00092 { 00093 ov7670_Init, 00094 ov7670_ReadID, 00095 ov7670_Config, 00096 }; 00097 00098 /* Initialization sequence for VGA resolution (640x480)*/ 00099 const unsigned char OV7670_VGA[][2]= 00100 { 00101 {0x12, 0x04}, //QVGA size, RGB mode 00102 {0x40, 0xd0}, //RGB565 00103 {0xb0, 0x84}, //Color mode 00104 {0x11, 0x00}, //PCLK settings, 30fps 00105 {0x32, 0x80}, //HREF 00106 {0x17, 0x17}, //HSTART 00107 {0x18, 0x05}, //HSTOP 00108 {0x03, 0x0a}, //VREF 00109 {0x19, 0x02}, //VSTART 00110 {0x1a, 0x7a}, //VSTOP 00111 // Additional parameters 00112 {0x34, 0x11}, {0x3f, 0x00}, 00113 {0x75, 0x05}, {0x76, 0xe1}, 00114 {0x4c, 0x00}, {0x77, 0x01}, 00115 {0xb8, 0x0a}, {0x41, 0x18}, 00116 {0x3b, 0x12}, {0xa4, 0x88}, 00117 {0x96, 0x00}, {0x97, 0x30}, 00118 {0x98, 0x20}, {0x99, 0x30}, 00119 {0x9a, 0x84}, {0x9b, 0x29}, 00120 {0x9c, 0x03}, {0x9d, 0x4c}, 00121 {0x9e, 0x3f}, {0x78, 0x04}, 00122 {0x0e, 0x61}, {0x0f, 0x4b}, 00123 {0x16, 0x02}, {0x1e, 0x00}, 00124 {0x21, 0x02}, {0x22, 0x91}, 00125 {0x29, 0x07}, {0x33, 0x0b}, 00126 {0x35, 0x0b}, {0x37, 0x1d}, 00127 {0x38, 0x71}, {0x39, 0x2a}, 00128 {0x3c, 0x78}, {0x4d, 0x40}, 00129 {0x4e, 0x20}, {0x69, 0x00}, 00130 {0x6b, 0x3a}, {0x74, 0x10}, 00131 {0x8d, 0x4f}, {0x8e, 0x00}, 00132 {0x8f, 0x00}, {0x90, 0x00}, 00133 {0x91, 0x00}, {0x96, 0x00}, 00134 {0x9a, 0x00}, {0xb1, 0x0c}, 00135 {0xb2, 0x0e}, {0xb3, 0x82}, 00136 00137 {0x4f, 0x99}, 00138 {0x50, 0x99}, 00139 {0x51, 0x00}, 00140 {0x52, 0x28}, 00141 {0x53, 0x71}, 00142 {0x54, 0x99}, 00143 {0x58, 0x9e}, 00144 {0x56,0x40}, 00145 {0x58,0x9e}, 00146 {0x59,0x88}, 00147 {0x5a,0x88}, 00148 {0x5b,0x44}, 00149 {0x5c,0x67}, 00150 {0x5d,0x49}, 00151 {0x5e,0x0e}, 00152 {0x69,0x00}, 00153 {0x6a,0x40}, 00154 {0x6b,0x0a}, 00155 {0x6c,0x0a}, 00156 {0x6d,0x55}, 00157 {0x6e,0x11}, 00158 {0x6f,0x9f}, 00159 00160 00161 }; 00162 00163 /* Initialization sequence for QVGA resolution (320x240) */ 00164 const unsigned char OV7670_QVGA[][2]= 00165 { 00166 00167 // Image format 00168 {0x12, 0x14}, //QVGA size, RGB mode 00169 {0x40, 0xd0}, //RGB565 00170 {0xb0, 0x84}, //Color mode 00171 00172 // Hardware window 00173 {0x11, 0x01}, //PCLK settings, 15fps 00174 {0x32, 0x80}, //HREF 00175 {0x17, 0x17}, //HSTART 00176 {0x18, 0x05}, //HSTOP 00177 {0x03, 0x0a}, //VREF 00178 {0x19, 0x02}, //VSTART 00179 {0x1a, 0x7a}, //VSTOP 00180 00181 // Scalling numbers 00182 {0x70, 0x3a}, //X_SCALING 00183 {0x71, 0x35}, //Y_SCALING 00184 {0x72, 0x11}, //DCW_SCALING 00185 {0x73, 0xf0}, //PCLK_DIV_SCALING 00186 {0xa2, 0x02}, //PCLK_DELAY_SCALING 00187 00188 // Matrix coefficients 00189 {0x4f, 0x80}, {0x50, 0x80}, 00190 {0x51, 0x00}, {0x52, 0x22}, 00191 {0x53, 0x5e}, {0x54, 0x80}, 00192 {0x58, 0x9e}, 00193 00194 // Gamma curve values 00195 {0x7a, 0x20}, {0x7b, 0x10}, 00196 {0x7c, 0x1e}, {0x7d, 0x35}, 00197 {0x7e, 0x5a}, {0x7f, 0x69}, 00198 {0x80, 0x76}, {0x81, 0x80}, 00199 {0x82, 0x88}, {0x83, 0x8f}, 00200 {0x84, 0x96}, {0x85, 0xa3}, 00201 {0x86, 0xaf}, {0x87, 0xc4}, 00202 {0x88, 0xd7}, {0x89, 0xe8}, 00203 00204 // AGC and AEC parameters 00205 {0xa5, 0x05}, {0xab, 0x07}, 00206 {0x24, 0x95}, {0x25, 0x33}, 00207 {0x26, 0xe3}, {0x9f, 0x78}, 00208 {0xa0, 0x68}, {0xa1, 0x03}, 00209 {0xa6, 0xd8}, {0xa7, 0xd8}, 00210 {0xa8, 0xf0}, {0xa9, 0x90}, 00211 {0xaa, 0x94}, {0x10, 0x00}, 00212 00213 // AWB parameters 00214 {0x43, 0x0a}, {0x44, 0xf0}, 00215 {0x45, 0x34}, {0x46, 0x58}, 00216 {0x47, 0x28}, {0x48, 0x3a}, 00217 {0x59, 0x88}, {0x5a, 0x88}, 00218 {0x5b, 0x44}, {0x5c, 0x67}, 00219 {0x5d, 0x49}, {0x5e, 0x0e}, 00220 {0x6c, 0x0a}, {0x6d, 0x55}, 00221 {0x6e, 0x11}, {0x6f, 0x9f}, 00222 {0x6a, 0x40}, {0x01, 0x40}, 00223 {0x02, 0x60}, {0x13, 0xe7}, 00224 00225 // Additional parameters 00226 {0x34, 0x11}, {0x3f, 0x00}, 00227 {0x75, 0x05}, {0x76, 0xe1}, 00228 {0x4c, 0x00}, {0x77, 0x01}, 00229 {0xb8, 0x0a}, {0x41, 0x18}, 00230 {0x3b, 0x12}, {0xa4, 0x88}, 00231 {0x96, 0x00}, {0x97, 0x30}, 00232 {0x98, 0x20}, {0x99, 0x30}, 00233 {0x9a, 0x84}, {0x9b, 0x29}, 00234 {0x9c, 0x03}, {0x9d, 0x4c}, 00235 {0x9e, 0x3f}, {0x78, 0x04}, 00236 {0x0e, 0x61}, {0x0f, 0x4b}, 00237 {0x16, 0x02}, {0x1e, 0x00}, 00238 {0x21, 0x02}, {0x22, 0x91}, 00239 {0x29, 0x07}, {0x33, 0x0b}, 00240 {0x35, 0x0b}, {0x37, 0x1d}, 00241 {0x38, 0x71}, {0x39, 0x2a}, 00242 {0x3c, 0x78}, {0x4d, 0x40}, 00243 {0x4e, 0x20}, {0x69, 0x00}, 00244 {0x6b, 0x3a}, {0x74, 0x10}, 00245 {0x8d, 0x4f}, {0x8e, 0x00}, 00246 {0x8f, 0x00}, {0x90, 0x00}, 00247 {0x91, 0x00}, {0x96, 0x00}, 00248 {0x9a, 0x00}, {0xb1, 0x0c}, 00249 {0xb2, 0x0e}, {0xb3, 0x82}, 00250 {0x4b, 0x01}, 00251 }; 00252 00253 /* Initialization sequence for QQVGA resolution (160x120) */ 00254 const char OV7670_QQVGA[][2]= 00255 { 00256 // Not implemented yet 00257 {0x12, 0x14}, //QVGA size, RGB mode 00258 {0x40, 0xd0}, //RGB565 00259 {0xb0, 0x84}, //Color mode 00260 00261 }; 00262 00263 /** 00264 * @} 00265 */ 00266 00267 /** @defgroup OV7670_Private_Functions 00268 * @{ 00269 */ 00270 00271 /** 00272 * @brief Initializes the OV7670 CAMERA component. 00273 * @param DeviceAddr: Device address on communication Bus. 00274 * @param resolution: Camera resolution 00275 * @retval None 00276 */ 00277 void ov7670_Init(uint16_t DeviceAddr, uint32_t resolution) 00278 { 00279 uint32_t index; 00280 00281 /* Initialize I2C */ 00282 CAMERA_IO_Init(); 00283 00284 /* Prepare the camera to be configured by resetting all its registers */ 00285 CAMERA_IO_Write(DeviceAddr, OV7670_SENSOR_COM7, 0x80); 00286 CAMERA_Delay(200); 00287 00288 /* Initialize OV7670 */ 00289 switch (resolution) 00290 { 00291 case CAMERA_R160x120: 00292 { 00293 for(index=0; index<(sizeof(OV7670_QQVGA)/2); index++) 00294 { 00295 CAMERA_IO_Write(DeviceAddr, OV7670_QQVGA[index][0], OV7670_QQVGA[index][1]); 00296 CAMERA_Delay(20); 00297 } 00298 break; 00299 } 00300 case CAMERA_R320x240: 00301 { 00302 for(index=0; index<(sizeof(OV7670_QVGA)/2); index++) 00303 { 00304 CAMERA_IO_Write(DeviceAddr, OV7670_QVGA[index][0], OV7670_QVGA[index][1]); 00305 CAMERA_Delay(20); 00306 } 00307 break; 00308 } 00309 case CAMERA_R480x272: 00310 { 00311 /* Not supported resolution */ 00312 break; 00313 } 00314 case CAMERA_R640x480: 00315 { 00316 for(index=0; index<(sizeof(OV7670_VGA)/2); index++) 00317 { 00318 CAMERA_IO_Write(DeviceAddr, OV7670_VGA[index][0], OV7670_VGA[index][1]); 00319 CAMERA_Delay(20); 00320 } 00321 break; 00322 } 00323 default: 00324 { 00325 break; 00326 } 00327 } 00328 } 00329 00330 /** 00331 * @brief Configures the OV7670 camera feature. 00332 * @param DeviceAddr: Device address on communication Bus. 00333 * @param feature: Camera feature to be configured 00334 * @param value: Value to be configured 00335 * @param brightness_value: Brightness value to be configured 00336 * @retval None 00337 */ 00338 void ov7670_Config(uint16_t DeviceAddr, uint32_t feature, uint32_t value, uint32_t brightness_value) 00339 { 00340 uint8_t tslb, mtx1, mtx2, mtx3, mtx4, mtx5, mtx6; 00341 uint64_t value_tmp; 00342 uint32_t br_value; 00343 00344 /* Convert the input value into ov7670 parameters */ 00345 value_tmp = ov7670_ConvertValue(feature, value); 00346 br_value = (uint32_t)ov7670_ConvertValue(CAMERA_CONTRAST_BRIGHTNESS, brightness_value); 00347 00348 switch(feature) 00349 { 00350 case CAMERA_CONTRAST_BRIGHTNESS: 00351 { 00352 CAMERA_IO_Write(DeviceAddr, OV7670_SENSOR_BRTN, br_value); 00353 CAMERA_IO_Write(DeviceAddr, OV7670_SENSOR_CNST1, value_tmp); 00354 break; 00355 } 00356 case CAMERA_BLACK_WHITE: 00357 case CAMERA_COLOR_EFFECT: 00358 { 00359 tslb = (uint8_t)(value_tmp >> 48); 00360 mtx1 = (uint8_t)(value_tmp >> 40); 00361 mtx2 = (uint8_t)(value_tmp >> 32); 00362 mtx3 = (uint8_t)(value_tmp >> 24); 00363 mtx4 = (uint8_t)(value_tmp >> 16); 00364 mtx5 = (uint8_t)(value_tmp >> 8); 00365 mtx6 = (uint8_t)(value_tmp); 00366 CAMERA_IO_Write(DeviceAddr, OV7670_SENSOR_TSLB, tslb); 00367 CAMERA_IO_Write(DeviceAddr, OV7670_SENSOR_MTX1, mtx1); 00368 CAMERA_IO_Write(DeviceAddr, OV7670_SENSOR_MTX2, mtx2); 00369 CAMERA_IO_Write(DeviceAddr, OV7670_SENSOR_MTX3, mtx3); 00370 CAMERA_IO_Write(DeviceAddr, OV7670_SENSOR_MTX4, mtx4); 00371 CAMERA_IO_Write(DeviceAddr, OV7670_SENSOR_MTX5, mtx5); 00372 CAMERA_IO_Write(DeviceAddr, OV7670_SENSOR_MTX6, mtx6); 00373 break; 00374 } 00375 default: 00376 { 00377 break; 00378 } 00379 } 00380 } 00381 00382 /** 00383 * @brief Read the OV7670 Camera identity. 00384 * @param DeviceAddr: Device address on communication Bus. 00385 * @retval the OV7670 ID 00386 */ 00387 uint16_t ov7670_ReadID(uint16_t DeviceAddr) 00388 { 00389 /* Initialize I2C */ 00390 CAMERA_IO_Init(); 00391 00392 /* Get the camera ID */ 00393 return (CAMERA_IO_Read(DeviceAddr, OV7670_SENSOR_PIDH)); 00394 } 00395 00396 /****************************************************************************** 00397 Static Functions 00398 *******************************************************************************/ 00399 /** 00400 * @brief Convert input values into ov7670 parameters. 00401 * @param feature: Camera feature to be configured 00402 * @param value: Value to be configured 00403 * @retval The converted value 00404 */ 00405 static uint64_t ov7670_ConvertValue(uint32_t feature, uint32_t value) 00406 { 00407 uint64_t ret = 0; 00408 00409 switch(feature) 00410 { 00411 case CAMERA_BLACK_WHITE: 00412 { 00413 switch(value) 00414 { 00415 case CAMERA_BLACK_WHITE_BW: 00416 { 00417 ret = OV7670_BLACK_WHITE_BW; 00418 break; 00419 } 00420 case CAMERA_BLACK_WHITE_NEGATIVE: 00421 { 00422 ret = OV7670_BLACK_WHITE_NEGATIVE; 00423 break; 00424 } 00425 case CAMERA_BLACK_WHITE_BW_NEGATIVE: 00426 { 00427 ret = OV7670_BLACK_WHITE_BW_NEGATIVE; 00428 break; 00429 } 00430 case CAMERA_BLACK_WHITE_NORMAL: 00431 { 00432 ret = OV7670_BLACK_WHITE_NORMAL; 00433 break; 00434 } 00435 default: 00436 { 00437 ret = OV7670_BLACK_WHITE_NORMAL; 00438 break; 00439 } 00440 } 00441 break; 00442 } 00443 case CAMERA_CONTRAST_BRIGHTNESS: 00444 { 00445 switch(value) 00446 { 00447 case CAMERA_BRIGHTNESS_LEVEL0: 00448 { 00449 ret = OV7670_BRIGHTNESS_LEVEL0; 00450 break; 00451 } 00452 case CAMERA_BRIGHTNESS_LEVEL1: 00453 { 00454 ret = OV7670_BRIGHTNESS_LEVEL1; 00455 break; 00456 } 00457 case CAMERA_BRIGHTNESS_LEVEL2: 00458 { 00459 ret = OV7670_BRIGHTNESS_LEVEL2; 00460 break; 00461 } 00462 case CAMERA_BRIGHTNESS_LEVEL3: 00463 { 00464 ret = OV7670_BRIGHTNESS_LEVEL3; 00465 break; 00466 } 00467 case CAMERA_BRIGHTNESS_LEVEL4: 00468 { 00469 ret = OV7670_BRIGHTNESS_LEVEL4; 00470 break; 00471 } 00472 case CAMERA_CONTRAST_LEVEL0: 00473 { 00474 ret = OV7670_CONTRAST_LEVEL0; 00475 break; 00476 } 00477 case CAMERA_CONTRAST_LEVEL1: 00478 { 00479 ret = OV7670_CONTRAST_LEVEL1; 00480 break; 00481 } 00482 case CAMERA_CONTRAST_LEVEL2: 00483 { 00484 ret = OV7670_CONTRAST_LEVEL2; 00485 break; 00486 } 00487 case CAMERA_CONTRAST_LEVEL3: 00488 { 00489 ret = OV7670_CONTRAST_LEVEL3; 00490 break; 00491 } 00492 case CAMERA_CONTRAST_LEVEL4: 00493 { 00494 ret = OV7670_CONTRAST_LEVEL4; 00495 break; 00496 } 00497 default: 00498 { 00499 ret = OV7670_CONTRAST_LEVEL0; 00500 break; 00501 } 00502 } 00503 break; 00504 } 00505 case CAMERA_COLOR_EFFECT: 00506 { 00507 switch(value) 00508 { 00509 case CAMERA_COLOR_EFFECT_ANTIQUE: 00510 { 00511 ret = OV7670_COLOR_EFFECT_ANTIQUE; 00512 break; 00513 } 00514 case CAMERA_COLOR_EFFECT_BLUE: 00515 { 00516 ret = OV7670_COLOR_EFFECT_BLUE; 00517 break; 00518 } 00519 case CAMERA_COLOR_EFFECT_GREEN: 00520 { 00521 ret = OV7670_COLOR_EFFECT_GREEN; 00522 break; 00523 } 00524 case CAMERA_COLOR_EFFECT_RED: 00525 { 00526 ret = OV7670_COLOR_EFFECT_RED; 00527 break; 00528 } 00529 case CAMERA_COLOR_EFFECT_NONE: 00530 default: 00531 { 00532 ret = OV7670_COLOR_EFFECT_NONE; 00533 break; 00534 } 00535 } 00536 break; 00537 default: 00538 { 00539 ret = 0; 00540 break; 00541 } 00542 } 00543 } 00544 00545 return ret; 00546 } 00547 00548 /** 00549 * @} 00550 */ 00551 00552 /** 00553 * @} 00554 */ 00555 00556 /** 00557 * @} 00558 */ 00559 00560 /** 00561 * @} 00562 */ 00563 00564 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Generated on Wed Jul 13 2022 20:24:49 by
1.7.2
