Nut/OS  4.10.3
API Reference
ih_at91adc.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2001-2005 by EmbeddedIT, 
00003  * Ole Reinhardt <ole.reinhardt@embedded-it.de> All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  *
00009  * 1. Redistributions of source code must retain the above copyright
00010  *    notice, this list of conditions and the following disclaimer.
00011  * 2. Redistributions in binary form must reproduce the above copyright
00012  *    notice, this list of conditions and the following disclaimer in the
00013  *    documentation and/or other materials provided with the distribution.
00014  * 3. Neither the name of the copyright holders nor the names of
00015  *    contributors may be used to endorse or promote products derived
00016  *    from this software without specific prior written permission.
00017  *
00018  * THIS SOFTWARE IS PROVIDED BY EMBEDDED IT AND CONTRIBUTORS
00019  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00020  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00021  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL EMBEDDED IT
00022  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
00023  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
00024  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
00025  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
00026  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
00027  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
00028  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029  *
00030  * For additional information see http://www.ethernut.de/
00031  *
00032  */
00033 
00034 /*
00035  * $Log$
00036  * Revision 1.3  2008/08/11 06:59:09  haraldkipp
00037  * BSD types replaced by stdint types (feature request #1282721).
00038  *
00039  * Revision 1.2  2008/07/26 09:43:01  haraldkipp
00040  * Added support for retrieving and setting the interrupt mode.
00041  *
00042  * Revision 1.1  2007/12/09 21:36:05  olereinhardt
00043  * Initial checkin of adc irq handler
00044  *
00045  */
00046 
00047 #include <arch/arm.h>
00048 #include <dev/irqreg.h>
00049 
00050 #if defined(ADC_ID)
00051 
00052 #ifndef NUT_IRQPRI_ADC
00053 #define NUT_IRQPRI_ADC  4
00054 #endif
00055 
00056 static int AdcIrqCtl(int cmd, void *param);
00057 
00058 IRQ_HANDLER sig_ADC = {
00059 #ifdef NUT_PERFMON
00060     0,                  /* Interrupt counter, ir_count. */
00061 #endif
00062     NULL,               /* Passed argument, ir_arg. */
00063     NULL,               /* Handler subroutine, ir_handler. */
00064     AdcIrqCtl           /* Interrupt control, ir_ctl. */
00065 };
00066 
00070 static void AdcIrqEntry(void) __attribute__ ((naked));
00071 void AdcIrqEntry(void)
00072 {
00073     IRQ_ENTRY();
00074 #ifdef NUT_PERFMON
00075     sig_ADC.ir_count++;
00076 #endif
00077     if (sig_ADC.ir_handler) {
00078         (sig_ADC.ir_handler) (sig_ADC.ir_arg);
00079     }
00080     IRQ_EXIT();
00081 }
00082 
00100 static int AdcIrqCtl(int cmd, void *param)
00101 {
00102     int rc = 0;
00103     unsigned int *ival = (unsigned int *)param;
00104     int_fast8_t enabled = inr(AIC_IMR) & _BV(ADC_ID);
00105 
00106     /* Disable interrupt. */
00107     if (enabled) {
00108         outr(AIC_IDCR, _BV(ADC_ID));
00109     }
00110 
00111     switch(cmd) {
00112     case NUT_IRQCTL_INIT:
00113         /* Set the vector. */
00114         outr(AIC_SVR(ADC_ID), (unsigned int)AdcIrqEntry);
00115         /* Initialize to edge triggered with defined priority. */
00116         outr(AIC_SMR(ADC_ID), AIC_SRCTYPE_INT_EDGE_TRIGGERED | NUT_IRQPRI_ADC);
00117         /* Clear interrupt */
00118         outr(AIC_ICCR, _BV(ADC_ID));
00119         break;
00120     case NUT_IRQCTL_STATUS:
00121         if (enabled) {
00122             *ival |= 1;
00123         }
00124         else {
00125             *ival &= ~1;
00126         }
00127         break;
00128     case NUT_IRQCTL_ENABLE:
00129         enabled = 1;
00130         break;
00131     case NUT_IRQCTL_DISABLE:
00132         enabled = 0;
00133         break;
00134     case NUT_IRQCTL_GETMODE:
00135         {
00136             unsigned int val = inr(AIC_SMR(ADC_ID)) & AIC_SRCTYPE;
00137             if (val == AIC_SRCTYPE_INT_LEVEL_SENSITIVE || val == AIC_SRCTYPE_EXT_HIGH_LEVEL) {
00138                 *ival = NUT_IRQMODE_LEVEL;
00139             } else  {
00140                 *ival = NUT_IRQMODE_EDGE;
00141             }
00142         }
00143         break;
00144     case NUT_IRQCTL_SETMODE:
00145         if (*ival == NUT_IRQMODE_LEVEL) {
00146             outr(AIC_SMR(ADC_ID), (inr(AIC_SMR(ADC_ID)) & ~AIC_SRCTYPE) | AIC_SRCTYPE_INT_LEVEL_SENSITIVE);
00147         } else if (*ival == NUT_IRQMODE_EDGE) {
00148             outr(AIC_SMR(ADC_ID), (inr(AIC_SMR(ADC_ID)) & ~AIC_SRCTYPE) | AIC_SRCTYPE_INT_EDGE_TRIGGERED);
00149         } else  {
00150             rc = -1;
00151         }
00152         break;
00153     case NUT_IRQCTL_GETPRIO:
00154         *ival = inr(AIC_SMR(ADC_ID)) & AIC_PRIOR;
00155         break;
00156     case NUT_IRQCTL_SETPRIO:
00157         outr(AIC_SMR(ADC_ID), (inr(AIC_SMR(ADC_ID)) & ~AIC_PRIOR) | *ival);
00158         break;
00159 #ifdef NUT_PERFMON
00160     case NUT_IRQCTL_GETCOUNT:
00161         *ival = (unsigned int)sig_ADC.ir_count;
00162         sig_ADC.ir_count = 0;
00163         break;
00164 #endif
00165     default:
00166         rc = -1;
00167         break;
00168     }
00169 
00170     /* Enable interrupt. */
00171     if (enabled) {
00172         outr(AIC_IECR, _BV(ADC_ID));
00173     }
00174     return rc;
00175 }
00176 
00177 #endif /* ADC_ID */