forked from zephyrproject-rtos/zephyr
/
test_driver.c
81 lines (58 loc) · 1.45 KB
/
test_driver.c
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
/*
* Copyright (c) 2021 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "test_driver.h"
#include <zephyr/kernel.h>
#include <zephyr/pm/device.h>
struct test_driver_data {
size_t count;
bool ongoing;
bool async;
struct k_sem sync;
};
static int test_driver_action(const struct device *dev,
enum pm_device_action action)
{
struct test_driver_data *data = dev->data;
data->ongoing = true;
if (data->async) {
k_sem_take(&data->sync, K_FOREVER);
data->async = false;
}
data->ongoing = false;
data->count++;
return 0;
}
void test_driver_pm_async(const struct device *dev)
{
struct test_driver_data *data = dev->data;
data->async = true;
}
void test_driver_pm_done(const struct device *dev)
{
struct test_driver_data *data = dev->data;
k_sem_give(&data->sync);
}
bool test_driver_pm_ongoing(const struct device *dev)
{
struct test_driver_data *data = dev->data;
return data->ongoing;
}
size_t test_driver_pm_count(const struct device *dev)
{
struct test_driver_data *data = dev->data;
return data->count;
}
int test_driver_init(const struct device *dev)
{
struct test_driver_data *data = dev->data;
k_sem_init(&data->sync, 0, 1);
return 0;
}
PM_DEVICE_DEFINE(test_driver, test_driver_action);
static struct test_driver_data data;
DEVICE_DEFINE(test_driver, "test_driver", &test_driver_init,
PM_DEVICE_GET(test_driver), &data, NULL, POST_KERNEL,
CONFIG_KERNEL_INIT_PRIORITY_DEVICE, NULL);