M471M/R1/S BSP V3.01.000
The Board Support Package for M4521
cdc_core.c
Go to the documentation of this file.
1/**************************************************************************/
9#include <stdio.h>
10#include <string.h>
11#include <stdlib.h>
12
13#include "NuMicro.h"
14
15#include "usb.h"
16#include "usbh_lib.h"
17#include "usbh_cdc.h"
18
19
37#define USB_XFER_TIMEOUT 100
38
39
50{
51 uint32_t xfer_len;
52 int ret;
53
54 if(cdev == NULL)
56
57 if(cdev->iface_cdc == NULL)
59
60 ret = usbh_ctrl_xfer(cdev->udev,
61 REQ_TYPE_IN | REQ_TYPE_CLASS_DEV | REQ_TYPE_TO_IFACE, /* bmRequestType */
62 CDC_GET_LINE_CODING, /* bRequest */
63 0, /* wValue */
64 cdev->iface_cdc->if_num, /* wIndex */
65 7, /* wLength */
66 (uint8_t *)line_code, /* data buffer */
67 &xfer_len, CDC_CMD_TIMEOUT);
68
69 if((ret < 0) || (xfer_len != 7))
70 {
71 CDC_DBGMSG("GET_LINE_CODIN command failed. %d, %d\n", ret, xfer_len);
72 return ret;
73 }
74 return ret;
75}
76
77
88{
89 uint32_t xfer_len;
90 int ret;
91
92 if(cdev == NULL)
93 return USBH_ERR_NOT_FOUND;
94
95 if(cdev->iface_cdc == NULL)
96 return USBH_ERR_NOT_FOUND;
97
98 if((line_code->stop_bits != 0) && (line_code->stop_bits != 1) &&
99 (line_code->stop_bits != 2))
101
102 if(line_code->parity > 4)
104
105 if((line_code->data_bits != 5) && (line_code->data_bits != 6) &&
106 (line_code->data_bits != 7) && (line_code->data_bits != 8) &&
107 (line_code->data_bits != 16))
109
110 ret = usbh_ctrl_xfer(cdev->udev,
111 REQ_TYPE_OUT | REQ_TYPE_CLASS_DEV | REQ_TYPE_TO_IFACE, /* bmRequestType */
112 CDC_SET_LINE_CODING, /* bRequest */
113 0, /* wValue */
114 cdev->iface_cdc->if_num, /* wIndex */
115 7, /* wLength */
116 (uint8_t *)line_code, /* data buffer */
117 &xfer_len, CDC_CMD_TIMEOUT);
118
119 if(ret < 0)
120 {
121 CDC_DBGMSG("SET_LINE_CODIN command failed. %d\n", ret);
122 return ret;
123 }
124 return 0;
125}
126
137int32_t usbh_cdc_set_control_line_state(CDC_DEV_T *cdev, int active_carrier, int DTE_present)
138{
139 uint32_t xfer_len;
140 int ret;
141 uint16_t ctrl_bitmap = 0;
142
143 if(cdev == NULL)
145
146 if(cdev->iface_cdc == NULL)
148
149 if(active_carrier)
150 ctrl_bitmap |= 0x02;
151
152 if(DTE_present)
153 ctrl_bitmap |= 0x01;
154
155 ret = usbh_ctrl_xfer(cdev->udev,
156 REQ_TYPE_OUT | REQ_TYPE_CLASS_DEV | REQ_TYPE_TO_IFACE, /* bmRequestType */
157 CDC_SET_CONTROL_LINE_STATE, /* bRequest */
158 ctrl_bitmap, /* wValue */
159 cdev->iface_cdc->if_num, /* wIndex */
160 0, /* wLength */
161 NULL, /* data buffer */
162 &xfer_len, CDC_CMD_TIMEOUT);
163
164 if(ret)
165 {
166 CDC_DBGMSG("SET_CONTROL_LINE_STATE command failed. %d\n", ret);
167 return ret;
168 }
169 return ret;
170}
171
173/*
174 * CDC INT-in complete function
175 */
176static void cdc_int_in_irq(UTR_T *utr)
177{
178 CDC_DEV_T *cdev;
179 int ret;
180
181 //CDC_DBGMSG("cdc_int_in_irq. %d\n", utr->xfer_len);
182
183 cdev = (CDC_DEV_T *)utr->context;
184
185 if(utr->status)
186 {
187 CDC_DBGMSG("cdc_int_in_irq - has error: 0x%x\n", utr->status);
188 return;
189 }
190
191 if(cdev->sts_func && utr->xfer_len)
192 cdev->sts_func(cdev, utr->buff, utr->xfer_len);
193
194 utr->xfer_len = 0;
195 ret = usbh_int_xfer(utr);
196 if(ret)
197 {
198 CDC_DBGMSG("cdc_int_in_irq - failed to submit interrupt-in request (%d)", ret);
199 free_utr(utr);
200 cdev->utr_sts = NULL;
201 }
202}
203
205
215{
216 EP_INFO_T *ep;
217 UTR_T *utr;
218 int ret;
219
220 if((cdev == NULL) || (cdev->iface_cdc == NULL))
221 return USBH_ERR_NOT_FOUND;
222
223 if(!func || cdev->utr_sts)
225
226 ep = cdev->ep_sts;
227 if(ep == NULL)
228 {
229 ep = usbh_iface_find_ep(cdev->iface_cdc, 0, EP_ADDR_DIR_IN | EP_ATTR_TT_INT);
230 if(ep == NULL)
231 {
232 CDC_DBGMSG("Interrupt-in endpoint not found in this CDC device!\n");
234 }
235 cdev->ep_sts = ep;
236 }
237
238 utr = alloc_utr(cdev->udev);
239 if(utr == NULL)
240 {
241 CDC_DBGMSG("Failed to allocated UTR!\n");
242 return USBH_ERR_MEMORY_OUT;
243 }
244
245 utr->buff = (uint8_t *)cdev->sts_buff;
246 utr->context = cdev;
247 utr->ep = ep;
248 utr->data_len = ep->wMaxPacketSize;
249 if(utr->data_len > CDC_STATUS_BUFF_SIZE)
250 {
251 CDC_DBGMSG("Warning! CDC_STATUS_BUFF_SIZE %d is smaller than max. packet size %d!\n", CDC_STATUS_BUFF_SIZE, ep->wMaxPacketSize);
252 utr->data_len = CDC_STATUS_BUFF_SIZE;
253 }
254 utr->xfer_len = 0;
255 utr->func = cdc_int_in_irq;
256
257 cdev->utr_sts = utr;
258 cdev->sts_func = func;
259
260 ret = usbh_int_xfer(utr);
261 if(ret < 0)
262 {
263 CDC_DBGMSG("Error - failed to submit interrupt read request (%d)", ret);
264 free_utr(utr);
265 cdev->utr_sts = NULL;
266 return ret;
267 }
268
269 return 0;
270}
271
272/*
273 * CDC BULK-in complete function
274 */
275static void cdc_bulk_in_irq(UTR_T *utr)
276{
277 CDC_DEV_T *cdev;
278
279 //CDC_DBGMSG("cdc_bulk_in_irq. %d\n", utr->xfer_len);
280
281 cdev = (CDC_DEV_T *)utr->context;
282
283 if(utr->status)
284 {
285 CDC_DBGMSG("cdc_bulk_in_irq - has error: 0x%x\n", utr->status);
286 return;
287 }
288
289 if(cdev->rx_func)
290 cdev->rx_func(cdev, utr->buff, utr->xfer_len);
291
292 free_utr(utr);
293 cdev->utr_rx = NULL;
294 cdev->rx_busy = 0;
295}
296
298
308{
309 EP_INFO_T *ep;
310 UTR_T *utr;
311 int ret;
312
313 if((cdev == NULL) || (cdev->iface_data == NULL))
314 return USBH_ERR_NOT_FOUND;
315
316 if(!func)
318
319 ep = cdev->ep_rx;
320 if(ep == NULL)
321 {
322 ep = usbh_iface_find_ep(cdev->iface_data, 0, EP_ADDR_DIR_IN | EP_ATTR_TT_BULK);
323 if(ep == NULL)
324 {
325 CDC_DBGMSG("Bulk-in endpoint not found in this CDC device!\n");
327 }
328 cdev->ep_rx = ep;
329 }
330
331 utr = alloc_utr(cdev->udev);
332 if(utr == NULL)
333 {
334 CDC_DBGMSG("Failed to allocated UTR!\n");
335 return USBH_ERR_MEMORY_OUT;
336 }
337
338 utr->buff = (uint8_t *)cdev->rx_buff;
339 utr->context = cdev;
340 utr->ep = ep;
341 utr->data_len = ep->wMaxPacketSize;
342 if(utr->data_len > CDC_RX_BUFF_SIZE)
343 {
344 CDC_DBGMSG("Warning! CDC_RX_BUFF_SIZE %d is smaller than max. packet size %d!\n", CDC_RX_BUFF_SIZE, ep->wMaxPacketSize);
345 utr->data_len = CDC_RX_BUFF_SIZE;
346 }
347 utr->xfer_len = 0;
348 utr->func = cdc_bulk_in_irq;
349
350 cdev->rx_func = func;
351 cdev->utr_rx = utr;
352 cdev->rx_busy = 1;
353
354 ret = usbh_bulk_xfer(utr);
355 if(ret < 0)
356 {
357 CDC_DBGMSG("Error - failed to submit bulk in request (%d)", ret);
358 free_utr(utr);
359 cdev->utr_rx = NULL;
360 cdev->rx_busy = 0;
361 return ret;
362 }
363 return 0;
364}
365
366/*
367 * CDC BULK-in complete function
368 */
369static volatile int bulk_out_done;
370static void cdc_bulk_out_irq(UTR_T *utr)
371{
372 bulk_out_done = 1;
373}
374
384int32_t usbh_cdc_send_data(CDC_DEV_T *cdev, uint8_t *buff, int buff_len)
385{
386 EP_INFO_T *ep;
387 UTR_T *utr;
388 uint32_t t0;
389 int ret;
390
391 if((cdev == NULL) || (cdev->iface_data == NULL))
392 return USBH_ERR_NOT_FOUND;
393
394 ep = cdev->ep_tx;
395 if(ep == NULL)
396 {
397 ep = usbh_iface_find_ep(cdev->iface_data, 0, EP_ADDR_DIR_OUT | EP_ATTR_TT_BULK);
398 if(ep == NULL)
399 {
400 CDC_DBGMSG("Bulk-out endpoint not found in this CDC device!\n");
402 }
403 cdev->ep_tx = ep;
404 }
405
406 utr = alloc_utr(cdev->udev);
407 if(utr == NULL)
408 {
409 CDC_DBGMSG("Failed to allocated UTR!\n");
410 return USBH_ERR_MEMORY_OUT;
411 }
412
413 utr->context = cdev;
414 utr->ep = ep;
415 utr->buff = buff;
416 utr->data_len = buff_len;
417 utr->xfer_len = 0;
418 utr->func = cdc_bulk_out_irq;
419 bulk_out_done = 0;
420
421 ret = usbh_bulk_xfer(utr);
422 if(ret < 0)
423 {
424 CDC_DBGMSG("Error - failed to submit bulk in request (%d)", ret);
425 free_utr(utr);
426 return ret;
427 }
428
429 t0 = get_ticks();
430 while(bulk_out_done == 0)
431 {
432 if(get_ticks() - t0 > USB_XFER_TIMEOUT)
433 {
434 usbh_quit_utr(utr);
435 free_utr(utr);
436 return USBH_ERR_TIMEOUT;
437 }
438 }
439
440 free_utr(utr);
441 return 0;
442}
443 /* end of group USBH_CDC_EXPORTED_FUNCTIONS */
445 /* end of group USBH_CDC_Driver */
447 /* end of group USB_Host_Driver */
449 /* end of group Component_Library */
451
452
453
454
NuMicro peripheral access layer header file.
#define NULL
Definition: M471M_R1_S.h:13908
int32_t usbh_cdc_set_line_coding(CDC_DEV_T *cdev, LINE_CODING_T *line_code)
SET_LINE_CODING request.
Definition: cdc_core.c:87
int32_t usbh_cdc_start_to_receive_data(CDC_DEV_T *cdev, CDC_CB_FUNC *func)
HIDDEN_SYMBOLS.
Definition: cdc_core.c:307
int32_t usbh_cdc_set_control_line_state(CDC_DEV_T *cdev, int active_carrier, int DTE_present)
SET_CONTROL_LINE_STATE request.
Definition: cdc_core.c:137
int32_t usbh_cdc_send_data(CDC_DEV_T *cdev, uint8_t *buff, int buff_len)
Send a block of data via CDC device's bulk-out transfer pipe.
Definition: cdc_core.c:384
HIDDEN_SYMBOLS int32_t usbh_cdc_start_polling_status(CDC_DEV_T *cdev, CDC_CB_FUNC *func)
Start purge the CDC device's interrupt-in transfer pipe.
Definition: cdc_core.c:214
static void cdc_bulk_in_irq(UTR_T *utr)
Definition: cdc_core.c:275
static volatile int bulk_out_done
Definition: cdc_core.c:369
int32_t usbh_cdc_get_line_coding(CDC_DEV_T *cdev, LINE_CODING_T *line_code)
GET_LINE_CODING request.
Definition: cdc_core.c:49
#define USB_XFER_TIMEOUT
Definition: cdc_core.c:37
static void cdc_bulk_out_irq(UTR_T *utr)
Definition: cdc_core.c:370
#define USBH_ERR_TIMEOUT
Definition: usbh_lib.h:45
#define USBH_ERR_EP_NOT_FOUND
Definition: usbh_lib.h:39
#define USBH_ERR_INVALID_PARAM
Definition: usbh_lib.h:37
#define USBH_ERR_NOT_FOUND
Definition: usbh_lib.h:38
#define USBH_ERR_MEMORY_OUT
Definition: usbh_lib.h:31
uint32_t get_ticks(void)
A function return current tick count.
LINE_CODING_T
Definition: usbh_cdc.h:168
uint8_t rx_busy
Definition: usbh_cdc.h:189
IFACE_T * iface_data
Definition: usbh_cdc.h:178
uint32_t sts_buff[CDC_STATUS_BUFF_SIZE/4]
Definition: usbh_cdc.h:185
CDC_CB_FUNC * sts_func
Definition: usbh_cdc.h:187
UDEV_T * udev
Definition: usbh_cdc.h:176
IFACE_T * iface_cdc
Definition: usbh_cdc.h:177
UTR_T * utr_sts
Definition: usbh_cdc.h:183
EP_INFO_T * ep_rx
Definition: usbh_cdc.h:181
EP_INFO_T * ep_tx
Definition: usbh_cdc.h:182
uint32_t rx_buff[CDC_RX_BUFF_SIZE/4]
Definition: usbh_cdc.h:186
UTR_T * utr_rx
Definition: usbh_cdc.h:184
CDC_CB_FUNC * rx_func
Definition: usbh_cdc.h:188
EP_INFO_T * ep_sts
Definition: usbh_cdc.h:180
void() CDC_CB_FUNC(struct cdc_dev_t *cdev, uint8_t *rdata, int data_len)
Definition: usbh_lib.h:119
USB Host library header file.
USB Host CDC(Communication Device Class) driver header file.
USB Host library exported header file.