summaryrefslogtreecommitdiff
path: root/bsp/ls1cdev/drivers/touch.c
blob: 249e7682bf6a84636995f177489305736c5c009d (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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
/*
 * File      : touch.c
 * This file is part of RT-Thread RTOS
 * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Change Logs:
 * Date           Author       Notes
 * 2017-12-30    Sundm75       first version
 */
 
#include <rtthread.h>
#include <rtdevice.h>
#include <stdbool.h>
#include <drivers/spi.h>
#include "ls1c.h"
#include "ls1c_gpio.h"  
#include "ls1c_spi.h"
#include "drv_spi.h"
#include "touch.h"

#ifdef RT_USING_RTGUI

#include <rtgui/calibration.h>
#include <rtgui/event.h>
#include <rtgui/kbddef.h>
#include <rtgui/rtgui_server.h>
#include <rtgui/rtgui_system.h>

//����Ļ ����Ҫ  _ILI_HORIZONTAL_DIRECTION_
//����Ļ ��Ҫ  _ILI_HORIZONTAL_DIRECTION_

//#define _ILI_HORIZONTAL_DIRECTION_

#if defined(_ILI_HORIZONTAL_DIRECTION_)
#define X_WIDTH 272
#define Y_WIDTH 480
#else
#define X_WIDTH 480
#define Y_WIDTH 272
#endif
/*
TOUCH INT: 84
*/
#define IS_TOUCH_UP()     gpio_get(TOUCH_INT_PIN)

#define led_gpio 52    // led1ָʾ 

#define DUMMY                 0x00

/*
7  6 - 4  3      2     1-0
s  A2-A0 MODE SER/DFR PD1-PD0
*/
/* bit[1:0] power-down */
#define POWER_MODE0     (0) /* Power-Down Between Conversions. When */
                            /* each conversion is finished, the converter */
                            /* enters a low-power mode. At the start of the */
                            /* next conversion, the device instantly powers up */
                            /* to full power. There is no need for additional */
                            /* delays to ensure full operation, and the very first */
                            /* conversion is valid. The Y? switch is on when in */
                            /* power-down.*/
#define POWER_MODE1     (1) /* Reference is off and ADC is on. */
#define POWER_MODE2     (2) /* Reference is on and ADC is off. */
#define POWER_MODE3     (3) /* Device is always powered. Reference is on and */
                            /* ADC is on. */
/* bit[2] SER/DFR */
#define DIFFERENTIAL    (0<<2)
#define SINGLE_ENDED    (1<<2)
/* bit[3] mode */
#define MODE_12BIT      (0<<3)
#define MODE_8BIT       (1<<3)
/* bit[6:4] differential mode */
#define MEASURE_X       (((1<<2) | (0<<1) | (1<<0))<<4)
#define MEASURE_Y       (((0<<2) | (0<<1) | (1<<0))<<4)
#define MEASURE_Z1      (((0<<2) | (1<<1) | (1<<0))<<4)
#define MEASURE_Z2      (((1<<2) | (0<<1) | (0<<0))<<4)
/* bit[7] start */
#define START           (1<<7)

/* X Y change. */
#define TOUCH_MSR_X     (START | MEASURE_X | MODE_12BIT | DIFFERENTIAL | POWER_MODE0)
#define TOUCH_MSR_Y     (START | MEASURE_Y | MODE_12BIT | DIFFERENTIAL | POWER_MODE0)


/* ���¶���XPT2046 �Ĵ�����λ��*/
#if defined(_ILI_HORIZONTAL_DIRECTION_)
#define MIN_X_DEFAULT   2047
#define MAX_X_DEFAULT   47
#define MIN_Y_DEFAULT   102 
#define MAX_Y_DEFAULT   1939 
#else
#define MIN_X_DEFAULT   47
#define MAX_X_DEFAULT   2047
#define MIN_Y_DEFAULT   1939 
#define MAX_Y_DEFAULT   102
#endif



#define SAMP_CNT 8                              //the adc array size
#define SAMP_CNT_DIV2 4                         //the middle of the adc array
#define SH   10                                 // Valve value


/*�궨�� */
#define TOUCH_SPI_X                 SPI1 
#define TOUCH_INT_PIN               84 
#define TOUCH_CS_PIN                49 
#define TOUCH_SCK_PIN               46
#define TOUCH_MISO_PIN              47
#define TOUCH_MOSI_PIN              48     


/*�����ṹ�彫��Ҫ�õ��Ķ������д��*/
struct rtgui_touch_device 
{
    struct rt_device parent;                    /* ����ע���豸*/

    rt_uint16_t x, y;                           /* ��¼��ȡ����λ��ֵ  */ 

    rt_bool_t calibrating;                      /* ����У׼��־ */ 
    rt_touch_calibration_func_t calibration_func;/* �������� ����ָ�� */       

    rt_uint16_t min_x, max_x;                   /* У׼�� X ������С ���ֵ */ 
    rt_uint16_t min_y, max_y;                   /* У׼�� Y ������С ���ֵ */

    struct rt_spi_device * spi_device;          /* SPI �豸 ����ͨ�� */ 
    struct rt_event event;                       /* �¼�ͬ�������ڡ����жϡ� */ 
};
static struct rtgui_touch_device *touch = RT_NULL;

static rt_err_t  touch_send_then_recv(struct rt_spi_device *device,
                               const void           *send_buf,
                               rt_size_t             send_length,
                               void                 *recv_buf,
                               rt_size_t             recv_length)
{
    rt_err_t result;
    struct rt_spi_message message;
    rt_uint8_t dummy[128] ;

    rt_memset(dummy, DUMMY, sizeof(dummy));

    RT_ASSERT(device != RT_NULL);
    RT_ASSERT(device->bus != RT_NULL);

    result = rt_mutex_take(&(device->bus->lock), RT_WAITING_FOREVER);
    if (result == RT_EOK)
    {
        if (device->bus->owner != device)
        {
            /* not the same owner as current, re-configure SPI bus */
            result = device->bus->ops->configure(device, &device->config);
            if (result == RT_EOK)
            {
                /* set SPI bus owner */
                device->bus->owner = device;
            }
            else
            {
                /* configure SPI bus failed */
                result = -RT_EIO;
                goto __exit;
            }
        }

        /* send data */
        message.send_buf   = send_buf;
        message.recv_buf   = RT_NULL;
        message.length     = send_length;
        message.cs_take    = 1;
        message.cs_release = 0;
        message.next       = RT_NULL;

        result = device->bus->ops->xfer(device, &message);
        if (result == 0)
        {
            result = -RT_EIO;
            goto __exit;
        }

        /* recv data */
        message.send_buf   = dummy;
        message.recv_buf   = recv_buf;
        message.length     = recv_length;
        message.cs_take    = 0;
        message.cs_release = 1;
        message.next       = RT_NULL;

        result = device->bus->ops->xfer(device, &message);
        if (result == 0)
        {
            result = -RT_EIO;
            goto __exit;
        }

        result = RT_EOK;
    }
    else
    {
        return -RT_EIO;
    }

__exit:
    rt_mutex_release(&(device->bus->lock));

    return result;
}


static void rtgui_touch_calculate(void)
{
    if (touch != RT_NULL)
    {
        /* read touch */
        {
            rt_uint8_t i, j, k, min;
	        rt_uint16_t temp;
            rt_uint16_t tmpxy[2][SAMP_CNT];
            rt_uint8_t send_buffer[1];
            rt_uint8_t recv_buffer[2];
            for(i=0; i<SAMP_CNT; i++)
            {
                send_buffer[0] = TOUCH_MSR_X;
		  touch_send_then_recv(touch->spi_device, send_buffer, 1, recv_buffer, 2);
                rt_kprintf("touch x: %d ",(recv_buffer[0]*256|recv_buffer[1])>>4);
#if defined(_ILI_HORIZONTAL_DIRECTION_)
                tmpxy[1][i]  = (recv_buffer[0]<<8)|recv_buffer[1] ;
                tmpxy[1][i] >>= 4;
#else
                tmpxy[0][i]  = (recv_buffer[0]<<8)|recv_buffer[1] ;
                tmpxy[0][i] >>=4;

#endif
                send_buffer[0] = TOUCH_MSR_Y;
                touch_send_then_recv(touch->spi_device, send_buffer, 1, recv_buffer, 2);
		  rt_kprintf("touch y: %d \n",(recv_buffer[0]*256|recv_buffer[1])>>4);

#if defined(_ILI_HORIZONTAL_DIRECTION_)
                tmpxy[0][i]  = (recv_buffer[0]<<8)|recv_buffer[1] ;
                tmpxy[0][i] >>= 4;
#else
                tmpxy[1][i]  = (recv_buffer[0]<<8)|recv_buffer[1] ;
                tmpxy[1][i] >>= 4;
#endif
            }
            /*�ٴδ򿪴����ж�*/
            send_buffer[0] = 1 << 7;
		  touch_send_then_recv(touch->spi_device, send_buffer, 1, recv_buffer, 2);
                touch_send_then_recv(touch->spi_device, send_buffer, 1, recv_buffer, 2);
            /* calculate average */
			{
				rt_uint32_t total_x = 0;
				rt_uint32_t total_y = 0;
				for(k=0; k<2; k++)
				{ 
					// sorting the ADC value
					for(i=0; i<SAMP_CNT-1; i++)
					{
						min=i;
						for (j=i+1; j<SAMP_CNT; j++)
						{
							if (tmpxy[k][min] > tmpxy[k][j]) 
								min=j;
						}
						temp = tmpxy[k][i];
						tmpxy[k][i] = tmpxy[k][min];
						tmpxy[k][min] = temp;
				    }
				    //check value for Valve value
					if((tmpxy[k][SAMP_CNT_DIV2+1]-tmpxy[k][SAMP_CNT_DIV2-2]) > SH)
					{
						return;
					}
				}
				total_x=tmpxy[0][SAMP_CNT_DIV2-2]+tmpxy[0][SAMP_CNT_DIV2-1]+tmpxy[0][SAMP_CNT_DIV2]+tmpxy[0][SAMP_CNT_DIV2+1];
				total_y=tmpxy[1][SAMP_CNT_DIV2-2]+tmpxy[1][SAMP_CNT_DIV2-1]+tmpxy[1][SAMP_CNT_DIV2]+tmpxy[1][SAMP_CNT_DIV2+1];
				//calculate average value
				touch->x=total_x>>2;
				touch->y=total_y>>2;
                rt_kprintf("touch->x:%d touch->y:%d\r\n", touch->x, touch->y);
           } /* calculate average */
        } /* read touch */

        /* if it's not in calibration status  */
        /*����ֵ����*/
        if (touch->calibrating != RT_TRUE)
        {
            if (touch->max_x > touch->min_x)
            {
                touch->x = (touch->x - touch->min_x) * X_WIDTH/(touch->max_x - touch->min_x);
            }
            else
            {
                touch->x = (touch->min_x - touch->x) * X_WIDTH/(touch->min_x - touch->max_x);
            }

            if (touch->max_y > touch->min_y)
            {
                touch->y = (touch->y - touch->min_y) * Y_WIDTH /(touch->max_y - touch->min_y);
            }
            else
            {
                touch->y = (touch->min_y - touch->y) * Y_WIDTH /(touch->min_y - touch->max_y);
            }
        }
    }
}
#include "ls1c_regs.h"
#define TOUCH_INT_EN				__REG32(LS1C_INT4_EN)
rt_inline void touch_int_cmd(rt_bool_t NewState)
{
	if(NewState == RT_TRUE)
		{
			//TOUCH_INT_EN |= (1<<(TOUCH_INT_PIN-64));
			reg_set_one_bit(LS1C_INT4_EN, 1<<(TOUCH_INT_PIN-64));
		}
	else
		{
			//TOUCH_INT_EN &=(~ (1<<(TOUCH_INT_PIN-64)));
			reg_clr_one_bit(LS1C_INT4_EN, 1<<(TOUCH_INT_PIN-64));
		}

}

void ls1c_touch_irqhandler(void) /* TouchScreen */
{
  if(gpio_get(TOUCH_INT_PIN)==0)
  {
    /* ���������º���� */  
	if (gpio_level_low == gpio_get(led_gpio))
		gpio_set(led_gpio, gpio_level_high); 
	else
		gpio_set(led_gpio, gpio_level_low); 
	touch_int_cmd(RT_FALSE);
	rt_event_send(&touch->event, 1);
  }
}

/*�ܽų�ʼ���������жϴ�SPI1 CS0 �豸*/
rt_inline void touch_init(void)
{	 
    unsigned int touch_int_gpio = TOUCH_INT_PIN;     // �������ж�
    int touch_irq = LS1C_GPIO_TO_IRQ(touch_int_gpio);  
  
    // ��ʼ�������ж�  
    gpio_set_irq_type(touch_int_gpio, IRQ_TYPE_EDGE_FALLING);  
    rt_hw_interrupt_install(touch_irq, ls1c_touch_irqhandler, RT_NULL, "touch");  
    rt_hw_interrupt_umask(touch_irq);  
    gpio_init(touch_int_gpio, gpio_mode_input);  
  
    // ��ʼ��led  
    gpio_init(led_gpio, gpio_mode_output); 
    gpio_set(led_gpio, gpio_level_high);   
}


/* RT-Thread Device Interface */
static rt_err_t rtgui_touch_init (rt_device_t dev)
{
    rt_uint8_t send;
	rt_uint8_t recv_buffer[2];
    struct rtgui_touch_device * touch_device = (struct rtgui_touch_device *)dev;
	
    touch_init();
    rt_kprintf("touch_init ...\n");
    send = START | DIFFERENTIAL | POWER_MODE0;
    touch_send_then_recv(touch->spi_device, &send, 1, recv_buffer, 2);

    return RT_EOK;
}

static rt_err_t rtgui_touch_control (rt_device_t dev, int cmd, void *args)
{
    switch (cmd)
    {
    case RT_TOUCH_CALIBRATION:
        touch->calibrating = RT_TRUE;
        touch->calibration_func = (rt_touch_calibration_func_t)args;
        break;

    case RT_TOUCH_NORMAL:
        touch->calibrating = RT_FALSE;
        break;

    case RT_TOUCH_CALIBRATION_DATA:
    {
        struct calibration_data* data;
        data = (struct calibration_data*) args;

        //update
        touch->min_x = data->min_x;
        touch->max_x = data->max_x;
        touch->min_y = data->min_y;
        touch->max_y = data->max_y;
    }
    break;
    }

    return RT_EOK;
}

void _set_mouse_position(rt_uint32_t X, rt_uint32_t Y)
{}
static void touch_thread_entry(void *parameter)
{
    rt_bool_t touch_down = RT_FALSE;
    rt_uint32_t event_value;
    struct rtgui_event_mouse emouse;
    static struct _touch_previous
    {
        rt_uint32_t x;
        rt_uint32_t y;
    } touch_previous;

    RTGUI_EVENT_MOUSE_BUTTON_INIT(&emouse);
    emouse.wid = RT_NULL;

    while(1)
    {
        /* ���յ������ж��¼� */
        if(rt_event_recv(&touch->event,
                         1,
                         RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR,
                         100,
                         &event_value)
                == RT_EOK)
        {
            while(1)
            {
                if (IS_TOUCH_UP())
                {
                    /* ������̧�� */
                    /* touch up */
                    emouse.button = (RTGUI_MOUSE_BUTTON_LEFT |RTGUI_MOUSE_BUTTON_UP);

                    /* use old value */
                    emouse.x = touch->x;
                    emouse.y = touch->y;

                    if(touch_down != RT_TRUE) 
                    {                    
                        touch_int_cmd(RT_TRUE);
                        break;
                    }    

                    if ((touch->calibrating == RT_TRUE) && (touch->calibration_func != RT_NULL))
                    {
                        /* ����У׼���� */
                        /* callback function */
                        touch->calibration_func(emouse.x, emouse.y);
											
                    }
                    else
                    {
                        /* ��ui���ʹ������� */
                        rtgui_server_post_event(&emouse.parent, sizeof(struct rtgui_event_mouse));
                    }
                    rt_kprintf("touch up: (%d, %d)\n", emouse.x, emouse.y);

                    /* clean */
                    touch_previous.x = touch_previous.y = 0;
                    touch_down = RT_FALSE;

                    touch_int_cmd(RT_TRUE);
                    break;
                } /* touch up */
                else /* touch down or move */
                {
                    if(touch_down == RT_FALSE)
                    {
                        rt_thread_delay(RT_TICK_PER_SECOND / 10);
                    }
                    else
                    {
                        rt_thread_delay(5);
                    }

                    if(IS_TOUCH_UP()) continue;

                    /* calculation */
                    rtgui_touch_calculate();
                    
                    /* send mouse event */
                    emouse.parent.type = RTGUI_EVENT_MOUSE_BUTTON;
                    emouse.parent.sender = RT_NULL;

                    emouse.x = touch->x;
                    emouse.y = touch->y;
                    _set_mouse_position(emouse.x, emouse.y);
                    /* ������ */
                    /* init mouse button */
                    emouse.button = (RTGUI_MOUSE_BUTTON_LEFT |RTGUI_MOUSE_BUTTON_DOWN);

                    /* send event to server */
                    if (touch->calibrating != RT_TRUE)
                    {
#define previous_keep      8
                        /* filter. */
                        if((touch_previous.x > touch->x + previous_keep)
                            || (touch_previous.x < touch->x - previous_keep)
                            || (touch_previous.y > touch->y + previous_keep)
                            || (touch_previous.y < touch->y - previous_keep))
                        {
                            touch_previous.x = touch->x;
                            touch_previous.y = touch->y;
                            /* ��ui���ʹ������� */
                            rtgui_server_post_event(&emouse.parent, sizeof(struct rtgui_event_mouse));
                            if(touch_down == RT_FALSE)
                            {
                                touch_down = RT_TRUE;
                                rt_kprintf("touch down: (%d, %d)\n", emouse.x, emouse.y);
                            }
                            else
                            {
                                rt_kprintf("touch motion: (%d, %d)\n", emouse.x, emouse.y);
                            }
                        }
                    }
                    else
                    {
                        touch_down = RT_TRUE;
                    }
                } /* touch down or move */
            } /* read touch */
        } /* event recv */
    } /* thread while(1) */
}


rt_err_t rtgui_touch_hw_init(const char * spi_device_name)
{
    	 rt_uint32_t arg[2]; 
	struct rt_device * spi_device;
    	struct rt_thread * touch_thread;
	rt_err_t err;

	rt_kprintf("spi1 cs0 start...\n");
	spi_device = rt_device_find("spi10");
	if(spi_device ==  RT_NULL)
	{
		rt_kprintf("Did not find spi1, exit thread....\n");
		return;
	}
	err = rt_device_open(spi_device, RT_DEVICE_OFLAG_RDWR);
	if(err != RT_EOK)
	{
		rt_kprintf("Open spi1 failed %08X, exit thread....\n", err);
		return;
	}
      
      /* config spi */
      {
          struct rt_spi_configuration cfg;
          cfg.data_width = 8;
          cfg.mode = RT_SPI_MODE_0; 
       cfg.max_hz  = 200 * 1000; /* 200K */
        rt_spi_configure((struct rt_spi_device *)spi_device, &cfg);
      }

	touch = (struct rtgui_touch_device*)rt_malloc (sizeof(struct rtgui_touch_device));
    if (touch == RT_NULL) return RT_ENOMEM; /* no memory yet */

    /* clear device structure */
    rt_memset(&(touch->parent), 0, sizeof(struct rt_device));

    rt_event_init(&touch->event, "touch", RT_IPC_FLAG_FIFO);

    touch->spi_device = (struct rt_spi_device *)spi_device;
    touch->calibrating = false;

    touch->min_x = MIN_X_DEFAULT;
    touch->max_x = MAX_X_DEFAULT;
    touch->min_y = MIN_Y_DEFAULT; 
    touch->max_y = MAX_Y_DEFAULT;

    /* init device structure */
    touch->parent.type = RT_Device_Class_Miscellaneous;
    touch->parent.init = rtgui_touch_init;
    touch->parent.control = rtgui_touch_control;
    touch->parent.user_data = RT_NULL;

    /* register touch device to RT-Thread */
    rt_device_register(&(touch->parent), "touch", RT_DEVICE_FLAG_RDWR);
	

    touch_thread = rt_thread_create("touch_thread",
                                    touch_thread_entry, RT_NULL,
                                    4096, RTGUI_SVR_THREAD_PRIORITY-1, 1);
    if (touch_thread != RT_NULL) rt_thread_startup(touch_thread);

     rt_device_init((rt_device_t)touch);
   return RT_EOK;
}

#endif