/********************************************************************* * @fn hali2cReceive * @brief reads data into a buffer * @param address: linear address on part from which to read * @param buffer: target array for read characters * @param len: max number of characters to read * @return void */ STATIC void hali2cReceive( uint8 address, uint8 *buffer, uint16 len ) { //uint8 ch; uint16 i; if (!len) { return; } hali2cSendDeviceAddress(address); //ch = OCM_ADDRESS_BYTE(0, OCM_READ); //hali2cSend(&ch, 1, SEND_START, NOSEND_STOP); for ( i = 0; i < len-1; i++ ) { // SCL may be high. set SCL low. If SDA goes high when input // mode is set the slave won't see a STOP hali2cClock(0); OCM_DATA_HIGH(); buffer[i] = hali2cReceiveByte(); hali2cWrite(SMB_ACK); // write leaves SCL high } // condition SDA one more time... hali2cClock(0); OCM_DATA_HIGH(); buffer[i] = hali2cReceiveByte(); hali2cWrite(SMB_NAK); hali2cStop(); }
/***************************************************************************** * * * Read ADXL345 */ byte ADXL345ReadByte(byte address) { // address and W/R bit byte firstByte = 0x00; // reserve the read byte byte value = 0x00; // wait turns to receive the slave ack uint8 retry = HAL_I2C_RETRY_CNT; // Send the start condition hali2cStart(); // Send the slave address and wirte bit firstByte = 0xA6; do { if ( hali2cSendByte(firstByte) ) // takes care of ack polling { retry = HAL_I2C_RETRY_CNT; break; } } while ( --retry ); // Send the reg address do { if ( hali2cSendByte(address) ) // takes care of ack polling { retry = HAL_I2C_RETRY_CNT; break; } } while ( --retry ); // Send the start condition again hali2cStart(); // Send the slave address and read bit firstByte = 0xA7; do { if ( hali2cSendByte(firstByte) ) // takes care of ack polling { retry = HAL_I2C_RETRY_CNT; break; } } while ( --retry ); // Read a byte from ADXL345 value = hali2cReceiveByte(); // write NACK hali2cWrite(SMB_NAK); // Send stop condition hali2cStop(); return value; }
// EEPROM READ byte uint8_t hali2cRead_byte( uint8 devaddr, uint16 regaddr ) { uint8_t d = 0; hali2cSendDeviceAddress((devaddr<<1) | OCM_WRITE); // send internal eeprom addres (0 - 0x1fff) hali2cSendByte(regaddr >> 8); hali2cSendByte(regaddr & 0xff); hali2cStart(); hali2cSendDeviceAddress((devaddr<<1) | OCM_READ); d = hali2cReceiveByte(); hali2cWrite(SMB_NAK); hali2cStop(); return d; }
void ADXL345ReadBytes(uint8 startAddr, uint8* buffer, uint8 len) { // wait turns to receive the slave ack uint8 retry = HAL_I2C_RETRY_CNT; // 7-bit address and W/R bit byte firstByte = 0x00; byte i = 0; // Send start condition hali2cStart(); // Send the slave address and wirte bit firstByte = 0xA6; // seven-bit slave address and write bit '0', 0x1111_1110 do { if ( hali2cSendByte(firstByte) ) // takes care of ack polling { retry = HAL_I2C_RETRY_CNT; break; } } while ( --retry ); // Send the reg address do { if ( hali2cSendByte(startAddr) ) // takes care of ack polling { retry = HAL_I2C_RETRY_CNT; break; } } while ( --retry ); // this is the repeat start condition hali2cStart(); // Send the slave address and read bit firstByte = 0xA7; do { if ( hali2cSendByte(firstByte) ) // takes care of ack polling { retry = HAL_I2C_RETRY_CNT; break; } } while ( --retry ); // read several bytes for ( i = 0; i<len-1; i++ ) { // SCL may be high. set SCL low. If SDA goes high when input // mode is set the slave won't see a STOP hali2cClock(0); OCM_DATA_HIGH(); buffer[i] = hali2cReceiveByte(); hali2cWrite(SMB_ACK); // write leaves SCL high } // condition SDA one more time... // hali2cClock(0); // OCM_DATA_HIGH(); // buffer[i] = hali2cReceiveByte(); // hali2cWrite(SMB_NAK); // Send stop condition hali2cStop(); }