Skip to content

Commit 69113fb

Browse files
author
Matěj Sychra
committed
cleaning up commit as requested by esp8266#5162 (review)
1 parent 870e2eb commit 69113fb

File tree

1 file changed

+24
-129
lines changed

1 file changed

+24
-129
lines changed

cores/esp8266/core_esp8266_si2c.c

+24-129
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ static uint8_t twi_rxBuffer[TWI_BUFFER_LENGTH];
8484
static volatile uint8_t twi_rxBufferIndex;
8585

8686
static void (*twi_onSlaveTransmit)(void);
87-
static void (*twi_onSlaveReceive)(uint8_t*, int);
87+
static void (*twi_onSlaveReceive)(uint8_t*, size_t);
8888

8989
static void onSclChange(void);
9090
static void onSdaChange(void);
@@ -156,11 +156,6 @@ void twi_init(unsigned char sda, unsigned char scl)
156156
pinMode(twi_sda, INPUT_PULLUP);
157157
pinMode(twi_scl, INPUT_PULLUP);
158158
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);
164159
twi_setClockStretchLimit(230); // default value is 230 uS
165160

166161
if (twi_addr != 0)
@@ -176,26 +171,23 @@ void twi_setAddress(uint8_t address)
176171
twi_addr = address << 1;
177172
}
178173

179-
#if 0
180-
void twi_stop(void){
181-
pinMode(twi_sda, INPUT);
182-
pinMode(twi_scl, INPUT);
183-
}
184-
#endif
185-
186174
static void ICACHE_RAM_ATTR twi_delay(unsigned char v){
187175
unsigned int i;
188176
#pragma GCC diagnostic push
189177
#pragma GCC diagnostic ignored "-Wunused-but-set-variable"
190178
unsigned int reg;
191-
for(i=0;i<v;i++) reg = GPI;
179+
for (i = 0; i < v; i++) {
180+
reg = GPI;
181+
}
192182
#pragma GCC diagnostic pop
193183
}
194184

195185
static bool twi_write_start(void) {
196186
SCL_HIGH();
197187
SDA_HIGH();
198-
if (SDA_READ() == 0) return false;
188+
if (SDA_READ() == 0) {
189+
return false;
190+
}
199191
twi_delay(twi_dcount);
200192
SDA_LOW();
201193
twi_delay(twi_dcount);
@@ -212,7 +204,6 @@ static bool twi_write_stop(void){
212204
twi_delay(twi_dcount);
213205
SDA_HIGH();
214206
twi_delay(twi_dcount);
215-
216207
return true;
217208
}
218209

@@ -301,19 +292,25 @@ unsigned char twi_readFrom(unsigned char address, unsigned char* buf, unsigned i
301292
return 0;
302293
}
303294

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+
}
306299
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
309301
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;
311313
}
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
317314
}
318315

319316
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)
339336
return 0;
340337
}
341338

342-
void twi_attachSlaveRxEvent( void (*function)(uint8_t*, int) )
339+
void twi_attachSlaveRxEvent( void (*function)(uint8_t*, size_t) )
343340
{
344341
twi_onSlaveReceive = function;
345342
}
@@ -363,7 +360,6 @@ void ICACHE_RAM_ATTR twi_reply(uint8_t ack)
363360
}
364361
}
365362

366-
#if 1
367363
void ICACHE_RAM_ATTR twi_stop(void)
368364
{
369365
// send stop condition
@@ -372,17 +368,9 @@ void ICACHE_RAM_ATTR twi_stop(void)
372368
twi_ack = 1; // _BV(TWEA)
373369
twi_delay(5); // Maybe this should be here
374370
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-
382371
// update twi state
383372
twi_state = TWI_READY;
384373
}
385-
#endif
386374

387375
void ICACHE_RAM_ATTR twi_releaseBus(void)
388376
{
@@ -399,84 +387,7 @@ void ICACHE_RAM_ATTR twi_releaseBus(void)
399387

400388
void ICACHE_RAM_ATTR twi_onTwipEvent(uint8_t status)
401389
{
402-
//digitalWrite(13, HIGH);
403-
//switch(TW_STATUS){
404390
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-
480391
// Slave Receiver
481392
case TW_SR_SLA_ACK: // addressed, returned ack
482393
case TW_SR_GCALL_ACK: // addressed generally, returned ack
@@ -564,23 +475,19 @@ void ICACHE_RAM_ATTR twi_onTwipEvent(uint8_t status)
564475
twi_stop();
565476
break;
566477
}
567-
//digitalWrite(13, LOW);
568478
}
569479

570480
void ICACHE_RAM_ATTR onTimer()
571481
{
572-
//digitalWrite(13, HIGH);
573482
twi_releaseBus();
574483
twip_status = TW_BUS_ERROR;
575484
twi_onTwipEvent(twip_status);
576485
twip_mode = TWIPM_WAIT;
577486
twip_state = TWIP_BUS_ERR;
578-
//digitalWrite(13, LOW);
579487
}
580488

581489
static void eventTask(ETSEvent *e)
582490
{
583-
//digitalWrite(14, HIGH);
584491

585492
if (e == NULL) {
586493
return;
@@ -608,24 +515,16 @@ static void eventTask(ETSEvent *e)
608515
twi_onSlaveReceive(twi_rxBuffer, e->par);
609516
break;
610517
}
611-
612-
//digitalWrite(14, LOW);
613518
}
614519

615520
void ICACHE_RAM_ATTR onSclChange(void)
616521
{
617522
static uint8_t sda;
618523
static uint8_t scl;
619524

620-
//digitalWrite(2, LOW);
621-
//digitalWrite(2, HIGH);
622-
623525
sda = SDA_READ();
624526
scl = SCL_READ();
625527

626-
//digitalWrite(12, scl);
627-
//digitalWrite(14, LOW);
628-
629528
twip_status = 0xF8; // reset TWI status
630529

631530
switch (twip_state)
@@ -783,8 +682,6 @@ void ICACHE_RAM_ATTR onSdaChange(void)
783682
sda = SDA_READ();
784683
scl = SCL_READ();
785684

786-
//digitalWrite(2, sda);
787-
788685
switch (twip_state)
789686
{
790687
case TWIP_IDLE:
@@ -796,9 +693,7 @@ void ICACHE_RAM_ATTR onSdaChange(void)
796693
// START
797694
bitCount = 8;
798695
twip_state = TWIP_START;
799-
//digitalWrite(14, HIGH);
800696
ets_timer_arm_new(&timer, twi_timeout_ms, false, true); // Once, ms
801-
//digitalWrite(14, LOW);
802697
}
803698
break;
804699

0 commit comments

Comments
 (0)