0% found this document useful (0 votes)
8 views

Code

The fileSystem.c file contains code for managing a file system in embedded memory. It includes functions for initializing the file system, creating new files, appending data to existing files, recovering files, and resetting files. Key functions convert between decimal and binary representations for storing memory addresses and lengths in the file system data structure.

Uploaded by

Eduardo Pinheiro
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Code

The fileSystem.c file contains code for managing a file system in embedded memory. It includes functions for initializing the file system, creating new files, appending data to existing files, recovering files, and resetting files. Key functions convert between decimal and binary representations for storing memory addresses and lengths in the file system data structure.

Uploaded by

Eduardo Pinheiro
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

fileSystem.

1 /*
2 * fileSystem.c
3 *
4 * Created on: 23 de dez de 2022
5 * Author: edu_e
6 */
7
8
9 #include "main.h"
10 #include "application.h"
11 #include "string.h"
12 #include "string.h"
13 #include "stdlib.h"
14 #include "stdio.h"
15 #include "fileSystem.h"
16 #include "measurements.h"
17
18 #include "at24c64.h"
19 extern uint8_t vetorDebug[MAX_LEN_MEM_AREA_SYSTEM];
20 extern RTC_TimeTypeDef sTime;
21 extern RTC_DateTypeDef DateToUpdate;
22
23 MEM_FILE tempFile;
24 MEM_FILE_DECIMAL tempFileDecimal;
25 uint8_t vectorWithMaskForTheMemory[MAX_LEN_FS_DATA];
26
27 void FS_initialize(void)
28 {
29 //eepromRead(BANK1, (uint16_t)MEM_COUNTER_USED,&tempFile.header,
(uint16_t)(MAX_LEN_MEM_FILE));
30 if(tempFile.StructFolder.flagMemStatus == MEM_UNSTARTED)
31 {
32 memset(tempFile.header,0x00,MAX_LEN_MEM_FILE);
33 tempFile.StructFolder.flagMemStatus = RELEASE_TO_START;
34 eepromWrite(BANK1,
(uint16_t)MEM_COUNTER_USED,&tempFile.header,MAX_LEN_MEM_FILE);
35 }
36 }
37
38 void FS_new_file(void)
39 {
40 eepromRead(BANK1, (uint16_t)MEM_COUNTER_USED,&tempFile.header,
(uint16_t)(MEM_START_DATA_SAVED2 - MEM_COUNTER_USED)); //carrego header
41
42 if(tempFile.StructFolder.flagMemStatus & RELEASE_TO_START)//||
tempFile.StructFolder.flagMemStatus & EMPTY_MEMORY)
43 {
44 FS_convertMemoryDecimal();
45
46 tempFile.StructFolder.Date.Day = DateToUpdate.Date;

Page 1
fileSystem.c

47 tempFile.StructFolder.Date.Month = DateToUpdate.Month;
48 tempFile.StructFolder.Date.Year = DateToUpdate.Year;
49
50 tempFile.StructFolder.Date.Hours.Hour = convertHourToRTC(sT
ime.Hours , RTC_TO_HOUR);
51 tempFile.StructFolder.Date.Hours.Minute = convertHourToRTC(sT
ime.Minutes , RTC_TO_HOUR);
52 tempFile.StructFolder.Date.Hours.Second = convertHourToRTC(sT
ime.Seconds , RTC_TO_HOUR);
53
54 if(!(tempFile.StructFolder.flagMemStatus &
MEMORY_FAIL_BACKUP_USED))
55 {
56 tempFileDecimal.startAddrDecimal =
tempFileDecimal.lenDataEEPROM1Decimal >
tempFileDecimal.lenDataEEPROM2Decimal ?
tempFileDecimal.lenDataEEPROM1Decimal :
tempFileDecimal.lenDataEEPROM2Decimal;
57 if(tempFileDecimal.startAddrDecimal == 0x00)
58 {
59 tempFileDecimal.startAddrDecimal += MEM_START_FS;
60 }
61 }
62 else
63 {
64 tempFileDecimal.startAddrBkpDecimal =
tempFileDecimal.lenDataEEPROM1BkpDecimal >
tempFileDecimal.lenDataEEPROM2BkpDecimal ?
tempFileDecimal.lenDataEEPROM1BkpDecimal :
tempFileDecimal.lenDataEEPROM2BkpDecimal;
65 if(tempFileDecimal.startAddrBkpDecimal == 0x00)
66 {
67 tempFileDecimal.startAddrBkpDecimal += MEM_START_FS;
68 }
69 }
70 FS_convertMemoryVector();
71 tempFile.StructFolder.flagMemStatus += (MEMORY_IN_USE -
RELEASE_TO_START); //mantem apenas MEMORY_IN_USE
72 eepromWrite(BANK1,
(uint16_t)MEM_COUNTER_USED,&tempFile.header,(uint16_t)MAX_LEN_MEM_FILE);
73 }
74 }
75
76 void FS_append(TypeTempIN sensor)
77 {
78 uint8_t fLock = DISABLE;
79 uint16_t tempAddr;
80 memset(tempFile.header,0x00,MAX_LEN_MEM_FILE);
81 eepromRead(BANK1, (uint16_t)MEM_COUNTER_USED,&tempFile.header,
(uint16_t)(MAX_LEN_MEM_FILE));

Page 2
fileSystem.c

82
83 if(tempFile.StructFolder.flagMemStatus & MEMORY_IN_USE)
84 {
85 FS_convertMemoryDecimal();
86
87 if(!(tempFile.StructFolder.flagMemStatus &
MEMORY_FAIL_BACKUP_USED))
88 {
89 if(tempFileDecimal.lenDataEEPROM1Decimal == 0 ||
tempFileDecimal.lenDataEEPROM2Decimal == 0)
90 {
91 if(tempFileDecimal.lenDataEEPROM1Decimal >
tempFileDecimal.lenDataEEPROM2Decimal)
92 {
93 tempAddr = tempFileDecimal.lenDataEEPROM1Decimal;
94 }
95 else
96 {
97 tempAddr = tempFileDecimal.lenDataEEPROM2Decimal;
98 }
99 fLock = ENABLE;
100 }
101 else if(tempFileDecimal.lenDataEEPROM1Decimal >=
tempFileDecimal.lenDataEEPROM2Decimal)
102 {
103 tempFileDecimal.lenDataEEPROM2Decimal =
tempFileDecimal.lenDataEEPROM1Decimal + 4;
104 tempAddr = tempFileDecimal.lenDataEEPROM2Decimal;
105 fLock = ENABLE;
106 }
107 else
108 {
109 tempFileDecimal.lenDataEEPROM1Decimal =
tempFileDecimal.lenDataEEPROM2Decimal + 4;
110 tempAddr = tempFileDecimal.lenDataEEPROM1Decimal;
111 fLock = ENABLE;
112 }
113 }
114 else
115 {
116 if(tempFileDecimal.lenDataEEPROM1BkpDecimal == 0 ||
tempFileDecimal.lenDataEEPROM2BkpDecimal == 0)
117 {
118 if(tempFileDecimal.lenDataEEPROM1BkpDecimal >
tempFileDecimal.lenDataEEPROM2BkpDecimal)
119 {
120 tempAddr = tempFileDecimal.lenDataEEPROM1BkpDecimal;
121 }
122 else
123 {

Page 3
fileSystem.c

124 tempAddr = tempFileDecimal.lenDataEEPROM2BkpDecimal;


125 }
126 tempAddr = tempFileDecimal.lenDataEEPROM1BkpDecimal;
127 fLock = ENABLE;
128 }
129 else if(tempFileDecimal.lenDataEEPROM1BkpDecimal >=
tempFileDecimal.lenDataEEPROM2BkpDecimal)
130 {
131 tempFileDecimal.lenDataEEPROM2BkpDecimal =
tempFileDecimal.lenDataEEPROM1BkpDecimal + 4;
132 tempAddr = tempFileDecimal.lenDataEEPROM2BkpDecimal;
133 fLock = ENABLE;
134 }
135 else
136 {
137 tempFileDecimal.lenDataEEPROM1BkpDecimal =
tempFileDecimal.lenDataEEPROM2BkpDecimal + 4;
138 tempAddr = tempFileDecimal.lenDataEEPROM1BkpDecimal;
139 fLock = ENABLE;
140 }
141 }
142 }
143
144 if( fLock == ENABLE)
145 {
146 FS_packDataToMemory(sensor);
147
148 eepromWrite(BANK1, (uint16_t)(tempFileDecimal.startAddrDecimal +
tempAddr) ,vectorWithMaskForTheMemory,(uint16_t) MAX_LEN_FS_DATA);
149 /* LOG MEMORY ONLY
150 memset(vectorWithMaskForTheMemory,0x00,MAX_LEN_FS_DATA);
151 eepromRead(BANK1, (uint16_t)(tempFileDecimal.startAddrDecimal +
tempAddr) ,vectorWithMaskForTheMemory,(uint16_t) MAX_LEN_FS_DATA);*/
152
153 if(!(tempFile.StructFolder.flagMemStatus &
MEMORY_FAIL_BACKUP_USED))
154 {
155 if(tempFileDecimal.lenDataEEPROM1Decimal == 0 &&
tempFileDecimal.lenDataEEPROM2Decimal == 0)
156 {
157 tempFileDecimal.lenDataEEPROM1Decimal += 4;
158 }
159 }
160 else
161 {
162 if(tempFileDecimal.lenDataEEPROM1BkpDecimal == 0 &&
tempFileDecimal.lenDataEEPROM2BkpDecimal == 0)
163 {
164 tempFileDecimal.lenDataEEPROM1BkpDecimal += 4;
165 }

Page 4
fileSystem.c

166 }
167 }
168 FS_convertMemoryVector();
169 eepromWrite(BANK1,
(uint16_t)MEM_COUNTER_USED,&tempFile.header,(uint16_t)MAX_LEN_MEM_FILE);
170 /* LOG MEMORY ONLY
171 memset(vectorWithMaskForTheMemory, 0x00,MAX_LEN_FS_DATA);
172 eepromRead(BANK1, (uint16_t)(tempFileDecimal.startAddrDecimal +
tempAddr) ,vectorWithMaskForTheMemory,(uint16_t) MAX_LEN_FS_DATA);*/
173 }
174
175 void FS_recovey(void)
176 {
177
178 }
179
180 void FS_reset_file(void)
181 {
182
183 }
184
185 void FS_convertMemoryDecimal(void)
186 {
187 tempFileDecimal.startAddrDecimal = 0;
188 tempFileDecimal.startAddrDecimal |= tempFile.StructFolder.startAddr[0];
189 tempFileDecimal.startAddrDecimal |= tempFile.StructFolder.startAddr[1]
<< 8;
190
191 tempFileDecimal.startAddrBkpDecimal = 0;
192 tempFileDecimal.startAddrBkpDecimal |=
tempFile.StructFolder.startAddrBkp[0];
193 tempFileDecimal.startAddrBkpDecimal |=
tempFile.StructFolder.startAddrBkp[1] << 8;
194
195
196 tempFileDecimal.lenDataEEPROM1Decimal = 0;
197 tempFileDecimal.lenDataEEPROM1Decimal |=
tempFile.StructFolder.lenDataInEeprom1[0];
198 tempFileDecimal.lenDataEEPROM1Decimal |=
tempFile.StructFolder.lenDataInEeprom1[1] << 8;
199
200 tempFileDecimal.lenDataEEPROM2Decimal = 0;
201 tempFileDecimal.lenDataEEPROM2Decimal |=
tempFile.StructFolder.lenDataInEeprom2[0];
202 tempFileDecimal.lenDataEEPROM2Decimal |=
tempFile.StructFolder.lenDataInEeprom2[1] <<8;
203
204 tempFileDecimal.lenDataEEPROM1BkpDecimal = 0;
205 tempFileDecimal.lenDataEEPROM1BkpDecimal |=
tempFile.StructFolder.lenDataInEeprom1Bkp[0];

Page 5
fileSystem.c

206 tempFileDecimal.lenDataEEPROM1BkpDecimal |=
tempFile.StructFolder.lenDataInEeprom1Bkp[1] <<8;
207
208 tempFileDecimal.lenDataEEPROM2BkpDecimal = 0;
209 tempFileDecimal.lenDataEEPROM2BkpDecimal |=
tempFile.StructFolder.lenDataInEeprom2Bkp[0];
210 tempFileDecimal.lenDataEEPROM2BkpDecimal |=
tempFile.StructFolder.lenDataInEeprom2Bkp[1] <<8;
211 }
212
213 void FS_convertMemoryVector(void)
214 {
215 tempFile.StructFolder.startAddr[0] = (uint8_t)
(tempFileDecimal.startAddrDecimal);
216 tempFile.StructFolder.startAddr[1] = (uint8_t)
(tempFileDecimal.startAddrDecimal >> 8);
217
218 tempFile.StructFolder.lenDataInEeprom1[0] = (uint8_t)
(tempFileDecimal.lenDataEEPROM1Decimal);
219 tempFile.StructFolder.lenDataInEeprom1[1] = (uint8_t)
(tempFileDecimal.lenDataEEPROM1Decimal >> 8);
220
221 tempFile.StructFolder.lenDataInEeprom2[0] = (uint8_t)
(tempFileDecimal.lenDataEEPROM2Decimal);
222 tempFile.StructFolder.lenDataInEeprom2[1] = (uint8_t)
(tempFileDecimal.lenDataEEPROM2Decimal >> 8);
223
224 tempFile.StructFolder.lenDataInEeprom1Bkp[0] = (uint8_t)
(tempFileDecimal.lenDataEEPROM1BkpDecimal);
225 tempFile.StructFolder.lenDataInEeprom1Bkp[1] = (uint8_t)
(tempFileDecimal.lenDataEEPROM1BkpDecimal >> 8);
226
227 tempFile.StructFolder.lenDataInEeprom2Bkp[0] = (uint8_t)
(tempFileDecimal.lenDataEEPROM2BkpDecimal);
228 tempFile.StructFolder.lenDataInEeprom2Bkp[1] = (uint8_t)
(tempFileDecimal.lenDataEEPROM2BkpDecimal >> 8);
229 }
230
231 void FS_packDataToMemory(TypeTempIN sensor)
232 {
233 memset(vectorWithMaskForTheMemory, 0x00,MAX_LEN_FS_DATA);
234
235 vectorWithMaskForTheMemory[0] = (uint8_t)
(SensorsInput[sensor].tMeasure.temperature);
236 vectorWithMaskForTheMemory[1] = (uint8_t)
(SensorsInput[sensor].tMeasure.temperature >> 8);
237
238 vectorWithMaskForTheMemory[2] = (uint8_t)
(SensorsInput[sensor].tMeasure.umidity);
239 vectorWithMaskForTheMemory[3] = (uint8_t)

Page 6
fileSystem.c

(SensorsInput[sensor].tMeasure.umidity >> 8);


240
241 if(SensorsInput[sensor].tAquisition == TEMP_NONE)
242 {
243 vectorWithMaskForTheMemory[0] |= 0b11000000;
244 vectorWithMaskForTheMemory[1] |= 0b11000000;
245 vectorWithMaskForTheMemory[2] |= 0b11000000;
246 vectorWithMaskForTheMemory[3] |= 0b11000000;
247 }
248 else if(SensorsInput[sensor].tAquisition == TEMPERATURE)
249 {
250 vectorWithMaskForTheMemory[0] |= 0b01000000;
251 vectorWithMaskForTheMemory[1] |= 0b01000000;
252 vectorWithMaskForTheMemory[2] |= 0b11000000;
253 vectorWithMaskForTheMemory[3] |= 0b11000000;
254 }
255 else if(SensorsInput[sensor].tAquisition == HUMIDITY)
256 {
257 vectorWithMaskForTheMemory[0] |= 0b11000000;
258 vectorWithMaskForTheMemory[1] |= 0b11000000;
259 vectorWithMaskForTheMemory[2] |= 0b10000000;
260 vectorWithMaskForTheMemory[3] |= 0b10000000;
261 }
262
263 switch(sensor)
264 {
265 case TEMP2:
266 vectorWithMaskForTheMemory[1] |= 0b00010000;
267 vectorWithMaskForTheMemory[3] |= 0b00010000;
268 break;
269 case TEMP3:
270 vectorWithMaskForTheMemory[1] |= 0b00100000;
271 vectorWithMaskForTheMemory[3] |= 0b00100000;
272 break;
273 case TEMP4:
274 vectorWithMaskForTheMemory[1] |= 0b00110000;
275 vectorWithMaskForTheMemory[3] |= 0b00110000;
276 break;
277 default:
278 break;
279 }
280 }
281

Page 7

You might also like