From: Robin Krens Date: Fri, 19 Jul 2019 18:55:07 +0000 (+0800) Subject: small system SRAM info output X-Git-Url: https://robinkrens.nl/gitweb/?a=commitdiff_plain;h=a4ce13373a288b4496299396e26c2608852b713f;p=cortex-from-scratch small system SRAM info output Small sysinfo output of SRAM memory, stack and used memory so far --- diff --git a/Makefile b/Makefile index c4803b9..788955b 100644 --- a/Makefile +++ b/Makefile @@ -1,15 +1,21 @@ +# Copyright 2019 - Robin Krens +# Cross compilers links CC=arm-none-eabi-gcc LD=arm-none-eabi-ld AR=$(TOOLROOT)/arm-none-eabi-ar AS=arm-none-eabi-as MKIMG=arm-none-eabi-objcopy -LDFLAGS+= -mthumb -mcpu=cortex-m0 +# Compiler flags +# TODO:Cortex-m3 or Cortex-m0? +LDFLAGS+= -mthumb -mcpu=cortex-m3 CFLAGS+= -mcpu=cortex-m3 -mthumb -g +# Start up machine assembly as: $(AS) $(CFLAGS) -o start.o start.asm +# Compile and link all all: $(AS) $(CFLAGS) -o start.o start.asm $(CC) $(CFLAGS) -c -I./include -ffreestanding -o main.o main.c @@ -23,11 +29,22 @@ all: $(LD) -nostartfiles -T link.ld -o start.out start.o main.o uart.o ivt.o systick.o sysinfo.o lib.o mm.o regf.o $(MKIMG) -Obinary -R .data start.out kernel.bin +# Run in Qemu; note this is a patched version for stm32-f103c8 run: /usr/local/bin/qemu-system-arm -serial stdio -M stm32-f103c8 -kernel kernel.bin -examine: - arm-none-eabi-objdump -S start.out +# Examine all sections +examine-all: + arm-none-eabi-objdump -D start.out | less + +# Examine just headers +examine-header: + arm-none-eabi-objdump -x start.out | less + +# Flash kernel to board +flash: + stm32flash -w kernel.bin -v /dev/ttyUSB0 + %.o: %.c $(CC) -c $(CFLAGS) $< -o $@ diff --git a/ivt.c b/ivt.c index 183713f..973d306 100644 --- a/ivt.c +++ b/ivt.c @@ -83,8 +83,8 @@ void ivt_set_gate(unsigned char num, void * isr(), short pri) { /* Dummy interrupt */ -__attribute__ ((interrupt)) -void * dummy_isr(struct interrupt_frame * frame) { +// __attribute__ ((interrupt)) +void * dummy_isr(/* struct interrupt_frame * frame */) { uint8_t nr = *SCB_VTOR_ST & 0xFF; diff --git a/lib.c b/lib.c index bc5bc8c..d753ac3 100644 --- a/lib.c +++ b/lib.c @@ -9,6 +9,7 @@ char hexbuf[8]; +/* Still kind of a debug function */ void addrtohex(uint32_t addr) { char tmpbuf[6] = {'A', 'B', 'C', 'D', 'E', 'F'}; memset(&hexbuf, 0, sizeof(uint32_t) * 8); @@ -25,12 +26,12 @@ void addrtohex(uint32_t addr) { } } - uart_puts("ADDRESS: 0x"); + //uart_puts("ADDRESS: 0x"); for (int i = 7; i >= 0; i--) { uart_putc(hexbuf[i]); } //uart_puts(hexbuf); - uart_putc('\n'); + //uart_putc('\n'); } diff --git a/main.c b/main.c index c8a47aa..73e55e0 100644 --- a/main.c +++ b/main.c @@ -46,18 +46,21 @@ void main() ivt_init(); // clock_init(); uart_init(); - uart_puts("LOADING SYSTEM 0.1 ...\n"); + //uart_puts("LOADING SYSTEM 0.1 ...\n"); systick_init(); - sysinfo(); mm_init(); + sysinfo(); //int * p2 = mm_alloc(512); //memset(p2, 'a', 512); //addrtohex(p2); //addrtohex(*p2); - - //uint32_t * p = (volatile unsigned) 0x21000000; + + /* extern stub(); + stub(); + __asm__ __volatile__ ("udiv r1, r3 ,%0" :: "r"(0)); */ + //regw_u32(p, 0x0CCCCCCCC, 4, 0x01); //regw_u8(p, 0xFF, 0, 0x02); diff --git a/mm.c b/mm.c index 4f56459..77029e7 100644 --- a/mm.c +++ b/mm.c @@ -10,6 +10,7 @@ * SIMPLE BITMAP IMPLEMENTATION * */ + #define CHUNKS 128 #define FREE 0x00 #define ALLOC 0x01 diff --git a/start.asm b/start.asm index e4ca237..ba161a2 100644 --- a/start.asm +++ b/start.asm @@ -1,4 +1,4 @@ - .equ STACK_TOP, 0x20008000 /* placed at 32kB, TODO: could place at top of SRAM? */ + .equ STACK_TOP, 0x20010000 /* placed at 32kB, TODO: could place at top of SRAM? */ .text .global _start .global reset, nmi, hardfault @@ -27,12 +27,9 @@ hardfault: b hardfault .global stub stub: - mov r1, #'z' - ldr r0, [r1] - bx lr - /* ldr R0,=10 + ldr R0,=10 mov R1,#0 - udiv.w R2, R0, R1 */ + udiv.w R2, R0, R1 .data .word 'x' diff --git a/sysinfo.c b/sysinfo.c index 3971d3b..2d6f047 100644 --- a/sysinfo.c +++ b/sysinfo.c @@ -4,24 +4,25 @@ #include #include +#define MEM_SIZE 0x00010000 +#define MEM_OFFSET 0x20000000 + +uint32_t get_msp(void); void sysinfo() { uint32_t tmp = *MCU_ID; - - uart_puts("CHECKING SYS INFO\n"); + uart_puts("# ROBSYS 0.1 LOADING...\n"); uart_puts("# DEVICE ID: "); - if (tmp & 0x414) { + if (tmp & 0x414) uart_puts("HIGH DENSITY\n"); - } else { uart_puts("UNKNOWN\n"); } tmp = (tmp >> 16); uart_puts("# REVISION: "); -// addrtohex(tmp); switch (tmp) { case 0x1000: uart_puts("REVISION A\n"); @@ -36,5 +37,35 @@ void sysinfo() { uart_puts("UNKNOWN\n"); } + extern char _endofbss; + + uint32_t current_stack = get_msp(); + uint32_t stack_usage = (MEM_OFFSET + MEM_SIZE) - current_stack; + uint32_t data_bss = &_endofbss - MEM_OFFSET; + uint32_t mem_free = MEM_SIZE - stack_usage - data_bss; + + uart_puts("# TOTAL MEMORY: "); + addrtohex(MEM_SIZE); + uart_putc('\n'); + uart_puts("# FREE MEMORY: "); + addrtohex(mem_free); + uart_putc('\n'); + uart_puts("# STACK USAGE: "); + addrtohex(stack_usage); + uart_putc('\n'); + } +/* Get the master stack pointer's position + * Here we use a so-called naked function, + * that doesn't automatically push and pop */ + +uint32_t get_msp(void) __attribute__( ( naked ) ); +uint32_t get_msp(void) +{ + uint32_t msp = 0; + __asm__ volatile ("mrs %0, msp\n\t" + "mov r0, %0 \n\t" + "bx lr \n\t" : "=r" (msp) ); + return(msp); +} diff --git a/systick.c b/systick.c index e90714a..ed062c0 100644 --- a/systick.c +++ b/systick.c @@ -16,11 +16,12 @@ struct interrupt_frame { uint32_t psr; // N-4 }; -__attribute__ ((interrupt)) -void * systick_handler(struct interrupt_frame * frame) { +//__attribute__ ((interrupt)) +void * systick_handler(/* struct interrupt_frame * frame */) { - uint32_t volatile status; +// uint32_t volatile status; uart_puts("TICKING\n"); +// for(;;); }