123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- #include <stdio.h>
- #include <string.h>
- #include "eeprom_stm32.h"
- uint8_t DataBuf[FEE_PAGE_SIZE];
- uint16_t EEPROM_Init(void) {
-
- FLASH_Unlock();
-
-
- return FEE_DENSITY_BYTES;
- }
- void EEPROM_Erase(void) {
- int page_num = 0;
-
- do {
- FLASH_ErasePage(FEE_PAGE_BASE_ADDRESS + (page_num * FEE_PAGE_SIZE));
- page_num++;
- } while (page_num < FEE_DENSITY_PAGES);
- }
- uint16_t EEPROM_WriteDataByte(uint16_t Address, uint8_t DataByte) {
- FLASH_Status FlashStatus = FLASH_COMPLETE;
- uint32_t page;
- int i;
-
- if (Address > FEE_DENSITY_BYTES) {
- return 0;
- }
-
- page = FEE_ADDR_OFFSET(Address) / FEE_PAGE_SIZE;
-
- if ((*(__IO uint16_t *)(FEE_PAGE_BASE_ADDRESS + FEE_ADDR_OFFSET(Address))) == FEE_EMPTY_WORD) {
- FlashStatus = FLASH_ProgramHalfWord(FEE_PAGE_BASE_ADDRESS + FEE_ADDR_OFFSET(Address), (uint16_t)(0x00FF & DataByte));
- } else {
-
- memcpy(DataBuf, (uint8_t *)FEE_PAGE_BASE_ADDRESS + (page * FEE_PAGE_SIZE), FEE_PAGE_SIZE);
-
- if (DataByte == *(__IO uint8_t *)(FEE_PAGE_BASE_ADDRESS + FEE_ADDR_OFFSET(Address))) {
- return 0;
- }
-
- DataBuf[FEE_ADDR_OFFSET(Address) % FEE_PAGE_SIZE] = DataByte;
-
- FlashStatus = FLASH_ErasePage(FEE_PAGE_BASE_ADDRESS + (page * FEE_PAGE_SIZE));
-
- for (i = 0; i < (FEE_PAGE_SIZE / 2); i++) {
- if ((__IO uint16_t)(0xFF00 | DataBuf[FEE_ADDR_OFFSET(i)]) != 0xFFFF) {
- FlashStatus = FLASH_ProgramHalfWord((FEE_PAGE_BASE_ADDRESS + (page * FEE_PAGE_SIZE)) + (i * 2), (uint16_t)(0xFF00 | DataBuf[FEE_ADDR_OFFSET(i)]));
- }
- }
- }
- return FlashStatus;
- }
- uint8_t EEPROM_ReadDataByte(uint16_t Address) {
- uint8_t DataByte = 0xFF;
-
- DataByte = (*(__IO uint8_t *)(FEE_PAGE_BASE_ADDRESS + FEE_ADDR_OFFSET(Address)));
- return DataByte;
- }
- uint8_t eeprom_read_byte(const uint8_t *Address) {
- const uint16_t p = (const uint32_t)Address;
- return EEPROM_ReadDataByte(p);
- }
- void eeprom_write_byte(uint8_t *Address, uint8_t Value) {
- uint16_t p = (uint32_t)Address;
- EEPROM_WriteDataByte(p, Value);
- }
- void eeprom_update_byte(uint8_t *Address, uint8_t Value) {
- uint16_t p = (uint32_t)Address;
- EEPROM_WriteDataByte(p, Value);
- }
- uint16_t eeprom_read_word(const uint16_t *Address) {
- const uint16_t p = (const uint32_t)Address;
- return EEPROM_ReadDataByte(p) | (EEPROM_ReadDataByte(p + 1) << 8);
- }
- void eeprom_write_word(uint16_t *Address, uint16_t Value) {
- uint16_t p = (uint32_t)Address;
- EEPROM_WriteDataByte(p, (uint8_t)Value);
- EEPROM_WriteDataByte(p + 1, (uint8_t)(Value >> 8));
- }
- void eeprom_update_word(uint16_t *Address, uint16_t Value) {
- uint16_t p = (uint32_t)Address;
- EEPROM_WriteDataByte(p, (uint8_t)Value);
- EEPROM_WriteDataByte(p + 1, (uint8_t)(Value >> 8));
- }
- uint32_t eeprom_read_dword(const uint32_t *Address) {
- const uint16_t p = (const uint32_t)Address;
- return EEPROM_ReadDataByte(p) | (EEPROM_ReadDataByte(p + 1) << 8) | (EEPROM_ReadDataByte(p + 2) << 16) | (EEPROM_ReadDataByte(p + 3) << 24);
- }
- void eeprom_write_dword(uint32_t *Address, uint32_t Value) {
- uint16_t p = (const uint32_t)Address;
- EEPROM_WriteDataByte(p, (uint8_t)Value);
- EEPROM_WriteDataByte(p + 1, (uint8_t)(Value >> 8));
- EEPROM_WriteDataByte(p + 2, (uint8_t)(Value >> 16));
- EEPROM_WriteDataByte(p + 3, (uint8_t)(Value >> 24));
- }
- void eeprom_update_dword(uint32_t *Address, uint32_t Value) {
- uint16_t p = (const uint32_t)Address;
- uint32_t existingValue = EEPROM_ReadDataByte(p) | (EEPROM_ReadDataByte(p + 1) << 8) | (EEPROM_ReadDataByte(p + 2) << 16) | (EEPROM_ReadDataByte(p + 3) << 24);
- if (Value != existingValue) {
- EEPROM_WriteDataByte(p, (uint8_t)Value);
- EEPROM_WriteDataByte(p + 1, (uint8_t)(Value >> 8));
- EEPROM_WriteDataByte(p + 2, (uint8_t)(Value >> 16));
- EEPROM_WriteDataByte(p + 3, (uint8_t)(Value >> 24));
- }
- }
- void eeprom_read_block(void *buf, const void *addr, uint32_t len) {
- const uint8_t *p = (const uint8_t *)addr;
- uint8_t * dest = (uint8_t *)buf;
- while (len--) {
- *dest++ = eeprom_read_byte(p++);
- }
- }
- void eeprom_write_block(const void *buf, void *addr, uint32_t len) {
- uint8_t * p = (uint8_t *)addr;
- const uint8_t *src = (const uint8_t *)buf;
- while (len--) {
- eeprom_write_byte(p++, *src++);
- }
- }
- void eeprom_update_block(const void *buf, void *addr, uint32_t len) {
- uint8_t * p = (uint8_t *)addr;
- const uint8_t *src = (const uint8_t *)buf;
- while (len--) {
- eeprom_write_byte(p++, *src++);
- }
- }
|