This is the multi-page printable view of this section. Click here to print...

Return to the regular view of this page

As of 2025-09-10

fprintf Library

fprintf library
    This is a simple implementation of fprintf.

    fprintf Library

    This is a simple implementation of fprintf.

    {{< hint color=“info” >}} This library is provided for source code compatibility. For new implementations, it is recommended to use TWENETmcu/printf. {{< /hint >}}

    Reference

    tsFILE

    A structure that defines the output destination specified by vfPrintf() and vPutChar().

    Members
    TypeNameDescription
    uint8u8DeviceSpecifies the serial port (E_AHI_UART_0 or E_AHI_UART_1).
    bool_t (*) (uint8 u8Device, uint8 u8Char)bPutCharA function pointer for output. For the SERIAL library, SERIAL_bTxChar() is prepared, so specify that.

    {% hint style=“info” %} SERIAL_bTxChar() puts the byte passed as u8Char into the FIFO queue within the SERIAL library.

    By preparing your own output function, you can use this for outputting strings to destinations other than UART. {% endhint %}

    Sample code
    #include "serial.h"
    #include "fprintf.h"
    
    tsFILE sSerStream;
    tsSerialPortSetup sSerPort;
    
    void vSerialInit(uint32 u32Baud, tsUartOpt *pUartOpt) {
    	// initialize sSerPort
    	...
    	SERIAL_vInit(&sSerPort);
    
    	// for vfPrintf()
    	sSerStream.bPutChar = SERIAL_bTxChar;
    	sSerStream.u8Device = E_AHI_UART_0;
    }
    
    void vSerOut() {
        vfPrintf(&sSerStream, "HELLO!");
    }

    Here is an example of the character LCD output code.

    #include "serial.h"
    #include "fprintf.h"
    
    tsFILE sLcdStream;
    
    // handle LCD display
    PUBLIC bool_t LCD_bTxChar(uint8 u8Device, uint8 u8Data) {
    	int i;
    
    	switch (u8Data) {
    	case '\n':
    	...
    }
    
    void vInitHardware() {
        /* Initialise the LCD */
        vLcdReset(3, 0);
    
        /* register for vfPrintf() */
        sLcdStream.bPutChar = LCD_bTxChar;
        sLcdStream.u8Device = 0xFF;
    }
    
    void vSomeOutput() {
        vfPrintf(&sLcdStream, "Hello World!\n");
    }

    vfPrintf()

    Explanation

    This function outputs to the destination specified by the tsFILE structure (UART) using printf format.

    Arguments

    TypeNameDescription
    tsFILE*psStreamOutput destination
    const char *pcFormatOutput format
    Variable arguments
    Supported Formats
    sString
    dInteger (up to 32 bits)
    uUnsigned integer (up to 32 bits)
    xHexadecimal. a-f are lowercase.
    XHexadecimal. A-F are uppercase.
    bBit sequence

    Return Value

    None.

    Sample

    void cbToCoNet_vMain(void) {
    	while (!SERIAL_bRxQueueEmpty(sSerPort.u8SerialPort)) {
    		int16 i16Char;
    		i16Char = SERIAL_i16RxChar(sSerPort.u8SerialPort);
    		vfPrintf(&sSerStream, "\n\r## [%c] --> ", i16Char);
    	    SERIAL_vFlush(sSerStream.u8Device);
    		...
    	}
    }

    vPutChar()

    Explanation

    This function outputs one byte to the destination specified by the tsFILE structure (UART).

    Arguments

    TypeNameDescription
    tsFILE*psStreamOutput destination
    uint8u8CharOutput byte

    Return Value

    None

    Sample

    #define IS_ASC(c) ((c) >= 0x20 && (c) <= 0x7e)
    
    void cbToCoNet_vRxEvent(tsRxDataApp *pRx) {
    	uint8 u8i;
    	vfPrintf(&sSerStream, LB"RX(len=%d):[", pRx->u8Len);
    	for (i = 0; i < pRx->u8Len; i++) {
    		uint8 c = pRx->auData[i];
    		vPutChar(&sSerStream, IS_ASC(c) ? c : '.');
    	}
    }