Example #1
0
/*
 * 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;
}
Example #3
0
/*
 * 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;
}
Example #4
0
/** 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;
}
Example #5
0
/** Write I2C bus.
 * @param data data to send
 * @param sendStop if STOP required
 * @return Result of reading, or negative error code (-7 error in first writing, -9 error in final writing, -8 no ACK for first writing, -10 no ACK for final writing)
 */
uint8 WriteI2C(uint8 data, bool_t sendStop) {

#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, "WriteI2C\r\n");
#endif

	bool_t check_status;
	if (!I2Cdev_enabled) {
		i2cdev_debug_depth--;
		return -15;
	}

	vAHI_SiMasterWriteData8(data);
	bAHI_SiMasterSetCmdReg(E_AHI_SI_NO_START_BIT, sendStop,
			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 -11 - 2*(sendStop == SEND_STOP); // Return -7, or -9 if final reading
	}

	// Check acknowledge
	if ((check_status = bAHI_SiMasterCheckRxNack())) {

#ifdef I2CDEV_SERIAL_DEBUG
		i2cdev_debug_depth--;
#endif

		return -12 - 2*(sendStop == SEND_STOP); // Return -8, or -10 if final reading
	}


#ifdef I2CDEV_SERIAL_DEBUG
	i2cdev_debug_depth--;
#endif

	return 0;
}
Example #6
0
/** Write device register to read/write.
 * @param devAddr I2C slave device address
 * @return Status of operation (0: OK, -3 error in transmission, -5 no ACK received)
 */
int8 WriteRegisterI2C(uint8 regAddr) {

#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, "WriteRegisterI2C\r\n");
#endif

	bool_t check_status;
	if (!I2Cdev_enabled)
		StartI2C();

	vAHI_SiMasterWriteData8(regAddr);
	bAHI_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);

	check_status = IdleI2C();

	if (check_status) {

#ifdef I2CDEV_SERIAL_DEBUG
		i2cdev_debug_depth--;
#endif

		return -3;
	}

	//check acknowledge receive
	if ((check_status = bAHI_SiMasterCheckRxNack())) {

#ifdef I2CDEV_SERIAL_DEBUG
		i2cdev_debug_depth--;
#endif

		return -4;
	}


#ifdef I2CDEV_SERIAL_DEBUG
	i2cdev_debug_depth--;
#endif

	return 0;
}