Peripheral Interrupt Handler and Processing Task

In an embedded system, the most efficient method of detecting sensor and other data input is through peripheral interrupts. An interrupt handler, or Interrupt Service Routine (ISR), is called when a hardware device receives data (e.g.: a character arrives at a UART receive register). The ISR reads the data from the hardware device, and the data can then be processed by the firmware. Because interrupts preempt other code from running, in order to minimize performance impact on the rest of the system. the ISR should do its job as quickly as possible and leave the more complex data processing to code that is not running in an interrupt context.

 

The following demonstrates this setup. It’s similar to the previous reader and writer example. In this example, a UART peripheral interrupt handler generates the data for the writer task. The reader task remains exactly the same as in the previous example. This program then implements a simple echo terminal program. While this use is trivial, as the reader task does not perform much processing, the basic idea is very powerful and applicable to all sort of programming tasks. For example, in a TCP/IP stack, a low level ethernet interrupt driver can fetch data whenever it is presented in the Ethernet hardware, and then pass it onto a high level TCP task to form a TCP packet, before sending it off to the TCP/IP client.

 

void UART2_RX_ISR(void)

    {

    static int ch;

 

    ch = getchar();

    REXIS_MailboxTryPostFromISR(mbox1, &ch);

    NVIC_ClearPendingIRQ(USART2_IRQn);

    }

 

The getchar() call reads the data off the UART hardware register. As this handler is only called when there is data available, the getchar() call will return immediately with the data. The ISR posts the character as a message to the mailbox similar to the previous example. The reader task is exactly the same as before, and will print out all characters received.

 

Note: Only a small subset of the REXIS API functions are callable from within an ISR, and they all end with “…FromISR” in their names.