|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include "C++Utilities/CppUtils.hpp" |
| 4 | +#include "Concepts/Concepts.hpp" |
| 5 | +#include "ErrorHandler/ErrorHandler.hpp" |
| 6 | +#include "Flash/Flash.hpp" |
| 7 | +#include "FlashVariable.hpp" |
| 8 | + |
| 9 | +#define FLASH_STORER_MAX_SIZE ((uint32_t)(1028*64)) |
| 10 | +#define FLASH_STORER_START_ADDRESS (FLASH_STORER_MAX_ADDRESS - FLASH_STORER_MAX_SIZE) |
| 11 | +#define FLASH_STORER_MAX_ADDRESS ((uint32_t)0x080DFFF0U) |
| 12 | + |
| 13 | + |
| 14 | +class FlashStorer{ |
| 15 | + |
| 16 | +public: |
| 17 | + static vector<FlashVariable> variable_list; |
| 18 | + static uint32_t total_size; |
| 19 | + |
| 20 | + template<class... Type> |
| 21 | + static bool add_variables(Type&... var); |
| 22 | + static void read(void); |
| 23 | + static bool store_all(void); |
| 24 | + template<class Type> |
| 25 | + static bool store(Type& var); |
| 26 | + template<class... Type> requires requires{requires sizeof...(Type) != 1;} |
| 27 | + static bool store(Type&... var); |
| 28 | + |
| 29 | +private: |
| 30 | + template<class Type> |
| 31 | + static void add_size(Type& var);//?Mirar, si no hace falta borrar |
| 32 | +}; |
| 33 | + |
| 34 | +template<class Type> |
| 35 | +bool FlashStorer::store(Type& var) { |
| 36 | + uint32_t number_of_words = ((FlashStorer::total_size + FLASH_32BITS_WORLD - 1) / FLASH_32BITS_WORLD); //Fast ceiling the dvision result |
| 37 | + |
| 38 | + uint32_t* data = (uint32_t*)malloc(number_of_words * FLASH_32BITS_WORLD); |
| 39 | + Flash::read(FLASH_STORER_START_ADDRESS, (uint32_t*)data, number_of_words); |
| 40 | + |
| 41 | + uint32_t offset = 0; |
| 42 | + for(FlashVariable& v: FlashStorer::variable_list){ |
| 43 | + if (v.get_pointer() == &var) { |
| 44 | + memcpy((((uint8_t*)data) + offset), v.get_pointer(), v.get_size()); |
| 45 | + } |
| 46 | + |
| 47 | + offset += v.get_size(); |
| 48 | + } |
| 49 | + |
| 50 | + if(not Flash::write(data, FLASH_STORER_START_ADDRESS, number_of_words)){ |
| 51 | + return false; |
| 52 | + } |
| 53 | + |
| 54 | + free(data); |
| 55 | + return true; |
| 56 | +} |
| 57 | + |
| 58 | +template<class... Type> requires requires{requires sizeof...(Type) != 1;} |
| 59 | +bool FlashStorer::store(Type&... var){ |
| 60 | + return (FlashStorer::store(var) & ...); |
| 61 | +} |
| 62 | + |
| 63 | +template<class... Type> |
| 64 | +bool FlashStorer::add_variables(Type&... var){ |
| 65 | + (FlashStorer::variable_list.push_back(FlashVariable(var)), ...); |
| 66 | + FlashStorer::total_size = total_sizeof<Type...>::value; |
| 67 | + |
| 68 | + return FlashStorer::total_size >= FLASH_STORER_MAX_SIZE; |
| 69 | +} |
| 70 | + |
| 71 | + |
0 commit comments