summaryrefslogtreecommitdiffstats
path: root/firmware/target/arm/at91sam/lyre_proto1/timer-lyre_proto1.c
blob: 1b620e9c4f41202e1ae8d1e31ec50837675e0a7c (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
117
118
119
120
121
122
123
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 *
 *
 * Copyright (C) 2009 by Jorge Pinto
 *
 * 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 software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

#include "config.h"
#include "cpu.h"
#include "system.h"
#include "timer.h"
#include "logf.h"
#include "at91sam9260.h"

/*-----------------------------------------------------------------------------
 * Function Name       : pitc_handler
 * Object              : Handler for PITC interrupt
 *---------------------------------------------------------------------------*/
void pitc_handler(void)
{
    unsigned long pivr = 0;
    unsigned long pisr = 0;

    /* Read the PISR */
    pisr = AT91C_PITC_PISR & AT91C_PITC_PITS;

    if (pisr != 0)
    {
        /* Read the PIVR. It acknowledges the IT */
        pivr = AT91C_PITC_PIVR;

        if (pfn_timer != NULL)
            pfn_timer();
    }
}

bool timer_set(long cycles, bool start)
{
    if (cycles < 1000) /* Max value on PITC?? */
    {
        if (start && pfn_unregister != NULL)
        {
            pfn_unregister();
            pfn_unregister = NULL;
        }

        /* Configure a resolution of <cycles> ms */
        AT91C_PITC_PIMR = MCK_FREQ / ( 16 * cycles) - 1;

        return true;
    }

    return false;
}

bool timer_start(void)
{
    bool retval = true;
    volatile unsigned long pimr = 0;

    /* Configure a resolution of 1 ms */
    AT91C_PITC_PIMR = MCK_FREQ / (16 * 1000) - 1;

    /* Enable interrupts */
    /* Disable the interrupt on the interrupt controller */
    AT91C_AIC_IDCR = (1 << AT91C_ID_SYS);

    /* Save the interrupt handler routine pointer and the interrupt priority */
    AT91C_AIC_SVR(AT91C_ID_SYS) = (unsigned long) pitc_handler;
    /* Store the Source Mode Register */
    AT91C_AIC_SMR(AT91C_ID_SYS) = AT91C_AIC_SRCTYPE_INT_LEVEL_SENSITIVE | \
    AT91C_AIC_PRIOR_LOWEST;
    /* Clear the interrupt on the interrupt controller */
    AT91C_AIC_ICCR = (1 << AT91C_ID_SYS);

    /* Enable the interrupt on the interrupt controller */
    AT91C_AIC_IECR = (1 << AT91C_ID_SYS);

    /* Enable the interrupt on the pit */
    pimr = AT91C_PITC_PIMR;
    AT91C_PITC_PIMR = pimr | AT91C_PITC_PITIEN;

    /* Enable the pit */
    pimr = AT91C_PITC_PIMR;
    AT91C_PITC_PIMR = pimr | AT91C_PITC_PITEN;

    return retval;
}

void timer_stop(void)
{
    volatile unsigned long pimr = 0;

    /* Disable the interrupt on the interrupt controller */
    AT91C_AIC_IDCR = (1 << AT91C_ID_SYS);

    /* Clear the interrupt on the interrupt controller */
    AT91C_AIC_ICCR = (1 << AT91C_ID_SYS);

    /* Disable the interrupt on the pit */
    pimr = AT91C_PITC_PIMR;
    pimr &= ~AT91C_PITC_PITIEN;
    AT91C_PITC_PIMR = pimr;

    /* Disable the pit */
    pimr = AT91C_PITC_PIMR;
    pimr &= ~AT91C_PITC_PITEN;
    AT91C_PITC_PIMR = pimr;
}