PDA

View Full Version : Support for ATOMIC_BLOCK?



matt
11-23-2012, 03:46 AM
Any plans to implement the atomic functionality provided by the AVR file utils/atomic.h for the Teensy3?
I'd like to use an RFM22 library which uses the ATOMIC_BLOCK macros so currently it won't compile

Matt

PaulStoffregen
11-23-2012, 04:21 AM
Could you give a URL directly to this library?

matt
11-23-2012, 05:07 AM
Sorry, here it is:
http://www.open.com.au/mikem/arduino/RF22/
http://www.open.com.au/mikem/arduino/RF22/RF22-1.23.zip

pico
11-25-2012, 09:54 AM
I've ported a version of Dean Camera's ATOMIC_BLOCK macros from AVR to Maple, which is ARM Cortex M3, so I can't see why it shouldn't also work straight-up on the Teensy 3.0. The assembly language instructions should be the same, and the underlying compiler is gnu. If you want to test it on your Teensy 3.0, let me know and I'll email you the cortex_m3_atomic.h file (it includes everything, the inline functions are defined in the .h file.) Also, if Paul wants to include this in his distribution for the Teensy 3.0, I'm happy to donate to the cause.

matt
11-27-2012, 02:49 AM
Thanks, I'll give your file a go (PM sent). Hopefully Paul will be able to add support into Teensy 3.0 at some point.

Matt

PaulStoffregen
11-27-2012, 06:16 AM
Sure, I'll add it. can you post the code, or a link?

pico
11-27-2012, 10:53 AM
Thanks, I'll give your file a go (PM sent). Hopefully Paul will be able to add support into Teensy 3.0 at some point.

Matt

I've emailed you the file to the address in your PM. I've also cc'ed Paul at paul (at) pjrc.com. Hopefully, "should just work", but any questions or issues, let me know.

matt
11-27-2012, 10:35 PM
I've emailed you the file to the address in your PM. I've also cc'ed Paul at paul (at) pjrc.com. Hopefully, "should just work", but any questions or issues, let me know.
Thank you. I added the file into the library RF22 library directory for now (until it becomes part of the Teensy distribution)
I did find a small error in the file:


..\sketchbook\libraries\RF22/util/cortex_m3_atomic.h: In function 'void __set_primask(uint32_t)':
..\sketchbook\libraries\RF22/util/cortex_m3_atomic.h:18: error: expected ';' before 'asm'

So I added the missing ; here


{ __asm__ volatile ("MSR PRIMASK, %[value]\n\t""dmb\n\t""dsb\n\t""isb\n\t"::[value]"r"(setval):);


And it compiles. I can't confirm if it works yet, as I've found another problem :-(


..\sketchbook\libraries\RF22\RF22.cpp:520: error: 'memcpy_P' was not declared in this scope

Thanks for your help

Matt

PaulStoffregen
11-27-2012, 11:54 PM
Here is an updated Teensy 3 core library, with Pico's atomic.h and a definition for memcpy_P. I was able to compile the RF22 test example. I don't have the hardware, so I can't test if it actually works.

http://www.pjrc.com/teensy/beta/teensy3_27nov12.zip

To use this, replace your hardware/teensy/cores/teensy3 directory with the one from this file. Restart Arduino before using it.

Please let me know if it works?

pico
11-28-2012, 11:40 AM
I did find a small error in the file:


..\sketchbook\libraries\RF22/util/cortex_m3_atomic.h: In function 'void __set_primask(uint32_t)':
..\sketchbook\libraries\RF22/util/cortex_m3_atomic.h:18: error: expected ';' before 'asm'

So I added the missing ; here


{ __asm__ volatile ("MSR PRIMASK, %[value]\n\t""dmb\n\t""dsb\n\t""isb\n\t"::[value]"r"(setval):);


And it compiles.


Yes, don't know how that crept in. Usual random source of error between chair and keyboard, no doubt.

Actually, three characters went missing. As well as the ';', there should be a ' ' and '\' (space and backslash) to indicate a continuation line.


{ __asm__ volatile ("MSR PRIMASK, %[value]\n\t""dmb\n\t""dsb\n\t""isb\n\t"::[value]"r"(setval):); \


I'm not sure if it makes any difference to the inlining of the function, to have the function broken over more than one line, but that's the way Dean had them in the original, so I was trying to keep to the convention.

matt
11-28-2012, 12:19 PM
Please let me know if it works?
The library will recognise an RFM22B device attached to the SPI port, so it's partially working (it can at least read the device ID over the SPI port)
It now seems to get stuck in an endless interrupt loop. I need to debug further to find out why

Matt

matt
11-29-2012, 11:27 AM
I've got it working successfully now. Thanks Paul & Mark.

The problem with the interrupts were 2 fold
1) INT0 is Pin0, not Pin2 as on an Arduino
2) As posted elsewhere in the forum, I had to set Pin0 to input before the interrupts would work.
Since the attachInterrupt call is in a library, that means changing the library.

I assume that the arduino behaves differently in this case. Is it possible/sensible to make teensy3 set a pin as input when calling attachInterrupt?

Matt

mercapto
11-29-2012, 11:56 AM
The reason of defining the PinMode separately to attachInterrupt is the possibility of SoftwareInterrupts. If you define an Output Pin and attach the Pin to an Interrupt you can write to the Pin and call the ISR.

PaulStoffregen
11-29-2012, 05:05 PM
Arduino's attachInterrupt() function does not actually set the pin to input mode.

However, on Arduino, every pin defaults to input mode at startup. On Teensy 3.0, the non-analog pins default to disconnected.

I've considered adding startup could that would set every pin to input mode, so more closely emulate Arduino. I might indeed still do this, but there are trade-offs. The main problem is "floating" or disconnected input mode pins consume power. This happens on AVR too, and on a board like Teensy++ it can add up to quite a bit of current. I haven't tested how much current it can add on Teensy 3.0, but generally the problem can become worse as the chip runs on lower voltages.

So far, most programs have used pinMode() to define the pin as INPUT. Obviously the RF22 library example does not. Maybe it would be worthwhile to contact the author about adding pinMode to the examples?

JBeale
11-29-2012, 05:28 PM
The main problem is "floating" or disconnected input mode pins consume power.

The architecture does not provide the possibility of internal weak pullup or pulldown as a "keeper" to reduce floating-input power draw?

PaulStoffregen
11-29-2012, 06:37 PM
Both weak pullups and pulldowns are available.

However, in terms of emulating Arduino, enabling those by default would be even worse. The default behavior before your code runs is high impedance, which is needed for lots of circuitry that could be adversely affected if driven (even weakly) before your program runs.

MichaelMeissner
12-01-2012, 08:40 PM
Yes, don't know how that crept in. Usual random source of error between chair and keyboard, no doubt.

Actually, three characters went missing. As well as the ';', there should be a ' ' and '\' (space and backslash) to indicate a continuation line.


{ __asm__ volatile ("MSR PRIMASK, %[value]\n\t""dmb\n\t""dsb\n\t""isb\n\t"::[value]"r"(setval):); \


I'm not sure if it makes any difference to the inlining of the function, to have the function broken over more than one line, but that's the way Dean had them in the original, so I was trying to keep to the convention.

The compiler doesn't care, as long as you break it between the pairs of double quotes, since the compiler will glue together adjacent double quoted strings with only whitespace and comments in between the adjacent strings. I often times do this to make things more readable:



{ __asm__ volatile ("MSR PRIMASK, %[value]\n" \
"\tdmb\n" \
"\tdsb\n" \
"\tisb\n\t" \
: \
: [value] "r" (setval):); \