Skip to content

Commit 4b52003

Browse files
robert-hhdpgeorge
authored andcommitted
esp32/network_lan: Support configuration of ETH ref_clk pin.
Both the direction and the Pin used for ref_clk can now be configured. It Requires at least idf v4.4. The new keyword arguments to the constructor are: - ref_clk_mode=mode: with mode being Pin.IN or Pin.OUT. If it is not set, then the default configuration is used, which may be configured by kconfig settings. - ref_clk=pin_obj: which defines the Pin used for ref_clk. This is either Pin(0), Pin(16) or Pin(17). No check is done for the pin number. If it is the wrong one, it simply will not work. Besides that, no harm.
1 parent efb4bd3 commit 4b52003

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

Diff for: ports/esp32/network_lan.c

+18-1
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,20 @@ STATIC mp_obj_t get_lan(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_ar
9797
return MP_OBJ_FROM_PTR(&lan_obj);
9898
}
9999

100-
enum { ARG_id, ARG_mdc, ARG_mdio, ARG_power, ARG_phy_addr, ARG_phy_type };
100+
enum { ARG_id, ARG_mdc, ARG_mdio, ARG_power, ARG_phy_addr, ARG_phy_type,
101+
ARG_ref_clk_mode, ARG_ref_clk };
101102
static const mp_arg_t allowed_args[] = {
102103
{ MP_QSTR_id, MP_ARG_OBJ, {.u_obj = mp_const_none} },
103104
{ MP_QSTR_mdc, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_OBJ },
104105
{ MP_QSTR_mdio, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_OBJ },
105106
{ MP_QSTR_power, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
106107
{ MP_QSTR_phy_addr, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_INT },
107108
{ MP_QSTR_phy_type, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_INT },
109+
// Dynamic ref_clk configuration available at v4.4
110+
#if ESP_IDF_VERSION_MINOR >= 4
111+
{ MP_QSTR_ref_clk_mode, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
112+
{ MP_QSTR_ref_clk, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
113+
#endif
108114
};
109115

110116
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
@@ -139,6 +145,17 @@ STATIC mp_obj_t get_lan(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_ar
139145
eth_mac_config_t mac_config = ETH_MAC_DEFAULT_CONFIG();
140146
mac_config.smi_mdc_gpio_num = self->mdc_pin;
141147
mac_config.smi_mdio_gpio_num = self->mdio_pin;
148+
// Dynamic ref_clk configuration available at v4.4
149+
#if ESP_IDF_VERSION_MINOR >= 4
150+
if (args[ARG_ref_clk_mode].u_int != -1) {
151+
// Map the GPIO_MODE constants to EMAC_CLK constants.
152+
mac_config.clock_config.rmii.clock_mode =
153+
args[ARG_ref_clk_mode].u_int == GPIO_MODE_INPUT ? EMAC_CLK_EXT_IN : EMAC_CLK_OUT;
154+
}
155+
if (args[ARG_ref_clk].u_obj != mp_const_none) {
156+
mac_config.clock_config.rmii.clock_gpio = machine_pin_get_id(args[ARG_ref_clk].u_obj);
157+
}
158+
#endif
142159
esp_eth_mac_t *mac = esp_eth_mac_new_esp32(&mac_config);
143160

144161
eth_phy_config_t phy_config = ETH_PHY_DEFAULT_CONFIG();

0 commit comments

Comments
 (0)