📄 eth97j60.c
字号:
i.v[1] = 0x01;
MaskPtr++;
}
}
// Calculate and set the DMA end address
i.Val = TXSTART + (WORD)UnmaskedPatternLen - 1;
EDMAND = i.Val;
// Calculate the checksum on the given pattern using the DMA module
ECON1bits.CSUMEN = 1;
ECON1bits.DMAST = 1;
while(ECON1bits.DMAST);
// Make certain that the PM filter isn't enabled while it is
// being reconfigured.
ERXFCON = ERXFCON_UCEN | ERXFCON_CRCEN | ERXFCON_BCEN;
// Get the calculated DMA checksum and store it in the PM
// checksum registers
EPMCS = EDMACS;
// Set the Pattern Match offset and 8 byte mask
EPMO = PatternOffset;
for(i.Val = 0, PMRegister = &EPMM0; i.Val < 8; i.Val++)
{
*PMRegister++ = *PatternMask++;
}
// Begin using the new Pattern Match filter instead of the
// broadcast filter
ERXFCON = ERXFCON_UCEN | ERXFCON_CRCEN | ERXFCON_PMEN;
}//end MACSetPMFilter
/******************************************************************************
* Function: void MACDisablePMFilter(void)
*
* PreCondition: None
*
* Input: None
*
* Output: None
*
* Side Effects: None
*
* Overview: MACDisablePMFilter disables the Pattern Match receive
* filter (if enabled) and returns to the default filter
* configuration of: CRC AND (Unicast OR Broadcast).
*
* Note: None
*****************************************************************************/
void MACDisablePMFilter(void)
{
ERXFCON = ERXFCON_UCEN | ERXFCON_CRCEN | ERXFCON_BCEN;
return;
}//end MACDisablePMFilter
#endif // end of MAC_FILTER_BROADCASTS specific code
/******************************************************************************
* Function: BYTE MACGet()
*
* PreCondition: ERDPT must point to the place to read from.
*
* Input: None
*
* Output: Byte read from the Ethernet's buffer RAM
*
* Side Effects: None
*
* Overview: MACGet returns the byte pointed to by ERDPT and
* increments ERDPT so MACGet() can be called again. The
* increment will follow the receive buffer wrapping boundary.
*
* Note: For better performance, implement this function as a macro:
* #define MACGet() (EDATA)
*****************************************************************************/
BYTE MACGet()
{
return EDATA;
}//end MACGet
/******************************************************************************
* Function: WORD MACGetArray(BYTE *val, WORD len)
*
* PreCondition: ERDPT must point to the place to read from.
*
* Input: *val: Pointer to storage location
* len: Number of bytes to read from the data buffer.
*
* Output: Byte(s) of data read from the data buffer.
*
* Side Effects: None
*
* Overview: Reads several sequential bytes from the data buffer
* and places them into local memory. ERDPT is incremented
* after each byte, following the same rules as MACGet().
*
* Note: None
*****************************************************************************/
WORD MACGetArray(BYTE *val, WORD len)
{
WORD i = len;
while(i--)
{
*val++ = EDATA;
};
return len;
}//end MACGetArray
/******************************************************************************
* Function: void MACPut(BYTE val)
*
* PreCondition: EWRPT must point to the location to begin writing.
*
* Input: Byte to write into the Ethernet buffer memory
*
* Output: None
*
* Side Effects: None
*
* Overview: Writes to the EDATA register, which will indirectly
* increment EWRPTH:EWRPTL.
*
* Note: For better performance, implement this function as a macro:
* #define MACPut(a) EDATA = a
*****************************************************************************/
void MACPut(BYTE val)
{
EDATA = val;
}//end MACPut
/******************************************************************************
* Function: void MACPutArray(BYTE *val, WORD len)
*
* PreCondition: EWRPT must point to the location to begin writing.
*
* Input: *val: Pointer to source of bytes to copy.
* len: Number of bytes to write to the data buffer.
*
* Output: None
*
* Side Effects: None
*
* Overview: MACPutArray writes several sequential bytes to the
* Ethernet buffer RAM. It performs faster than multiple MACPut()
* calls. EWRPT is incremented by len.
*
* Note: None
*****************************************************************************/
void MACPutArray(BYTE *val, WORD len)
{
while(len--)
EDATA = *val++;
}//end MACPutArray
/******************************************************************************
* Function: ReadPHYReg
*
* PreCondition: Ethernet module must be enabled (ECON1.ETHEN = 1).
*
* Input: Address of the PHY register to read from.
*
* Output: 16 bits of data read from the PHY register.
*
* Side Effects: None
*
* Overview: ReadPHYReg performs an MII read operation. While in
* progress, it simply polls the MII BUSY bit wasting time
* (10.24us).
*
* Note: None
*****************************************************************************/
PHYREG ReadPHYReg(BYTE Register)
{
PHYREG Result;
// Set the right address and start the register read operation
MIREGADR = Register; Nop();
MICMD = MICMD_MIIRD; Nop();
// Loop to wait until the PHY register has been read through the MII
// This requires 10.24us
while(MISTATbits.BUSY);
// Stop reading
MICMD = 0x00; Nop();
// Obtain results and return
Result.VAL.v[0] = MIRDL;
Nop();
Result.VAL.v[1] = MIRDH;
return Result;
}//end ReadPHYReg
/******************************************************************************
* Function: WritePHYReg
*
* PreCondition: Ethernet module must be enabled (ECON1.ETHEN = 1).
*
* Input: Address of the PHY register to write to.
* 16 bits of data to write to PHY register.
*
* Output: None
*
* Side Effects: None
*
* Overview: WritePHYReg performs an MII write operation. While in
* progress, it simply polls the MII BUSY bit wasting time
* (10.24us).
*
* Note: None
*****************************************************************************/
void WritePHYReg(BYTE Register, WORD Data)
{
// Write the register address
MIREGADR = Register; Nop();
// Write the data
// Order is important: write low byte first, high byte last
MIWRL = ((WORD_VAL*)&Data)->v[0]; Nop();
MIWRH = ((WORD_VAL*)&Data)->v[1]; Nop();
// Wait until the PHY register has been written
// This operation requires 10.24us
while(MISTATbits.BUSY);
}//end WritePHYReg
/******************************************************************************
* Function: void MACSetDuplex(DUPLEX DuplexState)
*
* PreCondition: None
*
* Input: Member of DUPLEX enum:
* FULL: Set full duplex mode
* HALF: Set half duplex mode
*
* Output: None
*
* Side Effects: RX logic is enabled (RX_EN set), even if it wasn't set
* prior to calling this function.
*
* Overview: Disables RX, TX logic, sets MAC up for full duplex
* operation, sets PHY up for full duplex operation, and
* reenables RX logic. The back-to-back inter-packet gap
* register (MACBBIPG) is updated to maintain a 9.6us gap.
*
* Note: If a packet is being transmitted or received while this
* function is called, it will be aborted.
*****************************************************************************/
void MACSetDuplex(DUPLEX DuplexState)
{
PHYREG PhyReg;
BYTE Temp;
// Disable receive logic and abort any packets currently being transmitted
ECON1bits.RXEN = 0;
ECON1bits.TXRTS = 0;
// Set the PHY to the proper duplex mode
PhyReg = ReadPHYReg(PHCON1);
PhyReg.PHCON1bits.PDPXMD = DuplexState;
WritePHYReg(PHCON1, PhyReg.Val);
// Set the MAC to the proper duplex mode (without using a read-modify-write
// type of instruction)
Temp = MACON3;
Temp &= ~MACON3_FULDPX;
if(DuplexState)
{
Temp |= (MACON3_FULDPX);
}
MACON3 = Temp;
Nop();
// Set the back-to-back inter-packet gap time to IEEE specified
// requirements. The meaning of the MABBIPG value changes with the duplex
// state, so it must be updated in this function.
// In full duplex, 0x15 represents 9.6us; 0x12 is 9.6us in half duplex
MABBIPG = DuplexState ? 0x15 : 0x12;
// Reenable receive logic
ECON1bits.RXEN = 1;
}//end MACSetDuplex
/******************************************************************************
* Function: void MACPowerDown(void)
*
* PreCondition: None
*
* Input: None
*
* Output: None
*
* Side Effects: None
*
* Overview: MACPowerDown disables the Ethernet module.
* All MAC and PHY registers should not be accessed.
*
* Note: Normally, this function would be called before putting the
* PIC to sleep. If a packet is being transmitted while this
* function is called, this function will block until it is
* it complete. If anything is being received, it will be
* completed.
*
* The Ethernet module will continue to draw significant
* power in sleep mode if this function is not called first.
*****************************************************************************/
void MACPowerDown(void)
{
// Disable packet reception
ECON1bits.RXEN = 0;
// Make sure any last packet which was in-progress when RXEN was cleared
// is completed
while(ESTATbits.RXBUSY);
// If a packet is being transmitted, wait for it to finish
while(ECON1bits.TXRTS);
// Disable the Ethernet module
ECON2bits.ETHEN = 0;
}//end MACPowerDown
/******************************************************************************
* Function: void MACPowerUp(void)
*
* PreCondition: None
*
* Input: None
*
* Output: None
*
* Side Effects: None
*
* Overview: MACPowerUp returns the Ethernet module back to normal operation
* after a previous call to MACPowerDown(). Calling this
* function when already powered up will have no effect.
*
* Note: If a link partner is present, it will take 10s of
* milliseconds before a new link will be established after
* waking up. While not linked, packets which are
* transmitted will most likely be lost. MACIsLinked() can
* be called to determine if a link is established.
*****************************************************************************/
void MACPowerUp(void)
{
// Power up the Ethernet module
ECON2bits.ETHEN = 1;
// Wait at least 1ms for the PHY to stabilize
Delay10us(100);
// Enable packet reception
ECON1bits.RXEN = 1;
}//end MACPowerUp
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -