driver_pcal6416a.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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("In pcal6416a_init - Failed to acquire bus access and/or talk to slave.\n");
  45. // ERROR HANDLING; you can check errno to see what went wrong
  46. if (ioctl(fd_i2c_expander, I2C_SLAVE_FORCE, PCAL6416A_I2C_ADDR) < 0) {
  47. printf("In pcal6416a_init - Failed to acquire FORCED bus access and/or talk to slave.\n");
  48. // ERROR HANDLING; you can check errno to see what went wrong
  49. return false;
  50. }
  51. return false;
  52. }
  53. uint16_t val_enable_direction = 0xffff;
  54. i2c_smbus_write_word_data ( fd_i2c_expander , PCAL6416A_CONFIG , val_enable_direction );
  55. uint16_t val_enable_latch = 0x0000;
  56. i2c_smbus_write_word_data ( fd_i2c_expander , PCAL6416A_INPUT_LATCH , val_enable_latch );
  57. uint16_t val_enable_pullups = 0xffff;
  58. i2c_smbus_write_word_data ( fd_i2c_expander , PCAL6416A_EN_PULLUPDOWN , val_enable_pullups );
  59. uint16_t val_sel_pullups = 0xffff;
  60. i2c_smbus_write_word_data ( fd_i2c_expander , PCAL6416A_SEL_PULLUPDOWN , val_sel_pullups );
  61. //uint16_t val_enable_interrupts = 0x0000;
  62. uint16_t val_enable_interrupts = 0x0320;
  63. i2c_smbus_write_word_data ( fd_i2c_expander , PCAL6416A_INT_MASK , val_enable_interrupts );
  64. return true;
  65. }
  66. bool pcal6416a_deinit(void) {
  67. // Close I2C open interface
  68. close(fd_i2c_expander);
  69. return true;
  70. }
  71. int pcal6416a_read_mask_interrupts(void){
  72. int val_int = i2c_smbus_read_word_data ( fd_i2c_expander , PCAL6416A_INT_STATUS );
  73. if(val_int < 0){
  74. return val_int;
  75. }
  76. uint16_t val = val_int & 0xFFFF;
  77. DEBUG_PRINTF("READ PCAL6416A_INT_STATUS : 0x%04X\n",val);
  78. return (int) val;
  79. }
  80. int pcal6416a_read_mask_active_GPIOs(void){
  81. int val_int = i2c_smbus_read_word_data ( fd_i2c_expander , PCAL6416A_INPUT );
  82. if(val_int < 0){
  83. return val_int;
  84. }
  85. uint16_t val = val_int & 0xFFFF;
  86. val = 0xFFFF-val;
  87. DEBUG_PRINTF("READ PCAL6416A_INPUT (active GPIOs) : 0x%04X\n",val);
  88. return (int) val;
  89. }