aboutsummaryrefslogtreecommitdiffstats
path: root/firmware/src_simtrace/tc_etu.c
blob: dd359afd27713605dbbdb49843d03768c6b52117 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/* SimTrace TC (Timer / Clock) support code
 * (C) 2006 by Harald Welte <hwelte@hmw-consulting.de>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by 
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

/*------------------------------------------------------------------------------
 *          Headers
 *------------------------------------------------------------------------------*/
#include "board.h"

#include <string.h>

//FIXME:
static const Pin pTC[] = {{PIO_PA4B_TCLK0, PIO_PA0B_TIOA0, PIO_PA1B_TIOB0}};

/** Global timestamp in milliseconds since start of application */
volatile uint32_t dwTimeStamp = 0;
volatile uint8_t timeout_occured = 0;

// FIXME: Do I need the function?:
/**
 *  \brief Handler for Sytem Tick interrupt.
 *
 *  Process System Tick Event
 *  Increments the timestamp counter.
 */
void SysTick_Handler( void )
{
    dwTimeStamp ++;
    
}



void TC0_IrqHandler( void )
{
    volatile uint32_t dummy;
    /* Clear status bit to acknowledge interrupt */
    dummy = TC0->TC_CHANNEL[ 0 ].TC_SR;

    timeout_occured++;
//    printf("++++ TC0_Irq %d\n\r", timeout_occured);
}


void TC0_Counter_Reset( void )
{
    TC0->TC_CHANNEL[ 0 ].TC_CCR = TC_CCR_SWTRG ;
    timeout_occured = 0;
}

/*  == Timeouts ==
 *  One symbol is about 2ms --> Timeout = BUFLEN * 2ms ?
 *  For BUFLEN = 64 that is 7.8 Hz
 */
void Timer_Init() 
{
    uint32_t div;
    uint32_t tcclks;

    /** Enable peripheral clock. */
    PMC_EnablePeripheral(ID_TC0);

    /** Configure TC for a $ARG1 Hz frequency and trigger on RC compare. */
    TC_FindMckDivisor( 8, BOARD_MCK, &div, &tcclks, BOARD_MCK );
    TRACE_INFO("Chosen div, tcclk: %d, %d\r\n", div, tcclks);
    /* TC_CMR: TC Channel Mode Register: Capture Mode */
    /* CPCTRG: RC Compare resets the counter and starts the counter clock. */
    TC_Configure( TC0, 0, tcclks | TC_CMR_CPCTRG );
    /* TC_RC: TC Register C: contains the Register C value in real time. */
    TC0->TC_CHANNEL[ 0 ].TC_RC = ( BOARD_MCK / div ) / 4; 

    /* Configure and enable interrupt on RC compare */
    NVIC_EnableIRQ( (IRQn_Type)ID_TC0 );

    TC0->TC_CHANNEL[ 0 ].TC_IER = TC_IER_CPCS;  /* CPCS: RC Compare */
    TC_Start( TC0, 0 );

    return;

    /*** From here on we have code based on old simtrace code */

    /* Cfg PA4(TCLK0), PA0(TIOA0), PA1(TIOB0) */    

    PIO_Configure( pTC, PIO_LISTSIZE( pTC ) );



// FIXME:
//    PIO_ConfigureIt( &pinPhoneRST, ISR_PhoneRST ) ;
//    PIO_EnableIt( &pinPhoneRST ) ;

    /* enable interrupts for Compare-C and External Trigger */
    TC0->TC_CHANNEL[0].TC_IER = TC_IER_CPCS | TC_IER_ETRGS;

    //...
     /* Enable master clock for TC0 */
//     TC0->TC_CHANNEL[0].TC_CCR

    /* Reset to start timers */
    //...
}