2023-02-17 04:58 PM
I have configured PA2 as USART1 TX in STM32CubeMX, the code it generates looks correct:
void MX_USART1_UART_Init(void)
{
/* USER CODE BEGIN USART1_Init 0 */
/* USER CODE END USART1_Init 0 */
LL_USART_InitTypeDef USART_InitStruct = {0};
LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
/* Peripheral clock enable */
LL_APB1_GRP2_EnableClock(LL_APB1_GRP2_PERIPH_USART1);
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOA);
/**USART1 GPIO Configuration
PA2 ------> USART1_TX
PA3 ------> USART1_RX
*/
GPIO_InitStruct.Pin = TX_Pin;
GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
GPIO_InitStruct.Alternate = LL_GPIO_AF_1;
LL_GPIO_Init(TX_GPIO_Port, &GPIO_InitStruct);
GPIO_InitStruct.Pin = RX_Pin;
GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
GPIO_InitStruct.Alternate = LL_GPIO_AF_1;
LL_GPIO_Init(RX_GPIO_Port, &GPIO_InitStruct);
/* USART1 DMA Init */
/* USART1_TX Init */
LL_DMA_SetDataTransferDirection(DMA1, LL_DMA_CHANNEL_2, LL_DMA_DIRECTION_MEMORY_TO_PERIPH);
LL_DMA_SetChannelPriorityLevel(DMA1, LL_DMA_CHANNEL_2, LL_DMA_PRIORITY_LOW);
LL_DMA_SetMode(DMA1, LL_DMA_CHANNEL_2, LL_DMA_MODE_NORMAL);
LL_DMA_SetPeriphIncMode(DMA1, LL_DMA_CHANNEL_2, LL_DMA_PERIPH_NOINCREMENT);
LL_DMA_SetMemoryIncMode(DMA1, LL_DMA_CHANNEL_2, LL_DMA_MEMORY_INCREMENT);
LL_DMA_SetPeriphSize(DMA1, LL_DMA_CHANNEL_2, LL_DMA_PDATAALIGN_BYTE);
LL_DMA_SetMemorySize(DMA1, LL_DMA_CHANNEL_2, LL_DMA_MDATAALIGN_BYTE);
/* USART1 interrupt Init */
NVIC_SetPriority(USART1_IRQn, 0);
NVIC_EnableIRQ(USART1_IRQn);
/* USER CODE BEGIN USART1_Init 1 */
/* USER CODE END USART1_Init 1 */
USART_InitStruct.BaudRate = 1000000;
USART_InitStruct.DataWidth = LL_USART_DATAWIDTH_8B;
USART_InitStruct.StopBits = LL_USART_STOPBITS_1;
USART_InitStruct.Parity = LL_USART_PARITY_NONE;
USART_InitStruct.TransferDirection = LL_USART_DIRECTION_TX_RX;
USART_InitStruct.HardwareFlowControl = LL_USART_HWCONTROL_NONE;
USART_InitStruct.OverSampling = LL_USART_OVERSAMPLING_16;
LL_USART_Init(USART1, &USART_InitStruct);
LL_USART_DisableIT_CTS(USART1);
LL_USART_ConfigAsyncMode(USART1);
LL_USART_Enable(USART1);
/* USER CODE BEGIN USART1_Init 2 */
/* USER CODE END USART1_Init 2 */
}
And then I set it up in my code as follows:
// start uart (send/recv to/from BLE MCU)
LL_USART_EnableIT_TC(USART1);
LL_USART_EnableIT_RXNE(USART1);
LL_USART_EnableDirectionTx(USART1);
LL_USART_EnableDirectionRx(USART1);
// setup uart DMA
LL_USART_EnableDMAReq_TX(USART1);
LL_DMA_SetPeriphAddress(DMA1, LL_DMA_CHANNEL_2, LL_USART_DMA_GetRegAddr(USART1, LL_USART_DMA_REG_DATA_TRANSMIT));
LL_DMA_EnableIT_TC(DMA1, LL_DMA_CHANNEL_2);
It works fine if I use PA9, but using PA2 I get nothing - the GPIO is floating. Has anyone managed it and, if so, is there a trick I'm missing?
2023-02-17 05:00 PM
Sorry, I hit send too soon. The code for initiating a UART transmit is this
LL_DMA_SetDataLength(DMA1, LL_DMA_CHANNEL_2, 4);
LL_DMA_SetMemoryAddress(DMA1, LL_DMA_CHANNEL_2, (uint32)uart_tx_buffer);
LL_DMA_EnableChannel(DMA1, LL_DMA_CHANNEL_2);
which, as I say, works fine if I use PA9 for TX...
2023-02-17 11:40 PM
Are you sure is your mcu F4?
2023-02-17 11:49 PM
are you sure that TX_Pin is LL_GPIO_PIN_2?
my CubeMX sets GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
hth
KnarfB
2023-02-18 12:11 AM
Read out and check/post content of USART and relevant GPIO registers.
Make sure physical connection is OK by setting given pin to GPIO output and toggling it.
JW