u08 ds1631Reset(u08 i2cAddr) { u08 buffer[1]; // return the DS1631 to power-on reset defaults buffer[0] = DS1631_CMD_SWPOR; return i2cMasterSendNI(i2cAddr, 1, buffer); }
bool bma180_WriteReg(uint8_t address, uint8_t value) { reg_write_buf[0] = address; reg_write_buf[1] = value; if (i2cMasterSendNI(BMA180_DeviceID, sizeof(reg_write_buf), reg_write_buf) != I2C_OK) return false; return true; }
u08 spyglassSetLcdContrast(u08 contrast) { u08 data[2]; data[0] = 0; data[1] = contrast; return i2cMasterSendNI(MAX517_I2C_BASE_ADDR, 2, data); }
u08 ds1307_read_register(u08 reg) { device_data[0] = reg; i2cMasterSendNI(DS1307_BASE_ADDRESS,1,&device_data); i2cMasterReceiveNI(DS1307_BASE_ADDRESS,1,&device_data); return device_data[0]; }
// Returns 0 on success, err code on err uint8_t ds2482SendCmd(uint8_t cmd) { #if 1 // 50 msec is too much? int ret = TwMasterTransact(DS2482I2cAddr, &cmd, 1, 0, 0, 50 ); if(ret < 0) { return TwMasterError(); } return 0; //return ret == 0 ? 0 : -1; #else uint8_t data; uint8_t i2cStat; // send command i2cStat = i2cMasterSendNI(DS2482I2cAddr, 1, &cmd); if(i2cStat == I2C_ERROR_NODEV) { printf("No I2C Device\r\n"); return i2cStat; } // check status i2cStat = i2cMasterReceiveNI(DS2482I2cAddr, 1, &data); // rprintf("Cmd=0x%x Status=0x%x\r\n", cmd, data); return (i2cStat == I2C_OK); #endif }
void ds1631StopConvert(u08 i2cAddr) { u08 buffer[1]; // send the DS1631 Stop Convert command buffer[0] = DS1631_CMD_STOPCONV; i2cMasterSendNI(i2cAddr, 1, buffer); }
//Write a byte to a specified register static void gyroWrite8(u08 reg, u08 value) { //Combine reg and value in a 16 byt variable u08 transmission[2] = {reg, value}; u08 i2cstat = i2cMasterSendNI(L3GD20_ADDRESS, 2, (u08*)&transmission); assert(i2cstat == I2C_OK); }
void ds1631SetConfig(u08 i2cAddr, u08 config) { u08 buffer[2]; // write the DS1631 configuration byte buffer[0] = DS1631_CMD_ACCESSCONFIG; buffer[1] = config; i2cMasterSendNI(i2cAddr, 2, buffer); }
u08 ds1631GetConfig(u08 i2cAddr) { u08 buffer[1]; // write the DS1631 configuration byte buffer[0] = DS1631_CMD_ACCESSCONFIG; i2cMasterSendNI(i2cAddr, 2, buffer); i2cMasterReceiveNI(i2cAddr, 2, buffer); return buffer[0]; }
static void cellular_write_register(u08 reg,u08 data) { u08 device_data[2]; device_data[0] = reg; device_data[1] = data; cli(); i2cMasterSendNI(CELLULAR_BASE_ADDRESS,2,device_data); sei(); }
static int8_t cellular_read_register(u08 reg) { u08 device_data[2]; device_data[0] = reg; i2cMasterSendNI(CELLULAR_BASE_ADDRESS,1,device_data); _delay_ms(10); i2cMasterReceiveNI(CELLULAR_BASE_ADDRESS,1,device_data); return device_data[0]; }
void ds1631WriteTempReg(u08 i2cAddr, u08 cmd, s16 value) { u08 buffer[3]; // write the requested register with a temperature value buffer[0] = cmd; buffer[1] = value>>8; buffer[2] = value; i2cMasterSendNI(i2cAddr, 3, buffer); }
static u08 bmpRead8(u08 reg) { u08 i2cstat = i2cMasterSendNI(BMP180_ADDRESS, 1, ®); assert(i2cstat == I2C_OK); u08 outByte; i2cstat = i2cMasterReceiveNI(BMP180_ADDRESS, 1, &outByte); assert(i2cstat == I2C_OK); return outByte; }
static s16 bmpReadS16(u08 reg) { u08 i2cstat = i2cMasterSendNI(BMP180_ADDRESS, 1, ®); assert(i2cstat == I2C_OK); u08 outByte[2]; i2cstat = i2cMasterReceiveNI(BMP180_ADDRESS, 2, (u08*)&outByte); assert(i2cstat == I2C_OK); //Combine the 8-bit values into one 16-bit value return ((outByte[0] << 8) | outByte[1]); }
// Functions u08 ads7828Init(u08 i2cAddr) { u08 channel = 0x80; // setup default A/D voltage reference ads7828SetReference(0); // issue a convserion to test chip presence // return TRUE if chip detected // return FALSE if chip does not respond return (i2cMasterSendNI(i2cAddr, 1, &channel) == I2C_OK); }
s16 ds1631ReadTempReg(u08 i2cAddr, u08 cmd) { u08 buffer[2]; s16 T; // read the temperature value from the requested register i2cMasterSendNI(i2cAddr, 1, &cmd); i2cMasterReceiveNI(i2cAddr, 2, buffer); // pack bytes T = (s16)((buffer[0]<<8) | buffer[1]); // return result return T; }
void i2cDeviceSearch(void) { uint8_t i2cAddr; uint8_t i2cStat; // this function searches all device addresses on the I2C bus // and returns addresses that are live (have a device) rprintf("\r\nSearching for I2c devices on bus\r\n"); for(i2cAddr = 0; i2cAddr<0x80; i2cAddr+=2) { i2cStat = i2cMasterSendNI(i2cAddr, 0, 0); if(i2cStat == I2C_OK) rprintf("Device present at address 0x%x\r\n", i2cAddr); } rprintf("Search complete.\r\n"); }
u08 lis3l02ReadReg(u08 reg) { u08 data; u08 i2cStat; // set register i2cStat = i2cMasterSendNI(LIS3L02_I2C_ADDR, 1, ®); if(i2cStat == I2C_ERROR_NODEV) { rprintf("No I2C Device\r\n"); return i2cStat; } // read register i2cStat = i2cMasterReceiveNI(LIS3L02_I2C_ADDR, 1, &data); //rprintf("READ: Reg=0x%x Data=0x%x\r\n", reg, data); return data; }
// Returns 0 on success, err code on err uint8_t ds2482SendCmdArg(uint8_t cmd, uint8_t arg) { #if 1 uint8_t data[2]; // prepare command data[0] = cmd; data[1] = arg; //DEBUG_PUTS("ds2482SendCmdArg TwMasterTransact.. "); // 50 msec is too much? int ret = TwMasterTransact(DS2482I2cAddr, data, 2, 0, 0, 50 ); //DEBUG_PUTS("ds2482SendCmdArg TwMasterTransact out.. "); if(ret < 0) { return TwMasterError(); } return 0; //return ret == reply_size ? 0 : -1; #else uint8_t data[2]; uint8_t i2cStat; // prepare command data[0] = cmd; data[1] = arg; // send command i2cStat = i2cMasterSendNI(DS2482I2cAddr, 2, data); if(i2cStat == I2C_ERROR_NODEV) { printf("No I2C Device\r\n"); return i2cStat; } // check status i2cStat = i2cMasterReceiveNI(DS2482I2cAddr, 1, data); // rprintf("Cmd=0x%x Arg=0x%x Status=0x%x\r\n", cmd, arg, data[0]); return (i2cStat == I2C_OK); #endif }
u08 lis3l02WriteReg(u08 reg, u08 data) { u08 packet[2]; u08 i2cStat; // prepare packet packet[0] = reg; packet[1] = data; // write register i2cStat = i2cMasterSendNI(LIS3L02_I2C_ADDR, 2, packet); if(i2cStat == I2C_ERROR_NODEV) { rprintf("No I2C Device\r\n"); return i2cStat; } //rprintf("WRITE: Reg=0x%x Data=0x%x\r\n", reg, data); return (i2cStat == I2C_OK); }
//to check the working of ds1307 returns 1 if success else 0 u08 ds1307_check(void) { u08 data=0; return(!i2cMasterSendNI(DS1307_ID, 1, &data)); }
uchar usbFunctionWrite(uchar *data, uchar len) { uchar i; uchar counter; uint8_t temp; if(len > bytesRemaining) // if this is the last incomplete chunk len = bytesRemaining; // limit to the amount we can store bytesRemaining -= len; for(i = 0; i < len; i++) buffer[currentPosition++] = data[i]; if(bytesRemaining == 0) { switch(buffer[0]) { case CMD_READ_EEDATA: RESPONSE(read_eedata)->addr = REQUEST(read_eedata)->addr; RESPONSE(read_eedata)->value = eeprom_read_byte((uint8_t*)(uint16_t)REQUEST(read_eedata)->addr); buffer_len = sizeof(usb_response_read_eedata_t); break; case CMD_WRITE_EEDATA: RESPONSE(write_eedata)->result = 1; eeprom_write_byte((uint8_t*)(uint16_t)REQUEST(write_eedata)->addr, REQUEST(write_eedata)->value); buffer_len = sizeof(usb_response_write_eedata_t); break; case CMD_READ_VERSION: RESPONSE(read_version)->version_major = FIRMWARE_MAJOR; RESPONSE(read_version)->version_minor = FIRMWARE_MINOR; buffer_len = sizeof(usb_response_read_version_t);; break; case CMD_BOARD_TYPE: RESPONSE(board_type)->board_type = BOARD_TYPE_I2C; temp = eeprom_read_byte((uint8_t*)1); if((temp == 0) || (temp == 255)) temp = 9; RESPONSE(board_type)->serial = temp; // read from EEPROM address 1 #ifdef __AVR_ATmega88__ RESPONSE(board_type)->proc_type = PROCESSOR_TYPE_A88; #elif defined __AVR_ATmega168__ RESPONSE(board_type)->proc_type = PROCESSOR_TYPE_A168; #else #warn "UNKNOWN PROC TYPE!" RESPONSE(board_type)->proc_type = PROCESSOR_TYPE_UNKNOWN; #endif RESPONSE(board_type)->mhz = F_CPU/1000000; buffer_len = sizeof(usb_response_board_type_t); break; case CMD_BD_POWER_STATE: buffer_len = 0; break; case CMD_BD_POWER_INFO: buffer_len = 0; break; case CMD_I2C_READ: counter = REQUEST(i2c_read)->read_len; RESPONSE(i2c_read)->result = counter; buffer_len = REQUEST(i2c_read)->read_len + 1; if((i2cMasterSendNI(REQUEST(i2c_read)->device << 1, 1, &(REQUEST(i2c_read)->address)) == I2C_OK) && (i2cMasterReceiveNI(REQUEST(i2c_read)->device << 1, counter, (u08*)&(RESPONSE(i2c_read)->data)) == I2C_OK)) { RESPONSE(i2c_read)->result = 1; } else { RESPONSE(i2c_read)->result = 0; *RESPONSE(i2c_read)->data = I2C_E_NODEV; } break; case CMD_I2C_WRITE: buffer_len = sizeof(usb_response_i2c_write_t); counter = REQUEST(i2c_write)->len - 2; RESPONSE(i2c_write)->result = counter; if(i2cMasterSendNI(REQUEST(i2c_write)->device << 1, counter + 1, &(REQUEST(i2c_write)->address)) == I2C_OK) { RESPONSE(i2c_write)->result = 1; } else { RESPONSE(i2c_write)->result = 0; RESPONSE(i2c_write)->extended_result = I2C_E_NODEV; } break; case CMD_RESET: break; } } return bytesRemaining == 0; // return 1 if we have all data }
void testI2cMemory(void) { u08 i; u08 txdata[66]; u08 rxdata[66]; rprintfProgStrM("\r\nRunning I2C memory test (24xxyy devices)\r\n"); // compose address txdata[0] = 0; txdata[1] = 0; // compose data for(i=0; i<16; i++) txdata[2+i] = localBuffer[i]; // send address and data i2cMasterSendNI(TARGET_ADDR, 18, txdata); rprintfProgStrM("Stored data: "); // null-terminate data txdata[18] = 0; rprintfStr(&txdata[2]); rprintfCRLF(); timerPause(100); // send address i2cMasterSendNI(TARGET_ADDR, 2, txdata); // get data i2cMasterReceiveNI(TARGET_ADDR, 16, rxdata); // null-terminate received string rxdata[16] = 0; rprintfProgStrM("Received data: "); rprintfStr(rxdata); rprintfCRLF(); /* u08 c; u16 addr=0; while(1) { while(!uartReceiveByte(&c)); switch(c) { case '+': addr+=64; break; case '-': addr-=64; break; } c = 0; txdata[0] = (addr>>8); txdata[1] = (addr&0x00FF); i2cMasterSendNI(TARGET_ADDR, 2, txdata); i2cMasterReceiveNI(TARGET_ADDR, 64, rxdata); rprintfProgStrM("Received data at "); rprintfu16(addr); rprintfProgStrM(":\r\n"); debugPrintHexTable(64, rxdata); } */ }
u08 pcf8574Write(u08 nodeAddr, u08 data) { // write data return i2cMasterSendNI(PCF8574_I2C_BASE_ADDR+(nodeAddr<<1), 1, &data); }
void ds1307_write_register(u08 reg,u08 data) { device_data[0] = reg; device_data[1] = data; i2cMasterSendNI(DS1307_BASE_ADDRESS,2,&device_data); }
bool bma180_ReadReg(uint8_t address, uint8_t *value) { if (i2cMasterSendNI(BMA180_DeviceID, 1, &address) != I2C_OK) return false; if (i2cMasterReceiveNI(BMA180_DeviceID, 1, value) != I2C_OK) return false; return true; }
// TO SET THE DATE void DS1307_setdate(unsigned char dd, unsigned char mm, unsigned char yy) { u08 data[4]; data[0]=4;data[1]=dd;data[2]=mm;data[3]=yy; i2cMasterSendNI(DS1307_ID, 4, data); }
//TO SET THE TIME void DS1307_settime(unsigned char hh, unsigned char mm, unsigned char ss) { u08 data[4]; data[0]=0;data[1]=ss;data[2]=mm;data[3]=hh; i2cMasterSendNI(DS1307_ID, 4, data); }
//Initialize all sensors on the IMU board void IMUinit() { //Initialize the I2C bus i2cInit(); //Disable interrupts while everything is being set up CRITICAL_SECTION_START; //Initialize the gyro if(gyroInit(GYRO_RANGE_250DPS)) { theFlags.gyroEnabled = TRUE; //Set the pin as input cbi(DDRE, DDE4); //Disable the pullup on the relevant pin cbi(PORTE, PORTE4); //Attach the DRDY interrupt sbi(EICRB, ISC40); //Writing 1 to ISC40 and ISC41 sets interrupt on rising edge sbi(EICRB, ISC41); sbi(EIMSK, INT4); //Setting INT4 in EIMSK enables the interrupt (if they are globally enabled (but they are cause i2cinit did that)) //Now wait for DRDY to go low and reenmable it to start everything off while(inb(PINE) & _BV(PINE4)) { ; //Wait for the port to go low } gyroEnableDrdy(); } else { #ifdef IMU_DEBUG printf("Error intitializing the gyro!\r\n"); #endif theFlags.gyroEnabled = FALSE; } //Intialize the accelerometer if(accelInit()) { theFlags.accelEnabled = TRUE; //Set the pin as input cbi(DDRD, DDD2); //Disable the pullup on the relevant pin cbi(PORTD, PORTD2); //Attach the DRDY interrupt sbi(EICRA, ISC20); //Writing 1 to ISC20 and ISC21 sets interrupt on rising edge sbi(EICRA, ISC21); sbi(EIMSK, INT2); //Setting INT2 in EIMSK enables the interrupt (if they are globally enabled (but they are cause i2cinit did that)) //Now wait for DRDY to go low and reenable it to start everything off while(inb(PINB) & _BV(PINB2)) { ; //Wait for the port to go low } accelEnableDrdy(); } else { #ifdef IMU_DEBUG printf("Error intitializing the accelerometer!\r\n"); #endif theFlags.accelEnabled = FALSE; } //Initialize the magnetometer if(magInit()) { theFlags.magEnabled = TRUE; //Set the pin as input cbi(DDRD, DDD3); //Disable the pullup on the DRDY pin cbi(PORTD, PORTD3); //Attach the DRDY interrupt sbi(EICRA, ISC30); //Writing 1 to ISC30 and ISC31 sets interrupt on rising edge sbi(EICRA, ISC31); sbi(EIMSK, INT3); //Setting INT3 in EIMSK enables the interrupt (if they are globally enabled (but they are cause i2cinit did that)) } else { #ifdef IMU_DEBUG printf("Error intitializing the magnetometer!\r\n"); #endif theFlags.magEnabled = FALSE; } //Initialize the BMP180 pressure/temp sensor if(bmpInit()) { theFlags.bmpEnabled = TRUE; //Attach the interrupt handler for the timer ticks timerSetInterruptCallback( imuTimerTick ); //Start a temperature measurrement to set everything off const u08 toSend[2] = {BMP180_REGISTER_CONTROL, BMP180_REGISTER_READTEMPCMD}; i2cMasterSendNI(BMP180_ADDRESS, 2, (u08*)&toSend); myBmpState = TEMPERATURE_MEASURING; bmpLastStateChange = millis(); } else { #ifdef IMU_DEBUG printf("Error intitializing the BMP180!\r\n"); #endif theFlags.bmpEnabled = FALSE; } //Attach the custom stop handler after all sensors are initialized i2cSetStopHandler( customStopHandler ); //Re-enable the interrupts CRITICAL_SECTION_END; }