STIMA  3
i2c-rain.h
Go to the documentation of this file.
1 
3 /*********************************************************************
4 Copyright (C) 2017 Marco Baldinetti <m.baldinetti@digiteco.it>
5 authors:
6 Paolo patruno <p.patruno@iperbole.bologna.it>
7 Marco Baldinetti <m.baldinetti@digiteco.it>
8 
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2 of
12 the License, or (at your option) any later version.
13 
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 **********************************************************************/
22 
23 #ifndef _I2C_RAIN_H
24 #define _I2C_RAIN_H
25 
26 #include "i2c-rain-config.h"
27 #include <debug.h>
28 #include <i2c_config.h>
29 #include <avr/sleep.h>
30 #include <avr/power.h>
31 #include <avr/wdt.h>
32 #include <i2c_utility.h>
33 #include <rmap_utility.h>
34 #include <sdcard_utility.h>
35 #if (USE_JSON)
36 #include <json_utility.h>
37 #endif
38 #include <eeprom_utility.h>
39 #include <Wire.h>
40 #include <typedef.h>
41 #include <registers-rain.h>
42 #include <debug_config.h>
43 #include <SdFat.h>
44 #include <StreamUtils.h>
45 #include <ArduinoLog.h>
46 
47 /*********************************************************************
48 * TYPEDEF
49 *********************************************************************/
54 typedef struct {
55  uint8_t module_main_version;
56  uint8_t module_configuration_version;
57  uint8_t module_type;
58  uint8_t i2c_address;
59  bool is_oneshot;
61  uint8_t rain_for_tip;
63 
68 typedef struct {
69  uint8_t module_type;
70  uint8_t module_main_version;
71  uint8_t module_minor_version;
72  rain_t rain;
74 
79 typedef struct {
80  uint8_t i2c_address;
81  bool is_oneshot;
83  uint8_t rain_for_tip;
85 
86 /*********************************************************************
87 * TYPEDEF for Finite State Machine
88 *********************************************************************/
93 typedef enum {
94  INIT,
95  #if (USE_POWER_DOWN)
96  ENTER_POWER_DOWN,
97  #endif
99  END
101 
106 typedef enum {
113 
114 /*********************************************************************
115 * GLOBAL VARIABLE
116 *********************************************************************/
122 
128 
134 
140 
146 
152 
158 
164 
169 volatile uint8_t readable_data_address;
170 
175 volatile uint8_t readable_data_length;
176 
181 volatile uint8_t i2c_rx_data[I2C_MAX_DATA_LENGTH];
182 
187 volatile uint8_t lastcommand;
188 
193 volatile uint8_t i2c_error;
194 
199 volatile uint8_t i2c_time;
200 
205 volatile uint8_t ready_tasks_count;
206 
212 
217 volatile bool inside_transaction;
218 
223 volatile uint16_t transaction_time;
224 
229 bool is_start;
230 
235 bool is_stop;
236 
241 bool is_test;
242 
248 
253 rain_t rain;
254 
259 state_t state;
260 
266 
267 #if (ENABLE_SDCARD_LOGGING)
272 SdFat SD;
273 
278 File logFile;
279 
284 WriteLoggingStream loggingStream(logFile,Serial);
285 #endif
286 
287 /*********************************************************************
288 * FUNCTIONS
289 *********************************************************************/
297 void init_power_down(uint32_t *time_ms, uint32_t debouncing_ms);
298 
305 void init_wdt(uint8_t wdt_timer);
306 
312 void init_system(void);
313 
319 void init_buffers(void);
320 
326 void init_tasks(void);
327 
333 void init_pins(void);
334 
340 void init_wire(void);
341 
347 void init_spi(void);
348 
354 void init_rtc(void);
355 
356 #if (USE_TIMER_1)
362 void init_timer1(void);
363 
369 void init_tipping_bucket_interrupt(void);
370 
376 void start_timer(void);
377 
383 void stop_timer(void);
384 #endif
385 
391 void init_sensors(void);
392 
398 void print_configuration(void);
399 
405 void load_configuration(void);
406 
413 void save_configuration(bool);
414 
420 void commands(void);
421 
427 void tests(void);
428 
434 void reset_buffers(void);
435 
441 void exchange_buffers(void);
442 
448 void init_logging();
449 
450 /*********************************************************************
451 * TASKS
452 *********************************************************************/
458 
464 void tipping_bucket_task(void);
465 
470 volatile bool is_event_command_task;
471 
477 void command_task(void);
478 
479 /*********************************************************************
480 * INTERRUPTS HANDLER
481 *********************************************************************/
488 
494 void i2c_receive_interrupt_handler(int rx_data_length);
495 
501 
502 #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
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
configuration_t configuration
Configuration data.
Definition: i2c-leaf.h:148
volatile uint8_t i2c_error
Number of i2c error.
Definition: i2c-leaf.h:220
writable_data_t writable_data
Writable i2c register.
Definition: i2c-leaf.h:184
volatile uint8_t i2c_time
Time in seconds from last I2C reset.
Definition: i2c-leaf.h:226
writable_data_t * writable_data_ptr
Pointer for read and write data in i2c writable register.
Definition: i2c-leaf.h:190
uint32_t awakened_event_occurred_time_ms
System time (in millisecond) when the system has awakened from power down.
Definition: i2c-leaf.h:238
volatile uint16_t transaction_time
Timer counter variable for compute command transaction timeout.
Definition: i2c-leaf.h:301
state_t
Main loop finite state machine.
Definition: i2c-leaf.h:100
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
bool is_stop
Execute stop command.
Definition: i2c-leaf.h:250
volatile uint8_t ready_tasks_count
Number of tasks ready to execute.
Definition: i2c-leaf.h:232
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_oneshot
Received command is in oneshot mode.
Definition: i2c-opc.h:340
void load_configuration(void)
Load configuration from EEPROM.
Definition: i2c-leaf.ino:335
volatile bool is_event_tipping_bucket
Enable or disable the Tipping Bucket task.
Definition: i2c-rain.h:457
void init_tasks(void)
Init tasks variable and state.
Definition: i2c-leaf.ino:225
void init_system(void)
Init system.
Definition: i2c-leaf.ino:290
bool is_started
Start command is active and measure is active.
Definition: i2c-rain.h:247
void init_spi(void)
Init SPI library.
Definition: i2c-leaf.ino:262
rain_t rain
Rain data.
Definition: i2c-rain.h:253
void i2c_receive_interrupt_handler(int rx_data_length)
I2C receive interrupt handler.
Definition: i2c-leaf.ino:409
void reset_buffers(void)
Reset buffers to default value.
Definition: i2c-rain.ino:569
tipping_bucket_state_t
Tipping bucket task finite state machine.
Definition: i2c-rain.h:106
@ TIPPING_BUCKET_INIT
init task variables and wait half debounce time
Definition: i2c-rain.h:107
@ TIPPING_BUCKET_END
wait for the tipping bucket switch return open and performs end operations and deactivate task
Definition: i2c-rain.h:110
@ TIPPING_BUCKET_READ
read rain tips from variable shared with tipping bucket interrupt, validate it for spike re-reading t...
Definition: i2c-rain.h:108
@ TIPPING_BUCKET_WAIT_STATE
non-blocking waiting time
Definition: i2c-rain.h:111
@ TIPPING_BUCKET_COUNT
validate re-reading tipping bucket signal and increment total tips
Definition: i2c-rain.h:109
void init_wire(void)
Init wire (i2c) library and performs checks on the bus.
Definition: i2c-leaf.ino:251
void tipping_bucket_interrupt_handler(void)
Tipping bucket interrupt handler.
Definition: i2c-rain.ino:391
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
void tipping_bucket_task(void)
Tipping bucket task.
Definition: i2c-rain.ino:491
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-rain.h:93
@ INIT
init tasks and sensors
Definition: i2c-rain.h:94
@ TASKS_EXECUTION
execute active tasks
Definition: i2c-rain.h:98
@ END
go to ENTER_POWER_DOWN or TASKS_EXECUTION
Definition: i2c-rain.h:99
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
void i2c_request_interrupt_handler(void)
I2C request interrupt handler.
Definition: i2c-leaf.ino:395
tipping_bucket_state_t tipping_bucket_state
Current tipping bucket task state.
Definition: i2c-rain.h:265
void init_buffers(void)
Init buffers.
Definition: i2c-leaf.ino:208
void init_sensors(void)
Create and setup sensors.
Definition: i2c-leaf.ino:353
void init_logging()
Init logging system.
Definition: i2c-leaf.ino:141
void init_power_down(uint32_t *time_ms, uint32_t debouncing_ms)
Enter power down mode.
Definition: i2c-leaf.ino:173
void tests(void)
Performs specific operations based on the received command.
Definition: i2c-opc.ino:1400
void init_timer1(void)
Init Timer1 module.
Definition: stima.ino:737
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
uint8_t rain_for_tip
How much mm of rain for one tip of tipping bucket rain gauge.
Definition: i2c-rain.h:61
uint16_t tipping_bucket_time_ms
Tipping bucket time in milliseconds.
Definition: i2c-rain.h:60
Readable data through i2c bus.
Definition: i2c-leaf.h:76
rain_t rain
rain data
Definition: i2c-rain.h:72
Writable data through i2c bus.
Definition: i2c-leaf.h:87
uint8_t rain_for_tip
How much mm of rain for one tip of tipping bucket rain gauge.
Definition: i2c-rain.h:83
uint16_t tipping_bucket_time_ms
Tipping bucket time in milliseconds.
Definition: i2c-rain.h:82