Intenté desarrollar un ejemplo de USART en el kit de descubrimiento stm32L476, pero tengo algunos problemas con los controladores HAL, creo que estoy usando la función de activación de reloj de la interfaz de USART incorrecta y las funciones de activación del reloj de USART GPIO.
Aquí está mi código:
void HAL_USART_MspInit(USART_HandleTypeDef* U)
{
RCC_APB2ENR_USART1EN;
RCC_AHB2ENR_GPIOBEN;
GPIO_InitTypeDef G;
G.Pin = GPIO_PIN_6 |GPIO_PIN_7;
G.Mode = GPIO_MODE_AF_PP;
G.Pull = GPIO_NOPULL;
G.Alternate = GPIO_AF7_USART1;
G.Speed = GPIO_SPEED_HIGH;
HAL_GPIO_Init(GPIOB,&G);
}
void USART_Init()
{
USART_HandleTypeDef U;
U.Instance = USART1_BASE;
U.Init.BaudRate = 9600;
U.Init.WordLength = USART_WORDLENGTH_8B;
U.Init.Parity = USART_PARITY_NONE;
U.Init.StopBits = USART_STOPBITS_1;
U.Init.Mode = USART_MODE_TX_RX;
HAL_USART_Init(&U);
}
Y aquí está la descripción del controlador:
The USART HAL driver can be used as follows:
(#) Declare a USART_HandleTypeDef handle structure (eg. USART_HandleTypeDef husart).
(#) Initialize the USART low level resources by implementing the HAL_USART_MspInit() API:
(++) Enable the USARTx interface clock.
(++) USART pins configuration:
(+++) Enable the clock for the USART GPIOs.
(+++) Configure these USART pins as alternate function pull-up.
(++) NVIC configuration if you need to use interrupt process (HAL_USART_Transmit_IT(),
HAL_USART_Receive_IT() and HAL_USART_TransmitReceive_IT() APIs):
(+++) Configure the USARTx interrupt priority.
(+++) Enable the NVIC USART IRQ handle.
(++) USART interrupts handling:
-@@- The specific USART interrupts (Transmission complete interrupt,
RXNE interrupt and Error Interrupts) will be managed using the macros
__HAL_USART_ENABLE_IT() and __HAL_USART_DISABLE_IT() inside the transmit and receive process.
(++) DMA Configuration if you need to use DMA process (HAL_USART_Transmit_DMA()
HAL_USART_Receive_DMA() and HAL_USART_TransmitReceive_DMA() APIs):
(+++) Declare a DMA handle structure for the Tx/Rx channel.
(+++) Enable the DMAx interface clock.
(+++) Configure the declared DMA handle structure with the required Tx/Rx parameters.
(+++) Configure the DMA Tx/Rx channel.
(+++) Associate the initialized DMA handle to the USART DMA Tx/Rx handle.
(+++) Configure the priority and enable the NVIC for the transfer complete interrupt on the DMA Tx/Rx channel.
(#) Program the Baud Rate, Word Length, Stop Bit, Parity, Hardware
flow control and Mode (Receiver/Transmitter) in the husart handle Init structure.
(#) Initialize the USART registers by calling the HAL_USART_Init() API:
(++) This API configures also the low level Hardware GPIO, CLOCK, CORTEX...etc)
by calling the customized HAL_USART_MspInit(&husart) API.
[..]
(@) To configure and enable/disable the USART to wake up the MCU from stop mode, resort to UART API's
HAL_UARTEx_StopModeWakeUpSourceConfig(), HAL_UARTEx_EnableStopMode() and
HAL_UARTEx_DisableStopMode() in casting the USART handle to UART type UART_HandleTypeDef.
Desafortunadamente, creo que me he perdido algo, pero no puedo entenderlo. Intento buscar algunos ejemplos, pero no encontré nada.