77范文网 - 专业文章范例文档资料分享平台

stm32_flash_eeprom_断电保护_数值存储程序(3)

来源:网络收集 时间:2018-11-21 下载这篇文档 手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:或QQ: 处理(尽可能给您提供完整文档),感谢您的支持与谅解。点击这里给我发消息

/* Check each active page address starting from end */ while (Address > (PageStartAddress + 2)) {

/* Get the current location content to be compared with virtual address */ AddressValue = (*(__IO uint16_t*)Address);

/* Compare the read address with the virtual address */ if (AddressValue == VirtAddress) {

/* Get content of Address-2 which is variable value */ *Data = (*(__IO uint16_t*)(Address - 2));

/* In case variable value is read, reset ReadStatus flag */ ReadStatus = 0;

break; } else {

/* Next address location */ Address = Address - 4; } }

/* Return ReadStatus value: (0: variable exist, 1: variable doesn't exist) */ return ReadStatus; } /**

* @brief Writes/upadtes variable data in EEPROM. * @param VirtAddress: Variable virtual address * @param Data: 16 bit data to be written * @retval Success or error status:

* - FLASH_COMPLETE: on success * - PAGE_FULL: if valid page is full

* - NO_VALID_PAGE: if no valid page was found * - Flash error code: on write Flash error */

uint16_t EE_WriteVariable(uint16_t VirtAddress, uint16_t Data) {

uint16_t Status = 0;

/* Write the variable virtual address and value in the EEPROM */ Status = EE_VerifyPageFullWriteVariable(VirtAddress, Data);

/* In case the EEPROM active page is full */ if (Status == PAGE_FULL) {

/* Perform Page transfer */

Status = EE_PageTransfer(VirtAddress, Data); }

/* Return last operation status */ return Status; } /**

* @brief Erases PAGE0 and PAGE1 and writes VALID_PAGE header to PAGE0 * @param None

* @retval Status of the last operation (Flash write or erase) done during * EEPROM formating */

static FLASH_Status EE_Format(void) {

FLASH_Status FlashStatus = FLASH_COMPLETE;

/* Erase Page0 */

FlashStatus = FLASH_ErasePage(PAGE0_BASE_ADDRESS);

/* If erase operation was failed, a Flash error code is returned */ if (FlashStatus != FLASH_COMPLETE) {

return FlashStatus; }

/* Set Page0 as valid page: Write VALID_PAGE at Page0 base address */

FlashStatus = FLASH_ProgramHalfWord(PAGE0_BASE_ADDRESS, VALID_PAGE);

/* If program operation was failed, a Flash error code is returned */ if (FlashStatus != FLASH_COMPLETE) {

return FlashStatus; }

/* Erase Page1 */

FlashStatus = FLASH_ErasePage(PAGE1_BASE_ADDRESS);

/* Return Page1 erase operation status */

return FlashStatus; } /**

* @brief Find valid Page for write or read operation

* @param Operation: operation to achieve on the valid page. * This parameter can be one of the following values:

* @arg READ_FROM_VALID_PAGE: read operation from valid page * @arg WRITE_IN_VALID_PAGE: write operation from valid page

* @retval Valid page number (PAGE0 or PAGE1) or NO_VALID_PAGE in case * of no valid page was found */

static uint16_t EE_FindValidPage(uint8_t Operation) {

uint16_t PageStatus0 = 6, PageStatus1 = 6;

/* Get Page0 actual status */

PageStatus0 = (*(__IO uint16_t*)PAGE0_BASE_ADDRESS);

/* Get Page1 actual status */

PageStatus1 = (*(__IO uint16_t*)PAGE1_BASE_ADDRESS);

/* Write or read operation */ switch (Operation) {

case WRITE_IN_VALID_PAGE: /* ---- Write operation ---- */ if (PageStatus1 == VALID_PAGE) {

/* Page0 receiving data */

if (PageStatus0 == RECEIVE_DATA) {

return PAGE0; /* Page0 valid */ } else {

return PAGE1; /* Page1 valid */ } }

else if (PageStatus0 == VALID_PAGE) {

/* Page1 receiving data */

if (PageStatus1 == RECEIVE_DATA) {

return PAGE1; /* Page1 valid */

} else {

return PAGE0; /* Page0 valid */ } } else {

return NO_VALID_PAGE; /* No valid Page */ }

case READ_FROM_VALID_PAGE: /* ---- Read operation ---- */ if (PageStatus0 == VALID_PAGE) {

return PAGE0; /* Page0 valid */ }

else if (PageStatus1 == VALID_PAGE) {

return PAGE1; /* Page1 valid */ } else {

return NO_VALID_PAGE ; /* No valid Page */ }

default:

return PAGE0; /* Page0 valid */ } } /**

* @brief Verify if active page is full and Writes variable in EEPROM. * @param VirtAddress: 16 bit virtual address of the variable * @param Data: 16 bit data to be written as variable value * @retval Success or error status:

* - FLASH_COMPLETE: on success * - PAGE_FULL: if valid page is full

* - NO_VALID_PAGE: if no valid page was found * - Flash error code: on write Flash error */

static uint16_t EE_VerifyPageFullWriteVariable(uint16_t VirtAddress, uint16_t Data) {

FLASH_Status FlashStatus = FLASH_COMPLETE; uint16_t ValidPage = PAGE0;

//uint32_t Address = 0x08010000,

uint32_t PageEndAddress = 0x080107FF;

/* Get valid Page for write operation */

ValidPage = EE_FindValidPage(WRITE_IN_VALID_PAGE);

/* Check if there is no valid page */ if (ValidPage == NO_VALID_PAGE) {

return NO_VALID_PAGE; }

/* Get the valid Page start Address */

//Address = (uint32_t)(EEPROM_START_ADDRESS + (uint32_t)(ValidPage * PAGE_SIZE)); //Address = CurWrAddress;//当前写地址

/* Get the valid Page end Address */

PageEndAddress = (uint32_t)((EEPROM_START_ADDRESS - 2) + (uint32_t)((1 + ValidPage) * PAGE_SIZE));

/* Check each active page address starting from begining */ while (CurWrAddress < PageEndAddress) {

/* Verify if Address and Address+2 contents are 0xFFFFFFFF */ if ((*(__IO uint32_t*)CurWrAddress) == 0xFFFFFFFF) {

/* Set variable data */

FlashStatus = FLASH_ProgramHalfWord(CurWrAddress, Data);

/* If program operation was failed, a Flash error code is returned */ if (FlashStatus != FLASH_COMPLETE) {

return FlashStatus; }

/* Set variable virtual address */

FlashStatus = FLASH_ProgramHalfWord(CurWrAddress + 2, VirtAddress);

CurWrAddress = CurWrAddress + 4; /* Return program operation status */ return FlashStatus; } else

{//修改后的算法是不会执行到这里的 /* Next address location */

CurWrAddress = CurWrAddress + 4;

百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库stm32_flash_eeprom_断电保护_数值存储程序(3)在线全文阅读。

stm32_flash_eeprom_断电保护_数值存储程序(3).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印 下载失败或者文档不完整,请联系客服人员解决!
本文链接:https://www.77cn.com.cn/wenku/zonghe/294358.html(转载请注明文章来源)
Copyright © 2008-2022 免费范文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ: 邮箱:tiandhx2@hotmail.com
苏ICP备16052595号-18
× 注册会员免费下载(下载后可以自由复制和排版)
注册会员下载
全站内容免费自由复制
注册会员下载
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: