|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2025 Damien P. George, Angus Gratton |
| 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 | +#ifndef MICROPY_INCLUDED_EXTMOD_CYW43_CONFIG_COMMON_H |
| 27 | +#define MICROPY_INCLUDED_EXTMOD_CYW43_CONFIG_COMMON_H |
| 28 | + |
| 29 | +// The board-level config will be included here, so it can set some CYW43 values. |
| 30 | +#include "py/mpconfig.h" |
| 31 | +#include "py/mperrno.h" |
| 32 | +#include "py/mphal.h" |
| 33 | +#include "py/runtime.h" |
| 34 | +#include "extmod/modnetwork.h" |
| 35 | +#include "lwip/apps/mdns.h" |
| 36 | +#include "pendsv.h" |
| 37 | + |
| 38 | +// This file is included at the top of port-specific cyw43_configport.h files, |
| 39 | +// and holds the MicroPython-specific but non-port-specific parts. |
| 40 | +// |
| 41 | +// It's included into both MicroPython sources and the lib/cyw43-driver sources. |
| 42 | + |
| 43 | +#define CYW43_IOCTL_TIMEOUT_US (1000000) |
| 44 | +#define CYW43_NETUTILS (1) |
| 45 | +#define CYW43_PRINTF(...) mp_printf(MP_PYTHON_PRINTER, __VA_ARGS__) |
| 46 | + |
| 47 | +#define CYW43_EPERM MP_EPERM // Operation not permitted |
| 48 | +#define CYW43_EIO MP_EIO // I/O error |
| 49 | +#define CYW43_EINVAL MP_EINVAL // Invalid argument |
| 50 | +#define CYW43_ETIMEDOUT MP_ETIMEDOUT // Connection timed out |
| 51 | + |
| 52 | +#define CYW43_THREAD_ENTER MICROPY_PY_LWIP_ENTER |
| 53 | +#define CYW43_THREAD_EXIT MICROPY_PY_LWIP_EXIT |
| 54 | +#define CYW43_THREAD_LOCK_CHECK |
| 55 | + |
| 56 | +#define CYW43_HOST_NAME mod_network_hostname_data |
| 57 | + |
| 58 | +#define CYW43_ARRAY_SIZE(a) MP_ARRAY_SIZE(a) |
| 59 | + |
| 60 | +#define CYW43_HAL_PIN_MODE_INPUT MP_HAL_PIN_MODE_INPUT |
| 61 | +#define CYW43_HAL_PIN_MODE_OUTPUT MP_HAL_PIN_MODE_OUTPUT |
| 62 | +#define CYW43_HAL_PIN_PULL_NONE MP_HAL_PIN_PULL_NONE |
| 63 | +#define CYW43_HAL_PIN_PULL_UP MP_HAL_PIN_PULL_UP |
| 64 | +#define CYW43_HAL_PIN_PULL_DOWN MP_HAL_PIN_PULL_DOWN |
| 65 | + |
| 66 | +#define CYW43_HAL_MAC_WLAN0 MP_HAL_MAC_WLAN0 |
| 67 | +#define CYW43_HAL_MAC_BDADDR MP_HAL_MAC_BDADDR |
| 68 | + |
| 69 | +#define cyw43_hal_ticks_us mp_hal_ticks_us |
| 70 | +#define cyw43_hal_ticks_ms mp_hal_ticks_ms |
| 71 | + |
| 72 | +#define cyw43_hal_pin_obj_t mp_hal_pin_obj_t |
| 73 | +#define cyw43_hal_pin_config mp_hal_pin_config |
| 74 | +#define cyw43_hal_pin_read mp_hal_pin_read |
| 75 | +#define cyw43_hal_pin_low mp_hal_pin_low |
| 76 | +#define cyw43_hal_pin_high mp_hal_pin_high |
| 77 | + |
| 78 | +#define cyw43_hal_get_mac mp_hal_get_mac |
| 79 | +#define cyw43_hal_get_mac_ascii mp_hal_get_mac_ascii |
| 80 | +#define cyw43_hal_generate_laa_mac mp_hal_generate_laa_mac |
| 81 | + |
| 82 | +#define cyw43_schedule_internal_poll_dispatch(func) pendsv_schedule_dispatch(PENDSV_DISPATCH_CYW43, func) |
| 83 | + |
| 84 | +// Note: this function is only called if CYW43_POST_POLL_HOOK is defined in cyw43_configport.h |
| 85 | +void cyw43_post_poll_hook(void); |
| 86 | + |
| 87 | +#ifdef MICROPY_EVENT_POLL_HOOK |
| 88 | +// Older style hook macros on some ports |
| 89 | +#define CYW43_EVENT_POLL_HOOK MICROPY_EVENT_POLL_HOOK |
| 90 | +#else |
| 91 | +// Newer style hooks on other ports |
| 92 | +#define CYW43_EVENT_POLL_HOOK mp_event_handle_nowait() |
| 93 | +#endif |
| 94 | + |
| 95 | +static inline void cyw43_delay_us(uint32_t us) { |
| 96 | + uint32_t start = mp_hal_ticks_us(); |
| 97 | + while (mp_hal_ticks_us() - start < us) { |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +static inline void cyw43_delay_ms(uint32_t ms) { |
| 102 | + // PendSV may be disabled via CYW43_THREAD_ENTER, so this delay is a busy loop. |
| 103 | + uint32_t us = ms * 1000; |
| 104 | + uint32_t start = mp_hal_ticks_us(); |
| 105 | + while (mp_hal_ticks_us() - start < us) { |
| 106 | + CYW43_EVENT_POLL_HOOK; |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +#endif // MICROPY_INCLUDED_EXTMOD_CYW43_CONFIG_COMMON_H |
0 commit comments