19
19
#include " USBEndpoint.h"
20
20
extern uint32_t HAL_HCD_HC_GetMaxPacket (HCD_HandleTypeDef *hhcd, uint8_t chn_num);
21
21
extern uint32_t HAL_HCD_HC_GetType (HCD_HandleTypeDef *hhcd, uint8_t chn_num);
22
+ extern void HAL_HCD_DisableInt (HCD_HandleTypeDef* hhcd, uint8_t chn_num);
23
+ extern void HAL_HCD_EnableInt (HCD_HandleTypeDef* hhcd, uint8_t chn_num);
24
+
25
+
22
26
23
27
24
28
void USBEndpoint::init (HCED * hced_, ENDPOINT_TYPE type_, ENDPOINT_DIRECTION dir_, uint32_t size, uint8_t ep_number, HCTD* td_list_[2 ])
25
29
{
30
+ HCD_HandleTypeDef *hhcd;
31
+ uint32_t *addr;
32
+
26
33
hced = hced_;
27
34
type = type_;
28
35
dir = dir_;
@@ -46,10 +53,14 @@ void USBEndpoint::init(HCED * hced_, ENDPOINT_TYPE type_, ENDPOINT_DIRECTION dir
46
53
47
54
td_current = td_list[0 ];
48
55
td_next = td_list[1 ];
49
-
56
+ /* remove potential post pending from previous endpoint */
57
+ ep_queue.get (0 );
50
58
intf_nb = 0 ;
51
-
59
+ hhcd = (HCD_HandleTypeDef*)hced->hhcd ;
60
+ addr = &((uint32_t *)hhcd->pData )[hced->ch_num ];
61
+ *addr = 0 ;
52
62
state = USB_TYPE_IDLE;
63
+ speed =false ;
53
64
}
54
65
void USBEndpoint::setSize (uint32_t size)
55
66
{
@@ -59,8 +70,16 @@ void USBEndpoint::setSize(uint32_t size)
59
70
60
71
void USBEndpoint::setDeviceAddress (uint8_t addr)
61
72
{
73
+ HCD_HandleTypeDef *hhcd;
74
+ uint8_t hcd_speed = HCD_SPEED_FULL;
75
+ /* fix me : small speed device with hub not supported
76
+ if (this->speed) hcd_speed = HCD_SPEED_LOW; */
77
+ if (this ->speed ) {
78
+ USB_WARN (" small speed device on hub not supported" );
79
+ }
80
+ MBED_ASSERT (HAL_HCD_HC_Init ((HCD_HandleTypeDef*)hced->hhcd ,hced->ch_num , address, addr, hcd_speed, type, size)!=HAL_BUSY);
62
81
this ->device_address = addr;
63
- HAL_HCD_HC_Init ((HCD_HandleTypeDef*)hced-> hhcd ,hced-> ch_num , address, addr, HCD_SPEED_FULL, type, size);
82
+
64
83
}
65
84
66
85
void USBEndpoint::setSpeed (uint8_t speed)
@@ -70,58 +89,82 @@ void USBEndpoint::setSpeed(uint8_t speed)
70
89
71
90
72
91
73
- void USBEndpoint::setState (uint8_t st) {
74
- if (st > 18 )
92
+ void USBEndpoint::setState (USB_TYPE st)
93
+ {
94
+ /* modify this state is possible only with a plug */
95
+ if (state == USB_TYPE_FREE) {
75
96
return ;
76
- if (state == USB_TYPE_FREE) HAL_HCD_HC_Halt ((HCD_HandleTypeDef*)hced->hhcd , hced->ch_num );
97
+ }
98
+
99
+ state = st;
100
+ if (st == USB_TYPE_FREE) {
101
+ HCD_HandleTypeDef *hhcd = (HCD_HandleTypeDef*)hced->hhcd ;
102
+ uint32_t *addr = &((uint32_t *)hhcd->pData )[hced->ch_num ];
103
+ if ((*addr) && (type != INTERRUPT_ENDPOINT)) {
104
+ this ->ep_queue .put ((uint8_t *)1 );
105
+ }
106
+ MBED_ASSERT (HAL_HCD_HC_Halt ((HCD_HandleTypeDef*)hced->hhcd , hced->ch_num )!=HAL_BUSY);
107
+ HAL_HCD_DisableInt ((HCD_HandleTypeDef*)hced->hhcd , hced->ch_num );
108
+ *addr = 0 ;
77
109
78
- state = (USB_TYPE)st;
110
+ }
111
+ if (st == USB_TYPE_ERROR) {
112
+ MBED_ASSERT (HAL_HCD_HC_Halt ((HCD_HandleTypeDef*)hced->hhcd , hced->ch_num )!=HAL_BUSY);
113
+ HAL_HCD_DisableInt ((HCD_HandleTypeDef*)hced->hhcd , hced->ch_num );
114
+
115
+ }
116
+ if (st == USB_TYPE_ERROR) {
117
+ uint8_t hcd_speed = HCD_SPEED_FULL;
118
+ /* small speed device with hub not supported
119
+ if (this->speed) hcd_speed = HCD_SPEED_LOW;*/
120
+ MBED_ASSERT (HAL_HCD_HC_Init ((HCD_HandleTypeDef*)hced->hhcd ,hced->ch_num , address, 0 , hcd_speed, type, size)!=HAL_BUSY);
121
+ }
79
122
}
80
123
81
124
82
125
extern uint32_t HAL_HCD_HC_GetMaxPacket (HCD_HandleTypeDef *hhcd, uint8_t chn_num);
83
126
extern uint32_t HAL_HCD_HC_GetType (HCD_HandleTypeDef *hhcd, uint8_t chn_num);
84
127
85
-
128
+
86
129
USB_TYPE USBEndpoint::queueTransfer ()
87
130
{
88
131
HCD_HandleTypeDef *hhcd = (HCD_HandleTypeDef*)hced->hhcd ;
89
132
uint32_t *addr = &((uint32_t *)hhcd->pData )[hced->ch_num ];
90
133
uint32_t type = HAL_HCD_HC_GetType (hhcd, hced->ch_num );
91
134
uint32_t max_size = HAL_HCD_HC_GetMaxPacket (hhcd, hced->ch_num );
92
135
/* if a packet is queue on disconnected ; no solution for now */
93
- if (*addr == (uint32_t ) -1 ) {
94
- /* set td as disconnected */
95
- td_current->state = USB_TYPE_DISCONNECTED;
96
- return USB_TYPE_DISCONNECTED;
136
+ if (state == USB_TYPE_FREE) {
137
+ td_current->state = USB_TYPE_FREE;
138
+ return USB_TYPE_FREE;
97
139
}
140
+ ep_queue.get (0 );
98
141
MBED_ASSERT (*addr ==0 );
99
- if ((type == EP_TYPE_BULK) || (type== EP_TYPE_CTRL)) {
100
- transfer_len = td_current->size <= max_size ? td_current->size : max_size;
101
- transferred = td_current->size ;
102
- } else {
103
- transfer_len = td_current->size ;
104
- transferred = td_current->size ;
105
- MBED_ASSERT (transferred <= (int )max_size);
106
- }
142
+ transfer_len = td_current->size <= max_size ? td_current->size : max_size;
107
143
buf_start = (uint8_t *)td_current->currBufPtr ;
108
144
109
145
// Now add this free TD at this end of the queue
110
146
state = USB_TYPE_PROCESSING;
111
147
/* one request */
112
148
td_current->nextTD = (hcTd*)0 ;
113
-
149
+ #if defined(MAX_NYET_RETRY)
150
+ td_current->retry = 0 ;
151
+ #endif
152
+ td_current->setup = setup;
114
153
*addr = (uint32_t )td_current;
115
154
/* dir /setup is inverted for ST */
116
155
/* token is useful only ctrl endpoint */
117
156
/* last parameter is ping ? */
118
157
MBED_ASSERT (HAL_HCD_HC_SubmitRequest ((HCD_HandleTypeDef*)hced->hhcd , hced->ch_num , dir-1 , type,!setup,(uint8_t *) td_current->currBufPtr , transfer_len, 0 )==HAL_OK);
158
+ HAL_HCD_EnableInt ((HCD_HandleTypeDef*)hced->hhcd , hced->ch_num );
159
+
119
160
return USB_TYPE_PROCESSING;
120
161
}
121
162
122
163
void USBEndpoint::unqueueTransfer (volatile HCTD * td)
123
164
{
124
-
165
+ if (state==USB_TYPE_FREE) {
166
+ return ;
167
+ }
125
168
uint32_t *addr = &((uint32_t *)((HCD_HandleTypeDef*)hced->hhcd )->pData )[hced->ch_num ];
126
169
td->state =0 ;
127
170
td->currBufPtr =0 ;
0 commit comments