views:

216

answers:

4

Hi,

I am a newbie as far as it comes to embedded systems programming. I have to write lots of simple C or C++ programs like atoi, itoa, oct_to_dec, etc., which would have been easy to write in normal C.

But my hardware unit does not have the usual header functions and hence I cannot use standard library functions. :(

Could someone help me with some pointers to this? Any sampler or notes also would be of great help.

Thanks!

A: 

A simple possibility is to download something like GLibC code and then pick out the functions that you want. Just note that the code is GPL and will require you to distribute source code with the application.

Another possibility is to use the Android C library although there is a bit of ARM assembler for functions like memcpy. (Android has a more liberal licence)

Unfortunately anything that required a system call cannot be used on your system so there you are on your own.

doron
+4  A: 

Most embedded compilers do indeed have an implementation of at least a subset of the standard C libraries, including functions like itoa and atoi. Depending on the compiler and the type of microcontroller that you are using, you might not have to rewrite any of the functions.

Seeing that this is homework, however, that might be the point.

If you give more details on the MCU and compiler that you are using, as well as a specific problem that you are having, then I could edit my answer to make it more relevant to your needs.

General Pointers

Writing embedded code deals much more with the microcontroller's architecture than with library functions. You probably won't be using much in the way of printf or cout, but will be doing a lot of bit shifting and writing to registers. So brush up up your bitwise operators. That said, the most important thing you can do when learning to write embedded software is to master the architecture. I cannot stress this enough. You will spend a lot of time with your data sheet, so get a jump on your class and start reading the data sheet for your microcontroller.

Also, if this is your first embedded class, you shouldn't worry about strings, because you probably won't be using them at all. Most of your work will probably be centered around writing code to interface with hardware.

Kevin
+1  A: 

look at an ascii table, look for patterns. For instance, converting atoi consists of finding out how many characters are in a string, and then converting each character to it's digit equivalent, and then multiplying it by the correct power of 10

Alex Hart
Yeah but to read the characters, I generally use inbuilt header function. How will we read a string without using header functions
With math, one assumes.
Jonathan Grynspan
If you are only converting a character to it's decimal equivalent you can subtract the ascii value '0' from it.
Alex Hart
+2  A: 

Why would anybody in this day and age write atoi, itoa etc except as a learning exercise? Source code for these has been freely available for decades.

http://www.uclibc.org/ is a good place to go for an embedded systems c library - much smaller than glibc. It can also be used for closed source software (Lesser GPL licensed) and the source code is available. http://git.uclibc.org/uClibc/tree/

Dipstick