2025-10-17 6:52 PM - edited 2025-10-18 3:43 PM
#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);
}
}
Hardware Setup: STM32F407 Discovery Board connected with ttl. Developing in Keil. USART uses PB6 and PB7 as the default PA9 is used as VBUS in USB and prints random characters.
The code is expected to keep printing 'A' until user inputs; but the serial monitor does not show anything until I turn on the debug mode.
Edited to apply source code formatting - please see How to insert source code for future reference.
Solved! Go to Solution.
2025-10-18 3:44 PM
Thank you all! Problem SOLVED. I need to reset the board after I flash the codes in.
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?
2025-10-18 12:07 PM
2025-10-18 1:29 PM
Thank you for your reply! I am using standard library because the tutorial I am following does so. And I guess std library can be more related with the lower level of the board and gets me familiar with the concepts.
2025-10-18 1:58 PM
Thank you for your help! I've read through the link and will do better next time!
To clarify "does not work" in my title, my code does not print anything on serial monitor instead of junk characters.
2025-10-18 2:00 PM
Hello!
Thank you for your reply!
Yes, the USART does print correctly (ie, 'A's) on serial monitor in debug mode but does not print anything in normal running mode.
2025-10-18 3:42 PM
@XY wrote:Thank you all! Problem SOLVED. I need to reset the board after I flash the codes in.
I see you edited your original post to say you solved your issue.
In this community, you should not edit your orginal post to tell that you solved your issue but you need to add another post and accept it as solution.
Thank you for your understanding
2025-10-18 3:44 PM
Thank you all! Problem SOLVED. I need to reset the board after I flash the codes in.