NetBurner 3.5.7
PDF Version

USB CDC Abstract Control Model Device Class. More...

#include <usbd_cdc_acm.h>

Inherits nbrtos::USB::Device.

Public Member Functions

 dCDC_ACM (nbrtos::USB::Ctlr *parent, const usb_dev_t *desc, const usb_cfg_t *cfg, const nbrtos::USB::StrDescTbl *strTbl)
 Construct a CDC ACM device instance.
 
virtual void Event_BusReset ()
 Handle USB bus reset event.
 
virtual void Event_Suspend ()
 Handle USB suspend event.
 
virtual void Event_Resume ()
 Handle USB resume event.
 
virtual int Event_ControlMsg (nbrtos::USB::bufPtr_t ctlMsg, uint8_t bufLen)
 Handle USB control message.
 
virtual int SetConfiguration (uint16_t wValue)
 Set the active USB configuration.
 
virtual int SetAltSetting (uint16_t wIndex, uint16_t wValue)
 Set alternate interface setting.
 
virtual int ProcessBuffer (uint8_t ep, PoolPtr pp, uint16_t bufLen)
 Process a received data buffer.
 
virtual uint16_t GetStatus (eReqTarget_t target, uint16_t wIndex, int *err)
 Get device or endpoint status.
 
virtual uint8_t GetActiveConfig (int *err)
 Get currently active configuration.
 
virtual uint8_t GetAltSetting (uint8_t intfNum, int *err)
 Get current alternate setting for an interface.
 
virtual uint16_t GetMaxPktSize (uint8_t epnum, eDir_t dir)
 Get maximum packet size for an endpoint.
 
nbrtos::USB::eBusState_t GetUSBState ()
 Get current USB bus state.
 
void SetUSBState (nbrtos::USB::eBusState_t newState)
 Set USB bus state.
 
int ReadData (uint8_t *pData, int rdLen)
 Read data from the CDC device (non-blocking)
 
int ReadWTimeout (uint8_t *pData, int rdLen, TickTimeout timeout)
 Read data from the CDC device with timeout.
 
int WriteData (uint8_t *pData, int wrLen)
 Write data to the CDC device (non-blocking)
 
int WriteWTimeout (uint8_t *pData, int wrLen, TickTimeout timeout)
 Write data to the CDC device with timeout.
 
int Open ()
 Open the CDC device for use.
 
int Peek (uint8_t *buf)
 Peek at the next byte without removing it.
 
virtual int ProcessSetupPkt (uint8_t epnum, usb_setup_pkt_t msg)
 Process USB setup packet.
 

Static Public Member Functions

static int sProcessBuffer (Device *dev, uint8_t ep, PoolPtr pp, uint16_t bufLen)
 Static callback wrapper for ProcessBuffer.
 

Detailed Description

USB CDC Abstract Control Model Device Class.

This class implements a USB CDC ACM device that provides virtual serial port functionality. When connected to a host computer, the device enumerates as a standard COM port, enabling serial communication over USB.

The class inherits from nbrtos::USB::Device and implements all required virtual functions for USB device operation, plus CDC-specific functionality for line coding and control signal management.

Key features:

  • Buffered transmit and receive with FIFO storage
  • File descriptor interface for standard I/O operations
  • Configurable line parameters (baud, parity, stop bits, data bits)
  • Event handling for USB bus states (reset, suspend, resume)
  • Timeout-based read/write operations
Note
The device state machine handles USB enumeration automatically. Application code should use Open() to initialize the device and then use ReadData()/WriteData() or the file descriptor interface for data transfer.

Expand for Example Usage

Examples

Basic CDC ACM Device Setup
#include <predef.h>
#include <init.h>
#include <usbd_cdc_acm.h>
// USB descriptors (defined elsewhere)
extern const usb_dev_t myDeviceDesc;
extern const usb_cfg_t myConfigDesc;
extern const nbrtos::USB::StrDescTbl myStringTable;
dCDC_ACM *pCdcDevice = nullptr;
void UserMain(void *pd)
{
init();
// Get USB controller instance
nbrtos::USB::Ctlr *pUsb = nbrtos::USB::GetController(0);
// Create CDC ACM device
pCdcDevice = new dCDC_ACM(pUsb, &myDeviceDesc, &myConfigDesc, &myStringTable);
// Open the device for use
int fd = pCdcDevice->Open();
if (fd < 0)
{
iprintf("Failed to open CDC device\r\n");
}
// Main loop - echo received data
uint8_t buffer[64];
while (1)
{
int bytesRead = pCdcDevice->ReadWTimeout(buffer, sizeof(buffer),
if (bytesRead > 0)
{
pCdcDevice->WriteData(buffer, bytesRead);
}
}
}
USB CDC Abstract Control Model Device Class.
Definition usbd_cdc_acm.h:189
int WriteData(uint8_t *pData, int wrLen)
Write data to the CDC device (non-blocking)
int ReadWTimeout(uint8_t *pData, int rdLen, TickTimeout timeout)
Read data from the CDC device with timeout.
int Open()
Open the CDC device for use.
dCDC_ACM(nbrtos::USB::Ctlr *parent, const usb_dev_t *desc, const usb_cfg_t *cfg, const nbrtos::USB::StrDescTbl *strTbl)
Construct a CDC ACM device instance.
#define TICKS_PER_SECOND
System clock ticks per second.
Definition constants.h:49
void init()
System initialization. Ideally called at the beginning of all applications, since the easiest Recover...
Reading Data with Timeout
void ProcessSerialData(dCDC_ACM *pCdc)
{
uint8_t rxBuffer[256];
// Wait up to 500ms for data
int bytesRead = pCdc->ReadWTimeout(rxBuffer, sizeof(rxBuffer),
if (bytesRead > 0)
{
iprintf("Received %d bytes\r\n", bytesRead);
// Process received data...
}
else if (bytesRead == 0)
{
// Timeout - no data received
}
else
{
// Error occurred
iprintf("Read error: %d\r\n", bytesRead);
}
}
Checking USB Connection State
void MonitorUSBState(dCDC_ACM *pCdc)
{
nbrtos::USB::eBusState_t state = pCdc->GetUSBState();
switch (state)
{
case nbrtos::USB::eBusState_Configured:
iprintf("USB configured and ready\r\n");
break;
case nbrtos::USB::eBusState_Suspended:
iprintf("USB suspended\r\n");
break;
case nbrtos::USB::eBusState_Default:
iprintf("USB in default state\r\n");
break;
default:
iprintf("USB state: %d\r\n", (int)state);
break;
}
}
nbrtos::USB::eBusState_t GetUSBState()
Get current USB bus state.

Constructor & Destructor Documentation

◆ dCDC_ACM()

dCDC_ACM::dCDC_ACM ( nbrtos::USB::Ctlr * parent,
const usb_dev_t * desc,
const usb_cfg_t * cfg,
const nbrtos::USB::StrDescTbl * strTbl )

Construct a CDC ACM device instance.

Creates a new CDC ACM device attached to the specified USB controller. The device will not be active until Open() is called.

Parameters
parentPointer to the USB controller this device is attached to
descPointer to the USB device descriptor
cfgPointer to the USB configuration descriptor (includes CDC interface and endpoint descriptors)
strTblPointer to the string descriptor table for device identification strings
Note
The descriptor pointers must remain valid for the lifetime of the device object.

Member Function Documentation

◆ Event_BusReset()

virtual void dCDC_ACM::Event_BusReset ( )
virtual

Handle USB bus reset event.

Called by the USB stack when a bus reset is detected. Resets the device state machine and clears any pending transfers.

◆ Event_ControlMsg()

virtual int dCDC_ACM::Event_ControlMsg ( nbrtos::USB::bufPtr_t ctlMsg,
uint8_t bufLen )
virtual

Handle USB control message.

Processes control endpoint messages, including CDC-specific class requests such as GET_LINE_CODING, SET_LINE_CODING, and SET_CONTROL_LINE_STATE.

Parameters
ctlMsgPointer to the control message buffer
bufLenLength of the control message
Returns
0 on success, or negative error code

◆ Event_Resume()

virtual void dCDC_ACM::Event_Resume ( )
virtual

Handle USB resume event.

Called by the USB stack when the bus resumes from suspend state. Normal operation can continue after this event.

◆ Event_Suspend()

virtual void dCDC_ACM::Event_Suspend ( )
virtual

Handle USB suspend event.

Called by the USB stack when the bus enters suspend state. The device should reduce power consumption when suspended.

◆ GetActiveConfig()

virtual uint8_t dCDC_ACM::GetActiveConfig ( int * err)
virtual

Get currently active configuration.

Returns the current configuration value for GET_CONFIGURATION requests.

Parameters
[out]errPointer to receive error code
Returns
Active configuration number (0 if not configured)

◆ GetAltSetting()

virtual uint8_t dCDC_ACM::GetAltSetting ( uint8_t intfNum,
int * err )
virtual

Get current alternate setting for an interface.

Returns the alternate setting for GET_INTERFACE requests.

Parameters
intfNumInterface number
[out]errPointer to receive error code
Returns
Alternate setting value

◆ GetMaxPktSize()

virtual uint16_t dCDC_ACM::GetMaxPktSize ( uint8_t epnum,
eDir_t dir )
virtual

Get maximum packet size for an endpoint.

Returns the maximum packet size configured for the specified endpoint.

Parameters
epnumEndpoint number
dirEndpoint direction (IN or OUT)
Returns
Maximum packet size in bytes

◆ GetStatus()

virtual uint16_t dCDC_ACM::GetStatus ( eReqTarget_t target,
uint16_t wIndex,
int * err )
virtual

Get device or endpoint status.

Returns the status for GET_STATUS requests.

Parameters
targetRequest target (device, interface, or endpoint)
wIndexIndex value from the request (interface or endpoint number)
[out]errPointer to receive error code
Returns
Status value, or 0 on error

◆ GetUSBState()

nbrtos::USB::eBusState_t dCDC_ACM::GetUSBState ( )

Get current USB bus state.

Returns the current state of the USB connection.

Returns
Current USB bus state (e.g., configured, suspended, default)
See also
SetUSBState()

◆ Open()

int dCDC_ACM::Open ( )

Open the CDC device for use.

Initializes the CDC device, sets up the file descriptor interface, and prepares the device for data transfer. Must be called before using ReadData(), WriteData(), or the file descriptor.

Returns
Non-negative file descriptor on success, or negative error code
Note
The returned file descriptor can be used with standard I/O functions like read(), write(), and close().

Expand for Example Usage

Examples

Opening and Using as File Descriptor
dCDC_ACM *pCdc; // Previously created
int fd = pCdc->Open();
if (fd >= 0)
{
// Can now use file descriptor I/O
const char *msg = "Hello from NetBurner!\r\n";
write(fd, msg, strlen(msg));
// Or use the class methods directly
pCdc->WriteData((uint8_t *)"Direct write\r\n", 14);
}
int write(int fd, const char *buf, int nbytes)
Write data to the stream associated with a file descriptor (fd). Can be used to write data to stdio,...

◆ Peek()

int dCDC_ACM::Peek ( uint8_t * buf)

Peek at the next byte without removing it.

Returns the next byte in the receive buffer without removing it. Useful for protocols that need to look ahead in the data stream.

Parameters
bufPointer to receive the peeked byte
Returns
1 if byte available, 0 if no data, or negative error code

◆ ProcessBuffer()

virtual int dCDC_ACM::ProcessBuffer ( uint8_t ep,
PoolPtr pp,
uint16_t bufLen )
virtual

Process a received data buffer.

Called by the USB stack when data is received on an endpoint. Copies received data to the receive FIFO and signals waiting readers.

Parameters
epEndpoint number that received data
ppPool pointer to the received buffer
bufLenNumber of bytes received
Returns
0 on success, or negative error code

◆ ProcessSetupPkt()

virtual int dCDC_ACM::ProcessSetupPkt ( uint8_t epnum,
usb_setup_pkt_t msg )
virtual

Process USB setup packet.

Handles USB setup packets received on the control endpoint, including standard device requests and CDC class-specific requests.

Parameters
epnumEndpoint number (typically 0 for control endpoint)
msgUSB setup packet to process
Returns
0 on success, or negative error code

◆ ReadData()

int dCDC_ACM::ReadData ( uint8_t * pData,
int rdLen )

Read data from the CDC device (non-blocking)

Reads available data from the receive buffer. Returns immediately with whatever data is available, up to the requested length.

Parameters
pDataPointer to buffer to receive data
rdLenMaximum number of bytes to read
Returns
Number of bytes read (may be 0 if no data available), or negative error code
See also
ReadWTimeout() for blocking reads with timeout

◆ ReadWTimeout()

int dCDC_ACM::ReadWTimeout ( uint8_t * pData,
int rdLen,
TickTimeout timeout )

Read data from the CDC device with timeout.

Reads data from the receive buffer, waiting up to the specified timeout for data to become available.

Parameters
pDataPointer to buffer to receive data
rdLenMaximum number of bytes to read
timeoutMaximum time to wait for data (in system ticks)
Returns
Number of bytes read, 0 on timeout, or negative error code
See also
ReadData() for non-blocking reads

Expand for Example Usage

Examples

Reading a Line with Timeout
int ReadLine(dCDC_ACM *pCdc, char *buffer, int maxLen, TickTimeout timeout)
{
int index = 0;
uint8_t ch;
while (index < (maxLen - 1))
{
int result = pCdc->ReadWTimeout(&ch, 1, timeout);
if (result <= 0)
{
break; // Timeout or error
}
if (ch == '\n')
{
break; // End of line
}
if (ch != '\r')
{
buffer[index++] = (char)ch;
}
}
buffer[index] = '\0';
return index;
}

◆ SetAltSetting()

virtual int dCDC_ACM::SetAltSetting ( uint16_t wIndex,
uint16_t wValue )
virtual

Set alternate interface setting.

Called by the USB stack when the host selects an alternate setting for an interface.

Parameters
wIndexInterface number
wValueAlternate setting value
Returns
0 on success, or negative error code

◆ SetConfiguration()

virtual int dCDC_ACM::SetConfiguration ( uint16_t wValue)
virtual

Set the active USB configuration.

Called by the USB stack when the host selects a configuration. Initializes endpoints and prepares the device for data transfer.

Parameters
wValueConfiguration value from SET_CONFIGURATION request
Returns
0 on success, or negative error code

◆ SetUSBState()

void dCDC_ACM::SetUSBState ( nbrtos::USB::eBusState_t newState)

Set USB bus state.

Updates the internal USB state. Typically called by event handlers.

Parameters
newStateNew USB bus state to set
See also
GetUSBState()

◆ sProcessBuffer()

static int dCDC_ACM::sProcessBuffer ( Device * dev,
uint8_t ep,
PoolPtr pp,
uint16_t bufLen )
static

Static callback wrapper for ProcessBuffer.

Static member function that dispatches to the instance ProcessBuffer method. Used for registering with the USB stack callback system.

Parameters
devPointer to the device instance
epEndpoint number
ppPool pointer to the buffer
bufLenBuffer length
Returns
Result from ProcessBuffer()

◆ WriteData()

int dCDC_ACM::WriteData ( uint8_t * pData,
int wrLen )

Write data to the CDC device (non-blocking)

Writes data to the transmit buffer. Returns immediately after copying data to the buffer. The actual USB transmission occurs asynchronously.

Parameters
pDataPointer to data to write
wrLenNumber of bytes to write
Returns
Number of bytes written (may be less than requested if buffer space is limited), or negative error code
See also
WriteWTimeout() for blocking writes with timeout

◆ WriteWTimeout()

int dCDC_ACM::WriteWTimeout ( uint8_t * pData,
int wrLen,
TickTimeout timeout )

Write data to the CDC device with timeout.

Writes data to the transmit buffer, waiting up to the specified timeout for buffer space to become available.

Parameters
pDataPointer to data to write
wrLenNumber of bytes to write
timeoutMaximum time to wait for buffer space (in system ticks)
Returns
Number of bytes written, 0 on timeout, or negative error code
See also
WriteData() for non-blocking writes

The documentation for this class was generated from the following file: