#include <stm32f103x6.h>
#include <stdio.h>
#define Perpher_CLK 8000000UL
#define Baudrate 115200UL
void uart1_init(void)
{
RCC->APB2ENR |= RCC_APB2ENR_USART1EN;
RCC->APB2ENR |= RCC_APB2ENR_USART1EN;
GPIOA->CRH |= (GPIO_CRH_MODE9 | GPIO_CRH_CNF9_1); // Max op speed is 50MHz, set to alternate functionality
GPIOA->CRH &= ~GPIO_CRH_CNF9_0; // setting the alternate funtion to push pull type
GPIOA->CRH |= (GPIO_CRH_MODE10 | GPIO_CRH_CNF10_1); // Max op speed is 50MHz, set to alternate functionality
GPIOA->CRH &= ~GPIO_CRH_CNF10_0; // setting the alternate funtion to push pull type
USART1->CR1 |= RCC_APB2ENR_IOPAEN; // ENABLING THE USART
USART1->CR1 |= RCC_APB2ENR_AFIOEN;
USART1->CR1 |= USART_CR1_TE; // USART1 Transmitter enable
USART1->CR1 |= USART_CR1_RE; // USART1 Receiver enable
}
uint16_t compute_uart_bd(uint32_t perif_clk, uint32_t BaudRate)
{
return ((perif_clk + (BaudRate / 2U)) / BaudRate);
}
void uart_set_baudrate(USART_TypeDef *USARTx, uint32_t perif_clk, uint32_t BaudRate)
{
USARTx->BRR = compute_uart_bd(perif_clk, BaudRate);
}
void uart1_write(int ch)
{
// Make sure the transmit data register is empty
while (!(USART1->SR & USART_SR_TXE))
;
// Write the transmit data register
USART1->DR = (ch & 0xFF);
}
char uart1_read(void){
// Make sure the recive data register is not empty
while (!(USART1->SR & USART_SR_RXNE))
;
return USART1->DR;
}
/*Retargeting printf*/
int __io_putchar(int ch)
{
uart1_write(ch);
return ch;
}
int main(void)
{
char key;
uart1_init(); // setting the alternate funtion to push pull type
uart_set_baudrate(USART1, Perpher_CLK, Baudrate);
while (1)
{
uart1_write('y');
printf("Hello\n");
key = uart1_read();
if (key == 'i')
{
printf("%s", &key);
}
}
}
Loading
stm32-bluepill
stm32-bluepill