Personally I think 255 is probably enough, once you start using int arithmetic the code gets larger. Your proposal might work with the defaults taken like that. TwoWire(int speed=100, uint8_t buffersize=32) // max bufsize 255 ? or do we need an uint16_t for that? (MEGA / DUE?) It would be nice if they had done a malloc to allocate the buffers in the first place, and taken the buffer length as an argument, but no, that wasn't done, and it might be a bit late now. You only have to edit two defines to changes its length to suit you. So a 32-byte buffer is already taking up 160 bytes (out of the 2048 you have on a Uno). Static volatile uint8_t twi_txBufferLength Static volatile uint8_t twi_txBufferIndex Static volatile uint8_t twi_masterBufferLength Static volatile uint8_t twi_masterBufferIndex The data is now being transferred both ways between the 2 Nano masters and the Mega slave at the correct interval, without corrupt data.One problem with this request is that, in the existing library at least, the buffer is copied 5 times. The data is then filtered on each master to only keep the relevant information. The slave has to send the data destined to both masters to each of them because requestEvent can't tell which master is asking for data. The bus speed also had to be reduced because 800 kHz was too fast for the Mega 2560 (but worked between 3 Nano). I figured these out after asking question #35092 Half of the data was wrong without this filter (this should ideally be handled in the Wire library) I added an if statement to compare the receiveEvent data size with the expected size. ReceiveEvent, on the slave side, was also confusing real data with incoming requestEvent. I moved the timer to the master side before calling Wire.requestFrom. ReceiveEvent, on the slave side, contained a millis if condition timer that made it time out. The second master was then behaving the same and so on. I agree with st2000, it is better to stay out of trouble and have only one Master. You would need to check the return value of every Wire.endTransmission and every Wire.requestFrom. This made it keep the bus too long and eventually give it away to the second master at a random time, with missed transfers from the second master in between. I'm not sure if the Wire library fully implemented that. The code had several issues leading to a constant flow of wrong data (with correct data in the middle) between the master that won the bus arbitration.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |