Skip to content

Commit 404dae8

Browse files
committed
unix, stmhal: Introduce mp_hal_delay_ms(), mp_hal_ticks_ms().
These MPHAL functions are intended to replace previously used HAL_Delay(), HAL_GetTick() to provide better naming and MPHAL separation (they are fully equivalent otherwise). Also, refactor extmod/modlwip to use them.
1 parent 9011815 commit 404dae8

5 files changed

Lines changed: 20 additions & 16 deletions

File tree

extmod/lwip-include/lwipopts.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
#ifndef __LWIPOPTS_H__
22
#define __LWIPOPTS_H__
33

4+
#include <py/mpconfig.h>
5+
#include <py/misc.h>
6+
#include MICROPY_HAL_H
7+
48
// We're running without an OS for this port. We don't provide any services except light protection.
59
#define NO_SYS 1
610

@@ -26,7 +30,7 @@ typedef uint32_t sys_prot_t;
2630
// For now, we can simply define this as a macro for the timer code. But this function isn't
2731
// universal and other ports will need to do something else. It may be necessary to move
2832
// things like this into a port-provided header file.
29-
#define sys_now HAL_GetTick
33+
#define sys_now mp_hal_ticks_ms
3034

3135
#endif
3236

extmod/modlwip.c

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,6 @@
4848
#include "lwip/sio.h"
4949
#endif
5050

51-
// FIXME FIXME FIXME
52-
#define LWIP_DELAY HAL_Delay
53-
5451
#ifdef MICROPY_PY_LWIP_SLIP
5552
/******************************************************************************/
5653
// Slip object for modlwip. Requires a serial driver for the port that supports
@@ -324,7 +321,7 @@ STATIC mp_uint_t lwip_udp_receive(lwip_socket_obj_t *socket, byte *buf, mp_uint_
324321
if (socket->incoming == NULL) {
325322
if (socket->timeout != -1) {
326323
for (mp_uint_t retries = socket->timeout / 100; retries--;) {
327-
LWIP_DELAY(100);
324+
mp_hal_delay_ms(100);
328325
if (socket->incoming != NULL) break;
329326
}
330327
if (socket->incoming == NULL) {
@@ -333,7 +330,7 @@ STATIC mp_uint_t lwip_udp_receive(lwip_socket_obj_t *socket, byte *buf, mp_uint_
333330
}
334331
} else {
335332
while (socket->incoming == NULL) {
336-
LWIP_DELAY(100);
333+
mp_hal_delay_ms(100);
337334
}
338335
}
339336
}
@@ -378,7 +375,7 @@ STATIC mp_uint_t lwip_tcp_receive(lwip_socket_obj_t *socket, byte *buf, mp_uint_
378375
if (socket->incoming == NULL) {
379376
if (socket->timeout != -1) {
380377
for (mp_uint_t retries = socket->timeout / 100; retries--;) {
381-
LWIP_DELAY(100);
378+
mp_hal_delay_ms(100);
382379
if (socket->incoming != NULL) break;
383380
}
384381
if (socket->incoming == NULL) {
@@ -387,7 +384,7 @@ STATIC mp_uint_t lwip_tcp_receive(lwip_socket_obj_t *socket, byte *buf, mp_uint_
387384
}
388385
} else {
389386
while (socket->incoming == NULL) {
390-
LWIP_DELAY(100);
387+
mp_hal_delay_ms(100);
391388
}
392389
}
393390
}
@@ -572,15 +569,15 @@ STATIC mp_obj_t lwip_socket_accept(mp_obj_t self_in) {
572569
if (socket->incoming == NULL) {
573570
if (socket->timeout != -1) {
574571
for (mp_uint_t retries = socket->timeout / 100; retries--;) {
575-
LWIP_DELAY(100);
572+
mp_hal_delay_ms(100);
576573
if (socket->incoming != NULL) break;
577574
}
578575
if (socket->incoming == NULL) {
579576
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(ETIMEDOUT)));
580577
}
581578
} else {
582579
while (socket->incoming == NULL) {
583-
LWIP_DELAY(100);
580+
mp_hal_delay_ms(100);
584581
}
585582
}
586583
}
@@ -656,15 +653,15 @@ STATIC mp_obj_t lwip_socket_connect(mp_obj_t self_in, mp_obj_t addr_in) {
656653
// And now we wait...
657654
if (socket->timeout != -1) {
658655
for (mp_uint_t retries = socket->timeout / 100; retries--;) {
659-
LWIP_DELAY(100);
656+
mp_hal_delay_ms(100);
660657
if (socket->connected != 1) break;
661658
}
662659
if (socket->connected == 1) {
663660
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(ETIMEDOUT)));
664661
}
665662
} else {
666663
while (socket->connected == 1) {
667-
LWIP_DELAY(100);
664+
mp_hal_delay_ms(100);
668665
}
669666
}
670667
if (socket->connected == 2) {
@@ -946,7 +943,7 @@ STATIC mp_obj_t lwip_getaddrinfo(mp_obj_t host_in, mp_obj_t port_in) {
946943
}
947944
case ERR_INPROGRESS: {
948945
while(!lwip_dns_returned) {
949-
LWIP_DELAY(100);
946+
mp_hal_delay_ms(100);
950947
}
951948
if (lwip_dns_returned == 2) {
952949
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(ENOENT)));

stmhal/mphal.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,6 @@ int mp_hal_stdin_rx_chr(void);
2323
void mp_hal_stdout_tx_str(const char *str);
2424
void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len);
2525
void mp_hal_stdout_tx_strn_cooked(const char *str, mp_uint_t len);
26+
27+
#define mp_hal_delay_ms HAL_Delay
28+
#define mp_hal_ticks_ms HAL_GetTick

unix/unix_mphal.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ void mp_hal_stdout_tx_str(const char *str) {
119119
mp_hal_stdout_tx_strn(str, strlen(str));
120120
}
121121

122-
uint32_t HAL_GetTick(void) {
122+
uint32_t mp_hal_ticks_ms(void) {
123123
struct timeval tv;
124124
gettimeofday(&tv, NULL);
125125
return tv.tv_sec * 1000 + tv.tv_usec / 1000;

unix/unix_mphal.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,5 @@ void mp_hal_stdout_tx_str(const char *str);
3838
void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len);
3939
void mp_hal_stdout_tx_strn_cooked(const char *str, mp_uint_t len);
4040

41-
#define HAL_Delay(ms) usleep((ms) * 1000)
42-
uint32_t HAL_GetTick(void);
41+
#define mp_hal_delay_ms(ms) usleep((ms) * 1000)
42+
uint32_t mp_hal_ticks_ms(void);

0 commit comments

Comments
 (0)