git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@1186 35acf78f-673a-0410-8e92-d51de3d6d3f4

master
gdisirio 2009-09-27 12:00:29 +00:00
parent aee118ec3b
commit 38c94f3570
9 changed files with 245 additions and 126 deletions

View File

@ -49,7 +49,6 @@ void macObjectInit(MACDriver *macp) {
chSemInit(&macp->md_tdsem, MAC_TRANSMIT_DESCRIPTORS);
chSemInit(&macp->md_rdsem, 0);
macp->md_state = ifStopped;
}
/**
@ -65,46 +64,7 @@ void macObjectInit(MACDriver *macp) {
*/
void macSetAddress(MACDriver *macp, uint8_t *p) {
if (macp->md_state == ifStopped)
mac_lld_set_address(macp, p);
}
/**
* @brief Startsthe I/O activity and enters a low power mode.
*
* @param[in] macp pointer to the @p MACDriver object
*
*/
void macStart(MACDriver *macp) {
chSysLock();
if (macp->md_state == ifStarted) {
chSysUnlock();
return;
}
macp->md_state = ifStarted;
chSysUnlock();
mac_lld_start(macp);
}
/**
* @brief Stops the I/O activity.
*
* @param[in] macp pointer to the @p MACDriver object
*/
void macStop(MACDriver *macp) {
chSysLock();
if (macp->md_state == ifStopped) {
chSysUnlock();
return;
}
macp->md_state = ifStopped;
chSemResetI(&macp->md_tdsem, MAC_TRANSMIT_DESCRIPTORS);
chSemResetI(&macp->md_rdsem, 0);
chSchRescheduleS();
chSysUnlock();
mac_lld_stop(macp);
mac_lld_set_address(macp, p);
}
/**
@ -114,25 +74,26 @@ void macStop(MACDriver *macp) {
* invoking thread is queued until one is freed.
*
* @param[in] macp pointer to the @p MACDriver object
* @param[in] size size of the frame to be transmitted
* @param[in] time the number of ticks before the operation timeouts,
* the following special values are allowed:
* - @a TIME_IMMEDIATE immediate timeout.
* - @a TIME_INFINITE no timeout.
* .
* @return A pointer to a @p MACTransmitDescriptor structure or @p NULL if
* the operation timed out or the driver went in stop mode.
* the operation timed out.
*/
MACTransmitDescriptor *macWaitTransmitDescriptor(MACDriver *macp,
size_t size,
systime_t time) {
MACTransmitDescriptor *tdp;
chSysLock();
if ((macp->md_state == ifStopped) ||
(chSemWaitTimeoutS(&tdsem, time) != RDY_OK))
if (chSemWaitTimeoutS(&tdsem, time) != RDY_OK)
tdp = NULL;
else
tdp = max_lld_get_transmit_descriptor(macp);
tdp = max_lld_get_transmit_descriptor(macp, size);
chSysUnlock();
return tdp;
@ -148,8 +109,7 @@ MACTransmitDescriptor *macWaitTransmitDescriptor(MACDriver *macp,
void macReleaseTransmitDescriptor(MACDriver *macp,
MACTransmitDescriptor *tdp) {
if (macp->md_state == ifStarted)
mac_lld_release_transmit_descriptor(macp, tdp);
mac_lld_release_transmit_descriptor(macp, tdp);
}
/**
@ -159,26 +119,26 @@ void macReleaseTransmitDescriptor(MACDriver *macp,
* until one is received.
*
* @param[in] macp pointer to the @p MACDriver object
* @param[out szp size of the received frame
* @param[in] time the number of ticks before the operation timeouts,
* the following special values are allowed:
* - @a TIME_IMMEDIATE immediate timeout.
* - @a TIME_INFINITE no timeout.
* .
* @return A pointer to a @p MACReceiveDescriptor structure or @p NULL if
* the operation timed out, the driver went in stop mode or some
* transient error happened.
* the operation timed out or some transient error happened.
*/
MACReceiveDescriptor *macWaitReceiveDescriptor(MACDriver *macp,
size_t *szp,
systime_t time) {
MACReceiveDescriptor *rdp;
chSysLock();
if ((macp->md_state == ifStopped) ||
(chSemWaitTimeoutS(&rdsem, time) != RDY_OK))
if (chSemWaitTimeoutS(&rdsem, time) != RDY_OK)
rdp = NULL;
else
rdp = max_lld_get_receive_descriptor(macp);
rdp = max_lld_get_receive_descriptor(macp, szp);
chSysUnlock();
return rdp;
@ -195,8 +155,7 @@ MACReceiveDescriptor *macWaitReceiveDescriptor(MACDriver *macp,
void macReleaseReceiveDescriptor(MACDriver *macp,
MACReceiveDescriptor *rdp) {
if (macp->md_state == ifStarted)
mac_lld_release_receive_descriptor(macp, rdp);
mac_lld_release_receive_descriptor(macp, rdp);
}
/** @} */

View File

@ -54,10 +54,12 @@ extern "C" {
void macStart(MACDriver *macp);
void macStop(MACDriver *macp);
MACTransmitDescriptor *macWaitTransmitDescriptor(MACDriver *macp,
size_t size,
systime_t time);
void macReleaseTransmitDescriptor(MACDriver *macp,
MACTransmitDescriptor *tdp);
MACReceiveDescriptor *macWaitReceiveDescriptor(MACDriver *macp,
size_t *szp,
systime_t time);
void macReleaseReceiveDescriptor(MACDriver *macp,
MACReceiveDescriptor *rdp);

View File

@ -26,12 +26,164 @@
#include <ch.h>
#include <mac.h>
#include <phy.h>
#include "mii.h"
#include "at91lib/aic.h"
/**
* @brief EMAC object.
*/
MACDriver MAC1;
#define EMAC_PIN_MASK (AT91C_PB0_ETXCK_EREFCK | AT91C_PB1_ETXEN | \
AT91C_PB2_ETX0 | 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_ECRSDV | \
AT91C_PB16_ECOL | AT91C_PB17_ERXCK)
#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)
#ifndef __DOXYGEN__
static bool_t link_up;
static uint8_t default_mac[] = {0xAA, 0x55, 0x13, 0x37, 0x01, 0x10};
static MACReceiveDescriptor rd[EMAC_RECEIVE_BUFFERS] __attribute__((aligned(8)));
static uint8_t rb[EMAC_RECEIVE_BUFFERS * EMAC_RECEIVE_BUFFERS_SIZE] __attribute__((aligned(8)));
static MACReceiveDescriptor *rxptr;
static MACTransmitDescriptor td[EMAC_TRANSMIT_BUFFERS] __attribute__((aligned(8)));
static uint8_t tb[EMAC_TRANSMIT_BUFFERS * EMAC_TRANSMIT_BUFFERS_SIZE] __attribute__((aligned(8)));
static MACTransmitDescriptor *txptr;
#endif
/**
* @brief IRQ handler.
*/
/** @cond never*/
__attribute__((noinline))
/** @endcond*/
static void serve_interrupt(void) {
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)) {
if (rsr & AT91C_EMAC_REC) {
chSysLockFromIsr();
chSemSignalI(&MAC1.md_rdsem);
chSysUnlockFromIsr();
}
AT91C_BASE_EMAC->EMAC_RSR = RSR_BITS;
}
if ((isr & AT91C_EMAC_TCOMP) || (tsr & TSR_BITS)) {
if (tsr & AT91C_EMAC_COMP) {
chSysLockFromIsr();
chSemSignalI(&MAC1.md_tdsem);
chSysUnlockFromIsr();
}
AT91C_BASE_EMAC->EMAC_TSR = TSR_BITS;
}
AT91C_BASE_AIC->AIC_EOICR = 0;
}
/**
* @brief EMAC IRQ veneer handler.
*/
CH_IRQ_HANDLER(irq_handler) {
CH_IRQ_PROLOGUE();
serve_interrupt();
CH_IRQ_EPILOGUE();
}
/**
* @brief Low level MAC initialization.
*/
void mac_lld_init(void) {
unsigned i;
macObjectInit(&MAC1);
/*
* Buffers initialization.
*/
for (i = 0; i < EMAC_RECEIVE_BUFFERS; i++) {
rd[i].w1 = (uint32_t)&rb[i * EMAC_RECEIVE_BUFFERS_SIZE];
rd[i].w2 = 0;
}
rd[EMAC_RECEIVE_BUFFERS - 1].w1 |= W1_R_WRAP;
rxptr = rd;
for (i = 0; i < EMAC_TRANSMIT_BUFFERS; i++) {
td[i].w1 = (uint32_t)&tb[i * EMAC_TRANSMIT_BUFFERS_SIZE];
td[i].w2 = EMAC_TRANSMIT_BUFFERS_SIZE | W2_T_LAST_BUFFER | W2_T_USED;
}
td[EMAC_TRANSMIT_BUFFERS - 1].w2 |= W2_T_WRAP;
txptr = td;
/*
* Associated PHY initialization.
*/
phyReset(&MAC1);
/*
* EMAC pins setup and clock enable. Note, PB18 is not included because it is
* used as #PD control and not as EF100.
*/
AT91C_BASE_PMC->PMC_PCER = 1 << AT91C_ID_EMAC;
AT91C_BASE_PIOB->PIO_ASR = EMAC_PIN_MASK;
AT91C_BASE_PIOB->PIO_PDR = EMAC_PIN_MASK;
AT91C_BASE_PIOB->PIO_PPUDR = EMAC_PIN_MASK;
/*
* EMAC Initial setup.
*/
AT91C_BASE_EMAC->EMAC_NCR = 0; /* Stopped but MCE active.*/
AT91C_BASE_EMAC->EMAC_NCFGR = 2 << 10; /* MDC-CLK = MCK / 32 */
AT91C_BASE_EMAC->EMAC_USRIO = AT91C_EMAC_CLKEN;/* Enable EMAC in MII mode.*/
AT91C_BASE_EMAC->EMAC_RBQP = (AT91_REG)rd; /* RX descriptors list.*/
AT91C_BASE_EMAC->EMAC_TBQP = (AT91_REG)td; /* TX descriptors list.*/
AT91C_BASE_EMAC->EMAC_RSR = AT91C_EMAC_OVR |
AT91C_EMAC_REC |
AT91C_EMAC_BNA; /* Clears RSR.*/
AT91C_BASE_EMAC->EMAC_NCFGR |= AT91C_EMAC_DRFCS;/* Initial NCFGR settings.*/
AT91C_BASE_EMAC->EMAC_NCR |= AT91C_EMAC_TE |
AT91C_EMAC_RE |
AT91C_EMAC_CLRSTAT;/* Initial NCR settings.*/
mac_lld_set_address(&MAC1, default_mac);
#if PHY_HARDWARE == PHY_MICREL_KS8721
/*
* PHY device identification.
*/
AT91C_BASE_EMAC->EMAC_NCR |= AT91C_EMAC_MPE;
if ((phyGet(&MAC1, MII_PHYSID1) != (MII_KS8721_ID >> 16)) ||
((phyGet(&MAC1, MII_PHYSID2) & 0xFFF0) != (MII_KS8721_ID & 0xFFF0)))
chSysHalt();
AT91C_BASE_EMAC->EMAC_NCR &= ~AT91C_EMAC_MPE;
#endif
/*
* Interrupt configuration.
*/
AIC_ConfigureIT(AT91C_ID_EMAC,
AT91C_AIC_SRCTYPE_INT_HIGH_LEVEL | EMAC_INTERRUPT_PRIORITY,
irq_handler);
AIC_EnableIT(AT91C_ID_EMAC);
}
/**
@ -40,31 +192,14 @@ void mac_lld_init(void) {
* @param[in] macp pointer to the @p MACDriver object
* @param[in] p pointer to a six bytes buffer containing the MAC address. If
* this parameter is set to @p NULL then a system default MAC is
* used.
*
* @note This function should be invoked after the @p macInit() and before
* @p macStart() else the result is unspecified (performed or ignored).
* used. The MAC address must be aligned with the most significant
* byte first.
*/
void mac_lld_set_address(MACDriver *macp, uint8_t *p) {
}
/**
* @brief Starts the I/O activity and enters a low power mode.
*
* @param[in] macp pointer to the @p MACDriver object
*/
void mac_lld_start(MACDriver *macp) {
}
/**
* @brief Stops the I/O activity.
*
* @param[in] macp pointer to the @p MACDriver object
*/
void mac_lld_stop(MACDriver *macp) {
AT91C_BASE_EMAC->EMAC_SA1L = (AT91_REG)((p[2] << 24) | (p[3] << 16) |
(p[4] << 8) | p[5]);
AT91C_BASE_EMAC->EMAC_SA1H = (AT91_REG)((p[0] << 8) | p[1]);
}
/**
@ -73,12 +208,40 @@ void mac_lld_stop(MACDriver *macp) {
* returned.
*
* @param[in] macp pointer to the @p MACDriver object
* @param[in] size size of the frame to be transmitted
* @return A pointer to a @p MACTransmitDescriptor structure or @p NULL if
* a descriptor is not available or the driver went in stop mode.
* a descriptor is not available.
*/
MACTransmitDescriptor *max_lld_get_transmit_descriptor(MACDriver *macp) {
MACTransmitDescriptor *max_lld_get_transmit_descriptor(MACDriver *macp,
size_t size) {
MACTransmitDescriptor *tdp;
return NULL;
chDbgAssert(size <= EMAC_TRANSMIT_BUFFERS_SIZE,
"max_lld_get_transmit_descriptor(), #1",
"unexpected size");
if (!link_up)
return NULL;
chSysLock();
tdp = txptr;
chDbgAssert((tdp->w2 & W2_T_USED) && !(tdp->w2 & W2_T_LOCKED),
"max_lld_get_transmit_descriptor(), #2", "buffer not available");
if (!(tdp->w2 & W2_T_USED) || (tdp->w2 & W2_T_LOCKED)) {
chSysUnlock();
return NULL;
}
/*
* Set the buffer size and configuration, the buffer is also marked
* as locked.
*/
tdp->w2 = size | W2_T_LOCKED | W2_T_USED | W2_T_LAST_BUFFER;
if (++txptr >= &td[EMAC_TRANSMIT_BUFFERS]) {
tdp->w2 |= W2_T_WRAP;
txptr = td;
}
chSysUnlock();
return tdp;
}
/**
@ -87,10 +250,15 @@ MACTransmitDescriptor *max_lld_get_transmit_descriptor(MACDriver *macp) {
*
* @param[in] macp pointer to the @p MACDriver object
* @param[in] tdp the pointer to the @p MACTransmitDescriptor structure
* @param[in]
*/
void mac_lld_release_transmit_descriptor(MACDriver *macp,
MACTransmitDescriptor *tdp) {
chSysLock();
tdp->w2 &= ~(W2_T_LOCKED | W2_T_USED);
AT91C_BASE_EMAC->EMAC_NCR |= AT91C_EMAC_TSTART;
chSysUnlock();
}
/**
@ -108,11 +276,12 @@ uint8_t *mac_lld_get_transmit_buffer(MACTransmitDescriptor *tdp) {
* @brief Returns a received frame.
*
* @param[in] macp pointer to the @p MACDriver object
* @param[out szp size of the received frame
* @return A pointer to a @p MACReceiveDescriptor structure or @p NULL if
* the operation timed out, the driver went in stop mode or some
* transient error happened.
* the operation timed out or some transient error happened.
*/
MACReceiveDescriptor *max_lld_get_receive_descriptor(MACDriver *macp) {
MACReceiveDescriptor *max_lld_get_receive_descriptor(MACDriver *macp,
size_t *szp) {
return NULL;
}

View File

@ -42,6 +42,13 @@
/* EMAC specific constants and settings. */
/*===========================================================================*/
/**
* @brief Interrupt priority level for the EMAC device.
*/
#if !defined(EMAC_INTERRUPT_PRIORITY) || defined(__DOXYGEN__)
#define EMAC_INTERRUPT_PRIORITY (AT91C_AIC_PRIOR_HIGHEST - 3)
#endif
#define EMAC_RECEIVE_BUFFERS 24
#define EMAC_RECEIVE_BUFFERS_SIZE 128 /* Do not modify */
#define EMAC_TRANSMIT_BUFFERS MAC_TRANSMIT_DESCRIPTORS
@ -89,8 +96,6 @@
* @brief Structure representing a MAC driver.
*/
typedef struct {
enum {ifStopped = 0,
ifStarted} md_state; /**< @brief Interface status.*/
Semaphore md_tdsem; /**< Transmit semaphore.*/
Semaphore md_rdsem; /**< Receive semaphore.*/
} MACDriver;
@ -117,24 +122,28 @@ typedef struct {
/* External declarations. */
/*===========================================================================*/
/** @cond never*/
extern MACDriver MAC1;
#ifdef __cplusplus
extern "C" {
#endif
void mac_lld_init(void);
void mac_lld_set_address(MACDriver *macp, uint8_t *p);
void mac_lld_start(MACDriver *macp);
void mac_lld_stop(MACDriver *macp);
MACTransmitDescriptor *max_lld_get_transmit_descriptor(MACDriver *macp);
MACTransmitDescriptor *max_lld_get_transmit_descriptor(MACDriver *macp,
size_t size);
void mac_lld_release_transmit_descriptor(MACDriver *macp,
MACTransmitDescriptor *tdp);
uint8_t *mac_lld_get_transmit_buffer(MACTransmitDescriptor *tdp);
MACReceiveDescriptor *max_lld_get_receive_descriptor(MACDriver *macp);
MACReceiveDescriptor *max_lld_get_receive_descriptor(MACDriver *macp,
size_t *szp);
void mac_lld_release_receive_descriptor(MACDriver *macp,
MACReceiveDescriptor *rdp);
uint8_t *mac_lld_get_receive_buffer(MACReceiveDescriptor *rdp);
#ifdef __cplusplus
}
#endif
/** @endcond*/
#endif /* _MAC_LLD_H_ */

View File

@ -38,7 +38,7 @@ void phy_lld_init(void) {
}
/**
* Resets a PHY device.
* @brief Resets a PHY device.
*
* @param[in] macp pointer to the @p MACDriver object
*/

View File

@ -49,10 +49,10 @@
/**
* @brief Pins latched by the PHY at reset.
*/
#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 | \
#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_ECRSDV | AT91C_PB16_ECOL | \
AT91C_PIO_PB26)
/*===========================================================================*/

View File

@ -155,7 +155,7 @@ void emac_init(int prio) {
rxptr = rent;
for (i = 0; i < EMAC_TRANSMIT_BUFFERS; i++) {
tent[i].w1 = (uint32_t)&tbuffers[i * EMAC_TRANSMIT_BUFFERS_SIZE];
tent[i].w2 = EMAC_TRANSMIT_BUFFERS_SIZE | W2_T_USED;
tent[i].w2 = EMAC_TRANSMIT_BUFFERS_SIZE | W2_T_LAST_BUFFER | W2_T_USED;
}
tent[EMAC_TRANSMIT_BUFFERS - 1].w2 |= W2_T_WRAP;
txptr = tent;

View File

@ -41,42 +41,23 @@ void mac_lld_init(void) {
* @param[in] p pointer to a six bytes buffer containing the MAC address. If
* this parameter is set to @p NULL then a system default MAC is
* used.
*
* @note This function should be invoked after the @p macInit() and before
* @p macStart() else the result is unspecified (performed or ignored).
*/
void mac_lld_set_address(MACDriver *macp, uint8_t *p) {
}
/**
* @brief Starts the I/O activity and enters a low power mode.
*
* @param[in] macp pointer to the @p MACDriver object
*/
void mac_lld_start(MACDriver *macp) {
}
/**
* @brief Stops the I/O activity.
*
* @param[in] macp pointer to the @p MACDriver object
*/
void mac_lld_stop(MACDriver *macp) {
}
/**
* @brief Returns a transmission descriptor.
* @details One of the available transmission descriptors is locked and
* returned.
*
* @param[in] macp pointer to the @p MACDriver object
* @param[in] size size of the frame to be transmitted
* @return A pointer to a @p MACTransmitDescriptor structure or @p NULL if
* a descriptor is not available or the driver went in stop mode.
* a descriptor is not available.
*/
MACTransmitDescriptor *max_lld_get_transmit_descriptor(MACDriver *macp) {
MACTransmitDescriptor *max_lld_get_transmit_descriptor(MACDriver *macp,
size_t size) {
return NULL;
}
@ -108,11 +89,12 @@ uint8_t *mac_lld_get_transmit_buffer(MACTransmitDescriptor *tdp) {
* @brief Returns a received frame.
*
* @param[in] macp pointer to the @p MACDriver object
* @param[out szp size of the received frame
* @return A pointer to a @p MACReceiveDescriptor structure or @p NULL if
* the operation timed out, the driver went in stop mode or some
* transient error happened.
* the operation timed out or some transient error happened.
*/
MACReceiveDescriptor *max_lld_get_receive_descriptor(MACDriver *macp) {
MACReceiveDescriptor *max_lld_get_receive_descriptor(MACDriver *macp,
size_t *szp) {
return NULL;
}

View File

@ -46,8 +46,6 @@
* @brief Structure representing a MAC driver.
*/
typedef struct {
enum {ifStopped = 0,
ifStarted} md_state; /**< @brief Interface status.*/
Semaphore md_tdsem; /**< Transmit semaphore.*/
Semaphore md_rdsem; /**< Receive semaphore.*/
} MACDriver;
@ -75,13 +73,13 @@ extern "C" {
#endif
void mac_lld_init(void);
void mac_lld_set_address(MACDriver *macp, uint8_t *p);
void mac_lld_start(MACDriver *macp);
void mac_lld_stop(MACDriver *macp);
MACTransmitDescriptor *max_lld_get_transmit_descriptor(MACDriver *macp);
MACTransmitDescriptor *max_lld_get_transmit_descriptor(MACDriver *macp,
size_t size);
void mac_lld_release_transmit_descriptor(MACDriver *macp,
MACTransmitDescriptor *tdp);
uint8_t *mac_lld_get_transmit_buffer(MACTransmitDescriptor *tdp);
MACReceiveDescriptor *max_lld_get_receive_descriptor(MACDriver *macp);
MACReceiveDescriptor *max_lld_get_receive_descriptor(MACDriver *macp,
size_t *szp);
void mac_lld_release_receive_descriptor(MACDriver *macp,
MACReceiveDescriptor *rdp);
uint8_t *mac_lld_get_receive_buffer(MACReceiveDescriptor *rdp);