1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
|
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2019-07-29 zdzn first version
*/
#include "drv_i2c.h"
rt_uint8_t i2c_read_or_write(volatile rt_uint32_t base, rt_uint8_t* buf, rt_uint32_t len, rt_uint8_t flag)
{
rt_uint32_t status;
rt_uint32_t remaining = len;
rt_uint32_t i = 0;
rt_uint8_t reason = BCM283X_I2C_REASON_OK;
/* Clear FIFO */
BCM283X_BSC_C(base) |= (BSC_C_CLEAR_1 & BSC_C_CLEAR_1);
/* Clear Status */
BCM283X_BSC_S(base) = BSC_S_CLKT | BSC_S_ERR | BSC_S_DONE;
/* Set Data Length */
BCM283X_BSC_DLEN(base) = len;
if (flag)
{
/* Start read */
BCM283X_BSC_C(base) = BSC_C_I2CEN | BSC_C_ST | BSC_C_READ;
/* wait for transfer to complete */
while (!(BCM283X_BSC_S(base) & BSC_S_DONE))
{
/* we must empty the FIFO as it is populated and not use any delay */
while (remaining && (BCM283X_BSC_S(base) & BSC_S_RXD))
{
/* Read from FIFO, no barrier */
buf[i] = BCM283X_BSC_FIFO(base);
i++;
remaining--;
}
}
/* transfer has finished - grab any remaining stuff in FIFO */
while (remaining && (BCM283X_BSC_S(base) & BSC_S_RXD))
{
/* Read from FIFO, no barrier */
buf[i] = BCM283X_BSC_FIFO(base);
i++;
remaining--;
}
}
else
{
/* pre populate FIFO with max buffer */
while (remaining && (i < BSC_FIFO_SIZE))
{
BCM283X_BSC_FIFO(base) = buf[i];
i++;
remaining--;
}
/* Enable device and start transfer */
BCM283X_BSC_C(base) = BSC_C_I2CEN | BSC_C_ST;
/* Transfer is over when BCM2835_BSC_S_DONE */
while (!(BCM283X_BSC_S(base) & BSC_S_DONE))
{
while (remaining && (BCM283X_BSC_S(base) & BSC_S_TXD))
{
/* Write to FIFO */
BCM283X_BSC_FIFO(base) = buf[i];
i++;
remaining--;
}
}
}
status = BCM283X_BSC_S(base);
if (status & BSC_S_ERR)
{
reason = BCM283X_I2C_REASON_ERROR_NACK;
}
else if (status & BSC_S_CLKT)
{
reason = BCM283X_I2C_REASON_ERROR_CLKT;
}
else if (remaining)
{
reason = BCM283X_I2C_REASON_ERROR_DATA;
}
BCM283X_BSC_C(base) |= (BSC_S_DONE & BSC_S_DONE);
return reason;
}
struct raspi_i2c_hw_config
{
rt_uint8_t bsc_num;
rt_uint8_t sdl_pin;
rt_uint8_t scl_pin;
rt_uint8_t sdl_mode;
rt_uint8_t scl_mode;
};
#if (defined(BSP_USING_I2C0) || defined(BSP_USING_I2C1))
static rt_ssize_t raspi_i2c_mst_xfer(struct rt_i2c_bus_device *bus,
struct rt_i2c_msg msgs[],
rt_uint32_t num);
static rt_ssize_t raspi_i2c_slv_xfer(struct rt_i2c_bus_device *bus,
struct rt_i2c_msg msgs[],
rt_uint32_t num);
static rt_err_t raspi_i2c_bus_control(struct rt_i2c_bus_device *bus,
int cmd,
void *args);
static rt_uint32_t i2c_byte_wait_us = 0;
static rt_ssize_t raspi_i2c_mst_xfer(struct rt_i2c_bus_device *bus,
struct rt_i2c_msg msgs[],
rt_uint32_t num)
{
rt_size_t i;
rt_uint8_t reason;
RT_ASSERT(bus != RT_NULL);
volatile rt_uint32_t base = (volatile rt_uint32_t)(bus->parent.user_data);
if (bus->parent.user_data == 0)
base = BCM283X_BSC0_BASE;
else
base = BCM283X_BSC1_BASE;
BCM283X_BSC_A(base) = msgs->addr;
for (i = 0; i < num; i++)
{
if (msgs[i].flags & RT_I2C_RD)
reason = i2c_read_or_write(base, msgs->buf, msgs->len, 1);
else
reason = i2c_read_or_write(base, msgs->buf, msgs->len, 0);
}
return (reason == 0)? i : 0;
}
static rt_ssize_t raspi_i2c_slv_xfer(struct rt_i2c_bus_device *bus,
struct rt_i2c_msg msgs[],
rt_uint32_t num)
{
return 0;
}
static rt_err_t raspi_i2c_bus_control(struct rt_i2c_bus_device *bus,
int cmd,
void *args)
{
return RT_EOK;
}
static const struct rt_i2c_bus_device_ops raspi_i2c_ops =
{
.master_xfer = raspi_i2c_mst_xfer,
.slave_xfer = raspi_i2c_slv_xfer,
.i2c_bus_control = raspi_i2c_bus_control,
};
static rt_err_t raspi_i2c_configure(struct raspi_i2c_hw_config *cfg)
{
RT_ASSERT(cfg != RT_NULL);
volatile rt_uint32_t base = cfg->scl_mode ? BCM283X_BSC1_BASE : BCM283X_BSC0_BASE;
GPIO_FSEL(cfg->sdl_pin, cfg->sdl_mode); /* SDA */
GPIO_FSEL(cfg->scl_pin, cfg->scl_mode); /* SCL */
/* use 0xFFFE mask to limit a max value and round down any odd number */
rt_uint32_t divider = (BCM283X_CORE_CLK_HZ / 10000) & 0xFFFE;
BCM283X_BSC_DIV(base) = (rt_uint16_t) divider;
i2c_byte_wait_us = (divider * 1000000 * 9 / BCM283X_CORE_CLK_HZ);
return RT_EOK;
}
#endif
#if defined (BSP_USING_I2C0)
#define I2C0_BUS_NAME "i2c0"
static struct raspi_i2c_hw_config hw_device0 =
{
.bsc_num = 0,
.sdl_pin = RPI_GPIO_P1_27,
.scl_pin = RPI_GPIO_P1_28,
.sdl_mode = BCM283X_GPIO_FSEL_ALT0,
.scl_mode = BCM283X_GPIO_FSEL_ALT0,
};
struct rt_i2c_bus_device device0 =
{
.ops = &raspi_i2c_ops,
};
#endif
#if defined (BSP_USING_I2C1)
#define I2C1_BUS_NAME "i2c1"
static struct raspi_i2c_hw_config hw_device1 =
{
.bsc_num = 1,
.sdl_pin = RPI_GPIO_P1_03,
.scl_pin = RPI_GPIO_P1_05,
.sdl_mode = BCM283X_GPIO_FSEL_ALT0,
.scl_mode = BCM283X_GPIO_FSEL_ALT0,
};
struct rt_i2c_bus_device device1 =
{
.ops = &raspi_i2c_ops,
};
#endif
int rt_hw_i2c_init(void)
{
#if defined(BSP_USING_I2C0)
device0.parent.user_data = (void *)0;
raspi_i2c_configure(&hw_device0);
rt_i2c_bus_device_register(&device0, I2C0_BUS_NAME);
#endif
#if defined(BSP_USING_I2C1)
device1.parent.user_data = (void *)1;
raspi_i2c_configure(&hw_device1);
rt_i2c_bus_device_register(&device1, I2C1_BUS_NAME);
#endif
return 0;
}
INIT_DEVICE_EXPORT(rt_hw_i2c_init);
|