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

C Library I/O retargeting

Hello,

I'm having an issue with re-targeting the C library.

I'm using RL-Flash + RL-TCP + RTX.

I have (want) my printf etc (stdin, stdout stderr) erdirected to a UART.

While reading the documentation I saw that to re-target printf you must re-define fputc.

But when I do this it causes problems with fread/fwrite...

So if I only re-target the _sys_ functions everything seems to work BUT I can not use putchar, etc...

So I tried a combination similar to this:
my.st.com/.../Retarget.c

Still does not work.

How do correctly re-target the ARM C library such that:

  1. stdin, stdout and stderr are redirected to a UART
  2. RL-Flash (and RL-TCPnet with HTTP_uif.c) works as expected
  3. I can still use putchar, fputc, etc...

Also as a follow up can somebody explain:

#pragma import(_main_redirection)

Thanks.

M

Parents Reply Children
  • Maybe I'm missing something but I'm trying it right now and this is what I'm observing:

    printf - works.

    FS - works.

    putchar - I don't see that character on the terminal.

    If I add:

    int fputc(int ch, FILE *stream)
    {
        while (!(TERMINAL_UART_PORT->LSR & UART_LSR_THRE));
        UART_SendByte(TERMINAL_UART_PORT, (uint8_t)ch);
        return ch;
    }
    
    
    
    to the retarget.c file putchar works, printf works but the FS fails.
    
    Do you know what I'm missing?
    
    
    

  • What about flushing the sdio?
    Like this:

            putchar (0x08);                    /* echo backspace                 */
            putchar (' ');
            putchar (0x08);
            fflush (stdout);
    

  • Hi Franc,

    Thank you that seems to 'fix' putchar. (I should have thought of that)
    But I'm still having issues with getchar?

    I'm assuming this is because stdin and stdout are now being buffered?

    Can you explain the relation of the _sys_ functions and the C library (fputc, fgetc...)?

  • Yes, stdin and stdout are being buffered. If you retarget at fputc() layer, then the underlying _sys_ level is being bypassed. This is only acceptable for the stdio mode with no file system. If you want to use file system and stdio, you have to retarget at _sys_ layer.

  • Hi Franc,

    Thanks for your help and info.

    Do you know the library call order for fputc -> _sys_write?

    Also, I noticed in the SD_File example in the getline functtion they did not use getchar but rather getkey (a custom function)

    Is this because there is no way to have getchar return after a single character?

    Thanks again.

    M

  • Hi Franc,
    Can u please tell me how to redirect printf output directly to _sys_write rather than fputc. I am currently using both in my Retarget.c and if I try to remove fputs function then I get the following error:

    Library reports error: __use_no_semihosting_swi was requested, but a semihosting fputc was linked in