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