This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

C advice with app_twi peripheral configuration

I've been trying to wrap my head around the app_twi library by looking at the examples, and I'm slowly getting there. However, for my application I need to send multiple default configuration commands to my accelerometer.

So I was wondering, what is the neatest way to implement this? Would I simply make another default_config[] type array and put an additional write into the "lm75b_init_transfers" array? Or is there a neater way to include all of the config data in one default_config[] array? My C is a little rusty, but I feel like a struct might be what I need but I'm not sure how I'd implement it in this case.

The configuration code as it is in the example is show below.

uint8_t const lm75b_conf_reg_addr  = LM75B_REG_CONF;
uint8_t const lm75b_temp_reg_addr  = LM75B_REG_TEMP;
uint8_t const lm75b_tos_reg_addr   = LM75B_REG_TOS;
uint8_t const lm75b_thyst_reg_addr = LM75B_REG_THYST;


// Set default configuration of LM75B - write 0 to Conf register.
static uint8_t const default_config[] = { LM75B_REG_CONF, 0 }; 
///// I need two config commands 

app_twi_transfer_t const lm75b_init_transfers[LM75B_INIT_TRANSFER_COUNT] =
{
    APP_TWI_WRITE(LM75B_ADDR, default_config, sizeof(default_config), 0)
    /////, Another APP_TWI_WRITE for additional config command here?
};
Parents
  • I was sort of looking for a way to expand the configuration array neatly, I think I found a way that works okay by using a 3d array:

    void LSM303_config(app_twi_t *m_app_twi)
    {
    	// Default configuration array
    	uint8_t default_config[][2] = {
    		{LSM303_ACC_CTRL_REG1_A, ODR_10Hz | Acc_X_EN | Acc_Y_EN | Acc_Z_EN},
    		{LSM303_ACC_CTRL_REG2_A, 0x00}
    	};
    
    	// Queue transfers
    	app_twi_transfer_t const lsm303_cfg_transfers[2] = {
    		APP_TWI_WRITE(LSM303_ADDRESS_ACCEL, default_config[0], 2, 0),
    		APP_TWI_WRITE(LSM303_ADDRESS_ACCEL, default_config[1], 2, 0)
    	};
    	
    	APP_ERROR_CHECK( app_twi_perform(m_app_twi, lsm303_cfg_transfers, 2, NULL) );
    }
    
Reply
  • I was sort of looking for a way to expand the configuration array neatly, I think I found a way that works okay by using a 3d array:

    void LSM303_config(app_twi_t *m_app_twi)
    {
    	// Default configuration array
    	uint8_t default_config[][2] = {
    		{LSM303_ACC_CTRL_REG1_A, ODR_10Hz | Acc_X_EN | Acc_Y_EN | Acc_Z_EN},
    		{LSM303_ACC_CTRL_REG2_A, 0x00}
    	};
    
    	// Queue transfers
    	app_twi_transfer_t const lsm303_cfg_transfers[2] = {
    		APP_TWI_WRITE(LSM303_ADDRESS_ACCEL, default_config[0], 2, 0),
    		APP_TWI_WRITE(LSM303_ADDRESS_ACCEL, default_config[1], 2, 0)
    	};
    	
    	APP_ERROR_CHECK( app_twi_perform(m_app_twi, lsm303_cfg_transfers, 2, NULL) );
    }
    
Children
No Data
Related