| Nordic Thingy:52 v2.0.0

m_batt_meas.h
1 /*
2 Copyright (c) 2010 - 2017, Nordic Semiconductor ASA
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without modification,
6 are permitted provided that the following conditions are met:
7
8 1. Redistributions of source code must retain the above copyright notice, this
9 list of conditions and the following disclaimer.
10
11 2. Redistributions in binary form, except as embedded into a Nordic
12 Semiconductor ASA integrated circuit in a product or a software update for
13 such product, must reproduce the above copyright notice, this list of
14 conditions and the following disclaimer in the documentation and/or other
15 materials provided with the distribution.
16
17 3. Neither the name of Nordic Semiconductor ASA nor the names of its
18 contributors may be used to endorse or promote products derived from this
19 software without specific prior written permission.
20
21 4. This software, with or without modification, must only be used with a
22 Nordic Semiconductor ASA integrated circuit.
23
24 5. Any software provided in binary form under this license must not be reverse
25 engineered, decompiled, modified and/or disassembled.
26
27 THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
28 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
29 OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
30 DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
31 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
33 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
36 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 */
38
48 #ifndef __M_BATT_MEAS_H__
49 #define __M_BATT_MEAS_H__
50
51 #include "m_ble.h"
52 #include "nrf_saadc.h"
53 #include <stdint.h>
54 #include <stdbool.h>
55
56 #define MEAS_INTERVAL_LOW_LIMIT_MS 50
57 /* The SAADC is configured for oversampling for improved noise immunity.
58 corresponding settings can be found in sdk_config.h */
59
62 enum
63 {
64 M_BATT_STATUS_CODE_SUCCESS,
65 M_BATT_STATUS_CODE_INVALID_PARAM,
66 };
67
70 typedef enum
71 {
72 M_BATT_MEAS_EVENT_DATA,
73 M_BATT_MEAS_EVENT_LOW,
74 M_BATT_MEAS_EVENT_FULL,
75 M_BATT_MEAS_EVENT_USB_CONN_CHARGING,
76 M_BATT_MEAS_EVENT_USB_CONN_CHARGING_FINISHED,
77 M_BATT_MEAS_EVENT_USB_DISCONN,
78 M_BATT_MEAS_EVENT_ERROR,
79 }m_batt_meas_event_type_t;
80
83 typedef struct
84 {
85 m_batt_meas_event_type_t type;
86 uint16_t voltage_mv;
87 uint8_t level_percent;
88 bool valid_voltage;
89 }m_batt_meas_event_t;
90
93 typedef void (*m_batt_meas_event_handler_t)(m_batt_meas_event_t const * p_event);
94
99 typedef struct
100 {
101 uint32_t r_1_ohm;
102 uint32_t r_2_ohm;
103 }voltage_divider_t;
104
107 typedef struct
108 {
109 uint16_t num_elements;
110 uint16_t first_element_mv;
111 uint8_t delta_mv;
112 uint8_t const * voltage_to_soc;
113 }state_of_charge_t;
114
115
118 typedef struct
119 {
120 uint16_t app_timer_prescaler;
121 uint8_t adc_pin_no;
122 nrf_saadc_input_t adc_pin_no_ain;
123 uint8_t usb_detect_pin_no;
124 uint8_t batt_chg_stat_pin_no;
125 bool batt_mon_en_pin_used;
126 uint8_t batt_mon_en_pin_no;
127 uint16_t batt_voltage_limit_low;
128 uint16_t batt_voltage_limit_full;
129 voltage_divider_t voltage_divider;
130 state_of_charge_t state_of_charge;
131 }batt_meas_param_t;
132
135 typedef struct
136 {
137 m_batt_meas_event_handler_t evt_handler;
138 batt_meas_param_t batt_meas_param;
139 }batt_meas_init_t;
140
151 uint32_t m_batt_meas_init(m_ble_service_handle_t * p_handle, batt_meas_init_t const * const p_batt_meas_init);
152
163 uint32_t m_batt_meas_enable(uint32_t meas_interval_ms);
164
170 uint32_t m_batt_meas_disable(void);
171
172 #endif
173
batt_meas_param_t::adc_pin_no
uint8_t adc_pin_no
Analog in pin connected to battery (with possible voltage divider).
Definition: m_batt_meas.h:121
batt_meas_param_t::voltage_divider
voltage_divider_t voltage_divider
Voltage divider type containing resistor information.
Definition: m_batt_meas.h:129
batt_meas_param_t::batt_voltage_limit_low
uint16_t batt_voltage_limit_low
Low voltage limit [milliVolts].
Definition: m_batt_meas.h:127
batt_meas_param_t::batt_voltage_limit_full
uint16_t batt_voltage_limit_full
Full voltage limit [milliVolts].
Definition: m_batt_meas.h:128
m_batt_meas_event_t::voltage_mv
uint16_t voltage_mv
Battery voltage given in millivolts.
Definition: m_batt_meas.h:86
m_batt_meas_enable
uint32_t m_batt_meas_enable(uint32_t meas_interval_ms)
Function for enabling battery measurement at the given interval.
M_BATT_STATUS_CODE_INVALID_PARAM
Invalid parameters.
Definition: m_batt_meas.h:65
batt_meas_init_t::batt_meas_param
batt_meas_param_t batt_meas_param
Input parameters.
Definition: m_batt_meas.h:138
batt_meas_param_t::app_timer_prescaler
uint16_t app_timer_prescaler
App timer prescaler. See APP_TIMER_PRESCALER.
Definition: m_batt_meas.h:120
m_batt_meas_event_type_t
m_batt_meas_event_type_t
Battery and charge event codes.
Definition: m_batt_meas.h:70
M_BATT_MEAS_EVENT_ERROR
Error state detected signalled by the charger (not implemeted, CHG and CHG finished will toggle in ca...
Definition: m_batt_meas.h:78
m_batt_meas_event_t::valid_voltage
bool valid_voltage
True if an event is generated by a ADC conversion.
Definition: m_batt_meas.h:88
M_BATT_MEAS_EVENT_LOW
Low battery event (<= set by user).
Definition: m_batt_meas.h:73
M_BATT_STATUS_CODE_SUCCESS
Successfull.
Definition: m_batt_meas.h:64
m_ble_service_handle_t
BLE service handle structure.
Definition: m_ble.h:83
m_batt_meas_event_handler_t
void(* m_batt_meas_event_handler_t)(m_batt_meas_event_t const *p_event)
m_batt sensor event handler type. Should be implemented by user e.g. in main.c()
Definition: m_batt_meas.h:93
M_BATT_MEAS_EVENT_USB_CONN_CHARGING_FINISHED
Battery charging finished/not charging.
Definition: m_batt_meas.h:76
m_batt_meas_event_t::level_percent
uint8_t level_percent
Remaining battery capacity percent.
Definition: m_batt_meas.h:87
M_BATT_MEAS_EVENT_FULL
Full battery event (>= value set by user).
Definition: m_batt_meas.h:74
state_of_charge_t::num_elements
uint16_t num_elements
Number of elements in the voltage to state of charge vector.
Definition: m_batt_meas.h:109
batt_meas_param_t::state_of_charge
state_of_charge_t state_of_charge
Information on mapping from voltage to state of charge.
Definition: m_batt_meas.h:130
state_of_charge_t
Struct for providing m_batt_meas with information on converting voltage to state of charge (Remaining...
Definition: m_batt_meas.h:107
m_batt_meas_event_t::type
m_batt_meas_event_type_t type
Given event type.
Definition: m_batt_meas.h:85
state_of_charge_t::first_element_mv
uint16_t first_element_mv
Voltage of the first element in the vector [mV];.
Definition: m_batt_meas.h:110
batt_meas_param_t::adc_pin_no_ain
nrf_saadc_input_t adc_pin_no_ain
Same as above, but given as nrf_saadc_input_t. These two values must correspond.
Definition: m_batt_meas.h:122
batt_meas_param_t
Input parameters for m_batt_meas.
Definition: m_batt_meas.h:118
M_BATT_MEAS_EVENT_USB_DISCONN
USB disconnected, battery not charging.
Definition: m_batt_meas.h:77
batt_meas_param_t::batt_mon_en_pin_used
bool batt_mon_en_pin_used
Indicates if a pin is used to enable battery monitoring. (E.g. activates a voltage divider)...
Definition: m_batt_meas.h:125
M_BATT_MEAS_EVENT_USB_CONN_CHARGING
Battery main og tricle charging active.
Definition: m_batt_meas.h:75
state_of_charge_t::voltage_to_soc
uint8_t const * voltage_to_soc
Pointer to vector mapping from voltage to state of charge.
Definition: m_batt_meas.h:112
batt_meas_init_t
Init parameters for m_batt_meas.
Definition: m_batt_meas.h:135
batt_meas_init_t::evt_handler
m_batt_meas_event_handler_t evt_handler
Function pointer to the event handler (executed in main context).
Definition: m_batt_meas.h:137
m_batt_meas_init
uint32_t m_batt_meas_init(m_ble_service_handle_t *p_handle, batt_meas_init_t const *const p_batt_meas_init)
Function for initializing the battery driver.
state_of_charge_t::delta_mv
uint8_t delta_mv
Distance in voltage between elements in the SoC vector [mV].
Definition: m_batt_meas.h:111
M_BATT_MEAS_EVENT_DATA
Is called back at the desired frequency.
Definition: m_batt_meas.h:72
voltage_divider_t
Struct for providing m_batt_meas with physical voltage divider information.
Definition: m_batt_meas.h:99
m_batt_meas_event_t
The struct passed to the handler with relevant battery information.
Definition: m_batt_meas.h:83
m_batt_meas_disable
uint32_t m_batt_meas_disable(void)
Function for stopping the battery measurement.
batt_meas_param_t::batt_mon_en_pin_no
uint8_t batt_mon_en_pin_no
Pin for enabling battery monitoring.
Definition: m_batt_meas.h:126
batt_meas_param_t::batt_chg_stat_pin_no
uint8_t batt_chg_stat_pin_no
Pin connected to "Charging status output pin" (CSO) of the battery charger.
Definition: m_batt_meas.h:124
batt_meas_param_t::usb_detect_pin_no
uint8_t usb_detect_pin_no
Pin = high when USB is connected.
Definition: m_batt_meas.h:123