void i2c_common(mal_i2c_s *handle) {
	// Check if transmission is complete
	if (I2C_GetITStatus(handle->stm_handle, I2C_IT_TC) == SET) {
		// Transmit complete, stop transfer, clear interrupt with stop
		I2C_GenerateSTOP(handle->stm_handle, ENABLE);
		// Make sure it was expected
		if (I2C_STATE_WAITING_TRANSFER_COMPLETE != handle->state) {
			// We are in error
			handle->state = (volatile mal_hspec_stm32f0_i2c_states_e)I2C_STATE_ERROR;
		} else {
			// Next state
			handle->state = (volatile mal_hspec_stm32f0_i2c_states_e)I2C_STATE_WAIT_STOP;
		}
	}
	// Check for stop
	if (I2C_GetITStatus(handle->stm_handle, I2C_IT_STOPF) == SET) {
		// Clear interrupt
		I2C_ClearITPendingBit(handle->stm_handle, I2C_IT_STOPF);
		// Notify
		mal_i2c_result_e i2c_result = MAL_I2C_SUCCESS;
		if (I2C_STATE_WAIT_STOP != handle->state) {
			i2c_result = MAL_I2C_BUS_ERROR;
		}
		handle->msg->callback(handle->msg->handle, &handle->msg->packet, i2c_result, &handle->msg);
		// Check if a new message can be started
		if (handle->msg != NULL) {
			i2c_start_transfer(handle);
		} else {
			handle->is_active = false;
		}
	}
}
Esempio n. 2
0
void I2C1_EV_IRQHandler (void)
{	
	if(I2C_GetITStatus(I2C1, I2C_IT_TX_EMPTY))
	{
		I2C_ClearITPendingBit(I2C1, I2C_IT_TX_EMPTY);
		tx_flag = 1;
	}
		
	if(I2C_GetITStatus(I2C1, I2C_IT_RX_FULL))
	{
		I2C_ClearITPendingBit(I2C1, I2C_IT_RX_FULL);
		rx_flag = 1;
	}

}
void i2c_interrupt_receive_handler(mal_i2c_s *handle) {
	// Common errors
	i2c_common_errors(handle);
	// Receiver status
	if (I2C_GetITStatus(handle->stm_handle, I2C_IT_RXNE) == SET) {
		// Make sure we are receiving
		if (I2C_STATE_RECEIVING != handle->state && I2C_STATE_START != handle->state) {
			// Clear interrupt
			I2C_ClearITPendingBit(handle->stm_handle, I2C_IT_RXNE);
			// Stop receiving
			I2C_GenerateSTOP(handle->stm_handle, ENABLE);
			// We are in error
			handle->state = (volatile mal_hspec_stm32f0_i2c_states_e)I2C_STATE_ERROR;
		} else {
			// Read data, also clears interrupt
			handle->msg->packet.buffer[handle->data_ptr++] = I2C_ReceiveData(handle->stm_handle);
			// Check if data is complete
			if (handle->data_ptr >= handle->msg->packet.packet_size) {
				// Change state
				handle->state = (volatile mal_hspec_stm32f0_i2c_states_e)I2C_STATE_WAITING_TRANSFER_COMPLETE;
			} else {
				handle->state = (volatile mal_hspec_stm32f0_i2c_states_e)I2C_STATE_RECEIVING;
			}
		}
	}
	// Common states
	i2c_common(handle);
}
Esempio n. 4
0
void i2c2_ev_irq_handler(void) {
  switch (I2C_GetLastEvent(I2C2))
  {
    /* Slave Transmitter ---------------------------------------------------*/
  case I2C_EVENT_SLAVE_TRANSMITTER_ADDRESS_MATCHED:  /* EV1 */
    memcpy(i2c2_buf, coder_values, MY_I2C2_BUF_LEN);
    i2c2_idx = 0;

  case I2C_EVENT_SLAVE_BYTE_TRANSMITTED:             /* EV3 */
    /* Transmit I2C2 data */
    if (i2c2_idx < MY_I2C2_BUF_LEN) {
      I2C_SendData(I2C2, i2c2_buf[i2c2_idx]);
      i2c2_idx++;
    }
    break;


  case I2C_EVENT_SLAVE_STOP_DETECTED:                /* EV4 */
    LED_ON(1);
    /* Clear I2C2 STOPF flag: read of I2C_SR1 followed by a write on I2C_CR1 */
    (void)(I2C_GetITStatus(I2C2, I2C_IT_STOPF));
    I2C_Cmd(I2C2, ENABLE);
    break;

  }
}
Esempio n. 5
0
void TwoWire::onService(void) 
{
	if (I2C_GetITStatus(twi, I2C_IT_ADDR) == SET)
	{
		I2C_ITConfig(twi, I2C_IT_RXI | I2C_IT_TXI | I2C_IT_STOPI, ENABLE);
		srvBufferLength = 0;
		srvBufferIndex = 0;
		if (twi->ISR & (1 << 16)) {
			status = SLAVE_SEND;
			if (onRequestCallback)
				onRequestCallback();
		} else {
			status = SLAVE_RECV;
		}

		I2C_ClearITPendingBit(twi, I2C_IT_ADDR);
	}
	if (I2C_GetITStatus(twi, I2C_IT_TXIS) == SET)
	{
		uint8_t c = 'x';
		if (srvBufferIndex < srvBufferLength)
			c = srvBuffer[srvBufferIndex++];
		I2C_SendData(twi, c);
	}
	if (I2C_GetITStatus(twi, I2C_IT_RXNE) == SET)
	{
		if (srvBufferLength < BUFFER_LENGTH)
			srvBuffer[srvBufferLength++] = I2C_ReceiveData(twi);
	}
	if (I2C_GetITStatus(twi, I2C_IT_STOPF) == SET)
	{
		if (status == SLAVE_RECV && onReceiveCallback) {
			// Copy data into rxBuffer
			// (allows to receive another packet while the
			// user program reads actual data)
			for (uint8_t i = 0; i < srvBufferLength; ++i)
				rxBuffer[i] = srvBuffer[i];
			rxBufferIndex = 0;
			rxBufferLength = srvBufferLength;
			// Alert calling program
			onReceiveCallback( rxBufferLength);
		}
		I2C_ITConfig(twi, I2C_IT_ADDRI, ENABLE);
		I2C_ITConfig(twi, I2C_IT_RXI | I2C_IT_TXI | I2C_IT_STOPI, DISABLE);
		I2C_ClearITPendingBit(twi, I2C_IT_STOPF);
	}
}
Esempio n. 6
0
/*******************************************************************************
* Function Name  : I2C2_ER_IRQHandler
* Description    : This function handles I2C2 Error interrupt request.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void I2C2_ER_IRQHandler(void)
{
  /* Check on I2C2 AF flag and clear it */
  if (I2C_GetITStatus(I2C2, I2C_IT_AF)) 
  {
    I2C_ClearITPendingBit(I2C2, I2C_IT_AF);
  }
}
Esempio n. 7
0
void I2C1_ER_IRQHandler(void)
{
	/* Check on I2C2 AF flag and clear it */
	if (I2C_GetITStatus(I2C1, I2C_IT_AF))
	{
		I2C_ClearITPendingBit(I2C1, I2C_IT_AF);
		I2C_ITConfig(I2C1, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR, DISABLE);
	}
}
void i2c_common_errors(mal_i2c_s *handle) {
	// Bus error
	if (I2C_GetITStatus(handle->stm_handle, I2C_IT_BERR) == SET) {
		// Clear interrupt
		I2C_ClearITPendingBit(handle->stm_handle, I2C_IT_BERR);
		// Stop bus
		I2C_GenerateSTOP(handle->stm_handle, ENABLE);
		// Start or stop out of place switch to error state
		handle->state = (volatile mal_hspec_stm32f0_i2c_states_e)I2C_STATE_ERROR;
	}
	// Arbitration loss error
	if (I2C_GetITStatus(handle->stm_handle, I2C_IT_ARLO) == SET) {
		// Clear interrupt
		I2C_ClearITPendingBit(handle->stm_handle, I2C_IT_ARLO);
		// Start or stop out of place switch to error state
		handle->state = (volatile mal_hspec_stm32f0_i2c_states_e)I2C_STATE_ERROR;
	}
}
Esempio n. 9
0
void I2C1_EV_IRQHandler(void)
{
	/* Address matched */
	if (I2C_GetITStatus(I2C, I2C_IT_ADDRI))
	{
		uprintf("ADDRI\r\n");
		I2C_ClearITPendingBit(I2C, I2C_IT_ADDRI);
	}

	/* Received byte */
	if (I2C_GetITStatus(I2C, I2C_IT_RXI))
	{
		I2C_RX_Packets[I2C_RX_Index] = I2C_ReceiveData(I2C);
		uprintf("RXI: %i\r\n", I2C_RX_Packets[I2C_RX_Index]);
		if (I2C_RX_Index < I2C_RX_PACKET_BUFFER_LENGTH-1) 
		{
			I2C_RX_Index++;
		}
		I2C_ClearITPendingBit(I2C, I2C_IT_RXI);
	}

	/* Stop condition detected */
	if (I2C_GetITStatus(I2C, I2C_IT_STOPI))
	{
		uprintf("STOPI\r\n");
		I2C_ClearITPendingBit(I2C, I2C_IT_STOPI);
		I2C_ProcessCommand(I2C_RX_Packets, I2C_RX_Index+1);
		I2C_RX_Index = 0;
	}

	///* Transmit interrupt status */
	//if (I2C_GetITStatus(I2C, I2C_IT_TXI))
	//{
	//	if (I2C_TX_Index > 0)
	//	{
	//		I2C_SendData(I2C, I2C_TX_Packets[I2C_TX_Index]);
	//		I2C_TX_Index--;
	//		uprintf("Transmitted packet\r\n");
	//		I2C_ClearITPendingBit(I2C, I2C_IT_TXI);
	//	}
	//}
	//I2C_PrintInterruptStatus();
}
Esempio n. 10
0
void i2c_hand(void) {
	if(I2C_GetITStatus(I2C2, I2C_IT_ADDR) == SET) {
		handl_i2c_message();
		I2C_ClearFlag(I2C2, I2C_FLAG_ADDR);
		stat.is_tran = I2C_GetTransferDirection(I2C2);
		stat.numr = 0;
	}
	if(I2C_GetITStatus(I2C2, I2C_IT_STOPF) == SET) {
		handl_i2c_message();
		cTran++;
		I2C_ClearFlag(I2C2, I2C_FLAG_STOPF);
		stat.numr = 0;
	}
	if(I2C_GetITStatus(I2C2, I2C_IT_RXNE) == SET) {
		if(stat.numr < I2C_BUF_SIZE)
			stat.bufr[stat.numr++] = I2C_ReceiveData(I2C2);
	}
	return;
}
Esempio n. 11
0
void I2C_PrintInterruptStatus()
{
	uprintf("I2C_IT_ERRI %i\r\n", I2C_GetITStatus(I2C, I2C_IT_ERRI));
	uprintf("I2C_IT_TCI %i\r\n", I2C_GetITStatus(I2C, I2C_IT_TCI));
	uprintf("I2C_IT_STOPI %i\r\n", I2C_GetITStatus(I2C, I2C_IT_STOPI));
	uprintf("I2C_IT_NACKI %i\r\n", I2C_GetITStatus(I2C, I2C_IT_NACKI));
	uprintf("I2C_IT_ADDRI %i\r\n", I2C_GetITStatus(I2C, I2C_IT_ADDRI));
	uprintf("I2C_IT_RXI %i\r\n", I2C_GetITStatus(I2C, I2C_IT_RXI));
	uprintf("I2C_IT_TXI %i\r\n", I2C_GetITStatus(I2C, I2C_IT_TXI));
}
Esempio n. 12
0
void I2C_IRQ_err(i2c_bus *bus) {
  bool err = FALSE;
  bus->phy_error = 0;
  if (I2C_GetITStatus(I2C_HW(bus), I2C_IT_SMBALERT )) {
    DBG(D_I2C, D_WARN, "i2c_err: SMBus Alert\n");
    I2C_ClearITPendingBit(I2C_HW(bus), I2C_IT_SMBALERT );
    bus->phy_error |= 1 << I2C_ERR_PHY_SMBUS_ALERT;
    err = TRUE;
  }
  if (I2C_GetITStatus(I2C_HW(bus), I2C_IT_TIMEOUT )) {
    DBG(D_I2C, D_WARN, "i2c_err: Timeout or Tlow error\n");
    I2C_ClearITPendingBit(I2C_HW(bus), I2C_IT_TIMEOUT );
    bus->phy_error |= 1 << I2C_ERR_PHY_TIMEOUT;
    err = TRUE;
  }
  if (I2C_GetITStatus(I2C_HW(bus), I2C_IT_ARLO )) {
    DBG(D_I2C, D_WARN, "i2c_err: Arbitration lost\n");
    I2C_ClearITPendingBit(I2C_HW(bus), I2C_IT_ARLO );
    bus->phy_error |= 1 << I2C_ERR_PHY_ARBITRATION_LOST;
    err = TRUE;
  }
  if (I2C_GetITStatus(I2C_HW(bus), I2C_IT_PECERR )) {
    DBG(D_I2C, D_WARN, "i2c_err: PEC error\n");
    I2C_ClearITPendingBit(I2C_HW(bus), I2C_IT_PECERR );
    bus->phy_error |= 1 << I2C_ERR_PHY_PEC_ERR;
    err = TRUE;
  }
  if (I2C_GetITStatus(I2C_HW(bus), I2C_IT_OVR )) {
    DBG(D_I2C, D_WARN, "i2c_err: Overrun/Underrun flag\n");
    I2C_ClearITPendingBit(I2C_HW(bus), I2C_IT_OVR );
    bus->phy_error |= 1 << I2C_ERR_PHY_OVER_UNDERRUN;
    err = TRUE;
  }
  if (I2C_GetITStatus(I2C_HW(bus), I2C_IT_AF )) {
    DBG(D_I2C, D_WARN, "i2c_err: Acknowledge failure\n");
    I2C_ClearITPendingBit(I2C_HW(bus), I2C_IT_AF );
    bus->phy_error |= 1 << I2C_ERR_PHY_ACK_FAIL;
    err = TRUE;
  }
  if (I2C_GetITStatus(I2C_HW(bus), I2C_IT_BERR )) {
    DBG(D_I2C, D_WARN, "i2c_err: Bus error\n");
    I2C_ClearITPendingBit(I2C_HW(bus), I2C_IT_BERR );
    bus->phy_error |= 1 << I2C_ERR_PHY_BUS_ERR;
    err = TRUE;
  }

  if (err) {
    I2C_LOG_ERR(bus, bus->phy_error);
    i2c_error(bus, I2C_ERR_PHY, bus->phy_error & ((1 << I2C_ERR_PHY_BUS_ERR) | (1 << I2C_ERR_PHY_ARBITRATION_LOST)));
  }
}
Esempio n. 13
0
static inline void i2c_error(struct i2c_periph *periph)
{
  uint8_t err_nr = 0;
  periph->errors->er_irq_cnt;
  if (I2C_GetITStatus(periph->reg_addr, I2C_IT_AF)) {       /* Acknowledge failure */
    periph->errors->ack_fail_cnt++;
    I2C_ClearITPendingBit(periph->reg_addr, I2C_IT_AF);
    err_nr = 1;
  }
  if (I2C_GetITStatus(periph->reg_addr, I2C_IT_BERR)) {     /* Misplaced Start or Stop condition */
    periph->errors->miss_start_stop_cnt++;
    I2C_ClearITPendingBit(periph->reg_addr, I2C_IT_BERR);
    err_nr = 2;
  }
  if (I2C_GetITStatus(periph->reg_addr, I2C_IT_ARLO)) {     /* Arbitration lost */
    periph->errors->arb_lost_cnt++;
    I2C_ClearITPendingBit(periph->reg_addr, I2C_IT_ARLO);
    err_nr = 3;
  }
  if (I2C_GetITStatus(periph->reg_addr, I2C_IT_OVR)) {      /* Overrun/Underrun */
    periph->errors->over_under_cnt++;
    I2C_ClearITPendingBit(periph->reg_addr, I2C_IT_OVR);
    err_nr = 4;
  }
  if (I2C_GetITStatus(periph->reg_addr, I2C_IT_PECERR)) {   /* PEC Error in reception */
    periph->errors->pec_recep_cnt++;
    I2C_ClearITPendingBit(periph->reg_addr, I2C_IT_PECERR);
    err_nr = 5;
  }
  if (I2C_GetITStatus(periph->reg_addr, I2C_IT_TIMEOUT)) {  /* Timeout or Tlow error */
    periph->errors->timeout_tlow_cnt++;
    I2C_ClearITPendingBit(periph->reg_addr, I2C_IT_TIMEOUT);
    err_nr = 6;
  }
  if (I2C_GetITStatus(periph->reg_addr, I2C_IT_SMBALERT)) { /* SMBus alert */
    periph->errors->smbus_alert_cnt++;
    I2C_ClearITPendingBit(periph->reg_addr, I2C_IT_SMBALERT);
    err_nr = 7;
  }

#ifdef I2C_DEBUG_LED
  LED_ERROR(20, err_nr);
#endif

  return;

}
Esempio n. 14
0
/*******************************************************************************
* Function Name  : I2C2_EV_IRQHandler
* Description    : This function handles I2C2 Event interrupt request.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void I2C2_EV_IRQHandler(void)
{
  switch (I2C_GetLastEvent(I2C2))
  {
    /* Slave Transmitter ---------------------------------------------------*/
    case I2C_EVENT_SLAVE_TRANSMITTER_ADDRESS_MATCHED:  /* EV1 */
    
      /* Transmit I2C2 data */
      I2C_SendData(I2C2, I2C2_Buffer_Tx[Tx2_Idx++]);
      break;

   case I2C_EVENT_SLAVE_BYTE_TRANSMITTED:             /* EV3 */
      /* Transmit I2C2 data */
      I2C_SendData(I2C2, I2C2_Buffer_Tx[Tx2_Idx++]);
      break; 
  

    /* Slave Receiver ------------------------------------------------------*/
    case I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED:     /* EV1 */
      break;

    case I2C_EVENT_SLAVE_BYTE_RECEIVED:                /* EV2 */
      /* Store I2C2 received data */
      I2C2_Buffer_Rx[Rx2_Idx++] = I2C_ReceiveData(I2C2);

      if(Rx2_Idx == Tx2BufferSize)
      { 
        I2C_TransmitPEC(I2C2, ENABLE);  
        Direction = Receiver;
      }
      break; 

    case I2C_EVENT_SLAVE_STOP_DETECTED:                /* EV4 */
      /* Clear I2C2 STOPF flag: read of I2C_SR1 followed by a write on I2C_CR1 */
      (void)(I2C_GetITStatus(I2C2, I2C_IT_STOPF));
      I2C_Cmd(I2C2, ENABLE);
      break;
   
    default:
      break;
  }
}
/**
  * @brief  This function handles I2C2 Error interrupt request.
  * @param  None
  * @retval None
  */
void I2C1_ER_IRQHandler(void)
{
  /* Check on I2C1 SMBALERT flag and clear it */
  if (I2C_GetITStatus(I2C1, I2C_IT_SMBALERT))
  {
    I2C_ClearITPendingBit(I2C1, I2C_IT_SMBALERT);
    SMbusAlertOccurred++;
  }
  /* Check on I2C1 Time out flag and clear it */
  if (I2C_GetITStatus(I2C1, I2C_IT_TIMEOUT))
  {
    I2C_ClearITPendingBit(I2C1, I2C_IT_TIMEOUT);
  }
  /* Check on I2C1 Arbitration Lost flag and clear it */
  if (I2C_GetITStatus(I2C1, I2C_IT_ARLO))
  {
    I2C_ClearITPendingBit(I2C1, I2C_IT_ARLO);
  } 

  /* Check on I2C1 PEC error flag and clear it */
  if (I2C_GetITStatus(I2C1, I2C_IT_PECERR))
  {
    I2C_ClearITPendingBit(I2C1, I2C_IT_PECERR);
  } 
  /* Check on I2C1 Overrun/Underrun error flag and clear it */
  if (I2C_GetITStatus(I2C1, I2C_IT_OVR))
  {
    I2C_ClearITPendingBit(I2C1, I2C_IT_OVR);
  } 
  /* Check on I2C1 Acknowledge failure error flag and clear it */
  if (I2C_GetITStatus(I2C1, I2C_IT_AF))
  {
    I2C_ClearITPendingBit(I2C1, I2C_IT_AF);
  }
  /* Check on I2C1 Bus error flag and clear it */
  if (I2C_GetITStatus(I2C1, I2C_IT_BERR))
  {
    I2C_ClearITPendingBit(I2C1, I2C_IT_BERR);
  }   
}
Esempio n. 16
0
/**
  * @brief  This function handles I2C ERROR interrupt request.
  * @param  None
  * @retval None
  */
void I2C1_ER_IRQHandler(void)
{
	/* Check on I2C2 SMBALERT flag and clear it */
	if (I2C_GetITStatus(I2C1, I2C_IT_SMBALERT))	{
		I2C_ClearITPendingBit(I2C1, I2C_IT_SMBALERT);
	}
	/* Check on I2C2 Time out flag and clear it */
	if (I2C_GetITStatus(I2C1, I2C_IT_TIMEOUT)) {
		I2C_ClearITPendingBit(I2C1, I2C_IT_TIMEOUT);
	}
	/* Check on I2C2 Arbitration Lost flag and clear it */
	if (I2C_GetITStatus(I2C1, I2C_IT_ARLO))	{
		I2C_ClearITPendingBit(I2C1, I2C_IT_ARLO);
	}	
	/* Check on I2C2 PEC error flag and clear it */
	if (I2C_GetITStatus(I2C1, I2C_IT_PECERR)) {
		I2C_ClearITPendingBit(I2C1, I2C_IT_PECERR);
	} 
	/* Check on I2C2 Overrun/Underrun error flag and clear it */
	if (I2C_GetITStatus(I2C1, I2C_IT_OVR)) {
		I2C_ClearITPendingBit(I2C1, I2C_IT_OVR);
	} 
	/* Check on I2C2 Acknowledge failure error flag and clear it */
	if (I2C_GetITStatus(I2C1, I2C_IT_AF)) {
		I2C_ClearITPendingBit(I2C1, I2C_IT_AF);
	}
	/* Check on I2C2 Bus error flag and clear it */
	if (I2C_GetITStatus(I2C1, I2C_IT_BERR)) {
		I2C_ClearITPendingBit(I2C1, I2C_IT_BERR);
	}

//	Error_message("I2C Bus ERROR");
//	LCD_PutStrig(20, 160, 0, "Push button OK for continue");
//	while(EPM570_Read_Keys() != OK);
//
//	LCD_SetTextColor(0x0000);
//	LCD_PutStrig(20, 160, 0, "Push button OK for continue");
//	LCD_DrawGrid(&activeAreaGrid, DRAW); // перерисовываем сетку в области осциллограмм
}
Esempio n. 17
0
void DMA_I2C1MasterWrite(unsigned char device_id, unsigned short mem_byte_addr, unsigned short tx_count, unsigned char *tx_data )
{
	unsigned short temp;
	unsigned short i;
	unsigned char *p;
	DMA_InitTypeDef DMA_InitStructure;
	p = tx_data;
	
	for(i=0;i<tx_count;i++)
	{
		*((volatile unsigned int*)(0x20003000+i*4)) = *(p++);
	}
	
	//I2C_Cmd(I2C1, DISABLE);
	I2C_Send7bitAddress(I2C1, 0xA8, I2C_Direction_Transmitter);
	
	I2C_Cmd(I2C1, ENABLE);
	//send byte mem addr
	temp = ((mem_byte_addr) & 0xff);
	I2CTXByte(I2C1,CMD_WRITE,temp);  //tx memory addr
	
	
	I2C_DMA_DIR = TDMAE_SET;
	I2C_DMACmd(I2C1, ENABLE);
	
	DMA_DeInit(DMA1_Channel7);
	
	/*
	DMA_InitStructure.DMA_PeripheralBaseAddr = (I2C1_BASE + 0x10);
  DMA_InitStructure.DMA_MemoryBaseAddr = 0x20003000;
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST ;
  DMA_InitStructure.DMA_BufferSize = tx_count;
  DMA_InitStructure.DMA_PeripheralInc = DMA_DstInc_Disable;
  DMA_InitStructure.DMA_MemoryInc = DMA_SrcInc_Enable;
  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
  DMA_InitStructure.DMA_Mode = DMA_Mode_Hardware;
  DMA_InitStructure.DMA_Priority = DMA_Priority_Low;
  DMA_InitStructure.DMA_DST_PER = DST_PER_ACK6;
  DMA_InitStructure.DMA_SRC_PER = SRC_PER_ACK1;
	*/
	
	DMA_InitStructure.DMA_PeripheralBaseAddr = (I2C1_BASE + 0x10);
  DMA_InitStructure.DMA_MemoryBaseAddr =0x20003000;
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST ;
  DMA_InitStructure.DMA_BufferSize = tx_count;
  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
  DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
  DMA_InitStructure.DMA_Priority = DMA_Priority_Low;
  DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
		
	DMA_Init(DMA1_Channel7, &DMA_InitStructure);
	DMA_Cmd(DMA1_Channel7, ENABLE);

	DMAcheckStatus(DMA1_FLAG_TC7);

	I2CTXEmptyCheck(I2C1);

	//dec stop
	while(1)
	{
		if(I2C_GetITStatus(I2C1, I2C_FLAG_STOP_DET))
		{
				temp = I2C1->IC_CLR_STOP_DET;  
			break;
		}
	}
	DMA_Cmd(DMA1_Channel7, DISABLE);

	I2C_DMACmd(I2C1, DISABLE);
	Delay(0x000ffff); 
	
  //I2C_Cmd(I2C1, DISABLE);
}
Esempio n. 18
0
static void HAL_I2C_ER_InterruptHandler(HAL_I2C_Interface i2c)
{
    /* Useful for debugging, diable to save time */
    // DEBUG_D("ER:0x%04x\r\n",I2C_ReadRegister(i2cMap[i2c]->I2C_Peripheral, I2C_Register_SR1) & 0xFF00);
#if 0
    //Check whether specified I2C interrupt has occurred and clear IT pending bit.

    /* Check on I2C SMBus Alert flag and clear it */
    if (I2C_GetITStatus(i2cMap[i2c]->I2C_Peripheral, I2C_IT_SMBALERT))
    {
        I2C_ClearITPendingBit(i2cMap[i2c]->I2C_Peripheral, I2C_IT_SMBALERT);
    }

    /* Check on I2C PEC error flag and clear it */
    if (I2C_GetITStatus(i2cMap[i2c]->I2C_Peripheral, I2C_IT_PECERR))
    {
        I2C_ClearITPendingBit(i2cMap[i2c]->I2C_Peripheral, I2C_IT_PECERR);
    }

    /* Check on I2C Time out flag and clear it */
    if (I2C_GetITStatus(i2cMap[i2c]->I2C_Peripheral, I2C_IT_TIMEOUT))
    {
        I2C_ClearITPendingBit(i2cMap[i2c]->I2C_Peripheral, I2C_IT_TIMEOUT);
    }

    /* Check on I2C Arbitration Lost flag and clear it */
    if (I2C_GetITStatus(i2cMap[i2c]->I2C_Peripheral, I2C_IT_ARLO))
    {
        I2C_ClearITPendingBit(i2cMap[i2c]->I2C_Peripheral, I2C_IT_ARLO);
    }

    /* Check on I2C Overrun/Underrun error flag and clear it */
    if (I2C_GetITStatus(i2cMap[i2c]->I2C_Peripheral, I2C_IT_OVR))
    {
        I2C_ClearITPendingBit(i2cMap[i2c]->I2C_Peripheral, I2C_IT_OVR);
    }

    /* Check on I2C Bus error flag and clear it */
    if (I2C_GetITStatus(i2cMap[i2c]->I2C_Peripheral, I2C_IT_BERR))
    {
        I2C_ClearITPendingBit(i2cMap[i2c]->I2C_Peripheral, I2C_IT_BERR);
    }
#else
    /* Save I2C Acknowledge failure error flag, then clear it */
    if (I2C_GetITStatus(i2cMap[i2c]->I2C_Peripheral, I2C_IT_AF))
    {
        i2cMap[i2c]->ackFailure = true;
        I2C_ClearITPendingBit(i2cMap[i2c]->I2C_Peripheral, I2C_IT_AF);
    }

    /* Now clear all remaining error pending bits without worrying about specific error
     * interrupt bit. This is faster than checking and clearing each one.
     */

    /* Read SR1 register to get I2C error */
    if ((I2C_ReadRegister(i2cMap[i2c]->I2C_Peripheral, I2C_Register_SR1) & 0xFF00) != 0x00)
    {
        /* Clear I2C error flags */
        i2cMap[i2c]->I2C_Peripheral->SR1 &= 0x00FF;
    }
#endif

    /* We could call I2C_SoftwareResetCmd() and/or I2C_GenerateStop() from this error handler,
     * but for now only ACK failure is handled in code.
     */
}
Esempio n. 19
0
void DMA_I2C1MasterRead(unsigned char device_id, unsigned short mem_byte_addr, unsigned short rx_count, unsigned char *rx_data )
{
	unsigned char temp;
	unsigned short i;
	unsigned char *p;
	DMA_InitTypeDef DMA_InitStructure;
	p = rx_data;

	//I2C_Cmd(I2C1, DISABLE);
	I2C_Send7bitAddress(I2C1, 0xA8, I2C_Direction_Transmitter);
	I2C_Cmd(I2C1, ENABLE);
	
	//send byte mem addr
	temp = ((mem_byte_addr) & 0xff);
	I2CTXByte(I2C1,CMD_WRITE,temp);  //tx memory addr

	while(1)
	{
	
		if(I2C_GetITStatus(I2C1, I2C_FLAG_STOP_DET))
		{
				  
			break;
		}
	}
  
	//I2C_Cmd(I2C1, DISABLE);
	I2C_Send7bitAddress(I2C1, 0xA8, I2C_Direction_Receiver);
	I2C_Cmd(I2C1, ENABLE);
	
	DMA_DeInit(DMA1_Channel2);
	/*
	DMA_InitStructure.DMA_PeripheralBaseAddr = (I2C1_BASE + 0x10);
  DMA_InitStructure.DMA_MemoryBaseAddr = 0x20004000;
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
  DMA_InitStructure.DMA_BufferSize = rx_count;
  DMA_InitStructure.DMA_PeripheralInc = DMA_SrcInc_Disable;
  DMA_InitStructure.DMA_MemoryInc = DMA_DstInc_Enable;
  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
  DMA_InitStructure.DMA_Mode = DMA_Mode_Hardware;
  DMA_InitStructure.DMA_Priority = DMA_Priority_Low;
  DMA_InitStructure.DMA_DST_PER = DST_PER_ACK0;
  DMA_InitStructure.DMA_SRC_PER = SRC_PER_ACK7;
	*/
	
	
	DMA_InitStructure.DMA_PeripheralBaseAddr = (I2C1_BASE + 0x10);
  DMA_InitStructure.DMA_MemoryBaseAddr =0x20004000;
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
  DMA_InitStructure.DMA_BufferSize = rx_count;
  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Enable;
  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable;
  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
  DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
  DMA_InitStructure.DMA_Priority = DMA_Priority_Low;
  DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
	DMA_Init(DMA1_Channel2, &DMA_InitStructure);	
	
	
	DMA_Cmd(DMA1_Channel2, ENABLE);
	
	I2C_DMA_DIR = RDMAE_SET;
	I2C_DMACmd(I2C1, ENABLE);
	
	for(i=0;i<rx_count;i++)
	{
		I2C1->IC_DATA_CMD = 0x0100;
		
		while(1)
		{
			if(I2C_GetITStatus(I2C1, I2C_IT_TX_EMPTY))
			{
				break;
			}
		}
	}
	
	DMAcheckStatus(DMA1_FLAG_TC2);
	DMA_Cmd(DMA1_Channel2, DISABLE);	
	
	I2C_DMACmd(I2C1, DISABLE);
    //I2C_Cmd(I2C1, DISABLE);
}
Esempio n. 20
0
void i2c_interrupt_transmit_handler(mal_i2c_s *handle) {
	// Common errors
	i2c_common_errors(handle);
	// Nack event
	if (I2C_GetITStatus(handle->stm_handle, I2C_IT_NACKF) == SET) {
		// Clear interrupt
		I2C_ClearITPendingBit(handle->stm_handle, I2C_IT_NACKF);
		if (handle->state == I2C_STATE_TRANSMITTING) {
			mal_i2c_result_e result;
			// Check if write is complete
			if (handle->data_ptr >= handle->msg->packet.packet_size) {
				// Change state
				result = MAL_I2C_NACK_COMPLETE;
			} else {
				// Transfer is incomplete
				result = MAL_I2C_NACK_INCOMPLETE;
			}
			handle->msg->callback(handle->msg->handle, &handle->msg->packet, result, &handle->msg);
			// Next state
			handle->state = (volatile mal_hspec_stm32f0_i2c_states_e)I2C_STATE_WAIT_STOP;
		} else {
			// Unknown sate for nack
			handle->msg->callback(handle->msg->handle, &handle->msg->packet, MAL_I2C_NACK_INCOMPLETE, &handle->msg);
			// Next state
			handle->state = (volatile mal_hspec_stm32f0_i2c_states_e)I2C_STATE_WAIT_STOP;
		}
	}
	// Transmitter status
	if (I2C_GetITStatus(handle->stm_handle, I2C_IT_TXIS) == SET) {
		// Make sure we are transmitting
		if (I2C_STATE_TRANSMITTING != handle->state && I2C_STATE_START != handle->state) {
			// Stop receiving
			I2C_GenerateSTOP(handle->stm_handle, ENABLE);
			// We are in error
			handle->state = (volatile mal_hspec_stm32f0_i2c_states_e)I2C_STATE_ERROR;
			// Clear interrupt by transmitting zero
			I2C_SendData(handle->stm_handle, 0);
			// Clear interrupt
			I2C_ClearITPendingBit(handle->stm_handle, I2C_IT_TXIS);
		} else {
			// Check if there still data to be sent
			if (handle->data_ptr >= handle->msg->packet.packet_size) {
				// Clear interrupt by transmitting zero
				I2C_SendData(handle->stm_handle, 0);
				// Clear interrupt
				I2C_ClearITPendingBit(handle->stm_handle, I2C_IT_TXIS);
				// Flag error
				handle->state = (volatile mal_hspec_stm32f0_i2c_states_e)I2C_STATE_WAITING_TRANSFER_COMPLETE;
			} else {
				// Transmit next data
				I2C_SendData(handle->stm_handle, handle->msg->packet.buffer[handle->data_ptr++]);
				// Check if transmission should be complete
				if (handle->data_ptr >= handle->msg->packet.packet_size) {
					handle->state = (volatile mal_hspec_stm32f0_i2c_states_e)I2C_STATE_WAITING_TRANSFER_COMPLETE;
				} else {
					handle->state = (volatile mal_hspec_stm32f0_i2c_states_e)I2C_STATE_TRANSMITTING;
				}
			}
		}
	}
	// Common states
	i2c_common(handle);
}
Esempio n. 21
0
/**
  * @brief  This function handles I2Cx event interrupt request.
  * @param  None
  * @retval None
  */
void I2Cx_EV_IRQHandler(void)
{
#if defined (I2C_SLAVE)
  /* Get Last I2C Event */
  Event = I2C_GetLastEvent(I2Cx);
  switch (Event)
  { 
    /*****************************************************************************/
    /*                          Slave Transmitter Events                         */
    /*                                                                           */
    /* ***************************************************************************/  
    
    /* Check on EV1 */
  case I2C_EVENT_SLAVE_TRANSMITTER_ADDRESS_MATCHED:  
      I2C_SendData(I2Cx, TxBuffer[Tx_Idx++]);
      /* Enable I2C event interrupt */
      I2C_ITConfig(I2Cx, I2C_IT_BUF, ENABLE);
    break;
    
    /* Check on EV3 */
  case I2C_EVENT_SLAVE_BYTE_TRANSMITTING:  
  case I2C_EVENT_SLAVE_BYTE_TRANSMITTED:  
    if (Tx_Idx < NbrOfDataToSend)
      I2C_SendData(I2Cx, TxBuffer[Tx_Idx++]);
    else
      /* Disable I2C event interrupt */
      I2C_ITConfig(I2Cx, I2C_IT_EVT  | I2C_IT_BUF, DISABLE);
    break;
 
  default:
    break;  
  }
#endif /* I2C_SLAVE*/
  
#if defined (I2C_MASTER)
/*****************************************************************************/
/*                               Master Receiver                             */
/*                                                                           */
/* ***************************************************************************/  
#ifdef I2C_10BITS_ADDRESS  
  /* Check on SB Flag and clear it */
  if(I2C_GetITStatus(I2Cx, I2C_IT_SB)== SET)
  {
    if (Send_HeaderStatus == 0x00) 
    {
      /* Send Header to Slave for write */
      I2C_SendData(I2Cx, HEADER_ADDRESS_Write);
      Send_HeaderStatus = 0x01;
      GenerateStartStatus = 0x01;
    }
    else
    {
      /* Send Header to Slave for Read */
      I2C_SendData(I2Cx, HEADER_ADDRESS_Read);
      Send_HeaderStatus = 0x00;
      if (NumberOfByteToReceive == 0x03)
      {
        /* Disable buffer Interrupts */
        I2C_ITConfig(I2Cx, I2C_IT_BUF , DISABLE);
      }
      else
      {
        /* Enable buffer Interrupts */
        I2C_ITConfig(I2Cx, I2C_IT_BUF , ENABLE);
      }
    }
  }
  /* Check on ADD10 Flag */
  else if(I2C_GetITStatus(I2Cx, I2C_IT_ADD10)== SET)
  {
    /* Send slave Address */
    I2C_Send7bitAddress(I2Cx, (uint8_t)SLAVE_ADDRESS, I2C_Direction_Transmitter);   
  }   
  
#else /* I2C_7BITS_ADDRESS */
  
  /* Check on EV5 */
  if(I2C_GetITStatus(I2Cx, I2C_IT_SB)== SET)
  {
    /* Send slave Address for read */
    I2C_Send7bitAddress(I2Cx, SLAVE_ADDRESS, I2C_Direction_Receiver);
    if (NumberOfByteToReceive == 0x03)
    {
      /* Disable buffer Interrupts */
      I2C_ITConfig(I2Cx, I2C_IT_BUF , DISABLE);
    }
    else
    {
      /* Enable buffer Interrupts */
      I2C_ITConfig(I2Cx, I2C_IT_BUF , ENABLE);
    }
  }  
#endif /* I2C_10BITS_ADDRESS */
  
  else if(I2C_GetITStatus(I2Cx, I2C_IT_ADDR)== SET)
  {
    if (NumberOfByteToReceive == 1)
    {
      I2C_AcknowledgeConfig(I2Cx, DISABLE);
    }
    
    /* Clear ADDR Register */
    (void)(I2Cx->SR1);
    (void)(I2Cx->SR2);  
    if (GenerateStartStatus == 0x00)
    { 
      if (NumberOfByteToReceive == 1)
      {
         I2C_GenerateSTOP(I2Cx, ENABLE);  
      }  
      
      if (NumberOfByteToReceive == 2)
      {
        I2C_AcknowledgeConfig(I2Cx, DISABLE);
        I2C_PECPositionConfig(I2Cx, I2C_PECPosition_Next);
        /* Disable buffer Interrupts */
        I2C_ITConfig(I2Cx, I2C_IT_BUF , DISABLE);
      }
    }
    
#ifdef I2C_10BITS_ADDRESS   
    if (GenerateStartStatus == 0x01)
    {
      /* Repeated Start */
      I2C_GenerateSTART(I2Cx, ENABLE);
      GenerateStartStatus = 0x00;
    }
    
#endif /* I2C_10BITS_ADDRESS */
  } 
  
  else if((I2C_GetITStatus(I2Cx, I2C_IT_RXNE)== SET)&&(I2C_GetITStatus(I2Cx, I2C_IT_BTF)== RESET))
  {
    /* Store I2C received data */
    RxBuffer[Rx_Idx++] = I2C_ReceiveData (I2Cx);
    NumberOfByteToReceive--;
    
    if (NumberOfByteToReceive == 0x03)
    {
      /* Disable buffer Interrupts */
      I2C_ITConfig(I2Cx, I2C_IT_BUF , DISABLE);
    }

    if (NumberOfByteToReceive == 0x00)
    {
      /* Disable Error and Buffer Interrupts */
      I2C_ITConfig(I2Cx, (I2C_IT_EVT | I2C_IT_BUF), DISABLE);            
    }
  }    
  /* BUSY, MSL and RXNE flags */
  else if(I2C_GetITStatus(I2Cx, I2C_IT_BTF)== SET)
  {
    /* if Three bytes remaining for reception */
    if (NumberOfByteToReceive == 3)
    {
      I2C_AcknowledgeConfig(I2Cx, DISABLE);
      /* Store I2C received data */
      RxBuffer[Rx_Idx++] = I2C_ReceiveData (I2Cx);
      NumberOfByteToReceive--;        
    } 
    else if (NumberOfByteToReceive == 2)
    {           
      I2C_GenerateSTOP(I2Cx, ENABLE);    
      
      /* Store I2C received data */
      RxBuffer[Rx_Idx++] = I2C_ReceiveData (I2Cx);
      NumberOfByteToReceive--;
      /* Store I2C received data */
      RxBuffer[Rx_Idx++] = I2C_ReceiveData (I2Cx);
      NumberOfByteToReceive--;        
      /* Disable Error and Buffer Interrupts */
      I2C_ITConfig(I2Cx, (I2C_IT_EVT | I2C_IT_BUF), DISABLE);            
    }
    else 
    {
      /* Store I2C received data */
      RxBuffer[Rx_Idx++] = I2C_ReceiveData (I2Cx);
      NumberOfByteToReceive--;
    }
  } 
#endif /* I2C_MASTER*/
}
Esempio n. 22
0
/********************
- 传感器I2C总线的事件中断服务程序
*********************/
void SENSOR_I2C_BUS_EV_ISR(void)
{
	//-SB
	if(I2C_GetITStatus(SENSOR_I2C_BUS, I2C_IT_SB))
		{
		I2C_AcknowledgeConfig(SENSOR_I2C_BUS, DISABLE);
		if((sensor_state.data_direction==I2C_Direction_Receiver) && sensor_state.SUB_Transmitted)
			{
			sensor_state.SUB_Transmitted = SET;
			I2C_Send7bitAddress(SENSOR_I2C_BUS, sensor_state.sensor_address, I2C_Direction_Receiver);
			}
		else
			{
			I2C_Send7bitAddress(SENSOR_I2C_BUS, sensor_state.sensor_address, I2C_Direction_Transmitter);
			}
		}
	
	//-ADDR
	else if(I2C_GetITStatus(SENSOR_I2C_BUS, I2C_IT_ADDR))
		{
		volatile uint8_t a=SENSOR_I2C_BUS->SR1;//Read SR1,2 to clear ADDR
		a=SENSOR_I2C_BUS->SR2;
		if(sensor_state.data_direction==I2C_Direction_Receiver && sensor_state.SUB_Transmitted) //已发送SUB,开始接收1字节[ref manual P735]
			{
			I2C_AcknowledgeConfig(SENSOR_I2C_BUS, DISABLE);//turn off ACK
			I2C_GenerateSTOP(SENSOR_I2C_BUS,ENABLE);//program the stop
			I2C_ITConfig(SENSOR_I2C_BUS, I2C_IT_BUF, ENABLE);
			}
		else //接收多于3字节;发送SUB或正在发送
			{
			I2C_ITConfig(SENSOR_I2C_BUS, I2C_IT_BUF, ENABLE);
			}
		}/*
	//-BTF										
	else if(I2C_GetITStatus(SENSOR_I2C_BUS, I2C_IT_BTF))
		{
		volatile uint8_t b=SENSOR_I2C_BUS->SR1;
		if(sensor_state.data_direction==I2C_Direction_Receiver && sensor_state.SUB_Transmitted)
			{}
		else
			{
			I2C_GenerateSTART(SENSOR_I2C_BUS, ENABLE);
			}
		}*/
	
	//-RXNE
	else if(I2C_GetITStatus(SENSOR_I2C_BUS, I2C_IT_RXNE))
		{
		sensor_state.receive_data=I2C_ReceiveData(SENSOR_I2C_BUS);
		I2C_ITConfig(SENSOR_I2C_BUS, I2C_IT_BUF, DISABLE);
		sensor_state.data_receive_finish=SET;
		}
	
	//-TXE
	else if(I2C_GetITStatus(SENSOR_I2C_BUS, I2C_IT_TXE))
		{
		I2C_SendData(SENSOR_I2C_BUS, sensor_state.SUB);
		sensor_state.SUB_Transmitted=SET;
		if(sensor_state.data_direction==I2C_Direction_Receiver)
			I2C_ITConfig(SENSOR_I2C_BUS, I2C_IT_BUF, DISABLE);
		I2C_GenerateSTART(SENSOR_I2C_BUS, ENABLE);
		}
/**/
	if(sensor_state.data_receive_finish)
		{
		uint8_t temp=(sensor_state.I2C_run_flag&0x07);//读出偏移量(低3位)
		
		sensor_state.data_receive_finish=RESET;
		sensor_state.SUB_Transmitted=RESET;

		switch(sensor_state.I2C_run_flag&0xE0)
			{
			case ACCER_READING:
				{
				(*(ACCER_DATA_BASE+temp))=sensor_state.receive_data;
				}
				break;
			case GYRO_READING:
				{
				(*(GYRO_DATA_BASE+temp))=sensor_state.receive_data;
				}
				break;
			case MEGN_READING://reserve!
				{
				}
				break;
			default:
				break;
			}
		
		if(temp<5)//未读够6个字节数据(0:5)
			{
			sensor_state.I2C_run_flag++;//接收完一个字节,标志加1
			sensor_state.SUB++;
			sensor_state.SUB_Transmitted=RESET;
			I2C_GenerateSTART(SENSOR_I2C_BUS, ENABLE);
			}
		else//接收到第6个字节
			{
			sensor_state.I2C_run_flag=0;//复位所有运行标志
			}		
		}
}
Esempio n. 23
0
File: i2c.c Progetto: pandc/unitek
void I2Cx_EV_IRQHandler(void)
{
portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
uint32_t lasteve;

	i2c_cntr++;
	switch(i2c_oper)
	{
	case I2C_Direction_Transmitter:
		lasteve = I2C_GetLastEvent(I2Cx);
		switch(lasteve)
		{
		case I2C_EVENT_MASTER_MODE_SELECT:
		/* If SB = 1, I2Cx master sent a START on the bus: EV5) */
			// Send the slave address for transmssion or for reception
			// according to the configured value in the write master write routine
			I2C_Send7bitAddress(I2Cx, i2c_addr, job->dir);
			break;
	//---------------------------------------------------------------------------
	//--- TX States -------------------------------------------------------------
	//---------------------------------------------------------------------------
		case I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED:
			/* Initialize the Transmit counter */
			i2c_pb = job->buf;
			i2c_len = job->len;
			/* Write the first data in the data register */
			I2Cx->DR = *i2c_pb++;
			/* If no further data to be sent, disable the I2C BUF IT
			in order to not have a TxE  interrupt */
			if (--i2c_len == 0)
			{
				if (job->last || (job[1].dir != I2C_Direction_Transmitter))
				{
					/* Disable the BUF IT in order to wait the BTF event */
					I2C_ITConfig(I2Cx,I2C_IT_BUF,DISABLE);
				}
				else
				{
					job++;
					i2c_pb = job->buf;
					i2c_len = job->len;
				}
			}
			break;
		case I2C_EVENT_MASTER_BYTE_TRANSMITTING:
			/* Master transmits the remaing data: from data2 until the last one.  */
			/* If TXE is set */
			/* If there is still data to write */
			if (i2c_len)
			{
				/* Write the data in DR register */
				I2Cx->DR = *i2c_pb++;
				// If  no data remains to write, disable the BUF IT in order
				// to not have again a TxE interrupt
				if (--i2c_len == 0)
				{
					if (job->last || (job[1].dir != I2C_Direction_Transmitter))
					{
						/* Disable the BUF IT */
						I2C_ITConfig(I2Cx,I2C_IT_BUF,DISABLE);
					}
					else
					{
						job++;
						i2c_pb = job->buf;
						i2c_len = job->len;
					}
				}
			}
			break;
		case I2C_EVENT_MASTER_BYTE_TRANSMITTED:
			/* If BTF and TXE are set (EV8_2), program the STOP */
			if (job->last)
			{
				/* Program the STOP */
				I2C_GenerateSTOP(I2Cx,ENABLE);
				/* Disable EVT IT In order to not have again a BTF IT */
				I2C_ITConfig(I2Cx,I2C_IT_EVT | I2C_IT_BUF,DISABLE);
				i2c_done = TRUE;
				xSemaphoreGiveFromISR(xSemaphoreI2C_Work,&xHigherPriorityTaskWoken);
			}
			else
			{
				job++;
				i2c_oper = job->dir;
				/* Send START condition */
				I2C_GenerateSTART(I2Cx, ENABLE);
			}
			break;
		}
		break;
	//---------------------------------------------------------------------------
	//--- RX States -------------------------------------------------------------
	//---------------------------------------------------------------------------
	case I2C_Direction_Receiver:
		if (I2C_GetITStatus(I2Cx,I2C_IT_SB) == SET)
		{
			/* Send slave Address for read */
			I2C_Send7bitAddress(I2Cx, i2c_addr, I2C_Direction_Receiver);
			i2c_pb = job->buf;
			i2c_len = job->len;
			if (i2c_len == 3)
			{
				/* Disable buffer Interrupts */
				I2C_ITConfig(I2Cx, I2C_IT_BUF , DISABLE);
			}
			else
			{
				/* Enable buffer Interrupts */
				I2C_ITConfig(I2Cx, I2C_IT_BUF , ENABLE);
			}
		}
		else if (I2C_GetITStatus(I2Cx, I2C_IT_ADDR) == SET)
		{
			if (i2c_len == 1)
			{
				I2C_AcknowledgeConfig(I2Cx, DISABLE);
			}
			/* Clear ADDR Register */
			(void)(I2Cx->SR1);
			(void)(I2Cx->SR2);
			if (i2c_len == 1)
			{
				I2C_GenerateSTOP(I2Cx, ENABLE);
			}
			else if (i2c_len == 2)
			{
				I2C_AcknowledgeConfig(I2Cx, DISABLE);
				I2C_NACKPositionConfig(I2Cx, I2C_NACKPosition_Next);
				/* Disable buffer Interrupts */
				I2C_ITConfig(I2Cx, I2C_IT_BUF , DISABLE);
			}
		}
		else if ((I2C_GetITStatus(I2Cx, I2C_IT_RXNE) == SET) && (I2C_GetITStatus(I2Cx, I2C_IT_BTF) == RESET))
		{
			/* Store I2C received data */
			*i2c_pb++ = I2C_ReceiveData (I2Cx);
			i2c_len--;

			if (i2c_len == 3)
			{
				/* Disable buffer Interrupts */
				I2C_ITConfig(I2Cx, I2C_IT_BUF , DISABLE);
			}
			else if (i2c_len == 0)
			{
				/* Disable Error, Event and Buffer Interrupts */
				I2C_ITConfig(I2Cx,I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR, DISABLE);
				i2c_done = TRUE;
				xSemaphoreGiveFromISR(xSemaphoreI2C_Work,&xHigherPriorityTaskWoken);
			}
		}    
		/* BUSY, MSL and RXNE flags */
		else if (I2C_GetITStatus(I2Cx, I2C_IT_BTF) == SET)
		{
			/* if Three bytes remaining for reception */
			if (i2c_len == 3)
			{
				I2C_AcknowledgeConfig(I2Cx, DISABLE);
				/* Store I2C received data */
				*i2c_pb++ = I2C_ReceiveData(I2Cx);
				i2c_len--;
			} 
			else if (i2c_len == 2)
			{           
				I2C_GenerateSTOP(I2Cx, ENABLE);    

				/* Store I2C received data */
				*i2c_pb++ = I2C_ReceiveData(I2Cx);
				i2c_len--;
				/* Store I2C received data */
				*i2c_pb++ = I2C_ReceiveData(I2Cx);
				i2c_len--;
				/* Disable Error, Event and Buffer Interrupts */
				I2C_ITConfig(I2Cx,I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR, DISABLE);
				i2c_done = TRUE;
				xSemaphoreGiveFromISR(xSemaphoreI2C_Work,&xHigherPriorityTaskWoken);
			}
			else 
			{
				/* Store I2C received data */
				*i2c_pb++ = I2C_ReceiveData(I2Cx);
				i2c_len--;
			}
		}
		break;
	}
	portEND_SWITCHING_ISR(xHigherPriorityTaskWoken);
}
Esempio n. 24
0
void i2c2_er_irq_handler(void) {
  /* Check on I2C2 AF flag and clear it */
  if (I2C_GetITStatus(I2C2, I2C_IT_AF))  {
    I2C_ClearITPendingBit(I2C2, I2C_IT_AF);
  }
}
Esempio n. 25
0
/**
  * @brief  This function handles I2Cx event interrupt request.
  * @param  None
  * @retval None
  */
void I2Cx_EV_IRQHANDLER(void)
{
#if defined (I2C_MASTER)
  /* Once the Start condition is sent the master can be master receiver 
  or master transmitter */
  if (MasterMode == MASTER_MODE_TRANSMITTER)
  { 
    /* Get Last I2C Event */
    Event = I2C_GetLastEvent(I2Cx);
    switch (Event)
    {
  /* ************************************************************************/
  /*                        Master Transmitter Events                       */
  /*                                                                        */
  /* ************************************************************************/ 
/* Sending the header sequence for Master Transmitter case ----------------*/
      
#ifdef I2C_10BITS_ADDRESS
      /* Check on EV5 */
    case I2C_EVENT_MASTER_MODE_SELECT :
      /* Send Header to Slave for write */
      I2C_SendData(I2Cx, HEADER_ADDRESS_Write);
      break;
      
      /* Check on EV9 */
    case I2C_EVENT_MASTER_MODE_ADDRESS10:
      /* Send slave Address for write */
      I2C_Send7bitAddress(I2Cx, (uint8_t)SLAVE_ADDRESS, I2C_Direction_Transmitter);
      break;
      
      
#else  /* I2C_7BITS_ADDRESS */
      /* Check on EV5 */
    case I2C_EVENT_MASTER_MODE_SELECT :
      /* Send slave Address for write */
      I2C_Send7bitAddress(I2Cx, (uint8_t)SLAVE_ADDRESS, I2C_Direction_Transmitter);
      break;
      
#endif /* I2C_10BITS_ADDRESS */ 
      
      /* Check on EV6 */
    case I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED:
      /* Transmit the First Data  */
      I2C_SendData(I2Cx, TxBuffer[Tx_Idx++]);
      break;
      
      /* Check on EV8 */
    case I2C_EVENT_MASTER_BYTE_TRANSMITTING:
    case I2C_EVENT_MASTER_BYTE_TRANSMITTED:      
      if (Tx_Idx == (uint8_t)NumberOfByteToTransmit)
      {
        /* Send STOP condition */
        I2C_GenerateSTOP(I2Cx, ENABLE);
        I2C_ITConfig(I2Cx, I2C_IT_EVT | I2C_IT_BUF, DISABLE);
      }
      else
      {
        /* Transmit Data TxBuffer */
        I2C_SendData(I2Cx, TxBuffer[Tx_Idx++]);
      }
      break;
           
    default:
      break;
    }
    
  }
  /*************************************************************************/
  /*                        Master Receiver Events                         */
  /*                                                                       */
  /*************************************************************************/
  else /* MASTER_MODE_RECEIVER */
  {
#ifdef I2C_10BITS_ADDRESS  
    /* Check on SB Flag and clear it */
    if(I2C_GetITStatus(I2Cx, I2C_IT_SB)== SET)
    {
      if (Send_HeaderStatus == 0x00) 
      {
        /* Send Header to Slave for write */
        I2C_SendData(I2Cx, HEADER_ADDRESS_Write);
        Send_HeaderStatus = 0x01;
        GenerateStartStatus = 0x01;
      }
      else
      {
        /* Send Header to Slave for Read */
        I2C_SendData(I2Cx, HEADER_ADDRESS_Read);
        Send_HeaderStatus = 0x00;
        if (NumberOfByteToReceive == 0x03)
        {
          /* Disable buffer Interrupts */
          I2C_ITConfig(I2Cx, I2C_IT_BUF , DISABLE);
        }
        else
        {
          /* Enable buffer Interrupts */
          I2C_ITConfig(I2Cx, I2C_IT_BUF , ENABLE);
        }
      }
    }
    /* Check on ADD10 Flag */
    else if(I2C_GetITStatus(I2Cx, I2C_IT_ADD10)== SET)
    {
      /* Send slave Address */
      I2C_Send7bitAddress(I2Cx, (uint8_t)SLAVE_ADDRESS, I2C_Direction_Transmitter);   
    }   
    
#else /* I2C_7BITS_ADDRESS */
    
    /* Check on EV5 */
    if(I2C_GetITStatus(I2Cx, I2C_IT_SB)== SET)
    {
      /* Send slave Address for read */
      I2C_Send7bitAddress(I2Cx, SLAVE_ADDRESS, I2C_Direction_Receiver);
      if (NumberOfByteToReceive == 0x03)
      {
        /* Disable buffer Interrupts */
        I2C_ITConfig(I2Cx, I2C_IT_BUF , DISABLE);
      }
      else
      {
        /* Enable buffer Interrupts */
        I2C_ITConfig(I2Cx, I2C_IT_BUF , ENABLE);
      }
    }  
#endif /* I2C_10BITS_ADDRESS */
    
    else if(I2C_GetITStatus(I2Cx, I2C_IT_ADDR)== SET)
    {
      if (NumberOfByteToReceive == 1)
      {
        I2C_AcknowledgeConfig(I2Cx, DISABLE);
      }
      /* Clear ADDR Register */
      (void)(I2Cx->SR1);
      (void)(I2Cx->SR2);  
      if (GenerateStartStatus == 0x00)
      { 
        if (NumberOfByteToReceive == 1)
        {
          I2C_GenerateSTOP(I2Cx, ENABLE);  
        }  
        
        if (NumberOfByteToReceive == 2)
        {
          I2C_AcknowledgeConfig(I2Cx, DISABLE);
          I2C_NACKPositionConfig(I2Cx, I2C_NACKPosition_Next);
          /* Disable buffer Interrupts */
          I2C_ITConfig(I2Cx, I2C_IT_BUF , DISABLE);
        }
      }
      
#ifdef I2C_10BITS_ADDRESS   
      if (GenerateStartStatus == 0x01)
      {
        /* Repeated Start */
        I2C_GenerateSTART(I2Cx, ENABLE);
        GenerateStartStatus = 0x00;
      }
      
#endif /* I2C_10BITS_ADDRESS */
    } 
    else if((I2C_GetITStatus(I2Cx, I2C_IT_RXNE)== SET)&&(I2C_GetITStatus(I2Cx, I2C_IT_BTF)== RESET))
    {
      /* Store I2C received data */
      RxBuffer[Rx_Idx++] = I2C_ReceiveData (I2Cx);
      NumberOfByteToReceive--;
      
      if (NumberOfByteToReceive == 0x03)
      {
        /* Disable buffer Interrupts */
        I2C_ITConfig(I2Cx, I2C_IT_BUF , DISABLE);
      }
      
      if (NumberOfByteToReceive == 0x00)
      {
        /* Disable Error and Buffer Interrupts */
        I2C_ITConfig(I2Cx, (I2C_IT_EVT | I2C_IT_BUF), DISABLE);            
      }
    }    
    /* BUSY, MSL and RXNE flags */
    else if(I2C_GetITStatus(I2Cx, I2C_IT_BTF)== SET)
    {
      /* if Three bytes remaining for reception */
      if (NumberOfByteToReceive == 3)
      {
        I2C_AcknowledgeConfig(I2Cx, DISABLE);
        /* Store I2C received data */
        RxBuffer[Rx_Idx++] = I2C_ReceiveData (I2Cx);
        NumberOfByteToReceive--;        
      } 
      else if (NumberOfByteToReceive == 2)
      {           
        I2C_GenerateSTOP(I2Cx, ENABLE);    
        
        /* Store I2C received data */
        RxBuffer[Rx_Idx++] = I2C_ReceiveData (I2Cx);
        NumberOfByteToReceive--;
        /* Store I2C received data */
        RxBuffer[Rx_Idx++] = I2C_ReceiveData (I2Cx);
        NumberOfByteToReceive--;        
        /* Disable Error and Buffer Interrupts */
        I2C_ITConfig(I2Cx, (I2C_IT_EVT | I2C_IT_BUF), DISABLE);            
      }
      else 
      {
        /* Store I2C received data */
        RxBuffer[Rx_Idx++] = I2C_ReceiveData (I2Cx);
        NumberOfByteToReceive--;
      }
    } 
  }  
#endif /* I2C_MASTER*/
  

#if defined (I2C_SLAVE)
  /* Get Last I2C Event */
  Event = I2C_GetLastEvent(I2Cx);
  switch (Event)
  { 
    /* ****************************************************************************/
    /*                          Slave Transmitter Events                          */
    /*                                                                            */
    /* ****************************************************************************/  
    
    /* Check on EV1 */
  case I2C_EVENT_SLAVE_TRANSMITTER_ADDRESS_MATCHED:  
    I2C_SendData(I2Cx, TxBuffer[Tx_Idx++]);
    I2C_ITConfig(I2Cx, I2C_IT_BUF , ENABLE);
    break;
    /* Check on EV3 */
  case I2C_EVENT_SLAVE_BYTE_TRANSMITTING:
  case I2C_EVENT_SLAVE_BYTE_TRANSMITTED:    
    if (Tx_Idx < NumberOfByteToTransmit)
    {
      I2C_SendData(I2Cx, TxBuffer[Tx_Idx++]);
    }
    else
    {
      /* Disable I2C event interrupt */
      I2C_ITConfig(I2Cx, I2C_IT_EVT  | I2C_IT_BUF, DISABLE);
    }
    break;
    
    /* ****************************************************************************/
    /*                              Slave Receiver Events                         */
    /*                                                                            */
    /* ****************************************************************************/ 
    
    /* check on EV1*/
  case I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED:
    Rx_Idx = 0x00;
    break;
    
    /* Check on EV2*/
  case I2C_EVENT_SLAVE_BYTE_RECEIVED:  
  case (I2C_EVENT_SLAVE_BYTE_RECEIVED | I2C_SR1_BTF):  
    RxBuffer[Rx_Idx++] = I2C_ReceiveData(I2Cx);
    break;
 
    /* Check on EV4 */
  case I2C_EVENT_SLAVE_STOP_DETECTED:             
    I2C_GetFlagStatus(I2Cx, I2C_FLAG_STOPF);
    I2C_Cmd(I2Cx, ENABLE);
    break;
    
  default:
    break;
  }
#endif /* I2C_SLAVE*/  
}