44 #define _________________________________________DEFINITIONS_SECTION________________________________________________________________________ 46 #define RegisterRCC oC_Register( SystemControl , RCC ) 47 #define RegisterRCC2 oC_Register( SystemControl , RCC2 ) 48 #define RegisterRIS oC_Register( SystemControl , RIS ) 49 #define MAX_FREQUENCY oC_MACHINE_MAXIMUM_FREQUENCY 51 #undef _________________________________________DEFINITIONS_SECTION________________________________________________________________________ 58 #define _________________________________________MACROS_SECTION_____________________________________________________________________________ 60 #define IsRam(Address) oC_LSF_IsRamAddress(Address) 61 #define IsRom(Address) oC_LSF_IsRomAddress(Address) 63 #undef _________________________________________MACROS_SECTION_____________________________________________________________________________ 70 #define _________________________________________TYPES_SECTION______________________________________________________________________________ 74 OscSource_MainOsc = 0x0 ,
75 OscSource_PrecisionInternalOsc = 0x1 ,
76 OscSource_PrecisionInternalOscDiv4 = 0x2 ,
77 OscSource_InternalOsc = 0x3 ,
78 OscSource_HibernationOsc = 0x7
92 Xtal_4MHz096kHz = 0x07 ,
93 Xtal_4MHz915200Hz = 0x08 ,
95 Xtal_5MHz120kHz = 0x0A ,
97 Xtal_6MHz144kHz = 0x0C ,
98 Xtal_7MHz372800Hz = 0x0D ,
100 Xtal_8MHz192kHz = 0x0F ,
103 Xtal_12MHz288kHz = 0x12 ,
104 Xtal_13MHz560kHz = 0x13 ,
105 Xtal_14MHz318180Hz = 0x14 ,
107 Xtal_16MHz384kHz = 0x16 ,
136 #undef _________________________________________TYPES_SECTION______________________________________________________________________________ 143 #define _________________________________________LOCAL_PROTOTYPES_SECTION___________________________________________________________________ 145 static bool ConfigureClock ( OscSource_t OscSource , Xtal_t Xtal , Pll_t Pll , SysDiv_t SysDiv );
148 static void CallClockConfiguredEvent (
void );
150 #undef _________________________________________LOCAL_PROTOTYPES_SECTION___________________________________________________________________ 157 #define _________________________________________VARIABLES_SECTION__________________________________________________________________________ 163 [Xtal_4MHz ] = 4000000 ,
164 [Xtal_4MHz096kHz ] = 4096000 ,
165 [Xtal_4MHz915200Hz ] = 4915200 ,
166 [Xtal_5MHz ] = 5000000 ,
167 [Xtal_5MHz120kHz ] = 5120000 ,
168 [Xtal_6MHz ] = 6000000 ,
169 [Xtal_6MHz144kHz ] = 6144000 ,
170 [Xtal_7MHz372800Hz ] = 7372800 ,
171 [Xtal_8MHz ] = 8000000 ,
172 [Xtal_8MHz192kHz ] = 8192000 ,
173 [Xtal_10MHz ] = 10000000 ,
174 [Xtal_12MHz ] = 12000000 ,
175 [Xtal_12MHz288kHz ] = 12288000 ,
176 [Xtal_13MHz560kHz ] = 13560000 ,
177 [Xtal_14MHz318180Hz ] = 14318180 ,
178 [Xtal_16MHz ] = 16000000 ,
179 [Xtal_16MHz384kHz ] = 16384000 ,
180 [Xtal_18MHz ] = 18000000 ,
181 [Xtal_20MHz ] = 20000000 ,
182 [Xtal_24MHz ] = 24000000 ,
183 [Xtal_25MHz ] = 25000000
185 static const oC_Frequency_t InternalOscilatorsFrequenciesArray[] = {
191 #undef _________________________________________VARIABLES_SECTION__________________________________________________________________________ 198 #define _________________________________________INTERFACE_FUNCTIONS_SECTION________________________________________________________________ 211 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
225 ClockConfiguredHandler =
NULL;
228 errorCode = oC_ErrorCode_None;
244 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
254 if(errorCode != oC_ErrorCode_None)
256 errorCode = oC_ErrorCode_CannotRestoreDefaultState;
284 return CurrentFrequency;
293 return MAX_FREQUENCY;
303 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
308 oC_AssignErrorCodeIfFalse(&errorCode , ClockConfiguredHandler ==
NULL , oC_ErrorCode_InterruptHandlerAlreadySet ) &&
309 oC_AssignErrorCodeIfFalse(&errorCode , IsRom(Interrupt) || IsRam(Interrupt) , oC_ErrorCode_WrongEventHandlerAddress )
315 ClockConfiguredHandler = Interrupt;
316 errorCode = oC_ErrorCode_None;
333 oC_UInt_t numberOfCyclesPerMicrosecond = CurrentFrequency/1000000UL;
335 oC_MCS_Delay(numberOfCyclesPerMicrosecond*Microseconds);
348 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
354 oC_AssignErrorCodeIfFalse(&errorCode , TargetFrequency > 0 && PermissibleDifference >= 0 , oC_ErrorCode_WrongFrequency)
358 errorCode = oC_ErrorCode_FrequencyNotPossible;
360 for(OscSource_t oscSource =OscSource_PrecisionInternalOsc;
361 (oscSource <= OscSource_InternalOsc) && (errorCode != oC_ErrorCode_None)
364 for(Pll_t pll = Pll_DontUse ; pll <= Pll_400MHz ; pll++)
366 SysDiv_t sysDiv = SysDiv_1;
368 if(FindDivisor(TargetFrequency,PermissibleDifference,InternalOscilatorsFrequenciesArray[oscSource],pll,&sysDiv))
370 if(ConfigureClock(oscSource , xtal , pll , sysDiv))
374 case Pll_DontUse: CurrentFrequency = InternalOscilatorsFrequenciesArray[oscSource] / (sysDiv + 1);
break;
375 case Pll_200MHz: CurrentFrequency = oC_MHz(200) / (sysDiv + 1);
break;
376 case Pll_400MHz: CurrentFrequency = oC_MHz(400) / (sysDiv + 1);
break;
379 errorCode = oC_ErrorCode_None;
380 CallClockConfiguredEvent();
385 errorCode = oC_ErrorCode_ClockConfigurationError;
390 if(oscSource != OscSource_PrecisionInternalOsc)
411 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
412 Xtal_t xtal = GetXtalForFrequency(OscillatorFrequency , 0);
417 oC_AssignErrorCodeIfFalse(&errorCode , TargetFrequency > 0 && OscillatorFrequency > 0 && PermissibleDifference >= 0 , oC_ErrorCode_WrongFrequency) &&
418 oC_AssignErrorCodeIfFalse(&errorCode , xtal != Xtal_NotCorrect , oC_ErrorCode_UnsupportedOscillator)
421 errorCode = oC_ErrorCode_FrequencyNotPossible;
425 for(Pll_t pll = Pll_DontUse ; pll <= Pll_400MHz ; pll++)
427 SysDiv_t sysDiv = SysDiv_1;
429 if(FindDivisor(TargetFrequency,PermissibleDifference,OscillatorFrequency,pll,&sysDiv))
431 if(ConfigureClock(OscSource_MainOsc , xtal , pll , sysDiv))
435 case Pll_DontUse: CurrentFrequency = OscillatorFrequency / (sysDiv + 1);
break;
436 case Pll_200MHz: CurrentFrequency = oC_MHz(200) / (sysDiv + 1);
break;
437 case Pll_400MHz: CurrentFrequency = oC_MHz(400) / (sysDiv + 1);
break;
441 errorCode = oC_ErrorCode_None;
442 CallClockConfiguredEvent();
447 errorCode = oC_ErrorCode_ClockConfigurationError;
467 oC_ErrorCode_t errorCode = oC_ErrorCode_ImplementError;
472 oC_AssignErrorCodeIfFalse(&errorCode , TargetFrequency > 0 && OscillatorFrequency > 0 && PermissibleDifference >= 0 , oC_ErrorCode_WrongFrequency) &&
478 SysDiv_t sysDiv = SysDiv_1;
480 if(FindDivisor(TargetFrequency,PermissibleDifference,OscillatorFrequency,Pll_DontUse,&sysDiv))
482 if(ConfigureClock(OscSource_HibernationOsc , xtal , Pll_DontUse , sysDiv))
484 CurrentFrequency = OscillatorFrequency / (sysDiv + 1);
486 errorCode = oC_ErrorCode_None;
487 CallClockConfiguredEvent();
491 errorCode = oC_ErrorCode_ClockConfigurationError;
496 errorCode = oC_ErrorCode_FrequencyNotPossible;
504 #undef _________________________________________INTERFACE_FUNCTIONS_SECTION________________________________________________________________ 512 #define _________________________________________LOCAL_FUNCTIONS_SECTION____________________________________________________________________ 519 static bool ConfigureClock( OscSource_t OscSource , Xtal_t Xtal , Pll_t Pll , SysDiv_t SysDiv )
525 rcc.Value = RegisterRCC->Value;
526 rcc2.Value = RegisterRCC2->Value;
537 RegisterRCC->Value = rcc.Value;
546 if(OscSource < OscSource_HibernationOsc)
548 rcc.OSCSRC = OscSource;
551 rcc2.OSCSRC2 = OscSource;
556 RegisterRCC->Value = rcc.Value;
557 RegisterRCC2->Value = rcc2.Value;
563 if(Pll == Pll_DontUse)
565 if(SysDiv <= SysDiv_16)
568 rcc2.SYSDIV2 = SysDiv;
570 else if(SysDiv <= SysDiv_64)
572 rcc2.SYSDIV2 = SysDiv;
580 else if(Pll == Pll_200MHz)
584 if(SysDiv <= SysDiv_2)
588 else if(SysDiv <= SysDiv_16)
591 rcc2.SYSDIV2 = SysDiv;
593 else if(SysDiv < SysDiv_64)
595 rcc2.SYSDIV2 = SysDiv;
608 if(SysDiv <= SysDiv_4)
614 rcc2.SYSDIV2 = SysDiv >> 1;
615 rcc2.SYSDIV2LSB = SysDiv % 2;
620 RegisterRCC->Value = rcc.Value;
621 RegisterRCC2->Value = rcc2.Value;
628 if(Pll != Pll_DontUse)
630 while(!RegisterRIS->PLLLRIS);
635 RegisterRCC->BYPASS = 0;
636 RegisterRCC2->BYPASS2 = 0;
658 for( Xtal_t x = Xtal_4MHz ; x <= Xtal_25MHz ; x++ )
660 if(
oC_ABS( XtalFrequencyArray[x] , Frequency ) <= AcceptableDifference )
681 SysDiv_t startSysDiv = SysDiv_1;
682 SysDiv_t endSysDiv = SysDiv_64;
684 if(Pll == Pll_DontUse)
686 frequencyToDivide = OscillatorFrequency;
687 startSysDiv = SysDiv_1;
688 endSysDiv = SysDiv_64;
690 else if(Pll == Pll_200MHz)
692 frequencyToDivide = oC_MHz(200);
693 startSysDiv = SysDiv_3;
694 endSysDiv = SysDiv_64;
696 else if(Pll == Pll_400MHz)
698 frequencyToDivide = oC_MHz(400);
699 startSysDiv = SysDiv_5;
700 endSysDiv = SysDiv_128;
703 for(SysDiv_t sysDiv = startSysDiv ; sysDiv <= endSysDiv ; sysDiv++ )
708 if(Pll == Pll_400MHz && sysDiv == SysDiv_7)
714 if(
oC_ABS(mainOscFrequency,Frequency) <= Difference)
730 static void CallClockConfiguredEvent(
void )
732 if( ClockConfiguredHandler)
734 ClockConfiguredHandler(CurrentFrequency);
739 #undef _________________________________________LOCAL_FUNCTIONS_SECTION____________________________________________________________________ The file with interface for registers module.
oC_ErrorCode_t oC_CLOCK_LLD_TurnOffDriver(void)
release the driver
double oC_Frequency_t
type to store frequency
oC_ErrorCode_t oC_CLOCK_LLD_SetClockConfiguredInterrupt(oC_CLOCK_LLD_Interrupt_t Interrupt)
configures an interrupt for clock configured event
oC_CLOCK_LLD_ClockSource_t oC_CLOCK_LLD_GetClockSource(void)
returns source of the system clock
oC_CLOCK_LLD_ClockSource_t
type for storing source of the system clock
The file with interface for LSF module.
oC_Frequency_t oC_CLOCK_LLD_GetMaximumClockFrequency(void)
returns maximum frequency permissible for the machine
oC_ErrorCode_t oC_CLOCK_LLD_ConfigureInternalClock(oC_Frequency_t TargetFrequency, oC_Frequency_t PermissibleDifference)
configures system clock in internal mode
bool oC_MEM_LLD_IsFlashAddress(const void *Address)
checks if the pointer is in flash section
an external source of the clock is used
The file with LLD interface for the MEM driver.
#define oC_Machine_DefaultFrequency
returns default frequency of the machine clock
an internal clock source is used
void oC_MCS_Delay(register oC_UInt_t Cycles)
delays operations for cycles
The file with interface for the module library.
oC_Frequency_t oC_CLOCK_LLD_GetClockFrequency(void)
returns frequency of the system clock
oC_ErrorCode_t oC_CLOCK_LLD_TurnOnDriver(void)
initializes the driver to work
The file with LLD interface for the CLOCK driver.
oC_ErrorCode_t oC_CLOCK_LLD_ConfigureExternalClock(oC_Frequency_t TargetFrequency, oC_Frequency_t PermissibleDifference, oC_Frequency_t OscillatorFrequency)
configures system clock to work in external mode
static void oC_Module_TurnOn(oC_Module_t Module)
sets module as turned on
Contains machine core specific functions.
#define oC_MACHINE_HIBERNATION_OSCILLATOR_FREQUENCY
#define oC_MACHINE_PRECISION_INTERNAL_OSCILLATOR_FREQUENCY
static void oC_MCS_EnterCriticalSection(void)
Enters to critical section.
oC_ErrorCode_t oC_CLOCK_LLD_ConfigureHibernationClock(oC_Frequency_t TargetFrequency, oC_Frequency_t PermissibleDifference, oC_Frequency_t OscillatorFrequency)
configures system clock to work in hibernation mode
The file with interface interrupt module.
static bool oC_Module_TurnOffVerification(oC_ErrorCode_t *outErrorCode, oC_Module_t Module)
verify if module is turned off
void(* oC_CLOCK_LLD_Interrupt_t)(oC_Frequency_t Frequency)
type for storing interrupts pointers
static bool oC_MCS_ExitCriticalSection(void)
Exits from critical section.
static bool oC_Module_TurnOnVerification(oC_ErrorCode_t *outErrorCode, oC_Module_t Module)
verify if module is turned on
#define oC_RegisterType_(REGISTER_NAME)
Returns name of type for storing register value.
The file with LLD interface for the SYS driver.
bool oC_CLOCK_LLD_DelayForMicroseconds(oC_UInt_t Microseconds)
perform a delay for us
#define oC_MACHINE_INTERNAL_OSCILLATOR_FREQUENCY
bool oC_MEM_LLD_IsRamAddress(const void *Address)
checks if the pointer is in ram section
#define NULL
pointer to a zero
static void oC_Module_TurnOff(oC_Module_t Module)
sets module as turned off