DIRECT PROGRAMMING SAMD21 REGISTERS
OUTPUT PWM FREQUENCY / RESOLUTION OF ARDUINO ZERO
TABLE OF CONTENTS
3. BLOCKS DIAGRAM DESCRIPTION
4. SYSTEM MATHEMATICS & THE PPWM TOOL
5. SKETCHES GENERAL
6. PRECISION VOLTAGE TRANSLATOR VREF. = 2229.7 mV
7. PRECISION VOLTAGE TRANSLATOR VREF. = 2500 mV
At the present date Arduino Zero technical documentation is incomplete in many operating areas. This paper forms the basis for the definition and the choice of the operating parameters of the Arduino Zero board to control either the PWM output frequency or the PWM output resolution of up to 8 pins. It must be noted that, with the contemporary IDEs, the maximum available device output resolution is 8 Bit, because, even if the ambigous vendor's description seems to imply differently, the new command, analogWriteResolution is not effective with Arduino Zero. The proposed software solves this problem, obtaining resolutions up to and exceeding 16 Bit, trading off with output PWM frequency.
The proposed system works faultlessly with Windows XP SP3 and with Windows W7. To date no tests with Window 8 or Windows 10 have been carried out.
The system produces a high quality Dual Slope (or Phase Correct) PWM signal by using up to 3 UP/DOWN Registers or Timers.
The information herewith contained is only valid for Arduino/Genuino Zero boards equipped with IDE 1.6.0 and later (1.6.8 recommended) supplied by Arduino c.c. This information describes just a few of the infinite configurations made possible by the remarkable complexity and flexibility of the SAMD21 Microprocessor Architecture. The present work consists of the following documents:
a) This description.
b) Functional Blocks Diagram.
c) PRECISION PWM Excel tool.
d) Example sketches explaining the setup and use of the information provided.
The functional Blocks Diagram explains the frequency generation / PWM generation system of this particular set-up within the ATMEL SAMD21 Microprocessor that is the engine of the Arduino Zero board. This is a functional diagram, which follows the system logic and does not necessarily respect the physical interconnection of the various parts. Study of this diagram will immediately clarify the meaning of the expressions used in the sketches and give a comprehensive system picture.
2. DEVICE INSTALLATION
The detailed device description can be found here:
Make sure you have a good/professional quality USB cable with MICRO B USB connector (expensive). Most cheap cables do not work.
The installation for this ARM Cortex device is more complex than for other boards. First go here:
and install the ARM Cortex core, following the detailed instructions.
Then go here:
read all notices and follow instructions to install the drivers. All work described in our posts will be done using the PROGRAMMING PORT.
Once the board is connected to the USB, be extra careful in checking:
(a) the COM PORT in the PC's DEVICE MANAGER and in the IDE.
(b) that in the BOARD MANAGER of the IDE "Genuino/Arduino Zero - Programming Port" is selected.
Load sketch "ZERO BLINKER SPECIAL" (uploaded here) to check that all is in working order.
3. BLOCKS DIAGRAM DESCRIPTION
NOTE: all information from the SAMD 21 Technical Spreadsheet and from ASF Source Code Documentation, available on the ATMEL site.
With reference to the block diagram, the SAMD 21 chip is equipped with the following oscillators, of which one can be selected according to the job to be implemented, as shown further on.
• XOSC32K – frequency 32.768 KHz.
• OSCULP32K – frequency 32 KHz, low power.
• OSC 8M – frequency 8 MHz.
• DFLL48M – frequency 48 MHz. Phase Locked Loop Oscillator locked to the 8 Mhz carrier.
The oscillators serve a group of 8 Generic Clock Controllers, or GCLKs. Only GCLKs 4 to 7 are available for independent use. The output of the chosen GCLK is connected to frequency scaler GENDIV, which can be set to divide the Clock frequency by any divisor D in the range: 1 – 250 with the command:
The processed Clock signal is then further divided by Prescaler CTRLA, which has a default setting of 16. CTRLA possible Prescaler settings are: N = 1, 2, 4, 8, 16, 64, 256 and 1024 and can be set with the command:
Prescaler’s output drives the three U/D Registers TCC0 (24Bit), TCC1 (24Bit) & TCC2 (16Bit). They are programmed with the PER parameter and count UP to the PER value, then down to zero and so on: in this mode of operation the PER parameter is used TWICE, hence we have an additional /2 factor. The value of the PER parameter is crucial for setting either the PWM output resolution, or the PWM output frequency or finding a good compromise between them. By Divisor and Prescaler manipulation a very large number of solutions is possible, as will be seen further on. The PER parameter setting command is:
REG_TCCX_PER = K (with X = 0, 1, 2)
We now need to connect the outputs of the Timers to the output pins by means of a Multiplexer. We do this in two steps:
a) first we enable the Multiplexer with the PORT MULTIPLEXER ENABLE expression shown in the blocks diagram and
b) second we connect the Timers to the outputs with the CONNECT THE TCCX TIMERS TO THE OUTPUTS expression, shown in the blocks diagram.
Output pins are paired in EVEN and ODD numbers, as shown both in the above expression and in the outputs with different colours.
The COMPARE CHANNEL BUFFERS CCBX set the PWM percentage or duration either by reading an integer from zero (PWM 0% D.C.) to PER (PWM 100% D.C.) set with the command below, or by reading voltage values on the INPUT PINS (A0 to A5) or by reading data received through Serial or other COMMS. The command is:
REG_TCCX_CCBW, where W = 0, 1, 2, & 3.
The PWM capable output pins are:
ARDUINO REGISTERS SAMD 21
D2...............TCC0 CCB0..............Chip Pin PA8 (even)
D3...............TCC1 CCB1..............Chip Pin PA9 (odd)
D4...............TCC1 CCB0..............Chip Pin PA14 (even)
D5...............TCC0 CCB1..............Chip Pin PA15 (odd)
D6...............TCC0 CCB2..............Chip Pin PA20 (even)
D7...............TCC0 CCB3..............Chip Pin PA21 (odd)
D11..............TCC2 CCB0..............Chip Pin PA16 (even)
D13..............TCC2 CCB1..............Chip Pin PA17 (odd)
The Arduino Zero board is therefore capable of up to 8 Multi-Frequency / Multi-Resolution PWM input/output channels. The following pins are free for other uses: for example pins D8 & D9 can be used as inputs when D11 and D13 are used as outputs.
D0 & D1: Serial Comms.
D8, D9,D10, D12