diff --git a/cores/arduino/board.cpp b/cores/arduino/board.cpp index bf1dc01..a8b8703 100644 --- a/cores/arduino/board.cpp +++ b/cores/arduino/board.cpp @@ -1,11 +1,15 @@ #include "board.h" #include "mik32_hal_pcc.h" +#include "mik32_hal_irq.h" #include "Arduino.h" // --------------------- init --------------------- // // called before setup() void pre_init(void) { + // set irq vector to ram region + write_csr(mtvec, 0x020000c0); + HAL_Init(); // gpio clock @@ -23,6 +27,8 @@ void post_init(void) { // enable global interrupts by default interrupts(); + + ((GPIO_0)->SET = (GPIO_PIN_0)); } // --------------------- other --------------------- // diff --git a/cores/arduino/mik32/shared/ldscripts/spifi_cpp.ld b/cores/arduino/mik32/shared/ldscripts/spifi_cpp.ld index 18bcb89..20ded45 100644 --- a/cores/arduino/mik32/shared/ldscripts/spifi_cpp.ld +++ b/cores/arduino/mik32/shared/ldscripts/spifi_cpp.ld @@ -21,8 +21,8 @@ SECTIONS { *crt0.S.o(.text .text.*) *(.text.smallsysteminit) *(.text.SmallSystemInit) - . = ORIGIN(rom) + 0xC0; - KEEP(*crt0.S.o(.trap_text)) + /*. = ORIGIN(rom) + 0xC0;*/ + /*KEEP(*crt0.S.o(.trap_text))*/ *(.text) *(.text.*) @@ -50,16 +50,17 @@ SECTIONS { AT( __TEXT_END__ ) { PROVIDE(__DATA_START__ = .); _gp = .; + . = ORIGIN(ram) + 0xC0; + KEEP(*crt0.S.o(.trap_text)) *(.srodata.cst16) *(.srodata.cst8) *(.srodata.cst4) *(.srodata.cst2) *(.srodata*) *(.sdata .sdata.* .sdata* .sdata*.* .gnu.linkonce.s.*) *(.data .data.*) - *(.ramfunc) . = ALIGN(CL_SIZE); - } >ram - + PROVIDE(__DATA_END__ = .); + } >ram + __DATA_IMAGE_START__ = LOADADDR(.data); __DATA_IMAGE_END__ = LOADADDR(.data) + SIZEOF(.data); - ASSERT(__DATA_IMAGE_END__ < ORIGIN(rom) + LENGTH(rom), "Data image overflows rom section") /* thread-local data segment */ .tdata : { @@ -85,10 +86,26 @@ SECTIONS { .bss : { *(.bss .bss.*) + *(COMMON) . = ALIGN(CL_SIZE); PROVIDE(__BSS_END__ = .); } >ram + /* Code intended to be copied to REGION_RAM before execution */ + .ram_text : + AT( ALIGN(__DATA_IMAGE_END__, CL_SIZE) ) { + PROVIDE(__RAM_TEXT_START__ = .); + *(.ram_text) + . = ALIGN(CL_SIZE); + PROVIDE(__RAM_TEXT_END__ = .); + } > ram + + __RAM_TEXT_IMAGE_START__ = LOADADDR(.ram_text); + __RAM_TEXT_IMAGE_END__ = LOADADDR(.ram_text) + SIZEOF(.ram_text); + ASSERT(__RAM_TEXT_IMAGE_END__ < ORIGIN(rom) + LENGTH(rom), "rom segment overflows") + + ASSERT(__RAM_TEXT_END__ < ORIGIN(ram) + LENGTH(ram) - STACK_SIZE - HEAP_SIZE, "REGION_RAM section overflows") + .heap : { PROVIDE(__heap_start = .); . += HEAP_SIZE; diff --git a/cores/arduino/mik32/shared/runtime/crt0.S b/cores/arduino/mik32/shared/runtime/crt0.S index e058b76..e4185cd 100644 --- a/cores/arduino/mik32/shared/runtime/crt0.S +++ b/cores/arduino/mik32/shared/runtime/crt0.S @@ -62,6 +62,11 @@ memset_2: _start: + li t0, 128000 + start_loop_delay: + nop + addi t0, t0, -1 + bnez t0, start_loop_delay # Init stack and global pointer # la_abs sp, __C_STACK_TOP__ @@ -73,20 +78,19 @@ _start: la_abs a2, __DATA_IMAGE_END__ la_abs a3, __DATA_START__ memcpy a1, a2, a3, t0 + + # Init ramfunc + # + la_abs a1, __RAM_TEXT_IMAGE_START__ + la_abs a2, __RAM_TEXT_IMAGE_END__ + la_abs a3, __RAM_TEXT_START__ + memcpy a1, a2, a3, t0 # Clear bss # la_abs a1, __BSS_START__ la_abs a2, __BSS_END__ memset a1, a2, zero - - #ifdef MIK32V0 - # Enable pad_config clocking - # - li t0, (1 << 3) - li t1, 0x50014 - sw t0, (t1) - #endif jalr_abs ra, SmallSystemInit jalr_abs ra, SystemInit diff --git a/cores/arduino/trap_handler.c b/cores/arduino/trap_handler.c index 5479e39..4495da8 100644 --- a/cores/arduino/trap_handler.c +++ b/cores/arduino/trap_handler.c @@ -14,13 +14,13 @@ void __attribute__((weak)) wire_handler_wrapper(void) extern void softSerial_handler_wrapper(void); // ---------------------------------------------- // -void trap_handler(void) +void __attribute__((noinline, section(".ram_text"))) trap_handler(void) { - ((GPIO_0)->SET = (GPIO_PIN_8)); + ((GPIO_0)->CLEAR = (GPIO_PIN_0)); // gpio interrupt if (EPIC_CHECK_GPIO_IRQ()) - // gpio_interrupts_handler(); - softSerial_handler_wrapper(); + gpio_interrupts_handler(); + // softSerial_handler_wrapper(); // tone timer interrupt if (EPIC_CHECK_TIMER16_1()) @@ -40,5 +40,5 @@ void trap_handler(void) // reset all interrupts HAL_EPIC_Clear(0xFFFFFFFF); - ((GPIO_0)->CLEAR = (GPIO_PIN_8)); + ((GPIO_0)->SET = (GPIO_PIN_0)); } diff --git a/libraries/SoftwareSerial/src/SoftwareSerial.h b/libraries/SoftwareSerial/src/SoftwareSerial.h index 66d798a..a7584de 100644 --- a/libraries/SoftwareSerial/src/SoftwareSerial.h +++ b/libraries/SoftwareSerial/src/SoftwareSerial.h @@ -74,7 +74,7 @@ private: static uint32_t subtract_cap(uint32_t num, uint16_t sub); // private static method for timing - static void tunedDelay(uint32_t delayTicks) __attribute__((noinline, section(".ramfunc"))); + static void tunedDelay(uint32_t delayTicks) __attribute__((noinline, section(".ram_text"))); public: // public methods @@ -94,7 +94,7 @@ public: } int peek(); - virtual size_t write(uint8_t byte) __attribute__((noinline, section(".ramfunc"))); + virtual size_t write(uint8_t byte) __attribute__((noinline, section(".ram_text"))); virtual int read(); virtual int available(); virtual void flush(); diff --git a/platform.txt b/platform.txt index 9ee1959..547e26d 100644 --- a/platform.txt +++ b/platform.txt @@ -18,8 +18,7 @@ compiler.MIK32_Amur.extra_include= "-I{build.core.path}/avr/" "-I{build.core.pat compiler.define="-DMCU_{build.mcu}" "-DF_CPU={build.f_cpu}" "-DARDUINO={runtime.ide.version}" "-DARDUINO_{build.board}" "-DARDUINO_ARCH_{build.arch}" "-DMIK32V2" "-D__ELBEAR_ACE_UNO__" "-D__RISC_V__" "-D__ARDUINO_ARC__" "-D__CPU_ARC__" build.flags.optimize=-Os -# build.ldscript=spifi_cpp.ld -build.ldscript=ram.ld +build.ldscript=spifi_cpp.ld # Compiler flags compiler.warning_flags=-Wunused -Wuninitialized