# MIOS32_AIN

## Defines
#define | ADC_PERIPHERAL_FRQ (MIOS32_SYS_CPU_FREQUENCY/4)
#define | ADC_CLKDIV (25-1)
#define | NUM_CHANNELS_MAX 8
#define | NUM_AIN_PINS (NUM_CHANNELS_MAX * (1 << MIOS32_AIN_MUX_PINS))
#define | NUM_CHANGE_WORDS (1 + (NUM_AIN_PINS>>5))
#define | SPIKE_FILTER 1
#define | SPIKE_FILTER_DIFF 64
#define | NUM_CHANNELS_MAX 16
#define | NUM_AIN_PINS (NUM_CHANNELS_MAX * (1 << MIOS32_AIN_MUX_PINS))
#define | NUM_CHANGE_WORDS (1 + (NUM_AIN_PINS>>5))
#define | NUM_CHANNELS_MAX 8
#define | NUM_AIN_PINS (NUM_CHANNELS_MAX * (1 << MIOS32_AIN_MUX_PINS))
#define | NUM_CHANGE_WORDS (1 + (NUM_AIN_PINS>>5))
## Functions
s32 | MIOS32_AIN_Init (u32 mode)
s32 | MIOS32_AIN_ServicePrepareCallback_Init (void *_service_prepare_callback)
s32 | MIOS32_AIN_PinGet (u32 pin)
s32 | MIOS32_AIN_DeadbandGet (void)
s32 | MIOS32_AIN_DeadbandSet (u16 deadband)
s32 | MIOS32_AIN_Handler (void *_callback)
s32 | MIOS32_AIN_StartConversions (void)

## Detailed Description

AIN driver for MIOS32

ADC channels which should be converted have to be specified with a mask (MIOS32_AIN_CHANNEL_MASK), which has to be added to the application specific mios32_config.h file.

Conversion results are transfered into the adc_conversion_values[] array after a burst scan After the scan is completed, the ADC interrupt will be invoked to calculate the final (optionally oversampled) values, and to transfer them into the ain_pin_values[] array if the value change is greater than the defined MIOS32_AIN_DEADBAND (can be changed with MIOS32_AIN_DeadbandSet() during runtime)

Value changes (within the deadband) will be notified to the MIOS32_AIN_Handler(). This function isn't called directly by the application, but it's part of the programming model framework. E.g., if the "traditional" framework is used, the AIN handler will be called each mS, and it will call the 'APP_AIN_NotifyChange(u32 pin, u32 pin_value)' hook on pin changes.
The AIN handler will trigger a new scan after all pins have been checked.

Analog channels can be multiplexed via MBHP_AINX4 modules. The selection pins can be connected to any free GPIO pin, the assignments have to be added to the mios32_config.h file.
Usually the 3 selection lines are connected to J5C.A0/1/2 of the core module. Together with the 8 analog channels at J5A/B this results into 64 analog pins.

It's possible to define an oversampling rate, which leads to an accumulation of conversion results to increase the resolution and to improve the accuracy.

A special idle mechanism has been integrated which avoids sporadical jittering values of AIN pins which could happen due to EMI issues.
MIOS32_AIN_IDLE_CTR defines the number of conversions after which the pin goes into idle state if no conversion exceeded the MIOS32_AIN_DEADBAND. In idle state, MIOS32_AIN_DEADBAND_IDLE will be used instead, which is greater (accordingly the pin will be less sensitive). The pin will use the original MIOS32_AIN_DEADBAND again once MIOS32_AIN_DEADBAND_IDLE has been exceeded.
This feature can be disabled by setting MIOS32_AIN_DEADBAND_IDLE to 0 in your mios32_config.h file.

Especially due to the bad layout of the LPCXPRESSO board a simple spike filter has been added which filters values which are much higher (> 64) than the previous value. This filter is currently always enabled (no MIOS32_* flag)

AIN driver for MIOS32

ADC channels which should be converted have to be specified with a mask (MIOS32_AIN_CHANNEL_MASK), which has to be added to the application specific mios32_config.h file.

Conversion results are transfered into the adc_conversion_values[] array by DMA1 Channel 1 to relieve the CPU.

After the scan is completed, the DMA channel interrupt will be invoked to calculate the final (optionally oversampled) values, and to transfer them into the ain_pin_values[] array if the value change is greater than the defined MIOS32_AIN_DEADBAND (can be changed with MIOS32_AIN_DeadbandSet() during runtime)

Value changes (within the deadband) will be notified to the MIOS32_AIN_Handler(). This function isn't called directly by the application, but it's part of the programming model framework. E.g., if the "traditional" framework is used, the AIN handler will be called each mS, and it will call the 'APP_AIN_NotifyChange(u32 pin, u32 pin_value)' hook on pin changes.
The AIN handler will trigger a new scan after all pins have been checked.

Analog channels can be multiplexed via MBHP_AINX4 modules. The selection pins can be connected to any free GPIO pin, the assignments have to be added to the mios32_config.h file.
Usually the 3 selection lines are connected to J5C.A0/1/2 of the core module. Together with the 8 analog channels at J5A/B this results into 64 analog pins.

The AIN driver is flexible enough to increase the number of ADC channels to not less than 16 (connected to J5A/B/C and J16). Together with 4 AINX4 multiplexers this results into 128 analog channels.

It's possible to define an oversampling rate, which leads to an accumulation of conversion results to increase the resolution and to improve the accuracy.

A special idle mechanism has been integrated which avoids sporadical jittering values of AIN pins which could happen due to EMI issues.
MIOS32_AIN_IDLE_CTR defines the number of conversions after which the pin goes into idle state if no conversion exceeded the MIOS32_AIN_DEADBAND. In idle state, MIOS32_AIN_DEADBAND_IDLE will be used instead, which is greater (accordingly the pin will be less sensitive). The pin will use the original MIOS32_AIN_DEADBAND again once MIOS32_AIN_DEADBAND_IDLE has been exceeded.
This feature can be disabled by setting MIOS32_AIN_DEADBAND_IDLE to 0 in your mios32_config.h file.

## Define Documentation

#define ADC_CLKDIV (25-1)

#define ADC_PERIPHERAL_FRQ (MIOS32_SYS_CPU_FREQUENCY/4)

#define NUM_AIN_PINS (NUM_CHANNELS_MAX * (1 << MIOS32_AIN_MUX_PINS))

#define NUM_CHANGE_WORDS (1 + (NUM_AIN_PINS>>5))

#define NUM_CHANNELS_MAX 8

#define NUM_CHANNELS_MAX 16

#define NUM_CHANNELS_MAX 8

#define SPIKE_FILTER 1

#define SPIKE_FILTER_DIFF 64

## Function Documentation

s32 MIOS32_AIN_DeadbandGet | ( | void |  | ) |

Returns:
  the deadband which is used to notify changes
  < 0 on error

s32 MIOS32_AIN_DeadbandSet | ( | u16 | deadband | ) |

Sets the difference between last and current pot value which has to be achieved to trigger the callback function passed to AINSER_Handler()

Returns:
  < 0 on error

s32 MIOS32_AIN_Handler | ( | void * | _callback | ) |

Checks for pin changes, and calls given callback function with following parameters on pin changes:

       void AIN_NotifyChanged(u32 pin, u16 value)

Parameters:

  `[in]` | _callback | pointer to callback function

Returns:
  < 0 on errors

Here is the call graph for this function:

s32 MIOS32_AIN_Init | ( | u32 | mode | ) |

Initializes AIN driver

Parameters:

  `[in]` | mode | currently only mode 0 supported

Returns:
  < 0 if initialisation failed

Here is the call graph for this function:

s32 MIOS32_AIN_PinGet | ( | u32 | pin | ) |

Returns value of an AIN Pin

Parameters:

  `[in]` | pin | number

Returns:
  AIN pin value - resolution depends on the selected MIOS32_AIN_OVERSAMPLING_RATE!
  -1 if pin doesn't exist

s32 MIOS32_AIN_ServicePrepareCallback_Init | ( | void * | _service_prepare_callback | ) |

Installs an optional "Service Prepare" callback function, which is called before all ADC channels are scanned.

It is useful to switch additional multiplexers, to reconfigure ADC pins of touch panels, etc.

The scan will be started if the callback function returns 0

The scan will be skipped if the callback function returns a value >= 1 so that it is possible to insert setup times while switching analog inputs.

An usage example can be found unter $MIOS32_PATH/apps/examples/dog_g_touchpanel

Parameters:

  `[in]` | *_callback_service_prepare | pointer to callback function:
          s32 AIN_ServicePrepare(void);

Returns:
  < 0 on errors

s32 MIOS32_AIN_StartConversions | ( | void |  | ) |

Starts an ADC conversion (use this if the MIOS32_AIN_Handler call has been disabled via define MIOS32_DONT_SERVICE_AIN 1 in mios32_config.h

In this case, the MIOS32_AIN_StartConversions() function has to be called periodically from the application (e.g. from a timer), and conversion values can be retrieved with MIOS32_AIN_PinGet()

Returns:
  < 0 on errors