/********************************************************************* * @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(); }
/********************************************************************* * @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(); } }
/***************************************************************************** * * * 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 WRITE page uint8_t hali2c_write_page(uint8_t devaddr, uint16_t regaddr, uint8_t *data, uint8_t size) { hali2cSendDeviceAddress((devaddr<<1) | OCM_WRITE); hali2cSendByte(regaddr >> 8); hali2cSendByte(regaddr & 0xff); while (size--) { hali2cSendByte(*data); // // rc = i2c_sda(I2C_CHECK_ACK); *data++; } hali2cStop(); return 1; }
// EEPROM WRITE byte uint8_t hali2cWrite_byte( uint8 devaddr, uint16 regaddr, uint8 data ) { //uint8 ch; // uint16 i; //uint8_t d = 0; hali2cSendDeviceAddress((devaddr<<1) | OCM_WRITE); hali2cSendByte(regaddr >> 8); hali2cSendByte(regaddr & 0xff); hali2cSendByte(data); hali2cStop(); return 1; }
// 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(); }