Skip to content

Commit

Permalink
implement support to software reboot into the system bootloader for f…
Browse files Browse the repository at this point in the history
…lashing (using stm32flash);
  • Loading branch information
gatekeep committed Aug 1, 2024
1 parent 40f9083 commit e23d16c
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 0 deletions.
61 changes: 61 additions & 0 deletions FirmwareMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ IO io;

void setup()
{
io.init();

serial.start();
}

Expand Down Expand Up @@ -121,9 +123,68 @@ void loop()
// ---------------------------------------------------------------------------
// Firmware Entry Point
// ---------------------------------------------------------------------------
#include <stm32f10x_flash.h>

#define STM32_CNF_PAGE_ADDR (uint32_t)0x0800FC00
#define STM32_CNF_PAGE ((uint32_t *)0x0800FC00)
#define STM32_CNF_PAGE_24 24U

void jumpToBootLoader()
{
// Disable RCC, set it to default (after reset) settings Internal clock, no PLL, etc.
RCC_DeInit();
USART_DeInit(USART1);
USART_DeInit(UART5);

// Disable Systick timer
SysTick->CTRL = 0;
SysTick->LOAD = 0;
SysTick->VAL = 0;

// Clear Interrupt Enable Register & Interrupt Pending Register
for (uint8_t i = 0; i < sizeof(NVIC->ICER) / sizeof(NVIC->ICER[0]); i++) {
NVIC->ICER[i] = 0xFFFFFFFF;
NVIC->ICPR[i] = 0xFFFFFFFF;
}

#if defined(STM32F10X_MD)
volatile uint32_t addr = 0x1FFFF000;
#endif

// Update the NVIC's vector
SCB->VTOR = addr;

void (*SysMemBootJump)(void);
SysMemBootJump = (void (*)(void))(*((uint32_t *)(addr + 4)));
__ASM volatile ("MSR msp, %0" : : "r" (*(uint32_t *)addr) : "sp"); // __set_MSP
SysMemBootJump();
}

int main()
{
// does the configuration page contain the request bootloader flag?
if ((STM32_CNF_PAGE[STM32_CNF_PAGE_24] != 0xFFFFFFFFU) && (STM32_CNF_PAGE[STM32_CNF_PAGE_24] != 0x00U)) {
uint8_t bootloadMode = (STM32_CNF_PAGE[STM32_CNF_PAGE_24] >> 8) & 0xFFU;
if ((bootloadMode & 0x20U) == 0x20U) {
// we unfortunately need to discard the configuration area entirely for bootloader mode...
FLASH_Unlock();
FLASH_ClearFlag(FLASH_FLAG_BSY | FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR);

#if defined(STM32F4XX) || defined(STM32F7XX)
if (FLASH_EraseSector(STM32_CNF_SECTOR, VoltageRange_3) != FLASH_COMPLETE) {
FLASH_Lock();
return RSN_FAILED_ERASE_FLASH;
}
#elif defined(STM32F10X_MD)
if (FLASH_ErasePage(STM32_CNF_PAGE_ADDR) != FLASH_COMPLETE) {
FLASH_Lock();
return RSN_FAILED_ERASE_FLASH;
}
#endif
jumpToBootLoader();
}
}

setup();

for (;;)
Expand Down
7 changes: 7 additions & 0 deletions IO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ IO::IO():
m_txFrequency(DEFAULT_FREQUENCY),
m_rfPower(0U),
m_gainMode(ADF_GAIN_AUTO)
{
/* stub */
}

/* Initializes the air interface sampler. */

void IO::init()
{
initInt();

Expand Down
9 changes: 9 additions & 0 deletions IO.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ class DSP_FW_API IO {
*/
IO();

/**
* @brief Initializes the air interface sampler.
*/
void init();
/**
* @brief Starts air interface sampler.
*/
Expand Down Expand Up @@ -213,6 +217,11 @@ class DSP_FW_API IO {
*/
void selfTest();

/**
* @brief
*/
void resetMCU();

/**
* @brief
* @param[out] int1
Expand Down
19 changes: 19 additions & 0 deletions IOSTM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,25 @@ void IO::getUDID(uint8_t* buffer)

/* */

void IO::resetMCU()
{
DEBUG1("reset - bye-bye");

delayUS(250 * 1000);

setLEDInt(false);
setCOSInt(false);
setDMRInt(false);
setP25Int(false);
setNXDNInt(false);

delayUS(250 * 1000);

NVIC_SystemReset();
}

/* */

void IO::delayBit()
{
delay_ns();
Expand Down
4 changes: 4 additions & 0 deletions SerialPort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,10 @@ void SerialPort::process()
}
break;

case CMD_RESET_MCU:
io.resetMCU();
break;

case CMD_SET_BUFFERS:
err = setBuffers(m_buffer + 3U, m_len - 3U);
if (err == RSN_OK) {
Expand Down
2 changes: 2 additions & 0 deletions SerialPort.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ enum DVM_COMMANDS {
CMD_FLSH_READ = 0xE0U, //! Read Flash Partition
CMD_FLSH_WRITE = 0xE1U, //! Write Flash Partition

CMD_RESET_MCU = 0xEAU, //! Soft Reboot MCU

CMD_DEBUG1 = 0xF1U, //!
CMD_DEBUG2 = 0xF2U, //!
CMD_DEBUG3 = 0xF3U, //!
Expand Down

0 comments on commit e23d16c

Please sign in to comment.