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,13 @@ 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
+ MBED_ASSERT (HAL_HCD_HC_Init ((HCD_HandleTypeDef*)hced->hhcd ,hced->ch_num , address, addr, hcd_speed, type, size)!=HAL_BUSY);
62
78
this ->device_address = addr;
63
- HAL_HCD_HC_Init ((HCD_HandleTypeDef*)hced-> hhcd ,hced-> ch_num , address, addr, HCD_SPEED_FULL, type, size);
79
+
64
80
}
65
81
66
82
void USBEndpoint::setSpeed (uint8_t speed)
@@ -70,58 +86,82 @@ void USBEndpoint::setSpeed(uint8_t speed)
70
86
71
87
72
88
73
- void USBEndpoint::setState (uint8_t st) {
74
- if (st > 18 )
89
+ void USBEndpoint::setState (USB_TYPE st)
90
+ {
91
+ /* modify this state is possible only with a plug */
92
+ if (state == USB_TYPE_FREE) {
75
93
return ;
76
- if (state == USB_TYPE_FREE) HAL_HCD_HC_Halt ((HCD_HandleTypeDef*)hced->hhcd , hced->ch_num );
94
+ }
95
+
96
+ state = st;
97
+ if (st == USB_TYPE_FREE) {
98
+ HCD_HandleTypeDef *hhcd = (HCD_HandleTypeDef*)hced->hhcd ;
99
+ uint32_t *addr = &((uint32_t *)hhcd->pData )[hced->ch_num ];
100
+ if ((*addr) && (type != INTERRUPT_ENDPOINT)) {
101
+ this ->ep_queue .put ((uint8_t *)1 );
102
+ }
103
+ MBED_ASSERT (HAL_HCD_HC_Halt ((HCD_HandleTypeDef*)hced->hhcd , hced->ch_num )!=HAL_BUSY);
104
+ HAL_HCD_DisableInt ((HCD_HandleTypeDef*)hced->hhcd , hced->ch_num );
105
+ *addr = 0 ;
106
+
107
+ }
108
+ if (st == USB_TYPE_ERROR) {
109
+ MBED_ASSERT (HAL_HCD_HC_Halt ((HCD_HandleTypeDef*)hced->hhcd , hced->ch_num )!=HAL_BUSY);
110
+ HAL_HCD_DisableInt ((HCD_HandleTypeDef*)hced->hhcd , hced->ch_num );
77
111
78
- state = (USB_TYPE)st;
112
+ }
113
+ if (st == USB_TYPE_ERROR) {
114
+ uint8_t hcd_speed = HCD_SPEED_FULL;
115
+ /* small speed device with hub not supported
116
+ if (this->speed) hcd_speed = HCD_SPEED_LOW;*/
117
+ MBED_ASSERT (HAL_HCD_HC_Init ((HCD_HandleTypeDef*)hced->hhcd ,hced->ch_num , address, 0 , hcd_speed, type, size)!=HAL_BUSY);
118
+ }
79
119
}
80
120
81
121
82
122
extern uint32_t HAL_HCD_HC_GetMaxPacket (HCD_HandleTypeDef *hhcd, uint8_t chn_num);
83
123
extern uint32_t HAL_HCD_HC_GetType (HCD_HandleTypeDef *hhcd, uint8_t chn_num);
84
124
85
-
125
+
86
126
USB_TYPE USBEndpoint::queueTransfer ()
87
127
{
88
128
HCD_HandleTypeDef *hhcd = (HCD_HandleTypeDef*)hced->hhcd ;
89
129
uint32_t *addr = &((uint32_t *)hhcd->pData )[hced->ch_num ];
90
130
uint32_t type = HAL_HCD_HC_GetType (hhcd, hced->ch_num );
91
131
uint32_t max_size = HAL_HCD_HC_GetMaxPacket (hhcd, hced->ch_num );
92
132
/* 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;
133
+ if (state == USB_TYPE_FREE) {
134
+ td_current->state = USB_TYPE_FREE;
135
+ return USB_TYPE_FREE;
97
136
}
137
+ ep_queue.get (0 );
98
138
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
- }
139
+ transfer_len = td_current->size <= max_size ? td_current->size : max_size;
107
140
buf_start = (uint8_t *)td_current->currBufPtr ;
108
141
109
142
// Now add this free TD at this end of the queue
110
143
state = USB_TYPE_PROCESSING;
111
144
/* one request */
112
145
td_current->nextTD = (hcTd*)0 ;
113
-
146
+ #if defined(MAX_NYET_RETRY)
147
+ td_current->retry = 0 ;
148
+ #endif
149
+ td_current->setup = setup;
114
150
*addr = (uint32_t )td_current;
115
151
/* dir /setup is inverted for ST */
116
152
/* token is useful only ctrl endpoint */
117
153
/* last parameter is ping ? */
118
154
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);
155
+ HAL_HCD_EnableInt ((HCD_HandleTypeDef*)hced->hhcd , hced->ch_num );
156
+
119
157
return USB_TYPE_PROCESSING;
120
158
}
121
159
122
160
void USBEndpoint::unqueueTransfer (volatile HCTD * td)
123
161
{
124
-
162
+ if (state==USB_TYPE_FREE) {
163
+ return ;
164
+ }
125
165
uint32_t *addr = &((uint32_t *)((HCD_HandleTypeDef*)hced->hhcd )->pData )[hced->ch_num ];
126
166
td->state =0 ;
127
167
td->currBufPtr =0 ;
0 commit comments