From 404dae80a9d5cb6315071fe6206b2a6026be0a09 Mon Sep 17 00:00:00 2001
From: Paul Sokolovsky <pfalcon@users.sourceforge.net>
Date: Tue, 27 Oct 2015 23:31:42 +0300
Subject: [PATCH] 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.
---
 extmod/lwip-include/lwipopts.h |  6 +++++-
 extmod/modlwip.c               | 21 +++++++++------------
 stmhal/mphal.h                 |  3 +++
 unix/unix_mphal.c              |  2 +-
 unix/unix_mphal.h              |  4 ++--
 5 files changed, 20 insertions(+), 16 deletions(-)

diff --git a/extmod/lwip-include/lwipopts.h b/extmod/lwip-include/lwipopts.h
index ddb434890..a11bac935 100644
--- a/extmod/lwip-include/lwipopts.h
+++ b/extmod/lwip-include/lwipopts.h
@@ -1,6 +1,10 @@
 #ifndef __LWIPOPTS_H__
 #define __LWIPOPTS_H__
 
+#include <py/mpconfig.h>
+#include <py/misc.h>
+#include MICROPY_HAL_H
+
 // We're running without an OS for this port. We don't provide any services except light protection.
 #define NO_SYS 1
 
@@ -26,7 +30,7 @@ typedef uint32_t sys_prot_t;
 // For now, we can simply define this as a macro for the timer code. But this function isn't
 // universal and other ports will need to do something else. It may be necessary to move
 // things like this into a port-provided header file.
-#define sys_now HAL_GetTick
+#define sys_now mp_hal_ticks_ms
 
 #endif
 
diff --git a/extmod/modlwip.c b/extmod/modlwip.c
index 7711db1a9..ced9c06a1 100644
--- a/extmod/modlwip.c
+++ b/extmod/modlwip.c
@@ -48,9 +48,6 @@
 #include "lwip/sio.h"
 #endif
 
-// FIXME FIXME FIXME
-#define LWIP_DELAY HAL_Delay
-
 #ifdef MICROPY_PY_LWIP_SLIP
 /******************************************************************************/
 // 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_
     if (socket->incoming == NULL) {
         if (socket->timeout != -1) {
             for (mp_uint_t retries = socket->timeout / 100; retries--;) {
-                LWIP_DELAY(100);
+                mp_hal_delay_ms(100);
                 if (socket->incoming != NULL) break;
             }
             if (socket->incoming == NULL) {
@@ -333,7 +330,7 @@ STATIC mp_uint_t lwip_udp_receive(lwip_socket_obj_t *socket, byte *buf, mp_uint_
             }
         } else {
             while (socket->incoming == NULL) {
-                LWIP_DELAY(100);
+                mp_hal_delay_ms(100);
             }
         }
     }
@@ -378,7 +375,7 @@ STATIC mp_uint_t lwip_tcp_receive(lwip_socket_obj_t *socket, byte *buf, mp_uint_
     if (socket->incoming == NULL) {
         if (socket->timeout != -1) {
             for (mp_uint_t retries = socket->timeout / 100; retries--;) {
-                LWIP_DELAY(100);
+                mp_hal_delay_ms(100);
                 if (socket->incoming != NULL) break;
             }
             if (socket->incoming == NULL) {
@@ -387,7 +384,7 @@ STATIC mp_uint_t lwip_tcp_receive(lwip_socket_obj_t *socket, byte *buf, mp_uint_
             }
         } else {
             while (socket->incoming == NULL) {
-                LWIP_DELAY(100);
+                mp_hal_delay_ms(100);
             }
         }
     }
@@ -572,7 +569,7 @@ STATIC mp_obj_t lwip_socket_accept(mp_obj_t self_in) {
     if (socket->incoming == NULL) {
         if (socket->timeout != -1) {
             for (mp_uint_t retries = socket->timeout / 100; retries--;) {
-                LWIP_DELAY(100);
+                mp_hal_delay_ms(100);
                 if (socket->incoming != NULL) break;
             }
             if (socket->incoming == NULL) {
@@ -580,7 +577,7 @@ STATIC mp_obj_t lwip_socket_accept(mp_obj_t self_in) {
             }
         } else {
             while (socket->incoming == NULL) {
-                LWIP_DELAY(100);
+                mp_hal_delay_ms(100);
             }
         }
     }
@@ -656,7 +653,7 @@ STATIC mp_obj_t lwip_socket_connect(mp_obj_t self_in, mp_obj_t addr_in) {
             // And now we wait...
             if (socket->timeout != -1) {
                 for (mp_uint_t retries = socket->timeout / 100; retries--;) {
-                    LWIP_DELAY(100);
+                    mp_hal_delay_ms(100);
                     if (socket->connected != 1) break;
                 }
                 if (socket->connected == 1) {
@@ -664,7 +661,7 @@ STATIC mp_obj_t lwip_socket_connect(mp_obj_t self_in, mp_obj_t addr_in) {
                 }
             } else {
                 while (socket->connected == 1) {
-                    LWIP_DELAY(100);
+                    mp_hal_delay_ms(100);
                 }
             }
             if (socket->connected == 2) {
@@ -946,7 +943,7 @@ STATIC mp_obj_t lwip_getaddrinfo(mp_obj_t host_in, mp_obj_t port_in) {
         }
         case ERR_INPROGRESS: {
             while(!lwip_dns_returned) {
-                LWIP_DELAY(100);
+                mp_hal_delay_ms(100);
             }
             if (lwip_dns_returned == 2) {
                 nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(ENOENT)));
diff --git a/stmhal/mphal.h b/stmhal/mphal.h
index 012777b87..83b28d8d7 100644
--- a/stmhal/mphal.h
+++ b/stmhal/mphal.h
@@ -23,3 +23,6 @@ int mp_hal_stdin_rx_chr(void);
 void mp_hal_stdout_tx_str(const char *str);
 void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len);
 void mp_hal_stdout_tx_strn_cooked(const char *str, mp_uint_t len);
+
+#define mp_hal_delay_ms HAL_Delay
+#define mp_hal_ticks_ms HAL_GetTick
diff --git a/unix/unix_mphal.c b/unix/unix_mphal.c
index 0974b6ad2..54a710a3b 100644
--- a/unix/unix_mphal.c
+++ b/unix/unix_mphal.c
@@ -119,7 +119,7 @@ void mp_hal_stdout_tx_str(const char *str) {
     mp_hal_stdout_tx_strn(str, strlen(str));
 }
 
-uint32_t HAL_GetTick(void) {
+uint32_t mp_hal_ticks_ms(void) {
     struct timeval tv;
     gettimeofday(&tv, NULL);
     return tv.tv_sec * 1000 + tv.tv_usec / 1000;
diff --git a/unix/unix_mphal.h b/unix/unix_mphal.h
index 09d063575..2e10d48a6 100644
--- a/unix/unix_mphal.h
+++ b/unix/unix_mphal.h
@@ -38,5 +38,5 @@ void mp_hal_stdout_tx_str(const char *str);
 void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len);
 void mp_hal_stdout_tx_strn_cooked(const char *str, mp_uint_t len);
 
-#define HAL_Delay(ms) usleep((ms) * 1000)
-uint32_t HAL_GetTick(void);
+#define mp_hal_delay_ms(ms) usleep((ms) * 1000)
+uint32_t mp_hal_ticks_ms(void);
-- 
GitLab