STIMA  3
i2c-radiation.h
1 /*********************************************************************
2 Copyright (C) 2022 Marco Baldinetti <m.baldinetti@digiteco.it>
3 authors:
4 Paolo patruno <p.patruno@iperbole.bologna.it>
5 Marco Baldinetti <m.baldinetti@digiteco.it>
6 
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of
10 the License, or (at your option) any later version.
11 
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 **********************************************************************/
20 
21 #ifndef _I2C_SOLAR_RADIATION_H
22 #define _I2C_SOLAR_RADIATION_H
23 
24 #include "i2c-radiation-config.h"
25 #include <debug.h>
26 #include <i2c_config.h>
27 #include <avr/sleep.h>
28 #include <avr/power.h>
29 #include <avr/wdt.h>
30 #include <i2c_utility.h>
31 #include <rmap_utility.h>
32 #include <sdcard_utility.h>
33 #if (USE_JSON)
34 #include <json_utility.h>
35 #endif
36 #include <eeprom_utility.h>
37 #include <Wire.h>
38 #include <TimeLib.h>
39 #include <typedef.h>
40 #include <ADS1115.h>
41 #include <registers-radiation.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;
60 
61  // 10 bit
62  float adc_voltage_offset_1;
63  float adc_voltage_offset_2;
64  float adc_voltage_min;
65  float adc_voltage_max;
66 
67  // 16 bit HR
68  float adc_calibration_offset[ADS1115_CHANNEL_COUNT];
69  float adc_calibration_gain[ADS1115_CHANNEL_COUNT];
70  float sensor_rad_max[ADS1115_CHANNEL_COUNT];
71  float sensor_voltage_max[ADS1115_CHANNEL_COUNT];
73 
78 typedef struct {
79  int16_t sample;
80  int16_t avg;
81 } data_t;
82 
87 typedef struct {
88  uint8_t module_type;
89  uint8_t module_main_version;
90  uint8_t module_minor_version;
91  data_t solar_radiation;
93 
98 typedef struct {
99  uint8_t i2c_address;
100  bool is_oneshot;
101 
102  /*
103  // 10 bit
104  float adc_voltage_offset_1;
105  float adc_voltage_offset_2;
106  float adc_voltage_min;
107  float adc_voltage_max;
108  */
109 
110  // 16 bit hr
111  float adc_calibration_offset[ADS1115_CHANNEL_COUNT];
112  float adc_calibration_gain[ADS1115_CHANNEL_COUNT];
113  float sensor_voltage_max[ADS1115_CHANNEL_COUNT];
114  float sensor_rad_max[ADS1115_CHANNEL_COUNT];
116 
117 /*********************************************************************
118 * TYPEDEF for Finite State Machine
119 *********************************************************************/
124 typedef enum {
125  INIT,
126  #if (USE_POWER_DOWN)
127  ENTER_POWER_DOWN,
128  #endif
130  END
131 } state_t;
132 
137 typedef enum {
138  SOLAR_RADIATION_INIT,
139  SOLAR_RADIATION_READING,
140  SOLAR_RADIATION_ELABORATE,
141  SOLAR_RADIATION_END,
142  SOLAR_RADIATION_WAIT_STATE
143 } solar_radiation_state_t;
144 
149 typedef enum {
150  SOLAR_RADIATION_HR_INIT,
151  SOLAR_RADIATION_HR_READ,
152  SOLAR_RADIATION_HR_EVALUATE,
153  SOLAR_RADIATION_HR_PROCESS,
154  SOLAR_RADIATION_HR_END,
155  SOLAR_RADIATION_HR_WAIT_STATE
156 } solar_radiation_hr_state_t;
157 
158 /*********************************************************************
159 * GLOBAL VARIABLE
160 *********************************************************************/
161 
162 #if (ENABLE_SDCARD_LOGGING)
167 SdFat SD;
168 
173 File logFile;
174 
179 WriteLoggingStream loggingStream(logFile,Serial);
180 #endif
181 
187 
193 
199 
205 
211 
217 
223 
229 
234 volatile uint8_t readable_data_address;
235 
240 volatile uint8_t readable_data_length;
241 
246 volatile uint8_t i2c_rx_data[I2C_MAX_DATA_LENGTH];
247 
252 volatile uint8_t i2c_error;
253 
258 volatile uint8_t ready_tasks_count;
259 
265 
270 volatile bool inside_transaction;
271 
276 volatile uint16_t transaction_time;
277 
282 volatile uint8_t lastcommand;
283 
288 bool is_start;
289 
294 bool is_stop;
295 
300 bool is_test_read;
301 
306 bool is_oneshot;
307 
312 bool is_continuous;
313 
318 bool is_test;
319 
320 int16_t sample;
321 int16_t average;
322 
323 
324 #if (USE_SENSOR_VSR)
325 ADS1115 adc1(ADC_I2C_ADDRESS);
326 float getAdcCalibratedValue (float adc_value, float offset, float gain);
327 float getAdcAnalogValue (float adc_value, float min, float max);
328 adsGain_t adsgain;
329 #endif
330 
331 uint8_t solar_radiation_acquisition_count;
332 
337 uint16_t samples_count;
338 
343 uint16_t samples_error_count;
344 
349 volatile uint16_t timer_counter_ms;
350 volatile uint16_t timer_counter_s;
351 
352 #if (USE_SENSOR_DSR || USE_SENSOR_VSR)
353 volatile bool is_solar_radiation_on;
354 #endif
355 
360 state_t state;
361 
366 solar_radiation_state_t solar_radiation_state;
367 
368 solar_radiation_hr_state_t solar_radiation_hr_state;
369 
370 /*********************************************************************
371 * FUNCTIONS
372 *********************************************************************/
373 
379 void init_logging();
380 
388 void init_power_down(uint32_t *time_ms, uint32_t debouncing_ms);
389 
396 void init_wdt(uint8_t wdt_timer);
397 
403 void init_system(void);
404 
410 void init_buffers(void);
411 
417 void init_tasks(void);
418 
424 void init_pins(void);
425 
431 void init_wire(void);
432 
438 void init_spi(void);
439 
445 void init_adc(void);
446 
452 void init_rtc(void);
453 
454 #if (USE_TIMER_1)
460 void init_timer1(void);
461 
467 void start_timer(void);
468 
474 void stop_timer(void);
475 #endif
476 
482 void init_sensors(void);
483 
489 void print_configuration(void);
490 
496 void load_configuration(void);
497 
504 void save_configuration(bool);
505 
511 void commands(void);
512 
518 void tests(void);
519 
526 void reset_buffer(void);
527 
533 void exchange_buffers(void);
534 
540 void make_report (bool init=false);
541 
542 
543 #if (USE_SENSOR_DSR)
544 #define solarRadiationRead() (analogRead(SOLAR_RADIATION_ANALOG_PIN))
545 #define isSolarRadiationOn() (is_solar_radiation_on)
546 #define isSolarRadiationOff() (!is_solar_radiation_on)
547 
548 void solarRadiationPowerOff(void);
549 void solarRadiationPowerOn(void);
550 void solaRadiationOffset(uint8_t count, uint8_t delay_ms, float *offset, float ideal);
551 float getSolarRadiationMv(float adc_value, float offset_mv);
552 float getSolarRadiation(float adc_value);
553 #endif
554 
555 #if (USE_SENSOR_VSR)
556 void solarRadiationPowerOff(void);
557 void solarRadiationPowerOn(void);
558 void solaRadiationOffset(uint8_t count, uint8_t delay_ms, float *offset, float ideal);
559 float getSolarRadiation(float adc_value, float adc_voltage_min, float adc_voltage_max);
560 #endif
561 
562 /*********************************************************************
563 * TASKS
564 *********************************************************************/
565 
570 volatile bool is_event_solar_radiation_task;
571 
578 void solar_radiation_task(void);
579 
586 void solar_radiation_task_hr(void);
587 
592 volatile bool is_event_command_task;
593 
600 void command_task(void);
601 
602 /*********************************************************************
603 * INTERRUPTS HANDLER
604 *********************************************************************/
611 
618 
619 #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
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
uint8_t samples_count
Number of samples to be acquired for make one observation.
Definition: i2c-leaf.h:265
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
volatile uint16_t timer_counter_ms
Timer counter variable for execute timed task with time multiple of base Timer1 time.
Definition: i2c-leaf.h:277
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
void reset_buffer(void)
Reset sample and observations buffers to default value.
Definition: i2c-thr.ino:1252
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
void tests()
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
Definition: i2c-power.h:68
Readable data through i2c bus.
Definition: i2c-leaf.h:76
Writable data through i2c bus.
Definition: i2c-leaf.h:87