Saturday, 11 October 2014

How can I detect which arduino board (or which controller) in software?


I have a project that I want to work on either a Uno or a Mega (or even a Due) and it would be nice if I didn't need two versions of the software. For example, on a Mega, to use SoftwareSerial, you have to use different pins than the ones on an Uno. See the docs on Software Serial. Anyway, it would be nice to detect that I'm using an Uno so I can just use pins 4 and 5 for TX/RX and if I'm using a Mega the software will detect and just use pins 10 and 11 (and of course, I'll have to wire it up differently but at least the software will be the same).



Answer




To my knowledge you cannot detect the board type, but you can read the ATmega device ID. Check this question how it can be done: Can an ATmega or ATtiny device signature be read while running? Notice though when using this method, several register assignments will change, not just the pinout. Therefore your code may get significantly more complex. The advantage is that if you manage to work around all changing register assignments and other hardware dependencies, you can use a single .hex file to program your devices directly from avrdude.



Another way to figure out the board/controller type is at compile time. Basically you compile parts of the code or set macros depending on the device type configured in the Arduino IDE. Check this code sniplet for an example:


#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
#define DEBUG_CAPTURE_SIZE 7168
#define CAPTURE_SIZE 7168

#elif defined(__AVR_ATmega328P__)
#define DEBUG_CAPTURE_SIZE 1024
#define CAPTURE_SIZE 1024
#else
#define DEBUG_CAPTURE_SIZE 532
#define CAPTURE_SIZE 532
#endif

The code sniplet was shamelessly copied from https://github.com/gillham/logic_analyzer/wiki Check that code for some some more device specific trickery.


Depending on your host's operating system, the supported controller types can be found in the following file:




  • Linux: /usr/lib/avr/include/avr/io.h

  • Windows: ...\Arduino\hardware\tools\avr\avr\include\avr\io.h


The use of C-preprocessor (by which the above code is handled) is probably out of scope for this site. http://stackoverflow.com would be the better place for detailed questions.


If you are on Linux you can easily find all supported controller types by typing:


grep 'defined (__AVR' /usr/lib/avr/include/avr/io.h | sed 's/^[^(]*(\([^)]*\))/\1/'

No comments:

Post a Comment

arduino - Can I use TI's cc2541 BLE as micro controller to perform operations/ processing instead of ATmega328P AU to save cost?

I am using arduino pro mini (which contains Atmega328p AU ) along with cc2541(HM-10) to process and transfer data over BLE to smartphone. I...