-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Pin manipulation lock board when number is equal/above NUM_DIGITAL_PINS #2612
Comments
Hi @VitorBoss |
Yes, i'm using pin numbers as they are stored in eeprom.
The check still persists on |
Well, I agree with you anyway, some libraries requires GPIO to be as fast as possible and adding this check will probably change the behavior. |
The code is a RT engine management, time isn't that critical but the amount of necessary changes is. |
Why not. Anyway even if it is a RT engine, I don't understand how you reach this issue as pins are known and fixed so how you reach this? Moreover simply check in user app if pin less than NUM_DIGITAL_PINS easily fix the issue. |
I think you didn't understood the problem, I'll try to explain better. Currently all peripherals are in pin numbers range, that means I need to deal with user input, lets say the function is assigned to pin 20, this is USB D+ pin on Black F407VE, I need to change the value so the board don't stop communicating. Since the pin numbers start from 0 and not all supported boards have the same pin numbers/capacity I need to set the value to an invalid number to avoid locking/bricking another peripheral/device. I can't always check the pin number as some functions uses bit banging. I accept suggestions on how to deal with this. |
Just disassembled the binary, the function digitalRead() takes 84 cycles without the change.
This take 100, this is extra 95.2ns at 168MHz(if my math is correct) |
Based only on the cycles it adds 19% which is not neglectable.
So adding this check will also impact the bit banging AFAIU. I admit I don't understand your use case. Have you a simple sketch demonstrating it? |
This is current code assembly:
With a valid pin number the code take 22 clock cycles to finalize, with analog number it take 23 clock cycles. This is with change:
With a valid pin number the code take 23 clock cycles to finalize, with analog number it take 32 clock cycles. As you can see the speed difference is just a single clock cycle with valid pin number below board maximum, inside the analog range the hit is 9 clock cycles, this isn't nothing to sneeze at, but as you can see the protection don't increase the whole 16 instructions it grow. |
Hi @VitorBoss Please, would you be so kind to create a PR for this? And I've checked some libraries using GPIO with precise timing and they used LL or *Fast api. |
Additional context
I have a quite complex code in hands, since the core don't protect used peripherals(like USB) pins I had to code my own.
I have pins that can be reassign, and need to check is it isn't USB/SPI/UART, when I detect a conflict I set the pin to NUM_DIGITAL_PINS but this is causing the CPU to lock, and the weird thing is, this don't happen while debugging!
After entering the rabbit hole of stepping the code via STM32CubeProgrammer I found the problem is in the
digitalRead
, since it don't check if the pin number is valid it causes an exception when theget_GPIO_Port(STM_PORT(pn)
part returns aNULL
and theLL_GPIO_IsInputPinSet
tries to read a NULL offset.The code also uses EEPROM emulation using internal flash, even when the NULL pointer access don't lock the CPU it causes the flash to stop writing.
The fix is pretty simple, I have tested it right now. Just add a check on the digital manipulation on
wiring_digital.c
like thisThis is how
pinMode
do it, and the Teensy core also do this check.The text was updated successfully, but these errors were encountered: