All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
AFE: Analog Front End Driver

## Overview

The KSDK provides driver for the Analog Front End (AFE) module of Kinetis devices.

The Analog Front End or AFE is an integrated module that is comprised of ΣΔADCs, PGA, filtering and phase compensation blocks. The AFE is responsible for measuring the phase voltage, phase current and neutral current.

# Function groups

## Channel configuration structures

The driver uses instances of the channel configuration structures to configuration and initialization AFE channel. This structure holds the settings of the AFE measurement channel. The settings include AFE hardware/software triggering, AFE continuous/Single conversion mode, AFE channel mode, AFE channel analog gain, AFE channel oversampling ration. The AFE channel mode selects whether the bypass mode is enabled or disabled and the external clock selection.

## User configuration structures

The AFE driver uses instances of the user configuration structure afe_config_t for the AFE driver configuration. This structure holds the configuration which is common for all AFE channels. The settings include AFE low power mode, AFE result format, AFE clock divider mode, AFE clock source mode, and AFE start up delay of modulators.

## AFE Initialization

To initialize the AFE driver, for a typical use case, call the AFE_GetDefaultConfig() function which populates the structure. Then, call the AFE_Init() function and pass the base address of the AFE peripheral and a pointer to the user configuration structure.

To configure the AFE channel, for a typical use case call the AFE_GetDefaultChnConfig() function which populates the structure. Then, call the AFE_SetChnConfig() function and pass the base address of the AFE peripheral and a pointer to the channel configuration structure.

## AFE Conversion

The driver contains functions for software triggering, a channel delay after trigger setting, a result (raw or converted to right justified), reading and waiting functions.

If the software triggering is enabled (hwTriggerEnable parameter in afe_chn_config_t is a false value), call the AFE_SoftTriggerConv() function to start conversion.

# Typical use case

## AFE Initialization

/* Get AFE config default */
AFE_GetDefaultConfig(&afeExampleStruct);
afeExampleStruct.startupCount = 80U; /* startupCnt = (Clk_freq/Clk_div)*20e-6 */
/* Get channel config default */
AFE_GetDefaultChannelConfig(&afeChnExampleStruct);
/* Init AFE module */
AFE_Init(AFE, &afeExampleStruct);
/* AFE_Init AFE channel 0 */
AFE_SetChannelConfig(AFE, CHANNEL_INDEX, &afeChnExampleStruct);
/* Disable interrupt */
AFE_DisableChannelInterrupts(AFE, kAFE_Channel0InterruptEnable);
/* Disable DMA */
AFE_EnableChannelDMA(AFE, (uint32_t)kAFE_Channel0DMAEnable, false);

## AFE Conversion

/* Software trigger conversion */
while (1)
{
/* Wait next triger from user */
PRINTF("Press any key to trigger AFE conversion\r\n");
GETCHAR();
/* Software trigger conversion */
AFE_DoSoftwareTriggerChannel(AFE, kAFE_Channel0Trigger);
/* Wait until conversion complete */
while ((kAFE_Channel0ConversionCompleteFlag & AFE_GetChannelStatusFlags(AFE)) == 0)
{
}
/* Read result conversion */
result0 = (int32_t)AFE_GetChannelConversionValue(AFE, CHANNEL_INDEX);
/* Print result */
PRINTF("result0 = %d \r\n", result0);
}

## Data Structures

struct | afe_channel_config_t
| Defines the structure to initialize the AFE channel. More...
struct | afe_config_t
| Defines the structure to initialize the AFE module. More...

## Enumerations

enum

_afe_channel_status_flag {
kAFE_Channel0OverflowFlag = AFE_SR_OVR0_MASK,
kAFE_Channel1OverflowFlag = AFE_SR_OVR1_MASK,
kAFE_Channel2OverflowFlag = AFE_SR_OVR2_MASK,
kAFE_Channel0ReadyFlag = AFE_SR_RDY0_MASK,
kAFE_Channel1ReadyFlag = AFE_SR_RDY1_MASK,
kAFE_Channel2ReadyFlag = AFE_SR_RDY2_MASK,
kAFE_Channel0ConversionCompleteFlag = AFE_SR_COC0_MASK,
kAFE_Channel1ConversionCompleteFlag = AFE_SR_COC1_MASK,
kAFE_Channel2ConversionCompleteFlag = AFE_SR_COC2_MASK
}

| Defines the type of status flags. More...
enum | _afe_channel_interrupt_enable { kAFE_Channel0InterruptEnable = AFE_DI_INTEN0_MASK, kAFE_Channel1InterruptEnable = AFE_DI_INTEN1_MASK, kAFE_Channel2InterruptEnable = AFE_DI_INTEN2_MASK }
| Defines AFE interrupt enable. More...
enum | _afe_channel_dma_enable { kAFE_Channel0DMAEnable = AFE_DI_DMAEN0_MASK, kAFE_Channel1DMAEnable = AFE_DI_DMAEN1_MASK, kAFE_Channel2DMAEnable = AFE_DI_DMAEN2_MASK }
| Defines AFE DMA enable. More...
enum | _afe_software_trigger_command { kAFE_Channel0Trigger = AFE_CR_SOFT_TRG0_MASK, kAFE_Channel1Trigger = AFE_CR_SOFT_TRG1_MASK, kAFE_Channel2Trigger = AFE_CR_SOFT_TRG2_MASK }
| Defines AFE channel trigger flag. More...
enum | afe_decimator_oversample_ratio_t { kAFE_DecimatorOversampleRatio64 = 0U, kAFE_DecimatorOversampleRatio128 = 1U, kAFE_DecimatorOversampleRatio256 = 2U, kAFE_DecimatorOversampleRatio512 = 3U, kAFE_DecimatorOversampleRatio1024 = 4U, kAFE_DecimatorOversampleRatio2048 = 5U }
| AFE OSR modes. More...
enum | afe_result_format_t { kAFE_ResultFormatLeft = 0U, kAFE_ResultFormatRight = 1U }
| Defines the AFE result format modes. More...
enum | afe_clock_divider_t { kAFE_ClockDivider1 = 0U, kAFE_ClockDivider2 = 1U, kAFE_ClockDivider4 = 2U, kAFE_ClockDivider8 = 3U, kAFE_ClockDivider16 = 4U, kAFE_ClockDivider32 = 5U, kAFE_ClockDivider64 = 6U, kAFE_ClockDivider128 = 7U, kAFE_ClockDivider256 = 8U }
| Defines the AFE clock divider modes. More...
enum | afe_clock_source_t { kAFE_ClockSource0 = 0U, kAFE_ClockSource1 = 1U, kAFE_ClockSource2 = 2U, kAFE_ClockSource3 = 3U }
| Defines the AFE clock source modes. More...
enum | afe_pga_gain_t { kAFE_PgaDisable = 0U, kAFE_PgaGain1 = 1U, kAFE_PgaGain2 = 2U, kAFE_PgaGain4 = 3U, kAFE_PgaGain8 = 4U, kAFE_PgaGain16 = 5U, kAFE_PgaGain32 = 6U }
| Defines the PGA's values. More...
enum | afe_bypass_mode_t { kAFE_BypassInternalClockPositiveEdge = 0U, kAFE_BypassExternalClockPositiveEdge = 1U, kAFE_BypassInternalClockNegativeEdge = 2U, kAFE_BypassExternalClockNegativeEdge = 3U, kAFE_BypassDisable = 4U }
| Defines the bypass modes. More...

## Driver version

#define | FSL_AFE_DRIVER_VERSION (MAKE_VERSION(2, 0, 1))
| Version 2.0.1. More...

## AFE Initialization

void | AFE_Init (AFE_Type *base, const afe_config_t *config)
| Initialization for the AFE module. More...
void | AFE_Deinit (AFE_Type *base)
| De-Initialization for the AFE module. More...
void | AFE_GetDefaultConfig (afe_config_t *config)
| Fills the user configure structure. More...
static void | AFE_SoftwareReset (AFE_Type *base, bool enable)
| Software reset the AFE module. More...
static void | AFE_Enable (AFE_Type *base, bool enable)
| Enables all configured AFE channels. More...

## AFE Conversion

void | AFE_SetChannelConfig (AFE_Type *base, uint32_t channel, const afe_channel_config_t *config)
| Configure the selected AFE channel. More...
void | AFE_GetDefaultChannelConfig (afe_channel_config_t *config)
| Fills the channel configuration structure. More...
uint32_t | AFE_GetChannelConversionValue (AFE_Type *base, uint32_t channel)
| Reads the raw conversion value. More...
static void | AFE_DoSoftwareTriggerChannel (AFE_Type *base, uint32_t mask)
| Triggers the AFE conversion by software. More...
static uint32_t | AFE_GetChannelStatusFlags (AFE_Type *base)
| Gets the AFE status flag state. More...
void | AFE_SetChannelPhaseDelayValue (AFE_Type *base, uint32_t channel, uint32_t value)
| Sets phase delays value. More...
static void | AFE_SetChannelPhasetDelayOk (AFE_Type *base)
| Asserts the phase delay setting. More...
static void | AFE_EnableChannelInterrupts (AFE_Type *base, uint32_t mask)
| Enables AFE interrupt. More...
static void | AFE_DisableChannelInterrupts (AFE_Type *base, uint32_t mask)
| Disables AFE interrupt. More...
static uint32_t | AFE_GetEnabledChannelInterrupts (AFE_Type *base)
| Returns mask of all enabled AFE interrupts. More...
void | AFE_EnableChannelDMA (AFE_Type *base, uint32_t mask, bool enable)
| Enables/Disables AFE DMA. More...

## Data Structure Documentation

struct afe_channel_config_t

This structure keeps the configuration for the AFE channel.

### Data Fields

bool | enableHardwareTrigger
| Enable triggering by hardware. More...
bool | enableContinuousConversion
| Enable continuous conversion mode. More...
afe_bypass_mode_t | channelMode
| Select if channel is in bypassed mode. More...
afe_pga_gain_t | pgaGainSelect
| Select the analog gain applied to the input signal. More...
afe_decimator_oversample_ratio_t | decimatorOversampleRatio
| Select the over sampling ration. More...

#### Field Documentation

bool afe_channel_config_t::enableHardwareTrigger

bool afe_channel_config_t::enableContinuousConversion

afe_bypass_mode_t afe_channel_config_t::channelMode

afe_pga_gain_t afe_channel_config_t::pgaGainSelect

afe_decimator_oversample_ratio_t afe_channel_config_t::decimatorOversampleRatio

struct afe_config_t

This structure keeps the configuration for the AFE module.

### Data Fields

bool | enableLowPower
| Enable low power mode. More...
afe_result_format_t | resultFormat
| Select the result format. More...
afe_clock_divider_t | clockDivider
| Select the clock divider ration for the modulator clock. More...
afe_clock_source_t | clockSource
| Select clock source for modulator clock. More...
uint8_t | startupCount
| Select the start up delay of modulators. More...

#### Field Documentation

bool afe_config_t::enableLowPower

afe_result_format_t afe_config_t::resultFormat

afe_clock_divider_t afe_config_t::clockDivider

afe_clock_source_t afe_config_t::clockSource

uint8_t afe_config_t::startupCount

## Macro Definition Documentation

#define FSL_AFE_DRIVER_VERSION (MAKE_VERSION(2, 0, 1))

## Enumeration Type Documentation

enum _afe_channel_status_flag

Enumerator
kAFE_Channel0OverflowFlag | Channel 0 previous conversion result has not been read and new data has already arrived.
kAFE_Channel1OverflowFlag | Channel 1 previous conversion result has not been read and new data has already arrived.
kAFE_Channel2OverflowFlag | Channel 2 previous conversion result has not been read and new data has already arrived.
kAFE_Channel0ReadyFlag | Channel 0 is ready to conversion.
kAFE_Channel1ReadyFlag | Channel 1 is ready to conversion.
kAFE_Channel2ReadyFlag | Channel 2 is ready to conversion.
kAFE_Channel0ConversionCompleteFlag | Channel 0 conversion is complete.
kAFE_Channel1ConversionCompleteFlag | Channel 1 conversion is complete.
kAFE_Channel2ConversionCompleteFlag | Channel 2 conversion is complete.

enum _afe_channel_interrupt_enable

Enumerator
kAFE_Channel0InterruptEnable | Channel 0 Interrupt.
kAFE_Channel1InterruptEnable | Channel 1 Interrupt.
kAFE_Channel2InterruptEnable | Channel 2 Interrupt.

enum _afe_channel_dma_enable

Enumerator
kAFE_Channel0DMAEnable | Channel 0 DMA.
kAFE_Channel1DMAEnable | Channel 1 DMA.
kAFE_Channel2DMAEnable | Channel 2 DMA.

enum _afe_software_trigger_command

Enumerator
kAFE_Channel0Trigger | Channel 0 software trigger.
kAFE_Channel1Trigger | Channel 1 software trigger.
kAFE_Channel2Trigger | Channel 2 software trigger.

enum afe_decimator_oversample_ratio_t

Enumerator
kAFE_DecimatorOversampleRatio64 | Decimator over sample ratio is 64.
kAFE_DecimatorOversampleRatio128 | Decimator over sample ratio is 128.
kAFE_DecimatorOversampleRatio256 | Decimator over sample ratio is 256.
kAFE_DecimatorOversampleRatio512 | Decimator over sample ratio is 512.
kAFE_DecimatorOversampleRatio1024 | Decimator over sample ratio is 1024.
kAFE_DecimatorOversampleRatio2048 | Decimator over sample ratio is 2048.

enum afe_result_format_t

Enumerator
kAFE_ResultFormatLeft | Left justified result format.
kAFE_ResultFormatRight | Right justified result format.

enum afe_clock_divider_t

Enumerator
kAFE_ClockDivider1 | Clock divided by 1.
kAFE_ClockDivider2 | Clock divided by 2.
kAFE_ClockDivider4 | Clock divided by 4.
kAFE_ClockDivider8 | Clock divided by 8.
kAFE_ClockDivider16 | Clock divided by 16.
kAFE_ClockDivider32 | Clock divided by 32.
kAFE_ClockDivider64 | Clock divided by 64.
kAFE_ClockDivider128 | Clock divided by 128.
kAFE_ClockDivider256 | Clock divided by 256.

enum afe_clock_source_t

Enumerator
kAFE_ClockSource0 | Modulator clock source 0.
kAFE_ClockSource1 | Modulator clock source 1.
kAFE_ClockSource2 | Modulator clock source 2.
kAFE_ClockSource3 | Modulator clock source 3.

enum afe_pga_gain_t

Enumerator
kAFE_PgaDisable | PGA disabled.
kAFE_PgaGain1 | Input gained by 1.
kAFE_PgaGain2 | Input gained by 2.
kAFE_PgaGain4 | Input gained by 4.
kAFE_PgaGain8 | Input gained by 8.
kAFE_PgaGain16 | Input gained by 16.
kAFE_PgaGain32 | Input gained by 32.

enum afe_bypass_mode_t

Enumerator
kAFE_BypassInternalClockPositiveEdge | Bypassed channel mode - internal clock selected, positive edge for registering data by the decimation filter.
kAFE_BypassExternalClockPositiveEdge | Bypassed channel mode - external clock selected, positive edge for registering data by the decimation filter.
kAFE_BypassInternalClockNegativeEdge | Bypassed channel mode - internal clock selected, negative edge for registering data by the decimation filter.
kAFE_BypassExternalClockNegativeEdge | Bypassed channel mode - external clock selected, negative edge for registering data by the decimation filter.
kAFE_BypassDisable | Normal channel mode.

## Function Documentation

void AFE_Init | ( | AFE_Type * | base,
|  | const afe_config_t * | config
| ) |  |

This function configures the AFE module for the configuration which are shared by all channels.

Parameters

  base | AFE peripheral base address.
  config | Pointer to structure of "afe_config_t".

void AFE_Deinit | ( | AFE_Type * | base | ) |

This function disables clock.

Parameters

  base | AFE peripheral base address.

void AFE_GetDefaultConfig | ( | afe_config_t * | config | ) |

This function fills the afe_config_t structure with default settings. Defaut value are:

* config->enableLowPower = false;
* config->resultFormat = kAFE_ResultFormatRight;
* config->clockDivider = kAFE_ClockDivider2;
* config->clockSource = kAFE_ClockSource1;
* config->startupCount = 2U;
*

Parameters

  config | Pointer to structure of "afe_config_t".

static void AFE_SoftwareReset | ( | AFE_Type * | base,
|  | bool | enable
| ) |  |

inlinestatic

This function is to reset all the ADCs, PGAs, decimation filters and clock configuration bits. When asserted as "false", all ADCs, PGAs and decimation filters are disabled. Clock Configuration bits are reset. When asserted as "true", all ADCs, PGAs and decimation filters are enabled.

Parameters

  base | AFE peripheral base address.
  enable | Assert the reset command.

static void AFE_Enable | ( | AFE_Type * | base,
|  | bool | enable
| ) |  |

inlinestatic

This function enables AFE and filter.

Parameters

  base | AFE peripheral base address.
  enable | Enable the AFE module or not.

void AFE_SetChannelConfig | ( | AFE_Type * | base,
|  | uint32_t | channel,
|  | const afe_channel_config_t * | config
| ) |  |

This function configures the selected AFE channel.

Parameters

  base | AFE peripheral base address.
  channel | AFE channel index.
  config | Pointer to structure of "afe_channel_config_t".

void AFE_GetDefaultChannelConfig | ( | afe_channel_config_t * | config | ) |

This function fills the afe_channel_config_t structure with default settings. Default value are:

* config->enableHardwareTrigger = false;
* config->enableContinuousConversion = false;
* config->channelMode = kAFE_Normal;
* config->decimatorOversampleRatio = kAFE_DecimatorOversampleRatio64;
* config->pgaGainSelect = kAFE_PgaGain1;
*

Parameters

  config | Pointer to structure of "afe_channel_config_t".

uint32_t AFE_GetChannelConversionValue | ( | AFE_Type * | base,
|  | uint32_t | channel
| ) |  |

This function returns the raw conversion value of the selected channel.

Parameters

  base | AFE peripheral base address.
  channel | AFE channel index.

Returns
  Conversion value.

Note
  The returned value could be left or right adjusted according to the AFE module configuration.

static void AFE_DoSoftwareTriggerChannel | ( | AFE_Type * | base,
|  | uint32_t | mask
| ) |  |

inlinestatic

This function triggers the AFE conversion by executing a software command. It starts the conversion on selected channels if the software trigger option is selected for the channels.

Parameters

  base | AFE peripheral base address.

  mask

  AFE channel mask software trigger. The parameter can be combination of the following source if defined:
  - kAFE_Channel0Trigger
  - kAFE_Channel1Trigger
  - kAFE_Channel2Trigger
  - kAFE_Channel3Trigger

static uint32_t AFE_GetChannelStatusFlags | ( | AFE_Type * | base | ) |

inlinestatic

This function gets all AFE status.

Parameters

  base | AFE peripheral base address.

Returns
  the mask of these status flag bits.

void AFE_SetChannelPhaseDelayValue | ( | AFE_Type * | base,
|  | uint32_t | channel,
|  | uint32_t | value
| ) |  |

This function sets the phase delays for channels. This delay is inserted before the trigger response of the decimation filters. The delay is used to provide a phase compensation between AFE channels in step of prescaled modulator clock periods.

Parameters

  base | AFE peripheral base address.
  channel | AFE channel index.
  value | delay time value.

static void AFE_SetChannelPhasetDelayOk | ( | AFE_Type * | base | ) |

inlinestatic

This function should be called after all desired channel's delay registers are loaded. Values in channel's delay registers are active after calling this function and after the conversation starts.

Parameters

  base | AFE peripheral base address.

static void AFE_EnableChannelInterrupts | ( | AFE_Type * | base,
|  | uint32_t | mask
| ) |  |

inlinestatic

This function enables one channel interrupt.

Parameters

  base | AFE peripheral base address.

  mask

  AFE channel interrupt mask. The parameter can be combination of the following source if defined:
  - kAFE_Channel0InterruptEnable
  - kAFE_Channel1InterruptEnable
  - kAFE_Channel2InterruptEnable
  - kAFE_Channel3InterruptEnable

static void AFE_DisableChannelInterrupts | ( | AFE_Type * | base,
|  | uint32_t | mask
| ) |  |

inlinestatic

This function disables one channel interrupt.

Parameters

  base | AFE peripheral base address.

  mask

  AFE channel interrupt mask. The parameter can be combination of the following source if defined:
  - kAFE_Channel0InterruptEnable
  - kAFE_Channel1InterruptEnable
  - kAFE_Channel2InterruptEnable
  - kAFE_Channel3InterruptEnable

static uint32_t AFE_GetEnabledChannelInterrupts | ( | AFE_Type * | base | ) |

inlinestatic

Parameters

  base | AFE peripheral base address.

Returns
  Return the mask of these interrupt enable/disable bits.

void AFE_EnableChannelDMA | ( | AFE_Type * | base,
|  | uint32_t | mask,
|  | bool | enable
| ) |  |

This function enables/disables one channel DMA request.

Parameters

  base | AFE peripheral base address.
  mask | AFE channel dma mask.

  enable

  Pass true to enable interrupt, false to disable. The parameter can be combination of the following source if defined:
  - kAFE_Channel0DMAEnable
  - kAFE_Channel1DMAEnable
  - kAFE_Channel2DMAEnable
  - kAFE_Channel3DMAEnable