example_service_multiple_configurations.c
Go to the documentation of this file.
1 // Copyright (c) Acconeer AB, 2020-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_multiple_configurations.c
26 * @brief This is an example on how the Service API can be used with multiple configurations
27 * @n
28 * The example executes as follows:
29 * - Create a first configuration
30 * - Create a second configuration
31 * - Create a processing instance using the first configuration
32 * - Create a second processing instance using the second configuration
33 * - Create a sensor instance
34 * - Prepare a sensor for the first configuration
35 * - Perform a sensor measurement and read out the data
36 * - Process the measurement with the first processing instance
37 * - Prepare a sensor for the second configuration
38 * - Perform a sensor measurement and read out the data
39 * - Process the measurement with the second processing instance
40 * - Destroy the sensor instance
41 * - Destroy the first processing instance
42 * - Destroy the second processing instance
43 * - Destroy the first configuration
44 * - Destroy the second configuration
45 */
46
47 #define SENSOR_ID (1U)
48 #define SENSOR_TIMEOUT_MS (1000U)
49 #define MAX_DATA_ENTRY_LEN (15U) // "-32000+-32000i" + zero termination
50 #define NBR_CONFIGS (2U)
51
52
53 typedef struct example_config
54 {
55 int16_t start_point;
56 uint16_t num_points;
57 uint16_t sweeps_per_frame;
58 } example_config_t;
59
60
61 static void print_data(acc_int16_complex_t *data, uint16_t data_length, uint32_t cfg);
62
63
64 static void cleanup(acc_config_t **config, acc_processing_t **processing,
65 acc_sensor_t *sensor, void *buffer, uint32_t nbr_configs);
66
67
68 int acconeer_main(int argc, char *argv[]);
69
70
71 int acconeer_main(int argc, char *argv[])
72 {
73 (void)argc;
74 (void)argv;
75
76 acc_config_t *config[NBR_CONFIGS] = { NULL };
77 acc_processing_t *processing[NBR_CONFIGS] = { NULL };
78 acc_sensor_t *sensor = NULL;
79 void *buffer = NULL;
80 uint32_t buffer_size = 0;
81 acc_processing_metadata_t proc_meta[NBR_CONFIGS];
82 acc_processing_result_t proc_result;
83
84 example_config_t example_configurations[] =
85 {
86 {
87 .start_point = 100,
88 .num_points = 100,
89 .sweeps_per_frame = 1,
90 },
91 {
92 .start_point = 120,
93 .num_points = 120,
94 .sweeps_per_frame = 3,
95 }
96 };
97
98 printf("Acconeer software version %s\n", acc_version_get());
99
100 const acc_hal_a121_t *hal = acc_hal_rss_integration_get_implementation();
101
102 if (!acc_rss_hal_register(hal))
103 {
104 return EXIT_FAILURE;
105 }
106
107 for (uint32_t cfg = 0U; cfg < NBR_CONFIGS; cfg++)
108 {
109 config[cfg] = acc_config_create();
110 if (config[cfg] == NULL)
111 {
112 printf("acc_config_create() failed\n");
113 cleanup(config, processing, sensor, buffer, NBR_CONFIGS);
114 return EXIT_FAILURE;
115 }
116
117 acc_config_start_point_set(config[cfg], example_configurations[cfg].start_point);
118 acc_config_num_points_set(config[cfg], example_configurations[cfg].num_points);
119 acc_config_sweeps_per_frame_set(config[cfg], example_configurations[cfg].sweeps_per_frame);
120
121 processing[cfg] = acc_processing_create(config[cfg], &proc_meta[cfg]);
122 if (processing[cfg] == NULL)
123 {
124 printf("acc_processing_create() failed\n");
125 cleanup(config, processing, sensor, buffer, NBR_CONFIGS);
126 return EXIT_FAILURE;
127 }
128
129 uint32_t current_buffer_size = 0;
130
131 if (!acc_rss_get_buffer_size(config[cfg], &current_buffer_size))
132 {
133 printf("acc_rss_get_buffer_size() failed\n");
134 cleanup(config, processing, sensor, buffer, NBR_CONFIGS);
135 return EXIT_FAILURE;
136 }
137
138 if (buffer_size < current_buffer_size)
139 {
140 buffer_size = current_buffer_size;
141 }
142 }
143
144 buffer = acc_integration_mem_alloc(buffer_size);
145 if (buffer == NULL)
146 {
147 printf("buffer allocation failed\n");
148 cleanup(config, processing, sensor, buffer, NBR_CONFIGS);
149 return EXIT_FAILURE;
150 }
151
152 acc_hal_integration_sensor_supply_on(SENSOR_ID);
153 acc_hal_integration_sensor_enable(SENSOR_ID);
154
155 sensor = acc_sensor_create(SENSOR_ID);
156 if (sensor == NULL)
157 {
158 printf("acc_sensor_create() failed\n");
159 cleanup(config, processing, sensor, buffer, NBR_CONFIGS);
160 return EXIT_FAILURE;
161 }
162
163 bool status;
164 bool cal_complete = false;
165 acc_cal_result_t cal_result;
166
167 do
168 {
169 status = acc_sensor_calibrate(sensor, &cal_complete, &cal_result, buffer, buffer_size);
170
171 if (status && !cal_complete)
172 {
173 status = acc_hal_integration_wait_for_sensor_interrupt(SENSOR_ID, SENSOR_TIMEOUT_MS);
174 }
175 } while (status && !cal_complete);
176
177 if (!status)
178 {
179 printf("acc_sensor_calibrate() failed\n");
180 acc_sensor_status(sensor);
181 cleanup(config, processing, sensor, buffer, NBR_CONFIGS);
182 return EXIT_FAILURE;
183 }
184
185 // Reset sensor after calibration by disabling it
186 acc_hal_integration_sensor_disable(SENSOR_ID);
187 acc_hal_integration_sensor_enable(SENSOR_ID);
188
189 for (uint32_t i = 0U; i < 5U; i++)
190 {
191 for (uint32_t cfg = 0U; cfg < NBR_CONFIGS; cfg++)
192 {
193 if (!acc_sensor_prepare(sensor, config[cfg], &cal_result, buffer, buffer_size))
194 {
195 printf("acc_sensor_prepare() failed\n");
196 acc_sensor_status(sensor);
197 cleanup(config, processing, sensor, buffer, NBR_CONFIGS);
198 return EXIT_FAILURE;
199 }
200
201 if (!acc_sensor_measure(sensor))
202 {
203 printf("acc_sensor_measure failed\n");
204 acc_sensor_status(sensor);
205 cleanup(config, processing, sensor, buffer, NBR_CONFIGS);
206 return EXIT_FAILURE;
207 }
208
209 if (!acc_hal_integration_wait_for_sensor_interrupt(SENSOR_ID, SENSOR_TIMEOUT_MS))
210 {
211 printf("Sensor interrupt timeout\n");
212 acc_sensor_status(sensor);
213 cleanup(config, processing, sensor, buffer, NBR_CONFIGS);
214 return EXIT_FAILURE;
215 }
216
217 if (!acc_sensor_read(sensor, buffer, buffer_size))
218 {
219 printf("acc_sensor_read failed\n");
220 acc_sensor_status(sensor);
221 cleanup(config, processing, sensor, buffer, NBR_CONFIGS);
222 return EXIT_FAILURE;
223 }
224
225 acc_processing_execute(processing[cfg], buffer, &proc_result);
226
227 print_data(proc_result.frame, proc_meta[cfg].frame_data_length, cfg);
228 }
229 }
230
231 cleanup(config, processing, sensor, buffer, NBR_CONFIGS);
232
233 printf("Application finished OK\n");
234
235 return EXIT_SUCCESS;
236 }
237
238
239 static void cleanup(acc_config_t **config, acc_processing_t **processing,
240 acc_sensor_t *sensor, void *buffer, uint32_t nbr_configs)
241 {
242 acc_hal_integration_sensor_disable(SENSOR_ID);
243 acc_hal_integration_sensor_supply_off(SENSOR_ID);
244
245 if (sensor != NULL)
246 {
247 acc_sensor_destroy(sensor);
248 }
249
250 for (uint32_t cfg = 0U; cfg < nbr_configs; cfg++)
251 {
252 if (processing[cfg] != NULL)
253 {
254 acc_processing_destroy(processing[cfg]);
255 }
256
257 if (config[cfg] != NULL)
258 {
259 acc_config_destroy(config[cfg]);
260 }
261 }
262
263 if (buffer != NULL)
264 {
265 acc_integration_mem_free(buffer);
266 }
267 }
268
269
270 static void print_data(acc_int16_complex_t *data, uint16_t data_length, uint32_t cfg)
271 {
272 printf("Processed data for config %" PRIu32 ":\n", cfg);
273 char buffer[MAX_DATA_ENTRY_LEN];
274
275 for (uint16_t i = 0; i < data_length; i++)
276 {
277 if ((i > 0) && ((i % 8) == 0))
278 {
279 printf("\n");
280 }
281
282 snprintf(buffer, sizeof(buffer), "%" PRIi16 "+%" PRIi16 "i", data[i].real, data[i].imag);
283
284 printf("%14s ", buffer);
285 }
286
287 printf("\n");
288 }
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.
SENSOR_ID
#define SENSOR_ID
Definition: example_service_multiple_configurations.c:47
example_config::num_points
uint16_t num_points
Definition: example_service_multiple_configurations.c:56
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
acc_processing_destroy
void acc_processing_destroy(acc_processing_t *handle)
Destroy a processing instance identified with the provided processing handle.
print_data
static void print_data(acc_int16_complex_t *data, uint16_t data_length, uint32_t cfg)
Definition: example_service_multiple_configurations.c:270
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
example_config
Definition: example_detector_presence_multiple_configurations.c:49
acc_sensor_read
bool acc_sensor_read(const acc_sensor_t *sensor, void *buffer, uint32_t buffer_size)
Read out radar data.
MAX_DATA_ENTRY_LEN
#define MAX_DATA_ENTRY_LEN
Definition: example_service_multiple_configurations.c:49
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_sweeps_per_frame_set
void acc_config_sweeps_per_frame_set(acc_config_t *config, uint16_t sweeps)
Set sweeps per frame.
acc_config_destroy
void acc_config_destroy(acc_config_t *config)
Destroy a configuration freeing any resources allocated.
NBR_CONFIGS
#define NBR_CONFIGS
Definition: example_service_multiple_configurations.c:50
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.
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
example_config::start_point
int16_t start_point
Definition: example_service_multiple_configurations.c:55
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
example_config::sweeps_per_frame
uint16_t sweeps_per_frame
Definition: example_service_multiple_configurations.c:57
acc_rss_hal_register
bool acc_rss_hal_register(const acc_hal_a121_t *hal)
Register an integration.
acc_sensor.h
example_config_t
struct example_config example_config_t
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_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
acconeer_main
int acconeer_main(int argc, char *argv[])
Assembly test example.
Definition: example_service_multiple_configurations.c:71
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_status
void acc_sensor_status(const acc_sensor_t *sensor)
Check the status of the sensor.
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
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.
cleanup
static void cleanup(acc_config_t **config, acc_processing_t **processing, acc_sensor_t *sensor, void *buffer, uint32_t nbr_configs)
Definition: example_service_multiple_configurations.c:239
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
SENSOR_TIMEOUT_MS
#define SENSOR_TIMEOUT_MS
Definition: example_service_multiple_configurations.c:48
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.