summaryrefslogtreecommitdiff
path: root/boards/ek-tm4c1294xl/sleep_modes/sleep_modes.c
blob: 6a67a0ec836d548a7239e7b7428df0816334d13a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
//*****************************************************************************
//
// sleep_modes.c - Sleep modes example.
//
// Copyright (c) 2014 Texas Instruments Incorporated.  All rights reserved.
// Software License Agreement
//
//*****************************************************************************

#include <stdint.h>
#include <stdbool.h>
#include "inc/hw_ints.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "driverlib/debug.h"
#include "driverlib/fpu.h"
#include "driverlib/gpio.h"
#include "driverlib/interrupt.h"
#include "driverlib/pin_map.h"
#include "driverlib/pwm.h"
#include "driverlib/rom.h"
#include "driverlib/rom_map.h"
#include "driverlib/sysctl.h"
#include "driverlib/timer.h"
#include "driverlib/uart.h"
#include "utils/uartstdio.h"
#include "drivers/buttons.h"

//*****************************************************************************
//
//! \addtogroup example_list
//! <h1>Sleep Modes(sleep_modes)</h1>
//!
//! This example demonstrates the different power modes available on the Tiva
//! C Series devices. The user button (USR-SW1) is used to cycle through the
//! different power modes.
//! The SRAM, Flash, and LDO are all configured to a lower power setting for
//! the different modes.
//!
//! A timer is configured to toggle an LED in an ISR in both Run and Sleep
//! mode.
//! In Deep-Sleep the PWM is used to toggle the same LED in hardware. The three
//! remaining LEDs are used to indicate the current power mode.
//!
//!         LED key in addition to the toggling LED:
//!             3 LEDs on - Run Mode
//!             2 LEDs on - Sleep Mode
//!             1 LED on - Deep-Sleep Mode
//!
//! UART0, connected to the Virtual Serial Port and running at 115,200, 8-N-1,
//! is used to display messages from this application.
//
//*****************************************************************************

//****************************************************************************
//
// Status LED defines.
//
//****************************************************************************

//
// PF4
//
#define RUN_GPIO_SYSCTL     SYSCTL_PERIPH_GPIOF
#define RUN_GPIO_BASE       GPIO_PORTF_BASE
#define RUN_GPIO_PIN        GPIO_PIN_4

//
// PN0
//
#define SLEEP_GPIO_SYSCTL   SYSCTL_PERIPH_GPION
#define SLEEP_GPIO_BASE     GPIO_PORTN_BASE
#define SLEEP_GPIO_PIN      GPIO_PIN_0

//
// PN1
//
#define DSLEEP_GPIO_SYSCTL  SYSCTL_PERIPH_GPION
#define DSLEEP_GPIO_BASE    GPIO_PORTN_BASE
#define DSLEEP_GPIO_PIN     GPIO_PIN_1

//
// PF0
//
#define TOGGLE_GPIO_SYSCTL  SYSCTL_PERIPH_GPIOF
#define TOGGLE_GPIO_BASE    GPIO_PORTF_BASE
#define TOGGLE_GPIO_PIN     GPIO_PIN_0

//****************************************************************************
//
// System clock rate in Hz.
//
//****************************************************************************
uint32_t g_ui32SysClock;

//****************************************************************************
//
// Global to track power state:
// 0 - Run Mode
// 1 - Sleep Mode
// 2 - Deep-Sleep Mode
//
//****************************************************************************
volatile uint32_t g_ui32SleepMode = 0;

//*****************************************************************************
//
// The error routine that is called if the driver library encounters an error.
//
//*****************************************************************************
#ifdef DEBUG
void
__error__(char *pcFilename, uint32_t ui32Line)
{
}
#endif

//*****************************************************************************
//
// Configure the UART and its pins. This must be called before UARTprintf().
//
//*****************************************************************************
void
ConfigureUART(void)
{
    //
    // Enable the GPIO Peripheral used by the UART.
    //
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);

    //
    // Enable UART0.
    //
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);

    //
    // Configure GPIO Pins for UART mode.
    //
    ROM_GPIOPinConfigure(GPIO_PA0_U0RX);
    ROM_GPIOPinConfigure(GPIO_PA1_U0TX);
    ROM_GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);

    //
    // Initialize the UART for console I/O.
    //
    UARTStdioConfig(0, 115200, g_ui32SysClock);
}

//*****************************************************************************
//
// The function for setting the state of the LEDs for indicating the current
// power mode.
//
//*****************************************************************************
void
PowerLEDsSet(void)
{
    switch (g_ui32SleepMode)
    {
        case 0:

            //
            // Turn on LED(s) to indicate we are in Run Mode.
            //
            ROM_GPIOPinWrite(RUN_GPIO_BASE, RUN_GPIO_PIN, RUN_GPIO_PIN);
            ROM_GPIOPinWrite(SLEEP_GPIO_BASE, SLEEP_GPIO_PIN, SLEEP_GPIO_PIN);
            ROM_GPIOPinWrite(DSLEEP_GPIO_BASE, DSLEEP_GPIO_PIN,
                    DSLEEP_GPIO_PIN);

            break;

        case 1:

            //
            // Turn on LED(s) to indicate we are in Sleep Mode.
            //
            ROM_GPIOPinWrite(RUN_GPIO_BASE, RUN_GPIO_PIN, ~RUN_GPIO_PIN);
            ROM_GPIOPinWrite(SLEEP_GPIO_BASE, SLEEP_GPIO_PIN, SLEEP_GPIO_PIN);
            ROM_GPIOPinWrite(DSLEEP_GPIO_BASE, DSLEEP_GPIO_PIN,
                    DSLEEP_GPIO_PIN);

            break;

        case 2:

            //
            // Turn on LED(s) to indicate we are in Deep-Sleep Mode.
            //
            ROM_GPIOPinWrite(RUN_GPIO_BASE, RUN_GPIO_PIN, ~RUN_GPIO_PIN);
            ROM_GPIOPinWrite(SLEEP_GPIO_BASE, SLEEP_GPIO_PIN, ~SLEEP_GPIO_PIN);
            ROM_GPIOPinWrite(DSLEEP_GPIO_BASE, DSLEEP_GPIO_PIN,
                    DSLEEP_GPIO_PIN);

            break;

        default:
            break;
    }
}

//*****************************************************************************
//
// The interrupt handler for the button interrupt.
//
//*****************************************************************************
void
ButtonIntHandler(void)
{
    //
    // Delay here on button push for simple debouncing.
    //
    SysCtlDelay(g_ui32SysClock / 10);

    //
    // Clear the timer interrupt.
    //
    ROM_GPIOIntClear(GPIO_PORTJ_AHB_BASE, GPIO_INT_PIN_0);

    //
    // Increment Mode.
    //
    g_ui32SleepMode = (g_ui32SleepMode + 1) % 3;

    switch (g_ui32SleepMode)
    {
        //
        // Enter Run Mode.
        //
        case 0:

            //
            // Disable the PWM.
            //
            ROM_PWMGenDisable(PWM0_BASE, PWM_GEN_0);

            //
            // Configure Toggle LED as a GPIO output.
            //
            ROM_GPIOPinTypeGPIOOutput(TOGGLE_GPIO_BASE, TOGGLE_GPIO_PIN);

            //
            // Enable the timer.
            //
            ROM_TimerEnable(TIMER0_BASE, TIMER_A);

            //
            // Print mode over the UART.
            //
            UARTprintf("\033[100D");
            UARTprintf("\033[K");
            UARTprintf("Run\t\tMOSC with PLL\tTimer");
            SysCtlDelay(10000);
            break;

            //
            // Enter Sleep Mode.
            //
        case 1:

            //
            // Print mode over the UART.
            // Delay to let the UART finish before going to Sleep.
            //
            UARTprintf("\033[100D");
            UARTprintf("\033[K");
            UARTprintf("Sleep\t\tPIOSC\t\tTimer");
            SysCtlDelay(10000);

            //
            // Switch clock to PIOSC and power down the MOSC before going into 
            // Sleep.
            //
            g_ui32SysClock = MAP_SysCtlClockFreqSet((SYSCTL_OSC_INT |
                                                      SYSCTL_USE_OSC |
                                                      SYSCTL_MAIN_OSC_DIS), 
                                                      16000000);

            break;

            //
            // Enter Deep-Sleep Mode.
            //
        case 2:

            //
            // Switch back to the MOSC + PLL.
            //
            g_ui32SysClock = MAP_SysCtlClockFreqSet((SYSCTL_XTAL_25MHZ |
                                                      SYSCTL_OSC_MAIN |
                                                      SYSCTL_USE_PLL |
                                                      SYSCTL_CFG_VCO_320), 
                                                      16000000);

            //
            // Disable the timer.
            //
            ROM_TimerDisable(TIMER0_BASE, TIMER_A);

            //
            // Configure the toggle pin as a PWM pin.
            //
            ROM_GPIOPinConfigure(GPIO_PF0_M0PWM0);
            ROM_GPIOPinTypePWM(TOGGLE_GPIO_BASE, TOGGLE_GPIO_PIN);

            //
            // Enable the PWM.
            //
            ROM_PWMGenEnable(PWM0_BASE, PWM_GEN_0);

            //
            // Print mode over the UART.
            // Delay to let the UART finish before going to Sleep.
            //
            UARTprintf("\033[100D");
            UARTprintf("\033[K");
            UARTprintf("Deep-Sleep\tLFIOSC\t\tPWM");
            SysCtlDelay(10000);
            break;

        default:
            break;
    }

    //
    // Set LEDs to show what mode we are in.
    //
    PowerLEDsSet();
}


//*****************************************************************************
//
// The interrupt handler for the timer interrupt.
//
//*****************************************************************************
void
Timer0IntHandler(void)
{
    //
    // Clear the timer interrupt.
    //
    ROM_TimerIntClear(TIMER0_BASE, TIMER_TIMA_TIMEOUT);

    //
    // Toggle the LED.
    //
    ROM_GPIOPinWrite(TOGGLE_GPIO_BASE, TOGGLE_GPIO_PIN,
            (ROM_GPIOPinRead(TOGGLE_GPIO_BASE, TOGGLE_GPIO_PIN) ^
             TOGGLE_GPIO_PIN));
}

//*****************************************************************************
//
// This example application demonstrates the use of the different sleep modes
// and different power configuration options.
//
//*****************************************************************************
int
main(void)
{
    //
    // Set the clocking to run from the MOSC with the PLL at 16MHz.
    //
    g_ui32SysClock = MAP_SysCtlClockFreqSet((SYSCTL_XTAL_25MHZ |
                                              SYSCTL_OSC_MAIN |
                                              SYSCTL_USE_PLL |
                                              SYSCTL_CFG_VCO_320), 16000000);


    //
    // Set the clocking for Deep-Sleep.
    // Power down the PIOSC & MOSC to save power and run from the
    // internal 30kHz osc.
    //
    ROM_SysCtlDeepSleepClockConfigSet(1, (SYSCTL_DSLP_OSC_INT30 |
                SYSCTL_DSLP_PIOSC_PD | SYSCTL_DSLP_MOSC_PD));

    //
    // Initialize the UART and write the banner.
    // Indicate we are currently in Run Mode.
    //
    ConfigureUART();
    UARTprintf("\033[2J\033[H");
    UARTprintf("Sleep Modes example\n\n");
    UARTprintf("Mode:\t\tClock Source:\tLED Toggle Source:");
    UARTprintf("\nRun\t\tMOSC with PLL\tTimer");

    //
    // Initialize the buttons driver.
    //
    ButtonsInit();

    //
    // Enable the interrupt for the button.
    //
    ROM_GPIOIntEnable(GPIO_PORTJ_AHB_BASE, GPIO_INT_PIN_0);

    //
    // Enable the GPIO ports that are used for the on-board LEDs.
    //
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPION);
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);

    //
    // Set pad config.
    //
    MAP_GPIOPadConfigSet(GPIO_PORTJ_BASE, GPIO_PIN_0,
                          GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU);

    //
    // Set direction.
    //
    ROM_GPIODirModeSet(GPIO_PORTJ_BASE, GPIO_PIN_0, GPIO_DIR_MODE_IN);

    //
    // Enable the interrupt for the button.
    //
    ROM_GPIOIntEnable(GPIO_PORTJ_BASE, GPIO_INT_PIN_0);

    //
    // Enable interrupt to NVIC.
    //
    ROM_IntEnable(INT_GPIOJ);

    //
    // Enable the GPIO ports that are used for the on-board LEDs.
    //
    ROM_SysCtlPeripheralEnable(RUN_GPIO_SYSCTL);
    ROM_SysCtlPeripheralEnable(SLEEP_GPIO_SYSCTL);
    ROM_SysCtlPeripheralEnable(DSLEEP_GPIO_SYSCTL);
    ROM_SysCtlPeripheralEnable(TOGGLE_GPIO_SYSCTL);

    //
    // Enable the GPIO pins for the LED.
    //
    ROM_GPIOPinTypeGPIOOutput(RUN_GPIO_BASE, RUN_GPIO_PIN);
    ROM_GPIOPinTypeGPIOOutput(SLEEP_GPIO_BASE, SLEEP_GPIO_PIN);
    ROM_GPIOPinTypeGPIOOutput(DSLEEP_GPIO_BASE, DSLEEP_GPIO_PIN);
    ROM_GPIOPinTypeGPIOOutput(TOGGLE_GPIO_BASE, TOGGLE_GPIO_PIN);

    //
    // Enable the peripherals used by this example.
    //
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM0);

    //
    // Enable processor interrupts.
    //
    ROM_IntMasterEnable();

    //
    // Configure the 32-bit periodic timer.
    //
    ROM_TimerConfigure(TIMER0_BASE, TIMER_CFG_PERIODIC);
    ROM_TimerLoadSet(TIMER0_BASE, TIMER_A, g_ui32SysClock);

    //
    // Setup the interrupts for the timer timeout.
    //
    ROM_IntEnable(INT_TIMER0A);
    ROM_TimerIntEnable(TIMER0_BASE, TIMER_TIMA_TIMEOUT);

    //
    // Configure the PWM0 to count down without synchronization.
    // This will be used in Deep-Sleep.
    //
    ROM_PWMGenConfigure(PWM0_BASE, PWM_GEN_0,
                        PWM_GEN_MODE_DOWN | PWM_GEN_MODE_NO_SYNC);

    //
    // Enable the PWM0 output signal.
    //
    ROM_PWMOutputState(PWM0_BASE, PWM_OUT_0_BIT, true);

    //
    // Set up the period to match the timer.
    //
    ROM_PWMGenPeriodSet(PWM0_BASE, PWM_GEN_0, 65000);

    //
    // Configure the PWM for a 50% duty cycle.
    //
    ROM_PWMPulseWidthSet(PWM0_BASE, PWM_OUT_0, 65000 >> 1);

    //
    // Enable the timer.
    //
    ROM_TimerEnable(TIMER0_BASE, TIMER_A);

    //
    // Enable the Timer in Sleep Mode.
    //
    ROM_SysCtlPeripheralSleepEnable(SYSCTL_PERIPH_TIMER0);

    //
    // Enable the PWM in Deep-Sleep Mode.
    //
    ROM_SysCtlPeripheralDeepSleepEnable(SYSCTL_PERIPH_PWM0);

    //
    // Enable the Button Port in Sleep & Deep-Sleep Mode.
    //
    ROM_SysCtlPeripheralSleepEnable(SYSCTL_PERIPH_GPIOJ);
    ROM_SysCtlPeripheralDeepSleepEnable(SYSCTL_PERIPH_GPIOJ);

    //
    // Enable the LED Ports in Sleep & Deep-Sleep Mode.
    //
    ROM_SysCtlPeripheralSleepEnable(SLEEP_GPIO_SYSCTL);
    ROM_SysCtlPeripheralDeepSleepEnable(DSLEEP_GPIO_SYSCTL);
    ROM_SysCtlPeripheralSleepEnable(TOGGLE_GPIO_SYSCTL);
    ROM_SysCtlPeripheralDeepSleepEnable(TOGGLE_GPIO_SYSCTL);

    //
    // Enable Auto Clock Gating Control.
    //
    ROM_SysCtlPeripheralClockGating(true);

    //
    // Set LDO to 1.15V in Sleep.
    // Set LDO to 1.10V in Deep-Sleep.
    //
    SysCtlLDOSleepSet(SYSCTL_LDO_1_15V);
    SysCtlLDODeepSleepSet(SYSCTL_LDO_1_10V);

    //
    // Set SRAM to Standby when in Sleep Mode.
    // Set Flash & SRAM to Low Power in Deep-Sleep Mode.
    //
    SysCtlSleepPowerSet(SYSCTL_SRAM_STANDBY);
    SysCtlDeepSleepPowerSet(SYSCTL_FLASH_LOW_POWER | SYSCTL_SRAM_LOW_POWER);

    //
    // Call to set initial LED power state.
    //
    PowerLEDsSet();

    //
    // Loop forever.
    //
    while(1)
    {
        //
        // Handle going into the different sleep modes outside of
        // interrupt context.
        //
        if (g_ui32SleepMode == 1)
        {
            //
            // Go into Sleep Mode.
            //
            ROM_SysCtlSleep();
        }
        else if (g_ui32SleepMode == 2)
        {
            //
            // Go into Deep-Sleep Mode.
            //
            ROM_SysCtlDeepSleep();
        }
    }
}