/******************************************************************************* * @fn bspI2cInit * * @brief Initialize the RTOS I2C driver (must be called only once) * * @param none * * @return none */ void bspI2cInit(void) { Semaphore_Params semParamsMutex; // Create protection semaphore Semaphore_Params_init(&semParamsMutex); semParamsMutex.mode = Semaphore_Mode_BINARY; Semaphore_construct(&mutex, 1, &semParamsMutex); // Reset the I2C controller HapiResetPeripheral(PRCM_PERIPH_I2C0); I2C_init(); I2C_Params_init(&i2cParams); i2cParams.bitRate = I2C_400kHz; i2cHandle = I2C_open(Board_I2C, &i2cParams); // Initialize local variables slaveAddr = 0xFF; interface = BSP_I2C_INTERFACE_0; if (i2cHandle == NULL) { Task_exit(); } }
/******************************************************************************* * @fn i2c_sel_interfas * @brief Selecciona interfas * @param Interfas - 0 o 1=MPU * @param dir - Esclavo * @return ok Siempre */ bool i2c_sel_interface(uint8_t Interface, uint8_t dir) { slaveAddr = dir; //Dispositivo esclavo if (Interface != interface) //no es la actual ya seleccionada? { interface = Interface; I2C_close(i2cHandle); //Cierra apertura actual, si la hay // Sets custom to NULL, selects I2C interface 0 I2C_Params_init(&i2cParams); // Assign I2C data/clock pins according to selected I2C interface 1 if (interface == 1) { //i2cParams.custom = (void*)&pinCfg1; //así lo hacen en el sensor tag i2cParams.custom = (uintptr_t)&pinCfg1; //Modifico para suprimir el warning de tipo, pues espera un puntero uint } // Re-open RTOS driver with new bus pin assignment i2cHandle = I2C_open(Board_I2C, &i2cParams); } return true; }
/******************************************************************************* * @fn bspI2cSelect * * @brief Select an I2C interface and slave * * @param newInterface - selected interface * @param address - slave address * * @return true if success */ bool bspI2cSelect(uint8_t newInterface, uint8_t address) { // Acquire I2C resource if (!Semaphore_pend(Semaphore_handle(&mutex),MS_2_TICKS(I2C_TIMEOUT))) { return false; } // Store new slave address slaveAddr = address; // Interface changed ? if (newInterface != interface) { // Store new interface interface = newInterface; // Shut down RTOS driver I2C_close(i2cHandle); // Sets custom to NULL, selects I2C interface 0 I2C_Params_init(&i2cParams); // Assign I2C data/clock pins according to selected I2C interface 1 if (interface == BSP_I2C_INTERFACE_1) { i2cParams.custom = (void*)&pinCfg1; } // Re-open RTOS driver with new bus pin assignment i2cHandle = I2C_open(Board_I2C, &i2cParams); } return true; }
void bspI2cInit(void) { Semaphore_Params semParamsMutex; // Create protection semaphore Semaphore_Params_init(&semParamsMutex); semParamsMutex.mode = Semaphore_Mode_BINARY; Semaphore_construct(&mutex, 1, &semParamsMutex); // Reset the I2C controller HWREG(PRCM_BASE + PRCM_O_RESETI2C) = PRCM_RESETI2C_I2C; I2C_init(); I2C_Params_init(&I2CParams); I2CParams.bitRate = I2C_400kHz; I2Chandle = I2C_open(Board_I2C, &I2CParams); // Initialize local variables slaveAddr = 0xFF; interface = BSP_I2C_INTERFACE_0; #ifdef POWER_SAVING checkI2cConstraint = false; #endif /* if (I2Chandle == NULL) { while(1) { // wait here for ever } } */ }
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); } }
void HalI2CInit(void) { #ifdef TI_DRIVERS_I2C_INCLUDED Board_initI2C(); // Setup I2C and initialize the driver. I2C_Params_init(&I2CParams); I2CParams.bitRate = I2C_400kHz; I2Chandle = I2C_open(Board_I2C, &I2CParams); if (I2Chandle == NULL) { while(1) { // whait here for ever } } #endif }
/******************************************************************************* * @fn i2c_inicia * @brief Inicia i2c */ bool i2c_inicia(void) { I2C_init(); I2C_Params_init(&i2cParams); i2cParams.bitRate = I2C_400kHz; i2cHandle = I2C_open(Board_I2C, &i2cParams); slaveAddr = 0xFF; interface = 0; if (i2cHandle == NULL) { return false; } return true; }
// Initialize as a master void TwoWire::begin(void) { I2C_Params params; /* return if I2C already started */ if (begun == TRUE) return; I2C_Params_init(¶ms); params.transferMode = I2C_MODE_BLOCKING; params.bitRate = I2C_400kHz; i2c = Board_openI2C(i2cModule, ¶ms); if (i2c != NULL) { GateMutex_construct(&gate, NULL); gateEnterCount = 0; begun = TRUE; } }
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 } }
/*! * @brief Function to initialize a given I2C CC26XX peripheral specified by the * particular handle. The parameter specifies which mode the I2C * will operate. * * After calling the open function, the I2C is enabled. If there is no active * I2C transactions, the device can enter standby. * * @pre The I2CCC26XX_Config structure must exist and be persistent before this * function can be called. I2CCC26XX has been initialized with I2CCC26XX_init(). * Calling context: Task * * @param handle An I2C_Handle * * @param params Pointer to a parameter block, if NULL it will use default values. * * @return A I2C_Handle on success, or a NULL on an error or if it has been * already opened. * * @note The generic I2C API should be used when accessing the I2CCC26XX. * * @sa I2CCC26XX_close(), I2CCC26XX_init(), I2C_open(), I2C_init() */ I2C_Handle I2CCC26XX_open(I2C_Handle handle, I2C_Params *params) { union { Hwi_Params hwiParams; Semaphore_Params semParams; } paramsUnion; UInt key; I2C_Params i2cParams; I2CCC26XX_Object *object; I2CCC26XX_HWAttrs const *hwAttrs; /* Get the pointer to the object and hwAttrs */ object = handle->object; hwAttrs = handle->hwAttrs; /* Determine if the device index was already opened */ key = Hwi_disable(); if(object->isOpen == true){ Hwi_restore(key); return (NULL); } /* Mark the handle as being used */ object->isOpen = true; Hwi_restore(key); /* Store the I2C parameters */ if (params == NULL) { /* No params passed in, so use the defaults */ I2C_Params_init(&i2cParams); params = &i2cParams; } /* Configure the IOs early to ensure allocation is allowed (PIN driver and IO config setup only).*/ if (I2CCC26XX_initIO(handle, params->custom)) { /* If initialization and allocation of IOs failed, log error and return NULL pointer */ Log_print1(Diags_USER1, "I2C: Pin allocation failed, open did not succeed (baseAddr:0x%x)", hwAttrs->baseAddr); return (NULL); } /* Save parameters */ object->transferMode = params->transferMode; object->transferCallbackFxn = params->transferCallbackFxn; object->bitRate = params->bitRate; /* Create Hwi object for this I2C peripheral */ Hwi_Params_init(¶msUnion.hwiParams); paramsUnion.hwiParams.arg = (UArg)handle; Hwi_construct(&(object->hwi), hwAttrs->intNum, I2CCC26XX_hwiFxn, ¶msUnion.hwiParams, NULL); /* * Create thread safe handles for this I2C peripheral * Semaphore to provide exclusive access to the I2C peripheral */ Semaphore_Params_init(¶msUnion.semParams); paramsUnion.semParams.mode = Semaphore_Mode_BINARY; Semaphore_construct(&(object->mutex), 1, ¶msUnion.semParams); /* * Store a callback function that posts the transfer complete * semaphore for synchronous mode */ if (object->transferMode == I2C_MODE_BLOCKING) { /* * Semaphore to cause the waiting task to block for the I2C * to finish */ Semaphore_construct(&(object->transferComplete), 0, ¶msUnion.semParams); /* Store internal callback function */ object->transferCallbackFxn = I2CCC26XX_blockingCallback; } else { /* Check to see if a callback function was defined for async mode */ Assert_isTrue(object->transferCallbackFxn != NULL, NULL); } /* Specify the idle state for this I2C peripheral */ object->mode = I2CCC26XX_IDLE_MODE; /* Clear the head pointer */ object->headPtr = NULL; object->tailPtr = NULL; /* Power on the I2C module */ Power_setDependency(hwAttrs->powerMngrId); /* Initialize the I2C hardware module */ I2CCC26XX_initHw(handle); /* Register notification functions */ Power_registerNotify(&object->i2cPostObj, Power_AWAKE_STANDBY, (Fxn)i2cPostNotify, (UInt32)handle, NULL ); Log_print1(Diags_USER1, "I2C: Object created 0x%x", hwAttrs->baseAddr); /* Return the address of the handle */ return (handle); }