2018-06-08 12:20 PM
This may be a galactically stupid question, but I'm an 8-bit programmer making the leap to 32-bit programming.
On 8-bit CPUs, we could 'share' (read and set) a 1 byte (char, uint8_t) variable between ISRs and the main loop. Setting or reading it was atomic, so the ISR could set a flag and the main loop could read it, without corruption by interrupting between assembly instructions.
Is the same true, by chance, for 32-bit processors and 32 bit variables (uint32_t, int32_t)? Can I read to and write from a single 32-bit variable from ISRs and main loop with impunity?
(I'm guessing I'm not so lucky. In fact now I'm hoping nobody shoots down my 8-bit assertion as a premise...)
Thanks!
#isr #atomic2018-06-08 01:23 PM
Sure you can.
Cortex-M instruction set provides atomic load/store exclusive instructions LDRX, STRX as well as byte and half-word variants.
See
http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0553a/BABFFBJB.html
andhttp://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0553a/BABFFBJB.html
2018-06-08 04:27 PM
I declare a table and flags in main.h
then every module can access it directly.
I use the DMA ADC to fill that table and set the flags, then the foreground loop can work at it own pace, checking buffer levels and completion flags.
2018-06-09 02:53 AM
'
Is the same true, by chance, for 32-bit processors and 32 bit variables (uint32_t, int32_t)?'
yes, and also true for non-32-bit types -> you just need to be careful with atomicity.
2018-06-10 07:54 PM
Hello, thank you. Do you think that I can trust that a reputable C compiler (I'm using Keil, so it's good) will use these atomic instructions for general read/writes to a uint32_t? Or should I inspect the assembly code for each possible conflict and confirm that it's using these commands? Or should I consult my compiler documentation?
Thanks... not asking you to research for me, just wondering what your gut feeling is.
2018-06-10 07:55 PM
Thank you dhenry. Similar question as mine above for David: do you think that I can trust that a good C compiler will be 'careful with atomicity', or should I inspecting assembly at every get and set? Thanks.
2018-06-11 03:20 AM
The compiler will generate ordinary LDR/STR instructions for reading/writing uint32_t or any other type.
You must explicitly use LDREX/STREX instructions to access the shared variable. Keil offers intrinsics for this but they are deprecated
http://www.keil.com/support/man/docs/armcc/armcc_chr1359124997286.htm