Skip to content

Commit 3bcd825

Browse files
authored
fix(kts1622/tla2528): Ensure protection using base_mutex (#443)
1 parent 59a6ad4 commit 3bcd825

File tree

2 files changed

+35
-13
lines changed

2 files changed

+35
-13
lines changed

components/kts1622/include/kts1622.hpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ class Kts1622 : public BasePeripheral<> {
161161
* output pins on the ports.
162162
*/
163163
void output(uint8_t p0, uint8_t p1, std::error_code &ec) {
164+
std::lock_guard<std::recursive_mutex> lock(base_mutex_);
164165
output(Port::PORT0, p0, ec);
165166
if (ec)
166167
return;
@@ -175,6 +176,7 @@ class Kts1622 : public BasePeripheral<> {
175176
* output pins on the ports.
176177
*/
177178
void output(uint16_t value, std::error_code &ec) {
179+
std::lock_guard<std::recursive_mutex> lock(base_mutex_);
178180
output(Port::PORT0, value & 0xFF, ec);
179181
if (ec)
180182
return;
@@ -189,6 +191,7 @@ class Kts1622 : public BasePeripheral<> {
189191
* @param ec Error code to set if an error occurs.
190192
*/
191193
void clear_pins(Port port, uint8_t mask, std::error_code &ec) {
194+
std::lock_guard<std::recursive_mutex> lock(base_mutex_);
192195
auto addr = port == Port::PORT0 ? Registers::OUTPORT0 : Registers::OUTPORT1;
193196
auto data = read_u8_from_register((uint8_t)addr, ec);
194197
if (ec)
@@ -205,6 +208,7 @@ class Kts1622 : public BasePeripheral<> {
205208
* @param ec Error code to set if an error occurs.
206209
*/
207210
void clear_pins(uint8_t p0, uint8_t p1, std::error_code &ec) {
211+
std::lock_guard<std::recursive_mutex> lock(base_mutex_);
208212
clear_pins(Port::PORT0, p0, ec);
209213
if (ec)
210214
return;
@@ -218,6 +222,7 @@ class Kts1622 : public BasePeripheral<> {
218222
* @param ec Error code to set if an error occurs.
219223
*/
220224
void clear_pins(uint16_t mask, std::error_code &ec) {
225+
std::lock_guard<std::recursive_mutex> lock(base_mutex_);
221226
clear_pins(Port::PORT0, mask & 0xFF, ec);
222227
if (ec)
223228
return;
@@ -233,6 +238,7 @@ class Kts1622 : public BasePeripheral<> {
233238
*/
234239
void set_pins(Port port, uint8_t mask, std::error_code &ec) {
235240
auto addr = port == Port::PORT0 ? Registers::OUTPORT0 : Registers::OUTPORT1;
241+
std::lock_guard<std::recursive_mutex> lock(base_mutex_);
236242
auto data = read_u8_from_register((uint8_t)addr, ec);
237243
if (ec)
238244
return;
@@ -248,6 +254,7 @@ class Kts1622 : public BasePeripheral<> {
248254
* @param ec Error code to set if an error occurs.
249255
*/
250256
void set_pins(uint8_t p0, uint8_t p1, std::error_code &ec) {
257+
std::lock_guard<std::recursive_mutex> lock(base_mutex_);
251258
set_pins(Port::PORT0, p0, ec);
252259
if (ec)
253260
return;
@@ -261,6 +268,7 @@ class Kts1622 : public BasePeripheral<> {
261268
* @param ec Error code to set if an error occurs.
262269
*/
263270
void set_pins(uint16_t mask, std::error_code &ec) {
271+
std::lock_guard<std::recursive_mutex> lock(base_mutex_);
264272
set_pins(Port::PORT0, mask & 0xFF, ec);
265273
if (ec)
266274
return;
@@ -284,6 +292,7 @@ class Kts1622 : public BasePeripheral<> {
284292
* @return The pin values as a 16 bit mask (P0_0 lsb, P1_7 msb).
285293
*/
286294
uint16_t get_output(std::error_code &ec) {
295+
std::lock_guard<std::recursive_mutex> lock(base_mutex_);
287296
uint16_t p0 = read_u8_from_register((uint8_t)Registers::OUTPORT0, ec);
288297
if (ec)
289298
return 0;
@@ -300,6 +309,7 @@ class Kts1622 : public BasePeripheral<> {
300309
* @param ec Error code to set if an error occurs.
301310
*/
302311
void set_port_output_drive_mode(Port port, OutputDriveMode mode, std::error_code &ec) {
312+
std::lock_guard<std::recursive_mutex> lock(base_mutex_);
303313
// get the current value from the register
304314
uint8_t data = read_u8_from_register((uint8_t)Registers::OUT_CFG, ec);
305315
if (ec)
@@ -397,6 +407,7 @@ class Kts1622 : public BasePeripheral<> {
397407
*/
398408
void configure_interrupt(uint8_t p0, uint8_t p1, InterruptType type, std::error_code &ec) {
399409
logger_.info("Configuring interrupt p0:{:#08b}, p1:{:#08b} to {}", p0, p1, type);
410+
std::lock_guard<std::recursive_mutex> lock(base_mutex_);
400411
configure_interrupt(Port::PORT0, p0, type, ec);
401412
if (ec)
402413
return;
@@ -442,6 +453,7 @@ class Kts1622 : public BasePeripheral<> {
442453
* @param ec Error code to set if an error occurs.
443454
*/
444455
void clear_interrupts(std::error_code &ec) {
456+
std::lock_guard<std::recursive_mutex> lock(base_mutex_);
445457
write_u8_to_register((uint8_t)Registers::INT_CLEAR0, 0xFF, ec);
446458
if (ec)
447459
return;
@@ -489,6 +501,7 @@ class Kts1622 : public BasePeripheral<> {
489501
* @param ec Error code to set if an error occurs.
490502
*/
491503
void set_direction(uint16_t mask, bool direction, std::error_code &ec) {
504+
std::lock_guard<std::recursive_mutex> lock(base_mutex_);
492505
auto addr = Registers::CONFIG0;
493506
uint8_t data[] = {0, 0};
494507
read_many_from_register((uint8_t)addr, data, 2, ec);
@@ -615,6 +628,7 @@ class Kts1622 : public BasePeripheral<> {
615628
// if pull == PullResistor::NO_PULL, then clear the bits in the appropriate
616629
// PULL_UP_DOWN_EN register
617630
auto addr = port == Port::PORT0 ? Registers::PULL_UP_DOWN_EN0 : Registers::PULL_UP_DOWN_EN1;
631+
std::lock_guard<std::recursive_mutex> lock(base_mutex_);
618632
auto data = read_u8_from_register((uint8_t)addr, ec);
619633
if (ec)
620634
return;
@@ -705,6 +719,7 @@ class Kts1622 : public BasePeripheral<> {
705719
};
706720

707721
void init(const Config &config, std::error_code &ec) {
722+
std::lock_guard<std::recursive_mutex> lock(base_mutex_);
708723
set_direction(Port::PORT0, config.port_0_direction_mask, ec);
709724
if (ec)
710725
return;

components/tla2528/include/tla2528.hpp

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,7 @@ class Tla2528 : public BasePeripheral<> {
553553
};
554554

555555
void init(const Config &config, std::error_code &ec) {
556+
std::lock_guard<std::recursive_mutex> lock(base_mutex_);
556557
// Set the data format
557558
set_data_format(data_format_, append_, ec);
558559
if (ec)
@@ -601,6 +602,7 @@ class Tla2528 : public BasePeripheral<> {
601602
}
602603

603604
void set_data_format(DataFormat format, Append append, std::error_code &ec) {
605+
std::lock_guard<std::recursive_mutex> lock(base_mutex_);
604606
data_format_ = format;
605607
logger_.info("Data format set to {}", data_format_);
606608
append_ = append;
@@ -712,20 +714,25 @@ class Tla2528 : public BasePeripheral<> {
712714
std::vector<uint16_t> values(num_inputs);
713715
size_t num_bytes = num_inputs * num_bytes_per_sample_;
714716
uint8_t raw_values[num_bytes];
715-
// start the auto conversion sequence
716-
start_auto_conversion(ec);
717-
if (ec) {
718-
return {};
719-
}
720-
read_many(raw_values, num_bytes, ec);
721-
if (ec) {
722-
return {};
723-
}
724-
// stop the auto conversion sequence
725-
stop_auto_conversion(ec);
726-
if (ec) {
727-
return {};
717+
718+
{
719+
std::lock_guard<std::recursive_mutex> lock(base_mutex_);
720+
// start the auto conversion sequence
721+
start_auto_conversion(ec);
722+
if (ec) {
723+
return {};
724+
}
725+
read_many(raw_values, num_bytes, ec);
726+
if (ec) {
727+
return {};
728+
}
729+
// stop the auto conversion sequence
730+
stop_auto_conversion(ec);
731+
if (ec) {
732+
return {};
733+
}
728734
}
735+
729736
int analog_index = 0;
730737
// only pull out the ones that were configured as analog inputs
731738
for (int i = 0; i < num_bytes; i += num_bytes_per_sample_) {

0 commit comments

Comments
 (0)