espressif_tinyusb/src/class/cdc/cdc_device.c

364 lines
12 KiB
C
Raw Normal View History

/**************************************************************************/
/*!
@file cdc_device.c
@author hathach (tinyusb.org)
@section LICENSE
Software License Agreement (BSD License)
Copyright (c) 2013, hathach (tinyusb.org)
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holders nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This file is part of the tinyusb stack.
*/
/**************************************************************************/
#include "tusb_option.h"
2018-04-12 08:23:52 +02:00
#if (MODE_DEVICE_SUPPORTED && CFG_TUD_CDC)
#define _TINY_USB_SOURCE_FILE_
//--------------------------------------------------------------------+
// INCLUDE
//--------------------------------------------------------------------+
2018-03-22 08:15:16 +01:00
#include "common/tusb_common.h"
#include "cdc_device.h"
2018-03-22 08:15:16 +01:00
#include "device/usbd_pvt.h"
//--------------------------------------------------------------------+
// MACRO CONSTANT TYPEDEF
//--------------------------------------------------------------------+
typedef struct
{
2018-04-19 11:35:58 +02:00
uint8_t itf_num;
uint8_t ep_notif;
uint8_t ep_in;
uint8_t ep_out;
// Bit 0: DTR (Data Terminal Ready), Bit 1: RTS (Request to Send)
uint8_t line_state;
// Data that is not cleared by usb bus reset
struct {
cdc_line_coding_t line_coding;
char wanted_char;
uint8_t rx_ff_buf[CFG_TUD_CDC_RX_BUFSIZE];
uint8_t tx_ff_buf[CFG_TUD_CDC_RX_BUFSIZE];
tu_fifo_t rx_ff;
tu_fifo_t tx_ff;
}intact;
2018-04-19 11:35:58 +02:00
}cdcd_interface_t;
//--------------------------------------------------------------------+
// INTERNAL OBJECT & FUNCTION DECLARATION
//--------------------------------------------------------------------+
// TODO multiple interfaces
CFG_TUSB_ATTR_USBRAM CFG_TUSB_MEM_ALIGN static uint8_t _rx_buf[64];
CFG_TUSB_ATTR_USBRAM CFG_TUSB_MEM_ALIGN static uint8_t _tx_buf[64];
2018-03-08 08:38:06 +01:00
static cdcd_interface_t _cdcd_itf[CFG_TUD_CDC];
2018-03-08 09:00:38 +01:00
//--------------------------------------------------------------------+
// APPLICATION API
//--------------------------------------------------------------------+
bool tud_cdc_n_connected(uint8_t itf)
{
2018-04-17 12:06:40 +02:00
// DTR (bit 0) active isconsidered as connected
return BIT_TEST_(_cdcd_itf[itf].line_state, 0);
2018-03-08 09:00:38 +01:00
}
uint8_t tud_cdc_n_get_line_state (uint8_t itf)
{
return _cdcd_itf[itf].line_state;
}
void tud_cdc_n_get_line_coding (uint8_t itf, cdc_line_coding_t* coding)
{
(*coding) = _cdcd_itf[itf].intact.line_coding;
}
void tud_cdc_n_set_wanted_char (uint8_t itf, char wanted)
{
_cdcd_itf[itf].intact.wanted_char = wanted;
}
//--------------------------------------------------------------------+
// READ API
//--------------------------------------------------------------------+
uint32_t tud_cdc_n_available(uint8_t itf)
2018-03-08 09:00:38 +01:00
{
return tu_fifo_count(&_cdcd_itf[itf].intact.rx_ff);
2018-03-08 09:00:38 +01:00
}
char tud_cdc_n_read_char(uint8_t itf)
2018-03-08 09:00:38 +01:00
{
char ch;
return tu_fifo_read(&_cdcd_itf[itf].intact.rx_ff, &ch) ? ch : (-1);
}
uint32_t tud_cdc_n_read(uint8_t itf, void* buffer, uint32_t bufsize)
{
return tu_fifo_read_n(&_cdcd_itf[itf].intact.rx_ff, buffer, bufsize);
}
char tud_cdc_n_peek(uint8_t itf, int pos)
{
char ch;
return tu_fifo_peek_at(&_cdcd_itf[itf].intact.rx_ff, pos, &ch) ? ch : (-1);
}
void tud_cdc_n_read_flush (uint8_t itf)
{
tu_fifo_clear(&_cdcd_itf[itf].intact.rx_ff);
}
//--------------------------------------------------------------------+
// WRITE API
//--------------------------------------------------------------------+
uint32_t tud_cdc_n_write_char(uint8_t itf, char ch)
{
return tu_fifo_write(&_cdcd_itf[itf].intact.tx_ff, &ch) ? 1 : 0;
}
uint32_t tud_cdc_n_write(uint8_t itf, void const* buffer, uint32_t bufsize)
{
return tu_fifo_write_n(&_cdcd_itf[itf].intact.tx_ff, buffer, bufsize);
}
bool tud_cdc_n_write_flush (uint8_t itf)
{
uint8_t edpt = _cdcd_itf[itf].ep_in;
VERIFY( !dcd_edpt_busy(TUD_RHPORT, edpt) ); // skip if previous transfer not complete
uint16_t count = tu_fifo_read_n(&_cdcd_itf[itf].intact.tx_ff, _tx_buf, sizeof(_tx_buf));
VERIFY( tud_cdc_n_connected(itf) ); // fifo is empty if not connected
if ( count ) TU_ASSERT( dcd_edpt_xfer(TUD_RHPORT, edpt, _tx_buf, count) );
return true;
}
2018-03-08 09:00:38 +01:00
//--------------------------------------------------------------------+
2018-03-08 09:00:38 +01:00
// USBD Driver API
//--------------------------------------------------------------------+
void cdcd_init(void)
{
arrclr_( _cdcd_itf );
for(uint8_t i=0; i<CFG_TUD_CDC; i++)
{
_cdcd_itf[i].intact.wanted_char = -1;
// default line coding is : stop bit = 1, parity = none, data bits = 8
_cdcd_itf[i].intact.line_coding.bit_rate = 115200;
_cdcd_itf[i].intact.line_coding.stop_bits = 0;
_cdcd_itf[i].intact.line_coding.parity = 0;
_cdcd_itf[i].intact.line_coding.data_bits = 8;
// config fifo
tu_fifo_config(&_cdcd_itf[i].intact.rx_ff, _cdcd_itf[i].intact.rx_ff_buf, CFG_TUD_CDC_RX_BUFSIZE, 1, true);
tu_fifo_config(&_cdcd_itf[i].intact.tx_ff, _cdcd_itf[i].intact.tx_ff_buf, CFG_TUD_CDC_TX_BUFSIZE, 1, false);
}
}
void cdcd_reset(uint8_t rhport)
{
(void) rhport;
for(uint8_t i=0; i<CFG_TUD_CDC; i++)
2018-03-07 05:01:23 +01:00
{
memclr_(&_cdcd_itf[i], offsetof(cdcd_interface_t, intact));
tu_fifo_clear(&_cdcd_itf[i].intact.rx_ff);
tu_fifo_clear(&_cdcd_itf[i].intact.tx_ff);
2018-03-07 05:01:23 +01:00
}
}
2018-03-23 06:32:40 +01:00
tusb_error_t cdcd_open(uint8_t rhport, tusb_desc_interface_t const * p_interface_desc, uint16_t *p_length)
{
if ( CDC_COMM_SUBCLASS_ABSTRACT_CONTROL_MODEL != p_interface_desc->bInterfaceSubClass) return TUSB_ERROR_CDC_UNSUPPORTED_SUBCLASS;
if ( !(is_in_range(CDC_COMM_PROTOCOL_ATCOMMAND, p_interface_desc->bInterfaceProtocol, CDC_COMM_PROTOCOL_ATCOMMAND_CDMA) ||
0xff == p_interface_desc->bInterfaceProtocol) )
{
return TUSB_ERROR_CDC_UNSUPPORTED_PROTOCOL;
}
// Find available interface
cdcd_interface_t * p_cdc = NULL;
for(uint8_t i=0; i<CFG_TUD_CDC; i++)
{
if ( _cdcd_itf[i].ep_in == 0 )
{
p_cdc = &_cdcd_itf[i];
break;
}
}
//------------- Control Interface -------------//
p_cdc->itf_num = p_interface_desc->bInterfaceNumber;
uint8_t const * p_desc = descriptor_next ( (uint8_t const *) p_interface_desc );
2018-03-23 06:32:40 +01:00
(*p_length) = sizeof(tusb_desc_interface_t);
// Communication Functional Descriptors
2018-07-13 12:48:26 +02:00
while( TUSB_DESC_CLASS_SPECIFIC == p_desc[DESC_OFFSET_TYPE] )
{
2018-07-13 12:48:26 +02:00
(*p_length) += p_desc[DESC_OFFSET_LEN];
p_desc = descriptor_next(p_desc);
}
2018-07-13 12:48:26 +02:00
if ( TUSB_DESC_ENDPOINT == p_desc[DESC_OFFSET_TYPE])
{ // notification endpoint if any
2018-03-28 08:47:58 +02:00
TU_ASSERT( dcd_edpt_open(rhport, (tusb_desc_endpoint_t const *) p_desc), TUSB_ERROR_DCD_OPEN_PIPE_FAILED);
2018-03-23 06:32:40 +01:00
p_cdc->ep_notif = ((tusb_desc_endpoint_t const *) p_desc)->bEndpointAddress;
2018-07-13 12:48:26 +02:00
(*p_length) += p_desc[DESC_OFFSET_LEN];
p_desc = descriptor_next(p_desc);
}
//------------- Data Interface (if any) -------------//
2018-07-13 12:48:26 +02:00
if ( (TUSB_DESC_INTERFACE == p_desc[DESC_OFFSET_TYPE]) &&
(TUSB_CLASS_CDC_DATA == ((tusb_desc_interface_t const *) p_desc)->bInterfaceClass) )
{
// next to endpoint descritpor
2018-07-13 12:48:26 +02:00
(*p_length) += p_desc[DESC_OFFSET_LEN];
p_desc = descriptor_next(p_desc);
// Open endpoint pair with usbd helper
tusb_desc_endpoint_t const *p_desc_ep = (tusb_desc_endpoint_t const *) p_desc;
TU_ASSERT_ERR( usbd_open_edpt_pair(rhport, p_desc_ep, TUSB_XFER_BULK, &p_cdc->ep_out, &p_cdc->ep_in) );
(*p_length) += 2*sizeof(tusb_desc_endpoint_t);
}
2018-03-08 08:38:06 +01:00
// Prepare for incoming data
TU_ASSERT( dcd_edpt_xfer(rhport, p_cdc->ep_out, _rx_buf, sizeof(_rx_buf)), TUSB_ERROR_DCD_EDPT_XFER);
2018-03-08 08:38:06 +01:00
return TUSB_ERROR_NONE;
}
tusb_error_t cdcd_control_request_st(uint8_t rhport, tusb_control_request_t const * p_request)
{
2018-03-15 07:22:28 +01:00
OSAL_SUBTASK_BEGIN
//------------- Class Specific Request -------------//
2018-03-11 15:05:27 +01:00
if (p_request->bmRequestType_bit.type != TUSB_REQ_TYPE_CLASS) return TUSB_ERROR_DCD_CONTROL_REQUEST_NOT_SUPPORT;
// TODO Support multiple interfaces
uint8_t const itf = 0;
cdcd_interface_t* p_cdc = &_cdcd_itf[itf];
if ( (CDC_REQUEST_GET_LINE_CODING == p_request->bRequest) || (CDC_REQUEST_SET_LINE_CODING == p_request->bRequest) )
2018-03-15 07:22:28 +01:00
{
uint16_t len = min16_of(sizeof(cdc_line_coding_t), p_request->wLength);
usbd_control_xfer_st(rhport, p_request->bmRequestType_bit.direction, &p_cdc->intact.line_coding, len);
// Invoke callback
if (CDC_REQUEST_SET_LINE_CODING == p_request->bRequest)
{
if ( tud_cdc_line_coding_cb ) tud_cdc_line_coding_cb(itf, &p_cdc->intact.line_coding);
}
2018-03-15 07:22:28 +01:00
}
else if (CDC_REQUEST_SET_CONTROL_LINE_STATE == p_request->bRequest )
{
2018-04-09 13:48:38 +02:00
// CDC PSTN v1.2 section 6.3.12
// Bit 0: Indicates if DTE is present or not.
// This signal corresponds to V.24 signal 108/2 and RS-232 signal DTR (Data Terminal Ready)
// Bit 1: Carrier control for half-duplex modems.
// This signal corresponds to V.24 signal 105 and RS-232 signal RTS (Request to Send)
2018-03-08 08:38:06 +01:00
p_cdc->line_state = (uint8_t) p_request->wValue;
2018-04-09 13:48:38 +02:00
dcd_control_status(rhport, p_request->bmRequestType_bit.direction); // ACK control request
// Invoke callback
if ( tud_cdc_line_state_cb) tud_cdc_line_state_cb(itf, BIT_TEST_(p_request->wValue, 0), BIT_TEST_(p_request->wValue, 1));
2018-03-15 07:22:28 +01:00
}
else
{
2018-03-28 08:54:28 +02:00
dcd_control_stall(rhport); // stall unsupported request
}
2018-03-15 07:22:28 +01:00
OSAL_SUBTASK_END
}
tusb_error_t cdcd_xfer_cb(uint8_t rhport, uint8_t ep_addr, tusb_event_t event, uint32_t xferred_bytes)
{
// TODO Support multiple interfaces
uint8_t const itf = 0;
cdcd_interface_t* p_cdc = &_cdcd_itf[itf];
// receive new data
2018-03-22 09:55:35 +01:00
if ( ep_addr == p_cdc->ep_out )
{
char const wanted = p_cdc->intact.wanted_char;
for(uint32_t i=0; i<xferred_bytes; i++)
{
// Check for wanted char and invoke callback if needed
if ( tud_cdc_rx_wanted_cb && ( wanted != -1 ) && ( wanted == _rx_buf[i] ) )
{
tud_cdc_rx_wanted_cb(itf, wanted);
}else
{
tu_fifo_write(&p_cdc->intact.rx_ff, &_rx_buf[i]);
}
}
// invoke receive callback (if there is still data)
if (tud_cdc_rx_cb && tu_fifo_count(&p_cdc->intact.rx_ff) ) tud_cdc_rx_cb(itf);
2018-03-08 08:38:06 +01:00
// prepare for next
TU_ASSERT( dcd_edpt_xfer(rhport, p_cdc->ep_out, _rx_buf, sizeof(_rx_buf)), TUSB_ERROR_DCD_EDPT_XFER );
2018-03-08 08:38:06 +01:00
}
2018-03-07 09:30:32 +01:00
// nothing to do with in and notif endpoint
return TUSB_ERROR_NONE;
}
2018-06-06 08:40:02 +02:00
#if CFG_TUD_CDC_FLUSH_ON_SOF
void cdcd_sof(uint8_t rhport)
{
for(uint8_t i=0; i<CFG_TUD_CDC; i++)
{
tud_cdc_n_flush(i);
}
2018-03-08 08:38:06 +01:00
}
2018-06-06 08:40:02 +02:00
#endif
2018-03-08 08:38:06 +01:00
#endif