1 |
577 |
jeremybenn |
/********************
|
2 |
|
|
* Original work (C) COPYRIGHT 2006 STMicroelectronics **************************
|
3 |
|
|
* Modifications (C) CopyRight 2006 Richard barry
|
4 |
|
|
* File Name : 91x_enet.c
|
5 |
|
|
* Author : MCD Application Team
|
6 |
|
|
* Date First Issued : May 2006
|
7 |
|
|
* Description : ENET library functions
|
8 |
|
|
********************************************************************************
|
9 |
|
|
* History:
|
10 |
|
|
* May 2006: v1.0
|
11 |
|
|
********************************************************************************
|
12 |
|
|
* THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
|
13 |
|
|
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
|
14 |
|
|
* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
|
15 |
|
|
* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
|
16 |
|
|
* CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
|
17 |
|
|
* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
|
18 |
|
|
*******************************************************************************/
|
19 |
|
|
|
20 |
|
|
|
21 |
|
|
/* Includes ------------------------------------------------------------------*/
|
22 |
|
|
#include "FreeRTOS.h"
|
23 |
|
|
#include "task.h"
|
24 |
|
|
#include "91x_lib.h"
|
25 |
|
|
#include "string.h" //include when using memcpy function
|
26 |
|
|
|
27 |
|
|
/* Include of other module interface headers ---------------------------------*/
|
28 |
|
|
/* Local includes ------------------------------------------------------------*/
|
29 |
|
|
/* Private typedef -----------------------------------------------------------*/
|
30 |
|
|
/* Private define ------------------------------------------------------------*/
|
31 |
|
|
#ifndef NULL
|
32 |
|
|
#define NULL (0)
|
33 |
|
|
#endif
|
34 |
|
|
/* Function return values */
|
35 |
|
|
#define ENET_OK (1)
|
36 |
|
|
#define ENET_NOK (0)
|
37 |
|
|
|
38 |
|
|
/* PHY interface constants. */
|
39 |
|
|
#define STE100P_STATUS_REG 0x01
|
40 |
|
|
#define STE100P_CONTROL_REG 0x00
|
41 |
|
|
#define STE100P_LINK_ABILITY 0x05
|
42 |
|
|
#define STE100P_STATUS_LINKED 0x0004
|
43 |
|
|
#define STE100P_AUTO_NEGOTIATE_ABILITY 0x1000
|
44 |
|
|
#define STE100P_AUTO_NEGOTIATE_COMPLETE 0x20
|
45 |
|
|
#define STE100P_10HALF 0x0020
|
46 |
|
|
#define STE100P_10FULL 0x0040
|
47 |
|
|
#define STE100P_100HALF 0x0080
|
48 |
|
|
#define STE100P_100FULL 0x0100
|
49 |
|
|
#define STE100P_CTRL_FULL 0x0100
|
50 |
|
|
|
51 |
|
|
|
52 |
|
|
/* Private macro -------------------------------------------------------------*/
|
53 |
|
|
/* Private variables ---------------------------------------------------------*/
|
54 |
|
|
#define ENET_NUM_RX_BUFFERS 8
|
55 |
|
|
|
56 |
|
|
static ENET_DMADSCRBase dmaTxDscrBase, dmaRxDscrBase[ ENET_NUM_RX_BUFFERS ];
|
57 |
|
|
static u8 RxBuff[ ENET_NUM_RX_BUFFERS ][ENET_BUFFER_SIZE];
|
58 |
|
|
u8 TxBuff[ENET_BUFFER_SIZE];
|
59 |
|
|
|
60 |
|
|
/* Interface functions -------------------------------------------------------*/
|
61 |
|
|
/* Private functions ---------------------------------------------------------*/
|
62 |
|
|
|
63 |
|
|
/*******************************************************************************
|
64 |
|
|
* Function Name : ENET_SetMACConfig(ENET_MACConfig * MAC_Config)
|
65 |
|
|
* Description : MAC Control Register Configuration
|
66 |
|
|
* Input : MAC_Config structure
|
67 |
|
|
* Output : None
|
68 |
|
|
* Return : None
|
69 |
|
|
*******************************************************************************/
|
70 |
|
|
void ENET_MACControlConfig(ENET_MACConfig *MAC_Config)
|
71 |
|
|
{
|
72 |
|
|
/* ReceiveALL bit */
|
73 |
|
|
if (MAC_Config->ReceiveALL==ENABLE) ENET_MAC->MCR |= MAC_MCR_RA;
|
74 |
|
|
else ENET_MAC->MCR &=~MAC_MCR_RA;
|
75 |
|
|
|
76 |
|
|
/* MIIPrescaler */
|
77 |
|
|
ENET_MAC->MCR &=~(0x3<<24);
|
78 |
|
|
if ((MAC_Config->MIIPrescaler) == MIIPrescaler_2)
|
79 |
|
|
ENET_MAC->MCR |=0x1<<24;
|
80 |
|
|
|
81 |
|
|
/* Loopback mode */
|
82 |
|
|
if (MAC_Config->LoopbackMode==ENABLE)
|
83 |
|
|
{
|
84 |
|
|
ENET_MAC->MCR &=~MAC_MCR_LM;
|
85 |
|
|
ENET_MAC->MCR |=0x1<<21;
|
86 |
|
|
ENET_MAC->MCR &=~MAC_MCR_DRO; /*enable frame reception during transmission*/
|
87 |
|
|
}
|
88 |
|
|
|
89 |
|
|
/* Address filtering mode */
|
90 |
|
|
ENET_MAC->MCR &=~MAC_MCR_AFM;
|
91 |
|
|
ENET_MAC->MCR |= MAC_Config->AddressFilteringMode;
|
92 |
|
|
|
93 |
|
|
/* VLAN Filtering Mode */
|
94 |
|
|
ENET_MAC->MCR |= (MAC_Config->VLANFilteringMode)<<15;
|
95 |
|
|
|
96 |
|
|
/*Wrong Frame Pass */
|
97 |
|
|
if (MAC_Config->PassWrongFrame == ENABLE) ENET_MAC->MCR |=MAC_MCR_PWF;
|
98 |
|
|
else ENET_MAC->MCR &=~MAC_MCR_PWF;
|
99 |
|
|
|
100 |
|
|
/* Late Collision Retransmission*/
|
101 |
|
|
if (MAC_Config->LateCollision == ENABLE) ENET_MAC->MCR |=MAC_MCR_ELC;
|
102 |
|
|
else ENET_MAC->MCR &=~MAC_MCR_ELC;
|
103 |
|
|
|
104 |
|
|
/* Broadcast Frame Reception */
|
105 |
|
|
if (MAC_Config->BroadcastFrameReception == ENABLE) ENET_MAC->MCR &=~MAC_MCR_DBF;
|
106 |
|
|
else ENET_MAC->MCR |=MAC_MCR_DBF;
|
107 |
|
|
|
108 |
|
|
/* PacketRetry */
|
109 |
|
|
if (MAC_Config->PacketRetry == ENABLE) ENET_MAC->MCR &=~MAC_MCR_DPR;
|
110 |
|
|
else ENET_MAC->MCR |=MAC_MCR_DPR;
|
111 |
|
|
|
112 |
|
|
/* RxFrameFiltering */
|
113 |
|
|
if (MAC_Config->RxFrameFiltering == ENABLE) ENET_MAC->MCR |=MAC_MCR_RVFF;
|
114 |
|
|
else ENET_MAC->MCR &=~MAC_MCR_RVFF;
|
115 |
|
|
|
116 |
|
|
/* AutomaticPadRemoval */
|
117 |
|
|
if (MAC_Config->AutomaticPadRemoval == ENABLE) ENET_MAC->MCR |=MAC_MCR_APR;
|
118 |
|
|
else ENET_MAC->MCR &=~MAC_MCR_APR;
|
119 |
|
|
|
120 |
|
|
/* DefferalCheck */
|
121 |
|
|
if (MAC_Config->DeferralCheck == ENABLE) ENET_MAC->MCR |=MAC_MCR_DCE;
|
122 |
|
|
else ENET_MAC->MCR &=~MAC_MCR_DCE;
|
123 |
|
|
|
124 |
|
|
}
|
125 |
|
|
|
126 |
|
|
|
127 |
|
|
|
128 |
|
|
/*******************************************************************************
|
129 |
|
|
* Function Name : ENET_SetOperatingMode
|
130 |
|
|
* Description : Sets the Operating mode
|
131 |
|
|
* Input : ENET_OperatingMode:(see ENET_OperatingMode in 91x_enet.h)
|
132 |
|
|
* Output : None
|
133 |
|
|
* Return : None
|
134 |
|
|
*******************************************************************************/
|
135 |
|
|
portBASE_TYPE ENET_SetOperatingMode( void )
|
136 |
|
|
{
|
137 |
|
|
unsigned long ulStatusReg, ulControlReg, ulLinkAbilityReg;
|
138 |
|
|
|
139 |
|
|
/* Link status is latched, so read twice to get current value */
|
140 |
|
|
ulStatusReg = ENET_MIIReadReg(0, STE100P_STATUS_REG);
|
141 |
|
|
ulStatusReg = ENET_MIIReadReg(0, STE100P_STATUS_REG);
|
142 |
|
|
|
143 |
|
|
if( !( ulStatusReg & STE100P_STATUS_LINKED ) )
|
144 |
|
|
{
|
145 |
|
|
/* No Link. */
|
146 |
|
|
return pdFAIL;
|
147 |
|
|
}
|
148 |
|
|
|
149 |
|
|
ulControlReg = ENET_MIIReadReg(0, STE100P_CONTROL_REG);
|
150 |
|
|
if (ulControlReg & STE100P_AUTO_NEGOTIATE_ABILITY)
|
151 |
|
|
{
|
152 |
|
|
/* AutoNegotiation is enabled. */
|
153 |
|
|
if (!(ulStatusReg & STE100P_AUTO_NEGOTIATE_COMPLETE))
|
154 |
|
|
{
|
155 |
|
|
/* Auto-negotiation in progress. */
|
156 |
|
|
return pdFAIL;
|
157 |
|
|
}
|
158 |
|
|
|
159 |
|
|
ulLinkAbilityReg = ENET_MIIReadReg(0, STE100P_LINK_ABILITY);
|
160 |
|
|
if( ( ulLinkAbilityReg & STE100P_100FULL ) || ( ulLinkAbilityReg & STE100P_10FULL ) )
|
161 |
|
|
{
|
162 |
|
|
ENET_MAC->MCR |=MAC_MCR_FDM; /* full duplex mode */
|
163 |
|
|
ENET_MAC->MCR &=~MAC_MCR_DRO; /* enable frame reception during transmission */
|
164 |
|
|
}
|
165 |
|
|
else
|
166 |
|
|
{
|
167 |
|
|
ENET_MAC->MCR &=~MAC_MCR_FDM; /* half duplex mode */
|
168 |
|
|
ENET_MAC->MCR |=MAC_MCR_DRO; /* disable frame reception during transmission */
|
169 |
|
|
}
|
170 |
|
|
}
|
171 |
|
|
else
|
172 |
|
|
{
|
173 |
|
|
if( ulStatusReg & STE100P_CTRL_FULL )
|
174 |
|
|
{
|
175 |
|
|
ENET_MAC->MCR |=MAC_MCR_FDM; /* full duplex mode */
|
176 |
|
|
ENET_MAC->MCR &=~MAC_MCR_DRO; /* enable frame reception during transmission */
|
177 |
|
|
}
|
178 |
|
|
else
|
179 |
|
|
{
|
180 |
|
|
ENET_MAC->MCR &=~MAC_MCR_FDM; /* half duplex mode */
|
181 |
|
|
ENET_MAC->MCR |=MAC_MCR_DRO; /* disable frame reception during transmission */
|
182 |
|
|
}
|
183 |
|
|
}
|
184 |
|
|
|
185 |
|
|
return pdPASS;
|
186 |
|
|
}
|
187 |
|
|
|
188 |
|
|
/*******************************************************************************
|
189 |
|
|
* Function Name : ENET_MIIWriteReg
|
190 |
|
|
* Description : Writes a value on the PHY registers
|
191 |
|
|
* Input : phyDev PHY device address
|
192 |
|
|
: phyReg PHY register to be written
|
193 |
|
|
* : phyVal PHY register value
|
194 |
|
|
* Output : None
|
195 |
|
|
* Return : None
|
196 |
|
|
*******************************************************************************/
|
197 |
|
|
void ENET_MIIWriteReg (u8 phyDev, u8 phyReg, u32 phyVal)
|
198 |
|
|
{
|
199 |
|
|
|
200 |
|
|
volatile u32 addr;
|
201 |
|
|
volatile u32 res; /* temporary result for address register status */
|
202 |
|
|
volatile u32 timeout;
|
203 |
|
|
|
204 |
|
|
/* Prepare the MII register address */
|
205 |
|
|
addr = 0;
|
206 |
|
|
addr |= ((phyDev<<11) & MAC_MII_ADDR_PHY_ADDR); /* set the PHY address */
|
207 |
|
|
addr |= ((phyReg<<6) & MAC_MII_ADDR_MII_REG); /* select the corresponding register */
|
208 |
|
|
addr |= MAC_MII_ADDR_MII_WRITE; /* in write mode */
|
209 |
|
|
addr |= MAC_MII_ADDR_MII_BUSY;
|
210 |
|
|
|
211 |
|
|
/* Check for the Busy flag */
|
212 |
|
|
timeout=0;
|
213 |
|
|
do
|
214 |
|
|
{
|
215 |
|
|
timeout++;
|
216 |
|
|
res = ENET_MAC->MIIA;
|
217 |
|
|
} while ((res & MAC_MII_ADDR_MII_BUSY) && (timeout < (u32 )MII_WRITE_TO));
|
218 |
|
|
|
219 |
|
|
/* Give the value to the MII data register */
|
220 |
|
|
ENET_MAC->MIID = (phyVal & 0xFFFF);
|
221 |
|
|
|
222 |
|
|
/* write the result value into the MII Address register */
|
223 |
|
|
ENET_MAC->MIIA =addr;
|
224 |
|
|
|
225 |
|
|
/* Check for the Busy flag */
|
226 |
|
|
timeout=0;
|
227 |
|
|
do
|
228 |
|
|
{
|
229 |
|
|
timeout++;
|
230 |
|
|
res = ENET_MAC->MIIA;
|
231 |
|
|
} while ((res & MAC_MII_ADDR_MII_BUSY) && (timeout < (u32 )MII_WRITE_TO));
|
232 |
|
|
|
233 |
|
|
}
|
234 |
|
|
|
235 |
|
|
/*******************************************************************************
|
236 |
|
|
* Function Name : ENET_MIIReadReg
|
237 |
|
|
* Description : Writes a value on the PHY
|
238 |
|
|
* Input : phyDev PHY device address
|
239 |
|
|
* : phyReg PHY register to be read
|
240 |
|
|
* Output : None
|
241 |
|
|
* Return : The read value (16 bits)
|
242 |
|
|
*******************************************************************************/
|
243 |
|
|
u32 ENET_MIIReadReg (u8 phyDev, u32 phyReg )
|
244 |
|
|
{
|
245 |
|
|
|
246 |
|
|
u32 rValue;
|
247 |
|
|
u32 addr;
|
248 |
|
|
u32 res; /* temporary result for address register status */
|
249 |
|
|
u32 timeout; /* timeout value for read process */
|
250 |
|
|
|
251 |
|
|
/* prepare the MII register address */
|
252 |
|
|
addr = 0;
|
253 |
|
|
addr |= ((phyDev<<11) & MAC_MII_ADDR_PHY_ADDR); /* set the PHY address */
|
254 |
|
|
addr |= ((phyReg<<6) & MAC_MII_ADDR_MII_REG); /* select the corresponding register */
|
255 |
|
|
addr &= ~(MAC_MII_ADDR_MII_WRITE); /* ... in read mode */
|
256 |
|
|
addr |= MAC_MII_ADDR_MII_BUSY;
|
257 |
|
|
|
258 |
|
|
/* Check for the Busy flag */
|
259 |
|
|
timeout = 0;
|
260 |
|
|
|
261 |
|
|
do
|
262 |
|
|
{
|
263 |
|
|
timeout++;
|
264 |
|
|
res = ENET_MAC->MIIA;
|
265 |
|
|
} while ((res & MAC_MII_ADDR_MII_BUSY) && (timeout < (u32 )MII_READ_TO));
|
266 |
|
|
|
267 |
|
|
/* write the result value into the MII Address register */
|
268 |
|
|
ENET_MAC->MIIA = addr;
|
269 |
|
|
|
270 |
|
|
/* Check for the Busy flag */
|
271 |
|
|
timeout = 0;
|
272 |
|
|
|
273 |
|
|
do
|
274 |
|
|
{
|
275 |
|
|
timeout++;
|
276 |
|
|
res = ENET_MAC->MIIA;
|
277 |
|
|
} while ((res & MAC_MII_ADDR_MII_BUSY) && (timeout < (u32 )MII_READ_TO));
|
278 |
|
|
|
279 |
|
|
/* read the result value from data register*/
|
280 |
|
|
rValue = ENET_MAC->MIID;
|
281 |
|
|
|
282 |
|
|
return (rValue & 0x0000FFFF);
|
283 |
|
|
}
|
284 |
|
|
|
285 |
|
|
/*******************************************************************************
|
286 |
|
|
* Function Name : ENET_RxDscrInit
|
287 |
|
|
* Description : Initializes the Rx ENET descriptor chain. Single Descriptor
|
288 |
|
|
* Input : None
|
289 |
|
|
* Output : None
|
290 |
|
|
* Return : None
|
291 |
|
|
*******************************************************************************/
|
292 |
|
|
|
293 |
|
|
void ENET_RxDscrInit(void)
|
294 |
|
|
{
|
295 |
|
|
int i;
|
296 |
|
|
|
297 |
|
|
for( i = 0; i < ENET_NUM_RX_BUFFERS; i++ )
|
298 |
|
|
{
|
299 |
|
|
/* Assign temp Rx array to the ENET buffer */
|
300 |
|
|
dmaRxDscrBase[ i ].dmaAddr = (u32)&(RxBuff[ i ][ 0 ]);
|
301 |
|
|
|
302 |
|
|
/* Initialize RX ENET Status and control */
|
303 |
|
|
dmaRxDscrBase[ i ].dmaStatCntl = 0x4000;
|
304 |
|
|
|
305 |
|
|
/* Initialize the next descriptor- In our case its single descriptor */
|
306 |
|
|
dmaRxDscrBase[ i ].dmaNext = (u32)&(dmaRxDscrBase[i+1]) | 0x01;
|
307 |
|
|
|
308 |
|
|
/* Set the max packet size */
|
309 |
|
|
dmaRxDscrBase[ i ].dmaStatCntl = ENET_MAX_PACKET_SIZE | ENET_NEXT_ENABLE;
|
310 |
|
|
|
311 |
|
|
/* Setting the VALID bit */
|
312 |
|
|
dmaRxDscrBase[ i ].dmaPackStatus = DMA_DSCR_RX_STATUS_VALID_MSK;
|
313 |
|
|
}
|
314 |
|
|
|
315 |
|
|
dmaRxDscrBase[ ENET_NUM_RX_BUFFERS - 1 ].dmaNext = (u32)&(dmaRxDscrBase[ 0 ]);
|
316 |
|
|
|
317 |
|
|
/* Setting the RX NEXT Descriptor Register inside the ENET */
|
318 |
|
|
ENET_DMA->RXNDAR = (u32)&(dmaRxDscrBase) | 0x01;
|
319 |
|
|
}
|
320 |
|
|
|
321 |
|
|
/*******************************************************************************
|
322 |
|
|
* Function Name : ENET_TxDscrInit
|
323 |
|
|
* Description : Initializes the Tx ENET descriptor chain with single descriptor
|
324 |
|
|
* Input : None
|
325 |
|
|
* Output : None
|
326 |
|
|
* Return : None
|
327 |
|
|
*******************************************************************************/
|
328 |
|
|
|
329 |
|
|
void ENET_TxDscrInit(void)
|
330 |
|
|
{
|
331 |
|
|
|
332 |
|
|
/* ENET Start Address */
|
333 |
|
|
dmaTxDscrBase.dmaAddr = (u32)TxBuff;
|
334 |
|
|
|
335 |
|
|
/* Next Descriptor Address */
|
336 |
|
|
dmaTxDscrBase.dmaNext = (u32)&(dmaTxDscrBase);
|
337 |
|
|
|
338 |
|
|
/* Initialize ENET status and control */
|
339 |
|
|
dmaTxDscrBase.dmaStatCntl = 0;
|
340 |
|
|
|
341 |
|
|
/* Tx next set to Tx decriptor base */
|
342 |
|
|
ENET_DMA->TXNDAR = (u32)&(dmaTxDscrBase);
|
343 |
|
|
|
344 |
|
|
/* Enable next enable */
|
345 |
|
|
ENET_DMA->TXNDAR |= DMA_DSCR_NXT_NPOL_EN;
|
346 |
|
|
|
347 |
|
|
}
|
348 |
|
|
|
349 |
|
|
/*******************************************************************************
|
350 |
|
|
* Function Name : ENET_Init
|
351 |
|
|
* Description : ENET MAC, PHY and DMA initializations
|
352 |
|
|
* Input : None
|
353 |
|
|
* Output : None
|
354 |
|
|
* Return : None
|
355 |
|
|
*******************************************************************************/
|
356 |
|
|
void ENET_Init ()
|
357 |
|
|
{
|
358 |
|
|
|
359 |
|
|
vu32 regValue;
|
360 |
|
|
ENET_MACConfig *MAC_Config;
|
361 |
|
|
ENET_MACConfig config;
|
362 |
|
|
|
363 |
|
|
/* De-assert the SRESET bit of ENET + MAC devices */
|
364 |
|
|
ENET_DMA->SCR &=~DMA_SCR_SRESET;
|
365 |
|
|
MAC_Config =&config;
|
366 |
|
|
/* Initialize MAC control register with common values */
|
367 |
|
|
MAC_Config->ReceiveALL = DISABLE;
|
368 |
|
|
if (SCU_GetHCLKFreqValue() > 50000)
|
369 |
|
|
MAC_Config->MIIPrescaler = MIIPrescaler_2;
|
370 |
|
|
MAC_Config->LoopbackMode = DISABLE;
|
371 |
|
|
MAC_Config->AddressFilteringMode = MAC_Perfect_Multicast_Perfect;
|
372 |
|
|
MAC_Config->VLANFilteringMode = VLANfilter_VLTAG;
|
373 |
|
|
MAC_Config->PassWrongFrame = DISABLE;
|
374 |
|
|
MAC_Config->LateCollision = DISABLE;
|
375 |
|
|
MAC_Config->BroadcastFrameReception = ENABLE;
|
376 |
|
|
MAC_Config->PacketRetry = ENABLE;
|
377 |
|
|
MAC_Config->RxFrameFiltering = ENABLE;
|
378 |
|
|
MAC_Config->AutomaticPadRemoval = ENABLE;
|
379 |
|
|
MAC_Config->DeferralCheck = ENABLE;
|
380 |
|
|
|
381 |
|
|
/* Configure MAC control register */
|
382 |
|
|
ENET_MACControlConfig(MAC_Config);
|
383 |
|
|
|
384 |
|
|
/* DMA initialization */
|
385 |
|
|
/* Read the ENET DMA Status and Control Register */
|
386 |
|
|
regValue = ENET_DMA->SCR;
|
387 |
|
|
|
388 |
|
|
/* Setup Tx Max burst size */
|
389 |
|
|
regValue &= ~(u32)DMA_SCR_TX_MAX_BURST_SZ;
|
390 |
|
|
regValue |= (u32)DMA_SCR_TX_MAX_BURST_SZ_VAL;
|
391 |
|
|
|
392 |
|
|
/* Setup Rx Max Burst size */
|
393 |
|
|
regValue &= ~(u32)DMA_SCR_RX_MAX_BURST_SZ;
|
394 |
|
|
regValue |= (u32)DMA_SCR_RX_MAX_BURST_SZ_VAL;
|
395 |
|
|
|
396 |
|
|
/* Write Tx & Rx burst size to the ENET status and control register */
|
397 |
|
|
ENET_DMA->SCR = regValue;
|
398 |
|
|
|
399 |
|
|
/* Put the PHY in reset mode */
|
400 |
|
|
ENET_MIIWriteReg(0x0,MAC_MII_REG_XCR, 0x8000);
|
401 |
|
|
|
402 |
|
|
/* Delay to assure PHY reset */
|
403 |
|
|
vTaskDelay( 3000 / portTICK_RATE_MS );
|
404 |
|
|
|
405 |
|
|
/* initialize the opearting mode */
|
406 |
|
|
while( ENET_SetOperatingMode() == pdFAIL )
|
407 |
|
|
{
|
408 |
|
|
vTaskDelay( 3000 / portTICK_RATE_MS );
|
409 |
|
|
}
|
410 |
|
|
|
411 |
|
|
/*set MAC physical*/
|
412 |
|
|
//ENET_MAC->MAH = (MAC_ADDR5<<8) + MAC_ADDR4;
|
413 |
|
|
//ENET_MAC->MAL = (MAC_ADDR3<<24) + (MAC_ADDR2<<16) + (MAC_ADDR1<<8) + MAC_ADDR0;
|
414 |
|
|
|
415 |
|
|
/* Initialize Rx and Tx descriptors in memory */
|
416 |
|
|
ENET_TxDscrInit();
|
417 |
|
|
ENET_RxDscrInit();
|
418 |
|
|
|
419 |
|
|
// What's happening ???
|
420 |
|
|
#ifdef DEBUG
|
421 |
|
|
//int pippo = 1; // Do NOT remove!!!
|
422 |
|
|
#endif
|
423 |
|
|
}
|
424 |
|
|
|
425 |
|
|
/********************************************************************************
|
426 |
|
|
* Function Name : ENET_HandleRxPkt
|
427 |
|
|
* Description : receive a packet and copy it to memory pointed by ppkt.
|
428 |
|
|
* Input : ppkt: pointer on application receive buffer.
|
429 |
|
|
* Output : None
|
430 |
|
|
* Return : ENET_NOK - If there is no packet
|
431 |
|
|
* : ENET_OK - If there is a packet
|
432 |
|
|
*******************************************************************************/
|
433 |
|
|
u32 ENET_HandleRxPkt ( void *ppkt)
|
434 |
|
|
{
|
435 |
|
|
ENET_DMADSCRBase *pDescr;
|
436 |
|
|
u16 size;
|
437 |
|
|
static int iNextRx = 0;
|
438 |
|
|
|
439 |
|
|
if( dmaRxDscrBase[ iNextRx ].dmaPackStatus & DMA_DSCR_RX_STATUS_VALID_MSK )
|
440 |
|
|
{
|
441 |
|
|
return 0;
|
442 |
|
|
}
|
443 |
|
|
|
444 |
|
|
pDescr = &dmaRxDscrBase[ iNextRx ];
|
445 |
|
|
|
446 |
|
|
/*Get the size of the packet*/
|
447 |
|
|
size = ((pDescr->dmaPackStatus & 0x7ff) - 4);
|
448 |
|
|
|
449 |
|
|
//MEMCOPY_L2S_BY4((u8*)ppkt, RxBuff, size); /*optimized memcopy function*/
|
450 |
|
|
memcpy(ppkt, RxBuff[iNextRx], size); //string.h library*/
|
451 |
|
|
|
452 |
|
|
/* Give the buffer back to ENET */
|
453 |
|
|
pDescr->dmaPackStatus = DMA_DSCR_RX_STATUS_VALID_MSK;
|
454 |
|
|
|
455 |
|
|
iNextRx++;
|
456 |
|
|
|
457 |
|
|
if( iNextRx >= ENET_NUM_RX_BUFFERS )
|
458 |
|
|
{
|
459 |
|
|
iNextRx = 0;
|
460 |
|
|
}
|
461 |
|
|
|
462 |
|
|
/* Return no error */
|
463 |
|
|
return size;
|
464 |
|
|
}
|
465 |
|
|
|
466 |
|
|
/*******************************************************************************
|
467 |
|
|
* Function Name : ENET_TxPkt
|
468 |
|
|
* Description : Transmit a packet
|
469 |
|
|
* Input : ppkt: pointer to application packet Buffer
|
470 |
|
|
* : size: Tx Packet size
|
471 |
|
|
* Output : None
|
472 |
|
|
* Return : None
|
473 |
|
|
*******************************************************************************/
|
474 |
|
|
|
475 |
|
|
u8 *pcGetNextBuffer( void )
|
476 |
|
|
{
|
477 |
|
|
if( dmaTxDscrBase.dmaPackStatus & DMA_DSCR_TX_STATUS_VALID_MSK )
|
478 |
|
|
{
|
479 |
|
|
return NULL;
|
480 |
|
|
}
|
481 |
|
|
else
|
482 |
|
|
{
|
483 |
|
|
return ( unsigned char * ) TxBuff;
|
484 |
|
|
}
|
485 |
|
|
}
|
486 |
|
|
|
487 |
|
|
void ENET_TxPkt(void *ppkt, u16 size)
|
488 |
|
|
{
|
489 |
|
|
/* Setting the Frame Length*/
|
490 |
|
|
dmaTxDscrBase.dmaStatCntl = (size&0xFFF);
|
491 |
|
|
|
492 |
|
|
/* Start the ENET by setting the VALID bit in dmaPackStatus of current descr*/
|
493 |
|
|
dmaTxDscrBase.dmaPackStatus = DMA_DSCR_TX_STATUS_VALID_MSK;
|
494 |
|
|
|
495 |
|
|
/* Start the transmit operation */
|
496 |
|
|
ENET_DMA->TXSTR|= DMA_TX_START_FETCH;
|
497 |
|
|
}
|
498 |
|
|
|
499 |
|
|
/*******************************************************************************
|
500 |
|
|
* Function Name : ENET_Start
|
501 |
|
|
* Description : Enables ENET MAC reception / transmission & starts DMA fetch
|
502 |
|
|
* Input : None
|
503 |
|
|
* Output : None
|
504 |
|
|
* Return : None
|
505 |
|
|
*******************************************************************************/
|
506 |
|
|
|
507 |
|
|
void ENET_Start ( void)
|
508 |
|
|
{
|
509 |
|
|
u32 value;
|
510 |
|
|
|
511 |
|
|
/* Force a ENET abort by software for the receive block */
|
512 |
|
|
ENET_DMA->RXSTR &=~ DMA_RX_START_DMA_EN;
|
513 |
|
|
|
514 |
|
|
/* Force a ENET abort by software for the transmit block */
|
515 |
|
|
ENET_DMA->TXSTR &=~DMA_TX_START_DMA_EN;
|
516 |
|
|
|
517 |
|
|
/* Reset all interrupts */
|
518 |
|
|
ENET_DMA->ISR = 0xFFFFFFFF;
|
519 |
|
|
|
520 |
|
|
/* Setup Descriptor Fetch ENET_PhyDelay for Receive Block */
|
521 |
|
|
value = ENET_DMA->RXSTR;
|
522 |
|
|
value &= ~( DMA_RX_START_DFETCH_DLY );
|
523 |
|
|
value |= DMA_RX_START_DFETCH_DEFAULT;
|
524 |
|
|
ENET_DMA->RXSTR= value;
|
525 |
|
|
|
526 |
|
|
/* Setup Descriptor Fetch ENET_PhyDelay for Transmit Block */
|
527 |
|
|
value = ENET_DMA->TXSTR;
|
528 |
|
|
value &= ~( DMA_TX_START_DFETCH_DLY );
|
529 |
|
|
value |= DMA_TX_START_DFETCH_DEFAULT;
|
530 |
|
|
ENET_DMA->TXSTR= value;
|
531 |
|
|
|
532 |
|
|
/* Set Tx underrun bit */
|
533 |
|
|
value &= ~( DMA_TX_START_URUN );
|
534 |
|
|
value |= DMA_TX_START_URUN;
|
535 |
|
|
ENET_DMA->TXSTR = value;
|
536 |
|
|
|
537 |
|
|
/* Clear the interrupts */
|
538 |
|
|
ENET_DMA->IER = 0x0;
|
539 |
|
|
|
540 |
|
|
/* MAC TX enable */
|
541 |
|
|
ENET_MAC->MCR|= MAC_MCR_TE;
|
542 |
|
|
|
543 |
|
|
/* MAC RX enable */
|
544 |
|
|
ENET_MAC->MCR|= MAC_MCR_RE;
|
545 |
|
|
|
546 |
|
|
/* Start the DMA Fetch */
|
547 |
|
|
ENET_DMA->RXSTR|= DMA_RX_START_FETCH;
|
548 |
|
|
}
|
549 |
|
|
|
550 |
|
|
|
551 |
|
|
/*******************************************************************************
|
552 |
|
|
* Function Name : ENET_InitClocksGPIO
|
553 |
|
|
* Description : Reset, clocks & GPIO Ethernet Pin initializations
|
554 |
|
|
* Input : None
|
555 |
|
|
* Output : None
|
556 |
|
|
* Return : None
|
557 |
|
|
*******************************************************************************/
|
558 |
|
|
void ENET_InitClocksGPIO(void)
|
559 |
|
|
{
|
560 |
|
|
|
561 |
|
|
GPIO_InitTypeDef GPIO_Struct;
|
562 |
|
|
|
563 |
|
|
SCU_AHBPeriphClockConfig(__ENET, ENABLE);
|
564 |
|
|
SCU_AHBPeriphReset(__ENET,DISABLE);
|
565 |
|
|
SCU_PHYCLKConfig(ENABLE);
|
566 |
|
|
|
567 |
|
|
GPIO_DeInit(GPIO1);
|
568 |
|
|
GPIO_Struct.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2 |GPIO_Pin_3 |GPIO_Pin_4 |GPIO_Pin_7 ;
|
569 |
|
|
GPIO_Struct.GPIO_Type = GPIO_Type_PushPull;
|
570 |
|
|
GPIO_Struct.GPIO_Direction = GPIO_PinOutput;
|
571 |
|
|
GPIO_Struct.GPIO_IPConnected = GPIO_IPConnected_Disable;
|
572 |
|
|
GPIO_Struct.GPIO_Alternate=GPIO_OutputAlt2;
|
573 |
|
|
GPIO_Init(GPIO1, &GPIO_Struct);
|
574 |
|
|
|
575 |
|
|
|
576 |
|
|
GPIO_DeInit(GPIO5);
|
577 |
|
|
GPIO_Struct.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
|
578 |
|
|
GPIO_Struct.GPIO_Type = GPIO_Type_PushPull;
|
579 |
|
|
GPIO_Struct.GPIO_Direction = GPIO_PinOutput;
|
580 |
|
|
GPIO_Struct.GPIO_IPConnected = GPIO_IPConnected_Disable;
|
581 |
|
|
GPIO_Struct.GPIO_Alternate=GPIO_OutputAlt2;
|
582 |
|
|
GPIO_Init(GPIO5, &GPIO_Struct);
|
583 |
|
|
|
584 |
|
|
}
|
585 |
|
|
|
586 |
|
|
/******************** (C) COPYRIGHT 2006 STMicroelectronics *******************/
|
587 |
|
|
|
588 |
|
|
|