From 2d75a9be32e2510ff32d331c33178c4625a53054 Mon Sep 17 00:00:00 2001 From: Vegard Storheil Eriksen Date: Sat, 10 Sep 2022 17:10:09 +0200 Subject: riscv: Add critical section. --- interrupt/critical_section.h | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 interrupt/critical_section.h (limited to 'interrupt') diff --git a/interrupt/critical_section.h b/interrupt/critical_section.h new file mode 100644 index 0000000..aec03a5 --- /dev/null +++ b/interrupt/critical_section.h @@ -0,0 +1,37 @@ +#pragma once + +#include + +#ifdef CORTEX_M +struct critical_section { + uint32_t primask; + + critical_section() { + asm volatile("mrs %0, primask" : "=r" (primask)); + + asm volatile("cpsid i"); + + asm volatile("dmb"); + } + + ~critical_section() { + asm volatile("dmb"); + + asm volatile("msr primask, %0" :: "r" (primask)); + } +}; +#endif + +#ifdef RISCV +struct critical_section { + uint32_t mie; + + critical_section() { + asm volatile("csrrw %0, mie, x0" : "=r" (mie)); + } + + ~critical_section() { + asm volatile("csrw mie, %0" :: "r" (mie)); + } +}; +#endif -- cgit v1.2.3