/* Pedometer events are always mapped to this pin. */ #define MMA9553_DEFAULT_GPIO_PIN mma9551_gpio6 #define MMA9553_DEFAULT_GPIO_POLARITY 0
/* Bitnum used for GPIO configuration = bit number in high status byte */ #define MMA9553_STATUS_TO_BITNUM(bit) (ffs(bit) - 9) #define MMA9553_MAX_BITNUM MMA9553_STATUS_TO_BITNUM(BIT(16))
#define MMA9553_DEFAULT_SAMPLE_RATE 30 /* Hz */
/* * The internal activity level must be stable for ACTTHD samples before * ACTIVITY is updated. The ACTIVITY variable contains the current activity * level and is updated every time a step is detected or once a second * if there are no steps.
*/ #define MMA9553_ACTIVITY_THD_TO_SEC(thd) ((thd) / MMA9553_DEFAULT_SAMPLE_RATE) #define MMA9553_ACTIVITY_SEC_TO_THD(sec) ((sec) * MMA9553_DEFAULT_SAMPLE_RATE)
/* * Autonomously suspend pedometer if acceleration vector magnitude * is near 1g (4096 at 0.244 mg/LSB resolution) for 30 seconds.
*/ #define MMA9553_DEFAULT_SLEEPMIN 3688 /* 0,9 g */ #define MMA9553_DEFAULT_SLEEPMAX 4508 /* 1,1 g */ #define MMA9553_DEFAULT_SLEEPTHD (MMA9553_DEFAULT_SAMPLE_RATE * 30)
#define MMA9553_CONFIG_RETRIES 2
/* Status register - activity field */ enum activity_level {
ACTIVITY_UNKNOWN,
ACTIVITY_REST,
ACTIVITY_WALKING,
ACTIVITY_JOGGING,
ACTIVITY_RUNNING,
};
struct mma9553_data { struct i2c_client *client; /* * 1. Serialize access to HW (requested by mma9551_core API). * 2. Serialize sequences that power on/off the device and access HW.
*/ struct mutex mutex; struct mma9553_conf_regs conf; struct mma9553_event events[MMA9553_EVENTS_INFO_SIZE]; int num_events;
u8 gpio_bitnum; /* * This is used for all features that depend on step count: * step count, distance, speed, calories.
*/ bool stepcnt_enabled;
u16 stepcnt;
u8 activity;
s64 timestamp;
};
for (i = 0; i < data->num_events; i++) if (data->events[i].info->type == type &&
data->events[i].info->mod == mod &&
data->events[i].info->dir == dir) return &data->events[i];
for (i = 0; i < data->num_events; i++) if ((check_type && data->events[i].info->type == type &&
data->events[i].enabled) ||
(!check_type && data->events[i].enabled)) returntrue;
/* * If both step detector and activity are enabled, use the MRGFL bit. * This bit is the logical OR of the SUSPCHG, STEPCHG, and ACTCHG flags.
*/ if (activity_enabled && ev_step_detect->enabled)
bitnum = MMA9553_STATUS_TO_BITNUM(MMA9553_MASK_STATUS_MRGFL); elseif (ev_step_detect->enabled)
bitnum = MMA9553_STATUS_TO_BITNUM(MMA9553_MASK_STATUS_STEPCHG); elseif (activity_enabled)
bitnum = MMA9553_STATUS_TO_BITNUM(MMA9553_MASK_STATUS_ACTCHG); else/* Reset */
appid = MMA9551_APPID_NONE;
if (data->gpio_bitnum == bitnum) return 0;
/* Save initial values for activity and stepcnt */ if (activity_enabled || ev_step_detect->enabled) {
ret = mma9553_read_activity_stepcnt(data, &data->activity,
&data->stepcnt); if (ret < 0) return ret;
}
ret = mma9551_gpio_config(data->client, MMA9553_DEFAULT_GPIO_PIN, appid,
bitnum, MMA9553_DEFAULT_GPIO_POLARITY); if (ret < 0) return ret;
data->gpio_bitnum = bitnum;
return 0;
}
staticint mma9553_init(struct mma9553_data *data)
{ int ret;
ret = mma9551_read_version(data->client); if (ret) return ret;
/* * Read all the pedometer configuration registers. This is used as * a device identification command to differentiate the MMA9553L * from the MMA9550L.
*/
ret = mma9551_read_config_words(data->client, MMA9551_APPID_PEDOMETER,
MMA9553_REG_CONF_SLEEPMIN, sizeof(data->conf) / sizeof(u16),
(u16 *)&data->conf); if (ret < 0) {
dev_err(&data->client->dev, "failed to read configuration registers\n"); return ret;
}
/* Reset GPIO */
data->gpio_bitnum = MMA9553_MAX_BITNUM;
ret = mma9553_conf_gpio(data); if (ret < 0) return ret;
ret = mma9551_app_reset(data->client, MMA9551_RSC_PED); if (ret < 0) return ret;
/* Init config registers */
data->conf.sleepmin = MMA9553_DEFAULT_SLEEPMIN;
data->conf.sleepmax = MMA9553_DEFAULT_SLEEPMAX;
data->conf.sleepthd = MMA9553_DEFAULT_SLEEPTHD;
data->conf.config = mma9553_set_bits(data->conf.config, 1,
MMA9553_MASK_CONF_CONFIG); /* * Clear the activity debounce counter when the activity level changes, * so that the confidence level applies for any activity level.
*/
data->conf.config = mma9553_set_bits(data->conf.config, 1,
MMA9553_MASK_CONF_ACT_DBCNTM);
ret = mma9551_write_config_words(data->client, MMA9551_APPID_PEDOMETER,
MMA9553_REG_CONF_SLEEPMIN, sizeof(data->conf) / sizeof(u16),
(u16 *)&data->conf); if (ret < 0) {
dev_err(&data->client->dev, "failed to write configuration registers\n"); return ret;
}
/* * The HW only counts steps and other dependent * parameters (speed, distance, calories, activity) * if power is on (from enabling an event or the * step counter).
*/
powered_on = mma9553_is_any_event_enabled(data, false, 0) ||
data->stepcnt_enabled; if (!powered_on) {
dev_err(&data->client->dev, "No channels enabled\n"); return -EINVAL;
}
/* * The device does not support confidence value levels, * so we will always have 100% for current activity and * 0% for the others.
*/ if (chan->channel2 == mma9553_activity_to_mod(activity))
*val = 100; else
*val = 0; return IIO_VAL_INT; default: return -EINVAL;
} case IIO_CHAN_INFO_RAW: switch (chan->type) { case IIO_VELOCITY: /* m/h */ if (chan->channel2 != IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z) return -EINVAL;
ret = mma9553_read_status_word(data,
MMA9553_REG_SPEED,
&tmp); if (ret < 0) return ret;
*val = tmp; return IIO_VAL_INT; case IIO_ENERGY: /* Cal or kcal */
ret = mma9553_read_status_word(data,
MMA9553_REG_CALORIES,
&tmp); if (ret < 0) return ret;
*val = tmp; return IIO_VAL_INT; case IIO_ACCEL:
mutex_lock(&data->mutex);
ret = mma9551_read_accel_chan(data->client,
chan, val, val2);
mutex_unlock(&data->mutex); return ret; default: return -EINVAL;
} case IIO_CHAN_INFO_SCALE: switch (chan->type) { case IIO_VELOCITY: /* m/h to m/s */ if (chan->channel2 != IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z) return -EINVAL;
*val = 0;
*val2 = 277; /* 0.000277 */ return IIO_VAL_INT_PLUS_MICRO; case IIO_ENERGY: /* Cal or kcal to J */
*val = 4184; return IIO_VAL_INT; case IIO_ACCEL: return mma9551_read_accel_scale(val, val2); default: return -EINVAL;
} case IIO_CHAN_INFO_ENABLE:
*val = data->stepcnt_enabled; return IIO_VAL_INT; case IIO_CHAN_INFO_CALIBHEIGHT:
tmp = mma9553_get_bits(data->conf.height_weight,
MMA9553_MASK_CONF_HEIGHT);
*val = tmp / 100; /* cm to m */
*val2 = (tmp % 100) * 10000; return IIO_VAL_INT_PLUS_MICRO; case IIO_CHAN_INFO_CALIBWEIGHT:
*val = mma9553_get_bits(data->conf.height_weight,
MMA9553_MASK_CONF_WEIGHT); return IIO_VAL_INT; case IIO_CHAN_INFO_DEBOUNCE_COUNT: switch (chan->type) { case IIO_STEPS:
*val = mma9553_get_bits(data->conf.filter,
MMA9553_MASK_CONF_FILTSTEP); return IIO_VAL_INT; default: return -EINVAL;
} case IIO_CHAN_INFO_DEBOUNCE_TIME: switch (chan->type) { case IIO_STEPS:
*val = mma9553_get_bits(data->conf.filter,
MMA9553_MASK_CONF_FILTTIME); return IIO_VAL_INT; default: return -EINVAL;
} case IIO_CHAN_INFO_INT_TIME: switch (chan->type) { case IIO_VELOCITY: if (chan->channel2 != IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z) return -EINVAL;
*val = mma9553_get_bits(data->conf.speed_step,
MMA9553_MASK_CONF_SPDPRD); return IIO_VAL_INT; default: return -EINVAL;
} default: return -EINVAL;
}
}
staticint mma9553_write_raw(struct iio_dev *indio_dev, struct iio_chan_spec const *chan, int val, int val2, long mask)
{ struct mma9553_data *data = iio_priv(indio_dev); int ret, tmp;
switch (mask) { case IIO_CHAN_INFO_ENABLE: if (data->stepcnt_enabled == !!val) return 0;
mutex_lock(&data->mutex);
ret = mma9551_set_power_state(data->client, val); if (ret < 0) {
mutex_unlock(&data->mutex); return ret;
}
data->stepcnt_enabled = val;
mutex_unlock(&data->mutex); return 0; case IIO_CHAN_INFO_CALIBHEIGHT: /* m to cm */
tmp = val * 100 + val2 / 10000; if (tmp < 0 || tmp > 255) return -EINVAL;
mutex_lock(&data->mutex);
ret = mma9553_set_config(data,
MMA9553_REG_CONF_HEIGHT_WEIGHT,
&data->conf.height_weight,
tmp, MMA9553_MASK_CONF_HEIGHT);
mutex_unlock(&data->mutex); return ret; case IIO_CHAN_INFO_CALIBWEIGHT: if (val < 0 || val > 255) return -EINVAL;
mutex_lock(&data->mutex);
ret = mma9553_set_config(data,
MMA9553_REG_CONF_HEIGHT_WEIGHT,
&data->conf.height_weight,
val, MMA9553_MASK_CONF_WEIGHT);
mutex_unlock(&data->mutex); return ret; case IIO_CHAN_INFO_DEBOUNCE_COUNT: switch (chan->type) { case IIO_STEPS: /* * Set to 0 to disable step filtering. If the value * specified is greater than 6, then 6 will be used.
*/ if (val < 0) return -EINVAL; if (val > 6)
val = 6;
mutex_lock(&data->mutex);
ret = mma9553_set_config(data, MMA9553_REG_CONF_FILTER,
&data->conf.filter, val,
MMA9553_MASK_CONF_FILTSTEP);
mutex_unlock(&data->mutex); return ret; default: return -EINVAL;
} case IIO_CHAN_INFO_DEBOUNCE_TIME: switch (chan->type) { case IIO_STEPS: if (val < 0 || val > 127) return -EINVAL;
mutex_lock(&data->mutex);
ret = mma9553_set_config(data, MMA9553_REG_CONF_FILTER,
&data->conf.filter, val,
MMA9553_MASK_CONF_FILTTIME);
mutex_unlock(&data->mutex); return ret; default: return -EINVAL;
} case IIO_CHAN_INFO_INT_TIME: switch (chan->type) { case IIO_VELOCITY: if (chan->channel2 != IIO_MOD_ROOT_SUM_SQUARED_X_Y_Z) return -EINVAL; /* * If set to a value greater than 5, then 5 will be * used. Warning: Do not set SPDPRD to 0 or 1 as * this may cause undesirable behavior.
*/ if (val < 2) return -EINVAL; if (val > 5)
val = 5;
mutex_lock(&data->mutex);
ret = mma9553_set_config(data,
MMA9553_REG_CONF_SPEED_STEP,
&data->conf.speed_step, val,
MMA9553_MASK_CONF_SPDPRD);
mutex_unlock(&data->mutex); return ret; default: return -EINVAL;
} default: return -EINVAL;
}
}
*val2 = 0; switch (info) { case IIO_EV_INFO_VALUE: switch (chan->type) { case IIO_STEPS:
*val = mma9553_get_bits(data->conf.speed_step,
MMA9553_MASK_CONF_STEPCOALESCE); return IIO_VAL_INT; case IIO_ACTIVITY: /* * The device does not support confidence value levels. * We set an average of 50%.
*/
*val = 50; return IIO_VAL_INT; default: return -EINVAL;
} case IIO_EV_INFO_PERIOD: switch (chan->type) { case IIO_ACTIVITY:
*val = MMA9553_ACTIVITY_THD_TO_SEC(data->conf.actthd); return IIO_VAL_INT; default: return -EINVAL;
} default: return -EINVAL;
}
}
staticint mma9553_write_event_value(struct iio_dev *indio_dev, conststruct iio_chan_spec *chan, enum iio_event_type type, enum iio_event_direction dir, enum iio_event_info info, int val, int val2)
{ struct mma9553_data *data = iio_priv(indio_dev); int ret;
switch (info) { case IIO_EV_INFO_VALUE: switch (chan->type) { case IIO_STEPS: if (val < 0 || val > 255) return -EINVAL;
mutex_lock(&data->mutex);
ret = mma9553_set_config(data,
MMA9553_REG_CONF_SPEED_STEP,
&data->conf.speed_step, val,
MMA9553_MASK_CONF_STEPCOALESCE);
mutex_unlock(&data->mutex); return ret; default: return -EINVAL;
} case IIO_EV_INFO_PERIOD: switch (chan->type) { case IIO_ACTIVITY: if (val < 0 || val > MMA9553_ACTIVITY_THD_TO_SEC(
MMA9553_MAX_ACTTHD)) return -EINVAL;
mutex_lock(&data->mutex);
ret = mma9553_set_config(data, MMA9553_REG_CONF_ACTTHD,
&data->conf.actthd,
MMA9553_ACTIVITY_SEC_TO_THD
(val), MMA9553_MASK_CONF_WORD);
mutex_unlock(&data->mutex); return ret; default: return -EINVAL;
} default: return -EINVAL;
}
}
gender = mma9553_get_bits(data->conf.filter, MMA9553_MASK_CONF_MALE); /* * HW expects 0 for female and 1 for male, * while iio index is 0 for male and 1 for female.
*/ return !gender;
}
data->timestamp = iio_get_time_ns(indio_dev); /* * Since we only configure the interrupt pin when an * event is enabled, we are sure we have at least * one event enabled at this point.
*/ return IRQ_WAKE_THREAD;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.