Skip to content

Commit

Permalink
firmware/sys/storage: Added supports to del registers an rwp bit set
Browse files Browse the repository at this point in the history
  • Loading branch information
eduazocar committed Oct 12, 2022
1 parent c8e0dd3 commit fa02419
Show file tree
Hide file tree
Showing 8 changed files with 554 additions and 296 deletions.
3 changes: 3 additions & 0 deletions firmware/sys/storage/Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
SRC = storage.c
SRC += storage_register.c
SRC += storage_internal.c


include $(RIOTBASE)/Makefile.base
64 changes: 0 additions & 64 deletions firmware/sys/storage/include/storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,30 +35,6 @@
extern "C" {
#endif

#define LAST_AVAILABLE_PAGE (FLASHPAGE_NUMOF - 1) /*!< Last position in the block EEPROM*/
#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.
*/
/**
* @brief This function initializes all components needed to start and to save the data
*
Expand Down Expand Up @@ -87,48 +63,8 @@ int mtd_save(const void *value, uint32_t len, uint32_t offset);
*/
int mtd_load(void *value, uint16_t len, uint32_t offset);

/**
* @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_save_reg(const void *value, const uint8_t *key, uint16_t len);

/**
* @brief Load data from mtd_storage localizating with its knew key and saves in @p value the data.
*
* @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 .
*
* @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_load_reg(void *value, const 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.
*
* @warning The mtd storage needs to define a flashpage limit after base program
* memory allocation
*
* @retval 0 Erased data success
* @retval -1 Erased Fail.
*/
int8_t mtd_erase_all(void);

/**
* @brief Check the available registers, to write/save data.
*
* @return int8_t
*/
int8_t mtd_available_idx(void);

/**
* @brief Dump all data inside mtd storage
*
Expand Down
73 changes: 73 additions & 0 deletions firmware/sys/storage/include/storage_internal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright (c) 2022 Mesh4all <mesh4all.org>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* @ingroup storage_module
* @{
* @file
* @brief This is the storage module where you can save data in the flash memory.
* This data should not be greater than 64 bytes. Data formats allowed are string
* and uint8.
*
* @author xkevin190 <kevinvelasco193@gmail.com>
* @author eduazocar <eduazocarv@gmail.com>
*
*/
#ifndef STORAGE_INTERNAL_H
#define STORAGE_INTERNAL_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 (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*/

/**
* @brief Saves any value in any position of mtd_storage.
*
* @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(const void *value, uint32_t len, uint32_t offset);

/**
* @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_load(void *value, uint16_t len, uint32_t offset);

#ifdef __cplusplus
}
#endif
#endif /* STORAGE_INTERNAL_H */
/** @} */
106 changes: 106 additions & 0 deletions firmware/sys/storage/include/storage_register.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* Copyright (c) 2022 Mesh4all <mesh4all.org>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* @ingroup storage_module
* @{
* @file
* @brief This is the storage module where you can save data in the flash memory by registers
* you can add an identifier o key to locate a specify data.
*
* @author eduazocar <eduazocarv@gmail.com>
*
*/
#ifndef STORAGE_REGISTER_H
#define STORAGE_REGISTER_H

#include "mtd_flashpage.h"

#ifdef __cplusplus
extern "C" {
#endif

#define MTD_REGISTER_INDEX_LIMIT (512) /*!< Reference to max number of bytes to add an register*/
#define RWP_WRITE_BITMASK (0x80) /*!< Read/Write protection bit mask to set Writeable register*/
#define RWP_READ_BITMASK (0x00) /*!< Read/Write protection bit mask to set only-read 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)) /*!< Max number of indexes that can be saved in the mtd storage */

/**
* @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_save_reg(const void *value, const uint8_t *key, uint16_t len);

/**
* @brief Load data from mtd_storage localizating with its knew key and saves in @p value the data.
*
* @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 .
*
* @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_load_reg(void *value, const 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.
*
* @warning The mtd storage needs to define a flashpage limit after base program
* memory allocation
*
* @retval 0 Erased data success
* @retval -1 Erased Fail.
*/
int8_t mtd_reg_del(uint8_t *key, uint16_t size);

/**
* @brief
*
* @param[inout] idx First available index to write a register.
* @param[inout] count Number of available registers to write.
* @return int8_t
*/
int8_t mtd_available_idx(uint8_t *idx, uint8_t *count);

/**
* @brief Removes all registers and set all their read/write protection bit, making that registers
* writeable
*
* @return int
*/
int mtd_clear_all_regs(void);

#ifdef __cplusplus
}
#endif
#endif /* STORAGE_REGISTER_H */
/** @} */
Loading

0 comments on commit fa02419

Please sign in to comment.