Subversion Repositories freemyipod

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
15 theseven 1
//
2
//
3
//    Copyright 2010 TheSeven
4
//
5
//
427 farthen 6
//    This file is part of emCORE.
15 theseven 7
//
427 farthen 8
//    emCORE is free software: you can redistribute it and/or
15 theseven 9
//    modify it under the terms of the GNU General Public License as
10
//    published by the Free Software Foundation, either version 2 of the
11
//    License, or (at your option) any later version.
12
//
427 farthen 13
//    emCORE is distributed in the hope that it will be useful,
15 theseven 14
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
15
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16
//    See the GNU General Public License for more details.
17
//
18
//    You should have received a copy of the GNU General Public License along
427 farthen 19
//    with emCORE.  If not, see <http://www.gnu.org/licenses/>.
15 theseven 20
//
21
//
22
 
23
 
24
#include "global.h"
25
#include "i2c.h"
26
#include "thread.h"
27
#include "s5l8701.h"
28
 
29
 
30
static struct mutex i2cmutex;
31
static struct wakeup i2cwakeup;
32
 
33
 
560 theseven 34
static void i2c_on()
15 theseven 35
{
36
    /* enable I2C clock */
88 theseven 37
    PWRCON(0) &= ~(1 << 5);
15 theseven 38
 
39
    IICCON = (1 << 7) | /* ACK_GEN */
40
             (0 << 6) | /* CLKSEL = PCLK/16 */
41
             (1 << 5) | /* INT_EN */
42
             (1 << 4) | /* IRQ clear */
43
             (7 << 0);  /* CK_REG */
44
 
45
    /* serial output on */
46
    IICSTAT = (1 << 4);
560 theseven 47
}
15 theseven 48
 
560 theseven 49
static void i2c_off()
50
{
51
    /* serial output off */
52
    IICSTAT = 0;
53
 
54
    /* disable I2C clock */
55
    PWRCON(0) |= (1 << 5);
56
}
57
 
58
void i2c_init()
59
{
60
    mutex_init(&i2cmutex);
61
    wakeup_init(&i2cwakeup);
62
 
63
    /* enable I2C pins */
64
    PCON10 = (2 << 2) |
65
             (2 << 0);
66
 
85 theseven 67
    interrupt_enable(IRQ_IIC, true);
15 theseven 68
}
69
 
70
void i2c_send(uint32_t bus, uint32_t device, uint32_t address, const uint8_t* data, uint32_t length)
71
{
72
    mutex_lock(&i2cmutex, TIMEOUT_BLOCK);
560 theseven 73
	i2c_on();
15 theseven 74
    IICDS = device & ~1;
75
    IICSTAT = 0xF0;
76
    IICCON = 0xB7;
77
    wakeup_wait(&i2cwakeup, TIMEOUT_BLOCK);
78
    if (address >= 0)
79
    {
80
        /* write address */
81
        IICDS = address;
82
        IICCON = 0xB7;
83
        wakeup_wait(&i2cwakeup, TIMEOUT_BLOCK);
84
    }
85
    /* write data */
86
    while (length--)
87
    {
88
        IICDS = *data++;
89
        IICCON = 0xB7;
90
        wakeup_wait(&i2cwakeup, TIMEOUT_BLOCK);
91
    }
92
    /* STOP */
93
    IICSTAT = 0xD0;
94
    IICCON = 0xB7;
95
    while ((IICSTAT & (1 << 5)) != 0) yield();
560 theseven 96
	i2c_off();
15 theseven 97
    mutex_unlock(&i2cmutex);
98
}
99
 
100
void i2c_recv(uint32_t bus, uint32_t device, uint32_t address, uint8_t* data, uint32_t length)
101
{
102
    mutex_lock(&i2cmutex, TIMEOUT_BLOCK);
560 theseven 103
	i2c_on();
15 theseven 104
    if (address >= 0)
105
    {
106
        /* START */
107
        IICDS = device & ~1;
108
        IICSTAT = 0xF0;
109
        IICCON = 0xB7;
110
        wakeup_wait(&i2cwakeup, TIMEOUT_BLOCK);
111
        /* write address */
112
        IICDS = address;
113
        IICCON = 0xB7;
114
        wakeup_wait(&i2cwakeup, TIMEOUT_BLOCK);
115
    }
116
    /* (repeated) START */
117
    IICDS = device | 1;
118
    IICSTAT = 0xB0;
119
    IICCON = 0xB7;
120
    wakeup_wait(&i2cwakeup, TIMEOUT_BLOCK);
121
    while (length--)
122
    {
123
        IICCON = (length == 0) ? 0x37 : 0xB7; /* NACK or ACK */
124
        wakeup_wait(&i2cwakeup, TIMEOUT_BLOCK);
125
        *data++ = IICDS;
126
    }
127
    /* STOP */
128
    IICSTAT = 0x90;
129
    IICCON = 0xB7;
130
    while ((IICSTAT & (1 << 5)) != 0) yield();
560 theseven 131
	i2c_off();
15 theseven 132
    mutex_unlock(&i2cmutex);
133
}
134
 
135
void i2c_sendbyte(uint32_t bus, uint32_t device, uint32_t address, uint32_t data)
136
{
137
    uint8_t buf[1];
138
    buf[0] = data;
139
    i2c_send(bus, device, address, buf, 1);
140
}
141
 
142
uint8_t i2c_recvbyte(uint32_t bus, uint32_t device, uint32_t address)
143
{
144
    uint8_t buf[1];
145
    i2c_recv(bus, device, address, buf, 1);
146
    return buf[0];
147
}
148
 
149
void INT_IIC()
150
{
151
    /* disable interrupt (but don't clear it yet) */
152
    IICCON &= ~((1 << 4) | (1 << 5));
153
 
154
    wakeup_signal(&i2cwakeup);
155
}