uint8_t TW88Write(uint8_t regAddr, uint8_t data) { int8_t retries; retries=3; while(I2C_GetFlagStatus(I2C1, I2C_ISR_BUSY) != RESET) { if ((retries--)>0) Delay(1); else return 0;} //I2C_TransferHandling(I2C1, TW88_I2C_ADDR, 1, I2C_SoftEnd_Mode, I2C_Generate_Start_Write); /* Configure slave address, nbytes, reload, end mode and start or stop generation */ I2C_TransferHandling(I2C1, TW88_I2C_ADDR, 1, I2C_Reload_Mode, I2C_Generate_Start_Write); retries=3; while(I2C_GetFlagStatus(I2C1, I2C_ISR_TXIS) == RESET) {if ((retries--)>0) Delay(1); else return 0; } //send the register address I2C_SendData(I2C1, (uint8_t) regAddr); retries=3; while(I2C_GetFlagStatus(I2C1, I2C_ISR_TCR) == RESET){if ((retries--)>0) Delay(1); else return 0;} /* Configure slave address, nbytes, reload, end mode and start or stop generation */ I2C_TransferHandling(I2C1, TW88_I2C_ADDR, 1, I2C_AutoEnd_Mode, I2C_No_StartStop); retries=3; while(I2C_GetFlagStatus(I2C1, I2C_ISR_TXIS) == RESET) {if ((retries--)>0) Delay(1); else return 0;} I2C_SendData(I2C1, data); retries=3; while(I2C_GetFlagStatus(I2C1, I2C_ISR_STOPF) == RESET) {if ((retries--)>0) Delay(1); else return 0;} I2C_ClearFlag(I2C1, I2C_ICR_STOPCF); return 1; }
// Make this so we can handle if the nunchuk is disconnected void f3d_i2c1_read_nunchuk (uint8_t device, uint8_t* buffer, uint16_t numbytes) { tryagain: ; int count = 100; while (I2C_GetFlagStatus(I2C1, I2C_ISR_BUSY) != RESET){ } I2C_TransferHandling(I2C1, 0xA4, 1, I2C_AutoEnd_Mode, I2C_Generate_Start_Write); // This is where were screwing up while ((I2C_GetFlagStatus(I2C1, I2C_ISR_TXIS) == RESET) && count){ asm("nop"); count--; } if (count <= 0) { goto tryagain; } I2C_SendData(I2C1,0x00); while(I2C_GetFlagStatus(I2C1, I2C_ISR_STOPF) == RESET){ } I2C_ClearFlag(I2C1, I2C_ICR_STOPCF); delay(1); while (I2C_GetFlagStatus(I2C1, I2C_ISR_BUSY) != RESET) { } I2C_TransferHandling(I2C1, 0xA4, 6, I2C_AutoEnd_Mode, I2C_Generate_Start_Read); while (numbytes--) { while(I2C_GetFlagStatus(I2C1, I2C_ISR_RXNE) == RESET); *buffer++ = I2C_ReceiveData(I2C1); } while (I2C_GetFlagStatus(I2C1, I2C_ISR_STOPF) == RESET); I2C_ClearFlag(I2C1, I2C_ICR_STOPCF); }
/** * @brief Write to the configuration register of the LM75. * @param RegValue: specifies the value to be written to LM75 configuration * register. * @retval None */ uint8_t LM75_WriteConfReg(uint8_t RegValue) { uint8_t LM75_BufferTX = 0; LM75_BufferTX = (uint8_t)(RegValue); /* Test on BUSY Flag */ LM75Timeout = LM75_LONG_TIMEOUT; while(I2C_GetFlagStatus(LM75_I2C, I2C_ISR_BUSY) != RESET) { if((LM75Timeout--) == 0) return LM75_TIMEOUT_UserCallback(); } /* Configure slave address, nbytes, reload, end mode and start or stop generation */ I2C_TransferHandling(LM75_I2C, LM75_ADDR, 1, I2C_Reload_Mode, I2C_Generate_Start_Write); /* Wait until TXIS flag is set */ LM75Timeout = LM75_LONG_TIMEOUT; while(I2C_GetFlagStatus(LM75_I2C, I2C_ISR_TXIS) == RESET) { if((LM75Timeout--) == 0) return LM75_TIMEOUT_UserCallback(); } /* Send Register address */ I2C_SendData(LM75_I2C, (uint8_t)LM75_REG_CONF); /* Wait until TCR flag is set */ LM75Timeout = LM75_LONG_TIMEOUT; while(I2C_GetFlagStatus(LM75_I2C, I2C_ISR_TCR) == RESET) { if((LM75Timeout--) == 0) return LM75_TIMEOUT_UserCallback(); } /* Configure slave address, nbytes, reload, end mode and start or stop generation */ I2C_TransferHandling(LM75_I2C, LM75_ADDR, 1, I2C_AutoEnd_Mode, I2C_No_StartStop); /* Wait until TXIS flag is set */ LM75Timeout = LM75_LONG_TIMEOUT; while(I2C_GetFlagStatus(LM75_I2C, I2C_ISR_TXIS) == RESET) { if((LM75Timeout--) == 0) return LM75_TIMEOUT_UserCallback(); } /* Write data to TXDR */ I2C_SendData(LM75_I2C, (uint8_t)LM75_BufferTX); /* Wait until STOPF flag is set */ LM75Timeout = LM75_LONG_TIMEOUT; while(I2C_GetFlagStatus(LM75_I2C, I2C_ISR_STOPF) == RESET) { if((LM75Timeout--) == 0) return LM75_TIMEOUT_UserCallback(); } /* Clear STOPF flag */ I2C_ClearFlag(LM75_I2C, I2C_ICR_STOPCF); return LM75_OK; }
/** * @brief Read the configuration register from the LM75. * @param None * @retval LM75 configuration register value. */ uint8_t LM75_ReadConfReg(void) { uint8_t LM75_BufferRX[2] ={0,0}; /* Test on BUSY Flag */ LM75Timeout = LM75_LONG_TIMEOUT; while(I2C_GetFlagStatus(LM75_I2C, I2C_ISR_BUSY) != RESET) { if((LM75Timeout--) == 0) return LM75_TIMEOUT_UserCallback(); } /* Configure slave address, nbytes, reload, end mode and start or stop generation */ I2C_TransferHandling(LM75_I2C, LM75_ADDR, 1, I2C_SoftEnd_Mode, I2C_Generate_Start_Write); /* Wait until TXIS flag is set */ LM75Timeout = LM75_LONG_TIMEOUT; while(I2C_GetFlagStatus(LM75_I2C, I2C_ISR_TXIS) == RESET) { if((LM75Timeout--) == 0) return LM75_TIMEOUT_UserCallback(); } /* Send Register address */ I2C_SendData(LM75_I2C, (uint8_t)LM75_REG_CONF); /* Wait until TC flag is set */ LM75Timeout = LM75_LONG_TIMEOUT; while(I2C_GetFlagStatus(LM75_I2C, I2C_ISR_TC) == RESET) { if((LM75Timeout--) == 0) return LM75_TIMEOUT_UserCallback(); } /* Configure slave address, nbytes, reload, end mode and start or stop generation */ I2C_TransferHandling(LM75_I2C, LM75_ADDR, 1, I2C_AutoEnd_Mode, I2C_Generate_Start_Read); /* Wait until RXNE flag is set */ LM75Timeout = LM75_LONG_TIMEOUT; while(I2C_GetFlagStatus(LM75_I2C, I2C_ISR_RXNE) == RESET) { if((LM75Timeout--) == 0) return LM75_TIMEOUT_UserCallback(); } /* Read data from RXDR */ LM75_BufferRX[0]= I2C_ReceiveData(LM75_I2C); /* Wait until STOPF flag is set */ LM75Timeout = LM75_LONG_TIMEOUT; while(I2C_GetFlagStatus(LM75_I2C, I2C_ISR_STOPF) == RESET) { if((LM75Timeout--) == 0) return LM75_TIMEOUT_UserCallback(); } /* Clear STOPF flag */ I2C_ClearFlag(LM75_I2C, I2C_ICR_STOPCF); /* Return Register value */ return (uint8_t)LM75_BufferRX[0]; }
void ADXL345_Read(uint8_t DeviceAddr, uint8_t RegAddr, uint8_t* pBuffer, uint16_t NumByteToRead) { /* Test on BUSY Flag */ while(I2C_GetFlagStatus(ADXL345_I2C, I2C_ISR_BUSY) != RESET) { } /* Configure slave address, nbytes, reload, end mode and start or stop generation */ I2C_TransferHandling(ADXL345_I2C, DeviceAddr, 1, I2C_SoftEnd_Mode, I2C_Generate_Start_Write); /* Wait until TXIS flag is set */ while(I2C_GetFlagStatus(ADXL345_I2C, I2C_ISR_TXIS) == RESET) { } if(NumByteToRead>1) RegAddr |= 0x80; /* Send Register address */ I2C_SendData(ADXL345_I2C, (uint8_t)RegAddr); /* Wait until TC flag is set */ while(I2C_GetFlagStatus(ADXL345_I2C, I2C_ISR_TC) == RESET) { } /* Configure slave address, nbytes, reload, end mode and start or stop generation */ I2C_TransferHandling(ADXL345_I2C, DeviceAddr, NumByteToRead, I2C_AutoEnd_Mode, I2C_Generate_Start_Read); /* Wait until all data are received */ while (NumByteToRead) { /* Wait until RXNE flag is set */ while(I2C_GetFlagStatus(ADXL345_I2C, I2C_ISR_RXNE) == RESET) { } /* Read data from RXDR */ *pBuffer = I2C_ReceiveData(ADXL345_I2C); /* Point to the next location where the byte read will be saved */ pBuffer++; /* Decrement the read bytes counter */ NumByteToRead--; } /* Wait until STOPF flag is set */ while(I2C_GetFlagStatus(ADXL345_I2C, I2C_ISR_STOPF) == RESET) { } /* Clear STOPF flag */ I2C_ClearFlag(ADXL345_I2C, I2C_ICR_STOPCF); }
/** * @brief Writes one byte to the LSM303DLHC. * @param DeviceAddr : specifies the slave address to be programmed. * @param RegAddr : specifies the LSM303DLHC register to be written. * @param pBuffer : pointer to the buffer containing the data to be written to the LSM303DLH. * @retval LSM303DLHC Status */ uint16_t LSM303DLHC_Write(uint8_t DeviceAddr, uint8_t RegAddr, uint8_t* pBuffer) { /* Test on BUSY Flag */ LSM303DLHC_Timeout = LSM303DLHC_LONG_TIMEOUT; while (I2C_GetFlagStatus(LSM303DLHC_I2C, I2C_ISR_BUSY ) != RESET) { if ((LSM303DLHC_Timeout--) == 0) return LSM303DLHC_TIMEOUT_UserCallback(); } /* Configure slave address, nbytes, reload, end mode and start or stop generation */ I2C_TransferHandling(LSM303DLHC_I2C, DeviceAddr, 1, I2C_Reload_Mode, I2C_Generate_Start_Write); /* Wait until TXIS flag is set */ LSM303DLHC_Timeout = LSM303DLHC_LONG_TIMEOUT; while (I2C_GetFlagStatus(LSM303DLHC_I2C, I2C_ISR_TXIS ) == RESET) { if ((LSM303DLHC_Timeout--) == 0) return LSM303DLHC_TIMEOUT_UserCallback(); } /* Send Register address */ I2C_SendData(LSM303DLHC_I2C, (uint8_t) RegAddr); /* Wait until TCR flag is set */ LSM303DLHC_Timeout = LSM303DLHC_LONG_TIMEOUT; while (I2C_GetFlagStatus(LSM303DLHC_I2C, I2C_ISR_TCR ) == RESET) { if ((LSM303DLHC_Timeout--) == 0) return LSM303DLHC_TIMEOUT_UserCallback(); } /* Configure slave address, nbytes, reload, end mode and start or stop generation */ I2C_TransferHandling(LSM303DLHC_I2C, DeviceAddr, 1, I2C_AutoEnd_Mode, I2C_No_StartStop); /* Wait until TXIS flag is set */ LSM303DLHC_Timeout = LSM303DLHC_LONG_TIMEOUT; while (I2C_GetFlagStatus(LSM303DLHC_I2C, I2C_ISR_TXIS ) == RESET) { if ((LSM303DLHC_Timeout--) == 0) return LSM303DLHC_TIMEOUT_UserCallback(); } /* Write data to TXDR */ I2C_SendData(LSM303DLHC_I2C, *pBuffer); /* Wait until STOPF flag is set */ LSM303DLHC_Timeout = LSM303DLHC_LONG_TIMEOUT; while (I2C_GetFlagStatus(LSM303DLHC_I2C, I2C_ISR_STOPF ) == RESET) { if ((LSM303DLHC_Timeout--) == 0) return LSM303DLHC_TIMEOUT_UserCallback(); } /* Clear STOPF flag */ I2C_ClearFlag(LSM303DLHC_I2C, I2C_ICR_STOPCF ); return LSM303DLHC_OK ; }
/* @brief Reads bytes from MPU6050 * * @param SlaveAddr - Slave I2C address * @param RegAddr - register address * @param pBuffer - buffer to write to * @ param NumByteToRead - number of bytes to read * * @retval @MPU6050_errorstatus */ MPU6050_errorstatus MPU6050_Read(uint8_t SlaveAddr, uint8_t RegAddr, uint8_t* pBuffer, uint16_t NumByteToRead) { /* Test if SDA line busy */ MPU6050_Timeout = MPU6050_LONG_TIMEOUT; while(I2C_GetFlagStatus(MPU6050_I2C, I2C_FLAG_BUSY) != RESET) { if((MPU6050_Timeout--) == 0) return MPU6050_I2C_ERROR; } I2C_TransferHandling(MPU6050_I2C, SlaveAddr, 1, I2C_SoftEnd_Mode, I2C_Generate_Start_Write); MPU6050_Timeout = MPU6050_LONG_TIMEOUT; while(I2C_GetFlagStatus(MPU6050_I2C, I2C_FLAG_TXIS) == RESET) { if((MPU6050_Timeout--) == 0) return MPU6050_I2C_ERROR; } if(NumByteToRead>1) RegAddr |= 0x80; I2C_SendData(MPU6050_I2C, (uint8_t)RegAddr); MPU6050_Timeout = MPU6050_LONG_TIMEOUT; while(I2C_GetFlagStatus(MPU6050_I2C, I2C_FLAG_TC) == RESET) { if((MPU6050_Timeout--) == 0) return MPU6050_I2C_TX_ERROR; } I2C_TransferHandling(MPU6050_I2C, SlaveAddr, NumByteToRead, I2C_AutoEnd_Mode, I2C_Generate_Start_Read); while (NumByteToRead) { MPU6050_Timeout = MPU6050_LONG_TIMEOUT; while(I2C_GetFlagStatus(MPU6050_I2C, I2C_FLAG_RXNE) == RESET) { if((MPU6050_Timeout--) == 0) return MPU6050_I2C_RX_ERROR; } *pBuffer = I2C_ReceiveData(MPU6050_I2C); pBuffer++; NumByteToRead--; } MPU6050_Timeout = MPU6050_LONG_TIMEOUT; while(I2C_GetFlagStatus(MPU6050_I2C, I2C_FLAG_STOPF) == RESET) { if((MPU6050_Timeout--) == 0) return MPU6050_I2C_ERROR; } I2C_ClearFlag(MPU6050_I2C, I2C_FLAG_STOPF); return MPU6050_NO_ERROR; }
uint16_t I2C_Write(uint8_t deviceAddressess, uint8_t registerAddress, uint8_t* dataPointer) { I2C_Timeout = I2C_LONG_TIMEOUT; while(I2C_GetFlagStatus(I2C, I2C_ISR_BUSY) != RESET) { if((I2C_Timeout--) == 0) return I2C_TIMEOUT_UserCallback(); } /* Configure slave address, nbytes, reload, end mode and start or stop generation */ I2C_TransferHandling(I2C, deviceAddressess, 1, I2C_Reload_Mode, I2C_Generate_Start_Write); /* Wait until TXIS flag is set */ I2C_Timeout = I2C_LONG_TIMEOUT; while(I2C_GetFlagStatus(I2C, I2C_ISR_TXIS) == RESET) { if((I2C_Timeout--) == 0) return I2C_TIMEOUT_UserCallback(); } /* Send Register address */ I2C_SendData(I2C, (uint8_t) registerAddress); /* Wait until TCR flag is set */ I2C_Timeout = I2C_LONG_TIMEOUT; while(I2C_GetFlagStatus(I2C, I2C_ISR_TCR) == RESET) { if((I2C_Timeout--) == 0) return I2C_TIMEOUT_UserCallback(); } /* Configure slave address, nbytes, reload, end mode and start or stop generation */ I2C_TransferHandling(I2C, deviceAddressess, 1, I2C_AutoEnd_Mode, I2C_No_StartStop); /* Wait until TXIS flag is set */ I2C_Timeout = I2C_LONG_TIMEOUT; while(I2C_GetFlagStatus(I2C, I2C_ISR_TXIS) == RESET) { if((I2C_Timeout--) == 0) return I2C_TIMEOUT_UserCallback(); } /* Write data to TXDR */ I2C_SendData(I2C, *dataPointer); /* Wait until STOPF flag is set */ I2C_Timeout = I2C_LONG_TIMEOUT; while(I2C_GetFlagStatus(I2C, I2C_ISR_STOPF) == RESET) { if((I2C_Timeout--) == 0) return I2C_TIMEOUT_UserCallback(); } /* Clear STOPF flag */ I2C_ClearFlag(I2C, I2C_ICR_STOPCF); return I2C_OK; }
void I2C_WriteRegister(uint8_t addr, uint8_t reg, uint8_t val) { while(I2C_GetFlagStatus(I2C2, I2C_FLAG_BUSY) == SET); I2C_TransferHandling(I2C2, addr, 1, I2C_Reload_Mode, I2C_Generate_Start_Write); while(I2C_GetFlagStatus(I2C2, I2C_FLAG_TXIS) == RESET); I2C_SendData(I2C2, reg); while(I2C_GetFlagStatus(I2C2, I2C_FLAG_TCR) == RESET); I2C_TransferHandling(I2C2, addr, 1, I2C_AutoEnd_Mode, I2C_No_StartStop); while(I2C_GetFlagStatus(I2C2, I2C_FLAG_TXIS) == RESET); I2C_SendData(I2C2, val); while(I2C_GetFlagStatus(I2C2, I2C_FLAG_STOPF) == RESET); I2C_ClearFlag(I2C2, I2C_FLAG_STOPF); }
/* Private functions ---------------------------------------------------------*/ int EEP_WriteData(uint8_t DevAddr, uint16_t RegAddr, uint8_t* DataBuf, uint16_t DataLen) { EEP_Timeout = EEP_LONG_TIMEOUT; while(I2C_GetFlagStatus(I2C2,I2C_FLAG_BUSY)) { if(EEP_Timeout-- == 0) return ERROR; } I2C_TransferHandling(I2C2,DevAddr,2,I2C_Reload_Mode,I2C_Generate_Start_Write); while(I2C_GetFlagStatus(I2C2,I2C_FLAG_TXIS) == RESET) { if(EEP_Timeout-- == 0) return ERROR; } I2C_SendData(I2C2,RegAddr>>8); while(I2C_GetFlagStatus(I2C2,I2C_FLAG_TXIS) == RESET) { if(EEP_Timeout-- == 0) return ERROR; } I2C_SendData(I2C2,RegAddr&0xFF); while(I2C_GetFlagStatus(I2C2,I2C_ISR_TCR) == RESET) { if(EEP_Timeout-- == 0) return ERROR; } I2C_TransferHandling(I2C2,DevAddr,DataLen,I2C_AutoEnd_Mode,I2C_No_StartStop); while(DataLen--) { while(I2C_GetFlagStatus(I2C2,I2C_FLAG_TXIS) == RESET) { if(EEP_Timeout-- == 0) return ERROR; } I2C_SendData(I2C2, *DataBuf); DataBuf++; } while(I2C_GetFlagStatus(I2C2,I2C_FLAG_STOPF) == RESET) { if(EEP_Timeout-- == 0) return ERROR; } I2C_ClearFlag(I2C2, I2C_ICR_STOPCF); return SUCCESS; }
// // Originally, 'endTransmission' was an f(void) function. // It has been modified to take one parameter indicating // whether or not a STOP should be performed on the bus. // Calling endTransmission(false) allows a sketch to // perform a repeated start. // // WARNING: Nothing in the library keeps track of whether // the bus tenure has been properly ended with a STOP. It // is very possible to leave the bus in a hung state if // no call to endTransmission(true) is made. Some I2C // devices will behave oddly if they do not see a STOP. // uint8_t TwoWire::endTransmission(uint8_t sendStop) { uint32_t _millis; _millis = millis(); while(I2C_GetFlagStatus(twi, I2C_FLAG_BUSY) != RESET) { if(EVENT_TIMEOUT < (millis() - _millis)) return 0; } if (sendStop == true) { I2C_TransferHandling(twi, txAddress, txBufferLength, I2C_AutoEnd_Mode, I2C_Generate_Start_Write); } else { I2C_TransferHandling(twi, txAddress, txBufferLength, I2C_SoftEnd_Mode, I2C_Generate_Start_Write); } uint8_t *pBuffer = txBuffer; uint8_t NumByteToWrite = txBufferLength; /* While there is data to be read */ while(NumByteToWrite--) { _millis = millis(); while(I2C_GetFlagStatus(twi, I2C_FLAG_TXIS) == RESET) { if(EVENT_TIMEOUT < (millis() - _millis)) return 0; } /* Send the current byte to slave */ I2C_SendData(twi, *pBuffer++); } _millis = millis(); if (sendStop == true) { while(I2C_GetFlagStatus(twi, I2C_FLAG_STOPF) == RESET) { if(EVENT_TIMEOUT < (millis() - _millis)) return 0; } } else { while(I2C_GetFlagStatus(twi, I2C_FLAG_STOPF) == RESET) { if(EVENT_TIMEOUT < (millis() - _millis)) return 0; } } // reset tx buffer iterator vars txBufferLength = 0; status = MASTER_IDLE; return 0; }
int EEP_ReadData(uint8_t DevAddr, uint16_t RegAddr, uint8_t* DataBuf, uint16_t DataLen) { while(I2C_GetFlagStatus(I2C2,I2C_FLAG_BUSY)) { if(EEP_Timeout-- == 0) return ERROR; } I2C_TransferHandling(I2C2,DevAddr,2,I2C_SoftEnd_Mode,I2C_Generate_Start_Write); while(I2C_GetFlagStatus(I2C2,I2C_FLAG_TXIS) == RESET) { if(EEP_Timeout-- == 0) return ERROR; } I2C_SendData(I2C2,RegAddr>>8); while(I2C_GetFlagStatus(I2C2,I2C_FLAG_TXIS) == RESET) { if(EEP_Timeout-- == 0) return ERROR; } I2C_SendData(I2C2,RegAddr&0xFF); while(I2C_GetFlagStatus(I2C2,I2C_ISR_TC) == RESET) { if(EEP_Timeout-- == 0) return ERROR; } I2C_TransferHandling(I2C2,DevAddr,DataLen,I2C_AutoEnd_Mode,I2C_Generate_Start_Read); while(DataLen) { while(I2C_GetFlagStatus(I2C2,I2C_FLAG_RXNE) == RESET) { if(EEP_Timeout-- == 0) return ERROR; } *DataBuf = I2C_ReceiveData(I2C2); DataBuf++; DataLen--; } while(I2C_GetFlagStatus(I2C2,I2C_FLAG_STOPF) == RESET) { if(EEP_Timeout-- == 0) return ERROR; } return SUCCESS; }
//We're really fortunate with the HMC5883L as it automatically //increments the internal register counter with every read //so we need to set the internal register pointer to the first data //register (the X value register) and just read the next 6 pieces //of data, X1, X2, Z1, Z2 Y1, Y2 and //voila! We have the compass values! uint8_t I2C_RdReg(int8_t Reg, int8_t *Data, uint8_t DCnt){ int8_t Cnt, SingleData = 0; //As per, ensure the I2C peripheral isn't busy! while(I2C_GetFlagStatus(HMC_I2C, I2C_FLAG_BUSY) == SET); //Again, start another tranfer using the "transfer handling" //function, the end bit being set in software this time //round, generate a start condition and indicate you will //be writing data to the HMC device. I2C_TransferHandling(HMC_I2C, HMCAddr, 1, I2C_SoftEnd_Mode, I2C_Generate_Start_Write); //Wait until the transmit interrupt status is set while(I2C_GetFlagStatus(HMC_I2C, I2C_FLAG_TXIS) == RESET); //Send the address of the register you wish to read I2C_SendData(HMC_I2C, (uint8_t)Reg); //Wait until transfer is complete! while(I2C_GetFlagStatus(HMC_I2C, I2C_FLAG_TC) == RESET); //As per, start another transfer, we want to read DCnt //amount of bytes. Generate a start condition and //indicate that we want to read. I2C_TransferHandling(HMC_I2C, HMCAddr, DCnt, I2C_AutoEnd_Mode, I2C_Generate_Start_Read); //Read in DCnt pieces of data for(Cnt = 0; Cnt<DCnt; Cnt++){ //Wait until the RX register is full of luscious data! while(I2C_GetFlagStatus(HMC_I2C, I2C_FLAG_RXNE) == RESET); //If we're only reading one byte, place that data direct into the //SingleData variable. If we're reading more than 1 piece of data //store in the array "Data" (a pointer from main) if(DCnt > 1) Data[Cnt] = I2C_ReceiveData(HMC_I2C); else SingleData = I2C_ReceiveData(HMC_I2C); } //Wait for the stop condition to be sent while(I2C_GetFlagStatus(HMC_I2C, I2C_FLAG_STOPF) == RESET); //Clear the stop flag for next transfers I2C_ClearFlag(HMC_I2C, I2C_FLAG_STOPF); //Return a single piece of data if DCnt was //less than 1, otherwise 0 will be returned. return SingleData; }
/** * @brief Checks the LM75 status. * @param None * @retval ErrorStatus: LM75 Status (ERROR or SUCCESS). */ ErrorStatus LM75_GetStatus(void) { uint32_t I2C_TimeOut = I2C_TIMEOUT; /* Configure slave address, nbytes, reload, end mode and start or stop generation */ I2C_TransferHandling(LM75_I2C, LM75_ADDR, 0, I2C_AutoEnd_Mode, I2C_No_StartStop); /* Clear NACKF and STOPF */ I2C_ClearFlag(LM75_I2C, I2C_ICR_NACKCF | I2C_ICR_STOPCF); /* Generate start */ I2C_GenerateSTART(LM75_I2C, ENABLE); /* Wait until timeout elapsed */ while ((I2C_GetFlagStatus(LM75_I2C, I2C_ISR_STOPF) == RESET) && (I2C_TimeOut-- != 0)); /* Check if Temp sensor is ready for use */ if ((I2C_GetFlagStatus(LM75_I2C, I2C_ISR_NACKF) != RESET) || (I2C_TimeOut == 0)) { /* Clear NACKF and STOPF */ I2C_ClearFlag(LM75_I2C, I2C_ICR_NACKCF | I2C_ICR_STOPCF); return ERROR; } else { /* Clear STOPF */ I2C_ClearFlag(LM75_I2C, I2C_ICR_STOPCF); return SUCCESS; } }
int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) { I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); int timeout; int count; if (length == 0) return 0; // Configure slave address, nbytes, reload, end mode and start generation I2C_TransferHandling(i2c, address, length, I2C_SoftEnd_Mode, I2C_Generate_Start_Write); // Write all bytes for (count = 0; count < length; count++) { i2c_byte_write(obj, data[count]); } timeout = FLAG_TIMEOUT; while(!I2C_GetFlagStatus(i2c, I2C_FLAG_TC)) { timeout--; if (timeout == 0) return 0; } if(stop) i2c_stop(obj); return count; }
uint8_t i2cReceiveS(I2C_TypeDef* I2Cx, const uint8_t addr, uint8_t *buffer, uint8_t len) { uint8_t timeout; if (chSemWaitTimeout(&i2c1_semS, MS2ST(I2C_TIMEOUT)) != MSG_OK) { return 1; } I2C_TransferHandling(I2Cx, addr, len, I2C_AutoEnd_Mode, I2C_Generate_Start_Read); while (len--) { timeout = 100; while(I2C_GetFlagStatus(I2Cx, I2C_ISR_RXNE) == RESET) { chThdSleepMicroseconds(50); if (!timeout--) { chSemSignal(&i2c1_semS); return 1; } }; *buffer++ = I2Cx->RXDR; } chSemSignal(&i2c1_semS); return 0; }
int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) { I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); int count; int timeout; int value; // Configure slave address, nbytes, reload, end mode and start or stop generation I2C_TransferHandling(i2c, address, length, I2C_SoftEnd_Mode, I2C_Generate_Start_Read); // Read all bytes for (count = 0; count < length; count++) { value = i2c_byte_read(obj, 0); data[count] = (char)value; } timeout = FLAG_TIMEOUT; while (!I2C_GetFlagStatus(i2c, I2C_FLAG_TC)) { timeout--; if (timeout == 0) return -1; } if (stop) i2c_stop(obj); return length; }
/* sla includes also the r/w bit */ uint8_t u8g_i2c_start(uint8_t sla) { while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY) == SET); I2C_TransferHandling(I2C1, sla, 1, I2C_Reload_Mode, I2C_Generate_Start_Write); while(I2C_GetFlagStatus(I2C1, I2C_FLAG_TXIS) == RESET); return 1; }
void f3d_i2c1_read_nunchuk (uint8_t device, uint8_t* buffer, uint16_t numbytes) { while (I2C_GetFlagStatus(I2C1, I2C_ISR_BUSY) != RESET); I2C_TransferHandling(I2C1, 0xA4, 1, I2C_AutoEnd_Mode, I2C_Generate_Start_Write); while (I2C_GetFlagStatus(I2C1, I2C_ISR_TXIS) == RESET); I2C_SendData(I2C1,0x00); while(I2C_GetFlagStatus(I2C1, I2C_ISR_STOPF) == RESET); I2C_ClearFlag(I2C1, I2C_ICR_STOPCF); delay(1); while (I2C_GetFlagStatus(I2C1, I2C_ISR_BUSY) != RESET); I2C_TransferHandling(I2C1, 0xA4, 6, I2C_AutoEnd_Mode, I2C_Generate_Start_Read); while (numbytes--) { while(I2C_GetFlagStatus(I2C1, I2C_ISR_RXNE) == RESET); *buffer++ = I2C_ReceiveData(I2C1); } while (I2C_GetFlagStatus(I2C1, I2C_ISR_STOPF) == RESET); I2C_ClearFlag(I2C1, I2C_ICR_STOPCF); }
void f3d_i2c1_write(uint8_t device, uint8_t reg, uint8_t* value) { while (I2C_GetFlagStatus(I2C1, I2C_ISR_BUSY) != RESET); I2C_TransferHandling(I2C1, device, 1, I2C_Reload_Mode, I2C_Generate_Start_Write); while (I2C_GetFlagStatus(I2C1, I2C_ISR_TXIS) == RESET); I2C_SendData(I2C1,reg); while(I2C_GetFlagStatus(I2C1, I2C_ISR_TCR) == RESET); I2C_TransferHandling(I2C1, device, 1, I2C_AutoEnd_Mode, I2C_No_StartStop); while(I2C_GetFlagStatus(I2C1, I2C_ISR_TXIS) == RESET); I2C_SendData(I2C1, *value); while(I2C_GetFlagStatus(I2C1, I2C_ISR_STOPF) == RESET); I2C_ClearFlag(I2C1, I2C_ICR_STOPCF); }
void ADXL345_Write(uint8_t DeviceAddr, uint8_t RegAddr, uint8_t* pBuffer) { /* Test on BUSY Flag */ while(I2C_GetFlagStatus(ADXL345_I2C, I2C_ISR_BUSY) != RESET) { } /* Configure slave address, nbytes, reload, end mode and start or stop generation */ I2C_TransferHandling(ADXL345_I2C, DeviceAddr, 1, I2C_Reload_Mode, I2C_Generate_Start_Write); /* Wait until TXIS flag is set */ while(I2C_GetFlagStatus(ADXL345_I2C, I2C_ISR_TXIS) == RESET) { } /* Send Register address */ I2C_SendData(ADXL345_I2C, (uint8_t) RegAddr); /* Wait until TCR flag is set */ while(I2C_GetFlagStatus(ADXL345_I2C, I2C_ISR_TCR) == RESET) { } /* Configure slave address, nbytes, reload, end mode and start or stop generation */ I2C_TransferHandling(ADXL345_I2C, DeviceAddr, 1, I2C_AutoEnd_Mode, I2C_No_StartStop); /* Wait until TXIS flag is set */ while(I2C_GetFlagStatus(ADXL345_I2C, I2C_ISR_TXIS) == RESET) { } /* Write data to TXDR */ I2C_SendData(ADXL345_I2C, *pBuffer); /* Wait until STOPF flag is set */ while(I2C_GetFlagStatus(ADXL345_I2C, I2C_ISR_STOPF) == RESET) { } /* Clear STOPF flag */ I2C_ClearFlag(ADXL345_I2C, I2C_ICR_STOPCF); }
void I2C_WrReg(uint8_t Reg, uint8_t Val){ //Wait until I2C isn't busy while(I2C_GetFlagStatus(HMC_I2C, I2C_FLAG_BUSY) == SET); //"Handle" a transfer - The STM32F0 series has a shocking //I2C interface... Regardless! Send the address of the HMC //sensor down the I2C Bus and generate a start saying we're //going to write one byte. I'll be completely honest, //the I2C peripheral doesn't make too much sense to me //and a lot of the code is from the Std peripheral library I2C_TransferHandling(HMC_I2C, HMCAddr, 1, I2C_Reload_Mode, I2C_Generate_Start_Write); //Ensure the transmit interrupted flag is set while(I2C_GetFlagStatus(HMC_I2C, I2C_FLAG_TXIS) == RESET); //Send the address of the register we wish to write to I2C_SendData(HMC_I2C, Reg); //Ensure that the transfer complete reload flag is //set, essentially a standard TC flag while(I2C_GetFlagStatus(HMC_I2C, I2C_FLAG_TCR) == RESET); //Now that the HMC5883L knows which register //we want to write to, send the address again //and ensure the I2C peripheral doesn't add //any start or stop conditions I2C_TransferHandling(HMC_I2C, HMCAddr, 1, I2C_AutoEnd_Mode, I2C_No_StartStop); //Again, wait until the transmit interrupted flag is set while(I2C_GetFlagStatus(HMC_I2C, I2C_FLAG_TXIS) == RESET); //Send the value you wish you write to the register I2C_SendData(HMC_I2C, Val); //Wait for the stop flag to be set indicating //a stop condition has been sent while(I2C_GetFlagStatus(HMC_I2C, I2C_FLAG_STOPF) == RESET); //Clear the stop flag for the next potential transfer I2C_ClearFlag(HMC_I2C, I2C_FLAG_STOPF); }
void i2c_write(uint8_t* pBuffer, uint8_t num) { uint8_t i = 0; I2C_ITConfig(I2C2, I2C_IT_RXI | I2C_IT_TXI | I2C_IT_ADDRI | I2C_IT_STOPI, DISABLE); while(I2C_GetFlagStatus(I2C2, I2C_ISR_BUSY) != RESET); I2C_TransferHandling(I2C2, I2C_SLAVE, num, I2C_AutoEnd_Mode, I2C_Generate_Start_Write); for(; i < num; i++) { while(I2C_GetFlagStatus(I2C2, I2C_ISR_TXIS) == RESET); I2C_SendData(I2C2, (uint8_t) I2C_SLAVE); } while(I2C_GetFlagStatus(I2C2, I2C_ISR_STOPF) == RESET); I2C_ClearFlag(I2C2, I2C_ICR_STOPCF); }
void i2c_start_transfer(mal_i2c_s *handle) { handle->cmd = (volatile mal_i2c_cmd_e)handle->msg->packet.cmd; // Reset variables handle->state = (volatile mal_hspec_stm32f0_i2c_states_e)I2C_STATE_START; handle->data_ptr = 0; // Start transfer uint32_t transfer_type = I2C_Generate_Start_Read; if (MAL_I2C_WRITE == handle->cmd) { transfer_type = I2C_Generate_Start_Write; } I2C_TransferHandling(handle->stm_handle, handle->msg->packet.address << 1, handle->msg->packet.packet_size, I2C_SoftEnd_Mode, transfer_type); }
uint8_t I2C_ReadReg(uint8_t addr, uint8_t reg, uint8_t *buffer, uint8_t length) { uint8_t count = 0; while(I2C_GetFlagStatus(I2C2, I2C_FLAG_BUSY) == SET); I2C_TransferHandling(I2C2, addr, 1, I2C_SoftEnd_Mode, I2C_Generate_Start_Write); while(I2C_GetFlagStatus(I2C2, I2C_FLAG_TXIS) == RESET); I2C_SendData(I2C2, reg); while(I2C_GetFlagStatus(I2C2, I2C_FLAG_TC) == RESET); I2C_TransferHandling(I2C2, addr, length, I2C_AutoEnd_Mode, I2C_Generate_Start_Read); while(count < length){ while(I2C_GetFlagStatus(I2C2, I2C_FLAG_RXNE) == RESET); buffer[count] = I2C_ReceiveData(I2C2); count++; } while(I2C_GetFlagStatus(I2C2, I2C_FLAG_STOPF) == RESET); I2C_ClearFlag(I2C2, I2C_FLAG_STOPF); return count; }
void f3d_i2c1_read(uint8_t device, uint8_t reg, uint8_t* buffer, uint16_t numbytes) { while (I2C_GetFlagStatus(I2C1, I2C_ISR_BUSY) != RESET); // Wait for I2C to become free I2C_TransferHandling(I2C1, device, 1, I2C_SoftEnd_Mode, I2C_Generate_Start_Write); // Send start and device address while (I2C_GetFlagStatus(I2C1, I2C_ISR_TXIS) == RESET); // Confirm that the transmitter empty if (numbytes>1) { reg |=0x80; // indicate that the transfer will involve multiple bytes } I2C_SendData(I2C1,reg); // Send the register address while (I2C_GetFlagStatus(I2C1, I2C_ISR_TC) == RESET); I2C_TransferHandling(I2C1, device, numbytes, I2C_AutoEnd_Mode, I2C_Generate_Start_Read); while (numbytes--) { while(I2C_GetFlagStatus(I2C1, I2C_ISR_RXNE) == RESET); *buffer++ = I2C_ReceiveData(I2C1); } while (I2C_GetFlagStatus(I2C1, I2C_ISR_STOPF) == RESET); // Wait for stop flag generation I2C_ClearFlag(I2C1, I2C_ICR_STOPCF); }
uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity, uint8_t sendStop) { address <<= 1; if (quantity > BUFFER_LENGTH) quantity = BUFFER_LENGTH; uint32_t _millis; _millis = millis(); while(I2C_GetFlagStatus(twi, I2C_FLAG_BUSY) != RESET) { if(EVENT_TIMEOUT < (millis() - _millis)) return 0; } I2C_TransferHandling(twi, address, quantity, I2C_AutoEnd_Mode, I2C_Generate_Start_Read); uint8_t *pBuffer = rxBuffer; uint8_t numByteToRead = quantity; uint8_t bytesRead = 0; /* While there is data to be read */ while(numByteToRead) { _millis = millis(); while(I2C_GetFlagStatus(twi, I2C_FLAG_RXNE) == RESET) { if(EVENT_TIMEOUT < (millis() - _millis)) return 0; } /* Read a byte from the Slave */ *pBuffer = I2C_ReceiveData(twi); bytesRead++; /* Point to the next location where the byte read will be saved */ pBuffer++; /* Decrement the read bytes counter */ numByteToRead--; } _millis = millis(); while(I2C_GetFlagStatus(twi, I2C_FLAG_STOPF) == RESET) { if(EVENT_TIMEOUT < (millis() - _millis)) return 0; } // set rx buffer iterator vars rxBufferIndex = 0; rxBufferLength = bytesRead; return bytesRead; }
int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) { I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); int count; int value; if (length == 0) return 0; // Configure slave address, nbytes, reload, end mode and start or stop generation I2C_TransferHandling(i2c, address, length, I2C_AutoEnd_Mode, I2C_Generate_Start_Read); // Read all bytes for (count = 0; count < length; count++) { value = i2c_byte_read(obj, 0); data[count] = (char)value; } return length; }
int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) { I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c); //int timeout; int count; if (length == 0) return 0; // [TODO] The stop is always sent even with I2C_SoftEnd_Mode. To be corrected. // Configure slave address, nbytes, reload, end mode and start or stop generation //if (stop) { I2C_TransferHandling(i2c, address, length, I2C_AutoEnd_Mode, I2C_Generate_Start_Write); //} //else { // I2C_TransferHandling(i2c, address, length, I2C_SoftEnd_Mode, I2C_Generate_Start_Write); //} // Write all bytes for (count = 0; count < length; count++) { if (i2c_byte_write(obj, data[count]) != 1) { i2c_stop(obj); return 0; } } /* if (stop) { // Wait until STOPF flag is set timeout = LONG_TIMEOUT; while (I2C_GetFlagStatus(i2c, I2C_ISR_STOPF) == RESET) { timeout--; if (timeout == 0) { return 0; } } // Clear STOPF flag I2C_ClearFlag(i2c, I2C_ICR_STOPCF); } */ return count; }
uint8_t u8g_i2c_send_byte(uint8_t data) { //Send the address of the register we wish to write to I2C_SendData(I2C1, control); //Ensure that the transfer complete reload flag is Set, essentially a standard //TC flag while(I2C_GetFlagStatus(I2C1, I2C_FLAG_TCR) == RESET); //Now that the HMC5883L knows which register we want to write to, send the address //again and ensure the I2C peripheral doesn't add any start or stop conditions I2C_TransferHandling(I2C1, 0x78, 1, I2C_AutoEnd_Mode, I2C_No_StartStop); //Again, wait until the transmit interrupted flag is set while(I2C_GetFlagStatus(I2C1, I2C_FLAG_TXIS) == RESET); //Send the value you wish you write to the register I2C_SendData(I2C1, data); while(I2C_GetFlagStatus(I2C1, I2C_FLAG_TCR) == RESET); return 1; }