NetBurner 3.5.7
PDF Version
ueue/src/usbd_cdc_ncm.h
1
17#ifndef __USBD_CDC_NCM_H
18#define __USBD_CDC_NCM_H
19
20#ifdef SOMRT1061
21#include <usb.h>
22#include <nettypes.h>
23#include <buffers.h>
24#include <nbrtos.h>
25#include "ncm_protocol.h"
26
27// Forward declaration
28class InterfaceBlock;
30
31/*******************************************************************************
32 * USB Endpoint Configuration
33 ******************************************************************************/
34#define NCM_BULK_EP_NUM (1) // Bulk IN/OUT endpoint number
35#define NCM_INT_EP_NUM (2) // Interrupt IN endpoint number
36#define NCM_BULK_MAX_PACKET_HS (512) // High-speed bulk packet size
37#define NCM_BULK_MAX_PACKET_FS (64) // Full-speed bulk packet size
38#define NCM_INT_MAX_PACKET (16) // Interrupt packet size
39
40/*******************************************************************************
41 * CDC Class Requests
42 ******************************************************************************/
43#define CDC_SEND_ENCAPSULATED_CMD (0x00)
44#define CDC_GET_ENCAPSULATED_RESP (0x01)
45
46/*******************************************************************************
47 * USB String Descriptor Indices
48 ******************************************************************************/
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)
53
63class dCDC_NCM : public nbrtos::USB::Device {
64protected:
65 // NCM state
66 ncm_state_t ncmState;
67 uint8_t currentAltSetting;
68
69 // Device identity
70 MACADR macAddress;
71 uint32_t linkSpeed; // in bps (100 Mbps = 100000000)
72
73 // NTB configuration
74 uint32_t ntbInMaxSize; // Max NTB size device will accept
75 uint32_t ntbOutMaxSize; // Max NTB size device will send
76
77 // Statistics
78 uint32_t txFramesOk;
79 uint32_t rxFramesOk;
80 uint32_t txFramesError;
81 uint32_t rxFramesError;
82 uint32_t rxNoBuffer;
83 uint32_t txQueueOverflow; // Frames dropped due to full queue
84
85 // USB buffers (aligned for DMA)
86 uint8_t ntbRxBuf[NCM_MAX_NTB_SIZE] __attribute__((aligned(64)));
87 uint8_t ntbTxBuf[NCM_MAX_NTB_SIZE] __attribute__((aligned(64)));
88
89 // TX state
90 ncm_tx_state_t txState;
91 uint16_t txSequence;
92 PoolPtr txPendingFrame;
93
94 // TX queue for buffering frames when busy
95 static const int TX_QUEUE_SIZE = 16;
96 PoolPtr txQueue[TX_QUEUE_SIZE];
97 volatile int txQueueHead; // Next slot to write
98 volatile int txQueueTail; // Next slot to read
99 volatile int txQueueCount; // Number of frames in queue
100
101 // RX state
102 ncm_rx_state_t rxState;
103 uint32_t rxBufLen;
104
105 // Device state
106 uint32_t devState;
107 int baseEP;
108
109 // Synchronization
110 OS_CRIT ncm_crit;
111
112 // Network interface pointer and number
113 NcmInterfaceBlock *pInterface;
114 int ifNumber;
115
116 // Friend for network interface callbacks
117 friend class NcmInterfaceBlock;
118 friend void UserMain(void *pd);
119
120protected:
124 int QueueRx();
125
130 bool TxQueueEnqueue(PoolPtr pp);
131
137
141 bool TxQueueEmpty() const { return txQueueCount == 0; }
142
146 bool TxQueueFull() const { return txQueueCount >= TX_QUEUE_SIZE; }
147
151 void TxStartNextFrame();
152
156 int ParseNtb16(uint8_t *ntb, uint32_t length);
157
161 int BuildNtb16(uint8_t *destBuf, uint8_t *frameData, uint32_t frameLen);
162
167
172
173public:
177 dCDC_NCM(nbrtos::USB::Ctlr *parent, const usb_dev_t *desc,
178 const usb_cfg_t *cfg, const nbrtos::USB::StrDescTbl *strTbl);
179
183 void SetMACAddress(const MACADR &mac);
184
188 void SetLinkSpeed(uint32_t speedMbps);
189
193 ncm_state_t GetNcmState() const { return ncmState; }
194
198 bool IsDataReady() const { return ncmState == NCM_STATE_CONNECTED; }
199
203 uint32_t GetTxFramesOk() const { return txFramesOk; }
204
208 uint32_t GetRxFramesOk() const { return rxFramesOk; }
209
213 uint32_t GetTxFramesError() const { return txFramesError; }
214
218 uint32_t GetRxFramesError() const { return rxFramesError; }
219
223 uint32_t GetRxNoBuffer() const { return rxNoBuffer; }
224
228 uint32_t GetTxQueueOverflow() const { return txQueueOverflow; }
229
233 int GetTxQueueDepth() const { return txQueueCount; }
234
238 uint32_t GetNtbMaxSize() const { return ntbInMaxSize; }
239
240 // USB Device overrides
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;
253
254 nbrtos::USB::eBusState_t GetUSBState();
255 void SetUSBState(nbrtos::USB::eBusState_t newState);
256
257 // NCM protocol handlers
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);
262
263 // Data transfer
264 int TransmitEthernetFrame(PoolPtr pp);
265 void DeliverToNetworkStack(uint8_t *data, uint16_t len);
266
267 // State machine processing
268 void ProcessTxStateMachine();
269 void ProcessRxStateMachine();
270
271 // Network interface registration
272 void SetNetworkInterface(NcmInterfaceBlock *pIntf, int interfaceNum) {
273 pInterface = pIntf;
274 ifNumber = interfaceNum;
275 }
276 NcmInterfaceBlock *GetNetworkInterface() { return pInterface; }
277 int GetInterfaceNumber() { return ifNumber; }
278};
279
289int InitializeUSBNCM(dCDC_NCM *pNcm);
290
291// Global NCM device instances (USB1 and USB2)
292extern dCDC_NCM usb_ncm1;
293extern dCDC_NCM usb_ncm2;
294
295#endif /* SOMRT1061 */
296#endif /* __USBD_CDC_NCM_H */
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