sp64.c (1972B)
1 /* Copyright 2019 Neil Kettle 2 * 3 * This program is free software: you can redistribute it and/or modify 4 * it under the terms of the GNU General Public License as published by 5 * the Free Software Foundation, either version 2 of the License, or 6 * (at your option) any later version. 7 * 8 * This program is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * GNU General Public License for more details. 12 * 13 * You should have received a copy of the GNU General Public License 14 * along with this program. If not, see <http://www.gnu.org/licenses/>. 15 */ 16 17 #include "sp64.h" 18 19 #ifdef RIGHT_HALF 20 bool i2c_initialized = 0; 21 i2c_status_t mcp23018_status = I2C_STATUS_ERROR; 22 23 uint8_t init_mcp23018(void) 24 { 25 uint8_t data[3]; 26 mcp23018_status = I2C_STATUS_ERROR; 27 28 // I2C subsystem 29 if (i2c_initialized == 0) { 30 i2c_init(); // on pins D(1,0) 31 i2c_initialized = true; 32 _delay_ms(1000); 33 } 34 35 // set pin direction 36 // - unused : input : 1 37 // - input : input : 1 38 // - driving : output : 39 data[0] = IODIRA; 40 data[1] = 0b00000000; 41 data[2] = 0b11111111; 42 mcp23018_status = i2c_transmit(I2C_ADDR, (uint8_t *)data, 3, MCP23018_I2C_TIMEOUT); 43 if (mcp23018_status != I2C_STATUS_SUCCESS) 44 goto out; 45 46 // set pull-up 47 // - unused : on : 1 48 // - input : on : 1 49 // - driving : off : 0 50 data[0] = GPPUA; 51 data[1] = 0b00000000; 52 data[2] = 0b11111111; 53 mcp23018_status = i2c_transmit(I2C_ADDR, (uint8_t *)data, 3, MCP23018_I2C_TIMEOUT); 54 if (mcp23018_status != I2C_STATUS_SUCCESS) 55 goto out; 56 57 // set logical value (doesn't matter on inputs) 58 // - unused : hi-Z : 1 59 // - input : hi-Z : 1 60 // - driving : hi-Z : 1 61 data[0] = OLATA; 62 data[1] = 0b11111111; 63 data[2] = 0b11111111; 64 mcp23018_status = i2c_transmit(I2C_ADDR, (uint8_t *)data, 3, MCP23018_I2C_TIMEOUT); 65 66 out: 67 return (mcp23018_status); 68 } 69 #endif