2 FreeRTOS V5.4.2 - Copyright (C) 2009 Real Time Engineers Ltd.
\r
4 This file is part of the FreeRTOS distribution.
\r
6 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
7 the terms of the GNU General Public License (version 2) as published by the
\r
8 Free Software Foundation and modified by the FreeRTOS exception.
\r
9 **NOTE** The exception to the GPL is included to allow you to distribute a
\r
10 combined work that includes FreeRTOS without being obliged to provide the
\r
11 source code for proprietary components outside of the FreeRTOS kernel.
\r
12 Alternative commercial license and support terms are also available upon
\r
13 request. See the licensing section of http://www.FreeRTOS.org for full
\r
16 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
\r
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
21 You should have received a copy of the GNU General Public License along
\r
22 with FreeRTOS; if not, write to the Free Software Foundation, Inc., 59
\r
23 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
\r
26 ***************************************************************************
\r
28 * Looking for a quick start? Then check out the FreeRTOS eBook! *
\r
29 * See http://www.FreeRTOS.org/Documentation for details *
\r
31 ***************************************************************************
\r
35 Please ensure to read the configuration and relevant port sections of the
\r
36 online documentation.
\r
38 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
41 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
44 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
45 licensing and training services.
\r
48 /* FreeRTOS includes. */
\r
49 #include "FreeRTOS.h"
\r
54 /* Library includes. */
\r
55 #include "stm32fxxx_eth.h"
\r
56 #include "stm32f10x_gpio.h"
\r
57 #include "stm32f10x_rcc.h"
\r
58 #include "stm32f10x_nvic.h"
\r
60 /*-----------------------------------------------------------*/
\r
62 /* Hardware specifics. */
\r
63 #define uipRCC_MAC_CLOCK ( 1UL << 14UL )
\r
64 #define uipRCC_MAC_TX_CLOCK ( 1UL << 15UL )
\r
65 #define uipRCC_MAC_RX_CLOCK ( 1UL << 16UL )
\r
66 #define uipPHY_ADDRESS ( 1 )
\r
67 #define uipENET_IRQ_NUM ( 61 )
\r
68 #define uipMODE_MII ( 1UL << 23UL )
\r
69 #define uipREMAP_MAC_IO ( 1UL << 21UL )
\r
71 /* The number of descriptors to chain together for use by the Rx DMA. */
\r
72 #define uipNUM_RX_DESCRIPTORS 4
\r
74 /* The total number of buffers to be available. At most (?) there should be
\r
75 one available for each Rx descriptor, one for current use, and one that is
\r
76 in the process of being transmitted. */
\r
77 #define uipNUM_BUFFERS ( uipNUM_RX_DESCRIPTORS + 2 )
\r
79 /* Each buffer is sized to fit an entire Ethernet packet. This is for
\r
80 simplicity and speed, but could waste RAM. */
\r
81 #define uipMAX_PACKET_SIZE 1520
\r
83 /* The field in the descriptor that is unused by this configuration is used to
\r
84 hold the send count. This is just #defined to a meaningful name. */
\r
85 #define SendCount Buffer2NextDescAddr
\r
87 /* If no buffers are available, then wait this long before looking again.... */
\r
88 #define uipBUFFER_WAIT_DELAY ( 3 / portTICK_RATE_MS )
\r
90 /* ...and don't look more than this many times. */
\r
91 #define uipBUFFER_WAIT_ATTEMPTS ( 30 )
\r
93 /* Let the DMA know that a new descriptor has been made available to it. */
\r
94 #define prvRxDescriptorAvailable() ETH_DMA->DMARPDR = 0
\r
96 /*-----------------------------------------------------------*/
\r
99 * Configure the IO for Ethernet use.
\r
101 static void prvSetupEthGPIO( void );
\r
104 * Return a pointer to an unused buffer, marking the returned buffer as now
\r
107 static unsigned char *prvGetNextBuffer( void );
\r
109 /*-----------------------------------------------------------*/
\r
111 /* Allocate the Rx descriptors used by the DMA. */
\r
112 static ETH_DMADESCTypeDef xRxDescriptors[ uipNUM_RX_DESCRIPTORS ] __attribute__((aligned(4)));
\r
114 /* Allocate the descriptor used for transmitting. It might be that better
\r
115 performance could be achieved by having more than one Tx descriptor, but
\r
116 in this simple case only one is used. */
\r
117 static volatile ETH_DMADESCTypeDef xTxDescriptor __attribute__((aligned(4)));
\r
119 /* Buffers used for receiving and transmitting data. */
\r
120 static unsigned char ucMACBuffers[ uipNUM_BUFFERS ][ uipMAX_PACKET_SIZE ] __attribute__((aligned(4)));
\r
122 /* Each ucBufferInUse index corresponds to a position in the same index in the
\r
123 ucMACBuffers array. If the index contains a 1 then the buffer within
\r
124 ucMACBuffers is in use, if it contains a 0 then the buffer is free. */
\r
125 static unsigned char ucBufferInUse[ uipNUM_BUFFERS ] = { 0 };
\r
127 /* Index to the Rx descriptor to inspect next when looking for a received
\r
129 static unsigned long ulNextDescriptor;
\r
131 /* The uip_buffer is not a fixed array, but instead gets pointed to the buffers
\r
132 allocated within this file. */
\r
133 extern unsigned char * uip_buf;
\r
135 /*-----------------------------------------------------------*/
\r
137 portBASE_TYPE xEthInitialise( void )
\r
139 static ETH_InitTypeDef xEthInit; /* Static so as not to take up too much stack space. */
\r
140 NVIC_InitTypeDef xNVICInit;
\r
141 const unsigned char ucMACAddress[] = { configMAC_ADDR0, configMAC_ADDR1, configMAC_ADDR2, configMAC_ADDR3, configMAC_ADDR4, configMAC_ADDR5 };
\r
142 portBASE_TYPE xReturn;
\r
145 /* Start with things in a safe known state. */
\r
147 for( ul = 0; ul < uipNUM_RX_DESCRIPTORS; ul++ )
\r
149 ETH_DMARxDescReceiveITConfig( &( xRxDescriptors[ ul ] ), DISABLE );
\r
152 /* Route clock to the peripheral. */
\r
153 RCC->AHBENR |= ( uipRCC_MAC_CLOCK | uipRCC_MAC_TX_CLOCK | uipRCC_MAC_RX_CLOCK );
\r
155 /* Set the MAC address. */
\r
156 ETH_MACAddressConfig( ETH_MAC_Address0, ( unsigned char * ) ucMACAddress );
\r
158 /* Use MII mode. */
\r
159 AFIO->MAPR &= ~( uipMODE_MII );
\r
161 /* Configure all the GPIO as required for MAC/PHY interfacing. */
\r
164 /* Reset the peripheral. */
\r
165 ETH_SoftwareReset();
\r
166 while( ETH_GetSoftwareResetStatus() == SET );
\r
168 /* Initialise using the whopping big structure. Code space could be saved
\r
169 by making this a const struct, however that would mean changes to the
\r
170 structure within the library header files could break the code, so for now
\r
171 just set everything manually at run time. */
\r
172 xEthInit.ETH_AutoNegotiation = ETH_AutoNegotiation_Enable;
\r
173 xEthInit.ETH_Watchdog = ETH_Watchdog_Disable;
\r
174 xEthInit.ETH_Jabber = ETH_Jabber_Disable;
\r
175 xEthInit.ETH_JumboFrame = ETH_JumboFrame_Disable;
\r
176 xEthInit.ETH_InterFrameGap = ETH_InterFrameGap_96Bit;
\r
177 xEthInit.ETH_CarrierSense = ETH_CarrierSense_Enable;
\r
178 xEthInit.ETH_Speed = ETH_Speed_10M;
\r
179 xEthInit.ETH_ReceiveOwn = ETH_ReceiveOwn_Disable;
\r
180 xEthInit.ETH_LoopbackMode = ETH_LoopbackMode_Disable;
\r
181 xEthInit.ETH_Mode = ETH_Mode_HalfDuplex;
\r
182 xEthInit.ETH_ChecksumOffload = ETH_ChecksumOffload_Disable;
\r
183 xEthInit.ETH_RetryTransmission = ETH_RetryTransmission_Disable;
\r
184 xEthInit.ETH_AutomaticPadCRCStrip = ETH_AutomaticPadCRCStrip_Disable;
\r
185 xEthInit.ETH_BackOffLimit = ETH_BackOffLimit_10;
\r
186 xEthInit.ETH_DeferralCheck = ETH_DeferralCheck_Disable;
\r
187 xEthInit.ETH_ReceiveAll = ETH_ReceiveAll_Enable;
\r
188 xEthInit.ETH_SourceAddrFilter = ETH_SourceAddrFilter_Disable;
\r
189 xEthInit.ETH_PassControlFrames = ETH_PassControlFrames_ForwardPassedAddrFilter;
\r
190 xEthInit.ETH_BroadcastFramesReception = ETH_BroadcastFramesReception_Disable;
\r
191 xEthInit.ETH_DestinationAddrFilter = ETH_DestinationAddrFilter_Normal;
\r
192 xEthInit.ETH_PromiscuousMode = ETH_PromiscuousMode_Disable;
\r
193 xEthInit.ETH_MulticastFramesFilter = ETH_MulticastFramesFilter_Perfect;
\r
194 xEthInit.ETH_UnicastFramesFilter = ETH_UnicastFramesFilter_Perfect;
\r
195 xEthInit.ETH_HashTableHigh = 0x0;
\r
196 xEthInit.ETH_HashTableLow = 0x0;
\r
197 xEthInit.ETH_PauseTime = 0x0;
\r
198 xEthInit.ETH_ZeroQuantaPause = ETH_ZeroQuantaPause_Disable;
\r
199 xEthInit.ETH_PauseLowThreshold = ETH_PauseLowThreshold_Minus4;
\r
200 xEthInit.ETH_UnicastPauseFrameDetect = ETH_UnicastPauseFrameDetect_Disable;
\r
201 xEthInit.ETH_ReceiveFlowControl = ETH_ReceiveFlowControl_Disable;
\r
202 xEthInit.ETH_TransmitFlowControl = ETH_TransmitFlowControl_Disable;
\r
203 xEthInit.ETH_VLANTagComparison = ETH_VLANTagComparison_16Bit;
\r
204 xEthInit.ETH_VLANTagIdentifier = 0x0;
\r
205 xEthInit.ETH_DropTCPIPChecksumErrorFrame = ETH_DropTCPIPChecksumErrorFrame_Disable;
\r
206 xEthInit.ETH_ReceiveStoreForward = ETH_ReceiveStoreForward_Enable;
\r
207 xEthInit.ETH_FlushReceivedFrame = ETH_FlushReceivedFrame_Disable;
\r
208 xEthInit.ETH_TransmitStoreForward = ETH_TransmitStoreForward_Enable;
\r
209 xEthInit.ETH_TransmitThresholdControl = ETH_TransmitThresholdControl_64Bytes;
\r
210 xEthInit.ETH_ForwardErrorFrames = ETH_ForwardErrorFrames_Disable;
\r
211 xEthInit.ETH_ForwardUndersizedGoodFrames = ETH_ForwardUndersizedGoodFrames_Disable;
\r
212 xEthInit.ETH_ReceiveThresholdControl = ETH_ReceiveThresholdControl_64Bytes;
\r
213 xEthInit.ETH_SecondFrameOperate = ETH_SecondFrameOperate_Disable;
\r
214 xEthInit.ETH_AddressAlignedBeats = ETH_AddressAlignedBeats_Enable;
\r
215 xEthInit.ETH_FixedBurst = ETH_FixedBurst_Disable;
\r
216 xEthInit.ETH_RxDMABurstLength = ETH_RxDMABurstLength_1Beat;
\r
217 xEthInit.ETH_TxDMABurstLength = ETH_TxDMABurstLength_1Beat;
\r
218 xEthInit.ETH_DescriptorSkipLength = 0x0;
\r
219 xEthInit.ETH_DMAArbitration = ETH_DMAArbitration_RoundRobin_RxTx_1_1;
\r
221 xReturn = ETH_Init( &xEthInit, uipPHY_ADDRESS );
\r
223 /* Check a link was established. */
\r
224 if( xReturn != pdFAIL )
\r
226 /* Rx and Tx interrupts are used. */
\r
227 ETH_DMAITConfig( ETH_DMA_IT_NIS | ETH_DMA_IT_R | ETH_DMA_IT_T, ENABLE );
\r
229 /* Only a single Tx descriptor is used. For now it is set to use an Rx
\r
230 buffer, but will get updated to point to where ever uip_buf is
\r
231 pointing prior to its use. */
\r
232 ETH_DMATxDescChainInit( ( void * ) &xTxDescriptor, ( void * ) ucMACBuffers, 1 );
\r
233 ETH_DMARxDescChainInit( xRxDescriptors, ( void * ) ucMACBuffers, uipNUM_RX_DESCRIPTORS );
\r
234 for( ul = 0; ul < uipNUM_RX_DESCRIPTORS; ul++ )
\r
236 /* Ensure received data generates an interrupt. */
\r
237 ETH_DMARxDescReceiveITConfig( &( xRxDescriptors[ ul ] ), ENABLE );
\r
239 /* Fix up the addresses used by the descriptors.
\r
240 The way ETH_DMARxDescChainInit() is not compatible with the buffer
\r
241 declarations in this file. */
\r
242 xRxDescriptors[ ul ].Buffer1Addr = ( unsigned long ) &( ucMACBuffers[ ul ][ 0 ] );
\r
244 /* Mark the buffer used by this descriptor as in use. */
\r
245 ucBufferInUse[ ul ] = pdTRUE;
\r
248 /* When receiving data, start at the first descriptor. */
\r
249 ulNextDescriptor = 0;
\r
251 /* Initialise uip_buf to ensure it points somewhere valid. */
\r
252 uip_buf = prvGetNextBuffer();
\r
254 /* SendCount must be initialised to 2 to ensure the Tx descriptor looks
\r
255 as if its available (as if it has already been sent twice. */
\r
256 xTxDescriptor.SendCount = 2;
\r
258 /* Switch on the interrupts in the NVIC. */
\r
259 xNVICInit.NVIC_IRQChannel = uipENET_IRQ_NUM;
\r
260 xNVICInit.NVIC_IRQChannelPreemptionPriority = configLIBRARY_KERNEL_INTERRUPT_PRIORITY;
\r
261 xNVICInit.NVIC_IRQChannelSubPriority = 0;
\r
262 xNVICInit.NVIC_IRQChannelCmd = ENABLE;
\r
263 NVIC_Init( &xNVICInit );
\r
265 /* Buffers and descriptors are all set up, now enable the MAC. */
\r
268 /* Let the DMA know there are Rx descriptors available. */
\r
269 prvRxDescriptorAvailable();
\r
274 /*-----------------------------------------------------------*/
\r
276 static unsigned char *prvGetNextBuffer( void )
\r
279 unsigned char *ucReturn = NULL;
\r
280 unsigned long ulAttempts = 0;
\r
282 while( ucReturn == NULL )
\r
284 /* Look through the buffers to find one that is not in use by
\r
286 for( x = 0; x < uipNUM_BUFFERS; x++ )
\r
288 if( ucBufferInUse[ x ] == pdFALSE )
\r
290 ucBufferInUse[ x ] = pdTRUE;
\r
291 ucReturn = &( ucMACBuffers[ x ][ 0 ] );
\r
296 /* Was a buffer found? */
\r
297 if( ucReturn == NULL )
\r
301 if( ulAttempts >= uipBUFFER_WAIT_ATTEMPTS )
\r
306 /* Wait then look again. */
\r
307 vTaskDelay( uipBUFFER_WAIT_DELAY );
\r
313 /*-----------------------------------------------------------*/
\r
315 unsigned short usGetMACRxData( void )
\r
317 unsigned short usReturn;
\r
319 if( ( xRxDescriptors[ ulNextDescriptor ].Status & ETH_DMARxDesc_ES ) != 0 )
\r
321 /* Error in Rx. Discard the frame and give it back to the DMA. */
\r
322 xRxDescriptors[ ulNextDescriptor ].Status = ETH_DMARxDesc_OWN;
\r
323 prvRxDescriptorAvailable();
\r
325 /* No data to return. */
\r
328 /* Start from the next descriptor the next time this function is called. */
\r
329 ulNextDescriptor++;
\r
330 if( ulNextDescriptor >= uipNUM_RX_DESCRIPTORS )
\r
332 ulNextDescriptor = 0UL;
\r
335 else if( ( xRxDescriptors[ ulNextDescriptor ].Status & ETH_DMARxDesc_OWN ) == 0 )
\r
337 /* Mark the current buffer as free as uip_buf is going to be set to
\r
338 the buffer that contains the received data. */
\r
339 vReturnBuffer( uip_buf );
\r
341 /* Get the received data length from the top 2 bytes of the Status
\r
342 word and the data itself. */
\r
343 usReturn = ( unsigned short ) ( ( xRxDescriptors[ ulNextDescriptor ].Status & ETH_DMARxDesc_FL ) >> 16UL );
\r
344 uip_buf = ( unsigned char * ) ( xRxDescriptors[ ulNextDescriptor ].Buffer1Addr );
\r
346 /* Allocate a new buffer to the descriptor. */
\r
347 xRxDescriptors[ ulNextDescriptor ].Buffer1Addr = ( unsigned long ) prvGetNextBuffer();
\r
349 /* Give the descriptor back to the DMA. */
\r
350 xRxDescriptors[ ulNextDescriptor ].Status = ETH_DMARxDesc_OWN;
\r
351 prvRxDescriptorAvailable();
\r
353 /* Start from the next descriptor the next time this function is called. */
\r
354 ulNextDescriptor++;
\r
355 if( ulNextDescriptor >= uipNUM_RX_DESCRIPTORS )
\r
357 ulNextDescriptor = 0UL;
\r
362 /* No received data at all. */
\r
368 /*-----------------------------------------------------------*/
\r
370 void vSendMACData( unsigned short usDataLen )
\r
372 unsigned long ulAttempts = 0UL;
\r
374 /* Check to see if the Tx descriptor is free. The check against <2 is to
\r
375 ensure the buffer has been sent twice and in so doing preventing a race
\r
376 condition with the DMA on the ETH_DMATxDesc_OWN bit. */
\r
377 while( ( xTxDescriptor.SendCount < 2 ) && ( xTxDescriptor.Status & ETH_DMATxDesc_OWN ) == ETH_DMATxDesc_OWN )
\r
379 /* Wait for the Tx descriptor to become available. */
\r
380 vTaskDelay( uipBUFFER_WAIT_DELAY );
\r
383 if( ulAttempts > uipBUFFER_WAIT_ATTEMPTS )
\r
385 /* Something has gone wrong as the Tx descriptor is still in use.
\r
386 Clear it down manually, the data it was sending will probably be
\r
388 xTxDescriptor.Status &= ~ETH_DMATxDesc_OWN;
\r
389 vReturnBuffer( ( unsigned char * ) xTxDescriptor.Buffer1Addr );
\r
394 /* Setup the Tx descriptor for transmission. */
\r
395 xTxDescriptor.SendCount = 0;
\r
396 xTxDescriptor.Buffer1Addr = ( unsigned long ) uip_buf;
\r
397 xTxDescriptor.ControlBufferSize = ( unsigned long ) usDataLen;
\r
398 xTxDescriptor.Status = ETH_DMATxDesc_OWN | ETH_DMATxDesc_LS | ETH_DMATxDesc_FS | ETH_DMATxDesc_TER | ETH_DMATxDesc_TCH | ETH_DMATxDesc_IC;
\r
399 ETH_DMA->DMASR = ETH_DMASR_TBUS;
\r
400 ETH_DMA->DMATPDR = 0;
\r
402 /* uip_buf is being sent by the Tx descriptor. Allocate a new buffer. */
\r
403 uip_buf = prvGetNextBuffer();
\r
405 /*-----------------------------------------------------------*/
\r
407 static void prvSetupEthGPIO( void )
\r
409 GPIO_InitTypeDef xEthInit;
\r
411 /* Remap MAC IO. */
\r
412 AFIO->MAPR |= ( uipREMAP_MAC_IO );
\r
414 /* Set PA2, PA8, PB5, PB8, PB11, PB12, PB13, PC1 and PC2 for Ethernet
\r
416 xEthInit.GPIO_Pin = GPIO_Pin_2;/* | GPIO_Pin_8; This should be set when the 25MHz is generated by MCO. */
\r
417 xEthInit.GPIO_Speed = GPIO_Speed_50MHz;
\r
418 xEthInit.GPIO_Mode = GPIO_Mode_AF_PP;
\r
419 GPIO_Init( GPIOA, &xEthInit );
\r
421 xEthInit.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_8 | GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13; /*5*/
\r
422 GPIO_Init( GPIOB, &xEthInit );
\r
424 xEthInit.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2;
\r
425 GPIO_Init( GPIOC, &xEthInit );
\r
428 /* Configure PA0, PA1, PA3, PB10, PC3, PD8, PD9, PD10, PD11 and PD12 as
\r
430 xEthInit.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_3;
\r
431 xEthInit.GPIO_Mode = GPIO_Mode_IN_FLOATING;
\r
432 GPIO_Init( GPIOA, &xEthInit );
\r
434 xEthInit.GPIO_Pin = GPIO_Pin_10;
\r
435 GPIO_Init( GPIOB, &xEthInit );
\r
437 xEthInit.GPIO_Pin = GPIO_Pin_3;
\r
438 GPIO_Init( GPIOC, &xEthInit );
\r
440 xEthInit.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12;
\r
441 GPIO_Init( GPIOD, &xEthInit );
\r
443 /*-----------------------------------------------------------*/
\r
445 void vReturnBuffer( unsigned char *pucBuffer )
\r
449 /* Mark a buffer as free for use. */
\r
450 for( ul = 0; ul < uipNUM_BUFFERS; ul++ )
\r
452 if( ucMACBuffers[ ul ] == pucBuffer )
\r
454 ucBufferInUse[ ul ] = pdFALSE;
\r
459 /*-----------------------------------------------------------*/
\r
461 void vMAC_ISR( void )
\r
463 unsigned long ulStatus;
\r
464 extern xSemaphoreHandle xEMACSemaphore;
\r
465 long xHigherPriorityTaskWoken = pdFALSE;
\r
467 /* What caused the interrupt? */
\r
468 ulStatus = ETH_DMA->DMASR;
\r
470 /* Clear everything before leaving. */
\r
471 ETH_DMA->DMASR = ulStatus;
\r
473 if( ulStatus & ETH_DMA_IT_R )
\r
475 /* Data was received. Ensure the uIP task is not blocked as data has
\r
477 xSemaphoreGiveFromISR( xEMACSemaphore, &xHigherPriorityTaskWoken );
\r
480 if( ulStatus & ETH_DMA_IT_T )
\r
482 /* Data was transmitted. */
\r
483 if( xTxDescriptor.SendCount == 0 )
\r
486 ( xTxDescriptor.SendCount )++;
\r
488 xTxDescriptor.Status = ETH_DMATxDesc_OWN | ETH_DMATxDesc_LS | ETH_DMATxDesc_FS | ETH_DMATxDesc_TER | ETH_DMATxDesc_TCH | ETH_DMATxDesc_IC;
\r
489 ETH_DMA->DMASR = ETH_DMASR_TBUS;
\r
490 ETH_DMA->DMATPDR = 0;
\r
494 /* The Tx buffer is no longer required. */
\r
495 vReturnBuffer( ( unsigned char * ) xTxDescriptor.Buffer1Addr );
\r
499 /* If xSemaphoreGiveFromISR() unblocked a task, and the unblocked task has
\r
500 a higher priority than the currently executing task, then
\r
501 xHigherPriorityTaskWoken will have been set to pdTRUE and this ISR should
\r
502 return directly to the higher priority unblocked task. */
\r
503 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r