uint8_t u8g_com_ssd_start_sequence(u8g_t *u8g) { /* are we requested to set the a0 state? */ if ( U8G_SSD_A0_SET == 0 ) return 1; /* setup bus, might be a repeated start */ I2C_Start_NoAck(); I2C_SendByte( U8G_I2C_ADDR<< 1 | I2C_Direction_Transmitter); I2C_NoAck(); if ( U8G_SSD_A0_STATE == 0 ) { I2C_SendByte(U8G_I2C_CMD_MODE); I2C_NoAck(); } else { I2C_SendByte(U8G_I2C_DATA_MODE); I2C_NoAck(); } U8G_SSD_A0_SET = 0; return 1; }
bool I2C_Write_NoAck(uint8_t addr, uint8_t reg, uint8_t data) { if (!I2C_Start_NoAck()) return false; I2C_SendByte(addr << 1 | I2C_Direction_Transmitter); I2C_NoAck(); //I2C_SendByte(reg); //I2C_NoAck(); I2C_SendByte(data); I2C_NoAck(); I2C_Stop(); return true; }
bool i2cRead(uint8_t addr, uint8_t reg, uint8_t len, uint8_t *buf) { if (!I2C_Start()) return false; I2C_SendByte(addr << 1 | I2C_Direction_Transmitter); if (!I2C_WaitAck()) { I2C_Stop(); return false; } I2C_SendByte(reg); I2C_WaitAck(); I2C_Start(); I2C_SendByte(addr << 1 | I2C_Direction_Receiver); I2C_WaitAck(); while (len) { *buf = I2C_ReceiveByte(); if (len == 1) I2C_NoAck(); else I2C_Ack(); buf++; len--; } I2C_Stop(); return true; }
bool i2cRead(I2CDevice device, uint8_t addr, uint8_t reg, uint8_t len, uint8_t *buf) { UNUSED(device); if (!I2C_Start()) { return false; } I2C_SendByte(addr << 1 | I2C_Direction_Transmitter); if (!I2C_WaitAck()) { I2C_Stop(); i2cErrorCount++; return false; } I2C_SendByte(reg); I2C_WaitAck(); I2C_Start(); I2C_SendByte(addr << 1 | I2C_Direction_Receiver); I2C_WaitAck(); while (len) { *buf = I2C_ReceiveByte(); if (len == 1) { I2C_NoAck(); } else { I2C_Ack(); } buf++; len--; } I2C_Stop(); return true; }
void HAL_I2C_NoAck(void) { #ifdef USE_I2C_OLD I2C_NoAck(); #else IIC_NAck(); #endif }
bool I2C_WriteBuffer_NoAck(uint8_t addr, uint8_t reg, uint8_t len, uint8_t * data) { int i; if (!I2C_Start_NoAck()) return false; I2C_SendByte(addr << 1 | I2C_Direction_Transmitter); I2C_NoAck(); //I2C_SendByte(reg); //I2C_NoAck(); for (i = 0; i < len; i++) { I2C_SendByte(data[i]); I2C_NoAck(); } I2C_Stop(); return true; }
/*********************************************************** * Function: // 函数名称 * Description: // 函数功能、性能等的描述 * Input: // 1.输入参数1,说明,包括每个参数的作用、取值说明及参数间关系 * Input: // 2.输入参数2,说明,包括每个参数的作用、取值说明及参数间关系 * Output: // 1.输出参数1,说明 * Return: // 函数返回值的说明 * Others: // 其它说明 ***********************************************************/ static uint8_t IIC_RegRead( uint8_t address, uint8_t reg, uint8_t *value ) { uint8_t err = ERR_NONE; err = I2C_Start( ); if( err ) { goto lbl_ERR_REG_RD_STOP; } I2C_SendByte( address << 1 ); err = I2C_WaitAck( ); if( err ) { goto lbl_ERR_REG_RD_STOP; } I2C_SendByte( reg ); err = I2C_WaitAck( ); if( err ) { goto lbl_ERR_REG_RD_STOP; } err = I2C_Start( ); if( err ) { goto lbl_ERR_REG_RD; } I2C_SendByte( ( address << 1 ) | 0x01 ); err = I2C_WaitAck( ); if( err ) { goto lbl_ERR_REG_RD_STOP; } *value = I2C_ReceiveByte( ); I2C_NoAck( ); I2C_Stop( ); return ERR_NONE; lbl_ERR_REG_RD_STOP: I2C_Stop( ); lbl_ERR_REG_RD: //rt_kprintf( "reg_rd error=%02x reg=%02x\r\n", err, reg ); return err; }
/***********************单字节读取******************/ uint8_t MPU6050_ReadByte(uint8_t SlaveAddress,uint8_t REG_Address) { unsigned char REG_data; I2C_Start();//if(!I2C_Start())return 0 I2C_SendByte(SlaveAddress); //I2C_SendByte(((REG_Address & 0x0700) >>7) | REG_Address & 0xFFFE);//设置高起始地址+器件地址 I2C_WaitAck();//if(!I2C_WaitAck()){I2C_Stop();test=1; return 0;} I2C_SendByte((uint8_t) REG_Address); //设置低起始地址 I2C_WaitAck(); //if(!I2C_WaitAck()){I2C_Stop(); return 0;} I2C_Start();//if(!I2C_Start())return 0; I2C_SendByte(SlaveAddress+1); I2C_WaitAck();//if(!I2C_WaitAck()){I2C_Stop(); return 0;} REG_data= I2C_ReadByte(); I2C_NoAck(); I2C_Stop(); return REG_data; }
/****************************************************************************** / 函数功能:单字节写入 / 修改日期:none / 输入参数: / @arg SlaveAddress 从器件地址 / @arg REG_Address 寄存器地址 / 输出参数: 读出的字节数据 / 使用说明:这时一个完整的单字节读取函数 ******************************************************************************/ uint8_t Single_Read(uint8_t SlaveAddress,uint8_t REG_Address) { uint8_t REG_data; I2C_Start();//if(!I2C_Start())return 0 I2C_SendByte(SlaveAddress); //I2C_SendByte(((REG_Address & 0x0700) >>7) | REG_Address & 0xFFFE);//设置高起始地址+器件地址 I2C_WaitAck();//if(!I2C_WaitAck()){I2C_Stop();return 0;} I2C_SendByte((uint8_t) REG_Address); //设置低起始地址 I2C_WaitAck();//if(!I2C_WaitAck()){I2C_Stop();return 0;} I2C_Start();//if(!I2C_Start())return 0; I2C_SendByte(SlaveAddress+1); I2C_WaitAck();//if(!I2C_Start())return 0; REG_data = I2C_ReadByte(); I2C_NoAck(); I2C_Stop(); return REG_data; }
//单字节读取***************************************** unsigned char Single_Read(unsigned char SlaveAddress,unsigned char REG_Address) { unsigned char REG_data; if(!I2C_Start())return FALSE; I2C_SendByte(SlaveAddress); //I2C_SendByte(((REG_Address & 0x0700) >>7) | REG_Address & 0xFFFE);//设置高起始地址+器件地址 if(!I2C_WaitAck()){I2C_Stop(); return FALSE;} I2C_SendByte((u8) REG_Address); //设置低起始地址 I2C_WaitAck(); I2C_Start(); I2C_SendByte(SlaveAddress+1); I2C_WaitAck(); REG_data= I2C_ReadByte(); I2C_NoAck(); I2C_Stop(); return REG_data; }
u8 ReadI2C(u8 address, u8 WriteAddress) // 读出1串数据 { u8 ch; // 定义存储读出数据的临时变量; I2C_Start(); // 启动总线,开始传输数据; I2C_SendByte(WriteAddress); I2C_WaitAck(); // 发送从器件硬件地址; I2C_SendByte(address); I2C_WaitAck(); // 发送从器件内部数据存储器的地址; I2C_Start(); // 重新启动总线,开始传输数据; I2C_SendByte(WriteAddress + 1); I2C_WaitAck(); // 发送从器件内部数据存储器的地址; ch = I2C_ReceiveByte(); I2C_NoAck(); // 将读出的一个字节数据存入临时变量,发送非应答位; I2C_Stop(); // 发送停止信号,释放总路线; return (ch); }
/******************************************************************************* * Function Name : I2C_ReadByte * Description : 读取一串数据 * Input : - pBuffer: 存放读出数据 * - length: 待读出长度 * - ReadAddress: 待读出地址 * - DeviceAddress: 器件类型 * Output : None * Return : 返回为:=1成功读入,=0失败 * Attention : None *******************************************************************************/ int I2C_ReadByte(uint8_t* pBuffer, uint16_t length, uint8_t ReadAddress, uint8_t DeviceAddress) { if(!I2C_Start()) { return DISABLE; } I2C_SendByte( DeviceAddress ); /* 器件地址 */ if( !I2C_WaitAck() ) { I2C_Stop(); return DISABLE; } I2C_SendByte( ReadAddress ); /* 设置低起始地址 */ I2C_WaitAck(); I2C_Stop(); if(!I2C_Start()) { return DISABLE; } I2C_SendByte( DeviceAddress + 1 ); /* 器件地址 */ if(!I2C_WaitAck()) { I2C_Stop(); return DISABLE; } while(length) { *pBuffer = I2C_ReceiveByte(); if(length == 1) { I2C_NoAck(); } else { I2C_Ack(); } pBuffer++; length--; } I2C_Stop(); return ENABLE; }
/*--------------------------------------------------------------------------- TITLE : u8g_com_sw_i2c_fn WORK : ARG : void RET : void ---------------------------------------------------------------------------*/ uint8_t u8g_com_sw_i2c_fn(u8g_t *u8g, uint8_t msg, uint8_t arg_val, void *arg_ptr) { switch(msg) { case U8G_COM_MSG_STOP: break; case U8G_COM_MSG_INIT: //-- I2C 초기화 // I2C_SW_Init(NULL); I2C_Stop(); break; case U8G_COM_MSG_CHIP_SELECT: U8G_SSD_A0_STATE = 0; U8G_SSD_A0_SET = 1; if( arg_val == 0 ) { I2C_Stop(); } break; case U8G_COM_MSG_RESET: break; case U8G_COM_MSG_WRITE_BYTE: if ( u8g_com_ssd_start_sequence(u8g) == 0 ) { I2C_Stop(); return 0; } I2C_SendByte(arg_val); I2C_NoAck(); break; case U8G_COM_MSG_WRITE_SEQ: if ( u8g_com_ssd_start_sequence(u8g) == 0 ) { I2C_Stop(); return 0; } { register uint8_t *ptr = arg_ptr; while( arg_val > 0 ) { I2C_SendByte(*ptr++); I2C_NoAck(); arg_val--; } } break; case U8G_COM_MSG_WRITE_SEQ_P: if ( u8g_com_ssd_start_sequence(u8g) == 0 ) { I2C_Stop(); return 0; } { register uint8_t *ptr = arg_ptr; while( arg_val > 0 ) { I2C_SendByte(*ptr); I2C_NoAck(); ptr++; arg_val--; } } break; case U8G_COM_MSG_ADDRESS: /* define cmd (arg_val = 0) or data mode (arg_val = 1) */ U8G_SSD_A0_STATE = arg_val; U8G_SSD_A0_SET = 1; break; } return 1; }