Skip to content

Commit b662f96

Browse files
committed
stm32wb: Fixed alignment problem in hal_write, + bug in PNB mask
1 parent 35545ff commit b662f96

1 file changed

Lines changed: 34 additions & 26 deletions

File tree

hal/stm32wb.c

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ PKA_HandleTypeDef hpka = { };
9797
#define FLASH_ACR_LATENCY_MASK (0x07)
9898

9999
#ifndef WOLFSSL_STM32_PKA
100-
#define FLASH_SR_BSY (1 << 16)
100+
#define FLASH_SR_BSY (1 << 16)
101+
#define FLASH_SR_CFGBSY (1 << 18)
101102
#define FLASH_SR_SIZERR (1 << 6)
102103
#define FLASH_SR_PGAERR (1 << 5)
103104
#define FLASH_SR_WRPERR (1 << 4)
@@ -114,7 +115,7 @@ PKA_HandleTypeDef hpka = { };
114115
#endif /* !WOLFSSL_STM32_PKA */
115116

116117
#define FLASH_CR_PNB_SHIFT 3
117-
#define FLASH_CR_PNB_MASK 0x3f
118+
#define FLASH_CR_PNB_MASK 0xFF
118119

119120
#define FLASH_KEY1 (0x45670123)
120121
#define FLASH_KEY2 (0xCDEF89AB)
@@ -129,7 +130,7 @@ static void RAMFUNCTION flash_set_waitstates(unsigned int waitstates)
129130

130131
static RAMFUNCTION void flash_wait_complete(void)
131132
{
132-
while ((FLASH_SR & FLASH_SR_BSY) == FLASH_SR_BSY)
133+
while ((FLASH_SR & (FLASH_SR_BSY | FLASH_SR_CFGBSY)) != 0)
133134
;
134135
}
135136

@@ -138,11 +139,35 @@ static void RAMFUNCTION flash_clear_errors(void)
138139
FLASH_SR |= ( FLASH_SR_SIZERR | FLASH_SR_PGAERR | FLASH_SR_WRPERR | FLASH_SR_PROGERR);
139140
}
140141

142+
143+
144+
void RAMFUNCTION hal_flash_unlock(void)
145+
{
146+
flash_wait_complete();
147+
if ((FLASH_CR & FLASH_CR_LOCK) != 0) {
148+
FLASH_KEY = FLASH_KEY1;
149+
DMB();
150+
FLASH_KEY = FLASH_KEY2;
151+
DMB();
152+
while ((FLASH_CR & FLASH_CR_LOCK) != 0)
153+
;
154+
}
155+
}
156+
157+
void RAMFUNCTION hal_flash_lock(void)
158+
{
159+
flash_wait_complete();
160+
if ((FLASH_CR & FLASH_CR_LOCK) == 0)
161+
FLASH_CR |= FLASH_CR_LOCK;
162+
}
163+
141164
int RAMFUNCTION hal_flash_write(uint32_t address, const uint8_t *data, int len)
142165
{
143166
int i = 0;
144167
uint32_t *src, *dst;
168+
uint32_t pdword[2] __attribute__((aligned(16)));
145169
uint32_t reg;
170+
146171
flash_clear_errors();
147172
reg = FLASH_CR & (~FLASH_CR_FSTPG);
148173
FLASH_CR = reg | FLASH_CR_PG;
@@ -153,9 +178,11 @@ int RAMFUNCTION hal_flash_write(uint32_t address, const uint8_t *data, int len)
153178
uint32_t idx = i >> 2;
154179
src = (uint32_t *)data;
155180
dst = (uint32_t *)(address);
181+
pdword[0] = src[idx];
182+
pdword[1] = src[idx + 1];
156183
flash_wait_complete();
157-
dst[idx] = src[idx];
158-
dst[idx + 1] = src[idx + 1];
184+
dst[idx] = pdword[0];
185+
dst[idx + 1] = pdword[1];
159186
flash_wait_complete();
160187
i+=8;
161188
} else {
@@ -180,43 +207,24 @@ int RAMFUNCTION hal_flash_write(uint32_t address, const uint8_t *data, int len)
180207
return 0;
181208
}
182209

183-
void RAMFUNCTION hal_flash_unlock(void)
184-
{
185-
flash_wait_complete();
186-
if ((FLASH_CR & FLASH_CR_LOCK) != 0) {
187-
FLASH_KEY = FLASH_KEY1;
188-
DMB();
189-
FLASH_KEY = FLASH_KEY2;
190-
DMB();
191-
while ((FLASH_CR & FLASH_CR_LOCK) != 0)
192-
;
193-
}
194-
}
195-
196-
void RAMFUNCTION hal_flash_lock(void)
197-
{
198-
flash_wait_complete();
199-
if ((FLASH_CR & FLASH_CR_LOCK) == 0)
200-
FLASH_CR |= FLASH_CR_LOCK;
201-
}
202-
203210

204211
int RAMFUNCTION hal_flash_erase(uint32_t address, int len)
205212
{
206-
int start = -1, end = -1;
207213
uint32_t end_address;
208214
uint32_t p;
209215
if (len == 0)
210216
return -1;
211217
address -= FLASHMEM_ADDRESS_SPACE;
212218
end_address = address + len - 1;
219+
flash_wait_complete();
213220
for (p = address; p < end_address; p += FLASH_PAGE_SIZE) {
214221
uint32_t reg;
215222
flash_clear_errors();
216223
reg = FLASH_CR & ~((FLASH_CR_PNB_MASK << FLASH_CR_PNB_SHIFT) | FLASH_CR_FSTPG | FLASH_CR_PG);
217224
FLASH_CR = reg | ((p >> 12) << FLASH_CR_PNB_SHIFT) | FLASH_CR_PER;
218225
DMB();
219226
FLASH_CR |= FLASH_CR_STRT;
227+
DMB();
220228
flash_wait_complete();
221229
FLASH_CR &= ~(FLASH_CR_PER);
222230
}

0 commit comments

Comments
 (0)