example_service_sensor_hibernate.c
Go to the documentation of this file.
1 // Copyright (c) Acconeer AB, 2022-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 #include "acc_config.h"
13 #include "acc_definitions_a121.h"
14 #include "acc_definitions_common.h"
15 #include "acc_hal_definitions_a121.h"
16 #include "acc_hal_integration_a121.h"
17 #include "acc_integration.h"
18 #include "acc_processing.h"
19 #include "acc_rss_a121.h"
20 #include "acc_sensor.h"
21
22 #include "acc_version.h"
23
24
25 /** \example example_service_sensor_hibernate.c
26 * @brief This is an example on how hibernation can be used in the Service API
27 * @n
28 * The example executes as follows:
29 * - Create a configuration
30 * - Create a processing instance using the previously created configuration
31 * - Create a sensor instance
32 * - Prepare sensor
33 * - Put sensor in hibernation
34 * - Wake up sensor from hibernation
35 * - Perform a sensor measurement and read out the data
36 * - Process the measurement
37 * - Destroy the sensor instance
38 * - Destroy the processing instance
39 * - Destroy the configuration
40 */
41
42
43 #define SENSOR_ID (1U)
44 #define SENSOR_TIMEOUT_MS (1000U)
45 #define MAX_DATA_ENTRY_LEN 15 // "-32000+-32000i" + zero termination
46
47
48 static void set_config(acc_config_t *config);
49
50
51 static void print_data(acc_int16_complex_t *data, uint16_t data_length);
52
53
54 static void cleanup(acc_config_t *config, acc_processing_t *processing,
55 acc_sensor_t *sensor, void *buffer);
56
57
58 int acconeer_main(int argc, char *argv[]);
59
60
61 int acconeer_main(int argc, char *argv[])
62 {
63 (void)argc;
64 (void)argv;
65 acc_config_t *config = NULL;
66 acc_processing_t *processing = NULL;
67 acc_sensor_t *sensor = NULL;
68 void *buffer = NULL;
69 uint32_t buffer_size = 0;
70 acc_processing_metadata_t proc_meta;
71 acc_processing_result_t proc_result;
72
73 printf("Acconeer software version %s\n", acc_version_get());
74
75 const acc_hal_a121_t *hal = acc_hal_rss_integration_get_implementation();
76
77 if (!acc_rss_hal_register(hal))
78 {
79 return EXIT_FAILURE;
80 }
81
82 config = acc_config_create();
83 if (config == NULL)
84 {
85 printf("acc_config_create() failed\n");
86 cleanup(config, processing, sensor, buffer);
87 return EXIT_FAILURE;
88 }
89
90 set_config(config);
91
92 processing = acc_processing_create(config, &proc_meta);
93 if (processing == NULL)
94 {
95 printf("acc_processing_create() failed\n");
96 cleanup(config, processing, sensor, buffer);
97 return EXIT_FAILURE;
98 }
99
100 if (!acc_rss_get_buffer_size(config, &buffer_size))
101 {
102 printf("acc_rss_get_buffer_size() failed\n");
103 cleanup(config, processing, sensor, buffer);
104 return EXIT_FAILURE;
105 }
106
107 buffer = acc_integration_mem_alloc(buffer_size);
108 if (buffer == NULL)
109 {
110 printf("buffer allocation failed\n");
111 cleanup(config, processing, sensor, buffer);
112 return EXIT_FAILURE;
113 }
114
115 acc_hal_integration_sensor_supply_on(SENSOR_ID);
116 acc_hal_integration_sensor_enable(SENSOR_ID);
117
118 sensor = acc_sensor_create(SENSOR_ID);
119 if (sensor == NULL)
120 {
121 printf("acc_sensor_create() failed\n");
122 cleanup(config, processing, sensor, buffer);
123 return EXIT_FAILURE;
124 }
125
126 bool status;
127 bool cal_complete = false;
128 acc_cal_result_t cal_result;
129
130 do
131 {
132 status = acc_sensor_calibrate(sensor, &cal_complete, &cal_result, buffer, buffer_size);
133
134 if (status && !cal_complete)
135 {
136 status = acc_hal_integration_wait_for_sensor_interrupt(SENSOR_ID, SENSOR_TIMEOUT_MS);
137 }
138 } while (status && !cal_complete);
139
140 if (!status)
141 {
142 printf("acc_sensor_calibrate() failed\n");
143 acc_sensor_status(sensor);
144 cleanup(config, processing, sensor, buffer);
145 return EXIT_FAILURE;
146 }
147
148 // Reset sensor after calibration by disabling it
149 acc_hal_integration_sensor_disable(SENSOR_ID);
150
151 acc_hal_integration_sensor_enable(SENSOR_ID);
152
153 if (!acc_sensor_prepare(sensor, config, &cal_result, buffer, buffer_size))
154 {
155 printf("acc_sensor_prepare() failed\n");
156 acc_sensor_status(sensor);
157 cleanup(config, processing, sensor, buffer);
158 return EXIT_FAILURE;
159 }
160
161 if (!acc_sensor_hibernate_on(sensor))
162 {
163 printf("acc_sensor_hibernate_on failed\n");
164 acc_sensor_status(sensor);
165 cleanup(config, processing, sensor, buffer);
166 return EXIT_FAILURE;
167 }
168
169 acc_hal_integration_sensor_disable(SENSOR_ID);
170
171 for (uint32_t i = 0U; i < 5U; i++)
172 {
173 acc_hal_integration_sensor_enable(SENSOR_ID);
174 if (!acc_sensor_hibernate_off(sensor))
175 {
176 printf("acc_sensor_hibernate_off failed\n");
177 acc_sensor_status(sensor);
178 cleanup(config, processing, sensor, buffer);
179 return EXIT_FAILURE;
180 }
181
182 if (!acc_sensor_measure(sensor))
183 {
184 printf("acc_sensor_measure failed\n");
185 acc_sensor_status(sensor);
186 cleanup(config, processing, sensor, buffer);
187 return EXIT_FAILURE;
188 }
189
190 if (!acc_hal_integration_wait_for_sensor_interrupt(SENSOR_ID, SENSOR_TIMEOUT_MS))
191 {
192 printf("Sensor interrupt timeout\n");
193 acc_sensor_status(sensor);
194 cleanup(config, processing, sensor, buffer);
195 return EXIT_FAILURE;
196 }
197
198 if (!acc_sensor_read(sensor, buffer, buffer_size))
199 {
200 printf("acc_sensor_read failed\n");
201 acc_sensor_status(sensor);
202 cleanup(config, processing, sensor, 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(config, processing, sensor, buffer);
211 return EXIT_FAILURE;
212 }
213
214 acc_hal_integration_sensor_disable(SENSOR_ID);
215
216 acc_processing_execute(processing, buffer, &proc_result);
217
218 print_data(proc_result.frame, proc_meta.frame_data_length);
219
220 /*
221 * Here the system can be put into low power mode until
222 * requesting the next frame.
223 */
224 }
225
226 acc_hal_integration_sensor_enable(SENSOR_ID);
227 if (!acc_sensor_hibernate_off(sensor))
228 {
229 printf("acc_sensor_hibernate_off failed\n");
230 acc_sensor_status(sensor);
231 cleanup(config, processing, sensor, buffer);
232 return EXIT_FAILURE;
233 }
234
235 cleanup(config, processing, sensor, buffer);
236
237 printf("Application finished OK\n");
238
239 return EXIT_SUCCESS;
240 }
241
242
243 static void cleanup(acc_config_t *config, acc_processing_t *processing,
244 acc_sensor_t *sensor, void *buffer)
245 {
246 acc_hal_integration_sensor_disable(SENSOR_ID);
247 acc_hal_integration_sensor_supply_off(SENSOR_ID);
248
249 if (sensor != NULL)
250 {
251 acc_sensor_destroy(sensor);
252 }
253
254 if (processing != NULL)
255 {
256 acc_processing_destroy(processing);
257 }
258
259 if (config != NULL)
260 {
261 acc_config_destroy(config);
262 }
263
264 if (buffer != NULL)
265 {
266 acc_integration_mem_free(buffer);
267 }
268 }
269
270
271 static void print_data(acc_int16_complex_t *data, uint16_t data_length)
272 {
273 printf("Processed data:\n");
274 char buffer[MAX_DATA_ENTRY_LEN];
275
276 for (uint16_t i = 0; i < data_length; i++)
277 {
278 if ((i > 0) && ((i % 8) == 0))
279 {
280 printf("\n");
281 }
282
283 snprintf(buffer, sizeof(buffer), "%" PRIi16 "+%" PRIi16 "i", data[i].real, data[i].imag);
284
285 printf("%14s ", buffer);
286 }
287
288 printf("\n");
289 }
290
291
292 static void set_config(acc_config_t *config)
293 {
294 // Add configuration of the sensor here
295
296 acc_config_start_point_set(config, 80);
297 acc_config_num_points_set(config, 160);
298 }
SENSOR_ID
#define SENSOR_ID
Definition: example_service_sensor_hibernate.c:43
acc_config_start_point_set
void acc_config_start_point_set(acc_config_t *config, int32_t start_point)
Set the starting point of the sweep.
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_config_t *config)
Definition: example_service_sensor_hibernate.c:292
acc_processing_destroy
void acc_processing_destroy(acc_processing_t *handle)
Destroy a processing instance identified with the provided processing handle.
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_processing_result_t
Result provided by the processing module.
Definition: acc_processing.h:71
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
acc_rss_get_buffer_size
bool acc_rss_get_buffer_size(const acc_config_t *config, uint32_t *buffer_size)
Get the buffer size needed for the specified config.
acc_int16_complex_t
Data type for interger-based representation of complex numbers.
Definition: acc_definitions_common.h:43
acc_config_destroy
void acc_config_destroy(acc_config_t *config)
Destroy a configuration freeing any resources allocated.
acc_cal_result_t
Definition: acc_definitions_a121.h:19
acc_processing_execute
void acc_processing_execute(acc_processing_t *handle, void *buffer, acc_processing_result_t *result)
Process the data according to the configuration used in create.
cleanup
static void cleanup(acc_config_t *config, acc_processing_t *processing, acc_sensor_t *sensor, void *buffer)
Definition: example_service_sensor_hibernate.c:243
acc_config_create
acc_config_t * acc_config_create(void)
Create a configuration.
acc_integration.h
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_integration_mem_alloc
void * acc_integration_mem_alloc(size_t size)
Allocate dynamic memory.
Definition: acc_integration_stm32.c:632
acc_processing_metadata_t
Metadata that will be populated by the processing module during creation.
Definition: acc_processing.h:36
acc_hal_a121_t
Definition: acc_hal_definitions_a121.h:82
acc_rss_hal_register
bool acc_rss_hal_register(const acc_hal_a121_t *hal)
Register an integration.
acc_sensor.h
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
acc_processing_metadata_t::frame_data_length
uint16_t frame_data_length
Definition: acc_processing.h:39
acc_sensor_hibernate_off
bool acc_sensor_hibernate_off(const acc_sensor_t *sensor)
Restore sensor after exiting hibernation.
acc_version_get
const char * acc_version_get(void)
Get the version of the Acconeer software.
acc_config_t
struct acc_config acc_config_t
Definition: acc_config.h:26
printf
#define printf
Definition: printf.h:60
snprintf
#define snprintf
Definition: printf.h:84
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_config_num_points_set
void acc_config_num_points_set(acc_config_t *config, uint16_t num_points)
Set the number of data points to measure.
acc_hal_definitions_a121.h
acc_processing_result_t::frame
acc_int16_complex_t * frame
Definition: acc_processing.h:91
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_processing_t
struct acc_processing_handle acc_processing_t
Definition: acc_processing.h:30
acc_sensor_hibernate_on
bool acc_sensor_hibernate_on(acc_sensor_t *sensor)
Prepare sensor for entering hibernation.
acc_sensor_status
void acc_sensor_status(const acc_sensor_t *sensor)
Check the status of the sensor.
print_data
static void print_data(acc_int16_complex_t *data, uint16_t data_length)
Definition: example_service_sensor_hibernate.c:271
acc_sensor_prepare
bool acc_sensor_prepare(acc_sensor_t *sensor, const acc_config_t *config, const acc_cal_result_t *cal_result, void *buffer, uint32_t buffer_size)
Prepare a sensor to do a measurement.
acc_integration_mem_free
void acc_integration_mem_free(void *ptr)
Free dynamic memory.
Definition: acc_integration_stm32.c:644
acc_definitions_common.h
MAX_DATA_ENTRY_LEN
#define MAX_DATA_ENTRY_LEN
Definition: example_service_sensor_hibernate.c:45
acc_config.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.
SENSOR_TIMEOUT_MS
#define SENSOR_TIMEOUT_MS
Definition: example_service_sensor_hibernate.c:44
acc_sensor_measure
bool acc_sensor_measure(acc_sensor_t *sensor)
Start a radar measurement with previously prepared configuration.
acc_processing_create
acc_processing_t * acc_processing_create(const acc_config_t *config, acc_processing_metadata_t *processing_metadata)
Create a processing instance with the provided configuration.
acc_processing.h
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.
acconeer_main
int acconeer_main(int argc, char *argv[])
Assembly test example.
Definition: example_service_sensor_hibernate.c:61
acc_definitions_a121.h
acc_sensor_create
acc_sensor_t * acc_sensor_create(acc_sensor_id_t sensor_id)
Create a sensor instance.