Files
d_power/B-G474E-DPOW1_Keil/Core/Src/retarget.c
2026-05-23 18:02:41 +03:00

102 lines
2.0 KiB
C

#include "retarget.h"
#include <stdio.h>
#include "app_config.h"
#include "stm32g474xx.h"
#if defined(__ARMCC_VERSION) && (__ARMCC_VERSION < 6000000)
#pragma import(__use_no_semihosting)
struct __FILE
{
int handle;
};
#elif defined(__ARMCC_VERSION)
__asm(".global __use_no_semihosting\n");
#endif
FILE __stdout;
FILE __stdin;
static void gpio_pc10_pc11_to_usart3(void)
{
RCC->AHB2ENR |= RCC_AHB2ENR_GPIOCEN;
(void)RCC->AHB2ENR;
/* PC10 = USART3_TX, PC11 = USART3_RX, AF7: ST-LINK VCP on CN3. */
GPIOC->MODER &= ~((3UL << (10U * 2U)) | (3UL << (11U * 2U)));
GPIOC->MODER |= ((2UL << (10U * 2U)) | (2UL << (11U * 2U)));
GPIOC->OTYPER &= ~((1UL << 10U) | (1UL << 11U));
GPIOC->OSPEEDR |= ((3UL << (10U * 2U)) | (3UL << (11U * 2U)));
GPIOC->PUPDR &= ~((3UL << (10U * 2U)) | (3UL << (11U * 2U)));
GPIOC->AFR[1] &= ~((0xFUL << ((10U - 8U) * 4U)) | (0xFUL << ((11U - 8U) * 4U)));
GPIOC->AFR[1] |= ((7UL << ((10U - 8U) * 4U)) | (7UL << ((11U - 8U) * 4U)));
}
void Retarget_Init(uint32_t baudrate)
{
gpio_pc10_pc11_to_usart3();
RCC->APB1ENR1 |= RCC_APB1ENR1_USART3EN;
(void)RCC->APB1ENR1;
USART3->CR1 = 0U;
USART3->CR2 = 0U;
USART3->CR3 = 0U;
/* APB1 is not prescaled; use the clock that really started. */
USART3->BRR = (SystemCoreClock + (baudrate / 2U)) / baudrate;
USART3->CR1 = USART_CR1_TE | USART_CR1_RE | USART_CR1_UE;
}
void Retarget_PutChar(char ch)
{
while ((USART3->ISR & USART_ISR_TXE_TXFNF) == 0U)
{
__NOP();
}
USART3->TDR = (uint8_t)ch;
}
int fputc(int ch, FILE *f)
{
(void)f;
if (ch == '\n')
{
Retarget_PutChar('\r');
}
Retarget_PutChar((char)ch);
return ch;
}
int fgetc(FILE *f)
{
(void)f;
while ((USART3->ISR & USART_ISR_RXNE_RXFNE) == 0U)
{
__NOP();
}
return (int)(USART3->RDR & 0xFFU);
}
void _ttywrch(int ch)
{
Retarget_PutChar((char)ch);
}
void _sys_exit(int return_code)
{
(void)return_code;
while (1)
{
__NOP();
}
}