Skip to content

Commit d354657

Browse files
Add STM32.flashErase()/STM32.flashProgram() for OTA; move USBD to use timer based USB_VBUS detection
1 parent 9f8689e commit d354657

File tree

21 files changed

+266
-152
lines changed

21 files changed

+266
-152
lines changed

cores/stm32l4/STM32.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,43 @@ void STM32Class::reset()
196196
stm32l4_system_reset();
197197
}
198198

199+
bool STM32Class::flashErase(uint32_t address)
200+
{
201+
if (address & 2047) {
202+
return false;
203+
}
204+
205+
if ((address < FLASHSTART) || (address >= FLASHEND)) {
206+
return false;
207+
}
208+
209+
stm32l4_flash_unlock();
210+
stm32l4_flash_erase(address, 1);
211+
stm32l4_flash_lock();
212+
213+
return true;
214+
}
215+
216+
bool STM32Class::flashProgram(uint32_t address, const void *data, uint32_t count)
217+
{
218+
if ((address & 7) || (count & 7)) {
219+
return false;
220+
}
221+
222+
if ((address < FLASHSTART) || ((address + count) > FLASHEND)) {
223+
return false;
224+
}
225+
226+
if (count)
227+
{
228+
stm32l4_flash_unlock();
229+
stm32l4_flash_program(address, (const uint8_t*)data, count);
230+
stm32l4_flash_lock();
231+
}
232+
233+
return true;
234+
}
235+
199236
void STM32Class::lsco(bool enable)
200237
{
201238
stm32l4_system_lsco_configure((enable ? SYSTEM_LSCO_MODE_LSE : SYSTEM_LSCO_MODE_NONE));

cores/stm32l4/STM32.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@
5050
#define WAKEUP_SYNC 0x00000400
5151
#define WAKEUP_TIMEOUT 0x00000800
5252

53+
#define FLASHSTART ((uint32_t)(&__FlashBase))
54+
#define FLASHEND ((uint32_t)(&__FlashLimit))
55+
5356
class STM32Class {
5457
public:
5558
uint64_t getSerial();
@@ -69,6 +72,9 @@ class STM32Class {
6972
void shutdown(uint32_t pin, uint32_t mode, uint32_t timeout = 0);
7073
void reset();
7174

75+
bool flashErase(uint32_t address);
76+
bool flashProgram(uint32_t address, const void *data, uint32_t count);
77+
7278
void lsco(bool enable);
7379
};
7480

cores/stm32l4/avr/eeprom.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ static void eeprom_flash_initialize(void)
113113
wdata[14] = (uint8_t)(EEPROM_FLASH_MAGIC_2 >> 16);
114114
wdata[15] = (uint8_t)(EEPROM_FLASH_MAGIC_2 >> 24);
115115

116-
stm32l4_flash_program((uint32_t)eeprom_flash_data + 1*EEPROM_FLASH_BANK - 16, 16, &wdata[0]);
116+
stm32l4_flash_program((uint32_t)eeprom_flash_data + 1*EEPROM_FLASH_BANK - 16, &wdata[0], 16);
117117

118118
stm32l4_flash_lock();
119119

@@ -208,7 +208,7 @@ static void eeprom_flash_write(uint32_t offset, const uint8_t *data)
208208
wdata[6] = 0x00;
209209
wdata[7] = 0x00;
210210

211-
stm32l4_flash_program((uint32_t)eeprom_flash_data + eeprom_flash_slot, 8, &wdata[0]);
211+
stm32l4_flash_program((uint32_t)eeprom_flash_data + eeprom_flash_slot, &wdata[0], 8);
212212

213213
eeprom_flash_slot += 8;
214214
}
@@ -244,7 +244,7 @@ static void eeprom_flash_write(uint32_t offset, const uint8_t *data)
244244
eeprom_flash_read(soffset +4, &wdata[4]);
245245
}
246246

247-
stm32l4_flash_program((uint32_t)eeprom_flash_data +sbottom +soffset, 8, &wdata[0]);
247+
stm32l4_flash_program((uint32_t)eeprom_flash_data +sbottom +soffset, &wdata[0], 8);
248248
}
249249

250250
sequence = ((eeprom_flash_data[eeprom_flash_limit +0] << 0) |
@@ -271,7 +271,7 @@ static void eeprom_flash_write(uint32_t offset, const uint8_t *data)
271271
wdata[14] = (uint8_t)(EEPROM_FLASH_MAGIC_2 >> 16);
272272
wdata[15] = (uint8_t)(EEPROM_FLASH_MAGIC_2 >> 24);
273273

274-
stm32l4_flash_program((uint32_t)eeprom_flash_data +sbottom +EEPROM_FLASH_BANK - 16, 16, &wdata[0]);
274+
stm32l4_flash_program((uint32_t)eeprom_flash_data +sbottom +EEPROM_FLASH_BANK - 16, &wdata[0], 16);
275275

276276
eeprom_flash_bottom = sbottom;
277277
eeprom_flash_top = sbottom + EEPROM_FLASH_SIZE;

cores/stm32l4/stm32l4_wiring.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ extern "C" {
3737

3838
#include "armv7m.h"
3939

40-
#define retained __attribute__((section(".databkp")))
40+
#define retained __attribute__((section(".backup")))
4141

4242
static inline void interrupts(void)
4343
{
@@ -181,6 +181,9 @@ extern uint32_t SystemCoreClock;
181181

182182
#define F_CPU SystemCoreClock
183183

184+
extern uint32_t __FlashBase;
185+
extern uint32_t __FlashLimit;
186+
184187
#ifdef __cplusplus
185188
} // extern "C"
186189
#endif

cores/stm32l4/stm32l4_wiring_private.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ extern "C" {
4848
#include "stm32l4_system.h"
4949
#include "stm32l4_rtc.h"
5050
#include "stm32l4_sai.h"
51+
#include "stm32l4_flash.h"
5152

5253
#define STM32L4_SVCALL_IRQ_PRIORITY 15
5354
#define STM32L4_PENDSV_IRQ_PRIORITY 15

system/STM32L4xx/Include/armv7m_timer.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ extern void armv7m_timer_create(armv7m_timer_t *timer, armv7m_timer_callback_t c
5353
extern bool armv7m_timer_start(armv7m_timer_t *timer, uint32_t timeout);
5454
extern bool armv7m_timer_stop(armv7m_timer_t *timer);
5555

56-
extern void armv7m_timer_heartbeat(uint32_t millis);
5756
extern void armv7m_timer_initialize(void);
5857

5958
#ifdef __cplusplus

system/STM32L4xx/Include/stm32l4_flash.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ extern uint32_t stm32l4_flash_size(void);
4141
extern bool stm32l4_flash_unlock(void);
4242
extern void stm32l4_flash_lock(void);
4343
extern bool stm32l4_flash_erase(uint32_t address, uint32_t count);
44-
extern bool stm32l4_flash_program(uint32_t address, uint32_t count, const uint8_t *data);
44+
extern bool stm32l4_flash_program(uint32_t address, const uint8_t *data, uint32_t count);
4545

4646
#ifdef __cplusplus
4747
}

system/STM32L4xx/Include/stm32l4_system.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ extern uint32_t stm32l4_system_hclk(void);
234234
extern uint32_t stm32l4_system_pclk1(void);
235235
extern uint32_t stm32l4_system_pclk2(void);
236236
extern uint32_t stm32l4_system_saiclk(void);
237-
extern void stm32l4_system_notify(uint32_t slot, stm32l4_system_callback_t callback, void *context, uint32_t events);
237+
extern int stm32l4_system_notify(int slot, stm32l4_system_callback_t callback, void *context, uint32_t events);
238238
extern void stm32l4_system_lock(uint32_t lock);
239239
extern void stm32l4_system_unlock(uint32_t lock);
240240
extern bool stm32l4_system_stop(uint32_t timeout);
-1.23 KB
Binary file not shown.
-1.23 KB
Binary file not shown.

0 commit comments

Comments
 (0)