/
How to use standard printf() - rcX V2.1
How to use standard printf() - rcX V2.1
This example shows how standard printf() can be included into rcX and used for teminal printouts.
The printf() consists of two files:
- rX_UartStdIOHook.c
The UART function implementation (initialization and libc integration) - rX_UartStdIOHook.h
Defines of the Hook functions
UART Hock InitializationFunction
/*****************************************************************************/ /*! rcX Function called to init the UART * \param szUart Name of the UART instance * \param szInterrupt Name of the UART's interrupt instance * \return RX_OK on success */ /*****************************************************************************/ RX_RESULT UartStdIOHookInit(const char* szUart, const char* szInterrupt) { RX_RESULT erXRes = RX_OK; if ( (RX_OK != (erXRes = rX_MemAllocateMemory(&s_hRecvSem, RX_SEMAPHORE_SIZE))) || (RX_OK != (erXRes = rX_SemCreateSemaphore(NULL, s_hRecvSem, 0))) ) { rX_MemFreeMemory(s_hRecvSem); } else { if( (RX_OK == (erXRes = Drv_UrtIdentifyUart(szUart, 0, &s_hUart))) && (RX_OK == (erXRes = Drv_UrtInitializeUart(s_hUart, UartRxReady, NULL, NULL, NULL, TRUE, &s_unUartHandler))) && (RX_OK == (erXRes = Drv_UrtEnableUart(s_hUart))) && (RX_OK == (erXRes = Drv_UrtEnableReceiver(s_hUart, TRUE))) ) { if(RX_OK == (erXRes = Drv_IntIdentifyInterrupt(szInterrupt, 0, &s_ptIntUart))) { Drv_IntInitializeInterrupt(s_ptIntUart, s_unUartHandler.fnIrq, s_hUart); Drv_IntEnableInterrupt(s_ptIntUart); } libc_file_init(); /* init the libc part */ Libc_RegisterStdOutFileHook(0, UartWrite, 0); Libc_RegisterStdOutFileHook(1, UartWrite, 0); Libc_RegisterStdInFileHook(UartRead, 0); } else { rX_SemDeleteSemaphore(s_hRecvSem); rX_MemFreeMemory(s_hRecvSem); } } return erXRes; }
The UART hook function must be called in the main task of the application (see below).
Example Task including UART initialization
/*****************************************************************************/ /*! Toolkit initialization function * \param argc * \param argv */ /*****************************************************************************/ void ToolkitSampleTask(void* pvParam) { TLR_HANDLE hReadyLed = NULL; TLR_HANDLE hRunLed = NULL; int32_t lTkRet = CIFX_NO_ERROR; RX_HANDLE hSerDPM = NULL; /* Signal task initialized */ rX_SysSetTaskInitialized(NULL); /* Redirect printf output to uart */ UartStdIOHookInit("REALUART", "UART"); ....... }
The complete example can be downloaded here:
Downloads