Fix doc path
[opentx.git] / radio / src / storage / eeprom_rlc.h
blob9585449454f879dbe00052c6d5387affe1a4f1b2
1 /*
2 * Copyright (C) OpenTX
4 * Based on code named
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"
26 #if defined(CPUARM)
27 #define blkid_t uint16_t
28 #define EEFS_VERS 5
29 #define MAXFILES 62
30 #define BS 64
31 #elif defined(CPUM2560) || defined(CPUM2561) || defined(CPUM128)
32 #define blkid_t uint8_t
33 #define EEFS_VERS 5
34 #define MAXFILES 36
35 #define BS 16
36 #else
37 #define blkid_t uint8_t
38 #define EEFS_VERS 4
39 #define MAXFILES 20
40 #define BS 16
41 #endif
43 PACK(struct DirEnt {
44 blkid_t startBlk;
45 uint16_t size:12;
46 uint16_t typ:4;
47 });
49 #if defined(CPUARM)
50 #define EEFS_EXTRA_FIELDS uint8_t spare[2];
51 #else
52 #define EEFS_EXTRA_FIELDS
53 #endif
55 PACK(struct EeFs {
56 uint8_t version;
57 blkid_t mySize;
58 blkid_t freeList;
59 uint8_t bs;
60 EEFS_EXTRA_FIELDS
61 DirEnt files[MAXFILES];
62 });
64 extern EeFs eeFs;
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)
77 #if defined(CPUM64)
78 #define FIRSTBLK (RESV/BS)
79 #define BLOCKS (EEPROM_SIZE/BS)
80 #define BLOCKS_OFFSET 0
81 #else
82 #define FIRSTBLK 1
83 #define BLOCKS (1+(EEPROM_SIZE-RESV)/BS)
84 #define BLOCKS_OFFSET (RESV-BS)
85 #endif
87 uint16_t EeFsGetFree();
89 class EFile
91 public:
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);
107 // protected:
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))
118 #define ERR_NONE 0
119 #define ERR_FULL 1
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
130 uint8_t m_zeroes;
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;
141 uint16_t m_rlc_len;
142 uint8_t * m_rlc_buf;
143 uint8_t m_cur_rlc_len;
144 uint8_t m_write1_byte;
145 uint8_t m_write_len;
146 uint8_t * m_write_buf;
147 #if defined (EEPROM_PROGRESS_BAR)
148 uint8_t m_ratio;
149 #endif
151 public:
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
168 void flush();
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);
175 #endif
178 extern RlcFile theFile; //used for any file operation
180 inline void eeFlush()
182 theFile.flush();
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)
199 #else
200 #define DISPLAY_PROGRESS_BAR(x)
201 #endif
203 #if defined(CPUARM)
204 bool eeCopyModel(uint8_t dst, uint8_t src);
205 void eeSwapModels(uint8_t id1, uint8_t id2);
206 void eeDeleteModel(uint8_t idx);
207 #else
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))
211 #endif
213 #if defined(SDCARD)
214 const pm_char * eeBackupModel(uint8_t i_fileSrc);
215 const pm_char * eeRestoreModel(uint8_t i_fileDst, char *model_name);
216 #endif
218 // For conversions
219 #if defined(CPUARM)
220 void loadRadioSettingsSettings();
221 void loadModel(int index, bool alarms=true);
222 #endif
224 bool eepromOpen();
225 void eeLoadModelName(uint8_t id, char *name);
226 bool eeLoadGeneral();
228 // For EEPROM backup/restore
229 #if defined(CPUARM)
230 inline bool isEepromStart(const void * buffer)
232 // OpenTX EEPROM
234 const EeFs * eeprom = (const EeFs *)buffer;
235 if (eeprom->version==EEFS_VERS && eeprom->mySize==sizeof(eeFs) && eeprom->bs==BS)
236 return true;
239 // ersky9x EEPROM
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)
245 return true;
248 return false;
251 void eepromBackup();
252 #endif
254 #endif // _EEPROM_RLC_H_