⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lh7a404_timer_driver.c

📁 在sharp 404开发板的串口测试代码
💻 C
📖 第 1 页 / 共 2 页
字号:
                 break;

            case TIMER_MODE:
                if ((INT_32) arg == TIMER_FREERUN)
                {
                    /* Set timer to freerun mode */
                    timerregs->control &= ~TIMER_CTRL_PERIODIC;
                }
                else if ((INT_32) arg == TIMER_PERIODIC)
                {
                    /* Set timer to periodic mode */
                    timerregs->control |= TIMER_CTRL_PERIODIC;
                }
                break;

            case TIMER_CLOCK:
                /* This command is not valid on timer 3 */
                if (timerregs == TIMER3)
                {
                    status = SMA_BAD_CLK;
                }
                else
                {
                    if ((INT_32) arg == TIMER_CLOCK_2K)
                    {
                        /* Set timer clock to 2KHz */
                        timerregs->control &= ~TIMER_CTRL_508K;
                    }
                    else if ((INT_32) arg == TIMER_CLOCK_508K)
                    {
                        /* Set timer clock to 508KHz */
                        timerregs->control |= TIMER_CTRL_508K;
                    }
                    else
                    {
                        /* Not a valid clock configuration */
                        status = SMA_BAD_CLK;
                    }
                }
                break;

            case TIMER_SET_COUNT:
                /* Set timer count value */
                timerregs->load = TIMER_LOAD((INT_32) arg);
                break;

            case TIMER_SET_USECS:
                /* Set the timer to the passed time in uSecs */
                timer_set_delay(timerregs, (UNS_32) arg);
                break;

            case TIMER_INT_CLEAR:
                /* Clear the timer interrupt */
                timerregs->clear = 0;
                break;

            case TIMER_SET_BZCONT:
                /* Set the state of the BZTOG output */
                switch ((INT_32) arg)
                {
                    case TIMER_BZTOG_LOW:
                        /* Drive BZTOG low */
                        BZCONT = 0;
                        break;

                    case TIMER_BZTOG_HIGH:
                        /* Drive BZTOG high */
                        BZCONT = BZCONT_BZTOG;
                        break;

                    case TIMER_BZTOG_TOGGLE:
                        /* Drive BZTOG with timer 1 underflow */
                        BZCONT = BZCONT_BZMOD;
                        break;

                    default:
                        /* Invalid argument */
                        status = SMA_BAD_PARAMS;
                        break;
                }
                break;

            case TIMER_GET_STATUS:
                /* Return a timer status */
                switch (arg)
                {
                    case TIMER_ENABLE_ST:
                        /* Return timer enabled status */
                        if ((timerregs->control &
                            TIMER_CTRL_ENABLE) != 0)
                        {
                            /* Timer is enabled */
                            status = 1;
                        }
                        else
                        {
                            /* Timer is disabled */
                            status = 0;
                        }
                        break;

                    case TIMER_MODE_ST:
                        /* Return timer mode */
                        if ((timerregs->control &
                            TIMER_CTRL_PERIODIC) != 0)
                        {
                            /* Periodic timer mode */
                            status = TIMER_PERIODIC;
                        }
                        else
                        {
                            /* Freerunning timer mode */
                            status = TIMER_FREERUN;
                        }
                        break;

                    case TIMER_CLOCK_ST:
                        /* If this is timer 3, just return with an
                           error status, as timer 3 does not have a
                           programmable clock */
                        if (timerregs == TIMER3)
                        {
                            status = _ERROR;
                        }
                        else if ((timerregs->control &
                            TIMER_CTRL_508K) != 0)
                        {
                            /* 508KHz timer clock */
                            status = TIMER_CLOCK_508K;
                        }
                        else
                        {
                            /* 2KHz timer clock */
                            status = TIMER_CLOCK_2K;
                        }
                        break;

                    case TIMER_COUNT_ST:
                        /* Return the timer load value */
                        status = TIMER_LOAD(timerregs->load);
                        break;

                    case TIMER_VALUE_ST:
                        /* Return the current timer value */
                        status = TIMER_LOAD(timerregs->value);
                        break;

                    case TIMER_BZCONT_ST:
                        /* Return BZTOG output state */
                        if ((BZCONT & BZCONT_BZMOD) != 0)
                        {
                            /* Timer 1 underflow controls state */
                            status = TIMER_BZTOG_TOGGLE;
                        }
                        else if ((BZCONT & BZCONT_BZTOG) != 0)
                        {
                            /* State is driven high */
                            status = TIMER_BZTOG_HIGH;
                        }
                        else
                        {
                            /* State is driven low */
                            status = TIMER_BZTOG_LOW;
                        }
                        break;

                    default:
                        /* Unsupported parameter */
                        status = SMA_BAD_PARAMS;
                        break;
                }
                break;

            default:
                /* Unsupported parameter */
                status = SMA_BAD_PARAMS;
        }
    }

    return status;
}

/***********************************************************************
 *
 * Function: timer_read
 *
 * Purpose: Timer read function (stub only)
 *
 * Processing:
 *     Return 0 to the caller.
 *
 * Parameters:
 *     devid:     Pointer to timer descriptor
 *     buffer:    Pointer to data buffer to copy to
 *     max_bytes: Number of bytes to read
 *
 * Outputs: None
 *
 * Returns: Number of bytes actually read (always 0)
 *
 * Notes: None
 *
 **********************************************************************/
INT_32 timer_read(INT_32 devid,
                  void *buffer,
                  INT_32 max_bytes)
{
    return 0;
}

/***********************************************************************
 *
 * Function: timer_write
 *
 * Purpose: Timer write function (stub only)
 *
 * Processing:
 *     Return 0 to the caller.
 *
 * Parameters:
 *     devid:   Pointer to timer descriptor
 *     buffer:  Pointer to data buffer to copy from
 *     n_bytes: Number of bytes to write
 *
 * Outputs: None
 *
 * Returns: Number of bytes actually written (always 0)
 *
 * Notes: None
 *
 **********************************************************************/
INT_32 timer_write(INT_32 devid,
                   void *buffer,
                   INT_32 n_bytes)
{
    return 0;
}

/*********************************************************************** 
 * 
 * Function: timer_wait_us
 * 
 * Purpose: Delay for usec microSeconds 
 * 
 * Processing:
 *     If the timer argument is a valid timer, stop the timer and use
 *     the timer_set_delay function to program the timer underflow to
 *     the needed delay and then restart the timer. Monitor the raw
 *     timer count value until the value underflows. Stop the timer and
 *     then clear the pending timer interrupt.
 * 
 * Parameters: 
 *     timer : Must be TIMER1, TIMER2 or TIMER3
 *     usec  : the delay time in microseconds
 * 
 * Outputs: None
 * 
 * Returns: Nothing
 * 
 * Notes: 
 *     This function is intended for simple non-interruptable delays
 *     with accurate timing. Using this function will change the
 *     settings of the timer the function is used with. For very small
 *     times with a low CPU and bus clock speed, the timing may be
 *     inaccurate. Use with care.
 * 
 **********************************************************************/
void timer_wait_us(TIMER_REGS_T *timer,
                   UNS_32 usec)
{
    UNS_16 count, count2;

    if ((timer == TIMER1) || (timer == TIMER2) || (timer == TIMER3))
    {
        timer->control = TIMER_CTRL_DISABLE;
        timer_set_delay(timer, usec);

        /* Start timer */
        timer->control |= TIMER_CTRL_ENABLE;

        /* Wait for timer to underflow */
        count2 = (UNS_16) timer->value;
        count = count2;
        while (count == count2)
        {
            count2 = (UNS_16) timer->value;
            if (count > count2)
            {
                count = count2;
            }
        }

        timer->control = TIMER_CTRL_DISABLE;
        timer->clear = 0;
    }
}

/***********************************************************************
 *
 * Function: timer_wait_ms
 *
 * Purpose:
 *     Wait for the specified number of milliseconds
 *
 * Processing:
 *     Call timer_wait_us() with a value of 1000 usec for the number of
 *     times specified by the msec argument.
 *
 * Parameters:
 *     timer: Must be one of TIMER1, TIMER2 or TIMER3
 *     msec:   the delay time in milliseconds
 *
 * Outputs: None
 *
 * Returns: Nothing
 *
 * Notes: None
 *
 **********************************************************************/
void timer_wait_ms(TIMER_REGS_T *timer, UNS_32 msec)
{
    UNS_32 elapsed_ms;

    for (elapsed_ms = 0; elapsed_ms < msec; elapsed_ms++)
    {
        timer_wait_us(timer, MSEC_PER_USEC);
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -