2015-08-03 06:52 AM
2015-08-03 11:51 AM
Here's how you'd do it using the Snippets library, you'll have to port to Cube yourself
// STM32L053R8-NUCLEO USART2 9600 baud PA2 (TX) PA3 (RX) - sourcer32@gmail.com
/* Includes ------------------------------------------------------------------*/
#include ''stm32l0xx.h''
#include <
stdio.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_USART2(void);
void Configure_USART2(void);
static __IO uint32_t Tick;
/***************************************************************************/
void USART_String(const char *s)
{
while(*s)
{
while(USART2->ISR & USART_ISR_TXE); // Wait for TXE to assert
USART2->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_USART2();
Configure_USART2();
USART_String(''L0 USART2
'');
USART_String(''The quick brown dog jumps over the lazy dog
'');
printf(''Hello World!
'');
while(1) /* Infinite loop */
{
uint16_t data;
// Echo Demo
while(USART2->ISR & USART_ISR_RXNE); // Wait for RXNE to assert
data = USART2->RDR; // Read receive register
while(USART2->ISR & USART_ISR_TXE); // Wait for TXE to assert
USART2->TDR = data; // Write transmit register
}
}
/******************************************************************************/
/* Hosting of stdio functionality through USART2 */
/******************************************************************************/
#include <
rt_misc.h
>
#pragma import(__use_no_semihosting_swi)
struct __FILE { int handle; /* Add whatever you need here */ };
FILE __stdout;
FILE __stdin;
int fputc(int ch, FILE *f)
{
while(USART2->ISR & USART_ISR_TXE); // Wait for TXE to assert
USART2->TDR = ch; // Write transmit register
return(ch);
}
int fgetc(FILE *f)
{
char ch;
while(USART2->ISR & USART_ISR_RXNE); // Wait for RXNE to assert
ch = USART2->RDR; // Read receive register
return((int)ch);
}
int ferror(FILE *f)
{
/* Your implementation of ferror */
return EOF;
}
void _ttywrch(int ch)
{
while(USART2->ISR & USART_ISR_TXE); // Wait for TXE to assert
USART2->TDR = ch; // Write transmit register}
}
void _sys_exit(int return_code)
{
label: goto label; /* endless loop */
}
/***************************************************************************/
/**
* 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 USART2 pins on GPIO PA2 PA3
* Param None
* Retval None
*/
void Configure_GPIO_USART2(void)
{
/* Enable the peripheral clock of GPIOA */
RCC->IOPENR |= RCC_IOPENR_GPIOAEN;
/* GPIO configuration for USART2 signals */
/* (1) Select AF mode (10) on PA2 and PA3 */
/* (2) AF4 for USART2 signals */
GPIOA->MODER = (GPIOA->MODER & ~(GPIO_MODER_MODE2|GPIO_MODER_MODE3))\
| (GPIO_MODER_MODE2_1 | GPIO_MODER_MODE3_1); /* (1) */
GPIOA->AFR[0] = (GPIOA->AFR[0] &~ (0x0000FF00))\
| (4 << (2 * 4)) | (4 << (3 * 4)); /* (2) */
}
/**
* Brief This function configures USART2.
* Param None
* Retval None
*/
void Configure_USART2(void)
{
/* Enable the peripheral clock USART2 */
RCC->APB1ENR |= RCC_APB1ENR_USART2EN;
/* Configure USART2 */
/* (1) oversampling by 16, 9600 baud */
/* (2) 8 data bit, 1 start bit, 1 stop bit, no parity */
USART2->BRR = 160000 / 96; /* (1) */
USART2->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.
* Param None
* Retval None
*/
void SysTick_Handler(void)
{
Tick++;
}