This repository has been archived by the owner on Dec 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathi2c.c
195 lines (142 loc) · 4.34 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
#include "i2c.h"
#include "usart.h"
#define I2C_SPEED 100000
#define I2C_TIMEOUT 100000
void I2C_Setup(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
I2C_InitTypeDef I2C_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
/* I2C1 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
/* I2C1 SDA and SCL configuration */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_Init(GPIOB, &GPIO_InitStructure);
//SCL is pin06 and SDA is pin 07 for I2C1
/*enable I2C*/
I2C_Cmd(I2C1,ENABLE);
I2C_DeInit(I2C1);
I2C_StructInit(&I2C_InitStructure);
/* I2C1 configuration */
// I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
// I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
// I2C_InitStructure.I2C_OwnAddress1 = 0xA0;
// I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
// I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
// I2C_InitStructure.I2C_ClockSpeed = I2C_SPEED;
I2C_Init(I2C1, &I2C_InitStructure);
//I2C_AcknowledgeConfig(I2C1, ENABLE);
/*!< Enable SMBus Alert interrupt */
//I2C_ITConfig(I2C1, I2C_IT_ERR, ENABLE); //dont think this is necessary
}
uint8_t I2C_ReadShort(uint16_t reg_address, uint16_t dev_address)
{
uint16_t data = 0;
uint32_t timeout = I2C_TIMEOUT;
dev_address = dev_address << 1;
I2C_AcknowledgeConfig(I2C1, ENABLE);
while (I2C_GetFlagStatus(I2C1,I2C_FLAG_BUSY)) {
if (! timeout--)
Usart1Put('q');
}
I2C_GenerateSTART(I2C1, ENABLE);
timeout = I2C_TIMEOUT;
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)){
if (! timeout--)
Usart1Put('w');
}
I2C_Send7bitAddress(I2C1, dev_address, I2C_Direction_Transmitter);
timeout = I2C_TIMEOUT;
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)){
if (! timeout--)
Usart1Put('e');
}
I2C_SendData(I2C1, reg_address); // write register address
timeout = I2C_TIMEOUT;
while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED)){
if (! timeout--)
Usart1Put('r');
}
/* restart */
I2C_GenerateSTART(I2C1, ENABLE);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)){
if (! timeout--)
Usart1Put('t');
}
I2C_Send7bitAddress(I2C1, dev_address, I2C_Direction_Receiver);
timeout = I2C_TIMEOUT;
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)){
if (! timeout--)
Usart1Put('y');
}
/* before last one, disable ACK */
I2C_NACKPositionConfig(I2C1, I2C_NACKPosition_Next);
I2C_AcknowledgeConfig(I2C1, DISABLE);
/* Receive */
I2C_ReceiveData(I2C1);
timeout = I2C_TIMEOUT;
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED)){
if (! timeout--)
Usart1Put('u');
}
/* Receive */
data = I2C_ReceiveData(I2C1);
timeout = I2C_TIMEOUT;
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED)){
if (! timeout--)
Usart1Put('i');
}
I2C_GenerateSTOP(I2C1, ENABLE);
timeout = I2C_TIMEOUT;
while(I2C_GetFlagStatus(I2C1, I2C_FLAG_STOPF)){
if (! timeout--)
Usart1Put('o');
}
return data;
}
uint8_t I2C_WriteShort(uint16_t reg_address, uint16_t dev_address, uint8_t data)
{
uint32_t timeout = I2C_TIMEOUT;
dev_address = dev_address << 1;
I2C_AcknowledgeConfig(I2C1, ENABLE);
while (I2C_GetFlagStatus(I2C1,I2C_FLAG_BUSY)) {
if (! timeout--)
Usart1Put('q');
}
I2C_GenerateSTART(I2C1, ENABLE);
timeout = I2C_TIMEOUT;
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)){
if (! timeout--)
Usart1Put('w');
}
I2C_Send7bitAddress(I2C1, dev_address, I2C_Direction_Transmitter);
timeout = I2C_TIMEOUT;
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)){
if (! timeout--)
Usart1Put('e');
}
I2C_SendData(I2C1, reg_address); // write register address
timeout = I2C_TIMEOUT;
while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED)){
if (! timeout--)
Usart1Put('r');
}
/* send */
I2C_SendData(I2C1, data);
timeout = I2C_TIMEOUT;
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED)){
if (! timeout--)
Usart1Put('t');
}
I2C_GenerateSTOP(I2C1, ENABLE);
timeout = I2C_TIMEOUT;
while(I2C_GetFlagStatus(I2C1, I2C_FLAG_STOPF)){
if (! timeout--)
Usart1Put('y');
}
return 0;
}