Skip to content

Commit 51614ce

Browse files
iabdalkaderdpgeorge
authored andcommitted
stm32/eth: Add low-power mode configuration option.
Add low power functionality configurable with: lan.config(low_power=True/False)
1 parent c4ed17f commit 51614ce

File tree

3 files changed

+56
-11
lines changed

3 files changed

+56
-11
lines changed

ports/stm32/eth.c

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#define PHY_BCR (0x0000)
4747
#define PHY_BCR_SOFT_RESET (0x8000)
4848
#define PHY_BCR_AUTONEG_EN (0x1000)
49+
#define PHY_BCR_POWER_DOWN (0x0800U)
4950

5051
#undef PHY_BSR
5152
#define PHY_BSR (0x0001)
@@ -188,17 +189,6 @@ STATIC uint32_t eth_phy_read(uint32_t reg) {
188189
void eth_init(eth_t *self, int mac_idx) {
189190
mp_hal_get_mac(mac_idx, &self->netif.hwaddr[0]);
190191
self->netif.hwaddr_len = 6;
191-
}
192-
193-
void eth_set_trace(eth_t *self, uint32_t value) {
194-
self->trace_flags = value;
195-
}
196-
197-
STATIC int eth_mac_init(eth_t *self) {
198-
// Configure MPU
199-
uint32_t irq_state = mpu_config_start();
200-
mpu_config_region(MPU_REGION_ETH, (uint32_t)&eth_dma, MPU_CONFIG_ETH(MPU_REGION_SIZE_16KB));
201-
mpu_config_end(irq_state);
202192

203193
// Configure GPIO
204194
mp_hal_pin_config_alt_static(MICROPY_HW_ETH_MDC, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, STATIC_AF_ETH_MDC);
@@ -211,6 +201,27 @@ STATIC int eth_mac_init(eth_t *self) {
211201
mp_hal_pin_config_alt_static(MICROPY_HW_ETH_RMII_TXD0, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, STATIC_AF_ETH_RMII_TXD0);
212202
mp_hal_pin_config_alt_static(MICROPY_HW_ETH_RMII_TXD1, MP_HAL_PIN_MODE_ALT, MP_HAL_PIN_PULL_NONE, STATIC_AF_ETH_RMII_TXD1);
213203

204+
// Enable peripheral clock
205+
#if defined(STM32H7)
206+
__HAL_RCC_ETH1MAC_CLK_ENABLE();
207+
__HAL_RCC_ETH1TX_CLK_ENABLE();
208+
__HAL_RCC_ETH1RX_CLK_ENABLE();
209+
#else
210+
__HAL_RCC_ETH_CLK_ENABLE();
211+
#endif
212+
}
213+
214+
void eth_set_trace(eth_t *self, uint32_t value) {
215+
self->trace_flags = value;
216+
}
217+
218+
STATIC int eth_mac_init(eth_t *self) {
219+
// Configure MPU
220+
uint32_t irq_state = mpu_config_start();
221+
mpu_config_region(MPU_REGION_ETH, (uint32_t)&eth_dma, MPU_CONFIG_ETH(MPU_REGION_SIZE_16KB));
222+
mpu_config_end(irq_state);
223+
224+
// Enable peripheral clock
214225
#if defined(STM32H7)
215226
__HAL_RCC_ETH1MAC_CLK_ENABLE();
216227
__HAL_RCC_ETH1TX_CLK_ENABLE();
@@ -791,6 +802,10 @@ int eth_link_status(eth_t *self) {
791802

792803
int eth_start(eth_t *self) {
793804
eth_lwip_deinit(self);
805+
806+
// Make sure Eth is Not in low power mode.
807+
eth_low_power_mode(self, false);
808+
794809
int ret = eth_mac_init(self);
795810
if (ret < 0) {
796811
return ret;
@@ -805,4 +820,29 @@ int eth_stop(eth_t *self) {
805820
return 0;
806821
}
807822

823+
void eth_low_power_mode(eth_t *self, bool enable) {
824+
(void)self;
825+
826+
// Enable eth clock
827+
#if defined(STM32H7)
828+
__HAL_RCC_ETH1MAC_CLK_ENABLE();
829+
#else
830+
__HAL_RCC_ETH_CLK_ENABLE();
831+
#endif
832+
833+
uint16_t bcr = eth_phy_read(PHY_BCR);
834+
if (enable) {
835+
// Enable low-power mode.
836+
eth_phy_write(PHY_BCR, bcr | PHY_BCR_POWER_DOWN);
837+
// Disable eth clock.
838+
#if defined(STM32H7)
839+
__HAL_RCC_ETH1MAC_CLK_DISABLE();
840+
#else
841+
__HAL_RCC_ETH_CLK_DISABLE();
842+
#endif
843+
} else {
844+
// Disable low-power mode.
845+
eth_phy_write(PHY_BCR, bcr & (~PHY_BCR_POWER_DOWN));
846+
}
847+
}
808848
#endif // defined(MICROPY_HW_ETH_MDC)

ports/stm32/eth.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,6 @@ struct netif *eth_netif(eth_t *self);
3535
int eth_link_status(eth_t *self);
3636
int eth_start(eth_t *self);
3737
int eth_stop(eth_t *self);
38+
void eth_low_power_mode(eth_t *self, bool enable);
3839

3940
#endif // MICROPY_INCLUDED_STM32_ETH_H

ports/stm32/network_lan.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ STATIC mp_obj_t network_lan_config(size_t n_args, const mp_obj_t *args, mp_map_t
134134
eth_set_trace(self->eth, mp_obj_get_int(e->value));
135135
break;
136136
}
137+
case MP_QSTR_low_power: {
138+
eth_low_power_mode(self->eth, mp_obj_get_int(e->value));
139+
break;
140+
}
137141
default:
138142
mp_raise_ValueError(MP_ERROR_TEXT("unknown config param"));
139143
}

0 commit comments

Comments
 (0)