blob: de8617211637425764b615f058550899e06dc569 [file] [log] [blame]
Kévin Redon69b92d92019-01-24 16:39:20 +01001/*
2 * Code generated from Atmel Start.
3 *
4 * This file will be overwritten when reconfiguring your Atmel Start project.
5 * Please copy examples or other code you want to keep to a separate file or main.c
6 * to avoid loosing it when reconfiguring.
7 */
8#include "atmel_start.h"
9#include "usb_start.h"
10
11#if CONF_USBD_HS_SP
12static uint8_t single_desc_bytes[] = {
13 /* Device descriptors and Configuration descriptors list. */
14 CDCD_ACM_HS_DESCES_LS_FS};
15static uint8_t single_desc_bytes_hs[] = {
16 /* Device descriptors and Configuration descriptors list. */
17 CDCD_ACM_HS_DESCES_HS};
18#define CDCD_ECHO_BUF_SIZ CONF_USB_CDCD_ACM_DATA_BULKIN_MAXPKSZ_HS
19#else
20static uint8_t single_desc_bytes[] = {
21 /* Device descriptors and Configuration descriptors list. */
22 CDCD_ACM_DESCES_LS_FS};
23#define CDCD_ECHO_BUF_SIZ CONF_USB_CDCD_ACM_DATA_BULKIN_MAXPKSZ
24#endif
25
26static struct usbd_descriptors single_desc[]
27 = {{single_desc_bytes, single_desc_bytes + sizeof(single_desc_bytes)}
28#if CONF_USBD_HS_SP
29 ,
30 {single_desc_bytes_hs, single_desc_bytes_hs + sizeof(single_desc_bytes_hs)}
31#endif
32};
33
34/** Buffers to receive and echo the communication bytes. */
35static uint32_t usbd_cdc_buffer[CDCD_ECHO_BUF_SIZ / 4];
36
37/** Ctrl endpoint buffer */
38static uint8_t ctrl_buffer[64];
39
40/**
41 * \brief Callback invoked when bulk OUT data received
42 */
43static bool usb_device_cb_bulk_out(const uint8_t ep, const enum usb_xfer_code rc, const uint32_t count)
44{
45 cdcdf_acm_write((uint8_t *)usbd_cdc_buffer, count);
46
47 /* No error. */
48 return false;
49}
50
51/**
52 * \brief Callback invoked when bulk IN data received
53 */
54static bool usb_device_cb_bulk_in(const uint8_t ep, const enum usb_xfer_code rc, const uint32_t count)
55{
56 /* Echo data. */
57 cdcdf_acm_read((uint8_t *)usbd_cdc_buffer, sizeof(usbd_cdc_buffer));
58
59 /* No error. */
60 return false;
61}
62
63/**
64 * \brief Callback invoked when Line State Change
65 */
66static bool usb_device_cb_state_c(usb_cdc_control_signal_t state)
67{
68 if (state.rs232.DTR) {
69 /* Callbacks must be registered after endpoint allocation */
70 cdcdf_acm_register_callback(CDCDF_ACM_CB_READ, (FUNC_PTR)usb_device_cb_bulk_out);
71 cdcdf_acm_register_callback(CDCDF_ACM_CB_WRITE, (FUNC_PTR)usb_device_cb_bulk_in);
72 /* Start Rx */
73 cdcdf_acm_read((uint8_t *)usbd_cdc_buffer, sizeof(usbd_cdc_buffer));
74 }
75
76 /* No error. */
77 return false;
78}
79
80/**
81 * \brief CDC ACM Init
82 */
83void cdc_device_acm_init(void)
84{
85 /* usb stack init */
86 usbdc_init(ctrl_buffer);
87
88 /* usbdc_register_funcion inside */
89 cdcdf_acm_init();
90
91 usbdc_start(single_desc);
92 usbdc_attach();
93}
94
95/**
96 * Example of using CDC ACM Function.
97 * \note
98 * In this example, we will use a PC as a USB host:
99 * - Connect the DEBUG USB on XPLAINED board to PC for program download.
100 * - Connect the TARGET USB on XPLAINED board to PC for running program.
101 * The application will behave as a virtual COM.
102 * - Open a HyperTerminal or other COM tools in PC side.
103 * - Send out a character or string and it will echo the content received.
104 */
105void cdcd_acm_example(void)
106{
107 while (!cdcdf_acm_is_enabled()) {
108 // wait cdc acm to be installed
109 };
110
111 cdcdf_acm_register_callback(CDCDF_ACM_CB_STATE_C, (FUNC_PTR)usb_device_cb_state_c);
112
113 while (1) {
114 }
115}
116
117void usb_init(void)
118{
119
120 cdc_device_acm_init();
121}