Skip to content

Commit 42fce73

Browse files
committed
flash: add flash algo common layer
If target supports flash algo, it can get common HAL implementation, providing feature "HAL_FLASH_CMSIS_ALGO". This simplifies target code, and having one implementation that can be shared by many targets. Be careful with flash cmsis algo, in some cases they execute code that can affect the system. For instance, it can disable cache, or affect some system clocks. Therefore this feature should be well tested.
1 parent 665152d commit 42fce73

File tree

2 files changed

+252
-0
lines changed

2 files changed

+252
-0
lines changed
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2016 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "flash_api.h"
18+
#include "flash_data.h"
19+
#include "critical.h"
20+
21+
#define MBED_FLASH_ALGO_ERASE 1UL
22+
#define MBED_FLASH_ALGO_PROGRAM 2UL
23+
24+
extern uint32_t SystemCoreClock;
25+
26+
/*
27+
This binary blob (thumb code) sets r9 (static base) as the code we are jumping to
28+
is PIC (position independent code).
29+
30+
These are the instructions (r0 is a pointer to arg_t):
31+
push {r5, lr, r4}
32+
mov r5, r9
33+
push {r5}
34+
ldr r5, [r0, #20]
35+
ldr r3, [r0, #16]
36+
mov r9, r3
37+
ldr r3, [r0, #12]
38+
ldr r2, [r0, #8]
39+
ldr r1, [r0, #4]
40+
ldr r0, [r0, #0]
41+
blx r5
42+
pop {r5}
43+
mov r9, r5
44+
pop {r4-r5, pc}
45+
bx r14
46+
*/
47+
static uint32_t jump_to_flash_algo[] = {
48+
0x464DB530,
49+
0x6945B420,
50+
0x46996903,
51+
0x688268C3,
52+
0x68006841,
53+
0xBC2047A8,
54+
0xBD3046A9
55+
};
56+
57+
// should be called within critical section
58+
static int32_t flash_algo_init(flash_t *obj, uint32_t address, uint32_t function)
59+
{
60+
args_t arguments = {
61+
.r0 = address,
62+
.r1 = SystemCoreClock,
63+
.r2 = function,
64+
.r3 = 0,
65+
.r9 = (uint32_t)obj->flash_algo->algo_blob + obj->flash_algo->static_base,
66+
.pc = (uint32_t)obj->flash_algo->algo_blob + obj->flash_algo->init
67+
};
68+
return ((flash_algo_jump_t)(((uint32_t)&jump_to_flash_algo) | 1))(&arguments);
69+
}
70+
71+
// should be called within critical section
72+
static int32_t flash_algo_uninit(flash_t *obj, uint32_t address, uint32_t function)
73+
{
74+
args_t arguments = {
75+
.r0 = address,
76+
.r1 = SystemCoreClock,
77+
.r2 = function,
78+
.r3 = 0,
79+
.r9 = (uint32_t)obj->flash_algo->algo_blob + obj->flash_algo->static_base,
80+
.pc = (uint32_t)obj->flash_algo->algo_blob + obj->flash_algo->uninit
81+
};
82+
return ((flash_algo_jump_t)(((uint32_t)&jump_to_flash_algo) | 1))(&arguments);
83+
}
84+
85+
86+
int32_t flash_init(flash_t *obj)
87+
{
88+
flash_set_target_config(obj);
89+
return 0;
90+
}
91+
92+
int32_t flash_free(flash_t *obj)
93+
{
94+
return 0;
95+
}
96+
97+
int32_t flash_erase_sector(flash_t *obj, uint32_t address)
98+
{
99+
core_util_critical_section_enter();
100+
flash_algo_init(obj, address, MBED_FLASH_ALGO_ERASE);
101+
102+
args_t arguments = {
103+
.r0 = address,
104+
.r1 = 0,
105+
.r2 = 0,
106+
.r3 = 0,
107+
.r9 = (uint32_t)obj->flash_algo->algo_blob + obj->flash_algo->static_base,
108+
.pc = (uint32_t)obj->flash_algo->algo_blob + obj->flash_algo->erase_sector
109+
};
110+
int32_t ret = ((flash_algo_jump_t)(((uint32_t)&jump_to_flash_algo) | 1))(&arguments);
111+
112+
flash_algo_uninit(obj, address, MBED_FLASH_ALGO_ERASE);
113+
core_util_critical_section_exit();
114+
return ret ? -1 : 0;
115+
}
116+
117+
118+
int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data, uint32_t size)
119+
{
120+
core_util_critical_section_enter();
121+
flash_algo_init(obj, address, MBED_FLASH_ALGO_PROGRAM);
122+
123+
args_t arguments = {
124+
.r0 = address,
125+
.r1 = size,
126+
.r2 = (uint32_t)data,
127+
.r3 = 0,
128+
.r9 = (uint32_t)obj->flash_algo->algo_blob + obj->flash_algo->static_base,
129+
.pc = (uint32_t)obj->flash_algo->algo_blob + obj->flash_algo->program_page
130+
};
131+
int32_t ret = ((flash_algo_jump_t)(((uint32_t)&jump_to_flash_algo) | 1))(&arguments);
132+
133+
flash_algo_uninit(obj, address, MBED_FLASH_ALGO_PROGRAM);
134+
core_util_critical_section_exit();
135+
return ret ? -1 : 0;
136+
}
137+
138+
139+
uint32_t flash_get_sector_size(const flash_t *obj, uint32_t address)
140+
{
141+
const sector_info_t *sectors = obj->target_config->sectors;
142+
143+
if (address >= obj->target_config->flash_start + obj->target_config->flash_size) {
144+
return MBED_FLASH_INVALID_SIZE;
145+
}
146+
147+
int sector_index = obj->target_config->sector_info_count - 1;
148+
for (; sector_index >= 0; sector_index--) {
149+
if (address >= sectors[sector_index].start) {
150+
return sectors[sector_index].size;
151+
}
152+
}
153+
return MBED_FLASH_INVALID_SIZE;
154+
}
155+
156+
uint32_t flash_get_page_size(const flash_t *obj)
157+
{
158+
return obj->target_config->page_size;
159+
}
160+
161+
uint32_t flash_get_start_address(const flash_t *obj)
162+
{
163+
return obj->target_config->flash_start;
164+
}
165+
166+
uint32_t flash_get_size(const flash_t *obj)
167+
{
168+
return obj->target_config->flash_size;
169+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2016 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
#ifndef MBED_FLASH_DATA_H
17+
#define MBED_FLASH_DATA_H
18+
19+
#include <stdint.h>
20+
21+
// Target flash algorithm structure
22+
typedef struct {
23+
const uint32_t init;
24+
const uint32_t uninit;
25+
const uint32_t erase_sector;
26+
const uint32_t program_page;
27+
const uint32_t static_base;
28+
uint32_t *algo_blob;
29+
} flash_algo_t;
30+
31+
typedef struct {
32+
const uint32_t start;
33+
const uint32_t size;
34+
} sector_info_t;
35+
36+
typedef struct {
37+
const uint32_t page_size;
38+
const uint32_t flash_start;
39+
const uint32_t flash_size;
40+
const sector_info_t *sectors;
41+
const uint32_t sector_info_count;
42+
} flash_target_config_t;
43+
44+
// Target flash configuration
45+
struct flash_s {
46+
const flash_target_config_t *target_config;
47+
const flash_algo_t *flash_algo;
48+
};
49+
50+
typedef struct {
51+
uint32_t r0;
52+
uint32_t r1;
53+
uint32_t r2;
54+
uint32_t r3;
55+
uint32_t r9;
56+
uint32_t pc;
57+
} args_t;
58+
59+
typedef int32_t (*flash_algo_jump_t)(args_t*);
60+
61+
// prototypes for flash algo CMSIS API
62+
63+
typedef int (*CMSIS_Algo_Function_Init)(unsigned long adr, unsigned long clk, unsigned long fnc);
64+
typedef int (*CMSIS_Algo_Function_UnInit)(unsigned long fnc);
65+
typedef int (*CMSIS_Algo_Function_EraseSector)(unsigned long adr);
66+
typedef int (*CMSIS_Algo_Function_EraseChip)(void);
67+
typedef int (*CMSIS_Algo_Function_ProgramPage)(unsigned long adr, unsigned long sz, unsigned char *buf);
68+
typedef unsigned long (*CMSIS_Algo_Function_Verify)(unsigned long adr, unsigned long sz, unsigned char *buf);
69+
70+
#ifdef __cplusplus
71+
extern "C" {
72+
#endif
73+
74+
/* Set target configuration
75+
*/
76+
void flash_set_target_config(flash_t *obj);
77+
78+
#ifdef __cplusplus
79+
};
80+
#endif
81+
82+
83+
#endif

0 commit comments

Comments
 (0)