Move more code from pico_cyw43_arch to pico_cyw43_driver (#1201)

Basically the integration code (cyw43_config.h and related implementations) are now in the driver.
cyw43_arch now just has
  * async_context creation per CYW43_ARCH_TYPE
  * pre-existing cyw43_arch methods for connect etc.
diff --git a/src/rp2_common/pico_cyw43_arch/cyw43_arch.c b/src/rp2_common/pico_cyw43_arch/cyw43_arch.c
index aaa9c72..bdf19ec 100644
--- a/src/rp2_common/pico_cyw43_arch/cyw43_arch.c
+++ b/src/rp2_common/pico_cyw43_arch/cyw43_arch.c
@@ -119,30 +119,6 @@
     return cyw43_arch_wifi_connect_until(ssid, pw, auth, make_timeout_time_ms(timeout_ms));
 }
 
-// todo maybe add an #ifdef in cyw43_driver
-uint32_t storage_read_blocks(__unused uint8_t *dest, __unused uint32_t block_num, __unused uint32_t num_blocks) {
-    // shouldn't be used
-    panic_unsupported();
-}
-
-// Generate a mac address if one is not set in otp
-void cyw43_hal_generate_laa_mac(__unused int idx, uint8_t buf[6]) {
-    CYW43_DEBUG("Warning. No mac in otp. Generating mac from board id\n");
-    pico_unique_board_id_t board_id;
-    pico_get_unique_board_id(&board_id);
-    memcpy(buf, &board_id.id[2], 6);
-    buf[0] &= (uint8_t)~0x1; // unicast
-    buf[0] |= 0x2; // locally administered
-}
-
-// Return mac address
-void cyw43_hal_get_mac(__unused int idx, uint8_t buf[6]) {
-    // The mac should come from cyw43 otp.
-    // This is loaded into the state after the driver is initialised
-    // cyw43_hal_generate_laa_mac is called by the driver to generate a mac if otp is not set
-    memcpy(buf, cyw43_state.mac, 6);
-}
-
 uint32_t cyw43_arch_get_country_code(void) {
     return country_code;
 }
@@ -175,52 +151,4 @@
 
 void cyw43_arch_wait_for_work_until(absolute_time_t until) {
     async_context_wait_for_work_until(async_context, until);
-}
-
-// Prevent background processing in pensv and access by the other core
-// These methods are called in pensv context and on either core
-// They can be called recursively
-void cyw43_thread_enter(void) {
-    async_context_acquire_lock_blocking(async_context);
-}
-
-void cyw43_thread_exit(void) {
-    async_context_release_lock(async_context);
-}
-
-#ifndef NDEBUG
-void cyw43_thread_lock_check(void) {
-    async_context_lock_check(async_context);
-}
-#endif
-
-void cyw43_await_background_or_timeout_us(uint32_t timeout_us) {
-    async_context_wait_for_work_until(async_context, make_timeout_time_us(timeout_us));
-}
-
-void cyw43_delay_ms(uint32_t ms) {
-    async_context_wait_until(async_context, make_timeout_time_ms(ms));
-}
-
-void cyw43_delay_us(uint32_t us) {
-    async_context_wait_until(async_context, make_timeout_time_us(us));
-}
-
-#if !CYW43_LWIP
-static void no_lwip_fail() {
-    panic("cyw43 has no ethernet interface");
-}
-void __attribute__((weak)) cyw43_cb_tcpip_init(cyw43_t *self, int itf) {
-}
-void __attribute__((weak)) cyw43_cb_tcpip_deinit(cyw43_t *self, int itf) {
-}
-void __attribute__((weak)) cyw43_cb_tcpip_set_link_up(cyw43_t *self, int itf) {
-    no_lwip_fail();
-}
-void __attribute__((weak)) cyw43_cb_tcpip_set_link_down(cyw43_t *self, int itf) {
-    no_lwip_fail();
-}
-void __attribute__((weak)) cyw43_cb_process_ethernet(void *cb_data, int itf, size_t len, const uint8_t *buf) {
-    no_lwip_fail();
-}
-#endif
+}
\ No newline at end of file
diff --git a/src/rp2_common/pico_cyw43_arch/include/cyw43_configport.h b/src/rp2_common/pico_cyw43_arch/include/cyw43_configport.h
deleted file mode 100644
index 68cc4b8..0000000
--- a/src/rp2_common/pico_cyw43_arch/include/cyw43_configport.h
+++ /dev/null
@@ -1,77 +0,0 @@
-/*
- * Copyright (c) 2022 Raspberry Pi (Trading) Ltd.
- *
- * SPDX-License-Identifier: BSD-3-Clause
- */
-
-// This header is included by cyw43_driver to setup its environment
-
-#ifndef _CYW43_CONFIGPORT_H
-#define _CYW43_CONFIGPORT_H
-
-#include "pico.h"
-
-#ifdef PICO_CYW43_ARCH_HEADER
-#include __XSTRING(PICO_CYW43_ARCH_HEADER)
-#else
-#if PICO_CYW43_ARCH_POLL
-#include "pico/cyw43_arch/arch_poll.h"
-#elif PICO_CYW43_ARCH_THREADSAFE_BACKGROUND
-#include "pico/cyw43_arch/arch_threadsafe_background.h"
-#elif PICO_CYW43_ARCH_FREERTOS
-#include "pico/cyw43_arch/arch_freertos.h"
-#else
-#error must specify support pico_cyw43_arch architecture type or set PICO_CYW43_ARCH_HEADER
-#endif
-#endif
-
-#ifndef CYW43_HOST_NAME
-#define CYW43_HOST_NAME "PicoW"
-#endif
-
-#ifndef CYW43_GPIO
-#define CYW43_GPIO 1
-#endif
-
-#ifndef CYW43_LOGIC_DEBUG
-#define CYW43_LOGIC_DEBUG 0
-#endif
-
-#ifndef CYW43_USE_OTP_MAC
-#define CYW43_USE_OTP_MAC 1
-#endif
-
-#ifndef CYW43_NO_NETUTILS
-#define CYW43_NO_NETUTILS 1
-#endif
-
-#ifndef CYW43_IOCTL_TIMEOUT_US
-#define CYW43_IOCTL_TIMEOUT_US 1000000
-#endif
-
-#ifndef CYW43_USE_STATS
-#define CYW43_USE_STATS 0
-#endif
-
-// todo should this be user settable?
-#ifndef CYW43_HAL_MAC_WLAN0
-#define CYW43_HAL_MAC_WLAN0 0
-#endif
-
-#ifndef STATIC
-#define STATIC static
-#endif
-
-#ifndef CYW43_USE_SPI
-#define CYW43_USE_SPI 1
-#endif
-
-#ifndef CYW43_SPI_PIO
-#define CYW43_SPI_PIO 1
-#endif
-
-#ifndef CYW43_WIFI_NVRAM_INCLUDE_FILE
-#define CYW43_WIFI_NVRAM_INCLUDE_FILE "wifi_nvram_43439.h"
-#endif
-
-#endif
\ No newline at end of file
diff --git a/src/rp2_common/pico_cyw43_arch/include/pico/cyw43_arch.h b/src/rp2_common/pico_cyw43_arch/include/pico/cyw43_arch.h
index 350d5f5..b4bd448 100644
--- a/src/rp2_common/pico_cyw43_arch/include/pico/cyw43_arch.h
+++ b/src/rp2_common/pico_cyw43_arch/include/pico/cyw43_arch.h
@@ -15,6 +15,21 @@
 
 #include "cyw43.h"
 #include "cyw43_country.h"
+#include "pico/async_context.h"
+
+#ifdef PICO_CYW43_ARCH_HEADER
+#include __XSTRING(PICO_CYW43_ARCH_HEADER)
+#else
+#if PICO_CYW43_ARCH_POLL
+#include "pico/cyw43_arch/arch_poll.h"
+#elif PICO_CYW43_ARCH_THREADSAFE_BACKGROUND
+#include "pico/cyw43_arch/arch_threadsafe_background.h"
+#elif PICO_CYW43_ARCH_FREERTOS
+#include "pico/cyw43_arch/arch_freertos.h"
+#else
+#error must specify support pico_cyw43_arch architecture type or set PICO_CYW43_ARCH_HEADER
+#endif
+#endif
 
 /**
  * \defgroup cyw43_driver cyw43_driver
@@ -234,6 +249,9 @@
  * \sa cyw43_arch_lwip_end
  * \sa cyw43_arch_lwip_protect
  */
+static inline void cyw43_arch_lwip_begin(void) {
+    cyw43_thread_enter();
+}
 
 /*!
  * \fn void cyw43_arch_lwip_end(void)
@@ -249,6 +267,9 @@
  * \sa cyw43_arch_lwip_begin
  * \sa cyw43_arch_lwip_protect
  */
+static inline void cyw43_arch_lwip_end(void) {
+    cyw43_thread_exit();
+}
 
 /*!
  * \fn int cyw43_arch_lwip_protect(int (*func)(void *param), void *param)
@@ -266,6 +287,12 @@
  * \sa cyw43_arch_lwip_begin
  * \sa cyw43_arch_lwip_end
  */
+static inline int cyw43_arch_lwip_protect(int (*func)(void *param), void *param) {
+    cyw43_arch_lwip_begin();
+    int rc = func(param);
+    cyw43_arch_lwip_end();
+    return rc;
+}
 
 /*!
  * \fn void cyw43_arch_lwip_check(void)
diff --git a/src/rp2_common/pico_cyw43_arch/include/pico/cyw43_arch/arch_freertos.h b/src/rp2_common/pico_cyw43_arch/include/pico/cyw43_arch/arch_freertos.h
index 3d365b3..db2ea3c 100644
--- a/src/rp2_common/pico_cyw43_arch/include/pico/cyw43_arch/arch_freertos.h
+++ b/src/rp2_common/pico_cyw43_arch/include/pico/cyw43_arch/arch_freertos.h
@@ -7,12 +7,6 @@
 #ifndef _PICO_CYW43_ARCH_ARCH_FREERTOS_H
 #define _PICO_CYW43_ARCH_ARCH_FREERTOS_H
 
-#include "pico/cyw43_arch/arch_common.h"
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
 // PICO_CONFIG: CYW43_TASK_STACK_SIZE, Stack size for the CYW43 FreeRTOS task in 4-byte words, type=int, default=1024, group=pico_cyw43_arch
 #ifndef CYW43_TASK_STACK_SIZE
 #define CYW43_TASK_STACK_SIZE 1024
@@ -23,8 +17,4 @@
 #define CYW43_TASK_PRIORITY (tskIDLE_PRIORITY + 4)
 #endif
 
-#ifdef __cplusplus
-}
-#endif
-
 #endif
diff --git a/src/rp2_common/pico_cyw43_arch/include/pico/cyw43_arch/arch_poll.h b/src/rp2_common/pico_cyw43_arch/include/pico/cyw43_arch/arch_poll.h
index 0b95593..a221917 100644
--- a/src/rp2_common/pico_cyw43_arch/include/pico/cyw43_arch/arch_poll.h
+++ b/src/rp2_common/pico_cyw43_arch/include/pico/cyw43_arch/arch_poll.h
@@ -7,16 +7,6 @@
 #ifndef _PICO_CYW43_ARCH_ARCH_POLL_H
 #define _PICO_CYW43_ARCH_ARCH_POLL_H
 
-#include "pico/cyw43_arch/arch_common.h"
-
-#include <assert.h>
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-#ifdef __cplusplus
-}
-#endif
+// now obsolete; kept for backwards compatibility
 
 #endif
diff --git a/src/rp2_common/pico_cyw43_arch/include/pico/cyw43_arch/arch_threadsafe_background.h b/src/rp2_common/pico_cyw43_arch/include/pico/cyw43_arch/arch_threadsafe_background.h
index db71863..005c15a 100644
--- a/src/rp2_common/pico_cyw43_arch/include/pico/cyw43_arch/arch_threadsafe_background.h
+++ b/src/rp2_common/pico_cyw43_arch/include/pico/cyw43_arch/arch_threadsafe_background.h
@@ -7,14 +7,6 @@
 #ifndef _PICO_CYW43_ARCH_ARCH_THREADSAFE_BACKGROUND_H
 #define _PICO_CYW43_ARCH_ARCH_THREADSAFE_BACKGROUND_H
 
-#include "pico/cyw43_arch/arch_common.h"
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-#ifdef __cplusplus
-}
-#endif
+// now obsolete; kept for backwards compatibility
 
 #endif
\ No newline at end of file
diff --git a/src/rp2_common/pico_cyw43_driver/cyw43_driver.c b/src/rp2_common/pico_cyw43_driver/cyw43_driver.c
index 008d6a1..6a3d01c 100644
--- a/src/rp2_common/pico_cyw43_driver/cyw43_driver.c
+++ b/src/rp2_common/pico_cyw43_driver/cyw43_driver.c
@@ -6,6 +6,7 @@
 
 #include "hardware/gpio.h"
 #include "hardware/irq.h"
+#include "pico/unique_id.h"
 #include "cyw43.h"
 #include "pico/cyw43_driver.h"
 
@@ -119,4 +120,76 @@
     async_context_execute_sync(context, cyw43_irq_deinit, NULL);
     cyw43_deinit(&cyw43_state);
     cyw43_async_context = NULL;
-}
\ No newline at end of file
+}
+
+// todo maybe add an #ifdef in cyw43_driver
+uint32_t storage_read_blocks(__unused uint8_t *dest, __unused uint32_t block_num, __unused uint32_t num_blocks) {
+    // shouldn't be used
+    panic_unsupported();
+}
+
+// Generate a mac address if one is not set in otp
+void __attribute__((weak)) cyw43_hal_generate_laa_mac(__unused int idx, uint8_t buf[6]) {
+    CYW43_DEBUG("Warning. No mac in otp. Generating mac from board id\n");
+    pico_unique_board_id_t board_id;
+    pico_get_unique_board_id(&board_id);
+    memcpy(buf, &board_id.id[2], 6);
+    buf[0] &= (uint8_t)~0x1; // unicast
+    buf[0] |= 0x2; // locally administered
+}
+
+// Return mac address
+void cyw43_hal_get_mac(__unused int idx, uint8_t buf[6]) {
+    // The mac should come from cyw43 otp.
+    // This is loaded into the state after the driver is initialised
+    // cyw43_hal_generate_laa_mac is called by the driver to generate a mac if otp is not set
+    memcpy(buf, cyw43_state.mac, 6);
+}
+
+// Prevent background processing in pensv and access by the other core
+// These methods are called in pensv context and on either core
+// They can be called recursively
+void cyw43_thread_enter(void) {
+    async_context_acquire_lock_blocking(cyw43_async_context);
+}
+
+void cyw43_thread_exit(void) {
+    async_context_release_lock(cyw43_async_context);
+}
+
+#ifndef NDEBUG
+void cyw43_thread_lock_check(void) {
+    async_context_lock_check(cyw43_async_context);
+}
+#endif
+
+void cyw43_await_background_or_timeout_us(uint32_t timeout_us) {
+    async_context_wait_for_work_until(cyw43_async_context, make_timeout_time_us(timeout_us));
+}
+
+void cyw43_delay_ms(uint32_t ms) {
+    async_context_wait_until(cyw43_async_context, make_timeout_time_ms(ms));
+}
+
+void cyw43_delay_us(uint32_t us) {
+    async_context_wait_until(cyw43_async_context, make_timeout_time_us(us));
+}
+
+#if !CYW43_LWIP
+static void no_lwip_fail() {
+    panic("cyw43 has no ethernet interface");
+}
+void __attribute__((weak)) cyw43_cb_tcpip_init(cyw43_t *self, int itf) {
+}
+void __attribute__((weak)) cyw43_cb_tcpip_deinit(cyw43_t *self, int itf) {
+}
+void __attribute__((weak)) cyw43_cb_tcpip_set_link_up(cyw43_t *self, int itf) {
+    no_lwip_fail();
+}
+void __attribute__((weak)) cyw43_cb_tcpip_set_link_down(cyw43_t *self, int itf) {
+    no_lwip_fail();
+}
+void __attribute__((weak)) cyw43_cb_process_ethernet(void *cb_data, int itf, size_t len, const uint8_t *buf) {
+    no_lwip_fail();
+}
+#endif
diff --git a/src/rp2_common/pico_cyw43_arch/include/pico/cyw43_arch/arch_common.h b/src/rp2_common/pico_cyw43_driver/include/cyw43_configport.h
similarity index 75%
rename from src/rp2_common/pico_cyw43_arch/include/pico/cyw43_arch/arch_common.h
rename to src/rp2_common/pico_cyw43_driver/include/cyw43_configport.h
index 290e198..bb31310 100644
--- a/src/rp2_common/pico_cyw43_arch/include/pico/cyw43_arch/arch_common.h
+++ b/src/rp2_common/pico_cyw43_driver/include/cyw43_configport.h
@@ -4,19 +4,68 @@
  * SPDX-License-Identifier: BSD-3-Clause
  */
 
-#ifndef _PICO_CYW43_ARCH_ARCH_COMMON_H
-#define _PICO_CYW43_ARCH_ARCH_COMMON_H
+// This header is included by cyw43_driver to setup its environment
+
+#ifndef _CYW43_CONFIGPORT_H
+#define _CYW43_CONFIGPORT_H
 
 #include "pico.h"
-#include "pico/time.h"
 #include "hardware/gpio.h"
-#include "pico/error.h"
-#include "pico/async_context.h"
+#include "pico/time.h"
 
 #ifdef __cplusplus
 extern "C" {
 #endif
 
+#ifndef CYW43_HOST_NAME
+#define CYW43_HOST_NAME "PicoW"
+#endif
+
+#ifndef CYW43_GPIO
+#define CYW43_GPIO 1
+#endif
+
+#ifndef CYW43_LOGIC_DEBUG
+#define CYW43_LOGIC_DEBUG 0
+#endif
+
+#ifndef CYW43_USE_OTP_MAC
+#define CYW43_USE_OTP_MAC 1
+#endif
+
+#ifndef CYW43_NO_NETUTILS
+#define CYW43_NO_NETUTILS 1
+#endif
+
+#ifndef CYW43_IOCTL_TIMEOUT_US
+#define CYW43_IOCTL_TIMEOUT_US 1000000
+#endif
+
+#ifndef CYW43_USE_STATS
+#define CYW43_USE_STATS 0
+#endif
+
+// todo should this be user settable?
+#ifndef CYW43_HAL_MAC_WLAN0
+#define CYW43_HAL_MAC_WLAN0 0
+#endif
+
+#ifndef STATIC
+#define STATIC static
+#endif
+
+#ifndef CYW43_USE_SPI
+#define CYW43_USE_SPI 1
+#endif
+
+#ifndef CYW43_SPI_PIO
+#define CYW43_SPI_PIO 1
+#endif
+
+#ifndef CYW43_WIFI_NVRAM_INCLUDE_FILE
+#define CYW43_WIFI_NVRAM_INCLUDE_FILE "wifi_nvram_43439.h"
+#endif
+
 // Note, these are negated, because cyw43_driver negates them before returning!
 #define CYW43_EPERM            (-PICO_ERROR_NOT_PERMITTED) // Operation not permitted
 #define CYW43_EIO              (-PICO_ERROR_IO) // I/O error
@@ -69,12 +118,15 @@
 
 
 void cyw43_thread_enter(void);
+
 void cyw43_thread_exit(void);
 
 #define CYW43_THREAD_ENTER cyw43_thread_enter();
 #define CYW43_THREAD_EXIT cyw43_thread_exit();
 #ifndef NDEBUG
+
 void cyw43_thread_lock_check(void);
+
 #define cyw43_arch_lwip_check() cyw43_thread_lock_check()
 #define CYW43_THREAD_LOCK_CHECK cyw43_arch_lwip_check();
 #else
@@ -88,44 +140,18 @@
 #define CYW43_DO_IOCTL_WAIT cyw43_await_background_or_timeout_us(1000);
 
 void cyw43_delay_ms(uint32_t ms);
+
 void cyw43_delay_us(uint32_t us);
 
 void cyw43_schedule_internal_poll_dispatch(void (*func)(void));
 
 void cyw43_post_poll_hook(void);
+
 #define CYW43_POST_POLL_HOOK cyw43_post_poll_hook();
 
-static inline void cyw43_arch_lwip_begin(void) {
-    cyw43_thread_enter();
-}
-
-static inline void cyw43_arch_lwip_end(void) {
-    cyw43_thread_exit();
-}
-
-static inline int cyw43_arch_lwip_protect(int (*func)(void *param), void *param) {
-    cyw43_arch_lwip_begin();
-    int rc = func(param);
-    cyw43_arch_lwip_end();
-    return rc;
-}
-
-#if CYW43_USE_BTSTACK
-static inline int cyw43_bt_init(void) { return 0; }
-static inline void cyw43_bt_deinit(void) {}
-
-static inline void cyw43_arch_btstack_begin(void) {
-    cyw43_thread_enter();
-}
-
-static inline void cyw43_arch_btstack_end(void) {
-    cyw43_thread_exit();
-}
-#endif
-
 #ifdef __cplusplus
 }
 #endif
 
-#endif
 
+#endif
\ No newline at end of file