-
Notifications
You must be signed in to change notification settings - Fork 0
/
i2c.c
238 lines (197 loc) · 5.6 KB
/
i2c.c
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
/*
* i2c.c
*
* Created on: 2012. 8. 7.
* Author: East
*/
#include "i2c.h"
#include <stdlib.h>
#include "CMSIS/stm32f10x_gpio.h"
#include "CMSIS/stm32f10x_rcc.h"
#include "CMSIS/stm32f10x_i2c.h"
#include "CMSIS/misc.h"
#include "system_timer.h"
#define I2C_TIMEOUT_US 100
typedef enum I2cState
{
I2C_ERROR_STATE, I2C_COMPLETE_STATE, I2C_ON_GOING_STATE
} I2cState;
typedef struct I2cDataContainer
{
uint16_t address;
uint8_t* data;
uint32_t size;
uint8_t direction;
volatile uint32_t index;
volatile I2cState state;
} I2cDataContainer;
static I2cDataContainer i2c2DataContainer =
{ 0, NULL, 0, I2C_Direction_Transmitter, 0, I2C_COMPLETE_STATE };
void I2C2_EV_IRQHandler()
{
switch (I2C_GetLastEvent(I2C2))
{
case I2C_EVENT_MASTER_MODE_SELECT:
I2C_Send7bitAddress(I2C2, (uint8_t) (i2c2DataContainer.address << 1),
i2c2DataContainer.direction);
break;
case I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED:
i2c2DataContainer.index = 0;
I2C_SendData(I2C2, i2c2DataContainer.data[i2c2DataContainer.index]);
break;
case I2C_EVENT_MASTER_BYTE_TRANSMITTING:
break;
case I2C_EVENT_MASTER_BYTE_TRANSMITTED:
i2c2DataContainer.index++;
if (i2c2DataContainer.index < i2c2DataContainer.size)
{
I2C_SendData(I2C2, i2c2DataContainer.data[i2c2DataContainer.index]);
}
else if (i2c2DataContainer.index == i2c2DataContainer.size)
{
I2C_GenerateSTOP(I2C2, ENABLE);
i2c2DataContainer.state = I2C_COMPLETE_STATE;
}
break;
///////////////////////////////////////////////////////////
case I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED:
i2c2DataContainer.data[i2c2DataContainer.index++] = I2C_ReceiveData(
I2C2);
break;
case I2C_EVENT_MASTER_BYTE_RECEIVED:
if (i2c2DataContainer.index < i2c2DataContainer.size)
{
if (i2c2DataContainer.index + 1 == i2c2DataContainer.size)
{
I2C_NACKPositionConfig(I2C2, I2C_NACKPosition_Current);
}
i2c2DataContainer.data[i2c2DataContainer.index++] = I2C_ReceiveData(
I2C2);
}
else
{
I2C_GenerateSTOP(I2C2, ENABLE);
i2c2DataContainer.state = I2C_COMPLETE_STATE;
}
break;
}
}
void I2C2_ER_IRQHandler()
{
if (I2C_GetITStatus(I2C2, I2C_IT_BERR))
{
i2c2DataContainer.state = I2C_ERROR_STATE;
I2C_ClearITPendingBit(I2C2, I2C_IT_BERR);
}
if (I2C_GetITStatus(I2C2, I2C_IT_AF))
{
i2c2DataContainer.state = I2C_ERROR_STATE;
I2C_GenerateSTOP(I2C2, ENABLE);
I2C_ClearITPendingBit(I2C2, I2C_IT_AF);
}
if (I2C_GetITStatus(I2C2, I2C_IT_ARLO))
{
i2c2DataContainer.state = I2C_ERROR_STATE;
I2C_ClearITPendingBit(I2C2, I2C_IT_ARLO);
}
if (I2C_GetITStatus(I2C2, I2C_IT_OVR))
{
i2c2DataContainer.state = I2C_ERROR_STATE;
I2C_ClearITPendingBit(I2C2, I2C_IT_OVR);
}
}
BOOL I2c2WriteRequest(uint16_t address, uint8_t* data, uint32_t size)
{
uint32_t systemTickCount = GetSystemTickCount();
while (I2C_GetFlagStatus(I2C2, I2C_FLAG_BUSY)
|| i2c2DataContainer.state == I2C_ON_GOING_STATE)
{
if (TimeInterval(systemTickCount) > I2C_TIMEOUT_US)
{
i2c2DataContainer.state = I2C_ERROR_STATE;
return FALSE;
}
}
i2c2DataContainer.state = I2C_ON_GOING_STATE;
i2c2DataContainer.address = address;
i2c2DataContainer.data = data;
i2c2DataContainer.size = size;
i2c2DataContainer.index = 0;
i2c2DataContainer.direction = I2C_Direction_Transmitter;
I2C_GenerateSTART(I2C2, ENABLE);
return TRUE;
}
BOOL I2c2ReadRequest(uint16_t address, uint8_t* data, uint32_t availableSize)
{
uint32_t systemTickCount = GetSystemTickCount();
while (I2C_GetFlagStatus(I2C2, I2C_FLAG_BUSY)
|| i2c2DataContainer.state == I2C_ON_GOING_STATE)
{
if (TimeInterval(systemTickCount) > I2C_TIMEOUT_US)
{
i2c2DataContainer.state = I2C_ERROR_STATE;
return FALSE;
}
}
i2c2DataContainer.state = I2C_ON_GOING_STATE;
i2c2DataContainer.address = address;
i2c2DataContainer.data = data;
i2c2DataContainer.size = availableSize;
i2c2DataContainer.index = 0;
i2c2DataContainer.direction = I2C_Direction_Receiver;
I2C_GenerateSTART(I2C2, ENABLE);
return TRUE;
}
BOOL IsI2c2OnGoing()
{
return i2c2DataContainer.state == I2C_ON_GOING_STATE;
}
BOOL IsI2c2Completed()
{
return i2c2DataContainer.state == I2C_COMPLETE_STATE;
}
BOOL IsI2c2Error()
{
return i2c2DataContainer.state == I2C_ERROR_STATE;
}
void I2cRccConfig()
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
}
void I2cNvicConfig()
{
NVIC_InitTypeDef nvicI2cInit;
nvicI2cInit.NVIC_IRQChannel = I2C2_EV_IRQn;
nvicI2cInit.NVIC_IRQChannelPreemptionPriority = 2;
nvicI2cInit.NVIC_IRQChannelSubPriority = 0;
nvicI2cInit.NVIC_IRQChannelCmd = ENABLE;
I2C_ITConfig(I2C2, I2C_IT_EVT, DISABLE);
NVIC_Init(&nvicI2cInit);
nvicI2cInit.NVIC_IRQChannel = I2C2_ER_IRQn;
nvicI2cInit.NVIC_IRQChannelPreemptionPriority = 2;
nvicI2cInit.NVIC_IRQChannelSubPriority = 0;
nvicI2cInit.NVIC_IRQChannelCmd = ENABLE;
I2C_ITConfig(I2C2, I2C_IT_ERR, DISABLE);
NVIC_Init(&nvicI2cInit);
}
void I2cInit(uint32_t clockSpeed)
{
GPIO_InitTypeDef gpioI2cInit;
/* Configure I2C2 pins: SCL and SDA ----------------------------------------*/
gpioI2cInit.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
gpioI2cInit.GPIO_Speed = GPIO_Speed_50MHz;
gpioI2cInit.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_Init(GPIOB, &gpioI2cInit);
I2C_InitTypeDef i2cInit;
/* I2C2 configuration: SMBus Host ------------------------------------------*/
i2cInit.I2C_Mode = I2C_Mode_I2C;
i2cInit.I2C_DutyCycle = I2C_DutyCycle_2;
i2cInit.I2C_OwnAddress1 = 0;
i2cInit.I2C_Ack = I2C_Ack_Enable;
i2cInit.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
i2cInit.I2C_ClockSpeed = clockSpeed;
I2C_Init(I2C2, &i2cInit);
I2C_ITConfig(I2C2, I2C_IT_EVT | I2C_IT_ERR, ENABLE);
I2C_Cmd(I2C2, ENABLE);
}