Skip to content

Commit bdcab9c

Browse files
authored
ci: add static analysis action (#137)
* ci: add static analysis action (need to figure out / make a good build target for it * suppress cstyleCast * fix: static analysis findings * fix: builds * fix sa * ignore magic_enum.hpp in static analysis * add maybe_unused to variable in generated states; update config in ads1x15 * update to suppress magic_enum as well? * doc: rebuild * WIP fixing magic_enum SA erorrs * replace magic_enum header in state_machine include folder with magic_enum external git submodule (also updated from magic enum 0.8 to 0.9.5 (master) * remove unused config in static analysis (esp. since with the magic_enum update the SA doesnt take more than a minute or two :) )
1 parent af0852f commit bdcab9c

File tree

163 files changed

+809
-2196
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

163 files changed

+809
-2196
lines changed

.github/workflows/static_analysis.yml

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: Static analysis
2+
3+
on: [pull_request]
4+
5+
jobs:
6+
static_analysis:
7+
runs-on: ubuntu-latest
8+
9+
steps:
10+
- name: Checkout repo
11+
uses: actions/checkout@v2
12+
with:
13+
submodules: 'recursive'
14+
15+
- name: Run static analysis
16+
uses: esp-cpp/StaticAnalysis@master
17+
with:
18+
# Do not build the project and do not use cmake to generate compile_commands.json
19+
use_cmake: false
20+
21+
# Use the 5.2 release version since it's what we build with
22+
esp_idf_version: release/v5.2
23+
24+
# (Optional) cppcheck args
25+
cppcheck_args: -i$GITHUB_WORKSPACE/lib -i$GITHUB_WORKSPACE/external -i$GITHUB_WORKSPACE/components/esp_littlefs -i$GITHUB_WORKSPACE/components/lvgl -i$GITHUB_WORKSPACE/components/esp-dsp --force --enable=all --inline-suppr --inconclusive --platform=mips32 --suppressions-list=$GITHUB_WORKSPACE/suppressions.txt

.gitmodules

+3
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,6 @@
2525
[submodule "lib/pybind11"]
2626
path = lib/pybind11
2727
url = [email protected]:pybind/pybind11
28+
[submodule "external/magic_enum"]
29+
path = external/magic_enum
30+
url = [email protected]:neargye/magic_enum

components/adc/include/continuous_adc.hpp

+19-21
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <atomic>
44
#include <optional>
55
#include <unordered_map>
6+
#include <vector>
67

78
#include "esp_adc/adc_cali.h"
89
#include "esp_adc/adc_cali_scheme.h"
@@ -48,17 +49,15 @@ class ContinuousAdc {
4849
* @brief Initialize and start the continuous adc reader.
4950
* @param config Config used to initialize the reader.
5051
*/
51-
ContinuousAdc(const Config &config)
52+
explicit ContinuousAdc(const Config &config)
5253
: sample_rate_hz_(config.sample_rate_hz), window_size_bytes_(config.window_size_bytes),
5354
num_channels_(config.channels.size()), conv_mode_(config.convert_mode),
55+
result_data_(window_size_bytes_, 0xcc),
5456
logger_({.tag = "Continuous Adc",
5557
.rate_limit = std::chrono::milliseconds(100),
5658
.level = config.log_level}) {
5759
// initialize the adc continuous subsystem
5860
init(config.channels);
59-
// allocate memory for the task to store result data from DMA
60-
result_data_ = new uint8_t[window_size_bytes_];
61-
memset(result_data_, 0xcc, window_size_bytes_);
6261
// and start the task
6362
using namespace std::placeholders;
6463
task_ =
@@ -78,12 +77,11 @@ class ContinuousAdc {
7877
vTaskNotifyGiveFromISR(task_handle_, &mustYield);
7978
// then stop the task
8079
task_->stop();
81-
delete[] result_data_;
8280
stop();
8381
ESP_ERROR_CHECK(adc_continuous_deinit(handle_));
8482
// clean up the calibration data
85-
for (auto &config : configs_) {
86-
auto id = get_id(config);
83+
for (const auto &config : configs_) {
84+
[[maybe_unused]] auto id = get_id(config);
8785
#if ADC_CALI_SCHEME_CURVE_FITTING_SUPPORTED
8886
ESP_ERROR_CHECK(adc_cali_delete_scheme_curve_fitting(cali_handles_[id]));
8987
#elif ADC_CALI_SCHEME_LINE_FITTING_SUPPORTED
@@ -173,14 +171,14 @@ class ContinuousAdc {
173171
// wait until conversion is ready (will be notified by the registered
174172
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
175173
auto current_timestamp = std::chrono::high_resolution_clock::now();
176-
for (auto &config : configs_) {
174+
for (const auto &config : configs_) {
177175
auto id = get_id(config);
178176
sums_[id] = 0;
179177
num_samples_[id] = 0;
180178
}
181179
esp_err_t ret;
182180
uint32_t ret_num = 0;
183-
ret = adc_continuous_read(handle_, result_data_, window_size_bytes_, &ret_num, 0);
181+
ret = adc_continuous_read(handle_, result_data_.data(), window_size_bytes_, &ret_num, 0);
184182
if (ret == ESP_ERR_TIMEOUT) {
185183
// this is ok, we read faster than the hardware could give us data
186184
return false;
@@ -341,21 +339,20 @@ class ContinuousAdc {
341339
bool calibrated = false;
342340

343341
#if ADC_CALI_SCHEME_CURVE_FITTING_SUPPORTED
344-
if (!calibrated) {
345-
logger_.info("calibration scheme version is {}", "Curve Fitting");
346-
adc_cali_curve_fitting_config_t cali_config = {
347-
.unit_id = unit,
348-
.atten = atten,
349-
.bitwidth = bitwidth,
350-
};
351-
ret = adc_cali_create_scheme_curve_fitting(&cali_config, &handle);
352-
if (ret == ESP_OK) {
353-
calibrated = true;
354-
}
342+
logger_.info("calibration scheme version is {}", "Curve Fitting");
343+
adc_cali_curve_fitting_config_t cali_config = {
344+
.unit_id = unit,
345+
.atten = atten,
346+
.bitwidth = bitwidth,
347+
};
348+
ret = adc_cali_create_scheme_curve_fitting(&cali_config, &handle);
349+
if (ret == ESP_OK) {
350+
calibrated = true;
355351
}
356352
#endif
357353

358354
#if ADC_CALI_SCHEME_LINE_FITTING_SUPPORTED
355+
// cppcheck-suppress knownConditionTrueFalse
359356
if (!calibrated) {
360357
logger_.info("calibration scheme version is {}", "Line Fitting");
361358
adc_cali_line_fitting_config_t cali_config = {
@@ -373,6 +370,7 @@ class ContinuousAdc {
373370
*out_handle = handle;
374371
if (ret == ESP_OK) {
375372
logger_.info("Calibration Success");
373+
// cppcheck-suppress knownConditionTrueFalse
376374
} else if (ret == ESP_ERR_NOT_SUPPORTED || !calibrated) {
377375
logger_.warn("eFuse not burnt, skip software calibration");
378376
} else {
@@ -399,11 +397,11 @@ class ContinuousAdc {
399397
adc_continuous_handle_t handle_;
400398
adc_digi_convert_mode_t conv_mode_;
401399
adc_digi_output_format_t output_format_;
400+
std::vector<uint8_t> result_data_;
402401
Logger logger_;
403402
std::unique_ptr<Task> task_;
404403
TaskHandle_t task_handle_{NULL};
405404
std::mutex data_mutex_;
406-
uint8_t *result_data_;
407405

408406
std::atomic<bool> running_{false};
409407

components/adc/include/oneshot_adc.hpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ class OneshotAdc {
4343
* @brief Initialize the oneshot adc reader.
4444
* @param config Config used to initialize the reader.
4545
*/
46-
OneshotAdc(const Config &config) : logger_({.tag = "Oneshot Adc", .level = config.log_level}) {
46+
explicit OneshotAdc(const Config &config)
47+
: logger_({.tag = "Oneshot Adc", .level = config.log_level}) {
4748
init(config);
4849
}
4950

components/ads1x15/include/ads1x15.hpp

+4-5
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ class Ads1x15 {
3434
* @param data_len Number of data bytes to read.
3535
* @return True if the read was successful.
3636
*/
37-
typedef std::function<bool(uint8_t dev_addr, uint8_t *data, size_t data_len)>
38-
read_fn;
37+
typedef std::function<bool(uint8_t dev_addr, uint8_t *data, size_t data_len)> read_fn;
3938

4039
/**
4140
* @brief Gain values for the ADC conversion.
@@ -104,7 +103,7 @@ class Ads1x15 {
104103
* @brief Construct Ads1x15 specficially for ADS1015.
105104
* @param config Configuration structure.
106105
*/
107-
Ads1x15(const Ads1015Config &config)
106+
explicit Ads1x15(const Ads1015Config &config)
108107
: gain_(config.gain), ads1015rate_(config.sample_rate), bit_shift_(4),
109108
address_(config.device_address), write_(config.write), read_(config.read),
110109
logger_({.tag = "Ads1015", .level = config.log_level}) {}
@@ -113,7 +112,7 @@ class Ads1x15 {
113112
* @brief Construct Ads1x15 specficially for ADS1115.
114113
* @param config Configuration structure.
115114
*/
116-
Ads1x15(const Ads1115Config &config)
115+
explicit Ads1x15(const Ads1115Config &config)
117116
: gain_(config.gain), ads1115rate_(config.sample_rate), bit_shift_(0),
118117
address_(config.device_address), write_(config.write), read_(config.read),
119118
logger_({.tag = "Ads1115", .level = config.log_level}) {}
@@ -138,7 +137,7 @@ class Ads1x15 {
138137

139138
bool conversion_complete(std::error_code &ec);
140139

141-
float raw_to_mv(int16_t raw) {
140+
float raw_to_mv(int16_t raw) const {
142141
// see data sheet Table 3
143142
float fsRange;
144143
switch (gain_) {

components/ads1x15/src/ads1x15.cpp

+8-7
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,14 @@ int16_t Ads1x15::sample_raw(int channel, std::error_code &ec) {
5656
return 0;
5757
}
5858
// Start with default values
59-
uint16_t config = REG_CONFIG_CQUE_1CONV | // Comparator enabled and asserts on 1
60-
// match
61-
REG_CONFIG_CLAT_NONLAT | // non-latching (default val)
62-
REG_CONFIG_CPOL_ACTVLOW | // Alert/Rdy active low (default val)
63-
REG_CONFIG_CMODE_TRAD | // Traditional comparator (default val)
64-
REG_CONFIG_MODE_SINGLE; // Single conversion mode
65-
// Set PGA/voltage range
59+
uint16_t config = REG_CONFIG_MODE_SINGLE;
60+
// This is equivalent to the below (since the rest are 0x0000):
61+
//
62+
// REG_CONFIG_CQUE_1CONV | // Comparator enabled and asserts on 1 match
63+
// REG_CONFIG_CLAT_NONLAT | // non-latching (default val)
64+
// REG_CONFIG_CPOL_ACTVLOW | // Alert/Rdy active low (default val)
65+
// REG_CONFIG_CMODE_TRAD | // Traditional comparator (default val)
66+
// REG_CONFIG_MODE_SINGLE; // Single conversion mode Set PGA/voltage range
6667
config |= (uint16_t)gain_;
6768
// Set data rate
6869
config |= rate_;

components/ads7138/include/ads7138.hpp

+13-16
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <chrono>
55
#include <functional>
66
#include <mutex>
7+
#include <numeric>
78
#include <thread>
89
#include <unordered_map>
910

@@ -177,7 +178,7 @@ class Ads7138 {
177178
* @brief Construct Ads7138
178179
* @param config Configuration structure.
179180
*/
180-
Ads7138(const Config &config)
181+
explicit Ads7138(const Config &config)
181182
: config_(config), mode_(config.mode), avdd_mv_(config.avdd_volts * 1000.0f) // Convert to mV
182183
,
183184
data_format_(config.oversampling_ratio == OversamplingRatio::NONE ? DataFormat::RAW
@@ -920,16 +921,16 @@ class Ads7138 {
920921
logger_.info("Statistics enabled set to {}", statistics_enabled_);
921922
}
922923

924+
static uint8_t bit_pred(uint8_t value, Channel channel) {
925+
return value | (1 << static_cast<uint8_t>(channel));
926+
};
927+
923928
void set_pin_configuration(std::error_code &ec) {
924929
logger_.info("Setting digital mode for outputs {} and inputs {}", digital_outputs_,
925930
digital_inputs_);
926931
uint8_t data = 0;
927-
for (auto channel : digital_inputs_) {
928-
data |= 1 << static_cast<uint8_t>(channel);
929-
}
930-
for (auto channel : digital_outputs_) {
931-
data |= 1 << static_cast<uint8_t>(channel);
932-
}
932+
std::accumulate(digital_inputs_.begin(), digital_inputs_.end(), data, bit_pred);
933+
std::accumulate(digital_outputs_.begin(), digital_outputs_.end(), data, bit_pred);
933934
// don't have to do anything for analog inputs since they are the default
934935
// state (0)
935936
write_one_(Register::PIN_CFG, data, ec);
@@ -939,9 +940,7 @@ class Ads7138 {
939940
logger_.info("Setting digital output for pins {}", digital_outputs_);
940941
// default direction is input (0)
941942
uint8_t data = 0;
942-
for (auto channel : digital_outputs_) {
943-
data |= 1 << static_cast<uint8_t>(channel);
944-
}
943+
std::accumulate(digital_outputs_.begin(), digital_outputs_.end(), data, bit_pred);
945944
write_one_(Register::GPIO_CFG, data, ec);
946945
}
947946

@@ -951,9 +950,7 @@ class Ads7138 {
951950
logger_.info("Setting analog inputs for autonomous mode");
952951
// configure the analog inputs for autonomous conversion sequence
953952
uint8_t data = 0;
954-
for (auto channel : analog_inputs_) {
955-
data |= 1 << static_cast<uint8_t>(channel);
956-
}
953+
std::accumulate(analog_inputs_.begin(), analog_inputs_.end(), data, bit_pred);
957954
write_one_(Register::AUTO_SEQ_CH_SEL, data, ec);
958955
}
959956
}
@@ -1204,7 +1201,7 @@ class Ads7138 {
12041201
* @param raw Raw ADC value.
12051202
* @return Voltage in mV.
12061203
*/
1207-
float raw_to_mv(uint16_t raw) {
1204+
float raw_to_mv(uint16_t raw) const {
12081205
// we have a 16-bit ADC, so we can represent 2^16 = 65536 values
12091206
// we were configured with avdd_mv_ as the reference voltage, so we
12101207
// can represent avdd_mv_ volts with 65536 values
@@ -1218,7 +1215,7 @@ class Ads7138 {
12181215
* @param mv Voltage in mV.
12191216
* @return Raw ADC value.
12201217
*/
1221-
uint16_t mv_to_raw(float mv) {
1218+
uint16_t mv_to_raw(float mv) const {
12221219
// we have a 16-bit ADC, so we can represent 2^16 = 65536 values
12231220
// we were configured with avdd_mv_ as the reference voltage, so we
12241221
// can represent avdd_mv_ volts with 65536 values
@@ -1512,7 +1509,7 @@ class Ads7138 {
15121509
write_many_(reg, (uint8_t *)&value, 2, ec);
15131510
}
15141511

1515-
void write_many_(Register reg, uint8_t *data, uint8_t len, std::error_code &ec) {
1512+
void write_many_(Register reg, const uint8_t *data, uint8_t len, std::error_code &ec) {
15161513
std::lock_guard<std::recursive_mutex> lock(mutex_);
15171514
uint8_t total_len = len + 2;
15181515
uint8_t data_with_header[total_len];

components/as5600/include/as5600.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ class As5600 {
9393
/**
9494
* @brief Construct the As5600 and start the update task.
9595
*/
96-
As5600(const Config &config)
96+
explicit As5600(const Config &config)
9797
: address_(config.device_address), write_(config.write), read_(config.read),
9898
velocity_filter_(config.velocity_filter), update_period_(config.update_period),
9999
logger_({.tag = "As5600", .level = config.log_level}) {

components/aw9523/include/aw9523.hpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ class Aw9523 {
8686
* @brief Construct the Aw9523. Will call initialize() if auto_init is true.
8787
* @param config Config structure for configuring the AW9523
8888
*/
89-
Aw9523(const Config &config)
89+
explicit Aw9523(const Config &config)
9090
: config_(config), address_(config.device_address), write_(config.write), read_(config.read),
9191
logger_({.tag = "Aw9523", .level = config.log_level}) {
9292
if (config.auto_init) {
@@ -305,7 +305,7 @@ class Aw9523 {
305305
void set_interrupt(uint8_t p0, uint8_t p1, std::error_code &ec) {
306306
logger_.debug("Setting interrupt on change p0:{}, p1:{}", p0, p1);
307307
auto addr = Registers::INTPORT0;
308-
uint8_t data[] = {p0, p1};
308+
const uint8_t data[] = {p0, p1};
309309
write_many_((uint8_t)addr, data, 2, ec);
310310
}
311311

@@ -330,7 +330,7 @@ class Aw9523 {
330330
void set_direction(uint8_t p0, uint8_t p1, std::error_code &ec) {
331331
logger_.debug("Setting direction p0:{}, p1:{}", p0, p1);
332332
auto addr = Registers::DIRPORT0;
333-
uint8_t data[] = {p0, p1};
333+
const uint8_t data[] = {p0, p1};
334334
write_many_((uint8_t)addr, data, 2, ec);
335335
}
336336

@@ -355,7 +355,7 @@ class Aw9523 {
355355
void configure_led(uint8_t p0, uint8_t p1, std::error_code &ec) {
356356
logger_.debug("Configuring LED function p0:{}, p1:{}", p0, p1);
357357
auto addr = Registers::LEDMODE0;
358-
uint8_t data[] = {p0, p1};
358+
const uint8_t data[] = {p0, p1};
359359
write_many_((uint8_t)addr, data, 2, ec);
360360
}
361361

@@ -370,7 +370,7 @@ class Aw9523 {
370370
uint8_t p1 = (mask >> 8) & 0xFF;
371371
logger_.debug("Configuring LED function p0:{}, p1:{}", p0, p1);
372372
auto addr = Registers::LEDMODE0;
373-
uint8_t data[] = {p0, p1};
373+
const uint8_t data[] = {p0, p1};
374374
write_many_((uint8_t)addr, data, 2, ec);
375375
}
376376

@@ -540,7 +540,7 @@ class Aw9523 {
540540
write_many_(reg_addr, &data, 1, ec);
541541
}
542542

543-
void write_many_(uint8_t reg_addr, uint8_t *write_data, size_t write_data_len,
543+
void write_many_(uint8_t reg_addr, const uint8_t *write_data, size_t write_data_len,
544544
std::error_code &ec) {
545545
uint8_t total_len = 1 + write_data_len;
546546
uint8_t data[total_len];

components/bldc_driver/include/bldc_driver.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class BldcDriver {
4747
* @note Enables the driver.
4848
* @param config Config used to initialize the driver.
4949
*/
50-
BldcDriver(const Config &config)
50+
explicit BldcDriver(const Config &config)
5151
: gpio_ah_((gpio_num_t)config.gpio_a_h), gpio_al_((gpio_num_t)config.gpio_a_l),
5252
gpio_bh_((gpio_num_t)config.gpio_b_h), gpio_bl_((gpio_num_t)config.gpio_b_l),
5353
gpio_ch_((gpio_num_t)config.gpio_c_h), gpio_cl_((gpio_num_t)config.gpio_c_l),

0 commit comments

Comments
 (0)