Numworks Epsilon  1.4.1
Graphing Calculator Operating System
battery.cpp
Go to the documentation of this file.
1 #include <ion/battery.h>
2 #include "battery.h"
3 #include "regs/regs.h"
4 
5 /* To measure the battery voltage, we're using the internal ADC. The ADC works
6  * by comparing the input voltage to a reference voltage. The only fixed voltage
7  * we have around is 2.8V, so that's the one we're using as a refrence. However,
8  * and ADC can only measure voltage that is lower than the reference voltage. So
9  * we need to use a voltage divider before sampling Vbat.
10  * To avoid draining the battery, we're using an high-impedence voltage divider,
11  * so we need to be careful when sampling the ADC. See AN2834 for more info. */
12 
13 namespace Ion {
14 namespace Battery {
15 
16 bool isCharging() {
18 }
19 
21  if (voltage() < 3.2f) {
22  return Charge::EMPTY;
23  }
24  if (voltage() < 3.5f) {
25  return Charge::LOW;
26  }
27  if (voltage() < 3.8f) {
29  }
30  return Charge::FULL;
31 }
32 
33 float voltage() {
34  ADC.CR2()->setSWSTART(true);
35  while (ADC.SR()->getEOC() != true) {
36  }
37  uint16_t value = ADC.DR()->get();
38 
39  // The ADC is 12 bits by default
41 }
42 
43 }
44 }
45 
46 namespace Ion {
47 namespace Battery {
48 namespace Device {
49 
50 void init() {
51  initGPIO();
52 
53  /* The BAT_SNS pin is connected to Vbat through a divider bridge. It therefore
54  * has a voltage of Vbat/2. We'll measure this using ADC channel 0. */
56 
57  // Step 2 - Enable the ADC
58  RCC.APB2ENR()->setADC1EN(true);
59  ADC.CR2()->setADON(true);
60 
61  // Configure the ADC channel
62  ADC.SQR1()->setL(0); // Always sample the same channel
63  ADC.SQR3()->setSQ1(ADCChannel);
64  ADC.SMPR()->setSamplingTime(ADCChannel, ADC::SMPR::SamplingTime::Cycles480); // Use the max sampling time
65 }
66 
67 void initGPIO() {
68  /* Step 1 - Configure the GPIOs
69  * The BAT_CHRG pin is connected to the Li-Po charging IC. That pin uses an
70  * open-drain output. Open-drain output are either connected to ground or left
71  * floating. To interact with such an output, our input must therefore be
72  * pulled up. */
75 }
76 
77 void shutdown() {
80 
81  // Disable the ADC
82  ADC.CR2()->setADON(false);
83  RCC.APB2ENR()->setADC1EN(false);
84 }
85 
86 }
87 }
88 }
Charge level()
Definition: battery.cpp:20
constexpr float ADCDividerBridgeRatio
Definition: battery.h:29
bool isCharging()
Definition: battery.cpp:16
constexpr uint8_t ChargingPin
Definition: battery.h:22
bool get(int index) volatile
Definition: gpio.h:44
unsigned short uint16_t
Definition: stdint.h:5
Definition: adc.h:6
Definition: rcc.h:6
T get() volatile
Definition: register.h:18
Definition: adc.h:17
Definition: adc.h:55
constexpr uint8_t ADCPin
Definition: battery.h:25
constexpr GPIO ChargingGPIO
Definition: battery.h:21
Definition: adc.h:8
constexpr float ADCReferenceVoltage
Definition: battery.h:28
Definition: adc.h:45
Definition: adc.h:50
constexpr GPIO ADCGPIO
Definition: battery.h:24
void setPull(int index, Pull pull) volatile
Definition: gpio.h:39
float voltage()
Definition: battery.cpp:33
Definition: adc.h:23
constexpr uint8_t ADCChannel
Definition: battery.h:26
Definition: gpio.h:42
void setMode(int index, Mode mode) volatile
Definition: gpio.h:17
void setSamplingTime(int channel, SamplingTime t) volatile
Definition: adc.h:40
Definition: backlight.h:6