summaryrefslogtreecommitdiff
path: root/bsp/samd21/sam_d2x_asflib/sam0/drivers/sdadc/sdadc.c
blob: ef868a51a6c5c498c5fe8e16b648d064869b6673 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/**
 * \file
 *
 * \brief SAM Sigma-Delta Analog-to-Digital Converter (SDADC) Driver
 *
 * Copyright (C) 2015 Atmel Corporation. All rights reserved.
 *
 * \asf_license_start
 *
 * \page License
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * 3. The name of Atmel may not be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * 4. This software may only be redistributed and used in connection with an
 *    Atmel microcontroller product.
 *
 * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
 * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * \asf_license_stop
 *
 */
/*
 * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
 */

#include "sdadc.h"

/**
* \internal Configure MUX settings for the analog pins
*
* This function will set the given SDADC input pins
* to the analog function in the pinmux, giving
* the SDADC access to the analog signal
*
* \param [in] pin AINxx pin to configure
*/
static inline void _sdadc_configure_ain_pin(uint32_t pin)
{
	/* Pinmapping table for AINxx -> GPIO pin number */
	const uint32_t pinmapping[] = {
#if (SAMC21E)
			PIN_PA06B_SDADC_INN0, PIN_PA07B_SDADC_INP0,
#elif (SAMC21G)
			PIN_PA06B_SDADC_INN0, PIN_PA07B_SDADC_INP0,
			PIN_PB08B_SDADC_INN1, PIN_PB09B_SDADC_INP1,
#elif (SAMC21J)
			PIN_PA06B_SDADC_INN0, PIN_PA07B_SDADC_INP0,
			PIN_PB08B_SDADC_INN1, PIN_PB09B_SDADC_INP1,
			PIN_PB06B_SDADC_INN2, PIN_PB07B_SDADC_INP2,
#else
#  error SDADC pin mappings are not defined for this device.
#endif
	};

	uint32_t pin_map_result;

	struct system_pinmux_config config;
	system_pinmux_get_config_defaults(&config);

	config.input_pull   = SYSTEM_PINMUX_PIN_PULL_NONE;
	config.mux_position = 1;

	pin_map_result = pinmapping[pin * 2];
	system_pinmux_pin_set_config(pin_map_result, &config);

	pin_map_result = pinmapping[pin * 2 + 1];
	system_pinmux_pin_set_config(pin_map_result, &config);
}

/**
 * \internal Writes an SDADC configuration to the hardware module
 *
 * Writes out a given SDADC module configuration to the hardware module.
 *
 * \param[out] module_inst  Pointer to the SDADC software instance struct
 * \param[in]  config       Pointer to configuration struct
 *
 * \return Status of the configuration procedure.
 * \retval STATUS_OK               The configuration was successful
 * \retval STATUS_ERR_INVALID_ARG  Invalid argument(s) were provided
 */
static enum status_code _sdadc_set_config(
		struct sdadc_module *const module_inst,
		struct sdadc_config *const config)
{
	/* Get the hardware module pointer */
	Sdadc *const sdadc_module = module_inst->hw;

	/* Configure GCLK channel and enable clock */
	struct system_gclk_chan_config gclk_chan_conf;
	system_gclk_chan_get_config_defaults(&gclk_chan_conf);
	gclk_chan_conf.source_generator = config->clock_source;
	system_gclk_chan_set_config(SDADC_GCLK_ID, &gclk_chan_conf);
	system_gclk_chan_enable(SDADC_GCLK_ID);

	/* Setup pinmuxing for analog inputs */
	_sdadc_configure_ain_pin(config->mux_input);

	/* Configure run in standby */
	sdadc_module->CTRLA.reg = (config->run_in_standby << SDADC_CTRLA_RUNSTDBY_Pos)
			| (config->on_command << SDADC_CTRLA_ONDEMAND_Pos);

	while (sdadc_is_syncing(module_inst)) {
		/* Wait for synchronization */
	}

	/* Configure reference */
	sdadc_module->REFCTRL.reg = (config->reference.ref_sel) | (config->reference.ref_range) |
						(config->reference.on_ref_buffer << SDADC_REFCTRL_ONREFBUF_Pos);

	/* Configure CTRLB */
	sdadc_module->CTRLB.reg =
			(config->skip_count << SDADC_CTRLB_SKPCNT_Pos) |
			(config->clock_prescaler / 2 - 1) | config->osr;

	while (sdadc_is_syncing(module_inst)) {
		/* Wait for synchronization */
	}

	/* Configure CTRLC */
	sdadc_module->CTRLC.reg =
			(config->freerunning << SDADC_CTRLC_FREERUN_Pos);

	/* Configure SEQCTRL */
	sdadc_module->SEQCTRL.reg =
			(config->seq_enable[0]) | (config->seq_enable[1] << 1) | (config->seq_enable[2] << 2);

	/* Check validity of window thresholds */
	if (config->window.window_mode != SDADC_WINDOW_MODE_DISABLE) {
		if (config->window.window_lower_value > (int32_t)(SDADC_RESULT_RESULT_Msk / 2) ||
			config->window.window_lower_value < -(int32_t)(SDADC_RESULT_RESULT_Msk / 2 + 1) ||
			config->window.window_upper_value > (int32_t)(SDADC_RESULT_RESULT_Msk / 2) ||
			config->window.window_upper_value < -(int32_t)(SDADC_RESULT_RESULT_Msk / 2 + 1)) {
			/* Invalid value */
			return STATUS_ERR_INVALID_ARG;
		} else if (config->window.window_lower_value > (int32_t)SDADC_RESULT_RESULT_Msk ||
				config->window.window_upper_value > (int32_t)SDADC_RESULT_RESULT_Msk){
			/* Invalid value */
			return STATUS_ERR_INVALID_ARG;
		}
	}

	while (sdadc_is_syncing(module_inst)) {
		/* Wait for synchronization */
	}

	/* Configure window mode */
	sdadc_module->WINCTRL.reg = config->window.window_mode;

	while (sdadc_is_syncing(module_inst)) {
		/* Wait for synchronization */
	}

	/* Configure lower threshold */
	sdadc_module->WINLT.reg =
			config->window.window_lower_value << SDADC_WINLT_WINLT_Pos;

	while (sdadc_is_syncing(module_inst)) {
		/* Wait for synchronization */
	}

	/* Configure lower threshold */
	sdadc_module->WINUT.reg = config->window.window_upper_value <<
			SDADC_WINUT_WINUT_Pos;

	while (sdadc_is_syncing(module_inst)) {
		/* Wait for synchronization */
	}

	/* Configure pin scan mode and positive and negative input pins */
	sdadc_module->INPUTCTRL.reg = config->mux_input;

	/* Configure events */
	sdadc_module->EVCTRL.reg = config->event_action;

	/* Disable all interrupts */
	sdadc_module->INTENCLR.reg = (1 << SDADC_INTENCLR_WINMON_Pos) |
			(1 << SDADC_INTENCLR_OVERRUN_Pos) | (1 << SDADC_INTENCLR_RESRDY_Pos);

	/* Make sure offset correction value is valid */
	if (config->correction.offset_correction > (int32_t)(SDADC_OFFSETCORR_MASK / 2) ||
		config->correction.offset_correction < - (int32_t)(SDADC_OFFSETCORR_MASK / 2 + 1)) {
		return STATUS_ERR_INVALID_ARG;
	} else {
		while (sdadc_is_syncing(module_inst)) {
			/* Wait for synchronization */
		}

		/* Set offset correction value */
		sdadc_module->OFFSETCORR.reg = config->correction.offset_correction <<
				SDADC_OFFSETCORR_OFFSETCORR_Pos;
	}

	/* Make sure gain_correction value is valid */
	if (config->correction.gain_correction > SDADC_GAINCORR_MASK) {
		return STATUS_ERR_INVALID_ARG;
	} else {
		while (sdadc_is_syncing(module_inst)) {
			/* Wait for synchronization */
		}

		/* Set gain correction value */
		sdadc_module->GAINCORR.reg = config->correction.gain_correction <<
				SDADC_GAINCORR_GAINCORR_Pos;
	}

	/* Make sure shift_correction value is valid */
	if (config->correction.shift_correction > SDADC_SHIFTCORR_MASK) {
		return STATUS_ERR_INVALID_ARG;
	} else {
		while (sdadc_is_syncing(module_inst)) {
			/* Wait for synchronization */
		}

		/* Set shift correction value */
		sdadc_module->SHIFTCORR.reg = config->correction.shift_correction <<
				SDADC_SHIFTCORR_SHIFTCORR_Pos;
	}

	return STATUS_OK;
}

/**
 * \brief Initializes the SDADC.
 *
 * Initializes the SDADC device struct and the hardware module based on the
 * given configuration struct values.
 *
 * \param[out] module_inst Pointer to the SDADC software instance struct
 * \param[in]  hw          Pointer to the SDADC module instance
 * \param[in]  config      Pointer to the configuration struct
 *
 * \return Status of the initialization procedure.
 * \retval STATUS_OK                The initialization was successful
 * \retval STATUS_ERR_INVALID_ARG   Invalid argument(s) were provided
 * \retval STATUS_BUSY          The module is busy with a reset operation
 * \retval STATUS_ERR_DENIED        The module is enabled
 */
enum status_code sdadc_init(
		struct sdadc_module *const module_inst,
		Sdadc *hw,
		struct sdadc_config *config)
{
	/* Sanity check arguments */
	Assert(module_inst);
	Assert(hw);
	Assert(config);

	/* Associate the software module instance with the hardware module */
	module_inst->hw = hw;

	/* Turn on the digital interface clock */
	system_apb_clock_set_mask(SYSTEM_CLOCK_APB_APBC, MCLK_APBCMASK_SDADC);

	if (hw->CTRLA.reg & SDADC_CTRLA_SWRST) {
		/* We are in the middle of a reset. Abort. */
		return STATUS_BUSY;
	}

	if (hw->CTRLA.reg & SDADC_CTRLA_ENABLE) {
		/* Module must be disabled before initialization. Abort. */
		return STATUS_ERR_DENIED;
	}

	/* Store the selected reference for later use */
	module_inst->reference = config->reference;

#if SDADC_CALLBACK_MODE == true
	for (uint8_t i = 0; i < SDADC_CALLBACK_N; i++) {
		module_inst->callback[i] = NULL;
	};

	module_inst->registered_callback_mask = 0;
	module_inst->enabled_callback_mask = 0;
	module_inst->remaining_conversions = 0;
	module_inst->job_status = STATUS_OK;

	_sdadc_instances[0] = module_inst;

	if (config->event_action == SDADC_EVENT_ACTION_DISABLED &&
			!config->freerunning) {
		module_inst->software_trigger = true;
	} else {
		module_inst->software_trigger = false;
	}
#endif

	/* Write configuration to module */
	return _sdadc_set_config(module_inst, config);
}