|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2021 Damien P. George |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | + * of this software and associated documentation files (the "Software"), to deal |
| 10 | + * in the Software without restriction, including without limitation the rights |
| 11 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | + * copies of the Software, and to permit persons to whom the Software is |
| 13 | + * furnished to do so, subject to the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be included in |
| 16 | + * all copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | + * THE SOFTWARE. |
| 25 | + */ |
| 26 | + |
| 27 | +#include "py/runtime.h" |
| 28 | +#include "py/mphal.h" |
| 29 | + |
| 30 | +static struct k_poll_signal wait_signal; |
| 31 | +static struct k_poll_event wait_events[2] = { |
| 32 | + K_POLL_EVENT_INITIALIZER(K_POLL_TYPE_SIGNAL, |
| 33 | + K_POLL_MODE_NOTIFY_ONLY, |
| 34 | + &wait_signal), |
| 35 | + K_POLL_EVENT_STATIC_INITIALIZER(K_POLL_TYPE_SEM_AVAILABLE, |
| 36 | + K_POLL_MODE_NOTIFY_ONLY, |
| 37 | + NULL, 0), |
| 38 | +}; |
| 39 | + |
| 40 | +void mp_hal_init(void) { |
| 41 | + k_poll_signal_init(&wait_signal); |
| 42 | +} |
| 43 | + |
| 44 | +void mp_hal_signal_event(void) { |
| 45 | + k_poll_signal_raise(&wait_signal, 0); |
| 46 | +} |
| 47 | + |
| 48 | +void mp_hal_wait_sem(struct k_sem *sem, uint32_t timeout_ms) { |
| 49 | + mp_uint_t t0 = mp_hal_ticks_ms(); |
| 50 | + if (sem) { |
| 51 | + k_poll_event_init(&wait_events[1], K_POLL_TYPE_SEM_AVAILABLE, K_POLL_MODE_NOTIFY_ONLY, sem); |
| 52 | + } |
| 53 | + for (;;) { |
| 54 | + k_timeout_t wait; |
| 55 | + if (timeout_ms == (uint32_t)-1) { |
| 56 | + wait = K_FOREVER; |
| 57 | + } else { |
| 58 | + uint32_t dt = mp_hal_ticks_ms() - t0; |
| 59 | + if (dt >= timeout_ms) { |
| 60 | + return; |
| 61 | + } |
| 62 | + wait = K_MSEC(timeout_ms - dt); |
| 63 | + } |
| 64 | + k_poll(wait_events, sem ? 2 : 1, wait); |
| 65 | + if (wait_events[0].state == K_POLL_STATE_SIGNALED) { |
| 66 | + wait_events[0].signal->signaled = 0; |
| 67 | + wait_events[0].state = K_POLL_STATE_NOT_READY; |
| 68 | + mp_handle_pending(true); |
| 69 | + } else if (sem && wait_events[1].state == K_POLL_STATE_SEM_AVAILABLE) { |
| 70 | + wait_events[1].state = K_POLL_STATE_NOT_READY; |
| 71 | + return; |
| 72 | + } |
| 73 | + } |
| 74 | +} |
0 commit comments