/
SSD1963_touch.c
170 lines (137 loc) · 3.88 KB
/
SSD1963_touch.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include "stm32f4xx.h"
#include "SSD1963_touch.h"
OS_SemaphoreHandle SPI_Mutex;
/**
* @brief Displays a Cross.
* @param Xpos: specifies the left X position.
* @param Ypos: specifies the botton Y position.
* @retval None
*/
void LCD_Cross(uint16_t Ypos, uint16_t Xpos)
{
LCD_Line(Xpos - 30, Ypos, Xpos + 30, Ypos, BLACK); /* Horizontal Line */
LCD_Line(Xpos, Ypos - 30, Xpos, Ypos + 30, BLACK); /* Vertical Line */
}
void LCD_Touch_Config(void)
{
uint8_t i;
GPIO_InitTypeDef GPIO_InitStructure;
/* Peripheral Clock Enable -------------------------------------------------*/
SPI_Mutex = OS_SemaphoreCreateMutex();
/* Enable GPIO clocks */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOE | RCC_AHB1Periph_GPIOG, ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; // SCK
GPIO_Init(GPIOE, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; // MOSI
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; // CS
GPIO_Init(GPIOE, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; // CS
GPIO_Init(GPIOE, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; // MISO
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; // IRQ1
GPIO_Init(GPIOG, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; // IRQ2
GPIO_Init(GPIOE, &GPIO_InitStructure);
TOUCH_CS_SET(LCD1_TOUCH_CS_PIN);
TOUCH_CS_SET(LCD2_TOUCH_CS_PIN);
}
void Touch_Write(uint8_t out, uint16_t CS_Pin)
{
uint8_t i;
TOUCH_CS_SET(CS_Pin);
TOUCH_MOSI_RESET;
TOUCH_SCK_RESET;
TOUCH_CS_RESET(CS_Pin);
for (i = 0; i < 8; i++) {
if ((out >> (7-i)) & 0x1) {
TOUCH_MOSI_SET;
} else {
TOUCH_MOSI_RESET;
}
LCD_Delay(5);
TOUCH_SCK_SET;
LCD_Delay(5);
TOUCH_SCK_RESET;
}
}
uint16_t Touch_Read(uint16_t CS_Pin)
{
uint8_t i;
uint16_t temp = 0;
TOUCH_MOSI_RESET;
TOUCH_SCK_SET;
for (i = 0; i < 16; i++) {
LCD_Delay(5);
TOUCH_SCK_RESET;
LCD_Delay(5);
if (TOUCH_MISO == Bit_SET && i < 12) {
temp |= (1 << (11-i));
}
LCD_Delay(5);
TOUCH_SCK_SET;
}
TOUCH_CS_SET(CS_Pin);
return temp;
}
uint16_t Touch_ReadX(uint8_t LCD)
{
uint16_t CS_Pin;
Touch_Lock();
if (LCD == 1) CS_Pin = LCD1_TOUCH_CS_PIN;
else if (LCD == 2) CS_Pin = LCD2_TOUCH_CS_PIN;
Touch_Write(CHX, CS_Pin);
LCD_Delay(50);
Touch_Unlock();
return Touch_Read(CS_Pin);
}
uint16_t Touch_ReadY(uint8_t LCD)
{
uint16_t CS_Pin;
Touch_Lock();
if (LCD == 1) CS_Pin = LCD1_TOUCH_CS_PIN;
else if (LCD == 2) CS_Pin = LCD2_TOUCH_CS_PIN;
Touch_Write(CHY, CS_Pin);
LCD_Delay(50);
Touch_Unlock();
return Touch_Read(CS_Pin);
}
uint16_t Touch_ReadAvgX(uint8_t measurements, uint8_t LCD)
{
uint8_t i;
uint32_t avg = 0;
for (i = 0; i < measurements; i++) {
avg += Touch_ReadX(LCD);
LCD_Delay(100);
}
avg /= measurements;
return avg;
}
uint16_t Touch_ReadAvgY(uint8_t measurements, uint8_t LCD)
{
uint8_t i;
uint32_t avg = 0;
for (i = 0; i < measurements; i++) {
avg += Touch_ReadY(LCD);
LCD_Delay(100);
}
avg /= measurements;
return avg;
}