/**
  * @brief  User callback that manages the I2C device errors.
  * @note   Make sure that the define USE_SINGLE_ERROR_CALLBACK is uncommented in
  *         the cpal_conf.h file, otherwise this callback will not be functional.
  * @param  pDevInitStruct. 
  * @param  DeviceError.
  * @retval None
  */ 
void CPAL_I2C_ERR_UserCallback(CPAL_DevTypeDef pDevInstance, uint32_t DeviceError)
{
  /* if an Acknowledge failure error occurred */
  if (DeviceError == CPAL_I2C_ERR_AF )
  {
    LCD_DisplayStringLine(Line9, (uint8_t*)" Slave Not yet Ready");
  } 
  else
  {
    LCD_DisplayStringLine(Line9, (uint8_t*)" Device Err occurred ");
  }
  
  /* Update CPAL states */
  I2C_DevStructures[pDevInstance]->CPAL_State = CPAL_STATE_READY;
  I2C_DevStructures[pDevInstance]->wCPAL_DevError = CPAL_I2C_ERR_NONE;
  
  /* Deinitialize CPAL device */
  CPAL_I2C_DeInit(I2C_DevStructures[pDevInstance]);
  
  /* Initialize CPAL device with the selected parameters */
  CPAL_I2C_Init(I2C_DevStructures[pDevInstance]);    
  
  /* Switch the LCD write color */
  Switch_ErrorColor();
  
  ActionState = ACTION_NONE;
}
/**
  * @brief  User callback that manages the I2C device errors
  * @note   Make sure that the define USE_SINGLE_ERROR_CALLBACK is uncommented in
  *         the cpal_conf.h file, otherwise this callback will not be functional
  * @param  pDevInitStruct
  * @param  DeviceError
  * @retval None
  */
void CPAL_I2C_ERR_UserCallback(CPAL_DevTypeDef pDevInstance, uint32_t DeviceError)
{ 
  /* Update CPAL_State */
  I2C_DevStructures[pDevInstance]->CPAL_State = CPAL_STATE_READY;
  
  /* Update Display settings */
  LCD_SetBackColor(Red);
  LCD_SetTextColor(White); 
  
  /* if an error occurred except AF*/
  if (DeviceError != CPAL_I2C_ERR_AF )
  {
    /* Deinitialize device */
    CPAL_I2C_DeInit(I2C_DevStructures[pDevInstance]);
    
    /* Initialize device with the selected parameters */
    CPAL_I2C_Init(I2C_DevStructures[pDevInstance]);   
    
    /* Update LCD Display */
    LCD_DisplayStringLine(Line8, MESSAGE11);    
  }
  else
  {
    /* Update LCD Display */
    LCD_DisplayStringLine(Line8, MESSAGE10);
  }
  
  /* Update wCPAL_DevError */  
  I2C_DevStructures[pDevInstance]->wCPAL_DevError = CPAL_I2C_ERR_NONE ;
  
  TransferStatus = FAILED;
}
/**
  * @brief  Initialize and Configure I2C Peripheral used to communicate with IO_Expanders.
  * @param  None
  * @retval None
  */
static void IOE_Init(void)
{
  /* Deinitialize CPAL peripheral */
  CPAL_I2C_DeInit(&IOE_DevStructure);
       
  /* Initialize CPAL peripheral */
  CPAL_I2C_Init(&IOE_DevStructure);
}
/**
 * @brief  User callback that manages the I2C device errors.
 * @note   Make sure that the define USE_SINGLE_ERROR_CALLBACK is uncommented in
 *         the cpal_conf.h file, otherwise this callback will not be functional.
 * @param  pDevInitStruct.
 * @param  DeviceError.
 * @retval None
 */
void CPAL_I2C_ERR_UserCallback(CPAL_DevTypeDef pDevInstance,
		uint32_t DeviceError) {
	i2c_Error();
	/* Update CPAL states */
	I2C1_DevStructure.CPAL_State = CPAL_STATE_READY;
	I2C1_DevStructure.wCPAL_DevError = CPAL_I2C_ERR_NONE;

	/* DeInitialize CPAL device */
	CPAL_I2C_DeInit(&I2C1_DevStructure);

	/* Initialize CPAL device with the selected parameters */
	CPAL_I2C_Init(&I2C1_DevStructure);
}
/**
  * @brief  User callback that manages the Timeout error.
  * @param  pDevInitStruct .
  * @retval None.
  */
uint32_t CPAL_TIMEOUT_UserCallback(CPAL_InitTypeDef* pDevInitStruct)
{
     /* Set default values for the state parameters */
  pDevInitStruct->CPAL_State = CPAL_STATE_READY;
  pDevInitStruct->wCPAL_DevError = CPAL_I2C_ERR_NONE ;        
  pDevInitStruct->wCPAL_Timeout  = CPAL_I2C_TIMEOUT_DEFAULT;  
  
  /* Reset the CPAL device */
  CPAL_I2C_DeInit(pDevInitStruct);  
  
  /* Initialize CPAL device with the selected parameters */
  CPAL_I2C_Init(pDevInitStruct);    
  
  return CPAL_PASS;
}
/**
 * @brief  User callback that manages the Timeout error.
 * @param  pDevInitStruct .
 * @retval None.
 */
uint32_t CPAL_TIMEOUT_UserCallback(CPAL_InitTypeDef* pDevInitStruct) {
	i2c_Timeout();
	/* Update CPAL states */
	pDevInitStruct->CPAL_State = CPAL_STATE_READY;
	pDevInitStruct->wCPAL_DevError = CPAL_I2C_ERR_NONE;
	pDevInitStruct->wCPAL_Timeout = CPAL_I2C_TIMEOUT_DEFAULT;

	/* DeInitialize CPAL device */
	CPAL_I2C_DeInit(pDevInitStruct);

	/* Initialize CPAL device with the selected parameters */
	CPAL_I2C_Init(pDevInitStruct);

	return CPAL_PASS;
}
/**
  * @brief  User callback that manages the I2C device errors.
  * @note   Make sure that the define USE_SINGLE_ERROR_CALLBACK is uncommented in
  *         the cpal_conf.h file, otherwise this callback will not be functional.
  * @param  pDevInitStruct. 
  * @param  DeviceError.
  * @retval None
  */ 
void CPAL_I2C_ERR_UserCallback(CPAL_DevTypeDef pDevInstance, uint32_t DeviceError)
{
    if (pDevInstance == CPAL_I2C1)
  {
    /* Set default values for the state parameters */
    I2C1_DevStructure.CPAL_State = CPAL_STATE_READY;
    I2C1_DevStructure.wCPAL_DevError = CPAL_I2C_ERR_NONE ;  
  
    /* Reset the CPAL device */
    CPAL_I2C_DeInit(&I2C1_DevStructure);  
  
    /* Initialize CPAL device with the selected parameters */
    CPAL_I2C_Init(&I2C1_DevStructure);    
  }
    
  return;
}
/**
  * @brief  User callback that manages the Timeout error
  * @param  pDevInitStruct
  * @retval None.
  */
uint32_t CPAL_TIMEOUT_UserCallback(CPAL_InitTypeDef* pDevInitStruct)
{
  /* Update LCD Display */
  LCD_SetBackColor(Red);
  LCD_SetTextColor(White); 
  LCD_DisplayStringLine(Line8, MESSAGE12);
  
  /* Update CPAL_State, wCPAL_DevError and wCPAL_Timeout */ 
  pDevInitStruct->CPAL_State = CPAL_STATE_READY;
  pDevInitStruct->wCPAL_DevError = CPAL_I2C_ERR_NONE ;
  pDevInitStruct->wCPAL_Timeout  = CPAL_I2C_TIMEOUT_DEFAULT; 
  
  /* DeInitialize CPAL device */
  CPAL_I2C_DeInit(pDevInitStruct);  
  
  /* Initialize CPAL device with the selected parameters */
  CPAL_I2C_Init(pDevInitStruct);
  
  return CPAL_PASS;
}
/**
  * @brief  User callback that manages the Timeout error.
  * @param  pDevInitStruct .
  * @retval None.
  */
uint32_t CPAL_TIMEOUT_UserCallback(CPAL_InitTypeDef* pDevInitStruct)
{
  I2C_DevStructure.CPAL_State = CPAL_STATE_READY;
  I2C_DevStructure.wCPAL_DevError = CPAL_I2C_ERR_NONE ;        /* No Device Error */
  I2C_DevStructure.wCPAL_Timeout  = CPAL_I2C_TIMEOUT_DEFAULT;  /* Stop timeout countdown */ 
  
  /* DeInitialize CPAL device */
  CPAL_I2C_DeInit(&I2C_DevStructure);  
  
  /* Initialize CPAL device with the selected parameters */
  CPAL_I2C_Init(pDevInitStruct);    
  
  /* Switch the LCD write color */
  Switch_ErrorColor();
  
  LCD_DisplayStringLine(Line9, (uint8_t*)"  Timeout Recovered ");
  
  ActionState = ACTION_NONE; 
  
  return CPAL_PASS;  
}
Example #10
0
/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
int main(void)
{
  /*!< At this stage the microcontroller clock setting is already configured, 
       this is done through SystemInit() function which is called from startup
       file (startup_stm32f0xx.s) before to branch to application main.
       To reconfigure the default setting of SystemInit() function, refer to
       system_stm32f0xx.c file
  */
    
  /* Configure Clocks */
  RCC_Config();
  
  /* Initialize LEDs, Key Button and LCD available on
  STM320518-EVAL board *****************************************************/
  STM_EVAL_LEDInit(LED1);
  STM_EVAL_LEDInit(LED2);
  STM_EVAL_LEDInit(LED3);
  STM_EVAL_LEDInit(LED4);
  
  /* Initialize the LCD */
  STM320518_LCD_Init();
  
  /* Display message on  LCD ***********************************************/
  /* Clear the LCD */ 
  LCD_Clear(White);  
  /* Set the LCD Back Color */
  LCD_SetBackColor(Blue);
  /* Set the LCD Text Color */
  LCD_SetTextColor(Yellow);
  LCD_DisplayStringLine(Line0, MESSAGE1);
  LCD_DisplayStringLine(Line1, MESSAGE2);  
  /* Set the LCD Back Color */
  LCD_SetBackColor(White);
  /* Set the LCD Text Color */
  LCD_SetTextColor(Blue);
  
  /* Configure the Push buttons in Polling mode */
  STM_EVAL_PBInit(BUTTON_KEY, Mode_GPIO);
  
  /* if STM32 device is set as Master */
#ifdef I2C_MASTER     

  /* Configure and enable the systick timer to generate an interrupt each 1 ms */
  SysTick_Config((SystemCoreClock / 1000));
   
  /* Deinitialize I2Cx Device */ 
  CPAL_I2C_DeInit(&MASTERSTRUCTURE); 
  
  /* Initialize CPAL I2C structure parameters values */
  CPAL_I2C_StructInit(&MASTERSTRUCTURE);
  
#ifdef CPAL_I2C_DMA_PROGMODEL
  MASTERSTRUCTURE.wCPAL_Options =  CPAL_OPT_NO_MEM_ADDR | CPAL_OPT_DMATX_TCIT;
  MASTERSTRUCTURE.CPAL_ProgModel = CPAL_PROGMODEL_DMA;
#elif defined (CPAL_I2C_IT_PROGMODEL)
  MASTERSTRUCTURE.wCPAL_Options =  CPAL_OPT_NO_MEM_ADDR;
  MASTERSTRUCTURE.CPAL_ProgModel = CPAL_PROGMODEL_INTERRUPT;
#else
 #error "Please select one of the programming model (in main.h)"
#endif
  
  /* Set I2C Speed */
  MASTERSTRUCTURE.pCPAL_I2C_Struct->I2C_Timing = MASTER_I2C_TIMING;
  
  /* Select Master Mode */
  MASTERSTRUCTURE.CPAL_Mode = CPAL_MODE_MASTER; 
  
  /* Initialize I2Cx Device*/
  CPAL_I2C_Init(&MASTERSTRUCTURE); 
  
  /* Infinite loop */
  while(1)
  {   
    /* Initialize Transfer parameters */
    MASTERSTRUCTURE.pCPAL_TransferTx = &sTxStructure;    
    sTxStructure.wNumData = BufferSize;
    sTxStructure.pbBuffer = (uint8_t*)BufferTX;
    sTxStructure.wAddr1 = OWNADDRESS;
    
    /* Update LCD Display */
    LCD_SetBackColor(White);
    LCD_SetTextColor(Blue);    
    LCD_DisplayStringLine(Line8, MEASSAGE_EMPTY);
    LCD_DisplayStringLine(Line5, MESSAGE4);
    LCD_DisplayStringLine(Line6, MESSAGE5);
    
    /* wait until Key button is pushed */
    while(STM_EVAL_PBGetState(BUTTON_KEY));
    
    /* Update LCD Display */
    LCD_DisplayStringLine(Line5, MEASSAGE_EMPTY);
    LCD_DisplayStringLine(Line6, MEASSAGE_EMPTY);
    
    /* Write operation */
    CPAL_I2C_Write(&MASTERSTRUCTURE);
    
    /* Wait until communication finishes */
    while ((MASTERSTRUCTURE.CPAL_State != CPAL_STATE_READY) && (MASTERSTRUCTURE.CPAL_State != CPAL_STATE_ERROR));
    
    if (TransferStatus == PASSED)
    {
      /* Update LCD Display */
      LCD_SetBackColor(Red);
      LCD_SetTextColor(White);    
      LCD_DisplayStringLine(Line8, MESSAGE6);      
    }
    else
    {
      TransferStatus = PASSED;
    }
    
    Delay(1000);
  }
#endif /* I2C_MASTER */
  
  /* if STM32 device is set as Slave */  
#ifdef I2C_SLAVE    
  
  /* GPIOA Periph clock enable */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
  
  /* Output System Clock on MCO pin (PA.08) */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
  
  RCC_MCOConfig(RCC_MCOSource_SYSCLK);
  
  /* Deinitialize I2Cx Device */ 
  CPAL_I2C_DeInit(&SLAVESTRUCTURE); 
  
  /* Initialize CPAL I2C structure parameters values */
  CPAL_I2C_StructInit(&SLAVESTRUCTURE);
  
#ifdef CPAL_I2C_DMA_PROGMODEL
  SLAVESTRUCTURE.wCPAL_Options = CPAL_OPT_I2C_NACK_ADD | CPAL_OPT_I2C_WAKEUP_STOP | CPAL_OPT_DMARX_TCIT;
  SLAVESTRUCTURE.CPAL_ProgModel = CPAL_PROGMODEL_DMA;
#elif defined (CPAL_I2C_IT_PROGMODEL)
  SLAVESTRUCTURE.wCPAL_Options =  CPAL_OPT_I2C_NACK_ADD | CPAL_OPT_I2C_WAKEUP_STOP;
  SLAVESTRUCTURE.CPAL_ProgModel = CPAL_PROGMODEL_INTERRUPT;
#else
 #error "Please select one of the programming model (in main.h)"
#endif
  
  /* Configure Own address 1 */
  SLAVESTRUCTURE.pCPAL_I2C_Struct->I2C_OwnAddress1 = OWNADDRESS;
  
  /* Set I2C Speed */
  SLAVESTRUCTURE.pCPAL_I2C_Struct->I2C_Timing = SLAVE_I2C_TIMING;
  
  /* Select Slave Mode */ 
  SLAVESTRUCTURE.CPAL_Mode = CPAL_MODE_SLAVE; 
  
  /* Initialize I2Cx Device*/
  CPAL_I2C_Init(&SLAVESTRUCTURE);    
  
  /* Infinite loop */
  while(1)
  {     
    /* Reset BufferRX value */
    Reset_bBuffer(BufferRX, (uint16_t)BufferSize);
    
    /* Initialize Transfer parameters */
    SLAVESTRUCTURE.pCPAL_TransferRx = &sRxStructure;    
    sRxStructure.wNumData = BufferSize;         
    sRxStructure.pbBuffer = (uint8_t*)BufferRX;
    
    /* Update LCD Display */
    LCD_SetBackColor(White);
    LCD_SetTextColor(Blue);
    LCD_DisplayStringLine(Line8, MEASSAGE_EMPTY);
    LCD_DisplayStringLine(Line9, MEASSAGE_EMPTY);
    LCD_DisplayStringLine(Line5, MESSAGE7);
    
    Delay(1000);
    
    /* Update LCD Display */
    LCD_DisplayStringLine(Line5, MEASSAGE_EMPTY);
    LCD_DisplayStringLine(Line6, MESSAGE8);
    
    /* Read operation */
    CPAL_I2C_Read(&SLAVESTRUCTURE);  
    
    /* Enter Stop Mode and wait for interrupt to wake up */
    PWR_EnterSTOPMode(PWR_Regulator_LowPower, PWR_STOPEntry_WFI);
    
    /* Wait until communication finishes */
    while ((SLAVESTRUCTURE.CPAL_State != CPAL_STATE_READY) && (SLAVESTRUCTURE.CPAL_State != CPAL_STATE_ERROR));
    
    /* Configure SystemClock*/
    RestoreConfiguration();
    
    /* Configure and enable the systick timer to generate an interrupt each 1 ms */
    SysTick_Config((SystemCoreClock / 1000));
  
    /* Update LCD Display */
    LCD_DisplayStringLine(Line6, MEASSAGE_EMPTY);    
    LCD_SetBackColor(Red);
    LCD_SetTextColor(White);     
    LCD_DisplayStringLine(Line8, MESSAGE9);
    
    /* If are received correctly */
    if (Compare_bBuffer((uint8_t*)BufferTX, BufferRX, BufferSize) == PASSED )
    {          
      /* Update LCD Display */
      LCD_DisplayStringLine(Line9, MESSAGE6);      
    }
    else
    {          
      /* Update LCD Display */
      LCD_DisplayStringLine(Line9, MESSAGE10);
    }
    
    Delay(1500);
  }  
#endif /* I2C_SLAVE */
}
/**
  * @brief  DeInitialize peripherals used by the I2C EEPROM driver.
  * @param  sEEInitStruct : Pointer to sEE Device structure
  * @retval None
  */
void sEE_DeInit(sEE_InitTypeDef * sEEInitStruct)
{
	/* Deinitialize CPAL peripheral */
	CPAL_I2C_DeInit(sEEInitStruct->sEE_CPALStructure);
}
/**
  * @brief  Deinitialize the LM75_I2C.
  * @param  None
  * @retval None
  */
void LM75_DeInit(void)
{
    /* Initialize CPAL peripheral */
  CPAL_I2C_DeInit(&LM75_DevStructure);
}
void MPU9150_DeInit(void)
{
    /* Initialize CPAL peripheral */
  CPAL_I2C_DeInit(&MPU9150_DevStructure);
}