AFEC Clock and PRESCAL

The AFE clock frequency is selected in the PRESCAL field of the AFEC_MR.

PRESCAL must be programmed to provide the AFE clock frequency. As given in the section “Electrical Characteristics” of the product datasheet, the AFE is able to run at up to 40 MHz for maximum of 2 MSps bit-rate.

The AFE clock frequency ranges from fperipheral clock/2 if PRESCAL is 1, and fperipheral clock/512 if PRESCAL is set to 255 (0xFF).

Code example to set the PRESCAL value:

/**
 * \brief Set AFE clock.
 *
 * \param pAFE Pointer to an AFE instance.
 * \param dwPres prescale value
 * \param dwMck Board MCK (Hz)
 *
 * \return AFE clock
 */

extern uint32_t AFEC_SetClock( Afec* pAFE, uint32_t dwClk, uint32_t dwMck )
{ 
	uint32_t dwPres, dwMr;
	/* Formula for PRESCAL is: PRESCAL = peripheral clock/ fAFE Clock - 1 */

	dwPres = (dwMck) / (dwClk ) - 1;
	dwMr = AFEC_MR_PRESCAL(dwPres);
	if (dwMr == 0) return 0;

	dwMr |= (pAFE->AFEC_MR & ~AFEC_MR_PRESCAL_Msk);
	pAFE->AFEC_MR = dwMr;
	dwAFEClock = dwMck / (dwPres + 1);
	return dwAFEClock;
}