1 /* 2 * Copyright (C) 2017 BlueKitchen GmbH 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the copyright holders nor the names of 14 * contributors may be used to endorse or promote products derived 15 * from this software without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD AND CONTRIBUTORS 18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 20 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 21 * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 24 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 25 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 27 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 * 30 */ 31 32 /* 33 * hal_flash_bank_memory.c -- volatile test environment that provides just two memory banks 34 * 35 */ 36 37 #define __BTSTACK_FILE__ "hal_flash_bank_memory.c" 38 39 #include "hal_flash_bank.h" 40 #include "hal_flash_bank_memory.h" 41 #include "btstack_debug.h" 42 43 #include "stdint.h" 44 #include "string.h" 45 46 #ifdef BTSTACK_TEST 47 #include "stdio.h" 48 #include <stdlib.h> // exit(..) 49 #endif 50 51 static uint32_t hal_flash_bank_memory_get_size(void * context){ 52 hal_flash_bank_memory_t * self = (hal_flash_bank_memory_t *) context; 53 return self->bank_size; 54 } 55 56 static uint32_t hal_flash_bank_memory_get_alignment(void * context){ 57 UNUSED(context); 58 return 1; 59 } 60 61 static void hal_flash_bank_memory_erase(void * context, int bank){ 62 hal_flash_bank_memory_t * self = (hal_flash_bank_memory_t *) context; 63 if (bank > 1) return; 64 memset(self->banks[bank], 0xff, self->bank_size); 65 } 66 67 static void hal_flash_bank_memory_read(void * context, int bank, uint32_t offset, uint8_t * buffer, uint32_t size){ 68 hal_flash_bank_memory_t * self = (hal_flash_bank_memory_t *) context; 69 70 // log_info("read offset %u, len %u", offset, size); 71 72 if (bank > 1) return; 73 if (offset > self->bank_size) return; 74 if ((offset + size) > self->bank_size) return; 75 76 memcpy(buffer, &self->banks[bank][offset], size); 77 } 78 79 static void hal_flash_bank_memory_write(void * context, int bank, uint32_t offset, const uint8_t * data, uint32_t size){ 80 hal_flash_bank_memory_t * self = (hal_flash_bank_memory_t *) context; 81 82 log_info("write offset %u, len %u", offset, size); 83 log_info_hexdump(data, size); 84 85 if (bank > 1) return; 86 if (offset > self->bank_size) return; 87 if ((offset + size) > self->bank_size) return; 88 89 int i; 90 for (i=0;i<size;i++){ 91 // write 0xff doesn't change anything 92 if (data[i] == 0xff) continue; 93 // writing something other than 0x00 is only allowed once 94 if (self->banks[bank][offset] != 0xff && data[i] != 0x00){ 95 printf("Error: offset %u written twice. Data: 0x%02x!\n", offset+i, data[i]); 96 exit(10); 97 return; 98 } 99 self->banks[bank][offset++] = data[i]; 100 } 101 } 102 103 static const hal_flash_bank_t hal_flash_bank_memory_instance = { 104 /* uint32_t (*get_size)(..) */ &hal_flash_bank_memory_get_size, 105 /* uint32_t (*get_alignment)(..); */ &hal_flash_bank_memory_get_alignment, 106 /* void (*erase)(..); */ &hal_flash_bank_memory_erase, 107 /* void (*read)(..); */ &hal_flash_bank_memory_read, 108 /* void (*write)(..); */ &hal_flash_bank_memory_write, 109 }; 110 111 /** 112 * Initialize instance 113 */ 114 const hal_flash_bank_t * hal_flash_bank_memory_init_instance(hal_flash_bank_memory_t * self, uint8_t * storage, uint32_t storage_size){ 115 self->bank_size = storage_size / 2; 116 self->banks[0] = storage; 117 self->banks[1] = &storage[self->bank_size]; 118 memset(storage, 0xff, storage_size); 119 return &hal_flash_bank_memory_instance; 120 } 121 122