//****************************************************************************
//
//! 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
    }
}
//****************************************************************************
//
//! Write an 8 bit data to a register on the PWM board
//!
//! \param  regAddress: the address of the register
//          data: the 8 bit data
//
//!  \return 0: SUCCESS; >0 Failure
//****************************************************************************
static int write(uint8_t regAddress, uint8_t data) 
{
    unsigned char DataBuf[2];
    int iRetVal;

    //Construct the Data buffer befor sending
    DataBuf[0] = (unsigned char)regAddress;
    DataBuf[1] = (unsigned char)data;

    // Write the data to the register on the PWM Board
    iRetVal = I2C_IF_Write((unsigned char)PWM_ADDRESS, DataBuf, 2, 1);

    if(iRetVal == SUCCESS)
    {
        #ifdef PWM_DEBUG
        //UART_PRINT("I2C Write complete\n\r");
        #endif
        return SUCCESS;
    }
    else
    {
        #ifdef PWM_DEBUG
        UART_PRINT("I2C Write failed\n\r");
        #endif
        return FAILURE;
    }
}
Exemplo n.º 3
0
//****************************************************************************
//
//! Parses the write command parameters and invokes the I2C APIs
//!
//! \param pcInpString pointer to the write command parameters
//! 
//! This function  
//!    1. Parses the write command parameters.
//!    2. Invokes the corresponding I2C APIs
//!
//! \return 0: Success, < 0: Failure.
//
//****************************************************************************
int
ProcessWriteCommand(char *pcInpString)
{
    unsigned char ucDevAddr, ucStopBit, ucLen;
    unsigned char aucDataBuf[256];
    char *pcErrPtr;
    int iRetVal, iLoopCnt;

    //
    // 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 written
    //
    pcInpString = strtok(NULL, " ");
    RETERR_IF_TRUE(pcInpString == NULL);
    ucLen = (unsigned char)strtoul(pcInpString, &pcErrPtr, 10);
    //RETERR_IF_TRUE(ucLen > sizeof(aucDataBuf));

    for(iLoopCnt = 0; iLoopCnt < ucLen; iLoopCnt++)
    {
        //
        // Store the data to be written
        //
        pcInpString = strtok(NULL, " ");
        RETERR_IF_TRUE(pcInpString == NULL);
        aucDataBuf[iLoopCnt] = 
                (unsigned char)strtoul(pcInpString+2, &pcErrPtr, 16);
    }
    
    //
    // Get the stop bit
    //
    pcInpString = strtok(NULL, " ");
    RETERR_IF_TRUE(pcInpString == NULL);
    ucStopBit = (unsigned char)strtoul(pcInpString, &pcErrPtr, 10);
    
    //
    // Write the data to the specified address
    //
    iRetVal = I2C_IF_Write(ucDevAddr, aucDataBuf, ucLen, ucStopBit);
    if(iRetVal == SUCCESS)
    {
        UART_PRINT("I2C Write complete\n\r");
    }
    else
    {
        UART_PRINT("I2C Write failed\n\r");
        return FAILURE;
    }

    return SUCCESS;
}
Exemplo n.º 4
0
struct bm222_ctx *bm222_init(uint8_t addr) {
  struct bm222_ctx *ctx = (struct bm222_ctx *) calloc(1, sizeof(*ctx));
  if (ctx == NULL) return NULL;
  ctx->addr = addr;
  {
    unsigned char val[2] = {BM222_REG_BGW_SOFTRESET, BM222_DO_SOFT_RESET};
    if (I2C_IF_Write(addr, val, 2, 1) != 0) goto out_err;
    osi_Sleep(2 /* ms */); /* t_w,up1 = 1.8 ms */
  }
  if (!bm222_fifo_init(ctx)) return false;
  {
    unsigned char val[2] = {BM222_REG_PMU_BW, BM222_PMU_BW_125_HZ};
    if (I2C_IF_Write(addr, val, 2, 1) != 0) goto out_err;
  }
  return ctx;
out_err:
  free(ctx);
  return NULL;
}
Exemplo n.º 5
0
//****************************************************************************
//
//! Parses the writereg command parameters and invokes the I2C APIs
//! i2c writereg 0x<dev_addr> 0x<reg_offset> <wrlen> <0x<byte0> [0x<byte1> ... ]>
//!
//! \param pcInpString pointer to the readreg command parameters
//! 
//! This function  
//!    1. Parses the writereg command parameters.
//!    2. Invokes the corresponding I2C APIs
//!
//! \return 0: Success, < 0: Failure.
//
//****************************************************************************
int
ProcessWriteRegCommand(char *pcInpString)
{
    unsigned char ucDevAddr, ucRegOffset, ucWrLen;
    unsigned char aucDataBuf[256];
    char *pcErrPtr;
    int iLoopCnt = 0;

    //
    // Get the device address
    //
    pcInpString = strtok(NULL, " ");
    RETERR_IF_TRUE(pcInpString == NULL);
    ucDevAddr = (unsigned char)strtoul(pcInpString+2, &pcErrPtr, 16);
    
    //
    // Get the register offset to be written
    //
    pcInpString = strtok(NULL, " ");
    RETERR_IF_TRUE(pcInpString == NULL);
    ucRegOffset = (unsigned char)strtoul(pcInpString+2, &pcErrPtr, 16);
    aucDataBuf[iLoopCnt] = ucRegOffset;
    iLoopCnt++;
    
    //
    // Get the length of data to be written
    //
    pcInpString = strtok(NULL, " ");
    RETERR_IF_TRUE(pcInpString == NULL);
    ucWrLen = (unsigned char)strtoul(pcInpString, &pcErrPtr, 10);
    //RETERR_IF_TRUE(ucWrLen > sizeof(aucDataBuf));
   
    //
    // Get the bytes to be written
    //
    for(; iLoopCnt < ucWrLen + 1; iLoopCnt++)
    {
        //
        // Store the data to be written
        //
        pcInpString = strtok(NULL, " ");
        RETERR_IF_TRUE(pcInpString == NULL);
        aucDataBuf[iLoopCnt] = 
                (unsigned char)strtoul(pcInpString+2, &pcErrPtr, 16);
    }
    //
    // Write the data values.
    //
    RET_IF_ERR(I2C_IF_Write(ucDevAddr,&aucDataBuf[0],ucWrLen+1,1));

    UART_PRINT("I2C Write To address complete\n\r");

    return SUCCESS;
}
Exemplo n.º 6
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;
}
Exemplo n.º 7
0
//****************************************************************************
//
//! Sets the value in the specified register
//!
//! \param ucRegAddr is the offset register address
//! \param ucRegValue is the register value to be set
//! 
//! This function  
//!    1. Returns the value in the specified register
//!
//! \return 0: Success, < 0: Failure.
//
//****************************************************************************
int
SetRegisterValue(unsigned char ucRegAddr, unsigned char ucRegValue)
{
    unsigned char ucData[2];
    //
    // Select the register to be written followed by the value.
    //
    ucData[0] = ucRegAddr;
    ucData[1] = ucRegValue;
    //
    // Initiate the I2C write
    //
    if(I2C_IF_Write(BMA222_DEV_ADDR,ucData,2,1) == 0)
    {
        return SUCCESS;
    }
    else
    {
        DBG_PRINT("I2C write failed\n\r");
    }

    return FAILURE;
}
Exemplo n.º 8
0
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;
}
Exemplo n.º 9
0
bool bm222_fifo_init(struct bm222_ctx *ctx) {
  unsigned char val[2] = {BM222_REG_FIFO_CONFIG_1,
                          BM222_FIFO_MODE_FIFO | BM222_FIFO_DATA_ALL};
  return (I2C_IF_Write(ctx->addr, val, 2, 1) == 0);
}