@@ -84,7 +84,7 @@ static uint8_t twi_rxBuffer[TWI_BUFFER_LENGTH];
84
84
static volatile uint8_t twi_rxBufferIndex ;
85
85
86
86
static void (* twi_onSlaveTransmit )(void );
87
- static void (* twi_onSlaveReceive )(uint8_t * , int );
87
+ static void (* twi_onSlaveReceive )(uint8_t * , size_t );
88
88
89
89
static void onSclChange (void );
90
90
static void onSdaChange (void );
@@ -156,11 +156,6 @@ void twi_init(unsigned char sda, unsigned char scl)
156
156
pinMode (twi_sda , INPUT_PULLUP );
157
157
pinMode (twi_scl , INPUT_PULLUP );
158
158
twi_setClock (preferred_si2c_clock );
159
- //pinMode(2, OUTPUT);
160
- //pinMode(12, OUTPUT);
161
- //pinMode(14, OUTPUT);
162
- //pinMode(13, OUTPUT);
163
- //digitalWrite(2, HIGH);
164
159
twi_setClockStretchLimit (230 ); // default value is 230 uS
165
160
166
161
if (twi_addr != 0 )
@@ -176,26 +171,23 @@ void twi_setAddress(uint8_t address)
176
171
twi_addr = address << 1 ;
177
172
}
178
173
179
- #if 0
180
- void twi_stop (void ){
181
- pinMode (twi_sda , INPUT );
182
- pinMode (twi_scl , INPUT );
183
- }
184
- #endif
185
-
186
174
static void ICACHE_RAM_ATTR twi_delay (unsigned char v ){
187
175
unsigned int i ;
188
176
#pragma GCC diagnostic push
189
177
#pragma GCC diagnostic ignored "-Wunused-but-set-variable"
190
178
unsigned int reg ;
191
- for (i = 0 ;i < v ;i ++ ) reg = GPI ;
179
+ for (i = 0 ; i < v ; i ++ ) {
180
+ reg = GPI ;
181
+ }
192
182
#pragma GCC diagnostic pop
193
183
}
194
184
195
185
static bool twi_write_start (void ) {
196
186
SCL_HIGH ();
197
187
SDA_HIGH ();
198
- if (SDA_READ () == 0 ) return false;
188
+ if (SDA_READ () == 0 ) {
189
+ return false;
190
+ }
199
191
twi_delay (twi_dcount );
200
192
SDA_LOW ();
201
193
twi_delay (twi_dcount );
@@ -212,7 +204,6 @@ static bool twi_write_stop(void){
212
204
twi_delay (twi_dcount );
213
205
SDA_HIGH ();
214
206
twi_delay (twi_dcount );
215
-
216
207
return true;
217
208
}
218
209
@@ -301,19 +292,25 @@ unsigned char twi_readFrom(unsigned char address, unsigned char* buf, unsigned i
301
292
return 0 ;
302
293
}
303
294
304
- uint8_t twi_status (){
305
- if (SCL_READ ()== 0 ) return I2C_SCL_HELD_LOW ; //SCL held low by another device, no procedure available to recover
295
+ uint8_t twi_status () {
296
+ if (SCL_READ ()== 0 ) {
297
+ return I2C_SCL_HELD_LOW ; // SCL held low by another device, no procedure available to recover
298
+ }
306
299
int clockCount = 20 ;
307
-
308
- while (SDA_READ ()== 0 && clockCount > 0 ){ //if SDA low, read the bits slaves have to sent to a max
300
+ while (SDA_READ ()== 0 && clockCount > 0 ) { // if SDA low, read the bits slaves have to sent to a max
309
301
twi_read_bit ();
310
- if (SCL_READ ()== 0 ) return I2C_SCL_HELD_LOW_AFTER_READ ; //I2C bus error. SCL held low beyond slave clock stretch time
302
+ if (SCL_READ ()== 0 ) {
303
+ return I2C_SCL_HELD_LOW_AFTER_READ ; // I2C bus error. SCL held low beyond slave clock stretch time
304
+ }
305
+ }
306
+ if (SDA_READ ()== 0 ) {
307
+ return I2C_SDA_HELD_LOW ; // I2C bus error. SDA line held low by slave/another_master after n bits.
308
+ }
309
+ if (!twi_write_start ()) {
310
+ return I2C_SDA_HELD_LOW_AFTER_INIT ; // line busy. SDA again held low by another device. 2nd master?
311
+ } else {
312
+ return I2C_OK ;
311
313
}
312
-
313
- if (SDA_READ ()== 0 ) return I2C_SDA_HELD_LOW ; //I2C bus error. SDA line held low by slave/another_master after n bits.
314
-
315
- if (!twi_write_start ()) return I2C_SDA_HELD_LOW_AFTER_INIT ; //line busy. SDA again held low by another device. 2nd master?
316
- else return I2C_OK ; //all ok
317
314
}
318
315
319
316
uint8_t twi_transmit (const uint8_t * data , uint8_t length )
@@ -339,7 +336,7 @@ uint8_t twi_transmit(const uint8_t* data, uint8_t length)
339
336
return 0 ;
340
337
}
341
338
342
- void twi_attachSlaveRxEvent ( void (* function )(uint8_t * , int ) )
339
+ void twi_attachSlaveRxEvent ( void (* function )(uint8_t * , size_t ) )
343
340
{
344
341
twi_onSlaveReceive = function ;
345
342
}
@@ -363,7 +360,6 @@ void ICACHE_RAM_ATTR twi_reply(uint8_t ack)
363
360
}
364
361
}
365
362
366
- #if 1
367
363
void ICACHE_RAM_ATTR twi_stop (void )
368
364
{
369
365
// send stop condition
@@ -372,17 +368,9 @@ void ICACHE_RAM_ATTR twi_stop(void)
372
368
twi_ack = 1 ; // _BV(TWEA)
373
369
twi_delay (5 ); // Maybe this should be here
374
370
SDA_HIGH (); // _BV(TWSTO)
375
-
376
- // wait for stop condition to be exectued on bus
377
- // TWINT is not set after a stop condition!
378
- while (false) { //TWCR & _BV(TWSTO)){
379
- continue ;
380
- }
381
-
382
371
// update twi state
383
372
twi_state = TWI_READY ;
384
373
}
385
- #endif
386
374
387
375
void ICACHE_RAM_ATTR twi_releaseBus (void )
388
376
{
@@ -399,84 +387,7 @@ void ICACHE_RAM_ATTR twi_releaseBus(void)
399
387
400
388
void ICACHE_RAM_ATTR twi_onTwipEvent (uint8_t status )
401
389
{
402
- //digitalWrite(13, HIGH);
403
- //switch(TW_STATUS){
404
390
switch (status ) {
405
- #if 0
406
- // All Master
407
- case TW_START : // sent start condition
408
- case TW_REP_START : // sent repeated start condition
409
- // copy device address and r/w bit to output register and ack
410
- TWDR = twi_slarw ;
411
- twi_reply (1 );
412
- break ;
413
-
414
- // Master Transmitter
415
- case TW_MT_SLA_ACK : // slave receiver acked address
416
- case TW_MT_DATA_ACK : // slave receiver acked data
417
- // if there is data to send, send it, otherwise stop
418
- if (twi_masterBufferIndex < twi_masterBufferLength ){
419
- // copy data to output register and ack
420
- TWDR = twi_masterBuffer [twi_masterBufferIndex ++ ];
421
- twi_reply (1 );
422
- }else {
423
- if (twi_sendStop )
424
- twi_stop ();
425
- else {
426
- twi_inRepStart = true; // we're gonna send the START
427
- // don't enable the interrupt. We'll generate the start, but we
428
- // avoid handling the interrupt until we're in the next transaction,
429
- // at the point where we would normally issue the start.
430
- TWCR = _BV (TWINT ) | _BV (TWSTA )| _BV (TWEN ) ;
431
- twi_state = TWI_READY ;
432
- }
433
- }
434
- break ;
435
- case TW_MT_SLA_NACK : // address sent, nack received
436
- twi_error = TW_MT_SLA_NACK ;
437
- twi_stop ();
438
- break ;
439
- case TW_MT_DATA_NACK : // data sent, nack received
440
- twi_error = TW_MT_DATA_NACK ;
441
- twi_stop ();
442
- break ;
443
- case TW_MT_ARB_LOST : // lost bus arbitration
444
- twi_error = TW_MT_ARB_LOST ;
445
- twi_releaseBus ();
446
- break ;
447
-
448
- // Master Receiver
449
- case TW_MR_DATA_ACK : // data received, ack sent
450
- // put byte into buffer
451
- twi_masterBuffer [twi_masterBufferIndex ++ ] = TWDR ;
452
- case TW_MR_SLA_ACK : // address sent, ack received
453
- // ack if more bytes are expected, otherwise nack
454
- if (twi_masterBufferIndex < twi_masterBufferLength ){
455
- twi_reply (1 );
456
- }else {
457
- twi_reply (0 );
458
- }
459
- break ;
460
- case TW_MR_DATA_NACK : // data received, nack sent
461
- // put final byte into buffer
462
- twi_masterBuffer [twi_masterBufferIndex ++ ] = TWDR ;
463
- if (twi_sendStop )
464
- twi_stop ();
465
- else {
466
- twi_inRepStart = true; // we're gonna send the START
467
- // don't enable the interrupt. We'll generate the start, but we
468
- // avoid handling the interrupt until we're in the next transaction,
469
- // at the point where we would normally issue the start.
470
- TWCR = _BV (TWINT ) | _BV (TWSTA )| _BV (TWEN ) ;
471
- twi_state = TWI_READY ;
472
- }
473
- break ;
474
- case TW_MR_SLA_NACK : // address sent, nack received
475
- twi_stop ();
476
- break ;
477
- // TW_MR_ARB_LOST handled by TW_MT_ARB_LOST case
478
- #endif
479
-
480
391
// Slave Receiver
481
392
case TW_SR_SLA_ACK : // addressed, returned ack
482
393
case TW_SR_GCALL_ACK : // addressed generally, returned ack
@@ -564,23 +475,19 @@ void ICACHE_RAM_ATTR twi_onTwipEvent(uint8_t status)
564
475
twi_stop ();
565
476
break ;
566
477
}
567
- //digitalWrite(13, LOW);
568
478
}
569
479
570
480
void ICACHE_RAM_ATTR onTimer ()
571
481
{
572
- //digitalWrite(13, HIGH);
573
482
twi_releaseBus ();
574
483
twip_status = TW_BUS_ERROR ;
575
484
twi_onTwipEvent (twip_status );
576
485
twip_mode = TWIPM_WAIT ;
577
486
twip_state = TWIP_BUS_ERR ;
578
- //digitalWrite(13, LOW);
579
487
}
580
488
581
489
static void eventTask (ETSEvent * e )
582
490
{
583
- //digitalWrite(14, HIGH);
584
491
585
492
if (e == NULL ) {
586
493
return ;
@@ -608,24 +515,16 @@ static void eventTask(ETSEvent *e)
608
515
twi_onSlaveReceive (twi_rxBuffer , e -> par );
609
516
break ;
610
517
}
611
-
612
- //digitalWrite(14, LOW);
613
518
}
614
519
615
520
void ICACHE_RAM_ATTR onSclChange (void )
616
521
{
617
522
static uint8_t sda ;
618
523
static uint8_t scl ;
619
524
620
- //digitalWrite(2, LOW);
621
- //digitalWrite(2, HIGH);
622
-
623
525
sda = SDA_READ ();
624
526
scl = SCL_READ ();
625
527
626
- //digitalWrite(12, scl);
627
- //digitalWrite(14, LOW);
628
-
629
528
twip_status = 0xF8 ; // reset TWI status
630
529
631
530
switch (twip_state )
@@ -783,8 +682,6 @@ void ICACHE_RAM_ATTR onSdaChange(void)
783
682
sda = SDA_READ ();
784
683
scl = SCL_READ ();
785
684
786
- //digitalWrite(2, sda);
787
-
788
685
switch (twip_state )
789
686
{
790
687
case TWIP_IDLE :
@@ -796,9 +693,7 @@ void ICACHE_RAM_ATTR onSdaChange(void)
796
693
// START
797
694
bitCount = 8 ;
798
695
twip_state = TWIP_START ;
799
- //digitalWrite(14, HIGH);
800
696
ets_timer_arm_new (& timer , twi_timeout_ms , false, true); // Once, ms
801
- //digitalWrite(14, LOW);
802
697
}
803
698
break ;
804
699
0 commit comments