Skip to main content
Visitor II
October 18, 2025
Solved

My STM32 Code only works under debug mode

  • October 18, 2025
  • 5 replies
  • 578 views
#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.

    This topic has been closed for replies.
    Best answer by XY

    Thank you all! Problem SOLVED. I need to reset the board after I flash the codes in.

    5 replies

    Super User
    October 18, 2025

    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

    XYAuthor
    Visitor II
    October 18, 2025

    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.

    Graduate II
    October 18, 2025

    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.

    XYAuthor
    Visitor II
    October 18, 2025

    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.

    Technical Moderator
    October 18, 2025

    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?

    XYAuthor
    Visitor II
    October 18, 2025
    Yes. Sorry for the confusion in my post. ##- Please type your reply above
    this line -##
    Technical Moderator
    October 18, 2025

    @XY wrote:

    Thank you all! Problem SOLVED. I need to reset the board after I flash the codes in.


     


    @XY 

    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

    XYAuthor
    Visitor II
    October 18, 2025

    Have done so!

    XYAuthorAnswer
    Visitor II
    October 18, 2025

    Thank you all! Problem SOLVED. I need to reset the board after I flash the codes in.