Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[nrfconnect] Optimized external flash power consumption. #16219

Merged
merged 1 commit into from
Mar 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions examples/all-clusters-app/nrfconnect/main/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,9 @@ CHIP_ERROR AppTask::Init()
void AppTask::InitOTARequestor()
{
#if CONFIG_CHIP_OTA_REQUESTOR
mOTAImageProcessor.SetOTADownloader(&mBDXDownloader);
mBDXDownloader.SetImageProcessorDelegate(&mOTAImageProcessor);
mOTARequestorDriver.Init(&mOTARequestor, &mOTAImageProcessor);
OTAImageProcessorNrf::Get().SetOTADownloader(&mBDXDownloader);
mBDXDownloader.SetImageProcessorDelegate(&OTAImageProcessorNrf::Get());
mOTARequestorDriver.Init(&mOTARequestor, &OTAImageProcessorNrf::Get());
mOTARequestorStorage.Init(chip::Server::GetInstance().GetPersistentStorage());
mOTARequestor.Init(chip::Server::GetInstance(), mOTARequestorStorage, mOTARequestorDriver, mBDXDownloader);
chip::SetRequestorInstance(&mOTARequestor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
#include <platform/CHIPDeviceLayer.h>

#if CONFIG_CHIP_OTA_REQUESTOR
#include "OTAUtil.h"
#include <app/clusters/ota-requestor/BDXDownloader.h>
#include <app/clusters/ota-requestor/DefaultOTARequestorStorage.h>
#include <app/clusters/ota-requestor/GenericOTARequestorDriver.h>
#include <app/clusters/ota-requestor/OTARequestor.h>
#include <platform/nrfconnect/OTAImageProcessorImpl.h>
#endif

struct k_timer;
Expand Down Expand Up @@ -76,7 +76,6 @@ class AppTask
#if CONFIG_CHIP_OTA_REQUESTOR
chip::DefaultOTARequestorStorage mOTARequestorStorage;
chip::DeviceLayer::GenericOTARequestorDriver mOTARequestorDriver;
chip::DeviceLayer::OTAImageProcessorImpl mOTAImageProcessor;
chip::BDXDownloader mBDXDownloader;
chip::OTARequestor mOTARequestor;
#endif
Expand Down
9 changes: 4 additions & 5 deletions examples/lighting-app/nrfconnect/main/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@
#include <system/SystemClock.h>

#if CONFIG_CHIP_OTA_REQUESTOR
#include "OTAUtil.h"
#include <app/clusters/ota-requestor/BDXDownloader.h>
#include <app/clusters/ota-requestor/DefaultOTARequestorStorage.h>
#include <app/clusters/ota-requestor/GenericOTARequestorDriver.h>
#include <app/clusters/ota-requestor/OTARequestor.h>
#include <platform/nrfconnect/OTAImageProcessorImpl.h>
#endif

#include <dk_buttons_and_leds.h>
Expand Down Expand Up @@ -88,7 +88,6 @@ bool sHaveBLEConnections = false;
#if CONFIG_CHIP_OTA_REQUESTOR
DefaultOTARequestorStorage sRequestorStorage;
GenericOTARequestorDriver sOTARequestorDriver;
OTAImageProcessorImpl sOTAImageProcessor;
chip::BDXDownloader sBDXDownloader;
chip::OTARequestor sOTARequestor;
#endif
Expand Down Expand Up @@ -202,9 +201,9 @@ CHIP_ERROR AppTask::Init()
void AppTask::InitOTARequestor()
{
#if CONFIG_CHIP_OTA_REQUESTOR
sOTAImageProcessor.SetOTADownloader(&sBDXDownloader);
sBDXDownloader.SetImageProcessorDelegate(&sOTAImageProcessor);
sOTARequestorDriver.Init(&sOTARequestor, &sOTAImageProcessor);
OTAImageProcessorNrf::Get().SetOTADownloader(&sBDXDownloader);
sBDXDownloader.SetImageProcessorDelegate(&OTAImageProcessorNrf::Get());
sOTARequestorDriver.Init(&sOTARequestor, &OTAImageProcessorNrf::Get());
sRequestorStorage.Init(Server::GetInstance().GetPersistentStorage());
sOTARequestor.Init(Server::GetInstance(), sRequestorStorage, sOTARequestorDriver, sBDXDownloader);
chip::SetRequestorInstance(&sOTARequestor);
Expand Down
9 changes: 4 additions & 5 deletions examples/lock-app/nrfconnect/main/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@
#include <system/SystemClock.h>

#if CONFIG_CHIP_OTA_REQUESTOR
#include "OTAUtil.h"
#include <app/clusters/ota-requestor/BDXDownloader.h>
#include <app/clusters/ota-requestor/DefaultOTARequestorStorage.h>
#include <app/clusters/ota-requestor/GenericOTARequestorDriver.h>
#include <app/clusters/ota-requestor/OTARequestor.h>
#include <platform/nrfconnect/OTAImageProcessorImpl.h>
#endif

#include <dk_buttons_and_leds.h>
Expand Down Expand Up @@ -77,7 +77,6 @@ bool sHaveBLEConnections = false;
#if CONFIG_CHIP_OTA_REQUESTOR
DefaultOTARequestorStorage sRequestorStorage;
GenericOTARequestorDriver sOTARequestorDriver;
OTAImageProcessorImpl sOTAImageProcessor;
chip::BDXDownloader sBDXDownloader;
chip::OTARequestor sOTARequestor;
#endif
Expand Down Expand Up @@ -183,9 +182,9 @@ CHIP_ERROR AppTask::Init()
void AppTask::InitOTARequestor()
{
#if CONFIG_CHIP_OTA_REQUESTOR
sOTAImageProcessor.SetOTADownloader(&sBDXDownloader);
sBDXDownloader.SetImageProcessorDelegate(&sOTAImageProcessor);
sOTARequestorDriver.Init(&sOTARequestor, &sOTAImageProcessor);
OTAImageProcessorNrf::Get().SetOTADownloader(&sBDXDownloader);
sBDXDownloader.SetImageProcessorDelegate(&OTAImageProcessorNrf::Get());
sOTARequestorDriver.Init(&sOTARequestor, &OTAImageProcessorNrf::Get());
sRequestorStorage.Init(Server::GetInstance().GetPersistentStorage());
sOTARequestor.Init(Server::GetInstance(), sRequestorStorage, sOTARequestorDriver, sBDXDownloader);
chip::SetRequestorInstance(&sOTARequestor);
Expand Down
37 changes: 37 additions & 0 deletions examples/platform/nrfconnect/util/include/OTAUtil.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2022 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#include <platform/nrfconnect/OTAImageProcessorImpl.h>

#ifdef CONFIG_CHIP_OTA_REQUESTOR
namespace OTAImageProcessorNrf {
// compile-time factory method
inline chip::DeviceLayer::OTAImageProcessorImpl & Get()
{
#if CONFIG_PM_DEVICE && CONFIG_NORDIC_QSPI_NOR
static chip::DeviceLayer::ExtFlashHandler sQSPIHandler;
static chip::DeviceLayer::OTAImageProcessorImplPMDevice sOTAImageProcessor{ sQSPIHandler };
#else
static chip::DeviceLayer::OTAImageProcessorImpl sOTAImageProcessor;
#endif
return sOTAImageProcessor;
}
} // namespace OTAImageProcessorNrf

#endif
9 changes: 4 additions & 5 deletions examples/pump-app/nrfconnect/main/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@
#include <system/SystemClock.h>

#if CONFIG_CHIP_OTA_REQUESTOR
#include "OTAUtil.h"
#include <app/clusters/ota-requestor/BDXDownloader.h>
#include <app/clusters/ota-requestor/DefaultOTARequestorStorage.h>
#include <app/clusters/ota-requestor/GenericOTARequestorDriver.h>
#include <app/clusters/ota-requestor/OTARequestor.h>
#include <platform/nrfconnect/OTAImageProcessorImpl.h>
#endif

#include <dk_buttons_and_leds.h>
Expand Down Expand Up @@ -80,7 +80,6 @@ bool sHaveBLEConnections = false;
#if CONFIG_CHIP_OTA_REQUESTOR
DefaultOTARequestorStorage sRequestorStorage;
GenericOTARequestorDriver sOTARequestorDriver;
OTAImageProcessorImpl sOTAImageProcessor;
chip::BDXDownloader sBDXDownloader;
chip::OTARequestor sOTARequestor;
#endif
Expand Down Expand Up @@ -180,9 +179,9 @@ CHIP_ERROR AppTask::Init()
void AppTask::InitOTARequestor()
{
#if CONFIG_CHIP_OTA_REQUESTOR
sOTAImageProcessor.SetOTADownloader(&sBDXDownloader);
sBDXDownloader.SetImageProcessorDelegate(&sOTAImageProcessor);
sOTARequestorDriver.Init(&sOTARequestor, &sOTAImageProcessor);
OTAImageProcessorNrf::Get().SetOTADownloader(&sBDXDownloader);
sBDXDownloader.SetImageProcessorDelegate(&OTAImageProcessorNrf::Get());
sOTARequestorDriver.Init(&sOTARequestor, &OTAImageProcessorNrf::Get());
sRequestorStorage.Init(chip::Server::GetInstance().GetPersistentStorage());
sOTARequestor.Init(chip::Server::GetInstance(), sRequestorStorage, sOTARequestorDriver, sBDXDownloader);
chip::SetRequestorInstance(&sOTARequestor);
Expand Down
9 changes: 4 additions & 5 deletions examples/pump-controller-app/nrfconnect/main/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@
#include <system/SystemClock.h>

#if CONFIG_CHIP_OTA_REQUESTOR
#include "OTAUtil.h"
#include <app/clusters/ota-requestor/BDXDownloader.h>
#include <app/clusters/ota-requestor/DefaultOTARequestorStorage.h>
#include <app/clusters/ota-requestor/GenericOTARequestorDriver.h>
#include <app/clusters/ota-requestor/OTARequestor.h>
#include <platform/nrfconnect/OTAImageProcessorImpl.h>
#endif

#include <dk_buttons_and_leds.h>
Expand Down Expand Up @@ -77,7 +77,6 @@ bool sHaveBLEConnections = false;
#if CONFIG_CHIP_OTA_REQUESTOR
DefaultOTARequestorStorage sRequestorStorage;
GenericOTARequestorDriver sOTARequestorDriver;
OTAImageProcessorImpl sOTAImageProcessor;
chip::BDXDownloader sBDXDownloader;
chip::OTARequestor sOTARequestor;
#endif
Expand Down Expand Up @@ -177,9 +176,9 @@ CHIP_ERROR AppTask::Init()
void AppTask::InitOTARequestor()
{
#if CONFIG_CHIP_OTA_REQUESTOR
sOTAImageProcessor.SetOTADownloader(&sBDXDownloader);
sBDXDownloader.SetImageProcessorDelegate(&sOTAImageProcessor);
sOTARequestorDriver.Init(&sOTARequestor, &sOTAImageProcessor);
OTAImageProcessorNrf::Get().SetOTADownloader(&sBDXDownloader);
sBDXDownloader.SetImageProcessorDelegate(&OTAImageProcessorNrf::Get());
sOTARequestorDriver.Init(&sOTARequestor, &OTAImageProcessorNrf::Get());
sRequestorStorage.Init(chip::Server::GetInstance().GetPersistentStorage());
sOTARequestor.Init(chip::Server::GetInstance(), sRequestorStorage, sOTARequestorDriver, sBDXDownloader);
chip::SetRequestorInstance(&sOTARequestor);
Expand Down
40 changes: 40 additions & 0 deletions src/platform/nrfconnect/OTAImageProcessorImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <dfu/dfu_target.h>
#include <dfu/dfu_target_mcuboot.h>
#include <dfu/mcuboot.h>
#include <pm/device.h>
#include <sys/reboot.h>

namespace chip {
Expand Down Expand Up @@ -141,5 +142,44 @@ CHIP_ERROR OTAImageProcessorImpl::ProcessHeader(ByteSpan & block)
return CHIP_NO_ERROR;
}

// external flash power consumption optimization
void ExtFlashHandler::DoAction(Action action)
{
#if CONFIG_PM_DEVICE && CONFIG_NORDIC_QSPI_NOR && !CONFIG_SOC_NRF52840 // nRF52 is optimized per default
// utilize the QSPI driver sleep power mode
const auto * qspi_dev = device_get_binding(DT_LABEL(DT_INST(0, nordic_qspi_nor)));
if (qspi_dev)
{
const auto requestedAction = Action::WAKE_UP == action ? PM_DEVICE_ACTION_RESUME : PM_DEVICE_ACTION_SUSPEND;
(void) pm_device_action_run(qspi_dev, requestedAction); // not much can be done in case of a failure
}
#endif
}

OTAImageProcessorImplPMDevice::OTAImageProcessorImplPMDevice(ExtFlashHandler & aHandler) : mHandler(aHandler)
{
mHandler.DoAction(ExtFlashHandler::Action::SLEEP);
}

CHIP_ERROR OTAImageProcessorImplPMDevice::PrepareDownload()
{
mHandler.DoAction(ExtFlashHandler::Action::WAKE_UP);
return OTAImageProcessorImpl::PrepareDownload();
}

CHIP_ERROR OTAImageProcessorImplPMDevice::Abort()
{
auto status = OTAImageProcessorImpl::Abort();
mHandler.DoAction(ExtFlashHandler::Action::SLEEP);
return status;
}

CHIP_ERROR OTAImageProcessorImplPMDevice::Apply()
{
auto status = OTAImageProcessorImpl::Apply();
mHandler.DoAction(ExtFlashHandler::Action::SLEEP);
return status;
}

} // namespace DeviceLayer
} // namespace chip
25 changes: 24 additions & 1 deletion src/platform/nrfconnect/OTAImageProcessorImpl.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/*
*
* Copyright (c) 2021 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -53,5 +52,29 @@ class OTAImageProcessorImpl : public OTAImageProcessorInterface
uint8_t mBuffer[kBufferSize];
};

class ExtFlashHandler
{
public:
enum class Action : uint8_t
{
WAKE_UP,
SLEEP
};
virtual ~ExtFlashHandler() {}
virtual void DoAction(Action action);
};

class OTAImageProcessorImplPMDevice : public OTAImageProcessorImpl
{
public:
explicit OTAImageProcessorImplPMDevice(ExtFlashHandler & aHandler);
CHIP_ERROR PrepareDownload() override;
CHIP_ERROR Abort() override;
CHIP_ERROR Apply() override;

private:
ExtFlashHandler & mHandler;
};

} // namespace DeviceLayer
} // namespace chip