Skip to content

Commit 3e5ccd7

Browse files
committed
Adding solutions
1 parent 11dfe62 commit 3e5ccd7

File tree

258 files changed

+67227
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

258 files changed

+67227
-0
lines changed

MKR1010_USBIso_Core_v1/CDCIso.cpp

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/*
2+
* CDCIso.cpp
3+
*
4+
* Created: 23/04/2020 10:34:21
5+
* Author: Gebruiker
6+
*/
7+
8+
#include <arduino.h>
9+
#include "CDCIso.h"
10+
#include <USB/USBCore.h>
11+
#include <USB/PluggableUSB.h>
12+
#include <USB/CDC.h>
13+
14+
CDCIso_::CDCIso_(void) : PluggableUSBModule(2,2,epIsoType)
15+
{
16+
epIsoType[0] = USB_ENDPOINT_TYPE_BULK | USB_ENDPOINT_IN(0);
17+
epIsoType[1] = USB_ENDPOINT_TYPE_ISOCHRONOUS | USB_ENDPOINT_IN(0);
18+
PluggableUSB().plug(this);
19+
}
20+
21+
int CDCIso_::getInterface(uint8_t* interfaceNum)
22+
{
23+
24+
interfaceNum[0] += 1;
25+
26+
epEPNum[0]=pluggedEndpoint;
27+
epEPNum[1]=pluggedEndpoint+1;
28+
29+
CDCIsoDescriptor _cdcIsoInterface = {
30+
31+
// CDC Iso interface
32+
D_INTERFACE(uint8_t(pluggedInterface + 1), 2, 0xFF, 0, 0),
33+
D_ENDPOINT(USB_ENDPOINT_OUT(pluggedEndpoint), USB_ENDPOINT_TYPE_BULK, EPX_SIZE, 0),
34+
D_ENDPOINT(USB_ENDPOINT_IN (pluggedEndpoint+1), USB_ENDPOINT_TYPE_ISOCHRONOUS, 1023, 1)
35+
};
36+
return USBDevice.sendControl(&_cdcIsoInterface, sizeof(_cdcIsoInterface));
37+
38+
}
39+
40+
bool CDCIso_::setup(USBSetup& setup)
41+
{
42+
return false;
43+
}
44+
45+
int CDCIso_::getDescriptor(USBSetup& setup)
46+
{
47+
return 0;
48+
}
49+
50+
void CDCIso_::enableInterrupt() {
51+
USB->DEVICE.DeviceEndpoint[epEPNum[0]].EPINTENSET.bit.TRCPT0=1; //TO DO CHECK RIGHT BANK?
52+
USB->DEVICE.DeviceEndpoint[epEPNum[1]].EPINTENSET.bit.TRCPT1=1;
53+
}
54+
55+
void CDCIso_::handleEndpoint(int ep) {
56+
if (ep==epEPNum[1]) {
57+
lenwritebuffer=sizeof(writebuffer);
58+
write(writebuffer,1023);
59+
}
60+
}
61+
62+
size_t CDCIso_::write(const uint8_t *buffer, size_t size)
63+
{
64+
uint32_t r = send(epEPNum[1], buffer, size);
65+
66+
if (r > 0) {
67+
return r;
68+
} else {
69+
//setWriteError();
70+
return 0;
71+
}
72+
}
73+
74+
uint32_t CDCIso_::send(uint32_t ep, const void *data, uint32_t len)
75+
{
76+
uint32_t written = 0;
77+
uint32_t length = 0;
78+
79+
if (!usbConfiguration)
80+
return -1;
81+
if (len > 16384)
82+
return -1;
83+
84+
#ifdef PIN_LED_TXL
85+
if (txLEDPulse == 0)
86+
digitalWrite(PIN_LED_TXL, LOW);
87+
88+
txLEDPulse = TX_RX_LED_PULSE_MS;
89+
#endif
90+
91+
// Flash area
92+
while (len != 0)
93+
{
94+
if (USB->DEVICE.DeviceEndpoint[epEPNum[1]].EPSTATUS.bit.BK1RDY) { //USBCore equivalent: usbd.epBank1IsReady(ep) or usb.DeviceEndpoint[ep].EPSTATUS.bit.BK1RDY;
95+
// previous transfer is still not complete
96+
// convert the timeout from microseconds to a number of times through
97+
// the wait loop; it takes (roughly) 23 clock cycles per iteration.
98+
uint32_t timeout = microsecondsToClockCycles(70 * 1000) / 23; //70msec
99+
// Wait for (previous) transfer to complete
100+
// inspired by Paul Stoffregen's work on Teensy
101+
while (!USB->DEVICE.DeviceEndpoint[epEPNum[1]].EPINTFLAG.bit.TRCPT1) { //USBCore equivalent: usbd.epBank1IsTransferComplete(ep) or usb.DeviceEndpoint[ep].EPINTFLAG.bit.TRCPT1;
102+
if (LastTransmitTimedOut[1] || timeout-- == 0) {
103+
LastTransmitTimedOut[1] = 1;
104+
// set byte count to zero, so that ZLP is sent
105+
// instead of stale data
106+
*(EPn_BK1_Addr.b8+0x004) = *(EPn_BK1_Addr.b8+0x004) & (0xFFFFFFF0); //Byte count on position 0 to 7 set to 0 //USBCore equivalent: usbd.epBank1SetByteCount(ep, 0) or EP[ep].DeviceDescBank[1].PCKSIZE.bit.BYTE_COUNT = bc;
107+
//return -1;
108+
}
109+
}
110+
}
111+
112+
LastTransmitTimedOut[1] = 0;
113+
114+
if (len >= iso_pcksize) {
115+
*(EPn_BK1_Addr.b8+0x004) = *(EPn_BK1_Addr.b8+0x004) | (0x1uL << 32); // Auto ZLP on position 31 //USBCore equivalent code: usbd.epBank1EnableAutoZLP(ep) or EP[ep].DeviceDescBank[1].PCKSIZE.bit.AUTO_ZLP = 1
116+
length = iso_pcksize;
117+
} else {
118+
length = len;
119+
}
120+
121+
/* memcopy could be safer in multi threaded environment */
122+
memcpy(&databuffer, data, length);
123+
124+
//set Byte count to length of data
125+
*(uint32_t *) (EPn_BK1_Addr.b8+0x004) = *(uint32_t *) (EPn_BK1_Addr.b8+0x004) | (length); //Byte count on position 0 to 13 set to length //USBCore equivalent: usbd.epBank1SetByteCount(ep, 0) or EP[ep].DeviceDescBank[1].PCKSIZE.bit.BYTE_COUNT = bc;
126+
127+
// Clear the transfer complete flag
128+
USB->DEVICE.DeviceEndpoint[epEPNum[1]].EPINTFLAG.reg = ((0x3ul << 0) & (2 << 0));//USBCore equivalent code: usbd.epBank1AckTransferComplete(ep) or usb.DeviceEndpoint[ep].EPINTFLAG.reg = USB_DEVICE_EPINTFLAG_TRCPT(2);
129+
130+
// RAM buffer is full, we can send data (IN)
131+
USB->DEVICE.DeviceEndpoint[epEPNum[1]].EPSTATUSSET.bit.BK1RDY=1; //USBCore equivalent code: usbd.epBank1SetReady(ep) or usb.DeviceEndpoint[ep].EPSTATUSSET.bit.BK1RDY = 1
132+
133+
written += length;
134+
len -= length;
135+
data = (char *)data + length;
136+
137+
}
138+
139+
return written;
140+
}

MKR1010_USBIso_Core_v1/CDCIso.h

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* CDCIso.h
3+
*
4+
* Created: 23/04/2020 10:34:00
5+
* Author: Gebruiker
6+
*/
7+
8+
9+
#ifndef CDCISO_H_
10+
#define CDCISO_H_
11+
12+
#include <Arduino.h>
13+
#include <USB/PluggableUSB.h>
14+
#include <USB/USBCore.h>
15+
16+
17+
typedef struct
18+
{
19+
//Iso
20+
InterfaceDescriptor dif;
21+
EndpointDescriptor in;
22+
EndpointDescriptor out;
23+
} CDCIsoDescriptor;
24+
25+
union MemAddr {
26+
uint32_t* b32;
27+
uint8_t* b8;
28+
};
29+
30+
class CDCIso_ : public PluggableUSBModule {
31+
public:
32+
CDCIso_(void);
33+
bool begin(void);
34+
void enableInterrupt();
35+
size_t write(const uint8_t *buffer, size_t size);
36+
37+
38+
uint8_t writebuffer[1023];
39+
uint8_t lenwritebuffer;
40+
uint8_t databuffer[1023];
41+
42+
uint32_t iso_pcksize;
43+
uint8_t iso_pckcode;
44+
45+
uint32_t usbConfiguration = 0;
46+
47+
uint8_t epEPNum[2];
48+
USBSetup EP0_BK0;
49+
uint32_t* ptrEP0_BK0;
50+
MemAddr EP0_BK0_Addr;
51+
MemAddr EPn_BK1_Addr;
52+
53+
protected:
54+
int getInterface(uint8_t* interfaceNum);
55+
bool setup(USBSetup& setup);
56+
int getDescriptor(USBSetup& setup);
57+
void handleEndpoint(int ep);
58+
59+
private:
60+
uint32_t epIsoType[2];
61+
uint32_t send(uint32_t ep, const void *data, uint32_t len);
62+
63+
char LastTransmitTimedOut[2] = {0,0};
64+
};
65+
66+
67+
#endif /* CDCISO_H_ */
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* CDCIso_event.h
3+
*
4+
* Created: 25/04/2020 2:50:22
5+
* Author: Gebruiker
6+
*/
7+
8+
9+
10+
#include <Arduino.h>
11+
#include "CDCIso.h"
12+
#include <USB/SAMD21_USBDevice.h>
13+
#include <USB/USBAPI.h>
14+
15+
#ifndef CDCISO_EVENT_H_
16+
#define CDCISO_EVENT_H_
17+
18+
19+
20+
21+
CDCIso_ USBISO;
22+
23+
24+
void UDD_Handler_Iso(void) {
25+
26+
if (USB->DEVICE.DeviceEndpoint[0].EPINTFLAG.bit.RXSTP) { //USBCore equivalent code:usbd.epBank0IsSetupReceived(0) or usb.DeviceEndpoint[ep].EPINTFLAG.bit.RXSTP;
27+
USBISO.EP0_BK0_Addr.b32=(uint32_t *) (USB->DEVICE.DESCADD.reg);
28+
//DESCADD contains the address (=> cast to 32 bit adddress) to the pointer that contains (=>request content) the address (cast to 32bit address) to the EP0 BK0 content
29+
memcpy(&(USBISO.EP0_BK0), (uint32_t *) *((uint32_t *) (USB->DEVICE.DESCADD.reg)), sizeof(USBSetup));
30+
if (REQUEST_STANDARD == (USBISO.EP0_BK0.bmRequestType & REQUEST_TYPE)) {
31+
switch (USBISO.EP0_BK0.bRequest) {
32+
case SET_CONFIGURATION:
33+
if (REQUEST_DEVICE == (USBISO.EP0_BK0.bmRequestType & REQUEST_RECIPIENT)) {
34+
switch (USBISO.iso_pcksize) { //EP[ep].DeviceDescBank[1].PCKSIZE.bit.SIZE
35+
case 8:
36+
USBISO.iso_pckcode = 0x0;
37+
case 16:
38+
USBISO.iso_pckcode = 0x01;
39+
case 32:
40+
USBISO.iso_pckcode = 0x02;
41+
case 64:
42+
USBISO.iso_pckcode = 0x03;
43+
case 128:
44+
USBISO.iso_pckcode = 0x04;
45+
case 256:
46+
USBISO.iso_pckcode = 0x05;
47+
case 512:
48+
USBISO.iso_pckcode = 0x06;
49+
case 1024:
50+
USBISO.iso_pckcode = 0x07; //1024 is necessary is the check below is >= and otherwise always AutoZLP!! //TO DO define HS
51+
}
52+
USBISO.EPn_BK1_Addr.b8 = USBISO.EP0_BK0_Addr.b8 + 2*0x10*USBISO.epEPNum[1]+0x10; //EPn has offset 2x (0xn0) //BK1 has offset 0x10
53+
*(USBISO.EPn_BK1_Addr.b32) = (uint32_t) &(USBISO.databuffer); //Set EPn_BK1 32bit content (=> cast to 32bit value) to address of data_iso_buffer; Equivalent to USBCore statement: usbd.epBank1SetAddress(ep, &udd_ep_in_cache_buffer[ep]);
54+
*(uint32_t *)(USBISO.EPn_BK1_Addr.b8+0x004) = *(uint32_t *) (USBISO.EPn_BK1_Addr.b8+0x004) | ((uint32_t) USBISO.iso_pckcode << 28); //PckSize has offset 0x004; 0x7 on position 28 means 1023bit; Equivalent to USBCore statement: usbd.epBank1SetSize(ep, 1023);
55+
USB->DEVICE.DeviceEndpoint[USBISO.epEPNum[1]].EPCFG.bit.EPTYPE1 = 2; //0x02 stands for IN Isochronous
56+
USBISO.usbConfiguration = USBISO.EP0_BK0.wValueL;
57+
USBISO.enableInterrupt();
58+
//leave it to the USBCore to handle the other EP's
59+
}
60+
}
61+
}
62+
}
63+
USBDevice.ISRHandler();
64+
65+
}
66+
67+
68+
69+
#endif /* CDCISO_EVENT_H_ */
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include "CDCIso.h"
2+
#include "CDCIso_event.h"
3+
4+
void setup() {
5+
// put your setup code here, to run once:
6+
USBISO.iso_pcksize=1024;
7+
USB_SetHandler(UDD_Handler_Iso);
8+
SerialUSB.print("test");
9+
delay(1000);
10+
SerialUSB.print("test");
11+
delay(1000);
12+
for (int i=0; i<1023;i++) {
13+
USBISO.writebuffer[i]=0xAA;
14+
}
15+
USBISO.write(USBISO.writebuffer,1023);
16+
17+
}
18+
19+
void loop() {
20+
// put your main code here, to run repeatedly:
21+
22+
}
77.5 KB
Binary file not shown.
57.6 MB
Binary file not shown.
460 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)