Kévin Redon | 69b92d9 | 2019-01-24 16:39:20 +0100 | [diff] [blame] | 1 | /* |
| 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 |
| 12 | static uint8_t single_desc_bytes[] = { |
| 13 | /* Device descriptors and Configuration descriptors list. */ |
| 14 | CDCD_ACM_HS_DESCES_LS_FS}; |
| 15 | static 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 |
| 20 | static 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 | |
| 26 | static 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. */ |
| 35 | static uint32_t usbd_cdc_buffer[CDCD_ECHO_BUF_SIZ / 4]; |
| 36 | |
| 37 | /** Ctrl endpoint buffer */ |
| 38 | static uint8_t ctrl_buffer[64]; |
| 39 | |
| 40 | /** |
| 41 | * \brief Callback invoked when bulk OUT data received |
| 42 | */ |
| 43 | static 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 | */ |
| 54 | static 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 | */ |
| 66 | static 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 | */ |
| 83 | void 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 | /** |
Kévin Redon | 8e53800 | 2019-01-30 11:19:19 +0100 | [diff] [blame] | 96 | * \brief Start USB stack |
Kévin Redon | 69b92d9 | 2019-01-24 16:39:20 +0100 | [diff] [blame] | 97 | */ |
Kévin Redon | 8e53800 | 2019-01-30 11:19:19 +0100 | [diff] [blame] | 98 | void usb_start(void) |
Kévin Redon | 69b92d9 | 2019-01-24 16:39:20 +0100 | [diff] [blame] | 99 | { |
| 100 | while (!cdcdf_acm_is_enabled()) { |
| 101 | // wait cdc acm to be installed |
| 102 | }; |
| 103 | |
| 104 | cdcdf_acm_register_callback(CDCDF_ACM_CB_STATE_C, (FUNC_PTR)usb_device_cb_state_c); |
Kévin Redon | 69b92d9 | 2019-01-24 16:39:20 +0100 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | void usb_init(void) |
| 108 | { |
| 109 | |
| 110 | cdc_device_acm_init(); |
| 111 | } |