driver_pcal6416a.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #include <stdbool.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <unistd.h>
  7. #include <fcntl.h>
  8. #include <errno.h>
  9. #include <sys/select.h>
  10. #include <linux/input.h>
  11. #include "read_conf_file.h"
  12. #include <assert.h>
  13. #include <linux/i2c.h>
  14. #include <linux/i2c-dev.h>
  15. #include "smbus.h"
  16. #include "driver_pcal6416a.h"
  17. /****************************************************************
  18. * Defines
  19. ****************************************************************/
  20. #define DEBUG_PCAL6416A_PRINTF (0)
  21. #if (DEBUG_PCAL6416A_PRINTF)
  22. #define DEBUG_PRINTF(...) printf(__VA_ARGS__);
  23. #else
  24. #define DEBUG_PRINTF(...)
  25. #endif
  26. /****************************************************************
  27. * Static variables
  28. ****************************************************************/
  29. static int fd_i2c_expander;
  30. static char i2c0_sysfs_filename[] = "/dev/i2c-0";
  31. /****************************************************************
  32. * Static functions
  33. ****************************************************************/
  34. /****************************************************************
  35. * Public functions
  36. ****************************************************************/
  37. bool pcal6416a_init(void) {
  38. if ((fd_i2c_expander = open(i2c0_sysfs_filename,O_RDWR)) < 0) {
  39. printf("Failed to open the I2C bus %s", i2c0_sysfs_filename);
  40. // ERROR HANDLING; you can check errno to see what went wrong
  41. return false;
  42. }
  43. if (ioctl(fd_i2c_expander,I2C_SLAVE,PCAL6416A_I2C_ADDR) < 0) {
  44. printf("Failed to acquire bus access and/or talk to slave.\n");
  45. // ERROR HANDLING; you can check errno to see what went wrong
  46. return false;
  47. }
  48. uint16_t val_enable_direction = 0xffff;
  49. i2c_smbus_write_word_data ( fd_i2c_expander , PCAL6416A_CONFIG , val_enable_direction );
  50. uint16_t val_enable_latch = 0x0000;
  51. i2c_smbus_write_word_data ( fd_i2c_expander , PCAL6416A_INPUT_LATCH , val_enable_latch );
  52. uint16_t val_enable_pullups = 0xffff;
  53. i2c_smbus_write_word_data ( fd_i2c_expander , PCAL6416A_EN_PULLUPDOWN , val_enable_pullups );
  54. uint16_t val_sel_pullups = 0xffff;
  55. i2c_smbus_write_word_data ( fd_i2c_expander , PCAL6416A_SEL_PULLUPDOWN , val_sel_pullups );
  56. //uint16_t val_enable_interrupts = 0x0000;
  57. uint16_t val_enable_interrupts = 0x0320;
  58. i2c_smbus_write_word_data ( fd_i2c_expander , PCAL6416A_INT_MASK , val_enable_interrupts );
  59. return true;
  60. }
  61. bool pcal6416a_deinit(void) {
  62. // Close I2C open interface
  63. close(fd_i2c_expander);
  64. return true;
  65. }
  66. int pcal6416a_read_mask_interrupts(void){
  67. int val_int = i2c_smbus_read_word_data ( fd_i2c_expander , PCAL6416A_INT_STATUS );
  68. if(val_int < 0){
  69. return val_int;
  70. }
  71. uint16_t val = val_int & 0xFFFF;
  72. DEBUG_PRINTF("READ PCAL6416A_INT_STATUS : 0x%04X\n",val);
  73. return (int) val;
  74. }
  75. int pcal6416a_read_mask_active_GPIOs(void){
  76. int val_int = i2c_smbus_read_word_data ( fd_i2c_expander , PCAL6416A_INPUT );
  77. if(val_int < 0){
  78. return val_int;
  79. }
  80. uint16_t val = val_int & 0xFFFF;
  81. val = 0xFFFF-val;
  82. DEBUG_PRINTF("READ PCAL6416A_INPUT (active GPIOs) : 0x%04X\n",val);
  83. return (int) val;
  84. }