soft_gs.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. #include "soft_gs.h"
  2. #include "common.h"
  3. #include "drv_usart.h"
  4. #include "gcs_vklink_v30.h"
  5. #include "params.h"
  6. #include "rkfifo.h"
  7. #include "soft_time.h"
  8. #include "vklink.h"
  9. struct GCS_Link gcs_link = {
  10. .link_protocal_type =
  11. GCS_LINK_PROTOCAL_TYPE_VKLINK, /* 默认使用 VKLINK 协议 */
  12. .vklink_protocal_version = 300, /* 默认 vklink v300 版本 */
  13. .link_status = COMP_NOEXIST, /* 默认状态是未连接地面站 */
  14. };
  15. /**
  16. * @brief 设置地面站 vklink 的协议版本
  17. *
  18. * @param version
  19. */
  20. void gs_set_vklink_protocal_version(struct GCS_Link *gcs, uint16_t version)
  21. {
  22. switch (version)
  23. {
  24. case 300:
  25. case 400:
  26. gcs->vklink_protocal_version = version;
  27. break;
  28. default:
  29. break;
  30. }
  31. }
  32. /**
  33. * @brief 电台串口初始化
  34. *
  35. * @param gs_bps 串口波特率
  36. */
  37. int gcs_init(struct GCS_Link *gcs, unsigned int gs_bps)
  38. {
  39. int ret = 0;
  40. gcs_link.uart = uart_find("uart1");
  41. if (gcs_link.uart)
  42. {
  43. gcs_link.uart->ops->init(gs_bps);
  44. }
  45. else
  46. {
  47. ret = -1;
  48. }
  49. return ret;
  50. }
  51. /**
  52. * @brief 地面站失联检测
  53. *
  54. */
  55. void _gs_uart_link_status_check(struct GCS_Link *pgcs)
  56. {
  57. /* 检测间隔 0.5 */
  58. const uint32_t check_dt_us = 500000;
  59. /* 失联时间 */
  60. const uint16_t linklost_time_s = (uint16_t)conf_par.lostlink_time;
  61. if (micros() - pgcs->last_check_time_us > check_dt_us)
  62. {
  63. pgcs->last_check_time_us = micros();
  64. if (pgcs->link_status == COMP_NORMAL)
  65. {
  66. pgcs->link_lost_time_us += check_dt_us;
  67. if (pgcs->link_lost_time_us > linklost_time_s * 1000000 &&
  68. linklost_time_s > 0)
  69. {
  70. pgcs->link_status = COMP_LOST;
  71. }
  72. }
  73. }
  74. }
  75. /**
  76. * @brief 获取地面站连接状态
  77. *
  78. * @return comp_status
  79. */
  80. comp_status gs_get_link_status(struct GCS_Link *pgcs) { return pgcs->link_status; }
  81. /**
  82. * @brief 地面站解析数据循环
  83. *
  84. */
  85. void gs_receive_msg_poll(struct GCS_Link *pgcs)
  86. {
  87. uint8_t c;
  88. /* 如果 rx fifo 中有数据,则读出数据进行解析 */
  89. while (pgcs->uart->ops->read(&c, 1) == 1)
  90. {
  91. switch (pgcs->link_protocal_type)
  92. {
  93. case GCS_LINK_PROTOCAL_TYPE_VKLINK:
  94. default:
  95. if (VKlink_ParseChar(&pgcs->vklink_rx_msg, c) == 1)
  96. {
  97. /* 对接收消息进行解析 */
  98. switch (pgcs->vklink_protocal_version)
  99. {
  100. /* vklink v30 协议 */
  101. case 300:
  102. gcs_vklink_v300_rx_decode(pgcs);
  103. break;
  104. /* vklink v40 协议 */
  105. case 400:
  106. // gcs_vklink_v400_rx_decode(&pgcs->vklink_rx_msg);
  107. break;
  108. default:
  109. break;
  110. }
  111. }
  112. break;
  113. }
  114. }
  115. }
  116. /**
  117. * @brief 发送数据到地面站,轮询调用
  118. *
  119. */
  120. void gs_send_msg_poll(struct GCS_Link *pgcs)
  121. {
  122. /* 检测地面站失联 */
  123. _gs_uart_link_status_check(pgcs);
  124. /* 发消息给地面站 */
  125. if (pgcs->link_protocal_type == GCS_LINK_PROTOCAL_TYPE_VKLINK)
  126. {
  127. switch (pgcs->vklink_protocal_version)
  128. {
  129. case 300:
  130. gcs_vklink_v300_tx_poll(pgcs);
  131. break;
  132. case 400:
  133. break;
  134. default:
  135. break;
  136. }
  137. }
  138. }
  139. void gs_tx_vklink_msg(struct GCS_Link *pgcs, const VKlink_Msg_Type *msg)
  140. {
  141. uint8_t *tx_buff = pgcs->msg_pack_buffer;
  142. uint32_t tx_len = VKlink_MsgTxFormat(msg, tx_buff);
  143. pgcs->uart->ops->write(tx_buff, tx_len);
  144. }
  145. void gs_send_pos_msg(struct GCS_Link *pgcs, uint32_t pos_num)
  146. {
  147. if (gcs_link.link_protocal_type == GCS_LINK_PROTOCAL_TYPE_VKLINK)
  148. {
  149. switch (gcs_link.vklink_protocal_version)
  150. {
  151. case 300:
  152. gcs_vklink_v300_set_tx_msg(pgcs, GCS_VKLINK_V300_POS_ID, (void *)pos_num);
  153. break;
  154. case 400:
  155. // gcs_vklink_v400_set_tx_msg(GCS_VKLINK_V400_POS_ID, 0);
  156. break;
  157. default:
  158. break;
  159. }
  160. }
  161. }
  162. void gs_send_ack_msg(struct GCS_Link *pgcs, void *arg)
  163. {
  164. if (gcs_link.link_protocal_type == GCS_LINK_PROTOCAL_TYPE_VKLINK)
  165. {
  166. switch (gcs_link.vklink_protocal_version)
  167. {
  168. case 300:
  169. gcs_vklink_v300_set_tx_msg(pgcs, GCS_VKLINK_V300_ACK_ID, arg);
  170. break;
  171. case 400:
  172. // gcs_vklink_v400_set_tx_msg(GCS_VKLINK_V400_ACK_ID, arg);
  173. break;
  174. default:
  175. break;
  176. }
  177. }
  178. }
  179. void gs_send_payload_msg(struct GCS_Link *pgcs, const void *data, uint32_t len)
  180. {
  181. if (gcs_link.link_protocal_type == GCS_LINK_PROTOCAL_TYPE_VKLINK)
  182. {
  183. switch (gcs_link.vklink_protocal_version)
  184. {
  185. case 300:
  186. {
  187. struct gcs_transparent_transmission_arg tt_arg;
  188. tt_arg.data = data;
  189. tt_arg.data_len = len;
  190. gcs_vklink_v300_set_tx_msg(pgcs, GCS_VKLINK_V300_PORT_UART4_DATA, &tt_arg);
  191. }
  192. break;
  193. case 400:
  194. // gcs_vklink_v400_set_tx_msg(GCS_VKLINK_V400_PAYLOAD_DATA_ID, 0);
  195. break;
  196. default:
  197. break;
  198. }
  199. }
  200. }