【国民技术N32项目移植】N32L43XRL-STB串口调试 使用printf打印日志

电子说

1.2w人已加入

描述

通过串口输出打印信息

  • 添加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_10
    
    void 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(...)
    #endif
    
    void 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

    Printf
      审核编辑:汤梓红

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分