Nut/OS  4.10.3
API Reference
ih_int0.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 AvrInterrupt0Ctl(int cmd, void *param);
00084 
00085 IRQ_HANDLER sig_INTERRUPT0 = {
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     AvrInterrupt0Ctl            /* Interrupt control, ir_ctl. */
00092 };
00093 
00111 static int AvrInterrupt0Ctl(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, INT0);
00116 
00117     /* Disable interrupt. */
00118     cbi(EIMSK, INT0);
00119 
00120     switch (cmd) {
00121     case NUT_IRQCTL_INIT:
00122 #ifdef __AVR_ENHANCED__
00123         /* Initialize to low level triggered. */
00124         cbi(EICRA, ISC00);
00125         cbi(EICRA, ISC01);
00126 #endif
00127     case NUT_IRQCTL_CLEAR:
00128 #ifdef __AVR_ENHANCED__
00129         /* Clear any pending interrupt. */
00130         outb(EIFR, _BV(INTF0));
00131 #endif /* __AVR_ENHANCED__ */
00132         break;
00133     case NUT_IRQCTL_STATUS:
00134 #ifdef __AVR_ENHANCED__
00135         if (bit_is_set(EIFR, INTF0)) {
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(ISC01) | _BV(ISC00));
00157             if (bval == 0) {
00158                 *ival = NUT_IRQMODE_LOWLEVEL;
00159             }
00160             else if (bval == (_BV(ISC01) | _BV(ISC00))) {
00161                 *ival = NUT_IRQMODE_RISINGEDGE;
00162             } else  {
00163                 *ival = NUT_IRQMODE_FALLINGEDGE;
00164             }
00165         }
00166 #else /* !__AVR_ENHANCED__ */
00167         *ival = NUT_IRQMODE_LOWLEVEL;
00168 #endif /* __AVR_ENHANCED__ */
00169         break;
00170     case NUT_IRQCTL_SETMODE:
00171 #ifdef __AVR_ENHANCED__
00172         if (*ival == NUT_IRQMODE_LOWLEVEL) {
00173             cbi(EICRA, ISC00);
00174             cbi(EICRA, ISC01);
00175         } else if (*ival == NUT_IRQMODE_FALLINGEDGE) {
00176             cbi(EICRA, ISC00);
00177             sbi(EICRA, ISC01);
00178         } else  if (*ival == NUT_IRQMODE_RISINGEDGE) {
00179             sbi(EICRA, ISC00);
00180             sbi(EICRA, ISC01);
00181         } else  {
00182             rc = -1;
00183         }
00184 #else /* !__AVR_ENHANCED__ */
00185         if (*ival != NUT_IRQMODE_LOWLEVEL) {
00186             rc = -1;
00187         }
00188 #endif /* __AVR_ENHANCED__ */
00189         break;
00190     case NUT_IRQCTL_GETPRIO:
00191         *ival = 0;
00192         break;
00193 #ifdef NUT_PERFMON
00194     case NUT_IRQCTL_GETCOUNT:
00195         *ival = (unsigned int) sig_INTERRUPT0.ir_count;
00196         sig_INTERRUPT0.ir_count = 0;
00197         break;
00198 #endif
00199     default:
00200         rc = -1;
00201         break;
00202     }
00203 
00204     /* Enable interrupt. */
00205     if (enabled) {
00206         sbi(EIMSK, INT0);
00207     }
00208     return rc;
00209 }
00210 
00215 #ifdef __IMAGECRAFT__
00216 #pragma interrupt_handler SIG_INTERRUPT0:iv_INT0
00217 #endif
00218 NUTSIGNAL(SIG_INTERRUPT0, sig_INTERRUPT0)
00219 
00220