soft_gs.c 5.2 KB

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