nova proba

Committer:
bosko001
Date:
Sat Apr 11 17:11:38 2020 +0000
Revision:
4:7abcf4543282
Parent:
DataFlash/flashDataBlock.cpp@3:1196befeace0
dxml bug removed

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bosko001 3:1196befeace0 1 #include "flashDataBlock.h"
bosko001 3:1196befeace0 2 #include "FlashIAPBlockDevice.h"
bosko001 3:1196befeace0 3
bosko001 3:1196befeace0 4 extern void printf_niz( char *s, char *niz, int val );
bosko001 3:1196befeace0 5 //{
bosko001 3:1196befeace0 6 // printf("%s",s);
bosko001 3:1196befeace0 7 // for( int i=0; i < val; i++) printf("%c", niz[i]);
bosko001 3:1196befeace0 8 // printf("\n\r");
bosko001 3:1196befeace0 9 //}
bosko001 3:1196befeace0 10
bosko001 3:1196befeace0 11
bosko001 3:1196befeace0 12 FlashIAPBlockDevice bd((uint32_t)FLASH_ADDRESS,BLOCK_VALUE);
bosko001 3:1196befeace0 13
bosko001 3:1196befeace0 14 int device_size;
bosko001 3:1196befeace0 15 int read_size;
bosko001 3:1196befeace0 16 int program_size;
bosko001 3:1196befeace0 17 int erase_size;
bosko001 3:1196befeace0 18 int erase_value;
bosko001 3:1196befeace0 19 int buffer_dimension = buffer_dimension;
bosko001 3:1196befeace0 20 void init_fdb( )
bosko001 3:1196befeace0 21 {
bosko001 3:1196befeace0 22 // Initialize the flash IAP block device and print the memory layout
bosko001 3:1196befeace0 23 int err = bd.init();
bosko001 3:1196befeace0 24 printf("init err=%d \n\r", err);
bosko001 3:1196befeace0 25
bosko001 3:1196befeace0 26 printf("Flash block device size: %llu\n\r", device_size = bd.size());
bosko001 3:1196befeace0 27 printf("Flash block device read size: %llu\n\r", read_size = bd.get_read_size());
bosko001 3:1196befeace0 28 printf("Flash block device program size: %llu\n\r", program_size = bd.get_program_size());
bosko001 3:1196befeace0 29 printf("Flash block device erase size: %llu\n\r", erase_size = bd.get_erase_size());
bosko001 3:1196befeace0 30 printf("Flash block device geterase value: %x\n\r", erase_value = bd.get_erase_value());
bosko001 3:1196befeace0 31
bosko001 3:1196befeace0 32 if( bd.is_valid_read( 0, bd.get_read_size() ) ) printf(" read valid \n\r");
bosko001 3:1196befeace0 33 }
bosko001 3:1196befeace0 34
bosko001 3:1196befeace0 35 void deinit_fdb( void )
bosko001 3:1196befeace0 36 {
bosko001 3:1196befeace0 37 bd.deinit();
bosko001 3:1196befeace0 38 }
bosko001 3:1196befeace0 39
bosko001 3:1196befeace0 40 int scale_buffer_dimension( int val )
bosko001 3:1196befeace0 41 {
bosko001 3:1196befeace0 42 if( val > device_size ) { val = buffer_dimension = device_size; printf("buf_dim = %d\n\r", buffer_dimension);}
bosko001 3:1196befeace0 43 else if( val > buffer_dimension ) { buffer_dimension = val; printf("buf_dim = %d\n\r", buffer_dimension);}
bosko001 3:1196befeace0 44 return val;
bosko001 3:1196befeace0 45 }
bosko001 3:1196befeace0 46
bosko001 3:1196befeace0 47 int read_fdb( int rel_adr, char *buffer, int val )
bosko001 3:1196befeace0 48 {
bosko001 3:1196befeace0 49 int err;
bosko001 3:1196befeace0 50 val = scale_buffer_dimension( rel_adr+val );
bosko001 3:1196befeace0 51 err = bd.read(buffer, rel_adr, val);
bosko001 3:1196befeace0 52 return (err == 0) ? val : 0 ;
bosko001 3:1196befeace0 53 }
bosko001 3:1196befeace0 54
bosko001 3:1196befeace0 55 int write_byte_in_flash( int adr, char value )
bosko001 3:1196befeace0 56 {
bosko001 3:1196befeace0 57 int err = 0;
bosko001 3:1196befeace0 58 int prog_page = program_size;
bosko001 3:1196befeace0 59 int adr_first = (adr/prog_page)*prog_page;
bosko001 3:1196befeace0 60 int adr_last = adr_first + prog_page;
bosko001 3:1196befeace0 61
bosko001 3:1196befeace0 62 scale_buffer_dimension( adr );
bosko001 3:1196befeace0 63
bosko001 3:1196befeace0 64 char *buffer = (char*)malloc(buffer_dimension);
bosko001 3:1196befeace0 65
bosko001 3:1196befeace0 66 if( !(err = bd.read(buffer, 0, buffer_dimension)))
bosko001 3:1196befeace0 67
bosko001 3:1196befeace0 68 if( !(err = bd.erase(0, erase_size)))
bosko001 3:1196befeace0 69 {
bosko001 3:1196befeace0 70 buffer[adr] = value;
bosko001 3:1196befeace0 71 err = bd.program(buffer, 0, (buffer_dimension % prog_page) ? buffer_dimension + prog_page - buffer_dimension % prog_page: buffer_dimension);
bosko001 3:1196befeace0 72 }
bosko001 3:1196befeace0 73
bosko001 3:1196befeace0 74 free(buffer);
bosko001 3:1196befeace0 75 return err;
bosko001 3:1196befeace0 76 }
bosko001 3:1196befeace0 77 int write_niz_in_flash( int adr, char *niz, int nizval )
bosko001 3:1196befeace0 78 {
bosko001 3:1196befeace0 79 int err = 0;
bosko001 3:1196befeace0 80 int prog_page = bd.get_program_size();
bosko001 3:1196befeace0 81 int adr_first = (adr/prog_page)*prog_page;
bosko001 3:1196befeace0 82 int adr_last = adr_first + prog_page;
bosko001 3:1196befeace0 83
bosko001 3:1196befeace0 84 scale_buffer_dimension( adr + nizval );
bosko001 3:1196befeace0 85
bosko001 3:1196befeace0 86 char *buffer = (char*)malloc(buffer_dimension);
bosko001 3:1196befeace0 87
bosko001 3:1196befeace0 88 if(!(err = bd.read(buffer, 0, buffer_dimension)))
bosko001 3:1196befeace0 89 if(!(err = bd.erase(0, bd.get_erase_size())))
bosko001 3:1196befeace0 90 {
bosko001 3:1196befeace0 91 for( int i = adr; i<(adr+nizval); i++) buffer[adr+i] = niz[i];
bosko001 3:1196befeace0 92 err = bd.program(buffer, 0, (buffer_dimension % prog_page) ? buffer_dimension + prog_page - buffer_dimension % prog_page: buffer_dimension);
bosko001 3:1196befeace0 93 }
bosko001 3:1196befeace0 94 free(buffer);
bosko001 3:1196befeace0 95 return err;
bosko001 3:1196befeace0 96 }