Show and tell: hooking Newlib C and C++ stdio up to debug console.

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

Show and tell: hooking Newlib C and C++ stdio up to debug console.

1,247件の閲覧回数
Slaymaker
Contributor III

Because it was tricky to find out how to do this (feel free to cluebat me and others with a documentation reference I missed), here's how I've hooked up stdio up to the MCUExpresso debug console serial port. YMMV.

Relevant gcc, g++ -D options:

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 (new file):

#include "newlib_platform_hooks.h"

#include "fsl_debug_console.h"

#include <sys/stat.h>

// 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;
}

 

0 件の賞賛
返信
1 返信

1,212件の閲覧回数
ZhangJennie
NXP TechSupport
NXP TechSupport

Hi Slaymaker 

Please let us know your chip part number and how to reproduce the issue with demo code. thus we can assign your question to the right engineer.

Thanks,

Jun Zhang

0 件の賞賛
返信