nic.h
Go to the documentation of this file.
1 /**
2  * @file nic.h
3  * @brief Network interface controller abstraction layer
4  *
5  * @section License
6  *
7  * Copyright (C) 2010-2018 Oryx Embedded SARL. All rights reserved.
8  *
9  * This file is part of CycloneTCP Open.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24  *
25  * @author Oryx Embedded SARL (www.oryx-embedded.com)
26  * @version 1.9.0
27  **/
28 
29 #ifndef _NIC_H
30 #define _NIC_H
31 
32 //Dependencies
33 #include "core/net.h"
34 
35 //Tick interval to handle NIC periodic operations
36 #ifndef NIC_TICK_INTERVAL
37  #define NIC_TICK_INTERVAL 1000
38 #elif (NIC_TICK_INTERVAL < 10)
39  #error NIC_TICK_INTERVAL parameter is not valid
40 #endif
41 
42 //Maximum duration a write operation may block
43 #ifndef NIC_MAX_BLOCKING_TIME
44  #define NIC_MAX_BLOCKING_TIME INFINITE_DELAY
45 #elif (NIC_MAX_BLOCKING_TIME < 0)
46  #error NIC_MAX_BLOCKING_TIME parameter is not valid
47 #endif
48 
49 //Size of the NIC driver context
50 #ifndef NIC_CONTEXT_SIZE
51  #define NIC_CONTEXT_SIZE 16
52 #elif (NIC_CONTEXT_SIZE < 1)
53  #error NIC_CONTEXT_SIZE parameter is not valid
54 #endif
55 
56 //C++ guard
57 #ifdef __cplusplus
58  extern "C" {
59 #endif
60 
61 
62 /**
63  * @brief NIC types
64  **/
65 
66 typedef enum
67 {
68  NIC_TYPE_UNKNOWN = 0, ///<Unknown interface type
69  NIC_TYPE_ETHERNET = 1, ///<Ethernet interface
70  NIC_TYPE_PPP = 2, ///<PPP interface
71  NIC_TYPE_6LOWPAN = 3 ///<6LoWPAN interface
72 } NicType;
73 
74 
75 /**
76  * @brief Link state
77  **/
78 
79 typedef enum
80 {
84 } NicLinkState;
85 
86 
87 /**
88  * @brief Link speed
89  **/
90 
91 typedef enum
92 {
96  NIC_LINK_SPEED_1GBPS = 1000000000
97 } NicLinkSpeed;
98 
99 
100 /**
101  * @brief Duplex mode
102  **/
103 
104 typedef enum
105 {
109 } NicDuplexMode;
110 
111 
112 //NIC abstraction layer
113 typedef error_t (*NicInit)(NetInterface *interface);
114 typedef void (*NicTick)(NetInterface *interface);
115 typedef void (*NicEnableIrq)(NetInterface *interface);
116 typedef void (*NicDisableIrq)(NetInterface *interface);
117 typedef void (*NicEventHandler)(NetInterface *interface);
118 typedef error_t (*NicSendPacket)(NetInterface *interface, const NetBuffer *buffer, size_t offset);
120 typedef error_t (*NicUpdateMacConfig)(NetInterface *interface);
121 typedef void (*NicWritePhyReg)(uint8_t phyAddr, uint8_t regAddr, uint16_t data);
122 typedef uint16_t (*NicReadPhyReg)(uint8_t phyAddr, uint8_t regAddr);
123 
124 //PHY abstraction layer
125 typedef error_t (*PhyInit)(NetInterface *interface);
126 typedef void (*PhyTick)(NetInterface *interface);
127 typedef void (*PhyEnableIrq)(NetInterface *interface);
128 typedef void (*PhyDisableIrq)(NetInterface *interface);
129 typedef void (*PhyEventHandler)(NetInterface *interface);
130 
131 typedef error_t (*PhyTagFrame)(NetInterface **interface, NetBuffer *buffer,
132  size_t *offset, uint16_t *type);
133 
134 typedef error_t (*PhyUntagFrame)(NetInterface **interface, uint8_t **frame,
135  size_t *length);
136 
137 //SPI abstraction layer
138 typedef error_t (*SpiInit)(void);
140 typedef error_t (*SpiSetBitrate)(uint_t bitrate);
141 typedef void (*SpiAssertCs)(void);
142 typedef void (*SpiDeassertCs)(void);
143 typedef uint8_t (*SpiTransfer)(uint8_t data);
144 
145 //UART abstraction layer
146 typedef error_t (*UartInit)(void);
147 typedef void (*UartEnableIrq)(void);
148 typedef void (*UartDisableIrq)(void);
149 typedef void (*UartStartTx)(void);
150 
151 //External interrupt line abstraction layer
152 typedef error_t (*ExtIntInit)(void);
153 typedef void (*ExtIntEnableIrq)(void);
154 typedef void (*ExtIntDisableIrq)(void);
155 
156 
157 /**
158  * @brief NIC driver
159  **/
160 
161 typedef struct
162 {
164  size_t mtu;
179  //bool_t autoIpv4ChecksumCalc;
180  //bool_t autoIpv4ChecksumVerif;
181  //bool_t autoIpv6ChecksumCalc;
182  //bool_t autoIpv6ChecksumVerif;
183  //bool_t autoIcmpChecksumCalc;
184  //bool_t autoIcmpChecksumVerif;
185  //bool_t autoTcpChecksumCalc;
186  //bool_t autoTcpChecksumVerif;
187  //bool_t autoUdpChecksumCalc;
188  //bool_t autoUdpChecksumVerif;
189 } NicDriver;
190 
191 
192 /**
193  * @brief PHY driver
194  **/
195 
196 typedef struct
197 {
205 } PhyDriver;
206 
207 
208 /**
209  * @brief SPI driver
210  **/
211 
212 typedef struct
213 {
220 } SpiDriver;
221 
222 
223 /**
224  * @brief UART driver
225  **/
226 
227 typedef struct
228 {
233 } UartDriver;
234 
235 
236 /**
237  * @brief External interrupt line driver
238  **/
239 
240 typedef struct
241 {
245 } ExtIntDriver;
246 
247 
248 //Tick counter to handle periodic operations
250 
251 //NIC abstraction layer
254 
255 void nicTick(NetInterface *interface);
256 
257 error_t nicSendPacket(NetInterface *interface, const NetBuffer *buffer, size_t offset);
259 
260 void nicProcessPacket(NetInterface *interface, void *packet, size_t length);
261 void nicNotifyLinkChange(NetInterface *interface);
262 
263 //C++ guard
264 #ifdef __cplusplus
265  }
266 #endif
267 
268 #endif
void(* UartStartTx)(void)
Definition: nic.h:149
NicType type
Definition: nic.h:163
uint32_t systime_t
Definition: compiler_port.h:44
NicEnableIrq enableIrq
Definition: nic.h:167
void(* NicDisableIrq)(NetInterface *interface)
Definition: nic.h:116
void nicTick(NetInterface *interface)
Network controller timer handler.
Definition: nic.c:122
error_t(* PhyUntagFrame)(NetInterface **interface, uint8_t **frame, size_t *length)
Definition: nic.h:134
size_t mtu
Definition: nic.h:164
void nicNotifyLinkChange(NetInterface *interface)
Process link state change event.
Definition: nic.c:298
NicLinkSpeed
Link speed.
Definition: nic.h:91
void(* NicTick)(NetInterface *interface)
Definition: nic.h:114
PhyTagFrame tagFrame
Definition: nic.h:203
void(* ExtIntEnableIrq)(void)
Definition: nic.h:153
void nicProcessPacket(NetInterface *interface, void *packet, size_t length)
Handle a packet received by the network controller.
Definition: nic.c:239
PhyTick tick
Definition: nic.h:199
NicDisableIrq disableIrq
Definition: nic.h:168
ExtIntEnableIrq enableIrq
Definition: nic.h:243
TCP/IP stack core.
error_t(* UartInit)(void)
Definition: nic.h:146
PhyUntagFrame untagFrame
Definition: nic.h:204
error_t(* SpiSetBitrate)(uint_t bitrate)
Definition: nic.h:140
NicDuplexMode
Duplex mode.
Definition: nic.h:104
error_t(* PhyTagFrame)(NetInterface **interface, NetBuffer *buffer, size_t *offset, uint16_t *type)
Definition: nic.h:131
error_t(* NicUpdateMacConfig)(NetInterface *interface)
Definition: nic.h:120
NicLinkState
Link state.
Definition: nic.h:79
PPP interface.
Definition: nic.h:70
UART driver.
Definition: nic.h:227
char_t type
NetInterface * nicGetLogicalInterface(NetInterface *interface)
Retrieve logical interface.
Definition: nic.c:60
void(* UartEnableIrq)(void)
Definition: nic.h:147
PhyDisableIrq disableIrq
Definition: nic.h:201
SpiAssertCs assertCs
Definition: nic.h:217
bool_t autoCrcCalc
Definition: nic.h:176
bool_t autoCrcStrip
Definition: nic.h:178
void(* PhyEnableIrq)(NetInterface *interface)
Definition: nic.h:127
SpiDeassertCs deassertCs
Definition: nic.h:218
void(* SpiAssertCs)(void)
Definition: nic.h:141
error_t(* ExtIntInit)(void)
Definition: nic.h:152
SpiInit init
Definition: nic.h:214
NicReadPhyReg readPhyReg
Definition: nic.h:174
void(* NicEnableIrq)(NetInterface *interface)
Definition: nic.h:115
PHY driver.
Definition: nic.h:196
SpiTransfer transfer
Definition: nic.h:219
PhyEventHandler eventHandler
Definition: nic.h:202
SpiSetBitrate setBitrate
Definition: nic.h:216
SPI driver.
Definition: nic.h:212
void(* PhyDisableIrq)(NetInterface *interface)
Definition: nic.h:128
Unknown interface type.
Definition: nic.h:68
NicEventHandler eventHandler
Definition: nic.h:169
void(* PhyEventHandler)(NetInterface *interface)
Definition: nic.h:129
NIC driver.
Definition: nic.h:161
NicInit init
Definition: nic.h:165
uint16_t(* NicReadPhyReg)(uint8_t phyAddr, uint8_t regAddr)
Definition: nic.h:122
Structure describing a buffer that spans multiple chunks.
Definition: net_mem.h:86
bool_t autoPadding
Definition: nic.h:175
void(* NicEventHandler)(NetInterface *interface)
Definition: nic.h:117
error_t(* NicInit)(NetInterface *interface)
Definition: nic.h:113
void(* NicWritePhyReg)(uint8_t phyAddr, uint8_t regAddr, uint16_t data)
Definition: nic.h:121
void(* SpiDeassertCs)(void)
Definition: nic.h:142
uint16_t regAddr
Ethernet interface.
Definition: nic.h:69
error_t(* NicSendPacket)(NetInterface *interface, const NetBuffer *buffer, size_t offset)
Definition: nic.h:118
error_t
Error codes.
Definition: error.h:40
void(* ExtIntDisableIrq)(void)
Definition: nic.h:154
NicTick tick
Definition: nic.h:166
UartDisableIrq disableIrq
Definition: nic.h:231
void(* PhyTick)(NetInterface *interface)
Definition: nic.h:126
error_t(* PhyInit)(NetInterface *interface)
Definition: nic.h:125
unsigned int uint_t
Definition: compiler_port.h:43
error_t nicSendPacket(NetInterface *interface, const NetBuffer *buffer, size_t offset)
Send a packet to the network controller.
Definition: nic.c:148
void(* UartDisableIrq)(void)
Definition: nic.h:148
uint8_t data[]
Definition: dtls_misc.h:167
PhyInit init
Definition: nic.h:198
systime_t nicTickCounter
Definition: nic.c:51
#define NetInterface
Definition: net.h:34
UartInit init
Definition: nic.h:229
uint8_t mode
Definition: sntp_client.h:143
PhyEnableIrq enableIrq
Definition: nic.h:200
UartStartTx startTx
Definition: nic.h:232
ExtIntDisableIrq disableIrq
Definition: nic.h:244
UartEnableIrq enableIrq
Definition: nic.h:230
NicSendPacket sendPacket
Definition: nic.h:170
error_t nicUpdateMacAddrFilter(NetInterface *interface)
Configure MAC address filtering.
Definition: nic.c:204
NicWritePhyReg writePhyReg
Definition: nic.h:173
6LoWPAN interface
Definition: nic.h:71
uint8_t(* SpiTransfer)(uint8_t data)
Definition: nic.h:143
bool_t autoCrcVerif
Definition: nic.h:177
External interrupt line driver.
Definition: nic.h:240
NicUpdateMacAddrFilter updateMacAddrFilter
Definition: nic.h:171
NetInterface * nicGetPhysicalInterface(NetInterface *interface)
Retrieve physical interface.
Definition: nic.c:85
uint8_t length
Definition: dtls_misc.h:140
NicType
NIC types.
Definition: nic.h:66
error_t(* NicUpdateMacAddrFilter)(NetInterface *interface)
Definition: nic.h:119
error_t(* SpiSetMode)(uint_t mode)
Definition: nic.h:139
int bool_t
Definition: compiler_port.h:47
error_t(* SpiInit)(void)
Definition: nic.h:138
SpiSetMode setMode
Definition: nic.h:215
ExtIntInit init
Definition: nic.h:242
NicUpdateMacConfig updateMacConfig
Definition: nic.h:172