2018-02-06 09:10 PM
I have been trying to get the USART to work on the stm32f3 discovery for a while without any success. Currently, the TX pin (PA2) is at a constant 3.3V, the RX pin (PA3) is at a constant 0V. I am using an FTDI Basic breakout board to read the USART, and I am using the keil uvision 5 IDE.
The code is given below:
/* Includes ------------------------------------------------------------------*/
&sharpinclude <stm32f30x.h>&sharpinclude <stm32f30x_rcc.h>&sharpinclude <stm32f30x_rcc.c>&sharpinclude <stm32f30x_gpio.h>&sharpinclude <stm32f30x_gpio.c>&sharpinclude <stm32f30x_misc.h>&sharpinclude <stm32f30x_misc.c>&sharpinclude <stm32f30x_tim.h>&sharpinclude <stm32f30x_tim.c>&sharpinclude <stm32f30x_usart.h>&sharpinclude <stm32f30x_usart.c>//&sharpinclude <arm_math.h>/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*//* Private macro -------------------------------------------------------------*//* Private variables ---------------------------------------------------------*/volatile int ms;/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
void SysTick_Handler(void){ ms++;}/**
* @brief Main program. * @param None * @retval None */int main(void){ GPIO_InitTypeDef GPIO_InitStruct; USART_InitTypeDef USART_InitStruct; NVIC_InitTypeDef NVIC_InitStruct; /*GPIO configuration for indicator LED*/ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOE,ENABLE); GPIO_InitStruct.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_11|GPIO_Pin_12; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(GPIOE,&GPIO_InitStruct); //Turn LED on GPIO_SetBits(GPIOE,GPIO_Pin_8|GPIO_Pin_12); /*GPIO configuration for USART. PA2 = USART2_TX, PA3 = USART2_RX*/ //Enable clock for GPIOA RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA,ENABLE); GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOA,&GPIO_InitStruct); //AF configuration GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_7); GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_7); /*USART configuration*/ RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE); USART_Cmd(USART2, ENABLE); USART_InitStruct.USART_BaudRate = 9600; USART_InitStruct.USART_WordLength = USART_WordLength_8b; USART_InitStruct.USART_StopBits = USART_StopBits_1; USART_InitStruct.USART_Parity = USART_Parity_No; USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;USART_Init(USART2, &USART_InitStruct);
//Enable RXNE interrupt USART_ITConfig(USART2,USART_IT_RXNE,ENABLE); GPIO_SetBits(GPIOE,GPIO_Pin_9); //Enable USART global interrupt //NVIC_EnableIRQ(USART2_IRQn); NVIC_InitStruct.NVIC_IRQChannel = USART2_IRQn; NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0; NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStruct); GPIO_SetBits(GPIOE,GPIO_Pin_10); if (SysTick_Config(SystemCoreClock / 1000)) { /* Capture error */ while (1); } GPIO_SetBits(GPIOE,GPIO_Pin_11); while (1) { if (ms>99){ USART_SendData(USART2,'S'); while(USART_GetFlagStatus(USART2,USART_FLAG_TXE)==RESET){}; if (GPIO_ReadOutputDataBit(GPIOE,GPIO_Pin_12)){ GPIO_ResetBits(GPIOE,GPIO_Pin_12); } else{ GPIO_SetBits(GPIOE,GPIO_Pin_12); } ms=0; } }}void USART2_IRQHandler(void){
//RXNE handler if(USART_GetITStatus(USART2,USART_IT_RXNE) != RESET){ //If recieved 't', toggle LED, transmit 'T' if((char)USART_ReceiveData(USART2) == 't'){ //Toggle LED if (GPIO_ReadOutputDataBit(GPIOE,GPIO_Pin_8)){ GPIO_ResetBits(GPIOE,GPIO_Pin_8); } else{ GPIO_SetBits(GPIOE,GPIO_Pin_8); } USART_ClearITPendingBit(USART2,USART_IT_RXNE); //USART_SendData(USART1,'T'); //while(USART_GetFlagStatus(USART2,USART_FLAG_TXE)==RESET){} } }}#stm32f3-discovery #usartSolved! Go to Solution.
2018-02-07 07:17 AM
st118581
,you should not enable UART before you configure it. First do the configuration, afterUART_Init function callUART_Cmd and enable it.
Best regards,
Tilen
2018-02-07 07:17 AM
st118581
,you should not enable UART before you configure it. First do the configuration, afterUART_Init function callUART_Cmd and enable it.
Best regards,
Tilen
2018-02-07 03:21 PM
The Rx pin should not be Low....
before the Uart will function this pin should be high...
2018-02-07 04:41 PM
Would also recommend checking TXE *before* writing to the data register, you're trying to ensure the register is empty, not wait for it to transit or move to the shift register.
...
USART_Init(USART2, &
USART_InitStruct)
;USART_Cmd(USART2, ENABLE);
...
while(USART_GetFlagStatus(USART2,USART_FLAG_TXE)==RESET){};
USART_SendData(USART2,'S');
...
https://community.st.com/0D50X00009XkgXFSAZ