MAX32680 Peripheral Driver API
Peripheral Driver API for the MAX32680
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Modules
adc.h
1
6/******************************************************************************
7 *
8 * Copyright (C) 2022-2023 Maxim Integrated Products, Inc. (now owned by
9 * Analog Devices, Inc.),
10 * Copyright (C) 2023-2024 Analog Devices, Inc.
11 *
12 * Licensed under the Apache License, Version 2.0 (the "License");
13 * you may not use this file except in compliance with the License.
14 * You may obtain a copy of the License at
15 *
16 * http://www.apache.org/licenses/LICENSE-2.0
17 *
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
23 *
24 ******************************************************************************/
25
26/* Define to prevent redundant inclusion */
27#ifndef LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32680_ADC_H_
28#define LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32680_ADC_H_
29
30/* **** Includes **** */
31#include <stdint.h>
32#include "adc_regs.h"
33#include "mcr_regs.h"
34
35#ifdef __cplusplus
36extern "C" {
37#endif
38
45// Macros to select ADC channels
46#define MXC_V_ADC_CTRL_ADC_CHSEL_AIN0 ((uint32_t)(0x00000000UL))
47#define MXC_V_ADC_CTRL_ADC_CHSEL_AIN1 ((uint32_t)(0x00000001UL))
48#define MXC_V_ADC_CTRL_ADC_CHSEL_AIN2 ((uint32_t)(0x00000002UL))
49#define MXC_V_ADC_CTRL_ADC_CHSEL_AIN3 ((uint32_t)(0x00000003UL))
50#define MXC_V_ADC_CTRL_ADC_CHSEL_AIN4 ((uint32_t)(0x00000004UL))
51#define MXC_V_ADC_CTRL_ADC_CHSEL_AIN5 ((uint32_t)(0x00000005UL))
52#define MXC_V_ADC_CTRL_ADC_CHSEL_AIN6 ((uint32_t)(0x00000006UL))
53#define MXC_V_ADC_CTRL_ADC_CHSEL_AIN7 ((uint32_t)(0x00000007UL))
54#define MXC_V_ADC_CTRL_ADC_CHSEL_VCOREA ((uint32_t)(0x00000008UL))
55#define MXC_V_ADC_CTRL_ADC_CHSEL_VCOREB ((uint32_t)(0x00000009UL))
56#define MXC_V_ADC_CTRL_ADC_CHSEL_VRXOUT ((uint32_t)(0x0000000AUL))
57#define MXC_V_ADC_CTRL_ADC_CHSEL_VTXOUT ((uint32_t)(0x0000000BUL))
58#define MXC_V_ADC_CTRL_ADC_CHSEL_VDDA ((uint32_t)(0x0000000CUL))
59#define MXC_V_ADC_CTRL_ADC_CHSEL_VDDB ((uint32_t)(0x0000000DUL))
60#define MXC_V_ADC_CTRL_ADC_CHSEL_VDDI0 ((uint32_t)(0x0000000EUL))
61#define MXC_V_ADC_CTRL_ADC_CHSEL_VDDI0H ((uint32_t)(0x0000000FUL))
62#define MXC_V_ADC_CTRL_ADC_CHSEL_VREGI ((uint32_t)(0x00000010UL))
63
64/***************************************************************************************************************
65 DATA STRUCTURES FOR ADC INITIALIZATION
66***************************************************************************************************************/
70typedef enum {
71 MXC_ADC_CH_0 = MXC_V_ADC_CTRL_ADC_CHSEL_AIN0, // Select Channel 0
72 MXC_ADC_CH_1 = MXC_V_ADC_CTRL_ADC_CHSEL_AIN1, // Select Channel 1
73 MXC_ADC_CH_2 = MXC_V_ADC_CTRL_ADC_CHSEL_AIN2, // Select Channel 2
74 MXC_ADC_CH_3 = MXC_V_ADC_CTRL_ADC_CHSEL_AIN3, // Select Channel 3
75 MXC_ADC_CH_4 = MXC_V_ADC_CTRL_ADC_CHSEL_AIN4, // Channel 0 divided by 5
76 MXC_ADC_CH_5 = MXC_V_ADC_CTRL_ADC_CHSEL_AIN5, // Channel 1 divided by 5
77 MXC_ADC_CH_6 = MXC_V_ADC_CTRL_ADC_CHSEL_AIN6, // VDDB divided by 4
78 MXC_ADC_CH_7 = MXC_V_ADC_CTRL_ADC_CHSEL_AIN7, // VDD18 input select
79 MXC_ADC_CH_VCOREA = MXC_V_ADC_CTRL_ADC_CHSEL_VCOREA, // VDD12 input select
80 MXC_ADC_CH_VCOREB = MXC_V_ADC_CTRL_ADC_CHSEL_VCOREB,
81 MXC_ADC_CH_VRXOUT = MXC_V_ADC_CTRL_ADC_CHSEL_VRXOUT, // VRTC divided by 2
82 MXC_ADC_CH_VTXOUT = MXC_V_ADC_CTRL_ADC_CHSEL_VTXOUT, // TMON input select
83 MXC_ADC_CH_VDDA = MXC_V_ADC_CTRL_ADC_CHSEL_VDDA,
84 MXC_ADC_CH_VDDB = MXC_V_ADC_CTRL_ADC_CHSEL_VDDB,
85 MXC_ADC_CH_VDDIO = MXC_V_ADC_CTRL_ADC_CHSEL_VDDI0,
86 MXC_ADC_CH_VDDIOH = MXC_V_ADC_CTRL_ADC_CHSEL_VDDI0H,
87 MXC_ADC_CH_VREGI = MXC_V_ADC_CTRL_ADC_CHSEL_VREGI,
89
90#if 0
94typedef enum {
95 MXC_ADC_COMP_0 = MXC_F_MCR_AINCOMP_AINCOMP0PD,
96 MXC_ADC_COMP_1 = MXC_F_MCR_AINCOMP_AINCOMP1PD,
97 MXC_ADC_COMP_2 = MXC_F_MCR_AINCOMP_AINCOMP2PD,
98 MXC_ADC_COMP_3 = MXC_F_MCR_AINCOMP_AINCOMP3PD,
99 MXC_ADC_COMP_HYST_0 = 1 << MXC_F_MCR_AINCOMP_AINCOMPHYST_POS, //Refer to the V HYST specification in the data sheet electrical characteristics
100 // for the values corresponding to this field value.
101 MXC_ADC_COMP_HYST_1 = 2 << MXC_F_MCR_AINCOMP_AINCOMPHYST_POS, //Refer to the V HYST specification in the data sheet electrical characteristics
102 // for the values corresponding to this field value.
103} mxc_adc_comp_t;
104#endif
105
110typedef enum {
111 MXC_ADC_MONITOR_0,
112 MXC_ADC_MONITOR_1,
113 MXC_ADC_MONITOR_2,
114 MXC_ADC_MONITOR_3,
116
121typedef enum {
122 MXC_ADC_SCALE_2X, // ADC Scale by 2x (this scales ADC Reference by 1/2)
123 MXC_ADC_SCALE_1, // ADC Scale by 1x (no scaling)
124 MXC_ADC_SCALE_2, // ADC Scale by 1/2
125 MXC_ADC_SCALE_3, // ADC Scale by 1/3
126 MXC_ADC_SCALE_4, // ADC Scale by 1/4
127 MXC_ADC_SCALE_6, // ADC Scale by 1/6 (this uses 1/3 and an additional 1/2 scaling)
128 MXC_ADC_SCALE_8, // ADC Scale by 1/8 (this uses 1/4 and an additional 1/2 scaling)
130
134typedef enum {
135 MXC_ADC_REF_INT, // Selects internal bandgap reference
136 MXC_ADC_REF_EXT, // Selects external reference pins
138
139// Callback used when a conversion event is complete
140typedef void (*mxc_adc_complete_cb_t)(void *req, int error);
141
142// Callback used when a monitor detects that a channel has reached a limit
143typedef void (*mxc_adc_monitor_cb_t)(void *req, int error);
144
145// Used to set up a monitor to watch a channel
146typedef struct {
147 mxc_adc_monitor_t monitor; // Monitor to use
148 mxc_adc_scale_t scale; // Channel scale to use (if external channel)
149 mxc_adc_chsel_t channel; // Channel to use
150 int lowThreshold; // Low Threshold for monitor (RAW ADC counts)
151 int highThreshold; // High Threshold for monitor (RAW ADC counts)
152 mxc_adc_monitor_cb_t callback; // Function to call when the channel crosses threshold
153} mxc_adc_monitor_req_t;
154
155typedef struct {
156 mxc_adc_chsel_t channel; // Channel to use
157 mxc_adc_scale_t scale; // Channel scale to use (if external channel)
158 int rawADCValue; // Result of the conversion
159 mxc_adc_complete_cb_t callback; // Function to call when callback is complete
160} mxc_adc_conversion_req_t;
161
167int MXC_ADC_Init(void);
168
175
181int MXC_ADC_Busy(void);
182
188void MXC_ADC_EnableInt(uint32_t flags);
189
195void MXC_ADC_DisableInt(uint32_t flags);
196
203
209void MXC_ADC_ClearFlags(uint32_t flags);
210
219
226
232void MXC_ADC_SetDataAlignment(int msbJustify);
233
234// Internal channels are approx. known and have fixed scaling
235// Externals channels can be scaled with standard scaling (1-4x)
236// Or by using a separate 1/2 input scale, or a 1/2 ref scale (total range 0.5-8x)
243
250
258
266
274void MXC_ADC_SetMonitorHighThreshold(mxc_adc_monitor_t monitor, uint32_t threshold);
275
284
292void MXC_ADC_SetMonitorLowThreshold(mxc_adc_monitor_t monitor, uint32_t threshold);
293
302
311
320
321// Monitor a channel and call the callback if it hits a limit
329void MXC_ADC_EnableMonitorAsync(mxc_adc_monitor_t monitor, mxc_adc_monitor_cb_t callback);
330
337
347
359int MXC_ADC_StartConversionAsync(mxc_adc_chsel_t channel, mxc_adc_complete_cb_t callback);
360
368
377int MXC_ADC_Convert(mxc_adc_conversion_req_t *req);
378
388int MXC_ADC_ConvertAsync(mxc_adc_conversion_req_t *req);
389
395void MXC_ADC_Monitor(mxc_adc_monitor_req_t req);
396
404void MXC_ADC_MonitorAsync(mxc_adc_monitor_req_t req);
405
411void MXC_ADC_EnableComparators(uint32_t mask);
412
418void MXC_ADC_DisableComparators(uint32_t mask);
419
426int MXC_ADC_GetData(uint16_t *outdata);
429#ifdef __cplusplus
430}
431#endif
432
433#endif // LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32680_ADC_H_
Registers, Bit Masks and Bit Positions for the ADC Peripheral Module.
int MXC_ADC_GetMonitorHighThreshold(mxc_adc_monitor_t monitor)
Set the high limit for a specific monitor.
void MXC_ADC_ClearFlags(uint32_t flags)
Performs the ADC startup procedure.
mxc_adc_monitor_t
Definition: adc.h:110
int MXC_ADC_GetData(uint16_t *outdata)
Gets the result from the previous ADC conversion.
void MXC_ADC_EnableMonitorAsync(mxc_adc_monitor_t monitor, mxc_adc_monitor_cb_t callback)
Set a callback to be called when a monitor goes out of range.
void MXC_ADC_DisableMonitor(mxc_adc_monitor_t monitors)
Disable channel high/low monitor.
int MXC_ADC_GetConversionSpeed(void)
Gets the current ADC conversion speed.
int MXC_ADC_GetMonitorLowThreshold(mxc_adc_monitor_t monitor)
Set the low limit for a specific monitor.
int MXC_ADC_GetMonitorChannel(mxc_adc_monitor_t monitor)
Get the channel used by a monitor.
void MXC_ADC_SetDataAlignment(int msbJustify)
Gets the current ADC conversion speed.
void MXC_ADC_SetExtScale(mxc_adc_scale_t scale)
Sets the scaling used for conversions on external channels.
int MXC_ADC_Busy(void)
Checks if the ADC is busy (performing a conversion)
void MXC_ADC_DisableComparators(uint32_t mask)
Disable the Comparators.
void MXC_ADC_RefSelect(mxc_adc_ref_t ref)
Select the ADC reference source.
void MXC_ADC_DisableMonitorAsync(mxc_adc_monitor_t monitor)
Disable a callback for a monitor.
void MXC_ADC_EnableMonitor(mxc_adc_monitor_t monitors)
Enable channel high/low monitor.
int MXC_ADC_SetConversionSpeed(uint32_t hz)
Sets the ADC conversion speed.
void MXC_ADC_SetMonitorHighThreshold(mxc_adc_monitor_t monitor, uint32_t threshold)
Set the high limit for a specific monitor.
int MXC_ADC_StartConversion(mxc_adc_chsel_t channel)
Perform a conversion on a specific channel.
mxc_adc_chsel_t
Definition: adc.h:70
int MXC_ADC_ConvertAsync(mxc_adc_conversion_req_t *req)
Perform a conversion on a specific channel.
void MXC_ADC_EnableInt(uint32_t flags)
Enable specific ADC interrupts.
int MXC_ADC_Convert(mxc_adc_conversion_req_t *req)
Perform a conversion on a specific channel.
int MXC_ADC_Init(void)
Performs the ADC startup procedure.
mxc_adc_scale_t
Definition: adc.h:121
void MXC_ADC_SetMonitorChannel(mxc_adc_monitor_t monitor, mxc_adc_chsel_t channel)
Set a monitor to use a specific channel.
mxc_adc_ref_t
Definition: adc.h:134
int MXC_ADC_StartConversionAsync(mxc_adc_chsel_t channel, mxc_adc_complete_cb_t callback)
Perform a conversion on a specific channel.
void MXC_ADC_MonitorAsync(mxc_adc_monitor_req_t req)
Monitor a specific channel for an out of range event.
void MXC_ADC_Monitor(mxc_adc_monitor_req_t req)
Monitor a specific channel for an out of range event.
int MXC_ADC_GetFlags(void)
Performs the ADC startup procedure.
void MXC_ADC_EnableComparators(uint32_t mask)
Enable the Comparators.
int MXC_ADC_Handler(void)
Call this function from the ADC ISR when using Async API functions.
void MXC_ADC_DisableInt(uint32_t flags)
Disable specific ADC interrupts.
void MXC_ADC_SetMonitorLowThreshold(mxc_adc_monitor_t monitor, uint32_t threshold)
Set the low limit for a specific monitor.
int MXC_ADC_Shutdown(void)
Shuts down the ADC.
Registers, Bit Masks and Bit Positions for the MCR Peripheral Module.