Skip to content

Commit 772a360

Browse files
iabdalkaderdpgeorge
authored andcommitted
mimxrt: Integrate support for WiFi via the CYW43 driver.
This commit adds the necessary configuration and hooks to get the CYW43 driver working with the mimxrt port. Signed-off-by: iabdalkader <i.abdalkader@gmail.com>
1 parent 46d83d9 commit 772a360

File tree

7 files changed

+209
-0
lines changed

7 files changed

+209
-0
lines changed

ports/mimxrt/board_init.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ void board_init(void) {
6363

6464
// 1ms tick timer
6565
SysTick_Config(SystemCoreClock / 1000);
66+
NVIC_SetPriority(SysTick_IRQn, NVIC_EncodePriority(NVIC_PRIORITYGROUP_4, 0, 0));
6667

6768
// USB0
6869
usb_phy0_init(0b0111, 0b0110, 0b0110); // Configure nominal values for D_CAL and TXCAL45DP/DN

ports/mimxrt/cyw43_configport.h

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2022 Damien P. George
7+
* Copyright (c) 2022 Jim Mussared
8+
*
9+
* Permission is hereby granted, free of charge, to any person obtaining a copy
10+
* of this software and associated documentation files (the "Software"), to deal
11+
* in the Software without restriction, including without limitation the rights
12+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
* copies of the Software, and to permit persons to whom the Software is
14+
* furnished to do so, subject to the following conditions:
15+
*
16+
* The above copyright notice and this permission notice shall be included in
17+
* all copies or substantial portions of the Software.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
* THE SOFTWARE.
26+
*/
27+
#ifndef MICROPY_INCLUDED_STM32_CYW43_CONFIGPORT_H
28+
#define MICROPY_INCLUDED_STM32_CYW43_CONFIGPORT_H
29+
30+
// 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"
35+
#include "pendsv.h"
36+
#include "sdio.h"
37+
38+
#define CYW43_USE_SPI (0)
39+
#define CYW43_LWIP (1)
40+
#define CYW43_USE_STATS (0)
41+
42+
#ifndef CYW43_CHIPSET_FIRMWARE_INCLUDE_FILE
43+
#define CYW43_CHIPSET_FIRMWARE_INCLUDE_FILE "lib/cyw43-driver/firmware/w4343WA1_7_45_98_50_combined.h"
44+
#endif
45+
46+
#ifndef CYW43_WIFI_NVRAM_INCLUDE_FILE
47+
#define CYW43_WIFI_NVRAM_INCLUDE_FILE "lib/cyw43-driver/firmware/wifi_nvram_1dx.h"
48+
#endif
49+
50+
#define CYW43_IOCTL_TIMEOUT_US (1000000)
51+
#define CYW43_SLEEP_MAX (50)
52+
#define CYW43_NETUTILS (1)
53+
#define CYW43_CLEAR_SDIO_INT (1)
54+
55+
#define CYW43_EPERM MP_EPERM // Operation not permitted
56+
#define CYW43_EIO MP_EIO // I/O error
57+
#define CYW43_EINVAL MP_EINVAL // Invalid argument
58+
#define CYW43_ETIMEDOUT MP_ETIMEDOUT // Connection timed out
59+
60+
#define CYW43_THREAD_ENTER MICROPY_PY_LWIP_ENTER
61+
#define CYW43_THREAD_EXIT MICROPY_PY_LWIP_EXIT
62+
#define CYW43_THREAD_LOCK_CHECK
63+
64+
#define CYW43_HOST_NAME mod_network_hostname
65+
66+
#define CYW43_SDPCM_SEND_COMMON_WAIT __WFI();
67+
#define CYW43_DO_IOCTL_WAIT __WFI();
68+
69+
#define CYW43_ARRAY_SIZE(a) MP_ARRAY_SIZE(a)
70+
71+
#define CYW43_HAL_PIN_MODE_INPUT MP_HAL_PIN_MODE_INPUT
72+
#define CYW43_HAL_PIN_MODE_OUTPUT MP_HAL_PIN_MODE_OUTPUT
73+
#define CYW43_HAL_PIN_PULL_NONE MP_HAL_PIN_PULL_NONE
74+
#define CYW43_HAL_PIN_PULL_UP MP_HAL_PIN_PULL_UP
75+
#define CYW43_HAL_PIN_PULL_DOWN MP_HAL_PIN_PULL_DOWN
76+
77+
#define CYW43_HAL_MAC_WLAN0 MP_HAL_MAC_WLAN0
78+
79+
#define cyw43_hal_ticks_us mp_hal_ticks_us
80+
#define cyw43_hal_ticks_ms mp_hal_ticks_ms
81+
82+
#define cyw43_hal_pin_obj_t mp_hal_pin_obj_t
83+
#define cyw43_hal_pin_read mp_hal_pin_read
84+
#define cyw43_hal_pin_low mp_hal_pin_low
85+
#define cyw43_hal_pin_high mp_hal_pin_high
86+
87+
#define cyw43_hal_get_mac mp_hal_get_mac
88+
#define cyw43_hal_get_mac_ascii mp_hal_get_mac_ascii
89+
#define cyw43_hal_generate_laa_mac mp_hal_generate_laa_mac
90+
91+
#define cyw43_delay_us mp_hal_delay_us
92+
#define cyw43_delay_ms mp_hal_delay_ms
93+
94+
#define CYW43_PIN_WL_REG_ON MICROPY_HW_WL_REG_ON
95+
#define CYW43_PIN_WL_HOST_WAKE MICROPY_HW_WL_HOST_WAKE
96+
#define CYW43_PIN_WL_SDIO_1 MICROPY_HW_SDIO_D1
97+
98+
#define CYW43_PIN_BT_REG_ON MICROPY_HW_BT_REG_ON
99+
#ifdef MICROPY_HW_BT_HOST_WAKE
100+
#define CYW43_PIN_BT_HOST_WAKE MICROPY_HW_BT_HOST_WAKE
101+
#endif
102+
#ifdef MICROPY_HW_BT_DEV_WAKE
103+
#define CYW43_PIN_BT_DEV_WAKE MICROPY_HW_BT_DEV_WAKE
104+
#endif
105+
#ifdef MICROPY_HW_BT_CTS
106+
#define CYW43_PIN_BT_CTS MICROPY_HW_BT_CTS
107+
#endif
108+
109+
#if MICROPY_HW_ENABLE_RF_SWITCH
110+
#define CYW43_PIN_WL_RFSW_VDD pin_WL_RFSW_VDD
111+
#endif
112+
113+
#define cyw43_schedule_internal_poll_dispatch(func) pendsv_schedule_dispatch(PENDSV_DISPATCH_CYW43, func)
114+
115+
static inline void cyw43_hal_pin_config(cyw43_hal_pin_obj_t pin, uint8_t mode, uint8_t pull, uint8_t alt) {
116+
machine_pin_set_mode(pin, mode);
117+
}
118+
119+
static inline void cyw43_hal_pin_config_irq_falling(cyw43_hal_pin_obj_t pin, int enable) {
120+
if (enable) {
121+
machine_pin_config(pin, PIN_MODE_IT_FALLING, PIN_PULL_UP_100K, 0, 0, PIN_AF_MODE_ALT5);
122+
}
123+
}
124+
125+
static inline void cyw43_sdio_init(void) {
126+
sdio_init(NVIC_EncodePriority(NVIC_PRIORITYGROUP_4, 14, 0));
127+
}
128+
129+
static inline void cyw43_sdio_reinit(void) {
130+
sdio_reenable();
131+
}
132+
133+
static inline void cyw43_sdio_deinit(void) {
134+
sdio_deinit();
135+
}
136+
137+
static inline void cyw43_sdio_set_irq(bool enable) {
138+
sdio_enable_irq(enable);
139+
}
140+
141+
static inline void cyw43_sdio_enable_high_speed_4bit(void) {
142+
sdio_enable_high_speed_4bit();
143+
}
144+
145+
static inline int cyw43_sdio_transfer(uint32_t cmd, uint32_t arg, uint32_t *resp) {
146+
return sdio_transfer(cmd, arg, resp);
147+
}
148+
149+
static inline int cyw43_sdio_transfer_cmd53(bool write, uint32_t block_size, uint32_t arg, size_t len, uint8_t *buf) {
150+
return sdio_transfer_cmd53(write, block_size, arg, len, buf);
151+
}
152+
153+
#define CYW43_EVENT_POLL_HOOK MICROPY_EVENT_POLL_HOOK
154+
155+
#endif // MICROPY_INCLUDED_STM32_CYW43_CONFIGPORT_H

ports/mimxrt/machine_pin.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
#include "shared/runtime/mpirq.h"
3535
#include "extmod/virtpin.h"
3636
#include "pin.h"
37+
#if MICROPY_PY_NETWORK_CYW43
38+
#include "pendsv.h"
39+
#endif
3740

3841
// Local functions
3942
STATIC mp_obj_t machine_pin_obj_init_helper(const machine_pin_obj_t *self, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
@@ -98,6 +101,16 @@ void call_handler(GPIO_Type *gpio, int gpio_nr, int pin) {
98101
if (isr & mask) {
99102
gpio->ISR = mask; // clear the ISR flag
100103
int index = GET_PIN_IRQ_INDEX(gpio_nr, pin);
104+
#if MICROPY_PY_NETWORK_CYW43
105+
extern void (*cyw43_poll)(void);
106+
const machine_pin_obj_t *pin = MICROPY_HW_WL_HOST_WAKE;
107+
if (pin->gpio == gpio && pin->pin == (index % 32)) {
108+
if (cyw43_poll) {
109+
pendsv_schedule_dispatch(PENDSV_DISPATCH_CYW43, cyw43_poll);
110+
}
111+
return;
112+
}
113+
#endif
101114
machine_pin_irq_obj_t *irq = MP_STATE_PORT(machine_pin_irq_objects[index]);
102115
if (irq != NULL) {
103116
irq->flags = irq->trigger;

ports/mimxrt/main.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@
4343
#if MICROPY_PY_LWIP
4444
#include "lwip/init.h"
4545
#include "lwip/apps/mdns.h"
46+
#if MICROPY_PY_NETWORK_CYW43
47+
#include "lib/cyw43-driver/src/cyw43.h"
48+
#endif
4649
#endif
4750
#include "systick.h"
4851
#include "extmod/modnetwork.h"
@@ -69,6 +72,17 @@ int main(void) {
6972
systick_enable_dispatch(SYSTICK_DISPATCH_LWIP, mod_network_lwip_poll_wrapper);
7073
#endif
7174

75+
#if MICROPY_PY_NETWORK_CYW43
76+
{
77+
cyw43_init(&cyw43_state);
78+
uint8_t buf[8];
79+
memcpy(&buf[0], "PYBD", 4);
80+
mp_hal_get_mac_ascii(MP_HAL_MAC_WLAN0, 8, 4, (char *)&buf[4]);
81+
cyw43_wifi_ap_set_ssid(&cyw43_state, 8, buf);
82+
cyw43_wifi_ap_set_password(&cyw43_state, 8, (const uint8_t *)"pybd0123");
83+
}
84+
#endif
85+
7286
for (;;) {
7387
#if defined(MICROPY_HW_LED1)
7488
led_init();

ports/mimxrt/mpconfigport.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,12 +170,20 @@ extern const struct _mp_obj_type_t network_lan_type;
170170
#define MICROPY_HW_NIC_ETH
171171
#endif
172172

173+
#if MICROPY_PY_NETWORK_CYW43
174+
extern const struct _mp_obj_type_t mp_network_cyw43_type;
175+
#define MICROPY_HW_NIC_CYW43 { MP_ROM_QSTR(MP_QSTR_WLAN), MP_ROM_PTR(&mp_network_cyw43_type) },
176+
#else
177+
#define MICROPY_HW_NIC_CYW43
178+
#endif
179+
173180
#ifndef MICROPY_BOARD_NETWORK_INTERFACES
174181
#define MICROPY_BOARD_NETWORK_INTERFACES
175182
#endif
176183

177184
#define MICROPY_PORT_NETWORK_INTERFACES \
178185
MICROPY_HW_NIC_ETH \
186+
MICROPY_HW_NIC_CYW43 \
179187
MICROPY_BOARD_NETWORK_INTERFACES \
180188

181189
#ifndef MICROPY_BOARD_ROOT_POINTERS

ports/mimxrt/mpnetworkport.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@
4343
#include "lwip/dhcp.h"
4444
#include "lwip/apps/mdns.h"
4545

46+
#if MICROPY_PY_NETWORK_CYW43
47+
#include "extmod/network_cyw43.h"
48+
#include "lib/cyw43-driver/src/cyw43.h"
49+
#endif
50+
4651
// Poll lwIP every 128ms
4752
#define LWIP_TICK(tick) (((tick) & ~(SYSTICK_DISPATCH_NUM_SLOTS - 1) & 0x7f) == 0)
4853

@@ -59,6 +64,16 @@ void mod_network_lwip_poll_wrapper(uint32_t ticks_ms) {
5964
if (LWIP_TICK(ticks_ms)) {
6065
pendsv_schedule_dispatch(PENDSV_DISPATCH_LWIP, pyb_lwip_poll);
6166
}
67+
68+
#if MICROPY_PY_NETWORK_CYW43
69+
if (cyw43_poll) {
70+
if (cyw43_sleep != 0) {
71+
if (--cyw43_sleep == 0) {
72+
pendsv_schedule_dispatch(PENDSV_DISPATCH_CYW43, cyw43_poll);
73+
}
74+
}
75+
}
76+
#endif
6277
}
6378

6479
#endif // MICROPY_PY_LWIP

ports/mimxrt/pendsv.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ enum {
3131
#if MICROPY_PY_NETWORK && MICROPY_PY_LWIP
3232
PENDSV_DISPATCH_LWIP,
3333
#endif
34+
#if MICROPY_PY_NETWORK_CYW43
35+
PENDSV_DISPATCH_CYW43,
36+
#endif
3437
MICROPY_BOARD_PENDSV_ENTRIES
3538
PENDSV_DISPATCH_MAX
3639
};

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy