qmk_firmware

QMK firmware for my keyboards (Corne, Sweep Ferris) and trackball (Ploopy Adept)
Log | Files | Refs | Submodules | LICENSE

tca6424.c (3031B)


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