Sound Byte Libs 1ee2ca6
C++ firmware library for audio applications on 32-bit ARM Cortex-M processors
Loading...
Searching...
No Matches
driver.hpp
Go to the documentation of this file.
1/**
2 * @file driver.hpp
3 * @brief ADC driver interface - canonical types for MCU driver implementations
4 * @ingroup hal
5 *
6 * This header provides everything an ADC driver needs:
7 * - AdcHandle: Channel reference with peripheral and channel number
8 * - SampleTime: Sampling duration configuration
9 *
10 * ## Low-Level Driver Interface
11 *
12 * Drivers implement the following static interface:
13 *
14 * // Initialize the ADC peripheral
15 * static void init();
16 *
17 * // Configure channel for conversion
18 * static void configure_channel(const sbl::AdcHandle& handle,
19 * sbl::adc::SampleTime sample_time);
20 *
21 * // Start single conversion
22 * static void start_conversion(const sbl::AdcHandle& handle);
23 *
24 * // Check if conversion is complete
25 * static bool is_conversion_complete();
26 *
27 * // Read raw conversion result
28 * static uint16_t read_raw();
29 *
30 * // Get ADC resolution (compile-time constant)
31 * static constexpr uint8_t resolution_bits();
32 *
33 * ## Convenience Helpers
34 *
35 * The core library provides convenience functions that compose the
36 * low-level primitives:
37 *
38 * // Single blocking read
39 * uint16_t value = sbl::adc::read<sbl::driver::Adc>(handle);
40 *
41 * Usage in driver:
42 * #include <sbl/hal/adc/driver.hpp>
43 * using sbl::adc::SampleTime;
44 */
45
46#ifndef SBL_HAL_ADC_DRIVER_HPP_
47#define SBL_HAL_ADC_DRIVER_HPP_
48
49#include "handle.hpp"
50#include "types.hpp"
51
52namespace sbl {
53
54// Convenience namespace alias for drivers
55// Maps sbl::core::hal::adc -> sbl::adc for cleaner code
56namespace adc {
59
60 /**
61 * @brief Blocking single-channel ADC read
62 *
63 * Convenience function that composes low-level driver primitives.
64 * Configures the channel, starts conversion, waits for completion,
65 * and returns the raw value.
66 *
67 * @tparam Driver ADC driver type (e.g., sbl::driver::Adc)
68 * @param handle ADC channel handle from hardware config
69 * @param sample_time Sampling duration (default: Medium)
70 * @return Raw ADC value (resolution depends on driver)
71 *
72 * Example:
73 * auto value = sbl::adc::read<sbl::driver::Adc>(sbl::hw::adc::knob1);
74 */
75 template<typename Driver>
76 inline uint16_t read(const AdcHandle& handle,
77 SampleTime sample_time = SampleTime::Medium) {
78 Driver::configure_channel(handle, sample_time);
79 Driver::start_conversion(handle);
80 while (!Driver::is_conversion_complete()) {
81 // Busy wait - suitable for polling mode
82 }
83 return Driver::read_raw();
84 }
85
86 /**
87 * @brief Convert raw ADC value to voltage (millivolts)
88 *
89 * @tparam Driver ADC driver type for resolution info
90 * @param raw_value Raw ADC reading
91 * @param vref_mv Reference voltage in millivolts (default: 3300mV)
92 * @return Voltage in millivolts
93 */
94 template<typename Driver>
95 inline uint32_t to_millivolts(uint16_t raw_value, uint32_t vref_mv = 3300) {
96 constexpr uint32_t max_value = (1u << Driver::resolution_bits()) - 1;
97 return (static_cast<uint32_t>(raw_value) * vref_mv) / max_value;
98 }
99
100} // namespace adc
101
102} // namespace sbl
103
104#endif // SBL_HAL_ADC_DRIVER_HPP_
uint32_t to_millivolts(uint16_t raw_value, uint32_t vref_mv=3300)
Convert raw ADC value to voltage (millivolts)
Definition driver.hpp:95
uint16_t read(const AdcHandle &handle, SampleTime sample_time=SampleTime::Medium)
Blocking single-channel ADC read.
Definition driver.hpp:76
Resolution
ADC resolution configuration.
Definition types.hpp:43
SampleTime
ADC sample time configuration.
Definition types.hpp:31
Root namespace for all Sound Byte Libs functionality.
Definition aliases.hpp:24
ADC channel handle.
Definition handle.hpp:29
Common types for SBL hardware abstraction.
UART handle type for hardware abstraction.