Skip to content

Commit

Permalink
[Silabs] Add OTA support for series3 (project-chip#37055)
Browse files Browse the repository at this point in the history
* Change freertos include used to fix the macro not found

* Add OTA Support for series3
  • Loading branch information
jmartinez-silabs authored Jan 14, 2025
1 parent 8a0fc4d commit a5ebda6
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 18 deletions.
1 change: 1 addition & 0 deletions examples/platform/silabs/SoftwareFaultReports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/

#include "SoftwareFaultReports.h"
#include "FreeRTOSConfig.h"
#include "silabs_utils.h"
#include <app/clusters/software-diagnostics-server/software-diagnostics-server.h>
#include <app/util/attribute-storage.h>
Expand Down
4 changes: 0 additions & 4 deletions src/platform/silabs/CHIPDevicePlatformConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,7 @@
#endif // CHIP_DEVICE_CONFIG_CHIP_TASK_STACK_SIZE

#ifndef CHIP_DEVICE_CONFIG_THREAD_TASK_STACK_SIZE
#if defined(EFR32MG21)
#define CHIP_DEVICE_CONFIG_THREAD_TASK_STACK_SIZE (2 * 1024)
#else
#define CHIP_DEVICE_CONFIG_THREAD_TASK_STACK_SIZE (8 * 1024)
#endif
#endif // CHIP_DEVICE_CONFIG_THREAD_TASK_STACK_SIZE

#define CHIP_DEVICE_CONFIG_ENABLE_WIFI_TELEMETRY 0
Expand Down
2 changes: 2 additions & 0 deletions src/platform/silabs/OTAImageProcessorImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ class OTAImageProcessorImpl : public OTAImageProcessorInterface
static void HandleApply(intptr_t context);
static void HandleAbort(intptr_t context);
static void HandleProcessBlock(intptr_t context);
static void LockRadioProcessing();
static void UnlockRadioProcessing();
CHIP_ERROR ProcessHeader(ByteSpan & block);

/**
Expand Down
76 changes: 62 additions & 14 deletions src/platform/silabs/efr32/OTAImageProcessorImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,22 @@ extern "C" {
#include "sl_core.h"
}

#include <platform/silabs/SilabsConfig.h>

#ifdef _SILICON_LABS_32B_SERIES_2
// Series 2 bootloader_ api calls must be called from a critical section context for thread safeness
#define WRAP_BL_DFU_CALL(code) \
{ \
CORE_CRITICAL_SECTION(code;) \
}
#else
// series 3 bootloader_ calls uses rtos mutex for thread safety. Cannot be called within a critical section
#define WRAP_BL_DFU_CALL(code) \
{ \
code; \
}
#endif

/// No error, operation OK
#define SL_BOOTLOADER_OK 0L

Expand Down Expand Up @@ -144,7 +160,12 @@ void OTAImageProcessorImpl::HandlePrepareDownload(intptr_t context)

ChipLogProgress(SoftwareUpdate, "HandlePrepareDownload: started");

CORE_CRITICAL_SECTION(bootloader_init();)
WRAP_BL_DFU_CALL(err = bootloader_init())
if (err != SL_BOOTLOADER_OK)
{
ChipLogProgress(SoftwareUpdate, "bootloader_init Failed error: %ld", err);
}

mSlotId = 0; // Single slot until we support multiple images
writeBufOffset = 0;
mWriteOffset = 0;
Expand Down Expand Up @@ -186,7 +207,8 @@ void OTAImageProcessorImpl::HandleFinalize(intptr_t context)
return;
}
#endif // SL_BTLCTRL_MUX
CORE_CRITICAL_SECTION(err = bootloader_eraseWriteStorage(mSlotId, mWriteOffset, writeBuffer, kAlignmentBytes);)
WRAP_BL_DFU_CALL(err = bootloader_eraseWriteStorage(mSlotId, mWriteOffset, writeBuffer, kAlignmentBytes))

#if SL_BTLCTRL_MUX
err = sl_wfx_host_post_bootloader_spi_transfer();
if (err != SL_STATUS_OK)
Expand All @@ -208,11 +230,27 @@ void OTAImageProcessorImpl::HandleFinalize(intptr_t context)
ChipLogProgress(SoftwareUpdate, "OTA image downloaded successfully");
}

// TODO: SE access is not thread safe. It asserts if other tasks accesses it during bootloader_verifyImage or
// bootloader_setImageToBootload steps - MATTER-4155 - PLATFORM_HYD-3235
void OTAImageProcessorImpl::LockRadioProcessing()
{
#if !SL_WIFI && defined(_SILICON_LABS_32B_SERIES_3)
DeviceLayer::ThreadStackMgr().LockThreadStack();
#endif // SL_WIFI
}

void OTAImageProcessorImpl::UnlockRadioProcessing()
{
#if !SL_WIFI && defined(_SILICON_LABS_32B_SERIES_3)
DeviceLayer::ThreadStackMgr().UnlockThreadStack();
#endif // SL_WIFI
}

void OTAImageProcessorImpl::HandleApply(intptr_t context)
{
uint32_t err = SL_BOOTLOADER_OK;

ChipLogProgress(SoftwareUpdate, "HandleApply: started");
ChipLogProgress(SoftwareUpdate, "HandleApply: verifying image");

// Force KVS to store pending keys such as data from StoreCurrentUpdateInfo()
chip::DeviceLayer::PersistedStorage::KeyValueStoreMgrImpl().ForceKeyMapSave();
Expand All @@ -224,11 +262,13 @@ void OTAImageProcessorImpl::HandleApply(intptr_t context)
return;
}
#endif // SL_BTLCTRL_MUX
#if defined(SL_TRUSTZONE_NONSECURE)
CORE_CRITICAL_SECTION(err = bootloader_verifyImage(mSlotId);)
#else
CORE_CRITICAL_SECTION(err = bootloader_verifyImage(mSlotId, NULL);)
#endif

#if defined(_SILICON_LABS_32B_SERIES_3) && CHIP_PROGRESS_LOGGING
osDelay(100); // sl-temp: delay for uart print before verifyImage
#endif // _SILICON_LABS_32B_SERIES_3 && CHIP_PROGRESS_LOGGING
LockRadioProcessing();
WRAP_BL_DFU_CALL(err = bootloader_verifyImage(mSlotId, NULL))
UnlockRadioProcessing();
if (err != SL_BOOTLOADER_OK)
{
ChipLogError(SoftwareUpdate, "bootloader_verifyImage() error: %ld", err);
Expand All @@ -239,13 +279,14 @@ void OTAImageProcessorImpl::HandleApply(intptr_t context)
if (err != SL_STATUS_OK)
{
ChipLogError(SoftwareUpdate, "sl_wfx_host_post_bootloader_spi_transfer() error: %ld", err);
return;
}
#endif // SL_BTLCTRL_MUX
return;
}

CORE_CRITICAL_SECTION(err = bootloader_setImageToBootload(mSlotId);)
ChipLogProgress(SoftwareUpdate, "Image verified, Set image to bootload");
LockRadioProcessing();
WRAP_BL_DFU_CALL(err = bootloader_setImageToBootload(mSlotId))
UnlockRadioProcessing();
if (err != SL_BOOTLOADER_OK)
{
ChipLogError(SoftwareUpdate, "bootloader_setImageToBootload() error: %ld", err);
Expand All @@ -256,7 +297,6 @@ void OTAImageProcessorImpl::HandleApply(intptr_t context)
if (err != SL_STATUS_OK)
{
ChipLogError(SoftwareUpdate, "sl_wfx_host_post_bootloader_spi_transfer() error: %ld", err);
return;
}
#endif // SL_BTLCTRL_MUX
return;
Expand All @@ -270,8 +310,15 @@ void OTAImageProcessorImpl::HandleApply(intptr_t context)
return;
}
#endif // SL_BTLCTRL_MUX

ChipLogProgress(SoftwareUpdate, "Reboot and install new image...");
#if defined(_SILICON_LABS_32B_SERIES_3) && CHIP_PROGRESS_LOGGING
osDelay(100); // sl-temp: delay for uart print before reboot
#endif // _SILICON_LABS_32B_SERIES_3 && CHIP_PROGRESS_LOGGING
LockRadioProcessing();
// This reboots the device
CORE_CRITICAL_SECTION(bootloader_rebootAndInstall();)
WRAP_BL_DFU_CALL(bootloader_rebootAndInstall())
UnlockRadioProcessing(); // Unneccessay but for good measure
}

void OTAImageProcessorImpl::HandleAbort(intptr_t context)
Expand Down Expand Up @@ -330,7 +377,8 @@ void OTAImageProcessorImpl::HandleProcessBlock(intptr_t context)
return;
}
#endif // SL_BTLCTRL_MUX
CORE_CRITICAL_SECTION(err = bootloader_eraseWriteStorage(mSlotId, mWriteOffset, writeBuffer, kAlignmentBytes);)
WRAP_BL_DFU_CALL(err = bootloader_eraseWriteStorage(mSlotId, mWriteOffset, writeBuffer, kAlignmentBytes))

#if SL_BTLCTRL_MUX
err = sl_wfx_host_post_bootloader_spi_transfer();
if (err != SL_STATUS_OK)
Expand Down

0 comments on commit a5ebda6

Please sign in to comment.