2016-05-24 19:40:08 +02:00
|
|
|
#include "inout.h"
|
2016-02-25 21:48:49 +01:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
volatile uint8_t* InOut::getPort( Pin enmPin, Type enmType )
|
|
|
|
{
|
2016-02-25 22:20:24 +01:00
|
|
|
volatile uint8_t *vpui8Port = nullptr;
|
2016-05-24 19:40:08 +02:00
|
|
|
|
2016-02-25 21:48:49 +01:00
|
|
|
if( enmPin == Pin::P_NONE )
|
|
|
|
{
|
|
|
|
return vpui8Port;
|
|
|
|
}
|
2016-05-24 19:40:08 +02:00
|
|
|
|
2016-02-25 21:48:49 +01:00
|
|
|
uint8_t ui8Port = ( static_cast<uint16_t>( enmPin ) >> 4 ) & 0x0F;
|
2016-05-24 19:40:08 +02:00
|
|
|
|
2016-02-25 21:48:49 +01:00
|
|
|
switch( ui8Port )
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
{
|
|
|
|
if( enmType == Type::T_PIN )
|
|
|
|
{
|
|
|
|
vpui8Port = PORT_PINA;
|
|
|
|
}
|
|
|
|
else if( enmType == Type::T_DDR )
|
|
|
|
{
|
|
|
|
vpui8Port = PORT_DDRA;
|
|
|
|
}
|
|
|
|
else if( enmType == Type::T_PORT )
|
|
|
|
{
|
|
|
|
vpui8Port = PORT_PORTA;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
{
|
|
|
|
if( enmType == Type::T_PIN )
|
|
|
|
{
|
|
|
|
vpui8Port = PORT_PINB;
|
|
|
|
}
|
|
|
|
else if( enmType == Type::T_DDR )
|
|
|
|
{
|
|
|
|
vpui8Port = PORT_DDRB;
|
|
|
|
}
|
|
|
|
else if( enmType == Type::T_PORT )
|
|
|
|
{
|
|
|
|
vpui8Port = PORT_PORTB;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
{
|
|
|
|
if( enmType == Type::T_PIN )
|
|
|
|
{
|
|
|
|
vpui8Port = PORT_PINC;
|
|
|
|
}
|
|
|
|
else if( enmType == Type::T_DDR )
|
|
|
|
{
|
|
|
|
vpui8Port = PORT_DDRC;
|
|
|
|
}
|
|
|
|
else if( enmType == Type::T_PORT )
|
|
|
|
{
|
|
|
|
vpui8Port = PORT_PORTC;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
{
|
|
|
|
if( enmType == Type::T_PIN )
|
|
|
|
{
|
|
|
|
vpui8Port = PORT_PIND;
|
|
|
|
}
|
|
|
|
else if( enmType == Type::T_DDR )
|
|
|
|
{
|
|
|
|
vpui8Port = PORT_DDRD;
|
|
|
|
}
|
|
|
|
else if( enmType == Type::T_PORT )
|
|
|
|
{
|
|
|
|
vpui8Port = PORT_PORTD;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return vpui8Port;
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
uint8_t InOut::getPin( Pin enmPin )
|
|
|
|
{
|
|
|
|
return static_cast<uint16_t>( enmPin ) & 0x0F;
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
2016-02-25 22:20:24 +01:00
|
|
|
void InOut::setPinDirection( Pin enmPin, Dir enmDir, bool bPullup )
|
2016-02-25 21:48:49 +01:00
|
|
|
{
|
|
|
|
if( enmPin == Pin::P_NONE )
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2016-05-24 19:40:08 +02:00
|
|
|
|
2016-02-25 22:20:24 +01:00
|
|
|
volatile uint8_t *vpui8PortDir = getPort( enmPin, Type::T_DDR );
|
|
|
|
volatile uint8_t *vpui8PortOut = getPort( enmPin, Type::T_PORT );
|
2016-02-25 21:48:49 +01:00
|
|
|
uint8_t ui8Pin = getPin( enmPin );
|
2016-05-24 19:40:08 +02:00
|
|
|
|
2016-02-25 21:48:49 +01:00
|
|
|
setPinDirection( vpui8PortDir, ui8Pin, enmDir );
|
2016-05-24 19:40:08 +02:00
|
|
|
|
2016-02-25 22:20:24 +01:00
|
|
|
if( enmDir == Dir::D_IN )
|
2016-02-25 21:48:49 +01:00
|
|
|
{
|
2016-02-25 22:20:24 +01:00
|
|
|
writePin( vpui8PortOut, ui8Pin, bPullup );
|
2016-02-25 21:48:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
bool InOut::readPin( Pin enmPin )
|
|
|
|
{
|
|
|
|
if( enmPin == Pin::P_NONE )
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2016-05-24 19:40:08 +02:00
|
|
|
|
2016-02-25 22:20:24 +01:00
|
|
|
volatile uint8_t *vpui8Port = getPort( enmPin, Type::T_PIN );
|
2016-02-25 21:48:49 +01:00
|
|
|
uint8_t ui8Pin = getPin( enmPin );
|
2016-05-24 19:40:08 +02:00
|
|
|
|
2016-02-25 21:48:49 +01:00
|
|
|
return readPin( vpui8Port, ui8Pin );
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
void InOut::writePin( Pin enmPin, bool bValue )
|
|
|
|
{
|
|
|
|
if( enmPin == Pin::P_NONE )
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2016-05-24 19:40:08 +02:00
|
|
|
|
2016-02-25 22:20:24 +01:00
|
|
|
volatile uint8_t *vpui8Port = getPort( enmPin, Type::T_PORT );
|
2016-02-25 21:48:49 +01:00
|
|
|
uint8_t ui8Pin = getPin( enmPin );
|
|
|
|
|
|
|
|
writePin( vpui8Port, ui8Pin, bValue );
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
uint8_t InOut::readPort( Pin enmPortPin )
|
|
|
|
{
|
|
|
|
if( enmPortPin == Pin::P_NONE )
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2016-05-24 19:40:08 +02:00
|
|
|
|
2016-02-25 22:20:24 +01:00
|
|
|
volatile uint8_t *vpui8Port = getPort( enmPortPin, Type::T_PIN );
|
2016-05-24 19:40:08 +02:00
|
|
|
|
2016-02-25 21:48:49 +01:00
|
|
|
return readPort( vpui8Port );
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
void InOut::writePort( Pin enmPortPin, uint8_t ui8Value )
|
|
|
|
{
|
|
|
|
if( enmPortPin == Pin::P_NONE )
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2016-05-24 19:40:08 +02:00
|
|
|
|
2016-02-25 22:20:24 +01:00
|
|
|
volatile uint8_t *vpui8Port = getPort( enmPortPin, Type::T_PORT );
|
2016-02-25 21:48:49 +01:00
|
|
|
|
|
|
|
writePort( vpui8Port, ui8Value );
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
InOutPin::InOutPin()
|
|
|
|
{
|
|
|
|
setPin( InOut::Pin::P_NONE );
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
InOutPin::InOutPin( InOut::Pin enmPin )
|
|
|
|
{
|
|
|
|
setPin( enmPin );
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
InOutPin::~InOutPin()
|
|
|
|
{}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
void InOutPin::setPin( InOut::Pin enmPin )
|
|
|
|
{
|
|
|
|
m_vpui8Input = InOut::getPort( enmPin, InOut::Type::T_PIN );
|
|
|
|
m_vpui8Dir = InOut::getPort( enmPin, InOut::Type::T_DDR );
|
|
|
|
m_vpui8Output = InOut::getPort( enmPin, InOut::Type::T_PORT );
|
2016-05-24 19:40:08 +02:00
|
|
|
|
2016-02-25 21:48:49 +01:00
|
|
|
m_ui8Pin = InOut::getPin( enmPin );
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
InOutPort::InOutPort()
|
|
|
|
{
|
|
|
|
setPort( InOut::Pin::P_NONE );
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
InOutPort::InOutPort( InOut::Pin enmPin )
|
|
|
|
{
|
|
|
|
setPort( enmPin );
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
InOutPort::~InOutPort()
|
|
|
|
{}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
void InOutPort::setPort( InOut::Pin enmPortPin )
|
|
|
|
{
|
|
|
|
m_vpui8Input = InOut::getPort( enmPortPin, InOut::Type::T_PIN );
|
|
|
|
m_vpui8Dir = InOut::getPort( enmPortPin, InOut::Type::T_DDR );
|
|
|
|
m_vpui8Output = InOut::getPort( enmPortPin, InOut::Type::T_PORT );
|
2016-05-24 19:40:08 +02:00
|
|
|
}
|