Единственные изменения, которые я сделал при переключении между C и C++, - это запуск _write и моего обработчика сброса. как код C с использованием extern "C" (без которого ничего не происходит).
Ниже приведен мой файл main.cpp, пожалуйста, игнорируйте более уродливое форматирование
Код: Выделить всё
#include
#include
#include
#include
#define GPIOBS 0x48000400
#define RCC_APB2ENR 0x4002104C
// #define ODR 0x14
// #define OTR 0x04
#define SERIAL_PORT "/dev/ttyACM0"
#include "../bsp/stm32l433xx.h"
uint32_t SystemCoreClock = 0;
extern uint32_t _sidata, _sdata, _edata, _sbss, _ebss;
uint32_t arr[2000];
extern "C" {
// Reset handler: set the stack pointer and branch to main().
__attribute__((naked)) void reset_handler(void) {
// Set the stack pointer to the 'end of stack' value.
__asm__("LDR r0, =_estack\n\t"
"MOV sp, r0");
// Branch to main().
__asm__("B main");
}
}
extern "C" {
int __io_putchar(int ch){
while (!(USART2->ISR & USART_ISR_TXE)) {};
USART2->TDR = ch;
return ch;
}
int _write(int handle, const char* data, int size) {
int count = size;
while (count--) {
__io_putchar(*data++);
}
while (!(USART2->ISR & USART_ISR_TXE)) {};
USART2->TDR = 74;
while (!(USART2->ISR & USART_ISR_TXE)) {};
USART2->TDR = 10;
while (!(USART2->ISR & USART_ISR_TXE)) {};
USART2->TDR = 13;
return size;
}
void init(){
// copy the flash varibales into ram
memcpy(&_sdata, &_sidata, 4*(&_edata - &_sdata));
// Clear the .bss section in RAM.
memset(&_sbss, 0x00, 4* (&_ebss -&_sbss));
for (int i = 0; i < 10000; ++i){
arr[i] = i;
}
// Enable floating-point unit.
SCB->CPACR |= (0xF AHB2ENR |= 7;
// Enable floating-point unit.
SCB->CPACR |= (0xF CR |= (RCC_CR_HSION);
while (!(RCC->CR & RCC_CR_HSIRDY)) {
};
RCC->CFGR &= ~(RCC_CFGR_SW);
RCC->CFGR |= (RCC_CFGR_SW_HSI);
while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_HSI) {
};
SystemCoreClock = 16000000;
RCC->APB1ENR1 |= (RCC_APB1ENR1_USART2EN);
RCC->AHB2ENR |= (RCC_AHB2ENR_GPIOAEN);
// Configure pins A2, A15 for USART2 (AF7, AF3).
GPIOA->MODER &= ~((0x3
Подробнее здесь: [url]https://stackoverflow.com/questions/79149447/printf-stops-working-sends-garbage-data-when-transitioning-from-c-to-c-for-s[/url]