bflb_Camera.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #include "bflb_Camera.h"
  2. #include "bflb_common.h"
  3. #include "bflb_i2c.h"
  4. #include "bflb_cam.h"
  5. #include "bflb_gpio.h"
  6. #include "image_sensor.h"
  7. #include "lvgl.h"
  8. #include "board.h"
  9. #include <stdint.h>
  10. #include "FreeRTOS.h"
  11. #include "task.h"
  12. // Global variables
  13. static struct bflb_device_s *i2c0;
  14. struct bflb_device_s *cam0;
  15. static struct bflb_cam_config_s cam_config;
  16. static struct image_sensor_config_s *sensor_config;
  17. static volatile uint8_t g_cam_inited = 0;
  18. static volatile uint8_t g_cam_callback_inited = 0;
  19. static lv_obj_t *canvas_cam;
  20. extern PikaEventListener* g_pika_bflb_event_listener;
  21. extern volatile uint8_t g_callback_thread_inited;
  22. extern volatile uint8_t g_cam_callback_task_flag;
  23. void canvas_cam_demo_update(void *pic_addr);
  24. static void cam_demo_callback(void) {
  25. if (!canvas_cam) {
  26. return;
  27. }
  28. uint16_t* pic_addr;
  29. uint32_t pic_size;
  30. pic_size = bflb_cam_get_frame_info(cam0, (void*)&pic_addr);
  31. bflb_cam_pop_one_frame(cam0);
  32. for (size_t i = 0; i < pic_size / sizeof(uint16_t); i++) {
  33. pic_addr[i] = __bswap16(pic_addr[i]);
  34. }
  35. canvas_cam_demo_update(pic_addr);
  36. }
  37. void cam_py_callback(void) {
  38. if (!g_cam_callback_inited) {
  39. return;
  40. }
  41. pika_debug("cam_py_callback\r\n");
  42. pks_eventListener_sendSignal(g_pika_bflb_event_listener, (uintptr_t)cam0, 0);
  43. }
  44. static void cam_isr(int irq, void* arg) {
  45. static volatile uint32_t cam_int_cnt = 0;
  46. bflb_cam_int_clear(cam0, CAM_INTCLR_NORMAL);
  47. // pika_debug("CAM interrupt, pop picture %d: 0x%08x, len: %d\r\n",
  48. // cam_int_cnt++, (uint32_t)pic_addr, pic_size);
  49. cam_demo_callback();
  50. g_cam_callback_task_flag = 1;
  51. // cam_py_callback();
  52. }
  53. static void cam_init(void) {
  54. struct bflb_cam_config_s cam_config;
  55. struct image_sensor_config_s* sensor_config;
  56. struct bflb_device_s* i2c0;
  57. i2c0 = bflb_device_get_by_name("i2c0");
  58. cam0 = bflb_device_get_by_name("cam0");
  59. if (image_sensor_scan(i2c0, &sensor_config)) {
  60. pika_debug("Sensor name: %s", sensor_config->name);
  61. } else {
  62. pika_platform_printf("\r\nError! Can't identify sensor!\r\n");
  63. cam0 = NULL;
  64. return;
  65. }
  66. _callback_thread_init();
  67. bflb_cam_int_mask(cam0, CAM_INTMASK_NORMAL, false);
  68. bflb_irq_attach(cam0->irq_num, cam_isr, NULL);
  69. bflb_irq_enable(cam0->irq_num);
  70. memcpy(&cam_config, sensor_config, IMAGE_SENSOR_INFO_COPY_SIZE);
  71. cam_config.with_mjpeg = false;
  72. cam_config.output_format = CAM_OUTPUT_FORMAT_AUTO;
  73. static lv_color_t cam_buffer[4][320 * 240] __section(".psmram_data");
  74. cam_config.output_bufaddr = (uint32_t)(uintptr_t)(void*)cam_buffer;
  75. cam_config.output_bufsize = sizeof(cam_buffer);
  76. bflb_cam_init(cam0, &cam_config);
  77. pika_debug("cam init ok");
  78. // bflb_cam_stop(cam0);
  79. }
  80. void init_cam(struct bflb_device_s *gpio) {
  81. /* DVP0 GPIO init */
  82. /* I2C GPIO */
  83. // bflb_gpio_init(gpio, GPIO_PIN_0, GPIO_FUNC_I2C0 | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1);
  84. // bflb_gpio_init(gpio, GPIO_PIN_1, GPIO_FUNC_I2C0 | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1);
  85. /* Power down GPIO */
  86. bflb_gpio_init(gpio, GPIO_PIN_16, GPIO_OUTPUT | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1);
  87. bflb_gpio_reset(gpio, GPIO_PIN_16);
  88. /* MCLK GPIO */
  89. bflb_gpio_init(gpio, GPIO_PIN_6, GPIO_FUNC_CLKOUT | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1);
  90. /* DVP0 GPIO */
  91. bflb_gpio_init(gpio, GPIO_PIN_24, GPIO_FUNC_CAM | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1);
  92. bflb_gpio_init(gpio, GPIO_PIN_25, GPIO_FUNC_CAM | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1);
  93. bflb_gpio_init(gpio, GPIO_PIN_26, GPIO_FUNC_CAM | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1);
  94. bflb_gpio_init(gpio, GPIO_PIN_27, GPIO_FUNC_CAM | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1);
  95. bflb_gpio_init(gpio, GPIO_PIN_28, GPIO_FUNC_CAM | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1);
  96. bflb_gpio_init(gpio, GPIO_PIN_29, GPIO_FUNC_CAM | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1);
  97. bflb_gpio_init(gpio, GPIO_PIN_30, GPIO_FUNC_CAM | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1);
  98. bflb_gpio_init(gpio, GPIO_PIN_31, GPIO_FUNC_CAM | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1);
  99. bflb_gpio_init(gpio, GPIO_PIN_32, GPIO_FUNC_CAM | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1);
  100. bflb_gpio_init(gpio, GPIO_PIN_33, GPIO_FUNC_CAM | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1);
  101. bflb_gpio_init(gpio, GPIO_PIN_34, GPIO_FUNC_CAM | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1);
  102. cam_init();
  103. }
  104. void bflb_Camera___init__(PikaObj *self) {
  105. pika_debug("cam init");
  106. if(!g_cam_inited) {
  107. init_cam(bflb_device_get_by_name("gpio"));
  108. g_cam_inited = 1;
  109. }
  110. }
  111. void bflb_Camera_start(PikaObj *self) {
  112. pika_debug("cam start");
  113. bflb_cam_start(cam0);
  114. }
  115. void bflb_Camera_stop(PikaObj *self) {
  116. pika_debug("cam stop");
  117. bflb_cam_stop(cam0);
  118. }
  119. int bflb_Camera_get_frame_count(PikaObj *self) {
  120. return (int)bflb_cam_get_frame_count(cam0);
  121. }
  122. PikaObj* bflb_Camera_get_frame_info(PikaObj *self) {
  123. uint8_t *pic_addr;
  124. uint32_t pic_size;
  125. pic_size = bflb_cam_get_frame_info(cam0, &pic_addr);
  126. obj_setInt(self, "pic_addr", (uintptr_t)pic_addr);
  127. obj_setInt(self, "pic_size", pic_size);
  128. return obj_newTuple(arg_newInt((uintptr_t)pic_addr), arg_newInt(pic_size));
  129. }
  130. void bflb_Camera_pop_one_frame(PikaObj *self) {
  131. bflb_cam_pop_one_frame(cam0);
  132. uint16_t* pic_addr = (uint16_t*)obj_getInt(self, "pic_addr");
  133. uint32_t pic_size = obj_getInt(self, "pic_size");
  134. pika_debug("pic_addr: %p, pic_size: %d", pic_addr, pic_size);
  135. for (size_t i = 0; i < pic_size / sizeof(uint16_t); i++) {
  136. pic_addr[i] = __bswap16(pic_addr[i]);
  137. }
  138. pika_debug("after bswap16");
  139. }
  140. static lv_obj_t *canvas_cam_create(lv_obj_t *parent);
  141. void demo(void) {
  142. bflb_cam_start(cam0);
  143. canvas_cam = canvas_cam_create(lv_scr_act());
  144. }
  145. void canvas_cam_demo_update(void *pic_addr) {
  146. lv_obj_t *canvas = canvas_cam;
  147. lv_canvas_set_buffer(canvas, pic_addr, 320, 240, LV_IMG_CF_TRUE_COLOR);
  148. }
  149. static lv_obj_t *canvas_cam_create(lv_obj_t *parent) {
  150. lv_obj_t *canvas;
  151. canvas = lv_canvas_create(parent);
  152. lv_obj_set_size(canvas, 320, 240);
  153. lv_obj_align(canvas, LV_ALIGN_TOP_MID, 0, 0);
  154. return canvas;
  155. }
  156. void bflb_Camera_demo(PikaObj *self) {
  157. demo();
  158. }
  159. void bflb_Camera_set_callback(PikaObj *self, Arg* callback){
  160. obj_setArg(self, "eventCallBack", callback);
  161. /* init event_listener for the first time */
  162. if (NULL == g_pika_bflb_event_listener) {
  163. pks_eventListener_init(&g_pika_bflb_event_listener);
  164. }
  165. uint32_t eventId = (uintptr_t)cam0;
  166. pks_eventListener_registEvent(g_pika_bflb_event_listener, eventId, self);
  167. g_cam_callback_inited = 1;
  168. pika_debug("bflb_Camera_set_callback: %p\r\n", eventId);
  169. }