写入的固定字节的逻辑
刚上电需要查找到上次写入的位置
运行的时候就按照找到的位置一直写下去
uint32_t timer_value;//找到刚上电flash 的起始地址和 SEQ
uint32_t timer_start_addr;//
static uint32_t check_last_place_and_timer_value(void)
{
static uint32_t *(value_p)=NULL;
value_p =(uint32_t *)(timer_fstorage.start_addr);
static uint16_t cnt =0;
cnt=((timer_fstorage.end_addr+1- timer_fstorage.start_addr)/4)-1;
static uint64_t sum1=0;
static uint64_t sum2=0;
for(uint16_t j=0;j<cnt;j++)
{
sum1=0;
sum2=0;
for(uint8_t i=0;i<1;i++)//第一个值
{
sum1+=*(value_p);
value_p+=1;
}
for(uint8_t i=0;i<1;i++)//第二个值
{
sum2+=*(value_p);
value_p++;
}
if((sum1!=0xFFFFFFFF)&&(sum2==0xFFFFFFFF))
{
value_p-=2;//存SEQ——NUM的位置
// uint32_t *alarm_msg_p=(alarm_msg_t * )value_p;
//cur_seq_num =alarm_msg_p->seq_num;
timer_value=* value_p;
timer_start_addr=(uint32_t)value_p+4 ;
PINFO("timer_start_addr %x",timer_start_addr);
PINFO("timer_value %d",timer_value);
return 1;
break;
}
value_p-=1;//
}
//需要考虑最后一个字节是数据 timer_fstorage.end_addr是数据 timer_fstorage.start_addr是全0xFF
sum2 =*((uint32_t *)(timer_fstorage.start_addr));//
static uint32_t flash=0;
flash=(timer_fstorage.end_addr+1-4);//
sum1 =*(uint32_t *)flash;
if((sum1!=0xFFFFFFFF)&&(sum2==0xFFFFFFFF))//
{
value_p-=2;//存SEQ——NUM的位置
// uint32_t *alarm_msg_p=(alarm_msg_t * )value_p;
//cur_seq_num =alarm_msg_p->seq_num;
nrf_fstorage_erase(&timer_fstorage, fstorage.start_addr, 1, NULL);//erase one page
wait_for_flash_ready(&timer_fstorage);
timer_value=sum1;
timer_start_addr=timer_fstorage.start_addr;//;(timer_fstorage.end_addr+1-4) ;
PINFO("timer_start_addr %x",timer_start_addr);
PINFO("timer_value %d",timer_value);
return 1;
}
timer_value= 0;
timer_start_addr=timer_fstorage.start_addr;
//cur_seq_num =0;
//cur_start_addr= timer_fstorage.start_addr;//从0地址开始的数据需要擦除第一页
PINFO("timer_start_addr %x",timer_start_addr);
PINFO("timer_value %d",timer_value);
nrf_fstorage_erase(&timer_fstorage, fstorage.start_addr, 1, NULL);//erase one page
wait_for_flash_ready(&timer_fstorage);
return 0;
}
static void write_timer_flash_func( uint32_t * timer)//ʵÏÖÑ»·Ð´ flash µÄ¹¦ÄÜ
{
ret_code_t rc;
ret_code_t err_code;
//static uint32_t a=1000;
//static uint32_t end_addr;
//end_addr =0;
if(write_timer_addr<timer_fstorage.end_addr)
{
rc = nrf_fstorage_write(&timer_fstorage, write_timer_addr,timer, sizeof(uint32_t), NULL);
APP_ERROR_CHECK(rc);
wait_for_flash_ready(&timer_fstorage);
write_timer_addr+=sizeof(uint32_t);
alarm_msg.seq_num++;
// //PINFO("seq_num “%d”, alarm_msg_p->seq_num);
// //дµ½Ã¿Ò»Ò³µÄ×îºóÁ½¸öÊý¾Ý¿é ¾Í²Á³ýÏÂÒ»Ò³
if(((write_timer_addr%0x1000)(0x1000-0x4))||((write_timer_addr%0x1000)(0x1000-0x8)))
{
//NRF_LOG_INFO("erase next page 4096 \"%x\" .", write_timer_addr);
uint32_t temp_addr=0;
if((write_timer_addr%0x1000)==(0x1000-0x4))
{
temp_addr=write_timer_addr+0x4;
if(temp_addr>timer_fstorage.end_addr)
{
temp_addr =timer_fstorage.start_addr;
}
nrf_gpio_pin_set(30);
err_code = nrf_fstorage_erase(&timer_fstorage, temp_addr, 1, NULL);//erase one page
APP_ERROR_CHECK(err_code);
wait_for_flash_ready(&timer_fstorage);
nrf_gpio_pin_clear(30);
}
else
{
temp_addr=write_timer_addr+0x8;
if(temp_addr>timer_fstorage.end_addr)
{
temp_addr =timer_fstorage.start_addr;
}
nrf_gpio_pin_set(30);
err_code = nrf_fstorage_erase(&timer_fstorage, temp_addr, 1, NULL);//erase one page
APP_ERROR_CHECK(err_code);
wait_for_flash_ready(&timer_fstorage);
nrf_gpio_pin_clear(30);
}
PINFO("erase next page 4096 \"%x\" .", temp_addr);
}
if(write_timer_addr>timer_fstorage.end_addr)
{
write_timer_addr=timer_fstorage.start_addr;
}
if(write_timer_addr==0x60000)
{
static uint8_t a;
a++;
}
// NRF_LOG_INFO("write_timer_addr \"%x\" .", write_timer_addr);
}
}