2008-04-24 09:36:56 +00:00
|
|
|
/*
|
|
|
|
ChibiOS/RT - Copyright (C) 2006-2007 Giovanni Di Sirio.
|
|
|
|
|
|
|
|
This file is part of ChibiOS/RT.
|
|
|
|
|
|
|
|
ChibiOS/RT is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
ChibiOS/RT is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2008-05-13 15:33:52 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2008-05-21 13:57:47 +00:00
|
|
|
#include <ch.h>
|
|
|
|
|
2008-04-24 09:36:56 +00:00
|
|
|
#include "board.h"
|
|
|
|
#include "sam7x_emac.h"
|
|
|
|
#include "mii.h"
|
|
|
|
#include "at91lib/aic.h"
|
|
|
|
|
2008-05-14 14:50:58 +00:00
|
|
|
#define EMAC_RECEIVE_BUFFERS 24
|
2008-05-05 15:23:14 +00:00
|
|
|
#define EMAC_RECEIVE_BUFFERS_SIZE 128
|
|
|
|
#define EMAC_TRANSMIT_BUFFERS 2
|
|
|
|
#define EMAC_TRANSMIT_BUFFERS_SIZE 1518
|
|
|
|
|
2008-05-20 12:25:09 +00:00
|
|
|
EventSource EMACFrameTransmitted; /* A frame was transmitted. */
|
|
|
|
EventSource EMACFrameReceived; /* A frame was received. */
|
2008-05-26 10:07:44 +00:00
|
|
|
//static int received; /* Buffered frames counter. */
|
2008-05-20 12:25:09 +00:00
|
|
|
static bool_t link_up; /* Last from EMACGetLinkStatus()*/
|
2008-05-06 15:04:12 +00:00
|
|
|
|
|
|
|
static uint8_t default_mac[] = {0xAA, 0x55, 0x13, 0x37, 0x01, 0x10};
|
2008-05-05 15:23:14 +00:00
|
|
|
|
2008-05-26 14:57:55 +00:00
|
|
|
static BufDescriptorEntry rent[EMAC_RECEIVE_BUFFERS] __attribute__((aligned(8)));
|
2008-05-13 15:33:52 +00:00
|
|
|
static uint8_t rbuffers[EMAC_RECEIVE_BUFFERS * EMAC_RECEIVE_BUFFERS_SIZE] __attribute__((aligned(8)));
|
2008-05-26 14:57:55 +00:00
|
|
|
static BufDescriptorEntry *rxptr;
|
2008-05-13 15:33:52 +00:00
|
|
|
|
2008-05-26 14:57:55 +00:00
|
|
|
static BufDescriptorEntry tent[EMAC_TRANSMIT_BUFFERS] __attribute__((aligned(8)));
|
2008-05-13 15:33:52 +00:00
|
|
|
static uint8_t tbuffers[EMAC_TRANSMIT_BUFFERS * EMAC_TRANSMIT_BUFFERS_SIZE] __attribute__((aligned(8)));
|
2008-05-26 14:57:55 +00:00
|
|
|
static BufDescriptorEntry *txptr;
|
2008-04-24 09:36:56 +00:00
|
|
|
|
2008-04-30 14:58:33 +00:00
|
|
|
#define PHY_ADDRESS 1
|
2008-04-24 09:36:56 +00:00
|
|
|
#define AT91C_PB15_ERXDV AT91C_PB15_ERXDV_ECRSDV
|
2008-05-19 13:53:20 +00:00
|
|
|
#define EMAC_PIN_MASK (AT91C_PB0_ETXCK_EREFCK | \
|
|
|
|
AT91C_PB1_ETXEN | AT91C_PB2_ETX0 | \
|
2008-04-24 09:36:56 +00:00
|
|
|
AT91C_PB3_ETX1 | AT91C_PB4_ECRS | \
|
|
|
|
AT91C_PB5_ERX0 | AT91C_PB6_ERX1 | \
|
|
|
|
AT91C_PB7_ERXER | AT91C_PB8_EMDC | \
|
|
|
|
AT91C_PB9_EMDIO | AT91C_PB10_ETX2 | \
|
|
|
|
AT91C_PB11_ETX3 | AT91C_PB12_ETXER | \
|
|
|
|
AT91C_PB13_ERX2 | AT91C_PB14_ERX3 | \
|
|
|
|
AT91C_PB15_ERXDV | AT91C_PB16_ECOL | \
|
|
|
|
AT91C_PB17_ERXCK)
|
|
|
|
|
2008-05-06 15:04:12 +00:00
|
|
|
#define PHY_LATCHED_PINS (AT91C_PB4_ECRS | AT91C_PB5_ERX0 | AT91C_PB6_ERX1 | \
|
|
|
|
AT91C_PB7_ERXER | AT91C_PB13_ERX2 | AT91C_PB14_ERX3 | \
|
|
|
|
AT91C_PB15_ERXDV | AT91C_PB16_ECOL | PIOB_PHY_IRQ)
|
|
|
|
|
2008-04-30 14:58:33 +00:00
|
|
|
/*
|
|
|
|
* PHY utilities.
|
|
|
|
*/
|
|
|
|
static uint32_t phy_get(uint8_t regno) {
|
|
|
|
|
|
|
|
AT91C_BASE_EMAC->EMAC_MAN = (1 << 30) | // SOF = 01
|
|
|
|
(2 << 28) | // RW = 10
|
|
|
|
(PHY_ADDRESS << 23) |
|
|
|
|
(regno << 18) |
|
|
|
|
(2 << 16); // CODE = 10
|
|
|
|
while (!( AT91C_BASE_EMAC->EMAC_NSR & AT91C_EMAC_IDLE))
|
|
|
|
;
|
|
|
|
return AT91C_BASE_EMAC->EMAC_MAN & 0xFFFF;
|
|
|
|
}
|
|
|
|
|
2008-05-06 15:04:12 +00:00
|
|
|
/*static void phy_put(uint8_t regno, uint32_t val) {
|
2008-04-30 14:58:33 +00:00
|
|
|
|
|
|
|
AT91C_BASE_EMAC->EMAC_MAN = (1 << 30) | // SOF = 01
|
|
|
|
(1 << 28) | // RW = 01
|
|
|
|
(PHY_ADDRESS << 23) |
|
|
|
|
(regno << 18) |
|
|
|
|
(2 << 16) | // CODE = 10
|
|
|
|
val;
|
|
|
|
while (!( AT91C_BASE_EMAC->EMAC_NSR & AT91C_EMAC_IDLE))
|
|
|
|
;
|
2008-05-06 15:04:12 +00:00
|
|
|
}*/
|
|
|
|
|
2008-05-07 12:00:55 +00:00
|
|
|
#define RSR_BITS (AT91C_EMAC_BNA | AT91C_EMAC_REC | AT91C_EMAC_OVR)
|
|
|
|
#define TSR_BITS (AT91C_EMAC_UBR | AT91C_EMAC_COL | AT91C_EMAC_RLES | \
|
|
|
|
AT91C_EMAC_BEX | AT91C_EMAC_COMP | AT91C_EMAC_UND)
|
|
|
|
|
2008-05-06 15:04:12 +00:00
|
|
|
__attribute__((noinline))
|
|
|
|
static void ServeInterrupt(void) {
|
2008-05-07 12:00:55 +00:00
|
|
|
uint32_t isr, rsr, tsr;
|
|
|
|
|
|
|
|
/* Fix for the EMAC errata */
|
|
|
|
isr = AT91C_BASE_EMAC->EMAC_ISR;
|
|
|
|
rsr = AT91C_BASE_EMAC->EMAC_RSR;
|
|
|
|
tsr = AT91C_BASE_EMAC->EMAC_TSR;
|
|
|
|
|
|
|
|
if ((isr & AT91C_EMAC_RCOMP) || (rsr & RSR_BITS)) {
|
2008-05-13 15:33:52 +00:00
|
|
|
if (rsr & AT91C_EMAC_REC) {
|
2008-05-26 10:07:44 +00:00
|
|
|
// received++;
|
2009-01-10 15:20:40 +00:00
|
|
|
chSysLockI();
|
2008-07-23 10:38:16 +00:00
|
|
|
chEvtBroadcastI(&EMACFrameReceived);
|
2009-01-10 15:20:40 +00:00
|
|
|
chSysUnlockI();
|
2008-05-13 15:33:52 +00:00
|
|
|
}
|
2008-05-07 12:00:55 +00:00
|
|
|
AT91C_BASE_EMAC->EMAC_RSR = RSR_BITS;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((isr & AT91C_EMAC_TCOMP) || (tsr & TSR_BITS)) {
|
2009-01-10 15:20:40 +00:00
|
|
|
if (tsr & AT91C_EMAC_COMP) {
|
|
|
|
chSysLockI();
|
2008-07-23 10:38:16 +00:00
|
|
|
chEvtBroadcastI(&EMACFrameTransmitted);
|
2009-01-10 15:20:40 +00:00
|
|
|
chSysUnlockI();
|
|
|
|
}
|
2008-05-07 12:00:55 +00:00
|
|
|
AT91C_BASE_EMAC->EMAC_TSR = TSR_BITS;
|
|
|
|
}
|
|
|
|
AT91C_BASE_AIC->AIC_EOICR = 0;
|
2008-05-06 15:04:12 +00:00
|
|
|
}
|
|
|
|
|
2009-01-10 15:20:40 +00:00
|
|
|
CH_IRQ_HANDLER void EMACIrqHandler(void) {
|
2008-05-06 15:04:12 +00:00
|
|
|
|
2009-01-10 16:21:27 +00:00
|
|
|
CH_IRQ_PROLOGUE();
|
|
|
|
|
2008-05-06 15:04:12 +00:00
|
|
|
ServeInterrupt();
|
2009-01-10 16:21:27 +00:00
|
|
|
|
|
|
|
CH_IRQ_EPILOGUE();
|
2008-04-30 14:58:33 +00:00
|
|
|
}
|
|
|
|
|
2008-04-24 09:36:56 +00:00
|
|
|
/*
|
|
|
|
* EMAC subsystem initialization.
|
|
|
|
*/
|
|
|
|
void InitEMAC(int prio) {
|
|
|
|
int i;
|
|
|
|
|
2008-05-05 15:23:14 +00:00
|
|
|
/*
|
|
|
|
* Buffers initialization.
|
|
|
|
*/
|
2008-05-26 10:07:44 +00:00
|
|
|
// received = 0;
|
2008-04-24 09:36:56 +00:00
|
|
|
for (i = 0; i < EMAC_RECEIVE_BUFFERS; i++) {
|
2008-05-05 15:23:14 +00:00
|
|
|
rent[i].w1 = (uint32_t)&rbuffers[i * EMAC_RECEIVE_BUFFERS_SIZE];
|
2008-04-24 09:36:56 +00:00
|
|
|
rent[i].w2 = 0;
|
|
|
|
}
|
|
|
|
rent[EMAC_RECEIVE_BUFFERS - 1].w1 |= W1_R_WRAP;
|
2008-05-14 14:50:58 +00:00
|
|
|
rxptr = rent;
|
2008-05-05 15:23:14 +00:00
|
|
|
for (i = 0; i < EMAC_TRANSMIT_BUFFERS; i++) {
|
2008-05-26 10:07:44 +00:00
|
|
|
tent[i].w1 = (uint32_t)&tbuffers[i * EMAC_TRANSMIT_BUFFERS_SIZE];
|
|
|
|
tent[i].w2 = EMAC_TRANSMIT_BUFFERS_SIZE | W2_T_USED;
|
2008-05-05 15:23:14 +00:00
|
|
|
}
|
2008-05-13 15:33:52 +00:00
|
|
|
tent[EMAC_TRANSMIT_BUFFERS - 1].w2 |= W2_T_WRAP;
|
2008-05-14 14:50:58 +00:00
|
|
|
txptr = tent;
|
2008-05-05 15:23:14 +00:00
|
|
|
|
2008-04-24 09:36:56 +00:00
|
|
|
/*
|
2008-05-06 15:04:12 +00:00
|
|
|
* Disables the pullups on all the pins that are latched on reset by the PHY.
|
|
|
|
* The status latched into the PHY is:
|
|
|
|
* PHYADDR = 00001
|
|
|
|
* PCS_LPBK = 0 (disabled)
|
|
|
|
* ISOLATE = 0 (disabled)
|
|
|
|
* RMIISEL = 0 (MII mode)
|
|
|
|
* RMIIBTB = 0 (BTB mode disabled)
|
|
|
|
* SPEED = 1 (100mbps)
|
|
|
|
* DUPLEX = 1 (full duplex)
|
|
|
|
* ANEG_EN = 1 (auto negotiation enabled)
|
2008-04-24 09:36:56 +00:00
|
|
|
*/
|
2008-05-06 15:04:12 +00:00
|
|
|
AT91C_BASE_PIOB->PIO_PPUDR = PHY_LATCHED_PINS;
|
2008-04-24 09:36:56 +00:00
|
|
|
|
|
|
|
/*
|
2008-05-06 15:04:12 +00:00
|
|
|
* PHY power control.
|
2008-04-24 09:36:56 +00:00
|
|
|
*/
|
2008-05-06 15:04:12 +00:00
|
|
|
AT91C_BASE_PIOB->PIO_OER = PIOB_PHY_PD; // Becomes an output.
|
|
|
|
AT91C_BASE_PIOB->PIO_PPUDR = PIOB_PHY_PD; // Default pullup disabled.
|
|
|
|
AT91C_BASE_PIOB->PIO_SODR = PIOB_PHY_PD; // Output to high level.
|
2008-04-24 09:36:56 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* PHY reset by pulsing the NRST pin.
|
|
|
|
*/
|
|
|
|
AT91C_BASE_RSTC->RSTC_RMR = 0xA5000100;
|
|
|
|
AT91C_BASE_RSTC->RSTC_RCR = 0xA5000000 | AT91C_RSTC_EXTRST;
|
|
|
|
while (!(AT91C_BASE_RSTC->RSTC_RSR & AT91C_RSTC_NRSTL))
|
|
|
|
;
|
|
|
|
|
|
|
|
/*
|
2008-05-19 13:53:20 +00:00
|
|
|
* EMAC pins setup and clock enable. Note, PB18 is not included because it is
|
|
|
|
* used as #PD control and not as EF100.
|
2008-04-24 09:36:56 +00:00
|
|
|
*/
|
2008-05-19 13:53:20 +00:00
|
|
|
AT91C_BASE_PMC->PMC_PCER = 1 << AT91C_ID_EMAC;
|
2008-04-24 09:36:56 +00:00
|
|
|
AT91C_BASE_PIOB->PIO_ASR = EMAC_PIN_MASK;
|
|
|
|
AT91C_BASE_PIOB->PIO_PDR = EMAC_PIN_MASK;
|
2008-05-20 12:25:09 +00:00
|
|
|
AT91C_BASE_PIOB->PIO_PPUDR = EMAC_PIN_MASK;
|
2008-04-24 09:36:56 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* EMAC setup.
|
|
|
|
*/
|
2008-05-20 12:25:09 +00:00
|
|
|
AT91C_BASE_EMAC->EMAC_NCR = 0; // Initial setting.
|
2008-05-05 15:23:14 +00:00
|
|
|
AT91C_BASE_EMAC->EMAC_NCFGR = 2 << 10; // MDC-CLK = MCK / 32
|
2008-04-30 14:58:33 +00:00
|
|
|
AT91C_BASE_EMAC->EMAC_USRIO = AT91C_EMAC_CLKEN; // Enable EMAC in MII mode
|
2008-05-05 15:23:14 +00:00
|
|
|
AT91C_BASE_EMAC->EMAC_RBQP = (AT91_REG)rent; // RX buffers list
|
|
|
|
AT91C_BASE_EMAC->EMAC_TBQP = (AT91_REG)tent; // TX buffers list
|
|
|
|
AT91C_BASE_EMAC->EMAC_RSR = AT91C_EMAC_OVR |
|
|
|
|
AT91C_EMAC_REC |
|
|
|
|
AT91C_EMAC_BNA; // Clears RSR
|
2008-05-26 14:57:55 +00:00
|
|
|
AT91C_BASE_EMAC->EMAC_NCFGR |= AT91C_EMAC_DRFCS; // Initial NCFGR settings
|
2008-05-05 15:23:14 +00:00
|
|
|
AT91C_BASE_EMAC->EMAC_NCR |= AT91C_EMAC_TE |
|
|
|
|
AT91C_EMAC_RE |
|
|
|
|
AT91C_EMAC_CLRSTAT; // Initial NCR settings
|
2008-05-06 15:04:12 +00:00
|
|
|
EMACSetAddress(default_mac);
|
|
|
|
|
|
|
|
/*
|
2008-05-20 12:25:09 +00:00
|
|
|
* PHY detection and settings.
|
2008-05-06 15:04:12 +00:00
|
|
|
*/
|
|
|
|
AT91C_BASE_EMAC->EMAC_NCR |= AT91C_EMAC_MPE;
|
|
|
|
if ((phy_get(MII_PHYSID1) != (MII_MICREL_ID >> 16)) ||
|
2008-05-19 13:53:20 +00:00
|
|
|
((phy_get(MII_PHYSID2) & 0xFFF0) != (MII_MICREL_ID & 0xFFF0)))
|
2008-05-18 16:10:31 +00:00
|
|
|
chSysHalt();
|
2008-05-20 12:25:09 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Waits for auto-negotiation to end and then detects the link status.
|
|
|
|
*/
|
2008-05-06 15:04:12 +00:00
|
|
|
AT91C_BASE_EMAC->EMAC_NCR &= ~AT91C_EMAC_MPE;
|
2008-04-30 14:58:33 +00:00
|
|
|
|
2008-05-06 15:04:12 +00:00
|
|
|
/*
|
|
|
|
* Interrupt setup.
|
|
|
|
*/
|
2008-05-13 15:33:52 +00:00
|
|
|
AT91C_BASE_EMAC->EMAC_IER = AT91C_EMAC_RCOMP | AT91C_EMAC_TCOMP;
|
2008-05-06 15:04:12 +00:00
|
|
|
AIC_ConfigureIT(AT91C_ID_EMAC,
|
|
|
|
AT91C_AIC_SRCTYPE_INT_HIGH_LEVEL | prio,
|
|
|
|
EMACIrqHandler);
|
|
|
|
AIC_EnableIT(AT91C_ID_EMAC);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Event sources setup.
|
|
|
|
*/
|
|
|
|
chEvtInit(&EMACFrameTransmitted);
|
|
|
|
chEvtInit(&EMACFrameReceived);
|
2008-04-30 14:58:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the MAC address.
|
|
|
|
*/
|
2008-05-21 13:57:47 +00:00
|
|
|
void EMACSetAddress(const uint8_t *eaddr) {
|
2008-04-30 14:58:33 +00:00
|
|
|
|
|
|
|
AT91C_BASE_EMAC->EMAC_SA1L = (AT91_REG)((eaddr[3] << 24) | (eaddr[2] << 16) |
|
|
|
|
(eaddr[1] << 8) | eaddr[0]);
|
|
|
|
AT91C_BASE_EMAC->EMAC_SA1H = (AT91_REG)((eaddr[5] << 8) | eaddr[4]);
|
2008-04-24 09:36:56 +00:00
|
|
|
}
|
|
|
|
|
2008-05-20 12:25:09 +00:00
|
|
|
/*
|
|
|
|
* Returns TRUE if the link is active. To be invoked at regular intervals in
|
|
|
|
* order to monitor the link.
|
|
|
|
* @note It is not thread-safe.
|
|
|
|
*/
|
|
|
|
bool_t EMACGetLinkStatus(void) {
|
|
|
|
uint32_t ncfgr, bmsr, bmcr, lpa;
|
|
|
|
|
|
|
|
AT91C_BASE_EMAC->EMAC_NCR |= AT91C_EMAC_MPE;
|
|
|
|
(void)phy_get(MII_BMSR);
|
|
|
|
bmsr = phy_get(MII_BMSR);
|
|
|
|
if (!(bmsr & BMSR_LSTATUS)) {
|
|
|
|
AT91C_BASE_EMAC->EMAC_NCR &= ~AT91C_EMAC_MPE;
|
|
|
|
return link_up = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
ncfgr = AT91C_BASE_EMAC->EMAC_NCFGR & ~(AT91C_EMAC_SPD | AT91C_EMAC_FD);
|
|
|
|
bmcr = phy_get(MII_BMCR);
|
|
|
|
if (bmcr & BMCR_ANENABLE) {
|
|
|
|
lpa = phy_get(MII_LPA);
|
|
|
|
if (lpa & (LPA_100HALF | LPA_100FULL | LPA_100BASE4))
|
|
|
|
ncfgr |= AT91C_EMAC_SPD;
|
|
|
|
if (lpa & (LPA_10FULL | LPA_100FULL))
|
|
|
|
ncfgr |= AT91C_EMAC_FD;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (bmcr & BMCR_SPEED100)
|
|
|
|
ncfgr |= AT91C_EMAC_SPD;
|
|
|
|
if (bmcr & BMCR_FULLDPLX)
|
|
|
|
ncfgr |= AT91C_EMAC_FD;
|
|
|
|
}
|
|
|
|
AT91C_BASE_EMAC->EMAC_NCFGR = ncfgr;
|
|
|
|
AT91C_BASE_EMAC->EMAC_NCR &= ~AT91C_EMAC_MPE;
|
|
|
|
return link_up = TRUE;
|
|
|
|
}
|
|
|
|
|
2008-05-21 13:57:47 +00:00
|
|
|
/*
|
|
|
|
* Allocates and locks a buffer for a transmission operation.
|
|
|
|
*/
|
2008-05-26 10:07:44 +00:00
|
|
|
BufDescriptorEntry *EMACGetTransmitBuffer(void) {
|
2008-05-21 13:57:47 +00:00
|
|
|
BufDescriptorEntry *cptr;
|
|
|
|
|
|
|
|
if (!link_up)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
chSysLock();
|
|
|
|
cptr = txptr;
|
|
|
|
if (!(cptr->w2 & W2_T_USED) ||
|
|
|
|
(cptr->w2 & W2_T_LOCKED)) {
|
|
|
|
chSysUnlock();
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
cptr->w2 |= W2_T_LOCKED; /* Locks the buffer while copying.*/
|
|
|
|
if (++txptr >= &tent[EMAC_TRANSMIT_BUFFERS])
|
|
|
|
txptr = tent;
|
|
|
|
chSysUnlock();
|
|
|
|
return cptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Transmits a previously allocated buffer and then releases it.
|
|
|
|
*/
|
|
|
|
void EMACTransmit(BufDescriptorEntry *cptr, size_t size) {
|
2008-07-23 10:38:16 +00:00
|
|
|
|
2008-05-26 10:07:44 +00:00
|
|
|
chDbgAssert(size <= EMAC_TRANSMIT_BUFFERS_SIZE, "sam7x_emac.c, EMACTransmit");
|
2008-05-21 13:57:47 +00:00
|
|
|
|
|
|
|
chSysLock();
|
2008-05-26 13:23:11 +00:00
|
|
|
if (cptr < &tent[EMAC_TRANSMIT_BUFFERS - 1])
|
|
|
|
cptr->w2 = size | W2_T_LAST_BUFFER;
|
|
|
|
else
|
|
|
|
cptr->w2 = size | W2_T_LAST_BUFFER | W2_T_WRAP;
|
2008-05-21 13:57:47 +00:00
|
|
|
AT91C_BASE_EMAC->EMAC_NCR |= AT91C_EMAC_TSTART;
|
|
|
|
chSysUnlock();
|
|
|
|
}
|
|
|
|
|
2008-05-13 15:33:52 +00:00
|
|
|
/*
|
|
|
|
* Reads a buffered frame.
|
|
|
|
* Returns TRUE if a frame was present and read else FALSE.
|
2008-05-20 12:25:09 +00:00
|
|
|
* @note It is not thread-safe.
|
2008-04-24 09:36:56 +00:00
|
|
|
*/
|
2008-05-13 15:33:52 +00:00
|
|
|
bool_t EMACReceive(uint8_t *buf, size_t *sizep) {
|
|
|
|
unsigned n;
|
|
|
|
size_t size;
|
|
|
|
uint8_t *p;
|
|
|
|
bool_t overflow, found;
|
|
|
|
|
2008-05-26 10:07:44 +00:00
|
|
|
// chSysLock();
|
|
|
|
// if (received <= 0) {
|
|
|
|
// chSysUnlock();
|
|
|
|
// return FALSE;
|
|
|
|
// }
|
|
|
|
// received--;
|
|
|
|
// chSysUnlock();
|
2008-05-13 15:33:52 +00:00
|
|
|
|
|
|
|
n = EMAC_RECEIVE_BUFFERS;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Skips unused buffers, if any.
|
|
|
|
*/
|
|
|
|
skip:
|
2008-05-14 14:50:58 +00:00
|
|
|
while (n && !(rxptr->w1 & W1_R_OWNERSHIP)) {
|
2008-05-26 10:07:44 +00:00
|
|
|
if (++rxptr >= &rent[EMAC_RECEIVE_BUFFERS])
|
2008-05-14 14:50:58 +00:00
|
|
|
rxptr = rent;
|
2008-05-13 15:33:52 +00:00
|
|
|
n--;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Skips fragments, if any.
|
|
|
|
*/
|
2008-05-14 14:50:58 +00:00
|
|
|
while (n && (rxptr->w1 & W1_R_OWNERSHIP) && !(rxptr->w2 & W2_R_FRAME_START)) {
|
|
|
|
rxptr->w1 &= ~W1_R_OWNERSHIP;
|
2008-05-26 10:07:44 +00:00
|
|
|
if (++rxptr >= &rent[EMAC_RECEIVE_BUFFERS])
|
2008-05-14 14:50:58 +00:00
|
|
|
rxptr = rent;
|
2008-05-13 15:33:52 +00:00
|
|
|
n--;
|
|
|
|
}
|
|
|
|
|
|
|
|
restart:
|
|
|
|
p = buf;
|
|
|
|
size = 0;
|
|
|
|
found = overflow = FALSE;
|
|
|
|
while (n && !found) {
|
|
|
|
size_t segsize;
|
|
|
|
|
2008-05-14 14:50:58 +00:00
|
|
|
if (!(rxptr->w1 & W1_R_OWNERSHIP))
|
2008-05-13 15:33:52 +00:00
|
|
|
goto skip; /* Empty buffer for some reason... */
|
|
|
|
|
2008-05-14 14:50:58 +00:00
|
|
|
if (size && (rxptr->w2 & W2_R_FRAME_START))
|
2008-05-13 15:33:52 +00:00
|
|
|
goto restart; /* Another start buffer for some reason... */
|
|
|
|
|
2008-05-14 14:50:58 +00:00
|
|
|
if (rxptr->w2 & W2_R_FRAME_END) {
|
|
|
|
segsize = (rxptr->w2 & W2_T_LENGTH_MASK) - size;
|
|
|
|
if (((rxptr->w2 & W2_T_LENGTH_MASK) > *sizep) ||
|
2008-05-13 15:33:52 +00:00
|
|
|
(segsize > EMAC_RECEIVE_BUFFERS_SIZE))
|
|
|
|
overflow = TRUE;
|
|
|
|
found = TRUE;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
segsize = EMAC_RECEIVE_BUFFERS_SIZE;
|
|
|
|
if (size + segsize > *sizep)
|
|
|
|
overflow = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!overflow) {
|
2008-05-26 10:07:44 +00:00
|
|
|
chDbgAssert(segsize <= 128, "sam7x_emac.c, EMACReceive()")
|
|
|
|
memcpy(p, (void *)(rxptr->w1 & W1_R_ADDRESS_MASK), segsize);
|
2008-05-13 15:33:52 +00:00
|
|
|
p += segsize;
|
|
|
|
size += segsize;
|
|
|
|
}
|
|
|
|
|
2008-05-14 14:50:58 +00:00
|
|
|
rxptr->w1 &= ~W1_R_OWNERSHIP;
|
2008-05-26 10:07:44 +00:00
|
|
|
if (++rxptr >= &rent[EMAC_RECEIVE_BUFFERS])
|
2008-05-14 14:50:58 +00:00
|
|
|
rxptr = rent;
|
2008-05-13 15:33:52 +00:00
|
|
|
n--;
|
|
|
|
}
|
2008-05-05 15:23:14 +00:00
|
|
|
|
2008-05-13 15:33:52 +00:00
|
|
|
*sizep = size;
|
|
|
|
return found && !overflow;
|
2008-05-05 15:23:14 +00:00
|
|
|
}
|