gc2145.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. // Copyright 2015-2021 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #include <stdint.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include "freertos/FreeRTOS.h"
  17. #include "freertos/task.h"
  18. #include "sccb.h"
  19. #include "gc2145.h"
  20. #include "gc2145_regs.h"
  21. #include "gc2145_settings.h"
  22. #if defined(ARDUINO_ARCH_ESP32) && defined(CONFIG_ARDUHAL_ESP_LOG)
  23. #include "esp32-hal-log.h"
  24. #else
  25. #include "esp_log.h"
  26. static const char *TAG = "gc2145";
  27. #endif
  28. #define H8(v) ((v)>>8)
  29. #define L8(v) ((v)&0xff)
  30. //#define REG_DEBUG_ON
  31. static int read_reg(uint8_t slv_addr, const uint16_t reg)
  32. {
  33. int ret = SCCB_Read(slv_addr, reg);
  34. #ifdef REG_DEBUG_ON
  35. if (ret < 0) {
  36. ESP_LOGE(TAG, "READ REG 0x%04x FAILED: %d", reg, ret);
  37. }
  38. #endif
  39. return ret;
  40. }
  41. static int write_reg(uint8_t slv_addr, const uint16_t reg, uint8_t value)
  42. {
  43. int ret = 0;
  44. #ifndef REG_DEBUG_ON
  45. ret = SCCB_Write(slv_addr, reg, value);
  46. #else
  47. int old_value = read_reg(slv_addr, reg);
  48. if (old_value < 0) {
  49. return old_value;
  50. }
  51. if ((uint8_t)old_value != value) {
  52. ESP_LOGI(TAG, "NEW REG 0x%04x: 0x%02x to 0x%02x", reg, (uint8_t)old_value, value);
  53. ret = SCCB_Write(slv_addr, reg, value);
  54. } else {
  55. ESP_LOGD(TAG, "OLD REG 0x%04x: 0x%02x", reg, (uint8_t)old_value);
  56. ret = SCCB_Write(slv_addr, reg, value);//maybe not?
  57. }
  58. if (ret < 0) {
  59. ESP_LOGE(TAG, "WRITE REG 0x%04x FAILED: %d", reg, ret);
  60. }
  61. #endif
  62. return ret;
  63. }
  64. static int check_reg_mask(uint8_t slv_addr, uint16_t reg, uint8_t mask)
  65. {
  66. return (read_reg(slv_addr, reg) & mask) == mask;
  67. }
  68. static int set_reg_bits(uint8_t slv_addr, uint16_t reg, uint8_t offset, uint8_t mask, uint8_t value)
  69. {
  70. int ret = 0;
  71. uint8_t c_value, new_value;
  72. ret = read_reg(slv_addr, reg);
  73. if (ret < 0) {
  74. return ret;
  75. }
  76. c_value = ret;
  77. new_value = (c_value & ~(mask << offset)) | ((value & mask) << offset);
  78. ret = write_reg(slv_addr, reg, new_value);
  79. return ret;
  80. }
  81. static int write_regs(uint8_t slv_addr, const uint16_t (*regs)[2])
  82. {
  83. int i = 0, ret = 0;
  84. while (!ret && regs[i][0] != REGLIST_TAIL) {
  85. if (regs[i][0] == REG_DLY) {
  86. vTaskDelay(regs[i][1] / portTICK_PERIOD_MS);
  87. } else {
  88. ret = write_reg(slv_addr, regs[i][0], regs[i][1]);
  89. }
  90. i++;
  91. }
  92. return ret;
  93. }
  94. static void print_regs(uint8_t slv_addr)
  95. {
  96. #ifdef DEBUG_PRINT_REG
  97. vTaskDelay(pdMS_TO_TICKS(100));
  98. ESP_LOGI(TAG, "REG list look ======================");
  99. for (size_t i = 0xf0; i <= 0xfe; i++) {
  100. ESP_LOGI(TAG, "reg[0x%02x] = 0x%02x", i, read_reg(slv_addr, i));
  101. }
  102. ESP_LOGI(TAG, "\npage 0 ===");
  103. write_reg(slv_addr, 0xfe, 0x00); // page 0
  104. for (size_t i = 0x03; i <= 0x24; i++) {
  105. ESP_LOGI(TAG, "p0 reg[0x%02x] = 0x%02x", i, read_reg(slv_addr, i));
  106. }
  107. for (size_t i = 0x80; i <= 0xa2; i++) {
  108. ESP_LOGI(TAG, "p0 reg[0x%02x] = 0x%02x", i, read_reg(slv_addr, i));
  109. }
  110. ESP_LOGI(TAG, "\npage 3 ===");
  111. write_reg(slv_addr, 0xfe, 0x03); // page 3
  112. for (size_t i = 0x01; i <= 0x43; i++) {
  113. ESP_LOGI(TAG, "p3 reg[0x%02x] = 0x%02x", i, read_reg(slv_addr, i));
  114. }
  115. #endif
  116. }
  117. static int reset(sensor_t *sensor)
  118. {
  119. int ret = 0;
  120. // Software Reset: clear all registers and reset them to their default values
  121. ret = write_reg(sensor->slv_addr, RESET_RELATED, 0xe0);
  122. if (ret) {
  123. ESP_LOGE(TAG, "Software Reset FAILED!");
  124. return ret;
  125. }
  126. vTaskDelay(100 / portTICK_PERIOD_MS);
  127. ret = write_regs(sensor->slv_addr, gc2145_default_init_regs);
  128. if (ret == 0) {
  129. ESP_LOGD(TAG, "Camera defaults loaded");
  130. vTaskDelay(100 / portTICK_PERIOD_MS);
  131. #ifdef CONFIG_IDF_TARGET_ESP32
  132. write_reg(sensor->slv_addr, 0xfe, 0x00);
  133. //ensure pclk <= 15MHz for esp32
  134. set_reg_bits(sensor->slv_addr, 0xf8, 0, 0x3f, 2); // divx4
  135. set_reg_bits(sensor->slv_addr, 0xfa, 4, 0x0f, 2); // divide_by
  136. #endif
  137. }
  138. return ret;
  139. }
  140. static int set_pixformat(sensor_t *sensor, pixformat_t pixformat)
  141. {
  142. int ret = 0;
  143. switch (pixformat) {
  144. case PIXFORMAT_RGB565:
  145. write_reg(sensor->slv_addr, 0xfe, 0x00);
  146. ret = set_reg_bits(sensor->slv_addr, P0_OUTPUT_FORMAT, 0, 0x1f, 6); //RGB565
  147. break;
  148. case PIXFORMAT_YUV422:
  149. write_reg(sensor->slv_addr, 0xfe, 0x00);
  150. ret = set_reg_bits(sensor->slv_addr, P0_OUTPUT_FORMAT, 0, 0x1f, 2); //yuv422
  151. break;
  152. default:
  153. ESP_LOGW(TAG, "unsupport format");
  154. ret = -1;
  155. break;
  156. }
  157. if (ret == 0) {
  158. sensor->pixformat = pixformat;
  159. ESP_LOGD(TAG, "Set pixformat to: %u", pixformat);
  160. }
  161. return ret;
  162. }
  163. static int set_framesize(sensor_t *sensor, framesize_t framesize)
  164. {
  165. int ret = 0;
  166. if (framesize > FRAMESIZE_UXGA) {
  167. ESP_LOGW(TAG, "Invalid framesize: %u", framesize);
  168. framesize = FRAMESIZE_UXGA;
  169. }
  170. sensor->status.framesize = framesize;
  171. uint16_t w = resolution[framesize].width;
  172. uint16_t h = resolution[framesize].height;
  173. uint16_t row_s = (resolution[FRAMESIZE_UXGA].height - h) / 2;
  174. uint16_t col_s = (resolution[FRAMESIZE_UXGA].width - w) / 2;
  175. (void)row_s;
  176. (void)col_s;
  177. #if CONFIG_GC_SENSOR_SUBSAMPLE_MODE
  178. struct subsample_cfg {
  179. uint16_t ratio_numerator;
  180. uint16_t ratio_denominator;
  181. uint8_t reg0x99;
  182. uint8_t reg0x9b;
  183. uint8_t reg0x9c;
  184. uint8_t reg0x9d;
  185. uint8_t reg0x9e;
  186. uint8_t reg0x9f;
  187. uint8_t reg0xa0;
  188. uint8_t reg0xa1;
  189. uint8_t reg0xa2;
  190. };
  191. const struct subsample_cfg subsample_cfgs[] = { // define some subsample ratio
  192. // {60, 420, 0x77, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, //1/7 // A smaller ratio brings a larger view, but it reduces the frame rate
  193. // {84, 420, 0x55, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, //1/5
  194. // {105, 420, 0x44, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},//1/4
  195. {140, 420, 0x33, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},//1/3
  196. {210, 420, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},//1/2
  197. {240, 420, 0x77, 0x02, 0x46, 0x02, 0x46, 0x02, 0x46, 0x02, 0x46},//4/7
  198. {252, 420, 0x55, 0x02, 0x04, 0x02, 0x04, 0x02, 0x04, 0x02, 0x04},//3/5
  199. {280, 420, 0x33, 0x00, 0x02, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00},//2/3
  200. {420, 420, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},//1/1
  201. };
  202. uint16_t win_w = resolution[FRAMESIZE_UXGA].width;
  203. uint16_t win_h = resolution[FRAMESIZE_UXGA].height;
  204. const struct subsample_cfg *cfg = NULL;
  205. /**
  206. * Strategy: try to keep the maximum perspective
  207. */
  208. uint8_t i = 0;
  209. if (framesize >= FRAMESIZE_QVGA) {
  210. i = 1;
  211. }
  212. for (; i < sizeof(subsample_cfgs) / sizeof(struct subsample_cfg); i++) {
  213. cfg = &subsample_cfgs[i];
  214. if ((win_w * cfg->ratio_numerator / cfg->ratio_denominator >= w) && (win_h * cfg->ratio_numerator / cfg->ratio_denominator >= h)) {
  215. win_w = w * cfg->ratio_denominator / cfg->ratio_numerator;
  216. win_h = h * cfg->ratio_denominator / cfg->ratio_numerator;
  217. row_s = (resolution[FRAMESIZE_UXGA].height - win_h) / 2;
  218. col_s = (resolution[FRAMESIZE_UXGA].width - win_w) / 2;
  219. ESP_LOGI(TAG, "subsample win:%dx%d, ratio:%f", win_w, win_h, (float)cfg->ratio_numerator / (float)cfg->ratio_denominator);
  220. break;
  221. }
  222. }
  223. write_reg(sensor->slv_addr, 0xfe, 0x00);
  224. write_reg(sensor->slv_addr, P0_CROP_ENABLE, 0x01);
  225. write_reg(sensor->slv_addr, 0x09, H8(row_s));
  226. write_reg(sensor->slv_addr, 0x0a, L8(row_s));
  227. write_reg(sensor->slv_addr, 0x0b, H8(col_s));
  228. write_reg(sensor->slv_addr, 0x0c, L8(col_s));
  229. write_reg(sensor->slv_addr, 0x0d, H8(win_h + 8));
  230. write_reg(sensor->slv_addr, 0x0e, L8(win_h + 8));
  231. write_reg(sensor->slv_addr, 0x0f, H8(win_w + 16));
  232. write_reg(sensor->slv_addr, 0x10, L8(win_w + 16));
  233. write_reg(sensor->slv_addr, 0x99, cfg->reg0x99);
  234. write_reg(sensor->slv_addr, 0x9b, cfg->reg0x9b);
  235. write_reg(sensor->slv_addr, 0x9c, cfg->reg0x9c);
  236. write_reg(sensor->slv_addr, 0x9d, cfg->reg0x9d);
  237. write_reg(sensor->slv_addr, 0x9e, cfg->reg0x9e);
  238. write_reg(sensor->slv_addr, 0x9f, cfg->reg0x9f);
  239. write_reg(sensor->slv_addr, 0xa0, cfg->reg0xa0);
  240. write_reg(sensor->slv_addr, 0xa1, cfg->reg0xa1);
  241. write_reg(sensor->slv_addr, 0xa2, cfg->reg0xa2);
  242. write_reg(sensor->slv_addr, 0x95, H8(h));
  243. write_reg(sensor->slv_addr, 0x96, L8(h));
  244. write_reg(sensor->slv_addr, 0x97, H8(w));
  245. write_reg(sensor->slv_addr, 0x98, L8(w));
  246. #elif CONFIG_GC_SENSOR_WINDOWING_MODE
  247. write_reg(sensor->slv_addr, 0xfe, 0x00);
  248. write_reg(sensor->slv_addr, P0_CROP_ENABLE, 0x01);
  249. // write_reg(sensor->slv_addr, 0xec, col_s / 8); //measure window
  250. // write_reg(sensor->slv_addr, 0xed, row_s / 8);
  251. // write_reg(sensor->slv_addr, 0xee, (col_s + h) / 8);
  252. // write_reg(sensor->slv_addr, 0xef, (row_s + w) / 8);
  253. write_reg(sensor->slv_addr, 0x09, H8(row_s));
  254. write_reg(sensor->slv_addr, 0x0a, L8(row_s));
  255. write_reg(sensor->slv_addr, 0x0b, H8(col_s));
  256. write_reg(sensor->slv_addr, 0x0c, L8(col_s));
  257. write_reg(sensor->slv_addr, 0x0d, H8(h + 8));
  258. write_reg(sensor->slv_addr, 0x0e, L8(h + 8));
  259. write_reg(sensor->slv_addr, 0x0f, H8(w + 8));
  260. write_reg(sensor->slv_addr, 0x10, L8(w + 8));
  261. write_reg(sensor->slv_addr, 0x95, H8(h));
  262. write_reg(sensor->slv_addr, 0x96, L8(h));
  263. write_reg(sensor->slv_addr, 0x97, H8(w));
  264. write_reg(sensor->slv_addr, 0x98, L8(w));
  265. #endif
  266. if (ret == 0) {
  267. ESP_LOGD(TAG, "Set framesize to: %ux%u", w, h);
  268. }
  269. return ret;
  270. }
  271. static int set_hmirror(sensor_t *sensor, int enable)
  272. {
  273. int ret = 0;
  274. sensor->status.hmirror = enable;
  275. ret = write_reg(sensor->slv_addr, 0xfe, 0x00);
  276. ret |= set_reg_bits(sensor->slv_addr, P0_ANALOG_MODE1, 0, 0x01, enable != 0);
  277. if (ret == 0) {
  278. ESP_LOGD(TAG, "Set h-mirror to: %d", enable);
  279. }
  280. return ret;
  281. }
  282. static int set_vflip(sensor_t *sensor, int enable)
  283. {
  284. int ret = 0;
  285. sensor->status.vflip = enable;
  286. ret = write_reg(sensor->slv_addr, 0xfe, 0x00);
  287. ret |= set_reg_bits(sensor->slv_addr, P0_ANALOG_MODE1, 1, 0x01, enable != 0);
  288. if (ret == 0) {
  289. ESP_LOGD(TAG, "Set v-flip to: %d", enable);
  290. }
  291. return ret;
  292. }
  293. static int set_colorbar(sensor_t *sensor, int enable)
  294. {
  295. int ret = 0;
  296. // ret = write_reg(sensor->slv_addr, 0xfe, 0x00);
  297. // ret |= set_reg_bits(sensor->slv_addr, P0_DEBUG_MODE3, 3, 0x01, enable);
  298. if (ret == 0) {
  299. sensor->status.colorbar = enable;
  300. ESP_LOGD(TAG, "Set colorbar to: %d", enable);
  301. }
  302. return ret;
  303. }
  304. static int get_reg(sensor_t *sensor, int reg, int mask)
  305. {
  306. int ret = 0;
  307. if (mask > 0xFF) {
  308. ESP_LOGE(TAG, "mask should not more than 0xff");
  309. } else {
  310. ret = read_reg(sensor->slv_addr, reg);
  311. }
  312. if (ret > 0) {
  313. ret &= mask;
  314. }
  315. return ret;
  316. }
  317. static int set_reg(sensor_t *sensor, int reg, int mask, int value)
  318. {
  319. int ret = 0;
  320. if (mask > 0xFF) {
  321. ESP_LOGE(TAG, "mask should not more than 0xff");
  322. } else {
  323. ret = read_reg(sensor->slv_addr, reg);
  324. }
  325. if (ret < 0) {
  326. return ret;
  327. }
  328. value = (ret & ~mask) | (value & mask);
  329. if (mask > 0xFF) {
  330. } else {
  331. ret = write_reg(sensor->slv_addr, reg, value);
  332. }
  333. return ret;
  334. }
  335. static int init_status(sensor_t *sensor)
  336. {
  337. write_reg(sensor->slv_addr, 0xfe, 0x00);
  338. sensor->status.brightness = 0;
  339. sensor->status.contrast = 0;
  340. sensor->status.saturation = 0;
  341. sensor->status.sharpness = 0;
  342. sensor->status.denoise = 0;
  343. sensor->status.ae_level = 0;
  344. sensor->status.gainceiling = 0;
  345. sensor->status.awb = 0;
  346. sensor->status.dcw = 0;
  347. sensor->status.agc = 0;
  348. sensor->status.aec = 0;
  349. sensor->status.hmirror = check_reg_mask(sensor->slv_addr, P0_ANALOG_MODE1, 0x01);
  350. sensor->status.vflip = check_reg_mask(sensor->slv_addr, P0_ANALOG_MODE1, 0x02);
  351. sensor->status.colorbar = 0;
  352. sensor->status.bpc = 0;
  353. sensor->status.wpc = 0;
  354. sensor->status.raw_gma = 0;
  355. sensor->status.lenc = 0;
  356. sensor->status.quality = 0;
  357. sensor->status.special_effect = 0;
  358. sensor->status.wb_mode = 0;
  359. sensor->status.awb_gain = 0;
  360. sensor->status.agc_gain = 0;
  361. sensor->status.aec_value = 0;
  362. sensor->status.aec2 = 0;
  363. print_regs(sensor->slv_addr);
  364. return 0;
  365. }
  366. static int set_dummy(sensor_t *sensor, int val)
  367. {
  368. ESP_LOGW(TAG, "Unsupported");
  369. return -1;
  370. }
  371. static int set_gainceiling_dummy(sensor_t *sensor, gainceiling_t val)
  372. {
  373. ESP_LOGW(TAG, "Unsupported");
  374. return -1;
  375. }
  376. int gc2145_detect(int slv_addr, sensor_id_t *id)
  377. {
  378. if (GC2145_SCCB_ADDR == slv_addr) {
  379. uint8_t MIDL = SCCB_Read(slv_addr, CHIP_ID_LOW);
  380. uint8_t MIDH = SCCB_Read(slv_addr, CHIP_ID_HIGH);
  381. uint16_t PID = MIDH << 8 | MIDL;
  382. if (GC2145_PID == PID) {
  383. id->PID = PID;
  384. return PID;
  385. } else {
  386. ESP_LOGI(TAG, "Mismatch PID=0x%x", PID);
  387. }
  388. }
  389. return 0;
  390. }
  391. int gc2145_init(sensor_t *sensor)
  392. {
  393. sensor->init_status = init_status;
  394. sensor->reset = reset;
  395. sensor->set_pixformat = set_pixformat;
  396. sensor->set_framesize = set_framesize;
  397. sensor->set_contrast = set_dummy;
  398. sensor->set_brightness = set_dummy;
  399. sensor->set_saturation = set_dummy;
  400. sensor->set_sharpness = set_dummy;
  401. sensor->set_denoise = set_dummy;
  402. sensor->set_gainceiling = set_gainceiling_dummy;
  403. sensor->set_quality = set_dummy;
  404. sensor->set_colorbar = set_colorbar;
  405. sensor->set_whitebal = set_dummy;
  406. sensor->set_gain_ctrl = set_dummy;
  407. sensor->set_exposure_ctrl = set_dummy;
  408. sensor->set_hmirror = set_hmirror;
  409. sensor->set_vflip = set_vflip;
  410. sensor->set_aec2 = set_dummy;
  411. sensor->set_awb_gain = set_dummy;
  412. sensor->set_agc_gain = set_dummy;
  413. sensor->set_aec_value = set_dummy;
  414. sensor->set_special_effect = set_dummy;
  415. sensor->set_wb_mode = set_dummy;
  416. sensor->set_ae_level = set_dummy;
  417. sensor->set_dcw = set_dummy;
  418. sensor->set_bpc = set_dummy;
  419. sensor->set_wpc = set_dummy;
  420. sensor->set_raw_gma = set_dummy;
  421. sensor->set_lenc = set_dummy;
  422. sensor->get_reg = get_reg;
  423. sensor->set_reg = set_reg;
  424. sensor->set_res_raw = NULL;
  425. sensor->set_pll = NULL;
  426. sensor->set_xclk = NULL;
  427. ESP_LOGD(TAG, "GC2145 Attached");
  428. return 0;
  429. }