Skip to content

Commit 86533a5

Browse files
committed
[sam] first USB with Init/Attach/Detach
1 parent 08b6140 commit 86533a5

File tree

7 files changed

+221
-24
lines changed

7 files changed

+221
-24
lines changed

hardware/arduino/sam/cores/sam/USB/USBAPI.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ class USB_
1515
USB_();
1616
bool configured();
1717

18-
void attach();
19-
void detach(); // Serial port goes down too...
18+
bool attach();
19+
bool detach(); // Serial port goes down too...
2020
void poll();
2121
};
2222
extern USB_ USB;

hardware/arduino/sam/cores/sam/USB/USBCore.cpp.disabled

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,9 @@ const DeviceDescriptor USB_DeviceDescriptorA =
9494
//==================================================================
9595
//==================================================================
9696

97-
volatile uint8_t _usbConfiguration = 0;
98-
uint8_t _cdcComposite = 0;
97+
volatile uint32_t _usbConfiguration = 0;
98+
volatile uint32_t _usbInitialized = 0;
99+
uint32_t _cdcComposite = 0;
99100

100101

101102
//==================================================================
@@ -129,7 +130,7 @@ int USBD_Recv(uint8_t ep, void* d, int len)
129130
}
130131

131132
n = FifoByteCount(ep);
132-
133+
133134
len = min(n,len);
134135
n = len;
135136
dst = (uint8_t*)d;
@@ -151,16 +152,16 @@ static bool USBD_SendControl(uint8_t d)
151152
{
152153
return false;
153154
}
154-
155+
155156
Send8( d ) ;
156-
157+
157158
if ( !((_cmark + 1) & 0x3F) )
158159
{
159160
ClearIN(); // Fifo is full, release this packet
160161
}
161162
}
162163
_cmark++;
163-
164+
164165
return true ;
165166
}
166167

@@ -173,7 +174,7 @@ int USBD_SendControl(uint8_t flags, const void* d, int len)
173174
while ( len-- )
174175
{
175176
uint8_t c = *data++ ;
176-
177+
177178
if ( !SendControl( c ) )
178179
{
179180
return -1;
@@ -293,7 +294,7 @@ static bool USBD_SendDescriptor(Setup& setup)
293294
{
294295
return false ;
295296
}
296-
297+
297298
if ( desc_length == 0 )
298299
{
299300
desc_length = *desc_addr;
@@ -308,7 +309,7 @@ static bool USBD_SendDescriptor(Setup& setup)
308309
void USB_ISR()
309310
{
310311
SetEP(0) ;
311-
312+
312313
if ( !ReceivedSetupInt() )
313314
{
314315
return;
@@ -367,7 +368,7 @@ void USB_ISR()
367368
{
368369
InitEndpoints();
369370
_usbConfiguration = setup.wValueL;
370-
}
371+
}
371372
else
372373
{
373374
ok = false;
@@ -458,17 +459,38 @@ USB_ USB;
458459

459460
USB_::USB_()
460461
{
462+
if ( USBD_Init() == 0UL )
463+
{
464+
_usbInitialized=1UL ;
465+
}
461466
}
462467

463-
void USB_::attach(void)
468+
bool USB_::attach(void)
464469
{
465-
USBD_Attach() ;
470+
if ( _usbInitialized != 0UL )
471+
{
472+
USBD_Attach() ;
473+
474+
return true ;
475+
}
476+
else
477+
{
478+
return false ;
479+
}
466480
}
467481

468-
void USB_::detach(void)
482+
bool USB_::detach(void)
469483
{
470-
UDPHS->UDPHS_CTRL |= UDPHS_CTRL_DETACH; // detach
471-
UDPHS->UDPHS_CTRL &= ~UDPHS_CTRL_PULLD_DIS; // Enable Pull Down
484+
if ( _usbInitialized != 0UL )
485+
{
486+
USBD_Detach() ;
487+
488+
return true ;
489+
}
490+
else
491+
{
492+
return false ;
493+
}
472494
}
473495

474496
// Check for interrupts

hardware/arduino/sam/cores/sam/build_gcc/libarduino_sam3u_ek.mk

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,6 @@ create_output:
148148
# @echo *$(A_SRC)
149149
# @echo -------------------------
150150

151-
# -@mkdir $(subst /,$(SEP),$(OUTPUT_BIN)) 1>NUL 2>&1
152-
-mkdir $(subst /,$(SEP),$(OUTPUT_BIN))
153151
-@mkdir $(OUTPUT_PATH) 1>NUL 2>&1
154152

155153
$(addprefix $(OUTPUT_PATH)/,$(C_OBJ)): $(OUTPUT_PATH)/%.o: %.c

hardware/arduino/sam/cores/sam/build_gcc/libarduino_sam3x_ek.mk

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,6 @@ create_output:
148148
# @echo *$(A_SRC)
149149
# @echo -------------------------
150150

151-
# -@mkdir $(subst /,$(SEP),$(OUTPUT_BIN)) 1>NUL 2>&1
152-
-mkdir $(subst /,$(SEP),$(OUTPUT_BIN))
153151
-@mkdir $(OUTPUT_PATH) 1>NUL 2>&1
154152

155153
$(addprefix $(OUTPUT_PATH)/,$(C_OBJ)): $(OUTPUT_PATH)/%.o: %.c

hardware/arduino/sam/system/libsam/include/USB_device.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
99
This library is distributed in the hope that it will be useful,
1010
but WITHOUT ANY WARRANTY; without even the implied warranty of
11-
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
1212
See the GNU Lesser General Public License for more details.
1313
1414
You should have received a copy of the GNU Lesser General Public
@@ -64,6 +64,8 @@ extern void USBD_InitEndpoints(void) ;
6464

6565
extern void USBD_InitControl(int end) ;
6666

67+
extern uint32_t USBD_Init(void) ;
68+
6769
extern void USBD_Attach(void) ;
6870
extern void USBD_Detach(void) ;
6971

hardware/arduino/sam/system/libsam/include/uotghs.h

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
99
This library is distributed in the hope that it will be useful,
1010
but WITHOUT ANY WARRANTY; without even the implied warranty of
11-
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
1212
See the GNU Lesser General Public License for more details.
1313
1414
You should have received a copy of the GNU Lesser General Public
@@ -19,4 +19,29 @@
1919
#ifndef UOTGHS_H_INCLUDED
2020
#define UOTGHS_H_INCLUDED
2121

22+
#define EP_SINGLE_64 (0x32UL) // EP0
23+
#define EP_DOUBLE_64 (0x36UL) // Other endpoints
24+
25+
26+
// Control Endpoint
27+
#define EP_TYPE_CONTROL (UOTGHS_DEVEPTCFG_EPSIZE_64_BYTE | UOTGHS_DEVEPTCFG_EPTYPE_CTRL | UOTGHS_DEVEPTCFG_EPBK_1_BANK)
28+
29+
// CDC Endpoints
30+
#ifdef CDC_ENABLED
31+
#define EP_TYPE_BULK_IN (UOTGHS_DEVEPTCFG_EPSIZE_512_BYTE | UOTGHS_DEVEPTCFG_EPDIR_IN | UOTGHS_DEVEPTCFG_EPTYPE_BLK | UOTGHS_DEVEPTCFG_EPBK_2_BANK)
32+
#define EP_TYPE_BULK_OUT (UOTGHS_DEVEPTCFG_EPSIZE_512_BYTE | UOTGHS_DEVEPTCFG_EPTYPE_BLK | UOTGHS_DEVEPTCFG_EPBK_2_BANK)
33+
#define EP_TYPE_INTERRUPT_IN (UOTGHS_DEVEPTCFG_EPSIZE_64_BYTE | UOTGHS_DEVEPTCFG_EPDIR_IN | UOTGHS_DEVEPTCFG_EPTYPE_INTRPT | UOTGHS_DEVEPTCFG_EPBK_2_BANK)
34+
#endif
35+
36+
// HID Endpoints
37+
#ifdef HID_ENABLED
38+
#define EP_TYPE_INTERRUPT_IN_HID (UOTGHS_DEVEPTCFG_EPSIZE_64_BYTE | UOTGHS_DEVEPTCFG_EPDIR_IN | UOTGHS_DEVEPTCFG_EPTYPE_INTRPT | UOTGHS_DEVEPTCFG_EPBK_2_BANK)
39+
#endif
40+
41+
// Various definitions
42+
#define EP_TYPE_INTERRUPT_OUT (UOTGHS_DEVEPTCFG_EPSIZE_64_BYTE | UOTGHS_DEVEPTCFG_EPTYPE_INTRPT | UOTGHS_DEVEPTCFG_EPTYPE_INTRPT | UOTGHS_DEVEPTCFG_EPBK_1_BANK)
43+
#define EP_TYPE_ISOCHRONOUS_IN (UOTGHS_DEVEPTCFG_EPSIZE_1024_BYTE | UOTGHS_DEVEPTCFG_EPDIR_IN | UOTGHS_DEVEPTCFG_EPTYPE_ISO | UOTGHS_DEVEPTCFG_EPBK_3_BANK)
44+
#define EP_TYPE_ISOCHRONOUS_OUT (UOTGHS_DEVEPTCFG_EPSIZE_1024_BYTE | UOTGHS_DEVEPTCFG_EPTYPE_ISO | UOTGHS_DEVEPTCFG_EPBK_3_BANK)
45+
46+
2247
#endif /* UOTGHS_H_INCLUDED */

hardware/arduino/sam/system/libsam/source/uotghs.c

Lines changed: 153 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
99
This library is distributed in the hope that it will be useful,
1010
but WITHOUT ANY WARRANTY; without even the implied warranty of
11-
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
1212
See the GNU Lesser General Public License for more details.
1313
1414
You should have received a copy of the GNU Lesser General Public
@@ -20,5 +20,157 @@
2020

2121
#if SAM3XA_SERIES
2222

23+
void USBD_InitEndpoints(void)
24+
{
25+
}
26+
27+
uint32_t USBD_Init(void)
28+
{
29+
uint32_t ul ;
30+
31+
// Enables the USB Clock
32+
pmc_enable_periph_clk(ID_UOTGHS);
33+
pmc_enable_upll_clock();
34+
pmc_switch_udpck_to_upllck(0); // div=0+1
35+
pmc_enable_udpck();
36+
37+
// Configure interrupts
38+
NVIC_SetPriority((IRQn_Type) ID_UOTGHS, 0UL);
39+
NVIC_EnableIRQ((IRQn_Type) ID_UOTGHS);
40+
41+
// Always authorize asynchrone USB interrupts to exit from sleep mode
42+
// for SAM3 USB wake up device except BACKUP mode
43+
pmc_set_fast_startup_input(PMC_FSMR_USBAL);
44+
45+
// Enable USB macro
46+
UOTGHS->UOTGHS_CTRL |= UOTGHS_CTRL_USBE;
47+
48+
// Automatic mode speed for device
49+
UOTGHS->UOTGHS_DEVCTRL &= ~UOTGHS_DEVCTRL_SPDCONF_Msk; // Normal mode
50+
51+
UOTGHS->UOTGHS_DEVCTRL &= ~( UOTGHS_DEVCTRL_LS | UOTGHS_DEVCTRL_TSTJ | UOTGHS_DEVCTRL_TSTK |
52+
UOTGHS_DEVCTRL_TSTPCKT | UOTGHS_DEVCTRL_OPMODE2 ); // Normal mode
53+
54+
UOTGHS->UOTGHS_DEVCTRL = 0;
55+
UOTGHS->UOTGHS_HSTCTRL = 0;
56+
57+
// Enable OTG pad
58+
UOTGHS->UOTGHS_CTRL |= UOTGHS_CTRL_OTGPADE;
59+
60+
// Enable clock OTG pad
61+
UOTGHS->UOTGHS_CTRL &= ~UOTGHS_CTRL_FRZCLK;
62+
63+
// Usb disable
64+
UOTGHS->UOTGHS_CTRL &= ~UOTGHS_CTRL_USBE;
65+
UOTGHS->UOTGHS_CTRL &= ~UOTGHS_CTRL_OTGPADE;
66+
UOTGHS->UOTGHS_CTRL |= UOTGHS_CTRL_FRZCLK;
67+
68+
// Usb enable
69+
UOTGHS->UOTGHS_CTRL |= UOTGHS_CTRL_USBE;
70+
UOTGHS->UOTGHS_CTRL |= UOTGHS_CTRL_OTGPADE;
71+
UOTGHS->UOTGHS_CTRL &= ~UOTGHS_CTRL_FRZCLK;
72+
73+
// Usb select_device
74+
UOTGHS->UOTGHS_CTRL &= ~UOTGHS_CTRL_UIDE;
75+
UOTGHS->UOTGHS_CTRL |= UOTGHS_CTRL_UIMOD_Device;
76+
77+
// Device is in the Attached state
78+
// deviceState = USBD_STATE_SUSPENDED;
79+
// previousDeviceState = USBD_STATE_POWERED;
80+
81+
// Enable USB macro and clear all other bits
82+
UOTGHS->UOTGHS_DEVCTRL |= UOTGHS_CTRL_USBE;
83+
UOTGHS->UOTGHS_DEVCTRL = UOTGHS_CTRL_USBE;
84+
85+
// Configure the pull-up on D+ and disconnect it
86+
USBD_Detach();
87+
88+
// Clear General IT
89+
UOTGHS->UOTGHS_SCR = (UOTGHS_SCR_IDTIC|UOTGHS_SCR_VBUSTIC|UOTGHS_SCR_SRPIC|UOTGHS_SCR_VBERRIC|UOTGHS_SCR_BCERRIC|UOTGHS_SCR_ROLEEXIC|UOTGHS_SCR_HNPERRIC|UOTGHS_SCR_STOIC|UOTGHS_SCR_VBUSRQC);
90+
91+
// Clear OTG Device IT
92+
UOTGHS->UOTGHS_DEVICR = (UOTGHS_DEVICR_SUSPC|UOTGHS_DEVICR_MSOFC|UOTGHS_DEVICR_SOFC|UOTGHS_DEVICR_EORSTC|UOTGHS_DEVICR_WAKEUPC|UOTGHS_DEVICR_EORSMC|UOTGHS_DEVICR_UPRSMC);
93+
94+
// Clear OTG Host IT
95+
UOTGHS->UOTGHS_HSTICR = (UOTGHS_HSTICR_DCONNIC|UOTGHS_HSTICR_DDISCIC|UOTGHS_HSTICR_RSTIC|UOTGHS_HSTICR_RSMEDIC|UOTGHS_HSTICR_RXRSMIC|UOTGHS_HSTICR_HSOFIC|UOTGHS_HSTICR_HWUPIC);
96+
97+
// Reset all Endpoints Fifos
98+
UOTGHS->UOTGHS_DEVEPT |= (UOTGHS_DEVEPT_EPRST0|UOTGHS_DEVEPT_EPRST1|UOTGHS_DEVEPT_EPRST2|UOTGHS_DEVEPT_EPRST3|UOTGHS_DEVEPT_EPRST4|
99+
UOTGHS_DEVEPT_EPRST5|UOTGHS_DEVEPT_EPRST6|UOTGHS_DEVEPT_EPRST7|UOTGHS_DEVEPT_EPRST8);
100+
UOTGHS->UOTGHS_DEVEPT &= ~(UOTGHS_DEVEPT_EPRST0|UOTGHS_DEVEPT_EPRST1|UOTGHS_DEVEPT_EPRST2|UOTGHS_DEVEPT_EPRST3|UOTGHS_DEVEPT_EPRST4|
101+
UOTGHS_DEVEPT_EPRST5|UOTGHS_DEVEPT_EPRST6|UOTGHS_DEVEPT_EPRST7|UOTGHS_DEVEPT_EPRST8);
102+
103+
// Disable all endpoints
104+
UOTGHS->UOTGHS_DEVEPT &= ~(UOTGHS_DEVEPT_EPEN0|UOTGHS_DEVEPT_EPEN1|UOTGHS_DEVEPT_EPEN2|UOTGHS_DEVEPT_EPEN3|UOTGHS_DEVEPT_EPEN4|
105+
UOTGHS_DEVEPT_EPEN5|UOTGHS_DEVEPT_EPEN6|UOTGHS_DEVEPT_EPEN7|UOTGHS_DEVEPT_EPEN8);
106+
107+
// Device is in the Attached state
108+
// deviceState = USBD_STATE_SUSPENDED;
109+
// previousDeviceState = USBD_STATE_POWERED;
110+
111+
// Automatic mode speed for device
112+
UOTGHS->UOTGHS_DEVCTRL &= ~UOTGHS_DEVCTRL_SPDCONF_Msk;
113+
// Force Full Speed mode for device
114+
//UOTGHS->UOTGHS_DEVCTRL = UOTGHS_DEVCTRL_SPDCONF_FORCED_FS;
115+
// Force High Speed mode for device
116+
//UOTGHS->UOTGHS_DEVCTRL = UOTGHS_DEVCTRL_SPDCONF_HIGH_SPEED;
117+
118+
UOTGHS->UOTGHS_DEVCTRL &= ~(UOTGHS_DEVCTRL_LS|UOTGHS_DEVCTRL_TSTJ| UOTGHS_DEVCTRL_TSTK|UOTGHS_DEVCTRL_TSTPCKT|UOTGHS_DEVCTRL_OPMODE2) ;
119+
120+
// Enable USB macro
121+
UOTGHS->UOTGHS_DEVCTRL |= UOTGHS_CTRL_USBE;
122+
123+
// Enable the UID pin select
124+
UOTGHS->UOTGHS_DEVCTRL |= UOTGHS_CTRL_UIDE;
125+
126+
// Enable OTG pad
127+
UOTGHS->UOTGHS_DEVCTRL |= UOTGHS_CTRL_OTGPADE;
128+
129+
// Enable clock OTG pad
130+
UOTGHS->UOTGHS_DEVCTRL &= ~UOTGHS_CTRL_FRZCLK;
131+
132+
// With OR without DMA !!!
133+
// Initialization of DMA
134+
for( ul=1; ul<= UOTGHSDEVDMA_NUMBER ; ul++ )
135+
{
136+
// RESET endpoint canal DMA:
137+
// DMA stop channel command
138+
UOTGHS->UOTGHS_DEVDMA[ul].UOTGHS_DEVDMACONTROL = 0; // STOP command
139+
140+
// Disable endpoint
141+
UOTGHS->UOTGHS_DEVEPTIDR[ul] = (UOTGHS_DEVEPTIDR_TXINEC|UOTGHS_DEVEPTIDR_RXOUTEC|UOTGHS_DEVEPTIDR_RXSTPEC|UOTGHS_DEVEPTIDR_UNDERFEC|UOTGHS_DEVEPTIDR_NAKOUTEC|
142+
UOTGHS_DEVEPTIDR_HBISOINERREC|UOTGHS_DEVEPTIDR_NAKINEC|UOTGHS_DEVEPTIDR_HBISOFLUSHEC|UOTGHS_DEVEPTIDR_OVERFEC|UOTGHS_DEVEPTIDR_STALLEDEC|
143+
UOTGHS_DEVEPTIDR_CRCERREC|UOTGHS_DEVEPTIDR_SHORTPACKETEC|UOTGHS_DEVEPTIDR_MDATEC|UOTGHS_DEVEPTIDR_DATAXEC|UOTGHS_DEVEPTIDR_ERRORTRANSEC|
144+
UOTGHS_DEVEPTIDR_NBUSYBKEC|UOTGHS_DEVEPTIDR_FIFOCONC|UOTGHS_DEVEPTIDR_EPDISHDMAC|UOTGHS_DEVEPTIDR_NYETDISC|UOTGHS_DEVEPTIDR_STALLRQC);
145+
146+
// Reset endpoint config
147+
UOTGHS->UOTGHS_DEVEPTCFG[ul] = 0UL;
148+
149+
// Reset DMA channel (Buff count and Control field)
150+
UOTGHS->UOTGHS_DEVDMA[ul].UOTGHS_DEVDMACONTROL = 0x02UL; // NON STOP command
151+
152+
// Reset DMA channel 0 (STOP)
153+
UOTGHS->UOTGHS_DEVDMA[ul].UOTGHS_DEVDMACONTROL = 0UL; // STOP command
154+
155+
// Clear DMA channel status (read the register to clear it)
156+
UOTGHS->UOTGHS_DEVDMA[ul].UOTGHS_DEVDMASTATUS = UOTGHS->UOTGHS_DEVDMA[ul].UOTGHS_DEVDMASTATUS;
157+
}
158+
159+
UOTGHS->UOTGHS_DEVCTRL |= UOTGHS_CTRL_VBUSTE;
160+
UOTGHS->UOTGHS_DEVIER = UOTGHS_DEVIER_WAKEUPES;
161+
162+
return 0UL ;
163+
}
164+
165+
void USBD_Attach(void)
166+
{
167+
UOTGHS->UOTGHS_DEVCTRL &= ~(unsigned int)UOTGHS_DEVCTRL_DETACH;
168+
}
169+
170+
void USBD_Detach(void)
171+
{
172+
UOTGHS->UOTGHS_DEVCTRL |= UOTGHS_DEVCTRL_DETACH;
173+
}
174+
23175

24176
#endif /* SAM3XA_SERIES */

0 commit comments

Comments
 (0)