This is a little less likely to happen to you but it happened to me! If you're used to 8-bit platforms, you can do this
nice thing where you can typecast variables around. e.g.
uint8_t mybuffer[4];
float f = (float)mybuffer;
You can't be guaranteed that this will work on a 32-bit platform because mybuffer might not be aligned to a 2 or 4-
byte boundary. The ARM Cortex-M0 can only directly access data on 16-bit boundaries (every 2 or 4 bytes). Trying
to access an odd-boundary byte (on a 1 or 3 byte location) will cause a Hard Fault and stop the MCU. Thankfully,
there's an easy work around ... just use memcpy!
uint8_t mybuffer[4];
float f;
memcpy(f, mybuffer, 4)
Floating Point Conversion
Like the AVR Arduinos, the M0 library does not have full support for converting floating point numbers to ASCII
strings. Functions like sprintf will not convert floating point. Fortunately, the standard AVR-LIBC library includes the
dtostrf function which can handle the conversion for you.
Unfortunately, the M0 run-time library does not have dtostrf. You may see some references to using #include
<avr/dtostrf.h> to get dtostrf in your code. And while it will compile, it does not work.
Instead, check out this thread to find a working dtostrf function you can include in your code:
http://forum.arduino.cc/index.php?topic=368720.0 (http://adafru.it/lFS)
How Much RAM Available?
The ATSAMD21G18 has 32K of RAM, but you still might need to track it for some reason. You can do so with this
handy function:
extern "C" char *sbrk(int i);
int FreeRam () {
char stack_dummy = 0;
return &stack_dummy - sbrk(0);
}
Thx to http://forum.arduino.cc/index.php?topic=365830.msg2542879#msg2542879 (http://adafru.it/m6D) for the tip!
Storing data in FLASH
If you're used to AVR, you've probably used PROGMEM to let the compiler know you'd like to put a variable or
string in flash memory to save on RAM. On the ARM, its a little easier, simply add const before the variable name:
const char str[] = "My very long string";
That string is now in FLASH. You can manipulate the string just like RAM data, the compiler will automatically read
from FLASH so you dont need special progmem-knowledgeable functions.
You can verify where data is stored by printing out the address:
Serial.print("Address of str $"); Serial.println((int)&str, HEX);