-
Notifications
You must be signed in to change notification settings - Fork 46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(ADC): support DMA by setting DREQs based on ADC FIFO fill level #90
Conversation
The existing ADC and DMA code already had nearly everything – so this commit just adds some DREQ logic. It is inspired by the PIO DREQ code.
Looks good, thanks! Mind sharing here the complete test code that you used? |
One thing I noticed is that the sample timing using I tested this using |
Unfortunately, it's currently not in a good shape at all – like a combination of Python and Typescript hacks. But I plan to publish my (Python) project as open source :) |
Another thing I noticed – I think the simulated ADC code is in need of some better reset logic. Currently, it cannot get out of |
The reason I'm asking - if at some point in the future there someone points out issues with ADC + DMA, we'll have a baseline code that we know works well.
Yes, if you need timing accuracy, you should replace the clock with a virtual one. The real time clock can never be accurate enough on a preemptive multi-tasking OS (it might be possible if you pin the process to a specific core, and make sure nothing else runs on the same core, but I doubt anyone will do this). |
Also, I'm interested: how have you come across rp2040js? Do you have some ideas to build upon it? |
OK, makes sense, I'll somehow compile what I did. Not sure what would be the best way to document this – just make it part of this PR? But I probably won't put up much effort for automatizing such a test.
That sounds like a good idea. But I'm not sure how I would go about that – and not sure I'm currently interested in digging into this :)
Well, I'm wondering if there are some "low hanging fruit" for this problem – something like e.g. https://github.com/onury/tasktimer In general, with a modern Linux kernel, it should be possible to get much better accuracy. However, I'm not sure how well Node lends itself to something like that.
Um, not completely sure any more – it either was by just googling some Pico DMA/ADC details, or by actually searching for something like "raspberry pi pico simulator" because my Pico HW is not arriving and I wanted to test some code ;) What sold me on it was the example https://wokwi.com/arduino/projects/300504213470839309 and of course the Github repo. (BTW – I think those nice Micropython examples are not reachable directly via the wokwi.com page, only from the Definitely a very pleasant surprise that you took the effort of creating this!
Well, not too much currently to be honest. I'm just using it to run my code in an actually realistic Micropython environment, so for my developing and testing my project code. I find it very impressive that you can run the "official" Micropython port/SDK. In my test description for ADC/DMA I will also include how I connected the "usual" Micropython development tools to rp2040js. Could be worth it to try and include that in your |
OK, I now don't think it would be easy at all to use a better "real" clock here... |
Thanks! Adding a comment with the code in this discussion can be very useful for anyone who ever touches this code in the future. It's okay not automating the test - we don't have automated tests for all the peripherals.
Good point - I opened wokwi/wokwi-features#265 to track this. And thanks for the feedback!
If you are interested, I can probably come up with an example. It's would probably be similar in nature to the clock implementation we use in AVR8js |
Alright, regarding ADC/DMA testing – Everything I did is based on my project which I just published on https://github.com/tomods/GrinderController. ADC/DMA handling is done in RP2040ADC.py. The state machine calls To provide some input to the simulated ADC, I just hacked up your demo script, see micropython-run.ts. This is probably not very elegant. I then used As rp2040js does not support flash/filesystem (yet), I used one more hack – just removing all Python The debug logging my code currently does will then output the averaged ADC values coming in via DMA every 10 cycles. So then I just looked at those and manually compared them to the values provided in my Before applying the changes, DMA would – quite obviously – hang indefinitely. Ideally, this test would be extended to really test that each and every ADC value is read/copied, but I currently don't see a big risk here. You can also find my timing test code using |
Alright, this seems to work well. Thanks Tobias! Released as rp2040js 0.14.8, also made a Wokwi test project based on your code: |
Nice, you're welcome! By the way – did I see correctly that your simulator web "frontend" is not open source? At least I couldn't find it. Would be interested in learning how you did the file handling – especially for the Python scripts – and how the simulated inputs/output are connected to rp2040js. |
True
There are some details here (for read-only access): And if you wish to make the virtual flash filesystem writable as well, check out the discussion in #88 |
p.s. GrinderController sounds like a cool project. Instead of measuring the battery voltage, how about measuring the current? This can be done directly (by adding a low-resistance power resistor in series), or indirectly (with an hall-effect current sensor) |
Thanks for checking out my project :) Measuring the current – more or less directly – was my first thought for the project, and is currently my "backup plan". Basically, I'd like to keep things as simple as possible, especially regarding hardware components. So, my current plan is to try it out using the battery voltage measurement only – if that fails, I will probably try to add a shunt resistor to measure the current. Thanks for the hint about other types of current sensors! |
That worked :) Would you be interested in a PR for |
Perhaps it's better to have a different example, |
(littlefs stuff continued at #92) |
The existing ADC and DMA code already had nearly everything – so this just adds some DREQ logic to the ADC code. It is inspired by the PIO DREQ code.
I did some basic tests from Micropython, using the approach from https://iosoft.blog/2021/10/26/pico-adc-dma/ – seems to work fine so far.
Please note that I'm a Javascript/Typescript novice ;)