Skip to content
This repository was archived by the owner on Apr 28, 2021. It is now read-only.

Commit 8ce9724

Browse files
committed
handling pins polarity now: if they need to be activated high or low
1 parent 14099e2 commit 8ce9724

File tree

8 files changed

+98
-43
lines changed

8 files changed

+98
-43
lines changed

driver_axp209.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,18 @@ static char i2c0_sysfs_filename[] = "/dev/i2c-0";
4040
* Public functions
4141
****************************************************************/
4242
bool axp209_init(void) {
43+
int err;
44+
45+
/* Open i2c file interface */
4346
if ((fd_axp209 = open(i2c0_sysfs_filename,O_RDWR)) < 0) {
4447
printf("In axp209_init - Failed to open the I2C bus %s", i2c0_sysfs_filename);
4548
// ERROR HANDLING; you can check errno to see what went wrong
4649
return false;
4750
}
4851

52+
/* Acquire AXP209 bus */
4953
if (ioctl(fd_axp209, I2C_SLAVE, AXP209_I2C_ADDR) < 0) {
50-
printf("In axp209_init - Failed to acquire bus access and/or talk to slave.\n");
54+
printf("In axp209_init - Failed to acquire bus access and/or talk to slave, trying to force it\n");
5155
// ERROR HANDLING; you can check errno to see what went wrong
5256
if (ioctl(fd_axp209, I2C_SLAVE_FORCE, AXP209_I2C_ADDR) < 0) {
5357
printf("In axp209_init - Failed to acquire FORCED bus access and/or talk to slave.\n");
@@ -56,8 +60,13 @@ bool axp209_init(void) {
5660
}
5761
}
5862

59-
// Enable only chosen interrupts (PEK short and long presses)
60-
int err;
63+
/* Set N_OE Shutdown delay to 3S*/
64+
err = i2c_smbus_write_byte_data(fd_axp209 , AXP209_REG_32H, 0x43);
65+
if(err < 0){
66+
printf("ERROR Setting AXP209 N_OE Shutdown delay to 3S\n");
67+
}
68+
69+
/* Enable only chosen interrupts (PEK short and long presses)*/
6170
/*err = i2c_smbus_write_byte_data(fd_axp209 , AXP209_INTERRUPT_BANK_1_ENABLE, 0x00);
6271
if(err < 0){
6372
printf("ERROR initializing interrupts 1 for AXP209\n");

driver_axp209.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#define AXP209_I2C_ADDR 0x34
1515

1616
// Chip registers adresses
17+
#define AXP209_REG_32H 0x32
1718
#define AXP209_INTERRUPT_BANK_1_ENABLE 0x40
1819
#define AXP209_INTERRUPT_BANK_1_STATUS 0x48
1920
#define AXP209_INTERRUPT_BANK_2_ENABLE 0x41

funkey_gpio_management.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ int main(int argc, char **argv, char **envp)
4545
// Variables
4646
STRUCT_MAPPED_GPIO * chained_list_mapping = NULL;
4747
int nb_valid_gpios = 0;
48-
int * gpio_pins = NULL;
48+
int * gpio_pins_idx_declared = NULL;
49+
bool * gpios_pins_active_high = NULL;
4950

5051
// Set the signal callback for Ctrl-C
5152
signal(SIGINT, signal_handler);
@@ -54,10 +55,10 @@ int main(int argc, char **argv, char **envp)
5455
init_uinput();
5556

5657
// Read Conf File: Get GPIO pins to declare and all valid pin mappings
57-
get_mapping_from_conf_file(&chained_list_mapping, &nb_valid_gpios, &gpio_pins);
58+
get_mapping_from_conf_file(&chained_list_mapping, &nb_valid_gpios, &gpio_pins_idx_declared, &gpios_pins_active_high);
5859

5960
// Init GPIOs
60-
init_mapping_gpios(gpio_pins, nb_valid_gpios, chained_list_mapping);
61+
init_mapping_gpios(gpio_pins_idx_declared, gpios_pins_active_high, nb_valid_gpios, chained_list_mapping);
6162

6263
// Main Loop
6364
while (keepgoing) {

funkey_gpio_mapping.conf

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
# Format:
55
#
66
# - First all GPIO Pin numbers must be declared (integers separated by commas)
7-
# Example: 0,1,2,3,4,6,7,11,12,13,14,15
7+
# By default, pins are declared active high, to declare them active low, add char '*'
8+
# Example: 0,1,2,3,4,6,7,10*,11,12,13,14,15
89
#
910
# - Then the mapping can be done as follows (one line per mapping):
1011
# Pin_number[+Pin_number...], type_mapping, value, str_help_name_pin, str_help_fct_pin
@@ -22,7 +23,7 @@
2223

2324
###################################
2425
# Pins declaration:
25-
0,1,2,3,4,6,7,10,11,12,13,14,15
26+
0,1,2,3,4,6,7,10*,11,12,13,14,15
2627

2728

2829
###################################
@@ -50,7 +51,8 @@
5051
7+13, KEYBOARD, KEY_W, KEY_W, Brightness--
5152
11, KEYBOARD, KEY_Y, KEY_Y, Y
5253
7+11, KEYBOARD, KEY_C, KEY_C, Volume++
53-
10, SHELL_COMMAND, poweroff, N_OE, Poweroff because N_OE_received
54+
#10, SHELL_COMMAND, poweroff, N_OE, Poweroff because N_OE_received
55+
10, KEYBOARD, KEY_T, KEY_T, Should Poweroff because N_OE_received
5456

5557
# Bypass to remove when button axp working:
5658
#7+15, KEYBOARD, KEY_Q, 7+11, Launch menu

gpio_mapping.c

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
} while(0)
2424

2525
#define DEBUG_GPIO_PRINTF (0)
26+
#define DEBUG_PERIODIC_CHECK_PRINTF (0)
2627
#define ERROR_GPIO_PRINTF (1)
2728

2829
#if (DEBUG_GPIO_PRINTF)
@@ -31,6 +32,12 @@
3132
#define GPIO_PRINTF(...)
3233
#endif
3334

35+
#if (DEBUG_PERIODIC_CHECK_PRINTF)
36+
#define PERIODIC_CHECK_PRINTF(...) printf(__VA_ARGS__);
37+
#else
38+
#define PERIODIC_CHECK_PRINTF(...)
39+
#endif
40+
3441
#if (ERROR_GPIO_PRINTF)
3542
#define GPIO_ERROR_PRINTF(...) printf(__VA_ARGS__);
3643
#else
@@ -53,7 +60,8 @@
5360
* Static variables
5461
****************************************************************/
5562
static int nb_mapped_gpios;
56-
static int * gpio_pins;
63+
static int * gpio_pins_idx_declared;
64+
static bool * gpios_pins_active_high;
5765
STRUCT_MAPPED_GPIO * chained_list_mapping;
5866
static int max_fd = 0;
5967
static int gpio_fd_interrupt_expander_gpio;
@@ -62,8 +70,6 @@ static fd_set fds;
6270
static bool * mask_gpio_value;
6371
static bool interrupt_i2c_expander_found = false;
6472
static bool interrupt_axp209_found = false;
65-
static bool mapping_PEK_short_press_activated = false;
66-
static bool mapping_PEK_long_press_activated = false;
6773

6874

6975
/****************************************************************
@@ -136,7 +142,7 @@ static void find_and_call_mapping_function(int idx_gpio_interrupted,
136142

137143
//GPIO_PRINTF(" Mapping searching for idx_gpio_interrupted: %d, and %s: \n", idx_gpio_interrupted, activation?"activation":"deactivation")
138144
for (i=0; i < current->nb_simultaneous_pins; i++){
139-
//GPIO_PRINTF(" Pin in mapping: %d, pin_idx=%d\n", gpio_pins[current->pins_idx[i]], current->pins_idx[i]);
145+
//GPIO_PRINTF(" Pin in mapping: %d, pin_idx=%d\n", gpio_pins_idx_declared[current->pins_idx[i]], current->pins_idx[i]);
140146
// Find if current mapping contains interrupted pin
141147
if (current->pins_idx[i] == idx_gpio_interrupted){
142148
gpio_found_pin_in_mapping = true;
@@ -156,7 +162,7 @@ static void find_and_call_mapping_function(int idx_gpio_interrupted,
156162
// if real mapping already found => need to deactivate previously activated ones
157163
if(mapping_found && current->activated){
158164
GPIO_PRINTF(" Mapping Deactivation because real one already found: GPIO %d found in following activated mapping: \n",
159-
gpio_pins[idx_gpio_interrupted]);
165+
gpio_pins_idx_declared[idx_gpio_interrupted]);
160166
print_chained_list_node(current);
161167
apply_mapping_desactivation(current);
162168
}
@@ -167,15 +173,15 @@ static void find_and_call_mapping_function(int idx_gpio_interrupted,
167173
}
168174
// Print information and activate mapping
169175
GPIO_PRINTF(" Mapping Activation: GPIO %d found in following deactivated mapping: \n",
170-
gpio_pins[idx_gpio_interrupted]);
176+
gpio_pins_idx_declared[idx_gpio_interrupted]);
171177
print_chained_list_node(current);
172178
apply_mapping_activation(current);
173179
}
174180
}
175181
else{ // Treating deactivation cases
176182
if(current->activated){
177183
GPIO_PRINTF(" Mapping Desactivation: GPIO %d found in following activated mapping: \n",
178-
gpio_pins[idx_gpio_interrupted]);
184+
gpio_pins_idx_declared[idx_gpio_interrupted]);
179185
print_chained_list_node(current);
180186
apply_mapping_desactivation(current);
181187
}
@@ -229,7 +235,9 @@ static int deinit_gpio_interrupt(int fd_saved)
229235
* Public functions
230236
****************************************************************/
231237
/***** Init I2C expander pin mappings *****/
232-
int init_mapping_gpios(int * gpio_pins_to_declare, int nb_gpios_to_declare,
238+
int init_mapping_gpios(int * gpio_pins_to_declare,
239+
bool * gpios_pins_active_high_declared,
240+
int nb_gpios_to_declare,
233241
STRUCT_MAPPED_GPIO * chained_list_mapping_gpios)
234242
{
235243
// Variables
@@ -238,7 +246,8 @@ int init_mapping_gpios(int * gpio_pins_to_declare, int nb_gpios_to_declare,
238246

239247
// Store arguments
240248
nb_mapped_gpios = nb_gpios_to_declare;
241-
gpio_pins = gpio_pins_to_declare;
249+
gpio_pins_idx_declared = gpio_pins_to_declare;
250+
gpios_pins_active_high = gpios_pins_active_high_declared;
242251
chained_list_mapping = chained_list_mapping_gpios;
243252

244253
// Init values
@@ -308,6 +317,7 @@ int listen_gpios_interrupts(void)
308317
int nb_interrupts = 0;
309318
bool previous_mask_gpio_value[nb_mapped_gpios];
310319
bool mask_gpio_current_interrupts[nb_mapped_gpios];
320+
bool forced_interrupt = false;
311321

312322
// Back up master
313323
fd_set dup = fds;
@@ -331,11 +341,12 @@ int listen_gpios_interrupts(void)
331341
#endif //TIMEOUT_SEC_SANITY_CHECK_GPIO_EXP
332342
if(!nb_interrupts){
333343
// Timeout case
334-
GPIO_PRINTF(" Timeout, forcing sanity check\n");
344+
PERIODIC_CHECK_PRINTF(" Timeout, forcing sanity check\n");
335345

336346
// Timeout forcing a "Found interrupt" event for sanity check
337347
interrupt_i2c_expander_found = true;
338348
interrupt_axp209_found = true;
349+
forced_interrupt = true;
339350
}
340351
else if ( nb_interrupts < 0) {
341352
perror("select");
@@ -378,7 +389,12 @@ int listen_gpios_interrupts(void)
378389

379390
#ifdef ENABLE_AXP209_INTERRUPTS
380391
if(interrupt_axp209_found){
381-
GPIO_PRINTF(" Processing interrupt AXP209\n");
392+
if(forced_interrupt){
393+
PERIODIC_CHECK_PRINTF(" Processing forced interrupt AXP209\n");
394+
}
395+
else{
396+
GPIO_PRINTF(" Processing real interrupt AXP209\n");
397+
}
382398
int val_int_bank_3 = axp209_read_interrupt_bank_3();
383399
if(val_int_bank_3 < 0){
384400
GPIO_PRINTF(" Could not read AXP209 with I2C\n");
@@ -389,21 +405,23 @@ int listen_gpios_interrupts(void)
389405
if(val_int_bank_3 & AXP209_INTERRUPT_PEK_SHORT_PRESS){
390406
GPIO_PRINTF(" AXP209 short PEK key press detected\n");
391407
sendKeyAndStopKey(KEY_IDX_MAPPED_FOR_SHORT_PEK_PRESS);
392-
/*sendKey(KEY_IDX_MAPPED_FOR_SHORT_PEK_PRESS, !mapping_PEK_short_press_activated);
393-
mapping_PEK_short_press_activated = !mapping_PEK_short_press_activated;*/
394408

395409
}
396410
if(val_int_bank_3 & AXP209_INTERRUPT_PEK_LONG_PRESS){
397411
GPIO_PRINTF(" AXP209 long PEK key press detected\n");
398412
sendKeyAndStopKey(KEY_IDX_MAPPED_FOR_LONG_PEK_PRESS);
399-
/*sendKey(KEY_IDX_MAPPED_FOR_LONG_PEK_PRESS, !mapping_PEK_long_press_activated);
400-
mapping_PEK_long_press_activated = !mapping_PEK_long_press_activated;*/
401413
}
402414
}
403415
#endif //ENABLE_AXP209_INTERRUPTS
404416

405417
if(interrupt_i2c_expander_found){
406-
GPIO_PRINTF(" Processing interrupt PCAL6416AHF\n");
418+
if(forced_interrupt){
419+
PERIODIC_CHECK_PRINTF(" Processing forced interrupt PCAL6416AHF\n");
420+
}
421+
else{
422+
GPIO_PRINTF(" Processing real interrupt PCAL6416AHF\n");
423+
}
424+
407425
// Read I2C GPIO masks:
408426
int val_i2c_mask_interrupted = pcal6416a_read_mask_interrupts();
409427
if(val_i2c_mask_interrupted < 0){
@@ -420,25 +438,26 @@ int listen_gpios_interrupts(void)
420438

421439
// Find GPIO idx correspondance
422440
for (idx_gpio=0; idx_gpio<nb_mapped_gpios; idx_gpio++){
423-
uint16_t tmp_mask_gpio = (1 << gpio_pins[idx_gpio]);
441+
uint16_t tmp_mask_gpio = (1 << gpio_pins_idx_declared[idx_gpio]);
424442

425443
// Found GPIO idx in active GPIOs
426444
if(val_i2c_mask_active & tmp_mask_gpio){
427445
mask_gpio_value[idx_gpio] = true;
428446
}
429-
447+
430448
// Found GPIO idx in interrupt mask
431449
if(val_i2c_mask_interrupted & tmp_mask_gpio){
432450
// Print information
433-
GPIO_PRINTF(" --> Interrupt GPIO: %d, idx_pin: %d\n", gpio_pins[idx_gpio], idx_gpio);
451+
GPIO_PRINTF(" --> Interrupt GPIO: %d, idx_pin: %d\n", gpio_pins_idx_declared[idx_gpio], idx_gpio);
434452
mask_gpio_current_interrupts[idx_gpio] = true;
435453
}
436454

437455
// Sanity check: if we missed an interrupt for some reason, check if the new values are the same
438456
if( !mask_gpio_current_interrupts[idx_gpio] &&
439457
(mask_gpio_value[idx_gpio] != previous_mask_gpio_value[idx_gpio]) ){
440458
// Print information
441-
GPIO_PRINTF(" --> No Interrupt (missed) but value has changed on GPIO: %d, idx_pin: %d\n", gpio_pins[idx_gpio], idx_gpio);
459+
GPIO_PRINTF(" --> No Interrupt (missed) but value has changed on GPIO: %d, idx_pin: %d\n",
460+
gpio_pins_idx_declared[idx_gpio], idx_gpio);
442461
mask_gpio_current_interrupts[idx_gpio] = true;
443462
}
444463
}
@@ -448,7 +467,8 @@ int listen_gpios_interrupts(void)
448467
for (idx_gpio=0; idx_gpio<nb_mapped_gpios; idx_gpio++){
449468
if(!mask_gpio_current_interrupts[idx_gpio]) continue;
450469

451-
if (mask_gpio_value[idx_gpio]){
470+
if ( (mask_gpio_value[idx_gpio] && gpios_pins_active_high[idx_gpio]) ||
471+
(!mask_gpio_value[idx_gpio] && !gpios_pins_active_high[idx_gpio]) ){
452472
find_and_call_mapping_function(idx_gpio,
453473
chained_list_mapping,
454474
mask_gpio_current_interrupts,

gpio_mapping.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
****************************************************************/
77
#include "gpio-utils.h"
88
#include "uinput.h"
9-
#include "read_conf_file.h"
9+
#include "read_conf_file.h"
1010

1111
/****************************************************************
1212
* Defines
@@ -17,7 +17,9 @@
1717
/****************************************************************
1818
* Public functions
1919
****************************************************************/
20-
int init_mapping_gpios(int * gpio_pins_to_declare, int nb_gpios_to_declare,
20+
int init_mapping_gpios(int * gpio_pins_to_declare,
21+
bool * gpios_pins_active_high_declared,
22+
int nb_gpios_to_declare,
2123
STRUCT_MAPPED_GPIO * chained_list_mapping_gpios);
2224
int deinit_mapping_gpios(void);
2325
int listen_gpios_interrupts(void);

0 commit comments

Comments
 (0)