-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathcore.cpp
More file actions
205 lines (195 loc) · 8.63 KB
/
core.cpp
File metadata and controls
205 lines (195 loc) · 8.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include "core.h"
#include "../global.h"
#include "../storage.h"
#include "../utils/bus_backup.h"
#include "../utils/string_utils.h"
#include "../utils/timing.h"
#include "../utils/uart.h"
#include "driver/gpio.h"
#include "esp_ota_ops.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "hal/gpio_hal.h"
#include "soc/io_mux_reg.h"
#include "soc/soc.h"
#include <memory>
#include <stdexcept>
#include <stdlib.h>
Core::Core(const std::string name) : Module(core, name) {
this->properties["debug"] = std::make_shared<BooleanVariable>(false);
this->properties["millis"] = std::make_shared<IntegerVariable>();
this->properties["heap"] = std::make_shared<IntegerVariable>();
this->properties["last_message_age"] = std::make_shared<IntegerVariable>();
}
void Core::step() {
this->properties.at("millis")->integer_value = millis();
this->properties.at("heap")->integer_value = xPortGetFreeHeapSize();
this->properties.at("last_message_age")->integer_value = millis_since(this->last_message_millis);
Module::step();
}
void Core::call(const std::string method_name, const std::vector<ConstExpression_ptr> arguments) {
if (method_name == "restart") {
Module::expect(arguments, 0);
esp_restart();
} else if (method_name == "version") {
const esp_app_desc_t *app_desc = esp_app_get_description();
echo("version: %s", app_desc->version);
} else if (method_name == "info") {
Module::expect(arguments, 0);
const esp_app_desc_t *app_desc = esp_app_get_description();
echo("lizard version: %s", app_desc->version);
echo("compile time: %s, %s", app_desc->date, app_desc->time);
echo("idf version: %s", app_desc->idf_ver);
} else if (method_name == "print") {
static char buffer[1024];
int pos = 0;
for (auto const &argument : arguments) {
if (argument != arguments[0]) {
pos += csprintf(&buffer[pos], sizeof(buffer) - pos, " ");
}
pos += argument->print_to_buffer(&buffer[pos], sizeof(buffer) - pos);
}
echo(buffer);
} else if (method_name == "output") {
Module::expect(arguments, 1, string);
this->output_list.clear();
std::string format = arguments[0]->evaluate_string();
while (!format.empty()) {
std::string element = cut_first_word(format);
if (element.find('.') == std::string::npos) {
// variable[:precision]
std::string variable_name = cut_first_word(element, ':');
const unsigned int precision = element.empty() ? 0 : atoi(element.c_str());
this->output_list.push_back({nullptr, variable_name, precision});
} else {
// module.property[:precision]
std::string module_name = cut_first_word(element, '.');
const ConstModule_ptr module = Global::get_module(module_name);
const std::string property_name = cut_first_word(element, ':');
const unsigned int precision = element.empty() ? 0 : atoi(element.c_str());
this->output_list.push_back({module, property_name, precision});
}
}
this->output_on = true;
} else if (method_name == "startup_checksum") {
uint16_t checksum = 0;
for (char const &c : Storage::startup) {
checksum += c;
}
echo("checksum: %04x", checksum);
} else if (method_name == "get_pin_status") {
Module::expect(arguments, 1, integer);
const int gpio_num = arguments[0]->evaluate_integer();
if (gpio_num < 0 || gpio_num >= GPIO_NUM_MAX) {
throw std::runtime_error("invalid pin");
}
bool pullup, pulldown, input_enabled, output_enabled, open_drain, sleep_sel_enabled;
uint32_t drive_strength, func_sel, signal_output;
static gpio_hal_context_t _gpio_hal = {.dev = GPIO_HAL_GET_HW(GPIO_PORT_0)};
gpio_hal_get_io_config(&_gpio_hal, gpio_num, &pullup, &pulldown, &input_enabled, &output_enabled,
&open_drain, &drive_strength, &func_sel, &signal_output, &sleep_sel_enabled);
const int gpio_level = gpio_get_level(static_cast<gpio_num_t>(gpio_num));
echo("GPIO_Status[%d]| Level: %d| InputEn: %d| OutputEn: %d| OpenDrain: %d| Pullup: %d| Pulldown: %d| "
"DriveStrength: %d| SleepSel: %d",
gpio_num, gpio_level, input_enabled, output_enabled, open_drain, pullup, pulldown,
drive_strength, sleep_sel_enabled);
} else if (method_name == "set_pin_level") {
Module::expect(arguments, 2, integer, integer);
const int gpio_num = arguments[0]->evaluate_integer();
if (gpio_num < 0 || gpio_num >= GPIO_NUM_MAX) {
throw std::runtime_error("invalid pin");
}
const int value = arguments[1]->evaluate_integer();
if (value < 0 || value > 1) {
throw std::runtime_error("invalid value");
}
gpio_config_t io_conf;
io_conf.mode = GPIO_MODE_OUTPUT;
io_conf.pin_bit_mask = (1ULL << gpio_num);
io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
io_conf.pull_up_en = GPIO_PULLUP_DISABLE;
io_conf.intr_type = GPIO_INTR_DISABLE;
gpio_config(&io_conf);
const esp_err_t err = gpio_set_level(static_cast<gpio_num_t>(gpio_num), value);
if (err != ESP_OK) {
throw std::runtime_error("failed to set pin");
}
echo("GPIO_set[%d] set to %d", gpio_num, value);
} else if (method_name == "get_pin_strapping") {
Module::expect(arguments, 1, integer);
const gpio_num_t gpio_num = static_cast<gpio_num_t>(arguments[0]->evaluate_integer());
if (gpio_num < 0 || gpio_num >= GPIO_NUM_MAX) {
throw std::runtime_error("invalid pin");
}
const uint32_t strapping_reg = REG_READ(GPIO_STRAP_REG);
// Register 4.13. GPIO_STRAP_REG (0x0038)
// https://www.espressif.com/sites/default/files/documentation/esp32_technical_reference_manual_en.pdf
switch (gpio_num) {
case GPIO_NUM_0:
echo("Strapping GPIO0: %d", (strapping_reg & BIT(0)) ? 1 : 0);
break;
case GPIO_NUM_2:
echo("Strapping GPIO2: %d", (strapping_reg & BIT(1)) ? 1 : 0);
break;
case GPIO_NUM_4:
echo("Strapping GPIO4: %d", (strapping_reg & BIT(5)) ? 1 : 0);
break;
case GPIO_NUM_5:
echo("Strapping GPIO5: %d", (strapping_reg & BIT(4)) ? 1 : 0);
break;
case GPIO_NUM_12:
echo("Strapping GPIO12 (MTDI): %d", (strapping_reg & BIT(3)) ? 1 : 0);
break;
case GPIO_NUM_15:
echo("Strapping GPIO15 (MTDO): %d", (strapping_reg & BIT(2)) ? 1 : 0);
break;
default:
echo("Not a strapping pin");
break;
}
} else if (method_name == "forget_serial_bus") {
Module::expect(arguments, 0);
bus_backup::remove();
} else if (method_name == "pause_broadcasts") {
Module::expect(arguments, 0);
Module::broadcast_paused = true;
echo("broadcasts paused");
} else if (method_name == "resume_broadcasts") {
Module::expect(arguments, 0);
Module::broadcast_paused = false;
echo("broadcasts resumed");
} else {
Module::call(method_name, arguments);
}
}
std::string Core::get_output() const {
static char output_buffer[1024];
int pos = 0;
for (auto const &element : this->output_list) {
if (pos > 0) {
pos += csprintf(&output_buffer[pos], sizeof(output_buffer) - pos, " ");
}
const Variable_ptr variable =
element.module ? element.module->get_property(element.property_name) : Global::get_variable(element.property_name);
switch (variable->type) {
case boolean:
pos += csprintf(&output_buffer[pos], sizeof(output_buffer) - pos, "%s", variable->boolean_value ? "true" : "false");
break;
case integer:
pos += csprintf(&output_buffer[pos], sizeof(output_buffer) - pos, "%lld", variable->integer_value);
break;
case number:
pos += csprintf(&output_buffer[pos], sizeof(output_buffer) - pos, "%.*f", element.precision, variable->number_value);
break;
case string:
pos += csprintf(&output_buffer[pos], sizeof(output_buffer) - pos, "\"%s\"", variable->string_value.c_str());
break;
default:
throw std::runtime_error("invalid type");
}
}
return std::string(output_buffer);
}
void Core::keep_alive() {
this->last_message_millis = millis();
}