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
wprasek
Associate II
Posted on October 14, 2014 at 20:33

Any ideas?

Posted on October 15, 2014 at 01:47

You're probably going to want to stop poking things with the debugger.

[DEAD LINK /public/STe2ecommunities/mcu/Lists/STM32Java/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/STM32Java/stm32l100%20UART&FolderCTID=0x01200200770978C69A1141439FE559EB459D758000F9A0E3A95BA69146A17C2E80209ADC21&currentviews=309]https://my.st.com/public/STe2ecommunities/mcu/Lists/STM32Java/Flat.aspx?RootFolder=%2Fpublic%2FSTe2ecommunities%2Fmcu%2FLists%2FSTM32Java%2Fstm32l100%20UART&FolderCTID=0x01200200770978C69A1141439FE559EB459D758000F9A0E3A95BA69146A17C2E80209ADC21¤tviews=309

/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/STM32L15x%20UART%20Configuration%20and%20use%20problem&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B&currentviews=2292

// STM32L15x USART1 LOOP (PA.9 Tx, PA.10 Rx) - sourcer32@gmail.com
#include ''stm32l1xx.h''
/**************************************************************************************/
void RCC_Configuration(void)
{
/* --------------------------- System Clocks Configuration -----------------*/
/* USART1 clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
/* GPIOA clock enable */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
}
/**************************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Configure USART Tx & Rx as alternate function */
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);
/* Mux out USART1 Tx & Rx */
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);
}
/**************************************************************************************/
void USART1_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
/* USARTx configuration ------------------------------------------------------*/
/* USARTx configured as follow:
- BaudRate = 9600 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 = 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 OutString(char *s)
{
while(*s)
{
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); // Wait for Empty
USART_SendData(USART1, *s++);
}
}
/**************************************************************************************/
int main(void)
{
RCC_Configuration();
GPIO_Configuration();
USART1_Configuration();
OutString(''This is an echo back test for USART1

'');
while(1) // Don't want to exit
{
uint16_t Data;
while(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET); // Wait for Char
Data = USART_ReceiveData(USART1); // Collect Char
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); // Wait for Empty
USART_SendData(USART1, Data); // Echo Char
}
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf(''Wrong parameters value: file %s on line %d

'', file, line) */
/* Infinite loop */
while (1);
}
#endif

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on October 15, 2014 at 09:46

Clive, while you are right that

> All other registers are 0

Including BRR?

JW

wprasek
Associate II
Posted on October 17, 2014 at 15:33

Yes...  including BRR

Posted on October 17, 2014 at 17:42

Sounds like your tools or clocks are busted. You're using CooCox, get some better tools. Both Keil and IAR have free evaluations, and valid/workable template projects provided by ST.

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