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 BLUEKITCHEN GMBH 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 BLUEKITCHEN
21 * GMBH 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 #include "inttypes.h"
46
hal_flash_bank_memory_get_size(void * context)47 static uint32_t hal_flash_bank_memory_get_size(void * context){
48 hal_flash_bank_memory_t * self = (hal_flash_bank_memory_t *) context;
49 return self->bank_size;
50 }
51
hal_flash_bank_memory_get_alignment(void * context)52 static uint32_t hal_flash_bank_memory_get_alignment(void * context){
53 UNUSED(context);
54 return 1;
55 }
56
hal_flash_bank_memory_erase(void * context,int bank)57 static void hal_flash_bank_memory_erase(void * context, int bank){
58 hal_flash_bank_memory_t * self = (hal_flash_bank_memory_t *) context;
59 if (bank > 1) return;
60 memset(self->banks[bank], 0xff, self->bank_size);
61 }
62
hal_flash_bank_memory_read(void * context,int bank,uint32_t offset,uint8_t * buffer,uint32_t size)63 static void hal_flash_bank_memory_read(void * context, int bank, uint32_t offset, uint8_t * buffer, uint32_t size){
64 hal_flash_bank_memory_t * self = (hal_flash_bank_memory_t *) context;
65
66 // log_info("read offset %u, len %u", offset, size);
67
68 if (bank > 1) return;
69 if (offset > self->bank_size) return;
70 if ((offset + size) > self->bank_size) return;
71
72 memcpy(buffer, &self->banks[bank][offset], size);
73 }
74
hal_flash_bank_memory_write(void * context,int bank,uint32_t offset,const uint8_t * data,uint32_t size)75 static void hal_flash_bank_memory_write(void * context, int bank, uint32_t offset, const uint8_t * data, uint32_t size){
76 hal_flash_bank_memory_t * self = (hal_flash_bank_memory_t *) context;
77
78 log_info("write offset %" PRIu32", len %" PRIu32, offset, size);
79 log_info_hexdump(data, size);
80
81 if (bank > 1) return;
82 if (offset > self->bank_size) return;
83 if ((offset + size) > self->bank_size) return;
84
85 uint32_t i;
86 for (i=0;i<size;i++){
87 // write 0xff doesn't change anything
88 if (data[i] == 0xff) {
89 offset++;
90 continue;
91 }
92 // writing something other than 0x00 is only allowed once
93 if (self->banks[bank][offset] != 0xff && data[i] != 0x00){
94 log_error("Error: offset %" PRIu32 " written twice. Data: 0x%02x!", offset+i, data[i]);
95 } else {
96 self->banks[bank][offset++] = data[i];
97 }
98 }
99 }
100
101 static const hal_flash_bank_t hal_flash_bank_memory_instance = {
102 /* uint32_t (*get_size)(..) */ &hal_flash_bank_memory_get_size,
103 /* uint32_t (*get_alignment)(..); */ &hal_flash_bank_memory_get_alignment,
104 /* void (*erase)(..); */ &hal_flash_bank_memory_erase,
105 /* void (*read)(..); */ &hal_flash_bank_memory_read,
106 /* void (*write)(..); */ &hal_flash_bank_memory_write,
107 };
108
109 /**
110 * Initialize instance
111 */
hal_flash_bank_memory_init_instance(hal_flash_bank_memory_t * self,uint8_t * storage,uint32_t storage_size)112 const hal_flash_bank_t * hal_flash_bank_memory_init_instance(hal_flash_bank_memory_t * self, uint8_t * storage, uint32_t storage_size){
113 self->bank_size = storage_size / 2;
114 self->banks[0] = storage;
115 self->banks[1] = &storage[self->bank_size];
116 memset(storage, 0xff, storage_size);
117 return &hal_flash_bank_memory_instance;
118 }
119
120