From 8206c113449b09d22812f2000a832448749094ca Mon Sep 17 00:00:00 2001
From: CVR <cvrsicom@gmail.com>
Date: Thu, 16 Sep 2021 12:25:25 +1200
Subject: [PATCH] Intial commit

---
 .gitignore                    |  4 ++++
 .vscode/c_cpp_properties.json | 19 ++++++++++++++++++
 .vscode/settings.json         |  3 +++
 LICENSE                       | 19 ++++++++++++++++++
 README.md                     |  2 ++
 eeprom.sh                     |  7 +++++++
 makefile                      | 38 +++++++++++++++++++++++++++++++++++
 src/eeprom.cpp                | 28 ++++++++++++++++++++++++++
 src/eeprom.h                  |  8 ++++++++
 test/main.cpp                 | 15 ++++++++++++++
 10 files changed, 143 insertions(+)
 create mode 100644 .gitignore
 create mode 100644 .vscode/c_cpp_properties.json
 create mode 100644 .vscode/settings.json
 create mode 100644 LICENSE
 create mode 100644 README.md
 create mode 100644 eeprom.sh
 create mode 100644 makefile
 create mode 100644 src/eeprom.cpp
 create mode 100644 src/eeprom.h
 create mode 100644 test/main.cpp

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..c1241aa
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,4 @@
+*.hex
+*.elf
+*.bin
+
diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json
new file mode 100644
index 0000000..3aecb35
--- /dev/null
+++ b/.vscode/c_cpp_properties.json
@@ -0,0 +1,19 @@
+{
+    "configurations": [
+        {
+            "name": "Linux",
+            "includePath": [
+                "${workspaceFolder}/**",
+                "/usr/lib/avr/include/"
+            ],
+            "defines": [
+                "__AVR_ATmega16__"
+            ],
+            "compilerPath": "/usr/bin/gcc",
+            "cStandard": "gnu18",
+            "cppStandard": "gnu++14",
+            "intelliSenseMode": "gcc-x64"
+        }
+    ],
+    "version": 4
+}
\ No newline at end of file
diff --git a/.vscode/settings.json b/.vscode/settings.json
new file mode 100644
index 0000000..3b66410
--- /dev/null
+++ b/.vscode/settings.json
@@ -0,0 +1,3 @@
+{
+    "git.ignoreLimitWarning": true
+}
\ No newline at end of file
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..204b93d
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,19 @@
+MIT License Copyright (c) <year> <copyright holders>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is furnished
+to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice (including the next
+paragraph) shall be included in all copies or substantial portions of the
+Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
+FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
+OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
+OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..3ef95bf
--- /dev/null
+++ b/README.md
@@ -0,0 +1,2 @@
+# eeprom
+
diff --git a/eeprom.sh b/eeprom.sh
new file mode 100644
index 0000000..ca627c6
--- /dev/null
+++ b/eeprom.sh
@@ -0,0 +1,7 @@
+#!/bin/sh
+
+	avrdude -p $1 -c $2 -t << END
+	write eeprom 0 $3
+	dump eeprom 0 16
+	quit
+	END
\ No newline at end of file
diff --git a/makefile b/makefile
new file mode 100644
index 0000000..609704e
--- /dev/null
+++ b/makefile
@@ -0,0 +1,38 @@
+MCU?=atmega32
+F_CPU=8000000
+PROG?=dragon_jtag
+DEBUGGER = dragon
+PORT?=/dev/ttyUSB0
+TYPE?=DEBUG
+
+CC=avr-g++
+OBJCOPY=avr-objcopy
+CFLAGS=-Wall -g -O -mmcu=${MCU} -DF_CPU=${F_CPU} -I. -std=gnu++11 -D${TYPE} -Wno-write-strings
+TARGET=main
+SRCS=src/eeprom.cpp test/main.cpp test/usart/src/usart.cpp
+
+default: build flash
+
+build:
+	${CC} ${CFLAGS} -o bin/${TARGET}.bin  ${SRCS}
+	${CC} ${CFLAGS} -o bin/${TARGET}.elf  ${SRCS}
+	${OBJCOPY} -j .text -j .data -O ihex bin/${TARGET}.bin bin/${TARGET}.hex
+
+flash:
+	avrdude -p ${MCU} -c ${PROG} -P ${PORT} ${BAUD} -U flash:w:bin/main.hex
+
+clean:
+	rm -f bin/*
+
+debug: flash avarice
+	avr-gdb -ex "target remote :4242" bin/main.elf
+
+avarice:
+	sleep 1
+	avarice --file bin/main.elf --part ${MCU} --${DEBUGGER} :4242 &
+
+term:
+	screen /dev/ttyUSB0 1000000
+
+eeprom:
+	sh eeprom.sh
\ No newline at end of file
diff --git a/src/eeprom.cpp b/src/eeprom.cpp
new file mode 100644
index 0000000..d0b9e55
--- /dev/null
+++ b/src/eeprom.cpp
@@ -0,0 +1,28 @@
+#include "eeprom.h"
+
+namespace eeprom
+{
+    void write(uint8_t address, uint8_t data) {
+        /* Wait for completion of previous write */
+        while(EECR & (1<<EEWE));
+        /* Set up address and data registers */
+        EEAR = address;
+        EEDR = data;
+        /* Write logical one to EEMWE */
+        EECR |= (1<<EEMWE);
+        /* Start eeprom write by setting EEWE */
+        EECR |= (1<<EEWE);
+    }
+
+    uint8_t read(uint8_t address) {
+        /* Wait for completion of previous write */
+        while(EECR & (1<<EEWE));
+        /* Set up address register */
+        EEAR = address;
+        /* Start eeprom read by writing EERE */
+        EECR |= (1<<EERE);
+        /* Return data from data register */
+        return EEDR;
+    }
+
+} // namespace eeprom
\ No newline at end of file
diff --git a/src/eeprom.h b/src/eeprom.h
new file mode 100644
index 0000000..db027ec
--- /dev/null
+++ b/src/eeprom.h
@@ -0,0 +1,8 @@
+#pragma once
+#include <avr/io.h>
+
+namespace eeprom
+{
+    void write(uint8_t address, uint8_t data);
+    uint8_t read(uint8_t address);
+} // namespace eeprom
diff --git a/test/main.cpp b/test/main.cpp
new file mode 100644
index 0000000..e982a50
--- /dev/null
+++ b/test/main.cpp
@@ -0,0 +1,15 @@
+#include "../src/eeprom.h"
+#include "usart/src/usart.h"
+#include <avr/io.h>
+#include <util/delay.h>
+
+int main() {
+    usart::init(1000000);
+    printf("\033[2J");
+    for(int i=0;i<16;i++)
+        printf("%i %x\n\r", i, eeprom::read(i));
+
+    DDRB |= (1<<PB3);
+    
+    
+}