/**
  * @brief  Writes more than one byte to the EEPROM with a single WRITE cycle.
  * @note   The number of byte can't exceed the EEPROM page size.
  * @param  sEEInitStruct : Pointer to sEE Device structure
  * @param  pBuffer : pointer to the buffer containing the data to be written to 
  *         the EEPROM.
  * @param  WriteAddr : EEPROM's internal address to write to.
  * @param  NumByteToWrite : pointer to the variable holding number of bytes to 
  *         written to the EEPROM. 
  * 
  *        @note The variable pointed by NumByteToWrite is reset to 0 when all the 
  *              data are read from the EEPROM. Application should monitor this 
  *              variable in order know when the transfer is complete.
  * 
  * @note When number of data to be written is higher than 1, this function just 
  *       configure the communication and enable the DMA channel to transfer data.
  *       Meanwhile, the user application may perform other tasks.
  *       When number of data to be written is 1, then the DMA is not used.
  * 
  * @retval None
  */
static uint32_t sEE_WritePage(sEE_InitTypeDef * sEEInitStruct,
			      uint8_t * pBuffer, uint16_t WriteAddr,
			      uint32_t NumByteToWrite)
{
	sEEInitStruct->sEE_CPALStructure->wCPAL_Options = 0;

	/* Enable 16Bit memory register option on CPAL */
	if (sEEInitStruct->sEEMemoryAddrMode & sEE_OPT_16BIT_REG) {
		sEEInitStruct->sEE_CPALStructure->wCPAL_Options =
		    CPAL_OPT_16BIT_REG;
	}

	sEEInitStruct->sEE_CPALStructure->pCPAL_TransferTx =
	    sEE_TXTransfer[sEEInitStruct->sEE_CPALStructure->CPAL_Dev];

	/* Configure transfer parameters */
	sEEInitStruct->sEE_CPALStructure->pCPAL_TransferTx->wNumData =
	    (uint32_t) (NumByteToWrite);
	sEEInitStruct->sEE_CPALStructure->pCPAL_TransferTx->pbBuffer = pBuffer;
	sEEInitStruct->sEE_CPALStructure->pCPAL_TransferTx->wAddr1 =
	    (uint32_t) ((uint8_t) sEEInitStruct->sEEAddress);
	sEEInitStruct->sEE_CPALStructure->pCPAL_TransferTx->wAddr2 =
	    (uint32_t) ((uint16_t) WriteAddr);

	/* Write Operation */
	return CPAL_I2C_Write(sEEInitStruct->sEE_CPALStructure);

}
Example #2
0
void I2C_Lib_MasterRW(I2C_Lib_Op_Type op,uint8_t slave_addr,uint16_t length,uint8_t* data)
{
    static CPAL_TransferTypeDef CPAL_Transfer; //Must be static because I2C transfer is async

    I2C_Lib_WaitForRWDone();
    
    CPAL_Transfer.wNumData = length;
    CPAL_Transfer.pbBuffer = data;
    CPAL_Transfer.wAddr1 = (uint32_t)slave_addr<<1; //device address is high 7-bit
    // CPAL_Transfer.wAddr2 = (uint32_t)reg_addr;

    I2C_HOST_DEV.wCPAL_Options = CPAL_OPT_NO_MEM_ADDR;

    if(op == I2C_OP_Write){
        I2C_HOST_DEV.pCPAL_TransferTx = &CPAL_Transfer;
        if(CPAL_I2C_Write(&I2C_HOST_DEV) != CPAL_PASS){
            ERR_MSG("CPAL_I2C_Write Failed");
        }
    }
    else if(op == I2C_OP_Read){
        I2C_HOST_DEV.pCPAL_TransferRx = &CPAL_Transfer;
        if(CPAL_I2C_Read(&I2C_HOST_DEV) != CPAL_PASS){
            ERR_MSG("CPAL_I2C_Read Failed");
        }
    }

}
int stm32_i2c_write(unsigned char slave_addr,  unsigned char reg_addr, unsigned char length, unsigned char *data)
{    
  /* Disable all options */
  MPU9150_DevStructure.wCPAL_Options = 0;
  
  /* point to CPAL_TransferTypeDef structure */
  MPU9150_DevStructure.pCPAL_TransferTx = &MPU9150_TXTransfer;
  
  /* Configure transfer parameters */  
  MPU9150_DevStructure.pCPAL_TransferTx->wNumData = (uint32_t)length;
  MPU9150_DevStructure.pCPAL_TransferTx->pbBuffer = data ;
  MPU9150_DevStructure.pCPAL_TransferTx->wAddr1   = (uint32_t)slave_addr;
  MPU9150_DevStructure.pCPAL_TransferTx->wAddr2   = (uint32_t)reg_addr;
  
  /* Write Operation */
  if(CPAL_I2C_Write(&MPU9150_DevStructure) == CPAL_PASS)
  {
    while ((MPU9150_DevStructure.CPAL_State != CPAL_STATE_READY) && (MPU9150_DevStructure.CPAL_State != CPAL_STATE_ERROR) )
    { }
    
    if (MPU9150_DevStructure.CPAL_State == CPAL_STATE_ERROR)
    {
      return -1;
    }
  }
  else
  {
    return -1;
  }
  
  return 0;
}
Example #4
0
uint8_t mBusWriteBurstNoAdd(uint8_t slaveAddr, uint8_t length, uint8_t *data, uint8_t i2cNum)
{
  mBusTypeDef* mBusCurrent = getmBusPtr(i2cNum);
  CPAL_InitTypeDef* mBusStruct = mBusCurrent->CPAL_InitStruct;
  uint8_t i;
  for(i=0;i<length;i++)
  {
    mBusCurrent->bufferBurstWr[i] = data[i]; 
  }
  mBusStruct->wCPAL_Options |= CPAL_OPT_NO_MEM_ADDR;
  mBusStruct->pCPAL_TransferTx = &(mBusCurrent->mBusTx); 
  mBusStruct->pCPAL_TransferTx->wNumData = length;
  mBusStruct->pCPAL_TransferTx->pbBuffer = mBusCurrent->bufferBurstWr;
  mBusStruct->pCPAL_TransferTx->wAddr1   = (uint32_t)slaveAddr;

  if(CPAL_I2C_Write(mBusStruct) == CPAL_PASS)
  {
    while((mBusStruct->CPAL_State != CPAL_STATE_READY) && (mBusStruct->CPAL_State != CPAL_STATE_ERROR)){}
    if (mBusStruct->CPAL_State == CPAL_STATE_ERROR)
    {
      mBusStruct->CPAL_State = CPAL_STATE_READY;
      mBusStruct->wCPAL_Options &= ~CPAL_OPT_NO_MEM_ADDR;
      return ERROR;
    }
    mBusStruct->wCPAL_Options &= ~CPAL_OPT_NO_MEM_ADDR;
    return SUCCESS;
  }
  else 
  {
    mBusStruct->wCPAL_Options &= ~CPAL_OPT_NO_MEM_ADDR;
    return ERROR;
  }
}
Example #5
0
uint8_t mBusWrite(uint8_t slaveAddr, uint8_t regAddr, uint8_t data, uint8_t i2cNum)
{
  mBusTypeDef* mBusCurrent = getmBusPtr(i2cNum);
  CPAL_InitTypeDef* mBusStruct = mBusCurrent->CPAL_InitStruct;
	mBusCurrent->bufferWr[0] = data;
	mBusStruct->pCPAL_TransferTx = &(mBusCurrent->mBusTx); 
  mBusStruct->pCPAL_TransferTx->wNumData = 1;
 	mBusStruct->pCPAL_TransferTx->pbBuffer = mBusCurrent->bufferWr;
  mBusStruct->pCPAL_TransferTx->wAddr1   = (uint32_t)slaveAddr;
  mBusStruct->pCPAL_TransferTx->wAddr2   = (uint32_t)regAddr;

  if (CPAL_I2C_Write(mBusStruct) == CPAL_PASS)
  {
    while((mBusStruct->CPAL_State != CPAL_STATE_READY) && (mBusStruct->CPAL_State != CPAL_STATE_ERROR)){}
    if (mBusStruct->CPAL_State == CPAL_STATE_ERROR)
    {
      mBusStruct->CPAL_State = CPAL_STATE_READY;
      return ERROR;
    }
    return SUCCESS;
  }
  else 
  {
    return ERROR;
  }
}
Example #6
0
uint8_t mBusWriteBurstNoAdd(uint8_t slaveAddr, uint8_t length, uint8_t *data)
{
  uint8_t i;
  for(i=0;i<length;i++)
  {
    bufferBurstWr[i] = data[i]; 
  }
  mBusStruct.wCPAL_Options |= CPAL_OPT_NO_MEM_ADDR;
  mBusStruct.pCPAL_TransferTx = &mBusTx; 
  mBusStruct.pCPAL_TransferTx->wNumData = length;
  mBusStruct.pCPAL_TransferTx->pbBuffer = bufferBurstWr ;
  mBusStruct.pCPAL_TransferTx->wAddr1   = (uint32_t)slaveAddr;

  if(CPAL_I2C_Write(&mBusStruct) == CPAL_PASS)
  {
    while((mBusStruct.CPAL_State != CPAL_STATE_READY) && (mBusStruct.CPAL_State != CPAL_STATE_ERROR)){}
    if (mBusStruct.CPAL_State == CPAL_STATE_ERROR)
    {
      mBusStruct.CPAL_State = CPAL_STATE_READY;
      mBusStruct.wCPAL_Options &= ~CPAL_OPT_NO_MEM_ADDR;
      return ERROR;
    }
    mBusStruct.wCPAL_Options &= ~CPAL_OPT_NO_MEM_ADDR;
    return SUCCESS;
  }
  else 
  {
    mBusStruct.wCPAL_Options &= ~CPAL_OPT_NO_MEM_ADDR;
    return ERROR;
  }
}
Example #7
0
uint8_t mBusWriteNoAdd(uint8_t slaveAddr, uint8_t data)
{
  bufferWr[0] = data;
  mBusStruct.wCPAL_Options |= CPAL_OPT_NO_MEM_ADDR;
  mBusStruct.pCPAL_TransferTx = &mBusTx; 
  mBusStruct.pCPAL_TransferTx->wNumData = 1;
  mBusStruct.pCPAL_TransferTx->pbBuffer = bufferWr;
  mBusStruct.pCPAL_TransferTx->wAddr1   = (uint32_t)slaveAddr;

  if(CPAL_I2C_Write(&mBusStruct) == CPAL_PASS)
  {
    while((mBusStruct.CPAL_State != CPAL_STATE_READY) && (mBusStruct.CPAL_State != CPAL_STATE_ERROR)){}
    if (mBusStruct.CPAL_State == CPAL_STATE_ERROR)
    {
      mBusStruct.CPAL_State = CPAL_STATE_READY;
      mBusStruct.wCPAL_Options &= ~CPAL_OPT_NO_MEM_ADDR;
      return ERROR;
    }
    mBusStruct.wCPAL_Options &= ~CPAL_OPT_NO_MEM_ADDR;
    return SUCCESS;
  }
  else 
  {
    mBusStruct.wCPAL_Options &= ~CPAL_OPT_NO_MEM_ADDR;
    return ERROR;
  }
}
/**
  * @brief  Writes a value in a register of the device through I2C.
  * @param  DeviceAddr: The address of the IOExpander, could be : IOE_1_ADDR. 
  * @param  RegisterAddr: The target register address
  * @param  RegisterValue: The target register value to be written 
  * @retval IOE_OK: if all operations are OK. Other value if error.
  */
uint8_t I2C_WriteDeviceRegister(uint8_t DeviceAddr, uint8_t RegisterAddr, uint8_t RegisterValue)
{
  uint32_t read_verif = 0;
  
  IOE_Buffer[0] = RegisterValue;
  
  /* Disable all options */
  IOE_DevStructure.wCPAL_Options  = 0;
  
  /* point to CPAL_TransferTypeDef structure */
  IOE_DevStructure.pCPAL_TransferTx = &IOE_TXTransfer;
  
  /* Configure transfer parameters */  
  IOE_DevStructure.pCPAL_TransferTx->wNumData = 1;
  IOE_DevStructure.pCPAL_TransferTx->pbBuffer = IOE_Buffer ;
  IOE_DevStructure.pCPAL_TransferTx->wAddr1   = (uint32_t)DeviceAddr;
  IOE_DevStructure.pCPAL_TransferTx->wAddr2   = (uint32_t)RegisterAddr;
  
  /* Write Operation */
  if (CPAL_I2C_Write(&IOE_DevStructure)== CPAL_PASS)
  {
    while ((IOE_DevStructure.CPAL_State != CPAL_STATE_READY) && (IOE_DevStructure.CPAL_State != CPAL_STATE_ERROR) )
    { }  
  }
  
#ifdef VERIFY_WRITTENDATA
  /* Verify (if needed) that the loaded data is correct  */
  
  /* Read the just written register*/
  read_verif = I2C_ReadDeviceRegister(DeviceAddr, RegisterAddr);
  /* Load the register and verify its value  */
  if (read_verif != RegisterValue)
  {
    /* Control data wrongly transferred */
    read_verif = IOE_FAILURE;
  }
  else
  {
    /* Control data correctly transferred */
    read_verif = 0;
  }
#endif
  
  /* Return the verifying value: 0 (Passed) or 1 (Failed) */
  return read_verif;
}
Example #9
0
// Write a byte stream to 7-bit slave address 'slave_addr' beginning at 16-bit 
// register address 'reg_addr'. Non-blocking unless previous transaction in 
// ongoing.  Address is high byte first first, data is native.
uint8_t mBusMasterWriteBurst(uint8_t slaveAddr, uint16_t regAddr, uint8_t length, uint8_t *data, uint8_t i2cNum)
{
  mBusTypeDef* mBus = getmBusPtr(i2cNum);
  CPAL_InitTypeDef* mBusStruct = mBus->CPAL_InitStruct;

  // Block If Device is Busy (a previous transaction is ongoing)
  // while (((mBusStruct->CPAL_State & CPAL_STATE_BUSY) != 0)
  //         || (mBusStruct->CPAL_State == CPAL_STATE_READY_TX)
  //         || (mBusStruct->CPAL_State == CPAL_STATE_READY_RX)) {};


  // while(mBus->CPAL_InitStruct->CPAL_State != CPAL_STATE_READY
  //    && mBus->CPAL_InitStruct->CPAL_State != CPAL_STATE_DISABLED
  //    && mBus->CPAL_InitStruct->CPAL_State != CPAL_STATE_ERROR) {};
  // DelayMicroseconds(1);

  // if(mBus->CPAL_InitStruct->CPAL_State == CPAL_STATE_DISABLED
  // || mBus->CPAL_InitStruct->CPAL_State == CPAL_STATE_ERROR) {
  //   mRedON;
  //   mBus->CPAL_InitStruct->CPAL_State = CPAL_STATE_READY;
  //   mBusInit(i2cNum);
  // }

  memcpy(mBus->bufferBurstWr, data, length);
  mBusStruct->pCPAL_TransferTx = &(mBus->mBusTx); 
  mBusStruct->pCPAL_TransferTx->wNumData = length;
  mBusStruct->pCPAL_TransferTx->pbBuffer = mBus->bufferBurstWr;
  mBusStruct->pCPAL_TransferTx->wAddr1   = (uint32_t)slaveAddr;
  mBusStruct->pCPAL_TransferTx->wAddr2   = (uint32_t)regAddr;
  mBusStruct->wCPAL_Options |= CPAL_OPT_16BIT_REG;

  if(CPAL_I2C_Write(mBusStruct) == CPAL_PASS) {
    return SUCCESS;
  }
  else {
    //WarnReport(ERROR_MBUS_MASTER_WRITE_BURST, "CPAL_I2C_Write failed");

    WarnReport(ERROR_MBUS_MASTER_WRITE_BURST, "CPAL_I2C_Write fail, Mode=%u,State=%u,Error=%u",
      mBus->CPAL_InitStruct->CPAL_Mode,
      mBus->CPAL_InitStruct->CPAL_State,
      mBus->CPAL_InitStruct->wCPAL_DevError);

    return ERROR;
  }
}
Example #10
0
uint8_t mBusWriteBurstNB(uint8_t slaveAddr, uint8_t regAddr, uint8_t length, uint8_t *data)
{
  uint8_t i;
  for(i=0;i<length;i++)
  {
    bufferBurstWr[i] = data[i]; 
  }
  mBusStruct.pCPAL_TransferTx = &mBusTx; 
  mBusStruct.pCPAL_TransferTx->wNumData = length;
  mBusStruct.pCPAL_TransferTx->pbBuffer = bufferBurstWr ;
  mBusStruct.pCPAL_TransferTx->wAddr1   = (uint32_t)slaveAddr;
  mBusStruct.pCPAL_TransferTx->wAddr2   = (uint32_t)regAddr;

  if(CPAL_I2C_Write(&mBusStruct) == CPAL_PASS)
  {
    return SUCCESS;
  }
  else 
  {
    return ERROR;
  }
}
Example #11
0
uint8_t mBusWriteBurstNB(uint8_t slaveAddr, uint8_t regAddr, uint8_t length, uint8_t *data, uint8_t i2cNum)
{
  mBusTypeDef* mBusCurrent = getmBusPtr(i2cNum);
  CPAL_InitTypeDef* mBusStruct = mBusCurrent->CPAL_InitStruct;
  uint8_t i;
  for(i=0;i<length;i++)
  {
    mBusCurrent->bufferBurstWr[i] = data[i]; 
  }
  mBusStruct->pCPAL_TransferTx = &(mBusCurrent->mBusTx); 
  mBusStruct->pCPAL_TransferTx->wNumData = length;
  mBusStruct->pCPAL_TransferTx->pbBuffer = mBusCurrent->bufferBurstWr;
  mBusStruct->pCPAL_TransferTx->wAddr1   = (uint32_t)slaveAddr;
  mBusStruct->pCPAL_TransferTx->wAddr2   = (uint32_t)regAddr;

  if(CPAL_I2C_Write(mBusStruct) == CPAL_PASS)
  {
    return SUCCESS;
  }
  else 
  {
    return ERROR;
  }
}
Example #12
0
uint8_t mBusWrite(uint8_t slaveAddr, uint8_t regAddr, uint8_t data)
{
	bufferWr[0] = data;
	mBusStruct.pCPAL_TransferTx = &mBusTx; 
  mBusStruct.pCPAL_TransferTx->wNumData = 1;
 	mBusStruct.pCPAL_TransferTx->pbBuffer = bufferWr;
  mBusStruct.pCPAL_TransferTx->wAddr1   = (uint32_t)slaveAddr;
  mBusStruct.pCPAL_TransferTx->wAddr2   = (uint32_t)regAddr;

  if(CPAL_I2C_Write(&mBusStruct) == CPAL_PASS)
  {
    while((mBusStruct.CPAL_State != CPAL_STATE_READY) && (mBusStruct.CPAL_State != CPAL_STATE_ERROR)){}
    if (mBusStruct.CPAL_State == CPAL_STATE_ERROR)
    {
      mBusStruct.CPAL_State = CPAL_STATE_READY;
      return ERROR;
    }
    return SUCCESS;
  }
  else 
  {
    return ERROR;
  }
}
Example #13
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 */
}
Example #14
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
  */
  
  /* Initialize LEDs and LCD available on STM320518-EVAL board ****************/
  STM_EVAL_LEDInit(LED1);
  STM_EVAL_LEDInit(LED2);
  STM_EVAL_LEDInit(LED3);
  STM_EVAL_LEDInit(LED4);
    
  /* Initialize TIM6 */
  TIM6_Config();
  
  /* 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);
  LCD_DisplayStringLine(Line3, MESSAGE3);
  
  /* Set the LCD Back Color */
  LCD_SetBackColor(White);
  /* Set the LCD Text Color */
  LCD_SetTextColor(Blue);
  
  /* Configure the Push buttons in interrupt mode *****************************/
  STM_EVAL_PBInit(BUTTON_KEY, Mode_EXTI);
  STM_EVAL_PBInit(BUTTON_TAMPER, Mode_EXTI);
  
  /* Start CPAL communication configuration ***********************************/
  /* Initialize local Reception structures */
  sRxStructure.wNumData = BufferSize;       /* Maximum Number of data to be received */
  sRxStructure.pbBuffer = tRxBuffer;        /* Common Rx buffer for all received data */
  sRxStructure.wAddr1 = 0;                  /* Not needed */
  sRxStructure.wAddr2 = 0;                  /* Not needed */
  
  /* Initialize local Transmission structures */
  sTxStructure.wNumData = BufferSize;       /* Maximum Number of data to be received */
  sTxStructure.pbBuffer = (uint8_t*)tStateSignal;     /* Common Rx buffer for all received data */
  sTxStructure.wAddr1 = OWN_ADDRESS;        /* The own board address */
  sTxStructure.wAddr2 = 0;                  /* Not needed */
  
  /* Set SYSCLK as I2C clock source */
  RCC_I2CCLKConfig(RCC_I2C1CLK_SYSCLK);
  
  /* Configure the device structure */
  CPAL_I2C_StructInit(&I2C_DevStructure);      /* Set all fields to default values */
  I2C_DevStructure.CPAL_Mode = CPAL_MODE_SLAVE;
#ifdef CPAL_I2C_DMA_PROGMODEL
  I2C_DevStructure.wCPAL_Options =  CPAL_OPT_NO_MEM_ADDR | CPAL_OPT_DMATX_TCIT | CPAL_OPT_DMARX_TCIT;
  I2C_DevStructure.CPAL_ProgModel = CPAL_PROGMODEL_DMA;
#elif defined (CPAL_I2C_IT_PROGMODEL)
  I2C_DevStructure.wCPAL_Options =  CPAL_OPT_NO_MEM_ADDR;
  I2C_DevStructure.CPAL_ProgModel = CPAL_PROGMODEL_INTERRUPT;
#else
 #error "Please select one of the programming model (in stm32f0xx_i2c_cpal_conf.h)"
#endif
  I2C_DevStructure.pCPAL_I2C_Struct->I2C_Timing = I2C_TIMING;
  I2C_DevStructure.pCPAL_I2C_Struct->I2C_OwnAddress1 = OWN_ADDRESS;
  I2C_DevStructure.pCPAL_TransferRx = &sRxStructure;
  I2C_DevStructure.pCPAL_TransferTx = &sTxStructure;
  
  /* Initialize CPAL device with the selected parameters */
  CPAL_I2C_Init(&I2C_DevStructure);    
  
  /* Infinite loop */
  while (1)
  {
    /* Write operations ------------------------------------------------------*/
    /* Check if any action has been triggered by push buttons */
    if ((ActionState != ACTION_PENDING) && (ActionState != ACTION_NONE))
    {
      /* Check if the current CPAL device state allows write operation */
      if ((I2C_DevStructure.CPAL_State == CPAL_STATE_READY) || \
        (I2C_DevStructure.CPAL_State == CPAL_STATE_BUSY_RX) ||\
          (I2C_DevStructure.CPAL_State == CPAL_STATE_DISABLED))
      {        
        /* Initialize local Transmission structures */
        sTxStructure.wNumData = BufferSize;   /* Maximum Number of data to be received */
        sTxStructure.wAddr1 = OWN_ADDRESS;    /* The own board address */
        sTxStructure.wAddr2 = 0;              /* Not needed */        
        
        switch (ActionState)
        {
          
        case BUTTON_KEY: 
          sTxStructure.pbBuffer = (uint8_t*)tSignal1; 
          Divider = 1;
          break;
          
        case BUTTON_TAMPER:
          sTxStructure.pbBuffer = (uint8_t*)tSignal2; 
          Divider = 2;
          break;
          
        case ACTION_PERIODIC:
          sTxStructure.pbBuffer = (uint8_t*)tStateSignal; 
          break;    
          
        default:
          sTxStructure.pbBuffer = (uint8_t*)tSignal1; 
          break;            
        } 
        
        /* Configure the device mode to master */
        I2C_DevStructure.CPAL_Mode = CPAL_MODE_MASTER;
        /* Force the CPAL state to ready (in case a read operation has been initiated) */
        I2C_DevStructure.CPAL_State = CPAL_STATE_READY;
                
        /* Prevent other actions to be performed while the current is not finished */
        ActionState = ACTION_PENDING;
        TransmitMode = STATE_ON;
        
        /* Configure a Timer to generate periodic interrupt: used to send state signal */
        TIM17_Config(PeriodicValue/Divider);   
        
        /* Start writing data in master mode */
        if (CPAL_I2C_Write(&I2C_DevStructure) == CPAL_PASS)
        {
         }
      }      
    }
    
    
    /* Read Operations -------------------------------------------------------*/
    if (((I2C_DevStructure.CPAL_State == CPAL_STATE_READY) || \
      (I2C_DevStructure.CPAL_State == CPAL_STATE_DISABLED)) && \
        (TransmitMode == STATE_OFF))
    {                  
      /* Initialize local Reception structures */
      sRxStructure.wNumData = BufferSize;       /* Maximum Number of data to be received */
      sRxStructure.pbBuffer = tRxBuffer;        /* Common Rx buffer for all received data */
      
      /* Reconfigure device for slave receiver mode */
      I2C_DevStructure.CPAL_Mode = CPAL_MODE_SLAVE;
      I2C_DevStructure.CPAL_State = CPAL_STATE_READY;
      
      /* Start waiting for data to be received in slave mode */
      if (CPAL_I2C_Read(&I2C_DevStructure) == CPAL_PASS)
      {
        LCD_DisplayStringLine(Line9, MEASSAGE_EMPTY); 
      }
    }   
  }
}
Example #15
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_stm32xxx_xx.s) before to branch to application main.
       To reconfigure the default setting of SystemInit() function, refer to
       system_stm32xxx.c file
  */
  
  /* Initialize LEDs, Key Button, LCD and COM port(USART) available on
  STM3210X-EVAL board ******************************************************/
  STM_EVAL_LEDInit(LED1);
  STM_EVAL_LEDInit(LED2);
  STM_EVAL_LEDInit(LED3);
  STM_EVAL_LEDInit(LED4);
    
  /* Initialize TIM6 */
  TIM6_Config();
  
  /* Initialize the LCD */
#ifdef USE_STM322xG_EVAL   
  STM322xG_LCD_Init();
#elif defined USE_STM324xG_EVAL   
  STM324xG_LCD_Init();
#elif defined USE_STM3210C_EVAL   
  STM3210C_LCD_Init();
#elif defined USE_STM32100E_EVAL
  STM32100E_LCD_Init();  
#elif defined USE_STM32L152_EVAL
  STM32L152_LCD_Init();
#elif defined USE_STM32L152D_EVAL
  STM32L152D_LCD_Init();
#endif
  
  /* Display message on STM3210X-EVAL 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);
  LCD_DisplayStringLine(Line5, MESSAGE3);
  
  /* Configure the Push buttons in interrupt mode *****************************/
#if defined (USE_STM32100E_EVAL) || defined(USE_STM3210C_EVAL)  || defined(USE_STM322xG_EVAL ) || defined (USE_STM324xG_EVAL)
  STM_EVAL_PBInit(BUTTON_KEY, Mode_EXTI);
  STM_EVAL_PBInit(BUTTON_TAMPER, Mode_EXTI);
#elif defined (USE_STM32L152_EVAL) || defined (USE_STM32L152D_EVAL)
  STM_EVAL_PBInit(BUTTON_LEFT, Mode_EXTI);
  STM_EVAL_PBInit(BUTTON_RIGHT, Mode_EXTI);
#endif /* USE_STM32100E_EVAL || USE_STM3210C_EVAL || USE_STM322xG_EVAL || USE_STM324xG_EVAL */
  
  /* Start CPAL communication configuration ***********************************/
  /* Initialize local Reception structures */
  sRxStructure.wNumData = BufferSize;       /* Maximum Number of data to be received */
  sRxStructure.pbBuffer = tRxBuffer;        /* Common Rx buffer for all received data */
  sRxStructure.wAddr1 = OWN_ADDRESS;        /* The own board address */
  sRxStructure.wAddr2 = 0;                  /* Not needed */
  
  /* Initialize local Transmission structures */
  sTxStructure.wNumData = BufferSize;       /* Maximum Number of data to be received */
  sTxStructure.pbBuffer = (uint8_t*)tStateSignal;     /* Common Rx buffer for all received data */
  sTxStructure.wAddr1 = OWN_ADDRESS;        /* The own board address */
  sTxStructure.wAddr2 = 0;                  /* Not needed */
  
  /* Configure the device structure */
  CPAL_I2C_StructInit(&I2C_DevStructure);      /* Set all fields to default values */
  I2C_DevStructure.CPAL_Mode = CPAL_MODE_SLAVE;
#ifdef CPAL_I2C_DMA_PROGMODEL
  I2C_DevStructure.wCPAL_Options =  CPAL_OPT_NO_MEM_ADDR | CPAL_OPT_I2C_NACK_ADD;
  I2C_DevStructure.CPAL_ProgModel = CPAL_PROGMODEL_DMA;
#elif defined (CPAL_I2C_IT_PROGMODEL)
  I2C_DevStructure.wCPAL_Options =  CPAL_OPT_NO_MEM_ADDR | CPAL_OPT_I2C_NACK_ADD;
  I2C_DevStructure.CPAL_ProgModel = CPAL_PROGMODEL_INTERRUPT;
#else
 #error "Please select one of the programming model (in main.h)"
#endif
  I2C_DevStructure.pCPAL_I2C_Struct->I2C_ClockSpeed = I2C_SPEED;
  I2C_DevStructure.pCPAL_I2C_Struct->I2C_OwnAddress1 = OWN_ADDRESS;
  I2C_DevStructure.pCPAL_TransferRx = &sRxStructure;
  I2C_DevStructure.pCPAL_TransferTx = &sTxStructure;
  
  /* Initialize CPAL device with the selected parameters */
  CPAL_I2C_Init(&I2C_DevStructure);    
  
  /* Infinite loop */
  while (1)
  {
    /* Write operations ------------------------------------------------------*/
    /* Check if any action has been triggered by push buttons */
    if ((ActionState != ACTION_PENDING) && (ActionState != ACTION_NONE))
    {
      /* Check if the current CPAL device state allows write operation */
      if (((DeviceMode == SLAVE) && (LastMode == SLAVE))||\
         (((I2C_DevStructure.CPAL_State == CPAL_STATE_READY) ||\
           (I2C_DevStructure.CPAL_State == CPAL_STATE_DISABLED)) && (DeviceMode == MASTER)))
      {  
        if (LastMode == SLAVE)
        {         
          /* Set the LCD Back Color */
          LCD_SetBackColor(Red);
          /* Set the LCD Text Color */
          LCD_SetTextColor(White);
          LCD_DisplayStringLine(Line3, (uint8_t*)" MASTER MODE ACTIVE ");
          /* Set the LCD Back Color */
          LCD_SetBackColor(White);
          /* Set the LCD Text Color */
          LCD_SetTextColor(Blue);
          
          /* Disable CPAL_OPT_I2C_NACK_ADD option when switch to Master mode */
          I2C_DevStructure.wCPAL_Options &= (~CPAL_OPT_I2C_NACK_ADD);
        }
        
        LastMode = MASTER;        
        
        /* Initialize local Reception structures */
        sRxStructure.wNumData = BufferSize;
        
        /* Initialize local Transmission structures */
        sTxStructure.wNumData = BufferSize;
        
        switch (ActionState)
        {
          
        case BUTTON_KEY: 
          TransmitMode = STATE_ON;
          sTxStructure.pbBuffer = (uint8_t*)tSignal; 
          Divider = 2;
          break;
          
        case BUTTON_TAMPER:
          TransmitMode = STATE_OFF;          
          sRxStructure.pbBuffer = tRxBuffer;        
          Divider = 4;
          break;
          
        case ACTION_PERIODIC:
          if(TransmitMode == STATE_ON)
          {
            sTxStructure.pbBuffer = (uint8_t*)tStateSignal; 
          }
          break;    
          
        default:
          break;            
        } 
        
        /* Configure the device mode to master */
        I2C_DevStructure.CPAL_Mode = CPAL_MODE_MASTER;
        /* Force the CPAL state to ready (in case a read operation has been initiated) */
        I2C_DevStructure.CPAL_State = CPAL_STATE_READY;
        
        /* Prevent other actions to be performed while the current is not finished */
        ActionState = ACTION_PENDING;
        DeviceMode = MASTER;
        
        /* Configure a Timer to generate periodic interrupt: used to send state signal */
        TIM7_Config(PeriodicValue/Divider);   
        
        if(TransmitMode == STATE_ON)
        {
          /* Start writing data in master mode */
          if (CPAL_I2C_Write(&I2C_DevStructure) == CPAL_PASS)
          {
          }
        }
        else
        {
          /* Start reading data in master mode */
          if (CPAL_I2C_Read(&I2C_DevStructure) == CPAL_PASS)
          {
          }
        }
        
      }      
    }
    
    
    /* Read Operations -------------------------------------------------------*/
    if (((I2C_DevStructure.CPAL_State == CPAL_STATE_READY) || \
         (I2C_DevStructure.CPAL_State == CPAL_STATE_DISABLED)) && \
         (DeviceMode == SLAVE))
    {                  
      /* Reconfigure device for slave receiver mode */
      I2C_DevStructure.CPAL_Mode = CPAL_MODE_SLAVE;
      I2C_DevStructure.CPAL_State = CPAL_STATE_READY;
      
      if (LastMode == SLAVE)
      {        
        /* Set the LCD Back Color */
        LCD_SetBackColor(Red);
        /* Set the LCD Text Color */
        LCD_SetTextColor(White);
        LCD_DisplayStringLine(Line3, (uint8_t*)"  SLAVE MODE ACTIVE ");
        /* Set the LCD Back Color */
        LCD_SetBackColor(White);
        /* Set the LCD Text Color */
        LCD_SetTextColor(Blue);
      }
      
      /* Start waiting for data to be received in slave mode */
      if (CPAL_I2C_Listen(&I2C_DevStructure) == CPAL_PASS)
      {
        LCD_DisplayStringLine(Line9, MEASSAGE_EMPTY); 
      }
    }   
  }
}