BLDC control on ATAVRMC303 with ATxMega128A1
clksys_driver.c
Go to the documentation of this file.
1 /* This file has been prepared for Doxygen automatic documentation generation.*/
68 #include "clksys_driver.h"
69 
70 void CCPWrite( volatile uint8_t * address, uint8_t value )
71 {
72 #ifdef __ICCAVR__
73  asm("movw r30, r16");
74 #ifdef RAMPZ
75  RAMPZ = 0;
76 #endif
77  asm("ldi r16, 0xD8 \n"
78  "out 0x34, r16 \n"
79  "st Z, r18 \n");
80 
81 #elif defined __GNUC__
82  volatile uint8_t * tmpAddr = address;
83 #ifdef RAMPZ
84  RAMPZ = 0;
85 #endif
86  asm volatile(
87  "movw r30, %0" "\n\t"
88  "ldi r16, %2" "\n\t"
89  "out %3, r16" "\n\t"
90  "st Z, %1"
91  :
92  : "r" (tmpAddr), "r" (value), "M" (CCP_IOREG_gc), "m" (CCP)
93  : "r16", "r30", "r31"
94  );
95 
96 #endif
97 }
98 
99 
108 extern void CCPWrite( volatile uint8_t __near * address, uint8_t value );
109 
110 
125 void CLKSYS_XOSC_Config( OSC_FRQRANGE_t freqRange,
126  bool lowPower32kHz,
127  OSC_XOSCSEL_t xoscModeSelection )
128 {
129  OSC.XOSCCTRL = (uint8_t) freqRange |
130  ( lowPower32kHz ? OSC_X32KLPM_bm : 0 ) |
131  xoscModeSelection;
132 }
133 
134 
151 void CLKSYS_PLL_Config( OSC_PLLSRC_t clockSource, uint8_t factor )
152 {
153  factor &= OSC_PLLFAC_gm;
154  OSC.PLLCTRL = (uint8_t) clockSource | ( factor << OSC_PLLFAC_gp );
155 }
156 
157 
171 uint8_t CLKSYS_Disable( uint8_t oscSel )
172 {
173  OSC.CTRL &= ~oscSel;
174  uint8_t clkEnabled = OSC.CTRL & oscSel;
175  return clkEnabled;
176 }
177 
178 
190 void CLKSYS_Prescalers_Config( CLK_PSADIV_t PSAfactor,
191  CLK_PSBCDIV_t PSBCfactor )
192 {
193  uint8_t PSconfig = (uint8_t) PSAfactor | PSBCfactor;
194  CCPWrite( &CLK.PSCTRL, PSconfig );
195 }
196 
197 
209 uint8_t CLKSYS_Main_ClockSource_Select( CLK_SCLKSEL_t clockSource )
210 {
211  uint8_t clkCtrl = ( CLK.CTRL & ~CLK_SCLKSEL_gm ) | clockSource;
212  CCPWrite( &CLK.CTRL, clkCtrl );
213  clkCtrl = ( CLK.CTRL & clockSource );
214  return clkCtrl;
215 }
216 
217 
225 void CLKSYS_RTC_ClockSource_Enable( CLK_RTCSRC_t clockSource )
226 {
227  CLK.RTCCTRL = ( CLK.RTCCTRL & ~CLK_RTCSRC_gm ) |
228  clockSource |
229  CLK_RTCEN_bm;
230 }
231 
232 
244 void CLKSYS_AutoCalibration_Enable( uint8_t clkSource, bool extReference )
245 {
246  OSC.DFLLCTRL = ( OSC.DFLLCTRL & ~clkSource ) |
247  ( extReference ? clkSource : 0 );
248  if (clkSource == OSC_RC2MCREF_bm) {
249  DFLLRC2M.CALA |= DFLL_ENABLE_bm;
250  } else if (clkSource == OSC_RC2MCREF_bm) {
251  DFLLRC32M.CALA |= DFLL_ENABLE_bm;
252  }
253 }
254 
255 
265 {
266  CCPWrite( &OSC.XOSCFAIL, ( OSC_XOSCFDIF_bm | OSC_XOSCFDEN_bm ) );
267 }
268 
269 
277 {
278  CCPWrite( &CLK.LOCK, CLK_LOCK_bm );
279 }
280 
void CLKSYS_Configuration_Lock(void)
This function lock the entire clock system configuration.
void CCPWrite(volatile uint8_t *address, uint8_t value)
Definition: clksys_driver.c:70
void CLKSYS_Prescalers_Config(CLK_PSADIV_t PSAfactor, CLK_PSBCDIV_t PSBCfactor)
This function changes the prescaler configuration.
void CLKSYS_XOSC_Config(OSC_FRQRANGE_t freqRange, bool lowPower32kHz, OSC_XOSCSEL_t xoscModeSelection)
This function configures the external oscillator.
void CLKSYS_XOSC_FailureDetection_Enable(void)
This function enables the External Oscillator Failure Detection (XOSCFD) feature. ...
void CLKSYS_PLL_Config(OSC_PLLSRC_t clockSource, uint8_t factor)
This function configures the internal high-frequency PLL.
uint8_t CLKSYS_Main_ClockSource_Select(CLK_SCLKSEL_t clockSource)
This function selects the main system clock source.
void CLKSYS_AutoCalibration_Enable(uint8_t clkSource, bool extReference)
This function enables automatic calibration of the selected internal oscillator.
void CLKSYS_RTC_ClockSource_Enable(CLK_RTCSRC_t clockSource)
This function selects a Real-Time Counter clock source.
XMEGA Clock System driver header file.
uint8_t CLKSYS_Disable(uint8_t oscSel)
This function disables the selected oscillator.