stm32h750/bootloader/src/uart_download.c
2024-11-20 22:20:01 +08:00

81 lines
2.9 KiB
C

#include "uart_download.h"
#include "stdio.h"
static UART_HandleTypeDef uart_download;
static void UART_DOWNLOAD_Msp_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
RCC_PeriphCLKInitTypeDef RCC_PeriphClkInit;
/*##-1- Enable peripherals and GPIO Clocks #################################*/
/* Enable GPIO TX/RX clock */
USART_DOWNLOAD_TX_GPIO_CLK_ENABLE();
USART_DOWNLOAD_RX_GPIO_CLK_ENABLE();
/* Select HSI as source of USARTx clocks */
RCC_PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART_DOWNLOAD;
RCC_PeriphClkInit.Usart16ClockSelection = RCC_USART_DOWNLOAD_CLKSOURCE_HSI;
HAL_RCCEx_PeriphCLKConfig(&RCC_PeriphClkInit);
/* Enable USARTx clock */
USART_DOWNLOAD_CLK_ENABLE();
/*##-2- Configure peripheral GPIO ##########################################*/
/* UART TX GPIO pin configuration */
GPIO_InitStruct.Pin = USART_DOWNLOAD_TX_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = USART_DOWNLOAD_TX_AF;
HAL_GPIO_Init(USART_DOWNLOAD_TX_GPIO_PORT, &GPIO_InitStruct);
/* UART RX GPIO pin configuration */
GPIO_InitStruct.Pin = USART_DOWNLOAD_RX_PIN;
GPIO_InitStruct.Alternate = USART_DOWNLOAD_RX_AF;
HAL_GPIO_Init(USART_DOWNLOAD_RX_GPIO_PORT, &GPIO_InitStruct);
/* NVIC for USART */
HAL_NVIC_SetPriority(USART_DOWNLOAD_IRQn, 0, 1);
HAL_NVIC_EnableIRQ(USART_DOWNLOAD_IRQn);
}
void uart_download_init(void)
{
uart_download.Instance = USART_DOWNLOAD;
uart_download.Init.BaudRate = UART_DOWNLOAD_BAUDRATE;
uart_download.Init.WordLength = UART_WORDLENGTH_8B;
uart_download.Init.StopBits = UART_STOPBITS_1;
uart_download.Init.Parity = UART_PARITY_NONE;
uart_download.Init.HwFlowCtl = UART_HWCONTROL_NONE;
uart_download.Init.Mode = UART_MODE_TX_RX;
uart_download.Init.ClockPrescaler = UART_PRESCALER_DIV1;
uart_download.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
uart_download.Init.OverSampling = UART_OVERSAMPLING_16;
UART_DOWNLOAD_Msp_Init();
if(HAL_UART_Init(&uart_download) != HAL_OK) {
printf("%s, %s, %d: HAL_UART_Init failed.\r\n", __FILE__, __FUNCTION__, __LINE__);
return;
}
/* Set the RXFIFO threshold */
HAL_UARTEx_SetRxFifoThreshold(&uart_download, UART_RXFIFO_THRESHOLD_1_2);
/* Enable the FIFO mode */
HAL_UARTEx_EnableFifoMode(&uart_download);
}
void uart_download_send(void)
{
if (__HAL_UART_GET_FLAG(&uart_download, UART_FLAG_TXFNF)) {
uart_download.Instance->TDR = 'A';
}
while (__HAL_UART_GET_FLAG(&uart_download, UART_FLAG_RXFNE)) {
printf("Recv: %02lX\r\n", uart_download.Instance->RDR);
}
}