Skip to content

Commit

Permalink
finish bootloader and memory layout
Browse files Browse the repository at this point in the history
  • Loading branch information
catphish committed Mar 4, 2018
1 parent d89a835 commit 987f361
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 60 deletions.
91 changes: 56 additions & 35 deletions boot.s
Original file line number Diff line number Diff line change
@@ -1,46 +1,67 @@
/* Simple startup file for Cortex-M3 */

/* Thumb-2 instructions are only supported in unified syntax mode */
.syntax unified
/* Thumb-2 instructions are only supported in unified syntax mode */
.syntax unified

/* Vector table definition */
.section ".cs3.interrupt_vector"
.long __cs3_stack /* Top of Stack */
.long Reset_Handler /* Reset Handler */
.long NMI_Handler /* NMI Handler */
.long HardFault_Handler /* Hard Fault Handler */
.long MemManage_Handler /* MPU Fault Handler */
.long BusFault_Handler /* Bus Fault Handler */
.long UsageFault_Handler /* Usage Fault Handler */
.long 0 /* Reserved */
.long 0 /* Reserved */
.long 0 /* Reserved */
.long 0 /* Reserved */
.long SVC_Handler /* SVCall Handler */
.long DebugMon_Handler /* Debug Monitor Handler */
.long 0 /* Reserved */
.long PendSV_Handler /* PendSV Handler */
.long SysTick_Handler /* SysTick Handler */
.section ".isr_vector", "a"
.long _sram_end /* Top of Stack */
.long Reset_Handler /* Reset Handler */
.long NMI_Handler /* NMI Handler */
.long HardFault_Handler /* Hard Fault Handler */
.long MemManage_Handler /* MPU Fault Handler */
.long BusFault_Handler /* Bus Fault Handler */
.long UsageFault_Handler /* Usage Fault Handler */
.long 0 /* Reserved */
.long 0 /* Reserved */
.long 0 /* Reserved */
.long 0 /* Reserved */
.long SVC_Handler /* SVCall Handler */
.long DebugMon_Handler /* Debug Monitor Handler */
.long 0 /* Reserved */
.long PendSV_Handler /* PendSV Handler */
.long SysTick_Handler /* SysTick Handler */

/* Program section */
.section ".text"
/* Program section */
.section ".text"

/* Declare as thumb function. Otherwise it will not be linked
* correctly
*/
.thumb_func
/* Export the symbol so linker can see this */
.global Reset_Handler
/* Declare as thumb function. Otherwise it will not be linked
* correctly
*/
.thumb_func
/* Export the symbol so linker can see this */
.global Reset_Handler
Reset_Handler:
/* Jump to main(), a thumb function */
LDR R0, =main
BX R0
/* If main() ever exit, this should hold MCU from running wild */
B .
CopyDataInitializersStart:
ldr r0, =_sdata /* write to this addr */
ldr r1, =_edata /* until you get to this addr */
ldr r2, =_sidata /* reading from this addr */
b CopyDataInitializersEnterLoop
CopyDataInitializersLoop:
ldmia r2!, {r3}
stmia r0!, {r3}
CopyDataInitializersEnterLoop:
cmp r0, r1
bcc CopyDataInitializersLoop
WriteZeroToBssStart:
ldr r0, =_sbss
ldr r1, =_ebss
movs r2, #0
b WriteZeroToBssEnterLoop
WriteZeroToBssLoop:
stmia r0!, {r2}
WriteZeroToBssEnterLoop:
cmp r0, r1
bcc WriteZeroToBssLoop
/* Jump to main(), a thumb function */
LDR R0, =setup
BLX R0
LDR R0, =main
BLX R0
/* If main() ever exit, this should hold MCU from running wild */
B .

/* This is how the lazy guy doing it: by aliasing all the
* interrupts into single address
*/
/* All standard interrupts will land here */
.thumb_func
NMI_Handler:
.thumb_func
Expand Down
50 changes: 44 additions & 6 deletions linker.ld
Original file line number Diff line number Diff line change
@@ -1,23 +1,61 @@
ENTRY(Reset_Handler)

/* memory layout for an STM32L476RG */
MEMORY
{
FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 0x100000
SRAM (xrw) : ORIGIN = 0x20000000, LENGTH = 0x18000
}
/* End of SRAM for top of stack */
_sram_end = 0x20000000 + 0x18000;

/* output sections */
/* Sections */
SECTIONS
{
/* program code into FLASH */
.isr_vector :
{
. = ALIGN(4);
KEEP(*(.isr_vector))
. = ALIGN(4);
} >FLASH

.text :
{
*(.vector_table) /* Vector table */
*(.text) /* Program code */
. = ALIGN(4);
*(.text) /* .text sections (code) */
*(.text*) /* .text sections (code) */
*(.rodata) /* .rodata sections (constants, strings, etc.) */
*(.rodata*) /* .rodata sections (constants, strings, etc.) */
. = ALIGN(4);
} >FLASH

/* uninitialized global and static variables into SRAM */
.rodata :
{
. = ALIGN(4);
*(.rodata) /* .rodata sections (constants, strings, etc.) */
*(.rodata*) /* .rodata* sections (constants, strings, etc.) */
. = ALIGN(4);
} >FLASH

_sidata = LOADADDR(.data);
.data :
{
*(.data)
. = ALIGN(4);
_sdata = .; /* create a global symbol at data start */
*(.data) /* .data sections */
*(.data*) /* .data* sections */
. = ALIGN(4);
_edata = .; /* define a global symbol at data end */
} >SRAM AT> FLASH

.bss :
{
. = ALIGN(4);
_sbss = .;
*(.bss)
*(.bss*)
*(COMMON)
. = ALIGN(4);
_ebss = .;
} >SRAM
}
30 changes: 12 additions & 18 deletions main.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <stdint.h>

#define MMIO32(addr) (*(volatile uint32_t *)(addr))
#define MMIO32(addr) (*(volatile uint32_t *)(addr))
#define PERIPH_BASE_AHB2 (0x48000000U)
#define GPIO_PORT_A_BASE (PERIPH_BASE_AHB2 + 0x0000)
#define GPIOA GPIO_PORT_A_BASE
Expand All @@ -13,31 +13,25 @@
#define RCC_AHB2ENR_OFFSET 0x4c
#define RCC_AHB2ENR MMIO32(RCC_BASE + RCC_AHB2ENR_OFFSET)

/* work out end of RAM address as initial stack pointer */
#define SRAM_BASE 0x20000000
#define SRAM_SIZE 0x18000 // 96KiB RAM
#define SRAM_END (SRAM_BASE + SRAM_SIZE)

# define nop() __asm__ __volatile__ ("nop" ::)

int main(void) {
int ontime = 500000;
int offtime = 250000;

void setup() {
RCC_AHB2ENR |= 1;
nop();
nop();
GPIOA_MODER = 0xAB555555;
GPIOA_ODR = 0xFFFFFFFF;
}

int main() {
int n;
while (1) {
for(n=0;n<500000;n++) nop();
GPIOA_ODR = 0x0;
for(n=0;n<500000;n++) nop();
GPIOA_ODR = 0xFFFF;
for(n=0;n<ontime;n++) nop();
GPIOA_ODR = 0x0;
for(n=0;n<offtime;n++) nop();
}
return(0);
}

/* vector table */
uint32_t vector_table[] __attribute__((section(".vector_table"))) =
{
(uint32_t)SRAM_END, // initial stack pointer
(uint32_t)main // main as Reset_Handler
};
3 changes: 2 additions & 1 deletion make.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
set -e
arm-none-eabi-gcc --specs=nosys.specs -std=gnu11 -Wall -pedantic -g -O2 -mlittle-endian -mthumb -mthumb-interwork -mcpu=cortex-m4 -fsingle-precision-constant -Wdouble-promotion -c main.c -o main.o
arm-none-eabi-ld -o main.elf -T linker.ld main.o
arm-none-eabi-gcc --specs=nosys.specs -std=gnu11 -Wall -pedantic -g -O2 -mlittle-endian -mthumb -mthumb-interwork -mcpu=cortex-m4 -fsingle-precision-constant -Wdouble-promotion -c boot.s -o boot.o
arm-none-eabi-ld -o main.elf -T linker.ld boot.o main.o
arm-none-eabi-objcopy -O binary main.elf main.bin
st-flash write main.bin 0x8000000

0 comments on commit 987f361

Please sign in to comment.