uMIDI
The swiss army knife for quick and easy developement of MIDI applications.
Data Structures | Functions | Variables
adc.c File Reference

ADC configuration and service functions. More...

#include <stddef.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include "adc.h"
#include "math.h"
#include "midi.h"
#include "leds.h"
#include "usb.h"
Include dependency graph for adc.c:

Data Structures

struct  adc_channel_regs
 ADC channel configuration registers. More...
 

Functions

static uint16_t compensate_offset (uint16_t sample)
 
static uint16_t perform_filtered_measurement (enum adc_channel channel)
 
uint16_t calibrate_adc_offset (enum adc_channel channel)
 Calibrates the ADC offset using the given channel. More...
 
void disable_adc_interrupt (enum adc_channel channel)
 Disables the interrupt for an ADC channel. More...
 
void enable_adc_interrupt (enum adc_channel channel)
 Enables the interrupt for an ADC channel. More...
 
void init_adc_module (const struct adc_config *const config)
 Initializes and calibrates the ADC. More...
 
void init_adc_conversion (const struct adc_conversion_config *const config)
 Sets up an ADC conversion. More...
 
void set_adc_channel0_max_value (void)
 Defines the current ADC value as MIDI max value. More...
 
void set_adc_channel0_min_value (void)
 Defines the current ADC value as MIDI min value. More...
 
void set_adc_offset (uint16_t new_offset)
 Sets the ADC offset. More...
 
void trigger_adc (enum adc_channel channel)
 Triggers a conversion for the given channel. More...
 
 ISR (ADCA_CH0_vect)
 Interrupt service routine for ADC channel 0. More...
 
static void invoke_callbacks (enum adc_channel channel, uint16_t value)
 Invokes registered callbacks for the given channels. More...
 
 ISR (ADCA_CH1_vect)
 Interrupt service routine for ADC channel 1. More...
 
 ISR (ADCA_CH2_vect)
 Interrupt service routine for ADC channel 2. More...
 
 ISR (ADCA_CH3_vect)
 Interrupt service routine for ADC channel 3. More...
 

Variables

static const struct adc_channel_regs adc_regs []
 Lookup table for ADC channel configuration registers. More...
 
static uint16_t offset
 ADC offset. More...
 
static uint16_t sample_buffer [(1<<(8 *sizeof(adc_accumulator) - 12))]
 Buffer for ADC conversion results of channel 0. More...
 
static void(* callbacks_signed [sizeof(adc_regs)/sizeof(struct adc_channel_regs)])(int16_t)
 Conversion callbacks for signed values. More...
 
static void(* callbacks_unsigned [sizeof(adc_regs)/sizeof(struct adc_channel_regs)])(uint16_t)
 Conversion callbacks for unsigned values. More...
 
static struct linear_rangeadc_input_range
 Conversion callbacks for unsigned values. More...
 

Detailed Description

ADC configuration and service functions.

Function Documentation

◆ calibrate_adc_offset()

uint16_t calibrate_adc_offset ( enum adc_channel  channel)

Calibrates the ADC offset using the given channel.

Parameters
channelthe ADC channel
Returns
the measured offset

◆ compensate_offset()

static uint16_t compensate_offset ( uint16_t  sample)
static

◆ disable_adc_interrupt()

void disable_adc_interrupt ( enum adc_channel  channel)
inline

Disables the interrupt for an ADC channel.

Parameters
channelthe ADC channel

◆ enable_adc_interrupt()

void enable_adc_interrupt ( enum adc_channel  channel)
inline

Enables the interrupt for an ADC channel.

Parameters
channelthe ADC channel

◆ init_adc_conversion()

void init_adc_conversion ( const struct adc_conversion_config config)

Sets up an ADC conversion.

Registers the callbacks, configures the input and performs a dummy-conversion. The GPIO pin is not configured here – this must be done through the GPIO module.

Parameters
configthe ADC conversion configuration
See also
init_gpio_module

◆ init_adc_module()

void init_adc_module ( const struct adc_config config)

Initializes and calibrates the ADC.

Selects the voltage reference, sets the clock prescaler and conversion mode and calibrates the ADC

Parameters
configthe shared ADC configuration

◆ invoke_callbacks()

static void invoke_callbacks ( enum adc_channel  channel,
uint16_t  value 
)
static

Invokes registered callbacks for the given channels.

Parameters
channelthe ADC channel whose callbacks should be invoked
valuethe ADC conversion result

◆ ISR() [1/4]

ISR ( ADCA_CH0_vect  )

Interrupt service routine for ADC channel 0.

This routine is different from the others: It computes a mean value of the last ADC_SAMPLE_BUFFER_SIZE samples, converts the result to a valid MIDI value in the range [0, 127] and passes it to the callback for unsigned values. All this happens in an atomic block.

◆ ISR() [2/4]

ISR ( ADCA_CH1_vect  )

Interrupt service routine for ADC channel 1.

Calls the registered callback(s) for the channel in an atomic block.

◆ ISR() [3/4]

ISR ( ADCA_CH2_vect  )

Interrupt service routine for ADC channel 2.

Calls the registered callback(s) for the channel in an atomic block.

◆ ISR() [4/4]

ISR ( ADCA_CH3_vect  )

Interrupt service routine for ADC channel 3.

Calls the registered callback(s) for the channel in an atomic block.

◆ perform_filtered_measurement()

static uint16_t perform_filtered_measurement ( enum adc_channel  channel)
static

◆ set_adc_channel0_max_value()

void set_adc_channel0_max_value ( void  )

Defines the current ADC value as MIDI max value.

◆ set_adc_channel0_min_value()

void set_adc_channel0_min_value ( void  )

Defines the current ADC value as MIDI min value.

◆ set_adc_offset()

void set_adc_offset ( uint16_t  offset)

Sets the ADC offset.

Parameters
offsetthe offset

◆ trigger_adc()

void trigger_adc ( enum adc_channel  channel)

Triggers a conversion for the given channel.

Parameters
channelthe ADC channel

Variable Documentation

◆ adc_input_range

struct linear_range* adc_input_range
static

Conversion callbacks for unsigned values.

Used in the interrupt service routines

◆ adc_regs

const struct adc_channel_regs adc_regs[]
static
Initial value:
= {
{
.control_register = &ADCA_CH0_CTRL,
.mux_register = &ADCA_CH0_MUXCTRL,
.interrupt_register = &ADCA_CH0_INTCTRL,
.interrupt_flag = &ADCA_CH0_INTFLAGS,
.result_register = &ADCA.CH0RES
},
{
.control_register = &ADCA_CH1_CTRL,
.mux_register = &ADCA_CH1_MUXCTRL,
.interrupt_register = &ADCA_CH1_INTCTRL,
.interrupt_flag = &ADCA_CH1_INTFLAGS,
.result_register = &ADCA.CH1RES
},
{
.control_register = &ADCA_CH2_CTRL,
.mux_register = &ADCA_CH2_MUXCTRL,
.interrupt_register = &ADCA_CH2_INTCTRL,
.interrupt_flag = &ADCA_CH2_INTFLAGS,
.result_register = &ADCA.CH2RES
},
{
.control_register = &ADCA_CH3_CTRL,
.mux_register = &ADCA_CH3_MUXCTRL,
.interrupt_register = &ADCA_CH3_INTCTRL,
.interrupt_flag = &ADCA_CH3_INTFLAGS,
.result_register = &ADCA.CH3RES
},
}

Lookup table for ADC channel configuration registers.

◆ callbacks_signed

void(* callbacks_signed[sizeof(adc_regs)/sizeof(struct adc_channel_regs)])(int16_t)
static

Conversion callbacks for signed values.

Used in the interrupt service routines

◆ callbacks_unsigned

void(* callbacks_unsigned[sizeof(adc_regs)/sizeof(struct adc_channel_regs)])(uint16_t)
static

Conversion callbacks for unsigned values.

Used in the interrupt service routines

◆ offset

uint16_t offset
static

ADC offset.

Set during offset calibration

◆ sample_buffer

uint16_t sample_buffer[(1<<(8 *sizeof(adc_accumulator) - 12))]
static

Buffer for ADC conversion results of channel 0.

See also
ISR(ADCA_CH0_vect)