Adding I2C memory address to write.

ChibiOS public support forum for topics related to the STMicroelectronics STM32 family of micro-controllers.

Moderators: RoccoMarco, barthess

customyota
Posts: 2
Joined: Fri May 18, 2018 11:31 pm
Has thanked: 3 times

Adding I2C memory address to write.

Postby customyota » Sat May 19, 2018 12:01 am

Is there a way to add a memory address to an I2C transmit in ChibiOS? For example, send the slave address, followed by the memory address, followed by the data? I'm thinking something equivalent to ST's HAL_I2C_Mem_Write().

Specifically, what I'm trying to do is send screen data to an SSD1306 OLED and I need to start the i2c stream with the control bits before I send the actual screen data. I thought about just prepending the transmit buffer with the control bits, but with the buffer being 1K, I don't know of a way that's both fast and memory efficient to do so.

Any help would be appreciated.

User avatar
Giovanni
Site Admin
Posts: 14444
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1074 times
Been thanked: 921 times
Contact:

Re: Adding I2C memory address to write.

Postby Giovanni » Sat May 19, 2018 6:03 am

Hi,

i2cMasterTransmitTimeout () is the function you need, however the send buffer is monolithic, you cannot split it. The transfer is done using DMA and it is not possible to have non-contiguous buffers.

http://chibios.sourceforge.net/docs3/ha ... ac4f4f4132

Giovanni

customyota
Posts: 2
Joined: Fri May 18, 2018 11:31 pm
Has thanked: 3 times

Re: Adding I2C memory address to write.

Postby customyota » Sat May 19, 2018 7:49 am

That's what I've been using so far, and it works easily for the most part. The issue is when I need to write a separate byte after the device address, but before the actual data. As far as I can tell, i2cMasterTransmitTimeout() just sends the device address, followed by the send buffer, with no way to prepend the send buffer with another byte. I could just copy the buffer into a new buffer that includes the control byte at the beginning, but since the buffer is over a hundred bytes long, and up to 1k, this seems like a very inefficient way to accomplish it.

steved
Posts: 823
Joined: Fri Nov 09, 2012 2:22 pm
Has thanked: 12 times
Been thanked: 135 times

Re: Adding I2C memory address to write.

Postby steved » Sat May 19, 2018 8:27 am

This is an unavoidable downside of using DMA to transfer data - unless some form of 'chained' DMA is used, which I don't think is directly possible on the ST devices. It could be done by modifying the I2C driver to change the DMA to point to a new buffer once the first transfer is complete (as long as the I2C peripheral doesn't automatically send a STOP on DMA complete); in fact since the usual requirement is to send a 1-4 byte address before the main data block, these few bytes could be defined in a uint32_t and sent using interrupts.

With the current driver, to conserve memory, I usually have a fixed size write buffer in the 100-300 byte region, and break up the larger writes; obviously you have to send the new address with each transaction.

steved
Posts: 823
Joined: Fri Nov 09, 2012 2:22 pm
Has thanked: 12 times
Been thanked: 135 times

Re: Adding I2C memory address to write.

Postby steved » Sat May 19, 2018 10:34 am

Had a quick look at the driver, and chaining buffers might be as simple as reprogramming the DMA in i2c_lld_setup_tx_transfer(), and picking up that there's another buffer to go in the I2C_ISR_TC flag handler

Would need a new API call; something like:

Code: Select all

msg_t i2cMasterTransmitTwoTimeout(I2CDriver *i2cp, i2caddr_t addr,
                                      const uint8_t *txbuf1, size_t txbytes1,
                                      const uint8_t *txbuf2, size_t txbytes2,
                                      uint8_t *rxbuf, size_t rxbytes,
                                      systime_t timeout)


We could probably dispense with the receive capability to keep the number of parameters down.

Alternatively we could just pass a list of buffer descriptors { start, length }


Return to “STM32 Support”

Who is online

Users browsing this forum: No registered users and 17 guests