0

I'm trying to use I2C between an STM32F103C8T6 development board and an Arduino Uno.

However, I always get a HAL_BUSY error when I call HAL_I2C_Master_Transmit in the code below.

(I used 10K pullup resistors at SCL and SDA) on the STM32 side and a logic level converter between STM32 and Arduino.

How can I send messages from the STM32 to the Arduino?

int main(void)
{
  ...
  MX_I2C1_Init();

  char buffer[16];
  strcpy((char*) buffer,  (const char*) "Test 1");

  while (1)
  {
      HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_14);
      HAL_Delay(1000);

      if (HAL_I2C_Master_Transmit(&hi2c1, 8, (uint8_t *) buffer,  7, 1000) != HAL_OK)
      {
          HAL_Delay(1000);
      }

Settings (all default):

  hi2c1.Instance = I2C1;
  hi2c1.Init.ClockSpeed = 100000;
  hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
  hi2c1.Init.OwnAddress1 = 0;
  hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  hi2c1.Init.OwnAddress2 = 0;
  hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
Michel Keijzers
  • 13,867
  • 18
  • 69
  • 139
  • 1
    Is the Arduino address '8'? – Long Pham Jul 06 '19 at 14:40
  • Yes, I also tried 8 << 1 but it makes no difference. (btw, it's the value 8 (0x08), not the character 8 (0x48). – Michel Keijzers Jul 06 '19 at 14:42
  • 1
    Yeah I know :) BTW, HAL I2C functions require left shifted address. – Long Pham Jul 06 '19 at 15:04
  • 1
    Maybe your problem is the same as [this](https://electronics.stackexchange.com/questions/272427/stm32-busy-flag-is-set-after-i2c-initialization). – Long Pham Jul 06 '19 at 15:47
  • @LongPham ... thanks for that info ... so far I have no luck but I will try again. – Michel Keijzers Jul 07 '19 at 19:35
  • 1
    Try it without level shifters, all possible I2C pins are 5V tolerant on the STM32F1. Are the GPIO pins set to open-drain? – followed Monica to Codidact Jul 07 '19 at 19:37
  • No difference (I checked open drain, this is indeed the setting used when I2C is selected), I also tried another STM32 and the alternative I2C1 and (normal) I2C2 pins, no difference. This week I will test it on Arduino to Arduino ... if that works, I might revert to an Arduino's-only solution. – Michel Keijzers Jul 07 '19 at 22:56
  • 1
    Did you initialise wire library in UNO with : wire.begin(0x8); – Meenie Leis Jul 08 '19 at 06:38
  • @MeenieLeis Yes, I did that too, including `Wire.onReceive(receiveEvent)`, and the callback function, and in the loop function of Arduino: `while (Wire.available() > 0) { char c = Wire.read(); Serial.print(c); } }` – Michel Keijzers Jul 08 '19 at 07:36
  • Now it'd make sense to check what's going on on the wire. Are the bytes acknowledged by the slave? – followed Monica to Codidact Jul 08 '19 at 09:12
  • 1
    page no.23 : https://www.st.com/content/ccc/resource/technical/document/errata_sheet/7f/05/b0/bc/34/2f/4c/21/CD00288116.pdf/files/CD00288116.pdf/jcr:content/translations/en.CD00288116.pdf – Meenie Leis Jul 08 '19 at 09:32
  • 1
    @MichelKeijzers The transmit function first checks `hi2c1.State`so can you check `hi2c1.State` right after `MX_I2C1_Init()`? It should be equal to `HAL_I2C_STATE_READY` – Long Pham Jul 08 '19 at 13:05
  • @berendi I have not checked it, but I managed to get it to work from Arduino Uno to Uno so the circuit must be ok (actually it's just two wires, without any pull up resistor). – Michel Keijzers Jul 08 '19 at 19:57
  • @MeenieLeis Thanks for that document ... though I have the default (lowest speed) and I can't find anything related to parity error in my own code. I would hope if there is an errata made by ST, the first thing they would do is make sure their own created code is ok. – Michel Keijzers Jul 08 '19 at 19:59
  • @LongPham I will check when I have a bit more time. – Michel Keijzers Jul 08 '19 at 20:00

0 Answers0