Table of Contents
Refer to STM32CubeIDE Input/Output on STM32 Nucleo Board in advance for basics.
1 Create a new STM32 project
2 Start programming
Include standard library header <iostream>.
#include <iostream>
In the main loop, we print messages standard character output and scan an integer from the standard character input.
int i; setvbuf(stdin, NULL, _IONBF, 0); while (1) { std::cout << "cout helo" << std::endl; std::cin >> i; std::cout << "i = " << i << std::endl; HAL_Delay(500); }
Similarly, we implement __io_putchar() and __io_getchar().
extern "C" { int __io_putchar(int ch) { HAL_UART_Transmit(&huart2, (uint8_t *)&ch, 1, HAL_MAX_DELAY); return 0; } int __io_getchar(void) { uint8_t ch = 0; __HAL_UART_CLEAR_OREFLAG(&huart2); HAL_UART_Receive(&huart2, &ch, 1, HAL_MAX_DELAY); HAL_UART_Transmit(&huart2, &ch, 1, HAL_MAX_DELAY); return ch; } }
Note that they are enclosed in extern "C".
This is to stop name mangling on functions __io_putchar() and __io_getchar(), so that the C program (syscalls.c) will be able to see their symbols when linking.
In the end, the whole main.cpp looks like:
When building the project, we will see arm-none-eabi-g++ being invoked instead of arm-none-eabi-gcc to compile C++ files and link.
The sample results will be something like :
3 references
- How to Use C++ with STM32CubeIDE
- stackoverflow
- Arm Developer
- cppreference
- How to use C++ with STM32 HAL
No comments:
Post a Comment