SAMV71 Xplained Ultra Software Package 1.3

gmac_tapdev.c

00001 /* ----------------------------------------------------------------------------
00002  *         SAM Software Package License 
00003  * ----------------------------------------------------------------------------
00004  * Copyright (c) 2013, Atmel Corporation
00005  *
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions are met:
00010  *
00011  * - Redistributions of source code must retain the above copyright notice,
00012  * this list of conditions and the disclaimer below.
00013  *
00014  * Atmel's name may not be used to endorse or promote products derived from
00015  * this software without specific prior written permission.
00016  *
00017  * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00019  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
00020  * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00022  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
00023  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00024  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00025  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
00026  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027  * ----------------------------------------------------------------------------
00028  */
00029 
00030 /*----------------------------------------------------------------------------
00031  *        Headers
00032  *----------------------------------------------------------------------------*/
00033 
00034 #include "board.h"
00035 
00036 #include "uip.h"
00037 #include "uip_arp.h"
00038 #include "string.h"
00039 #include "gmac_tapdev.h"
00040 
00041 /*----------------------------------------------------------------------------
00042  *        Definitions
00043  *----------------------------------------------------------------------------*/
00044 #define TX_BUFFERS         32     /** Must be a power of 2 */
00045 #define RX_BUFFERS         32     /** Must be a power of 2 */
00046 #define BUFFER_SIZE        1536
00047 #define DUMMY_SIZE              2
00048 #define DUMMY_BUFF_SIZE         512
00049 
00050 
00051 #define GMAC_CAF_DISABLE  0
00052 #define GMAC_CAF_ENABLE   1
00053 #define GMAC_NBC_DISABLE  0
00054 #define GMAC_NBC_ENABLE   1
00055 /*----------------------------------------------------------------------------
00056  *        Variables
00057  *----------------------------------------------------------------------------*/
00058 
00059 /* The PINs for GMAC */
00060 static const Pin gmacPins[]   = {BOARD_GMAC_RUN_PINS};
00061 static const Pin gmacResetPin = BOARD_GMAC_RESET_PIN;
00062 
00063 /* The GMAC driver instance */
00064 COMPILER_ALIGNED(32) static sGmacd gGmacd;
00065 
00066 /* The GMACB driver instance */
00067 COMPILER_ALIGNED(32) static GMacb gGmacb;
00068 
00069 /** TX descriptors list */
00070 COMPILER_ALIGNED(32) static sGmacTxDescriptor gTxDs[TX_BUFFERS],  gDummyTxDs[DUMMY_SIZE];
00071 
00072 /** TX callbacks list */
00073 COMPILER_ALIGNED(32) static fGmacdTransferCallback gTxCbs[TX_BUFFERS], gDummyTxCbs[DUMMY_SIZE];
00074 
00075 /** RX descriptors list */
00076 COMPILER_ALIGNED(32) static sGmacRxDescriptor gRxDs[RX_BUFFERS], gDummyRxDs[DUMMY_SIZE];
00077 
00078 /** Send Buffer */
00079 /* Section 3.6 of AMBA 2.0 spec states that burst should not cross 1K Boundaries.
00080    Receive buffer manager writes are burst of 2 words => 3 lsb bits of the address
00081    shall be set to 0 */
00082 COMPILER_ALIGNED(32) static uint8_t pTxBuffer[TX_BUFFERS * BUFFER_SIZE], pTxDummyBuffer[DUMMY_SIZE * DUMMY_BUFF_SIZE];
00083 
00084 /** Receive Buffer */
00085 COMPILER_ALIGNED(32) static uint8_t pRxBuffer[RX_BUFFERS * BUFFER_SIZE], pRxDummyBuffer[DUMMY_SIZE * DUMMY_BUFF_SIZE];
00086 
00087 /* MAC address used for demo */
00088 static uint8_t gGMacAddress[6] = {0x00, 0x45, 0x56, 0x78, 0x9a, 0xbc};
00089 
00090 /*----------------------------------------------------------------------------
00091  *        Exported functions
00092  *----------------------------------------------------------------------------*/
00093 
00094 /**
00095  * Gmac interrupt handler
00096  */
00097 void GMAC_Handler(void)
00098 {
00099     GMACD_Handler(&gGmacd, GMAC_QUE_0);
00100 }
00101 
00102 
00103 /**
00104  * Set the MAC address of the system.
00105  * Should only be called before tapdev_init is called.
00106  */
00107 void gmac_tapdev_setmac(u8_t *addr)
00108 {
00109     gGMacAddress[0] = addr[0];
00110     gGMacAddress[1] = addr[1];
00111     gGMacAddress[2] = addr[2];
00112     gGMacAddress[3] = addr[3];
00113     gGMacAddress[4] = addr[4];
00114     gGMacAddress[5] = addr[5];
00115 }
00116 
00117 /**
00118  * Initialization for GMAC device.
00119  * Should be called at the beginning of the program to set up the
00120  * network interface.
00121  */
00122 void gmac_tapdev_init(void)
00123 {
00124     sGmacd    *pGmacd = &gGmacd;
00125     GMacb      *pGmacb = &gGmacb;
00126     sGmacInit Que0, Que;
00127 //    uint32_t  dwErrCount = 0 ;
00128 
00129     /* Init GMAC driver structure */
00130     memset(&Que0, 0, sizeof(Que0));
00131     Que0.bIsGem = 1;
00132     Que0.bDmaBurstLength = 4;
00133     Que0.pRxBuffer =pRxBuffer;
00134     Que0.pRxD = gRxDs;
00135     Que0.wRxBufferSize = BUFFER_SIZE;
00136     Que0.wRxSize = RX_BUFFERS;
00137     Que0.pTxBuffer = pTxBuffer;
00138     Que0.pTxD = gTxDs;
00139     Que0.wTxBufferSize = BUFFER_SIZE;
00140     Que0.wTxSize = TX_BUFFERS;
00141     Que0.pTxCb = gTxCbs;
00142     
00143 
00144     
00145     memset(&Que, 0, sizeof(Que));
00146     Que.bIsGem = 1;
00147     Que.bDmaBurstLength = 4;
00148     Que.pRxBuffer =pRxDummyBuffer;
00149     Que.pRxD = gDummyRxDs;
00150     Que.wRxBufferSize = DUMMY_BUFF_SIZE;
00151     Que.wRxSize = DUMMY_SIZE;
00152     Que.pTxBuffer = pTxDummyBuffer;
00153     Que.pTxD = gDummyTxDs;
00154     Que.wTxBufferSize = DUMMY_BUFF_SIZE;
00155     Que.wTxSize = DUMMY_SIZE;
00156     Que.pTxCb = gDummyTxCbs;
00157     
00158     /* Init GMAC driver structure */
00159     GMACD_Init(pGmacd, GMAC, ID_GMAC, GMAC_CAF_ENABLE, GMAC_NBC_DISABLE);
00160     GMACD_InitTransfer(pGmacd, &Que, GMAC_QUE_2);
00161     
00162     GMACD_InitTransfer(pGmacd, &Que, GMAC_QUE_1);
00163     
00164     GMACD_InitTransfer(pGmacd, &Que0, GMAC_QUE_0);
00165     GMAC_SetAddress(gGmacd.pHw, 0, gGMacAddress);
00166 
00167     /* Setup GMAC buffers and interrupts */
00168     /* Configure and enable interrupt on RC compare */    
00169     NVIC_ClearPendingIRQ(GMAC_IRQn);
00170     NVIC_EnableIRQ(GMAC_IRQn);
00171     
00172     /* Init GMACB driver */
00173     GMACB_Init(pGmacb, pGmacd, BOARD_GMAC_PHY_ADDR);
00174 
00175     /* PHY initialize */
00176     if (!GMACB_InitPhy(pGmacb, BOARD_MCK,  &gmacResetPin, 1,  gmacPins, PIO_LISTSIZE( gmacPins )))
00177     {
00178         printf( "P: PHY Initialize ERROR!\n\r" ) ;
00179         return;
00180     }
00181 
00182     /* Auto Negotiate, work in RMII mode */
00183     if (!GMACB_AutoNegotiate(pGmacb))
00184     {
00185         printf( "P: Auto Negotiate ERROR!\n\r" ) ;
00186         return;
00187     }
00188     
00189     
00190     printf( "P: Link detected \n\r" ) ;
00191 }
00192 
00193 /**
00194  * Read for GMAC device.
00195  */
00196 uint32_t gmac_tapdev_read( void )
00197 {
00198     uint32_t pkt_len = 0 ;
00199     if ( GMACD_OK != GMACD_Poll( &gGmacd, (uint8_t*)uip_buf, UIP_CONF_BUFFER_SIZE, &pkt_len, GMAC_QUE_0) )
00200     {
00201         pkt_len = 0 ;
00202     }
00203     return pkt_len ;
00204 }
00205 
00206 /**
00207  * Send to GMAC device
00208  */
00209 void gmac_tapdev_send( void )
00210 {
00211     uint8_t gmac_rc ;
00212 
00213     gmac_rc = GMACD_Send( &gGmacd, (void*)uip_buf, uip_len, NULL, GMAC_QUE_0) ;
00214     if ( gmac_rc != GMACD_OK )
00215     {
00216         TRACE_ERROR( "E: Send, rc 0x%x\n\r", gmac_rc ) ;
00217     }
00218 }
00219 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines