Started Devs for I2C support on AVR
authorandre Ebersold <andre.ebersold@free.fr>
Mon, 23 Oct 2023 20:46:56 +0000 (22:46 +0200)
committerandre Ebersold <andre.ebersold@free.fr>
Mon, 23 Oct 2023 20:46:56 +0000 (22:46 +0200)
HAL/AVR/AvrI2C.cpp [new file with mode: 0644]
HAL/AVR/AvrI2C.h [new file with mode: 0644]
HAL/Abstract/II2C.h [new file with mode: 0644]
HAL/Drivers/DS3231.cpp [new file with mode: 0644]
HAL/Drivers/DS3231.h [new file with mode: 0644]
README
setupenv.bat [new file with mode: 0644]

diff --git a/HAL/AVR/AvrI2C.cpp b/HAL/AVR/AvrI2C.cpp
new file mode 100644 (file)
index 0000000..042213f
--- /dev/null
@@ -0,0 +1,36 @@
+#include <avr/interrupt.h>
+#include <util/twi.h>
+
+#include "Utils/StdTypes.h"
+#include "HAL/Abstract/II2C.h"
+#include "AVR/AvrI2C.h"
+
+#ifndef F_CPU
+#define F_CPU 16000000
+#endif
+#define SCL_CLOCK 50000
+
+void AvrI2C::init()
+{
+    uint8_t   twst;
+    TWSR = 0;                         // no prescaler
+    TWBR = ((F_CPU/SCL_CLOCK)-16)/2; 
+    TWCR = (1<<(TWINT))|(1<<TWSTA )|(1<<TWEN);
+    printf("TWCR 0x%x \n",TWCR);
+    while( !( TWCR & (1<< TWINT) ) ) ;
+    printf(" Start condition has been transmitted \n");
+    if( (TWSR&0xF8) != TW_START){
+     printf(" Error \n");
+    }
+    // Setting Address 
+    TWDR = 0x1C; 
+    // cleating TWCR 
+    TWCR = (1<<TWINT) |(1<<TWEN);
+
+    while ( !(TWRC & (1<<TWINT))) ; 
+    if ((TWSR & 0xF8) != TW_MT_SLA_ACK)
+    {
+       printf(" Error at TWSR   0x%x\n", TWSR);  // here is the problem !!!!!!!!!  TWSR value should be 0x18
+       return; 
+    }        
+}
diff --git a/HAL/AVR/AvrI2C.h b/HAL/AVR/AvrI2C.h
new file mode 100644 (file)
index 0000000..8e98cff
--- /dev/null
@@ -0,0 +1,15 @@
+#ifndef __AVR_I2C__
+#define __AVR_I2C__
+
+class AvrI2C : public II2C
+{
+    public:
+        AvrI2C();
+
+        void init();
+
+        
+};
+
+/* vim: set et sw=4 ts=4 list: */
+#endif
diff --git a/HAL/Abstract/II2C.h b/HAL/Abstract/II2C.h
new file mode 100644 (file)
index 0000000..121b6dd
--- /dev/null
@@ -0,0 +1,12 @@
+#ifndef __II2C_H__
+#define __II2C_H__
+
+class II2C
+{
+    void II2C() {}
+
+    virtual write(Uint8_t);
+    virtual read(Uint8_t);
+};
+
+#endif
diff --git a/HAL/Drivers/DS3231.cpp b/HAL/Drivers/DS3231.cpp
new file mode 100644 (file)
index 0000000..5b1c145
--- /dev/null
@@ -0,0 +1,9 @@
+
+#include "Abstract/II2C.h"
+#include "DS3241.h"
+
+DS3231::DS3231(II2C *argI2c)
+ : itsII2C(argI2c)
+{
+}
+
diff --git a/HAL/Drivers/DS3231.h b/HAL/Drivers/DS3231.h
new file mode 100644 (file)
index 0000000..957c4ac
--- /dev/null
@@ -0,0 +1,16 @@
+#ifndef __DS3231_H__
+#define __DS3231_H__
+
+/**
+ * Rtc Driver using DS3231
+ */
+class DS3231 
+{
+    public:
+        DS3241(II2C *);
+    private:
+        II2C *itsII2C;
+};
+
+/* vim: set et sw=4 ts=4 list: */
+#endif
diff --git a/README b/README
index d34ea4c49d1873c529707b431238d49380e309e6..fe6eb348fe02fc98eb030e42c4af616df9e29618 100644 (file)
--- a/README
+++ b/README
@@ -8,5 +8,11 @@ Create a Build dir and launch the command below:
 
 cmake -DCMAKE_TOOLCHAIN_FILE=/home/aebersol/Devs/avr/rules/gcc-avr-generic.cmake  ../
 
+On Windows : 
+cmake
+-DCMAKE_TOOLCHAIN_FILE=/home/aebersol/Devs/avr/rules/gcc-avr-generic.cmake 
+-DCMAKE_MAKE_PROGRAM=c:\msys64\usr\bin\make.exe
+-G "MSYS64 Makefiles"  ../
+
 Flashing instruction
 --------------------
diff --git a/setupenv.bat b/setupenv.bat
new file mode 100644 (file)
index 0000000..3deebd2
--- /dev/null
@@ -0,0 +1,6 @@
+
+SET PATH=%PATH%;C:\Tools\avr8-gnu-toolchain-3.7.0.1796-win32.any.x86_64\avr8-gnu-toolchain-win32_x86_64\bin
+SET PATH=%PATH%;C:\Program Files\CMake\bin
+SET PATH=%PATH%;C:\msys64\usr\bin
+
+SET AVR_FIND_ROOT_PATH=C:\Tools\avr8-gnu-toolchain-3.7.0.1796-win32.any.x86_64\avr8-gnu-toolchain-win32_x86_64\