Skip to content

Commit e23d16c

Browse files
committed
implement support to software reboot into the system bootloader for flashing (using stm32flash);
1 parent 40f9083 commit e23d16c

File tree

6 files changed

+102
-0
lines changed

6 files changed

+102
-0
lines changed

FirmwareMain.cpp

+61
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ IO io;
7373

7474
void setup()
7575
{
76+
io.init();
77+
7678
serial.start();
7779
}
7880

@@ -121,9 +123,68 @@ void loop()
121123
// ---------------------------------------------------------------------------
122124
// Firmware Entry Point
123125
// ---------------------------------------------------------------------------
126+
#include <stm32f10x_flash.h>
127+
128+
#define STM32_CNF_PAGE_ADDR (uint32_t)0x0800FC00
129+
#define STM32_CNF_PAGE ((uint32_t *)0x0800FC00)
130+
#define STM32_CNF_PAGE_24 24U
131+
132+
void jumpToBootLoader()
133+
{
134+
// Disable RCC, set it to default (after reset) settings Internal clock, no PLL, etc.
135+
RCC_DeInit();
136+
USART_DeInit(USART1);
137+
USART_DeInit(UART5);
138+
139+
// Disable Systick timer
140+
SysTick->CTRL = 0;
141+
SysTick->LOAD = 0;
142+
SysTick->VAL = 0;
143+
144+
// Clear Interrupt Enable Register & Interrupt Pending Register
145+
for (uint8_t i = 0; i < sizeof(NVIC->ICER) / sizeof(NVIC->ICER[0]); i++) {
146+
NVIC->ICER[i] = 0xFFFFFFFF;
147+
NVIC->ICPR[i] = 0xFFFFFFFF;
148+
}
149+
150+
#if defined(STM32F10X_MD)
151+
volatile uint32_t addr = 0x1FFFF000;
152+
#endif
153+
154+
// Update the NVIC's vector
155+
SCB->VTOR = addr;
156+
157+
void (*SysMemBootJump)(void);
158+
SysMemBootJump = (void (*)(void))(*((uint32_t *)(addr + 4)));
159+
__ASM volatile ("MSR msp, %0" : : "r" (*(uint32_t *)addr) : "sp"); // __set_MSP
160+
SysMemBootJump();
161+
}
124162

125163
int main()
126164
{
165+
// does the configuration page contain the request bootloader flag?
166+
if ((STM32_CNF_PAGE[STM32_CNF_PAGE_24] != 0xFFFFFFFFU) && (STM32_CNF_PAGE[STM32_CNF_PAGE_24] != 0x00U)) {
167+
uint8_t bootloadMode = (STM32_CNF_PAGE[STM32_CNF_PAGE_24] >> 8) & 0xFFU;
168+
if ((bootloadMode & 0x20U) == 0x20U) {
169+
// we unfortunately need to discard the configuration area entirely for bootloader mode...
170+
FLASH_Unlock();
171+
FLASH_ClearFlag(FLASH_FLAG_BSY | FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR);
172+
173+
#if defined(STM32F4XX) || defined(STM32F7XX)
174+
if (FLASH_EraseSector(STM32_CNF_SECTOR, VoltageRange_3) != FLASH_COMPLETE) {
175+
FLASH_Lock();
176+
return RSN_FAILED_ERASE_FLASH;
177+
}
178+
#elif defined(STM32F10X_MD)
179+
if (FLASH_ErasePage(STM32_CNF_PAGE_ADDR) != FLASH_COMPLETE) {
180+
FLASH_Lock();
181+
return RSN_FAILED_ERASE_FLASH;
182+
}
183+
#endif
184+
jumpToBootLoader();
185+
}
186+
}
187+
127188
setup();
128189

129190
for (;;)

IO.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ IO::IO():
3333
m_txFrequency(DEFAULT_FREQUENCY),
3434
m_rfPower(0U),
3535
m_gainMode(ADF_GAIN_AUTO)
36+
{
37+
/* stub */
38+
}
39+
40+
/* Initializes the air interface sampler. */
41+
42+
void IO::init()
3643
{
3744
initInt();
3845

IO.h

+9
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ class DSP_FW_API IO {
6262
*/
6363
IO();
6464

65+
/**
66+
* @brief Initializes the air interface sampler.
67+
*/
68+
void init();
6569
/**
6670
* @brief Starts air interface sampler.
6771
*/
@@ -213,6 +217,11 @@ class DSP_FW_API IO {
213217
*/
214218
void selfTest();
215219

220+
/**
221+
* @brief
222+
*/
223+
void resetMCU();
224+
216225
/**
217226
* @brief
218227
* @param[out] int1

IOSTM.cpp

+19
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,25 @@ void IO::getUDID(uint8_t* buffer)
338338

339339
/* */
340340

341+
void IO::resetMCU()
342+
{
343+
DEBUG1("reset - bye-bye");
344+
345+
delayUS(250 * 1000);
346+
347+
setLEDInt(false);
348+
setCOSInt(false);
349+
setDMRInt(false);
350+
setP25Int(false);
351+
setNXDNInt(false);
352+
353+
delayUS(250 * 1000);
354+
355+
NVIC_SystemReset();
356+
}
357+
358+
/* */
359+
341360
void IO::delayBit()
342361
{
343362
delay_ns();

SerialPort.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,10 @@ void SerialPort::process()
178178
}
179179
break;
180180

181+
case CMD_RESET_MCU:
182+
io.resetMCU();
183+
break;
184+
181185
case CMD_SET_BUFFERS:
182186
err = setBuffers(m_buffer + 3U, m_len - 3U);
183187
if (err == RSN_OK) {

SerialPort.h

+2
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ enum DVM_COMMANDS {
109109
CMD_FLSH_READ = 0xE0U, //! Read Flash Partition
110110
CMD_FLSH_WRITE = 0xE1U, //! Write Flash Partition
111111

112+
CMD_RESET_MCU = 0xEAU, //! Soft Reboot MCU
113+
112114
CMD_DEBUG1 = 0xF1U, //!
113115
CMD_DEBUG2 = 0xF2U, //!
114116
CMD_DEBUG3 = 0xF3U, //!

0 commit comments

Comments
 (0)