sysparam fixes, tests, spi flash refactoring (#299)

Original work by @ourairquality
* Sysparam threadsafe and SPI access
* Sysparam test cases
* Fix for negative int8
* Sysparam getting bool without memory allocation. Bool tests.
* SPI flash refactoring.
* Extract common spiflash.c into core.
* Use spiflash.c in sysparam.
* Use memcpy in spiflash.c insted of hand-written version.
* Tests for spiflash.c
This commit is contained in:
sheinz 2017-03-21 23:18:04 +02:00 committed by Ruslan V. Uss
parent 07ca0d2e9e
commit a91ec6eb61
10 changed files with 724 additions and 406 deletions

View file

@ -36,4 +36,6 @@ typedef struct {
uint32_t status_mask;
} sdk_flashchip_t;
extern sdk_flashchip_t sdk_flashchip;
#endif /* _FLASHCHIP_H */

64
core/include/spiflash.h Normal file
View file

@ -0,0 +1,64 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2016 sheinz (https://github.com/sheinz)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef __SPIFLASH_H__
#define __SPIFLASH_H__
#include <stdint.h>
#include <stdbool.h>
#include "common_macros.h"
#define SPI_FLASH_SECTOR_SIZE 4096
/**
* Read data from SPI flash.
*
* @param addr Address to read from. Can be not aligned.
* @param buf Buffer to read to. Doesn't have to be aligned.
* @param size Size of data to read. Buffer size must be >= than data size.
*
* @return true if success, otherwise false
*/
bool IRAM spiflash_read(uint32_t addr, uint8_t *buf, uint32_t size);
/**
* Write data to SPI flash.
*
* @param addr Address to write to. Can be not aligned.
* @param buf Buffer of data to write to flash. Doesn't have to be aligned.
* @param size Size of data to write. Buffer size must be >= than data size.
*
* @return true if success, otherwise false
*/
bool IRAM spiflash_write(uint32_t addr, uint8_t *buf, uint32_t size);
/**
* Erase a sector.
*
* @param addr Address of sector to erase. Must be sector aligned.
*
* @return true if success, otherwise false
*/
bool IRAM spiflash_erase_sector(uint32_t addr);
#endif // __SPIFLASH_H__

View file

@ -119,7 +119,7 @@ sysparam_status_t sysparam_init(uint32_t base_addr, uint32_t top_addr);
* you reformat the area currently being used, you will also need to call
* sysparam_init() again afterward before you will be able to continue using
* it.
*/
*/
sysparam_status_t sysparam_create_area(uint32_t base_addr, uint16_t num_sectors, bool force);
/** Get the start address and size of the currently active sysparam area
@ -180,24 +180,20 @@ sysparam_status_t sysparam_compact();
*/
sysparam_status_t sysparam_get_data(const char *key, uint8_t **destptr, size_t *actual_length, bool *is_binary);
/** Get the value associated with a key (static buffers only)
/** Get the value associated with a key (static value buffer)
*
* This performs the same function as sysparam_get_data() but without
* performing any memory allocations. It can thus be used before the heap has
* been configured or in other cases where using the heap would be a problem
* (i.e. in an OOM handler, etc). It requires that the caller pass in a
* suitably sized buffer for the value to be read (if the supplied buffer is
* not large enough, the returned value will be truncated and the full
* required length will be returned in `actual_length`).
*
* NOTE: In addition to being large enough for the value, the supplied buffer
* must also be at least as large as the length of the key being requested.
* If it is not, an error will be returned.
* allocating memory for the result value. It can thus be used before the heap
* has been configured or in other cases where using the heap would be a
* problem (i.e. in an OOM handler, etc). It requires that the caller pass in
* a suitably sized buffer for the value to be read (if the supplied buffer is
* not large enough, the returned value will be truncated and the full required
* length will be returned in `actual_length`).
*
* @param[in] key Key name (zero-terminated string)
* @param[in] buffer Pointer to a buffer to hold the returned value
* @param[in] buffer_size Length of the supplied buffer in bytes
* @param[out] actual_length pointer to a location to hold the actual length
* @param[in] dest Pointer to a buffer to hold the returned value.
* @param[in] dest_size Length of the supplied buffer in bytes.
* @param[out] actual_length Pointer to a location to hold the actual length
* of the data which was associated with the key
* (may be NULL).
* @param[out] is_binary Pointer to a bool to hold whether the returned
@ -210,10 +206,10 @@ sysparam_status_t sysparam_get_data(const char *key, uint8_t **destptr, size_t *
* @retval ::SYSPARAM_ERR_CORRUPT Sysparam region has bad/corrupted data
* @retval ::SYSPARAM_ERR_IO I/O error reading/writing flash
*/
sysparam_status_t sysparam_get_data_static(const char *key, uint8_t *buffer, size_t buffer_size, size_t *actual_length, bool *is_binary);
sysparam_status_t sysparam_get_data_static(const char *key, uint8_t *dest, size_t dest_size, size_t *actual_length, bool *is_binary);
/** Get the string value associated with a key
*
*
* This routine can be used if you know that the value in a key will (or at
* least should) be a string. It will return a zero-terminated char buffer
* containing the value retrieved.
@ -240,9 +236,10 @@ sysparam_status_t sysparam_get_data_static(const char *key, uint8_t *buffer, siz
sysparam_status_t sysparam_get_string(const char *key, char **destptr);
/** Get the int32_t value associated with a key
*
*
* This routine can be used if you know that the value in a key will (or at
* least should) be an int32_t value.
* least should) be an int32_t value. This is done without allocating any
* memory.
*
* Note: If the status result is anything other than ::SYSPARAM_OK, the value
* in `result` is not changed. This means it is possible to set a default
@ -266,7 +263,8 @@ sysparam_status_t sysparam_get_int32(const char *key, int32_t *result);
/** Get the int8_t value associated with a key
*
* This routine can be used if you know that the value in a key will (or at
* least should) be a uint8_t binary value.
* least should) be a uint8_t binary value. This is done without allocating any
* memory.
*
* Note: If the status result is anything other than ::SYSPARAM_OK, the value
* in `result` is not changed. This means it is possible to set a default
@ -288,7 +286,7 @@ sysparam_status_t sysparam_get_int32(const char *key, int32_t *result);
sysparam_status_t sysparam_get_int8(const char *key, int8_t *result);
/** Get the boolean value associated with a key
*
*
* This routine can be used if you know that the value in a key will (or at
* least should) be a boolean setting. It will read the specified value as a
* text string and attempt to parse it as a boolean value.
@ -320,7 +318,7 @@ sysparam_status_t sysparam_get_bool(const char *key, bool *result);
*
* The supplied value can be any data, up to 255 bytes in length. If `value`
* is NULL or `value_len` is 0, this is treated as a request to delete any
* current entry matching `key`.
* current entry matching `key`. This is done without allocating any memory.
*
* If `binary` is true, the data will be considered binary (unprintable) data,
* and this will be annotated in the saved entry. This does not affect the
@ -368,7 +366,8 @@ sysparam_status_t sysparam_set_string(const char *key, const char *value);
/** Set a key's value as a number
*
* Write an int32_t binary value to the specified key. This does the inverse of
* the sysparam_get_int32() function.
* the sysparam_get_int32() function. This is done without allocating any
* memory.
*
* @param[in] key Key name (zero-terminated string)
* @param[in] value Value to set
@ -386,10 +385,8 @@ sysparam_status_t sysparam_set_int32(const char *key, int32_t value);
/** Set a key's value as a number
*
* Write an int8_t binary value to the specified key. This does the inverse of
* the sysparam_get_int8() function.
*
* Note that if the key already contains a value which parses to the same
* boolean (true/false) value, it is left unchanged.
* the sysparam_get_int8() function. This is done without allocating any
* memory.
*
* @param[in] key Key name (zero-terminated string)
* @param[in] value Value to set