Kaynağa Gözat

【完善】多线程并发机制。

Signed-off-by: armink <armink.ztl@gmail.com>
armink 7 yıl önce
ebeveyn
işleme
da941825da
7 değiştirilmiş dosya ile 148 ekleme ve 104 silme
  1. 0 26
      lib/utils/stdout_helpers.c
  2. 16 2
      port/mpconfigport.h
  3. 123 0
      port/mphalport.c
  4. 0 20
      port/mphalport.h
  5. 0 1
      port/mpthreadport.c
  6. 9 7
      port/rtt_getchar.c
  7. 0 48
      port/uart_core.c

+ 0 - 26
lib/utils/stdout_helpers.c

@@ -1,26 +0,0 @@
-#include <string.h>
-#include <unistd.h>
-#include "py/mpconfig.h"
-#include "py/mphal.h"
-
-/*
- * Extra stdout functions
- * These can be either optimized for a particular port, or reference
- * implementation below can be used.
- */
-
-// Send "cooked" string of given length, where every occurrence of
-// LF character is replaced with CR LF.
-void mp_hal_stdout_tx_strn_cooked(const char *str, size_t len) {
-    while (len--) {
-        if (*str == '\n') {
-            mp_hal_stdout_tx_strn("\r", 1);
-        }
-        mp_hal_stdout_tx_strn(str++, 1);
-    }
-}
-
-// Send zero-terminated string
-void mp_hal_stdout_tx_str(const char *str) {
-    mp_hal_stdout_tx_strn(str, strlen(str));
-}

+ 16 - 2
port/mpconfigport.h

@@ -226,8 +226,22 @@
 #define MICROPY_PY_USSL             (0)
 #define MICROPY_SSL_MBEDTLS         (0)
 
-#define MICROPY_EVENT_POLL_HOOK rt_thread_delay(1);
-#define MICROPY_THREAD_YIELD() rt_thread_delay(1)
+#if MICROPY_PY_THREAD
+#define MICROPY_EVENT_POLL_HOOK \
+    do { \
+        extern void mp_handle_pending(void); \
+        mp_handle_pending(); \
+        MP_THREAD_GIL_EXIT(); \
+        MP_THREAD_GIL_ENTER(); \
+    } while (0);
+#else
+#define MICROPY_EVENT_POLL_HOOK \
+    do { \
+        extern void mp_handle_pending(void); \
+        mp_handle_pending(); \
+        rt_thread_delay(1); \
+    } while (0);
+#endif
 
 #if defined(__CC_ARM)
 #include <sys/types.h>

+ 123 - 0
port/mphalport.c

@@ -0,0 +1,123 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2018 Armink (armink.ztl@gmail.com)
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include <stdio.h>
+
+#include <rtthread.h>
+#include <py/mpconfig.h>
+#include <py/runtime.h>
+#include "mphalport.h"
+#include "rtt_getchar.h"
+
+int mp_hal_stdin_rx_chr(void) {
+    char ch;
+    while (1) {
+        ch = rtt_getchar();
+        if (ch != 0xFF) {
+            break;
+        }
+        MICROPY_EVENT_POLL_HOOK;
+        rt_thread_delay(1);
+    }
+    return ch;
+}
+
+void mp_hal_stdout_tx_char(char c) {
+    rt_device_t console;
+
+    console = rt_console_get_device();
+    rt_device_write(console, 0, &c, 1);
+}
+
+void mp_hal_stdout_tx_str(const char *str) {
+    MP_THREAD_GIL_EXIT();
+    while (*str) {
+        mp_hal_stdout_tx_char(*str++);
+    }
+    MP_THREAD_GIL_ENTER();
+}
+
+void mp_hal_stdout_tx_strn_cooked(const char *str, uint32_t len) {
+    MP_THREAD_GIL_EXIT();
+    while (len--) {
+        if (*str == '\n') {
+            mp_hal_stdout_tx_char('\r');
+        }
+        mp_hal_stdout_tx_char(*str++);
+    }
+    MP_THREAD_GIL_ENTER();
+}
+
+// Send string of given length
+void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
+    rt_device_t console;
+
+    console = rt_console_get_device();
+    if (console) {
+        MP_THREAD_GIL_EXIT();
+        rt_device_write(console, 0, str, len);
+        MP_THREAD_GIL_ENTER();
+    }
+}
+
+mp_uint_t mp_hal_ticks_us(void) {
+    return rt_tick_get() * 1000;
+}
+
+mp_uint_t mp_hal_ticks_ms(void) {
+    return rt_tick_get() * 1000 / RT_TICK_PER_SECOND;
+}
+
+mp_uint_t mp_hal_ticks_cpu(void) {
+    return rt_tick_get();
+}
+
+void mp_hal_delay_us(mp_uint_t us) {
+    rt_tick_t t0 = rt_tick_get(), t1, dt;
+    uint64_t dtick = us * RT_TICK_PER_SECOND / 1000000L;
+    while (1) {
+        t1 = rt_tick_get();
+        dt = t1 - t0;
+        if (dt >= dtick) {
+            break;
+        }
+        mp_handle_pending();
+    }
+}
+
+void mp_hal_delay_ms(mp_uint_t ms) {
+    rt_tick_t t0 = rt_tick_get(), t1, dt;
+    uint64_t dtick = ms * RT_TICK_PER_SECOND / 1000L;
+    while (1) {
+        t1 = rt_tick_get();
+        dt = t1 - t0;
+        if (dt >= dtick) {
+            break;
+        }
+        MICROPY_EVENT_POLL_HOOK;
+        rt_thread_delay(1);
+    }
+}

+ 0 - 20
port/mphalport.h

@@ -27,26 +27,6 @@
 #include <rtthread.h>
 #include <drivers/pin.h>
 
-static inline mp_uint_t mp_hal_ticks_us(void) {
-    return rt_tick_get() * 1000;
-}
-
-static inline mp_uint_t mp_hal_ticks_ms(void) {
-    return rt_tick_get() * 1000 / RT_TICK_PER_SECOND;
-}
-
-static inline mp_uint_t mp_hal_ticks_cpu(void) {
-    return rt_tick_get();
-}
-
-static inline void mp_hal_delay_us(mp_uint_t delay) {
-    rt_thread_delay(rt_tick_from_millisecond(delay / 1000));
-}
-
-static inline void mp_hal_delay_ms(mp_uint_t delay) {
-    rt_thread_delay(rt_tick_from_millisecond(delay));
-}
-
 #define MP_HAL_PIN_FMT                 "%s"
 
 extern void mp_hal_set_interrupt_char (int c);

+ 0 - 1
port/mpthreadport.c

@@ -131,7 +131,6 @@ void mp_thread_create_ex(void *(*entry)(void*), void *arg, size_t *stack_size, i
 
         level = rt_hw_interrupt_disable();
 
-
         node->thread = th;
         rt_list_insert_before(&thread_list, &(node->list));
 

+ 9 - 7
port/rtt_getchar.c

@@ -33,13 +33,13 @@
 
 #define UART_FIFO_SIZE 256
 
-static struct rt_semaphore notice;
 static struct rt_ringbuffer *rx_fifo = NULL;
 static rt_err_t (*odev_rx_ind)(rt_device_t dev, rt_size_t size) = NULL;
 
 static rt_err_t getchar_rx_ind(rt_device_t dev, rt_size_t size) {
     uint8_t ch;
     rt_size_t i;
+    rt_base_t int_lvl;
 
     for (i = 0; i < size; i++) {
         /* read a char */
@@ -47,8 +47,9 @@ static rt_err_t getchar_rx_ind(rt_device_t dev, rt_size_t size) {
             if (ch == mp_interrupt_char) {
                 mp_keyboard_interrupt();
             } else {
+                int_lvl = rt_hw_interrupt_disable();
                 rt_ringbuffer_put_force(rx_fifo, &ch, 1);
-                rt_sem_release(&notice);
+                rt_hw_interrupt_enable(int_lvl);
             }
         }
     }
@@ -59,8 +60,6 @@ void rtt_getchar_init(void) {
     rt_base_t int_lvl;
     rt_device_t console;
 
-    rt_sem_init(&notice, "uart_notice", 0, RT_IPC_FLAG_FIFO);
-
     /* create RX FIFO */
     rx_fifo = rt_ringbuffer_create(UART_FIFO_SIZE);
     /* created must success */
@@ -81,7 +80,6 @@ void rtt_getchar_deinit(void) {
     rt_base_t int_lvl;
     rt_device_t console;
 
-    rt_sem_detach(&notice);
     rt_ringbuffer_destroy(rx_fifo);
 
     int_lvl = rt_hw_interrupt_disable();
@@ -95,9 +93,13 @@ void rtt_getchar_deinit(void) {
 
 int rtt_getchar(void) {
     uint8_t ch;
+    rt_base_t int_lvl;
 
-    rt_sem_take(&notice, RT_WAITING_FOREVER);
-    rt_ringbuffer_getchar(rx_fifo, &ch);
+    int_lvl = rt_hw_interrupt_disable();
+    if (!rt_ringbuffer_getchar(rx_fifo, &ch)) {
+        ch = 0xFF;
+    }
+    rt_hw_interrupt_enable(int_lvl);
 
     return ch;
 

+ 0 - 48
port/uart_core.c

@@ -1,48 +0,0 @@
-/*
- * This file is part of the MicroPython project, http://micropython.org/
- *
- * The MIT License (MIT)
- *
- * Copyright (c) 2017 Armink (armink.ztl@gmail.com)
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- */
-
-#include "py/mpconfig.h"
-#include "rtt_getchar.h"
-#include <rtthread.h>
-
-/*
- * Core UART functions to implement for a port
- */
-
-// Receive single character
-int mp_hal_stdin_rx_chr(void) {
-    return rtt_getchar();
-}
-
-// Send string of given length
-void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
-    rt_device_t console;
-
-    console = rt_console_get_device();
-    if (console) {
-        rt_device_write(console, 0, str, len);
-    }
-}