RocketLogger  2.0.0
rl_hw.c
Go to the documentation of this file.
1 
32 #include <errno.h>
33 #include <stdio.h>
34 #include <string.h>
35 
36 #include "calibration.h"
37 #include "gpio.h"
38 #include "log.h"
39 #include "pru.h"
40 #include "rl.h"
41 #include "rl_file.h"
42 #include "sensor/sensor.h"
43 
44 #include "rl_hw.h"
45 
46 gpio_t *gpio_fhr1 = NULL;
47 gpio_t *gpio_fhr2 = NULL;
50 
51 void hw_init(rl_config_t const *const config) {
52  // GPIO configuration
53  gpio_init();
54  // force high range (negative enable)
55  gpio_fhr1 = gpio_setup(GPIO_FHR1, GPIO_MODE_OUT, "rocketlogger");
56  gpio_fhr2 = gpio_setup(GPIO_FHR2, GPIO_MODE_OUT, "rocketlogger");
57  gpio_set_value(gpio_fhr1, (config->channel_force_range[0] ? 0 : 1));
58  gpio_set_value(gpio_fhr2, (config->channel_force_range[1] ? 0 : 1));
59  // leds
61  gpio_setup(GPIO_LED_STATUS, GPIO_MODE_OUT, "rocketlogger");
65 
66  // PRU
67  pru_init();
68 
69  // SENSORS (if enabled)
70  if (config->ambient_enable) {
71  sensors_init();
73  }
74 
75  // STATE
76  if (config->file_enable) {
77  // calculate disk use rate in bytes per second:
78  // - int32_t/channel + uint32_t bytes/sample for digital at sample rate
79  // - 2 timestamp at update rate
80  // - int32_t/sensor channel + 2 timestamps at 1 Hz
82  (sizeof(int32_t) * count_channels(config->channel_enable) *
83  config->sample_rate) +
84  (sizeof(rl_timestamp_t) * 2 * config->update_rate) +
85  (sizeof(uint32_t) * (config->digital_enable ? 1 : 0) *
86  config->sample_rate) +
87  (sizeof(int32_t) * rl_status.sensor_count) +
88  (sizeof(rl_timestamp_t) * 2 * (rl_status.sensor_count > 0 ? 1 : 0));
89  }
91 }
92 
93 void hw_deinit(rl_config_t const *const config) {
94 
95  // GPIO (set to default state only, (un)export is handled by daemon)
96  // reset force high range GPIOs to force high range (negative enable)
99 
100  // reset status LED, leave error LED in current state
102 
107  gpio_deinit();
108 
109  // PRU
110  // stop first if running in background
111  if (config->background_enable) {
112  pru_stop();
113  }
114  pru_deinit();
115 
116  // SENSORS (if enabled)
117  if (config->ambient_enable) {
119  sensors_deinit();
120  }
121 
122  // RESET sampling specific state
125 }
126 
127 int hw_sample(rl_config_t const *const config) {
128  int ret;
129  FILE *data_file = (FILE *)-1;
130  FILE *ambient_file = (FILE *)-1;
131 
132  // reset calibration if ignored, load otherwise
133  if (config->calibration_ignore) {
136  } else {
137  ret = calibration_load();
138  if (ret < 0) {
140  "no calibration file, returning uncalibrated values");
141  }
142  }
143 
144  // create/open measurement files
145  if (config->file_enable) {
146  data_file = fopen64(config->file_name, "w+");
147  if (data_file == NULL) {
149  "failed to open data file '%s'; %d message: %s",
150  config->file_name, errno, strerror(errno));
151  return ERROR;
152  }
153  }
154 
155  if (config->ambient_enable) {
156  char *ambient_file_name =
158  ambient_file = fopen64(ambient_file_name, "w+");
159  if (data_file == NULL) {
161  "failed to open ambient file '%s'; %d message: %s",
162  ambient_file, errno, strerror(errno));
163  return ERROR;
164  }
165  }
166 
167  // SAMPLE
168  ret = pru_sample(data_file, ambient_file, config);
169  if (ret < 0) {
170  // error occurred
172  }
173 
174  // close data files
175  if (config->file_enable) {
176  fclose(data_file);
177  }
178  if (config->ambient_enable) {
179  fclose(ambient_file);
180  }
181 
182  return SUCCESS;
183 }
void calibration_reset_offsets(void)
Definition: calibration.c:45
int calibration_load(void)
Definition: calibration.c:57
void calibration_reset_scales(void)
Definition: calibration.c:51
gpio_t * gpio_setup(int gpio_number, gpio_mode_t mode, const char *name)
Definition: gpio.c:103
int gpio_set_value(gpio_t *gpio, int value)
Definition: gpio.c:135
int gpio_init()
Definition: gpio.c:77
void gpio_release(gpio_t *gpio)
Definition: gpio.c:130
void gpio_deinit()
Definition: gpio.c:92
@ GPIO_MODE_OUT
GPIO write mode.
Definition: gpio.h:56
#define GPIO_FHR2
GPIO number for forcing I2 high.
Definition: gpio.h:41
struct gpiod_line gpio_t
Definition: gpio.h:72
#define GPIO_LED_STATUS
GPIO number of status LED.
Definition: gpio.h:43
#define GPIO_FHR1
GPIO number for forcing I1 high.
Definition: gpio.h:39
#define GPIO_LED_ERROR
GPIO number of error LED.
Definition: gpio.h:45
int rl_log(rl_log_level_t log_level, char const *const format,...)
Definition: log.c:82
@ RL_LOG_ERROR
Error.
Definition: log.h:49
@ RL_LOG_WARNING
Warning.
Definition: log.h:50
int pru_sample(FILE *data_file, FILE *ambient_file, rl_config_t const *const config)
Definition: pru.c:130
void pru_deinit(void)
Definition: pru.c:74
void pru_stop(void)
Definition: pru.c:615
int pru_init(void)
Definition: pru.c:57
int rl_status_write(rl_status_t *const status)
Definition: rl.c:641
#define ERROR
Function return value for errors (use errno to indicate the error)
Definition: rl.h:46
#define SUCCESS
Function return value for successful completion.
Definition: rl.h:44
char * rl_file_get_ambient_file_name(char const *const data_file_name)
Definition: rl_file.c:95
void hw_deinit(rl_config_t const *const config)
Definition: rl_hw.c:93
gpio_t * gpio_fhr2
Definition: rl_hw.c:47
void hw_init(rl_config_t const *const config)
Definition: rl_hw.c:51
gpio_t * gpio_led_error
Definition: rl_hw.c:49
gpio_t * gpio_fhr1
Definition: rl_hw.c:46
gpio_t * gpio_led_status
Definition: rl_hw.c:48
int hw_sample(rl_config_t const *const config)
Definition: rl_hw.c:127
int sensors_scan(bool sensor_available[SENSOR_REGISTRY_SIZE])
Definition: sensor.c:156
int sensors_init(void)
Definition: sensor.c:121
void sensors_close(bool const sensor_available[SENSOR_REGISTRY_SIZE])
Definition: sensor.c:222
void sensors_deinit(void)
Definition: sensor.c:126
Definition: rl.h:154
bool channel_force_range[RL_CHANNEL_SWITCHED_COUNT]
Current channels to force to high range.
Definition: rl.h:170
char file_name[PATH_MAX]
Data file name.
Definition: rl.h:184
bool background_enable
Put the measurement process in background after successful start.
Definition: rl.h:158
bool ambient_enable
Enable logging of ambient sensor.
Definition: rl.h:180
uint32_t sample_rate
Sampling rate.
Definition: rl.h:164
bool channel_enable[RL_CHANNEL_COUNT]
Channels to sample.
Definition: rl.h:168
uint32_t update_rate
Data update rate.
Definition: rl.h:166
bool file_enable
Enable storing measurements to file.
Definition: rl.h:182
bool digital_enable
Enable digital inputs.
Definition: rl.h:174
bool calibration_ignore
Perform calibration measurement (ignore existing calibration)
Definition: rl.h:178
Definition: rl.h:201
uint32_t disk_use_rate
Disk space in bytes required per minute when sampling.
Definition: rl.h:219
uint16_t sensor_count
Number of sensors found connected to the system.
Definition: rl.h:221
bool sensor_available[RL_SENSOR_COUNT_MAX]
Identifiers of sensors found.
Definition: rl.h:223
int count_channels(bool const channels[RL_CHANNEL_COUNT])
Definition: util.c:72
struct rl_timestamp rl_timestamp_t
Definition: util.h:59