stm32f4 i2c problem

787562067 googcheng at qq.com
Thu Apr 4 01:48:44 UTC 2019


HI, i have tried some days to know stm32 i2c, almost make it , but i still can not read the data correctly
i think this is the right method, but dont know where i go wrong. please reply me



//add i2c pin config in io.h


#define STM32F4_PIN_I2C(port, idx, altfunc) \
  { \
    { \
      .pin_first = STM32F4_GPIO_PIN(port, idx), \
      .pin_last = STM32F4_GPIO_PIN(port, idx), \
      .mode = STM32F4_GPIO_MODE_AF, \
      .otype = STM32F4_GPIO_OTYPE_OPEN_DRAIN, \
      .ospeed = STM32F4_GPIO_OSPEED_2_MHZ, \
      .pupd = STM32F4_GPIO_NO_PULL, \
      .af = altfunc \
    } \
  }

#define STM32F4_PIN_I2C1_SCL      STM32F4_PIN_I2C(1, 8, STM32F4_GPIO_AF_I2C1)
#define STM32F4_PIN_I2C1_SDA      STM32F4_PIN_I2C(1, 9, STM32F4_GPIO_AF_I2C1)
#endif /* STM32F4_FAMILY_F4XXXX */




start-config-io.c


 #endif
  #ifdef STM32F4_ENABLE_UART_4
    STM32F4_PIN_UART4_TX_PA0,
    STM32F4_PIN_UART4_RX_PA1,
  #endif
  #ifdef STM32F4_ENABLE_UART_5
    STM32F4_PIN_UART5_TX_PC12,
    STM32F4_PIN_UART5_RX_PD2,
  #endif
  #ifdef STM32F4_ENABLE_USART_6
    STM32F4_PIN_USART6_TX_PC6,
    STM32F4_PIN_USART6_RX_PC7,
  #endif
  #ifdef STM32F4_ENABLE_I2C1
    STM32F4_PIN_I2C1_SCL,
    STM32F4_PIN_I2C1_SDA,
  #endif
  #ifdef STM32F4_ENABLE_I2C2
    #error Not implemented.
  #endif






the application code, using the stm32 i2c driver :


extern stm32f4_i2c_bus_entry *const stm32f4_i2c1;

rtems_task Init(rtems_task_argument argument)
{
    puts( "\n\n*** i2c test begin ***\n");
    LED_INIT();
    LED_OFF();
    uint8_t buf[20] = {0};
    
    rtems_status_code sc = stm32f4_i2c_init(stm32f4_i2c1);
    printf("the sc is %d\n", sc);
    
    
    stm32f4_i2c_message msg = {0};
    msg.addr = 0x50;
    msg.read = false;
    msg.len = 1;
    msg.buf = buf;
    buf[0] = 0x66;
    sc = stm32f4_i2c_process_message(stm32f4_i2c1, &msg);
    printf("the write status %d\n", sc);
    rtems_task_wake_after(2);
    
    
    memset(buf, 0, sizeof(buf));
    msg.read = true;
    msg.len = 1;
    msg.buf = buf;
    sc = stm32f4_i2c_process_message(stm32f4_i2c1, &msg);
    printf("the read status %d\n", sc);
    sleep(1);
    
    int i = 0;
    for(i = 0; i < 10; i++)
        printf("%x\t", buf[i]);
  
  

    (void)rtems_task_delete( RTEMS_SELF );
}




but i can not read the data, log outputs:



*** i2c test begin ***

the sc is 0
the write status 0
the read status 0

[09:32:46.369]Rx 0       0       0       0       0       0       0       0       0       0
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.rtems.org/pipermail/users/attachments/20190404/636e0a61/attachment.html>


More information about the users mailing list