Kinetis SDK v.1.3 API Reference Manual  Rev. 0
Freescale Semiconductor, Inc.
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
AFE Peripheral driver

Overview

This section describes the programming interface of the AFE Peripheral driver. The AFE peripheral driver provides functions to initialize AFE module and channels, conversion triggering, and result reading.

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 phase delay compensation, AFE channel mode, AFE channel analog gain, AFE channel oversampling ration, and AFE channel interrupt or DMA function. 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_user_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.

Initialization

  1. To initialize the AFE driver, call the AFE_DRV_Init() function and pass the instance number of the AFE peripheral and a pointer to the user configuration structure. For a typical use case, call the AFE_DRV_StructInitUserConfigDefault() function which populates the structure.
  2. Then, call the AFE_DRV_ChnInit function and pass the instance number, channel number and a pointer to the channel configuration structure. For a typical use case, call the AFE_DRV_StructInitChnConfigDefault() function which populates the structure.
  3. The start up value can be set by the AFE_DRV_SetStartUpVal20us() function call or by filling the startupCnt parameter of the AfeChnConfig structure.
  4. If the phase delay values are configured, call the AFE_DRV_AssertDelayOk() function and pass the instance number.
  5. Finally, call the AFE_DRV_Enable() function and pass the instance number and a true boolean parameter. Ensure that the clock and VREF are configured properly before configuring the AFE. For more information, see a VREF and documentation for the KSDK Clock Manager.
typedef struct AfeUserConfig
{
bool lowPowerEnable;

This is an example of a typical AFE configuration. The afe_user_config_t instantiation is configured in the normal mode (low power mode disabled), result format is justified to the right, clock divider is selected 2 and a first clock source is selected (see the microcontroller documentation for information about these selections). The afe_chn_config_t instantiation is configured in the normal mode (no bypass), a continuous conversion, a zero delay, no interrupt or DMA request, the software trigger enabled, and the PGA disabled and the OSR is 2048.

This is an example code to set up a user and a channel AFE configuration instantiation:

afeConfig.lowPowerEnable = false;
afeTestStruct.startupCnt = 80; // startupCnt = (Clk_freq/Clk_div)*20e-6
afe_chn_config_t afeChnConfig;
afeChnConfig.chnMode = kAfeNormal;
afeChnConfig.continuousConvEnable = true;
afeChnConfig.delay = 0;
afeChnConfig.chnEvent = kAfeNoneReq;
afeChnConfig.hwTriggerEnable = false;
afeChnConfig.pgaGainSel = kAfePgaDisable;

This example shows how to call the AFE_DRV_Init() given the user configuration structure and the AFE instance 0.

uint32_t afeInstance = 0;
UART_DRV_Init(afeInstance, &afeConfig);

This example shows how to call the AFE_DRV_ChnInit() given the channel configuration structure and the AFE channel 0 and 1.

uint32_t afeChannel0 = 0;
uint32_t afeChannel1 = 1;
AFE_DRV_ChnInit(afeInstance, afeChannel0, &afeChnConfig); // Configure AFE channel 0
AFE_DRV_ChnInit(afeInstance, afeChannel1, &afeChnConfig); // Configure AFE channel 1

The last part of the initialization contains the start up count setting, delay values confirmation, and the AFE module enabling.

AFE_DRV_AssertDelayOk(afeInstance); // Assert delay for all channels simultaneously
AFE_DRV_Enable(afeInstance, true); // Enable all configured channels

Measuring

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 is a false value), call the AFE_DRV_SoftTriggerConv() function to start conversion. In this example, the channel 0 and channel 1 are triggered simultaneously. After the conversion is completed, the results are read.

int32_t res0,res1;
AFE_DRV_SoftTriggerConv(afeInstance, CHN_TRIG_MASK(0) | CHN_TRIG_MASK(1));
while(1)
{
AFE_DRV_WaitConvDone(afeInstance, 0);
AFE_DRV_WaitConvDone(afeInstance, 1);
res0 = AFE_DRV_GetChnConvValue(afeInstance, 0);
res1 = AFE_DRV_GetChnConvValue(afeInstance, 1);
}

If the hardware triggering is enabled (hwTriggerEnable parameter in afe_chn_config is a true value), the AFE_DRV_SoftTriggerConv() function isn't called. The rest of the code is the same.

If the continuous conversion isn't allowed (continuousConvEnable parameter in afe_chn_config is a false value), call the AFE_DRV_SoftTriggerConv() function (in the software trigger case) in the main loop to trigger the next conversion.

Data Structures

struct  afe_chn_config_t
 Defines the structure to initialize the AFE channel. More...
 
struct  afe_user_config_t
 Defines the structure to initialize the AFE module. More...
 
struct  afe_delay_config_t
 Defines the structure to configure phase delay of each AFE channel. More...
 

Enumerations

enum  afe_pga_state_t {
  kAfePgaDisable = 0U,
  kAfePgaGain1 = 1U,
  kAfePgaGain2 = 2U,
  kAfePgaGain4 = 3U,
  kAfePgaGain8 = 4U,
  kAfePgaGain16 = 5U,
  kAfePgaGain32 = 6U
}
 Defines the PGA's values. More...
 
enum  afe_chn_mode_t {
  kAfeNormal = 0,
  kAfeBypassExternCltPosEdge = 1,
  kAfeBypassExternClkNegEdge = 2,
  kAfeBypassInternClkPosEdge = 3,
  kAfeBypassInternClkNegEdge = 4
}
 Defines the channel's modes. More...
 
enum  afe_chn_event_t {
  kAfeNoneReq = 0,
  kAfeIntReq = 1,
  kAfeDmaReq = 2
}
 Defines the channel's event requests. More...
 
enum  afe_flag_t {
  kAfeOverflowFlag = 0U,
  kAfeReadyFlag = 1U,
  kAfeConvCompleteFlag = 2U
}
 Defines the type of event flags. More...
 

Functions

afe_status_t AFE_DRV_StructInitUserConfigDefault (afe_user_config_t *userConfigPtr)
 Fills the user configure structure. More...
 
afe_status_t AFE_DRV_StructInitChnConfigDefault (afe_chn_config_t *chnConfigPtr)
 Fills the channel configuration structure. More...
 
afe_status_t AFE_DRV_Init (uint32_t instance, afe_user_config_t *userConfigPtr)
 Initializes the AFE module. More...
 
afe_status_t AFE_DRV_ChnInit (uint32_t instance, uint32_t chn, afe_chn_config_t *chnConfigPtr)
 Initializes the selected AFE channel. More...
 
void AFE_DRV_Enable (uint32_t instance, bool enable)
 Enables/disables all configured AFE channels. More...
 
void AFE_DRV_WaitConvDone (uint32_t instance, uint32_t chn)
 Waits until the last conversion is complete. More...
 
void AFE_DRV_WaitChnReady (uint32_t instance, uint32_t chn)
 Waits until the channel is ready for conversion. More...
 
void AFE_DRV_SoftTriggerConv (uint32_t instance, uint32_t chnMask)
 Triggers the AFE conversion by software. More...
 
bool AFE_DRV_GetChnFlag (uint32_t instance, uint32_t chn, afe_flag_t flag)
 Gets the flag for channel's events. More...
 
uint32_t AFE_DRV_GetChnConvValRaw (uint32_t instance, uint32_t chn)
 Reads the conversion value in a raw form. More...
 
int32_t AFE_DRV_GetChnConvVal (uint32_t instance, uint32_t chn)
 Reads the conversion value in 2's complement form. More...
 
void AFE_DRV_Deinit (uint32_t instance)
 De-initializes the AFE module. More...
 
void AFE_DRV_ChnDeinit (uint32_t instance, uint32_t chn)
 De-initializes the selected AFE channel. More...
 
void AFE_DRV_AssertDelayOk (uint32_t instance)
 Asserts the phase delay setting. More...
 
void AFE_DRV_SetPhaseDelays (uint32_t instance, afe_delay_config_t *delayConfigPtr)
 Sets phase delays. More...
 

Variables

AFE_Type *const g_afeBase []
 Table of base addresses for AFE instances. More...
 
const IRQn_Type g_afeIrqId []
 Table to save AFE IRQ enumeration numbers defined in CMSIS header file. More...
 

Data Structure Documentation

struct afe_chn_config_t

This structure keeps the configuration for the AFE channel.

Data Fields

bool hwTriggerEnable
 Enable triggering by hardware.
 
bool continuousConvEnable
 Enable continuous conversion mode.
 
uint32_t delay
 Set the phase compensation.
 
afe_chn_mode_t chnMode
 Select if channel is in bypassed mode.
 
afe_pga_state_t pgaGainSel
 Select the analog gain applied to the input signal.
 
afe_chn_osr_mode_t decimOSR
 Select the over sampling ration.
 
afe_chn_event_t chnEvent
 Select DMA or interrupt function.
 
struct afe_user_config_t

This structure keeps the configuration for the AFE module.

Data Fields

bool lowPowerEnable
 Enable low power mode.
 
afe_result_format_mode_t resultFormat
 Select the result format.
 
afe_clk_divider_mode_t clkDividerMode
 Select the clock divider ration for the modulator clock.
 
afe_clk_src_mode_t clkSrcMode
 Select clock source for modulator clock.
 
uint8_t startupCnt
 Select the start up delay of modulators.
 
struct afe_delay_config_t

This structure keeps the phase delay of each AFE channel.

Data Fields

uint32_t delayChn0
 Phase compensation of channel0. More...
 
uint32_t delayChn1
 Phase compensation of channel1. More...
 
uint32_t delayChn2
 Phase compensation of channel2. More...
 

Field Documentation

uint32_t afe_delay_config_t::delayChn0
uint32_t afe_delay_config_t::delayChn1
uint32_t afe_delay_config_t::delayChn2

Enumeration Type Documentation

Enumerator
kAfePgaDisable 

PGA disabled.

kAfePgaGain1 

Input gained by 1.

kAfePgaGain2 

Input gained by 2.

kAfePgaGain4 

Input gained by 4.

kAfePgaGain8 

Input gained by 8.

kAfePgaGain16 

Input gained by 16.

kAfePgaGain32 

Input gained by 32.

Enumerator
kAfeNormal 

Normal channel mode.

kAfeBypassExternCltPosEdge 

Bypassed channel mode - external clock selected, positive edge for registering data by the decimation filter.

kAfeBypassExternClkNegEdge 

Bypassed channel mode - external clock selected, negative edge for registering data by the decimation filter.

kAfeBypassInternClkPosEdge 

Bypassed channel mode - internal clock selected, positive edge for registering data by the decimation filter.

kAfeBypassInternClkNegEdge 

Bypassed channel mode - external clock selected, negative edge for registering data by the decimation filter.

Enumerator
kAfeNoneReq 

None request is enabled if conversion is completed.

kAfeIntReq 

Interrupt request is enabled if conversion is completed.

kAfeDmaReq 

DMA request is enabled if conversion is completed.

enum afe_flag_t
Enumerator
kAfeOverflowFlag 

Indicates if a previous conversion result has not been read and new data has already arrived.

kAfeReadyFlag 

Indicates whether a channel is ready to conversion.

kAfeConvCompleteFlag 

Indicates whether a conversion is complete.

Function Documentation

afe_status_t AFE_DRV_StructInitUserConfigDefault ( afe_user_config_t userConfigPtr)

This function fills the afe_user_config_t structure with default settings. These setting are:

.lowPowerEnable = false
.resultFormat = kAfeResultFormatLeft
.clkDividerMode = kAfeClkDividerInputOf2
.clkSrcMode = kAfeClkSrcClk0
.startupCnt = 125
Parameters
userConfigPtrPointer to structure of "afe_user_config_t".
Returns
Execution status.
afe_status_t AFE_DRV_StructInitChnConfigDefault ( afe_chn_config_t chnConfigPtr)

This function fills the afe_chn_config_t structure with default settings. These setting are:

.hwTriggerEnable = false
.continuousConvEnable = false
.chnMode = kAfeNormal
.decimOSR = kAfeDecOsrOf64
.delay = 0
.pgaGainSel = kAfePgaGain1
.chnEvent = kAfeNoneReq
Parameters
chnConfigPtrPointer to structure of "afe_chn_config_t".
Returns
Execution status.
afe_status_t AFE_DRV_Init ( uint32_t  instance,
afe_user_config_t userConfigPtr 
)

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

Parameters
instanceThe AFE instance number.
userConfigPtrPointer to structure of "afe_user_config_t". If startupCnt parameter is less than two, this value is calculated according to equation Statup_cnt = (clk_freq/clk_div)*20e-6.
Returns
Execution status.
afe_status_t AFE_DRV_ChnInit ( uint32_t  instance,
uint32_t  chn,
afe_chn_config_t chnConfigPtr 
)

This function configures the selected AFE channel.

Parameters
instanceThe AFE instance number.
chnChannel which will be triggered.
chnConfigPtrPointer to structure of "afe_chn_config_t".
Returns
Execution status.
void AFE_DRV_Enable ( uint32_t  instance,
bool  enable 
)

This function enables or disables all channels whose SD_MOD_EN and DEC_EN bits are asserted.

Parameters
instanceThe AFE instance number.
enableEnable (true) or disable (false) all ADCs and filters.
void AFE_DRV_WaitConvDone ( uint32_t  instance,
uint32_t  chn 
)

This function holds the program until the last conversion is complete.

Parameters
instanceThe AFE instance number.
chnChannel which is triggered.
void AFE_DRV_WaitChnReady ( uint32_t  instance,
uint32_t  chn 
)

This function holds the program until the channel is ready for conversion.

Parameters
instanceThe AFE instance number.
chnChannel which is triggered.
void AFE_DRV_SoftTriggerConv ( uint32_t  instance,
uint32_t  chnMask 
)

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
instanceThe AFE instance number.
chnMaskChannel(s) mask which is triggered.
bool AFE_DRV_GetChnFlag ( uint32_t  instance,
uint32_t  chn,
afe_flag_t  flag 
)

This function returns the selected flag of the desired channel.

Parameters
instanceThe AFE instance number.
chnThe AFE Channel.
flagIndicated event, see to "afe_flag_t".
Returns
Assertion of indicated event.
uint32_t AFE_DRV_GetChnConvValRaw ( uint32_t  instance,
uint32_t  chn 
)

This function returns the conversion value of the selected channel. The returned value could be left or right adjusted according to the AFE module configuration.

Parameters
instanceThe AFE instance number.
chnThe AFE Channel.
Returns
Conversion value.
int32_t AFE_DRV_GetChnConvVal ( uint32_t  instance,
uint32_t  chn 
)

This function returns the conversion value of the selected channel. The returned value is in the twos complement (right adjusted) format.

Parameters
instanceThe AFE instance number.
chnThe AFE Channel.
Returns
Conversion value in 2's complement format.
void AFE_DRV_Deinit ( uint32_t  instance)

When the AFE is no longer used, calling this function shuts down the device to reduce power consumption.

Parameters
instanceThe AFE instance number.
void AFE_DRV_ChnDeinit ( uint32_t  instance,
uint32_t  chn 
)

De-initializes the selected AFE channel configuration and interrupt.

Parameters
instanceThe AFE instance number.
chnThe AFE Channel.
void AFE_DRV_AssertDelayOk ( uint32_t  instance)

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
instanceThe AFE instance number.
void AFE_DRV_SetPhaseDelays ( uint32_t  instance,
afe_delay_config_t delayConfigPtr 
)

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. The DelayOk bit is asserted in this function and the 'AFE_DRV_AssertDelayOk()' function doesn't have to be called. The delays for each channel are stored in a 'afe_delay_config_t' structure.

Parameters
instanceThe AFE instance number.
delayConfigPtrPointer to structure of "afe_delay_config_t".

Variable Documentation

AFE_Type* const g_afeBase[]
const IRQn_Type g_afeIrqId[]