imc_api.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * imc_api.c
  3. *
  4. * Created on: Jul 21, 2022
  5. * Author: yoojin.lim (yoojin.lim@etri.re.kr)
  6. */
  7. #include <stdio.h>
  8. #include "cmsis_os.h"
  9. #include "ImC/imc_api.h"
  10. #include "sc03mpd/sc03mpd.h"
  11. #include "ImC/decoder.h"
  12. /**
  13. * @brief turn Set of 4 LEDs ON and OFF after msec
  14. * @param argument:
  15. * ledSet : indicates LED 0~3
  16. * e.g. {1,0,1,0} for LED 0 and LED 2 are ON
  17. * length : number of LEDs (ledSet)
  18. * msec : ON duration
  19. * @retval None
  20. */
  21. void imc_led_on (uint8_t ledSet[], uint8_t length, uint32_t msec)
  22. {
  23. HAL_GPIO_WritePin(LED0_GPIO, LED0_GPIO_PIN, GPIO_PIN_RESET); // LED0 OFF
  24. HAL_GPIO_WritePin(LED1_GPIO, LED1_GPIO_PIN, GPIO_PIN_RESET); // LED1 OFF
  25. HAL_GPIO_WritePin(LED2_GPIO, LED2_GPIO_PIN, GPIO_PIN_RESET); // LED2 OFF
  26. HAL_GPIO_WritePin(LED3_GPIO, LED3_GPIO_PIN, GPIO_PIN_RESET); // LED3 OFF
  27. for ( uint8_t i = 0; i < length ; i++ ) {
  28. if ( ledSet[i] )
  29. {
  30. switch (i) { // LED ON
  31. case 0: HAL_GPIO_WritePin(LED0_GPIO, LED0_GPIO_PIN, GPIO_PIN_SET); break;
  32. case 1: HAL_GPIO_WritePin(LED1_GPIO, LED1_GPIO_PIN, GPIO_PIN_SET); break;
  33. case 2: HAL_GPIO_WritePin(LED2_GPIO, LED2_GPIO_PIN, GPIO_PIN_SET); break;
  34. case 3: HAL_GPIO_WritePin(LED3_GPIO, LED3_GPIO_PIN, GPIO_PIN_SET); break;
  35. }
  36. }
  37. }
  38. osDelay(msec);
  39. HAL_GPIO_WritePin(LED0_GPIO, LED0_GPIO_PIN, GPIO_PIN_RESET); // LED0 OFF
  40. HAL_GPIO_WritePin(LED1_GPIO, LED1_GPIO_PIN, GPIO_PIN_RESET); // LED1 OFF
  41. HAL_GPIO_WritePin(LED2_GPIO, LED2_GPIO_PIN, GPIO_PIN_RESET); // LED2 OFF
  42. HAL_GPIO_WritePin(LED3_GPIO, LED3_GPIO_PIN, GPIO_PIN_RESET); // LED3 OFF
  43. }
  44. /**
  45. * @brief turn Set of 4 LEDs ON and OFF after msec
  46. * @param argument:
  47. * ledSet : indicates LED 0~3
  48. * e.g. {1,0,1,0} for LED 0 and LED 2 are ON
  49. * length : number of LEDs (ledSet)
  50. * msec : blink period
  51. * @retval None
  52. */
  53. void imc_led_blink (uint8_t ledSet[], uint8_t length, uint32_t msec)
  54. {
  55. imc_led_on (ledSet, length, msec);
  56. osDelay(msec);
  57. }
  58. int32_t imc_sc03mpd_init (sc03mpd_ifx_t* ifx)
  59. {
  60. /* initial operation process :
  61. * 1) power up
  62. * 2) delay 2.5s
  63. * 3) reset command
  64. * 4) set image resolution command (currently 160x120)
  65. * 5) set image compressibility command
  66. * 6) reset command
  67. * Note: after setting the desired image size, you need to reset the camera first,
  68. * and then the new setting would be enabled.
  69. * OPTION for SKKU) manually step color, select black-white
  70. */
  71. #if !CAM_FAST_READY
  72. printf("[CAM] wait %dms for boot-up\r\n", DELAY_AFTER_POWERUP);
  73. osDelay (DELAY_AFTER_POWERUP);
  74. #endif
  75. //printf("[SC03MPD] init CAM ...\r\n");
  76. // (M33) moved to caller (static fn.)
  77. //MX_USART3_UART_Init();
  78. SC03MPD_ASSERT(sc03mpd_reset(ifx), "[CAM] FAILED reset", INIT_ERROR)
  79. SC03MPD_ASSERT(sc03mpd_set_ires(ifx, IMAGE_RESOLUTION), "[CAM] FAILED set resolution", INIT_ERROR)
  80. SC03MPD_ASSERT(sc03mpd_reset(ifx), "[CAM] FAILED reset for image setting", INIT_ERROR)
  81. SC03MPD_ASSERT(sc03mpd_set_icmp(ifx, 0x00), "[CAM] FAILED set compression rate", INIT_ERROR)
  82. #if CAM_BLACK_WHITE
  83. // after reset color mode setting is initialized
  84. SC03MPD_ASSERT(sc03mpd_reset(ifx), "[CAM] FAILED reset for image setting", INIT_ERROR)
  85. SC03MPD_ASSERT(sc03mpd_set_clr(ifx, SC03MPD_CLR_BW), "[CAM] FAILED set black-white", INIT_ERROR)
  86. #endif
  87. printf("[CAM] OK init\r\n");
  88. return CAM_STS_ENON;
  89. INIT_ERROR:
  90. return CAM_STS_EINI;
  91. }
  92. uint16_t ilen = 0;
  93. /* 2022. 8. 25. fix logical seq. error under V0706 protocol; stop -> read length -> read data -> resume */
  94. int32_t imc_sc03mpd_capture (sc03mpd_ifx_t* ifx) {
  95. //printf("[SC03MPD] start capture ...\r\n");
  96. /* CAPTURE */
  97. SC03MPD_ASSERT(sc03mpd_capture(ifx), "[CAM] FAILED capture", CAPTURE_ERROR)
  98. osDelay (DELAY_AFTER_CAPTURE);
  99. /* GET length */
  100. ilen = 0;
  101. SC03MPD_ASSERT(sc03mpd_get_ilen(ifx, &ilen), "[CAM] FAILED get length", CAPTURE_ERROR)
  102. printf("[CAM] OK capture (length = %d, %04X)\r\n", ilen, ilen);
  103. osDelay (DELAY_AFTER_LENGTH);
  104. /* READ image data */
  105. //printf("[SC03MPD] download to 0x%08X\r\n", IMG_RAM_ADDR);
  106. uint8_t* imem = (uint8_t*)IMG_RAM_ADDR;
  107. uint16_t addr = 0;
  108. uint16_t nreq = 0;
  109. uint16_t rest = ilen;
  110. uint32_t tsStart = 0;
  111. uint32_t tsEnd = 0;
  112. tsStart = HAL_GetTick();
  113. while (rest > 0)
  114. {
  115. nreq = (rest > IMG_BLK_SIZE)? IMG_BLK_SIZE : rest;
  116. if (sc03mpd_get_idat(ifx, addr, nreq, imem))
  117. {
  118. printf("[CAM] FAILED download: %04X (%d)\r\n", addr, nreq);
  119. goto CAPTURE_ERROR;
  120. }
  121. //printf ("#READ: %04X (%d), DATA: %02X\r\n", addr, nreq, *imem);
  122. rest -= nreq;
  123. addr += nreq;
  124. imem += nreq;
  125. }
  126. tsEnd = HAL_GetTick();
  127. printf("[CAM] OK download; %lu ticks, 0x%08X to 0x%08X\r\n", tsEnd-tsStart, IMG_RAM_ADDR, IMG_RAM_ADDR+ilen);
  128. #if CAM_IMAGE_FIRST_TWO_BYTES
  129. /* Note: JPEG IMAGE DATA must be FF D8 in first and FF D9 in end.
  130. * 2022. 7. 20. ETRI can not assure that the EOI is FF D9.
  131. * */
  132. printf("[SC03MPD] image data (first 2 bytes): ");
  133. uint8_t* aimg = (uint8_t*)IMG_RAM_ADDR;
  134. for ( uint16_t i = 0; i < 2; i++, aimg++ )
  135. printf("%02X ", *aimg);
  136. printf("\r\n");
  137. printf("\r\n");
  138. #endif
  139. /* STOP CAPTURE = RESUME */
  140. SC03MPD_ASSERT(sc03mpd_stop(ifx), "[CAM] FAILED stop", CAPTURE_ERROR)
  141. printf("[CAM] OK stop capture\r\n");
  142. return CAM_STS_ENON;
  143. CAPTURE_ERROR:
  144. return CAM_STS_ECAP;
  145. }
  146. int32_t imc_sc03mpd_reset (sc03mpd_ifx_t* ifx)
  147. {
  148. SC03MPD_ASSERT(sc03mpd_reset(ifx), "[CAM] FAILED reset", RESET_ERROR)
  149. printf("[CAM] OK reset\r\n");
  150. return CAM_STS_ENON;
  151. RESET_ERROR:
  152. return CAM_STS_EINI;
  153. }
  154. void imc_sbc_power_off (uint32_t msec)
  155. {
  156. printf("[EPS] SBC OFF\r\n");
  157. osDelay(msec);
  158. HAL_GPIO_WritePin(SBC_POWER_GPIO, SBC_POWER_GPIO_PIN, POWER_OFF);
  159. }
  160. void imc_sbc_power_on (uint32_t msec)
  161. {
  162. printf("[EPS] SBC on\r\n");
  163. osDelay(msec);
  164. HAL_GPIO_WritePin(SBC_POWER_GPIO, SBC_POWER_GPIO_PIN, POWER_ON);
  165. }
  166. void imc_cam_power_off (uint32_t msec)
  167. {
  168. printf("[EPS] CAM OFF\r\n");
  169. osDelay(msec);
  170. HAL_GPIO_WritePin(CAM_POWER_GPIO, CAM_POWER_GPIO_PIN, POWER_OFF);
  171. }
  172. void imc_cam_power_on (uint32_t msec)
  173. {
  174. printf("[EPS] CAM ON\r\n");
  175. osDelay(msec);
  176. HAL_GPIO_WritePin(CAM_POWER_GPIO, CAM_POWER_GPIO_PIN, POWER_ON);
  177. }
  178. /*
  179. * energy level : 0~7 with 3bits of GPIO pins
  180. * ignore level update between reading each pin
  181. *
  182. * (2022.11.10.) read the level three times (per 3ms), and return lower level
  183. *
  184. */
  185. uint8_t imc_get_energy_level ()
  186. {
  187. uint8_t curr = 8;
  188. uint8_t bigger = 0;
  189. for (uint8_t i = 0; i < 3; i ++)
  190. {
  191. curr = (HAL_GPIO_ReadPin(ENERGY_LV0_GPIO, ENERGY_LV0_GPIO_PIN)
  192. + (HAL_GPIO_ReadPin(ENERGY_LV1_GPIO, ENERGY_LV1_GPIO_PIN) << 1)
  193. + (HAL_GPIO_ReadPin(ENERGY_LV2_GPIO, ENERGY_LV2_GPIO_PIN) << 2));
  194. if ( (curr > bigger) && (curr > 0) )
  195. bigger = curr;
  196. osDelay(DELAY_GET_ENERGY_LEVEL);
  197. }
  198. return bigger;
  199. }