From 077cf75268e14c612c12f67f8b697779b844885b Mon Sep 17 00:00:00 2001 From: Ibrahim Date: Sat, 19 Nov 2022 21:06:45 +0200 Subject: [PATCH] added external eeprom --- External EEPROM/.cproject | 115 ++++++++++++++++++ External EEPROM/.project | 27 ++++ .../.settings/de.innot.avreclipse.core.prefs | 7 ++ .../org.eclipse.core.resources.prefs | 2 + External EEPROM/src/external_eeprom.c | 86 +++++++++++++ External EEPROM/src/external_eeprom.h | 31 +++++ External EEPROM/src/std_types.h | 40 ++++++ External EEPROM/src/twi.c | 96 +++++++++++++++ External EEPROM/src/twi.h | 40 ++++++ 9 files changed, 444 insertions(+) create mode 100644 External EEPROM/.cproject create mode 100644 External EEPROM/.project create mode 100644 External EEPROM/.settings/de.innot.avreclipse.core.prefs create mode 100644 External EEPROM/.settings/org.eclipse.core.resources.prefs create mode 100644 External EEPROM/src/external_eeprom.c create mode 100644 External EEPROM/src/external_eeprom.h create mode 100644 External EEPROM/src/std_types.h create mode 100644 External EEPROM/src/twi.c create mode 100644 External EEPROM/src/twi.h diff --git a/External EEPROM/.cproject b/External EEPROM/.cproject new file mode 100644 index 0000000..950c2fe --- /dev/null +++ b/External EEPROM/.cproject @@ -0,0 +1,115 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/External EEPROM/.project b/External EEPROM/.project new file mode 100644 index 0000000..24a6c36 --- /dev/null +++ b/External EEPROM/.project @@ -0,0 +1,27 @@ + + + External EEPROM + + + + + + org.eclipse.cdt.managedbuilder.core.genmakebuilder + clean,full,incremental, + + + + + org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder + full,incremental, + + + + + + org.eclipse.cdt.core.cnature + org.eclipse.cdt.managedbuilder.core.managedBuildNature + org.eclipse.cdt.managedbuilder.core.ScannerConfigNature + de.innot.avreclipse.core.avrnature + + diff --git a/External EEPROM/.settings/de.innot.avreclipse.core.prefs b/External EEPROM/.settings/de.innot.avreclipse.core.prefs new file mode 100644 index 0000000..6139929 --- /dev/null +++ b/External EEPROM/.settings/de.innot.avreclipse.core.prefs @@ -0,0 +1,7 @@ +avrtarget/ClockFrequency=1000000 +avrtarget/ExtRAMSize=0 +avrtarget/ExtendedRAM=false +avrtarget/MCUType=atmega32 +avrtarget/UseEEPROM=false +avrtarget/UseExtendedRAMforHeap=true +eclipse.preferences.version=1 diff --git a/External EEPROM/.settings/org.eclipse.core.resources.prefs b/External EEPROM/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000..99f26c0 --- /dev/null +++ b/External EEPROM/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +encoding/=UTF-8 diff --git a/External EEPROM/src/external_eeprom.c b/External EEPROM/src/external_eeprom.c new file mode 100644 index 0000000..26867d6 --- /dev/null +++ b/External EEPROM/src/external_eeprom.c @@ -0,0 +1,86 @@ + /****************************************************************************** + * + * Module: External EEPROM + * File Name: external_eeprom.c + * Description: Source file for the External EEPROM Memory + * Author: Ibrahim Mohamed + * + *******************************************************************************/ + +#include "external_eeprom.h" +#include "twi.h" + +/******************************************************************************* + * Functions DEFINITIONS * + *******************************************************************************/ + +uint8 EEPROM_writeByte(uint16 u16addr,uint8 u8data) +{ + /* Send the Start Bit */ + TWI_start(); + if (TWI_getStatus() != TWI_START) + return ERROR; + + /* Send the device address, we need to get A8 A9 A10 address bits from the + * memory location address and R/W=0 (write) */ + TWI_writeByte((uint8)(0xA0 | ((u16addr & 0x0700)>>7))); + if (TWI_getStatus() != TWI_MT_SLA_W_ACK) + return ERROR; + + /* Send the required memory location address */ + TWI_writeByte((uint8)(u16addr)); + if (TWI_getStatus() != TWI_MT_DATA_ACK) + return ERROR; + + /* write byte to eeprom */ + TWI_writeByte(u8data); + if (TWI_getStatus() != TWI_MT_DATA_ACK) + return ERROR; + + /* Send the Stop Bit */ + TWI_stop(); + + return SUCCESS; + +} + +uint8 EEPROM_readByte(uint16 u16addr,uint8 *u8data) +{ + /* Send the Start Bit */ + TWI_start(); + if (TWI_getStatus() != TWI_START) + return ERROR; + + /* Send the device address, we need to get A8 A9 A10 address bits from the + * memory location address and R/W=0 (write) */ + TWI_writeByte((uint8)((0xA0) | ((u16addr & 0x0700)>>7))); + if (TWI_getStatus() != TWI_MT_SLA_W_ACK) + return ERROR; + + /* Send the required memory location address */ + TWI_writeByte((uint8)(u16addr)); + if (TWI_getStatus() != TWI_MT_DATA_ACK) + return ERROR; + + /* Send the Repeated Start Bit */ + TWI_start(); + if (TWI_getStatus() != TWI_REP_START) + return ERROR; + + /* Send the device address, we need to get A8 A9 A10 address bits from the + * memory location address and R/W=1 (Read) */ + TWI_writeByte((uint8)((0xA0) | ((u16addr & 0x0700)>>7) | 1)); + if (TWI_getStatus() != TWI_MT_SLA_R_ACK) + return ERROR; + + /* Read Byte from Memory without send ACK */ + *u8data = TWI_readByteWithNACK(); + if (TWI_getStatus() != TWI_MR_DATA_NACK) + return ERROR; + + /* Send the Stop Bit */ + TWI_stop(); + + return SUCCESS; +} + diff --git a/External EEPROM/src/external_eeprom.h b/External EEPROM/src/external_eeprom.h new file mode 100644 index 0000000..b3d1f9a --- /dev/null +++ b/External EEPROM/src/external_eeprom.h @@ -0,0 +1,31 @@ + /****************************************************************************** + * + * Module: External EEPROM + * File Name: external_eeprom.h + * Description: Header file for the External EEPROM Memory + * Author: Ibrahim Mohamed + * + *******************************************************************************/ + +#ifndef EXTERNAL_EEPROM_H_ +#define EXTERNAL_EEPROM_H_ + +#include "std_types.h" + +/******************************************************************************* + * Preprocessor Macros * + *******************************************************************************/ +#define ERROR 0 +#define SUCCESS 1 + +/******************************************************************************* + * Functions Prototypes * + *******************************************************************************/ + +uint8 EEPROM_writeByte(uint16 u16addr,uint8 u8data); +uint8 EEPROM_readByte(uint16 u16addr,uint8 *u8data); + + + + +#endif /* EXTERNAL_EEPROM_H_ */ diff --git a/External EEPROM/src/std_types.h b/External EEPROM/src/std_types.h new file mode 100644 index 0000000..8fc76a6 --- /dev/null +++ b/External EEPROM/src/std_types.h @@ -0,0 +1,40 @@ + /****************************************************************************** + * + * Module: Common - Platform Types Abstraction + * File Name: std_types.h + * Description: types for AVR + * Author: Ibrahim Mohamed + * + *******************************************************************************/ + +#ifndef STD_TYPES_H_ +#define STD_TYPES_H_ + +/* Boolean Data Type */ +typedef unsigned char boolean; + +/* Boolean Values */ +#ifndef FALSE +#define FALSE (0u) +#endif +#ifndef TRUE +#define TRUE (1u) +#endif + +#define LOGIC_HIGH (1u) +#define LOGIC_LOW (0u) + +#define NULL_PTR ((void*)0) + +typedef unsigned char uint8; /* 0 .. 255 */ +typedef signed char sint8; /* -128 .. +127 */ +typedef unsigned short uint16; /* 0 .. 65535 */ +typedef signed short sint16; /* -32768 .. +32767 */ +typedef unsigned long uint32; /* 0 .. 4294967295 */ +typedef signed long sint32; /* -2147483648 .. +2147483647 */ +typedef unsigned long long uint64; /* 0 .. 18446744073709551615 */ +typedef signed long long sint64; /* -9223372036854775808 .. 9223372036854775807 */ +typedef float float32; +typedef double float64; + +#endif /* STD_TYPE_H_ */ diff --git a/External EEPROM/src/twi.c b/External EEPROM/src/twi.c new file mode 100644 index 0000000..cd2cdb3 --- /dev/null +++ b/External EEPROM/src/twi.c @@ -0,0 +1,96 @@ + /****************************************************************************** + * + * Module: TWI(I2C) + * File Name: twi.h + * Description: Source file for the TWI(I2C) AVR driver + * Author: Ibrahim Mohamed + * + *******************************************************************************/ + +#include "twi.h" +#include "common_macros.h" +#include + +void TWI_init(void) +{ + /* Bit Rate: 400.000 kbps using zero pre-scaler TWPS=00 and F_CPU=8Mhz */ + TWBR = 0x02; + TWSR = 0x00; + + /* Two Wire Bus address my address if any master device want to call me: 0x1 (used in case this MC is a slave device) + General Call Recognition: Off */ + TWAR = 0b00000010; // my address = 0x01 :) + + TWCR = (1<