Skip to content

Commit 64c6214

Browse files
authored
Merge pull request #4474 from kegilbert/stm32_l0_flash_api-rebase
STM32 L0: Add Flash API support - Rebase
2 parents 1607c83 + affab79 commit 64c6214

File tree

3 files changed

+153
-5
lines changed

3 files changed

+153
-5
lines changed

targets/TARGET_STM/TARGET_STM32L0/common_objects.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,11 @@ struct i2c_s {
111111
#endif
112112
};
113113

114+
struct flash_s {
115+
/* nothing to be stored for now */
116+
uint32_t dummy;
117+
};
118+
114119
#include "gpio_object.h"
115120

116121
#ifdef __cplusplus
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2017 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 "mbed_critical.h"
19+
20+
#if DEVICE_FLASH
21+
#include "mbed_assert.h"
22+
#include "cmsis.h"
23+
24+
/* L0 targets embed 32 pages sectors */
25+
#define NUM_PAGES_IN_SECTOR 32
26+
27+
int32_t flash_init(flash_t *obj)
28+
{
29+
/* Unlock the Flash to enable the flash control register access *************/
30+
HAL_FLASH_Unlock();
31+
return 0;
32+
}
33+
34+
int32_t flash_free(flash_t *obj)
35+
{
36+
/* Lock the Flash to disable the flash control register access (recommended
37+
* to protect the FLASH memory against possible unwanted operation) *********/
38+
HAL_FLASH_Lock();
39+
return 0;
40+
}
41+
42+
int32_t flash_erase_sector(flash_t *obj, uint32_t address)
43+
{
44+
uint32_t FirstPage = 0;
45+
uint32_t PAGEError = 0;
46+
FLASH_EraseInitTypeDef EraseInitStruct;
47+
48+
if ((address >= (FLASH_BASE + FLASH_SIZE)) || (address < FLASH_BASE)) {
49+
50+
return -1;
51+
}
52+
53+
/* Clear OPTVERR bit set on virgin samples */
54+
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_OPTVERR);
55+
56+
/* MBED HAL erases 1 sector at a time */
57+
/* Fill EraseInit structure*/
58+
EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
59+
EraseInitStruct.PageAddress = address;
60+
EraseInitStruct.NbPages = NUM_PAGES_IN_SECTOR;
61+
62+
/* Note: If an erase operation in Flash memory also concerns data in the data or instruction cache,
63+
you have to make sure that these data are rewritten before they are accessed during code
64+
execution. If this cannot be done safely, it is recommended to flush the caches by setting the
65+
DCRST and ICRST bits in the FLASH_CR register. */
66+
67+
if (HAL_FLASHEx_Erase(&EraseInitStruct, &PAGEError) != HAL_OK) {
68+
return -1;
69+
} else {
70+
return 0;
71+
}
72+
}
73+
74+
int32_t flash_program_page(flash_t *obj, uint32_t address,
75+
const uint8_t *data, uint32_t size)
76+
{
77+
uint32_t StartAddress = 0;
78+
79+
if ((address >= (FLASH_BASE + FLASH_SIZE)) || (address < FLASH_BASE)) {
80+
return -1;
81+
}
82+
83+
if ((size % 4) != 0) {
84+
/* L4 flash devices can only be programmed 64bits/8 bytes at a time */
85+
return -1;
86+
}
87+
88+
/* Program the user Flash area word by word */
89+
StartAddress = address;
90+
91+
/* HW needs an aligned address to program flash, which data
92+
* parameters doesn't ensure */
93+
if ((uint32_t) data % 4 != 0) {
94+
volatile uint32_t data32;
95+
while (address < (StartAddress + size)) {
96+
for (uint8_t i =0; i < 4; i++) {
97+
*(((uint8_t *) &data32) + i) = *(data + i);
98+
}
99+
100+
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, address, data32) == HAL_OK) {
101+
address = address + 4;
102+
data = data + 4;
103+
} else {
104+
return -1;
105+
}
106+
}
107+
} else { /* case where data is aligned, so let's avoid any copy */
108+
while (address < (StartAddress + size)) {
109+
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, address, *((uint32_t*) data)) == HAL_OK) {
110+
address = address + 4;
111+
data = data + 4;
112+
} else {
113+
return -1;
114+
}
115+
}
116+
}
117+
118+
return 0;
119+
}
120+
121+
uint32_t flash_get_sector_size(const flash_t *obj, uint32_t address) {
122+
/* considering 1 sector = 1 page */
123+
if ((address >= (FLASH_BASE + FLASH_SIZE)) || (address < FLASH_BASE)) {
124+
return MBED_FLASH_INVALID_SIZE;
125+
} else {
126+
return (NUM_PAGES_IN_SECTOR * FLASH_PAGE_SIZE);
127+
}
128+
}
129+
130+
uint32_t flash_get_page_size(const flash_t *obj) {
131+
/* considering 1 sector = 1 page */
132+
return FLASH_PAGE_SIZE;
133+
}
134+
135+
uint32_t flash_get_start_address(const flash_t *obj) {
136+
return FLASH_BASE;
137+
}
138+
139+
uint32_t flash_get_size(const flash_t *obj) {
140+
return FLASH_SIZE;
141+
}
142+
143+
#endif

targets/targets.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,7 +1059,7 @@
10591059
"default_toolchain": "uARM",
10601060
"supported_form_factors": ["ARDUINO"],
10611061
"detect_code": ["0780"],
1062-
"device_has": ["ANALOGIN", "I2C", "I2CSLAVE", "I2C_ASYNCH", "INTERRUPTIN", "LOWPOWERTIMER", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "SPI_ASYNCH", "STDIO_MESSAGES"],
1062+
"device_has": ["ANALOGIN", "I2C", "I2CSLAVE", "I2C_ASYNCH", "INTERRUPTIN", "LOWPOWERTIMER", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "SPI_ASYNCH", "STDIO_MESSAGES", "FLASH"],
10631063
"default_lib": "small",
10641064
"release_versions": ["2"],
10651065
"device_name": "STM32L011K4"
@@ -1072,7 +1072,7 @@
10721072
"default_toolchain": "uARM",
10731073
"supported_form_factors": ["ARDUINO"],
10741074
"detect_code": ["0790"],
1075-
"device_has": ["ANALOGIN", "I2C", "I2CSLAVE", "I2C_ASYNCH", "INTERRUPTIN", "LOWPOWERTIMER", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "SPI_ASYNCH", "STDIO_MESSAGES"],
1075+
"device_has": ["ANALOGIN", "I2C", "I2CSLAVE", "I2C_ASYNCH", "INTERRUPTIN", "LOWPOWERTIMER", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "SPI_ASYNCH", "STDIO_MESSAGES", "FLASH"],
10761076
"default_lib": "small",
10771077
"release_versions": ["2"],
10781078
"device_name": "STM32L031K6"
@@ -1085,7 +1085,7 @@
10851085
"supported_toolchains": ["ARM", "uARM", "GCC_ARM", "IAR"],
10861086
"inherits": ["Target"],
10871087
"detect_code": ["0715"],
1088-
"device_has": ["ANALOGIN", "ANALOGOUT", "I2C", "I2CSLAVE", "I2C_ASYNCH", "INTERRUPTIN", "LOWPOWERTIMER", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_FC", "SERIAL_ASYNCH", "SLEEP", "SPI", "SPISLAVE", "SPI_ASYNCH", "STDIO_MESSAGES"],
1088+
"device_has": ["ANALOGIN", "ANALOGOUT", "I2C", "I2CSLAVE", "I2C_ASYNCH", "INTERRUPTIN", "LOWPOWERTIMER", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_FC", "SERIAL_ASYNCH", "SLEEP", "SPI", "SPISLAVE", "SPI_ASYNCH", "STDIO_MESSAGES", "FLASH"],
10891089
"default_lib": "small",
10901090
"release_versions": ["2"],
10911091
"device_name": "STM32L053R8"
@@ -1098,7 +1098,7 @@
10981098
"supported_toolchains": ["ARM", "uARM", "GCC_ARM", "IAR"],
10991099
"inherits": ["Target"],
11001100
"detect_code": ["0760"],
1101-
"device_has": ["ANALOGIN", "ANALOGOUT", "I2C", "I2CSLAVE", "I2C_ASYNCH", "INTERRUPTIN", "LOWPOWERTIMER", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_FC", "SERIAL_ASYNCH", "SLEEP", "SPI", "SPISLAVE", "SPI_ASYNCH", "STDIO_MESSAGES", "TRNG"],
1101+
"device_has": ["ANALOGIN", "ANALOGOUT", "I2C", "I2CSLAVE", "I2C_ASYNCH", "INTERRUPTIN", "LOWPOWERTIMER", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_FC", "SERIAL_ASYNCH", "SLEEP", "SPI", "SPISLAVE", "SPI_ASYNCH", "STDIO_MESSAGES", "TRNG", "FLASH"],
11021102
"release_versions": ["2", "5"],
11031103
"device_name": "STM32L073RZ"
11041104
},
@@ -1262,7 +1262,7 @@
12621262
"extra_labels": ["STM", "STM32L0", "STM32L053C8"],
12631263
"supported_toolchains": ["ARM", "uARM", "IAR", "GCC_ARM"],
12641264
"macros": ["RTC_LSI=1"],
1265-
"device_has": ["ANALOGIN", "ANALOGOUT", "I2C", "I2CSLAVE", "INTERRUPTIN", "LOWPOWERTIMER", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "SPI_ASYNCH", "STDIO_MESSAGES"],
1265+
"device_has": ["ANALOGIN", "ANALOGOUT", "I2C", "I2CSLAVE", "INTERRUPTIN", "LOWPOWERTIMER", "PORTIN", "PORTINOUT", "PORTOUT", "PWMOUT", "RTC", "SERIAL", "SERIAL_FC", "SLEEP", "SPI", "SPISLAVE", "SPI_ASYNCH", "STDIO_MESSAGES", "FLASH"],
12661266
"default_lib": "small",
12671267
"release_versions": ["2"],
12681268
"device_name": "STM32L053C8"

0 commit comments

Comments
 (0)