Files
Embedded-Hacking/drivers/0x06_adc/adc.c
T
Kevin Thomas e54c756423 refactor: enforce max 8 code lines, add docstrings, fix warnings across all Rust and C SDK projects
Rust (all 15 projects):
- Refactored overlength functions: format_counter, format_u8, format_f32_1,
  format_u32_minimal, gpio_drive, read_sensor, poll_sensor, format_round_trip,
  format_u32, prepare_write_buf, write_min_digits, write_temp, UartDriver::init,
  init_spi, angle_to_pulse_us, compute_servo_level
- Added 200+ docstrings to test functions, mock structs, impl blocks
- Fixed pub static comments (//) to doc comments (///) in all main.rs files
- Fixed helper function ordering (helpers above callers)
- Fixed Fn(u32) -> FnMut(u32) bound in button poll_button
- Moved OneShot trait import from main.rs to board.rs in adc project
- Added unsafe {} blocks in flash unsafe fn bodies (Rust 2024 edition)
- Removed unused hal::Clock imports from pwm/servo main.rs
- All 15 projects build with zero errors and zero warnings

C Pico SDK (all 15 projects):
- Added docstrings to all public functions, macros, and static variables
- All 15 projects rebuilt with zero errors

Cleanup:
- Removed build/ and target/ directories from git tracking
- Added target/ to .gitignore
- Deleted temporary fix_rust_docs.py script
2026-04-06 08:33:17 -04:00

102 lines
3.2 KiB
C

/**
* @file adc.c
* @brief Implementation of the 12-bit ADC driver
* @author Kevin Thomas
* @date 2025
*
* MIT License
*
* Copyright (c) 2025 Kevin Thomas
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "adc.h"
#include "pico/stdlib.h"
#include "hardware/adc.h"
/** @brief ADC reference voltage in millivolts */
#define ADC_VREF_MV 3300
/** @brief Maximum 12-bit ADC raw value */
#define ADC_FULL_SCALE 4095
/** @brief Currently selected ADC input channel */
static uint8_t active_channel = 0;
/**
* @brief Convert a raw 12-bit ADC value to millivolts
*
* Scales the raw value linearly against the 3.3 V reference.
*
* @param raw 12-bit ADC conversion result (0 - 4095)
* @return uint32_t Equivalent voltage in millivolts (0 - 3300)
*/
static uint32_t _raw_to_mv(uint16_t raw) {
return (uint32_t)raw * ADC_VREF_MV / ADC_FULL_SCALE;
}
/**
* @brief Convert a raw temperature-sensor ADC value to degrees Celsius
*
* Applies the RP2350 datasheet formula:
* T = 27 - (V - 0.706) / 0.001721
*
* @param raw 12-bit ADC result from the internal temperature sensor (channel 4)
* @return float Die temperature in degrees Celsius
*/
static float _raw_to_celsius(uint16_t raw) {
float voltage = (float)raw * 3.3f / (float)ADC_FULL_SCALE;
return 27.0f - (voltage - 0.706f) / 0.001721f;
}
/**
* @brief Initialize the ADC peripheral and configure an analog GPIO pin
*
* @param gpio GPIO pin number for the analog input
* @param channel ADC channel number corresponding to the GPIO
*/
void adc_driver_init(uint32_t gpio, uint8_t channel) {
active_channel = channel;
adc_init();
adc_gpio_init(gpio);
adc_set_temp_sensor_enabled(true);
adc_select_input(channel);
}
/**
* @brief Perform a single ADC conversion and return millivolts
*
* @return uint32_t Measured voltage in millivolts (0 to 3300)
*/
uint32_t adc_driver_read_mv(void) {
return _raw_to_mv(adc_read());
}
/**
* @brief Read the on-chip temperature sensor in degrees Celsius
*
* @return float Die temperature in degrees Celsius
*/
float adc_driver_read_temp_celsius(void) {
adc_select_input(4);
float result = _raw_to_celsius(adc_read());
adc_select_input(active_channel);
return result;
}