1 //*****************************************************************************
2 //
3 // sysctl.c - Driver for the system controller.
4 //
5 // Copyright (c) 2005-2017 Texas Instruments Incorporated.  All rights reserved.
6 // Software License Agreement
7 //
8 //   Redistribution and use in source and binary forms, with or without
9 //   modification, are permitted provided that the following conditions
10 //   are met:
11 //
12 //   Redistributions of source code must retain the above copyright
13 //   notice, this list of conditions and the following disclaimer.
14 //
15 //   Redistributions in binary form must reproduce the above copyright
16 //   notice, this list of conditions and the following disclaimer in the
17 //   documentation and/or other materials provided with the
18 //   distribution.
19 //
20 //   Neither the name of Texas Instruments Incorporated nor the names of
21 //   its contributors may be used to endorse or promote products derived
22 //   from this software without specific prior written permission.
23 //
24 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 //
36 // This is part of revision 2.1.4.178 of the Tiva Peripheral Driver Library.
37 //
38 //*****************************************************************************
39 
40 //*****************************************************************************
41 //
42 //! \addtogroup sysctl_api
43 //! @{
44 //
45 //*****************************************************************************
46 
47 #include <stdbool.h>
48 #include <stdint.h>
49 #include "hw_ints.h"
50 #include "hw_nvic.h"
51 #include "hw_sysctl.h"
52 #include "hw_types.h"
53 #include "hw_flash.h"
54 #include "cpu.h"
55 #include "debug.h"
56 #include "interrupt.h"
57 #include "sysctl.h"
58 
59 //*****************************************************************************
60 //
61 // The flash shift used in the math to calculate the flash sector size.
62 //
63 //*****************************************************************************
64 #ifndef FLASH_PP_MAINSS_S
65 #define FLASH_PP_MAINSS_S       16
66 #endif
67 
68 //*****************************************************************************
69 //
70 // This macro converts the XTAL value provided in the ui32Config parameter to
71 // an index to the g_pui32Xtals array.
72 //
73 //*****************************************************************************
74 #define SysCtlXtalCfgToIndex(a) ((a & 0x7c0) >> 6)
75 
76 //*****************************************************************************
77 //
78 // An array that maps the crystal number in RCC to a frequency.
79 //
80 //*****************************************************************************
81 static const uint32_t g_pui32Xtals[] =
82 {
83     1000000,
84     1843200,
85     2000000,
86     2457600,
87     3579545,
88     3686400,
89     4000000,
90     4096000,
91     4915200,
92     5000000,
93     5120000,
94     6000000,
95     6144000,
96     7372800,
97     8000000,
98     8192000,
99     10000000,
100     12000000,
101     12288000,
102     13560000,
103     14318180,
104     16000000,
105     16384000,
106     18000000,
107     20000000,
108     24000000,
109     25000000
110 };
111 
112 //*****************************************************************************
113 //
114 // Maximum number of VCO entries in the g_pui32XTALtoVCO and
115 // g_pui32VCOFrequencies structures for a device.
116 //
117 //*****************************************************************************
118 #define MAX_VCO_ENTRIES         2
119 #define MAX_XTAL_ENTRIES        18
120 
121 //*****************************************************************************
122 //
123 // These macros are used in the g_pui32XTALtoVCO table to make it more
124 // readable.
125 //
126 //*****************************************************************************
127 #define PLL_M_TO_REG(mi, mf)                                                  \
128         ((uint32_t)mi | (uint32_t)(mf << SYSCTL_PLLFREQ0_MFRAC_S))
129 #define PLL_N_TO_REG(n)                                                       \
130         ((uint32_t)(n - 1) << SYSCTL_PLLFREQ1_N_S)
131 #define PLL_Q_TO_REG(q)                                                       \
132         ((uint32_t)(q - 1) << SYSCTL_PLLFREQ1_Q_S)
133 
134 //*****************************************************************************
135 //
136 // Look up of the values that go into the PLLFREQ0 and PLLFREQ1 registers.
137 //
138 //*****************************************************************************
139 static const uint32_t g_pppui32XTALtoVCO[MAX_VCO_ENTRIES][MAX_XTAL_ENTRIES][3] =
140 {
141     {
142         //
143         // VCO 320 MHz
144         //
145         { PLL_M_TO_REG(64, 0),   PLL_N_TO_REG(1), PLL_Q_TO_REG(2) },     // 5 MHz
146         { PLL_M_TO_REG(62, 512), PLL_N_TO_REG(1), PLL_Q_TO_REG(2) },     // 5.12 MHz
147         { PLL_M_TO_REG(160, 0),  PLL_N_TO_REG(3), PLL_Q_TO_REG(2) },     // 6 MHz
148         { PLL_M_TO_REG(52, 85),  PLL_N_TO_REG(1), PLL_Q_TO_REG(2) },     // 6.144 MHz
149         { PLL_M_TO_REG(43, 412), PLL_N_TO_REG(1), PLL_Q_TO_REG(2) },     // 7.3728 MHz
150         { PLL_M_TO_REG(40, 0),   PLL_N_TO_REG(1), PLL_Q_TO_REG(2) },     // 8 MHz
151         { PLL_M_TO_REG(39, 64),  PLL_N_TO_REG(1), PLL_Q_TO_REG(2) },     // 8.192 MHz
152         { PLL_M_TO_REG(32, 0),   PLL_N_TO_REG(1), PLL_Q_TO_REG(2) },     // 10 MHz
153         { PLL_M_TO_REG(80, 0),   PLL_N_TO_REG(3), PLL_Q_TO_REG(2) },     // 12 MHz
154         { PLL_M_TO_REG(26, 43),  PLL_N_TO_REG(1), PLL_Q_TO_REG(2) },     // 12.288 MHz
155         { PLL_M_TO_REG(23, 613), PLL_N_TO_REG(1), PLL_Q_TO_REG(2) },     // 13.56 MHz
156         { PLL_M_TO_REG(22, 358), PLL_N_TO_REG(1), PLL_Q_TO_REG(2) },     // 14.318180 MHz
157         { PLL_M_TO_REG(20, 0),   PLL_N_TO_REG(1), PLL_Q_TO_REG(2) },     // 16 MHz
158         { PLL_M_TO_REG(19, 544), PLL_N_TO_REG(1), PLL_Q_TO_REG(2) },     // 16.384 MHz
159         { PLL_M_TO_REG(160, 0),  PLL_N_TO_REG(9), PLL_Q_TO_REG(2) },     // 18 MHz
160         { PLL_M_TO_REG(16, 0),   PLL_N_TO_REG(1), PLL_Q_TO_REG(2) },     // 20 MHz
161         { PLL_M_TO_REG(40, 0),   PLL_N_TO_REG(3), PLL_Q_TO_REG(2) },     // 24 MHz
162         { PLL_M_TO_REG(64, 0),   PLL_N_TO_REG(5), PLL_Q_TO_REG(2) },     // 25 MHz
163     },
164     {
165         //
166         // VCO 480 MHz
167         //
168         { PLL_M_TO_REG(96, 0),   PLL_N_TO_REG(1), PLL_Q_TO_REG(2) },     // 5 MHz
169         { PLL_M_TO_REG(93, 768), PLL_N_TO_REG(1), PLL_Q_TO_REG(2) },     // 5.12 MHz
170         { PLL_M_TO_REG(80, 0),   PLL_N_TO_REG(1), PLL_Q_TO_REG(2) },     // 6 MHz
171         { PLL_M_TO_REG(78, 128), PLL_N_TO_REG(1), PLL_Q_TO_REG(2) },     // 6.144 MHz
172         { PLL_M_TO_REG(65, 107), PLL_N_TO_REG(1), PLL_Q_TO_REG(2) },     // 7.3728 MHz
173         { PLL_M_TO_REG(60, 0),   PLL_N_TO_REG(1), PLL_Q_TO_REG(2) },     // 8 MHz
174         { PLL_M_TO_REG(58, 608), PLL_N_TO_REG(1), PLL_Q_TO_REG(2) },     // 8.192 MHz
175         { PLL_M_TO_REG(48, 0),   PLL_N_TO_REG(1), PLL_Q_TO_REG(2) },     // 10 MHz
176         { PLL_M_TO_REG(40, 0),   PLL_N_TO_REG(1), PLL_Q_TO_REG(2) },     // 12 MHz
177         { PLL_M_TO_REG(39, 64),  PLL_N_TO_REG(1), PLL_Q_TO_REG(2) },     // 12.288 MHz
178         { PLL_M_TO_REG(35, 408), PLL_N_TO_REG(1), PLL_Q_TO_REG(2) },     // 13.56 MHz
179         { PLL_M_TO_REG(33, 536), PLL_N_TO_REG(1), PLL_Q_TO_REG(2) },     // 14.318180 MHz
180         { PLL_M_TO_REG(30, 0),   PLL_N_TO_REG(1), PLL_Q_TO_REG(2) },     // 16 MHz
181         { PLL_M_TO_REG(29, 304), PLL_N_TO_REG(1), PLL_Q_TO_REG(2) },     // 16.384 MHz
182         { PLL_M_TO_REG(80, 0),   PLL_N_TO_REG(3), PLL_Q_TO_REG(2) },     // 18 MHz
183         { PLL_M_TO_REG(24, 0),   PLL_N_TO_REG(1), PLL_Q_TO_REG(2) },     // 20 MHz
184         { PLL_M_TO_REG(20, 0),   PLL_N_TO_REG(1), PLL_Q_TO_REG(2) },     // 24 MHz
185         { PLL_M_TO_REG(96, 0),   PLL_N_TO_REG(5), PLL_Q_TO_REG(2) },     // 25 MHz
186     },
187 };
188 
189 //*****************************************************************************
190 //
191 // The mapping of system clock frequency to flash memory timing parameters.
192 //
193 //*****************************************************************************
194 static const struct
195 {
196     uint32_t ui32Frequency;
197     uint32_t ui32MemTiming;
198 }
199 g_sXTALtoMEMTIM[] =
200 {
201     { 16000000, (SYSCTL_MEMTIM0_FBCHT_0_5 | SYSCTL_MEMTIM0_FBCE |
202                  (0 << SYSCTL_MEMTIM0_FWS_S) |
203                  SYSCTL_MEMTIM0_EBCHT_0_5 | SYSCTL_MEMTIM0_EBCE |
204                  (0 << SYSCTL_MEMTIM0_EWS_S) |
205                  SYSCTL_MEMTIM0_MB1) },
206     { 40000000, (SYSCTL_MEMTIM0_FBCHT_1_5 | (1 << SYSCTL_MEMTIM0_FWS_S) |
207                  SYSCTL_MEMTIM0_EBCHT_1_5 | (1 << SYSCTL_MEMTIM0_EWS_S) |
208                  SYSCTL_MEMTIM0_MB1) },
209     { 60000000, (SYSCTL_MEMTIM0_FBCHT_2 | (2 << SYSCTL_MEMTIM0_FWS_S) |
210                  SYSCTL_MEMTIM0_EBCHT_2 | (2 << SYSCTL_MEMTIM0_EWS_S) |
211                  SYSCTL_MEMTIM0_MB1) },
212     { 80000000, (SYSCTL_MEMTIM0_FBCHT_2_5 | (3 << SYSCTL_MEMTIM0_FWS_S) |
213                  SYSCTL_MEMTIM0_EBCHT_2_5 | (3 << SYSCTL_MEMTIM0_EWS_S) |
214                  SYSCTL_MEMTIM0_MB1) },
215     { 100000000, (SYSCTL_MEMTIM0_FBCHT_3 | (4 << SYSCTL_MEMTIM0_FWS_S) |
216                   SYSCTL_MEMTIM0_EBCHT_3 | (4 << SYSCTL_MEMTIM0_EWS_S) |
217                   SYSCTL_MEMTIM0_MB1) },
218     { 120000000, (SYSCTL_MEMTIM0_FBCHT_3_5 | (5 << SYSCTL_MEMTIM0_FWS_S) |
219                   SYSCTL_MEMTIM0_EBCHT_3_5 | (5 << SYSCTL_MEMTIM0_EWS_S) |
220                   SYSCTL_MEMTIM0_MB1) },
221 };
222 
223 //*****************************************************************************
224 //
225 // Get the correct memory timings for a given system clock value.
226 //
227 //*****************************************************************************
228 static uint32_t
_SysCtlMemTimingGet(uint32_t ui32SysClock)229 _SysCtlMemTimingGet(uint32_t ui32SysClock)
230 {
231     uint_fast8_t ui8Idx;
232 
233     //
234     // Loop through the flash memory timings.
235     //
236     for(ui8Idx = 0;
237         ui8Idx < (sizeof(g_sXTALtoMEMTIM) / sizeof(g_sXTALtoMEMTIM[0]));
238         ui8Idx++)
239     {
240         //
241         // See if the system clock frequency is less than the maximum frequency
242         // for this flash memory timing.
243         //
244         if(ui32SysClock <= g_sXTALtoMEMTIM[ui8Idx].ui32Frequency)
245         {
246             //
247             // This flash memory timing is the best choice for the system clock
248             // frequency, so return it now.
249             //
250             return(g_sXTALtoMEMTIM[ui8Idx].ui32MemTiming);
251         }
252     }
253 
254     //
255     // An appropriate flash memory timing could not be found, so the device is
256     // being clocked too fast.  Return the default flash memory timing.
257     //
258     return(0);
259 }
260 
261 //*****************************************************************************
262 //
263 // Calculate the system frequency from the register settings base on the
264 // oscillator input.
265 //
266 //*****************************************************************************
267 static uint32_t
_SysCtlFrequencyGet(uint32_t ui32Xtal)268 _SysCtlFrequencyGet(uint32_t ui32Xtal)
269 {
270     uint32_t ui32Result;
271     uint_fast16_t ui16F1, ui16F2;
272     uint_fast16_t ui16PInt, ui16PFract;
273     uint_fast8_t ui8Q, ui8N;
274 
275     //
276     // Extract all of the values from the hardware registers.
277     //
278     ui16PFract = ((HWREG(SYSCTL_PLLFREQ0) & SYSCTL_PLLFREQ0_MFRAC_M) >>
279                   SYSCTL_PLLFREQ0_MFRAC_S);
280     ui16PInt = HWREG(SYSCTL_PLLFREQ0) & SYSCTL_PLLFREQ0_MINT_M;
281     ui8Q = (((HWREG(SYSCTL_PLLFREQ1) & SYSCTL_PLLFREQ1_Q_M) >>
282              SYSCTL_PLLFREQ1_Q_S) + 1);
283     ui8N = (((HWREG(SYSCTL_PLLFREQ1) & SYSCTL_PLLFREQ1_N_M) >>
284              SYSCTL_PLLFREQ1_N_S) + 1);
285 
286     //
287     // Divide the crystal value by N.
288     //
289     ui32Xtal /= (uint32_t)ui8N;
290 
291     //
292     // Calculate the multiplier for bits 9:5.
293     //
294     ui16F1 = ui16PFract / 32;
295 
296     //
297     // Calculate the multiplier for bits 4:0.
298     //
299     ui16F2 = ui16PFract - (ui16F1 * 32);
300 
301     //
302     // Get the integer portion.
303     //
304     ui32Result = ui32Xtal * (uint32_t)ui16PInt;
305 
306     //
307     // Add first fractional bits portion(9:0).
308     //
309     ui32Result += (ui32Xtal * (uint32_t)ui16F1) / 32;
310 
311     //
312     // Add the second fractional bits portion(4:0).
313     //
314     ui32Result += (ui32Xtal * (uint32_t)ui16F2) / 1024;
315 
316     //
317     // Divide the result by Q.
318     //
319     ui32Result = ui32Result / (uint32_t)ui8Q;
320 
321     //
322     // Return the resulting PLL frequency.
323     //
324     return(ui32Result);
325 }
326 
327 //*****************************************************************************
328 //
329 // Look up of the possible VCO frequencies.
330 //
331 //*****************************************************************************
332 static const uint32_t g_pui32VCOFrequencies[MAX_VCO_ENTRIES] =
333 {
334     160000000,                              // VCO 320
335     240000000,                              // VCO 480
336 };
337 
338 //*****************************************************************************
339 //
340 // The base addresses of the various peripheral control registers.
341 //
342 //*****************************************************************************
343 #define SYSCTL_PPBASE           0x400fe300
344 #define SYSCTL_SRBASE           0x400fe500
345 #define SYSCTL_RCGCBASE         0x400fe600
346 #define SYSCTL_SCGCBASE         0x400fe700
347 #define SYSCTL_DCGCBASE         0x400fe800
348 #define SYSCTL_PCBASE           0x400fe900
349 #define SYSCTL_PRBASE           0x400fea00
350 
351 //*****************************************************************************
352 //
353 //! \internal
354 //! Checks a peripheral identifier.
355 //!
356 //! \param ui32Peripheral is the peripheral identifier.
357 //!
358 //! This function determines if a peripheral identifier is valid.
359 //!
360 //! \return Returns \b true if the peripheral identifier is valid and \b false
361 //! otherwise.
362 //
363 //*****************************************************************************
364 #ifdef DEBUG
365 static bool
_SysCtlPeripheralValid(uint32_t ui32Peripheral)366 _SysCtlPeripheralValid(uint32_t ui32Peripheral)
367 {
368     return((ui32Peripheral == SYSCTL_PERIPH_ADC0) ||
369            (ui32Peripheral == SYSCTL_PERIPH_ADC1) ||
370            (ui32Peripheral == SYSCTL_PERIPH_CAN0) ||
371            (ui32Peripheral == SYSCTL_PERIPH_CAN1) ||
372            (ui32Peripheral == SYSCTL_PERIPH_COMP0) ||
373            (ui32Peripheral == SYSCTL_PERIPH_CCM0) ||
374            (ui32Peripheral == SYSCTL_PERIPH_EEPROM0) ||
375            (ui32Peripheral == SYSCTL_PERIPH_EPHY0) ||
376            (ui32Peripheral == SYSCTL_PERIPH_EMAC0) ||
377            (ui32Peripheral == SYSCTL_PERIPH_EPI0) ||
378            (ui32Peripheral == SYSCTL_PERIPH_FAN0) ||
379            (ui32Peripheral == SYSCTL_PERIPH_GPIOA) ||
380            (ui32Peripheral == SYSCTL_PERIPH_GPIOB) ||
381            (ui32Peripheral == SYSCTL_PERIPH_GPIOC) ||
382            (ui32Peripheral == SYSCTL_PERIPH_GPIOD) ||
383            (ui32Peripheral == SYSCTL_PERIPH_GPIOE) ||
384            (ui32Peripheral == SYSCTL_PERIPH_GPIOF) ||
385            (ui32Peripheral == SYSCTL_PERIPH_GPIOG) ||
386            (ui32Peripheral == SYSCTL_PERIPH_GPIOH) ||
387            (ui32Peripheral == SYSCTL_PERIPH_GPIOJ) ||
388            (ui32Peripheral == SYSCTL_PERIPH_GPIOK) ||
389            (ui32Peripheral == SYSCTL_PERIPH_GPIOL) ||
390            (ui32Peripheral == SYSCTL_PERIPH_GPIOM) ||
391            (ui32Peripheral == SYSCTL_PERIPH_GPION) ||
392            (ui32Peripheral == SYSCTL_PERIPH_GPIOP) ||
393            (ui32Peripheral == SYSCTL_PERIPH_GPIOQ) ||
394            (ui32Peripheral == SYSCTL_PERIPH_GPIOR) ||
395            (ui32Peripheral == SYSCTL_PERIPH_GPIOS) ||
396            (ui32Peripheral == SYSCTL_PERIPH_GPIOT) ||
397            (ui32Peripheral == SYSCTL_PERIPH_HIBERNATE) ||
398            (ui32Peripheral == SYSCTL_PERIPH_I2C0) ||
399            (ui32Peripheral == SYSCTL_PERIPH_I2C1) ||
400            (ui32Peripheral == SYSCTL_PERIPH_I2C2) ||
401            (ui32Peripheral == SYSCTL_PERIPH_I2C3) ||
402            (ui32Peripheral == SYSCTL_PERIPH_I2C4) ||
403            (ui32Peripheral == SYSCTL_PERIPH_I2C5) ||
404            (ui32Peripheral == SYSCTL_PERIPH_I2C6) ||
405            (ui32Peripheral == SYSCTL_PERIPH_I2C7) ||
406            (ui32Peripheral == SYSCTL_PERIPH_I2C8) ||
407            (ui32Peripheral == SYSCTL_PERIPH_I2C9) ||
408            (ui32Peripheral == SYSCTL_PERIPH_LCD0) ||
409            (ui32Peripheral == SYSCTL_PERIPH_PWM0) ||
410            (ui32Peripheral == SYSCTL_PERIPH_PWM1) ||
411            (ui32Peripheral == SYSCTL_PERIPH_QEI0) ||
412            (ui32Peripheral == SYSCTL_PERIPH_QEI1) ||
413            (ui32Peripheral == SYSCTL_PERIPH_SSI0) ||
414            (ui32Peripheral == SYSCTL_PERIPH_SSI1) ||
415            (ui32Peripheral == SYSCTL_PERIPH_SSI2) ||
416            (ui32Peripheral == SYSCTL_PERIPH_SSI3) ||
417            (ui32Peripheral == SYSCTL_PERIPH_TIMER0) ||
418            (ui32Peripheral == SYSCTL_PERIPH_TIMER1) ||
419            (ui32Peripheral == SYSCTL_PERIPH_TIMER2) ||
420            (ui32Peripheral == SYSCTL_PERIPH_TIMER3) ||
421            (ui32Peripheral == SYSCTL_PERIPH_TIMER4) ||
422            (ui32Peripheral == SYSCTL_PERIPH_TIMER5) ||
423            (ui32Peripheral == SYSCTL_PERIPH_TIMER6) ||
424            (ui32Peripheral == SYSCTL_PERIPH_TIMER7) ||
425            (ui32Peripheral == SYSCTL_PERIPH_UART0) ||
426            (ui32Peripheral == SYSCTL_PERIPH_UART1) ||
427            (ui32Peripheral == SYSCTL_PERIPH_UART2) ||
428            (ui32Peripheral == SYSCTL_PERIPH_UART3) ||
429            (ui32Peripheral == SYSCTL_PERIPH_UART4) ||
430            (ui32Peripheral == SYSCTL_PERIPH_UART5) ||
431            (ui32Peripheral == SYSCTL_PERIPH_UART6) ||
432            (ui32Peripheral == SYSCTL_PERIPH_UART7) ||
433            (ui32Peripheral == SYSCTL_PERIPH_UDMA) ||
434            (ui32Peripheral == SYSCTL_PERIPH_USB0) ||
435            (ui32Peripheral == SYSCTL_PERIPH_WDOG0) ||
436            (ui32Peripheral == SYSCTL_PERIPH_WDOG1) ||
437            (ui32Peripheral == SYSCTL_PERIPH_WTIMER0) ||
438            (ui32Peripheral == SYSCTL_PERIPH_WTIMER1) ||
439            (ui32Peripheral == SYSCTL_PERIPH_WTIMER2) ||
440            (ui32Peripheral == SYSCTL_PERIPH_WTIMER3) ||
441            (ui32Peripheral == SYSCTL_PERIPH_WTIMER4) ||
442            (ui32Peripheral == SYSCTL_PERIPH_WTIMER5));
443 }
444 #endif
445 
446 //*****************************************************************************
447 //
448 //! Gets the size of the SRAM.
449 //!
450 //! This function determines the size of the SRAM on the Tiva device.
451 //!
452 //! \return The total number of bytes of SRAM.
453 //
454 //*****************************************************************************
455 uint32_t
SysCtlSRAMSizeGet(void)456 SysCtlSRAMSizeGet(void)
457 {
458     return((HWREG(FLASH_SSIZE) + 1) * 256);
459 }
460 
461 //*****************************************************************************
462 //
463 //! Gets the size of the flash.
464 //!
465 //! This function determines the size of the flash on the Tiva device.
466 //!
467 //! \return The total number of bytes of flash.
468 //
469 //*****************************************************************************
470 uint32_t
SysCtlFlashSizeGet(void)471 SysCtlFlashSizeGet(void)
472 {
473 
474     //
475     // TM4C123 devices report the flash size in DC0.
476     //
477     if(CLASS_IS_TM4C123)
478     {
479         //
480         // Compute the size of the flash.
481         //
482         return(((HWREG(SYSCTL_DC0) & SYSCTL_DC0_FLASHSZ_M) << 11) + 0x800);
483     }
484     else
485     {
486         //
487         // Get the flash size from the FLASH_PP register.
488         //
489         return(2048 * ((HWREG(FLASH_PP) & FLASH_PP_SIZE_M) + 1));
490     }
491 }
492 
493 //*****************************************************************************
494 //
495 //! Gets the size of a single eraseable sector of flash.
496 //!
497 //! This function determines the flash sector size on the Tiva device.
498 //! This size determines the erase granularity of the device flash.
499 //!
500 //! \return The number of bytes in a single flash sector.
501 //
502 //*****************************************************************************
503 uint32_t
SysCtlFlashSectorSizeGet(void)504 SysCtlFlashSectorSizeGet(void)
505 {
506     //
507     // TM4C129 devices store the value in a different register.
508     //
509     if(CLASS_IS_TM4C129)
510     {
511         //
512         // Get the flash sector size from the FLASH_PP register.
513         //
514         return(1 << (10 +
515                      ((HWREG(FLASH_PP) &
516                        FLASH_PP_MAINSS_M) >> FLASH_PP_MAINSS_S)));
517     }
518     else
519     {
520         //
521         // The sector size is fixed at 1KB.
522         //
523         return(1024);
524     }
525 }
526 
527 //*****************************************************************************
528 //
529 //! Determines if a peripheral is present.
530 //!
531 //! \param ui32Peripheral is the peripheral in question.
532 //!
533 //! This function determines if a particular peripheral is present in the
534 //! device.  Each member of the Tiva family has a different peripheral
535 //! set; this function determines which peripherals are present on this device.
536 //!
537 //! The \e ui32Peripheral parameter must be only one of the following values:
538 //! \b SYSCTL_PERIPH_ADC0, \b SYSCTL_PERIPH_ADC1, \b SYSCTL_PERIPH_CAN0,
539 //! \b SYSCTL_PERIPH_CAN1, \b SYSCTL_PERIPH_CCM0,\b SYSCTL_PERIPH_COMP0,
540 //! \b SYSCTL_PERIPH_EEPROM0, \b SYSCTL_PERIPH_EMAC, \b SYSCTL_PERIPH_EPHY,
541 //! \b SYSCTL_PERIPH_EPI0,
542 //! \b SYSCTL_PERIPH_GPIOA, \b SYSCTL_PERIPH_GPIOB, \b SYSCTL_PERIPH_GPIOC,
543 //! \b SYSCTL_PERIPH_GPIOD, \b SYSCTL_PERIPH_GPIOE, \b SYSCTL_PERIPH_GPIOF,
544 //! \b SYSCTL_PERIPH_GPIOG, \b SYSCTL_PERIPH_GPIOH, \b SYSCTL_PERIPH_GPIOJ,
545 //! \b SYSCTL_PERIPH_GPIOK, \b SYSCTL_PERIPH_GPIOL, \b SYSCTL_PERIPH_GPIOM,
546 //! \b SYSCTL_PERIPH_GPION, \b SYSCTL_PERIPH_GPIOP, \b SYSCTL_PERIPH_GPIOQ,
547 //! \b SYSCTL_PERIPH_GPIOR, \b SYSCTL_PERIPH_GPIOS, \b SYSCTL_PERIPH_GPIOT,
548 //! \b SYSCTL_PERIPH_HIBERNATE,
549 //! \b SYSCTL_PERIPH_I2C0, \b SYSCTL_PERIPH_I2C1, \b SYSCTL_PERIPH_I2C2,
550 //! \b SYSCTL_PERIPH_I2C3, \b SYSCTL_PERIPH_I2C4, \b SYSCTL_PERIPH_I2C5,
551 //! \b SYSCTL_PERIPH_I2C6, \b SYSCTL_PERIPH_I2C7, \b SYSCTL_PERIPH_I2C8,
552 //! \b SYSCTL_PERIPH_I2C9, \b SYSCTL_PERIPH_LCD0,
553 //! \b SYSCTL_PERIPH_ONEWIRE0,
554 //! \b SYSCTL_PERIPH_PWM0, \b SYSCTL_PERIPH_PWM1, \b SYSCTL_PERIPH_QEI0,
555 //! \b SYSCTL_PERIPH_QEI1, \b SYSCTL_PERIPH_SSI0, \b SYSCTL_PERIPH_SSI1,
556 //! \b SYSCTL_PERIPH_SSI2, \b SYSCTL_PERIPH_SSI3, \b SYSCTL_PERIPH_TIMER0,
557 //! \b SYSCTL_PERIPH_TIMER1, \b SYSCTL_PERIPH_TIMER2, \b SYSCTL_PERIPH_TIMER3,
558 //! \b SYSCTL_PERIPH_TIMER4, \b SYSCTL_PERIPH_TIMER5, \b SYSCTL_PERIPH_TIMER6,
559 //! \b SYSCTL_PERIPH_TIMER7, \b SYSCTL_PERIPH_UART0, \b SYSCTL_PERIPH_UART1,
560 //! \b SYSCTL_PERIPH_UART2, \b SYSCTL_PERIPH_UART3, \b SYSCTL_PERIPH_UART4,
561 //! \b SYSCTL_PERIPH_UART5, \b SYSCTL_PERIPH_UART6, \b SYSCTL_PERIPH_UART7,
562 //! \b SYSCTL_PERIPH_UDMA, \b SYSCTL_PERIPH_USB0, \b SYSCTL_PERIPH_WDOG0,
563 //! \b SYSCTL_PERIPH_WDOG1, \b SYSCTL_PERIPH_WTIMER0, \b SYSCTL_PERIPH_WTIMER1,
564 //! \b SYSCTL_PERIPH_WTIMER2, \b SYSCTL_PERIPH_WTIMER3,
565 //! \b SYSCTL_PERIPH_WTIMER4, or \b SYSCTL_PERIPH_WTIMER5
566 //!
567 //! \return Returns \b true if the specified peripheral is present and \b false
568 //! if it is not.
569 //
570 //*****************************************************************************
571 bool
SysCtlPeripheralPresent(uint32_t ui32Peripheral)572 SysCtlPeripheralPresent(uint32_t ui32Peripheral)
573 {
574     //
575     // Check the arguments.
576     //
577     ASSERT(_SysCtlPeripheralValid(ui32Peripheral));
578 
579     //
580     // See if this peripheral is present.
581     //
582     return(HWREGBITW(SYSCTL_PPBASE + ((ui32Peripheral & 0xff00) >> 8),
583                      ui32Peripheral & 0xff));
584 }
585 
586 //*****************************************************************************
587 //
588 //! Determines if a peripheral is ready.
589 //!
590 //! \param ui32Peripheral is the peripheral in question.
591 //!
592 //! This function determines if a particular peripheral is ready to be
593 //! accessed.  The peripheral may be in a non-ready state if it is not enabled,
594 //! is being held in reset, or is in the process of becoming ready after being
595 //! enabled or taken out of reset.
596 //!
597 //! The \e ui32Peripheral parameter must be only one of the following values:
598 //! \b SYSCTL_PERIPH_ADC0, \b SYSCTL_PERIPH_ADC1, \b SYSCTL_PERIPH_CAN0,
599 //! \b SYSCTL_PERIPH_CAN1, \b SYSCTL_PERIPH_CCM0,\b SYSCTL_PERIPH_COMP0,
600 //! \b SYSCTL_PERIPH_EEPROM0, \b SYSCTL_PERIPH_EMAC, \b SYSCTL_PERIPH_EPHY,
601 //! \b SYSCTL_PERIPH_EPI0,
602 //! \b SYSCTL_PERIPH_GPIOA, \b SYSCTL_PERIPH_GPIOB, \b SYSCTL_PERIPH_GPIOC,
603 //! \b SYSCTL_PERIPH_GPIOD, \b SYSCTL_PERIPH_GPIOE, \b SYSCTL_PERIPH_GPIOF,
604 //! \b SYSCTL_PERIPH_GPIOG, \b SYSCTL_PERIPH_GPIOH, \b SYSCTL_PERIPH_GPIOJ,
605 //! \b SYSCTL_PERIPH_GPIOK, \b SYSCTL_PERIPH_GPIOL, \b SYSCTL_PERIPH_GPIOM,
606 //! \b SYSCTL_PERIPH_GPION, \b SYSCTL_PERIPH_GPIOP, \b SYSCTL_PERIPH_GPIOQ,
607 //! \b SYSCTL_PERIPH_GPIOR, \b SYSCTL_PERIPH_GPIOS, \b SYSCTL_PERIPH_GPIOT,
608 //! \b SYSCTL_PERIPH_HIBERNATE,
609 //! \b SYSCTL_PERIPH_I2C0, \b SYSCTL_PERIPH_I2C1, \b SYSCTL_PERIPH_I2C2,
610 //! \b SYSCTL_PERIPH_I2C3, \b SYSCTL_PERIPH_I2C4, \b SYSCTL_PERIPH_I2C5,
611 //! \b SYSCTL_PERIPH_I2C6, \b SYSCTL_PERIPH_I2C7, \b SYSCTL_PERIPH_I2C8,
612 //! \b SYSCTL_PERIPH_I2C9, \b SYSCTL_PERIPH_LCD0,
613 //! \b SYSCTL_PERIPH_ONEWIRE0,
614 //! \b SYSCTL_PERIPH_PWM0, \b SYSCTL_PERIPH_PWM1, \b SYSCTL_PERIPH_QEI0,
615 //! \b SYSCTL_PERIPH_QEI1, \b SYSCTL_PERIPH_SSI0, \b SYSCTL_PERIPH_SSI1,
616 //! \b SYSCTL_PERIPH_SSI2, \b SYSCTL_PERIPH_SSI3, \b SYSCTL_PERIPH_TIMER0,
617 //! \b SYSCTL_PERIPH_TIMER1, \b SYSCTL_PERIPH_TIMER2, \b SYSCTL_PERIPH_TIMER3,
618 //! \b SYSCTL_PERIPH_TIMER4, \b SYSCTL_PERIPH_TIMER5, \b SYSCTL_PERIPH_TIMER6,
619 //! \b SYSCTL_PERIPH_TIMER7, \b SYSCTL_PERIPH_UART0, \b SYSCTL_PERIPH_UART1,
620 //! \b SYSCTL_PERIPH_UART2, \b SYSCTL_PERIPH_UART3, \b SYSCTL_PERIPH_UART4,
621 //! \b SYSCTL_PERIPH_UART5, \b SYSCTL_PERIPH_UART6, \b SYSCTL_PERIPH_UART7,
622 //! \b SYSCTL_PERIPH_UDMA, \b SYSCTL_PERIPH_USB0, \b SYSCTL_PERIPH_WDOG0,
623 //! \b SYSCTL_PERIPH_WDOG1, \b SYSCTL_PERIPH_WTIMER0, \b SYSCTL_PERIPH_WTIMER1,
624 //! \b SYSCTL_PERIPH_WTIMER2, \b SYSCTL_PERIPH_WTIMER3,
625 //! \b SYSCTL_PERIPH_WTIMER4, or \b SYSCTL_PERIPH_WTIMER5
626 //!
627 //! \note The ability to check for a peripheral being ready varies based on the
628 //! Tiva part in use.  Please consult the data sheet for the part you are
629 //! using to determine if this feature is available.
630 //!
631 //! \return Returns \b true if the specified peripheral is ready and \b false
632 //! if it is not.
633 //
634 //*****************************************************************************
635 bool
SysCtlPeripheralReady(uint32_t ui32Peripheral)636 SysCtlPeripheralReady(uint32_t ui32Peripheral)
637 {
638     //
639     // Check the arguments.
640     //
641     ASSERT(_SysCtlPeripheralValid(ui32Peripheral));
642 
643     //
644     // See if this peripheral is ready.
645     //
646     return(HWREGBITW(SYSCTL_PRBASE + ((ui32Peripheral & 0xff00) >> 8),
647                      ui32Peripheral & 0xff));
648 }
649 
650 //*****************************************************************************
651 //
652 //! Powers on a peripheral.
653 //!
654 //! \param ui32Peripheral is the peripheral to be powered on.
655 //!
656 //! This function turns on the power to a peripheral.  The peripheral continues
657 //! to receive power even when its clock is not enabled.
658 //!
659 //! The \e ui32Peripheral parameter must be only one of the following values:
660 //! \b SYSCTL_PERIPH_CAN0,\b SYSCTL_PERIPH_CAN1, \b SYSCTL_PERIPH_EMAC,
661 //! \b SYSCTL_PERIPH_EPHY, \b SYSCTL_PERIPH_LCD0, \b SYSCTL_PERIPH_USB0
662 //!
663 //! \note The ability to power off a peripheral varies based on the Tiva
664 //! part in use.  Please consult the data sheet for the part you are using to
665 //! determine if this feature is available.
666 //!
667 //! \return None.
668 //
669 //*****************************************************************************
670 void
SysCtlPeripheralPowerOn(uint32_t ui32Peripheral)671 SysCtlPeripheralPowerOn(uint32_t ui32Peripheral)
672 {
673     //
674     // Check the arguments.
675     //
676     ASSERT(_SysCtlPeripheralValid(ui32Peripheral));
677 
678     //
679     // Power on this peripheral.
680     //
681     HWREGBITW(SYSCTL_PCBASE + ((ui32Peripheral & 0xff00) >> 8),
682               ui32Peripheral & 0xff) = 1;
683 }
684 
685 //*****************************************************************************
686 //
687 //! Powers off a peripheral.
688 //!
689 //! \param ui32Peripheral is the peripheral to be powered off.
690 //!
691 //! This function allows the power to a peripheral to be turned off.  The
692 //! peripheral continues to receive power when its clock is enabled, but
693 //! the power is removed when its clock is disabled.
694 //!
695 //! The \e ui32Peripheral parameter must be only one of the following values:
696 //! \b SYSCTL_PERIPH_CAN0,\b SYSCTL_PERIPH_CAN1, \b SYSCTL_PERIPH_EMAC,
697 //! \b SYSCTL_PERIPH_EPHY, \b SYSCTL_PERIPH_LCD0, \b SYSCTL_PERIPH_USB0
698 //!
699 //! \note The ability to power off a peripheral varies based on the Tiva
700 //! part in use.  Please consult the data sheet for the part you are using to
701 //! determine if this feature is available.
702 //!
703 //! \return None.
704 //
705 //*****************************************************************************
706 void
SysCtlPeripheralPowerOff(uint32_t ui32Peripheral)707 SysCtlPeripheralPowerOff(uint32_t ui32Peripheral)
708 {
709     //
710     // Check the arguments.
711     //
712     ASSERT(_SysCtlPeripheralValid(ui32Peripheral));
713 
714     //
715     // Power off this peripheral.
716     //
717     HWREGBITW(SYSCTL_PCBASE + ((ui32Peripheral & 0xff00) >> 8),
718               ui32Peripheral & 0xff) = 0;
719 }
720 
721 //*****************************************************************************
722 //
723 //! Performs a software reset of a peripheral.
724 //!
725 //! \param ui32Peripheral is the peripheral to reset.
726 //!
727 //! This function performs a software reset of the specified peripheral.  An
728 //! individual peripheral reset signal is asserted for a brief period and then
729 //! de-asserted, returning the internal state of the peripheral to its reset
730 //! condition.
731 //!
732 //! The \e ui32Peripheral parameter must be only one of the following values:
733 //! \b SYSCTL_PERIPH_ADC0, \b SYSCTL_PERIPH_ADC1, \b SYSCTL_PERIPH_CAN0,
734 //! \b SYSCTL_PERIPH_CAN1, \b SYSCTL_PERIPH_CCM0,\b SYSCTL_PERIPH_COMP0,
735 //! \b SYSCTL_PERIPH_EEPROM0, \b SYSCTL_PERIPH_EMAC, \b SYSCTL_PERIPH_EPHY,
736 //! \b SYSCTL_PERIPH_EPI0,
737 //! \b SYSCTL_PERIPH_GPIOA, \b SYSCTL_PERIPH_GPIOB, \b SYSCTL_PERIPH_GPIOC,
738 //! \b SYSCTL_PERIPH_GPIOD, \b SYSCTL_PERIPH_GPIOE, \b SYSCTL_PERIPH_GPIOF,
739 //! \b SYSCTL_PERIPH_GPIOG, \b SYSCTL_PERIPH_GPIOH, \b SYSCTL_PERIPH_GPIOJ,
740 //! \b SYSCTL_PERIPH_GPIOK, \b SYSCTL_PERIPH_GPIOL, \b SYSCTL_PERIPH_GPIOM,
741 //! \b SYSCTL_PERIPH_GPION, \b SYSCTL_PERIPH_GPIOP, \b SYSCTL_PERIPH_GPIOQ,
742 //! \b SYSCTL_PERIPH_GPIOR, \b SYSCTL_PERIPH_GPIOS, \b SYSCTL_PERIPH_GPIOT,
743 //! \b SYSCTL_PERIPH_HIBERNATE,
744 //! \b SYSCTL_PERIPH_I2C0, \b SYSCTL_PERIPH_I2C1, \b SYSCTL_PERIPH_I2C2,
745 //! \b SYSCTL_PERIPH_I2C3, \b SYSCTL_PERIPH_I2C4, \b SYSCTL_PERIPH_I2C5,
746 //! \b SYSCTL_PERIPH_I2C6, \b SYSCTL_PERIPH_I2C7, \b SYSCTL_PERIPH_I2C8,
747 //! \b SYSCTL_PERIPH_I2C9, \b SYSCTL_PERIPH_LCD0,
748 //! \b SYSCTL_PERIPH_ONEWIRE0,
749 //! \b SYSCTL_PERIPH_PWM0, \b SYSCTL_PERIPH_PWM1, \b SYSCTL_PERIPH_QEI0,
750 //! \b SYSCTL_PERIPH_QEI1, \b SYSCTL_PERIPH_SSI0, \b SYSCTL_PERIPH_SSI1,
751 //! \b SYSCTL_PERIPH_SSI2, \b SYSCTL_PERIPH_SSI3, \b SYSCTL_PERIPH_TIMER0,
752 //! \b SYSCTL_PERIPH_TIMER1, \b SYSCTL_PERIPH_TIMER2, \b SYSCTL_PERIPH_TIMER3,
753 //! \b SYSCTL_PERIPH_TIMER4, \b SYSCTL_PERIPH_TIMER5, \b SYSCTL_PERIPH_TIMER6,
754 //! \b SYSCTL_PERIPH_TIMER7, \b SYSCTL_PERIPH_UART0, \b SYSCTL_PERIPH_UART1,
755 //! \b SYSCTL_PERIPH_UART2, \b SYSCTL_PERIPH_UART3, \b SYSCTL_PERIPH_UART4,
756 //! \b SYSCTL_PERIPH_UART5, \b SYSCTL_PERIPH_UART6, \b SYSCTL_PERIPH_UART7,
757 //! \b SYSCTL_PERIPH_UDMA, \b SYSCTL_PERIPH_USB0, \b SYSCTL_PERIPH_WDOG0,
758 //! \b SYSCTL_PERIPH_WDOG1, \b SYSCTL_PERIPH_WTIMER0, \b SYSCTL_PERIPH_WTIMER1,
759 //! \b SYSCTL_PERIPH_WTIMER2, \b SYSCTL_PERIPH_WTIMER3,
760 //! \b SYSCTL_PERIPH_WTIMER4, or \b SYSCTL_PERIPH_WTIMER5
761 //!
762 //! \return None.
763 //
764 //*****************************************************************************
765 void
SysCtlPeripheralReset(uint32_t ui32Peripheral)766 SysCtlPeripheralReset(uint32_t ui32Peripheral)
767 {
768     volatile uint_fast8_t ui8Delay;
769 
770     //
771     // Check the arguments.
772     //
773     ASSERT(_SysCtlPeripheralValid(ui32Peripheral));
774 
775     //
776     // Put the peripheral into the reset state.
777     //
778     HWREGBITW(SYSCTL_SRBASE + ((ui32Peripheral & 0xff00) >> 8),
779               ui32Peripheral & 0xff) = 1;
780 
781     //
782     // Delay for a little bit.
783     //
784     for(ui8Delay = 0; ui8Delay < 16; ui8Delay++)
785     {
786     }
787 
788     //
789     // Take the peripheral out of the reset state.
790     //
791     HWREGBITW(SYSCTL_SRBASE + ((ui32Peripheral & 0xff00) >> 8),
792               ui32Peripheral & 0xff) = 0;
793 }
794 
795 //*****************************************************************************
796 //
797 //! Enables a peripheral.
798 //!
799 //! \param ui32Peripheral is the peripheral to enable.
800 //!
801 //! This function enables a peripheral.  At power-up, all peripherals are
802 //! disabled; they must be enabled in order to operate or respond to register
803 //! reads/writes.
804 //!
805 //! The \e ui32Peripheral parameter must be only one of the following values:
806 //! \b SYSCTL_PERIPH_ADC0, \b SYSCTL_PERIPH_ADC1, \b SYSCTL_PERIPH_CAN0,
807 //! \b SYSCTL_PERIPH_CAN1, \b SYSCTL_PERIPH_CCM0,\b SYSCTL_PERIPH_COMP0,
808 //! \b SYSCTL_PERIPH_EEPROM0, \b SYSCTL_PERIPH_EMAC, \b SYSCTL_PERIPH_EPHY,
809 //! \b SYSCTL_PERIPH_EPI0,
810 //! \b SYSCTL_PERIPH_GPIOA, \b SYSCTL_PERIPH_GPIOB, \b SYSCTL_PERIPH_GPIOC,
811 //! \b SYSCTL_PERIPH_GPIOD, \b SYSCTL_PERIPH_GPIOE, \b SYSCTL_PERIPH_GPIOF,
812 //! \b SYSCTL_PERIPH_GPIOG, \b SYSCTL_PERIPH_GPIOH, \b SYSCTL_PERIPH_GPIOJ,
813 //! \b SYSCTL_PERIPH_GPIOK, \b SYSCTL_PERIPH_GPIOL, \b SYSCTL_PERIPH_GPIOM,
814 //! \b SYSCTL_PERIPH_GPION, \b SYSCTL_PERIPH_GPIOP, \b SYSCTL_PERIPH_GPIOQ,
815 //! \b SYSCTL_PERIPH_GPIOR, \b SYSCTL_PERIPH_GPIOS, \b SYSCTL_PERIPH_GPIOT,
816 //! \b SYSCTL_PERIPH_HIBERNATE,
817 //! \b SYSCTL_PERIPH_I2C0, \b SYSCTL_PERIPH_I2C1, \b SYSCTL_PERIPH_I2C2,
818 //! \b SYSCTL_PERIPH_I2C3, \b SYSCTL_PERIPH_I2C4, \b SYSCTL_PERIPH_I2C5,
819 //! \b SYSCTL_PERIPH_I2C6, \b SYSCTL_PERIPH_I2C7, \b SYSCTL_PERIPH_I2C8,
820 //! \b SYSCTL_PERIPH_I2C9, \b SYSCTL_PERIPH_LCD0,
821 //! \b SYSCTL_PERIPH_ONEWIRE0,
822 //! \b SYSCTL_PERIPH_PWM0, \b SYSCTL_PERIPH_PWM1, \b SYSCTL_PERIPH_QEI0,
823 //! \b SYSCTL_PERIPH_QEI1, \b SYSCTL_PERIPH_SSI0, \b SYSCTL_PERIPH_SSI1,
824 //! \b SYSCTL_PERIPH_SSI2, \b SYSCTL_PERIPH_SSI3, \b SYSCTL_PERIPH_TIMER0,
825 //! \b SYSCTL_PERIPH_TIMER1, \b SYSCTL_PERIPH_TIMER2, \b SYSCTL_PERIPH_TIMER3,
826 //! \b SYSCTL_PERIPH_TIMER4, \b SYSCTL_PERIPH_TIMER5, \b SYSCTL_PERIPH_TIMER6,
827 //! \b SYSCTL_PERIPH_TIMER7, \b SYSCTL_PERIPH_UART0, \b SYSCTL_PERIPH_UART1,
828 //! \b SYSCTL_PERIPH_UART2, \b SYSCTL_PERIPH_UART3, \b SYSCTL_PERIPH_UART4,
829 //! \b SYSCTL_PERIPH_UART5, \b SYSCTL_PERIPH_UART6, \b SYSCTL_PERIPH_UART7,
830 //! \b SYSCTL_PERIPH_UDMA, \b SYSCTL_PERIPH_USB0, \b SYSCTL_PERIPH_WDOG0,
831 //! \b SYSCTL_PERIPH_WDOG1, \b SYSCTL_PERIPH_WTIMER0, \b SYSCTL_PERIPH_WTIMER1,
832 //! \b SYSCTL_PERIPH_WTIMER2, \b SYSCTL_PERIPH_WTIMER3,
833 //! \b SYSCTL_PERIPH_WTIMER4, or \b SYSCTL_PERIPH_WTIMER5
834 //!
835 //! \note It takes five clock cycles after the write to enable a peripheral
836 //! before the the peripheral is actually enabled.  During this time, attempts
837 //! to access the peripheral result in a bus fault.  Care should be taken
838 //! to ensure that the peripheral is not accessed during this brief time
839 //! period.
840 //!
841 //! \return None.
842 //
843 //*****************************************************************************
844 void
SysCtlPeripheralEnable(uint32_t ui32Peripheral)845 SysCtlPeripheralEnable(uint32_t ui32Peripheral)
846 {
847     //
848     // Check the arguments.
849     //
850     ASSERT(_SysCtlPeripheralValid(ui32Peripheral));
851 
852     //
853     // Enable this peripheral.
854     //
855     HWREGBITW(SYSCTL_RCGCBASE + ((ui32Peripheral & 0xff00) >> 8),
856               ui32Peripheral & 0xff) = 1;
857 }
858 
859 //*****************************************************************************
860 //
861 //! Disables a peripheral.
862 //!
863 //! \param ui32Peripheral is the peripheral to disable.
864 //!
865 //! This function disables a peripheral.  Once disabled, they do not operate or
866 //! respond to register reads/writes.
867 //!
868 //! The \e ui32Peripheral parameter must be only one of the following values:
869 //! \b SYSCTL_PERIPH_ADC0, \b SYSCTL_PERIPH_ADC1, \b SYSCTL_PERIPH_CAN0,
870 //! \b SYSCTL_PERIPH_CAN1, \b SYSCTL_PERIPH_CCM0,\b SYSCTL_PERIPH_COMP0,
871 //! \b SYSCTL_PERIPH_EEPROM0, \b SYSCTL_PERIPH_EMAC, \b SYSCTL_PERIPH_EPHY,
872 //! \b SYSCTL_PERIPH_EPI0,
873 //! \b SYSCTL_PERIPH_GPIOA, \b SYSCTL_PERIPH_GPIOB, \b SYSCTL_PERIPH_GPIOC,
874 //! \b SYSCTL_PERIPH_GPIOD, \b SYSCTL_PERIPH_GPIOE, \b SYSCTL_PERIPH_GPIOF,
875 //! \b SYSCTL_PERIPH_GPIOG, \b SYSCTL_PERIPH_GPIOH, \b SYSCTL_PERIPH_GPIOJ,
876 //! \b SYSCTL_PERIPH_GPIOK, \b SYSCTL_PERIPH_GPIOL, \b SYSCTL_PERIPH_GPIOM,
877 //! \b SYSCTL_PERIPH_GPION, \b SYSCTL_PERIPH_GPIOP, \b SYSCTL_PERIPH_GPIOQ,
878 //! \b SYSCTL_PERIPH_GPIOR, \b SYSCTL_PERIPH_GPIOS, \b SYSCTL_PERIPH_GPIOT,
879 //! \b SYSCTL_PERIPH_HIBERNATE,
880 //! \b SYSCTL_PERIPH_I2C0, \b SYSCTL_PERIPH_I2C1, \b SYSCTL_PERIPH_I2C2,
881 //! \b SYSCTL_PERIPH_I2C3, \b SYSCTL_PERIPH_I2C4, \b SYSCTL_PERIPH_I2C5,
882 //! \b SYSCTL_PERIPH_I2C6, \b SYSCTL_PERIPH_I2C7, \b SYSCTL_PERIPH_I2C8,
883 //! \b SYSCTL_PERIPH_I2C9, \b SYSCTL_PERIPH_LCD0,
884 //! \b SYSCTL_PERIPH_ONEWIRE0,
885 //! \b SYSCTL_PERIPH_PWM0, \b SYSCTL_PERIPH_PWM1, \b SYSCTL_PERIPH_QEI0,
886 //! \b SYSCTL_PERIPH_QEI1, \b SYSCTL_PERIPH_SSI0, \b SYSCTL_PERIPH_SSI1,
887 //! \b SYSCTL_PERIPH_SSI2, \b SYSCTL_PERIPH_SSI3, \b SYSCTL_PERIPH_TIMER0,
888 //! \b SYSCTL_PERIPH_TIMER1, \b SYSCTL_PERIPH_TIMER2, \b SYSCTL_PERIPH_TIMER3,
889 //! \b SYSCTL_PERIPH_TIMER4, \b SYSCTL_PERIPH_TIMER5, \b SYSCTL_PERIPH_TIMER6,
890 //! \b SYSCTL_PERIPH_TIMER7, \b SYSCTL_PERIPH_UART0, \b SYSCTL_PERIPH_UART1,
891 //! \b SYSCTL_PERIPH_UART2, \b SYSCTL_PERIPH_UART3, \b SYSCTL_PERIPH_UART4,
892 //! \b SYSCTL_PERIPH_UART5, \b SYSCTL_PERIPH_UART6, \b SYSCTL_PERIPH_UART7,
893 //! \b SYSCTL_PERIPH_UDMA, \b SYSCTL_PERIPH_USB0, \b SYSCTL_PERIPH_WDOG0,
894 //! \b SYSCTL_PERIPH_WDOG1, \b SYSCTL_PERIPH_WTIMER0, \b SYSCTL_PERIPH_WTIMER1,
895 //! \b SYSCTL_PERIPH_WTIMER2, \b SYSCTL_PERIPH_WTIMER3,
896 //! \b SYSCTL_PERIPH_WTIMER4, or \b SYSCTL_PERIPH_WTIMER5
897 //!
898 //! \return None.
899 //
900 //*****************************************************************************
901 void
SysCtlPeripheralDisable(uint32_t ui32Peripheral)902 SysCtlPeripheralDisable(uint32_t ui32Peripheral)
903 {
904     //
905     // Check the arguments.
906     //
907     ASSERT(_SysCtlPeripheralValid(ui32Peripheral));
908 
909     //
910     // Disable this peripheral.
911     //
912     HWREGBITW(SYSCTL_RCGCBASE + ((ui32Peripheral & 0xff00) >> 8),
913               ui32Peripheral & 0xff) = 0;
914 }
915 
916 //*****************************************************************************
917 //
918 //! Enables a peripheral in sleep mode.
919 //!
920 //! \param ui32Peripheral is the peripheral to enable in sleep mode.
921 //!
922 //! This function allows a peripheral to continue operating when the processor
923 //! goes into sleep mode.  Because the clocking configuration of the device
924 //! does not change, any peripheral can safely continue operating while the
925 //! processor is in sleep mode and can therefore wake the processor from sleep
926 //! mode.
927 //!
928 //! Sleep mode clocking of peripherals must be enabled via
929 //! SysCtlPeripheralClockGating(); if disabled, the peripheral sleep mode
930 //! configuration is maintained but has no effect when sleep mode is entered.
931 //!
932 //! The \e ui32Peripheral parameter must be only one of the following values:
933 //! \b SYSCTL_PERIPH_ADC0, \b SYSCTL_PERIPH_ADC1, \b SYSCTL_PERIPH_CAN0,
934 //! \b SYSCTL_PERIPH_CAN1, \b SYSCTL_PERIPH_CCM0,\b SYSCTL_PERIPH_COMP0,
935 //! \b SYSCTL_PERIPH_EEPROM0, \b SYSCTL_PERIPH_EMAC, \b SYSCTL_PERIPH_EPHY,
936 //! \b SYSCTL_PERIPH_EPI0,
937 //! \b SYSCTL_PERIPH_GPIOA, \b SYSCTL_PERIPH_GPIOB, \b SYSCTL_PERIPH_GPIOC,
938 //! \b SYSCTL_PERIPH_GPIOD, \b SYSCTL_PERIPH_GPIOE, \b SYSCTL_PERIPH_GPIOF,
939 //! \b SYSCTL_PERIPH_GPIOG, \b SYSCTL_PERIPH_GPIOH, \b SYSCTL_PERIPH_GPIOJ,
940 //! \b SYSCTL_PERIPH_GPIOK, \b SYSCTL_PERIPH_GPIOL, \b SYSCTL_PERIPH_GPIOM,
941 //! \b SYSCTL_PERIPH_GPION, \b SYSCTL_PERIPH_GPIOP, \b SYSCTL_PERIPH_GPIOQ,
942 //! \b SYSCTL_PERIPH_GPIOR, \b SYSCTL_PERIPH_GPIOS, \b SYSCTL_PERIPH_GPIOT,
943 //! \b SYSCTL_PERIPH_HIBERNATE,
944 //! \b SYSCTL_PERIPH_I2C0, \b SYSCTL_PERIPH_I2C1, \b SYSCTL_PERIPH_I2C2,
945 //! \b SYSCTL_PERIPH_I2C3, \b SYSCTL_PERIPH_I2C4, \b SYSCTL_PERIPH_I2C5,
946 //! \b SYSCTL_PERIPH_I2C6, \b SYSCTL_PERIPH_I2C7, \b SYSCTL_PERIPH_I2C8,
947 //! \b SYSCTL_PERIPH_I2C9, \b SYSCTL_PERIPH_LCD0,
948 //! \b SYSCTL_PERIPH_ONEWIRE0,
949 //! \b SYSCTL_PERIPH_PWM0, \b SYSCTL_PERIPH_PWM1, \b SYSCTL_PERIPH_QEI0,
950 //! \b SYSCTL_PERIPH_QEI1, \b SYSCTL_PERIPH_SSI0, \b SYSCTL_PERIPH_SSI1,
951 //! \b SYSCTL_PERIPH_SSI2, \b SYSCTL_PERIPH_SSI3, \b SYSCTL_PERIPH_TIMER0,
952 //! \b SYSCTL_PERIPH_TIMER1, \b SYSCTL_PERIPH_TIMER2, \b SYSCTL_PERIPH_TIMER3,
953 //! \b SYSCTL_PERIPH_TIMER4, \b SYSCTL_PERIPH_TIMER5, \b SYSCTL_PERIPH_TIMER6,
954 //! \b SYSCTL_PERIPH_TIMER7, \b SYSCTL_PERIPH_UART0, \b SYSCTL_PERIPH_UART1,
955 //! \b SYSCTL_PERIPH_UART2, \b SYSCTL_PERIPH_UART3, \b SYSCTL_PERIPH_UART4,
956 //! \b SYSCTL_PERIPH_UART5, \b SYSCTL_PERIPH_UART6, \b SYSCTL_PERIPH_UART7,
957 //! \b SYSCTL_PERIPH_UDMA, \b SYSCTL_PERIPH_USB0, \b SYSCTL_PERIPH_WDOG0,
958 //! \b SYSCTL_PERIPH_WDOG1, \b SYSCTL_PERIPH_WTIMER0, \b SYSCTL_PERIPH_WTIMER1,
959 //! \b SYSCTL_PERIPH_WTIMER2, \b SYSCTL_PERIPH_WTIMER3,
960 //! \b SYSCTL_PERIPH_WTIMER4, or \b SYSCTL_PERIPH_WTIMER5
961 //!
962 //! \return None.
963 //
964 //*****************************************************************************
965 void
SysCtlPeripheralSleepEnable(uint32_t ui32Peripheral)966 SysCtlPeripheralSleepEnable(uint32_t ui32Peripheral)
967 {
968     //
969     // Check the arguments.
970     //
971     ASSERT(_SysCtlPeripheralValid(ui32Peripheral));
972 
973     //
974     // Enable this peripheral in sleep mode.
975     //
976     HWREGBITW(SYSCTL_SCGCBASE + ((ui32Peripheral & 0xff00) >> 8),
977               ui32Peripheral & 0xff) = 1;
978 }
979 
980 //*****************************************************************************
981 //
982 //! Disables a peripheral in sleep mode.
983 //!
984 //! \param ui32Peripheral is the peripheral to disable in sleep mode.
985 //!
986 //! This function causes a peripheral to stop operating when the processor goes
987 //! into sleep mode.  Disabling peripherals while in sleep mode helps to lower
988 //! the current draw of the device.  If enabled (via SysCtlPeripheralEnable()),
989 //! the peripheral automatically resumes operation when the processor
990 //! leaves sleep mode, maintaining its entire state from before sleep mode was
991 //! entered.
992 //!
993 //! Sleep mode clocking of peripherals must be enabled via
994 //! SysCtlPeripheralClockGating(); if disabled, the peripheral sleep mode
995 //! configuration is maintained but has no effect when sleep mode is entered.
996 //!
997 //! The \e ui32Peripheral parameter must be only one of the following values:
998 //! \b SYSCTL_PERIPH_ADC0, \b SYSCTL_PERIPH_ADC1, \b SYSCTL_PERIPH_CAN0,
999 //! \b SYSCTL_PERIPH_CAN1, \b SYSCTL_PERIPH_CCM0,\b SYSCTL_PERIPH_COMP0,
1000 //! \b SYSCTL_PERIPH_EEPROM0, \b SYSCTL_PERIPH_EMAC, \b SYSCTL_PERIPH_EPHY,
1001 //! \b SYSCTL_PERIPH_EPI0,
1002 //! \b SYSCTL_PERIPH_GPIOA, \b SYSCTL_PERIPH_GPIOB, \b SYSCTL_PERIPH_GPIOC,
1003 //! \b SYSCTL_PERIPH_GPIOD, \b SYSCTL_PERIPH_GPIOE, \b SYSCTL_PERIPH_GPIOF,
1004 //! \b SYSCTL_PERIPH_GPIOG, \b SYSCTL_PERIPH_GPIOH, \b SYSCTL_PERIPH_GPIOJ,
1005 //! \b SYSCTL_PERIPH_GPIOK, \b SYSCTL_PERIPH_GPIOL, \b SYSCTL_PERIPH_GPIOM,
1006 //! \b SYSCTL_PERIPH_GPION, \b SYSCTL_PERIPH_GPIOP, \b SYSCTL_PERIPH_GPIOQ,
1007 //! \b SYSCTL_PERIPH_GPIOR, \b SYSCTL_PERIPH_GPIOS, \b SYSCTL_PERIPH_GPIOT,
1008 //! \b SYSCTL_PERIPH_HIBERNATE,
1009 //! \b SYSCTL_PERIPH_I2C0, \b SYSCTL_PERIPH_I2C1, \b SYSCTL_PERIPH_I2C2,
1010 //! \b SYSCTL_PERIPH_I2C3, \b SYSCTL_PERIPH_I2C4, \b SYSCTL_PERIPH_I2C5,
1011 //! \b SYSCTL_PERIPH_I2C6, \b SYSCTL_PERIPH_I2C7, \b SYSCTL_PERIPH_I2C8,
1012 //! \b SYSCTL_PERIPH_I2C9, \b SYSCTL_PERIPH_LCD0,
1013 //! \b SYSCTL_PERIPH_ONEWIRE0,
1014 //! \b SYSCTL_PERIPH_PWM0, \b SYSCTL_PERIPH_PWM1, \b SYSCTL_PERIPH_QEI0,
1015 //! \b SYSCTL_PERIPH_QEI1, \b SYSCTL_PERIPH_SSI0, \b SYSCTL_PERIPH_SSI1,
1016 //! \b SYSCTL_PERIPH_SSI2, \b SYSCTL_PERIPH_SSI3, \b SYSCTL_PERIPH_TIMER0,
1017 //! \b SYSCTL_PERIPH_TIMER1, \b SYSCTL_PERIPH_TIMER2, \b SYSCTL_PERIPH_TIMER3,
1018 //! \b SYSCTL_PERIPH_TIMER4, \b SYSCTL_PERIPH_TIMER5, \b SYSCTL_PERIPH_TIMER6,
1019 //! \b SYSCTL_PERIPH_TIMER7, \b SYSCTL_PERIPH_UART0, \b SYSCTL_PERIPH_UART1,
1020 //! \b SYSCTL_PERIPH_UART2, \b SYSCTL_PERIPH_UART3, \b SYSCTL_PERIPH_UART4,
1021 //! \b SYSCTL_PERIPH_UART5, \b SYSCTL_PERIPH_UART6, \b SYSCTL_PERIPH_UART7,
1022 //! \b SYSCTL_PERIPH_UDMA, \b SYSCTL_PERIPH_USB0, \b SYSCTL_PERIPH_WDOG0,
1023 //! \b SYSCTL_PERIPH_WDOG1, \b SYSCTL_PERIPH_WTIMER0, \b SYSCTL_PERIPH_WTIMER1,
1024 //! \b SYSCTL_PERIPH_WTIMER2, \b SYSCTL_PERIPH_WTIMER3,
1025 //! \b SYSCTL_PERIPH_WTIMER4, or \b SYSCTL_PERIPH_WTIMER5
1026 //!
1027 //! \return None.
1028 //
1029 //*****************************************************************************
1030 void
SysCtlPeripheralSleepDisable(uint32_t ui32Peripheral)1031 SysCtlPeripheralSleepDisable(uint32_t ui32Peripheral)
1032 {
1033     //
1034     // Check the arguments.
1035     //
1036     ASSERT(_SysCtlPeripheralValid(ui32Peripheral));
1037 
1038     //
1039     // Disable this peripheral in sleep mode.
1040     //
1041     HWREGBITW(SYSCTL_SCGCBASE + ((ui32Peripheral & 0xff00) >> 8),
1042               ui32Peripheral & 0xff) = 0;
1043 }
1044 
1045 //*****************************************************************************
1046 //
1047 //! Enables a peripheral in deep-sleep mode.
1048 //!
1049 //! \param ui32Peripheral is the peripheral to enable in deep-sleep mode.
1050 //!
1051 //! This function allows a peripheral to continue operating when the processor
1052 //! goes into deep-sleep mode.  Because the clocking configuration of the
1053 //! device may change, not all peripherals can safely continue operating while
1054 //! the processor is in deep-sleep mode.  Those that must run at a particular
1055 //! frequency (such as a UART) do not work as expected if the clock changes.
1056 //! It is the responsibility of the caller to make sensible choices.
1057 //!
1058 //! Deep-sleep mode clocking of peripherals must be enabled via
1059 //! SysCtlPeripheralClockGating(); if disabled, the peripheral deep-sleep mode
1060 //! configuration is maintained but has no effect when deep-sleep mode is
1061 //! entered.
1062 //!
1063 //! The \e ui32Peripheral parameter must be only one of the following values:
1064 //! \b SYSCTL_PERIPH_ADC0, \b SYSCTL_PERIPH_ADC1, \b SYSCTL_PERIPH_CAN0,
1065 //! \b SYSCTL_PERIPH_CAN1, \b SYSCTL_PERIPH_CCM0,\b SYSCTL_PERIPH_COMP0,
1066 //! \b SYSCTL_PERIPH_EEPROM0, \b SYSCTL_PERIPH_EMAC, \b SYSCTL_PERIPH_EPHY,
1067 //! \b SYSCTL_PERIPH_EPI0,
1068 //! \b SYSCTL_PERIPH_GPIOA, \b SYSCTL_PERIPH_GPIOB, \b SYSCTL_PERIPH_GPIOC,
1069 //! \b SYSCTL_PERIPH_GPIOD, \b SYSCTL_PERIPH_GPIOE, \b SYSCTL_PERIPH_GPIOF,
1070 //! \b SYSCTL_PERIPH_GPIOG, \b SYSCTL_PERIPH_GPIOH, \b SYSCTL_PERIPH_GPIOJ,
1071 //! \b SYSCTL_PERIPH_GPIOK, \b SYSCTL_PERIPH_GPIOL, \b SYSCTL_PERIPH_GPIOM,
1072 //! \b SYSCTL_PERIPH_GPION, \b SYSCTL_PERIPH_GPIOP, \b SYSCTL_PERIPH_GPIOQ,
1073 //! \b SYSCTL_PERIPH_GPIOR, \b SYSCTL_PERIPH_GPIOS, \b SYSCTL_PERIPH_GPIOT,
1074 //! \b SYSCTL_PERIPH_HIBERNATE,
1075 //! \b SYSCTL_PERIPH_I2C0, \b SYSCTL_PERIPH_I2C1, \b SYSCTL_PERIPH_I2C2,
1076 //! \b SYSCTL_PERIPH_I2C3, \b SYSCTL_PERIPH_I2C4, \b SYSCTL_PERIPH_I2C5,
1077 //! \b SYSCTL_PERIPH_I2C6, \b SYSCTL_PERIPH_I2C7, \b SYSCTL_PERIPH_I2C8,
1078 //! \b SYSCTL_PERIPH_I2C9, \b SYSCTL_PERIPH_LCD0,
1079 //! \b SYSCTL_PERIPH_ONEWIRE0,
1080 //! \b SYSCTL_PERIPH_PWM0, \b SYSCTL_PERIPH_PWM1, \b SYSCTL_PERIPH_QEI0,
1081 //! \b SYSCTL_PERIPH_QEI1, \b SYSCTL_PERIPH_SSI0, \b SYSCTL_PERIPH_SSI1,
1082 //! \b SYSCTL_PERIPH_SSI2, \b SYSCTL_PERIPH_SSI3, \b SYSCTL_PERIPH_TIMER0,
1083 //! \b SYSCTL_PERIPH_TIMER1, \b SYSCTL_PERIPH_TIMER2, \b SYSCTL_PERIPH_TIMER3,
1084 //! \b SYSCTL_PERIPH_TIMER4, \b SYSCTL_PERIPH_TIMER5, \b SYSCTL_PERIPH_TIMER6,
1085 //! \b SYSCTL_PERIPH_TIMER7, \b SYSCTL_PERIPH_UART0, \b SYSCTL_PERIPH_UART1,
1086 //! \b SYSCTL_PERIPH_UART2, \b SYSCTL_PERIPH_UART3, \b SYSCTL_PERIPH_UART4,
1087 //! \b SYSCTL_PERIPH_UART5, \b SYSCTL_PERIPH_UART6, \b SYSCTL_PERIPH_UART7,
1088 //! \b SYSCTL_PERIPH_UDMA, \b SYSCTL_PERIPH_USB0, \b SYSCTL_PERIPH_WDOG0,
1089 //! \b SYSCTL_PERIPH_WDOG1, \b SYSCTL_PERIPH_WTIMER0, \b SYSCTL_PERIPH_WTIMER1,
1090 //! \b SYSCTL_PERIPH_WTIMER2, \b SYSCTL_PERIPH_WTIMER3,
1091 //! \b SYSCTL_PERIPH_WTIMER4, or \b SYSCTL_PERIPH_WTIMER5
1092 //!
1093 //! \return None.
1094 //
1095 //*****************************************************************************
1096 void
SysCtlPeripheralDeepSleepEnable(uint32_t ui32Peripheral)1097 SysCtlPeripheralDeepSleepEnable(uint32_t ui32Peripheral)
1098 {
1099     //
1100     // Check the arguments.
1101     //
1102     ASSERT(_SysCtlPeripheralValid(ui32Peripheral));
1103 
1104     //
1105     // Enable this peripheral in deep-sleep mode.
1106     //
1107     HWREGBITW(SYSCTL_DCGCBASE + ((ui32Peripheral & 0xff00) >> 8),
1108               ui32Peripheral & 0xff) = 1;
1109 }
1110 
1111 //*****************************************************************************
1112 //
1113 //! Disables a peripheral in deep-sleep mode.
1114 //!
1115 //! \param ui32Peripheral is the peripheral to disable in deep-sleep mode.
1116 //!
1117 //! This function causes a peripheral to stop operating when the processor goes
1118 //! into deep-sleep mode.  Disabling peripherals while in deep-sleep mode helps
1119 //! to lower the current draw of the device, and can keep peripherals that
1120 //! require a particular clock frequency from operating when the clock changes
1121 //! as a result of entering deep-sleep mode.  If enabled (via
1122 //! SysCtlPeripheralEnable()), the peripheral automatically resumes
1123 //! operation when the processor leaves deep-sleep mode, maintaining its entire
1124 //! state from before deep-sleep mode was entered.
1125 //!
1126 //! Deep-sleep mode clocking of peripherals must be enabled via
1127 //! SysCtlPeripheralClockGating(); if disabled, the peripheral deep-sleep mode
1128 //! configuration is maintained but has no effect when deep-sleep mode is
1129 //! entered.
1130 //!
1131 //! The \e ui32Peripheral parameter must be only one of the following values:
1132 //! \b SYSCTL_PERIPH_ADC0, \b SYSCTL_PERIPH_ADC1, \b SYSCTL_PERIPH_CAN0,
1133 //! \b SYSCTL_PERIPH_CAN1, \b SYSCTL_PERIPH_CCM0,\b SYSCTL_PERIPH_COMP0,
1134 //! \b SYSCTL_PERIPH_EEPROM0, \b SYSCTL_PERIPH_EMAC, \b SYSCTL_PERIPH_EPHY,
1135 //! \b SYSCTL_PERIPH_EPI0,
1136 //! \b SYSCTL_PERIPH_GPIOA, \b SYSCTL_PERIPH_GPIOB, \b SYSCTL_PERIPH_GPIOC,
1137 //! \b SYSCTL_PERIPH_GPIOD, \b SYSCTL_PERIPH_GPIOE, \b SYSCTL_PERIPH_GPIOF,
1138 //! \b SYSCTL_PERIPH_GPIOG, \b SYSCTL_PERIPH_GPIOH, \b SYSCTL_PERIPH_GPIOJ,
1139 //! \b SYSCTL_PERIPH_GPIOK, \b SYSCTL_PERIPH_GPIOL, \b SYSCTL_PERIPH_GPIOM,
1140 //! \b SYSCTL_PERIPH_GPION, \b SYSCTL_PERIPH_GPIOP, \b SYSCTL_PERIPH_GPIOQ,
1141 //! \b SYSCTL_PERIPH_GPIOR, \b SYSCTL_PERIPH_GPIOS, \b SYSCTL_PERIPH_GPIOT,
1142 //! \b SYSCTL_PERIPH_HIBERNATE,
1143 //! \b SYSCTL_PERIPH_I2C0, \b SYSCTL_PERIPH_I2C1, \b SYSCTL_PERIPH_I2C2,
1144 //! \b SYSCTL_PERIPH_I2C3, \b SYSCTL_PERIPH_I2C4, \b SYSCTL_PERIPH_I2C5,
1145 //! \b SYSCTL_PERIPH_I2C6, \b SYSCTL_PERIPH_I2C7, \b SYSCTL_PERIPH_I2C8,
1146 //! \b SYSCTL_PERIPH_I2C9, \b SYSCTL_PERIPH_LCD0,
1147 //! \b SYSCTL_PERIPH_ONEWIRE0,
1148 //! \b SYSCTL_PERIPH_PWM0, \b SYSCTL_PERIPH_PWM1, \b SYSCTL_PERIPH_QEI0,
1149 //! \b SYSCTL_PERIPH_QEI1, \b SYSCTL_PERIPH_SSI0, \b SYSCTL_PERIPH_SSI1,
1150 //! \b SYSCTL_PERIPH_SSI2, \b SYSCTL_PERIPH_SSI3, \b SYSCTL_PERIPH_TIMER0,
1151 //! \b SYSCTL_PERIPH_TIMER1, \b SYSCTL_PERIPH_TIMER2, \b SYSCTL_PERIPH_TIMER3,
1152 //! \b SYSCTL_PERIPH_TIMER4, \b SYSCTL_PERIPH_TIMER5, \b SYSCTL_PERIPH_TIMER6,
1153 //! \b SYSCTL_PERIPH_TIMER7, \b SYSCTL_PERIPH_UART0, \b SYSCTL_PERIPH_UART1,
1154 //! \b SYSCTL_PERIPH_UART2, \b SYSCTL_PERIPH_UART3, \b SYSCTL_PERIPH_UART4,
1155 //! \b SYSCTL_PERIPH_UART5, \b SYSCTL_PERIPH_UART6, \b SYSCTL_PERIPH_UART7,
1156 //! \b SYSCTL_PERIPH_UDMA, \b SYSCTL_PERIPH_USB0, \b SYSCTL_PERIPH_WDOG0,
1157 //! \b SYSCTL_PERIPH_WDOG1, \b SYSCTL_PERIPH_WTIMER0, \b SYSCTL_PERIPH_WTIMER1,
1158 //! \b SYSCTL_PERIPH_WTIMER2, \b SYSCTL_PERIPH_WTIMER3,
1159 //! \b SYSCTL_PERIPH_WTIMER4, or \b SYSCTL_PERIPH_WTIMER5
1160 //!
1161 //! \return None.
1162 //
1163 //*****************************************************************************
1164 void
SysCtlPeripheralDeepSleepDisable(uint32_t ui32Peripheral)1165 SysCtlPeripheralDeepSleepDisable(uint32_t ui32Peripheral)
1166 {
1167     //
1168     // Check the arguments.
1169     //
1170     ASSERT(_SysCtlPeripheralValid(ui32Peripheral));
1171 
1172     //
1173     // Disable this peripheral in deep-sleep mode.
1174     //
1175     HWREGBITW(SYSCTL_DCGCBASE + ((ui32Peripheral & 0xff00) >> 8),
1176               ui32Peripheral & 0xff) = 0;
1177 }
1178 
1179 //*****************************************************************************
1180 //
1181 //! Controls peripheral clock gating in sleep and deep-sleep mode.
1182 //!
1183 //! \param bEnable is a boolean that is \b true if the sleep and deep-sleep
1184 //! peripheral configuration should be used and \b false if not.
1185 //!
1186 //! This function controls how peripherals are clocked when the processor goes
1187 //! into sleep or deep-sleep mode.  By default, the peripherals are clocked the
1188 //! same as in run mode; if peripheral clock gating is enabled, they are
1189 //! clocked according to the configuration set by
1190 //! SysCtlPeripheralSleepEnable(), SysCtlPeripheralSleepDisable(),
1191 //! SysCtlPeripheralDeepSleepEnable(), and SysCtlPeripheralDeepSleepDisable().
1192 //!
1193 //! \return None.
1194 //
1195 //*****************************************************************************
1196 void
SysCtlPeripheralClockGating(bool bEnable)1197 SysCtlPeripheralClockGating(bool bEnable)
1198 {
1199     if(CLASS_IS_TM4C123)
1200     {
1201         //
1202         // Enable peripheral clock gating as requested.
1203         //
1204         if(bEnable)
1205         {
1206             HWREG(SYSCTL_RCC) |= SYSCTL_RCC_ACG;
1207         }
1208         else
1209         {
1210             HWREG(SYSCTL_RCC) &= ~(SYSCTL_RCC_ACG);
1211         }
1212     }
1213     else
1214     {
1215         //
1216         // Enable peripheral clock gating as requested.
1217         //
1218         if(bEnable)
1219         {
1220             HWREG(SYSCTL_RSCLKCFG) |= SYSCTL_RSCLKCFG_ACG;
1221         }
1222         else
1223         {
1224             HWREG(SYSCTL_RSCLKCFG) &= ~SYSCTL_RSCLKCFG_ACG;
1225         }
1226     }
1227 }
1228 
1229 //*****************************************************************************
1230 //
1231 //! Registers an interrupt handler for the system control interrupt.
1232 //!
1233 //! \param pfnHandler is a pointer to the function to be called when the system
1234 //! control interrupt occurs.
1235 //!
1236 //! This function registers the handler to be called when a system control
1237 //! interrupt occurs.  This function enables the global interrupt in the
1238 //! interrupt controller; specific system control interrupts must be enabled
1239 //! via SysCtlIntEnable().  It is the interrupt handler's responsibility to
1240 //! clear the interrupt source via SysCtlIntClear().
1241 //!
1242 //! System control can generate interrupts when the PLL achieves lock, if the
1243 //! internal LDO current limit is exceeded, if the internal oscillator fails,
1244 //! if the main oscillator fails, if the internal LDO output voltage droops too
1245 //! much, if the external voltage droops too much, or if the PLL fails.
1246 //!
1247 //! \sa IntRegister() for important information about registering interrupt
1248 //! handlers.
1249 //!
1250 //! \note The events that cause system control interrupts vary based on the
1251 //! Tiva part in use.  Please consult the data sheet for the part you are
1252 //! using to determine which interrupt sources are available.
1253 //!
1254 //! \return None.
1255 //
1256 //*****************************************************************************
1257 void
SysCtlIntRegister(void (* pfnHandler)(void))1258 SysCtlIntRegister(void (*pfnHandler)(void))
1259 {
1260     //
1261     // Register the interrupt handler, returning an error if an error occurs.
1262     //
1263     IntRegister(INT_SYSCTL_TM4C123, pfnHandler);
1264 
1265     //
1266     // Enable the system control interrupt.
1267     //
1268     IntEnable(INT_SYSCTL_TM4C123);
1269 }
1270 
1271 //*****************************************************************************
1272 //
1273 //! Unregisters the interrupt handler for the system control interrupt.
1274 //!
1275 //! This function unregisters the handler to be called when a system control
1276 //! interrupt occurs.  This function also masks off the interrupt in the
1277 //! interrupt controller so that the interrupt handler no longer is called.
1278 //!
1279 //! \sa IntRegister() for important information about registering interrupt
1280 //! handlers.
1281 //!
1282 //! \return None.
1283 //
1284 //*****************************************************************************
1285 void
SysCtlIntUnregister(void)1286 SysCtlIntUnregister(void)
1287 {
1288     //
1289     // Disable the interrupt.
1290     //
1291     IntDisable(INT_SYSCTL_TM4C123);
1292 
1293     //
1294     // Unregister the interrupt handler.
1295     //
1296     IntUnregister(INT_SYSCTL_TM4C123);
1297 }
1298 
1299 //*****************************************************************************
1300 //
1301 //! Enables individual system control interrupt sources.
1302 //!
1303 //! \param ui32Ints is a bit mask of the interrupt sources to be enabled.  Must
1304 //! be a logical OR of \b SYSCTL_INT_BOR0, \b SYSCTL_INT_VDDA_OK,
1305 //! \b SYSCTL_INT_MOSC_PUP, \b SYSCTL_INT_USBPLL_LOCK,
1306 //! \b SYSCTL_INT_PLL_LOCK, \b SYSCTL_INT_MOSC_FAIL, \b SYSCTL_INT_BOR, and/or
1307 //! \b SYSCTL_INT_BOR1.
1308 //!
1309 //! This function enables the indicated system control interrupt sources.  Only
1310 //! the sources that are enabled can be reflected to the processor interrupt;
1311 //! disabled sources have no effect on the processor.
1312 //!
1313 //! \note The interrupt sources vary based on the Tiva part in use.
1314 //! Please consult the data sheet for the part you are using to determine
1315 //! which interrupt sources are available.
1316 //!
1317 //! \return None.
1318 //
1319 //*****************************************************************************
1320 void
SysCtlIntEnable(uint32_t ui32Ints)1321 SysCtlIntEnable(uint32_t ui32Ints)
1322 {
1323     //
1324     // Enable the specified interrupts.
1325     //
1326     HWREG(SYSCTL_IMC) |= ui32Ints;
1327 }
1328 
1329 //*****************************************************************************
1330 //
1331 //! Disables individual system control interrupt sources.
1332 //!
1333 //! \param ui32Ints is a bit mask of the interrupt sources to be disabled.
1334 //! Must be a logical OR of \b SYSCTL_INT_BOR0, \b SYSCTL_INT_VDDA_OK,
1335 //! \b SYSCTL_INT_MOSC_PUP, \b SYSCTL_INT_USBPLL_LOCK,
1336 //! \b SYSCTL_INT_PLL_LOCK, \b SYSCTL_INT_MOSC_FAIL, \b SYSCTL_INT_BOR, and/or
1337 //! \b SYSCTL_INT_BOR1.
1338 //!
1339 //! This function disables the indicated system control interrupt sources.
1340 //! Only the sources that are enabled can be reflected to the processor
1341 //! interrupt; disabled sources have no effect on the processor.
1342 //!
1343 //! \note The interrupt sources vary based on the Tiva part in use.
1344 //! Please consult the data sheet for the part you are using to determine
1345 //! which interrupt sources are available.
1346 //!
1347 //! \return None.
1348 //
1349 //*****************************************************************************
1350 void
SysCtlIntDisable(uint32_t ui32Ints)1351 SysCtlIntDisable(uint32_t ui32Ints)
1352 {
1353     //
1354     // Disable the specified interrupts.
1355     //
1356     HWREG(SYSCTL_IMC) &= ~(ui32Ints);
1357 }
1358 
1359 //*****************************************************************************
1360 //
1361 //! Clears system control interrupt sources.
1362 //!
1363 //! \param ui32Ints is a bit mask of the interrupt sources to be cleared.  Must
1364 //! be a logical OR of \b SYSCTL_INT_BOR0, \b SYSCTL_INT_VDDA_OK,
1365 //! \b SYSCTL_INT_MOSC_PUP, \b SYSCTL_INT_USBPLL_LOCK,
1366 //! \b SYSCTL_INT_PLL_LOCK, \b SYSCTL_INT_MOSC_FAIL, \b SYSCTL_INT_BOR, and/or
1367 //! \b SYSCTL_INT_BOR1.
1368 //!
1369 //! The specified system control interrupt sources are cleared, so that they no
1370 //! longer assert.  This function must be called in the interrupt handler to
1371 //! keep it from being called again immediately on exit.
1372 //!
1373 //! \note Because there is a write buffer in the Cortex-M processor, it may
1374 //! take several clock cycles before the interrupt source is actually cleared.
1375 //! Therefore, it is recommended that the interrupt source be cleared early in
1376 //! the interrupt handler (as opposed to the very last action) to avoid
1377 //! returning from the interrupt handler before the interrupt source is
1378 //! actually cleared.  Failure to do so may result in the interrupt handler
1379 //! being immediately reentered (because the interrupt controller still sees
1380 //! the interrupt source asserted).
1381 //!
1382 //! \note The interrupt sources vary based on the Tiva part in use.
1383 //! Please consult the data sheet for the part you are using to determine
1384 //! which interrupt sources are available.
1385 //!
1386 //! \return None.
1387 //
1388 //*****************************************************************************
1389 void
SysCtlIntClear(uint32_t ui32Ints)1390 SysCtlIntClear(uint32_t ui32Ints)
1391 {
1392     //
1393     // Clear the requested interrupt sources.
1394     //
1395     HWREG(SYSCTL_MISC) = ui32Ints;
1396 }
1397 
1398 //*****************************************************************************
1399 //
1400 //! Gets the current interrupt status.
1401 //!
1402 //! \param bMasked is false if the raw interrupt status is required and true if
1403 //! the masked interrupt status is required.
1404 //!
1405 //! This function returns the interrupt status for the system controller.
1406 //! Either the raw interrupt status or the status of interrupts that are
1407 //! allowed to reflect to the processor can be returned.
1408 //!
1409 //! \note The interrupt sources vary based on the Tiva part in use.
1410 //! Please consult the data sheet for the part you are using to determine
1411 //! which interrupt sources are available.
1412 //!
1413 //! \return The current interrupt status, enumerated as a bit field of
1414 //! \b SYSCTL_INT_BOR0, \b SYSCTL_INT_VDDA_OK,
1415 //! \b SYSCTL_INT_MOSC_PUP, \b SYSCTL_INT_USBPLL_LOCK,
1416 //! \b SYSCTL_INT_PLL_LOCK, \b SYSCTL_INT_MOSC_FAIL, \b SYSCTL_INT_BOR, and/or
1417 //! \b SYSCTL_INT_BOR1.
1418 //
1419 //*****************************************************************************
1420 uint32_t
SysCtlIntStatus(bool bMasked)1421 SysCtlIntStatus(bool bMasked)
1422 {
1423     //
1424     // Return either the interrupt status or the raw interrupt status as
1425     // requested.
1426     //
1427     if(bMasked)
1428     {
1429         return(HWREG(SYSCTL_MISC));
1430     }
1431     else
1432     {
1433         return(HWREG(SYSCTL_RIS));
1434     }
1435 }
1436 
1437 //*****************************************************************************
1438 //
1439 //! Sets the output voltage of the LDO when the device enters sleep mode.
1440 //!
1441 //! \param ui32Voltage is the required output voltage from the LDO while in
1442 //! sleep mode.
1443 //!
1444 //! This function sets the output voltage of the LDO while in sleep mode.
1445 //! The \e ui32Voltage parameter must be one of the following values:
1446 //! \b SYSCTL_LDO_0_90V, \b SYSCTL_LDO_0_95V, \b SYSCTL_LDO_1_00V,
1447 //! \b SYSCTL_LDO_1_05V, \b SYSCTL_LDO_1_10V, \b SYSCTL_LDO_1_15V, or
1448 //! \b SYSCTL_LDO_1_20V.
1449 //!
1450 //! \note The availability of this feature, the default LDO voltage, and the
1451 //! adjustment range varies with the Tiva part in use.  Please consult the
1452 //! data sheet for the part you are using to determine whether this support is
1453 //! available.
1454 //!
1455 //! \return None.
1456 //
1457 //*****************************************************************************
1458 void
SysCtlLDOSleepSet(uint32_t ui32Voltage)1459 SysCtlLDOSleepSet(uint32_t ui32Voltage)
1460 {
1461     //
1462     // Check the arguments.
1463     //
1464     ASSERT((ui32Voltage == SYSCTL_LDO_0_90V) ||
1465            (ui32Voltage == SYSCTL_LDO_0_95V) ||
1466            (ui32Voltage == SYSCTL_LDO_1_00V) ||
1467            (ui32Voltage == SYSCTL_LDO_1_05V) ||
1468            (ui32Voltage == SYSCTL_LDO_1_10V) ||
1469            (ui32Voltage == SYSCTL_LDO_1_15V) ||
1470            (ui32Voltage == SYSCTL_LDO_1_20V));
1471 
1472     //
1473     // Set the sleep-mode LDO voltage to the requested value.
1474     //
1475     HWREG(SYSCTL_LDOSPCTL) = ui32Voltage;
1476 }
1477 
1478 //*****************************************************************************
1479 //
1480 //! Returns the output voltage of the LDO when the device enters sleep mode.
1481 //!
1482 //! This function determines the output voltage of the LDO while in sleep mode,
1483 //! as specified by the control register.
1484 //!
1485 //! \note The availability of this feature, the default LDO voltage, and the
1486 //! adjustment range varies with the Tiva part in use.  Please consult the
1487 //! data sheet for the part you are using to determine whether this support is
1488 //! available.
1489 //!
1490 //! \return Returns the sleep-mode voltage of the LDO and is one of
1491 //! \b SYSCTL_LDO_0_90V, \b SYSCTL_LDO_0_95V, \b SYSCTL_LDO_1_00V,
1492 //! \b SYSCTL_LDO_1_05V, \b SYSCTL_LDO_1_10V, \b SYSCTL_LDO_1_15V, or
1493 //! \b SYSCTL_LDO_1_20V.
1494 //
1495 //*****************************************************************************
1496 uint32_t
SysCtlLDOSleepGet(void)1497 SysCtlLDOSleepGet(void)
1498 {
1499     //
1500     // Return the sleep-mode LDO voltage setting.
1501     //
1502     return(HWREG(SYSCTL_LDOSPCTL));
1503 }
1504 
1505 //*****************************************************************************
1506 //
1507 //! Sets the output voltage of the LDO when the device enters deep-sleep
1508 //! mode.
1509 //!
1510 //! \param ui32Voltage is the required output voltage from the LDO while in
1511 //! deep-sleep mode.
1512 //!
1513 //! This function sets the output voltage of the LDO while in deep-sleep mode.
1514 //! The \e ui32Voltage parameter specifies the output voltage of the LDO and
1515 //! must be one of the following values: \b SYSCTL_LDO_0_90V,
1516 //! \b SYSCTL_LDO_0_95V, \b SYSCTL_LDO_1_00V, \b SYSCTL_LDO_1_05V,
1517 //! \b SYSCTL_LDO_1_10V, \b SYSCTL_LDO_1_15V, or \b SYSCTL_LDO_1_20V.
1518 //!
1519 //! \note The availability of this feature, the default LDO voltage, and the
1520 //! adjustment range varies with the Tiva part in use.  Please consult the
1521 //! data sheet for the part you are using to determine whether this support is
1522 //! available.
1523 //!
1524 //! \return None.
1525 //
1526 //*****************************************************************************
1527 void
SysCtlLDODeepSleepSet(uint32_t ui32Voltage)1528 SysCtlLDODeepSleepSet(uint32_t ui32Voltage)
1529 {
1530     //
1531     // Check the arguments.
1532     //
1533     ASSERT((ui32Voltage == SYSCTL_LDO_0_90V) ||
1534            (ui32Voltage == SYSCTL_LDO_0_95V) ||
1535            (ui32Voltage == SYSCTL_LDO_1_00V) ||
1536            (ui32Voltage == SYSCTL_LDO_1_05V) ||
1537            (ui32Voltage == SYSCTL_LDO_1_10V) ||
1538            (ui32Voltage == SYSCTL_LDO_1_15V) ||
1539            (ui32Voltage == SYSCTL_LDO_1_20V));
1540 
1541     //
1542     // Set the deep-sleep LDO voltage to the requested value.
1543     //
1544     HWREG(SYSCTL_LDODPCTL) = ui32Voltage;
1545 }
1546 
1547 //*****************************************************************************
1548 //
1549 //! Returns the output voltage of the LDO when the device enters deep-sleep
1550 //! mode.
1551 //!
1552 //! This function returns the output voltage of the LDO when the device is
1553 //! in deep-sleep mode, as specified by the control register.
1554 //!
1555 //! \note The availability of this feature, the default LDO voltage, and the
1556 //! adjustment range varies with the Tiva part in use.  Please consult the
1557 //! data sheet for the part you are using to determine whether this support is
1558 //! available.
1559 //!
1560 //! \return Returns the deep-sleep-mode voltage of the LDO; is one of
1561 //! \b SYSCTL_LDO_0_90V, \b SYSCTL_LDO_0_95V, \b SYSCTL_LDO_1_00V,
1562 //! \b SYSCTL_LDO_1_05V, \b SYSCTL_LDO_1_10V, \b SYSCTL_LDO_1_15V, or
1563 //! \b SYSCTL_LDO_1_20V.
1564 //
1565 //*****************************************************************************
1566 uint32_t
SysCtlLDODeepSleepGet(void)1567 SysCtlLDODeepSleepGet(void)
1568 {
1569     //
1570     // Return the deep-sleep-mode LDO voltage setting.
1571     //
1572     return(HWREG(SYSCTL_LDODPCTL));
1573 }
1574 
1575 //*****************************************************************************
1576 //
1577 //! Configures the power to the flash and SRAM while in sleep mode.
1578 //!
1579 //! \param ui32Config is the required flash and SRAM power configuration.
1580 //!
1581 //! This function allows the power configuration of the flash and SRAM while in
1582 //! sleep mode to be set.  The \e ui32Config parameter is the logical OR of the
1583 //! flash power configuration and the SRAM power configuration.
1584 //!
1585 //! The flash power configuration is specified as either:
1586 //!
1587 //! - \b SYSCTL_FLASH_NORMAL - The flash is left in fully powered mode,
1588 //!    providing fast wake-up time but higher power consumption.
1589 //! - \b SYSCTL_FLASH_LOW_POWER - The flash is in low power mode, providing
1590 //!   reduced power consumption but longer wake-up time.
1591 //!
1592 //! The SRAM power configuration is specified as one of:
1593 //!
1594 //! - \b SYSCTL_SRAM_NORMAL - The SRAM is left in fully powered mode, providing
1595 //!   fast wake-up time but higher power consumption.
1596 //! - \b SYSCTL_SRAM_STANDBY - The SRAM is placed into a lower power mode,
1597 //!   providing reduced power consumption but longer wake-up time.
1598 //! - \b SYSCTL_SRAM_LOW_POWER - The SRAM is placed into lowest power mode,
1599 //!   providing further reduced power consumption but longer wake-up time.
1600 //!
1601 //! \note The availability of this feature varies with the Tiva part in
1602 //! use.  Please consult the data sheet for the part you are using to determine
1603 //! whether this support is available.
1604 //!
1605 //! \return None.
1606 //
1607 //*****************************************************************************
1608 void
SysCtlSleepPowerSet(uint32_t ui32Config)1609 SysCtlSleepPowerSet(uint32_t ui32Config)
1610 {
1611     //
1612     // Set the sleep-mode flash and SRAM power configuration.
1613     //
1614     HWREG(SYSCTL_SLPPWRCFG) = ui32Config;
1615 }
1616 
1617 //*****************************************************************************
1618 //
1619 //! Configures the power to the flash and SRAM while in deep-sleep mode.
1620 //!
1621 //! \param ui32Config is the required flash and SRAM power configuration.
1622 //!
1623 //! This function allows the power configuration of the flash and SRAM while in
1624 //! deep-sleep mode to be set.  The \e ui32Config parameter is the logical OR
1625 //! of the flash power configuration and the SRAM power configuration.
1626 //!
1627 //! The flash power configuration is specified as either:
1628 //!
1629 //! - \b SYSCTL_FLASH_NORMAL - The flash is left in fully powered mode,
1630 //!    providing fast wake-up time but higher power consumption.
1631 //! - \b SYSCTL_FLASH_LOW_POWER - The flash is in low power mode, providing
1632 //!   reduced power consumption but longer wake-up time.
1633 //!
1634 //! The SRAM power configuration is specified as one of:
1635 //!
1636 //! - \b SYSCTL_LDO_SLEEP - The LDO is in sleep mode.
1637 //! - \b SYSCTL_TEMP_LOW_POWER - The temperature sensor in low power mode.
1638 //! - \b SYSCTL_SRAM_NORMAL - The SRAM is left in fully powered mode, providing
1639 //!   fast wake-up time but higher power consumption.
1640 //! - \b SYSCTL_SRAM_STANDBY - The SRAM is placed into a lower power mode,
1641 //!   providing reduced power consumption but longer wake-up time.
1642 //! - \b SYSCTL_SRAM_LOW_POWER - The SRAM is placed into lowest power mode,
1643 //!   providing further reduced power consumption but longer wake-up time.
1644 //!
1645 //! \note The availability of this feature varies with the Tiva part in
1646 //! use.  Please consult the data sheet for the part you are using to determine
1647 //! whether this support is available.
1648 //!
1649 //! \return None.
1650 //
1651 //*****************************************************************************
1652 void
SysCtlDeepSleepPowerSet(uint32_t ui32Config)1653 SysCtlDeepSleepPowerSet(uint32_t ui32Config)
1654 {
1655     //
1656     // Set the deep-sleep-mode flash and SRAM power configuration.
1657     //
1658     HWREG(SYSCTL_DSLPPWRCFG) = ui32Config;
1659 }
1660 
1661 //*****************************************************************************
1662 //
1663 //! Resets the device.
1664 //!
1665 //! This function performs a software reset of the entire device.  The
1666 //! processor and all peripherals are reset and all device registers are
1667 //! returned to their default values (with the exception of the reset cause
1668 //! register, which maintains its current value but has the software reset
1669 //! bit set as well).
1670 //!
1671 //! \return This function does not return.
1672 //
1673 //*****************************************************************************
1674 void
SysCtlReset(void)1675 SysCtlReset(void)
1676 {
1677     //
1678     // Perform a software reset request.  This request causes the device to
1679     // reset, no further code is executed.
1680     //
1681     HWREG(NVIC_APINT) = NVIC_APINT_VECTKEY | NVIC_APINT_SYSRESETREQ;
1682 
1683     //
1684     // The device should have reset, so this should never be reached.  Just in
1685     // case, loop forever.
1686     //
1687     while(1)
1688     {
1689     }
1690 }
1691 
1692 //*****************************************************************************
1693 //
1694 //! Puts the processor into sleep mode.
1695 //!
1696 //! This function places the processor into sleep mode; it does not return
1697 //! until the processor returns to run mode.  The peripherals that are enabled
1698 //! via SysCtlPeripheralSleepEnable() continue to operate and can wake up the
1699 //! processor (if automatic clock gating is enabled with
1700 //! SysCtlPeripheralClockGating(), otherwise all peripherals continue to
1701 //! operate).
1702 //!
1703 //! \return None.
1704 //
1705 //*****************************************************************************
1706 void
SysCtlSleep(void)1707 SysCtlSleep(void)
1708 {
1709     //
1710     // Wait for an interrupt.
1711     //
1712     CPUwfi();
1713 }
1714 
1715 //*****************************************************************************
1716 //
1717 //! Puts the processor into deep-sleep mode.
1718 //!
1719 //! This function places the processor into deep-sleep mode; it does not return
1720 //! until the processor returns to run mode.  The peripherals that are enabled
1721 //! via SysCtlPeripheralDeepSleepEnable() continue to operate and can wake up
1722 //! the processor (if automatic clock gating is enabled with
1723 //! SysCtlPeripheralClockGating(), otherwise all peripherals continue to
1724 //! operate).
1725 //!
1726 //! \return None.
1727 //
1728 //*****************************************************************************
1729 void
SysCtlDeepSleep(void)1730 SysCtlDeepSleep(void)
1731 {
1732     //
1733     // Enable deep-sleep.
1734     //
1735     HWREG(NVIC_SYS_CTRL) |= NVIC_SYS_CTRL_SLEEPDEEP;
1736 
1737     //
1738     // Wait for an interrupt.
1739     //
1740     CPUwfi();
1741 
1742     //
1743     // Disable deep-sleep so that a future sleep works correctly.
1744     //
1745     HWREG(NVIC_SYS_CTRL) &= ~(NVIC_SYS_CTRL_SLEEPDEEP);
1746 }
1747 
1748 //*****************************************************************************
1749 //
1750 //! Gets the reason for a reset.
1751 //!
1752 //! This function returns the reason(s) for a reset.  Because the reset
1753 //! reasons are sticky until either cleared by software or a power-on reset,
1754 //! multiple reset reasons may be returned if multiple resets have occurred.
1755 //! The reset reason is a logical OR of \b SYSCTL_CAUSE_HSRVREQ,
1756 //! \b SYSCTL_CAUSE_HIB, \b SYSCTL_CAUSE_WDOG1, \b SYSCTL_CAUSE_SW,
1757 //! \b SYSCTL_CAUSE_WDOG0, \b SYSCTL_CAUSE_BOR, \b SYSCTL_CAUSE_POR,
1758 //! and/or \b SYSCTL_CAUSE_EXT.
1759 //!
1760 //! \return Returns the reason(s) for a reset.
1761 //
1762 //*****************************************************************************
1763 uint32_t
SysCtlResetCauseGet(void)1764 SysCtlResetCauseGet(void)
1765 {
1766     //
1767     // Return the reset reasons.
1768     //
1769     return(HWREG(SYSCTL_RESC));
1770 }
1771 
1772 //*****************************************************************************
1773 //
1774 //! Clears reset reasons.
1775 //!
1776 //! \param ui32Causes are the reset causes to be cleared; must be a logical OR
1777 //! of \b SYSCTL_CAUSE_HSRVREQ, \b SYSCTL_CAUSE_HIB, \b SYSCTL_CAUSE_WDOG1,
1778 //! \b SYSCTL_CAUSE_SW, \b SYSCTL_CAUSE_WDOG0, \b SYSCTL_CAUSE_BOR,
1779 //! \b SYSCTL_CAUSE_POR, and/or \b SYSCTL_CAUSE_EXT.
1780 //!
1781 //! This function clears the specified sticky reset reasons.  Once cleared,
1782 //! another reset for the same reason can be detected, and a reset for a
1783 //! different reason can be distinguished (instead of having two reset causes
1784 //! set).  If the reset reason is used by an application, all reset causes
1785 //! should be cleared after they are retrieved with SysCtlResetCauseGet().
1786 //!
1787 //! \return None.
1788 //
1789 //*****************************************************************************
1790 void
SysCtlResetCauseClear(uint32_t ui32Causes)1791 SysCtlResetCauseClear(uint32_t ui32Causes)
1792 {
1793     //
1794     // Clear the given reset reasons.
1795     //
1796     HWREG(SYSCTL_RESC) &= ~(ui32Causes);
1797 }
1798 
1799 //*****************************************************************************
1800 //
1801 //! Provides a small delay.
1802 //!
1803 //! \param ui32Count is the number of delay loop iterations to perform.
1804 //!
1805 //! This function provides a means of generating a delay by executing a simple
1806 //! 3 instruction cycle loop a given number of times.  It is written in
1807 //! assembly to keep the loop instruction count consistent across tool chains.
1808 //!
1809 //! It is important to note that this function does NOT provide an accurate
1810 //! timing mechanism.  Although the delay loop is 3 instruction cycles long,
1811 //! the execution time of the loop will vary dramatically depending upon the
1812 //! application's interrupt environment (the loop will be interrupted unless
1813 //! run with interrupts disabled and this is generally an unwise thing to do)
1814 //! and also the current system clock rate and flash timings (wait states and
1815 //! the operation of the prefetch buffer affect the timing).
1816 //!
1817 //! For better accuracy, the ROM version of this function may be used.  This
1818 //! version will not suffer from flash- and prefect buffer-related timing
1819 //! variability but will still be delayed by interrupt service routines.
1820 //!
1821 //! For best accuracy, a system timer should be used with code either polling
1822 //! for a particular timer value being exceeded or processing the timer
1823 //! interrupt to determine when a particular time period has elapsed.
1824 //!
1825 //! \return None.
1826 //
1827 //*****************************************************************************
1828 #if defined(ewarm) || defined(DOXYGEN)
1829 void
SysCtlDelay(uint32_t ui32Count)1830 SysCtlDelay(uint32_t ui32Count)
1831 {
1832     __asm("    subs    r0, #1\n"
1833           "    bne.n   SysCtlDelay\n"
1834           "    bx      lr");
1835 }
1836 #endif
1837 #if defined(codered) || defined(gcc) || defined(sourcerygxx)
1838 void __attribute__((naked))
SysCtlDelay(uint32_t ui32Count)1839 SysCtlDelay(uint32_t ui32Count)
1840 {
1841     __asm("    subs    r0, #1\n"
1842           "    bne     SysCtlDelay\n"
1843           "    bx      lr");
1844 }
1845 #endif
1846 #if defined(rvmdk) || defined(__ARMCC_VERSION)
1847 __asm void
SysCtlDelay(uint32_t ui32Count)1848 SysCtlDelay(uint32_t ui32Count)
1849 {
1850     subs    r0, #1;
1851     bne     SysCtlDelay;
1852     bx      lr;
1853 }
1854 #endif
1855 //
1856 // For CCS implement this function in pure assembly.  This prevents the TI
1857 // compiler from doing funny things with the optimizer.
1858 //
1859 #if defined(ccs)
1860 __asm("    .sect \".text:SysCtlDelay\"\n"
1861       "    .clink\n"
1862       "    .thumbfunc SysCtlDelay\n"
1863       "    .thumb\n"
1864       "    .global SysCtlDelay\n"
1865       "SysCtlDelay:\n"
1866       "    subs r0, #1\n"
1867       "    bne.n SysCtlDelay\n"
1868       "    bx lr\n");
1869 #endif
1870 
1871 //*****************************************************************************
1872 //
1873 //! Sets the configuration of the main oscillator (MOSC) control.
1874 //!
1875 //! \param ui32Config is the required configuration of the MOSC control.
1876 //!
1877 //! This function configures the control of the main oscillator.  The
1878 //! \e ui32Config is specified as the logical OR of the following values:
1879 //!
1880 //! - \b SYSCTL_MOSC_VALIDATE enables the MOSC verification circuit that
1881 //!   detects a failure of the main oscillator (such as a loss of the clock).
1882 //! - \b SYSCTL_MOSC_INTERRUPT indicates that a MOSC failure should generate an
1883 //!   interrupt instead of resetting the processor.
1884 //! - \b SYSCTL_MOSC_NO_XTAL indicates that there is no crystal or oscillator
1885 //!   connected to the OSC0/OSC1 pins, allowing power consumption to be
1886 //!   reduced.
1887 //! - \b SYSCTL_MOSC_PWR_DIS disable power to the main oscillator.  If this
1888 //!   parameter is not specified, the MOSC input remains powered.
1889 //! - \b SYSCTL_MOSC_LOWFREQ MOSC is less than 10 MHz.
1890 //! - \b SYSCTL_MOSC_HIGHFREQ MOSC is greater than 10 MHz.
1891 //! - \b SYSCTL_MOSC_SESRC specifies that the MOSC is a single-ended
1892 //!   oscillator connected to OSC0.  If this parameter is not specified, the
1893 //!   input is assumed to be a crystal.
1894 //!
1895 //! \note The availability of MOSC control varies based on the Tiva part
1896 //! in use.  Please consult the data sheet for the part you are using to
1897 //! determine whether this support is available.  In addition, the capability
1898 //! of MOSC control varies based on the Tiva part in use.
1899 //!
1900 //! \return None.
1901 //
1902 //*****************************************************************************
1903 void
SysCtlMOSCConfigSet(uint32_t ui32Config)1904 SysCtlMOSCConfigSet(uint32_t ui32Config)
1905 {
1906     //
1907     // Configure the MOSC control.
1908     //
1909     HWREG(SYSCTL_MOSCCTL) = ui32Config;
1910 }
1911 
1912 //*****************************************************************************
1913 //
1914 //! Calibrates the precision internal oscillator.
1915 //!
1916 //! \param ui32Type is the type of calibration to perform.
1917 //!
1918 //! This function performs a calibration of the PIOSC.  There are three types
1919 //! of calibration available; the desired calibration type as specified in
1920 //! \e ui32Type is one of:
1921 //!
1922 //! - \b SYSCTL_PIOSC_CAL_AUTO to perform automatic calibration using the
1923 //!   32-kHz clock from the hibernate module as a reference.  This type is
1924 //!   only possible on parts that have a hibernate module, and then only if
1925 //!   it is enabled, a 32.768-kHz clock source is attached to the XOSC0/1
1926 //!   pins and the hibernate module's RTC is also enabled.
1927 //!
1928 //! - \b SYSCTL_PIOSC_CAL_FACT to reset the PIOSC calibration to the factory
1929 //!   provided calibration.
1930 //!
1931 //! - \b SYSCTL_PIOSC_CAL_USER to set the PIOSC calibration to a user-supplied
1932 //!   value.  The value to be used is ORed into the lower 7-bits of this value,
1933 //!   with 0x40 being the ``nominal'' value (in other words, if everything were
1934 //!   perfect, 0x40 provides exactly 16 MHz).  Values larger than 0x40
1935 //!   slow down PIOSC, and values smaller than 0x40 speed up PIOSC.
1936 //!
1937 //! \return Returns 1 if the calibration was successful and 0 if it failed.
1938 //
1939 //*****************************************************************************
1940 uint32_t
SysCtlPIOSCCalibrate(uint32_t ui32Type)1941 SysCtlPIOSCCalibrate(uint32_t ui32Type)
1942 {
1943     //
1944     // Perform the requested calibration.  If performing user calibration, the
1945     // UTEN bit must be set with one write, then the UT field in a second
1946     // write, and the UPDATE bit in a final write.  For other calibration
1947     // types, a single write to set UPDATE or CAL is all that is required.
1948     //
1949     if(ui32Type & (SYSCTL_PIOSCCAL_UTEN | SYSCTL_PIOSCCAL_UPDATE))
1950     {
1951         HWREG(SYSCTL_PIOSCCAL) = ui32Type & SYSCTL_PIOSCCAL_UTEN;
1952         HWREG(SYSCTL_PIOSCCAL) =
1953             ui32Type & (SYSCTL_PIOSCCAL_UTEN | SYSCTL_PIOSCCAL_UT_M);
1954     }
1955     HWREG(SYSCTL_PIOSCCAL) = ui32Type;
1956 
1957     //
1958     // See if an automatic calibration was requested.
1959     //
1960     if(ui32Type & SYSCTL_PIOSCCAL_CAL)
1961     {
1962         //
1963         // Wait for the automatic calibration to complete.
1964         //
1965         while((HWREG(SYSCTL_PIOSCSTAT) & SYSCTL_PIOSCSTAT_CR_M) == 0)
1966         {
1967         }
1968 
1969         //
1970         // If the automatic calibration failed, return an error.
1971         //
1972         if((HWREG(SYSCTL_PIOSCSTAT) & SYSCTL_PIOSCSTAT_CR_M) !=
1973            SYSCTL_PIOSCSTAT_CRPASS)
1974         {
1975             return(0);
1976         }
1977     }
1978 
1979     //
1980     // The calibration was successful.
1981     //
1982     return(1);
1983 }
1984 
1985 //*****************************************************************************
1986 //
1987 //! Sets the type of reset issued due to certain reset events.
1988 //!
1989 //! \param ui32Behavior specifies the types of resets for each of the
1990 //! configurable reset events.
1991 //!
1992 //! This function sets the types of reset issued when a configurable reset
1993 //! event occurs.  The reset events that are configurable are: Watchdog 0 or 1,
1994 //! a brown out and the external RSTn pin.  The valid actions are either a
1995 //! system reset or a full POR sequence.  See the data sheet for more
1996 //! information on the differences between a full POR and a system reset.  All
1997 //! reset behaviors can be configured with a single call using the logical OR
1998 //! of the values defined below.  Any reset option that is not specifically set
1999 //! remains configured for its default behavior. Either POR or system reset
2000 //! can be selected for each reset cause.
2001 //!
2002 //! Valid values are logical combinations of the following:
2003 //!
2004 //! - \b SYSCTL_ONRST_WDOG0_POR configures a Watchdog 0 reset to perform a full
2005 //!   POR.
2006 //! - \b SYSCTL_ONRST_WDOG0_SYS configures a Watchdog 0 reset to perform a
2007 //!   system reset.
2008 //! - \b SYSCTL_ONRST_WDOG1_POR configures a Watchdog 1 reset to perform a full
2009 //!   POR.
2010 //! - \b SYSCTL_ONRST_WDOG1_SYS configures a Watchdog 1 reset to perform a
2011 //!   system reset.
2012 //! - \b SYSCTL_ONRST_BOR_POR configures a brown-out reset to perform a full
2013 //!   POR.
2014 //! - \b SYSCTL_ONRST_BOR_SYS configures a brown-out reset to perform a system
2015 //!   reset.
2016 //! - \b SYSCTL_ONRST_EXT_POR configures an external pin reset to perform a
2017 //!   full POR.
2018 //! - \b SYSCTL_ONRST_EXT_SYS configures an external pin reset to perform a
2019 //!   system reset.
2020 //!
2021 //! \b Example: Set Watchdog 0 reset to trigger a POR and a brown-out reset
2022 //! to trigger a system reset while leaving the remaining resets with their
2023 //! default behaviors.
2024 //!
2025 //! \verbatim
2026 //! SysCtlResetBehaviorSet(SYSCTL_ONRST_WDOG0_POR | SYSCTL_ONRST_BOR_SYS);
2027 //! \endverbatim
2028 //!
2029 //! \note This function cannot be used with TM4C123 devices.
2030 //!
2031 //! \return None.
2032 //
2033 //*****************************************************************************
2034 void
SysCtlResetBehaviorSet(uint32_t ui32Behavior)2035 SysCtlResetBehaviorSet(uint32_t ui32Behavior)
2036 {
2037     HWREG(SYSCTL_RESBEHAVCTL) = ui32Behavior;
2038 }
2039 
2040 //*****************************************************************************
2041 //
2042 //! Returns the current types of reset issued due to reset events.
2043 //!
2044 //! This function returns the types of resets issued when a configurable reset
2045 //! occurs.  The value returned is a logical OR combination of the valid values
2046 //! that are described in the documentation for the \e ui32Behavior parameter
2047 //! of the SysCtlResetBehaviorSet() function.
2048 //!
2049 //! \note This function should only be used with Flurry-class devices.
2050 //!
2051 //! \return The reset behaviors for all configurable resets.
2052 //
2053 //*****************************************************************************
2054 uint32_t
SysCtlResetBehaviorGet(void)2055 SysCtlResetBehaviorGet(void)
2056 {
2057     return(HWREG(SYSCTL_RESBEHAVCTL));
2058 }
2059 
2060 //*****************************************************************************
2061 //
2062 //! Configures the system clock.
2063 //!
2064 //! \param ui32Config is the required configuration of the device clocking.
2065 //! \param ui32SysClock is the requested processor frequency.
2066 //!
2067 //! This function configures the main system clocking for the device.  The
2068 //! input frequency, oscillator source, whether or not to enable the PLL, and
2069 //! the system clock divider are all configured with this function.  This
2070 //! function configures the system frequency to the closest available divisor
2071 //! of one of the fixed PLL VCO settings provided in the \e ui32Config
2072 //! parameter.  The caller sets the \e ui32SysClock parameter to request the
2073 //! system clock frequency, and this function then attempts to match this using
2074 //! the values provided in the \e ui32Config parameter.  If this function
2075 //! cannot exactly match the requested frequency, it picks the closest
2076 //! frequency that is lower than the requested frequency.  The \e ui32Config
2077 //! parameter provides the remaining configuration options using a set of
2078 //! defines that are a logical OR of several different values, many of which
2079 //! are grouped into sets where only one of the set can be chosen.  This
2080 //! function returns the current system frequency which may not match the
2081 //! requested frequency.
2082 //!
2083 //! If the application is using an external crystal then the frequency is
2084 //! set by using one of the following values:
2085 //! \b SYSCTL_XTAL_5MHZ, \b SYSCTL_XTAL_6MHZ, \b SYSCTL_XTAL_8MHZ,
2086 //! \b SYSCTL_XTAL_10MHZ, \b SYSCTL_XTAL_12MHZ, \b SYSCTL_XTAL_16MHZ,
2087 //! \b SYSCTL_XTAL_18MHZ, \b SYSCTL_XTAL_20MHZ, \b SYSCTL_XTAL_24MHZ, or
2088 //! \b SYSCTL_XTAL_25MHz.
2089 //!
2090 //! The oscillator source is chosen with one of the following values:
2091 //!
2092 //! - \b SYSCTL_OSC_MAIN to use an external crystal or oscillator.
2093 //! - \b SYSCTL_OSC_INT to use the 16-MHz precision internal oscillator.
2094 //! - \b SYSCTL_OSC_INT30 to use the internal low frequency oscillator.
2095 //! - \b SYSCTL_OSC_EXT32 to use the hibernate modules 32.786-kHz oscillator.
2096 //! This option is only available on devices that include the hibernation
2097 //! module.
2098 //!
2099 //! The system clock source is chosen with one of the following values:
2100 //!
2101 //! - \b SYSCTL_USE_PLL is used to select the PLL output as the system clock.
2102 //! - \b SYSCTL_USE_OSC is used to choose one of the oscillators as the
2103 //! system clock.
2104 //!
2105 //! The PLL VCO frequency is chosen with one of the the following values:
2106 //!
2107 //! - \b SYSCTL_CFG_VCO_480 to set the PLL VCO output to 480-MHz
2108 //! - \b SYSCTL_CFG_VCO_320 to set the PLL VCO output to 320-MHz
2109 //!
2110 //! Example: Configure the system clocking to be 40 MHz with a 320-MHz PLL
2111 //! setting using the 16-MHz internal oscillator.
2112 //!
2113 //! \verbatim
2114 //! SysCtlClockFreqSet(SYSCTL_OSC_INT | SYSCTL_USE_PLL | SYSCTL_CFG_VCO_320,
2115 //!                    40000000);
2116 //! \endverbatim
2117 //!
2118 //! \note This function cannot be used with TM4C123 devices.  For TM4C123
2119 //! devices use the SysCtlClockSet() function.
2120 //!
2121 //! \return The actual configured system clock frequency in Hz or zero if the
2122 //! value could not be changed due to a parameter error or PLL lock failure.
2123 //
2124 //*****************************************************************************
2125 uint32_t
SysCtlClockFreqSet(uint32_t ui32Config,uint32_t ui32SysClock)2126 SysCtlClockFreqSet(uint32_t ui32Config, uint32_t ui32SysClock)
2127 {
2128     int32_t i32Timeout, i32VCOIdx, i32XtalIdx;
2129     uint32_t ui32MOSCCTL;
2130     uint32_t ui32Delay;
2131     uint32_t ui32SysDiv, ui32Osc, ui32OscSelect, ui32RSClkConfig;
2132 
2133     //
2134     // TM4C123 devices should not use this function.
2135     //
2136     if(CLASS_IS_TM4C123)
2137     {
2138         return(0);
2139     }
2140 
2141     //
2142     // Get the index of the crystal from the ui32Config parameter.
2143     //
2144     i32XtalIdx = SysCtlXtalCfgToIndex(ui32Config);
2145 
2146     //
2147     // Determine which non-PLL source was selected.
2148     //
2149     if((ui32Config & 0x38) == SYSCTL_OSC_INT)
2150     {
2151         //
2152         // Use the nominal frequency for the PIOSC oscillator and set the
2153         // crystal select.
2154         //
2155         ui32Osc = 16000000;
2156         ui32OscSelect = SYSCTL_RSCLKCFG_OSCSRC_PIOSC;
2157         ui32OscSelect |= SYSCTL_RSCLKCFG_PLLSRC_PIOSC;
2158 
2159         //
2160         // Force the crystal index to the value for 16-MHz.
2161         //
2162         i32XtalIdx = SysCtlXtalCfgToIndex(SYSCTL_XTAL_16MHZ);
2163     }
2164     else if((ui32Config & 0x38) == SYSCTL_OSC_INT30)
2165     {
2166         //
2167         // Use the nominal frequency for the low frequency oscillator.
2168         //
2169         ui32Osc = 30000;
2170         ui32OscSelect = SYSCTL_RSCLKCFG_OSCSRC_LFIOSC;
2171     }
2172     else if((ui32Config & 0x38) == (SYSCTL_OSC_EXT32 & 0x38))
2173     {
2174         //
2175         // Use the RTC frequency.
2176         //
2177         ui32Osc = 32768;
2178         ui32OscSelect = SYSCTL_RSCLKCFG_OSCSRC_RTC;
2179     }
2180     else if((ui32Config & 0x38) == SYSCTL_OSC_MAIN)
2181     {
2182         //
2183         // Bounds check the source frequency for the main oscillator.  The is
2184         // because the PLL tables in the g_pppui32XTALtoVCO structure range
2185         // from 5MHz to 25MHz.
2186         //
2187         if((i32XtalIdx > (SysCtlXtalCfgToIndex(SYSCTL_XTAL_25MHZ))) ||
2188            (i32XtalIdx < (SysCtlXtalCfgToIndex(SYSCTL_XTAL_5MHZ))))
2189         {
2190             return(0);
2191         }
2192 
2193         ui32Osc = g_pui32Xtals[i32XtalIdx];
2194 
2195         //
2196         // Set the PLL source select to MOSC.
2197         //
2198         ui32OscSelect = SYSCTL_RSCLKCFG_OSCSRC_MOSC;
2199         ui32OscSelect |= SYSCTL_RSCLKCFG_PLLSRC_MOSC;
2200 
2201         //
2202         // Clear MOSC power down, high oscillator range setting, and no crystal
2203         // present setting.
2204         //
2205         ui32MOSCCTL = HWREG(SYSCTL_MOSCCTL) &
2206                       ~(SYSCTL_MOSCCTL_OSCRNG | SYSCTL_MOSCCTL_PWRDN |
2207                         SYSCTL_MOSCCTL_NOXTAL);
2208 
2209         //
2210         // Increase the drive strength for MOSC of 10 MHz and above.
2211         //
2212         if(i32XtalIdx >= (SysCtlXtalCfgToIndex(SYSCTL_XTAL_10MHZ) -
2213                           (SysCtlXtalCfgToIndex(SYSCTL_XTAL_5MHZ))))
2214         {
2215             ui32MOSCCTL |= SYSCTL_MOSCCTL_OSCRNG;
2216         }
2217 
2218         HWREG(SYSCTL_MOSCCTL) = ui32MOSCCTL;
2219 
2220         //
2221         // Timeout using the legacy delay value.
2222         //
2223         ui32Delay = 524288;
2224 
2225         while((HWREG(SYSCTL_RIS) & SYSCTL_RIS_MOSCPUPRIS) == 0)
2226         {
2227             ui32Delay--;
2228 
2229             if(ui32Delay == 0)
2230             {
2231                 break;
2232             }
2233         }
2234 
2235         //
2236         // If the main oscillator failed to start up then do not switch to
2237         // it and return.
2238         //
2239         if(ui32Delay == 0)
2240         {
2241             return(0);
2242         }
2243 
2244 
2245     }
2246     else
2247     {
2248         //
2249         // This was an invalid request because no oscillator source was
2250         // indicated.
2251         //
2252         ui32Osc = 0;
2253         ui32OscSelect = SYSCTL_RSCLKCFG_OSCSRC_PIOSC;
2254     }
2255 
2256     //
2257     // Check if the running with the PLL enabled was requested.
2258     //
2259     if((ui32Config & SYSCTL_USE_OSC) == SYSCTL_USE_PLL)
2260     {
2261         //
2262         // ui32Config must be SYSCTL_OSC_MAIN or SYSCTL_OSC_INT.
2263         //
2264         if(((ui32Config & 0x38) != SYSCTL_OSC_MAIN) &&
2265            ((ui32Config & 0x38) != SYSCTL_OSC_INT))
2266         {
2267             return(0);
2268         }
2269 
2270         //
2271         // Get the VCO index out of the ui32Config parameter.
2272         //
2273         i32VCOIdx = (ui32Config >> 24) & 7;
2274 
2275         //
2276         // Check that the VCO index is not out of bounds.
2277         //
2278         ASSERT(i32VCOIdx < MAX_VCO_ENTRIES);
2279 
2280         //
2281         // Set the memory timings for the maximum external frequency since
2282         // this could be a switch to PIOSC or possibly to MOSC which can be
2283         // up to 25MHz.
2284         //
2285         HWREG(SYSCTL_MEMTIM0) = _SysCtlMemTimingGet(25000000);
2286 
2287         //
2288         // Clear the old PLL divider and source in case it was set.
2289         //
2290         ui32RSClkConfig = HWREG(SYSCTL_RSCLKCFG) &
2291                           ~(SYSCTL_RSCLKCFG_PSYSDIV_M |
2292                             SYSCTL_RSCLKCFG_OSCSRC_M |
2293                             SYSCTL_RSCLKCFG_PLLSRC_M | SYSCTL_RSCLKCFG_USEPLL);
2294 
2295         //
2296         // Update the memory timings to match running from PIOSC.
2297         //
2298         ui32RSClkConfig |= SYSCTL_RSCLKCFG_MEMTIMU;
2299 
2300         //
2301         // Update clock configuration to switch back to PIOSC.
2302         //
2303         HWREG(SYSCTL_RSCLKCFG) = ui32RSClkConfig;
2304 
2305         //
2306         // The table starts at 5 MHz so modify the index to match this.
2307         //
2308         i32XtalIdx -= SysCtlXtalCfgToIndex(SYSCTL_XTAL_5MHZ);
2309 
2310         //
2311         // Calculate the System divider such that we get a frequency that is
2312         // the closest to the requested frequency without going over.
2313         //
2314         ui32SysDiv = (g_pui32VCOFrequencies[i32VCOIdx] + ui32SysClock - 1) /
2315                      ui32SysClock;
2316 
2317         //
2318         // Set the oscillator source.
2319         //
2320         HWREG(SYSCTL_RSCLKCFG) |= ui32OscSelect;
2321 
2322         //
2323         // Set the M, N and Q values provided from the table and preserve
2324         // the power state of the main PLL.
2325         //
2326         HWREG(SYSCTL_PLLFREQ1) =
2327             g_pppui32XTALtoVCO[i32VCOIdx][i32XtalIdx][1];
2328         HWREG(SYSCTL_PLLFREQ1) |= PLL_Q_TO_REG(ui32SysDiv);
2329         HWREG(SYSCTL_PLLFREQ0) =
2330             (g_pppui32XTALtoVCO[i32VCOIdx][i32XtalIdx][0] |
2331              (HWREG(SYSCTL_PLLFREQ0) & SYSCTL_PLLFREQ0_PLLPWR));
2332 
2333         //
2334         // Calculate the actual system clock as PSYSDIV is always div-by 2.
2335         //
2336         ui32SysClock = _SysCtlFrequencyGet(ui32Osc) / 2;
2337 
2338         //
2339         // Set the Flash and EEPROM timing values.
2340         //
2341         HWREG(SYSCTL_MEMTIM0) = _SysCtlMemTimingGet(ui32SysClock);
2342 
2343         //
2344         // Check if the PLL is already powered up.
2345         //
2346         if(HWREG(SYSCTL_PLLFREQ0) & SYSCTL_PLLFREQ0_PLLPWR)
2347         {
2348             //
2349             // Trigger the PLL to lock to the new frequency.
2350             //
2351             HWREG(SYSCTL_RSCLKCFG) |= SYSCTL_RSCLKCFG_NEWFREQ;
2352         }
2353         else
2354         {
2355             //
2356             // Power up the PLL.
2357             //
2358             HWREG(SYSCTL_PLLFREQ0) |= SYSCTL_PLLFREQ0_PLLPWR;
2359         }
2360 
2361         //
2362         // Wait until the PLL has locked.
2363         //
2364         for(i32Timeout = 32768; i32Timeout > 0; i32Timeout--)
2365         {
2366             if((HWREG(SYSCTL_PLLSTAT) & SYSCTL_PLLSTAT_LOCK))
2367             {
2368                 break;
2369             }
2370         }
2371 
2372         //
2373         // If the loop above did not timeout then switch over to the PLL
2374         //
2375         if(i32Timeout)
2376         {
2377             ui32RSClkConfig = HWREG(SYSCTL_RSCLKCFG);
2378             ui32RSClkConfig |= (1 << SYSCTL_RSCLKCFG_PSYSDIV_S) |
2379                                 ui32OscSelect | SYSCTL_RSCLKCFG_USEPLL;
2380             ui32RSClkConfig |= SYSCTL_RSCLKCFG_MEMTIMU;
2381 
2382             //
2383             // Set the new clock configuration.
2384             //
2385             HWREG(SYSCTL_RSCLKCFG) = ui32RSClkConfig;
2386         }
2387         else
2388         {
2389             ui32SysClock = 0;
2390         }
2391     }
2392     else
2393     {
2394         //
2395         // Set the Flash and EEPROM timing values for PIOSC.
2396         //
2397         HWREG(SYSCTL_MEMTIM0) = _SysCtlMemTimingGet(16000000);
2398 
2399         //
2400         // Make sure that the PLL is powered down since it is not being used.
2401         //
2402         HWREG(SYSCTL_PLLFREQ0) &= ~SYSCTL_PLLFREQ0_PLLPWR;
2403 
2404         //
2405         // Clear the old PLL divider and source in case it was set.
2406         //
2407         ui32RSClkConfig = HWREG(SYSCTL_RSCLKCFG);
2408         ui32RSClkConfig &= ~(SYSCTL_RSCLKCFG_OSYSDIV_M |
2409                              SYSCTL_RSCLKCFG_OSCSRC_M |
2410                              SYSCTL_RSCLKCFG_USEPLL);
2411 
2412         //
2413         // Update the memory timings.
2414         //
2415         ui32RSClkConfig |= SYSCTL_RSCLKCFG_MEMTIMU;
2416 
2417         //
2418         // Set the new clock configuration.
2419         //
2420         HWREG(SYSCTL_RSCLKCFG) = ui32RSClkConfig;
2421 
2422         //
2423         // If zero given as the system clock then default to divide by 1.
2424         //
2425         if(ui32SysClock == 0)
2426         {
2427             ui32SysDiv = 0;
2428         }
2429         else
2430         {
2431             //
2432             // Calculate the System divider based on the requested
2433             // frequency.
2434             //
2435             ui32SysDiv = ui32Osc / ui32SysClock;
2436 
2437             //
2438             // If the system divisor is not already zero, subtract one to
2439             // set the value in the register which requires the value to
2440             // be n-1.
2441             //
2442             if(ui32SysDiv != 0)
2443             {
2444                 ui32SysDiv -= 1;
2445             }
2446 
2447             //
2448             // Calculate the system clock.
2449             //
2450             ui32SysClock = ui32Osc / (ui32SysDiv + 1);
2451         }
2452 
2453         //
2454         // Set the memory timing values for the new system clock.
2455         //
2456         HWREG(SYSCTL_MEMTIM0) = _SysCtlMemTimingGet(ui32SysClock);
2457 
2458         //
2459         // Set the new system clock values.
2460         //
2461         ui32RSClkConfig = HWREG(SYSCTL_RSCLKCFG);
2462         ui32RSClkConfig |= (ui32SysDiv << SYSCTL_RSCLKCFG_OSYSDIV_S) |
2463                            ui32OscSelect;
2464 
2465         //
2466         // Update the memory timings.
2467         //
2468         ui32RSClkConfig |= SYSCTL_RSCLKCFG_MEMTIMU;
2469 
2470         //
2471         // Set the new clock configuration.
2472         //
2473         HWREG(SYSCTL_RSCLKCFG) = ui32RSClkConfig;
2474     }
2475 
2476     //
2477     // Finally change the OSCSRC back to PIOSC
2478     //
2479     HWREG(SYSCTL_RSCLKCFG) &= ~(SYSCTL_RSCLKCFG_OSCSRC_M);
2480 
2481     return(ui32SysClock);
2482 }
2483 
2484 //*****************************************************************************
2485 //
2486 //! Sets the clocking of the device.
2487 //!
2488 //! \param ui32Config is the required configuration of the device clocking.
2489 //!
2490 //! This function configures the clocking of the device.  The input crystal
2491 //! frequency, oscillator to be used, use of the PLL, and the system clock
2492 //! divider are all configured with this function.
2493 //!
2494 //! The \e ui32Config parameter is the logical OR of several different values,
2495 //! many of which are grouped into sets where only one can be chosen.
2496 //!
2497 //! The system clock divider is chosen with one of the following values:
2498 //! \b SYSCTL_SYSDIV_1, \b SYSCTL_SYSDIV_2, \b SYSCTL_SYSDIV_3, ...
2499 //! \b SYSCTL_SYSDIV_64.
2500 //!
2501 //! The use of the PLL is chosen with either \b SYSCTL_USE_PLL or
2502 //! \b SYSCTL_USE_OSC.
2503 //!
2504 //! The external crystal frequency is chosen with one of the following values:
2505 //! \b SYSCTL_XTAL_4MHZ, \b SYSCTL_XTAL_4_09MHZ, \b SYSCTL_XTAL_4_91MHZ,
2506 //! \b SYSCTL_XTAL_5MHZ, \b SYSCTL_XTAL_5_12MHZ, \b SYSCTL_XTAL_6MHZ,
2507 //! \b SYSCTL_XTAL_6_14MHZ, \b SYSCTL_XTAL_7_37MHZ, \b SYSCTL_XTAL_8MHZ,
2508 //! \b SYSCTL_XTAL_8_19MHZ, \b SYSCTL_XTAL_10MHZ, \b SYSCTL_XTAL_12MHZ,
2509 //! \b SYSCTL_XTAL_12_2MHZ, \b SYSCTL_XTAL_13_5MHZ, \b SYSCTL_XTAL_14_3MHZ,
2510 //! \b SYSCTL_XTAL_16MHZ, \b SYSCTL_XTAL_16_3MHZ, \b SYSCTL_XTAL_18MHZ,
2511 //! \b SYSCTL_XTAL_20MHZ, \b SYSCTL_XTAL_24MHZ, or \b SYSCTL_XTAL_25MHz.
2512 //! Values below \b SYSCTL_XTAL_5MHZ are not valid when the PLL is in
2513 //! operation.
2514 //!
2515 //! The oscillator source is chosen with one of the following values:
2516 //! \b SYSCTL_OSC_MAIN, \b SYSCTL_OSC_INT, \b SYSCTL_OSC_INT4,
2517 //! \b SYSCTL_OSC_INT30, or \b SYSCTL_OSC_EXT32.  \b SYSCTL_OSC_EXT32 is only
2518 //! available on devices with the hibernate module, and then only when the
2519 //! hibernate module has been enabled.
2520 //!
2521 //! The internal and main oscillators are disabled with the
2522 //! \b SYSCTL_INT_OSC_DIS and \b SYSCTL_MAIN_OSC_DIS flags, respectively.
2523 //! The external oscillator must be enabled in order to use an external clock
2524 //! source.  Note that attempts to disable the oscillator used to clock the
2525 //! device is prevented by the hardware.
2526 //!
2527 //! To clock the system from an external source (such as an external crystal
2528 //! oscillator), use \b SYSCTL_USE_OSC \b | \b SYSCTL_OSC_MAIN.  To clock the
2529 //! system from the main oscillator, use \b SYSCTL_USE_OSC \b |
2530 //! \b SYSCTL_OSC_MAIN.  To clock the system from the PLL, use
2531 //! \b SYSCTL_USE_PLL \b | \b SYSCTL_OSC_MAIN, and select the appropriate
2532 //! crystal with one of the \b SYSCTL_XTAL_xxx values.
2533 //!
2534 //! \note This function should only be called on TM4C123 devices.  For
2535 //! all other devices use the SysCtlClockFreqSet() function.
2536 //!
2537 //! \note If selecting the PLL as the system clock source (that is, via
2538 //! \b SYSCTL_USE_PLL), this function polls the PLL lock interrupt to
2539 //! determine when the PLL has locked.  If an interrupt handler for the
2540 //! system control interrupt is in place, and it responds to and clears the
2541 //! PLL lock interrupt, this function delays until its timeout has occurred
2542 //! instead of completing as soon as PLL lock is achieved.
2543 //!
2544 //! \return None.
2545 //
2546 //*****************************************************************************
2547 void
SysCtlClockSet(uint32_t ui32Config)2548 SysCtlClockSet(uint32_t ui32Config)
2549 {
2550     uint32_t ui32Delay, ui32RCC, ui32RCC2;
2551 
2552     //
2553     // Get the current value of the RCC and RCC2 registers.
2554     //
2555     ui32RCC = HWREG(SYSCTL_RCC);
2556     ui32RCC2 = HWREG(SYSCTL_RCC2);
2557 
2558     //
2559     // Bypass the PLL and system clock dividers for now.
2560     //
2561     ui32RCC |= SYSCTL_RCC_BYPASS;
2562     ui32RCC &= ~(SYSCTL_RCC_USESYSDIV);
2563     ui32RCC2 |= SYSCTL_RCC2_BYPASS2;
2564 
2565     //
2566     // Write the new RCC value.
2567     //
2568     HWREG(SYSCTL_RCC) = ui32RCC;
2569     HWREG(SYSCTL_RCC2) = ui32RCC2;
2570 
2571     //
2572     // See if the oscillator needs to be enabled.
2573     //
2574     if((ui32RCC & SYSCTL_RCC_MOSCDIS) && !(ui32Config & SYSCTL_MAIN_OSC_DIS))
2575     {
2576         //
2577         // Make sure that the required oscillators are enabled.  For now, the
2578         // previously enabled oscillators must be enabled along with the newly
2579         // requested oscillators.
2580         //
2581         ui32RCC &= (~SYSCTL_RCC_MOSCDIS | (ui32Config & SYSCTL_MAIN_OSC_DIS));
2582 
2583         //
2584         // Clear the MOSC power up raw interrupt status to be sure it is not
2585         // set when waiting below.
2586         //
2587         HWREG(SYSCTL_MISC) = SYSCTL_MISC_MOSCPUPMIS;
2588 
2589         //
2590         // Write the new RCC value.
2591         //
2592         HWREG(SYSCTL_RCC) = ui32RCC;
2593 
2594         //
2595         // Timeout using the legacy delay value.
2596         //
2597         ui32Delay = 524288;
2598 
2599         while((HWREG(SYSCTL_RIS) & SYSCTL_RIS_MOSCPUPRIS) == 0)
2600         {
2601             ui32Delay--;
2602 
2603             if(ui32Delay == 0)
2604             {
2605                 break;
2606             }
2607         }
2608 
2609         //
2610         // If the main oscillator failed to start up then do not switch to
2611         // it and return.
2612         //
2613         if(ui32Delay == 0)
2614         {
2615             return;
2616         }
2617 
2618     }
2619 
2620     //
2621     // Set the new crystal value and oscillator source.  Because the OSCSRC2
2622     // field in RCC2 overlaps the XTAL field in RCC, the OSCSRC field has a
2623     // special encoding within ui32Config to avoid the overlap.
2624     //
2625     ui32RCC &= ~(SYSCTL_RCC_XTAL_M | SYSCTL_RCC_OSCSRC_M);
2626     ui32RCC |= ui32Config & (SYSCTL_RCC_XTAL_M | SYSCTL_RCC_OSCSRC_M);
2627     ui32RCC2 &= ~(SYSCTL_RCC2_USERCC2 | SYSCTL_RCC2_OSCSRC2_M);
2628     ui32RCC2 |= ui32Config & (SYSCTL_RCC2_USERCC2 | SYSCTL_RCC_OSCSRC_M);
2629     ui32RCC2 |= (ui32Config & 0x00000008) << 3;
2630 
2631     //
2632     // Write the new RCC value.
2633     //
2634     HWREG(SYSCTL_RCC) = ui32RCC;
2635     HWREG(SYSCTL_RCC2) = ui32RCC2;
2636 
2637     //
2638     // Set the PLL configuration.
2639     //
2640     ui32RCC &= ~SYSCTL_RCC_PWRDN;
2641     ui32RCC |= ui32Config & SYSCTL_RCC_PWRDN;
2642     ui32RCC2 &= ~SYSCTL_RCC2_PWRDN2;
2643     ui32RCC2 |= ui32Config & SYSCTL_RCC2_PWRDN2;
2644 
2645     //
2646     // Clear the PLL lock interrupt.
2647     //
2648     HWREG(SYSCTL_MISC) = SYSCTL_MISC_PLLLMIS;
2649 
2650     //
2651     // Write the new RCC value.
2652     //
2653     if(ui32RCC2 & SYSCTL_RCC2_USERCC2)
2654     {
2655         HWREG(SYSCTL_RCC2) = ui32RCC2;
2656         HWREG(SYSCTL_RCC) = ui32RCC;
2657     }
2658     else
2659     {
2660         HWREG(SYSCTL_RCC) = ui32RCC;
2661         HWREG(SYSCTL_RCC2) = ui32RCC2;
2662     }
2663 
2664     //
2665     // Set the requested system divider and disable the appropriate
2666     // oscillators.  This value is not written immediately.
2667     //
2668     ui32RCC &= ~(SYSCTL_RCC_SYSDIV_M | SYSCTL_RCC_USESYSDIV |
2669                  SYSCTL_RCC_MOSCDIS);
2670     ui32RCC |= ui32Config & (SYSCTL_RCC_SYSDIV_M | SYSCTL_RCC_USESYSDIV |
2671                              SYSCTL_RCC_MOSCDIS);
2672     ui32RCC2 &= ~(SYSCTL_RCC2_SYSDIV2_M);
2673     ui32RCC2 |= ui32Config & SYSCTL_RCC2_SYSDIV2_M;
2674     if(ui32Config & SYSCTL_RCC2_DIV400)
2675     {
2676         ui32RCC |= SYSCTL_RCC_USESYSDIV;
2677         ui32RCC2 &= ~(SYSCTL_RCC_USESYSDIV);
2678         ui32RCC2 |= ui32Config & (SYSCTL_RCC2_DIV400 | SYSCTL_RCC2_SYSDIV2LSB);
2679     }
2680     else
2681     {
2682         ui32RCC2 &= ~(SYSCTL_RCC2_DIV400);
2683     }
2684 
2685     //
2686     // See if the PLL output is being used to clock the system.
2687     //
2688     if(!(ui32Config & SYSCTL_RCC_BYPASS))
2689     {
2690         //
2691         // Wait until the PLL has locked.
2692         //
2693         for(ui32Delay = 32768; ui32Delay > 0; ui32Delay--)
2694         {
2695             if((HWREG(SYSCTL_PLLSTAT) & SYSCTL_PLLSTAT_LOCK))
2696             {
2697                 break;
2698             }
2699         }
2700 
2701         //
2702         // Enable use of the PLL.
2703         //
2704         ui32RCC &= ~(SYSCTL_RCC_BYPASS);
2705         ui32RCC2 &= ~(SYSCTL_RCC2_BYPASS2);
2706     }
2707 
2708     //
2709     // Write the final RCC value.
2710     //
2711     HWREG(SYSCTL_RCC) = ui32RCC;
2712     HWREG(SYSCTL_RCC2) = ui32RCC2;
2713 
2714     //
2715     // Delay for a little bit so that the system divider takes effect.
2716     //
2717     SysCtlDelay(16);
2718 }
2719 
2720 //*****************************************************************************
2721 //
2722 //! Gets the processor clock rate.
2723 //!
2724 //! This function determines the clock rate of the processor clock, which is
2725 //! also the clock rate of the peripheral modules (with the exception of
2726 //! PWM, which has its own clock divider; other peripherals may have different
2727 //! clocking, see the device data sheet for details).
2728 //!
2729 //! \note This cannot return accurate results if SysCtlClockSet() has not
2730 //! been called to configure the clocking of the device, or if the device is
2731 //! directly clocked from a crystal (or a clock source) that is not one of the
2732 //! supported crystal frequencies.  In the latter case, this function should be
2733 //! modified to directly return the correct system clock rate.
2734 //!
2735 //! \note This function can only be called on TM4C123 devices.  For TM4C129
2736 //! devices, the return value from SysCtlClockFreqSet() indicates the system
2737 //! clock frequency.
2738 //!
2739 //! \return The processor clock rate for TM4C123 devices only.
2740 //
2741 //*****************************************************************************
2742 uint32_t
SysCtlClockGet(void)2743 SysCtlClockGet(void)
2744 {
2745     uint32_t ui32RCC, ui32RCC2, ui32PLL, ui32Clk, ui32Max;
2746     uint32_t ui32PLL1;
2747 
2748     //
2749     // This function is only valid on TM4C123 devices.
2750     //
2751     ASSERT(CLASS_IS_TM4C123);
2752 
2753     //
2754     // Read RCC and RCC2.
2755     //
2756     ui32RCC = HWREG(SYSCTL_RCC);
2757     ui32RCC2 = HWREG(SYSCTL_RCC2);
2758 
2759     //
2760     // Get the base clock rate.
2761     //
2762     switch((ui32RCC2 & SYSCTL_RCC2_USERCC2) ?
2763            (ui32RCC2 & SYSCTL_RCC2_OSCSRC2_M) :
2764            (ui32RCC & SYSCTL_RCC_OSCSRC_M))
2765     {
2766         //
2767         // The main oscillator is the clock source.  Determine its rate from
2768         // the crystal setting field.
2769         //
2770         case SYSCTL_RCC_OSCSRC_MAIN:
2771         {
2772             ui32Clk = g_pui32Xtals[(ui32RCC & SYSCTL_RCC_XTAL_M) >>
2773                                    SYSCTL_RCC_XTAL_S];
2774             break;
2775         }
2776 
2777         //
2778         // The internal oscillator is the source clock.
2779         //
2780         case SYSCTL_RCC_OSCSRC_INT:
2781         {
2782             //
2783             // The internal oscillator on all devices is 16 MHz.
2784             //
2785             ui32Clk = 16000000;
2786             break;
2787         }
2788 
2789         //
2790         // The internal oscillator divided by four is the source clock.
2791         //
2792         case SYSCTL_RCC_OSCSRC_INT4:
2793         {
2794             //
2795             // The internal oscillator on all devices is 16 MHz.
2796             //
2797             ui32Clk = 16000000 / 4;
2798             break;
2799         }
2800 
2801         //
2802         // The internal 30-KHz oscillator is the source clock.
2803         //
2804         case SYSCTL_RCC_OSCSRC_30:
2805         {
2806             //
2807             // The internal 30-KHz oscillator has an accuracy of +/- 30%.
2808             //
2809             ui32Clk = 30000;
2810             break;
2811         }
2812 
2813         //
2814         // The 32.768-KHz clock from the hibernate module is the source clock.
2815         //
2816         case SYSCTL_RCC2_OSCSRC2_32:
2817         {
2818             ui32Clk = 32768;
2819             break;
2820         }
2821 
2822         //
2823         // An unknown setting, so return a zero clock (that is, an unknown
2824         // clock rate).
2825         //
2826         default:
2827         {
2828             return(0);
2829         }
2830     }
2831 
2832     //
2833     // Default the maximum frequency to the maximum 32-bit unsigned value.
2834     //
2835     ui32Max = 0xffffffff;
2836 
2837     //
2838     // See if the PLL is being used.
2839     //
2840     if(((ui32RCC2 & SYSCTL_RCC2_USERCC2) &&
2841         !(ui32RCC2 & SYSCTL_RCC2_BYPASS2)) ||
2842        (!(ui32RCC2 & SYSCTL_RCC2_USERCC2) && !(ui32RCC & SYSCTL_RCC_BYPASS)))
2843     {
2844         //
2845         // Read the two PLL frequency registers.  The formula for a
2846         // TM4C123 device is "(xtal * m) / ((q + 1) * (n + 1))".
2847         //
2848         ui32PLL = HWREG(SYSCTL_PLLFREQ0);
2849         ui32PLL1 = HWREG(SYSCTL_PLLFREQ1);
2850 
2851         //
2852         // Divide the input clock by the dividers.
2853         //
2854         ui32Clk /= ((((ui32PLL1 & SYSCTL_PLLFREQ1_Q_M) >>
2855                       SYSCTL_PLLFREQ1_Q_S) + 1) *
2856                     (((ui32PLL1 & SYSCTL_PLLFREQ1_N_M) >>
2857                       SYSCTL_PLLFREQ1_N_S) + 1) * 2);
2858 
2859         //
2860         // Multiply the clock by the multiplier, which is split into an
2861         // integer part and a fractional part.
2862         //
2863         ui32Clk = ((ui32Clk * ((ui32PLL & SYSCTL_PLLFREQ0_MINT_M) >>
2864                                SYSCTL_PLLFREQ0_MINT_S)) +
2865                    ((ui32Clk * ((ui32PLL & SYSCTL_PLLFREQ0_MFRAC_M) >>
2866                                 SYSCTL_PLLFREQ0_MFRAC_S)) >> 10));
2867 
2868         //
2869         // Force the system divider to be enabled.  It is always used when
2870         // using the PLL, but in some cases it does not read as being enabled.
2871         //
2872         ui32RCC |= SYSCTL_RCC_USESYSDIV;
2873 
2874         //
2875         // Calculate the maximum system frequency.
2876         //
2877         switch(HWREG(SYSCTL_DC1) & SYSCTL_DC1_MINSYSDIV_M)
2878         {
2879             case SYSCTL_DC1_MINSYSDIV_80:
2880             {
2881                 ui32Max = 80000000;
2882                 break;
2883             }
2884             case SYSCTL_DC1_MINSYSDIV_50:
2885             {
2886                 ui32Max = 50000000;
2887                 break;
2888             }
2889             case SYSCTL_DC1_MINSYSDIV_40:
2890             {
2891                 ui32Max = 40000000;
2892                 break;
2893             }
2894             case SYSCTL_DC1_MINSYSDIV_25:
2895             {
2896                 ui32Max = 25000000;
2897                 break;
2898             }
2899             case SYSCTL_DC1_MINSYSDIV_20:
2900             {
2901                 ui32Max = 20000000;
2902                 break;
2903             }
2904             default:
2905             {
2906                 break;
2907             }
2908         }
2909     }
2910 
2911     //
2912     // See if the system divider is being used.
2913     //
2914     if(ui32RCC & SYSCTL_RCC_USESYSDIV)
2915     {
2916         //
2917         // Adjust the clock rate by the system clock divider.
2918         //
2919         if(ui32RCC2 & SYSCTL_RCC2_USERCC2)
2920         {
2921             if((ui32RCC2 & SYSCTL_RCC2_DIV400) &&
2922                (((ui32RCC2 & SYSCTL_RCC2_USERCC2) &&
2923                  !(ui32RCC2 & SYSCTL_RCC2_BYPASS2)) ||
2924                 (!(ui32RCC2 & SYSCTL_RCC2_USERCC2) &&
2925                  !(ui32RCC & SYSCTL_RCC_BYPASS))))
2926 
2927             {
2928                 ui32Clk = ((ui32Clk * 2) / (((ui32RCC2 &
2929                                               (SYSCTL_RCC2_SYSDIV2_M |
2930                                                SYSCTL_RCC2_SYSDIV2LSB)) >>
2931                                              (SYSCTL_RCC2_SYSDIV2_S - 1)) +
2932                                             1));
2933             }
2934             else
2935             {
2936                 ui32Clk /= (((ui32RCC2 & SYSCTL_RCC2_SYSDIV2_M) >>
2937                              SYSCTL_RCC2_SYSDIV2_S) + 1);
2938             }
2939         }
2940         else
2941         {
2942             ui32Clk /= (((ui32RCC & SYSCTL_RCC_SYSDIV_M) >>
2943                          SYSCTL_RCC_SYSDIV_S) + 1);
2944         }
2945     }
2946 
2947     //
2948     // Limit the maximum clock to the maximum clock frequency.
2949     //
2950     if(ui32Max < ui32Clk)
2951     {
2952         ui32Clk = ui32Max;
2953     }
2954 
2955     //
2956     // Return the computed clock rate.
2957     //
2958     return(ui32Clk);
2959 }
2960 
2961 //*****************************************************************************
2962 //
2963 //! Sets the clocking of the device while in deep-sleep mode.
2964 //!
2965 //! \param ui32Config is the required configuration of the device clocking
2966 //! while in deep-sleep mode.
2967 //!
2968 //! This function configures the clocking of the device while in deep-sleep
2969 //! mode.  The oscillator to be used and the system clock divider are
2970 //! configured with this function.
2971 //!
2972 //! The \e ui32Config parameter is the logical OR of the following values:
2973 //!
2974 //! The system clock divider is chosen from one of the following values:
2975 //! \b SYSCTL_DSLP_DIV_1, \b SYSCTL_DSLP_DIV_2, \b SYSCTL_DSLP_DIV_3, ...
2976 //! \b SYSCTL_DSLP_DIV_64.
2977 //!
2978 //! The oscillator source is chosen from one of the following values:
2979 //! \b SYSCTL_DSLP_OSC_MAIN, \b SYSCTL_DSLP_OSC_INT, \b SYSCTL_DSLP_OSC_INT30,
2980 //! or \b SYSCTL_DSLP_OSC_EXT32.  \b SYSCTL_OSC_EXT32 is only available on
2981 //! devices with the hibernation module, and then only when the hibernation
2982 //! module has been enabled.
2983 //!
2984 //! The precision internal oscillator can be powered down in deep-sleep mode by
2985 //! specifying \b SYSCTL_DSLP_PIOSC_PD.  The precision internal oscillator is
2986 //! not powered down if it is required for operation while in deep-sleep
2987 //! (based on other configuration settings.)
2988 //!
2989 //! \note This function should only be called on TM4C123 devices.  For
2990 //! other devices use the SysCtlDeepSleepClockConfigSet() function.
2991 //!
2992 //! \note The availability of deep-sleep clocking configuration varies with the
2993 //! Tiva part in use.  Please consult the data sheet for the part you are
2994 //! using to determine whether this support is available.
2995 //!
2996 //! \return None.
2997 //
2998 //*****************************************************************************
2999 void
SysCtlDeepSleepClockSet(uint32_t ui32Config)3000 SysCtlDeepSleepClockSet(uint32_t ui32Config)
3001 {
3002     //
3003     // Set the deep-sleep clock configuration.
3004     //
3005     HWREG(SYSCTL_DSLPCLKCFG) = ui32Config;
3006 }
3007 
3008 //*****************************************************************************
3009 //
3010 //! Sets the clock configuration of the device while in deep-sleep mode.
3011 //!
3012 //! \param ui32Div is the clock divider when in deep-sleep mode.
3013 //! \param ui32Config is the configuration of the device clocking while
3014 //! in deep-sleep mode.
3015 //!
3016 //! This function configures the clocking of the device while in deep-sleep
3017 //! mode.  The \e ui32Config parameter selects the oscillator and the
3018 //! \e ui32Div parameter sets the clock divider used in deep-sleep mode.  The
3019 //! valid values for the \e ui32Div parameter range from 1 to 1024, however not
3020 //! all Tiva microcontrollers support this full range.  This function
3021 //! replaces the SysCtlDeepSleepClockSet() function and can be used on
3022 //! Tiva devices that support deep-sleep mode.
3023 //!
3024 //! The oscillator source is chosen from one of the following values:
3025 //! \b SYSCTL_DSLP_OSC_MAIN, \b SYSCTL_DSLP_OSC_INT, \b SYSCTL_DSLP_OSC_INT30,
3026 //! or \b SYSCTL_DSLP_OSC_EXT32.  The \b SYSCTL_DSLP_OSC_EXT32 option is only
3027 //! available on devices with the hibernation module, and then only when the
3028 //! hibernation module is enabled.
3029 //!
3030 //! The precision internal oscillator can be powered down in deep-sleep mode by
3031 //! specifying \b SYSCTL_DSLP_PIOSC_PD.  The precision internal oscillator is
3032 //! not powered down if it is required for operation while in deep-sleep
3033 //! (based on other configuration settings).
3034 //!
3035 //! The main oscillator can be powered down in deep-sleep mode by
3036 //! specifying \b SYSCTL_DSLP_MOSC_PD.  The main oscillator is
3037 //! not powered down if it is required for operation while in deep-sleep
3038 //! (based on other configuration settings).
3039 //!
3040 //! \note The availability of deep-sleep clocking configuration and the
3041 //! configuration values vary with the Tiva device in use.  Please consult
3042 //! the data sheet for the device you are using to determine whether the
3043 //! desired configuration options are available and to determine the valid
3044 //! range for the clock divider.
3045 //!
3046 //! \return None.
3047 //
3048 //*****************************************************************************
3049 void
SysCtlDeepSleepClockConfigSet(uint32_t ui32Div,uint32_t ui32Config)3050 SysCtlDeepSleepClockConfigSet(uint32_t ui32Div, uint32_t ui32Config)
3051 {
3052     uint32_t ui32Value;
3053 
3054     ASSERT(ui32Div != 0);
3055 
3056     if(CLASS_IS_TM4C123)
3057     {
3058         //
3059         // Set the deep-sleep clock configuration.
3060         //
3061         HWREG(SYSCTL_DSLPCLKCFG) = (ui32Config & ~SYSCTL_DSLPCLKCFG_D_M) |
3062                                    ((ui32Div - 1) << SYSCTL_DSLPCLKCFG_D_S);
3063     }
3064     else
3065     {
3066         //
3067         // Initialize the value with the divider.
3068         //
3069         ui32Value = ui32Div - 1;
3070 
3071         //
3072         // Set the clock source selection based on the defines used for
3073         // SysCtlDeepSleepClockSet() function so that there is some backwards
3074         // compatibility.
3075         //
3076         switch(ui32Config & SYSCTL_DSLPCLKCFG_O_M)
3077         {
3078             //
3079             // Choose the main external oscillator.
3080             //
3081             case SYSCTL_DSLP_OSC_MAIN:
3082             {
3083                 ui32Value |= SYSCTL_DSCLKCFG_DSOSCSRC_MOSC;
3084 
3085                 break;
3086             }
3087 
3088             //
3089             // Choose the low frequency oscillator.
3090             //
3091             case SYSCTL_DSLP_OSC_INT30:
3092             {
3093                 ui32Value |= SYSCTL_DSCLKCFG_DSOSCSRC_LFIOSC;
3094 
3095                 break;
3096             }
3097 
3098             //
3099             // Choose the low frequency oscillator.
3100             //
3101             case SYSCTL_DSLP_OSC_EXT32:
3102             {
3103                 ui32Value |= SYSCTL_DSCLKCFG_DSOSCSRC_RTC;
3104 
3105                 break;
3106             }
3107             //
3108             // The zero value uses the PIOSC as the clock source.
3109             //
3110             case SYSCTL_DSLP_OSC_INT:
3111             default:
3112             {
3113                 break;
3114             }
3115         }
3116 
3117         //
3118         // Set the PIOSC power down bit.
3119         //
3120         if(ui32Config & SYSCTL_DSLP_PIOSC_PD)
3121         {
3122             ui32Value |= SYSCTL_DSCLKCFG_PIOSCPD;
3123         }
3124 
3125         //
3126         // Set the PIOSC power down bit.
3127         //
3128         if(ui32Config & SYSCTL_DSLP_MOSC_PD)
3129         {
3130             ui32Value |= SYSCTL_DSCLKCFG_MOSCDPD;
3131         }
3132 
3133         //
3134         // Update the deep-sleep clock configuration.
3135         //
3136         HWREG(SYSCTL_DSCLKCFG) = ui32Value;
3137     }
3138 }
3139 
3140 //*****************************************************************************
3141 //
3142 //! Sets the PWM clock configuration.
3143 //!
3144 //! \param ui32Config is the configuration for the PWM clock; it must be one of
3145 //! \b SYSCTL_PWMDIV_1, \b SYSCTL_PWMDIV_2, \b SYSCTL_PWMDIV_4,
3146 //! \b SYSCTL_PWMDIV_8, \b SYSCTL_PWMDIV_16, \b SYSCTL_PWMDIV_32, or
3147 //! \b SYSCTL_PWMDIV_64.
3148 //!
3149 //! This function configures the rate of the clock provided to the PWM module
3150 //! as a ratio of the processor clock.  This clock is used by the PWM module to
3151 //! generate PWM signals; its rate forms the basis for all PWM signals.
3152 //!
3153 //! \note This function should only be used with TM4C123 devices.  For
3154 //! other TM4C devices, the PWMClockSet() function should be used.
3155 //!
3156 //! \note The clocking of the PWM is dependent on the system clock rate as
3157 //! configured by SysCtlClockSet().
3158 //!
3159 //! \return None.
3160 //
3161 //*****************************************************************************
3162 void
SysCtlPWMClockSet(uint32_t ui32Config)3163 SysCtlPWMClockSet(uint32_t ui32Config)
3164 {
3165     //
3166     // Check the arguments.
3167     //
3168     ASSERT((ui32Config == SYSCTL_PWMDIV_1) ||
3169            (ui32Config == SYSCTL_PWMDIV_2) ||
3170            (ui32Config == SYSCTL_PWMDIV_4) ||
3171            (ui32Config == SYSCTL_PWMDIV_8) ||
3172            (ui32Config == SYSCTL_PWMDIV_16) ||
3173            (ui32Config == SYSCTL_PWMDIV_32) ||
3174            (ui32Config == SYSCTL_PWMDIV_64));
3175 
3176     //
3177     // Check that there is a PWM block on this part.
3178     //
3179     ASSERT(HWREG(SYSCTL_DC1) & (SYSCTL_DC1_PWM0 | SYSCTL_DC1_PWM1));
3180 
3181     //
3182     // Set the PWM clock configuration into the run-mode clock configuration
3183     // register.
3184     //
3185     HWREG(SYSCTL_RCC) = ((HWREG(SYSCTL_RCC) &
3186                           ~(SYSCTL_RCC_USEPWMDIV | SYSCTL_RCC_PWMDIV_M)) |
3187                          ui32Config);
3188 }
3189 
3190 //*****************************************************************************
3191 //
3192 //! Gets the current PWM clock configuration.
3193 //!
3194 //! This function returns the current PWM clock configuration.
3195 //!
3196 //! \return Returns the current PWM clock configuration; is one of
3197 //! \b SYSCTL_PWMDIV_1, \b SYSCTL_PWMDIV_2, \b SYSCTL_PWMDIV_4,
3198 //! \b SYSCTL_PWMDIV_8, \b SYSCTL_PWMDIV_16, \b SYSCTL_PWMDIV_32, or
3199 //! \b SYSCTL_PWMDIV_64.
3200 //!
3201 //! \note This function should only be used with TM4C123 devices.  For
3202 //! other TM4C devices, the PWMClockGet() function should be used.
3203 //
3204 //*****************************************************************************
3205 uint32_t
SysCtlPWMClockGet(void)3206 SysCtlPWMClockGet(void)
3207 {
3208     //
3209     // Check that there is a PWM block on this part.
3210     //
3211     ASSERT(HWREG(SYSCTL_DC1) & (SYSCTL_DC1_PWM0 | SYSCTL_DC1_PWM1));
3212 
3213     //
3214     // Return the current PWM clock configuration.  Make sure that
3215     // SYSCTL_PWMDIV_1 is returned in all cases where the divider is disabled.
3216     //
3217     if(!(HWREG(SYSCTL_RCC) & SYSCTL_RCC_USEPWMDIV))
3218     {
3219         //
3220         // The divider is not active so reflect this in the value we return.
3221         //
3222         return(SYSCTL_PWMDIV_1);
3223     }
3224     else
3225     {
3226         //
3227         // The divider is active so directly return the masked register value.
3228         //
3229         return(HWREG(SYSCTL_RCC) &
3230                (SYSCTL_RCC_USEPWMDIV | SYSCTL_RCC_PWMDIV_M));
3231     }
3232 }
3233 
3234 //*****************************************************************************
3235 //
3236 //! Enables access to a GPIO peripheral via the AHB.
3237 //!
3238 //! \param ui32GPIOPeripheral is the GPIO peripheral to enable.
3239 //!
3240 //! This function is used to enable the specified GPIO peripheral to be
3241 //! accessed from the Advanced Host Bus (AHB) instead of the legacy Advanced
3242 //! Peripheral Bus (APB).  When a GPIO peripheral is enabled for AHB access,
3243 //! the \b _AHB_BASE form of the base address should be used for GPIO
3244 //! functions.  For example, instead of using \b GPIO_PORTA_BASE as the base
3245 //! address for GPIO functions, use \b GPIO_PORTA_AHB_BASE instead.
3246 //!
3247 //! The \e ui32GPIOPeripheral argument must be only one of the following
3248 //! values:
3249 //! \b SYSCTL_PERIPH_GPIOA, \b SYSCTL_PERIPH_GPIOB, \b SYSCTL_PERIPH_GPIOC,
3250 //! \b SYSCTL_PERIPH_GPIOD, \b SYSCTL_PERIPH_GPIOE, \b SYSCTL_PERIPH_GPIOF,
3251 //! \b SYSCTL_PERIPH_GPIOG, \b SYSCTL_PERIPH_GPIOH, or \b SYSCTL_PERIPH_GPIOJ.
3252 //!
3253 //! \note On some devices, all GPIO ports are only available on AHB.
3254 //!
3255 //! \return None.
3256 //
3257 //*****************************************************************************
3258 void
SysCtlGPIOAHBEnable(uint32_t ui32GPIOPeripheral)3259 SysCtlGPIOAHBEnable(uint32_t ui32GPIOPeripheral)
3260 {
3261     //
3262     // Check the arguments.
3263     //
3264     ASSERT((ui32GPIOPeripheral == SYSCTL_PERIPH_GPIOA) ||
3265            (ui32GPIOPeripheral == SYSCTL_PERIPH_GPIOB) ||
3266            (ui32GPIOPeripheral == SYSCTL_PERIPH_GPIOC) ||
3267            (ui32GPIOPeripheral == SYSCTL_PERIPH_GPIOD) ||
3268            (ui32GPIOPeripheral == SYSCTL_PERIPH_GPIOE) ||
3269            (ui32GPIOPeripheral == SYSCTL_PERIPH_GPIOF) ||
3270            (ui32GPIOPeripheral == SYSCTL_PERIPH_GPIOG) ||
3271            (ui32GPIOPeripheral == SYSCTL_PERIPH_GPIOH) ||
3272            (ui32GPIOPeripheral == SYSCTL_PERIPH_GPIOJ));
3273 
3274     //
3275     // Enable this GPIO for AHB access.
3276     //
3277     HWREG(SYSCTL_GPIOHBCTL) |= (1 << (ui32GPIOPeripheral & 0xF));
3278 }
3279 
3280 //*****************************************************************************
3281 //
3282 //! Disables access to a GPIO peripheral via the AHB.
3283 //!
3284 //! \param ui32GPIOPeripheral is the GPIO peripheral to disable.
3285 //!
3286 //! This function disables the specified GPIO peripheral for access from the
3287 //! Advanced Host Bus (AHB).  Once disabled, the GPIO peripheral is accessed
3288 //! from the legacy Advanced Peripheral Bus (APB).
3289 //!
3290 //! The \b ui32GPIOPeripheral argument must be only one of the following
3291 //! values:
3292 //! \b SYSCTL_PERIPH_GPIOA, \b SYSCTL_PERIPH_GPIOB, \b SYSCTL_PERIPH_GPIOC,
3293 //! \b SYSCTL_PERIPH_GPIOD, \b SYSCTL_PERIPH_GPIOE, \b SYSCTL_PERIPH_GPIOF,
3294 //! \b SYSCTL_PERIPH_GPIOG, \b SYSCTL_PERIPH_GPIOH, or \b SYSCTL_PERIPH_GPIOJ.
3295 //!
3296 //! \note Some devices allow disabling AHB access to GPIO ports that are only
3297 //! present on the AHB.  Disabling AHB access to these ports will disable
3298 //! access to these GPIO ports. On some devices, all GPIO ports are only
3299 //! available on AHB.
3300 //!
3301 //! \return None.
3302 //
3303 //*****************************************************************************
3304 void
SysCtlGPIOAHBDisable(uint32_t ui32GPIOPeripheral)3305 SysCtlGPIOAHBDisable(uint32_t ui32GPIOPeripheral)
3306 {
3307     //
3308     // Check the arguments.
3309     //
3310     ASSERT((ui32GPIOPeripheral == SYSCTL_PERIPH_GPIOA) ||
3311            (ui32GPIOPeripheral == SYSCTL_PERIPH_GPIOB) ||
3312            (ui32GPIOPeripheral == SYSCTL_PERIPH_GPIOC) ||
3313            (ui32GPIOPeripheral == SYSCTL_PERIPH_GPIOD) ||
3314            (ui32GPIOPeripheral == SYSCTL_PERIPH_GPIOE) ||
3315            (ui32GPIOPeripheral == SYSCTL_PERIPH_GPIOF) ||
3316            (ui32GPIOPeripheral == SYSCTL_PERIPH_GPIOG) ||
3317            (ui32GPIOPeripheral == SYSCTL_PERIPH_GPIOH) ||
3318            (ui32GPIOPeripheral == SYSCTL_PERIPH_GPIOJ));
3319 
3320     //
3321     // Disable this GPIO for AHB access.
3322     //
3323     HWREG(SYSCTL_GPIOHBCTL) &= ~(1 << (ui32GPIOPeripheral & 0xF));
3324 }
3325 
3326 //*****************************************************************************
3327 //
3328 //! Powers up the USB PLL.
3329 //!
3330 //! This function enables the USB controller's PLL, which is used by its
3331 //! physical layer.  This call is necessary before connecting to any external
3332 //! devices.
3333 //!
3334 //! \note This function should only be called on TM4C123 devices.
3335 //!
3336 //! \return None.
3337 //
3338 //*****************************************************************************
3339 void
SysCtlUSBPLLEnable(void)3340 SysCtlUSBPLLEnable(void)
3341 {
3342     //
3343     // Turn on the USB PLL.
3344     //
3345     HWREG(SYSCTL_RCC2) &= ~SYSCTL_RCC2_USBPWRDN;
3346 }
3347 
3348 //*****************************************************************************
3349 //
3350 //! Powers down the USB PLL.
3351 //!
3352 //! This function disables the USB controller's PLL, which is used by its
3353 //! physical layer.  The USB registers are still accessible, but the physical
3354 //! layer no longer functions.
3355 //!
3356 //! \note This function should only be called on TM4C123 devices.
3357 //!
3358 //! \return None.
3359 //
3360 //*****************************************************************************
3361 void
SysCtlUSBPLLDisable(void)3362 SysCtlUSBPLLDisable(void)
3363 {
3364     //
3365     // Turn off the USB PLL.
3366     //
3367     HWREG(SYSCTL_RCC2) |= SYSCTL_RCC2_USBPWRDN;
3368 }
3369 
3370 //*****************************************************************************
3371 //
3372 //! Configures the response to system voltage events.
3373 //!
3374 //! \param ui32Config holds the configuration options for the voltage events.
3375 //!
3376 //! This function configures the response to voltage-related events.
3377 //! These events are triggered when the voltage rails drop below certain
3378 //! levels.  The \e ui32Config parameter provides the configuration for the
3379 //! voltage events and is a combination of the \b SYSCTL_VEVENT_* values.
3380 //!
3381 //! The response to a brown out on the VDDA rail is set by using one of the
3382 //! following values:
3383 //! - \b SYSCTL_VEVENT_VDDABO_NONE - There is no action taken on a VDDA
3384 //!      brown out.
3385 //! - \b SYSCTL_VEVENT_VDDABO_INT - A system interrupt is generated when a
3386 //!      VDDA brown out occurs.
3387 //! - \b SYSCTL_VEVENT_VDDABO_NMI - An NMI is generated when a VDDA brown out
3388 //!      occurs.
3389 //! - \b SYSCTL_VEVENT_VDDABO_RST - A reset is generated when a VDDA brown out
3390 //!      occurs.  The type of reset that is generated is controller by the
3391 //!      \b SYSCTL_ONRST_BOR_* setting passed into the SysCtlResetBehaviorSet()
3392 //!      function.
3393 //!
3394 //! The response to a brown out on the VDD rail is set by using one of the
3395 //! following values:
3396 //! - \b SYSCTL_VEVENT_VDDBO_NONE - There is no action taken on a VDD
3397 //!      brown out.
3398 //! - \b SYSCTL_VEVENT_VDDBO_INT - A system interrupt is generated when a
3399 //!      VDD brown out occurs.
3400 //! - \b SYSCTL_VEVENT_VDDBO_NMI - An NMI is generated when a VDD brown out
3401 //!      occurs.
3402 //! - \b SYSCTL_VEVENT_VDDBO_RST - A reset is generated when a VDD brown out
3403 //!      occurs.  The type of reset that is generated is controller by the
3404 //!      \b SYSCTL_ONRST_BOR_* setting passed into the SysCtlResetBehaviorSet()
3405 //!      function.
3406 //!
3407 //! \b Example: Configure the voltage events to trigger an interrupt on a VDDA
3408 //! brown out, an NMI on a VDDC brown out and a reset on a VDD brown out.
3409 //!
3410 //! \verbatim
3411 //!
3412 //! //
3413 //! // Configure the BOR rest to trigger a full POR.  This is needed because
3414 //! // the SysCtlVoltageEventConfig() call is triggering a reset so the type
3415 //! // of reset is specified by this call.
3416 //! //
3417 //! SysCtlResetBehaviorSet(SYSCTL_ONRST_BOR_POR);
3418 //!
3419 //! //
3420 //! // Trigger an interrupt on a VDDA brown out and a reset on a VDD brown out.
3421 //! //
3422 //! SysCtlVoltageEventConfig(SYSCTL_VEVENT_VDDABO_INT |
3423 //!                          SYSCTL_VEVENT_VDDBO_RST);
3424 //! \endverbatim
3425 //!
3426 //! \return None.
3427 //
3428 //*****************************************************************************
3429 void
SysCtlVoltageEventConfig(uint32_t ui32Config)3430 SysCtlVoltageEventConfig(uint32_t ui32Config)
3431 {
3432     //
3433     // Set the requested events.
3434     //
3435     HWREG(SYSCTL_PTBOCTL) = ui32Config;
3436 }
3437 
3438 //*****************************************************************************
3439 //
3440 //! Returns the voltage event status.
3441 //!
3442 //! This function returns the voltage event status for the system controller.
3443 //! The value returned is a logical OR of the following values:
3444 //! - \b SYSCTL_VESTAT_VDDBOR a brown-out event occurred on the VDD rail.
3445 //! - \b SYSCTL_VESTAT_VDDABOR a brown-out event occurred on the VDDA rail.
3446 //!
3447 //! The values returned from this function can be passed to the
3448 //! SysCtlVoltageEventClear() to clear the current voltage event status.
3449 //! Because voltage events are not cleared due to a reset, the voltage event
3450 //! status must be cleared by calling SysCtlVoltageEventClear().
3451 //!
3452 //! \b Example: Clear the current voltage event status.
3453 //!
3454 //! \verbatim
3455 //! uint32_t ui32VoltageEvents;
3456 //!
3457 //! //
3458 //! // Read the current voltage event status.
3459 //! //
3460 //! ui32VoltageEvents = SysCtlVoltageEventStatus();
3461 //!
3462 //! //
3463 //! // Clear all the current voltage events.
3464 //! //
3465 //! SysCtlVoltageEventClear(ui32VoltageEvents);
3466 //! \endverbatim
3467 //!
3468 //! \return The current voltage event status.
3469 //!
3470 //! \note The availability of voltage events varies with the Tiva part
3471 //! in use.  Please consult the data sheet for the part you are using to
3472 //! determine which interrupt sources are available.
3473 //
3474 //*****************************************************************************
3475 uint32_t
SysCtlVoltageEventStatus(void)3476 SysCtlVoltageEventStatus(void)
3477 {
3478     //
3479     // Return the current voltage event status.
3480     //
3481     return(HWREG(SYSCTL_PWRTC));
3482 }
3483 
3484 //*****************************************************************************
3485 //
3486 //! Clears the voltage event status.
3487 //!
3488 //! \param ui32Status is a bit mask of the voltage events to clear.
3489 //!
3490 //! This function clears the current voltage events status for the values
3491 //! specified in the \e ui32Status parameter.  The \e ui32Status value must be
3492 //! a logical OR of the following values:
3493 //! - \b SYSCTL_VESTAT_VDDBOR a brown-out event occurred on the VDD rail.
3494 //! - \b SYSCTL_VESTAT_VDDABOR a brown-out event occurred on the VDDA rail.
3495 //!
3496 //! \b Example: Clear the current voltage event status.
3497 //!
3498 //! \verbatim
3499 //! //
3500 //! // Clear all the current voltage events.
3501 //! //
3502 //! SysCtlVoltageEventClear(SysCtlVoltageEventStatus());
3503 //! \endverbatim
3504 //!
3505 //! \note The availability of voltage event status varies with the
3506 //! Tiva part in use.  Please consult the data sheet for the part you are
3507 //! using to determine which interrupt sources are available.
3508 //!
3509 //! \return None.
3510 //
3511 //*****************************************************************************
3512 void
SysCtlVoltageEventClear(uint32_t ui32Status)3513 SysCtlVoltageEventClear(uint32_t ui32Status)
3514 {
3515     //
3516     // Clear the requested voltage events.
3517     //
3518     HWREG(SYSCTL_PWRTC) |= ui32Status;
3519 }
3520 
3521 //*****************************************************************************
3522 //
3523 //! Gets the effective VCO frequency.
3524 //!
3525 //! \param ui32Crystal holds the crystal value used for the PLL.
3526 //! \param pui32VCOFrequency is a pointer to the storage location which holds
3527 //! value of the VCO computed.
3528 //!
3529 //! This function calculates the VCO of the PLL before the system divider is
3530 //! applied
3531 //!
3532 //! \return \b true if the PLL is configured correctly and a VCO is valid or
3533 //! \b false if the device is not TM4C129x or the PLL is not used
3534 //
3535 //*****************************************************************************
3536 bool
SysCtlVCOGet(uint32_t ui32Crystal,uint32_t * pui32VCOFrequency)3537 SysCtlVCOGet(uint32_t ui32Crystal, uint32_t *pui32VCOFrequency)
3538 {
3539     int32_t i32XtalIdx;
3540     uint32_t ui32RSClkConfig, ui32PLLFreq0, ui32PLLFreq1, ui32Osc;
3541     uint32_t ui32MInt, ui32MFrac, ui32NDiv, ui32QDiv, ui32TempVCO;
3542 
3543     //
3544     // Check if TM4C123 device is being used. should not use this function.
3545     //
3546     if(CLASS_IS_TM4C123)
3547     {
3548         //
3549         // Return error if TM4C123.
3550         //
3551         *pui32VCOFrequency = 0;
3552         return(false);
3553     }
3554 
3555     //
3556     // Read the RSCLKCFG register to determine if PLL is being used.
3557     //
3558     ui32RSClkConfig = HWREG(SYSCTL_RSCLKCFG);
3559 
3560     //
3561     // Check if PLL is used.
3562     //
3563     if((ui32RSClkConfig & SYSCTL_RSCLKCFG_USEPLL) != SYSCTL_RSCLKCFG_USEPLL)
3564     {
3565         //
3566         // Return error if PLL is not used.
3567         //
3568         *pui32VCOFrequency = 0;
3569         return(false);
3570     }
3571 
3572     //
3573     // Get the index of the crystal from the ui32Config parameter.
3574     //
3575     i32XtalIdx = SysCtlXtalCfgToIndex(ui32Crystal);
3576 
3577     //
3578     // Get the value of the crystal frequency based on the index
3579     //
3580     ui32Osc = g_pui32Xtals[i32XtalIdx];
3581 
3582     //
3583     // Read the PLLFREQ0 and PLLFREQ1 registers to get information on the
3584     // MINT, MFRAC, N and Q values of the PLL
3585     //
3586     ui32PLLFreq0 = HWREG(SYSCTL_PLLFREQ0);
3587     ui32PLLFreq1 = HWREG(SYSCTL_PLLFREQ1);
3588 
3589     ui32MInt = (ui32PLLFreq0 & SYSCTL_PLLFREQ0_MINT_M) >>
3590                SYSCTL_PLLFREQ0_MINT_S;
3591     ui32MFrac = (ui32PLLFreq0 & SYSCTL_PLLFREQ0_MFRAC_M) >>
3592                 SYSCTL_PLLFREQ0_MFRAC_S;
3593     ui32NDiv = (ui32PLLFreq1 & SYSCTL_PLLFREQ1_N_M) >>
3594                SYSCTL_PLLFREQ1_N_S;
3595     ui32QDiv = (ui32PLLFreq1 & SYSCTL_PLLFREQ1_Q_M) >>
3596                SYSCTL_PLLFREQ1_Q_S;
3597 
3598     //
3599     // Calculate the VCO at the output of the PLL
3600     //
3601     ui32TempVCO = (ui32Osc * ui32MInt) + ((ui32Osc * ui32MFrac) / 1024);
3602     ui32TempVCO /= ((ui32NDiv + 1) * (ui32QDiv + 1));
3603 
3604     *pui32VCOFrequency = ui32TempVCO;
3605     return(true);
3606 }
3607 
3608 //*****************************************************************************
3609 //
3610 //! Returns the current NMI status.
3611 //!
3612 //! This function returns the NMI status for the system controller.  The valid
3613 //! values for the \e ui32Ints parameter are a logical OR of the following
3614 //! values:
3615 //! - \b SYSCTL_NMI_MOSCFAIL the main oscillator is not present or did not
3616 //!   start.
3617 //! - \b SYSCTL_NMI_TAMPER a tamper event has been detected.
3618 //! - \b SYSCTL_NMI_WDT0 watchdog 0 generated a timeout.
3619 //! - \b SYSCTL_NMI_WDT1 watchdog 1 generated a timeout.
3620 //! - \b SYSCTL_NMI_POWER a power event occurred.
3621 //! - \b SYSCTL_NMI_EXTERNAL an external NMI pin asserted.
3622 //!
3623 //! \b Example: Clear all current NMI status flags.
3624 //!
3625 //! \verbatim
3626 //!
3627 //! //
3628 //! // Clear all the current NMI sources.
3629 //! //
3630 //! SysCtlNMIClear(SysCtlNMIStatus());
3631 //! \endverbatim
3632 //!
3633 //! \note The availability of the NMI status varies with the Tiva part in
3634 //! use.  Please consult the data sheet for the part you are using to determine
3635 //! which interrupt sources are available.
3636 //!
3637 //! \return The current NMI status.
3638 //
3639 //*****************************************************************************
3640 uint32_t
SysCtlNMIStatus(void)3641 SysCtlNMIStatus(void)
3642 {
3643     return(HWREG(SYSCTL_NMIC));
3644 }
3645 
3646 //*****************************************************************************
3647 //
3648 //! Clears NMI sources.
3649 //!
3650 //! \param ui32Ints is a bit mask of the non-maskable interrupt sources.
3651 //!
3652 //! This function clears the current NMI status specified in the \e ui32Ints
3653 //! parameter.  The valid values for the \e ui32Ints parameter are a logical OR
3654 //! of the following values:
3655 //! - \b SYSCTL_NMI_MOSCFAIL the main oscillator is not present or did not
3656 //!   start.
3657 //! - \b SYSCTL_NMI_TAMPER a tamper event has been detected.
3658 //! - \b SYSCTL_NMI_WDT0 watchdog 0 generated a timeout.
3659 //! - \b SYSCTL_NMI_WDT1 watchdog 1 generated a timeout.
3660 //! - \b SYSCTL_NMI_POWER a power event occurred.
3661 //! - \b SYSCTL_NMI_EXTERNAL an external NMI pin asserted.
3662 //!
3663 //! \b Example: Clear all current NMI status flags.
3664 //!
3665 //! \verbatim
3666 //!
3667 //! //
3668 //! // Clear all the current NMI sources.
3669 //! //
3670 //! SysCtlNMIClear(SysCtlNMIStatus());
3671 //! \endverbatim
3672 //!
3673 //! \note The availability of the NMI status varies with the Tiva part in
3674 //! use.  Please consult the data sheet for the part you are using to determine
3675 //! which interrupt sources are available.
3676 //!
3677 //! \return None.
3678 //
3679 //*****************************************************************************
3680 void
SysCtlNMIClear(uint32_t ui32Ints)3681 SysCtlNMIClear(uint32_t ui32Ints)
3682 {
3683     //
3684     // Clear the requested interrupt sources.
3685     //
3686     HWREG(SYSCTL_NMIC) &= ~ui32Ints;
3687 }
3688 
3689 //*****************************************************************************
3690 //
3691 //! Configures and enables or disables the clock output on the DIVSCLK pin.
3692 //!
3693 //! \param ui32Config holds the configuration options including enabling or
3694 //! disabling the clock output on the DIVSCLK pin.
3695 //! \param ui32Div is the divisor for the clock selected in the \e ui32Config
3696 //! parameter.
3697 //!
3698 //! This function selects the source for the DIVSCLK, enables or disables
3699 //! the clock output and provides an output divider value.  The \e ui32Div
3700 //! parameter specifies the divider for the selected clock source and has a
3701 //! valid range of 1-256.  The \e ui32Config parameter configures
3702 //! the DIVSCLK output based on the following settings:
3703 //!
3704 //! The first setting allows the output to be enabled or disabled.
3705 //! - \b SYSCTL_CLKOUT_EN - enable the DIVSCLK output.
3706 //! - \b SYSCTL_CLKOUT_DIS - disable the DIVSCLK output (default).
3707 //!
3708 //! The next group of settings selects the source for the DIVSCLK.
3709 //! - \b SYSCTL_CLKOUT_SYSCLK - use the current system clock as the
3710 //!      source (default).
3711 //! - \b SYSCTL_CLKOUT_PIOSC - use the PIOSC as the source.
3712 //! - \b SYSCTL_CLKOUT_MOSC - use the MOSC as the source.
3713 //!
3714 //! \b Example: Enable the PIOSC divided by 4 as the DIVSCLK output.
3715 //!
3716 //! \verbatim
3717 //!
3718 //! //
3719 //! // Enable the PIOSC divided by 4 as the DIVSCLK output.
3720 //! //
3721 //! SysCtlClockOutConfig(SYSCTL_DIVSCLK_EN | SYSCTL_DIVSCLK_SRC_PIOSC, 4);
3722 //! \endverbatim
3723 //!
3724 //! \note The availability of the DIVSCLK output varies with the Tiva part
3725 //! in use.  Please consult the data sheet for the part you are using to
3726 //! determine which interrupt sources are available.
3727 //!
3728 //! \return None.
3729 //
3730 //*****************************************************************************
3731 void
SysCtlClockOutConfig(uint32_t ui32Config,uint32_t ui32Div)3732 SysCtlClockOutConfig(uint32_t ui32Config, uint32_t ui32Div)
3733 {
3734     ASSERT(ui32Div != 0);
3735     ASSERT((ui32Config & ~(SYSCTL_CLKOUT_EN | SYSCTL_CLKOUT_DIS |
3736                            SYSCTL_CLKOUT_SYSCLK | SYSCTL_CLKOUT_PIOSC |
3737                            SYSCTL_CLKOUT_MOSC)) == 0);
3738 
3739     //
3740     // Set the requested configuration and divisor.
3741     //
3742     HWREG(SYSCTL_DIVSCLK) = ui32Config | ((ui32Div - 1) &
3743                                           SYSCTL_DIVSCLK_DIV_M);
3744 }
3745 
3746 //*****************************************************************************
3747 //
3748 //! Configures the alternate peripheral clock source.
3749 //!
3750 //! \param ui32Config holds the configuration options for the alternate
3751 //! peripheral clock.
3752 //!
3753 //! This function configures the alternate peripheral clock.  The alternate
3754 //! peripheral clock is used to provide a known clock in all operating modes
3755 //! to peripherals that support using the alternate peripheral clock as an
3756 //! input clock.  The \e ui32Config parameter value provides the clock input
3757 //! source using one of the following values:
3758 //! - \b SYSCTL_ALTCLK_PIOSC - use the PIOSC as the alternate clock
3759 //!      source (default).
3760 //! - \b SYSCTL_ALTCLK_RTCOSC - use the Hibernate module RTC clock as the
3761 //!      alternate clock source.
3762 //! - \b SYSCTL_ALTCLK_LFIOSC - use the low-frequency internal oscillator as
3763 //!      the alternate clock source.
3764 //!
3765 //! \b Example: Select the Hibernate module RTC clock as the alternate clock
3766 //! source.
3767 //!
3768 //! \verbatim
3769 //!
3770 //! //
3771 //! // Select the Hibernate module RTC clock as the alternate clock source.
3772 //! //
3773 //! SysCtlAltClkConfig(SYSCTL_ALTCLK_RTCOSC);
3774 //! \endverbatim
3775 //!
3776 //! \note The availability of the alternate peripheral clock varies with the
3777 //! Tiva part in use.  Please consult the data sheet for the part you are
3778 //! using to determine which interrupt sources are available.
3779 //!
3780 //! \return None.
3781 //
3782 //*****************************************************************************
3783 void
SysCtlAltClkConfig(uint32_t ui32Config)3784 SysCtlAltClkConfig(uint32_t ui32Config)
3785 {
3786     //
3787     // Set the requested configuration and divisor.
3788     //
3789     HWREG(SYSCTL_ALTCLKCFG) = ui32Config;
3790 }
3791 
3792 //*****************************************************************************
3793 //
3794 // Close the Doxygen group.
3795 //! @}
3796 //
3797 //*****************************************************************************
3798