Hello, all!
I'm working with a peripheral that requires the SAMD21 to send it very large chunks of data. Unfortunately
the stock Wire library has a small buffer and just won't transmit what I need to do.
I've experimented with increasing the serial ring buffer size to 1024 bytes, which works for the data I need to transmit now but that will consume that 1k of memory at all times, whether I'm transmitting or not.
I found a library intended for AVR parts that allows the user to stream an arbitrary number of bytes to the slave, just by passing a pointer to an array.
I'm sure some of you have seen this one in the past. It's a master-only library, which is all I need for this application.
Does anyone know of a similar library for Zero / M0 / SAMD21 parts?
Thanks!
The Wire llibrary expects you to fill its small buffer. Then it starts the I2C interrupt sequence. It blocks until the whole buffer send has been completed.
Repeat with the next few bytes until finished. You have to handle the splitting into small bursts that fit the Wire buffer.
Other interrupt driven peripherals handle transparently e.g. Serial.
They empty the buffer immediately. i.e. you only block when the buffer does not empty fast enough to keep up with your writes.
If you want continuous streaming, write your own handler to work like Serial.
David.
david_prentice:
The Wire llibrary expects you to fill its small buffer. Then it starts the I2C interrupt sequence. It blocks until the whole buffer send has been completed.
Repeat with the next few bytes until finished. You have to handle the splitting into small bursts that fit the Wire buffer.
Other interrupt driven peripherals handle transparently e.g. Serial.
They empty the buffer immediately. i.e. you only block when the buffer does not empty fast enough to keep up with your writes.
If you want continuous streaming, write your own handler to work like Serial.
David.
Sure, makes sense. Writing big arrays to an EEPROM isn't a huge deal since it has a 32 byte page size anyway, just split at page boundaries and the Wire lib can handle 32 byte transfers without issue. This is a little difference since I may have to transfer slightly over 4k bytes in one go.
Is there anything stopping me from addressing the SERCOM directly?
Example:
#include <Wire.h>
void setup() {
Wire.begin();
}
void loop() {
// use Wire library to send three bytes to device 0x21
Wire.beginTransmission(0x21);
Wire.write(0x0A);
Wire.write(0x1B);
Wire.write(0xC0);
Wire.endTransmission();
// test of raw sercom commands
// equivalent to Wire.beginTransmission()
sercom3.startTransmissionWIRE(0x21, WIRE_WRITE_FLAG);
// equivalent to Wire.write() but does not use the txbuffer
sercom3.sendDataMasterWIRE(0x0A);
sercom3.sendDataMasterWIRE(0x1B);
sercom3.sendDataMasterWIRE(0xC0);
// equivalent to Wire.endTransmission()
sercom3.prepareCommandBitsWire(WIRE_MASTER_ACT_STOP);
}
Works fine. Granted, I have to keep track of when the array is empty to know when to stop, but otherwise not a big deal.
Obviously substitute your sercom for sercom3 if you are creating another I2C interface or moving the default one, but all of the stuff needed is in SERCOM.h - sendDataMasterWIRE and similar. Note that prepareCommandBitsWire doesn't capitalize the whole word WIRE. Ugh.
Thanks for the advice! Much appreciated.
Here's tested and working code to send a large I2C transfer without allocating a larger transmit buffer. This code assumes that you have included Wire.h and called Wire.begin() in setup() to initialize the SERCOM and set up the pins. It also assumes that you're using SERCOM3 for I2C / Wire, which is default for the Zero. It doesn't do any kind of error checking or any of that, but it certainly could be adapted to do so.
void writeLargeArray(byte deviceaddress, unsigned int datasize, uint8_t* data) {
// uses raw I2C writes, rather than use the txbuffer
sercom3.startTransmissionWIRE(0x30, WIRE_WRITE_FLAG);
for (unsigned int i = 0; i < datasize; i++) {
// send one byte at a time from the array to the I2C device
sercom3.sendDataMasterWIRE((byte) data[i]);
}
// stop sending the data
sercom3.prepareCommandBitsWire(WIRE_MASTER_ACT_STOP);
}