Subversion Repositories freemyipod

Rev

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

Rev 85 Rev 87
Line 26... Line 26...
26
#include "thread.h"
26
#include "thread.h"
27
#include "s5l8720.h"
27
#include "s5l8720.h"
28
 
28
 
29
 
29
 
30
static struct mutex i2cmutex;
30
static struct mutex i2cmutex;
31
static struct wakeup i2cwakeup;
-
 
32
 
31
 
33
 
32
 
34
void i2c_init()
33
void i2c_init()
35
{
34
{
36
    mutex_init(&i2cmutex);
35
    mutex_init(&i2cmutex);
37
    wakeup_init(&i2cwakeup);
-
 
38
 
-
 
39
    interrupt_enable(IRQ_IIC, true);
-
 
40
}
36
}
41
 
37
 
42
void i2c_send(uint32_t bus, uint32_t device, uint32_t address, const uint8_t* data, uint32_t length)
38
void i2c_send(uint32_t bus, uint32_t device, uint32_t address, const uint8_t* data, uint32_t length)
43
{
39
{
44
    mutex_lock(&i2cmutex, TIMEOUT_BLOCK);
40
    mutex_lock(&i2cmutex, TIMEOUT_BLOCK);
-
 
41
    IICDS(bus) = device & ~1;
-
 
42
    IICSTAT(bus) = 0xF0;
-
 
43
    IICCON(bus) = 0xB7;
-
 
44
    while ((IICCON(bus) & 0x10) == 0) yield();
-
 
45
    if (address >= 0)
45
 
46
    {
-
 
47
        /* write address */
-
 
48
        IICDS(bus) = address;
-
 
49
        IICCON(bus) = 0xB7;
-
 
50
        while ((IICCON(bus) & 0x10) == 0) yield();
-
 
51
    }
-
 
52
    /* write data */
-
 
53
    while (length--)
-
 
54
    {
-
 
55
        IICDS(bus) = *data++;
-
 
56
        IICCON(bus) = 0xB7;
-
 
57
        while ((IICCON(bus) & 0x10) == 0) yield();
-
 
58
    }
-
 
59
    /* STOP */
-
 
60
    IICSTAT(bus) = 0xD0;
-
 
61
    IICCON(bus) = 0xB7;
-
 
62
    while ((IICSTAT(bus) & (1 << 5)) != 0) yield();
46
    mutex_unlock(&i2cmutex);
63
    mutex_unlock(&i2cmutex);
47
}
64
}
48
 
65
 
49
void i2c_recv(uint32_t bus, uint32_t device, uint32_t address, uint8_t* data, uint32_t length)
66
void i2c_recv(uint32_t bus, uint32_t device, uint32_t address, uint8_t* data, uint32_t length)
50
{
67
{
51
    mutex_lock(&i2cmutex, TIMEOUT_BLOCK);
68
    mutex_lock(&i2cmutex, TIMEOUT_BLOCK);
-
 
69
    if (address >= 0)
52
 
70
    {
-
 
71
        /* START */
-
 
72
        IICDS(bus) = device & ~1;
-
 
73
        IICSTAT(bus) = 0xF0;
-
 
74
        IICCON(bus) = 0xB7;
-
 
75
        while ((IICCON(bus) & 0x10) == 0) yield();
-
 
76
        /* write address */
-
 
77
        IICDS(bus) = address;
-
 
78
        IICCON(bus) = 0xB7;
-
 
79
        while ((IICCON(bus) & 0x10) == 0) yield();
-
 
80
    }
-
 
81
    /* (repeated) START */
-
 
82
    IICDS(bus) = device | 1;
-
 
83
    IICSTAT(bus) = 0xB0;
-
 
84
    IICCON(bus) = 0xB7;
-
 
85
    while ((IICCON(bus) & 0x10) == 0) yield();
-
 
86
    while (length--)
-
 
87
    {
-
 
88
        IICCON(bus) = (length == 0) ? 0x37 : 0xB7; /* NACK or ACK */
-
 
89
        while ((IICCON(bus) & 0x10) == 0) yield();
-
 
90
        *data++ = IICDS(bus);
-
 
91
    }
-
 
92
    /* STOP */
-
 
93
    IICSTAT(bus) = 0x90;
-
 
94
    IICCON(bus) = 0xB7;
-
 
95
    while ((IICSTAT(bus) & (1 << 5)) != 0) yield();
53
    mutex_unlock(&i2cmutex);
96
    mutex_unlock(&i2cmutex);
54
}
97
}
55
 
98
 
56
void i2c_sendbyte(uint32_t bus, uint32_t device, uint32_t address, uint32_t data)
99
void i2c_sendbyte(uint32_t bus, uint32_t device, uint32_t address, uint32_t data)
57
{
100
{
Line 64... Line 107...
64
{
107
{
65
    uint8_t buf[1];
108
    uint8_t buf[1];
66
    i2c_recv(bus, device, address, buf, 1);
109
    i2c_recv(bus, device, address, buf, 1);
67
    return buf[0];
110
    return buf[0];
68
}
111
}
69
 
-
 
70
void INT_IIC()
-
 
71
{
-
 
72
 
-
 
73
    wakeup_signal(&i2cwakeup);
-
 
74
}
-