// 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 }
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]; }
uint8_t ds2482ReadConfig(void) { uint8_t status; // set read pointer to config register ds2482SendCmdArg(DS2482_CMD_SRP, DS2482_READPTR_CR); i2cMasterReceiveNI(DS2482I2cAddr, 1, &status); return status; }
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]; }
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 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]); }
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; }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // reads the specified page of data from the eeprom chip EE_STATUS ee_readBytes(uint16_t page, uint16_t length, uint8_t * data) { EE_STATUS status = __writeActiveAddress(page); if (I2C_OK != status) return status; // perform block read status = i2cMasterReceiveNI(AT24C1024_ADDRESS, length, data); if (I2C_OK != status) return status; return I2C_OK; }
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; }
uint8_t ds2482BusyWait(uint8_t *status_p) { uint8_t ret; // set read pointer to status register //DEBUG_PUTS("ds2482BusyWait cmd.. "); ret = ds2482SendCmdArg(DS2482_CMD_SRP, DS2482_READPTR_SR); //DEBUG_PUTS("ds2482BusyWait cmd done.. "); if( ret ) return ret; // check status until busy bit is cleared //DEBUG_PUTS("i2cMasterReceiveNI.. "); do { i2cMasterReceiveNI(DS2482I2cAddr, 1, status_p); //printf("ds2482BusyWait status = 0x%02X ", *status_p); } while(*status_p & DS2482_STATUS_1WB); // return the status register value return 0; }
// 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 }
/** * Returns 0 on success, err code on error. */ uint8_t ds2482BusReadByte(uint8_t *out) { // wait for DS2482 to be ready uint8_t status; uint8_t ret = ds2482BusyWait(&status); if( ret ) return ret; // send 1WRB command ret = ds2482SendCmd(DS2482_CMD_1WRB); if( ret ) return ret; // wait for read to finish ret = ds2482BusyWait(&status); // TODO CHECK 4 ERR! (nonzero return) if( ret ) return ret; // set read pointer to data register ret = ds2482SendCmdArg(DS2482_CMD_SRP, DS2482_READPTR_RDR); if( ret ) return ret; // read data ret = i2cMasterReceiveNI(DS2482I2cAddr, 1, out); if( ret ) return ret; return 0; }
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); } */ }
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 }
u08 pcf8574Read(u08 nodeAddr) { u08 data; i2cMasterReceiveNI(PCF8574_I2C_BASE_ADDR+(nodeAddr<<1), 1, &data); return 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; }
/*! \brief Read the current time/date from the realtime clock. Stores the data and can be retrieved with the get functions in this file */ void ds1307_read(void) { if (allowed_to_read) i2cMasterReceiveNI(DS1307_ADDR,7,(unsigned char *)time_data); }