esp32-off-Line-tools/components/bsp_flash/bsp_flash.c

133 lines
3.6 KiB
C

#include "bsp_flash.h"
#include "esp_log.h"
static const char *TAG = "flash.h";
SemaphoreHandle_t xSemaphore_ESPFlash;
void ESPFlash_Init(void)
{
xSemaphore_ESPFlash = xSemaphoreCreateMutex();// 创建互斥型信号量。
}
uint32_t Read_ESPFlash(uint8_t SubType,uint32_t addr,uint8_t* buf,uint32_t len)
{
if(len == 0)return 1;
xSemaphoreTake( xSemaphore_ESPFlash, portMAX_DELAY );//获得资源的使用权
esp_partition_t* res=esp_partition_find_first(0x01,SubType,NULL);
if(res==NULL)
{
ESP_LOGI(TAG,"no partition is found\r\n");
xSemaphoreGive( xSemaphore_ESPFlash );//释放
return 0;
}
if(esp_partition_read(res,addr, &buf[0], len)!=ESP_OK)
{
ESP_LOGI(TAG,"SubType: 0x%X 0x%lX",SubType,addr);
ESP_LOGI(TAG,"partition read fail\r\n");
xSemaphoreGive( xSemaphore_ESPFlash );//释放
return 0x0;
}
else
{
xSemaphoreGive( xSemaphore_ESPFlash );//释放
return len;
}
return 0;
}
uint32_t IRAM_ATTR Write_ESPFlash(uint8_t SubType,uint32_t addr,uint8_t* buf,uint32_t len)
{
ESP_LOGI(TAG,"SubType: %d %lx",SubType,addr);
xSemaphoreTake( xSemaphore_ESPFlash, portMAX_DELAY );//获得资源的使用权
esp_partition_t* res=esp_partition_find_first(0x01,SubType,NULL);
if(res==NULL)
{
ESP_LOGI(TAG,"no partition is found\r\n");
xSemaphoreGive( xSemaphore_ESPFlash );//释放
return 0;
}
else
{
//ESP_LOGI(TAG,"partiton info:\r\n name:%s\r\n size:%x\r\n start address:%x\r\n",res->label,res->size,res->address);
}
#if 1 //使能后会先擦再写
if(esp_partition_erase_range(res,0,4096)!=ESP_OK)
{
ESP_LOGI(TAG,"partition erase fail\r\n");
xSemaphoreGive( xSemaphore_ESPFlash );//释放
return 0x0;
}
#endif
if(esp_partition_write(res,addr, &buf[0], len)!=ESP_OK)
{
ESP_LOGI(TAG,"partition write fail\r\n");
xSemaphoreGive( xSemaphore_ESPFlash );//释放
return 0x0;
}
else
{
xSemaphoreGive( xSemaphore_ESPFlash );//释放
return len;
}
return 0;
}
uint32_t IRAM_ATTR Erase_ESPFlash(uint8_t SubType,uint32_t addr)
{
xSemaphoreTake( xSemaphore_ESPFlash, portMAX_DELAY );//获得资源的使用权
esp_partition_t* res=esp_partition_find_first(0x01,SubType,NULL);
if(res==NULL)
{
//ESP_LOGI(TAG,"no partition is found\r\n");
xSemaphoreGive( xSemaphore_ESPFlash );//释放
return 0;
}
else
{
// ESP_LOGI(TAG,"partiton info:\r\n name:%s\r\n size:%lx\r\n start address:%lx\r\n",res->label,res->size,res->address);
}
if(esp_partition_erase_range(res,addr,4096)!=ESP_OK)
{
ESP_LOGI(TAG,"partition erase fail\r\n");
xSemaphoreGive( xSemaphore_ESPFlash );//释放
return 0x0;
}else{
xSemaphoreGive( xSemaphore_ESPFlash );//释放
return addr;
}
}
void test_partiton(void)
{
uint8_t write_partition_data[16],read_partition_data[20];
esp_partition_t* res=esp_partition_find_first(0x01,0x41,NULL);
if(res==NULL)
{
printf("no partition is found\r\n");
return;
}
else
{
printf("partiton info:\r\n name:%s\r\n size:%lx\r\n start address:%lx\r\n",
res->label,res->size,res->address);
}
if(esp_partition_read(res,0, read_partition_data, 16)!=ESP_OK)
{
printf("partition read fail\r\n");
}
else
{
printf("read partition data from :");
for(uint8_t i=0;i<16;i++)
printf("%X ",read_partition_data[i]);
printf("\r\n");
}
}