Lines Matching +full:dma +full:- +full:channel +full:- +full:mask
2 * Copyright (c) 2017-2018, 2020, NXP
4 * SPDX-License-Identifier: Apache-2.0
13 #include <zephyr/drivers/dma.h>
36 uint32_t dma_slot; /* ADC DMA MUX slot */
73 #define SIM_SOPT7_ADCSET(x, shifts, mask) \ argument
74 (((uint32_t)(((uint32_t)(x)) << shifts)) & mask)
79 uint32_t channel, int status) in adc_dma_callback() argument
82 struct mcux_adc16_data *data = dev->data; in adc_dma_callback()
84 LOG_DBG("DMA done"); in adc_dma_callback()
85 data->buffer++; in adc_dma_callback()
86 adc_context_on_sampling_done(&data->ctx, dev); in adc_dma_callback()
93 const struct mcux_adc16_config *config = dev->config; in adc_hw_trigger_enable()
95 /* enable ADC trigger channel */ in adc_hw_trigger_enable()
96 SIM->SOPT7 |= SIM_SOPT7_ADCSET(config->hw_trigger_src, in adc_hw_trigger_enable()
97 config->trg_offset, config->trg_bits) | in adc_hw_trigger_enable()
98 SIM_SOPT7_ADCSET(1, config->alt_offset, config->alt_bits); in adc_hw_trigger_enable()
105 uint8_t channel_id = channel_cfg->channel_id; in mcux_adc16_channel_setup()
108 LOG_ERR("Channel %d is not valid", channel_id); in mcux_adc16_channel_setup()
109 return -EINVAL; in mcux_adc16_channel_setup()
112 if (channel_cfg->acquisition_time != ADC_ACQ_TIME_DEFAULT) { in mcux_adc16_channel_setup()
113 LOG_ERR("Invalid channel acquisition time"); in mcux_adc16_channel_setup()
114 return -EINVAL; in mcux_adc16_channel_setup()
117 if (channel_cfg->differential) { in mcux_adc16_channel_setup()
119 return -EINVAL; in mcux_adc16_channel_setup()
122 if (channel_cfg->gain != ADC_GAIN_1) { in mcux_adc16_channel_setup()
123 LOG_ERR("Invalid channel gain"); in mcux_adc16_channel_setup()
124 return -EINVAL; in mcux_adc16_channel_setup()
137 const struct mcux_adc16_config *config = dev->config; in start_read()
138 struct mcux_adc16_data *data = dev->data; in start_read()
143 ADC_Type *base = config->base; in start_read()
145 switch (sequence->resolution) { in start_read()
166 return -EINVAL; in start_read()
169 tmp32 = base->CFG1 & ~(ADC_CFG1_MODE_MASK); in start_read()
171 base->CFG1 = tmp32; in start_read()
173 switch (sequence->oversampling) { in start_read()
191 return -EINVAL; in start_read()
193 ADC16_SetHardwareAverage(config->base, mode); in start_read()
195 if (sequence->buffer_size < 2) { in start_read()
196 LOG_ERR("sequence buffer size too small %d < 2", sequence->buffer_size); in start_read()
197 return -EINVAL; in start_read()
200 if (sequence->options) { in start_read()
201 if (sequence->buffer_size < in start_read()
202 2 * (sequence->options->extra_samplings + 1)) { in start_read()
204 return -EINVAL; in start_read()
208 data->buffer = sequence->buffer; in start_read()
210 adc_context_start_read(&data->ctx, sequence); in start_read()
212 error = adc_context_wait_for_completion(&data->ctx); in start_read()
214 dma_stop(data->dev_dma, data->adc_dma_config.dma_channel); in start_read()
222 struct mcux_adc16_data *data = dev->data; in mcux_adc16_read()
225 adc_context_lock(&data->ctx, false, NULL); in mcux_adc16_read()
227 adc_context_release(&data->ctx, error); in mcux_adc16_read()
237 struct mcux_adc16_data *data = dev->data; in mcux_adc16_read_async()
240 adc_context_lock(&data->ctx, true, async); in mcux_adc16_read_async()
242 adc_context_release(&data->ctx, error); in mcux_adc16_read_async()
250 const struct mcux_adc16_config *config = dev->config; in mcux_adc16_start_channel()
251 struct mcux_adc16_data *data = dev->data; in mcux_adc16_start_channel()
256 data->channel_id = find_lsb_set(data->channels) - 1; in mcux_adc16_start_channel()
258 LOG_DBG("Starting channel %d", data->channel_id); in mcux_adc16_start_channel()
264 channel_config.channelNumber = data->channel_id; in mcux_adc16_start_channel()
265 ADC16_SetChannelConfig(config->base, channel_group, &channel_config); in mcux_adc16_start_channel()
268 dma_start(data->dev_dma, data->adc_dma_config.dma_channel); in mcux_adc16_start_channel()
270 LOG_DBG("Starting channel done"); in mcux_adc16_start_channel()
278 data->channels = ctx->sequence.channels; in adc_context_start_sampling()
279 data->repeat_buffer = data->buffer; in adc_context_start_sampling()
282 LOG_DBG("config dma"); in adc_context_start_sampling()
283 data->adc_dma_config.dma_block.block_size = 2; in adc_context_start_sampling()
284 data->adc_dma_config.dma_block.dest_address = (uint32_t)data->buffer; in adc_context_start_sampling()
285 data->adc_dma_config.dma_cfg.head_block = in adc_context_start_sampling()
286 &(data->adc_dma_config.dma_block); in adc_context_start_sampling()
287 dma_config(data->dev_dma, data->adc_dma_config.dma_channel, in adc_context_start_sampling()
288 &data->adc_dma_config.dma_cfg); in adc_context_start_sampling()
291 mcux_adc16_start_channel(data->dev); in adc_context_start_sampling()
301 data->buffer = data->repeat_buffer; in adc_context_update_buffer_pointer()
308 const struct mcux_adc16_config *config = dev->config; in mcux_adc16_isr()
309 struct mcux_adc16_data *data = dev->data; in mcux_adc16_isr()
310 ADC_Type *base = config->base; in mcux_adc16_isr()
315 LOG_DBG("Finished channel %d. Result is 0x%04x", data->channel_id, in mcux_adc16_isr()
318 *data->buffer++ = result; in mcux_adc16_isr()
319 data->channels &= ~BIT(data->channel_id); in mcux_adc16_isr()
321 if (data->channels) { in mcux_adc16_isr()
324 adc_context_on_sampling_done(&data->ctx, dev); in mcux_adc16_isr()
331 const struct mcux_adc16_config *config = dev->config; in mcux_adc16_init()
332 struct mcux_adc16_data *data = dev->data; in mcux_adc16_init()
333 ADC_Type *base = config->base; in mcux_adc16_init()
341 adc_config.clockSource = (adc16_clock_source_t)config->clk_source; in mcux_adc16_init()
343 (adc16_long_sample_mode_t)config->long_sample; in mcux_adc16_init()
344 adc_config.enableHighSpeed = config->high_speed; in mcux_adc16_init()
345 adc_config.enableContinuousConversion = config->continuous_convert; in mcux_adc16_init()
370 if (config->channel_mux_b) { in mcux_adc16_init()
380 err = pinctrl_apply_state(config->pincfg, PINCTRL_STATE_DEFAULT); in mcux_adc16_init()
385 data->dev = dev; in mcux_adc16_init()
387 /* dma related init */ in mcux_adc16_init()
389 /* Enable DMA. */ in mcux_adc16_init()
391 data->adc_dma_config.dma_cfg.block_count = 1U; in mcux_adc16_init()
392 data->adc_dma_config.dma_cfg.dma_slot = config->dma_slot; in mcux_adc16_init()
393 data->adc_dma_config.dma_cfg.channel_direction = PERIPHERAL_TO_MEMORY; in mcux_adc16_init()
394 data->adc_dma_config.dma_cfg.source_burst_length = 2U; in mcux_adc16_init()
395 data->adc_dma_config.dma_cfg.dest_burst_length = 2U; in mcux_adc16_init()
396 data->adc_dma_config.dma_cfg.channel_priority = 0U; in mcux_adc16_init()
397 data->adc_dma_config.dma_cfg.dma_callback = adc_dma_callback; in mcux_adc16_init()
398 data->adc_dma_config.dma_cfg.user_data = (void *)dev; in mcux_adc16_init()
400 data->adc_dma_config.dma_cfg.source_data_size = 2U; in mcux_adc16_init()
401 data->adc_dma_config.dma_cfg.dest_data_size = 2U; in mcux_adc16_init()
402 data->adc_dma_config.dma_block.source_address = (uint32_t)&base->R[0]; in mcux_adc16_init()
404 if (data->dev_dma == NULL || !device_is_ready(data->dev_dma)) { in mcux_adc16_init()
405 LOG_ERR("dma binding fail"); in mcux_adc16_init()
406 return -EINVAL; in mcux_adc16_init()
409 if (config->periodic_trigger) { in mcux_adc16_init()
412 data->adc_dma_config.dma_channel = in mcux_adc16_init()
413 dma_request_channel(data->dev_dma, (void *)&adc_filter); in mcux_adc16_init()
417 data->adc_dma_config.dma_channel = in mcux_adc16_init()
418 dma_request_channel(data->dev_dma, (void *)&adc_filter); in mcux_adc16_init()
420 if (data->adc_dma_config.dma_channel == -EINVAL) { in mcux_adc16_init()
421 LOG_ERR("can not allocate dma channel"); in mcux_adc16_init()
422 return -EINVAL; in mcux_adc16_init()
424 LOG_DBG("dma allocated channel %d", data->adc_dma_config.dma_channel); in mcux_adc16_init()
426 config->irq_config_func(dev); in mcux_adc16_init()
430 adc_context_unlock_unconditionally(&data->ctx); in mcux_adc16_init()