mcp23018.c (2002B)
1 /* 2 * Copyright 2020 Richard Titmuss (richard.titmuss@gmail.com) 3 * Copyright 2012-2018 Jun Wako, Jack Humbert, Yiancar 4 * 5 * This program is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation, either version 2 of the License, or 8 * (at your option) any later version. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 */ 18 19 #include "i2c_master.h" 20 #include "mcp23018.h" 21 22 #define MCP23018_ADDR 0b0100000 23 24 #define MCP23018_TIMEOUT 100 25 26 static i2c_status_t mcp23018_status = I2C_STATUS_ERROR; 27 28 void msp23018_init(void) { 29 mcp23018_status = I2C_STATUS_SUCCESS; 30 31 // Set pin direction 32 uint8_t iodir[] = {0b00001111, 0b11111111}; 33 mcp23018_writeReg(IODIRA, iodir, 2); 34 35 // Set pull-up 36 uint8_t gppu[] = {0b00001111, 0b11111000}; 37 mcp23018_writeReg(GPPUA, gppu, 2); 38 39 // LEDs output high 40 uint8_t gpio[] = {0b00000000, 0b00000111}; 41 mcp23018_writeReg(GPIOA, gpio, 2); 42 } 43 44 bool mcp23018_reset_required(void) { return mcp23018_status != I2C_STATUS_SUCCESS; } 45 46 i2c_status_t mcp23018_writeReg(uint8_t regaddr, const uint8_t* data, uint16_t length) { 47 if (mcp23018_status) { 48 return mcp23018_status; 49 } 50 51 mcp23018_status = i2c_write_register((MCP23018_ADDR << 1), regaddr, data, length, MCP23018_TIMEOUT); 52 return mcp23018_status; 53 } 54 55 i2c_status_t mcp23018_readReg(uint8_t regaddr, uint8_t* data, uint16_t length) { 56 if (mcp23018_status) { 57 return mcp23018_status; 58 } 59 60 mcp23018_status = i2c_read_register((MCP23018_ADDR << 1), regaddr, data, length, MCP23018_TIMEOUT); 61 return mcp23018_status; 62 }