Exemplo n.º 1
0
//
// This is a low priority task that will continuously access one I2C device
// Frequently it will release the I2C device so that others can also use I2C
//
static void Task1(void *pvParameters)
{
    int i = 0;


    if (PIOS_I2C_LockDevice(MAX_LOCK_WAIT / portTICK_RATE_MS))
    {
        if (PIOS_I2C_Transfer(I2C_Write, DEVICE_1_ADDRESS<<1, (uint8_t*)"\x20\xB0\xB1\xB2", 4) != 0)
            OnError();
        PIOS_I2C_UnlockDevice();
    }
    else
    {
        OnError();
    }

    for(;;)
    {
        i++;
        if (i==100)
        {
            PIOS_LED_Toggle(LED1);
            i = 0;
        }

        DebugPinHigh(DEBUG_PIN_TASK1_WAIT);
        if (PIOS_I2C_LockDevice(MAX_LOCK_WAIT / portTICK_RATE_MS))
        {
            uint8_t buf[20];

            DebugPinLow(DEBUG_PIN_TASK1_WAIT);
            DebugPinHigh(DEBUG_PIN_TASK1_LOCKED);

            // Write A0 A1 A2  at address 0x10
            DebugPinHigh(DEBUG_PIN_TRANSFER);
            if (PIOS_I2C_Transfer(I2C_Write, DEVICE_1_ADDRESS<<1, (uint8_t*)"\x10\xA0\xA1\xA2", 4) != 0)
                OnError();
            DebugPinLow(DEBUG_PIN_TRANSFER);


            // Read 3 bytes at address 0x20 and check
            DebugPinHigh(DEBUG_PIN_TRANSFER);
            if (PIOS_I2C_Transfer(I2C_Write_WithoutStop, DEVICE_1_ADDRESS<<1, (uint8_t*)"\x20", 1) != 0)
                OnError();
            DebugPinLow(DEBUG_PIN_TRANSFER);

            DebugPinHigh(DEBUG_PIN_TRANSFER);
            if (PIOS_I2C_Transfer(I2C_Read, DEVICE_1_ADDRESS<<1, buf, 3) != 0)
                OnError();
            DebugPinLow(DEBUG_PIN_TRANSFER);

            if (memcmp(buf, "\xB0\xB1\xB2",3) != 0)
                OnError();

            // Read 3 bytes at address 0x10 and check
            DebugPinHigh(DEBUG_PIN_TRANSFER);
            if (PIOS_I2C_Transfer(I2C_Write_WithoutStop, DEVICE_1_ADDRESS<<1, (uint8_t*)"\x10", 1) != 0)
                OnError();
            DebugPinLow(DEBUG_PIN_TRANSFER);

            DebugPinHigh(DEBUG_PIN_TRANSFER);
            if (PIOS_I2C_Transfer(I2C_Read, DEVICE_1_ADDRESS<<1, buf, 3) != 0)
                OnError();
            DebugPinLow(DEBUG_PIN_TRANSFER);

            if (memcmp(buf, "\xA0\xA1\xA2",3) != 0)
                OnError();

            DebugPinLow(DEBUG_PIN_TASK1_LOCKED);
            PIOS_I2C_UnlockDevice();
        }
        else
        {
            OnError();
        }
    }
}
Exemplo n.º 2
0
// This is a high priority task that will periodically perform some actions on the second I2C device
// Most of the time it will have to wait for the other task task to release I2C
static void Task2(void *pvParameters)
{
    portTickType xLastExecutionTime;

    xLastExecutionTime = xTaskGetTickCount();
    uint32_t count = 0;

    for(;;)
    {
        uint8_t buf[20];

        DebugPinHigh(DEBUG_PIN_TASK2_WAIT);
        if (PIOS_I2C_LockDevice(MAX_LOCK_WAIT / portTICK_RATE_MS))
        {
            DebugPinLow(DEBUG_PIN_TASK2_WAIT);
            DebugPinHigh(DEBUG_PIN_TASK2_LOCKED);

            // Write value of count to address 0x10
            buf[0] = 0x10;				// The address
            memcpy(&buf[1], &count, 4);	// The data to write
            DebugPinHigh(DEBUG_PIN_TRANSFER);
            if (PIOS_I2C_Transfer(I2C_Write, DEVICE_2_ADDRESS<<1, buf, 5) != 0)
                OnError();
            DebugPinLow(DEBUG_PIN_TRANSFER);

            DebugPinLow(DEBUG_PIN_TASK2_LOCKED);
            PIOS_I2C_UnlockDevice();
        }
        else
        {
            OnError();
        }

        vTaskDelay(2 / portTICK_RATE_MS);

        DebugPinHigh(DEBUG_PIN_TASK2_WAIT);
        if (PIOS_I2C_LockDevice(1 / portTICK_RATE_MS))
        {
            DebugPinLow(DEBUG_PIN_TASK2_WAIT);
            DebugPinHigh(DEBUG_PIN_TASK2_LOCKED);

            // Read at address 0x10 and check
            DebugPinHigh(DEBUG_PIN_TRANSFER);
            if (PIOS_I2C_Transfer(I2C_Write_WithoutStop, DEVICE_2_ADDRESS<<1, (uint8_t*)"\x10", 1) != 0)
                OnError();
            DebugPinLow(DEBUG_PIN_TRANSFER);

            DebugPinHigh(DEBUG_PIN_TRANSFER);
            if (PIOS_I2C_Transfer(I2C_Read, DEVICE_2_ADDRESS<<1, buf, 4) != 0)
                OnError();
            DebugPinLow(DEBUG_PIN_TRANSFER);

            DebugPinHigh(DEBUG_PIN_TRANSFER);
            if (memcmp(buf, &count, 4) != 0)
                OnError();
            DebugPinLow(DEBUG_PIN_TRANSFER);


            DebugPinLow(DEBUG_PIN_TASK2_LOCKED);
            PIOS_I2C_UnlockDevice();
        }
        else
        {
            OnError();
        }

        vTaskDelay(5 / portTICK_RATE_MS);

        count++;
    }
}
Exemplo n.º 3
0
			.info = __func__,
			.addr = MPU9150_MAG_ADDR,
			.rw = PIOS_I2C_TXN_WRITE,
			.len = sizeof(mag_addr_buffer),
			.buf = mag_addr_buffer,
		},
		{
			.info = __func__,
			.addr = MPU9150_MAG_ADDR,
			.rw = PIOS_I2C_TXN_READ,
			.len = sizeof(mag_read_buffer),
			.buf = mag_read_buffer,
		}
	};

	int32_t retval = PIOS_I2C_Transfer(i2c_id, mag_txn_list, NELEMENTS(mag_txn_list));
	if (retval < 0)
		return -1;

	return 0;
}

/**
 * @brief Reads one or more bytes into a buffer
 * \param[in] address MPU9150 register address (depends on size)
 * \param[out] buffer destination buffer
 * \param[in] len number of bytes which should be read
 * \return 0 if operation was successful
 * \return -1 if error during I2C transfer
 * \return -2 if unable to claim i2c device
 */