Programming Sequence

  1. 1.Configure the pins for the AFEC.

    By default, Channel 5 of the AFEC0 is configured as analog input and, as a result, does not require further configuration.

    For a better readability, a local definition related to the channel used can be added as follows:
    /*----------------------------------------------------------------------------
     *        Local definitions
     *----------------------------------------------------------------------------*/
    
    
    /** First AFE Channel used*/
    #define TEST_CHANNEL    5
  2. 2.Initialize the AFEC with AFEC_Initialize().
    /**
     *  \brief Initialize AFE.
     *
     */
    static void _afe_initialization(void) 
    {
    	/* Step 1: Configure the pins for AFEC. (If not already done by default) */	
    	/* Step 2: AFEC init */
    	AFEC_Initialize( AFEC0, ID_AFEC0 );
    }
  3. 3.Configure the AFEC_MR.
    By using the macro “AFEC_SetExtModeReg( pAFEC, mode )” defined in the afec.h file, configure the AFEC_MR as follows:
    • Free Run mode disabled
    • Transfer period = 1, where TRANSFER = (Transfer period / AFE clock periods) – 6
    • Tracking time = 2, where TRACKTIM = (Tracking time / AFE clock periods) – 1
    • Conversion time = tAFE_CONV = (6 + 1 + 15) x tAFE_Clock
    • AFEC_MR.ONE = 1
    • Start-up time = 64 (64 periods of AFE clock)

    In this example, we chose to set the tracking time to the maximum value (15). For more details, refer to the AFE Timings in the Electrical Characteristics section of the product datasheet.

    /**
     *  \brief Initialize AFE.
     *
     */
    static void _afe_initialization(void) 
    {
    	/* Step 1: Configure the pins for AFEC. (If not already done by default) */
    	/* Step 2: AFEC init */
    	AFEC_Initialize( AFEC0, ID_AFEC0 );
    	
    	/*Step 3: AFEC Mode Register Configuration */
    	AFEC_SetModeReg(AFEC0,AFEC_MR_FREERUN_OFF|AFEC_MR_TRANSFER(1)|AFEC_MR_TRACKTIM(2)| AFEC_MR_ONE| AFEC_MR_STARTUP_SUT64);
    }
  4. 4.Set the AFEC clock with AFEC_SetClock().
    The AFEC_SetClock function requires three arguments:
    • Afec* pAFEC (pointer to the AFE instance)
    • uint32_t dwPres (Prescale rate selection) used as follow: PRESCAL = fperipheral clock/ fAFE Clock - 1
    • uint32_t dwMck (main board crystal frequency) this value is already defined in the libboard\board.h file (BOARD_MCK).

    A local definition related to the AFE Clock value used can be added as follows:

    /*----------------------------------------------------------------------------
     *        Local definitions
     *----------------------------------------------------------------------------*/
    
    /** IRQ priority for PIO (The lower the value, the greater the priority) */
    #define IRQ_PRIOR_PIO    0
    
    /** LED0 blink time, LED1 blink half this time, in ms */
    #define BLINK_PERIOD        1000
    
    /** First AFE Channel used*/
    #define TEST_CHANNEL    5
    
    /* AFE Clock Value */
    #define AFE_CLK         2200000
    /**
     *  \brief Initialize AFE.
     *
     */
    static void _afe_initialization(void) 
    {
    	/* Step 1: Configure the pins for AFEC. (If not already done by default) */
    	/* Step 2: AFEC init */
    	AFEC_Initialize( AFEC0, ID_AFEC0 );
    	
    	/*Step 3: AFEC Mode Register Configuration */
    	AFEC_SetModeReg(AFEC0,AFEC_MR_FREERUN_ON|AFEC_MR_TRANSFER(1)|AFEC_MR_TRACKTIM(2)| AFEC_MR_ONE| AFEC_MR_STARTUP_SUT64);
    
    	/* Step 4	Set AFEC clock AFEC_SetClock() */
    	AFEC_SetClock( AFEC0, AFE_CLK, BOARD_MCK ) ;
    }
  5. 5.Select the active channel using the macro definition AFEC_EnableChannel().
    /**
     *  \brief Initialize AFE.
     *
     */
    static void _afe_initialization(void) {
    	
    	/* Step 1: Configure the pins for AFEC. (If not already done by default) */
    	/* Step 2: AFEC init */
    	AFEC_Initialize( AFEC0, ID_AFEC0 );
    	
    	/*Step 3: AFEC Mode Register Configuration */
    	AFEC_SetModeReg(AFEC0,AFEC_MR_FREERUN_ON|AFEC_MR_TRANSFER(1)|AFEC_MR_TRACKTIM(2)| AFEC_MR_ONE| AFEC_MR_STARTUP_SUT64);
    
    	/* Step 4	Set AFEC clock AFEC_SetClock() */
    	AFEC_SetClock( AFEC0, AFE_CLK, BOARD_MCK ) ;
    	
    	/* Step 5	Select the channel using the Macro definition AFEC_EnableChannel(). */
    	AFEC_EnableChannel(AFEC0, TEST_CHANNEL);
    }
  6. 6.Adjust the channel offset correction to get a good single-ended result form.

    An offset value set in AFEC_COCR.AOFF defines the analog offset to be used for channel CSEL (configured in the AFEC_CSELR). This value is used as an input value for the DAC included in the AFE.

    A local definition related to the CHANNEL OFFSET value used can be added as follows:

    /*----------------------------------------------------------------------------
     *        Local definitions
     *----------------------------------------------------------------------------*/
    
    /** IRQ priority for PIO (The lower the value, the greater the priority) */
    #define IRQ_PRIOR_PIO    0
    
    /** LED0 blink time, LED1 blink half this time, in ms */
    #define BLINK_PERIOD        1000
    
    /** First AFE Channel used*/
    #define TEST_CHANNEL    5
    
    /** AFE Channel DAC Offset */
    #define CHANNEL_OFFSET	0x200
    /**
     *  \brief Initialize AFE.
     *
     */
    static void _afe_initialization(void)
    {
    	
    	/* Step 1: Configure the pins for AFEC. (If not already done by default) */
    	/* Step 2: AFEC init */
    	AFEC_Initialize( AFEC0, ID_AFEC0 );
    	
    	/*Step 3: AFEC Mode Register Configuration */
    	AFEC_SetModeReg(AFEC0,AFEC_MR_FREERUN_ON|AFEC_MR_TRANSFER(1)|AFEC_MR_TRACKTIM(2)| AFEC_MR_ONE| AFEC_MR_STARTUP_SUT64);
    
    	/* Step 4	Set AFEC clock AFEC_SetClock() */
    	AFEC_SetClock( AFEC0, AFE_CLK, BOARD_MCK ) ;
    	
    	/* Step 5	Select the active channel using the Macro definition AFEC_EnableChannel(). */
    	AFEC_EnableChannel(AFEC0, TEST_CHANNEL);
    	
    	/* Step 6	Adjust the channel level offset */
    	AFEC_SetAnalogOffset(AFEC0, TEST_CHANNEL, CHANNEL_OFFSET); 
    }
  7. 7.Enable the PGA0 and PGA1 of the AFEC0 and adjust the performances by tuning the AFE Bias Current Control.
    /**
     *  \brief Initialize AFE.
     *
     */
    static void _afe_initialization(void)
    {
    	
    	/* Step 1: Configure the pins for AFEC. (If not already done by default) */
    	/* Step 2: AFEC init */
    	AFEC_Initialize( AFEC0, ID_AFEC0 );
    	
    	/*Step 3: AFEC Mode Register Configuration */
    	AFEC_SetModeReg(AFEC0,AFEC_MR_FREERUN_ON|AFEC_MR_TRANSFER(1)|AFEC_MR_TRACKTIM(2)| AFEC_MR_ONE| AFEC_MR_STARTUP_SUT64);
    
    	/* Step 4	Set AFEC clock AFEC_SetClock() */
    	AFEC_SetClock( AFEC0, AFE_CLK, BOARD_MCK ) ;
    	
    	/* Step 5	Select the active channel using the Macro definition AFEC_Enable Channel(). */
    	AFEC_EnableChannel(AFEC0, TEST_CHANNEL);
    	
    	/* Step 6	Adjust the channel level offset */
    	AFEC_SetAnalogOffset(AFEC0, TEST_CHANNEL, CHANNEL_OFFSET);
    	
    	/* Step 7 Enable the PGA 0 and 1 of the AFEC0 and adjust the performances by tuning the AFE Bias Current Control */
    	AFEC_SetAnalogControl(AFEC0, AFEC_ACR_IBCTL(1) | AFEC_ACR_PGA0_ON | AFEC_ACR_PGA1_ON 	);
    }
  8. 8.Configure the Extended Mode Register (AFEC_EMR ) by disabling averaging, enabling the channel tag and using the Single Trigger mode.
    /**
     *  \brief Initialize AFE.
     *
     */
    static void _afe_initialization(void) {
    	
    	/* Step 1: Configure the pins for AFEC. (If not already done by default) */
    	/* Step 2: AFEC init */
    	AFEC_Initialize( AFEC0, ID_AFEC0 );
    	
    	/*Step 3: AFEC Mode Register Configuration */
    	AFEC_SetModeReg(AFEC0,AFEC_MR_FREERUN_ON|AFEC_MR_TRANSFER(1)|AFEC_MR_TRACKTIM(2)| AFEC_MR_ONE| AFEC_MR_STARTUP_SUT64);
    
    	/* Step 4	Set AFEC clock AFEC_SetClock() */
    	AFEC_SetClock( AFEC0, AFE_CLK, BOARD_MCK ) ;
    	
    	/* Step 5	Select the active channel using the Macro definition AFEC_EnableChannel(). */
    	AFEC_EnableChannel(AFEC0, TEST_CHANNEL);
    	
    	/* Step 6	Adjust the channel level offset */
    	AFEC_SetAnalogOffset(AFEC0, TEST_CHANNEL, CHANNEL_OFFSET);
    	
    	/* Step 7 Enable the PGA 0 and 1 of the AFEC0 and adjust the performances by tuning the AFE Bias Current Control */
    	AFEC_SetAnalogControl(AFEC0, AFEC_ACR_IBCTL(1) | AFEC_ACR_PGA0_ON |	AFEC_ACR_PGA1_ON );
    	
    	/* Step 8 Configure the Extended Mode Register by disabling the Averaging, enabling the channel tag and using the Single Trigger Mode */
    	AFEC_SetExtModeReg(AFEC0,0| AFEC_EMR_RES(256)| AFEC_EMR_TAG | AFEC_EMR_STM ); 
    	
    }

    At this point, the configuration of the AFEC is finished. The next steps in the sequence start a conversion and wait for the End Of Conversion flag. This is implemented directly inside the main function as described below.

  9. 9.Start conversion by software trigger.
    /* Step 9: Start Conversion by software trigger */
    AFEC_StartConversion(AFEC0);
  10. 10.Wait for the end of the conversion by polling the status with AFEC_GetStatus().
    /* Step 10: Wait for the end of the conversion by polling status with AFEC_GetStatus() */
    while (!(AFEC_GetStatus(AFEC0) & AFEC_ISR_EOC5));
  11. 11.Get the converted data using AFEC_GetConvertedData() or AFEC_GetLastConvertedData().
    /* Step 11: Finally, get the converted data using AFEC_GetConvertedData() or AFEC_GetLastConvertedData() */
    printf("\n\rCH Voltage(mV) \n\r");
    
    ch =  (AFEC_GetLastConvertedData(AFEC0) & AFEC_LCDR_CHNB_Msk ) >> AFEC_LCDR_CHNB_Pos;
    voltage = ((AFEC_GetLastConvertedData(AFEC0) & AFEC_LCDR_LDATA_Msk)) * 3254/ 4096;
    
    printf("%02u %04u\n\r" ,(unsigned int)ch,(unsigned int)voltage)