stdio integration

I’m using GCC/Eclipse for the LPC2148, and I’d like to integrate my own output devices so I can treat them like a standard stdio item. I want to be able to do something like:

fHnd = fopen("I2C_LCD","+r");
fputc('x',fHnd);

And have that call my I/O open function, and my function for sending a character.

I already have all of my own IO functions written for UART support on my development board, (and I hope to make LCD over I2C soon). I just want to use the standard C way of getting data to/from them.

I considered ‘wrapping’ everything and using ‘xfopen’ to forward commands to ‘myfopen’ or ‘fopen’ based on the input, but that is crufty. Can anyone suggest a good way to do this, or point me in the right direction to someplace on how I could do this?

Can I even do this without recompiling the gcc libraries?

This (stdio) is a linker-level thing. One way to do it is to use newlib and provide a device table with your low-level IO functions (stdin, stdout, stderr, etc). I found this guy’s newlib-based tarball really helpful as an example:

http://www.aeolusdevelopment.com/Files/ … rel_5a.zip

Excellent! I looked into newlib, and it looks like it’s exactly what I need. Thanks for the help.

You can also use funopen() or fopencookie(), which aren’t completely std parts of stdio, but is reasonably common and sounds like it does what you want. (glibc and uclibc both have fopencookie().)

Hi wiml. Can you please point me to an example of using fopencookie(), it’s really hard to find any examples of how to set up cookie IO out there. Thanks!

Hmm, I’ve only used funopen(), not fopencookie(), but they look basically similar. I tried [Google’s nifty code search and mostly found glibc itself, but here’s a [trivial example of fopencookie() in use.

The cookie itself is optional; it’s just a pointer that gets passed aong to your read/write/seek/close functions so that they know what “file” they’re supposed to be acting on. If you only have one “file” (eg your I2C port) you can just use NULL or whatever.](http://www.google.com/codesearch?q=+fopencookie+show:nASlu9pyvu0:yXQahA56Hn0:XnXITpYW5RQ&sa=N&cd=20&ct=rc&cs_p=http://gentoo.osuosl.org/distfiles/glibc-2.4.tar.bz2&cs_f=glibc-2.4/stdio-common/tst-cookie.c#a0)](http://www.google.com/codesearch)