Contiki-NG
batmon-sensor.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018, 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  * \addtogroup cc13xx-cc26xx-batmon
32  * @{
33  *
34  * \file
35  * Driver for the CC13xx/CC26xx AON battery monitor.
36  */
37 /*---------------------------------------------------------------------------*/
38 #include "contiki.h"
39 #include "lib/sensors.h"
40 
41 #include "batmon-sensor.h"
42 /*---------------------------------------------------------------------------*/
43 #include <ti/devices/DeviceFamily.h>
44 #include DeviceFamily_constructPath(driverlib/aon_batmon.h)
45 /*---------------------------------------------------------------------------*/
46 #include <stdint.h>
47 #include <stdio.h>
48 /*---------------------------------------------------------------------------*/
49 #define DEBUG 0
50 #if DEBUG
51 #define PRINTF(...) printf(__VA_ARGS__)
52 #else
53 #define PRINTF(...)
54 #endif
55 /*---------------------------------------------------------------------------*/
56 #define SENSOR_STATUS_DISABLED 0
57 #define SENSOR_STATUS_ENABLED 1
58 
59 static int enabled = SENSOR_STATUS_DISABLED;
60 /*---------------------------------------------------------------------------*/
61 /**
62  * \brief Returns a reading from the sensor
63  * \param type BATMON_SENSOR_TYPE_TEMP or BATMON_SENSOR_TYPE_VOLT
64  *
65  * \return The value as returned by the respective CC26xxware function
66  */
67 static int
68 value(int type)
69 {
70  if(enabled == SENSOR_STATUS_DISABLED) {
71  PRINTF("Sensor Disabled\n");
72  return BATMON_SENSOR_READING_ERROR;
73  }
74 
75  switch(type) {
76  case BATMON_SENSOR_TYPE_TEMP: return (int)AONBatMonTemperatureGetDegC();
77  case BATMON_SENSOR_TYPE_VOLT: return (int)AONBatMonBatteryVoltageGet();
78  default:
79  PRINTF("Invalid type\n");
80  return BATMON_SENSOR_READING_ERROR;
81  }
82 }
83 /*---------------------------------------------------------------------------*/
84 /**
85  * \brief Configuration function for the battery monitor sensor.
86  *
87  * \param type Activate, enable or disable the sensor. See below
88  * \param enable If
89  *
90  * When type == SENSORS_HW_INIT we turn on the hardware
91  * When type == SENSORS_ACTIVE and enable==1 we enable the sensor
92  * When type == SENSORS_ACTIVE and enable==0 we disable the sensor
93  */
94 static int
95 configure(int type, int enable)
96 {
97  switch(type) {
98  case SENSORS_HW_INIT:
99  AONBatMonEnable();
100  enabled = SENSOR_STATUS_ENABLED;
101  break;
102  case SENSORS_ACTIVE:
103  if(enable) {
104  AONBatMonEnable();
105  enabled = SENSOR_STATUS_ENABLED;
106  } else {
107  AONBatMonDisable();
108  enabled = SENSOR_STATUS_DISABLED;
109  }
110  break;
111  default:
112  break;
113  }
114  return enabled;
115 }
116 /*---------------------------------------------------------------------------*/
117 /**
118  * \brief Returns the status of the sensor
119  * \param type SENSORS_ACTIVE or SENSORS_READY
120  * \return 1 if the sensor is enabled
121  */
122 static int
123 status(int type)
124 {
125  switch(type) {
126  case SENSORS_ACTIVE:
127  case SENSORS_READY:
128  return enabled;
129  break;
130  default:
131  break;
132  }
133  return SENSOR_STATUS_DISABLED;
134 }
135 /*---------------------------------------------------------------------------*/
136 SENSORS_SENSOR(batmon_sensor, "Battery Monitor", value, configure, status);
137 /*---------------------------------------------------------------------------*/
138 /** @} */
static int value(int type)
Returns a reading from the sensor.
Definition: batmon-sensor.c:68
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:95