RTC_n32g45x/Log.c

108 lines
2.7 KiB
C

//
// Created by ilya on 13.02.23.
//
#include "Log.h"
#if LOG_ENABLE
#include "n32g45x.h"
#include "n32g45x_gpio.h"
#include "n32g45x_usart.h"
#include "n32g45x_rcc.h"
#define LOG_USARTx USART1
#define LOG_PERIPH RCC_APB2_PERIPH_USART1
#define RCC_ENABLECLK RCC_EnableAPB2PeriphClk
#define LOG_GPIO GPIOA
#define LOG_PERIPH_GPIO RCC_APB2_PERIPH_GPIOA
#define LOG_REMAP 0//GPIO_RMP_USART1
#define LOG_TX_PIN GPIO_PIN_9
#define LOG_RX_PIN GPIO_PIN_10
void log_init(void)
{
GPIO_InitType GPIO_InitStructure;
USART_InitType USART_InitStructure;
// close JTAG
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_AFIO | LOG_PERIPH_GPIO, ENABLE);
if (LOG_REMAP)
{
if (LOG_REMAP == GPIO_RMP_USART1)
{
// release PB4
GPIO_ConfigPinRemap(GPIO_RMP_SW_JTAG_NO_NJTRST, ENABLE);
}
GPIO_ConfigPinRemap(LOG_REMAP, ENABLE);
}
RCC_ENABLECLK(LOG_PERIPH, ENABLE);
GPIO_InitStructure.Pin = LOG_TX_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitPeripheral(LOG_GPIO, &GPIO_InitStructure);
//GPIO_InitStructure.Pin = LOG_RX_PIN;
//GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
//GPIO_InitPeripheral(LOG_GPIO, &GPIO_InitStructure);
USART_InitStructure.BaudRate = 115200;
USART_InitStructure.WordLength = USART_WL_8B;
USART_InitStructure.StopBits = USART_STPB_1;
USART_InitStructure.Parity = USART_PE_NO;
USART_InitStructure.HardwareFlowControl = USART_HFCTRL_NONE;
USART_InitStructure.Mode = USART_MODE_TX;
// init uart
USART_Init(LOG_USARTx, &USART_InitStructure);
// enable uart
USART_Enable(LOG_USARTx, ENABLE);
}
static int is_lr_sent = 0;
int fputc(int ch, FILE* f)
{
if (ch == '\r')
{
is_lr_sent = 1;
}
else if (ch == '\n')
{
if (!is_lr_sent)
{
USART_SendData(LOG_USARTx, (uint8_t)'\r');
/* Loop until the end of transmission */
while (USART_GetFlagStatus(LOG_USARTx, USART_FLAG_TXC) == RESET)
{
}
}
is_lr_sent = 0;
}
else
{
is_lr_sent = 0;
}
USART_SendData(LOG_USARTx, (uint8_t)ch);
/* Loop until the end of transmission */
while (USART_GetFlagStatus(LOG_USARTx, USART_FLAG_TXC) == RESET)
{
}
return ch;
}
#ifdef USE_FULL_ASSERT
__WEAK void assert_failed(const uint8_t* expr, const uint8_t* file, uint32_t line)
{
log_error("assertion failed: `%s` at %s:%d", expr, file, line);
while (1)
{
}
}
#endif // USE_FULL_ASSERT
#endif // LOG_ENABLE