int pifacedigital_open(uint8_t hw_addr) { if ((mcp23s17_fd = pifacedigital_open_noinit(hw_addr)) < 0) { fprintf(stderr, "pifacedigital_open: ERROR Could not open MCP23S17 device."); return -1; } // set up config register const uint8_t ioconfig = BANK_OFF | \ INT_MIRROR_OFF | \ SEQOP_OFF | \ DISSLW_OFF | \ HAEN_ON | \ ODR_OFF | \ INTPOL_LOW; mcp23s17_write_reg(ioconfig, IOCON, hw_addr, mcp23s17_fd); // I/O direction mcp23s17_write_reg(0x00, IODIRA, hw_addr, mcp23s17_fd); mcp23s17_write_reg(0xff, IODIRB, hw_addr, mcp23s17_fd); // GPIOB pull ups mcp23s17_write_reg(0xff, GPPUB, hw_addr, mcp23s17_fd); // enable interrupts mcp23s17_write_reg(0xff, GPINTENB, hw_addr, mcp23s17_fd); return mcp23s17_fd; // returns the fd, for advanced users. }
void mcp23s17_write_bit(uint8_t data, uint8_t bit_num, uint8_t reg, uint8_t hw_addr, int fd) { uint8_t reg_data = mcp23s17_read_reg(reg, hw_addr, fd); if (data) { reg_data |= 1 << bit_num; // set } else { reg_data &= 0xff ^ (1 << bit_num); // clear } return mcp23s17_write_reg(reg_data, reg, hw_addr, fd); }
void pifacedigital_close(uint8_t hw_addr) { if (pfd_count <= 0) return; pfd_count--; // disable interrupts if enabled const uint8_t intenb = mcp23s17_read_reg(GPINTENB, hw_addr, mcp23s17_fd); if (intenb) { mcp23s17_write_reg(0, GPINTENB, hw_addr, mcp23s17_fd); // now do some other interrupt stuff... // TODO } // if no more PiFace Digital's, close the fd if (pfd_count <= 0) { pfd_count = 0; close(mcp23s17_fd); } }
void pifacedigital_write_reg(uint8_t data, uint8_t reg, uint8_t hw_addr) { mcp23s17_write_reg(data, reg, hw_addr, mcp23s17_fd); }