17#ifndef __USBD_CDC_NCM_H
18#define __USBD_CDC_NCM_H
25#include "ncm_protocol.h"
34#define NCM_BULK_EP_NUM (1)
35#define NCM_INT_EP_NUM (2)
36#define NCM_BULK_MAX_PACKET_HS (512)
37#define NCM_BULK_MAX_PACKET_FS (64)
38#define NCM_INT_MAX_PACKET (16)
43#define CDC_SEND_ENCAPSULATED_CMD (0x00)
44#define CDC_GET_ENCAPSULATED_RESP (0x01)
49#define NCM_STRING_IDX_MANUFACTURER (1)
50#define NCM_STRING_IDX_PRODUCT (2)
51#define NCM_STRING_IDX_SERIAL (3)
52#define NCM_STRING_IDX_MAC (4)
63class dCDC_NCM :
public nbrtos::USB::Device {
67 uint8_t currentAltSetting;
74 uint32_t ntbInMaxSize;
75 uint32_t ntbOutMaxSize;
80 uint32_t txFramesError;
81 uint32_t rxFramesError;
83 uint32_t txQueueOverflow;
86 uint8_t ntbRxBuf[NCM_MAX_NTB_SIZE] __attribute__((aligned(64)));
87 uint8_t ntbTxBuf[NCM_MAX_NTB_SIZE] __attribute__((aligned(64)));
90 ncm_tx_state_t txState;
95 static const int TX_QUEUE_SIZE = 16;
97 volatile int txQueueHead;
98 volatile int txQueueTail;
99 volatile int txQueueCount;
102 ncm_rx_state_t rxState;
146 bool TxQueueFull()
const {
return txQueueCount >= TX_QUEUE_SIZE; }
161 int BuildNtb16(uint8_t *destBuf, uint8_t *frameData, uint32_t frameLen);
177 dCDC_NCM(nbrtos::USB::Ctlr *parent,
const usb_dev_t *desc,
178 const usb_cfg_t *cfg,
const nbrtos::USB::StrDescTbl *strTbl);
198 bool IsDataReady()
const {
return ncmState == NCM_STATE_CONNECTED; }
241 virtual void Event_BusReset()
override;
242 virtual void Event_Suspend()
override;
243 virtual void Event_Resume()
override;
244 virtual int Event_ControlMsg(nbrtos::USB::bufPtr_t ctlMsg, uint8_t bufLen)
override;
245 virtual int SetConfiguration(uint16_t wValue)
override;
246 virtual int SetAltSetting(uint16_t wIndex, uint16_t wValue)
override;
247 virtual int ProcessBuffer(uint8_t epnum,
PoolPtr pp, uint16_t bufLen)
override;
248 virtual int ProcessSetupPkt(uint8_t epnum, usb_setup_pkt_t msg)
override;
249 virtual uint16_t GetMaxPktSize(uint8_t epnum, eDir_t dir)
override;
250 virtual uint16_t GetStatus(eReqTarget_t target, uint16_t wIndex,
int *err)
override;
251 virtual uint8_t GetActiveConfig(
int *err)
override;
252 virtual uint8_t GetAltSetting(uint8_t intfNum,
int *err)
override;
254 nbrtos::USB::eBusState_t GetUSBState();
255 void SetUSBState(nbrtos::USB::eBusState_t newState);
258 int HandleGetNtbParameters(uint8_t epnum, uint16_t wLength);
259 int HandleSetNtbInputSize(uint8_t *data, uint16_t length);
260 int HandleGetNtbInputSize(uint8_t epnum, uint16_t wLength);
261 int HandleSetEthernetPacketFilter(uint16_t wValue);
264 int TransmitEthernetFrame(
PoolPtr pp);
265 void DeliverToNetworkStack(uint8_t *data, uint16_t len);
268 void ProcessTxStateMachine();
269 void ProcessRxStateMachine();
274 ifNumber = interfaceNum;
289int InitializeUSBNCM(
dCDC_NCM *pNcm);
Network interface configuration block class for interface control and configuration.
Definition netinterface.h:244
Used to store and manipulate MAC addresses.
Definition nettypes.h:69
Network interface block for NCM USB device.
Definition src/ncm_interface.h:30
USB CDC NCM Device Class for Ethernet-over-USB.
Definition src/usbd_cdc_ncm.h:63
int BuildNtb16(uint8_t *destBuf, uint8_t *frameData, uint32_t frameLen)
Build NTB16 for transmission.
uint32_t GetRxFramesError() const
Get RX frame errors.
Definition ueue/src/usbd_cdc_ncm.h:218
uint32_t GetNtbMaxSize() const
Get max NTB size.
Definition ueue/src/usbd_cdc_ncm.h:238
void SetMACAddress(const MACADR &mac)
Set the MAC address for this NCM interface.
int SendNetworkConnectionNotification(bool connected)
Send NETWORK_CONNECTION notification.
uint32_t GetTxFramesOk() const
Get TX frames transmitted successfully.
Definition ueue/src/usbd_cdc_ncm.h:203
dCDC_NCM(nbrtos::USB::Ctlr *parent, const usb_dev_t *desc, const usb_cfg_t *cfg, const nbrtos::USB::StrDescTbl *strTbl)
Constructor.
int GetTxQueueDepth() const
Get current TX queue depth.
Definition ueue/src/usbd_cdc_ncm.h:233
uint32_t GetTxQueueOverflow() const
Get TX queue overflow count.
Definition ueue/src/usbd_cdc_ncm.h:228
bool IsDataReady() const
Check if NCM is ready for data transfer.
Definition ueue/src/usbd_cdc_ncm.h:198
bool TxQueueEmpty() const
Check if TX queue is empty.
Definition ueue/src/usbd_cdc_ncm.h:141
ncm_state_t GetNcmState() const
Get the current NCM state.
Definition ueue/src/usbd_cdc_ncm.h:193
int ParseNtb16(uint8_t *ntb, uint32_t length)
Parse received NTB and extract datagrams.
int SendSpeedChangeNotification()
Send CONNECTION_SPEED_CHANGE notification.
uint32_t GetRxFramesOk() const
Get RX frames received successfully.
Definition ueue/src/usbd_cdc_ncm.h:208
int QueueRx()
Queue a receive operation on the bulk OUT endpoint.
void SetLinkSpeed(uint32_t speedMbps)
Set the link speed in Mbps.
friend void UserMain(void *pd)
Main application entry point.
Definition aes/src/main.cpp:75
uint32_t GetTxFramesError() const
Get TX frame errors.
Definition ueue/src/usbd_cdc_ncm.h:213
bool TxQueueEnqueue(PoolPtr pp)
Add a frame to the TX queue.
Definition ueue/src/usbd_cdc_ncm.cpp:993
void TxStartNextFrame()
Start transmitting the next frame from queue.
Definition ueue/src/usbd_cdc_ncm.cpp:1019
PoolPtr TxQueueDequeue()
Remove a frame from the TX queue.
Definition ueue/src/usbd_cdc_ncm.cpp:1006
bool TxQueueFull() const
Check if TX queue is full.
Definition ueue/src/usbd_cdc_ncm.h:146
uint32_t GetRxNoBuffer() const
Get RX no buffer count.
Definition ueue/src/usbd_cdc_ncm.h:223
int32_t GetInterfaceNumber(InterfaceBlock *pifb)
Returns the Interface Number of the specified network interface InterfaceBlock.
Mutual-exclusion critical section for protecting shared resources between tasks.
Definition nbrtos.h:1391
Main buffer structure for network and serial communication.
Definition buffers.h:90