AnsweredAssumed Answered

STM32L100C-DISCO USART Problems

Question asked by prock on Oct 2, 2014
Latest reply on Oct 17, 2014 by Clive One
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?

01.#include "misc.h"
02.#include "stm32l1xx.h"
03.#include "stm32l1xx_gpio.h"
04.#include "stm32l1xx_rcc.h"
05.#include "stm32l1xx_usart.h"
06. 
07.void Usart1Put(uint8_t ch)
08.{
09.    USART_SendData(USART1, (uint8_t) ch);
10. 
11.    //Loop until the end of transmission
12.    while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
13.    {
14.    }
15.}
16.uint8_t Usart1Get(void){
17.    while ( USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET);
18.    return (uint8_t)USART_ReceiveData(USART1);
19.}
20. 
21.int main(void)
22.{
23.    GPIO_InitTypeDef GPIO_InitStructure;
24.    USART_InitTypeDef USART_InitStructure;
25.    USART_ClockInitTypeDef USART_ClockInitStructure;
26. 
27.    USART_DeInit(USART1);
28. 
29.    /* Enable GPIO clock */
30.    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
31. 
32.    /* Enable UART clock */
33.    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
34. 
35.    /* Connect PXx to USARTx_Tx */
36.    GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);
37. 
38.    /* Connect PXx to USARTx_Rx */
39.    GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);
40. 
41.    /* Configure USART Tx and Rx as alternate function push-pull */
42.    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
43.    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
44.    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
45.    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
46.    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
47.    GPIO_Init(GPIOA, &GPIO_InitStructure);
48. 
49. 
50.    USART_ClockStructInit(&USART_ClockInitStructure);
51.    USART_ClockInit(USART1, &USART_ClockInitStructure);
52. 
53.    /* USART configuration */
54.    USART_InitStructure.USART_BaudRate = 9600;
55.    USART_InitStructure.USART_WordLength = USART_WordLength_8b;
56.    USART_InitStructure.USART_StopBits = USART_StopBits_1;
57.    USART_InitStructure.USART_Parity = USART_Parity_No;
58.    USART_InitStructure.USART_HardwareFlowControl =
59.            USART_HardwareFlowControl_None;
60.    USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
61. 
62.    USART_Init(USART1, &USART_InitStructure);
63. 
64.    /* Enable USART */
65.    USART_Cmd(USART1, ENABLE);
66. 
67. 
68.    uint8_t c = 'w';
69.    Usart1Put(c);
70.    while (1) {
71.        c = Usart1Get();
72.        Usart1Put(c);
73.    }
74.}

Outcomes