Exemplo n.º 1
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;
	}

}
Exemplo n.º 2
0
static void HAL_I2C_SoftwareReset(HAL_I2C_Interface i2c)
{
    /* Disable the I2C peripheral */
    I2C_Cmd(i2cMap[i2c]->I2C_Peripheral, DISABLE);

    /* Reset all I2C registers */
    I2C_SoftwareResetCmd(i2cMap[i2c]->I2C_Peripheral, ENABLE);
    I2C_SoftwareResetCmd(i2cMap[i2c]->I2C_Peripheral, DISABLE);

    /* Clear all I2C interrupt error flags, and re-enable them */
    I2C_ClearITPendingBit(i2cMap[i2c]->I2C_Peripheral, I2C_IT_SMBALERT | I2C_IT_PECERR |
            I2C_IT_TIMEOUT | I2C_IT_ARLO | I2C_IT_OVR | I2C_IT_BERR | I2C_IT_AF);
    I2C_ITConfig(i2cMap[i2c]->I2C_Peripheral, I2C_IT_ERR, ENABLE);

    /* Re-enable Event and Buffer interrupts in Slave mode */
    if(i2cMap[i2c]->mode == I2C_MODE_SLAVE)
    {
        I2C_ITConfig(i2cMap[i2c]->I2C_Peripheral, I2C_IT_EVT | I2C_IT_BUF, ENABLE);
    }

    /* Enable the I2C peripheral */
    I2C_Cmd(i2cMap[i2c]->I2C_Peripheral, ENABLE);

    /* Apply I2C configuration after enabling it */
    I2C_Init(i2cMap[i2c]->I2C_Peripheral, &i2cMap[i2c]->I2C_InitStructure);
}
Exemplo n.º 3
0
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;
		}
	}
}
Exemplo n.º 4
0
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);
}
Exemplo 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);
	}
}
Exemplo 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);
  }
}
Exemplo 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);
	}
}
Exemplo n.º 8
0
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;
	}
}
Exemplo 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();
}
Exemplo n.º 10
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)));
  }
}
Exemplo n.º 11
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;

}
/****************************************************************************************************
 * @fn      I2C_Slave_Initialise
 *          Call this function to set up the I2C slave to its initial standby state.
 *
 ***************************************************************************************************/
static void I2C_Slave_Initialise(void)
{
    I2C_InitTypeDef   I2C_InitStructure;

    /* Enable the bus */
    I2C_Cmd(I2C_SLAVE_BUS, ENABLE);

    /* Configure the I2C interface */
    I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
    I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
    I2C_InitStructure.I2C_OwnAddress1 = I2C_SLAVE_ADDR_8BIT;
    I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
    I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
    I2C_InitStructure.I2C_ClockSpeed = I2C_SLAVE_BUS_CLOCK;
    I2C_Init(I2C_SLAVE_BUS, &I2C_InitStructure);

    /* Enable interrupts and clear flags */
    I2C_ITConfig( I2C_SLAVE_BUS, (I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR), ENABLE );
    I2C_ClearITPendingBit( I2C_SLAVE_BUS, I2C_IT_SMBALERT | I2C_IT_TIMEOUT | I2C_IT_PECERR | I2C_IT_OVR
        | I2C_IT_AF | I2C_IT_ARLO | I2C_IT_BERR );
}
Exemplo n.º 13
0
/****************************************************************************************************
 * @fn      I2C_Start_Transfer
 *          Call this function to send a prepared data. Also include how many bytes that should be
 *          sent/read including the address byte. The function will initiate the transfer and return
 *          immediately (or return with error if previous transfer pending). User must wait for
 *          transfer to complete by calling I2C_Wait_Completion
 *
 * @param   //TODO
 *
 * @return  none
 *
 ***************************************************************************************************/
uint8_t I2C_Start_Transfer( uint8_t slaveAddr, uint16_t regAddr, uint8_t *pData, uint8_t dataSize, I2C_SendMode_t sendMode )
{
    /* Check that no transfer is already pending*/
    if (asyncXfer.i2c_txrx_status == I2C_TXRX_STATUS_ACTIVE)
    {

        D0_printf("I2C_Start_Transfer: A transfer is already pending\n\r");
        return I2C_ERR_BUSY;
    }

    if ((sendMode == I2C_MASTER_READ) || (sendMode == I2C_MASTER_WRITE))
    {
        /* Update the transfer descriptor */
        asyncXfer.i2c_slave_addr  = (slaveAddr << 1);
        asyncXfer.i2c_slave_reg   = regAddr;
        asyncXfer.i2c_txrx_status = I2C_TXRX_STATUS_ACTIVE;
        asyncXfer.pData           = pData;
        asyncXfer.num             = dataSize;
        asyncXfer.byte_index      = 0;
        asyncXfer.i2c_txrx_phase  = 0;
        gSendMode = sendMode;
        /* Enable interrupts and clear flags */
        I2C_ITConfig( I2C_SENSOR_BUS, I2C_IT_EVT | I2C_IT_BUF | I2C_IT_ERR, ENABLE );
        I2C_ClearITPendingBit( I2C_SENSOR_BUS, I2C_IT_SMBALERT | I2C_IT_TIMEOUT | I2C_IT_PECERR | I2C_IT_OVR
            | I2C_IT_AF | I2C_IT_ARLO | I2C_IT_BERR );

        /* Enable ACK as it is disabled in interrupt handler after each transaction */
        I2C_SENSOR_BUS->CR1 |= CR1_ACK_Set;

        /* Initiate start */
        I2C_GenerateSTART( I2C_SENSOR_BUS, ENABLE );
    }
    else
    {
        return I2C_ERR_REQ;
    }
    return I2C_ERR_OK;
}
/**
  * @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);
  }   
}
Exemplo n.º 15
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); // перерисовываем сетку в области осциллограмм
}
Exemplo n.º 16
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);
}
Exemplo n.º 17
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.
     */
}
Exemplo n.º 18
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_stm32f10x_xx.s) before to branch to application main.
       To reconfigure the default setting of SystemInit() function, refer to
       system_stm32f10x.c file
     */
 
  /* NVIC Configuration */
  NVIC_Configuration();

  /* Initialize the LCD */
#ifdef USE_STM32100E_EVAL
  STM32100E_LCD_Init();
#elif defined USE_STM3210E_EVAL
  STM3210E_LCD_Init();
#elif defined USE_STM32100B_EVAL
  STM32100B_LCD_Init();
#elif defined USE_STM3210B_EVAL
  STM3210B_LCD_Init();
#endif

  
#ifdef USE_STM3210E_EVAL
  /* Disable FSMC only for STM32 High-density and XL-density devices */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, DISABLE);
#endif /* USE_STM3210E_EVAL */
  
  /* Initialize the Temperature Sensor */
  LM75_Init();

  if (LM75_GetStatus() == SUCCESS)
  {    
#ifdef USE_STM3210E_EVAL
    /* Enable FSMC */
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, ENABLE);
#endif /* USE_STM3210E_EVAL */
    
    /* Clear the LCD */
    LCD_Clear(LCD_COLOR_WHITE);
    
    /* Set the Back Color */
    LCD_SetBackColor(LCD_COLOR_BLUE);
    
    /* Set the Text Color */
    LCD_SetTextColor(LCD_COLOR_GREEN);
    
    LCD_DisplayStringLine(LCD_LINE_0, "     Temperature    ");

#ifdef USE_STM3210E_EVAL        
    /* Disable FSMC */
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, DISABLE);

    /* Initialize the Temperature Sensor */
    LM75_Init();
    
#endif /* USE_STM3210E_EVAL */
    
    /* Configure the Temperature sensor device STLM75:
    - Thermostat mode Interrupt
    - Fault tolerance: 00
    */
    LM75_WriteConfReg(0x02);
    
    /* Configure the THYS and TOS inorder to use the SMbus alert interrupt */
    LM75_WriteReg(LM75_REG_THYS, TEMPERATURE_THYS << 8);  /*31ÝC*/
    LM75_WriteReg(LM75_REG_TOS, TEMPERATURE_TOS << 8);   /*32ÝC*/
    
    I2C_ClearITPendingBit(LM75_I2C, I2C_IT_SMBALERT);
    
    SMbusAlertOccurred = 0;
    
    /* Infinite Loop */
    while (1)
    {
#ifdef USE_STM3210E_EVAL        
    /* Disable FSMC */
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, DISABLE);

    /* Initialize the Temperature Sensor */
    LM75_Init();
#endif /* USE_STM3210E_EVAL */
      
      /* Get double of Temperature value */
      TempValue = LM75_ReadTemp();

#ifdef USE_STM3210E_EVAL          
      /* Enable FSMC */
      RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, ENABLE);
#endif /* USE_STM3210E_EVAL */
      
      if (TempValue <= 256)
      {
        /* Positive temperature measured */
        TempCelsiusDisplay[7] = '+';
        
        /* Initialize the temperature sensor value*/
        TempValueCelsius = TempValue;
      }
      else
      {
        /* Negative temperature measured */
        TempCelsiusDisplay[7] = '-';
        /* Remove temperature value sign */
        TempValueCelsius = 0x200 - TempValue;
      }
      
      /* Calculate temperature digits in ÝC */
      if ((TempValueCelsius & 0x01) == 0x01)
      {
        TempCelsiusDisplay[12] = 0x05 + 0x30;
        TempFahrenheitDisplay[12] = 0x05 + 0x30;
      }
      else
      {
        TempCelsiusDisplay[12] = 0x00 + 0x30;
        TempFahrenheitDisplay[12] = 0x00 + 0x30;
      }
      
      TempValueCelsius >>= 1;
      
      TempCelsiusDisplay[8] = (TempValueCelsius / 100) + 0x30;
      TempCelsiusDisplay[9] = ((TempValueCelsius % 100) / 10) + 0x30;
      TempCelsiusDisplay[10] = ((TempValueCelsius % 100) % 10) + 0x30;
      
      if (TempValue > 256)
      {
        if (((9 * TempValueCelsius) / 5) <= 32)
        {
          /* Convert temperature ÝC to Fahrenheit */
          TempValueFahrenheit = abs (32 - ((9 * TempValueCelsius) / 5));
          
          /* Calculate temperature digits in ÝF */
          TempFahrenheitDisplay[8] = (TempValueFahrenheit / 100) + 0x30;
          TempFahrenheitDisplay[9] = ((TempValueFahrenheit % 100) / 10) + 0x30;
          TempFahrenheitDisplay[10] = ((TempValueFahrenheit % 100) % 10) + 0x30;
          /* Positive temperature measured */
          TempFahrenheitDisplay[7] = '+';
        }
        else
        {
          /* Convert temperature ÝC to Fahrenheit */
          TempValueFahrenheit = abs(((9 * TempValueCelsius) / 5) - 32);
          /* Calculate temperature digits in ÝF */
          TempFahrenheitDisplay[8] = (TempValueFahrenheit / 100) + 0x30;
          TempFahrenheitDisplay[9] = ((TempValueFahrenheit % 100) / 10) + 0x30;
          TempFahrenheitDisplay[10] = ((TempValueFahrenheit % 100) % 10) + 0x30;
          
          /* Negative temperature measured */
          TempFahrenheitDisplay[7] = '-';
        }
      }
      else
      {
        /* Convert temperature ÝC to Fahrenheit */
        TempValueFahrenheit = ((9 * TempValueCelsius) / 5) + 32;
        
        /* Calculate temperature digits in ÝF */
        TempFahrenheitDisplay[8] = (TempValueFahrenheit / 100) + 0x30;
        TempFahrenheitDisplay[9] = ((TempValueFahrenheit % 100) / 10) + 0x30;
        TempFahrenheitDisplay[10] = ((TempValueFahrenheit % 100) % 10) + 0x30;
        
        /* Positive temperature measured */
        TempFahrenheitDisplay[7] = '+';
      }
      
      /* Display Fahrenheit value on LCD */
      for (index = 0; index < 20; index++)
      {
        LCD_DisplayChar(LCD_LINE_6, (319 - (16 * index)), TempCelsiusDisplay[index]);
        
        LCD_DisplayChar(LCD_LINE_7, (319 - (16 * index)), TempFahrenheitDisplay[index]);
      }
      
      if (SMbusAlertOccurred == 1)
      {
        /* Set the Back Color */
        LCD_SetBackColor(LCD_COLOR_BLUE);
        /* Set the Text Color */
        LCD_SetTextColor(LCD_COLOR_RED);
        LCD_DisplayStringLine(LCD_LINE_2, "Warning: Temp exceed");
        LCD_DisplayStringLine(LCD_LINE_3, "        32 C        ");
      }
      if (SMbusAlertOccurred == 2)
      {
        /* Set the Back Color */
        LCD_SetBackColor(LCD_COLOR_WHITE);
        /* Set the Text Color */
        LCD_SetTextColor(LCD_COLOR_WHITE);
        LCD_ClearLine(LCD_LINE_2);
        LCD_ClearLine(LCD_LINE_3);
        SMbusAlertOccurred = 0;
        /* Set the Back Color */
        LCD_SetBackColor(LCD_COLOR_BLUE);
        /* Set the Text Color */
        LCD_SetTextColor(LCD_COLOR_GREEN);
      }
    }
  }
Exemplo n.º 19
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);
  }
}