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