summaryrefslogtreecommitdiffstats
path: root/firmware/target/arm/s5l8702/ipod6g/piezo-ipod6g.c
blob: 06735673becc159490cf8635acaf6690e465b7b1 (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
124
125
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2006-2007 Robert Keevil
 *
 * 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 "system.h"
#include "kernel.h"
#include "piezo.h"

static unsigned int duration;
static bool beeping;

void INT_TIMERA(void)
{
    /* clear interrupt */
    TACON = TACON;
    if (!(--duration))
        piezo_stop();
}

static void piezo_start(unsigned short cycles, unsigned short periods)
{
#ifndef SIMULATOR
    duration = periods;
    beeping = 1;
    /* select TA_OUT function on GPIO ports */
    PCON0 = (PCON0 & 0x00ffffff) | 0x53000000;
    /* configure timer for 100 kHz (12 MHz / 4 / 30) */
    TACMD = (1 << 1);   /* TA_CLR */
    TAPRE = 30 - 1;     /* prescaler */
    TACON = (1 << 13) | /* TA_INT1_EN */
            (0 << 12) | /* TA_INT0_EN */
            (0 << 11) | /* TA_START */
            (1 << 8) |  /* TA_CS = ECLK / 4 */
            (1 << 6) |  /* select ECLK (12 MHz) */
            (1 << 4);   /* TA_MODE_SEL = PWM mode */
    TADATA0 = cycles;   /* set interval period */
    TADATA1 = cycles << 1; /* set interval period */
    TACMD = (1 << 0);   /* TA_EN */
#endif
}

void piezo_stop(void)
{
#ifndef SIMULATOR
    beeping = 0;
    TACMD = (1 << 1);   /* TA_CLR */
    /* configure GPIO for the lowest power consumption */
    PCON0 = (PCON0 & 0x00ffffff) | 0xee000000;
#endif
}

void piezo_clear(void)
{
    piezo_stop();
}

bool piezo_busy(void)
{
    return beeping;
}

void piezo_init(void)
{
    piezo_stop();
}

void piezo_button_beep(bool beep, bool force)
{
    if (force)
        while (beeping)
            yield();

    if (!beeping)
    {
        if (beep)
            piezo_start(22, 457);
        else
            piezo_start(40, 4);
    }
}

#ifdef BOOTLOADER
void piezo_tone(uint32_t period /*uS*/, int32_t duration /*ms*/)
{
    int32_t stop = USEC_TIMER + duration*1000;
    uint32_t level = 0;

    while ((int32_t)USEC_TIMER - stop < 0)
    {
        level ^= 1;
        GPIOCMD = 0x0060e | level;
        udelay(period >> 1);
    }

    GPIOCMD = 0x0060e;
}

void piezo_seq(uint16_t *seq)
{
    uint16_t period;

    while ((period = *seq++) != 0)
    {
        piezo_tone(period, *seq++);
        udelay(*seq++ * 1000);
    }
}
#endif