Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

firmware/sys/storage: Re-modification of storage module #359

Merged
merged 1 commit into from
Oct 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 0 additions & 76 deletions firmware/storage_init.c

This file was deleted.

161 changes: 66 additions & 95 deletions firmware/sys/storage/include/storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,32 @@
#ifndef STORAGE_H
#define STORAGE_H

#include <stdio.h>
#include "mtd_flashpage.h"

#ifdef __cplusplus
extern "C" {
#endif

#define LAST_AVAILABLE_PAGE (FLASHPAGE_NUMOF - 1) /*!< Last position in the block EEPROM*/
#define MAX_SIZE_STORAGE (FLASHPAGE_SIZE) /*!< max size to save in the page */

#define MAX_NUMOF_FLASHPAGES (FLASHPAGE_NUMOF) /*!< max num of pages that can be manipulated */

#if CPU_SAMD21A
#undef MAX_NUMOF_FLASHPAGES
#define EEPROM_SIZE (FLASHPAGE_SIZE * FLASHPAGE_PAGES_PER_ROW * 64)
#define MAX_NUMOF_FLASHPAGES (EEPROM_SIZE / FLASHPAGE_SIZE)
#endif
#define MAX_SIZE_STORAGE (4096) /*!< max size of mtd_storage, only writeable 4K */
#define MAX_NUMOF_FLASHPAGES \
(MAX_SIZE_STORAGE / FLASHPAGE_SIZE) /*!< max num of pages that can be manipulated */

#define MTD_START_ADDR \
((uint32_t)(flashpage_addr(LAST_AVAILABLE_PAGE)) - \
MAX_SIZE_STORAGE) /*!< Reference to the first address writeable*/
#define MTD_LAST_ADDR \
(uint32_t)(flashpage_addr(LAST_AVAILABLE_PAGE)) /*!< Reference to the last address writeable*/
#define MTD_REGISTER_INDEX_LIMIT (512) /*!< Reference to max number of bytes to add an register*/

typedef struct {
uint16_t size;
uint8_t key[25];
uint8_t rwp;
uint32_t ptr_content;
} mtd_register_t;

#define MTD_REG_IDX_NUMOF (MTD_REGISTER_INDEX_LIMIT / sizeof(mtd_register_t)) /*!< */

/** @note The storage EEPROM section page could be resize with the bootloader block size
* @warning Always the block EEPROM and BOOTLOADER are affected between them.
Expand All @@ -56,126 +65,88 @@ extern "C" {
* @return 0 Satisfactory result
* -1 Failed result
*/
int mtd_start(void);
int8_t mtd_start(void);

/**
* @brief This function is executed by mtd write_string and mtd_write_uint8 internally. It is not
* recommended to use it directly, use the functions mentioned
* @brief Saves any value in any position of mtd_storage.
*
* @param [in] key this is the address where will be saved the data in the memory
* @param [out] value value to save
* @retval 0 Satisfactory result
* @retval -1 Failed result
* @param [in] value Any type of value.
* @param [in] len size of @p value that will be saved.
* @param [in] offset reference to an position in mtd_storage.
* @return int8_t
*/
int mtd_save(uint32_t key, void *value);
int mtd_save(void *value, uint32_t len, uint32_t offset);

/**
* @brief This function is executed to save any data type or any strutc, The function saves all the
* information compressed in EEPROM/FLASH_MEMORY. The data is stored in each page of the
* EEPROM/MEMORY_FLASH section.
* @warning: The EEPROM/FLASH_MEMORY section size depends of itself hardware.
*
* @param [in] value The input value given could be any datatype or data struct
* @param [in] len size of the all elements in the container of @p value
* @retval 0 Satisfactory result
* @retval -1 Failed result
* @brief Loads any value in any position of mtd_storage.
*
* @param [out] value Any type of value.
* @param [in] len size of @p value that will be loaded.
* @param [in] offset reference to an position in mtd_storage.
* @return int8_t
*/
int mtd_save_compress(void *value, uint16_t len);
int mtd_load(void *value, uint16_t len, uint32_t offset);

/**
* @brief load all data in the storage, depending of the number of bytes ( @p len ) required to read
* from the storage block in the mtd device.
*
* @param [out] value pointer to an struct or variable where will be loaded the data of the
* storage.
* @param [in] len size of the struct or variable @p value
* @retval 0 Satisfactory result
* @retval -1 Failed result
* @brief Saves data using a key as identifier to localize a group of bytes.
*
* @param [in] value Any type of value.
* @param [in] key Identifier to set/get a specified value.
* @param [in] len size of @p value that will be saved.
* @return int8_t
*/
int mtd_load(void *value, uint16_t len);
int mtd_save_reg(void *value, uint8_t *key, uint16_t len);

/**
* @brief Removes all saved data in mtd_storage. This will erase all until the
* last page since to available flash page to write eeprom.
* @brief Load data from mtd_storage localizating with its knew key and saves in @p value the data.
*
* @warning The mtd storage needs to define a flashpage limit after base program
* memory allocation
* @param [out] value Any type of value.
* @param [in] key Identifier to set/get a specified value.
* @param [in] len size of data that will be saved in @p value .
*
* @retval 0 Erased data success
* @retval -1 Erased Fail.
* @note @p size is used to check with the data saved in mtd_storage and won't be overflowed the @p
* value parameter.
* @return int8_t
*/
int mtd_erase_all(void);
int mtd_load_reg(void *value, uint8_t *key, uint16_t len);

/**
* @brief Dump all data inside mtd storage
* @brief Removes all saved data in mtd_storage. This will erase all until the
* last page since to available flash page to write eeprom.
*
* @warning The mtd storage needs to define a flashpage limit after base program
* memory allocation
*
* @retval 0 Exists data inside of mtd storage.
* @retval -1 All data inside of mtd is erased.
*/
int mtd_dump(void);

/**
* @brief Function used to get the strings already saved
*
* @param [in] key This is the address where will be saved the data in the memory
* @param [in] len Length of the data
* @param [out] output Output to save
* @return 0 Satisfactory result
* -1 Failed result
*/
int mtd_read_string(uint32_t key, char *output, size_t len);

/**
* @brief Function used to get the uint8 values already saved
*
* @param [in] key This is the address where will be saved the data in the memory
* @param [out] output Output to save
* @return 0 Satisfactory result
* -1 Failed result
* @retval 0 Erased data success
* @retval -1 Erased Fail.
*/
int mtd_read_u8(uint32_t key, uint8_t *output);
int8_t mtd_erase_all(void);

/**
* @brief This function deletes the saved data
* @brief Check the available registers, to write/save data.
*
* @param key This is the address where will be removed the data in the memory
* @return 0 Satisfactory result
* -1 Failed result
* @return int8_t
*/
int mtd_erase_flashpage(uint32_t key);
int8_t mtd_available_idx(void);

/**
* @brief This function is used to write only strings.
* If they are greater than 64 bytes your string will be
* cut and will save only 64 bytes
* @brief Dump all data inside mtd storage
*
* @param [in] key This is the address where will be saved the data in the memory
* @param [out] value Value to save
* @return 0 Satisfactory result
* -1 Failed result
*/
int mtd_write_string(uint32_t key, char *value);

/**
* @brief Function used to write only uint8_t
* @warning The mtd storage needs to define a flashpage limit after base program
* memory allocation
*
* @param [in] key This is the address where will be saved the data in the memory
* @param [out] value Value to save
* @return 0 Satisfactory result
* -1 Failed result
* @retval 0 Exists data inside of mtd storage.
* @retval -1 All data inside of mtd is erased.
*/
int mtd_write_uint8(uint32_t key, uint8_t *value);
int8_t mtd_dump(void);

/**
* @brief Function used to get the length of strings
*
* @param [in] key This is the address of memory
* @brief Dump all data inside an flshpage from mtd_storage
*
* @param page
* @return int8_t
*/
int mtd_get_string_len(uint32_t key);
int8_t mtd_dump_flashpage(uint16_t page);

#ifdef __cplusplus
}
Expand Down
Loading