N32L43XRL-STB串口调试 使用printf打印日志,xrl

描述

通过串口输出打印信息

  • 添加log.c#include "log.h"#if LOG_ENABLE#include "n32l43x.h"#include "n32l43x_gpio.h"#include "n32l43x_usart.h"#include "n32l43x_rcc.h"#define LOG_USARTx USART1#define LOG_PERIPH RCC_APB2_PERIPH_USART1#define LOG_GPIO GPIOA#define LOG_PERIPH_GPIO RCC_APB2_PERIPH_GPIOA#define LOG_TX_PIN GPIO_PIN_9#define LOG_RX_PIN GPIO_PIN_10void log_init(void){ GPIO_InitType GPIO_InitStructure; USART_InitType USART_InitStructure; GPIO_InitStruct(&GPIO_InitStructure); RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_AFIO | LOG_PERIPH_GPIO, ENABLE); RCC_EnableAPB2PeriphClk(LOG_PERIPH, ENABLE); GPIO_InitStructure.Pin = LOG_TX_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Slew_Rate = GPIO_Slew_Rate_High;GPIO_InitStructure.GPIO_Alternate = GPIO_AF4_USART1; GPIO_InitPeripheral(LOG_GPIO, &GPIO_InitStructure); GPIO_InitStructure.Pin = LOG_RX_PIN; GPIO_InitStructure.GPIO_Pull = GPIO_Pull_Up; GPIO_InitStructure.GPIO_Alternate = GPIO_AF4_USART1; 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_RX | USART_MODE_TX; // init uart USART_Init(LOG_USARTx, &USART_InitStructure); // enable uart USART_Enable(LOG_USARTx, ENABLE);}int fputc(int ch, FILE* f){ USART_SendData(LOG_USARTx, (uint8_t)ch); while (USART_GetFlagStatus(LOG_USARTx, USART_FLAG_TXDE) == 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#endif // LOG_ENABLE#endif // LOG_ENABLE
  • 添加log.h#ifndef __LOG_H__#define __LOG_H__#ifndef LOG_ENABLE#define LOG_ENABLE 1#endif#if LOG_ENABLE#include #define LOG_NONE 0#define LOG_ERROR 10#define LOG_WARNING 20#define LOG_INFO 30#define LOG_DEBUG 40#ifndef LOG_LEVEL#define LOG_LEVEL LOG_DEBUG#endif#if LOG_LEVEL >= LOG_INFO#define log_info(...) printf(__VA_ARGS__)#else#define log_info(...)#endif#if LOG_LEVEL >= LOG_ERROR#define log_error(...) printf(__VA_ARGS__)#else#define log_error(...)#endif#if LOG_LEVEL >= LOG_WARNING#define log_warning(...) printf(__VA_ARGS__)#else#define log_warning(...)#endif#if LOG_LEVEL >= LOG_DEBUG#define log_debug(...) printf(__VA_ARGS__)#else#define log_debug(...)#endifvoid log_init(void);#else /* !LOG_ENABLE */#define log_info(...)#define log_warning(...)#define log_error(...)#define log_debug(...)#define log_init()#endif#define log_func() log_debug("call %s [%d]\\r\\n", __FUNCTION__,__LINE__)#endif /* __LOG_H__ */#endif /* __LOG_H__ */#endif /* __LOG_H__ */
  • man调用int main(void){ log_init(); log_func();//assert_param(0); while (1) { }}}}

异常处理

  • 若无法正常打印信息,则查看keil5配置,需要 打开use microLIB


     审核编辑:汤梓红

相关推荐

相关文章