Skip to content

[sw/lib] restore previous registers in neorv32_xirq_get_num function to avoid side effects #1057

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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