1 /*
2 * Copyright (C) 2019 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 * 4. Any redistribution, use, or modification is done solely for
17 * personal benefit and not for any commercial purpose or for
18 * monetary gain.
19 *
20 * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
24 * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * Please inquire about commercial licensing options at
34 * [email protected]
35 *
36 */
37
38 /*
39 * Made for BlueKitchen by OneWave with <3
40 * Author: [email protected]
41 */
42
43 #include <stdint.h>
44 #include <string.h> // memcpy
45
46 #include "hal_flash_bank_stm32wb.h"
47 #include "stm32wbxx_hal.h"
48 #include "shci.h"
49 #include "app_conf.h"
50 #include "btstack_debug.h"
51
52 #define STM32WB_FLASH_ALIGNMENT 8
53 #define LITTLE_TO_64(buff, offset) ((uint64_t) ( (((uint64_t) ((uint8_t*)(buff))[(offset)+7]) << 56) | (((uint64_t) ((uint8_t*)(buff))[(offset)+6]) << 48) | (((uint64_t) ((uint8_t*)(buff))[(offset)+5]) << 40) | (((uint64_t) ((uint8_t*)(buff))[(offset)+4]) << 32) | (((uint64_t) ((uint8_t*)(buff))[(offset)+3]) << 24) | (((uint64_t) ((uint8_t*)(buff))[(offset)+2]) << 16) | (((uint64_t) ((uint8_t*)(buff))[(offset)+1]) << 8) | (((uint64_t) ((uint8_t*)(buff))[(offset)+0]) << 0) ))
54
hal_flash_bank_stm32wb_get_size(void * context)55 static uint32_t hal_flash_bank_stm32wb_get_size(void * context){
56 hal_flash_bank_stm32wb_t * self = (hal_flash_bank_stm32wb_t *) context;
57 return self->page_size;
58 }
59
hal_flash_bank_stm32wb_get_alignment(void * context)60 static uint32_t hal_flash_bank_stm32wb_get_alignment(void * context){
61 UNUSED(context);
62 return STM32WB_FLASH_ALIGNMENT;
63 }
64
hal_flash_bank_stm32wb_erase(void * context,int bank)65 static void hal_flash_bank_stm32wb_erase(void * context, int bank){
66 hal_flash_bank_stm32wb_t * self = (hal_flash_bank_stm32wb_t *) context;
67 if (bank > 1) return;
68 FLASH_EraseInitTypeDef eraseInit;
69 eraseInit.TypeErase = FLASH_TYPEERASE_PAGES;
70 eraseInit.Page = self->page_index[bank];
71 eraseInit.NbPages = 1;
72 uint32_t sectorError;
73
74 // Get HW semaphore to get flash access
75 while( LL_HSEM_1StepLock( HSEM, CFG_HW_FLASH_SEMID )){};
76
77 // Inform CPU2 from erase activity
78 SHCI_C2_FLASH_EraseActivity(ERASE_ACTIVITY_ON);
79
80 // Unlock the flash
81 HAL_FLASH_Unlock();
82
83 // Erase the flash
84 HAL_FLASHEx_Erase(&eraseInit, §orError);
85
86 // RE-Lock the flash
87 HAL_FLASH_Lock();
88
89 // Release HW semaphore
90 LL_HSEM_ReleaseLock( HSEM, CFG_HW_FLASH_SEMID, 0 );
91
92 // Inform CPU2 from end of flash erase activity
93 SHCI_C2_FLASH_EraseActivity(ERASE_ACTIVITY_OFF);
94 }
95
hal_flash_bank_stm32wb_read(void * context,int bank,uint32_t offset,uint8_t * buffer,uint32_t size)96 static void hal_flash_bank_stm32wb_read(void * context, int bank, uint32_t offset, uint8_t * buffer, uint32_t size){
97 hal_flash_bank_stm32wb_t * self = (hal_flash_bank_stm32wb_t *) context;
98
99 if (bank > 1) return;
100 if (offset > self->page_size) return;
101 if ((offset + size) > self->page_size) return;
102
103 memcpy(buffer, (uint8_t *) (FLASH_BASE + self->page_start[bank] + offset), size);
104 }
105
hal_flash_bank_stm32wb_write(void * context,int bank,uint32_t offset,const uint8_t * data,uint32_t size)106 static void hal_flash_bank_stm32wb_write(void * context, int bank, uint32_t offset, const uint8_t * data, uint32_t size){
107 hal_flash_bank_stm32wb_t * self = (hal_flash_bank_stm32wb_t *) context;
108
109 if (bank > 1) return;
110 if (offset > self->page_size) return;
111 if ((offset + size) > self->page_size) return;
112
113 HAL_StatusTypeDef status = 0;
114
115 // Get HW semaphore to get flash access
116 while( LL_HSEM_1StepLock( HSEM, CFG_HW_FLASH_SEMID )){};
117
118 unsigned int i;
119 HAL_FLASH_Unlock();
120 for (i=0;i<size;i+=STM32WB_FLASH_ALIGNMENT){
121 status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, FLASH_BASE + self->page_start[bank] + offset + i, LITTLE_TO_64(data, i));
122 if (status != 0) break;
123 }
124 HAL_FLASH_Lock();
125
126 // Release HW semaphore
127 LL_HSEM_ReleaseLock( HSEM, CFG_HW_FLASH_SEMID, 0 );
128
129 if (status != 0){
130 log_error("Flash error: %x", status);
131 }
132 }
133
134 static const hal_flash_bank_t hal_flash_bank_stm32wb_impl = {
135 /* uint32_t (*get_size)() */ &hal_flash_bank_stm32wb_get_size,
136 /* uint32_t (*get_alignment)(..); */ &hal_flash_bank_stm32wb_get_alignment,
137 /* void (*erase)(..); */ &hal_flash_bank_stm32wb_erase,
138 /* void (*read)(..); */ &hal_flash_bank_stm32wb_read,
139 /* void (*write)(..); */ &hal_flash_bank_stm32wb_write,
140 };
141
hal_flash_bank_stm32wb_init_instance(hal_flash_bank_stm32wb_t * context,uint32_t page_size,uint32_t bank_0_page_id,uint32_t bank_1_page_id)142 const hal_flash_bank_t * hal_flash_bank_stm32wb_init_instance(hal_flash_bank_stm32wb_t * context, uint32_t page_size,
143 uint32_t bank_0_page_id, uint32_t bank_1_page_id){
144 context->page_size = page_size;
145 context->page_index[0] = bank_0_page_id;
146 context->page_index[1] = bank_1_page_id;
147 context->page_start[0] = bank_0_page_id * page_size;
148 context->page_start[1] = bank_1_page_id * page_size;
149 return &hal_flash_bank_stm32wb_impl;
150 }
151