30.12.2014 Views

QDK PIC24/dsPIC-C30 - Quantum Leaps

QDK PIC24/dsPIC-C30 - Quantum Leaps

QDK PIC24/dsPIC-C30 - Quantum Leaps

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>QDK</strong><br />

<strong>PIC24</strong>/<strong>dsPIC</strong>-<strong>C30</strong><br />

www.state-machine.com/pic<br />

"and.w w0,#7,w0 \n" \<br />

"mov.w #1,w1 \n" \<br />

"sl w1,w0,w0 \n" \<br />

"ior.b #1,w0 \n" \<br />

"com.b w0,w0 \n" \<br />

"disi #0x3FFF \n" \<br />

"and.b _QK_intNest_" : "=r"(this_sr) : : "w0", "w1"); \<br />

if (QK_intNest_ == 0) { \<br />

uint8_t p = QK_schedPrio_(); \<br />

if (p != (uint8_t)0) { \<br />

__asm__ volatile ("clr.b SR"); \<br />

QK_sched_(p); \<br />

__asm__ volatile ("mov.w %0,SR" : : "r"(this_sr)); \<br />

} \<br />

} \<br />

__asm__ volatile ("disi #0x0000"); \<br />

} while (0);<br />

#include "qk.h" /* QK platform-independent public interface */<br />

(1) The macro QK_ISR() defines the ISR attributes, as required by the MPLAB <strong>C30</strong> compiler. The<br />

macro is designed to be before the actual ISR signature, as will be illustrated in Listing 8. The<br />

argument psv_ controls PSV usage in the ISR and can be either auto_psv or no_auto_psv (see<br />

Section 3.3.1).<br />

(2) The macro QK_ISR() then uses the attribute __preprologue__ to define the specific interrupt entry<br />

sequence in assembly, which the compiler copies verbatim before any generated ISR code.<br />

(3) The macro QK_ISR_EXIT() must be invoked as the last instruction of every ISR of IPL 1..6. Again,<br />

the usage of this macro will be illustrated in Listing 8.<br />

4.2 ISRs with the Preemptive QK Kernel<br />

Even though the QK interrupt entry/exit macros are somewhat involved, their use is actually very simple<br />

and enables very straightforward coding ISRs in C, without any need to use assembly. The following<br />

Listing 8 shows how to write ISRs for the preemptive QK kernel:<br />

NOTE: The QK interrupt entry and exit macros QK_ISR() and QK_ISR_EXIT() work only with the<br />

optimization level O1 or higher (O2, O3, or Os) in the <strong>C30</strong> compiler. The MPLAB <strong>C30</strong> compiler will<br />

report a compile-time error “'asm' operand requires impossible reload” when no optimization is<br />

used.<br />

Listing 8: T2Interrupt ISR for QK<br />

(1) QK_ISR(no_auto_psv) _T2Interrupt() {<br />

_T2IF = 0; /* clear Timer 2 interrupt flag */<br />

#ifdef Q_SPY<br />

(2) l_tickTime += BSP_TMR2_PERIOD; /* account for TMR2 overflow */<br />

#endif<br />

QF_TICK(&l_T2Interrupt); /* handle all armed time events in QF */<br />

Copyright © <strong>Quantum</strong> <strong>Leaps</strong>, LLC. All Rights Reserved.<br />

19 of 35

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!