8 ms·
Shhhhhh! If you let them know how fun it is then everyone will want to be C programmers :-) I got to use my crufty C knowledge to useful effect when I discover
by ChuckMcM 13y ago
Shhhhhh! If you let them know how fun it is then everyone will want to be C programmers :-)
I got to use my crufty C knowledge to useful effect when I discovered that there is no standard system reset on Cortex M chips. That lead me to trying to call "reset_handler" (basically the function that kicks off things at startup) which I couldn't do inside an ISR because lo and behold there is "magic" in isrs, they are done in "Handler" mode versus "Thread" mode and jumping to thread mode code is just wrong apparently. C hackery to the rescue, hey the stack frame is standard, make a pointer to the first variable in the function walk backwards on the stack to return address, change it to be the function that should run next, and return. Voila, system reset.
The whole time I am going "Really? I have to look under your covers just to make you do something anyone might want to do?" As a respondent to one of my questions put it "ARM is a mixture of clever ideas intermixed with a healthy dose of WTF?"
- couchand 13y agoCortex as in Cortex Semi?
- diydsp 13y agoExactly! Fortunately, the article didn't point out one of C's great advantages is that it's so lightweight it can fit into and run on all kinds of interesting systems where other languages are infeasible economically. Cortices rule!
- pjmlp 13y agoExcept there are other safer languages to choose from, like Pascal and Basic compilers. http://www.mikroe.com/mikropascal/ http://www.mikroe.com/mikropascal/ http://www.mikroe.com/mikrobasic/ http://www.mikroe.com/mikrobasic/
- brokenparser 13y agoAnd they're all way too slow for what I'm doing.
- pjmlp 13y agoWhy are they slow? If you are going to invoke the usual C FUD about bounds checking and such, you can disable them on parts where performance REALLY matters. Plus all compilers share the same backend.
- exDM69 13y ago> Except there are other safer languages to choose from, like Pascal and Basic compilers. I'm curious, why do you say "safer"? These are languages for microcontroller programming. The things you do there are bound to be "unsafe", like peeking and poking memory for memory mapped i/o and disabling/enabling interrupts. Unless, of course, all the possible things (i/o, timers, interrupts, etc) are wrapped in some kind of "safe" api so you essentially don't have access to low level facilities any more. The Arduino programming environment is kinda like this but you can still cause bad things to happen and if all else fails, hang the device with an infinite loop. Is there any backing for such claims of "safety"?
- weland 13y ago> I'm curious, why do you say "safer"? These are languages for microcontroller programming. The things you do there are bound to be "unsafe", like peeking and poking memory for memory mapped i/o and disabling/enabling interrupts. There are things which are inherently safe by design in languages like Pascal -- e.g. in a naively-written program you can't write past the boundary of the UART RX buffer and thrash some other array in your program -- but your observation is fair.
- eckzow 13y agoI don't mean to be a party pooper since I've done my fair share of hacky workarounds in v7-M processors and it is always exhilarating when it works... But since I'm a Cortex M fanboy I have to defend its reset capabilities :) For your specific case, try something like volatile uint32_t *AIRCR = (volatile uint32_t*)0xE000ED0C; const uint32_t VECTKEY = 0x05FA << 16; const uint32_t SYSRESETREQ = 1 << 2; void take_reset() { *AIRCR = VECTKEY | SYSRESETREQ; __dsb(); while(1); } It's technically dependent on external hardware in your processor subsystem, but it should work if your implementer has half a brain (or, at least cares enough to read the integration manual). If it doesn't work, please flog your implementer publicly so that I can know to avoid them in the future... Incidentally, even that tiny code snippet is uses a C extension (the __dsb intrinsic) which is either a great example of how C can be wielded to great power (I can generate raw instructions!) or how C is terribly handicapped (I need a special compiler extension or all my system code is horribly broken!). All depends on point of view, I guess... Anyway, more info @ page 498, http://web.eecs.umich.edu/~prabal/teaching/eecs373-f10/readings/ARMv7-M_ARM.pdf http://web.eecs.umich.edu/~prabal/teaching/eecs373-f10/readi...
- stusmall 13y agoFor anyone else curious on the __dsb() call, from ARM's docs: The Data Synchronization Barrier (DSB) acts as a special kind of memory barrier. The DSB operation will complete when all explicit memory accesses before this instruction have completed. No instructions after the DSB will be executed until the DSB instruction has completed, that is, when all of the pending accesses have completed.
- ChuckMcM 13y agoI too had already been through that particular part of the ARM7-M manual, after coming up through the data sheet to the technical reference guide, to the Cortex M architecture manual, and yes to the base ARM7-M architecture specification. There is "should" and "does" :-) I had followed the exact same sequence and discovered on my system it didn't cause a system reset. I escalated it a bit (in this case ST Micro) and the caveats came out, "Well if you system is correctly designed, if the actual reset pin isn't connected to something that is interfering, if the core is in a state where it can actually take a reset, ..." This being unlike pretty much every processor I've worked on, from PDP-8's, 11's, 10's, VAXen, IBM 360/370, Sun 1, 2, 3, 4, Motorola 6800, 68K, 68K, Z80, 8080, 806, Pentium *, the list goes on. Most actually have a reset instruction, usually privileged, to force a system reset. But the difference is that the reset sequence was guaranteed to force a system reset if it executed, all of those previous processors had a company that made the processor as opposed to simply licensed the processor to a third party. Since it is completely reproducible on my system I've got an action item to create a small test case for errata analysis. Yes, that is the code they suggest, with assurances that it will work in pretty much any case. Except when it might not. It was a bit stunning for me, I still marvel at the notion. Like an add statement that will 99.999% of the time add its operands together[1] unless it doesn't. [1] No there isn't such a thing in ARM it is just the way I hear "It will almost always reset the system."
- Svip 13y agoI want to be a C programmer now! (I am actually serious, this article has inspired me to try some more C; which I have been seriously neglecting.)
- adobriyan 13y ago> I want to be a C programmer now! Don't hold your breath.
- nevvvermind 13y agoHa. Me too. I always felt a little intimidated by it, but I can't pass the chance to actually make my own structures.
- csmithuk 13y agoDo it; you will never regret it. I primarily write C# (for cash reasons) but my heart will always be with C.
- lmm 13y ago> Do it; you will never regret it. Citation needed. I hate hate hated C and am very glad to have seen the back of it.
- csmithuk 13y agoI too hated C once to the point I actually kicked a PC over on its side out of anger. I never understood pointers, structures and string manipulation. I genuinely hated it and wanted it to go away. I deleted all the code I'd written then went for a walk. Then suddenly... click. Suddenly, in my mind appeared an abstract model for it. It was complete and possible for one person to understand. Since then I understand what I'm doing rather than know how to drive the compiler. That has never happened for any other language I've written and that includes Z80+6502+X86 ASM, C++, C#, VB3-6, PDS7, ksh, Python, Perl and PHP to name a few. Assembly is close but not abstract enough. Enlightenment is probably the best word to describe it.
- timthorn 13y agoRe: your ARM comment - had you noticed who wrote the article?
- ChuckMcM 13y agoWho Simon Tatham? I've been a fan ever since I modified the PuTTY code to work in an embedded system. I did not realize he was at ARM these days, gives me hope.