1

TX works and prints info in console (I use Putty) but when I send on STM and echo I don't receive anything (and when I write symbols in console they don't appear). Baud rate and other config settings are correct.

  1. init_pa9() - Configures PA9 as UART1 TX (output) pin.
  2. init_pa10() - Configures PA10 as UART1 RX (input) pin with pull-up.
  3. init_usart1() - Initializes USART1 with 115200 baud, 8N1, enables TX and RX, and sets up RX interrupt.
  4. UART_SendChar() / UART_SendString() - Functions to transmit a single character or a string over UART.
  5. USART1_IRQHandler() - Interrupt handler for USART1; reads received data from RX and immediately sends it back (echo).
  6. main() - Initializes pins and UART, sends a welcome message, then waits in an infinite loop; all echoing happens via interrupts.
#include "stm32h7xx.h"
#include "stm32h7xx_ll_gpio.h"
#include "stm32h7xx_ll_tim.h"
#include "stm32h7xx_ll_rcc.h"
#include "stm32h7xx_ll_bus.h"
#include "stm32h7xx_ll_usart.h"
#include "stm32h7xx_hal.h"
#include "stdbool.h"

void init_pa9(void) {
  //USART1_TX
  LL_AHB4_GRP1_EnableClock(LL_AHB4_GRP1_PERIPH_GPIOA);

  LL_GPIO_SetPinMode(GPIOA, LL_GPIO_PIN_9, LL_GPIO_MODE_ALTERNATE);
  LL_GPIO_SetAFPin_8_15(GPIOA, LL_GPIO_PIN_9, LL_GPIO_AF_7);
  LL_GPIO_SetPinSpeed(GPIOA, LL_GPIO_PIN_9, LL_GPIO_SPEED_FREQ_HIGH);
  LL_GPIO_SetPinPull(GPIOA, LL_GPIO_PIN_9, LL_GPIO_PULL_NO);
  LL_GPIO_SetPinOutputType(GPIOA, LL_GPIO_PIN_9, LL_GPIO_OUTPUT_PUSHPULL);
}
void init_a10(void) {
    // USART1_RX
    LL_AHB4_GRP1_EnableClock(LL_AHB4_GRP1_PERIPH_GPIOA);

    LL_GPIO_SetPinMode(GPIOA, LL_GPIO_PIN_10, LL_GPIO_MODE_ALTERNATE);
    LL_GPIO_SetAFPin_8_15(GPIOA, LL_GPIO_PIN_10, LL_GPIO_AF_7);
    LL_GPIO_SetPinSpeed(GPIOA, LL_GPIO_PIN_10, LL_GPIO_SPEED_FREQ_HIGH);
    LL_GPIO_SetPinPull(GPIOA, LL_GPIO_PIN_10, LL_GPIO_PULL_NO);
    LL_GPIO_SetPinOutputType(GPIOA, LL_GPIO_PIN_10, LL_GPIO_OUTPUT_PUSHPULL);
}

void init_USART1 (){
    LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_USART1);

    LL_RCC_SetUSARTClockSource(LL_RCC_USART16_CLKSOURCE_PCLK2);
    LL_USART_Disable(USART1);
    LL_USART_SetDataWidth(USART1, LL_USART_DATAWIDTH_8B);
    LL_USART_SetStopBitsLength(USART1, LL_USART_STOPBITS_1);
    LL_USART_SetParity(USART1, LL_USART_PARITY_NONE);
    LL_USART_SetTransferDirection(USART1, LL_USART_DIRECTION_TX_RX);
    LL_USART_SetBaudRate(USART1, SystemCoreClock, LL_USART_PRESCALER_DIV1, LL_USART_OVERSAMPLING_16, 115200);

    LL_USART_EnableIT_RXNE_RXFNE(USART1);
    NVIC_EnableIRQ(USART1_IRQn);

    LL_USART_Enable(USART1);

    while(!LL_USART_IsActiveFlag_TEACK(USART1));
}

void UART_SendChar(char ch) {
    while (!LL_USART_IsActiveFlag_TXE_TXFNF(USART1)); // <--- исправлено
    LL_USART_TransmitData8(USART1, ch);
}

void UART_SendString(const char *str) {
    while (*str) {
        UART_SendChar(*str++);
    }
}

void USART1_IRQHandler() {
    if (LL_USART_IsActiveFlag_RXNE_RXFNE(USART1))
    {
        uint8_t rx_byte = LL_USART_ReceiveData8(USART1);
        LL_USART_TransmitData8(USART1, rx_byte);
    }
}

int main(void) {
  init_pa9();
  init_a10();
  init_USART1();

  UART_SendString("Hello from STM32H7!\r\n");
  UART_SendString("UART TX working!\r\n");
  while (1) {
  }
}
13
  • 1
    Do not add photos and screen shot: just copy the output from putty. Also "not functioning" is not much help. Describe what it is not working: do you get nothing? garbage? Or error when you try to read? Commented Nov 11 at 10:26
  • When I try to enter characters in the Putty console, they don't appear, and the green slider doesn't move to the right. This means I don't see any characters or echo from the STM at all. I think the receiver is configured incorrectly. (sorry for my bad english) Commented Nov 11 at 10:35
  • What happens when you send a byte to your device while you run your code in a debugger? Do you get an interrupt? Do you see any useful flags in the UART registers? Commented Nov 11 at 11:58
  • Unfortunatelly,i am unabel to use debugger cuz i dont have St-link. i didnt face moments when i needed debug so i just didnt thought about it Commented Nov 11 at 12:35
  • 1
    @sawdust. Oh, I am certain, but would advise anyone to refer to the reference manual rather than take my word for it. The "output type" can be set, but only has meaning when configured as an output. Apologies if I was less emphatic than you would have liked. You were not wrong to question it, it is redundant and confusing, but not actually causing the observed failure. Commented Nov 14 at 14:45

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.