pic32mz_eth_driver.h
Go to the documentation of this file.
1 /**
2  * @file pic32mz_eth_driver.h
3  * @brief PIC32MZ Ethernet MAC driver
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2025 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.5.0
29  **/
30 
31 #ifndef _PIC32MZ_ETH_DRIVER_H
32 #define _PIC32MZ_ETH_DRIVER_H
33 
34 //Dependencies
35 #include "core/nic.h"
36 
37 //Number of TX buffers
38 #ifndef PIC32MZ_ETH_TX_BUFFER_COUNT
39  #define PIC32MZ_ETH_TX_BUFFER_COUNT 3
40 #elif (PIC32MZ_ETH_TX_BUFFER_COUNT < 1)
41  #error PIC32MZ_ETH_TX_BUFFER_COUNT parameter is not valid
42 #endif
43 
44 //TX buffer size
45 #ifndef PIC32MZ_ETH_TX_BUFFER_SIZE
46  #define PIC32MZ_ETH_TX_BUFFER_SIZE 1536
47 #elif (PIC32MZ_ETH_TX_BUFFER_SIZE != 1536)
48  #error PIC32MZ_ETH_TX_BUFFER_SIZE parameter is not valid
49 #endif
50 
51 //Number of RX buffers
52 #ifndef PIC32MZ_ETH_RX_BUFFER_COUNT
53  #define PIC32MZ_ETH_RX_BUFFER_COUNT 6
54 #elif (PIC32MZ_ETH_RX_BUFFER_COUNT < 1)
55  #error PIC32MZ_ETH_RX_BUFFER_COUNT parameter is not valid
56 #endif
57 
58 //RX buffer size
59 #ifndef PIC32MZ_ETH_RX_BUFFER_SIZE
60  #define PIC32MZ_ETH_RX_BUFFER_SIZE 1536
61 #elif (PIC32MZ_ETH_RX_BUFFER_SIZE != 1536)
62  #error PIC32MZ_ETH_RX_BUFFER_SIZE parameter is not valid
63 #endif
64 
65 //Ethernet interrupt priority
66 #ifndef PIC32MZ_ETH_IRQ_PRIORITY
67  #define PIC32MZ_ETH_IRQ_PRIORITY 2
68 #elif (PIC32MZ_ETH_IRQ_PRIORITY < 0)
69  #error PIC32MZ_ETH_IRQ_PRIORITY parameter is not valid
70 #endif
71 
72 //Ethernet interrupt subpriority
73 #ifndef PIC32MZ_ETH_IRQ_SUB_PRIORITY
74  #define PIC32MZ_ETH_IRQ_SUB_PRIORITY 0
75 #elif (PIC32MZ_ETH_IRQ_SUB_PRIORITY < 0)
76  #error PIC32MZ_ETH_IRQ_SUB_PRIORITY parameter is not valid
77 #endif
78 
79 //Processor-specific definitions
80 #if defined(__32MZ1025W104132__) || defined(__32MZ2051W104132__) || \
81  defined(__WFI32E01__) || defined(__WFI32E02__) || defined(__WFI32E03__)
82  //Enable Ethernet interrupts
83  #define ETH_SET_ETHIE() IEC5SET = _IEC5_ETHIE_MASK
84  //Disable Ethernet interrupts
85  #define ETH_CLEAR_ETHIE() IEC5CLR = _IEC5_ETHIE_MASK
86  //Clear Ethernet interrupt flag
87  #define ETH_CLEAR_ETHIF() IFS5CLR = _IFS5_ETHIF_MASK
88 
89  //Set Ethernet interrupt priority
90  #define ETH_SET_ETHIP(n) \
91  IPC41CLR = _IPC41_ETHIP_MASK, \
92  IPC41SET = ((n) << _IPC41_ETHIP_POSITION)
93 
94  //Set Ethernet interrupt subpriority
95  #define ETH_SET_ETHIS(n) \
96  IPC41CLR = _IPC41_ETHIS_MASK, \
97  IPC41SET = ((n) << _IPC41_ETHIS_POSITION)
98 #else
99  //Enable Ethernet interrupts
100  #define ETH_SET_ETHIE() IEC4SET = _IEC4_ETHIE_MASK
101  //Disable Ethernet interrupts
102  #define ETH_CLEAR_ETHIE() IEC4CLR = _IEC4_ETHIE_MASK
103  //Clear Ethernet interrupt flag
104  #define ETH_CLEAR_ETHIF() IFS4CLR = _IFS4_ETHIF_MASK
105 
106  //Set Ethernet interrupt priority
107  #define ETH_SET_ETHIP(n) \
108  IPC38CLR = _IPC38_ETHIP_MASK, \
109  IPC38SET = ((n) << _IPC38_ETHIP_POSITION)
110 
111  //Set Ethernet interrupt subpriority
112  #define ETH_SET_ETHIS(n) \
113  IPC38CLR = _IPC38_ETHIS_MASK, \
114  IPC38SET = ((n) << _IPC38_ETHIS_POSITION)
115 #endif
116 
117 //EMAC1MCFG register
118 #define _EMAC1MCFG_CLKSEL_DIV4 (0 << _EMAC1MCFG_CLKSEL_POSITION)
119 #define _EMAC1MCFG_CLKSEL_DIV6 (2 << _EMAC1MCFG_CLKSEL_POSITION)
120 #define _EMAC1MCFG_CLKSEL_DIV8 (3 << _EMAC1MCFG_CLKSEL_POSITION)
121 #define _EMAC1MCFG_CLKSEL_DIV10 (4 << _EMAC1MCFG_CLKSEL_POSITION)
122 #define _EMAC1MCFG_CLKSEL_DIV14 (5 << _EMAC1MCFG_CLKSEL_POSITION)
123 #define _EMAC1MCFG_CLKSEL_DIV20 (6 << _EMAC1MCFG_CLKSEL_POSITION)
124 #define _EMAC1MCFG_CLKSEL_DIV28 (7 << _EMAC1MCFG_CLKSEL_POSITION)
125 #define _EMAC1MCFG_CLKSEL_DIV40 (8 << _EMAC1MCFG_CLKSEL_POSITION)
126 #define _EMAC1MCFG_CLKSEL_DIV48 (9 << _EMAC1MCFG_CLKSEL_POSITION)
127 #define _EMAC1MCFG_CLKSEL_DIV50 (10 << _EMAC1MCFG_CLKSEL_POSITION)
128 
129 //Transmit buffer descriptor flags
130 #define ETH_TX_CTRL_SOP 0x80000000
131 #define ETH_TX_CTRL_EOP 0x40000000
132 #define ETH_TX_CTRL_BYTE_COUNT 0x07FF0000
133 #define ETH_TX_CTRL_NPV 0x00000100
134 #define ETH_TX_CTRL_EOWN 0x00000080
135 #define ETH_TX_STATUS1_VLAN 0x00080000
136 #define ETH_TX_STATUS1_BACKPRESSURE 0x00040000
137 #define ETH_TX_STATUS1_PAUSE 0x00020000
138 #define ETH_TX_STATUS1_CONTROL 0x00010000
139 #define ETH_TX_STATUS1_TOTAL_BYTES 0x0000FFFF
140 #define ETH_TX_STATUS2_UNDERRUN 0x80000000
141 #define ETH_TX_STATUS2_GIANT 0x40000000
142 #define ETH_TX_STATUS2_LATE_COL 0x20000000
143 #define ETH_TX_STATUS2_MAX_COL 0x10000000
144 #define ETH_TX_STATUS2_EXCESSIVE_DEFER 0x08000000
145 #define ETH_TX_STATUS2_PACKET_DEFER 0x04000000
146 #define ETH_TX_STATUS2_BROADCAST 0x02000000
147 #define ETH_TX_STATUS2_MULTICAST 0x01000000
148 #define ETH_TX_STATUS2_DONE 0x00800000
149 #define ETH_TX_STATUS2_LEN_OUT_OF_RANGE 0x00400000
150 #define ETH_TX_STATUS2_LEN_CHECK_ERROR 0x00200000
151 #define ETH_TX_STATUS2_CRC_ERROR 0x00100000
152 #define ETH_TX_STATUS2_COL_COUNT 0x000F0000
153 #define ETH_TX_STATUS2_BYTE_COUNT 0x0000FFFF
154 
155 //Receive buffer descriptor flags
156 #define ETH_RX_CTRL_SOP 0x80000000
157 #define ETH_RX_CTRL_EOP 0x40000000
158 #define ETH_RX_CTRL_BYTE_COUNT 0x07FF0000
159 #define ETH_RX_CTRL_NPV 0x00000100
160 #define ETH_RX_CTRL_EOWN 0x00000080
161 #define ETH_RX_STATUS1_MULTICAST_MATCH 0x80000000
162 #define ETH_RX_STATUS1_BROADCAST_MATCH 0x40000000
163 #define ETH_RX_STATUS1_UNICAST_MATCH 0x20000000
164 #define ETH_RX_STATUS1_PATTERN_MATCH 0x10000000
165 #define ETH_RX_STATUS1_MAGIC_PACKET_MATCH 0x08000000
166 #define ETH_RX_STATUS1_HASH_TABLE_MATCH 0x04000000
167 #define ETH_RX_STATUS1_NOT_MATCH 0x02000000
168 #define ETH_RX_STATUS1_RUNT_PACKET 0x01000000
169 #define ETH_RX_STATUS1_PACKET_CHECKSUM 0x0000FFFF
170 #define ETH_RX_STATUS2_VLAN 0x40000000
171 #define ETH_RX_STATUS2_UNKNOWN_OP_CODE 0x20000000
172 #define ETH_RX_STATUS2_PAUSE 0x10000000
173 #define ETH_RX_STATUS2_CONTROL 0x08000000
174 #define ETH_RX_STATUS2_DRIBBLE_NIBBLE 0x04000000
175 #define ETH_RX_STATUS2_BROADCAST 0x02000000
176 #define ETH_RX_STATUS2_MULTICAST 0x01000000
177 #define ETH_RX_STATUS2_OK 0x00800000
178 #define ETH_RX_STATUS2_LEN_OUT_OF_RANGE 0x00400000
179 #define ETH_RX_STATUS2_LEN_CHECK_ERROR 0x00200000
180 #define ETH_RX_STATUS2_CRC_ERROR 0x00100000
181 #define ETH_RX_STATUS2_CODE_VIOLATION 0x00080000
182 #define ETH_RX_STATUS2_CARRIER_EVENT 0x00040000
183 #define ETH_RX_STATUS2_RXDV_EVENT 0x00020000
184 #define ETH_RX_STATUS2_LONG_EVENT 0x00010000
185 #define ETH_RX_STATUS2_BYTE_COUNT 0x0000FFFF
186 
187 //C++ guard
188 #ifdef __cplusplus
189 extern "C" {
190 #endif
191 
192 
193 /**
194  * @brief TX buffer descriptor
195  **/
196 
197 typedef struct
198 {
199  uint32_t control;
200  uint32_t address;
201  uint32_t status1;
202  uint32_t status2;
203  uint32_t next;
205 
206 
207 /**
208  * @brief RX buffer descriptor
209  **/
210 
211 typedef struct
212 {
213  uint32_t control;
214  uint32_t address;
215  uint32_t status1;
216  uint32_t status2;
217  uint32_t next;
219 
220 
221 //PIC32MZ Ethernet MAC driver
222 extern const NicDriver pic32mzEthDriver;
223 
224 //PIC32MZ Ethernet MAC related functions
226 void pic32mzEthInitGpio(NetInterface *interface);
227 void pic32mzEthInitBufferDesc(NetInterface *interface);
228 
229 void pic32mzEthTick(NetInterface *interface);
230 
231 void pic32mzEthEnableIrq(NetInterface *interface);
232 void pic32mzEthDisableIrq(NetInterface *interface);
233 void pic32mzEthIrqHandler(void);
234 void pic32mzEthEventHandler(NetInterface *interface);
235 
237  const NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary);
238 
240 
243 
244 void pic32mzEthWritePhyReg(uint8_t opcode, uint8_t phyAddr,
245  uint8_t regAddr, uint16_t data);
246 
247 uint16_t pic32mzEthReadPhyReg(uint8_t opcode, uint8_t phyAddr,
248  uint8_t regAddr);
249 
250 uint32_t pic32mzEthCalcCrc(const void *data, size_t length);
251 
252 //C++ guard
253 #ifdef __cplusplus
254 }
255 #endif
256 
257 #endif
uint8_t opcode
Definition: dns_common.h:188
void pic32mzEthWritePhyReg(uint8_t opcode, uint8_t phyAddr, uint8_t regAddr, uint16_t data)
Write PHY register.
void pic32mzEthEventHandler(NetInterface *interface)
PIC32MZ Ethernet MAC event handler.
Structure describing a buffer that spans multiple chunks.
Definition: net_mem.h:89
uint8_t data[]
Definition: ethernet.h:222
void pic32mzEthInitBufferDesc(NetInterface *interface)
Initialize DMA descriptor lists.
void pic32mzEthInitGpio(NetInterface *interface)
GPIO configuration.
uint16_t pic32mzEthReadPhyReg(uint8_t opcode, uint8_t phyAddr, uint8_t regAddr)
Read PHY register.
void pic32mzEthEnableIrq(NetInterface *interface)
Enable interrupts.
error_t
Error codes.
Definition: error.h:43
error_t pic32mzEthInit(NetInterface *interface)
PIC32MZ Ethernet MAC initialization.
#define NetInterface
Definition: net.h:36
void pic32mzEthIrqHandler(void)
PIC32MZ Ethernet MAC interrupt service routine.
#define NetTxAncillary
Definition: net_misc.h:36
error_t pic32mzEthReceivePacket(NetInterface *interface)
Receive a packet.
uint8_t length
Definition: tcp.h:375
uint16_t regAddr
error_t pic32mzEthUpdateMacConfig(NetInterface *interface)
Adjust MAC configuration parameters for proper operation.
const NicDriver pic32mzEthDriver
PIC32MZ Ethernet MAC driver.
RX buffer descriptor.
Network interface controller abstraction layer.
uint32_t pic32mzEthCalcCrc(const void *data, size_t length)
CRC calculation.
error_t pic32mzEthUpdateMacAddrFilter(NetInterface *interface)
Configure MAC address filtering.
TX buffer descriptor.
error_t pic32mzEthSendPacket(NetInterface *interface, const NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
Send a packet.
void pic32mzEthDisableIrq(NetInterface *interface)
Disable interrupts.
NIC driver.
Definition: nic.h:286
void pic32mzEthTick(NetInterface *interface)
PIC32MZ Ethernet MAC timer handler.