mirror of
https://github.com/UpsilonNumworks/Upsilon.git
synced 2026-03-18 21:30:38 +01:00
Bootloader pre-release
This commit is contained in:
@@ -4,9 +4,13 @@
|
||||
#include <drivers/config/clocks.h>
|
||||
#include <drivers/config/internal_flash.h>
|
||||
#include <drivers/external_flash.h>
|
||||
#include <drivers/timing.h>
|
||||
#include <regs/regs.h>
|
||||
#include <ion.h>
|
||||
|
||||
#include <bootloader/drivers/stm32_drivers.h>
|
||||
|
||||
using namespace STM32;
|
||||
typedef void(*ISR)(void);
|
||||
extern ISR InitialisationVector[];
|
||||
|
||||
@@ -439,6 +443,20 @@ bool pcbVersionIsLocked() {
|
||||
return *reinterpret_cast<const uint8_t *>(InternalFlash::Config::OTPLockAddress(k_pcbVersionOTPIndex)) == 0;
|
||||
}
|
||||
|
||||
void jumpToInternalBootloader() {
|
||||
asm volatile ("cpsie i" : : : "memory");
|
||||
|
||||
STM32::rcc_deinit();
|
||||
STM32::hal_deinit();
|
||||
STM32::systick_deinit();
|
||||
|
||||
const uint32_t p = (*((uint32_t *) 0x1FF00000));
|
||||
asm volatile ("MSR msp, %0" : : "r" (p) : );
|
||||
void (*SysMemBootJump)(void);
|
||||
SysMemBootJump = (void (*)(void)) (*((uint32_t *) 0x1FF00004));
|
||||
SysMemBootJump();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
56
bootloader/drivers/stm32_drivers.cpp
Normal file
56
bootloader/drivers/stm32_drivers.cpp
Normal file
@@ -0,0 +1,56 @@
|
||||
#include "stm32_drivers.h"
|
||||
|
||||
void STM32::rcc_deinit() {
|
||||
SET_BIT(STM_32_RCC->CR, (0x1UL << (0U)));
|
||||
while (READ_BIT(STM_32_RCC->CR, (0x1UL << (1U))) == 0) {}
|
||||
SET_BIT(STM_32_RCC->CR, (0x10UL << (3U)));
|
||||
CLEAR_REG(STM_32_RCC->CFGR);
|
||||
while (READ_BIT(STM_32_RCC->CFGR, (0x3UL << (2U))) != 0) {}
|
||||
CLEAR_BIT(STM_32_RCC->CR, (0x1UL << (16U)) | (0x1UL << (18U)) | (0x1UL << (19U)));
|
||||
while (READ_BIT(STM_32_RCC->CR, (0x1UL << (17U))) != 0) {}
|
||||
CLEAR_BIT(STM_32_RCC->CR, (0x1UL << (24U)));
|
||||
while (READ_BIT(STM_32_RCC->CR, (0x1UL << (25U))) != 0) {}
|
||||
CLEAR_BIT(STM_32_RCC->CR, (0x1UL << (26U)));
|
||||
while (READ_BIT(STM_32_RCC->CR, (0x1UL << (27U))) != 0) {}
|
||||
CLEAR_BIT(STM_32_RCC->CR, (0x1UL << (28U)));
|
||||
while (READ_BIT(STM_32_RCC->CR, (0x1UL << (29U))) != 0) {}
|
||||
STM_32_RCC->PLLCFGR = ((0x10UL << (0x0U)) | (0x040UL << (6U)) | (0x080UL << (6U)) | (0x4UL << (24U)) | 0x20000000U);
|
||||
STM_32_RCC->PLLI2SCFGR = ((0x040UL << (6U)) | (0x080UL << (6U)) | (0x4UL << (24U)) | (0x2UL << (28U)));
|
||||
STM_32_RCC->PLLSAICFGR = ((0x040UL << (6U)) | (0x080UL << (6U)) | (0x4UL << (24U)) | 0x20000000U);
|
||||
CLEAR_BIT(STM_32_RCC->CIR, ((0x1UL << (8U)) | (0x1UL << (9U)) | (0x1UL << (10U)) | (0x1UL << (11U)) | (0x1UL << (12U)) | (0x1UL << (13U)) | (0x1UL << (14U))));
|
||||
SET_BIT(STM_32_RCC->CIR, ((0x1UL << (16U)) | (0x1UL << (17U)) | (0x1UL << (18U)) | (0x1UL << (19U)) | (0x1UL << (20U)) | (0x1UL << (21U)) | (0x1UL << (22U)) | (0x1UL << (23U))));
|
||||
CLEAR_BIT(STM_32_RCC->CSR, ((0x1UL << (0U))));
|
||||
SET_BIT(STM_32_RCC->CSR, ((0x1UL << (24U))));
|
||||
uint32_t sysclock = ((uint32_t)16000000U);
|
||||
uint32_t a = ((sysclock / 1000U));
|
||||
uint32_t b = 15U;
|
||||
STM_32_SysTick->LOAD = (uint32_t)(a - 1UL);
|
||||
STM_32_SCB->SHPR[(((uint32_t)(-1))&0xFUL)-4UL] = (uint8_t)((((1UL << 4U)-1UL) << (8U - 4UL)) & (uint32_t)0xFFUL);
|
||||
STM_32_SysTick->VAL = 0U;
|
||||
STM_32_SysTick->CTRL = (1UL << 2U) | (1UL << 1U) | (1UL);
|
||||
uint32_t c = ((uint32_t)((STM_32_SCB->AIRCR & (7UL << 8U)) >> 8U));
|
||||
uint32_t d = (c & (uint32_t)0x07UL);
|
||||
uint32_t e;
|
||||
uint32_t f;
|
||||
e = ((7UL - d) > (uint32_t)(4UL)) ? (uint32_t)(4UL) : (7UL - d);
|
||||
f = ((d + (uint32_t)(4UL)) < (uint32_t)(7UL)) ? (uint32_t)(0UL) : (uint32_t)((d - 7UL) + (uint32_t)(4UL));
|
||||
uint32_t g = (((b & (uint32_t)((1UL << (e)) - 1UL)) << f) | ((0UL & (uint32_t)((1UL << (f)) - 1UL))));
|
||||
STM_32_SCB->SHPR[(((uint32_t)(-1))&0xFUL)-4UL] = (uint8_t)((g << (8U - 4UL)) & (uint32_t)0xFFUL);
|
||||
}
|
||||
|
||||
void STM32::hal_deinit() {
|
||||
STM_32_RCC->APB1RSTR = 0xFFFFFFFFU;
|
||||
STM_32_RCC->APB1RSTR = 0x00U;
|
||||
STM_32_RCC->APB2RSTR = 0xFFFFFFFFU;
|
||||
STM_32_RCC->APB2RSTR = 0x00U;
|
||||
STM_32_RCC->AHB1RSTR = 0xFFFFFFFFU;
|
||||
STM_32_RCC->AHB1RSTR = 0x00U;
|
||||
STM_32_RCC->AHB2RSTR = 0xFFFFFFFFU;
|
||||
STM_32_RCC->AHB2RSTR = 0x00U;
|
||||
STM_32_RCC->AHB3RSTR = 0xFFFFFFFFU;
|
||||
STM_32_RCC->AHB3RSTR = 0x00U;
|
||||
}
|
||||
|
||||
void STM32::systick_deinit() {
|
||||
STM_32_SysTick->CTRL = STM_32_SysTick->LOAD = STM_32_SysTick->VAL = 0;
|
||||
}
|
||||
153
bootloader/drivers/stm32_drivers.h
Normal file
153
bootloader/drivers/stm32_drivers.h
Normal file
@@ -0,0 +1,153 @@
|
||||
#include <stdint.h>
|
||||
|
||||
/*
|
||||
Here we implement a very little part of the code from the default stm32 libs because we only need the unload function.
|
||||
Now we include the license of the original code as required.
|
||||
*/
|
||||
|
||||
/*
|
||||
This software component is provided to you as part of a software package and
|
||||
applicable license terms are in the Package_license file. If you received this
|
||||
software component outside of a package or without applicable license terms,
|
||||
the terms of the BSD-3-Clause license shall apply.
|
||||
You may obtain a copy of the BSD-3-Clause at:
|
||||
https://opensource.org/licenses/BSD-3-Clause
|
||||
*/
|
||||
|
||||
namespace STM32 {
|
||||
|
||||
typedef struct
|
||||
{
|
||||
volatile uint32_t CR;
|
||||
volatile uint32_t PLLCFGR;
|
||||
volatile uint32_t CFGR;
|
||||
volatile uint32_t CIR;
|
||||
volatile uint32_t AHB1RSTR;
|
||||
volatile uint32_t AHB2RSTR;
|
||||
volatile uint32_t AHB3RSTR;
|
||||
uint32_t RESERVED0;
|
||||
volatile uint32_t APB1RSTR;
|
||||
volatile uint32_t APB2RSTR;
|
||||
uint32_t RESERVED1[2];
|
||||
volatile uint32_t AHB1ENR;
|
||||
volatile uint32_t AHB2ENR;
|
||||
volatile uint32_t AHB3ENR;
|
||||
uint32_t RESERVED2;
|
||||
volatile uint32_t APB1ENR;
|
||||
volatile uint32_t APB2ENR;
|
||||
uint32_t RESERVED3[2];
|
||||
volatile uint32_t AHB1LPENR;
|
||||
volatile uint32_t AHB2LPENR;
|
||||
volatile uint32_t AHB3LPENR;
|
||||
uint32_t RESERVED4;
|
||||
volatile uint32_t APB1LPENR;
|
||||
volatile uint32_t APB2LPENR;
|
||||
uint32_t RESERVED5[2];
|
||||
volatile uint32_t BDCR;
|
||||
volatile uint32_t CSR;
|
||||
uint32_t RESERVED6[2];
|
||||
volatile uint32_t SSCGR;
|
||||
volatile uint32_t PLLI2SCFGR;
|
||||
volatile uint32_t PLLSAICFGR;
|
||||
volatile uint32_t DCKCFGR1;
|
||||
volatile uint32_t DCKCFGR2;
|
||||
} STM32_RCC_TypeDef;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
volatile uint32_t CTRL;
|
||||
volatile uint32_t LOAD;
|
||||
volatile uint32_t VAL;
|
||||
volatile const uint32_t CALIB;
|
||||
} STM32_SysTick_Type;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
volatile uint32_t ISER[8U];
|
||||
uint32_t RESERVED0[24U];
|
||||
volatile uint32_t ICER[8U];
|
||||
uint32_t RSERVED1[24U];
|
||||
volatile uint32_t ISPR[8U];
|
||||
uint32_t RESERVED2[24U];
|
||||
volatile uint32_t ICPR[8U];
|
||||
uint32_t RESERVED3[24U];
|
||||
volatile uint32_t IABR[8U];
|
||||
uint32_t RESERVED4[56U];
|
||||
volatile uint8_t IP[240U];
|
||||
uint32_t RESERVED5[644U];
|
||||
volatile uint32_t STIR;
|
||||
} STM32_NVIC_Type;
|
||||
|
||||
typedef struct {
|
||||
volatile const uint32_t CPUID;
|
||||
volatile uint32_t ICSR;
|
||||
volatile uint32_t VTOR;
|
||||
volatile uint32_t AIRCR;
|
||||
volatile uint32_t SCR;
|
||||
volatile uint32_t CCR;
|
||||
volatile uint8_t SHPR[12U];
|
||||
volatile uint32_t SHCSR;
|
||||
volatile uint32_t CFSR;
|
||||
volatile uint32_t HFSR;
|
||||
volatile uint32_t DFSR;
|
||||
volatile uint32_t MMFAR;
|
||||
volatile uint32_t BFAR;
|
||||
volatile uint32_t AFSR;
|
||||
volatile const uint32_t ID_PFR[2U];
|
||||
volatile const uint32_t ID_DFR;
|
||||
volatile const uint32_t ID_AFR;
|
||||
volatile const uint32_t ID_MFR[4U];
|
||||
volatile const uint32_t ID_ISAR[5U];
|
||||
uint32_t RESERVED0[1U];
|
||||
volatile const uint32_t CLIDR;
|
||||
volatile const uint32_t CTR;
|
||||
volatile const uint32_t CCSIDR;
|
||||
volatile uint32_t CSSELR;
|
||||
volatile uint32_t CPACR;
|
||||
uint32_t RESERVED3[93U];
|
||||
volatile uint32_t STIR;
|
||||
uint32_t RESERVED4[15U];
|
||||
volatile const uint32_t MVFR0;
|
||||
volatile const uint32_t MVFR1;
|
||||
volatile const uint32_t MVFR2;
|
||||
uint32_t RESERVED5[1U];
|
||||
volatile uint32_t ICIALLU;
|
||||
uint32_t RESERVED6[1U];
|
||||
volatile uint32_t ICIMVAU;
|
||||
volatile uint32_t DCIMVAC;
|
||||
volatile uint32_t DCISW;
|
||||
volatile uint32_t DCCMVAU;
|
||||
volatile uint32_t DCCMVAC;
|
||||
volatile uint32_t DCCSW;
|
||||
volatile uint32_t DCCIMVAC;
|
||||
volatile uint32_t DCCISW;
|
||||
uint32_t RESERVED7[6U];
|
||||
volatile uint32_t ITCMCR;
|
||||
volatile uint32_t DTCMCR;
|
||||
volatile uint32_t AHBPCR;
|
||||
volatile uint32_t CACR;
|
||||
volatile uint32_t AHBSCR;
|
||||
uint32_t RESERVED8[1U];
|
||||
volatile uint32_t ABFSR;
|
||||
} STM32_SCB_Type;
|
||||
|
||||
#define RCC_BASE 0x40023800UL
|
||||
#define SysTick_BASE 0xE000E010UL
|
||||
#define NVIC_BASE 0xE000E100UL
|
||||
#define SCB_BASE 0xE000ED00UL
|
||||
|
||||
#define SET_BIT(REG, BIT) ((REG) |= (BIT))
|
||||
#define CLEAR_BIT(REG, BIT) ((REG) &= ~(BIT))
|
||||
#define READ_BIT(REG, BIT) ((REG) & (BIT))
|
||||
#define CLEAR_REG(REG) ((REG) = (0x0))
|
||||
#define READ_REG(REG) ((REG))
|
||||
|
||||
#define STM_32_RCC ((STM32::STM32_RCC_TypeDef *) RCC_BASE)
|
||||
#define STM_32_SysTick ((STM32::STM32_SysTick_Type *) SysTick_BASE)
|
||||
#define STM_32_NVIC ((STM32::STM32_NVIC_Type *) NVIC_BASE)
|
||||
#define STM_32_SCB ((STM32_SCB_Type *) SCB_BASE)
|
||||
|
||||
extern void rcc_deinit();
|
||||
extern void hal_deinit();
|
||||
extern void systick_deinit();
|
||||
}
|
||||
Reference in New Issue
Block a user