Tool Libs
UMIT-TIROL Institute of Automation and Control Engineering library collection
 
Loading...
Searching...
No Matches
adc.h
Go to the documentation of this file.
1
5#pragma once
6
7#include <utils/queue.h>
8
9#include "gpio.h"
10#include "dma.h"
11
12#if defined(ADC)
13// workaround for Legacy HAL defining 'ADC' as the common base between the
14// different ADCs. current HAL defines and uses it as 'ADC123_COMMON', so
15// we can safely discard it
16#undef ADC
17#endif
18
19namespace ADC {
21 struct Channel {
23 double get() {
24 return raw * lsb;
25 }
27 int16_t &raw;
28 const double lsb;
29 };
30
32 struct HW {
34 struct Conf {
35 ADC_TypeDef *adc;
36 uint32_t prescaler;
37 DMA_Stream_TypeDef *dmaStream;
38 uint32_t dmaChannel;
39 };
41 struct ChannelConf {
42 uint32_t channel;
43 uint32_t samplingTime;
44 double lsb;
46 };
47
49 HW(const Conf &conf)
50 : dma {{
51 .stream = conf.dmaStream,
52 .init = {
53 .Channel = conf.dmaChannel,
54 .MemInc = DMA_MINC_ENABLE,
55 .PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD,
56 .MemDataAlignment = DMA_MDATAALIGN_HALFWORD,
57 .Mode = DMA_CIRCULAR,
58 }
59 }}
60 {
61 handle.Instance = conf.adc;
62 handle.Init = {
63 .ClockPrescaler = conf.prescaler,
64 .ScanConvMode = ENABLE,
65 .ExternalTrigConv = ADC_SOFTWARE_START,
66 .DMAContinuousRequests = ENABLE,
67 };
68 }
69
71 void measure() {
72 while (HAL_ADC_Start_DMA(&handle, (uint32_t*)iBuffer, channelCnt) != HAL_OK);
73 }
74
78 void init() {
79 while (HAL_ADC_Init(&handle) != HAL_OK);
80 __HAL_LINKDMA(&handle, DMA_Handle, dma.handle);
81 }
82
85 auto ret = Channel{iBuffer[channelCnt], conf.lsb};
86
87 ADC_ChannelConfTypeDef sConfig {
88 .Channel = conf.channel,
89 .Rank = channelCnt + 1,
90 .SamplingTime = conf.samplingTime,
91 };
92 while (HAL_ADC_ConfigChannel(&handle, &sConfig) != HAL_OK);
93
94 channelCnt++;
95
96 return ret;
97 }
98
99 ADC_HandleTypeDef handle{};
100 int16_t iBuffer[16];
101 uint32_t &channelCnt = handle.Init.NbrOfConversion;
102 DMA::HW dma;
103 };
104}
Wrapper class for simple digital input/output pin.
Definition gpio.h:13
Copyright (c) 2023 IACE.
Copyright (c) 2020 IACE.
Copyright (c) 2023 IACE.
Class representing one measured pin/value.
Definition adc.h:21
double get()
return measured value
Definition adc.h:23
int16_t & raw
raw value from adc
Definition adc.h:27
Analog to Digital Converter Peripheral Driver.
Definition adc.h:32
DMA_Stream_TypeDef * dmaStream
DMA Stream for reading out raw values.
Definition adc.h:37
Channel get(ChannelConf conf)
register/create pin/channel for analog measurement
Definition adc.h:84
double lsb
Least Significant Bit value factor.
Definition adc.h:44
void init()
initialize driver.
Definition adc.h:78
uint32_t prescaler
Peripheral clock prescaler.
Definition adc.h:36
void measure()
start measuring all registered channels/pins
Definition adc.h:71
ADC_TypeDef * adc
ADC Peripheral.
Definition adc.h:35
DIO pin
Analog configured pin.
Definition adc.h:45
uint32_t samplingTime
sampling time
Definition adc.h:43
uint32_t channel
ADC Channel number [i.e. Pin].
Definition adc.h:42
uint32_t dmaChannel
DMA Channel.
Definition adc.h:38
HW(const Conf &conf)
create Driver with given Conf
Definition adc.h:49
channel configuration
Definition adc.h:41
driver configuration
Definition adc.h:34