Subversion Repositories freemyipod

Rev

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

Rev 427 Rev 560
Line 29... Line 29...
29
 
29
 
30
static struct mutex i2cmutex;
30
static struct mutex i2cmutex;
31
static struct wakeup i2cwakeup;
31
static struct wakeup i2cwakeup;
32
 
32
 
33
 
33
 
34
void i2c_init()
34
static void i2c_on()
35
{
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 */
36
    /* enable I2C clock */
44
    PWRCON(0) &= ~(1 << 5);
37
    PWRCON(0) &= ~(1 << 5);
45
 
38
 
46
    /* initial config */
-
 
47
    IICADD = 0;
-
 
48
    IICCON = (1 << 7) | /* ACK_GEN */
39
    IICCON = (1 << 7) | /* ACK_GEN */
49
             (0 << 6) | /* CLKSEL = PCLK/16 */
40
             (0 << 6) | /* CLKSEL = PCLK/16 */
50
             (1 << 5) | /* INT_EN */
41
             (1 << 5) | /* INT_EN */
51
             (1 << 4) | /* IRQ clear */
42
             (1 << 4) | /* IRQ clear */
52
             (7 << 0);  /* CK_REG */
43
             (7 << 0);  /* CK_REG */
53
 
44
 
54
    /* serial output on */
45
    /* serial output on */
55
    IICSTAT = (1 << 4);
46
    IICSTAT = (1 << 4);
-
 
47
}
-
 
48
 
-
 
49
static void i2c_off()
-
 
50
{
-
 
51
    /* serial output off */
-
 
52
    IICSTAT = 0;
56
 
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
			 
57
    interrupt_enable(IRQ_IIC, true);
67
    interrupt_enable(IRQ_IIC, true);
58
}
68
}
59
 
69
 
60
void i2c_send(uint32_t bus, uint32_t device, uint32_t address, const uint8_t* data, uint32_t length)
70
void i2c_send(uint32_t bus, uint32_t device, uint32_t address, const uint8_t* data, uint32_t length)
61
{
71
{
62
    mutex_lock(&i2cmutex, TIMEOUT_BLOCK);
72
    mutex_lock(&i2cmutex, TIMEOUT_BLOCK);
-
 
73
	i2c_on();
63
    IICDS = device & ~1;
74
    IICDS = device & ~1;
64
    IICSTAT = 0xF0;
75
    IICSTAT = 0xF0;
65
    IICCON = 0xB7;
76
    IICCON = 0xB7;
66
    wakeup_wait(&i2cwakeup, TIMEOUT_BLOCK);
77
    wakeup_wait(&i2cwakeup, TIMEOUT_BLOCK);
67
    if (address >= 0)
78
    if (address >= 0)
Line 80... Line 91...
80
    }
91
    }
81
    /* STOP */
92
    /* STOP */
82
    IICSTAT = 0xD0;
93
    IICSTAT = 0xD0;
83
    IICCON = 0xB7;
94
    IICCON = 0xB7;
84
    while ((IICSTAT & (1 << 5)) != 0) yield();
95
    while ((IICSTAT & (1 << 5)) != 0) yield();
-
 
96
	i2c_off();
85
    mutex_unlock(&i2cmutex);
97
    mutex_unlock(&i2cmutex);
86
}
98
}
87
 
99
 
88
void i2c_recv(uint32_t bus, uint32_t device, uint32_t address, uint8_t* data, uint32_t length)
100
void i2c_recv(uint32_t bus, uint32_t device, uint32_t address, uint8_t* data, uint32_t length)
89
{
101
{
90
    mutex_lock(&i2cmutex, TIMEOUT_BLOCK);
102
    mutex_lock(&i2cmutex, TIMEOUT_BLOCK);
-
 
103
	i2c_on();
91
    if (address >= 0)
104
    if (address >= 0)
92
    {
105
    {
93
        /* START */
106
        /* START */
94
        IICDS = device & ~1;
107
        IICDS = device & ~1;
95
        IICSTAT = 0xF0;
108
        IICSTAT = 0xF0;
Line 113... Line 126...
113
    }
126
    }
114
    /* STOP */
127
    /* STOP */
115
    IICSTAT = 0x90;
128
    IICSTAT = 0x90;
116
    IICCON = 0xB7;
129
    IICCON = 0xB7;
117
    while ((IICSTAT & (1 << 5)) != 0) yield();
130
    while ((IICSTAT & (1 << 5)) != 0) yield();
-
 
131
	i2c_off();
118
    mutex_unlock(&i2cmutex);
132
    mutex_unlock(&i2cmutex);
119
}
133
}
120
 
134
 
121
void i2c_sendbyte(uint32_t bus, uint32_t device, uint32_t address, uint32_t data)
135
void i2c_sendbyte(uint32_t bus, uint32_t device, uint32_t address, uint32_t data)
122
{
136
{