cancel
Showing results for 
Search instead for 
Did you mean: 

Flash, BLE, and C2

eleventen
Associate III

I'm working to perform flash operations while the BLE stack is running and there is an active connection. Once I initiate an erase operation, the connection times out with a supervisor timeout.

There seems to be an elaborate sequence of steps necessary to initiate a flash erase per the Cube code for pal_nvm:

  • HSEM acquire FLASH
  • Flash CR unlock
  • SHCI_C2_FLASH_EraseActivity( ERASE_ACTIVITY_ON);
  • Wait for suspend to be clear
  • Initiate the erase
  • Acknowledge erase (clear erase op)
  • SHCI_C2_FLASH_EraseActivity( ERASE_ACTIVITY_OFF);
  • Flash CR lock
  • HSEM release FLASH

What is the C2_FLASH_EraseActivity? Is there any explanation of what this means or does? All of the other steps are, for the most part, explained.

4 REPLIES 4
Remi QUINTIN
ST Employee

Extract from the header file

/**

 * SHCI_C2_FLASH_EraseActivity

 * @brief Provides the information of the start and the end of a flash erase window on the CPU1

 *

 * @param erase_activity: Start/End of erase activity

  * @retval Status

 */

 SHCI_CmdStatus_t SHCI_C2_FLASH_EraseActivity( SHCI_EraseActivity_t erase_activity );

The M4 core sends this command to warn the M0+ core about the fact that it may perform Flash page erase at any time.

Once the selected pages have been erased, the M4 core sends a new command notifying the completion of the Flash activity.

On the other side, the CM0+ core wakes up just before the radio activity to protect the RF timings and can also warns the CM4 core that it may start a Flash page erase. For sure it triggers a Flash write protection all the time the CM0+ core is needing a Flash access for its own data processing.

Regarding the loss of the BLE connection, I would recommend to erase the Flash page by page to let the CMO+, cote get access to the flash when it needs to secure the BLE data processing timings.

eleventen
Associate III

I have seen this text. It doesn't provide anything over what can be observed from the use of the function in the code. IOW, it doesn't explain how long this notification can be active. It doesn't explain how this notification affects the CM0. Does the BLE stack enter a hibernation mode? Can everything on the CM0 operate normally while this is active? What would happen if I left this active all of the time? As far as I'm concerned, the CM0 should not be erasing flash sectors, but there could be something hidden going on.

There is also no mention of how this notification interacts with the HSEM lock. Do I need to notify before acquiring the HSEM lock? After? Do I need to honor a timing barrier?

In my current code, I am erasing a single 4K block and the connection drops.

The pal_nvm.c code performs an erase like this. Note that there are multiple copies of this function. I'm showing the one that is enabled.

  

 263   while( LL_HSEM_1StepLock( HSEM, CFG_HW_FLASH_SEMID ) );
  264   HAL_FLASH_Unlock();
  265   __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_WRPERR | FLASH_FLAG_OPTVERR);
  266   
  267 //  SHCI_C2_FLASH_EraseActivity( ERASE_ACTIVITY_ON);
  268 
  269   while(LL_FLASH_IsActiveFlag_OperationSuspended());
  270   status = HAL_FLASHEx_Erase(&erase, &pageError);
  271   while(LL_FLASH_IsActiveFlag_OperationSuspended());
  272   
  273 //  SHCI_C2_FLASH_EraseActivity( ERASE_ACTIVITY_OFF);
  274 
  275   HAL_FLASH_Lock();
  276   LL_HSEM_ReleaseLock( HSEM, CFG_HW_FLASH_SEMID, 0 );

The erase activity flag code is disabled. There is another copy of the code that includes these invocations, but it is disabled by the preprocessor. There are two locations in the Cube where these calls are made. Both are disabled.

eleventen
Associate III

It is interesting to note that the Thread OTA demo does not erase flash (HAL_FLASHEx_Erase) while BLE is active. It erases memory only in Delete_Sectors which is only called when the application is initialized. The same is true for the BLE OTA sample, flash is erased when the application is initialized.

The Mesh application is a little more difficult to dissect. The factory reset function, which erases flash, seems to be called only on Mesh_Init. There is a function, AppliNvm_Process that handles flash erases and writes. It's difficult to say whether or not this code is invoked while a BLE connection is active.

From what I can tell, the only call into the ApplNvm code that erases flash, other than the factory reset, is AppliNvm_ClearModelState(). It is called during an unprovisioning call.

The point is that it isn't clear that there is a working example of performing flash write/erase while the BLE stack is active and connected. Even if this were an example, it would be better for developers to demonstrate something much simpler.

Remi QUINTIN
ST Employee

Hard to understand why erasing one single page could lead to the loss of the connection.

There are examples of Flash erase here: STM32Cube_FW_WB_V1.3.0\Projects\P-NUCLEO-WB55.Nucleo\Examples\FLASH\FLASH_EraseProgram

I got another example where the erase activity is mentioned to the C2 core. I don’t know why it is commented in the pal_ncm.c file.

void erase_page(void)

{  

 if(erasedFinished)

 {

   erasedFinished = 0;

   BSP_LED_On(LED_RED);

   static int loopCounter = 0;

   while( LL_HSEM_1StepLock( HSEM, CFG_HW_FLASH_SEMID ) );

   status = SHCI_C2_FLASH_EraseActivity(ERASE_ACTIVITY_ON);

   if (status != SHCI_Success)

     Error_Handler();

   HAL_FLASH_Unlock();

   while(LL_FLASH_IsActiveFlag_OperationSuspended());

   status = HAL_FLASHEx_Erase(&pEraseInit, &PageError);

   if (status != SHCI_Success)

     Error_Handler();

   HAL_FLASH_Lock();

   LL_HSEM_ReleaseLock( HSEM, CFG_HW_FLASH_SEMID, 0 );

   status = SHCI_C2_FLASH_EraseActivity(ERASE_ACTIVITY_OFF);  

   if (status != SHCI_Success)

     Error_Handler();

   loopCounter++;

   BSP_LED_Off(LED_RED);

   erasedFinished = 1;

 }

}