This repository has been archived on 2025-02-28. You can view files and clone it, but cannot push or open issues or pull requests.
controller-hd/User/system/bsp/eeprom.c

92 lines
2.8 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* @file eeprom.c
* @author xxx
* @date 2023-11-16 10:28:47
* @brief STM32L072xx EEPROM 驱动
* @copyright Copyright (c) 2023 by xxx, All Rights Reserved.
*/
#include "eeprom.h"
#include "main.h"
#ifdef STM32L072xx
#define PEKEY1 0x89ABCDEF // FLASH_PEKEYR
#define PEKEY2 0x02030405 // FLASH_PEKEYR
#define LOCK __enable_irq(); // 系统开全局中断
#define UNLOCK __disable_irq(); // 系统关全局中断
/**
* @brief 用于配置读取和写入EEPROM的函数
* @param {uint32_t} read_addr - 读取地址
* @param {uint8_t} *data - 存储数据的指针
* @param {uint16_t} length - 读取或写入的数据长度
* @return {*}
* @note: 这些函数用于在特定的地址读取或写入数据到EEPROM中。地址和数据以字节为单位长度以字节为单位。
*/
void chip_eeprom_config_read(uint32_t read_addr, uint8_t *data, uint16_t length)
{
uint8_t *wAddr;
wAddr = (uint8_t *)(read_addr);
while (length--)
{
*data++ = *wAddr++;
}
}
/**
* @brief 用于配置写入EEPROM的函数
* @param {uint32_t} write_addr - 写入地址
* @param {uint8_t} *data - 存储数据的指针
* @param {uint16_t} length - 写入的数据长度
* @return {*}
* @note: 这些函数用于在特定的地址写入数据到EEPROM中。地址和数据以字节为单位长度以字节为单位。在写入数据之前需要先解锁EEPROM写入数据然后锁定EEPROM。
*/
void chip_eeprom_config_write(uint32_t write_addr, uint8_t *data, uint16_t length)
{
uint8_t *addr;
addr = (uint8_t *)(write_addr);
UNLOCK
FLASH->PDKEYR = PEKEY1;
FLASH->PDKEYR = PEKEY2;
while (FLASH->PECR & FLASH_PECR_PELOCK)
;
while (length--)
{
*addr++ = *data++;
while (FLASH->SR & FLASH_SR_BSY)
;
}
FLASH->PECR |= FLASH_PECR_PELOCK;
LOCK
}
#endif // STM32L072xx
#ifdef STM32L476xx
/**
* @brief 用于配置读取和写入SRAM2的函数
* @param {uint32_t} read_addr - 读取地址
* @param {uint8_t} *data - 存储数据的指针
* @param {uint16_t} length - 读取或写入的数据长度
* @return {*}
* @note: 这些函数用于在特定的地址读取或写入数据到SRAM2中。地址和数据以字节为单位长度以字节为单位。
*/
void chip_eeprom_config_read(uint32_t read_addr, uint8_t *data, uint16_t length)
{
}
/**
* @brief 用于配置写入SRAM2的函数
* @param {uint32_t} write_addr - 写入地址 从0开始
* @param {uint8_t} *data - 存储数据的指针
* @param {uint16_t} length - 写入的数据长度
* @return {*}
* @note: 这些函数用于在特定的地址写入数据到SRAM2中。地址和数据以字节为单位长度以字节为单位。
*/
void chip_eeprom_config_write(uint32_t write_addr, uint8_t *data, uint16_t length)
{
}
#endif // STM32L476xx