2 * This file is part of Cleanflight.
4 * Cleanflight is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * Cleanflight is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
21 #include "drivers/system.h"
22 #include "config/config_streamer.h"
23 #include "common/utils.h"
25 #if defined(CONFIG_IN_FILE)
31 #define FLASH_PAGE_SIZE (0x400)
33 static FILE *eepromFd
= NULL
;
34 static bool streamerLocked
= true;
35 static char eepromPath
[260] = EEPROM_FILENAME
;
37 bool configFileSetPath(char* path
)
39 if(!path
|| strlen(path
) > 260) {
43 strcpy(eepromPath
, path
);
47 void config_streamer_impl_unlock(void)
49 if (eepromFd
!= NULL
) {
50 fprintf(stderr
, "[EEPROM] Unable to load %s\n", eepromPath
);
55 eepromFd
= fopen(eepromPath
,"r+");
56 if (eepromFd
!= NULL
) {
58 fseek(eepromFd
, 0 , SEEK_END
);
59 size_t size
= ftell(eepromFd
);
62 size_t n
= fread(eepromData
, 1, sizeof(eepromData
), eepromFd
);
64 fprintf(stderr
,"[EEPROM] Loaded '%s' (%ld of %ld bytes)\n", eepromPath
, size
, sizeof(eepromData
));
65 streamerLocked
= false;
67 fprintf(stderr
, "[EEPROM] Failed to load '%s'\n", eepromPath
);
70 printf("[EEPROM] Created '%s', size = %ld\n", eepromPath
, sizeof(eepromData
));
71 streamerLocked
= false;
72 if ((eepromFd
= fopen(eepromPath
, "w+")) == NULL
) {
73 fprintf(stderr
, "[EEPROM] Failed to create '%s'\n", eepromPath
);
74 streamerLocked
= true;
76 if (fwrite(eepromData
, sizeof(eepromData
), 1, eepromFd
) != 1) {
77 fprintf(stderr
, "[EEPROM] Write failed: %s\n", strerror(errno
));
78 streamerLocked
= true;
83 void config_streamer_impl_lock(void)
86 if (eepromFd
!= NULL
) {
87 fseek(eepromFd
, 0, SEEK_SET
);
88 fwrite(eepromData
, 1, sizeof(eepromData
), eepromFd
);
91 fprintf(stderr
, "[EEPROM] Saved '%s'\n", eepromPath
);
92 streamerLocked
= false;
94 fprintf(stderr
, "[EEPROM] Unlock error\n");
98 int config_streamer_impl_write_word(config_streamer_t
*c
, config_streamer_buffer_align_type_t
*buffer
)
100 if (streamerLocked
) {
104 if ((c
->address
>= (uintptr_t)eepromData
) && (c
->address
< (uintptr_t)ARRAYEND(eepromData
))) {
105 *((uint32_t*)c
->address
) = *buffer
;
106 fprintf(stderr
, "[EEPROM] Program word %p = %08x\n", (void*)c
->address
, *((uint32_t*)c
->address
));
108 fprintf(stderr
, "[EEPROM] Program word %p out of range!\n", (void*)c
->address
);
111 c
->address
+= CONFIG_STREAMER_BUFFER_SIZE
;