Fix declaring OSPIFBlockDevice and QSPIFBlockDevice as globals, fix some STM32U585 issues #167
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary of changes
@tim35ca on the forums pointed out that declaring an OSPIFBlockDevice as a global object can cause it to not work correctly. This is because the author of OSPIFBlockDevice made a common mistake: interacting with global variables in an object constructor, without realizing that those global variables might not yet have been initialized. The C++ environment is free to initialize globals in any order it wants, so depending on your luck for the day and the direction of the wind, the globals in OSPIFBlockDevice.cpp may or may not have been initialized by the time your globally declared block device instance gets constructed.
If they aren't, you will likely get a HardFault due to accessing a null
_active_ospif_flash_csel_arr
pointer.In this PR, I changed the code around a bit so that the global variables (the mutex and the csel array) are now stored as local static variables in functions. This gives them a well-defined init order -- they will always be initialized the first time the function is called. I also cleaned up the code a bit (hello ScopedMutexLock, goodbye
goto
) and fixed an issue where OSPIFBlockDevice wasn't available on my test target due to an error in targets.json.Also applied fixes for some other issues that @TIM35 was running into with the U585 board, including:
Update: Since QSPIFBlockDevice was found to have the same problem, I also applied this fix there.
Impact of changes
OSPIFBlockDevice should no longer hardfault when declared globally.
Migration actions required
None
Documentation
None
Pull request type
Test results
Reviewers