📄 sysctl.c
字号:
//
// Set the requested system divider and disable the appropriate
// oscillators. This will not get written immediately.
//
ulRCC &= ~(SYSCTL_RCC_SYSDIV_M | SYSCTL_RCC_USESYSDIV |
SYSCTL_RCC_IOSCDIS | SYSCTL_RCC_MOSCDIS);
ulRCC |= ulConfig & (SYSCTL_RCC_SYSDIV_M | SYSCTL_RCC_USESYSDIV |
SYSCTL_RCC_IOSCDIS | SYSCTL_RCC_MOSCDIS);
ulRCC2 &= ~(SYSCTL_RCC2_SYSDIV2_M);
ulRCC2 |= ulConfig & SYSCTL_RCC2_SYSDIV2_M;
//
// See if the PLL output is being used to clock the system.
//
if(!(ulConfig & SYSCTL_RCC_BYPASS))
{
//
// Wait until the PLL has locked.
//
for(ulDelay = 32768; ulDelay > 0; ulDelay--)
{
if(HWREG(SYSCTL_RIS) & SYSCTL_INT_PLL_LOCK)
{
break;
}
}
//
// Enable use of the PLL.
//
ulRCC &= ~(SYSCTL_RCC_BYPASS);
ulRCC2 &= ~(SYSCTL_RCC2_BYPASS2);
}
//
// Write the final RCC value.
//
HWREG(SYSCTL_RCC) = ulRCC;
HWREG(SYSCTL_RCC2) = ulRCC2;
//
// Delay for a little bit so that the system divider takes effect.
//
SysCtlDelay(16);
}
//*****************************************************************************
//
//! Gets the processor clock rate.
//!
//! This function determines the clock rate of the processor clock. This is
//! also the clock rate of all the peripheral modules (with the exception of
//! PWM, which has its own clock divider).
//!
//! \note This will not return accurate results if SysCtlClockSet() has not
//! been called to configure the clocking of the device, or if the device is
//! directly clocked from a crystal (or a clock source) that is not one of the
//! supported crystal frequencies. In the later case, this function should be
//! modified to directly return the correct system clock rate.
//!
//! \return The processor clock rate.
//
//*****************************************************************************
unsigned long
SysCtlClockGet(void)
{
unsigned long ulRCC, ulRCC2, ulPLL, ulClk;
//
// Read RCC and RCC2. For Sandstorm-class devices (which do not have
// RCC2), the RCC2 read will return 0, which indicates that RCC2 is
// disabled (since the SYSCTL_RCC2_USERCC2 bit is clear).
//
ulRCC = HWREG(SYSCTL_RCC);
ulRCC2 = HWREG(SYSCTL_RCC2);
//
// Get the base clock rate.
//
switch((ulRCC2 & SYSCTL_RCC2_USERCC2) ?
(ulRCC2 & SYSCTL_RCC2_OSCSRC2_M) :
(ulRCC & SYSCTL_RCC_OSCSRC_M))
{
//
// The main oscillator is the clock source. Determine its rate from
// the crystal setting field.
//
case SYSCTL_RCC_OSCSRC_MAIN:
{
ulClk = g_pulXtals[(ulRCC & SYSCTL_RCC_XTAL_M) >>
SYSCTL_RCC_XTAL_S];
break;
}
//
// The internal oscillator is the source clock.
//
case SYSCTL_RCC_OSCSRC_INT:
{
//
// See if this is a Sandstorm-class or Fury-class device.
//
if(CLASS_IS_SANDSTORM)
{
//
// The internal oscillator on a Sandstorm-class device is
// 15 MHz +/- 50%.
//
ulClk = 15000000;
}
else
{
//
// The internal oscillator on a Fury-class device is 12 MHz
// +/- 30%.
//
ulClk = 12000000;
}
break;
}
//
// The internal oscillator divided by four is the source clock.
//
case SYSCTL_RCC_OSCSRC_INT4:
{
//
// See if this is a Sandstorm-class or Fury-class device.
//
if(CLASS_IS_SANDSTORM)
{
//
// The internal oscillator on a Sandstorm-class device is
// 15 MHz +/- 50%.
//
ulClk = 15000000 / 4;
}
else
{
//
// The internal oscillator on a Fury-class device is 12 MHz
// +/- 30%.
//
ulClk = 12000000 / 4;
}
break;
}
//
// The internal 30 KHz oscillator is the source clock.
//
case SYSCTL_RCC_OSCSRC_30:
{
//
// The internal 30 KHz oscillator has an accuracy of +/- 30%.
//
ulClk = 30000;
break;
}
//
// The 32 KHz clock from the hibernate module is the source clock.
//
case SYSCTL_RCC2_OSCSRC2_32:
{
ulClk = 32768;
break;
}
//
// An unknown setting, so return a zero clock (that is, an unknown
// clock rate).
//
default:
{
return(0);
}
}
//
// See if the PLL is being used.
//
if(((ulRCC2 & SYSCTL_RCC2_USERCC2) && !(ulRCC2 & SYSCTL_RCC2_BYPASS2)) ||
(!(ulRCC2 & SYSCTL_RCC2_USERCC2) && !(ulRCC & SYSCTL_RCC_BYPASS)))
{
//
// Get the PLL configuration.
//
ulPLL = HWREG(SYSCTL_PLLCFG);
//
// See if this is a Sandstorm-class or Fury-class device.
//
if(CLASS_IS_SANDSTORM)
{
//
// Compute the PLL output frequency based on its input frequency.
// The formula for a Sandstorm-class devices is
// "(xtal * (f + 2)) / (r + 2)".
//
ulClk = ((ulClk * (((ulPLL & SYSCTL_PLLCFG_F_M) >>
SYSCTL_PLLCFG_F_S) + 2)) /
(((ulPLL & SYSCTL_PLLCFG_R_M) >>
SYSCTL_PLLCFG_R_S) + 2));
}
else
{
//
// Compute the PLL output frequency based on its input frequency.
// The formula for a Fury-class device is
// "(xtal * f) / ((r + 1) * 2)".
//
ulClk = ((ulClk * ((ulPLL & SYSCTL_PLLCFG_F_M) >>
SYSCTL_PLLCFG_F_S)) /
((((ulPLL & SYSCTL_PLLCFG_R_M) >>
SYSCTL_PLLCFG_R_S) + 1) * 2));
}
//
// See if the optional output divide by 2 is being used.
//
if(ulPLL & SYSCTL_PLLCFG_OD_2)
{
ulClk /= 2;
}
//
// See if the optional output divide by 4 is being used.
//
if(ulPLL & SYSCTL_PLLCFG_OD_4)
{
ulClk /= 4;
}
}
//
// See if the system divider is being used.
//
if(ulRCC & SYSCTL_RCC_USESYSDIV)
{
//
// Adjust the clock rate by the system clock divider.
//
if(ulRCC2 & SYSCTL_RCC2_USERCC2)
{
ulClk /= (((ulRCC2 & SYSCTL_RCC2_SYSDIV2_M) >>
SYSCTL_RCC2_SYSDIV2_S) + 1);
}
else
{
ulClk /= (((ulRCC & SYSCTL_RCC_SYSDIV_M) >> SYSCTL_RCC_SYSDIV_S) +
1);
}
}
//
// Return the computed clock rate.
//
return(ulClk);
}
//*****************************************************************************
//
//! Sets the PWM clock configuration.
//!
//! \param ulConfig is the configuration for the PWM clock; it must be one of
//! \b SYSCTL_PWMDIV_1, \b SYSCTL_PWMDIV_2, \b SYSCTL_PWMDIV_4,
//! \b SYSCTL_PWMDIV_8, \b SYSCTL_PWMDIV_16, \b SYSCTL_PWMDIV_32, or
//! \b SYSCTL_PWMDIV_64.
//!
//! This function sets the rate of the clock provided to the PWM module as a
//! ratio of the processor clock. This clock is used by the PWM module to
//! generate PWM signals; its rate forms the basis for all PWM signals.
//!
//! \note The clocking of the PWM is dependent upon the system clock rate as
//! configured by SysCtlClockSet().
//!
//! \return None.
//
//*****************************************************************************
void
SysCtlPWMClockSet(unsigned long ulConfig)
{
//
// Check the arguments.
//
ASSERT((ulConfig == SYSCTL_PWMDIV_1) ||
(ulConfig == SYSCTL_PWMDIV_2) ||
(ulConfig == SYSCTL_PWMDIV_4) ||
(ulConfig == SYSCTL_PWMDIV_8) ||
(ulConfig == SYSCTL_PWMDIV_16) ||
(ulConfig == SYSCTL_PWMDIV_32) ||
(ulConfig == SYSCTL_PWMDIV_64));
//
// Check that there is a PWM block on this part.
//
ASSERT(HWREG(SYSCTL_DC1) & SYSCTL_DC1_PWM);
//
// Set the PWM clock configuration into the run-mode clock configuration
// register.
//
HWREG(SYSCTL_RCC) = ((HWREG(SYSCTL_RCC) &
~(SYSCTL_RCC_USEPWMDIV | SYSCTL_RCC_PWMDIV_M)) |
ulConfig);
}
//*****************************************************************************
//
//! Gets the current PWM clock configuration.
//!
//! This function returns the current PWM clock configuration.
//!
//! \return Returns the current PWM clock configuration; will be one of
//! \b SYSCTL_PWMDIV_1, \b SYSCTL_PWMDIV_2, \b SYSCTL_PWMDIV_4,
//! \b SYSCTL_PWMDIV_8, \b SYSCTL_PWMDIV_16, \b SYSCTL_PWMDIV_32, or
//! \b SYSCTL_PWMDIV_64.
//
//*****************************************************************************
unsigned long
SysCtlPWMClockGet(void)
{
//
// Check that there is a PWM block on this part.
//
ASSERT(HWREG(SYSCTL_DC1) & SYSCTL_DC1_PWM);
//
// Return the current PWM clock configuration. Make sure that
// SYSCTL_PWMDIV_1 is returned in all cases where the divider is disabled.
//
if(!(HWREG(SYSCTL_RCC) & SYSCTL_RCC_USEPWMDIV))
{
//
// The divider is not active so reflect this in the value we return.
//
return(SYSCTL_PWMDIV_1);
}
else
{
//
// The divider is active so directly return the masked register value.
//
return(HWREG(SYSCTL_RCC) &
(SYSCTL_RCC_USEPWMDIV | SYSCTL_RCC_PWMDIV_M));
}
}
//*****************************************************************************
//
//! Sets the sample rate of the ADC.
//!
//! \param ulSpeed is the desired sample rate of the ADC; must be one of
//! \b SYSCTL_ADCSPEED_1MSPS, \b SYSCTL_ADCSPEED_500KSPS,
//! \b SYSCTL_ADCSPEED_250KSPS, or \b SYSCTL_ADCSPEED_125KSPS.
//!
//! This function sets the rate at which the ADC samples are captured by the
//! ADC block. The sampling speed may be limited by the hardware, so the
/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -