When using the Flash of BlueNRG-1/2 to store application data, you may encounter problems such as no Bluetooth signal or freezing.
Because the interrupt will be turned off for about 20 ms during the erasing process , the radio interrupt is turned off during the erasing period , the state of the entire radio is delayed or the state of the entire RF part is destroyed. Although ST 's SDK provides a demo for accessing Flash ( BLE_Beacon_FlashManagement ), when multiple Flash operations are performed, it is difficult to handle the mutual exclusion of Bluetooth events, especially for some applications with multiple connections or using master and slave at the same time, multiple Bluetooth events, mutual exclusion becomes more troublesome.
In order to optimize this problem, facilitate users to operate Flash, and prevent applications from considering mutually exclusive Bluetooth events, here is my approach for reference:
Use a bidirectional linked list to manage Flash operations ( Flash erase or Flash write, Flash read does not need to be mutually exclusive with BLE related events), and use a bidirectional linked list to manage idle time status. Perform Flash operations in idle time . From the datasheet, we can know that operating Flash , especially erasing, will take up a lot of time. In order to avoid operating Flash taking up too much time slices, each write or erase of Flash is managed with a list, and each write or erase operation is inserted at the end of the Flash operation list. In the main loop, check whether there is enough time to write. If the current idle time is sufficient, take out the first node in the Flash operation list, perform the Flash operation, and then remove the node.
As for how to detect idle time, a linked list is also used to manage idle time. First, during initialization, the radio active event needs to be set to be thrown to the application
aci_hal_set_radio_activity_mask(0x0001|0x0002|0x0004|0x0008|0x0010|0x0020);
after setting, rewrite the function aci_hal_end_of_radio_activity_event . When an event is triggered, it will enter this function, and then manage the idle linked list in this function.
As for when to write to Flash, there is a Flash operation scheduling function in the main loop , which continuously schedules whether there is data to be written. In the Flash operation scheduling function, there are two main things to do. One is to calculate the idle time and give it to the underlying Flash operation list scheduling. The other is to remove expired nodes. If the idle time is enough, a task is taken out from the Flash task list that needs to be operated to execute.
The actual code can be referred to the GitHub project: https://github.com/wallekuang/BlueNRG_Demo