Skip to content

Commit ea80c18

Browse files
committed
update tinyusb with latest tuh_max3421_spi_xfer_api() signature
tested with nrf52840, samd21, samd51
1 parent 34c5949 commit ea80c18

File tree

6 files changed

+38
-37
lines changed

6 files changed

+38
-37
lines changed

src/arduino/Adafruit_USBH_Host.cpp

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ void tuh_max3421_spi_cs_api(uint8_t rhport, bool active) {
153153
}
154154

155155
bool tuh_max3421_spi_xfer_api(uint8_t rhport, uint8_t const *tx_buf,
156-
size_t tx_len, uint8_t *rx_buf, size_t rx_len) {
156+
uint8_t *rx_buf, size_t xfer_bytes) {
157157
(void)rhport;
158158

159159
if (!Adafruit_USBH_Host::_instance) {
@@ -167,27 +167,32 @@ bool tuh_max3421_spi_xfer_api(uint8_t rhport, uint8_t const *tx_buf,
167167
// SAMD 21/51 can only work reliably at 12MHz
168168
uint32_t const max_clock = 12000000ul;
169169
#else
170-
uint32_t const max_clock = 26000000ul;
170+
// uint32_t const max_clock = 26000000ul;
171+
uint32_t const max_clock = 4000000ul;
171172
#endif
172173

173174
SPISettings config(max_clock, MSBFIRST, SPI_MODE0);
174175
host->_spi->beginTransaction(config);
175176

176-
// TODO improve spi speed with burst transfer
177-
for (size_t count = 0; count < tx_len || count < rx_len; count++) {
177+
#ifdef ARDUINO_ARCH_SAMD
178+
// SAMD cannot use transfer(tx_buf, rx_buf, len) API since it default to use
179+
// DMA. However, since this can be invoked within EIC_Handler (ISR) which may
180+
// have priority higher than DMA ISR. That will cause blocking wait for
181+
// dma_busy not working
182+
for (size_t count = 0; count < xfer_bytes; count++) {
178183
uint8_t data = 0x00;
179-
180-
// TX
181-
if (count < tx_len) {
184+
if (tx_buf) {
182185
data = tx_buf[count];
183186
}
184187
data = host->_spi->transfer(data);
185188

186-
// RX
187-
if (count < rx_len) {
189+
if (rx_buf) {
188190
rx_buf[count] = data;
189191
}
190192
}
193+
#else
194+
host->_spi->transfer(tx_buf, rx_buf, xfer_bytes);
195+
#endif
191196

192197
host->_spi->endTransaction();
193198
return true;
@@ -241,7 +246,8 @@ void tuh_max3421_int_api(uint8_t rhport, bool enabled) {
241246
#error "MAX3421e host is not Unsupported by this architecture"
242247
#endif
243248
}
244-
}
249+
250+
} // extern C
245251
#endif
246252

247253
#endif

src/arduino/Adafruit_USBH_Host.h

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
extern "C" {
3737
void tuh_max3421_spi_cs_api(uint8_t rhport, bool active);
3838
bool tuh_max3421_spi_xfer_api(uint8_t rhport, uint8_t const *tx_buf,
39-
size_t tx_len, uint8_t *rx_buf, size_t rx_len);
39+
uint8_t *rx_buf, size_t xfer_bytes);
4040
void tuh_max3421_int_api(uint8_t rhport, bool enabled);
4141
}
4242

@@ -66,17 +66,9 @@ class Adafruit_USBH_Host {
6666
static Adafruit_USBH_Host *_instance;
6767

6868
private:
69-
// uint16_t const *descrip`tor_string_cb(uint8_t index, uint16_t langid);
70-
//
71-
// friend uint8_t const *tud_descriptor_device_cb(void);
72-
// friend uint8_t const *tud_descriptor_configuration_cb(uint8_t index);
73-
// friend uint16_t const *tud_descriptor_string_cb(uint8_t index,
74-
// uint16_t langid);
75-
7669
friend void tuh_max3421_spi_cs_api(uint8_t rhport, bool active);
7770
friend bool tuh_max3421_spi_xfer_api(uint8_t rhport, uint8_t const *tx_buf,
78-
size_t tx_len, uint8_t *rx_buf,
79-
size_t rx_len);
71+
uint8_t *rx_buf, size_t xfer_bytes);
8072
friend void tuh_max3421_int_api(uint8_t rhport, bool enabled);
8173
};
8274

src/class/hid/hid_device.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
//--------------------------------------------------------------------+
3737
// INCLUDE
3838
//--------------------------------------------------------------------+
39-
4039
#include "device/usbd.h"
4140
#include "device/usbd_pvt.h"
4241

src/class/midi/midi_device.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
//--------------------------------------------------------------------+
3737
// INCLUDE
3838
//--------------------------------------------------------------------+
39-
4039
#include "device/usbd.h"
4140
#include "device/usbd_pvt.h"
4241

src/class/usbtmc/usbtmc_device.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,15 @@
6767
// Ability to defer status byte transmission
6868
// Transmission of status byte in response to USB488 SRQ condition
6969

70-
#include "tusb_option.h"
71-
72-
#if (CFG_TUD_ENABLED && CFG_TUD_USBTMC)
73-
7470
// ESP32 out-of-sync
7571
#ifdef ARDUINO_ARCH_ESP32
7672
#include "arduino/ports/esp32/tusb_config_esp32.h"
7773
#endif
7874

75+
#include "tusb_option.h"
76+
77+
#if (CFG_TUD_ENABLED && CFG_TUD_USBTMC)
78+
7979
#include "device/usbd.h"
8080
#include "device/usbd_pvt.h"
8181

src/portable/analog/max3421/hcd_max3421.c

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -215,16 +215,21 @@ static max3421_data_t _hcd_data;
215215
// API: SPI transfer with MAX3421E, must be implemented by application
216216
//--------------------------------------------------------------------+
217217

218-
void tuh_max3421_spi_cs_api(uint8_t rhport, bool active);
219-
bool tuh_max3421_spi_xfer_api(uint8_t rhport, uint8_t const * tx_buf, size_t tx_len, uint8_t * rx_buf, size_t rx_len);
220-
void tuh_max3421_int_api(uint8_t rhport, bool enabled);
218+
// API to control MAX3421 SPI CS
219+
extern void tuh_max3421_spi_cs_api(uint8_t rhport, bool active);
221220

222-
static void handle_connect_irq(uint8_t rhport, bool in_isr);
223-
static inline void hirq_write(uint8_t rhport, uint8_t data, bool in_isr);
221+
// API to transfer data with MAX3421 SPI
222+
// Either tx_buf or rx_buf can be NULL, which means transfer is write or read only
223+
extern bool tuh_max3421_spi_xfer_api(uint8_t rhport, uint8_t const* tx_buf, uint8_t* rx_buf, size_t xfer_bytes);
224+
225+
// API to enable/disable MAX3421 INTR pin interrupt
226+
extern void tuh_max3421_int_api(uint8_t rhport, bool enabled);
224227

225228
//--------------------------------------------------------------------+
226229
// SPI Helper
227230
//--------------------------------------------------------------------+
231+
static void handle_connect_irq(uint8_t rhport, bool in_isr);
232+
static inline void hirq_write(uint8_t rhport, uint8_t data, bool in_isr);
228233

229234
static void max3421_spi_lock(uint8_t rhport, bool in_isr) {
230235
// disable interrupt and mutex lock (for pre-emptive RTOS) if not in_isr
@@ -254,9 +259,9 @@ static void fifo_write(uint8_t rhport, uint8_t reg, uint8_t const * buffer, uint
254259

255260
max3421_spi_lock(rhport, in_isr);
256261

257-
tuh_max3421_spi_xfer_api(rhport, &reg, 1, &hirq, 1);
262+
tuh_max3421_spi_xfer_api(rhport, &reg, &hirq, 1);
258263
_hcd_data.hirq = hirq;
259-
tuh_max3421_spi_xfer_api(rhport, buffer, len, NULL, 0);
264+
tuh_max3421_spi_xfer_api(rhport, buffer, NULL, len);
260265

261266
max3421_spi_unlock(rhport, in_isr);
262267

@@ -268,9 +273,9 @@ static void fifo_read(uint8_t rhport, uint8_t * buffer, uint16_t len, bool in_is
268273

269274
max3421_spi_lock(rhport, in_isr);
270275

271-
tuh_max3421_spi_xfer_api(rhport, &reg, 1, &hirq, 1);
276+
tuh_max3421_spi_xfer_api(rhport, &reg, &hirq, 1);
272277
_hcd_data.hirq = hirq;
273-
tuh_max3421_spi_xfer_api(rhport, NULL, 0, buffer, len);
278+
tuh_max3421_spi_xfer_api(rhport, NULL, buffer, len);
274279

275280
max3421_spi_unlock(rhport, in_isr);
276281
}
@@ -281,7 +286,7 @@ static void reg_write(uint8_t rhport, uint8_t reg, uint8_t data, bool in_isr) {
281286

282287
max3421_spi_lock(rhport, in_isr);
283288

284-
tuh_max3421_spi_xfer_api(rhport, tx_buf, 2, rx_buf, 2);
289+
tuh_max3421_spi_xfer_api(rhport, tx_buf, rx_buf, 2);
285290

286291
max3421_spi_unlock(rhport, in_isr);
287292

@@ -295,7 +300,7 @@ static uint8_t reg_read(uint8_t rhport, uint8_t reg, bool in_isr) {
295300

296301
max3421_spi_lock(rhport, in_isr);
297302

298-
bool ret = tuh_max3421_spi_xfer_api(rhport, tx_buf, 2, rx_buf, 2);
303+
bool ret = tuh_max3421_spi_xfer_api(rhport, tx_buf, rx_buf, 2);
299304

300305
max3421_spi_unlock(rhport, in_isr);
301306

0 commit comments

Comments
 (0)