bosko lekovic
/
EthToCom_11
nova proba
DataFlash/flashDataBlock.cpp
- Committer:
- bosko001
- Date:
- 2020-06-11
- Revision:
- 22:11279d24a105
- Parent:
- 13:03617f6ed112
- Child:
- 26:c14d034d7459
File content as of revision 22:11279d24a105:
#include "mbed.h" #include "flashDataBlock.h" #include "FlashIAPBlockDevice.h" extern void printf_niz( char *s, char *niz, int val ); //{ // printf("%s",s); // for( int i=0; i < val; i++) printf("%c", niz[i]); // printf("\n\r"); //} FlashIAPBlockDevice bd((uint32_t)FLASH_ADDRESS,BLOCK_VALUE); int device_size; int read_size; int program_size; int erase_size; int erase_value; int buffer_dimension = buffer_dimension; int init_fdb( ) { // Initialize the flash IAP block device and print the memory layout int err = bd.init(); if( err ) { printf("flash data block init err=%d \n\r", err); return 1; } else printf("flash data block init OK \n\r"); device_size = bd.size(); read_size = bd.get_read_size(); program_size = bd.get_program_size(); erase_size = bd.get_erase_size(); erase_value = bd.get_erase_value(); // printf("Flash block device size: %x\n\r", device_size = bd.size()); // printf("Flash block device read size: %x\n\r", read_size = bd.get_read_size()); // printf("Flash block device program size: %x\n\r", program_size = bd.get_program_size()); // printf("Flash block device erase size: %x\n\r", erase_size = bd.get_erase_size()); // printf("Flash block device geterase value: %x\n\r", erase_value = bd.get_erase_value()); if( bd.is_valid_read( 0, bd.get_read_size() ) ) printf(" flash data block read valid \n\r"); else {printf(" flash data block read NOT valid \n\r"); return 2;} return 0; } void deinit_fdb( void ) { bd.deinit(); } int scale_buffer_dimension( int val ) { if( val > device_size ) { val = buffer_dimension = device_size; printf("buf_dim = %d\n\r", buffer_dimension);} else if( val > buffer_dimension ) { buffer_dimension = val; printf("buf_dim = %d\n\r", buffer_dimension);} return val; } int read_fdb( int rel_adr, char *buffer, int val ) { int err; val = scale_buffer_dimension( rel_adr+val ); err = bd.read(buffer, rel_adr, val); return (err == 0) ? val : 0 ; } int write_byte_in_flash( int adr, char value ) { int err = 0; int prog_page = program_size; int adr_first = (adr/prog_page)*prog_page; int adr_last = adr_first + prog_page; scale_buffer_dimension( adr ); char *buffer = (char*)malloc(buffer_dimension); if( !(err = bd.read(buffer, 0, buffer_dimension))) if( !(err = bd.erase(0, erase_size))) { buffer[adr] = value; err = bd.program(buffer, 0, (buffer_dimension % prog_page) ? buffer_dimension + prog_page - buffer_dimension % prog_page: buffer_dimension); } free(buffer); return err; } int write_niz_in_flash( int adr, char *niz, int nizval ) { int err = 0; int prog_page = bd.get_program_size(); int adr_first = (adr/prog_page)*prog_page; int adr_last = adr_first + prog_page; scale_buffer_dimension( adr + nizval ); char *buffer = (char*)malloc(buffer_dimension); if(!(err = bd.read(buffer, 0, buffer_dimension))) if(!(err = bd.erase(0, bd.get_erase_size()))) { for( int i = adr; i<(adr+nizval); i++) buffer[adr+i] = niz[i]; err = bd.program(buffer, 0, (buffer_dimension % prog_page) ? buffer_dimension + prog_page - buffer_dimension % prog_page: buffer_dimension); } free(buffer); return err; }