Libraries for Zero?

When I was working with an Arduino Uno, I used a few avr-libc libraries (util/delay.h, util/crc16.h, etc).

A point-in-the-best-direction for Arduino Zero equivalents would be most appreciated.

I would expect <util/delay.h> and <util/crc16.h> to work fine on an Arduino ZERO. Have you tried them?

I would not expect <avr/pgmspace.h> or other AVR-specific include files to work on a non-AVR processor but you didn’t ask about that. :slight_smile:

Heh, well, I do admit that I just picked the first two AVR libraries that my build failed to find when I wrote that post.

It looks like crc16.h is mostly assembly, however, which certainly won't build for an M0. It seems like delay.h might have a chance with a little modification? At first glance the __builtin_avr_delay_cycles() call is the only thing I see that would fail. The more hardware-ish libraries such as pgmspace.h certainly would be the ones that would have the most problems, however, as you pointed out.

I'm certainly not the first person to have looked at this, but google comes up surprisingly empty. There are a few other threads in here where people ported AVR hardware references to M0. Maybe I'll add a complete one once I'm done to help the next guy.

BigBobby:
It looks like crc16.h is mostly assembly, however, which certainly won't build for an M0.

Are you looking at the AVR version of delay.h? You should look at the SAMD version, probably somewhere in the Arduino15 directory. Remember that when you select the target board you are also selecting which compilers and libraries to use.

Yes, I was looking at the AVR libraries I used with the Uno that were easy to find in the “C:\Program Files (x86)\Arduino\hardware\tools\avr\avr\include\util” subdirectory and documented in avr-libc: Modules.

It seems when I downloaded the M0 package it put the closest equivalent in the “C:\Users<username>\AppData\Local\Arduino15\packages\arduino\hardware\samd\1.6.15\cores\arduino” directory?

I don’t see the full range of utils that I got for the AVR, but it’s a step in the right direction.

It seems when I downloaded the M0 package it put the closest equivalent in the “C:\Users<username>\AppData\Local\Arduino15\packages\arduino\hardware\samd\1.6.15\cores\arduino” directory?

The “util” functions are avr-libc specific functions, not part of either C standards or Arduino, and not directly duplicated by the ARM libraries, as far as I know. (there may be near-equivalents, somewhere.) There is some attempt to “fake” a few of the avr-specific functions (pgmspace in particular) by providing an “avr” sub-directory in the (arm) arduino core directory, but I don’t see an equivalent for “util”

I did go searching for an instruction-loop based delay() function at one point, with rather depressing results.
The avr delay functions are ultimately dependent on a __builtin_avr_delay_cycles() provided by the compiler, and there’s no equivalent of that in the ARM compiler :frowning: It turns out that the ARM architecture makes this particularly difficult to do, what with its pipelining, and especially the “unpredictability” of instruction memory access times. It turns out that most flash memory tops out at about 20MHz. Chips that run faster than that (including the 48MHz Zeros) introduce “wait states”, so that accesses to flash take more than one cycle. But that’s bad, so various “acceleration” is added to speed things up again (this ranges from accessing flash more than one instruction at a time, to more complicated schemes bordering on “cache.” Although the CM0 doesn’t specifically include cache ‘support’.) All in all, it gets very difficult to predict exactly how many cycles a particular instruction sequence or loop will take :frowning:

As for CRC, I’d think that the Zero has enough memory to just use one of the traditional pure-C byte-wide CRC functions…

Well hello! The 2016 version of you was just helping me here → http://forum.arduino.cc/index.php?topic=403674.5. Is the “newlib nano” for the ARM chips something you still feel I should be looking at?

Some of what you wrote here I figured out over the past hour once I’d located where the ARM package was placed. It does look like I’m going to write my own byte-wide CRC code. Not a big deal, but I like to investigate what exists to minimize the projects I zip up for distribution before including my own stuff.

Is the "newlib nano" for the ARM chips something you still feel I should be looking at?

Well, it's still what all the ARM Arduinos are using, AFAIK.

It does look like I'm going to write my own byte-wide CRC code.

There's one in rfc1662 (with references.) You can probably find one already written with constants for any popular polynomial, as well as ones that will calculate the table at run-time, given the polynomial specification...