|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2023 Arduino SA |
| 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 | +#include "shared/runtime/softtimer.h" |
| 30 | + |
| 31 | +#if MICROPY_PY_LWIP |
| 32 | + |
| 33 | +#include "lwip/timeouts.h" |
| 34 | + |
| 35 | +static mp_sched_node_t network_poll_node; |
| 36 | +static soft_timer_entry_t network_timer; |
| 37 | + |
| 38 | +u32_t sys_now(void) { |
| 39 | + return mp_hal_ticks_ms(); |
| 40 | +} |
| 41 | + |
| 42 | +static void network_poll(mp_sched_node_t *node) { |
| 43 | + // Run the lwIP internal updates |
| 44 | + sys_check_timeouts(); |
| 45 | + |
| 46 | + #if MICROPY_PY_NETWORK_ESP_HOSTED |
| 47 | + extern int esp_hosted_wifi_poll(void); |
| 48 | + // Poll the NIC for incoming data |
| 49 | + if (esp_hosted_wifi_poll() == -1) { |
| 50 | + soft_timer_remove(&network_timer); |
| 51 | + } |
| 52 | + #endif |
| 53 | +} |
| 54 | + |
| 55 | +void mod_network_poll_events(void) { |
| 56 | + mp_sched_schedule_node(&network_poll_node, network_poll); |
| 57 | +} |
| 58 | + |
| 59 | +static void network_timer_callback(soft_timer_entry_t *self) { |
| 60 | + mod_network_poll_events(); |
| 61 | +} |
| 62 | + |
| 63 | +void mod_network_lwip_init(void) { |
| 64 | + static bool timer_started = false; |
| 65 | + if (timer_started) { |
| 66 | + soft_timer_remove(&network_timer); |
| 67 | + timer_started = false; |
| 68 | + } |
| 69 | + // Start poll timer. |
| 70 | + soft_timer_static_init(&network_timer, SOFT_TIMER_MODE_PERIODIC, 128, network_timer_callback); |
| 71 | + soft_timer_reinsert(&network_timer, 128); |
| 72 | + timer_started = true; |
| 73 | +} |
| 74 | +#endif // MICROPY_PY_LWIP |
0 commit comments