Go to file
Jeff Epler 372306411a
samd: Don't rely on RTC interrupt
I instrumented RTC_Handler and determined that on SAMD51 it was possible
for the interrupt to be delivered well before the actual overflow of the
RTC COUNT register (e.g., a value as small as 0xffff_fffd could be seen
at the time of overflow)

Rather than depending on the overflow interrupt coming in at the same time
as COUNT overflows (exactly), rely only on observed values of COUNT in
_get_count, overflowing when it wraps around from a high value to a low
one.

With this change, PLUS a second change so that it is possible to warp
the RTC counter close to an overflow and test in 20ms instead of 3 days,
there was no problem detected over 20000+ overflows. Before, a substantial
fraction (much greater than 10%) of overflows failed.

Fixes #5985

Change to common-hal/rtc/RTC.c for time warping (plus make rtc_old_count non-static):
```patch
 void common_hal_rtc_set_calibration(int calibration) {
+
+    common_hal_mcu_disable_interrupts();
+
+        RTC->MODE0.COUNT.reg = 0xffffff00;
+        rtc_old_count = 0;
+        do {
+        while ((RTC->MODE0.SYNCBUSY.reg & (RTC_MODE0_SYNCBUSY_COUNTSYNC | RTC_MODE0_SYNCBUSY_COUNT)) != 0) { }
+    }
+    while(RTC->MODE0.COUNT.reg < 0xffffff00);
+    common_hal_mcu_enable_interrupts();
+
+    mp_printf(&mp_plat_print, "Warping RTC in calibration setter count=%08x rtc_old_count=%08x\n", RTC->MODE0.COUNT.reg, rtc_old_count);
```

Test program:
```python
import time
from rtc import RTC

i = 0
while True:
    RTC().calibration = 1 # Warps to ~16ms before overflow, with patch to RTC code
    t0 = time.monotonic_ns()
    et = t0 + 20_000_000 # 20ms
    while (t1 := time.monotonic_ns()) < et: pass
    i += 1
    print(f"{i:6d}: duration {t1-t0}")
    if t1-t0 > 200_000_000: break
    print()
```
2022-03-25 14:48:03 -05:00
.github Fix running tests 2022-02-16 14:28:56 -06:00
data update nvm.toml module to latest 2021-11-04 08:56:14 -06:00
devices/ble_hci Add BLE scanning for S3 and C3. 2022-01-25 16:28:46 -08:00
docs Add binascii crc32 to documentation 2022-03-09 14:02:08 +01:00
examples Fix all natmod tests to run on x64 2022-02-13 11:18:38 -06:00
extmod Rename _uasyncio to _asyncio. 2022-02-23 18:44:55 +09:00
frozen update frozen libraries before 7.2.0 RC 2022-02-18 14:23:10 -05:00
lib Start of USB host API 2022-03-07 18:07:25 -08:00
LICENSES Add an explicit SPDX license file for ter-u12n. 2020-07-06 19:15:41 +01:00
locale Translated using Weblate (Swedish) 2022-03-23 09:58:08 +01:00
logo Add license to some obvious files. 2020-07-06 19:16:25 +01:00
mpy-cross Merge tag 'v1.18' 2022-02-15 12:36:26 -06:00
ports samd: Don't rely on RTC interrupt 2022-03-25 14:48:03 -05:00
py Add mdns module 2022-03-17 18:16:16 -07:00
shared Merge remote-tracking branch 'adafruit/7.2.x' into merge_7.2.2 2022-03-15 13:56:11 -07:00
shared-bindings Merge pull request #6175 from tannewt/mdns 2022-03-21 15:55:03 -05:00
shared-module Fix no scaled framebuffer display 2022-03-23 17:37:56 -05:00
supervisor Merge remote-tracking branch 'adafruit/main' into c3_serial_jtag 2022-03-23 12:17:02 -07:00
tests remove unused import 2022-03-19 11:36:23 -05:00
tools Merge remote-tracking branch 'origin/main' into revamp-duplicate-usb-check 2022-03-24 09:44:41 -05:00
.git-blame-ignore-revs top: Update .git-blame-ignore-revs for latest formatting commit. 2021-04-27 23:46:46 +10:00
.gitattributes Merge MicroPython 1.12 into CircuitPython 2021-05-03 14:01:18 -07:00
.gitignore refine stubs-building procedure 2021-06-22 08:59:05 -05:00
.gitmodules Update ESP-IDF to fix PinAlarm 2022-03-11 14:40:49 -08:00
.mailmap Add a .mailmap file to collate together multiple author entries. 2020-07-06 19:15:41 +01:00
.pre-commit-config.yaml Add neopixel support on PWM capable pins 2021-12-14 16:59:53 -08:00
.readthedocs.yml update sphinx 2021-12-22 11:32:54 -06:00
.rosie.yml Add license to some obvious files. 2020-07-06 19:16:25 +01:00
ACKNOWLEDGEMENTS Merge MicroPython 1.12 into CircuitPython 2021-05-03 14:01:18 -07:00
ACKNOWLEDGEMENTS.license Add license to some obvious files. 2020-07-06 19:16:25 +01:00
BUILDING.md Update submodule instructions. 2022-01-14 11:46:48 -05:00
CODE_OF_CONDUCT.md Update CODE_OF_CONDUCT.md from source-of-truth repository. 2020-06-03 10:43:36 +01:00
conf.py fix finding the describe script during the doc build 2022-02-15 09:06:27 -06:00
CONTRIBUTING.md Fix error 2020-10-26 23:36:54 +01:00
LICENSE LICENSE,docs: Update copyright year range to include 2022. 2022-01-06 15:50:14 +11:00
license.rst Improve docs and update to CircuitPython. 2017-01-05 16:20:46 -08:00
main.c Merge remote-tracking branch 'adafruit/7.2.x' into merge_7.2.2 2022-03-17 14:15:55 -07:00
Makefile Ignore failure of first command. It's deliberate 2021-11-29 16:25:15 -08:00
MANIFEST.in-stubs refine stubs-building procedure 2021-06-22 08:59:05 -05:00
README.rst add esp32s3 documentation in readme 2021-12-03 21:20:55 +05:30
README.rst-stubs Actually reduce the README text for the stubs package 2021-06-24 13:41:01 -05:00
requirements-ci.txt build docs dynamically 2021-09-16 00:05:42 +05:30
requirements-dev.txt use pip-installed circuitpython_typing instead of private copy 2022-02-25 13:49:06 -05:00
requirements-doc.txt add adafruit-circuitpython-typing to requirements-doc.txt 2022-02-27 00:06:40 -05:00
setup.py-stubs use --long for describe in setup.py-stubs 2022-02-18 21:22:02 -05:00
WEBUSB_README.md Rename esp32s2 port to espressif 2021-09-13 16:44:55 -07:00

CircuitPython
=============

.. image:: https://s3.amazonaws.com/adafruit-circuit-python/CircuitPython_Repo_header_logo.png

|Build Status| |Doc Status| |License| |Discord| |Weblate|

`circuitpython.org <https://circuitpython.org>`__ \| `Get CircuitPython <#get-circuitpython>`__ \|
`Documentation <#documentation>`__ \| `Contributing <#contributing>`__ \|
`Branding <#branding>`__ \| `Differences from Micropython <#differences-from-micropython>`__ \|
`Project Structure <#project-structure>`__

**CircuitPython** is a *beginner friendly*, open source version of Python for tiny, inexpensive
computers called microcontrollers.

This package contains the "stubs", or type definitions for CircuitPython.  With some advanced
editors and other tools, this information can be identify TypeErrors, AttributeErrors, and other
problems before you deploy your code to a device and can even help autocomplete your code.

.. |Build Status| image:: https://github.com/adafruit/circuitpython/workflows/Build%20CI/badge.svg
   :target: https://github.com/adafruit/circuitpython/actions?query=branch%3Amain
.. |Doc Status| image:: https://readthedocs.org/projects/circuitpython/badge/?version=latest
   :target: http://circuitpython.readthedocs.io/
.. |Discord| image:: https://img.shields.io/discord/327254708534116352.svg
   :target: https://adafru.it/discord
.. |License| image:: https://img.shields.io/badge/License-MIT-brightgreen.svg
   :target: https://choosealicense.com/licenses/mit/
.. |Weblate| image:: https://hosted.weblate.org/widgets/circuitpython/-/svg-badge.svg
   :target: https://hosted.weblate.org/engage/circuitpython/?utm_source=widget