synaptics_touchpad.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /* Copyright (c) 2009 Nordic Semiconductor. All Rights Reserved.
  2. *
  3. * The information contained herein is property of Nordic Semiconductor ASA.
  4. * Terms and conditions of usage are described in detail in NORDIC
  5. * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
  6. *
  7. * Licensees are granted free, non-transferable use of the information. NO
  8. * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
  9. * the file.
  10. *
  11. */
  12. #include <stdbool.h>
  13. #include <stdint.h>
  14. #include "twi_master.h"
  15. #include "synaptics_touchpad.h"
  16. /*lint ++flb "Enter library region" */
  17. #define PRODUCT_ID_BYTES 10U //!< Number of bytes to expect to be in product ID
  18. static uint8_t m_device_address; // !< Device address in bits [7:1]
  19. static const uint8_t expected_product_id[PRODUCT_ID_BYTES] = {'T', 'M', '1', '9', '4', '4', '-', '0', '0', '2'}; //!< Product ID expected to get from product ID query
  20. bool touchpad_init(uint8_t device_address)
  21. {
  22. bool transfer_succeeded = true;
  23. m_device_address = (uint8_t)(device_address << 1);
  24. // Do a soft reset
  25. uint8_t reset_command = 0x01;
  26. transfer_succeeded &= touchpad_write_register(TOUCHPAD_RESET, reset_command);
  27. // Page select 0
  28. uint8_t page_to_select = 0x00;
  29. transfer_succeeded &= touchpad_write_register(TOUCHPAD_PAGESELECT, page_to_select);
  30. // Read and verify product ID
  31. transfer_succeeded &= touchpad_product_id_verify();
  32. return transfer_succeeded;
  33. }
  34. bool touchpad_product_id_verify(void)
  35. {
  36. bool transfer_succeeded = true;
  37. uint8_t product_id[PRODUCT_ID_BYTES];
  38. transfer_succeeded &= touchpad_product_id_read(product_id, PRODUCT_ID_BYTES);
  39. for (uint8_t i = 0; i < 10; i++)
  40. {
  41. if (product_id[i] != expected_product_id[i])
  42. {
  43. transfer_succeeded = false;
  44. }
  45. }
  46. return transfer_succeeded;
  47. }
  48. bool touchpad_reset(void)
  49. {
  50. uint8_t w2_data[2] = {TOUCHPAD_COMMAND, 0x01};
  51. return twi_master_transfer(m_device_address, w2_data, 2, TWI_ISSUE_STOP);
  52. }
  53. bool touchpad_interrupt_status_read(uint8_t *interrupt_status)
  54. {
  55. return touchpad_read_register(TOUCHPAD_INT_STATUS, interrupt_status);
  56. }
  57. bool touchpad_set_sleep_mode(TouchpadSleepMode_t mode)
  58. {
  59. return touchpad_write_register(TOUCHPAD_CONTROL, (uint8_t)mode);
  60. }
  61. bool touchpad_read_register(uint8_t register_address, uint8_t *value)
  62. {
  63. bool transfer_succeeded = true;
  64. transfer_succeeded &= twi_master_transfer(m_device_address, &register_address, 1, TWI_DONT_ISSUE_STOP);
  65. if (transfer_succeeded)
  66. {
  67. transfer_succeeded &= twi_master_transfer(m_device_address | TWI_READ_BIT, value, 1, TWI_ISSUE_STOP);
  68. }
  69. return transfer_succeeded;
  70. }
  71. bool touchpad_write_register(uint8_t register_address, const uint8_t value)
  72. {
  73. uint8_t w2_data[2];
  74. w2_data[0] = register_address;
  75. w2_data[1] = value;
  76. return twi_master_transfer(m_device_address, w2_data, 2, TWI_ISSUE_STOP);
  77. }
  78. bool touchpad_product_id_read(uint8_t * product_id, uint8_t product_id_bytes)
  79. {
  80. uint8_t w2_data[1];
  81. bool transfer_succeeded = true;
  82. w2_data[0] = TOUCHPAD_PRODUCT_ID;
  83. transfer_succeeded &= twi_master_transfer(m_device_address, w2_data, 1, TWI_DONT_ISSUE_STOP);
  84. if (transfer_succeeded)
  85. {
  86. transfer_succeeded &= twi_master_transfer(m_device_address | TWI_READ_BIT, product_id, product_id_bytes, TWI_ISSUE_STOP);
  87. }
  88. return transfer_succeeded;
  89. }
  90. /*lint --flb "Leave library region" */