46#include "nrf_drv_power.h"
47#include "nrf_drv_usbd.h"
49#include "app_usbd_core.h"
50#include "app_usbd_string_desc.h"
51#include "app_usbd_cdc_acm.h"
52#include "app_usbd_serial_num.h"
57cdc_acm_port_ev_handler(app_usbd_class_inst_t
const *p_inst,
58 app_usbd_cdc_acm_user_event_t event);
60#define CDC_ACM_COMM_INTERFACE 0
61#define CDC_ACM_COMM_EPIN NRF_DRV_USBD_EPIN2
63#define CDC_ACM_DATA_INTERFACE 1
64#define CDC_ACM_DATA_EPIN NRF_DRV_USBD_EPIN1
65#define CDC_ACM_DATA_EPOUT NRF_DRV_USBD_EPOUT1
67APP_USBD_CDC_ACM_GLOBAL_DEF(m_app_cdc_acm, cdc_acm_port_ev_handler,
68 CDC_ACM_COMM_INTERFACE, CDC_ACM_DATA_INTERFACE,
69 CDC_ACM_COMM_EPIN, CDC_ACM_DATA_EPIN,
71 APP_USBD_CDC_COMM_PROTOCOL_NONE);
73#define RX_BUFFER_SIZE NRF_DRV_USBD_EPSIZE
74#define TX_BUFFER_SIZE NRF_DRV_USBD_EPSIZE
76static uint8_t usb_rx_data[RX_BUFFER_SIZE];
77static uint8_t usb_tx_data[TX_BUFFER_SIZE];
79static volatile uint8_t enabled = 0;
80static volatile uint8_t buffered_data = 0;
81static volatile uint8_t tx_buffer_busy = 0;
84static int (*input_handler)(
unsigned char c);
87cdc_acm_port_ev_handler(app_usbd_class_inst_t
const *p_inst,
88 app_usbd_cdc_acm_user_event_t event)
90 app_usbd_cdc_acm_t
const *p_cdc_acm = app_usbd_cdc_acm_class_get(p_inst);
93 case APP_USBD_CDC_ACM_USER_EVT_PORT_OPEN:
96 app_usbd_cdc_acm_read_any(&m_app_cdc_acm, usb_rx_data, RX_BUFFER_SIZE);
101 case APP_USBD_CDC_ACM_USER_EVT_PORT_CLOSE:
108 case APP_USBD_CDC_ACM_USER_EVT_TX_DONE:
114 case APP_USBD_CDC_ACM_USER_EVT_RX_DONE:
119 size_t rx_size = app_usbd_cdc_acm_rx_size(p_cdc_acm);
124 for(i = 0; i < rx_size; i++) {
125 input_handler(usb_rx_data[i]);
130 ret = app_usbd_cdc_acm_read_any(&m_app_cdc_acm,
131 usb_rx_data, RX_BUFFER_SIZE);
132 }
while(ret == NRF_SUCCESS);
143usbd_user_ev_handler(app_usbd_event_type_t event)
146 case APP_USBD_EVT_STOPPED:
154 case APP_USBD_EVT_POWER_DETECTED:
156 if(!nrf_drv_usbd_is_enabled()) {
162 case APP_USBD_EVT_POWER_REMOVED:
170 case APP_USBD_EVT_POWER_READY:
184 static const app_usbd_config_t usbd_config = {
185 .ev_state_proc = usbd_user_ev_handler
189 app_usbd_class_inst_t
const *class_cdc_acm;
191 app_usbd_serial_num_generate();
193 ret = app_usbd_init(&usbd_config);
194 if(ret != NRF_SUCCESS) {
198 class_cdc_acm = app_usbd_cdc_acm_class_inst_get(&m_app_cdc_acm);
200 ret = app_usbd_class_append(class_cdc_acm);
201 if(ret != NRF_SUCCESS) {
205 ret = app_usbd_power_events_enable();
206 if(ret != NRF_SUCCESS) {
217 if(!enabled || tx_buffer_busy || buffered_data == 0) {
225 ret = app_usbd_cdc_acm_write(&m_app_cdc_acm, usb_tx_data, buffered_data);
227 }
while(ret != NRF_SUCCESS);
230 while(tx_buffer_busy) {
245 if(buffered_data < TX_BUFFER_SIZE) {
246 usb_tx_data[buffered_data] = b;
250 if(buffered_data == TX_BUFFER_SIZE) {
258 input_handler =
input;
void usb_serial_writeb(uint8_t b)
Write a byte over USB.
void usb_serial_set_input(int(*input)(unsigned char c))
Set an input hook for bytes received over USB.
void usb_serial_flush()
Immediately transmit the content of Serial-over-USB TX buffers.
void usb_serial_init()
Initialise the Serial-over-USB process.
void watchdog_periodic(void)
Writes the WDT clear sequence.
static void input(void)
Process a received 6lowpan packet.