blob: c6d9fe940b675b948f4844f683950a666a1aae81 [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
Kévin Redon69b92d92019-01-24 16:39:20 +010011#define CDCD_ECHO_BUF_SIZ CONF_USB_CDCD_ACM_DATA_BULKIN_MAXPKSZ
Kévin Redon69b92d92019-01-24 16:39:20 +010012
13/** Buffers to receive and echo the communication bytes. */
14static uint32_t usbd_cdc_buffer[CDCD_ECHO_BUF_SIZ / 4];
15
16/** Ctrl endpoint buffer */
17static uint8_t ctrl_buffer[64];
18
19/**
20 * \brief Callback invoked when bulk OUT data received
21 */
22static 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 */
33static 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 */
45static 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 Welte34a87062019-04-19 22:33:36 +020059extern const struct usbd_descriptors usb_descs[];
60
Kévin Redon69b92d92019-01-24 16:39:20 +010061/**
62 * \brief CDC ACM Init
63 */
64void 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 Welte34a87062019-04-19 22:33:36 +020072 usbdc_start((struct usbd_descriptors *) usb_descs);
Kévin Redon69b92d92019-01-24 16:39:20 +010073 usbdc_attach();
74}
75
76/**
Kévin Redon8e538002019-01-30 11:19:19 +010077 * \brief Start USB stack
Kévin Redon69b92d92019-01-24 16:39:20 +010078 */
Kévin Redon8e538002019-01-30 11:19:19 +010079void usb_start(void)
Kévin Redon69b92d92019-01-24 16:39:20 +010080{
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 Redon69b92d92019-01-24 16:39:20 +010086}
87
88void usb_init(void)
89{
90
91 cdc_device_acm_init();
Harald Weltec7a58ba2019-04-18 17:59:19 +020092 ccid_df_init();
Kévin Redon69b92d92019-01-24 16:39:20 +010093}