I2C_Status AccelerometerRegWrite(uint8_t reg, uint8_t val) { I2C_Status retVal = I2C_OK; uint8_t tmp[2]; tmp[0] = reg; tmp[1] = val; do { I2C_Start(ALB_I2C, LIS2DH_ADDR, 0); retVal = I2C_WriteBytes(ALB_I2C, tmp, 2); if(retVal != I2C_OK) { break; } retVal = I2C_WaitForTX(ALB_I2C); if(retVal != I2C_OK) { break; } I2C_Stop(ALB_I2C); } while(0); return retVal; }
I2C_Status PressureSensorRegWrite(uint8_t reg, uint8_t val) { I2C_Status retVal = I2C_OK; uint8_t tmp[2]; tmp[0] = reg; tmp[1] = val; do { I2C_Start(ALB_I2C, MPL311_ADDR, 0); retVal = I2C_WriteBytes(ALB_I2C, tmp, 2); if(retVal != I2C_OK) { break; } retVal = I2C_WaitForTX(ALB_I2C); if(retVal != I2C_OK) { break; } I2C_Stop(ALB_I2C); } while(0); return retVal; }
I2C_Status InitTempSensor(uint8_t index) { I2C_Status retVal = I2C_OK; // Write the config values into the TX buffer i2cTxBuffer[0] = TMP102_CONFIG_1_VAL; i2cTxBuffer[1] = TMP102_CONFIG_2_VAL; taskENTER_CRITICAL(); // TODO: log something if the sensor fails to initialize do { // I2C Write I2C_Start(SLB_I2C, GetTmp102Addr(index), 0); retVal = I2C_WriteByte(SLB_I2C, TMP102_CONFIG_ADDR); if(retVal != I2C_OK) { break; } retVal = I2C_WaitForTX(SLB_I2C); if(retVal != I2C_OK) { break; } I2C_Stop(SLB_I2C); // I2C Write I2C_Start(SLB_I2C, GetTmp102Addr(index), 0); retVal = I2C_WriteBytes(SLB_I2C, i2cTxBuffer, 2); if(retVal != I2C_OK) { break; } retVal = I2C_WaitForTX(SLB_I2C); if(retVal != I2C_OK) { break; } I2C_Stop(SLB_I2C); // I2C Read I2C_Start(SLB_I2C, GetTmp102Addr(index), 1); if((retVal = I2C_ReadBytes(SLB_I2C, i2cRxBuffer, 2)) != I2C_OK) { break; } I2C_Stop(SLB_I2C); }while(0); taskEXIT_CRITICAL(); return retVal; }
void mpu6050_enable() { uint8_t data = 0; bool res; res = I2C_WriteBytes(I2C2, &data, MPU_ADDR,0x6B, 1); if (!res) { LEDOn(LED_ERR); } }
// Write data to eeprom from buffer BOOL I2C_WriteNByte(BYTE DevAddr, WORD Addr, BYTE* Buf, BYTE Len, BYTE I2cDevice) { BOOL Ack = FALSE; #if (defined(FUNC_BREAK_POINT_EN) && (FUNC_RESTORE_DEVICE_SELECE == FUNC_RESTORE_DEVICE_EEPROM)) if(((gSys.SystemMode == SYS_MODE_USB) || (gSys.SystemMode == SYS_MODE_SD)) && (I2cDevice == EEPROM_IIC)) { #if (I2C_PIN_CONFIGURE == SDIO_TO_A3A4A5) SET_CARD_TO_A3A4A5(); #if defined(SD_DETECT_PIN_USE_A4) ClrGpioRegBit(CARD_DETECT_PORT_PD, CARD_DETECT_BIT); #endif #endif //写E2PROM时快速响应需要读卡时需要重新将复用IO设置回总线模式 SongPlayDo(); #if (I2C_PIN_CONFIGURE == SDIO_TO_A3A4A5) #if defined(SD_DETECT_PIN_USE_A4) SetGpioRegBit(CARD_DETECT_PORT_PD, CARD_DETECT_BIT); #endif SET_CARD_NOT_TO_GPIO(); #endif KeyEventGet(); } #endif Ack = I2C_SendAddr(DevAddr, Addr, IIC_WRITE, I2cDevice); if(Ack == TRUE) { Ack = I2C_WriteBytes(Buf, Len, I2cDevice); } I2C_Stop(I2cDevice); // DBG_APP(("I2C_WriteNByte, ACK: %bu\n", (BYTE)Ack)); return Ack; }
// Read one of the temperature sensors. This function will // block the calling task until the entire sensor read has been completed I2C_Status ReadTempSensor(uint8_t index, uint16_t* tempOut) { I2C_Status retVal = I2C_OK; uint16_t temp = 0; taskENTER_CRITICAL(); do { // Write to the i2cTxBuffer i2cTxBuffer[0] = TMP102_CONFIG_1_ONESHOT_VAL; i2cTxBuffer[1] = TMP102_CONFIG_2_VAL; // Write to the config register to begin a one-shot temperature conversion // I2C Write I2C_Start(SLB_I2C, GetTmp102Addr(index), 0); if((retVal = I2C_WriteByte(SLB_I2C, TMP102_CONFIG_ADDR)) != I2C_OK) { break; } if((retVal = I2C_WaitForTX(SLB_I2C)) != I2C_OK) { break; } I2C_Stop(SLB_I2C); // I2C Write I2C_Start(SLB_I2C, GetTmp102Addr(index), 0); if((retVal = I2C_WriteBytes(SLB_I2C, i2cTxBuffer, 2)) != I2C_OK) { break; } if((retVal = I2C_WaitForTX(SLB_I2C)) != I2C_OK) { break; } I2C_Stop(SLB_I2C); // Read the temperature registers from the sensor // I2C Write I2C_Start(SLB_I2C, GetTmp102Addr(index), 0); if((retVal = I2C_WriteByte(SLB_I2C, TMP102_TEMP_ADDR)) != I2C_OK) { break; } if((retVal = I2C_WaitForTX(SLB_I2C)) != I2C_OK) { break; } I2C_Stop(SLB_I2C); // I2C Read I2C_Start(SLB_I2C, GetTmp102Addr(index), 1); if((retVal = I2C_ReadBytes(SLB_I2C, i2cRxBuffer, 2)) != I2C_OK) { break; } I2C_Stop(SLB_I2C); // Process returned data temp = (((uint16_t)i2cRxBuffer[0]) << 4) & 0xFFF0; temp |= (((uint16_t)i2cRxBuffer[1]) >> 4) & 0x000F; *tempOut = temp; } while(0); taskEXIT_CRITICAL(); return retVal; }