34
34
#include "py/mperrno.h"
35
35
#include "py/mphal.h"
36
36
#include "lib/netutils/netutils.h"
37
- #include "modnetwork.h"
38
- #include "pin.h"
39
- #include "spi.h"
37
+
38
+ #include "shared-bindings/network/__init__.h"
39
+ #include "shared-bindings/digitalio/DigitalInOut.h"
40
+ #include "shared-bindings/digitalio/DriveMode.h"
41
+ #include "shared-bindings/busio/SPI.h"
42
+ #include "shared-bindings/random/__init__.h"
43
+
44
+ #if MICROPY_PY_WIZNET5K
40
45
41
46
#include "ethernet/wizchip_conf.h"
42
47
#include "ethernet/socket.h"
47
52
typedef struct _wiznet5k_obj_t {
48
53
mp_obj_base_t base ;
49
54
mp_uint_t cris_state ;
50
- const spi_t * spi ;
51
- const pin_obj_t * cs ;
52
- const pin_obj_t * rst ;
55
+ busio_spi_obj_t * spi ;
56
+ digitalio_digitalinout_obj_t cs ;
57
+ digitalio_digitalinout_obj_t rst ;
53
58
uint8_t socket_used ;
54
59
} wiznet5k_obj_t ;
55
60
@@ -64,21 +69,19 @@ STATIC void wiz_cris_exit(void) {
64
69
}
65
70
66
71
STATIC void wiz_cs_select (void ) {
67
- mp_hal_pin_low ( wiznet5k_obj .cs );
72
+ common_hal_digitalio_digitalinout_set_value ( & wiznet5k_obj .cs , 0 );
68
73
}
69
74
70
75
STATIC void wiz_cs_deselect (void ) {
71
- mp_hal_pin_high ( wiznet5k_obj .cs );
76
+ common_hal_digitalio_digitalinout_set_value ( & wiznet5k_obj .cs , 1 );
72
77
}
73
78
74
79
STATIC void wiz_spi_read (uint8_t * buf , uint32_t len ) {
75
- HAL_StatusTypeDef status = HAL_SPI_Receive (wiznet5k_obj .spi -> spi , buf , len , 5000 );
76
- (void )status ;
80
+ (void )common_hal_busio_spi_read (wiznet5k_obj .spi , buf , len , 0 );
77
81
}
78
82
79
83
STATIC void wiz_spi_write (const uint8_t * buf , uint32_t len ) {
80
- HAL_StatusTypeDef status = HAL_SPI_Transmit (wiznet5k_obj .spi -> spi , (uint8_t * )buf , len , 5000 );
81
- (void )status ;
84
+ (void )common_hal_busio_spi_write (wiznet5k_obj .spi , buf , len );
82
85
}
83
86
84
87
STATIC int wiznet5k_gethostbyname (mp_obj_t nic , const char * name , mp_uint_t len , uint8_t * out_ip ) {
@@ -332,6 +335,19 @@ STATIC mp_obj_t wiznet5k_socket_disconnect(mp_obj_t self_in) {
332
335
}
333
336
#endif
334
337
338
+ void create_random_mac_address (uint8_t * mac ) {
339
+ uint32_t rb1 = shared_modules_random_getrandbits (24 );
340
+ uint32_t rb2 = shared_modules_random_getrandbits (24 );
341
+ // first octet has multicast bit (0) cleared and local bit (1) set
342
+ // everything else is just set randomly
343
+ mac [0 ] = ((uint8_t )(rb1 >> 16 ) & 0xfe ) | 0x02 ;
344
+ mac [1 ] = (uint8_t )(rb1 >> 8 );
345
+ mac [2 ] = (uint8_t )(rb1 );
346
+ mac [3 ] = (uint8_t )(rb2 >> 16 );
347
+ mac [4 ] = (uint8_t )(rb2 >> 8 );
348
+ mac [5 ] = (uint8_t )(rb2 );
349
+ }
350
+
335
351
/******************************************************************************/
336
352
// MicroPython bindings
337
353
@@ -344,32 +360,28 @@ STATIC mp_obj_t wiznet5k_make_new(const mp_obj_type_t *type, size_t n_args, size
344
360
// init the wiznet5k object
345
361
wiznet5k_obj .base .type = (mp_obj_type_t * )& mod_network_nic_type_wiznet5k ;
346
362
wiznet5k_obj .cris_state = 0 ;
347
- wiznet5k_obj .spi = spi_from_mp_obj (args [0 ]);
348
- wiznet5k_obj .cs = pin_find ( args [1 ]);
349
- wiznet5k_obj .rst = pin_find ( args [2 ]);
363
+ wiznet5k_obj .spi = MP_OBJ_TO_PTR (args [0 ]);
364
+ common_hal_digitalio_digitalinout_construct ( & wiznet5k_obj .cs , args [1 ]);
365
+ common_hal_digitalio_digitalinout_construct ( & wiznet5k_obj .rst , args [2 ]);
350
366
wiznet5k_obj .socket_used = 0 ;
351
367
352
368
/*!< SPI configuration */
353
- SPI_InitTypeDef * init = & wiznet5k_obj .spi -> spi -> Init ;
354
- init -> Mode = SPI_MODE_MASTER ;
355
- init -> Direction = SPI_DIRECTION_2LINES ;
356
- init -> DataSize = SPI_DATASIZE_8BIT ;
357
- init -> CLKPolarity = SPI_POLARITY_LOW ; // clock is low when idle
358
- init -> CLKPhase = SPI_PHASE_1EDGE ; // data latched on first edge, which is rising edge for low-idle
359
- init -> NSS = SPI_NSS_SOFT ;
360
- init -> BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2 ; // clock freq = f_PCLK / this_prescale_value; Wiz820i can do up to 80MHz
361
- init -> FirstBit = SPI_FIRSTBIT_MSB ;
362
- init -> TIMode = SPI_TIMODE_DISABLED ;
363
- init -> CRCCalculation = SPI_CRCCALCULATION_DISABLED ;
364
- init -> CRCPolynomial = 7 ; // unused
365
- spi_init (wiznet5k_obj .spi , false);
366
-
367
- mp_hal_pin_output (wiznet5k_obj .cs );
368
- mp_hal_pin_output (wiznet5k_obj .rst );
369
-
370
- mp_hal_pin_low (wiznet5k_obj .rst );
371
- mp_hal_delay_ms (1 ); // datasheet says 2us
372
- mp_hal_pin_high (wiznet5k_obj .rst );
369
+ // XXX probably should check if the provided SPI is already configured, and
370
+ // if so skip configuration?
371
+
372
+ common_hal_busio_spi_configure (wiznet5k_obj .spi ,
373
+ 10000000 , // BAUDRATE 10MHz
374
+ 1 , // HIGH POLARITY
375
+ 1 , // SECOND PHASE TRANSITION
376
+ 8 // 8 BITS
377
+ );
378
+
379
+ common_hal_digitalio_digitalinout_switch_to_output (& wiznet5k_obj .cs , 1 , DRIVE_MODE_PUSH_PULL );
380
+ common_hal_digitalio_digitalinout_switch_to_output (& wiznet5k_obj .rst , 1 , DRIVE_MODE_PUSH_PULL );
381
+
382
+ common_hal_digitalio_digitalinout_set_value (& wiznet5k_obj .rst , 0 );
383
+ mp_hal_delay_us (10 ); // datasheet says 2us
384
+ common_hal_digitalio_digitalinout_set_value (& wiznet5k_obj .rst , 1 );
373
385
mp_hal_delay_ms (160 ); // datasheet says 150ms
374
386
375
387
reg_wizchip_cris_cbfunc (wiz_cris_enter , wiz_cris_exit );
@@ -381,13 +393,13 @@ STATIC mp_obj_t wiznet5k_make_new(const mp_obj_type_t *type, size_t n_args, size
381
393
382
394
// set some sensible default values; they are configurable using ifconfig method
383
395
wiz_NetInfo netinfo = {
384
- .mac = {0x00 , 0x08 , 0xdc , 0xab , 0xcd , 0xef },
385
396
.ip = {192 , 168 , 0 , 18 },
386
397
.sn = {255 , 255 , 255 , 0 },
387
398
.gw = {192 , 168 , 0 , 1 },
388
399
.dns = {8 , 8 , 8 , 8 }, // Google public DNS
389
400
.dhcp = NETINFO_STATIC ,
390
401
};
402
+ create_random_mac_address (netinfo .mac );
391
403
ctlnetwork (CN_SET_NETINFO , (void * )& netinfo );
392
404
393
405
// seems we need a small delay after init
@@ -499,3 +511,16 @@ const mod_network_nic_type_t mod_network_nic_type_wiznet5k = {
499
511
.settimeout = wiznet5k_socket_settimeout ,
500
512
.ioctl = wiznet5k_socket_ioctl ,
501
513
};
514
+
515
+ STATIC const mp_rom_map_elem_t mp_module_wiznet_globals_table [] = {
516
+ { MP_ROM_QSTR (MP_QSTR___name__ ), MP_ROM_QSTR (MP_QSTR_wiznet ) },
517
+ { MP_ROM_QSTR (MP_QSTR_WIZNET5K ), MP_ROM_PTR (& mod_network_nic_type_wiznet5k ) },
518
+ };
519
+ STATIC MP_DEFINE_CONST_DICT (mp_module_wiznet_globals , mp_module_wiznet_globals_table );
520
+
521
+ const mp_obj_module_t wiznet_module = {
522
+ .base = { & mp_type_module },
523
+ .globals = (mp_obj_dict_t * )& mp_module_wiznet_globals ,
524
+ };
525
+
526
+ #endif // MICROPY_PY_WIZNET5K
0 commit comments