rp2040

RP2040 Programming without SDK
Log | Files | Refs

commit 74bf05d533a89fee840998a101cef415e180ffa1
parent 2abd2bbbc4ddb143e60f6eea043a7b9be4c290a3
Author: Matsuda Kenji <info@mtkn.jp>
Date:   Sat,  2 Mar 2024 08:23:39 +0900

merge

Diffstat:
Aex1_sram/Makefile | 40++++++++++++++++++++++++++++++++++++++++
Aex1_sram/boot2.s | 46++++++++++++++++++++++++++++++++++++++++++++++
Aex1_sram/main.c | 7+++++++
Aex1_sram/memmap.ld | 26++++++++++++++++++++++++++
Aex1_sram/start.s | 66++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mex3/Makefile | 2+-
Mex3/boot2.s | 17++++++++---------
Mex3/main.c | 7++-----
Mex3/memmap.ld | 9++++++++-
Mex3/start.s | 3++-
10 files changed, 206 insertions(+), 17 deletions(-)

diff --git a/ex1_sram/Makefile b/ex1_sram/Makefile @@ -0,0 +1,40 @@ +CC = arm-none-eabi-gcc +AS = arm-none-eabi-as +LD = arm-none-eabi-ld +OBJCOPY = arm-none-eabi-objcopy +BINCRC = ../tools/bincrc +BIN2UF2 = ../tools/bin2uf2 + +MCPU = -mcpu=cortex-m0plus +ASFLAGS = $(MCPU) +CFLAGS = $(MCPU) -ffreestanding -nostartfiles -O0 -fpic -mthumb -c +LDFLAGS = --no-relax -nostdlib + +all: tools led.uf2 + +clean: + rm -f *.o *.elf *.uf2 *.bin + cd ../tools && make clean + +.s.o: + $(AS) $(ASFLAGS) -o $@ $< + +.c.o: + $(CC) $(CFLAGS) -o $@ $< + +led.elf: boot2.o main.o start.o memmap.ld + $(LD) $(LDFLAGS) -o $@ -T memmap.ld boot2.o main.o start.o + +led.bin: led.elf + $(OBJCOPY) -O binary led.elf $@ + +led.uf2: led.bin + $(BINCRC) led.bin led_crc.bin + $(BIN2UF2) led_crc.bin $@ + +flash: all + mount /dev/disk/by-label/RPI-RP2 /mnt + cp led.uf2 /mnt + +tools: + cd ../tools && make diff --git a/ex1_sram/boot2.s b/ex1_sram/boot2.s @@ -0,0 +1,46 @@ +.cpu cortex-m0plus +.thumb + + .section .boot2 +boot2: + // setup xip + ldr r3, rom_base + ldrh r0, [r3, #0x14] // rom_func_table + ldr r1, =('C' | 'X' << 8) // _flash_enter_cmd_xip() + ldrh r2, [r3, #0x18] // rom_table_lookup + blx r2 + blx r0 + + // load main program to sram + ldr r0, =text_start + ldr r1, =text_size + mov r2, #0 + ldr r3, sram_base +copy: + cmp r1, r2 + beq end + ldr r4, [r0, r2] + str r4, [r3, r2] + add r2, #4 + b copy +end: + + // setup vector table + ldr r0, =vectors + ldr r1, m0plus_vtor + str r0, [r1, #0] // M0PLUS: VTOR + ldr r1, [r0, #4] // entry point + ldr r0, [r0, #0] // stack pointer + mov sp, r0 + bx r1 + + .align 2 +rom_base: + .word 0x00000000 +sram_base: + .word 0x20000000 +m0plus_vtor: + .word 0xe0000000 + 0xed08 +literals: + .ltorg + diff --git a/ex1_sram/main.c b/ex1_sram/main.c @@ -0,0 +1,7 @@ +void led(void); + +void +main(void) +{ + led(); +} diff --git a/ex1_sram/memmap.ld b/ex1_sram/memmap.ld @@ -0,0 +1,26 @@ +MEMORY +{ + FLASH(rx) : ORIGIN = 0x10000000, LENGTH = 2048k + SRAM(rwx) : ORIGIN = 0x20000000, LENGTH = 264k +} + +SECTIONS +{ + .boot2 : { + *(.boot2) + . = 0x100; + } > FLASH + + .vectors : { + *(.vectors) + . += SIZEOF(.vectors); + . = ALIGN(2); + text_start = .; + } > FLASH + + .text : { + *(.text) + } > SRAM AT > FLASH + text_size = SIZEOF(.text); +} + diff --git a/ex1_sram/start.s b/ex1_sram/start.s @@ -0,0 +1,66 @@ +.cpu cortex-m0plus +.thumb + + .section .vectors + .global vectors +vectors: + .word 0x20040000 // initial SP + .word (reset+1) // entry point + + .section .text +reset: + bl main + + .global led +led: + // unreset gpio + mov r0, #1 + lsl r0, r0, #5 // io_bank0 + ldr r3, resets_base + ldr r1, atomic_clr + str r0, [r3, r1] // RESETS: RESET + +reset_chk: + ldr r1, [r3, #0x8] // RESETS: RESET_DONE + tst r0, r1 + beq reset_chk + + // set gpio functions + ldr r3, io_bank0_base + mov r0, #5 // sio + mov r1, #0xcc + str r0, [r3, r1] // IO_BANK0: GPIO25_CTRL + + // enable gpio output + ldr r3, sio_base + mov r0, #1 + lsl r0, r0, #25 // gpio25 + str r0, [r3, #0x24] // SIO: GPIO_OE + + // blink led on gpio25 + ldr r4, sio_base + mov r5, r0 +loop: + str r5, [r4, #0x1c] // SIO_GPIO_OUT_XOR + bl delay + b loop + +delay: + mov r0, #1 + lsl r0, r0, #20 +delay_loop: + sub r0, r0, #1 + bne delay_loop + bx lr + +// literals + .align 2 +atomic_clr: + .word 0x00003000 +resets_base: + .word 0x4000c000 +io_bank0_base: + .word 0x40014000 +sio_base: + .word 0xd0000000 + diff --git a/ex3/Makefile b/ex3/Makefile @@ -1,6 +1,6 @@ +CC = arm-none-eabi-gcc AS = arm-none-eabi-as LD = arm-none-eabi-ld -CC = arm-none-eabi-gcc OBJCOPY = arm-none-eabi-objcopy BINCRC = ../tools/bincrc BIN2UF2 = ../tools/bin2uf2 diff --git a/ex3/boot2.s b/ex3/boot2.s @@ -11,24 +11,23 @@ boot2: blx r2 blx r0 - // copy main code to SRAM - ldr r0, =start_text + // load main program to sram + ldr r0, =text_start ldr r1, =text_size mov r2, #0 - ldr r3, rom_base - ldr r4, sram_base + ldr r3, sram_base copy: - cmp r2, r1 + cmp r1, r2 beq end - ldr r5, [r0, r2] - str r5, [r4, r2] + ldr r4, [r0, r2] + str r4, [r3, r2] add r2, #4 b copy end: - // set vector table + // setup vector table ldr r0, =vectors - ldr r1, m0plus_vtor + ldr r1, m0plus_vtor str r0, [r1, #0] // M0PLUS: VTOR ldr r1, [r0, #4] // entry point ldr r0, [r0, #0] // stack pointer diff --git a/ex3/main.c b/ex3/main.c @@ -6,7 +6,6 @@ void set_alarm(unsigned int); int main(void) { init(); - puts("C file"); return 0; } @@ -15,11 +14,9 @@ isr_svcall(void) { puts("svcall"); } -int count = 0; - void isr_alarm(void) { - printh(count++); - puts("alarm fired"); + char *msg = "hello"; + puts(msg); set_alarm(1000 * 1000); } diff --git a/ex3/memmap.ld b/ex3/memmap.ld @@ -12,11 +12,18 @@ SECTIONS } > FLASH .vectors : { *(.vectors) + . += SIZEOF(.vectors); + . = ALIGN(2); + text_start = .; } > FLASH - start_text = .; .text : { *(.text) } > SRAM AT > FLASH text_size = SIZEOF(.text); + .rodata : { + *(.rodata) + } > SRAM AT > FLASH + text_size += SIZEOF(.rodata); + text_size = 0x300; } diff --git a/ex3/start.s b/ex3/start.s @@ -1,6 +1,7 @@ .cpu cortex-m0plus .thumb + .align 8 .section .vectors .global vectors vectors: @@ -305,7 +306,7 @@ bled: str r5, [r4, #0x18] // SIO: GPIO_OUT_XOR bl delay pop {r0, r1, r2, r3, r4, r5, pc} - + delay: push {r0} mov r0, #1