2025-10-17 6:52 PM - last edited on 2025-10-18 1:24 AM by Andrew Neil
#include <stdint.h>
#include <stdio.h>
#include "stm32f4xx.h"
volatile uint8_t rxdata = 0;
int main(void)
{
SystemInit();
SystemCoreClockUpdate();
// clock configutation
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); // TX: PB6 RX: PB7
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); // LED LED4: PD12 LED3: PD13
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
// gpio initialization
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_StructInit(&GPIO_InitStruct);
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStruct);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_USART1);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_USART1);
// usart initialization
USART_InitTypeDef USART_InitStruct;
USART_StructInit(&USART_InitStruct);
USART_InitStruct.USART_BaudRate = 115200;
USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_InitStruct.USART_Parity = USART_Parity_No;
USART_InitStruct.USART_StopBits = USART_StopBits_1;
USART_InitStruct.USART_WordLength = USART_WordLength_8b;
USART_Init(USART1, &USART_InitStruct);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_Cmd(USART1, ENABLE); // enable usart1
NVIC_InitTypeDef NVIC_InitStruct;
NVIC_InitStruct.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStruct);
char msg[64];
while(1){
USART_SendData(USART1, 'A');
if (rxdata!= 0) {
int idx = 0;
int len = snprintf(msg, sizeof(msg), "Recieved: %c", (char)rxdata);
for (int idx = 0; idx < len; idx++) {
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
USART_SendData(USART1, (uint16_t)msg[idx]);
}
rxdata = 0;
}
}
}
void USART1_IRQHandler(void) {
if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) {
rxdata = (uint8_t)USART_ReceiveData(USART1); // echo
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
}
}
USART uses PB6 and PB7 as the default PA9 is used as VBUS in USB and prints random characters. Does anyone know what's wrong?
Edited to apply source code formatting - please see How to insert source code for future reference.
2025-10-18 1:29 AM - edited 2025-10-18 1:32 AM
Welcome to the forum.
Please give more details of your setup - target hardware, host system, tools, etc - see How to write your question to maximize your chances to find a solution for best results.
In particular, what do you mean by, "only works under debug mode" - as opposed to what?
@XY wrote:prints random characters. Does anyone know what's wrong?
Usually, random or "junk" characters indicate that the baud rate is wrong.
PS:
You haven't shown the code for your functions USART_SendData(), USART_ReceiveData(), etc
2025-10-18 3:18 AM
Some disrupted info Label F7 , but in code f4, and for newbie seems you is better use MXCube HAL generated code as your legacy st lib.
2025-10-18 4:04 AM
Hello @XY and welcome to the community,
I dind't undrestand the relation between the title and the content of your request!
Do you mean USART does print correctly the characters in debug and it doensn't in standalone?