cancel
Showing results for 
Search instead for 
Did you mean: 

F4Discovery using UART1

chrispadbury
Associate II
Posted on March 26, 2012 at 16:03

Hi, I not having much success with using the UART (USART1) on my F4Discovery board (STM32F407). I am pretty new to STM32 and Keil (the IDE I am using). Here's my code:

&sharpinclude ''stm32f4_discovery.h''

&sharpinclude ''stm32f4xx_usart.h''

&sharpinclude ''stm32f4xx.h''

void usartSetup(void);

void USART_PutChar(uint8_t ch);

int main (void) {

    usartSetup();

    USART_PutChar(0);   //I realise this won't send a 0

    USART_PutChar(8);   //I realise this won't send an 8

    USART_PutChar(255);   //I realise this won't send a 255

    while (1) {

        //loop forever

    }

}

void usartSetup (void) {

    USART_InitTypeDef USART_InitStructure;

    //USART_StructInit(&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 USART_PutChar(uint8_t ch) {

    USART_SendData(USART1, (uint16_t) 0x49);

    while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);

    USART_SendData(USART1, (uint16_t) 0x49);

}

 I'd be very grateful if someone could help. It never sends the 0x49 out of the UART1 TX (have checked pin PA9 and PB6) and then gets endlessly stuck on the while(USART_GetFlagStatus...). I am observing using the Keil debugger and see it get stuck in the while. I also have a scope probe on the TX pin and never see anything.

I am including the stm32f4xx_usart.c driver into the project.

Thanks!

#rs232-uart-usart-f4discover-407 #uart #stm32f4discovery
20 REPLIES 20
chrispadbury
Associate II
Posted on March 28, 2012 at 17:54

Out of interest, Is there a more elegant way to write to the upper 8 pins of portE. I am more used to eight bit micros, where it is simpler dealing with single bytes all the time.

Posted on March 28, 2012 at 19:51

GPIO_SetBits(GPIOE, (c << 8))

; You could also touch the underlying register(s).
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
chrispadbury
Associate II
Posted on April 11, 2012 at 18:20

Thanks for that. A late response, but I think I would have to add a line to Reset the desired pins as well:

GPIO_ResetBits(GPIOE, ((255-c) << 8))

;

So the complete section of code would be:

GPIO_SetBits(GPIOE, (c << 8))

;

GPIO_ResetBits(GPIOE, ((255-c) << 8))

;

Posted on April 11, 2012 at 18:35

I had envision just losing all the bit testing stuff, while leaving the initial clearing code, thus :

GPIO_ResetBits(GPIOE, GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15);

GPIO_SetBits(GPIOE, (c << 8));

Doing a read-mask-write directly on the register would of course be a more optimal choice. There is probably also a clever trick with the 32-bit BSRR register. Perhaps :

GPIOE->BSRR = ((u32)(0xFF ^ c) << 24) | (u32)(c << 8);

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
jlchoobs
Associate II
Posted on April 20, 2012 at 11:04

Clive, I tried your Uart1 Example code as a new project, and it works fine.

But, when I dropped the Uart1 Code into the F4Disc Demo/Usb Project, it returned characters at the wrong speed on PB6.  Would the PbButtonInit() or anything else cause this problem - maybe Clock/PortClockSpeed ?  Nothing else is using the PB6/PB7 Pins.

Posted on April 20, 2012 at 14:00

A lot of the F4 library code assumes that the device is clocked with an external 25 MHz crystal, the F4 discovery has an 8 MHz crystal. You need to check the library, and setting of things like HSE_VALUE.

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

Thanks Clive, I missed that.

I adjusted the PLL in the System_stm32f4xx.c File PLL_M=8 intead of 25,

but did not know thast there were any other Registers.

jihed_azaza
Associate
Posted on May 01, 2012 at 12:50

good morning ,

how we can use the usart in order to reveive data.

i want a programme

thnx

Posted on May 01, 2012 at 13:31

I'd try digging through the STM32 examples.

Once you have the port configured, the reading side looks like this :

while(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET); // Wait for Character
ch = USART_ReceiveData(USART1);

Personally, I'd be using interrupts and a ring buffer, but that's more involved than the simple example.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
jihed_azaza
Associate
Posted on May 01, 2012 at 14:57

this is exactly what i want a code wich can receive a data using the mecanisme of interrupt.this a code but it don't work

/

/* ------------------------Includes ---------

-------------*/

#include ''stm32f10x.h''

#include ''stm32f10x_gpio.h''

#include ''stm32f10x_rcc.h''

#include ''stm32f10x_dma.h''

#include ''stm32f10x_usart.h''

/* Définition de la structure*/

char st[];

int i;

/**************************************************************************************/

void RCC_Configuration(void)

{

/* --------------------------- System Clocks Configuration -----------------*/

/* Enable DMA clock */

RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);

/* Enable GPIO bank clock */

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

/* Enable USART1 clock */

RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);

}

void GPIO_Configuration(void)

{

GPIO_InitTypeDef GPIO_InitStructure;

/* Configure USART Rx as input floating */

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; // PA.10 USART1.RX

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

GPIO_Init(GPIOA, &GPIO_InitStructure);

}

void USART1_Configuration(void)

{

USART_InitTypeDef USART_InitStructure;

/* USARTx configuration ------------------------------------------------------*/

/* USARTx configured as follow:

- BaudRate = 1200 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 = 1200;

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_Init(USART1, &USART_InitStructure);

USART_Cmd(USART1, ENABLE);

}

void DMA_Configuration(void)

{

// USART1 Channel 5

DMA_DeInit(DMA1_Channel5);

DMA_Cmd(DMA1_Channel5, DISABLE);

}

void DMA_USART_String(char *st)

{

DMA_InitTypeDef DMA_InitStructure;

/* Disable the DMA Controller/Channel */

DMA_DeInit(DMA1_Channel5);

/* USART1_Tx_DMA_Channel (triggered by USART1 Tx event) Config */

DMA_InitStructure.DMA_PeripheralBaseAddr = (u8)&USART1->DR;

DMA_InitStructure.DMA_MemoryBaseAddr = (u8)st;

DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;

DMA_InitStructure.DMA_BufferSize = strlen(st);

DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Enable;

DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;

DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;

DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;

DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;

DMA_InitStructure.DMA_Priority = DMA_Priority_High;

DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;

DMA_Init(DMA1_Channel5, &DMA_InitStructure);

/* Enable the DMA Controller/Channel */

DMA_Cmd(DMA1_Channel5, ENABLE);

/* Enable USART1 DMA TX request */

//USART_DMACmd(USART1, USART_DMAReq_Rx, ENABLE);

/* Wait of DMA completion */

while(DMA_GetFlagStatus(DMA1_FLAG_TC4) == RESET);

/* Loop until the end of transmit */

while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);

}

int main(void)

{

RCC_Configuration();

GPIO_Configuration();

USART1_Configuration();

DMA_Configuration();

while(1 ){

if(USART_GetFlagStatus(USART1,USART_IT_RXNE)==SET)

{

for(i=0; i<20; i++){

st[i]= USART_ReceiveData(USART1);

printf('' %c'',st[i]&0xFF); /* print the input char */

}

}

// DR[i]=GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_10);

// DMA_USART_String(&DR[i]);

}

};

/******************** Function Name : Delay**************************/

void Delay(vu32 nCount)

{ for(; nCount != 0; nCount--);}