core: analog: add analogWriteFrequency#493
Conversation
|
Pushed up with another possible new API: analogWriteLastStatus That returns the last return code from zephyr pwm_ call Allows you to check for example if your set frequency fails on the next analogWrite... Example sketch: |
Built
|
| Artifact | Board | Core | Tests | RAM | Sketches | Warnings | Errors |
|---|---|---|---|---|---|---|---|
✅* zephyr_contrib |
ek_ra8d1
| 📗 | ✅* |
11.9% |
2 | 2 | - |
frdm_mcxn947
| 7 🏷️ | ✅* |
58.0% |
2 | 2 | - | |
frdm_rw612
| 2 🏷️ | ✅* |
83.0% |
2 | 2 | - | |
✔️* zephyr_main |
giga
| 5 🏷️ | ✅* |
54.7% |
58 | 18 | - |
nano33ble
| 1 🏷️ | ✅* |
78.8% |
30 | 12 | - | |
nano_matter
| 1 🏷️ | ✅* |
|
22 | 10 | - | |
nicla_vision
| 5 🏷️ | ✔️* |
57.0% |
52 | 10 | (10*) | |
niclasense
| 2 🏷️ | ✅* |
|
26 | 12 | - | |
opta
| 5 🏷️ | ✅* |
57.7% |
62 | 18 | - | |
portentac33
| 3 🏷️ | ✔️* |
|
66 | 10 | (8*) | |
portentah7
| 4 🏷️ | ✅* |
58.5% |
74 | 18 | - | |
✅* zephyr_unoq |
unoq
| 📗 | ✅* |
26.9% |
56 | 10 | - |
Legend
Board Test Status description 🔥 🔥 Test run failed to complete. ❌ 🔴 Test completed with unexpected errors. ✔️* 🚫 Test completed with errors, but all are known/expected. ✅* 🟡 Test completed with some warnings; no errors detected. ✅ 🟢 Test passed successfully, with no warnings or errors. 🌑 🌑 Test was skipped.
I know there are several of us who wished that the Arduino API would expose more of the capabilities of the board, for example currently most all of the PWM pins are hard configured for something like 500hz. Such as: https://forum.arduino.cc/t/arduino-uno-q-very-poor-pwm/1445321 This Pull Request added the API: analogWriteFrequency which has two parameters, which pin, and frequency in floating point. The analogWrite function was updated to use the user requested frequency. This reqired the code to not typically use the arduino_pwm[idx].period field of the devlice tree array directly as this array is defined as const. So created a simple array to hold the current frequency converted to nanoseconds. And we needed to change the zephyr function we called to: (void)pwm_set(arduino_pwm[idx].dev, arduino_pwm[idx].channel, arduino_pwm_periods[idx], value, arduino_pwm[idx].flags); Tested with simple sketch on Q: ``` //----------------------------------------------------------------------------- // Copy of the Arduino Zephyr analog code - to map from Pin Number to zephyr // object and channel //----------------------------------------------------------------------------- void setup() { Serial.begin(115200); pinMode(3, OUTPUT); // Start of the PWM... analogWriteResolution(10); analogWrite(PWM_PIN, 64); delay(100); // Quick and dirty to set the period and pulse in nano seconds //size_t idx = pwm_pin_index(PWM_PIN); //pwm_set(arduino_pwm[idx].dev, arduino_pwm[idx].channel, 1000, 500, PWM_POLARITY_NORMAL); analogWriteFrequency(PWM_PIN, 1000000); } uint16_t duty = 0; void loop() { delay(10); digitalWrite(3, !digitalRead(3)); analogWrite(PWM_PIN, duty); duty += 64; if (duty == 1024) duty = 1023; else if (duty > 1024) duty = 0; } ``` Which appears to work Also added: a version of that function to return the current frequency for a pin, as well as another function that will return the status code returned for the last pwm_set call within analogWrite
|
Added in method to return the current frequency for a pin, can also use this to enumerate which pins are defined as PWM pins. Sketch has been extended like: With the Serial output of: |
Built
|
| Artifact | Board | Core | Tests | RAM | Sketches | Warnings | Errors |
|---|---|---|---|---|---|---|---|
✅* zephyr_contrib |
ek_ra8d1
| 📗 | ✅* |
11.9% |
2 | 2 | - |
frdm_mcxn947
| 7 🏷️ | ✅* |
58.0% |
2 | 2 | - | |
frdm_rw612
| 2 🏷️ | ✅* |
83.0% |
2 | 2 | - | |
✔️* zephyr_main |
giga
| 5 🏷️ | ✅* |
54.7% |
58 | 16 | - |
nano33ble
| 1 🏷️ | ✅* |
78.8% |
30 | 10 | - | |
nano_matter
| 1 🏷️ | ✅* |
|
22 | 8 | - | |
nicla_vision
| 5 🏷️ | ✔️* |
57.0% |
52 | 8 | (10*) | |
niclasense
| 2 🏷️ | ✅* |
|
26 | 12 | - | |
opta
| 5 🏷️ | ✅* |
57.7% |
62 | 18 | - | |
portentac33
| 3 🏷️ | ✔️* |
|
66 | 8 | (8*) | |
portentah7
| 4 🏷️ | ✅* |
58.5% |
74 | 16 | - | |
✅* zephyr_unoq |
unoq
| 📗 | ✅* |
26.9% |
56 | 8 | - |
Legend
Board Test Status description 🔥 🔥 Test run failed to complete. ❌ 🔴 Test completed with unexpected errors. ✔️* 🚫 Test completed with errors, but all are known/expected. ✅* 🟡 Test completed with some warnings; no errors detected. ✅ 🟢 Test passed successfully, with no warnings or errors. 🌑 🌑 Test was skipped.
|
Memory usage change @ cc3cb88
Click for full report table
Click for full report CSV |
|
@KurtE REASON: Used the hack identified in https://forum.arduino.cc/t/arduino-uno-q-very-poor-pwm/1445321, this allowed to get me to get better performance when using the XV-11 Lidar which seems to work better when changing the freq from 500hz to 4khz. Think I mentioned this in the thread as well. Results of adding the analogwritefreq is seen in https://forum.arduino.cc/t/getting-a-neato-xv-11-lidar-working-on-the-q/1445568/12 If I run into any issues I will let post it here. UPDATE: Thank you.. Hopefully it will get incorporated. |
I know there are several of us who wished that the Arduino API would expose more of the capabilities of the board, for example currently most all of the PWM pins are hard configured for something like 500hz. Such as: https://forum.arduino.cc/t/arduino-uno-q-very-poor-pwm/1445321
This Pull Request added the API:
analogWriteFrequency which has two parameters, which pin, and frequency in floating point.
The analogWrite function was updated to use the user requested frequency. This reqired the code to not typically use the arduino_pwm[idx].period field of the devlice tree array directly as this array is defined as const. So created a simple array to hold the current frequency converted to nanoseconds.
And we needed to change the zephyr function we called to:
(void)pwm_set(arduino_pwm[idx].dev, arduino_pwm[idx].channel,
arduino_pwm_periods[idx], value,
arduino_pwm[idx].flags);
Tested with simple sketch on Q:
Which appears to work