cy_capsense_processing.c 86 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226
  1. /***************************************************************************//**
  2. * \file cy_capsense_processing.c
  3. * \version 3.0
  4. *
  5. * \brief
  6. * This file provides the source code for the Data Processing module functions.
  7. * The Data Processing module is responsible for the low-level raw count
  8. * processing provided by the sensing module, maintaining baseline and
  9. * difference values and performing high-level widget processing like
  10. * updating button status or calculating slider position.
  11. *
  12. ********************************************************************************
  13. * \copyright
  14. * Copyright 2018-2021, Cypress Semiconductor Corporation (an Infineon company)
  15. * or an affiliate of Cypress Semiconductor Corporation. All rights reserved.
  16. * You may use this file only in accordance with the license, terms, conditions,
  17. * disclaimers, and limitations in the end user license agreement accompanying
  18. * the software package with which this file was provided.
  19. *******************************************************************************/
  20. #include "cy_syslib.h"
  21. #include <stddef.h>
  22. #include <string.h>
  23. #include "cycfg_capsense_defines.h"
  24. #include "cy_capsense_common.h"
  25. #include "cy_capsense_processing.h"
  26. #include "cy_capsense_filter.h"
  27. #include "cy_capsense_lib.h"
  28. #include "cy_capsense_centroid.h"
  29. #include "cy_capsense_sensing_v3.h"
  30. #if (defined(CY_IP_MXCSDV2) || defined(CY_IP_M0S8CSDV2) || defined(CY_IP_M0S8MSCV3))
  31. /*******************************************************************************
  32. * Local definition
  33. *******************************************************************************/
  34. /* Raw data normalization and scaling */
  35. #define CY_CAPSENSE_SCALING_SHIFT (15)
  36. #define CY_CAPSENSE_MAX_TX_PATTERN_NUM (32)
  37. /* CIC2 Filter Divider */
  38. #define CY_CAPSENSE_CIC2_DIVIDER_1 (1u)
  39. #define CY_CAPSENSE_CIC2_DIVIDER_2 (2u)
  40. #define CY_CAPSENSE_CIC2_DIVIDER_4 (4u)
  41. #define CY_CAPSENSE_CIC2_DIVIDER_8 (8u)
  42. #define CY_CAPSENSE_CIC2_DIVIDER_16 (16u)
  43. /** \} \endcond */
  44. /*******************************************************************************
  45. * Function Name: Cy_CapSense_InitializeAllStatuses
  46. ****************************************************************************//**
  47. *
  48. * Performs initialization of all statuses and related modules including
  49. * debounce counters and touch positions of all the widgets.
  50. *
  51. * The initialization includes the following tasks:
  52. * * Reset the debounce counters of all the widgets.
  53. * * Reset the number of touches.
  54. * * Reset the position filter history for slider and touchpad widgets.
  55. * * Clear all status of widgets and sensors.
  56. * * Enable all the widgets.
  57. *
  58. * Calling this function is accompanied by
  59. * * Cy_CapSense_InitializeAllBaselines().
  60. * * Cy_CapSense_InitializeAllFilters().
  61. *
  62. * \param context
  63. * The pointer to the CAPSENSE&trade; context structure \ref cy_stc_capsense_context_t.
  64. *
  65. *******************************************************************************/
  66. void Cy_CapSense_InitializeAllStatuses(const cy_stc_capsense_context_t * context)
  67. {
  68. uint32_t widgetId;
  69. for(widgetId = context->ptrCommonConfig->numWd; widgetId-- > 0u;)
  70. {
  71. Cy_CapSense_InitializeWidgetStatus(widgetId, context);
  72. #if (CY_CAPSENSE_DISABLE != CY_CAPSENSE_GESTURE_EN)
  73. Cy_CapSense_InitializeWidgetGestures(widgetId, context);
  74. #endif
  75. }
  76. }
  77. /*******************************************************************************
  78. * Function Name: Cy_CapSense_InitializeWidgetStatus
  79. ****************************************************************************//**
  80. *
  81. * Performs initialization of all statuses, debounce counters, and touch positions
  82. * of the specified widget.
  83. *
  84. * The initialization includes:
  85. * * Resets the debounce counter of the widget.
  86. * * Resets the number of touches.
  87. * * Resets the position filter history for slider and touchpad widgets.
  88. * * Clears widget and sensor statuses.
  89. * * Enables the widget.
  90. *
  91. * The Button and Matrix Button widgets have individual debounce counters per
  92. * sensor for the CSD widgets and per node for the CSX widgets.
  93. *
  94. * The Slider and Touchpad widgets have a single debounce counter per widget.
  95. *
  96. * The Proximity widget has two debounce counters per sensor. One is for the
  97. * proximity event and the second is for the touch event.
  98. *
  99. * All debounce counters during initialization are set to the value of the
  100. * onDebounce widget parameter.
  101. *
  102. * Calling this function is accompanied by
  103. * * Cy_CapSense_InitializeWidgetBaseline().
  104. * * Cy_CapSense_InitializeWidgetFilter().
  105. *
  106. * \param widgetId
  107. * Specifies the ID number of the widget. A macro for the widget ID can be found
  108. * in the cycfg_capsense.h file defined as CY_CAPSENSE_<WIDGET_NAME>_WDGT_ID.
  109. *
  110. * \param context
  111. * The pointer to the CAPSENSE&trade; context structure \ref cy_stc_capsense_context_t.
  112. *
  113. *******************************************************************************/
  114. void Cy_CapSense_InitializeWidgetStatus(
  115. uint32_t widgetId,
  116. const cy_stc_capsense_context_t * context)
  117. {
  118. uint32_t snsIndex;
  119. const cy_stc_capsense_widget_config_t * ptrWdCfg = &context->ptrWdConfig[widgetId];
  120. cy_stc_capsense_widget_context_t * ptrWdCxt = ptrWdCfg->ptrWdContext;
  121. cy_stc_capsense_sensor_context_t * ptrSnsCxt = ptrWdCfg->ptrSnsContext;
  122. uint32_t snsNumber = ptrWdCfg->numSns;
  123. #if(CY_CAPSENSE_DISABLE != CY_CAPSENSE_TOUCHPAD_EN)
  124. uint32_t filterSize;
  125. cy_stc_capsense_position_t * ptrHistory;
  126. #endif
  127. /* Clear widget statuses (Non active, Not disabled, Working) */
  128. ptrWdCxt->status &= (uint8_t)~(CY_CAPSENSE_WD_ACTIVE_MASK |
  129. CY_CAPSENSE_WD_DISABLE_MASK |
  130. CY_CAPSENSE_WD_WORKING_MASK);
  131. /* Clear sensor status */
  132. for (snsIndex = snsNumber; snsIndex-- >0u;)
  133. {
  134. ptrSnsCxt->status &= (uint8_t)~(CY_CAPSENSE_SNS_TOUCH_STATUS_MASK | CY_CAPSENSE_SNS_PROX_STATUS_MASK);
  135. ptrSnsCxt++;
  136. }
  137. /* Reset debounce counters */
  138. switch (ptrWdCfg->wdType)
  139. {
  140. #if((CY_CAPSENSE_DISABLE != CY_CAPSENSE_BUTTON_EN) ||\
  141. (CY_CAPSENSE_DISABLE != CY_CAPSENSE_MATRIX_EN))
  142. case (uint8_t)CY_CAPSENSE_WD_MATRIX_BUTTON_E:
  143. case (uint8_t)CY_CAPSENSE_WD_BUTTON_E:
  144. /* Each button requires one debounce counter */
  145. (void)memset(ptrWdCfg->ptrDebounceArr, (int32_t)ptrWdCxt->onDebounce, (size_t)snsNumber);
  146. break;
  147. #endif
  148. #if((CY_CAPSENSE_DISABLE != CY_CAPSENSE_SLIDER_EN) ||\
  149. (CY_CAPSENSE_DISABLE != CY_CAPSENSE_TOUCHPAD_EN))
  150. case (uint8_t)CY_CAPSENSE_WD_LINEAR_SLIDER_E:
  151. case (uint8_t)CY_CAPSENSE_WD_RADIAL_SLIDER_E:
  152. case (uint8_t)CY_CAPSENSE_WD_TOUCHPAD_E:
  153. /* Each widget requires one debounce counter */
  154. if (ptrWdCfg->senseMethod == CY_CAPSENSE_CSD_GROUP)
  155. {
  156. *(ptrWdCfg->ptrDebounceArr) = ptrWdCxt->onDebounce;
  157. }
  158. else
  159. {
  160. /*
  161. * CSX Touchpad has debounce located in another place. Moreover,
  162. * debounce counter is initialized at ID assignment, so no need
  163. * to do it here.
  164. */
  165. }
  166. break;
  167. #endif
  168. #if(CY_CAPSENSE_DISABLE != CY_CAPSENSE_CSD_PROXIMITY_EN)
  169. case (uint8_t)CY_CAPSENSE_WD_PROXIMITY_E:
  170. /* Proximity widgets have 2 debounce counters per sensor (for touch and prox detection) */
  171. (void)memset(ptrWdCfg->ptrDebounceArr, (int32_t)ptrWdCxt->onDebounce, (size_t)(snsNumber << 1u));
  172. break;
  173. #endif
  174. default:
  175. /* No other widget types */
  176. break;
  177. }
  178. /* Reset touch numbers */
  179. #if((CY_CAPSENSE_DISABLE != CY_CAPSENSE_TOUCHPAD_EN) ||\
  180. (CY_CAPSENSE_DISABLE != CY_CAPSENSE_MATRIX_EN) ||\
  181. (CY_CAPSENSE_DISABLE != CY_CAPSENSE_SLIDER_EN))
  182. switch (ptrWdCfg->wdType)
  183. {
  184. case (uint8_t)CY_CAPSENSE_WD_TOUCHPAD_E:
  185. case (uint8_t)CY_CAPSENSE_WD_MATRIX_BUTTON_E:
  186. case (uint8_t)CY_CAPSENSE_WD_LINEAR_SLIDER_E:
  187. case (uint8_t)CY_CAPSENSE_WD_RADIAL_SLIDER_E:
  188. /* Clean number of touches */
  189. ptrWdCxt->wdTouch.numPosition = CY_CAPSENSE_POSITION_NONE;
  190. if (0u != (ptrWdCfg->posFilterConfig & CY_CAPSENSE_POSITION_FILTERS_MASK))
  191. {
  192. ptrWdCfg->ptrPosFilterHistory->numPosition = CY_CAPSENSE_POSITION_NONE;
  193. }
  194. break;
  195. default:
  196. /* No action on other widget types */
  197. break;
  198. }
  199. #endif
  200. #if (CY_CAPSENSE_DISABLE != CY_CAPSENSE_BALLISTIC_MULTIPLIER_EN)
  201. /* Reset ballistic displacement */
  202. if (0u != (ptrWdCfg->centroidConfig & CY_CAPSENSE_CENTROID_BALLISTIC_MASK))
  203. {
  204. ptrWdCxt->xDelta = 0;
  205. ptrWdCxt->yDelta = 0;
  206. ptrWdCfg->ptrBallisticContext->oldTouchNumber = 0u;
  207. }
  208. #endif
  209. /* Reset touch history */
  210. if (0u != (ptrWdCfg->posFilterConfig & CY_CAPSENSE_POSITION_FILTERS_MASK))
  211. {
  212. #if(CY_CAPSENSE_DISABLE != CY_CAPSENSE_TOUCHPAD_EN)
  213. switch (ptrWdCfg->wdType)
  214. {
  215. case (uint8_t)CY_CAPSENSE_WD_TOUCHPAD_E:
  216. /* Clean position filter history */
  217. if (ptrWdCfg->senseMethod == CY_CAPSENSE_CSX_GROUP)
  218. {
  219. /* Reset all history IDs to undefined state */
  220. ptrHistory = ptrWdCfg->ptrPosFilterHistory->ptrPosition;
  221. filterSize = (ptrWdCfg->posFilterConfig & CY_CAPSENSE_POSITION_FILTERS_SIZE_MASK) >> CY_CAPSENSE_POSITION_FILTERS_SIZE_OFFSET;
  222. for (snsIndex = 0u; snsIndex < CY_CAPSENSE_MAX_CENTROIDS; snsIndex++)
  223. {
  224. ptrHistory->id = CY_CAPSENSE_CSX_TOUCHPAD_ID_UNDEFINED;
  225. ptrHistory += filterSize;
  226. }
  227. }
  228. break;
  229. default:
  230. /* No action on other widget types */
  231. break;
  232. }
  233. #endif
  234. #if (CY_CAPSENSE_DISABLE != CY_CAPSENSE_ADAPTIVE_FILTER_EN)
  235. /* Init Adaptive IIR filter */
  236. if (0u != (ptrWdCfg->posFilterConfig & CY_CAPSENSE_POSITION_AIIR_MASK))
  237. {
  238. Cy_CapSense_AdaptiveFilterInitialize_Lib(&ptrWdCfg->aiirConfig,
  239. ptrWdCfg->ptrPosFilterHistory->ptrPosition);
  240. }
  241. #endif
  242. }
  243. }
  244. #if (CY_CAPSENSE_DISABLE != CY_CAPSENSE_GESTURE_EN)
  245. /*******************************************************************************
  246. * Function Name: Cy_CapSense_InitializeWidgetGestures
  247. ****************************************************************************//**
  248. *
  249. * Performs initialization of all gestures for the specified widget.
  250. *
  251. * \param widgetId
  252. * Specifies the ID number of the widget. A macro for the widget ID can be found
  253. * in the cycfg_capsense.h file defined as CY_CAPSENSE_<WIDGET_NAME>_WDGT_ID.
  254. *
  255. * \param context
  256. * The pointer to the CAPSENSE&trade; context structure \ref cy_stc_capsense_context_t.
  257. *
  258. *******************************************************************************/
  259. void Cy_CapSense_InitializeWidgetGestures(
  260. uint32_t widgetId,
  261. const cy_stc_capsense_context_t * context)
  262. {
  263. const cy_stc_capsense_widget_config_t * ptrWdCfg = &context->ptrWdConfig[widgetId];
  264. cy_stc_capsense_widget_context_t * ptrWdCxt = ptrWdCfg->ptrWdContext;
  265. if (((uint8_t)CY_CAPSENSE_WD_LINEAR_SLIDER_E == ptrWdCfg->wdType) ||
  266. ((uint8_t)CY_CAPSENSE_WD_TOUCHPAD_E == ptrWdCfg->wdType))
  267. {
  268. if (NULL != ptrWdCfg->ptrGestureConfig)
  269. {
  270. if (0u != (ptrWdCfg->ptrGestureConfig->gestureEnableMask & CY_CAPSENSE_GESTURE_ALL_GESTURES_MASK))
  271. {
  272. ptrWdCxt->gestureDetected = 0u;
  273. ptrWdCxt->gestureDirection = 0u;
  274. Cy_CapSense_Gesture_ResetState(ptrWdCfg->ptrGestureContext);
  275. }
  276. }
  277. }
  278. }
  279. /*******************************************************************************
  280. * Function Name: Cy_CapSense_DecodeWidgetGestures
  281. ****************************************************************************//**
  282. *
  283. * Performs decoding of all gestures for the specified widget.
  284. *
  285. * This function should be called by application program only after all sensors
  286. * are scanned and all data processing is executed using
  287. * Cy_CapSense_ProcessAllWidgets() or Cy_CapSense_ProcessWidget() functions
  288. * for the widget. Calling this function multiple times without a new sensor
  289. * scan and process causes unexpected behavior.
  290. *
  291. * \note The function (Gesture detection functionality) requires a timestamp
  292. * for its operation. The timestamp should be initialized and maintained
  293. * in the application program prior to calling this function. See the
  294. * descriptions of the Cy_CapSense_SetGestureTimestamp() and
  295. * Cy_CapSense_IncrementGestureTimestamp() functions for details.
  296. *
  297. * \param widgetId
  298. * Specifies the ID number of the widget. A macro for the widget ID can be found
  299. * in the cycfg_capsense.h file defined as CY_CAPSENSE_<WIDGET_NAME>_WDGT_ID.
  300. *
  301. * \param context
  302. * The pointer to the CAPSENSE&trade; context structure \ref cy_stc_capsense_context_t.
  303. *
  304. * \return
  305. * Returns the detected Gesture mask and direction of detected gestures.
  306. * The same information is stored in ptrWdContext->gestureDetected and
  307. * ptrWdContext->gestureDirection registers. Corresponding macros could be found
  308. * \ref group_capsense_macros_gesture.
  309. * * bit[0..15] - detected gesture masks gesture
  310. * * bit[0] - one-finger single click gesture
  311. * * bit[1] - one-finger double click gesture
  312. * * bit[2] - one-finger click and drag gesture
  313. * * bit[3] - two-finger single click gesture
  314. * * bit[4] - one-finger scroll gesture
  315. * * bit[5] - two-finger scroll gesture
  316. * * bit[6] - one-finger edge swipe
  317. * * bit[7] - one-finger flick
  318. * * bit[8] - one-finger rotate
  319. * * bit[9] - two-finger zoom
  320. * * bit[13] - touchdown event
  321. * * bit[14] - liftoff event
  322. * * bit[16..31] - gesture direction if detected
  323. * * bit[0..1] - direction of one-finger scroll gesture
  324. * * bit[2..3] - direction of two-finger scroll gesture
  325. * * bit[4..5] - direction of one-finger edge swipe gesture
  326. * * bit[6] - direction of one-finger rotate gesture
  327. * * bit[7] - direction of two-finger zoom gesture
  328. * * bit[8..10] - direction of one-finger flick gesture
  329. *
  330. * \funcusage
  331. *
  332. * An example of gesture decoding:
  333. * \snippet capsense/snippet/main.c snippet_Cy_CapSense_Gesture
  334. *
  335. * An example of gesture status parsing:
  336. * \snippet capsense/snippet/main.c snippet_Cy_CapSense_Gesture_Macro
  337. *
  338. *******************************************************************************/
  339. uint32_t Cy_CapSense_DecodeWidgetGestures(
  340. uint32_t widgetId,
  341. const cy_stc_capsense_context_t * context)
  342. {
  343. uint32_t gestureStatus = 0u;
  344. uint32_t posIndex;
  345. uint32_t positionNum;
  346. const cy_stc_capsense_widget_config_t * ptrWdCfg = &context->ptrWdConfig[widgetId];
  347. cy_stc_capsense_widget_context_t * ptrWdCxt = ptrWdCfg->ptrWdContext;
  348. cy_stc_capsense_gesture_position_t position[CY_CAPSENSE_MAX_CENTROIDS];
  349. if (((uint8_t)CY_CAPSENSE_WD_LINEAR_SLIDER_E == ptrWdCfg->wdType) ||
  350. ((uint8_t)CY_CAPSENSE_WD_TOUCHPAD_E == ptrWdCfg->wdType))
  351. {
  352. if (NULL != ptrWdCfg->ptrGestureConfig)
  353. {
  354. if (0u != (ptrWdCfg->ptrGestureConfig->gestureEnableMask & CY_CAPSENSE_GESTURE_ALL_GESTURES_MASK))
  355. {
  356. positionNum = ptrWdCxt->wdTouch.numPosition;
  357. if (positionNum > CY_CAPSENSE_MAX_CENTROIDS)
  358. {
  359. positionNum = 0u;
  360. }
  361. for (posIndex = 0u; posIndex < positionNum; posIndex++)
  362. {
  363. position[posIndex].x = ptrWdCxt->wdTouch.ptrPosition[posIndex].x;
  364. position[posIndex].y = ptrWdCxt->wdTouch.ptrPosition[posIndex].y;
  365. }
  366. Cy_CapSense_Gesture_Decode(context->ptrCommonContext->timestamp, (uint32_t)ptrWdCxt->wdTouch.numPosition,
  367. &position[0u], ptrWdCfg->ptrGestureConfig, ptrWdCfg->ptrGestureContext);
  368. ptrWdCxt->gestureDetected = ptrWdCfg->ptrGestureContext->detected;
  369. ptrWdCxt->gestureDirection = ptrWdCfg->ptrGestureContext->direction;
  370. gestureStatus = (uint32_t)ptrWdCxt->gestureDetected | ((uint32_t)ptrWdCxt->gestureDirection << CY_CAPSENSE_GESTURE_DIRECTION_OFFSET);
  371. }
  372. }
  373. }
  374. return gestureStatus;
  375. }
  376. #endif /* (CY_CAPSENSE_DISABLE != CY_CAPSENSE_GESTURE_EN) */
  377. #if (CY_CAPSENSE_ENABLE == CY_CAPSENSE_CSX_EN)
  378. /*******************************************************************************
  379. * Function Name: Cy_CapSense_DpProcessCsxWidgetRawCounts
  380. ****************************************************************************//**
  381. *
  382. * Performs default processing of the raw counts of the specified CSX widget.
  383. *
  384. * The processing includes the following tasks:
  385. * - Run Filters.
  386. * - Update Baselines.
  387. * - Update Differences.
  388. * The same process is applied to all the sensors of the specified widget.
  389. *
  390. * \param widgetId
  391. * Specifies the ID number of the widget. A macro for the widget ID can be found
  392. * in the cycfg_capsense.h file defined as CY_CAPSENSE_<WIDGET_NAME>_WDGT_ID.
  393. *
  394. * \param context
  395. * The pointer to the CAPSENSE&trade; context structure \ref cy_stc_capsense_context_t.
  396. *
  397. * \return
  398. * Returns the status of the specified widget processing operation:
  399. * - Zero - if operation was successfully completed;
  400. * - Non-zero - if baseline processing of any sensor of the specified widget
  401. * failed. The result is concatenated with the index of failed sensor.
  402. *
  403. *******************************************************************************/
  404. uint32_t Cy_CapSense_DpProcessCsxWidgetRawCounts(
  405. uint32_t widgetId,
  406. const cy_stc_capsense_context_t * context)
  407. {
  408. uint32_t result = CY_CAPSENSE_STATUS_SUCCESS;
  409. uint32_t snsIndex;
  410. uint32_t snsHistorySize;
  411. uint32_t freqChIndex;
  412. uint16_t * ptrHistorySns;
  413. uint16_t * ptrBslnInvSns;
  414. uint8_t * ptrHistoryLowSns = NULL;
  415. cy_stc_capsense_sensor_context_t * ptrSnsCxtSns;
  416. const cy_stc_capsense_widget_config_t * ptrWdCfg;
  417. ptrWdCfg = &context->ptrWdConfig[widgetId];
  418. snsHistorySize = (uint32_t)ptrWdCfg->rawFilterConfig & CY_CAPSENSE_RC_FILTER_SNS_HISTORY_SIZE_MASK;
  419. for(freqChIndex = 0u; freqChIndex < CY_CAPSENSE_CONFIGURED_FREQ_NUM; freqChIndex++)
  420. {
  421. ptrSnsCxtSns = &ptrWdCfg->ptrSnsContext[freqChIndex * context->ptrCommonConfig->numSns];
  422. ptrBslnInvSns = &ptrWdCfg->ptrBslnInv[freqChIndex * context->ptrCommonConfig->numSns];
  423. ptrHistorySns = &ptrWdCfg->ptrRawFilterHistory[freqChIndex * (CY_CAPSENSE_RAW_HISTORY_SIZE / CY_CAPSENSE_CONFIGURED_FREQ_NUM)];
  424. if(CY_CAPSENSE_IIR_FILTER_PERFORMANCE == (ptrWdCfg->rawFilterConfig & CY_CAPSENSE_RC_FILTER_IIR_MODE_MASK))
  425. {
  426. ptrHistoryLowSns = &ptrWdCfg->ptrRawFilterHistoryLow[freqChIndex *
  427. (CY_CAPSENSE_IIR_HISTORY_LOW_SIZE / CY_CAPSENSE_CONFIGURED_FREQ_NUM)];
  428. }
  429. for (snsIndex = 0u; snsIndex < ptrWdCfg->numSns; snsIndex++)
  430. {
  431. #if (CY_CAPSENSE_DISABLE != CY_CAPSENSE_RAWCOUNT_FILTER_EN)
  432. Cy_CapSense_FtRunEnabledFiltersInternal(ptrWdCfg, ptrSnsCxtSns, ptrHistorySns, ptrHistoryLowSns);
  433. #endif
  434. result |= Cy_CapSense_FtUpdateBaseline(ptrWdCfg->ptrWdContext, ptrSnsCxtSns, ptrBslnInvSns, context);
  435. Cy_CapSense_DpUpdateDifferences(ptrWdCfg->ptrWdContext, ptrSnsCxtSns);
  436. ptrSnsCxtSns++;
  437. ptrBslnInvSns++;
  438. ptrHistorySns += snsHistorySize;
  439. if(NULL != ptrHistoryLowSns)
  440. {
  441. ptrHistoryLowSns++;
  442. }
  443. }
  444. }
  445. #if (CY_CAPSENSE_DISABLE != CY_CAPSENSE_MULTI_FREQUENCY_SCAN_EN)
  446. ptrSnsCxtSns = ptrWdCfg->ptrSnsContext;
  447. for (snsIndex = ptrWdCfg->numSns; snsIndex-- > 0u;)
  448. {
  449. Cy_CapSense_RunMfsFiltering(ptrSnsCxtSns, context);
  450. ptrSnsCxtSns++;
  451. }
  452. #endif
  453. #if (CY_CAPSENSE_DISABLE != CY_CAPSENSE_MULTI_FREQUENCY_WIDGET_EN)
  454. (void) Cy_CapSense_RunMfsMedian(widgetId, context);
  455. #endif
  456. return result;
  457. }
  458. /*******************************************************************************
  459. * Function Name: Cy_CapSense_DpProcessCsxWidgetStatus
  460. ****************************************************************************//**
  461. *
  462. * Updates the status of the CSX widget in the Data Structure.
  463. *
  464. * This function determines the type of widget and runs the appropriate function
  465. * that implements the status update algorithm for this type of widget.
  466. *
  467. * When the widget-specific processing completes this function updates the
  468. * sensor and widget status registers in the data structure.
  469. *
  470. * \param ptrWdConfig
  471. * The pointer to the widget configuration structure.
  472. *
  473. *******************************************************************************/
  474. void Cy_CapSense_DpProcessCsxWidgetStatus(
  475. const cy_stc_capsense_widget_config_t * ptrWdConfig)
  476. {
  477. switch (ptrWdConfig->wdType)
  478. {
  479. #if((CY_CAPSENSE_DISABLE != CY_CAPSENSE_CSX_BUTTON_EN) ||\
  480. (CY_CAPSENSE_DISABLE != CY_CAPSENSE_CSX_MATRIX_EN))
  481. case (uint8_t)CY_CAPSENSE_WD_BUTTON_E:
  482. case (uint8_t)CY_CAPSENSE_WD_MATRIX_BUTTON_E:
  483. Cy_CapSense_DpProcessButton(ptrWdConfig);
  484. break;
  485. #endif
  486. #if(CY_CAPSENSE_DISABLE != CY_CAPSENSE_CSX_LINEAR_SLIDER_EN)
  487. case (uint8_t)CY_CAPSENSE_WD_LINEAR_SLIDER_E:
  488. Cy_CapSense_DpProcessSlider(ptrWdConfig);
  489. break;
  490. #endif
  491. #if(CY_CAPSENSE_DISABLE != CY_CAPSENSE_CSX_TOUCHPAD_EN)
  492. case (uint8_t)CY_CAPSENSE_WD_TOUCHPAD_E:
  493. Cy_CapSense_DpProcessCsxTouchpad(ptrWdConfig);
  494. break;
  495. #endif
  496. default:
  497. /* Nothing to process since widget type is not valid */
  498. break;
  499. }
  500. }
  501. /*******************************************************************************
  502. * Function Name: Cy_CapSense_DpProcessCsxSensorRawCountsExt
  503. ****************************************************************************//**
  504. *
  505. * Performs customized processing of the CSX sensor raw counts.
  506. *
  507. * If all bits are set at once, the default processing order will take place.
  508. * For a custom order, this function can be called multiple times and execute
  509. * one task at a time.
  510. *
  511. * \param ptrWdConfig
  512. * The pointer to the widget configuration structure.
  513. *
  514. * \param ptrSnsContext
  515. * The pointer to the sensor context structure.
  516. *
  517. * \param ptrSnsRawHistory
  518. * The pointer to the filter history.
  519. *
  520. * \param ptrSnsRawHistoryLow
  521. * The pointer to the extended filter history.
  522. *
  523. * \param mode
  524. * The bit-mask with the data processing tasks to be executed.
  525. * The mode parameters can take the following values:
  526. * - CY_CAPSENSE_PROCESS_FILTER (0x01) Run Firmware Filter
  527. * - CY_CAPSENSE_PROCESS_BASELINE (0x02) Update Baselines
  528. * - CY_CAPSENSE_PROCESS_DIFFCOUNTS (0x04) Update Difference Counts
  529. * - CY_CAPSENSE_PROCESS_ALL Execute all tasks
  530. *
  531. * \param ptrSnsBslnInv
  532. * The pointer to the sensor baseline inversion used for BIST if enabled.
  533. *
  534. * \param context
  535. * The pointer to the CAPSENSE&trade; context structure \ref cy_stc_capsense_context_t.
  536. *
  537. * \return
  538. * Returns the status of the specified sensor processing operation:
  539. * - CY_CAPSENSE_STATUS_SUCCESS if operation was successfully completed;
  540. * - Non-zero - if baseline processing of any
  541. * sensor of the specified widget failed. The result is concatenated with the index
  542. * of failed sensor.
  543. *
  544. *******************************************************************************/
  545. uint32_t Cy_CapSense_DpProcessCsxSensorRawCountsExt(
  546. const cy_stc_capsense_widget_config_t * ptrWdConfig,
  547. cy_stc_capsense_sensor_context_t * ptrSnsContext,
  548. uint16_t * ptrSnsRawHistory,
  549. uint8_t * ptrSnsRawHistoryLow,
  550. uint32_t mode,
  551. uint16_t * ptrSnsBslnInv,
  552. const cy_stc_capsense_context_t * context)
  553. {
  554. uint32_t result = CY_CAPSENSE_STATUS_SUCCESS;
  555. cy_stc_capsense_widget_context_t * ptrWdCxt = ptrWdConfig->ptrWdContext;
  556. #if (CY_CAPSENSE_DISABLE != CY_CAPSENSE_RAWCOUNT_FILTER_EN)
  557. if (0u != (mode & CY_CAPSENSE_PROCESS_FILTER))
  558. {
  559. Cy_CapSense_FtRunEnabledFiltersInternal(ptrWdConfig,
  560. ptrSnsContext,
  561. ptrSnsRawHistory,
  562. ptrSnsRawHistoryLow);
  563. }
  564. #else
  565. (void)ptrSnsRawHistory;
  566. (void)ptrSnsRawHistoryLow;
  567. #endif
  568. if (0u != (mode & CY_CAPSENSE_PROCESS_BASELINE))
  569. {
  570. result = Cy_CapSense_FtUpdateBaseline(ptrWdCxt, ptrSnsContext, ptrSnsBslnInv, context);
  571. }
  572. if (0u != (mode & CY_CAPSENSE_PROCESS_DIFFCOUNTS))
  573. {
  574. Cy_CapSense_DpUpdateDifferences(ptrWdCxt, ptrSnsContext);
  575. }
  576. return result;
  577. }
  578. #endif /* (CY_CAPSENSE_ENABLE == CY_CAPSENSE_CSX_EN) */
  579. #if (CY_CAPSENSE_ENABLE == CY_CAPSENSE_CSD_EN)
  580. /*******************************************************************************
  581. * Function Name: Cy_CapSense_DpProcessCsdWidgetRawCounts
  582. ****************************************************************************//**
  583. *
  584. * Performs default processing of the raw counts of the specified CSD widget.
  585. *
  586. * The processing includes the following tasks:
  587. * - Run Filters.
  588. * - Update Baselines.
  589. * - Update Differences.
  590. * The same process is applied to all the sensors of the specified widget.
  591. *
  592. * \param widgetId
  593. * Specifies the ID number of the widget. A macro for the widget ID can be found
  594. * in the cycfg_capsense.h file defined as CY_CAPSENSE_<WIDGET_NAME>_WDGT_ID.
  595. *
  596. * \param context
  597. * The pointer to the CAPSENSE&trade; context structure \ref cy_stc_capsense_context_t.
  598. *
  599. * \return
  600. * Returns the status of the specified widget processing operation:
  601. * - Zero - if operation was successfully completed.
  602. * - Non-zero - if baseline processing of any sensor of the specified widget
  603. * failed. The result is concatenated with the index of failed sensor.
  604. *
  605. *******************************************************************************/
  606. uint32_t Cy_CapSense_DpProcessCsdWidgetRawCounts(
  607. uint32_t widgetId,
  608. const cy_stc_capsense_context_t * context)
  609. {
  610. uint32_t result = CY_CAPSENSE_STATUS_SUCCESS;
  611. uint32_t snsIndex;
  612. uint32_t snsHistorySize;
  613. uint32_t freqChIndex;
  614. uint16_t * ptrHistorySns;
  615. uint16_t * ptrBslnInvSns;
  616. uint8_t * ptrHistoryLowSns = NULL;
  617. cy_stc_capsense_sensor_context_t * ptrSnsCxtSns;
  618. const cy_stc_capsense_widget_config_t * ptrWdCfg;
  619. ptrWdCfg = &context->ptrWdConfig[widgetId];
  620. #if (CY_CAPSENSE_ENABLE == CY_CAPSENSE_SMARTSENSE_FULL_EN)
  621. cy_stc_capsense_smartsense_csd_noise_envelope_t * ptrNEHistory = ptrWdCfg->ptrNoiseEnvelope;
  622. #endif
  623. snsHistorySize = (uint32_t)ptrWdCfg->rawFilterConfig & CY_CAPSENSE_RC_FILTER_SNS_HISTORY_SIZE_MASK;
  624. for(freqChIndex = 0u; freqChIndex < CY_CAPSENSE_CONFIGURED_FREQ_NUM; freqChIndex++)
  625. {
  626. ptrSnsCxtSns = &ptrWdCfg->ptrSnsContext[freqChIndex * context->ptrCommonConfig->numSns];
  627. ptrBslnInvSns = &ptrWdCfg->ptrBslnInv[freqChIndex * context->ptrCommonConfig->numSns];
  628. ptrHistorySns = &ptrWdCfg->ptrRawFilterHistory[freqChIndex * (CY_CAPSENSE_RAW_HISTORY_SIZE / CY_CAPSENSE_CONFIGURED_FREQ_NUM)];
  629. if(CY_CAPSENSE_IIR_FILTER_PERFORMANCE == (ptrWdCfg->rawFilterConfig & CY_CAPSENSE_RC_FILTER_IIR_MODE_MASK))
  630. {
  631. ptrHistoryLowSns = &ptrWdCfg->ptrRawFilterHistoryLow[freqChIndex *
  632. (CY_CAPSENSE_IIR_HISTORY_LOW_SIZE / CY_CAPSENSE_CONFIGURED_FREQ_NUM)];
  633. }
  634. for (snsIndex = 0u; snsIndex < ptrWdCfg->numSns; snsIndex++)
  635. {
  636. #if (CY_CAPSENSE_DISABLE != CY_CAPSENSE_RAWCOUNT_FILTER_EN)
  637. Cy_CapSense_FtRunEnabledFiltersInternal(ptrWdCfg, ptrSnsCxtSns, ptrHistorySns, ptrHistoryLowSns);
  638. #endif
  639. /* Run auto-tuning activities */
  640. #if (CY_CAPSENSE_ENABLE == CY_CAPSENSE_SMARTSENSE_FULL_EN)
  641. Cy_CapSense_RunNoiseEnvelope_Lib(ptrSnsCxtSns->raw, ptrWdCfg->ptrWdContext->sigPFC, ptrNEHistory);
  642. Cy_CapSense_DpUpdateThresholds(ptrWdCfg->ptrWdContext, ptrNEHistory, snsIndex);
  643. #if(CY_CAPSENSE_DISABLE != CY_CAPSENSE_CSD_PROXIMITY_EN)
  644. if ((uint8_t)CY_CAPSENSE_WD_PROXIMITY_E == ptrWdCfg->wdType)
  645. {
  646. ptrWdCfg->ptrWdContext->proxTh = (uint16_t)(((uint32_t)ptrWdCfg->ptrWdContext->fingerTh *
  647. context->ptrCommonConfig->proxTouchCoeff) / CY_CAPSENSE_PERCENTAGE_100);
  648. }
  649. #endif
  650. ptrNEHistory++;
  651. #endif
  652. result |= Cy_CapSense_FtUpdateBaseline(ptrWdCfg->ptrWdContext, ptrSnsCxtSns, ptrBslnInvSns, context);
  653. Cy_CapSense_DpUpdateDifferences(ptrWdCfg->ptrWdContext, ptrSnsCxtSns);
  654. ptrSnsCxtSns++;
  655. ptrBslnInvSns++;
  656. ptrHistorySns += snsHistorySize;
  657. if(NULL != ptrHistoryLowSns)
  658. {
  659. ptrHistoryLowSns++;
  660. }
  661. }
  662. }
  663. #if (CY_CAPSENSE_ENABLE == CY_CAPSENSE_MULTI_FREQUENCY_SCAN_EN)
  664. ptrSnsCxtSns = ptrWdCfg->ptrSnsContext;
  665. for (snsIndex = ptrWdCfg->numSns; snsIndex-- > 0u;)
  666. {
  667. Cy_CapSense_RunMfsFiltering(ptrSnsCxtSns, context);
  668. ptrSnsCxtSns++;
  669. }
  670. #endif
  671. #if (CY_CAPSENSE_DISABLE != CY_CAPSENSE_MULTI_FREQUENCY_WIDGET_EN)
  672. (void) Cy_CapSense_RunMfsMedian(widgetId, context);
  673. #endif
  674. return result;
  675. }
  676. /*******************************************************************************
  677. * Function Name: Cy_CapSense_DpProcessCsdSensorRawCountsExt
  678. ****************************************************************************//**
  679. *
  680. * Performs customized processing of the CSX sensor raw counts.
  681. *
  682. * If all bits are set at once, the default processing order will take place.
  683. * For a custom order, this function can be called multiple times and execute
  684. * one task at a time.
  685. *
  686. * \param ptrWdConfig
  687. * The pointer to the widget configuration structure.
  688. *
  689. * \param ptrSnsContext
  690. * The pointer to the sensor context structure.
  691. *
  692. * \param ptrSnsRawHistory
  693. * The pointer to the filter history.
  694. *
  695. * \param ptrSnsRawHistoryLow
  696. * The pointer to the extended filter history.
  697. *
  698. * \param mode
  699. * The bit-mask with the data processing tasks to be executed.
  700. * The mode parameters can take the following values:
  701. * - CY_CAPSENSE_PROCESS_FILTER (0x01) Run Firmware Filter
  702. * - CY_CAPSENSE_PROCESS_BASELINE (0x02) Update Baselines
  703. * - CY_CAPSENSE_PROCESS_DIFFCOUNTS (0x04) Update Difference Counts
  704. * - CY_CAPSENSE_PROCESS_CALC_NOISE (0x08) Calculate Noise (only if FW Tuning is enabled)
  705. * - CY_CAPSENSE_PROCESS_THRESHOLDS (0x10) Update Thresholds (only if FW Tuning is enabled)
  706. * - CY_CAPSENSE_PROCESS_ALL Execute all tasks
  707. *
  708. * \param ptrBslnInvSns
  709. * The pointer to the sensor baseline inversion used for BIST if enabled.
  710. *
  711. * \param context
  712. * The pointer to the CAPSENSE&trade; context structure \ref cy_stc_capsense_context_t.
  713. *
  714. * \return
  715. * Returns the status of the specified sensor processing operation:
  716. * - CY_CAPSENSE_STATUS_SUCCESS if operation was successfully completed.
  717. * - CY_CAPSENSE_STATUS_BAD_DATA if baseline processing of any sensor of the specified widget
  718. * failed.
  719. *
  720. *******************************************************************************/
  721. uint32_t Cy_CapSense_DpProcessCsdSensorRawCountsExt(
  722. const cy_stc_capsense_widget_config_t * ptrWdConfig,
  723. cy_stc_capsense_sensor_context_t * ptrSnsContext,
  724. uint16_t * ptrSnsRawHistory,
  725. uint8_t * ptrSnsRawHistoryLow,
  726. uint32_t mode,
  727. uint16_t * ptrBslnInvSns,
  728. const cy_stc_capsense_context_t * context)
  729. {
  730. uint32_t result = CY_CAPSENSE_STATUS_SUCCESS;
  731. cy_stc_capsense_widget_context_t * ptrWdCxt = ptrWdConfig->ptrWdContext;
  732. #if (CY_CAPSENSE_DISABLE != CY_CAPSENSE_RAWCOUNT_FILTER_EN)
  733. if (0u != (mode & CY_CAPSENSE_PROCESS_FILTER))
  734. {
  735. Cy_CapSense_FtRunEnabledFiltersInternal(ptrWdConfig, ptrSnsContext,
  736. ptrSnsRawHistory, ptrSnsRawHistoryLow);
  737. }
  738. #else
  739. (void)ptrSnsRawHistory;
  740. (void)ptrSnsRawHistoryLow;
  741. #endif
  742. if (0u != (mode & CY_CAPSENSE_PROCESS_BASELINE))
  743. {
  744. result = Cy_CapSense_FtUpdateBaseline(ptrWdCxt, ptrSnsContext, ptrBslnInvSns, context);
  745. }
  746. if (0u != (mode & CY_CAPSENSE_PROCESS_DIFFCOUNTS))
  747. {
  748. Cy_CapSense_DpUpdateDifferences(ptrWdCxt, ptrSnsContext);
  749. }
  750. return result;
  751. }
  752. /*******************************************************************************
  753. * Function Name: Cy_CapSense_DpProcessCsdWidgetStatus
  754. ****************************************************************************//**
  755. *
  756. * Updates the status of the CSD widget in the Data Structure.
  757. *
  758. * This function determines the type of widget and runs the appropriate function
  759. * that implements the status update algorithm for this type of widget.
  760. *
  761. * When the widget-specific processing completes this function updates the
  762. * sensor and widget status registers in the data structure.
  763. *
  764. * \param ptrWdConfig
  765. * The pointer to the widget configuration structure.
  766. *
  767. * \param context
  768. * The pointer to the CAPSENSE&trade; context structure \ref cy_stc_capsense_context_t.
  769. *
  770. *******************************************************************************/
  771. void Cy_CapSense_DpProcessCsdWidgetStatus(
  772. const cy_stc_capsense_widget_config_t * ptrWdConfig,
  773. cy_stc_capsense_context_t * context)
  774. {
  775. (void)context;
  776. switch (ptrWdConfig->wdType)
  777. {
  778. #if(CY_CAPSENSE_DISABLE != CY_CAPSENSE_CSD_BUTTON_EN)
  779. case (uint8_t)CY_CAPSENSE_WD_BUTTON_E:
  780. Cy_CapSense_DpProcessButton(ptrWdConfig);
  781. break;
  782. #endif
  783. #if(CY_CAPSENSE_DISABLE != CY_CAPSENSE_CSD_SLIDER_EN)
  784. case (uint8_t)CY_CAPSENSE_WD_LINEAR_SLIDER_E:
  785. case (uint8_t)CY_CAPSENSE_WD_RADIAL_SLIDER_E:
  786. Cy_CapSense_DpProcessSlider(ptrWdConfig);
  787. break;
  788. #endif
  789. #if(CY_CAPSENSE_DISABLE != CY_CAPSENSE_CSD_MATRIX_EN)
  790. case (uint8_t)CY_CAPSENSE_WD_MATRIX_BUTTON_E:
  791. Cy_CapSense_DpProcessCsdMatrix(ptrWdConfig);
  792. break;
  793. #endif
  794. #if(CY_CAPSENSE_DISABLE != CY_CAPSENSE_CSD_TOUCHPAD_EN)
  795. case (uint8_t)CY_CAPSENSE_WD_TOUCHPAD_E:
  796. Cy_CapSense_DpProcessCsdTouchpad(ptrWdConfig, context);
  797. break;
  798. #endif
  799. #if(CY_CAPSENSE_DISABLE != CY_CAPSENSE_CSD_PROXIMITY_EN)
  800. case (uint8_t)CY_CAPSENSE_WD_PROXIMITY_E:
  801. Cy_CapSense_DpProcessProximity(ptrWdConfig);
  802. break;
  803. #endif
  804. default:
  805. /* No other widget types */
  806. break;
  807. }
  808. }
  809. #endif /* (CY_CAPSENSE_ENABLE == CY_CAPSENSE_CSD_EN) */
  810. /*******************************************************************************
  811. * Function Name: Cy_CapSense_DpUpdateThresholds
  812. ****************************************************************************//**
  813. *
  814. * Updates noise and finger thresholds for a specified widget.
  815. *
  816. * Used for smart sensing algorithm.
  817. *
  818. * \param ptrWdContext
  819. * The pointer to the widget context structure.
  820. *
  821. * \param ptrNoiseEnvelope
  822. * The pointer to the noise-envelope history structure.
  823. *
  824. * \param startFlag
  825. * The flag indicates when a new widget is processed.
  826. *
  827. *******************************************************************************/
  828. void Cy_CapSense_DpUpdateThresholds(
  829. cy_stc_capsense_widget_context_t * ptrWdContext,
  830. const cy_stc_capsense_smartsense_csd_noise_envelope_t * ptrNoiseEnvelope,
  831. uint32_t startFlag)
  832. {
  833. cy_stc_capsense_smartsense_update_thresholds_t thresholds;
  834. /* Calculate Thresholds */
  835. thresholds.fingerTh = ptrWdContext->fingerTh;
  836. Cy_CapSense_UpdateThresholds_Lib(ptrNoiseEnvelope, &thresholds, ptrWdContext->sigPFC, startFlag);
  837. /* Update CAPSENSE&trade; context */
  838. ptrWdContext->fingerTh = thresholds.fingerTh;
  839. ptrWdContext->noiseTh = (uint16_t)thresholds.noiseTh;
  840. ptrWdContext->nNoiseTh = (uint16_t)thresholds.nNoiseTh;
  841. ptrWdContext->hysteresis = (uint16_t)thresholds.hysteresis;
  842. }
  843. /*******************************************************************************
  844. * Function Name: Cy_CapSense_DpUpdateDifferences
  845. ****************************************************************************//**
  846. *
  847. * Calculates new difference values.
  848. *
  849. * This function calculates the difference between the baseline and raw counts.
  850. * If the difference is positive (raw > baseline), and higher than the widget
  851. * noise threshold, it is saved into the data structure for further processing.
  852. * Otherwise the difference is set to zero. The final difference value is saved
  853. * with the subtracted noise threshold value.
  854. *
  855. * \param ptrWdContext
  856. * The pointer to the widget context structure.
  857. *
  858. * \param ptrSnsContext
  859. * The pointer to the sensor context structure.
  860. *
  861. *******************************************************************************/
  862. void Cy_CapSense_DpUpdateDifferences(
  863. const cy_stc_capsense_widget_context_t * ptrWdContext,
  864. cy_stc_capsense_sensor_context_t * ptrSnsContext)
  865. {
  866. ptrSnsContext->diff = 0u;
  867. if ((uint32_t)ptrSnsContext->raw > ((uint32_t)ptrSnsContext->bsln + ptrWdContext->noiseTh))
  868. {
  869. ptrSnsContext->diff = ptrSnsContext->raw - ptrSnsContext->bsln;
  870. }
  871. }
  872. #if((CY_CAPSENSE_DISABLE != CY_CAPSENSE_BUTTON_EN) || (CY_CAPSENSE_DISABLE != CY_CAPSENSE_CSX_MATRIX_EN))
  873. /*******************************************************************************
  874. * Function Name: Cy_CapSense_DpProcessButton
  875. ****************************************************************************//**
  876. *
  877. * Processes the status of the Button widget.
  878. *
  879. * This function processes the status of the CSD/CSX Button widget and
  880. * CSX Matrix Button widget. It applies the hysteresis and debounce algorithm
  881. * to each sensor difference value. This function is expected to be called
  882. * after each new widget scan. If it is called multiple times for the same
  883. * scan data, the debounce algorithm will not work properly.
  884. *
  885. * \param ptrWdConfig
  886. * The pointer to the widget configuration structure.
  887. *
  888. *******************************************************************************/
  889. void Cy_CapSense_DpProcessButton(
  890. const cy_stc_capsense_widget_config_t * ptrWdConfig)
  891. {
  892. uint32_t snsIndex;
  893. uint32_t activeCount = 0u;
  894. uint32_t touchTh;
  895. #if(CY_CAPSENSE_DISABLE != CY_CAPSENSE_CSX_MATRIX_EN)
  896. uint32_t startIndex = 0u;
  897. #endif
  898. uint8_t * ptrDebounceCnt = ptrWdConfig->ptrDebounceArr;
  899. cy_stc_capsense_sensor_context_t * ptrSnsCxt = ptrWdConfig->ptrSnsContext;
  900. cy_stc_capsense_widget_context_t * ptrWdCxt = ptrWdConfig->ptrWdContext;
  901. ptrWdCxt->status &= (uint8_t)~CY_CAPSENSE_WD_ACTIVE_MASK;
  902. /* Go through all widget's sensors */
  903. for (snsIndex = 0u; snsIndex < ptrWdConfig->numSns; snsIndex++)
  904. {
  905. /* Calculate touch threshold based on current sensor state */
  906. touchTh = (0u == ptrSnsCxt->status) ?
  907. ((uint32_t)ptrWdCxt->fingerTh + ptrWdCxt->hysteresis) :
  908. ((uint32_t)ptrWdCxt->fingerTh - ptrWdCxt->hysteresis);
  909. if (0u < (*ptrDebounceCnt))
  910. {
  911. /* Decrement debounce counter */
  912. (*ptrDebounceCnt)--;
  913. }
  914. /* No touch */
  915. if (ptrSnsCxt->diff <= touchTh)
  916. {
  917. /* Reset debounce counter */
  918. *ptrDebounceCnt = ptrWdCxt->onDebounce;
  919. ptrSnsCxt->status = 0u;
  920. }
  921. /* New touch or touch still exists */
  922. if (0u == (*ptrDebounceCnt))
  923. {
  924. ptrSnsCxt->status = CY_CAPSENSE_SNS_TOUCH_STATUS_MASK;
  925. activeCount++;
  926. #if(CY_CAPSENSE_DISABLE != CY_CAPSENSE_CSX_MATRIX_EN)
  927. startIndex = snsIndex;
  928. #endif
  929. }
  930. /* Update widget status */
  931. if (0u != ptrSnsCxt->status)
  932. {
  933. ptrWdCxt->status |= (uint8_t)CY_CAPSENSE_WD_ACTIVE_MASK;
  934. }
  935. ptrSnsCxt++;
  936. ptrDebounceCnt++;
  937. }
  938. /* Update position struct */
  939. #if(CY_CAPSENSE_DISABLE != CY_CAPSENSE_CSX_MATRIX_EN)
  940. if (((uint8_t)CY_CAPSENSE_WD_MATRIX_BUTTON_E == ptrWdConfig->wdType) &&
  941. (CY_CAPSENSE_CSX_GROUP == ptrWdConfig->senseMethod))
  942. {
  943. ptrWdCxt->wdTouch.numPosition = (uint8_t)activeCount;
  944. ptrWdCxt->wdTouch.ptrPosition->id = (uint16_t)startIndex;
  945. ptrWdCxt->wdTouch.ptrPosition->x = (uint16_t)(startIndex / ptrWdConfig->numRows);
  946. ptrWdCxt->wdTouch.ptrPosition->y = (uint16_t)(startIndex % ptrWdConfig->numRows);
  947. }
  948. #endif
  949. }
  950. #endif /* ((CY_CAPSENSE_DISABLE != CY_CAPSENSE_BUTTON_EN) || (CY_CAPSENSE_DISABLE != CY_CAPSENSE_CSX_MATRIX_EN)) */
  951. #if(CY_CAPSENSE_DISABLE != CY_CAPSENSE_CSD_PROXIMITY_EN)
  952. /*******************************************************************************
  953. * Function Name: Cy_CapSense_DpProcessProximity
  954. ****************************************************************************//**
  955. *
  956. * Processes the status of the Proximity widget.
  957. *
  958. * This function processes the status of the CSD Proximity widget. It applies the
  959. * hysteresis and debounce algorithm to each sensor difference value.
  960. * The proximity and touch events are considered independently so debounce and
  961. * hysteresis are also applied independently.
  962. *
  963. * This function is expected to be called after each new widget scan. If it is
  964. * called multiple times for the same scan data the debounce algorithm
  965. * will not work properly.
  966. *
  967. * \param ptrWdConfig
  968. * The pointer to the widget configuration structure.
  969. *
  970. *******************************************************************************/
  971. void Cy_CapSense_DpProcessProximity(const cy_stc_capsense_widget_config_t * ptrWdConfig)
  972. {
  973. uint32_t snsTh;
  974. uint32_t snsIndex;
  975. uint32_t snsStMask;
  976. uint8_t * ptrDebounceCnt = ptrWdConfig->ptrDebounceArr;
  977. cy_stc_capsense_sensor_context_t * ptrSnsCxt = ptrWdConfig->ptrSnsContext;
  978. cy_stc_capsense_widget_context_t * ptrWdCxt = ptrWdConfig->ptrWdContext;
  979. /* Reset widget status */
  980. ptrWdCxt->status &= (uint8_t)~CY_CAPSENSE_WD_ACTIVE_MASK;
  981. /* Go through all sensor's status bits */
  982. for (snsIndex = 0u; snsIndex < ((uint32_t)ptrWdConfig->numSns << 1u); snsIndex++)
  983. {
  984. /*
  985. * Proximity - odd; Touch - even. Example:
  986. * Bit 0 -> touch event
  987. * Bit 1 -> proximity event
  988. */
  989. snsTh = ptrWdCxt->proxTh;
  990. snsStMask = CY_CAPSENSE_SNS_PROX_STATUS_MASK;
  991. if (0u == (snsIndex & 0x01u))
  992. {
  993. snsTh = ptrWdCxt->fingerTh;
  994. snsStMask = CY_CAPSENSE_SNS_TOUCH_STATUS_MASK;
  995. }
  996. /* Calculate threshold based on current sensor state */
  997. snsTh = (0u == (snsStMask & ptrSnsCxt->status)) ?
  998. (snsTh + ptrWdCxt->hysteresis) :
  999. (snsTh - ptrWdCxt->hysteresis);
  1000. if (0u < (*ptrDebounceCnt))
  1001. {
  1002. /* Decrement debounce counter */
  1003. (*ptrDebounceCnt)--;
  1004. }
  1005. /* No touch */
  1006. if (ptrSnsCxt->diff <= snsTh)
  1007. {
  1008. /* Reset debounce counter */
  1009. *ptrDebounceCnt = ptrWdCxt->onDebounce;
  1010. ptrSnsCxt->status &= (uint8_t)(~snsStMask);
  1011. }
  1012. /* New touch or touch still exists */
  1013. if (0u == (*ptrDebounceCnt))
  1014. {
  1015. ptrSnsCxt->status |= (uint8_t)snsStMask;
  1016. }
  1017. /* Update widget status */
  1018. if (0u != (ptrSnsCxt->status & (uint8_t)snsStMask))
  1019. {
  1020. ptrWdCxt->status |= (uint8_t)CY_CAPSENSE_WD_ACTIVE_MASK;
  1021. }
  1022. if (0u != (snsIndex & 0x01u))
  1023. {
  1024. ptrSnsCxt++;
  1025. }
  1026. ptrDebounceCnt++;
  1027. }
  1028. }
  1029. #endif /* (CY_CAPSENSE_DISABLE != CY_CAPSENSE_CSD_PROXIMITY_EN) */
  1030. #if(CY_CAPSENSE_DISABLE != CY_CAPSENSE_SLIDER_EN)
  1031. /*******************************************************************************
  1032. * Function Name: Cy_CapSense_DpProcessSlider
  1033. ****************************************************************************//**
  1034. *
  1035. * Processes the status of the Linear/Radial Slider widget.
  1036. * The process involves running the Linear/Radial centroid algorithm.
  1037. *
  1038. * It applies the hysteresis and debounce algorithm to the widget ignoring
  1039. * the individual states of the sensors.
  1040. *
  1041. * This function is expected to be called after each new widget scan. If it is
  1042. * called multiple times for the same scan data, the debounce algorithm
  1043. * will not work properly.
  1044. *
  1045. * \param ptrWdConfig
  1046. * The pointer to the widget configuration structure.
  1047. *
  1048. *******************************************************************************/
  1049. void Cy_CapSense_DpProcessSlider(
  1050. const cy_stc_capsense_widget_config_t * ptrWdConfig)
  1051. {
  1052. uint32_t snsIndex;
  1053. uint32_t touchTh;
  1054. uint32_t wdActive = 0u;
  1055. uint8_t * ptrDebounceCnt = ptrWdConfig->ptrDebounceArr;
  1056. cy_stc_capsense_sensor_context_t * ptrSnsCxt = ptrWdConfig->ptrSnsContext;
  1057. cy_stc_capsense_widget_context_t * ptrWdCxt = ptrWdConfig->ptrWdContext;
  1058. uint32_t sensorNumber = ptrWdConfig->numSns;
  1059. cy_stc_capsense_position_t newPosition[CY_CAPSENSE_MAX_CENTROIDS];
  1060. cy_stc_capsense_touch_t newTouch = {&newPosition[0u], CY_CAPSENSE_POSITION_NONE};
  1061. /* Calculate touch threshold based on current slider state */
  1062. touchTh = (0u == (ptrWdCxt->status & CY_CAPSENSE_WD_ACTIVE_MASK)) ?
  1063. ((uint32_t)ptrWdCxt->fingerTh + ptrWdCxt->hysteresis) :
  1064. ((uint32_t)ptrWdCxt->fingerTh - ptrWdCxt->hysteresis);
  1065. if (0u < (*ptrDebounceCnt))
  1066. {
  1067. /* Decrement debounce counter */
  1068. (*ptrDebounceCnt)--;
  1069. }
  1070. /* Check new widget activity */
  1071. for (snsIndex = sensorNumber; snsIndex-- > 0u;)
  1072. {
  1073. ptrSnsCxt->status = (touchTh < ptrSnsCxt->diff) ? CY_CAPSENSE_SNS_TOUCH_STATUS_MASK : 0u;
  1074. wdActive |= ptrSnsCxt->status;
  1075. ptrSnsCxt++;
  1076. }
  1077. /* No touch detected */
  1078. if (0u == wdActive)
  1079. {
  1080. /* Reset debounce counter */
  1081. (*ptrDebounceCnt) = ptrWdCxt->onDebounce;
  1082. ptrWdCxt->status &= (uint8_t)(~CY_CAPSENSE_WD_ACTIVE_MASK);
  1083. }
  1084. if (0u == (*ptrDebounceCnt))
  1085. {
  1086. /* New touch detected or touch still exists from previous cycle */
  1087. ptrWdCxt->status |= CY_CAPSENSE_WD_ACTIVE_MASK;
  1088. }
  1089. else
  1090. {
  1091. if (0u != wdActive)
  1092. {
  1093. /* Clear sensor state if activity was detected but debounce was not passed */
  1094. ptrSnsCxt = ptrWdConfig->ptrSnsContext;
  1095. for (snsIndex = sensorNumber; snsIndex-- > 0u;)
  1096. {
  1097. ptrSnsCxt->status = 0u;
  1098. ptrSnsCxt++;
  1099. }
  1100. }
  1101. }
  1102. /* Centroid processing */
  1103. if (CY_CAPSENSE_WD_ACTIVE_MASK == (ptrWdCxt->status & CY_CAPSENSE_WD_ACTIVE_MASK))
  1104. {
  1105. switch (ptrWdConfig->wdType)
  1106. {
  1107. #if(CY_CAPSENSE_DISABLE != CY_CAPSENSE_CSD_RADIAL_SLIDER_EN)
  1108. case (uint8_t)CY_CAPSENSE_WD_RADIAL_SLIDER_E:
  1109. Cy_CapSense_DpCentroidRadial(&newTouch, ptrWdConfig);
  1110. break;
  1111. #endif
  1112. #if((CY_CAPSENSE_DISABLE != CY_CAPSENSE_CSD_LINEAR_SLIDER_EN) ||\
  1113. (CY_CAPSENSE_DISABLE != CY_CAPSENSE_CSX_LINEAR_SLIDER_EN))
  1114. case (uint8_t)CY_CAPSENSE_WD_LINEAR_SLIDER_E:
  1115. #if((CY_CAPSENSE_DISABLE != CY_CAPSENSE_CSD_DIPLEX_SLIDER_EN) ||\
  1116. (CY_CAPSENSE_DISABLE != CY_CAPSENSE_CSX_DIPLEX_SLIDER_EN))
  1117. if ((0u != (ptrWdConfig->centroidConfig & CY_CAPSENSE_DIPLEXING_MASK)))
  1118. {
  1119. /* Run local maximum search for diplexed slider */
  1120. Cy_CapSense_DpCentroidDiplex(&newTouch, ptrWdConfig);
  1121. }
  1122. else
  1123. {
  1124. Cy_CapSense_DpCentroidLinear(&newTouch, ptrWdConfig);
  1125. }
  1126. #else
  1127. Cy_CapSense_DpCentroidLinear(&newTouch, ptrWdConfig);
  1128. #endif
  1129. break;
  1130. #endif
  1131. default:
  1132. /* No action on other widget types */
  1133. break;
  1134. }
  1135. }
  1136. #if (CY_CAPSENSE_DISABLE != CY_CAPSENSE_POSITION_FILTER_EN)
  1137. /* Position filtering */
  1138. if (0u != (ptrWdConfig->posFilterConfig & CY_CAPSENSE_POSITION_FILTERS_MASK))
  1139. {
  1140. Cy_CapSense_ProcessPositionFilters(&newTouch, ptrWdConfig);
  1141. }
  1142. #endif
  1143. /* Copy positions into public structure */
  1144. ptrWdConfig->ptrWdContext->wdTouch.numPosition = newTouch.numPosition;
  1145. for (snsIndex = 0u; snsIndex < newTouch.numPosition; snsIndex++)
  1146. {
  1147. ptrWdConfig->ptrWdContext->wdTouch.ptrPosition[snsIndex] = newTouch.ptrPosition[snsIndex];
  1148. }
  1149. }
  1150. #endif /* (CY_CAPSENSE_DISABLE != CY_CAPSENSE_SLIDER_EN) */
  1151. #if(CY_CAPSENSE_DISABLE != CY_CAPSENSE_CSD_MATRIX_EN)
  1152. /*******************************************************************************
  1153. * Function Name: Cy_CapSense_DpProcessCsdMatrix
  1154. ****************************************************************************//**
  1155. *
  1156. * Processes the status of the CSD Matrix Button widget.
  1157. *
  1158. * This function processes the status of the CSD Matrix Button widget.
  1159. * It applies the hysteresis and debounce algorithm to each sensor value.
  1160. *
  1161. * Then the function analyzes how many row and column sensors are active.
  1162. * If only one per row and one per column, the function considers this as
  1163. * a valid touch and updates the corresponding Data Structure registers
  1164. * with the button id and active row and column sensors.
  1165. *
  1166. * If multiple sensors are active in row or column sensors, this function sets
  1167. * the corresponding registers to the CY_CAPSENSE_POSITION_MULTIPLE
  1168. * value that indicates that it is not possible to detect the touched button id.
  1169. *
  1170. * This function is expected to be called after each new widget scan. If it is
  1171. * called multiple times for the same scan data, the debounce algorithm
  1172. * will not work properly.
  1173. *
  1174. * \param ptrWdConfig
  1175. * The pointer to the widget configuration structure.
  1176. *
  1177. *******************************************************************************/
  1178. void Cy_CapSense_DpProcessCsdMatrix(
  1179. const cy_stc_capsense_widget_config_t * ptrWdConfig)
  1180. {
  1181. uint32_t snsIndex;
  1182. uint32_t touchTh;
  1183. uint32_t colNumber = ptrWdConfig->numCols;
  1184. uint8_t * ptrDebounceCnt = ptrWdConfig->ptrDebounceArr;
  1185. cy_stc_capsense_sensor_context_t * ptrSnsCxt = ptrWdConfig->ptrSnsContext;
  1186. cy_stc_capsense_widget_context_t * ptrWdCxt = ptrWdConfig->ptrWdContext;
  1187. cy_stc_capsense_position_t * ptrSnsTouch = ptrWdCxt->wdTouch.ptrPosition;
  1188. uint32_t numActiveRows = 0u;
  1189. uint32_t numActiveCols = 0u;
  1190. uint32_t activeRow = 0u;
  1191. uint32_t activeCol = 0u;
  1192. /* Reset status */
  1193. ptrWdCxt->status &= (uint8_t)~CY_CAPSENSE_WD_ACTIVE_MASK;
  1194. /* Go through all widget's sensors */
  1195. for (snsIndex = 0u; snsIndex < ptrWdConfig->numSns; snsIndex++)
  1196. {
  1197. /* Calculate touch threshold based on current sensor state */
  1198. touchTh = (0u == ptrSnsCxt->status) ?
  1199. ((uint32_t)ptrWdCxt->fingerTh + ptrWdCxt->hysteresis) :
  1200. ((uint32_t)ptrWdCxt->fingerTh - ptrWdCxt->hysteresis);
  1201. if (0u < (*ptrDebounceCnt))
  1202. {
  1203. /* Decrement debounce counter */
  1204. (*ptrDebounceCnt)--;
  1205. }
  1206. /* No touch */
  1207. if (ptrSnsCxt->diff <= touchTh)
  1208. {
  1209. /* Reset debounce counter */
  1210. *ptrDebounceCnt = ptrWdCxt->onDebounce;
  1211. ptrSnsCxt->status = 0u;
  1212. }
  1213. /* New touch or touch still exists */
  1214. if (0u == (*ptrDebounceCnt))
  1215. {
  1216. ptrSnsCxt->status = CY_CAPSENSE_SNS_TOUCH_STATUS_MASK;
  1217. }
  1218. /* Update information about active row/col sensors */
  1219. if (0u != ptrSnsCxt->status)
  1220. {
  1221. if (snsIndex < colNumber)
  1222. {
  1223. numActiveCols++;
  1224. activeCol = snsIndex;
  1225. }
  1226. else
  1227. {
  1228. numActiveRows++;
  1229. activeRow = snsIndex - colNumber;
  1230. }
  1231. }
  1232. ptrSnsCxt++;
  1233. ptrDebounceCnt++;
  1234. }
  1235. /*
  1236. * Number of touches (numActiveCols * numActiveRows):
  1237. * 0 -> No touch
  1238. * 1 -> One touch
  1239. * 2+ -> Multiple touches
  1240. */
  1241. ptrWdCxt->wdTouch.numPosition = (uint8_t)(numActiveCols * numActiveRows);
  1242. if (ptrWdCxt->wdTouch.numPosition > CY_CAPSENSE_POSITION_ONE)
  1243. {
  1244. ptrWdCxt->wdTouch.numPosition = CY_CAPSENSE_POSITION_MULTIPLE;
  1245. }
  1246. if (CY_CAPSENSE_POSITION_ONE == ptrWdCxt->wdTouch.numPosition)
  1247. {
  1248. ptrSnsTouch->x = (uint16_t)activeCol;
  1249. ptrSnsTouch->y = (uint16_t)activeRow;
  1250. ptrSnsTouch->z = 0u;
  1251. ptrSnsTouch->id = (uint16_t)((activeRow * colNumber) + activeCol);
  1252. }
  1253. /* Update widget status if any activity is detected (even non-valid) */
  1254. if ((0u != numActiveRows) || (0u != numActiveCols))
  1255. {
  1256. ptrWdCxt->status |= (uint8_t)CY_CAPSENSE_WD_ACTIVE_MASK;
  1257. }
  1258. }
  1259. #endif /* (CY_CAPSENSE_DISABLE != CY_CAPSENSE_CSD_MATRIX_EN) */
  1260. #if(CY_CAPSENSE_DISABLE != CY_CAPSENSE_CSD_TOUCHPAD_EN)
  1261. /*******************************************************************************
  1262. * Function Name: Cy_CapSense_DpProcessCsdTouchpad
  1263. ****************************************************************************//**
  1264. *
  1265. * Processes status of the CSD Touchpad widget. This includes running
  1266. * Centroid algorithm and updating status in the Data Structure registers.
  1267. *
  1268. * \param ptrWdConfig
  1269. * The pointer to the widget configuration structure.
  1270. *
  1271. * \param context
  1272. * The pointer to the CAPSENSE&trade; context structure \ref cy_stc_capsense_context_t.
  1273. *
  1274. *******************************************************************************/
  1275. void Cy_CapSense_DpProcessCsdTouchpad(
  1276. const cy_stc_capsense_widget_config_t * ptrWdConfig,
  1277. const cy_stc_capsense_context_t * context)
  1278. {
  1279. uint32_t snsIndex;
  1280. uint32_t touchTh;
  1281. uint32_t wdActiveCol = 0uL;
  1282. uint32_t wdActiveRow = 0uL;
  1283. uint8_t * ptrDebounceCnt = ptrWdConfig->ptrDebounceArr;
  1284. cy_stc_capsense_sensor_context_t * ptrSnsCxt = ptrWdConfig->ptrSnsContext;
  1285. cy_stc_capsense_widget_context_t * ptrWdCxt = ptrWdConfig->ptrWdContext;
  1286. uint32_t sensorNumber = ptrWdConfig->numSns;
  1287. uint32_t colNumber = ptrWdConfig->numCols;
  1288. uint32_t rowNumber = ptrWdConfig->numRows;
  1289. cy_stc_capsense_position_t newPosition[CY_CAPSENSE_MAX_CENTROIDS];
  1290. cy_stc_capsense_touch_t newTouch = {&newPosition[0u], CY_CAPSENSE_POSITION_NONE};
  1291. #if (CY_CAPSENSE_DISABLE != CY_CAPSENSE_BALLISTIC_MULTIPLIER_EN)
  1292. cy_stc_capsense_ballistic_delta_t delta;
  1293. #endif
  1294. /* Calculate touch threshold based on current sensor state */
  1295. touchTh = (0u == (ptrWdCxt->status & CY_CAPSENSE_WD_ACTIVE_MASK)) ?
  1296. ((uint32_t)ptrWdCxt->fingerTh + ptrWdCxt->hysteresis) :
  1297. ((uint32_t)ptrWdCxt->fingerTh - ptrWdCxt->hysteresis);
  1298. if (0u < (*ptrDebounceCnt))
  1299. {
  1300. /* Decrement debounce counter */
  1301. (*ptrDebounceCnt)--;
  1302. }
  1303. /* Widget is considered as active if at least one sensor is active on both axes */
  1304. for (snsIndex = colNumber; snsIndex-- > 0uL;)
  1305. {
  1306. ptrSnsCxt->status = (touchTh < ptrSnsCxt->diff) ? CY_CAPSENSE_SNS_TOUCH_STATUS_MASK : 0u;
  1307. wdActiveCol |= ptrSnsCxt->status;
  1308. ptrSnsCxt++;
  1309. }
  1310. for (snsIndex = rowNumber; snsIndex-- > 0uL;)
  1311. {
  1312. ptrSnsCxt->status = (touchTh < ptrSnsCxt->diff) ? CY_CAPSENSE_SNS_TOUCH_STATUS_MASK : 0u;
  1313. wdActiveRow |= ptrSnsCxt->status;
  1314. ptrSnsCxt++;
  1315. }
  1316. /* No touch detected */
  1317. if ((0uL == wdActiveCol) || (0uL == wdActiveRow))
  1318. {
  1319. /* Reset debounce counter */
  1320. (*ptrDebounceCnt) = ptrWdCxt->onDebounce;
  1321. ptrWdCxt->status &= (uint8_t)(~CY_CAPSENSE_WD_ACTIVE_MASK);
  1322. }
  1323. if (0u == (*ptrDebounceCnt))
  1324. {
  1325. /* New touch detected or touch still exists from previous cycle */
  1326. ptrWdCxt->status |= CY_CAPSENSE_WD_ACTIVE_MASK;
  1327. }
  1328. else
  1329. {
  1330. if ((0uL != wdActiveCol) && (0uL != wdActiveRow))
  1331. {
  1332. /* Clear sensor state if activity was detected but debounce was not passed */
  1333. ptrSnsCxt = ptrWdConfig->ptrSnsContext;
  1334. for (snsIndex = sensorNumber; snsIndex-- > 0uL;)
  1335. {
  1336. ptrSnsCxt->status = 0u;
  1337. ptrSnsCxt++;
  1338. }
  1339. }
  1340. }
  1341. /* If widget is still active after debounce run the centroid algorithm */
  1342. if (CY_CAPSENSE_WD_ACTIVE_MASK == (ptrWdCxt->status & CY_CAPSENSE_WD_ACTIVE_MASK))
  1343. {
  1344. /* 3x3 CSD Touchpad processing */
  1345. if (0u != (ptrWdConfig->centroidConfig & CY_CAPSENSE_CENTROID_3X3_MASK))
  1346. {
  1347. /* Centroid processing */
  1348. Cy_CapSense_DpCentroidTouchpad(&newTouch, ptrWdConfig);
  1349. }
  1350. #if (CY_CAPSENSE_DISABLE != CY_CAPSENSE_ADVANCED_CENTROID_5X5_EN)
  1351. /* 5x5 Advanced CSD Touchpad processing */
  1352. if (0u != (ptrWdConfig->centroidConfig & CY_CAPSENSE_CENTROID_5X5_MASK))
  1353. {
  1354. /* Centroid processing */
  1355. Cy_CapSense_DpAdvancedCentroidTouchpad(&newTouch, ptrWdConfig);
  1356. }
  1357. #endif
  1358. }
  1359. #if (CY_CAPSENSE_DISABLE != CY_CAPSENSE_POSITION_FILTER_EN)
  1360. /* Position filtering */
  1361. if (0u != (ptrWdConfig->posFilterConfig & CY_CAPSENSE_POSITION_FILTERS_MASK))
  1362. {
  1363. Cy_CapSense_ProcessPositionFilters(&newTouch, ptrWdConfig);
  1364. }
  1365. #endif
  1366. /* Copy positions into public structure */
  1367. ptrWdCxt->wdTouch.numPosition = newTouch.numPosition;
  1368. if (CY_CAPSENSE_POSITION_MULTIPLE != ptrWdCxt->wdTouch.numPosition)
  1369. {
  1370. for (snsIndex = 0u; snsIndex < newTouch.numPosition; snsIndex++)
  1371. {
  1372. ptrWdCxt->wdTouch.ptrPosition[snsIndex] = newTouch.ptrPosition[snsIndex];
  1373. }
  1374. }
  1375. #if (CY_CAPSENSE_DISABLE != CY_CAPSENSE_BALLISTIC_MULTIPLIER_EN)
  1376. /* Ballistic Multiplier Filtering */
  1377. if (0u != (ptrWdConfig->centroidConfig & CY_CAPSENSE_CENTROID_BALLISTIC_MASK))
  1378. {
  1379. Cy_CapSense_BallisticMultiplier_Lib(&ptrWdConfig->ballisticConfig,
  1380. &ptrWdCxt->wdTouch,
  1381. &delta,
  1382. context->ptrCommonContext->timestamp,
  1383. ptrWdConfig->ptrBallisticContext);
  1384. ptrWdCxt->xDelta = delta.deltaX;
  1385. ptrWdCxt->yDelta = delta.deltaY;
  1386. }
  1387. #else
  1388. (void) context;
  1389. #endif
  1390. }
  1391. #endif /* (CY_CAPSENSE_DISABLE != CY_CAPSENSE_CSD_TOUCHPAD_EN) */
  1392. #if(CY_CAPSENSE_DISABLE != CY_CAPSENSE_CSX_TOUCHPAD_EN)
  1393. /*******************************************************************************
  1394. * Function Name: Cy_CapSense_DpProcessCsxTouchpad
  1395. ****************************************************************************//**
  1396. *
  1397. * Processes the status of the CSX Touchpad widget.
  1398. * The process involves running the 3x3 centroid algorithm with the
  1399. * tracking of finger id.
  1400. *
  1401. * \param ptrWdConfig
  1402. * The pointer to the widget configuration structure.
  1403. *
  1404. *******************************************************************************/
  1405. void Cy_CapSense_DpProcessCsxTouchpad(
  1406. const cy_stc_capsense_widget_config_t * ptrWdConfig)
  1407. {
  1408. uint32_t touchTh;
  1409. uint32_t snsIndex;
  1410. cy_stc_capsense_sensor_context_t * ptrSnsCxt = ptrWdConfig->ptrSnsContext;
  1411. cy_stc_capsense_widget_context_t * ptrWdCxt = ptrWdConfig->ptrWdContext;
  1412. /* Calculate touch threshold based on current sensor state */
  1413. touchTh = (0u == (ptrWdCxt->status & CY_CAPSENSE_WD_ACTIVE_MASK)) ?
  1414. ((uint32_t)ptrWdCxt->fingerTh + ptrWdCxt->hysteresis) :
  1415. ((uint32_t)ptrWdCxt->fingerTh - ptrWdCxt->hysteresis);
  1416. for (snsIndex = 0u; snsIndex < ptrWdConfig->numSns; snsIndex++)
  1417. {
  1418. ptrSnsCxt->status = (touchTh < ptrSnsCxt->diff) ? CY_CAPSENSE_SNS_TOUCH_STATUS_MASK : 0u;
  1419. ptrSnsCxt++;
  1420. }
  1421. /* Check whether sensors are active and located all local maxima */
  1422. Cy_CapSense_DpFindLocalMaxDd(ptrWdConfig);
  1423. /* Calculate centroid position for all found local maxima */
  1424. Cy_CapSense_DpCalcTouchPadCentroid(ptrWdConfig);
  1425. /* Identify all touches and assign them correct ID based on historical data */
  1426. Cy_CapSense_DpTouchTracking(ptrWdConfig);
  1427. /* Filter the position data and write it into data structure */
  1428. Cy_CapSense_DpFilterTouchRecord(ptrWdConfig);
  1429. }
  1430. #endif /* (CY_CAPSENSE_DISABLE != CY_CAPSENSE_CSX_TOUCHPAD_EN) */
  1431. #if (CY_CAPSENSE_DISABLE != CY_CAPSENSE_MULTI_FREQUENCY_SCAN_EN)
  1432. /*******************************************************************************
  1433. * Function Name: Cy_CapSense_RunMfsFiltering
  1434. ****************************************************************************//**
  1435. *
  1436. * Selects the median difference signal when the multi-frequency scan is enabled.
  1437. *
  1438. * \param ptrSnsContext
  1439. * The pointer to the widget configuration structure.
  1440. *
  1441. * \param context
  1442. * The pointer to the CAPSENSE&trade; context structure \ref cy_stc_capsense_context_t.
  1443. *
  1444. *******************************************************************************/
  1445. void Cy_CapSense_RunMfsFiltering(
  1446. cy_stc_capsense_sensor_context_t * ptrSnsContext,
  1447. const cy_stc_capsense_context_t * context)
  1448. {
  1449. ptrSnsContext->diff = (uint16_t)Cy_CapSense_FtMedian((uint32_t)ptrSnsContext->diff,
  1450. (uint32_t)ptrSnsContext[CY_CAPSENSE_MFS_CH1_INDEX * context->ptrCommonConfig->numSns].diff,
  1451. (uint32_t)ptrSnsContext[CY_CAPSENSE_MFS_CH2_INDEX * context->ptrCommonConfig->numSns].diff);
  1452. }
  1453. #endif
  1454. #if (CY_CAPSENSE_PLATFORM_BLOCK_FIFTH_GEN)
  1455. #if (CY_CAPSENSE_DISABLE != CY_CAPSENSE_MULTI_PHASE_ENABLED)
  1456. /*******************************************************************************
  1457. * Function Name: Cy_CapSense_ProcessWidgetMptxDeconvolution
  1458. ****************************************************************************//**
  1459. *
  1460. * Performs raw count deconvolution for the specified CSX widget when
  1461. * Multi-phase Tx is enabled.
  1462. *
  1463. * This function decodes raw counts received after scanning into normal view by
  1464. * performing deconvolution algorithm. If the function is called for a widget with
  1465. * disabled Multi-phase Tx, the function returns CY_CAPSENSE_STATUS_BAD_DATA.
  1466. *
  1467. * No need to call this function from application layer since the
  1468. * Cy_CapSense_ProcessAllWidgets() and Cy_CapSense_ProcessWidget() functions calls
  1469. * deconvolution automatically.
  1470. *
  1471. * DAC auto-calibration when enabled performs sensor auto-calibration without
  1472. * performing deconvolution.
  1473. * The deconvolution algorithm for even number of TX electrodes decreases raw count
  1474. * level twice (keeping the signal on the same level).
  1475. *
  1476. * If specific processing is implemented using the Cy_CapSense_ProcessWidgetExt()
  1477. * and Cy_CapSense_ProcessSensorExt() function then a call of this function is
  1478. * required prior doing the specific processing.
  1479. *
  1480. * \param widgetId
  1481. * Specifies the ID number of the widget.
  1482. *
  1483. * \param context
  1484. * The pointer to the CAPSENSE&trade; context structure \ref cy_stc_capsense_context_t.
  1485. *
  1486. * \return
  1487. * Returns the status of the processing operation.
  1488. *
  1489. *******************************************************************************/
  1490. cy_capsense_status_t Cy_CapSense_ProcessWidgetMptxDeconvolution(
  1491. uint32_t widgetId,
  1492. cy_stc_capsense_context_t * context)
  1493. {
  1494. const cy_stc_capsense_widget_config_t * ptrWdCfg;
  1495. uint32_t idx;
  1496. uint32_t sumIdx;
  1497. uint32_t ceIdx;
  1498. uint32_t rotIdx;
  1499. /* Order of multi-TX sequence */
  1500. uint32_t mptxOrderLocal;
  1501. int32_t localBuf[CY_CAPSENSE_MPTX_MAX_ORDER];
  1502. int16_t deconvCoefRot[CY_CAPSENSE_MAX_TX_PATTERN_NUM * 2u];
  1503. int32_t accum;
  1504. uint32_t accumTmp;
  1505. cy_capsense_status_t result = CY_CAPSENSE_STATUS_BAD_DATA;
  1506. uint32_t freqChIndex;
  1507. ptrWdCfg = &context->ptrWdConfig[widgetId];
  1508. mptxOrderLocal = ptrWdCfg->mptxOrder;
  1509. if (mptxOrderLocal >= CY_CAPSENSE_MPTX_MIN_ORDER)
  1510. {
  1511. result = CY_CAPSENSE_STATUS_SUCCESS;
  1512. (void)memcpy(&deconvCoefRot[0], (const void *)&ptrWdCfg->ptrMptxTable->deconvCoef[0u], mptxOrderLocal * CY_CAPSENSE_BYTES_IN_16_BITS);
  1513. (void)memcpy(&deconvCoefRot[mptxOrderLocal], (const void *)&ptrWdCfg->ptrMptxTable->deconvCoef[0u], mptxOrderLocal * CY_CAPSENSE_BYTES_IN_16_BITS);
  1514. for(freqChIndex = 0u; freqChIndex < CY_CAPSENSE_CONFIGURED_FREQ_NUM; freqChIndex++)
  1515. {
  1516. ceIdx = (uint32_t)ptrWdCfg->numRows * ptrWdCfg->numCols;
  1517. while (ceIdx >= mptxOrderLocal)
  1518. {
  1519. ceIdx -= mptxOrderLocal;
  1520. /* Copy vector formed by RX[rxIdx] and TX[txIdx..txIdx+mptxOrderLocal]
  1521. * from sensors to localBuf */
  1522. idx = mptxOrderLocal;
  1523. while (0u != idx--)
  1524. {
  1525. localBuf[idx] = (int32_t)ptrWdCfg->ptrSnsContext[idx + ceIdx +
  1526. (context->ptrCommonConfig->numSns * freqChIndex)].raw;
  1527. }
  1528. /* Multiply vector stored in localBuf by the matrix of deconvolution coefficients. */
  1529. idx = mptxOrderLocal;
  1530. while (0u != idx--)
  1531. {
  1532. accum = 0;
  1533. rotIdx = idx + mptxOrderLocal - 1u;
  1534. sumIdx = mptxOrderLocal;
  1535. while (0u != sumIdx--)
  1536. {
  1537. accum += localBuf[sumIdx] * deconvCoefRot[rotIdx];
  1538. rotIdx--;
  1539. }
  1540. if (0 > accum)
  1541. {
  1542. accumTmp = 0u;
  1543. }
  1544. else
  1545. {
  1546. accumTmp = (uint32_t)accum;
  1547. /* Shift the result in such a way that guarantees no overflow */
  1548. accumTmp >>= CY_CAPSENSE_SCALING_SHIFT;
  1549. if ((uint32_t)UINT16_MAX < accumTmp)
  1550. {
  1551. accumTmp = UINT16_MAX;
  1552. }
  1553. }
  1554. /* Convert the result to unsigned 16 bit and store in the target buffer */
  1555. ptrWdCfg->ptrSnsContext[idx + ceIdx +
  1556. (context->ptrCommonConfig->numSns * freqChIndex)].raw = (uint16_t)accumTmp;
  1557. }
  1558. }
  1559. }
  1560. }
  1561. return result;
  1562. }
  1563. #endif
  1564. /*******************************************************************************
  1565. * Function Name: Cy_CapSense_PreProcessWidget
  1566. ****************************************************************************//**
  1567. *
  1568. * Executes the pre-processing of scan raw data for specified widgets.
  1569. *
  1570. * This function is called prior any other processing function for
  1571. * the fifth CAPSENSE&trade; HW generation. The pre-processing routine implements
  1572. * the following operations:
  1573. * - Executes the CIC2 pre-processing if the filter mode is set to CIC2.
  1574. * - Limits raw count to maximum value.
  1575. * - Executes the raw data inversion for the CSX sensors.
  1576. *
  1577. * No need to call this function from application layer since the
  1578. * Cy_CapSense_ProcessAllWidgets() and Cy_CapSense_ProcessWidget() functions
  1579. * calls it automatically.
  1580. *
  1581. * If specific processing is implemented using the Cy_CapSense_ProcessWidgetExt()
  1582. * and Cy_CapSense_ProcessSensorExt() function then a call of this function is
  1583. * required prior doing the specific processing. If Multi-phase TX is enabled
  1584. * then deconvolution should be executed after call of this function using
  1585. * the Cy_CapSense_ProcessWidgetMptxDeconvolution() function.
  1586. *
  1587. * \param widgetId
  1588. * The widget ID, for which the pre-processing should be executed.
  1589. *
  1590. * \param context
  1591. * The pointer to the CAPSENSE&trade; context structure \ref cy_stc_capsense_context_t.
  1592. *
  1593. *******************************************************************************/
  1594. void Cy_CapSense_PreProcessWidget(
  1595. uint32_t widgetId,
  1596. const cy_stc_capsense_context_t * context)
  1597. {
  1598. /* Adjusts raw count if CIC2 enabled */
  1599. #if (CY_CAPSENSE_DISABLE != CY_CAPSENSE_CIC2_FILTER_EN)
  1600. Cy_CapSense_PreProcessWidgetCic2Raw(widgetId, context);
  1601. #endif
  1602. /* Limit raw count to the maximum possible raw count value */
  1603. Cy_CapSense_PreProcessWidgetLimitRaw(widgetId, context);
  1604. #if (CY_CAPSENSE_ENABLE == CY_CAPSENSE_CSX_EN)
  1605. /* Inverts raw count for CSX widgets */
  1606. if(CY_CAPSENSE_CSX_GROUP == context->ptrWdConfig[widgetId].senseMethod)
  1607. {
  1608. Cy_CapSense_PreProcessWidgetInvertRaw(widgetId, context);
  1609. }
  1610. #endif /* (CY_CAPSENSE_ENABLE == CY_CAPSENSE_CSX_EN) */
  1611. }
  1612. /*******************************************************************************
  1613. * Function Name: Cy_CapSense_PreProcessSensor
  1614. ****************************************************************************//**
  1615. *
  1616. * Executes the pre-processing of scan raw data for specified sensor.
  1617. *
  1618. * This function is called prior any other processing function for
  1619. * the fifth CAPSENSE&trade; HW generation. The pre-processing routine implements
  1620. * the following operations:
  1621. * - Executes the CIC2 pre-processing if the filter mode is set to CIC2.
  1622. * - Limits raw count to maximum value.
  1623. * - Executes the raw data inversion for the CSX sensors.
  1624. *
  1625. * No need to call this function from application layer since the
  1626. * Cy_CapSense_ProcessAllWidgets() and Cy_CapSense_ProcessWidget() functions
  1627. * calls it automatically.
  1628. *
  1629. * If specific processing is implemented using the Cy_CapSense_ProcessWidgetExt()
  1630. * and Cy_CapSense_ProcessSensorExt() function then a call of this function is
  1631. * required prior doing the specific processing. If Multi-phase TX is enabled
  1632. * then deconvolution should be executed after pre-processing of all sensors
  1633. * of the specified widget using the Cy_CapSense_ProcessWidgetMptxDeconvolution()
  1634. * function.
  1635. *
  1636. * \param widgetId
  1637. * The widget ID, for which the pre-processing should be executed.
  1638. *
  1639. * \param sensorId
  1640. * The sensor ID, for which the pre-processing should be executed.
  1641. *
  1642. * \param context
  1643. * The pointer to the CAPSENSE&trade; context structure \ref cy_stc_capsense_context_t.
  1644. *
  1645. *******************************************************************************/
  1646. void Cy_CapSense_PreProcessSensor(
  1647. uint32_t widgetId,
  1648. uint32_t sensorId,
  1649. const cy_stc_capsense_context_t * context)
  1650. {
  1651. /* Adjusts raw count if CIC2 enabled */
  1652. #if (CY_CAPSENSE_DISABLE != CY_CAPSENSE_CIC2_FILTER_EN)
  1653. Cy_CapSense_PreProcessSensorCic2Raw(widgetId, sensorId, context);
  1654. #endif
  1655. /* Limit raw count to the maximum possible raw count value */
  1656. Cy_CapSense_PreProcessSensorLimitRaw(widgetId, sensorId, context);
  1657. #if (CY_CAPSENSE_ENABLE == CY_CAPSENSE_CSX_EN)
  1658. /* Inverts raw count for CSX widgets */
  1659. if(CY_CAPSENSE_CSX_GROUP == context->ptrWdConfig[widgetId].senseMethod)
  1660. {
  1661. Cy_CapSense_PreProcessSensorInvertRaw(widgetId, sensorId, context);
  1662. }
  1663. #endif /* (CY_CAPSENSE_ENABLE == CY_CAPSENSE_CSX_EN) */
  1664. }
  1665. /*******************************************************************************
  1666. * Function Name: Cy_CapSense_PreProcessWidgetLimitRaw
  1667. ****************************************************************************//**
  1668. *
  1669. * This internal function limits the raw data for specified
  1670. * widgets to the maximum possible raw data for the specified CAPSENSE&trade;
  1671. * configuration.
  1672. *
  1673. * \param widgetId
  1674. * The widget ID, for which the pre-processing should be executed.
  1675. *
  1676. * \param context
  1677. * The pointer to the CAPSENSE&trade; context structure \ref cy_stc_capsense_context_t.
  1678. *
  1679. *******************************************************************************/
  1680. void Cy_CapSense_PreProcessWidgetLimitRaw(
  1681. uint32_t widgetId,
  1682. const cy_stc_capsense_context_t * context)
  1683. {
  1684. uint32_t snsIndex;
  1685. const cy_stc_capsense_widget_config_t * ptrWdCfg = &context->ptrWdConfig[widgetId];
  1686. cy_stc_capsense_sensor_context_t * ptrSnsCxt = &ptrWdCfg->ptrSnsContext[0u];
  1687. uint32_t maxCount = ptrWdCfg->ptrWdContext->maxRawCount;
  1688. for(snsIndex = 0u; snsIndex < ptrWdCfg->numSns; snsIndex++)
  1689. {
  1690. #if (CY_CAPSENSE_ENABLE == CY_CAPSENSE_CSD_EN)
  1691. if ((CY_CAPSENSE_CSD_GROUP == ptrWdCfg->senseMethod) && (ptrWdCfg->numCols <= snsIndex))
  1692. {
  1693. maxCount = ptrWdCfg->ptrWdContext->maxRawCountRow;
  1694. }
  1695. #endif /* (CY_CAPSENSE_ENABLE == CY_CAPSENSE_CSD_EN) */
  1696. if((uint32_t)ptrSnsCxt->raw > maxCount)
  1697. {
  1698. ptrSnsCxt->raw = (uint16_t)maxCount;
  1699. }
  1700. ptrSnsCxt++;
  1701. }
  1702. }
  1703. /*******************************************************************************
  1704. * Function Name: Cy_CapSense_PreProcessSensorLimitRaw
  1705. ****************************************************************************//**
  1706. *
  1707. * This internal function limits the raw data for specified
  1708. * widgets to the maximum possible raw data for the specified CAPSENSE&trade;
  1709. * configuration.
  1710. *
  1711. * \param widgetId
  1712. * The widget ID, for which the pre-processing should be executed.
  1713. *
  1714. * \param sensorId
  1715. * The sensor ID, for which the pre-processing should be executed.
  1716. *
  1717. * \param context
  1718. * The pointer to the CAPSENSE&trade; context structure \ref cy_stc_capsense_context_t.
  1719. *
  1720. *******************************************************************************/
  1721. void Cy_CapSense_PreProcessSensorLimitRaw(
  1722. uint32_t widgetId,
  1723. uint32_t sensorId,
  1724. const cy_stc_capsense_context_t * context)
  1725. {
  1726. const cy_stc_capsense_widget_config_t * ptrWdCfg = &context->ptrWdConfig[widgetId];
  1727. cy_stc_capsense_sensor_context_t * ptrSnsCxt = &ptrWdCfg->ptrSnsContext[sensorId];
  1728. uint32_t maxCount = ptrWdCfg->ptrWdContext->maxRawCount;
  1729. #if (CY_CAPSENSE_ENABLE == CY_CAPSENSE_CSD_EN)
  1730. if ((CY_CAPSENSE_CSD_GROUP == ptrWdCfg->senseMethod) && (ptrWdCfg->numCols <= sensorId))
  1731. {
  1732. maxCount = ptrWdCfg->ptrWdContext->maxRawCountRow;
  1733. }
  1734. #endif /* (CY_CAPSENSE_ENABLE == CY_CAPSENSE_CSD_EN) */
  1735. if((uint32_t)ptrSnsCxt->raw > maxCount)
  1736. {
  1737. ptrSnsCxt->raw = (uint16_t)maxCount;
  1738. }
  1739. }
  1740. #if (CY_CAPSENSE_ENABLE == CY_CAPSENSE_CSX_EN)
  1741. /*******************************************************************************
  1742. * Function Name: Cy_CapSense_PreProcessWidgetInvertRaw
  1743. ****************************************************************************//**
  1744. *
  1745. * This internal function executes the raw data inversion for specified
  1746. * widgets. Raw data is inverted relative to the theoretical MAX value.
  1747. *
  1748. * \param widgetId
  1749. * The widget ID, for which the pre-processing should be executed.
  1750. *
  1751. * \param context
  1752. * The pointer to the CAPSENSE&trade; context structure \ref cy_stc_capsense_context_t.
  1753. *
  1754. *******************************************************************************/
  1755. void Cy_CapSense_PreProcessWidgetInvertRaw(
  1756. uint32_t widgetId,
  1757. const cy_stc_capsense_context_t * context)
  1758. {
  1759. uint32_t snsIndex;
  1760. const cy_stc_capsense_widget_config_t * ptrWdCfg = &context->ptrWdConfig[widgetId];
  1761. cy_stc_capsense_sensor_context_t * ptrSnsCxt = &ptrWdCfg->ptrSnsContext[0u];
  1762. uint32_t maxCount = ptrWdCfg->ptrWdContext->maxRawCount;
  1763. for(snsIndex = 0u; snsIndex < ptrWdCfg->numSns; snsIndex++)
  1764. {
  1765. ptrSnsCxt->raw = (uint16_t)(maxCount - ptrSnsCxt->raw);
  1766. ptrSnsCxt++;
  1767. }
  1768. }
  1769. /*******************************************************************************
  1770. * Function Name: Cy_CapSense_PreProcessSensorInvertRaw
  1771. ****************************************************************************//**
  1772. *
  1773. * This internal function executes the raw data inversion for specified
  1774. * sensors. The raw data is inverted relative to the theoretical MAX value.
  1775. *
  1776. * \param widgetId
  1777. * The widget ID, for which the pre-processing should be executed.
  1778. *
  1779. * \param sensorId
  1780. * The sensor ID, for which the pre-processing should be executed.
  1781. *
  1782. * \param context
  1783. * The pointer to the CAPSENSE&trade; context structure \ref cy_stc_capsense_context_t.
  1784. *
  1785. *******************************************************************************/
  1786. void Cy_CapSense_PreProcessSensorInvertRaw(
  1787. uint32_t widgetId,
  1788. uint32_t sensorId,
  1789. const cy_stc_capsense_context_t * context)
  1790. {
  1791. const cy_stc_capsense_widget_config_t * ptrWdCfg = &context->ptrWdConfig[widgetId];
  1792. cy_stc_capsense_sensor_context_t * ptrSnsCxt = &ptrWdCfg->ptrSnsContext[sensorId];
  1793. uint32_t maxCount = ptrWdCfg->ptrWdContext->maxRawCount;
  1794. ptrSnsCxt->raw = (uint16_t)(maxCount - ptrSnsCxt->raw);
  1795. }
  1796. #endif /* (CY_CAPSENSE_ENABLE == CY_CAPSENSE_CSX_EN) */
  1797. #if (CY_CAPSENSE_DISABLE != CY_CAPSENSE_CIC2_FILTER_EN)
  1798. /*******************************************************************************
  1799. * Function Name: Cy_CapSense_PreProcessWidgetCic2Raw
  1800. ****************************************************************************//**
  1801. *
  1802. * This internal function executes the pre-processing
  1803. * of raw data, for specified widgets produced by the fifth CAPSENSE&trade; HW generation with the
  1804. * filter mode set to CIC2.
  1805. *
  1806. * \param widgetId
  1807. * The widget ID, for which the pre-processing should be executed.
  1808. *
  1809. * \param context
  1810. * The pointer to the CAPSENSE&trade; context structure \ref cy_stc_capsense_context_t.
  1811. *
  1812. *******************************************************************************/
  1813. void Cy_CapSense_PreProcessWidgetCic2Raw(
  1814. uint32_t widgetId,
  1815. const cy_stc_capsense_context_t * context)
  1816. {
  1817. uint32_t snsIndex;
  1818. uint32_t tmpVal;
  1819. uint32_t clkDivider;
  1820. uint32_t cic2Samples;
  1821. uint32_t cic2Divider;
  1822. #if((CY_CAPSENSE_DISABLE != CY_CAPSENSE_CSD_MATRIX_EN) ||\
  1823. (CY_CAPSENSE_DISABLE != CY_CAPSENSE_CSD_TOUCHPAD_EN))
  1824. uint32_t cic2SamplesRow;
  1825. uint32_t cic2DividerRow;
  1826. uint32_t twoDimWidget = 0u;
  1827. #endif
  1828. const cy_stc_capsense_widget_config_t * ptrWdCfg = &context->ptrWdConfig[widgetId];
  1829. cy_stc_capsense_sensor_context_t * ptrSnsCxt = &ptrWdCfg->ptrSnsContext[0u];
  1830. clkDivider = context->ptrWdContext[widgetId].snsClk;
  1831. cic2Samples = ((clkDivider * ptrWdCfg->ptrWdContext->numSubConversions /
  1832. ptrWdCfg->ptrWdContext->cicRate) - 1u) * ptrWdCfg->numChopCycles;
  1833. cic2Divider = Cy_CapSense_GetCIC2HwDivider(cic2Samples);
  1834. #if((CY_CAPSENSE_DISABLE != CY_CAPSENSE_CSD_MATRIX_EN) ||\
  1835. (CY_CAPSENSE_DISABLE != CY_CAPSENSE_CSD_TOUCHPAD_EN))
  1836. if ((CY_CAPSENSE_CSD_GROUP == ptrWdCfg->senseMethod) &&
  1837. (((uint8_t)CY_CAPSENSE_WD_MATRIX_BUTTON_E == ptrWdCfg->wdType) ||
  1838. ((uint8_t)CY_CAPSENSE_WD_TOUCHPAD_E == ptrWdCfg->wdType)))
  1839. {
  1840. twoDimWidget = 1u;
  1841. clkDivider = context->ptrWdContext[widgetId].rowSnsClk;
  1842. cic2SamplesRow = ((clkDivider * ptrWdCfg->ptrWdContext->numSubConversions /
  1843. ptrWdCfg->ptrWdContext->cicRate) - 1u) * ptrWdCfg->numChopCycles;
  1844. cic2DividerRow = Cy_CapSense_GetCIC2HwDivider(cic2SamplesRow);
  1845. }
  1846. #endif
  1847. for(snsIndex = 0u; snsIndex < ptrWdCfg->numSns; snsIndex++)
  1848. {
  1849. #if((CY_CAPSENSE_DISABLE != CY_CAPSENSE_CSD_MATRIX_EN) ||\
  1850. (CY_CAPSENSE_DISABLE != CY_CAPSENSE_CSD_TOUCHPAD_EN))
  1851. if ((0u != twoDimWidget) && (ptrWdCfg->numCols <= snsIndex))
  1852. {
  1853. tmpVal = ((uint32_t)ptrSnsCxt->raw * cic2DividerRow) / cic2SamplesRow;
  1854. }
  1855. else
  1856. {
  1857. tmpVal = ((uint32_t)ptrSnsCxt->raw * cic2Divider) / cic2Samples;
  1858. }
  1859. #else
  1860. tmpVal = ((uint32_t)ptrSnsCxt->raw * cic2Divider) / cic2Samples;
  1861. #endif
  1862. if (tmpVal > CY_CAPSENSE_16_BIT_MASK)
  1863. {
  1864. tmpVal = CY_CAPSENSE_16_BIT_MASK;
  1865. }
  1866. ptrSnsCxt->raw = (uint16_t)tmpVal;
  1867. ptrSnsCxt++;
  1868. }
  1869. }
  1870. /*******************************************************************************
  1871. * Function Name: Cy_CapSense_PreProcessSensorCic2Raw
  1872. ****************************************************************************//**
  1873. *
  1874. * This internal function executes the pre-processing of raw data
  1875. * produced by the fifth CAPSENSE&trade; HW generation with the filter mode set
  1876. * to CIC2.
  1877. *
  1878. * \param widgetId
  1879. * The widget ID, for which the pre-processing should be executed.
  1880. *
  1881. * \param sensorId
  1882. * The sensor ID, for which the pre-processing should be executed.
  1883. *
  1884. * \param context
  1885. * The pointer to the CAPSENSE&trade; context structure \ref cy_stc_capsense_context_t.
  1886. *
  1887. *******************************************************************************/
  1888. void Cy_CapSense_PreProcessSensorCic2Raw(
  1889. uint32_t widgetId,
  1890. uint32_t sensorId,
  1891. const cy_stc_capsense_context_t * context)
  1892. {
  1893. uint32_t tmpVal;
  1894. uint32_t clkDivider;
  1895. uint32_t cic2Samples;
  1896. uint32_t cic2Divider;
  1897. const cy_stc_capsense_widget_config_t * ptrWdCfg = &context->ptrWdConfig[widgetId];
  1898. cy_stc_capsense_sensor_context_t * ptrSnsCxt = &ptrWdCfg->ptrSnsContext[sensorId];
  1899. clkDivider = context->ptrWdContext[widgetId].snsClk;
  1900. if ((CY_CAPSENSE_CSD_GROUP == ptrWdCfg->senseMethod) && (ptrWdCfg->numCols <= sensorId))
  1901. {
  1902. clkDivider = context->ptrWdContext[widgetId].rowSnsClk;
  1903. }
  1904. cic2Samples = ((clkDivider * ptrWdCfg->ptrWdContext->numSubConversions /
  1905. ptrWdCfg->ptrWdContext->cicRate) - 1u) * ptrWdCfg->numChopCycles;
  1906. cic2Divider = Cy_CapSense_GetCIC2HwDivider(cic2Samples);
  1907. tmpVal = ((uint32_t)ptrSnsCxt->raw * cic2Divider) / cic2Samples;
  1908. if (tmpVal > CY_CAPSENSE_16_BIT_MASK)
  1909. {
  1910. tmpVal = CY_CAPSENSE_16_BIT_MASK;
  1911. }
  1912. ptrSnsCxt->raw = (uint16_t)tmpVal;
  1913. }
  1914. /*******************************************************************************
  1915. * Function Name: Cy_CapSense_GetCIC2HwDivider
  1916. ****************************************************************************//**
  1917. *
  1918. * This internal function determines the value of the divider that will be
  1919. * applied to the data, accumulated by the CIC2 HW for the specified number of
  1920. * samples.
  1921. *
  1922. * \param cic2Samples
  1923. * The number of CIC2 samples for the specified sensing parameters. This value
  1924. * can be obtained by using the Cy_CapSense_GetCIC2SamplesNum function.
  1925. *
  1926. * \return
  1927. * The CIC2 HW divider value.
  1928. *
  1929. *******************************************************************************/
  1930. uint32_t Cy_CapSense_GetCIC2HwDivider(
  1931. uint32_t cic2Samples)
  1932. {
  1933. uint32_t cic2Divider;
  1934. if(cic2Samples > CY_CAPSENSE_CIC2_DIVIDER_8)
  1935. {
  1936. cic2Divider = CY_CAPSENSE_CIC2_DIVIDER_16;
  1937. }
  1938. else if(cic2Samples > CY_CAPSENSE_CIC2_DIVIDER_4)
  1939. {
  1940. cic2Divider = CY_CAPSENSE_CIC2_DIVIDER_8;
  1941. }
  1942. else if(cic2Samples > CY_CAPSENSE_CIC2_DIVIDER_2)
  1943. {
  1944. cic2Divider = CY_CAPSENSE_CIC2_DIVIDER_4;
  1945. }
  1946. else if(cic2Samples > CY_CAPSENSE_CIC2_DIVIDER_1)
  1947. {
  1948. cic2Divider = CY_CAPSENSE_CIC2_DIVIDER_2;
  1949. }
  1950. else
  1951. {
  1952. cic2Divider = CY_CAPSENSE_CIC2_DIVIDER_1;
  1953. }
  1954. return(cic2Divider);
  1955. }
  1956. #endif /* #if (CY_CAPSENSE_DISABLE != CY_CAPSENSE_CIC2_FILTER_EN) */
  1957. #if (CY_CAPSENSE_DISABLE != CY_CAPSENSE_MULTI_FREQUENCY_WIDGET_EN)
  1958. /*******************************************************************************
  1959. * Function Name: Cy_CapSense_RunMfsMedian
  1960. ****************************************************************************//**
  1961. *
  1962. * Applies the median filter to the specified multi-frequency widget and updates
  1963. * the specified widget diff counts.
  1964. *
  1965. * This function is a low-level function and is called automatically by
  1966. * high-level processing functions like Cy_CapSense_ProcessWidget()
  1967. * and Cy_CapSense_ProcessAllWidgets().
  1968. *
  1969. * It is not recommended to use this function directly on application level.
  1970. *
  1971. * The function applies the median filter to diff count of each sensor of the
  1972. * specified widget (with enabled multi-frequency feature) and update the diff
  1973. * count of the specified main widget.
  1974. *
  1975. * This function is needed to implement customer-specific use cases.
  1976. *
  1977. * \note
  1978. * This function is available only for the fifth-generation CAPSENSE&trade;.
  1979. *
  1980. * \param widgetId
  1981. * Specifies the ID number of the widget. A macro for the widget ID can be found
  1982. * in the cycfg_capsense.h file defined as CY_CAPSENSE_<WIDGET_NAME>_WDGT_ID.
  1983. *
  1984. * \param context
  1985. * The pointer to the CAPSENSE&trade; context structure \ref cy_stc_capsense_context_t.
  1986. *
  1987. * \return
  1988. * Returns the status of the widget processing:
  1989. * - CY_CAPSENSE_STATUS_SUCCESS - The operation is successfully completed
  1990. * - CY_CAPSENSE_STATUS_BAD_PARAM - The input parameter is invalid
  1991. * either widgetId is not valid or multi-frequency is not enabled for this widget
  1992. * or the specified widgetId is derivative of the main widget.
  1993. *
  1994. *******************************************************************************/
  1995. cy_capsense_status_t Cy_CapSense_RunMfsMedian(
  1996. uint32_t widgetId,
  1997. const cy_stc_capsense_context_t * context)
  1998. {
  1999. cy_capsense_status_t result = CY_CAPSENSE_STATUS_BAD_PARAM;
  2000. uint32_t snsIndex;
  2001. const cy_stc_capsense_widget_config_t * ptrWdCfg;
  2002. cy_stc_capsense_sensor_context_t * ptrSnsCxt0;
  2003. cy_stc_capsense_sensor_context_t * ptrSnsCxt1;
  2004. cy_stc_capsense_sensor_context_t * ptrSnsCxt2;
  2005. /* Check parameter validity */
  2006. if (widgetId < context->ptrCommonConfig->numWd)
  2007. {
  2008. ptrWdCfg = &context->ptrWdConfig[widgetId];
  2009. ptrSnsCxt0 = ptrWdCfg[CY_CAPSENSE_MFS_CH0_INDEX].ptrSnsContext;
  2010. ptrSnsCxt1 = ptrWdCfg[CY_CAPSENSE_MFS_CH1_INDEX].ptrSnsContext;
  2011. ptrSnsCxt2 = ptrWdCfg[CY_CAPSENSE_MFS_CH2_INDEX].ptrSnsContext;
  2012. if (((ptrWdCfg->mfsConfig & CY_CAPSENSE_MFS_EN_MASK) != 0u) &&
  2013. ((ptrWdCfg->mfsConfig & CY_CAPSENSE_MFS_WIDGET_FREQ_ALL_CH_MASK) == 0u))
  2014. {
  2015. /* Calculate median */
  2016. for (snsIndex = ptrWdCfg->numSns; snsIndex-- > 0u;)
  2017. {
  2018. ptrSnsCxt0->diff = (uint16_t)Cy_CapSense_FtMedian(ptrSnsCxt0->diff,
  2019. ptrSnsCxt1->diff,
  2020. ptrSnsCxt2->diff);
  2021. ptrSnsCxt0++;
  2022. ptrSnsCxt1++;
  2023. ptrSnsCxt2++;
  2024. }
  2025. result = CY_CAPSENSE_STATUS_SUCCESS;
  2026. }
  2027. }
  2028. return (result);
  2029. }
  2030. #endif
  2031. #endif /* CY_CAPSENSE_PLATFORM_BLOCK_FIFTH_GEN */
  2032. #endif /* (defined(CY_IP_MXCSDV2) || defined(CY_IP_M0S8CSDV2) || defined(CY_IP_M0S8MSCV3)) */
  2033. /* [] END OF FILE */