summaryrefslogtreecommitdiff
path: root/interrupt/critical_section.h
blob: aec03a59341367268e125f1f6a3713e6c2a7d8b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#pragma once

#include <cstdint>

#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