forked from zephyrproject-rtos/zephyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflash_stm32f4x.c
112 lines (87 loc) · 2.08 KB
/
flash_stm32f4x.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
* Copyright (c) 2017 Linaro Limited
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <kernel.h>
#include <device.h>
#include <string.h>
#include <flash.h>
#include <init.h>
#include <soc.h>
#include <flash_stm32.h>
bool flash_stm32_valid_range(off_t offset, u32_t len, bool write)
{
ARG_UNUSED(write);
return offset >= 0 && (offset + len - 1 <= STM32F4X_FLASH_END);
}
static int write_byte(off_t offset, u8_t val, struct flash_stm32_priv *p)
{
struct stm32f4x_flash *regs = p->regs;
u32_t tmp;
int rc;
/* if the control register is locked, do not fail silently */
if (regs->cr & FLASH_CR_LOCK) {
return -EIO;
}
rc = flash_stm32_wait_flash_idle(p);
if (rc < 0) {
return rc;
}
regs->cr &= ~CR_PSIZE_MASK;
regs->cr |= FLASH_PSIZE_BYTE;
regs->cr |= FLASH_CR_PG;
/* flush the register write */
tmp = regs->cr;
*((u8_t *) offset + CONFIG_FLASH_BASE_ADDRESS) = val;
rc = flash_stm32_wait_flash_idle(p);
regs->cr &= (~FLASH_CR_PG);
return rc;
}
static int erase_sector(u16_t sector, struct flash_stm32_priv *p)
{
struct stm32f4x_flash *regs = p->regs;
u32_t tmp;
int rc;
/* if the control register is locked, do not fail silently */
if (regs->cr & FLASH_CR_LOCK) {
return -EIO;
}
rc = flash_stm32_wait_flash_idle(p);
if (rc < 0) {
return rc;
}
regs->cr &= STM32F4X_SECTOR_MASK;
regs->cr |= FLASH_CR_SER | (sector << 3);
regs->cr |= FLASH_CR_STRT;
/* flush the register write */
tmp = regs->cr;
rc = flash_stm32_wait_flash_idle(p);
regs->cr &= ~(FLASH_CR_SER | FLASH_CR_SNB);
return rc;
}
int flash_stm32_block_erase_loop(unsigned int offset, unsigned int len,
struct flash_stm32_priv *p)
{
int i, rc = 0;
i = stm32f4x_get_sector(offset);
for (; i <= stm32f4x_get_sector(offset + len - 1); i++) {
rc = erase_sector(i, p);
if (rc < 0) {
break;
}
}
return rc;
}
int flash_stm32_write_range(unsigned int offset, const void *data,
unsigned int len, struct flash_stm32_priv *p)
{
int i, rc = 0;
for (i = 0; i < len; i++, offset++) {
rc = write_byte(offset, ((const u8_t *) data)[i], p);
if (rc < 0) {
return rc;
}
}
return rc;
}