cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F4 Usart1 problem

thommagn
Associate II
Posted on October 18, 2012 at 11:01

Hi,

Im using a STM32f405RG and i am trying to get a simple USART example to work. I load the following code to the mcu:

#include ''stm32f4xx.h''
#include ''stm32f4xx_gpio.h''
#include ''stm32f4xx_usart.h''
#include ''stm32f4xx_rcc.h''
void init_usart(void){
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
/* enable peripheral clock for USART2 */
RCC_APB1PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
/* GPIOA clock enable */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
/* GPIOA Configuration: USART1 TX on PA9 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Connect USART1 pins to AF9 */
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);
USART_InitStructure.USART_BaudRate = 57600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE); // enable USART2
}
void Delay(__IO uint32_t nCount)
{
while(nCount--)
{
}
}
int main(void){
init_usart();
while(1){
USART_SendData(USART1, 'h'); 
Delay(0x3FFFFF);
}
}

when

GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);

is executed, the pin goes high, and nothing else happens.. The pin never resets and there is no data transmitted over the usart. Have anyone seen anything like this before? EDIT: If I change the code to work with USART2 everything works.. I wonder if it may have something to do with the

RCC_APB2Periph_USART1.

Im not very familliar with this and how the USART behaves if it's incorrect set. I'm also using CooCox.

/Tom
7 REPLIES 7
Posted on October 18, 2012 at 13:13

RCC_APB1PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);

Not APB1
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
David Littell
Senior III
Posted on October 18, 2012 at 13:41

I thought peripherals like that were either on one bus or the other, so how did his error even compile?

Posted on October 18, 2012 at 14:02

I thought peripherals like that were either on one bus or the other, so how did his error even compile?

Because the syntax is correct, the preprocessor substitutes a bit vector, it's not an enum, and it cares not about which bus it relates too.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
thommagn
Associate II
Posted on October 18, 2012 at 15:46

Hi, thanks for the input. I can't belive I made such a stupid mistake, have been working with USART2-4 only. Guess i got blind when trying to switch to USART1!

I changed

RCC_APB1PeriphClockCmd( ... 

to

RCC_APB2PeriphClockCmd( ...

But the result is the same, the signal goes high and nothing else happens. 

Have I made another mindblowing mistake I'm unable to see?

/ Tom

Posted on October 18, 2012 at 17:58

You're using CooCox, does that count?

Make sure you're looking at the right pin, and what it's connected too. PA9 has a big capacitor hung off it on the STM32F4-Discovery board, try 1200 baud?

// STM32F405 USART1 (Tx PA.9, Rx PA.10) - sourcer32@gmail.com
#include ''stm32f4xx.h''
/**************************************************************************************/
void RCC_Configuration(void)
{
/* --------------------------- System Clocks Configuration -----------------*/
/* USART1 clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
/* GPIOA clock enable */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
}
/**************************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/*-------------------------- GPIO Configuration ----------------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Connect USART pins to AF */
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1); // USART1_TX
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1); // USART1_RX
}
/**************************************************************************************/
void USART1_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
/* USARTx configuration ------------------------------------------------------*/
/* USARTx configured as follow:
- BaudRate = 9600 baud
- Word Length = 8 Bits
- One Stop Bit
- No parity
- Hardware flow control disabled (RTS and CTS signals)
- Receive and transmit enabled
*/
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);
}
/**************************************************************************************/
int main(void)
{
RCC_Configuration();
GPIO_Configuration();
USART1_Configuration();
while(1)
{
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); // Wait for Empty
USART_SendData(USART1, 0x49); // Send 'I'
}
while(1); // Don't want to exit
}

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
thommagn
Associate II
Posted on October 18, 2012 at 18:46

About Coocox, probably not. I have an external oscilator on my chip, and i'm not sure that i've done everything correct adjusting for it. In fact, I have only defined HSE_VALUE to the frequency of my oscillator and executed the following commands at startup:

RCC_HSEConfig(RCC_HSE_ON);

RCC_WaitForHSEStartUp();

So I didn't trust myself. 

About the USART problem, your first suggestion was correct! I had my project outside coocox workspace, and for some reason I loaded the wrong elf-file onto the chip. The Idea of copying a project from coocox workspace to another location, then trying to debug that project is no go. (Probably it was I who did something wrong in this process,,.)

/Tom

Posted on October 18, 2012 at 20:00

The clock stuff usually resides in SystemInit() in system_stm324xx.c

To run from HSE you must select it as a source once you have it running.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..