36#ifndef _FREERTOS_DRIVERS_COMMON_MCP23017GPIO_HXX_
37#define _FREERTOS_DRIVERS_COMMON_MCP23017GPIO_HXX_
50 static constexpr long long POLLING_DELAY =
MSEC_TO_NSEC(20);
77 void init(
const char *i2c_path)
79 int fd = ::open(i2c_path, O_RDWR);
146 bool push_dir =
false;
147 bool push_lat =
false;
200 .len = (uint16_t)(len + 1),
203 struct i2c_rdwr_ioctl_data ioctl_data = {
216 struct i2c_msg msgs[] = {
217 {.addr =
i2cAddress_, .flags = 0, .len = 1, .buf = ®},
218 {.addr =
i2cAddress_, .flags = I2C_M_RD, .len = len, .buf = data}};
220 struct i2c_rdwr_ioctl_data ioctl_data = {
293 if (new_state != old_state)
303 parent_->
dirty_ |= parent_->DIRTY_LAT;
324 uint8_t desired = (dir == Direction::DOUTPUT) ? 0 :
pinBit_;
328 (parent_->
dir_[
port_] & (~pinBit_)) | desired;
329 parent_->
dirty_ |= parent_->DIRTY_DIR;
337 return Direction::DINPUT;
341 return Direction::DOUTPUT;
int ioctl(int fd, unsigned long int key,...)
Request and ioctl transaction.
See OSMutexLock in os/OS.hxx.
Lightweight locking class for protecting small critical sections.
This class implements an execution of tasks pulled off an input queue.
ActiveTimers * active_timers()
OS-independent abstraction for GPIO.
Value
Defines the options for GPIO level.
Direction
Defines the options for GPIO direction.
Direction direction() const override
Gets the GPIO direction.
const uint8_t pinBit_
one bit that denotes the output pin.
void clr() const override
Clears the GPIO output pin to low.
Value read() const override
Retrieves the current Value of a GPIO input pin.
void set() const override
Sets the GPIO output pin to high.
void write(Value new_state) const override
Writes a GPIO output pin (set or clear to a specific state).
const uint8_t port_
0 or 1 for portA/B
void set_direction(Direction dir) const override
Sets the GPIO direction.
This timer runs in the parent executor and upon every timeout it executes the update / polling of the...
long long timeout() override
Clients of timer should override this function.
uint8_t lat_[2]
Shadow of the latch registers.
int fd_
I2C port file descriptor.
uint8_t gpio_[2]
Shadow of the input registers.
void update_in()
Updates input registers.
MCP23017(ExecutorBase *executor, bool a2, bool a1, bool a0)
Constructor.
void update_out()
Updates the output direction and latch registers if any of it is dirty.
@ BASE_ADDRESS
I2C address of the first device.
uint8_t dir_[2]
Shadow of the direction registers.
void register_read(uint8_t reg, uint8_t *data, uint16_t len)
Reads one or more (sequential) registers in the MCP23017.
uint8_t i2cAddress_
Address of this particular device on the I2C port.
uint8_t dirty_
Bit mask of registers that need updating.
ExecutorBase * executor_
Executor. We will be blocking this for the I2C IO.
void init(int i2c_fd)
Initializes the device.
RefreshTimer timer_
Timer instance to schedule work on the executor.
void init(const char *i2c_path)
Initializes the device.
void register_write(uint8_t reg, const uint8_t *data, uint16_t len)
Writes one or more (sequential) registers in the MCP23017.
A timer that can schedule itself to run on an executor at specified times in the future.
@ RESTART
Restart the timer with existing period.
void start(long long period=-1)
Starts a timer.
#define ARRAYSIZE(a)
Returns the number of elements in a statically defined array (of static size)
#define HASSERT(x)
Checks that the value of expression x is true, else terminates the current process.
#define DISALLOW_COPY_AND_ASSIGN(TypeName)
Removes default copy-constructor and assignment added by C++.
#define MSEC_TO_NSEC(_msec)
Convert a millisecond value to a nanosecond value.