1 /*!
2  * @file        tsc_acq.h
3  *
4  * @brief       This file contains external declarations of the tsc_acq.c file.
5  *
6  * @version     V1.0.1
7  *
8  * @date        2022-09-20
9  *
10  * @attention
11  *
12  *  Copyright (C) 2020-2022 Geehy Semiconductor
13  *
14  *  You may not use this file except in compliance with the
15  *  GEEHY COPYRIGHT NOTICE (GEEHY SOFTWARE PACKAGE LICENSE).
16  *
17  *  The program is only for reference, which is distributed in the hope
18  *  that it will be useful and instructional for customers to develop
19  *  their software. Unless required by applicable law or agreed to in
20  *  writing, the program is distributed on an "AS IS" BASIS, WITHOUT
21  *  ANY WARRANTY OR CONDITIONS OF ANY KIND, either express or implied.
22  *  See the GEEHY SOFTWARE PACKAGE LICENSE for the governing permissions
23  *  and limitations under the License.
24  */
25 
26 /* Define to prevent recursive inclusion */
27 #ifndef __TSC_ACQ_H
28 #define __TSC_ACQ_H
29 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 
34 /* Includes */
35 #include "apm32f0xx.h"
36 #include "tsc_config.h"
37 #include "tsc_types.h"
38 #include "apm32f0xx_gpio.h"
39 #include "apm32f0xx_rcm.h"
40 
41 /** @addtogroup TSC_Driver_Library TSC Driver Library
42   @{
43 */
44 
45 /** @addtogroup TSC_Acquisition_Driver TSC Acquisition Driver
46   @{
47 */
48 
49 #if !defined(APM32F051) && defined(APM32F071) && defined(APM32F072) && defined(APM32F091)
50 #error "Device family not declared in the toolchain compiler preprocessor."
51 #endif
52 
53 /** @defgroup TSC_Acquisition_Macros Macros
54   @{
55   */
56 
57 #ifndef CONST
58 #define CONST const
59 #endif
60 
61 /* SysTick enable/disable interrupt macros */
62 #define enableInterrupts()  {SysTick->CTRL |= SysTick_CTRL_TICKINT_Msk;}
63 #define disableInterrupts() {SysTick->CTRL &= ~SysTick_CTRL_TICKINT_Msk;}
64 
65 #define TSC_NB_GROUPS (8)
66 
67 #define TSC_GROUP1 (0x01)
68 #define TSC_GROUP2 (0x02)
69 #define TSC_GROUP3 (0x04)
70 #define TSC_GROUP4 (0x08)
71 #define TSC_GROUP5 (0x10)
72 #define TSC_GROUP6 (0x20)
73 #define TSC_GROUP7 (0x40)
74 #define TSC_GROUP8 (0x80)
75 
76 /* GxIOy masks */
77 #define TSC_GROUP1_IO1 (0x00000001)
78 #define TSC_GROUP1_IO2 (0x00000002)
79 #define TSC_GROUP1_IO3 (0x00000004)
80 #define TSC_GROUP1_IO4 (0x00000008)
81 #define TSC_GROUP2_IO1 (0x00000010)
82 #define TSC_GROUP2_IO2 (0x00000020)
83 #define TSC_GROUP2_IO3 (0x00000040)
84 #define TSC_GROUP2_IO4 (0x00000080)
85 #define TSC_GROUP3_IO1 (0x00000100)
86 #define TSC_GROUP3_IO2 (0x00000200)
87 #define TSC_GROUP3_IO3 (0x00000400)
88 #define TSC_GROUP3_IO4 (0x00000800)
89 #define TSC_GROUP4_IO1 (0x00001000)
90 #define TSC_GROUP4_IO2 (0x00002000)
91 #define TSC_GROUP4_IO3 (0x00004000)
92 #define TSC_GROUP4_IO4 (0x00008000)
93 #define TSC_GROUP5_IO1 (0x00010000)
94 #define TSC_GROUP5_IO2 (0x00020000)
95 #define TSC_GROUP5_IO3 (0x00040000)
96 #define TSC_GROUP5_IO4 (0x00080000)
97 #define TSC_GROUP6_IO1 (0x00100000)
98 #define TSC_GROUP6_IO2 (0x00200000)
99 #define TSC_GROUP6_IO3 (0x00400000)
100 #define TSC_GROUP6_IO4 (0x00800000)
101 #define TSC_GROUP7_IO1 (0x01000000)
102 #define TSC_GROUP7_IO2 (0x02000000)
103 #define TSC_GROUP7_IO3 (0x04000000)
104 #define TSC_GROUP7_IO4 (0x08000000)
105 #define TSC_GROUP8_IO1 (0x10000000)
106 #define TSC_GROUP8_IO2 (0x20000000)
107 #define TSC_GROUP8_IO3 (0x40000000)
108 #define TSC_GROUP8_IO4 (0x80000000)
109 
110 /* Exported types, For all devices/acquisitions */
111 typedef uint16_t  TSC_tMeas_T;         /*!< Measurement */
112 typedef uint16_t  TSC_tRefer_T;        /*!< Reference */
113 typedef int16_t   TSC_tDelta_T;        /*!< Delta */
114 
115 typedef uint8_t   TSC_tIndexSrc_T;     /*!< Channel source index */
116 typedef uint16_t  TSC_tIndexDest_T;    /*!< Channel destination index */
117 
118 typedef uint8_t   TSC_tRefRest_T;      /*!< Reference Rest (ECS) */
119 typedef uint16_t  TSC_tKCoeff_T;       /*!< K coefficient (ECS) */
120 
121 typedef uint8_t   TSC_tIndex_T;        /*!< Generic index */
122 typedef uint16_t  TSC_tNum_T;          /*!< Generic number */
123 typedef uint8_t   TSC_tCounter_T;      /*!< Generic counter used for debounce */
124 
125 typedef uint8_t   TSC_tThreshold_T;    /*!< Delta threshold */
126 
127 typedef int16_t   TSC_tPosition_T;     /*!< Linear and Rotary sensors position */
128 typedef uint8_t   TSC_tsignPosition_T; /*!< Linear and Rotary sensors position */
129 
130 typedef uint16_t  TSC_tTick_ms_T;      /*!< Time in ms */
131 typedef uint8_t   TSC_tTick_sec_T;     /*!< Time in sec */
132 
133 /**@} end of group TSC_Acquisition_Macros */
134 
135 /** @defgroup TSC_Acquisition_Enumerations Enumerations
136   @{
137   */
138 
139 /**@} end of group TSC_Acquisition_Enumerations */
140 
141 /** @defgroup TSC_Acquisition_Structures Structures
142   @{
143   */
144 
145 /* Pointer to the Measure filter function */
146 typedef TSC_tMeas_T(* TSC_pMeasFilter_T)(TSC_tMeas_T, TSC_tMeas_T);
147 /* Pointer to the Delta filter function */
148 typedef TSC_tDelta_T(* TSC_pDeltaFilter_T)(TSC_tDelta_T);
149 
150 /**
151  * @brief   Channel destination index
152  */
153 typedef struct
154 {
155     TSC_tIndexDest_T  IdxDest; /*!< Index in the Channel data array */
156 } TSC_Channel_Dest_T;
157 
158 /**
159  * @brief   Channel Source and Configuration
160  */
161 typedef struct
162 {
163     TSC_tIndexSrc_T  IdxSrc; /*!< Index of TSC->IOGxCNT[] registers */
164     /* For apm32f0xx only */
165     uint32_t         msk_IOCHCTRL_channel; /*!< Mask of the Channel IO (electrodes only) */
166     uint32_t         msk_IOGCSTS_group;    /*!< Mask of the Group used (electrodes only) */
167 } TSC_Channel_Src_T;
168 
169 /**
170  * @brief   Channel flags
171  */
172 typedef struct
173 {
174     unsigned int DataReady : 1;   /*!< To identify a new measurement (TSC_DATA_T) */
175     unsigned int AcqStatus : 2;   /*!< Acquisition status (TSC_ACQ_STATUS_T) */
176     unsigned int ObjStatus : 2;   /*!< Object status (TSC_OBJ_STATUS_T) */
177 } TSC_Channel_Flag_T;
178 
179 /**
180  * @brief    Channel Data
181  */
182 typedef struct
183 {
184     TSC_Channel_Flag_T   Flag;    /*!< Flag */
185     TSC_tRefer_T         Refer;   /*!< Reference */
186     TSC_tRefRest_T       RefRest; /*!< Reference rest for ECS */
187     TSC_tDelta_T         Delta;   /*!< Delta */
188 #if TOUCH_USE_MEAS > 0
189     TSC_tMeas_T          Meas;    /*!< Hold the last acquisition measure */
190 #endif
191 } TSC_Channel_Data_T;
192 
193 /**
194  * @brief   Block Configuration
195  */
196 typedef struct
197 {
198     /* Common to all acquisitions */
199     CONST TSC_Channel_Src_T*  p_chSrc;     /*!< Pointer to the Channel Source and Configuration */
200     CONST TSC_Channel_Dest_T* p_chDest;    /*!< Pointer to the Channel Destination */
201     TSC_Channel_Data_T*       p_chData;    /*!< Pointer to the Channel Data */
202     TSC_tNum_T                NumChannel;  /*!< Number of channels in the block */
203     /* For apm32f0xx only */
204     uint32_t      msk_IOCHCTRL_channels;   /*!< Mask of all channel IOs (electrodes and shields) */
205     uint32_t      msk_IOGCSTS_groups;      /*!< Mask of all groups used (electrodes only) */
206 } TSC_Block_T;
207 
208 /**
209  * @brief  Structure containing all data of a Zone.
210  *         A Zone is a set of Blocks.
211  *         Variables of this structure type can be placed in RAM or ROM.
212  */
213 typedef struct
214 {
215     /* Common to all acquisitions */
216     TSC_tIndex_T*         indexBlock;   /*!< Pointer to an array of block indexes */
217     TSC_pDeltaFilter_T*   daltaFilter;  /*!< Pointer to a Delta filter function */
218     TSC_tNum_T            numBlock;     /*!< Number of blocks in the zone */
219 } TSC_Zone_T;
220 
221 /**@} end of group TSC_Acquisition_Structures */
222 
223 /** @defgroup TSC_Acquisition_Functions Functions
224   @{
225   */
226 
227 TSC_STATUS_T TSC_Acq_Config(void);
228 void TSC_Acq_ConfigGPIO(void);
229 TSC_STATUS_T TSC_Acq_ConfigBlock(TSC_tIndex_T idxBlock);
230 TSC_BOOL_T TSC_Acq_UseFilter(TSC_Channel_Data_T* pCh);
231 TSC_BOOL_T TSC_Acq_TestReferenceRange(TSC_Channel_Data_T* pCh);
232 TSC_BOOL_T TSC_Acq_TestFirstReference(TSC_Channel_Data_T* pCh, TSC_tMeas_T newMeas);
233 void TSC_Acq_StartPerConfigBlock(void);
234 TSC_STATUS_T TSC_Acq_WaitBlockEOA(void);
235 TSC_ACQ_STATUS_T TSC_Acq_CheckNoise(void);
236 TSC_tMeas_T TSC_Acq_ReadMeasurVal(TSC_tIndexSrc_T index);
237 TSC_tDelta_T TSC_Acq_ComputeDelta(TSC_tRefer_T refVal, TSC_tMeas_T measVal);
238 TSC_tMeas_T TSC_Acq_ComputeMeas(TSC_tRefer_T refVal, TSC_tDelta_T deltaVal);
239 
240 TSC_STATUS_T TSC_Acq_ConfigZone(CONST TSC_Zone_T* zone, TSC_tIndex_T idxBlock);
241 TSC_STATUS_T TSC_Acq_ReadBlockResult(TSC_tIndex_T idxBlock, TSC_pMeasFilter_T mfilter, TSC_pDeltaFilter_T dfilter);
242 TSC_STATUS_T TSC_Acq_CalibrateBlock(TSC_tIndex_T block);
243 void TSC_acq_ClearBlockData(TSC_tIndex_T block);
244 
245 #ifdef __cplusplus
246 }
247 #endif
248 
249 #endif /* __TSC_ACQ_H */
250 
251 /**@} end of group TSC_Acquisition_Functions */
252 /**@} end of group TSC_Acquisition_Driver */
253 /**@} end of group TSC_Driver_Library */
254