uart_bridge.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #include "stm32g4xx_hal.h"
  2. #include "uart_bridge.h"
  3. #include "uart_bridge_cfg.h"
  4. #include "cmsis_os.h"
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. UART_HandleTypeDef huart_bridge;
  9. osMessageQId mb_rx_queue;
  10. osThreadId ub_task_handle;
  11. void vUartBridge(void const *params);
  12. static uint8_t rx_byte;
  13. static uint32_t error_cnt = 0;
  14. static char rx_buf[DATA_BUF_SIZE];
  15. uint32_t forw_turns;
  16. uint32_t back_turns;
  17. uint32_t direction;
  18. //
  19. void init_usart(void)
  20. {
  21. GPIO_InitTypeDef GPIO_InitStruct = {0};
  22. __HAL_RCC_GPIOA_CLK_ENABLE();
  23. __HAL_RCC_USART2_CLK_ENABLE();
  24. GPIO_InitStruct.Pin = GPIO_PIN_2 | GPIO_PIN_3;
  25. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  26. GPIO_InitStruct.Pull = GPIO_PULLUP;
  27. GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
  28. GPIO_InitStruct.Alternate = GPIO_AF7_USART2;
  29. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  30. GPIO_InitStruct.Pin = GPIO_PIN_1;
  31. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  32. GPIO_InitStruct.Pull = GPIO_NOPULL;
  33. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  34. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  35. HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_RESET);
  36. huart_bridge.Instance = USART2;
  37. huart_bridge.Init.BaudRate = UB_BAUDRATE;
  38. huart_bridge.Init.WordLength = UART_WORDLENGTH_8B;
  39. huart_bridge.Init.StopBits = UART_STOPBITS_1;
  40. huart_bridge.Init.Parity = UART_PARITY_NONE;
  41. huart_bridge.Init.Mode = UART_MODE_TX_RX;
  42. huart_bridge.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  43. huart_bridge.Init.OverSampling = UART_OVERSAMPLING_8;
  44. huart_bridge.Init.OneBitSampling= UART_ONE_BIT_SAMPLE_DISABLE;
  45. HAL_UART_Init(&huart_bridge);
  46. //HAL_RS485Ex_Init(&huart_bridge, UART_DE_POLARITY_HIGH, 0, 0);
  47. HAL_NVIC_SetPriority(USART2_IRQn, 6, 0);
  48. HAL_NVIC_EnableIRQ(USART2_IRQn);
  49. HAL_UART_Receive_IT(&huart_bridge, &rx_byte, 1);
  50. }
  51. //
  52. void ub_init_os(void)
  53. {
  54. osMessageQDef(ub_queue, 20, uint8_t);
  55. mb_rx_queue = osMessageCreate(osMessageQ(ub_queue), NULL);
  56. osThreadDef(UB, vUartBridge, osPriorityNormal, 0, 2*128);
  57. ub_task_handle = osThreadCreate(osThread(UB), NULL);
  58. }
  59. //
  60. void vUartBridge(void const *params)
  61. {
  62. osEvent event;
  63. uint8_t cnt = 0;
  64. bool begin = false;
  65. for (;;)
  66. {
  67. event = osMessageGet(mb_rx_queue, 1000);
  68. if (event.status == osEventMessage) {
  69. printf("%c", event.value.v);
  70. if (event.value.v == '{') {
  71. rx_buf[cnt++] = event.value.v;
  72. begin = true;
  73. continue;
  74. }
  75. if (begin)
  76. rx_buf[cnt++] = event.value.v;
  77. if ((event.value.v == '\n') && (begin)) {
  78. forw_turns = get_uint_param(rx_buf, 0);
  79. back_turns = get_uint_param(rx_buf, 1);
  80. direction = get_uint_param(rx_buf, 2);
  81. begin = false;
  82. cnt = 0;
  83. }
  84. if (cnt == DATA_BUF_SIZE) {
  85. cnt = 0;
  86. }
  87. }
  88. }
  89. }
  90. //
  91. uint32_t get_uint_param(char* buf, uint8_t index)
  92. {
  93. char str_val[10] = {0};
  94. char *end, *begin;
  95. if (index == 0) {
  96. end = strchr(buf, ',');
  97. memcpy(str_val, &buf[1], end - buf - 1);
  98. }
  99. else {
  100. begin = strchr(buf, ',');
  101. *begin = ' ';
  102. end = strchr(buf, ',');
  103. memcpy(str_val, begin + 1, end - begin - 1);
  104. }
  105. return atoi(str_val);
  106. }
  107. //
  108. void usart_bridge_rx_cb(void)
  109. {
  110. //printf("%c", rx_byte);
  111. osMessagePut(mb_rx_queue, rx_byte, 0);
  112. HAL_UART_Receive_IT(&huart_bridge, &rx_byte, 1);
  113. }
  114. //
  115. void usart_error_cb(void)
  116. {
  117. error_cnt++;
  118. printf("error cb\r\n");
  119. }
  120. extern "C" {
  121. void USART2_IRQHandler(void)
  122. {
  123. HAL_UART_IRQHandler(&huart_bridge);
  124. }
  125. }