Basic UART driver on the STM32L152 Discovery Board

Before starting with this post I must advice you something: This board has not free UART pins for any of its three on-chip USARTs! Unbelievable.

So, the best I could do was to disable the on-board leds and then to use those pins for my UART. (BTW, the other USARTs pins are mapped to the on- board LCD.) And as you might know, it will be always a great feature to have at least one UART – RS232 channel in our projects, even for only debugging purposes.

Well, time to get the hands dirty. My little driver is then based upon the USART1 which is mapped onto ports PB6..7.

Driver:

void UART1_Init();
void UART1_Config(uint32_t BaudRate);
void UART1_Cmd(FunctionalState NewState);
void UART1_Write(char *Buffer, uint32_t Len);

void UART1_Init()
{
 GPIO_InitTypeDef GPIO_InitStructure; 

 /* Pin configuration */
 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
 GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
 GPIO_Init(GPIOB, &GPIO_InitStructure);

 /* Alternate function conf */
 GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_USART1 );
 GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_USART1 );

 /* Enable the USART 1 clock */
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
}

void UART1_Config(uint32_t BaudRate)
{
 USART_InitTypeDef USART_InitStructure;

 /* USART conf */
 USART_InitStructure.USART_BaudRate = BaudRate;
 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);
}

void UART1_Cmd(FunctionalState NewState)
{
 /* Enable/disable the USART */
 USART_Cmd(USART1, NewState);
}

void UART1_Write(char *Buffer, uint32_t Len)
{
 for (; Len > 0; Len--) {

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

 USART_SendData(USART1, *Buffer);
 Buffer++;
 }
}

Usage:

#include "stm32l1xx.h"
#include "stm32l1xx_conf.h"

/* You might include here the code above, or may be you want to organize 
it in its own uart1.c and uart1.h files
*/

int main(void)
{
 GPIO_InitTypeDef GPIO_InitStructure;
 USART_InitTypeDef USART_InitStructure;

 RCC_Config();

 UART1_Init();
 UART1_Config(115200);
 UART1_Cmd(ENABLE);

 while (1)
 {
 UART1_Write("hola mundo",11);
 DelaySW(5000);

 UART1_Write("Hello world",12);
 Delay(5000);
 }
}

void DelaySW(uint32_t delay)
{
 uint32_t i=10000;

 for(;delay > 0; delay--){
 for(;i>0;i--)
 ;
 }
}

void RCC_Config(void)
{ 
 RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);

 /* Allow access to the RTC */
 PWR_RTCAccessCmd(ENABLE);

 /* Reset Backup Domain */
 RCC_RTCResetCmd(ENABLE);
 RCC_RTCResetCmd(DISABLE);

 /*!< LSE Enable */
 RCC_LSEConfig(RCC_LSE_ON);

 /*!< Wait till LSE is ready */
 while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
 {}
 /*!< LCD Clock Source Selection */
 RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
}

As always, any comment is welcomed.

Anuncios

5 comentarios sobre “Basic UART driver on the STM32L152 Discovery Board

  1. Hi,
    Thanks for your example,

    Why are you configuring the RCC in your function called RCC_Config(void) ?
    Is it necessary to handle proprely the UART ?

    Thanks

    Théo

    1. Hi,

      Yes, it’s necessary to make a call to the RCC_Config() function ‘cause it configures the system’s clocks. Please take a look to the bolts and nuts of this function so you figure out how it works (btw, it’s not as complicated as it sounds).

    1. Hi

      I’m using Eclipse as IDE. This is better than Coocox ‘cause you’re in control of everything, whilst in Coocox you’re tide to the Coocox’s owners wishes and desires. Of course it’s a little bit harder to get a system up and running in Eclipse from scratch, but it’s worthwhile.

Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s