因为找出如何做到这一点很棘手(请随意给我和其他人提供我错过的文档参考),下面是我如何将 stdio 连接到 MCUExpresso 调试控制台串行端口。
相关的
gccg++ -D选项:
SERIAL_PORT_TYPE_UART=1
SDK_DEBUGCONSOLE=1
DEBUG_CONSOLE_RX_ENABLE=1
SERIAL_PORT_TYPE_USBCDC=1
DEBUG_CONSOLE_TRANSFER_NON_BLOCKING
USB_DEVICE_CONFIG_CDC_ACM=1
PRINTF_FLOAT_ENABLE=1
PRINTF_ADVANCED_ENABLE=1
newlib_platform_hooks.c(新文件):
#include "newlib_platform_hooks.h"
#include "fsl_debug_console.h"
#include
// Hook implementations assume assumes caller doesn't close and reopen stdin, stdout or stderr
int DbgConsole_SendDataReliable(uint8_t *ch, size_t size);
// TODO: Add more as needed.
int _close(int file) {
return 0;
}
int _isatty(int file) {
return file <= 2;
}
int _fstat(int file, struct stat *st) {
st->st_mode = S_IFCHR;
return 0;
}
int _lseek(int file, int ptr, int dir) {
return 0;
}
int _read(int file, char *s, int len) {
int num_chars_read = 0;
while (num_chars_read < len) {
if (num_chars_read == 0) {
int r = DbgConsole_Getchar();
if (r < 0) {
break;
}
*s++ = r;
} else if (DbgConsole_TryGetchar(s++) != kStatus_Success) {
break;
}
++num_chars_read;
}
return num_chars_read;
}
int _stat(char *file, struct stat *st) {
st->st_mode = S_IFCHR;
return 0;
}
int _write(int fd, char *s, int len) {
if (len > 0) {
if (fd == 1 || fd == 2) {
uint8_t *p = (uint8_t*) s;
uint8_t *next_char_to_write = p;
for (uint8_t *end = next_char_to_write + len; p != end; ++p) {
if (*p == '\n') {
if (next_char_to_write != p) {
DbgConsole_SendDataReliable(next_char_to_write, p - next_char_to_write);
}
DbgConsole_SendDataReliable((uint8_t*) "\r", 1);
next_char_to_write = p;
}
}
if (next_char_to_write != p) {
DbgConsole_SendDataReliable(next_char_to_write, p - next_char_to_write);
}
}
}
return len;
}