Clear interrupt and trigger callback if new data is generated

This commit is contained in:
Antonio Lignan 2016-01-21 16:21:00 +01:00
parent f5b52e8094
commit 426fa24e50
2 changed files with 40 additions and 15 deletions

View file

@ -98,6 +98,9 @@ PROCESS_THREAD(remote_grove_gyro_process, ev, data)
PROCESS_EXIT(); PROCESS_EXIT();
} }
/* Register the interrupt handler */
GROVE_GYRO_REGISTER_INT(gyro_interrupt_callback);
/* The gyroscope sensor should be on now but the three gyroscope axis should /* The gyroscope sensor should be on now but the three gyroscope axis should
* be off, to enable a single axis or any combination of the 3 you can use as * be off, to enable a single axis or any combination of the 3 you can use as
* argument either GROVE_GYRO_X, GROVE_GYRO_Y, GROVE_GYRO_Z. To enable or * argument either GROVE_GYRO_X, GROVE_GYRO_Y, GROVE_GYRO_Z. To enable or
@ -105,8 +108,12 @@ PROCESS_THREAD(remote_grove_gyro_process, ev, data)
*/ */
grove_gyro.configure(GROVE_GYRO_POWER_ON, GROVE_GYRO_XYZ); grove_gyro.configure(GROVE_GYRO_POWER_ON, GROVE_GYRO_XYZ);
/* Register the interrupt handler */ /* Enabling the data interrupt will feed data directly to the extern data
GROVE_GYRO_REGISTER_INT(gyro_interrupt_callback); * structure and notify us about it, depending on the sampling rate and
* divisor this could generate many interrupts/second. A zero argument would
* disable the interrupts
*/
grove_gyro.configure(GROVE_GYRO_DATA_INTERRUPT, 1);
/* And periodically poll the sensor, values are in º/s */ /* And periodically poll the sensor, values are in º/s */
@ -124,12 +131,12 @@ PROCESS_THREAD(remote_grove_gyro_process, ev, data)
if(grove_gyro.value(GROVE_GYRO_XYZ) == GROVE_GYRO_SUCCESS) { if(grove_gyro.value(GROVE_GYRO_XYZ) == GROVE_GYRO_SUCCESS) {
/* Converted values with a 2-digit precision */ /* Converted values with a 2-digit precision */
printf("Gyro: X %u.%u, Y %u.%u, Z %u.%u\n", gyro_values.x / 100, printf("Gyro: X: %u.%u, Y: %u.%u, Z: %u.%u\n", gyro_values.x / 100,
gyro_values.x % 100, gyro_values.x % 100,
gyro_values.y / 100, gyro_values.y / 100,
gyro_values.y % 100, gyro_values.y % 100,
gyro_values.z / 100, gyro_values.z / 100,
gyro_values.z % 100); gyro_values.z % 100);
} else { } else {
printf("Error, enable the DEBUG flag in the grove-gyro driver for info,"); printf("Error, enable the DEBUG flag in the grove-gyro driver for info,");
printf(" or check if the sensor is properly connected\n"); printf(" or check if the sensor is properly connected\n");

View file

@ -46,7 +46,7 @@
#include "dev/grove-gyro.h" #include "dev/grove-gyro.h"
#include "lib/sensors.h" #include "lib/sensors.h"
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
#define DEBUG 1 #define DEBUG 0
#if DEBUG #if DEBUG
#define PRINTF(...) printf(__VA_ARGS__) #define PRINTF(...) printf(__VA_ARGS__)
#else #else
@ -110,10 +110,19 @@ grove_gyro_sampdiv(uint8_t value)
return GROVE_GYRO_ERROR; return GROVE_GYRO_ERROR;
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
static void static uint8_t
grove_gyro_clear_interrupt(void) grove_gyro_clear_interrupt(void)
{ {
/* FIXME */ uint8_t aux = 0;
/* Clear interrupt */
grove_gyro_read_reg(GROVE_GYRO_INT_STATUS, &aux, 1);
if(aux & GROVE_GYRO_INT_STATUS_DATA_RDY_MASK) {
return GROVE_GYRO_INT_STATUS_DATA_RDY_MASK;
}
return 0;
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
static int static int
@ -199,7 +208,7 @@ grove_gyro_power_mgmt(uint8_t value, uint8_t type)
/* Power-up delay */ /* Power-up delay */
if(type == GROVE_GYRO_POWER_ON) { if(type == GROVE_GYRO_POWER_ON) {
// clock_delay_usec(50000); clock_delay_usec(25000);
} }
return GROVE_GYRO_SUCCESS; return GROVE_GYRO_SUCCESS;
@ -345,10 +354,20 @@ PROCESS_THREAD(grove_gyro_int_process, ev, data)
PROCESS_EXITHANDLER(); PROCESS_EXITHANDLER();
PROCESS_BEGIN(); PROCESS_BEGIN();
static uint8_t axis_to_read = 0;
while(1) { while(1) {
PROCESS_YIELD_UNTIL(ev == PROCESS_EVENT_POLL); PROCESS_YIELD_UNTIL(ev == PROCESS_EVENT_POLL);
grove_gyro_clear_interrupt(); if(grove_gyro_clear_interrupt() == GROVE_GYRO_INT_STATUS_DATA_RDY_MASK) {
grove_gyro_int_callback(0);
axis_to_read += (power_mgmt & GROVE_GYRO_X) ? 0: GROVE_GYRO_X;
axis_to_read += (power_mgmt & GROVE_GYRO_Y) ? 0: GROVE_GYRO_Y;
axis_to_read += (power_mgmt & GROVE_GYRO_Z) ? 0: GROVE_GYRO_Z;
if(grove_gyro_read(axis_to_read) == GROVE_GYRO_SUCCESS) {
grove_gyro_int_callback(GROVE_GYRO_SUCCESS);
}
}
} }
PROCESS_END(); PROCESS_END();
} }
@ -466,7 +485,6 @@ configure(int type, int value)
/* Enable interrupt and latch the pin until cleared */ /* Enable interrupt and latch the pin until cleared */
if(grove_gyro_interrupt(GROVE_GYRO_INT_CFG_RAW_READY_EN + if(grove_gyro_interrupt(GROVE_GYRO_INT_CFG_RAW_READY_EN +
GROVE_GYRO_INT_CFG_LATCH_CLR_ANY +
GROVE_GYRO_INT_CFG_LATCH_EN) == GROVE_GYRO_ERROR) { GROVE_GYRO_INT_CFG_LATCH_EN) == GROVE_GYRO_ERROR) {
PRINTF("Gyro: failed to enable the interrupt\n"); PRINTF("Gyro: failed to enable the interrupt\n");
return GROVE_GYRO_ERROR; return GROVE_GYRO_ERROR;