📄 lh79520_timer_driver.c
字号:
* Parameters:
* devid: Pointer to timer config structure
* cmd: ioctl command
* arg: ioctl argument
*
* Outputs: None
*
* Returns: The status of the ioctl operation
*
* Notes: None
*
**********************************************************************/
STATUS timer_ioctl(INT_32 devid,
INT_32 cmd,
INT_32 arg)
{
TIMER_REGS_T *timerregs;
TIMER_CFG_T *timercfgptr = (TIMER_CFG_T *) devid;
STATUS status = _ERROR;
if (timercfgptr->init == TRUE)
{
status = _NO_ERROR;
timerregs = timercfgptr->regptr;
switch (cmd)
{
case TIMER_START:
/* Enable timer */
timerregs->control |= TMRCTRL_ENABLE;
break;
case TIMER_STOP:
/* Disable timer */
timerregs->control &= ~TMRCTRL_ENABLE;
break;
case TIMER_MODE:
if ((INT_32) arg == TIMER_FREERUN)
{
/* Set timer to freerun mode */
timerregs->control &= ~TMRCTRL_MODE_PERIODIC;
}
else if ((INT_32) arg == TIMER_PERIODIC)
{
/* Set timer to periodic mode */
timerregs->control |= TMRCTRL_MODE_PERIODIC;
}
break;
case TIMER_SET_USECS:
/* Set the timer to the passed time in micro seconds - us */
timer_set_delay_us(timerregs, (UNS_32) arg);
break;
case TIMER_INT_CLEAR:
/* Clear the timer interrupt */
timerregs->clear = 0;
break;
case TIMER_SET_PRESCALE:
/* set timer prescaler 1, 16, 256 */
switch ((INT_32) arg)
{
case PRESCALE_1: /* default when after open */
timerregs->control &= ~(_BIT(2) | _BIT(3));
break;
case PRESCALE_16:
timerregs->control &= ~(_BIT(2) | _BIT(3));
timerregs->control |= TMRCTRL_PRESCALE16;
break;
case PRESCALE_256:
timerregs->control &= ~(_BIT(2) | _BIT(3));
timerregs->control |= TMRCTRL_PRESCALE256;
break;
default:
status = _ERROR;
}
break;
case TIMER_SET_COUNT:
/* Set timer count value */
timerregs->load = (UNS_32) arg;
break;
case TIMER_SET_CASCADE:
/* Set timer cascade mode */
if (arg == 1)
{
/* set timer as cascade mode */
timerregs->control |= TMRCTRL_CASCADE_ENABLE;
}
else if (arg == 0)
{
/* set timer as no cascade mode */
timerregs->control &= ~TMRCTRL_CASCADE_ENABLE;
}
else
{
status = _ERROR;
}
break;
case TIMER_GET_STATUS:
/* Return a timer status */
switch ((INT_32) arg)
{
case GET_TIMER_ENABLE:
/* Return timer enabled status */
if ((timerregs->control &
TMRCTRL_ENABLE) != 0)
{
/* Timer is enabled */
status = TRUE;
}
else
{
/* Timer is disabled */
status = FALSE;
}
break;
case GET_TIMER_MODE:
/* Return timer mode */
if ((timerregs->control &
TMRCTRL_MODE_PERIODIC) != 0)
{
/* Periodic timer mode */
status = TIMER_PERIODIC;
}
else
{
/* Freerunning timer mode */
status = TIMER_FREERUN;
}
break;
case GET_TIMER_COUNT:
/* Return the timer load value */
status = timerregs->load;
break;
case GET_TIMER_VALUE:
/* Return the current timer value */
status = timerregs->value;
break;
default:
/* Unsupported parameter */
status = _ERROR;
break;
}
break;
default:
/* Unsupported parameter */
status = _ERROR;
}
}
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 TIMER0, TIMER1, TIMER2 or TIMER3
* usec : the delay time in microseconds
*
* Outputs: None
*
* Returns: Nothing
*
* Notes:
* This function defines the timer to be polled until due.
* The timer used for this function may be reprogrammed to a
* different configuration. Interrupts should be disabled prior to
* calling this function.
*
**********************************************************************/
void timer_wait_us(TIMER_REGS_T *timer,
UNS_32 usec)
{
if ((timer == TIMER1) || (timer == TIMER1) || (timer == TIMER2)
|| (timer == TIMER3))
{
timer->control &= ~TMRCTRL_ENABLE;
timer_set_delay_us(timer, usec);
/* Set the timer to be periodic */
timer->control |= TMRCTRL_MODE_PERIODIC;
/* Start timer */
timer->control |= TMRCTRL_ENABLE;
/* Wait for timer to underflow */
while (timer->value > 1)
{
}
timer->control &= ~TMRCTRL_ENABLE;
timer->clear = 0;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -