-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpioexpander.c
192 lines (170 loc) · 5.71 KB
/
gpioexpander.c
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
* TCA9554 - Library to manage TCA9554 GPIO expander based on libohiboard
* Copyright (C) 2020 Marco Giammarini <http://www.warcomeb.it>
*
* Authors:
* Marco Giammarini <m.giammarini@warcomeb.it>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "gpioexpander.h"
#include "TCA9554.h"
#define GPIOEXPANDER_PIN(x) (((1)<<(x & 0x0007)))
static inline uint8_t __attribute__((always_inline)) getAddress (GPIOExpander_Pins_t pin)
{
if (pin == 0xFFFFFFFF)
{
ohiassert(0);
return 0;
}
switch ((pin & GPIOEXPANDER_DEVICE_MASK) >> 16)
{
case TCA9554_ADDRESS_0x20:
return 0x20;
case TCA9554_ADDRESS_0x21:
return 0x21;
case TCA9554_ADDRESS_0x22:
return 0x22;
case TCA9554_ADDRESS_0x23:
return 0x23;
case TCA9554_ADDRESS_0x24:
return 0x24;
case TCA9554_ADDRESS_0x25:
return 0x25;
case TCA9554_ADDRESS_0x26:
return 0x26;
case TCA9554_ADDRESS_0x27:
return 0x27;
default:
ohiassert(0);
return 0;
}
}
static inline uint8_t __attribute__((always_inline)) getPin (GPIOExpander_Pins_t pin)
{
if (pin == 0xFFFFFFFF)
{
ohiassert(0);
return 0;
}
return ((uint8_t)(pin & GPIOEXPANDER_PINS_MASK));
}
GPIOExpander_Device_t GPIOExpander_init (GPIOExpander_LowLevelDriver_t dev)
{
GPIOExpander_Device_t device = {0};
device.device = (Iic_DeviceHandle)dev;
return device;
}
GPIOExpander_Errors_t GPIOExpander_config (GPIOExpander_DeviceHandle_t dev,
GPIOExpander_Pins_t pin,
GPIOExpander_Type_t type)
{
if (dev == null)
{
ohiassert(0);
return GPIOEXPANDER_ERRORS_WRONG_DEVICE;
}
uint8_t address = getAddress(pin);
uint8_t pinNumber = getPin(pin);
if ((address != 0) && (pinNumber < TCA9554_PINS_NUMBER))
{
// Write register...
uint8_t mypin = GPIOEXPANDER_PIN(pinNumber);
TCA9554_Errors_t ret = TCA9554_config(dev->device,address,mypin,(uint8_t)type);
if (ret != TCA9554_ERRORS_NO_ERROR)
{
return GPIOEXPANDER_ERRORS_COMMUNICATION_FAIL;
}
return GPIOEXPANDER_ERRORS_NO_ERROR;
}
return GPIOEXPANDER_ERRORS_WRONG_PARAMS;
}
GPIOExpander_Errors_t GPIOExpander_set (GPIOExpander_DeviceHandle_t dev, GPIOExpander_Pins_t pin)
{
if (dev == null)
{
ohiassert(0);
return GPIOEXPANDER_ERRORS_WRONG_DEVICE;
}
uint8_t address = getAddress(pin);
uint8_t pinNumber = getPin(pin);
if ((address != 0) && (pinNumber < TCA9554_PINS_NUMBER))
{
// Write register...
uint8_t mypin = GPIOEXPANDER_PIN(pinNumber);
TCA9554_Errors_t ret = TCA9554_writeOutput(dev->device,address,mypin,GPIO_HIGH);
if (ret != TCA9554_ERRORS_NO_ERROR)
{
return GPIOEXPANDER_ERRORS_COMMUNICATION_FAIL;
}
return GPIOEXPANDER_ERRORS_NO_ERROR;
}
return GPIOEXPANDER_ERRORS_WRONG_PARAMS;
}
GPIOExpander_Errors_t GPIOExpander_clear (GPIOExpander_DeviceHandle_t dev, GPIOExpander_Pins_t pin)
{
if (dev == null)
{
ohiassert(0);
return GPIOEXPANDER_ERRORS_WRONG_DEVICE;
}
uint8_t address = getAddress(pin);
uint8_t pinNumber = getPin(pin);
if ((address != 0) && (pinNumber < TCA9554_PINS_NUMBER))
{
// Write register...
uint8_t mypin = GPIOEXPANDER_PIN(pinNumber);
TCA9554_Errors_t ret = TCA9554_writeOutput(dev->device,address,mypin,GPIO_LOW);
if (ret != TCA9554_ERRORS_NO_ERROR)
{
return GPIOEXPANDER_ERRORS_COMMUNICATION_FAIL;
}
return GPIOEXPANDER_ERRORS_NO_ERROR;
}
return GPIOEXPANDER_ERRORS_WRONG_PARAMS;
}
GPIOExpander_Errors_t GPIOExpander_toggle (GPIOExpander_DeviceHandle_t dev, GPIOExpander_Pins_t pin)
{
// TODO!
return GPIOEXPANDER_ERRORS_NO_ERROR;
}
GPIOExpander_Errors_t GPIOExpander_get (GPIOExpander_DeviceHandle_t dev, GPIOExpander_Pins_t pin, Gpio_Level* level)
{
if (dev == null)
{
ohiassert(0);
return GPIOEXPANDER_ERRORS_WRONG_DEVICE;
}
uint8_t address = getAddress(pin);
uint8_t pinNumber = getPin(pin);
if ((address != 0) && (pinNumber < TCA9554_PINS_NUMBER))
{
// Write register...
uint8_t mypin = GPIOEXPANDER_PIN(pinNumber);
Gpio_Level readValue = 0;
TCA9554_Errors_t ret = TCA9554_readInput(dev->device,address,mypin,&readValue);
if (ret != TCA9554_ERRORS_NO_ERROR)
{
return GPIOEXPANDER_ERRORS_COMMUNICATION_FAIL;
}
*level = readValue;
return GPIOEXPANDER_ERRORS_NO_ERROR;
}
return GPIOEXPANDER_ERRORS_WRONG_PARAMS;
}