summaryrefslogtreecommitdiff
path: root/bsp/wch/risc-v/libraries/ch32_drivers/drv_i2c.c
blob: 7d6f58a92d84f7caad7eef08f88dedf1fbe9c577 (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
/*
 * Copyright (c) 2006-2024, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2024-4-1       IceBear003   the first version
 */

#include "board.h"
#include "drv_i2c.h"

#ifdef BSP_USING_HWI2C

#define LOG_TAG "drv.hwi2c"
#include "drv_log.h"

#if !defined(BSP_USING_I2C1) && !defined(BSP_USING_I2C2)
#error "Please define at least one BSP_USING_I2Cx"
/* this driver can be disabled at menuconfig -> RT-Thread Components -> Device Drivers */
#endif

#define TIMEOUT 0xFF

#ifdef BSP_USING_I2C1
struct i2c_bus_device i2c_bus1;
#endif
#ifdef BSP_USING_I2C2
struct i2c_bus_device i2c_bus2;
#endif

static int i2c_read(I2C_TypeDef *i2c_periph,
                         rt_uint16_t addr,
                         rt_uint8_t flags,
                         rt_uint16_t len,
                         rt_uint8_t *buf)
{
    rt_uint16_t try = 0;
    while (I2C_GetFlagStatus(i2c_periph, I2C_FLAG_BUSY))
        if (try++ >= TIMEOUT) return -1;

    I2C_GenerateSTART(i2c_periph, ENABLE);

    try = 0;
    while (!I2C_CheckEvent(i2c_periph, I2C_EVENT_MASTER_MODE_SELECT))                       //EVT5
        if (try++ >= TIMEOUT) return -1;

    if(flags & RT_I2C_ADDR_10BIT)   //10-bit address mode
    {
        rt_uint8_t frame_head = 0xF0 + (addr >> 8) << 1;                                    //11110XX0
        I2C_SendData(i2c_periph, frame_head);                                               //FrameHead

        try = 0;
        while (!I2C_CheckEvent(i2c_periph, I2C_EVENT_MASTER_MODE_ADDRESS10))                //EVT9
            if (try++ >= TIMEOUT) return -1;

        I2C_SendData(i2c_periph,0xff & addr);

        try = 0;
        while (!I2C_CheckEvent(i2c_periph, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))     //EVT6
            if (try++ >= TIMEOUT) return -1;

        I2C_GenerateSTART(i2c_periph, ENABLE);                                              //Sr

        try = 0;
        while (!I2C_CheckEvent(i2c_periph, I2C_EVENT_MASTER_MODE_SELECT))                   //EVT5
            if (try++ >= TIMEOUT) return -1;

        I2C_SendData(i2c_periph,frame_head);                                                //Resend FrameHead
    }
    else
    {
        I2C_Send7bitAddress(i2c_periph, addr << 1, I2C_Direction_Receiver);
    }

    try = 0;
    while (!I2C_CheckEvent(i2c_periph, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED))            //EVT6
        if (try++ >= TIMEOUT) return -1;

    while(len-- > 0)
    {

        try = 0;
        while (!I2C_GetFlagStatus(i2c_periph, I2C_FLAG_RXNE))                               //Got ACK For the Last Byte
            if (try++ >= TIMEOUT) return -1;

        *(buf++)=I2C_ReceiveData(i2c_periph);
    }

    I2C_GenerateSTOP(i2c_periph, ENABLE);
}

static int i2c_write(I2C_TypeDef *i2c_periph,
                            rt_uint16_t addr,
                            rt_uint8_t flags,
                            rt_uint8_t *buf,
                            rt_uint16_t len)
{
    rt_uint16_t try = 0;
    while (I2C_GetFlagStatus(i2c_periph, I2C_FLAG_BUSY))
        if (try++ >= TIMEOUT) return -1;

    I2C_GenerateSTART(i2c_periph, ENABLE);

    try = 0;
    while (!I2C_CheckEvent(i2c_periph, I2C_EVENT_MASTER_MODE_SELECT))                       //EVT5
        if (try++ >= TIMEOUT) return -1;

    if(flags & RT_I2C_ADDR_10BIT)   //10-bit address mode
    {
        I2C_SendData(i2c_periph,0xF0 + (addr >> 8) << 1);                                   //FrameHead 11110XX0

        try = 0;
        while (!I2C_CheckEvent(i2c_periph, I2C_EVENT_MASTER_MODE_ADDRESS10))                //EVT9
            if (try++ >= TIMEOUT) return -1;

        I2C_SendData(i2c_periph,0xff & addr);
    }
    else    //7-bit address mode
    {
        I2C_Send7bitAddress(i2c_periph, addr << 1, I2C_Direction_Transmitter);
    }

    try = 0;
    while (!I2C_CheckEvent(i2c_periph, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))         //EVT6
        if (try++ >= TIMEOUT) return -1;

    //Missing Evt8_1 (No definition)

    while(len-- > 0)
    {
        try = 0;
        while (!I2C_GetFlagStatus(i2c_periph, I2C_FLAG_TXE))                                //Got ACK For the Last Byte
            if (try++ >= TIMEOUT) return -1;

        I2C_SendData(i2c_periph,*(buf++));
    }

    try = 0;
    while(!I2C_CheckEvent(i2c_periph, I2C_EVENT_MASTER_BYTE_TRANSMITTING))                  //Last byte sent successfully
        if (try++ >= TIMEOUT) return -1;

    I2C_GenerateSTOP(i2c_periph, ENABLE);
}

rt_size_t i2c_master_xfer(struct rt_i2c_bus_device *bus,
                            struct rt_i2c_msg msgs[],
                                    rt_uint32_t num)
{
    struct rt_i2c_msg *msg;
    struct i2c_bus_device *i2c_bus_dev;
    rt_uint32_t index;

    i2c_bus_dev = (struct i2c_bus_device *)bus;

    for (index = 0; index < num; index++)
    {
        msg = &msgs[index];
        if (msg->flags & RT_I2C_RD)
        {
            i2c_read(i2c_bus_dev->periph,
                          msg->addr,
                          msg->flags,
                          msg->len,
                          msg->buf);
        }
        else
        {
            i2c_write(i2c_bus_dev->periph,
                           msg->addr,
                           msg->flags,
                           msg->buf,
                           msg->len);
        }
    }

    return index;
}

static const struct rt_i2c_bus_device_ops ch32_i2c_ops =
{
        .master_xfer = i2c_master_xfer,
        .slave_xfer = RT_NULL,                   //Not Used in i2c_core?
        .i2c_bus_control = RT_NULL
};

int rt_hw_i2c_init(struct i2c_config *config = &{5000, I2C_DutyCycle_2, 0, I2C_Ack_Disable, I2C_AcknowledgedAddress_7bit})
{
    int result = RT_EOK;

    GPIO_InitTypeDef GPIO_InitStructure = {0};
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;

    I2C_InitTypeDef I2C_InitTSturcture = {0};
    I2C_StructInit(&I2C_InitTSturcture);
    I2C_InitTSturcture.I2C_ClockSpeed = config->clock_speed;
    I2C_InitTSturcture.I2C_DutyCycle = config->duty_cycle;
    I2C_InitTSturcture.I2C_OwnAddress1 = config->own_address;
    I2C_InitTSturcture.I2C_Ack = config->enable_ack ? I2C_Ack_Enable : I2C_Ack_Disable;
    I2C_InitTSturcture.I2C_AcknowledgedAddress = config->is_7_bit_address ? I2C_AcknowledgedAddress_7bit : I2C_AcknowledgedAddress_10bit;


#ifdef BSP_USING_I2C1

    i2c_bus1.periph = I2C1;

    //Clock & IO Initialization
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
    GPIO_Init(GPIOB, &GPIO_InitStructure);

    GPIO_PinRemapConfig(GPIO_Remap_I2C1, ENABLE);

    //I2C Initialization Config
    I2C_Init(I2C1, &I2C_InitTSturcture);
    I2C_Cmd(I2C1, ENABLE);

    //Hook to RT-Thread
    i2c_bus1.parent.ops = &ch32_i2c_ops;
    result += rt_i2c_bus_device_register(&i2c_bus1.parent, "i2c1");

#endif

#ifdef BSP_USING_I2C2

    i2c_bus2.periph = I2C2;

    //Clock & IO Initialization
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
    GPIO_Init(GPIOB, &GPIO_InitStructure);

    //I2C Initialization Config
    I2C_Init(I2C2, &I2C_InitTSturcture);
    I2C_Cmd(I2C2, ENABLE);

    //Hook to RT-Thread
    i2c_bus2.parent.ops = &ch32_i2c_ops;
    result += rt_i2c_bus_device_register(&i2c_bus2.parent, "i2c2");

#endif

    return result;
}

#endif //BSP_USING_HWI2C