SAMV71 Xplained Ultra Software Package 1.5

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_SECTION(".ram_nocache")
00071 COMPILER_ALIGNED(32) static sGmacTxDescriptor gTxDs[TX_BUFFERS],  gDummyTxDs[DUMMY_SIZE];
00072 
00073 /** TX callbacks list */
00074 COMPILER_ALIGNED(32) static fGmacdTransferCallback gTxCbs[TX_BUFFERS], gDummyTxCbs[DUMMY_SIZE];
00075 
00076 /** RX descriptors list */
00077 COMPILER_SECTION(".ram_nocache")
00078 COMPILER_ALIGNED(32) static sGmacRxDescriptor gRxDs[RX_BUFFERS], gDummyRxDs[DUMMY_SIZE];
00079 
00080 /** Send Buffer */
00081 /* Section 3.6 of AMBA 2.0 spec states that burst should not cross 1K Boundaries.
00082    Receive buffer manager writes are burst of 2 words => 3 lsb bits of the address
00083    shall be set to 0 */
00084 COMPILER_ALIGNED(32) static uint8_t pTxBuffer[TX_BUFFERS * BUFFER_SIZE], pTxDummyBuffer[DUMMY_SIZE * DUMMY_BUFF_SIZE];
00085 
00086 /** Receive Buffer */
00087 COMPILER_ALIGNED(32) static uint8_t pRxBuffer[RX_BUFFERS * BUFFER_SIZE], pRxDummyBuffer[DUMMY_SIZE * DUMMY_BUFF_SIZE];
00088 
00089 /* MAC address used for demo */
00090 static uint8_t gGMacAddress[6] = {0x00, 0x45, 0x56, 0x78, 0x9a, 0xbc};
00091 
00092 /*----------------------------------------------------------------------------
00093  *        Exported functions
00094  *----------------------------------------------------------------------------*/
00095 
00096 /**
00097  * Gmac interrupt handler
00098  */
00099 void GMAC_Handler(void)
00100 {
00101     GMACD_Handler(&gGmacd, GMAC_QUE_0);
00102 }
00103 
00104 
00105 /**
00106  * Set the MAC address of the system.
00107  * Should only be called before tapdev_init is called.
00108  */
00109 void gmac_tapdev_setmac(u8_t *addr)
00110 {
00111     gGMacAddress[0] = addr[0];
00112     gGMacAddress[1] = addr[1];
00113     gGMacAddress[2] = addr[2];
00114     gGMacAddress[3] = addr[3];
00115     gGMacAddress[4] = addr[4];
00116     gGMacAddress[5] = addr[5];
00117 }
00118 
00119 /**
00120  * Initialization for GMAC device.
00121  * Should be called at the beginning of the program to set up the
00122  * network interface.
00123  */
00124 void gmac_tapdev_init(void)
00125 {
00126     sGmacd    *pGmacd = &gGmacd;
00127     GMacb      *pGmacb = &gGmacb;
00128     sGmacInit Que0, Que;
00129 
00130     
00131     /* Init GMAC driver structure */
00132     memset(&Que0, 0, sizeof(Que0));
00133     Que0.bIsGem = 1;
00134     Que0.bDmaBurstLength = 4;
00135     Que0.pRxBuffer =pRxBuffer;
00136     Que0.pRxD = gRxDs;
00137     Que0.wRxBufferSize = BUFFER_SIZE;
00138     Que0.wRxSize = RX_BUFFERS;
00139     Que0.pTxBuffer = pTxBuffer;
00140     Que0.pTxD = gTxDs;
00141     Que0.wTxBufferSize = BUFFER_SIZE;
00142     Que0.wTxSize = TX_BUFFERS;
00143     Que0.pTxCb = gTxCbs;
00144     
00145 
00146     
00147     memset(&Que, 0, sizeof(Que));
00148     Que.bIsGem = 1;
00149     Que.bDmaBurstLength = 4;
00150     Que.pRxBuffer =pRxDummyBuffer;
00151     Que.pRxD = gDummyRxDs;
00152     Que.wRxBufferSize = DUMMY_BUFF_SIZE;
00153     Que.wRxSize = DUMMY_SIZE;
00154     Que.pTxBuffer = pTxDummyBuffer;
00155     Que.pTxD = gDummyTxDs;
00156     Que.wTxBufferSize = DUMMY_BUFF_SIZE;
00157     Que.wTxSize = DUMMY_SIZE;
00158     Que.pTxCb = gDummyTxCbs;
00159     
00160     /* Init GMAC driver structure */
00161     GMACD_Init(pGmacd, GMAC, ID_GMAC, GMAC_CAF_ENABLE, GMAC_NBC_DISABLE);
00162     GMACD_InitTransfer(pGmacd, &Que, GMAC_QUE_2);
00163     
00164     GMACD_InitTransfer(pGmacd, &Que, GMAC_QUE_1);
00165     
00166     GMACD_InitTransfer(pGmacd, &Que0, GMAC_QUE_0);
00167     GMAC_SetAddress(gGmacd.pHw, 0, gGMacAddress);
00168 
00169     /* Setup GMAC buffers and interrupts */
00170     /* Configure and enable interrupt on RC compare */    
00171     NVIC_ClearPendingIRQ(GMAC_IRQn);
00172     NVIC_EnableIRQ(GMAC_IRQn);
00173     
00174     /* Init GMACB driver */
00175     GMACB_Init(pGmacb, pGmacd, BOARD_GMAC_PHY_ADDR);
00176 
00177     /* PHY initialize */
00178     if (!GMACB_InitPhy(pGmacb, BOARD_MCK,  &gmacResetPin, 1,  gmacPins, PIO_LISTSIZE( gmacPins )))
00179     {
00180         printf( "P: PHY Initialize ERROR!\n\r" ) ;
00181         return;
00182     }
00183 
00184     /* Auto Negotiate, work in RMII mode */
00185     if (!GMACB_AutoNegotiate(pGmacb))
00186     {
00187         printf( "P: Auto Negotiate ERROR!\n\r" ) ;
00188         return;
00189     }
00190     
00191     
00192     printf( "P: Link detected \n\r" ) ;
00193 }
00194 
00195 /**
00196  * Read for GMAC device.
00197  */
00198 uint32_t gmac_tapdev_read( void )
00199 {
00200     uint32_t pkt_len = 0 ;
00201     if ( GMACD_OK != GMACD_Poll( &gGmacd, (uint8_t*)uip_buf, UIP_CONF_BUFFER_SIZE, &pkt_len, GMAC_QUE_0) )
00202     {
00203         pkt_len = 0 ;
00204     }
00205     return pkt_len ;
00206 }
00207 
00208 /**
00209  * Send to GMAC device
00210  */
00211 void gmac_tapdev_send( void )
00212 {
00213     uint8_t gmac_rc ;
00214 
00215     gmac_rc = GMACD_Send( &gGmacd, (void*)uip_buf, uip_len, NULL, GMAC_QUE_0) ;
00216     if ( gmac_rc != GMACD_OK )
00217     {
00218         TRACE_ERROR( "E: Send, rc 0x%x\n\r", gmac_rc ) ;
00219     }
00220 }
00221 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines