xref: /btstack/port/msp432p401lp-cc256x/hal_flash_bank_msp432.c (revision fc456f6da9c3708d9551b3d5d265a56ce46aa362)
1*fc456f6dSMatthias Ringwald /*
2*fc456f6dSMatthias Ringwald  * Copyright (C) 2017 BlueKitchen GmbH
3*fc456f6dSMatthias Ringwald  *
4*fc456f6dSMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
5*fc456f6dSMatthias Ringwald  * modification, are permitted provided that the following conditions
6*fc456f6dSMatthias Ringwald  * are met:
7*fc456f6dSMatthias Ringwald  *
8*fc456f6dSMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
9*fc456f6dSMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
10*fc456f6dSMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
11*fc456f6dSMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
12*fc456f6dSMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
13*fc456f6dSMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
14*fc456f6dSMatthias Ringwald  *    contributors may be used to endorse or promote products derived
15*fc456f6dSMatthias Ringwald  *    from this software without specific prior written permission.
16*fc456f6dSMatthias Ringwald  *
17*fc456f6dSMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD AND CONTRIBUTORS
18*fc456f6dSMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19*fc456f6dSMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20*fc456f6dSMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
21*fc456f6dSMatthias Ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22*fc456f6dSMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23*fc456f6dSMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
24*fc456f6dSMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25*fc456f6dSMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26*fc456f6dSMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27*fc456f6dSMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28*fc456f6dSMatthias Ringwald  * SUCH DAMAGE.
29*fc456f6dSMatthias Ringwald  *
30*fc456f6dSMatthias Ringwald  */
31*fc456f6dSMatthias Ringwald 
32*fc456f6dSMatthias Ringwald /*
33*fc456f6dSMatthias Ringwald  *  hal_flash_bank_stm32.c
34*fc456f6dSMatthias Ringwald  *
35*fc456f6dSMatthias Ringwald  *  HAL abstraction for Flash memory that can be written anywhere
36*fc456f6dSMatthias Ringwald  *  after being erased
37*fc456f6dSMatthias Ringwald  */
38*fc456f6dSMatthias Ringwald 
39*fc456f6dSMatthias Ringwald #include <stdint.h>
40*fc456f6dSMatthias Ringwald #include <string.h> // memcpy
41*fc456f6dSMatthias Ringwald 
42*fc456f6dSMatthias Ringwald #include "hal_flash_bank_msp432.h"
43*fc456f6dSMatthias Ringwald #include "driverlib.h"
44*fc456f6dSMatthias Ringwald #include "btstack_defines.h"
45*fc456f6dSMatthias Ringwald 
46*fc456f6dSMatthias Ringwald static uint32_t hal_flash_bank_msp432_get_size(void * context){
47*fc456f6dSMatthias Ringwald 	hal_flash_bank_msp432_t * self = (hal_flash_bank_msp432_t *) context;
48*fc456f6dSMatthias Ringwald 	return self->sector_size;
49*fc456f6dSMatthias Ringwald }
50*fc456f6dSMatthias Ringwald 
51*fc456f6dSMatthias Ringwald static uint32_t hal_flash_bank_memory_msp432_get_alignment(void * context){
52*fc456f6dSMatthias Ringwald     UNUSED(context);
53*fc456f6dSMatthias Ringwald     return 4;
54*fc456f6dSMatthias Ringwald }
55*fc456f6dSMatthias Ringwald 
56*fc456f6dSMatthias Ringwald static void hal_flash_bank_msp432_erase(void * context, int bank){
57*fc456f6dSMatthias Ringwald 
58*fc456f6dSMatthias Ringwald 	hal_flash_bank_msp432_t * self = (hal_flash_bank_msp432_t *) context;
59*fc456f6dSMatthias Ringwald 	if (bank > 1) return;
60*fc456f6dSMatthias Ringwald 
61*fc456f6dSMatthias Ringwald     /* Unprotecting sector  */
62*fc456f6dSMatthias Ringwald     MAP_FlashCtl_unprotectSector(FLASH_MAIN_MEMORY_SPACE_BANK1,self->sectors[bank]);
63*fc456f6dSMatthias Ringwald 
64*fc456f6dSMatthias Ringwald     /* Trying to erase the sector. Within this function, the API will
65*fc456f6dSMatthias Ringwald         automatically try to erase the maximum number of tries. If it fails,
66*fc456f6dSMatthias Ringwald          trap in an infinite loop */
67*fc456f6dSMatthias Ringwald     if(!MAP_FlashCtl_eraseSector(self->banks[bank]))
68*fc456f6dSMatthias Ringwald         while(1);
69*fc456f6dSMatthias Ringwald 
70*fc456f6dSMatthias Ringwald     /* Setting the sector back to protected  */
71*fc456f6dSMatthias Ringwald     MAP_FlashCtl_protectSector(FLASH_MAIN_MEMORY_SPACE_BANK1,self->sectors[bank]);
72*fc456f6dSMatthias Ringwald }
73*fc456f6dSMatthias Ringwald 
74*fc456f6dSMatthias Ringwald static void hal_flash_bank_msp432_read(void * context, int bank, uint32_t offset, uint8_t * buffer, uint32_t size){
75*fc456f6dSMatthias Ringwald 	hal_flash_bank_msp432_t * self = (hal_flash_bank_msp432_t *) context;
76*fc456f6dSMatthias Ringwald 
77*fc456f6dSMatthias Ringwald 	if (bank > 1) return;
78*fc456f6dSMatthias Ringwald 	if (offset > self->sector_size) return;
79*fc456f6dSMatthias Ringwald 	if ((offset + size) > self->sector_size) return;
80*fc456f6dSMatthias Ringwald 
81*fc456f6dSMatthias Ringwald 	memcpy(buffer, ((uint8_t *) self->banks[bank]) + offset, size);
82*fc456f6dSMatthias Ringwald }
83*fc456f6dSMatthias Ringwald 
84*fc456f6dSMatthias Ringwald static void hal_flash_bank_msp432_write(void * context, int bank, uint32_t offset, const uint8_t * data, uint32_t size){
85*fc456f6dSMatthias Ringwald 	hal_flash_bank_msp432_t * self = (hal_flash_bank_msp432_t *) context;
86*fc456f6dSMatthias Ringwald 
87*fc456f6dSMatthias Ringwald 	if (bank > 1) return;
88*fc456f6dSMatthias Ringwald 	if (offset > self->sector_size) return;
89*fc456f6dSMatthias Ringwald 	if ((offset + size) > self->sector_size) return;
90*fc456f6dSMatthias Ringwald 
91*fc456f6dSMatthias Ringwald     /* Unprotecting sector  */
92*fc456f6dSMatthias Ringwald     MAP_FlashCtl_unprotectSector(FLASH_MAIN_MEMORY_SPACE_BANK1,self->sectors[bank]);
93*fc456f6dSMatthias Ringwald 
94*fc456f6dSMatthias Ringwald     /* Trying to program the memory. Within this function, the API will
95*fc456f6dSMatthias Ringwald         automatically try to program the maximum number of tries. If it fails,
96*fc456f6dSMatthias Ringwald         trap inside an infinite loop */
97*fc456f6dSMatthias Ringwald     if(!MAP_FlashCtl_programMemory( (void *) data,
98*fc456f6dSMatthias Ringwald             (void*) ((uint8_t *) self->banks[bank]) + offset, size))
99*fc456f6dSMatthias Ringwald                 while(1);
100*fc456f6dSMatthias Ringwald 
101*fc456f6dSMatthias Ringwald     /* Setting the sector back to protected  */
102*fc456f6dSMatthias Ringwald     MAP_FlashCtl_protectSector(FLASH_MAIN_MEMORY_SPACE_BANK1,self->sectors[bank]);
103*fc456f6dSMatthias Ringwald }
104*fc456f6dSMatthias Ringwald 
105*fc456f6dSMatthias Ringwald static const hal_flash_bank_t hal_flash_bank_msp432_impl = {
106*fc456f6dSMatthias Ringwald 	/* uint32_t (*get_size)() */         &hal_flash_bank_msp432_get_size,
107*fc456f6dSMatthias Ringwald 	/* uint32_t (*get_alignment)(..); */ &hal_flash_bank_memory_msp432_get_alignment,
108*fc456f6dSMatthias Ringwald 	/* void (*erase)(..);             */ &hal_flash_bank_msp432_erase,
109*fc456f6dSMatthias Ringwald 	/* void (*read)(..);              */ &hal_flash_bank_msp432_read,
110*fc456f6dSMatthias Ringwald 	/* void (*write)(..);             */ &hal_flash_bank_msp432_write,
111*fc456f6dSMatthias Ringwald };
112*fc456f6dSMatthias Ringwald 
113*fc456f6dSMatthias Ringwald const hal_flash_bank_t * hal_flash_bank_msp432_init_instance(hal_flash_bank_msp432_t * context, uint32_t sector_size,
114*fc456f6dSMatthias Ringwald 		uint32_t bank_0_sector, uint32_t bank_1_sector, uintptr_t bank_0_addr, uintptr_t bank_1_addr){
115*fc456f6dSMatthias Ringwald 	context->sector_size = sector_size;
116*fc456f6dSMatthias Ringwald 	context->sectors[0] = bank_0_sector;
117*fc456f6dSMatthias Ringwald 	context->sectors[1] = bank_1_sector;
118*fc456f6dSMatthias Ringwald 	context->banks[0]   = bank_0_addr;
119*fc456f6dSMatthias Ringwald 	context->banks[1]   = bank_1_addr;
120*fc456f6dSMatthias Ringwald 	return &hal_flash_bank_msp432_impl;
121*fc456f6dSMatthias Ringwald }
122