0

How fast can you read from an MCP23008?
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

How fast can you read from an MCP23008?

by Keith_Henrickson on Sun Jun 11, 2017 11:41 pm

I'm running the following sample project on a Wiced:
Code: Select all | TOGGLE FULL SIZE
#include "Adafruit_MCP23008.h"

static Adafruit_MCP23008 keys;

void setup() {
  // put your setup code here, to run once:
  keys.begin();
}

void loop() {
  // put your main code here, to run repeatedly:
  Serial.print("A:");
  Serial.println(millis());
  uint8_t row_selector[4] = {0xE0, 0xD0, 0xB0, 0x70};

  int row = 0;
  keys.writeGPIO(row_selector[row]);
  keys.readGPIO();
  Serial.print("B:");
  Serial.println(millis());
}


And I get the following results:
Code: Select all | TOGGLE FULL SIZE
A:2113
B:2116

A:2116
B:2119

A:2119
B:2122


Does it really take 3ms for the Wiced to read and write a total of 4 bytes?

Doing a rough calculation, that would be 333 scans/sec. At 4 bytes each, that's 1332 bytes/second, or 10656 bits/sec, about 1/10 the theoretical limit of I2C....

Keith_Henrickson
 
Posts: 22
Joined: Sun May 23, 2010 12:40 am

Re: How fast can you read from an MCP23008?

by adafruit_support_carter on Mon Jun 12, 2017 2:04 pm

Some of that time could be in the serial prints. Try storing the millis() values in variables and printing after the fact.
Code: Select all | TOGGLE FULL SIZE
uint32_t start = millis()
// do stuff
uint32_t end = millis()
// serial print start/end as you wish

adafruit_support_carter
 
Posts: 2905
Joined: Tue Nov 29, 2016 2:45 pm

Re: How fast can you read from an MCP23008?

by Keith_Henrickson on Fri Jun 16, 2017 1:13 am

Thank you. I did remove everything except for a single read statment. I now have:
Code: Select all | TOGGLE FULL SIZE
#include "Adafruit_MCP23008.h"

static Adafruit_MCP23008 keys;

void setup() {
  // put your setup code here, to run once:
  keys.begin();
}

void loop() {
  keys.readGPIO();
}


However, I'm seeing a problem if I put it up on the scope (which yes, I should have done in the first place).

If you take a look at the first two screenshots, (3 and 4, in that order), it looks like a fairly normal write via I2C of address 0x20 (the chip address), it's address 0x09 (the base).

There is an extra transition on SDA that I'm not sure about. From the examples I've seen it should just go 1, and then SCL will follow. But it seems that a stop bit is being inserted.

TEK00003.PNG
TEK00003.PNG (7.29 KiB) Viewed 201 times

TEK00004.PNG
TEK00004.PNG (6.95 KiB) Viewed 201 times
Last edited by adafruit_support_carter on Mon Jun 19, 2017 10:42 am, edited 1 time in total.
Reason: inlining images to force order

Keith_Henrickson
 
Posts: 22
Joined: Sun May 23, 2010 12:40 am

Re: How fast can you read from an MCP23008?

by Keith_Henrickson on Fri Jun 16, 2017 1:22 am

Sorry, it took too many pictures to tell the story.

The bus is then idle for about 700ms (same line of code in my program still).

Then a whole new I2C transaction is started, of a READ from 0x20, which comes back with 0x00 and... a NACK? That looks to me in image 6 like the last bit is a 1. That's a NACK correct?

TEK00005.PNG
TEK00005.PNG (7.15 KiB) Viewed 199 times

TEK00006.PNG
inlining images to force order
TEK00006.PNG (6.86 KiB) Viewed 199 times

Keith_Henrickson
 
Posts: 22
Joined: Sun May 23, 2010 12:40 am

Re: How fast can you read from an MCP23008?

by adafruit_support_carter on Mon Jun 19, 2017 10:53 am

There is an extra transition on SDA that I'm not sure about. From the examples I've seen it should just go 1, and then SCL will follow. But it seems that a stop bit is being inserted.

The other way around. The stop condition for I2C is: SDA goes high after SCL

That's a NACK correct?

Maybe. The ACK/NACK is the 9th bit. A 1 indicates an issue. Do you get that with every transaction?

adafruit_support_carter
 
Posts: 2905
Joined: Tue Nov 29, 2016 2:45 pm

Re: How fast can you read from an MCP23008?

by Keith_Henrickson on Sat Jul 15, 2017 6:51 pm

Yep, that's every transaction. I'm not sure why the WICED is complaining.
It hands me back the data, it's just REAAALLY slow.

Keith_Henrickson
 
Posts: 22
Joined: Sun May 23, 2010 12:40 am

Re: How fast can you read from an MCP23008?

by Keith_Henrickson on Sat Jul 15, 2017 9:05 pm

I just realized I had a Feather M0 datalogger laying around. Some of the pins I use on the WICED are occupied on the M0, but it works for simple I2C.

I get a MUCH better output using the same code:

A:8913
B:8913
A:8913
B:8914
A:8914
B:8914
A:8914
B:8915
A:8915
B:8915

Usually reads are completed in the same millisecond, even with my very crude and wasteful performance monitoring code.

Something is definitely up with the WICED. I'm not sure how to tell if it's something in my setup code, or if the microcontroller is sensitive to something wrong with my board design.

I have another WICED I could put pins on, if this is possibly a hardware problem on the WICED.

Keith_Henrickson
 
Posts: 22
Joined: Sun May 23, 2010 12:40 am

Re: How fast can you read from an MCP23008?

by adafruit_support_carter on Sun Jul 16, 2017 11:10 am

Are your running the same code on both the WICED and the M0? (post if so)

adafruit_support_carter
 
Posts: 2905
Joined: Tue Nov 29, 2016 2:45 pm

Re: How fast can you read from an MCP23008?

by Keith_Henrickson on Sun Jul 16, 2017 6:54 pm

Just reran to be sure what I was seeing:
Code: Select all | TOGGLE FULL SIZE
#include "Adafruit_MCP23008.h"

static Adafruit_MCP23008 keys;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(250000);
  keys.begin();
}

void loop() {
  uint32_t start = millis();
  keys.readGPIO();
  uint32_t end = millis();

  Serial.print("A:");
  Serial.println(start);
  Serial.print("B:");
  Serial.println(end);
}


From the WICED:
Code: Select all | TOGGLE FULL SIZE
A:2111
B:2122
A:2123
B:2125
A:2125
B:2135
A:2136
B:2138
A:2138
B:2148


From the M0 datalogger:
Code: Select all | TOGGLE FULL SIZE
A:604
B:604
A:604
B:605
A:605
B:605
A:605
B:606
A:606
B:606
A:607
B:607
A:607
B:607


The WICED takes 10ms to do something that the datalogger takes < 1ms to do.

Keith_Henrickson
 
Posts: 22
Joined: Sun May 23, 2010 12:40 am

Re: How fast can you read from an MCP23008?

by adafruit_support_carter on Mon Jul 17, 2017 10:38 am

Weird. The timing isn't consistent on the WICED. Try running this slightly modified version of the code on both and see what happens:

Code: Select all | TOGGLE FULL SIZE
#include "Adafruit_MCP23008.h"

#define RATE 100

static Adafruit_MCP23008 keys;
uint32_t start_time, end_time, delta_time;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(250000);
  keys.begin();
}

void loop() {
  start_time = millis();
  keys.readGPIO();
  end_time = millis();

  delta_time = end_time - start_time;
  Serial.println(delta_time);

  delay(RATE);
}

adafruit_support_carter
 
Posts: 2905
Joined: Tue Nov 29, 2016 2:45 pm

Re: How fast can you read from an MCP23008?

by Keith_Henrickson on Tue Jul 25, 2017 12:57 am

Interesting....

With the M0 Datalogger, all 0s.

With the Wiced, I get:
Code: Select all | TOGGLE FULL SIZE
2
2
2
3
2
2
2
2
2


Slightly more consistent than my code, but still much slower on the wiced than the datalogger.

Keith_Henrickson
 
Posts: 22
Joined: Sun May 23, 2010 12:40 am

Re: How fast can you read from an MCP23008?

by adafruit_support_carter on Sun Jul 30, 2017 12:15 pm

So basically you're curious why a 120MHz M3 reads slower than a 48MHz M0? Other than the timing difference, does it work as expected?

adafruit_support_carter
 
Posts: 2905
Joined: Tue Nov 29, 2016 2:45 pm

Re: How fast can you read from an MCP23008?

by Keith_Henrickson on Sun Jul 30, 2017 12:41 pm

The reason this is a problem is I am trying to scan a 4x4 keypad. It does work, in the sense that I get the right data back.

However, with every operation taking 2 ms, the rate of key scanning is miserable. Because a key scan isn't a single read of the i2c.

It actually makes my Chip 8 emulator run slowly, which says something given that it has a 2ms instruction time.

Keith_Henrickson
 
Posts: 22
Joined: Sun May 23, 2010 12:40 am

Re: How fast can you read from an MCP23008?

by adafruit_support_carter on Sun Jul 30, 2017 1:25 pm

Might be related to I2C speed. Use your scope and see what the I2C transaction looks like for the two different boards. That is, a scope trace for the M0 (Adalogger) and a scope trace for the M3 (Wiced). See if there is a difference in their frequency.

adafruit_support_carter
 
Posts: 2905
Joined: Tue Nov 29, 2016 2:45 pm

Please be positive and constructive with your questions and comments.