@@ -54,6 +54,12 @@ enum {
5454 CPUCTL_ADDR = 16u << 3 , // 0x80
5555 PINCTL_ADDR = 17u << 3 , // 0x88
5656 REVISION_ADDR = 18u << 3 , // 0x90
57+ // 19 is not used
58+ IOPINS1_ADDR = 20u << 3 , // 0xA0
59+ IOPINS2_ADDR = 21u << 3 , // 0xA8
60+ GPINIRQ_ADDR = 22u << 3 , // 0xB0
61+ GPINIEN_ADDR = 23u << 3 , // 0xB8
62+ GPINPOL_ADDR = 24u << 3 , // 0xC0
5763 HIRQ_ADDR = 25u << 3 , // 0xC8
5864 HIEN_ADDR = 26u << 3 , // 0xD0
5965 MODE_ADDR = 27u << 3 , // 0xD8
@@ -207,7 +213,9 @@ typedef struct {
207213static max3421_data_t _hcd_data ;
208214
209215//--------------------------------------------------------------------+
210- // API: SPI transfer with MAX3421E, must be implemented by application
216+ // API: SPI transfer with MAX3421E
217+ // - spi_cs_api(), spi_xfer_api(), int_api(): must be implemented by application
218+ // - reg_read(), reg_write(): is implemented by this driver, can be used by application
211219//--------------------------------------------------------------------+
212220
213221// API to control MAX3421 SPI CS
@@ -220,11 +228,18 @@ extern bool tuh_max3421_spi_xfer_api(uint8_t rhport, uint8_t const* tx_buf, uint
220228// API to enable/disable MAX3421 INTR pin interrupt
221229extern void tuh_max3421_int_api (uint8_t rhport , bool enabled );
222230
231+ // API to read MAX3421's register. Implemented by TinyUSB
232+ uint8_t tuh_max3421_reg_read (uint8_t rhport , uint8_t reg , bool in_isr );
233+
234+ // API to write MAX3421's register. Implemented by TinyUSB
235+ bool tuh_max3421_reg_write (uint8_t rhport , uint8_t reg , uint8_t data , bool in_isr );
236+
223237//--------------------------------------------------------------------+
224- // SPI Helper
238+ // SPI Commands and Helper
225239//--------------------------------------------------------------------+
226- static void handle_connect_irq (uint8_t rhport , bool in_isr );
227- static inline void hirq_write (uint8_t rhport , uint8_t data , bool in_isr );
240+
241+ #define reg_read tuh_max3421_reg_read
242+ #define reg_write tuh_max3421_reg_write
228243
229244static void max3421_spi_lock (uint8_t rhport , bool in_isr ) {
230245 // disable interrupt and mutex lock (for pre-emptive RTOS) if not in_isr
@@ -248,61 +263,60 @@ static void max3421_spi_unlock(uint8_t rhport, bool in_isr) {
248263 }
249264}
250265
251- static void fifo_write (uint8_t rhport , uint8_t reg , uint8_t const * buffer , uint16_t len , bool in_isr ) {
252- uint8_t hirq ;
253- reg |= CMDBYTE_WRITE ;
266+ uint8_t tuh_max3421_reg_read (uint8_t rhport , uint8_t reg , bool in_isr ) {
267+ uint8_t tx_buf [ 2 ] = { reg , 0 } ;
268+ uint8_t rx_buf [ 2 ] = { 0 , 0 } ;
254269
255270 max3421_spi_lock (rhport , in_isr );
256-
257- tuh_max3421_spi_xfer_api (rhport , & reg , & hirq , 1 );
258- _hcd_data .hirq = hirq ;
259- tuh_max3421_spi_xfer_api (rhport , buffer , NULL , len );
260-
271+ bool ret = tuh_max3421_spi_xfer_api (rhport , tx_buf , rx_buf , 2 );
261272 max3421_spi_unlock (rhport , in_isr );
262273
274+ _hcd_data .hirq = rx_buf [0 ];
275+ return ret ? rx_buf [1 ] : 0 ;
263276}
264277
265- static void fifo_read (uint8_t rhport , uint8_t * buffer , uint16_t len , bool in_isr ) {
266- uint8_t hirq ;
267- uint8_t const reg = RCVVFIFO_ADDR ;
278+ bool tuh_max3421_reg_write (uint8_t rhport , uint8_t reg , uint8_t data , bool in_isr ) {
279+ uint8_t tx_buf [ 2 ] = { reg | CMDBYTE_WRITE , data } ;
280+ uint8_t rx_buf [ 2 ] = { 0 , 0 } ;
268281
269282 max3421_spi_lock (rhport , in_isr );
283+ bool ret = tuh_max3421_spi_xfer_api (rhport , tx_buf , rx_buf , 2 );
284+ max3421_spi_unlock (rhport , in_isr );
270285
271- tuh_max3421_spi_xfer_api (rhport , & reg , & hirq , 1 );
272- _hcd_data .hirq = hirq ;
273- tuh_max3421_spi_xfer_api (rhport , NULL , buffer , len );
286+ // HIRQ register since we are in full-duplex mode
287+ _hcd_data .hirq = rx_buf [0 ];
274288
275- max3421_spi_unlock ( rhport , in_isr ) ;
289+ return ret ;
276290}
277291
278- static void reg_write (uint8_t rhport , uint8_t reg , uint8_t data , bool in_isr ) {
279- uint8_t tx_buf [ 2 ] = { reg | CMDBYTE_WRITE , data } ;
280- uint8_t rx_buf [ 2 ] = { 0 , 0 } ;
292+ static void fifo_write (uint8_t rhport , uint8_t reg , uint8_t const * buffer , uint16_t len , bool in_isr ) {
293+ uint8_t hirq ;
294+ reg |= CMDBYTE_WRITE ;
281295
282296 max3421_spi_lock (rhport , in_isr );
283297
284- tuh_max3421_spi_xfer_api (rhport , tx_buf , rx_buf , 2 );
298+ tuh_max3421_spi_xfer_api (rhport , & reg , & hirq , 1 );
299+ _hcd_data .hirq = hirq ;
300+ tuh_max3421_spi_xfer_api (rhport , buffer , NULL , len );
285301
286302 max3421_spi_unlock (rhport , in_isr );
287303
288- // HIRQ register since we are in full-duplex mode
289- _hcd_data .hirq = rx_buf [0 ];
290304}
291305
292- static uint8_t reg_read (uint8_t rhport , uint8_t reg , bool in_isr ) {
293- uint8_t tx_buf [ 2 ] = { reg , 0 } ;
294- uint8_t rx_buf [ 2 ] = { 0 , 0 } ;
306+ static void fifo_read (uint8_t rhport , uint8_t * buffer , uint16_t len , bool in_isr ) {
307+ uint8_t hirq ;
308+ uint8_t const reg = RCVVFIFO_ADDR ;
295309
296310 max3421_spi_lock (rhport , in_isr );
297311
298- bool ret = tuh_max3421_spi_xfer_api (rhport , tx_buf , rx_buf , 2 );
312+ tuh_max3421_spi_xfer_api (rhport , & reg , & hirq , 1 );
313+ _hcd_data .hirq = hirq ;
314+ tuh_max3421_spi_xfer_api (rhport , NULL , buffer , len );
299315
300316 max3421_spi_unlock (rhport , in_isr );
301-
302- _hcd_data .hirq = rx_buf [0 ];
303- return ret ? rx_buf [1 ] : 0 ;
304317}
305318
319+ //------------- register write helper -------------//
306320static inline void hirq_write (uint8_t rhport , uint8_t data , bool in_isr ) {
307321 reg_write (rhport , HIRQ_ADDR , data , in_isr );
308322 // HIRQ write 1 is clear
0 commit comments