nt99141.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  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. * NT99141 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 "nt99141.h"
  15. #include "nt99141_regs.h"
  16. #include "nt99141_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 = "NT99141";
  24. #endif
  25. //#define REG_DEBUG_ON
  26. static int read_reg(uint8_t slv_addr, const uint16_t reg)
  27. {
  28. int ret = SCCB_Read16(slv_addr, reg);
  29. #ifdef REG_DEBUG_ON
  30. if (ret < 0) {
  31. ESP_LOGE(TAG, "READ REG 0x%04x FAILED: %d", reg, ret);
  32. }
  33. #endif
  34. return ret;
  35. }
  36. static int check_reg_mask(uint8_t slv_addr, uint16_t reg, uint8_t mask)
  37. {
  38. return (read_reg(slv_addr, reg) & mask) == mask;
  39. }
  40. static int read_reg16(uint8_t slv_addr, const uint16_t reg)
  41. {
  42. int ret = 0, ret2 = 0;
  43. ret = read_reg(slv_addr, reg);
  44. if (ret >= 0) {
  45. ret = (ret & 0xFF) << 8;
  46. ret2 = read_reg(slv_addr, reg + 1);
  47. if (ret2 < 0) {
  48. ret = ret2;
  49. } else {
  50. ret |= ret2 & 0xFF;
  51. }
  52. }
  53. return ret;
  54. }
  55. static int write_reg(uint8_t slv_addr, const uint16_t reg, uint8_t value)
  56. {
  57. int ret = 0;
  58. #ifndef REG_DEBUG_ON
  59. ret = SCCB_Write16(slv_addr, reg, value);
  60. #else
  61. int old_value = read_reg(slv_addr, reg);
  62. if (old_value < 0) {
  63. return old_value;
  64. }
  65. if ((uint8_t)old_value != value) {
  66. ESP_LOGD(TAG, "NEW REG 0x%04x: 0x%02x to 0x%02x", reg, (uint8_t)old_value, value);
  67. ret = SCCB_Write16(slv_addr, reg, value);
  68. } else {
  69. ESP_LOGD(TAG, "OLD REG 0x%04x: 0x%02x", reg, (uint8_t)old_value);
  70. ret = SCCB_Write16(slv_addr, reg, value);//maybe not?
  71. }
  72. if (ret < 0) {
  73. ESP_LOGE(TAG, "WRITE REG 0x%04x FAILED: %d", reg, ret);
  74. }
  75. #endif
  76. return ret;
  77. }
  78. static int set_reg_bits(uint8_t slv_addr, uint16_t reg, uint8_t offset, uint8_t mask, uint8_t value)
  79. {
  80. int ret = 0;
  81. uint8_t c_value, new_value;
  82. ret = read_reg(slv_addr, reg);
  83. if (ret < 0) {
  84. return ret;
  85. }
  86. c_value = ret;
  87. new_value = (c_value & ~(mask << offset)) | ((value & mask) << offset);
  88. ret = write_reg(slv_addr, reg, new_value);
  89. return ret;
  90. }
  91. static int write_regs(uint8_t slv_addr, const uint16_t (*regs)[2])
  92. {
  93. int i = 0, ret = 0;
  94. while (!ret && regs[i][0] != REGLIST_TAIL) {
  95. if (regs[i][0] == REG_DLY) {
  96. vTaskDelay(regs[i][1] / portTICK_PERIOD_MS);
  97. } else {
  98. ret = write_reg(slv_addr, regs[i][0], regs[i][1]);
  99. }
  100. i++;
  101. }
  102. return ret;
  103. }
  104. static int write_reg16(uint8_t slv_addr, const uint16_t reg, uint16_t value)
  105. {
  106. if (write_reg(slv_addr, reg, value >> 8) || write_reg(slv_addr, reg + 1, value)) {
  107. return -1;
  108. }
  109. return 0;
  110. }
  111. static int write_addr_reg(uint8_t slv_addr, const uint16_t reg, uint16_t x_value, uint16_t y_value)
  112. {
  113. if (write_reg16(slv_addr, reg, x_value) || write_reg16(slv_addr, reg + 2, y_value)) {
  114. return -1;
  115. }
  116. return 0;
  117. }
  118. #define write_reg_bits(slv_addr, reg, mask, enable) set_reg_bits(slv_addr, reg, 0, mask, enable?mask:0)
  119. 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 seld5, bool pclk_manual, uint8_t pclk_div)
  120. {
  121. return -1;
  122. }
  123. static int set_ae_level(sensor_t *sensor, int level);
  124. static int reset(sensor_t *sensor)
  125. {
  126. int ret = 0;
  127. // Software Reset: clear all registers and reset them to their default values
  128. ret = write_reg(sensor->slv_addr, SYSTEM_CTROL0, 0x01);
  129. if (ret) {
  130. ESP_LOGE(TAG, "Software Reset FAILED!");
  131. return ret;
  132. }
  133. vTaskDelay(100 / portTICK_PERIOD_MS);
  134. ret = write_regs(sensor->slv_addr, sensor_default_regs); //re-initial
  135. if (ret == 0) {
  136. ESP_LOGD(TAG, "Camera defaults loaded");
  137. ret = set_ae_level(sensor, 0);
  138. vTaskDelay(100 / portTICK_PERIOD_MS);
  139. }
  140. return ret;
  141. }
  142. static int set_pixformat(sensor_t *sensor, pixformat_t pixformat)
  143. {
  144. int ret = 0;
  145. const uint16_t (*regs)[2];
  146. switch (pixformat) {
  147. case PIXFORMAT_YUV422:
  148. regs = sensor_fmt_yuv422;
  149. break;
  150. case PIXFORMAT_GRAYSCALE:
  151. regs = sensor_fmt_grayscale;
  152. break;
  153. case PIXFORMAT_RGB565:
  154. case PIXFORMAT_RGB888:
  155. regs = sensor_fmt_rgb565;
  156. break;
  157. case PIXFORMAT_JPEG:
  158. regs = sensor_fmt_jpeg;
  159. break;
  160. case PIXFORMAT_RAW:
  161. regs = sensor_fmt_raw;
  162. break;
  163. default:
  164. ESP_LOGE(TAG, "Unsupported pixformat: %u", pixformat);
  165. return -1;
  166. }
  167. ret = write_regs(sensor->slv_addr, regs);
  168. if (ret == 0) {
  169. sensor->pixformat = pixformat;
  170. ESP_LOGD(TAG, "Set pixformat to: %u", pixformat);
  171. }
  172. return ret;
  173. }
  174. static int set_image_options(sensor_t *sensor)
  175. {
  176. int ret = 0;
  177. uint8_t reg20 = 0;
  178. uint8_t reg21 = 0;
  179. uint8_t reg4514 = 0;
  180. uint8_t reg4514_test = 0;
  181. // V-Flip
  182. if (sensor->status.vflip) {
  183. reg20 |= 0x01;
  184. reg4514_test |= 1;
  185. }
  186. // H-Mirror
  187. if (sensor->status.hmirror) {
  188. reg21 |= 0x02;
  189. reg4514_test |= 2;
  190. }
  191. switch (reg4514_test) {
  192. }
  193. if (write_reg(sensor->slv_addr, TIMING_TC_REG20, reg20 | reg21)) {
  194. ESP_LOGE(TAG, "Setting Image Options Failed");
  195. ret = -1;
  196. }
  197. ESP_LOGD(TAG, "Set Image Options: Compression: %u, Binning: %u, V-Flip: %u, H-Mirror: %u, Reg-4514: 0x%02x",
  198. sensor->pixformat == PIXFORMAT_JPEG, sensor->status.binning, sensor->status.vflip, sensor->status.hmirror, reg4514);
  199. return ret;
  200. }
  201. static int set_framesize(sensor_t *sensor, framesize_t framesize)
  202. {
  203. int ret = 0;
  204. sensor->status.framesize = framesize;
  205. ret = write_regs(sensor->slv_addr, sensor_default_regs);
  206. if (framesize == FRAMESIZE_QVGA) {
  207. ESP_LOGD(TAG, "Set FRAMESIZE_QVGA");
  208. ret = write_regs(sensor->slv_addr, sensor_framesize_QVGA);
  209. #if CONFIG_NT99141_SUPPORT_XSKIP
  210. ESP_LOGD(TAG, "Set FRAMESIZE_QVGA: xskip mode");
  211. ret = write_regs(sensor->slv_addr, sensor_framesize_QVGA_xskip);
  212. #elif CONFIG_NT99141_SUPPORT_CROP
  213. ESP_LOGD(TAG, "Set FRAMESIZE_QVGA: crop mode");
  214. ret = write_regs(sensor->slv_addr, sensor_framesize_QVGA_crop);
  215. #endif
  216. } else if (framesize == FRAMESIZE_VGA) {
  217. ESP_LOGD(TAG, "Set FRAMESIZE_VGA");
  218. // ret = write_regs(sensor->slv_addr, sensor_framesize_VGA);
  219. ret = write_regs(sensor->slv_addr, sensor_framesize_VGA_xyskip);// Resolution:640*360 This configuration is equally-scaled without deforming
  220. #ifdef CONFIG_NT99141_SUPPORT_XSKIP
  221. ESP_LOGD(TAG, "Set FRAMESIZE_QVGA: xskip mode");
  222. ret = write_regs(sensor->slv_addr, sensor_framesize_VGA_xskip);
  223. #elif CONFIG_NT99141_SUPPORT_CROP
  224. ESP_LOGD(TAG, "Set FRAMESIZE_QVGA: crop mode");
  225. ret = write_regs(sensor->slv_addr, sensor_framesize_VGA_crop);
  226. #endif
  227. } else if (framesize >= FRAMESIZE_HD) {
  228. ESP_LOGD(TAG, "Set FRAMESIZE_HD");
  229. ret = write_regs(sensor->slv_addr, sensor_framesize_HD);
  230. } else {
  231. ESP_LOGD(TAG, "Dont suppost this size, Set FRAMESIZE_VGA");
  232. ret = write_regs(sensor->slv_addr, sensor_framesize_VGA);
  233. }
  234. return ret;
  235. }
  236. static int set_hmirror(sensor_t *sensor, int enable)
  237. {
  238. int ret = 0;
  239. sensor->status.hmirror = enable;
  240. ret = set_image_options(sensor);
  241. if (ret == 0) {
  242. ESP_LOGD(TAG, "Set h-mirror to: %d", enable);
  243. }
  244. return ret;
  245. }
  246. static int set_vflip(sensor_t *sensor, int enable)
  247. {
  248. int ret = 0;
  249. sensor->status.vflip = enable;
  250. ret = set_image_options(sensor);
  251. if (ret == 0) {
  252. ESP_LOGD(TAG, "Set v-flip to: %d", enable);
  253. }
  254. return ret;
  255. }
  256. static int set_quality(sensor_t *sensor, int qs)
  257. {
  258. int ret = 0;
  259. ret = write_reg(sensor->slv_addr, COMPRESSION_CTRL07, qs & 0x3f);
  260. if (ret == 0) {
  261. sensor->status.quality = qs;
  262. ESP_LOGD(TAG, "Set quality to: %d", qs);
  263. }
  264. return ret;
  265. }
  266. static int set_colorbar(sensor_t *sensor, int enable)
  267. {
  268. int ret = 0;
  269. ret = write_reg_bits(sensor->slv_addr, PRE_ISP_TEST_SETTING_1, TEST_COLOR_BAR, enable);
  270. if (ret == 0) {
  271. sensor->status.colorbar = enable;
  272. ESP_LOGD(TAG, "Set colorbar to: %d", enable);
  273. }
  274. return ret;
  275. }
  276. static int set_gain_ctrl(sensor_t *sensor, int enable)
  277. {
  278. int ret = 0;
  279. ret = write_reg_bits(sensor->slv_addr, 0x32bb, 0x87, enable);
  280. if (ret == 0) {
  281. ESP_LOGD(TAG, "Set gain_ctrl to: %d", enable);
  282. sensor->status.agc = enable;
  283. }
  284. return ret;
  285. }
  286. static int set_exposure_ctrl(sensor_t *sensor, int enable)
  287. {
  288. int ret = 0;
  289. int data = 0;
  290. // ret = write_reg_bits(sensor->slv_addr, 0x32bb, 0x87, enable);
  291. data = read_reg(sensor->slv_addr, 0x3201);
  292. ESP_LOGD(TAG, "set_exposure_ctrl:enable");
  293. if (enable) {
  294. ESP_LOGD(TAG, "set_exposure_ctrl:enable");
  295. ret = write_reg(sensor->slv_addr, 0x3201, (1 << 5) | data);
  296. } else {
  297. ESP_LOGD(TAG, "set_exposure_ctrl:disable");
  298. ret = write_reg(sensor->slv_addr, 0x3201, (~(1 << 5)) & data);
  299. }
  300. if (ret == 0) {
  301. ESP_LOGD(TAG, "Set exposure_ctrl to: %d", enable);
  302. sensor->status.aec = enable;
  303. }
  304. return ret;
  305. }
  306. static int set_whitebal(sensor_t *sensor, int enable)
  307. {
  308. int ret = 0;
  309. if (ret == 0) {
  310. ESP_LOGD(TAG, "Set awb to: %d", enable);
  311. sensor->status.awb = enable;
  312. }
  313. return ret;
  314. }
  315. //Advanced AWB
  316. static int set_dcw_dsp(sensor_t *sensor, int enable)
  317. {
  318. int ret = 0;
  319. if (ret == 0) {
  320. ESP_LOGD(TAG, "Set dcw to: %d", enable);
  321. sensor->status.dcw = enable;
  322. }
  323. return ret;
  324. }
  325. //night mode enable
  326. static int set_aec2(sensor_t *sensor, int enable)
  327. {
  328. int ret = 0;
  329. if (ret == 0) {
  330. ESP_LOGD(TAG, "Set aec2 to: %d", enable);
  331. sensor->status.aec2 = enable;
  332. }
  333. return ret;
  334. }
  335. static int set_bpc_dsp(sensor_t *sensor, int enable)
  336. {
  337. int ret = 0;
  338. if (ret == 0) {
  339. ESP_LOGD(TAG, "Set bpc to: %d", enable);
  340. sensor->status.bpc = enable;
  341. }
  342. return ret;
  343. }
  344. static int set_wpc_dsp(sensor_t *sensor, int enable)
  345. {
  346. int ret = 0;
  347. if (ret == 0) {
  348. ESP_LOGD(TAG, "Set wpc to: %d", enable);
  349. sensor->status.wpc = enable;
  350. }
  351. return ret;
  352. }
  353. //Gamma enable
  354. static int set_raw_gma_dsp(sensor_t *sensor, int enable)
  355. {
  356. int ret = 0;
  357. if (ret == 0) {
  358. ESP_LOGD(TAG, "Set raw_gma to: %d", enable);
  359. sensor->status.raw_gma = enable;
  360. }
  361. return ret;
  362. }
  363. static int set_lenc_dsp(sensor_t *sensor, int enable)
  364. {
  365. int ret = 0;
  366. if (ret == 0) {
  367. ESP_LOGD(TAG, "Set lenc to: %d", enable);
  368. sensor->status.lenc = enable;
  369. }
  370. return ret;
  371. }
  372. static int get_agc_gain(sensor_t *sensor)
  373. {
  374. ESP_LOGD(TAG, "get_agc_gain can not be configured at present");
  375. return 0;
  376. }
  377. //real gain
  378. static int set_agc_gain(sensor_t *sensor, int gain)
  379. {
  380. ESP_LOGD(TAG, "set_agc_gain can not be configured at present");
  381. // ESP_LOGD(TAG, "GAIN = %d\n", gain);
  382. int cnt = gain / 2;
  383. switch (cnt) {
  384. case 0:
  385. ESP_LOGD(TAG, "set_agc_gain: 1x");
  386. write_reg(sensor->slv_addr, 0X301D, 0X00);
  387. break;
  388. case 1:
  389. ESP_LOGD(TAG,"set_agc_gain: 2x");
  390. write_reg(sensor->slv_addr, 0X301D, 0X0F);
  391. break;
  392. case 2:
  393. ESP_LOGD(TAG,"set_agc_gain: 4x");
  394. write_reg(sensor->slv_addr, 0X301D, 0X2F);
  395. break;
  396. case 3:
  397. ESP_LOGD(TAG,"set_agc_gain: 6x");
  398. write_reg(sensor->slv_addr, 0X301D, 0X37);
  399. break;
  400. case 4:
  401. ESP_LOGD(TAG,"set_agc_gain: 8x");
  402. write_reg(sensor->slv_addr, 0X301D, 0X3F);
  403. break;
  404. default:
  405. ESP_LOGD(TAG,"fail set_agc_gain");
  406. break;
  407. }
  408. return 0;
  409. }
  410. static int get_aec_value(sensor_t *sensor)
  411. {
  412. ESP_LOGD(TAG, "get_aec_value can not be configured at present");
  413. return 0;
  414. }
  415. static int set_aec_value(sensor_t *sensor, int value)
  416. {
  417. ESP_LOGD(TAG, "set_aec_value can not be configured at present");
  418. int ret = 0;
  419. // ESP_LOGD(TAG, " set_aec_value to: %d", value);
  420. ret = write_reg_bits(sensor->slv_addr, 0x3012, 0x00, (value >> 8) & 0xff);
  421. ret = write_reg_bits(sensor->slv_addr, 0x3013, 0x01, value & 0xff);
  422. if (ret == 0) {
  423. ESP_LOGD(TAG, " set_aec_value to: %d", value);
  424. // sensor->status.aec = enable;
  425. }
  426. return ret;
  427. }
  428. static int set_ae_level(sensor_t *sensor, int level)
  429. {
  430. ESP_LOGD(TAG, "set_ae_level can not be configured at present");
  431. int ret = 0;
  432. if (level < 0) {
  433. level = 0;
  434. } else if (level > 9) {
  435. level = 9;
  436. }
  437. for (int i = 0; i < 5; i++) {
  438. ret += write_reg(sensor->slv_addr, sensor_ae_level[ 5 * level + i ][0], sensor_ae_level[5 * level + i ][1]);
  439. }
  440. if (ret) {
  441. ESP_LOGE(TAG, " fail to set ae level: %d", ret);
  442. }
  443. return 0;
  444. }
  445. static int set_wb_mode(sensor_t *sensor, int mode)
  446. {
  447. int ret = 0;
  448. if (mode < 0 || mode > 4) {
  449. return -1;
  450. }
  451. ret = write_reg(sensor->slv_addr, 0x3201, (mode != 0));
  452. if (ret) {
  453. return ret;
  454. }
  455. switch (mode) {
  456. case 1://Sunny
  457. ret = write_reg16(sensor->slv_addr, 0x3290, 0x01)
  458. || write_reg16(sensor->slv_addr, 0x3291, 0x38)
  459. || write_reg16(sensor->slv_addr, 0x3296, 0x01)
  460. || write_reg16(sensor->slv_addr, 0x3297, 0x68)
  461. || write_reg16(sensor->slv_addr, 0x3060, 0x01);
  462. break;
  463. case 2://Cloudy
  464. ret = write_reg16(sensor->slv_addr, 0x3290, 0x01)
  465. || write_reg16(sensor->slv_addr, 0x3291, 0x51)
  466. || write_reg16(sensor->slv_addr, 0x3296, 0x01)
  467. || write_reg16(sensor->slv_addr, 0x3297, 0x00)
  468. || write_reg16(sensor->slv_addr, 0x3060, 0x01);
  469. break;
  470. case 3://INCANDESCENCE]
  471. ret = write_reg16(sensor->slv_addr, 0x3290, 0x01)
  472. || write_reg16(sensor->slv_addr, 0x3291, 0x30)
  473. || write_reg16(sensor->slv_addr, 0x3296, 0x01)
  474. || write_reg16(sensor->slv_addr, 0x3297, 0xCB)
  475. || write_reg16(sensor->slv_addr, 0x3060, 0x01);
  476. break;
  477. case 4://FLUORESCENT
  478. ret = write_reg16(sensor->slv_addr, 0x3290, 0x01)
  479. || write_reg16(sensor->slv_addr, 0x3291, 0x70)
  480. || write_reg16(sensor->slv_addr, 0x3296, 0x01)
  481. || write_reg16(sensor->slv_addr, 0x3297, 0xFF)
  482. || write_reg16(sensor->slv_addr, 0x3060, 0x01);
  483. break;
  484. default://AUTO
  485. break;
  486. }
  487. if (ret == 0) {
  488. ESP_LOGD(TAG, "Set wb_mode to: %d", mode);
  489. sensor->status.wb_mode = mode;
  490. }
  491. return ret;
  492. }
  493. static int set_awb_gain_dsp(sensor_t *sensor, int enable)
  494. {
  495. int ret = 0;
  496. int old_mode = sensor->status.wb_mode;
  497. int mode = enable ? old_mode : 0;
  498. ret = set_wb_mode(sensor, mode);
  499. if (ret == 0) {
  500. sensor->status.wb_mode = old_mode;
  501. ESP_LOGD(TAG, "Set awb_gain to: %d", enable);
  502. sensor->status.awb_gain = enable;
  503. }
  504. return ret;
  505. }
  506. static int set_special_effect(sensor_t *sensor, int effect)
  507. {
  508. int ret = 0;
  509. if (effect < 0 || effect > 6) {
  510. return -1;
  511. }
  512. uint8_t *regs = (uint8_t *)sensor_special_effects[effect];
  513. ret = write_reg(sensor->slv_addr, 0x32F1, regs[0])
  514. || write_reg(sensor->slv_addr, 0x32F4, regs[1])
  515. || write_reg(sensor->slv_addr, 0x32F5, regs[2])
  516. || write_reg(sensor->slv_addr, 0x3060, regs[3]);
  517. if (ret == 0) {
  518. ESP_LOGD(TAG, "Set special_effect to: %d", effect);
  519. sensor->status.special_effect = effect;
  520. }
  521. return ret;
  522. }
  523. static int set_brightness(sensor_t *sensor, int level)
  524. {
  525. int ret = 0;
  526. uint8_t value = 0;
  527. switch (level) {
  528. case 3:
  529. value = 0xA0;
  530. break;
  531. case 2:
  532. value = 0x90;
  533. break;
  534. case 1:
  535. value = 0x88;
  536. break;
  537. case -1:
  538. value = 0x78;
  539. break;
  540. case -2:
  541. value = 0x70;
  542. break;
  543. case -3:
  544. value = 0x60;
  545. break;
  546. default: // 0
  547. break;
  548. }
  549. ret = write_reg(sensor->slv_addr, 0x32F2, value);
  550. if (ret == 0) {
  551. ESP_LOGD(TAG, "Set brightness to: %d", level);
  552. sensor->status.brightness = level;
  553. }
  554. return ret;
  555. }
  556. static int set_contrast(sensor_t *sensor, int level)
  557. {
  558. int ret = 0;
  559. uint8_t value1 = 0, value2 = 0 ;
  560. switch (level) {
  561. case 3:
  562. value1 = 0xD0;
  563. value2 = 0xB0;
  564. break;
  565. case 2:
  566. value1 = 0xE0;
  567. value2 = 0xA0;
  568. break;
  569. case 1:
  570. value1 = 0xF0;
  571. value2 = 0x90;
  572. break;
  573. case 0:
  574. value1 = 0x00;
  575. value2 = 0x80;
  576. break;
  577. case -1:
  578. value1 = 0x10;
  579. value2 = 0x70;
  580. break;
  581. case -2:
  582. value1 = 0x20;
  583. value2 = 0x60;
  584. break;
  585. case -3:
  586. value1 = 0x30;
  587. value2 = 0x50;
  588. break;
  589. default: // 0
  590. break;
  591. }
  592. ret = write_reg(sensor->slv_addr, 0x32FC, value1);
  593. ret = write_reg(sensor->slv_addr, 0x32F2, value2);
  594. ret = write_reg(sensor->slv_addr, 0x3060, 0x01);
  595. if (ret == 0) {
  596. ESP_LOGD(TAG, "Set contrast to: %d", level);
  597. sensor->status.contrast = level;
  598. }
  599. return ret;
  600. }
  601. static int set_saturation(sensor_t *sensor, int level)
  602. {
  603. int ret = 0;
  604. if (level > 4 || level < -4) {
  605. return -1;
  606. }
  607. uint8_t *regs = (uint8_t *)sensor_saturation_levels[level + 4];
  608. {
  609. ret = write_reg(sensor->slv_addr, 0x32F3, regs[0]);
  610. if (ret) {
  611. return ret;
  612. }
  613. }
  614. if (ret == 0) {
  615. ESP_LOGD(TAG, "Set saturation to: %d", level);
  616. sensor->status.saturation = level;
  617. }
  618. return ret;
  619. }
  620. static int set_sharpness(sensor_t *sensor, int level)
  621. {
  622. int ret = 0;
  623. if (level > 3 || level < -3) {
  624. return -1;
  625. }
  626. uint8_t mt_offset_2 = (level + 3) * 8;
  627. uint8_t mt_offset_1 = mt_offset_2 + 1;
  628. ret = write_reg_bits(sensor->slv_addr, 0x5308, 0x40, false)//0x40 means auto
  629. || write_reg(sensor->slv_addr, 0x5300, 0x10)
  630. || write_reg(sensor->slv_addr, 0x5301, 0x10)
  631. || write_reg(sensor->slv_addr, 0x5302, mt_offset_1)
  632. || write_reg(sensor->slv_addr, 0x5303, mt_offset_2)
  633. || write_reg(sensor->slv_addr, 0x5309, 0x10)
  634. || write_reg(sensor->slv_addr, 0x530a, 0x10)
  635. || write_reg(sensor->slv_addr, 0x530b, 0x04)
  636. || write_reg(sensor->slv_addr, 0x530c, 0x06);
  637. if (ret == 0) {
  638. ESP_LOGD(TAG, "Set sharpness to: %d", level);
  639. sensor->status.sharpness = level;
  640. }
  641. return ret;
  642. }
  643. static int set_gainceiling(sensor_t *sensor, gainceiling_t level)
  644. {
  645. ESP_LOGD(TAG, "set_gainceiling can not be configured at present");
  646. return 0;
  647. }
  648. static int get_denoise(sensor_t *sensor)
  649. {
  650. return (read_reg(sensor->slv_addr, 0x5306) / 4) + 1;
  651. }
  652. static int set_denoise(sensor_t *sensor, int level)
  653. {
  654. ESP_LOGD(TAG, "set_denoise can not be configured at present");
  655. return 0;
  656. }
  657. static int get_reg(sensor_t *sensor, int reg, int mask)
  658. {
  659. int ret = 0, ret2 = 0;
  660. if (mask > 0xFF) {
  661. ret = read_reg16(sensor->slv_addr, reg);
  662. if (ret >= 0 && mask > 0xFFFF) {
  663. ret2 = read_reg(sensor->slv_addr, reg + 2);
  664. if (ret2 >= 0) {
  665. ret = (ret << 8) | ret2 ;
  666. } else {
  667. ret = ret2;
  668. }
  669. }
  670. } else {
  671. ret = read_reg(sensor->slv_addr, reg);
  672. }
  673. if (ret > 0) {
  674. ret &= mask;
  675. }
  676. return ret;
  677. }
  678. static int set_reg(sensor_t *sensor, int reg, int mask, int value)
  679. {
  680. int ret = 0, ret2 = 0;
  681. if (mask > 0xFF) {
  682. ret = read_reg16(sensor->slv_addr, reg);
  683. if (ret >= 0 && mask > 0xFFFF) {
  684. ret2 = read_reg(sensor->slv_addr, reg + 2);
  685. if (ret2 >= 0) {
  686. ret = (ret << 8) | ret2 ;
  687. } else {
  688. ret = ret2;
  689. }
  690. }
  691. } else {
  692. ret = read_reg(sensor->slv_addr, reg);
  693. }
  694. if (ret < 0) {
  695. return ret;
  696. }
  697. value = (ret & ~mask) | (value & mask);
  698. if (mask > 0xFFFF) {
  699. ret = write_reg16(sensor->slv_addr, reg, value >> 8);
  700. if (ret >= 0) {
  701. ret = write_reg(sensor->slv_addr, reg + 2, value & 0xFF);
  702. }
  703. } else if (mask > 0xFF) {
  704. ret = write_reg16(sensor->slv_addr, reg, value);
  705. } else {
  706. ret = write_reg(sensor->slv_addr, reg, value);
  707. }
  708. return ret;
  709. }
  710. 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)
  711. {
  712. int ret = 0;
  713. ret = write_addr_reg(sensor->slv_addr, X_ADDR_ST_H, startX, startY)
  714. || write_addr_reg(sensor->slv_addr, X_ADDR_END_H, endX, endY)
  715. || write_addr_reg(sensor->slv_addr, X_OFFSET_H, offsetX, offsetY)
  716. || write_addr_reg(sensor->slv_addr, X_TOTAL_SIZE_H, totalX, totalY)
  717. || write_addr_reg(sensor->slv_addr, X_OUTPUT_SIZE_H, outputX, outputY);
  718. if (!ret) {
  719. sensor->status.scale = scale;
  720. sensor->status.binning = binning;
  721. ret = set_image_options(sensor);
  722. }
  723. return ret;
  724. }
  725. 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)
  726. {
  727. return set_pll(sensor, bypass > 0, multiplier, sys_div, pre_div, root_2x > 0, seld5, pclk_manual > 0, pclk_div);
  728. }
  729. static int set_xclk(sensor_t *sensor, int timer, int xclk)
  730. {
  731. int ret = 0;
  732. if (xclk > 10)
  733. {
  734. ESP_LOGE(TAG, "only XCLK under 10MHz is supported, and XCLK is now set to 10M");
  735. xclk = 10;
  736. }
  737. sensor->xclk_freq_hz = xclk * 1000000U;
  738. ret = xclk_timer_conf(timer, sensor->xclk_freq_hz);
  739. return ret;
  740. }
  741. int nt99141_detect(int slv_addr, sensor_id_t *id)
  742. {
  743. if (NT99141_SCCB_ADDR == slv_addr) {
  744. SCCB_Write16(slv_addr, 0x3008, 0x01);//bank sensor
  745. uint16_t h = SCCB_Read16(slv_addr, 0x3000);
  746. uint16_t l = SCCB_Read16(slv_addr, 0x3001);
  747. uint16_t PID = (h<<8) | l;
  748. if (NT99141_PID == PID) {
  749. id->PID = PID;
  750. return PID;
  751. } else {
  752. ESP_LOGI(TAG, "Mismatch PID=0x%x", PID);
  753. }
  754. }
  755. return 0;
  756. }
  757. static int init_status(sensor_t *sensor)
  758. {
  759. sensor->status.brightness = 0;
  760. sensor->status.contrast = 0;
  761. sensor->status.saturation = 0;
  762. sensor->status.sharpness = (read_reg(sensor->slv_addr, 0x3301));
  763. sensor->status.denoise = get_denoise(sensor);
  764. sensor->status.ae_level = 0;
  765. sensor->status.gainceiling = read_reg16(sensor->slv_addr, 0x32F0) & 0xFF;
  766. sensor->status.awb = check_reg_mask(sensor->slv_addr, ISP_CONTROL_01, 0x10);
  767. sensor->status.dcw = !check_reg_mask(sensor->slv_addr, 0x5183, 0x80);
  768. sensor->status.agc = !check_reg_mask(sensor->slv_addr, AEC_PK_MANUAL, AEC_PK_MANUAL_AGC_MANUALEN);
  769. sensor->status.aec = !check_reg_mask(sensor->slv_addr, AEC_PK_MANUAL, AEC_PK_MANUAL_AEC_MANUALEN);
  770. sensor->status.hmirror = check_reg_mask(sensor->slv_addr, TIMING_TC_REG21, TIMING_TC_REG21_HMIRROR);
  771. sensor->status.vflip = check_reg_mask(sensor->slv_addr, TIMING_TC_REG20, TIMING_TC_REG20_VFLIP);
  772. sensor->status.colorbar = check_reg_mask(sensor->slv_addr, PRE_ISP_TEST_SETTING_1, TEST_COLOR_BAR);
  773. sensor->status.bpc = check_reg_mask(sensor->slv_addr, 0x5000, 0x04);
  774. sensor->status.wpc = check_reg_mask(sensor->slv_addr, 0x5000, 0x02);
  775. sensor->status.raw_gma = check_reg_mask(sensor->slv_addr, 0x5000, 0x20);
  776. sensor->status.lenc = check_reg_mask(sensor->slv_addr, 0x5000, 0x80);
  777. sensor->status.quality = read_reg(sensor->slv_addr, COMPRESSION_CTRL07) & 0x3f;
  778. sensor->status.special_effect = 0;
  779. sensor->status.wb_mode = 0;
  780. sensor->status.awb_gain = check_reg_mask(sensor->slv_addr, 0x3000, 0x01);
  781. sensor->status.agc_gain = get_agc_gain(sensor);
  782. sensor->status.aec_value = get_aec_value(sensor);
  783. sensor->status.aec2 = check_reg_mask(sensor->slv_addr, 0x3000, 0x04);
  784. return 0;
  785. }
  786. int nt99141_init(sensor_t *sensor)
  787. {
  788. sensor->reset = reset;
  789. sensor->set_pixformat = set_pixformat;
  790. sensor->set_framesize = set_framesize;
  791. sensor->set_contrast = set_contrast;
  792. sensor->set_brightness = set_brightness;
  793. sensor->set_saturation = set_saturation;
  794. sensor->set_sharpness = set_sharpness;
  795. sensor->set_gainceiling = set_gainceiling;
  796. sensor->set_quality = set_quality;
  797. sensor->set_colorbar = set_colorbar;
  798. sensor->set_gain_ctrl = set_gain_ctrl;
  799. sensor->set_exposure_ctrl = set_exposure_ctrl;
  800. sensor->set_whitebal = set_whitebal;
  801. sensor->set_hmirror = set_hmirror;
  802. sensor->set_vflip = set_vflip;
  803. sensor->init_status = init_status;
  804. sensor->set_aec2 = set_aec2;
  805. sensor->set_aec_value = set_aec_value;
  806. sensor->set_special_effect = set_special_effect;
  807. sensor->set_wb_mode = set_wb_mode;
  808. sensor->set_ae_level = set_ae_level;
  809. sensor->set_dcw = set_dcw_dsp;
  810. sensor->set_bpc = set_bpc_dsp;
  811. sensor->set_wpc = set_wpc_dsp;
  812. sensor->set_awb_gain = set_awb_gain_dsp;
  813. sensor->set_agc_gain = set_agc_gain;
  814. sensor->set_raw_gma = set_raw_gma_dsp;
  815. sensor->set_lenc = set_lenc_dsp;
  816. sensor->set_denoise = set_denoise;
  817. sensor->get_reg = get_reg;
  818. sensor->set_reg = set_reg;
  819. sensor->set_res_raw = set_res_raw;
  820. sensor->set_pll = _set_pll;
  821. sensor->set_xclk = set_xclk;
  822. return 0;
  823. }