i2c Memory access
Glad it's working.
One small complication I ran into was a false triggering at the beginning of the ACK bit. I'm not sure if that's what you saw, but it was a bit tricky. Sometimes there would be a small delay from when the master releases the SDA line to when Teensy 3.0 begins the ACK bit. The I2C interrupt occurs before that time, so the first rising edge is sometimes that brief moment where SDA can rise enough to trigger the rising edge interrupt. Fortunately, I was viewing the waveforms on a good oscilloscope the while working, so it was easy to see (when it happens... not every time).
I have tried the new lib and it compiles ok
, not tried onRequest yet, but I have a missing peice of information. The T2 code uses pins 5 & 6 for SCL and SDA what pins are used on the T3? The test code below works but obviously needs these pins reasigned to get the i2c bus connected to the mem chips.
#include <LiquidCrystal.h>
#include <Wire.h>
// RS E D4 D5 D6 D7
LiquidCrystal lcd(2, 3, 4, 7, 8, 9);
const byte byte_size=256;
long chip_mem=0; //256000/8; // bytes in memory chip 256 kbit /8 =32k
const byte word_size=2; // stored value max 65534 at each address
byte mem_chips=3; // number of chips on th c2i bus max 8
const byte chip_base_address=80; // chip manufactuer c2i address
unsigned long total_address=0;
byte next_chip=0;
void setup()
{
Wire.begin(); // join i2c bus
//Wire.onRequest(receiveEvent); // register event not used yet
//Wire.onReceive(receiveEvent);
lcd.begin(20,4);
analogWrite(10,80);
pinMode(11,OUTPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
chip_mem=256000/8;
total_address=(chip_mem/word_size)*mem_chips;
lcd.setCursor(0,0);
lcd.print(total_address);
lcd.print(" Word Data");
delay(5000);
lcd.setCursor(0,0);
lcd.print(total_address*2);
lcd.print(" Bytes");
delay(5000);
}
int timer=0;
unsigned int x=0;
unsigned int c=0;
void loop()
{
unsigned long p=0;
lcd.clear();
timer=millis();
for (long m=0; m<total_address; m++)
{
switch (putmem(m,p))
{
case 0:
lcd.setCursor(0,0);
lcd.print(m);
lcd.print("<No Write ");
lcd.setCursor(0,1);
lcd.print(p);
lcd.print("<Same ");
break;
case 1:
lcd.setCursor(0,0);
lcd.print(m);
lcd.print("<Write ");
lcd.setCursor(0,1);
lcd.print(p);
lcd.print("<Value ");
break;
case 2:
lcd.setCursor(0,0);
lcd.print(m);
lcd.print(" Addrs error ");
break;
case 3:
lcd.setCursor(0,0);
lcd.print(m);
lcd.print(" Memory error ");
break;
}
p=p+2;
if(p>65000) p=0;
}
lcd.print((millis()-timer)/1000);
delay(4000);
lcd.clear();
for (long m=0; m<total_address; m++) //for (int m=0; m<=total_address; m++)
{
c=getmem(m);
lcd.setCursor(0,2);
lcd.print(m);
//lcd.print(" <Read Addrs ");
lcd.setCursor(0,3);
lcd.print(c);
}
delay(5000);
x++;
}
byte putmem(unsigned long address, unsigned int value)
{
int ret_flag=0;
int time_out=3000;
int tocount=0;
const unsigned int_max=65536;
unsigned long address1=address;
next_chip=int(address/(chip_mem/word_size));
if (getmem(address)!=value)
{
address=address-(next_chip*(chip_mem/word_size));
address=address*word_size;
if (address % word_size==0) // must be a logical address position
{
lcd.setCursor(19,0);
lcd.print(next_chip);
byte hib=int(address/256);
byte lob=(address-int(hib*256));
byte bca=chip_base_address+next_chip;
Wire.beginTransmission(bca);
Wire.send(hib); // address high byte
Wire.send(lob); // address low byte
hib=(value/256);
Wire.send(hib); // Data High (MSByte)
Wire.send(int(value-(hib*256))); // Data Low (LSByte)
Wire.endTransmission();
ret_flag=1;
}
else ret_flag=2;
if(value>0 && value<int_max)
while(getmem(address1)!=value){if(tocount++>time_out) {ret_flag=3;break;}}
// else delay(8);
}
return ret_flag;
}
unsigned int getmem(unsigned long address)
{
next_chip=int(address/(chip_mem/word_size));
address=address-(next_chip*(chip_mem/word_size));
address=(address*word_size);
unsigned int value[]={0,0};
unsigned int ret_val=0;
byte x=0;
byte lob=(address-int((address/byte_size)*256));
byte hib=int(address/256);
byte bca=chip_base_address+next_chip;
Wire.beginTransmission(bca);
Wire.send(hib); // address high byte
Wire.send(lob); // address low byte
Wire.endTransmission();
Wire.requestFrom(bca,word_size);
while(Wire.available()) // collect full value
value[x++] = Wire.read(); // receive byte
ret_val=int(value[0]*256) + value[1];
return ret_val;
}