usart.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. #include "stm32g0xx_hal.h"
  2. #include "usart.h"
  3. void init_usart(void)
  4. {
  5. GPIO_InitTypeDef GPIO_InitStruct;
  6. USART_BRIDGE_TX_PORT_CLK_ENABLE
  7. USART_BRIDGE_RX_PORT_CLK_ENABLE
  8. GPIO_InitStruct.Pin = USART_BRIDGE_TX_PIN;
  9. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  10. GPIO_InitStruct.Pull = GPIO_PULLUP;
  11. GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
  12. GPIO_InitStruct.Alternate = USART_BRIDGE_AF;
  13. HAL_GPIO_Init(USART_BRIDGE_TX_PORT, &GPIO_InitStruct);
  14. GPIO_InitStruct.Pin = USART_BRIDGE_RX_PIN;
  15. HAL_GPIO_Init(USART_BRIDGE_RX_PORT, &GPIO_InitStruct);
  16. USART_BRIDGE_CLK_ENABLE
  17. uart.Instance = USART_BRIDGE_USART;
  18. uart.Init.BaudRate = USART_BRIDGE_SPEED;
  19. uart.Init.WordLength = UART_WORDLENGTH_8B;
  20. uart.Init.StopBits = UART_STOPBITS_1;
  21. uart.Init.Parity = UART_PARITY_NONE;
  22. uart.Init.Mode = UART_MODE_TX_RX;
  23. uart.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  24. uart.Init.OverSampling = UART_OVERSAMPLING_8;
  25. uart.Init.OneBitSampling= UART_ONE_BIT_SAMPLE_DISABLE;
  26. HAL_UART_Init(&uart);
  27. HAL_NVIC_SetPriority(USART_BRIDGE_IRQn, 6, 0);
  28. HAL_NVIC_EnableIRQ(USART_BRIDGE_IRQn);
  29. HAL_UART_Receive_IT(&uart, (uint8_t*)&m_uartByte, 1);
  30. }