bool I2C::_MasterRead (uint8_t* data, uint32_t data_size, uint8_t slave_addr, uint8_t* reg_addr) { if (!WaitStatus(I2C::BUSY, false)) { //ClearBUSY(); _stuckBUSY = true; return false; } if (reg_addr) { SendStart(true); if (!WaitStatus(I2C::MASTER_EV5, true)) //BSY MSL SB return false; Send7bitAddress(slave_addr, true); if (!WaitStatus(I2C::MASTER_EV6_TRA, true)) //MSL | BUSY | ADDR | TXE | TRA return false; SendByte(*reg_addr); if (!WaitStatus(I2C::MASTER_EV8_2, true)) //TRA, BUSY, MSL, TXE and BTF return false; } SendStart(true); if (!WaitStatus(I2C::MASTER_EV5, true)) //BSY MSL SB return false; Send7bitAddress(slave_addr, false); if (!WaitStatus(I2C::MASTER_EV6_RECV, true)) //MSL | BUSY | ADDR return false; if (data_size == 1) //if (false) { EnableACK(false); SendStop(true); // if (!WaitStatus(I2C::MASTER_EV7, true)) //MSL | BUSY | ADDR if (!WaitStatus(I2C::RXNE, true)) //MSL | BUSY | ADDR return false; *data = ReceiveByte(); EnableACK(true); return true; } else { return ReceiveDMA(data, data_size); } }
//***************************************************************************** // @brief Writes one byte to the LSM303. // @param slAddr : slave address LSM_A_ADDRESS or LSM_M_ADDRESS // @param pBuffer : pointer to the buffer containing the data to be written // to the LSM303. // @param WriteAddr : address of the register in which the data will // be written // @retval None //***************************************************************************** static void ByteWrite(uint8_t slAddr, uint8_t* pBuffer, uint8_t WriteAddr) { /* Send START condition */ GenerateSTART(I2C1, ENABLE); /* Test on EV5 and clear it */ while(!CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)); /* Send address for write */ Send7bitAddress(I2C1, slAddr, I2C_Direction_Transmitter); /* Test on EV6 and clear it */ while(!CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)); /* Send the internal address to write to */ SendData(I2C1, WriteAddr); /* Test on EV8 and clear it */ while(!CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED)); /* Send the byte to be written */ SendData(I2C1, *pBuffer); /* Test on EV8 and clear it */ while(!CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED)); /* Send STOP condition */ GenerateSTOP(I2C1, ENABLE); }
bool I2C::_MasterWrite (uint8_t* data, uint32_t data_size, uint8_t slave_addr, uint8_t* preg_addr) { if (!WaitStatus(I2C::BUSY, false)) { //ClearBUSY(); _stuckBUSY= true; return false; } SendStart(true); if (!WaitStatus(I2C::MASTER_EV5, true)) return false; Send7bitAddress(slave_addr, true); if (!WaitStatus(I2C::MASTER_EV6_TRA, true)) //MSL | BUSY | ADDR | TXE | TRA return false; //////////////////////////////////////////////////////////////////////////// if (preg_addr) { SendByte(*preg_addr); if (!WaitStatus(I2C::MASTER_EV8_2, true)) //TRA, BUSY, MSL, TXE and BTF return false; } //////////////////////////////////////////////////////////////////////////// if (data_size == 1) { SendByte(*data); if (!WaitStatus(I2C::MASTER_EV8, true)) //TRA, BUSY, MSL, TXE and BTF return false; SendStop(true); return true; } else { return SendDMA (data, data_size); } }
//***************************************************************************** // @brief Reads a block of data from the LSM303 // @param slAddr : slave address LSM_A_ADDRESS or LSM_M_ADDRESS // @param pBuffer : pointer to the buffer that receives the data read // from the LSM303. // @param ReadAddr : LSM303's internal address to read from. // @param NumByteToRead : number of bytes to read from the LSM303 // ( NumByteToRead > 1 only for the Mgnetometer readinf). // @retval None //***************************************************************************** static void BufferRead(uint8_t slAddr, uint8_t* pBuffer, uint8_t ReadAddr , uint16_t NumByteToRead) { /* While the bus is busy */ while(GetFlagStatus(I2C1, I2C_FLAG_BUSY)); /* Send START condition */ GenerateSTART(I2C1, ENABLE); /* Test on EV5 and clear it */ while(!CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)); /* Send address for write */ Send7bitAddress(I2C1, slAddr, I2C_Direction_Transmitter); /* Test on EV6 and clear it */ while(!CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)); /* Clear EV6 by setting again the PE bit */ Cmd(I2C1, ENABLE); /* Send the internal address to read from */ SendData(I2C1, ReadAddr); /* Test on EV8 and clear it */ while(!CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED)); /* Send STRAT condition a second time */ GenerateSTART(I2C1, ENABLE); /* Test on EV5 and clear it */ while(!CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)); /* Send LSM303 address for read */ Send7bitAddress(I2C1, slAddr, I2C_Direction_Receiver); /* Test on EV6 and clear it */ while(!CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)); /* While there is data to be read */ while(NumByteToRead) { if(NumByteToRead == 1) { /* Disable Acknowledgement */ AcknowledgeConfig(I2C1, DISABLE); /* Send STOP Condition */ GenerateSTOP(I2C1, ENABLE); } /* Test on EV7 and clear it */ if(CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED)) { /* Read a byte from the sensor */ *pBuffer = ReceiveData(I2C1); /* Point to the next location where the byte read will be saved */ pBuffer++; /* Decrement the read bytes counter */ NumByteToRead--; } } /* Enable Acknowledgement to be ready for another reception */ AcknowledgeConfig(I2C1, ENABLE); }