PICoHolic
Joined: 04 Jan 2005 Posts: 224
|
MDIO CL22 master using bit-banging SPI |
Posted: Wed Jul 25, 2018 7:09 am |
|
|
mdio.h
Code: |
#ifndef _MDIO_H_
#define _MDIO_H
///////////////////////////////////////////////////////////////////////////////
//IO def:
#define MDC_PIN PIN_C0
#define MDIO_PIN PIN_C1
#define DriveMDC() output_drive(MDC_PIN)
#define SetMDC() output_high(MDC_PIN)
#define ClearMDC() output_low(MDC_PIN)
#define DriveMDIO() output_drive(MDIO_PIN)
#define SetMDIO() output_high(MDIO_PIN)
#define ClearMDIO() output_low(MDIO_PIN)
#define FloatMDIO() output_float(MDIO_PIN)
#define GetMDIO() input(MDIO_PIN)
//MDIO over SPI
#define BITRATE 500000
#use SPI(master, baud=BITRATE, bits=32, DI=MDIO_PIN, DO=MDIO_PIN, CLK=MDC_PIN, mode=0, stream=MDIO_SPI32, force_sw)
#use SPI(master, baud=BITRATE, bits=16, DI=MDIO_PIN, DO=MDIO_PIN, CLK=MDC_PIN, mode=2, stream=MDIO_SPI16, force_sw)
#use SPI(master, baud=BITRATE, bits=14, DI=MDIO_PIN, DO=MDIO_PIN, CLK=MDC_PIN, mode=0, stream=MDIO_SPI14, force_sw)
#use SPI(master, baud=BITRATE, bits=1, DI=MDIO_PIN, DO=MDIO_PIN, CLK=MDC_PIN, mode=2, stream=MDIO_SPI1, force_sw)
///////////////////////////////////////////////////////////////////////////////
//Prototypes:
void MDIO_init(void);
void MDIO_write_22(unsigned int8 PHYAddress, unsigned int8 RegAddress, unsigned int16 Data);
int1 MDIO_read_22(unsigned int8 PHYAddress, unsigned int8 RegAddress, unsigned int16 *Data);
///////////////////////////////////////////////////////////////////////////////
#endif
|
mdio.c
Code: |
#include "mdio.h"
///////////////////////////////////////////////////////////////////////////////
/**
*
*
* @author m_richa (03/09/18)
*/
void MDIO_init(void)
{
FloatMDIO();
ClearMDC();
DriveMDC();
}
///////////////////////////////////////////////////////////////////////////////
/**
*
*
* @author m_richa (03/09/18)
*
* @param PHYAddress
* @param RegAddress
* @param Data
*/
void MDIO_write_22(unsigned int8 PHYAddress, unsigned int8 RegAddress, unsigned int16 Data)
{
unsigned int32 Preamble = 0xFFFFFFFF;
unsigned int32 RawOut = ((unsigned int32)01 << 30) | \
((unsigned int32)01 << 28) | \
((unsigned int32)PHYAddress << 23) | \
((unsigned int32)RegAddress << 18) | \
((unsigned int32)02 << 16) | \
(unsigned int32)Data;
SetMDIO();
DriveMDIO();
//Preamble:
spi_xfer(MDIO_SPI32, Preamble);
//Raw data:
spi_xfer(MDIO_SPI32, RawOut);
//Release data:
FloatMDIO();
}
///////////////////////////////////////////////////////////////////////////////
/**
*
*
* @author m_richa (03/09/18)
*
* @param PHYAddress
* @param RegAddress
* @param Data
*
* @return int1
*/
int1 MDIO_read_22(unsigned int8 PHYAddress, unsigned int8 RegAddress, unsigned int16 *Data)
{
unsigned int32 Preamble = 0xFFFFFFFF;
unsigned int16 RawOut = ((unsigned int16)01 << 12) | \
((unsigned int16)02 << 10) | \
((unsigned int16)PHYAddress << 5) | \
(unsigned int16)RegAddress;
unsigned int8 TA=0;
SetMDIO();
DriveMDIO();
//Preamble:
spi_xfer(MDIO_SPI32, Preamble);
//Raw data:
spi_xfer(MDIO_SPI14, RawOut);
//Release data and set clock
FloatMDIO();
SetMDC();
TA = spi_xfer(MDIO_SPI1,0);
*Data = spi_xfer(MDIO_SPI16,0);
//Clear clock:
ClearMDC();
return (!bit_test(TA, 0)); //return TA state
}
///////////////////////////////////////////////////////////////////////////////
|
MDIO Background:
https://www.totalphase.com/support/articles/200349206-MDIO-Background |
|