Skip to content

Commit

Permalink
Bootloader: Implemented jump to system memory
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiasbock committed Mar 19, 2018
1 parent 696e331 commit 59b2813
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 3 deletions.
6 changes: 6 additions & 0 deletions Inc/bootloader.h
@@ -0,0 +1,6 @@

#ifndef BOOTLOADER_H

void jump_to_st_usb_bootloader();

#endif
4 changes: 2 additions & 2 deletions Inc/config.h
Expand Up @@ -25,8 +25,8 @@
#endif #endif


#ifdef PLATFORM_CANTACT #ifdef PLATFORM_CANTACT
#define CAN_RX_BUFFER_SIZE 350 #define CAN_RX_BUFFER_SIZE 340
#define CAN_TX_BUFFER_SIZE 350 #define CAN_TX_BUFFER_SIZE 340
#endif #endif


#define CAN_TX_TIMEOUT 20 #define CAN_TX_TIMEOUT 20
Expand Down
1 change: 1 addition & 0 deletions Inc/slcan.h
Expand Up @@ -77,6 +77,7 @@ enum slcan_message_type {
CANTACT_SET_MODE2 = 'M', CANTACT_SET_MODE2 = 'M',
CANTACT_SET_FILTER = 'F', CANTACT_SET_FILTER = 'F',
CANTACT_SET_MASK = 'K', CANTACT_SET_MASK = 'K',
CANTACT_START_BOOTLOADER = 'B',


USBTIN_OPEN_LOOPBACK = 'I', USBTIN_OPEN_LOOPBACK = 'I',
USBTIN_OPEN_LISTEN_ONLY = 'L', USBTIN_OPEN_LISTEN_ONLY = 'L',
Expand Down
50 changes: 50 additions & 0 deletions Src/bootloader.c
@@ -0,0 +1,50 @@

#include <bootloader.h>
#include <platform.h>
#include <config.h>
#include <led.h>


void jump_to_st_usb_bootloader()
{
// Disable interrupts
__disable_irq();

// Disable RCC and reset it to default values
RCC->CFGR = 0;
RCC->CR = 0x83;

//Disable systick timer and reset it to default values
SysTick->CTRL = 0;
SysTick->LOAD = 0;
SysTick->VAL = 0;

// Disable USB
USB->CNTR = USB_CNTR_PDWN;

// Disable CAN
CAN->MCR = CAN_MCR_SLEEP;

// Disable LEDs
// TODO: Surprisingly, this doesn't switch off the LED.
// HAL_GPIO_WritePin(LED_ACTIVITY_PORT, LED_ACTIVITY_PIN, GPIO_PIN_RESET);

// Enable interrupts
// __enable_irq();

__DSB();

// Re-map system memory to address 0x0
SYSCFG->CFGR1 = (SYSCFG->CFGR1 & ~SYSCFG_CFGR1_MEM_MODE) | SYSCFG_CFGR1_MEM_MODE_0;

// Fetch and apply bootloader's stack pointer
__set_MSP(*(uint32_t*)(0x00));

__DSB();
__ISB();

// Invoke bootloader's reset handler
void (*SysMemBootJump)(void);
SysMemBootJump = (void (*)(void)) (*((uint32_t*)(4)));
SysMemBootJump();
}
5 changes: 4 additions & 1 deletion Src/slcan.c
Expand Up @@ -7,7 +7,8 @@
#include "can.h" #include "can.h"
#include "fifo.h" #include "fifo.h"
#include "slcan.h" #include "slcan.h"
#include <error.h> #include "error.h"
#include "bootloader.h"




int8_t slcan_parse_frame(CanRxMsgTypeDef* frame, uint8_t* buf) { int8_t slcan_parse_frame(CanRxMsgTypeDef* frame, uint8_t* buf) {
Expand Down Expand Up @@ -184,6 +185,8 @@ int8_t slcan_parse_command(uint8_t* buf, uint8_t len) {
return SUCCESS; return SUCCESS;
// error // error
return ERROR_TX_FIFO_OVERRUN; return ERROR_TX_FIFO_OVERRUN;
} else if (buf[0] == CANTACT_START_BOOTLOADER) {
jump_to_st_usb_bootloader();
} }


return ERROR_SLCAN_COMMAND_NOT_RECOGNIZED; return ERROR_SLCAN_COMMAND_NOT_RECOGNIZED;
Expand Down

0 comments on commit 59b2813

Please sign in to comment.