CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

MDIO CL22 master using bit-banging SPI

 
Post new topic   Reply to topic    CCS Forum Index -> Code Library
View previous topic :: View next topic  
Author Message
PICoHolic



Joined: 04 Jan 2005
Posts: 224

View user's profile Send private message

MDIO CL22 master using bit-banging SPI
PostPosted: Wed Jul 25, 2018 7:09 am     Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> Code Library All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group