//**************************************************************************** // //! Read an 8 bit data from a register on the PWM board //! //! \param regAddress: register's address // //! \return 8 bit data //**************************************************************************** static uint8_t read(uint8_t regAddress) { int iRetVal; unsigned char DataBuf[1]; //Construct the data buffer DataBuf[0] = (unsigned char) regAddress; //Send command to the PWM board iRetVal = I2C_IF_Write((unsigned char)PWM_ADDRESS, DataBuf, 1, 1); if(iRetVal == FAILURE) { #ifdef PWM_DEBUG UART_PRINT("I2C Read failed\n\r"); #endif return 0; // TODO: figure out error code, use 0 as temp } //Read the value from the register iRetVal = I2C_IF_Read((unsigned char)PWM_ADDRESS, DataBuf, 1); if(iRetVal == SUCCESS) { #ifdef PWM_DEBUG //UART_PRINT("I2C Read complete\n\r"); #endif return ((uint8_t)DataBuf[0]); } else { #ifdef PWM_DEBUG UART_PRINT("I2C Read failed\n\r"); #endif return 0;// TODO: figure out error code, use 0 as temp } }
int HDC1050::get_tempature_and_humidity(HDC1050::reading &measurement) { // execute pointer write transaction at 0x00 int rc = I2C_IF_Write(DEVICE_ADDRESS,0 /* device address*/, 1 /* bytes to read */, 0 /*no stop bit */); // wait at least 6.5 milliseconds - every 1000 ticks is 12.5 us MAP_UtilsDelay(10000000); static uint8_t temperature[2]; static uint8_t humidity[2]; uint32_t temp = 0; uint32_t hum = 0; // read I2C_IF_Read(DEVICE_ADDRESS + Registers8::temperature, &temperature[0], 2); I2C_IF_Read(DEVICE_ADDRESS + Registers8::humidity , &humidity[0], 2); // process readings temp = temperature[1]; temp = temp << 4; temp += temperature[0]; temp = temp * 165 - 40; temp = temp << 16; measurement.temperature = temp; // celsius hum = humidity[1]; hum *= 100; hum = hum << 16; measurement.humidity = hum; // relative humidty as percentage // todo: check for nack return 0; }
//**************************************************************************** // //! Parses the readreg command parameters and invokes the I2C APIs //! i2c readreg 0x<dev_addr> 0x<reg_offset> <rdlen> //! //! \param pcInpString pointer to the readreg command parameters //! //! This function //! 1. Parses the readreg command parameters. //! 2. Invokes the corresponding I2C APIs //! //! \return 0: Success, < 0: Failure. // //**************************************************************************** int ProcessReadRegCommand(char *pcInpString) { unsigned char ucDevAddr, ucRegOffset, ucRdLen; unsigned char aucRdDataBuf[256]; char *pcErrPtr; // // Get the device address // pcInpString = strtok(NULL, " "); RETERR_IF_TRUE(pcInpString == NULL); ucDevAddr = (unsigned char)strtoul(pcInpString+2, &pcErrPtr, 16); // // Get the register offset address // pcInpString = strtok(NULL, " "); RETERR_IF_TRUE(pcInpString == NULL); ucRegOffset = (unsigned char)strtoul(pcInpString+2, &pcErrPtr, 16); // // Get the length of data to be read // pcInpString = strtok(NULL, " "); RETERR_IF_TRUE(pcInpString == NULL); ucRdLen = (unsigned char)strtoul(pcInpString, &pcErrPtr, 10); //RETERR_IF_TRUE(ucLen > sizeof(aucDataBuf)); // // Write the register address to be read from. // Stop bit implicitly assumed to be 0. // RET_IF_ERR(I2C_IF_Write(ucDevAddr,&ucRegOffset,1,0)); // // Read the specified length of data // RET_IF_ERR(I2C_IF_Read(ucDevAddr, &aucRdDataBuf[0], ucRdLen)); UART_PRINT("I2C Read From address complete\n\r"); // // Display the buffer over UART on successful readreg // DisplayBuffer(aucRdDataBuf, ucRdLen); return SUCCESS; }
//**************************************************************************** // //! Parses the read command parameters and invokes the I2C APIs //! //! \param pcInpString pointer to the user command parameters //! //! This function //! 1. Parses the read command parameters. //! 2. Invokes the corresponding I2C APIs //! //! \return 0: Success, < 0: Failure. // //**************************************************************************** int ProcessReadCommand(char *pcInpString) { unsigned char ucDevAddr, ucLen; unsigned char aucDataBuf[256]; char *pcErrPtr; int iRetVal; // // Get the device address // pcInpString = strtok(NULL, " "); RETERR_IF_TRUE(pcInpString == NULL); ucDevAddr = (unsigned char)strtoul(pcInpString+2, &pcErrPtr, 16); // // Get the length of data to be read // pcInpString = strtok(NULL, " "); RETERR_IF_TRUE(pcInpString == NULL); ucLen = (unsigned char)strtoul(pcInpString, &pcErrPtr, 10); //RETERR_IF_TRUE(ucLen > sizeof(aucDataBuf)); // // Read the specified length of data // iRetVal = I2C_IF_Read(ucDevAddr, aucDataBuf, ucLen); if(iRetVal == SUCCESS) { UART_PRINT("I2C Read complete\n\r"); // // Display the buffer over UART on successful write // DisplayBuffer(aucDataBuf, ucLen); } else { UART_PRINT("I2C Read failed\n\r"); return FAILURE; } return SUCCESS; }