|
|
|
@ -1,8 +1,26 @@
|
|
|
|
|
/**
|
|
|
|
|
**************************************************************************************************
|
|
|
|
|
* @file usart.c
|
|
|
|
|
* @author Kerem Yollu & Edwin Koch
|
|
|
|
|
* @date 03.11.2021
|
|
|
|
|
* @version 0.4 Unstable
|
|
|
|
|
**************************************************************************************************
|
|
|
|
|
* @brief Implementation of usart.h for the STM32F042K6 MCU
|
|
|
|
|
*
|
|
|
|
|
* **Detailed Description :**
|
|
|
|
|
*
|
|
|
|
|
* This source code uses bit manipulation in order to minimise the footprint of pin initialisation
|
|
|
|
|
* and manipulation. It's based on the CMSIS/Device/ST/STM32F0xx/Include/stm32f042x6.h Header file
|
|
|
|
|
* to get the obtain the right Registers.
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
**************************************************************************************************
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include"usart.h"
|
|
|
|
|
#include"pin.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define USART2_EN (1U << 17)
|
|
|
|
|
#define CR1_TE_EN (1U << 3)
|
|
|
|
|
#define CR1_UE_EN (1U << 0)
|
|
|
|
@ -11,26 +29,18 @@
|
|
|
|
|
|
|
|
|
|
#define UART_BAUD 115200
|
|
|
|
|
#define SYST_CLK 8000000
|
|
|
|
|
|
|
|
|
|
#define AF1 0x01
|
|
|
|
|
|
|
|
|
|
void usartSetBaudRate(uint32_t baud);
|
|
|
|
|
|
|
|
|
|
void usartInit()
|
|
|
|
|
{
|
|
|
|
|
/* COnfiguring teh pins for the uart*/
|
|
|
|
|
//pinConfig(pinA2, alternate, none, none, normal);
|
|
|
|
|
//pinConfig(pinA15, alternate, none, none, normal);
|
|
|
|
|
pinConfig(pinA2, alternate, def_stage, def_res, def_speed);
|
|
|
|
|
pinConfig(pinA15, alternate, def_stage, def_res, def_speed);
|
|
|
|
|
|
|
|
|
|
//Sets pina2 to alternate mode as UART_TX
|
|
|
|
|
//pinSetAlternate(pinA2, 1);
|
|
|
|
|
|
|
|
|
|
GPIOA->MODER &= ~(1u <<4);
|
|
|
|
|
GPIOA->MODER |= (1u <<5);
|
|
|
|
|
|
|
|
|
|
GPIOA->AFR[0] &= ~(1u<<11);
|
|
|
|
|
GPIOA->AFR[0] &= ~(1u<<10);
|
|
|
|
|
GPIOA->AFR[0] &= ~(1u<<9);
|
|
|
|
|
GPIOA->AFR[0] |= (1u<<8);
|
|
|
|
|
pinSetAlternate(pinA2, AF1);
|
|
|
|
|
|
|
|
|
|
//Enable the priferas bus and UART
|
|
|
|
|
RCC->APB1ENR |= USART2_EN;
|
|
|
|
@ -38,8 +48,7 @@ void usartInit()
|
|
|
|
|
usartSetBaudRate(UART_BAUD);
|
|
|
|
|
|
|
|
|
|
//enables the transmit and sets all the other values to 0 = Default
|
|
|
|
|
USART2->CR1 = 0; // The = 0 is on purpose to set uart to default mode.
|
|
|
|
|
USART2->CR1 |= CR1_TE_EN; // The = 0 is on purpose to set uart to default mode.
|
|
|
|
|
USART2->CR1 = CR1_TE_EN; // The = 0 is on purpose to set uart to default mode.
|
|
|
|
|
|
|
|
|
|
//UART Enable
|
|
|
|
|
USART2->CR1 |= CR1_UE_EN;
|
|
|
|
@ -47,12 +56,13 @@ void usartInit()
|
|
|
|
|
|
|
|
|
|
static uint16_t usartComputeBaudRate(uint32_t clk, uint32_t baud)
|
|
|
|
|
{
|
|
|
|
|
return((clk + (baud/2U))/baud);
|
|
|
|
|
//return((clk + (baud/2U))/baud);
|
|
|
|
|
return(clk/baud);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void usartSetBaudRate(uint32_t baud)
|
|
|
|
|
{
|
|
|
|
|
USART2->BRR = usartComputeBaudRate(baud, SYST_CLK);
|
|
|
|
|
USART2->BRR = usartComputeBaudRate(SYST_CLK,baud);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void usartSendChar(int ch)
|
|
|
|
|