Skip to content

Commit a60700b

Browse files
committed
Get DHCP working ...
1 parent 9b36d33 commit a60700b

File tree

5 files changed

+42
-28
lines changed

5 files changed

+42
-28
lines changed

drivers/wiznet5k/ethernet/socket.c

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//! \version 1.0.3
77
//! \date 2013/10/21
88
//! \par Revision history
9+
//! <2018/10/09> Nick Moore fixes for CircuitPython
910
//! <2014/05/01> V1.0.3. Refer to M20140501
1011
//! 1. Implicit type casting -> Explicit type casting.
1112
//! 2. replace 0x01 with PACK_REMAINED in recvfrom()
@@ -393,15 +394,7 @@ int32_t WIZCHIP_EXPORT(sendto)(uint8_t sn, uint8_t * buf, uint16_t len, uint8_t
393394
CHECK_SOCKDATA();
394395
//M20140501 : For avoiding fatal error on memory align mismatched
395396
//if(*((uint32_t*)addr) == 0) return SOCKERR_IPINVALID;
396-
{
397-
uint32_t taddr;
398-
taddr = ((uint32_t)addr[0]) & 0x000000FF;
399-
taddr = (taddr << 8) + ((uint32_t)addr[1] & 0x000000FF);
400-
taddr = (taddr << 8) + ((uint32_t)addr[2] & 0x000000FF);
401-
taddr = (taddr << 8) + ((uint32_t)addr[3] & 0x000000FF);
402-
if (taddr == 0xFFFFFFFF || taddr == 0) return SOCKERR_IPINVALID;
403-
}
404-
//
397+
if ((addr[0] | addr[1] | addr[2] | addr[3]) == 0) return SOCKERR_IPINVALID;
405398
if(port == 0) return SOCKERR_PORTZERO;
406399
tmp = getSn_SR(sn);
407400
if(tmp != SOCK_MACRAW && tmp != SOCK_UDP) return SOCKERR_SOCKSTATUS;

drivers/wiznet5k/internet/dhcp/dhcp.c

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//! \version 1.1.0
77
//! \date 2013/11/18
88
//! \par Revision history
9+
//! <2018/10/09> Modified by Nick Moore for CircuitPython
910
//! <2013/11/18> 1st Release
1011
//! <2012/12/20> V1.1.0
1112
//! 1. Optimize code
@@ -51,7 +52,7 @@
5152

5253
//#include "Ethernet/socket.h"
5354
//#include "Internet/DHCP/dhcp.h"
54-
#include "../../Ethernet/socket.h"
55+
#include "../../ethernet/socket.h"
5556
#include "dhcp.h"
5657

5758
/* If you want to display debug & processing message, Define _DHCP_DEBUG_ in dhcp.h */
@@ -408,7 +409,7 @@ void send_DHCP_DISCOVER(void)
408409
printf("> Send DHCP_DISCOVER\r\n");
409410
#endif
410411

411-
sendto(DHCP_SOCKET, (uint8_t *)pDHCPMSG, RIP_MSG_SIZE, ip, DHCP_SERVER_PORT);
412+
WIZCHIP_EXPORT(sendto)(DHCP_SOCKET, (uint8_t *)pDHCPMSG, RIP_MSG_SIZE, ip, DHCP_SERVER_PORT);
412413
}
413414

414415
/* SEND DHCP REQUEST */
@@ -503,7 +504,7 @@ void send_DHCP_REQUEST(void)
503504
printf("> Send DHCP_REQUEST\r\n");
504505
#endif
505506

506-
sendto(DHCP_SOCKET, (uint8_t *)pDHCPMSG, RIP_MSG_SIZE, ip, DHCP_SERVER_PORT);
507+
WIZCHIP_EXPORT(sendto)(DHCP_SOCKET, (uint8_t *)pDHCPMSG, RIP_MSG_SIZE, ip, DHCP_SERVER_PORT);
507508

508509
}
509510

@@ -564,7 +565,7 @@ void send_DHCP_DECLINE(void)
564565
printf("\r\n> Send DHCP_DECLINE\r\n");
565566
#endif
566567

567-
sendto(DHCP_SOCKET, (uint8_t *)pDHCPMSG, RIP_MSG_SIZE, ip, DHCP_SERVER_PORT);
568+
WIZCHIP_EXPORT(sendto)(DHCP_SOCKET, (uint8_t *)pDHCPMSG, RIP_MSG_SIZE, ip, DHCP_SERVER_PORT);
568569
}
569570

570571
/* PARSE REPLY pDHCPMSG */
@@ -581,13 +582,13 @@ int8_t parseDHCPMSG(void)
581582

582583
if((len = getSn_RX_RSR(DHCP_SOCKET)) > 0)
583584
{
584-
len = recvfrom(DHCP_SOCKET, (uint8_t *)pDHCPMSG, len, svr_addr, &svr_port);
585+
len = WIZCHIP_EXPORT(recvfrom)(DHCP_SOCKET, (uint8_t *)pDHCPMSG, len, svr_addr, &svr_port);
585586
#ifdef _DHCP_DEBUG_
586587
printf("DHCP message : %d.%d.%d.%d(%d) %d received. \r\n",svr_addr[0],svr_addr[1],svr_addr[2], svr_addr[3],svr_port, len);
587588
#endif
588589
}
589590
else return 0;
590-
if (svr_port == DHCP_SERVER_PORT) {
591+
if (svr_port == DHCP_SERVER_PORT) {
591592
// compare mac address
592593
if ( (pDHCPMSG->chaddr[0] != DHCP_CHADDR[0]) || (pDHCPMSG->chaddr[1] != DHCP_CHADDR[1]) ||
593594
(pDHCPMSG->chaddr[2] != DHCP_CHADDR[2]) || (pDHCPMSG->chaddr[3] != DHCP_CHADDR[3]) ||
@@ -677,7 +678,7 @@ uint8_t DHCP_run(void)
677678
if(dhcp_state == STATE_DHCP_STOP) return DHCP_STOPPED;
678679

679680
if(getSn_SR(DHCP_SOCKET) != SOCK_UDP)
680-
socket(DHCP_SOCKET, Sn_MR_UDP, DHCP_CLIENT_PORT, 0x00);
681+
WIZCHIP_EXPORT(socket)(DHCP_SOCKET, Sn_MR_UDP, DHCP_CLIENT_PORT, 0x00);
681682

682683
ret = DHCP_RUNNING;
683684
type = parseDHCPMSG();
@@ -801,7 +802,7 @@ uint8_t DHCP_run(void)
801802

802803
void DHCP_stop(void)
803804
{
804-
close(DHCP_SOCKET);
805+
WIZCHIP_EXPORT(close)(DHCP_SOCKET);
805806
dhcp_state = STATE_DHCP_STOP;
806807
}
807808

@@ -869,7 +870,7 @@ int8_t check_DHCP_leasedIP(void)
869870

870871
// IP conflict detection : ARP request - ARP reply
871872
// Broadcasting ARP Request for check the IP conflict using UDP sendto() function
872-
ret = sendto(DHCP_SOCKET, (uint8_t *)"CHECK_IP_CONFLICT", 17, DHCP_allocated_ip, 5000);
873+
ret = WIZCHIP_EXPORT(sendto)(DHCP_SOCKET, (uint8_t *)"CHECK_IP_CONFLICT", 17, DHCP_allocated_ip, 5000);
873874

874875
// RCR value restore
875876
setRCR(tmp);
@@ -893,7 +894,7 @@ int8_t check_DHCP_leasedIP(void)
893894
}
894895
}
895896

896-
void DHCP_init(uint8_t s, uint8_t * buf)
897+
void DHCP_init(uint8_t s, DHCP_INIT_BUFFER_TYPE* buf)
897898
{
898899
uint8_t zeroip[4] = {0,0,0,0};
899900
getSHAR(DHCP_CHADDR);

drivers/wiznet5k/internet/dhcp/dhcp.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555

5656
/* Retry to processing DHCP */
5757
#define MAX_DHCP_RETRY 2 ///< Maximum retry count
58-
#define DHCP_WAIT_TIME 10 ///< Wait Time 10s
58+
#define DHCP_WAIT_TIME 3 ///< Wait Time 3s (was 10s)
5959

6060
/* UDP port numbers for DHCP */
6161
#define DHCP_SERVER_PORT 67 ///< DHCP server port number
@@ -78,12 +78,14 @@ enum
7878
DHCP_STOPPED ///< Stop processing DHCP protocol
7979
};
8080

81+
#define DHCP_INIT_BUFFER_TYPE uint32_t
82+
#define DHCP_INIT_BUFFER_SIZE (137)
8183
/*
8284
* @brief DHCP client initialization (outside of the main loop)
8385
* @param s - socket number
8486
* @param buf - buffer for processing DHCP message
8587
*/
86-
void DHCP_init(uint8_t s, uint8_t * buf);
88+
void DHCP_init(uint8_t s, DHCP_INIT_BUFFER_TYPE* buf);
8789

8890
/*
8991
* @brief DHCP 1s Tick Timer handler

ports/atmel-samd/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ SRC_MOD += $(addprefix $(WIZNET5K_DIR)/,\
300300
ethernet/wizchip_conf.c \
301301
ethernet/socket.c \
302302
internet/dns/dns.c \
303+
internet/dhcp/dhcp.c \
303304
)
304305

305306
endif # MICROPY_PY_WIZNET5K

shared-bindings/wiznet/wiznet5k.c

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
#include "ethernet/wizchip_conf.h"
4949
#include "ethernet/socket.h"
5050
#include "internet/dns/dns.h"
51+
#include "internet/dhcp/dhcp.h"
5152

5253
/// \moduleref network
5354

@@ -337,6 +338,24 @@ STATIC mp_obj_t wiznet5k_socket_disconnect(mp_obj_t self_in) {
337338
}
338339
#endif
339340

341+
static void wiznet5k_try_dhcp(void) {
342+
DHCP_INIT_BUFFER_TYPE dhcp_buf[DHCP_INIT_BUFFER_SIZE];
343+
344+
// Set up the socket to listen on UDP 68 before calling DHCP_init
345+
WIZCHIP_EXPORT(socket)(0, MOD_NETWORK_SOCK_DGRAM, DHCP_CLIENT_PORT, 0);
346+
DHCP_init(0, dhcp_buf);
347+
348+
// try a few times for DHCP ... XXX this should be asynchronous.
349+
for (int i=0; i<10; i++) {
350+
DHCP_time_handler();
351+
int dhcp_state = DHCP_run();
352+
if (dhcp_state == DHCP_IP_LEASED || dhcp_state == DHCP_IP_CHANGED) break;
353+
mp_hal_delay_ms(1000);
354+
}
355+
DHCP_stop();
356+
WIZCHIP_EXPORT(close)(0);
357+
}
358+
340359
/******************************************************************************/
341360
// MicroPython bindings
342361

@@ -377,23 +396,21 @@ STATIC mp_obj_t wiznet5k_make_new(const mp_obj_type_t *type, size_t n_args, size
377396
reg_wizchip_cs_cbfunc(wiz_cs_select, wiz_cs_deselect);
378397
reg_wizchip_spi_cbfunc(wiz_spi_read, wiz_spi_write);
379398

380-
uint8_t sn_size[16] = {2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2}; // 2k buffer for each socket
399+
// 2k buffer for each socket
400+
uint8_t sn_size[16] = {2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2};
381401
ctlwizchip(CW_INIT_WIZCHIP, sn_size);
382402

383-
// set some sensible default values; they are configurable using ifconfig method
384403
wiz_NetInfo netinfo = {
385-
.ip = {192, 168, 0, 18},
386-
.sn = {255, 255, 255, 0},
387-
.gw = {192, 168, 0, 1},
388-
.dns = {8, 8, 8, 8}, // Google public DNS
389-
.dhcp = NETINFO_STATIC,
404+
.dhcp = NETINFO_DHCP,
390405
};
391406
network_module_create_random_mac_address(netinfo.mac);
392407
ctlnetwork(CN_SET_NETINFO, (void*)&netinfo);
393408

394409
// seems we need a small delay after init
395410
mp_hal_delay_ms(250);
396411

412+
wiznet5k_try_dhcp();
413+
397414
// register with network module
398415
network_module_register_nic(&wiznet5k_obj);
399416

0 commit comments

Comments
 (0)