Void cycleLED(UArg arg0, UArg arg1) { unsigned int i = 0; uint8_t writeBuffer[4]; I2C_Handle handle; I2C_Params i2cparams; I2C_Transaction i2c; I2C_Params_init(&i2cparams); i2cparams.bitRate = I2C_400kHz; handle = I2C_open(Board_I2C_TPL0401, &i2cparams); if (handle == NULL) { System_abort("I2C was not opened"); } i2c.slaveAddress = Board_TPL0401_ADDR; i2c.readCount = 0; i2c.readBuf = NULL; i2c.writeBuf = writeBuffer; /* Enable the PWM oscillator */ writeBuffer[0] = 0x00; writeBuffer[1] = 0x81; i2c.writeCount = 2; if (!I2C_transfer(handle, &i2c)) { GPIO_write(Board_LED1, Board_LED_ON); System_abort("Bad I2C transfer!"); } /* Bring the LEDs into PWM mode */ writeBuffer[0] = 0x8C; writeBuffer[1] = 0xAA; writeBuffer[2] = 0xAA; i2c.writeCount = 3; if (!I2C_transfer(handle, &i2c)) { GPIO_write(Board_LED1, Board_LED_ON); System_abort("Bad I2C transfer!"); } i2c.writeCount = 4; while (true) { /* Point to the new LED pattern */ i2c.writeBuf = (uint8_t *) &(rgbcmd[i]); if (!I2C_transfer(handle, &i2c)) { GPIO_write(Board_LED1, Board_LED_ON); System_abort("Bad I2C transfer!"); } /* Reached the end of the RGB patterns; reset index */ if (rgbcmd[++i].LED == 0x00) { i = 0; } Task_sleep(100); } }
uint8_t TwoWire::endTransmission(uint8_t sendStop) { bool ret; WireContext *wc = getWireContext(); if (i2c == NULL) { return (4); /* 4 = 'other error' */ } ret = I2C_transfer(i2c, &(wc->i2cTransaction)); wc->txWriteIndex = 0; wc->i2cTransaction.writeCount = 0; wc->rxReadIndex = 0; //i2cTransaction will overwrite buffer staring at 0 wc->rxWriteIndex = wc->i2cTransaction.readCount; wc->i2cTransaction.readCount = 0; if (sendStop) { wc->idle = true; } if (gateEnterCount) { GateMutex_leave(GateMutex_handle(&gate), --gateEnterCount); } /* success = 0; 4 = other error */ return (ret ? 0 : 4); }
// // Burst read from an I2C device // bool bspI2cRead(uint8_t *data, uint8_t len) { masterTransaction.writeCount = 0; masterTransaction.writeBuf = NULL; masterTransaction.readCount = len; masterTransaction.readBuf = data; masterTransaction.slaveAddress = slaveAddr; return I2C_transfer(I2Chandle, &masterTransaction) == TRUE; }
// // Write and read in one operation // bool bspI2cWriteRead(uint8_t *wdata, uint8_t wlen, uint8_t *rdata, uint8_t rlen) { masterTransaction.writeCount = wlen; masterTransaction.writeBuf = wdata; masterTransaction.readCount = rlen; masterTransaction.readBuf = rdata; masterTransaction.slaveAddress = slaveAddr; return I2C_transfer(I2Chandle, &masterTransaction) == TRUE; }
/******************************************************************************* * @fn i2c_escribe * @brief Escribe por i2c * @param pbuf - Puntero al Buffer * @param nBits - Número de bits * @return ok */ bool i2c_escribe(uint8_t *pbuf, uint8_t nBits) { I2C_Transaction masterTransaction; masterTransaction.writeCount = nBits; masterTransaction.writeBuf = pbuf; masterTransaction.readCount = 0; masterTransaction.readBuf = NULL; masterTransaction.slaveAddress = slaveAddr; return I2C_transfer(i2cHandle, &masterTransaction) == TRUE; }
/******************************************************************************* * @fn i2c_escribe_lee * @brief Lee y escribe * @param pBufTx - Buffer Tx * @param nBytesTx - Bytes Tx * @param pBufRx - Buffer Rx * @param nBytesRx - Bytes Rx * @return ok */ bool i2c_escribe_lee(uint8_t *pBufTx, uint8_t nBytesTx, uint8_t *pBufRx, uint8_t nBytesRx) { I2C_Transaction masterTransaction; masterTransaction.writeCount = nBytesTx; masterTransaction.writeBuf = pBufTx; masterTransaction.readCount = nBytesRx; masterTransaction.readBuf = pBufRx; masterTransaction.slaveAddress = slaveAddr; return I2C_transfer(i2cHandle, &masterTransaction) == TRUE; }
// // Burst write to an I2C device // bool HalI2CWrite(uint8_t *data, uint8_t len) { bool fSuccess; #ifdef TI_DRIVERS_I2C_INCLUDED I2C_Transaction masterTransaction; masterTransaction.writeCount = len; masterTransaction.writeBuf = data; masterTransaction.readCount = 0; masterTransaction.readBuf = NULL; masterTransaction.slaveAddress = slaveAddr; fSuccess = I2C_transfer(I2Chandle, &masterTransaction); #endif return fSuccess; }
// // Write and read in once operation // bool HalI2CWriteRead(uint8_t *wdata, uint8_t wlen, uint8_t *rdata, uint8_t rlen) { bool fSuccess; #ifdef TI_DRIVERS_I2C_INCLUDED I2C_Transaction masterTransaction; masterTransaction.writeCount = wlen; masterTransaction.writeBuf = wdata; masterTransaction.readCount = rlen; masterTransaction.readBuf = rdata; masterTransaction.slaveAddress = slaveAddr; fSuccess = I2C_transfer(I2Chandle, &masterTransaction); #endif return fSuccess; }
Void Task_I2C(UArg arg0, UArg arg1) { I2C_Handle i2c; I2C_Params i2cParams; I2C_Transaction i2cTransaction; uint8_t txBuffer[4]; uint8_t rxBuffer[4]; I2C_Params_init(&i2cParams); i2cParams.bitRate = I2C_400kHz; i2cParams.transferMode = I2C_MODE_BLOCKING; i2c = I2C_open(Board_I2C_NFC, &i2cParams); if (i2c == NULL) { System_abort("Error Initializing I2C\n"); } else { System_printf("\nTask_I2C"); } i2cTransaction.slaveAddress = 0x17; for (;;) { i2cTransaction.writeBuf = txBuffer; i2cTransaction.writeCount = sizeof(txBuffer); i2cTransaction.readCount = 0; txBuffer[0]=1; txBuffer[1]=2; txBuffer[2]=3; txBuffer[3]=4; if (!I2C_transfer(i2c, &i2cTransaction)) { System_printf("Bad I2C transfer!"); } Task_sleep(200); //One Tick is 1ms } }
uint8_t BH1750_init(void){ uint8_t mode = BH1750_CONTINUOUS_HIGH_RES_MODE; i2c_BH1750_Transaction.slaveAddress = BH1750_SLAVE_ADDRESS_1; i2c_BH1750_Transaction.writeBuf = txBH1750_Buffer; i2c_BH1750_Transaction.writeCount = 1; i2c_BH1750_Transaction.readBuf = rxBH1750_Buffer; i2c_BH1750_Transaction.readCount = 0; //txBH1750_Buffer[0] = BH1750_POWER_ON; //I2C_transfer(i2c, &i2c_BH1750_Transaction); //__delay_ms(100); switch (mode) { case BH1750_CONTINUOUS_HIGH_RES_MODE: case BH1750_CONTINUOUS_HIGH_RES_MODE_2: case BH1750_CONTINUOUS_LOW_RES_MODE: case BH1750_ONE_TIME_HIGH_RES_MODE: case BH1750_ONE_TIME_HIGH_RES_MODE_2: case BH1750_ONE_TIME_LOW_RES_MODE: txBH1750_Buffer[0] = mode; I2C_transfer(i2c, &i2c_BH1750_Transaction); //__delay_ms(120); break; default: break; } return 1; }
uint16_t get_BH1750_LightLevel(void){ uint16_t level; //BH1750_init(); i2c_BH1750_Transaction.slaveAddress = BH1750_SLAVE_ADDRESS_1; i2c_BH1750_Transaction.writeBuf = txBH1750_Buffer; i2c_BH1750_Transaction.writeCount = 1; i2c_BH1750_Transaction.readBuf = rxBH1750_Buffer; i2c_BH1750_Transaction.readCount = 2; txBH1750_Buffer[0] = BH1750_CONTINUOUS_HIGH_RES_MODE; I2C_transfer(i2c, &i2c_BH1750_Transaction); level = rxBH1750_Buffer[0]; level <<= 8; level |= rxBH1750_Buffer[1]; level /= 1.2; return level; }