diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..d0e4686 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "i2c/i2c"] + path = i2c/i2c + url = git@git.blackmark.me:avr/i2c.git diff --git a/LICENSE b/LICENSE index 78a4674..80da57f 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2019 BlackMark +Copyright (c) 2020 BlackMark Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/hardware.hpp b/hardware.hpp deleted file mode 100644 index 4ece8ad..0000000 --- a/hardware.hpp +++ /dev/null @@ -1,93 +0,0 @@ -#pragma once - -#include - -#include - -namespace i2c { - -#if defined(__AVR_ATmega1284P__) - -enum class Prescaler { - PRESCALER_1 = 0b00, - PRESCALER_4 = 0b01, - PRESCALER_16 = 0b10, - PRESCALER_64 = 0b11, -}; - -template -class Hardware { - public: - static void init() - { - TWSR = static_cast(Pres); - TWBR = calcClock(); - } - - template - static bool start(bool read) - { - TWCR = (1 << TWINT) | (1 << TWSTA) | (1 << TWEN); - while (!(TWCR & (1 << TWINT))) - ; - - uint8_t status = TW_STATUS & 0xF8; - if (status != TW_START && status != TW_REP_START) - return false; - - TWDR = (Addr << 1) | read; - TWCR = (1 << TWINT) | (1 << TWEN); - - while (!(TWCR & (1 << TWINT))) - ; - - status = TW_STATUS & 0xF8; - if (status != TW_MT_SLA_ACK && status != TW_MR_SLA_ACK) - return false; - - return true; - } - - static bool write(uint8_t data) - { - TWDR = data; - TWCR = (1 << TWINT) | (1 << TWEN); - - while (!(TWCR & (1 << TWINT))) - ; - - uint8_t status = TW_STATUS & 0xF8; - if (status != TW_MT_DATA_ACK) - return false; - - return true; - } - - template - static uint8_t read() - { - TWCR = (1 << TWINT) | (1 << TWEN) | (!LastByte << TWEA); - while (!(TWCR & (1 << TWINT))) - ; - - return TWDR; - } - - static void stop() - { - TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWSTO); - - while (TWCR & (1 << TWSTO)) - ; - } - - private: - static constexpr uint8_t calcClock() - { - return ((F_CPU / Clock) - 16) / 2; - } -}; - -#endif - -} // namespace i2c diff --git a/i2c.atsln b/i2c.atsln new file mode 100644 index 0000000..a40c8b5 --- /dev/null +++ b/i2c.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}") = "i2c", "i2c\i2c.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/i2c.hpp b/i2c.hpp deleted file mode 100644 index 00ada38..0000000 --- a/i2c.hpp +++ /dev/null @@ -1,39 +0,0 @@ -#pragma once - -#include - -#include "hardware.hpp" - -namespace i2c { - -template -struct I2c { - static void init() - { - Driver::init(); - } - - template - static bool start(bool read) - { - return Driver::template start(read); - } - - static bool write(uint8_t data) - { - return Driver::write(data); - } - - template - static uint8_t read() - { - return Driver::template read(); - } - - static void stop() - { - Driver::stop(); - } -}; - -} // namespace i2c diff --git a/i2c/clock.hpp b/i2c/clock.hpp new file mode 100644 index 0000000..94845e9 --- /dev/null +++ b/i2c/clock.hpp @@ -0,0 +1,4 @@ +#pragma once + +#define F_CPU 16000000 +#include diff --git a/i2c/i2c b/i2c/i2c new file mode 160000 index 0000000..939cdc1 --- /dev/null +++ b/i2c/i2c @@ -0,0 +1 @@ +Subproject commit 939cdc1196edb161c6ea25eb61a72fa4488cba06 diff --git a/i2c/i2c.cppproj b/i2c/i2c.cppproj new file mode 100644 index 0000000..27c8bde --- /dev/null +++ b/i2c/i2c.cppproj @@ -0,0 +1,196 @@ + + + + 2.0 + 7.0 + com.Atmel.AVRGCC8.CPP + dce6c7e3-ee26-4d79-826b-08594b9ad897 + ATmega328P + none + Executable + CPP + $(MSBuildProjectName) + .elf + $(MSBuildProjectDirectory)\$(Configuration) + i2c + i2c + i2c + avr-g++-9.1.0 + true + false + true + true + 0x20000000 + + true + exception_table + 2 + 0 + 0 + + com.atmel.avrdbg.tool.stk500 + + 0x1E950F + + + + 125000 + + ISP + + com.atmel.avrdbg.tool.stk500 + + + STK500 + + ISP + 125000 + + + + + + + + + + + + + + + + + -mmcu=atmega328p + True + True + True + True + False + True + True + + + %24(PackRepoDir)\Atmel\ATmega_DFP\1.4.346\include + + + True + True + True + True + -fno-threadsafe-statics -std=c11 + True + True + + + %24(PackRepoDir)\Atmel\ATmega_DFP\1.4.346\include + + + True + True + True + -fno-threadsafe-statics -Wextra -std=c++17 + + + libm + + + + + %24(PackRepoDir)\Atmel\ATmega_DFP\1.4.346\include + + + + + NDEBUG + + + Optimize for size (-Os) + + + NDEBUG + + + Optimize for size (-Os) + + + + + + + -mmcu=atmega328p + True + True + True + True + False + True + True + + + %24(PackRepoDir)\Atmel\ATmega_DFP\1.4.346\include + + + True + True + True + True + -fno-threadsafe-statics -std=c11 + True + True + + + %24(PackRepoDir)\Atmel\ATmega_DFP\1.4.346\include + + + True + True + True + -fno-threadsafe-statics -Wextra -std=c++17 + + + libm + + + + + %24(PackRepoDir)\Atmel\ATmega_DFP\1.4.346\include + + + + + DEBUG + + + Optimize (-O1) + Maximum (-g3) + + + DEBUG + + + Optimize (-O1) + Maximum (-g3) + Default (-Wa,-g) + + + + + + compile + + + compile + + + compile + + + compile + + + + + + + \ No newline at end of file diff --git a/i2c/main.cpp b/i2c/main.cpp new file mode 100644 index 0000000..3fe7dc9 --- /dev/null +++ b/i2c/main.cpp @@ -0,0 +1,20 @@ +#include "clock.hpp" + +#include "i2c/i2c.hpp" + +void i2cTest() +{ + using namespace i2c; + using config = Hardware; + + I2c twi; + + twi.init(); +} + +int main() +{ + i2cTest(); + + return 0; +}