Nut/OS  4.10.3
API Reference
ih_int2.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2001-2005 by egnite Software GmbH. All rights reserved.
00003  *
00004  * Redistribution and use in source and binary forms, with or without
00005  * modification, are permitted provided that the following conditions
00006  * are met:
00007  *
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the copyright holders nor the names of
00014  *    contributors may be used to endorse or promote products derived
00015  *    from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY EGNITE SOFTWARE GMBH AND CONTRIBUTORS
00018  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00019  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00020  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL EGNITE
00021  * SOFTWARE GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00022  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00023  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
00024  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
00025  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00026  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
00027  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00028  * SUCH DAMAGE.
00029  *
00030  * For additional information see http://www.ethernut.de/
00031  *
00032  */
00033 
00076 #include <dev/irqreg.h>
00077 
00082 
00083 static int AvrInterrupt2Ctl(int cmd, void *param);
00084 
00085 IRQ_HANDLER sig_INTERRUPT2 = {
00086 #ifdef NUT_PERFMON
00087     0,                          /* Interrupt counter, ir_count. */
00088 #endif
00089     NULL,                       /* Passed argument, ir_arg. */
00090     NULL,                       /* Handler subroutine, ir_handler. */
00091     AvrInterrupt2Ctl            /* Interrupt control, ir_ctl. */
00092 };
00093 
00111 static int AvrInterrupt2Ctl(int cmd, void *param)
00112 {
00113     int rc = 0;
00114     unsigned int *ival = (unsigned int *) param;
00115     int_fast8_t enabled = bit_is_set(EIMSK, INT2);
00116 
00117     /* Disable interrupt. */
00118     cbi(EIMSK, INT2);
00119 
00120     switch (cmd) {
00121     case NUT_IRQCTL_INIT:
00122 #ifdef __AVR_ENHANCED__
00123         /* Initialize to low level triggered. */
00124         cbi(EICRA, ISC20);
00125         cbi(EICRA, ISC21);
00126 #endif
00127     case NUT_IRQCTL_CLEAR:
00128 #ifdef __AVR_ENHANCED__
00129         /* Clear any pending interrupt. */
00130         outb(EIFR, _BV(INTF2));
00131 #endif /* __AVR_ENHANCED__ */
00132         break;
00133     case NUT_IRQCTL_STATUS:
00134 #ifdef __AVR_ENHANCED__
00135         if (bit_is_set(EIFR, INTF2)) {
00136             *ival = 1;
00137         } else {
00138             *ival = 0;
00139         }
00140 #else /* !__AVR_ENHANCED__ */
00141         *ival = 0;
00142 #endif /* __AVR_ENHANCED__ */
00143         if (enabled) {
00144             *ival |= 0x80;
00145         }
00146         break;
00147     case NUT_IRQCTL_ENABLE:
00148         enabled = 1;
00149         break;
00150     case NUT_IRQCTL_DISABLE:
00151         enabled = 0;
00152         break;
00153     case NUT_IRQCTL_GETMODE:
00154 #ifdef __AVR_ENHANCED__
00155         {
00156             uint8_t bval = inb(EICRA) & (_BV(ISC21) | _BV(ISC20));
00157             if (bval == _BV(ISC21)) {
00158                 *ival = NUT_IRQMODE_FALLINGEDGE;
00159             } else if (bval == _BV(ISC21 | ISC20)) {
00160                 *ival = NUT_IRQMODE_RISINGEDGE;
00161             } else {
00162                 *ival = NUT_IRQMODE_LOWLEVEL;
00163             }
00164         }
00165 #else /* !__AVR_ENHANCED__ */
00166         *ival = NUT_IRQMODE_LOWLEVEL;
00167 #endif /* __AVR_ENHANCED__ */
00168         break;
00169     case NUT_IRQCTL_SETMODE:
00170 #ifdef __AVR_ENHANCED__
00171         if (*ival == NUT_IRQMODE_LOWLEVEL) {
00172             cbi(EICRA, ISC20);
00173             cbi(EICRA, ISC21);
00174         } else if (*ival == NUT_IRQMODE_FALLINGEDGE) {
00175             cbi(EICRA, ISC20);
00176             sbi(EICRA, ISC21);
00177         } else if (*ival == NUT_IRQMODE_RISINGEDGE) {
00178             sbi(EICRA, ISC20);
00179             sbi(EICRA, ISC21);
00180         } else {
00181             rc = -1;
00182         }
00183 #else /* !__AVR_ENHANCED__ */
00184         if (*ival != NUT_IRQMODE_LOWLEVEL) {
00185             rc = -1;
00186         }
00187 #endif /* __AVR_ENHANCED__ */
00188         break;
00189     case NUT_IRQCTL_GETPRIO:
00190         *ival = 2;
00191         break;
00192 #ifdef NUT_PERFMON
00193     case NUT_IRQCTL_GETCOUNT:
00194         *ival = (unsigned int) sig_INTERRUPT2.ir_count;
00195         sig_INTERRUPT2.ir_count = 0;
00196         break;
00197 #endif
00198     default:
00199         rc = -1;
00200         break;
00201     }
00202 
00203     /* Enable interrupt. */
00204     if (enabled) {
00205         sbi(EIMSK, INT2);
00206     }
00207     return rc;
00208 }
00209 
00213 #ifdef __IMAGECRAFT__
00214 #pragma interrupt_handler SIG_INTERRUPT2:iv_INT2
00215 #endif
00216 NUTSIGNAL(SIG_INTERRUPT2, sig_INTERRUPT2)
00217 
00218 
00219