2012-10-18 02:01 AM
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.
/Tom2012-10-18 04:13 AM
RCC_APB1PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
Not APB1
2012-10-18 04:41 AM
I thought peripherals like that were either on one bus or the other, so how did his error even compile?
2012-10-18 05:02 AM
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.2012-10-18 06:46 AM
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 changedRCC_APB1PeriphClockCmd( ...
toRCC_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?/ Tom2012-10-18 08:58 AM
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
}
2012-10-18 09:46 AM
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,,.)/Tom2012-10-18 11:00 AM
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.