I have the ble_peripheral blinky example running, and I have a separate project with the USBD working fine (based on the usbd_cdc_acm example), but when I try to merge the two together, the app_usbd_init causes a crash. Is it not possible? What am I doing wrong?
Thanks in advance. I've attached my usbd_out source file.
--jeff
#include "app_usbd.h"
#include "app_usbd_cdc_acm.h"
#include "app_usbd_serial_num.h"
//#include "app_usbd_string_desc.h"
#include "boards.h"
#include "nrf_drv_clock.h"
#ifndef USBD_POWER_DETECTION
#define USBD_POWER_DETECTION true
#endif
#define CDC_ACM_COMM_INTERFACE 0
#define CDC_ACM_COMM_EPIN NRF_DRV_USBD_EPIN2
#define CDC_ACM_DATA_INTERFACE 1
#define CDC_ACM_DATA_EPIN NRF_DRV_USBD_EPIN1
#define CDC_ACM_DATA_EPOUT NRF_DRV_USBD_EPOUT1
#define READ_SIZE 1
static char m_rx_buffer[READ_SIZE];
static char m_tx_buffer[NRF_DRV_USBD_EPSIZE];
static void cdc_acm_user_ev_handler(app_usbd_class_inst_t const * p_inst, app_usbd_cdc_acm_user_event_t event);
static void usbd_user_ev_handler(app_usbd_event_type_t event);
/**
* @brief CDC_ACM class instance
* */
APP_USBD_CDC_ACM_GLOBAL_DEF(m_app_cdc_acm,
cdc_acm_user_ev_handler,
CDC_ACM_COMM_INTERFACE,
CDC_ACM_DATA_INTERFACE,
CDC_ACM_COMM_EPIN,
CDC_ACM_DATA_EPIN,
CDC_ACM_DATA_EPOUT,
//APP_USBD_CDC_COMM_PROTOCOL_AT_V250
APP_USBD_CDC_COMM_PROTOCOL_NONE
);
/*
typedef struct serial_command_hdr
{
zb_uint16_t size;
zb_uint8_t command; // serial_command_t
} serial_command_hdr_t;
static void serial_reset()
{
serial_command_hdr_t rts;
rts.size = 0xC0C0;
rts.command = SERIAL_CMD_INVALID;
app_usbd_cdc_acm_write(&serial_uart, &rts, sizeof(rts) );
}
*/
static void cdc_acm_user_ev_handler(app_usbd_class_inst_t const * p_inst,
app_usbd_cdc_acm_user_event_t event)
{
app_usbd_cdc_acm_t const * p_cdc_acm = app_usbd_cdc_acm_class_get(p_inst);
switch (event)
{
case APP_USBD_CDC_ACM_USER_EVT_PORT_OPEN:
{
// bsp_board_led_on(LED_CDC_ACM_OPEN);
/*Setup first transfer*/
ret_code_t ret = app_usbd_cdc_acm_read(&m_app_cdc_acm,
m_rx_buffer,
READ_SIZE);
UNUSED_VARIABLE(ret);
break;
}
case APP_USBD_CDC_ACM_USER_EVT_PORT_CLOSE:
// bsp_board_led_off(LED_CDC_ACM_OPEN);
break;
case APP_USBD_CDC_ACM_USER_EVT_TX_DONE:
// bsp_board_led_invert(LED_CDC_ACM_TX);
break;
case APP_USBD_CDC_ACM_USER_EVT_RX_DONE:
{
ret_code_t ret;
// NRF_LOG_INFO("Bytes waiting: %d", app_usbd_cdc_acm_bytes_stored(p_cdc_acm));
do
{
/*Get amount of data transfered*/
size_t size = app_usbd_cdc_acm_rx_size(p_cdc_acm);
// NRF_LOG_INFO("RX: size: %lu char: %c", size, m_rx_buffer[0]);
/* Fetch data until internal buffer is empty */
ret = app_usbd_cdc_acm_read(&m_app_cdc_acm,
m_rx_buffer,
READ_SIZE);
} while (ret == NRF_SUCCESS);
// bsp_board_led_invert(LED_CDC_ACM_RX);
break;
}
default:
break;
}
}
static void usbd_user_ev_handler(app_usbd_event_type_t event)
{
switch (event)
{
case APP_USBD_EVT_DRV_SUSPEND:
// PRINT("USB_RESUME\n",NULL);
break;
case APP_USBD_EVT_DRV_RESUME:
// PRINT("USB_RESUME\n",NULL);
break;
case APP_USBD_EVT_STARTED:
break;
case APP_USBD_EVT_STOPPED:
app_usbd_disable();
// bsp_board_leds_off();
break;
case APP_USBD_EVT_POWER_DETECTED:
// PRINT("USB power detected\n",NULL);
if (!nrf_drv_usbd_is_enabled())
{
app_usbd_enable();
}
break;
case APP_USBD_EVT_POWER_REMOVED:
// PRINT("USB power removed\n",NULL);
app_usbd_stop();
break;
case APP_USBD_EVT_POWER_READY:
// PRINT("USB ready\n",NULL);
app_usbd_start();
break;
default:
// PRINT("USB unhandled event:%x\n", event);
break;
}
}
void usbd_message(char *msg)
{
#ifdef NotNow
size_t size = sprintf(m_tx_buffer, msg);
app_usbd_cdc_acm_write(&m_app_cdc_acm, m_tx_buffer, size);
#endif
}
void serial_init()
{
ret_code_t ret;
bsp_board_led_on(BSP_BOARD_LED_3);
static const app_usbd_config_t usbd_config = {
.ev_state_proc = usbd_user_ev_handler
};
ret = nrf_drv_clock_init();
// APP_ERROR_CHECK(ret);
bsp_board_led_on(BSP_BOARD_LED_2);
nrf_drv_clock_lfclk_request(NULL);
while(!nrf_drv_clock_lfclk_is_running())
{
/* Just waiting */
}
bsp_board_led_on(BSP_BOARD_LED_1);
app_usbd_serial_num_generate();
ret = app_usbd_init(&usbd_config);
bsp_board_led_on(BSP_BOARD_LED_0);
return;
app_usbd_class_inst_t const * class_cdc_acm = app_usbd_cdc_acm_class_inst_get(&m_app_cdc_acm);
ret = app_usbd_class_append(class_cdc_acm);
// EVT_LOG("USBD CDC ACM example started.\n", NULL);
if (USBD_POWER_DETECTION)
{
ret = app_usbd_power_events_enable();
}
else
{
// EVT_LOG("No USB power detection enabled\nStarting USB now\n", NULL);
app_usbd_enable();
app_usbd_start();
}
}