How to catch (hook into) a soft-reset, to deinitialize a library/driver? #12850
-
Hi, Some drivers (User C Modules on esp32) need to be deinitialized before a soft-reset, like the esp32-camera and espnow, otherwise they (can) crash the system. I can't find any example on how to get a soft reset (or ctrl+D) to automatically trigger my deinit() function. Is this possible? I need it for this project. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
before calling edit, You can also pass function in sys.exit(deinit()):
note that there is also a machine.soft_reset() (Performs a soft reset of the interpreter, deleting all Python objects and resetting the Python heap) , but it do not accept any input parameters. read more: Reset and boot modes |
Beta Was this translation helpful? Give feedback.
-
May I suggest this. You will need to modify the 'main.c' file. As you are making a custom firmware, this will not be a problem for you. In 'main.c', add a single new line at line 153: soft_reset_exit:
int sret = pyexec_file_if_exists("sreset.py"); Now upload 'sreset.py' to the flash. As an example, 'sreset.py' might have the following lines: import time
print('doing soft reset')
print('deinit all the drivers here')
print('....')
print('wait a while')
time.sleep(3) Time to see if it works
It works! |
Beta Was this translation helpful? Give feedback.
Answering the question how do other drivers do this: For things like file handles, since the types are implemented in C, they can use a finalizer (assuming the port you are using has GC finalization enabled) to release the resources. There is a special GC function that is called in most ports that ensures that all finalizers are run on soft reset.
But if all of your code is written in Python, I'm not sure what could be done.