Go Down

Topic: Arduino Zero Arcitecture Call (Read 138 times) previous topic - next topic

Need_for_sleep

Good Morning all,

I've got a two faced question that may be very simple to answer but also could put me at a halt on my project.  To keep things short for now--I'm currently working on a research project at my college over the summer, which requires audio sampling and recording from a microphone on an Arduino to an SD card.  I've been using an incredible library I found on GitHub (https://github.com/TMRh20/AutoAnalogAudio) which was originally optimized for an Arduino Due.  I've run it on a Due, and it works perfectly.  However, I want to port my code over to a Zero because my product will need to conserve power and sleep for about 23 hours of the day, and it's much easier to conserve power on a Zero than a Due.

To get to the meet and the potatoes:
In the SRC of the library I linked to, there are files to get the program working on SAM and AVR processors.  The default source file is the AVR, which obviously won't run on a Zero.  My thoughts are that since one of the very first lines on the SAM source file is

Code: [Select]
#if defined (ARDUINO_ARCH_SAM)

that it would be possible to change that to

Code: [Select]
#if defined (ARDUINO_ARCH_SAMD)

since the Zero runs a SAMD type architecture.  Now since this did not link the source file properly I'm going to assume I'm 100% incorrect with my assumptions, and this is why I've come back to the forums.  If you have any recommendations at all, they'd be greatly appreciated.  Also if anyone knows if it's possible to easily conserve power on the Due (yes I know wrong forum, but I thought it would be worth mentioning) it would be a huge help.

pert

That's correct. ARDUINO_ARCH_SAMD will only be defined when compiling for a board with SAMD architecture. We would probably need more information on exactly what you mean by "did not link the source file properly" to provide any further assistance.

Need_for_sleep

Hi Pert,

Thank you very much for the response.  To clear things up; There are two source files.  One is the default, and used for the AVR boards.  The program uses this files as the default when there is no SAM board found.  The second source file is utilized for the Due, however I want to see if it is possible to make this run on the Zero as is.  I'm trying to change the source file such that it recognizes the Zero's architecture, and uses this source file as opposed to the AVR file.  Does that help at all?  Thank you!

Need_for_sleep

In addition, I know the two boards although having a similar processor and architecture were not made to always run the same code (my apologies in advanced if I'm not relaying enough information or asking stupid questions).  However, generally speaking, would it take more time to try and rewrite code to make it functional on the Zero (it highly utilizes the ADC and DAC, with additional interrupts I believe) or to try and make a sleep function for a Due to have it conserve as much power as possible?

I know it's a very open ended and likely an impossible question to answer without knowing more.  In a bit of a time constraint (as I know we all are with projects) and trying to weigh my options as to what the better solution may be.

Thank you very much again in advanced for the help!

pert

OK, I see now. That library puts the source files in architecture specific folders. When present, the Arduino IDE will automatically select the file in the folder that matches the architecture of the selected board. Here's my suggestion:
  • Create a new folder,  AutoAnalogAudio/src/samd
  • Copy AutoAnalogAudio/src/sam/AutoAnalogAudio.cpp to AutoAnalogAudio/src/samd
  • Open AutoAnalogAudio/src/samd/AutoAnalogAudio.cpp in a text editor
  • Change line 21 from:

Code: [Select]
#if defined (ARDUINO_ARCH_SAM)
to:
Code: [Select]
#if defined (ARDUINO_ARCH_SAMD)

This will give you a separate copy of AutoAnalogAudio.cpp specifically for SAMD so any changes you make there will not affect SAM compatibility. If you don't like the code duplication it is also possible to use a single source file with the preprocessor directives to determine which architecture specific code is used depending on which board is being compiled for. Generally I prefer that approach but since the author of the library has already gone down the other path it might be easiest to continue in that direction.

Need_for_sleep

Thank you so much!  I will try this now and get back to you with my results.

Need_for_sleep

Just to follow up,

Unfortunately this did not solve my problems as I would have liked it to.  I've accumulated around 300 errors along the way as well.

I tried making a single source file in addition to adding one just for SAMD, with no success.  I realized about half way through I also had to go into the parent folder and AVR sub folder to change a handful of #if defines to include SAMD.  There was also a case where the AVR checks to see if SAM had been defined, so I added a check for SAMD, but that also seemed to give me errors.  I'm not sure if it's worth trying to get this single library to work on the Zero, so I think I may work my way over to using power conservation/low power backup mode on the Due.

Thank you again very much for your help with this!

pert

#7
Jul 13, 2017, 08:03 pm Last Edit: Jul 13, 2017, 08:04 pm by pert
Porting some libraries can be very difficult if they use a lot of architecture specific code. At a high level the Arduino API makes transitioning from one architecture to another fairly easy but to go deeper than that you often will need to be willing to spend some quality time with a datasheet that is many hundreds of pages long.

To write unified code that works for multiple architectures you can do this:

Code: [Select]
#if !defined(ARDUINO_ARCH_AVR) && !defined(ARDUINO_ARCH_SAM) && !defined(ARDUINO_ARCH_SAMD)
#error "The architecture of your board is not supported by this library."
#endif

...
Code: [Select]
#if defined(ARDUINO_ARCH_AVR)
// AVR specific code here
#elif defined(ARDUINO_ARCH_SAM)
// SAM specific code here
#elif defined(ARDUINO_ARCH_SAMD)
// SAMD specific code here
#endif


This allows you to share generic portions of the code between all supported architectures and the preprocessor directives are only needed when architecture specific code is required. In the case of source code that has a lot of code that can be shared, this option will require much less effort to maintain.

westfw

Quote
I've been using an incredible library I found on GitHub (https://github.com/TMRh20/AutoAnalogAudio) which was originally optimized for an Arduino Due.
Based on the datasheets, It looks to me like the SAM3X and SAMD ADC peripherals are "substantially different."
The chances that the Due library can be easily modified to run on the SAMD are very small :-(
On the other hand, if you like the Due library, at least you have a "design" that could be used as a template.  That might be about half the work.  (But I gather that that particular sort of work isn't supposed to be part of your project...)

You might also look at PJRC's "Teensy 3.x" series.  These are smaller and more recent than the Due, probably leading to  better power consumption, and Paul has an extensive audio library[url] for them.


Go Up