diff --git a/examples/zolertia/zoul/test-pm10-sensor.c b/examples/zolertia/zoul/test-pm10-sensor.c index 7aedbf60c..d307f9050 100644 --- a/examples/zolertia/zoul/test-pm10-sensor.c +++ b/examples/zolertia/zoul/test-pm10-sensor.c @@ -64,13 +64,11 @@ static struct etimer et; PROCESS_THREAD(test_pm10_sensor_process, ev, data) { PROCESS_BEGIN(); - SENSORS_ACTIVATE(pm10); static uint16_t pm10_value; - /* Configure the ADC ports */ /* Use pin number not mask, for example if using the PA5 pin then use 2 */ - printf("return configure, %d \n", pm10.configure(SENSORS_ACTIVE, ADC_PIN)); + pm10.configure(SENSORS_ACTIVE, ADC_PIN); /* And periodically poll the sensor */ @@ -79,8 +77,8 @@ PROCESS_THREAD(test_pm10_sensor_process, ev, data) PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et)); leds_toggle(LEDS_GREEN); - printf("PM10 value\n"); pm10_value = pm10.value(1); + if(pm10_value != ADC_WRAPPER_ERROR) { printf("PM10 value = %u ppm\n", pm10_value); } else { diff --git a/platform/zoul/dev/pm10-sensor.c b/platform/zoul/dev/pm10-sensor.c index 015852c93..8f94eb04f 100644 --- a/platform/zoul/dev/pm10-sensor.c +++ b/platform/zoul/dev/pm10-sensor.c @@ -49,52 +49,54 @@ #include "dev/gpio.h" #include "dev/ioc.h" /*---------------------------------------------------------------------------*/ +#define PM10_SENSOR_PORT_BASE GPIO_PORT_TO_BASE(PM10_SENSOR_CTRL_PORT) +#define PM10_SENSOR_PIN_MASK GPIO_PIN_MASK(PM10_SENSOR_CTRL_PIN) +/*---------------------------------------------------------------------------*/ static uint8_t enabled; /*---------------------------------------------------------------------------*/ -#ifdef PM10_SENSOR_CONF_CTRL_PIN -#define PM10_SENSOR_CTRL_PIN PM10_SENSOR_CONF_CTRL_PIN -#else -#define PM10_SENSOR_CTRL_PIN 0x07 -#endif - -#define PM10_SENSOR_PORT GPIO_A_BASE - static int configure(int type, int value) { - int error; if(type != SENSORS_ACTIVE) { return PM10_ERROR; } - /*Set PA7 as output, used as pulse-driven wave*/ - ioc_set_over(PM10_SENSOR_PORT, PM10_SENSOR_CTRL_PIN, IOC_OVERRIDE_DIS); - GPIO_SOFTWARE_CONTROL(PM10_SENSOR_PORT, GPIO_PIN_MASK(PM10_SENSOR_CTRL_PIN)); - GPIO_SET_OUTPUT(PM10_SENSOR_PORT, GPIO_PIN_MASK(PM10_SENSOR_CTRL_PIN)); + if(value) { + /* Set as output, used as pulse-driven wave */ + GPIO_SOFTWARE_CONTROL(PM10_SENSOR_PORT_BASE, PM10_SENSOR_PIN_MASK); + ioc_set_over(PM10_SENSOR_CTRL_PORT, PM10_SENSOR_CTRL_PIN, IOC_OVERRIDE_DIS); + GPIO_SET_OUTPUT(PM10_SENSOR_PORT_BASE, PM10_SENSOR_PIN_MASK); + GPIO_CLR_PIN(PM10_SENSOR_PORT_BASE, PM10_SENSOR_PIN_MASK); - GPIO_CLR_PIN(PM10_SENSOR_PORT, GPIO_PIN_MASK(PM10_SENSOR_CTRL_PIN)); + enabled = 1; + return adc_sensors.configure(ANALOG_PM10_SENSOR, value); + } - /* Use pin number not mask, for example if using the PA5 pin then use 5 */ - error = adc_sensors.configure(ANALOG_PM10_SENSOR, value); - - return error; + enabled = 0; + return PM10_SUCCESS; } /*---------------------------------------------------------------------------*/ static int value(int type) { uint16_t val; - /*Set Pulse Wave pin before measure*/ - GPIO_SET_PIN(PM10_SENSOR_PORT, GPIO_PIN_MASK(PM10_SENSOR_CTRL_PIN)); - /*Pulse wave delay*/ - clock_delay_usec(280); - /*Data acquisition*/ + + if(!enabled) { + return PM10_ERROR; + } + + /* Set Pulse Wave pin before measure */ + GPIO_SET_PIN(PM10_SENSOR_PORT_BASE, PM10_SENSOR_PIN_MASK); + /* Pulse wave delay */ + clock_delay_usec(PM10_SENSOR_PULSE_DELAY); + /* Data acquisition */ val = adc_sensors.value(ANALOG_PM10_SENSOR); - /*Clear pulse wave pin*/ - GPIO_CLR_PIN(PM10_SENSOR_PORT, GPIO_PIN_MASK(PM10_SENSOR_CTRL_PIN)); + /* Clear pulse wave pin */ + GPIO_CLR_PIN(PM10_SENSOR_PORT_BASE, PM10_SENSOR_PIN_MASK); - /*Applied constant conversion from UAir project*/ - /*to obtain value in ppm (value in mV * 0.28)*/ + /* Applied constant conversion from UAir project + * to obtain value in ppm (value in mV * 0.28) + */ val *= 28; val /= 1000; diff --git a/platform/zoul/dev/pm10-sensor.h b/platform/zoul/dev/pm10-sensor.h index e090f8202..61057e0d1 100644 --- a/platform/zoul/dev/pm10-sensor.h +++ b/platform/zoul/dev/pm10-sensor.h @@ -43,17 +43,29 @@ */ /*---------------------------------------------------------------------------*/ #include "lib/sensors.h" - +/*---------------------------------------------------------------------------*/ #ifndef PM10_SENSOR_H_ #define PM10_SENSOR_H_ - /* -------------------------------------------------------------------------- */ -#define PM10_ERROR -1 -#define PM10_SENSOR "PM10 Sensor" +#define PM10_ERROR (-1) +#define PM10_SUCCESS 0 +#define PM10_SENSOR "PM10 Sensor" +#define PM10_SENSOR_PULSE_DELAY 280 +/* -------------------------------------------------------------------------- */ +#ifdef PM10_SENSOR_CONF_CTRL_PIN +#define PM10_SENSOR_CTRL_PIN PM10_SENSOR_CONF_CTRL_PIN +#else +#define PM10_SENSOR_CTRL_PIN 7 +#endif +#ifdef PM10_SENSOR_CONF_CTRL_PORT +#define PM10_SENSOR_CTRL_PORT PM10_SENSOR_CONF_CTRL_PORT +#else +#define PM10_SENSOR_CTRL_PORT GPIO_A_NUM +#endif /* -------------------------------------------------------------------------- */ extern const struct sensors_sensor pm10; /* -------------------------------------------------------------------------- */ -#endif /* ifndef VAC_SENSOR_H_ */ +#endif /* ifndef PM10_SENSOR_H_ */ /** * @} * @}