MCPcopy Create free account
hub / github.com/kiibohd/controller / storage_write

Function storage_write

Lib/storage.c:348–456  ·  view source on GitHub ↗

Write storage block data - Buffer to write from address - Starting address to write to size - Number of bytes to write into storage clear - Set 0 for normal write, Set 1 to indicate nothing of value in the block (empty block) storage_init() must be called first Returns 0 - Not enough space 1 - Success 2 - Failed to write successfully, but incremented counters (i.e. try again)

Source from the content-addressed store, hash-verified

346// 1 - Success
347// 2 - Failed to write successfully, but incremented counters (i.e. try again)
348uint8_t storage_write(uint8_t* data, uint16_t address, uint16_t size, uint8_t clear )
349{
350 // Flashing buffer
351 uint8_t page_buffer[STORAGE_FLASH_PAGE_SIZE];
352
353 // Make sure there is enough address space in the storage
354 if ( size + address > STORAGE_SIZE )
355 {
356 return 0;
357 }
358
359 // Make sure this isn't the same data already set in flash
360 // If so, just exit, no need to wear the flash any further
361 if ( memcmp( storage_buffer, data, size ) == 0 )
362 {
363 return 1;
364 }
365
366 // Set internal buffer to all 0xffs
367 // This way it's possible to do partial writes to the page
368 for ( int i = 0; i < STORAGE_FLASH_PAGE_SIZE; i++ )
369 {
370 page_buffer[i] = 0xff;
371 }
372
373 // Erase flash, only erases if erase_flag has been set
374 storage_erase_flash();
375
376 // Check which type of block we are writing
377 uint8_t block_type = 0x00; // Normal block
378 if ( clear )
379 {
380 // Cleared page, update status to indicate this page should be ignored
381 block_type = 0x02;
382 cleared_block = 1;
383 }
384 else
385 {
386 // Valid block
387 cleared_block = 0;
388 }
389
390 // Clear empty page flag
391 page_buffer[(current_storage_index * (STORAGE_SIZE + 1))] = block_type;
392
393 // Prepare flashing and in-memory buffers so we can write to flash
394 for ( int i = 0; i < size; i++ )
395 {
396 // Write to flashing buffer
397 page_buffer[i + address + (current_storage_index * (STORAGE_SIZE + 1)) + 1] = data[i];
398
399 // Write to in-memory storage
400 storage_buffer[i + address] = data[i];
401 }
402
403 // Write flashing buffer to flash
404 uint32_t status = flash_write(
405 (STORAGE_FLASH_START + current_page * STORAGE_FLASH_PAGE_SIZE),

Callers 2

storage_clear_pageFunction · 0.85
storage_save_settingsFunction · 0.85

Calls 3

storage_erase_flashFunction · 0.85
flash_writeFunction · 0.85
memcmpFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…