Skip to content

Commit 212fa6c

Browse files
committed
add auto endpoint number
1 parent 8b9389f commit 212fa6c

File tree

7 files changed

+32
-22
lines changed

7 files changed

+32
-22
lines changed

cores/nRF5/usb/Adafruit_USBDevice.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,15 @@ Adafruit_USBDevice::Adafruit_USBDevice(void)
127127

128128
_desc_cfglen = sizeof(tusb_desc_configuration_t);
129129
_itf_count = 0;
130+
_epin_count = _epout_count = 1;
130131

131132
tud_desc_set.config = _desc_cfg;
132133
tud_desc_set.device = &_desc_device;
133134
}
134135

135-
// Add interface descriptor, interface number will be updated to match current count
136+
// Add interface descriptor
137+
// - Interface number will be updated to match current count
138+
// - Endpoint number is updated to be unique
136139
bool Adafruit_USBDevice::addInterface(Adafruit_USBInterface& itf)
137140
{
138141
uint8_t* desc = _desc_cfg+_desc_cfglen;
@@ -155,6 +158,10 @@ bool Adafruit_USBDevice::addInterface(Adafruit_USBInterface& itf)
155158
{
156159
// No alternate interface support
157160
((tusb_desc_interface_t*) desc)->bInterfaceNumber = _itf_count++;
161+
}else if (desc[1] == TUSB_DESC_ENDPOINT)
162+
{
163+
tusb_desc_endpoint_t* desc_ep = (tusb_desc_endpoint_t*) desc;
164+
desc_ep->bEndpointAddress |= (desc_ep->bEndpointAddress & 0x80) ? _epin_count++ : _epout_count++;
158165
}
159166

160167
if (desc[0] == 0) return false;

cores/nRF5/usb/Adafruit_USBDevice.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ class Adafruit_USBDevice
4343

4444
uint8_t _itf_count;
4545

46+
uint8_t _epin_count;
47+
uint8_t _epout_count;
48+
4649
public:
4750
Adafruit_USBDevice(void);
4851

cores/nRF5/usb/USBSerial.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
#include "Arduino.h"
2828
#include "tusb.h"
2929

30+
#define EPOUT 0x00
31+
#define EPIN 0x80
32+
3033
USBSerial Serial;
3134

3235
USBSerial::USBSerial(void)
@@ -37,8 +40,7 @@ USBSerial::USBSerial(void)
3740
uint16_t USBSerial::getDescriptor(uint8_t* buf, uint16_t bufsize)
3841
{
3942
// CDC is mostly always existed for DFU
40-
// Let's pick EP 1 & 2 for it
41-
uint8_t desc[] = { TUD_CDC_DESCRIPTOR(0, 0, 0x81, 8, 0x02, 0x82, 64) };
43+
uint8_t desc[] = { TUD_CDC_DESCRIPTOR(0, 0, EPIN, 8, EPOUT, EPIN, 64) };
4244
uint16_t const len = sizeof(desc);
4345

4446
if ( bufsize < len ) return 0;

cores/nRF5/usb/tusb_config.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,7 @@
6161
#define CFG_TUD_CDC 1
6262

6363
// disable msc for feather nrf52840 for now until have a more stable QSPI driver
64-
#ifdef ARDUINO_NRF52840_FEATHER
65-
#define CFG_TUD_MSC 0
66-
#else
6764
#define CFG_TUD_MSC 1
68-
#endif
69-
7065

7166
#define CFG_TUD_HID 0
7267
#define CFG_TUD_HID_KEYBOARD 0
@@ -97,11 +92,6 @@
9792
// Buffer size of Device Mass storage
9893
#define CFG_TUD_MSC_BUFSIZE 512
9994

100-
// Number of Blocks
101-
#include <stdint.h>
102-
extern uint32_t flash_qspi_size (void);
103-
#define CFG_TUD_MSC_BLOCK_NUM (flash_qspi_size() / CFG_TUD_MSC_BLOCK_SZ)
104-
10595
// Block size
10696
#define CFG_TUD_MSC_BLOCK_SZ 512
10797

cores/nRF5/usb/usb_msc_flash.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ int32_t tud_msc_read10_cb (uint8_t lun, uint32_t lba, uint32_t offset, void* buf
9696

9797
// Callback invoked when received WRITE10 command.
9898
// Process data in buffer to disk's storage and return number of written bytes
99-
int32_t tud_msc_write10_cb (uint8_t lun, uint32_t lba, uint32_t offset, void* buffer, uint32_t bufsize)
99+
int32_t tud_msc_write10_cb (uint8_t lun, uint32_t lba, uint32_t offset, uint8_t* buffer, uint32_t bufsize)
100100
{
101101
(void) lun;
102102

@@ -119,5 +119,13 @@ void tud_msc_write10_complete_cb (uint8_t lun)
119119
flash_qspi_flush();
120120
}
121121

122+
void tud_msc_capacity_cb(uint8_t lun, uint32_t* block_count, uint16_t* block_size)
123+
{
124+
(void) lun;
125+
126+
*block_count = flash_qspi_size() / CFG_TUD_MSC_BLOCK_SZ;
127+
*block_size = CFG_TUD_MSC_BUFSIZE;
128+
}
129+
122130
#endif // CFG_TUD_MSC
123131
#endif // nrf52840

libraries/Adafruit_USB_MSC/Adafruit_USBDev_MSC.cpp renamed to libraries/Adafruit_USBDev_MSC/Adafruit_USBDev_MSC.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,17 @@
2424

2525
#include "Adafruit_USBDev_MSC.h"
2626

27-
Adafruit_USBDev_MSC::Adafruit_USBDev_MSC(uint8_t epout, uint8_t epin, uint16_t epsize)
27+
#define EPOUT 0x00
28+
#define EPIN 0x80
29+
#define EPSIZE 64 // TODO must be 512 for highspeed device
30+
31+
Adafruit_USBDev_MSC::Adafruit_USBDev_MSC(void)
2832
{
29-
_epout = epout;
30-
_epin = epin;
31-
_epsize = epsize;
3233
}
3334

3435
uint16_t Adafruit_USBDev_MSC::getDescriptor(uint8_t* buf, uint16_t bufsize)
3536
{
36-
uint8_t desc[] = { TUD_MSC_DESCRIPTOR(0, 0, _epout, _epin, _epsize) };
37+
uint8_t desc[] = { TUD_MSC_DESCRIPTOR(0, 0, EPOUT, EPIN, EPSIZE) };
3738
uint16_t const len = sizeof(desc);
3839

3940
if ( bufsize < len ) return 0;

libraries/Adafruit_USB_MSC/Adafruit_USBDev_MSC.h renamed to libraries/Adafruit_USBDev_MSC/Adafruit_USBDev_MSC.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,9 @@
3030
class Adafruit_USBDev_MSC : Adafruit_USBInterface
3131
{
3232
private:
33-
uint16_t _epsize;
34-
uint8_t _epout, _epin;
33+
3534
public:
36-
Adafruit_USBDev_MSC(uint8_t epout, uint8_t epin, uint16_t epsize);
35+
Adafruit_USBDev_MSC(void);
3736

3837
void begin(void);
3938

0 commit comments

Comments
 (0)