mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-11-01 05:51:36 +08:00
Add reset and samplerate ioctl to HMC5883 driver
This commit is contained in:
@@ -191,6 +191,11 @@ private:
|
|||||||
*/
|
*/
|
||||||
void stop();
|
void stop();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reset the device
|
||||||
|
*/
|
||||||
|
int reset();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Perform the on-sensor scale calibration routine.
|
* Perform the on-sensor scale calibration routine.
|
||||||
*
|
*
|
||||||
@@ -365,6 +370,9 @@ HMC5883::init()
|
|||||||
if (I2C::init() != OK)
|
if (I2C::init() != OK)
|
||||||
goto out;
|
goto out;
|
||||||
|
|
||||||
|
/* reset the device configuration */
|
||||||
|
reset();
|
||||||
|
|
||||||
/* allocate basic report buffers */
|
/* allocate basic report buffers */
|
||||||
_num_reports = 2;
|
_num_reports = 2;
|
||||||
_reports = new struct mag_report[_num_reports];
|
_reports = new struct mag_report[_num_reports];
|
||||||
@@ -381,9 +389,6 @@ HMC5883::init()
|
|||||||
if (_mag_topic < 0)
|
if (_mag_topic < 0)
|
||||||
debug("failed to create sensor_mag object");
|
debug("failed to create sensor_mag object");
|
||||||
|
|
||||||
/* set range */
|
|
||||||
set_range(_range_ga);
|
|
||||||
|
|
||||||
ret = OK;
|
ret = OK;
|
||||||
/* sensor is ok, but not calibrated */
|
/* sensor is ok, but not calibrated */
|
||||||
_sensor_ok = true;
|
_sensor_ok = true;
|
||||||
@@ -542,62 +547,61 @@ int
|
|||||||
HMC5883::ioctl(struct file *filp, int cmd, unsigned long arg)
|
HMC5883::ioctl(struct file *filp, int cmd, unsigned long arg)
|
||||||
{
|
{
|
||||||
switch (cmd) {
|
switch (cmd) {
|
||||||
|
|
||||||
case SENSORIOCSPOLLRATE: {
|
case SENSORIOCSPOLLRATE: {
|
||||||
switch (arg) {
|
switch (arg) {
|
||||||
|
|
||||||
|
/* switching to manual polling */
|
||||||
|
case SENSOR_POLLRATE_MANUAL:
|
||||||
|
stop();
|
||||||
|
_measure_ticks = 0;
|
||||||
|
return OK;
|
||||||
|
|
||||||
|
/* external signalling (DRDY) not supported */
|
||||||
|
case SENSOR_POLLRATE_EXTERNAL:
|
||||||
|
|
||||||
|
/* zero would be bad */
|
||||||
|
case 0:
|
||||||
|
return -EINVAL;
|
||||||
|
|
||||||
|
/* set default/max polling rate */
|
||||||
|
case SENSOR_POLLRATE_MAX:
|
||||||
|
case SENSOR_POLLRATE_DEFAULT: {
|
||||||
|
/* do we need to start internal polling? */
|
||||||
|
bool want_start = (_measure_ticks == 0);
|
||||||
|
|
||||||
|
/* set interval for next measurement to minimum legal value */
|
||||||
|
_measure_ticks = USEC2TICK(HMC5883_CONVERSION_INTERVAL);
|
||||||
|
|
||||||
|
/* if we need to start the poll state machine, do it */
|
||||||
|
if (want_start)
|
||||||
|
start();
|
||||||
|
|
||||||
/* switching to manual polling */
|
|
||||||
case SENSOR_POLLRATE_MANUAL:
|
|
||||||
stop();
|
|
||||||
_measure_ticks = 0;
|
|
||||||
return OK;
|
return OK;
|
||||||
|
}
|
||||||
|
|
||||||
/* external signalling (DRDY) not supported */
|
/* adjust to a legal polling interval in Hz */
|
||||||
case SENSOR_POLLRATE_EXTERNAL:
|
default: {
|
||||||
|
/* do we need to start internal polling? */
|
||||||
|
bool want_start = (_measure_ticks == 0);
|
||||||
|
|
||||||
/* zero would be bad */
|
/* convert hz to tick interval via microseconds */
|
||||||
case 0:
|
unsigned ticks = USEC2TICK(1000000 / arg);
|
||||||
return -EINVAL;
|
|
||||||
|
|
||||||
/* set default/max polling rate */
|
/* check against maximum rate */
|
||||||
case SENSOR_POLLRATE_MAX:
|
if (ticks < USEC2TICK(HMC5883_CONVERSION_INTERVAL))
|
||||||
case SENSOR_POLLRATE_DEFAULT: {
|
return -EINVAL;
|
||||||
/* do we need to start internal polling? */
|
|
||||||
bool want_start = (_measure_ticks == 0);
|
|
||||||
|
|
||||||
/* set interval for next measurement to minimum legal value */
|
/* update interval for next measurement */
|
||||||
_measure_ticks = USEC2TICK(HMC5883_CONVERSION_INTERVAL);
|
_measure_ticks = ticks;
|
||||||
|
|
||||||
/* if we need to start the poll state machine, do it */
|
/* if we need to start the poll state machine, do it */
|
||||||
if (want_start)
|
if (want_start)
|
||||||
start();
|
start();
|
||||||
|
|
||||||
return OK;
|
return OK;
|
||||||
}
|
|
||||||
|
|
||||||
/* adjust to a legal polling interval in Hz */
|
|
||||||
default: {
|
|
||||||
/* do we need to start internal polling? */
|
|
||||||
bool want_start = (_measure_ticks == 0);
|
|
||||||
|
|
||||||
/* convert hz to tick interval via microseconds */
|
|
||||||
unsigned ticks = USEC2TICK(1000000 / arg);
|
|
||||||
|
|
||||||
/* check against maximum rate */
|
|
||||||
if (ticks < USEC2TICK(HMC5883_CONVERSION_INTERVAL))
|
|
||||||
return -EINVAL;
|
|
||||||
|
|
||||||
/* update interval for next measurement */
|
|
||||||
_measure_ticks = ticks;
|
|
||||||
|
|
||||||
/* if we need to start the poll state machine, do it */
|
|
||||||
if (want_start)
|
|
||||||
start();
|
|
||||||
|
|
||||||
return OK;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
case SENSORIOCGPOLLRATE:
|
case SENSORIOCGPOLLRATE:
|
||||||
if (_measure_ticks == 0)
|
if (_measure_ticks == 0)
|
||||||
@@ -633,13 +637,15 @@ HMC5883::ioctl(struct file *filp, int cmd, unsigned long arg)
|
|||||||
return _num_reports - 1;
|
return _num_reports - 1;
|
||||||
|
|
||||||
case SENSORIOCRESET:
|
case SENSORIOCRESET:
|
||||||
/* XXX implement this */
|
return reset();
|
||||||
return -EINVAL;
|
|
||||||
|
|
||||||
case MAGIOCSSAMPLERATE:
|
case MAGIOCSSAMPLERATE:
|
||||||
|
/* same as pollrate because device is in single measurement mode*/
|
||||||
|
return ioctl(filp, SENSORIOCSPOLLRATE, arg);
|
||||||
|
|
||||||
case MAGIOCGSAMPLERATE:
|
case MAGIOCGSAMPLERATE:
|
||||||
/* not supported, always 1 sample per poll */
|
/* same as pollrate because device is in single measurement mode*/
|
||||||
return -EINVAL;
|
return ioctl(filp, SENSORIOCGPOLLRATE, 0);
|
||||||
|
|
||||||
case MAGIOCSRANGE:
|
case MAGIOCSRANGE:
|
||||||
return set_range(arg);
|
return set_range(arg);
|
||||||
@@ -702,6 +708,13 @@ HMC5883::stop()
|
|||||||
work_cancel(HPWORK, &_work);
|
work_cancel(HPWORK, &_work);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
HMC5883::reset()
|
||||||
|
{
|
||||||
|
/* set range */
|
||||||
|
return set_range(_range_ga);
|
||||||
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
HMC5883::cycle_trampoline(void *arg)
|
HMC5883::cycle_trampoline(void *arg)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user