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

📄 lib_tc.c

📁 嵌入式系统开发用源代码 包含At91C arm芯片相关各种例程 包括整数性能测试,浮点测试,硬件驱动等
💻 C
📖 第 1 页 / 共 2 页
字号:
#ifndef _REFERENCE
//*-----------------------------------------------------------------------------
//*      ATMEL Microcontroller Software Support  -  ROUSSET  -
//*-----------------------------------------------------------------------------
//* The software is delivered "AS IS" without warranty or condition of any
//* kind, either express, implied or statutory. This includes without
//* limitation any warranty or condition with respect to merchantability or
//* fitness for any particular purpose, or against the infringements of
//* intellectual property rights of others.
//*-----------------------------------------------------------------------------
//* File Name           : lib_tc.c
//* Object              : Timer Counter Library.
//* Translator          : ARM Software Development Toolkit V2.11a
//*
//* Imported resources  :
//*     tc0_interrupt_handler, tc1_interrupt_handler, tc2_interrupt_handler
//*     define_as_peripheral
//*     init_interrupt
//* Exported resources  :
//*     init_timer_capture
//*     read_timer_status
//*     init_timer_irq, enable_timer_irq, disable_timer_irq
//*     soft_trig_timer
//*     both_pwm_high_first, both_pwm_low_first
//*
//* 1.0 07/08/97 JCZ    : Creation
//* 1.1 21/09/98 JCZ    : delay_microsec : Disable the counter in one shot mode
//* 2.0 21/10/98 JCZ    : Clean up
//*-----------------------------------------------------------------------------

/*----- Called Macro instructions definition -----*/
/* None */

/*----- Files to be included Definition -----*/
#ifdef  AT91_TRACE
#include    <stdio.h>
#endif  /* AT91_TRACE */

#include    "Include/std_c.h"
#include    "Include/tc.h"
#include    "Include/aic.h"
#include    "Include/pio.h"
#include    "Include/prior_irq.h"

/*----- Types and Constants Definition -----*/

/* Default the Master clock speed to 32 MHz */
#ifndef MCK
#define MCK     32000000
#define MCKMHz  32
#endif

/*----- Imported Resources Definition -----*/

#define _REFERENCE(x)   extern x;

#include    "Library/lib_aic.c"     /* AIC Library */
#include    "Library/lib_pio.c"     /* PIO Library */

#undef _REFERENCE

/* Reference the TC Interrupt Handlers written in assembler */
extern void tc0_interrupt_handler ( void ) ;
extern void tc1_interrupt_handler ( void ) ;
extern void tc2_interrupt_handler ( void ) ;

/*---- Internal Resources Definition -----*/

typedef struct
{
    StructTCBlock   *TCBBase ;
    u_int           PioPin ;
    TypeAICHandler  TCHandler ;
    u_char          ChannelId ;
    u_char          PioCtrl ;
    u_char          SourceId ;
    u_char          SourcePrior ;
} StructConstTimer ;

const   StructConstTimer    ConstTimer[NB_TIMER] =
{
    /* Timer 0 */
    {
        TCB0_BASE,
        PIN_TC0,
        tc0_interrupt_handler,
        (u_char)0,
        (u_char)PIO_TC0,
        (u_char)TC0IRQ,
        (u_char)TC0_IRQ_PRIORITY
    } ,
    /* Timer 1 */
    {
        TCB0_BASE,
        PIN_TC1,
        tc1_interrupt_handler,
        (u_char)1,
        (u_char)PIO_TC1,
        (u_char)TC1IRQ,
        (u_char)TC1_IRQ_PRIORITY
    } ,
    /* Timer 2 */
    {
        TCB0_BASE,
        PIN_TC2,
        tc2_interrupt_handler,
        (u_char)2,
        (u_char)PIO_TC2,
        (u_char)TC2IRQ,
        (u_char)TC2_IRQ_PRIORITY
    }
} ;

/* Table of the Timer Counter interrupt handler addresses */
TypeTCHandler   TCHandlerTable[NB_TC_BLOCK*NB_TC_CHANNEL] ;

//*P
//*----------------------------------------------------------------------------
//* Function Name       : no_handler_tc
//* Object              : Default TC Handler.
//* Input Parameters    : none
//* Output Parameters   : none
//* Functions called    : none
//*----------------------------------------------------------------------------
void no_handler_tc ( StructTC *tc_pt )
{
//* Begin
#ifdef  AT91_TRACE
    printf ( "Unknown Timer Counter Interrupt : 0x%x\n", (u_int) tc_pt ) ;
#endif  /* AT91_TRACE */
//* End
}

//*P
//*----------------------------------------------------------------------------
//* Function Name       : compute_register
//* Object              : Default TC Handler.
//* Input Parameters    : <mode> : Mode Register image pointer
//*                     : <ra> : RA Register image pointer
//*                     : <rb> : RB Register image pointer
//*                     : <rc> : RC Register image pointer
//* Output Parameters   : True if no error occurs
//* Functions called    : none
//*----------------------------------------------------------------------------
u_int compute_register ( u_int *mode, u_int *ra, u_int *rb, u_int *rc )
{
//* Begin
    u_int   rc_save ;

    //* Check a or b cycle higher than period, return False
    if (( *ra > *rc ) || ( *rb > *rc )) return ( FALSE ) ;

    //* If period(microsec) * MCK(MHz) div 2 < 2, return False
    if (( rc_save = ( *rc * MCKMHz ) >> 1 ) < 2 ) return ( FALSE ) ;

    //* If period(microsec) * MCK(MHz) div 2 < Counter max value
    if ( rc_save < ( 1<<16 ))
    {
        //* Select MCK/2
        *mode = TCMCK2 ;
        //* RA value is a level(microsed)*MCK(MHz) div 2
        *ra = (( *ra * MCKMHz ) >> 1 ) ;
        //* RB value is a level(microsed)*MCK(MHz) div 2
        *rb = (( *rb * MCKMHz ) >> 1 ) ;
    }
    //* Else
    else
    {
        //* If period(microsec) * MCK(MHz) div 8 < Counter max value
        if (( rc_save = ( *rc * MCKMHz ) >> 3 ) < ( 1 << 16 ))
        {
            //* Select MCK/8
            *mode = TCMCK8 ;
            //* RA value is a level(microsed)*MCK(MHz) div 8
            *ra = (( *ra * MCKMHz ) >> 3 ) ;
            //* RB value is a level(microsed)*MCK(MHz) div 8
            *rb = (( *rb * MCKMHz ) >> 3 ) ;
        }
        //* Else
        else
        {
            //* If period(microsec) * MCK(MHz) div 32 < Counter max value
            if (( rc_save = ( *rc * MCKMHz ) >> 5 ) < ( 1 << 16 ))
            {
                //* Select MCK/32
                *mode = TCMCK32 ;
                //* RA value is a level(microsed)*MCK(MHz) div 32
                *ra = (( *ra * MCKMHz ) >> 5 ) ;
                //* RB value is a level(microsed)*MCK(MHz) div 32
                *rb = (( *rb * MCKMHz ) >> 5 ) ;
            }
            //* Else
            else
            {
                //* If period(microsec) * MCK(MHz) div 128 < Counter max value
                if (( rc_save = ( *rc * MCKMHz ) >> 7 ) < ( 1 << 16 ))
                {
                    //* Select MCK/128
                    *mode = TCMCK128 ;
                    //* RA value is a level(microsed)*MCK(MHz) div 128
                    *ra = (( *ra * MCKMHz ) >> 7 ) ;
                    //* RB value is a level(microsed)*MCK(MHz) div 128
                    *rb = (( *rb * MCKMHz ) >> 7 ) ;
                }
                //* Else
                else
                {
                    //* If period(microsec) * MCK(MHz) div 1024 < Counter max value
                    if (( rc_save = ( *rc * MCKMHz ) >> 10 ) < ( 1 << 16 ))
                    {
                        //* Select MCK/1024
                        *mode = TCMCK1024 ;
                        //* RA value is a level(microsed)*MCK(MHz) div 1024
                        *ra = (( *ra * MCKMHz ) >> 10 ) ;
                        //* RB value is a level(microsed)*MCK(MHz) div 1024
                        *rb = (( *rb * MCKMHz ) >> 10 ) ;
                    }
                    //* Else
                    else
                    {
                        //* Return False
                        return ( FALSE ) ;
                    }
                    //* EndIf
                }
                //* EndIf
            }
            //* EndIf
        }
        //* EndIf
    }
    //* EndIf
    *rc = rc_save ;

    //* Return True
    return ( TRUE ) ;
}
//* End


/*---- External Resources Definition -----*/

#define _REFERENCE(x)   x
#define CORPS
#endif

//*P
//*----------------------------------------------------------------------------
//* Function Name       : get_channel_pointer
//* Object              : Get a Timer Counter Channel Base Address
//* Input Parameters    : <timer_id> = the channel number
//* Output Parameters   : The TCC Base Address, or 0 if the timer id is out of
//*                     : range
//* Functions called    : none
//*----------------------------------------------------------------------------
_REFERENCE (StructTC *get_tc_pointer ( u_int timer_id ))
#ifdef CORPS
//* Begin
{
    //* If timer_id is out of range, return False
    if ( timer_id >= NB_TIMER ) return ( (StructTC *) 0 ) ;

    //* Return the value of the Base Address of the channel
    return ( &ConstTimer[timer_id].TCBBase->
                    TC[ConstTimer[timer_id].ChannelId] ) ;
}
//* End
#endif

//*P
//*----------------------------------------------------------------------------
//* Function Name       : read_timer_status
//* Object              : Read the Status of a Timer Counter Channel
//* Input Parameters    : <timer_id> = the channel number
//* Output Parameters   : the status value, or 0 if the timer id is out of range
//* Functions called    : none
//*----------------------------------------------------------------------------
_REFERENCE (u_int read_timer_status ( u_int timer_id ))
#ifdef CORPS
//* Begin
{
    //* If timer_id is out of range, return False
    if ( timer_id >= NB_TIMER ) return ( FALSE ) ;

    //* Return the value of the Status Register
    return ( ConstTimer[timer_id].TCBBase->
                TC[ConstTimer[timer_id].ChannelId].TC_SR ) ;
}
//* End
#endif

//*P
//*----------------------------------------------------------------------------
//* Function Name       : init_timer_irq
//* Object              : Initialize the Timer Counter Handlers
//* Input Parameters    : none
//* Output Parameters   : none
//* Functions called    : none
//*----------------------------------------------------------------------------
_REFERENCE (void init_timer_irq ( void ))
#ifdef CORPS
//* Begin
{
    u_int       tc_id ;

    //* For each timer counter channel
    for ( tc_id = 0 ; tc_id < NB_TIMER ; tc_id ++ )
    {
        //* Initialise the default handler address
        TCHandlerTable[tc_id] = no_handler_tc ;
        //* Enable the interrupt on the Interrupt controller for Timer Counter
        init_interrupt ( ConstTimer[tc_id].SourceId,
                         ConstTimer[tc_id].SourcePrior,
                         EdgeTriggered ,
                         ConstTimer[tc_id].TCHandler ) ;
    }
    //* EndFor
}
//* End
#endif

//*P
//*----------------------------------------------------------------------------
//* Function Name       : enable_timer_irq
//* Object              : Define an Interrupt Handler for a Timer Counter
//* Input Parameters    : <timer_id> = the channel number
//*                     : <mask> = identify the interrupts to enable
//*                     : <handler_pt> = the handler pointer
//* Output Parameters   : False if an error occurs, otherwise True
//* Functions called    : none
//*----------------------------------------------------------------------------
_REFERENCE (u_int enable_timer_irq ( u_int timer_id,
                                     u_int mask,
                                     TypeTCHandler handler_pt ))

⌨️ 快捷键说明

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