#include <cstdio> // For std::printf
#include <iostream> // For std::cout (optional, see explanation)

UART_HandleTypeDef huart2;

void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART2_UART_Init(void);

int main() {
  HAL_Init();

  SystemClock_Config();

  MX_GPIO_Init();
  MX_USART2_UART_Init();

  // Redirect printf to UART2 (Important for Wokwi)
  std::setvbuf(stdout, nullptr, _IONBF, 0);

  // You can use either std::printf or std::cout
  std::printf("Starting STM32 C031C6 Blinky with Serial Output (C++)\r\n");
  // Or:
  // std::cout << "Starting STM32 C031C6 Blinky with Serial Output (C++ using cout)" << std::endl;

  while (1) {
    HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5); // Toggle LED (PA5)

    std::printf("LED toggled!\r\n");
    // Or:
    // std::cout << "LED toggled! (using cout)" << std::endl;

    HAL_Delay(500); // Delay 500ms
  }
}

// ... (SystemClock_Config and MX_GPIO_Init remain the same)

static void MX_USART2_UART_Init(void) {
  huart2.Instance = USART2;
  huart2.Init.BaudRate = 115200;
  huart2.Init.WordLength = UART_WORDLENGTH_8B;
  huart2.Init.StopBits = UART_STOPBITS_1;
  huart2.Init.Parity = UART_PARITY_NONE;
  huart2.Init.Mode = UART_MODE_TX_RX;
  huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  if (HAL_UART_Init(&huart2) != HAL_OK) {
    Error_Handler();
  }
}

void Error_Handler(void) {
  __disable_irq();
  while (1) {
    // Error handling code
  }
}
Loading
st-nucleo-c031c6