-1

I'm new with stm32 microcontrollers, I decided to start with STM32F303RET6 nucleoboard. I'm trying to set up the simplest usart transmission using CMSIS, but for some reason, I cannot see anything in Putty terminal. There are no problems with it if I use HAL_LIB, it works just fine, however, I want to setup it up with CMSIS... Can you please point me, to where I have a mistake?

#include "stm32f3xx.h"
#include "main.h"
void SystemClock_Config(void);

void initClocks(void)
{
    SET_BIT(RCC->AHBENR, RCC_AHBENR_GPIOAEN); // ENABLE GPIO PORT A CLOCK
    RCC->APB1ENR |= RCC_APB1ENR_USART2EN;     // ENABLE UART2 CLOCK
}
void configPinMode(void)
{
    GPIOA->MODER &= ~((3u << 4)    // CLEAR PA2
                     |(3u << 6));  // CLEAR PA3

    // SET PIN MODES
    GPIOA->MODER |= ((2u << 4)     // SET PA2 TO AF
                    |(2u << 6));   // SET PA3 TO AF
}
void setAF(void)
{
    GPIOA->AFR[2] &= ~((15u << 8)       // CLEAR PA2 AF
                      |(15u << 12));     // CLEAR PA3 AF

    // SET ALTERNATE FUNCTION
    GPIOA->AFR[2] |= ((7u << 8)         // SET PA2 AF
                     |(7u << 12));       // SET PA3 AF

}
void configUart1Pins(void)
{
    configPinMode();
    setAF();
}

void configUart1(void)
{
    USART2->CR1 &= ~(USART_CR1_M1       // CLEAR M1 FOR 1 START BIT AND 8 DATA BITS (28)
                    |(0x03 << 26)       // INHIBIT INTERRUPTS AT BITS 26 AND 27     (27/26)
                    |USART_CR1_OVER8    // OVERSAMPLING BY 16                       (15)
                    |USART_CR1_CMIE     // INHIBIT CHARACTER MATCH INTERRUPT        (14)
                    |USART_CR1_MME      // DON'T ENABLE MUTE MODE                   (13)
                    |USART_CR1_M0       // CLEAR M0 FOR 1 START BIT AND 8 DATA BITS (12)
                    |USART_CR1_PCE      // NOT IMPLEMENTING PARITY CONTROL          (10)
                    |(0x1F << 3)        // INHIBIT INTERRUPTS AT BITS 4 TO 8        (4-8)
                    |USART_CR1_TE       // DON'T ENABLE TRANSMITTER JUST YET        (3)
                    |USART_CR1_RE       // DON'T ENABLE RECEIVER JUST YET           (2)
                    |USART_CR1_UE);     // DON'T ENABLE UART1 JUST YET              (0)

    // CONFIGURE USART CR2 REGISTER
    // CLEAR BITS
    USART2->CR2 &= ~(USART_CR2_RTOEN    // DISABLE RECEIVER TIMEOUT             (23)
                    |USART_CR2_ABREN    // NO AUTOMATIC BAUD RATE DETECTION     (20)
                    |USART_CR2_MSBFIRST // TRANSMIT/RECEIVE LSB FIRST           (19)
                    |(0x03 << 16)       // IDLE STATE HIGH FOR RX/TX PINS       (17/16)
                    |USART_CR2_SWAP     // DON'T SWAP FUNCTION OF RX/TX PINS    (15)
                    |USART_CR2_LINEN    // NO LIN MODE                          (14)
                    |(0x03 << 12)       // 1 STOP BIT                           (13/12)
                    |USART_CR2_CLKEN    // DON'T USE CLOCK WITH UART            (11)
                    |USART_CR2_LBDIE);  // NO LIN BREAK DETECTION INTERRUPT     (6)

    // CONFIGURE USART CR3 REGISTER
    // CLEAR BITS
    CLEAR_REG(USART2->CR3);
    // SET BITS
    USART2->CR3 |= (USART_CR3_OVRDIS    // DISABLE OVERRUN FUNCTIONALITY (12)
                   |USART_CR3_ONEBIT);  // USE ONE SAMPLE BIT METHOD     (11)

    // SET BAUD RATE IN BRR REGISTER
    USART2->BRR = 833;                   // 8M/833 = 9600

    // ENABLE UART
    USART2->CR1 |= (USART_CR1_TE        // ENABLE TRANSMITTER (3)
                   |USART_CR1_RE        // ENABLE RECEIVER    (2)
                   |USART_CR1_UE);      // ENABLE UART1       (0)


}

void initUART(void)
{
    initClocks();
    configUart1Pins();
    configUart1();
}

void transmitUart(uint8_t data)     // BYTE OF DATA TO BE TRANSMITTED
{

    while(!(USART2->ISR & USART_ISR_TXE));

    USART2->TDR = data;
}

void transmitStrUart(char* str) // POINTER TO A STRING TO BE TRANSMITTED
{

    while(*str)
    {
         transmitUart(*str++);
    }
}



int main(void)
{

  HAL_Init();
  SystemClock_Config();
  initUART();


  while (1)
  {

    transmitUart(88);
    HAL_Delay(500);


  }

}

void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  {
    Error_Handler();
  }
}


void Error_Handler(void)
{

  __disable_irq();
  while (1)
  {
  }

}
pmacfarlane
  • 3,057
  • 1
  • 7
  • 24
  • I don't understand the array index `2` in this `GPIOA->AFR[2]`. From looking at the [reference manual](https://www.st.com/resource/en/reference_manual/rm0316-stm32f303xbcde-stm32f303x68-stm32f328x8-stm32f358xc-stm32f398xe-advanced-armbased-mcus-stmicroelectronics.pdf) in section 11.4.12 there doesn't seem to be an array of `AFR` registers. At best there are two of them, and so index `2` is out of bounds. – pmacfarlane Aug 05 '23 at 20:54
  • If it works with the HAL, but not with your code, did you compare the two approaches and check the differences? What does the HAL do that your code does differently? Can you start with the HAL and then replace the HAL functions with your own code one function at a time? Then you know which parts are correct and which are not. – wovano Aug 06 '23 at 07:09
  • Also, do you know what works and what doesn't in your code? Did you debug it yet? Does the code run at all? Does it hang somewhere? Did you see any transmission on the UART lines with an scope? – wovano Aug 06 '23 at 07:11
  • Code compiles, AFR[2] as I understood is AFRL register, AFR[1] -> AFRH. MB I need to spend more time understanding how exactly HAL lib working with UART. I have tried to compare these 2 variants, but HAL lib structure for me seems complicated. (I did not find the description of functions, that setup registers for HAL communication) – RustyDoggo Aug 06 '23 at 18:24

0 Answers0