3030#define CS_PIN 25u
3131#define IRQ_SAMPLE_DELAY_NS 100
3232
33+ // The pins should all work in the same gpio base
34+ static_assert ((DATA_OUT_PIN < 32 && DATA_IN_PIN < 32 && CLOCK_PIN < 32 ) || (DATA_OUT_PIN > 16 && DATA_IN_PIN > 16 && CLOCK_PIN > 16 ), "" );
35+
3336#ifdef CYW43_SPI_PROGRAM_NAME
3437#define SPI_PROGRAM_NAME CYW43_SPI_PROGRAM_NAME
3538#else
@@ -85,16 +88,10 @@ __force_inline static uint32_t __swap16x2(uint32_t a) {
8588}
8689#define SWAP32 (a ) __swap16x2(a)
8790
88- #ifndef CYW43_SPI_PIO_PREFERRED_PIO
89- #define CYW43_SPI_PIO_PREFERRED_PIO 1
90- #endif
91- static_assert (CYW43_SPI_PIO_PREFERRED_PIO >=0 && CYW43_SPI_PIO_PREFERRED_PIO < NUM_PIOS , "" );
92-
9391typedef struct {
94- pio_hw_t * pio ;
95- uint8_t pio_func_sel ;
96- int8_t pio_offset ;
97- int8_t pio_sm ;
92+ PIO pio ;
93+ uint pio_offset ;
94+ uint pio_sm ;
9895 int8_t dma_out ;
9996 int8_t dma_in ;
10097} bus_data_t ;
@@ -105,38 +102,17 @@ int cyw43_spi_init(cyw43_int_t *self) {
105102 // Only does something if CYW43_LOGIC_DEBUG=1
106103 logic_debug_init ();
107104
108- static_assert (NUM_PIOS == 2 || NUM_PIOS == 3 , "" );
109-
110- #if NUM_PIOS == 2
111- pio_hw_t * pios [NUM_PIOS ] = {pio0 , pio1 };
112- #else
113- pio_hw_t * pios [NUM_PIOS ] = {pio0 , pio1 , pio2 };
114- #endif
115- uint pio_index = CYW43_SPI_PIO_PREFERRED_PIO ;
116- // Check we can add the program
117- for (uint i = 1 ; i < NUM_PIOS ;i ++ ) {
118- if (pio_can_add_program (pios [pio_index ], & SPI_PROGRAM_FUNC )) break ;
119- if (i == NUM_PIOS - 1 ) {
120- return CYW43_FAIL_FAST_CHECK (- CYW43_EIO );
121- }
122- pio_index = (pio_index + 1 ) % NUM_PIOS ;
123- }
124105 assert (!self -> bus_data );
125106 self -> bus_data = & bus_data_instance ;
126107 bus_data_t * bus_data = (bus_data_t * )self -> bus_data ;
127- bus_data -> pio = pios [ pio_index ] ;
108+ bus_data -> pio = NULL ;
128109 bus_data -> dma_in = -1 ;
129110 bus_data -> dma_out = -1 ;
130111
131- static_assert (GPIO_FUNC_PIO1 == GPIO_FUNC_PIO0 + 1 , "" );
132- bus_data -> pio_func_sel = GPIO_FUNC_PIO0 + pio_index ;
133- bus_data -> pio_sm = (int8_t )pio_claim_unused_sm (bus_data -> pio , false);
134- if (bus_data -> pio_sm < 0 ) {
112+ if (!pio_claim_free_sm_and_add_program_for_gpio_range (& SPI_PROGRAM_FUNC , & bus_data -> pio , & bus_data -> pio_sm , & bus_data -> pio_offset , DATA_OUT_PIN , 1 , true)) {
135113 cyw43_spi_deinit (self );
136114 return CYW43_FAIL_FAST_CHECK (- CYW43_EIO );
137115 }
138-
139- bus_data -> pio_offset = pio_add_program (bus_data -> pio , & SPI_PROGRAM_FUNC );
140116 pio_sm_config config = SPI_PROGRAM_GET_DEFAULT_CONFIG_FUNC (bus_data -> pio_offset );
141117
142118 sm_config_set_clkdiv_int_frac (& config , cyw43_pio_clock_div_int , cyw43_pio_clock_div_frac );
@@ -159,7 +135,7 @@ int cyw43_spi_init(cyw43_int_t *self) {
159135 hw_set_bits (& bus_data -> pio -> input_sync_bypass , 1u << DATA_IN_PIN );
160136 pio_sm_set_config (bus_data -> pio , bus_data -> pio_sm , & config );
161137 pio_sm_set_consecutive_pindirs (bus_data -> pio , bus_data -> pio_sm , CLOCK_PIN , 1 , true);
162- gpio_set_function (DATA_OUT_PIN , bus_data -> pio_func_sel );
138+ gpio_set_function (DATA_OUT_PIN , pio_get_funcsel ( bus_data -> pio ) );
163139
164140 // Set data pin to pull down and schmitt
165141 gpio_set_pulls (DATA_IN_PIN , false, true);
@@ -179,10 +155,8 @@ int cyw43_spi_init(cyw43_int_t *self) {
179155void cyw43_spi_deinit (cyw43_int_t * self ) {
180156 if (self -> bus_data ) {
181157 bus_data_t * bus_data = (bus_data_t * )self -> bus_data ;
182- if (bus_data -> pio_sm >= 0 ) {
183- if (bus_data -> pio_offset != -1 )
184- pio_remove_program (bus_data -> pio , & SPI_PROGRAM_FUNC , bus_data -> pio_offset );
185- pio_sm_unclaim (bus_data -> pio , bus_data -> pio_sm );
158+ if (bus_data -> pio ) {
159+ pio_remove_program_and_unclaim_sm (& SPI_PROGRAM_FUNC , bus_data -> pio , bus_data -> pio_sm , bus_data -> pio_offset );
186160 }
187161 if (bus_data -> dma_out >= 0 ) {
188162 dma_channel_cleanup (bus_data -> dma_out );
@@ -210,8 +184,8 @@ static __noinline void ns_delay(uint32_t ns) {
210184
211185static void start_spi_comms (cyw43_int_t * self ) {
212186 bus_data_t * bus_data = (bus_data_t * )self -> bus_data ;
213- gpio_set_function (DATA_OUT_PIN , bus_data -> pio_func_sel );
214- gpio_set_function (CLOCK_PIN , bus_data -> pio_func_sel );
187+ gpio_set_function (DATA_OUT_PIN , pio_get_funcsel ( bus_data -> pio ) );
188+ gpio_set_function (CLOCK_PIN , pio_get_funcsel ( bus_data -> pio ) );
215189 gpio_pull_down (CLOCK_PIN );
216190 // Pull CS low
217191 cs_set (false);
0 commit comments