forked from espressif/esp-who
-
Notifications
You must be signed in to change notification settings - Fork 0
/
twi.c
executable file
·372 lines (323 loc) · 10.1 KB
/
twi.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*
si2c.c - Software I2C library for ESP31B
Copyright (c) 2015 Hristo Gochkov. All rights reserved.
This file is part of the ESP31B core for Arduino environment.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdint.h>
#include <stdbool.h>
#include "twi.h"
#include "soc/gpio_reg.h"
#include "soc/gpio_struct.h"
#include "soc/io_mux_reg.h"
#include "driver/rtc_io.h"
#include <stdio.h>
#define LOW 0x0
#define HIGH 0x1
//GPIO FUNCTIONS
#define INPUT 0x01
#define OUTPUT 0x02
#define PULLUP 0x04
#define INPUT_PULLUP 0x05
#define PULLDOWN 0x08
#define INPUT_PULLDOWN 0x09
#define OPEN_DRAIN 0x10
#define OUTPUT_OPEN_DRAIN 0x12
#define SPECIAL 0xF0
#define FUNCTION_1 0x00
#define FUNCTION_2 0x20
#define FUNCTION_3 0x40
#define FUNCTION_4 0x60
#define FUNCTION_5 0x80
#define FUNCTION_6 0xA0
#define ESP_REG(addr) *((volatile uint32_t *)(addr))
const DRAM_ATTR uint8_t pin_to_mux[GPIO_PIN_COUNT] = { 0x44, 0x88, 0x40, 0x84, 0x48, 0x6c, 0x60, 0x64, 0x68, 0x54, 0x58, 0x5c, 0x34, 0x38, 0x30, 0x3c, 0x4c, 0x50, 0x70, 0x74, 0x78, 0x7c, 0x80, 0x8c, 0, 0x24, 0x28, 0x2c, 0, 0, 0, 0, 0x1c, 0x20, 0x14, 0x18, 0x04, 0x08, 0x0c, 0x10};
static void pinMode(uint8_t pin, uint8_t mode)
{
if(pin >= 40) {
return;
}
uint32_t rtc_reg = rtc_gpio_desc[pin].reg;
//RTC pins PULL settings
if(rtc_reg) {
//lock rtc
ESP_REG(rtc_reg) = ESP_REG(rtc_reg) & ~(rtc_gpio_desc[pin].mux);
if(mode & PULLUP) {
ESP_REG(rtc_reg) = (ESP_REG(rtc_reg) | rtc_gpio_desc[pin].pullup) & ~(rtc_gpio_desc[pin].pulldown);
} else if(mode & PULLDOWN) {
ESP_REG(rtc_reg) = (ESP_REG(rtc_reg) | rtc_gpio_desc[pin].pulldown) & ~(rtc_gpio_desc[pin].pullup);
} else {
ESP_REG(rtc_reg) = ESP_REG(rtc_reg) & ~(rtc_gpio_desc[pin].pullup | rtc_gpio_desc[pin].pulldown);
}
//unlock rtc
}
uint32_t pinFunction = 0, pinControl = 0;
//lock gpio
if(mode & INPUT) {
if(pin < 32) {
GPIO.enable_w1tc = BIT(pin);
} else {
GPIO.enable1_w1tc.val = BIT(pin - 32);
}
} else if(mode & OUTPUT) {
if(pin > 33){
//unlock gpio
return;//pins above 33 can be only inputs
} else if(pin < 32) {
GPIO.enable_w1ts = BIT(pin);
} else {
GPIO.enable1_w1ts.val = BIT(pin - 32);
}
}
if(mode & PULLUP) {
pinFunction |= FUN_PU;
} else if(mode & PULLDOWN) {
pinFunction |= FUN_PD;
}
pinFunction |= ((uint32_t)2 << FUN_DRV_S);//what are the drivers?
pinFunction |= FUN_IE;//input enable but required for output as well?
if(mode & (INPUT | OUTPUT)) {
pinFunction |= ((uint32_t)2 << MCU_SEL_S);
} else if(mode == SPECIAL) {
pinFunction |= ((uint32_t)(((pin)==1||(pin)==3)?0:1) << MCU_SEL_S);
} else {
pinFunction |= ((uint32_t)(mode >> 5) << MCU_SEL_S);
}
ESP_REG(DR_REG_IO_MUX_BASE + pin_to_mux[pin]) = pinFunction;
if(mode & OPEN_DRAIN) {
pinControl = (1 << GPIO_PIN0_PAD_DRIVER_S);
}
GPIO.pin[pin].val = pinControl;
//unlock gpio
}
static void digitalWrite(uint8_t pin, uint8_t val)
{
if(val) {
if(pin < 32) {
GPIO.out_w1ts = BIT(pin);
} else if(pin < 34) {
GPIO.out1_w1ts.val = BIT(pin - 32);
}
} else {
if(pin < 32) {
GPIO.out_w1tc = BIT(pin);
} else if(pin < 34) {
GPIO.out1_w1tc.val = BIT(pin - 32);
}
}
}
unsigned char twi_dcount = 18;
static unsigned char twi_sda, twi_scl;
static inline void SDA_LOW() {
//Enable SDA (becomes output and since GPO is 0 for the pin,
// it will pull the line low)
if (twi_sda < 32) {
GPIO.enable_w1ts = BIT(twi_sda);
} else {
GPIO.enable1_w1ts.val = BIT(twi_sda - 32);
}
}
static inline void SDA_HIGH() {
//Disable SDA (becomes input and since it has pullup it will go high)
if (twi_sda < 32) {
GPIO.enable_w1tc = BIT(twi_sda);
} else {
GPIO.enable1_w1tc.val = BIT(twi_sda - 32);
}
}
static inline uint32_t SDA_READ() {
if (twi_sda < 32) {
return (GPIO.in & BIT(twi_sda)) != 0;
} else {
return (GPIO.in1.val & BIT(twi_sda - 32)) != 0;
}
}
static void SCL_LOW() {
if (twi_scl < 32) {
GPIO.enable_w1ts = BIT(twi_scl);
} else {
GPIO.enable1_w1ts.val = BIT(twi_scl - 32);
}
}
static void SCL_HIGH() {
if (twi_scl < 32) {
GPIO.enable_w1tc = BIT(twi_scl);
} else {
GPIO.enable1_w1tc.val = BIT(twi_scl - 32);
}
}
static uint32_t SCL_READ() {
if (twi_scl < 32) {
return (GPIO.in & BIT(twi_scl)) != 0;
}
else {
return (GPIO.in1.val & BIT(twi_scl - 32)) != 0;
}
}
#ifndef FCPU80
#define FCPU80 80000000L
#endif
#if F_CPU == FCPU80
#define TWI_CLOCK_STRETCH 800
#else
#define TWI_CLOCK_STRETCH 1600
#endif
void twi_setClock(unsigned int freq){
#if F_CPU == FCPU80
if(freq <= 100000) twi_dcount = 19;//about 100KHz
else if(freq <= 200000) twi_dcount = 8;//about 200KHz
else if(freq <= 300000) twi_dcount = 3;//about 300KHz
else if(freq <= 400000) twi_dcount = 1;//about 400KHz
else twi_dcount = 1;//about 400KHz
#else
if(freq <= 100000) twi_dcount = 32;//about 100KHz
else if(freq <= 200000) twi_dcount = 14;//about 200KHz
else if(freq <= 300000) twi_dcount = 8;//about 300KHz
else if(freq <= 400000) twi_dcount = 5;//about 400KHz
else if(freq <= 500000) twi_dcount = 3;//about 500KHz
else if(freq <= 600000) twi_dcount = 2;//about 600KHz
else twi_dcount = 1;//about 700KHz
#endif
}
void twi_init(unsigned char sda, unsigned char scl){
twi_sda = sda;
twi_scl = scl;
pinMode(twi_sda, OUTPUT);
pinMode(twi_scl, OUTPUT);
digitalWrite(twi_sda, 0);
digitalWrite(twi_scl, 0);
pinMode(twi_sda, INPUT_PULLUP);
pinMode(twi_scl, INPUT_PULLUP);
twi_setClock(100000);
}
void twi_stop(void){
pinMode(twi_sda, INPUT);
pinMode(twi_scl, INPUT);
}
static void twi_delay(unsigned char v){
unsigned int i;
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-but-set-variable"
unsigned int reg;
for(i=0;i<v;i++) reg = REG_READ(GPIO_IN_REG);
#pragma GCC diagnostic pop
}
static bool twi_write_start(void) {
SCL_HIGH();
SDA_HIGH();
if (SDA_READ() == 0) return false;
twi_delay(twi_dcount);
SDA_LOW();
twi_delay(twi_dcount);
return true;
}
static bool twi_write_stop(void){
unsigned int i = 0;
SCL_LOW();
SDA_LOW();
twi_delay(twi_dcount);
SCL_HIGH();
while (SCL_READ() == 0 && (i++) < TWI_CLOCK_STRETCH);// Clock stretching (up to 100us)
twi_delay(twi_dcount);
SDA_HIGH();
twi_delay(twi_dcount);
return true;
}
bool do_log = false;
static bool twi_write_bit(bool bit) {
unsigned int i = 0;
SCL_LOW();
if (bit) {SDA_HIGH(); if (do_log) {twi_delay(twi_dcount+1);}}
else {SDA_LOW(); if (do_log) {} }
twi_delay(twi_dcount+1);
SCL_HIGH();
while (SCL_READ() == 0 && (i++) < TWI_CLOCK_STRETCH);// Clock stretching (up to 100us)
twi_delay(twi_dcount);
return true;
}
static bool twi_read_bit(void) {
unsigned int i = 0;
SCL_LOW();
SDA_HIGH();
twi_delay(twi_dcount+2);
SCL_HIGH();
while (SCL_READ() == 0 && (i++) < TWI_CLOCK_STRETCH);// Clock stretching (up to 100us)
bool bit = SDA_READ();
twi_delay(twi_dcount);
return bit;
}
static bool twi_write_byte(unsigned char byte) {
if (byte == 0x43) {
// printf("TWB %02x ", (uint32_t) byte);
// do_log = true;
}
unsigned char bit;
for (bit = 0; bit < 8; bit++) {
twi_write_bit((byte & 0x80) != 0);
byte <<= 1;
}
if (do_log) {
printf("\n");
do_log = false;
}
return !twi_read_bit();//NACK/ACK
}
static unsigned char twi_read_byte(bool nack) {
unsigned char byte = 0;
unsigned char bit;
for (bit = 0; bit < 8; bit++) byte = (byte << 1) | twi_read_bit();
twi_write_bit(nack);
return byte;
}
unsigned char twi_writeTo(unsigned char address, unsigned char * buf, unsigned int len, unsigned char sendStop){
unsigned int i;
if(!twi_write_start()) return 4;//line busy
if(!twi_write_byte(((address << 1) | 0) & 0xFF)) {
if (sendStop) twi_write_stop();
return 2; //received NACK on transmit of address
}
for(i=0; i<len; i++) {
if(!twi_write_byte(buf[i])) {
if (sendStop) twi_write_stop();
return 3;//received NACK on transmit of data
}
}
if(sendStop) twi_write_stop();
i = 0;
while(SDA_READ() == 0 && (i++) < 10){
SCL_LOW();
twi_delay(twi_dcount);
SCL_HIGH();
twi_delay(twi_dcount);
}
return 0;
}
unsigned char twi_readFrom(unsigned char address, unsigned char* buf, unsigned int len, unsigned char sendStop){
unsigned int i;
if(!twi_write_start()) return 4;//line busy
if(!twi_write_byte(((address << 1) | 1) & 0xFF)) {
if (sendStop) twi_write_stop();
return 2;//received NACK on transmit of address
}
for(i=0; i<(len-1); i++) buf[i] = twi_read_byte(false);
buf[len-1] = twi_read_byte(true);
if(sendStop) twi_write_stop();
i = 0;
while(SDA_READ() == 0 && (i++) < 10){
SCL_LOW();
twi_delay(twi_dcount);
SCL_HIGH();
twi_delay(twi_dcount);
}
return 0;
}