/*! \brief Writes data from buffer. \param indata Pointer to data buffer \param bytes Number of bytes to transfer \param slave_adr Slave address on I2C bus \param slave_reg Slave memory address from which the reading is started \return 1 if successful, otherwise 0 */ u8 I2C_WriteBytes(u8* indata, u8 bytes, u8 slave_adr, u8 slave_reg) { u8 index, ack = 0; if(!I2C_StartCond()) { return 0; } if(!I2C_WriteByte((u8)(slave_adr | WRITE))) { return 0; } if(!I2C_WriteByte(slave_reg)) { return 0; } for(index = 0; index < bytes; index++) { ack = I2C_WriteByte(indata[index]); if(!ack) { break; } } //put stop here write_scl(1); DELAY_US(SCL_SDA_DELAY); SDA_HIGH; return ack; }
/** * @ Opis Rozpoczyna pomiar temperatury. * @ Parametry Brak. * @ Zwracana wartosc Brak. */ void BMP180_StartTemperature() { I2C_Start(); I2C_Write_SLA(BMP180_SLA); I2C_WriteByte(BMP180_CTRL_MEAS_REG); I2C_WriteByte(BMP180_T_MEASURE); I2C_Stop(); }
/** * @ Opis Rozpoczyna pomiar cisnienia. * @ Parametry Brak. * @ Zwracana wartosc Brak. */ void BMP180_StartPressure() { I2C_Start(); I2C_Write_SLA(BMP180_SLA); I2C_WriteByte(BMP180_CTRL_MEAS_REG); I2C_WriteByte(BMP180_Struct.BMP180_Mode); I2C_Stop(); }
/******************************************************************************* * Function Name : Sensor_Init * Description : Sensor初始化 * Input : None * Output : None * Return : None * Attention : 返回1成功,返回0失败 *******************************************************************************/ int Sensor_Init(void) { uint16_t i=0; uint8_t Sensor_IDCode = 0; I2C_Configuration(); if( 0 == I2C_WriteByte ( 0x12, 0x80 , ADDR_OV7670 ) ) /* Reset SCCB */ { return 0 ; } Delay(1500); if( 0 == I2C_ReadByte( &Sensor_IDCode, 1, 0x0b, ADDR_OV7670 ) ) /* read ID */ { return 0; /* error*/ } if(Sensor_IDCode == OV7670) /* ID = OV7670 */ { for( i=0 ; i < OV7670_REG_NUM ; i++ ) { if( 0 == I2C_WriteByte( OV7670_Reg[i][0], OV7670_Reg[i][1] , ADDR_OV7670 ) ) { return 0; } } } else /* NO ID */ { return 0; } return 1; }
void HotPlugService (void) { DisableInterrupts(0xFF); Active9232(); if ((HDCP_TxSupports == true)) { if (HDCP_AksvValid == true) { // AV MUTE TPI_DEBUG_PRINT (("TMDS -> Enabled (Video Muted)\n")); ReadModifyWriteTPI(0x1A, BIT_6 | BIT_4 | BIT_3, 0x40 | 0x00 | 0x08); tmdsPoweredUp = true; EnableInterrupts(BIT_0 | 0x02 | 0x10 | 0x20 | 0x40 | 0x80); } } else { TPI_DEBUG_PRINT (("TMDS -> Enabled\n")); ReadModifyWriteTPI(0x1A, BIT_6 | BIT_4 | BIT_3, 0x40 | 0x00 | 0x08); tmdsPoweredUp = TRUE; EnableInterrupts(0x01 | 0x02 | 0x10 | 0x40); } TxPowerStateD0(); I2C_WriteByte(TPI_SLAVE_ADDR, 0xcd, 0x0); I2C_WriteByte(TPI_SLAVE_ADDR, 0x19, 0x0); }
bool GetDDC_Access(uint8_t* SysCtrlRegVal) { uint8_t sysCtrl, TPI_ControlImage, DDCReqTimeout = T_DDC_ACCESS; sysCtrl = I2C_ReadByte(TPI_SLAVE_ADDR, 0x1A); *SysCtrlRegVal = sysCtrl; sysCtrl |= BIT_2; I2C_WriteByte(TPI_SLAVE_ADDR, 0x1A, sysCtrl); while (DDCReqTimeout--) { TPI_ControlImage = I2C_ReadByte(TPI_SLAVE_ADDR, 0x1A); if (TPI_ControlImage & BIT_1) { sysCtrl |= BIT_1; I2C_WriteByte(TPI_SLAVE_ADDR, 0x1A, sysCtrl); return true; } I2C_WriteByte(TPI_SLAVE_ADDR, 0x1A, sysCtrl); DelayMS(200); } I2C_WriteByte(TPI_SLAVE_ADDR, 0x1A, sysCtrl); return false; }
//************************************************************ //BU9792 refresh display unsigned char BU9792_Refresh(unsigned char * Src, unsigned int Len) { unsigned char temp = 0, i; Len = Len; //I2C_Stop(I2C_BUS_0); I2C_Start(I2C_BUS_0); while (I2C_WriteByte(BU9799_ADDR, I2C_BUS_0) && (temp < 218)) { I2C_Stop(I2C_BUS_0); DelayMs(1); I2C_Start(I2C_BUS_0); temp++; } if (temp >= 218) { I2C_Stop(I2C_BUS_0); return (0xff); } //I2C_WriteByte(LCD_ICSET_M|0x80,I2C_BUS_0) ; /*high address*/ I2C_WriteByte(LCD_ICSET_M | 0x80, I2C_BUS_0); /* 高地址 */ //I2C_WriteByte(0x00,I2C_BUS_0) ; /*send data low address*/ I2C_WriteByte(0x00, I2C_BUS_0); /* 发送数据低地址*/ for (i = 0; i < 18; i++) { //I2C_WriteByte(*Src++,I2C_BUS_0) ; /*send device address*/ I2C_WriteByte(*Src++, I2C_BUS_0); /* 发送器件地址 */ } I2C_Stop(I2C_BUS_0); return 0; }
//保存校准参数 //保存区域:FM24C16的 0~12这段地址区间,总共12个字节 void save_adjdata(void) { u32 temp; //保存校正结果! temp=penConfig.xfac*100000000;//保存x校正因素 I2C_WriteByte(temp&0xff, 0, ADDR_24LC08); I2C_WriteByte((temp>>8)&0xff, 1, ADDR_24LC08); I2C_WriteByte((temp>>16)&0xff, 2, ADDR_24LC08); I2C_WriteByte((temp>>24)&0xff, 3, ADDR_24LC08); temp=penConfig.yfac*100000000;//保存y校正因素 I2C_WriteByte(temp&0xff, 4, ADDR_24LC08); I2C_WriteByte((temp>>8)&0xff, 5, ADDR_24LC08); I2C_WriteByte((temp>>16)&0xff, 6, ADDR_24LC08); I2C_WriteByte((temp>>24)&0xff, 7, ADDR_24LC08); //保存x偏移量 I2C_WriteByte(penConfig.xoff&0xff,8,ADDR_24LC08); I2C_WriteByte((penConfig.xoff>>8)&0xff,9,ADDR_24LC08); //保存y偏移量 I2C_WriteByte(penConfig.yoff&0xff,10,ADDR_24LC08); I2C_WriteByte((penConfig.yoff>>8)&0xff,11,ADDR_24LC08); I2C_WriteByte(0xff,12,ADDR_24LC08); //标记校准过了 }
// send address via IIC. BOOL I2C_SendAddr(BYTE DevAddr, WORD Addr, BYTE ReadWriteFlag, BYTE I2cDevice) { I2C_Start(I2cDevice); if(!I2C_WriteByte(DevAddr, I2cDevice)) { WaitMs(EEPROM_WRITE_TIME); // Wairt for Programming-time. I2C_Start(I2cDevice); if(!I2C_WriteByte(DevAddr, I2cDevice))// | PageAddr)) { I2C_Stop(I2cDevice); return FALSE; } } if(!I2C_WriteByte((BYTE)Addr, I2cDevice)) { I2C_Stop(I2cDevice); return FALSE; } if(ReadWriteFlag == IIC_READ) { I2C_Start(I2cDevice); if(!I2C_WriteByte((DevAddr | IIC_READ), I2cDevice)) { I2C_Stop(I2cDevice); return FALSE; } } return TRUE; }
/* @brief 设置时间. @param None. @retval None. */ void FM31256_SetTime (unsigned char *data) { /* 读时间 */ I2C_WriteByte(REG_WRITE,FLAGS,0x2); I2C_WriteSByte (REG_WRITE, SECONDS, data, 7); /* 读完要恢复0 */ I2C_WriteByte(REG_WRITE,FLAGS,0); }
/* @brief 获取时间 BCD码. @param None. @retval None. */ void FM31256_GetTime (unsigned char *data) { /* 读时间 */ I2C_WriteByte(REG_WRITE,FLAGS,1); I2C_ReadSByte (REG_READ, SECONDS, data, 7); /* 读完要恢复0 */ I2C_WriteByte(REG_WRITE,FLAGS,0); }
/* *========================================================= * 函数功能:写MPU6050寄存器 * 参数: * Regs_Addr - 寄存器地址 * Regs_Data - 寄存器值 * 函数返回值: *========================================================= */ void Single_WriteI2C(unsigned char Regs_Addr,unsigned char Regs_Data) { I2C_Start(); I2C_WriteByte(SlaveAddress); //先写Slave地址,并配置成写模式 I2C_WriteByte(Regs_Addr); //写寄存器地址 I2C_WriteByte(Regs_Data); //写寄存器内容 I2C_Stop(); //结束本段IIC进程 }
//============================================================================== //Sets Information in the RTC. //byte0: Sets "Word Address". //byte1: Sets the information. //============================================================================== void DS3231_SetInfo(unsigned char byte0, unsigned char byte1){ I2C_Start(); I2C_WriteByte(0xD0); I2C_WriteByte(byte0); I2C_WriteByte(byte1); I2C_Stop(); Nop(); Nop(); }
// 磁力计初始化 void HMC5883L_Init(void) { // 设置标准数据输出速率为75HZ I2C_WriteByte(HMC5883L_Addr, HMC5883L_ConfigurationRegisterA, 0x18); // 设置采样频率为+-1.3Ga I2C_WriteByte(HMC5883L_Addr, HMC5883L_ConfigurationRegisterB, 0x20); // 开启连续测量模式 I2C_WriteByte(HMC5883L_Addr, HMC5883L_ModeRegister, 0x00); }
//============================================================================== //Gets information of RTC. //byte 0: Sets the "Word Address" //byte1: Return the information. //============================================================================== unsigned char DS3231_GetInfo(unsigned char byte0){ unsigned char byte1 = 0; I2C_Start(); I2C_WriteByte(0xD0); I2C_WriteByte(byte0); I2C_RepeatedStart(); I2C_WriteByte(0xD1); byte1 = I2C_ReadByte(0); I2C_Stop(); return(byte1); }
void ReadSector(char SectorNum, unsigned char *SectorData) { unsigned char RxBuffer[10]; unsigned char IDCode; // needed only if read procedure is different between cut. Today the procedure is identical //read sector RxBuffer[0]=SectorNum; I2C_WriteByte(0xE0,0x45,RxBuffer,1); RxBuffer[0]=0xC9; I2C_WriteByte(0xE0,0x46,RxBuffer,1); msleep(5); // wait I2C_ReadByte(0xE0,0x20,SectorData,8); //read RAM }
unsigned char initRTCmodule(unsigned char devADDR){ unsigned char btemp; Nop(); Nop(); // I2C_Close(); I2C_Init(39); // I2C 100 KHZ, 16 MHZ OSC (see note in "myI2C.c") Nop(); Nop(); // I2C_Start(); I2C_Idle(); btemp = I2C_WriteByte(devADDR); Nop(); Nop(); Nop(); if (btemp != 0){ Nop(); Nop(); I2C_Stop(); I2C_Close(); return 0xFF; } else { btemp = DS3231_GetInfo(0x0F); Nop(); Nop(); return btemp; }; }
int I2C_ReadRepeated(I2C_ConfigStruct* conf, uint8_t addr, uint8_t* data, uint32_t len) { int i; I2C_Start(conf); //Write addr | read command if(I2C_WriteByte(conf, (addr | 1)) != 0) { printf("Received NACK at address phase!!\r\n"); return -1; } //Read data for(i=0; i<len; i++) { data[i] = I2C_ReadByte(conf); if( i == (len - 1) ) I2C_SendNACK(conf); else I2C_SendACK(conf); } return 0;//success }
static bool i2c_imx_write(struct device *dev, u8_t *txBuffer, u8_t txSize) { I2C_Type *base = DEV_BASE(dev); struct i2c_imx_data *data = DEV_DATA(dev); struct i2c_master_transfer *transfer = &data->transfer; transfer->isBusy = true; /* Clear I2C interrupt flag to avoid spurious interrupt */ I2C_ClearStatusFlag(base, i2cStatusInterrupt); /* Set I2C work under Tx mode */ I2C_SetDirMode(base, i2cDirectionTransmit); transfer->currentDir = i2cDirectionTransmit; transfer->txBuff = txBuffer; transfer->txSize = txSize; I2C_WriteByte(base, *transfer->txBuff); transfer->txBuff++; transfer->txSize--; /* Enable I2C interrupt, subsequent data transfer will be handled * in ISR. */ I2C_SetIntCmd(base, true); /* Wait for the transfer to complete */ k_sem_take(&data->device_sync_sem, K_FOREVER); return transfer->ack; }
I2C_Status PressureSensorRegRead(uint8_t reg, uint8_t* val) { I2C_Status retVal = I2C_OK; do { I2C_Start(ALB_I2C, MPL311_ADDR, 0); retVal = I2C_WriteByte(ALB_I2C, reg); if(retVal != I2C_OK) { break; } retVal = I2C_WaitForTX(ALB_I2C); if(retVal != I2C_OK) { break; } I2C_Start(ALB_I2C, MPL311_ADDR, 1); if((retVal = I2C_ReadBytes(ALB_I2C, val, 1)) != I2C_OK) { break; } I2C_Stop(ALB_I2C); } while(0); return retVal; }
static void InitForceUsbIdSwitchOpen (void) { I2C_WriteByte(0x72, 0x90, 0x26); // Disable CBUS discovery ReadModifyWriteTPI(0x95, SI_BIT_6, SI_BIT_6); // Force USB ID switch to open ReadModifyWriteTPI(0x95, SI_BIT_6, SI_BIT_6); // Force USB ID switch to open WriteByteTPI(0x92, 0x46); // Force MHD mode }
I2C_Status AccelerometerRegRead(uint8_t reg, uint8_t* val) { I2C_Status retVal = I2C_OK; do { I2C_Start(ALB_I2C, LIS2DH_ADDR, 0); retVal = I2C_WriteByte(ALB_I2C, reg); if(retVal != I2C_OK) { break; } retVal = I2C_WaitForTX(ALB_I2C); if(retVal != I2C_OK) { break; } I2C_Start(ALB_I2C, LIS2DH_ADDR, 1); if((retVal = I2C_ReadBytes(ALB_I2C, val, 1)) != I2C_OK) { break; } I2C_Stop(ALB_I2C); } while(0); return retVal; }
// Read the altimeter. This function will // block the calling task until the entire sensor read has been completed I2C_Status ReadPressureSensor(uint32_t* altOut) { I2C_Status retVal = I2C_OK; uint32_t temp = 0; taskENTER_CRITICAL(); do { retVal = PressureSensorRegWrite(MPL3115A2_CTRL_REG1, MPL3115A2_CTRL_REG1_SBYB | MPL3115A2_CTRL_REG1_OS128 | MPL3115A2_CTRL_REG1_ALT); if(retVal != I2C_OK) { break; } uint8_t sta = 0; while (! (sta & MPL3115A2_REGISTER_STATUS_PDR)) { retVal = PressureSensorRegRead(MPL3115A2_REGISTER_STATUS, &sta); // TODO: a delay here would be nice to prevent slamming the I2C bus } I2C_Start(ALB_I2C, MPL311_ADDR, 0); retVal = I2C_WriteByte(ALB_I2C, MPL3115A2_REGISTER_PRESSURE_MSB); if(retVal != I2C_OK) { break; } retVal = I2C_WaitForTX(ALB_I2C); if(retVal != I2C_OK) { break; } I2C_Start(ALB_I2C, MPL311_ADDR, 1); if((retVal = I2C_ReadBytes(ALB_I2C, i2cRxBuffer, 3)) != I2C_OK) { break; } I2C_Stop(ALB_I2C); // Process returned data temp = i2cRxBuffer[0] << 24; temp |= i2cRxBuffer[1] << 16; temp |= i2cRxBuffer[2] << 8; *altOut = temp; } while(0); taskEXIT_CRITICAL(); return retVal; }
BOOL I2cWritePT2313(BYTE ivol) { BOOL acktemp = 0; #if defined(FUNC_PT231X_IIC)||defined(AU6210K_NR_D_8_CSRBT) I2C_Start(PT231X_IIC); acktemp = I2C_WriteByte(PT231X_AD,PT231X_IIC); acktemp |= I2C_WriteByte(ivol,PT231X_IIC); I2C_Stop(PT231X_IIC); #else I2C_Start(EEPROM_IIC); acktemp = I2C_WriteByte(PT231X_AD,EEPROM_IIC); acktemp |= I2C_WriteByte(ivol,EEPROM_IIC); I2C_Stop(EEPROM_IIC); #endif return acktemp; }
void Set_NDOF_mode() { //Failure flag uint8_t failedWrite = 0; //Set IMU to NDOF mode I2C_WriteByte(0x28, 0x3d, 0b00001100, PIO_Data, PIO_DDIR, &failedWrite); }
void InitCBusRegs(void) { I2C_WriteByte(0xC8, 0x1F, 0x02); // Heartbeat Max Fail Enable I2C_WriteByte(0xC8, 0x07, DDC_XLTN_TIMEOUT_MAX_VAL | 0x06); // Increase DDC translation layer timer I2C_WriteByte(0xC8, 0x40, 0x03); // CBUS Drive Strength I2C_WriteByte(0xC8, 0x42, 0x06); // CBUS DDC interface ignore segment pointer I2C_WriteByte(0xC8, 0x36, 0x0C); //I2C_WriteByte(0xC8, 0x44, 0x02); I2C_WriteByte(0xC8, 0x3D, 0xFD); I2C_WriteByte(0xC8, 0x1C, 0x00); I2C_WriteByte(0xC8, 0x44, 0x00); I2C_WriteByte(0xC8, 0x09, 0x60); // Enable PVC Xfer aborted / follower aborted }
/* *========================================================= * 函数功能:读MPU6050单字节 * 参数 * Regs_Addr - 寄存器地址 * 函数返回值:寄存器值 *========================================================= */ unsigned char Single_ReadI2C(unsigned char Regs_Addr) { unsigned char ret; I2C_Start(); I2C_WriteByte(SlaveAddress); //先写Slave地址,并配置成写模式 I2C_WriteByte(Regs_Addr); //写寄存器地址 I2C_Start(); I2C_WriteByte(SlaveAddress+1); //写Slave地址,并配置成读模式 ret=I2C_ReadByte(); //从传感器中读出数据 I2C_SlaveAck(); I2C_Stop(); //结束本段IIC进程 return ret; }
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; }
static void InitCBusRegs(void) { I2C_WriteByte(CBUS_SLAVE_ADDR, 0x1F, 0x02); I2C_WriteByte(CBUS_SLAVE_ADDR, 0x07, 0x30 | 0x06); I2C_WriteByte(CBUS_SLAVE_ADDR, 0x40, 0x03); I2C_WriteByte(CBUS_SLAVE_ADDR, 0x42, 0x06); I2C_WriteByte(CBUS_SLAVE_ADDR, 0x36, 0x0C); I2C_WriteByte(CBUS_SLAVE_ADDR, 0x3D, 0xFD); I2C_WriteByte(CBUS_SLAVE_ADDR, 0x1C, 0x00); I2C_WriteByte(CBUS_SLAVE_ADDR, 0x44, 0x02); I2C_WriteByte(CBUS_SLAVE_ADDR, 0x09, 0x60); }
bool ReleaseDDC(uint8_t SysCtrlRegVal) { uint8_t DDCReqTimeout = T_DDC_ACCESS, TPI_ControlImage; SysCtrlRegVal &= ~(0x6); while (DDCReqTimeout--) { I2C_WriteByte(TPI_SLAVE_ADDR, 0x1A, SysCtrlRegVal); TPI_ControlImage = I2C_ReadByte(TPI_SLAVE_ADDR, 0x1A); if (!(TPI_ControlImage & 0x6)) return true; } return false; }