|
@@ -0,0 +1,105 @@
|
|
1
|
+/*
|
|
2
|
+ * lipo.c
|
|
3
|
+ *
|
|
4
|
+ * https://github.com/raspberrypi/pico-examples/blob/master/adc/read_vsys/power_status.c
|
|
5
|
+ *
|
|
6
|
+ * Copyright (c) 2023 Thomas Buck (thomas@xythobuz.de)
|
|
7
|
+ *
|
|
8
|
+ * This program is free software: you can redistribute it and/or modify
|
|
9
|
+ * it under the terms of the GNU General Public License as published by
|
|
10
|
+ * the Free Software Foundation, either version 3 of the License, or
|
|
11
|
+ * (at your option) any later version.
|
|
12
|
+ *
|
|
13
|
+ * This program is distributed in the hope that it will be useful,
|
|
14
|
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
15
|
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
16
|
+ * GNU General Public License for more details.
|
|
17
|
+ *
|
|
18
|
+ * See <http://www.gnu.org/licenses/>.
|
|
19
|
+ */
|
|
20
|
+
|
|
21
|
+#include "stdbool.h"
|
|
22
|
+#include "hardware/adc.h"
|
|
23
|
+
|
|
24
|
+#include "config.h"
|
|
25
|
+#include "lipo.h"
|
|
26
|
+
|
|
27
|
+#if CYW43_USES_VSYS_PIN
|
|
28
|
+#include "pico/cyw43_arch.h"
|
|
29
|
+#endif
|
|
30
|
+
|
|
31
|
+#ifndef PICO_POWER_SAMPLE_COUNT
|
|
32
|
+#define PICO_POWER_SAMPLE_COUNT 3
|
|
33
|
+#endif
|
|
34
|
+
|
|
35
|
+// Pin used for ADC 0
|
|
36
|
+#define PICO_FIRST_ADC_PIN 26
|
|
37
|
+
|
|
38
|
+static const float full_battery = 4.1f;
|
|
39
|
+static const float empty_battery = 3.2f;
|
|
40
|
+
|
|
41
|
+bool lipo_charging(void) {
|
|
42
|
+#if defined CYW43_WL_GPIO_VBUS_PIN
|
|
43
|
+ return cyw43_arch_gpio_get(CYW43_WL_GPIO_VBUS_PIN);
|
|
44
|
+#elif defined PICO_VBUS_PIN
|
|
45
|
+ gpio_set_function(PICO_VBUS_PIN, GPIO_FUNC_SIO);
|
|
46
|
+ return gpio_get(PICO_VBUS_PIN);
|
|
47
|
+#else
|
|
48
|
+#error "No VBUS Pin available!"
|
|
49
|
+#endif
|
|
50
|
+}
|
|
51
|
+
|
|
52
|
+float lipo_voltage(void) {
|
|
53
|
+#ifndef PICO_VSYS_PIN
|
|
54
|
+#error "No VSYS Pin available!"
|
|
55
|
+#endif
|
|
56
|
+
|
|
57
|
+#if CYW43_USES_VSYS_PIN
|
|
58
|
+ cyw43_thread_enter();
|
|
59
|
+ // Make sure cyw43 is awake
|
|
60
|
+ cyw43_arch_gpio_get(CYW43_WL_GPIO_VBUS_PIN);
|
|
61
|
+#endif
|
|
62
|
+
|
|
63
|
+ // setup adc
|
|
64
|
+ adc_gpio_init(PICO_VSYS_PIN);
|
|
65
|
+ adc_select_input(PICO_VSYS_PIN - PICO_FIRST_ADC_PIN);
|
|
66
|
+
|
|
67
|
+ adc_fifo_setup(true, false, 0, false, false);
|
|
68
|
+ adc_run(true);
|
|
69
|
+
|
|
70
|
+ // We seem to read low values initially - this seems to fix it
|
|
71
|
+ int ignore_count = PICO_POWER_SAMPLE_COUNT;
|
|
72
|
+ while (!adc_fifo_is_empty() || ignore_count-- > 0) {
|
|
73
|
+ (void)adc_fifo_get_blocking();
|
|
74
|
+ }
|
|
75
|
+
|
|
76
|
+ // read vsys
|
|
77
|
+ uint32_t vsys = 0;
|
|
78
|
+ for(int i = 0; i < PICO_POWER_SAMPLE_COUNT; i++) {
|
|
79
|
+ uint16_t val = adc_fifo_get_blocking();
|
|
80
|
+ vsys += val;
|
|
81
|
+ }
|
|
82
|
+
|
|
83
|
+ adc_run(false);
|
|
84
|
+ adc_fifo_drain();
|
|
85
|
+
|
|
86
|
+ vsys /= PICO_POWER_SAMPLE_COUNT;
|
|
87
|
+
|
|
88
|
+#if CYW43_USES_VSYS_PIN
|
|
89
|
+ cyw43_thread_exit();
|
|
90
|
+#endif
|
|
91
|
+
|
|
92
|
+ // Generate voltage
|
|
93
|
+ const float conversion_factor = 3.3f / (1 << 12);
|
|
94
|
+ return vsys * 3 * conversion_factor;
|
|
95
|
+}
|
|
96
|
+
|
|
97
|
+float lipo_percentage(float voltage) {
|
|
98
|
+ float percentage = 100.0f * ((voltage - empty_battery) / (full_battery - empty_battery));
|
|
99
|
+ if (percentage >= 100.0f) {
|
|
100
|
+ percentage = 100.0f;
|
|
101
|
+ } else if (percentage < 0.0f) {
|
|
102
|
+ percentage = 0.0f;
|
|
103
|
+ }
|
|
104
|
+ return percentage;
|
|
105
|
+}
|