This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

How printf function can sends a data using UART?

Hi ,
I'm working on FlashFs example for mcb1700 and can run it on my board but i couldn't understand how printf function can send a string to RS232 using UART0 of lpc1768. I read another thread that one asked a similar question on C51 and a person said that printf function calls putchar() and we should modify putchar function to send the string to another UART but the question is this:
however i know that i can modify SER_int()in serial.c for changing the UART port to UART3 or so on.
How the printf function can communicate with SER_getChar()in serial.c file to send a string to serial port?
for example how the following code sends the string to RS232 ic with UART0?
printf("\nRead data from file %s\n",fname);
i couldn't find the source code in program.

thanks.

Parents
  • suppose that i have modified serial.c file as below:

    int SER_putChar (int uart, int c) { LPC_UART_TypeDef *pUart;

    pUart = (uart == 0) ? (LPC_UART_TypeDef *)LPC_UART0 : (LPC_UART_TypeDef *)LPC_UART1; while (!(pUart->LSR & 0x20)); return (pUart->THR = c);
    }

    /*---------------------------------------------------------------------------- Read character from Serial Port (blocking read) *----------------------------------------------------------------------------*/
    int SER_getChar (int uart) { LPC_UART_TypeDef *pUart;

    pUart = (uart == 0) ? (LPC_UART_TypeDef *)LPC_UART0 : (LPC_UART_TypeDef *)LPC_UART1; while (!(pUart->LSR & 0x01)); return (pUart->RBR);
    }

    /*---------------------------------------------------------------------------- Read character from Serial Port (non blocking read) *----------------------------------------------------------------------------*/
    int SER_getChar_nb (int uart) { LPC_UART_TypeDef *pUart;

    pUart = (uart == 0) ? (LPC_UART_TypeDef *)LPC_UART0 : (LPC_UART_TypeDef *)LPC_UART1; if (pUart->LSR & 0x01) return (pUart->RBR); else return 0;
    }

    /*---------------------------------------------------------------------------- Write character to Serial Port *----------------------------------------------------------------------------*/
    void SER_putString (int uart, unsigned char *s) {

    while (*s != 0) { SER_putChar(uart, *s++); }
    }

    and my retarget.c functions are as below

    #define STDIN 0x8001
    #define STDOUT 0x8002
    #define STDERR 0x8003

    /* Standard IO device name defines. */
    const char __stdin_name[] = "STDIN";
    const char __stdout_name[] = "STDOUT";
    const char __stderr_name[] = "STDERR";

    struct __FILE { int handle; /* Add whatever you need here */ };

    #ifdef STDIO
    /*---------------------------------------------------------------------------- Write character to Serial Port *----------------------------------------------------------------------------*/
    int sendchar (int c) {

    if (c == '\n') { SER_putChar (1, 0x0D); } SER_putChar (1, c);

    return (c);
    }

    /*---------------------------------------------------------------------------- Read character from Serial Port (blocking read) *----------------------------------------------------------------------------*/
    int getkey (void) {

    return (SER_getChar(1));
    } #endif

    /*--------------------------- _ttywrch --------------------------------------*/

    void _ttywrch (int ch) {
    #ifdef STDIO sendchar(ch);
    #endif
    }

    /*--------------------------- _sys_open -------------------------------------*/

    FILEHANDLE _sys_open (const char *name, int openmode) { /* Register standard Input Output devices. */ if (strcmp(name, "STDIN") == 0) { return (STDIN); } if (strcmp(name, "STDOUT") == 0) { return (STDOUT); } if (strcmp(name, "STDERR") == 0) { return (STDERR); } return (__fopen (name, openmode));
    }

    /*--------------------------- _sys_close ------------------------------------*/

    int _sys_close (FILEHANDLE fh) { if (fh > 0x8000) { return (0); } return (__fclose (fh));
    }

    /*--------------------------- _sys_write ------------------------------------*/

    int _sys_write (FILEHANDLE fh, const U8 *buf, U32 len, int mode) {
    #ifdef STDIO if (fh == STDOUT) { /* Standard Output device. */ for ( ; len; len--) { sendchar (*buf++); } return (0); }
    #endif if (fh > 0x8000) { return (-1); } return (__write (fh, buf, len));
    }

    /*--------------------------- _sys_read -------------------------------------*/

    int _sys_read (FILEHANDLE fh, U8 *buf, U32 len, int mode) {
    #ifdef STDIO if (fh == STDIN) { /* Standard Input device. */ for ( ; len; len--) { *buf++ = getkey (); } return (0); }
    #endif if (fh > 0x8000) { return (-1); } return (__read (fh, buf, len));
    }

    /*--------------------------- _sys_istty ------------------------------------*/

    int _sys_istty (FILEHANDLE fh) { if (fh > 0x8000) { return (1); } return (0);
    }

    /*--------------------------- _sys_seek -------------------------------------*/

    int _sys_seek (FILEHANDLE fh, long pos) { if (fh > 0x8000) { return (-1); } return (__setfpos (fh, pos));
    }

    /*--------------------------- _sys_ensure -----------------------------------*/

    int _sys_ensure (FILEHANDLE fh) { if (fh > 0x8000) { return (-1); } return (__flushbuf (fh));
    }

    /*--------------------------- _sys_flen -------------------------------------*/

    long _sys_flen (FILEHANDLE fh) { if (fh > 0x8000) { return (0); } return (__get_flen (fh));
    }

    /*--------------------------- _sys_tmpnam -----------------------------------*/

    int _sys_tmpnam (char *name, int sig, unsigned maxlen) { return (1);
    }

    /*--------------------------- _sys_command_string ---------------------------*/

    char *_sys_command_string (char *cmd, int len) { return (cmd);
    }

    /*--------------------------- _sys_exit -------------------------------------*/

    void _sys_exit (int return_code) { /* Endless loop. */ while (1);
    }

    Can you tell me what will happen step by step when following code is written on FLashFS program:

    printf ("\nFile closed.\n");

Reply
  • suppose that i have modified serial.c file as below:

    int SER_putChar (int uart, int c) { LPC_UART_TypeDef *pUart;

    pUart = (uart == 0) ? (LPC_UART_TypeDef *)LPC_UART0 : (LPC_UART_TypeDef *)LPC_UART1; while (!(pUart->LSR & 0x20)); return (pUart->THR = c);
    }

    /*---------------------------------------------------------------------------- Read character from Serial Port (blocking read) *----------------------------------------------------------------------------*/
    int SER_getChar (int uart) { LPC_UART_TypeDef *pUart;

    pUart = (uart == 0) ? (LPC_UART_TypeDef *)LPC_UART0 : (LPC_UART_TypeDef *)LPC_UART1; while (!(pUart->LSR & 0x01)); return (pUart->RBR);
    }

    /*---------------------------------------------------------------------------- Read character from Serial Port (non blocking read) *----------------------------------------------------------------------------*/
    int SER_getChar_nb (int uart) { LPC_UART_TypeDef *pUart;

    pUart = (uart == 0) ? (LPC_UART_TypeDef *)LPC_UART0 : (LPC_UART_TypeDef *)LPC_UART1; if (pUart->LSR & 0x01) return (pUart->RBR); else return 0;
    }

    /*---------------------------------------------------------------------------- Write character to Serial Port *----------------------------------------------------------------------------*/
    void SER_putString (int uart, unsigned char *s) {

    while (*s != 0) { SER_putChar(uart, *s++); }
    }

    and my retarget.c functions are as below

    #define STDIN 0x8001
    #define STDOUT 0x8002
    #define STDERR 0x8003

    /* Standard IO device name defines. */
    const char __stdin_name[] = "STDIN";
    const char __stdout_name[] = "STDOUT";
    const char __stderr_name[] = "STDERR";

    struct __FILE { int handle; /* Add whatever you need here */ };

    #ifdef STDIO
    /*---------------------------------------------------------------------------- Write character to Serial Port *----------------------------------------------------------------------------*/
    int sendchar (int c) {

    if (c == '\n') { SER_putChar (1, 0x0D); } SER_putChar (1, c);

    return (c);
    }

    /*---------------------------------------------------------------------------- Read character from Serial Port (blocking read) *----------------------------------------------------------------------------*/
    int getkey (void) {

    return (SER_getChar(1));
    } #endif

    /*--------------------------- _ttywrch --------------------------------------*/

    void _ttywrch (int ch) {
    #ifdef STDIO sendchar(ch);
    #endif
    }

    /*--------------------------- _sys_open -------------------------------------*/

    FILEHANDLE _sys_open (const char *name, int openmode) { /* Register standard Input Output devices. */ if (strcmp(name, "STDIN") == 0) { return (STDIN); } if (strcmp(name, "STDOUT") == 0) { return (STDOUT); } if (strcmp(name, "STDERR") == 0) { return (STDERR); } return (__fopen (name, openmode));
    }

    /*--------------------------- _sys_close ------------------------------------*/

    int _sys_close (FILEHANDLE fh) { if (fh > 0x8000) { return (0); } return (__fclose (fh));
    }

    /*--------------------------- _sys_write ------------------------------------*/

    int _sys_write (FILEHANDLE fh, const U8 *buf, U32 len, int mode) {
    #ifdef STDIO if (fh == STDOUT) { /* Standard Output device. */ for ( ; len; len--) { sendchar (*buf++); } return (0); }
    #endif if (fh > 0x8000) { return (-1); } return (__write (fh, buf, len));
    }

    /*--------------------------- _sys_read -------------------------------------*/

    int _sys_read (FILEHANDLE fh, U8 *buf, U32 len, int mode) {
    #ifdef STDIO if (fh == STDIN) { /* Standard Input device. */ for ( ; len; len--) { *buf++ = getkey (); } return (0); }
    #endif if (fh > 0x8000) { return (-1); } return (__read (fh, buf, len));
    }

    /*--------------------------- _sys_istty ------------------------------------*/

    int _sys_istty (FILEHANDLE fh) { if (fh > 0x8000) { return (1); } return (0);
    }

    /*--------------------------- _sys_seek -------------------------------------*/

    int _sys_seek (FILEHANDLE fh, long pos) { if (fh > 0x8000) { return (-1); } return (__setfpos (fh, pos));
    }

    /*--------------------------- _sys_ensure -----------------------------------*/

    int _sys_ensure (FILEHANDLE fh) { if (fh > 0x8000) { return (-1); } return (__flushbuf (fh));
    }

    /*--------------------------- _sys_flen -------------------------------------*/

    long _sys_flen (FILEHANDLE fh) { if (fh > 0x8000) { return (0); } return (__get_flen (fh));
    }

    /*--------------------------- _sys_tmpnam -----------------------------------*/

    int _sys_tmpnam (char *name, int sig, unsigned maxlen) { return (1);
    }

    /*--------------------------- _sys_command_string ---------------------------*/

    char *_sys_command_string (char *cmd, int len) { return (cmd);
    }

    /*--------------------------- _sys_exit -------------------------------------*/

    void _sys_exit (int return_code) { /* Endless loop. */ while (1);
    }

    Can you tell me what will happen step by step when following code is written on FLashFS program:

    printf ("\nFile closed.\n");

Children