microHAL
An abstraction layer for your future F4xx projects
Loading...
Searching...
No Matches
adc.h
Go to the documentation of this file.
1
16#ifndef ADC_H
17#define ADC_H
18
19/* -- Includes -- */
20#include <stdint.h>
21#include "stm32f4xx.h"
22#include "defines.h"
23
24/* -- Structs -- */
28struct __attribute__((packed)) ADCRegs {
29 REG32 SR;
30 REG32 CR1;
31 REG32 CR2;
32 REG32 SMPR[2];
33 REG32 JOFR[4];
34 REG32 HTR;
35 REG32 LTR;
36 REG32 SQR[3];
37 REG32 JSQR;
38 REG32 JDR[4];
39 REG32 DR;
40};
41
42_Static_assert((sizeof(struct ADCRegs)) == (sizeof(uint32_t) * 20U),
43 "ADC register struct size mismatch. Is it aligned?");
44
48struct __attribute__((packed)) ADCCommonRegs {
49 REG32 CSR;
50 REG32 CCR;
51 REG32 CDR;
52};
53
54_Static_assert((sizeof(struct ADCCommonRegs)) == (sizeof(uint32_t) * 3U),
55 "ADC Common register struct size mismatch. Is it aligned?");
56
57#ifndef UTEST
58#define ADC_(NUM) (struct ADCRegs *)(ADC1_BASE + (0x100UL * ((uint8_t)NUM)))
59#define ADC_COMMON (struct ADCCommonRegs *)(ADC123_COMMON_BASE)
60#else
61extern struct ADCRegs *ADC_(const uint8_t number);
62extern struct ADCCommonRegs *ADC_COMMON;
63#endif
64
68struct __attribute__((packed)) ADCModes {
69 volatile _Bool DMA : 1;
70 volatile _Bool DDS : 1;
71 volatile _Bool CONT : 1;
72 volatile _Bool DISC : 1; // One stop per conversion
73 volatile _Bool SCAN : 1;
74};
75
76_Static_assert((sizeof(struct ADCModes)) == (sizeof(uint8_t) * 1U),
77 "ADC Configuration struct size mismatch. Is it aligned?");
78
79/* -- Enums -- */
83typedef enum adc_peripheral {
84#ifdef ADC1_BASE
85 ADC_PERIPH_1 = 0x00,
86#endif
87#ifdef ADC2_BASE
88 ADC_PERIPH_2 = 0x01,
89#endif
90#ifdef ADC3_BASE
91 ADC_PERIPH_3 = 0x02,
92#endif
93 ADC_PERIPH_LEN
95
99typedef enum adc_res {
100 ADC_RES_B12 = 0x00, // >= 15 ADCCLK cycles
101 ADC_RES_B10 = 0x01, // >= 13 ADCCLK cycles
102 ADC_RES_B08 = 0x02, // >= 11 ADCCLK cycles
103 ADC_RES_B06 = 0x03 // >= 9 ADCCLK cycles
105
109typedef enum adc_trigger {
110 ADC_TRIGGER_NONE = 0x00,
111 ADC_TRIGGER_RISE = 0x01,
112 ADC_TRIGGER_FALL = 0x02,
113 ADC_TRIGGER_BOTH = 0x03
115
119typedef enum adc_samplerate {
120 ADC_SAMPLERATE_C003 = 0x00,
121 ADC_SAMPLERATE_C015 = 0x01,
122 ADC_SAMPLERATE_C028 = 0x02,
123 ADC_SAMPLERATE_C056 = 0x03,
124 ADC_SAMPLERATE_C084 = 0x04,
125 ADC_SAMPLERATE_C112 = 0x05,
126 ADC_SAMPLERATE_C144 = 0x06,
127 ADC_SAMPLERATE_C480 = 0x07
129
133typedef enum adc_prescaler {
134 ADC_PRESCALER_DIV2 = 0x00,
135 ADC_PRESCALER_DIV4 = 0x01,
136 ADC_PRESCALER_DIV6 = 0x02,
137 ADC_PRESCALER_DIV8 = 0x03,
139
149void adc_set_prescaler(const adc_prescaler_t value);
150
162void adc_set_resolution(const adc_peripheral_t adc, const adc_res_t value);
163
176void adc_set_samplerate(const adc_peripheral_t adc, const uint8_t channel,
177 const adc_samplerate_t value);
178
189void adc_set_modes(const adc_peripheral_t adc, const struct ADCModes config);
190
202void adc_set_seq(const adc_peripheral_t adc, const uint8_t *seq,
203 const uint8_t count);
204
215void adc_on(const adc_peripheral_t adc);
216
226void adc_off(const adc_peripheral_t adc);
227
234uint16_t adc_read(const adc_peripheral_t adc);
235
236#endif
adc_prescaler
Available ADC prescaler dividers.
Definition adc.h:133
enum adc_samplerate adc_samplerate_t
Available ADC samplerates (in cycles)
adc_res
Available ADC resolutions.
Definition adc.h:99
void adc_set_resolution(const adc_peripheral_t adc, const adc_res_t value)
Sets the ADC resolution to the specified value.
Definition adc.c:44
adc_peripheral
Available ADC peripherals.
Definition adc.h:83
enum adc_prescaler adc_prescaler_t
Available ADC prescaler dividers.
enum adc_trigger adc_trigger_t
Available ADC trigger modes.
uint16_t adc_read(const adc_peripheral_t adc)
Reads the last ADC conversion result.
Definition adc.c:183
enum adc_res adc_res_t
Available ADC resolutions.
void adc_set_prescaler(const adc_prescaler_t value)
Sets the ADC Prescaler divider to the specified value.
Definition adc.c:23
void adc_set_seq(const adc_peripheral_t adc, const uint8_t *seq, const uint8_t count)
Sets the ADC conversion sequence to the specified order.
Definition adc.c:126
enum adc_peripheral adc_peripheral_t
Available ADC peripherals.
void adc_off(const adc_peripheral_t adc)
Stops the ADC conversion.
Definition adc.c:172
adc_trigger
Available ADC trigger modes.
Definition adc.h:109
void adc_set_samplerate(const adc_peripheral_t adc, const uint8_t channel, const adc_samplerate_t value)
Sets the ADC sampling rate to the specified value.
Definition adc.c:69
void adc_set_modes(const adc_peripheral_t adc, const struct ADCModes config)
Sets the ADC modes according to the configuration.
Definition adc.c:102
void adc_on(const adc_peripheral_t adc)
Starts the ADC conversion.
Definition adc.c:153
adc_samplerate
Available ADC samplerates (in cycles)
Definition adc.h:119
Defines used commonly in most files.
CMSIS device header stubs.
Contains ADC common registers.
Definition adc.h:48
Contains ADC conversion modes.
Definition adc.h:68
Contains ADC registers.
Definition adc.h:28