@@ -34,9 +34,74 @@ static unsigned char twi_sda, twi_scl;
34
34
static uint32_t twi_clockStretchLimit ;
35
35
static unsigned char twi_addr = 0 ;
36
36
37
+ // modes (private)
38
+ #define TWIPM_UNKNOWN 0
39
+ #define TWIPM_IDLE 1
40
+ #define TWIPM_ADDRESSED 2
41
+ #define TWIPM_WAIT 3
42
+
43
+ // states (private)
44
+ #define TWIP_UNKNOWN 0
45
+ #define TWIP_IDLE 1
46
+ #define TWIP_START 2
47
+ #define TWIP_SEND_ACK 3
48
+ #define TWIP_WAIT_ACK 4
49
+ #define TWIP_WAIT_STOP 5
50
+ #define TWIP_SLA_W 6
51
+ #define TWIP_SLA_R 7
52
+ #define TWIP_REP_START 8
53
+ #define TWIP_READ 9
54
+ #define TWIP_STOP 10
55
+ #define TWIP_REC_ACK 11
56
+ #define TWIP_READ_ACK 12
57
+ #define TWIP_RWAIT_ACK 13
58
+ #define TWIP_WRITE 14
59
+ #define TWIP_BUS_ERR 15
60
+
61
+ static volatile uint8_t twip_mode = TWIPM_IDLE ;
62
+ static volatile uint8_t twip_state = TWIP_IDLE ;
63
+ static volatile uint8_t twip_status = TW_NO_INFO ;
64
+ static volatile uint8_t bitCount = 0 ;
65
+
66
+ #define TWDR twi_data
67
+ static volatile uint8_t twi_data = 0x00 ;
68
+ static volatile uint8_t twi_ack = 0 ;
69
+ static volatile uint8_t twi_ack_rec = 0 ;
70
+ static volatile int twi_timeout_ms = 10 ;
71
+
72
+ #define TWI_READY 0
73
+ #define TWI_MRX 1
74
+ #define TWI_MTX 2
75
+ #define TWI_SRX 3
76
+ #define TWI_STX 4
77
+ static volatile uint8_t twi_state = TWI_READY ;
78
+ static volatile uint8_t twi_error = 0xFF ;
79
+
80
+ static uint8_t twi_txBuffer [TWI_BUFFER_LENGTH ];
81
+ static volatile uint8_t twi_txBufferIndex ;
82
+ static volatile uint8_t twi_txBufferLength ;
83
+
84
+ static uint8_t twi_rxBuffer [TWI_BUFFER_LENGTH ];
85
+ static volatile uint8_t twi_rxBufferIndex ;
86
+
87
+ static void (* twi_onSlaveTransmit )(void );
88
+ static void (* twi_onSlaveReceive )(uint8_t * , int );
89
+
37
90
void onSclChange (void );
38
91
void onSdaChange (void );
39
92
93
+ #define TASK_QUEUE_SIZE 1
94
+ #define TASK_QUEUE_PRIO 2
95
+
96
+ #define TWI_SIG_RANGE 0x00000100
97
+ #define TWI_SIG_RX (TWI_SIG_RANGE + 0x01)
98
+ #define TWI_SIG_TX (TWI_SIG_RANGE + 0x02)
99
+
100
+ static ETSEvent task_queue [TASK_QUEUE_SIZE ];
101
+ static void task (ETSEvent * e );
102
+ static ETSTimer timer ;
103
+ void onTimer (void * timer_arg );
104
+
40
105
#define SDA_LOW () (GPES = (1 << twi_sda)) //Enable SDA (becomes output and since GPO is 0 for the pin, it will pull the line low)
41
106
#define SDA_HIGH () (GPEC = (1 << twi_sda)) //Disable SDA (becomes input and since it has pullup it will go high)
42
107
#define SDA_READ () ((GPI & (1 << twi_sda)) != 0)
@@ -76,19 +141,6 @@ void twi_setClockStretchLimit(uint32_t limit){
76
141
twi_clockStretchLimit = limit * TWI_CLOCK_STRETCH_MULTIPLIER ;
77
142
}
78
143
79
- #define TASK_QUEUE_SIZE 1
80
- #define TASK_QUEUE_PRIO 2
81
-
82
- #define TWI_SIG_RANGE 0x00000100
83
- #define TWI_SIG_RX (TWI_SIG_RANGE + 0x01)
84
- #define TWI_SIG_TX (TWI_SIG_RANGE + 0x02)
85
-
86
-
87
- static ETSEvent task_queue [TASK_QUEUE_SIZE ];
88
- static void task (ETSEvent * e );
89
- static ETSTimer timer ;
90
- void onTimer (void * timer_arg );
91
-
92
144
void twi_init (unsigned char sda , unsigned char scl ){
93
145
94
146
@@ -264,60 +316,6 @@ uint8_t twi_status(){
264
316
else return I2C_OK ; //all ok
265
317
}
266
318
267
-
268
-
269
- #define TWIPM_UNKNOWN 0
270
- #define TWIPM_IDLE 1
271
- #define TWIPM_ADDRESSED 2
272
- #define TWIPM_WAIT 3
273
-
274
- #define TWIP_UNKNOWN 0
275
- #define TWIP_IDLE 1
276
- #define TWIP_START 2
277
- #define TWIP_SEND_ACK 3
278
- #define TWIP_WAIT_ACK 4
279
- #define TWIP_WAIT_STOP 5
280
- #define TWIP_SLA_W 6
281
- #define TWIP_SLA_R 7
282
- #define TWIP_REP_START 8
283
- #define TWIP_READ 9
284
- #define TWIP_STOP 10
285
- #define TWIP_REC_ACK 11
286
- #define TWIP_READ_ACK 12
287
- #define TWIP_RWAIT_ACK 13
288
- #define TWIP_WRITE 14
289
- #define TWIP_BUS_ERR 99
290
-
291
- static volatile uint8_t twip_mode = TWIPM_IDLE ;
292
- static volatile uint8_t twip_state = TWIP_IDLE ;
293
- static volatile uint8_t twip_status = TW_NO_INFO ;
294
- static volatile uint8_t bitCount = 0 ;
295
-
296
- #define TWDR twi_data
297
- static volatile uint8_t twi_data = 0x00 ;
298
- static volatile uint8_t twi_ack = 0 ;
299
- static volatile uint8_t twi_ack_rec = 0 ;
300
- static volatile int twi_timeout_ms = 10 ;
301
-
302
- #define TWI_READY 0
303
- #define TWI_MRX 1
304
- #define TWI_MTX 2
305
- #define TWI_SRX 3
306
- #define TWI_STX 4
307
- static volatile uint8_t twi_state = TWI_READY ;
308
- static volatile uint8_t twi_error = 0xFF ;
309
-
310
- static uint8_t twi_txBuffer [TWI_BUFFER_LENGTH ];
311
- static volatile uint8_t twi_txBufferIndex ;
312
- static volatile uint8_t twi_txBufferLength ;
313
-
314
- static uint8_t twi_rxBuffer [TWI_BUFFER_LENGTH ];
315
- static volatile uint8_t twi_rxBufferIndex ;
316
-
317
-
318
- static void (* twi_onSlaveTransmit )(void );
319
- static void (* twi_onSlaveReceive )(uint8_t * , int );
320
-
321
319
uint8_t twi_transmit (const uint8_t * data , uint8_t length )
322
320
{
323
321
uint8_t i ;
0 commit comments