23
23
#include " pins_arduino.h"
24
24
#include " wiring_private.h"
25
25
26
- extern " C" {
27
26
28
- unsigned int preferred_si2c_clock = 100000 ;
27
+
28
+ extern " C" {
29
29
#include " twi_util.h"
30
30
31
31
#include " ets_sys.h"
32
32
33
+
33
34
#pragma GCC diagnostic push
34
35
#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
35
36
37
+ // modes (private)
38
+ typedef enum { TWIPM_UNKNOWN = 0 , TWIPM_IDLE, TWIPM_ADDRESSED, TWIPM_WAIT} twipModeType;
39
+
40
+ // states (private)
41
+ typedef enum { TWIP_UNKNOWN = 0 , TWIP_IDLE, TWIP_START, TWIP_SEND_ACK, TWIP_WAIT_ACK, TWIP_WAIT_STOP, TWIP_SLA_W, TWIP_SLA_R, TWIP_REP_START, TWIP_READ, TWIP_STOP, TWIP_REC_ACK, TWIP_READ_ACK, TWIP_RWAIT_ACK, TWIP_WRITE, TWIP_BUS_ERR } twipStateType;
42
+ typedef enum { TWI_READY=0 , TWI_MRX, TWI_MTX, TWI_SRX, TWI_STX } twiStateType;
43
+
36
44
static struct twi {
45
+ unsigned int preferred_si2c_clock; // = 100000;
37
46
unsigned char twi_dcount;// = 18;
38
47
unsigned char twi_sda, twi_scl;
39
48
uint32_t twi_clockStretchLimit;
40
49
unsigned char twi_addr;// = 0;
41
50
42
- // modes (private)
43
- #define TWIPM_UNKNOWN 0
44
- #define TWIPM_IDLE 1
45
- #define TWIPM_ADDRESSED 2
46
- #define TWIPM_WAIT 3
47
51
48
- // states (private)
49
- #define TWIP_UNKNOWN 0
50
- #define TWIP_IDLE 1
51
- #define TWIP_START 2
52
- #define TWIP_SEND_ACK 3
53
- #define TWIP_WAIT_ACK 4
54
- #define TWIP_WAIT_STOP 5
55
- #define TWIP_SLA_W 6
56
- #define TWIP_SLA_R 7
57
- #define TWIP_REP_START 8
58
- #define TWIP_READ 9
59
- #define TWIP_STOP 10
60
- #define TWIP_REC_ACK 11
61
- #define TWIP_READ_ACK 12
62
- #define TWIP_RWAIT_ACK 13
63
- #define TWIP_WRITE 14
64
- #define TWIP_BUS_ERR 15
65
-
66
- volatile int twip_mode;// = TWIPM_IDLE;
67
- volatile int twip_state;// = TWIP_IDLE;
52
+ volatile twipModeType twip_mode;// = TWIPM_IDLE;
53
+ volatile twipStateType twip_state;// = TWIP_IDLE;
68
54
volatile int twip_status;// = TW_NO_INFO;
69
55
volatile int bitCount;// = 0;
70
56
@@ -74,12 +60,7 @@ static struct twi {
74
60
volatile int twi_ack_rec;// = 0;
75
61
volatile int twi_timeout_ms;// = 10;
76
62
77
- #define TWI_READY 0
78
- #define TWI_MRX 1
79
- #define TWI_MTX 2
80
- #define TWI_SRX 3
81
- #define TWI_STX 4
82
- volatile int twi_state;// = TWI_READY;
63
+ volatile twiStateType twi_state;// = TWI_READY;
83
64
volatile uint8_t twi_error;// = 0xFF;
84
65
85
66
uint8_t twi_txBuffer[TWI_BUFFER_LENGTH];
@@ -101,8 +82,8 @@ static struct twi {
101
82
102
83
ETSEvent eventTaskQueue[EVENTTASK_QUEUE_SIZE];
103
84
ETSTimer timer;
104
- } twi = { 18 , 0 , 0 , 0 , 0 , TWIPM_IDLE, TWIP_IDLE, TW_NO_INFO, 0 , 0 , 0 , 0 , 10 , TWI_READY, 0xff , {0 }, 0 , 0 , {0 }, 0 , NULL , NULL , {{0 }}, {0 ,0 ,0 ,0 }};
105
- // = { twi_dcount: 18, twi_addr: 0, twip_mode: TWIPM_IDLE, twip_state: TWIP_IDLE, twip_status: TW_NO_INFO, bitCount: 0, twi_data: 0x00, twi_ack: 0, twi_ack_rec: 0, twi_timeout_ms: 10, twi_state: TWI_READY, twi_error: 0xFF };
85
+ } twi = { 100000 , 18 , 0 , 0 , 0 , 0 , TWIPM_IDLE, TWIP_IDLE, TW_NO_INFO, 0 , 0 , 0 , 0 , 10 , TWI_READY, 0xff , {0 }, 0 , 0 , {0 }, 0 , NULL , NULL , {{0 }}, {0 ,0 ,0 ,0 }};
86
+ // = { preferred_si2c_clock: 100000, twi_dcount: 18, twi_addr: 0, twip_mode: TWIPM_IDLE, twip_state: TWIP_IDLE, twip_status: TW_NO_INFO, bitCount: 0, twi_data: 0x00, twi_ack: 0, twi_ack_rec: 0, twi_timeout_ms: 10, twi_state: TWI_READY, twi_error: 0xFF };
106
87
#pragma GCC diagnostic pop
107
88
108
89
static void onSclChange (void );
@@ -128,7 +109,7 @@ static void onTimer(void *unused);
128
109
#endif
129
110
130
111
void twi_setClock (unsigned int freq){
131
- preferred_si2c_clock = freq;
112
+ twi. preferred_si2c_clock = freq;
132
113
#if F_CPU == FCPU80
133
114
if (freq <= 50000 ) twi.twi_dcount = 38 ;// about 50KHz
134
115
else if (freq <= 100000 ) twi.twi_dcount = 19 ;// about 100KHz
@@ -164,7 +145,7 @@ void twi_init(unsigned char sda, unsigned char scl)
164
145
twi.twi_scl = scl;
165
146
pinMode (twi.twi_sda , INPUT_PULLUP);
166
147
pinMode (twi.twi_scl , INPUT_PULLUP);
167
- twi_setClock (preferred_si2c_clock);
148
+ twi_setClock (twi. preferred_si2c_clock );
168
149
twi_setClockStretchLimit (230 ); // default value is 230 uS
169
150
170
151
if (twi.twi_addr != 0 )
0 commit comments