1- /*
2- * Copyright (c) 2023 Raspberry Pi (Trading) Ltd.
3- *
4- * SPDX-License-Identifier: BSD-3-Clause
5- */
61
72#include <stdio.h>
83#include <string.h>
@@ -70,26 +65,25 @@ static wiznet_pio_spi_state_t *active_state;
7065static wiznet_pio_spi_funcs_t * get_wiznet_pio_spi_impl (void );
7166
7267#if CIRCUITPY_WIZNET_W6300
73- static uint16_t mk_cmd_buf (uint8_t * pdst , uint8_t opcode , uint16_t addr )
74- {
75- pdst [0 ] = ( (opcode >> 7 & 0x01 ) << 4 ) | ( (opcode >> 6 & 0x01 ) << 0 );
76- pdst [1 ] = ( (opcode >> 5 & 0x01 ) << 4 ) | ( (opcode >> 4 & 0x01 ) << 0 );
77- pdst [2 ] = ( (opcode >> 3 & 0x01 ) << 4 ) | ( (opcode >> 2 & 0x01 ) << 0 );
78- pdst [3 ] = ( (opcode >> 1 & 0x01 ) << 4 ) | ( (opcode >> 0 & 0x01 ) << 0 );
68+ static uint16_t mk_cmd_buf (uint8_t * pdst , uint8_t opcode , uint16_t addr ){
69+ pdst [0 ] = ( (opcode >> 7 & 0x01 ) << 4 ) | ( (opcode >> 6 & 0x01 ) << 0 );
70+ pdst [1 ] = ( (opcode >> 5 & 0x01 ) << 4 ) | ( (opcode >> 4 & 0x01 ) << 0 );
71+ pdst [2 ] = ( (opcode >> 3 & 0x01 ) << 4 ) | ( (opcode >> 2 & 0x01 ) << 0 );
72+ pdst [3 ] = ( (opcode >> 1 & 0x01 ) << 4 ) | ( (opcode >> 0 & 0x01 ) << 0 );
7973
80- pdst [4 ] = ((uint8_t )(addr >> 8 ) & 0xFF );
81- pdst [5 ] = ((uint8_t )(addr >> 0 ) & 0xFF );
74+ pdst [4 ] = ((uint8_t )(addr >> 8 ) & 0xFF );
75+ pdst [5 ] = ((uint8_t )(addr >> 0 ) & 0xFF );
8276
83- pdst [6 ] = 0 ;
77+ pdst [6 ] = 0 ;
8478
85- return 6 + 1 ;
79+ return 6 + 1 ;
8680}
8781#endif
8882
8983// Initialise our gpios
9084static void wiznet_pio_spi_gpio_setup (wiznet_pio_spi_state_t * state ) {
9185
92- #if CIRCUITPY_WIZNET_W6300
86+ #if CIRCUITPY_WIZNET_W6300
9387 gpio_init (state -> spi_config -> data_io0_pin );
9488 gpio_init (state -> spi_config -> data_io1_pin );
9589 gpio_init (state -> spi_config -> data_io2_pin );
@@ -112,7 +106,7 @@ static void wiznet_pio_spi_gpio_setup(wiznet_pio_spi_state_t *state) {
112106 gpio_init (state -> spi_config -> irq_pin );
113107 gpio_set_dir (state -> spi_config -> irq_pin , GPIO_IN );
114108 gpio_set_pulls (state -> spi_config -> irq_pin , false, false);
115- #else // W55RP20
109+ #else // W55RP20
116110 // Setup MOSI, MISO and IRQ
117111 gpio_init (state -> spi_config -> data_out_pin );
118112 gpio_set_dir (state -> spi_config -> data_out_pin , GPIO_OUT );
@@ -136,14 +130,16 @@ static void wiznet_pio_spi_gpio_setup(wiznet_pio_spi_state_t *state) {
136130
137131wiznet_pio_spi_handle_t wiznet_pio_spi_open (const wiznet_pio_spi_config_t * wiznet_pio_spi_config ) {
138132 wiznet_pio_spi_state_t * state = NULL ;
139- for (size_t i = 0 ; i < count_of (wiznet_pio_spi_state ); i ++ ) {
133+ for (size_t i = 0 ; i < count_of (wiznet_pio_spi_state ); i ++ ) {
140134 if (!wiznet_pio_spi_state [i ].funcs ) {
141135 state = & wiznet_pio_spi_state [i ];
142136 break ;
143137 }
144138 }
145139 assert (state );
146- if (!state ) return NULL ;
140+ if (!state ) {
141+ return NULL ;
142+ }
147143 state -> spi_config = wiznet_pio_spi_config ;
148144 state -> funcs = get_wiznet_pio_spi_impl ();
149145
@@ -182,13 +178,13 @@ wiznet_pio_spi_handle_t wiznet_pio_spi_open(const wiznet_pio_spi_config_t *wizne
182178
183179 sm_config_set_clkdiv_int_frac (& sm_config , state -> spi_config -> clock_div_major , state -> spi_config -> clock_div_minor );
184180 hw_write_masked (& pads_bank0_hw -> io [state -> spi_config -> clock_pin ],
185- (uint )PADS_DRIVE_STRENGTH << PADS_BANK0_GPIO0_DRIVE_LSB ,
186- PADS_BANK0_GPIO0_DRIVE_BITS
187- );
181+ (uint )PADS_DRIVE_STRENGTH << PADS_BANK0_GPIO0_DRIVE_LSB ,
182+ PADS_BANK0_GPIO0_DRIVE_BITS
183+ );
188184 hw_write_masked (& pads_bank0_hw -> io [state -> spi_config -> clock_pin ],
189- (uint )1 << PADS_BANK0_GPIO0_SLEWFAST_LSB ,
190- PADS_BANK0_GPIO0_SLEWFAST_BITS
191- );
185+ (uint )1 << PADS_BANK0_GPIO0_SLEWFAST_LSB ,
186+ PADS_BANK0_GPIO0_SLEWFAST_BITS
187+ );
192188
193189 #if CIRCUITPY_WIZNET_W6300
194190 printf ("\r\n[QSPI QUAD MODE]\r\n" );
@@ -202,7 +198,7 @@ wiznet_pio_spi_handle_t wiznet_pio_spi_open(const wiznet_pio_spi_config_t *wizne
202198 sm_config_set_out_shift (& sm_config , false, true, 8 );
203199
204200 hw_set_bits (& state -> pio -> input_sync_bypass ,
205- (1u << state -> spi_config -> data_io0_pin ) | (1u << state -> spi_config -> data_io1_pin ) | (1u << state -> spi_config -> data_io2_pin ) | (1u << state -> spi_config -> data_io3_pin ));
201+ (1u << state -> spi_config -> data_io0_pin ) | (1u << state -> spi_config -> data_io1_pin ) | (1u << state -> spi_config -> data_io2_pin ) | (1u << state -> spi_config -> data_io3_pin ));
206202 pio_sm_set_config (state -> pio , state -> pio_sm , & sm_config );
207203 pio_sm_set_consecutive_pindirs (state -> pio , state -> pio_sm , state -> spi_config -> clock_pin , 1 , true);
208204
@@ -259,8 +255,9 @@ void wiznet_pio_spi_close(wiznet_pio_spi_handle_t handle) {
259255 wiznet_pio_spi_state_t * state = (wiznet_pio_spi_state_t * )handle ;
260256 if (state ) {
261257 if (state -> pio_sm >= 0 ) {
262- if (state -> pio_offset != -1 )
258+ if (state -> pio_offset != -1 ) {
263259 pio_remove_program (state -> pio , & WIZNET_PIO_SPI_PROGRAM_FUNC , state -> pio_offset );
260+ }
264261
265262 pio_sm_unclaim (state -> pio , state -> pio_sm );
266263 }
@@ -289,17 +286,17 @@ static __noinline void ns_delay(uint32_t ns) {
289286static void wiznet_pio_spi_frame_start (void ) {
290287 assert (active_state );
291288 #if CIRCUITPY_WIZNET_W6300
292- gpio_set_function (active_state -> spi_config -> data_io0_pin , active_state -> pio_func_sel );
293- gpio_set_function (active_state -> spi_config -> data_io1_pin , active_state -> pio_func_sel );
294- gpio_set_function (active_state -> spi_config -> data_io2_pin , active_state -> pio_func_sel );
295- gpio_set_function (active_state -> spi_config -> data_io3_pin , active_state -> pio_func_sel );
289+ gpio_set_function (active_state -> spi_config -> data_io0_pin , active_state -> pio_func_sel );
290+ gpio_set_function (active_state -> spi_config -> data_io1_pin , active_state -> pio_func_sel );
291+ gpio_set_function (active_state -> spi_config -> data_io2_pin , active_state -> pio_func_sel );
292+ gpio_set_function (active_state -> spi_config -> data_io3_pin , active_state -> pio_func_sel );
296293
297- gpio_set_function (active_state -> spi_config -> clock_pin , active_state -> pio_func_sel );
298- gpio_pull_down (active_state -> spi_config -> clock_pin );
294+ gpio_set_function (active_state -> spi_config -> clock_pin , active_state -> pio_func_sel );
295+ gpio_pull_down (active_state -> spi_config -> clock_pin );
299296 #else
300- gpio_set_function (active_state -> spi_config -> data_out_pin , active_state -> pio_func_sel );
301- gpio_set_function (active_state -> spi_config -> clock_pin , active_state -> pio_func_sel );
302- gpio_pull_down (active_state -> spi_config -> clock_pin );
297+ gpio_set_function (active_state -> spi_config -> data_out_pin , active_state -> pio_func_sel );
298+ gpio_set_function (active_state -> spi_config -> clock_pin , active_state -> pio_func_sel );
299+ gpio_pull_down (active_state -> spi_config -> clock_pin );
303300 #endif
304301 // Pull CS low
305302 cs_set (active_state , false);
@@ -319,24 +316,23 @@ static void wiznet_pio_spi_frame_end(void) {
319316
320317#if CIRCUITPY_WIZNET_W6300
321318
322- bool wiznet_pio_spi_read_byte (uint8_t op_code , uint16_t AddrSel , uint8_t * rx , uint16_t rx_length )
323- {
319+ bool wiznet_pio_spi_read_byte (uint8_t op_code , uint16_t AddrSel , uint8_t * rx , uint16_t rx_length ) {
324320 uint8_t command_buf [8 ] = {0 ,};
325321 uint16_t command_len = mk_cmd_buf (command_buf , op_code , AddrSel );
326322 uint32_t loop_cnt = 0 ;
327323
328324 pio_sm_set_enabled (active_state -> pio , active_state -> pio_sm , false);
329325 pio_sm_set_wrap (active_state -> pio , active_state -> pio_sm , active_state -> pio_offset , active_state -> pio_offset + WIZNET_PIO_SPI_OFFSET_READ_BITS_END - 1 );
330- //pio_sm_set_wrap(active_state->pio, active_state->pio_sm, active_state->pio_offset + PIO_SPI_OFFSET_WRITE_BITS, active_state->pio_offset + PIO_SPI_OFFSET_READ_BITS_END - 1);
326+ // pio_sm_set_wrap(active_state->pio, active_state->pio_sm, active_state->pio_offset + PIO_SPI_OFFSET_WRITE_BITS, active_state->pio_offset + PIO_SPI_OFFSET_READ_BITS_END - 1);
331327 pio_sm_clear_fifos (active_state -> pio , active_state -> pio_sm );
332328
333329 loop_cnt = 2 ;
334330 pio_sm_set_pindirs_with_mask (active_state -> pio ,
335- active_state -> pio_sm ,
336- (1u << active_state -> spi_config -> data_io0_pin ) | (1u << active_state -> spi_config -> data_io1_pin ) | (1u << active_state -> spi_config -> data_io2_pin ) | (1u << active_state -> spi_config -> data_io3_pin ),
337- (1u << active_state -> spi_config -> data_io0_pin ) | (1u << active_state -> spi_config -> data_io1_pin ) | (1u << active_state -> spi_config -> data_io2_pin ) | (1u << active_state -> spi_config -> data_io3_pin ));
331+ active_state -> pio_sm ,
332+ (1u << active_state -> spi_config -> data_io0_pin ) | (1u << active_state -> spi_config -> data_io1_pin ) | (1u << active_state -> spi_config -> data_io2_pin ) | (1u << active_state -> spi_config -> data_io3_pin ),
333+ (1u << active_state -> spi_config -> data_io0_pin ) | (1u << active_state -> spi_config -> data_io1_pin ) | (1u << active_state -> spi_config -> data_io2_pin ) | (1u << active_state -> spi_config -> data_io3_pin ));
338334
339- /* @todo: Implement to use. */
335+ /* @todo: Implement to use. */
340336
341337 pio_sm_restart (active_state -> pio , active_state -> pio_sm );
342338 pio_sm_clkdiv_restart (active_state -> pio , active_state -> pio_sm );
@@ -383,8 +379,7 @@ bool wiznet_pio_spi_read_byte(uint8_t op_code, uint16_t AddrSel, uint8_t *rx, ui
383379 return true;
384380}
385381
386- bool wiznet_pio_spi_write_byte (uint8_t op_code , uint16_t AddrSel , const uint8_t * tx , uint16_t tx_length )
387- {
382+ bool wiznet_pio_spi_write_byte (uint8_t op_code , uint16_t AddrSel , const uint8_t * tx , uint16_t tx_length ) {
388383 uint8_t command_buf [8 ] = {0 ,};
389384 uint16_t command_len = mk_cmd_buf (command_buf , op_code , AddrSel );
390385 uint32_t loop_cnt = 0 ;
@@ -396,11 +391,9 @@ bool wiznet_pio_spi_write_byte(uint8_t op_code, uint16_t AddrSel, const uint8_t
396391
397392 loop_cnt = 2 ;
398393 pio_sm_set_pindirs_with_mask (active_state -> pio ,
399- active_state -> pio_sm ,
400- (1u << active_state -> spi_config -> data_io0_pin ) | (1u << active_state -> spi_config -> data_io1_pin ) | (1u << active_state -> spi_config -> data_io2_pin ) | (1u << active_state -> spi_config -> data_io3_pin ),
401- (1u << active_state -> spi_config -> data_io0_pin ) | (1u << active_state -> spi_config -> data_io1_pin ) | (1u << active_state -> spi_config -> data_io2_pin ) | (1u << active_state -> spi_config -> data_io3_pin ));
402-
403-
394+ active_state -> pio_sm ,
395+ (1u << active_state -> spi_config -> data_io0_pin ) | (1u << active_state -> spi_config -> data_io1_pin ) | (1u << active_state -> spi_config -> data_io2_pin ) | (1u << active_state -> spi_config -> data_io3_pin ),
396+ (1u << active_state -> spi_config -> data_io0_pin ) | (1u << active_state -> spi_config -> data_io1_pin ) | (1u << active_state -> spi_config -> data_io2_pin ) | (1u << active_state -> spi_config -> data_io3_pin ));
404397
405398 pio_sm_restart (active_state -> pio , active_state -> pio_sm );
406399 pio_sm_clkdiv_restart (active_state -> pio , active_state -> pio_sm );
@@ -412,7 +405,6 @@ bool wiznet_pio_spi_write_byte(uint8_t op_code, uint16_t AddrSel, const uint8_t
412405 pio_sm_exec (active_state -> pio , active_state -> pio_sm , pio_encode_jmp (active_state -> pio_offset ));
413406 dma_channel_abort (active_state -> dma_out );
414407
415-
416408 dma_channel_config out_config = dma_channel_get_default_config (active_state -> dma_out );
417409 channel_config_set_transfer_data_size (& out_config , DMA_SIZE_8 );
418410 channel_config_set_bswap (& out_config , true);
@@ -428,14 +420,13 @@ bool wiznet_pio_spi_write_byte(uint8_t op_code, uint16_t AddrSel, const uint8_t
428420 const uint32_t fdebug_tx_stall = 1u << (PIO_FDEBUG_TXSTALL_LSB + active_state -> pio_sm );
429421 active_state -> pio -> fdebug = fdebug_tx_stall ;
430422 // pio_sm_set_enabled(active_state->pio, active_state->pio_sm, true);
431- while (!(active_state -> pio -> fdebug & fdebug_tx_stall ))
432- {
433- tight_loop_contents (); // todo timeout
423+ while (!(active_state -> pio -> fdebug & fdebug_tx_stall )) {
424+ tight_loop_contents (); // todo timeout
434425 }
435426 #if 1
436427
437428 __compiler_memory_barrier ();
438- //pio_sm_set_enabled(active_state->pio, active_state->pio_sm, false);
429+ // pio_sm_set_enabled(active_state->pio, active_state->pio_sm, false);
439430
440431 pio_sm_set_consecutive_pindirs (active_state -> pio , active_state -> pio_sm , active_state -> spi_config -> data_io0_pin , 4 , false);
441432
@@ -521,14 +512,8 @@ bool wiznet_pio_spi_transfer(const uint8_t *tx, size_t tx_length, uint8_t *rx, s
521512 state -> pio -> fdebug = fDebugTxStall ;
522513 pio_sm_set_enabled (state -> pio , state -> pio_sm , true);
523514 while (!(state -> pio -> fdebug & fDebugTxStall )) {
524- // printf("WIZNET_PIO_SPI: waiting for tx stall\n");
525515 tight_loop_contents (); // todo timeout
526516 }
527- uint32_t timeout = 1000000 ;
528- if (timeout == 0 ) {
529- printf ("ERROR: PIO TXSTALL timeout!\n" );
530- return false;
531- }
532517 __compiler_memory_barrier ();
533518 pio_sm_set_enabled (state -> pio , state -> pio_sm , false);
534519 pio_sm_set_consecutive_pindirs (state -> pio , state -> pio_sm , state -> spi_config -> data_in_pin , 1 , false);
@@ -549,8 +534,8 @@ void wiznet_pio_spi_read_buffer(uint8_t *pBuf, uint16_t len) {
549534 #if CIRCUITPY_WIZNET_W6300
550535
551536 if (!wiznet_pio_spi_read_byte (active_state -> spi_header [0 ], (active_state -> spi_header [1 ] << 8 ) | active_state -> spi_header [2 ], pBuf , len )) {
552- panic ("spi failed writing header" );
553- }
537+ panic ("spi failed writing header" );
538+ }
554539
555540 #else // W55RP20
556541
@@ -562,10 +547,6 @@ void wiznet_pio_spi_read_buffer(uint8_t *pBuf, uint16_t len) {
562547
563548}
564549
565-
566- // If we have been asked to write a spi header already, then write it and the rest of the buffer
567- // or else if we've been given enough data for just the spi header, save it until the next call
568- // or we're writing a byte in which case we're given a buffer including the spi header
569550void wiznet_pio_spi_write_buffer (const uint8_t * pBuf , uint16_t len ) {
570551 assert (active_state );
571552
@@ -578,21 +559,21 @@ void wiznet_pio_spi_write_buffer(const uint8_t *pBuf, uint16_t len) {
578559 } else {
579560 if (active_state -> spi_header_count == WIZNET_PIO_SPI_HEADER_LEN ) {
580561 #if CIRCUITPY_WIZNET_W6300
581- if (!wiznet_pio_spi_write_byte (active_state -> spi_header [0 ], (active_state -> spi_header [1 ] << 8 ) | active_state -> spi_header [2 ], pBuf , len )) {
582- panic ("spi failed writing header" );
583- }
584- active_state -> spi_header_count = 0 ;
585- assert (active_state -> spi_header_count == 0 );
562+ if (!wiznet_pio_spi_write_byte (active_state -> spi_header [0 ], (active_state -> spi_header [1 ] << 8 ) | active_state -> spi_header [2 ], pBuf , len )) {
563+ panic ("spi failed writing header" );
564+ }
565+ active_state -> spi_header_count = 0 ;
566+ assert (active_state -> spi_header_count == 0 );
586567 #else
587- if (!wiznet_pio_spi_transfer (active_state -> spi_header , WIZNET_PIO_SPI_HEADER_LEN , NULL , 0 )) {
588- panic ("spi failed writing header" );
589- }
590- active_state -> spi_header_count = 0 ;
591- assert (active_state -> spi_header_count == 0 );
568+ if (!wiznet_pio_spi_transfer (active_state -> spi_header , WIZNET_PIO_SPI_HEADER_LEN , NULL , 0 )) {
569+ panic ("spi failed writing header" );
570+ }
571+ active_state -> spi_header_count = 0 ;
572+ assert (active_state -> spi_header_count == 0 );
592573
593- if (!wiznet_pio_spi_transfer (pBuf , len , NULL , 0 )) {
574+ if (!wiznet_pio_spi_transfer (pBuf , len , NULL , 0 )) {
594575 panic ("spi failed writing buffer" );
595- }
576+ }
596577 #endif
597578 }
598579 }
0 commit comments