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/*********************************************************************************/
5894void 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- /*********************************************************************************/
0 commit comments