i2c slave block transfer driver library

Dependents:   i2c_slave_block_example i2c_lora_slave

I2C block read/write slave

operates with I2C master using the i2c_smbus_read_i2c_block_data() and i2c_smbus_write_i2c_block_data() calls.

Tested with raspberry pi as master.
STM32Lx nucleo as slave.

signal pinmbed slaveRPi master
GND6
SCLD155
SDAD143
IRQany avail pinany avail pin

IRQ pin is implemented in application, not in this library. (see example)

Up to 32byte length per transfer.
Raspberry pi I2C doesnt support SMBUS block read, so I2C block transfers are supported here.
SMBUS block transfers are variable length. Instead, here in this driver, I2C block length is mapped to command byte by const array cmd_to_length[] (see example)

master write to slave

For i2c_smbus_write_i2c_block_data() from master, this driver lets the main loop on slave take the write request. The main loop on slave calls service_i2c(), which then calls service_i2c_write() upon each write from master. This occurs thru a circular buffer, permitting the slave to take its time doing the request, while the master can issue several back-to-back requests without delays.

clock stretching

For i2c_smbus_read_i2c_block_data(): Since raspberry pi doesnt support clock stretching out the the box, providing transmit data must be done in the interrupt service routine. fill_tx_buf() populates the transmit buffer to be sent to master, which must be done immediately because this occurs inside interrupt handler. If I2C master were to support clock stretching, then transmit buffer work could be done in main loop of slave.
/media/uploads/dudmuck/i2c_rpi_nucleo_sm_.png
An STM32 running at 32MHz is unlikely to operate with 400KHz I2C. Use 100KHz speed with 32MHz CPU, or use faster speed CPU for 400KHz I2C.

Revision:
0:20421a857bd5
Child:
1:914409dc83b1
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TARGET_STM/smbus.c	Fri Jan 18 14:25:26 2019 -0800
@@ -0,0 +1,180 @@
+#include "smbus.h"
+#include "device.h"
+#include "smbus_defs.h"
+#include "cmds.h"
+
+
+SMBUS_HandleTypeDef SmbusHandle;
+
+static volatile state_e state;
+
+i2c_slave_t i2c;
+
+static inline void put_cbuf(uint8_t o)
+{
+    i2c.cbuf[i2c.cbuf_in] = o;
+    if (++i2c.cbuf_in == _RX_BUF_SIZE)
+        i2c.cbuf_in = 0;
+}
+
+static volatile uint8_t i2c_buf_idx;
+
+#ifdef TARGET_STM32L4
+void SMBUSx_ER_IRQHandler()
+{
+    asm("nop"); // TODO
+}
+#endif /* TARGET_STM32L4 */
+
+#ifdef TARGET_STM32L4
+void SMBUSx_EV_IRQHandler(void)
+#else
+void SMBUSx_IRQHandler(void)
+#endif
+{
+    uint32_t tmpisrvalue = SMBUS_GET_ISR_REG(&SmbusHandle);
+    uint32_t icr = 0;
+    static uint8_t xfer_length;
+    static bool i2cTransferDirection; // true = read
+    static uint8_t cmd;
+    static uint8_t rx_buf_[32];
+
+    if (tmpisrvalue & SMBUS_FLAG_RXNE) {
+        uint8_t rxdr = SmbusHandle.Instance->RXDR;
+        if (state == STATE_CMD) {
+            cmd = rxdr;
+            SmbusHandle.Instance->CR2 &= ~I2C_CR2_NACK; // ack last byte read
+            xfer_length = cmd_to_length[cmd];
+            i2c_buf_idx = 0;
+            state = STATE_XFERING;
+        } else if (state == STATE_XFERING && i2cTransferDirection == 0) { // if xfering host write
+            if (i2c_buf_idx < sizeof(rx_buf_))
+                rx_buf_[i2c_buf_idx++] = rxdr;
+        }
+
+        icr |= SMBUS_FLAG_RXNE;
+    }
+
+
+    if (tmpisrvalue & SMBUS_FLAG_ADDR) {
+        i2cTransferDirection = SMBUS_GET_DIR(&SmbusHandle);
+        if (i2cTransferDirection) {
+            // 1: Read transfer, slave enters transmitter mode..  cmd has already been provided
+            fill_tx_buf(cmd);
+
+            __HAL_SMBUS_ENABLE_IT(&SmbusHandle, SMBUS_IT_ERRI | SMBUS_IT_TCI | SMBUS_IT_STOPI | SMBUS_IT_NACKI | SMBUS_IT_TXI);
+        } else {
+            // 0: Write transfer, slave enters receiver mode.
+            __HAL_SMBUS_ENABLE_IT(&SmbusHandle, SMBUS_IT_ERRI | SMBUS_IT_TCI | SMBUS_IT_STOPI | SMBUS_IT_NACKI | SMBUS_IT_RXI);
+            if (state == STATE_NONE) {
+                state = STATE_CMD;
+            }
+        }
+
+        icr |= SMBUS_FLAG_ADDR;
+    }
+
+    if (tmpisrvalue & SMBUS_FLAG_AF) {
+        /* last byte the host wants was sent */
+        icr |= SMBUS_FLAG_AF;
+    }
+
+    if (tmpisrvalue & SMBUS_FLAG_STOPF) {
+        icr |= SMBUS_FLAG_STOPF;
+        if (i2cTransferDirection == 0) {   // host done writing to buf
+            unsigned i;
+            /* send to mainloop */
+            put_cbuf(cmd);
+            put_cbuf(i2c_buf_idx);
+            for (i = 0; i < i2c_buf_idx; i++)
+                put_cbuf(rx_buf_[i]);
+        }
+
+        if (!(tmpisrvalue & SMBUS_FLAG_TXE)) {
+            /* slave wanted to send more than host wanted to receive */
+            SmbusHandle.Instance->ISR = SMBUS_FLAG_TXE;
+        }
+
+        state = STATE_NONE;
+    }
+
+    if (tmpisrvalue & SMBUS_FLAG_TXIS ) {
+        if (state == STATE_XFERING && i2cTransferDirection) {  // xfering in host-read direction
+            SmbusHandle.Instance->TXDR = i2c.tx_buf[i2c_buf_idx++];
+            if (i2c_buf_idx == xfer_length) {
+                state = STATE_SEND_DONE;
+                __HAL_SMBUS_DISABLE_IT(&SmbusHandle, SMBUS_IT_TXI);
+            }
+        }
+    } else if (icr == 0) {
+        /* unhandled interrupt flag: halt this irq */
+#ifdef TARGET_STM32L4
+        HAL_NVIC_DisableIRQ(SMBUSx_EV_IRQn);
+#else
+        HAL_NVIC_DisableIRQ(SMBUSx_IRQn);
+#endif
+    }
+
+    __HAL_SMBUS_CLEAR_FLAG(&SmbusHandle, icr);
+}
+
+int smbus_init(uint8_t slaveAddress)
+{
+    uint32_t tmpisr;
+    //HAL_StatusTypeDef status;
+
+    SmbusHandle.Instance                  = I2Cx;
+
+    SmbusHandle.Init.Timing               = I2C_TIMING;
+    SmbusHandle.Init.AnalogFilter         = SMBUS_ANALOGFILTER_ENABLE;
+    SmbusHandle.Init.OwnAddress1          = slaveAddress;
+    SmbusHandle.Init.AddressingMode       = SMBUS_ADDRESSINGMODE_7BIT;
+    SmbusHandle.Init.DualAddressMode      = SMBUS_DUALADDRESS_DISABLE;
+    SmbusHandle.Init.OwnAddress2          = 0x00;
+    SmbusHandle.Init.GeneralCallMode      = SMBUS_GENERALCALL_DISABLE;
+    SmbusHandle.Init.NoStretchMode        = SMBUS_NOSTRETCH_DISABLE;
+    SmbusHandle.Init.PacketErrorCheckMode = SMBUS_PEC_DISABLE;
+    SmbusHandle.Init.PeripheralMode       = SMBUS_PERIPHERAL_MODE_SMBUS_SLAVE;
+    SmbusHandle.Init.SMBusTimeout         = 0;
+
+    if(HAL_SMBUS_Init(&SmbusHandle) != HAL_OK)
+    { 
+        return -1;
+    }
+
+    state = STATE_NONE;
+
+    tmpisr = SMBUS_IT_ADDRI | SMBUS_IT_STOPI | SMBUS_IT_NACKI | SMBUS_IT_ERRI;
+    __HAL_SMBUS_ENABLE_IT(&SmbusHandle, tmpisr);
+
+    return 0;
+}
+
+static inline uint8_t get_cbuf()
+{
+    uint8_t ret = i2c.cbuf[i2c.cbuf_out];
+    if (++i2c.cbuf_out == _RX_BUF_SIZE)
+        i2c.cbuf_out = 0;
+
+    return ret;
+}
+
+/* service_i2c(): call from main loop */
+void service_i2c()
+{
+    if (i2c.cbuf_in != i2c.cbuf_out) {
+        uint8_t n, cmd, len;
+        cmd = get_cbuf();
+        len = get_cbuf();
+        uint8_t buf[32];
+        /* host is writing: parse here */
+        for (n = 0; n < len; n++)
+            buf[n] = get_cbuf();
+
+        /*if (len != cmd_to_length[cmd])
+            pc.printf("[%02x != %02x] ", len, cmd_to_length[cmd]);*/
+
+        service_i2c_write(cmd, len, buf);
+    }
+}
+