STIMA  3
i2c-th.h
1 
2 /*********************************************************************
3 Copyright (C) 2017 Marco Baldinetti <m.baldinetti@digiteco.it>
4 authors:
5 Paolo patruno <p.patruno@iperbole.bologna.it>
6 Marco Baldinetti <m.baldinetti@digiteco.it>
7 
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2 of
11 the License, or (at your option) any later version.
12 
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17 
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 **********************************************************************/
21 
22 #ifndef _I2C_TH_H
23 #define _I2C_TH_H
24 
25 #include "i2c-th-config.h"
26 #include <debug.h>
27 #include <i2c_config.h>
28 #include <avr/sleep.h>
29 #include <avr/power.h>
30 #include <avr/wdt.h>
31 #include <i2c_utility.h>
32 #include <rmap_utility.h>
33 #include <sdcard_utility.h>
34 #if (USE_JSON)
35 #include <json_utility.h>
36 #endif
37 #include <eeprom_utility.h>
38 #include <Wire.h>
39 #include <TimeLib.h>
40 #include <typedef.h>
41 #include <registers-th.h>
42 #include <SensorDriver.h>
43 #include <debug_config.h>
44 #include <SdFat.h>
45 #include <StreamUtils.h>
46 #include <ArduinoLog.h>
47 
48 /*********************************************************************
49 * TYPEDEF
50 *********************************************************************/
51 
56 typedef struct {
57  char type[4];
58  uint8_t i2c_address;
60 
61 
62 
67 typedef struct {
68  uint8_t module_main_version;
69  uint8_t module_configuration_version;
70  uint8_t module_type;
71  uint8_t i2c_address;
72  bool is_oneshot;
73  bool is_continuous;
74  sensor_conf_t sensors[2];
76 
81 typedef struct {
82  uint8_t module_type;
83  uint8_t module_main_version;
84  uint8_t module_minor_version;
85  value_t temperature;
86  value_t humidity;
88 
93 typedef struct {
94  uint8_t i2c_address;
95  bool is_oneshot;
96  sensor_conf_t sensors[2];
98 
103 typedef struct {
104  int32_t values[SAMPLES_COUNT_MAX];
105  uint16_t count;
106  int32_t *read_ptr;
107  int32_t *write_ptr;
108 } sample_t;
109 
111 //\struct observation_t
112 //\brief Observations values for temperature and humidity NOT USED
113 //*/
114 //typedef struct {
115 // int32_t values[OBSERVATION_COUNT]; //!< buffer containing the mean values calculated on a one sample buffer respectively
116 // uint16_t count; //!< number of observations
117 // uint32_t *read_ptr; //!< reader pointer to buffer (read observations for calculate report value)
118 // uint32_t *write_ptr; //!< writer pointer to buffer (add new observation)
119 //} observation_t;
120 
121 /*********************************************************************
122 * TYPEDEF for Finite State Machine
123 *********************************************************************/
128 typedef enum {
129  INIT,
130  #if (USE_POWER_DOWN)
131  ENTER_POWER_DOWN,
132  #endif
134  END
135 } state_t;
136 
141 typedef enum {
143  SENSORS_READING_SETUP_CHECK,
144  SENSORS_READING_POWER_OFF,
145  SENSORS_READING_POWER_ON,
146  SENSORS_READING_SETUP,
156 
157 /*********************************************************************
158 * GLOBAL VARIABLE
159 *********************************************************************/
160 
166 
172 
178 
184 
190 
196 
202 
208 
213 volatile uint8_t readable_data_address;
214 
219 volatile uint8_t readable_data_length;
220 
225 volatile uint8_t i2c_rx_data[I2C_MAX_DATA_LENGTH];
226 
231 volatile uint8_t lastcommand;
232 
237 volatile uint8_t i2c_error;
238 
243 volatile uint8_t ready_tasks_count;
244 
250 
255 volatile bool inside_transaction;
256 
261 volatile uint16_t transaction_time;
262 
263 
268 bool is_start;
269 
274 bool is_stop;
275 
280 bool is_test_read;
281 
286 SensorDriver *sensors[SENSORS_MAX];
287 
292 uint8_t sensors_count;
293 
298 sample_t temperature_samples;
299 
304 sample_t humidity_samples;
305 
310 volatile uint16_t timer_counter;
311 
316 state_t state;
317 
322 sensors_reading_state_t sensors_reading_state;
323 
324 #if (ENABLE_SDCARD_LOGGING)
329 SdFat SD;
330 
335 File logFile;
336 
341 WriteLoggingStream loggingStream(logFile,Serial);
342 #endif
343 
344 /*********************************************************************
345 * FUNCTIONS
346 *********************************************************************/
354 void init_power_down(uint32_t *time_ms, uint32_t debouncing_ms);
355 
362 void init_wdt(uint8_t wdt_timer);
363 
369 void init_system(void);
370 
376 void init_buffers(void);
377 
383 void init_tasks(void);
384 
390 void init_pins(void);
391 
397 void init_wire(void);
398 
404 void init_spi(void);
405 
411 void init_rtc(void);
412 
413 #if (USE_TIMER_1)
419 void init_timer1(void);
420 
426 void start_timer(void);
427 
433 void stop_timer(void);
434 #endif
435 
441 void init_sensors(void);
442 
448 void print_configuration(void);
449 
455 void load_configuration(void);
456 
463 void save_configuration(bool);
464 
470 void commands(void);
471 
477 void reset_samples_buffer(void);
478 
484 void reset_observations_buffer(void);
485 
491 void exchange_buffers(void);
492 
499 void samples_processing(bool is_force_processing);
500 
506 void init_logging();
507 
508 
509 /*********************************************************************
510 * TASKS
511 *********************************************************************/
516 volatile bool is_event_sensors_reading;
517 
524 void sensors_reading_task(void);
525 
530 volatile bool is_event_command_task;
531 
538 void command_task(void);
539 
540 /*********************************************************************
541 * INTERRUPTS HANDLER
542 *********************************************************************/
549 
556 
557 #endif
#define I2C_MAX_DATA_LENGTH
Max length in bytes for i2c bus data buffer.
Definition: i2c_config.h:36
volatile bool is_event_command_task
Enable or disable the Command task.
Definition: i2c-leaf.h:502
volatile bool inside_transaction
Status of command transaction.
Definition: i2c-leaf.h:295
void load_configuration(void)
Load configuration from EEPROM.
Definition: i2c-leaf.ino:335
volatile uint8_t readable_data_length
Number of bytes to read at readable_data_address.
Definition: i2c-leaf.h:202
volatile uint8_t lastcommand
last command received.
Definition: i2c-leaf.h:214
bool is_start
Execute start command.
Definition: i2c-leaf.h:244
void init_tasks(void)
Init tasks variable and state.
Definition: i2c-leaf.ino:225
configuration_t configuration
Configuration data.
Definition: i2c-leaf.h:148
void init_system(void)
Init system.
Definition: i2c-leaf.ino:290
volatile uint8_t i2c_error
Number of i2c error.
Definition: i2c-leaf.h:220
void init_spi(void)
Init SPI library.
Definition: i2c-leaf.ino:262
writable_data_t writable_data
Writable i2c register.
Definition: i2c-leaf.h:184
void reset_samples_buffer(void)
Reset samples buffers to default value.
Definition: i2c-leaf.ino:549
writable_data_t * writable_data_ptr
Pointer for read and write data in i2c writable register.
Definition: i2c-leaf.h:190
void init_wire(void)
Init wire (i2c) library and performs checks on the bus.
Definition: i2c-leaf.ino:251
void i2c_receive_interrupt_handler(void)
I2C receive interrupt handler.
uint32_t awakened_event_occurred_time_ms
System time (in millisecond) when the system has awakened from power down.
Definition: i2c-leaf.h:238
void save_configuration(bool)
Save configuration to EEPROM.
Definition: i2c-leaf.ino:312
void init_rtc(void)
Init RTC module.
Definition: i2c-leaf.ino:266
volatile uint16_t transaction_time
Timer counter variable for compute command transaction timeout.
Definition: i2c-leaf.h:301
void init_wdt(uint8_t wdt_timer)
Init watchdog.
Definition: i2c-leaf.ino:202
void command_task(void)
Command Task. Execute the command received on i2c bus by reading i2c received data buffer.
Definition: i2c-leaf.ino:557
state_t
Main loop finite state machine.
Definition: i2c-leaf.h:100
@ INIT
init tasks and sensors
Definition: i2c-leaf.h:101
@ TASKS_EXECUTION
execute active tasks
Definition: i2c-leaf.h:105
@ END
go to ENTER_POWER_DOWN or TASKS_EXECUTION
Definition: i2c-leaf.h:106
void init_pins(void)
Init hardware pins.
Definition: i2c-leaf.ino:244
void commands(void)
Performs specific operations based on the received command.
Definition: i2c-leaf.ino:649
void exchange_buffers(void)
Exchange reader and writer pointer to buffer.
Definition: i2c-leaf.ino:535
void print_configuration(void)
Print current configuration.
Definition: i2c-leaf.ino:301
volatile readable_data_t readable_data_1
First readable i2c register.
Definition: i2c-leaf.h:154
volatile readable_data_t * readable_data_write_ptr
Pointer for write data in i2c readable register.
Definition: i2c-leaf.h:172
void samples_processing(bool is_force_processing)
Main routine for processing the samples to calculate an observation.
Definition: i2c-opc.ino:560
void i2c_request_interrupt_handler(void)
I2C request interrupt handler.
Definition: i2c-leaf.ino:395
bool is_stop
Execute stop command.
Definition: i2c-leaf.h:250
void init_buffers(void)
Init buffers.
Definition: i2c-leaf.ino:208
volatile uint8_t ready_tasks_count
Number of tasks ready to execute.
Definition: i2c-leaf.h:232
void init_sensors(void)
Create and setup sensors.
Definition: i2c-leaf.ino:353
void init_logging()
Init logging system.
Definition: i2c-leaf.ino:141
state_t state
Current main loop state.
Definition: i2c-leaf.h:283
volatile uint8_t i2c_rx_data[I2C_MAX_DATA_LENGTH]
Buffer for i2c received data.
Definition: i2c-leaf.h:208
volatile uint8_t readable_data_address
Address of readable i2c register.
Definition: i2c-leaf.h:196
volatile readable_data_t * readable_data_temp_ptr
Temporary pointer for exchange read and write pointer for i2c readable register.
Definition: i2c-leaf.h:178
volatile readable_data_t * readable_data_read_ptr
Pointer for read data in i2c readable register.
Definition: i2c-leaf.h:166
volatile readable_data_t readable_data_2
Second readable i2c register.
Definition: i2c-leaf.h:160
bool is_test_read
Received command is in continuous mode.
Definition: i2c-leaf.h:256
void init_power_down(uint32_t *time_ms, uint32_t debouncing_ms)
Enter power down mode.
Definition: i2c-leaf.ino:173
bool is_oneshot
Received command is in oneshot mode.
Definition: i2c-opc.h:340
bool is_continuous
Received command is in continuous mode.
Definition: i2c-opc.h:346
volatile uint16_t timer_counter
Timer counter variable for execute timed task with time multiple of base Timer1 time.
Definition: i2c-opc.h:397
void reset_observations_buffer()
Reset observations buffers to default value.
Definition: i2c-opc.ino:1286
sensors_reading_state_t
Sensors reading task finite state machine.
Definition: stima.h:234
@ SENSORS_READING_INIT
init task variables
Definition: stima.h:235
@ SENSORS_READING_IS_GETTED
check if the sensor has been readed
Definition: stima.h:240
@ SENSORS_READING_NEXT
go to next sensor
Definition: stima.h:242
@ SENSORS_READING_PREPARE
prepare sensor
Definition: stima.h:237
@ SENSORS_READING_GET
read and get values from sensor
Definition: stima.h:239
@ SENSORS_READING_IS_PREPARED
check if the sensor has been prepared
Definition: stima.h:238
@ SENSORS_READING_END
performs end operations and deactivate task
Definition: stima.h:243
@ SENSORS_READING_READ
intermediate state (future implementation...)
Definition: stima.h:241
@ SENSORS_READING_WAIT_STATE
non-blocking waiting time
Definition: stima.h:244
void init_timer1(void)
Init Timer1 module.
Definition: stima.ino:737
void sensors_reading_task(bool do_prepare=true, bool do_get=true, char *driver=NULL, char *type=NULL, uint8_t address=0, uint8_t node=0, uint8_t *sensor_index=0, uint32_t *wait_time=NULL)
Sensors reading Task. Read data from sensors.
Definition: stima.ino:2644
void start_timer(void)
Start Timer1 module.
Definition: stima.ino:741
void stop_timer(void)
Stop Timer1 module.
Definition: stima.ino:749
EEPROM saved configuration.
Definition: i2c-leaf.h:55
Readable data through i2c bus.
Definition: i2c-leaf.h:76
value_t temperature
temperature data for report
Definition: i2c-th.h:85
value_t humidity
humidity data for report
Definition: i2c-th.h:86
Samples values for measured opc data.
Definition: i2c-opc.h:156
int32_t * read_ptr
reader pointer
Definition: i2c-th.h:106
uint16_t count
samples counter
Definition: i2c-th.h:105
int32_t * write_ptr
writer pointer
Definition: i2c-th.h:107
Definition: i2c-th.h:56
uint8_t i2c_address
i2c address of sensor
Definition: i2c-th.h:58
Writable data through i2c bus.
Definition: i2c-leaf.h:87