pic32mx_eth_driver.h
Go to the documentation of this file.
1 /**
2  * @file pic32mx_eth_driver.h
3  * @brief PIC32MX Ethernet MAC driver
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2024 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.4.4
29  **/
30 
31 #ifndef _PIC32MX_ETH_DRIVER_H
32 #define _PIC32MX_ETH_DRIVER_H
33 
34 //Dependencies
35 #include "core/nic.h"
36 
37 //Number of TX buffers
38 #ifndef PIC32MX_ETH_TX_BUFFER_COUNT
39  #define PIC32MX_ETH_TX_BUFFER_COUNT 2
40 #elif (PIC32MX_ETH_TX_BUFFER_COUNT < 1)
41  #error PIC32MX_ETH_TX_BUFFER_COUNT parameter is not valid
42 #endif
43 
44 //TX buffer size
45 #ifndef PIC32MX_ETH_TX_BUFFER_SIZE
46  #define PIC32MX_ETH_TX_BUFFER_SIZE 1536
47 #elif (PIC32MX_ETH_TX_BUFFER_SIZE != 1536)
48  #error PIC32MX_ETH_TX_BUFFER_SIZE parameter is not valid
49 #endif
50 
51 //Number of RX buffers
52 #ifndef PIC32MX_ETH_RX_BUFFER_COUNT
53  #define PIC32MX_ETH_RX_BUFFER_COUNT 4
54 #elif (PIC32MX_ETH_RX_BUFFER_COUNT < 1)
55  #error PIC32MX_ETH_RX_BUFFER_COUNT parameter is not valid
56 #endif
57 
58 //RX buffer size
59 #ifndef PIC32MX_ETH_RX_BUFFER_SIZE
60  #define PIC32MX_ETH_RX_BUFFER_SIZE 1536
61 #elif (PIC32MX_ETH_RX_BUFFER_SIZE != 1536)
62  #error PIC32MX_ETH_RX_BUFFER_SIZE parameter is not valid
63 #endif
64 
65 //Ethernet interrupt priority
66 #ifndef PIC32MX_ETH_IRQ_PRIORITY
67  #define PIC32MX_ETH_IRQ_PRIORITY 2
68 #elif (PIC32MX_ETH_IRQ_PRIORITY < 0)
69  #error PIC32MX_ETH_IRQ_PRIORITY parameter is not valid
70 #endif
71 
72 //Ethernet interrupt subpriority
73 #ifndef PIC32MX_ETH_IRQ_SUB_PRIORITY
74  #define PIC32MX_ETH_IRQ_SUB_PRIORITY 0
75 #elif (PIC32MX_ETH_IRQ_SUB_PRIORITY < 0)
76  #error PIC32MX_ETH_IRQ_SUB_PRIORITY parameter is not valid
77 #endif
78 
79 //EMAC1MCFG register
80 #define _EMAC1MCFG_CLKSEL_DIV4 (0 << _EMAC1MCFG_CLKSEL_POSITION)
81 #define _EMAC1MCFG_CLKSEL_DIV6 (2 << _EMAC1MCFG_CLKSEL_POSITION)
82 #define _EMAC1MCFG_CLKSEL_DIV8 (3 << _EMAC1MCFG_CLKSEL_POSITION)
83 #define _EMAC1MCFG_CLKSEL_DIV10 (4 << _EMAC1MCFG_CLKSEL_POSITION)
84 #define _EMAC1MCFG_CLKSEL_DIV14 (5 << _EMAC1MCFG_CLKSEL_POSITION)
85 #define _EMAC1MCFG_CLKSEL_DIV20 (6 << _EMAC1MCFG_CLKSEL_POSITION)
86 #define _EMAC1MCFG_CLKSEL_DIV28 (7 << _EMAC1MCFG_CLKSEL_POSITION)
87 #define _EMAC1MCFG_CLKSEL_DIV40 (8 << _EMAC1MCFG_CLKSEL_POSITION)
88 
89 //Transmit buffer descriptor flags
90 #define ETH_TX_CTRL_SOP 0x80000000
91 #define ETH_TX_CTRL_EOP 0x40000000
92 #define ETH_TX_CTRL_BYTE_COUNT 0x07FF0000
93 #define ETH_TX_CTRL_NPV 0x00000100
94 #define ETH_TX_CTRL_EOWN 0x00000080
95 #define ETH_TX_STATUS1_VLAN 0x00080000
96 #define ETH_TX_STATUS1_BACKPRESSURE 0x00040000
97 #define ETH_TX_STATUS1_PAUSE 0x00020000
98 #define ETH_TX_STATUS1_CONTROL 0x00010000
99 #define ETH_TX_STATUS1_TOTAL_BYTES 0x0000FFFF
100 #define ETH_TX_STATUS2_UNDERRUN 0x80000000
101 #define ETH_TX_STATUS2_GIANT 0x40000000
102 #define ETH_TX_STATUS2_LATE_COL 0x20000000
103 #define ETH_TX_STATUS2_MAX_COL 0x10000000
104 #define ETH_TX_STATUS2_EXCESSIVE_DEFER 0x08000000
105 #define ETH_TX_STATUS2_PACKET_DEFER 0x04000000
106 #define ETH_TX_STATUS2_BROADCAST 0x02000000
107 #define ETH_TX_STATUS2_MULTICAST 0x01000000
108 #define ETH_TX_STATUS2_DONE 0x00800000
109 #define ETH_TX_STATUS2_LEN_OUT_OF_RANGE 0x00400000
110 #define ETH_TX_STATUS2_LEN_CHECK_ERROR 0x00200000
111 #define ETH_TX_STATUS2_CRC_ERROR 0x00100000
112 #define ETH_TX_STATUS2_COL_COUNT 0x000F0000
113 #define ETH_TX_STATUS2_BYTE_COUNT 0x0000FFFF
114 
115 //Receive buffer descriptor flags
116 #define ETH_RX_CTRL_SOP 0x80000000
117 #define ETH_RX_CTRL_EOP 0x40000000
118 #define ETH_RX_CTRL_BYTE_COUNT 0x07FF0000
119 #define ETH_RX_CTRL_NPV 0x00000100
120 #define ETH_RX_CTRL_EOWN 0x00000080
121 #define ETH_RX_STATUS1_MULTICAST_MATCH 0x80000000
122 #define ETH_RX_STATUS1_BROADCAST_MATCH 0x40000000
123 #define ETH_RX_STATUS1_UNICAST_MATCH 0x20000000
124 #define ETH_RX_STATUS1_PATTERN_MATCH 0x10000000
125 #define ETH_RX_STATUS1_MAGIC_PACKET_MATCH 0x08000000
126 #define ETH_RX_STATUS1_HASH_TABLE_MATCH 0x04000000
127 #define ETH_RX_STATUS1_NOT_MATCH 0x02000000
128 #define ETH_RX_STATUS1_RUNT_PACKET 0x01000000
129 #define ETH_RX_STATUS1_PACKET_CHECKSUM 0x0000FFFF
130 #define ETH_RX_STATUS2_VLAN 0x40000000
131 #define ETH_RX_STATUS2_UNKNOWN_OP_CODE 0x20000000
132 #define ETH_RX_STATUS2_PAUSE 0x10000000
133 #define ETH_RX_STATUS2_CONTROL 0x08000000
134 #define ETH_RX_STATUS2_DRIBBLE_NIBBLE 0x04000000
135 #define ETH_RX_STATUS2_BROADCAST 0x02000000
136 #define ETH_RX_STATUS2_MULTICAST 0x01000000
137 #define ETH_RX_STATUS2_OK 0x00800000
138 #define ETH_RX_STATUS2_LEN_OUT_OF_RANGE 0x00400000
139 #define ETH_RX_STATUS2_LEN_CHECK_ERROR 0x00200000
140 #define ETH_RX_STATUS2_CRC_ERROR 0x00100000
141 #define ETH_RX_STATUS2_CODE_VIOLATION 0x00080000
142 #define ETH_RX_STATUS2_CARRIER_EVENT 0x00040000
143 #define ETH_RX_STATUS2_RXDV_EVENT 0x00020000
144 #define ETH_RX_STATUS2_LONG_EVENT 0x00010000
145 #define ETH_RX_STATUS2_BYTE_COUNT 0x0000FFFF
146 
147 //C++ guard
148 #ifdef __cplusplus
149 extern "C" {
150 #endif
151 
152 
153 /**
154  * @brief TX buffer descriptor
155  **/
156 
157 typedef struct
158 {
159  uint32_t control;
160  uint32_t address;
161  uint32_t status1;
162  uint32_t status2;
163  uint32_t next;
165 
166 
167 /**
168  * @brief RX buffer descriptor
169  **/
170 
171 typedef struct
172 {
173  uint32_t control;
174  uint32_t address;
175  uint32_t status1;
176  uint32_t status2;
177  uint32_t next;
179 
180 
181 //PIC32MX Ethernet MAC driver
182 extern const NicDriver pic32mxEthDriver;
183 
184 //PIC32MX Ethernet MAC related functions
186 void pic32mxEthInitGpio(NetInterface *interface);
187 void pic32mxEthInitBufferDesc(NetInterface *interface);
188 
189 void pic32mxEthTick(NetInterface *interface);
190 
191 void pic32mxEthEnableIrq(NetInterface *interface);
192 void pic32mxEthDisableIrq(NetInterface *interface);
193 void pic32mxEthIrqHandler(void);
194 void pic32mxEthEventHandler(NetInterface *interface);
195 
197  const NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary);
198 
200 
203 
204 void pic32mxEthWritePhyReg(uint8_t opcode, uint8_t phyAddr,
205  uint8_t regAddr, uint16_t data);
206 
207 uint16_t pic32mxEthReadPhyReg(uint8_t opcode, uint8_t phyAddr,
208  uint8_t regAddr);
209 
210 uint32_t pic32mxEthCalcCrc(const void *data, size_t length);
211 
212 //C++ guard
213 #ifdef __cplusplus
214 }
215 #endif
216 
217 #endif
void pic32mxEthEnableIrq(NetInterface *interface)
Enable interrupts.
uint8_t opcode
Definition: dns_common.h:188
Structure describing a buffer that spans multiple chunks.
Definition: net_mem.h:89
uint8_t data[]
Definition: ethernet.h:222
uint32_t pic32mxEthCalcCrc(const void *data, size_t length)
CRC calculation.
error_t
Error codes.
Definition: error.h:43
uint16_t pic32mxEthReadPhyReg(uint8_t opcode, uint8_t phyAddr, uint8_t regAddr)
Read PHY register.
#define NetInterface
Definition: net.h:36
void pic32mxEthInitGpio(NetInterface *interface)
GPIO configuration.
error_t pic32mxEthReceivePacket(NetInterface *interface)
Receive a packet.
#define NetTxAncillary
Definition: net_misc.h:36
void pic32mxEthIrqHandler(void)
PIC32MX Ethernet MAC interrupt service routine.
uint8_t length
Definition: tcp.h:368
error_t pic32mxEthInit(NetInterface *interface)
PIC32MX Ethernet MAC initialization.
error_t pic32mxEthUpdateMacAddrFilter(NetInterface *interface)
Configure MAC address filtering.
void pic32mxEthEventHandler(NetInterface *interface)
PIC32MX Ethernet MAC event handler.
uint16_t regAddr
void pic32mxEthTick(NetInterface *interface)
PIC32MX Ethernet MAC timer handler.
Network interface controller abstraction layer.
error_t pic32mxEthSendPacket(NetInterface *interface, const NetBuffer *buffer, size_t offset, NetTxAncillary *ancillary)
Send a packet.
RX buffer descriptor.
void pic32mxEthDisableIrq(NetInterface *interface)
Disable interrupts.
const NicDriver pic32mxEthDriver
PIC32MX Ethernet MAC driver.
error_t pic32mxEthUpdateMacConfig(NetInterface *interface)
Adjust MAC configuration parameters for proper operation.
TX buffer descriptor.
NIC driver.
Definition: nic.h:286
void pic32mxEthInitBufferDesc(NetInterface *interface)
Initialize DMA descriptor lists.
void pic32mxEthWritePhyReg(uint8_t opcode, uint8_t phyAddr, uint8_t regAddr, uint16_t data)
Write PHY register.