46 #include "nrf_drv_usbd.h" 48 #include "app_usbd_core.h" 49 #include "app_usbd_string_desc.h" 50 #include "app_usbd_cdc_acm.h" 51 #include "app_usbd_serial_num.h" 56 cdc_acm_port_ev_handler(app_usbd_class_inst_t
const *p_inst,
57 app_usbd_cdc_acm_user_event_t event);
59 #define CDC_ACM_COMM_INTERFACE 0 60 #define CDC_ACM_COMM_EPIN NRF_DRV_USBD_EPIN2 62 #define CDC_ACM_DATA_INTERFACE 1 63 #define CDC_ACM_DATA_EPIN NRF_DRV_USBD_EPIN1 64 #define CDC_ACM_DATA_EPOUT NRF_DRV_USBD_EPOUT1 66 APP_USBD_CDC_ACM_GLOBAL_DEF(m_app_cdc_acm, cdc_acm_port_ev_handler,
67 CDC_ACM_COMM_INTERFACE, CDC_ACM_DATA_INTERFACE,
68 CDC_ACM_COMM_EPIN, CDC_ACM_DATA_EPIN,
70 APP_USBD_CDC_COMM_PROTOCOL_NONE);
72 #define RX_BUFFER_SIZE NRF_DRV_USBD_EPSIZE 73 #define TX_BUFFER_SIZE NRF_DRV_USBD_EPSIZE 75 static uint8_t usb_rx_data[RX_BUFFER_SIZE];
76 static uint8_t usb_tx_data[TX_BUFFER_SIZE];
78 static volatile uint8_t enabled = 0;
79 static volatile uint8_t buffered_data = 0;
80 static volatile uint8_t tx_buffer_busy = 0;
83 static int (*input_handler)(
unsigned char c);
86 cdc_acm_port_ev_handler(app_usbd_class_inst_t
const *p_inst,
87 app_usbd_cdc_acm_user_event_t event)
89 app_usbd_cdc_acm_t
const *p_cdc_acm = app_usbd_cdc_acm_class_get(p_inst);
92 case APP_USBD_CDC_ACM_USER_EVT_PORT_OPEN:
95 app_usbd_cdc_acm_read_any(&m_app_cdc_acm, usb_rx_data, RX_BUFFER_SIZE);
100 case APP_USBD_CDC_ACM_USER_EVT_PORT_CLOSE:
107 case APP_USBD_CDC_ACM_USER_EVT_TX_DONE:
113 case APP_USBD_CDC_ACM_USER_EVT_RX_DONE:
118 size_t rx_size = app_usbd_cdc_acm_rx_size(p_cdc_acm);
123 for(i = 0; i < rx_size; i++) {
124 input_handler(usb_rx_data[i]);
129 ret = app_usbd_cdc_acm_read_any(&m_app_cdc_acm,
130 usb_rx_data, RX_BUFFER_SIZE);
131 }
while(ret == NRF_SUCCESS);
145 app_usbd_class_inst_t
const *class_cdc_acm;
147 app_usbd_serial_num_generate();
149 ret = app_usbd_init(NULL);
150 if(ret != NRF_SUCCESS) {
154 class_cdc_acm = app_usbd_cdc_acm_class_inst_get(&m_app_cdc_acm);
156 ret = app_usbd_class_append(class_cdc_acm);
157 if(ret != NRF_SUCCESS) {
171 if(!enabled || tx_buffer_busy || buffered_data == 0) {
179 ret = app_usbd_cdc_acm_write(&m_app_cdc_acm, usb_tx_data, buffered_data);
181 }
while(ret != NRF_SUCCESS);
184 while(tx_buffer_busy) {
199 if(buffered_data < TX_BUFFER_SIZE) {
200 usb_tx_data[buffered_data] = b;
204 if(buffered_data == TX_BUFFER_SIZE) {
212 input_handler =
input;
void usb_serial_init()
Initialise the Serial-over-USB process.
void usb_serial_writeb(uint8_t b)
Write a byte over USB.
void usb_serial_flush()
Immediately transmit the content of Serial-over-USB TX buffers.
void watchdog_periodic(void)
Writes the WDT clear sequence.
static void input(void)
Process a received 6lowpan packet.
void usb_serial_set_input(int(*input)(unsigned char c))
Set an input hook for bytes received over USB.