Nut/OS  4.10.3
API Reference
ih_at91pio.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2005-2006 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 
00034 /*
00035  * $Log$
00036  * Revision 1.5  2008/08/11 06:59:10  haraldkipp
00037  * BSD types replaced by stdint types (feature request #1282721).
00038  *
00039  * Revision 1.4  2008/07/26 09:43:01  haraldkipp
00040  * Added support for retrieving and setting the interrupt mode.
00041  *
00042  * Revision 1.3  2006/07/05 07:56:34  haraldkipp
00043  * Interrupt handler will be included only, if the related interrupt
00044  * ID is defined in the platform specific header file.
00045  *
00046  * Revision 1.2  2006/06/28 17:10:15  haraldkipp
00047  * Include more general header file for ARM.
00048  *
00049  * Revision 1.1  2005/10/24 08:56:09  haraldkipp
00050  * First check in.
00051  *
00052  */
00053 
00054 #include <arch/arm.h>
00055 #include <dev/irqreg.h>
00056 
00057 #if defined(PIO_ID)
00058 
00059 #ifndef NUT_IRQPRI_PIO
00060 #define NUT_IRQPRI_PIO  4
00061 #endif
00062 
00063 static int PortIoIrqCtl(int cmd, void *param);
00064 
00065 IRQ_HANDLER sig_PIO = {
00066 #ifdef NUT_PERFMON
00067     0,                  /* Interrupt counter, ir_count. */
00068 #endif
00069     NULL,               /* Passed argument, ir_arg. */
00070     NULL,               /* Handler subroutine, ir_handler. */
00071     PortIoIrqCtl        /* Interrupt control, ir_ctl. */
00072 };
00073 
00077 static void PortIoIrqEntry(void) __attribute__ ((naked));
00078 void PortIoIrqEntry(void)
00079 {
00080     IRQ_ENTRY();
00081 #ifdef NUT_PERFMON
00082     sig_PIO.ir_count++;
00083 #endif
00084     if (sig_PIO.ir_handler) {
00085         (sig_PIO.ir_handler) (sig_PIO.ir_arg);
00086     }
00087     IRQ_EXIT();
00088 }
00089 
00107 static int PortIoIrqCtl(int cmd, void *param)
00108 {
00109     int rc = 0;
00110     unsigned int *ival = (unsigned int *)param;
00111     int_fast8_t enabled = inr(AIC_IMR) & _BV(PIO_ID);
00112 
00113     /* Disable interrupt. */
00114     if (enabled) {
00115         outr(AIC_IDCR, _BV(PIO_ID));
00116     }
00117 
00118     switch(cmd) {
00119     case NUT_IRQCTL_INIT:
00120         /* Set the vector. */
00121         outr(AIC_SVR(PIO_ID), (unsigned int)PortIoIrqEntry);
00122         /* Initialize to edge triggered with defined priority. */
00123         outr(AIC_SMR(PIO_ID), AIC_SRCTYPE_INT_EDGE_TRIGGERED | NUT_IRQPRI_PIO);
00124         /* Clear interrupt */
00125         outr(AIC_ICCR, _BV(PIO_ID));
00126         break;
00127     case NUT_IRQCTL_STATUS:
00128         if (enabled) {
00129             *ival |= 1;
00130         }
00131         else {
00132             *ival &= ~1;
00133         }
00134         break;
00135     case NUT_IRQCTL_ENABLE:
00136         enabled = 1;
00137         break;
00138     case NUT_IRQCTL_DISABLE:
00139         enabled = 0;
00140         break;
00141     case NUT_IRQCTL_GETMODE:
00142         {
00143             unsigned int val = inr(AIC_SMR(PIO_ID)) & AIC_SRCTYPE;
00144             if (val == AIC_SRCTYPE_INT_LEVEL_SENSITIVE || val == AIC_SRCTYPE_EXT_HIGH_LEVEL) {
00145                 *ival = NUT_IRQMODE_LEVEL;
00146             } else  {
00147                 *ival = NUT_IRQMODE_EDGE;
00148             }
00149         }
00150         break;
00151     case NUT_IRQCTL_SETMODE:
00152         if (*ival == NUT_IRQMODE_LEVEL) {
00153             outr(AIC_SMR(PIO_ID), (inr(AIC_SMR(PIO_ID)) & ~AIC_SRCTYPE) | AIC_SRCTYPE_INT_LEVEL_SENSITIVE);
00154         } else if (*ival == NUT_IRQMODE_EDGE) {
00155             outr(AIC_SMR(PIO_ID), (inr(AIC_SMR(PIO_ID)) & ~AIC_SRCTYPE) | AIC_SRCTYPE_INT_EDGE_TRIGGERED);
00156         } else  {
00157             rc = -1;
00158         }
00159         break;
00160     case NUT_IRQCTL_GETPRIO:
00161         *ival = inr(AIC_SMR(PIO_ID)) & AIC_PRIOR;
00162         break;
00163     case NUT_IRQCTL_SETPRIO:
00164         outr(AIC_SMR(PIO_ID), (inr(AIC_SMR(PIO_ID)) & ~AIC_PRIOR) | *ival);
00165         break;
00166 #ifdef NUT_PERFMON
00167     case NUT_IRQCTL_GETCOUNT:
00168         *ival = (unsigned int)sig_PIO.ir_count;
00169         sig_PIO.ir_count = 0;
00170         break;
00171 #endif
00172     default:
00173         rc = -1;
00174         break;
00175     }
00176 
00177     /* Enable interrupt. */
00178     if (enabled) {
00179         outr(AIC_IECR, _BV(PIO_ID));
00180     }
00181     return rc;
00182 }
00183 
00184 #endif /* PIO_ID */