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

Supports auxiliary power control. #2774

Merged
merged 11 commits into from
Jul 25, 2024
13 changes: 10 additions & 3 deletions hal/inc/power_hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
#include <stddef.h>
#include "static_assert.h"

#define HAL_POWER_CONFIG_VERSION_0 0
#define HAL_POWER_CONFIG_VERSION_1 1

#define HAL_POWER_CONFIG_VERSION HAL_POWER_CONFIG_VERSION_1

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
Expand Down Expand Up @@ -61,9 +66,11 @@ typedef struct hal_power_config {
uint16_t charge_current; // charge current
uint16_t termination_voltage; // termination voltage
uint8_t soc_bits; // bits precision for SoC calculation (18 (default) or 19)
uint8_t reserved2;
uint32_t reserved3[4];
} hal_power_config;
uint8_t aux_pwr_ctrl_pin; // pin number for auxiliary power control
uint8_t aux_pwr_ctrl_pin_level; // active level for auxiliary power control
uint8_t reserved2[3];
uint32_t reserved3[3];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add ((packed)) just in case.

} __attribute__((packed)) hal_power_config;
static_assert(sizeof(hal_power_config) == 32, "hal_power_config size changed");
XuGuohui marked this conversation as resolved.
Show resolved Hide resolved

int hal_power_load_config(hal_power_config* conf, void* reserved);
Expand Down
1 change: 1 addition & 0 deletions hal/network/api/ifapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ typedef struct if_wiznet_pin_remap {

int if_init(void);
int if_init_platform(void*);
int if_init_platform_postpone(void*);

int if_get_list(struct if_list** ifs);
int if_free_list(struct if_list* ifs);
Expand Down
4 changes: 4 additions & 0 deletions hal/network/lwip/ifapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,10 @@ __attribute__((weak)) int if_init_platform(void*) {
return 0;
}

__attribute__((weak)) int if_init_platform_postpone(void*) {
return 0;
}

int if_get_list(struct if_list** ifs) {
if (ifs == nullptr) {
return -1;
Expand Down
1 change: 1 addition & 0 deletions hal/network/lwip/ifapi_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include "socket_hal_posix.h"
#include <lwip/netif.h>
#include <lwip/netifapi.h>

#define IF_T_DEFINED
typedef struct netif* if_t;
Expand Down
12 changes: 11 additions & 1 deletion hal/network/lwip/wiznet/wiznetif.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,13 @@ const unsigned int WIZNET_DEFAULT_RX_FRAMES_PER_ITERATION = PBUF_POOL_SIZE / 2;

WizNetif* WizNetif::instance_ = nullptr;

WizNetif::WizNetif(hal_spi_interface_t spi, hal_pin_t cs, hal_pin_t reset, hal_pin_t interrupt, const uint8_t mac[6])
WizNetif::WizNetif(hal_spi_interface_t spi, hal_pin_t cs, hal_pin_t reset, hal_pin_t interrupt, const uint8_t mac[6], bool postpone)
: BaseNetif(),
spi_(spi),
cs_(cs),
reset_(reset),
interrupt_(interrupt),
postpone_(postpone),
pwrState_(IF_POWER_STATE_NONE),
spiLock_(spi, WIZNET_DEFAULT_CONFIG) {

Expand Down Expand Up @@ -283,6 +284,9 @@ err_t WizNetif::initInterface() {
hwReset();
if (!isPresent()) {
pwrState_ = IF_POWER_STATE_DOWN;
if (postpone_) {
return ERR_OK;
}
return ERR_IF;
}
pwrState_ = IF_POWER_STATE_UP;
Expand All @@ -305,6 +309,12 @@ bool WizNetif::isPresent() {
return false;
}

auto pwr = pwrState_.load();
pwrState_ = IF_POWER_STATE_UP;
if (pwr != IF_POWER_STATE_UP) {
notifyPowerState(IF_POWER_STATE_UP);
}

return true;
}

Expand Down
5 changes: 3 additions & 2 deletions hal/network/lwip/wiznet/wiznetif.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,15 @@ namespace particle { namespace net {
class WizNetif : public BaseNetif {
public:
WizNetif() = delete;
WizNetif(hal_spi_interface_t spi, hal_pin_t cs, hal_pin_t reset, hal_pin_t interrupt, const uint8_t mac[6]);
WizNetif(hal_spi_interface_t spi, hal_pin_t cs, hal_pin_t reset, hal_pin_t interrupt, const uint8_t mac[6], bool postpone = false);
virtual ~WizNetif();

virtual int powerUp() override;
virtual int powerDown() override;

virtual int getPowerState(if_power_state_t* state) const override;
virtual int getNcpState(unsigned int* state) const override;
bool isPresent();

static WizNetif* instance() {
return instance_;
Expand All @@ -58,7 +59,6 @@ class WizNetif : public BaseNetif {
err_t initInterface();

void hwReset();
bool isPresent();

static void interruptCb(void* arg);
static void loop(void* arg);
Expand All @@ -83,6 +83,7 @@ class WizNetif : public BaseNetif {
hal_pin_t cs_;
hal_pin_t reset_;
hal_pin_t interrupt_;
bool postpone_;

std::atomic<if_power_state_t> pwrState_;

Expand Down
19 changes: 18 additions & 1 deletion hal/src/b5som/network/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ int if_init_platform(void*) {
wizNetifConfigData.size = sizeof(WizNetifConfigData);
wizNetifConfigData.version = WIZNETIF_CONFIG_DATA_VERSION;
WizNetifConfig::instance()->getConfigData(&wizNetifConfigData);
en2 = new WizNetif(HAL_SPI_INTERFACE1, wizNetifConfigData.cs_pin, wizNetifConfigData.reset_pin, wizNetifConfigData.int_pin, mac);
en2 = new WizNetif(HAL_SPI_INTERFACE1, wizNetifConfigData.cs_pin, wizNetifConfigData.reset_pin, wizNetifConfigData.int_pin, mac, true /* postpone initialization */);
}

uint8_t dummy;
Expand All @@ -152,6 +152,23 @@ int if_init_platform(void*) {
return 0;
}

int if_init_platform_postpone(void*) {
if (en2) {
uint8_t dummy;
if (!if_get_index(en2->interface(), &dummy)) {
auto wizNetif = reinterpret_cast<WizNetif*>(en2);
if (wizNetif->isPresent()) {
return 0;
}
netifapi_netif_remove(en2->interface());
XuGuohui marked this conversation as resolved.
Show resolved Hide resolved
}
LOG(INFO, "Ethernet interface is not present");
delete en2;
en2 = nullptr;
}
return 0;
}

extern "C" {

struct netif* lwip_hook_ip4_route_src(const ip4_addr_t* src, const ip4_addr_t* dst) {
Expand Down
25 changes: 24 additions & 1 deletion hal/src/boron/network/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,11 @@ int if_init_platform(void*) {
wizNetifConfigData.size = sizeof(WizNetifConfigData);
wizNetifConfigData.version = WIZNETIF_CONFIG_DATA_VERSION;
WizNetifConfig::instance()->getConfigData(&wizNetifConfigData);
en2 = new WizNetif(HAL_SPI_INTERFACE1, wizNetifConfigData.cs_pin, wizNetifConfigData.reset_pin, wizNetifConfigData.int_pin, mac);
bool postpone = false;
#if PLATFORM_ID == PLATFORM_BSOM
postpone = true; /* postpone initialization */
#endif
en2 = new WizNetif(HAL_SPI_INTERFACE1, wizNetifConfigData.cs_pin, wizNetifConfigData.reset_pin, wizNetifConfigData.int_pin, mac, postpone);
}

uint8_t dummy;
Expand All @@ -152,6 +156,25 @@ int if_init_platform(void*) {
return 0;
}

#if PLATFORM_ID == PLATFORM_BSOM
int if_init_platform_postpone(void*) {
if (en2) {
uint8_t dummy;
if (!if_get_index(en2->interface(), &dummy)) {
auto wizNetif = reinterpret_cast<WizNetif*>(en2);
if (wizNetif->isPresent()) {
return 0;
}
netifapi_netif_remove(en2->interface());
}
LOG(INFO, "Ethernet interface is not present");
delete en2;
en2 = nullptr;
}
return 0;
}
#endif

extern "C" {

struct netif* lwip_hook_ip4_route_src(const ip4_addr_t* src, const ip4_addr_t* dst) {
Expand Down
19 changes: 18 additions & 1 deletion hal/src/msom/network/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ int if_init_platform(void*) {
wizNetifConfigData.size = sizeof(WizNetifConfigData);
wizNetifConfigData.version = WIZNETIF_CONFIG_DATA_VERSION;
WizNetifConfig::instance()->getConfigData(&wizNetifConfigData);
en2 = new WizNetif(HAL_SPI_INTERFACE1, wizNetifConfigData.cs_pin, wizNetifConfigData.reset_pin, wizNetifConfigData.int_pin, mac);
en2 = new WizNetif(HAL_SPI_INTERFACE1, wizNetifConfigData.cs_pin, wizNetifConfigData.reset_pin, wizNetifConfigData.int_pin, mac, true /* postpone initialization */);
}

uint8_t dummy;
Expand Down Expand Up @@ -200,6 +200,23 @@ int if_init_platform(void*) {
return 0;
}

int if_init_platform_postpone(void*) {
if (en2) {
uint8_t dummy;
if (!if_get_index(en2->interface(), &dummy)) {
auto wizNetif = reinterpret_cast<WizNetif*>(en2);
if (wizNetif->isPresent()) {
return 0;
}
LOG(INFO, "Remove Ethernet interface");
netifapi_netif_remove(en2->interface());
}
delete en2;
en2 = nullptr;
}
return 0;
}

extern "C" {

struct netif* lwip_hook_ip4_route_src(const ip4_addr_t* src, const ip4_addr_t* dst) {
Expand Down
1 change: 1 addition & 0 deletions system/inc/system_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ enum SystemEvents {
out_of_memory = 1<<18, // heap request was not satisfied
ble_prov_mode = 1<<19, // ble provisioning mode
firmware_update_status = 1<<20, // firmware update status changed
aux_power_state = 1<<21, // Auxiliary power status changed

all_events = 0xFFFFFFFFFFFFFFFF
};
Expand Down
18 changes: 16 additions & 2 deletions system/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,14 @@ RunTimeInfoDiagnosticData g_usedRamDiagData(DIAG_ID_SYSTEM_USED_RAM, DIAG_NAME_S
}
);

#if HAL_PLATFORM_LWIP
void if_init_postpone(system_event_t event, int param, void* pointer, void* context) {
if (event == aux_power_state) {
if_init_platform_postpone(nullptr);
}
}
#endif /* HAL_PLATFORM_LWIP */

} // namespace

/*******************************************************************************
Expand All @@ -735,6 +743,11 @@ void app_setup_and_loop(void)
}
#endif // HAL_PLATFORM_ASSETS

// Reset all persistent settings to factory defaults if necessary
// This should be called before system_part2_post_init() so that the
// configurations set in STARTUP() is not reset by factory reset
resetSettingsToFactoryDefaultsIfNeeded();
XuGuohui marked this conversation as resolved.
Show resolved Hide resolved

// NOTE: this calls user app global constructors
system_part2_post_init();

Expand All @@ -745,8 +758,9 @@ void app_setup_and_loop(void)

LED_SIGNAL_START(NETWORK_OFF, BACKGROUND);

// Reset all persistent settings to factory defaults if necessary
resetSettingsToFactoryDefaultsIfNeeded();
#if HAL_PLATFORM_LWIP
system_subscribe_event(aux_power_state, if_init_postpone, nullptr);
#endif /* HAL_PLATFORM_LWIP */

system_power_management_init();

Expand Down
Loading