AnsweredAssumed Answered

stm32f746 I2C DMA Transmit problem

Question asked by mustafa kocaoglu on Aug 17, 2016
Latest reply on Aug 19, 2016 by FTITI.Walid
                                   
I am trying to transmit multiple bytes on I2C bus using dma.
            In the code I am trying to send 4 bytes. I see on my scope that four bytes are being sent but the problem is that the bytes are shift to left 1 bit.
           
I see on scope this values:  {0x00,0xFE,0X06,0X20}
            I see on scope 10 cycle on scl pin when address sending (pic1). After that all data shift to left 1 bit. 

           
           

           
           

           
           
/*TRANSMIT*/ 
           

           
           
uint8_t tsc_Txdata[4]={0x00,0xFF,0x03,0x10};
           
     while(HAL_I2C_Master_Transmit_DMA(&I2cHandle, (uint16_t)I2C_ADDRESS, (uint8_t*)tsc_Txdata, 4)!= HAL_OK)
      {

           
         if (HAL_I2C_GetError(&I2cHandle) != HAL_I2C_ERROR_AF)
           {
           GUI_DispStringAt("I2C Write Error...", 0, 20);
           }
      }
      
        while (HAL_I2C_GetState(&I2cHandle) != HAL_I2C_STATE_READY)
              {
 
              }
           

           
           
/*CONFIG*/
           

           
  I2cHandle.Instance              = I2Cx;
  I2cHandle.Init.Timing           = I2C_TIMING;
  I2cHandle.Init.AddressingMode   = I2C_ADDRESSINGMODE_7BIT;
  I2cHandle.Init.DualAddressMode  = I2C_DUALADDRESS_DISABLE;
  I2cHandle.Init.GeneralCallMode  = I2C_GENERALCALL_DISABLE;
  I2cHandle.Init.NoStretchMode    = I2C_NOSTRETCH_DISABLE;  
  I2cHandle.Init.OwnAddress1      = I2C_ADDRESS;
  I2cHandle.Init.OwnAddress2      = 0xFF;

  if(HAL_I2C_Init(&I2cHandle) != HAL_OK)
  {
    /* Initialization Error */
     GUI_DispStringAt("error...", 0, 30); 
  }
   /* Enable the Analog I2C Filter */
  HAL_I2CEx_ConfigAnalogFilter(&I2cHandle,I2C_ANALOGFILTER_ENABLE); 
           

           

           

           

           
/*DMA INIT*/
           

           
#define I2C_TIMING      0x40912732

/* Definition for I2Cx clock resources */
#define I2Cx                              I2C2
#define RCC_PERIPHCLK_I2Cx                RCC_PERIPHCLK_I2C2
#define RCC_I2CxCLKSOURCE_SYSCLK          RCC_I2C2CLKSOURCE_PCLK1
#define I2Cx_CLK_ENABLE()               __HAL_RCC_I2C2_CLK_ENABLE()
#define I2Cx_SCL_SDA_GPIO_CLK_ENABLE()  __HAL_RCC_GPIOH_CLK_ENABLE()
#define I2Cx_DMA_CLK_ENABLE()           __HAL_RCC_DMA1_CLK_ENABLE()

#define I2Cx_FORCE_RESET()              __HAL_RCC_I2C2_FORCE_RESET()
#define I2Cx_RELEASE_RESET()            __HAL_RCC_I2C2_RELEASE_RESET()

/* Definition for I2Cx Pins */
#define I2Cx_SCL_PIN                    GPIO_PIN_4
#define I2Cx_SCL_SDA_GPIO_PORT          GPIOH
#define I2Cx_SDA_PIN                    GPIO_PIN_5
#define I2Cx_SCL_SDA_AF                 GPIO_AF4_I2C2

/* Definition for I2Cx's NVIC */
#define I2Cx_EV_IRQn                    I2C2_EV_IRQn
#define I2Cx_ER_IRQn                    I2C2_ER_IRQn
#define I2Cx_EV_IRQHandler              I2C2_EV_IRQHandler
#define I2Cx_ER_IRQHandler              I2C2_ER_IRQHandler


/* Definition for I2Cx's DMA */
#define I2Cx_DMA                        DMA1   
#define I2Cx_DMA_INSTANCE_TX            DMA1_Stream7
#define I2Cx_DMA_INSTANCE_RX            DMA1_Stream2
#define I2Cx_DMA_CHANNEL_TX             DMA_CHANNEL_7
#define I2Cx_DMA_CHANNEL_RX             DMA_CHANNEL_7

/* Definition for I2Cx's DMA NVIC */
#define I2Cx_DMA_TX_IRQn                DMA1_Stream7_IRQn
#define I2Cx_DMA_RX_IRQn                DMA1_Stream2_IRQn
#define I2Cx_DMA_TX_IRQHandler          DMA1_Stream7_IRQHandler
#define I2Cx_DMA_RX_IRQHandler          DMA1_Stream2_IRQHandler
           

           

           

           

           
void HAL_I2C_MspInit(I2C_HandleTypeDef* hi2c)
{
  GPIO_InitTypeDef  GPIO_InitStruct;
  static DMA_HandleTypeDef hdma_tx;
  static DMA_HandleTypeDef hdma_rx;
  RCC_PeriphCLKInitTypeDef  RCC_PeriphCLKInitStruct;
  
  /*##-1- Configure the I2C clock source. The clock is derived from the SYSCLK #*/
  RCC_PeriphCLKInitStruct.PeriphClockSelection = RCC_PERIPHCLK_I2Cx;
  RCC_PeriphCLKInitStruct.I2c1ClockSelection = RCC_I2CxCLKSOURCE_SYSCLK;
  HAL_RCCEx_PeriphCLKConfig(&RCC_PeriphCLKInitStruct);

  /*##-2- Enable peripherals and GPIO Clocks #################################*/
  /* Enable GPIO TX/RX clock */
  I2Cx_SCL_SDA_GPIO_CLK_ENABLE();
  /* Enable I2Cx clock */
  I2Cx_CLK_ENABLE(); 

  /* Enable DMAx clock */
  I2Cx_DMA_CLK_ENABLE();   
  
  /*##-3- Configure peripheral GPIO ##########################################*/  
  /* I2C TX GPIO pin configuration  */
  GPIO_InitStruct.Pin       = I2Cx_SCL_PIN;
  GPIO_InitStruct.Mode      = GPIO_MODE_AF_OD;
  GPIO_InitStruct.Pull      = GPIO_PULLUP;
  GPIO_InitStruct.Speed     = GPIO_SPEED_HIGH;
  GPIO_InitStruct.Alternate = I2Cx_SCL_SDA_AF;
  HAL_GPIO_Init(I2Cx_SCL_SDA_GPIO_PORT, &GPIO_InitStruct);
    
  /* I2C RX GPIO pin configuration  */
  GPIO_InitStruct.Pin       = I2Cx_SDA_PIN;
  GPIO_InitStruct.Alternate = I2Cx_SCL_SDA_AF;
  HAL_GPIO_Init(I2Cx_SCL_SDA_GPIO_PORT, &GPIO_InitStruct);
    
  /*##-4- Configure the DMA Channels #########################################*/
  /* Configure the DMA handler for Transmission process */
  hdma_tx.Instance                 = I2Cx_DMA_INSTANCE_TX;
  hdma_tx.Init.Channel             = I2Cx_DMA_CHANNEL_TX;                     
  hdma_tx.Init.Direction           = DMA_MEMORY_TO_PERIPH;
  hdma_tx.Init.PeriphInc           = DMA_PINC_DISABLE;
  hdma_tx.Init.MemInc              = DMA_MINC_ENABLE;
  hdma_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
  hdma_tx.Init.MemDataAlignment    = DMA_MDATAALIGN_BYTE;
  hdma_tx.Init.Mode                = DMA_NORMAL;
  hdma_tx.Init.Priority            = DMA_PRIORITY_LOW;
  hdma_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;           /* FIFO mode disabled               */
  hdma_tx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
  hdma_tx.Init.MemBurst = DMA_MBURST_SINGLE;              /* Memory burst                     */
  hdma_tx.Init.PeriphBurst = DMA_PBURST_SINGLE;           /* Peripheral burst                 */

  HAL_DMA_Init(&hdma_tx);   
  
  /* Associate the initialized DMA handle to the the I2C handle */
  __HAL_LINKDMA(hi2c, hdmatx, hdma_tx);
    
  /* Configure the DMA handler for Transmission process */
  hdma_rx.Instance                 = I2Cx_DMA_INSTANCE_RX;
  hdma_rx.Init.Channel             = I2Cx_DMA_CHANNEL_RX;                     
  hdma_rx.Init.Direction           = DMA_PERIPH_TO_MEMORY;
  hdma_rx.Init.PeriphInc           = DMA_PINC_DISABLE;
  hdma_rx.Init.MemInc              = DMA_MINC_ENABLE;
  hdma_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
  hdma_rx.Init.MemDataAlignment    = DMA_MDATAALIGN_BYTE;
  hdma_rx.Init.Mode                = DMA_NORMAL;
  hdma_rx.Init.Priority            = DMA_PRIORITY_HIGH;
  hdma_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;           /* FIFO mode disabled               */
  hdma_rx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
  hdma_rx.Init.MemBurst = DMA_MBURST_SINGLE;              /* Memory burst                     */
  hdma_rx.Init.PeriphBurst = DMA_PBURST_SINGLE;           /* Peripheral burst                 */

  HAL_DMA_Init(&hdma_rx);
   
}

           

Outcomes