1 /*
2  * Copyright (c) 2022 Raspberry Pi (Trading) Ltd.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 // This header is included by cyw43_driver to setup its environment
8 
9 #ifndef _CYW43_CONFIGPORT_H
10 #define _CYW43_CONFIGPORT_H
11 
12 #include "pico.h"
13 #include "hardware/gpio.h"
14 #include "pico/time.h"
15 
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
19 
20 #ifndef CYW43_HOST_NAME
21 #define CYW43_HOST_NAME "PicoW"
22 #endif
23 
24 #ifndef CYW43_GPIO
25 #define CYW43_GPIO 1
26 #endif
27 
28 #ifndef CYW43_LOGIC_DEBUG
29 #define CYW43_LOGIC_DEBUG 0
30 #endif
31 
32 #ifndef CYW43_USE_OTP_MAC
33 #define CYW43_USE_OTP_MAC 1
34 #endif
35 
36 #ifndef CYW43_NO_NETUTILS
37 #define CYW43_NO_NETUTILS 1
38 #endif
39 
40 #ifndef CYW43_IOCTL_TIMEOUT_US
41 #define CYW43_IOCTL_TIMEOUT_US 1000000
42 #endif
43 
44 #ifndef CYW43_USE_STATS
45 #define CYW43_USE_STATS 0
46 #endif
47 
48 // todo should this be user settable?
49 #ifndef CYW43_HAL_MAC_WLAN0
50 #define CYW43_HAL_MAC_WLAN0 0
51 #endif
52 
53 #ifndef STATIC
54 #define STATIC static
55 #endif
56 
57 #ifndef CYW43_USE_SPI
58 #define CYW43_USE_SPI 1
59 #endif
60 
61 #ifndef CYW43_SPI_PIO
62 #define CYW43_SPI_PIO 1
63 #endif
64 
65 #ifndef CYW43_CHIPSET_FIRMWARE_INCLUDE_FILE
66 #if CYW43_ENABLE_BLUETOOTH
67 #define CYW43_CHIPSET_FIRMWARE_INCLUDE_FILE "wb43439A0_7_95_49_00_combined.h"
68 #else
69 #define CYW43_CHIPSET_FIRMWARE_INCLUDE_FILE "w43439A0_7_95_49_00_combined.h"
70 #endif
71 #endif
72 
73 #ifndef CYW43_WIFI_NVRAM_INCLUDE_FILE
74 #define CYW43_WIFI_NVRAM_INCLUDE_FILE "wifi_nvram_43439.h"
75 #endif
76 
77 // Note, these are negated, because cyw43_driver negates them before returning!
78 #define CYW43_EPERM            (-PICO_ERROR_NOT_PERMITTED) // Operation not permitted
79 #define CYW43_EIO              (-PICO_ERROR_IO) // I/O error
80 #define CYW43_EINVAL           (-PICO_ERROR_INVALID_ARG) // Invalid argument
81 #define CYW43_ETIMEDOUT        (-PICO_ERROR_TIMEOUT) // Connection timed out
82 
83 #ifndef CYW43_WL_GPIO_COUNT
84 #define CYW43_WL_GPIO_COUNT 3
85 #endif
86 
87 #define CYW43_NUM_GPIOS        CYW43_WL_GPIO_COUNT
88 
89 #define cyw43_hal_pin_obj_t uint
90 
91 // get the number of elements in a fixed-size array
92 #define CYW43_ARRAY_SIZE(a) count_of(a)
93 
cyw43_hal_ticks_us(void)94 static inline uint32_t cyw43_hal_ticks_us(void) {
95     return time_us_32();
96 }
97 
cyw43_hal_ticks_ms(void)98 static inline uint32_t cyw43_hal_ticks_ms(void) {
99     return to_ms_since_boot(get_absolute_time());
100 }
101 
102 #if CYW43_PIN_WL_DYNAMIC
103 // these are just an index into an array
104 typedef enum cyw43_pin_index_t {
105     CYW43_PIN_INDEX_WL_REG_ON,
106     CYW43_PIN_INDEX_WL_DATA_OUT,
107     CYW43_PIN_INDEX_WL_DATA_IN,
108     CYW43_PIN_INDEX_WL_HOST_WAKE,
109     CYW43_PIN_INDEX_WL_CLOCK,
110     CYW43_PIN_INDEX_WL_CS,
111     CYW43_PIN_INDEX_WL_COUNT // last
112 } cyw43_pin_index_t;
113 #define CYW43_PIN_WL_REG_ON cyw43_get_pin_wl(CYW43_PIN_INDEX_WL_REG_ON)
114 #define CYW43_PIN_WL_DATA_OUT cyw43_get_pin_wl(CYW43_PIN_INDEX_WL_DATA_OUT)
115 #define CYW43_PIN_WL_DATA_IN cyw43_get_pin_wl(CYW43_PIN_INDEX_WL_DATA_IN)
116 #define CYW43_PIN_WL_HOST_WAKE cyw43_get_pin_wl(CYW43_PIN_INDEX_WL_HOST_WAKE)
117 #define CYW43_PIN_WL_CLOCK cyw43_get_pin_wl(CYW43_PIN_INDEX_WL_CLOCK)
118 #define CYW43_PIN_WL_CS cyw43_get_pin_wl(CYW43_PIN_INDEX_WL_CS)
119 // Lookup the gpio value in an array
120 uint cyw43_get_pin_wl(cyw43_pin_index_t pin_id);
121 #else
122 // Just return the gpio number configured at build time
123 #define CYW43_PIN_WL_REG_ON CYW43_DEFAULT_PIN_WL_REG_ON
124 #define CYW43_PIN_WL_DATA_OUT CYW43_DEFAULT_PIN_WL_DATA_OUT
125 #define CYW43_PIN_WL_DATA_IN CYW43_DEFAULT_PIN_WL_DATA_IN
126 #define CYW43_PIN_WL_HOST_WAKE CYW43_DEFAULT_PIN_WL_HOST_WAKE
127 #define CYW43_PIN_WL_CLOCK CYW43_DEFAULT_PIN_WL_CLOCK
128 #define CYW43_PIN_WL_CS CYW43_DEFAULT_PIN_WL_CS
129 #endif // !CYW43_PIN_WL_DYNAMIC
130 
cyw43_hal_pin_read(cyw43_hal_pin_obj_t pin)131 static inline int cyw43_hal_pin_read(cyw43_hal_pin_obj_t pin) {
132     return gpio_get(pin);
133 }
134 
cyw43_hal_pin_low(cyw43_hal_pin_obj_t pin)135 static inline void cyw43_hal_pin_low(cyw43_hal_pin_obj_t pin) {
136     gpio_put(pin, false);
137 }
138 
cyw43_hal_pin_high(cyw43_hal_pin_obj_t pin)139 static inline void cyw43_hal_pin_high(cyw43_hal_pin_obj_t pin) {
140     gpio_put(pin, true);
141 }
142 
143 #define CYW43_HAL_PIN_MODE_INPUT           (GPIO_IN)
144 #define CYW43_HAL_PIN_MODE_OUTPUT          (GPIO_OUT)
145 
146 #define CYW43_HAL_PIN_PULL_NONE            (0)
147 #define CYW43_HAL_PIN_PULL_UP              (1)
148 #define CYW43_HAL_PIN_PULL_DOWN            (2)
149 
cyw43_hal_pin_config(cyw43_hal_pin_obj_t pin,uint32_t mode,uint32_t pull,__unused uint32_t alt)150 static inline void cyw43_hal_pin_config(cyw43_hal_pin_obj_t pin, uint32_t mode, uint32_t pull, __unused uint32_t alt) {
151     assert((mode == CYW43_HAL_PIN_MODE_INPUT || mode == CYW43_HAL_PIN_MODE_OUTPUT) && alt == 0);
152     gpio_set_dir(pin, mode);
153     gpio_set_pulls(pin, pull == CYW43_HAL_PIN_PULL_UP, pull == CYW43_HAL_PIN_PULL_DOWN);
154 }
155 
156 void cyw43_hal_get_mac(int idx, uint8_t buf[6]);
157 
158 void cyw43_hal_generate_laa_mac(int idx, uint8_t buf[6]);
159 
160 
161 void cyw43_thread_enter(void);
162 
163 void cyw43_thread_exit(void);
164 
165 #define CYW43_THREAD_ENTER cyw43_thread_enter();
166 #define CYW43_THREAD_EXIT cyw43_thread_exit();
167 #ifndef NDEBUG
168 
169 void cyw43_thread_lock_check(void);
170 
171 #define cyw43_arch_lwip_check() cyw43_thread_lock_check()
172 #define CYW43_THREAD_LOCK_CHECK cyw43_arch_lwip_check();
173 #else
174 #define cyw43_arch_lwip_check() ((void)0)
175 #define CYW43_THREAD_LOCK_CHECK
176 #endif
177 
178 void cyw43_await_background_or_timeout_us(uint32_t timeout_us);
179 // todo not 100% sure about the timeouts here; MP uses __WFI which will always wakeup periodically
180 #define CYW43_SDPCM_SEND_COMMON_WAIT cyw43_await_background_or_timeout_us(1000);
181 #define CYW43_DO_IOCTL_WAIT cyw43_await_background_or_timeout_us(1000);
182 
183 void cyw43_delay_ms(uint32_t ms);
184 
185 void cyw43_delay_us(uint32_t us);
186 
187 void cyw43_schedule_internal_poll_dispatch(void (*func)(void));
188 
189 void cyw43_post_poll_hook(void);
190 
191 #define CYW43_POST_POLL_HOOK cyw43_post_poll_hook();
192 
193 // Allow malloc and free to be changed
194 #ifndef cyw43_malloc
195 #define cyw43_malloc malloc
196 #endif
197 #ifndef cyw43_free
198 #define cyw43_free free
199 #endif
200 
201 #ifdef __cplusplus
202 }
203 #endif
204 
205 
206 #endif