hpm_ov5640.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  1. /*
  2. * Copyright (c) 2021 HPMicro
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. */
  7. #include "hpm_ov5640.h"
  8. static const ov5640_reg_val_t ov5640_init_param[] = {
  9. /* system setting */
  10. {0x3008, 0x42}, /* software power down, bit[6] */
  11. {0x3103, 0x03}, /* system clock from PLL, bit[1] */
  12. {0x3037, 0x13}, /* PLL root divider, bit[4], PLL pre-divider, bit[3:0] */
  13. {0x3108, 0x01}, /* PCLK root divider, bit[5:4], SCLK2x root divider, bit[3:2], SCLK root divider, bit[1:0] */
  14. {0x3630, 0x36},
  15. {0x3631, 0x0e},
  16. {0x3632, 0xe2},
  17. {0x3633, 0x12},
  18. {0x3621, 0xe0},
  19. {0x3704, 0xa0},
  20. {0x3703, 0x5a},
  21. {0x302c, 0xc2}, /* pad control */
  22. {0x4004, 0x02}, /* BLC 2 lines */
  23. {0x3002, 0x1c}, /* reset JFIFO, SFIFO, JPEG */
  24. {0x3006, 0xc3}, /* disable clock of JPEG2x, JPEG */
  25. {0x4713, 0x03}, /* JPEG mode 3 */
  26. {0x4407, 0x04}, /* Quantization scale */
  27. {0x460b, 0x35},
  28. {0x5001, 0xa3}, /* SDE on, scale on, UV average off, color matrix on, AWB on */
  29. {0x3503, 0x00}, /* AEC/AGC on */
  30. {0x3c07, 0x08}, /* light meter 1 threshold [7:0] */
  31. {0x3820, 0x41}, /* Sensor flip off, ISP flip on */
  32. {0x3821, 0x07}, /* Sensor mirror on, ISP mirror on, H binning on */
  33. {0x3618, 0x00},
  34. {0x3612, 0x29},
  35. {0x3709, 0x52},
  36. {0x370c, 0x03},
  37. {0x3715, 0x78},
  38. {0x3717, 0x01},
  39. {0x370b, 0x60},
  40. {0x3705, 0x1a},
  41. {0x3905, 0x02},
  42. {0x3906, 0x10},
  43. {0x3901, 0x0a},
  44. {0x3731, 0x12},
  45. {0x3600, 0x08}, /* VCM control */
  46. {0x3601, 0x33}, /* VCM control */
  47. {0x302d, 0x60}, /* system control */
  48. {0x3620, 0x52},
  49. {0x371b, 0x20},
  50. {0x471c, 0x50},
  51. {0x3a13, 0x43}, /* pre-gain = 1.047x */
  52. {0x3a18, 0x00}, /* gain ceiling */
  53. {0x3a19, 0xf8}, /* gain ceiling = 15.5x */
  54. {0x3635, 0x13},
  55. {0x3636, 0x03},
  56. {0x3634, 0x40},
  57. {0x3622, 0x01},
  58. /* 50/60Hz detection */
  59. {0x3a02, 0x0b}, /* 60Hz max exposure, night mode 5fps */
  60. {0x3a03, 0x88}, /* 60Hz max exposure */
  61. {0x3a14, 0x0b}, /* 50Hz max exposure, night mode 5fps */
  62. {0x3a15, 0x88}, /* 50Hz max exposure */
  63. {0x3c01, 0x34}, /* Band auto, bit[7] */
  64. {0x3c04, 0x28}, /* threshold low sum */
  65. {0x3c05, 0x98}, /* threshold high sum */
  66. {0x3c06, 0x00}, /* light meter 1 threshold[15:8] */
  67. {0x3c07, 0x08}, /* light meter 1 threshold[7:0] */
  68. {0x3c08, 0x00}, /* light meter 2 threshold[15:8] */
  69. {0x3c09, 0x1c}, /* light meter 2 threshold[7:0] */
  70. {0x3c0a, 0x9c}, /* sample number[15:8] */
  71. {0x3c0b, 0x40}, /* sample number[7:0] */
  72. {0x3708, 0x64},
  73. {0x4001, 0x02}, /* BLC start from line 2 */
  74. {0x4005, 0x1a}, /* BLC always update */
  75. {0x3000, 0x00}, /* enable blocks */
  76. {0x3004, 0xff}, /* enable clocks */
  77. {0x302e, 0x00},
  78. {0x440e, 0x00},
  79. {0x5000, 0xa7}, /* Lenc on, raw gamma on, BPC on, WPC on, CIP on */
  80. /* AEC target */
  81. {0x3a0f, 0x30}, /* stable range in high */
  82. {0x3a10, 0x28}, /* stable range in low */
  83. {0x3a1b, 0x30}, /* stable range out high */
  84. {0x3a1e, 0x26}, /* stable range out low */
  85. {0x3a11, 0x60}, /* fast zone high */
  86. {0x3a1f, 0x14}, /* fast zone low */
  87. /* Lens correction */
  88. {0x5800, 0x23},
  89. {0x5801, 0x14},
  90. {0x5802, 0x0f},
  91. {0x5803, 0x0f},
  92. {0x5804, 0x12},
  93. {0x5805, 0x26},
  94. {0x5806, 0x0c},
  95. {0x5807, 0x08},
  96. {0x5808, 0x05},
  97. {0x5809, 0x05},
  98. {0x580a, 0x08},
  99. {0x580b, 0x0d},
  100. {0x580c, 0x08},
  101. {0x580d, 0x03},
  102. {0x580e, 0x00},
  103. {0x580f, 0x00},
  104. {0x5810, 0x03},
  105. {0x5811, 0x09},
  106. {0x5812, 0x07},
  107. {0x5813, 0x03},
  108. {0x5814, 0x00},
  109. {0x5815, 0x01},
  110. {0x5816, 0x03},
  111. {0x5817, 0x08},
  112. {0x5818, 0x0d},
  113. {0x5819, 0x08},
  114. {0x581a, 0x05},
  115. {0x581b, 0x06},
  116. {0x581c, 0x08},
  117. {0x581d, 0x0e},
  118. {0x581e, 0x29},
  119. {0x581f, 0x17},
  120. {0x5820, 0x11},
  121. {0x5821, 0x11},
  122. {0x5822, 0x15},
  123. {0x5823, 0x28},
  124. {0x5824, 0x46},
  125. {0x5825, 0x26},
  126. {0x5826, 0x08},
  127. {0x5827, 0x26},
  128. {0x5828, 0x64},
  129. {0x5829, 0x26},
  130. {0x582a, 0x24},
  131. {0x582b, 0x22},
  132. {0x582c, 0x24},
  133. {0x582d, 0x24},
  134. {0x582e, 0x06},
  135. {0x582f, 0x22},
  136. {0x5830, 0x40},
  137. {0x5831, 0x42},
  138. {0x5832, 0x24},
  139. {0x5833, 0x26},
  140. {0x5834, 0x24},
  141. {0x5835, 0x22},
  142. {0x5836, 0x22},
  143. {0x5837, 0x26},
  144. {0x5838, 0x44},
  145. {0x5839, 0x24},
  146. {0x583a, 0x26},
  147. {0x583b, 0x28},
  148. {0x583c, 0x42},
  149. {0x583d, 0xce}, /* lenc BR offset */
  150. /* AWB */
  151. {0x5180, 0xff}, /* AWB B block */
  152. {0x5181, 0xf2}, /* AWB control */
  153. {0x5182, 0x00}, /* [7:4] max local counter, [3:0] max fast counter */
  154. {0x5183, 0x14}, /* AWB advanced */
  155. {0x5184, 0x25},
  156. {0x5185, 0x24},
  157. {0x5186, 0x09},
  158. {0x5187, 0x09},
  159. {0x5188, 0x09},
  160. {0x5189, 0x75},
  161. {0x518a, 0x54},
  162. {0x518b, 0xe0},
  163. {0x518c, 0xb2},
  164. {0x518d, 0x42},
  165. {0x518e, 0x3d},
  166. {0x518f, 0x56},
  167. {0x5190, 0x46},
  168. {0x5191, 0xf8}, /* AWB top limit */
  169. {0x5192, 0x04}, /* AWB bottom limit */
  170. {0x5193, 0x70}, /* red limit */
  171. {0x5194, 0xf0}, /* green limit */
  172. {0x5195, 0xf0}, /* blue limit */
  173. {0x5196, 0x03}, /* AWB control */
  174. {0x5197, 0x01}, /* local limit */
  175. {0x5198, 0x04},
  176. {0x5199, 0x12},
  177. {0x519a, 0x04},
  178. {0x519b, 0x00},
  179. {0x519c, 0x06},
  180. {0x519d, 0x82},
  181. {0x519e, 0x38}, /* AWB control */
  182. /* Gamma */
  183. {0x5480, 0x01}, /* Gamma bias plus on, bit[0] */
  184. {0x5481, 0x08},
  185. {0x5482, 0x14},
  186. {0x5483, 0x28},
  187. {0x5484, 0x51},
  188. {0x5485, 0x65},
  189. {0x5486, 0x71},
  190. {0x5487, 0x7d},
  191. {0x5488, 0x87},
  192. {0x5489, 0x91},
  193. {0x548a, 0x9a},
  194. {0x548b, 0xaa},
  195. {0x548c, 0xb8},
  196. {0x548d, 0xcd},
  197. {0x548e, 0xdd},
  198. {0x548f, 0xea},
  199. {0x5490, 0x1d},
  200. /* color matrix */
  201. {0x5381, 0x1e}, /* CMX1 for Y */
  202. {0x5382, 0x5b}, /* CMX2 for Y */
  203. {0x5383, 0x08}, /* CMX3 for Y */
  204. {0x5384, 0x0a}, /* CMX4 for U */
  205. {0x5385, 0x7e}, /* CMX5 for U */
  206. {0x5386, 0x88}, /* CMX6 for U */
  207. {0x5387, 0x7c}, /* CMX7 for V */
  208. {0x5388, 0x6c}, /* CMX8 for V */
  209. {0x5389, 0x10}, /* CMX9 for V */
  210. {0x538a, 0x01}, /* sign[9] */
  211. {0x538b, 0x98}, /* sign[8:1] */
  212. /* UV adjust */
  213. {0x5580, 0x06}, /* saturation on, bit[1] */
  214. {0x5583, 0x40},
  215. {0x5584, 0x10},
  216. {0x5003, 0x08},
  217. {0x5589, 0x10},
  218. {0x558a, 0x00},
  219. {0x558b, 0xf8},
  220. {0x501d, 0x40}, /* enable manual offset of contrast */
  221. /* CIP */
  222. {0x5300, 0x08}, /* CIP sharpen MT threshold 1 */
  223. {0x5301, 0x30}, /* CIP sharpen MT threshold 2 */
  224. {0x5302, 0x10}, /* CIP sharpen MT offset 1 */
  225. {0x5303, 0x00}, /* CIP sharpen MT offset 2 */
  226. {0x5304, 0x08}, /* CIP DNS threshold 1 */
  227. {0x5305, 0x30}, /* CIP DNS threshold 2 */
  228. {0x5306, 0x08}, /* CIP DNS offset 1 */
  229. {0x5307, 0x16}, /* CIP DNS offset 2 */
  230. {0x5309, 0x08}, /* CIP sharpen TH threshold 1 */
  231. {0x530a, 0x30}, /* CIP sharpen TH threshold 2 */
  232. {0x530b, 0x04}, /* CIP sharpen TH offset 1 */
  233. {0x530c, 0x06}, /* CIP sharpen TH offset 2 */
  234. {0x5025, 0x00},
  235. {0x3008, 0x02}, /* wake up from standby, bit[6] */
  236. };
  237. /* Resolution configuration */
  238. static const ov5640_reg_val_t ov5640_resolution_800_480_param[] = {
  239. {0x3800, 0x00}, {0x3801, 0x00}, {0x3802, 0x00}, {0x3803, 0x04},
  240. {0x3804, 0x0a}, {0x3805, 0x3f}, {0x3806, 0x07}, {0x3807, 0x9b},
  241. {0x3808, 0x03}, {0x3809, 0x20}, {0x380A, 0x01}, {0x380B, 0xe0},
  242. {0x380C, 0x07}, {0x380D, 0x68}, {0x380E, 0x03}, {0x380F, 0xd8},
  243. {0x3810, 0x00}, {0x3811, 0x10}, {0x3812, 0x00}, {0x3813, 0x06}, {0x3814, 0x31}, {0x3815, 0x31},
  244. };
  245. static const ov5640_reg_val_t ov5640_resolution_vga_param[] = {
  246. {0x3800, 0x00}, {0x3801, 0x00}, {0x3802, 0x00}, {0x3803, 0x04},
  247. {0x3804, 0x0a}, {0x3805, 0x3f}, {0x3806, 0x07}, {0x3807, 0x9b},
  248. {0x3808, 0x02}, {0x3809, 0x80}, {0x380A, 0x01}, {0x380B, 0xe0},
  249. {0x380C, 0x07}, {0x380D, 0x68}, {0x380E, 0x03}, {0x380F, 0xd8},
  250. {0x3810, 0x00}, {0x3811, 0x10}, {0x3812, 0x00}, {0x3813, 0x06}, {0x3814, 0x31}, {0x3815, 0x31},
  251. };
  252. static const ov5640_reg_val_t ov5640_resolution_qvga_param[] = {
  253. {0x3800, 0x00}, {0x3801, 0x00}, {0x3802, 0x00}, {0x3803, 0x04},
  254. {0x3804, 0x0a}, {0x3805, 0x3f}, {0x3806, 0x07}, {0x3807, 0x9b},
  255. {0x3808, 0x01}, {0x3809, 0x40}, {0x380A, 0x00}, {0x380B, 0xf0},
  256. {0x380C, 0x07}, {0x380D, 0x68}, {0x380E, 0x03}, {0x380F, 0xd8},
  257. {0x3810, 0x00}, {0x3811, 0x10}, {0x3812, 0x00}, {0x3813, 0x06}, {0x3814, 0x31}, {0x3815, 0x31},
  258. };
  259. static const ov5640_reg_val_t ov5640_resolution_480_272_param[] = {
  260. {0x3800, 0x00}, {0x3801, 0x00}, {0x3802, 0x00}, {0x3803, 0xfa},
  261. {0x3804, 0x0a}, {0x3805, 0x3f}, {0x3806, 0x06}, {0x3807, 0xa9},
  262. {0x3808, 0x01}, {0x3809, 0xE0}, {0x380A, 0x01}, {0x380B, 0x10},
  263. {0x380C, 0x07}, {0x380D, 0x64}, {0x380E, 0x02}, {0x380F, 0xe4},
  264. {0x3810, 0x00}, {0x3811, 0x10}, {0x3812, 0x00}, {0x3813, 0x04}, {0x3814, 0x31}, {0x3815, 0x31},
  265. };
  266. static const ov5640_reg_val_t ov5640_resolution_720p_param[] = {
  267. {0x3800, 0x00}, {0x3801, 0x00}, {0x3802, 0x00}, {0x3803, 0xfa},
  268. {0x3804, 0x0a}, {0x3805, 0x3f}, {0x3806, 0x06}, {0x3807, 0xa9},
  269. {0x3808, 0x05}, {0x3809, 0x00}, {0x380A, 0x02}, {0x380B, 0xd0},
  270. {0x380C, 0x07}, {0x380D, 0x64}, {0x380E, 0x02}, {0x380F, 0xe4},
  271. {0x3810, 0x00}, {0x3811, 0x10}, {0x3812, 0x00}, {0x3813, 0x04}, {0x3814, 0x31}, {0x3815, 0x31},
  272. };
  273. static const ov5640_reg_val_t ov5640_resolution_1080p_param[] = {
  274. {0x3800, 0x01}, {0x3801, 0x50}, {0x3802, 0x01}, {0x3803, 0xb2},
  275. {0x3804, 0x08}, {0x3805, 0xef}, {0x3806, 0x05}, {0x3807, 0xf1},
  276. {0x3808, 0x07}, {0x3809, 0x80}, {0x380A, 0x04}, {0x380B, 0x38},
  277. {0x380C, 0x09}, {0x380D, 0xc4}, {0x380E, 0x04}, {0x380F, 0x60},
  278. {0x3810, 0x00}, {0x3811, 0x10}, {0x3812, 0x00}, {0x3813, 0x04}, {0x3814, 0x11}, {0x3815, 0x11},
  279. };
  280. /* DVP clock */
  281. static const ov5640_clock_config_t ov5640_dvp_clock_configs[] = {
  282. {
  283. .resolution = (uint32_t)video_resolution_800_480,
  284. .pllctrl1 = 0x11,
  285. .pllctrl2 = 0x46,
  286. .vfifoctrl0c = 0x22,
  287. .pclkdiv = 0x02,
  288. .pclkperiod = 0x22,
  289. },
  290. {
  291. .resolution = (uint32_t)video_resolution_vga,
  292. .pllctrl1 = 0x11,
  293. .pllctrl2 = 0x46,
  294. .vfifoctrl0c = 0x22,
  295. .pclkdiv = 0x02,
  296. .pclkperiod = 0x22,
  297. },
  298. {
  299. .resolution = (uint32_t)video_resolution_qvga,
  300. .pllctrl1 = 0x11,
  301. .pllctrl2 = 0x46,
  302. .vfifoctrl0c = 0x22,
  303. .pclkdiv = 0x02,
  304. .pclkperiod = 0x22,
  305. },
  306. {
  307. .resolution = (uint32_t)video_resolution_480_272,
  308. .pllctrl1 = 0x21,
  309. .pllctrl2 = 0x69,
  310. .vfifoctrl0c = 0x20,
  311. .pclkdiv = 0x04,
  312. .pclkperiod = 0x16,
  313. },
  314. {
  315. .resolution = (uint32_t)video_resolution_720p,
  316. .pllctrl1 = 0x21,
  317. .pllctrl2 = 0x69,
  318. .vfifoctrl0c = 0x20,
  319. .pclkdiv = 0x04,
  320. .pclkperiod = 0x16,
  321. },
  322. {
  323. .resolution = (uint32_t)video_resolution_1080p,
  324. .pllctrl1 = 0x21,
  325. .pllctrl2 = 0x69,
  326. .vfifoctrl0c = 0x20,
  327. .pclkdiv = 0x04,
  328. .pclkperiod = 0x16,
  329. },
  330. };
  331. static const ov5640_light_mode_config_t ov5640_light_mode_configs[] = {
  332. /* Auto. */
  333. {
  334. .lightmode = camera_light_mode_auto,
  335. .awbctrl = 0x00,
  336. .awbr_h = 0x04,
  337. .awbr_l = 0x00,
  338. .awbg_h = 0x04,
  339. .awbg_l = 0x00,
  340. .awbb_h = 0x04,
  341. .awbb_l = 0x00,
  342. },
  343. /* Sunny. */
  344. {
  345. .lightmode = camera_light_mode_sunny,
  346. .awbctrl = 0x01,
  347. .awbr_h = 0x06,
  348. .awbr_l = 0x1c,
  349. .awbg_h = 0x04,
  350. .awbg_l = 0x00,
  351. .awbb_h = 0x04,
  352. .awbb_l = 0xf3,
  353. },
  354. /* Office. */
  355. {
  356. .lightmode = camera_light_mode_office,
  357. .awbctrl = 0x01,
  358. .awbr_h = 0x05,
  359. .awbr_l = 0x48,
  360. .awbg_h = 0x04,
  361. .awbg_l = 0x00,
  362. .awbb_h = 0x07,
  363. .awbb_l = 0xcf,
  364. },
  365. /* Cloudy. */
  366. {
  367. .lightmode = camera_light_mode_cloudy,
  368. .awbctrl = 0x01,
  369. .awbr_h = 0x06,
  370. .awbr_l = 0x48,
  371. .awbg_h = 0x04,
  372. .awbg_l = 0x00,
  373. .awbb_h = 0x04,
  374. .awbb_l = 0xd3,
  375. },
  376. /* Home. */
  377. {
  378. .lightmode = camera_light_mode_home,
  379. .awbctrl = 0x01,
  380. .awbr_h = 0x04,
  381. .awbr_l = 0x10,
  382. .awbg_h = 0x04,
  383. .awbg_l = 0x00,
  384. .awbb_h = 0x08,
  385. .awbb_l = 0x40,
  386. },
  387. };
  388. static const ov5640_special_effect_config_t ov5640_special_effect_configs[] = {
  389. /* Normal. */
  390. {
  391. .effect = camera_special_effect_normal,
  392. .sdectrl0 = 0x06,
  393. .sdectrl3 = 0x40,
  394. .sdectrl4 = 0x10,
  395. },
  396. /* Bluish. */
  397. {
  398. .effect = camera_special_effect_bluish,
  399. .sdectrl0 = 0x1e,
  400. .sdectrl3 = 0xa0,
  401. .sdectrl4 = 0x40,
  402. },
  403. /* Redish. */
  404. {
  405. .effect = camera_special_effect_redish,
  406. .sdectrl0 = 0x1e,
  407. .sdectrl3 = 0x80,
  408. .sdectrl4 = 0xc0,
  409. },
  410. /* B & W */
  411. {
  412. .effect = camera_special_effect_bw,
  413. .sdectrl0 = 0x1e,
  414. .sdectrl3 = 0x80,
  415. .sdectrl4 = 0x80,
  416. },
  417. /* Sepia. */
  418. {
  419. .effect = camera_special_effect_sepia,
  420. .sdectrl0 = 0x1e,
  421. .sdectrl3 = 0x40,
  422. .sdectrl4 = 0xa0,
  423. },
  424. /* Negtive. */
  425. {
  426. .effect = camera_special_effect_negtive,
  427. .sdectrl0 = 0x40,
  428. .sdectrl3 = 0x40,
  429. .sdectrl4 = 0x10,
  430. },
  431. /* Greenish. */
  432. {
  433. .effect = camera_special_effect_greenish,
  434. .sdectrl0 = 0x1e,
  435. .sdectrl3 = 0x60,
  436. .sdectrl4 = 0x60,
  437. },
  438. };
  439. static const ov5640_clock_config_t *ov5640_get_clock_config(const camera_config_t *config)
  440. {
  441. uint32_t i;
  442. if (camera_interface_dvp == config->interface) {
  443. for (i = 0; i < ARRAY_SIZE(ov5640_dvp_clock_configs); i++) {
  444. if (HPM_CAMERA_RESOLUTION(config->width, config->height) == ov5640_dvp_clock_configs[i].resolution) {
  445. return &ov5640_dvp_clock_configs[i];
  446. }
  447. }
  448. }
  449. return NULL;
  450. }
  451. hpm_stat_t ov5640_read_register(camera_context_t *context, uint16_t reg, uint8_t *buf)
  452. {
  453. uint8_t r[2];
  454. r[0] = reg >> 8;
  455. r[1] = reg & 0xFF;
  456. hpm_stat_t stat = i2c_master_write(context->ptr, context->i2c_device_addr, r, 2);
  457. if (stat != status_success) {
  458. return stat;
  459. }
  460. return i2c_master_read(context->ptr, context->i2c_device_addr, buf, 1);
  461. }
  462. hpm_stat_t ov5640_write_register(camera_context_t *context, uint16_t reg, uint8_t val)
  463. {
  464. uint8_t r[2];
  465. r[0] = reg >> 8;
  466. r[1] = reg & 0xFF;
  467. return i2c_master_address_write(context->ptr, context->i2c_device_addr, r, sizeof(r), &val, 1);
  468. }
  469. hpm_stat_t ov5640_write_multi_registers(camera_context_t *context, const ov5640_reg_val_t regval[], uint32_t len)
  470. {
  471. uint32_t i;
  472. hpm_stat_t stat = status_success;
  473. for (i = 0; i < len; i++) {
  474. HPM_CHECK_RET(ov5640_write_register(context, regval[i].regaddr, regval[i].regval));
  475. }
  476. return stat;
  477. }
  478. hpm_stat_t ov5640_software_reset(camera_context_t *context)
  479. {
  480. hpm_stat_t stat = status_success;
  481. HPM_CHECK_RET(ov5640_write_register(context, 0x3103, 0x11));
  482. HPM_CHECK_RET(ov5640_write_register(context, 0x3008, 0x82));
  483. return stat;
  484. }
  485. hpm_stat_t ov5640_set_pixel_format(camera_context_t *context, display_pixel_format_t pixel_format)
  486. {
  487. hpm_stat_t stat = status_success;
  488. switch (pixel_format) {
  489. case display_pixel_format_y8:
  490. case display_pixel_format_yuv422:
  491. HPM_CHECK_RET(ov5640_write_register(context, 0x4300, 0x30));
  492. HPM_CHECK_RET(ov5640_write_register(context, 0x501f, 0x00));
  493. break;
  494. case display_pixel_format_rgb565:
  495. HPM_CHECK_RET(ov5640_write_register(context, 0x4300, 0x6F));
  496. HPM_CHECK_RET(ov5640_write_register(context, 0x501f, 0x01));
  497. break;
  498. default:
  499. stat = status_invalid_argument;
  500. break;
  501. }
  502. return stat;
  503. }
  504. hpm_stat_t ov5640_check_chip_id(camera_context_t *context)
  505. {
  506. hpm_stat_t stat = status_success;
  507. uint8_t val_h = 0;
  508. uint8_t val_l = 0;
  509. HPM_CHECK_RET(ov5640_read_register(context, OV5640_CHIP_ID_HIGH_BYTE_ADDR, &val_h));
  510. HPM_CHECK_RET(ov5640_read_register(context, OV5640_CHIP_ID_LOW_BYTE_ADDR, &val_l));
  511. if (val_h != OV5640_CHIP_ID_HIGH_BYTE_VALUE) {
  512. return status_fail;
  513. }
  514. if (val_l != OV5640_CHIP_ID_LOW_BYTE_VALUE) {
  515. return status_fail;
  516. }
  517. return stat;
  518. }
  519. hpm_stat_t ov5640_set_image_size(camera_context_t *context, camera_config_t *ov_config)
  520. {
  521. hpm_stat_t stat = status_success;
  522. switch (HPM_CAMERA_RESOLUTION(ov_config->width, ov_config->height)) {
  523. case video_resolution_800_480:
  524. stat = ov5640_write_multi_registers(context, ov5640_resolution_800_480_param, ARRAY_SIZE(ov5640_resolution_800_480_param));
  525. break;
  526. case video_resolution_vga:
  527. stat = ov5640_write_multi_registers(context, ov5640_resolution_vga_param, ARRAY_SIZE(ov5640_resolution_vga_param));
  528. break;
  529. case video_resolution_qvga:
  530. stat = ov5640_write_multi_registers(context, ov5640_resolution_qvga_param, ARRAY_SIZE(ov5640_resolution_qvga_param));
  531. break;
  532. case video_resolution_480_272:
  533. stat = ov5640_write_multi_registers(context, ov5640_resolution_480_272_param, ARRAY_SIZE(ov5640_resolution_480_272_param));
  534. break;
  535. case video_resolution_720p:
  536. stat = ov5640_write_multi_registers(context, ov5640_resolution_720p_param, ARRAY_SIZE(ov5640_resolution_720p_param));
  537. break;
  538. case video_resolution_1080p:
  539. stat = ov5640_write_multi_registers(context, ov5640_resolution_1080p_param, ARRAY_SIZE(ov5640_resolution_1080p_param));
  540. break;
  541. default:
  542. stat = status_invalid_argument;
  543. break;
  544. }
  545. return stat;
  546. }
  547. hpm_stat_t ov5640_set_clock_config(camera_context_t *context, camera_config_t *ov_config)
  548. {
  549. hpm_stat_t stat = status_success;
  550. const ov5640_clock_config_t *clock_config = ov5640_get_clock_config(ov_config);
  551. if (NULL == clock_config) {
  552. return status_invalid_argument;
  553. }
  554. HPM_CHECK_RET(ov5640_write_register(context, 0x3035, clock_config->pllctrl1));
  555. HPM_CHECK_RET(ov5640_write_register(context, 0x3036, clock_config->pllctrl2));
  556. HPM_CHECK_RET(ov5640_write_register(context, 0x460c, clock_config->vfifoctrl0c));
  557. HPM_CHECK_RET(ov5640_write_register(context, 0x3824, clock_config->pclkdiv));
  558. HPM_CHECK_RET(ov5640_write_register(context, 0x4837, clock_config->pclkperiod));
  559. return stat;
  560. }
  561. hpm_stat_t ov5640_set_interface(camera_context_t *context, camera_config_t *ov_config)
  562. {
  563. hpm_stat_t stat = status_success;
  564. if (camera_interface_dvp == ov_config->interface) {
  565. HPM_CHECK_RET(ov5640_write_register(context, 0x3034, 0x1a));
  566. /* Set Frex, Vsync, Href, PCLK, data, GPIO to output. */
  567. HPM_CHECK_RET(ov5640_write_register(context, 0x3017, 0xFF));
  568. HPM_CHECK_RET(ov5640_write_register(context, 0x3018, 0xFF));
  569. /* DVP mode */
  570. HPM_CHECK_RET(ov5640_write_register(context, 0x300e, 0x58));
  571. }
  572. return stat;
  573. }
  574. hpm_stat_t ov5640_start(camera_context_t *context)
  575. {
  576. return ov5640_write_register(context, 0x3008, 0x02);
  577. }
  578. hpm_stat_t ov5640_stop(camera_context_t *context)
  579. {
  580. return ov5640_write_register(context, 0x3008, 0x42);
  581. }
  582. hpm_stat_t ov5640_flip(camera_context_t *context)
  583. {
  584. hpm_stat_t stat = status_success;
  585. HPM_CHECK_RET(ov5640_write_register(context, 0x3821, 1));
  586. return stat;
  587. }
  588. hpm_stat_t ov5640_set_brightness(camera_context_t *context, int32_t brightness)
  589. {
  590. hpm_stat_t stat = status_success;
  591. if ((brightness < -4) || (brightness > 4)) {
  592. return status_invalid_argument;
  593. }
  594. HPM_CHECK_RET(ov5640_write_register(context, 0x3212, 0x03));
  595. if (brightness >= 0) {
  596. HPM_CHECK_RET(ov5640_write_register(context, 0x5588, 0x01));
  597. } else {
  598. brightness = -brightness;
  599. HPM_CHECK_RET(ov5640_write_register(context, 0x5588, 0x09));
  600. }
  601. HPM_CHECK_RET(ov5640_write_register(context, 0x5587, ((uint8_t)brightness) << 4U));
  602. HPM_CHECK_RET(ov5640_write_register(context, 0x3212, 0x13));
  603. HPM_CHECK_RET(ov5640_write_register(context, 0x3212, 0xa3));
  604. return stat;
  605. }
  606. hpm_stat_t ov5640_set_contrast(camera_context_t *context, int32_t contrast)
  607. {
  608. hpm_stat_t stat = status_success;
  609. uint8_t regval;
  610. if ((-4 > contrast) || (4 < contrast)) {
  611. return status_invalid_argument;
  612. }
  613. contrast = 0x20 + contrast * 0x04;
  614. regval = (uint8_t)contrast;
  615. HPM_CHECK_RET(ov5640_write_register(context, 0x3212, 0x03));
  616. HPM_CHECK_RET(ov5640_write_register(context, 0x5580, 0x04));
  617. HPM_CHECK_RET(ov5640_write_register(context, 0x5585, regval));
  618. HPM_CHECK_RET(ov5640_write_register(context, 0x5586, regval));
  619. HPM_CHECK_RET(ov5640_write_register(context, 0x3212, 0x13));
  620. HPM_CHECK_RET(ov5640_write_register(context, 0x3212, 0xa3));
  621. return stat;
  622. }
  623. hpm_stat_t ov5640_set_saturation(camera_context_t *context, int32_t saturation)
  624. {
  625. hpm_stat_t stat = status_success;
  626. uint8_t regval;
  627. if ((-4 > saturation) || (4 < saturation)) {
  628. return status_invalid_argument;
  629. }
  630. saturation = 0x40 + saturation * 0x10;
  631. regval = (uint8_t)saturation;
  632. HPM_CHECK_RET(ov5640_write_register(context, 0x3212, 0x03));
  633. HPM_CHECK_RET(ov5640_write_register(context, 0x5580, 0x02));
  634. HPM_CHECK_RET(ov5640_write_register(context, 0x5583, regval));
  635. HPM_CHECK_RET(ov5640_write_register(context, 0x5584, regval));
  636. HPM_CHECK_RET(ov5640_write_register(context, 0x5588, 0x41));
  637. HPM_CHECK_RET(ov5640_write_register(context, 0x3212, 0x13));
  638. HPM_CHECK_RET(ov5640_write_register(context, 0x3212, 0xa3));
  639. return stat;
  640. }
  641. hpm_stat_t ov5640_set_light_mode(camera_context_t *context, int32_t lightmode)
  642. {
  643. hpm_stat_t stat = status_success;
  644. uint8_t i;
  645. for (i = 0; i < ARRAY_SIZE(ov5640_light_mode_configs); i++) {
  646. if (lightmode == (int32_t)ov5640_light_mode_configs[i].lightmode) {
  647. HPM_CHECK_RET(ov5640_write_register(context, 0x3212, 0x03));
  648. HPM_CHECK_RET(ov5640_write_register(context, 0x3406, ov5640_light_mode_configs[i].awbctrl));
  649. HPM_CHECK_RET(ov5640_write_register(context, 0x3400, ov5640_light_mode_configs[i].awbr_h));
  650. HPM_CHECK_RET(ov5640_write_register(context, 0x3401, ov5640_light_mode_configs[i].awbr_l));
  651. HPM_CHECK_RET(ov5640_write_register(context, 0x3402, ov5640_light_mode_configs[i].awbg_h));
  652. HPM_CHECK_RET(ov5640_write_register(context, 0x3403, ov5640_light_mode_configs[i].awbg_l));
  653. HPM_CHECK_RET(ov5640_write_register(context, 0x3404, ov5640_light_mode_configs[i].awbb_h));
  654. HPM_CHECK_RET(ov5640_write_register(context, 0x3405, ov5640_light_mode_configs[i].awbb_l));
  655. HPM_CHECK_RET(ov5640_write_register(context, 0x3212, 0x13));
  656. HPM_CHECK_RET(ov5640_write_register(context, 0x3212, 0xa3));
  657. return stat;
  658. }
  659. }
  660. /* No configuration found. */
  661. return status_invalid_argument;
  662. }
  663. hpm_stat_t ov5640_set_special_effect(camera_context_t *context, int32_t effect)
  664. {
  665. hpm_stat_t stat = status_success;
  666. uint8_t i;
  667. for (i = 0; i < ARRAY_SIZE(ov5640_special_effect_configs); i++) {
  668. if (effect == (int32_t)ov5640_special_effect_configs[i].effect) {
  669. HPM_CHECK_RET(ov5640_write_register(context, 0x3212, 0x03));
  670. HPM_CHECK_RET(ov5640_write_register(context, 0x5580, ov5640_special_effect_configs[i].sdectrl0));
  671. HPM_CHECK_RET(ov5640_write_register(context, 0x5583, ov5640_special_effect_configs[i].sdectrl3));
  672. HPM_CHECK_RET(ov5640_write_register(context, 0x5584, ov5640_special_effect_configs[i].sdectrl4));
  673. HPM_CHECK_RET(ov5640_write_register(context, 0x5003, 0x08));
  674. HPM_CHECK_RET(ov5640_write_register(context, 0x3212, 0x13));
  675. HPM_CHECK_RET(ov5640_write_register(context, 0x3212, 0xa3));
  676. return stat;
  677. }
  678. }
  679. /* No configuration found. */
  680. return status_invalid_argument;
  681. }
  682. hpm_stat_t ov5640_init(camera_context_t *context, camera_config_t *ov_config)
  683. {
  684. hpm_stat_t stat = status_success;
  685. /* check the chip id */
  686. HPM_CHECK_RET(ov5640_check_chip_id(context));
  687. /* Initialize: load registers value */
  688. HPM_CHECK_RET(ov5640_write_multi_registers(context, ov5640_init_param, ARRAY_SIZE(ov5640_init_param)));
  689. /* configure image windowing */
  690. HPM_CHECK_RET(ov5640_set_image_size(context, ov_config));
  691. HPM_CHECK_RET(ov5640_flip(context));
  692. /* configure Pixel format */
  693. HPM_CHECK_RET(ov5640_set_pixel_format(context, ov_config->pixel_format));
  694. /* configure PCLK clock */
  695. HPM_CHECK_RET(ov5640_set_clock_config(context, ov_config));
  696. /* configure interface */
  697. HPM_CHECK_RET(ov5640_set_interface(context, ov_config));
  698. /* configure contrast */
  699. HPM_CHECK_RET(ov5640_set_contrast(context, 2));
  700. /* camera start */
  701. HPM_CHECK_RET(ov5640_write_register(context, 0x3008, 0x02));
  702. return stat;
  703. }
  704. void ov5640_power_up(camera_context_t *context)
  705. {
  706. assert((context->delay_ms != NULL) && (context->write_rst != NULL) && (context->write_pwdn != NULL));
  707. context->write_rst(OV5640_RST_ACTIVE);
  708. context->write_pwdn(OV5640_PWDN_ACTIVE);
  709. context->delay_ms(5);
  710. context->write_pwdn(OV5640_PWDN_INACTIVE);
  711. context->delay_ms(2);
  712. context->write_rst(OV5640_RST_INACTIVE);
  713. context->delay_ms(20);
  714. }