PER Firmware
Loading...
Searching...
No Matches
adc.h
Go to the documentation of this file.
1
9#ifndef __PHAL_G4_ADC_H__
10#define __PHAL_G4_ADC_H__
11
12#include "common/phal_G4/phal_G4.h"
13
14typedef enum {
15 ADC_RES_12_BIT = 0b00,
16 ADC_RES_10_BIT = 0b01,
17 ADC_RES_8_BIT = 0b10,
18 ADC_RES_6_BIT = 0b11
19} ADCResolution_t;
20
21typedef enum {
22 ADC_CLK_PRESC_0 = 0b0000,
23 ADC_CLK_PRESC_2 = 0b0001,
24 ADC_CLK_PRESC_4 = 0b0010,
25 ADC_CLK_PRESC_6 = 0b0011,
26 ADC_CLK_PRESC_8 = 0b0100,
27} ADCClkPrescaler_t;
28
29typedef enum {
30 ADC_DMA_OFF = 0b00,
31 ADC_DMA_ONESHOT = 0b01,
32 ADC_DMA_CIRCULAR = 0b11
33} ADCDMAMode_t;
34
35typedef enum {
36 ADC_DATA_ALIGN_RIGHT = 0b0,
37 ADC_DATA_ALIGN_LEFT = 0b1
38} ADCDataAlign_t;
39
40typedef enum {
41 ADC_OVERSAMPLE_NONE = 0,
42 ADC_OVERSAMPLE_2 = 2,
43 ADC_OVERSAMPLE_4 = 4,
44 ADC_OVERSAMPLE_8 = 8,
45 ADC_OVERSAMPLE_16 = 16,
46 ADC_OVERSAMPLE_32 = 32,
47 ADC_OVERSAMPLE_64 = 64,
48 ADC_OVERSAMPLE_128 = 128,
49 ADC_OVERSAMPLE_256 = 256,
50} ADCOversampleCount_t;
51
52typedef struct {
53 ADCClkPrescaler_t prescaler;
54 ADCResolution_t resolution;
55 ADCDataAlign_t data_align;
56 bool cont_conv_mode;
57 ADCOversampleCount_t oversample;
58 ADCDMAMode_t dma_mode;
59 ADC_TypeDef* periph;
61
62typedef enum {
63 ADC_CHN_SMP_CYCLES_3 = 0b000,
64 ADC_CHN_SMP_CYCLES_15 = 0b001,
65 ADC_CHN_SMP_CYCLES_28 = 0b010,
66 ADC_CHN_SMP_CYCLES_56 = 0b011,
67 ADC_CHN_SMP_CYCLES_84 = 0b100,
68 ADC_CHN_SMP_CYCLES_112 = 0b101,
69 ADC_CHN_SMP_CYCLES_144 = 0b110,
70 ADC_CHN_SMP_CYCLES_480 = 0b111,
71} ADCChannelSampleCycles_t;
72
73typedef enum {
74 ADC_CHANNEL_1 = 1,
75 ADC_CHANNEL_2 = 2,
76 ADC_CHANNEL_3 = 3,
77 ADC_CHANNEL_4 = 4,
78} ADCChannel_t;
79
80typedef struct {
81 ADCChannel_t channel; // not the GPIO channel, use the ADC channel
82 uint32_t rank; // order at which the channels will be polled, starting at 0
83 ADCChannelSampleCycles_t sampling_time;
85
86#define ADC1_CH1_GPIO_Port (GPIOA)
87#define ADC1_CH1_Pin (0)
88#define ADC1_CH2_GPIO_Port (GPIOA)
89#define ADC1_CH2_Pin (1)
90#define ADC1_CH3_GPIO_Port (GPIOA)
91#define ADC1_CH3_Pin (2)
92#define ADC1_CH4_GPIO_Port (GPIOA)
93#define ADC1_CH4_Pin (3)
94
103bool PHAL_initADC(ADCInitConfig_t* config, ADCChannelConfig_t channels[], uint8_t num_channels);
104
110bool PHAL_startADC(ADCInitConfig_t* config);
111
117bool PHAL_stopADC(ADCInitConfig_t* config);
118
125uint16_t PHAL_readADC(ADCInitConfig_t* config);
126
127// TODO ADC3 config (ADC2 doesn't support DMA)
128#define ADC1_DMA_CONT_CONFIG(mem_addr_, tx_size_, priority_) \
129 {.periph_addr = (uint32_t)&(ADC1->DR), \
130 .mem_addr = mem_addr_, \
131 .tx_size = tx_size_, \
132 .increment = true, \
133 .circular = true, \
134 .dir = 0b0, \
135 .mem_inc = true, \
136 .periph_inc = false, \
137 .mem_to_mem = false, \
138 .priority = priority_, \
139 .mem_size = DMA_SIZE_16BIT, \
140 .periph_size = DMA_SIZE_16BIT, \
141 .tx_isr_en = false, \
142 .dma_chan_request = 0b0000, \
143 .channel_idx = 1, \
144 .mux_request = DMA_REQUEST_ADC1, \
145 .periph = DMA1, \
146 .channel = DMA1_Channel1}
147
148#endif // __PHAL_G4_ADC_H__
bool PHAL_startADC(ADC_TypeDef *adc)
Starts the ADC conversions, requires PHAL_initADC to be called prior.
Definition adc.c:82
uint16_t PHAL_readADC(ADC_TypeDef *adc)
Reads the ADC data register.
Definition adc.c:92
bool PHAL_stopADC(ADC_TypeDef *adc)
Stops the ADC conversions, requires PHAL_initADC to be called prior.
Definition adc.c:87
bool PHAL_initADC(ADC_TypeDef *adc, ADCInitConfig_t *config, ADCChannelConfig_t channels[], uint8_t num_channels)
Initializes the ADC, requires GPIO config prior.
Definition adc.c:11
Definition adc.h:59
Definition adc.h:39