cancel
Showing results for 
Search instead for 
Did you mean: 

Store data to flash failed

YSHUA.1
Associate

The MCU is STM32L051C8T6 

when i store my data to flash ,it always be failed .

Is there any key point ignored by me, leading to some peripheral function conflicts。

This is a very urgent problem。

Thank you for all the answers �?�?

Thank you all

I am trying to use stm32l051 flash to store data. I used ADC, uart1, UART2, TIM2, RTC, watchdog peripherals in my project. Except for ADC, interrupt is used.

In my test program, without the above peripherals, flash can be erased and written normally.

But when I enable the above peripherals, and do the corresponding processing functions, flash erase and write will have problems. Erase failed, write failed. The error returned is pFlash.ErrorCode = FLASH_ERROR_WRP;

Here is my process code.

The detailed code is in the C file of the Enclosures

main{

Power_on_Flag=0;

  HAL_Init();

  SystemClock_Config();

SystemCoreClockUpdate();

All_GPIO_Init();

ADC1_Init();

TIM2_Init();

USART1_Init();

USART2_Init();

HAL_Delay(2000);

Configure_RTC();

 iwatchdog_init();

senser_data_error_num = get_data_store_length(); // this fuction can get the addr to write,without erase

get_senser_data();

feed_iwatchdog();

flash_test();

}

void flash_test(void)

{

unsigned char i = 0;

// flash_erase_page(); //erase all data ,I have tried to open or close this fuction

for(i = 0; i < 5 ; i++ )

{

sensee_data.temp_air = 0-i ;

sensee_data.Rh_air = i ;

sensee_data.temp_soil = i ;

sensee_data.Rh_soil = i ;

sensee_data.sun_shine = i ;

sensee_data.co2 = i ;

sensee_data.bat_val = i ;

write_data_to_flash(i+senser_data_error_num); //at begining ++

write_data_to_flash(MAX_DATA_NUMS-i-senser_data_error_num);//at ending --

}

while(1);

}

void write_data_to_flash(unsigned short nums)

{

union 

{

store_senser_t store_senser_buffer;

unsigned long uint_buffer_arriry[DATA_LENGTH/4];

}databuffer;

unsigned char i = 0 ;

unsigned long address = 0 ;

if(nums == 0)

return ;

 HAL_FLASH_Unlock();

HAL_FLASH_OB_Unlock();

HAL_Delay(10);

address = FLASH_DATA_ADDR + (nums - 1 ) * DATA_LENGTH ;

change_senser_data_to_flash_type();

databuffer.store_senser_buffer.time.year =  store_senser.time.year  ;

databuffer.store_senser_buffer.time.month =  store_senser.time.month  ;

databuffer.store_senser_buffer.time.date =  store_senser.time.date   ;

databuffer.store_senser_buffer.time.hour =  store_senser.time.hour   ;

databuffer.store_senser_buffer.time.min  =  store_senser.time.min    ;

databuffer.store_senser_buffer.data.bat_value =  store_senser.data.bat_value  ;

databuffer.store_senser_buffer.data.data_air_Rh =  store_senser.data.data_air_Rh   ;

databuffer.store_senser_buffer.data.data_air_temp =  store_senser.data.data_air_temp   ;

databuffer.store_senser_buffer.data.data_co2   =  store_senser.data.data_co2     ;

databuffer.store_senser_buffer.data.data_light  =  store_senser.data.data_light    ;

databuffer.store_senser_buffer.data.data_soil_Rh =  store_senser.data.data_soil_Rh   ;

databuffer.store_senser_buffer.data.data_soil_temp =  store_senser.data.data_soil_temp  ;

for(i = 0 ; i < DATA_LENGTH/4 ; i++) 

HAL_FLASH_Program(TYPEPROGRAM_WORD, address + i*4, databuffer.uint_buffer_arriry[i]);

HAL_FLASH_Lock();

HAL_FLASH_OB_Lock();

}

0 REPLIES 0