Contiki-NG
batmon-sensor.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2014, Texas Instruments Incorporated - http://www.ti.com/
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the copyright holder nor the names of its
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28 * OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30/*---------------------------------------------------------------------------*/
31/**
32 * \addtogroup cc26xx-batmon
33 * @{
34 *
35 * \file
36 * Driver for the CC13xx/CC26xx AON battery monitor
37 */
38/*---------------------------------------------------------------------------*/
39#include "contiki.h"
40#include "lib/sensors.h"
41#include "batmon-sensor.h"
42
43#include "ti-lib.h"
44
45#include <stdint.h>
46#include <stdio.h>
47/*---------------------------------------------------------------------------*/
48#define DEBUG 0
49#if DEBUG
50#define PRINTF(...) printf(__VA_ARGS__)
51#else
52#define PRINTF(...)
53#endif
54/*---------------------------------------------------------------------------*/
55#define SENSOR_STATUS_DISABLED 0
56#define SENSOR_STATUS_ENABLED 1
57
58static int enabled = SENSOR_STATUS_DISABLED;
59/*---------------------------------------------------------------------------*/
60/**
61 * \brief Returns a reading from the sensor
62 * \param type BATMON_SENSOR_TYPE_TEMP or BATMON_SENSOR_TYPE_VOLT
63 *
64 * \return The value as returned by the respective CC26xxware function
65 */
66static int
67value(int type)
68{
69 if(enabled == SENSOR_STATUS_DISABLED) {
70 PRINTF("Sensor Disabled\n");
71 return 0;
72 }
73
74 if(type == BATMON_SENSOR_TYPE_TEMP) {
75 return (int)ti_lib_aon_batmon_temperature_get_deg_c();
76 } else if(type == BATMON_SENSOR_TYPE_VOLT) {
77 return (int)ti_lib_aon_batmon_battery_voltage_get();
78 } else {
79 PRINTF("Invalid type\n");
80 }
81
82 return 0;
83}
84/*---------------------------------------------------------------------------*/
85/**
86 * \brief Configuration function for the battery monitor sensor.
87 *
88 * \param type Activate, enable or disable the sensor. See below
89 * \param enable If
90 *
91 * When type == SENSORS_HW_INIT we turn on the hardware
92 * When type == SENSORS_ACTIVE and enable==1 we enable the sensor
93 * When type == SENSORS_ACTIVE and enable==0 we disable the sensor
94 */
95static int
96configure(int type, int enable)
97{
98 switch(type) {
99 case SENSORS_HW_INIT:
100 ti_lib_aon_batmon_enable();
101 enabled = SENSOR_STATUS_ENABLED;
102 break;
103 case SENSORS_ACTIVE:
104 if(enable) {
105 ti_lib_aon_batmon_enable();
106 enabled = SENSOR_STATUS_ENABLED;
107 } else {
108 ti_lib_aon_batmon_disable();
109 enabled = SENSOR_STATUS_DISABLED;
110 }
111 break;
112 default:
113 break;
114 }
115 return enabled;
116}
117/*---------------------------------------------------------------------------*/
118/**
119 * \brief Returns the status of the sensor
120 * \param type SENSORS_ACTIVE or SENSORS_READY
121 * \return 1 if the sensor is enabled
122 */
123static int
124status(int type)
125{
126 switch(type) {
127 case SENSORS_ACTIVE:
128 case SENSORS_READY:
129 return enabled;
130 break;
131 default:
132 break;
133 }
134 return SENSOR_STATUS_DISABLED;
135}
136/*---------------------------------------------------------------------------*/
137SENSORS_SENSOR(batmon_sensor, "Battery Monitor", value, configure, status);
138/*---------------------------------------------------------------------------*/
139/** @} */
static int value(int type)
Returns a reading from the sensor.
Definition: batmon-sensor.c:67
static int status(int type)
Returns the status of the sensor.
static int configure(int type, int enable)
Configuration function for the battery monitor sensor.
Definition: batmon-sensor.c:96
Header file with macros which rename TI CC26xxware functions.