Skip to content

Commit

Permalink
sw/main: guard gpio access with mutex lock
Browse files Browse the repository at this point in the history
  • Loading branch information
NikLeberg committed Aug 4, 2023
1 parent 6ff8c82 commit 56223e4
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion sw/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,24 @@ void delay_ms(uint32_t time_ms) {
: [cnt_r] "r"(iterations));
}

static uint32_t lock_val = 0; // 0 = unlocked, 1 = locked
void lock(void) {
uint32_t val, status;
do {
val = neorv32_cpu_load_reservate_word((uint32_t)&lock_val);
if (val == 1) { // stil locked, store same value back
status = neorv32_cpu_store_conditional_word((uint32_t)&lock_val, val);
} else { // unlocked, set lock
status = neorv32_cpu_store_conditional_word((uint32_t)&lock_val, 1);
}
} while (val == 1 || status == 1);
}
void unlock(void) {
// We could write directly to lock_val but then the dcache could interfere.
// Here we choose to use lr/sc again as it always bypasses the dcache.
(void)neorv32_cpu_load_reservate_word((uint32_t)&lock_val);
(void)neorv32_cpu_store_conditional_word((uint32_t)&lock_val, 0);
}

/**
* @brief Main function
Expand All @@ -48,9 +66,12 @@ int main() {

// let each hart blink its own led
uint32_t hart_id = neorv32_cpu_csr_read(CSR_MHARTID);
uint32_t delay = (128 << hart_id);
for (;;) {
lock();
neorv32_gpio_pin_toggle(hart_id);
delay_ms((hart_id + 1) * 100);
unlock();
delay_ms(delay);
}

// this should never be reached
Expand Down

0 comments on commit 56223e4

Please sign in to comment.