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