28
28
#include <stdint.h>
29
29
#include <string.h>
30
30
31
- #include "py/objlist .h"
31
+ #include "py/mpconfig .h"
32
32
#include "py/runtime.h"
33
33
#include "py/stream.h"
34
34
#include "py/mperrno.h"
47
47
48
48
/// \moduleref network
49
49
50
+ #ifndef MICROPY_HW_WIZNET_SPI_BAUDRATE
51
+ #define MICROPY_HW_WIZNET_SPI_BAUDRATE (48000)
52
+ #endif
53
+
54
+ #ifndef WIZCHIP_SREG_ADDR
55
+ #if (_WIZCHIP_ == 5500 )
56
+ #define WIZCHIP_SREG_ADDR (sn , addr ) (_W5500_IO_BASE_ + (addr << 8) + (WIZCHIP_SREG_BLOCK(sn) << 3))
57
+ #else
58
+ #define WIZCHIP_SREG_ADDR (sn , addr ) (_WIZCHIP_IO_BASE_ + WIZCHIP_SREG_BLOCK(sn) + (addr))
59
+ #endif
60
+ #endif
61
+
50
62
typedef struct _wiznet5k_obj_t {
51
63
mp_obj_base_t base ;
52
64
mp_uint_t cris_state ;
53
65
machine_spi_obj_t * spi ;
54
- machine_pin_obj_t cs ;
55
- machine_pin_obj_t rst ;
66
+ mp_uint_t cs ;
67
+ mp_uint_t rst ;
56
68
uint8_t socket_used ;
57
69
} wiznet5k_obj_t ;
58
70
@@ -68,12 +80,12 @@ STATIC void wiz_cris_exit(void) {
68
80
69
81
STATIC void wiz_cs_select (void ) {
70
82
71
- mp_hal_pin_write (wiznet5k_obj .cs . id , 0 );
83
+ mp_hal_pin_write (wiznet5k_obj .cs , 0 );
72
84
}
73
85
74
86
STATIC void wiz_cs_deselect (void ) {
75
87
76
- mp_hal_pin_write (wiznet5k_obj .cs . id , 1 );
88
+ mp_hal_pin_write (wiznet5k_obj .cs , 1 );
77
89
78
90
}
79
91
@@ -351,31 +363,53 @@ STATIC mp_obj_t wiznet5k_socket_disconnect(mp_obj_t self_in) {
351
363
/// \classmethod \constructor(spi, pin_cs, pin_rst)
352
364
/// Create and return a WIZNET5K object.
353
365
STATIC mp_obj_t wiznet5k_make_new (const mp_obj_type_t * type , size_t n_args , size_t n_kw , const mp_obj_t * args ) {
366
+ uint32_t cs ;
367
+ uint32_t rst ;
368
+ machine_spi_obj_t * spi ;
354
369
// check arguments
355
- mp_arg_check_num (n_args , n_kw , 3 , 3 , false);
356
-
357
- machine_spi_obj_t * spi = spi_from_mp_obj (args [0 ]);
358
- mp_hal_pin_obj_t cs = mp_hal_get_pin_obj (args [1 ]);
359
- mp_hal_pin_obj_t rst = mp_hal_get_pin_obj (args [2 ]);
370
+ #ifdef MICROPY_HW_WIZNET_SPI_ID
371
+ // Allow auto-configuration of SPI if defined for board and no args passed
372
+ if (n_args == 0 ) {
373
+ // Initialize SPI.
374
+ mp_obj_t args [] = {
375
+ MP_OBJ_NEW_SMALL_INT (MICROPY_HW_WIZNET_SPI_ID ),
376
+ MP_OBJ_NEW_SMALL_INT (MICROPY_HW_WIZNET_SPI_BAUDRATE ),
377
+ };
378
+
379
+ spi = machine_spi_type .make_new ((mp_obj_t )& machine_spi_type , 2 , 0 , args );
380
+
381
+ cs = MICROPY_HW_WIZNET_PIN_CS ;
382
+ rst = MICROPY_HW_WIZNET_PIN_RST ;
383
+
384
+ } else {
385
+ #else
386
+ {
387
+ #endif
388
+ // If passing in args, must supply spi, pin_cs, pin_rst
389
+ mp_arg_check_num (n_args , n_kw , 3 , 3 , false);
390
+ spi = spi_from_mp_obj (args [0 ]);
391
+ cs = mp_hal_get_pin_obj (args [1 ]);
392
+ rst = mp_hal_get_pin_obj (args [2 ]);
393
+ }
360
394
361
395
// init the wiznet5k object
362
396
wiznet5k_obj .base .type = (mp_obj_type_t * )& mod_network_nic_type_wiznet5k ;
363
397
wiznet5k_obj .cris_state = 0 ;
364
398
wiznet5k_obj .spi = spi ;
365
- wiznet5k_obj .cs . id = cs ;
366
- wiznet5k_obj .rst . id = rst ;
399
+ wiznet5k_obj .cs = cs ;
400
+ wiznet5k_obj .rst = rst ;
367
401
368
402
wiznet5k_obj .socket_used = 0 ;
369
403
370
404
/*!< SPI configuration */
371
- spi_init (wiznet5k_obj .spi -> spi_inst , 48000 );
372
-
373
- mp_hal_pin_output (wiznet5k_obj .cs .id );
374
- mp_hal_pin_output (wiznet5k_obj .rst .id );
405
+ spi_init (wiznet5k_obj .spi -> spi_inst , MICROPY_HW_WIZNET_SPI_BAUDRATE );
375
406
376
- mp_hal_pin_write (wiznet5k_obj .rst .id ,0 );
407
+ mp_hal_pin_output (wiznet5k_obj .cs );
408
+ mp_hal_pin_output (wiznet5k_obj .rst );
409
+
410
+ mp_hal_pin_write (wiznet5k_obj .rst ,0 );
377
411
mp_hal_delay_ms (1 ); // datasheet says 2us
378
- mp_hal_pin_write (wiznet5k_obj .rst . id , 1 );
412
+ mp_hal_pin_write (wiznet5k_obj .rst , 1 );
379
413
mp_hal_delay_ms (160 ); // datasheet says 150ms
380
414
381
415
reg_wizchip_cris_cbfunc (wiz_cris_enter , wiz_cris_exit );
@@ -415,10 +449,10 @@ STATIC mp_obj_t wiznet5k_regs(mp_obj_t self_in) {
415
449
if (i % 16 == 0 ) {
416
450
printf ("\n %04x:" , i );
417
451
}
418
- #if _WIZCHIP_ == 5200
419
- uint32_t reg = i ;
420
- #else
452
+ #if _WIZCHIP_ == 5500
421
453
uint32_t reg = _WIZCHIP_IO_BASE_ | i << 8 ;
454
+ #else
455
+ uint32_t reg = i ;
422
456
#endif
423
457
printf (" %02x" , WIZCHIP_READ (reg ));
424
458
}
@@ -475,8 +509,15 @@ STATIC mp_obj_t wiznet5k_ifconfig(size_t n_args, const mp_obj_t *args) {
475
509
}
476
510
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (wiznet5k_ifconfig_obj , 1 , 2 , wiznet5k_ifconfig );
477
511
512
+ STATIC mp_obj_t wiznet5k_spi (mp_obj_t self_in ) {
513
+ (void )self_in ;
514
+ return MP_OBJ_FROM_PTR (wiznet5k_obj .spi );
515
+ }
516
+ STATIC MP_DEFINE_CONST_FUN_OBJ_1 (wiznet5k_spi_obj , wiznet5k_spi );
517
+
478
518
STATIC const mp_rom_map_elem_t wiznet5k_locals_dict_table [] = {
479
519
{ MP_ROM_QSTR (MP_QSTR_regs ), MP_ROM_PTR (& wiznet5k_regs_obj ) },
520
+ { MP_ROM_QSTR (MP_QSTR_spi ), MP_ROM_PTR (& wiznet5k_spi_obj ) },
480
521
{ MP_ROM_QSTR (MP_QSTR_ifconfig ), MP_ROM_PTR (& wiznet5k_ifconfig_obj ) },
481
522
{ MP_ROM_QSTR (MP_QSTR_isconnected ), MP_ROM_PTR (& wiznet5k_isconnected_obj ) },
482
523
};
0 commit comments