Subversion Repositories freemyipod

Rev

Rev 427 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 427 Rev 560
Line 28... Line 28...
28
 
28
 
29
 
29
 
30
static struct mutex i2cmutex;
30
static struct mutex i2cmutex;
31
 
31
 
32
 
32
 
-
 
33
static void i2c_on(int bus)
-
 
34
{
-
 
35
    /* enable I2C clock */
-
 
36
    clockgate_enable(CLOCKGATE_I2C(bus), true);
-
 
37
 
-
 
38
    IICCON(bus) = (1 << 7) | /* ACK_GEN */
-
 
39
                  (0 << 6) | /* CLKSEL = PCLK/16 */
-
 
40
                  (1 << 5) | /* INT_EN */
-
 
41
                  (1 << 4) | /* IRQ clear */
-
 
42
                  (7 << 0);  /* CK_REG */
-
 
43
 
-
 
44
    /* serial output on */
-
 
45
    IICSTAT(bus) = (1 << 4);
-
 
46
}
-
 
47
 
-
 
48
static void i2c_off(int bus)
-
 
49
{
-
 
50
    /* serial output off */
-
 
51
    IICSTAT(bus) = 0;
-
 
52
 
-
 
53
    /* disable I2C clock */
-
 
54
    clockgate_enable(CLOCKGATE_I2C(bus), false);
-
 
55
}
-
 
56
 
33
void i2c_init()
57
void i2c_init()
34
{
58
{
35
    mutex_init(&i2cmutex);
59
    mutex_init(&i2cmutex);
36
}
60
}
37
 
61
 
38
void i2c_send(uint32_t bus, uint32_t device, uint32_t address, const uint8_t* data, uint32_t length)
62
void i2c_send(uint32_t bus, uint32_t device, uint32_t address, const uint8_t* data, uint32_t length)
39
{
63
{
40
    mutex_lock(&i2cmutex, TIMEOUT_BLOCK);
64
    mutex_lock(&i2cmutex, TIMEOUT_BLOCK);
-
 
65
	i2c_on(bus);
41
    while (IIC10(bus));
66
    while (IIC10(bus));
42
    IICDS(bus) = device & ~1;
67
    IICDS(bus) = device & ~1;
43
    while (IIC10(bus));
68
    while (IIC10(bus));
44
    IICSTAT(bus) = 0xF0;
69
    IICSTAT(bus) = 0xF0;
45
    while (IIC10(bus));
70
    while (IIC10(bus));
Line 67... Line 92...
67
    while (IIC10(bus));
92
    while (IIC10(bus));
68
    IICSTAT(bus) = 0xD0;
93
    IICSTAT(bus) = 0xD0;
69
    while (IIC10(bus));
94
    while (IIC10(bus));
70
    IICCON(bus) = 0xB7;
95
    IICCON(bus) = 0xB7;
71
    while ((IICSTAT(bus) & (1 << 5)) != 0) yield();
96
    while ((IICSTAT(bus) & (1 << 5)) != 0) yield();
-
 
97
	i2c_off(bus);
72
    mutex_unlock(&i2cmutex);
98
    mutex_unlock(&i2cmutex);
73
}
99
}
74
 
100
 
75
void i2c_recv(uint32_t bus, uint32_t device, uint32_t address, uint8_t* data, uint32_t length)
101
void i2c_recv(uint32_t bus, uint32_t device, uint32_t address, uint8_t* data, uint32_t length)
76
{
102
{
77
    mutex_lock(&i2cmutex, TIMEOUT_BLOCK);
103
    mutex_lock(&i2cmutex, TIMEOUT_BLOCK);
-
 
104
	i2c_on(bus);
78
    if (address >= 0)
105
    if (address >= 0)
79
    {
106
    {
80
        /* START */
107
        /* START */
81
        while (IIC10(bus));
108
        while (IIC10(bus));
82
        IICDS(bus) = device & ~1;
109
        IICDS(bus) = device & ~1;
Line 111... Line 138...
111
    while (IIC10(bus));
138
    while (IIC10(bus));
112
    IICSTAT(bus) = 0x90;
139
    IICSTAT(bus) = 0x90;
113
    while (IIC10(bus));
140
    while (IIC10(bus));
114
    IICCON(bus) = 0xB7;
141
    IICCON(bus) = 0xB7;
115
    while ((IICSTAT(bus) & (1 << 5)) != 0) yield();
142
    while ((IICSTAT(bus) & (1 << 5)) != 0) yield();
-
 
143
	i2c_off(bus);
116
    mutex_unlock(&i2cmutex);
144
    mutex_unlock(&i2cmutex);
117
}
145
}
118
 
146
 
119
void i2c_sendbyte(uint32_t bus, uint32_t device, uint32_t address, uint32_t data)
147
void i2c_sendbyte(uint32_t bus, uint32_t device, uint32_t address, uint32_t data)
120
{
148
{