Hello,
I'm new in Arduino 33 IoT, and want to know about its timers and related functions and PWM pins.
In the case of Uno,
-Timer 0 for delay, millis, PWM(5, 6 pins)
-Timer 1 for servo, PWM(9, 10 pins)
-Timer 2 for tone, PWM(3, 11 pins)
What about the case of 33 IoT?
I couldn't find any documents about the case of 33 IoT.
Juraj
April 16, 2020, 5:13am
2
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "variant.h"
const PinDescription g_APinDescription[] = {
/*
+------------+------------------+--------+-----------------+--------+-----------------------+---------+---------+--------+--------+----------+----------+
| Pin number | MKR Board pin | PIN | Notes | Peri.A | Peripheral B | Perip.C | Perip.D | Peri.E | Peri.F | Periph.G | Periph.H |
| | | | | EIC | ADC | AC | PTC | DAC | SERCOMx | SERCOMx | TCCx | TCCx | COM | AC/GLCK |
| | | | |(EXTINT)|(AIN)|(AIN)| | | (x/PAD) | (x/PAD) | (x/WO) | (x/WO) | | |
+------------+------------------+--------+-----------------+--------+-----+-----+-----+-----+---------+---------+--------+--------+----------+----------+
| 00 | D0 | PA22 | | *06 | | | X10 | | 3/00 | 5/00 |* TC4/0 | TCC0/4 | | GCLK_IO6 |
| 01 | D1 | PA23 | | *07 | | | X11 | | 3/01 | 5/01 |* TC4/1 | TCC0/5 | USB/SOF | GCLK_IO7 |
| 02 | D2 | PA10 | | 10 | *18 | | X02 | | 0/02 | 2/02 |*TCC1/0 | TCC0/2 | I2S/SCK0 | GCLK_IO4 |
you can see in analogWrite implementation how this table is used for PWM
here I have other use of the timer using the arduino pin mapping
#endif
#ifdef ARDUINO_ARCH_SAMD
// setup the external interrupt
attachInterrupt(zcPin, zeroCrossing, RISING);
ulExtInt = g_APinDescription[zcPin].ulExtInt;
EIC->EVCTRL.reg |= (1 << ulExtInt);// enable event
EIC->INTENCLR.reg = EIC_INTENCLR_EXTINT(1 << ulExtInt); // turn-off interrupt
const PinDescription& pinDesc = g_APinDescription[triacPin]; // Arduino pin description
TCC = (Tcc*) GetTC(pinDesc.ulPWMChannel);
uint8_t tcChannel = GetTCChannelNumber(pinDesc.ulPWMChannel);
bool periF = (pinDesc.ulPinAttribute & PIN_ATTR_TIMER_ALT);
// setup the pin as TCC wave out pin
pinPeripheral(triacPin, periF ? PIO_TIMER_ALT : PIO_TIMER);
// setup the timer
REG_GCLK_CLKCTRL = (uint16_t) (GCLK_CLKCTRL_CLKEN | GCLK_CLKCTRL_GEN_GCLK0 | GCLK_CLKCTRL_ID(GCM_TCC0_TCC1)); // assign clock
I appreciate your reply.
I checked the link but I couldn't understand the codes
For example, D2 seems related with "TC5/0" and "TCC0/4" in the table, but I have no idea what they are
and whether they both effect analogWrite for D2 pin.
And it seems like the link does not show the mappings between the timers and the Arduino functions
that use timers internally such as delay, millis, analogWrite, tone, etc.
Would it be possible to provide some list of 33 IoT timer usages like the Uno example below?
[This example is for Uno]
-Timer 0 for delay, millis, PWM(5, 6 pins)
-Timer 1 for servo, PWM(9, 10 pins)
-Timer 2 for tone, PWM(3, 11 pins)
The above shows that if I change frequency of Timer 1, I cannot use servo function.
It also shows that if I use tone function, I cannot use PWM from 3 and 11 pins.
I would like to know such things for 33 IoT.
Thanks!
Juraj
April 16, 2020, 3:16pm
4
the table for 33 IoT is here
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "variant.h"
#include "Arduino.h"
/*
* Pins descriptions
*/
const PinDescription g_APinDescription[] = {
/*
+------------+------------------+--------+-----------------+--------+-----------------------+---------+---------+--------+--------+----------+----------+
| Pin number | NANO Board pin | PIN | Notes | Peri.A | Peripheral B | Perip.C | Perip.D | Peri.E | Peri.F | Periph.G | Periph.H |
| | | | | EIC | ADC | AC | PTC | DAC | SERCOMx | SERCOMx | TCCx | TCCx | COM | AC/GLCK |
| | | | |(EXTINT)|(AIN)|(AIN)| | | (x/PAD) | (x/PAD) | (x/WO) | (x/WO) | | |
+------------+------------------+--------+-----------------+--------+-----+-----+-----+-----+---------+---------+--------+--------+----------+----------+
| | Digital Low | | | | | | | | | | | | | |
+------------+------------------+--------+-----------------+--------+-----+-----+-----+-----+---------+---------+--------+--------+----------+----------+
| 0 | 0 -> RX | PB23 | | 07 | | | | | | *5/03 | TC7/1 | | | GCLK_IO1 |
| 1 | 1 <- TX | PB22 | | 06 | | | | | | *5/02 | TC7/0 | | | GCLK_IO0 |
the SAMD has mux for pins. the datasheet shows mapping of pins to peripherals.
the table in variant.cpp has all this options in the A, B, C, D, E, F columns and has a * at the mapping selected by Arduino for the implementation of the core functions like the analogWrite, analogRead, Serial, Serial1, attachInterrupt
this mapping is then put in an array of pin mapping structs. the index in this array is the pin number as labeled on the PCB (or some are internal).
so if you call analogWrite on 6, the function uses the mapping on index 6 to get the SAMD pin number PA04, the mux 'column' E selected by Arduino for analogWrite on this pin and the timer TTC0 and the channel 0 associated with mux 'column' E for PA04 .
Thank you for the quick deep answers!
So if I understood correctly, TCC0 is in charge of PWM out for D5,D6,D9,and D10 pins if analogWrite is called on those pins?
But still have no clue about what timers do the "delay", "tone", "servo (library)" functions use.
Juraj
April 16, 2020, 6:05pm
6
in Tone.cpp
#define TONE_TC TC5
#define TONE_TC_CHANNEL 0
delay.c
delay() use micros() which uses the SysTick counter, not an interrupt
ServoTimers.h
#define TC_FOR_TIMER1 TC4
#define CHANNEL_FOR_TIMER1 0
#define TC_FOR_TIMER2 TC4
#define CHANNEL_FOR_TIMER2 1
I really appreciate your kind crystal clear answers.
It was my first time to post a question and I get more confidence using Arduino because of you people who are willing to help others like me.
Thank you!
By the way, where can I find all the implementations of the functions such as
delay, servo, and tone?
I searched servo and found the following link
/*
Copyright (c) 2015 Arduino LLC. All right reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*
* Defines for 16 bit timers used with Servo library
This file has been truncated. show original
But couldn't find other functions (tone, delay) from Arduino Libraries · GitHub
And can I assume the codes for SAMD as for the 33 IoT?
Thank you.
Juraj
April 20, 2020, 9:56am
9
alexprotocol:
By the way, where can I find all the implementations of the functions such as
delay, servo, and tone?
I searched servo and found the following link
Servo/src/samd/ServoTimers.h at master · arduino-libraries/Servo · GitHub
But couldn't find other functions (tone, delay) from Arduino Libraries · GitHub
And can I assume the codes for SAMD as for the 33 IoT?
Thank you.
all this source files are on your computer and in verbose mode you can see in IDE console the command lines which compile them.
delay and tone are in core.
on github the samd core is here