I've written a few 8bit embedded systems and the codebase I inherited and have expanded is basically 80% global variables (extern volatile), and than non-global control flags and logic variables as needed.
The end result, is that you end up with a lot void() functions to modify the global variables.
The systems run fine and the software quite readable and easy to work on, but I always have that design nag in the back of my head that I should be refactoring everything and pointer'ize the next design.
I don't have any religiosity about the memory usage aspect, the static memory is there to use as much as the heap is. It's more a question of as systems get larger, I suspect maybe the globals start to be more of a hinderance than you think? I've never gotten there in program size.
Do many of you experienced embedded software programmers use pointers extensively in your 8bits systems?
I can think of three possible use cases for using pointers in C51: linked lists (which we don't use) in this system, structs in place of independent variables and then functions with pointers to modify the strucs, maybe a double pointer if you absolutely need to modify a pointer but I can't fathom a situation in building a consumer product with an 8051 (maybe building an OS...).
I get that the functions can get re-used if you pointer'ize your code, but in one sense the functions are pretty trivial and one off in the embedded systems I've built. Very application specific stuff.
A couple of you guys who are still around C51 forum, what is your most compelling (or not compelling) use case for pointers in the code you have released?
@Erik -- With my pointer kungfu studies, I am really starting to get the static/stack/heap memory divide. With more knowledge, comes more hand-wringing....
Do you take the time to really nitpick your global usage to prevent scope creep? I gotta be honest, if it costs $100/hr for design work, seems pretty low value to have a guy just nailing down the scope of every variable to a T.
I get that a static declared variable, lives in the static memory, so for all purposes it is a "global" but it has some extra protections.
Like you could really spend some time and change globals as static variables, and really drill down and try to protect variable scope... As a one man band, the probability that I am going to invardently jank up some mission critical global variable or something like that is highly improbable. But in a team of folks... maybe?
Do you take the time to really nitpick your global usage to prevent scope creep? no, with CLEAR variable names and employees that did not get their diploma by copying from the net no scope creep happens. That said a lot of 'global' variables can and should be declared static to limit the scope to the module they are in. however if a variable necessarily has to be used in more than one module I will declare it global
an example
static int ralph; void write ralph(int value) { ralph = value; }
makes ralph just as global as declaring it as a globa
I get that a static declared variable, lives in the static memory, no 'static' just limits the scope
now o clear a possible confusion 'global universals' such as int temp1 int temp2
should lead to firing the creator. No global should have more than one use.
FOR POSTERITY
Holy treasure trove batman:
ftp.ti.com/.../C51Primer.pdf
Have you ever seen this C51 Compiler guide, from 1996! Somehow this thing is living on a TI FTP server. Talk about a dream resource for C51.
This answers almost every question I've ever had (though sadly is a little silent on the pointer efficiency front!).
I see the company now does paid consulting for embedded design, but holy cow this PDF is the best thing I've ever read in the last 18 months when thinking of how to improve my code on the 8051.
They literally lay out everything from guys going from Assembly to C. So yes, they do actually explicitly explain the memory model. I get what you guys would debate on that front now. In my case, programs are so small that we aren't anywhere near resource constrained where we have to think of anything but the SMALL memory model.
I have to say it does seem to support the keep-it-simple approach.
Have you ever seen anything equivalent that this is detailed and conversational for the the ARM MDK? I find the KEIL documentation that is available online nowhere near as accessible and well written.
acceptable if not taken religiously
as most such it is a bit dated. e.g. it does not mention IDATA.
That it (due to date) mentions max 8K and then start discussing "pseudoRTOS' is a bit silly. If you need a (pseudo or not) RTOS (more think so than should) it is a grave mistake to choose the '51.
I would still recommend learning C on the PC and then adapting to the '51
we aren't anywhere near resource constrained where we have to think of anything but the SMALL memory model.
oddly enough one should not consider the LARGE model for a large code, the fact is that you are more likely to get by with the LARGE model for a small program.
One more thing, and I'm out:
I am sitting around playing with pointers this afternoon and profiling pointers. I think this probably puts the pointer question to rest for me.
#include <MCUHeader.h> struct Lion { char Age; char Name; char Weight; }; void SetAge(struct Lion *Lion, char age) { Lion->Age = age; } void main(void) { char x; struct Lion Leroy; for (x = 0; x <10; x++) { Leroy.Age = 5; } x = 10; for (x = 0; x <5; x++) { SetAge(&Leroy, 10); } }
I was messing around with using the Setter function with the Lion struct vs. accessing the struct member without the function.
////////////////////////// 5497 Cycles w/ Struct Init for (x = 0; x <10; x++) { SetAge(&Leroy, 5); } //////////////////////////
3857 Cycles w/ Struct Init for (x = 0; x <10; x++) { Leroy.Age = 5; } //////////////////////////
That's like a 30% performance hit to use the Setter function! I'm not super familiar with the compiler, maybe you can tweak that performance with an inline function. (God, I realize I've become an embedded developer, if I'm freaking about the function overhead now).
WOW. I was not expecting a penalty like that, which goes back to Mr. Burmans point.
(Keep in mind, I didn't start using these MCUs with assembly...!)
Erik -- I totally hear you on the RTOS on an 8051, at least on these cheap Asian MCUs the power consumption is atrocious compared to the PIC's and ARM Cortex M's I've used. I am looking through a modern RTOS source code, and can't even fathom running it on the C51 for our products.
We'll just program the bare metal interrupts and get the timing right... I'm not building nuclear missile guidance systems on a $0.20 MCU from Taiwan...
My #1 goal is to put these 8051s back to IDLE mode AS SOON POSSIBLE, and put that power pig to sleep.
With prices of MCU generally in free fall, and an abundance of computational power made available by Cotex M machines, it makes little sense to me to build new products using a highly limited platform that is both outdated and out-performed. Choosing such a chip at the heart of a product nowadays makes no sense as it offers no growth capabilities as many system require. A Cotex M0 will outperform any 8051 with its hands tied behind its back...
it makes little sense to me to build new products using a highly limited platform that is both outdated and out-performed. true - in many cases - but have a look at a '51 ISR with 'using' and with a 8051f120 at 100 MHz you get less than half the response time of a 166 MHz cortex. now, of course if that ISR is to do a lot, things may reverse.
I disagree with using the '51 where it does not fit (I use cortex a lot) but the '51 should not be thrown out of the toolbox.
for a small job you can have the whole '51 project running before you get the cortex BSP done.