-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpio_iic.c
189 lines (162 loc) · 2.92 KB
/
gpio_iic.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
/*
* gpio_iic.c
*
* Created on: 2024年5月30日
* Author: swiftchen
CTP_BufferRead(READ_BUFF, 0xD000, 2, 7);
CTP_BufferWrite(READ_BUFF, 0xD000, 2, 1);
*/
#include "gpio_iic.h"
void GPIOI2C_delay(uint32_t val)
{
for (int32_t i = 0; i < val * 60; i++) // val*30 350kHZ
{
//__NOP;
__asm("nop");
}
}
void GPIOI2C_Init(void) //配置为GPIO口,输出上拉
{
GPIO_SCL_OUTPUT;
GPIO_SCL_PULLUP;
GPIO_SDA_OUTPUT;
GPIO_SDA_PULLUP;
}
uint8_t GPIOI2C_Start(void)
{
GPIO_SCL_HIGH;
GPIO_SDA_HIGH;
GPIOI2C_delay(1);
GPIO_SDA_LOW;
GPIOI2C_delay(1);
GPIO_SCL_LOW;
GPIOI2C_delay(1);
return 0;
}
uint8_t GPIOI2C_Stop(void)
{
GPIO_SDA_LOW;
GPIOI2C_delay(1);
GPIO_SCL_HIGH;
GPIOI2C_delay(1);
GPIO_SDA_HIGH;
GPIOI2C_delay(2);
return 0;
}
uint8_t GPIOI2C_write_byte(const uint8_t s)
{
uint8_t ch_data, i = 0;
// GPIO_SCL_LOW;
ch_data = s;
for (i = 0; i < 8; i++)
{
if (ch_data & 0x80)
GPIO_SDA_HIGH;
else
GPIO_SDA_LOW;
GPIOI2C_delay(1);
GPIO_SCL_HIGH;
GPIOI2C_delay(1);
GPIO_SCL_LOW;
GPIOI2C_delay(1);
ch_data <<= 1;
}
// ACK
GPIO_SDA_INPUT;
GPIO_SDA_HIGH;
GPIOI2C_delay(1);
GPIO_SCL_HIGH;
GPIOI2C_delay(1);
for (uint8_t i = 0; i < 200; i++)
{
if (!GPIO_GetSDABit)
{
GPIO_SCL_LOW;
GPIO_SDA_OUTPUT;
GPIOI2C_delay(1);
// printf("ack 0k \r\n");
return 0; // ACK
}
}
GPIO_SCL_LOW;
GPIO_SDA_OUTPUT;
GPIOI2C_Stop(); // ERROR
GPIOI2C_delay(1);
// printf("ack error \r\n");
return 1; // NACK
}
uint8_t GPIOI2C_read_byte(uint8_t ack)
{
uint8_t ch_data = 0x00, i = 0;
GPIO_SDA_INPUT;
GPIO_SDA_HIGH;
GPIOI2C_delay(1);
for (i = 0; i < 8; i++)
{
GPIO_SCL_LOW;
GPIOI2C_delay(1);
GPIO_SCL_HIGH;
GPIOI2C_delay(1);
ch_data <<= 1;
if (GPIO_GetSDABit)
ch_data |= 0x01;
}
// ACK
GPIO_SDA_OUTPUT;
GPIO_SCL_LOW;
GPIOI2C_delay(1);
if (ack)
GPIO_SDA_HIGH;
else
GPIO_SDA_LOW;
GPIOI2C_delay(1);
GPIO_SCL_HIGH;
GPIOI2C_delay(1);
GPIO_SCL_LOW;
GPIOI2C_delay(1);
return ch_data;
}
uint8_t GPIO_BufferWrite(uint8_t *pBuffer, uint32_t WriteAddr, uint8_t AddrByte, uint16_t NumByte)
{
uint32_t addr;
GPIOI2C_Start();
GPIOI2C_write_byte((GPIO_IIC_ADDR) | 0);
for (; AddrByte > 0; AddrByte--)
{
addr = WriteAddr;
for (uint8_t i = 1; i < AddrByte; i++)
addr >>= 8;
GPIOI2C_write_byte(addr & 0xFF);
}
while (NumByte--)
{
GPIOI2C_write_byte(*pBuffer);
pBuffer++;
}
GPIOI2C_Stop();
return 0;
}
uint8_t GPIO_BufferRead(uint8_t *pBuffer, uint32_t ReadAddr, uint8_t AddrByte, uint16_t NumByte)
{
uint32_t addr;
GPIOI2C_Start();
GPIOI2C_write_byte(GPIO_IIC_ADDR | 0);
for (; AddrByte > 0; AddrByte--)
{
addr = ReadAddr;
for (uint8_t i = 1; i < AddrByte; i++)
addr >>= 8;
GPIOI2C_write_byte(addr & 0xFF);
}
GPIOI2C_Stop();
GPIOI2C_Start();
GPIOI2C_write_byte(GPIO_IIC_ADDR | 0x01);
for (; NumByte > 1; NumByte--)
{
*pBuffer = GPIOI2C_read_byte(0);
pBuffer++;
}
*pBuffer = GPIOI2C_read_byte(1);
GPIOI2C_Stop();
return 0;
}