added reading flash functions for stm32l0xx series

1. implemented HAL_StatusTypeDef HAL_FLASH_ReadWord(uint32_t Address, uint32_t* Data); and HAL_StatusTypeDef HAL_FLASH_ReadHalfWord(uint32_t Address, uint16_t* Data); which allows user to read the internal FLASH (with out read protection)

2. implemeted device_config struct to store device configurations, e.g. magnetometer fullscale, report period, etc.

3. added demonstration code of using flash reading apis in lora_demo.c, in which the configuration sent from the cloud is stored into the internal Data EEPROM bank1, and each time when the device is booted, the configurations would be loaded from the Data EEPROM and used to initialize the application.
This commit is contained in:
Winfred LIN
2020-04-03 18:21:16 +11:00
parent cafe9c8a87
commit 47d6313ba1
8 changed files with 313 additions and 64 deletions

View File

@@ -0,0 +1,43 @@
/**
******************************************************************************
* @file stm32l0xx_hal_flash_ex2.h
* @author jieranzhi
* @date 2020/04/02 14:59 CST
* @brief Header file of Flash EEPROM.
******************************************************************************
* @attention
*
* this file implementes FLASH read operations for stm32l0xx series(category 5
* devices), to implement a morefunctions, please refer to RM0367, which could
* be downloaded from:
* https://www.st.com/resource/en/reference_manual/dm00095744-ultra-low-power-
* stm32l0x3-advanced-arm-based-32-bit-mcus-stmicroelectronics.pdf
******************************************************************************
*/
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __STM32L0xx_HAL_FLASH_EX2_H
#define __STM32L0xx_HAL_FLASH_EX2_H
#ifdef __cplusplus
extern "C" {
#endif
/* Includes ------------------------------------------------------------------*/
#include "stm32l0xx_hal.h"
/* Include FLASH HAL Extended module */
#include "stm32l0xx_hal_flash_ex.h"
#include "stm32l0xx_hal_flash_ramfunc.h"
/* Exported functions --------------------------------------------------------*/
void HAL_FLASH_Prepare_Reading(uint8_t WaitState);
HAL_StatusTypeDef HAL_FLASH_ReadWord(uint32_t Address, uint32_t* Data);
HAL_StatusTypeDef HAL_FLASH_ReadHalfWord(uint32_t Address, uint16_t* Data);
#ifdef __cplusplus
}
#endif
#endif /* __STM32L0xx_HAL_FLASH_EX2_H */

View File

@@ -0,0 +1,89 @@
/**
******************************************************************************
* @file stm32l0xx_hal_flash_eeprom.c
* @author jieranzhi
* @date 2020/04/02 14:59 CST
* @brief implementation of Flash EEPROM operations.
******************************************************************************
* @attention
*
* this file implementes FLASH read operations for stm32l0xx series(category 5
* devices), to implement a morefunctions, please refer to RM0367, which could
* be downloaded from:
* https://www.st.com/resource/en/reference_manual/dm00095744-ultra-low-power-
* stm32l0x3-advanced-arm-based-32-bit-mcus-stmicroelectronics.pdf
******************************************************************************
*/
#include "stm32l0xx_hal_flash_ex2.h"
/**
* @brief set the correct number of wait states, and set the clock of the memory
interface
* @note The user must set the correct number of wait states (LATENCY bit in
* the FLASH_ACR register). No control is done to verify if the frequency
* or the power used is correct, with respect to the number of wait
* states. A wrong number of wait states can generate wrong read values
* (high frequency and 0 wait states) or a long time to execute a code
* (low frequency with 1 wait state).
* @param correct number of wait states
*/
void HAL_FLASH_Prepare_Reading(uint8_t WaitState)
{
__HAL_RCC_MIF_CLK_ENABLE();
__HAL_FLASH_SET_LATENCY(WaitState);
}
/**
* @brief Read word (4 bytes) from a specified address
* @note To correctly run this function, the HAL_FLASH_Prepare_Reading(...)
* function must be called before.
*
* @param Address Specifie the address to be read from.
* @param Data Specifie the data to store the
*
* @retval HAL_StatusTypeDef HAL Status
*/
HAL_StatusTypeDef HAL_FLASH_ReadWord(uint32_t Address, uint32_t* Data)
{
HAL_StatusTypeDef status = HAL_ERROR;
/* Check the parameters */
assert_param(IS_FLASH_PROGRAM_ADDRESS(Address)||IS_FLASH_DATA_ADDRESS(Address));
/* Wait for last operation to be completed */
status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
if(status == HAL_OK)
{
*Data = *(__IO uint32_t*)Address;
}
return status;
}
/**
* @brief Read half-word (2 bytes) from a specified address
* @note To correctly run this function, the HAL_FLASH_Prepare_Reading(...)
* function must be called before.
*
* @param Address Specifie the address to be read from.
* @param Data Specifie the data to store the
*
* @retval HAL_StatusTypeDef HAL Status
*/
HAL_StatusTypeDef HAL_FLASH_ReadHalfWord(uint32_t Address, uint16_t* Data)
{
HAL_StatusTypeDef status = HAL_ERROR;
/* Check the parameters */
assert_param(IS_FLASH_PROGRAM_ADDRESS(Address)||IS_FLASH_DATA_ADDRESS(Address));
/* Wait for last operation to be completed */
status = FLASH_WaitForLastOperation(FLASH_TIMEOUT_VALUE);
if(status == HAL_OK)
{
*Data = *(__IO uint16_t*)Address;
}
return status;
}