473,408 Members | 1,821 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Need to write putchar for embedded system

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.

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.

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.

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.

Elvis
Nov 15 '05 #1
5 4374
>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. 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.
Well, that assumes that everyone uses the serial port and not, for
example, a keypad and 8-character LCD display, which I suppose
someone could attach to this thing if, for example, it's going to
run a microwave oven.
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?
putchar() can return -1 for EOF and (unsigned char)c for a character.
Assuming that an int (must be at least 16 bits) has more bits than
a char (must be at least 8 bits, but could be 16 or 32), there is
no ambiguity here. Oh, yes, hardly anyone bothers to look at the
return value of putchar() anyway.
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.
I don't know of any implementation in which getchar() returns scan
codes unless you do something system-dependent, non-default, and
obnoxious to cause that. Scan codes aren't characters. You need
to combine the scan code(s) with shift states to get actual characters.
I would guess the only time getchar should return EOF is if the transmit
buffer is full.
No. getchar() should return EOF if the implementation-defined
end-of-file character is typed, if there even is such a character.
Or perhaps if DCD drops. Depending on what you are using it for,
a version of getchar() which *NEVER* returns EOF could be fine.

putchar() should return EOF, well, almost never. Maybe if CTS
drops. It should NOT return EOF because the program is sending
data faster than you can get it out the serial port. In that
situation it should WAIT. This could get complicated in a multi-tasking
system where an interrupt that there is space in the buffer causes
a thread/process/whatever to wake up. In a uni-tasking situation
it's easy: busy wait for the buffer to become free, then send the
character.
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).
No, you don't have to change SendData() to use EOF as a buffer-full code.
In putchar():
if (SendData(foo) == ERROR_BUFFER_FULL) return EOF;

Buffer full is not an error condition. Assuming you've got a
processor that's less slow than pitiful for 20 years ago (e.g.
100*K*Hz), any attempts to send a line of data bigger than the
buffer size (16 characters?) is going to fill the buffer almost
immediately.
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.


ANSI/ISO C does not have non-blocking I/O. That means you can't, using
standard routines, wait to output something while scanning for input
on something else. You might end up needing non-standard non-blocking
routines for the serial port, depending on what you need to do.
(SendData() might serve this purpose well as-is).

It also means that getchar() and putchar() need to be written as
blocking.

Gordon L. Burditt
Nov 15 '05 #2
On Thu, 30 Jun 2005 21:29:48 -0400, "Confused User"
<el***********@bellsouth.net> 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/~a...FAQ-acllc.html
Nov 15 '05 #3
Gordon & Jack,
A lot of helpful input. Thanks a lot!
I do want to elaborate on a few points and ask further questions, however,
it is 8:30am and I have to get to work. This info will help me today.

I will be back later for some further discussion on this topic.

Thanks again,

Elvis
-------------------------------------------------------------------
"Confused User" <el***********@bellsouth.net> wrote in message
news:X3****************@bignews1.bellsouth.net...
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.

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.

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.

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.

Elvis

Nov 15 '05 #4
Jack Klein wrote:
putchar() converts the value it receives to unsigned char,
which cannot be negative, and it returns that unsigned char.


putchar(-1) wouldn't return that unsigned char,
if sizeof(int) was equal to 1.

If putchar(-1) doesn't return EOF,
then it returns ((int)(unsigned char)-1).

--
pete
Nov 15 '05 #5
Confused User wrote:
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?
The C standard allows for embedded systems (aka. freestanding
implementations), and they aren't required to implement
any of the I/O functions in the C library.
I need to write putchar so that printf can function properly.
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.
68HC16s don't know anything about serial ports, so why would
they bother?

You should write your data to a buffer with sprintf or some other
method, and then send that buffer to the serial port, using a
function specifically designed for writing to serial ports.
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.
getchar and putchar work with unsigned values (ie. the range 0-255,
if you have 8-bit chars). You should cast signed chars to unsigned
before passing them to 'putchar', eg:

char *p, *str = "héllo";
for (p = str; *p; ++p)
putchar( (unsigned char) *p );

As an embedded programmer, I am not real familiar with what normal pass/fail
return codes should be.
Use your serial port functions, rather than trying to muck around
with someone else's standard library. Who knows, the next version of
your compiler might change something internally that will render
your customised putchar useless.
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.


Embedded compilers try to comply with the C standard for
freestanding implementations. Read it, if you have a few spare hours.

Nov 15 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Pflanzen Gold | last post by:
Hi, When entering a password we don't really see the characters - we see **** instead. How can I write such a routine in C? How can I 'echo off' the inputs? How can I get a characters/strings...
3
by: franco ziade | last post by:
I am stuck :( I am trying to pass the address of a function as an argument to a function (some OS call to launch a task): Example: -------- Declaration of the OS "lunch a Task" LunchTask(...,...
2
by: Jani Mantytorma | last post by:
I have embedded resource called Settings.xml. I'm able to read the resource but how can I write data to the resource. The code for read operation follows: private XmlDocument m_doc = new...
4
by: Phil | last post by:
k, here is my issue.. I have BLOB data in SQL that needs to be grabbed and made into a TIF file and placed on the client (could be in temp internet dir). The reason we need it in TIF format is...
9
by: kfeder | last post by:
Im new to C programming i wrote a simple program that displays A - F. Im having a problem displaying F to A. could someone help me . include <stdio.h> #include <stdlib.h> #include <string.h>...
14
by: Odinn | last post by:
Greetings , This is my first year at programming and 3rd week at this so count me as a pure newbie. I am trying to make a program that gets a number and prints out starts as this: For input 3 :...
9
by: anon.asdf | last post by:
In terms of efficieny: Is it better to use multiple putchar()'s after one another as one gets to new char's OR is it better to collect the characters to a char-array first, and then use...
63
by: Bill Cunningham | last post by:
I don't think I can do this without some help or hints. Here is the code I have. #include <stdio.h> #include <stdlib.h> double input(double input) { int count=0,div=0; double...
24
by: kevin | last post by:
write a program in c.
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.