forked from zynthian/zyncoder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zynmcp23008.h
101 lines (85 loc) · 3.04 KB
/
zynmcp23008.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
* ******************************************************************
* ZYNTHIAN PROJECT: Zyncoder Library
*
* Library for interfacing MCP23008 using polling (only zynswitches!).
*
* Copyright (C) 2015-2022 Fernando Moyano <[email protected]>
*
* ******************************************************************
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* For a full copy of the GNU General Public License see the LICENSE.txt file.
*
* ******************************************************************
*/
// WARNING! It's not possible to use this with zynmcp23017!!
#ifndef ZYNMCP23008_H
#define ZYNMCP23008_H
#include <stdint.h>
#include <pthread.h>
//-----------------------------------------------------------------------------
// IC registers
//-----------------------------------------------------------------------------
// MCP23x08 Registers
#define MCP23x08_IODIR 0x00
#define MCP23x08_IPOL 0x01
#define MCP23x08_GPINTEN 0x02
#define MCP23x08_DEFVAL 0x03
#define MCP23x08_INTCON 0x04
#define MCP23x08_IOCON 0x05
#define MCP23x08_GPPU 0x06
#define MCP23x08_INTF 0x07
#define MCP23x08_INTCAP 0x08
#define MCP23x08_GPIO 0x09
#define MCP23x08_OLAT 0x0A
// Bits in the IOCON register
#define IOCON_UNUSED 0x01
#define IOCON_INTPOL 0x02
#define IOCON_ODR 0x04
#define IOCON_HAEN 0x08
#define IOCON_DISSLW 0x10
#define IOCON_SEQOP 0x20
#define IOCON_MIRROR 0x40
#define IOCON_BANK_MODE 0x80
// Default initialisation mode
#define IOCON_INIT (IOCON_SEQOP)
// SPI Command codes
#define CMD_WRITE 0x40
#define CMD_READ 0x41
//-----------------------------------------------------------------------------
// MCP23008 stuff
//-----------------------------------------------------------------------------
#define MAX_NUM_MCP23008 4
//Switch Polling interval
#define POLL_ZYNSWITCHES_US 10000
typedef struct zynmcp23008_st {
uint8_t enabled;
int fd;
uint16_t base_pin;
uint8_t i2c_address;
uint8_t output_state;
} zynmcp23008_t;
void reset_zynmcp23008s();
int setup_zynmcp23008(uint8_t i, uint16_t base_pin, uint8_t i2c_address);
int8_t zynmcp23008_get_last_index();
int8_t zynmcp23008_pin2index(uint16_t pin);
void zynmcp23008_set_pin_mode (uint8_t i, uint16_t pin, uint8_t mode);
void zynmcp23008_set_pin_mode (uint8_t i, uint16_t pin, uint8_t mode);
void zynmcp23008_write_pin (uint8_t i, uint16_t pin, uint8_t val);
uint8_t zynmcp23008_read_pin (uint8_t i, uint16_t pin);
uint8_t zynmcp23008_read_pins_(uint8_t i);
//Switches Polling Thread (should be avoided!)
pthread_t init_poll_zynswitches();
void end_poll_zynswitches();
//-----------------------------------------------------------------------------
#endif