本帖最后由 小恩GG 于 2021-3-24 13:57 编辑
概要在MCU开发过程中,经常会用串口打印输出各类信息,如寄存器,变量,标记符的值,各种调试信息等,方便开发者探知软件的实现结果,功能错误原因等。
在本篇分享中,小编会以MCUXpresso IDE和MIMXRT1050-EVK为例,介绍如何在SDK 软件库中实现三种串口打印的实现方式,好的,我们开始吧。
PRINTF打印PRINTF是SDK 软件库自身定义的指向Printf功能函数的宏,代码细节如下所示,而为了使能PRINTF打印功能,需要设置SDK_DEBUGCONSOLE = 1(Project -> Properties -> C/C++ Build -> Setting -> Preprocessor,如下图1所示)在预编译阶段选择串口输出的目标函数:SDK DbgConsole_Printf()。 - #elif SDK_DEBUGCONSOLE == DEBUGCONSOLE_REDIRECT_TO_SDK /* Select printf, scanf, putchar, getchar of SDK version. */
- #define PRINTF DbgConsole_Printf
- #define SCANF DbgConsole_Scanf
- #define PUTCHAR DbgConsole_Putchar
- #define GETCHAR DbgConsole_Getchar
复制代码
图 1
printf打印printf是C标准库提供的打印函数,在使用它之前需定义__MCUXPRESSO和SDK_DEBUGCONSOLE_UART宏(Project -> Properties -> C/C++ Build -> Setting -> Preprocessor,如下图2所示),这样的话,C标准库中的printf和scanf函数的底层实现会指向fsl_debug_console.c中的_write和_read函数(如下所示)。 图 2 - #if ((defined(__GNUC__) && (!defined(__MCUXPRESSO)) && (defined(SDK_DEBUGCONSOLE_UART))) || \
- (defined(__MCUXPRESSO) && (defined(SDK_DEBUGCONSOLE_UART))))
- int __attribute__((weak)) _write(int handle, char *buffer, int size);
- int __attribute__((weak)) _write(int handle, char *buffer, int size)
- {
- if (buffer == NULL)
- {
- /* return -1 if error. */
- return -1;
- }
- /* This function only writes to "standard out" and "standard err" for all other file handles it returns failure. */
- if ((handle != 1) && (handle != 2))
- {
- return -1;
- }
- /* Send data. */
- (void)DbgConsole_SendDataReliable((uint8_t *)buffer, (size_t)size);
- return size;
- }
- int __attribute__((weak)) _read(int handle, char *buffer, int size);
- int __attribute__((weak)) _read(int handle, char *buffer, int size)
- {
- uint8_t ch = 0U;
- int actualSize = 0;
- /* This function only reads from "standard in", for all other file handles it returns failure. */
- if (handle != 0)
- {
- return -1;
- }
- /* Receive data. */
- for (; size > 0; size--)
- {
- if (DbgConsole_ReadCharacter(&ch) < 0)
- {
- break;
- }
- *buffer++ = (char)ch;
- actualSize++;
- if ((ch == 0U) || (ch == (uint8_t)'\n') || (ch == (uint8_t)'\r'))
- {
- break;
- }
- }
- return (actualSize > 0) ? actualSize : -1;
- }
- #endif
复制代码
std::cout输出std::cout是C++标准库提供的输出流功能,所以要使用它的前提是创建的工程是基于C++,或者按照小编先前的分享:《MCUXPresso C++工程变成C工程》,将C工程改造C++工程。
预编译设置跟printf打印实现一样,将C++标准库提供的输出和输入流功能的底层实现同样指向fsl_debug_console.c中的_write和_read函数。 注意:虽然上面介绍的都是串口打印输出的实现步骤,但输出的实现也代表了输入功能也被同样使能了
测试测试代码(请查看附件)是基于SDK软件库创建的C++工程,按照如上介绍,在project -> Properties -> C/C++ Build -> Setting -> Preprocessor中设置相应的宏,将以上三种打印输出方式全都使能,并用他们打印对应的信息,具体代码如下: - #include <stdio.h>
- #include "board.h"
- #include "peripherals.h"
- #include "pin_mux.h"
- #include "clock_config.h"
- #include "MIMXRT1052.h"
- #include "fsl_debug_console.h"
- #include<cstdint>
- #include <iostream>
- /* TODO: insert other include files here. */
- using namespace std;
- /* TODO: insert other definitions and declarations here. */
- /*
- * @brief Application entry point.
- *
- */
- int main(void) {
- /* Init board hardware. */
- BOARD_InitBootPins();
- BOARD_InitBootClocks();
- BOARD_InitBootPeripherals();
- /* Init FSL debug console. */
- BOARD_InitDebugConsole();
- PRINTF("PRINT method of print output\n");
- printf("print method of print output\n");
- cout<< "std::cout method of print output\n" <<endl;
- /* Force the counter to be placed into memory. */
- volatile static int i = 0 ;
- /* Enter an infinite loop, just incrementing a counter. */
- while(1) {
- i++ ;
- /* 'Dummy' NOP to allow source level single stepping of
- tight while() loop */
- __asm volatile ("nop");
- }
- return 0 ;
- }
复制代码
MIMXRT1052 C Project.zip
(781.17 KB, 下载次数: 7)
|