/* * write a buff to I2C * - i2c_device: i2c device pointer * - dev_addr: device address * - data: data buff * - len: data lenght */ uint8 suli_i2c_write(void * i2c_device, uint8 dev_addr, uint8 *data, uint8 len) { vAHI_SiMasterWriteSlaveAddr(dev_addr, FALSE); // bSetSTA, bSetSTO, bSetRD, bSetWR, bSetAckCtrl, bSetIACK); bAHI_SiMasterSetCmdReg(TRUE, FALSE, FALSE, TRUE, E_AHI_SI_SEND_ACK, E_AHI_SI_NO_IRQ_ACK); while(bAHI_SiMasterPollTransferInProgress()); //Waitforanindicationofsuccess int i; uint8 *old = data; for(i = 0; i < len; i++) { vAHI_SiMasterWriteData8(*data++); if(i == (len - 1)) //should send stop { bAHI_SiMasterSetCmdReg(FALSE, TRUE, FALSE, TRUE, E_AHI_SI_SEND_ACK, E_AHI_SI_NO_IRQ_ACK); } else { bAHI_SiMasterSetCmdReg(FALSE, FALSE, FALSE, TRUE, E_AHI_SI_SEND_ACK, E_AHI_SI_NO_IRQ_ACK); } while(bAHI_SiMasterPollTransferInProgress()); //Waitforanindicationofsuccess if(bAHI_SiMasterCheckRxNack()) { bAHI_SiMasterSetCmdReg(FALSE, TRUE, FALSE, FALSE, E_AHI_SI_SEND_ACK, E_AHI_SI_NO_IRQ_ACK); break; } } return data - old; }
// // Originally, 'endTransmission' was an f(void) function. // It has been modified to take one parameter indicating // whether or not a STOP should be performed on the bus. // Calling endTransmission(false) allows a sketch to // perform a repeated start. // // WARNING: Nothing in the library keeps track of whether // the bus tenure has been properly ended with a STOP. It // is very possible to leave the bus in a hung state if // no call to endTransmission(true) is made. Some I2C // devices will behave oddly if they do not see a STOP. // uint8_t TwoWire::endTransmission(uint8_t sendStop) { uint8_t error = 0; // transmit buffer (blocking) DBG_PRINTF("vAHI_SiMasterWriteSlaveAddr %x\r\n", txAddress); vAHI_SiMasterWriteSlaveAddr(txAddress, false); DBG_PRINTF("bAHI_SiMasterSetCmdReg\r\n"); bool ret = bAHI_SiMasterSetCmdReg(E_AHI_SI_START_BIT, E_AHI_SI_NO_STOP_BIT, E_AHI_SI_NO_SLAVE_READ, E_AHI_SI_SLAVE_WRITE, E_AHI_SI_SEND_ACK, E_AHI_SI_NO_IRQ_ACK); DBG_PRINTF("bAHI_SiPollTransferInProgress\r\n"); while(bAHI_SiPollTransferInProgress()); /* wait while busy */ /* check to see if we get an ACK back*/ DBG_PRINTF("bAHI_SiPollRxNack\r\n"); if(bAHI_SiPollRxNack()) error = 2; // error, got NACK on address transmit if (error == 0) { uint16_t sent = 0; while (sent < txBufferLength) { DBG_PRINTF("sent %d\n", txBuffer[sent]); vAHI_SiMasterWriteData8(txBuffer[sent++]); if(sent == (txBufferLength) ) { vAHI_SiSetCmdReg(E_AHI_SI_NO_START_BIT, E_AHI_SI_STOP_BIT, E_AHI_SI_NO_SLAVE_READ, E_AHI_SI_SLAVE_WRITE, E_AHI_SI_SEND_ACK, E_AHI_SI_NO_IRQ_ACK); } else { vAHI_SiSetCmdReg(E_AHI_SI_NO_START_BIT, E_AHI_SI_NO_STOP_BIT, E_AHI_SI_NO_SLAVE_READ, E_AHI_SI_SLAVE_WRITE, E_AHI_SI_SEND_ACK, E_AHI_SI_NO_IRQ_ACK); } while(bAHI_SiPollTransferInProgress()); /* wait while busy */ /* check to see if we get an ACK back*/ if(bAHI_SiPollRxNack()) { error = 3; // error, got NACK during data transmmit vAHI_SiSetCmdReg(E_AHI_SI_NO_START_BIT, E_AHI_SI_STOP_BIT, E_AHI_SI_NO_SLAVE_READ, E_AHI_SI_NO_SLAVE_WRITE, E_AHI_SI_SEND_ACK, E_AHI_SI_NO_IRQ_ACK); } DBG_PRINTF("sent done %d\n", sent); } } //if (error == 0) { // TWI_Stop(twi); // if (!TWI_WaitTransferComplete(twi, XMIT_TIMEOUT)) // error = 4; // error, finishing up //} txBufferLength = 0; // empty buffer status = MASTER_IDLE; return error; }
bool_t bSMBusSequentialRead_NACK(uint8 u8Address, uint8 u8Length, uint8* pu8Data) { /* Send address with write bit set */ vAHI_SiMasterWriteSlaveAddr(u8Address, !E_AHI_SI_SLAVE_RW_SET); vAHI_SiMasterSetCmdReg(E_AHI_SI_START_BIT, E_AHI_SI_NO_STOP_BIT, E_AHI_SI_NO_SLAVE_READ, E_AHI_SI_SLAVE_WRITE, E_AHI_SI_SEND_ACK, E_AHI_SI_NO_IRQ_ACK); if(!bSMBusWait()) return(FALSE); while(u8Length > 0){ u8Length--; /* * If its the last byte to be sent, send with * stop sequence set */ if(u8Length == 0){ vAHI_SiMasterSetCmdReg(E_AHI_SI_NO_START_BIT, E_AHI_SI_STOP_BIT, E_AHI_SI_SLAVE_READ, E_AHI_SI_NO_SLAVE_WRITE, E_AHI_SI_SEND_NACK, E_AHI_SI_NO_IRQ_ACK); } else { vAHI_SiMasterSetCmdReg(E_AHI_SI_NO_START_BIT, E_AHI_SI_NO_STOP_BIT, E_AHI_SI_SLAVE_READ, E_AHI_SI_NO_SLAVE_WRITE, E_AHI_SI_SEND_NACK, E_AHI_SI_NO_IRQ_ACK); } while(bAHI_SiMasterPollTransferInProgress()); /* busy wait */ *pu8Data++ = u8AHI_SiMasterReadData8(); } return(TRUE); }
/** Write device address on the bus. * @param devAddr I2C slave device address * @param readCommand if last bit of addr (8th) is 1 or 0 (read or write) * @return Status of operation (0: OK, -1 error in Write, -5 error in Read, -2 no ACK for Write, -6 no ACK for Read) */ int8 WriteAddressI2C(uint8 devAddr, bool_t readCommand) { #ifdef I2CDEV_SERIAL_DEBUG i2cdev_debug_depth++; int z; for(z=0;z<i2cdev_debug_depth;z++) DBG_vPrintf(TRACE_APP, "\t"); DBG_vPrintf(TRACE_APP, "WriteAddressI2C\r\n"); #endif bool_t check_status; if (!I2Cdev_enabled) StartI2C(); //IdleI2C(); vAHI_SiMasterWriteSlaveAddr(devAddr, readCommand); bAHI_SiMasterSetCmdReg(E_AHI_SI_START_BIT, E_AHI_SI_NO_STOP_BIT, E_AHI_SI_NO_SLAVE_READ, E_AHI_SI_SLAVE_WRITE, E_AHI_SI_SEND_ACK, E_AHI_SI_NO_IRQ_ACK); check_status = IdleI2C(); if (check_status) { #ifdef I2CDEV_SERIAL_DEBUG i2cdev_debug_depth--; #endif return -1 - 4*(readCommand == COMMAND_READ); // -1 if WRITE command, -5 if READ command } //check acknowledge receive if ((check_status = bAHI_SiMasterCheckRxNack())) { #ifdef I2CDEV_SERIAL_DEBUG i2cdev_debug_depth--; #endif return -2 - 4*(readCommand == COMMAND_READ); // -2 if WRITE command, -6 if READ command } #ifdef I2CDEV_SERIAL_DEBUG i2cdev_debug_depth--; #endif return 0; }
uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity, uint8_t sendStop) { if (quantity > BUFFER_LENGTH) quantity = BUFFER_LENGTH; DBG_PRINTF("vAHI_SiMasterWriteSlaveAddr %x\r\n", address); vAHI_SiMasterWriteSlaveAddr(address, true); vAHI_SiSetCmdReg(E_AHI_SI_START_BIT, E_AHI_SI_NO_STOP_BIT, E_AHI_SI_NO_SLAVE_READ, E_AHI_SI_SLAVE_WRITE, E_AHI_SI_SEND_ACK, E_AHI_SI_NO_IRQ_ACK); while(bAHI_SiPollTransferInProgress()); /* wait while busy */ // perform blocking read into buffer int readed = 0; do { if(readed == (quantity-1) ) { vAHI_SiSetCmdReg(E_AHI_SI_NO_START_BIT, E_AHI_SI_STOP_BIT, E_AHI_SI_SLAVE_READ, E_AHI_SI_NO_SLAVE_WRITE, E_AHI_SI_SEND_ACK, E_AHI_SI_NO_IRQ_ACK); } else { vAHI_SiSetCmdReg(E_AHI_SI_NO_START_BIT, E_AHI_SI_NO_STOP_BIT, E_AHI_SI_SLAVE_READ, E_AHI_SI_NO_SLAVE_WRITE, E_AHI_SI_SEND_ACK, E_AHI_SI_NO_IRQ_ACK); } while(bAHI_SiPollTransferInProgress()); /* wait while busy */ rxBuffer[readed++] = u8AHI_SiMasterReadData8(); DBG_PRINTF("u8AHI_SiMasterReadData8 %x\r\n", rxBuffer[readed-1]); } while (readed < quantity); // set rx buffer iterator vars rxBufferIndex = 0; rxBufferLength = readed; return readed; }
/* * read a buff to I2C * - i2c_device: i2c device pointer * - dev_addr: device address * - data: data buff * - len: data lenght * return */ uint8 suli_i2c_read(void *i2c_device, uint8 dev_addr, uint8 *buff, uint8 len) { vAHI_SiMasterWriteSlaveAddr(dev_addr, TRUE); // bSetSTA, bSetSTO, bSetRD, bSetWR, bSetAckCtrl, bSetIACK); bAHI_SiMasterSetCmdReg(TRUE, FALSE, FALSE, TRUE, E_AHI_SI_SEND_ACK, E_AHI_SI_NO_IRQ_ACK); while(bAHI_SiMasterPollTransferInProgress()); //Waitforanindicationofsuccess int i; uint8 *old = buff; for(i = 0; i < len; i++) { if(i == (len - 1)) //should send stop, nack { bAHI_SiMasterSetCmdReg(FALSE, TRUE, TRUE, FALSE, E_AHI_SI_SEND_NACK, E_AHI_SI_NO_IRQ_ACK); } else { bAHI_SiMasterSetCmdReg(FALSE, FALSE, TRUE, FALSE, E_AHI_SI_SEND_ACK, E_AHI_SI_NO_IRQ_ACK); } while(bAHI_SiMasterPollTransferInProgress()); //Waitforanindicationofsuccess *buff++ = u8AHI_SiMasterReadData8(); } return buff - old; }
PUBLIC bool_t bSMBusWrite(uint8 u8Address, uint8 u8Command, uint8 u8Length, uint8* pu8Data) { bool_t bCommandSent = FALSE; /* Send address with write bit set */ vAHI_SiMasterWriteSlaveAddr(u8Address, E_AHI_SI_SLAVE_RW_SET); vAHI_SiMasterSetCmdReg(E_AHI_SI_START_BIT, E_AHI_SI_NO_STOP_BIT, E_AHI_SI_NO_SLAVE_READ, E_AHI_SI_SLAVE_WRITE, E_AHI_SI_SEND_ACK, E_AHI_SI_NO_IRQ_ACK); if(!bSMBusWait()) return(FALSE); while(bCommandSent == FALSE || u8Length > 0){ if(!bCommandSent){ /* Send command byte */ vAHI_SiMasterWriteData8(u8Command); bCommandSent = TRUE; } else { u8Length--; /* Send data byte */ vAHI_SiMasterWriteData8(*pu8Data++); } /* * If its the last byte to be sent, send with * stop sequence set */ if(u8Length == 0){ vAHI_SiMasterSetCmdReg(E_AHI_SI_NO_START_BIT, E_AHI_SI_STOP_BIT, E_AHI_SI_NO_SLAVE_READ, E_AHI_SI_SLAVE_WRITE, E_AHI_SI_SEND_ACK, E_AHI_SI_NO_IRQ_ACK); } else { vAHI_SiMasterSetCmdReg(E_AHI_SI_NO_START_BIT, E_AHI_SI_NO_STOP_BIT, E_AHI_SI_NO_SLAVE_READ, E_AHI_SI_SLAVE_WRITE, E_AHI_SI_SEND_ACK, E_AHI_SI_NO_IRQ_ACK); } if(!bSMBusWait()) return(FALSE); } return(TRUE); }