27 #include <oc_saipll.h> 40 #define _________________________________________MACROS_SECTION_____________________________________________________________________________ 42 #define IsRam(Address) (oC_LSF_IsRamAddress(Address) || oC_LSF_IsExternalAddress(Address)) 43 #define IsRom(Address) oC_LSF_IsRomAddress(Address) 44 #define RCC_CR oC_Register(RCC,RCC_CR) 45 #define RCC_PLLSAICFGR oC_Register(RCC,RCC_PLLSAICFGR) 46 #define RCC_PLLCFGR oC_Register(RCC,RCC_PLLCFGR) 47 #define RCC_DCKCFGR oC_Register(RCC,RCC_DCKCFGR) 48 #define IsOutputLineUsed(OutputLineIndex) (WantedFrequencies[OutputLineIndex] != 0) 49 #define IsAnyOutputLineUsed() (IsOutputLineUsed(oC_SaiPll_OutputLine_LTDC) || \ 50 IsOutputLineUsed(oC_SaiPll_OutputLine_PLL48CLK) || \ 51 IsOutputLineUsed(oC_SaiPll_OutputLine_SAICLK) ) 52 #define SetOutputLineUsed(OutputLineIndex,Freq,PermissibleDifference) \ 53 WantedFrequencies[OutputLineIndex] = Freq;\ 54 PermissibleDifferencies[OutputLineIndex]= PermissibleDifference 55 #define GetPllSaiPDivisor(RegisterValue) ( ((RegisterValue+1)<<1) ) 56 #define GetPllSaiDivRDivisor(RegisterValue) ( 0x2 <<(RegisterValue) ) 57 #define CountFrequency(Input,Divisor) ( (oC_Frequency_t) (((oC_Frequency_t)(Input))/( (oC_Frequency_t)(Divisor) ))) 58 #define IsFrequencyAcceptable(WantedFrequency,PermissibleDifference,Frequency) ( oC_ABS(WantedFrequency,Frequency) <= PermissibleDifference ) 59 #define IsPLLSAIxCorrect(PLLSAIx,OutputLineIndex) ( (PLLSAIx) >= PLLSAIxRanges[OutputLineIndex].Min && (PLLSAIx) <= PLLSAIxRanges[OutputLineIndex].Max ) 60 #define IsPLLSAIDIVxCorrect(PLLSAIDIVx,OutputLineIndex) ( (PLLSAIDIVx) >= PLLSAIDIVxRanges[OutputLineIndex].Min && (PLLSAIDIVx) <= PLLSAIDIVxRanges[OutputLineIndex].Max ) 62 #undef _________________________________________MACROS_SECTION_____________________________________________________________________________ 69 #define _________________________________________TYPES_SECTION______________________________________________________________________________ 135 uint16_t ValueForRegister;
139 #undef _________________________________________TYPES_SECTION______________________________________________________________________________ 146 #define _________________________________________LOCAL_PROTOTYPES_SECTION___________________________________________________________________ 148 static oC_ErrorCode_t ReadCurrentPllConfig (
PllConfig_t * outPllConfig );
154 #undef _________________________________________LOCAL_PROTOTYPES_SECTION___________________________________________________________________ 161 #define _________________________________________VARIABLES_SECTION__________________________________________________________________________ 177 #undef _________________________________________VARIABLES_SECTION__________________________________________________________________________ 185 #define _________________________________________INTERFACE_FUNCTIONS_SECTION________________________________________________________________ 203 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
208 ErrorCondition( Frequency > 0 , oC_ErrorCode_WrongFrequency ) &&
209 ErrorCondition( IsRam(outRealFrequency) , oC_ErrorCode_OutputAddressNotInRAM )
215 oC_AssignErrorCode(&errorCode , ReadCurrentPllConfig(&pllConfig) ) &&
216 oC_AssignErrorCode(&errorCode , FindNewPllConfig(&pllConfig,outputLineIndex,Frequency,PermissibleDifference,outRealFrequency) ) &&
217 oC_AssignErrorCode(&errorCode , ConfigurePll(&pllConfig,OutputLine) )
220 SetOutputLineUsed(outputLineIndex,Frequency,PermissibleDifference);
221 errorCode = oC_ErrorCode_None;
228 #undef _________________________________________INTERFACE_FUNCTIONS_SECTION________________________________________________________________ 235 #define _________________________________________LOCAL_FUNCTIONS_SECTION____________________________________________________________________ 242 static oC_ErrorCode_t ReadCurrentPllConfig(
PllConfig_t * outPllConfig )
244 oC_ErrorCode_t errorCode = oC_ErrorCode_None;
248 outPllConfig->PLLM = RCC_PLLCFGR->PLLM;
249 outPllConfig->PLLSAIN = RCC_PLLSAICFGR->PLLSAIN;
250 outPllConfig->PLLSAIP = RCC_PLLSAICFGR->PLLSAIP;
251 outPllConfig->PLLSAIQ = RCC_PLLSAICFGR->PLLSAIQ;
252 outPllConfig->PLLSAIR = RCC_PLLSAICFGR->PLLSAIR;
254 outPllConfig->PLLSAIDIVQ= RCC_DCKCFGR->PLLSAIDIVQ;
255 outPllConfig->PLLSAIDIVR= RCC_DCKCFGR->PLLSAIDIVR;
256 outPllConfig->PLLSAIDIVP= 1;
284 oC_ASSERT(PllConfig->PLLSAIQ > 0);
285 oC_ASSERT(PllConfig->PLLSAIR > 0);
287 outFrequencies->PllSaiInput = CountFrequency(oscillatorFrequency , PllConfig->PLLM);
288 outFrequencies->VcoOutput = outFrequencies->PllSaiInput * ((
oC_Frequency_t)PllConfig->PLLSAIN);
289 outFrequencies->PLLSAIP = CountFrequency(outFrequencies->VcoOutput , GetPllSaiPDivisor(PllConfig->PLLSAIP));
290 outFrequencies->PLLSAIQ = CountFrequency(outFrequencies->VcoOutput , PllConfig->PLLSAIQ);
291 outFrequencies->PLLSAIR = CountFrequency(outFrequencies->VcoOutput , PllConfig->PLLSAIR);
292 outFrequencies->LTDC = CountFrequency(outFrequencies->PLLSAIR , GetPllSaiDivRDivisor(PllConfig->PLLSAIDIVR));
293 outFrequencies->SAICLK = CountFrequency(outFrequencies->PLLSAIQ , PllConfig->PLLSAIDIVQ + 1);
294 outFrequencies->PLL48CLK = outFrequencies->PLLSAIP;
308 for(uint16_t PLLSAIx = PLLSAIxRanges[OutputLineIndex].Min ; !found && PLLSAIx <= PLLSAIxRanges[OutputLineIndex].Max ; PLLSAIx++)
310 PllConfig->PLLSAIx[OutputLineIndex] = PLLSAIx;
312 for(uint16_t PLLSAIDIVx = PLLSAIDIVxRanges[OutputLineIndex].Min ; PLLSAIDIVx <= PLLSAIDIVxRanges[OutputLineIndex].Max ; PLLSAIDIVx++)
314 PllConfig->PLLSAIDIVx[OutputLineIndex] = PLLSAIDIVx;
316 CountFrequencies(PllConfig , &newFrequencies);
318 if(IsFrequencyAcceptable(Frequency,PermissibleDifference,newFrequencies.OutputLinesFrequencies[OutputLineIndex]))
323 *outRealFrequency = newFrequencies.OutputLinesFrequencies[OutputLineIndex];
340 oC_ErrorCode_t errorCode = oC_ErrorCode_FrequencyNotPossible;
344 if(IsAnyOutputLineUsed() && FindDivisorForOutputLine(PllConfig,OutputLineIndex,Frequency,PermissibleDifference,outRealFrequency))
350 errorCode = oC_ErrorCode_None;
353 if(oC_ErrorOccur(errorCode))
361 for(PllConfig->PLLSAIN = 49 ; PllConfig->PLLSAIN < 433 ; PllConfig->PLLSAIN++)
363 bool foundAll =
true;
367 if(OutputLineIndex == outputLineIndex)
369 foundAll = foundAll && FindDivisorForOutputLine(PllConfig,OutputLineIndex,Frequency,PermissibleDifference,outRealFrequency);
371 else if(IsOutputLineUsed(outputLineIndex))
373 foundAll = foundAll && FindDivisorForOutputLine(PllConfig,outputLineIndex,WantedFrequencies[outputLineIndex],PermissibleDifferencies[outputLineIndex],
NULL);
380 errorCode = oC_ErrorCode_None;
398 oC_ErrorCode_t errorCode = oC_ErrorCode_None;
402 uint16_t PLLSAIx = PllConfig->PLLSAIx[outputLineIndex];
403 uint16_t PLLSAIDIVx = PllConfig->PLLSAIDIVx[outputLineIndex];
405 if( !IsPLLSAIxCorrect(PLLSAIx , outputLineIndex) )
407 errorCode = oC_ErrorCode_MachineCanBeDamaged;
409 if( !IsPLLSAIDIVxCorrect(PLLSAIDIVx , outputLineIndex) )
411 errorCode = oC_ErrorCode_MachineCanBeDamaged;
415 if(!oC_ErrorOccur(errorCode))
419 RCC_CR->PLLSAION = 0;
421 while(RCC_CR->PLLSAIRDY == 1);
423 RCC_PLLSAICFGR->PLLSAIN = PllConfig->PLLSAIN;
424 RCC_PLLSAICFGR->PLLSAIP = PllConfig->PLLSAIP;
425 RCC_PLLSAICFGR->PLLSAIQ = PllConfig->PLLSAIQ;
426 RCC_PLLSAICFGR->PLLSAIR = PllConfig->PLLSAIR;
428 RCC_DCKCFGR->PLLSAIDIVR = PllConfig->PLLSAIDIVR;
429 RCC_DCKCFGR->PLLSAIDIVQ = PllConfig->PLLSAIDIVQ;
433 RCC_DCKCFGR->SAI1SEL = 0;
437 RCC_DCKCFGR->SAI2SEL = 0;
440 RCC_CR->PLLSAION = 1;
442 while(RCC_CR->PLLSAIRDY == 0);
450 #undef _________________________________________LOCAL_FUNCTIONS_SECTION____________________________________________________________________
oC_SaiPll_OutputLine_t
stores selection of the output line
double oC_Frequency_t
type to store frequency
Number of main elements (maximum index) in the type.
The file with interface for LSF module.
Additional definition for SAI2 CLK line.
Output of the PLL connected to the LTDC module (LTDC Clock)
Additional definition for SAI1 CLK line.
The file with LLD interface for the CLOCK driver.
This is the mask to get index of the line from the type.
Contains machine core specific functions.
Output of the PLL connected to the PLL48CLK.
static void oC_MCS_EnterCriticalSection(void)
Enters to critical section.
Static array definitions.
Output of the PLL connected to the SAI.
The file with interface for the machine module.
static bool oC_MCS_ExitCriticalSection(void)
Exits from critical section.
oC_ErrorCode_t oC_SaiPll_Configure(oC_SaiPll_OutputLine_t OutputLine, oC_Frequency_t Frequency, oC_Frequency_t PermissibleDifference, oC_Frequency_t *outRealFrequency)
Configures SAI PLL.
oC_Frequency_t oC_CLOCK_LLD_GetOscillatorFrequency(void)
returns frequency of the oscillator
#define NULL
pointer to a zero