diff --git a/include/hal/hmr/hmr_v1.h b/include/hal/hmr/hmr_v1.h index 419f612..0bf2a7f 100644 --- a/include/hal/hmr/hmr_v1.h +++ b/include/hal/hmr/hmr_v1.h @@ -52,6 +52,7 @@ #define DMR_IS_MAIN_CORE(core_id) (DMR_IS_CORE(core_id) && (DMR_CORE_ID(DMR_GROUP_ID(core_id), 0) == core_id)) void pos_hmr_tmr_irq(); +void pos_hmr_tmr_synch(); static inline unsigned int hmr_get_available_config(unsigned int cid) { return pulp_read32(ARCHI_HMR_GLOBAL_ADDR(cid) + HMR_TOP_OFFSET + HMR_REGISTERS_AVAIL_CONFIG_REG_OFFSET); @@ -99,6 +100,11 @@ static inline void hmr_set_dmr_config_all(unsigned int cid, bool rapid_recovery) (rapid_recovery ? 1< this should lock the cores together -// // load sp from tmr reg -// pos_hmr_load_state_from_stack(); -// } +void __attribute__((naked)) pos_hmr_tmr_synch() { + if (TMR_IS_MAIN_CORE(core_id())) { + eu_bar_setup(eu_bar_addr(TMR_BARRIER_ID(TMR_GROUP_ID(core_id()))), TMR_BARRIER_SETUP(TMR_GROUP_ID(core_id()))); + // eu_bar_setup(eu_bar_addr(0), hmr_get_active_cores(0)); + } + pos_hmr_store_state_to_stack(); + + // store sp to hmr core reg + __asm__ __volatile__( + "csrr t0, 0xf14 \n\t" // Read core id + "li t1, " QU(ARCHI_HMR_ADDR + HMR_CORE_OFFSET) " \n\t" + "andi t0, t0, 0x01f \n\t" + "sll t0, t0, " QU(HMR_CORE_SLL) " \n\t" + "add t0, t0, t1 \n\t" + "sw sp, " QU(HMR_CORE_REGS_SP_STORE_REG_OFFSET) "(t0) \n\t" + : : : "memory"); + + // enter barrier -> this should lock the cores together + eu_bar_trig_wait_clr(eu_bar_addr(TMR_BARRIER_ID(TMR_GROUP_ID(core_id())))); + + // several nops to delay and allow for core reset + __asm__ __volatile__( + "nop\n\t" + "nop\n\t" + "nop\n\t" + "nop\n\t" + "nop\n\t" + : : : "memory"); + + pos_hmr_tmr_reload(); +} + +int hmr_tmr_critical_section(int (*function_handle)()) { + int ret = 0; + if (TMR_IS_MAIN_CORE(core_id())) { + // enter critical section + hmr_enable_tmr(0, TMR_GROUP_ID(core_id())); + + // do critical stuff + ret += function_handle(); + + // exit critical section + hmr_disable_tmr(0, TMR_GROUP_ID(core_id())); + + } + synch_barrier(); + + return ret; +} // void pos_hmr_tmr_unsync() {