• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2021 Huawei Device Co., Ltd. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification,
5  * are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice, this list of
8  * conditions and the following disclaimer.
9  *
10  * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11  * of conditions and the following disclaimer in the documentation and/or other materials
12  * provided with the distribution.
13  *
14  * 3. Neither the name of the copyright holder nor the names of its contributors may be used
15  * to endorse or promote products derived from this software without specific prior written
16  * permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include "osTest.h"
32 #include "It_los_pm.h"
33 #include "los_timer.h"
34 #include "los_sched.h"
35 
36 #define myprintf printf
37 #define TEST_LOOP 5
38 static EVENT_CB_S g_pmTestEvent;
39 static UINT32 g_taskID1, g_taskID2;
40 static UINT32 g_deviceCount = 0;
41 static UINT32 g_sysCount = 0;
42 static volatile UINT32 g_pmTestCount = 0;
43 
DeviceSuspend(UINT32 mode)44 static UINT32 DeviceSuspend(UINT32 mode)
45 {
46     g_deviceCount++;
47     g_testCount++;
48     return LOS_OK;
49 }
50 
DeviceResume(UINT32 mode)51 static VOID DeviceResume(UINT32 mode)
52 {
53     g_deviceCount--;
54     return;
55 }
56 
57 static LosPmDevice g_device = {
58     .suspend = DeviceSuspend,
59     .resume = DeviceResume,
60 };
61 
SysResume(VOID)62 static VOID SysResume(VOID)
63 {
64     if (g_sysCount != (UINT32)-1) {
65         g_sysCount--;
66     }
67 }
68 
SysSuspend(VOID)69 static UINT32 SysSuspend(VOID)
70 {
71     g_testCount++;
72     g_sysCount++;
73 
74     if ((g_deviceCount != 1) || (g_sysCount != 1)) { /* 1: sys count */
75         g_sysCount = (UINT32)-1;
76     }
77 
78     UINT64 timeout = LOS_SchedTickTimeoutNsGet();
79     printf("pm timeout : %u ns -> %u ticks\n", (UINT32)timeout, (UINT32)(timeout / OS_NS_PER_TICK));
80     return ArchEnterSleep();
81 }
82 
SystemPmEarly(UINT32 mode)83 static UINT32 SystemPmEarly(UINT32 mode)
84 {
85     UINT32 ret;
86 
87     ret = LOS_TaskSuspend(g_taskID2);
88     ICUNIT_ASSERT_EQUAL(ret, LOS_OK, ret);
89 
90     ret = LOS_TaskSuspend(g_taskID1);
91     if (ret != LOS_OK) {
92         (VOID)LOS_TaskResume(g_taskID2);
93     }
94     ICUNIT_ASSERT_EQUAL(ret, LOS_OK, ret);
95 
96     return LOS_OK;
97 }
98 
SystemPmLate(UINT32 mode)99 static VOID SystemPmLate(UINT32 mode)
100 {
101     UINT32 ret;
102 
103     ICUNIT_ASSERT_EQUAL_VOID(mode, LOS_SYS_LIGHT_SLEEP, mode);
104 
105     ret = LOS_TaskResume(g_taskID2);
106     ICUNIT_ASSERT_EQUAL_VOID(ret, LOS_OK, ret);
107 
108     ret = LOS_TaskResume(g_taskID1);
109     ICUNIT_ASSERT_EQUAL_VOID(ret, LOS_OK, ret);
110 }
111 
112 static LosPmSysctrl g_sysctrl = {
113     .early = SystemPmEarly,
114     .late = SystemPmLate,
115     .normalSuspend = ArchEnterSleep,
116     .normalResume = NULL,
117     .lightSuspend = SysSuspend,
118     .lightResume = SysResume,
119 };
120 
121 #define TEST_FLAGS 100
PmTestTask(VOID)122 static VOID PmTestTask(VOID)
123 {
124     UINT32 ret;
125     UINT32 wakeCount;
126 
127     while (1) {
128         wakeCount = LOS_PmReadLock();
129 
130         ret = LOS_PmSuspend(wakeCount);
131         ICUNIT_GOTO_EQUAL(ret, LOS_OK, ret, EXIT);
132         ICUNIT_GOTO_NOT_EQUAL(g_testCount, 0, g_sysCount, EXIT);
133         ICUNIT_GOTO_EQUAL(g_sysCount, 0, g_sysCount, EXIT);
134 
135         g_pmTestCount++;
136         if (g_pmTestCount > TEST_LOOP) {
137             break;
138         }
139         printf("PmTestTask loop: %u\n", g_pmTestCount);
140     }
141 
142 EXIT:
143     g_pmTestCount = TEST_FLAGS;
144     (VOID)LOS_EventWrite(&g_pmTestEvent, 0x1); /* 0x1: test exit evnet */
145     return;
146 }
147 
148 #define TEST_TASK1_LOOP 10
149 static volatile UINT32 g_testSample1Count, g_testSample2Count;
TaskSampleEntry2(void)150 static void TaskSampleEntry2(void)
151 {
152     UINT32 g_testSample2Count = TEST_FLAGS;
153 
154     while (1) {
155         if (g_testSample2Count == TEST_FLAGS) {
156             g_testSample2Count = 0;
157             (VOID)LOS_PmLockRequest("TaskSampleEntry2");
158             myprintf("%s request pm lock\n", __FUNCTION__);
159         }
160 
161         myprintf("TaskSampleEntry2 running...count: %u\n\r", g_testSample2Count);
162         (VOID)LOS_TaskDelay(20); /* sleep 20 ticks */
163 
164         if (g_testSample2Count <= TEST_TASK1_LOOP) { /* */
165             g_testSample2Count++;
166         }
167         if (g_testSample2Count == TEST_TASK1_LOOP) {
168             LOS_PmLockRelease("TaskSampleEntry2");
169             myprintf("%s release pm lock\n", __FUNCTION__);
170         }
171 
172         if (g_pmTestCount > TEST_LOOP) {
173             break;
174         }
175     }
176 
177     (VOID)LOS_PmLockRelease("TaskSampleEntry2");
178     myprintf("TaskSampleEntry2 exit\n");
179 }
180 
TaskSampleEntry1(void)181 static void TaskSampleEntry1(void)
182 {
183     UINT32 g_testSample1Count = 0;
184 
185     while (1) {
186         if (g_testSample1Count == 0) {
187             (VOID)LOS_PmLockRequest("TaskSampleEntry1");
188             myprintf("%s request pm lock\n", __FUNCTION__);
189         }
190 
191         myprintf("TaskSampleEntry1 running...%u\n\r", g_testSample1Count);
192         LOS_TaskDelay(50); /* sleep 50 ticks */
193 
194         g_testSample1Count++;
195         if (g_testSample1Count == TEST_TASK1_LOOP) {
196             LOS_PmLockRelease("TaskSampleEntry1");
197             myprintf("%s release pm lock\n", __FUNCTION__);
198         } else if (g_testSample1Count == (TEST_TASK1_LOOP + 1)) { /* 1:  incremental */
199             g_testSample1Count = 0;
200             g_testSample2Count = TEST_FLAGS;
201         }
202 
203         if (g_pmTestCount > TEST_LOOP) {
204             break;
205         }
206     }
207 
208     (VOID)LOS_PmLockRelease("TaskSampleEntry1");
209     myprintf("TaskSampleEntry1 exit\n");
210 }
211 
TestCase(VOID)212 static UINT32 TestCase(VOID)
213 {
214     UINT32 ret;
215     TSK_INIT_PARAM_S task1 = { 0 };
216     g_sysCount = 0;
217     g_deviceCount = 0;
218     g_testCount = 0;
219 
220     ret = LOS_EventInit(&g_pmTestEvent);
221     ICUNIT_ASSERT_EQUAL(ret, LOS_OK, ret);
222 
223     ret = LOS_PmRegister(LOS_PM_TYPE_DEVICE, &g_device);
224     ICUNIT_ASSERT_EQUAL(ret, LOS_OK, ret);
225 
226     ret = LOS_PmRegister(LOS_PM_TYPE_SYSCTRL, &g_sysctrl);
227     ICUNIT_ASSERT_EQUAL(ret, LOS_OK, ret);
228 
229     ret = LOS_PmModeSet(LOS_SYS_LIGHT_SLEEP);
230     ICUNIT_ASSERT_EQUAL(ret, LOS_OK, ret);
231 
232     task1.pfnTaskEntry = (TSK_ENTRY_FUNC)PmTestTask;
233     task1.uwStackSize = 0x2000; /* 0x2000 pm task stack size */
234     task1.pcName = "pmTask";
235     task1.usTaskPrio = 5; /* 5: pm task prio */
236     ret = LOS_TaskCreate(&g_testTaskID01, &task1);
237     ICUNIT_ASSERT_EQUAL(ret, LOS_OK, ret);
238 
239     task1.pfnTaskEntry = (TSK_ENTRY_FUNC)TaskSampleEntry1;
240     task1.uwStackSize  = 0x1000; /* 0x1000 task stack size */
241     task1.pcName       = "TaskSampleEntry1";
242     task1.usTaskPrio   = 10; /* 10: task prio */
243     ret = LOS_TaskCreate(&g_taskID1, &task1);
244     ICUNIT_ASSERT_EQUAL(ret, LOS_OK, ret);
245 
246     task1.pfnTaskEntry = (TSK_ENTRY_FUNC)TaskSampleEntry2;
247     task1.uwStackSize  = 0x1000; /* 0x1000 task stack size */
248     task1.pcName       = "TaskSampleEntry2";
249     task1.usTaskPrio   = 12; /* 12: task prio */
250     ret = LOS_TaskCreate(&g_taskID2, &task1);
251     ICUNIT_ASSERT_EQUAL(ret, LOS_OK, ret);
252 
253     (VOID)LOS_EventRead(&g_pmTestEvent, 0xff, LOS_WAITMODE_OR | LOS_WAITMODE_CLR, LOS_WAIT_FOREVER);
254 
255     ret = LOS_PmUnregister(LOS_PM_TYPE_DEVICE, &g_device);
256     ICUNIT_ASSERT_EQUAL(ret, LOS_OK, ret);
257 
258     ret = LOS_PmModeSet(LOS_SYS_NORMAL_SLEEP);
259     ICUNIT_ASSERT_EQUAL(ret, LOS_OK, ret);
260 
261     (VOID)LOS_TaskDelete(g_taskID1);
262     (VOID)LOS_TaskDelete(g_taskID2);
263     (VOID)LOS_TaskDelete(g_testTaskID01);
264     return LOS_OK;
265 }
266 
ItLosPm002(VOID)267 VOID ItLosPm002(VOID) // IT_Layer_ModuleORFeature_No
268 {
269     TEST_ADD_CASE("ItLosPm002", TestCase, TEST_LOS, TEST_TASK, TEST_LEVEL0, TEST_FUNCTION);
270 }
271