Skip to content

Commit 5715e24

Browse files
committed
Support dynamic configuration of cyw43 gpio pins
Add CYW43_PIN_WL_DYNAMIC that means cyw43 gpio pins can be changed at runtime. Then CYW43_PIN_WL_* calls cyw43_get_pin_wl to get the gpio out of the array. cyw43_set_pins_wl can be used to change the cyw43 gpio pins although care is needed when calling this?
1 parent b10898f commit 5715e24

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed

src/rp2_common/pico_cyw43_driver/cyw43_bus_pio_spi.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@
2424

2525
#define IRQ_SAMPLE_DELAY_NS 100
2626

27+
#if !CYW43_PIN_WL_DYNAMIC
2728
// The pins should all work in the same gpio base
2829
static_assert((CYW43_PIN_WL_DATA_OUT < 32 && CYW43_PIN_WL_DATA_IN < 32 && CYW43_PIN_WL_CLOCK < 32) ||
2930
(CYW43_PIN_WL_DATA_OUT >= 16 && CYW43_PIN_WL_DATA_IN >= 16 && CYW43_PIN_WL_CLOCK >= 16), "");
31+
#endif
3032

3133
#ifdef CYW43_SPI_PROGRAM_NAME
3234
#define SPI_PROGRAM_NAME CYW43_SPI_PROGRAM_NAME
@@ -540,3 +542,50 @@ int cyw43_write_bytes(cyw43_int_t *self, uint32_t fn, uint32_t addr, size_t len,
540542
}
541543
}
542544
#endif
545+
546+
#if CYW43_PIN_WL_DYNAMIC
547+
548+
// storage for cyw43 pins
549+
static uint cyw43_pin_array[CYW43_PIN_INDEX_WL_COUNT] = {
550+
CYW43_DEFAULT_PIN_WL_REG_ON,
551+
CYW43_DEFAULT_PIN_WL_DATA_OUT,
552+
CYW43_DEFAULT_PIN_WL_DATA_IN,
553+
CYW43_DEFAULT_PIN_WL_HOST_WAKE,
554+
CYW43_DEFAULT_PIN_WL_CLOCK,
555+
CYW43_DEFAULT_PIN_WL_CS
556+
};
557+
558+
// Check the cyw43 gpio pin array is valid
559+
static bool cyw43_pins_valid(uint pins[CYW43_PIN_INDEX_WL_COUNT]) {
560+
// check the gpios are valid
561+
for(int i = 0; i < CYW43_PIN_INDEX_WL_COUNT; i++) {
562+
if (pins[i] >= NUM_BANK0_GPIOS) {
563+
return false;
564+
}
565+
}
566+
// These pins should use the same gpio base
567+
return (pins[CYW43_PIN_INDEX_WL_DATA_OUT] < 32 && pins[CYW43_PIN_INDEX_WL_DATA_IN] < 32 && pins[CYW43_PIN_INDEX_WL_CLOCK] < 32) ||
568+
(pins[CYW43_PIN_INDEX_WL_DATA_OUT] >= 16 && pins[CYW43_PIN_INDEX_WL_DATA_IN] >= 16 && pins[CYW43_PIN_INDEX_WL_CLOCK] >= 16);
569+
}
570+
571+
// Set the gpio pin array
572+
int cyw43_set_pins_wl(uint pins[CYW43_PIN_INDEX_WL_COUNT]) {
573+
assert(!bus_data_instance.pio);
574+
if (bus_data_instance.pio) {
575+
return PICO_ERROR_RESOURCE_IN_USE;
576+
}
577+
assert(cyw43_pins_valid(pins));
578+
if (!cyw43_pins_valid(pins)) {
579+
return PICO_ERROR_INVALID_ARG;
580+
}
581+
memcpy(cyw43_pin_array, pins, sizeof(cyw43_pin_array));
582+
return PICO_OK;
583+
}
584+
585+
// Get a gpio pin
586+
uint cyw43_get_pin_wl(cyw43_pin_index_t pin_id) {
587+
assert(pin_id < CYW43_PIN_INDEX_WL_COUNT);
588+
assert(cyw43_pin_array[pin_id] < NUM_BANK0_GPIOS);
589+
return cyw43_pin_array[pin_id];
590+
}
591+
#endif // CYW43_PIN_WL_DYNAMIC

src/rp2_common/pico_cyw43_driver/include/cyw43_configport.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,39 @@ static inline uint32_t cyw43_hal_ticks_ms(void) {
120120
#define CYW43_DEFAULT_PIN_WL_CS 25u
121121
#endif
122122

123+
// PICO_CONFIG: CYW43_PIN_WL_DYNAMIC, flag to indicate if cyw43 SPI pins can be changed at runtime, type=bool, default=false, advanced=true, group=pico_cyw43_driver
124+
#ifndef CYW43_PIN_WL_DYNAMIC
125+
#define CYW43_PIN_WL_DYNAMIC 0
126+
#endif
127+
128+
#if CYW43_PIN_WL_DYNAMIC
129+
// these are just an index into an array
130+
typedef enum cyw43_pin_index_t {
131+
CYW43_PIN_INDEX_WL_REG_ON,
132+
CYW43_PIN_INDEX_WL_DATA_OUT,
133+
CYW43_PIN_INDEX_WL_DATA_IN,
134+
CYW43_PIN_INDEX_WL_HOST_WAKE,
135+
CYW43_PIN_INDEX_WL_CLOCK,
136+
CYW43_PIN_INDEX_WL_CS,
137+
CYW43_PIN_INDEX_WL_COUNT // last
138+
} cyw43_pin_index_t;
139+
#define CYW43_PIN_WL_REG_ON cyw43_get_pin_wl(CYW43_PIN_INDEX_WL_REG_ON)
140+
#define CYW43_PIN_WL_DATA_OUT cyw43_get_pin_wl(CYW43_PIN_INDEX_WL_DATA_OUT)
141+
#define CYW43_PIN_WL_DATA_IN cyw43_get_pin_wl(CYW43_PIN_INDEX_WL_DATA_IN)
142+
#define CYW43_PIN_WL_HOST_WAKE cyw43_get_pin_wl(CYW43_PIN_INDEX_WL_HOST_WAKE)
143+
#define CYW43_PIN_WL_CLOCK cyw43_get_pin_wl(CYW43_PIN_INDEX_WL_CLOCK)
144+
#define CYW43_PIN_WL_CS cyw43_get_pin_wl(CYW43_PIN_INDEX_WL_CS)
145+
// Lookup the gpio value in an array
146+
uint cyw43_get_pin_wl(cyw43_pin_index_t pin_id);
147+
#else
148+
// Just return the gpio number configured at build time
123149
#define CYW43_PIN_WL_REG_ON CYW43_DEFAULT_PIN_WL_REG_ON
124150
#define CYW43_PIN_WL_DATA_OUT CYW43_DEFAULT_PIN_WL_DATA_OUT
125151
#define CYW43_PIN_WL_DATA_IN CYW43_DEFAULT_PIN_WL_DATA_IN
126152
#define CYW43_PIN_WL_HOST_WAKE CYW43_DEFAULT_PIN_WL_HOST_WAKE
127153
#define CYW43_PIN_WL_CLOCK CYW43_DEFAULT_PIN_WL_CLOCK
128154
#define CYW43_PIN_WL_CS CYW43_DEFAULT_PIN_WL_CS
155+
#endif // !CYW43_PIN_WL_DYNAMIC
129156

130157
static inline int cyw43_hal_pin_read(cyw43_hal_pin_obj_t pin) {
131158
return gpio_get(pin);

src/rp2_common/pico_cyw43_driver/include/pico/cyw43_driver.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616

1717
#include "pico.h"
1818

19+
#if CYW43_PIN_WL_DYNAMIC
20+
#include "cyw43_configport.h"
21+
#endif
22+
1923
#ifdef __cplusplus
2024
extern "C" {
2125
#endif
@@ -73,6 +77,21 @@ void cyw43_driver_deinit(struct async_context *context);
7377
void cyw43_set_pio_clock_divisor(uint16_t clock_div_int, uint8_t clock_div_frac);
7478
#endif
7579

80+
#if CYW43_PIN_WL_DYNAMIC
81+
/*! \brief Set the gpio pins for the communication with the cyw43 device
82+
* \ingroup pico_cyw43_driver
83+
*
84+
* Set or change the pins used to communicate with the cyw43 device
85+
* This function is only available if \ref CYW43_PIN_WL_DYNAMIC is true
86+
*
87+
* \note The cyw43 driver should not be de-initialised before this function is called or else the behaviour is undefined.
88+
*
89+
* \param pins An array containing the gpio pins to use
90+
* \return PICO_OK if the pin configuration could be changed and is valid
91+
*/
92+
int cyw43_set_pins_wl(uint pins[CYW43_PIN_INDEX_WL_COUNT]);
93+
#endif
94+
7695
#ifdef __cplusplus
7796
}
7897
#endif

0 commit comments

Comments
 (0)