luat_io_queue_air105.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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 "ioqueue"
  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) & 0xffffffff;
  46. msg.arg2 = tick & 0xffffffff;
  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, uint32_t *repeat_cnt, uint32_t *cmd_cnt)
  61. {
  62. if (hw_timer_id >= HW_TIMER_MAX) return;
  63. HWTimer_Stop(hw_timer_id);
  64. HWTimer_GetResultOperationInfo(hw_timer_id, repeat_cnt, cmd_cnt);
  65. }
  66. void luat_io_queue_clear(uint8_t hw_timer_id)
  67. {
  68. if (hw_timer_id >= HW_TIMER_MAX) return;
  69. HWTimer_ClearOperationQueue(hw_timer_id);
  70. }
  71. void luat_io_queue_release(uint8_t hw_timer_id)
  72. {
  73. if (hw_timer_id >= HW_TIMER_MAX) return;
  74. HWTimer_FreeOperationQueue(hw_timer_id);
  75. }
  76. void luat_io_queue_set_delay(uint8_t hw_timer_id, uint16_t time, uint8_t sub_tick, uint8_t is_continue)
  77. {
  78. if (hw_timer_id >= HW_TIMER_MAX) return;
  79. OPQueue_CmdStruct cmd;
  80. cmd.CB = NULL;
  81. cmd.Operation = is_continue?OP_QUEUE_CMD_CONTINUE_DELAY:OP_QUEUE_CMD_ONE_TIME_DELAY;
  82. cmd.PinOrDelay = sub_tick;
  83. cmd.uArg.Time = time;
  84. HWTimer_AddOperation(hw_timer_id, &cmd);
  85. }
  86. void luat_io_queue_repeat_delay(uint8_t hw_timer_id)
  87. {
  88. if (hw_timer_id >= HW_TIMER_MAX) return;
  89. OPQueue_CmdStruct cmd;
  90. cmd.CB = NULL;
  91. cmd.Operation = OP_QUEUE_CMD_REPEAT_DELAY;
  92. HWTimer_AddOperation(hw_timer_id, &cmd);
  93. }
  94. 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)
  95. {
  96. if (hw_timer_id >= HW_TIMER_MAX) return;
  97. OPQueue_CmdStruct cmd;
  98. cmd.CB = NULL;
  99. cmd.Operation = is_input?OP_QUEUE_CMD_SET_GPIO_DIR_IN:OP_QUEUE_CMD_SET_GPIO_DIR_OUT;
  100. cmd.PinOrDelay = pin;
  101. cmd.uArg.IOArg.Level = level;
  102. cmd.uArg.IOArg.PullMode = pull_mode;
  103. HWTimer_AddOperation(hw_timer_id, &cmd);
  104. }
  105. void luat_io_queue_add_io_out(uint8_t hw_timer_id, uint8_t pin, uint8_t level)
  106. {
  107. if (hw_timer_id >= HW_TIMER_MAX) return;
  108. OPQueue_CmdStruct cmd;
  109. cmd.CB = NULL;
  110. cmd.Operation = OP_QUEUE_CMD_GPIO_OUT;
  111. cmd.PinOrDelay = pin;
  112. cmd.uArg.IOArg.Level = level;
  113. HWTimer_AddOperation(hw_timer_id, &cmd);
  114. }
  115. void luat_io_queue_add_io_in(uint8_t hw_timer_id, uint8_t pin, CBFuncEx_t CB, void *user_data)
  116. {
  117. if (hw_timer_id >= HW_TIMER_MAX) return;
  118. OPQueue_CmdStruct cmd;
  119. cmd.CB = CB;
  120. cmd.uParam.pParam = user_data;
  121. cmd.Operation = CB?OP_QUEUE_CMD_GPIO_IN_CB:OP_QUEUE_CMD_GPIO_IN;
  122. cmd.PinOrDelay = pin;
  123. HWTimer_AddOperation(hw_timer_id, &cmd);
  124. }
  125. 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)
  126. {
  127. if (hw_timer_id >= HW_TIMER_MAX) return;
  128. OPQueue_CmdStruct cmd;
  129. cmd.CB = NULL;
  130. cmd.Operation = OP_QUEUE_CMD_CAPTURE_SET;
  131. cmd.PinOrDelay = pin;
  132. cmd.uParam.MaxCnt = max_tick;
  133. cmd.uArg.ExitArg.ExtiMode = irq_mode;
  134. cmd.uArg.ExitArg.PullMode = pull_mode;
  135. HWTimer_AddOperation(hw_timer_id, &cmd);
  136. }
  137. void luat_io_queue_capture(uint8_t hw_timer_id, CBFuncEx_t CB, void *user_data)
  138. {
  139. if (hw_timer_id >= HW_TIMER_MAX) return;
  140. OPQueue_CmdStruct cmd;
  141. cmd.CB = CB;
  142. cmd.PinOrDelay = 0xff;
  143. cmd.uParam.pParam = user_data;
  144. cmd.Operation = CB?OP_QUEUE_CMD_CAPTURE_CB:OP_QUEUE_CMD_CAPTURE;
  145. HWTimer_AddOperation(hw_timer_id, &cmd);
  146. }
  147. void luat_io_queue_capture_end(uint8_t hw_timer_id, uint8_t pin)
  148. {
  149. if (hw_timer_id >= HW_TIMER_MAX) return;
  150. OPQueue_CmdStruct cmd;
  151. cmd.CB = NULL;
  152. cmd.Operation = OP_QUEUE_CMD_CAPTURE_END;
  153. cmd.PinOrDelay = pin;
  154. HWTimer_AddOperation(hw_timer_id, &cmd);
  155. }
  156. void luat_io_queue_end(uint8_t hw_timer_id)
  157. {
  158. if (hw_timer_id >= HW_TIMER_MAX) return;
  159. OPQueue_CmdStruct cmd;
  160. cmd.Operation = OP_QUEUE_CMD_END;
  161. HWTimer_AddOperation(hw_timer_id, &cmd);
  162. }
  163. uint8_t luat_io_queue_check_done(uint8_t hw_timer_id)
  164. {
  165. if (hw_timer_id >= HW_TIMER_MAX) return 0;
  166. return HWTimer_CheckOperationQueueDone(hw_timer_id);
  167. }
  168. int luat_io_queue_get_size(uint8_t hw_timer_id)
  169. {
  170. if (hw_timer_id >= HW_TIMER_MAX) return 0;
  171. return HWTimer_GetOperationQueueLen(hw_timer_id);
  172. }
  173. 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)
  174. {
  175. if ((hw_timer_id >= HW_TIMER_MAX) || !HWTimer_GetOperationQueue(hw_timer_id))
  176. {
  177. *input_cnt = 0;
  178. *capture_cnt = 0;
  179. }
  180. else
  181. {
  182. OPQueue_CmdStruct *Cmd = HWTimer_GetOperationQueue(hw_timer_id);
  183. uint32_t len = HWTimer_GetOperationQueueLen(hw_timer_id);
  184. uint32_t input_pos = 0;
  185. uint32_t capture_pos = 0;
  186. uint32_t i;
  187. for(i = 0; i < len; i++)
  188. {
  189. switch(Cmd[i].Operation)
  190. {
  191. case OP_QUEUE_CMD_GPIO_IN:
  192. case OP_QUEUE_CMD_GPIO_IN_CB:
  193. input_buff[input_pos * 2] = Cmd[i].PinOrDelay;
  194. input_buff[input_pos * 2 + 1] = Cmd[i].uArg.IOArg.Level;
  195. input_pos++;
  196. break;
  197. case OP_QUEUE_CMD_CAPTURE:
  198. case OP_QUEUE_CMD_CAPTURE_CB:
  199. if (Cmd[i].PinOrDelay >= GPIO_NONE)
  200. {
  201. len = 0;
  202. break;
  203. }
  204. capture_buff[capture_pos * 6] = Cmd[i].PinOrDelay;
  205. capture_buff[capture_pos * 6 + 1] = Cmd[i].uArg.IOArg.Level;
  206. BytesPutLe32(&capture_buff[capture_pos * 6 + 2], Cmd[i].uParam.MaxCnt);
  207. capture_pos++;
  208. break;
  209. }
  210. }
  211. *input_cnt = input_pos;
  212. *capture_cnt = capture_pos;
  213. }
  214. return ;
  215. }
  216. void luat_io_queue_capture_start_with_sys_tick(uint8_t pin, uint8_t pull_mode, uint8_t irq_mode)
  217. {
  218. GPIO_PullConfig(pin, pull_mode, (pull_mode > 1)?0:1);
  219. GPIO_Config(pin, 1, 0);
  220. GPIO_ExtiSetCB(pin, luat_io_queue_capture_cb, NULL);
  221. switch(irq_mode)
  222. {
  223. case OP_QUEUE_CMD_IO_EXTI_BOTH:
  224. GPIO_ExtiConfig(pin, 0, 1, 1);
  225. break;
  226. case OP_QUEUE_CMD_IO_EXTI_UP:
  227. GPIO_ExtiConfig(pin, 0, 1, 0);
  228. break;
  229. case OP_QUEUE_CMD_IO_EXTI_DOWN:
  230. GPIO_ExtiConfig(pin, 0, 0, 1);
  231. break;
  232. }
  233. }
  234. void luat_io_queue_capture_end_with_sys_tick(uint8_t pin)
  235. {
  236. GPIO_ExtiSetCB(pin, NULL, NULL);
  237. GPIO_ExtiConfig(pin, 0, 0, 0);
  238. }