Modifications

Sauter à la navigation Sauter à la recherche
8 547 octets ajoutés ,  28 octobre 2017 à 19:13
aucun résumé de modification
Ligne 2 : Ligne 2 :     
{{traduction}}
 
{{traduction}}
 +
== Introduction ==
 +
 +
The ATSAMD21 is a very nice little chip but its fairly new as Arduino-compatible cores go. '''Most''' sketches & libraries will work but here's a few things we noticed!
 +
 +
The below note are for all M0 boards, but not all may apply (e.g. Trinket and Gemma M0 do not have ARef so you can skip the Analog References note!)
 +
 +
== Analog References ==
 +
If you'd like to use the '''ARef''' pin for a non-3.3V analog reference, the code to use is <code>analogReference(AR_EXTERNAL)</code> (it's AR_EXTERNAL not EXTERNAL)
 +
 +
== Pin Outputs & Pullups ==
 +
The old-style way of turning on a pin as an input with a pullup is to use
 +
 +
<code>pinMode(pin, INPUT)<br></code><code>digitalWrite(pin, HIGH)</code>
 +
 +
This is because the pullup-selection register is the same as the output-selection register.
 +
 +
For the M0, you can't do this anymore! Instead, use
 +
 +
<code>pinMode(pin, INPUT_PULLUP)</code>
 +
 +
which has the benefit of being backwards compatible with AVR.
 +
 +
== Serial vs SerialUSB ==
 +
99.9% of your existing Arduino sketches use '''Serial.print''' to debug and give output. For the Official Arduino SAMD/M0 core, this goes to the Serial5 port, which isn't exposed on the Feather. The USB port for the Official Arduino M0 core, is called '''SerialUSB''' instead.
 +
 +
In the Adafruit M0 Core, we fixed it so that Serial goes to USB when you use a Feather M0 so it will automatically work just fine.
 +
 +
'''However, on the off chance you are using the official Arduino SAMD core not the Adafruit version (which really, we recommend you use our version because as you can see it can vary) & you want your Serial prints and reads to use the USB port, use SerialUSB instead of Serial in your sketch'''
 +
 +
If you have existing sketches and code and you want them to work with the M0 without a huge find-replace, put
 +
 +
<code>#if defined(ARDUINO_SAMD_ZERO) && defined(SERIAL_PORT_USBVIRTUAL)<br>   // Required for Serial on Zero based boards<br>   #define Serial '''SERIAL_PORT_USBVIRTUAL'''<br> #endif</code>
 +
 +
'''right above the first''' function definition in your code. For example:
 +
 +
{{ADFImage|FEATHER-M0-ArduinoIDE-Croquis-01.png|640px}}
 +
 +
== AnalogWrite / PWM on Feather/Metro M0 ==
 +
After looking through the SAMD21 datasheet, we've found that some of the options listed in the multiplexer table don't exist on the specific chip used in the Feather M0.
 +
 +
For all SAMD21 chips, there are two peripherals that can generate PWM signals: The Timer/Counter (TC) and Timer/Counter for Control Applications (TCC). Each SAMD21 has multiple copies of each, called 'instances'.
 +
 +
Each TC instance has one count register, one control register, and two output channels. Either channel can be enabled and disabled, and either channel can be inverted. The pins connected to a TC instance can output identical versions of the same PWM waveform, or complementary waveforms.
 +
 +
Each TCC instance has a single count register, but multiple compare registers and output channels. There are options for different kinds of waveform, interleaved switching, programmable dead time, and so on.
 +
 +
The biggest members of the SAMD21 family have five TC instances with two 'waveform output' (WO) channels, and three TCC instances with eight WO channels:
 +
 +
 +
* TC[0-4],WO[0-1]
 +
* TCC[0-2],WO[0-7]
 +
 +
And those are the ones shown in the datasheet's multiplexer tables.
 +
 +
The SAMD21G used in the Feather M0 only has three TC instances with two output channels, and three TCC instances with eight output channels:
 +
 +
 +
* '''TC[3-5],WO[0-1]'''
 +
* '''TCC[0-2],WO[0-7]'''
 +
 +
Tracing the signals to the pins broken out on the Feather M0, the following pins can't do PWM at all:
 +
 +
 +
* '''Analog pin A5'''
 +
 +
The following pins can be configured for PWM without any signal conflicts as long as the SPI, I2C, and UART pins keep their protocol functions:
 +
 +
 +
* '''Digital pins 5, 6, 9, 10, 11, 12, and 13'''
 +
* '''Analog pins A3 and A4'''
 +
 +
If only the SPI pins keep their protocol functions, you can also do PWM on the following pins:
 +
 +
 +
* '''TX and SDA (Digital pins 1 and 20)'''
 +
 +
== analogWrite() PWM range ==
 +
On AVR, if you set a pin's PWM with <code>analogWrite(pin, 255)</code> it will turn the pin fully HIGH. On the ARM cortex, it will set it to be 255/256 so there will be very slim but still-existing pulses-to-0V. If you need the pin to be fully on, add test code that checks if you are trying to <code>analogWrite(pin, 255)</code> and, instead, does a <code>digitalWrite(pin, HIGH)</code>
 +
 +
== Missing header files ==
 +
there might be code that uses libraries that are not supported by the M0 core. For example if you have a line with
 +
 +
<code>#include <util/delay.h></code>
 +
 +
you'll get an error that says
 +
 +
<code>fatal error: util/delay.h: No such file or directory<br>  #include <util/delay.h><br>                         ^<br>compilation terminated.<br>Error compiling.</code>
 +
 +
In which case you can simply locate where the line is (the error will give you the file name and line number) and 'wrap it' with #ifdef's so it looks like:
 +
 +
<syntaxhighlight lang="python">
 +
#if !defined(ARDUINO_ARCH_SAM) &amp;&amp; !defined(ARDUINO_ARCH_SAMD) &amp;&amp; !defined(ESP8266) &amp;&amp; !defined(ARDUINO_ARCH_STM32F2)
 +
#include &lt;util/delay.h&gt;
 +
#endif
 +
</syntaxhighlight>
 +
 +
The above will also make sure that header file isn't included for other architectures
 +
 +
If the #include is in the arduino sketch itself, you can try just removing the line.
 +
 +
== Bootloader Launching ==
 +
For most other AVRs, clicking '''reset''' while plugged into USB will launch the bootloader manually, the bootloader will time out after a few seconds. For the M0, you'll need to ''double click ''the button. You will see a pulsing red LED to let you know you're in bootloader mode. Once in that mode, it wont time out! Click reset again if you want to go back to launching code
 +
 +
== Aligned Memory Access ==
 +
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.
 +
 +
<code>uint8_t mybuffer[4];</code><br><code>float f = (float)mybuffer;</code>
 +
 +
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!
 +
 +
<code>uint8_t mybuffer[4];<br>float f;<br>memcpy(f, mybuffer, 4)</code>
 +
 +
== 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:
 +
 +
<a href="http://forum.arduino.cc/index.php?topic=368720.0">http://forum.arduino.cc/index.php?topic=368720.0</a>
 +
 +
== 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:
 +
 +
<syntaxhighlight lang="python">
 +
extern "C" char *sbrk(int i);
 +
 +
int FreeRam () {
 +
  char stack_dummy = 0;
 +
  return &amp;stack_dummy - sbrk(0);
 +
}
 +
</syntaxhighlight>
 +
 +
Thx to <a href="http://forum.arduino.cc/index.php?topic=365830.msg2542879#msg2542879">http://forum.arduino.cc/index.php?topic=365830.msg2542879#msg2542879</a> 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:<br>'''Serial.print("Address of str $"); Serial.println((int)&str, HEX);'''
 +
 +
If the address is $2000000 or larger, its in SRAM. If the address is between $0000 and $3FFFF Then it is in FLASH
 +
    
{{FEATHER-M0-EXPRESS-TRAILER}}
 
{{FEATHER-M0-EXPRESS-TRAILER}}
29 917

modifications

Menu de navigation