Hello everyone I have been hitting my head against keyboard from last 5 hours now, I am unable to debug what’s the problem with the code,
from the user manual of the board I got to know that COM port of the board is connected to USART1 ,
and USART tx is connected to PA9, USART1 gets the clock from BUS APB2,
so I wrote the code accordingly and used putty to check the serial output but it displays nothing (I checked the COM port its COM3). Can anyone help ?
#include <stdint.h>
#include "stm32f4xx.h"
#define GPIOAEN (1U<<0)
#define USART1EN (1U<<4)
#define SYS_FREQ 16000000U
#define APB2_CLK SYS_FREQ
#define UART_BAUDRATE 115200
#define CR1_TE (1U<<3)
#define CR1_UE (1U<<13)
#define SR_TXE (1U<<7)
//how many UART? Which one to use
// PA9 and PA10 are TX and RX pin
static void uart_set_baudrate(USART_TypeDef *USARTx,uint32_t PeriphClk,uint32_t BaudRate);
static uint16_t compute_uart_bd(uint32_t PeriphClk,uint32_t BaudRate);
void uart_write(int ch);
void uar1_tx_init(void);
int main(void)
{
uar1_tx_init();
while(1)
{
uart_write('Y');
}
}
void uar1_tx_init(void)
{
/* *********Configure USART gpio pin******
* Enable clock access to gpioa
* set PA9 to alt func mode
* set PA9 alt func type to UART_TX (AF07)*/
RCC->AHB1ENR |=GPIOAEN;
GPIOA->MODER &=~(1U<<18);
GPIOA->MODER |=(1U<<19);
GPIOA->AFR[1] |=(1U<<4);
GPIOA->AFR[1] |=(1U<<5);
GPIOA->AFR[1] |=(1U<<6);
GPIOA->AFR[1] &=~(1U<<7);
/*****Configure usart module***
* Enable clock access to usart
* configure baudrate
*configure the transfer direction
Enable the usart module */
RCC->APB2ENR |= USART1EN;
uart_set_baudrate(USART1,APB2_CLK,UART_BAUDRATE);
USART1->CR1=CR1_TE; //enable txUSART
USART1->CR1|=CR1_UE;
}
void uart_write(int ch)
{
/*make sute tx data register is empty
* write to tx reg*/
while(!(USART1->SR & SR_TXE)){};
USART1->DR=(ch & 0xFF);//since we want to transmit 8 bit
}
static void uart_set_baudrate(USART_TypeDef *USARTx,uint32_t PeriphClk,uint32_t BaudRate)
{
USARTx->BRR=compute_uart_bd(PeriphClk,BaudRate);
}
static uint16_t compute_uart_bd(uint32_t PeriphClk,uint32_t BaudRate)
{
return (PeriphClk+(BaudRate/2U)/BaudRate);
}