tca6424.c (3077B)
1 /** 2 * @file tca6424.c 3 * @author astro 4 * @brief driver for the tca6424 5 * 6 * Copyright 2020 astro <yuleiz@gmail.com> 7 * 8 * This program is free software: you can redistribute it and/or modify 9 * it under the terms of the GNU General Public License as published by 10 * the Free Software Foundation, either version 2 of the License, or 11 * (at your option) any later version. 12 * 13 * This program is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 * GNU General Public License for more details. 17 * 18 * You should have received a copy of the GNU General Public License 19 * along with this program. If not, see <http://www.gnu.org/licenses/>. 20 */ 21 22 #include "tca6424.h" 23 #include "i2c_master.h" 24 25 #define TCA6424_INPUT_PORT0 0x0 26 #define TCA6424_INPUT_PORT1 0x01 27 #define TCA6424_INPUT_PORT2 0x02 28 29 #define TCA6424_OUTPUT_PORT0 0x04 30 #define TCA6424_OUTPUT_PORT1 0x05 31 #define TCA6424_OUTPUT_PORT2 0x06 32 33 #define TCA6424_POLARITY_PORT0 0x08 34 #define TCA6424_POLARITY_PORT1 0x09 35 #define TCA6424_POLARITY_PORT2 0x0A 36 37 #define TCA6424_CONF_PORT0 0x0C 38 #define TCA6424_CONF_PORT1 0x0D 39 #define TCA6424_CONF_PORT2 0x0E 40 41 #define TIMEOUT 100 42 43 void tca6424_init(void) 44 { 45 i2c_init(); 46 } 47 48 static void write_port(uint8_t p, uint8_t d) 49 { 50 i2c_write_register(TCA6424_ADDR, p, &d, 1, TIMEOUT); 51 } 52 53 static uint8_t read_port(uint8_t port) 54 { 55 uint8_t data = 0; 56 i2c_read_register(TCA6424_ADDR, port, &data, 1, TIMEOUT); 57 return data; 58 } 59 60 void tca6424_write_config(TCA6424_PORT port, uint8_t data) 61 { 62 switch(port) { 63 case TCA6424_PORT0: 64 write_port(TCA6424_CONF_PORT0, data); 65 break; 66 case TCA6424_PORT1: 67 write_port(TCA6424_CONF_PORT1, data); 68 break; 69 case TCA6424_PORT2: 70 write_port(TCA6424_CONF_PORT2, data); 71 break; 72 } 73 } 74 75 void tca6424_write_polarity(TCA6424_PORT port, uint8_t data) 76 { 77 switch(port) { 78 case TCA6424_PORT0: 79 write_port(TCA6424_POLARITY_PORT0, data); 80 break; 81 case TCA6424_PORT1: 82 write_port(TCA6424_POLARITY_PORT1, data); 83 break; 84 case TCA6424_PORT2: 85 write_port(TCA6424_POLARITY_PORT2, data); 86 break; 87 } 88 } 89 90 void tca6424_write_port(TCA6424_PORT port, uint8_t data) 91 { 92 switch(port) { 93 case TCA6424_PORT0: 94 write_port(TCA6424_OUTPUT_PORT0, data); 95 break; 96 case TCA6424_PORT1: 97 write_port(TCA6424_OUTPUT_PORT1, data); 98 break; 99 case TCA6424_PORT2: 100 write_port(TCA6424_OUTPUT_PORT2, data); 101 break; 102 } 103 } 104 105 uint8_t tca6424_read_port(TCA6424_PORT port) 106 { 107 switch(port) { 108 case TCA6424_PORT0: 109 return read_port(TCA6424_INPUT_PORT0); 110 case TCA6424_PORT1: 111 return read_port(TCA6424_INPUT_PORT1); 112 case TCA6424_PORT2: 113 return read_port(TCA6424_INPUT_PORT2); 114 } 115 116 return 0; 117 }