/* ****************************************************************************** * * Local Function * ****************************************************************************** */ static unsigned char adxl345_reg_write(unsigned char addr, unsigned char val) { unsigned char buf[2]; buf[0] = addr; // register address buf[1] = val; // register value return HalI2CWrite(sizeof(buf), buf); }
static unsigned char adxl345_reg_multi_read(unsigned char addr, unsigned char n, unsigned char *val) { /* send address we're reading from */ if (HalI2CWrite(1, &addr) == 1) { /* now read data */ return HalI2CRead(n, val); } return 0; }
unsigned char ssd1306_data(unsigned char data) { unsigned char buf[2]; buf[0] = 0x40; // [7] : continuation bit // [6] : data / command selection bit buf[1] = data; ssd1306_select(); return HalI2CWrite(sizeof(buf), buf); }
/************************************************************************************************** * @fn HalMotionI2cRead * * @brief This function implements the I2C protocol to read from the IMU-3000 gyroscope. * * input parameters * * @param device - which device is being written * addr - which register to read * numBytes - number of bytes to read * pBuf - pointer to buffer to place data * * output parameters * * None. * * @return None. **************************************************************************************************/ void HalMotionI2cRead( halMotionDevice_t device, uint8 addr, uint8 numBytes, uint8 *pBuf ) { uint8 localAddr = addr; /* Send address we're reading from */ HalI2CWrite( HalMotionDeviceAddressTable[device], 1, &localAddr ); /* Now read data */ HalI2CRead( HalMotionDeviceAddressTable[device], numBytes, pBuf ); }
// // Single write to on I2C device // bool HalI2CWriteSingle(uint8_t data) { #ifdef TI_DRIVERS_I2C_INCLUDED uint8_t d; // This is not optimal but is an implementation d = data; return (HalI2CWrite(&d, 1)); #endif }
/************************************************************************************************** * @fn HalSensorReadReg * * @brief This function implements the I2C protocol to read from a sensor. The sensor must * be selected before this routine is called. * * @param addr - which register to read * @param pBuf - pointer to buffer to place data * @param nBytes - numbver of bytes to read * * @return TRUE if the required number of bytes are reveived **************************************************************************************************/ bool HalSensorReadReg(uint8 addr, uint8 *pBuf, uint8 nBytes) { uint8 i = 0; /* Send address we're reading from */ if (HalI2CWrite(1,&addr) == 1) { /* Now read data */ i = HalI2CRead(nBytes,pBuf); } return i == nBytes; }
/************************************************************************************************** * @fn HalMotionI2cWrite * * @brief This function implements the I2C protocol to write to the IMU-3000 gyroscope. * * input parameters * * @param device - which device is being written * addr - which register to write * pData - pointer to buffer containing data to be written * numBytes - number of bytes of data to be written * * output parameters * * None. * * @return None. **************************************************************************************************/ void HalMotionI2cWrite( halMotionDevice_t device, uint8 addr, uint8 *pData, uint8 numBytes ) { static uint8 HalMotionI2CBuf[20]; uint8 i; uint8 *pBuf = HalMotionI2CBuf; /* Copy address and data to local buffer for burst write */ *pBuf++ = addr; for (i = 0; i < numBytes; i++) { *pBuf++ = *pData++; } /* Send address and data */ HalI2CWrite( HalMotionDeviceAddressTable[device], numBytes + 1, HalMotionI2CBuf ); }
/************************************************************************************************** * @fn HalSensorWriteReg * @brief This function implements the I2C protocol to write to a sensor. he sensor must * be selected before this routine is called. * * @param addr - which register to write * @param pBuf - pointer to buffer containing data to be written * @param nBytes - number of bytes to write * * @return TRUE if successful write */ bool HalSensorWriteReg(uint8 addr, uint8 *pBuf, uint8 nBytes) { uint8 i; uint8 *p = buffer; /* Copy address and data to local buffer for burst write */ *p++ = addr; for (i = 0; i < nBytes; i++) { *p++ = *pBuf++; } nBytes++; /* Send address and data */ i = HalI2CWrite(nBytes, buffer); if ( i!= nBytes) HAL_TOGGLE_LED2(); return (i == nBytes); }
/************************************************************************************************** * @fn halHumiWriteCmd * * @brief Write a command to the humidity sensor * * @param cmd - command to write * * @return TRUE if the command has been transmitted successfully **************************************************************************************************/ static bool HalHumiWriteCmd(uint8 cmd) { /* Send command */ return HalI2CWrite(1,&cmd) == 1; }