|
|
|
@ -21,7 +21,6 @@
|
|
|
|
|
#include"pin.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define USART2_EN (1U << 17)
|
|
|
|
|
#define CR1_TE_EN (1U << 3)
|
|
|
|
|
#define CR1_UE_EN (1U << 0)
|
|
|
|
|
|
|
|
|
@ -30,8 +29,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;
|
|
|
|
@ -43,25 +41,52 @@ void print_Usart(char *ptr)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void usartInit()
|
|
|
|
|
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*/
|
|
|
|
|
pinConfig(pinA2, alternate, def_stage, def_res, def_speed);
|
|
|
|
|
pinConfig(pinA15, alternate, def_stage, def_res, def_speed);
|
|
|
|
|
usartInitTx(pinTx);
|
|
|
|
|
usartInitRx(pinRx);
|
|
|
|
|
|
|
|
|
|
//Sets pina2 to alternate mode as UART_TX
|
|
|
|
|
pinSetAlternate(pinA2, AF1);
|
|
|
|
|
|
|
|
|
|
//Enable the priferas bus and UART
|
|
|
|
|
RCC->APB1ENR |= USART2_EN;
|
|
|
|
|
//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(UART_BAUD);
|
|
|
|
|
usartSetBaudRate(channel,baud);
|
|
|
|
|
|
|
|
|
|
//enables the transmit and sets all the other values to 0 = Default
|
|
|
|
|
USART2->CR1 = CR1_TE_EN; // The = 0 is on purpose to set uart to default mode.
|
|
|
|
|
USART_CHANNEL->CR1 = CR1_TE_EN; // The = 0 is on purpose to set uart to default mode.
|
|
|
|
|
|
|
|
|
|
//UART Enable
|
|
|
|
|
USART2->CR1 |= CR1_UE_EN;
|
|
|
|
|
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)
|
|
|
|
@ -70,9 +95,9 @@ static uint16_t usartComputeBaudRate(uint32_t clk, uint32_t baud)
|
|
|
|
|
return(clk/baud);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void usartSetBaudRate(uint32_t baud)
|
|
|
|
|
void usartSetBaudRate(usartNo_t channel, uint32_t baud)
|
|
|
|
|
{
|
|
|
|
|
USART2->BRR = usartComputeBaudRate(SYST_CLK,baud);
|
|
|
|
|
USART_CHANNEL->BRR = usartComputeBaudRate(SYST_CLK,baud);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void usartSendChar(int ch)
|
|
|
|
|