cancel
Showing results for 
Search instead for 
Did you mean: 

STM32L053 Discovery Kit (UART between PC and Kit)

mark_ko
Associate II
Posted on March 06, 2015 at 09:32

Hi, i am very new to STM32, just installed the IAR environment. Appreciate if anyone could advise.

Is there a UART example of String transfer using UART TX/RX of PA9 and PA10 in Polling mode  for STM32L053 Discovery Kit (Between PC and Kit) ?

Thanks
29 REPLIES 29
Posted on March 06, 2015 at 10:36

Not exactly what you wanted, but there are examples for interrupt-driven USART transmission and reception in the Snippets package http://www.st.com/web/catalog/tools/FM147/CL1794/SC961/SS1743/LN1898/PF260788#

I am sure something similar occurs in the Cube package too, but I don't subscribe to using that.

JW
mark_ko
Associate II
Posted on March 09, 2015 at 01:33

How about Polling examples on UART?

Thanks

Posted on March 09, 2015 at 16:35

Quick blind port by someone not even using this chip...

// STM32L0 USART1 9600 baud PA9 (TX) PA10 (RX) - sourcer32@gmail.com
#include ''stm32l0xx.h''
/** STM32L0_Snippets
*
*/
#define HSI_TIMEOUT_VALUE ((uint32_t)100) /* 100 ms */
#define PLL_TIMEOUT_VALUE ((uint32_t)100) /* 100 ms */
#define CLOCKSWITCH_TIMEOUT_VALUE ((uint32_t)5000) /* 5 s */
int SystemClock_Config(void);
void Configure_GPIO_USART1(void);
void Configure_USART1(void);
static __IO uint32_t Tick;
/***************************************************************************/
void USART_String(const char *s)
{
while(*s)
{
while((USART1->ISR & USART_ISR_TXE) == 0); // Wait for TXE to assert
USART1->TDR = *s++;
}
}
/***************************************************************************/
/**
* Brief Main program.
* Param None
* Retval None
*/
int main(void)
{
SysTick_Config(2000); /* 1ms config */
if (SystemClock_Config() == -1) /* Failed? */
while(1); /* Stop and die here */
SysTick_Config(16000); /* 1ms config */
Configure_GPIO_USART1();
Configure_USART1();
USART_String(''L0 USART1

'');
USART_String(''The quick brown dog jumps over the lazy dog

'');
while(1) /* Infinite loop */
{
uint16_t data;
// Echo Demo
while((USART1->ISR & USART_ISR_RXNE) == 0); // Wait for RXNE to assert
data = USART1->RDR; // Read receive register
while((USART1->ISR & USART_ISR_TXE) == 0); // Wait for TXE to assert
USART1->TDR = data; // Write transmit register
}
}
/***************************************************************************/
/**
* Brief This function configures the system clock @16MHz and voltage scale 1
* assuming the registers have their reset value before the call.
* POWER SCALE = RANGE 1
* SYSTEM CLOCK = PLL MUL8 DIV2
* PLL SOURCE = HSI/4
* FLASH LATENCY = 0
* Param None
* Retval None
*/
int SystemClock_Config(void)
{
uint32_t tickstart;
/* (1) Enable power interface clock */
/* (2) Select voltage scale 1 (1.65V - 1.95V)
i.e. (01) for VOS bits in PWR_CR */
/* (3) Enable HSI divided by 4 in RCC-> CR */
/* (4) Wait for HSI ready flag and HSIDIV flag */
/* (5) Set PLL on HSI, multiply by 8 and divided by 2 */
/* (6) Enable the PLL in RCC_CR register */
/* (7) Wait for PLL ready flag */
/* (8) Select PLL as system clock */
/* (9) Wait for clock switched on PLL */
RCC->APB1ENR |= (RCC_APB1ENR_PWREN); /* (1) */
PWR->CR = (PWR->CR & ~(PWR_CR_VOS)) | PWR_CR_VOS_0; /* (2) */
RCC->CR |= RCC_CR_HSION | RCC_CR_HSIDIVEN; /* (3) */
tickstart = Tick;
while ((RCC->CR & (RCC_CR_HSIRDY |RCC_CR_HSIDIVF)) != (RCC_CR_HSIRDY |RCC_CR_HSIDIVF)) /* (4) */
{
if ((Tick - tickstart ) > HSI_TIMEOUT_VALUE)
{
return(-1);
}
}
RCC->CFGR |= RCC_CFGR_PLLSRC_HSI | RCC_CFGR_PLLMUL8 | RCC_CFGR_PLLDIV2; /* (5) */
RCC->CR |= RCC_CR_PLLON; /* (6) */
tickstart = Tick;
while ((RCC->CR & RCC_CR_PLLRDY) == 0) /* (7) */
{
if ((Tick - tickstart ) > PLL_TIMEOUT_VALUE)
{
return(-1);
}
}
RCC->CFGR |= RCC_CFGR_SW_PLL; /* (8) */
tickstart = Tick;
while ((RCC->CFGR & RCC_CFGR_SWS_PLL) == 0) /* (9) */
{
if ((Tick - tickstart ) > CLOCKSWITCH_TIMEOUT_VALUE)
{
return(-1);
}
}
return(0); // Success
}
/***************************************************************************/
/**
* Brief This function :
- Enables GPIO clock
- Configures the USART1 pins on GPIO PA9 PA10
* Param None
* Retval None
*/
void Configure_GPIO_USART1(void)
{
/* Enable the peripheral clock of GPIOA */
RCC->IOPENR |= RCC_IOPENR_GPIOAEN;
/* GPIO configuration for USART1 signals */
/* (1) Select AF mode (10) on PA9 and PA10 */
/* (2) AF4 for USART1 signals */
GPIOA->MODER = (GPIOA->MODER & ~(GPIO_MODER_MODE9|GPIO_MODER_MODE10))\
| (GPIO_MODER_MODE9_1 | GPIO_MODER_MODE10_1); /* (1) */
GPIOA->AFR[1] = (GPIOA->AFR[1] &~ (0x00000FF0))\
| (4 << (1 * 4)) | (4 << (2 * 4)); /* (2) */
}
/**
* Brief This function configures USART1.
* Param None
* Retval None
*/
void Configure_USART1(void)
{
/* Enable the peripheral clock USART1 */
RCC->APB2ENR |= RCC_APB2ENR_USART1EN;
/* Configure USART1 */
/* (1) oversampling by 16, 9600 baud */
/* (2) 8 data bit, 1 start bit, 1 stop bit, no parity */
USART1->BRR = 160000 / 96; /* (1) */
USART1->CR1 = USART_CR1_TE | USART_CR1_UE; /* (2) */
}
/******************************************************************************/
/* Cortex-M0 Plus Processor Exceptions Handlers */
/******************************************************************************/
/**
* Brief This function handles NMI exception.
* Param None
* Retval None
*/
void NMI_Handler(void)
{
}
/**
* Brief This function handles Hard Fault exception.
* Param None
* Retval None
*/
void HardFault_Handler(void)
{
/* Go to infinite loop when Hard Fault exception occurs */
while (1)
{
}
}
/**
* Brief This function handles SVCall exception.
* Param None
* Retval None
*/
void SVC_Handler(void)
{
}
/**
* Brief This function handles PendSVC exception.
* Param None
* Retval None
*/
void PendSV_Handler(void)
{
}
/**
* Brief This function handles SysTick Handler.
* It toggles the green led if the action has been performed correctly
* and toggles the red led coding the error number
* Param None
* Retval None
*/
void SysTick_Handler(void)
{
Tick++;
}

I'd prefer an SPL release, but ST doesn't seem to want to do that, and I don't want to do Cube. Fixed while() loops.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
mark_ko
Associate II
Posted on March 10, 2015 at 08:23

Thanks. Will take a look.

mark_ko
Associate II
Posted on March 11, 2015 at 07:55

Thanks for the ''while'' loop changes. Works great.

Thanks for sharing the USART_String()

May i know how should i go about Receiving Strings of data?

Thanks

Posted on March 11, 2015 at 13:11

One character at a time?

You could either poll in a loop, or use interrupts. Depends really on what your trying to process, say a human interface, vs a stream from a GPS receiver, etc.

I've posted examples for other STM32 offerings, these could be adapted, as could some other examples from the Snippets release.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
mark_ko
Associate II
Posted on March 12, 2015 at 10:21

hi Clive,

If for stream of gps data, via interrupts. Can you show me the Post or Snippets that you mentioned? thanks

Posted on March 12, 2015 at 16:20

General NMEA line capture about 70% down this thread.

https://community.st.com/0D50X00009Xkal2SAB

 

https://community.st.com/0D50X00009Xkal2SAB#comment-38690

 More complete interrupt based NMEA capture and parse this thread.

https://community.st.com/0D50X00009Xkh8gSAB

 

Edit: Fixed DEAD LINKs, original post from Mar 12, 2015

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on March 12, 2015 at 16:25

Jan posted a link to the Snippets code

This specifically does USART receive via interrupt

STM32L0xx_Snippets_Package_V1.1.1\Projects\USART\02_Receiver\main.c

Cloning this project and merging in the salient code from my example, figure a 5-10 minute exercise.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..