#ifndef _MCU_FLASH_H_
#define _MCU_FLASH_H_
#include // STM32f103C8T6 64KB FLASH,20KB RAM,1个扇区1KB,程序起始0x8000000 #define FLASH_START_ADDR ((uint32_t)0x8000000) #define FLASH_END_ADDR ((uint32_t)(0x8000000 + FLASH_SECTOR_NUM * FLASH_SECTOR_SIZE)) #define FLASH_SECTOR_NUM 128 // 扇区数量 #define FLASH_SECTOR_SIZE 1024 // 扇区大小1KB typedef enum { FLASH_FAILURE = 0, FLASH_SUCCESS, } flash_status_t; //=========================================================================== flash_status_t FlashErase(uint32_t addr, uint8_t count); uint32_t FlashWrite(uint32_t addr, uint8_t *buffer, uint32_t length); uint32_t FlashRead(uint32_t addr, uint8_t *buffer, uint32_t length); #endif /* _MCU_FLASH_H_ */ #include "mcu_flash.h" #include #include "stm32f10x_flash.h" // addr:地址 count:块数量 flash_status_t FlashErase(uint32_t addr, uint8_t count) { uint8_t i; FLASH_Unlock(); FLASH_ClearFlag(FLASH_FLAG_BSY | FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR); for(i = 0; i < count; ++i) { if(FLASH_ErasePage(addr + i * FLASH_SECTOR_SIZE) != FLASH_COMPLETE) { return FLASH_FAILURE; } } FLASH_Lock(); return FLASH_SUCCESS; } uint32_t FlashWrite(uint32_t addr, uint8_t *buffer, uint32_t length) { uint16_t i, data = 0; FLASH_Unlock(); FLASH_ClearFlag(FLASH_FLAG_BSY | FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR); for(i = 0; i < length; i += 2) { data = (*(buffer + i + 1) << 8) + (*(buffer + i)); if(FLASH_ProgramHalfWord((uint32_t)(addr + i), data) != FLASH_COMPLETE) { return i; } } FLASH_Lock(); return length; } uint32_t FlashRead(uint32_t addr, uint8_t *buffer, uint32_t length) { memcpy(buffer, (void *)addr, length); return length; }
上一篇:STM32f103 —— can通信
下一篇:STM32 —— 多路DAC(输出电压和正弦波)
推荐阅读最新更新时间:2024-11-17 17:28
设计资源 培训 开发板 精华推荐
- AD8034AR高速峰值检波器有限输入范围的典型应用电路
- VL822_Hub
- MIC23201 的典型应用:具有超速控制的 2MHz PWM 2A 降压稳压器
- TB2938HQ 最大功率 49W BTL x 4ch 音频功率的典型应用
- 2_milk修改版
- 使用 LT3045HDD PGFB 禁用并具有反向输入保护的典型应用
- 使用 Diodes Incorporated 的 AP1506 的参考设计
- LTC3633AIUFD-2 双通道 3A、20V 单片式同步降压型稳压器的典型应用电路
- 使用 Analog Devices 的 LTC3803IS6-5 的参考设计
- LT6654AMPS6-3.3 具有升压输出电流和电流限制的电压基准的典型应用