driver_pcal6416a.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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-dev.h>
  14. #include "driver_pcal6416a.h"
  15. /****************************************************************
  16. * Defines
  17. ****************************************************************/
  18. #define DEBUG_PCAL6416A_PRINTF (0)
  19. #if (DEBUG_PCAL6416A_PRINTF)
  20. #define DEBUG_PRINTF(...) printf(__VA_ARGS__);
  21. #else
  22. #define DEBUG_PRINTF(...)
  23. #endif
  24. /****************************************************************
  25. * Static variables
  26. ****************************************************************/
  27. int fd_i2c_expander;
  28. char i2c0_sysfs_filename[] = "/dev/i2c-0";
  29. /****************************************************************
  30. * Static functions
  31. ****************************************************************/
  32. /****************************************************************
  33. * Public functions
  34. ****************************************************************/
  35. bool pcal6416a_init(void) {
  36. if ((fd_i2c_expander = open(i2c0_sysfs_filename,O_RDWR)) < 0) {
  37. printf("Failed to open the I2C bus %s", i2c0_sysfs_filename);
  38. // ERROR HANDLING; you can check errno to see what went wrong
  39. return false;
  40. }
  41. if (ioctl(fd_i2c_expander,I2C_SLAVE,PCAL6416A_I2C_ADDR) < 0) {
  42. printf("Failed to acquire bus access and/or talk to slave.\n");
  43. // ERROR HANDLING; you can check errno to see what went wrong
  44. return false;
  45. }
  46. uint16_t val_enable_pullups = 0xffff;
  47. i2c_smbus_write_word_data ( fd_i2c_expander , PCAL6416A_EN_PULLUPDOWN , val_enable_pullups );
  48. uint16_t val_enable_interrupts = 0x0000;
  49. i2c_smbus_write_word_data ( fd_i2c_expander , PCAL6416A_INT_MASK , val_enable_interrupts );
  50. return true;
  51. }
  52. bool pcal6416a_deinit(void) {
  53. // Close I2C open interface
  54. close(fd_i2c_expander);
  55. return true;
  56. }
  57. uint16_t pcal6416a_read_mask_interrupts(void){
  58. uint16_t val = i2c_smbus_read_word_data ( fd_i2c_expander , PCAL6416A_INT_STATUS );
  59. DEBUG_PRINTF("READ PCAL6416A_INT_STATUS : 0x%04X\n",val);
  60. return val;
  61. }
  62. uint16_t pcal6416a_read_mask_active_GPIOs(void){
  63. uint16_t val = i2c_smbus_read_word_data ( fd_i2c_expander , PCAL6416A_INPUT );
  64. val = 0xFFFF-val;
  65. DEBUG_PRINTF("READ PCAL6416A_INPUT (active GPIOs) : 0x%04X\n",val);
  66. return val;
  67. }