rtlink_hw.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-02-02 xiangxistu the first version
  9. * 2021-05-08 Sherman Optimize the operation function on the rt_link_receive_buffer
  10. */
  11. #include <rtthread.h>
  12. #include <rtlink.h>
  13. #include <rtlink_hw.h>
  14. #include <rtlink_port.h>
  15. #include <rtlink_utils.h>
  16. #define DBG_TAG "rtlink_hw"
  17. #ifdef USING_RT_LINK_HW_DEBUG
  18. #define DBG_LVL DBG_LOG
  19. #else
  20. #define DBG_LVL DBG_INFO
  21. #endif
  22. #define DBG_COLOR
  23. #include <rtdbg.h>
  24. static struct rt_link_receive_buffer *rx_buffer = RT_NULL;
  25. struct rt_link_receive_buffer *rt_link_hw_buffer_init(void *parameter)
  26. {
  27. rx_buffer = rt_malloc(sizeof(struct rt_link_receive_buffer));
  28. if (rx_buffer != RT_NULL)
  29. {
  30. rt_memset(rx_buffer, 0, sizeof(struct rt_link_receive_buffer));
  31. rx_buffer->read_point = rx_buffer->data;
  32. rx_buffer->write_point = rx_buffer->data;
  33. rx_buffer->end_point = rx_buffer->data + RT_LINK_RECEIVE_BUFFER_LENGTH; /* Point to memory that has no access rights */
  34. }
  35. else
  36. {
  37. LOG_E("receive buffer alloc failed, init failed.");
  38. }
  39. return rx_buffer;
  40. }
  41. static rt_size_t rt_link_hw_buffer_write(void *data, rt_size_t count)
  42. {
  43. int surplus = 0;
  44. if (rx_buffer == RT_NULL)
  45. {
  46. return 0;
  47. }
  48. /* (data)----(r)----(w)----(end) */
  49. if (rx_buffer->write_point >= rx_buffer->read_point)
  50. {
  51. rt_size_t w2end = rx_buffer->end_point - rx_buffer->write_point;
  52. surplus = RT_LINK_RECEIVE_BUFFER_LENGTH - (rx_buffer->write_point - rx_buffer->read_point);
  53. count = count > surplus ? surplus : count;
  54. if (count >= w2end)
  55. {
  56. rt_memcpy(rx_buffer->write_point, data, w2end);
  57. rx_buffer->write_point = rx_buffer->data;
  58. rt_memcpy(rx_buffer->write_point, (rt_uint8_t *)data + w2end, (count - w2end));
  59. rx_buffer->write_point += (count - w2end);
  60. }
  61. else
  62. {
  63. rt_memcpy(rx_buffer->write_point, data, count);
  64. rx_buffer->write_point += count;
  65. }
  66. }
  67. else /* (data)----(w)----(r)----(end) */
  68. {
  69. surplus = rx_buffer->read_point - rx_buffer->write_point;
  70. count = count > surplus ? surplus : count;
  71. rt_memcpy(rx_buffer->write_point, data, count);
  72. rx_buffer->write_point += count;
  73. }
  74. return count;
  75. }
  76. /* increases buffer pointer by one and circle around if necessary */
  77. void rt_link_hw_buffer_point_shift(rt_uint8_t **pointer_address, rt_size_t length)
  78. {
  79. rt_uint8_t *pointer = *pointer_address + length;
  80. if (rx_buffer->write_point >= rx_buffer->read_point)
  81. {
  82. if (pointer >= rx_buffer->write_point)
  83. {
  84. *pointer_address = rx_buffer->write_point;
  85. }
  86. else
  87. {
  88. *pointer_address = pointer;
  89. }
  90. }
  91. else
  92. {
  93. if (pointer >= rx_buffer->end_point)
  94. {
  95. *pointer_address = rx_buffer->data;
  96. pointer = pointer - rx_buffer->end_point + rx_buffer->data;
  97. if (pointer >= rx_buffer->write_point)
  98. {
  99. *pointer_address = rx_buffer->write_point;
  100. }
  101. else
  102. {
  103. *pointer_address = pointer;
  104. }
  105. }
  106. else
  107. {
  108. *pointer_address = pointer;
  109. }
  110. }
  111. }
  112. /* copy data from receive buffer */
  113. void rt_link_hw_copy(rt_uint8_t *dst, rt_uint8_t *src, rt_size_t count)
  114. {
  115. rt_uint8_t *pointer = RT_NULL;
  116. pointer = src + count;
  117. if (pointer >= rx_buffer->end_point)
  118. {
  119. rt_size_t offset = 0;
  120. offset = rx_buffer->end_point - src;
  121. rt_memcpy(dst, src, offset);
  122. rt_memcpy(dst + offset, rx_buffer->data, pointer - rx_buffer->end_point);
  123. }
  124. else
  125. {
  126. rt_memcpy(dst, src, count);
  127. }
  128. }
  129. /* Length of data received */
  130. rt_size_t rt_link_hw_recv_len(struct rt_link_receive_buffer *buffer)
  131. {
  132. if (buffer == RT_NULL)
  133. {
  134. return 0;
  135. }
  136. if (buffer->write_point >= buffer->read_point)
  137. {
  138. return (buffer->write_point - buffer->read_point);
  139. }
  140. else
  141. {
  142. return (RT_LINK_RECEIVE_BUFFER_LENGTH - (buffer->read_point - buffer->write_point));
  143. }
  144. }
  145. rt_err_t rt_link_reset_crc32(void)
  146. {
  147. #ifdef RT_LINK_USING_HW_CRC
  148. return rt_link_hw_crc32_reset();
  149. #else
  150. return rt_link_sf_crc32_reset();
  151. #endif
  152. }
  153. rt_uint32_t rt_link_crc32(rt_uint8_t *data, rt_size_t u32_size)
  154. {
  155. #ifdef RT_LINK_USING_HW_CRC
  156. return rt_link_hw_crc32(data, u32_size);
  157. #else
  158. return rt_link_sf_crc32(data, u32_size);
  159. #endif
  160. }
  161. rt_uint32_t rt_link_get_crc(rt_uint8_t using_buffer_ring, rt_uint8_t *data, rt_size_t size)
  162. {
  163. rt_uint32_t crc32 = 0x0;
  164. rt_size_t surplus = 0;
  165. if (data == RT_NULL)
  166. {
  167. LOG_D("warning, the parameter error: %d, data: 0x%08d.", size, data);
  168. return 0;
  169. }
  170. rt_link_reset_crc32();
  171. if (using_buffer_ring == 1)
  172. {
  173. /* modify the missing character */
  174. surplus = rx_buffer->end_point - data;
  175. if (surplus >= size)
  176. {
  177. crc32 = rt_link_crc32(data, size);
  178. }
  179. else
  180. {
  181. rt_link_crc32(data, surplus);
  182. crc32 = rt_link_crc32(rx_buffer->data, size - surplus);
  183. }
  184. }
  185. else
  186. {
  187. crc32 = rt_link_crc32(data, size);
  188. }
  189. return crc32;
  190. }
  191. rt_size_t rt_link_hw_send(void *data, rt_size_t length)
  192. {
  193. rt_size_t send_len = 0;
  194. send_len = rt_link_port_send(data, length);
  195. if (send_len <= 0)
  196. {
  197. rt_link_port_reconnect();
  198. send_len = rt_link_port_send(data, length);
  199. }
  200. return send_len;
  201. }
  202. rt_size_t rt_link_hw_write_cb(void *data, rt_size_t length)
  203. {
  204. /* write real data into rtlink receive buffer */
  205. rt_size_t len = rt_link_hw_buffer_write(data, length);
  206. struct rt_link_session *scb = rt_link_get_scb();
  207. if (scb)
  208. {
  209. rt_event_send(&scb->event, RT_LINK_READ_CHECK_EVENT);
  210. }
  211. return len;
  212. }
  213. rt_err_t rt_link_hw_init(void)
  214. {
  215. struct rt_link_session *scb = rt_link_get_scb();
  216. if ((rx_buffer != RT_NULL) || (scb == RT_NULL))
  217. {
  218. return -RT_ERROR;
  219. }
  220. /* alloc receive buffer to store data */
  221. if (rt_link_hw_buffer_init(RT_NULL) == RT_NULL)
  222. {
  223. return -RT_ENOMEM;
  224. }
  225. scb->rx_buffer = rx_buffer;
  226. scb->calculate_crc = rt_link_get_crc;
  227. if (RT_EOK != rt_link_port_init())
  228. {
  229. return -RT_ERROR;
  230. }
  231. #ifdef LINK_LAYER_USING_HW_CRC
  232. /* crc hardware device for mcu and node */
  233. if (RT_EOK != rt_link_hw_crc32_init())
  234. {
  235. return -RT_ERROR;
  236. }
  237. #endif
  238. LOG_I("link layer hardware environment init successful.");
  239. return RT_EOK;
  240. }
  241. rt_err_t rt_link_hw_deinit(void)
  242. {
  243. if (rx_buffer)
  244. {
  245. rt_free(rx_buffer);
  246. rx_buffer = RT_NULL;
  247. }
  248. struct rt_link_session *scb = rt_link_get_scb();
  249. if (scb)
  250. {
  251. scb->rx_buffer = rx_buffer;
  252. scb->calculate_crc = RT_NULL;
  253. }
  254. if (RT_EOK != rt_link_port_deinit())
  255. {
  256. return -RT_ERROR;
  257. }
  258. #ifdef LINK_LAYER_USING_HW_CRC
  259. /* crc hardware device for mcu and node */
  260. if (RT_EOK != rt_link_hw_crc32_deinit())
  261. {
  262. return -RT_ERROR;
  263. }
  264. #endif
  265. LOG_I("rtlink hardware deinit successful.");
  266. return RT_EOK;
  267. }