Subj : Re: Need to write putchar for embedded system To : comp.lang.c,comp.programming From : Jack Klein Date : Thu Jun 30 2005 10:45 pm On Thu, 30 Jun 2005 21:29:48 -0400, "Confused User" wrote in comp.lang.c: Neither of the groups that you posted this to is optimum, news:comp.arch.embedded would probably have been better. > I am working on device that utilizes a Motorola 68HC16 microcontroller. I am > using an old unsupported piece of crap Whitesmith's / Intermetrics / Tasking > compiler. The embedded compiler business was quite insestual for while > wasn't it? I need to write putchar so that printf can function properly. There are other compilers available if you don't like that one. > Anyway, the compiler comes with just a shell of a putchar routine. It > literally returns the character you passed to it and nothing else. That is > not why it's a piece of crap, although they could have at least supplied > something that writes to the serial port. Your assumption is that it should write to the serial port. Others might have other ideas. > So, I already have a serial port interrupt routing with circular buffers > (in/out) and a function call (SendData()) that copies a data from a pointer > argument and manages the circular buffer. So, I want to have my putchar > routine call SendData. > > I noticed from looking around that putchar returns EOF if unsuccessful. EOF > is defined as -1 in the compiler header file. Therefore, it occurs to me > that putchar can only be used for ASCII data. This is OK, I just need to > make sure I have that right. Because, binary data could include -1 right? > That means I should never use it alone to send binary data. If so, is this > true for the PC? After all, it's counterpart, getchar retrives binary scan > codes from the keyboard unless they have already been converted to ASCII > characters before they get to that level. No, you are completely wrong about putchar() being for ASCII data only. putchar() converts the value it receives to unsigned char, which cannot be negative, and it returns that unsigned char. The implementation is free to define any negative int value for EOF, although -1 is extremely common. Now the value of an unsigned char, any unsigned char, is not equivalent to the int -1. > I would guess the only time getchar should return EOF is if the transmit > buffer is full. My SendData() function already is set to return a fail code > if the buffer is full. putchar can pass this on to printf (of course I have > to change SendData() to use EOF as a fail code). > > As an embedded programmer, I am not real familiar with what normal pass/fail > return codes should be. The high level languages were developed around full > OS systems (Unix etc.) and then seem to have migrated to the embedded world > leaving some us without familiarity of conventions. > > Enough rambling.... Just looking for any constructive input. Again, the putchar() function is called with an int, but it only uses CHAR_BIT bits out of that int, and it treats them as an unsigned char. On your implementation, that means the value it deals with is in the range 0 to 255, inclusive. None of these values is the same as the int value -1. -- Jack Klein Home: http://JK-Technology.Com FAQs for comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html comp.lang.c++ http://www.parashift.com/c++-faq-lite/ alt.comp.lang.learn.c-c++ http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html .