lpcware

Are the Redlib versions of getchar() getc() and fgetc() casting EOF to a char by mistake?

Discussion created by lpcware Employee on Jun 15, 2016
Latest reply on Jun 15, 2016 by lpcware
Content originally posted in LPCWare by Appcon on Fri May 09 13:08:40 MST 2014
I am seeing unexpected values for EOF from Redlib "get" functions. I expect standard io "get" functions to return a 32-bit integer value.  I expect EOF to be the 32-bit twos complement representation for -1 (0xFFFFFFFF). My test app (below and attached) compares the return values of the standard lib "get" functions to the value returned by the low-level Board_UARTGetChar() function:


#include "stdio.h"
#include "string.h"
#include "chip.h"
#include "board.h"


static void UART_Init(void)
{
    /* Setup UART for 115200-8-N-1 */
    Chip_UART_Init(LPC_USART0);
    Chip_UART_SetBaud(LPC_USART0, 115200);
    Chip_UART_ConfigData(LPC_USART0, (UART_LCR_WLEN8 | UART_LCR_SBS_1BIT));
    Chip_UART_SetupFIFOS(LPC_USART0, (UART_FCR_FIFO_EN | UART_FCR_TRG_LEV2));
    Chip_UART_TXEnable(LPC_USART0);

    /* Reset USART FIFO and set trigger level 3 (14 chars) */
    Chip_UART_SetupFIFOS(LPC_USART0, (UART_FCR_FIFO_EN | UART_FCR_RX_RS | UART_FCR_TX_RS | UART_FCR_TRG_LEV3));
}


void EOF_Test(void)
{
    puts("\r\n-------- EOF Test --------\r");
    printf("                EOF = 0x%08X\r\n", EOF);
    printf("          getchar() = 0x%08X\r\n", getchar());
    printf("        getc(stdin) = 0x%08X\r\n", getc(stdin));
    printf("       fgetc(stdin) = 0x%08X\r\n", fgetc(stdin));
    printf("Board_UARTGetChar() = 0x%08X\r\n", Board_UARTGetChar());
}


int main(void)
{
    /* Init hardware drivers */
    SystemCoreClockUpdate();
    Board_Init();
    Board_UART_Init(LPC_USART0);
    Board_LED_Set(0, false);
    UART_Init();

    EOF_Test();

    /* DeInitialize UART0 peripheral */
    NVIC_DisableIRQ(USART0_IRQn);
    Chip_UART_DeInit(LPC_USART0);

    return 1;
}



The above code produced the following on UART0:



-------- EOF Test --------
                EOF = 0xFFFFFFFF
          getchar() = 0x000000FF
        getc(stdin) = 0x000000FF
       fgetc(stdin) = 0x000000FF
Board_UARTGetChar() = 0xFFFFFFFF



It appears the "get" functions cast EOF to a char. The low-level UART function acts as expected.

Can anyone confirm or deny this behavior? Is this as expected?

I am building for the NGX LPC1837 Evaluation Board with the following definitions:
_REDLIB_
_CODE_RED_
_USE_LPCOPEN_
CORE_M3


I am linking with the Redlib(nohost) option.

Any help or redirection is appreciated.

Thanks,
Sean

Original Attachment has been moved to: eof_3.zip

Outcomes