Skip to content

Commit 781fb3a

Browse files
Update docs, increase max analogWriteFreq to 1MHz (#551)
1 parent bda630e commit 781fb3a

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed

cores/rp2040/wiring_analog.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
#include <hardware/adc.h>
2828

2929
static uint32_t analogScale = 255;
30-
static uint16_t analogFreq = 1000;
30+
static uint32_t analogFreq = 1000;
3131
static bool pwmInitted = false;
3232
static bool adcInitted = false;
3333
static uint16_t analogWritePseudoScale = 1;
@@ -42,9 +42,9 @@ extern "C" void analogWriteFreq(uint32_t freq) {
4242
if (freq < 100) {
4343
DEBUGCORE("ERROR: analogWriteFreq too low (%d)\n", freq);
4444
analogFreq = 100;
45-
} else if (freq > 60000) {
45+
} else if (freq > 1000000) {
4646
DEBUGCORE("ERROR: analogWriteFreq too high (%d)\n", freq);
47-
analogFreq = 60000;
47+
analogFreq = 1000000;
4848
} else {
4949
analogFreq = freq;
5050
}

docs/analog.rst

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,21 @@ While up to 16 PWM channels can be generated, they are not independent
3030
and there are significant restrictions as to allowed pins in parallel.
3131
See the `RP2040 datasheet <https://datasheets.raspberrypi.org/rp2040/rp2040-datasheet.pdf>`_ for full details.
3232

33+
Analog Output Restrictions
34+
--------------------------
35+
36+
The PWM generator source clock restricts the legal combinations of
37+
frequency and ranges. For example, at 1MHz only about 6 bits of range
38+
are possible. When you define an ``analogWriteFreq`` and ``analogWriteRange``
39+
that can't be fulfilled by the hardware, the frequency will be preserved
40+
but the accuracy (range) will be reduced automatically. Your code will
41+
still send in the range you specify, but the core itself will transparently
42+
map it into the allowable PWN range.
43+
3344
void analogWriteFreq(uint32_t freq)
3445
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3546
Sets the master PWM frequency used (i.e. how often the PWM output cycles).
36-
From 100Hz to 60KHz are supported.
47+
From 100Hz to 1MHz are supported.
3748

3849
void analogWriteRange(uint32_t range) and analogWriteResolution(int res)
3950
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

docs/freertos.rst

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,23 @@ FreeRTOS
22
========
33

44
The SMP port of FreeRTOS is included with the core. This allows complex task operations
5-
and real preemptive multithreading in your sketches. To enable FreeRTOS, simply add
5+
and real preemptive multithreading in your sketches. While the ``setup1`` and ``loop1``
6+
way of multitasking is simplest for most folks, FreeRTOS is much more powerful.
7+
8+
Enabling FreeRTOS
9+
-----------------
10+
11+
To enable FreeRTOS, simply add
612

713
.. code:: c++
814

915
#include <FreeRTOS.h>
1016

1117
to your sketch and it will be included and enabled automatically.
1218

19+
Configuration and Predefined Tasks
20+
----------------------------------
21+
1322
FreeRTOS is configured with 8 priority levels (0 through 7) and a process for
1423
``setup()/loop()``, ``setup1()/loop1()``, and the USB port will be created. The task
1524
quantum is 1 millisecond (i.e. 1,000 switches per second).
@@ -19,8 +28,21 @@ only run in core 1 in this mode, the same as the default multithreading mode.
1928

2029
You can launch and manage additional processes using the standard FreeRTOS routines.
2130

22-
2331
``delay()`` and ``yield()`` free the CPU for other tasks, while ``delayMicroseconds()`` does not.
2432

33+
Caveats
34+
-------
35+
36+
While the core now supports FreeRTOS, most (probably all) Arduino libraries were not written
37+
to support preemptive multithreading. This means that all calls to a particular library should
38+
be made from a single task.
39+
40+
In particular, the ``LittleFS`` and ``SDFS`` libraries can not be called from different
41+
threads. Do all ``File`` operations from a single thread or else undefined behavior
42+
(aka strange crashes or data corruption) can occur.
43+
44+
More Information
45+
----------------
46+
2547
For full FreeRTOS documentation look at `FreeRTOS.org <https://freertos.org/index.html>`__
2648
and `FreeRTOS SMP support <https://freertos.org/symmetric-multiprocessing-introduction.html>`__.

0 commit comments

Comments
 (0)