drivers/device/MicoTimer.c

changeset 0
396b0bd970d3
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/drivers/device/MicoTimer.c	Fri Aug 13 10:49:23 2010 +0100
     1.3 @@ -0,0 +1,231 @@
     1.4 +/****************************************************************************
     1.5 +**
     1.6 +**  Name: MicoTimer.c
     1.7 +**
     1.8 +**  Description:
     1.9 +**        Implements functions for manipulating LatticeMico32 Timer
    1.10 +**
    1.11 +**  $Revision: $
    1.12 +**
    1.13 +** Disclaimer:
    1.14 +**
    1.15 +**   This source code is intended as a design reference which
    1.16 +**   illustrates how these types of functions can be implemented.  It
    1.17 +**   is the user's responsibility to verify their design for
    1.18 +**   consistency and functionality through the use of formal
    1.19 +**   verification methods.  Lattice Semiconductor provides no warranty
    1.20 +**   regarding the use or functionality of this code.
    1.21 +**
    1.22 +** --------------------------------------------------------------------
    1.23 +**
    1.24 +**                     Lattice Semiconductor Corporation
    1.25 +**                     5555 NE Moore Court
    1.26 +**                     Hillsboro, OR 97214
    1.27 +**                     U.S.A
    1.28 +**
    1.29 +**                     TEL: 1-800-Lattice (USA and Canada)
    1.30 +**                          (503)268-8001 (other locations)
    1.31 +**
    1.32 +**                     web:   http://www.latticesemi.com
    1.33 +**                     email: techsupport@latticesemi.com
    1.34 +**
    1.35 +** --------------------------------------------------------------------------
    1.36 +**
    1.37 +**  Change History (Latest changes on top)
    1.38 +**
    1.39 +**  Ver    Date        Description
    1.40 +** --------------------------------------------------------------------------
    1.41 +**
    1.42 +**  3.0   Mar-25-2008  Added Header
    1.43 +**
    1.44 +**---------------------------------------------------------------------------
    1.45 +*****************************************************************************/
    1.46 +
    1.47 +
    1.48 +#include "MicoTimer.h"
    1.49 +#include "MicoTimerService.h"
    1.50 +#include "MicoMacros.h"
    1.51 +#include "MicoInterrupts.h"
    1.52 +
    1.53 +
    1.54 +
    1.55 +/**************************************************************************
    1.56 + * driver-debug functions                                                 *
    1.57 + **************************************************************************/
    1.58 +#if _MICO_TIMER_DRIVER_DEBUG_ENABLED_
    1.59 +static void MicoTimerDumpCtx(MicoTimerCtx_t *pCtx)
    1.60 +{
    1.61 +    printf("\n Base     : 0x%x", pCtx->base);
    1.62 +    printf("\n IntrLevel: 0x%x", pCtx->intrLevel);
    1.63 +    printf("\n Context  : 0x%x", pCtx->userCtx);
    1.64 +    printf("\n Callback : 0x%x", pCtx->callback);
    1.65 +    return;
    1.66 +}
    1.67 +
    1.68 +
    1.69 +void MicoTimerDumpRegs(MicoTimerCtx_t *pCtx)
    1.70 +{
    1.71 +    MicoTimer_t *pTimer = (MicoTimer_t *)pCtx->base;
    1.72 +    printf("\n isr-status  : 0x%x",     pTimer->Status);
    1.73 +    printf("\n isr-control : 0x%x",     pTimer->Control);
    1.74 +    printf("\n isr-period  : 0x%x",     pTimer->Period);
    1.75 +    printf("\n isr-snapshot: 0x%x\n",   pTimer->Snapshot);
    1.76 +}
    1.77 +#endif
    1.78 +
    1.79 +
    1.80 +/******************************************************************************
    1.81 + * Timer interrupt-handler                                                    *
    1.82 + ******************************************************************************/
    1.83 +static void MicoTimerISR(unsigned int intrLevel, void *pContext)
    1.84 +{
    1.85 +    /*
    1.86 +     * flow:
    1.87 +     * - clear the timeout-bit
    1.88 +     * - invoke user-registered callback
    1.89 +     */
    1.90 +    MicoTimerCtx_t *ctx = (MicoTimerCtx_t *)pContext;
    1.91 +    volatile MicoTimer_t *pTimer = (MicoTimer_t *)ctx->base;
    1.92 +
    1.93 +    /* acknowledge the interrupt */
    1.94 +    pTimer->Status = 0;
    1.95 +
    1.96 +    /* call the isr */
    1.97 +    if(ctx->callback != 0)
    1.98 +        ((TimerCallback_t)ctx->callback)(ctx->userCtx);
    1.99 +
   1.100 +    return;
   1.101 +}
   1.102 +
   1.103 +
   1.104 +/******************************************************************************
   1.105 + * Initializes a timer                                                        *
   1.106 + *----------------------------------------------------------------------------*
   1.107 + * Inputs:                                                                    *
   1.108 + *     unsigned int IntNum: Interrupt-level                                   *
   1.109 + * Outputs:                                                                   *
   1.110 + * Return values:                                                             *
   1.111 + *            MICO_STATUS_E_INVALID_PARAM                                     *
   1.112 + *            MICO_STATUS_OK                                                  *
   1.113 + ******************************************************************************/
   1.114 +void MicoTimerInit( MicoTimerCtx_t *ctx )
   1.115 +{
   1.116 +    /* stop the timer (if it was running) */
   1.117 +    MicoTimerStop(ctx);
   1.118 +
   1.119 +    /* 
   1.120 +     * Enable interrupts without registering an isr:
   1.121 +     * this way, any spurious timer interrupt that might have
   1.122 +     * existed prior to this init being called, will cause the
   1.123 +     * Mico Interrupt-framework to acknowlede the CPU's interrupt-pending.
   1.124 +     */
   1.125 +    MicoEnableInterrupt(ctx->intrLevel);
   1.126 +
   1.127 +    /* register this timer for lookup service */
   1.128 +    ctx->lookupReg.name = ctx->name;
   1.129 +    ctx->lookupReg.deviceType = "TimerDevice";
   1.130 +    ctx->lookupReg.priv = ctx;
   1.131 +    MicoRegisterDevice( &(ctx->lookupReg) );
   1.132 +
   1.133 +    /* all done */
   1.134 +    return;
   1.135 +}
   1.136 +
   1.137 +
   1.138 +/******************************************************************************
   1.139 + * Starts a Mico32 timer                                                      *
   1.140 + *----------------------------------------------------------------------------*
   1.141 + * Inputs:                                                                    *
   1.142 + *     MicoTimerCtx_t *ctx: pointer to valid ctx                              *
   1.143 + *                                                                            *
   1.144 + *     TimerCallback_t callback: User-provided callback function, called      *
   1.145 + *             in interrupt-context.                                          *
   1.146 + *                                                                            *
   1.147 + *     void *priv: user-provided data that will be called in the callback     *
   1.148 + *                                                                            *
   1.149 + *     unsigned int timercount: ticks to load counter with                    *
   1.150 + *                                                                            *
   1.151 + *     int periodic: if 1, the timer is programmed to auto-load, else         *
   1.152 + *           timer is programmed not to reload on reaching terminal value     *
   1.153 + *                                                                            *
   1.154 + * Note: user MUST supply a valid ctx.                                        *
   1.155 + *       user MUST make sure timerCount is non-zero                           *
   1.156 + ******************************************************************************/
   1.157 +mico_status
   1.158 +MicoTimerStart( MicoTimerCtx_t *ctx, TimerCallback_t callback, void *priv, unsigned int timerCount, int periodic )
   1.159 +{
   1.160 +    volatile MicoTimer_t *pTimer;
   1.161 +    unsigned int regValue;
   1.162 +
   1.163 +    if( (ctx == 0) || (timerCount == 0) )
   1.164 +        return(MICO_STATUS_E_INVALID_PARAM);
   1.165 +
   1.166 +    /* flow:
   1.167 +     * - stop the timer,
   1.168 +     * - load new timerCount,
   1.169 +     * - configure the timer, taking into account the periodicity
   1.170 +     * - register the isr (user MUST provide an isr)
   1.171 +     * - start the timer.
   1.172 +     */
   1.173 +    pTimer          = (MicoTimer_t *)(ctx->base);
   1.174 +    regValue        = (MICO32_TIMER_CONTROL_START_BIT_MASK|MICO32_TIMER_CONTROL_INT_BIT_MASK);
   1.175 +    ctx->callback   = (void *)callback;
   1.176 +    ctx->userCtx    = priv;
   1.177 +
   1.178 +    MicoRegisterISR(ctx->intrLevel, ctx, MicoTimerISR);
   1.179 +
   1.180 +    if(periodic != 0)
   1.181 +        regValue |= MICO32_TIMER_CONTROL_CONT_BIT_MASK;
   1.182 +
   1.183 +
   1.184 +    pTimer->Control = MICO32_TIMER_CONTROL_STOP_BIT_MASK;
   1.185 +    pTimer->Period  = timerCount;
   1.186 +    pTimer->Control = regValue;
   1.187 +
   1.188 +
   1.189 +    return(MICO_STATUS_OK);
   1.190 +}
   1.191 +
   1.192 +
   1.193 +/******************************************************************************
   1.194 + *                                                                            *
   1.195 + * Stops a Mico32 timer                                                       *
   1.196 + *                                                                            *
   1.197 + *----------------------------------------------------------------------------*
   1.198 + *                                                                            *
   1.199 + * Note: user MUST supply a valid ctx.                                        *
   1.200 + *                                                                            *
   1.201 + ******************************************************************************/
   1.202 +mico_status
   1.203 +MicoTimerStop(MicoTimerCtx_t *ctx)
   1.204 +{
   1.205 +    volatile MicoTimer_t *pTimer;
   1.206 +    if(ctx == 0)
   1.207 +        return(MICO_STATUS_E_INVALID_PARAM);
   1.208 +
   1.209 +
   1.210 +    /* stop the timer first and ack any pending interrupts */
   1.211 +    pTimer = (MicoTimer_t *)(ctx->base);
   1.212 +    pTimer->Control = MICO32_TIMER_CONTROL_STOP_BIT_MASK;
   1.213 +    pTimer->Status = 0;
   1.214 +
   1.215 +    /* all done */
   1.216 +    return(MICO_STATUS_OK);
   1.217 +}
   1.218 +
   1.219 +
   1.220 +/******************************************************************************
   1.221 + * reads timer-snapshot                                                       *
   1.222 + *----------------------------------------------------------------------------*
   1.223 + * Note: user MUST supply a valid ctx.                                        *
   1.224 + ******************************************************************************/
   1.225 +unsigned int MicoTimerSnapshot( MicoTimerCtx_t *ctx)
   1.226 +{
   1.227 +    volatile MicoTimer_t *pTimer;
   1.228 +    if(ctx == 0){
   1.229 +        return(0);
   1.230 +    }
   1.231 +    pTimer = (MicoTimer_t *)(ctx->base);
   1.232 +    return(pTimer->Snapshot);
   1.233 +}
   1.234 +