Added interruption feature to the TSL2563 driver
This commit is contained in:
parent
881e78cb60
commit
a8a1ebf8b5
|
@ -39,6 +39,7 @@
|
|||
#define PROJECT_CONF_H_
|
||||
|
||||
#define BROADCAST_CHANNEL 129
|
||||
#define NETSTACK_CONF_RDC nullrdc_driver
|
||||
|
||||
#endif /* PROJECT_CONF_H_ */
|
||||
|
||||
|
|
|
@ -48,16 +48,24 @@
|
|||
#include <stdio.h>
|
||||
#include "contiki.h"
|
||||
#include "dev/i2c.h"
|
||||
#include "dev/leds.h"
|
||||
#include "dev/tsl2563.h"
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/* Default sensor's integration cycle is 402ms */
|
||||
#define SENSOR_READ_INTERVAL (CLOCK_SECOND/2)
|
||||
#define SENSOR_READ_INTERVAL (CLOCK_SECOND)
|
||||
/*---------------------------------------------------------------------------*/
|
||||
PROCESS(remote_tsl2563_process, "TSL2563 test process");
|
||||
AUTOSTART_PROCESSES(&remote_tsl2563_process);
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static struct etimer et;
|
||||
/*---------------------------------------------------------------------------*/
|
||||
void
|
||||
light_interrupt_callback(uint8_t value)
|
||||
{
|
||||
printf("* Light sensor interrupt!\n");
|
||||
leds_toggle(LEDS_PURPLE);
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
PROCESS_THREAD(remote_tsl2563_process, ev, data)
|
||||
{
|
||||
PROCESS_BEGIN();
|
||||
|
@ -69,13 +77,32 @@ PROCESS_THREAD(remote_tsl2563_process, ev, data)
|
|||
/* Default integration time is 402ms with 1x gain, use the below call to
|
||||
* change the gain and timming, see tsl2563.h for more options
|
||||
*/
|
||||
// tsl2563.configure(TSL2563_TIMMING_CFG, TSL2563_G16X_402MS);
|
||||
/* tsl2563.configure(TSL2563_TIMMING_CFG, TSL2563_G16X_402MS); */
|
||||
|
||||
/* Register the interrupt handler */
|
||||
TSL2563_REGISTER_INT(light_interrupt_callback);
|
||||
|
||||
/* Enable the interrupt source for values over the threshold. The sensor
|
||||
* compares against the value of CH0, one way to find out the required
|
||||
* threshold for a given lux quantity is to enable the DEBUG flag and see
|
||||
* the CH0 value for a given measurement. The other is to reverse the
|
||||
* calculations done in the calculate_lux() function. The below value roughly
|
||||
* represents a 2500 lux threshold, same as pointing a flashlight directly
|
||||
*/
|
||||
tsl2563.configure(TSL2563_INT_OVER, 0x15B8);
|
||||
|
||||
/* And periodically poll the sensor */
|
||||
|
||||
while(1) {
|
||||
etimer_set(&et, SENSOR_READ_INTERVAL);
|
||||
PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));
|
||||
light = tsl2563.value(TSL2563_VAL_READ);
|
||||
if(light != TSL2563_ERROR) {
|
||||
printf("Light = %u\n", (uint16_t)light);
|
||||
} else {
|
||||
printf("Error, enable the DEBUG flag in the tsl2563 driver for info, ");
|
||||
printf("or check if the sensor is properly connected\n");
|
||||
}
|
||||
}
|
||||
PROCESS_END();
|
||||
}
|
||||
|
|
|
@ -47,7 +47,7 @@
|
|||
#include "lib/sensors.h"
|
||||
#include "tsl2563.h"
|
||||
/*---------------------------------------------------------------------------*/
|
||||
#define DEBUG 1
|
||||
#define DEBUG 0
|
||||
#if DEBUG
|
||||
#define PRINTF(...) printf(__VA_ARGS__)
|
||||
#else
|
||||
|
@ -66,9 +66,9 @@ void (*tsl2563_int_callback)(uint8_t value);
|
|||
static uint16_t
|
||||
calculate_lux(uint8_t *buf)
|
||||
{
|
||||
uint32_t ch0, ch1 = 0;
|
||||
uint32_t chscale;
|
||||
uint32_t ratio, lratio, tmp = 0;
|
||||
uint32_t ch0, ch1, chscale = 0;
|
||||
uint32_t ratio = 0;
|
||||
uint32_t lratio, tmp = 0;
|
||||
uint16_t buffer[2];
|
||||
|
||||
/* The calculations below assume the integration time is 402ms and the gain
|
||||
|
@ -208,6 +208,17 @@ tsl2563_off(void)
|
|||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static int
|
||||
tsl2563_clear_interrupt(void)
|
||||
{
|
||||
uint8_t buf = (TSL2563_COMMAND + TSL2563_CLEAR_INTERRUPT);
|
||||
if(tsl2563_write_reg(&buf, 1) != I2C_MASTER_ERR_NONE) {
|
||||
PRINTF("TSL2563: failed to clear the interrupt\n");
|
||||
return TSL2563_ERROR;
|
||||
}
|
||||
return TSL2563_SUCCESS;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static int
|
||||
tsl2563_read_sensor(uint16_t *lux)
|
||||
{
|
||||
uint8_t buf[4];
|
||||
|
@ -217,6 +228,9 @@ tsl2563_read_sensor(uint16_t *lux)
|
|||
&buf[0], 2) == TSL2563_SUCCESS) {
|
||||
if(tsl2563_read_reg((TSL2563_COMMAND + TSL2563_D1LOW),
|
||||
&buf[2], 2) == TSL2563_SUCCESS) {
|
||||
|
||||
PRINTF("TSL2563: CH0 0x%02X%02X CH1 0x%02X%02X\n", buf[1], buf[0],
|
||||
buf[3], buf[2]);
|
||||
*lux = calculate_lux(buf);
|
||||
return TSL2563_SUCCESS;
|
||||
}
|
||||
|
@ -231,27 +245,28 @@ PROCESS_THREAD(tsl2563_int_process, ev, data)
|
|||
{
|
||||
PROCESS_EXITHANDLER();
|
||||
PROCESS_BEGIN();
|
||||
|
||||
while(1) {
|
||||
PROCESS_YIELD_UNTIL(ev == PROCESS_EVENT_POLL);
|
||||
|
||||
/* FIXME: Read interrupt source and clear */
|
||||
tsl2563_clear_interrupt();
|
||||
tsl2563_int_callback(0);
|
||||
}
|
||||
PROCESS_END();
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static void
|
||||
tsl2563_interrupt_handler(uint8_t port, uint8_t pin)
|
||||
{
|
||||
/* FIXME: Check if the interrupt is ours */
|
||||
/* There's no alert/interruption flag to check, clear the interruption by
|
||||
* writting to the CLEAR bit in the COMMAND register
|
||||
*/
|
||||
process_poll(&tsl2563_int_process);
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static int
|
||||
configure(int type, int value)
|
||||
{
|
||||
uint8_t buf[2];
|
||||
uint8_t buf[3];
|
||||
|
||||
if((type != TSL2563_ACTIVE) && (type != TSL2563_INT_OVER) &&
|
||||
(type != TSL2563_INT_BELOW) && (type != TSL2563_INT_DISABLE) &&
|
||||
|
@ -269,6 +284,9 @@ configure(int type, int value)
|
|||
i2c_init(I2C_SDA_PORT, I2C_SDA_PIN, I2C_SCL_PORT, I2C_SCL_PIN,
|
||||
I2C_SCL_NORMAL_BUS_SPEED);
|
||||
|
||||
/* Initialize interrupts handlers */
|
||||
tsl2563_int_callback = NULL;
|
||||
|
||||
/* Power on the sensor and check for the part number */
|
||||
if(tsl2563_on() == TSL2563_SUCCESS) {
|
||||
if(tsl2563_id_register(&buf[0]) == TSL2563_SUCCESS) {
|
||||
|
@ -280,13 +298,16 @@ configure(int type, int value)
|
|||
gain = buf[0] & TSL2563_TIMMING_GAIN;
|
||||
timming = buf[0] & TSL2563_TIMMING_INTEG_MASK;
|
||||
PRINTF("TSL2563: enabled, timming %u gain %u\n", timming, gain);
|
||||
|
||||
/* Clear any pending interrupt */
|
||||
if(tsl2563_clear_interrupt() == TSL2563_SUCCESS) {
|
||||
return TSL2563_SUCCESS;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return TSL2563_ERROR;
|
||||
|
||||
} else {
|
||||
if(tsl2563_off() == TSL2563_SUCCESS) {
|
||||
PRINTF("TSL2563: stopped\n");
|
||||
|
@ -297,8 +318,23 @@ configure(int type, int value)
|
|||
}
|
||||
|
||||
if(type == TSL2563_INT_DISABLE) {
|
||||
/* FIXME: disable interrupt */
|
||||
|
||||
/* Ensure the GPIO doesn't generate more interrupts, this may affect others
|
||||
* I2C digital sensors using the bus and sharing this pin, so an user may
|
||||
* comment the line below
|
||||
*/
|
||||
GPIO_DISABLE_INTERRUPT(TSL2563_INT_PORT_BASE, TSL2563_INT_PIN_MASK);
|
||||
|
||||
/* This also wipes out the persistance value, to be reconfigured when
|
||||
* enabling back the interruption
|
||||
*/
|
||||
buf[0] = (TSL2563_COMMAND + TSL2563_INTERRUPT);
|
||||
buf[1] = TSL2563_INTR_DISABLED;
|
||||
|
||||
if(tsl2563_write_reg(buf, 2) != TSL2563_SUCCESS) {
|
||||
PRINTF("TSL2563: failed to disable the interrupt\n");
|
||||
return TSL2563_ERROR;
|
||||
}
|
||||
return TSL2563_SUCCESS;
|
||||
}
|
||||
|
||||
|
@ -338,8 +374,36 @@ configure(int type, int value)
|
|||
return TSL2563_ERROR;
|
||||
}
|
||||
|
||||
/* Configure interrupt pins and initialize interrupts handlers */
|
||||
tsl2563_int_callback = NULL;
|
||||
/* From here we handle the interrupt configuration, it requires the interrupt
|
||||
* callback handler to have been previously set using the TSL2563_REGISTER_INT
|
||||
* macro
|
||||
*/
|
||||
|
||||
buf[1] = ((uint8_t *)&value)[0];
|
||||
buf[2] = ((uint8_t *)&value)[1];
|
||||
|
||||
if(type == TSL2563_INT_OVER) {
|
||||
buf[0] = (TSL2563_COMMAND + TSL2563_THRHIGHLOW);
|
||||
} else if(type == TSL2563_INT_BELOW) {
|
||||
buf[0] = (TSL2563_COMMAND + TSL2563_THRLOWLOW);
|
||||
}
|
||||
|
||||
if(tsl2563_write_reg(buf, 3) != TSL2563_SUCCESS) {
|
||||
PRINTF("TSL2563: failed to set interrupt level\n");
|
||||
return TSL2563_ERROR;
|
||||
}
|
||||
|
||||
/* Now configure the interruption register (level interrupt, 2 integration
|
||||
* cycles after threshold has been reached (roughly 804ms if timming is 402ms)
|
||||
*/
|
||||
buf[0] = (TSL2563_COMMAND + TSL2563_INTERRUPT);
|
||||
buf[1] = (TSL2563_INTR_LEVEL << TSL2563_INTR_SHIFT);
|
||||
buf[1] += TSL2563_INT_PERSIST_2_CYCLES;
|
||||
|
||||
if(tsl2563_write_reg(buf, 2) != TSL2563_SUCCESS) {
|
||||
PRINTF("TSL2563: failed to enable interrupt\n");
|
||||
return TSL2563_ERROR;
|
||||
}
|
||||
|
||||
/* Configure the interrupts pins */
|
||||
GPIO_SOFTWARE_CONTROL(TSL2563_INT_PORT_BASE, TSL2563_INT_PIN_MASK);
|
||||
|
@ -354,17 +418,17 @@ configure(int type, int value)
|
|||
/* Spin process until an interrupt is received */
|
||||
process_start(&tsl2563_int_process, NULL);
|
||||
|
||||
if(type == TSL2563_INT_OVER) {
|
||||
/* FIXME: add code */
|
||||
} else if(type == TSL2563_INT_BELOW) {
|
||||
/* FIXME: add code */
|
||||
}
|
||||
|
||||
/* Enable interrupts */
|
||||
GPIO_ENABLE_INTERRUPT(TSL2563_INT_PORT_BASE, TSL2563_INT_PIN_MASK);
|
||||
|
||||
/* The RE-Mote revision A has this pin shared and with a pull-down resistor,
|
||||
* for other platforms (like the firefly), change to enable pull-up internal
|
||||
* resistor instead if no external pull-up is present.
|
||||
*/
|
||||
ioc_set_over(I2C_INT_PORT, I2C_INT_PIN, IOC_OVERRIDE_PUE);
|
||||
nvic_interrupt_enable(I2C_INT_VECTOR);
|
||||
|
||||
PRINTF("TSL2563: Interrupt configured\n");
|
||||
return TSL2563_SUCCESS;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
|
|
|
@ -79,6 +79,7 @@
|
|||
/* -------------------------------------------------------------------------- */
|
||||
/* Uses the word read/write operation protocol */
|
||||
#define TSL2563_COMMAND 0xA0
|
||||
#define TSL2563_CLEAR_INTERRUPT 0x40
|
||||
/* -------------------------------------------------------------------------- */
|
||||
#define TSL2563_CONTROL_POWER_ON 0x03
|
||||
#define TSL2563_CONTROL_POWER_OFF 0x00
|
||||
|
|
|
@ -250,6 +250,9 @@
|
|||
#define I2C_SCL_PIN 3
|
||||
#define I2C_SDA_PORT GPIO_C_NUM
|
||||
#define I2C_SDA_PIN 2
|
||||
#define I2C_INT_PORT GPIO_D_NUM
|
||||
#define I2C_INT_PIN 1
|
||||
#define I2C_INT_VECTOR NVIC_INT_GPIO_PORT_D
|
||||
/** @} */
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/**
|
||||
|
|
Loading…
Reference in a new issue