Exemplo n.º 1
0
/**
 * @brief User event handler.
 * */
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:
        {
            m_port_is_open = true;
            /*Setup first transfer*/
            ret_code_t ret = app_usbd_cdc_acm_read(&nrf_cli_cdc_acm,
                                                   m_rx_buffer,
                                                   sizeof(m_rx_buffer));
            APP_ERROR_CHECK(ret);
            break;
        }
        case APP_USBD_CDC_ACM_USER_EVT_PORT_CLOSE:
            m_port_is_open = false;
            break;
        case APP_USBD_CDC_ACM_USER_EVT_TX_DONE:
            break;
        case APP_USBD_CDC_ACM_USER_EVT_RX_DONE:
        {
            /*Get amount of data transfered*/
            size_t size = app_usbd_cdc_acm_rx_size(p_cdc_acm);
            size_t qsize = nrf_queue_in(&m_rx_queue, m_rx_buffer, size);
            ASSERT(size == qsize);

            /*Setup next transfer*/
            ret_code_t ret = app_usbd_cdc_acm_read(&nrf_cli_cdc_acm,
                                                   m_rx_buffer,
                                                   sizeof(m_rx_buffer));

            ASSERT(ret == NRF_SUCCESS); /*Should not happen*/
            break;
        }
        default:
            break;
    }
}
Exemplo n.º 2
0
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:
        NRF_LOG_DEBUG("EVT_PORT_OPEN");
        if (!m_port_open)
        {
            ret_code_t ret_code;

            m_port_open = true;

            do {
                ret_code = app_usbd_cdc_acm_read(p_cdc_acm, &m_rx_byte, 1);
                if (ret_code == NRF_SUCCESS)
                {
                    ser_phi_hci_rx_byte(m_rx_byte);
                }
                else if (ret_code != NRF_ERROR_IO_PENDING)
                {
                    APP_ERROR_CHECK(ret_code);
                }
            } while (ret_code == NRF_SUCCESS);
        }
        break;

    case APP_USBD_CDC_ACM_USER_EVT_PORT_CLOSE:
        NRF_LOG_DEBUG("EVT_PORT_CLOSE");
        if (m_tx_in_progress)
        {
            m_ser_phy_hci_slip_event.evt_type = SER_PHY_HCI_SLIP_EVT_PKT_SENT;
            m_ser_phy_hci_slip_event_handler(&m_ser_phy_hci_slip_event);
            m_tx_in_progress = false;
        }
        m_port_open = false;
        break;

    case APP_USBD_CDC_ACM_USER_EVT_TX_DONE:
        // If there is a pending transfer (the second buffer is ready to
        // be sent), start it immediately.
        if (m_tx_pending)
        {
            APP_ERROR_CHECK(app_usbd_cdc_acm_write(p_cdc_acm,
                mp_tx_buf, m_tx_bytes));

            // Switch to the buffer that has just been sent completely
            // and now can be filled again.
            mp_tx_buf = (mp_tx_buf == m_tx_buf0) ? m_tx_buf1 : m_tx_buf0;
            m_tx_bytes = 0;

            m_ser_phy_hci_slip_event.evt_type = m_tx_evt_type;
            m_tx_evt_type = m_tx_pending_evt_type;

            m_tx_pending = false;
        }
        else
        {
            m_tx_in_progress = false;
            m_ser_phy_hci_slip_event.evt_type = m_tx_evt_type;
        }
        // If needed, notify the upper layer that the packet transfer is
        // complete (note that this notification may result in another
        // packet send request, so everything must be cleaned up above).
        if (m_ser_phy_hci_slip_event.evt_type != NO_EVENT)
        {
            m_ser_phy_hci_slip_event_handler(&m_ser_phy_hci_slip_event);
        }
        // And if the sending process is not yet finished, look what is
        // to be done next.
        if (m_tx_phase != PHASE_IDLE)
        {
            tx_buf_fill();
        }
        break;

    case APP_USBD_CDC_ACM_USER_EVT_RX_DONE:
        {
            ret_code_t ret_code;
            do
            {
                ser_phi_hci_rx_byte(m_rx_byte);

                ret_code = app_usbd_cdc_acm_read(p_cdc_acm, &m_rx_byte, 1);
            } while (ret_code == NRF_SUCCESS);
        }
        break;

    default:
        break;
    }
}