/*---------------------------------------------------------------------------* * Routine: SetLevel *---------------------------------------------------------------------------* * Description: * Set the gain of the audio amp to the desired level * Inputs: * void *aWorkspace * TUInt8 aLevel --new level to set the amp to * Outputs: * T_uezError -- Error code *---------------------------------------------------------------------------*/ T_uezError AudioAmp_WM8731_SetLevel(void *aWorkSpace, TUInt8 aLevel) { T_AudioAmp_WM8731_Workspace *p = (T_AudioAmp_WM8731_Workspace *)aWorkSpace; T_uezDevice i2c; TUInt8 data[4]; TUInt8 setLevel; UEZSemaphoreGrab(p->iSem, UEZ_TIMEOUT_INFINITE); p->iLevel = aLevel; setLevel = ((p->iLevel * (p->iMaxLevel - p->iMinLevel))/0xFF) + p->iMinLevel; if(!p->iIsMuted){ if(UEZI2COpen(p->iI2CBus, &i2c) == UEZ_ERROR_NONE){ data[0] = WM8731_LOUT1V; data[1] = (0 << 7) | //RZCEN setLevel; //VOL UEZI2CWrite(i2c, WM8731_ADDR, WM8731_I2C_Speed, data, 2, 100); data[0] = WM8731_ROUT1V; data[1] = (0 << 7) | //RZCEN setLevel; //VOL UEZI2CWrite(i2c, WM8731_ADDR, WM8731_I2C_Speed, data, 3, 100); UEZI2CClose(i2c); } } UEZSemaphoreRelease(p->iSem); return UEZ_ERROR_NONE; }
TUInt32 TestVCNL4010Task(T_uezTask aMyTask, void *aParameters) { TUInt8 cmd[2]; TUInt8 data[4]; TUInt32 ambient; TUInt32 proximity; int count = 0; // Set Current to 20 cmd[0] = REGISTER_PROX_CURRENT; cmd[1] = 20; // 20 x 10 = 200 mA, max UEZI2CWrite(G_i2cBus, G_i2cAddr, I2C_SPEED, cmd, 2, 1000); for(count = 0; count < 100; count++) { printf("Reading ... "); // Request to trigger both ALS and Proximity cmd[0] = REGISTER_COMMAND; cmd[1] = 0x18; UEZI2CWrite(G_i2cBus, G_i2cAddr, I2C_SPEED, cmd, 2, 1000); do { printf("?"); // Now read back the status (after writing command index) cmd[0] = REGISTER_COMMAND; UEZI2CWrite(G_i2cBus, G_i2cAddr, I2C_SPEED, cmd, 1, 1000); UEZI2CRead(G_i2cBus, G_i2cAddr, I2C_SPEED, data, 1, 1000); // Is it ready? if (data[0] & (COMMAND_MASK_PROX_DATA_READY | COMMAND_MASK_AMBI_DATA_READY)) break; // Not ready yet, pause and try again in a moment UEZTaskDelay(10); } while (1); // Now read the data cmd[0] = REGISTER_AMBI_VALUE; UEZI2CWrite(G_i2cBus, G_i2cAddr, I2C_SPEED, cmd, 1, 1000); UEZI2CRead(G_i2cBus, G_i2cAddr, I2C_SPEED, data, 4, 1000); ambient = (data[0] << 8) | data[1]; proximity = (data[2] << 8) | data[3]; printf("Ambient: %04X, Proximity: %04X\n", ambient, proximity); UEZTaskDelay(100); } return UEZ_ERROR_NONE; }
/*---------------------------------------------------------------------------* * Routine: SetLevel *---------------------------------------------------------------------------* * Description: * Set the gain of the audio amp to the desired level * Inputs: * void *aWorkspace * TUInt8 aLevel --new level to set the amp to * Outputs: * T_uezError -- Error code *---------------------------------------------------------------------------*/ T_uezError AudioAmp_LM48110_SetLevel(void *aWorkSpace, TUInt8 aLevel) { T_AudioAmp_LM48110_Workspace *p = (T_AudioAmp_LM48110_Workspace *)aWorkSpace; T_uezDevice i2c; TUInt8 data[5] = { 0x1C, //Turn on both inputs 0x20, //Mask for Diagnostic 0x40, //Mask for Fault }; TUInt8 setLevel; UEZSemaphoreGrab(p->iSem, UEZ_TIMEOUT_INFINITE); p->iLevel = aLevel; setLevel = ((p->iLevel * (p->iMaxLevel - p->iMinLevel))/0xFF) + p->iMinLevel; data[3] = (VOLUME_1_MASK | setLevel); data[4] = (VOLUME_2_MASK | setLevel); if(!p->iIsMuted){ if(UEZI2COpen(p->iI2CBus, &i2c) == UEZ_ERROR_NONE){ UEZI2CWrite(i2c, LM48100_ADDR, LM48100_SPEED, data, 5, 100);//UEZ_TIMEOUT_INFINITE); UEZI2CClose(i2c); } } UEZSemaphoreRelease(p->iSem); return UEZ_ERROR_NONE; }
/*-------------------------------------------------------------------------* * Prototypes: *-------------------------------------------------------------------------*/ TUInt32 TestVCNL4010FCT(const char *aI2CName, TUInt8 aI2CAddr, TUInt32 * const ambient, TUInt32 * const proximity) { TUInt8 cmd[2]; TUInt8 data[4]; UEZI2COpen(aI2CName, &G_i2cBus); G_i2cAddr = aI2CAddr; // Set Current to 20 cmd[0] = REGISTER_PROX_CURRENT; cmd[1] = 20; // 20 x 10 = 200 mA, max UEZI2CWrite(G_i2cBus, G_i2cAddr, I2C_SPEED, cmd, 2, 1000); printf("Reading ... "); // Request to trigger both ALS and Proximity cmd[0] = REGISTER_COMMAND; cmd[1] = 0x18; UEZI2CWrite(G_i2cBus, G_i2cAddr, I2C_SPEED, cmd, 2, 1000); do { printf("?"); // Now read back the status (after writing command index) cmd[0] = REGISTER_COMMAND; UEZI2CWrite(G_i2cBus, G_i2cAddr, I2C_SPEED, cmd, 1, 1000); UEZI2CRead(G_i2cBus, G_i2cAddr, I2C_SPEED, data, 1, 1000); // Is it ready? if (data[0] & (COMMAND_MASK_PROX_DATA_READY | COMMAND_MASK_AMBI_DATA_READY)) break; // Not ready yet, pause and try again in a moment UEZTaskDelay(10); } while (1); // Now read the data cmd[0] = REGISTER_AMBI_VALUE; UEZI2CWrite(G_i2cBus, G_i2cAddr, I2C_SPEED, cmd, 1, 1000); UEZI2CRead(G_i2cBus, G_i2cAddr, I2C_SPEED, data, 4, 1000); *ambient = (data[0] << 8) | data[1]; *proximity = (data[2] << 8) | data[3]; printf("Ambient: %04X, Proximity: %04X\n", *ambient, *proximity); UEZTaskDelay(100); return UEZ_ERROR_NONE; }
/*---------------------------------------------------------------------------* * Routine: Mute *---------------------------------------------------------------------------* * Description: * Sets the amp to standby so no sound is played * Inputs: * void *aWorkSpace * Outputs: * T_uezError -- Error code *---------------------------------------------------------------------------*/ T_uezError AudioAmp_WM8731_Mute(void *aWorkSpace) { T_AudioAmp_WM8731_Workspace *p = (T_AudioAmp_WM8731_Workspace *)aWorkSpace; T_uezDevice i2c; TUInt8 data[4]; UEZSemaphoreGrab(p->iSem, UEZ_TIMEOUT_INFINITE); if(UEZI2COpen(p->iI2CBus, &i2c) == UEZ_ERROR_NONE){ data[0] = WM8731_LOUT1V; data[1] = (0 << 7) | //RZCEN (0x00); //VOL UEZI2CWrite(i2c, WM8731_ADDR, WM8731_I2C_Speed, data, 2, 100); data[0] = WM8731_ROUT1V; data[1] = (0 << 7) | //RZCEN (0x00); //VOL UEZI2CWrite(i2c, WM8731_ADDR, WM8731_I2C_Speed, data, 3, 100); UEZI2CClose(i2c); } UEZSemaphoreRelease(p->iSem); return UEZ_ERROR_NONE; }
/*---------------------------------------------------------------------------* * Routine: Mute *---------------------------------------------------------------------------* * Description: * Sets the amp to standby so no sound is played * Inputs: * void *aWorkSpace * Outputs: * T_uezError -- Error code *---------------------------------------------------------------------------*/ T_uezError AudioAmp_LM48110_Mute(void *aWorkSpace) { T_AudioAmp_LM48110_Workspace *p = (T_AudioAmp_LM48110_Workspace *)aWorkSpace; T_uezDevice i2c; TUInt8 data[5] = { 0x1C, //Turn on both inputs 0x20, //Mask for Diagnostic 0x40, //Mask for Fault }; UEZSemaphoreGrab(p->iSem, UEZ_TIMEOUT_INFINITE); data[3] = (VOLUME_1_MASK | 0); data[4] = (VOLUME_2_MASK | 0); p->iIsMuted = ETrue; if(UEZI2COpen(p->iI2CBus, &i2c) == UEZ_ERROR_NONE){ UEZI2CWrite(i2c, LM48100_ADDR, LM48100_SPEED, data, 5, 100);//UEZ_TIMEOUT_INFINITE); UEZI2CClose(i2c); } UEZSemaphoreRelease(p->iSem); return UEZ_ERROR_NONE; }
/*---------------------------------------------------------------------------* * Routine: Open *---------------------------------------------------------------------------* * Description: * Initalizes pins for the audio amp * turns it on * sets the level to the default * Inputs: * void *aWorkSpace * Outputs: * T_uezError -- Error code *---------------------------------------------------------------------------*/ T_uezError AudioAmp_LM48110_Open(void *aWorkSpace) { T_AudioAmp_LM48110_Workspace *p = (T_AudioAmp_LM48110_Workspace *)aWorkSpace; T_uezDevice i2c; T_uezError error = UEZ_ERROR_NONE; TUInt8 data[5] = { 0x1C, //Turn on both inputs 0x20, //Mask for Diagnostic 0x40, //Mask for Fault (VOLUME_1_MASK | 0x00), //Set the volume to 0 (VOLUME_2_MASK | 0x00)}; // Set the volume to 0 UEZSemaphoreGrab(p->iSem, UEZ_TIMEOUT_INFINITE); if(p->iNumOpen == 0){ p->iIsOn = ETrue; if(UEZI2COpen(p->iI2CBus, &i2c) == UEZ_ERROR_NONE){ p->iNumOpen++; error = UEZI2CWrite(i2c, LM48100_ADDR, LM48100_SPEED, data, 5, 100);//UEZ_TIMEOUT_INFINITE); UEZI2CClose(i2c); } p->iLevel = 0; } UEZSemaphoreRelease(p->iSem); return error; }
/*---------------------------------------------------------------------------*/ int UEZCmdI2CWrite(void *aWorkspace, int argc, char *argv[]) { T_uezDevice i2c; T_uezError error; TUInt8 addr; TUInt32 v; TUInt8 data[128]; char busName[] = "I2C0"; int i; if (argc >= 4) { busName[3] = argv[1][0]; // Open up the bus error = UEZI2COpen(busName, &i2c); if (error) { FDICmdPrintf(aWorkspace, "I2C Bus %s not found!\n", busName); return UEZ_ERROR_NOT_FOUND; } // Get the address addr = FDICmdUValue(argv[2]); if (addr >= 128) { FDICmdPrintf(aWorkspace, "Address must be 7-bit (0-127)\n"); UEZI2CClose(i2c); return UEZ_ERROR_INVALID_PARAMETER; } // Setup the data to be written for (i = 3; (i < argc) && (i < sizeof(data)); i++) { v = FDICmdUValue(argv[i]); if (v >= 256) { FDICmdPrintf(aWorkspace, "Values in write must be bytes (0-255). %d is out of range.\n", v); UEZI2CClose(i2c); return UEZ_ERROR_INVALID_PARAMETER; } data[i-3] = v; } // Write the data FDICmdPrintf(aWorkspace, "Writing to bus %s, addr 0x%02X:\n", busName, addr); // For now, assume 100 kHz is valid error = UEZI2CWrite(i2c, addr, 100, data, argc-3, 1000); if (error == UEZ_ERROR_NONE) { FDICmdSendString(aWorkspace, "PASS: OK\n"); } else if (error == UEZ_ERROR_NAK) { FDICmdSendString(aWorkspace, "FAIL: NAK\n"); } else if (error == UEZ_ERROR_TIMEOUT) { FDICmdSendString(aWorkspace, "FAIL: Timeout!\n"); } else { FDICmdPrintf(aWorkspace, "FAIL: Error #%d\n", error); } UEZI2CClose(i2c); } else { FDICmdSendString(aWorkspace, "FAIL: Incorrect parameters.\nI2CWRITE <bus number> <addr> [<byte> ...]\n"); } return UEZ_ERROR_NONE; }
/*---------------------------------------------------------------------------* * Routine: TS_MC_AR1020_Open *---------------------------------------------------------------------------* * Description: * The TI TSC2046 is being opened. * Inputs: * void *aW -- Particular SPI workspace * Outputs: * T_uezError -- Error code *---------------------------------------------------------------------------*/ T_uezError TS_MC_AR1020_Open(void *aW) { T_MC_AR1020Workspace *p = (T_MC_AR1020Workspace *)aW; T_uezError error = UEZ_ERROR_NONE; I2C_Request r; T_uezDevice i2c2; TUInt8 dataout[4] = {0x00, 0x55,0x01,EnableTouch}; TUInt8 datain[4] = {0xff, 0xff, 0xff, 0xff}; TUInt8 commandToSend[8] = {0x00, 0x55,0x05,RegisterWrite,0x00, 0x00, 0x01,0x60 }; TUInt8 regstart[6]; HAL_GPIOPort **p_gpio; const TUInt32 pin = 15; TUInt32 Read = 0; HALInterfaceFind("GPIO2", (T_halWorkspace **)&p_gpio); (*p_gpio)->SetInputMode(p_gpio, 1<<pin); (*p_gpio)->SetMux(p_gpio, pin, 0); // set to GPIO (*p_gpio)->Read(p_gpio, 1<<pin, &Read); if(p->aNumOpen == 0) { error = UEZI2COpen("I2C2", &i2c2); error = UEZI2CWrite(i2c2, AR1020_I2C_ADDRESS, AR1020_I2C_SPEED, dataout, 4, UEZ_TIMEOUT_INFINITE); while(Read == 0) { (*p_gpio)->Read(p_gpio, 1<<pin, &Read); } error = UEZI2CRead(i2c2, AR1020_I2C_ADDRESS, AR1020_I2C_SPEED, datain, 5, UEZ_TIMEOUT_INFINITE); //send command to change TouchThreshold to 0x60 // to write a register first send RegisterStartAddressRequest 0x22 // calculate the address by adding the offset of the register to the start address TouchThreshold 0x02 // issue the register write command RegisterWrite 0x21 dataout[0] = 0; dataout[3] = RegisterStartAddressRequest; //get start address error = UEZI2CWrite(i2c2, AR1020_I2C_ADDRESS, AR1020_I2C_SPEED, dataout, 4, UEZ_TIMEOUT_INFINITE); while(Read == 0) { (*p_gpio)->Read(p_gpio, 1<<pin, &Read); } error = UEZI2CRead(i2c2, AR1020_I2C_ADDRESS, AR1020_I2C_SPEED, regstart, 6, UEZ_TIMEOUT_INFINITE); // commandToSend[5] = regstart[4]+ TouchThreshold; error = UEZI2CWrite(i2c2, AR1020_I2C_ADDRESS, AR1020_I2C_SPEED, commandToSend, 8, UEZ_TIMEOUT_INFINITE); while(Read == 0) { (*p_gpio)->Read(p_gpio, 1<<pin, &Read); } error = UEZI2CRead(i2c2, AR1020_I2C_ADDRESS, AR1020_I2C_SPEED, datain, 4, UEZ_TIMEOUT_INFINITE); commandToSend[5] = SensitivityFilter; commandToSend[6] = 1; commandToSend[7] = 10; Read = 0; error = UEZI2CWrite(i2c2, AR1020_I2C_ADDRESS, AR1020_I2C_SPEED, commandToSend, 8, UEZ_TIMEOUT_INFINITE); // while(Read == 0) // { // (*p_gpio)->Read(p_gpio, 1<<pin, &Read); // } // error = UEZI2CRead(i2c2, AR1020_I2C_ADDRESS, AR1020_I2C_SPEED, datain, 4, UEZ_TIMEOUT_INFINITE); UEZI2CClose(i2c2); } p->aNumOpen++; return error; }