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 | |
Kévin Redon | 69b92d9 | 2019-01-24 16:39:20 +0100 | [diff] [blame] | 11 | #define CDCD_ECHO_BUF_SIZ CONF_USB_CDCD_ACM_DATA_BULKIN_MAXPKSZ |
Kévin Redon | 69b92d9 | 2019-01-24 16:39:20 +0100 | [diff] [blame] | 12 | |
| 13 | /** Buffers to receive and echo the communication bytes. */ |
| 14 | static uint32_t usbd_cdc_buffer[CDCD_ECHO_BUF_SIZ / 4]; |
| 15 | |
| 16 | /** Ctrl endpoint buffer */ |
| 17 | static uint8_t ctrl_buffer[64]; |
| 18 | |
| 19 | /** |
| 20 | * \brief Callback invoked when bulk OUT data received |
| 21 | */ |
| 22 | static bool usb_device_cb_bulk_out(const uint8_t ep, const enum usb_xfer_code rc, const uint32_t count) |
| 23 | { |
| 24 | cdcdf_acm_write((uint8_t *)usbd_cdc_buffer, count); |
| 25 | |
| 26 | /* No error. */ |
| 27 | return false; |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * \brief Callback invoked when bulk IN data received |
| 32 | */ |
| 33 | static bool usb_device_cb_bulk_in(const uint8_t ep, const enum usb_xfer_code rc, const uint32_t count) |
| 34 | { |
| 35 | /* Echo data. */ |
| 36 | cdcdf_acm_read((uint8_t *)usbd_cdc_buffer, sizeof(usbd_cdc_buffer)); |
| 37 | |
| 38 | /* No error. */ |
| 39 | return false; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * \brief Callback invoked when Line State Change |
| 44 | */ |
| 45 | static bool usb_device_cb_state_c(usb_cdc_control_signal_t state) |
| 46 | { |
| 47 | if (state.rs232.DTR) { |
| 48 | /* Callbacks must be registered after endpoint allocation */ |
| 49 | cdcdf_acm_register_callback(CDCDF_ACM_CB_READ, (FUNC_PTR)usb_device_cb_bulk_out); |
| 50 | cdcdf_acm_register_callback(CDCDF_ACM_CB_WRITE, (FUNC_PTR)usb_device_cb_bulk_in); |
| 51 | /* Start Rx */ |
| 52 | cdcdf_acm_read((uint8_t *)usbd_cdc_buffer, sizeof(usbd_cdc_buffer)); |
| 53 | } |
| 54 | |
| 55 | /* No error. */ |
| 56 | return false; |
| 57 | } |
| 58 | |
Harald Welte | 34a8706 | 2019-04-19 22:33:36 +0200 | [diff] [blame] | 59 | extern const struct usbd_descriptors usb_descs[]; |
| 60 | |
Kévin Redon | 69b92d9 | 2019-01-24 16:39:20 +0100 | [diff] [blame] | 61 | /** |
| 62 | * \brief CDC ACM Init |
| 63 | */ |
| 64 | void cdc_device_acm_init(void) |
| 65 | { |
| 66 | /* usb stack init */ |
| 67 | usbdc_init(ctrl_buffer); |
| 68 | |
| 69 | /* usbdc_register_funcion inside */ |
| 70 | cdcdf_acm_init(); |
| 71 | |
Harald Welte | 34a8706 | 2019-04-19 22:33:36 +0200 | [diff] [blame] | 72 | usbdc_start((struct usbd_descriptors *) usb_descs); |
Kévin Redon | 69b92d9 | 2019-01-24 16:39:20 +0100 | [diff] [blame] | 73 | usbdc_attach(); |
| 74 | } |
| 75 | |
| 76 | /** |
Kévin Redon | 8e53800 | 2019-01-30 11:19:19 +0100 | [diff] [blame] | 77 | * \brief Start USB stack |
Kévin Redon | 69b92d9 | 2019-01-24 16:39:20 +0100 | [diff] [blame] | 78 | */ |
Kévin Redon | 8e53800 | 2019-01-30 11:19:19 +0100 | [diff] [blame] | 79 | void usb_start(void) |
Kévin Redon | 69b92d9 | 2019-01-24 16:39:20 +0100 | [diff] [blame] | 80 | { |
| 81 | while (!cdcdf_acm_is_enabled()) { |
| 82 | // wait cdc acm to be installed |
| 83 | }; |
| 84 | |
| 85 | 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] | 86 | } |
| 87 | |
| 88 | void usb_init(void) |
| 89 | { |
| 90 | |
| 91 | cdc_device_acm_init(); |
Harald Welte | c7a58ba | 2019-04-18 17:59:19 +0200 | [diff] [blame] | 92 | ccid_df_init(); |
Kévin Redon | 69b92d9 | 2019-01-24 16:39:20 +0100 | [diff] [blame] | 93 | } |