uart_bridge.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. #define RX_BUF_SIZE 20
  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 rx_byte_cnt = 0;
  14. static uint32_t error_cnt = 0;
  15. static char tx_buf[RX_BUF_SIZE];
  16. //
  17. void init_usart(void)
  18. {
  19. GPIO_InitTypeDef GPIO_InitStruct = {0};
  20. __HAL_RCC_GPIOA_CLK_ENABLE();
  21. __HAL_RCC_USART2_CLK_ENABLE();
  22. GPIO_InitStruct.Pin = GPIO_PIN_2 | GPIO_PIN_3;
  23. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  24. GPIO_InitStruct.Pull = GPIO_PULLUP;
  25. GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
  26. GPIO_InitStruct.Alternate = GPIO_AF7_USART2;
  27. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  28. GPIO_InitStruct.Pin = GPIO_PIN_1;
  29. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  30. GPIO_InitStruct.Pull = GPIO_NOPULL;
  31. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  32. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  33. HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_RESET);
  34. huart_bridge.Instance = USART2;
  35. huart_bridge.Init.BaudRate = UB_BAUDRATE;
  36. huart_bridge.Init.WordLength = UART_WORDLENGTH_8B;
  37. huart_bridge.Init.StopBits = UART_STOPBITS_1;
  38. huart_bridge.Init.Parity = UART_PARITY_NONE;
  39. huart_bridge.Init.Mode = UART_MODE_TX_RX;
  40. huart_bridge.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  41. huart_bridge.Init.OverSampling = UART_OVERSAMPLING_8;
  42. huart_bridge.Init.OneBitSampling= UART_ONE_BIT_SAMPLE_DISABLE;
  43. HAL_UART_Init(&huart_bridge);
  44. //HAL_RS485Ex_Init(&huart_bridge, UART_DE_POLARITY_HIGH, 0, 0);
  45. HAL_NVIC_SetPriority(USART2_IRQn, 6, 0);
  46. HAL_NVIC_EnableIRQ(USART2_IRQn);
  47. HAL_UART_Receive_IT(&huart_bridge, &rx_byte, 1);
  48. }
  49. //
  50. void ub_init_os(void)
  51. {
  52. osMessageQDef(ub_queue, 20, uint8_t);
  53. mb_rx_queue = osMessageCreate(osMessageQ(ub_queue), NULL);
  54. osThreadDef(UB, vUartBridge, osPriorityNormal, 0, 2*128);
  55. ub_task_handle = osThreadCreate(osThread(UB), NULL);
  56. }
  57. //
  58. void vUartBridge(void const *params)
  59. {
  60. osEvent event;
  61. uint8_t cnt = 0;
  62. bool begin = false;
  63. uint32_t value = 0;
  64. for (;;)
  65. {
  66. event = osMessageGet(mb_rx_queue, 1000);
  67. if (event.status == osEventMessage) {
  68. printf("%c", event.value.v);
  69. if (event.value.v == '{') {
  70. begin = true;
  71. continue;
  72. }
  73. if (begin)
  74. tx_buf[cnt++] = event.value.v;
  75. if ((event.value.v == '\n') && (begin)) {
  76. value = atoi(tx_buf);
  77. begin = false;
  78. cnt = 0;
  79. printf("Value: %u\r\n", value);
  80. }
  81. if (cnt == RX_BUF_SIZE) {
  82. cnt = 0;
  83. }
  84. }
  85. }
  86. }
  87. //
  88. void usart_bridge_rx_cb(void)
  89. {
  90. //rx_byte_cnt++;
  91. //printf("%c", rx_byte);
  92. osMessagePut(mb_rx_queue, rx_byte, 0);
  93. HAL_UART_Receive_IT(&huart_bridge, &rx_byte, 1);
  94. }
  95. //
  96. void usart_error_cb(void)
  97. {
  98. error_cnt++;
  99. printf("error cb\r\n");
  100. }
  101. extern "C" {
  102. void USART2_IRQHandler(void)
  103. {
  104. HAL_UART_IRQHandler(&huart_bridge);
  105. }
  106. }