-
Notifications
You must be signed in to change notification settings - Fork 28
/
io_pcf.c
312 lines (250 loc) · 6.78 KB
/
io_pcf.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#include "io_pcf.h"
#include "i2c.h"
#include "util.h"
#include <stdlib.h>
typedef struct attr_packed
{
uint16_t counters;
uint8_t pin_output_cache;
} pcf_data_instance_t;
assert_size(pcf_data_instance_t, 3);
typedef struct attr_packed
{
unsigned int polling:1;
pcf_data_instance_t instance[io_pcf_instance_size];
} pcf_data_t;
assert_size(pcf_data_t, (3 * io_pcf_instance_size) + 1);
static pcf_data_t pcf_data;
assert_size(pcf_data, (3 * io_pcf_instance_size) + 1);
static io_error_t init(const struct io_info_entry_T *info)
{
pcf_data.instance[info->instance].counters = 0;
pcf_data.instance[info->instance].pin_output_cache = 0xff;
pcf_data.polling = config_flags_match(flag_pcf_no_poll) ? 0 : 1;
if(i2c_send1(info->address, pcf_data.instance[info->instance].pin_output_cache) != i2c_error_ok)
return(io_error);
return(io_ok);
}
void io_pcf_pins_changed(uint32_t pin_status_mask, uint16_t pin_value_mask, uint8_t io)
{
io_config_pin_entry_t *pin_config;
unsigned int pin;
if(io >= io_id_size)
{
log("[pcf] pin change: invalid io\n");
return;
}
for(pin = 0; pin < io_pcf_pin_size; pin++)
{
if(!(pin_status_mask & (1 << pin)))
continue;
pin_config = &io_config[io][pin];
if(pin_config->llmode != io_pin_ll_counter)
continue;
io_pin_changed(io, pin, pin_value_mask);
}
}
static attr_pure unsigned int max_value(const struct io_info_entry_T *info, io_data_pin_entry_t *data, const io_config_pin_entry_t *pin_config, unsigned int pin)
{
unsigned int value = 0;
switch(pin_config->llmode)
{
case(io_pin_ll_input_digital):
case(io_pin_ll_output_digital):
{
value = 0x01;
break;
}
case(io_pin_ll_counter):
{
value = ~0;
break;
}
default:
break;
}
return(value);
}
static void pin_change_common_handler(const io_info_entry_t *info, bool interrupt)
{
static uint8_t previous_data[io_pcf_instance_size] = { 0xff, 0xff };
uint8_t current_data[io_pcf_instance_size];
uint8_t *previous, *current;
uint8_t i2c_data[1];
if(!pcf_data.instance[info->instance].counters)
{
log("io_pcf[%u]: pin-change-common-handler: no counters\n", info->address);
return;
}
previous = &previous_data[info->instance];
current = ¤t_data[info->instance];
if(i2c_receive(info->address, sizeof(i2c_data), i2c_data) != i2c_error_ok)
return;
*current = i2c_data[0] & pcf_data.instance[info->instance].counters;
if(*current != *previous)
dispatch_post_task(task_prio_low, task_pins_changed_pcf, *current ^ *previous, *current, info->id);
else
if(interrupt)
log("io_pcf[%u]: pin-change-common-handler called from interrupt with no interrupt pending\n", info->address);
*previous = *current;
}
static void periodic_slow(int io, const io_info_entry_t *info, io_data_entry_t *data, unsigned int rate_ms)
{
if(pcf_data.polling)
pin_change_common_handler(info, false);
}
static void pin_change_handler(int io, io_info_entry_t *info, io_data_entry_t *data)
{
pin_change_common_handler(info, true);
}
static io_error_t pin_mode(string_t *error_message, const struct io_info_entry_T *info, io_data_pin_entry_t *pin_data, const io_config_pin_entry_t *pin_config, int pin)
{
i2c_error_t error;
pcf_data.instance[info->instance].pin_output_cache &= ~(1 << pin);
pcf_data.instance[info->instance].counters &= ~(1 << pin);
switch(pin_config->llmode)
{
case(io_pin_ll_counter):
{
pcf_data.instance[info->instance].counters |= (1 << pin);
[[fallthrough]];
}
case(io_pin_ll_disabled):
case(io_pin_ll_input_digital):
{
pcf_data.instance[info->instance].pin_output_cache |= (1 << pin);
break;
}
case(io_pin_ll_output_digital):
{
break;
}
default:
{
if(error_message)
string_append(error_message, "invalid mode for this pin\n");
return(io_error);
}
}
if((error = i2c_send1(info->address, pcf_data.instance[info->instance].pin_output_cache)) != i2c_error_ok)
{
if(error_message)
i2c_error_format_string(error_message, error);
return(io_error);
}
return(io_ok);
}
static io_error_t read_pin(string_t *error_message, const struct io_info_entry_T *info, io_data_pin_entry_t *pin_data, const io_config_pin_entry_t *pin_config, int pin, unsigned int *value)
{
uint8_t i2c_data[1];
i2c_error_t error;
switch(pin_config->llmode)
{
case(io_pin_ll_input_digital):
case(io_pin_ll_output_digital):
{
if((error = i2c_receive(info->address, sizeof(i2c_data), i2c_data)) != i2c_error_ok)
{
i2c_error_format_string(error_message, error);
return(io_error);
}
break;
}
default:
{
if(error_message)
string_append(error_message, "invalid mode for this pin\n");
return(io_error);
}
}
*value = !!(i2c_data[0] & (1 << pin));
if(pin_config->static_flags & io_flag_static_invert)
*value = !*value;
return(io_ok);
}
static io_error_t write_pin(string_t *error_message, const struct io_info_entry_T *info, io_data_pin_entry_t *pin_data, const io_config_pin_entry_t *pin_config, int pin, unsigned int value)
{
i2c_error_t error;
if(pin_config->static_flags & io_flag_static_invert)
value = !value;
switch(pin_config->llmode)
{
case(io_pin_ll_output_digital):
{
if(value)
pcf_data.instance[info->instance].pin_output_cache |= (1 << pin);
else
pcf_data.instance[info->instance].pin_output_cache &= ~(1 << pin);
if((error = i2c_send1(info->address, pcf_data.instance[info->instance].pin_output_cache)) != i2c_error_ok)
{
i2c_error_format_string(error_message, error);
return(io_error);
}
break;
}
default:
{
if(error_message)
string_append(error_message, "invalid mode for this pin\n");
return(io_error);
}
}
return(io_ok);
}
static io_error_t set_mask(string_t *error_message, const struct io_info_entry_T *info, unsigned int mask, unsigned int pins)
{
i2c_error_t error;
pcf_data.instance[info->instance].pin_output_cache = pins & 0x000000ff;
if((error = i2c_send1(info->address, pcf_data.instance[info->instance].pin_output_cache)) != i2c_error_ok)
{
i2c_error_format_string(error_message, error);
return(io_error);
}
return(io_ok);
}
roflash const io_info_entry_t io_info_entry_pcf_3a =
{
io_id_pcf_3a, /* = 5 */
0x3a,
io_pcf_instance_3a,
8,
caps_input_digital |
caps_counter |
caps_output_digital |
caps_rotary_encoder,
"PCF8574A I2C I/O expander",
init,
(void *)0, // postinit
max_value,
periodic_slow,
(void *)0, // periodic_fast
pin_change_handler,
pin_mode,
(void *)0, // get pin info
read_pin,
write_pin,
set_mask,
};
roflash const io_info_entry_t io_info_entry_pcf_26 =
{
io_id_pcf_26, /* = 7 */
0x26,
io_pcf_instance_26,
8,
caps_input_digital |
caps_counter |
caps_output_digital |
caps_rotary_encoder,
"PCF8574 I2C I/O expander",
init,
(void *)0, // postinit
max_value,
periodic_slow,
(void *)0, // periodic_fast
pin_change_handler,
pin_mode,
(void *)0, // get pin info
read_pin,
write_pin,
set_mask,
};