Skip to content

Commit

Permalink
[sw/lib] restore previous registers in neorv32_xirq_get_num function
Browse files Browse the repository at this point in the history
neorv32_xirq_get_num didn't restore previous CSR_MIE and
NEORV32_XIRQ->EIE register and could cause unexpected side effect. So we save them before writing test bits and then restore them finally.
  • Loading branch information
donlon committed Oct 14, 2024
1 parent e688424 commit ce96e43
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion sw/lib/source/neorv32_xirq.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,23 @@ void neorv32_xirq_global_disable(void) {
**************************************************************************/
int neorv32_xirq_get_num(void) {

uint32_t mask;
uint32_t prev_mie, prev_xirq_eie, mask;
int i, cnt;

if (neorv32_xirq_available()) {

// save previous registers
prev_mie = neorv32_cpu_csr_read(CSR_MIE);
prev_xirq_eie = NEORV32_XIRQ->EIE;

neorv32_cpu_csr_clr(CSR_MIE, 1 << XIRQ_FIRQ_ENABLE); // make sure XIRQ cannot fire
NEORV32_XIRQ->EIE = 0xffffffffU; // try to set all enable bits
mask = NEORV32_XIRQ->EIE; // read back actually set flags

// restore previous registers
NEORV32_XIRQ->EIE = prev_xirq_eie;
neorv32_cpu_csr_write(CSR_MIE, prev_mie);

// count set bits
cnt = 0;
for (i=0; i<32; i++) {
Expand Down

0 comments on commit ce96e43

Please # to comment.