1

I'm trying to interface the following chip with STM32F4 https://www.nxp.com/docs/en/supporting-information/AN12207.pdf

I'm currently trying to transmit a repeated start using hal sequential transmission with an interrupt but it doesn't work at all, I get NAK.

Would someone give me insights how to interface it and which HAL functions should I use?

unsigned int axI2CWriteRead(unsigned char bus_unused_param, unsigned char addr,
                            unsigned char *pTx, unsigned short txLen,
                            unsigned char *pRx, unsigned short *pRxLen)
{
    extern I2C_HandleTypeDef hi2c3;
    bool recv_length = false;
    HAL_StatusTypeDef status;

    *pRxLen = 0;
    memset(pRx, 0, 2);
    uint8_t rxData[255] = {0};

    status = HAL_I2C_Master_Sequential_Transmit_IT(&hi2c3, 0x90, pTx, txLen, I2C_FIRST_FRAME);

    if (status != HAL_OK)
        return I2C_FAILED;

    while (HAL_I2C_GetState(&hi2c3) != HAL_I2C_STATE_READY)
        ;

    readblock = true;
    readblock_length = 0;
    status = HAL_I2C_Master_Sequential_Receive_IT(&hi2c3, 0x90, rxData, 255, I2C_LAST_FRAME);

    if (status != HAL_OK)
        return I2C_FAILED;
    while (HAL_I2C_GetState(&hi2c3) != HAL_I2C_STATE_READY)
        ;
    readblock = false;
    readblock_length = 0;


    *pRxLen = rxData[0] + 1;

    memcpy(pRx, rxData, *pRxLen);

    return I2C_OK;
}

I2C3 Initialization

static void MX_I2C3_Init(void)
{

  /* USER CODE BEGIN I2C3_Init 0 */

  /* USER CODE END I2C3_Init 0 */

  /* USER CODE BEGIN I2C3_Init 1 */

  /* USER CODE END I2C3_Init 1 */
  hi2c3.Instance = I2C3;
  hi2c3.Init.ClockSpeed = 100000;
  hi2c3.Init.DutyCycle = I2C_DUTYCYCLE_2;
  hi2c3.Init.OwnAddress1 = 0;
  hi2c3.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  hi2c3.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  hi2c3.Init.OwnAddress2 = 0;
  hi2c3.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  hi2c3.Init.NoStretchMode = I2C_NOSTRETCH_ENABLE;
  if (HAL_I2C_Init(&hi2c3) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN I2C3_Init 2 */

  /* USER CODE END I2C3_Init 2 */

}
andreahmed
  • 29
  • 5
  • Show `hi2c3` initialization... – KamilCuk May 17 '19 at 09:45
  • Also you can try using `HAL_I2C_Mem_Write/Read` functions. – KamilCuk May 17 '19 at 09:53
  • @KamilCuk but what's the address memory that needs to be written using Mem Write ? If I use Mem_Write/Read – andreahmed May 17 '19 at 10:23
  • @KamilCuk I added hi2c3 initalization. Please help :) – andreahmed May 17 '19 at 10:25
  • for example uint16_t MemAddSize, uint8_t *pData, MemAddSize. What should I write here. Please write an answer with an example command – andreahmed May 17 '19 at 10:29
  • @KamilCuk Also there is a big problem, the device needs a NACK to be generated, how would I solve this issue and the STMF407 doesn't have a NACK generator – andreahmed May 17 '19 at 10:36
  • Yes it has. NACK is just a state of the lines. Just read the documentation. And the source code of the functions. HAL drivers are well documentated in the headers. – KamilCuk May 17 '19 at 11:32
  • @KamilCuk Can you give an example of Mem write with that chip with an example command please – andreahmed May 17 '19 at 11:33
  • @KamilCuk I'm so confused by the memaddress what should it be ? – andreahmed May 17 '19 at 11:35
  • @KamilCuk Please I need your help – andreahmed May 17 '19 at 12:21
  • @KamilCuk Do you have an answer to my questions ? – andreahmed May 17 '19 at 20:44
  • No, and sorry, you have to solve your problems yourself. You cannot count on some randoms in the internet to solve the problems for you ; ) You can find many examples of I2C communication using STM32, you can reread the documentation and browse the HAL drivers source code, and you can find examples of HAL_MEM_* functions used over internet, you can inspect the lines over using osciloscope, there are many ways for you to inspect the issue more ;) – KamilCuk May 17 '19 at 20:51
  • @KamilCuk Thanks for your input, but have you read the datasheet ? There is no register address there – andreahmed May 17 '19 at 20:53

1 Answers1

0

According to the datasheet of the chip, the SCI2C communication is not pure I2C, but rather based on SMBus, which is derived from I2C. The STM32F4 HAL does not support SMBus but ST provides X-CUBE-SMBUS software package, which you can use to implement the SCI2C protocol. You have to dig into the protocol specifics and create your own library, since the protocol is pretty new and I could not find a previous implementation that you can use. If you get it working, I would recommend contributing to the open-source community by uploading it to GitHub or a similar sharing platform.

Bora
  • 451
  • 2
  • 5
  • 1. The smbus _stack_ is not implemented on stm32. You should be able to communicate with smbus devices in most cases with stm32, at least it worked for me. The difference is in ex. allowed voltages or timings or byte interpretations. 2. From the datasheet of the chip: `The I2C interface is using the Smartcard I2C protocol as defined in Ref. 3 which is based on SMBus.` Smart I2C protocol is implemented with those HAL_I2C_Mem_* hal functions. – KamilCuk May 17 '19 at 09:51
  • What do you mean by that the stack is not implemented on STM32? You have to implement the stack to be able to communicate with the chip. The easiest way to implement it would be to use the provided SMBus stack instead of dealing with the low-level I2C functions. – Bora May 17 '19 at 09:57
  • X-CODE-SMBUS implements the transport layer, not communication/netowrk layer. Ex. implements "process call" smbus command. Or "zone command" smbus command. These are commands specific to smbus protocol. But the communication protocol is I2C. So you can re-write X-CODE-SMBUS using HAL_I2C function. Smbus is (kind of?) a layer above. – KamilCuk May 17 '19 at 10:00
  • @Bora Can you show in the answer, how to use HAL_I2C_MEM using that datasheet? – andreahmed May 18 '19 at 09:43