example_service_low_power_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"
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_low_power_sensor_hibernate.c
26  * @brief This is an example on how to put the sensor in hibernation and the system in a low power state between measurements
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  * - Enable sensor
33  * - Prepare a sensor
34  * - Enter hibernation
35  * - Loop forever:
36  * - Exit hibernation
37  * - Perform a sensor measurement and read out the data
38  * - Enter hibernation
39  * - Process the measurement
40  * - Put the system in deep sleep for a specified amount of time
41  * - Destroy the sensor instance
42  * - Destroy the processing instance
43  * - Destroy the configuration
44  */
45 
46 
47 #define SENSOR_ID (1U)
48 #define SENSOR_TIMEOUT_MS (1000U)
49 #define MAX_DATA_ENTRY_LEN 15 // "-32000+-32000i" + zero termination
50 
51 #define SUSPEND_TIME_BETWEEN_UPDATES_MS (10000) // 0.1Hz
52 
53 static void set_config(acc_config_t *config);
54 
55 
56 static void print_data(acc_int16_complex_t *data, uint16_t data_length);
57 
58 
59 static void cleanup(acc_config_t *config, acc_processing_t *processing,
60  acc_sensor_t *sensor, void *buffer);
61 
62 
63 int acconeer_main(int argc, char *argv[]);
64 
65 
66 int acconeer_main(int argc, char *argv[])
67 {
68  (void)argc;
69  (void)argv;
70  acc_config_t *config = NULL;
71  acc_processing_t *processing = NULL;
72  acc_sensor_t *sensor = NULL;
73  void *buffer = NULL;
74  uint32_t buffer_size = 0;
75  acc_processing_metadata_t proc_meta;
76  acc_processing_result_t proc_result;
77 
78  printf("Acconeer software version %s\n", acc_version_get());
79 
82 
83  if (!acc_rss_hal_register(hal))
84  {
85  return EXIT_FAILURE;
86  }
87 
88  config = acc_config_create();
89  if (config == NULL)
90  {
91  printf("acc_config_create() failed\n");
92  cleanup(config, processing, sensor, buffer);
93  return EXIT_FAILURE;
94  }
95 
96  set_config(config);
97 
98  processing = acc_processing_create(config, &proc_meta);
99  if (processing == NULL)
100  {
101  printf("acc_processing_create() failed\n");
102  cleanup(config, processing, sensor, buffer);
103  return EXIT_FAILURE;
104  }
105 
106  if (!acc_rss_get_buffer_size(config, &buffer_size))
107  {
108  printf("acc_rss_get_buffer_size() failed\n");
109  cleanup(config, processing, sensor, buffer);
110  return EXIT_FAILURE;
111  }
112 
113  buffer = acc_integration_mem_alloc(buffer_size);
114  if (buffer == NULL)
115  {
116  printf("buffer allocation failed\n");
117  cleanup(config, processing, sensor, buffer);
118  return EXIT_FAILURE;
119  }
120 
123 
124  sensor = acc_sensor_create(SENSOR_ID);
125  if (sensor == NULL)
126  {
127  printf("acc_sensor_create() failed\n");
128  cleanup(config, processing, sensor, buffer);
129  return EXIT_FAILURE;
130  }
131 
132  bool status;
133  bool cal_complete = false;
134  acc_cal_result_t cal_result;
135 
136  do
137  {
138  status = acc_sensor_calibrate(sensor, &cal_complete, &cal_result, buffer, buffer_size);
139 
140  if (status && !cal_complete)
141  {
143  }
144  } while (status && !cal_complete);
145 
146  if (!status)
147  {
148  printf("acc_sensor_calibrate() failed\n");
149  acc_sensor_status(sensor);
150  cleanup(config, processing, sensor, buffer);
151  return EXIT_FAILURE;
152  }
153 
154  // Reset sensor after calibration by disabling it
156 
158 
159  if (!acc_sensor_prepare(sensor, config, &cal_result, buffer, buffer_size))
160  {
161  printf("acc_sensor_prepare() failed\n");
162  acc_sensor_status(sensor);
163  cleanup(config, processing, sensor, buffer);
164  return EXIT_FAILURE;
165  }
166 
167  if (!acc_sensor_hibernate_on(sensor))
168  {
169  printf("acc_sensor_hibernate_on failed\n");
170  acc_sensor_status(sensor);
171  cleanup(config, processing, sensor, buffer);
172  return EXIT_FAILURE;
173  }
174 
176 
177  while (true)
178  {
180  if (!acc_sensor_hibernate_off(sensor))
181  {
182  printf("acc_sensor_hibernate_off failed\n");
183  acc_sensor_status(sensor);
184  cleanup(config, processing, sensor, buffer);
185  return EXIT_FAILURE;
186  }
187 
188  if (!acc_sensor_measure(sensor))
189  {
190  printf("acc_sensor_measure failed\n");
191  acc_sensor_status(sensor);
192  cleanup(config, processing, sensor, buffer);
193  return EXIT_FAILURE;
194  }
195 
197  {
198  printf("Sensor interrupt timeout\n");
199  acc_sensor_status(sensor);
200  cleanup(config, processing, sensor, buffer);
201  return EXIT_FAILURE;
202  }
203 
204  if (!acc_sensor_read(sensor, buffer, buffer_size))
205  {
206  printf("acc_sensor_read failed\n");
207  acc_sensor_status(sensor);
208  cleanup(config, processing, sensor, buffer);
209  return EXIT_FAILURE;
210  }
211 
212  if (!acc_sensor_hibernate_on(sensor))
213  {
214  printf("acc_sensor_hibernate_on failed\n");
215  acc_sensor_status(sensor);
216  cleanup(config, processing, sensor, buffer);
217  return EXIT_FAILURE;
218  }
219 
221 
222  acc_processing_execute(processing, buffer, &proc_result);
223 
224  print_data(proc_result.frame, proc_meta.frame_data_length);
225 
227  }
229  if (!acc_sensor_hibernate_off(sensor))
230  {
231  printf("acc_sensor_hibernate_off failed\n");
232  acc_sensor_status(sensor);
233  cleanup(config, processing, sensor, buffer);
234  return EXIT_FAILURE;
235  }
236 
237  cleanup(config, processing, sensor, buffer);
238 
239  printf("Application finished OK\n");
240 
241  return EXIT_SUCCESS;
242 }
243 
244 
245 static void cleanup(acc_config_t *config, acc_processing_t *processing,
246  acc_sensor_t *sensor, void *buffer)
247 {
250 
251  if (sensor != NULL)
252  {
253  acc_sensor_destroy(sensor);
254  }
255 
256  if (processing != NULL)
257  {
258  acc_processing_destroy(processing);
259  }
260 
261  if (config != NULL)
262  {
263  acc_config_destroy(config);
264  }
265 
266  if (buffer != NULL)
267  {
268  acc_integration_mem_free(buffer);
269  }
270 }
271 
272 
273 static void print_data(acc_int16_complex_t *data, uint16_t data_length)
274 {
275  printf("Processed data:\n");
276  char buffer[MAX_DATA_ENTRY_LEN];
277 
278  for (uint16_t i = 0; i < data_length; i++)
279  {
280  if ((i > 0) && ((i % 8) == 0))
281  {
282  printf("\n");
283  }
284 
285  snprintf(buffer, sizeof(buffer), "%" PRIi16 "+%" PRIi16 "i", data[i].real, data[i].imag);
286 
287  printf("%14s ", buffer);
288  }
289 
290  printf("\n");
291 }
292 
293 
294 static void set_config(acc_config_t *config)
295 {
296  // Add configuration of the sensor here
297 
298  acc_config_start_point_set(config, 80);
299  acc_config_num_points_set(config, 160);
300 }
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
acc_processing_destroy
void acc_processing_destroy(acc_processing_t *handle)
Destroy a processing instance identified with the provided processing handle.
SUSPEND_TIME_BETWEEN_UPDATES_MS
#define SUSPEND_TIME_BETWEEN_UPDATES_MS
Definition: example_service_low_power_sensor_hibernate.c:51
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
MAX_DATA_ENTRY_LEN
#define MAX_DATA_ENTRY_LEN
Definition: example_service_low_power_sensor_hibernate.c:49
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.
cleanup
static void cleanup(acc_config_t *config, acc_processing_t *processing, acc_sensor_t *sensor, void *buffer)
Definition: example_service_low_power_sensor_hibernate.c:245
acc_version.h
set_config
static void set_config(acc_config_t *config)
Definition: example_service_low_power_sensor_hibernate.c:294
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.
SENSOR_TIMEOUT_MS
#define SENSOR_TIMEOUT_MS
Definition: example_service_low_power_sensor_hibernate.c:48
SENSOR_ID
#define SENSOR_ID
Definition: example_service_low_power_sensor_hibernate.c:47
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
print_data
static void print_data(acc_int16_complex_t *data, uint16_t data_length)
Definition: example_service_low_power_sensor_hibernate.c:273
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.
acconeer_main
int acconeer_main(int argc, char *argv[])
Assembly test example.
Definition: example_service_low_power_sensor_hibernate.c:66
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_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_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.
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_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
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.
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.
acc_definitions_a121.h
acc_sensor_create
acc_sensor_t * acc_sensor_create(acc_sensor_id_t sensor_id)
Create a sensor instance.