This removes the difference in the time.ticks_us() range between SAMD21 and
SAMD51.
The function mp_hal_ticks_us_64() is added and used for:
- SAMD51's mp_hal_ticks_us and mp_hal_delay_us().
For SAMD21, keep the previous methods, which are faster.
- mp_hal_ticks_ms() and mp_hal_tick_ms_64(), which saves some bytes
and removes a potential race condition every 50 days.
Also set the us-counter for SAMD51 to 16 MHz for a faster reading of the
microsecond value.
Note: With SAMD51, mp_hal_ticks_us_64() has a 60 bit range only, which is
still a long time (~36000 years).
Methods implemented are:
- rtc.init(date)
- rtc.datetime([new_date])
- rtc.calibration(value)
The presence of this class can be controlled by MICROPY_PY_MACHINE_RTC. If
the RTC module is used, the time module uses the RTC as well.
For boards without a 32kHz crystal, using RTC makes no sense, since it will
then use the ULP32K oscillator, which is not precise at all. Therefore, it
will by default only be enabled for boards using a crystal, but can be
enabled in the respective mpconfigboard.h.
Which just sets the CPU clock to 200kHz and switches the peripheral clock
off. There are two modes:
machine.lightsleep(duration_ms)
and
machine.lightsleep()
In any mode any configured pin.irq() event will terminate the sleep.
Current consumption in lightsleep for some boards:
- 1.5 - 2.5 mA when supplied trough an active USB
(Seeed XIAO w/o power LED, Adafruit ItsyBitsy)
- 0.8 - 2 mA when supplied through Gnd/+5V (Vusb)
(Seeed XIAO w/o power LED, Adafruit ItsyBitsy)
- < 1 mA for SAMD51 when supplied trough a battery connector
(Sparkfun Thing SAMD51 plus)
Related change: move the calls to SysTick_Config() into set_cpu_freq(). It
is required after each CPU freq change to have ticks_ms run at the proper
rate.
The range is 1MHz - 48 MHz. Note that below 8 MHz there is no USB support.
The frequency will be set to an integer fraction of 48 MHz. And after
changing the frequency, the peripherals like PWM, UART, I2C, SPI have to be
reconfigured.
Current consumption e.g. of the Seeed Xiao board at 1 MHz is about 1.5 mA,
mostly caused by the on-board LED (green LED with 1k resistor at 3.3V).
The value given for machine.freq(f) is extend to the range of 1_000_000 to
200_000_000. Frequencies below 48 MHz will be forced to an integer
fraction of 48 MHz. At frequencies below 8 MHz USB is switched off. The
power consumption e.g. of ADAFRUIT_ITSYBITSY_M4_EXPRESS drops to about
1.5 mA at 1 MHz.
Since the peripheral frequency is dropped as well, timing e.g. of PWM,
UART, I2C and SPI is affected and frequency/baud rate has to set again
after a frequency change below 48 MHz.
Two new compile flags are:
MICROPY_HW_DFLL_USB_SYNC: Effective only if DFLL48 does not run from the
crystal. It will synchronize the DFLL48M clock with the USB's SOF pulse.
If no USB is connected, it will fall back to open loop mode. The DFLL48M
clock is then pretty precise, but with a higher clock jitter at SAMD51
devices.
MICROPY_HW_MCU_OSC32KULP: Effective only if the devics uses a crystal as
clock source. Run the MCU clock from the ULP 32kHz oszillator instead of
the crystal. This flag was added to cater for a interference problem of
the crystal and Neopixel/Debug pins at Adafruit FEATHER Mx boards, which
causes the board to crash. Drawback: ticks_ms() and time.time() vs. than
ticks_us() and the peripherals like PWM run at not synchronous clocks.
The SAMD21 implementation is an adaption of @jimmo's code for STM32Lxx.
The only changes are the addresses and names of the port registers and the
timing parameters.
SAMD21: The precision is about +/-25ns at 48MHz clock frequency. The first
two cycles are about 40-60 ns longer than set. But still good enough to
drive a neopixel device.
SAMD51: The precision is about +/-30ns at 120MHz clock frequency. Good
enough to drive a neopixel device.
And use mp_hal_ticks_us() for SAM21's mp_hal_ticks_cpu(). The SAMD21 has
no CYCCNT register, and the SysTick register has only a 1 ms span (== 48000
count range).
Clock settings:
- GCLK0: 48 MHz (SAMD21) or 120 MHz(SAMD51).
- GCLK1: 32768 Hz for driving the PLL.
- GCLK2: 48 MHz for tzhe peripheral clock.
- GCLK3: 1 MHz (SAMD21) or 8 MHz (SAMD51) for the µs ticks timer.
- GCLK8: 1 kHz for WDT (SAMD21 only).
If a 32 kHz crystal is present, it will be used as clock source. Otherwise
the DFLL48M in open-loop mode is used.
GCLK0 for SAM51 can be changed between 48 MHz and 200 MHz. The specified
range is 96 MHz - 120 MHz.