diff --git a/boot.s b/boot.s index c4b2b8e..50ccfab 100644 --- a/boot.s +++ b/boot.s @@ -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 diff --git a/linker.ld b/linker.ld index a01706d..2dcb194 100644 --- a/linker.ld +++ b/linker.ld @@ -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 } diff --git a/main.c b/main.c index 97b80f0..70a6927 100644 --- a/main.c +++ b/main.c @@ -1,6 +1,6 @@ #include -#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 @@ -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