IRPyro API demo
Shows the basic functionality of the IRPyro API
main.c
Go to the documentation of this file.
1 /**
2 ******************************************************************************
3 * @file main.c
4 * @author _KEMET, Ltd
5 * @date March 2018
6 * @version Release 1.0.6
7 * @copyright (c) 2018 _KEMET, Ltd
8 * @brief Examples of single sensor and multiple sensor streaming. Also IOCTL
9 * of a single sensor.
10 *
11 * @verbatim
12  ===============================================================================
13  ##### How to use this example #####
14  ===============================================================================
15  [..]
16  Requirements
17  (#) The IRPyro API (IRPyroAPI.c)
18  (#) IRPyro breakout board (IRPYxxxxx-B1), IRPyro Base Board (dIRBB01) or
19  an IRPyro demo kit for motion, gas or flame. (See _KEMET website)
20  (#) Keil compiler version 5 onwards (https://www.keil.com/download/product/)
21  to build this example.
22  (#) C99 option enabled on the compiler to support initialization macros.
23  (#) The STM32 Standard Peripheral Library, see:
24  (http://www.st.com/content/st_com/en/products/embedded-software/mcus-embedded-software/stm32-embedded-software/stm32-standard-peripheral-libraries.html)
25  [..]
26  (#) See Micro_controller_template.c for details on how to configure SDA,SCL
27  INT and CS lines values.
28  (#) Create a variable of IRPyro_sensor_device_type
29  (#) Assign the values of the port and pin for the CS and INT lines
30  (#) Fill the AFE, CCP, and WUP registers with the desired values
31  (#) Compile, load and run.
32  (#) Display collected data on RealTerm or equivalent
33  @endverbatim
34  ******************************************************************************
35  @attention <h2><center>&copy; COPYRIGHT 2018 _KEMET, Ltd</center></h2>
36  @verbatim
37  Copyright (c) 2018, _KEMET, Ltd
38  All rights reserved.
39 
40  THIS SOFTWARE IS PROVIDED BY _KEMET, Ltd ''AS IS'' AND ANY
41  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
42  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  DISCLAIMED. IN NO EVENT SHALL _KEMET, Ltd BE LIABLE FOR ANY
44  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
45  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
47  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
48  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
49  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
50  @endverbatim
51 
52  ******************************************************************************
53  */
54 /* Includes ------------------------------------------------------------------*/
55 #include "main.h"
56 
57 static void read_a_sample(IRPyro_sensor_device_type* this_device);
58 static void read_sample_single_device(void);
59 static void read_sample_multiple_devices(void);
60 static void IOCTL_example(void);
61 static void wake_up_event_detection(void);
62 static void demo_board_sensor_control(int value);
63 static void usart_callback_setup(void);
64 static void IRPyro_sensor_population(IRPyro_devices this_list, uint8_t *list_size);
65 
66 /** @defgroup IRPyroAPI_Examples Code examples
67  * @brief How to stream IRPyro data and change parameters at high level
68  * @{
69  */
70 /**
71  * @brief Call point to the example functions
72  * @attention Uncomment the function to run
73  */
74 int main(void)
75 {
76  read_sample_single_device(); // use with single sensor board
77 // read_sample_multiple_devices(); // use with multiple sensors
78 // wake_up_event_detection(); // use with single sensor board
79 // IOCTL_example(); // use with single sensor board
80 }
81 /**
82  * @brief Example to initialize one IRPyro and stream data
83  *
84  * Creates an IRPyro sensor with all four channels active and the minimal
85  * required configuration: location of the CS and INT port/pin and a I2C
86  * address. See Read_Me.txt
87  * @attention Select display format to the UART: UART_stream_char_send() or
88  * UART_stream_binary_send() for plain text or binary format package.
89  * @note Modify accordingly depending on architecture
90  */
91 static void read_sample_single_device(void)
92 {
93  /*------------------------- IRPyro sensor definition ---------------------*/
94  /*
95  * NOTE: .number_of_active_channels can be set to 1 or 4.
96  * - when equal to 1 data from Chanel 2 is collected
97  * - when equal to 4 data from channels 1-4 is collected.
98  * IMPORTANT:
99  * FIFO data collected is stored in the first available position of the
100  * channel_value array.
101  *
102  * One channel active:
103  * channel 2 -> channel_value[0]
104  * Four channels active:
105  * channel 1 -> channel_value[0]
106  * channel 2 -> channel_value[1]
107  * channel 3 -> channel_value[2]
108  * channel 4 -> channel_value[3]
109  * For both cases:
110  * frame counter -> channel_value[5]
111  */
112 #define NUMBER_OF_SAMPLES_TO_COUNT 6000000
113  /*-------------------------- memory allocation----------------------------*/
114  IRPyro_sensor_device_type IRPyro_Sensor =
115  {
116  .CS_Port = 2,
117  .CS_Pin = 1,
118  .INT_Port = 1,
119  .INT_Pin = 4,
120  .address = 0x75,
121  .number_of_active_channels = 1,
122  .read_mode=0,
123  };
124 
125  /*----------------------- --- CONFIGURATION ------------------------------*/
126 
127  AFE_reg_type IRPyro_Sensor_AFE =
128  {
129  .S7_S0 = 0, // Sets the sampling rate to 1,000 samples per second.
130  .LP = LOW_POWER_DISABLE, // Low power sets the sampling rate to 166 samples per second.
131  .HP = HP_DISABLE, // High pass filter enabled
132  .C_LP = LOW_PASS_180, // Low pass filter set at 180Hz
133  .CLK_OUT = CLK_ENABLE, // Clock output enabled to synchronize other IRPyro sensors
134  .SYNC = MASTER, // IRPyro set as Master issues the synchronization to SLAVE
135  .TEMP = TEMP_DISABLE, //
136  .INT = INT_ENABLE // IRPyro indicates data ready enable
137  };
138 
139  CCP_reg_type IRPyro_Sensor_Ch_Cfg =
140  {
141  .Feedback_cap = FEEDBACK_200_80, // Feedback capacitance
142  .High_pass_filter_setting = HIGH_PASS_1HZ, // High pass filter set at 1Hz
143  .Feedback_transconductance = TCONDUCT_1_2T, // Time constant selection
144  .status_bit = CH_ENABLE // Channel ready to collect data
145  };
146 
147  WUP_reg_type IRPyro_Sensor_WUP_Cfg =
148  {
149  .UHT = MAX_WUP_THRESHOLD, // Upper High Threshold = 255 events
150  .ULT = SAFE_WUP_THRESHOLD, // Upper Low Threshold = 180 events
151  .LHT = MIN_WUP_THRESHOLD, // Low High Threshold = 0 events
152  .LLT = MIN_WUP_THRESHOLD, // Low Low Threshold = 0 events
153  .WT_Threshold = DEFAULT_NO_EVENTS, // Wake up event Threshold = 10 events
154  .DP0_DP2 = NOT_IN_USE_VALUE, // Active pixel channel
155  .CH0_CH2 = SEL_CH_2, // Reference channel
156  .ST = SLEEP_MODE_ONE_CH // Sleep mode type
157  };
158 
159  IRPyro_Sensor.AFE_register = IRPyro_Sensor_AFE;
160  IRPyro_Sensor.CCP_register[2] = IRPyro_Sensor_Ch_Cfg;
161  IRPyro_Sensor.WUP_register = IRPyro_Sensor_WUP_Cfg;
162 
163  /*-------------------------- board setup ---------------------------------*/
164  Micro_controller_template_init();
166  // disable all CS lines
168  /*----------------- enable the sensor for streaming-----------------------*/
169  IRPyro_open(&IRPyro_Sensor);
170 
171  for(int samples_counter = 0; samples_counter<NUMBER_OF_SAMPLES_TO_COUNT; ++samples_counter)
172  {
173  /*------------------------- application variables ------------------------*/
174 
175  /*-----------read the collected data and store in memory -----------------*/
176  IRPyro_read(&IRPyro_Sensor);
177  /*--------------- send collected data to a host --------------------------*/
178  UART_stream_char_send(IRPyro_Sensor.channel_value, IRPyro_Sensor.channel_saturation_byte);
179  //UART_stream_binary_send(IRPyro_Sensor.raw_data);
180  }
181  /*---------------- do what is needed with the value ----------------------*/
182 
183 
184  /*--------------------- close the device ---------------------------------*/
185  IRPyro_close(&IRPyro_Sensor);
186 }
187 /**
188  * @brief IRPyro data collection for a number samples
189  * @param this_device IRPyro configuration data
190  * @retval number_of_frames_lost
191  * @attention Select display format to the UART: UART_stream_char_send() or
192  * UART_stream_binary_send() for plain text or binary format package.
193  */
194 static void read_a_sample(IRPyro_sensor_device_type* this_device)
195 {
196 #define NUMBER_OF_FRAMES_TO_COUNT 3 // change to the desired number
197 #define IRPyro_FRAME_COUNT_LOCATION 5 // position on the user data buffer
198  uint16_t sample_counter = 0;
199  int read_delay = 1;
200 
201  /* A delay is created based on the power mode and the sample rate configured
202  to avoid reading the device too fast.
203  */
204  if(this_device->AFE_register.C_LP)
205  {
206  read_delay = (this_device->AFE_register.S7_S0 +1)*6; // on low power approximate factor
207  }
208  else
209  {
210  read_delay = this_device->AFE_register.S7_S0 +1;
211  }
212  IRPyro_read(this_device);
213  while (sample_counter < NUMBER_OF_FRAMES_TO_COUNT)
214  {
215  Micro_controller_delay_ms(read_delay);
216  if (IRPyro_read(this_device))
217  {
218  ++sample_counter;
219  UART_stream_char_send(this_device->channel_value, this_device->channel_saturation_byte);
220  // UART_stream_binary_send(this_device->raw_data);
221  }
222  }
223 }
224 /**
225  * @brief Example to initialize multiple IRPyros and stream data
226  *
227  * Creates a list of IRPyro sensors with all four channels active and the minimal
228  * required configuration: location of the CS and INT port/pin and a I2C
229  * address. See Read_Me.txt
230  * @attention Select display format to the UART: UART_stream_char_send() or
231  * UART_stream_binary_send() for plain text or binary format package.
232  * @note Modify accordingly depending on architecture
233  */
235 {
236  /*------------------------- IRPyro sensor definition ---------------------*/
237  /*
238  * NOTE: .number_of_active_channels can be set to 1 or 4.
239  * - when equal to 1 data from channel 2 is collected
240  * - when equal to 4 data from channels 1-4 is collected.
241  * IMPORTANT:
242  * FIFO data collected is stored in each individual sensor data structure
243  * using the same criteria as with a single sensor. See stream_data_IRPyro()
244  */
245 #define IRPyro_MAXINT 2147483647
246  /*------------------- Memory allocation, initialization ------------------*/
247 
248  IRPyro_devices IRPyro_device = {0};
249 
250  /*----------------------- --- CONFIGURATION ----- -----------------------*/
251 
252  AFE_reg_type IRPyro_AFE_Master =
253  {
254  .S7_S0 = 0, // Sets the sampling rate to 1,000 samples per second.
255  .LP = LOW_POWER_DISABLE, // Low power sets the sampling rate to 166 samples per second.
256  .HP = HP_DISABLE, // High pass filter enabled
257  .C_LP = LOW_PASS_180, // Low pass filter set at 180Hz
258  .CLK_OUT = CLK_ENABLE, // Clock output enabled to synchronize other IRPyro sensors
259  .SYNC = MASTER, // IRPyro set as Master issues the synchronization to SLAVE
260  .TEMP = TEMP_DISABLE, //
261  .INT = INT_ENABLE // IRPyro indicates data ready enable
262  };
263  AFE_reg_type IRPyro_AFE_Slave =
264  {
265  .S7_S0 = 0, // Sets the sampling rate to 1,000 samples per second.
266  .LP = LOW_POWER_DISABLE, // Low power sets the sampling rate to 166 samples per second.
267  .HP = HP_DISABLE, // High pass filter enabled
268  .C_LP = LOW_PASS_180, // Low pass filter set at 180Hz
269  .CLK_OUT = CLK_DISABLE, // Clock output enabled to synchronize other IRPyro sensors
270  .SYNC = SLAVE, // IRPyro set as Slave receives synchronization from Master
271  .TEMP = TEMP_DISABLE, //
272  .INT = INT_ENABLE // IRPyro indicates data ready enable
273  };
274 
275  CCP_reg_type IRPyro_Ch_Cfg =
276  {
277  .Feedback_cap = FEEDBACK_200_80, // Feedback capacitance
278  .High_pass_filter_setting = HIGH_PASS_1HZ, // High pass filter set at 1Hz
279  .Feedback_transconductance = TCONDUCT_1_2T, // Time constant selection
280  .status_bit = CH_ENABLE // Channel ready to collect data
281  };
282 
283  WUP_reg_type IRPyro_WUP_Cfg =
284  {
285  .UHT = MAX_WUP_THRESHOLD, // Upper High Threshold = 255 events
286  .ULT = SAFE_WUP_THRESHOLD, // Upper Low Threshold = 180 events
287  .LHT = MIN_WUP_THRESHOLD, // Low High Threshold = 0 events
288  .LLT = MIN_WUP_THRESHOLD, // Low Low Threshold = 0 events
289  .WT_Threshold = DEFAULT_NO_EVENTS, // Wake up time Threshold = 10 events
290  .DP0_DP2 = NOT_IN_USE_VALUE, // Active pixel channel
291  .CH0_CH2 = SEL_CH_2, // Reference channel
292  .ST = SLEEP_MODE_ONE_CH // Sleep mode type
293  };
294  uint8_t num_of_IRPyro_on_demo_board = 4; // number of devices on a demo board
295  /*----------------- ADDRESSES ------------*/
296  /* Assuming four sensors on an IRPyro Break out board or demo board*/
297  IRPyro_device[0].INT_Port = 1;
298  IRPyro_device[0].CS_Port = 2;
299  IRPyro_device[0].CS_Pin = 1;
300  IRPyro_device[0].INT_Pin = 4;
301  IRPyro_device[0].address = 0x70;
302 
303  IRPyro_device[1].CS_Port = 2;
304  IRPyro_device[1].INT_Port = 1;
305  IRPyro_device[1].CS_Pin = 5;
306  IRPyro_device[1].INT_Pin = 5;
307  IRPyro_device[1].address = 0x69;
308 
309  IRPyro_device[2].CS_Port = 2;
310  IRPyro_device[2].INT_Port = 1;
311  IRPyro_device[2].CS_Pin = 4;
312  IRPyro_device[2].INT_Pin = 6;
313  IRPyro_device[2].address = 0x68;
314 
315  IRPyro_device[3].CS_Port = 2;
316  IRPyro_device[3].INT_Port = 1;
317  IRPyro_device[3].CS_Pin = 3;
318  IRPyro_device[3].INT_Pin = 7;
319  IRPyro_device[3].address = 0x67;
320 
321  /*----------------- CONFIGURATION ------------*/
322  IRPyro_device[0].number_of_active_channels =1;
323  IRPyro_device[0].AFE_register = IRPyro_AFE_Master; // MASTER, INT, CLK ENABLED
324  IRPyro_device[0].CCP_register[2] = IRPyro_Ch_Cfg;
325  IRPyro_device[0].WUP_register = IRPyro_WUP_Cfg;
326 
327  IRPyro_device[1].number_of_active_channels =1;
328  IRPyro_device[1].AFE_register = IRPyro_AFE_Slave; // SLAVE, INT, CLK DISABLED
329  IRPyro_device[1].CCP_register[2] = IRPyro_Ch_Cfg;
330  IRPyro_device[1].WUP_register = IRPyro_WUP_Cfg;
331 
332  IRPyro_device[2].number_of_active_channels =1;
333  IRPyro_device[2].AFE_register = IRPyro_AFE_Slave; // SLAVE, INT, CLK DISABLED
334  IRPyro_device[2].CCP_register[2] = IRPyro_Ch_Cfg;
335  IRPyro_device[2].WUP_register = IRPyro_WUP_Cfg;
336 
337  IRPyro_device[3].number_of_active_channels =1;
338  IRPyro_device[3].AFE_register = IRPyro_AFE_Slave; // SLAVE, INT, CLK DISABLED
339  IRPyro_device[3].CCP_register[2] = IRPyro_Ch_Cfg;
340  IRPyro_device[3].WUP_register = IRPyro_WUP_Cfg;
341 
342  /*-------------------------- board setup ---------------------------------*/
343  Micro_controller_template_init();
345  IRPyro_sensor_population(IRPyro_device, &num_of_IRPyro_on_demo_board);
346  // disable all CS lines
348  /*-------- test the open_list, read_list and close_list functions --------*/
349  /*-------------------------- open_list ---------------------------------*/
350  IRPyro_open_devices(IRPyro_device, num_of_IRPyro_on_demo_board);
351  /*-------------------------- read_list ---------------------------------*/
352  for (int sweep=0; sweep< IRPyro_MAXINT; ++sweep)
353  {
354  IRPyro_read_devices(IRPyro_device, num_of_IRPyro_on_demo_board);
355  for(int i=0; i<IRPyro_LIST_SIZE; ++i)
356  {
357  UART_PutString(USART2, (uint8_t*)"SENSOR:");
358  UART_PutNumber(USART2, (uint32_t)i);
359  UART_PutString(USART2, (uint8_t*)" , ");
360  UART_stream_char_send(IRPyro_device[i].channel_value, IRPyro_device[i].channel_saturation_byte);
361  // UART_stream_binary_send(IRPyro_device[i].raw_data);
362  }
363  }
364  /*-------------------------- close_list ---------------------------------*/
365  IRPyro_close_devices(IRPyro_device, 4);
366  /*-------------------------- collate results -----------------------------*/
367 }
368 /**
369  * @brief Shows how to change sensor parameters on the run.
370  * @attention Select display format to the UART: UART_stream_char_send() or
371  * UART_stream_binary_send() for plain text or binary format package.
372  * @note This function demonstrates
373  sampling_rate_set,
374  sampling_rate_get,
375  high_pass_set,
376  high_pass_get,
377  low_pass_set,
378  low_pass_get,
379  gain_set,
380  gain_get,
381  power_off,
382  power_normal,
383  power_low,
384  power_sleep,
385  power_wakeup
386  */
387 static void IOCTL_example(void)
388 {
389  /*-------------------------- memory allocation----------------------------*/
390  IRPyro_sensor_device_type IRPyro_Sensor =
391  {
392  .CS_Port = 2,
393  .CS_Pin = 1,
394  .INT_Port = 1,
395  .INT_Pin = 4,
396  .address = 0x75,
397  .number_of_active_channels = 1,
398  .read_mode=0,
399  };
400  /*----------------------- --- CONFIGURATION ------------------------------*/
401  AFE_reg_type IRPyro_AFE_Single =
402  {
403  .S7_S0 = 0, // Sets the sampling rate to 1,000 samples per second.
404  .LP = LOW_POWER_DISABLE, // Low power sets the sampling rate to 166 samples per second.
405  .HP = HP_DISABLE, // High pass filter enabled
406  .C_LP = LOW_PASS_180, // Low pass filter set at 180Hz
407  .CLK_OUT = CLK_ENABLE, // Clock output enabled to synchronize other IRPyro sensors
408  .SYNC = MASTER, // IRPyro set as Master issues the synchronization to SLAVE
409  .TEMP = TEMP_DISABLE, //
410  .INT = INT_ENABLE // IRPyro indicates data ready enable
411  };
412  CCP_reg_type IRPyro_Ch_Cfg =
413  {
414  .Feedback_cap = FEEDBACK_200_80, // Feedback capacitance
415  .High_pass_filter_setting = HIGH_PASS_1HZ, // High pass filter set at 1Hz
416  .Feedback_transconductance = TCONDUCT_1_2T, // Time constant selection
417  .status_bit = CH_ENABLE // Channel ready to collect data
418  };
419  /*-------------------------- board setup ---------------------------------*/
420  Micro_controller_template_init();
422  // disable all CS lines
424 
425  /*-------- Single Sensor Configuration -----------------------------------*/
426  IRPyro_Sensor.AFE_register = IRPyro_AFE_Single;
427  IRPyro_Sensor.CCP_register[2] = IRPyro_Ch_Cfg;
428 
429  IRPyro_cmd_type IOCTL_cmd = {0};
430  IRPyro_arg_type IOCTL_arg = {0};
431 
432  IRPyro_open(&IRPyro_Sensor);
433  /*------------------------ Sampling rate ---------------------------------*/
434  IOCTL_cmd.cmd = sampling_rate_get; // Normal power, 0x00 = 1ms
435  IRPyro_ioctl(&IRPyro_Sensor, &IOCTL_cmd, IOCTL_arg);
436  read_a_sample(&IRPyro_Sensor);
437 
438  IOCTL_cmd.cmd = sampling_rate_set; // Normal power, 0xaa = 10 ms
439  IOCTL_arg.arg = 0x0A;
440  IRPyro_ioctl(&IRPyro_Sensor, &IOCTL_cmd, IOCTL_arg);
441  read_a_sample(&IRPyro_Sensor);
442 
443  IOCTL_cmd.cmd = sampling_rate_set; // Normal power, 0x00 = 1 ms
444  IOCTL_arg.arg = 0x00;
445  IRPyro_ioctl(&IRPyro_Sensor, &IOCTL_cmd, IOCTL_arg);
446  read_a_sample(&IRPyro_Sensor);
447  /*------------------------ High pass filter ------------------------------*/
448  IOCTL_cmd.cmd = high_pass_set; // Normal power, 0x02 = 2 Hz
449  IOCTL_arg.arg = HIGH_PASS_2HZ;
450  IOCTL_arg.channel = 1;
451  IRPyro_ioctl(&IRPyro_Sensor, &IOCTL_cmd, IOCTL_arg);
452  read_a_sample(&IRPyro_Sensor);
453 
454  IOCTL_cmd.cmd = high_pass_get;
455  IRPyro_ioctl(&IRPyro_Sensor, &IOCTL_cmd, IOCTL_arg);
456  read_a_sample(&IRPyro_Sensor);
457 
458  IOCTL_cmd.cmd = high_pass_set;
459  IOCTL_arg.arg = HIGH_PASS_8HZ;
460  IOCTL_arg.channel = 2;
461  IRPyro_ioctl(&IRPyro_Sensor, &IOCTL_cmd, IOCTL_arg);
462  read_a_sample(&IRPyro_Sensor);
463 
464  IOCTL_cmd.cmd = high_pass_get;
465  read_a_sample(&IRPyro_Sensor);
466  /*------------------------ Low pass filter ------------------------------*/
467  IOCTL_cmd.cmd = low_pass_set;
468  IOCTL_arg.arg = LOW_PASS_45;
469  IRPyro_ioctl(&IRPyro_Sensor, &IOCTL_cmd, IOCTL_arg);
470  read_a_sample(&IRPyro_Sensor);
471 
472  IOCTL_cmd.cmd = low_pass_get;
473  IRPyro_ioctl(&IRPyro_Sensor, &IOCTL_cmd, IOCTL_arg);
474  read_a_sample(&IRPyro_Sensor);
475  /*------------------------ Gain changes ----------------------------------*/
476  IOCTL_cmd.cmd = gain_set;
477  IOCTL_arg.arg = GAIN_08;
478  IOCTL_arg.channel = 3;
479  IRPyro_ioctl(&IRPyro_Sensor, &IOCTL_cmd, IOCTL_arg);
480  read_a_sample(&IRPyro_Sensor);
481 
482  IOCTL_cmd.cmd = gain_get;
483  IRPyro_ioctl(&IRPyro_Sensor, &IOCTL_cmd, IOCTL_arg);
484  read_a_sample(&IRPyro_Sensor);
485 
486  IOCTL_cmd.cmd = gain_set;
487  IOCTL_arg.arg = GAIN_16;
488  IOCTL_arg.channel = 4;
489  IRPyro_ioctl(&IRPyro_Sensor, &IOCTL_cmd, IOCTL_arg);
490  read_a_sample(&IRPyro_Sensor);
491  /*------------------------ Power control ---------------------------------*/
492  IOCTL_cmd.cmd = power_normal;
493  IRPyro_ioctl(&IRPyro_Sensor, &IOCTL_cmd, IOCTL_arg);
494  read_a_sample(&IRPyro_Sensor);
495 
496  IOCTL_cmd.cmd = power_low;
497  IRPyro_ioctl(&IRPyro_Sensor, &IOCTL_cmd, IOCTL_arg);
498  read_a_sample(&IRPyro_Sensor);
499 
500  IOCTL_cmd.cmd = power_sleep;
501  IRPyro_ioctl(&IRPyro_Sensor, &IOCTL_cmd, IOCTL_arg);
502 
503  IOCTL_cmd.cmd = power_wakeup;
504  IRPyro_ioctl(&IRPyro_Sensor, &IOCTL_cmd, IOCTL_arg);
505 
506  IOCTL_cmd.cmd = power_off;
507  IRPyro_ioctl(&IRPyro_Sensor, &IOCTL_cmd, IOCTL_arg);
508 
509  IRPyro_close(&IRPyro_Sensor);
510 };
511 /**
512  * @brief callback function
513  * @attention This function is called from an interrupt service request function
514  * and should be minimal.
515  */
516 static void wakeup_event_callback(void)
517 {
518 // add non blocking code
519 }
520 /**
521  * @brief Wake up event detection
522  * This function configures the IRPyro sensor to sleep mode
523  * @attention This function is called from an interrupt service request function
524  * and should be minimal.
525  */
526 static void wake_up_event_detection(void)
527 {
528 #define TIME_TO_WAIT 600000
529  /*------------------ Memory allocation, initialization-------------------*/
530  IRPyro_sensor_device_type IRPyro_single_sensor = {0}; // Instance of sensor
531  uint32_t time_to_wait = TIME_TO_WAIT;
532  uint8_t wake_up_detected = 0;
533  IRPyro_cmd_type IOCTL_cmd = {0};
534  IRPyro_arg_type IOCTL_arg = {0};
535 
536  /*-------------------Physical Location ----------------------------------*/
537  IRPyro_single_sensor.CS_Port = 2;
538  IRPyro_single_sensor.CS_Pin = 1;
539  IRPyro_single_sensor.INT_Port = 1;
540  IRPyro_single_sensor.INT_Pin = 4;
541  /*-------------------Test configuration ---------------------------------*/
542  IRPyro_single_sensor.read_mode = 0; // Interrupt configuration
543  IRPyro_single_sensor.address = 0x08; // I2C address to configure
544  IRPyro_single_sensor.number_of_active_channels = 1; // configures channel 2 for streaming
545  /*------------------- Wake up event definition -------------------------*/
546  IRPyro_single_sensor.WUP_register.UHT = MAX_WUP_THRESHOLD; // Upper High Threshold = 255 events
547  IRPyro_single_sensor.WUP_register.ULT = SAFE_WUP_THRESHOLD; // Upper Low Threshold = 180 events
548  IRPyro_single_sensor.WUP_register.LHT = MIN_WUP_THRESHOLD; // Low High Threshold = 0 events
549  IRPyro_single_sensor.WUP_register.LLT = MIN_WUP_THRESHOLD; // Low Low Threshold = 0 events
550  IRPyro_single_sensor.WUP_register.WT_Threshold = DEFAULT_NO_EVENTS; // Wake up event Threshold = 10 events
551  IRPyro_single_sensor.WUP_register.DP0_DP2 = NOT_IN_USE_VALUE; // Active pixel channel
552  IRPyro_single_sensor.WUP_register.CH0_CH2 = SEL_CH_2; // Reference channel
553  IRPyro_single_sensor.WUP_register.ST = SLEEP_MODE_ONE_CH; // Single channel selected
554  /*-------------------------- board setup ---------------------------------*/
555  Micro_controller_template_init();
557  // disable all CS lines
559  /*------------------------ Begin Test ------------------------------------*/
560  UART_PutString(USART2, (uint8_t*)"15 Configure sensor WAKE UP EVENT, SEND TO SLEEP, DETECT WAKE UP \n");
561  IRPyro_open(&IRPyro_single_sensor);
562  UART_PutString(USART2, (uint8_t*)"WAKE UP EVENT DETECTION BY HARDWARE INTERRUPT \n");
563 
564  /*------------------------ Send sensor to sleep --------------------------*/
565  UART_PutString(USART2, (uint8_t*)"SET IRPyro ON POWER NORMAL \n");
566  IOCTL_cmd.cmd = power_normal;
567  IRPyro_ioctl(&IRPyro_single_sensor, &IOCTL_cmd, IOCTL_arg);
568  read_a_sample(&IRPyro_single_sensor);
569 
570  UART_PutString(USART2, (uint8_t*)"SET IRPyro ON POWER SLEEP \n");
571  IOCTL_cmd.cmd = power_sleep;
572  IRPyro_ioctl(&IRPyro_single_sensor, &IOCTL_cmd, IOCTL_arg);
573 
574  /*------------------------ Send MCU to sleep -----------------------------*/
575  UART_PutString(USART2, (uint8_t*)"SEND MCU TO SLEEP \n");
576  UART_PutString(USART2, (uint8_t*)"WAIT FOR IRPyro TO WAKE MCU \n");
577  /*--------------- assign external function to wake-up event --------------*/
578  EXTernal_Callback_Setup(wakeup_event_callback);
579  // PWR_EnterSTOPMode(PWR_Regulator_LowPower, PWR_STOPEntry_WFI);
580  /*--------------- send micro controller to sleep --------------------------*/
581  PWR_EnterSleepMode(PWR_SLEEPEntry_WFI);
582 
583  /*wait for the sensor to wake up or time to run out*/
584  do
585  {
586  wake_up_detected = EXTI_detected(0);
587  --time_to_wait;
588  }
589  while ((time_to_wait != 0) && (wake_up_detected == 0));
590 
591  if (time_to_wait == 0 && wake_up_detected == 0)
592  {
593  IOCTL_cmd.cmd = power_wakeup;
594  UART_PutString(USART2, (uint8_t*)"FORCED POWER WAKE UP \n");
595  IRPyro_ioctl(&IRPyro_single_sensor, &IOCTL_cmd, IOCTL_arg);
596  UART_PutString(USART2, (uint8_t*)"READ TEST AFTER FORCED POWER WAKE UP \n");
597  read_a_sample(&IRPyro_single_sensor);
598  }
599  else
600  {
601  UART_PutString(USART2, (uint8_t*)"WAKE UP EVENT DETECTED!! \n");
602  UART_PutString(USART2, (uint8_t*)"READ TEST AFTER WAKE UP EVENT \n");
603  read_a_sample(&IRPyro_single_sensor);
604  }
605 
606  IRPyro_close(&IRPyro_single_sensor);
607  /*-------------------------- collate results -----------------------------*/
608  UART_PutString(USART2, (uint8_t*)"============\n");
609  UART_PutString(USART2, (uint8_t*)"--------------WAKE UP EVENT DETECTION BY HARDWARE INTERRUPT-------------------------\n");
610 }
611 /**
612  * @brief USART transmission function
613  * Code can be added here for the firmware to respond when the transmission software interrupt is enabled
614  */
615 static void tx_function(void)
616 {
617 
618 }
619 /**
620  * @brief USART RX function
621  * Code to catch data on the USART interface.
622  */
623 static void rx_function(void)
624 {
625  uint8_t uart_byte = (uint8_t)USART_ReceiveData(USART2);
626 }
627 /**
628  * @brief configures USART irq functions
629  */
630 static void usart_callback_setup(void)
631 {
632  USART_Callback_Rx_Setup(rx_function);
633  USART_Callback_Tx_Setup(tx_function);
634 }
635 /**
636  * @brief Detect IRPyro sensors present in the demo board
637  * @param this_list IRPyro sensor array
638  * @param list_size number of sensors in the array
639 */
640 static void IRPyro_sensor_population(IRPyro_devices this_list, uint8_t *list_size)
641 {
642  uint8_t num_of_IRPyro = *list_size;
643  uint8_t devices_present =0;
644  for(uint8_t current=0 ; current < num_of_IRPyro; ++ current)
645  {
646  if (IRPyro_open(&this_list[current]) == 1)
647  {
648  ++ devices_present;
649  }
650  }
651  *list_size = devices_present;
652 }
653 /**
654  * @brief Changes the state of all the CS lines as used on the IRPyro backplane board
655  * @param state 0 disabled / 1 enabled
656  */
658 {
659  CS_pin_set(2,1,state);
660  CS_pin_set(2,3,state);
661  CS_pin_set(2,4,state);
662  CS_pin_set(2,5,state);
663 }
664 /** end of IRPyroAPI_Examples
665  * @}
666  */
667 /* ********** Copyright (c) 2018 _KEMET, Ltd. **********END OF FILE************/
NUMBER_OF_FRAMES_TO_COUNT
#define NUMBER_OF_FRAMES_TO_COUNT
read_sample_single_device
static void read_sample_single_device(void)
Example to initialize one IRPyro and stream data.
Definition: main.c:91
read_sample_multiple_devices
static void read_sample_multiple_devices(void)
Example to initialize multiple IRPyros and stream data.
Definition: main.c:234
usart_callback_setup
static void usart_callback_setup(void)
configures USART irq functions
Definition: main.c:630
IOCTL_example
static void IOCTL_example(void)
Shows how to change sensor parameters on the run.
Definition: main.c:387
rx_function
static void rx_function(void)
USART RX function Code to catch data on the USART interface.
Definition: main.c:623
tx_function
static void tx_function(void)
USART transmission function Code can be added here for the firmware to respond when the transmission ...
Definition: main.c:615
wake_up_event_detection
static void wake_up_event_detection(void)
Wake up event detection This function configures the IRPyro sensor to sleep mode.
Definition: main.c:526
demo_board_sensor_control
static void demo_board_sensor_control(int value)
Changes the state of all the CS lines as used on the IRPyro backplane board.
Definition: main.c:657
read_a_sample
static void read_a_sample(IRPyro_sensor_device_type *this_device)
IRPyro data collection for a number samples.
Definition: main.c:194
wakeup_event_callback
static void wakeup_event_callback(void)
callback function
Definition: main.c:516
TIME_TO_WAIT
#define TIME_TO_WAIT
main.h
Function prototypes and initialization macros for example code.
main
int main(void)
Call point to the example functions.
Definition: main.c:74
NUMBER_OF_SAMPLES_TO_COUNT
#define NUMBER_OF_SAMPLES_TO_COUNT
IRPyro_sensor_population
static void IRPyro_sensor_population(IRPyro_devices this_list, uint8_t *list_size)
Detect IRPyro sensors present in the demo board.
Definition: main.c:640
IRPyro_MAXINT
#define IRPyro_MAXINT