freertos_f407/User/system/lib/flashdb/fal_eeprom24_port.c

70 lines
1.7 KiB
C

/*
* Copyright (c) 2006-2018, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2018-01-26 armink the first version
*/
#include "eeprom_fm24.h"
#include <fal.h>
static int init(void);
static int erase(long offset, size_t size);
static int read(long offset, uint8_t *buf, size_t size);
static int write(long offset, const uint8_t *buf, size_t size);
// 1.定义 flash 设备
struct fal_flash_dev eeprom_fm24 =
{
.name = EEPROM_FM24_DEV_NAME,
.addr = 0,
.len = FM24_PAGE_SIZE,
.blk_size = EEPROM_FM24_BLOCK_SIZE,
.ops = {init, read, write, erase},
.write_gran = 1};
static int init(void)
{
return 1;
}
static int erase(long offset, size_t size)
{
// uint8_t erase_size = size > FAL_ERASE_SIZE ? FAL_ERASE_SIZE : size;
// uint8_t buf[FAL_ERASE_SIZE];
// osel_memset(buf, 0xFF, FAL_ERASE_SIZE);
// for (uint8_t i = 0; i < (size / FM24_PAGE_SIZE); i++)
// {
// uint32_t addr = eeprom_fm24.addr + offset + i * FM24_PAGE_SIZE;
// eeprom_fm24_write(addr, (uint8_t *)buf, erase_size);
// }
return size;
}
static int read(long offset, uint8_t *buf, size_t size)
{
/* You can add your code under here. */
if (size == 0)
{
return 0;
}
uint32_t addr = eeprom_fm24.addr + offset;
BOOL res = eeprom_fm24_read(addr, buf, size);
return res == TRUE ? size : 0;
}
static int write(long offset, const uint8_t *buf, size_t size)
{
if (size == 0)
{
return 0;
}
uint32_t addr = eeprom_fm24.addr + offset;
BOOL res = eeprom_fm24_write(addr, (uint8_t *)buf, size);
return res == TRUE ? (int)size : -1;
}