|
1 def connect_lan(): |
|
2 import os |
|
3 from adafruit_wiznet5k import adafruit_wiznet5k |
|
4 |
|
5 global nic |
|
6 |
|
7 if os.getenv("WIZNET_IFCONFIG_0") is None: |
|
8 print("The network configuration is kept in 'settings.toml'") |
|
9 print("with the keys 'WIZNET_IFCONFIG_0' to 'WIZNET_IFCONFIG_3'.") |
|
10 print("Please add them there.") |
|
11 return None |
|
12 |
|
13 try: |
|
14 nic |
|
15 except NameError: |
|
16 nic = None |
|
17 |
|
18 if nic is None: |
|
19 import board |
|
20 import busio |
|
21 import digitalio |
|
22 from adafruit_wiznet5k.adafruit_wiznet5k import WIZNET5K |
|
23 |
|
24 SPI0_RX = board.GP16 |
|
25 SPI0_CSn = board.GP17 |
|
26 SPI0_SCK = board.GP18 |
|
27 SPI0_TX = board.GP19 |
|
28 W5x00_RSTn = board.GP20 |
|
29 |
|
30 ethernetRst = digitalio.DigitalInOut(W5x00_RSTn) |
|
31 ethernetRst.direction = digitalio.Direction.OUTPUT |
|
32 |
|
33 cs = digitalio.DigitalInOut(SPI0_CSn) |
|
34 spi = busio.SPI(SPI0_SCK, MOSI=SPI0_TX, MISO=SPI0_RX) |
|
35 |
|
36 nic = WIZNET5K(spi, cs, reset=ethernetRst, is_dhcp=False) |
|
37 |
|
38 nic.mac_address = adafruit_wiznet5k._DEFAULT_MAC |
|
39 if os.getenv("WIZNET_IFCONFIG_0") == 'dhcp': |
|
40 nic.set_dhcp(response_timeout=14) |
|
41 else: |
|
42 nic.ifconfig = ( |
|
43 nic.unpretty_ip(os.getenv("WIZNET_IFCONFIG_0")), |
|
44 nic.unpretty_ip(os.getenv("WIZNET_IFCONFIG_1")), |
|
45 nic.unpretty_ip(os.getenv("WIZNET_IFCONFIG_2")), |
|
46 tuple(int(a) for a in os.getenv("WIZNET_IFCONFIG_3").split('.')), |
|
47 ) |
|
48 |
|
49 return nic |