ov5640.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130
  1. /*
  2. * This file is part of the OpenMV project.
  3. * Copyright (c) 2013/2014 Ibrahim Abdelkader <i.abdalkader@gmail.com>
  4. * This work is licensed under the MIT license, see the file LICENSE for details.
  5. *
  6. * OV3660 driver.
  7. *
  8. */
  9. #include <stdint.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include "sccb.h"
  13. #include "xclk.h"
  14. #include "ov5640.h"
  15. #include "ov5640_regs.h"
  16. #include "ov5640_settings.h"
  17. #include "freertos/FreeRTOS.h"
  18. #include "freertos/task.h"
  19. #if defined(ARDUINO_ARCH_ESP32) && defined(CONFIG_ARDUHAL_ESP_LOG)
  20. #include "esp32-hal-log.h"
  21. #else
  22. #include "esp_log.h"
  23. static const char *TAG = "ov5640";
  24. #endif
  25. //#define REG_DEBUG_ON
  26. static int read_reg(uint8_t slv_addr, const uint16_t reg){
  27. int ret = SCCB_Read16(slv_addr, reg);
  28. #ifdef REG_DEBUG_ON
  29. if (ret < 0) {
  30. ESP_LOGE(TAG, "READ REG 0x%04x FAILED: %d", reg, ret);
  31. }
  32. #endif
  33. return ret;
  34. }
  35. static int check_reg_mask(uint8_t slv_addr, uint16_t reg, uint8_t mask){
  36. return (read_reg(slv_addr, reg) & mask) == mask;
  37. }
  38. static int read_reg16(uint8_t slv_addr, const uint16_t reg){
  39. int ret = 0, ret2 = 0;
  40. ret = read_reg(slv_addr, reg);
  41. if (ret >= 0) {
  42. ret = (ret & 0xFF) << 8;
  43. ret2 = read_reg(slv_addr, reg+1);
  44. if (ret2 < 0) {
  45. ret = ret2;
  46. } else {
  47. ret |= ret2 & 0xFF;
  48. }
  49. }
  50. return ret;
  51. }
  52. //static void dump_reg(sensor_t *sensor, const uint16_t reg){
  53. // int v = SCCB_Read16(sensor->slv_addr, reg);
  54. // if(v < 0){
  55. // ets_printf(" 0x%04x: FAIL[%d]\n", reg, v);
  56. // } else {
  57. // ets_printf(" 0x%04x: 0x%02X\n", reg, v);
  58. // }
  59. //}
  60. //
  61. //static void dump_range(sensor_t *sensor, const char * name, const uint16_t start_reg, const uint16_t end_reg){
  62. // ets_printf("%s: 0x%04x - 0x%04X\n", name, start_reg, end_reg);
  63. // for(uint16_t reg = start_reg; reg <= end_reg; reg++){
  64. // dump_reg(sensor, reg);
  65. // }
  66. //}
  67. //
  68. //static void dump_regs(sensor_t *sensor){
  69. //// dump_range(sensor, "All Regs", 0x3000, 0x6100);
  70. //// dump_range(sensor, "system and IO pad control", 0x3000, 0x3052);
  71. //// dump_range(sensor, "SCCB control", 0x3100, 0x3108);
  72. //// dump_range(sensor, "SRB control", 0x3200, 0x3211);
  73. //// dump_range(sensor, "AWB gain control", 0x3400, 0x3406);
  74. //// dump_range(sensor, "AEC/AGC control", 0x3500, 0x350D);
  75. //// dump_range(sensor, "VCM control", 0x3600, 0x3606);
  76. //// dump_range(sensor, "timing control", 0x3800, 0x3821);
  77. //// dump_range(sensor, "AEC/AGC power down domain control", 0x3A00, 0x3A25);
  78. //// dump_range(sensor, "strobe control", 0x3B00, 0x3B0C);
  79. //// dump_range(sensor, "50/60Hz detector control", 0x3C00, 0x3C1E);
  80. //// dump_range(sensor, "OTP control", 0x3D00, 0x3D21);
  81. //// dump_range(sensor, "MC control", 0x3F00, 0x3F0D);
  82. //// dump_range(sensor, "BLC control", 0x4000, 0x4033);
  83. //// dump_range(sensor, "frame control", 0x4201, 0x4202);
  84. //// dump_range(sensor, "format control", 0x4300, 0x430D);
  85. //// dump_range(sensor, "JPEG control", 0x4400, 0x4431);
  86. //// dump_range(sensor, "VFIFO control", 0x4600, 0x460D);
  87. //// dump_range(sensor, "DVP control", 0x4709, 0x4745);
  88. //// dump_range(sensor, "MIPI control", 0x4800, 0x4837);
  89. //// dump_range(sensor, "ISP frame control", 0x4901, 0x4902);
  90. //// dump_range(sensor, "ISP top control", 0x5000, 0x5063);
  91. //// dump_range(sensor, "AWB control", 0x5180, 0x51D0);
  92. //// dump_range(sensor, "CIP control", 0x5300, 0x530F);
  93. //// dump_range(sensor, "CMX control", 0x5380, 0x538B);
  94. //// dump_range(sensor, "gamma control", 0x5480, 0x5490);
  95. //// dump_range(sensor, "SDE control", 0x5580, 0x558C);
  96. //// dump_range(sensor, "scale control", 0x5600, 0x5606);
  97. //// dump_range(sensor, "AVG control", 0x5680, 0x56A2);
  98. //// dump_range(sensor, "LENC control", 0x5800, 0x5849);
  99. //// dump_range(sensor, "AFC control", 0x6000, 0x603F);
  100. //}
  101. static int write_reg(uint8_t slv_addr, const uint16_t reg, uint8_t value){
  102. int ret = 0;
  103. #ifndef REG_DEBUG_ON
  104. ret = SCCB_Write16(slv_addr, reg, value);
  105. #else
  106. int old_value = read_reg(slv_addr, reg);
  107. if (old_value < 0) {
  108. return old_value;
  109. }
  110. if ((uint8_t)old_value != value) {
  111. ESP_LOGI(TAG, "NEW REG 0x%04x: 0x%02x to 0x%02x", reg, (uint8_t)old_value, value);
  112. ret = SCCB_Write16(slv_addr, reg, value);
  113. } else {
  114. ESP_LOGD(TAG, "OLD REG 0x%04x: 0x%02x", reg, (uint8_t)old_value);
  115. ret = SCCB_Write16(slv_addr, reg, value);//maybe not?
  116. }
  117. if (ret < 0) {
  118. ESP_LOGE(TAG, "WRITE REG 0x%04x FAILED: %d", reg, ret);
  119. }
  120. #endif
  121. return ret;
  122. }
  123. static int set_reg_bits(uint8_t slv_addr, uint16_t reg, uint8_t offset, uint8_t mask, uint8_t value)
  124. {
  125. int ret = 0;
  126. uint8_t c_value, new_value;
  127. ret = read_reg(slv_addr, reg);
  128. if(ret < 0) {
  129. return ret;
  130. }
  131. c_value = ret;
  132. new_value = (c_value & ~(mask << offset)) | ((value & mask) << offset);
  133. ret = write_reg(slv_addr, reg, new_value);
  134. return ret;
  135. }
  136. static int write_regs(uint8_t slv_addr, const uint16_t (*regs)[2])
  137. {
  138. int i = 0, ret = 0;
  139. while (!ret && regs[i][0] != REGLIST_TAIL) {
  140. if (regs[i][0] == REG_DLY) {
  141. vTaskDelay(regs[i][1] / portTICK_PERIOD_MS);
  142. } else {
  143. ret = write_reg(slv_addr, regs[i][0], regs[i][1]);
  144. }
  145. i++;
  146. }
  147. return ret;
  148. }
  149. static int write_reg16(uint8_t slv_addr, const uint16_t reg, uint16_t value)
  150. {
  151. if (write_reg(slv_addr, reg, value >> 8) || write_reg(slv_addr, reg + 1, value)) {
  152. return -1;
  153. }
  154. return 0;
  155. }
  156. static int write_addr_reg(uint8_t slv_addr, const uint16_t reg, uint16_t x_value, uint16_t y_value)
  157. {
  158. if (write_reg16(slv_addr, reg, x_value) || write_reg16(slv_addr, reg + 2, y_value)) {
  159. return -1;
  160. }
  161. return 0;
  162. }
  163. #define write_reg_bits(slv_addr, reg, mask, enable) set_reg_bits(slv_addr, reg, 0, mask, (enable)?(mask):0)
  164. static int calc_sysclk(int xclk, bool pll_bypass, int pll_multiplier, int pll_sys_div, int pre_div, bool root_2x, int pclk_root_div, bool pclk_manual, int pclk_div)
  165. {
  166. const float pll_pre_div2x_map[] = { 1, 1, 2, 3, 4, 1.5, 6, 2.5, 8};
  167. const int pll_pclk_root_div_map[] = { 1, 2, 4, 8 };
  168. if(!pll_sys_div) {
  169. pll_sys_div = 1;
  170. }
  171. float pll_pre_div = pll_pre_div2x_map[pre_div];
  172. unsigned int root_2x_div = root_2x?2:1;
  173. unsigned int pll_pclk_root_div = pll_pclk_root_div_map[pclk_root_div];
  174. unsigned int REFIN = xclk / pll_pre_div;
  175. unsigned int VCO = REFIN * pll_multiplier / root_2x_div;
  176. unsigned int PLL_CLK = pll_bypass?(xclk):(VCO / pll_sys_div * 2 / 5);//5 here is 10bit mode / 2, for 8bit it should be 4 (reg 0x3034)
  177. unsigned int PCLK = PLL_CLK / pll_pclk_root_div / ((pclk_manual && pclk_div)?pclk_div:2);
  178. unsigned int SYSCLK = PLL_CLK / 4;
  179. ESP_LOGI(TAG, "Calculated XVCLK: %d Hz, REFIN: %u Hz, VCO: %u Hz, PLL_CLK: %u Hz, SYSCLK: %u Hz, PCLK: %u Hz", xclk, REFIN, VCO, PLL_CLK, SYSCLK, PCLK);
  180. return SYSCLK;
  181. }
  182. static int set_pll(sensor_t *sensor, bool bypass, uint8_t multiplier, uint8_t sys_div, uint8_t pre_div, bool root_2x, uint8_t pclk_root_div, bool pclk_manual, uint8_t pclk_div){
  183. int ret = 0;
  184. if(multiplier > 252 || multiplier < 4 || sys_div > 15 || pre_div > 8 || pclk_div > 31 || pclk_root_div > 3){
  185. ESP_LOGE(TAG, "Invalid arguments");
  186. return -1;
  187. }
  188. if(multiplier > 127){
  189. multiplier &= 0xFE;//only even integers above 127
  190. }
  191. ESP_LOGI(TAG, "Set PLL: bypass: %u, multiplier: %u, sys_div: %u, pre_div: %u, root_2x: %u, pclk_root_div: %u, pclk_manual: %u, pclk_div: %u", bypass, multiplier, sys_div, pre_div, root_2x, pclk_root_div, pclk_manual, pclk_div);
  192. calc_sysclk(sensor->xclk_freq_hz, bypass, multiplier, sys_div, pre_div, root_2x, pclk_root_div, pclk_manual, pclk_div);
  193. ret = write_reg(sensor->slv_addr, 0x3039, bypass?0x80:0x00);
  194. if (ret == 0) {
  195. ret = write_reg(sensor->slv_addr, 0x3034, 0x1A);//10bit mode
  196. }
  197. if (ret == 0) {
  198. ret = write_reg(sensor->slv_addr, 0x3035, 0x01 | ((sys_div & 0x0f) << 4));
  199. }
  200. if (ret == 0) {
  201. ret = write_reg(sensor->slv_addr, 0x3036, multiplier & 0xff);
  202. }
  203. if (ret == 0) {
  204. ret = write_reg(sensor->slv_addr, 0x3037, (pre_div & 0xf) | (root_2x?0x10:0x00));
  205. }
  206. if (ret == 0) {
  207. ret = write_reg(sensor->slv_addr, 0x3108, (pclk_root_div & 0x3) << 4 | 0x06);
  208. }
  209. if (ret == 0) {
  210. ret = write_reg(sensor->slv_addr, 0x3824, pclk_div & 0x1f);
  211. }
  212. if (ret == 0) {
  213. ret = write_reg(sensor->slv_addr, 0x460C, pclk_manual?0x22:0x20);
  214. }
  215. if (ret == 0) {
  216. ret = write_reg(sensor->slv_addr, 0x3103, 0x13);// system clock from pll, bit[1]
  217. }
  218. if(ret){
  219. ESP_LOGE(TAG, "set_sensor_pll FAILED!");
  220. }
  221. return ret;
  222. }
  223. static int set_ae_level(sensor_t *sensor, int level);
  224. static int reset(sensor_t *sensor)
  225. {
  226. //dump_regs(sensor);
  227. vTaskDelay(100 / portTICK_PERIOD_MS);
  228. int ret = 0;
  229. // Software Reset: clear all registers and reset them to their default values
  230. ret = write_reg(sensor->slv_addr, SYSTEM_CTROL0, 0x82);
  231. if(ret){
  232. ESP_LOGE(TAG, "Software Reset FAILED!");
  233. return ret;
  234. }
  235. vTaskDelay(100 / portTICK_PERIOD_MS);
  236. ret = write_regs(sensor->slv_addr, sensor_default_regs);
  237. if (ret == 0) {
  238. ESP_LOGD(TAG, "Camera defaults loaded");
  239. vTaskDelay(100 / portTICK_PERIOD_MS);
  240. //write_regs(sensor->slv_addr, sensor_regs_awb0);
  241. //write_regs(sensor->slv_addr, sensor_regs_gamma1);
  242. }
  243. return ret;
  244. }
  245. static int set_pixformat(sensor_t *sensor, pixformat_t pixformat)
  246. {
  247. int ret = 0;
  248. const uint16_t (*regs)[2];
  249. switch (pixformat) {
  250. case PIXFORMAT_YUV422:
  251. regs = sensor_fmt_yuv422;
  252. break;
  253. case PIXFORMAT_GRAYSCALE:
  254. regs = sensor_fmt_grayscale;
  255. break;
  256. case PIXFORMAT_RGB565:
  257. case PIXFORMAT_RGB888:
  258. regs = sensor_fmt_rgb565;
  259. break;
  260. case PIXFORMAT_JPEG:
  261. regs = sensor_fmt_jpeg;
  262. break;
  263. case PIXFORMAT_RAW:
  264. regs = sensor_fmt_raw;
  265. break;
  266. default:
  267. ESP_LOGE(TAG, "Unsupported pixformat: %u", pixformat);
  268. return -1;
  269. }
  270. ret = write_regs(sensor->slv_addr, regs);
  271. if(ret == 0) {
  272. sensor->pixformat = pixformat;
  273. ESP_LOGD(TAG, "Set pixformat to: %u", pixformat);
  274. }
  275. return ret;
  276. }
  277. static int set_image_options(sensor_t *sensor)
  278. {
  279. int ret = 0;
  280. uint8_t reg20 = 0;
  281. uint8_t reg21 = 0;
  282. uint8_t reg4514 = 0;
  283. uint8_t reg4514_test = 0;
  284. // compression
  285. if (sensor->pixformat == PIXFORMAT_JPEG) {
  286. reg21 |= 0x20;
  287. }
  288. // binning
  289. if (!sensor->status.binning) {
  290. reg20 |= 0x40;
  291. } else {
  292. reg20 |= 0x01;
  293. reg21 |= 0x01;
  294. reg4514_test |= 4;
  295. }
  296. // V-Flip
  297. if (sensor->status.vflip) {
  298. reg20 |= 0x06;
  299. reg4514_test |= 1;
  300. }
  301. // H-Mirror
  302. if (sensor->status.hmirror) {
  303. reg21 |= 0x06;
  304. reg4514_test |= 2;
  305. }
  306. switch (reg4514_test) {
  307. //no binning
  308. case 0: reg4514 = 0x88; break;//normal
  309. case 1: reg4514 = 0x00; break;//v-flip
  310. case 2: reg4514 = 0xbb; break;//h-mirror
  311. case 3: reg4514 = 0x00; break;//v-flip+h-mirror
  312. //binning
  313. case 4: reg4514 = 0xaa; break;//normal
  314. case 5: reg4514 = 0xbb; break;//v-flip
  315. case 6: reg4514 = 0xbb; break;//h-mirror
  316. case 7: reg4514 = 0xaa; break;//v-flip+h-mirror
  317. }
  318. if(write_reg(sensor->slv_addr, TIMING_TC_REG20, reg20)
  319. || write_reg(sensor->slv_addr, TIMING_TC_REG21, reg21)
  320. || write_reg(sensor->slv_addr, 0x4514, reg4514)){
  321. ESP_LOGE(TAG, "Setting Image Options Failed");
  322. return -1;
  323. }
  324. if (!sensor->status.binning) {
  325. ret = write_reg(sensor->slv_addr, 0x4520, 0x10)
  326. || write_reg(sensor->slv_addr, X_INCREMENT, 0x11)//odd:1, even: 1
  327. || write_reg(sensor->slv_addr, Y_INCREMENT, 0x11);//odd:1, even: 1
  328. } else {
  329. ret = write_reg(sensor->slv_addr, 0x4520, 0x0b)
  330. || write_reg(sensor->slv_addr, X_INCREMENT, 0x31)//odd:3, even: 1
  331. || write_reg(sensor->slv_addr, Y_INCREMENT, 0x31);//odd:3, even: 1
  332. }
  333. ESP_LOGD(TAG, "Set Image Options: Compression: %u, Binning: %u, V-Flip: %u, H-Mirror: %u, Reg-4514: 0x%02x",
  334. sensor->pixformat == PIXFORMAT_JPEG, sensor->status.binning, sensor->status.vflip, sensor->status.hmirror, reg4514);
  335. return ret;
  336. }
  337. static int set_framesize(sensor_t *sensor, framesize_t framesize)
  338. {
  339. int ret = 0;
  340. framesize_t old_framesize = sensor->status.framesize;
  341. sensor->status.framesize = framesize;
  342. if(framesize > FRAMESIZE_QSXGA){
  343. ESP_LOGE(TAG, "Invalid framesize: %u", framesize);
  344. return -1;
  345. }
  346. uint16_t w = resolution[framesize].width;
  347. uint16_t h = resolution[framesize].height;
  348. aspect_ratio_t ratio = resolution[framesize].aspect_ratio;
  349. ratio_settings_t settings = ratio_table[ratio];
  350. sensor->status.binning = (w <= (settings.max_width / 2) && h <= (settings.max_height / 2));
  351. sensor->status.scale = !((w == settings.max_width && h == settings.max_height)
  352. || (w == (settings.max_width / 2) && h == (settings.max_height / 2)));
  353. ret = write_addr_reg(sensor->slv_addr, X_ADDR_ST_H, settings.start_x, settings.start_y)
  354. || write_addr_reg(sensor->slv_addr, X_ADDR_END_H, settings.end_x, settings.end_y)
  355. || write_addr_reg(sensor->slv_addr, X_OUTPUT_SIZE_H, w, h);
  356. if (ret) {
  357. goto fail;
  358. }
  359. if (!sensor->status.binning) {
  360. ret = write_addr_reg(sensor->slv_addr, X_TOTAL_SIZE_H, settings.total_x, settings.total_y)
  361. || write_addr_reg(sensor->slv_addr, X_OFFSET_H, settings.offset_x, settings.offset_y);
  362. } else {
  363. if (w > 920) {
  364. ret = write_addr_reg(sensor->slv_addr, X_TOTAL_SIZE_H, settings.total_x - 200, settings.total_y / 2);
  365. } else {
  366. ret = write_addr_reg(sensor->slv_addr, X_TOTAL_SIZE_H, 2060, settings.total_y / 2);
  367. }
  368. if (ret == 0) {
  369. ret = write_addr_reg(sensor->slv_addr, X_OFFSET_H, settings.offset_x / 2, settings.offset_y / 2);
  370. }
  371. }
  372. if (ret == 0) {
  373. ret = write_reg_bits(sensor->slv_addr, ISP_CONTROL_01, 0x20, sensor->status.scale);
  374. }
  375. if (ret == 0) {
  376. ret = set_image_options(sensor);
  377. }
  378. if (ret) {
  379. goto fail;
  380. }
  381. if (sensor->pixformat == PIXFORMAT_JPEG) {
  382. //10MHz PCLK
  383. uint8_t sys_mul = 200;
  384. if(framesize < FRAMESIZE_QVGA || sensor->xclk_freq_hz == 16000000){
  385. sys_mul = 160;
  386. } else if(framesize < FRAMESIZE_XGA){
  387. sys_mul = 180;
  388. }
  389. ret = set_pll(sensor, false, sys_mul, 4, 2, false, 2, true, 4);
  390. //Set PLL: bypass: 0, multiplier: sys_mul, sys_div: 4, pre_div: 2, root_2x: 0, pclk_root_div: 2, pclk_manual: 1, pclk_div: 4
  391. } else {
  392. //ret = set_pll(sensor, false, 8, 1, 1, false, 1, true, 4);
  393. if (framesize > FRAMESIZE_HVGA) {
  394. ret = set_pll(sensor, false, 10, 1, 2, false, 1, true, 2);
  395. } else if (framesize >= FRAMESIZE_QVGA) {
  396. ret = set_pll(sensor, false, 8, 1, 1, false, 1, true, 4);
  397. } else {
  398. ret = set_pll(sensor, false, 20, 1, 1, false, 1, true, 8);
  399. }
  400. }
  401. if (ret == 0) {
  402. ESP_LOGD(TAG, "Set framesize to: %ux%u", w, h);
  403. }
  404. return ret;
  405. fail:
  406. sensor->status.framesize = old_framesize;
  407. ESP_LOGE(TAG, "Setting framesize to: %ux%u failed", w, h);
  408. return ret;
  409. }
  410. static int set_hmirror(sensor_t *sensor, int enable)
  411. {
  412. int ret = 0;
  413. sensor->status.hmirror = enable;
  414. ret = set_image_options(sensor);
  415. if (ret == 0) {
  416. ESP_LOGD(TAG, "Set h-mirror to: %d", enable);
  417. }
  418. return ret;
  419. }
  420. static int set_vflip(sensor_t *sensor, int enable)
  421. {
  422. int ret = 0;
  423. sensor->status.vflip = enable;
  424. ret = set_image_options(sensor);
  425. if (ret == 0) {
  426. ESP_LOGD(TAG, "Set v-flip to: %d", enable);
  427. }
  428. return ret;
  429. }
  430. static int set_quality(sensor_t *sensor, int qs)
  431. {
  432. int ret = 0;
  433. ret = write_reg(sensor->slv_addr, COMPRESSION_CTRL07, qs & 0x3f);
  434. if (ret == 0) {
  435. sensor->status.quality = qs;
  436. ESP_LOGD(TAG, "Set quality to: %d", qs);
  437. }
  438. return ret;
  439. }
  440. static int set_colorbar(sensor_t *sensor, int enable)
  441. {
  442. int ret = 0;
  443. ret = write_reg_bits(sensor->slv_addr, PRE_ISP_TEST_SETTING_1, TEST_COLOR_BAR, enable);
  444. if (ret == 0) {
  445. sensor->status.colorbar = enable;
  446. ESP_LOGD(TAG, "Set colorbar to: %d", enable);
  447. }
  448. return ret;
  449. }
  450. static int set_gain_ctrl(sensor_t *sensor, int enable)
  451. {
  452. int ret = 0;
  453. ret = write_reg_bits(sensor->slv_addr, AEC_PK_MANUAL, AEC_PK_MANUAL_AGC_MANUALEN, !enable);
  454. if (ret == 0) {
  455. ESP_LOGD(TAG, "Set gain_ctrl to: %d", enable);
  456. sensor->status.agc = enable;
  457. }
  458. return ret;
  459. }
  460. static int set_exposure_ctrl(sensor_t *sensor, int enable)
  461. {
  462. int ret = 0;
  463. ret = write_reg_bits(sensor->slv_addr, AEC_PK_MANUAL, AEC_PK_MANUAL_AEC_MANUALEN, !enable);
  464. if (ret == 0) {
  465. ESP_LOGD(TAG, "Set exposure_ctrl to: %d", enable);
  466. sensor->status.aec = enable;
  467. }
  468. return ret;
  469. }
  470. static int set_whitebal(sensor_t *sensor, int enable)
  471. {
  472. int ret = 0;
  473. ret = write_reg_bits(sensor->slv_addr, ISP_CONTROL_01, 0x01, enable);
  474. if (ret == 0) {
  475. ESP_LOGD(TAG, "Set awb to: %d", enable);
  476. sensor->status.awb = enable;
  477. }
  478. return ret;
  479. }
  480. //Advanced AWB
  481. static int set_dcw_dsp(sensor_t *sensor, int enable)
  482. {
  483. int ret = 0;
  484. ret = write_reg_bits(sensor->slv_addr, 0x5183, 0x80, !enable);
  485. if (ret == 0) {
  486. ESP_LOGD(TAG, "Set dcw to: %d", enable);
  487. sensor->status.dcw = enable;
  488. }
  489. return ret;
  490. }
  491. //night mode enable
  492. static int set_aec2(sensor_t *sensor, int enable)
  493. {
  494. int ret = 0;
  495. ret = write_reg_bits(sensor->slv_addr, 0x3a00, 0x04, enable);
  496. if (ret == 0) {
  497. ESP_LOGD(TAG, "Set aec2 to: %d", enable);
  498. sensor->status.aec2 = enable;
  499. }
  500. return ret;
  501. }
  502. static int set_bpc_dsp(sensor_t *sensor, int enable)
  503. {
  504. int ret = 0;
  505. ret = write_reg_bits(sensor->slv_addr, 0x5000, 0x04, enable);
  506. if (ret == 0) {
  507. ESP_LOGD(TAG, "Set bpc to: %d", enable);
  508. sensor->status.bpc = enable;
  509. }
  510. return ret;
  511. }
  512. static int set_wpc_dsp(sensor_t *sensor, int enable)
  513. {
  514. int ret = 0;
  515. ret = write_reg_bits(sensor->slv_addr, 0x5000, 0x02, enable);
  516. if (ret == 0) {
  517. ESP_LOGD(TAG, "Set wpc to: %d", enable);
  518. sensor->status.wpc = enable;
  519. }
  520. return ret;
  521. }
  522. //Gamma enable
  523. static int set_raw_gma_dsp(sensor_t *sensor, int enable)
  524. {
  525. int ret = 0;
  526. ret = write_reg_bits(sensor->slv_addr, 0x5000, 0x20, enable);
  527. if (ret == 0) {
  528. ESP_LOGD(TAG, "Set raw_gma to: %d", enable);
  529. sensor->status.raw_gma = enable;
  530. }
  531. return ret;
  532. }
  533. static int set_lenc_dsp(sensor_t *sensor, int enable)
  534. {
  535. int ret = 0;
  536. ret = write_reg_bits(sensor->slv_addr, 0x5000, 0x80, enable);
  537. if (ret == 0) {
  538. ESP_LOGD(TAG, "Set lenc to: %d", enable);
  539. sensor->status.lenc = enable;
  540. }
  541. return ret;
  542. }
  543. static int get_agc_gain(sensor_t *sensor)
  544. {
  545. int ra = read_reg(sensor->slv_addr, 0x350a);
  546. if (ra < 0) {
  547. return 0;
  548. }
  549. int rb = read_reg(sensor->slv_addr, 0x350b);
  550. if (rb < 0) {
  551. return 0;
  552. }
  553. int res = (rb & 0xF0) >> 4 | (ra & 0x03) << 4;
  554. if (rb & 0x0F) {
  555. res += 1;
  556. }
  557. return res;
  558. }
  559. //real gain
  560. static int set_agc_gain(sensor_t *sensor, int gain)
  561. {
  562. int ret = 0;
  563. if(gain < 0) {
  564. gain = 0;
  565. } else if(gain > 64) {
  566. gain = 64;
  567. }
  568. //gain value is 6.4 bits float
  569. //in order to use the max range, we deduct 1/16
  570. int gainv = gain << 4;
  571. if(gainv){
  572. gainv -= 1;
  573. }
  574. ret = write_reg(sensor->slv_addr, 0x350a, gainv >> 8) || write_reg(sensor->slv_addr, 0x350b, gainv & 0xff);
  575. if (ret == 0) {
  576. ESP_LOGD(TAG, "Set agc_gain to: %d", gain);
  577. sensor->status.agc_gain = gain;
  578. }
  579. return ret;
  580. }
  581. static int get_aec_value(sensor_t *sensor)
  582. {
  583. int ra = read_reg(sensor->slv_addr, 0x3500);
  584. if (ra < 0) {
  585. return 0;
  586. }
  587. int rb = read_reg(sensor->slv_addr, 0x3501);
  588. if (rb < 0) {
  589. return 0;
  590. }
  591. int rc = read_reg(sensor->slv_addr, 0x3502);
  592. if (rc < 0) {
  593. return 0;
  594. }
  595. int res = (ra & 0x0F) << 12 | (rb & 0xFF) << 4 | (rc & 0xF0) >> 4;
  596. return res;
  597. }
  598. static int set_aec_value(sensor_t *sensor, int value)
  599. {
  600. int ret = 0, max_val = 0;
  601. max_val = read_reg16(sensor->slv_addr, 0x380e);
  602. if (max_val < 0) {
  603. ESP_LOGE(TAG, "Could not read max aec_value");
  604. return -1;
  605. }
  606. if (value > max_val) {
  607. value =max_val;
  608. }
  609. ret = write_reg(sensor->slv_addr, 0x3500, (value >> 12) & 0x0F)
  610. || write_reg(sensor->slv_addr, 0x3501, (value >> 4) & 0xFF)
  611. || write_reg(sensor->slv_addr, 0x3502, (value << 4) & 0xF0);
  612. if (ret == 0) {
  613. ESP_LOGD(TAG, "Set aec_value to: %d / %d", value, max_val);
  614. sensor->status.aec_value = value;
  615. }
  616. return ret;
  617. }
  618. static int set_ae_level(sensor_t *sensor, int level)
  619. {
  620. int ret = 0;
  621. if (level < -5 || level > 5) {
  622. return -1;
  623. }
  624. //good targets are between 5 and 115
  625. int target_level = ((level + 5) * 10) + 5;
  626. int level_high, level_low;
  627. int fast_high, fast_low;
  628. level_low = target_level * 23 / 25; //0.92 (0.46)
  629. level_high = target_level * 27 / 25; //1.08 (2.08)
  630. fast_low = level_low >> 1;
  631. fast_high = level_high << 1;
  632. if(fast_high>255) {
  633. fast_high = 255;
  634. }
  635. ret = write_reg(sensor->slv_addr, 0x3a0f, level_high)
  636. || write_reg(sensor->slv_addr, 0x3a10, level_low)
  637. || write_reg(sensor->slv_addr, 0x3a1b, level_high)
  638. || write_reg(sensor->slv_addr, 0x3a1e, level_low)
  639. || write_reg(sensor->slv_addr, 0x3a11, fast_high)
  640. || write_reg(sensor->slv_addr, 0x3a1f, fast_low);
  641. if (ret == 0) {
  642. ESP_LOGD(TAG, "Set ae_level to: %d", level);
  643. sensor->status.ae_level = level;
  644. }
  645. return ret;
  646. }
  647. static int set_wb_mode(sensor_t *sensor, int mode)
  648. {
  649. int ret = 0;
  650. if (mode < 0 || mode > 4) {
  651. return -1;
  652. }
  653. ret = write_reg(sensor->slv_addr, 0x3406, (mode != 0));
  654. if (ret) {
  655. return ret;
  656. }
  657. switch (mode) {
  658. case 1://Sunny
  659. ret = write_reg16(sensor->slv_addr, 0x3400, 0x5e0) //AWB R GAIN
  660. || write_reg16(sensor->slv_addr, 0x3402, 0x410) //AWB G GAIN
  661. || write_reg16(sensor->slv_addr, 0x3404, 0x540);//AWB B GAIN
  662. break;
  663. case 2://Cloudy
  664. ret = write_reg16(sensor->slv_addr, 0x3400, 0x650) //AWB R GAIN
  665. || write_reg16(sensor->slv_addr, 0x3402, 0x410) //AWB G GAIN
  666. || write_reg16(sensor->slv_addr, 0x3404, 0x4f0);//AWB B GAIN
  667. break;
  668. case 3://Office
  669. ret = write_reg16(sensor->slv_addr, 0x3400, 0x520) //AWB R GAIN
  670. || write_reg16(sensor->slv_addr, 0x3402, 0x410) //AWB G GAIN
  671. || write_reg16(sensor->slv_addr, 0x3404, 0x660);//AWB B GAIN
  672. break;
  673. case 4://HOME
  674. ret = write_reg16(sensor->slv_addr, 0x3400, 0x420) //AWB R GAIN
  675. || write_reg16(sensor->slv_addr, 0x3402, 0x3f0) //AWB G GAIN
  676. || write_reg16(sensor->slv_addr, 0x3404, 0x710);//AWB B GAIN
  677. break;
  678. default://AUTO
  679. break;
  680. }
  681. if (ret == 0) {
  682. ESP_LOGD(TAG, "Set wb_mode to: %d", mode);
  683. sensor->status.wb_mode = mode;
  684. }
  685. return ret;
  686. }
  687. static int set_awb_gain_dsp(sensor_t *sensor, int enable)
  688. {
  689. int ret = 0;
  690. int old_mode = sensor->status.wb_mode;
  691. int mode = enable?old_mode:0;
  692. ret = set_wb_mode(sensor, mode);
  693. if (ret == 0) {
  694. sensor->status.wb_mode = old_mode;
  695. ESP_LOGD(TAG, "Set awb_gain to: %d", enable);
  696. sensor->status.awb_gain = enable;
  697. }
  698. return ret;
  699. }
  700. static int set_special_effect(sensor_t *sensor, int effect)
  701. {
  702. int ret=0;
  703. if (effect < 0 || effect > 6) {
  704. return -1;
  705. }
  706. uint8_t * regs = (uint8_t *)sensor_special_effects[effect];
  707. ret = write_reg(sensor->slv_addr, 0x5580, regs[0])
  708. || write_reg(sensor->slv_addr, 0x5583, regs[1])
  709. || write_reg(sensor->slv_addr, 0x5584, regs[2])
  710. || write_reg(sensor->slv_addr, 0x5003, regs[3]);
  711. if (ret == 0) {
  712. ESP_LOGD(TAG, "Set special_effect to: %d", effect);
  713. sensor->status.special_effect = effect;
  714. }
  715. return ret;
  716. }
  717. static int set_brightness(sensor_t *sensor, int level)
  718. {
  719. int ret = 0;
  720. uint8_t value = 0;
  721. bool negative = false;
  722. switch (level) {
  723. case 3:
  724. value = 0x30;
  725. break;
  726. case 2:
  727. value = 0x20;
  728. break;
  729. case 1:
  730. value = 0x10;
  731. break;
  732. case -1:
  733. value = 0x10;
  734. negative = true;
  735. break;
  736. case -2:
  737. value = 0x20;
  738. negative = true;
  739. break;
  740. case -3:
  741. value = 0x30;
  742. negative = true;
  743. break;
  744. default: // 0
  745. break;
  746. }
  747. ret = write_reg(sensor->slv_addr, 0x5587, value);
  748. if (ret == 0) {
  749. ret = write_reg_bits(sensor->slv_addr, 0x5588, 0x08, negative);
  750. }
  751. if (ret == 0) {
  752. ESP_LOGD(TAG, "Set brightness to: %d", level);
  753. sensor->status.brightness = level;
  754. }
  755. return ret;
  756. }
  757. static int set_contrast(sensor_t *sensor, int level)
  758. {
  759. int ret = 0;
  760. if(level > 3 || level < -3) {
  761. return -1;
  762. }
  763. ret = write_reg(sensor->slv_addr, 0x5586, (level + 4) << 3);
  764. if (ret == 0) {
  765. ESP_LOGD(TAG, "Set contrast to: %d", level);
  766. sensor->status.contrast = level;
  767. }
  768. return ret;
  769. }
  770. static int set_saturation(sensor_t *sensor, int level)
  771. {
  772. int ret = 0;
  773. if(level > 4 || level < -4) {
  774. return -1;
  775. }
  776. uint8_t * regs = (uint8_t *)sensor_saturation_levels[level+4];
  777. for(int i=0; i<11; i++) {
  778. ret = write_reg(sensor->slv_addr, 0x5381 + i, regs[i]);
  779. if (ret) {
  780. break;
  781. }
  782. }
  783. if (ret == 0) {
  784. ESP_LOGD(TAG, "Set saturation to: %d", level);
  785. sensor->status.saturation = level;
  786. }
  787. return ret;
  788. }
  789. static int set_sharpness(sensor_t *sensor, int level)
  790. {
  791. int ret = 0;
  792. if(level > 3 || level < -3) {
  793. return -1;
  794. }
  795. uint8_t mt_offset_2 = (level + 3) * 8;
  796. uint8_t mt_offset_1 = mt_offset_2 + 1;
  797. ret = write_reg_bits(sensor->slv_addr, 0x5308, 0x40, false)//0x40 means auto
  798. || write_reg(sensor->slv_addr, 0x5300, 0x10)
  799. || write_reg(sensor->slv_addr, 0x5301, 0x10)
  800. || write_reg(sensor->slv_addr, 0x5302, mt_offset_1)
  801. || write_reg(sensor->slv_addr, 0x5303, mt_offset_2)
  802. || write_reg(sensor->slv_addr, 0x5309, 0x10)
  803. || write_reg(sensor->slv_addr, 0x530a, 0x10)
  804. || write_reg(sensor->slv_addr, 0x530b, 0x04)
  805. || write_reg(sensor->slv_addr, 0x530c, 0x06);
  806. if (ret == 0) {
  807. ESP_LOGD(TAG, "Set sharpness to: %d", level);
  808. sensor->status.sharpness = level;
  809. }
  810. return ret;
  811. }
  812. static int set_gainceiling(sensor_t *sensor, gainceiling_t level)
  813. {
  814. int ret = 0, l = (int)level;
  815. ret = write_reg(sensor->slv_addr, 0x3A18, (l >> 8) & 3)
  816. || write_reg(sensor->slv_addr, 0x3A19, l & 0xFF);
  817. if (ret == 0) {
  818. ESP_LOGD(TAG, "Set gainceiling to: %d", l);
  819. sensor->status.gainceiling = l;
  820. }
  821. return ret;
  822. }
  823. static int get_denoise(sensor_t *sensor)
  824. {
  825. if (!check_reg_mask(sensor->slv_addr, 0x5308, 0x10)) {
  826. return 0;
  827. }
  828. return (read_reg(sensor->slv_addr, 0x5306) / 4) + 1;
  829. }
  830. static int set_denoise(sensor_t *sensor, int level)
  831. {
  832. int ret = 0;
  833. if (level < 0 || level > 8) {
  834. return -1;
  835. }
  836. ret = write_reg_bits(sensor->slv_addr, 0x5308, 0x10, level > 0);
  837. if (ret == 0 && level > 0) {
  838. ret = write_reg(sensor->slv_addr, 0x5306, (level - 1) * 4);
  839. }
  840. if (ret == 0) {
  841. ESP_LOGD(TAG, "Set denoise to: %d", level);
  842. sensor->status.denoise = level;
  843. }
  844. return ret;
  845. }
  846. static int get_reg(sensor_t *sensor, int reg, int mask)
  847. {
  848. int ret = 0, ret2 = 0;
  849. if(mask > 0xFF){
  850. ret = read_reg16(sensor->slv_addr, reg);
  851. if(ret >= 0 && mask > 0xFFFF){
  852. ret2 = read_reg(sensor->slv_addr, reg+2);
  853. if(ret2 >= 0){
  854. ret = (ret << 8) | ret2 ;
  855. } else {
  856. ret = ret2;
  857. }
  858. }
  859. } else {
  860. ret = read_reg(sensor->slv_addr, reg);
  861. }
  862. if(ret > 0){
  863. ret &= mask;
  864. }
  865. return ret;
  866. }
  867. static int set_reg(sensor_t *sensor, int reg, int mask, int value)
  868. {
  869. int ret = 0, ret2 = 0;
  870. if(mask > 0xFF){
  871. ret = read_reg16(sensor->slv_addr, reg);
  872. if(ret >= 0 && mask > 0xFFFF){
  873. ret2 = read_reg(sensor->slv_addr, reg+2);
  874. if(ret2 >= 0){
  875. ret = (ret << 8) | ret2 ;
  876. } else {
  877. ret = ret2;
  878. }
  879. }
  880. } else {
  881. ret = read_reg(sensor->slv_addr, reg);
  882. }
  883. if(ret < 0){
  884. return ret;
  885. }
  886. value = (ret & ~mask) | (value & mask);
  887. if(mask > 0xFFFF){
  888. ret = write_reg16(sensor->slv_addr, reg, value >> 8);
  889. if(ret >= 0){
  890. ret = write_reg(sensor->slv_addr, reg+2, value & 0xFF);
  891. }
  892. } else if(mask > 0xFF){
  893. ret = write_reg16(sensor->slv_addr, reg, value);
  894. } else {
  895. ret = write_reg(sensor->slv_addr, reg, value);
  896. }
  897. return ret;
  898. }
  899. static int set_res_raw(sensor_t *sensor, int startX, int startY, int endX, int endY, int offsetX, int offsetY, int totalX, int totalY, int outputX, int outputY, bool scale, bool binning)
  900. {
  901. int ret = 0;
  902. ret = write_addr_reg(sensor->slv_addr, X_ADDR_ST_H, startX, startY)
  903. || write_addr_reg(sensor->slv_addr, X_ADDR_END_H, endX, endY)
  904. || write_addr_reg(sensor->slv_addr, X_OFFSET_H, offsetX, offsetY)
  905. || write_addr_reg(sensor->slv_addr, X_TOTAL_SIZE_H, totalX, totalY)
  906. || write_addr_reg(sensor->slv_addr, X_OUTPUT_SIZE_H, outputX, outputY)
  907. || write_reg_bits(sensor->slv_addr, ISP_CONTROL_01, 0x20, scale);
  908. if(!ret){
  909. sensor->status.scale = scale;
  910. sensor->status.binning = binning;
  911. ret = set_image_options(sensor);
  912. }
  913. return ret;
  914. }
  915. static int _set_pll(sensor_t *sensor, int bypass, int multiplier, int sys_div, int root_2x, int pre_div, int seld5, int pclk_manual, int pclk_div)
  916. {
  917. int ret = 0;
  918. ret = set_pll(sensor, bypass > 0, multiplier, sys_div, pre_div, root_2x > 0, seld5, pclk_manual > 0, pclk_div);
  919. return ret;
  920. }
  921. static int set_xclk(sensor_t *sensor, int timer, int xclk)
  922. {
  923. int ret = 0;
  924. sensor->xclk_freq_hz = xclk * 1000000U;
  925. ret = xclk_timer_conf(timer, sensor->xclk_freq_hz);
  926. return ret;
  927. }
  928. static int init_status(sensor_t *sensor)
  929. {
  930. sensor->status.brightness = 0;
  931. sensor->status.contrast = 0;
  932. sensor->status.saturation = 0;
  933. sensor->status.sharpness = (read_reg(sensor->slv_addr, 0x5303) / 8) - 3;
  934. sensor->status.denoise = get_denoise(sensor);
  935. sensor->status.ae_level = 0;
  936. sensor->status.gainceiling = read_reg16(sensor->slv_addr, 0x3A18) & 0x3FF;
  937. sensor->status.awb = check_reg_mask(sensor->slv_addr, ISP_CONTROL_01, 0x01);
  938. sensor->status.dcw = !check_reg_mask(sensor->slv_addr, 0x5183, 0x80);
  939. sensor->status.agc = !check_reg_mask(sensor->slv_addr, AEC_PK_MANUAL, AEC_PK_MANUAL_AGC_MANUALEN);
  940. sensor->status.aec = !check_reg_mask(sensor->slv_addr, AEC_PK_MANUAL, AEC_PK_MANUAL_AEC_MANUALEN);
  941. sensor->status.hmirror = check_reg_mask(sensor->slv_addr, TIMING_TC_REG21, TIMING_TC_REG21_HMIRROR);
  942. sensor->status.vflip = check_reg_mask(sensor->slv_addr, TIMING_TC_REG20, TIMING_TC_REG20_VFLIP);
  943. sensor->status.colorbar = check_reg_mask(sensor->slv_addr, PRE_ISP_TEST_SETTING_1, TEST_COLOR_BAR);
  944. sensor->status.bpc = check_reg_mask(sensor->slv_addr, 0x5000, 0x04);
  945. sensor->status.wpc = check_reg_mask(sensor->slv_addr, 0x5000, 0x02);
  946. sensor->status.raw_gma = check_reg_mask(sensor->slv_addr, 0x5000, 0x20);
  947. sensor->status.lenc = check_reg_mask(sensor->slv_addr, 0x5000, 0x80);
  948. sensor->status.quality = read_reg(sensor->slv_addr, COMPRESSION_CTRL07) & 0x3f;
  949. sensor->status.special_effect = 0;
  950. sensor->status.wb_mode = 0;
  951. sensor->status.awb_gain = check_reg_mask(sensor->slv_addr, 0x3406, 0x01);
  952. sensor->status.agc_gain = get_agc_gain(sensor);
  953. sensor->status.aec_value = get_aec_value(sensor);
  954. sensor->status.aec2 = check_reg_mask(sensor->slv_addr, 0x3a00, 0x04);
  955. return 0;
  956. }
  957. int ov5640_detect(int slv_addr, sensor_id_t *id)
  958. {
  959. if (OV5640_SCCB_ADDR == slv_addr) {
  960. uint8_t h = SCCB_Read16(slv_addr, 0x300A);
  961. uint8_t l = SCCB_Read16(slv_addr, 0x300B);
  962. uint16_t PID = (h<<8) | l;
  963. if (OV5640_PID == PID) {
  964. id->PID = PID;
  965. return PID;
  966. } else {
  967. ESP_LOGI(TAG, "Mismatch PID=0x%x", PID);
  968. }
  969. }
  970. return 0;
  971. }
  972. int ov5640_init(sensor_t *sensor)
  973. {
  974. sensor->reset = reset;
  975. sensor->set_pixformat = set_pixformat;
  976. sensor->set_framesize = set_framesize;
  977. sensor->set_contrast = set_contrast;
  978. sensor->set_brightness = set_brightness;
  979. sensor->set_saturation = set_saturation;
  980. sensor->set_sharpness = set_sharpness;
  981. sensor->set_gainceiling = set_gainceiling;
  982. sensor->set_quality = set_quality;
  983. sensor->set_colorbar = set_colorbar;
  984. sensor->set_gain_ctrl = set_gain_ctrl;
  985. sensor->set_exposure_ctrl = set_exposure_ctrl;
  986. sensor->set_whitebal = set_whitebal;
  987. sensor->set_hmirror = set_hmirror;
  988. sensor->set_vflip = set_vflip;
  989. sensor->init_status = init_status;
  990. sensor->set_aec2 = set_aec2;
  991. sensor->set_aec_value = set_aec_value;
  992. sensor->set_special_effect = set_special_effect;
  993. sensor->set_wb_mode = set_wb_mode;
  994. sensor->set_ae_level = set_ae_level;
  995. sensor->set_dcw = set_dcw_dsp;
  996. sensor->set_bpc = set_bpc_dsp;
  997. sensor->set_wpc = set_wpc_dsp;
  998. sensor->set_awb_gain = set_awb_gain_dsp;
  999. sensor->set_agc_gain = set_agc_gain;
  1000. sensor->set_raw_gma = set_raw_gma_dsp;
  1001. sensor->set_lenc = set_lenc_dsp;
  1002. sensor->set_denoise = set_denoise;
  1003. sensor->get_reg = get_reg;
  1004. sensor->set_reg = set_reg;
  1005. sensor->set_res_raw = set_res_raw;
  1006. sensor->set_pll = _set_pll;
  1007. sensor->set_xclk = set_xclk;
  1008. return 0;
  1009. }