summaryrefslogtreecommitdiff
path: root/examples/imx7_colibri_m4/demo_apps/low_power_imx7d/rand_wfi/main.c
blob: 3c75c469d72dc54b10d20baf1d5b0f60d1eecf68 (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
/*
 * Copyright (c) 2015, Freescale Semiconductor, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * o Redistributions of source code must retain the above copyright notice, this list
 *   of conditions and the following disclaimer.
 *
 * o Redistributions in binary form must reproduce the above copyright notice, this
 *   list of conditions and the following disclaimer in the documentation and/or
 *   other materials provided with the distribution.
 *
 * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
 *   contributors may be used to endorse or promote products derived from this
 *   software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

///////////////////////////////////////////////////////////////////////////////
//  Includes
///////////////////////////////////////////////////////////////////////////////
#include "FreeRTOS.h"
#include "stdlib.h"
#include "task.h"
#include "board.h"
#include "debug_console_imx.h"
#include "gpt_timer.h"
#include "lpm_mcore.h"
#if defined(__GNUC__)
#include <errno.h>
#include <stdio.h>
#endif

///////////////////////////////////////////////////////////////////////////////
//  Macros
///////////////////////////////////////////////////////////////////////////////
#define PERIOD_MIN                      5
#define PERIOD_MAX                      10

////////////////////////////////////////////////////////////////////////////////
// Code
////////////////////////////////////////////////////////////////////////////////

/*!
 * @brief Set random time gpt timer to wakeup the system
 */
static uint32_t gpt_set_ramdom_period(uint32_t min_s, uint32_t max_s)
{
    uint32_t delay_s, delta_s;
    /*Generate the random number*/
    if (max_s <= min_s)
        delay_s = min_s;
    else {
        delta_s = max_s - min_s;
        delay_s = rand() % delta_s + min_s;
    }

    /*Set the GPT with delay_s, Taks will blocks until GPT event happens*/
    GPT_Set_Timer_Delay(1000 * delay_s);
    return delay_s;
}

/*!
 * @brief the main power mode cycling task
 */
void LowPowerTask(void *pvParameters)
{
    uint32_t elapsed_time, total_time;
    char control_char;

    PRINTF("\r\nLow Power Demo\r\n");

    /*
     * Wait For A7 Side Become Ready
     */
    PRINTF("********************************\r\n");
    PRINTF("Please wait :\r\n");
    PRINTF("    1) A7 peer is ready\r\n");
    PRINTF("Then press \"S\" to start the demo\r\n");
    PRINTF("********************************\r\n");

    for (;;) {
        PRINTF("\r\nPress \"S\" to start the demo : ");
        control_char = GETCHAR();
        PRINTF("%c", control_char);
        if ((control_char == 's') || (control_char == 'S')) {
            break;
        }
    }
    PRINTF("\r\n");

    while (!LPM_MCORE_CheckPeerReady()) {
        /*
         * Note, when vTaskDelay is called, idle task hook function
         * get opportunity to run
         */
        vTaskDelay(5);
    }

    LPM_MCORE_RegisterWakeupInterrupt(BOARD_GPC_BASEADDR, BOARD_GPTA_IRQ_NUM, GPC_IRQ_WAKEUP_ENABLE);

    LPM_MCORE_SetSelfReady();

    total_time = 0;
    while(1)
    {
        switch (LPM_MCORE_GetPowerStatus(BOARD_GPC_BASEADDR)) {
            case LPM_M4_STATE_RUN:
                /*---> WAIT*/
                LPM_MCORE_ChangeM4Clock(LPM_M4_LOW_FREQ);
                LPM_MCORE_SetPowerStatus(BOARD_GPC_BASEADDR, LPM_M4_STATE_WAIT);
                break;
            case LPM_M4_STATE_WAIT:
                /*---> STOP*/
                LPM_MCORE_ChangeM4Clock(LPM_M4_LOW_FREQ);
                LPM_MCORE_SetPowerStatus(BOARD_GPC_BASEADDR, LPM_M4_STATE_STOP);
                break;
            case LPM_M4_STATE_STOP:
                /*---> RUN*/
                LPM_MCORE_ChangeM4Clock(LPM_M4_HIGH_FREQ);
                LPM_MCORE_SetPowerStatus(BOARD_GPC_BASEADDR, LPM_M4_STATE_RUN);
                break;
            default:
                break;
        }

        elapsed_time = gpt_set_ramdom_period(PERIOD_MIN, PERIOD_MAX);
        PRINTF("GPT will triggle interrupt in %ds\r\n", elapsed_time);

        PRINTF("go to mode %s\r\n", LPM_MCORE_GetPowerStatusString());

        /*wait GPT interrupt handler getting executed*/
        GPT_Wait_Timer_Expire();
        total_time += elapsed_time;
        PRINTF("GPT Event! Total time %ds\r\n", total_time);
    }
}

/*!
 * @brief Custom function to be run in idletask
 */
void vApplicationIdleHook(void)
{
    /* Waiting for Wake up event. */
    LPM_MCORE_WaitForInt();
}

/*!
 * @brief Main function
 */
int main(void)
{
    // Initialize demo application pins setting and clock setting.
    hardware_init();

    PRINTF(" ************************************************************************\r\n");
    PRINTF(" *          i.MX 7Dual Dual Core Low Power Demo - M4 side               *\r\n");
    PRINTF(" *                                                                      *\r\n");
    PRINTF(" *        A GPT will change the M4 Power Mode with random period        *\r\n");
    PRINTF(" *                                                                      *\r\n");
    PRINTF(" ************************************************************************\r\n");

    // Init the GPT Timer
    GPT_Timer_Init();

    // Low Power Management Initialization
    LPM_MCORE_Init(BOARD_GPC_BASEADDR);

    // Create a demo task which will demo M4 core cycling through different power modes.
    xTaskCreate(LowPowerTask, "Low Power Task", configMINIMAL_STACK_SIZE,           // xTaskGenericCreate
                NULL, tskIDLE_PRIORITY+1, NULL);

    // Start FreeRTOS scheduler.
    vTaskStartScheduler();

    // Should never reach this point.
    while (true);
}

#if defined(__GNUC__)
/*!
 * @brief Function to override ARMGCC default function _sbrk
 *
 * _sbrk is called by malloc. ARMGCC default _sbrk compares "SP" register and
 * heap end, if heap end is larger than "SP", then _sbrk returns error and
 * memory allocation failed. This function changes to compare __HeapLimit with
 * heap end.
 *
 * Then rand() function used in this project will call malloc in GCC compiler,
 * so the customized version of _sbrk is needed here
 */
caddr_t _sbrk(int incr)
{
    extern uint32_t end __asm("end");
    extern uint32_t heap_limit __asm("__HeapLimit");
    static uint32_t *heap_end;
    char *prev_heap_end;

    if (heap_end == NULL)
        heap_end = &end;

    prev_heap_end = (char*)heap_end;

    if (heap_end + incr > &heap_limit)
    {
        errno = ENOMEM;
        return (caddr_t)-1;
    }

    heap_end += incr;

    return (caddr_t)prev_heap_end;
}
#endif
/*******************************************************************************
 * EOF
 ******************************************************************************/