46
46
#define PHY_BCR (0x0000)
47
47
#define PHY_BCR_SOFT_RESET (0x8000)
48
48
#define PHY_BCR_AUTONEG_EN (0x1000)
49
+ #define PHY_BCR_POWER_DOWN (0x0800U)
49
50
50
51
#undef PHY_BSR
51
52
#define PHY_BSR (0x0001)
@@ -188,17 +189,6 @@ STATIC uint32_t eth_phy_read(uint32_t reg) {
188
189
void eth_init (eth_t * self , int mac_idx ) {
189
190
mp_hal_get_mac (mac_idx , & self -> netif .hwaddr [0 ]);
190
191
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 );
202
192
203
193
// Configure GPIO
204
194
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) {
211
201
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 );
212
202
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 );
213
203
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
214
225
#if defined(STM32H7 )
215
226
__HAL_RCC_ETH1MAC_CLK_ENABLE ();
216
227
__HAL_RCC_ETH1TX_CLK_ENABLE ();
@@ -791,6 +802,10 @@ int eth_link_status(eth_t *self) {
791
802
792
803
int eth_start (eth_t * self ) {
793
804
eth_lwip_deinit (self );
805
+
806
+ // Make sure Eth is Not in low power mode.
807
+ eth_low_power_mode (self , false);
808
+
794
809
int ret = eth_mac_init (self );
795
810
if (ret < 0 ) {
796
811
return ret ;
@@ -805,4 +820,29 @@ int eth_stop(eth_t *self) {
805
820
return 0 ;
806
821
}
807
822
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
+ }
808
848
#endif // defined(MICROPY_HW_ETH_MDC)
0 commit comments