I have been trying to get the usart working on the stm32f3 discovery board for a while. I am using a 3.3V FTDI basic from sparkfun for the purpose. So far, I haven't got a peep from the program.
The program is given below:
/* Includes ------------------------------------------------------------------*/
#include <stm32f30x.h>
#include <stm32f30x_rcc.h>
#include <stm32f30x_rcc.c>
#include <stm32f30x_gpio.h>
#include <stm32f30x_gpio.c>
#include <stm32f30x_misc.h>
#include <stm32f30x_misc.c>
#include <stm32f30x_tim.h>
#include <stm32f30x_tim.c>
#include <stm32f30x_usart.h>
#include <stm32f30x_usart.c>
//#include <arm_math.h>
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
volatile int ms;
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
void SysTick_Handler(void)
{
ms++;
if (ms>99){
USART_SendData(USART1,'S');
while(USART_GetFlagStatus(USART2,USART_FLAG_TXE)==RESET){}
ms=0;
}
}
/**
* @brief Main program.
* @param None
* @retval None
*/
int main(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
USART_InitTypeDef USART_InitStruct;
/*GPIO configuration for indicator LED*/
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOE,ENABLE);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_8;
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 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_DOWN;
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*/
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);
//Enable USART global interrupt
NVIC_EnableIRQ(USART2_IRQn);
if (SysTick_Config(SystemCoreClock / 1000))
{
/* Capture error */
while (1);
}
while (1)
{
}
}
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_SendData(USART1,'T');
while(USART_GetFlagStatus(USART2,USART_FLAG_TXE)==RESET){}*/
}
}
}
I am using the Keil uvision 5 IDE.
Thank you.
EDIT: The current code is given below:
/* Includes ------------------------------------------------------------------*/
#include <stm32f30x.h>
#include <stm32f30x_rcc.h>
#include <stm32f30x_rcc.c>
#include <stm32f30x_gpio.h>
#include <stm32f30x_gpio.c>
#include <stm32f30x_misc.h>
#include <stm32f30x_misc.c>
#include <stm32f30x_tim.h>
#include <stm32f30x_tim.c>
#include <stm32f30x_usart.h>
#include <stm32f30x_usart.c>
//#include <arm_math.h>
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
volatile int ms;
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
void SysTick_Handler(void)
{
ms++;
if (ms>99){
USART_SendData(USART1,'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;
}
}
/**
* @brief Main program.
* @param None
* @retval None
*/
int main(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
USART_InitTypeDef USART_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_DOWN;
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);
GPIO_SetBits(GPIOE,GPIO_Pin_10);
if (SysTick_Config(SystemCoreClock / 1000))
{
/* Capture error */
while (1);
}
GPIO_SetBits(GPIOE,GPIO_Pin_11);
while (1)
{
}
}
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_SendData(USART1,'T');
while(USART_GetFlagStatus(USART2,USART_FLAG_TXE)==RESET){}*/
}
}
}
There is no response from the program at this time. (TX is constant at 3.3V, RX is at 0v).
UPDATE: I have tried using USART1 instead, and I still get no response. The other changes include moving the transmission part of the code to the while loop, and trying with GPIO_PuPd_UP, GPIO_PuPd_DOWN and GPIO_PuPd_NOPULL, with no result. Currently, when using USART2, the TX (PA2) is at a constant 3.3V, and RX(PA3) at 0v. On moving it to USART1, both(PC4,PC5) seem to be at a constant 3.3V.
UPDATE: The TX of the USART is working now, I had to enable the USART after initialising it, not before, as discussed here: https://community.st.com/thread/47271-usart-stm32f3-discovery
#include
ing .c files along with the .h files. You should only have to include the .h file. \$\endgroup\$