Basic code for writing to EEPROM.

This commit is contained in:
David Žaitlík
2021-09-09 19:19:20 +02:00
parent 7b0ad1fd65
commit 17286806fb
7 changed files with 418 additions and 76317 deletions

View File

@@ -11,6 +11,7 @@
/* TODO: add comments to everything */
#include "stdint.h"
#include "stm32l0xx.h"
#define VENDOR_NAME_LENGTH 64
#define PRODUCT_CODE_LENGTH 64
@@ -18,9 +19,22 @@
#define REVISION_LENGTH 16
#define SERIAL_NUMBER_LENGTH 64
#define DATA_EEPROM_BASE_ADDR ((uint32_t)0x08080000) /* Data EEPROM base address */
#define DATA_EEPROM_END_ADDR ((uint32_t)0x080801FF) /* Data EEPROM end address */
#define FLASH_PEKEY1 0x89ABCDEF
#define FLASH_PEKEY2 0x02030405
#define CONFIG_OK 0
#define CONFIG_ERROR -1
#define EEPROM_OK 0
#define EEPROM_ERROR -1
#define EEPROM_UNLOCK_ERROR -2
#define EEPROM_LOCK_ERROR -3
#define EEPROM_WRITE_ERROR -4
typedef struct
{
@@ -37,9 +51,15 @@ typedef struct
uint16_t led_co2_alert_limit2;
} config_t;
int8_t read_config(config_t *config);
int8_t config_read(config_t *config);
int8_t write_config(config_t *config);
int8_t config_write(config_t *config);
static int8_t eeprom_lock(void);
static int8_t eeprom_unlock(void);
static int8_t eeprom_program_byte(uint32_t addr, uint8_t ee_data);
static int8_t eeprom_program_halfword(uint32_t addr, uint16_t ee_data);
static int8_t eeprom_program_word(uint32_t addr, uint32_t ee_data);
#endif /* INC_CONFIG_H_ */

View File

@@ -53,6 +53,7 @@ extern "C" {
#include "sht4x.h"
#include "sps30.h"
#include "modbus.h"
#include "config.h"
/* USER CODE END Includes */
/* Exported types ------------------------------------------------------------*/

View File

@@ -7,12 +7,118 @@
#include "config.h"
int8_t read_config(config_t *config)
int8_t config_read(config_t *config)
{
return CONFIG_OK;
}
int8_t write_config(config_t *config)
int8_t config_write(config_t *config)
{
/* Unlock the EEPROM */
if (eeprom_unlock() != EEPROM_OK)
{
return EEPROM_UNLOCK_ERROR;
}
/* Reset the ERASE and DATA bits in the FLASH_PECR register to disable any residual erase */
FLASH->PECR = FLASH->PECR & ~(FLASH_PECR_ERASE | FLASH_PECR_DATA);
if (eeprom_program_byte(DATA_EEPROM_BASE_ADDR, 0xab) != EEPROM_OK)
{
return EEPROM_WRITE_ERROR;
}
if (eeprom_program_halfword(DATA_EEPROM_BASE_ADDR+2, 0x4321) != EEPROM_OK)
{
return EEPROM_WRITE_ERROR;
}
if (eeprom_program_word(DATA_EEPROM_BASE_ADDR + (4), 0x12345678) != EEPROM_OK)
{
return EEPROM_WRITE_ERROR;
}
for (uint8_t i = 0; i < 16; i ++)
{
eeprom_program_byte(DATA_EEPROM_BASE_ADDR + 8 + i, i+1);
}
if (eeprom_lock() != EEPROM_OK)
{
return EEPROM_LOCK_ERROR;
}
return CONFIG_OK;
}
static int8_t eeprom_lock(void)
{
while ((FLASH->SR & FLASH_SR_BSY) != 0) /* Wait for FLASH to be free */
{
/* TODO: insert timeout test */
}
FLASH->PECR = FLASH->PECR & ~(FLASH_PECR_ERRIE | FLASH_PECR_EOPIE); /* disable flash interrupts */
FLASH->PECR = FLASH->PECR | FLASH_PECR_PELOCK; /* Lock memory with PELOCK */
return EEPROM_OK;
}
/* Unlock the EEPROM: */
static int8_t eeprom_unlock(void)
{
while ((FLASH->SR & FLASH_SR_BSY) != 0) /* Wait for FLASH to be free */
{
/* TODO: insert timeout test */
}
if ((FLASH->PECR & FLASH_PECR_PELOCK) != 0) /* If PELOCK is locked */
{
/* Unlock PELOCK */
FLASH->PEKEYR = FLASH_PEKEY1; /* PEKEY1 */
FLASH->PEKEYR = FLASH_PEKEY2; /* PEKEY2 */
}
FLASH->PECR = FLASH->PECR | (FLASH_PECR_ERRIE | FLASH_PECR_EOPIE); /* enable flash interrupts */
return EEPROM_OK;
}
/**
* Brief This function programs a word of data EEPROM.
* The ERASE bit and DATA bit are cleared in PECR at the beginning
* words are automatically erased if required before programming
* Param addr is the 32-bit EEPROM address to program, data is the 32 bit word to program
* Retval None
*/
/* NOTE: The EEPROM must be unlocked and the flash interrupts must have been enabled prior to calling this function.*/
static int8_t eeprom_program_byte(uint32_t addr, uint8_t ee_data)
{
*(uint8_t *)(addr) = ee_data; /* write data to EEPROM */
//__WFI();
if (*(uint8_t *)(addr) != ee_data)
{
return EEPROM_WRITE_ERROR;
}
return EEPROM_OK;
}
static int8_t eeprom_program_halfword(uint32_t addr, uint16_t ee_data)
{
*(uint16_t *)(addr) = ee_data; /* write data to EEPROM */
//__WFI();
if (*(uint16_t *)(addr) != ee_data)
{
return EEPROM_WRITE_ERROR;
}
return EEPROM_OK;
}
static int8_t eeprom_program_word(uint32_t addr, uint32_t ee_data)
{
*(uint32_t *)(addr) = ee_data; /* write data to EEPROM */
//__WFI();
if (*(uint32_t *)(addr) != ee_data)
{
return EEPROM_WRITE_ERROR;
}
return EEPROM_OK;
}

View File

@@ -196,6 +196,14 @@ int main(void)
scd4x_start_periodic_measurement();
uint8_t scd4x_is_connected = 1;
uint8_t sps30_is_connected = 0;
config_t dummy_config;
int8_t config_write_err = config_write(&dummy_config);
uint32_t eeprom_dato1 = *(uint8_t *)(DATA_EEPROM_BASE_ADDR);
uint32_t eeprom_dato2 = *(uint16_t *)(DATA_EEPROM_BASE_ADDR+2);
uint32_t eeprom_dato3 = *(uint32_t *)(DATA_EEPROM_BASE_ADDR+4);
/* USER CODE END 2 */
/* Infinite loop */