uint8_t i2c_endTransmission(uint8_t stop) { // actually sends the buffer bool xferOK = false; uint8_t errorCode = 0; xferOK = USI_TWI_Start_Read_Write(USI_Buf,USI_BufIdx+1); // core func that does the work USI_BufIdx = 0; if (xferOK) { if (stop) { errorCode = USI_TWI_Master_Stop(); if (errorCode == 0) { errorCode = USI_TWI_Get_State_Info(); return errorCode; } } return 0; } else { // there was an error errorCode = USI_TWI_Get_State_Info(); // this function returns the error number return errorCode; } }
//read value of channel 1 of the light sensor uint16_t read_ch1(){ unsigned char message[2]; message[0] = 0x9E; I2C_write(LIGHT_SENSOR_ADDR, message, 1, FALSE); do{ I2C_read(LIGHT_SENSOR_ADDR, message, 2, TRUE); } while(USI_TWI_Get_State_Info() == USI_TWI_NO_ACK_ON_ADDRESS); return (*((uint16_t*)message)); }
uint8_t USI_TWI::endTransmission(){ // actually sends the buffer bool xferOK = false; uint8_t errorCode = 0; xferOK = USI_TWI_Start_Read_Write(USI_Buf,USI_BufIdx+1); // core func that does the work USI_BufIdx = 0; if (xferOK) return 0; else { // there was an error errorCode = USI_TWI_Get_State_Info(); // this function returns the error number return errorCode; } }
uint8_t i2c_requestFrom(uint8_t slaveAddr, uint8_t numBytes){ // setup for receiving from slave bool xferOK = false; uint8_t errorCode = 0; USI_LastRead = 0; USI_BytesAvail = numBytes; // save this off in a global numBytes++; // add extra byte to transmit header USI_Buf[0] = (slaveAddr<<TWI_ADR_BITS) | USI_RCVE; // setup address & Rcve bit xferOK = USI_TWI_Start_Read_Write(USI_Buf,numBytes); // core func that does the work // USI_Buf now holds the data read if (xferOK) { errorCode = USI_TWI_Master_Stop(); if (errorCode == 0) { errorCode = USI_TWI_Get_State_Info(); return errorCode; } return 0; } else { // there was an error errorCode = USI_TWI_Get_State_Info(); // this function returns the error number return errorCode; } }
static unsigned int display_number(unsigned int n) { unsigned char data[2]; unsigned int digit_1 = 0; unsigned int digit_2 = 0; if(n > 99) n = 99; digit_1 = n % 10; digit_2 = n / 10; if (digit_2 == 8) digit_2 = 9; else if (digit_2 == 9) digit_2 = 8; data[0] = PCF8574_ADDRESS << 1; data[1] = (digit_1 << 4) | (digit_2); USI_TWI_Start_Transceiver_With_Data(data, 2); return USI_TWI_Get_State_Info(); }
int16_t I2C_masterReadRegisterByte(I2C_Device *dev, uint8_t *reg, uint8_t rLen, uint8_t *data, uint8_t dLen) { #ifdef USI_TWI uint8_t cmd_buf[MAX_REQ_BUF]; // For speed reasons, use a static buffer... if (dLen +rLen >= MAX_REQ_BUF) dLen = MAX_REQ_BUF - (rLen + 1); cmd_buf[0] = (((dev->id << 4) & 0xF0) | ((dev->address << 1) & 0x0E)) | (FALSE << TWI_READ_BIT); for (int curReg = 0; curReg < rLen; curReg++) { cmd_buf[1 + curReg] = reg[curReg]; } int retval = USI_TWI_Start_Transceiver_With_Data(cmd_buf, 1 + rLen); if (retval != TRUE) return -1; cmd_buf[0] = (((dev->id << 4) & 0xF0) | ((dev->address << 1) & 0x0E)) | (TRUE << TWI_READ_BIT); do { retval = USI_TWI_Start_Transceiver_With_Data(cmd_buf, 1 + dLen); } while(USI_TWI_Get_State_Info() == USI_TWI_NO_ACK_ON_ADDRESS); for (uint8_t bufIdx = 0; bufIdx < dLen; bufIdx++) data[bufIdx] = cmd_buf[bufIdx + 1]; return (retval == TRUE) ? dLen : -1; #else uint8_t twiStatus; uint8_t retryCount = 0; int16_t retval = dLen; while (retryCount < MAX_RETRIES) { // Start command twiStatus = I2C_internal_sendCommand(I2C_StartCommand); if (twiStatus == TW_MT_ARB_LOST) { // We need to retry... retryCount++; continue; // Begin to loop again } else if ((twiStatus != TW_START) && (twiStatus != TW_REP_START)) { retval = -1; break; } // Now send slave address... asking for a write TWDR = ((dev->id << 4) & 0xF0) | ((dev->address << 1) & 0x0E) | TW_WRITE; twiStatus = I2C_internal_sendCommand(I2C_DataNACKCommand); if ((twiStatus == TW_MT_SLA_NACK) || (twiStatus == TW_MT_ARB_LOST)) { retryCount++; continue; } else if (twiStatus != TW_MT_SLA_ACK) { retval = -1; break; } // Send register address for (int curReg = 0; curReg < rLen; curReg++) { TWDR = reg[curReg]; twiStatus = I2C_internal_sendCommand(I2C_DataNACKCommand); if (twiStatus != TW_MT_DATA_ACK) { retval = -1; break; } } if (retval == -1) break; // Something bad happened sending the register address // Send start command again. twiStatus = I2C_internal_sendCommand(I2C_StartCommand); if (twiStatus == TW_MT_ARB_LOST) { // We need to retry... retryCount++; continue; // Begin to loop again } else if ((twiStatus != TW_START) && (twiStatus != TW_REP_START)) { retval = -1; break; } // Now send slave address... asking for a read TWDR = ((dev->id << 4) & 0xF0) | ((dev->address << 1) & 0x0E) | TW_READ; twiStatus = I2C_internal_sendCommand(I2C_DataNACKCommand); if ((twiStatus == TW_MR_SLA_NACK) || (twiStatus == TW_MR_ARB_LOST)) { retryCount++; continue; } else if (twiStatus != TW_MR_SLA_ACK) { retval = -1; break; } for (int curData = 0; curData < dLen; curData++) { // Read the data if (curData == (dLen - 1)) { // Last read... we need to send a NACK twiStatus = I2C_internal_sendCommand(I2C_DataNACKCommand); if (twiStatus != TW_MR_DATA_NACK) { retval = -1; break; } } else { // We have to perform other reads... send an ACK twiStatus = I2C_internal_sendCommand(I2C_DataACKCommand); if (twiStatus != TW_MR_DATA_ACK) { retval = -1; break; } } // Read the data! data[curData] = TWDR; } break; } twiStatus = I2C_internal_sendCommand(I2C_StopCommand); return retval; #endif }