commit e2e45becc0bc259cf67a5d2a199bcd695af2a100 Author: BlackMark Date: Thu Feb 25 21:48:49 2016 +0100 Added everything to git diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..38e47ad --- /dev/null +++ b/.gitattributes @@ -0,0 +1,8 @@ +*.h eol=lf +*.hpp eol=lf +*.c eol=lf +*.cpp eol=lf +*.vcxproj* eol=crlf +*.cppproj eol=crlf +*.sln eol=crlf +*.atsln eol=crlf diff --git a/InOut.atsln b/InOut.atsln new file mode 100644 index 0000000..574935a --- /dev/null +++ b/InOut.atsln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Atmel Studio Solution File, Format Version 11.00 +VisualStudioVersion = 14.0.23107.0 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{E66E83B9-2572-4076-B26E-6BE79FF3018A}") = "InOut", "InOut\InOut.cppproj", "{DCE6C7E3-EE26-4D79-826B-08594B9AD897}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|AVR = Debug|AVR + Release|AVR = Release|AVR + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {DCE6C7E3-EE26-4D79-826B-08594B9AD897}.Debug|AVR.ActiveCfg = Debug|AVR + {DCE6C7E3-EE26-4D79-826B-08594B9AD897}.Debug|AVR.Build.0 = Debug|AVR + {DCE6C7E3-EE26-4D79-826B-08594B9AD897}.Release|AVR.ActiveCfg = Release|AVR + {DCE6C7E3-EE26-4D79-826B-08594B9AD897}.Release|AVR.Build.0 = Release|AVR + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/InOut/Clock.h b/InOut/Clock.h new file mode 100644 index 0000000..cbdc6c5 --- /dev/null +++ b/InOut/Clock.h @@ -0,0 +1,14 @@ +/* +* Copyright (c) by BlackMark 2015 +* Date 24/11/2015 +* Version 1.1 +*/ + +#ifndef CLOCK_H +#define CLOCK_H + +#define F_CPU 16000000 + +#include + +#endif \ No newline at end of file diff --git a/InOut/InOut.cpp b/InOut/InOut.cpp new file mode 100644 index 0000000..8367531 --- /dev/null +++ b/InOut/InOut.cpp @@ -0,0 +1,225 @@ +#include "InOut.h" + +////////////////////////////////////////////////////////////////////////// +volatile uint8_t* InOut::getPort( Pin enmPin, Type enmType ) +{ + volatile uint8_t* vpui8Port = nullptr; + + if( enmPin == Pin::P_NONE ) + { + return vpui8Port; + } + + uint8_t ui8Port = ( static_cast( enmPin ) >> 4 ) & 0x0F; + + 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( enmPin ) & 0x0F; +} + +////////////////////////////////////////////////////////////////////////// +void InOut::setPinDirection( Pin enmPin, Dir enmDir, bool bPullup /* = true */ ) +{ + if( enmPin == Pin::P_NONE ) + { + return; + } + + volatile uint8_t* vpui8PortDir = getPort( enmPin, Type::T_DDR ); + volatile uint8_t* vpui8PortOut = getPort( enmPin, Type::T_PORT ); + uint8_t ui8Pin = getPin( enmPin ); + + setPinDirection( vpui8PortDir, ui8Pin, enmDir ); + + if( enmDir == Dir::D_IN && bPullup ) + { + writePin( vpui8PortOut, ui8Pin, true ); + } + else if( enmDir == Dir::D_IN && !bPullup ) + { + writePin( vpui8PortOut, ui8Pin, false ); + } +} + +////////////////////////////////////////////////////////////////////////// +bool InOut::readPin( Pin enmPin ) +{ + if( enmPin == Pin::P_NONE ) + { + return false; + } + + volatile uint8_t* vpui8Port = getPort( enmPin, Type::T_PIN ); + uint8_t ui8Pin = getPin( enmPin ); + + return readPin( vpui8Port, ui8Pin ); +} + +////////////////////////////////////////////////////////////////////////// +void InOut::writePin( Pin enmPin, bool bValue ) +{ + if( enmPin == Pin::P_NONE ) + { + return; + } + + volatile uint8_t* vpui8Port = getPort( enmPin, Type::T_PORT ); + uint8_t ui8Pin = getPin( enmPin ); + + writePin( vpui8Port, ui8Pin, bValue ); +} + +////////////////////////////////////////////////////////////////////////// +uint8_t InOut::readPort( Pin enmPortPin ) +{ + if( enmPortPin == Pin::P_NONE ) + { + return 0; + } + + volatile uint8_t* vpui8Port = getPort( enmPortPin, Type::T_PIN ); + + return readPort( vpui8Port ); +} + +////////////////////////////////////////////////////////////////////////// +void InOut::writePort( Pin enmPortPin, uint8_t ui8Value ) +{ + if( enmPortPin == Pin::P_NONE ) + { + return; + } + + volatile uint8_t* vpui8Port = getPort( enmPortPin, Type::T_PORT ); + + 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 ); + + 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 ); +} \ No newline at end of file diff --git a/InOut/InOut.cppproj b/InOut/InOut.cppproj new file mode 100644 index 0000000..23efc30 --- /dev/null +++ b/InOut/InOut.cppproj @@ -0,0 +1,184 @@ + + + + 2.0 + 7.0 + com.Atmel.AVRGCC8.CPP + dce6c7e3-ee26-4d79-826b-08594b9ad897 + ATmega328P + none + Executable + CPP + $(MSBuildProjectName) + .elf + $(MSBuildProjectDirectory)\$(Configuration) + InOut + InOut + InOut + Native + true + false + true + true + 0x20000000 + + true + exception_table + 2 + 0 + + + + + + + + + + + + + + + + 0x1E9705 + + + + 125000 + + ISP + + com.atmel.avrdbg.tool.stk500 + + + STK500 + + ISP + 125000 + + + + + -mmcu=atmega328p -B "%24(PackRepoDir)\Atmel\ATmega_DFP\1.0.91\gcc\dev\atmega328p" + True + True + True + True + True + False + True + True + + + NDEBUG + + + + + %24(PackRepoDir)\Atmel\ATmega_DFP\1.0.91\include + + + Optimize for size (-Os) + True + True + True + True + True + + + NDEBUG + + + + + %24(PackRepoDir)\Atmel\ATmega_DFP\1.0.91\include + + + Optimize for size (-Os) + True + True + True + True + -Wextra -std=c++11 + + + libm + + + + + echo "C:\avrdude-6.2\avrdude.exe" -v -p$(avrdevice) %%* -Uflash:w:"$(OutputDirectory)\$(Name).hex":i > "$(MSBuildProjectDirectory)\avrdude.bat" + + + + + -mmcu=atmega328p -B "%24(PackRepoDir)\Atmel\ATmega_DFP\1.0.91\gcc\dev\atmega328p" + True + True + True + True + True + False + True + True + + + DEBUG + + + + + %24(PackRepoDir)\Atmel\ATmega_DFP\1.0.91\include + + + Optimize (-O1) + True + True + Default (-g2) + True + True + True + + + DEBUG + + + + + %24(PackRepoDir)\Atmel\ATmega_DFP\1.0.91\include + + + Optimize (-O1) + True + True + Default (-g2) + True + True + -Wextra -std=c++11 + + + libm + + + Default (-Wa,-g) + + + echo "C:\avrdude-6.2\avrdude.exe" -v -p$(avrdevice) %%* -Uflash:w:"$(OutputDirectory)\$(Name).hex":i > "$(MSBuildProjectDirectory)\avrdude.bat" + + + + compile + + + compile + + + compile + + + compile + + + + \ No newline at end of file diff --git a/InOut/InOut.h b/InOut/InOut.h new file mode 100644 index 0000000..20a0efe --- /dev/null +++ b/InOut/InOut.h @@ -0,0 +1,306 @@ +/* +* Copyright (c) by BlackMark 2015-2016 +* Date 05/01/2016 +* Version 2.3 +*/ + +#ifndef INOUT_H +#define INOUT_H + +#include +#include + +#define AVR_DIP40 defined (__AVR_ATmega32A__) || defined (__AVR_ATmega644P__) || defined (__AVR_ATmega1284P__) +#define AVR_DIP28 defined (__AVR_ATmega8__) || defined (__AVR_ATmega168A__) || defined (__AVR_ATmega328P__) +#define AVR_DIP8 defined (__AVR_ATtiny13A__) + +#if AVR_DIP40 + #define PORT_PINA &PINA + #define PORT_DDRA &DDRA + #define PORT_PORTA &PORTA +#else + #define PORT_PINA nullptr + #define PORT_DDRA nullptr + #define PORT_PORTA nullptr +#endif + +#if AVR_DIP40 || AVR_DIP28 || AVR_DIP8 + #define PORT_PINB &PINB + #define PORT_DDRB &DDRB + #define PORT_PORTB &PORTB +#else + #define PORT_PINB nullptr + #define PORT_DDRB nullptr + #define PORT_PORTB nullptr +#endif + +#if AVR_DIP40 || AVR_DIP28 + #define PORT_PINC &PINC + #define PORT_DDRC &DDRC + #define PORT_PORTC &PORTC +#else + #define PORT_PINC nullptr + #define PORT_DDRC nullptr + #define PORT_PORTC nullptr +#endif + +#if AVR_DIP40 || AVR_DIP28 + #define PORT_PIND &PIND + #define PORT_DDRD &DDRD + #define PORT_PORTD &PORTD +#else + #define PORT_PIND nullptr + #define PORT_DDRD nullptr + #define PORT_PORTD nullptr +#endif + +class InOut +{ +public: + enum class Pin + { + P_NONE = -1, +#if AVR_DIP40 + P_A0 = 0x00, + P_A1 = 0x01, + P_A2 = 0x02, + P_A3 = 0x03, + P_A4 = 0x04, + P_A5 = 0x05, + P_A6 = 0x06, + P_A7 = 0x07, +#endif +#if AVR_DIP40 || AVR_DIP28 || AVR_DIP8 + P_B0 = 0x10, + P_B1 = 0x11, + P_B2 = 0x12, + P_B3 = 0x13, + P_B4 = 0x14, + P_B5 = 0x15, +#endif +#if AVR_DIP40 || AVR_DIP28 + P_B6 = 0x16, + P_B7 = 0x17, + P_C0 = 0x20, + P_C1 = 0x21, + P_C2 = 0x22, + P_C3 = 0x23, + P_C4 = 0x24, + P_C5 = 0x25, + P_C6 = 0x26, +#endif +#if AVR_DIP40 + P_C7 = 0x27, +#endif +#if AVR_DIP40 || AVR_DIP28 + P_D0 = 0x30, + P_D1 = 0x31, + P_D2 = 0x32, + P_D3 = 0x33, + P_D4 = 0x34, + P_D5 = 0x35, + P_D6 = 0x36, + P_D7 = 0x37, +#endif + }; + + enum class Dir + { + D_IN = 0, + D_OUT = 1 + }; + + enum class Type + { + T_PIN = 0, + T_DDR = 1, + T_PORT = 2 + }; + + static volatile uint8_t* getPort( Pin enmPin, Type enmType ); + static uint8_t getPin( Pin enmPin ); + + static void setPinDirection( Pin enmPin, Dir enmDir, bool bPullup = true ); + static bool readPin( Pin enmPin ); + static void writePin( Pin enmPin, bool bValue ); + + static uint8_t readPort( Pin enmPortPin ); + static void writePort( Pin enmPortPin, uint8_t ui8Value ); + + ////////////////////////////////////////////////////////////////////////// + static inline void setPinDirection( volatile uint8_t* vpui8Port, uint8_t ui8Pin, Dir enmDir ) + { + if( enmDir == Dir::D_OUT ) + { + *vpui8Port |= ( 1 << ui8Pin ); + } + else + { + *vpui8Port &= ~( 1 << ui8Pin ); + } + } + + ////////////////////////////////////////////////////////////////////////// + static inline bool readPin( volatile uint8_t* vpui8Port, uint8_t ui8Pin ) + { + if( ( ( *vpui8Port ) >> ui8Pin ) & 1 ) + { + return true; + } + + return false; + } + + ////////////////////////////////////////////////////////////////////////// + static inline void writePin( volatile uint8_t* vpui8Port, uint8_t ui8Pin, bool bValue ) + { + if( bValue ) + { + *vpui8Port |= ( 1 << ui8Pin ); + } + else + { + *vpui8Port &= ~( 1 << ui8Pin ); + } + } + + ////////////////////////////////////////////////////////////////////////// + static inline void setPortDirection( volatile uint8_t* vpui8Port, Dir enmDir ) + { + *vpui8Port = ( ( enmDir == InOut::Dir::D_OUT ) ? ( 0xFF ) : ( 0x00 ) ); + } + + ////////////////////////////////////////////////////////////////////////// + static inline uint8_t readPort( volatile uint8_t* vpui8Port ) + { + return *vpui8Port; + } + + ////////////////////////////////////////////////////////////////////////// + static inline void writePort( volatile uint8_t* vpui8Port, uint8_t ui8Value ) + { + *vpui8Port = ui8Value; + } +}; + +////////////////////////////////////////////////////////////////////////// + +class InOutPin +{ +private: + volatile uint8_t* m_vpui8Input; + volatile uint8_t* m_vpui8Dir; + volatile uint8_t* m_vpui8Output; + + uint8_t m_ui8Pin; + +public: + InOutPin(); + InOutPin( InOut::Pin enmPin ); + ~InOutPin(); + + void setPin( InOut::Pin enmPin ); + + ////////////////////////////////////////////////////////////////////////// + inline void setDirection( InOut::Dir enmDir, bool bPullup = true ) + { + if( !m_vpui8Dir || !m_vpui8Output ) + { + return; + } + + InOut::setPinDirection( m_vpui8Dir, m_ui8Pin, enmDir ); + + if( enmDir == InOut::Dir::D_IN && bPullup ) + { + InOut::writePin( m_vpui8Output, m_ui8Pin, true ); + } + else if( enmDir == InOut::Dir::D_IN && !bPullup ) + { + InOut::writePin( m_vpui8Output, m_ui8Pin, false ); + } + } + + ////////////////////////////////////////////////////////////////////////// + inline bool read() + { + if( !m_vpui8Input ) + { + return false; + } + + return InOut::readPin( m_vpui8Input, m_ui8Pin ); + } + + ////////////////////////////////////////////////////////////////////////// + inline void write( bool bValue ) + { + if( !m_vpui8Output ) + { + return; + } + + InOut::writePin( m_vpui8Output, m_ui8Pin, bValue ); + } +}; + +////////////////////////////////////////////////////////////////////////// + +class InOutPort +{ +private: + volatile uint8_t* m_vpui8Input; + volatile uint8_t* m_vpui8Dir; + volatile uint8_t* m_vpui8Output; + +public: + InOutPort(); + InOutPort( InOut::Pin enmPortPin ); + ~InOutPort(); + + void setPort( InOut::Pin enmPortPin ); + + ////////////////////////////////////////////////////////////////////////// + inline void setDirection( InOut::Dir enmDir, bool bPullup = true ) + { + if( !m_vpui8Dir || !m_vpui8Output ) + { + return; + } + + InOut::setPortDirection( m_vpui8Dir, enmDir ); + + if( enmDir == InOut::Dir::D_IN && bPullup ) + { + InOut::writePort( m_vpui8Output, 0xFF ); + } + else if( enmDir == InOut::Dir::D_IN && !bPullup ) + { + InOut::writePort( m_vpui8Output, 0x00 ); + } + } + + ////////////////////////////////////////////////////////////////////////// + inline uint8_t read() + { + if( !m_vpui8Input ) + { + return 0; + } + + return InOut::readPort( m_vpui8Input ); + } + + ////////////////////////////////////////////////////////////////////////// + inline void write( uint8_t ui8Value ) + { + if( !m_vpui8Output ) + { + return; + } + + InOut::writePort( m_vpui8Output, ui8Value ); + } +}; + +#endif \ No newline at end of file diff --git a/InOut/main.cpp b/InOut/main.cpp new file mode 100644 index 0000000..d16e46a --- /dev/null +++ b/InOut/main.cpp @@ -0,0 +1,50 @@ +/* +* Copyright (c) by BlackMark 2015-2016 +* Date 02/01/2016 +* Version 1.2 +*/ + +#include "Clock.h" +#include "InOut.h" + +int main() +{ + InOutPin cPinRed( InOut::Pin::P_B5 ); + InOutPin cPinGreen( InOut::Pin::P_B4 ); + InOutPin cPinBlue( InOut::Pin::P_B3 ); + + cPinRed.setDirection( InOut::Dir::D_OUT ); + cPinGreen.setDirection( InOut::Dir::D_OUT ); + cPinBlue.setDirection( InOut::Dir::D_OUT ); + + cPinRed.write( false ); + cPinGreen.write( false ); + cPinBlue.write( false ); + + while( true ) + { + cPinRed.write( true ); + _delay_ms( 1000 ); + cPinRed.write( false ); + + cPinGreen.write( true ); + _delay_ms( 1000 ); + cPinGreen.write( false ); + + cPinBlue.write( true ); + _delay_ms( 1000 ); + cPinBlue.write( false ); + + cPinRed.write( true ); + cPinGreen.write( true ); + cPinBlue.write( true ); + _delay_ms( 5000 ); + + cPinRed.write( false ); + cPinGreen.write( false ); + cPinBlue.write( false ); + _delay_ms( 1000 ); + } + + return 0; +} \ No newline at end of file