cancel
Showing results for 
Search instead for 
Did you mean: 

STM32L100C-DISCO USART Problems

wprasek
Associate II
Posted on October 02, 2014 at 06:04

Hi All,

I'm new to embedded programming so I ordered one of these discovery boards to get my feet wet. I'm trying to get USART working so my first goal is to jump the TX and RX pins to see if I can send+receive at the same time. So far I've had 0 luck. I'm using CoIDE and have based my code on some CMSIS examples I found online. Currently when I debug my code the RXNE register never gets set so it sits in an infinite loop waiting to receive something. Any ideas?


#include ''misc.h''

#include ''stm32l1xx.h''

#include ''stm32l1xx_gpio.h''

#include ''stm32l1xx_rcc.h''

#include ''stm32l1xx_usart.h''


void Usart1Put(uint8_t ch)

{

USART_SendData(USART1, (uint8_t) ch);


//Loop until the end of transmission

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

{

}

}

uint8_t Usart1Get(void){

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

return (uint8_t)USART_ReceiveData(USART1);

}


int main(void)

{

GPIO_InitTypeDef GPIO_InitStructure;

USART_InitTypeDef USART_InitStructure;

USART_ClockInitTypeDef USART_ClockInitStructure;


USART_DeInit(USART1);


/* Enable GPIO clock */

RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);


/* Enable UART clock */

RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);


/* Connect PXx to USARTx_Tx */

GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);


/* Connect PXx to USARTx_Rx */

GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);


/* Configure USART Tx and Rx as alternate function push-pull */

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;

GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;

GPIO_Init(GPIOA, &GPIO_InitStructure);



USART_ClockStructInit(&USART_ClockInitStructure);

USART_ClockInit(USART1, &USART_ClockInitStructure);


/* USART configuration */

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);


/* Enable USART */

USART_Cmd(USART1, ENABLE);



uint8_t c = 'w';

Usart1Put(c);

while (1) {

c = Usart1Get();

Usart1Put(c);

}

}

#stm32l100c-disco-usart
14 REPLIES 14
Posted on October 02, 2014 at 09:20

Read out and post the USART control and status register content.

JW
wprasek
Associate II
Posted on October 04, 2014 at 06:24

Prior to sending anything this is what I get: 

USART1->SR = 11000000  (192 in dec)

USART1->CR1 = 10000000001100 (8204 in dec)

After I send a byte the SR = 0 but CR1 is the same.  All other registers for USART1 are 0.

kjwhitcombe
Associate II
Posted on October 04, 2014 at 10:23

I don't see you initialising the USART_Initstructure anywhere (ie: USART_StructInit(&USART_InitStructure);)

I don't think that will help much, just something I noticed that is different to what I've always done.

In my experience it is all to easy to get stuck trying to debug the code when actually the problem is in hardware, I've seen many struggle on for hours only to find when I come help them with fresh eyes I've found for them that they have done a silly mistake like connecting all the signal wires up but not the all too important ground! or installed a connector offset by one pin, so it will pay to double check things like this, also have you got someway of checking it is actually TX'ing? simply connecting an LED with a suitable resistor and seeing if it flashes is a good low budget trick if you don't have an oscilloscope.
wprasek
Associate II
Posted on October 04, 2014 at 15:55

Thanks for the response Crazy_Cat_Gentleman.  I'm fairly certain nothing is transmitting because the TXE bit never gets set.  I must not be initializing a clock or something properly because I've never seen anything on the wire.  I'm assuming of course the SystemInit() function does everything I need it to.  Is there something in specific I have to initialize that I am not in my code?

wprasek
Associate II
Posted on October 04, 2014 at 16:15

Figured I should also post the GPIOA registers...

GPIOA->MODER = 0xA8280000

GPIOA->OSPEEDR = 0xC3C0000

GPIOA->PUPDR = 0x64140000

GPIOA->IDR = 0x6C08

GPIOA->IFR->IFR[1]->0x770

kjwhitcombe
Associate II
Posted on October 05, 2014 at 01:09

Sorry I'm not seeing anything wrong. Are you aware that startup_stm32l1xx_md.c has errors in the current coide release? (namely around interrupt vectors) Might be worth fixing that incase it is the cause, I'll attach a variation of it that works for me.

________________

Attachments :

startup_stm32l1xx_md.c : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I0f7&d=%2Fa%2F0X0000000bcT%2FSTkT5SaxwZx7NxnzcDpaVg5SdJqH0QZu0Ucxt257sWg&asPdf=false
wprasek
Associate II
Posted on October 06, 2014 at 20:58

Also of note, the TXE bit never gets sent after I send data.

Posted on October 07, 2014 at 09:04

I meant, *all* USART registers (including CR1, CR2, CR3 and BRR).

JW

wprasek
Associate II
Posted on October 07, 2014 at 15:53

All other registers are 0