Skip to content

Commit b2cda6c

Browse files
committed
extmod,alif,mimxrt,rp2,stm32: Create common cyw43 driver config header.
This is only a surface level refactor, some deeper refactoring would be possible with (for example) the SDIO interface in mimxrt and stm32, or the BTHCI interface which is is similar on supported ports. But sticking to cases where the macros are the same across all ports. This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <[email protected]>
1 parent 45e4deb commit b2cda6c

File tree

6 files changed

+135
-244
lines changed

6 files changed

+135
-244
lines changed

extmod/cyw43_config_common.h

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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

extmod/mpbthci.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
#ifndef MICROPY_INCLUDED_EXTMOD_MPBTHCI_H
2828
#define MICROPY_INCLUDED_EXTMOD_MPBTHCI_H
2929

30+
#include <stdint.h>
31+
#include <stdbool.h>
32+
#include <stddef.h>
33+
3034
#define MICROPY_PY_BLUETOOTH_HCI_READ_MODE_BYTE (0)
3135
#define MICROPY_PY_BLUETOOTH_HCI_READ_MODE_PACKET (1)
3236

ports/alif/cyw43_configport.h

Lines changed: 2 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,8 @@
2727
#define MICROPY_INCLUDED_ALIF_CYW43_CONFIGPORT_H
2828

2929
// 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"
3530
#include "extmod/mpbthci.h"
36-
#include "pendsv.h"
31+
#include "extmod/cyw43_config_common.h"
3732

3833
#ifndef static_assert
3934
#define static_assert(expr, msg) typedef int static_assert_##__LINE__[(expr) ? 1 : -1]
@@ -53,51 +48,15 @@
5348
#define CYW43_WARN(...) mp_printf(MP_PYTHON_PRINTER, __VA_ARGS__)
5449
#endif
5550

56-
#define CYW43_IOCTL_TIMEOUT_US (1000000)
57-
#define CYW43_SLEEP_MAX (50)
58-
#define CYW43_NETUTILS (1)
5951
#define CYW43_CLEAR_SDIO_INT (1)
6052

61-
#define CYW43_EPERM MP_EPERM // Operation not permitted
62-
#define CYW43_EIO MP_EIO // I/O error
63-
#define CYW43_EINVAL MP_EINVAL // Invalid argument
64-
#define CYW43_ETIMEDOUT MP_ETIMEDOUT // Connection timed out
65-
66-
#define CYW43_THREAD_ENTER MICROPY_PY_LWIP_ENTER
67-
#define CYW43_THREAD_EXIT MICROPY_PY_LWIP_EXIT
68-
#define CYW43_THREAD_LOCK_CHECK
69-
70-
#define CYW43_HOST_NAME mod_network_hostname_data
71-
7253
#define CYW43_SDPCM_SEND_COMMON_WAIT __WFE()
7354
#define CYW43_DO_IOCTL_WAIT // __WFE()
74-
#define CYW43_EVENT_POLL_HOOK mp_event_handle_nowait()
75-
76-
#define CYW43_ARRAY_SIZE(a) MP_ARRAY_SIZE(a)
77-
78-
#define CYW43_HAL_PIN_MODE_INPUT MP_HAL_PIN_MODE_INPUT
79-
#define CYW43_HAL_PIN_MODE_OUTPUT MP_HAL_PIN_MODE_OUTPUT
80-
#define CYW43_HAL_PIN_PULL_NONE 0
81-
82-
#define CYW43_HAL_MAC_WLAN0 MP_HAL_MAC_WLAN0
83-
#define CYW43_HAL_MAC_BDADDR MP_HAL_MAC_BDADDR
84-
85-
#define cyw43_hal_ticks_us mp_hal_ticks_us
86-
#define cyw43_hal_ticks_ms mp_hal_ticks_ms
87-
88-
#define cyw43_hal_pin_obj_t mp_hal_pin_obj_t
89-
#define cyw43_hal_pin_read mp_hal_pin_read
90-
#define cyw43_hal_pin_low mp_hal_pin_low
91-
#define cyw43_hal_pin_high mp_hal_pin_high
9255

9356
#define cyw43_hal_uart_set_baudrate mp_bluetooth_hci_uart_set_baudrate
9457
#define cyw43_hal_uart_write mp_bluetooth_hci_uart_write
9558
#define cyw43_hal_uart_readchar mp_bluetooth_hci_uart_readchar
9659

97-
#define cyw43_hal_get_mac mp_hal_get_mac
98-
#define cyw43_hal_get_mac_ascii mp_hal_get_mac_ascii
99-
#define cyw43_hal_generate_laa_mac mp_hal_generate_laa_mac
100-
10160
#define CYW43_PIN_WL_REG_ON pin_WL_REG_ON
10261
#define CYW43_PIN_WL_IRQ pin_WL_IRQ
10362

@@ -110,15 +69,7 @@
11069

11170
void cyw43_post_poll_hook(void);
11271

113-
static inline void cyw43_delay_us(uint32_t us) {
114-
uint32_t start = mp_hal_ticks_us();
115-
while (mp_hal_ticks_us() - start < us) {
116-
}
117-
}
118-
119-
static inline void cyw43_delay_ms(uint32_t ms) {
120-
mp_hal_delay_ms(ms);
121-
}
72+
#undef cyw43_hal_pin_config // mp_hal_pin_config on alif port is not API compatible
12273

12374
static inline void cyw43_hal_pin_config(mp_hal_pin_obj_t pin, uint32_t mode, uint32_t pull, uint32_t alt) {
12475
if (mode == MP_HAL_PIN_MODE_INPUT) {
@@ -127,7 +78,6 @@ static inline void cyw43_hal_pin_config(mp_hal_pin_obj_t pin, uint32_t mode, uin
12778
mp_hal_pin_output(pin);
12879
}
12980
}
130-
13181
static inline void cyw43_hal_pin_config_irq_falling(mp_hal_pin_obj_t pin, bool enable) {
13282
mp_hal_pin_config_irq_falling(pin, enable);
13383
}

ports/mimxrt/cyw43_configport.h

Lines changed: 2 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,8 @@
2828
#define MICROPY_INCLUDED_MIMXRT_CYW43_CONFIGPORT_H
2929

3030
// The board-level config will be included here, so it can set some CYW43 values.
31-
#include "py/mpconfig.h"
32-
#include "py/mperrno.h"
33-
#include "py/mphal.h"
34-
#include "extmod/modnetwork.h"
3531
#include "extmod/mpbthci.h"
36-
#include "pendsv.h"
32+
#include "extmod/cyw43_config_common.h"
3733
#include "sdio.h"
3834

3935
#define CYW43_USE_SPI (0)
@@ -61,55 +57,15 @@
6157
#define CYW43_BT_UART_BAUDRATE_DOWNLOAD_FIRMWARE MICROPY_HW_BLE_UART_BAUDRATE_DOWNLOAD_FIRMWARE
6258
#endif
6359

64-
#define CYW43_IOCTL_TIMEOUT_US (1000000)
65-
#define CYW43_SLEEP_MAX (50)
66-
#define CYW43_NETUTILS (1)
6760
#define CYW43_CLEAR_SDIO_INT (1)
6861

69-
#define CYW43_EPERM MP_EPERM // Operation not permitted
70-
#define CYW43_EIO MP_EIO // I/O error
71-
#define CYW43_EINVAL MP_EINVAL // Invalid argument
72-
#define CYW43_ETIMEDOUT MP_ETIMEDOUT // Connection timed out
73-
74-
#define CYW43_THREAD_ENTER MICROPY_PY_LWIP_ENTER
75-
#define CYW43_THREAD_EXIT MICROPY_PY_LWIP_EXIT
76-
#define CYW43_THREAD_LOCK_CHECK
77-
78-
#define CYW43_HOST_NAME mod_network_hostname_data
79-
8062
#define CYW43_SDPCM_SEND_COMMON_WAIT __WFI();
8163
#define CYW43_DO_IOCTL_WAIT __WFI();
8264

83-
#define CYW43_ARRAY_SIZE(a) MP_ARRAY_SIZE(a)
84-
85-
#define CYW43_HAL_PIN_MODE_INPUT MP_HAL_PIN_MODE_INPUT
86-
#define CYW43_HAL_PIN_MODE_OUTPUT MP_HAL_PIN_MODE_OUTPUT
87-
#define CYW43_HAL_PIN_PULL_NONE MP_HAL_PIN_PULL_NONE
88-
#define CYW43_HAL_PIN_PULL_UP MP_HAL_PIN_PULL_UP
89-
#define CYW43_HAL_PIN_PULL_DOWN MP_HAL_PIN_PULL_DOWN
90-
91-
#define CYW43_HAL_MAC_WLAN0 MP_HAL_MAC_WLAN0
92-
#define CYW43_HAL_MAC_BDADDR MP_HAL_MAC_BDADDR
93-
94-
#define cyw43_hal_ticks_us mp_hal_ticks_us
95-
#define cyw43_hal_ticks_ms mp_hal_ticks_ms
96-
97-
#define cyw43_hal_pin_obj_t mp_hal_pin_obj_t
98-
#define cyw43_hal_pin_read mp_hal_pin_read
99-
#define cyw43_hal_pin_low mp_hal_pin_low
100-
#define cyw43_hal_pin_high mp_hal_pin_high
101-
102-
#define cyw43_hal_get_mac mp_hal_get_mac
103-
#define cyw43_hal_get_mac_ascii mp_hal_get_mac_ascii
104-
#define cyw43_hal_generate_laa_mac mp_hal_generate_laa_mac
105-
10665
#define cyw43_hal_uart_set_baudrate mp_bluetooth_hci_uart_set_baudrate
10766
#define cyw43_hal_uart_write mp_bluetooth_hci_uart_write
10867
#define cyw43_hal_uart_readchar mp_bluetooth_hci_uart_readchar
10968

110-
#define cyw43_delay_us mp_hal_delay_us
111-
#define cyw43_delay_ms mp_hal_delay_ms
112-
11369
#define cyw43_bluetooth_controller_init mp_bluetooth_hci_controller_init
11470
#define cyw43_bluetooth_controller_deinit mp_bluetooth_hci_controller_deinit
11571
#define cyw43_bluetooth_controller_woken mp_bluetooth_hci_controller_woken
@@ -135,7 +91,7 @@
13591
#define CYW43_PIN_RFSW_VDD pin_WL_RFSW_VDD
13692
#endif
13793

138-
#define cyw43_schedule_internal_poll_dispatch(func) pendsv_schedule_dispatch(PENDSV_DISPATCH_CYW43, func)
94+
#undef cyw43_hal_pin_config // mp_hal_pin_config not yet implemented on this port
13995

14096
static inline void cyw43_hal_pin_config(cyw43_hal_pin_obj_t pin, uint8_t mode, uint8_t pull, uint8_t alt) {
14197
machine_pin_set_mode(pin, mode);
@@ -175,6 +131,4 @@ static inline int cyw43_sdio_transfer_cmd53(bool write, uint32_t block_size, uin
175131
return sdio_transfer_cmd53(write, block_size, arg, len, buf);
176132
}
177133

178-
#define CYW43_EVENT_POLL_HOOK MICROPY_EVENT_POLL_HOOK
179-
180134
#endif // MICROPY_INCLUDED_MIMXRT_CYW43_CONFIGPORT_H

0 commit comments

Comments
 (0)