Sound Byte Libs 0.5.1-340-gab5b79a
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 * @code
14 * // Initialize the ADC peripheral (returns void or bool)
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 * @endcode
33 *
34 * ## Convenience Helpers
35 *
36 * The core library provides convenience functions that compose the
37 * low-level primitives:
38 *
39 * @code
40 * // Single blocking read
41 * uint16_t value = sbl::adc::read<sbl::driver::Adc>(handle);
42 * @endcode
43 *
44 * Usage in driver:
45 * @code
46 * #include <sbl/hw/hal/adc/driver.hpp>
47 * using sbl::adc::SampleTime;
48 * @endcode
49 */
50
51#ifndef SBL_HW_HAL_ADC_DRIVER_HPP_
52#define SBL_HW_HAL_ADC_DRIVER_HPP_
53
54#include "handle.hpp"
55#include "types.hpp"
56
57namespace sbl {
58
59namespace adc {
62
63 /**
64 * @brief Blocking single-channel ADC read
65 *
66 * Convenience function that composes low-level driver primitives.
67 * Configures the channel, starts conversion, waits for completion,
68 * and returns the raw value.
69 *
70 * @tparam Driver ADC driver type (e.g., sbl::driver::Adc)
71 * @param handle ADC channel handle from hardware config
72 * @param sample_time Sampling duration (default: Medium)
73 * @return Raw ADC value (resolution depends on driver)
74 *
75 * @context Not ISR-safe: polls for conversion complete (busy-wait loop)
76 *
77 * Example:
78 * @code
79 * auto value = sbl::adc::read<sbl::driver::Adc>(sbl::hw::adc::knob1);
80 * @endcode
81 */
82template<typename Driver>
83inline uint16_t read(const AdcHandle& handle, SampleTime sample_time = SampleTime::Medium) {
84 Driver::configure_channel(handle, sample_time);
85 Driver::start_conversion(handle);
86 while (!Driver::is_conversion_complete()) {
87 // Busy wait - suitable for polling mode
88 }
89 return Driver::read_raw();
90}
91
92 /**
93 * @brief Convert raw ADC value to voltage (millivolts)
94 *
95 * @tparam Driver ADC driver type for resolution info
96 * @param raw_value Raw ADC reading
97 * @param vref_mv Reference voltage in millivolts (default: 3300mV)
98 * @return Voltage in millivolts
99 *
100 * @context ISR-safe: pure arithmetic, no hardware access
101 */
102template<typename Driver>
103inline uint32_t to_millivolts(uint16_t raw_value, uint32_t vref_mv = 3300) {
104 constexpr uint32_t MAX_VALUE = (1u << Driver::resolution_bits()) - 1;
105 return (static_cast<uint32_t>(raw_value) * vref_mv) / MAX_VALUE;
106}
107
108} // namespace adc
109
110} // namespace sbl
111
112#endif // SBL_HW_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:103
uint16_t read(const AdcHandle &handle, SampleTime sample_time=SampleTime::Medium)
Blocking single-channel ADC read.
Definition driver.hpp:83
SampleTime
ADC sample time configuration.
Definition types.hpp:32
Resolution
ADC resolution configuration.
Definition types.hpp:44
Root namespace for all Sound Byte Libs code.
MIDI message types.
ADC channel handle.
Definition handle.hpp:26
UART handle type for hardware abstraction.