gmouse_lld_ADS7843_board.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * This file is subject to the terms of the GFX License. If a copy of
  3. * the license was not distributed with this file, you can obtain one at:
  4. *
  5. * http://ugfx.io/license.html
  6. */
  7. #ifndef _GINPUT_LLD_MOUSE_BOARD_H
  8. #define _GINPUT_LLD_MOUSE_BOARD_H
  9. // Resolution and Accuracy Settings
  10. #define GMOUSE_ADS7843_PEN_CALIBRATE_ERROR 8
  11. #define GMOUSE_ADS7843_PEN_CLICK_ERROR 6
  12. #define GMOUSE_ADS7843_PEN_MOVE_ERROR 4
  13. #define GMOUSE_ADS7843_FINGER_CALIBRATE_ERROR 14
  14. #define GMOUSE_ADS7843_FINGER_CLICK_ERROR 18
  15. #define GMOUSE_ADS7843_FINGER_MOVE_ERROR 14
  16. // How much extra data to allocate at the end of the GMouse structure for the board's use
  17. #define GMOUSE_ADS7843_BOARD_DATA_SIZE 0
  18. #define T_CS_HIGH palSetPad(GPIOC, 13)
  19. #define T_CS_LOW palClearPad(GPIOC, 13)
  20. #define T_SCK_HIGH palSetPad(GPIOB, 0)
  21. #define T_SCK_LOW palClearPad(GPIOB, 0)
  22. #define T_MOSI_HIGH palSetPad(GPIOF, 11)
  23. #define T_MOSI_LOW palClearPad(GPIOF, 11)
  24. #define T_MISO_LVL palReadPad(GPIOB, 1)
  25. /**
  26. * @brief Initialise the board for the touch.
  27. *
  28. * @notapi
  29. *
  30. * ADS7843 (clone chip) is connected to this pins:
  31. * T_CS GPIOC_PIN13
  32. * T_SCK GPIOB_PIN0
  33. * T_MISO GPIOB_PIN2
  34. * T_MOSI GPIOF_PIN11
  35. * T_PEN GPIOB_PIN1
  36. */
  37. static gBool init_board(GMouse* m, unsigned driverinstance) {
  38. (void)m;
  39. (void)driverinstance;
  40. /*
  41. palSetPadMode(GPIOC, 13, PAL_MODE_OUTPUT_PUSHPULL);
  42. palSetPadMode(GPIOB, 0, PAL_MODE_OUTPUT_PUSHPULL);
  43. palSetPadMode(GPIOB, 2, PAL_MODE_INPUT_PULLUP);
  44. palSetPadMode(GPIOF, 11, PAL_MODE_OUTPUT_PUSHPULL);
  45. palSetPadMode(GPIOB, 1, PAL_MODE_INPUT);
  46. * GPIO config must be done by board.h
  47. */
  48. T_CS_HIGH;
  49. T_SCK_LOW;
  50. return true;
  51. }
  52. /**
  53. * @brief Check whether the surface is currently touched
  54. * @return TRUE if the surface is currently touched
  55. *
  56. * @notapi
  57. * PB1 is connected to TP_IRQ (low active).
  58. */
  59. static GFXINLINE gBool getpin_pressed(GMouse* m) {
  60. (void)m;
  61. return (!palReadPad(GPIOB, T_PEN));
  62. }
  63. /**
  64. * @brief Aquire the bus ready for readings
  65. *
  66. * @notapi
  67. * PC13 is connected to TP_CS (low active):
  68. */
  69. static GFXINLINE void aquire_bus(GMouse* m) {
  70. (void)m;
  71. T_CS_LOW;
  72. T_SCK_LOW;
  73. }
  74. /**
  75. * @brief Release the bus after readings
  76. *
  77. * @notapi
  78. */
  79. static GFXINLINE void release_bus(GMouse* m) {
  80. (void)m;
  81. T_CS_HIGH;
  82. }
  83. /**
  84. * @brief Write a value to touch controller
  85. * @return none
  86. *
  87. * params[in] value The value to be write.
  88. *
  89. * @notapi
  90. */
  91. static GFXINLINE void write_value(GMouse* m, gU16 value) {
  92. (void)m;
  93. int i;
  94. for (i=8; i>0; i--) {
  95. T_SCK_LOW;
  96. if (value & 0x0080) {
  97. T_MOSI_HIGH;
  98. } else {
  99. T_MOSI_LOW;
  100. }
  101. asm("nop");
  102. value <<= 1;
  103. T_SCK_HIGH;
  104. }
  105. }
  106. /**
  107. * @brief Read a value from touch controller
  108. * @return The value read from the controller
  109. *
  110. * params[in] port The controller port to read.
  111. *
  112. * @notapi
  113. */
  114. static GFXINLINE gU16 read_value(GMouse* m, gU16 port) {
  115. (void)m;
  116. int i;
  117. gU16 data = 0;
  118. write_value(m, port);
  119. gfxSleepMicroseconds(6); //The conversion time of ADS7846 is up to 6us
  120. T_SCK_LOW;
  121. asm("nop"); // or gfxSleepMicroseconds(1) ?
  122. T_SCK_HIGH; //Give 1 clock, clear BUSY
  123. asm("nop");
  124. //Read 16-bit data, only the upper 12 bits are valid
  125. for (i=16; i>0; i--) {
  126. T_SCK_LOW;
  127. data <<= 1;
  128. asm("nop");
  129. T_SCK_HIGH;
  130. if (T_MISO_LVL) {
  131. data ++;
  132. }
  133. }
  134. data >>= 4;
  135. return data;
  136. }
  137. #endif /* _GINPUT_LLD_MOUSE_BOARD_H */