I2C Async Combinable

Technical questions regarding the XTC tools and programming with XMOS.
Post Reply
RitchRock
XCore Addict
Posts: 186
Joined: Tue Jan 17, 2017 9:25 pm

I2C Async Combinable

Post by RitchRock »

I'm implementing Async Combinable, and things seem to be playing nicely. For others in need of a starting point in the future, here is a helper function for any task that needs an 8-bit register read.

Input is appreciated - I'm curious to know if know if I did something wrong, like for example calling i2c.operation_complete():, even-though my stopbit is zero.

Code: Select all


static char i2cReady =1;

uint8_t i2c_read_reg_async(client i2c_master_async_if i2c , uint8_t device_addr, uint8_t reg, i2c_regop_res_t &result)
{
    uint8_t a_reg[1] = {reg};
    uint8_t data[1] = {0};
    size_t n =1;
    i2c.write(device_addr, a_reg, 1, 0);
    i2cReady = 0;
    while(i2cReady == 0)
        {
            select{
                case i2c.operation_complete():
                    i2c_res_t result;
                    unsigned num_bytes_sent;
                    result = i2c.get_write_result(num_bytes_sent);
                    if (num_bytes_sent != 1)
                    {
                        printf("\nError writing register to read from on I2C bus. Result = %d\n", result);
                        result = I2C_REGOP_DEVICE_NACK;
                        i2c.send_stop_bit();
                        i2cReady = 1;
                        return 0;
                    }
                    i2cReady = 1;
                    break;
            }
        }
    i2c.read(device_addr, n, 1);
    i2cReady = 0;

    while(i2cReady == 0)
    {
        select{
            case i2c.operation_complete():
                i2c_res_t result;
                unsigned num_bytes_sent;
                result = i2c.get_read_data(data, n);
                if (result == I2C_ACK) {
                  result = I2C_REGOP_SUCCESS;
                } else {
                  result = I2C_REGOP_DEVICE_NACK;
                  printf("\nError reading on I2C bus. Result = %d\n", result);
                }
                i2cReady = 1;
                break;
        }
    }
    return data[0];
}


RitchRock
XCore Addict
Posts: 186
Joined: Tue Jan 17, 2017 9:25 pm

Post by RitchRock »

I should say, it's programming other ICs correctly, but when I try to combine it with another combinable function, the program compiles fine, but appears to lock up on execution (i.e. nothing happens).

The other combinable function is a simple user interface and is just waiting for a port value to change from a button press. I've successfully combined it with the regular I2C master (non async).
Post Reply