2014-10-01 09:04 PM
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
2014-10-02 12:20 AM
Read out and post the USART control and status register content.
JW2014-10-03 09:24 PM
2014-10-04 01:23 AM
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.2014-10-04 06:55 AM
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?
2014-10-04 07:15 AM
Figured I should also post the GPIOA registers...
GPIOA->MODER = 0xA8280000GPIOA->OSPEEDR = 0xC3C0000GPIOA->PUPDR = 0x64140000GPIOA->IDR = 0x6C08GPIOA->IFR->IFR[1]->0x7702014-10-04 04:09 PM
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=false2014-10-06 11:58 AM
Also of note, the TXE bit never gets sent after I send data.
2014-10-07 12:04 AM
I meant, *all* USART registers (including CR1, CR2, CR3 and BRR).
JW2014-10-07 06:53 AM
All other registers are 0