2000-02-05 22:37:44 +00:00
//=========================================================================//
// //
// PonyProg - Serial Device Programmer //
// //
2020-02-05 17:23:29 +01:00
// Copyright (C) 1997-2020 Claudio Lanconelli //
2000-02-05 22:37:44 +00:00
// //
2007-04-20 10:58:20 +00:00
// http://ponyprog.sourceforge.net //
2000-02-05 22:37:44 +00:00
// //
//-------------------------------------------------------------------------//
// //
// This program 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 version2 of //
// the License, or (at your option) any later version. //
// //
// This program 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 //
2017-10-08 22:28:35 +02:00
// along with this program (see LICENSE); if not, write to the //
2000-02-05 22:37:44 +00:00
// Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. //
// //
//=========================================================================//
2017-04-18 01:25:22 +02:00
# ifndef _SPIBUS_H
# define _SPIBUS_H
2000-02-05 22:37:44 +00:00
# include "busio.h"
# include "pgminter.h"
class SPIBus : public BusIO
{
2018-10-16 18:42:23 +02:00
public :
2019-03-29 18:47:46 +01:00
SPIBus ( BusInterface * ptr = 0 , bool cpha = false , bool cpol = false ) ;
2000-02-05 22:37:44 +00:00
virtual ~ SPIBus ( ) ;
virtual int Reset ( ) ;
void SetDelay ( ) ;
2019-03-23 01:26:31 +01:00
2008-01-27 23:39:11 +00:00
void SetFallingPhase ( bool cpha )
2017-04-18 01:25:22 +02:00
{
2019-03-23 01:26:31 +01:00
m_cpha = cpha ;
}
bool GetFallingPhase ( )
{
return m_cpha ;
}
void SetClockPolarity ( bool cpol )
{
m_cpol = cpol ;
}
bool GetClockPolarity ( )
{
return m_cpol ;
}
void SetMode ( int mode )
{
m_cpol = ( ( mode & SPIMODE_CPOL ) ! = 0 ) ;
m_cpha = ( ( mode & SPIMODE_CPHA ) ! = 0 ) ;
}
void SetMode ( bool cpha , bool cpol )
{
m_cpol = cpol ;
m_cpha = cpha ;
}
int GetMode ( )
{
int mode = 0 ;
if ( m_cpol )
2019-04-10 20:53:08 +02:00
{
2019-03-23 01:26:31 +01:00
mode | = SPIMODE_CPOL ;
2019-04-10 20:53:08 +02:00
}
2019-03-23 01:26:31 +01:00
if ( m_cpha )
2019-04-10 20:53:08 +02:00
{
2019-03-23 01:26:31 +01:00
mode | = SPIMODE_CPHA ;
2019-04-10 20:53:08 +02:00
}
2019-03-23 01:26:31 +01:00
return mode ;
2017-04-18 01:25:22 +02:00
}
2000-02-05 22:37:44 +00:00
2018-10-16 18:42:23 +02:00
protected :
2000-02-05 22:37:44 +00:00
int SendDataByte ( int by ) ;
int RecDataByte ( ) ;
void SetReset ( )
2017-04-18 01:25:22 +02:00
{
busI - > SetControlLine ( 1 ) ;
}
2000-02-05 22:37:44 +00:00
void ClearReset ( )
2017-04-18 01:25:22 +02:00
{
busI - > SetControlLine ( 0 ) ;
}
2000-02-05 22:37:44 +00:00
void setSCK ( )
2017-04-18 01:25:22 +02:00
{
busI - > SetClock ( 1 ) ;
}
2000-02-05 22:37:44 +00:00
void clearSCK ( )
2017-04-18 01:25:22 +02:00
{
busI - > SetClock ( 0 ) ;
}
2000-02-05 22:37:44 +00:00
int SendDataBit ( int b ) ;
int RecDataBit ( ) ;
2017-04-29 01:56:50 +02:00
private :
2019-03-23 01:26:31 +01:00
2000-02-05 22:37:44 +00:00
void bitMOSI ( int b )
2017-04-18 01:25:22 +02:00
{
busI - > SetDataOut ( b ) ;
}
2000-02-05 22:37:44 +00:00
void setMOSI ( )
2017-04-18 01:25:22 +02:00
{
busI - > SetDataOut ( 1 ) ;
}
2000-02-05 22:37:44 +00:00
void clearMOSI ( )
2017-04-18 01:25:22 +02:00
{
busI - > SetDataOut ( 0 ) ;
}
2000-02-05 22:37:44 +00:00
int getMISO ( ) const
2017-04-18 01:25:22 +02:00
{
return busI - > GetDataIn ( ) ;
}
2008-01-25 22:02:43 +00:00
2019-03-23 01:26:31 +01:00
bool m_cpol ; //clock polarity
bool m_cpha ; //rising edge vs fall edge sample
2000-02-05 22:37:44 +00:00
} ;
# endif