riscv-rust / riscv-rust-quickstart

A template for building Rust applications for HiFive1 boards
205 stars 24 forks source link

Use HAL for interrupt instead of using unsafe blocks #7

Closed rmsthebest closed 5 years ago

rmsthebest commented 5 years ago

I'm not confident in my embedded rust skills yet, but this seems to achieve the same result without resorting to unsafe register stuff.

Disasm commented 5 years ago

Nice catch, thanks! However, I got a build error:

error[E0133]: call to unsafe function is unsafe and requires unsafe function or block
  --> examples/leds_blink.rs:85:13
   |
85 |             riscv::asm::wfi();
   |             ^^^^^^^^^^^^^^^^^ call to unsafe function
   |
   = note: consult the function's documentation for information on how to avoid undefined behavior

error: aborting due to previous error
rmsthebest commented 5 years ago

That's embarrassing :) I only saw and removed the second unsafe block just before I pushed. Then I rebuilt with cargo build rather than cargo build --examples

I've removed the wfi, which results in a working program with same led blinking. But I guess it means we are reading the register a lot instead of waiting for an interrupt to wake the processor.

Not sure what is best

Disasm commented 5 years ago

A loop without wfi is less efficient, because it burns energy for nothing.

Even though I like that no-unsafe-attitude, yours is too radical :smile: There is nothing bad in having unsafe in embedded code, where it is needed. All your fixes in fact reduced one unsafe code (in main) to another unsafe code (in HAL), however it's great you noticed that HAL already implements functionality needed.

rmsthebest commented 5 years ago

I agree =)