7
7
* The MIT License (MIT)
8
8
*
9
9
* Copyright (c) 2016, 2017 Nick Moore @mnemote
10
+ * Copyright (c) 2017 "Eric Poulsen" <eric@zyxod.com>
10
11
*
11
12
* Based on esp8266/modnetwork.c which is Copyright (c) 2015 Paul Sokolovsky
12
13
* And the ESP IDF example code which is Public Domain / CC0
34
35
#include <stdint.h>
35
36
#include <string.h>
36
37
38
+
37
39
#include "py/nlr.h"
38
40
#include "py/objlist.h"
39
41
#include "py/runtime.h"
47
49
#include "esp_log.h"
48
50
#include "lwip/dns.h"
49
51
#include "tcpip_adapter.h"
50
- #include "eth_phy/phy.h"
51
- #include "eth_phy/phy_tlk110.h"
52
- #include "eth_phy/phy_lan8720.h"
52
+
53
+ #include "modnetwork.h"
53
54
54
55
#define MODNETWORK_INCLUDE_CONSTANTS (1)
55
56
57
+
56
58
NORETURN void _esp_exceptions (esp_err_t e ) {
57
59
switch (e ) {
58
60
case ESP_ERR_WIFI_NOT_INIT :
@@ -101,34 +103,15 @@ static inline void esp_exceptions(esp_err_t e) {
101
103
102
104
#define ESP_EXCEPTIONS (x ) do { esp_exceptions(x); } while (0);
103
105
104
- enum { PHY_LAN8720 , PHY_TLK110 };
105
-
106
106
typedef struct _wlan_if_obj_t {
107
107
mp_obj_base_t base ;
108
108
int if_id ;
109
109
} wlan_if_obj_t ;
110
110
111
- typedef struct _lan_if_obj_t {
112
- mp_obj_base_t base ;
113
- int if_id ; // MUST BE FIRST
114
- bool initialized ;
115
- bool active ;
116
- uint8_t mdc_pin ;
117
- uint8_t mdio_pin ;
118
- int8_t phy_power_pin ;
119
- uint8_t phy_addr ;
120
- uint8_t phy_type ;
121
- eth_phy_check_link_func link_func ;
122
- eth_phy_power_enable_func power_func ;
123
- } lan_if_obj_t ;
124
-
125
111
const mp_obj_type_t wlan_if_type ;
126
112
STATIC const wlan_if_obj_t wlan_sta_obj = {{& wlan_if_type }, WIFI_IF_STA };
127
113
STATIC const wlan_if_obj_t wlan_ap_obj = {{& wlan_if_type }, WIFI_IF_AP };
128
114
129
- const mp_obj_type_t lan_if_type ;
130
- STATIC lan_if_obj_t lan_obj = {{& lan_if_type }, ESP_IF_ETH , false, false};
131
-
132
115
//static wifi_config_t wifi_ap_config = {{{0}}};
133
116
static wifi_config_t wifi_sta_config = {{{0 }}};
134
117
@@ -206,7 +189,6 @@ STATIC void require_if(mp_obj_t wlan_if, int if_no) {
206
189
STATIC mp_obj_t get_wlan (size_t n_args , const mp_obj_t * args ) {
207
190
static int initialized = 0 ;
208
191
if (!initialized ) {
209
- ESP_LOGD ("modnetwork" , "esp_event_loop_init done" );
210
192
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT ();
211
193
ESP_LOGD ("modnetwork" , "Initializing WiFi" );
212
194
ESP_EXCEPTIONS ( esp_wifi_init (& cfg ) );
@@ -229,136 +211,14 @@ STATIC mp_obj_t get_wlan(size_t n_args, const mp_obj_t *args) {
229
211
}
230
212
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (get_wlan_obj , 0 , 1 , get_wlan );
231
213
232
- STATIC void phy_power_enable (bool enable ) {
233
- lan_if_obj_t * self = MP_OBJ_TO_PTR (& lan_obj );
234
-
235
- if (self -> phy_power_pin != -1 ) {
236
-
237
- if (!enable ) {
238
- /* Do the PHY-specific power_enable(false) function before powering down */
239
- self -> power_func (false);
240
- }
241
-
242
- gpio_pad_select_gpio (self -> phy_power_pin );
243
- gpio_set_direction (self -> phy_power_pin ,GPIO_MODE_OUTPUT );
244
- if (enable == true) {
245
- gpio_set_level (self -> phy_power_pin , 1 );
246
- } else {
247
- gpio_set_level (self -> phy_power_pin , 0 );
248
- }
249
-
250
- // Allow the power up/down to take effect, min 300us
251
- vTaskDelay (1 );
252
-
253
- if (enable ) {
254
- /* Run the PHY-specific power on operations now the PHY has power */
255
- self -> power_func (true);
256
- }
257
- }
258
- }
259
-
260
- STATIC void init_lan_rmii () {
261
- lan_if_obj_t * self = MP_OBJ_TO_PTR (& lan_obj );
262
- phy_rmii_configure_data_interface_pins ();
263
- phy_rmii_smi_configure_pins (self -> mdc_pin , self -> mdio_pin );
264
- }
265
-
266
- STATIC void init_lan () {
267
- lan_if_obj_t * self = MP_OBJ_TO_PTR (& lan_obj );
268
- eth_config_t config ;
269
-
270
- switch (self -> phy_type ) {
271
- case PHY_TLK110 :
272
- config = phy_tlk110_default_ethernet_config ;
273
- break ;
274
- case PHY_LAN8720 :
275
- config = phy_lan8720_default_ethernet_config ;
276
- break ;
277
- }
278
-
279
- self -> link_func = config .phy_check_link ;
280
-
281
- // Replace default power func with our own
282
- self -> power_func = config .phy_power_enable ;
283
- config .phy_power_enable = phy_power_enable ;
284
-
285
- config .phy_addr = self -> phy_addr ;
286
- config .gpio_config = init_lan_rmii ;
287
- config .tcpip_input = tcpip_adapter_eth_input ;
288
-
289
- if (esp_eth_init (& config ) == ESP_OK ) {
290
- esp_eth_enable ();
291
- self -> active = true;
292
- } else {
293
- mp_raise_msg (& mp_type_OSError , "esp_eth_init() failed" );
294
- }
295
- }
296
-
297
- STATIC mp_obj_t get_lan (size_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
298
- lan_if_obj_t * self = MP_OBJ_TO_PTR (& lan_obj );
299
-
300
- if (self -> initialized ) {
301
- return MP_OBJ_FROM_PTR (& lan_obj );
302
- }
303
-
304
- enum { ARG_id , ARG_mdc , ARG_mdio , ARG_power , ARG_phy_addr , ARG_phy_type };
305
-
306
- uint8_t default_pins [] = {23 , 18 , 17 }; // mdc, mdio, power
307
-
308
- static const mp_arg_t allowed_args [] = {
309
- { MP_QSTR_id , MP_ARG_OBJ , {.u_obj = mp_const_none } },
310
- { MP_QSTR_mdc , MP_ARG_OBJ , {.u_obj = MP_OBJ_NULL } },
311
- { MP_QSTR_mdio , MP_ARG_OBJ , {.u_obj = MP_OBJ_NULL } },
312
- { MP_QSTR_power , MP_ARG_OBJ , {.u_obj = MP_OBJ_NULL } },
313
- { MP_QSTR_phy_addr , MP_ARG_INT , {.u_int = 0x01 } },
314
- { MP_QSTR_phy_type , MP_ARG_INT , {.u_int = PHY_LAN8720 } },
315
- };
316
-
317
- mp_arg_val_t args [MP_ARRAY_SIZE (allowed_args )];
318
- mp_arg_parse_all (n_args , pos_args , kw_args , MP_ARRAY_SIZE (allowed_args ), allowed_args , args );
319
-
320
- if (args [ARG_id ].u_obj != mp_const_none ) {
321
- if (mp_obj_get_int (args [ARG_id ].u_obj ) != 0 ) {
322
- mp_raise_ValueError ("invalid LAN interface identifier" );
323
- }
324
- }
325
-
326
- for (int i = ARG_mdc ; i <= ARG_power ; ++ i ) {
327
- if (args [i ].u_obj == mp_const_none && i == ARG_power ) {
328
- args [i ].u_int = -1 ;
329
- } else if (args [i ].u_obj != MP_OBJ_NULL ) {
330
- args [i ].u_int = machine_pin_get_id (args [i ].u_obj );
331
- } else {
332
- args [i ].u_int = default_pins [i - ARG_mdc ];
333
- }
334
- }
335
-
336
- if (args [ARG_phy_addr ].u_int < 0x00 || args [ARG_phy_addr ].u_int > 0x1f ) {
337
- mp_raise_ValueError ("invalid phy address" );
338
- }
339
-
340
- if (args [ARG_phy_type ].u_int != PHY_LAN8720 && args [ARG_phy_type ].u_int != PHY_TLK110 ) {
341
- mp_raise_ValueError ("invalid phy type" );
342
- }
343
-
344
- self -> mdc_pin = args [ARG_mdc ].u_int ;
345
- self -> mdio_pin = args [ARG_mdio ].u_int ;
346
- self -> phy_power_pin = args [ARG_power ].u_int ;
347
- self -> phy_addr = args [ARG_phy_addr ].u_int ;
348
- self -> phy_type = args [ARG_phy_type ].u_int ;
349
- self -> initialized = true;
350
- init_lan ();
351
- return MP_OBJ_FROM_PTR (& lan_obj );
352
- }
353
- STATIC MP_DEFINE_CONST_FUN_OBJ_KW (get_lan_obj , 0 , get_lan );
354
-
355
214
STATIC mp_obj_t esp_initialize () {
356
215
static int initialized = 0 ;
357
216
if (!initialized ) {
358
217
ESP_LOGD ("modnetwork" , "Initializing TCP/IP" );
359
218
tcpip_adapter_init ();
360
219
ESP_LOGD ("modnetwork" , "Initializing Event Loop" );
361
220
ESP_EXCEPTIONS ( esp_event_loop_init (event_handler , NULL ) );
221
+ ESP_LOGD ("modnetwork" , "esp_event_loop_init done" );
362
222
initialized = 1 ;
363
223
}
364
224
return mp_const_none ;
@@ -387,28 +247,6 @@ STATIC mp_obj_t esp_active(size_t n_args, const mp_obj_t *args) {
387
247
388
248
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (esp_active_obj , 1 , 2 , esp_active );
389
249
390
- STATIC mp_obj_t lan_active (size_t n_args , const mp_obj_t * args ) {
391
-
392
- lan_if_obj_t * self = MP_OBJ_TO_PTR (args [0 ]);
393
-
394
- if (n_args > 1 ) {
395
- if (mp_obj_is_true (args [1 ])) {
396
- self -> active = (esp_eth_enable () == ESP_OK );
397
- if (!self -> active ) {
398
- mp_raise_msg (& mp_type_OSError , "ethernet enable failed" );
399
- }
400
- } else {
401
- self -> active = !(esp_eth_disable () == ESP_OK );
402
- if (self -> active ) {
403
- mp_raise_msg (& mp_type_OSError , "ethernet disable failed" );
404
- }
405
- }
406
- }
407
- return self -> active ? mp_const_true : mp_const_false ;
408
- }
409
-
410
- STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (lan_active_obj , 1 , 2 , lan_active );
411
-
412
250
STATIC mp_obj_t esp_connect (size_t n_args , const mp_obj_t * args ) {
413
251
414
252
mp_uint_t len ;
@@ -445,12 +283,6 @@ STATIC mp_obj_t esp_status(mp_obj_t self_in) {
445
283
446
284
STATIC MP_DEFINE_CONST_FUN_OBJ_1 (esp_status_obj , esp_status );
447
285
448
- STATIC mp_obj_t lan_isconnected (mp_obj_t self_in ) {
449
- lan_if_obj_t * self = MP_OBJ_TO_PTR (self_in );
450
- return self -> link_func () ? mp_const_true : mp_const_false ;
451
- }
452
- STATIC MP_DEFINE_CONST_FUN_OBJ_1 (lan_isconnected_obj , lan_isconnected );
453
-
454
286
STATIC mp_obj_t esp_scan (mp_obj_t self_in ) {
455
287
// check that STA mode is active
456
288
wifi_mode_t mode ;
@@ -550,7 +382,7 @@ STATIC mp_obj_t esp_ifconfig(size_t n_args, const mp_obj_t *args) {
550
382
}
551
383
}
552
384
553
- STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (esp_ifconfig_obj , 1 , 2 , esp_ifconfig );
385
+ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (esp_ifconfig_obj , 1 , 2 , esp_ifconfig );
554
386
555
387
STATIC mp_obj_t esp_config (size_t n_args , const mp_obj_t * args , mp_map_t * kwargs ) {
556
388
if (n_args != 1 && kwargs -> used != 0 ) {
@@ -699,21 +531,6 @@ const mp_obj_type_t wlan_if_type = {
699
531
.locals_dict = (mp_obj_t )& wlan_if_locals_dict ,
700
532
};
701
533
702
- STATIC const mp_map_elem_t lan_if_locals_dict_table [] = {
703
- { MP_OBJ_NEW_QSTR (MP_QSTR_active ), (mp_obj_t )& lan_active_obj },
704
- { MP_OBJ_NEW_QSTR (MP_QSTR_isconnected ), (mp_obj_t )& lan_isconnected_obj },
705
- { MP_OBJ_NEW_QSTR (MP_QSTR_status ), (mp_obj_t )& esp_status_obj },
706
- { MP_OBJ_NEW_QSTR (MP_QSTR_ifconfig ), (mp_obj_t )& esp_ifconfig_obj },
707
- };
708
-
709
- STATIC MP_DEFINE_CONST_DICT (lan_if_locals_dict , lan_if_locals_dict_table );
710
-
711
- const mp_obj_type_t lan_if_type = {
712
- { & mp_type_type },
713
- .name = MP_QSTR_LAN ,
714
- .locals_dict = (mp_obj_t )& lan_if_locals_dict ,
715
- };
716
-
717
534
STATIC mp_obj_t esp_phy_mode (size_t n_args , const mp_obj_t * args ) {
718
535
return mp_const_none ;
719
536
}
0 commit comments