Select one of the symbols to view example projects that use it.
 
Outline
#include "stdbool.h"
#include "hardware/adc.h"
#include "power_status.h"
#include "pico/cyw43_arch.h"
#define PICO_POWER_SAMPLE_COUNT
#define PICO_FIRST_ADC_PIN
power_source(bool *)
power_voltage(float *)
Files
loading...
SourceVuRaspberry Pi Pico SDK and Examplesread_vsys samplepower_status.c
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/** * Copyright (c) 2023 Raspberry Pi (Trading) Ltd. * * SPDX-License-Identifier: BSD-3-Clause *//* ... */ #include "stdbool.h" #include "hardware/adc.h" #include "power_status.h" #if CYW43_USES_VSYS_PIN #include "pico/cyw43_arch.h" #endif #ifndef PICO_POWER_SAMPLE_COUNT #define PICO_POWER_SAMPLE_COUNT 3 #endif // Pin used for ADC 0 #define PICO_FIRST_ADC_PIN 26 int power_source(bool *battery_powered) { #if defined CYW43_WL_GPIO_VBUS_PIN *battery_powered = !cyw43_arch_gpio_get(CYW43_WL_GPIO_VBUS_PIN); return PICO_OK;/* ... */ #elif defined PICO_VBUS_PIN gpio_set_function(PICO_VBUS_PIN, GPIO_FUNC_SIO); *battery_powered = !gpio_get(PICO_VBUS_PIN); return PICO_OK;/* ... */ #else return PICO_ERROR_NO_DATA; #endif }{ ... } int power_voltage(float *voltage_result) { #ifndef PICO_VSYS_PIN return PICO_ERROR_NO_DATA; #else #if CYW43_USES_VSYS_PIN cyw43_thread_enter(); // Make sure cyw43 is awake cyw43_arch_gpio_get(CYW43_WL_GPIO_VBUS_PIN);/* ... */ #endif // setup adc adc_gpio_init(PICO_VSYS_PIN); adc_select_input(PICO_VSYS_PIN - PICO_FIRST_ADC_PIN); adc_fifo_setup(true, false, 0, false, false); adc_run(true); // We seem to read low values initially - this seems to fix it int ignore_count = PICO_POWER_SAMPLE_COUNT; while (!adc_fifo_is_empty() || ignore_count-- > 0) { (void)adc_fifo_get_blocking(); }while (!adc_fifo_is_empty() || ignore_count-- > 0) { ... } // read vsys uint32_t vsys = 0; for(int i = 0; i < PICO_POWER_SAMPLE_COUNT; i++) { uint16_t val = adc_fifo_get_blocking(); vsys += val; }for (int i = 0; i < PICO_POWER_SAMPLE_COUNT; i++) { ... } adc_run(false); adc_fifo_drain(); vsys /= PICO_POWER_SAMPLE_COUNT; #if CYW43_USES_VSYS_PIN cyw43_thread_exit(); #endif // Generate voltage const float conversion_factor = 3.3f / (1 << 12); *voltage_result = vsys * 3 * conversion_factor; return PICO_OK;/* ... */ #endif }{ ... }
Details