diff --git a/arch/platform/zoul/remote-revb/power-mgmt.c b/arch/platform/zoul/remote-revb/power-mgmt.c index 0de8a890352f0e27a0cb9ddf53b338d5cb6a482a..8076ccc409a40556210f7e24e088d9a483e3d14a 100644 --- a/arch/platform/zoul/remote-revb/power-mgmt.c +++ b/arch/platform/zoul/remote-revb/power-mgmt.c @@ -62,7 +62,7 @@ #define PM_ENABLE_AS_OUTPUT GPIO_SET_OUTPUT(PM_ENABLE_PORT_BASE, \ PM_ENABLE_PIN_MASK) #define PM_ENABLE_LINE_CMD PM_ENABLE_LINE_SET; \ - clock_delay_usec(100); + clock_delay_usec(100); /* -------------------------------------------------------------------------- */ #define PM_NUMBITS(X) (1 << ((X)-1)) @@ -215,7 +215,7 @@ pm_get_num_cycles(void) } PM_ENABLE_LINE_CLR; PRINTF("PM: Sleep cycles: 0x%02x%02x%02x%02x\n", lbuf[0], lbuf[1], lbuf[2], - lbuf[3]); + lbuf[3]); return retval; } /* -------------------------------------------------------------------------- */ @@ -249,6 +249,8 @@ pm_shutdown_now(uint8_t type) int8_t pm_get_voltage(uint16_t *state) { + float result = 0x00; + if(!initialized) { return PM_ERROR; } @@ -260,10 +262,12 @@ pm_get_voltage(uint16_t *state) *state = (uint16_t)lbuf[0] << 8; *state += lbuf[1]; - /* Delay required for the command to finish */ - clock_delay_usec(3000); + /* Compensation */ + result = *state - PM_VBAT_OFF; + result /= PM_VBAT_MULT; + + *state = (uint16_t)(result * 100); - PRINTF("PM: Voltage %u [%u][%u]\n", *state, lbuf[0], lbuf[1]); PM_ENABLE_LINE_CLR; return PM_SUCCESS; } diff --git a/arch/platform/zoul/remote-revb/power-mgmt.h b/arch/platform/zoul/remote-revb/power-mgmt.h index d7ea54d795509117308314cab67eef758bba13be..502b59a86473d8f5ac046a6299097caa1d95a8b1 100644 --- a/arch/platform/zoul/remote-revb/power-mgmt.h +++ b/arch/platform/zoul/remote-revb/power-mgmt.h @@ -132,6 +132,12 @@ typedef enum { PM_USBSUSPEND /* USB in suspend mode */ } pm_charge_current_states; +/* + * VBAT Curve compensation + */ +#define PM_VBAT_MULT (float)-181.9 +#define PM_VBAT_OFF (float)1294.72 + /** @} */ /* -------------------------------------------------------------------------- */ /** \name Power Management functions diff --git a/examples/platform-specific/zoul/Makefile b/examples/platform-specific/zoul/Makefile index 3b57eb0355415bfcbab1d711395b0ff4eedb51fa..1f29bda4cada99850e2c3656f2883bc7eb6612ba 100644 --- a/examples/platform-specific/zoul/Makefile +++ b/examples/platform-specific/zoul/Makefile @@ -4,7 +4,7 @@ CONTIKI_PROJECT += test-grove-light-sensor test-grove-loudness-sensor CONTIKI_PROJECT += test-weather-meter test-grove-gyro test-lcd test-iaq CONTIKI_PROJECT += test-pm10-sensor test-vac-sensor test-aac-sensor CONTIKI_PROJECT += test-zonik test-dht22 test-ac-dimmer -CONTIKI_PROJECT += test-bme280 +CONTIKI_PROJECT += test-bme280 test-vbat CONTIKI_TARGET_SOURCEFILES += tsl256x.c sht25.c bmpx8x.c motion-sensor.c CONTIKI_TARGET_SOURCEFILES += adc-sensors.c weather-meter.c grove-gyro.c diff --git a/examples/platform-specific/zoul/test-vbat.c b/examples/platform-specific/zoul/test-vbat.c new file mode 100644 index 0000000000000000000000000000000000000000..e72c6ef8c33c1c9f5d756ac81be25bbc61ab5460 --- /dev/null +++ b/examples/platform-specific/zoul/test-vbat.c @@ -0,0 +1,96 @@ +/* + * Copyright (c) 2016, Zolertia - http://www.zolertia.com + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + */ +/*---------------------------------------------------------------------------*/ +/** + * \file + * An example showing how to get VBAT Voltage functionality on + * RE-Mote Platform + * \author + * Erik Bellido <ebellido@zolertia.com> + * Aitor Mejias <amejias@zolertia.com> + */ +/*---------------------------------------------------------------------------*/ +/* This is the main contiki header, it should be included always */ +#include "contiki.h" +#include "power-mgmt.h" +#include "dev/i2c.h" +#include "sys/etimer.h" +/*---------------------------------------------------------------------------*/ + +/*#define freq I2C_SCL_NORMAL_BUS_SPEED */ +static struct etimer et; + +/* RE-Mote revision B, low-power PIC version */ +#define PM_EXPECTED_VERSION 0x20 + +/*---------------------------------------------------------------------------*/ +/* We are going to create three different processes, with its own printable + * name. Processes are a great way to run different applications and features + * in parallel + */ +PROCESS(test_VBAT_process, "Test VBAT process"); + +/* But we are only going to automatically start the first two */ +AUTOSTART_PROCESSES(&test_VBAT_process); +/*---------------------------------------------------------------------------*/ +PROCESS_THREAD(test_VBAT_process, ev, data) +{ + + static uint8_t aux = 0x00; + static uint16_t voltage = 0x00; + + PROCESS_BEGIN(); + + if(pm_enable() != PM_SUCCESS) { + printf("PM Failed \n"); + } else if(pm_enable() == PM_SUCCESS) { + printf("Process PM started\n"); + } + + if((pm_get_fw_ver(&aux) == PM_ERROR) || (aux != PM_EXPECTED_VERSION)) { + printf("PM: unexpected version 0x%02X\n", aux); + } + printf("PM: firmware version 0x%02X OK\n", aux); + + while(1) { + + etimer_set(&et, CLOCK_SECOND); + + PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et)); + + /*-------------------voltage VBAT --------------*/ + if(pm_get_voltage(&voltage) != PM_SUCCESS) { + printf("PM: error retrieving voltage\n"); + } else { + printf("%u.%u V\n", voltage / 100, voltage % 100); + } + } + PROCESS_END(); +}