Пример #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;
}
Пример #2
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;
}
Пример #3
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;
}
Пример #4
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;
}
Пример #5
0
PRIVATE bool_t bSMBusWait(void)
{

	while(bAHI_SiMasterPollTransferInProgress()); /* busy wait */

	if (bAHI_SiMasterPollArbitrationLost() | bAHI_SiMasterCheckRxNack())	{

		/* release bus & abort */
		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);
		return(FALSE);
	}

	return(TRUE);

}