1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
26
27
30
31
34
35
38
39
42
43
46
47
50
53
54
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
94
95
98
99
102
103
106
107
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
165
166
167
168
169
170
171
172
173
174
175
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
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
429
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
493
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
577
578
579
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
612
613
614
615
616
617
618
619
620
621
625
626
631
632
633
634
635
636
637
638
639
640
641
642
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
707
712
713
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
780
781
782
783
784
785
786
787
788
789
790
791
795
796
797
798
799
800
804
805
806
807
808
809
810
811
812
813
818
819
820
821
822
823
824
825
832
839
840
841
842
843
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
873
874
875
876
877
878
879
880
881
882
883
884
889
890
891
892
893
894
895
896
897
898
903
908
909
914
919
920
926
927
928
929
930
931
938
939
940
941
942
945
946
949
950
953
954
/* ... */
#include "stmpe811.h"
/* ... */
/* ... */
/* ... */
/* ... */
/* ... */
#define STMPE811_MAX_INSTANCE 2
/* ... */
/* ... */
/* ... */
/* ... */
TS_DrvTypeDef stmpe811_ts_drv =
{
stmpe811_Init,
stmpe811_ReadID,
stmpe811_Reset,
stmpe811_TS_Start,
stmpe811_TS_DetectTouch,
stmpe811_TS_GetXY,
stmpe811_TS_EnableIT,
stmpe811_TS_ClearIT,
stmpe811_TS_ITStatus,
stmpe811_TS_DisableIT,
...};
IO_DrvTypeDef stmpe811_io_drv =
{
stmpe811_Init,
stmpe811_ReadID,
stmpe811_Reset,
stmpe811_IO_Start,
stmpe811_IO_Config,
stmpe811_IO_WritePin,
stmpe811_IO_ReadPin,
stmpe811_IO_EnableIT,
stmpe811_IO_DisableIT,
stmpe811_IO_ITStatus,
stmpe811_IO_ClearIT,
...};
uint8_t stmpe811[STMPE811_MAX_INSTANCE] = {0};
/* ... */
/* ... */
static uint8_t stmpe811_GetInstance(uint16_t DeviceAddr);
/* ... */
/* ... */
/* ... */
void stmpe811_Init(uint16_t DeviceAddr)
{
uint8_t instance;
uint8_t empty;
instance = stmpe811_GetInstance(DeviceAddr);
if(instance == 0xFF)
{
empty = stmpe811_GetInstance(0);
if(empty < STMPE811_MAX_INSTANCE)
{
stmpe811[empty] = DeviceAddr;
IOE_Init();
stmpe811_Reset(DeviceAddr);
}if (empty < STMPE811_MAX_INSTANCE) { ... }
}if (instance == 0xFF) { ... }
}{ ... }
/* ... */
void stmpe811_Reset(uint16_t DeviceAddr)
{
IOE_Write(DeviceAddr, STMPE811_REG_SYS_CTRL1, 2);
IOE_Delay(10);
IOE_Write(DeviceAddr, STMPE811_REG_SYS_CTRL1, 0);
IOE_Delay(2);
}{ ... }
/* ... */
uint16_t stmpe811_ReadID(uint16_t DeviceAddr)
{
IOE_Init();
return ((IOE_Read(DeviceAddr, STMPE811_REG_CHP_ID_LSB) << 8) |\
(IOE_Read(DeviceAddr, STMPE811_REG_CHP_ID_MSB)));
}{ ... }
/* ... */
void stmpe811_EnableGlobalIT(uint16_t DeviceAddr)
{
uint8_t tmp = 0;
tmp = IOE_Read(DeviceAddr, STMPE811_REG_INT_CTRL);
tmp |= (uint8_t)STMPE811_GIT_EN;
IOE_Write(DeviceAddr, STMPE811_REG_INT_CTRL, tmp);
}{ ... }
/* ... */
void stmpe811_DisableGlobalIT(uint16_t DeviceAddr)
{
uint8_t tmp = 0;
tmp = IOE_Read(DeviceAddr, STMPE811_REG_INT_CTRL);
tmp &= ~(uint8_t)STMPE811_GIT_EN;
IOE_Write(DeviceAddr, STMPE811_REG_INT_CTRL, tmp);
}{ ... }
/* ... */
void stmpe811_EnableITSource(uint16_t DeviceAddr, uint8_t Source)
{
uint8_t tmp = 0;
tmp = IOE_Read(DeviceAddr, STMPE811_REG_INT_EN);
tmp |= Source;
IOE_Write(DeviceAddr, STMPE811_REG_INT_EN, tmp);
}{ ... }
/* ... */
void stmpe811_DisableITSource(uint16_t DeviceAddr, uint8_t Source)
{
uint8_t tmp = 0;
tmp = IOE_Read(DeviceAddr, STMPE811_REG_INT_EN);
tmp &= ~Source;
IOE_Write(DeviceAddr, STMPE811_REG_INT_EN, tmp);
}{ ... }
/* ... */
void stmpe811_SetITPolarity(uint16_t DeviceAddr, uint8_t Polarity)
{
uint8_t tmp = 0;
tmp = IOE_Read(DeviceAddr, STMPE811_REG_INT_CTRL);
tmp &= ~(uint8_t)0x04;
tmp |= Polarity;
IOE_Write(DeviceAddr, STMPE811_REG_INT_CTRL, tmp);
}{ ... }
/* ... */
void stmpe811_SetITType(uint16_t DeviceAddr, uint8_t Type)
{
uint8_t tmp = 0;
tmp = IOE_Read(DeviceAddr, STMPE811_REG_INT_CTRL);
tmp &= ~(uint8_t)0x02;
tmp |= Type;
IOE_Write(DeviceAddr, STMPE811_REG_INT_CTRL, tmp);
}{ ... }
/* ... */
uint8_t stmpe811_GlobalITStatus(uint16_t DeviceAddr, uint8_t Source)
{
return((IOE_Read(DeviceAddr, STMPE811_REG_INT_STA) & Source) == Source);
}{ ... }
/* ... */
uint8_t stmpe811_ReadGITStatus(uint16_t DeviceAddr, uint8_t Source)
{
return((IOE_Read(DeviceAddr, STMPE811_REG_INT_STA) & Source));
}{ ... }
/* ... */
void stmpe811_ClearGlobalIT(uint16_t DeviceAddr, uint8_t Source)
{
IOE_Write(DeviceAddr, STMPE811_REG_INT_STA, Source);
}{ ... }
/* ... */
void stmpe811_IO_Start(uint16_t DeviceAddr, uint32_t IO_Pin)
{
uint8_t mode;
mode = IOE_Read(DeviceAddr, STMPE811_REG_SYS_CTRL2);
mode &= ~(STMPE811_IO_FCT | STMPE811_ADC_FCT);
IOE_Write(DeviceAddr, STMPE811_REG_SYS_CTRL2, mode);
stmpe811_IO_DisableAF(DeviceAddr, (uint8_t)IO_Pin);
}{ ... }
/* ... */
uint8_t stmpe811_IO_Config(uint16_t DeviceAddr, uint32_t IO_Pin, IO_ModeTypedef IO_Mode)
{
uint8_t error_code = 0;
switch(IO_Mode)
{
case IO_MODE_INPUT:
stmpe811_IO_InitPin(DeviceAddr, IO_Pin, STMPE811_DIRECTION_IN);
break;
case IO_MODE_INPUT:
case IO_MODE_OUTPUT:
stmpe811_IO_InitPin(DeviceAddr, IO_Pin, STMPE811_DIRECTION_OUT);
break;
case IO_MODE_OUTPUT:
case IO_MODE_IT_RISING_EDGE:
stmpe811_IO_EnableIT(DeviceAddr);
stmpe811_IO_EnablePinIT(DeviceAddr, IO_Pin);
stmpe811_IO_InitPin(DeviceAddr, IO_Pin, STMPE811_DIRECTION_IN);
stmpe811_SetITType(DeviceAddr, STMPE811_TYPE_EDGE);
stmpe811_IO_SetEdgeMode(DeviceAddr, IO_Pin, STMPE811_EDGE_RISING);
break;
case IO_MODE_IT_RISING_EDGE:
case IO_MODE_IT_FALLING_EDGE:
stmpe811_IO_EnableIT(DeviceAddr);
stmpe811_IO_EnablePinIT(DeviceAddr, IO_Pin);
stmpe811_IO_InitPin(DeviceAddr, IO_Pin, STMPE811_DIRECTION_IN);
stmpe811_SetITType(DeviceAddr, STMPE811_TYPE_EDGE);
stmpe811_IO_SetEdgeMode(DeviceAddr, IO_Pin, STMPE811_EDGE_FALLING);
break;
case IO_MODE_IT_FALLING_EDGE:
case IO_MODE_IT_LOW_LEVEL:
stmpe811_IO_EnableIT(DeviceAddr);
stmpe811_IO_EnablePinIT(DeviceAddr, IO_Pin);
stmpe811_IO_InitPin(DeviceAddr, IO_Pin, STMPE811_DIRECTION_IN);
stmpe811_SetITType(DeviceAddr, STMPE811_TYPE_LEVEL);
stmpe811_SetITPolarity(DeviceAddr, STMPE811_POLARITY_LOW);
break;
case IO_MODE_IT_LOW_LEVEL:
case IO_MODE_IT_HIGH_LEVEL:
stmpe811_IO_EnableIT(DeviceAddr);
stmpe811_IO_EnablePinIT(DeviceAddr, IO_Pin);
stmpe811_IO_InitPin(DeviceAddr, IO_Pin, STMPE811_DIRECTION_IN);
stmpe811_SetITType(DeviceAddr, STMPE811_TYPE_LEVEL);
stmpe811_SetITPolarity(DeviceAddr, STMPE811_POLARITY_HIGH);
break;
case IO_MODE_IT_HIGH_LEVEL:
default:
error_code = (uint8_t) IO_Mode;
break;default
}switch (IO_Mode) { ... }
return error_code;
}{ ... }
/* ... */
void stmpe811_IO_InitPin(uint16_t DeviceAddr, uint32_t IO_Pin, uint8_t Direction)
{
uint8_t tmp = 0;
tmp = IOE_Read(DeviceAddr, STMPE811_REG_IO_DIR);
if (Direction != STMPE811_DIRECTION_IN)
{
tmp |= (uint8_t)IO_Pin;
}if (Direction != STMPE811_DIRECTION_IN) { ... }
else
{
tmp &= ~(uint8_t)IO_Pin;
}else { ... }
IOE_Write(DeviceAddr, STMPE811_REG_IO_DIR, tmp);
}{ ... }
/* ... */
void stmpe811_IO_DisableAF(uint16_t DeviceAddr, uint32_t IO_Pin)
{
uint8_t tmp = 0;
tmp = IOE_Read(DeviceAddr, STMPE811_REG_IO_AF);
tmp |= (uint8_t)IO_Pin;
IOE_Write(DeviceAddr, STMPE811_REG_IO_AF, tmp);
}{ ... }
/* ... */
void stmpe811_IO_EnableAF(uint16_t DeviceAddr, uint32_t IO_Pin)
{
uint8_t tmp = 0;
tmp = IOE_Read(DeviceAddr, STMPE811_REG_IO_AF);
tmp &= ~(uint8_t)IO_Pin;
IOE_Write(DeviceAddr, STMPE811_REG_IO_AF, tmp);
}{ ... }
/* ... */
void stmpe811_IO_SetEdgeMode(uint16_t DeviceAddr, uint32_t IO_Pin, uint8_t Edge)
{
uint8_t tmp1 = 0, tmp2 = 0;
tmp1 = IOE_Read(DeviceAddr, STMPE811_REG_IO_FE);
tmp2 = IOE_Read(DeviceAddr, STMPE811_REG_IO_RE);
tmp1 &= ~(uint8_t)IO_Pin;
tmp2 &= ~(uint8_t)IO_Pin;
if (Edge & STMPE811_EDGE_FALLING)
{
tmp1 |= (uint8_t)IO_Pin;
}if (Edge & STMPE811_EDGE_FALLING) { ... }
if (Edge & STMPE811_EDGE_RISING)
{
tmp2 |= (uint8_t)IO_Pin;
}if (Edge & STMPE811_EDGE_RISING) { ... }
IOE_Write(DeviceAddr, STMPE811_REG_IO_FE, tmp1);
IOE_Write(DeviceAddr, STMPE811_REG_IO_RE, tmp2);
}{ ... }
/* ... */
void stmpe811_IO_WritePin(uint16_t DeviceAddr, uint32_t IO_Pin, uint8_t PinState)
{
if (PinState != 0)
{
IOE_Write(DeviceAddr, STMPE811_REG_IO_SET_PIN, (uint8_t)IO_Pin);
}if (PinState != 0) { ... }
else
{
IOE_Write(DeviceAddr, STMPE811_REG_IO_CLR_PIN, (uint8_t)IO_Pin);
}else { ... }
}{ ... }
/* ... */
uint32_t stmpe811_IO_ReadPin(uint16_t DeviceAddr, uint32_t IO_Pin)
{
return((uint32_t)(IOE_Read(DeviceAddr, STMPE811_REG_IO_MP_STA) & (uint8_t)IO_Pin));
}{ ... }
/* ... */
void stmpe811_IO_EnableIT(uint16_t DeviceAddr)
{
IOE_ITConfig();
stmpe811_EnableITSource(DeviceAddr, STMPE811_GIT_IO);
stmpe811_EnableGlobalIT(DeviceAddr);
}{ ... }
/* ... */
void stmpe811_IO_DisableIT(uint16_t DeviceAddr)
{
stmpe811_DisableGlobalIT(DeviceAddr);
stmpe811_DisableITSource(DeviceAddr, STMPE811_GIT_IO);
}{ ... }
/* ... */
void stmpe811_IO_EnablePinIT(uint16_t DeviceAddr, uint32_t IO_Pin)
{
uint8_t tmp = 0;
tmp = IOE_Read(DeviceAddr, STMPE811_REG_IO_INT_EN);
tmp |= (uint8_t)IO_Pin;
IOE_Write(DeviceAddr, STMPE811_REG_IO_INT_EN, tmp);
}{ ... }
/* ... */
void stmpe811_IO_DisablePinIT(uint16_t DeviceAddr, uint32_t IO_Pin)
{
uint8_t tmp = 0;
tmp = IOE_Read(DeviceAddr, STMPE811_REG_IO_INT_EN);
tmp &= ~(uint8_t)IO_Pin;
IOE_Write(DeviceAddr, STMPE811_REG_IO_INT_EN, tmp);
}{ ... }
/* ... */
uint32_t stmpe811_IO_ITStatus(uint16_t DeviceAddr, uint32_t IO_Pin)
{
return(IOE_Read(DeviceAddr, STMPE811_REG_IO_INT_STA) & (uint8_t)IO_Pin);
}{ ... }
/* ... */
void stmpe811_IO_ClearIT(uint16_t DeviceAddr, uint32_t IO_Pin)
{
stmpe811_ClearGlobalIT(DeviceAddr, STMPE811_GIT_IO);
IOE_Write(DeviceAddr, STMPE811_REG_IO_INT_STA, (uint8_t)IO_Pin);
IOE_Write(DeviceAddr, STMPE811_REG_IO_ED, (uint8_t)IO_Pin);
IOE_Write(DeviceAddr, STMPE811_REG_IO_RE, (uint8_t)IO_Pin);
IOE_Write(DeviceAddr, STMPE811_REG_IO_FE, (uint8_t)IO_Pin);
}{ ... }
/* ... */
void stmpe811_TS_Start(uint16_t DeviceAddr)
{
uint8_t mode;
mode = IOE_Read(DeviceAddr, STMPE811_REG_SYS_CTRL2);
mode &= ~(STMPE811_IO_FCT);
IOE_Write(DeviceAddr, STMPE811_REG_SYS_CTRL2, mode);
stmpe811_IO_EnableAF(DeviceAddr, STMPE811_TOUCH_IO_ALL);
mode &= ~(STMPE811_TS_FCT | STMPE811_ADC_FCT);
IOE_Write(DeviceAddr, STMPE811_REG_SYS_CTRL2, mode);
IOE_Write(DeviceAddr, STMPE811_REG_ADC_CTRL1, 0x49);
IOE_Delay(2);
IOE_Write(DeviceAddr, STMPE811_REG_ADC_CTRL2, 0x01);
/* ... */
IOE_Write(DeviceAddr, STMPE811_REG_TSC_CFG, 0x9A);
IOE_Write(DeviceAddr, STMPE811_REG_FIFO_TH, 0x01);
IOE_Write(DeviceAddr, STMPE811_REG_FIFO_STA, 0x01);
IOE_Write(DeviceAddr, STMPE811_REG_FIFO_STA, 0x00);
/* ... */
IOE_Write(DeviceAddr, STMPE811_REG_TSC_FRACT_XYZ, 0x01);
IOE_Write(DeviceAddr, STMPE811_REG_TSC_I_DRIVE, 0x01);
/* ... */
IOE_Write(DeviceAddr, STMPE811_REG_TSC_CTRL, 0x01);
IOE_Write(DeviceAddr, STMPE811_REG_INT_STA, 0xFF);
IOE_Delay(2);
}{ ... }
/* ... */
uint8_t stmpe811_TS_DetectTouch(uint16_t DeviceAddr)
{
uint8_t state;
uint8_t ret = 0;
state = ((IOE_Read(DeviceAddr, STMPE811_REG_TSC_CTRL) & (uint8_t)STMPE811_TS_CTRL_STATUS) == (uint8_t)STMPE811_TS_CTRL_STATUS);
if(state > 0)
{
if(IOE_Read(DeviceAddr, STMPE811_REG_FIFO_SIZE) > 0)
{
ret = 1;
}if (IOE_Read(DeviceAddr, STMPE811_REG_FIFO_SIZE) > 0) { ... }
}if (state > 0) { ... }
else
{
IOE_Write(DeviceAddr, STMPE811_REG_FIFO_STA, 0x01);
IOE_Write(DeviceAddr, STMPE811_REG_FIFO_STA, 0x00);
}else { ... }
return ret;
}{ ... }
/* ... */
void stmpe811_TS_GetXY(uint16_t DeviceAddr, uint16_t *X, uint16_t *Y)
{
uint8_t dataXYZ[4];
uint32_t uldataXYZ;
IOE_ReadMultiple(DeviceAddr, STMPE811_REG_TSC_DATA_NON_INC, dataXYZ, sizeof(dataXYZ)) ;
uldataXYZ = (dataXYZ[0] << 24)|(dataXYZ[1] << 16)|(dataXYZ[2] << 8)|(dataXYZ[3] << 0);
*X = (uldataXYZ >> 20) & 0x00000FFF;
*Y = (uldataXYZ >> 8) & 0x00000FFF;
IOE_Write(DeviceAddr, STMPE811_REG_FIFO_STA, 0x01);
IOE_Write(DeviceAddr, STMPE811_REG_FIFO_STA, 0x00);
}{ ... }
/* ... */
void stmpe811_TS_EnableIT(uint16_t DeviceAddr)
{
IOE_ITConfig();
stmpe811_EnableITSource(DeviceAddr, STMPE811_TS_IT);
stmpe811_EnableGlobalIT(DeviceAddr);
}{ ... }
/* ... */
void stmpe811_TS_DisableIT(uint16_t DeviceAddr)
{
stmpe811_DisableGlobalIT(DeviceAddr);
stmpe811_DisableITSource(DeviceAddr, STMPE811_TS_IT);
}{ ... }
/* ... */
uint8_t stmpe811_TS_ITStatus(uint16_t DeviceAddr)
{
return(stmpe811_ReadGITStatus(DeviceAddr, STMPE811_TS_IT));
}{ ... }
/* ... */
void stmpe811_TS_ClearIT(uint16_t DeviceAddr)
{
stmpe811_ClearGlobalIT(DeviceAddr, STMPE811_TS_IT);
}{ ... }
/* ... */
static uint8_t stmpe811_GetInstance(uint16_t DeviceAddr)
{
uint8_t idx = 0;
for(idx = 0; idx < STMPE811_MAX_INSTANCE ; idx ++)
{
if(stmpe811[idx] == DeviceAddr)
{
return idx;
}if (stmpe811[idx] == DeviceAddr) { ... }
}for (idx = 0; idx < STMPE811_MAX_INSTANCE ; idx ++) { ... }
return 0xFF;
}{ ... }
/* ... */
/* ... */
/* ... */
/* ... */