luat_io_queue_air105.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. * Copyright (c) 2022 OpenLuat & AirM2M
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  5. * this software and associated documentation files (the "Software"), to deal in
  6. * the Software without restriction, including without limitation the rights to
  7. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  8. * the Software, and to permit persons to whom the Software is furnished to do so,
  9. * subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in all
  12. * copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  16. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  17. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  18. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  19. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. */
  21. #include "luat_base.h"
  22. #include "luat_msgbus.h"
  23. #include "luat_lib_io_queue.h"
  24. #include "app_interface.h"
  25. #define LUAT_LOG_TAG "hwtimer"
  26. #include "luat_log.h"
  27. static int32_t luat_io_queue_done_cb(void *pData, void *pParam)
  28. {
  29. rtos_msg_t msg;
  30. msg.handler = l_io_queue_done_handler;
  31. msg.ptr = pParam;
  32. luat_msgbus_put(&msg, 1);
  33. return 0;
  34. }
  35. static int32_t __FUNC_IN_RAM__ luat_io_queue_dummy_cb(void *pData, void *pParam)
  36. {
  37. return 0;
  38. }
  39. static int32_t __FUNC_IN_RAM__ luat_io_queue_capture_cb(void *pData, void *pParam)
  40. {
  41. rtos_msg_t msg;
  42. uint64_t tick = GetSysTick();
  43. msg.handler = l_io_queue_capture_handler;
  44. msg.ptr = ((uint32_t)pData << 8) | GPIO_Input(pData);
  45. msg.arg1 = (tick >> 32);
  46. msg.arg2 = tick & 0x0ffffffff;
  47. luat_msgbus_put(&msg, 1);
  48. return 0;
  49. }
  50. void luat_io_queue_init(uint8_t hw_timer_id, uint32_t cmd_cnt, uint32_t repeat_cnt)
  51. {
  52. if (hw_timer_id >= HW_TIMER_MAX) return;
  53. HWTimer_InitOperationQueue(hw_timer_id, cmd_cnt, repeat_cnt, luat_io_queue_done_cb, hw_timer_id);
  54. }
  55. void luat_io_queue_start(uint8_t hw_timer_id)
  56. {
  57. if (hw_timer_id >= HW_TIMER_MAX) return;
  58. HWTimer_StartOperationQueue(hw_timer_id);
  59. }
  60. void luat_io_queue_stop(uint8_t hw_timer_id)
  61. {
  62. if (hw_timer_id >= HW_TIMER_MAX) return;
  63. HWTimer_Stop(hw_timer_id);
  64. }
  65. void luat_io_queue_clear(uint8_t hw_timer_id)
  66. {
  67. if (hw_timer_id >= HW_TIMER_MAX) return;
  68. HWTimer_ClearOperationQueue(hw_timer_id);
  69. }
  70. void luat_io_queue_release(uint8_t hw_timer_id)
  71. {
  72. if (hw_timer_id >= HW_TIMER_MAX) return;
  73. HWTimer_FreeOperationQueue(hw_timer_id);
  74. }
  75. void luat_io_queue_set_delay(uint8_t hw_timer_id, uint16_t time, uint8_t sub_tick, uint8_t is_continue)
  76. {
  77. if (hw_timer_id >= HW_TIMER_MAX) return;
  78. OPQueue_CmdStruct cmd;
  79. cmd.CB = NULL;
  80. cmd.Operation = is_continue?OP_QUEUE_CMD_CONTINUE_DELAY:OP_QUEUE_CMD_ONE_TIME_DELAY;
  81. cmd.PinOrDelay = sub_tick;
  82. cmd.uArg.Time = time;
  83. HWTimer_AddOperation(hw_timer_id, &cmd);
  84. }
  85. void luat_io_queue_repeat_delay(uint8_t hw_timer_id)
  86. {
  87. if (hw_timer_id >= HW_TIMER_MAX) return;
  88. OPQueue_CmdStruct cmd;
  89. cmd.CB = NULL;
  90. cmd.Operation = OP_QUEUE_CMD_REPEAT_DELAY;
  91. HWTimer_AddOperation(hw_timer_id, &cmd);
  92. }
  93. void luat_io_queue_add_io_config(uint8_t hw_timer_id, uint8_t pin, uint8_t is_input, uint8_t pull_mode, uint8_t level)
  94. {
  95. if (hw_timer_id >= HW_TIMER_MAX) return;
  96. OPQueue_CmdStruct cmd;
  97. cmd.CB = NULL;
  98. cmd.Operation = is_input?OP_QUEUE_CMD_SET_GPIO_DIR_IN:OP_QUEUE_CMD_SET_GPIO_DIR_OUT;
  99. cmd.PinOrDelay = pin;
  100. cmd.uArg.IOArg.Level = level;
  101. cmd.uArg.IOArg.PullMode = pull_mode;
  102. HWTimer_AddOperation(hw_timer_id, &cmd);
  103. }
  104. void luat_io_queue_add_io_out(uint8_t hw_timer_id, uint8_t pin, uint8_t level)
  105. {
  106. if (hw_timer_id >= HW_TIMER_MAX) return;
  107. OPQueue_CmdStruct cmd;
  108. cmd.CB = NULL;
  109. cmd.Operation = OP_QUEUE_CMD_GPIO_OUT;
  110. cmd.PinOrDelay = pin;
  111. cmd.uArg.IOArg.Level = level;
  112. HWTimer_AddOperation(hw_timer_id, &cmd);
  113. }
  114. void luat_io_queue_add_io_in(uint8_t hw_timer_id, uint8_t pin, CBFuncEx_t CB, void *user_data)
  115. {
  116. if (hw_timer_id >= HW_TIMER_MAX) return;
  117. OPQueue_CmdStruct cmd;
  118. cmd.CB = CB;
  119. cmd.uParam.pParam = user_data;
  120. cmd.Operation = CB?OP_QUEUE_CMD_GPIO_IN_CB:OP_QUEUE_CMD_GPIO_IN;
  121. cmd.PinOrDelay = pin;
  122. HWTimer_AddOperation(hw_timer_id, &cmd);
  123. }
  124. void luat_io_queue_capture_set(uint8_t hw_timer_id, uint32_t max_tick, uint8_t pin, uint8_t pull_mode, uint8_t irq_mode)
  125. {
  126. if (hw_timer_id >= HW_TIMER_MAX) return;
  127. OPQueue_CmdStruct cmd;
  128. cmd.CB = NULL;
  129. cmd.Operation = OP_QUEUE_CMD_CAPTURE_SET;
  130. cmd.PinOrDelay = pin;
  131. cmd.uParam.MaxCnt = max_tick;
  132. cmd.uArg.ExitArg.ExtiMode = irq_mode;
  133. cmd.uArg.ExitArg.PullMode = pull_mode;
  134. HWTimer_AddOperation(hw_timer_id, &cmd);
  135. }
  136. void luat_io_queue_capture(uint8_t hw_timer_id, CBFuncEx_t CB, void *user_data)
  137. {
  138. if (hw_timer_id >= HW_TIMER_MAX) return;
  139. OPQueue_CmdStruct cmd;
  140. cmd.CB = CB;
  141. cmd.PinOrDelay = 0xff;
  142. cmd.uParam.pParam = user_data;
  143. cmd.Operation = CB?OP_QUEUE_CMD_CAPTURE_CB:OP_QUEUE_CMD_CAPTURE;
  144. HWTimer_AddOperation(hw_timer_id, &cmd);
  145. }
  146. void luat_io_queue_capture_end(uint8_t hw_timer_id, uint8_t pin)
  147. {
  148. if (hw_timer_id >= HW_TIMER_MAX) return;
  149. OPQueue_CmdStruct cmd;
  150. cmd.CB = NULL;
  151. cmd.Operation = OP_QUEUE_CMD_CAPTURE_END;
  152. cmd.PinOrDelay = pin;
  153. HWTimer_AddOperation(hw_timer_id, &cmd);
  154. }
  155. void luat_io_queue_end(uint8_t hw_timer_id)
  156. {
  157. if (hw_timer_id >= HW_TIMER_MAX) return;
  158. OPQueue_CmdStruct cmd;
  159. cmd.Operation = OP_QUEUE_CMD_END;
  160. HWTimer_AddOperation(hw_timer_id, &cmd);
  161. }
  162. uint8_t luat_io_queue_check_done(uint8_t hw_timer_id)
  163. {
  164. if (hw_timer_id >= HW_TIMER_MAX) return 0;
  165. return HWTimer_CheckOperationQueueDone(hw_timer_id);
  166. }
  167. int luat_io_queue_get_size(uint8_t hw_timer_id)
  168. {
  169. if (hw_timer_id >= HW_TIMER_MAX) return 0;
  170. return HWTimer_GetOperationQueueLen(hw_timer_id);
  171. }
  172. void luat_io_queue_get_data(uint8_t hw_timer_id, uint8_t *input_buff, uint32_t *input_cnt, uint8_t *capture_buff, uint32_t *capture_cnt)
  173. {
  174. if ((hw_timer_id >= HW_TIMER_MAX) || !HWTimer_GetOperationQueue(hw_timer_id))
  175. {
  176. *input_cnt = 0;
  177. *capture_cnt = 0;
  178. }
  179. else
  180. {
  181. OPQueue_CmdStruct *Cmd = HWTimer_GetOperationQueue(hw_timer_id);
  182. uint32_t len = HWTimer_GetOperationQueueLen(hw_timer_id);
  183. uint32_t input_pos = 0;
  184. uint32_t capture_pos = 0;
  185. uint32_t i;
  186. for(i = 0; i < len; i++)
  187. {
  188. switch(Cmd[i].Operation)
  189. {
  190. case OP_QUEUE_CMD_GPIO_IN:
  191. case OP_QUEUE_CMD_GPIO_IN_CB:
  192. input_buff[input_pos * 2] = Cmd[i].PinOrDelay;
  193. input_buff[input_pos * 2 + 1] = Cmd[i].uArg.IOArg.Level;
  194. input_pos++;
  195. break;
  196. case OP_QUEUE_CMD_CAPTURE:
  197. case OP_QUEUE_CMD_CAPTURE_CB:
  198. if (Cmd[i].PinOrDelay >= GPIO_NONE)
  199. {
  200. len = 0;
  201. break;
  202. }
  203. capture_buff[capture_pos * 6] = Cmd[i].PinOrDelay;
  204. capture_buff[capture_pos * 6 + 1] = Cmd[i].uArg.IOArg.Level;
  205. BytesPutLe32(&capture_buff[capture_pos * 6 + 2], Cmd[i].uParam.MaxCnt);
  206. capture_pos++;
  207. break;
  208. }
  209. }
  210. *input_cnt = input_pos;
  211. *capture_cnt = capture_pos;
  212. }
  213. return ;
  214. }
  215. void luat_io_queue_capture_start_with_sys_tick(uint8_t pin, uint8_t pull_mode, uint8_t irq_mode)
  216. {
  217. GPIO_PullConfig(pin, pull_mode, (pull_mode > 1)?0:1);
  218. GPIO_Config(pin, 1, 0);
  219. GPIO_ExtiSetCB(pin, luat_io_queue_capture_cb, NULL);
  220. switch(irq_mode)
  221. {
  222. case OP_QUEUE_CMD_IO_EXTI_BOTH:
  223. GPIO_ExtiConfig(pin, 0, 1, 1);
  224. break;
  225. case OP_QUEUE_CMD_IO_EXTI_UP:
  226. GPIO_ExtiConfig(pin, 0, 1, 0);
  227. break;
  228. case OP_QUEUE_CMD_IO_EXTI_DOWN:
  229. GPIO_ExtiConfig(pin, 0, 0, 1);
  230. break;
  231. }
  232. }
  233. void luat_io_queue_capture_end_with_sys_tick(uint8_t pin)
  234. {
  235. GPIO_ExtiSetCB(pin, NULL, NULL);
  236. GPIO_ExtiConfig(pin, 0, 0, 0);
  237. }