8000 rp2/wiznet: Allow defining default HW details in mpboardconfig. · micropython/micropython@256d310 · GitHub
[go: up one dir, main page]

Skip to content

Commit 256d310

Browse files
committed
rp2/wiznet: Allow defining default HW details in mpboardconfig.
1 parent e255f7e commit 256d310

File tree

1 file changed

+62
-21
lines changed

1 file changed

+62
-21
lines changed

ports/rp2/modnwwiznet5k.c

Lines changed: 62 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
#include <stdint.h>
2929
#include <string.h>
3030

31-
#include "py/objlist.h"
31+
#include "py/mpconfig.h"
3232
#include "py/runtime.h"
3333
#include "py/stream.h"
3434
#include "py/mperrno.h"
@@ -47,12 +47,24 @@
4747

4848
/// \moduleref network
4949

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+
5062
typedef struct _wiznet5k_obj_t {
5163
mp_obj_base_t base;
5264
mp_uint_t cris_state;
5365
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;
5668
uint8_t socket_used;
5769
} wiznet5k_obj_t;
5870

@@ -68,12 +80,12 @@ STATIC void wiz_cris_exit(void) {
6880

6981
STATIC void wiz_cs_select(void) {
7082

71-
mp_hal_pin_write(wiznet5k_obj.cs.id, 0);
83+
mp_hal_pin_write(wiznet5k_obj.cs, 0);
7284
}
7385

7486
STATIC void wiz_cs_deselect(void) {
7587

76-
mp_hal_pin_write(wiznet5k_obj.cs.id, 1);
88+
mp_hal_pin_write(wiznet5k_obj.cs, 1);
7789

7890
}
7991

@@ -351,31 +363,53 @@ STATIC mp_obj_t wiznet5k_socket_disconnect(mp_obj_t self_in) {
351363
/// \classmethod \constructor(spi, pin_cs, pin_rst)
352364
/// Create and return a WIZNET5K object.
353365
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;
354369
// 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+
}
360394

361395
// init the wiznet5k object
362396
wiznet5k_obj.base.type = (mp_obj_type_t *)&mod_network_nic_type_wiznet5k;
363397
wiznet5k_obj.cris_state = 0;
364398
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;
367401

368402
wiznet5k_obj.socket_used = 0;
369403

370404
/*!< 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);
375406

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);
377411
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);
379413
mp_hal_delay_ms(160); // datasheet says 150ms
380414

381415
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) {
415449
if (i % 16 == 0) {
416450
printf("\n %04x:", i);
417451
}
418-
#if _WIZCHIP_ == 5200
419-
uint32_t reg = i;
420-
#else
452+
#if _WIZCHIP_ == 5500
421453
uint32_t reg = _WIZCHIP_IO_BASE_ | i << 8;
454+
#else
455+
uint32_t reg = i;
422456
#endif
423457
printf(" %02x", WIZCHIP_READ(reg));
424458
}
@@ -475,8 +509,15 @@ STATIC mp_obj_t wiznet5k_ifconfig(size_t n_args, const mp_obj_t *args) {
475509
}
476510
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(wiznet5k_ifconfig_obj, 1, 2, wiznet5k_ifconfig);
477511

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+
478518
STATIC const mp_rom_map_elem_t wiznet5k_locals_dict_table[] = {
479519
{ 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) },
480521
{ MP_ROM_QSTR(MP_QSTR_ifconfig), MP_ROM_PTR(&wiznet5k_ifconfig_obj) },
481522
{ MP_ROM_QSTR(MP_QSTR_isconnected), MP_ROM_PTR(&wiznet5k_isconnected_obj) },
482523
};

0 commit comments

Comments
 (0)
0