FEATHER-M0-ArduinoIDE-Croquis

De MCHobby - Wiki
Sauter à la navigation Sauter à la recherche


MCHobby investit du temps et de l'argent dans la réalisation de traduction et/ou documentation. C'est un travail long et fastidieux réalisé dans l'esprit Open-Source... donc gratuit et librement accessible.
SI vous aimez nos traductions et documentations ALORS aidez nous à en produire plus en achetant vos produits chez MCHobby.

Introduction

L'ATSAMD21 est un tout petit composant mais encore assez récent comme nouveau venu dans le monde des Arduino-compatible. La plupart des croquis et bibliothèques fonctionnerons mais il y a quelques petites choses à noter!

Les notes ci-dessous concerne toutes les cartes M0 mais il n'est pas nécessaire de toutes les appliquer (ex: Trinket M0 et Gemma M0 n'ont pas de broche ARef, vous pouvez donc sauter les notes concernant la Référence Analogique!)

Référence Analogique

Si vous désirez utiliser la broche ARef pour une référence analogique différente de 3.3V, le code à utiliser est analogReference(AR_EXTERNAL) (avec un AR_EXTERNAL et non EXTERNAL)

Broches et pull-up

L'ancienne façon d'activer une broche en entrée avec un avec résistance pullup etait:

pinMode(pin, INPUT)
digitalWrite(pin, HIGH)

Parce que le registre de selection-pullup (pullup-selection) est le même que le registre de sélection-des-sortie (output-selection).

Vous ne pouvez plus utiliser cette approche avec le M0! Il faut utiliser:

pinMode(pin, INPUT_PULLUP)

Approche qui à au moins le bénéfice d'être rétro-compatible avec AVR.

Serial ou SerialUSB

99.9% des croquis Arduino existant utilise Serial.print pour sortie de débogage ou sortie console. Sur un Arduino SAMD/M0 officiel, cette instruction utilise le port Serial5, qui n'est pas exposé sur un Feather.

A la place, le port USB d'un Arduino M0 officiel est appelé SerialUSB.

Sur un Adafruit M0, Adafruit a corrigé cela de sortie que Serial est dirigé vers l'USB lorsque vous utilisez un Feather M0 (et donc tout fonctionne correctement automatiquement).

Si vous avez des croquis existants et que vous désirez le faire fonctionner sur un M0 sans occasionner de nombreuses opérations de remplacement, placez le code suivant:

#if defined(ARDUINO_SAMD_ZERO) && defined(SERIAL_PORT_USBVIRTUAL)
   // requis pour les opérations Serial sur les cartes à base de Zero
   #define Serial '''SERIAL_PORT_USBVIRTUAL'''
#endif

juste au dessus de la première définition de fonction dans votre code.

Par exemple:

FEATHER-M0-ArduinoIDE-Croquis-01.png
Crédit: AdaFruit Industries www.adafruit.com

AnalogWrite / PWM sur Feather M0/Metro M0

Après avoir consulté la fiche technique du SAMD21, Adafruit à constaté qu'il manquait quelques options listée dans la table multiplexeur sur la puce spécifiquement utilisée sur un Feather M0.

Sur toutes les puces SAMD21, il y a deux périphériques capables de générer des signaux PWM: Le Timer/Counter (TC) et Timer/Counter pour le contrôle des applications (TCC). Chaque SAMD21 dispose de plusieurs copies de chaque, appelés 'instances'.

Chaque instance TC dispose d'un registre de comptage (count), un registre de contrôle (control) et de deux canaux de sortie (output channel). Chaque canal peut être activé, désactivé et même inversé. Les broches connectées sur une instance TC peuvent produire une version identique de la même forme d'onde, ou forme d'onde complémentaire.

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 analogWrite(pin, 255) 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 analogWrite(pin, 255) and, instead, does a digitalWrite(pin, HIGH)

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

#include <util/delay.h>

you'll get an error that says

fatal error: util/delay.h: No such file or directory
  #include <util/delay.h>
                         ^
compilation terminated.
Error compiling.

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:

#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

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.

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:

<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:

extern "C" char *sbrk(int i);

int FreeRam () {
  char stack_dummy = 0;
  return &amp;stack_dummy - sbrk(0);
}

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:
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



Source: Adafruit Feather M0 Express - Designed for CircuitPython créé par LadyAda, Tony DiCola, Scorr Shawcroft, Dan Halbert pour AdaFruit Industries. Crédit AdaFruit Industries

Traduit par Meurisse D. pour MCHobby.be

Traduit avec l'autorisation d'AdaFruit Industries - Translated with the permission from Adafruit Industries - www.adafruit.com

Toute référence, mention ou extrait de cette traduction doit être explicitement accompagné du texte suivant : «  Traduction par MCHobby (www.MCHobby.be) - Vente de kit et composants » avec un lien vers la source (donc cette page) et ce quelque soit le média utilisé.

L'utilisation commercial de la traduction (texte) et/ou réalisation, même partielle, pourrait être soumis à redevance. Dans tous les cas de figures, vous devez également obtenir l'accord du(des) détenteur initial des droits. Celui de MC Hobby s'arrêtant au travail de traduction proprement dit.