cancel
Showing results for 
Search instead for 
Did you mean: 

USART1 with STM32F0 Discovery

pic_micro
Associate II
Posted on December 22, 2014 at 17:14

Dear All,

I write a program for USART1 of STM32F0 Discovery board. I am sure that my hardware is working . I configured with PuTTY but it does not display the message Please advice PA10 RX to MAX 232 pin 12 PA9 TX to MAX 232 pin 11

#include ''stm32f0xx.h''
#include ''stm32f0xx_tim.h''
#include <
stm32f0xx_rcc.h
>
#include ''stm32f0xx_usart.h''
void gpio_init(){
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHBPeriphClockCmd (RCC_AHBPeriph_GPIOC | RCC_AHBPeriph_GPIOA,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_8 ; 
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOC, &GPIO_InitStructure); 
}
void LED(){
GPIOC->ODR ^= GPIO_Pin_8;
}
void USART_gpio_Initialize(){
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_0);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_0);
//Configure USART1 pins: Rx and Tx ----------------------------
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
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);
}
void USART_Initialize(){
//Configure USART2 setting: ----------------------------
USART_InitTypeDef USART_InitStructure;
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);
}
void Send_Data(){
while(1) {
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
USART_SendData(USART1, 'A');
}
}
int main(){
gpio_init();
LED();
USART_gpio_Initialize();
USART_Initialize();
Send_Data();
//while(1){}
}

11 REPLIES 11
Posted on December 22, 2014 at 17:38

RCC_APB1PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);

APB2 not APB1
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
pic_micro
Associate II
Posted on December 22, 2014 at 18:01

Dear Sir,

Great  thanks

I changed the code, bit problem is remaining the same

 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);

please advice

Posted on December 22, 2014 at 22:01

Do you have an oscilloscope? Can you confirm the bit timing with that?

The primary issue with serial baud rates on STM32 devices is the incorrect configuration of HSE_VALUE, this define must match the clock rate of the externally supplied clock if that is used.

I'm not going to look up MAX232 datasheets, but what voltage are you using, as I recall the MAX232 is not 3V compatible, perhaps the

http://www.maximintegrated.com/en/datasheet/index.mvp/id/1068

?

Can you receive data on the STM32 if you loop back TX to RX, on either the current USART, or a second one? Are there any other connections to USART1 on the STM32F0-DISCO board, review the schematic, check if it goes to the ST-LINK device.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
pic_micro
Associate II
Posted on December 23, 2014 at 16:52

Dear Sir,

Thanks for the reply

Do you have an oscilloscope? Can you confirm the bit timing with that?

Sorry I should not have

The primary issue with serial baud rates on STM32 devices is the incorrect configuration of HSE_VALUE, this define must match the clock rate of the externally supplied clock if that is used.

I am using default internal clock ofSTM32F0DISCOVERY (STM32F051R8T6) kit

Can you receive data on the STM32 if you loop back TX to RX

Yes.loop-backis working for second one ( Hardware USART)

Are there any other connections to USART1 on the STM32F0-DISCO board

As per the attached schismatic it does not have connected with others

________________

Attachments :

STM32F0DISCOVERY_Hardware_and_layout.pdf : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I0P1&d=%2Fa%2F0X0000000bb7%2FZb_yDjXy5HkhIvUJpAnORGHnVPrL6hGEhC7KUgDbxxw&asPdf=false
Posted on December 23, 2014 at 18:08

Please don't attach publicly available documents to the forum, cite a link if you must refer to them. The task to review the schematic is yours, not really mine.

A complete schematic of what circuitry you've attached, that might be salient, as I wouldn't have access to that.

Absent a scope you'll need to confirm the clocks, signals and voltages. Perhaps you can use the GetClocks api and sprintf to a buffer, and view that buffer in the debugger?
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
pic_micro
Associate II
Posted on December 24, 2014 at 18:29

Dear Sir,

Grate thanks for the advice 

Now my program is working

The issue was 

GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_

AF_0

);

GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_

AF_0

);

But it should be 

GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_

AF_1

);

GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_

AF_1

);

AF_0 should be AF_1

But my question is as per the stm32f0xx.gpio.h the GPIO_Alternate_function_selection_define

 @brief  AF 0 selection

  */

#define GPIO_AF_0            ((uint8_t)0x00) /* WKUP, EVENTOUT, TIM15, SPI1, TIM17,

                                                MCO, SWDAT, SWCLK, TIM14, BOOT,

                                               

USART1

, CEC, IR_OUT, SPI2 */

Please advice

Thanks in advance

Posted on December 24, 2014 at 21:35

On the PB6/7 pins USART1 is AF0, the Data Sheet should be the controlling document here.

One of the issues is that the F0 design has the pin mux design optimized to use the least amount of silicon, rather than say the F4 where more consistency is applied to the mux numbering across peripherals, but having more unused gaps.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
pic_micro
Associate II
Posted on December 25, 2014 at 08:31

Dear Sir,

Thanks for the reply

I configured the USART1 for pin PB6 - PB7 and it is working but stranger is it does care about following codes 

GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_0);

GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_0);

It is working without above codes

Posted on December 25, 2014 at 17:55

Probably because the reset setting of the bits related to the AF Mux are zero.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..