summaryrefslogtreecommitdiffstats
path: root/firmware/target/arm/tcc77x/iaudio7/ata2501.c
blob: bc39872de6a7c28efc60eb80d21baeda3e891542 (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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2008 Vitja Makarov
 *
 * 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 "button.h"

#include "ata2501.h"

#define STB   (1<<5)
#define SDATA (1<<4)
#define RESET (1<<6)
#define SIFMD (1<<7)
#define STB_DELAY 200

#define udelay _udelay

/* do we really need it? */
static void _udelay(int cycles)
{
    cycles /= 8;
    while (cycles--) {
        asm("nop;nop;");
    }
}

/*
  TODO: sensitivity using GPIOS
*/
void ata2501_init(void)
{
    GPIOD_DIR |= (RESET | STB | SIFMD | (1 << 8) | (1 << 9));
    GPIOD_DIR &= ~(SDATA);

    GPIOD &= ~RESET;
    udelay(1000);

    GPIOD |= RESET;

    GPIOD &= ~STB;

#if 1
    GPIOD &= ~((1 << 9) | (1 << 8));
    GPIOD |= ((1 << 8) | SIFMD) | (1 << 9);
#else
    GPIOD |= ((1 << 9) | (1 << 8));
    GPIOD &= ~(SIFMD);
#endif
}

unsigned short ata2501_read(void)
{
    unsigned short ret = 0;
    int i;

    for (i = 0; i < 12; i++) {
        GPIOD |= STB;
        udelay(50);

        ret <<= 1;
        if (GPIOD & SDATA)
            ret |= 1;
        udelay(50);
        GPIOD &= ~STB;
        udelay(100);
    }

    return ret;
}

//#define ATA2501_TEST
#ifdef ATA2501_TEST
#include "lcd.h"
#include "sprintf.h"

static
void bits(char *str, unsigned short val)
{
    int i;

    for (i = 0; i < 12; i++)
        str[i] = (val & (1 << i)) ? '1' : '0';
    str[i] = 0;
}

void ata2501_test(void)
{
    char buf[100];
    ata2501_init();

    while (1) {
        unsigned short data;
        int line = 0;

        data = ata2501_read();
        lcd_clear_display();
        lcd_puts(0, line++, "ATA2501 test");

        bits(buf, data);
        lcd_puts(0, line++, buf);

        lcd_update();
        udelay(2000);
    }
}
#endif