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
 
34
void i2c_init()
35
{
36
    mutex_init(&i2cmutex);
37
    wakeup_init(&i2cwakeup);
38
 
39
    /* enable I2C pins */
40
    PCON10 = (2 << 2) |
41
             (2 << 0);
42
 
43
    /* enable I2C clock */
88 theseven 44
    PWRCON(0) &= ~(1 << 5);
15 theseven 45
 
46
    /* initial config */
47
    IICADD = 0;
48
    IICCON = (1 << 7) | /* ACK_GEN */
49
             (0 << 6) | /* CLKSEL = PCLK/16 */
50
             (1 << 5) | /* INT_EN */
51
             (1 << 4) | /* IRQ clear */
52
             (7 << 0);  /* CK_REG */
53
 
54
    /* serial output on */
55
    IICSTAT = (1 << 4);
56
 
85 theseven 57
    interrupt_enable(IRQ_IIC, true);
15 theseven 58
}
59
 
60
void i2c_send(uint32_t bus, uint32_t device, uint32_t address, const uint8_t* data, uint32_t length)
61
{
62
    mutex_lock(&i2cmutex, TIMEOUT_BLOCK);
63
    IICDS = device & ~1;
64
    IICSTAT = 0xF0;
65
    IICCON = 0xB7;
66
    wakeup_wait(&i2cwakeup, TIMEOUT_BLOCK);
67
    if (address >= 0)
68
    {
69
        /* write address */
70
        IICDS = address;
71
        IICCON = 0xB7;
72
        wakeup_wait(&i2cwakeup, TIMEOUT_BLOCK);
73
    }
74
    /* write data */
75
    while (length--)
76
    {
77
        IICDS = *data++;
78
        IICCON = 0xB7;
79
        wakeup_wait(&i2cwakeup, TIMEOUT_BLOCK);
80
    }
81
    /* STOP */
82
    IICSTAT = 0xD0;
83
    IICCON = 0xB7;
84
    while ((IICSTAT & (1 << 5)) != 0) yield();
85
    mutex_unlock(&i2cmutex);
86
}
87
 
88
void i2c_recv(uint32_t bus, uint32_t device, uint32_t address, uint8_t* data, uint32_t length)
89
{
90
    mutex_lock(&i2cmutex, TIMEOUT_BLOCK);
91
    if (address >= 0)
92
    {
93
        /* START */
94
        IICDS = device & ~1;
95
        IICSTAT = 0xF0;
96
        IICCON = 0xB7;
97
        wakeup_wait(&i2cwakeup, TIMEOUT_BLOCK);
98
        /* write address */
99
        IICDS = address;
100
        IICCON = 0xB7;
101
        wakeup_wait(&i2cwakeup, TIMEOUT_BLOCK);
102
    }
103
    /* (repeated) START */
104
    IICDS = device | 1;
105
    IICSTAT = 0xB0;
106
    IICCON = 0xB7;
107
    wakeup_wait(&i2cwakeup, TIMEOUT_BLOCK);
108
    while (length--)
109
    {
110
        IICCON = (length == 0) ? 0x37 : 0xB7; /* NACK or ACK */
111
        wakeup_wait(&i2cwakeup, TIMEOUT_BLOCK);
112
        *data++ = IICDS;
113
    }
114
    /* STOP */
115
    IICSTAT = 0x90;
116
    IICCON = 0xB7;
117
    while ((IICSTAT & (1 << 5)) != 0) yield();
118
    mutex_unlock(&i2cmutex);
119
}
120
 
121
void i2c_sendbyte(uint32_t bus, uint32_t device, uint32_t address, uint32_t data)
122
{
123
    uint8_t buf[1];
124
    buf[0] = data;
125
    i2c_send(bus, device, address, buf, 1);
126
}
127
 
128
uint8_t i2c_recvbyte(uint32_t bus, uint32_t device, uint32_t address)
129
{
130
    uint8_t buf[1];
131
    i2c_recv(bus, device, address, buf, 1);
132
    return buf[0];
133
}
134
 
135
void INT_IIC()
136
{
137
    /* disable interrupt (but don't clear it yet) */
138
    IICCON &= ~((1 << 4) | (1 << 5));
139
 
140
    wakeup_signal(&i2cwakeup);
141
}