Closed adbensi closed 6 years ago
TWI buffer is limitted to 32 bytes. same on AVR.
I managed to solve the problem by breaking the reads / writes:
#include <utility/twi.h>
void _readBytes(FRAM_MB85RC_I2C & fram, uint16_t addr, uint8_t length, uint8_t * buffer) {
while (length != 0) {
uint8_t bytes_to_read = min(length, TWI_BUFFER_LENGTH);
fram.readArray(addr, bytes_to_read, buffer);
addr += bytes_to_read;
buffer += bytes_to_read;
length -= bytes_to_read;
}
}
void _writeBytes(FRAM_MB85RC_I2C & fram, uint16_t addr, uint8_t length, uint8_t *buffer) {
while (length != 0) {
uint8_t bytes_to_write = min(length, TWI_BUFFER_LENGTH);
fram.writeArray(addr, bytes_to_write, buffer);
addr += bytes_to_write;
buffer += bytes_to_write;
length -= bytes_to_write;
}
}
Thanks a lot! I test 1Mbit memory and 256Kbit memory, 1Mbit memory works when I change the Product ID / 0x758 = 1M and 0x510 = 256K/ if ((prodID != 0x758)&&(prodID != 0x510)) { Serial.print("Unexpected Product ID: 0x"); Serial.println(prodID, HEX); return false; } May be it was usefull to you, Thanks a lot again, Best Regards! Alexandre
Hey, I am also having these issues, and due to my lack of experience with C I'm having some issues getting Palatis' script that iterates through chunks of it working...
Any tips would be greatly appreciated:)
I got a FM24CL16 (connected to a ESP8266), from Aliexpress, to work using Palati's approach with a fixed chunk size of 16 bytes. "TWI_BUFFER_LENGTH" seems to be not declared on ESP8266. Using a size of 32 bytes results in one lost byte.
Here is my modified "store_anything"example:
/**************************************************************************/
/*!
@file FRAM_I2C_store_anything.ino
@author SOSAndroid (E. Ha.)
@license BSD (see license.txt)
Example sketch that stores and retreive any kind of data.
@section HISTORY
v1.0 - First release
v1.0.1 - fix constructor call error
*/
/**************************************************************************/
#include <Wire.h>
#include <math.h>
#include <FRAM_MB85RC_I2C.h>
//define a struct of various data types
typedef struct MYDATA_t {
bool data_0;
float data_1;
long data_2;
int data_3;
byte data_4;
char data_5[64];
};
//define a struct joining MYDATA_t to an array of bytes to be stored
typedef union MYDATA4I2C_t {
MYDATA_t datastruct;
uint8_t I2CPacket[sizeof(MYDATA_t)];
};
MYDATA4I2C_t mydata; //data to be written in memory
MYDATA4I2C_t readdata; //data read from memory
//random address to write from
uint16_t writeaddress = 0x01;
//Creating object for FRAM chip
FRAM_MB85RC_I2C mymemory(MB85RC_ADDRESS_A000, false,D3,16);
byte _readBytes(FRAM_MB85RC_I2C & fram, uint16_t addr, uint8_t length, uint8_t * buffer) {
byte result=0;
while (length != 0) {
uint8_t bytes_to_read = min(length, 16);
result = fram.readArray(addr, bytes_to_read, buffer);
if(result>0) return result;
addr += bytes_to_read;
buffer += bytes_to_read;
length -= bytes_to_read;
}
return result;
}
byte _writeBytes(FRAM_MB85RC_I2C & fram, uint16_t addr, uint8_t length, uint8_t *buffer) {
byte result=0;
while (length != 0) {
uint8_t bytes_to_write = min(length, 16);
result = fram.writeArray(addr, bytes_to_write, buffer);
if(result>0) return result;
addr += bytes_to_write;
buffer += bytes_to_write;
length -= bytes_to_write;
}
return result;
}
void setup() {
Serial.begin(115200);
while (!Serial) ; //wait until Serial ready
Wire.begin();
byte arraySize = sizeof(MYDATA_t);
Serial.println("Starting...");
mymemory.begin();
//---------init data - load array
mydata.datastruct.data_0 = true;
Serial.print("Data_0: ");
if (mydata.datastruct.data_0) Serial.println("true");
if (!mydata.datastruct.data_0) Serial.println("false");
mydata.datastruct.data_1 = 1.3575;
Serial.print("Data_1: ");
Serial.println(mydata.datastruct.data_1, DEC);
mydata.datastruct.data_2 = 314159L;
Serial.print("Data_2: ");
Serial.println(mydata.datastruct.data_2, DEC);
mydata.datastruct.data_3 = 142;
Serial.print("Data_3: ");
Serial.println(mydata.datastruct.data_3, DEC);
mydata.datastruct.data_4 = 0x50;
Serial.print("Data_4: 0x");
Serial.println(mydata.datastruct.data_4, HEX);
String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
str.toCharArray(mydata.datastruct.data_5, 64);
Serial.print("Data_5: ");
Serial.println(mydata.datastruct.data_5);
Serial.println("...... ...... ......");
Serial.println("Init Done - array loaded");
Serial.println("...... ...... ......");
//----------write to FRAM chip
byte result = _writeBytes(mymemory,writeaddress, arraySize, mydata.I2CPacket);
Serial.print("Array Size=");
Serial.println(arraySize);
if (result == 0) Serial.println("Write Done - array loaded in FRAM chip");
if (result != 0) Serial.println("Write failed");
Serial.println("...... ...... ......");
//---------read data from memory chip
result = _readBytes(mymemory,writeaddress, arraySize, readdata.I2CPacket);
if (result == 0) Serial.println("Read Done - array loaded with read data");
if (result != 0) Serial.println("Read failed");
Serial.println("...... ...... ......");
//---------Send data to serial
Serial.print("Data_0: ");
if (readdata.datastruct.data_0) Serial.println("true");
if (!readdata.datastruct.data_0) Serial.println("false");
Serial.print("Data_1: ");
Serial.println(readdata.datastruct.data_1, DEC);
Serial.print("Data_2: ");
Serial.println(readdata.datastruct.data_2, DEC);
Serial.print("Data_3: ");
Serial.println(readdata.datastruct.data_3, DEC);
Serial.print("Data_4: 0x");
Serial.println(readdata.datastruct.data_4, HEX);
Serial.print("Data_5: ");
Serial.println(readdata.datastruct.data_5);
Serial.println("...... ...... ......");
Serial.println("Read Write test done - check data if successfull");
Serial.println("...... ...... ......");
}
void loop() {
// nothing to do
}
"TWI_BUFFER_LENGTH" seems to be not declared on ESP8266.
https://github.com/esp8266/Arduino/blob/master/libraries/Wire/Wire.h#L32
Hello! Nice job, congradulations! I have one issue on the ESP8266, I receive only 32 chars..
Debug
ProductID 0x510 Density code 0x5 Density 256K Device identfied automatically ...... ...... ...... ...... ...... ...... ...... ...... ...... mydata : 7 7 8 9 9 10 10 10 11 12 13 13 14 15 15 16 16 16 17 18 18 18 19 19 20 20 20 21 22 22 Init Done - array loaded ...... ...... ...... Calculated address 0x50 Write Done - array loaded in FRAM chip ...... ...... ...... Calculated address 0x50 Read Done - array loaded with read data ...... ...... ...... 7 7 8 9 9 10 10 0 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 Read Write test done - check data if successfull ...... ...... ......
did You can try this sample?
code
/**/ /*! @file FRAM_I2C_store_anything.ino @author SOSAndroid (E. Ha.) @license BSD (see license.txt)
*/ /**/
include
include
include "C:\Users\Alexandre\OneDrive\PID\FRAM_MB85RC_I2C.h"
include "C:\Users\Alexandre\OneDrive\PID\FRAM_MB85RC_I2C.cpp"
//define a struct of various data types typedef struct MYDATA_t { char SSOM;
char var0; char var1;
char S1H; char S1M; char S1P; char S1T;
char S2H; char S2M; char S2P; char S2T;
char S3H; char S3M; char S3P; char S3T;
char S4H; char S4M; char S4P; char S4T;
char S5H; char S5M; char S5P; char S5T;
char S6H; char S6M; char S6P; char S6T;
char S7H; char S7M; char S7P; char S7T;
char S8H; char S8M; char S8P; char S8T;
char S9H; char S9M; char S9P; char S9T;
char S10H; char S10M; char S10P; char S10T;
char S11H; char S11M; char S11P; char S11T;
char S12H; char S12M; char S12P; char S12T;
char S13H; char S13M; char S13P; char S13T;
char S14H; char S14M; char S14P; char S14T;
char S15H; char S15M; char S15P; char S15T;
char S16H; char S16M; char S16P; char S16T;
char S17H; char S17M; char S17P; char S17T;
char S18H; char S18M; char S18P; char S18T;
char S19H; char S19M; char S19P; char S19T;
char S20H; char S20M; char S20P; char S20T;
char S21H; char S21M; char S21P; char S21T;
char S22H; char S22M; char S22P; char S22T;
char S23H; char S23M; char S23P; char S23T;
char S24H; char S24M; char S24P; char S24T;
char S25H; char S25M; char S25P; char S25T;
char S26H; char S26M; char S26P; char S26T;
char S27H; char S27M; char S27P; char S27T;
char S28H; char S28M; char S28P; char S28T;
char S29H; char S29M; char S29P; char S29T;
char S30H; char S30M; char S30P; char S30T;
char S31H; char S31M; char S31P; char S31T;
char S32H; char S32M; char S32P; char S32T;
char S33H; char S33M; char S33P; char S33T;
char S34H; char S34M; char S34P; char S34T;
char S35H; char S35M; char S35P; char S35T;
char A1H; char A1M; char A1P; char A1T;
char A2H; char A2M; char A2P; char A2T;
char A3H; char A3M; char A3P; char A3T;
char A4H; char A4M; char A4P; char A4T;
char A5H; char A5M; char A5P; char A5T;
char A6H; char A6M; char A6P; char A6T;
char A7H; char A7M; char A7P; char A7T;
char A8H; char A8M; char A8P; char A8T;
char A9H; char A9M; char A9P; char A9T;
char A10H; char A10M; char A10P; char A10T;
};
//define a struct joining MYDATA_t to an array of bytes to be stored typedef union MYDATA4I2C_t { MYDATA_t datastruct; uint8_t I2CPacket[sizeof(MYDATA_t)]; };
MYDATA4I2C_t mydata; //data to be written in memory MYDATA4I2C_t readdata; //data read from memory
//random address to write from uint16_t writeaddress = 0x025;
//Example for mamual mode uint8_t chipaddress = 0x50; boolean wp = false; //Allows memory write int pin = 13; uint16_t chipDensity = 256; //Just change that value to the density you want : 4, 16, 64, 128, 256, 512 or 1024
//Creating object for FRAM chip FRAM_MB85RC_I2C mymemory(chipaddress, wp, pin, chipDensity);
void setup() {
//---------init data - load array mydata.datastruct.SSOM = 1;
Serial.println(" mydata :"); Serial.println(mydata.datastruct.S1H,DEC); Serial.println(mydata.datastruct.S2H,DEC); Serial.println(mydata.datastruct.S3H,DEC); Serial.println(mydata.datastruct.S4H,DEC); Serial.println(mydata.datastruct.S5H,DEC); Serial.println(mydata.datastruct.S6H,DEC); Serial.println(mydata.datastruct.S7H,DEC); Serial.println(mydata.datastruct.S8H,DEC); Serial.println(mydata.datastruct.S9H,DEC); Serial.println(mydata.datastruct.S10H,DEC); Serial.println(mydata.datastruct.S11H,DEC); Serial.println(mydata.datastruct.S12H,DEC); Serial.println(mydata.datastruct.S13H,DEC); Serial.println(mydata.datastruct.S14H,DEC); Serial.println(mydata.datastruct.S15H,DEC); Serial.println(mydata.datastruct.S16H,DEC); Serial.println(mydata.datastruct.S17H,DEC); Serial.println(mydata.datastruct.S18H,DEC); Serial.println(mydata.datastruct.S19H,DEC); Serial.println(mydata.datastruct.S20H,DEC); Serial.println(mydata.datastruct.S21H,DEC); Serial.println(mydata.datastruct.S22H,DEC); Serial.println(mydata.datastruct.S23H,DEC); Serial.println(mydata.datastruct.S24H,DEC); Serial.println(mydata.datastruct.S25H,DEC); Serial.println(mydata.datastruct.S26H,DEC); Serial.println(mydata.datastruct.S27H,DEC); Serial.println(mydata.datastruct.S28H,DEC); Serial.println(mydata.datastruct.S29H,DEC); Serial.println(mydata.datastruct.S30H,DEC); Serial.println("Init Done - array loaded"); Serial.println("...... ...... ......");
//----------write to FRAM chip byte result = mymemory.writeArray(writeaddress, arraySize, mydata.I2CPacket);
//---------read data from memory chip result = mymemory.readArray(writeaddress, arraySize, readdata.I2CPacket); if (result == 0) Serial.println("Read Done - array loaded with read data"); if (result != 0) Serial.println("Read failed"); Serial.println("...... ...... ......");
//---------Send data to serial
}
void loop() { // nothing to do }