|
|
|
@ -4,16 +4,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define USART2_EN (1U << 17)
|
|
|
|
|
USART_TypeDef usart;
|
|
|
|
|
#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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
pinSetAlternate(pinA2, 1);
|
|
|
|
|
//pinConfig(pinA2, alternate, none, none, normal);
|
|
|
|
|
//pinConfig(pinA15, alternate, none, none, normal);
|
|
|
|
|
|
|
|
|
|
//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);
|
|
|
|
|
|
|
|
|
|
//Enable the priferas bus and UART
|
|
|
|
|
RCC->APB1ENR |= USART2_EN;
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
|
|
//UART Enable
|
|
|
|
|
USART2->CR1 |= CR1_UE_EN;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static uint16_t usartComputeBaudRate(uint32_t clk, uint32_t baud)
|
|
|
|
@ -23,10 +52,12 @@ static uint16_t usartComputeBaudRate(uint32_t clk, uint32_t baud)
|
|
|
|
|
|
|
|
|
|
void usartSetBaudRate(uint32_t baud)
|
|
|
|
|
{
|
|
|
|
|
usartComputeBaudRate(baud, 8000000);
|
|
|
|
|
USART2->BRR = usartComputeBaudRate(baud, SYST_CLK);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void usartSendChar(uint8_t ch)
|
|
|
|
|
void usartSendChar(int ch)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
// Make sure that TX buffer is empty
|
|
|
|
|
while(!(USART2->ISR & ISR_TXE)){}
|
|
|
|
|
USART2->TDR = (ch & 0xFF);
|
|
|
|
|
}
|
|
|
|
|