void *GPSLoop(void *some_void_ptr) { unsigned char Line[100]; int id, Length; struct i2c_info bb; struct TGPS *GPS; GPS = (struct TGPS *)some_void_ptr; Length = 0; while (1) { int i; unsigned char Character; printf ("SDA/SCL = %d/%d\n", Config.SDA, Config.SCL); if (OpenI2C(&bb, 0x42, Config.SDA, Config.SCL, 10, 100)) // struct, i2c address, SDA, SCL, us clock delay, timeout ms { printf("Failed to open I2C\n"); exit(1); } SetFlightMode(&bb); while (!bb.Failed) { Character = I2CGetc(&bb); if (Character == 0xFF) { delayMilliseconds (100); } else if (Character == '$') { Line[0] = Character; Length = 1; } else if (Length > 90) { Length = 0; } else if ((Length > 0) && (Character != '\r')) { Line[Length++] = Character; if (Character == '\n') { Line[Length] = '\0'; // puts(Line); ProcessLine(&bb, GPS, Line, Length); delayMilliseconds (100); Length = 0; } } } ResetI2C(&bb); } }
void Read_MPU6050(unsigned char value[]){ char writeaddress = (0x68 << 1)|I2C_WRITE; char readaddress = writeaddress + 0x01; int i=0; I2C1CONbits.ACKDT = 0; //set ACK while(I2Cstartevent()==0){ sprintf(NU32_RS232OutBuffer,"Trying to stop bus...\r\n"); WriteString(UART2,NU32_RS232OutBuffer); ResetI2C(); } I2Csendonebyte(writeaddress); I2Csendonebyte(0x3B); I2Crepeatstartevent(); I2Csendonebyte(readaddress); for(i=0; i<13; i++) { I2Creadonebyte(&value[i]); I2C1CONbits.ACKEN=1; while(I2C1CONbits.ACKEN); } I2Creadonebyte(&value[13]); I2C1CONbits.ACKDT = 1; I2C1CONbits.ACKEN=1; //Set NACK while(I2C1CONbits.ACKEN); I2Cstopevent(); }
/** * All modules MUST provide a FULL cleanup function. This must cleanup all allocations etc * e.g. we will have to populate this when we add circular buffers! * **/ static BT_ERROR i2cCleanup(BT_HANDLE hI2C) { ResetI2C(hI2C); // Disable peripheral clock. disablei2cPeripheralClock(hI2C); const BT_RESOURCE *pResource = BT_GetIntegratedResource(hI2C->i2c_master.pDevice, BT_RESOURCE_IRQ, 0); BT_DisableInterrupt(pResource->ulStart); return BT_ERR_NONE; }
//This function initiates a full transmission to write a single byte to //a register of a slave device via I2C. The device address is the 7 bit //address of the slave device with a 0 as the least significant bit. void I2Cwrite(char deviceaddress, char registeraddress, char value){ while(I2Cstartevent()==0){ sprintf(NU32_RS232OutBuffer,"Trying to stop bus...\r\n"); WriteString(UART2,NU32_RS232OutBuffer); ResetI2C(); } I2Csendonebyte(deviceaddress); I2Csendonebyte(registeraddress); I2Csendonebyte(value); I2Cstopevent(); }
//This function initiates a full transmission to read a single byte from //a register of a slave device via I2C.The device address is the 7 bit //address of the slave device with a 0 as the least significant bit. void I2Cread(char writeaddress, char registeraddress, char * value){ char readaddress = writeaddress + 0x01; while(I2Cstartevent()==0){ sprintf(NU32_RS232OutBuffer,"Trying to stop bus...\r\n"); WriteString(UART2,NU32_RS232OutBuffer); ResetI2C(); } I2Csendonebyte(writeaddress); I2Csendonebyte(registeraddress); I2Crepeatstartevent(); I2Csendonebyte(readaddress); I2Creadonebyte(value); I2Cstopevent(); }
void Read_HMC5883L(unsigned char value[]){ char writeaddress = (0x1E << 1)|I2C_WRITE; char readaddress = writeaddress + 0x01; I2C1CONbits.ACKDT = 0; //set ACK while(I2Cstartevent()==0){ sprintf(NU32_RS232OutBuffer,"Trying to stop bus...\r\n"); WriteString(UART2,NU32_RS232OutBuffer); ResetI2C(); } I2Csendonebyte(writeaddress); I2Csendonebyte(0x03); I2Crepeatstartevent(); I2Csendonebyte(readaddress); I2Creadonebyte(&value[0]); I2C1CONbits.ACKEN=1; while(I2C1CONbits.ACKEN); I2Creadonebyte(&value[1]); I2C1CONbits.ACKEN=1; while(I2C1CONbits.ACKEN); I2Creadonebyte(&value[2]); I2C1CONbits.ACKEN=1; while(I2C1CONbits.ACKEN); I2Creadonebyte(&value[3]); I2C1CONbits.ACKEN=1; while(I2C1CONbits.ACKEN); I2Creadonebyte(&value[4]); I2C1CONbits.ACKEN=1; while(I2C1CONbits.ACKEN); I2Creadonebyte(&value[5]); I2C1CONbits.ACKDT = 1; //set NACK I2C1CONbits.ACKEN=1; while(I2C1CONbits.ACKEN); I2Cstopevent(); }
static BT_HANDLE i2c_probe(const BT_INTEGRATED_DEVICE *pDevice, BT_ERROR *pError) { BT_ERROR Error = BT_ERR_NONE; BT_HANDLE hI2C = NULL; const BT_RESOURCE *pResource = BT_GetIntegratedResource(pDevice, BT_RESOURCE_MEM, 0); if(!pResource) { Error = BT_ERR_NO_MEMORY; goto err_free_out; } hI2C = BT_CreateHandle(&oHandleInterface, sizeof(struct _BT_OPAQUE_HANDLE), pError); if(!hI2C) { goto err_out; } hI2C->pRegs = (LPC11xx_I2C_REGS *) pResource->ulStart; pResource = BT_GetIntegratedResource(pDevice, BT_RESOURCE_ENUM, 0); if(!pResource) { Error = BT_ERR_NO_MEMORY; goto err_free_out; } hI2C->i2c_master.pDevice = pDevice; pResource = BT_GetIntegratedResource(pDevice, BT_RESOURCE_INTEGER, 0); if(!pResource) { Error = BT_ERR_GENERIC; goto err_free_out; } i2cSetPowerState(hI2C, BT_POWER_STATE_AWAKE); // Reset all registers to their defaults! ResetI2C(hI2C); i2cEnable(hI2C); i2c_set_clock_rate(hI2C, pResource->ulStart); pResource = BT_GetIntegratedResource(pDevice, BT_RESOURCE_IRQ, 0); if(!pResource) { Error = BT_ERR_GENERIC; goto err_free_out; } /* On NVIC we don't need to register interrupts, LINKER has patched vector for us * Error = BT_RegisterInterrupt(pResource->ulStart, I2C_irq_handler, hI2C); if(Error) { goto err_free_out; }*/ //@@Error = BT_EnableInterrupt(pResource->ulStart); pResource = BT_GetIntegratedResource(pDevice, BT_RESOURCE_BUSID, 0); if(!pResource) { goto err_free_out; } BT_u32 ulBusID = pResource->ulStart; hI2C->i2c_master.ulBusID = ulBusID; hI2C->i2c_master.hBus = hI2C; // Save pointer to bus's private data. hI2C->i2c_master.pDevice = pDevice; BT_I2C_RegisterBus(&hI2C->i2c_master); if(pError) { *pError = Error; } return hI2C; err_free_out: BT_DestroyHandle(hI2C); err_out: if(pError) { *pError = Error; } return NULL; }
@interrupt void I2C_INT(void) { /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// unsigned char SMBS_SR1 = I2CSR1; unsigned char SMBS_SR2 = I2CSR2; // stat[statnum][0] = SMBS_SR1; // stat[statnum++][1] = SMBS_SR2; if (SMBS_SR2 & BERR) //Check if Bus Error { ResetI2C(); return; } if (SMBS_SR2 & AF) { //EV3-1 STATE = EVT3_1; I2CDR = 0xFF; return; } //EV1: EVF=1, ADSL=1, cleared by reading SR1 register. //EV2: EVF=1, BTF=1, cleared by reading SR1 register followed by reading DR register. //EV3: EVF=1, BTF=1, cleared by reading SR1 register followed by writing DR register. //EV3-1: EVF=1, AF=1, BTF=1; AF is cleared by reading SR1 register. BTF is cleared by releasing the lines //(STOP=1, STOP=0) or by writing DR register (DR=FFh). If lines are released by STOP=1, STOP=0, the On-chip peripherals subsequent EV4 is not seen. //EV4: EVF=1, STOPF=1, cleared by reading SR2 register. if (SMBS_SR1 & EVF == 0) return; if (SMBS_SR1 & ADSL) { I2CAddress = I2CDR; STATE = EVT1; return; } if (SMBS_SR1 & BTF) { //Next, in 7-bit mode read the DR register to determine from the least significant bit (Data //Direction Bit) if the slave must enter Receiver or Transmitter mode. if (I2CAddress & BIT0) { //EV3 STATE = EVT3; //Read OP //I2CDR = EEPROM[I2COffset]; //I2CDR=eeprom[I2COffset]; if (I2COffset == 0) { I2CDR = eeprom_get_status(); } else if (I2COffset == 1) { I2CDR = eeprom_get_cfg(); } else if (I2COffset == 2) { I2CDR = eeprom_get_status_his(); } else { I2CDR = 0xaa; } I2COffset++; } else { //EV2 STATE = EVT2; if (I2COffset == INVALID_ADDR) I2COffset = I2CDR; else { //Write OP //EEPROM[I2COffset] = I2CDR; g_traped_pm_status = (PM_STATUS)I2CDR; Trap; //eeprom[I2COffset] = I2CDR; //I2COffset++; } } } else if (SMBS_SR2 & STOPF) { //EV4 STATE = EVT_NONE; I2COffset = INVALID_ADDR; } else { ResetI2C(); } return; }