cancel
Showing results for 
Search instead for 
Did you mean: 

USART problem on STM32L DISCOVERY

luca2
Associate II
Posted on February 09, 2014 at 17:43

Hi, 

I have a problem with the USART... basically i just want to sent out one integer (i will use a FTDI adapter to read it).  unfortunately my code get stuck to the instruction .

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

from debugging i noticed that the TXE bit is always 0.

Here more detailed information of the registers values: 

SR register = 0;

CR1 register = 8204;

CR2 register = 0;

CR3 register = 0;

Here there is my full code

&sharpinclude <stm32l1xx_usart.h>

&sharpinclude <stm32l1xx.h>

&sharpinclude <stm32l1xx_rcc.h>

&sharpinclude <stm32l1xx_gpio.h>

&sharpinclude <misc.h>

void initgpio(void){

RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);

RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);

GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);

GPIO_PinAFConfig(GPIOA,GPIO_PinSource10, GPIO_AF_USART1);

//initialize pin A9 (Tx)    and    pin A10 (RX) as AF

GPIO_InitTypeDef gpiousart;

gpiousart.GPIO_Pin =GPIO_Pin_9 | GPIO_Pin_10;

gpiousart.GPIO_Speed = GPIO_Speed_2MHz;

gpiousart.GPIO_Mode = GPIO_Mode_AF;

gpiousart.GPIO_OType= GPIO_OType_PP;

gpiousart.GPIO_PuPd = GPIO_PuPd_NOPULL;

GPIO_Init(GPIOA,&gpiousart);

}

void usartinit(void){

USART_InitTypeDef USARTuna;

USARTuna.USART_BaudRate= 9600;

USARTuna.USART_StopBits = USART_StopBits_1;

USARTuna.USART_Parity = USART_Parity_No;

USARTuna.USART_WordLength = USART_WordLength_8b;

USARTuna.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

USARTuna.USART_HardwareFlowControl = USART_HardwareFlowControl_None;

USART_Init(USART1, &USARTuna);

USART_Cmd(USART1, ENABLE);

}

int main(void)

{

initgpio();

usartinit();

RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);

//initialize pin B6 led

GPIO_InitTypeDef gpioled;

gpioled.GPIO_Pin =GPIO_Pin_6;

gpioled.GPIO_Speed = GPIO_Speed_2MHz;

gpioled.GPIO_Mode = GPIO_Mode_OUT;

gpioled.GPIO_OType = GPIO_OType_PP;

GPIO_Init(GPIOB,&gpioled);

volatile int j;

char k=0;

for (j = 0; j<10000; j++);

    while(1)

    {

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

    USART_SendData(USART1, k);

    GPIO_WriteBit(GPIOB, GPIO_Pin_6, Bit_SET);

    for (j = 0; j<10000; j++);

    GPIO_WriteBit(GPIOB, GPIO_Pin_6, Bit_RESET);

    for (j = 0; j<10000; j++);

    k++;

    }

}

The FOR cycles are delays... 

Any idea why this happens???? 

#discovery #usart #stm32l
7 REPLIES 7
Posted on February 09, 2014 at 21:36

Doesn't appear unreasonable, not sure what debugger you're using, but don't park the debugger over the peripheral, as it's more prone to interfere with it than help.

Consider also using different pins, don't PA9/PA10 clash with the LCD on the STM32L-DISCO?
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
luca2
Associate II
Posted on February 09, 2014 at 21:57

Hi Clive1, 

Thank you for your reply, i really appreciate your help. 

I am using COOCOX (COIDE).

Regarding the LCD, I physical removed it, so it should not interfere. 

I tried this code with USART2 too but still the same. Furthermore, i would also exclude some damages, since i tried on a second discovery board (same problem). 

During my last debugging,  I noticed that the USART register DR is always 0. 

Basically this instruction here:

USARTx->DR = (Data & (uint16_t)0x01FF);

which should write the DATA into DR,  it does not do it. The only things that happens when execute is that SR register sets to 0.

I would really appreciate some advises, since i already spent few days on this basic task.  

Thank you again. 

Posted on February 09, 2014 at 22:15

The DR reads a different register than the write.

The issue looks to relate to the USART not being clocked, but you look to enable it.

In CooCox you might want to make sure and call SystemInit() as the first thing you do in main() as they don't follow the CMSIS standard in that regard.

If not that, look very critically are what you're doing, that it's loading the flash of the part, not simulating. Look at the AHB/APB settings in the RCC.

Failing all that, try with the Keil Eval (v4.7x), which tends to work out of the box.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
luca2
Associate II
Posted on February 10, 2014 at 14:23

Hi Clive,

I even added those instructions in the beginning for the clock settings:

    SystemInit();

    RCC_HSICmd(ENABLE);

    RCC_SYSCLKConfig(RCC_SYSCLKSource_HSI);

    RCC_HCLKConfig(RCC_SYSCLK_Div1);

But nothing... still don't work.. how is possible that a so easy code is not working? am i the first one using the USART  on STM32L discovery?

I am really frustrated from this... regarding the keil...i am not familiar with it, and thus i will end up spending one week try to configuring it...

luca2
Associate II
Posted on February 10, 2014 at 16:11

i actually tried to use Keil and the code works fine... so probably some problem with cooide...

luca2
Associate II
Posted on February 10, 2014 at 16:56

Ok, I finally found the problem.... the problem was the startup file of COIDE.. i replaced it with this one found on internet and everything is fine now.. (the file is attached)

Hope that this help someone else...

Thank you Clive for your support..

________________

Attachments :

startup_stm32l1xx_md.c : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006HzZq&d=%2Fa%2F0X0000000bNp%2FSMS2msRr4B6TTfquZNyNC2mnk2XMVwrDQCUv4R00cEg&asPdf=false
michaelj.clift9
Associate
Posted on August 27, 2015 at 06:36

THANKYOU!!! I also had trouble getting the USART to work, I found this post and the replacement startup file fixed it 🙂