ov2640.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180
  1. /* Includes ------------------------------------------------------------------*/
  2. #include "ov2640.h"
  3. #include "xclk.h"
  4. #include "nrf_drv_gpiote.h"
  5. #include "app_scheduler.h"
  6. #include "user_twim.h"
  7. #include "app_timer.h"
  8. #include "action.h"
  9. static custome_event ov2640_event;
  10. static void Set_Ov2640_Queue(OV2640_QUEUE_TYPE type);
  11. static ov2640_work_e ov2640_work = OV2640_WORK_RST;
  12. static uint32_t ov2640_data_Array[8] = {
  13. CAM_D0_PIN, CAM_D1_PIN, CAM_D2_PIN, CAM_D3_PIN,
  14. CAM_D4_PIN, CAM_D5_PIN, CAM_D6_PIN, CAM_D7_PIN
  15. };
  16. APP_TIMER_DEF(ov2640_timer_id);
  17. static uint32_t ov2640_timer_tick = 0;
  18. static bool ov2640_timer_end = true;
  19. static bool ov2640_timer_stat = false;
  20. //TWI传输完成标志
  21. static volatile bool m_xfer_done = false;
  22. //TWI传输超时标志
  23. static volatile bool m_xfer_timeout = false;
  24. static uint8_t OV2640_ReadReg(uint16_t Addr);
  25. static ret_code_t OV2640_WriteReg(uint16_t Addr, uint8_t Data);
  26. /** @addtogroup
  27. * @{
  28. */
  29. /** @addtogroup DCMI_Camera
  30. * @{
  31. */
  32. /* Exported types ------------------------------------------------------------*/
  33. /* Camera devices enumeration */
  34. /* Private typedef -----------------------------------------------------------*/
  35. /* Private define ------------------------------------------------------------*/
  36. /* Private macro -------------------------------------------------------------*/
  37. /* Private variables ---------------------------------------------------------*/
  38. const unsigned char OV2640_JPEG_INIT[][2]=
  39. {
  40. 0xff, 0x00,
  41. 0x2c, 0xff,
  42. 0x2e, 0xdf,
  43. 0xff, 0x01,
  44. 0x3c, 0x32,
  45. 0x11, 0x00,
  46. 0x09, 0x02,
  47. 0x04, 0x28,
  48. 0x13, 0xe5,
  49. 0x14, 0x48,
  50. 0x2c, 0x0c,
  51. 0x33, 0x78,
  52. 0x3a, 0x33,
  53. 0x3b, 0xfB,
  54. 0x3e, 0x00,
  55. 0x43, 0x11,
  56. 0x16, 0x10,
  57. 0x39, 0x92,
  58. 0x35, 0xda,
  59. 0x22, 0x1a,
  60. 0x37, 0xc3,
  61. 0x23, 0x00,
  62. 0x34, 0xc0,
  63. 0x36, 0x1a,
  64. 0x06, 0x88,
  65. 0x07, 0xc0,
  66. 0x0d, 0x87,
  67. 0x0e, 0x41,
  68. 0x4c, 0x00,
  69. 0x48, 0x00,
  70. 0x5B, 0x00,
  71. 0x42, 0x03,
  72. 0x4a, 0x81,
  73. 0x21, 0x99,
  74. 0x24, 0x40,
  75. 0x25, 0x38,
  76. 0x26, 0x82,
  77. 0x5c, 0x00,
  78. 0x63, 0x00,
  79. 0x61, 0x70,
  80. 0x62, 0x80,
  81. 0x7c, 0x05,
  82. 0x20, 0x80,
  83. 0x28, 0x30,
  84. 0x6c, 0x00,
  85. 0x6d, 0x80,
  86. 0x6e, 0x00,
  87. 0x70, 0x02,
  88. 0x71, 0x94,
  89. 0x73, 0xc1,
  90. 0x12, 0x40,//0x40
  91. 0x17, 0x11,
  92. 0x18, 0x43,
  93. 0x19, 0x00,
  94. 0x1a, 0x4b,
  95. 0x32, 0x09,
  96. 0x37, 0xc0,
  97. 0x4f, 0x60,
  98. 0x50, 0xa8,
  99. 0x6d, 0x00,
  100. 0x3d, 0x38,
  101. 0x46, 0x3f,
  102. 0x4f, 0x60,
  103. 0x0c, 0x3c,
  104. 0xff, 0x00,
  105. 0xe5, 0x7f,
  106. 0xf9, 0xc0,
  107. 0x41, 0x24,
  108. 0xe0, 0x14,
  109. 0x76, 0xff,
  110. 0x33, 0xa0,
  111. 0x42, 0x20,
  112. 0x43, 0x18,
  113. 0x4c, 0x00,
  114. 0x87, 0xd5,
  115. 0x88, 0x3f,
  116. 0xd7, 0x03,
  117. 0xd9, 0x10,
  118. 0xd3, 0x82,
  119. 0xc8, 0x08,
  120. 0xc9, 0x80,
  121. 0x7c, 0x00,
  122. 0x7d, 0x00,
  123. 0x7c, 0x03,
  124. 0x7d, 0x48,
  125. 0x7d, 0x48,
  126. 0x7c, 0x08,
  127. 0x7d, 0x20,
  128. 0x7d, 0x10,
  129. 0x7d, 0x0e,
  130. 0x90, 0x00,
  131. 0x91, 0x0e,
  132. 0x91, 0x1a,
  133. 0x91, 0x31,
  134. 0x91, 0x5a,
  135. 0x91, 0x69,
  136. 0x91, 0x75,
  137. 0x91, 0x7e,
  138. 0x91, 0x88,
  139. 0x91, 0x8f,
  140. 0x91, 0x96,
  141. 0x91, 0xa3,
  142. 0x91, 0xaf,
  143. 0x91, 0xc4,
  144. 0x91, 0xd7,
  145. 0x91, 0xe8,
  146. 0x91, 0x20,
  147. 0x92, 0x00,
  148. 0x93, 0x06,
  149. 0x93, 0xe3,
  150. 0x93, 0x05,
  151. 0x93, 0x05,
  152. 0x93, 0x00,
  153. 0x93, 0x04,
  154. 0x93, 0x00,
  155. 0x93, 0x00,
  156. 0x93, 0x00,
  157. 0x93, 0x00,
  158. 0x93, 0x00,
  159. 0x93, 0x00,
  160. 0x93, 0x00,
  161. 0x96, 0x00,
  162. 0x97, 0x08,
  163. 0x97, 0x19,
  164. 0x97, 0x02,
  165. 0x97, 0x0c,
  166. 0x97, 0x24,
  167. 0x97, 0x30,
  168. 0x97, 0x28,
  169. 0x97, 0x26,
  170. 0x97, 0x02,
  171. 0x97, 0x98,
  172. 0x97, 0x80,
  173. 0x97, 0x00,
  174. 0x97, 0x00,
  175. 0xc3, 0xed,
  176. 0xa4, 0x00,
  177. 0xa8, 0x00,
  178. 0xc5, 0x11,
  179. 0xc6, 0x51,
  180. 0xbf, 0x80,
  181. 0xc7, 0x10,
  182. 0xb6, 0x66,
  183. 0xb8, 0xA5,
  184. 0xb7, 0x64,
  185. 0xb9, 0x7C,
  186. 0xb3, 0xaf,
  187. 0xb4, 0x97,
  188. 0xb5, 0xFF,
  189. 0xb0, 0xC5,
  190. 0xb1, 0x94,
  191. 0xb2, 0x0f,
  192. 0xc4, 0x5c,
  193. 0xc0, 0x64,
  194. 0xc1, 0x4B,
  195. 0x8c, 0x00,
  196. 0x86, 0x3D,
  197. 0x50, 0x00,
  198. 0x51, 0xC8,
  199. 0x52, 0x96,
  200. 0x53, 0x00,
  201. 0x54, 0x00,
  202. 0x55, 0x00,
  203. 0x5a, 0xC8,
  204. 0x5b, 0x96,
  205. 0x5c, 0x00,
  206. 0xd3, 0x7f,
  207. 0xc3, 0xed,
  208. 0x7f, 0x00,
  209. 0xda, 0x00,
  210. 0xe5, 0x1f,
  211. 0xe1, 0x67,
  212. 0xe0, 0x00,
  213. 0xdd, 0x7f,
  214. 0x05, 0x00,
  215. 0x12, 0x40,//0x40
  216. 0xd3, 0x7f,
  217. 0xc0, 0x16,
  218. 0xC1, 0x12,
  219. 0x8c, 0x00,
  220. 0x86, 0x3d,
  221. 0x50, 0x00,
  222. 0x51, 0x2C,
  223. 0x52, 0x24,
  224. 0x53, 0x00,
  225. 0x54, 0x00,
  226. 0x55, 0x00,
  227. 0x5A, 0x2c,
  228. 0x5b, 0x24,
  229. 0x5c, 0x00,
  230. };
  231. const unsigned char OV2640_YUV422[][2]=
  232. {
  233. 0xFF, 0x00,
  234. 0x05, 0x00,
  235. 0xDA, 0x10,
  236. 0xD7, 0x03,
  237. 0xDF, 0x00,
  238. 0x33, 0x80,
  239. 0x3C, 0x40,
  240. 0xe1, 0x77,
  241. 0x00, 0x00,
  242. };
  243. const unsigned char OV2640_JPEG[][2]=
  244. {
  245. 0xe0, 0x14,
  246. 0xe1, 0x77,
  247. 0xe5, 0x1f,
  248. 0xd7, 0x03,
  249. 0xda, 0x10,
  250. 0xe0, 0x00,
  251. 0xFF, 0x01,
  252. 0x04, 0x08,
  253. };
  254. /* JPG 320x240 */
  255. const unsigned char OV2640_800x600_JPEG[][2]=
  256. {
  257. {0xff, 0x01},
  258. {0x11, 0x01},
  259. {0x12, 0x00}, // Bit[6:4]: Resolution selection//0x02为彩条
  260. {0x17, 0x11}, // HREFST[10:3]
  261. {0x18, 0x75}, // HREFEND[10:3]
  262. {0x32, 0x36}, // Bit[5:3]: HREFEND[2:0]; Bit[2:0]: HREFST[2:0]
  263. {0x19, 0x01}, // VSTRT[9:2]
  264. {0x1a, 0x97}, // VEND[9:2]
  265. {0x03, 0x0f}, // Bit[3:2]: VEND[1:0]; Bit[1:0]: VSTRT[1:0]
  266. {0x37, 0x40},
  267. {0x4f, 0xbb},
  268. {0x50, 0x9c},
  269. {0x5a, 0x57},
  270. {0x6d, 0x80},
  271. {0x3d, 0x34},
  272. {0x39, 0x02},
  273. {0x35, 0x88},
  274. {0x22, 0x0a},
  275. {0x37, 0x40},
  276. {0x34, 0xa0},
  277. {0x06, 0x02},
  278. {0x0d, 0xb7},
  279. {0x0e, 0x01},
  280. ////////////////
  281. /*
  282. //176*144
  283. 0xff, 0x00,
  284. 0xc0, 0xC8,
  285. 0xc1, 0x96,
  286. 0x8c, 0x00,
  287. 0x86, 0x3D,
  288. 0x50, 0x9B,
  289. 0x51, 0x90,
  290. 0x52, 0x2C,
  291. 0x53, 0x00,
  292. 0x54, 0x00,
  293. 0x55, 0x88,
  294. 0x5a, 0x2C,
  295. 0x5b, 0x24,
  296. 0x5c, 0x00,
  297. 0xd3, 0x7F,
  298. ////////////
  299. */
  300. /*
  301. ////////////////
  302. //320*240
  303. 0xff, 0x00,
  304. 0xe0, 0x04,
  305. 0xc0, 0xc8,
  306. 0xc1, 0x96,
  307. 0x86, 0x3d,
  308. 0x50, 0x92,
  309. 0x51, 0x90,
  310. 0x52, 0x2c,
  311. 0x53, 0x00,
  312. 0x54, 0x00,
  313. 0x55, 0x88,
  314. 0x57, 0x00,
  315. 0x5a, 0x50,
  316. 0x5b, 0x3c,
  317. 0x5c, 0x00,
  318. 0xd3, 0x7F,
  319. 0xe0, 0x00,
  320. ///////////////////
  321. */
  322. /*
  323. 0xff, 0x00,
  324. 0xe0, 0x04,
  325. 0xc0, 0xc8,
  326. 0xc1, 0x96,
  327. 0x86, 0x35,
  328. 0x50, 0x92,
  329. 0x51, 0x90,
  330. 0x52, 0x2c,
  331. 0x53, 0x00,
  332. 0x54, 0x00,
  333. 0x55, 0x88,
  334. 0x57, 0x00,
  335. 0x5a, 0x58,
  336. 0x5b, 0x48,
  337. 0x5c, 0x00,
  338. 0xd3, 0x08,
  339. 0xe0, 0x00
  340. */
  341. /*
  342. //640*480
  343. 0xff, 0x00,
  344. 0xe0, 0x04,
  345. 0xc0, 0xc8,
  346. 0xc1, 0x96,
  347. 0x86, 0x3d,
  348. 0x50, 0x89,
  349. 0x51, 0x90,
  350. 0x52, 0x2c,
  351. 0x53, 0x00,
  352. 0x54, 0x00,
  353. 0x55, 0x88,
  354. 0x57, 0x00,
  355. 0x5a, 0xa0,
  356. 0x5b, 0x78,
  357. 0x5c, 0x00,
  358. 0xd3, 0x04,
  359. 0xe0, 0x00
  360. */
  361. /////////////////////
  362. //800*600
  363. 0xff, 0x00,
  364. 0xe0, 0x04,
  365. 0xc0, 0xc8,
  366. 0xc1, 0x96,
  367. 0x86, 0x35,
  368. 0x50, 0x89,
  369. 0x51, 0x90,
  370. 0x52, 0x2c,
  371. 0x53, 0x00,
  372. 0x54, 0x00,
  373. 0x55, 0x88,
  374. 0x57, 0x00,
  375. 0x5a, 0xc8,
  376. 0x5b, 0x96,
  377. 0x5c, 0x00,
  378. 0xd3, 0x02,
  379. 0xe0, 0x00
  380. /*
  381. //1280*1024
  382. 0xff, 0x00,
  383. 0xe0, 0x04,
  384. 0xc0, 0xc8,
  385. 0xc1, 0x96,
  386. 0x86, 0x3d,
  387. 0x50, 0x00,
  388. 0x51, 0x90,
  389. 0x52, 0x2c,
  390. 0x53, 0x00,
  391. 0x54, 0x00,
  392. 0x55, 0x88,
  393. 0x57, 0x00,
  394. 0x5a, 0x40,
  395. 0x5b, 0xf0,
  396. 0x5c, 0x01,
  397. 0xd3, 0x02,
  398. 0xe0, 0x00
  399. */
  400. /*
  401. /////////////////////
  402. //1600*1200
  403. 0xff, 0x00,
  404. 0xe0, 0x04,
  405. 0xc0, 0xc8,
  406. 0xc1, 0x96,
  407. 0x86, 0x3d,
  408. 0x50, 0x00,
  409. 0x51, 0x90,
  410. 0x52, 0x2c,
  411. 0x53, 0x00,
  412. 0x54, 0x00,
  413. 0x55, 0x88,
  414. 0x57, 0x00,
  415. 0x5a, 0x90,
  416. 0x5b, 0x2C,
  417. 0x5c, 0x05,//bit2->1;bit[1:0]->1
  418. 0xd3, 0x02,
  419. 0xe0, 0x00
  420. /////////////////////
  421. */
  422. /*
  423. //1024*768
  424. 0xff, 0x00,
  425. 0xc0, 0xC8,
  426. 0xc1, 0x96,
  427. 0x8c, 0x00,
  428. 0x86, 0x3D,
  429. 0x50, 0x00,
  430. 0x51, 0x90,
  431. 0x52, 0x2C,
  432. 0x53, 0x00,
  433. 0x54, 0x00,
  434. 0x55, 0x88,
  435. 0x5a, 0x00,
  436. 0x5b, 0xC0,
  437. 0x5c, 0x01,
  438. 0xd3, 0x02
  439. */
  440. };
  441. /* Private function prototypes -----------------------------------------------*/
  442. /* Private functions ---------------------------------------------------------*/
  443. //启动毫秒计时应用定时器。启动后,即开始产生毫秒计时,也就是提供了一个以毫秒为单位的计时基准
  444. static void ov2640_timers_stop(void)
  445. {
  446. if(ov2640_timer_stat==true)
  447. {
  448. ret_code_t err_code = app_timer_stop(ov2640_timer_id);
  449. APP_ERROR_CHECK(err_code);
  450. ov2640_timer_stat=false;
  451. }
  452. }
  453. //启动毫秒计时应用定时器。启动后,即开始产生毫秒计时,也就是提供了一个以毫秒为单位的计时基准
  454. static void ov2640_timers_start(uint32_t ms)
  455. {
  456. ret_code_t err_code = app_timer_start(ov2640_timer_id, APP_TIMER_TICKS(ms), NULL);
  457. APP_ERROR_CHECK(err_code);
  458. ov2640_timer_stat=true;
  459. }
  460. static void ov2640_timers_work(uint32_t ms)
  461. {
  462. ov2640_timers_stop();
  463. if(ov2640_timer_stat==false)
  464. {
  465. if(ms>APP_TIMER_SAFE_WINDOW_MS)
  466. {
  467. ov2640_timers_start(APP_TIMER_SAFE_WINDOW_MS);
  468. ov2640_timer_end=false;
  469. ov2640_timer_tick=ms-APP_TIMER_SAFE_WINDOW_MS;
  470. }
  471. else{
  472. ov2640_timers_start(ms);
  473. ov2640_timer_end=true;
  474. ov2640_timer_tick=0;
  475. }
  476. }
  477. }
  478. //ov2640定时器回调
  479. static void ov2640_timers_handler(void * p_context)
  480. {
  481. UNUSED_PARAMETER(p_context);
  482. ov2640_timer_stat=false;
  483. if(ov2640_timer_end==true)
  484. {
  485. Set_Ov2640_Queue(OV2640_QUEUE_TIME);
  486. }
  487. else{
  488. ov2640_timers_work(ov2640_timer_tick);
  489. }
  490. }
  491. /**@brief Function for the Timer initialization.
  492. *
  493. * @details Initializes the timer module.
  494. */
  495. void ov2640_timers_init(void)
  496. {
  497. // Initialize timer module, making it use the scheduler
  498. // ret_code_t err_code = app_timer_init();
  499. // APP_ERROR_CHECK(err_code);
  500. //创建一个应用定时器,产生秒计时。模式:周期性应用定时器,注册应用定时器事件句柄
  501. ret_code_t err_code = app_timer_create(&ov2640_timer_id,
  502. APP_TIMER_MODE_SINGLE_SHOT,
  503. ov2640_timers_handler);
  504. APP_ERROR_CHECK(err_code);
  505. }
  506. /**
  507. * @brief Initializes the hardware resources (I2C and GPIO) used to configure
  508. * the OV2640 camera.
  509. * @param None
  510. * @retval None
  511. */
  512. void OV2640_Gpio_Init(void)
  513. {
  514. xclk_timer_conf();
  515. nrf_gpio_cfg(
  516. CAM_PW_PIN,
  517. NRF_GPIO_PIN_DIR_OUTPUT,
  518. NRF_GPIO_PIN_INPUT_DISCONNECT,
  519. NRF_GPIO_PIN_NOPULL,
  520. NRF_GPIO_PIN_S0S1,
  521. NRF_GPIO_PIN_NOSENSE);
  522. CAM_PW_OFF;
  523. nrf_gpio_cfg_input(CAM_D0_PIN,NRF_GPIO_PIN_NOPULL);
  524. nrf_gpio_cfg_input(CAM_D1_PIN,NRF_GPIO_PIN_NOPULL);
  525. nrf_gpio_cfg_input(CAM_D2_PIN,NRF_GPIO_PIN_NOPULL);
  526. nrf_gpio_cfg_input(CAM_D3_PIN,NRF_GPIO_PIN_NOPULL);
  527. nrf_gpio_cfg_input(CAM_D4_PIN,NRF_GPIO_PIN_NOPULL);
  528. nrf_gpio_cfg_input(CAM_D5_PIN,NRF_GPIO_PIN_NOPULL);
  529. nrf_gpio_cfg_input(CAM_D6_PIN,NRF_GPIO_PIN_NOPULL);
  530. nrf_gpio_cfg_input(CAM_D7_PIN,NRF_GPIO_PIN_NOPULL);
  531. }
  532. void OV2640_CaptureGpioInit(nrf_drv_gpiote_in_config_t *config,nrfx_gpiote_evt_handler_t evt_handler)
  533. {
  534. ret_code_t err_code;
  535. //配置引脚为GPIOTE输入
  536. config->pull=NRF_GPIO_PIN_PULLUP;
  537. err_code = nrf_drv_gpiote_in_init(CAM_PCLK_PIN, config, evt_handler);
  538. APP_ERROR_CHECK(err_code);
  539. err_code = nrf_drv_gpiote_in_init(CAM_VSYNC_PIN, config, evt_handler);
  540. APP_ERROR_CHECK(err_code);
  541. err_code = nrf_drv_gpiote_in_init(CAM_HREF_PIN, config, evt_handler);
  542. APP_ERROR_CHECK(err_code);
  543. //使能事件模式
  544. // nrf_drv_gpiote_in_event_enable(CAM_PCLK_PIN,true);
  545. // nrf_drv_gpiote_in_event_enable(CAM_VSYNC_PIN,true);
  546. // nrf_drv_gpiote_in_event_enable(CAM_HREF_PIN,true);
  547. }
  548. static void ov2640_i2s_done(void)
  549. {
  550. m_xfer_done = true;
  551. }
  552. static void ov2640_i2s_timeout(void)
  553. {
  554. m_xfer_timeout = true;
  555. }
  556. void OV2640_i2s_init(void)
  557. {
  558. twim0_init(ov2640_i2s_done, ov2640_i2s_timeout);
  559. }
  560. void OV2640_i2s_uninit(void)
  561. {
  562. twim0_uinit();
  563. }
  564. /**
  565. * @brief Resets the OV2640 camera.
  566. * @param None
  567. * @retval None
  568. */
  569. static void OV2640_Reset(void)
  570. {
  571. OV2640_WriteReg(OV2640_DSP_RA_DLMT, 0x01);
  572. OV2640_WriteReg(OV2640_SENSOR_COM7, 0x80);
  573. }
  574. /**
  575. * @brief Reads the OV2640 Manufacturer identifier.
  576. * @param OV2640ID: Pointer to the OV2640 Manufacturer identifier
  577. * @retval None
  578. */
  579. static void OV2640_ReadID(OV2640_IDTypeDef *OV2640ID)
  580. {
  581. OV2640_WriteReg(OV2640_DSP_RA_DLMT, 0x01);
  582. OV2640ID->Manufacturer_ID1 = OV2640_ReadReg(OV2640_SENSOR_MIDH);
  583. OV2640ID->Manufacturer_ID2 = OV2640_ReadReg(OV2640_SENSOR_MIDL);
  584. OV2640ID->PIDH = OV2640_ReadReg(OV2640_SENSOR_PIDH);
  585. OV2640ID->PIDL = OV2640_ReadReg(OV2640_SENSOR_PIDL);
  586. }
  587. static void ov2640_cmd_jpeg_init(void)
  588. {
  589. /* Initialize OV2640 */
  590. for(int i=0; i<(sizeof(OV2640_JPEG_INIT)/2); i++)
  591. {
  592. OV2640_WriteReg(OV2640_JPEG_INIT[i][0], OV2640_JPEG_INIT[i][1]);
  593. nrf_delay_us(100);
  594. }
  595. }
  596. static void ov2640_cmd_yuv422(void)
  597. {
  598. /* Set to output YUV422 */
  599. for(int i=0; i<(sizeof(OV2640_YUV422)/2); i++)
  600. {
  601. OV2640_WriteReg(OV2640_YUV422[i][0], OV2640_YUV422[i][1]);
  602. nrf_delay_us(100);
  603. }
  604. }
  605. static void ov2640_cmd_jpeg(void)
  606. {
  607. OV2640_WriteReg(0xff, 0x01);
  608. OV2640_WriteReg(0x15, 0x00);
  609. /* Set to output JPEG */
  610. for(int i=0; i<(sizeof(OV2640_JPEG)/2); i++)
  611. {
  612. OV2640_WriteReg(OV2640_JPEG[i][0], OV2640_JPEG[i][1]);
  613. nrf_delay_us(100);
  614. }
  615. }
  616. /**
  617. * @brief Configures the OV2640 camera in JPEG mode.
  618. * @param JPEGImageSize: JPEG image size
  619. * @retval None
  620. */
  621. void OV2640_JPEGConfig(void)
  622. {
  623. for(int i=0; i<(sizeof(OV2640_800x600_JPEG)/2); i++)
  624. {
  625. OV2640_WriteReg(OV2640_800x600_JPEG[i][0], OV2640_800x600_JPEG[i][1]);
  626. nrf_delay_us(100);
  627. }
  628. }
  629. /**
  630. * @brief Configures the OV2640 camera brightness.
  631. * @param Brightness: Brightness value, where Brightness can be:
  632. * 0x40 for Brightness +2,
  633. * 0x30 for Brightness +1,
  634. * 0x20 for Brightness 0,
  635. * 0x10 for Brightness -1,
  636. * 0x00 for Brightness -2,
  637. * @retval None
  638. */
  639. void OV2640_BrightnessConfig(uint8_t Brightness)
  640. {
  641. OV2640_WriteReg(0xff, 0x00);
  642. OV2640_WriteReg(0x7c, 0x00);
  643. OV2640_WriteReg(0x7d, 0x04);
  644. OV2640_WriteReg(0x7c, 0x09);
  645. OV2640_WriteReg(0x7d, Brightness);
  646. OV2640_WriteReg(0x7d, 0x00);
  647. }
  648. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  649. const static uint8_t OV2640_AUTOEXPOSURE_LEVEL0[]=
  650. {
  651. 0xFF, 0x01, 0xff,
  652. 0x24, 0x20, 0xff,
  653. 0x25, 0x18, 0xff,
  654. 0x26, 0x60, 0xff,
  655. 0x00, 0x00, 0x00
  656. };
  657. const static uint8_t OV2640_AUTOEXPOSURE_LEVEL1[]=
  658. {
  659. 0xFF, 0x01, 0xff,
  660. 0x24, 0x34, 0xff,
  661. 0x25, 0x1c, 0xff,
  662. 0x26, 0x70, 0xff,
  663. 0x00, 0x00, 0x00
  664. };
  665. const static uint8_t OV2640_AUTOEXPOSURE_LEVEL2[]=
  666. {
  667. 0xFF, 0x01, 0xff,
  668. 0x24, 0x3e, 0xff,
  669. 0x25, 0x38, 0xff,
  670. 0x26, 0x81, 0xff,
  671. 0x00, 0x00, 0x00
  672. };
  673. const static uint8_t OV2640_AUTOEXPOSURE_LEVEL3[]=
  674. {
  675. 0xFF, 0x01, 0xff,
  676. 0x24, 0x48, 0xff,
  677. 0x25, 0x40, 0xff,
  678. 0x26, 0x81, 0xff,
  679. 0x00, 0x00, 0x00
  680. };
  681. const static uint8_t OV2640_AUTOEXPOSURE_LEVEL4[]=
  682. {
  683. 0xFF, 0x01, 0xff,
  684. 0x24, 0x58, 0xff,
  685. 0x25, 0x50, 0xff,
  686. 0x26, 0x92, 0xff,
  687. 0x00, 0x00, 0x00
  688. };
  689. void SCCB_WriteRegs(const uint8_t* pbuf)
  690. {
  691. while(1)
  692. {
  693. if((*pbuf == 0) && (*(pbuf + 1) == 0))
  694. {
  695. break;
  696. }
  697. else
  698. {
  699. OV2640_WriteReg(*pbuf, *(pbuf+1));
  700. pbuf += 2;
  701. }
  702. }
  703. }
  704. void OV2640_AutoExposure(uint8_t level)
  705. {
  706. switch(level)
  707. {
  708. case 0:
  709. SCCB_WriteRegs(OV2640_AUTOEXPOSURE_LEVEL0);
  710. break;
  711. case 1:
  712. SCCB_WriteRegs(OV2640_AUTOEXPOSURE_LEVEL1);
  713. break;
  714. case 2:
  715. SCCB_WriteRegs(OV2640_AUTOEXPOSURE_LEVEL2);
  716. break;
  717. case 3:
  718. SCCB_WriteRegs(OV2640_AUTOEXPOSURE_LEVEL3);
  719. break;
  720. case 4:
  721. SCCB_WriteRegs(OV2640_AUTOEXPOSURE_LEVEL4);
  722. break;
  723. default:
  724. SCCB_WriteRegs(OV2640_AUTOEXPOSURE_LEVEL0);
  725. break;
  726. }
  727. }
  728. /**
  729. * @brief Configures the OV2640 camera Black and white mode.
  730. * @param BlackWhite: BlackWhite value, where BlackWhite can be:
  731. * 0x18 for B&W,
  732. * 0x40 for Negative,
  733. * 0x58 for B&W negative,
  734. * 0x00 for Normal,
  735. * @retval None
  736. */
  737. void OV2640_BandWConfig(uint8_t BlackWhite)
  738. {
  739. OV2640_WriteReg(0xff, 0x00);
  740. OV2640_WriteReg(0x7c, 0x00);
  741. OV2640_WriteReg(0x7d, BlackWhite);
  742. OV2640_WriteReg(0x7c, 0x05);
  743. OV2640_WriteReg(0x7d, 0x80);
  744. OV2640_WriteReg(0x7d, 0x80);
  745. }
  746. /**
  747. * @brief Configures the OV2640 camera color effects.
  748. * @param value1: Color effects value1
  749. * @param value2: Color effects value2
  750. * where value1 and value2 can be:
  751. * value1 = 0x40, value2 = 0xa6 for Antique,
  752. * value1 = 0xa0, value2 = 0x40 for Bluish,
  753. * value1 = 0x40, value2 = 0x40 for Greenish,
  754. * value1 = 0x40, value2 = 0xc0 for Reddish,
  755. * @retval None
  756. */
  757. void OV2640_ColorEffectsConfig(uint8_t value1, uint8_t value2)
  758. {
  759. OV2640_WriteReg(0xff, 0x00);
  760. OV2640_WriteReg(0x7c, 0x00);
  761. OV2640_WriteReg(0x7d, 0x18);
  762. OV2640_WriteReg(0x7c, 0x05);
  763. OV2640_WriteReg(0x7d, value1);
  764. OV2640_WriteReg(0x7d, value2);
  765. }
  766. /**
  767. * @brief Configures the OV2640 camera contrast.
  768. * @param value1: Contrast value1
  769. * @param value2: Contrast value2
  770. * where value1 and value2 can be:
  771. * value1 = 0x28, value2 = 0x0c for Contrast +2,
  772. * value1 = 0x24, value2 = 0x16 for Contrast +1,
  773. * value1 = 0x20, value2 = 0x20 for Contrast 0,
  774. * value1 = 0x1c, value2 = 0x2a for Contrast -1,
  775. * value1 = 0x18, value2 = 0x34 for Contrast -2,
  776. * @retval None
  777. */
  778. void OV2640_ContrastConfig(uint8_t value1, uint8_t value2)
  779. {
  780. OV2640_WriteReg(0xff, 0x00);
  781. OV2640_WriteReg(0x7c, 0x00);
  782. OV2640_WriteReg(0x7d, 0x04);
  783. OV2640_WriteReg(0x7c, 0x07);
  784. OV2640_WriteReg(0x7d, 0x20);
  785. OV2640_WriteReg(0x7d, value1);
  786. OV2640_WriteReg(0x7d, value2);
  787. OV2640_WriteReg(0x7d, 0x06);
  788. }
  789. /**
  790. * @brief Writes a byte at a specific Camera register
  791. * @param Addr: OV2640 register address.
  792. * @param Data: Data to be written to the specific register
  793. * @retval 0x00 if write operation is OK.
  794. * 0xFF if timeout condition occured (device not connected or bus error).
  795. */
  796. static ret_code_t OV2640_WriteReg(uint16_t Addr, uint8_t Data)
  797. {
  798. uint8_t sdata[3] = {0};
  799. memcpy(sdata, &Addr, 2);
  800. sdata[2] = Data;
  801. m_xfer_done = false;
  802. nrf_drv_twim0_tx(OV2640_DEVICE_WRITE_ADDRESS, sdata, 3, false);
  803. while((m_xfer_done == false) && (m_xfer_timeout == false))
  804. {
  805. }
  806. if(m_xfer_done == false)
  807. {
  808. return NRF_ERROR_TIMEOUT;
  809. }
  810. /* If operation is OK, return 0 */
  811. return NRF_SUCCESS;
  812. }
  813. /**
  814. * @brief Reads a byte from a specific Camera register
  815. * @param Addr: OV2640 register address.
  816. * @retval data read from the specific register or 0xFF if timeout condition
  817. * occured.
  818. */
  819. static uint8_t OV2640_ReadReg(uint16_t Addr)
  820. {
  821. uint8_t recv_data = 0;
  822. uint8_t sdata[2] = {0};
  823. memcpy(sdata, &Addr, 2);
  824. m_xfer_done = false;
  825. nrf_drv_twim0_tx(OV2640_DEVICE_WRITE_ADDRESS, sdata, 2, true);
  826. while((m_xfer_done == false) && (m_xfer_timeout == false))
  827. {
  828. }
  829. if(m_xfer_done == false)
  830. {
  831. return NRF_ERROR_TIMEOUT;
  832. }
  833. nrf_drv_twim0_rx(OV2640_DEVICE_WRITE_ADDRESS, &recv_data, 1);
  834. while((m_xfer_done == false) && (m_xfer_timeout == false))
  835. {
  836. }
  837. if(m_xfer_done == false)
  838. {
  839. return NRF_ERROR_TIMEOUT;
  840. }
  841. /* return the read data */
  842. return recv_data;
  843. }
  844. static uint8_t get_ov2640_data(void)
  845. {
  846. uint8_t data = 0;
  847. uint8_t temp = nrf_gpio_pin_read(CAM_D0_PIN);
  848. data <<= 1;
  849. data |= temp;
  850. temp = nrf_gpio_pin_read(CAM_D1_PIN);
  851. data <<= 1;
  852. data |= temp;
  853. temp = nrf_gpio_pin_read(CAM_D2_PIN);
  854. data <<= 1;
  855. data |= temp;
  856. temp = nrf_gpio_pin_read(CAM_D3_PIN);
  857. data <<= 1;
  858. data |= temp;
  859. temp = nrf_gpio_pin_read(CAM_D4_PIN);
  860. data <<= 1;
  861. data |= temp;
  862. temp = nrf_gpio_pin_read(CAM_D5_PIN);
  863. data <<= 1;
  864. data |= temp;
  865. temp = nrf_gpio_pin_read(CAM_D6_PIN);
  866. data <<= 1;
  867. data |= temp;
  868. temp = nrf_gpio_pin_read(CAM_D7_PIN);
  869. data <<= 1;
  870. data |= temp;
  871. return data;
  872. }
  873. //void ov2640_init(void)
  874. //{
  875. // OV2640_ReadID(&OV2640_Camera_ID); //读取OV2640ID,测试硬件,依次为:0x7F,0xA2,0x26,0x42
  876. // OV2640_JPEGConfig(JPEG_320x240); //配置OV2640输出320*240像素的JPG图片
  877. //
  878. // OV2640_BrightnessConfig(0x20);
  879. // OV2640_AutoExposure(2);
  880. //}
  881. static uint8_t vsync_count = 0;
  882. static bool vsync_stat = false;
  883. static uint8_t jpg_array[512] = {0};
  884. static int jpg_count = 0;
  885. static uint16_t head_value = 0;
  886. static bool jpg_stat = false;
  887. static void ov2640_pro(void)
  888. {
  889. switch(ov2640_work)
  890. {
  891. case OV2640_WORK_I2C_INIT:{
  892. CAM_PW_ON;
  893. camera_enable_out_clock();
  894. OV2640_i2s_init();
  895. ov2640_work = OV2640_WORK_RST;
  896. ov2640_timers_work(10);
  897. }break;
  898. case OV2640_WORK_RST:{
  899. OV2640_Reset();
  900. ov2640_work = OV2640_WORK_JPEG_INIT;
  901. ov2640_timers_work(100);
  902. }break;
  903. case OV2640_WORK_JPEG_INIT:{
  904. ov2640_cmd_jpeg_init();
  905. ov2640_work = OV2640_WORK_YUV422;
  906. ov2640_timers_work(1);
  907. }break;
  908. case OV2640_WORK_YUV422:{
  909. ov2640_cmd_yuv422();
  910. ov2640_work = OV2640_WORK_JPEG;
  911. ov2640_timers_work(1);
  912. }break;
  913. case OV2640_WORK_JPEG:{
  914. ov2640_cmd_jpeg();
  915. ov2640_work = OV2640_WORK_JPEGCONFIG;
  916. ov2640_timers_work(1);
  917. }break;
  918. case OV2640_WORK_JPEGCONFIG:{
  919. OV2640_JPEGConfig();
  920. ov2640_work = OV2640_WORK_BNCONFIG;
  921. ov2640_timers_work(1);
  922. }break;
  923. case OV2640_WORK_BNCONFIG:{
  924. OV2640_BrightnessConfig(0x20);
  925. ov2640_work = OV2640_WORK_AUTOEXP;
  926. ov2640_timers_work(1);
  927. }break;
  928. case OV2640_WORK_AUTOEXP:{
  929. OV2640_AutoExposure(2);
  930. ov2640_work = OV2640_WORK_START;
  931. ov2640_timers_work(10);
  932. }break;
  933. case OV2640_WORK_START:{
  934. ov2640_work = OV2640_WORK_DELAY;
  935. vsync_count = 0;
  936. nrf_drv_gpiote_in_event_enable(CAM_VSYNC_PIN,true);
  937. }break;
  938. case OV2640_WORK_VSYNC:{
  939. if(jpg_count > 256 || jpg_count == 0)
  940. {
  941. camera_jpg_pro(&jpg_array[256], (jpg_count == 0)?256:jpg_count-256, true);
  942. }
  943. else{
  944. camera_jpg_pro(jpg_array, jpg_count, false);
  945. }
  946. ov2640_work = OV2640_WORK_DELAY;
  947. ov2640_timers_work(60000);
  948. }break;
  949. case OV2640_WORK_PCLK:{
  950. if(jpg_count >= 256)
  951. {
  952. camera_jpg_pro(jpg_array, 256, false);
  953. }
  954. else{
  955. camera_jpg_pro(&jpg_array[256], 256, false);
  956. }
  957. ov2640_work = OV2640_WORK_DELAY;
  958. }break;
  959. case OV2640_WORK_DELAY:{
  960. ov2640_work = OV2640_WORK_I2C_UNINIT;
  961. ov2640_timers_work(100);
  962. }break;
  963. case OV2640_WORK_I2C_UNINIT:{
  964. camera_disable_out_clock();
  965. OV2640_i2s_uninit();
  966. ov2640_work = OV2640_WORK_I2C_INIT;
  967. CAM_PW_OFF;
  968. }break;
  969. }
  970. }
  971. static bool href_pin_stat = false;
  972. void ov2640_href_pro(void)
  973. {
  974. uint32_t flag = nrf_gpio_pin_read(CAM_HREF_PIN);
  975. if(flag != 0)
  976. {
  977. href_pin_stat = true;
  978. }
  979. else{
  980. href_pin_stat = false;
  981. }
  982. }
  983. void ov2640_vsync_pro(void)
  984. {
  985. uint32_t flag = nrf_gpio_pin_read(CAM_VSYNC_PIN);
  986. if(flag != 0)
  987. {
  988. vsync_stat = true;
  989. if(vsync_count == 0)
  990. {
  991. vsync_count = 1;
  992. }
  993. }
  994. else{
  995. if(vsync_stat == true)
  996. {
  997. if(vsync_count == 1)
  998. {
  999. nrf_drv_gpiote_in_event_enable(CAM_PCLK_PIN,true);
  1000. nrf_drv_gpiote_in_event_enable(CAM_HREF_PIN,true);
  1001. vsync_count = 2;
  1002. }
  1003. else if(vsync_count == 2 && head_value == 0xFFD9)
  1004. {
  1005. nrf_drv_gpiote_in_event_disable(CAM_PCLK_PIN);
  1006. nrf_drv_gpiote_in_event_disable(CAM_VSYNC_PIN);
  1007. nrf_drv_gpiote_in_event_disable(CAM_HREF_PIN);
  1008. ov2640_work = OV2640_WORK_VSYNC;
  1009. Set_Ov2640_Queue(OV2640_QUEUE_INT);
  1010. }
  1011. }
  1012. vsync_stat = false;
  1013. }
  1014. }
  1015. static uint8_t ov2640_dat_value(void)
  1016. {
  1017. uint8_t data = 0x00;
  1018. for(int i = 0; i < 8; i++)
  1019. {
  1020. data <<= 1;
  1021. uint32_t flag = nrf_gpio_pin_read(ov2640_data_Array[i]);
  1022. if(flag != 0)
  1023. {
  1024. data |= 0x01;
  1025. }
  1026. }
  1027. return data;
  1028. }
  1029. void ov2640_pclk_pro(void)
  1030. {
  1031. uint32_t flag = nrf_gpio_pin_read(CAM_PCLK_PIN);
  1032. if(flag != 0)
  1033. {
  1034. if(href_pin_stat == true)
  1035. {
  1036. if(jpg_stat == true)
  1037. {
  1038. uint8_t dat = ov2640_dat_value();
  1039. head_value <<= 8;
  1040. head_value |= dat;
  1041. jpg_array[jpg_count++] = dat;
  1042. if(jpg_count == 256)
  1043. {
  1044. ov2640_work = OV2640_WORK_PCLK;
  1045. Set_Ov2640_Queue(OV2640_QUEUE_INT);
  1046. }
  1047. else if(jpg_count == 512)
  1048. {
  1049. ov2640_work = OV2640_WORK_PCLK;
  1050. Set_Ov2640_Queue(OV2640_QUEUE_INT);
  1051. jpg_count = 0;
  1052. }
  1053. }
  1054. else{
  1055. uint8_t dat = ov2640_dat_value();
  1056. head_value <<= 8;
  1057. head_value |= dat;
  1058. if(head_value == 0xFFD8)
  1059. {
  1060. jpg_stat = true;
  1061. }
  1062. }
  1063. }
  1064. }
  1065. }
  1066. static void ov2640_queue(void *p_event_data, uint16_t event_size)
  1067. {
  1068. custome_event *evt = (custome_event*)p_event_data;
  1069. if((ov2640_event.flag & (1 << OV2640_QUEUE_INT)) != 0)
  1070. {
  1071. ov2640_pro();
  1072. ov2640_event.flag &= (~(1 << OV2640_QUEUE_INT));
  1073. }
  1074. if((ov2640_event.flag & (1 << OV2640_QUEUE_TIME)) != 0)
  1075. {
  1076. ov2640_pro();
  1077. ov2640_event.flag &= (~(1 << OV2640_QUEUE_TIME));
  1078. }
  1079. }
  1080. static void Set_Ov2640_Queue(OV2640_QUEUE_TYPE type)
  1081. {
  1082. ov2640_event.flag |= (1<<type);
  1083. ret_code_t err_code = app_sched_event_put(&ov2640_event, sizeof(ov2640_event), ov2640_queue);
  1084. }
  1085. /**
  1086. * @}
  1087. */
  1088. /**
  1089. * @}
  1090. */
  1091. /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/