summaryrefslogtreecommitdiff
path: root/bsp/raspberry-pi/raspi4-32/driver/drv_i2c.c
blob: 54d2d1950b6317e04ffffa798f2f5bd388fd2ede (plain)
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author         Notes
 * 2020-11-28     bigmagic       first version
 */

#include "drv_i2c.h"
#include "drv_gpio.h"
#include "raspi4.h"
#include "mbox.h"

/*
* (3.3v)          -1   2-
* (SDA1/SDA3)     -3   4-
* (SCL1/SCL3)     -5   6-
* (SDA3)          -7   8-
*                 -9  10-
*                 -11 12-
*                 -13 14-
*                 -15 16-
*                 -17 18-
*                 -19 20-
* (SCL4)          -21 22-
*                 -23 24-    (SDA4)
*                 -25 26-    (SCL4)
*                 -27 28-
* (SCL3)          -29 30-
* (SDA4)          -31 32-
*/

#define DBG_TAG               "drv.i2c"
#define DBG_LVL               DBG_INFO
#include <rtdbg.h>

struct raspi_i2c_hw_config
{
    rt_uint32_t bsc_num;
    rt_uint32_t bsc_rate;
    rt_uint32_t bsc_address;
    rt_uint32_t sda_pin;
    rt_uint32_t scl_pin;
    rt_uint32_t sda_mode;
    rt_uint32_t scl_mode;
};

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 = I2C_REASON_OK;

    /* Clear FIFO */
    BSC_C(base) |= (BSC_C_CLEAR_1 & BSC_C_CLEAR_1);
    /* Clear Status */
    BSC_S(base) = BSC_S_CLKT | BSC_S_ERR | BSC_S_DONE;
    /* Set Data Length */
    BSC_DLEN(base) = len;
    if (flag)
    {
        /* Start read */
        BSC_C(base) = BSC_C_I2CEN | BSC_C_ST | BSC_C_READ;
        /* wait for transfer to complete */
        while (!(BSC_S(base) & BSC_S_DONE))
        {
            /* we must empty the FIFO as it is populated and not use any delay */
            while (remaining && (BSC_S(base) & BSC_S_RXD))
            {
                /* Read from FIFO, no barrier */
                buf[i] = BSC_FIFO(base);
                i++;
                remaining--;
            }
        }
        /* transfer has finished - grab any remaining stuff in FIFO */
        while (remaining && (BSC_S(base) & BSC_S_RXD))
        {
            /* Read from FIFO, no barrier */
            buf[i] = BSC_FIFO(base);
            i++;
            remaining--;
        }
    }
    else
    {
        LOG_D("i2c%d write start", flag);
        /* pre populate FIFO with max buffer */
        while (remaining && (i < BSC_FIFO_SIZE))
        {
            BSC_FIFO(base) = buf[i];
            i++;
            remaining--;
        }

        /* Enable device and start transfer */
        BSC_C(base) = BSC_C_I2CEN | BSC_C_ST;

        /* Transfer is over when BCM2835_BSC_S_DONE */
        while (!(BSC_S(base) & BSC_S_DONE))
        {
            while (remaining && (BSC_S(base) & BSC_S_TXD))
            {
                /* Write to FIFO */
                BSC_FIFO(base) = buf[i];
                i++;
                remaining--;
            }
        }
        LOG_D("i2c%d write end", flag);
    }

    status = BSC_S(base);
    if (status & BSC_S_ERR)
    {
        reason = I2C_REASON_ERROR_NACK;
    }
    else if (status & BSC_S_CLKT)
    {
        reason = I2C_REASON_ERROR_CLKT;
    }
    else if (remaining)
    {
        reason = I2C_REASON_ERROR_DATA;
    }
    BSC_C(base) |= (BSC_S_DONE & BSC_S_DONE);

    return reason;
}

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);

    struct raspi_i2c_hw_config *i2c_hw_config = (struct raspi_i2c_hw_config*)(bus->priv);

    //Slave Address
    BSC_A(i2c_hw_config->bsc_address) = msgs->addr;

    for (i = 0; i < num; i++)
    {
        if (msgs[i].flags & RT_I2C_RD)
            reason = i2c_read_or_write(i2c_hw_config->bsc_address, msgs->buf, msgs->len, 1);
        else
            reason = i2c_read_or_write(i2c_hw_config->bsc_address, 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 rt_err_t raspi_i2c_configure(struct raspi_i2c_hw_config *cfg)
{
    RT_ASSERT(cfg != RT_NULL);
    rt_uint32_t apb_clock = 0;
    prev_raspi_pin_mode(cfg->sda_pin, cfg->sda_mode);//sda
    prev_raspi_pin_mode(cfg->scl_pin, cfg->scl_mode);//scl
    /* use 0xFFFE mask to limit a max value and round down any odd number */
    apb_clock = bcm271x_mbox_clock_get_rate(CORE_CLK_ID);
    rt_uint32_t divider = (apb_clock / cfg->bsc_rate) & 0xFFFE;

    BSC_DIV(cfg->bsc_address) = (rt_uint16_t)divider;

    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,
};

#if defined (BSP_USING_I2C0)
#define I2C0_BUS_NAME    "i2c0"
static struct raspi_i2c_hw_config hw_device0 =
{
    .bsc_num = 0,
    .bsc_rate = 100000,//100k
    .bsc_address = BSC0_BASE,
    .sda_pin = GPIO_PIN_0,
    .scl_pin = GPIO_PIN_1,
    .sda_mode = ALT0,
    .scl_mode = ALT0,
};

struct rt_i2c_bus_device device0 =
{
    .ops = &raspi_i2c_ops,
    .priv =  (void *)&hw_device0,
};
#endif

#if defined (BSP_USING_I2C1)
#define I2C1_BUS_NAME    "i2c1"
static struct raspi_i2c_hw_config hw_device1 =
{
    .bsc_num = 1,
    .bsc_rate = 100000,//100k
    .bsc_address = BSC1_BASE,
    .sda_pin = GPIO_PIN_2,
    .scl_pin = GPIO_PIN_3,
    .sda_mode = ALT0,
    .scl_mode = ALT0,
};

struct rt_i2c_bus_device device1 =
{
    .ops = &raspi_i2c_ops,
    .priv =  (void *)&hw_device1,
};
#endif

#if defined (BSP_USING_I2C3)
#define I2C3_BUS_NAME    "i2c3"
static struct raspi_i2c_hw_config hw_device3 =
{
    .bsc_num = 3,
    .bsc_rate = 100000,//100k
    .bsc_address = BSC3_BASE,
#ifndef BSP_USING_I2C3_0
    .sda_pin = GPIO_PIN_2,
    .scl_pin = GPIO_PIN_3,
#else
    .sda_pin = GPIO_PIN_4,
    .scl_pin = GPIO_PIN_5,
#endif
    .sda_mode = ALT5,
    .scl_mode = ALT5,
};

struct rt_i2c_bus_device device3 =
{
    .ops = &raspi_i2c_ops,
    .priv =  (void *)&hw_device3,
};
#endif

#if defined (BSP_USING_I2C4)
#define I2C4_BUS_NAME    "i2c4"
static struct raspi_i2c_hw_config hw_device4 =
{
    .bsc_num = 4,
    .bsc_rate = 100000,//100k
    .bsc_address = BSC4_BASE,
#ifdef BSP_USING_I2C4_0
    .sda_pin = GPIO_PIN_6,
    .scl_pin = GPIO_PIN_7,
#else
    .sda_pin = GPIO_PIN_8,
    .scl_pin = GPIO_PIN_9,
#endif
    .sda_mode = ALT5,
    .scl_mode = ALT5,
};

struct rt_i2c_bus_device device4 =
{
    .ops = &raspi_i2c_ops,
    .priv =  (void *)&hw_device4,
};
#endif

#if defined (BSP_USING_I2C5)
#define I2C5_BUS_NAME    "i2c5"
static struct raspi_i2c_hw_config hw_device5 =
{
    .bsc_num = 5,
    .bsc_rate = 100000,//100k
    .bsc_address = BSC5_BASE,
#ifdef BSP_USING_I2C5_0
    .sda_pin = GPIO_PIN_10,
    .scl_pin = GPIO_PIN_11,
#else
    .sda_pin = GPIO_PIN_12,
    .scl_pin = GPIO_PIN_13,
#endif
    .sda_mode = ALT5,
    .scl_mode = ALT5,
};

struct rt_i2c_bus_device device5 =
{
    .ops = &raspi_i2c_ops,
    .priv =  (void *)&hw_device5,
};
#endif

#if defined (BSP_USING_I2C6)
#define I2C6_BUS_NAME    "i2c6"
static struct raspi_i2c_hw_config hw_device6 =
{
    .bsc_num = 6,
    .bsc_rate = 100000,//100k
    .bsc_address = BSC6_BASE,
#ifdef BSP_USING_I2C5_0
    .sda_pin = GPIO_PIN_0,
    .scl_pin = GPIO_PIN_1,
#else
    .sda_pin = GPIO_PIN_22,
    .scl_pin = GPIO_PIN_23,
#endif
    .sda_mode = ALT5,
    .scl_mode = ALT5,
};

struct rt_i2c_bus_device device6 =
{
    .ops = &raspi_i2c_ops,
    .priv =  (void *)&hw_device6,
};
#endif

int rt_hw_i2c_init(void)
{
#if defined(BSP_USING_I2C0)
    raspi_i2c_configure(&hw_device0);
    rt_i2c_bus_device_register(&device0, I2C0_BUS_NAME);
#endif

#if defined(BSP_USING_I2C1)
    raspi_i2c_configure(&hw_device1);
    rt_i2c_bus_device_register(&device1, I2C1_BUS_NAME);
#endif

#if defined(BSP_USING_I2C3)
    raspi_i2c_configure(&hw_device3);
    rt_i2c_bus_device_register(&device3, I2C3_BUS_NAME);
#endif

#if defined(BSP_USING_I2C4)
    raspi_i2c_configure(&hw_device4);
    rt_i2c_bus_device_register(&device4, I2C4_BUS_NAME);
#endif

#if defined(BSP_USING_I2C5)
    raspi_i2c_configure(&hw_device5);
    rt_i2c_bus_device_register(&device5, I2C5_BUS_NAME);
#endif

#if defined(BSP_USING_I2C6)
    raspi_i2c_configure(&hw_device6);
    rt_i2c_bus_device_register(&device6, I2C6_BUS_NAME);
#endif
    return 0;
}

INIT_DEVICE_EXPORT(rt_hw_i2c_init);