Skip to content

Instantly share code, notes, and snippets.

@xndcn
Last active November 5, 2024 12:24
Show Gist options
  • Save xndcn/3c534818b6486ecd2414d1cc7925c372 to your computer and use it in GitHub Desktop.
Save xndcn/3c534818b6486ecd2414d1cc7925c372 to your computer and use it in GitHub Desktop.
stm32_qemu_test
#include <sys/types.h>
#define SCB_MPU_CTRL (*(volatile uint32_t *)0xE000ED94)
#define SCB_MPU_RASR (*(volatile uint32_t *)0xE000EDA0)
#define SCB_MPU_RBAR (*(volatile uint32_t *)0xE000ED9C)
int main(void) {
SCB_MPU_CTRL = 0;
SCB_MPU_RBAR = 0x08000000 | (1<<4) | 0;
SCB_MPU_RASR = (7 << 24) | (18 << 1) | 1; // 512KB access
SCB_MPU_RBAR = 0x08000000 | (1<<4) | 1;
SCB_MPU_RASR = (4 << 1) | 1; // 32B no access
SCB_MPU_CTRL = 1;
while (1);
}
main.elf: main.c startup.s simple.ld
arm-none-eabi-gcc -o main.elf main.c startup.s -Wl,-T,simple.ld -g -mcpu=cortex-m3 -mthumb -nostartfiles
/******************************************************************************
Simple Linker File
.text program code;
.rodata read-only data;
.bss read-write zero initialized data.
.data read-write initialized data;
for simplicity : no heap defined.
******************************************************************************/
OUTPUT_FORMAT ("elf32-littlearm", "elf32-bigarm", "elf32-littlearm")
/* define entry point */
ENTRY(Reset_Handler)
/* calculate the Last RAM address*/
_estack = ORIGIN(RAM) + LENGTH(RAM) - 1;
MEMORY{
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 8K
FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 32K
}
/* --- Sections --- */
SECTIONS
{
/*--------------------------------------------------------------------------*/
/* interrupt vector goes at the beginning of FLASH */
.isr_vector : {
. = ALIGN(4); /* align */
_sisr_vector = .; /* define start symbol */
KEEP(*(.isr_vector)) /* Interrupt vectors */
. = ALIGN(4); /* align */
_eisr_vector = .; /* define end symbol */
} > FLASH
/*--------------------------------------------------------------------------*/
/* program data goes into FLASH */
.text : {
. = ALIGN(4); /* align */
_stext = .; /* define start symbol */
*(.text) /* insert program code .text */
*(.text*) /* .text* sections */
*(.glue_7) /* glue arm to thumb code */
*(.glue_7t) /* glue thumb to arm code */
*(.eh_frame)
. = ALIGN(4); /* align */
_etext = .; /* define end symbol */
} > FLASH
/*--------------------------------------------------------------------------*/
/* constant data goes into FLASH */
.rodata : {
*(.rodata) /* .rodata sections (constants, strings, etc.) */
*(.rodata*) /* .rodata* sections (constants, strings, etc.) */
. = ALIGN(4); /* align */
_erodata = .;
} > FLASH
/*--------------------------------------------------------------------------*/
/* ARM stack unwinding section (GDB uses this) */
.ARM.extab : {
__extab_start__ = .;/* define start symbol */
*(.ARM.extab* .gnu.linkonce.armextab.*)
__extab_end__ = .; /* define end symbol */
} > FLASH
.ARM : {
__exidx_start__ = .;/* define start symbol */
*(.ARM.exidx* .gnu.linkonce.armexidx.*)
__exidx_end__ = .; /* define end symbol */
} > FLASH
/*--------------------------------------------------------------------------*/
/* Define RAM Section : only sections who goes to ram after this point */
. = ORIGIN(RAM); /* for ld.lld it seems to be necessary to define the ram section this way.*/
/* otherwise, the .data sectin will not defined properly.*/
/* to be precise : the variable are in RAM but the initialization data would not be defined in flash */
/*--------------------------------------------------------------------------*/
/* initialized data goes to ram, but must be loaded from flash */
/*has to be behind bss. otherwise it strangly not working properly*/
.data : AT(__exidx_end__) {
_sdata = .; /* create a global symbol at data start */
*(.data) /* .data sections */
*(.data*) /* .data* sections */
. = ALIGN(4); /* align */
_edata = .; /* define a global symbol at data end */
} > RAM
_sidata = LOADADDR(.data); /* get the start address of the .data section */
/*--------------------------------------------------------------------------*/
/* zero initialized data goes to RAM and has to be cleared at startup */
.bss :
{
_sbss = .; /* define a global symbol at bss start */
__bss_start__ = .; /* symbolname defined by newlib*/
*(.bss) /* .bss sections */
*(.bss*) /* .bss* sections */
*(COMMON) /* common sections */
. = ALIGN(4); /* align */
_ebss = .; /* define a global symbol at bss end */
__bss_end__ = .; /* symbolname defined by newlib*/
} > RAM
/*--------------------------------------------------------------------------*/
.ARM.attributes 0 : { *(.ARM.attributes) }
}
/*
Startup Code for Qemu-Cortex-M3
used startup_stm32l072xx.s as prototype
*/
.syntax unified
.cpu cortex-m3
.fpu softvfp
.thumb
.global g_pfnVectors
.global Default_Handler
.word _sidata /* start address for the initialization values of the .data section.*/
.word _sdata /* start address for the .data section. defined in linker script */
.word _edata /* end address for the .data section. defined in linker script */
.word _sbss /* start address for the .bss section. defined in linker script */
.word _ebss /* end address for the .bss section. defined in linker script */
.section .text.Reset_Handler
.weak Reset_Handler
.type Reset_Handler, %function
Reset_Handler:
ldr r0, =_estack
mov sp, r0 /* set stack pointer */
/* Copy the data segment initializers from flash to SRAM */
movs r1, #0
b LoopCopyDataInit
CopyDataInit:
ldr r3, =_sidata
ldr r3, [r3, r1]
str r3, [r0, r1]
adds r1, r1, #4
LoopCopyDataInit:
ldr r0, =_sdata
ldr r3, =_edata
adds r2, r0, r1
cmp r2, r3
bcc CopyDataInit
ldr r2, =_sbss
b LoopFillZerobss
/* Zero fill the bss segment. */
FillZerobss:
movs r3, #0
str r3, [r2]
adds r2, r2, #4
LoopFillZerobss:
ldr r3, = _ebss
cmp r2, r3
bcc FillZerobss
/* Call the application's entry point.*/
bl main
.size Reset_Handler, .-Reset_Handler
.section .text.Default_Handler,"ax",%progbits
Default_Handler:
Infinite_Loop:
b Infinite_Loop
.size Default_Handler, .-Default_Handler
.section .isr_vector,"a",%progbits
.type g_pfnVectors, %object
.size g_pfnVectors, .-g_pfnVectors
g_pfnVectors:
.word _estack
.word Reset_Handler
.word NMI_Handler
.word HardFault_Handler
.word 0
.word 0
.word 0
.word 0
.word 0
.word 0
.word 0
.word SVC_Handler
.word 0
.word 0
.word PendSV_Handler
.word SysTick_Handler
.weak NMI_Handler
.thumb_set NMI_Handler,Default_Handler
.weak HardFault_Handler
.thumb_set HardFault_Handler,Default_Handler
.weak SVC_Handler
.thumb_set SVC_Handler,Default_Handler
.weak PendSV_Handler
.thumb_set PendSV_Handler,Default_Handler
.weak SysTick_Handler
.thumb_set SysTick_Handler,Default_Handler
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment