Author Topic: Embedded C Book Recommendation  (Read 20064 times)

0 Members and 1 Guest are viewing this topic.

Offline StiegeTopic starter

  • Regular Contributor
  • *
  • Posts: 72
  • Country: nz
Embedded C Book Recommendation
« on: February 11, 2013, 12:48:00 am »
Hey all,

I would class myself as an intermediate C programmer. I understand the basics well, however I want to have a more in depth understanding. Especially with regard to things like function pointers and using structs to control registers.

I've ordered this book:

http://www.amazon.com/gp/product/1418039594/ref=oh_details_o00_s00_i00

However I'm not sure it will hit the mark by itself.

Does anyone have any suggested reading?

Cheers
 

Offline smashedProton

  • Frequent Contributor
  • **
  • Posts: 641
  • Country: us
Re: Embedded C Book Recommendation
« Reply #1 on: February 11, 2013, 01:07:07 am »
I can't help except by linking you to arduino.cc/reference   :-[
http://www.garrettbaldwin.com/

Invention, my dear friends, is 93% perspiration, 6% electricity, 4% evaporation, and 2% butterscotch ripple.
 

Online IanB

  • Super Contributor
  • ***
  • Posts: 12477
  • Country: us
Re: Embedded C Book Recommendation
« Reply #2 on: February 11, 2013, 01:07:55 am »
Holy guacamole! That's an expensive book! I don't see how it's possible to justify $150 for a textbook when the Internet is full of free information these days...  :o

I would pay $150 for a book if it contains pure, timeless information, distilled and organized by an expert. There are some of those books around and I like to have them on my bookshelf. But for a tutorial guide on programming in C? It doesn't seem like good value.

I'm not sure how to advise you. I'm a strong believer in learning basic theory supported by seeing how it is applied in practical cases.

So for the C language, I would always recommend K&R. If you work through the book chapter by chapter and do all the exercises you will know as much about C as you need to know. It's not a big language (compare to Stroustrup's book on C++ which is many times bigger and yet is still no bigger than it needs to be).

Added to the C language, you need to learn about hardware. If you want to learn about AVR, read specific technical information on that. Learn how to understand the free technical references and datasheets for those processors, and use online tutorials to help when you get stuck.
 

Offline StiegeTopic starter

  • Regular Contributor
  • *
  • Posts: 72
  • Country: nz
Re: Embedded C Book Recommendation
« Reply #3 on: February 11, 2013, 01:13:43 am »
Holy guacamole! That's an expensive book! I don't see how it's possible to justify $150 for a textbook when the Internet is full of free information these days...  :o

I would pay $150 for a book if it contains pure, timeless information, distilled and organized by an expert. There are some of those books around and I like to have them on my bookshelf. But for a tutorial guide on programming in C? It doesn't seem like good value.

I'm not sure how to advise you. I'm a strong believer in learning basic theory supported by seeing how it is applied in practical cases.

So for the C language, I would always recommend K&R. If you work through the book chapter by chapter and do all the exercises you will know as much about C as you need to know. It's not a big language (compare to Stroustrup's book on C++ which is many times bigger and yet is still no bigger than it needs to be).

Added to the C language, you need to learn about hardware. If you want to learn about AVR, read specific technical information on that. Learn how to understand the free technical references and datasheets for those processors, and use online tutorials to help when you get stuck.

Cheers for that. I actually already understand the hardware. I've done quite a bit of C programming, things like RS-232 communication. Wrote an LCD display driver. Even a PID controller for a motor after I extracted the step response and did an optimisation in Vissim and converted the control function from continuous to the discrete domain. So I'm pretty experienced, but I want to improve further. The sad part is that whenever I buy books now it's usually for only 2-3 chapters. I own a lot of books too.

So, any further guidance on where I can read about function pointers and structs in embedded design as well as other more advanced techniques?

And of course, I bought the book used.
« Last Edit: February 11, 2013, 01:20:11 am by Stiege »
 

Offline notsob

  • Frequent Contributor
  • **
  • Posts: 706
  • Country: au
Re: Embedded C Book Recommendation
« Reply #4 on: February 11, 2013, 01:28:14 am »
I would have bought that book 2nd hand thru goodwillexpress - they act as a front end for a lot of bookshops

$52    Embedded C Programming and the Atmel Avr

http://www.goodwillexpress.com
 

Online IanB

  • Super Contributor
  • ***
  • Posts: 12477
  • Country: us
Re: Embedded C Book Recommendation
« Reply #5 on: February 11, 2013, 01:34:14 am »
So, any further guidance on where I can read about function pointers and structs in embedded design as well as other more advanced techniques?

Sorry, I'll have to let someone else chime in. Learning how to program AVR is on my to-do list (I have AdaFruit's Ice Tube clock and I need to hack the firmware to improve the time keeping), but I have not got around to it yet.

Function pointers are just functions you store in a variable. Like for instance "3" is a constant, but if you store the value 3 in a variable k then you can access 3 by referring to k. Likewise, the function int f(int x) has the constant name "f", but if you store the function in a variable fp then you can access the function through the variable fp. Function pointers can be useful for things like dispatch tables where you have an array of function pointers and you branch to the desired function by indexing into the array.

Structs are just a way of assigning names to locations in a region of memory offset from some base address. You can view a struct a bit like a sliding window that you move around the memory address space. The struct then allows you to access the memory locations under the window by name. Structs are especially useful like this when based off a pointer, so you have syntax like this: p->x, which means the structure member "x" that is accessed when the struct is positioned at location p in the memory map.

(By the way, K&R covers function pointers and structs in detail as these are essential elements of the C language.)
« Last Edit: February 11, 2013, 01:37:18 am by IanB »
 

Offline StiegeTopic starter

  • Regular Contributor
  • *
  • Posts: 72
  • Country: nz
Re: Embedded C Book Recommendation
« Reply #6 on: February 11, 2013, 02:03:37 am »
Yeah, I understand what you've said, I just want it as it specifically relates to embedded programming, and in quite a bit of detail. As in, source code and examples to try. I get confused at the moment when I see multiple levels of de-referencing.

Reading the internet is great, but getting the information from different places can be a bit confusing and it's easy to miss stuff. I like to back myself up by reading some more formal material from time to time.
 

Offline AlfBaz

  • Super Contributor
  • ***
  • Posts: 2187
  • Country: au
Re: Embedded C Book Recommendation
« Reply #7 on: February 11, 2013, 03:16:59 am »
I think the biggest pitfalls of C in an embedded scenario are ISO divergences for the compiler you are using.

The point to take from the above is once you've learnt what you need for C and you go to create your firmware on a paticular platform you need to go through the compiler documentation to see what it can and can't implement, any extra pragma's the compiler might have and (gcc in particular) any weird non-C type statements that are used to take advantage (or work around) hardware quirks for the controller you are using.

These sorts of problems become more convoluted if you wish to keep the code portable between platforms.

Sometimes its may be benificial to write more code and therefore use more resources, than to use things like function pointers. For example you may loose certain optimisations that can be generated by the compiler because with function pointers the compiler has no way of nowing when or if it will ever be called. Worse still if you have a crap compiler (there are plenty) it may not give a toss (and not even generate a warning) optimise the function and give you an intermitant problem that will be very hard to debug.

My favorite quote (and I beleive its from K&R)
Everyone knows its twice as hard to debug than it is to code, so if you are as clever as you can be when you code, how will you ever degug it?
 

Offline TerminalJack505

  • Super Contributor
  • ***
  • Posts: 1310
  • Country: 00
Re: Embedded C Book Recommendation
« Reply #8 on: February 11, 2013, 03:41:06 am »
Hey all,

I would class myself as an intermediate C programmer. I understand the basics well, however I want to have a more in depth understanding. Especially with regard to things like function pointers and using structs to control registers.

I've ordered this book:

http://www.amazon.com/gp/product/1418039594/ref=oh_details_o00_s00_i00

However I'm not sure it will hit the mark by itself.

Does anyone have any suggested reading?

Cheers

If you're programming for the AVR microcontrollers then I suggest you read the latest AVR libc documentation.  You can download it here, or, if you've installed Atmel's development software then you'll find it somewhere installed on your system.
 

Online amyk

  • Super Contributor
  • ***
  • Posts: 8492
Re: Embedded C Book Recommendation
« Reply #9 on: February 11, 2013, 11:55:09 am »
This is about C in general but still very useful book for the more advanced parts:
http://www.amazon.com/Expert-Programming-Peter-van-Linden/dp/0131774298

As AlfBaz pointed out above the nonstandard pieces are the biggest difference for C in embedded systems.
 

Offline caroper

  • Regular Contributor
  • *
  • Posts: 193
  • Country: za
    • Take your PIC
Re: Embedded C Book Recommendation
« Reply #10 on: February 11, 2013, 03:40:31 pm »
I can recommend the Smiley's Workshop series of articles By Joe Pardue from Nuts & Volts Magazine beginning in  August 2008.
Unfortunately his web site is undergoing changes and the old links to the articles have not yet been ported across, but if you do get a chance to look at them it is a very well written set of step by step guides that start with no required knowledge of C or Microcontrolers and is still going strong with the current issue covering the "time.c" standard library.

Even as an an accomplished developer for PIC and AVR chips I find his style entertaining, stimulating and often containing hints and tips that improve my own tool kit.

Cheers
Chris


p.s.
I forgot to mention he also has a book on the subject: https://store-jwm4nhbo.mybigcommerce.com/c-programming-microcontrollers-book-and-projects-kit/
Another popular author from Nuts and Volts has 3 books on the subject (Vols 1-3): https://store.nutsvolts.com/product.php?productid=16727&cat=328&page=2
« Last Edit: February 11, 2013, 04:51:41 pm by caroper »
 

Offline Kremmen

  • Super Contributor
  • ***
  • Posts: 1289
  • Country: fi
Re: Embedded C Book Recommendation
« Reply #11 on: February 11, 2013, 06:04:30 pm »
Yeah, I understand what you've said, I just want it as it specifically relates to embedded programming, and in quite a bit of detail. As in, source code and examples to try. I get confused at the moment when I see multiple levels of de-referencing.

Reading the internet is great, but getting the information from different places can be a bit confusing and it's easy to miss stuff. I like to back myself up by reading some more formal material from time to time.
Maybe it is just me but i find myself gravitating more and more towards C++ in my embedded projects. I do Atmel processors mostly, and in my experience already the Mega328 (the same that Arduino uses)  runs C++ code with no problems whatever. Some say C++ is too heavy and involved for embedded applications but that is not my experience at all. The AVR gcc supports all common C++ OO constructs (i haven't really explored the limits) like classes, inheritance, visibility and access rules, scoping, polymorphism etc. Dynamic class instantiation works as expected with minimum effort (you have to tell the compiler how to implement 'new' and 'delete'). It may be a feature that should be used with care in an app requiring deterministic timing and behavior, but i have used it e.g. to create linked lists of actions in user programmable sequences. One such was a camera dolly controller. The movements, camera control actions and timings were all child classes inherited from a generic base action. The actions were stored in a linked list, likewise a hierarchy of classes, with persistence to the EEPROM even. All of that would have been doable with plain C but so much easier and more elegant in C++. By the way it was done with a plain vanilla Arduino, with no problems in performance or memory size.

So, any further guidance on where I can read about function pointers and structs in embedded design as well as other more advanced techniques?

Move over to C++ and you don't need no steenking function pointers. The structs come free as every class is a struct and every struct is a class. All the "advanced" stuff is C++ anyway.
Nothing sings like a kilovolt.
Dr W. Bishop
 

Offline StiegeTopic starter

  • Regular Contributor
  • *
  • Posts: 72
  • Country: nz
Re: Embedded C Book Recommendation
« Reply #12 on: February 11, 2013, 07:28:49 pm »
Thanks for that Kremmen. I haven't done a project in C++ yet, but you might have just talked me into giving it a go.
 

Offline firewalker

  • Super Contributor
  • ***
  • Posts: 2452
  • Country: gr
Re: Embedded C Book Recommendation
« Reply #13 on: February 11, 2013, 07:38:36 pm »
I had a pik at he first edition of

"Embedded C Programming and the Atmel AVR
Richard H. Barnett, Sarah Cox, Larry O'Cull"

If I can recall I didn't like the fact that it was heavily dependent on CodeVision AVR compiller/IDE.

Alexander.
Become a realist, stay a dreamer.

 

Offline StiegeTopic starter

  • Regular Contributor
  • *
  • Posts: 72
  • Country: nz
Re: Embedded C Book Recommendation
« Reply #14 on: February 11, 2013, 07:40:30 pm »
p.s.
I forgot to mention he also has a book on the subject: https://store-jwm4nhbo.mybigcommerce.com/c-programming-microcontrollers-book-and-projects-kit/
Another popular author from Nuts and Volts has 3 books on the subject (Vols 1-3): https://store.nutsvolts.com/product.php?productid=16727&cat=328&page=2

Thanks for that, should just about do it for books!
 

Offline westfw

  • Super Contributor
  • ***
  • Posts: 4335
  • Country: us
Re: Embedded C Book Recommendation
« Reply #15 on: February 11, 2013, 10:52:13 pm »
Quote
any further guidance on where I can read about function pointers and structs in embedded design
Do you understand any assembly languages, or microcontroller architectures at the hardware level?
I've found it very useful to understanding C (which some people have called "a high level assembler"), to be able to imagine exactly how it compiles onto the (or "a", anyway) target device.

For example, a function pointer uses the indirect call mechanism; put the address of a function in a register and then do "icall" or the equivalent via that register.  The advantage is that you can load that register from all sorts of useful places, like tables (character dispatch table for an editor, say) or a structure (byte write and read functions associated with a higher level 'device' structure.)

Structures mapped onto io registers are particularly useful (IMO) when you have duplicated functions in the chip.  For instance, on an AVR you might have up to four uarts, all of which look like:
Code: [Select]
typedef struct uart_t_ {
    volatile uint8_t ucsra;  //status A
    volatile uint8_t ucsrb;  //status B
    volatile uint8_t ucsrc;  //status C
    uint8_t u_res1;  // reserved (not used)
    uint8_t ubrrl;    //bitrate low
    uint8_t ubrrh;   //bitrate high
    volatile uint8_t data;  // rx/tx data.
} uart_t;
So instead of writing code for each uart, you can stick a pointer into a variable (at some risk of being ugly and bug-attracting):
Code: [Select]
uart_senddata('a', (uart_t *) (&UCSR0A));
At one point I had re-written the Arduino serial code along these lines.  It got a lot smaller, and prettier.

Alas, this does not seem to be the way that chip vendors like to describe their peripherals.  There are some dangers; the C language definition does not tightly control how a compiler should lay out a structure.  Some compilers require (non-standard) additions to prevent them from adding padding, or reordering "things" (endianness, for one.)  Also, at the bit level, bitfields are not strongly defined.  So code written with structures may not be very portable...
 

Offline StiegeTopic starter

  • Regular Contributor
  • *
  • Posts: 72
  • Country: nz
Re: Embedded C Book Recommendation
« Reply #16 on: February 12, 2013, 12:20:11 am »
Yeah, I know assembly, had to code a multi tasking kernel in second year.

Pretty much what you've written I understand and have looked at some stuff online on how to do that, what I'm after is a book with all those cool tricks in one place. Why don't you start writing?!

I think I was concerned about the same things you've mentioned. Thats is, if I record the struct's bits in a certain order, is it for sure that the struct will be arranged as I've listed it (and therefore map onto the hardware properly)? I think you've answered that as "not always", and there can be extra stuff too. Interesting.

 

Offline Harvs

  • Super Contributor
  • ***
  • Posts: 1204
  • Country: au
Re: Embedded C Book Recommendation
« Reply #17 on: February 12, 2013, 09:24:49 pm »
Possibly what you're after is a book of embedded C design Patterns as apposed to a language reference.

I've been looking for a good one for some time, and I've picked up very valuable snippets from various embedded C programming books (such as Test Driven Development for Embedded C which has some good pointers about coding in an OO manner in C.)  But I'm yet to find a very good one, it's quite frustrating really.

http://www.amazon.com/Design-Patterns-Embedded-Systems-Engineering/dp/1856177076 This one looked like it was going to be good, but it's full of poor editing and code errors.  Which isn't much good for a programming book!
 

Offline Kremmen

  • Super Contributor
  • ***
  • Posts: 1289
  • Country: fi
Re: Embedded C Book Recommendation
« Reply #18 on: February 12, 2013, 09:41:56 pm »
Maybe a stupid question but referring to my earlier comment, why would you want to just simulate OO in C, when you can do it properly using C++? Especially as the compiler is readily available, at least for Atmel chips. It is not that hard once you get over the initial threshold.
Nothing sings like a kilovolt.
Dr W. Bishop
 

Offline Harvs

  • Super Contributor
  • ***
  • Posts: 1204
  • Country: au
Re: Embedded C Book Recommendation
« Reply #19 on: February 12, 2013, 10:26:55 pm »
Maybe a stupid question but referring to my earlier comment, why would you want to just simulate OO in C, when you can do it properly using C++? Especially as the compiler is readily available, at least for Atmel chips. It is not that hard once you get over the initial threshold.


I can't say that I fully understand the resource implications of using C++ vs C, and unfortunately I'm not sure that a lot of people that discuss this on forums really know either.


But... traditionally the manufacturer IDEs didn't support C++ very well at all, so it became a real headache to use them with C++ (like if memory serves me correctly Atmels IDE didn't even recognize files with cpp extension as being code, but it's been quite a while since I've used it).  Some manufacturers like Microchip didn't have a C++ compiler until recent (and I believe even now it's under consideration as to how much effort they'll put into supporting it in future, according on one of their employees on their forums.)  So for relatively small projects (like a lot of uC projects are), it's simply much quicker to put it together in C than deal with wrapping all the libraries in extern C statements and dealing with poor IDE support.
 

Online IanB

  • Super Contributor
  • ***
  • Posts: 12477
  • Country: us
Re: Embedded C Book Recommendation
« Reply #20 on: February 12, 2013, 10:35:44 pm »
Maybe a stupid question but referring to my earlier comment, why would you want to just simulate OO in C, when you can do it properly using C++? Especially as the compiler is readily available, at least for Atmel chips. It is not that hard once you get over the initial threshold.

I think one possible reason is that C is almost entirely transparent as a language. There is essentially no code that is automatically generated by the compiler, so you have very precise control over the code that is generated. What you see is what you get.

With C++ it is somewhat different. Now the compiler may generate code automatically (default constructors, default destructors, operator overloading, etc.) and now you need deep knowledge of what the compiler may do behind the scenes. If you trust the compiler and know what to expect, all is fine, but there are more pitfalls in C++ for the unwary.
 

Offline StiegeTopic starter

  • Regular Contributor
  • *
  • Posts: 72
  • Country: nz
Re: Embedded C Book Recommendation
« Reply #21 on: February 13, 2013, 04:32:58 am »
Possibly what you're after is a book of embedded C design Patterns as apposed to a language reference.

I've been looking for a good one for some time, and I've picked up very valuable snippets from various embedded C programming books (such as Test Driven Development for Embedded C which has some good pointers about coding in an OO manner in C.)  But I'm yet to find a very good one, it's quite frustrating really.

http://www.amazon.com/Design-Patterns-Embedded-Systems-Engineering/dp/1856177076 This one looked like it was going to be good, but it's full of poor editing and code errors.  Which isn't much good for a programming book!

I think you're exactly right. I am after a book of design patterns. Thanks heaps!
 

Offline StiegeTopic starter

  • Regular Contributor
  • *
  • Posts: 72
  • Country: nz
Re: Embedded C Book Recommendation
« Reply #22 on: February 13, 2013, 04:39:47 am »
Maybe a stupid question but referring to my earlier comment, why would you want to just simulate OO in C, when you can do it properly using C++? Especially as the compiler is readily available, at least for Atmel chips. It is not that hard once you get over the initial threshold.

I think one possible reason is that C is almost entirely transparent as a language. There is essentially no code that is automatically generated by the compiler, so you have very precise control over the code that is generated. What you see is what you get.

With C++ it is somewhat different. Now the compiler may generate code automatically (default constructors, default destructors, operator overloading, etc.) and now you need deep knowledge of what the compiler may do behind the scenes. If you trust the compiler and know what to expect, all is fine, but there are more pitfalls in C++ for the unwary.

Yeah I think this is probably my attraction to it. To me it's as low level as you can be while still having a relative degree of portability. I think learning about how to code in C to be like using C++ will also be beneficial for me to really understand what's going on behind the scenes. As I'm an EE and not a software guru, I pretty much see that as my job, even if the software guru's can do a lot more complicated things with less code using C++.
 

Offline Otatiaro

  • Regular Contributor
  • *
  • Posts: 85
Re: Embedded C Book Recommendation
« Reply #23 on: February 14, 2013, 07:18:04 pm »
Hello,

Yeah, I know assembly, had to code a multi tasking kernel in second year.

You coded a multi tasking kernel without using function pointers ?
I coded one this week and, well, I don't know how it's possible without function pointers.

Anyway ... function pointers are just that ... a function has a body, which is somewhere is memory, and the function pointer is the address of the entry point of the function's body.

You define the type of the pointer (with parameters types and return type) so that the compiler knows how to call it and get back the return value (this process depends on the processor ... typically first few parameters are in registers, then they are placed on the stack - this is why you don't want to have more than 3 parameters for a function, otherwise use a structure and pass structure's pointer - , and returned value is in register).

Structures are kind of arrays but with different elements in it (and the compiler calculates the offset for you).

It's nice to have a good knowledge of the platform you are working on, I see C (and even more C++) as a way to write assembler more efficiently, but I don't want the compiler to take decisions on his own, I want to drive it to get the result I want.

Plus you'll need good knowledge of all this to debug efficiently.

I'm now mainly working on ARM Cortex, it is a modern architecture and there is a lot to learn from how it works.

Thomas.
 

Offline StiegeTopic starter

  • Regular Contributor
  • *
  • Posts: 72
  • Country: nz
Re: Embedded C Book Recommendation
« Reply #24 on: February 14, 2013, 07:38:00 pm »
I coded it in assembly, there was a semester worth of notes to help me do it.

It would take me quite some time to figure out how to do it in C, it's something I'd like to know more about though.

I understand quite well what a structure, and what a function pointer is. I was after a book that talked about how to use them effectively, along with other more advance techniques.
« Last Edit: February 14, 2013, 07:43:15 pm by Stiege »
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf