bosko lekovic
/
ETHtoCOM_10
nova verzija tcp+udp->serial com
DataFlash/flashDataBlock.cpp@22:11279d24a105, 2020-06-11 (annotated)
- Committer:
- bosko001
- Date:
- Thu Jun 11 10:41:11 2020 +0200
- Revision:
- 22:11279d24a105
- Parent:
- 13:03617f6ed112
- Child:
- 26:c14d034d7459
uradjen soft reset
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 | 22:11279d24a105 | 21 | int 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 | 22:11279d24a105 | 25 | if( err ) { printf("flash data block init err=%d \n\r", err); return 1; } |
bosko001 | 22:11279d24a105 | 26 | else printf("flash data block init OK \n\r"); |
bosko001 | 22:11279d24a105 | 27 | |
bosko001 | 22:11279d24a105 | 28 | device_size = bd.size(); |
bosko001 | 22:11279d24a105 | 29 | read_size = bd.get_read_size(); |
bosko001 | 22:11279d24a105 | 30 | program_size = bd.get_program_size(); |
bosko001 | 22:11279d24a105 | 31 | erase_size = bd.get_erase_size(); |
bosko001 | 22:11279d24a105 | 32 | erase_value = bd.get_erase_value(); |
bosko001 | 3:1196befeace0 | 33 | |
bosko001 | 22:11279d24a105 | 34 | // printf("Flash block device size: %x\n\r", device_size = bd.size()); |
bosko001 | 22:11279d24a105 | 35 | // printf("Flash block device read size: %x\n\r", read_size = bd.get_read_size()); |
bosko001 | 22:11279d24a105 | 36 | // printf("Flash block device program size: %x\n\r", program_size = bd.get_program_size()); |
bosko001 | 22:11279d24a105 | 37 | // printf("Flash block device erase size: %x\n\r", erase_size = bd.get_erase_size()); |
bosko001 | 22:11279d24a105 | 38 | // printf("Flash block device geterase value: %x\n\r", erase_value = bd.get_erase_value()); |
bosko001 | 3:1196befeace0 | 39 | |
bosko001 | 22:11279d24a105 | 40 | if( bd.is_valid_read( 0, bd.get_read_size() ) ) printf(" flash data block read valid \n\r"); |
bosko001 | 22:11279d24a105 | 41 | else {printf(" flash data block read NOT valid \n\r"); return 2;} |
bosko001 | 22:11279d24a105 | 42 | |
bosko001 | 22:11279d24a105 | 43 | return 0; |
bosko001 | 3:1196befeace0 | 44 | } |
bosko001 | 3:1196befeace0 | 45 | |
bosko001 | 3:1196befeace0 | 46 | void deinit_fdb( void ) |
bosko001 | 3:1196befeace0 | 47 | { |
bosko001 | 3:1196befeace0 | 48 | bd.deinit(); |
bosko001 | 3:1196befeace0 | 49 | } |
bosko001 | 3:1196befeace0 | 50 | |
bosko001 | 3:1196befeace0 | 51 | int scale_buffer_dimension( int val ) |
bosko001 | 3:1196befeace0 | 52 | { |
bosko001 | 3:1196befeace0 | 53 | if( val > device_size ) { val = buffer_dimension = device_size; printf("buf_dim = %d\n\r", buffer_dimension);} |
bosko001 | 3:1196befeace0 | 54 | else if( val > buffer_dimension ) { buffer_dimension = val; printf("buf_dim = %d\n\r", buffer_dimension);} |
bosko001 | 3:1196befeace0 | 55 | return val; |
bosko001 | 3:1196befeace0 | 56 | } |
bosko001 | 3:1196befeace0 | 57 | |
bosko001 | 3:1196befeace0 | 58 | int read_fdb( int rel_adr, char *buffer, int val ) |
bosko001 | 3:1196befeace0 | 59 | { |
bosko001 | 3:1196befeace0 | 60 | int err; |
bosko001 | 3:1196befeace0 | 61 | val = scale_buffer_dimension( rel_adr+val ); |
bosko001 | 3:1196befeace0 | 62 | err = bd.read(buffer, rel_adr, val); |
bosko001 | 3:1196befeace0 | 63 | return (err == 0) ? val : 0 ; |
bosko001 | 3:1196befeace0 | 64 | } |
bosko001 | 3:1196befeace0 | 65 | |
bosko001 | 3:1196befeace0 | 66 | int write_byte_in_flash( int adr, char value ) |
bosko001 | 3:1196befeace0 | 67 | { |
bosko001 | 3:1196befeace0 | 68 | int err = 0; |
bosko001 | 3:1196befeace0 | 69 | int prog_page = program_size; |
bosko001 | 3:1196befeace0 | 70 | int adr_first = (adr/prog_page)*prog_page; |
bosko001 | 3:1196befeace0 | 71 | int adr_last = adr_first + prog_page; |
bosko001 | 3:1196befeace0 | 72 | |
bosko001 | 3:1196befeace0 | 73 | scale_buffer_dimension( adr ); |
bosko001 | 3:1196befeace0 | 74 | |
bosko001 | 3:1196befeace0 | 75 | char *buffer = (char*)malloc(buffer_dimension); |
bosko001 | 3:1196befeace0 | 76 | |
bosko001 | 3:1196befeace0 | 77 | if( !(err = bd.read(buffer, 0, buffer_dimension))) |
bosko001 | 3:1196befeace0 | 78 | |
bosko001 | 3:1196befeace0 | 79 | if( !(err = bd.erase(0, erase_size))) |
bosko001 | 3:1196befeace0 | 80 | { |
bosko001 | 3:1196befeace0 | 81 | buffer[adr] = value; |
bosko001 | 3:1196befeace0 | 82 | err = bd.program(buffer, 0, (buffer_dimension % prog_page) ? buffer_dimension + prog_page - buffer_dimension % prog_page: buffer_dimension); |
bosko001 | 3:1196befeace0 | 83 | } |
bosko001 | 3:1196befeace0 | 84 | |
bosko001 | 3:1196befeace0 | 85 | free(buffer); |
bosko001 | 3:1196befeace0 | 86 | return err; |
bosko001 | 3:1196befeace0 | 87 | } |
bosko001 | 3:1196befeace0 | 88 | int write_niz_in_flash( int adr, char *niz, int nizval ) |
bosko001 | 3:1196befeace0 | 89 | { |
bosko001 | 3:1196befeace0 | 90 | int err = 0; |
bosko001 | 3:1196befeace0 | 91 | int prog_page = bd.get_program_size(); |
bosko001 | 3:1196befeace0 | 92 | int adr_first = (adr/prog_page)*prog_page; |
bosko001 | 3:1196befeace0 | 93 | int adr_last = adr_first + prog_page; |
bosko001 | 3:1196befeace0 | 94 | |
bosko001 | 3:1196befeace0 | 95 | scale_buffer_dimension( adr + nizval ); |
bosko001 | 3:1196befeace0 | 96 | |
bosko001 | 3:1196befeace0 | 97 | char *buffer = (char*)malloc(buffer_dimension); |
bosko001 | 3:1196befeace0 | 98 | |
bosko001 | 3:1196befeace0 | 99 | if(!(err = bd.read(buffer, 0, buffer_dimension))) |
bosko001 | 3:1196befeace0 | 100 | if(!(err = bd.erase(0, bd.get_erase_size()))) |
bosko001 | 3:1196befeace0 | 101 | { |
bosko001 | 3:1196befeace0 | 102 | for( int i = adr; i<(adr+nizval); i++) buffer[adr+i] = niz[i]; |
bosko001 | 3:1196befeace0 | 103 | err = bd.program(buffer, 0, (buffer_dimension % prog_page) ? buffer_dimension + prog_page - buffer_dimension % prog_page: buffer_dimension); |
bosko001 | 3:1196befeace0 | 104 | } |
bosko001 | 3:1196befeace0 | 105 | free(buffer); |
bosko001 | 3:1196befeace0 | 106 | return err; |
bosko001 | 3:1196befeace0 | 107 | } |