Skip to content

Commit 2f56e3f

Browse files
Merge pull request #7 from TimerOverflow/20190723
20190723
2 parents 9f98001 + 009771a commit 2f56e3f

26 files changed

+399
-399
lines changed
Lines changed: 75 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,64 @@
11
/*********************************************************************************/
22
/*
33
* Author : Jeong Hyun Gu
4-
* File name : AvrEeprom.c
4+
* File name : SysEeprom.c
55
*/
66
/*********************************************************************************/
7-
#include <ina90.h>
8-
#include "AvrEeprom.h"
7+
#include "SysEeprom.h"
98
/*********************************************************************************/
10-
#if(AVR_EEPROM_REVISION_DATE != 20190114)
11-
#error wrong include file. (AvrEeprom.h)
9+
#if(SYS_EEPROM_REVISION_DATE != 20190723)
10+
#error wrong include file. (SysEeprom.h)
1211
#endif
1312
/*********************************************************************************/
1413
/** Global variable **/
1514

15+
/*********************************************************************************/
16+
static inline tU8 CheckAllOfInit(tag_EepControl *Eep)
17+
{
18+
return (Eep->Bit.InitGeneral) ? true : false;
19+
}
20+
/*********************************************************************************/
21+
tU8 InitEepCommonConfig(tag_EepCommonConfig *EepConfig, tU16 LastAddr, tU8 (*EepromWrite)(tU16 Addr, tU8 Data), tU8 (*EepromRead)(tU16 Addr, tU8 *pData))
22+
{
23+
tU16 *pLastAddr = (tU16 *) &EepConfig->LastAddr;
24+
25+
/*
26+
1) 인수
27+
- EepConfig : EepConfig 인스턴스의 주소
28+
- EepromWrite : 사용자가 구현한 EepromWrite 함수 포인터 주소
29+
- EepromRead : 사용자가 구현한 EepromRead 함수 포인터 주소
30+
31+
2) 반환
32+
- 초기화 성공 여부
33+
34+
3) 설명
35+
- tag_EepControl::Config 구조체의 필요한 정보를 초기화 한다.
36+
- EEPROM의 크기, HAL 구현 함수등을 연결.
37+
*/
1638

39+
*pLastAddr = LastAddr;
40+
EepConfig->EepromWrite = EepromWrite;
41+
EepConfig->EepromRead = EepromRead;
42+
EepConfig->Bit.Init = true;
43+
44+
return EepConfig->Bit.Init;
45+
}
1746
/*********************************************************************************/
18-
char InitEepControl(tag_EepControl *Eep, const unsigned char *DataBase, unsigned int Length)
47+
tU8 InitEepControl(tag_EepControl *Eep, const tU8 *DataBase, tU16 Length, tag_EepCommonConfig *Config)
1948
{
20-
const char GapOfAnotherSector = 10;
21-
static unsigned int AllocEepAddr = 0;
22-
char **pDataBase = (char **) &Eep->DataBase;
23-
unsigned int *pEepBase = (unsigned int *) &Eep->EepBase;
24-
unsigned int *pLength = (unsigned int *) &Eep->Length;
49+
const tU8 GapOfAnotherSector = 10;
50+
tU8 **pDataBase = (tU8 **) &Eep->DataBase;
51+
tU16 *pEepBase = (tU16 *) &Eep->EepBase;
52+
tU16 *pLength = (tU16 *) &Eep->Length;
2553
tag_EepBitField *pBit = (tag_EepBitField *) &Eep->Bit;
26-
54+
tag_EepCommonConfig **pConfig = (tag_EepCommonConfig **) &Eep->Config;
2755

2856
/*
2957
1) 인수
3058
- Eep : tag_EepControl 인스턴스의 주소
3159
- DataBase : eeprom을 통해 관리할 대상의 시작주소
3260
- Length : 관리할 대상의 크기
61+
- Config : Eeprom에 대한 크기 정보, HAL 함수 정보를 가지고 있는 tag_EepCommonConfig 구조체 인스턴스의 주소.
3362
3463
2) 반환
3564
- 초기화 성공 여부
@@ -39,25 +68,34 @@ char InitEepControl(tag_EepControl *Eep, const unsigned char *DataBase, unsigned
3968
- tag_EepControl 인스턴스에 eeprom으로 부터 읽거나 쓸 대상의 주소와, 할당할 eeprom의 시작과 길이를 초기화한다.
4069
*/
4170

71+
if(Config->Bit.Init == false)
72+
{
73+
return false;
74+
}
4275

43-
pBit->Init = false;
76+
*pConfig = (tag_EepCommonConfig *) Config;
77+
//Link with "Eep" struct.
4478

45-
if((DataBase != 0) && (Length != 0) && ((AllocEepAddr + Length + GapOfAnotherSector) < EEPROM_SIZE))
79+
if((DataBase != 0) && (Length != 0) && ((Config->AllocEepAddr + Length + GapOfAnotherSector) < Config->LastAddr))
4680
{
4781
*pLength = Length;
48-
*pDataBase = (char *) DataBase;
49-
*pEepBase = AllocEepAddr;
50-
AllocEepAddr += (Length + GapOfAnotherSector);
82+
*pDataBase = (tU8 *) DataBase;
83+
*pEepBase = Config->AllocEepAddr;
84+
Config->AllocEepAddr += (Length + GapOfAnotherSector);
5185

52-
pBit->Init = true;
86+
pBit->InitGeneral = true;
5387
}
5488

55-
return pBit->Init;
89+
pBit->InitComplete = CheckAllOfInit(Eep);
90+
91+
return pBit->InitGeneral;
5692
}
5793
/*********************************************************************************/
5894
void DoEepReadControl(tag_EepControl *Eep)
5995
{
60-
unsigned int Index = 0;
96+
tU8 *pDataBase = (tU8 *) Eep->DataBase;
97+
tU16 Index = 0;
98+
tU8 Data;
6199

62100
/*
63101
1) 인수
@@ -70,45 +108,24 @@ void DoEepReadControl(tag_EepControl *Eep)
70108
- 해당 인스턴스의 eeprom으로 부터 데이터를 읽어와 대상 버퍼에 값을 대입.
71109
*/
72110

73-
if(Eep->Bit.Init == false)
111+
if(Eep->Bit.InitComplete == false)
74112
{
75113
return;
76114
/* error or disabled */
77115
}
78116

79117
do
80118
{
81-
Eep->DataBase[Index] = Eeprom_Read(Eep->EepBase + (Index));
119+
Eep->Config->EepromRead(Eep->EepBase + (Index), &Data);
120+
pDataBase[Index] = Data;
82121
}while(++Index < Eep->Length);
83122
}
84123
/*********************************************************************************/
85-
void GetDataFromEeprom(char* const Dest, const int EepBase, int Length)
86-
{
87-
unsigned int Index = 0;
88-
89-
/*
90-
1) 인수
91-
- Dest : eeprom으로 부터 읽은 데이터를 대입할 대상의 주소
92-
- EepBase : 읽을 eeprom의 시작주소 (메모리 주소가 아님)
93-
- Length : 읽어올 데이터의 길이.
94-
95-
2) 반환
96-
- 없음.
97-
98-
3) 설명
99-
- tag_EepControl 구조체를 사용하지 않고 임의 eeprom 영역으로 부터 데이터를 읽어와 대상 버퍼에 값을 대입한다.
100-
*/
101-
102-
do
103-
{
104-
Dest[Index] = Eeprom_Read(EepBase + Index);
105-
}while(++Index < Length);
106-
}
107-
/*********************************************************************************/
108-
char DoEepWriteControl(tag_EepControl *Eep)
124+
tU8 DoEepWriteControl(tag_EepControl *Eep)
109125
{
110-
unsigned int *pIndex = (unsigned int *) &Eep->Index;
126+
tU16 *pIndex = (tU16 *) &Eep->Index;
111127
tag_EepBitField *pBit;
128+
tU8 Data = 0;
112129

113130
/*
114131
1) 인수
@@ -125,18 +142,18 @@ char DoEepWriteControl(tag_EepControl *Eep)
125142
- 값이 유효하여 쓰기를 진행하거나 해당영역의 마지막에 도달할 경우에만 while loop를 탈출한다.
126143
*/
127144

128-
129-
if((Eep->Bit.Init == false) || (Eep->Bit.Write == false))
145+
if((Eep->Bit.InitComplete == false) || (Eep->Bit.Write == false))
130146
{
131147
return false;
132148
/* error or disabled */
133149
}
134150

135151
while(true)
136152
{
137-
if(Eeprom_Read(Eep->EepBase + (*pIndex)) != Eep->DataBase[*pIndex])
153+
Eep->Config->EepromRead(Eep->EepBase + (*pIndex), &Data);
154+
if(Data != Eep->DataBase[*pIndex])
138155
{
139-
Eeprom_Write(Eep->EepBase + (*pIndex), Eep->DataBase[*pIndex]);
156+
Eep->Config->EepromWrite(Eep->EepBase + (*pIndex), Eep->DataBase[*pIndex]);
140157
return true;
141158
/* check valid data */
142159
}
@@ -165,42 +182,16 @@ void SetEepWriteEnable(tag_EepControl *Eep)
165182
- 'Eep.Bit.Write' 상태가 'true'일 때 DoEepWriteControl()함수가 쓰기 동작을 실행한다.
166183
*/
167184

185+
if(Eep->Bit.InitComplete == false)
186+
{
187+
return;
188+
/* error or disabled */
189+
}
190+
168191
tag_EepBitField *pBit = (tag_EepBitField *) &Eep->Bit;
169-
unsigned int *pIndex = (unsigned int *) &Eep->Index;
192+
tU16 *pIndex = (tU16 *) &Eep->Index;
170193

171194
pBit->Write = true;
172195
*pIndex = 0;
173196
}
174197
/*********************************************************************************/
175-
void Eeprom_Write(unsigned int Addr, char Data)
176-
{
177-
char cSREG;
178-
179-
while(EECR & 0x02) _WDR();
180-
EEAR = Addr;
181-
EEDR = Data;
182-
183-
cSREG = SREG;
184-
/* stored status register */
185-
186-
SREG &= ~0x80;
187-
/* disable global interrupt */
188-
189-
EECR |= 0x04;
190-
/* enable EEPROM Master Write EEMWE */
191-
192-
EECR |= 0x02;
193-
/* enable EEPROM Write EEWE */
194-
195-
SREG = cSREG;
196-
/* restored register */
197-
}
198-
/*********************************************************************************/
199-
char Eeprom_Read(unsigned int Addr)
200-
{
201-
while(EECR & 0x02) _WDR();
202-
EEAR = Addr;
203-
EECR |= 0x01;
204-
return EEDR;
205-
}
206-
/*********************************************************************************/
Lines changed: 41 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
11
/*********************************************************************************/
22
/*
33
* Author : Jeong Hyun Gu
4-
* File name : AvrEeprom.h
4+
* File name : SysEeprom.h
55
*/
66
/*********************************************************************************/
7-
#ifndef __AVR_EEPROM_H__
8-
#define __AVR_EEPROM_H__
7+
#ifndef __SYS_EEPROM_H__
8+
#define __SYS_EEPROM_H__
99
/*********************************************************************************/
10-
#define AVR_EEPROM_REVISION_DATE 20190114
10+
#include "SysTypedef.h"
11+
/*********************************************************************************/
12+
#define SYS_EEPROM_REVISION_DATE 20190723
1113
/*********************************************************************************/
1214
/** REVISION HISTORY **/
1315
/*
16+
2019. 07. 23. - Eeprom_Write(), Eeprom_Read() 삭제하고 HAL 적용.
17+
Jeong Hyun Gu - 접두어 AVR -> SYS로 변경.
18+
- GetDataFromEeprom() 삭제.
19+
- "tag_EepCommonConfig" 추가.
20+
1421
2019. 01. 14. - CPU타입 __AVR_ATMEGA2560__ 추가.
1522
Jeong Hyun Gu
1623
@@ -38,46 +45,6 @@
3845
#define true 1
3946
#define false 0
4047

41-
42-
#define __AVR_ATMEGA2560__
43-
44-
45-
#ifdef __AVR_ATMEGA8__
46-
#include <iom8.h>
47-
#define EEPROM_SIZE 512
48-
/* Endurance: 100,000 Write/Erase */
49-
#endif
50-
51-
#ifdef __AVR_ATMEGA16__
52-
#include <iom16.h>
53-
#define EEPROM_SIZE 512
54-
/* Endurance: 100,000 Write/Erase */
55-
#endif
56-
57-
#ifdef __AVR_ATMEGA32__
58-
#include <iom32.h>
59-
#define EEPROM_SIZE 1024
60-
/* Endurance: 100,000 Write/Erase */
61-
#endif
62-
63-
#ifdef __AVR_ATMEGA64__
64-
#include <iom64.h>
65-
#define EEPROM_SIZE 2048
66-
/* Endurance: 100,000 Write/Erase */
67-
#endif
68-
69-
#ifdef __AVR_ATMEGA128__
70-
#include <iom128.h>
71-
#define EEPROM_SIZE 4096
72-
/* Endurance: 100,000 Write/Erase */
73-
#endif
74-
75-
#ifdef __AVR_ATMEGA2560__
76-
#include <iom2560.h>
77-
#define EEPROM_SIZE 4096
78-
/* Endurance: 100,000 Write/Erase */
79-
#endif
80-
8148
/*********************************************************************************/
8249
/**Enum**/
8350

@@ -87,33 +54,46 @@
8754

8855
typedef struct
8956
{
90-
char Init : 1;
91-
char Write : 1;
57+
struct
58+
{
59+
tU8 Init : 1;
60+
}Bit;
61+
62+
const tU16 LastAddr; // eeprom마지막 주소(크기)
63+
tU16 AllocEepAddr;
64+
65+
tU8 (*EepromWrite)(tU16 Addr, tU8 Data);
66+
tU8 (*EepromRead)(tU16 Addr, tU8 *pData);
67+
}tag_EepCommonConfig;
68+
69+
typedef struct
70+
{
71+
tU8 InitGeneral : 1;
72+
tU8 InitComplete : 1;
73+
tU8 Write : 1;
9274
}tag_EepBitField;
9375

94-
typedef const struct
76+
typedef struct
9577
{
96-
char *DataBase; // 관리할 데이터의 시작 주소
97-
unsigned int EepBase; // eeprom의 시작 주소
98-
unsigned int Index; // 쓰기 인덱스
99-
unsigned int Length; // 관리할 데이터의 길이
78+
const tag_EepBitField Bit;
79+
const tag_EepCommonConfig *Config;
10080

101-
tag_EepBitField Bit;
81+
const tU16 EepBase; // eeprom의 시작 주소
82+
const tU8 *DataBase; // 관리할 데이터의 시작 주소
83+
84+
const tU16 Index; // 쓰기 인덱스
85+
const tU16 Length; // 관리할 데이터의 길이
10286
}tag_EepControl;
10387

10488
/*********************************************************************************/
10589
/**Function**/
10690

107-
char InitEepControl(tag_EepControl *Eep, const unsigned char *DataBase, unsigned int Length);
108-
109-
void DoEepReadControl(tag_EepControl *Eep);
110-
void GetDataFromEeprom(char* const Dest, const int EepBase, int Length);
91+
tU8 InitEepCommonConfig(tag_EepCommonConfig *EepConfig, tU16 LastAddr, tU8 (*EepromWrite)(tU16 Addr, tU8 Data), tU8 (*EepromRead)(tU16 Addr, tU8 *pData));
92+
tU8 InitEepControl(tag_EepControl *Eep, const tU8 *DataBase, tU16 Length, tag_EepCommonConfig *EepConfig);
11193

112-
char DoEepWriteControl(tag_EepControl *Eep);
11394
void SetEepWriteEnable(tag_EepControl *Eep);
114-
115-
void Eeprom_Write(unsigned int Addr, char Data);
116-
char Eeprom_Read(unsigned int Addr);
95+
void DoEepReadControl(tag_EepControl *Eep);
96+
tU8 DoEepWriteControl(tag_EepControl *Eep);
11797

11898
/*********************************************************************************/
119-
#endif //__AVR_EEPROM_H__
99+
#endif //__SYS_EEPROM_H__

0 commit comments

Comments
 (0)