i2c.c
4.51 KB
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
#include "HW_MCUIO.h"//#include "stm32f4xx_hal.h"
#include "platform.h"
#include "function.h"
#define I2C_PORT GPIOB
#define I2C_SCK GPIO_PIN_8// GPIO_PIN_6
#define I2C_SDA GPIO_PIN_9// GPIO_PIN_7
/*
This i2c driver from stm32F1xx; we should add delay when operation io,
because the stm32F4 much fast (The main frequency of stm32F4xx is 168M) !!
F4系列速度太快,需要加延时,读写才正常!!!*/
static void i2c_delay(unsigned short dy)
{
unsigned short i;
for(i=0;i<dy;i++);
}
#define I2C_IO_DY 5//1//
#define SDA_1 {HW_GPIO_SetPin(I2C_PORT, I2C_SDA);i2c_delay(I2C_IO_DY);}
#define SDA_0 {HW_GPIO_ResetPin(I2C_PORT, I2C_SDA);i2c_delay(I2C_IO_DY);}
#define SCK_1 {HW_GPIO_SetPin(I2C_PORT, I2C_SCK);i2c_delay(I2C_IO_DY);}
#define SCK_0 {HW_GPIO_ResetPin(I2C_PORT, I2C_SCK);i2c_delay(I2C_IO_DY);}
#define READ_SDA HW_GPIO_ReadPin(I2C_PORT, I2C_SDA)
void i2c_init( void )
{
HW_GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = I2C_SDA |I2C_SCK;
GPIO_InitStruct.Mode = HW_GPIO_MODE_OUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = HW_GPIO_SPEED_HIGH;
HW_GPIO_Init(I2C_PORT, &GPIO_InitStruct);
}
void SET_SDA_IN(void){
HW_GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = I2C_SDA;
GPIO_InitStruct.Mode = HW_GPIO_MODE_IPU;//GPIO_MODE_OUTPUT_PP
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = HW_GPIO_SPEED_HIGH;
HW_GPIO_Init(I2C_PORT, &GPIO_InitStruct);
}
void SET_SDA_OUT(void){
HW_GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = I2C_SDA;
GPIO_InitStruct.Mode = HW_GPIO_MODE_OUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;//GPIO_NOPULL;
GPIO_InitStruct.Speed = HW_GPIO_SPEED_HIGH;
HW_GPIO_Init(I2C_PORT, &GPIO_InitStruct);
}
void i2c_start(void)
{
SDA_1
SCK_1
SDA_0
SCK_0
}
void i2c_stop(void)
{
SDA_0
SCK_1
SDA_1
SCK_0
}
unsigned char i2c_send_byte(unsigned char byt)
{
unsigned char i;
SET_SDA_OUT();
for(i=0;i<8;i++){
if((byt<<i)&0x80) SDA_1
else SDA_0
SCK_1
SCK_0
}//for
//2,
SET_SDA_IN();
SCK_1
for(i=0;i<100;i++){
if(READ_SDA==0){
SCK_0
SET_SDA_OUT();
return 1;//ok
}
}//for
SCK_0
SET_SDA_OUT();
return 0; //fail
}
unsigned char i2c_read_byte(void)
{
unsigned char i;
unsigned char byt=0;
SET_SDA_IN();
for(i=0;i<8;i++){
byt <<=1;
SCK_1
if(READ_SDA) byt |=1;
SCK_0;
}//for
SET_SDA_OUT();
SDA_0//must
SCK_1
SCK_0
return byt;
}
#define EE_PAGE_SIZE 32
//---------------------------------- E2prom application --------------------------------------------
void EE_WriteBytes(unsigned int addr,const unsigned char *pdata,unsigned char len)
{
#if 0
unsigned char i;
i2c_start();
i2c_send_byte(0xA0);
i2c_send_byte(addr>>8);
i2c_send_byte(addr);
for(i=0;i<len;i++){
i2c_send_byte(pdata[i]);
}
i2c_stop();
//return 1;
basic_delay_ms(10);// delay_Nms(10);//HAL_Delay(10);//多次写入时必须延时!!
#else
unsigned char i;
unsigned char cur_len;
unsigned char offset=0;
// unsigned char remain_len;
// unsigned int cur_addr;
do{
//cur_addr = addr+offset;
//remain_len = len-offset;
//当前page可写入的字节个数;
if( (cur_len = EE_PAGE_SIZE - ( (addr+offset)%EE_PAGE_SIZE) ) >= (len-offset) ){
cur_len = len-offset;
}
else{
cur_len = cur_len;
}
i2c_start();
i2c_send_byte(0xA0);
i2c_send_byte((addr+offset)>>8);
i2c_send_byte(addr+offset);
for(i=0;i<cur_len;i++){
i2c_send_byte(pdata[offset+i]);
}
i2c_stop();
//return 1;
basic_delay_ms(10);// delay_Nms(10);//HAL_Delay(10);//多次写入时必须延时!!
offset +=cur_len;
if(offset >=len) break;
}while(1);
#endif
}
/*
*/
unsigned char EE_ReadBytes(unsigned int addr,unsigned char *pdata,unsigned char len)
{
unsigned char i;
i2c_start();
i2c_send_byte(0xA0);//write
i2c_send_byte(addr>>8);
i2c_send_byte(addr);
i2c_start();
i2c_send_byte(0xA1);//read
for(i=0;i<len;i++)
pdata[i]=i2c_read_byte();
i2c_stop();
return 1;
}
void i2c_test_basic(void)
{
unsigned char tmp[2]= {0xaa,0};
EE_WriteBytes(0xff,tmp,1);
EE_ReadBytes(0xff,tmp+1,1);
if( tmp[1] !=tmp[0]){
tmp[1] =tmp[1];
}
else{
tmp[1] =tmp[1];
}
}