/** * @brief This function handles I2Cx Error interrupt request. * @param None * @retval None */ void I2Cx_ER_IRQHandler(void) { #if defined (I2C_SLAVE) /* Read SR1 register to get I2C error */ if ((I2C_ReadRegister(I2Cx, I2C_Register_SR1) & 0xFF00) != 0x00) { /* Clears error flags */ I2Cx->SR1 &= 0x00FF; Tx_Idx = 0; } #endif /* I2C_SLAVE*/ #if defined (I2C_MASTER) /* Read SR1 register to get I2C error */ if ((I2C_ReadRegister(I2Cx, I2C_Register_SR1) & 0xFF00) != 0x00) { /* Clears erreur flags */ I2Cx->SR1 &= 0x00FF; /* Set LED3 and LED4 */ STM_EVAL_LEDOn(LED3); STM_EVAL_LEDOn(LED4); STM_EVAL_LEDOff(LED2); Rx_Idx = 0; } #endif /* I2C_MASTER*/ }
void Check_OPT() { uint16 Manufacturer, Device; uint8 Status; Status = I2C_ReadRegister(OPT3001_I2C_ADDRESS, REG_DEVICE_ID, &Device); if(Status == 0) { Add_To_DDR("Optic Error"); return; I2C_I2C_MSTR_ERR_LB_NAK; I2C_I2C_MSTR_ERR_ARB_LOST; } Status = I2C_ReadRegister(OPT3001_I2C_ADDRESS, REG_MANUFACTURER_ID, &Manufacturer); if(Status == 0) { Add_To_DDR("Optic Error"); return; } if((Manufacturer == 0x5449) && (Device == 0x3001)) { Add_To_DDR("Optic OK"); Add_To_DDR("Device OPT3001 EN"); Print_DDR(); return; } return; }
/** * @brief Wait for EEPROM Standby state * @param None * @retval None */ void sEE_WaitEepromStandbyState(void) { __IO uint8_t tempreg = 0; __IO uint32_t timeout = 0xFFFF; do { /*!< Send START condition */ I2C_GenerateSTART(sEE_I2C, ENABLE); /* Test on EEPROM_I2C EV5 and clear it */ while (!I2C_GetFlagStatus(sEE_I2C, I2C_FLAG_SB)) /* EV5 */ { } /*!< Send EEPROM address for write */ I2C_Send7bitAddress(sEE_I2C, (uint8_t)sEEAddress, I2C_Direction_Transmitter); /*!< Wait for address aknowledgement */ for (; timeout > 0; timeout--); /*!< Read sEE SR1 register to clear pending flags */ tempreg = I2C_ReadRegister(sEE_I2C, I2C_Register_SR1); } while (!(tempreg & 0x02)); /*!< Clear AF flag */ I2C_ClearFlag(sEE_I2C, I2C_FLAG_AF); /*!< STOP condition */ I2C_GenerateSTOP(sEE_I2C, ENABLE); }
//______________________________________________________________________________________ void I2C1_ER_IRQHandler(void) { if ((I2C_ReadRegister(I2C1, I2C_Register_SR1) & 0xFF00) != 0x00) { I2C1->SR1 &= 0x00FF; } }
void Calibrate (void) { unsigned char reg_val = 0; while (!reg_val) // Wait for a first set of data { reg_val = I2C_ReadRegister(MMA845x_I2C_ADDRESS, STATUS_REG) & 0x08; } I2C_ReadMultiRegisters(MMA845x_I2C_ADDRESS, OUT_X_MSB_REG, 6, AccData); // Read data output registers 0x01-0x06 Xout_14_bit = ((short) (AccData[0]<<8 | AccData[1])) >> 2; // Compute 14-bit X-axis output value Yout_14_bit = ((short) (AccData[2]<<8 | AccData[3])) >> 2; // Compute 14-bit Y-axis output value Zout_14_bit = ((short) (AccData[4]<<8 | AccData[5])) >> 2; // Compute 14-bit Z-axis output value Xoffset = Xout_14_bit / 8 * (-1); // Compute X-axis offset correction value Yoffset = Yout_14_bit / 8 * (-1); // Compute Y-axis offset correction value Zoffset = (Zout_14_bit - SENSITIVITY_2G) / 8 * (-1); // Compute Z-axis offset correction value I2C_WriteRegister(MMA845x_I2C_ADDRESS, CTRL_REG1, 0x00); // Standby mode to allow writing to the offset registers I2C_WriteRegister(MMA845x_I2C_ADDRESS, OFF_X_REG, Xoffset); I2C_WriteRegister(MMA845x_I2C_ADDRESS, OFF_Y_REG, Yoffset); I2C_WriteRegister(MMA845x_I2C_ADDRESS, OFF_Z_REG, Zoffset); I2C_WriteRegister(MMA845x_I2C_ADDRESS, CTRL_REG3, 0x00); // Push-pull, active low interrupt I2C_WriteRegister(MMA845x_I2C_ADDRESS, CTRL_REG4, 0x01); // Enable DRDY interrupt I2C_WriteRegister(MMA845x_I2C_ADDRESS, CTRL_REG5, 0x01); // DRDY interrupt routed to INT1 - PTA14 //I2C_WriteRegister(MMA845x_I2C_ADDRESS, CTRL_REG1, 0x3D); // ODR = 1.56Hz, Reduced noise, Active mode I2C_WriteRegister(MMA845x_I2C_ADDRESS, CTRL_REG1, 0xC3); // ODR = 1.56Hz, Reduced noise, Active mode }
void MAX3353_ISR(void) { // Clear the Interrupt pending flag gu8MAX3353StatusRegister=I2C_ReadRegister(MAX3353_I2C_ADDRESS,MAX3353_REG_INT_LATCH); // Set MAX3353 ISR flag FLAG_SET(_MAX3353,gu8ISR_Flags); }
/******************************************************************************* * Function Name : I2C_EE_WaitEepromStandbyState * Description : Wait for EEPROM Standby state * Input : None * Output : None * Return : None *******************************************************************************/ void I2C_EE_WaitEepromStandbyState(void) { vu16 SR1_Tmp = 0; do { /* Send START condition */ I2C_GenerateSTART(I2C1, ENABLE); /* Read I2C1 SR1 register */ SR1_Tmp = I2C_ReadRegister(I2C1, I2C_Register_SR1); /* Send EEPROM address for write */ I2C_Send7bitAddress(I2C1, EEPROM_ADDRESS, I2C_Direction_Transmitter); }while(!(I2C_ReadRegister(I2C1, I2C_Register_SR1) & 0x0002)); /* Clear AF flag */ I2C_ClearFlag(I2C1, I2C_FLAG_AF); }
void I2C2_ER_IRQHandler(void){ /* Read SR1 register to get I2C error */ if ((I2C_ReadRegister(I2C2, I2C_Register_SR1) & 0xFF00) != 0x00) { /* Clears erreur flags */ I2C2->SR1 &= 0x00FF; } }
//void I2C_Sensor_EV_IRQHandler(void) void I2C2_EV_IRQHandler(void) { if(I2C_GetFlagStatus(I2C_Sensor,I2C_FLAG_SB) == SET) //start sent { //clear flag I2C_ReadRegister(I2C_Sensor,I2C_Register_SR1); IIC_StartEvent(); }else if(I2C_GetFlagStatus(I2C_Sensor,I2C_FLAG_ADDR) == SET) //address sent { //clear flag I2C_ReadRegister(I2C_Sensor,I2C_Register_SR1); I2C_ReadRegister(I2C_Sensor,I2C_Register_SR2); IIC_AddressEvent(); }else if(I2C_GetFlagStatus(I2C_Sensor,I2C_FLAG_BTF) == SET) //data transferred { //clear flag I2C_ReadRegister(I2C_Sensor,I2C_Register_SR1); IIC_DataEvent(); }else { //clear flag I2C_ReadRegister(I2C_Sensor,I2C_Register_SR1); I2C_ReadRegister(I2C_Sensor,I2C_Register_SR2); I2C_ReadRegister(I2C_Sensor,I2C_Register_DR); } }
void i2c_wait_standby(void) { vu16 sr1_temp = 0; do { I2C_GenerateSTART(I2C1, ENABLE); sr1_temp = I2C_ReadRegister( I2C1, I2C_Register_SR1); I2C_Send7bitAddress(I2C1, I2C1_MPU6050, I2C_Direction_Transmitter); } while (!(I2C_ReadRegister(I2C1, I2C_Register_SR1)&0x0002)); I2C_ClearFlag(I2C1, I2C_FLAG_AF); I2C_GenerateSTOP(I2C1, ENABLE); }
void IMU_Init(void) { // Check which IMU is installed by trying to read ID registers unsigned char id_adxl = I2C_ReadRegister(ADXL_ADDRESS, 0x00); unsigned char id_itg = I2C_ReadRegister(ITG_ADDRESS, 0x00); unsigned char id_hmc[3]; I2C_ReadMultipleRegisters(HMC_ADDRESS, 0x0A, &id_hmc[0], 3); if ((id_adxl == 0xe5) && ((id_itg & 0x7e) == ITG_ADDRESS) && (id_hmc[0] == 'H' && id_hmc[1] == '4' && id_hmc[2] == '3')) { dbgu_printf("Found digital IMU!\r\n"); IMU_accel_init(); IMU_gyro_init(); IMU_magnet_init(); TC_DelayMs(100); double temp = IMU_get_temp(); dbgu_printf("Current temperature: %f °C\r\n", temp); imu_digital = 1; } else { imu_digital = 0; } // Todo: load values from eeprom IMU_Update(); IMU_Calibrate_Accel(); #ifdef USE_KALMAN_FILTER init_Gyro1DKalman(&filter_roll, Q_ACCEL, Q_GYRO, R_ACCEL); init_Gyro1DKalman(&filter_pitch, Q_ACCEL, Q_GYRO, R_ACCEL); float accX = ((float)(imu_AccelZ_raw - 512)) / 128.0f; float accY = ((float)(imu_AccelY_raw - 512)) / 128.0f; float accZ = ((float)(imu_AccelX_raw - 512)) / 128.0f; pitch = atan2(accX, sqrt(accY * accY + accZ * accZ)); roll = atan2(accY, -accZ); filter_roll.x_angle = roll; filter_pitch.x_angle = pitch; #endif }
/** * @brief Wait for EEPROM Standby state * @param None * @retval None */ void sEE_WaitEepromStandbyState(void) { __IO uint16_t SR1_Tmp = 0; do { /*!< Send START condition */ I2C_GenerateSTART(sEE_I2C, ENABLE); /*!< Read sEE SR1 register to clear pending flags */ SR1_Tmp = I2C_ReadRegister(sEE_I2C, I2C_Register_SR1); /*!< Send EEPROM address for write */ I2C_Send7bitAddress(sEE_I2C, sEEAddress, I2C_Direction_Transmitter); }while(!(I2C_ReadRegister(sEE_I2C, I2C_Register_SR1) & 0x0002)); /*!< Clear AF flag */ I2C_ClearFlag(sEE_I2C, I2C_FLAG_AF); /*!< STOP condition */ I2C_GenerateSTOP(sEE_I2C, ENABLE); }
/** * @brief Wait for EEPROM Standby state * @param None * @retval None */ static void I2C_EE_WaitEepromStandbyState(void) { __IO uint16_t SR1_Tmp = 0; do { /* Send START condition */ I2C_GenerateSTART(I2C_EE, ENABLE); /* Read I2C_EE SR1 register to clear pending flags */ SR1_Tmp = I2C_ReadRegister(I2C_EE, I2C_Register_SR1); /* Send EEPROM address for write */ I2C_Send7bitAddress(I2C_EE, EEPROM_ADDRESS, I2C_Direction_Transmitter); }while(!(I2C_ReadRegister(I2C_EE, I2C_Register_SR1) & 0x0002)); /* Clear AF flag */ I2C_ClearFlag(I2C_EE, I2C_FLAG_AF); /* STOP condition */ I2C_GenerateSTOP(I2C_EE, ENABLE); }
void Accelerometer_Init (void) { unsigned char reg_val = 0; I2C_WriteRegister(MMA845x_I2C_ADDRESS, CTRL_REG2, 0x40); // Reset all registers to POR values do // Wait for the RST bit to clear { reg_val = I2C_ReadRegister(MMA845x_I2C_ADDRESS, CTRL_REG2) & 0x40; } while (reg_val); I2C_WriteRegister(MMA845x_I2C_ADDRESS, XYZ_DATA_CFG_REG, 0x00); // +/-2g range -> 1g = 16384/4 = 4096 counts I2C_WriteRegister(MMA845x_I2C_ADDRESS, CTRL_REG2, 0x00); // 0x02High Resolution mode I2C_WriteRegister(MMA845x_I2C_ADDRESS, CTRL_REG1, 0xC3);// 0x3D); // ODR = 1.56Hz, Reduced noise, Active mode }
uint_8 MAX3353_CheckifPresent(void) { uint_8 u8Counter; for(u8Counter=0;u8Counter<4;u8Counter++) { u32ID=u32ID<<8; u32ID|=I2C_ReadRegister(MAX3353_I2C_ADDRESS,u8Counter); } if(u32ID!=MAX3353_MID) return(MAX3353_NOT_PRESENT); u32ID=0; for(u8Counter=4;u8Counter<8;u8Counter++) { u32ID=u32ID<<8; u32ID|=I2C_ReadRegister(MAX3353_I2C_ADDRESS,u8Counter); } if(u32ID!=MAX3353_PID) return(MAX3353_NOT_PRESENT); return(MAX3353_OK); }
/****************************************************************************** * Function name : main * Description : Main testing loop * Input param : None * Return : None * See also : None *******************************************************************************/ void main (void) { /* peripheral initialization */ #ifdef FAST_I2C_MODE CLK->CKDIVR = 0x00; // sys clock / 1 #else CLK->CKDIVR = 0x01; // sys clock / 2 #endif // Set GPIO for LED uses GPIOH->DDR |= 0x0F; GPIOH->CR1 |= 0x0F; // initialize timer 4 mandatory for timout and tick measurement TIM4_Init(); // Initialize I2C for communication I2C_Init(); // initialization of dummy field for test purpose memcpy(Dummy, DUMMY_INIT, MAX_DUMMY); #ifndef _COSMIC_ err_save= 0; TIM4_tout= loop_count= 0; #endif // Enable all interrupts enableInterrupts(); /* main test loop */ while(1) { // switch on LED1 at the beginning of test switch_on(LED1); // write 1 data bytes with offset 8 from Dummy filed to slave memory set_tout_ms(10); I2C_WriteRegister(8, 1, &Dummy[8]); // read 1 byte with offset 8 back from the image at slave memory if(tout()) { set_tout_ms(10); I2C_ReadRegister(8, 1, &Dummy[8]); } // write 6 bytes with offset 2 from Dummy filed to slave memory if(tout()) { set_tout_ms(10); I2C_WriteRegister(2, 6, &Dummy[2]); } // read 6 bytes with offset 2 back from the image at slave memory if(tout()) { set_tout_ms(10); I2C_ReadRegister(2, 6, &Dummy[2]); } // write 1 byte with offset 9 from Dummy filed to slave memory if(tout()) { set_tout_ms(10); I2C_WriteRegister(9, 1, &Dummy[9]); } // read 1 byte with offset 9 back from the image at slave memory if(tout()) { set_tout_ms(10); I2C_ReadRegister(9, 1, &Dummy[9]); } // write 2 bytes with offset 0 from Dummy filed to slave memory if(tout()) { set_tout_ms(10); I2C_WriteRegister(0, 2, &Dummy[0]); } // read 2 bytes with offset 0 back from the image at slave memory if(tout()) { set_tout_ms(10); I2C_ReadRegister(0, 2, &Dummy[0]); } // if a timout error occures switch on LED2 if(!tout()) switch_on(LED2); // switch off LED1 at the end of test switch_off(LED1); // check if dummy field is not corrupted => switch on LED 4 if test not successful if(memcmp(Dummy, DUMMY_INIT, MAX_DUMMY) != 0) switch_on(LED4); delay(1); } }
uint_8 MAX3353_Read_Register(uint_8 u8Register) { return(I2C_ReadRegister(MAX3353_I2C_ADDRESS,u8Register)); }
void I2C2_ER_IRQHandler(void){ uint16_t sr1,sr2; sr1 = I2C_ReadRegister(I2C2,I2C_Register_SR2); sr2 = I2C_ReadRegister(I2C2,I2C_Register_SR1); }
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. */ }