cusartstm32f7

USART Configuration with Registers doesn't Work


I am trying to implement a driver for USART for my board(F746G-Disco). For now, I implement:

#include "UART_DRIVER.h"
#include "stm32f746xx.h"
#include "stm32f7xx_hal.h"
#include "stdint.h"

void uart_gpio_pin_init(void) {
    __HAL_RCC_GPIOA_CLK_ENABLE();
    
    // PA9 FOR USART1 TX AND PB7 FOR USART1 RX
    GPIOA->MODER |= (2U << 18); // ALTERNATE FUNCTION
    GPIOA->OTYPER |= (1U << 9);
    GPIOA->OSPEEDR |= (1U << 19);
    GPIOA->PUPDR |= (1U << 19);
    GPIOA->AFR[1] |= (7U << 4); // AF7

    __HAL_RCC_GPIOB_CLK_ENABLE();
    GPIOB->MODER |= (2U << 6);
    GPIOB->OTYPER |= (1U << 7);
    GPIOB->OSPEEDR |= (1U << 17);
    GPIOB->PUPDR |= (1U << 17);
    GPIOB->AFR[0] |= (7U << 28); // AF7
}

void uart_init(void) {

    uart_gpio_pin_init();
    // disable USART
    USART1->CR1 = 0x00; // Disable Uart => UE=0
    USART1->CR1 &= (~(1U << 28) | ~(1 << 12)); // WORD LENGTH 8-BIT
    USART1->CR1 &= ~(1U << 15); // 16BIT OVERSAMPLING
    USART1->CR1 &= ~(1U << 10); // PARITY BIT DISABLE
    USART1->CR1 |= (1U << 3); // USART1 TRASNMITTER MODE
    USART1->CR1 |= (1U << 2); // USART1 RECEIVER MODE
    
    // 115200kpbs BAUD-RATE, SYSTEMCLOCK:168 MHz
    USART1->BRR = (0x5B2); 

    // ENABLE USART1
    USART1->CR1 = 0x01;
}


void uart_transmit_data(const char *data) {

    for (int var = 0; data[var] != '\0'; ++var) {
        USART1->TDR = (uint32_t)data[var];
        while((USART1->ISR & USART_ISR_TXE) == 0);
    }
}

But the problem is that the value of USART1->CR1 does not change. In the manual, it is said that to have a writable bits, Uart Enable(UE) pin must be 0. I have already did that but still nothing change. reference manual

datasheet(look page 76 for alternate function mapping)

Solution Edit: I have solved the problem. The problem is I didn't enable the clock for USART. Once __HAL_RCC_USART1_CLK_ENABLE(); macro is invoked in uart_gpio_pin_init function just before the register adjustments of USART, we get rid of the problem!


Solution

  • The problem is in the code that the USART clock wasn't enabled. Once __HAL_RCC_USART1_CLK_ENABLE(); macro is invoked in uart_gpio_pin_init function just before the register adjustments of USART, we get rid of the problem!