pic32cx_eth_driver.h
Go to the documentation of this file.
1 /**
2  * @file pic32cx_eth_driver.h
3  * @brief PIC32CX SG41/SG60/SG61 Ethernet MAC driver
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2026 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneTCP Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.6.4
29  **/
30 
31 #ifndef _PIC32CX_ETH_DRIVER_H
32 #define _PIC32CX_ETH_DRIVER_H
33 
34 //Dependencies
35 #include "core/nic.h"
36 
37 //Number of TX buffers
38 #ifndef PIC32CX_ETH_TX_BUFFER_COUNT
39  #define PIC32CX_ETH_TX_BUFFER_COUNT 3
40 #elif (PIC32CX_ETH_TX_BUFFER_COUNT < 1)
41  #error PIC32CX_ETH_TX_BUFFER_COUNT parameter is not valid
42 #endif
43 
44 //TX buffer size
45 #ifndef PIC32CX_ETH_TX_BUFFER_SIZE
46  #define PIC32CX_ETH_TX_BUFFER_SIZE 1536
47 #elif (PIC32CX_ETH_TX_BUFFER_SIZE != 1536)
48  #error PIC32CX_ETH_TX_BUFFER_SIZE parameter is not valid
49 #endif
50 
51 //Number of RX buffers
52 #ifndef PIC32CX_ETH_RX_BUFFER_COUNT
53  #define PIC32CX_ETH_RX_BUFFER_COUNT 72
54 #elif (PIC32CX_ETH_RX_BUFFER_COUNT < 12)
55  #error PIC32CX_ETH_RX_BUFFER_COUNT parameter is not valid
56 #endif
57 
58 //RX buffer size
59 #ifndef PIC32CX_ETH_RX_BUFFER_SIZE
60  #define PIC32CX_ETH_RX_BUFFER_SIZE 128
61 #elif (PIC32CX_ETH_RX_BUFFER_SIZE != 128)
62  #error PIC32CX_ETH_RX_BUFFER_SIZE parameter is not valid
63 #endif
64 
65 //Interrupt priority grouping
66 #ifndef PIC32CX_ETH_IRQ_PRIORITY_GROUPING
67  #define PIC32CX_ETH_IRQ_PRIORITY_GROUPING 4
68 #elif (PIC32CX_ETH_IRQ_PRIORITY_GROUPING < 0)
69  #error PIC32CX_ETH_IRQ_PRIORITY_GROUPING parameter is not valid
70 #endif
71 
72 //Ethernet interrupt group priority
73 #ifndef PIC32CX_ETH_IRQ_GROUP_PRIORITY
74  #define PIC32CX_ETH_IRQ_GROUP_PRIORITY 6
75 #elif (PIC32CX_ETH_IRQ_GROUP_PRIORITY < 0)
76  #error PIC32CX_ETH_IRQ_GROUP_PRIORITY parameter is not valid
77 #endif
78 
79 //Ethernet interrupt subpriority
80 #ifndef PIC32CX_ETH_IRQ_SUB_PRIORITY
81  #define PIC32CX_ETH_IRQ_SUB_PRIORITY 0
82 #elif (PIC32CX_ETH_IRQ_SUB_PRIORITY < 0)
83  #error PIC32CX_ETH_IRQ_SUB_PRIORITY parameter is not valid
84 #endif
85 
86 //TX buffer descriptor flags
87 #define GMAC_TX_USED 0x80000000
88 #define GMAC_TX_WRAP 0x40000000
89 #define GMAC_TX_RLE_ERROR 0x20000000
90 #define GMAC_TX_UNDERRUN_ERROR 0x10000000
91 #define GMAC_TX_AHB_ERROR 0x08000000
92 #define GMAC_TX_LATE_COL_ERROR 0x04000000
93 #define GMAC_TX_CHECKSUM_ERROR 0x00700000
94 #define GMAC_TX_NO_CRC 0x00010000
95 #define GMAC_TX_LAST 0x00008000
96 #define GMAC_TX_LENGTH 0x00003FFF
97 
98 //RX buffer descriptor flags
99 #define GMAC_RX_ADDRESS 0xFFFFFFFC
100 #define GMAC_RX_WRAP 0x00000002
101 #define GMAC_RX_OWNERSHIP 0x00000001
102 #define GMAC_RX_BROADCAST 0x80000000
103 #define GMAC_RX_MULTICAST_HASH 0x40000000
104 #define GMAC_RX_UNICAST_HASH 0x20000000
105 #define GMAC_RX_SAR 0x08000000
106 #define GMAC_RX_SAR_MASK 0x06000000
107 #define GMAC_RX_TYPE_ID 0x01000000
108 #define GMAC_RX_SNAP 0x01000000
109 #define GMAC_RX_TYPE_ID_MASK 0x00C00000
110 #define GMAC_RX_CHECKSUM_VALID 0x00C00000
111 #define GMAC_RX_VLAN_TAG 0x00200000
112 #define GMAC_RX_PRIORITY_TAG 0x00100000
113 #define GMAC_RX_VLAN_PRIORITY 0x000E0000
114 #define GMAC_RX_CFI 0x00010000
115 #define GMAC_RX_EOF 0x00008000
116 #define GMAC_RX_SOF 0x00004000
117 #define GMAC_RX_LENGTH_MSB 0x00002000
118 #define GMAC_RX_BAD_FCS 0x00002000
119 #define GMAC_RX_LENGTH 0x00001FFF
120 
121 //Processor-specific definitions
122 #if defined(__PIC32CX2051BZ62132__) || defined(__PIC32CX2051BZ62132__)
123  //GMAC interrupt
124  #define GMAC_IRQn ETH_IRQn
125  #define GMAC_Handler ETH_Handler
126 
127  //GMAC peripheral base address
128  #define GMAC_REGS ETH_REGS
129 
130  //GMAC registers
131  #define GMAC_NCR ETH_NCR
132  #define GMAC_NCFGR ETH_NCFGR
133  #define GMAC_NSR ETH_NSR
134  #define GMAC_TSR ETH_TSR
135  #define GMAC_RBQB ETH_RBQB
136  #define GMAC_TBQB ETH_TBQB
137  #define GMAC_RSR ETH_RSR
138  #define GMAC_ISR ETH_ISR
139  #define GMAC_IER ETH_IER
140  #define GMAC_IDR ETH_IDR
141  #define GMAC_MAN ETH_MAN
142  #define GMAC_HRB ETH_HRB
143  #define GMAC_HRT ETH_HRT
144  #define GMAC_SAB ETH_SAB
145  #define GMAC_SAT ETH_SAT
146 
147  //GMAC_NCR register
148  #define GMAC_NCR_TSTART_Msk ETH_NCR_TSTART_Msk
149  #define GMAC_NCR_MPE_Msk ETH_NCR_MPE_Msk
150  #define GMAC_NCR_TXEN_Msk ETH_NCR_TXEN_Msk
151  #define GMAC_NCR_RXEN_Msk ETH_NCR_RXEN_Msk
152 
153  //GMAC_NCFGR register
154  #define GMAC_NCFGR_DBW ETH_NCFGR_DBW
155  #define GMAC_NCFGR_CLK ETH_NCFGR_CLK
156  #define GMAC_NCFGR_GIGE_Msk ETH_NCFGR_GIGE_Msk
157  #define GMAC_NCFGR_MAXFS_Msk ETH_NCFGR_MAXFS_Msk
158  #define GMAC_NCFGR_UNIHEN_Msk ETH_NCFGR_UNIHEN_Msk
159  #define GMAC_NCFGR_MTIHEN_Msk ETH_NCFGR_MTIHEN_Msk
160  #define GMAC_NCFGR_FD_Msk ETH_NCFGR_FD_Msk
161  #define GMAC_NCFGR_SPD_Msk ETH_NCFGR_SPD_Msk
162 
163  //GMAC_NSR register
164  #define GMAC_NSR_IDLE_Msk ETH_NSR_IDLE_Msk
165 
166  //GMAC_TSR register
167  #define GMAC_TSR_HRESP_Msk ETH_TSR_HRESP_Msk
168  #define GMAC_TSR_UND_Msk ETH_TSR_UND_Msk
169  #define GMAC_TSR_TXCOMP_Msk ETH_TSR_TXCOMP_Msk
170  #define GMAC_TSR_TFC_Msk ETH_TSR_TFC_Msk
171  #define GMAC_TSR_TXGO_Msk ETH_TSR_TXGO_Msk
172  #define GMAC_TSR_RLE_Msk ETH_TSR_RLE_Msk
173  #define GMAC_TSR_COL_Msk ETH_TSR_COL_Msk
174  #define GMAC_TSR_UBR_Msk ETH_TSR_UBR_Msk
175 
176  //GMAC_RSR register
177  #define GMAC_RSR_HNO_Msk ETH_RSR_HNO_Msk
178  #define GMAC_RSR_RXOVR_Msk ETH_RSR_RXOVR_Msk
179  #define GMAC_RSR_REC_Msk ETH_RSR_REC_Msk
180  #define GMAC_RSR_BNA_Msk ETH_RSR_BNA_Msk
181 
182  //GMAC_IER register
183  #define GMAC_IER_HRESP_Msk ETH_IER_HRESP_Msk
184  #define GMAC_IER_ROVR_Msk ETH_IER_ROVR_Msk
185  #define GMAC_IER_TCOMP_Msk ETH_IER_TCOMP_Msk
186  #define GMAC_IER_TFC_Msk ETH_IER_TFC_Msk
187  #define GMAC_IER_RLEX_Msk ETH_IER_RLEX_Msk
188  #define GMAC_IER_TUR_Msk ETH_IER_TUR_Msk
189  #define GMAC_IER_RXUBR_Msk ETH_IER_RXUBR_Msk
190  #define GMAC_IER_RCOMP_Msk ETH_IER_RCOMP_Msk
191 
192  //GMAC_MAN register
193  #define GMAC_MAN_CLTTO_Msk ETH_MAN_CLTTO_Msk
194  #define GMAC_MAN_OP ETH_MAN_OP
195  #define GMAC_MAN_PHYA ETH_MAN_PHYA
196  #define GMAC_MAN_REGA ETH_MAN_REGA
197  #define GMAC_MAN_WTN ETH_MAN_WTN
198  #define GMAC_MAN_DATA ETH_MAN_DATA
199  #define GMAC_MAN_DATA_Msk ETH_MAN_DATA_Msk
200 #endif
201 
202 //C++ guard
203 #ifdef __cplusplus
204 extern "C" {
205 #endif
206 
207 
208 /**
209  * @brief Transmit buffer descriptor
210  **/
211 
212 typedef struct
213 {
214  uint32_t address;
215  uint32_t status;
217 
218 
219 /**
220  * @brief Receive buffer descriptor
221  **/
222 
223 typedef struct
224 {
225  uint32_t address;
226  uint32_t status;
228 
229 
230 //PIC32CX Ethernet MAC driver
231 extern const NicDriver pic32cxEthDriver;
232 
233 //PIC32CX Ethernet MAC related functions
235 void pic32cxEthInitGpio(NetInterface *interface);
236 void pic32cxEthInitBufferDesc(NetInterface *interface);
237 
238 void pic32cxEthTick(NetInterface *interface);
239 
240 void pic32cxEthEnableIrq(NetInterface *interface);
241 void pic32cxEthDisableIrq(NetInterface *interface);
242 void pic32cxEthEventHandler(NetInterface *interface);
243 
245  const NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary);
246 
248 
251 
252 void pic32cxEthWritePhyReg(uint8_t opcode, uint8_t phyAddr,
253  uint8_t regAddr, uint16_t data);
254 
255 uint16_t pic32cxEthReadPhyReg(uint8_t opcode, uint8_t phyAddr,
256  uint8_t regAddr);
257 
258 //C++ guard
259 #ifdef __cplusplus
260 }
261 #endif
262 
263 #endif
void pic32cxEthTick(NetInterface *interface)
PIC32CX Ethernet MAC timer handler.
uint8_t opcode
Definition: dns_common.h:191
uint16_t pic32cxEthReadPhyReg(uint8_t opcode, uint8_t phyAddr, uint8_t regAddr)
Read PHY register.
Structure describing a buffer that spans multiple chunks.
Definition: net_mem.h:89
void pic32cxEthWritePhyReg(uint8_t opcode, uint8_t phyAddr, uint8_t regAddr, uint16_t data)
Write PHY register.
uint8_t data[]
Definition: ethernet.h:224
void pic32cxEthEventHandler(NetInterface *interface)
PIC32CX Ethernet MAC event handler.
const NicDriver pic32cxEthDriver
PIC32CX Ethernet MAC driver.
void pic32cxEthDisableIrq(NetInterface *interface)
Disable interrupts.
error_t pic32cxEthReceivePacket(NetInterface *interface)
Receive a packet.
error_t
Error codes.
Definition: error.h:43
void pic32cxEthInitGpio(NetInterface *interface)
GPIO configuration.
error_t pic32cxEthUpdateMacConfig(NetInterface *interface)
Adjust MAC configuration parameters for proper operation.
#define NetInterface
Definition: net.h:40
void pic32cxEthEnableIrq(NetInterface *interface)
Enable interrupts.
#define NetTxAncillary
Definition: net_misc.h:36
Receive buffer descriptor.
uint16_t regAddr
Network interface controller abstraction layer.
void pic32cxEthInitBufferDesc(NetInterface *interface)
Initialize buffer descriptors.
Transmit buffer descriptor.
error_t pic32cxEthSendPacket(NetInterface *interface, const NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
Send a packet.
error_t pic32cxEthUpdateMacAddrFilter(NetInterface *interface)
Configure MAC address filtering.
NIC driver.
Definition: nic.h:286
error_t pic32cxEthInit(NetInterface *interface)
PIC32CX Ethernet MAC initialization.