STMicroelectronics / STM32CubeG0

STM32Cube MCU Full Package for the STM32G0 series - (HAL + LL Drivers, CMSIS Core, CMSIS Device, MW libraries plus a set of Projects running on all boards provided by ST (Nucleo, Evaluation and Discovery Kits))
Other
165 stars 75 forks source link

I2C Slave examples for STM32G0316-DISCO board #19

Closed kholia closed 2 years ago

kholia commented 3 years ago

Problem: The STM32G031J6 MCU (on a STM32G0316-DISCO board) is unable to work properly as an I2C Slave when operating in the Interrupt Mode with callbacks functions.

Details:

Screenshot_2021-09-25_09-35-04

Screenshot_2021-09-25_10-17-19

These screenshots show that the Slave is unable to handle Data read requests from the Master I2C device.

The Slave returned data is always 0xFF. I saw https://www.i2c-bus.org/i2c-primer/analysing-obscure-problems/data-bytes-from-slave-are-0xff/ on this topic.

The I2 Master device is Arduino Pro Mini ATMega328P running @ 8 MHz @ 3.3v.

Note: I am using 2.2k pull-up resistors on the SDA and SCL lines. Removing them doesn't make any difference.

Here is the configured pinout for my STM32G031J6 MCU:

Screenshot_2021-09-25_09-39-19

I2C2 global interrupt is enabled in my STM32CubeIDE project.

Here is how the I2C pins are configured:

Screenshot_2021-09-25_09-57-15

Clocks are configured as below:

Screenshot_2021-09-25_10-05-46

Note: In comparison, when I run a similar STM32CubeIDE project on my STM32F103C8T6 "Blue Pill" board, things just work well as expected (Slave is able to send back actual data).

I am attaching the source code for the I2C Master and I2C Slave (STM32G031J6) below:

Questions:

What further information may I provide for debugging this problem? Thanks for your help! :)

kholia commented 3 years ago

Possibly related https://community.st.com thread: Not able to read data from STM32G031 MCU I2C slave.

kholia commented 3 years ago

Possibly related issue:

If I enable clock stretching on this STM32G0 MCU, the SCL line goes down, and remains low forever. This could be a bug in my code but very similar code runs perfectly fine on a "Blue Pill" STM32 board with clock stretching enabled.

kholia commented 3 years ago

I fixed the HAL_I2C_AddrCallback code in my project - by copy-pasting this function from a working example for another board.

I2C works great on a bare STM32G030J6 MCU chip now. I am using the STM32G030J6 MCU as an I2C Peripheral device.

void HAL_I2C_AddrCallback(I2C_HandleTypeDef *hi2c, uint8_t TransferDirection, uint16_t AddrMatchCode) {
    if (AddrMatchCode == 80) {
        switch (TransferDirection) {
        case I2C_DIRECTION_TRANSMIT:
            if (HAL_I2C_Slave_Seq_Receive_IT(&hi2c2, rx_buff, 0x64u, I2C_FIRST_FRAME) != HAL_OK) {
                // Error_Handler();
            }
            break;
        case I2C_DIRECTION_RECEIVE:
            HAL_I2C_Slave_Seq_Transmit_IT(&hi2c2, &TX_COMMAND[0], 16,  I2C_FIRST_AND_LAST_FRAME);
            ++rx_counter;
            break;
        }
    }
}

It would still be nice to have official I2C examples for STM32G0316-DISCO board.

Side-note: The HAL I2C stuff seems to suffer from a (harmless) bug where HAL_I2C_ErrorCallback is called with HAL_I2C_ERROR_AF for no discernible reason.

http://bfin.sakura.ne.jp/?p=1302 ran into the same problem it seems.

KORKAD commented 2 years ago

Hello @kholia, Please note that similaire HAL I2C examples are already available for STM32G031K8 MCU but on Nucleo board not the DISCO.

For more details please refer to the I2C_TwoBoards_RestartComIT example.

With Regards KORKAD

kholia commented 2 years ago

Thanks! This ticket may be closed.