mikroSDK Reference Manual
nic.h
Go to the documentation of this file.
1
31#ifndef _NIC_H
32#define _NIC_H
33
34//Dependencies
35#include "core/net.h"
36
37//Tick interval to handle NIC periodic operations
38#ifndef NIC_TICK_INTERVAL
39 #define NIC_TICK_INTERVAL 1000
40#elif (NIC_TICK_INTERVAL < 10)
41 #error NIC_TICK_INTERVAL parameter is not valid
42#endif
43
44//Maximum duration a write operation may block
45#ifndef NIC_MAX_BLOCKING_TIME
46 #define NIC_MAX_BLOCKING_TIME INFINITE_DELAY
47#elif (NIC_MAX_BLOCKING_TIME < 0)
48 #error NIC_MAX_BLOCKING_TIME parameter is not valid
49#endif
50
51//Size of the NIC driver context
52#ifndef NIC_CONTEXT_SIZE
53 #define NIC_CONTEXT_SIZE 16
54#elif (NIC_CONTEXT_SIZE < 1)
55 #error NIC_CONTEXT_SIZE parameter is not valid
56#endif
57
58//Switch CPU port
59#define SWITCH_CPU_PORT 32
60#define SWITCH_CPU_PORT_MASK 0x80000000
61
62//Serial Management Interface
63#define SMI_SYNC 0xFFFFFFFF
64#define SMI_START 1
65#define SMI_OPCODE_0 0
66#define SMI_OPCODE_WRITE 1
67#define SMI_OPCODE_READ 2
68#define SMI_TA 2
69
70//C++ guard
71#ifdef __cplusplus
72extern "C" {
73#endif
74
75
90
91
96typedef enum
97{
98 NIC_LINK_STATE_DOWN = 0,
99 NIC_LINK_STATE_UP = 1,
100 NIC_LINK_STATE_AUTO = 2
102
103
108typedef enum
109{
110 NIC_LINK_SPEED_UNKNOWN = 0,
111 NIC_LINK_SPEED_10MBPS = 10000000,
112 NIC_LINK_SPEED_100MBPS = 100000000,
113 NIC_LINK_SPEED_1GBPS = 1000000000
115
116
121typedef enum
122{
123 NIC_UNKNOWN_DUPLEX_MODE = 0,
124 NIC_HALF_DUPLEX_MODE = 1,
125 NIC_FULL_DUPLEX_MODE = 2
127
128
133typedef enum
134{
135 SWITCH_PORT_STATE_UNKNOWN = 0,
136 SWITCH_PORT_STATE_DISABLED = 1,
137 SWITCH_PORT_STATE_BLOCKING = 2,
138 SWITCH_PORT_STATE_LISTENING = 3,
139 SWITCH_PORT_STATE_LEARNING = 4,
140 SWITCH_PORT_STATE_FORWARDING = 5
142
143
148typedef struct
149{
150 MacAddr macAddr;
151 uint8_t srcPort;
152 uint32_t destPorts;
153 bool_t override;
155
156
161typedef struct
162{
163 uint16_t vlanId;
164 bool_t valid;
165 uint16_t fid;
166 uint32_t ports;
168
169
170//NIC driver abstraction layer
171typedef error_t (*NicInit)(NetInterface *interface);
172typedef void (*NicTick)(NetInterface *interface);
173typedef void (*NicEnableIrq)(NetInterface *interface);
174typedef void (*NicDisableIrq)(NetInterface *interface);
175typedef void (*NicEventHandler)(NetInterface *interface);
176
177typedef error_t (*NicSendPacket)(NetInterface *interface,
178 const NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary);
179
180typedef error_t (*NicUpdateMacAddrFilter)(NetInterface *interface);
181typedef error_t (*NicUpdateMacConfig)(NetInterface *interface);
182
183typedef void (*NicWritePhyReg)(uint8_t opcode, uint8_t phyAddr,
184 uint8_t regAddr, uint16_t data);
185
186typedef uint16_t (*NicReadPhyReg)(uint8_t opcode, uint8_t phyAddr,
187 uint8_t regAddr);
188
189//Ethernet PHY driver abstraction layer
190typedef error_t (*PhyInit)(NetInterface *interface);
191typedef void (*PhyTick)(NetInterface *interface);
192typedef void (*PhyEnableIrq)(NetInterface *interface);
193typedef void (*PhyDisableIrq)(NetInterface *interface);
194typedef void (*PhyEventHandler)(NetInterface *interface);
195
196//Ethernet switch driver abstraction layer
197typedef error_t (*SwitchInit)(NetInterface *interface);
198typedef void (*SwitchTick)(NetInterface *interface);
199typedef void (*SwitchEnableIrq)(NetInterface *interface);
200typedef void (*SwitchDisableIrq)(NetInterface *interface);
201typedef void (*SwitchEventHandler)(NetInterface *interface);
202
203typedef error_t (*SwitchTagFrame)(NetInterface *interface, NetBuffer *buffer,
204 size_t *offset, NetTxAncillary *ancillary);
205
206typedef error_t (*SwitchUntagFrame)(NetInterface *interface, uint8_t **frame,
207 size_t *length, NetRxAncillary *ancillary);
208
209typedef bool_t (*SwitchGetLinkState)(NetInterface *interface, uint8_t port);
210typedef uint32_t (*SwitchGetLinkSpeed)(NetInterface *interface, uint8_t port);
211
212typedef NicDuplexMode (*SwitchGetDuplexMode)(NetInterface *interface,
213 uint8_t port);
214
215typedef void (*SwitchSetPortState)(NetInterface *interface, uint8_t port,
216 SwitchPortState state);
217
218typedef SwitchPortState (*SwitchGetPortState)(NetInterface *interface,
219 uint8_t port);
220
221typedef void (*SwitchSetAgingTime)(NetInterface *interface, uint32_t agingTime);
222
223typedef void (*SwitchEnableIgmpSnooping)(NetInterface *interface,
224 bool_t enable);
225
226typedef void (*SwitchEnableMldSnooping)(NetInterface *interface,
227 bool_t enable);
228
229typedef void (*SwitchEnableRsvdMcastTable)(NetInterface *interface,
230 bool_t enable);
231
232typedef error_t (*SwitchAddFdbEntry)(NetInterface *interface,
233 const SwitchFdbEntry *entry);
234
235typedef error_t (*SwitchDeleteFdbEntry)(NetInterface *interface,
236 const SwitchFdbEntry *entry);
237
238typedef error_t (*SwitchGetFdbEntry)(NetInterface *interface, uint_t index,
239 SwitchFdbEntry *entry);
240
241typedef void (*SwitchFlushStaticFdbTable)(NetInterface *interface);
242
243typedef void (*SwitchFlushDynamicFdbTable)(NetInterface *interface,
244 uint8_t port);
245
246typedef void (*SwitchSetUnknownMcastFwdPorts)(NetInterface *interface,
247 bool_t enable, uint32_t forwardPorts);
248
249//SMI driver abstraction layer
250typedef error_t (*SmiInit)(void);
251
252typedef void (*SmiWritePhyReg)(uint8_t opcode, uint8_t phyAddr,
253 uint8_t regAddr, uint16_t data);
254
255typedef uint16_t (*SmiReadPhyReg)(uint8_t opcode, uint8_t phyAddr,
256 uint8_t regAddr);
257
258//SPI driver abstraction layer
259typedef error_t (*SpiInit)(void);
260typedef error_t (*SpiSetMode)(uint_t mode);
261typedef error_t (*SpiSetBitrate)(uint_t bitrate);
262typedef void (*SpiAssertCs)(void);
263typedef void (*SpiDeassertCs)(void);
264typedef uint8_t (*SpiTransfer)(uint8_t data);
265
266//UART driver abstraction layer
267typedef error_t (*UartInit)(void);
268typedef void (*UartEnableIrq)(void);
269typedef void (*UartDisableIrq)(void);
270typedef void (*UartStartTx)(void);
271
272//External interrupt line driver abstraction layer
273typedef error_t (*ExtIntInit)(void);
274typedef void (*ExtIntEnableIrq)(void);
275typedef void (*ExtIntDisableIrq)(void);
276
277
282typedef struct
283{
284 NicType type;
285 size_t mtu;
286 NicInit init;
287 NicTick tick;
288 NicEnableIrq enableIrq;
289 NicDisableIrq disableIrq;
290 NicEventHandler eventHandler;
291 NicSendPacket sendPacket;
292 NicUpdateMacAddrFilter updateMacAddrFilter;
293 NicUpdateMacConfig updateMacConfig;
294 NicWritePhyReg writePhyReg;
295 NicReadPhyReg readPhyReg;
296 bool_t autoPadding;
297 bool_t autoCrcCalc;
298 bool_t autoCrcVerif;
299 bool_t autoCrcStrip;
300} NicDriver;
301
302
307typedef struct
308{
309 PhyInit init;
310 PhyTick tick;
311 PhyEnableIrq enableIrq;
312 PhyDisableIrq disableIrq;
313 PhyEventHandler eventHandler;
314} PhyDriver;
315
316
321typedef struct
322{
323 SwitchInit init;
324 SwitchTick tick;
325 SwitchEnableIrq enableIrq;
326 SwitchDisableIrq disableIrq;
327 SwitchEventHandler eventHandler;
328 SwitchTagFrame tagFrame;
329 SwitchUntagFrame untagFrame;
330 SwitchGetLinkState getLinkState;
331 SwitchGetLinkSpeed getLinkSpeed;
332 SwitchGetDuplexMode getDuplexMode;
333 SwitchSetPortState setPortState;
334 SwitchGetPortState getPortState;
335 SwitchSetAgingTime setAgingTime;
336 SwitchEnableIgmpSnooping enableIgmpSnooping;
337 SwitchEnableMldSnooping enableMldSnooping;
338 SwitchEnableRsvdMcastTable enableRsvdMcastTable;
339 SwitchAddFdbEntry addStaticFdbEntry;
340 SwitchDeleteFdbEntry deleteStaticFdbEntry;
341 SwitchGetFdbEntry getStaticFdbEntry;
342 SwitchFlushStaticFdbTable flushStaticFdbTable;
343 SwitchGetFdbEntry getDynamicFdbEntry;
344 SwitchFlushDynamicFdbTable flushDynamicFdbTable;
345 SwitchSetUnknownMcastFwdPorts setUnknownMcastFwdPorts;
347
348
353typedef struct
354{
355 SmiInit init;
356 SmiWritePhyReg writePhyReg;
357 SmiReadPhyReg readPhyReg;
358} SmiDriver;
359
360
365typedef struct
366{
367 SpiInit init;
368 SpiSetMode setMode;
369 SpiSetBitrate setBitrate;
370 SpiAssertCs assertCs;
371 SpiDeassertCs deassertCs;
372 SpiTransfer transfer;
373} SpiDriver;
374
375
380typedef struct
381{
382 UartInit init;
383 UartEnableIrq enableIrq;
384 UartDisableIrq disableIrq;
385 UartStartTx startTx;
386} UartDriver;
387
388
393typedef struct
394{
395 ExtIntInit init;
396 ExtIntEnableIrq enableIrq;
397 ExtIntDisableIrq disableIrq;
399
400
401//Tick counter to handle periodic operations
402extern systime_t nicTickCounter;
403
404//NIC abstraction layer
405NetInterface *nicGetLogicalInterface(NetInterface *interface);
406NetInterface *nicGetPhysicalInterface(NetInterface *interface);
407uint8_t nicGetSwitchPort(NetInterface *interface);
408uint16_t nicGetVlanId(NetInterface *interface);
409uint16_t nicGetVmanId(NetInterface *interface);
410
411bool_t nicIsParentInterface(NetInterface *interface, NetInterface *parent);
412
413void nicTick(NetInterface *interface);
414
415error_t nicSendPacket(NetInterface *interface, const NetBuffer *buffer,
416 size_t offset, NetTxAncillary *ancillary);
417
418error_t nicUpdateMacAddrFilter(NetInterface *interface);
419
420void nicProcessPacket(NetInterface *interface, uint8_t *packet, size_t length,
421 NetRxAncillary *ancillary);
422
423void nicNotifyLinkChange(NetInterface *interface);
424
425//C++ guard
426#ifdef __cplusplus
427}
428#endif
429
430#endif
error_t
Error codes.
Definition error.h:43
TCP/IP stack core.
NicLinkState
Link state.
Definition nic.h:97
NicType
NIC types.
Definition nic.h:81
@ NIC_TYPE_UNKNOWN
Unknown interface type.
Definition nic.h:82
@ NIC_TYPE_LOOPBACK
Loopback interface.
Definition nic.h:88
@ NIC_TYPE_6LOWPAN
6LoWPAN interface
Definition nic.h:87
@ NIC_TYPE_IPV6
IPv6 interface.
Definition nic.h:86
@ NIC_TYPE_IPV4
IPv4 interface.
Definition nic.h:85
@ NIC_TYPE_ETHERNET
Ethernet interface.
Definition nic.h:83
@ NIC_TYPE_PPP
PPP interface.
Definition nic.h:84
NicDuplexMode
Duplex mode.
Definition nic.h:122
SwitchPortState
Switch port state.
Definition nic.h:134
NicLinkSpeed
Link speed.
Definition nic.h:109
uint32_t systime_t
System time.
Definition os_port_none.h:90
External interrupt line driver.
Definition nic.h:394
Structure describing a buffer that spans multiple chunks.
Definition net_mem.h:89
NIC driver.
Definition nic.h:283
Ethernet PHY driver.
Definition nic.h:308
SMI driver.
Definition nic.h:354
SPI driver.
Definition nic.h:366
Ethernet switch driver.
Definition nic.h:322
Forwarding database entry.
Definition nic.h:149
VLAN entry.
Definition nic.h:162
UART driver.
Definition nic.h:381