Skip to content

Commit dd2e614

Browse files
Merge pull request #1 from TimerOverflow/20161028
20161028
2 parents 5150f8e + a294140 commit dd2e614

21 files changed

+2890
-0
lines changed

AvrEeprom.c

Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
/*********************************************************************************/
2+
/*
3+
* Author : Jung Hyun Gu
4+
* File name : AvrEeprom.c
5+
*/
6+
/*********************************************************************************/
7+
#include "AvrEeprom.h"
8+
/*********************************************************************************/
9+
/** Global variable **/
10+
11+
12+
/*********************************************************************************/
13+
char InitEepControl(tag_EepControl *Eep, const unsigned char *DataBase, unsigned int Length)
14+
{
15+
const char GapOfAnotherSector = 10;
16+
static unsigned int AllocEepAddr = 0;
17+
char **pDataBase = (char **) &Eep->DataBase;
18+
unsigned int *pEepBase = (unsigned int *) &Eep->EepBase;
19+
unsigned int *pLength = (unsigned int *) &Eep->Length;
20+
tag_EepBitField *pBit = (tag_EepBitField *) &Eep->Bit;
21+
22+
23+
/*
24+
1) 인수
25+
- Eep : tag_EepControl 인스턴스의 주소
26+
- DataBase : eeprom을 통해 관리할 대상의 시작주소
27+
- Length : 관리할 대상의 크기
28+
29+
2) 반환
30+
- 초기화 성공 여부
31+
32+
3) 설명
33+
- tag_EepControl 인스턴스를 초기화한다.
34+
- tag_EepControl 인스턴스에 eeprom으로 부터 읽거나 쓸 대상의 주소와, 할당할 eeprom의 시작과 길이를 초기화한다.
35+
*/
36+
37+
38+
pBit->Init = false;
39+
40+
if((DataBase != 0) && (Length != 0) && ((AllocEepAddr + Length + GapOfAnotherSector) < EEPROM_SIZE))
41+
{
42+
*pLength = Length;
43+
*pDataBase = (char *) DataBase;
44+
*pEepBase = AllocEepAddr;
45+
AllocEepAddr += (Length + GapOfAnotherSector);
46+
47+
pBit->Init = true;
48+
}
49+
50+
return pBit->Init;
51+
}
52+
/*********************************************************************************/
53+
void DoEepReadControl(tag_EepControl *Eep)
54+
{
55+
unsigned int Index = 0;
56+
57+
/*
58+
1) 인수
59+
- Eep : tag_EepControl 인스턴스의 주소
60+
61+
2) 반환
62+
- 없음.
63+
64+
3) 설명
65+
- 해당 인스턴스의 eeprom으로 부터 데이터를 읽어와 대상 버퍼에 값을 대입.
66+
*/
67+
68+
if(Eep->Bit.Init == false)
69+
{
70+
return;
71+
/* error or disabled */
72+
}
73+
74+
do
75+
{
76+
Eep->DataBase[Index] = Eeprom_Read(Eep->EepBase + (Index));
77+
}while(++Index <= Eep->Length);
78+
}
79+
/*********************************************************************************/
80+
void GetDataFromEeprom(char* const Dest, const int EepBase, int Length)
81+
{
82+
unsigned int Index = 0;
83+
84+
/*
85+
1) 인수
86+
- Dest : eeprom으로 부터 읽은 데이터를 대입할 대상의 주소
87+
- EepBase : 읽을 eeprom의 시작주소 (메모리 주소가 아님)
88+
- Length : 읽어올 데이터의 길이.
89+
90+
2) 반환
91+
- 없음.
92+
93+
3) 설명
94+
- tag_EepControl 구조체를 사용하지 않고 임의 eeprom 영역으로 부터 데이터를 읽어와 대상 버퍼에 값을 대입한다.
95+
*/
96+
97+
do
98+
{
99+
Dest[Index] = Eeprom_Read(EepBase + Index);
100+
}while(++Index <= Length);
101+
}
102+
/*********************************************************************************/
103+
char DoEepWriteControl(tag_EepControl *Eep)
104+
{
105+
unsigned int *pIndex = (unsigned int *) &Eep->Index;
106+
tag_EepBitField *pBit;
107+
108+
/*
109+
1) 인수
110+
- Eep : tag_EepControl 인스턴스의 주소
111+
112+
2) 반환
113+
- 쓰기동작 중일 때 true, 그외 상황일 때 false.
114+
115+
3) 설명
116+
- 해당 인스턴스의 eeprom 영역에 관리할 대상 데이터를 쓰기를 진행한다.
117+
- SetEepWriteEnable() 함수를 통해 'Eep.Bit.Write'를 활성화 시켜야 실행된다.
118+
- 쓰기전 해당주소의 값이 쓰려고 하는 값과 일치하지 않을 경우에만 유효하다고 판단하며,
119+
유효한 데이터만 쓰기를 진행하고 유효하지 않을 경우 다음 주소로 건너뛴다.
120+
- 값이 유효하여 쓰기를 진행하거나 해당영역의 마지막에 도달할 경우에만 while loop를 탈출한다.
121+
*/
122+
123+
124+
if((Eep->Bit.Init == false) || (Eep->Bit.Write == false))
125+
{
126+
return false;
127+
/* error or disabled */
128+
}
129+
130+
while(true)
131+
{
132+
if(Eeprom_Read(Eep->EepBase + (*pIndex)) != Eep->DataBase[*pIndex])
133+
{
134+
Eeprom_Write(Eep->EepBase + (*pIndex), Eep->DataBase[*pIndex]);
135+
return true;
136+
/* check valid data */
137+
}
138+
139+
if(++(*pIndex) > Eep->Length)
140+
{
141+
pBit = (tag_EepBitField *) &Eep->Bit;
142+
pBit->Write = false;
143+
return false;
144+
/* end of sector */
145+
}
146+
}
147+
}
148+
/*********************************************************************************/
149+
void SetEepWriteEnable(tag_EepControl *Eep)
150+
{
151+
/*
152+
1) 인수
153+
- Eep : tag_EepControl 인스턴스의 주소
154+
155+
2) 반환
156+
- 없음.
157+
158+
3) 설명
159+
- 해당 인스턴스에 쓰기 동작을 활성화한다.
160+
- 'Eep.Bit.Write' 상태가 'true'일 때 DoEepWriteControl()함수가 쓰기 동작을 실행한다.
161+
*/
162+
163+
tag_EepBitField *pBit = (tag_EepBitField *) &Eep->Bit;
164+
unsigned int *pIndex = (unsigned int *) &Eep->Index;
165+
166+
pBit->Write = true;
167+
*pIndex = 0;
168+
}
169+
/*********************************************************************************/
170+
void Eeprom_Write(unsigned int Addr, char Data)
171+
{
172+
char cSREG;
173+
174+
while(EECR & 0x02);
175+
EEAR = Addr;
176+
EEDR = Data;
177+
178+
cSREG = SREG;
179+
/* stored status register */
180+
181+
SREG &= ~0x80;
182+
/* disable global interrupt */
183+
184+
EECR |= 0x04;
185+
/* enable EEPROM Master Write EEMWE */
186+
187+
EECR |= 0x02;
188+
/* enable EEPROM Write EEWE */
189+
190+
SREG = cSREG;
191+
/* restored register */
192+
}
193+
/*********************************************************************************/
194+
char Eeprom_Read(unsigned int Addr)
195+
{
196+
while(EECR & 0x02);
197+
EEAR = Addr;
198+
EECR |= 0x01;
199+
return EEDR;
200+
}
201+
/*********************************************************************************/

AvrEeprom.h

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*********************************************************************************/
2+
/*
3+
* Author : Jung Hyun Gu
4+
* File name : AvrEeprom.h
5+
*/
6+
/*********************************************************************************/
7+
#ifndef __AVR_EEPROM_H__
8+
#define __AVR_EEPROM_H__
9+
/*********************************************************************************/
10+
/** REVISION HISTORY **/
11+
/*
12+
2016. 10. 28. - DoEepWriteControl() 반환값 수정.
13+
Jung Hyun Gu - 반환값 관련 주석 추가.
14+
15+
2016. 10. 28. - 초기버전.
16+
Jung Hyun Gu
17+
*/
18+
/*********************************************************************************/
19+
/**Define**/
20+
21+
#define true 1
22+
#define false 0
23+
24+
25+
#define __AVR_ATMEGA64__
26+
27+
28+
#ifdef __AVR_ATMEGA8__
29+
#include <iom8.h>
30+
#define EEPROM_SIZE 512
31+
/* Endurance: 100,000 Write/Erase */
32+
#endif
33+
34+
#ifdef __AVR_ATMEGA16__
35+
#include <iom16.h>
36+
#define EEPROM_SIZE 512
37+
/* Endurance: 100,000 Write/Erase */
38+
#endif
39+
40+
#ifdef __AVR_ATMEGA32__
41+
#include <iom32.h>
42+
#define EEPROM_SIZE 1024
43+
/* Endurance: 100,000 Write/Erase */
44+
#endif
45+
46+
#ifdef __AVR_ATMEGA64__
47+
#include <iom64.h>
48+
#define EEPROM_SIZE 2048
49+
/* Endurance: 100,000 Write/Erase */
50+
#endif
51+
52+
#ifdef __AVR_ATMEGA128__
53+
#include <iom128.h>
54+
#define EEPROM_SIZE 4096
55+
/* Endurance: 100,000 Write/Erase */
56+
#endif
57+
58+
/*********************************************************************************/
59+
/**Enum**/
60+
61+
62+
/*********************************************************************************/
63+
/**Struct**/
64+
65+
typedef struct
66+
{
67+
char Init : 1;
68+
char Write : 1;
69+
}tag_EepBitField;
70+
71+
typedef const struct
72+
{
73+
char *DataBase; // 관리할 데이터의 시작 주소
74+
unsigned int EepBase; // eeprom의 시작 주소
75+
unsigned int Index; // 쓰기 인덱스
76+
unsigned int Length; // 관리할 데이터의 길이
77+
78+
tag_EepBitField Bit;
79+
}tag_EepControl;
80+
81+
/*********************************************************************************/
82+
/**Function**/
83+
84+
char InitEepControl(tag_EepControl *Eep, const unsigned char *DataBase, unsigned int Length);
85+
86+
void DoEepReadControl(tag_EepControl *Eep);
87+
void GetDataFromEeprom(char* const Dest, const int EepBase, int Length);
88+
89+
char DoEepWriteControl(tag_EepControl *Eep);
90+
void SetEepWriteEnable(tag_EepControl *Eep);
91+
92+
void Eeprom_Write(unsigned int Addr, char Data);
93+
char Eeprom_Read(unsigned int Addr);
94+
95+
/*********************************************************************************/
96+
#endif //__AVR_EEPROM_H__

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
# SysEeprom
22
embedded EEPROM manage library
3+
4+
# Build & Tested Environment
5+
- IAR C/C++ Compiler for AVR 6.11.1 (6.11.1.50453)

0 commit comments

Comments
 (0)