I have the UART I need selected. What I am trying to do is have two UART serial ports configured at once. One for diagnostics output, and one for communication with other modules that will be connected to the micro. I wanted to be able to use fopen and fread with both of those ports and was having difficulty with it. I have solved one of the problems but am still encountering others. I'll explain what I've done first.
I have the debug output configured to UART0 in board.h as you have said. I have then added these definitions to the "User settings" section of the MQX PEX component to start configuring my second communication channel:
#define BOARD_ALTERNATE_UART_INSTANCE 2
#define BOARD_ALTERNATE_UART_BASEADDR UART2_BASE
#define ALT_SERIAL_DEVICE "ttyc"
I've then edited the SDK->rtos->mqx->mqx->source->bsp->init_bsp.c file to initialise and configure my second UART alongside the first one. To do this I've added the following initialisation options:
const NIO_SERIAL_INIT_DATA_STRUCT nio_serial_alternate_init =
{
.SERIAL_INSTANCE = BOARD_ALTERNATE_UART_INSTANCE,
.BAUDRATE = 38400,
.PARITY_MODE = kNioSerialParityDisabled,
.STOPBIT_COUNT = kNioSerialOneStopBit,
.BITCOUNT_PERCHAR = 8,
.RXTX_PRIOR = 4,
.MODULE = NIO_SERIAL_DEF_MODULE,
/* Always second clock source configuration is used. It can be :kClockLpuartSrcPllFllSel, kClockLpsciSrcPllFllSel, kClockLpuartSrcIrc48M)*/
/* For UART this is dummy value */
.CLK_SOURCE = 1,
};
Then I've duplicated the BSP_DEFAULT_IO_CHANNEL initialisation:
/* Install serial driver for alternate input and output */
res = _nio_dev_install(BSP_ALTERNATE_IO_CHANNEL, &nio_serial_dev_fn, (void*) &nio_serial_alternate_init,NULL);
assert(NULL != res);
/* Instal and set tty driver */
res = _nio_dev_install("ttyc:", &nio_tty_dev_fn, (void*)&(NIO_TTY_INIT_DATA_STRUCT){BSP_ALTERNATE_IO_CHANNEL, 0},NULL);
assert(NULL != res);
// close(0);
fd = open("ttyc:", NIO_TTY_FLAGS_EOL_RN | NIO_TTY_FLAGS_ECHO); // 0 - stdin
assert(fd == 4);
// close(1);
fd = open("ttyc:", NIO_TTY_FLAGS_EOL_RN | NIO_TTY_FLAGS_ECHO); // 1 - stdout
assert(fd == 6);
// close(2);
fd = open("ttyc:", NIO_TTY_FLAGS_EOL_RN | NIO_TTY_FLAGS_ECHO); // 2 - stderr
assert(fd == 7);
By doing this I have been able to use fopen("ttyc",0) to open to second UART.
My current issue is how to use fread in a non blocking manner. As far as I can see fread is meant to return the number of characters read, but my program hangs at fread until any characters are received. How do I get it to return 0 if there is nothing at that moment, or read if there are bytes to be read?