Bridgetek / ft9xx-sdk

ft90x SDK
MIT License
6 stars 0 forks source link

Add limited support for stdio library #10

Closed brtchip-gdm closed 1 year ago

brtchip-gdm commented 1 year ago

At the moment the examples rely on a discrete copy of tinyprintf to add stdio-like functionality in the examples. This has been added to the 3rdparty section of the Sources. The goal is to add stdio support (printf, etc) into the system libraries.

brtchip-gdm commented 1 year ago

It is the equivalent of adding this code to the user program or the library:

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <sys/stat.h>

_ssize_t _write_r (struct _reent *ptr, int fd, const void *buf, size_t size)
{
    (void)ptr;
    if ((fd == 0) || (fd == 1) || (fd == 2))
    {
        return (_ssize_t)uart_writen(BOARD_UART, buf, size);
    }
    errno = EBADF;
    return -1;
}

int _close_r (struct _reent *ptr, int fd)
{
    (void)ptr;
    if ((fd == 0) || (fd == 1) || (fd == 2))
    {
        return 0;
    }
    errno = EBADF;
    return -1;
}
int _fstat_r (struct _reent *ptr, int fd, struct stat *st)
{
    (void)ptr;
    (void)st;
    if ((fd == 0) || (fd == 1) || (fd == 2))
    {
        return 0;
    }
    errno = EBADF;
    return -1;
}
int _getpid_r (struct _reent *ptr)
{
    (void)ptr;
    return 0;
}
int _isatty_r (struct _reent *ptr, int fd)
{
    (void)ptr;
    if ((fd == 0) || (fd == 1) || (fd == 2))
        return 1;
    errno = ENOTTY;
    return 0;
}
int _kill_r (struct _reent *ptr, int pid, int sig)
{
    (void)ptr;
    (void)pid;
    (void)sig;
    return 0;
}
_off_t _lseek_r (struct _reent *ptr, int fd, _off_t offset, int whence)
{
    (void)ptr;
    (void)offset;
    (void)whence;
    if ((fd == 0) || (fd == 1) || (fd == 2))
        return (off_t)0;
    errno = EBADF;
    return (off_t)-1;
}
void *_sbrk_r (struct _reent *ptr, ptrdiff_t diff)
{
    (void)ptr;
    (void)diff;
    return NULL;
}

Paste it in and enjoy printf without adding tinyprintf to your projects. The resultant code is larger though.

brtchip-gdm commented 1 year ago

This has been implemented with a file called "Source/src/bootstrap.c". The stdio library can safely be added to the binaries now. To use the stdin/stdout/stderr pipes open the UART peripheral in the application before reading or writing any of the stdio pipes. The baud rate and other characteristics are set in the uart_open call:

    /* Enable the UART Device... */
    sys_enable(sys_device_uart0);
    /* Make GPIO48 function as UART0_TXD and GPIO49 function as UART0_RXD... */
    gpio_function(48, pad_uart0_txd); /* UART0 TXD */
    gpio_function(49, pad_uart0_rxd); /* UART0 RXD */

    /* Enable tfp_printf() functionality... */
    init_printf(UART0, tfp_putc);

    // Open the UART using the coding required.
    uart_open(UART0, 1, UART_DIVIDER_115200_BAUD, uart_data_bits_8, uart_parity_none, uart_stop_bits_1);

    printf("Hello, world!\r\n");