/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
This code transmit a message using the UART peripheral.
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
/* **************** START *********************** */
/* Libraries, Definitions and Global Declarations */
#include <stdint.h>
#include "main.h"
#include "user_uart.h"
uint8_t msg[] = "Hello\r\n";
void USER_RCC_Init( void );
void USER_GPIO_Init( void );
/* Superloop structure */
int main(void)
{
/* Declarations and Initializations */
USER_RCC_Init( );
USER_GPIO_Init( );
USER_UART2_Init( );
/* Repetitive block */
for(;;){
USER_UART2_Transmit( msg, sizeof( msg ));
GPIOA->BSRR = 0x1UL << 5U;
}
}
void USER_RCC_Init( void ){
/* System Clock (SYSCLK) configuration for 48 MHz */
FLASH->ACR &= ~( 0x6UL << 0U );// 2 HCLK cycles latency, if SYSCLK >=24MHz <=48MHz
FLASH->ACR |= ( 0x1UL << 0U );// 2 HCLK cycles latency, if SYSCLK >=24MHz <=48MHz
while(( FLASH->ACR & ( 0x7UL << 0U )) != 0x001UL );// wait until LATENCY[2:0]=001
RCC->CR &= ~( 0x7UL << 11U );// select HSISYS division factor by 1
while(!( RCC->CR & ( 0x1UL << 10U )));// wait until HSISYS is stable and ready
RCC->CFGR &= ~( 0x7UL << 0U );// select HSISYS as the SYSCLK clock source
RCC->CFGR &= ~( 0x1UL << 11U );// select HCLK division factor by 1
}
void USER_GPIO_Init( void ){
// Enable GPIOA clock
RCC->IOPENR = RCC->IOPENR | ( 0x1UL << 0U );
// Configure PA5 as output push pull
GPIOA->BSRR = 0x1UL << 21U; // Reset PA5 low to turn off LED
GPIOA->PUPDR = GPIOA->PUPDR & ~( 0x3UL << 10U ); // Clear pull-up/pull-down bits for PA5
GPIOA->OTYPER = GPIOA->OTYPER & ~( 0x1UL << 5U ); // Clear output type bit for PA5
GPIOA->MODER = GPIOA->MODER & ~( 0x2UL << 10U ); // Set PA5 as output
GPIOA->MODER = GPIOA->MODER | ( 0x1UL << 10U ); // Set PA5 as output
}