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
-
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.
-
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.
-
The start up value can be set by the AFE_DRV_SetStartUpVal20us() function call or by filling the startupCnt parameter of the AfeChnConfig structure.
-
If the phase delay values are configured, call the AFE_DRV_AssertDelayOk() function and pass the instance number.
-
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;
uint8_t startupCnt;
typedef struct AfeChnConfig
{
bool hwTriggerEnable;
bool continuousConvEnable;
int32_t delay;
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:
afeTestStruct.startupCnt = 80;
This example shows how to call the AFE_DRV_Init() given the user configuration structure and the AFE instance 0.
uint32_t afeInstance = 0;
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;
The last part of the initialization contains the start up count setting, delay values confirmation, and the AFE module enabling.
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;
while(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.
|
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...
|
|
|
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...
|
|
|
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...
|
|
This structure keeps the configuration for the AFE channel.
bool afe_chn_config_t::hwTriggerEnable |
bool afe_chn_config_t::continuousConvEnable |
int32_t afe_chn_config_t::delay |
This structure keeps the configuration for the AFE module.
bool afe_user_config_t::lowPowerEnable |
uint8_t afe_user_config_t::startupCnt |
struct afe_delay_config_t |
This structure keeps the phase delay of each AFE channel.
uint32_t afe_delay_config_t::delayChn0 |
uint32_t afe_delay_config_t::delayChn1 |
uint32_t afe_delay_config_t::delayChn2 |
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.
|
Enumerator |
---|
kAfeOverflowFlag |
Indicates if a previous conversion result has not been read and new data has already been arrived.
|
kAfeReadyFlag |
Indicates if a channel is ready to conversion.
|
kAfeConvCompleteFlag |
Indicates if a conversion is completed.
|
This function fills the afe_user_config_t structure with default settings. These setting are:
.lowPowerEnable = false
.startupCnt = 125
- Parameters
-
userConfigPtr | Pointer to structure of "afe_user_config_t". |
- Returns
- Execution status.
This function fills the afe_chn_config_t structure with default settings. These setting are:
.hwTriggerEnable = false
.continuousConvEnable = false
.decimOSR = kAfeDecOsrOf64
.delay = 0
- Parameters
-
userConfigPtr | Pointer to structure of "afe_chn_config_t". |
- Returns
- Execution status.
This function configures the AFE module for the configuration which are shared by all channels.
- Parameters
-
instance | The AFE instance number. |
userConfigPtr | Pointer 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.
This function configures the selected AFE channel.
- Parameters
-
instance | The AFE instance number. |
chn | Channel which will be triggered. |
chnConfigPtr | Pointer 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
-
instance | The AFE instance number. |
enable | Enable (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
-
instance | The AFE instance number. |
chn | Channel 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
-
instance | The AFE instance number. |
chn | Channel 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
-
instance | The AFE instance number. |
chn | Channel(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
-
instance | The AFE instance number. |
chn | The AFE Channel. |
flag | Indicated 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
-
instance | The AFE instance number. |
chn | The 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
-
instance | The AFE instance number. |
chn | The 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
-
instance | The AFE instance number. |
void AFE_DRV_ChnDeinit |
( |
uint32_t |
instance, |
|
|
uint32_t |
chn |
|
) |
| |
De-initializes the selected AFE channel configuration and interrupt.
- Parameters
-
instance | The AFE instance number. |
chn | The 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
-
instance | The AFE instance number. |
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 so 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
-
instance | The AFE instance number. |
delayConfigPtr | Pointer to structure of "afe_delay_config_t". |
AFE_Type* const g_afeBase[] |
const IRQn_Type g_afeIrqId[] |