Main Content

Hardware Interrupt

R2026b

Trigger downstream function-call subsystems from interrupt service routine for Teensy 4.0 and 4.1 boards

Since R2024b

  • Arduino Teensy4 Library Hardware Interrupt Block Icon

Libraries:
Simulink Support Package for Arduino Hardware / Advanced / TEENSY4

Description

Add-On Required: This feature requires the Simulink Support Package for Arduino Hardware add-on.

Use the Hardware Interrupt block to create an interrupt service routine (ISR) in the generated code of your Simulink® model. The ISR executes the downstream function-call subsystem associated with event ports of the block. The function call subsystem runs at the same priority as the ISR.

Using this block, you can:

  • Create an ISR on the Arduino® board.

  • Set priority for an ISR.

  • Enable or disable interrupt preemption.

This block generates code only for the ISR and events that you select or specify. To change the configuration, enable the interrupt, and specify triggering options, use the blocks associated with the selected peripherals. For example, to create an ISR for the CAN communication peripheral on the Hardware Interrupt block, your Simulink model must have an On-board CAN Receive or On-board CAN Transmit block.

Supported Arduino Boards

  • Teensy 4.0 (Arduino Compatible)

  • Teensy 4.1 (Arduino Compatible)

Examples

expand all

Configure the Teensy Hardware Interrupt block to handle CAN interrupts generated by the On-board CAN Receive and On-board CAN Transmit blocks.

Block diagram showing a Teensy Hardware Interrupt block configured for CAN interrupts, with its function-call output triggering a Function-Call Subsystem that contains an On-board CAN Receive block for a Teensy board.

Add the Teensy Hardware Interrupt block and a Function-Call subsystem block to your model. To handle the CAN receive interrupts, add an On-board CAN Receive block inside the Function-Call subsystem block. To configure the hardware interrupt to trigger the subsystem, connect the function-call output of the Teensy Hardware Interrupt block to the trigger port of the Function-Call subsystem block.

In the model settings, set Hardware board to either Teensy 4.0 (Arduino Compatible) or Teensy 4.1 (Arduino Compatible).

Configure the On-board CAN Receive block with the appropriate CAN module, bus speed, and message ID for your application.

In the Teensy Hardware Interrupt block parameters, set the Interrupt group to either Controller area network (CAN1), Controller area network (CAN2), or Controller area network (CAN3).

Configure the Teensy Hardware Interrupt block to handle interrupts generated by the same CAN module as the CAN blocks. To do so, set the Interrupt name that corresponds to the CAN module used by the model. For example, use CAN1_Handler when the On-board CAN Receive or On-board CAN Transmit block is configured for Controller area network.

In the Events to serve table, select the CAN receive event. Set the Simulink task priority parameter to control the execution priority of the function-call subsystem.

Enable Run interrupt service routine as atomic unit to prevent preemption by other interrupts while servicing the CAN interrupt.

Configure the Teensy Hardware Interrupt block to handle ADC conversion-complete interrupts generated by the Teensy Analog Input block. The Teensy Hardware Interrupt block responds to the ADC conversion-complete interrupt generated by the ADC peripheral and triggers a function-call subsystem to execute the interrupt-driven action.

Block diagram showing a Teensy Hardware Interrupt block configured for ADC conversion complete interrupts with its function-call output triggering a Function-Call Subsystem. A Teensy Analog Input block with ADC conversion complete interrupt enabled provides the interrupt source on a Teensy board.

Add the Teensy PWM, Teensy Analog Input, Teensy Hardware Interrupt, and Function-Call subsystem blocks to your model. Connect the function-call output of the Teensy Hardware Interrupt block to the trigger port of the Function-Call Subsystem.

In the model settings, set Hardware board to either Teensy 4.0 (Arduino Compatible) or Teensy 4.1 (Arduino Compatible).

In the Teensy PWM block parameters, set Pin number to a PWM-capable Teensy pin. Identify the FlexPWM timer module associated with the selected pin. For more information, see Map PWM Pins to FlexPWM Timer Modules.

In the Teensy Analog Input block parameters, set Pin number 1 to a valid Teensy analog input pin.

Set ADC trigger timer to match the FlexPWM timer used by the Teensy PWM block.

Set ADC trigger event to select when the ADC conversion occurs relative to the PWM cycle.

Select Enable ADC conversion complete interrupt.

In the Teensy Hardware Interrupt block parameters, set Interrupt group to Analog to digital converter (ADC).

Set Interrupt name to ADC_EOC_Channel_1 (or the channel that corresponds to the ADC channel used by the Teensy Analog Input block).

In the Events to serve table, select the ADC RESRDY event to trigger the function-call subsystem when the ADC conversion result is ready.

To prevent preemption by other interrupts while servicing the ADC interrupt, enable Run interrupt service routine as atomic unit.

Configure the Teensy Hardware Interrupt block to handle PWM interrupts generated by the Teensy PWM block. The PWM block configures the FlexPWM peripheral. The Teensy Hardware Interrupt block responds to selected FlexPWM events by triggering function-call subsystems in response to PWM-synchronized events.

Block diagram showing a Constant block providing a duty cycle value to a Teensy PWM block with overflow interrupt enabled, and a Teensy Hardware Interrupt block configured for FlexPWM interrupts with its function-call output triggering a Function-Call Subsystem on a Teensy board.

Add the Teensy PWM, Teensy Hardware Interrupt, and Function-Call subsystem blocks to your model. Connect the function-call output of the Teensy Hardware Interrupt block to the trigger port of the Function-Call subsystem.

In the model settings, set Hardware board to either Teensy 4.0 (Arduino Compatible) or Teensy 4.1 (Arduino Compatible).

In the Teensy PWM block parameters, set Pin number to a PWM-capable Teensy pin. Identify the FlexPWM timer module associated with the selected pin. For more information, see Map PWM Pins to FlexPWM Timer Modules.

Depending on which FlexPWM events you want to handle, enable either Enable overflow/underflow (OVF) interrupt or Enable compare match interrupt in the Teensy PWM block.

In the Teensy Hardware Interrupt block parameters, set Interrupt group to either PWM Interrupt FLEXPWM1_X, PWM Interrupt FLEXPWM2_X, PWM Interrupt FLEXPWM3_X, or PWM Interrupt FLEXPWM4_X.

Set Interrupt name to the handler that corresponds to the FlexPWM module used by the Teensy PWM block. For example, use FLEXPWM2_0_Handler when the Teensy PWM block is configured to use a pin mapped to FlexPWM module 2, sub-channel 0.

In the Events to serve table, select the required FlexPWM events. For example, select FlexPWM2_0_Overflow to handle the PWM overflow interrupt or FlexPWM2_0_A_RisingEdge to handle the PWM rising-edge interrupt.

To prevent preemption by other interrupts while servicing the ADC interrupt, enable Run interrupt service routine as atomic unit.

Extended Examples

Ports

Output

expand all

The block outputs a function call. The number of function call outputs depends on the number of events you select to serve in an ISR. For example, if you select CAN rx and Custom event in the Events to serve section, the block enables CAN rx and Custom event output ports.

Parameters

expand all

This parameter lists all the available ISRs for a particular type of peripheral in an Arduino board. The values available in the Interrupt name parameter change depending on the interrupt group you select in this parameter.

The interrupt name specifies the ISR for the Hardware Interrupt block. This parameter corresponds to the specific entry in the interrupt vector table of the Arduino board. The available ISRs depend on the Interrupt group.

Dependencies

  • To enable a CAN handler interrupt, set Interrupt group to the corresponding CAN value.

  • To enable a FLEXPWM handler interrupt, set Interrupt group to the corresponding PWM interrupt FLEXPWM value.

  • To enable an ADC end-of-conversion handler interrupt, set Interrupt group to the corresponding ADC value.

This read-only parameter indicates the position of the ISR in the interrupt vector table of the Arduino board.

The value you specify in this parameter sets the priority of the downstream function-call subsystem. The task priority in Simulink for the ISR is relative to the priority of the base rate that you set in the Simulink model.

Note

The default priority of the base rate in the Simulink model is 40, with a lower priority value indicating a higher priority. To achieve this, in the model settings, select Higher priority value indicates higher task priority in the Solver pane.

Select one or more events and their respective execution order from the Events to serve group to service events for the Interrupt name that you select. You can select custom events to execute along with the other events. You can also change the execution order of the events.

Select this parameter to block all other interrupts while executing the current ISR(s).

Select this option to clear all the interrupt status flags after servicing each event.

More About

expand all

Extended Capabilities

expand all

Version History

Introduced in R2024b

expand all