5 * th9x - http://code.google.com/p/th9x
6 * er9x - http://code.google.com/p/er9x
7 * gruvin9x - http://code.google.com/p/gruvin9x
9 * License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
21 #ifndef _EEPROM_RLC_H_
22 #define _EEPROM_RLC_H_
24 #include "definitions.h"
27 #define blkid_t uint16_t
31 #elif defined(CPUM2560) || defined(CPUM2561) || defined(CPUM128)
32 #define blkid_t uint8_t
37 #define blkid_t uint8_t
50 #define EEFS_EXTRA_FIELDS uint8_t spare[2];
52 #define EEFS_EXTRA_FIELDS
61 DirEnt files
[MAXFILES
];
66 #define FILE_TYP_GENERAL 1
67 #define FILE_TYP_MODEL 2
69 /// fileId of general file
70 #define FILE_GENERAL 0
71 /// convert model number 0..MAX_MODELS-1 int fileId
72 #define FILE_MODEL(n) (1+(n))
73 #define FILE_TMP (1+MAX_MODELS)
75 #define RESV sizeof(EeFs) //reserv for eeprom header with directory (eeFs)
78 #define FIRSTBLK (RESV/BS)
79 #define BLOCKS (EEPROM_SIZE/BS)
80 #define BLOCKS_OFFSET 0
83 #define BLOCKS (1+(EEPROM_SIZE-RESV)/BS)
84 #define BLOCKS_OFFSET (RESV-BS)
87 uint16_t EeFsGetFree();
93 ///remove contents of given file
94 static void rm(uint8_t i_fileId
);
96 ///swap contents of file1 with them of file2
97 static void swap(uint8_t i_fileId1
, uint8_t i_fileId2
);
99 ///return true if the file with given fileid exists
100 static bool exists(uint8_t i_fileId
);
102 ///open file for reading, no close necessary
103 void openRd(uint8_t i_fileId
);
105 uint8_t read(uint8_t *buf
, uint8_t len
);
109 uint8_t m_fileId
; //index of file in directory = filename
110 uint16_t m_pos
; //over all filepos
111 blkid_t m_currBlk
; //current block.id
112 uint8_t m_ofs
; //offset inside of the current block
115 #define eeFileSize(f) eeFs.files[f].size
116 #define eeModelSize(id) eeFileSize(FILE_MODEL(id))
120 extern uint8_t s_write_err
; // error reasons
121 inline uint8_t write_errno() { return s_write_err
; }
123 extern uint8_t s_sync_write
;
124 #define ENABLE_SYNC_WRITE(val) s_sync_write = val;
125 #define IS_SYNC_WRITE_ENABLE() s_sync_write
127 class RlcFile
: public EFile
129 uint8_t m_bRlc
; // control byte for run length decoder
132 #define WRITE_FIRST_LINK 0x01
133 #define WRITE_NEXT_LINK_1 0x02
134 #define WRITE_NEXT_LINK_2 0x03
135 #define WRITE_START_STEP 0x10
136 #define WRITE_FREE_UNUSED_BLOCKS_STEP1 0x20
137 #define WRITE_FREE_UNUSED_BLOCKS_STEP2 0x30
138 #define WRITE_FINAL_DIRENT_STEP 0x40
139 #define WRITE_TMP_DIRENT_STEP 0x50
140 uint8_t m_write_step
;
143 uint8_t m_cur_rlc_len
;
144 uint8_t m_write1_byte
;
146 uint8_t * m_write_buf
;
147 #if defined (EEPROM_PROGRESS_BAR)
153 void openRlc(uint8_t i_fileId
);
155 void create(uint8_t i_fileId
, uint8_t typ
, uint8_t sync_write
);
157 /// copy contents of i_fileSrc to i_fileDst
158 bool copy(uint8_t i_fileDst
, uint8_t i_fileSrc
);
160 inline bool isWriting() { return m_write_step
!= 0; }
161 void write(uint8_t *buf
, uint8_t i_len
);
162 void write1(uint8_t b
);
163 void nextWriteStep();
164 void nextRlcWriteStep();
165 void writeRlc(uint8_t i_fileId
, uint8_t typ
, uint8_t *buf
, uint16_t i_len
, uint8_t sync_write
);
167 // flush the current write operation if any
170 // read from opened file and decode rlc-coded data
171 uint16_t readRlc(uint8_t *buf
, uint16_t i_len
);
173 #if defined (EEPROM_PROGRESS_BAR)
174 void drawProgressBar(uint8_t x
);
178 extern RlcFile theFile
; //used for any file operation
180 inline void eeFlush()
185 void eepromWriteBlock(uint8_t * buffer
, size_t address
, size_t size
);
187 inline bool eepromIsWriting()
189 return theFile
.isWriting();
192 inline void eepromWriteProcess()
194 theFile
.nextWriteStep();
197 #if defined (EEPROM_PROGRESS_BAR)
198 #define DISPLAY_PROGRESS_BAR(x) theFile.drawProgressBar(x)
200 #define DISPLAY_PROGRESS_BAR(x)
204 bool eeCopyModel(uint8_t dst
, uint8_t src
);
205 void eeSwapModels(uint8_t id1
, uint8_t id2
);
206 void eeDeleteModel(uint8_t idx
);
208 #define eeCopyModel(dst, src) theFile.copy(FILE_MODEL(dst), FILE_MODEL(src))
209 #define eeSwapModels(id1, id2) EFile::swap(FILE_MODEL(id1), FILE_MODEL(id2))
210 #define eeDeleteModel(idx) EFile::rm(FILE_MODEL(idx))
214 const pm_char
* eeBackupModel(uint8_t i_fileSrc
);
215 const pm_char
* eeRestoreModel(uint8_t i_fileDst
, char *model_name
);
220 void loadRadioSettingsSettings();
221 void loadModel(int index
, bool alarms
=true);
225 void eeLoadModelName(uint8_t id
, char *name
);
226 bool eeLoadGeneral();
228 // For EEPROM backup/restore
230 inline bool isEepromStart(const void * buffer
)
234 const EeFs
* eeprom
= (const EeFs
*)buffer
;
235 if (eeprom
->version
==EEFS_VERS
&& eeprom
->mySize
==sizeof(eeFs
) && eeprom
->bs
==BS
)
241 const uint8_t * eeprom
= (const uint8_t *)buffer
;
242 uint8_t size
= eeprom
[1] ;
243 uint8_t bs
= eeprom
[3] ;
244 if (size
==0x80 && bs
==0x80)
254 #endif // _EEPROM_RLC_H_