/***************************************************************************** * * * 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; }
/********************************************************************* * @fn hali2cSend * @brief Sends buffer contents to SM-Bus device * @param buffer - ptr to buffered data to send * @param len - number of bytes in buffer * @param sendStart - whether or not to send start condition. * @param sendStop - whether or not to send stop condition. * @return void */ STATIC void hali2cSend( uint8 *buffer, uint16 len, uint8 sendStart, uint8 sendStop ) { uint16 i; uint8 retry = HAL_I2C_RETRY_CNT; if (!len) { return; } if (sendStart == SEND_START) { hali2cStart(); } for ( i = 0; i < len; i++ ) { do { if ( hali2cSendByte( buffer[i] ) ) // takes care of ack polling { break; } } while ( --retry ); } if (sendStop == SEND_STOP) { hali2cStop(); } }
// Wait for Finish EEPROM WRITE operation uint8_t hali2c_acknowledge_write_polling(uint8_t devaddr) { uint8_t rc; do{ hali2cStart(); rc = hali2cSendByte((devaddr<<1) | OCM_WRITE); }while (!rc); return 0; }
/********************************************************************* * @fn hali2cSendDeviceAddress * @brief Send onlythe device address. Do ack polling * * @param void * @return none */ STATIC void hali2cSendDeviceAddress(uint8 address) { uint8 retry = HAL_I2C_RETRY_CNT; do { hali2cStart(); if (hali2cSendByte(address)) // do ack polling... { break; } } while ( --retry ); }
// 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; }
///////////////////////////////////////////////////////////// // ADXL345 Vibrate Sensor //STATIC void hali2cSend( uint8 *buffer, uint16 len, uint8 sendStart, uint8 sendStop ); //STATIC _Bool hali2cSendByte( uint8 dByte ); //STATIC void hali2cWrite( bool dBit ); //STATIC void hali2cClock( bool dir ); //STATIC void hali2cStart( void ); //STATIC void hali2cStop( void ); //STATIC void hali2cReceive( uint8 address, uint8 *buffer, uint16 len ); //STATIC uint8 hali2cReceiveByte( void ); //STATIC _Bool hali2cRead( void ); //STATIC void hali2cSendDeviceAddress(uint8 address); void ADXL345WriteByte(byte address, byte value) { // Address and W/R bit byte firstByte = 0x00; // wait turns to receive the slave ack uint8 retry = HAL_I2C_RETRY_CNT; // Send the start condition hali2cStart(); // Send the slave address + w flag 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 value do { if ( hali2cSendByte(value) ) // takes care of ack polling { break; } } while ( --retry ); // Send stop condition hali2cStop(); }
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(); }