rnunes
rnunes

Reputation: 2845

Create a FILE in C

I'm using MPLAB C32. I want to map my peripherals in streams, in order to do something like:

FILE* uart2 = fopen("UART2", 9600, param2, param3);

or just

FILE* uart2 = fopen("UART");

and then use fprintf to write on it:

fprintf(uart2, fmt, params);

What's the usual way of creating a FILE? Without having a filesystem, I just want to map peripherals on it.

Upvotes: 1

Views: 930

Answers (2)

Michael Burr
Michael Burr

Reputation: 340516

According to the MPLAB C Compiler User's Guide, Document DS51686B, (http://ww1.microchip.com/downloads/en/DeviceDoc/51686B.pdf), the library has support for wiring up devices to the stdio facilities. An as luck would have it, UART 2 is set to stdin/stdout/stderr by default, but only output will work since _mon_getc isn't implemented by default. If you define your own, that should enable input from UART 2 via stdin.

2.3 Standard I/O:

The standard input/output library functions support two modes of operation, Simple and Full. The Simple mode supports I/O via a two function interface on a single character device used for stdout, stdin and stderr. The Full mode supports the complete set of standard I/O functions. The library will use Full mode if the application calls fopen, otherwise Simple mode is used.

Simple mode performs I/O using four functions, _mon_puts, _mon_write, _mon_getc and _mon_putc, to perform the raw device I/O. The default implementation of _mon_getc always returns failure (i.e., by default, character input is not available). The default implementation of _mon_putc writes a character to UART2. It is assumed that the application has performed any necessary initialization of the UART. The default implementations of _mon_puts and _mon_write both simply call _mon_putc iteratively. All four functions are defined as weak functions, and so may be overridden by the user application if different functionality is desired. See the “32-Bit Language Tools Libraries” for detailed information on these functions.

If you need more control than that, a description of how to customize the runtime to 'connect' your devices to the stdio facilities of the compiler's runtime library is documented in the MPLAB C32 "32-Bit Language Tools Libraries" document DS51685 (http://ww1.microchip.com/downloads/en/DeviceDoc/MPLAB%20C32%20Libraries.pdf).

It looks like most of the functions required to support 'full mode' are documented in "2.18 MISCELLANEOUS FUNCTIONS"

Upvotes: 1

useraged
useraged

Reputation: 1716

You have to write drivers for each peripheral. Also you have to redefine your FILE* so you can have enough information to call appropriate driver. And also you need to redefine fopen and fprintf functions too. But this is pointless. I don't recommend this kind of practice. There's already built library PIC32 Peripheral Library is out. I recommend to use it.

Upvotes: 4

Related Questions