xref: /btstack/port/renesas-tb-s1ja-cc256x/template/btstack_example/src/hal_flash_bank_synergy.c (revision 2fca4dad957cd7b88f4657ed51e89c12615dda72)
1 /*
2  * Copyright (C) 2020 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 #include <stdint.h>
40 #include <string.h> // memcpy
41 
42 #include "hal_flash_bank_synergy.h"
43 #include "btstack_debug.h"
44 #include "hal_data.h"
45 
hal_flash_bank_synergy_get_size(void * context)46 static uint32_t hal_flash_bank_synergy_get_size(void * context){
47     hal_flash_bank_synergy_t * self = (hal_flash_bank_synergy_t *) context;
48     return self->page_size;
49 }
50 
hal_flash_bank_synergy_get_alignment(void * context)51 static uint32_t hal_flash_bank_synergy_get_alignment(void * context){
52     UNUSED(context);
53     return 1;
54 }
55 
hal_flash_bank_synergy_erase(void * context,int bank)56 static void hal_flash_bank_synergy_erase(void * context, int bank){
57     hal_flash_bank_synergy_t * self = (hal_flash_bank_synergy_t *) context;
58     if (bank > 1) return;
59     g_flash0.p_api->erase(g_flash0.p_ctrl, self->page_start[bank], 1);
60 }
61 
hal_flash_bank_synergy_read(void * context,int bank,uint32_t offset,uint8_t * buffer,uint32_t size)62 static void hal_flash_bank_synergy_read(void * context, int bank, uint32_t offset, uint8_t * buffer, uint32_t size){
63     hal_flash_bank_synergy_t * self = (hal_flash_bank_synergy_t *) context;
64 
65     if (bank > 1) return;
66     if (offset > self->page_size) return;
67     if ((offset + size) > self->page_size) return;
68 
69     memcpy(buffer, (uint8_t *) (self->page_start[bank] + offset), size);
70 }
71 
hal_flash_bank_synergy_write(void * context,int bank,uint32_t offset,const uint8_t * data,uint32_t size)72 static void hal_flash_bank_synergy_write(void * context, int bank, uint32_t offset, const uint8_t * data, uint32_t size){
73     hal_flash_bank_synergy_t * self = (hal_flash_bank_synergy_t *) context;
74 
75     if (bank > 1) return;
76     if (offset > self->page_size) return;
77     if ((offset + size) > self->page_size) return;
78 
79     g_flash0.p_api->write(g_flash0.p_ctrl, data, self->page_start[bank] + offset, size);
80 }
81 
82 static const hal_flash_bank_t hal_flash_bank_synergy_impl = {
83     /* uint32_t (*get_size)() */         &hal_flash_bank_synergy_get_size,
84     /* uint32_t (*get_alignment)(..); */ &hal_flash_bank_synergy_get_alignment,
85     /* void (*erase)(..);             */ &hal_flash_bank_synergy_erase,
86     /* void (*read)(..);              */ &hal_flash_bank_synergy_read,
87     /* void (*write)(..);             */ &hal_flash_bank_synergy_write,
88 };
89 
hal_flash_bank_synergy_init_instance(hal_flash_bank_synergy_t * context,uint32_t page_size,uint32_t bank_0_addr,uint32_t bank_1_addr)90 const hal_flash_bank_t * hal_flash_bank_synergy_init_instance(hal_flash_bank_synergy_t * context, uint32_t page_size,
91         uint32_t bank_0_addr, uint32_t bank_1_addr){
92     context->page_size = page_size;
93     context->page_start[0] = bank_0_addr;
94     context->page_start[1] = bank_1_addr;
95     return &hal_flash_bank_synergy_impl;
96 }
97