example_detector_distance_low_power_hibernate.c
Go to the documentation of this file.
1 // Copyright (c) Acconeer AB, 2023
2 // All rights reserved
3 // This file is subject to the terms and conditions defined in the file
4 // 'LICENSES/license_acconeer.txt', (BSD 3-Clause License) which is part
5 // of this source code package.
6
7 #include <stdbool.h>
8 #include <stdint.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11
12
13 #include "acc_definitions_a121.h"
14 #include "acc_definitions_common.h"
15 #include "acc_detector_distance.h"
16 #include "acc_hal_definitions_a121.h"
17 #include "acc_hal_integration_a121.h"
18 #include "acc_integration.h"
19 #include "acc_integration_log.h"
20 #include "acc_rss_a121.h"
21 #include "acc_sensor.h"
22
23 #include "acc_version.h"
24
25
26 /** \example example_detector_distance_low_power_hibernate.c
27 * @brief This is an example on how to put the sensor in hibernation and the system in a low power state between measurements
28 * @n
29 * The example executes as follows:
30 * - Create a distance configuration
31 * - Create a sensor instance
32 * - Enable sensor
33 * - Create a detector instance
34 * - Calibrate the sensor
35 * - Calibrate the detector
36 * - Enter hibernation
37 * - Loop forever:
38 * - Exit hibernation
39 * - Prepare the detector
40 * - Perform a sensor measurement and read out the data
41 * - Enter hibernation
42 * - Process the measurement and get detector result
43 * - Put the system in deep sleep for a specified amount of time
44 * - Destroy the configuration
45 * - Destroy the detector instance
46 * - Destroy the sensor instance
47 */
48
49 #define SENSOR_ID (1U)
50 #define SENSOR_TIMEOUT_MS (1000U)
51 #define DEFAULT_UPDATE_RATE (1.0f)
52
53
54 static void print_result(acc_detector_distance_result_t *result);
55
56
57 static void cleanup(acc_detector_distance_handle_t *distance_handle,
58 acc_detector_distance_config_t *distance_config,
59 acc_sensor_t *sensor,
60 void *buffer,
61 void *calibration_buffer);
62
63
64 static void set_config(acc_detector_distance_config_t *distance_config);
65
66
67 int acconeer_main(int argc, char *argv[]);
68
69
70 int acconeer_main(int argc, char *argv[])
71 {
72 (void)argc;
73 (void)argv;
74 acc_detector_distance_config_t *distance_config = NULL;
75 acc_detector_distance_handle_t *distance_handle = NULL;
76 acc_sensor_t *sensor = NULL;
77 void *buffer = NULL;
78 void *calibration_buffer = NULL;
79 uint32_t buffer_size = 0U;
80 uint32_t calibration_buffer_size = 0U;
81
82 printf("Acconeer software version %s\n", acc_version_get());
83
84 const acc_hal_a121_t *hal = acc_hal_rss_integration_get_implementation();
85
86 if (!acc_rss_hal_register(hal))
87 {
88 return EXIT_FAILURE;
89 }
90
91 distance_config = acc_detector_distance_config_create();
92 if (distance_config == NULL)
93 {
94 printf("acc_detector_distance_config_create() failed\n");
95 cleanup(distance_handle, distance_config, sensor, buffer, calibration_buffer);
96 return EXIT_FAILURE;
97 }
98
99 set_config(distance_config);
100
101 uint32_t sleep_time_ms = (uint32_t)(1000.0f / DEFAULT_UPDATE_RATE);
102
103 acc_integration_set_periodic_wakeup(sleep_time_ms);
104
105 distance_handle = acc_detector_distance_create(distance_config);
106 if (distance_handle == NULL)
107 {
108 printf("acc_detector_distance_create() failed\n");
109 cleanup(distance_handle, distance_config, sensor, buffer, calibration_buffer);
110 return EXIT_FAILURE;
111 }
112
113 if (!acc_detector_distance_get_buffer_sizes(distance_handle, &buffer_size, &calibration_buffer_size))
114 {
115 printf("acc_detector_distance_get_buffer_size() failed\n");
116 cleanup(distance_handle, distance_config, sensor, buffer, calibration_buffer);
117 return EXIT_FAILURE;
118 }
119
120 buffer = acc_integration_mem_alloc(buffer_size);
121 if (buffer == NULL)
122 {
123 printf("buffer allocation failed\n");
124 cleanup(distance_handle, distance_config, sensor, buffer, calibration_buffer);
125 return EXIT_FAILURE;
126 }
127
128 calibration_buffer = acc_integration_mem_alloc(calibration_buffer_size);
129 if (calibration_buffer == NULL)
130 {
131 printf("buffer allocation failed\n");
132 cleanup(distance_handle, distance_config, sensor, buffer, calibration_buffer);
133 return EXIT_FAILURE;
134 }
135
136 acc_hal_integration_sensor_supply_on(SENSOR_ID);
137 acc_hal_integration_sensor_enable(SENSOR_ID);
138
139 sensor = acc_sensor_create(SENSOR_ID);
140 if (sensor == NULL)
141 {
142 printf("acc_sensor_create() failed\n");
143 cleanup(distance_handle, distance_config, sensor, buffer, calibration_buffer);
144 return EXIT_FAILURE;
145 }
146
147 // Calibrate sensor
148 bool status;
149 bool cal_complete = false;
150 acc_cal_result_t sensor_cal_result;
151
152 do
153 {
154 status = acc_sensor_calibrate(sensor, &cal_complete, &sensor_cal_result, buffer, buffer_size);
155
156 if (status && !cal_complete)
157 {
158 status = acc_hal_integration_wait_for_sensor_interrupt(SENSOR_ID, SENSOR_TIMEOUT_MS);
159 }
160 } while (status && !cal_complete);
161
162 if (!status)
163 {
164 printf("acc_sensor_calibrate() failed\n");
165 cleanup(distance_handle, distance_config, sensor, buffer, calibration_buffer);
166 return EXIT_FAILURE;
167 }
168
169 /* Reset sensor after calibration by disabling/enabling it */
170 acc_hal_integration_sensor_disable(SENSOR_ID);
171 acc_hal_integration_sensor_enable(SENSOR_ID);
172
173 // Calibrate detector
174 bool done = false;
175
176 do
177 {
178 status = acc_detector_distance_calibrate(sensor,
179 distance_handle,
180 &sensor_cal_result,
181 buffer,
182 buffer_size,
183 calibration_buffer,
184 calibration_buffer_size,
185 &done);
186 } while (status && !done && acc_hal_integration_wait_for_sensor_interrupt(SENSOR_ID, SENSOR_TIMEOUT_MS));
187
188 if (!status)
189 {
190 printf("acc_detector_distance_calibrate() failed\n");
191 cleanup(distance_handle, distance_config, sensor, buffer, calibration_buffer);
192 return EXIT_FAILURE;
193 }
194
195 acc_hal_integration_sensor_disable(SENSOR_ID);
196
197 acc_hal_integration_sensor_enable(SENSOR_ID);
198
199 if (!acc_detector_distance_prepare(distance_handle, distance_config, sensor, &sensor_cal_result, buffer, buffer_size))
200 {
201 printf("acc_detector_presence_prepare() failed\n");
202 cleanup(distance_handle, distance_config, sensor, buffer, calibration_buffer);
203 return EXIT_FAILURE;
204 }
205
206 if (!acc_sensor_hibernate_on(sensor))
207 {
208 printf("acc_sensor_hibernate_on failed\n");
209 acc_sensor_status(sensor);
210 cleanup(distance_handle, distance_config, sensor, buffer, calibration_buffer);
211 return EXIT_FAILURE;
212 }
213
214 acc_hal_integration_sensor_disable(SENSOR_ID);
215
216 while (true)
217 {
218 acc_detector_distance_result_t result;
219
220 acc_hal_integration_sensor_enable(SENSOR_ID);
221
222 if (!acc_sensor_hibernate_off(sensor))
223 {
224 printf("acc_sensor_hibernate_off failed\n");
225 acc_sensor_status(sensor);
226 cleanup(distance_handle, distance_config, sensor, buffer, calibration_buffer);
227 return EXIT_FAILURE;
228 }
229
230 bool result_available = false;
231
232 do
233 {
234 if (!acc_detector_distance_prepare(distance_handle, distance_config, sensor, &sensor_cal_result, buffer,
235 buffer_size))
236 {
237 printf("acc_detector_distance_prepare() failed\n");
238 cleanup(distance_handle, distance_config, sensor, buffer, calibration_buffer);
239 return EXIT_FAILURE;
240 }
241
242 if (!acc_sensor_measure(sensor))
243 {
244 printf("acc_sensor_measure failed\n");
245 cleanup(distance_handle, distance_config, sensor, buffer, calibration_buffer);
246 return EXIT_FAILURE;
247 }
248
249 if (!acc_hal_integration_wait_for_sensor_interrupt(SENSOR_ID, SENSOR_TIMEOUT_MS))
250 {
251 printf("Sensor interrupt timeout\n");
252 cleanup(distance_handle, distance_config, sensor, buffer, calibration_buffer);
253 return EXIT_FAILURE;
254 }
255
256 if (!acc_sensor_read(sensor, buffer, buffer_size))
257 {
258 printf("acc_sensor_read failed\n");
259 cleanup(distance_handle, distance_config, sensor, buffer, calibration_buffer);
260 return EXIT_FAILURE;
261 }
262
263 if (!acc_detector_distance_process(distance_handle, buffer, calibration_buffer, &result_available, &result))
264 {
265 printf("acc_detector_distance_process failed\n");
266 cleanup(distance_handle, distance_config, sensor, buffer, calibration_buffer);
267 return EXIT_FAILURE;
268 }
269 } while (!result_available);
270
271 if (!acc_sensor_hibernate_on(sensor))
272 {
273 printf("acc_sensor_hibernate_on failed\n");
274 acc_sensor_status(sensor);
275 cleanup(distance_handle, distance_config, sensor, buffer, calibration_buffer);
276 return EXIT_FAILURE;
277 }
278
279 acc_hal_integration_sensor_disable(SENSOR_ID);
280
281 print_result(&result);
282
283 acc_integration_sleep_until_periodic_wakeup();
284 }
285
286 cleanup(distance_handle, distance_config, sensor, buffer, calibration_buffer);
287
288 printf("Application finished OK\n");
289
290 return EXIT_SUCCESS;
291 }
292
293
294 static void cleanup(acc_detector_distance_handle_t *distance_handle,
295 acc_detector_distance_config_t *distance_config,
296 acc_sensor_t *sensor,
297 void *buffer,
298 void *calibration_buffer)
299 {
300 acc_hal_integration_sensor_disable(SENSOR_ID);
301 acc_hal_integration_sensor_supply_off(SENSOR_ID);
302
303 if (distance_config != NULL)
304 {
305 acc_detector_distance_config_destroy(distance_config);
306 }
307
308 if (distance_handle != NULL)
309 {
310 acc_detector_distance_destroy(distance_handle);
311 }
312
313 if (sensor != NULL)
314 {
315 acc_sensor_destroy(sensor);
316 }
317
318 if (buffer != NULL)
319 {
320 acc_integration_mem_free(buffer);
321 }
322
323 if (calibration_buffer != NULL)
324 {
325 acc_integration_mem_free(calibration_buffer);
326 }
327 }
328
329
330 static void set_config(acc_detector_distance_config_t *distance_config)
331 {
332 // Add configuration of the detector here
333
334 acc_detector_distance_config_start_set(distance_config, 0.25f);
335 acc_detector_distance_config_end_set(distance_config, 3.0f);
336 }
337
338
339 static void print_result(acc_detector_distance_result_t *result)
340 {
341 printf("%d detected distances", result->num_distances);
342 if (result->num_distances > 0)
343 {
344 printf(": ");
345
346 for (uint8_t i = 0; i < result->num_distances; i++)
347 {
348 printf("%" PRIfloat "m ", ACC_LOG_FLOAT_TO_INTEGER(result->distances[i]));
349 }
350 }
351
352 printf("\n");
353 }
acc_hal_integration_sensor_supply_off
void acc_hal_integration_sensor_supply_off(acc_sensor_id_t sensor_id)
Power off sensor supply.
Definition: acc_hal_integration_stm32cube_xm.c:113
acc_rss_a121.h
set_config
static void set_config(acc_detector_distance_config_t *distance_config)
Definition: example_detector_distance_low_power_hibernate.c:330
acc_hal_integration_sensor_supply_on
void acc_hal_integration_sensor_supply_on(acc_sensor_id_t sensor_id)
Power on sensor supply.
Definition: acc_hal_integration_stm32cube_xm.c:107
acc_sensor_read
bool acc_sensor_read(const acc_sensor_t *sensor, void *buffer, uint32_t buffer_size)
Read out radar data.
acc_version.h
acconeer_main
int acconeer_main(int argc, char *argv[])
Assembly test example.
Definition: example_detector_distance_low_power_hibernate.c:70
acc_cal_result_t
Definition: acc_definitions_a121.h:19
acc_detector_distance_config_start_set
void acc_detector_distance_config_start_set(acc_detector_distance_config_t *config, float start_m)
Set the start of measured interval in meters.
SENSOR_TIMEOUT_MS
#define SENSOR_TIMEOUT_MS
Definition: example_detector_distance_low_power_hibernate.c:50
acc_detector_distance_get_buffer_sizes
bool acc_detector_distance_get_buffer_sizes(const acc_detector_distance_handle_t *handle, uint32_t *buffer_size, uint32_t *calibration_buffer_size)
Get the buffer sizes needed given the provided detector handle.
acc_integration.h
acc_detector_distance_result_t::num_distances
uint8_t num_distances
Definition: acc_detector_distance.h:62
acc_detector_distance_create
acc_detector_distance_handle_t * acc_detector_distance_create(const acc_detector_distance_config_t *config)
Create a distance detector with the provided configuration.
acc_detector_distance_config_destroy
void acc_detector_distance_config_destroy(acc_detector_distance_config_t *config)
Destroy a configuration for a distance detector.
acc_hal_rss_integration_get_implementation
const acc_hal_a121_t * acc_hal_rss_integration_get_implementation(void)
Get hal implementation reference.
Definition: acc_hal_integration_stm32cube_xm.c:166
acc_detector_distance_process
bool acc_detector_distance_process(acc_detector_distance_handle_t *handle, void *buffer, void *calibration_buffer, bool *result_available, acc_detector_distance_result_t *result)
Process the data according to the configuration used in acc_detector_distance_config_create.
acc_integration_mem_alloc
void * acc_integration_mem_alloc(size_t size)
Allocate dynamic memory.
Definition: acc_integration_stm32.c:632
acc_hal_a121_t
Definition: acc_hal_definitions_a121.h:82
acc_detector_distance_calibrate
bool acc_detector_distance_calibrate(acc_sensor_t *sensor, acc_detector_distance_handle_t *handle, const acc_cal_result_t *sensor_cal_result, void *buffer, uint32_t buffer_size, void *calibration_buffer, uint32_t calibration_buffer_size, bool *calibration_complete)
Do a detector calibration.
SENSOR_ID
#define SENSOR_ID
Definition: example_detector_distance_low_power_hibernate.c:49
acc_rss_hal_register
bool acc_rss_hal_register(const acc_hal_a121_t *hal)
Register an integration.
acc_integration_set_periodic_wakeup
void acc_integration_set_periodic_wakeup(uint32_t time_msec)
Set up a periodic timer used to wake up the system from sleep.
Definition: acc_integration_stm32.c:531
acc_sensor.h
acc_detector_distance_destroy
void acc_detector_distance_destroy(acc_detector_distance_handle_t *handle)
Destroy the distance detector handle, freeing its resources.
acc_hal_integration_wait_for_sensor_interrupt
bool acc_hal_integration_wait_for_sensor_interrupt(acc_sensor_id_t sensor_id, uint32_t timeout_ms)
Wait for a sensor interrupt.
Definition: acc_hal_integration_stm32cube_xm.c:142
acc_hal_integration_a121.h
print_result
static void print_result(acc_detector_distance_result_t *result)
Definition: example_detector_distance_low_power_hibernate.c:339
acc_sensor_hibernate_off
bool acc_sensor_hibernate_off(const acc_sensor_t *sensor)
Restore sensor after exiting hibernation.
cleanup
static void cleanup(acc_detector_distance_handle_t *distance_handle, acc_detector_distance_config_t *distance_config, acc_sensor_t *sensor, void *buffer, void *calibration_buffer)
Definition: example_detector_distance_low_power_hibernate.c:294
acc_version_get
const char * acc_version_get(void)
Get the version of the Acconeer software.
printf
#define printf
Definition: printf.h:60
acc_hal_integration_sensor_enable
void acc_hal_integration_sensor_enable(acc_sensor_id_t sensor_id)
Enable sensor.
Definition: acc_hal_integration_stm32cube_xm.c:119
acc_hal_definitions_a121.h
acc_detector_distance_prepare
bool acc_detector_distance_prepare(const acc_detector_distance_handle_t *handle, const acc_detector_distance_config_t *config, acc_sensor_t *sensor, const acc_cal_result_t *sensor_cal_result, void *buffer, uint32_t buffer_size)
Prepare the detector for measurements.
acc_detector_distance_result_t
Distance detector result.
Definition: acc_detector_distance.h:49
acc_hal_integration_sensor_disable
void acc_hal_integration_sensor_disable(acc_sensor_id_t sensor_id)
Disable sensor.
Definition: acc_hal_integration_stm32cube_xm.c:130
acc_detector_distance_config_end_set
void acc_detector_distance_config_end_set(acc_detector_distance_config_t *config, float end_m)
Set the end of measured interval in meters.
acc_integration_log.h
acc_detector_distance_handle_t
struct acc_detector_distance_handle acc_detector_distance_handle_t
Definition: acc_detector_distance.h:35
acc_sensor_hibernate_on
bool acc_sensor_hibernate_on(acc_sensor_t *sensor)
Prepare sensor for entering hibernation.
ACC_LOG_FLOAT_TO_INTEGER
#define ACC_LOG_FLOAT_TO_INTEGER(a)
Definition: acc_integration_log.h:26
acc_sensor_status
void acc_sensor_status(const acc_sensor_t *sensor)
Check the status of the sensor.
acc_integration_mem_free
void acc_integration_mem_free(void *ptr)
Free dynamic memory.
Definition: acc_integration_stm32.c:644
acc_integration_sleep_until_periodic_wakeup
void acc_integration_sleep_until_periodic_wakeup(void)
Put the system in sleep until the periodic timer triggers.
Definition: acc_integration_stm32.c:590
acc_definitions_common.h
DEFAULT_UPDATE_RATE
#define DEFAULT_UPDATE_RATE
Definition: example_detector_distance_low_power_hibernate.c:51
acc_detector_distance_config_create
acc_detector_distance_config_t * acc_detector_distance_config_create(void)
Create a configuration for a distance detector.
acc_detector_distance_result_t::distances
float distances[(10U)]
Definition: acc_detector_distance.h:54
acc_detector_distance.h
acc_sensor_calibrate
bool acc_sensor_calibrate(acc_sensor_t *sensor, bool *cal_complete, acc_cal_result_t *cal_result, void *buffer, uint32_t buffer_size)
Calibrate a sensor.
PRIfloat
#define PRIfloat
Specifier for printing float type using integers.
Definition: acc_integration_log.h:31
acc_detector_distance_config_t
struct acc_detector_distance_config acc_detector_distance_config_t
Definition: acc_detector_distance.h:43
acc_sensor_measure
bool acc_sensor_measure(acc_sensor_t *sensor)
Start a radar measurement with previously prepared configuration.
acc_sensor_t
struct acc_sensor acc_sensor_t
Definition: acc_sensor.h:31
acc_sensor_destroy
void acc_sensor_destroy(acc_sensor_t *sensor)
Destroy a sensor instance freeing any resources allocated.
acc_definitions_a121.h
acc_sensor_create
acc_sensor_t * acc_sensor_create(acc_sensor_id_t sensor_id)
Create a sensor instance.