forked from zephyrproject-rtos/zephyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
adc_context.h
291 lines (246 loc) · 7.6 KB
/
adc_context.h
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
/*
* Copyright (c) 2018 Nordic Semiconductor ASA
* Copyright (c) 2017 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef ZEPHYR_DRIVERS_ADC_ADC_CONTEXT_H_
#define ZEPHYR_DRIVERS_ADC_ADC_CONTEXT_H_
#include <zephyr/drivers/adc.h>
#include <zephyr/sys/atomic.h>
#ifdef __cplusplus
extern "C" {
#endif
struct adc_context;
/*
* Each driver should provide implementations of the following two functions:
* - adc_context_start_sampling() that will be called when a sampling (of one
* or more channels, depending on the realized sequence) is to be started
* - adc_context_update_buffer_pointer() that will be called when the sample
* buffer pointer should be prepared for writing of next sampling results,
* the "repeat_sampling" parameter indicates if the results should be written
* in the same place as before (when true) or as consecutive ones (otherwise).
*/
static void adc_context_start_sampling(struct adc_context *ctx);
static void adc_context_update_buffer_pointer(struct adc_context *ctx,
bool repeat_sampling);
/*
* If a given driver uses some dedicated hardware timer to trigger consecutive
* samplings, it should implement also the following two functions. Otherwise,
* it should define the ADC_CONTEXT_USES_KERNEL_TIMER macro to enable parts of
* this module that utilize a standard kernel timer.
*/
static void adc_context_enable_timer(struct adc_context *ctx);
static void adc_context_disable_timer(struct adc_context *ctx);
/*
* If a driver needs to do something after a context complete then
* then this optional function can be overwritten. This will be called
* after a sequence has ended, and *not* when restarted with ADC_ACTION_REPEAT.
* To enable this function define ADC_CONTEXT_ENABLE_ON_COMPLETE.
*/
#ifdef ADC_CONTEXT_ENABLE_ON_COMPLETE
static void adc_context_on_complete(struct adc_context *ctx, int status);
#endif /* ADC_CONTEXT_ENABLE_ON_COMPLETE */
struct adc_context {
atomic_t sampling_requested;
#ifdef ADC_CONTEXT_USES_KERNEL_TIMER
struct k_timer timer;
#endif /* ADC_CONTEXT_USES_KERNEL_TIMER */
struct k_sem lock;
struct k_sem sync;
int status;
#ifdef CONFIG_ADC_ASYNC
struct k_poll_signal *signal;
bool asynchronous;
#endif /* CONFIG_ADC_ASYNC */
struct adc_sequence sequence;
struct adc_sequence_options options;
uint16_t sampling_index;
};
#ifdef ADC_CONTEXT_USES_KERNEL_TIMER
#define ADC_CONTEXT_INIT_TIMER(_data, _ctx_name) \
._ctx_name.timer = Z_TIMER_INITIALIZER(_data._ctx_name.timer, \
adc_context_on_timer_expired, \
NULL)
#endif /* ADC_CONTEXT_USES_KERNEL_TIMER */
#define ADC_CONTEXT_INIT_LOCK(_data, _ctx_name) \
._ctx_name.lock = Z_SEM_INITIALIZER(_data._ctx_name.lock, 0, 1)
#define ADC_CONTEXT_INIT_SYNC(_data, _ctx_name) \
._ctx_name.sync = Z_SEM_INITIALIZER(_data._ctx_name.sync, 0, 1)
#ifdef ADC_CONTEXT_USES_KERNEL_TIMER
static void adc_context_on_timer_expired(struct k_timer *timer_id);
#endif
static inline void adc_context_init(struct adc_context *ctx)
{
#ifdef ADC_CONTEXT_USES_KERNEL_TIMER
k_timer_init(&ctx->timer, adc_context_on_timer_expired, NULL);
#endif
k_sem_init(&ctx->lock, 0, 1);
k_sem_init(&ctx->sync, 0, 1);
}
static inline void adc_context_request_next_sampling(struct adc_context *ctx)
{
if (atomic_inc(&ctx->sampling_requested) == 0) {
adc_context_start_sampling(ctx);
} else {
/*
* If a sampling was already requested and was not finished yet,
* do not start another one from here, this will be done from
* adc_context_on_sampling_done() after the current sampling is
* complete. Instead, note this fact, and inform the user about
* it after the sequence is done.
*/
ctx->status = -EBUSY;
}
}
#ifdef ADC_CONTEXT_USES_KERNEL_TIMER
static inline void adc_context_enable_timer(struct adc_context *ctx)
{
k_timer_start(&ctx->timer, K_NO_WAIT, K_USEC(ctx->options.interval_us));
}
static inline void adc_context_disable_timer(struct adc_context *ctx)
{
k_timer_stop(&ctx->timer);
}
static void adc_context_on_timer_expired(struct k_timer *timer_id)
{
struct adc_context *ctx =
CONTAINER_OF(timer_id, struct adc_context, timer);
adc_context_request_next_sampling(ctx);
}
#endif /* ADC_CONTEXT_USES_KERNEL_TIMER */
static inline void adc_context_lock(struct adc_context *ctx,
bool asynchronous,
struct k_poll_signal *signal)
{
k_sem_take(&ctx->lock, K_FOREVER);
#ifdef CONFIG_ADC_ASYNC
ctx->asynchronous = asynchronous;
ctx->signal = signal;
#endif /* CONFIG_ADC_ASYNC */
}
static inline void adc_context_release(struct adc_context *ctx, int status)
{
#ifdef CONFIG_ADC_ASYNC
if (ctx->asynchronous && (status == 0)) {
return;
}
#endif /* CONFIG_ADC_ASYNC */
k_sem_give(&ctx->lock);
}
static inline void adc_context_unlock_unconditionally(struct adc_context *ctx)
{
if (!k_sem_count_get(&ctx->lock)) {
k_sem_give(&ctx->lock);
}
}
static inline int adc_context_wait_for_completion(struct adc_context *ctx)
{
#ifdef CONFIG_ADC_ASYNC
if (ctx->asynchronous) {
return 0;
}
#endif /* CONFIG_ADC_ASYNC */
k_sem_take(&ctx->sync, K_FOREVER);
return ctx->status;
}
static inline void adc_context_complete(struct adc_context *ctx, int status)
{
#ifdef ADC_CONTEXT_ENABLE_ON_COMPLETE
adc_context_on_complete(ctx, status);
#endif /* ADC_CONTEXT_ENABLE_ON_COMPLETE */
#ifdef CONFIG_ADC_ASYNC
if (ctx->asynchronous) {
if (ctx->signal) {
k_poll_signal_raise(ctx->signal, status);
}
k_sem_give(&ctx->lock);
return;
}
#endif /* CONFIG_ADC_ASYNC */
/*
* Override the status only when an error is signaled to this function.
* Please note that adc_context_request_next_sampling() might have set
* this field.
*/
if (status != 0) {
ctx->status = status;
}
k_sem_give(&ctx->sync);
}
static inline void adc_context_start_read(struct adc_context *ctx,
const struct adc_sequence *sequence)
{
ctx->sequence = *sequence;
ctx->status = 0;
if (sequence->options) {
ctx->options = *sequence->options;
ctx->sequence.options = &ctx->options;
ctx->sampling_index = 0U;
if (ctx->options.interval_us != 0U) {
atomic_set(&ctx->sampling_requested, 0);
adc_context_enable_timer(ctx);
return;
}
}
adc_context_start_sampling(ctx);
}
/*
* This function should be called after a sampling (of one or more channels,
* depending on the realized sequence) is done. It calls the defined callback
* function if required and takes further actions accordingly.
*/
static inline void adc_context_on_sampling_done(struct adc_context *ctx,
const struct device *dev)
{
if (ctx->sequence.options) {
adc_sequence_callback callback = ctx->options.callback;
enum adc_action action;
bool finish = false;
bool repeat = false;
if (callback) {
action = callback(dev,
&ctx->sequence,
ctx->sampling_index);
} else {
action = ADC_ACTION_CONTINUE;
}
switch (action) {
case ADC_ACTION_REPEAT:
repeat = true;
break;
case ADC_ACTION_FINISH:
finish = true;
break;
default: /* ADC_ACTION_CONTINUE */
if (ctx->sampling_index <
ctx->options.extra_samplings) {
++ctx->sampling_index;
} else {
finish = true;
}
}
if (!finish) {
adc_context_update_buffer_pointer(ctx, repeat);
/*
* Immediately start the next sampling if working with
* a zero interval or if the timer expired again while
* the current sampling was in progress.
*/
if (ctx->options.interval_us == 0U) {
adc_context_start_sampling(ctx);
} else if (atomic_dec(&ctx->sampling_requested) > 1) {
adc_context_start_sampling(ctx);
}
return;
}
if (ctx->options.interval_us != 0U) {
adc_context_disable_timer(ctx);
}
}
adc_context_complete(ctx, 0);
}
#ifdef __cplusplus
}
#endif
#endif /* ZEPHYR_DRIVERS_ADC_ADC_CONTEXT_H_ */