Skip to content

Commit

Permalink
Added new set_direction method to change I/O direction register (#247)
Browse files Browse the repository at this point in the history
  • Loading branch information
shogo-sugihara authored May 31, 2024
1 parent f5c9307 commit aa0b550
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions components/kts1622/include/kts1622.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,29 @@ class Kts1622 : public BasePeripheral<> {
set_direction((uint8_t)(mask & 0xFF), (uint8_t)(mask >> 8), ec);
}

/**
* @brief Set the i/o direction for the pins according to mask.
* @param mask The mask indicating pin position
* @param direction The direction indicating direction (1 = input, 0 = output)
* @param ec Error code to set if an error occurs.
*/
void set_direction(uint16_t mask, bool direction, std::error_code &ec) {
auto addr = Registers::CONFIG0;
uint8_t data[] = {0, 0};
read_many_from_register((uint8_t)addr, data, 2, ec);
if (ec) return;
if (direction) {
// To Input port
data[0] |= (uint8_t) mask;
data[1] |= (uint8_t)(mask >> 8);
} else {
// To Output port
data[0] &= (uint8_t) ~mask;
data[1] &= (uint8_t)(~mask >> 8);
}
write_many_to_register((uint8_t)addr, data, 2, ec);
}

/**
* @brief Set polarity inversion for the pins according to mask.
* @param port The port associated with the provided pin mask.
Expand Down

0 comments on commit aa0b550

Please sign in to comment.