You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
109 lines
2.5 KiB
109 lines
2.5 KiB
/**
|
|
**************************************************************************************************
|
|
* @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 CR1_TE_EN (1U << 3)
|
|
#define CR1_UE_EN (1U << 0)
|
|
|
|
#define ISR_TXE (1U << 7)
|
|
|
|
#define UART_BAUD 115200
|
|
#define SYST_CLK 8000000
|
|
#define AF1 0x01
|
|
#define USART_CHANNEL ((USART_TypeDef *)channel)
|
|
void print_Usart(char *ptr)
|
|
{
|
|
uint16_t len = 0;
|
|
|
|
while(ptr[len] != '\0')
|
|
{
|
|
usartSendChar(ptr[len]);
|
|
len++;
|
|
}
|
|
}
|
|
|
|
void usartInit( usartNo_t channel,
|
|
pinNo_t pinTx,
|
|
pinNo_t pinRx,
|
|
uint32_t baud,
|
|
usartWordLength_t lenght,
|
|
uint8_t parity,
|
|
uint8_t hwFlowControl)
|
|
{
|
|
/* COnfiguring teh pins for the uart*/
|
|
usartInitTx(pinTx);
|
|
usartInitRx(pinRx);
|
|
|
|
//Enable the UART Module on the periferal bus this must be done before setting any regiter.
|
|
if(channel == usart2)
|
|
{
|
|
RCC->APB1ENR |= RCC_APB1ENR_USART2EN;
|
|
}
|
|
else
|
|
{
|
|
RCC->APB2ENR |= RCC_APB2ENR_USART1EN;
|
|
}
|
|
|
|
usartSetBaudRate(channel,baud);
|
|
|
|
//enables the transmit and sets all the other values to 0 = Default
|
|
USART_CHANNEL->CR1 = CR1_TE_EN; // The = 0 is on purpose to set uart to default mode.
|
|
|
|
//UART Enable
|
|
USART_CHANNEL->CR1 |= CR1_UE_EN;
|
|
}
|
|
|
|
void usartInitTx(pinNo_t pinTx)
|
|
{
|
|
pinConfig(pinTx, alternate, def_stage, def_res, def_speed);
|
|
pinSetAlternate(pinTx, AF1);
|
|
}
|
|
|
|
void usartInitRx(pinNo_t pinRx)
|
|
{
|
|
pinConfig(pinRx, alternate, def_stage, def_res, def_speed);
|
|
pinSetAlternate(pinRx, AF1);
|
|
}
|
|
|
|
void usartSetWordLenght(usartNo_t channel, usartWordLength_t lenght)
|
|
{
|
|
|
|
}
|
|
|
|
static uint16_t usartComputeBaudRate(uint32_t clk, uint32_t baud)
|
|
{
|
|
//return((clk + (baud/2U))/baud);
|
|
return(clk/baud);
|
|
}
|
|
|
|
void usartSetBaudRate(usartNo_t channel, uint32_t baud)
|
|
{
|
|
USART_CHANNEL->BRR = usartComputeBaudRate(SYST_CLK,baud);
|
|
}
|
|
|
|
void usartSendChar(int ch)
|
|
{
|
|
// Make sure that TX buffer is empty
|
|
while(!(USART2->ISR & ISR_TXE)){}
|
|
USART2->TDR = (ch & 0xFF);
|
|
}
|