Atomic read/write for both processors to GPIO #1139
-
Earle, you have done an amazing job. I so much appreciate being able to stick with Arduino IDE even when using SDK routines. My program was working great (driving a BLDC motor) until I duplicated the code to run a second motor on the second processor. I've been using simple Arduino routines digitalRead() and digitalWrite() The problem is Motor 1 stops when motor 2 runs in certain conditions. Is there a way to do the atomic XOR write in Arduino? (SDK call?) Thanks for your help (or anyone else that has done this) Gavin |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments
-
I'm reading Datasheet 2.3.1.2. GPIO Control Thanks again |
Beta Was this translation helpful? Give feedback.
-
There might be a slightly faster way depending on the number of write ports on the GPIO block, but you'd need to ping the RPI Pico forums to get the HW designers' answer, but why not just use a mutex to protect GPIO accesses? Define a shared global GPIO mutex:
Then acquire the mutex before doing any atomic changes necessary:
Both cores' access to the GPIOs should use that same mutex. It will guarantee only one core at a time can modify GPIO state. |
Beta Was this translation helpful? Give feedback.
-
I'd also probably disable interrupts after taking the mutex (and before touching GPIOs), and re-enable them before exiting the mutex. I don't know BLDC motor control, but if it is driven by a Gray code then only 1 bit will be XOR'd between steps so you could just write to that XOR register inside the mutex protection and avoid any glitching) |
Beta Was this translation helpful? Give feedback.
-
The data sheet provides links to the relevant SDK code so this is where the XOR write (aka toggle) function can be found, viz: However, the GPIO operations are already atomic and made so at hardware level in the SIO block, so I suspect the problem is elsewhere in the code. Look at the duplicated code you mentioned and look for any shared (global) variables that the two processors are sharing, this is likely where the problem is. Post the code if you cannot spot the problem. |
Beta Was this translation helpful? Give feedback.
-
Thank you everyone for your suggestions. Realizing that the GPIO accesses are already atomic I took the advice and looked elsewhere. After rewriting the code (probably unnecessarily) to separate all the routines for each processor to no avail, I turned to the PCB. FYI ExpressPCB Plus is crap! I had a lot of hope for it as the idea and initial design are good (and it's free) but the implementation needs a lot of work! Back to KiCAD for me. |
Beta Was this translation helpful? Give feedback.
Thank you everyone for your suggestions. Realizing that the GPIO accesses are already atomic I took the advice and looked elsewhere. After rewriting the code (probably unnecessarily) to separate all the routines for each processor to no avail, I turned to the PCB.
As it turns out there was a random via under a part outline and thus not visible. Probably arrived while changing sides of a trace. Who knew a 20mil via could pass over an amp.
FYI ExpressPCB Plus is crap! I had a lot of hope for it as the idea and initial design are good (and it's free) but the implementation needs a lot of work! Back to KiCAD for me.
Thanks again, I learned a few things from the comments.