存储组件的实现思路分析及代码实现
生活随笔
收集整理的這篇文章主要介紹了
存储组件的实现思路分析及代码实现
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
文章目錄
- 1 存儲(chǔ)組件實(shí)現(xiàn)思路
- 1.1 存儲(chǔ)方式的選擇
- 1.2 stm32的存儲(chǔ)冷知識(shí)
- 1.3 eeprom存儲(chǔ)數(shù)據(jù)結(jié)構(gòu)設(shè)計(jì)
- 1.4 存儲(chǔ)組件操作流程
- 2 代碼實(shí)現(xiàn)
1 存儲(chǔ)組件實(shí)現(xiàn)思路
1.1 存儲(chǔ)方式的選擇
一般存儲(chǔ)方案可以分為如下幾種:
stm32l0有內(nèi)部的eeprom,所以我們更傾向于使用內(nèi)部的eeprom,并且eeprom也是統(tǒng)一編址,只是使用時(shí)需要先解鎖。
1.2 stm32的存儲(chǔ)冷知識(shí)
對(duì)于stm32f1和stm32l0存儲(chǔ)架構(gòu)是不一樣的,stm32f1在擦除flash時(shí)是不能響應(yīng)中斷的,而stm32l0擦除時(shí)是可以響應(yīng)中斷的。
1.3 eeprom存儲(chǔ)數(shù)據(jù)結(jié)構(gòu)設(shè)計(jì)
1.4 存儲(chǔ)組件操作流程
寫操作:
讀操作:
刪除操作:
2 代碼實(shí)現(xiàn)
app_storage.h:
#ifndef __APP_STORAGE_H #define __APP_STORAGE_H#include <stdbool.h>#define EEPROM_BANK1_BASE (0x08080000UL) #define EEPROM_BANK1_END (0x08080BFFUL) #define EEPROM_BANK1_SIZE 3072 #define EEPROM_BANK2_BASE (0x08080C00UL) #define EEPROM_BANK2_END (0x080817FFUL) #define EEPROM_BANK2_SIZE 3072#define STORAGE_PACK_LEN 64 #define STORAGE_DATA_LEN (STORAGE_PACK_LEN-8) #define STORAGE_PACK_COUNT (EEPROM_BANK2_SIZE/STORAGE_PACK_LEN) #define STORAGE_USED_FLAG 0XA1A2A3A4typedef struct {uint32_t flag;uint16_t name;uint16_t len;uint8_t data[STORAGE_DATA_LEN]; }storage_t;#define STORAGE ((storage_t*)EEPROM_BANK2_BASE)bool storage_write(uint16_t name,uint16_t len,uint8_t* pdata); bool storage_read(uint16_t name,uint16_t* p_len,uint8_t* pdata); bool storage_delete(uint16_t name);#endifapp_storage.c:
#include <stdbool.h> #include <stdint.h> #include <string.h> #include "main.h" #include "app_storage.h"static uint16_t storage_find_null() {for(uint8_t i=0;i<STORAGE_PACK_COUNT;i++){if(STORAGE[i].flag!=STORAGE_USED_FLAG){return i;}}return 0XFFFF; } static uint16_t storage_find_name(uint16_t name) {for(uint8_t i=0;i<STORAGE_PACK_COUNT;i++){if((STORAGE[i].flag==STORAGE_USED_FLAG)&&(STORAGE[i].name==name)){return i;}}return 0XFFFF; }bool storage_write(uint16_t name,uint16_t len,uint8_t* pdata) {uint16_t pack_number;if(len>STORAGE_DATA_LEN)return false;pack_number=storage_find_name(name);if(pack_number>=STORAGE_PACK_COUNT){pack_number=storage_find_null();if(pack_number>=STORAGE_PACK_COUNT)return false;} HAL_FLASHEx_DATAEEPROM_Unlock();HAL_FLASHEx_DATAEEPROM_Erase((uint32_t)&STORAGE[pack_number].flag);HAL_FLASHEx_DATAEEPROM_Program(FLASH_TYPEPROGRAMDATA_HALFWORD,(uint32_t)&STORAGE[pack_number].name,name);HAL_FLASHEx_DATAEEPROM_Program(FLASH_TYPEPROGRAMDATA_HALFWORD,(uint32_t)&STORAGE[pack_number].len,len);for(uint8_t i=0;i<len;i++){HAL_FLASHEx_DATAEEPROM_Program(FLASH_TYPEPROGRAMDATA_BYTE,(uint32_t)STORAGE[pack_number].data+i,pdata[i]);}HAL_FLASHEx_DATAEEPROM_Program(FLASH_TYPEPROGRAMDATA_WORD,(uint32_t)&STORAGE[pack_number].flag,STORAGE_USED_FLAG);HAL_FLASHEx_DATAEEPROM_Lock();return true; }bool storage_read(uint16_t name,uint16_t* p_len,uint8_t* pdata) {uint16_t pack_number;pack_number=storage_find_name(name);if(pack_number>=STORAGE_PACK_COUNT)return false;if(*p_len<STORAGE[pack_number].len)return false;memcpy(pdata,STORAGE[pack_number].data,STORAGE[pack_number].len);*p_len=STORAGE[pack_number].len;return true; } bool storage_delete(uint16_t name) {uint16_t pack_number;pack_number=storage_find_name(name);if(pack_number>=STORAGE_PACK_COUNT)return false; HAL_FLASHEx_DATAEEPROM_Unlock();HAL_FLASHEx_DATAEEPROM_Erase((uint32_t)&STORAGE[pack_number].flag);HAL_FLASHEx_DATAEEPROM_Lock(); return true; }測(cè)試代碼如下:
/* USER CODE END Header_StartDefaultTask */ void StartDefaultTask(void const * argument) {/* USER CODE BEGIN StartDefaultTask *//* Infinite loop */float temp,humi;uint8_t write_buf[]="1234567890";uint8_t read_buf[20];uint16_t read_len;#define TEST_NAME 0X0001debug_init();led_init();key_init();led_blink(2,5);sht30_init();for(;;){storage_write(TEST_NAME,sizeof(write_buf),write_buf);memset(read_buf,0,20);read_len=20;if(storage_read(TEST_NAME,&read_len,read_buf)){DEBUG("------%s-----",read_buf);}// if(sht30_get_temp_humi(&temp,&humi))// {// DEBUG("temp=%f,humi=%f",temp,humi);// }osDelay(1000);}/* USER CODE END StartDefaultTask */ } 《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結(jié)
以上是生活随笔為你收集整理的存储组件的实现思路分析及代码实现的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 铁投集团是做什么的
- 下一篇: 字节跳动上市了吗 哪一年成立的