github.com/tinygo-org/tinygo@v0.31.3-0.20240404173401-90b0bf646c27/src/runtime/interrupt/interrupt_cortexm.go (about)

     1  //go:build cortexm
     2  
     3  package interrupt
     4  
     5  import (
     6  	"device/arm"
     7  )
     8  
     9  // Enable enables this interrupt. Right after calling this function, the
    10  // interrupt may be invoked if it was already pending.
    11  func (irq Interrupt) Enable() {
    12  	arm.EnableIRQ(uint32(irq.num))
    13  }
    14  
    15  // Disable disables this interrupt.
    16  func (irq Interrupt) Disable() {
    17  	arm.DisableIRQ(uint32(irq.num))
    18  }
    19  
    20  // SetPriority sets the interrupt priority for this interrupt. A lower number
    21  // means a higher priority. Additionally, most hardware doesn't implement all
    22  // priority bits (only the uppoer bits).
    23  //
    24  // Examples: 0xff (lowest priority), 0xc0 (low priority), 0x00 (highest possible
    25  // priority).
    26  func (irq Interrupt) SetPriority(priority uint8) {
    27  	arm.SetPriority(uint32(irq.num), uint32(priority))
    28  }
    29  
    30  // State represents the previous global interrupt state.
    31  type State uintptr
    32  
    33  // Disable disables all interrupts and returns the previous interrupt state. It
    34  // can be used in a critical section like this:
    35  //
    36  //	state := interrupt.Disable()
    37  //	// critical section
    38  //	interrupt.Restore(state)
    39  //
    40  // Critical sections can be nested. Make sure to call Restore in the same order
    41  // as you called Disable (this happens naturally with the pattern above).
    42  func Disable() (state State) {
    43  	return State(arm.DisableInterrupts())
    44  }
    45  
    46  // Restore restores interrupts to what they were before. Give the previous state
    47  // returned by Disable as a parameter. If interrupts were disabled before
    48  // calling Disable, this will not re-enable interrupts, allowing for nested
    49  // cricital sections.
    50  func Restore(state State) {
    51  	arm.EnableInterrupts(uintptr(state))
    52  }
    53  
    54  // In returns whether the system is currently in an interrupt.
    55  func In() bool {
    56  	// The VECTACTIVE field gives the instruction vector that is currently
    57  	// active (in handler mode), or 0 if not in an interrupt.
    58  	// Documentation:
    59  	// https://developer.arm.com/documentation/dui0497/a/cortex-m0-peripherals/system-control-block/interrupt-control-and-state-register
    60  	vectactive := uint8(arm.SCB.ICSR.Get())
    61  	return vectactive != 0
    62  }