analogWrite() source #864
-
Where do I find the Pico's source code for analogWrite()? I would like to study it in hopes of writing a PWM based servo library. (I'm already using both PIOs, so I'm unable to use the Servo library provided.
|
Beta Was this translation helpful? Give feedback.
Replies: 4 comments
-
Learning to fish: You can use the search box, near the top left of a Github page, and use the default option of "In this repository". You may well find it in a different branch, so you'll normally want to change to the Master branch, by clicking the branch name. For some things, you'll need to look in the SDK repository: https://github.com/raspberrypi/pico-sdk Free fish: It's in this repository (rather than the SDK one), under: The other approach is to watch the micros() timer, and wait for it to reach the next time you want the code to take some action. E.g., see discussion #184 for an example of doing that. If you do it that way, you should expect some jitter caused by interrupts and page cache misses (or similar) where the CPU has to load more things into the cache - an instruction doesn't always take the same amount of time to execute, because of needing to load the cache, or copy from Flash into RAM, and interrupts can cause fairly significant delays to your code being executed. You can get around some of that by specifying that time critical functions should run from RAM by adding __not_in_flash_func to the function definition. See discussion #421 - you can skip the first half. You might be able to avoid some of the interruptions from interrupts (which will happen however you do it), by running the code on core 1, instead of the default core 0 - I'm not sure if that helps though. You can define setup1() and loop1() functions to do that. |
Beta Was this translation helpful? Give feedback.
-
Just a thought, you say you are already using both PIO's. I ask, as from playing with Micropython, Circuitpython and CPP, I think that Circuitpython manage to use a single PIO machine to control multiple Servo pins at the same time (up to 32 interleaved I think). So it may be worth a bit of searching in the Circuitpython source to try to see if anything there may be worth porting to CPP, either to free up some of you PIO machines, or rework what you have. |
Beta Was this translation helpful? Give feedback.
-
https://github.com/earlephilhower/arduino-pico/blob/master/cores/rp2040/wiring_analog.cpp |
Beta Was this translation helpful? Give feedback.
-
That link is just what I needed. Thank you! |
Beta Was this translation helpful? Give feedback.
That link is just what I needed. Thank you!