summaryrefslogtreecommitdiff
path: root/source/adc_task.c
blob: 67d5bd040ebfcfce3b2b6a3013962d4fff396cf8 (plain)
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*
 * adc_task.c
 *
 */

#include "com_task.h"
#include "adc_task.h"
#include "fsl_adc16.h"
#include "fsl_port.h"
#include "errno.h"
#include "stdlib.h"

/*
 * Apalis ADC0 -> PTB0 -> ADC0_SE8
 * Apalis ADC1 -> PTB1 -> ADC0_SE9
 * Apalis ADC2 -> PTB2 -> ADC0_SE12
 * Apalis ADC3 -> PTB3 -> ADC0_SE13
 *
 * Touch screen:
 * 	Force:
 * 			PTE6	X+ hi-off
 * 			PTB9	X- lo-off
 * 			PTC5	Y+ hi-off
 * 			PTC13	Y- lo-off
 * 	Sense:
 * 		PTB7 -> ADC1_SE13	X-
 * 		PTB6 -> ADC1_SE12	X+
 * 		PTC9 -> ADC1_SE5b	Y-
 * 		PTC8 -> ADC1_SE4b	Y+
 */
const uint8_t adc0_channels[ADC0_CHANNEL_CNT] = {8, 9, 12, 13};
const uint8_t tsc_channels[TSC0_CHANNEL_CNT] = {13, 12, 5, 4};

static int adc_task_init(void)
{
	adc16_config_t adc_config;
#ifdef BOARD_USES_ADC
	ADC16_GetDefaultConfig(&adc_config);
	adc_config.resolution = kADC16_ResolutionSE16Bit;
	adc_config.longSampleMode = kADC16_LongSampleDisabled;
	adc_config.clockDivider = kADC16_ClockDivider8;
	ADC16_Init(ADC0, &adc_config);
	ADC16_SetHardwareAverage(ADC0, kADC16_HardwareAverageDisabled);
	return 0;
#else
	return -ENODEV;
#endif
}

static int tsc_task_init(void)
{
	adc16_config_t adc_config;
#ifdef BOARD_USES_ADC
	ADC16_GetDefaultConfig(&adc_config);
	adc_config.resolution = kADC16_ResolutionSE12Bit;
	adc_config.longSampleMode = kADC16_LongSampleDisabled;
	adc_config.clockDivider = kADC16_ClockDivider8;
	ADC16_Init(ADC1, &adc_config);
	ADC16_SetChannelMuxMode(ADC1, kADC16_ChannelMuxB);
	ADC16_SetHardwareAverage(ADC1, kADC16_HardwareAverageCount32);
	return 0;
#else
	return -ENODEV;
#endif
}

#ifdef BOARD_USES_ADC
static void ts_force_drive(uint8_t xm, uint8_t xp, uint8_t ym, uint8_t yp)
{
	if (xp == 0)
		GPIO_SetPinsOutput(GPIOE, 1 << 6);
	else
		GPIO_ClearPinsOutput(GPIOE, 1 << 6);

	if (xm > 0)
		GPIO_SetPinsOutput(GPIOB, 1 << 9);
	else
		GPIO_ClearPinsOutput(GPIOB, 1 << 9);

	if (yp == 0)
		GPIO_SetPinsOutput(GPIOC, 1 << 5);
	else
		GPIO_ClearPinsOutput(GPIOC, 1 << 5);

	if (ym > 0)
		GPIO_SetPinsOutput(GPIOC, 1 << 13);
	else
		GPIO_ClearPinsOutput(GPIOC, 1 << 13);
}

static inline uint16_t do_adc_conversion(ADC_Type *base, adc16_channel_config_t *channel)
{
	ADC16_SetChannelConfig(base, 0, channel);
	while(ADC16_GetChannelStatusFlags(base, 0) == 0){vTaskDelay(0);}
	return (uint16_t)ADC16_GetChannelConversionValue(base, 0);
}

void adc_task(void *pvParameters)
{
	adc16_channel_config_t channel;
	int i;
	if (adc_task_init() < 0)
		return;

	channel.enableDifferentialConversion = false;
	channel.enableInterruptOnConversionCompleted = false;

	while(1) {
		for (i = 0; i < ADC0_CHANNEL_CNT;i ++){
			channel.channelNumber = adc0_channels[i];
			gen_regs.adc[i] = do_adc_conversion(ADC0, &channel);
		}
		vTaskDelay(1);
	}

}

enum touch_status {
	PEN_UP,
	PEN_DOWN
};

#define MIN_TOUCH_DET	5

/* 		PTB7 -> ADC1_SE13	X-
 * 		PTB6 -> ADC1_SE12	X+
 * 		PTC9 -> ADC1_SE5b	Y-
 * 		PTC8 -> ADC1_SE4b	Y+
 */
void tsc_task(void *pvParameters)
{
	adc16_channel_config_t channel;
	port_pin_config_t pin_config_pd, pin_config_ana;
	int old_status, status = PEN_UP;
	int irq_stat = 0;
	if (tsc_task_init() < 0)
		return;

	pin_config_pd.mux = kPORT_MuxAsGpio;
	pin_config_pd.openDrainEnable = kPORT_OpenDrainDisable;
	pin_config_pd.pullSelect = kPORT_PullDown;
	pin_config_pd.slewRate = kPORT_FastSlewRate;
	pin_config_pd.passiveFilterEnable = kPORT_PassiveFilterDisable;
	pin_config_pd.driveStrength = kPORT_LowDriveStrength;
	pin_config_pd.lockRegister = kPORT_UnlockRegister;

	pin_config_ana.mux = kPORT_PinDisabledOrAnalog;
	pin_config_ana.openDrainEnable = kPORT_OpenDrainDisable;
	pin_config_ana.pullSelect = kPORT_PullDisable;
	pin_config_ana.slewRate = kPORT_FastSlewRate;
	pin_config_ana.passiveFilterEnable = kPORT_PassiveFilterDisable;
	pin_config_ana.driveStrength = kPORT_LowDriveStrength;
	pin_config_ana.lockRegister = kPORT_UnlockRegister;

	channel.enableDifferentialConversion = false;
	channel.enableInterruptOnConversionCompleted = false;

	while(1) {
		//Touch detect power Y+, enable pulldown on xp and read xp GPIO
		ts_force_drive(0, 0, 0, 1);
		PORT_SetPinConfig(PORTB, 6u, &pin_config_pd);
		vTaskDelay(10);
		old_status = status;
		status = GPIO_ReadPinInput(GPIOB, 6u) ? PEN_DOWN:PEN_UP;
		PORT_SetPinConfig(PORTB, 6u, &pin_config_ana);

		if (status != old_status)
			irq_stat = 0;

		if (status == PEN_DOWN) {
			//probe ym with power across Y plane
			ts_force_drive(0, 0, 1, 1);
			vTaskDelay(10);
			channel.channelNumber = tsc_channels[0];
			gen_regs.tsc_ym = do_adc_conversion(ADC1, &channel);

			//probe yp with power across Y plane
			channel.channelNumber = tsc_channels[1];
			gen_regs.tsc_yp = do_adc_conversion(ADC1, &channel);

			//probe xm with power across X plane
			ts_force_drive(1, 1, 0, 0);
			vTaskDelay(10);
			channel.channelNumber = tsc_channels[2];
			gen_regs.tsc_xm = do_adc_conversion(ADC1, &channel);

			//probe xp with power across X plane
			channel.channelNumber = tsc_channels[3];
			gen_regs.tsc_xp = do_adc_conversion(ADC1, &channel);

			if (irq_stat == 0) {
				generate_irq(APALIS_TK1_K20_TSC_IRQ);
				irq_stat = 1;
			}
		} else {
			gen_regs.tsc_xm = 0;
			gen_regs.tsc_xp = 0;
			gen_regs.tsc_ym = 0;
			gen_regs.tsc_yp = 0;
			vTaskDelay(20);
			if (irq_stat == 0) {
				generate_irq(APALIS_TK1_K20_TSC_IRQ);
				irq_stat = 1;
			}
		}
		vTaskDelay(10);
	}

}

int tsc_registers(uint8_t *rx_buf, uint8_t *tx_buf)
{
	if (rx_buf[0] == APALIS_TK1_K20_READ_INST) {
		switch (rx_buf[1]) {
		case APALIS_TK1_K20_TSCREG:
			tx_buf[0] = 0x00;
			return 1;
		case APALIS_TK1_K20_TSC_XML:
			tx_buf[0] = gen_regs.tsc_xm & 0xFF;
			return 1;
		case APALIS_TK1_K20_TSC_XMH:
			tx_buf[0] = (gen_regs.tsc_xm >> 8) & 0xFF;
			return 1;
		case APALIS_TK1_K20_TSC_XPL:
			tx_buf[0] = gen_regs.tsc_xp & 0xFF;
			return 1;
		case APALIS_TK1_K20_TSC_XPH:
			tx_buf[0] = (gen_regs.tsc_xp >> 8) & 0xFF;
			return 1;
		case APALIS_TK1_K20_TSC_YML:
			tx_buf[0] = gen_regs.tsc_ym & 0xFF;
			return 1;
		case APALIS_TK1_K20_TSC_YMH:
			tx_buf[0] = (gen_regs.tsc_ym >> 8) & 0xFF;
			return 1;
		case APALIS_TK1_K20_TSC_YPL:
			tx_buf[0] = gen_regs.tsc_yp & 0xFF;
			return 1;
		case APALIS_TK1_K20_TSC_YPH:
			tx_buf[0] = (gen_regs.tsc_yp >> 8) & 0xFF;
			return 1;
		default:
			return -ENOENT;
		}
	} else if (rx_buf[0] == APALIS_TK1_K20_WRITE_INST) {
		switch (rx_buf[1]) {
		case APALIS_TK1_K20_TSCREG:
			return -ENOENT;
		default:
			return -ENOENT;
		}
	} else if (rx_buf[0] == APALIS_TK1_K20_BULK_READ_INST) {
		switch (rx_buf[1]){
		case APALIS_TK1_K20_TSC_XML:
			tx_buf[0] = gen_regs.tsc_xm & 0xFF;
			tx_buf[1] = (gen_regs.tsc_xm >> 8) & 0xFF;
			return 2;
		case APALIS_TK1_K20_TSC_XPL:
			tx_buf[0] = gen_regs.tsc_xp & 0xFF;
			tx_buf[1] = (gen_regs.tsc_xp >> 8) & 0xFF;
			return 2;
		case APALIS_TK1_K20_TSC_YML:
			tx_buf[0] = gen_regs.tsc_ym & 0xFF;
			tx_buf[1] = (gen_regs.tsc_ym >> 8) & 0xFF;
			return 2;
		case APALIS_TK1_K20_TSC_YPL:
			tx_buf[0] = gen_regs.tsc_yp & 0xFF;
			tx_buf[1] = (gen_regs.tsc_yp >> 8) & 0xFF;
			return 2;
		default:
			return -ENOENT;
		}
	}
	return -ENOENT;
}

int adc_registers(uint8_t *rx_buf, uint8_t *tx_buf)
{
	if (rx_buf[0] == APALIS_TK1_K20_READ_INST) {
		switch (rx_buf[1]) {
		case APALIS_TK1_K20_ADCREG:
			tx_buf[0] = 0x00;
			return 1;
		case APALIS_TK1_K20_ADC_CH0L:
			tx_buf[0] = gen_regs.adc[0] & 0xFF;
			return 1;
		case APALIS_TK1_K20_ADC_CH0H:
			tx_buf[0] = (gen_regs.adc[0] >> 8) & 0xFF;
			return 1;
		case APALIS_TK1_K20_ADC_CH1L:
			tx_buf[0] = gen_regs.adc[1] & 0xFF;
			return 1;
		case APALIS_TK1_K20_ADC_CH1H:
			tx_buf[0] = (gen_regs.adc[1] >> 8) & 0xFF;
			return 1;
		case APALIS_TK1_K20_ADC_CH2L:
			tx_buf[0] = gen_regs.adc[2] & 0xFF;
			return 1;
		case APALIS_TK1_K20_ADC_CH2H:
			tx_buf[0] = (gen_regs.adc[2] >> 8) & 0xFF;
			return 1;
		case APALIS_TK1_K20_ADC_CH3L:
			tx_buf[0] = gen_regs.adc[3] & 0xFF;
			return 1;
		case APALIS_TK1_K20_ADC_CH3H:
			tx_buf[0] = (gen_regs.adc[3] >> 8) & 0xFF;
			return 1;
		default:
			return -ENOENT;
		}
	} else if (rx_buf[0] == APALIS_TK1_K20_WRITE_INST) {
		switch (rx_buf[1]) {
		case APALIS_TK1_K20_ADCREG:
			return -ENOENT;
		default:
			return -ENOENT;
		}
	} else if (rx_buf[0] == APALIS_TK1_K20_BULK_READ_INST) {
		switch (rx_buf[1]){
		case APALIS_TK1_K20_ADC_CH0L:
			tx_buf[0] = gen_regs.adc[0] & 0xFF;
			tx_buf[1] = (gen_regs.adc[0] >> 8) & 0xFF;
			return 2;
		case APALIS_TK1_K20_ADC_CH1L:
			tx_buf[0] = gen_regs.adc[1] & 0xFF;
			tx_buf[1] = (gen_regs.adc[1] >> 8) & 0xFF;
			return 2;
		case APALIS_TK1_K20_ADC_CH2L:
			tx_buf[0] = gen_regs.adc[2] & 0xFF;
			tx_buf[1] = (gen_regs.adc[2] >> 8) & 0xFF;
			return 2;
		case APALIS_TK1_K20_ADC_CH3L:
			tx_buf[0] = gen_regs.adc[3] & 0xFF;
			tx_buf[1] = (gen_regs.adc[3] >> 8) & 0xFF;
			return 2;

		default:
			return -ENOENT;
		}
	}
	return -ENOENT;
}
#endif