Advertisement
Guest User

ads7846

a guest
Apr 8th, 2020
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.42 KB | None | 0 0
  1. /*********************************************************************
  2. *                SEGGER Microcontroller GmbH & Co. KG                *
  3. *        Solutions for real time microcontroller applications        *
  4. **********************************************************************
  5. *                                                                    *
  6. *        (c) 1996 - 2012  SEGGER Microcontroller GmbH & Co. KG       *
  7. *                                                                    *
  8. *        Internet: www.segger.com    Support:  support@segger.com    *
  9. *                                                                    *
  10. **********************************************************************
  11.  
  12. ** emWin V5.12 - Graphical user interface for embedded applications **
  13. emWin is protected by international copyright laws.   Knowledge of the
  14. source code may not be used to write a similar product.  This file may
  15. only be used in accordance with a license and should not be re-
  16. distributed in any way. We appreciate your understanding and fairness.
  17. ----------------------------------------------------------------------
  18. File        : TouchConf.c
  19. Purpose     : Touch screen hardware access routines and driver
  20.               configuration for Texas Instruments ADS7846 touch
  21.               screen controller.
  22. ---------------------------END-OF-HEADER------------------------------
  23. */
  24.  
  25. //#include "RTOS.h"
  26. #include "GUI.h"
  27. #include "main.h"
  28. #include "GUI_Type.h"
  29. #include "GUITDRV_ADS7846.h"
  30. #include "usart.h";
  31. #include <string.h>
  32.  
  33. /*********************************************************************
  34. *
  35. *       Macros
  36. *
  37. **********************************************************************
  38. */
  39.  
  40. // #define DEBUG_TOUCH
  41.  
  42. s[30];
  43.  
  44. #define S_DELAY() {for (int i=0; i < 3000; i++){ \
  45.   asm( \
  46. "mov     r0, r0\n\t"\
  47. "mov     r0, r0\n\t"\
  48. "mov     r0, r0"\
  49. );}};
  50.  
  51. #define CLK_SET()     {S_DELAY(); HAL_GPIO_WritePin(T_SCK_GPIO_Port, T_SCK_Pin, GPIO_PIN_SET);}
  52. #define CLK_CLR()     {S_DELAY(); HAL_GPIO_WritePin(T_SCK_GPIO_Port, T_SCK_Pin, GPIO_PIN_RESET);}
  53.  
  54. //#define CLK_SET()     {HAL_Delay(1); HAL_GPIO_WritePin(T_SCK_GPIO_Port, T_SCK_Pin, GPIO_PIN_SET);}
  55. //#define CLK_CLR()     {HAL_Delay(1); HAL_GPIO_WritePin(T_SCK_GPIO_Port, T_SCK_Pin, GPIO_PIN_RESET);}
  56.  
  57. #define CS_SET()      HAL_GPIO_WritePin(T_CS_GPIO_Port, T_CS_Pin, GPIO_PIN_RESET)
  58. #define CS_CLR()      HAL_GPIO_WritePin(T_CS_GPIO_Port, T_CS_Pin, GPIO_PIN_SET)
  59. #define DO_SET()      HAL_GPIO_WritePin(T_MOSI_GPIO_Port, T_MOSI_Pin, GPIO_PIN_SET)
  60. #define DO_CLR()      HAL_GPIO_WritePin(T_MOSI_GPIO_Port, T_MOSI_Pin, GPIO_PIN_RESET)
  61. #define DI_VAL()      HAL_GPIO_ReadPin(T_MISO_GPIO_Port, T_MISO_Pin)
  62. #define BUSY_VAL()    (0)
  63. #define PENIRQ_VAL()  HAL_GPIO_ReadPin(T_PEN_GPIO_Port, T_PEN_Pin)
  64.  
  65. /*********************************************************************
  66. *
  67. *       Local functions
  68. *
  69. **********************************************************************
  70. */
  71.  
  72. /*********************************************************************
  73. *
  74. *       _SendCmd
  75. *
  76. * Purpose:
  77. *   Write 8 bit command to peripheral.
  78. */
  79. void _SendCmd(U8 Data) {
  80.   int i;
  81.  
  82. /*  sprintf(s, "CMD: %2x\r\n", Data);
  83.   HAL_UART_Transmit(&huart1, (uint8_t*)s, strlen(s), 20);
  84. */
  85.   for (i = 0; i < 8; i++) {
  86.     //
  87.     // Output next bit
  88.     //
  89.     if (Data & (1 << 7)) {
  90.       DO_SET();
  91.     } else {
  92.       DO_CLR();
  93.     }
  94.     //
  95.     // Output one clock and prepare next bit
  96.     //
  97.     CLK_SET();
  98.     CLK_CLR();
  99.     Data <<= 1;
  100.   }
  101. }
  102.  
  103. /*********************************************************************
  104. *
  105. *       _GetResult
  106. *
  107. * Purpose:
  108. *   Read 16 bit result from peripheral.
  109. *
  110. * Return value:
  111. *   16 bit AD value.
  112. */
  113. U16 _GetResult(void) {
  114.   U16 r;
  115.   int i;
  116.   U8  v;
  117.  
  118.   for (i = 0; i < 16; i++) {
  119.     r <<= 1;
  120.     if (DI_VAL()) {
  121.       r |= 1;
  122.     }
  123.     CLK_SET();
  124.     CLK_CLR();
  125.   }
  126.  
  127.   r >>= 4;  // Remove padding bytes
  128.  
  129. #ifdef DEBUG_TOUCH
  130.   if (r)
  131.     {
  132.       sprintf(s, "Res: %4x\r\n", r);
  133.       HAL_UART_Transmit(&huart1, (uint8_t*)s, strlen(s), 20);
  134.     }
  135. #endif
  136.   return r;
  137. }
  138.  
  139. /*********************************************************************
  140. *
  141. *       _GetBusy
  142. *
  143. * Purpose:
  144. *   Checks the busy line of the peripheral.
  145. *
  146. * Return value:
  147. *   0 = Peripheral ready
  148. *   1 = Peripheral busy
  149. */
  150. char _GetBusy(void) {
  151.   //
  152.   // Output one clock as the busy line will only get low when
  153.   // providing a clock. After one clock the output data is ready
  154.   // thus needing one clock less in the _GetResult() routine. The
  155.   // _GetResult() routine can output more clocks than necessary as
  156.   // they are ignored.
  157.   //
  158.   CLK_SET();
  159.   CLK_CLR();
  160.   //
  161.   // Check busy line just to be sure that the peripheral is ready and data
  162.   // has been already shifted out with the last clock.
  163.   //
  164.   //if (BUSY_VAL()) {
  165.   //  return 1;
  166.   //}
  167.   return 0;
  168. }
  169.  
  170. /*********************************************************************
  171. *
  172. *       _GetPENIRQ
  173. *
  174. * Purpose:
  175. *   Checks the PENIRQ line of the peripheral for a touch event.
  176. *
  177. * Return value:
  178. *   0 = No touch event
  179. *   1 = Touch can be sampled
  180. */
  181. char _GetPENIRQ(void) {
  182.   if (PENIRQ_VAL()) {
  183.    return 0;
  184.   }
  185.   else
  186.   {
  187.       return 1;
  188.   }
  189. }
  190.  
  191. /*********************************************************************
  192. *
  193. *       _SetCS
  194. *
  195. * Purpose:
  196. *   Sets the chip select line of the peripheral.
  197. */
  198. void _SetCS(char OnOff) {
  199.   if (OnOff) {
  200.     CS_SET();
  201.   } else {
  202.     CS_CLR();
  203.   }
  204. }
  205.  
  206. /*********************************************************************
  207. *
  208. *       Public functions
  209. *
  210. **********************************************************************
  211. */
  212. /*********************************************************************
  213. *
  214. *       TOUCH_X_Init
  215. *
  216. * Purpose:
  217. *   Fill in the touch API routines and initalize the touch screen driver.
  218. */
  219. void TOUCH_X_Init(void);
  220. void TOUCH_X_Init(void) {
  221.  
  222.   GUITDRV_ADS7846_CONFIG Config = {0};
  223.  
  224.   Config.pfSendCmd   = _SendCmd;
  225.   Config.pfGetResult = _GetResult;
  226.   Config.pfGetBusy   = _GetBusy;
  227.   Config.pfGetPENIRQ = _GetPENIRQ;
  228.   Config.pfSetCS     = _SetCS;
  229.   Config.xLog0       = 0;
  230.   Config.xLog1       = 240;
  231.   Config.xPhys0      = 200;
  232.   Config.xPhys1      = 3800;
  233.   Config.yLog0       = 0;
  234.   Config.yLog1       = 320;
  235.   Config.yPhys0      = 320;
  236.   Config.yPhys1      = 3700;
  237.   Config.Orientation = GUI_SWAP_XY;
  238.   GUITDRV_ADS7846_Config(&Config);
  239. }
  240.  
  241. /*************************** End of file ****************************/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement