gc0308.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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 "gc0308.h"
  20. #include "gc0308_regs.h"
  21. #include "gc0308_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 = "gc0308";
  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. ESP_LOGI(TAG, "REG list look ======================");
  98. for (size_t i = 0xf0; i <= 0xfe; i++) {
  99. ESP_LOGI(TAG, "reg[0x%02x] = 0x%02x", i, read_reg(slv_addr, i));
  100. }
  101. ESP_LOGI(TAG, "\npage 0 ===");
  102. write_reg(slv_addr, 0xfe, 0x00); // page 0
  103. for (size_t i = 0x03; i <= 0xa2; i++) {
  104. ESP_LOGI(TAG, "p0 reg[0x%02x] = 0x%02x", i, read_reg(slv_addr, i));
  105. }
  106. ESP_LOGI(TAG, "\npage 3 ===");
  107. write_reg(slv_addr, 0xfe, 0x03); // page 3
  108. for (size_t i = 0x01; i <= 0x43; i++) {
  109. ESP_LOGI(TAG, "p3 reg[0x%02x] = 0x%02x", i, read_reg(slv_addr, i));
  110. }
  111. #endif
  112. }
  113. static int reset(sensor_t *sensor)
  114. {
  115. int ret = 0;
  116. // Software Reset: clear all registers and reset them to their default values
  117. ret = write_reg(sensor->slv_addr, RESET_RELATED, 0xf0);
  118. if (ret) {
  119. ESP_LOGE(TAG, "Software Reset FAILED!");
  120. return ret;
  121. }
  122. vTaskDelay(100 / portTICK_PERIOD_MS);
  123. ret = write_regs(sensor->slv_addr, gc0308_sensor_default_regs);
  124. if (ret == 0) {
  125. ESP_LOGD(TAG, "Camera defaults loaded");
  126. vTaskDelay(100 / portTICK_PERIOD_MS);
  127. write_reg(sensor->slv_addr, 0xfe, 0x00);
  128. #ifdef CONFIG_IDF_TARGET_ESP32
  129. set_reg_bits(sensor->slv_addr, 0x28, 4, 0x07, 1); //frequency division for esp32, ensure pclk <= 15MHz
  130. #endif
  131. }
  132. return ret;
  133. }
  134. static int set_pixformat(sensor_t *sensor, pixformat_t pixformat)
  135. {
  136. int ret = 0;
  137. switch (pixformat) {
  138. case PIXFORMAT_RGB565:
  139. write_reg(sensor->slv_addr, 0xfe, 0x00);
  140. ret = set_reg_bits(sensor->slv_addr, 0x24, 0, 0x0f, 6); //RGB565
  141. break;
  142. case PIXFORMAT_YUV422:
  143. write_reg(sensor->slv_addr, 0xfe, 0x00);
  144. ret = set_reg_bits(sensor->slv_addr, 0x24, 0, 0x0f, 2); //yuv422 Y Cb Y Cr
  145. break;
  146. default:
  147. ESP_LOGW(TAG, "unsupport format");
  148. ret = -1;
  149. break;
  150. }
  151. if (ret == 0) {
  152. sensor->pixformat = pixformat;
  153. ESP_LOGD(TAG, "Set pixformat to: %u", pixformat);
  154. }
  155. return ret;
  156. }
  157. static int set_framesize(sensor_t *sensor, framesize_t framesize)
  158. {
  159. int ret = 0;
  160. if (framesize > FRAMESIZE_VGA) {
  161. ESP_LOGW(TAG, "Invalid framesize: %u", framesize);
  162. framesize = FRAMESIZE_VGA;
  163. }
  164. sensor->status.framesize = framesize;
  165. uint16_t w = resolution[framesize].width;
  166. uint16_t h = resolution[framesize].height;
  167. uint16_t row_s = (resolution[FRAMESIZE_VGA].height - h) / 2;
  168. uint16_t col_s = (resolution[FRAMESIZE_VGA].width - w) / 2;
  169. (void)row_s;
  170. (void)col_s;
  171. #if CONFIG_GC_SENSOR_SUBSAMPLE_MODE
  172. struct subsample_cfg {
  173. uint16_t ratio_numerator;
  174. uint16_t ratio_denominator;
  175. uint8_t reg0x54;
  176. uint8_t reg0x56;
  177. uint8_t reg0x57;
  178. uint8_t reg0x58;
  179. uint8_t reg0x59;
  180. };
  181. const struct subsample_cfg subsample_cfgs[] = { // define some subsample ratio
  182. {84, 420, 0x55, 0x00, 0x00, 0x00, 0x00}, //1/5
  183. {105, 420, 0x44, 0x00, 0x00, 0x00, 0x00},//1/4
  184. {140, 420, 0x33, 0x00, 0x00, 0x00, 0x00},//1/3
  185. {210, 420, 0x22, 0x00, 0x00, 0x00, 0x00},//1/2
  186. {240, 420, 0x77, 0x02, 0x46, 0x02, 0x46},//4/7
  187. {252, 420, 0x55, 0x02, 0x04, 0x02, 0x04},//3/5
  188. {280, 420, 0x33, 0x02, 0x00, 0x02, 0x00},//2/3
  189. {420, 420, 0x11, 0x00, 0x00, 0x00, 0x00},//1/1
  190. };
  191. uint16_t win_w = 640;
  192. uint16_t win_h = 480;
  193. const struct subsample_cfg *cfg = NULL;
  194. /**
  195. * Strategy: try to keep the maximum perspective
  196. */
  197. for (size_t i = 0; i < sizeof(subsample_cfgs) / sizeof(struct subsample_cfg); i++) {
  198. cfg = &subsample_cfgs[i];
  199. if ((win_w * cfg->ratio_numerator / cfg->ratio_denominator >= w) && (win_h * cfg->ratio_numerator / cfg->ratio_denominator >= h)) {
  200. win_w = w * cfg->ratio_denominator / cfg->ratio_numerator;
  201. win_h = h * cfg->ratio_denominator / cfg->ratio_numerator;
  202. row_s = (resolution[FRAMESIZE_VGA].height - win_h) / 2;
  203. col_s = (resolution[FRAMESIZE_VGA].width - win_w) / 2;
  204. ESP_LOGI(TAG, "subsample win:%dx%d, ratio:%f", win_w, win_h, (float)cfg->ratio_numerator / (float)cfg->ratio_denominator);
  205. break;
  206. }
  207. }
  208. write_reg(sensor->slv_addr, 0xfe, 0x00);
  209. write_reg(sensor->slv_addr, 0x05, H8(row_s));
  210. write_reg(sensor->slv_addr, 0x06, L8(row_s));
  211. write_reg(sensor->slv_addr, 0x07, H8(col_s));
  212. write_reg(sensor->slv_addr, 0x08, L8(col_s));
  213. write_reg(sensor->slv_addr, 0x09, H8(win_h + 8));
  214. write_reg(sensor->slv_addr, 0x0a, L8(win_h + 8));
  215. write_reg(sensor->slv_addr, 0x0b, H8(win_w + 8));
  216. write_reg(sensor->slv_addr, 0x0c, L8(win_w + 8));
  217. write_reg(sensor->slv_addr, 0xfe, 0x01);
  218. set_reg_bits(sensor->slv_addr, 0x53, 7, 0x01, 1);
  219. set_reg_bits(sensor->slv_addr, 0x55, 0, 0x01, 1);
  220. write_reg(sensor->slv_addr, 0x54, cfg->reg0x54);
  221. write_reg(sensor->slv_addr, 0x56, cfg->reg0x56);
  222. write_reg(sensor->slv_addr, 0x57, cfg->reg0x57);
  223. write_reg(sensor->slv_addr, 0x58, cfg->reg0x58);
  224. write_reg(sensor->slv_addr, 0x59, cfg->reg0x59);
  225. write_reg(sensor->slv_addr, 0xfe, 0x00);
  226. #elif CONFIG_GC_SENSOR_WINDOWING_MODE
  227. write_reg(sensor->slv_addr, 0xfe, 0x00);
  228. write_reg(sensor->slv_addr, 0xf7, col_s / 4);
  229. write_reg(sensor->slv_addr, 0xf8, row_s / 4);
  230. write_reg(sensor->slv_addr, 0xf9, (col_s + h) / 4);
  231. write_reg(sensor->slv_addr, 0xfa, (row_s + w) / 4);
  232. write_reg(sensor->slv_addr, 0x05, H8(row_s));
  233. write_reg(sensor->slv_addr, 0x06, L8(row_s));
  234. write_reg(sensor->slv_addr, 0x07, H8(col_s));
  235. write_reg(sensor->slv_addr, 0x08, L8(col_s));
  236. write_reg(sensor->slv_addr, 0x09, H8(h + 8));
  237. write_reg(sensor->slv_addr, 0x0a, L8(h + 8));
  238. write_reg(sensor->slv_addr, 0x0b, H8(w + 8));
  239. write_reg(sensor->slv_addr, 0x0c, L8(w + 8));
  240. #endif
  241. if (ret == 0) {
  242. ESP_LOGD(TAG, "Set framesize to: %ux%u", w, h);
  243. }
  244. return 0;
  245. }
  246. static int set_contrast(sensor_t *sensor, int contrast)
  247. {
  248. if (contrast != 0) {
  249. write_reg(sensor->slv_addr, 0xfe, 0x00);
  250. write_reg(sensor->slv_addr, 0xb3, contrast);
  251. }
  252. return 0;
  253. }
  254. static int set_global_gain(sensor_t *sensor, int gain_level)
  255. {
  256. if (gain_level != 0) {
  257. write_reg(sensor->slv_addr, 0xfe, 0x00);
  258. write_reg(sensor->slv_addr, 0x50, gain_level);
  259. }
  260. return 0;
  261. }
  262. static int set_hmirror(sensor_t *sensor, int enable)
  263. {
  264. int ret = 0;
  265. sensor->status.hmirror = enable;
  266. ret = write_reg(sensor->slv_addr, 0xfe, 0x00);
  267. ret |= set_reg_bits(sensor->slv_addr, 0x14, 0, 0x01, enable != 0);
  268. if (ret == 0) {
  269. ESP_LOGD(TAG, "Set h-mirror to: %d", enable);
  270. }
  271. return ret;
  272. }
  273. static int set_vflip(sensor_t *sensor, int enable)
  274. {
  275. int ret = 0;
  276. sensor->status.vflip = enable;
  277. ret = write_reg(sensor->slv_addr, 0xfe, 0x00);
  278. ret |= set_reg_bits(sensor->slv_addr, 0x14, 1, 0x01, enable != 0);
  279. if (ret == 0) {
  280. ESP_LOGD(TAG, "Set v-flip to: %d", enable);
  281. }
  282. return ret;
  283. }
  284. static int set_colorbar(sensor_t *sensor, int enable)
  285. {
  286. int ret = 0;
  287. ret = write_reg(sensor->slv_addr, 0xfe, 0x00);
  288. ret |= set_reg_bits(sensor->slv_addr, 0x2e, 0, 0x01, enable);
  289. if (ret == 0) {
  290. sensor->status.colorbar = enable;
  291. ESP_LOGD(TAG, "Set colorbar to: %d", enable);
  292. }
  293. return ret;
  294. }
  295. static int get_reg(sensor_t *sensor, int reg, int mask)
  296. {
  297. int ret = 0;
  298. if (mask > 0xFF) {
  299. ESP_LOGE(TAG, "mask should not more than 0xff");
  300. } else {
  301. ret = read_reg(sensor->slv_addr, reg);
  302. }
  303. if (ret > 0) {
  304. ret &= mask;
  305. }
  306. return ret;
  307. }
  308. static int set_reg(sensor_t *sensor, int reg, int mask, int value)
  309. {
  310. int ret = 0;
  311. if (mask > 0xFF) {
  312. ESP_LOGE(TAG, "mask should not more than 0xff");
  313. } else {
  314. ret = read_reg(sensor->slv_addr, reg);
  315. }
  316. if (ret < 0) {
  317. return ret;
  318. }
  319. value = (ret & ~mask) | (value & mask);
  320. if (mask > 0xFF) {
  321. } else {
  322. ret = write_reg(sensor->slv_addr, reg, value);
  323. }
  324. return ret;
  325. }
  326. static int init_status(sensor_t *sensor)
  327. {
  328. write_reg(sensor->slv_addr, 0xfe, 0x00);
  329. sensor->status.brightness = 0;
  330. sensor->status.contrast = 0;
  331. sensor->status.saturation = 0;
  332. sensor->status.sharpness = 0;
  333. sensor->status.denoise = 0;
  334. sensor->status.ae_level = 0;
  335. sensor->status.gainceiling = 0;
  336. sensor->status.awb = 0;
  337. sensor->status.dcw = 0;
  338. sensor->status.agc = 0;
  339. sensor->status.aec = 0;
  340. sensor->status.hmirror = check_reg_mask(sensor->slv_addr, 0x14, 0x01);
  341. sensor->status.vflip = check_reg_mask(sensor->slv_addr, 0x14, 0x02);
  342. sensor->status.colorbar = 0;
  343. sensor->status.bpc = 0;
  344. sensor->status.wpc = 0;
  345. sensor->status.raw_gma = 0;
  346. sensor->status.lenc = 0;
  347. sensor->status.quality = 0;
  348. sensor->status.special_effect = 0;
  349. sensor->status.wb_mode = 0;
  350. sensor->status.awb_gain = 0;
  351. sensor->status.agc_gain = 0;
  352. sensor->status.aec_value = 0;
  353. sensor->status.aec2 = 0;
  354. print_regs(sensor->slv_addr);
  355. return 0;
  356. }
  357. static int set_dummy(sensor_t *sensor, int val)
  358. {
  359. ESP_LOGW(TAG, "Unsupported");
  360. return -1;
  361. }
  362. static int set_gainceiling_dummy(sensor_t *sensor, gainceiling_t val)
  363. {
  364. ESP_LOGW(TAG, "Unsupported");
  365. return -1;
  366. }
  367. int gc0308_detect(int slv_addr, sensor_id_t *id)
  368. {
  369. if (GC0308_SCCB_ADDR == slv_addr) {
  370. write_reg(slv_addr, 0xfe, 0x00);
  371. uint8_t PID = SCCB_Read(slv_addr, 0x00);
  372. if (GC0308_PID == PID) {
  373. id->PID = PID;
  374. return PID;
  375. } else {
  376. ESP_LOGI(TAG, "Mismatch PID=0x%x", PID);
  377. }
  378. }
  379. return 0;
  380. }
  381. int gc0308_init(sensor_t *sensor)
  382. {
  383. sensor->init_status = init_status;
  384. sensor->reset = reset;
  385. sensor->set_pixformat = set_pixformat;
  386. sensor->set_framesize = set_framesize;
  387. sensor->set_contrast = set_contrast;
  388. sensor->set_brightness = set_dummy;
  389. sensor->set_saturation = set_dummy;
  390. sensor->set_sharpness = set_dummy;
  391. sensor->set_denoise = set_dummy;
  392. sensor->set_gainceiling = set_gainceiling_dummy;
  393. sensor->set_quality = set_dummy;
  394. sensor->set_colorbar = set_colorbar;
  395. sensor->set_whitebal = set_dummy;
  396. sensor->set_gain_ctrl = set_global_gain;
  397. sensor->set_exposure_ctrl = set_dummy;
  398. sensor->set_hmirror = set_hmirror;
  399. sensor->set_vflip = set_vflip;
  400. sensor->set_aec2 = set_dummy;
  401. sensor->set_awb_gain = set_dummy;
  402. sensor->set_agc_gain = set_dummy;
  403. sensor->set_aec_value = set_dummy;
  404. sensor->set_special_effect = set_dummy;
  405. sensor->set_wb_mode = set_dummy;
  406. sensor->set_ae_level = set_dummy;
  407. sensor->set_dcw = set_dummy;
  408. sensor->set_bpc = set_dummy;
  409. sensor->set_wpc = set_dummy;
  410. sensor->set_raw_gma = set_dummy;
  411. sensor->set_lenc = set_dummy;
  412. sensor->get_reg = get_reg;
  413. sensor->set_reg = set_reg;
  414. sensor->set_res_raw = NULL;
  415. sensor->set_pll = NULL;
  416. sensor->set_xclk = NULL;
  417. ESP_LOGD(TAG, "GC0308 Attached");
  418. return 0;
  419. }