|
| 1 | +/* |
| 2 | +* Copyright (c) 2020 - 2025 Renesas Electronics Corporation and/or its affiliates |
| 3 | +* |
| 4 | +* SPDX-License-Identifier: BSD-3-Clause |
| 5 | +*/ |
| 6 | + |
| 7 | +/*******************************************************************************************************************//** |
| 8 | + * @ingroup RENESAS_CAPTOUCH_INTERFACES |
| 9 | + * @defgroup CTSU_API CTSU Interface |
| 10 | + * @brief Interface for Capacitive Touch Sensing Unit (CTSU) functions. |
| 11 | + * |
| 12 | + * @section CTSU_API_Summary Summary |
| 13 | + * The CTSU interface provides CTSU functionality. |
| 14 | + * |
| 15 | + * |
| 16 | + * @{ |
| 17 | + **********************************************************************************************************************/ |
| 18 | + |
| 19 | +#ifndef R_CTSU_API_H |
| 20 | +#define R_CTSU_API_H |
| 21 | + |
| 22 | +/*********************************************************************************************************************** |
| 23 | + * Includes |
| 24 | + **********************************************************************************************************************/ |
| 25 | + |
| 26 | +/* Register definitions, common services and error codes. */ |
| 27 | +#include "bsp_api.h" |
| 28 | +#include "r_transfer_api.h" |
| 29 | +#include "r_adc_api.h" |
| 30 | + |
| 31 | +/* Common macro for FSP header files. There is also a corresponding FSP_FOOTER macro at the end of this file. */ |
| 32 | +FSP_HEADER |
| 33 | + |
| 34 | +/********************************************************************************************************************** |
| 35 | + * Macro definitions |
| 36 | + **********************************************************************************************************************/ |
| 37 | +#define CTSU_COUNT_MAX (0xFFFF) |
| 38 | + |
| 39 | +#define CTSU_TARGET_VALUE_CONFIG_SUPPORT (1) |
| 40 | + |
| 41 | +/********************************************************************************************************************** |
| 42 | + * Typedef definitions |
| 43 | + **********************************************************************************************************************/ |
| 44 | + |
| 45 | +/** CTSU Events for callback function */ |
| 46 | +typedef enum e_ctsu_event |
| 47 | +{ |
| 48 | + CTSU_EVENT_SCAN_COMPLETE = 0x00, ///< Normal end |
| 49 | + CTSU_EVENT_OVERFLOW = 0x01, ///< Sensor counter overflow (CTSUST.CTSUSOVF set) |
| 50 | + CTSU_EVENT_ICOMP = 0x02, ///< Abnormal TSCAP voltage (CTSUERRS.CTSUICOMP set) |
| 51 | + CTSU_EVENT_ICOMP1 = 0x04 ///< Abnormal sensor current (CTSUSR.ICOMP1 set) |
| 52 | +} ctsu_event_t; |
| 53 | + |
| 54 | +/** CTSU Scan Start Trigger Select */ |
| 55 | +typedef enum e_ctsu_cap |
| 56 | +{ |
| 57 | + CTSU_CAP_SOFTWARE, ///< Scan start by software trigger |
| 58 | + CTSU_CAP_EXTERNAL ///< Scan start by external trigger |
| 59 | +} ctsu_cap_t; |
| 60 | + |
| 61 | +/** CTSU Transmission Power Supply Select */ |
| 62 | +typedef enum e_ctsu_txvsel |
| 63 | +{ |
| 64 | + CTSU_TXVSEL_VCC, ///< VCC selected |
| 65 | + CTSU_TXVSEL_INTERNAL_POWER ///< Internal logic power supply selected |
| 66 | +} ctsu_txvsel_t; |
| 67 | + |
| 68 | +/** CTSU Transmission Power Supply Select 2 (CTSU2 Only) */ |
| 69 | +typedef enum e_ctsu_txvsel2 |
| 70 | +{ |
| 71 | + CTSU_TXVSEL_MODE, ///< Follow TXVSEL setting |
| 72 | + CTSU_TXVSEL_VCC_PRIVATE, ///< VCC private selected |
| 73 | +} ctsu_txvsel2_t; |
| 74 | + |
| 75 | +/** CTSU Power Supply Capacity Adjustment (CTSU Only) */ |
| 76 | +typedef enum e_ctsu_atune1 |
| 77 | +{ |
| 78 | + CTSU_ATUNE1_NORMAL, ///< Normal output (40uA) |
| 79 | + CTSU_ATUNE1_HIGH ///< High-current output (80uA) |
| 80 | +} ctsu_atune1_t; |
| 81 | + |
| 82 | +/** CTSU Power Supply Capacity Adjustment (CTSU2 Only) */ |
| 83 | +typedef enum e_ctsu_atune12 |
| 84 | +{ |
| 85 | + CTSU_ATUNE12_80UA, ///< High-current output (80uA) |
| 86 | + CTSU_ATUNE12_40UA, ///< Normal output (40uA) |
| 87 | + CTSU_ATUNE12_20UA, ///< Low-current output (20uA) |
| 88 | + CTSU_ATUNE12_160UA ///< Very high-current output (160uA) |
| 89 | +} ctsu_atune12_t; |
| 90 | + |
| 91 | +/** CTSU Measurement Mode Select */ |
| 92 | +typedef enum e_ctsu_mode |
| 93 | +{ |
| 94 | + CTSU_MODE_SELF_MULTI_SCAN = 1, ///< Self-capacitance multi scan mode |
| 95 | + CTSU_MODE_MUTUAL_FULL_SCAN = 3, ///< Mutual capacitance full scan mode |
| 96 | + CTSU_MODE_MUTUAL_CFC_SCAN = 7, ///< Mutual capacitance cfc scan mode (CTSU2 Only) |
| 97 | + CTSU_MODE_CURRENT_SCAN = 9, ///< Current scan mode (CTSU2 Only) |
| 98 | + CTSU_MODE_CORRECTION_SCAN = 17, ///< Correction scan mode (CTSU2 Only) |
| 99 | + CTSU_MODE_DIAGNOSIS_SCAN = 33 ///< Diagnosis scan mode |
| 100 | +} ctsu_md_t; |
| 101 | + |
| 102 | +/** CTSU Non-Measured Channel Output Select (CTSU2 Only) */ |
| 103 | +typedef enum e_ctsu_posel |
| 104 | +{ |
| 105 | + CTSU_POSEL_LOW_GPIO, ///< Output low through GPIO |
| 106 | + CTSU_POSEL_HI_Z, ///< Hi-Z |
| 107 | + CTSU_POSEL_LOW, ///< Setting prohibited |
| 108 | + CTSU_POSEL_SAME_PULSE ///< Same phase pulse output as transmission channel through the power setting by the TXVSEL[1:0] bits |
| 109 | +} ctsu_posel_t; |
| 110 | + |
| 111 | +/** CTSU Spectrum Diffusion Frequency Division Setting (CTSU Only) */ |
| 112 | +typedef enum e_ctsu_ssdiv |
| 113 | +{ |
| 114 | + CTSU_SSDIV_4000, ///< 4.00 <= Base clock frequency (MHz) |
| 115 | + CTSU_SSDIV_2000, ///< 2.00 <= Base clock frequency (MHz) < 4.00 |
| 116 | + CTSU_SSDIV_1330, ///< 1.33 <= Base clock frequency (MHz) < 2.00 |
| 117 | + CTSU_SSDIV_1000, ///< 1.00 <= Base clock frequency (MHz) < 1.33 |
| 118 | + CTSU_SSDIV_0800, ///< 0.80 <= Base clock frequency (MHz) < 1.00 |
| 119 | + CTSU_SSDIV_0670, ///< 0.67 <= Base clock frequency (MHz) < 0.80 |
| 120 | + CTSU_SSDIV_0570, ///< 0.57 <= Base clock frequency (MHz) < 0.67 |
| 121 | + CTSU_SSDIV_0500, ///< 0.50 <= Base clock frequency (MHz) < 0.57 |
| 122 | + CTSU_SSDIV_0440, ///< 0.44 <= Base clock frequency (MHz) < 0.50 |
| 123 | + CTSU_SSDIV_0400, ///< 0.40 <= Base clock frequency (MHz) < 0.44 |
| 124 | + CTSU_SSDIV_0360, ///< 0.36 <= Base clock frequency (MHz) < 0.40 |
| 125 | + CTSU_SSDIV_0330, ///< 0.33 <= Base clock frequency (MHz) < 0.36 |
| 126 | + CTSU_SSDIV_0310, ///< 0.31 <= Base clock frequency (MHz) < 0.33 |
| 127 | + CTSU_SSDIV_0290, ///< 0.29 <= Base clock frequency (MHz) < 0.31 |
| 128 | + CTSU_SSDIV_0270, ///< 0.27 <= Base clock frequency (MHz) < 0.29 |
| 129 | + CTSU_SSDIV_0000 ///< 0.00 <= Base clock frequency (MHz) < 0.27 |
| 130 | +} ctsu_ssdiv_t; |
| 131 | + |
| 132 | +/** CTSU select data type for slect data get */ |
| 133 | +typedef enum e_ctsu_specific_data_type |
| 134 | +{ |
| 135 | + CTSU_SPECIFIC_RAW_DATA, |
| 136 | + CTSU_SPECIFIC_CCO_CORRECTION_DATA, |
| 137 | + CTSU_SPECIFIC_CORRECTION_DATA, |
| 138 | + CTSU_SPECIFIC_SELECTED_FREQ, |
| 139 | +} ctsu_specific_data_type_t; |
| 140 | + |
| 141 | +/** Callback function parameter data */ |
| 142 | +typedef struct st_ctsu_callback_args |
| 143 | +{ |
| 144 | + ctsu_event_t event; ///< The event can be used to identify what caused the callback. |
| 145 | + void const * p_context; ///< Placeholder for user data. Set in ctsu_api_t::open function in ::ctsu_cfg_t. |
| 146 | +} ctsu_callback_args_t; |
| 147 | + |
| 148 | +/** CTSU Control block. Allocate an instance specific control block to pass into the API calls. |
| 149 | + */ |
| 150 | +typedef void ctsu_ctrl_t; |
| 151 | + |
| 152 | +/** CTSU Configuration parameters. */ |
| 153 | +/** Element Configuration */ |
| 154 | +typedef struct st_ctsu_element |
| 155 | +{ |
| 156 | + ctsu_ssdiv_t ssdiv; ///< CTSU Spectrum Diffusion Frequency Division Setting (CTSU Only) |
| 157 | + uint16_t so; ///< CTSU Sensor Offset Adjustment |
| 158 | + uint8_t snum; ///< CTSU Measurement Count Setting |
| 159 | + uint8_t sdpa; ///< CTSU Base Clock Setting |
| 160 | +} ctsu_element_cfg_t; |
| 161 | + |
| 162 | +/** Configuration of each automatic judgement button */ |
| 163 | +typedef struct st_ctsu_auto_button_cfg |
| 164 | +{ |
| 165 | + uint8_t elem_index; ///< Element number used by this button for automatic judgement. |
| 166 | + uint16_t threshold; ///< Touch/non-touch judgement threshold for automatic judgement. |
| 167 | + uint16_t hysteresis; ///< Threshold hysteresis for chattering prevention for automatic judgement. |
| 168 | +} ctsu_auto_button_cfg_t; |
| 169 | + |
| 170 | +/** User configuration structure, used in open function */ |
| 171 | +typedef struct st_ctsu_cfg |
| 172 | +{ |
| 173 | + ctsu_cap_t cap; ///< CTSU Scan Start Trigger Select |
| 174 | + ctsu_txvsel_t txvsel; ///< CTSU Transmission Power Supply Select |
| 175 | + ctsu_txvsel2_t txvsel2; ///< CTSU Transmission Power Supply Select 2 (CTSU2 Only) |
| 176 | + ctsu_atune1_t atune1; ///< CTSU Power Supply Capacity Adjustment (CTSU Only) |
| 177 | + ctsu_atune12_t atune12; ///< CTSU Power Supply Capacity Adjustment (CTSU2 Only) |
| 178 | + ctsu_md_t md; ///< CTSU Measurement Mode Select |
| 179 | + ctsu_posel_t posel; ///< CTSU Non-Measured Channel Output Select (CTSU2 Only) |
| 180 | + uint8_t tsod; ///< TS all terminal output control for multi electrode scan |
| 181 | + uint8_t mec_ts; ///< TS number used when using the MEC function |
| 182 | + uint8_t mec_shield_ts; ///< TS number of active shield used when using MEC function |
| 183 | + uint8_t tlot; ///< Number of consecutive judgements exceeding the threshold L for automatic judgement |
| 184 | + uint8_t thot; ///< Number of consecutive judgements exceeding the threshold H for automatic judgement |
| 185 | + uint8_t jc; ///< Judgement condition for automatic judgement |
| 186 | + uint8_t ajmmat; ///< Measured value moving average number of times for automatic judgement |
| 187 | + uint8_t ajbmat; ///< Average number of baselines for automatic judgement |
| 188 | + uint8_t mtucfen; ///< Mutual capacity operation for automatic judgement |
| 189 | + uint8_t ajfen; ///< Automatic judgement function enabled for automatic judgement |
| 190 | + uint8_t autojudge_monitor_num; ///< Method number for QE monitor for automatic judgement |
| 191 | + uint8_t majirimd; ///< JMM or VMM for automatic judgement |
| 192 | + uint8_t ctsuchac0; ///< TS00-TS07 enable mask |
| 193 | + uint8_t ctsuchac1; ///< TS08-TS15 enable mask |
| 194 | + uint8_t ctsuchac2; ///< TS16-TS23 enable mask |
| 195 | + uint8_t ctsuchac3; ///< TS24-TS31 enable mask |
| 196 | + uint8_t ctsuchac4; ///< TS32-TS39 enable mask |
| 197 | + uint8_t ctsuchtrc0; ///< TS00-TS07 mutual-tx mask |
| 198 | + uint8_t ctsuchtrc1; ///< TS08-TS15 mutual-tx mask |
| 199 | + uint8_t ctsuchtrc2; ///< TS16-TS23 mutual-tx mask |
| 200 | + uint8_t ctsuchtrc3; ///< TS24-TS31 mutual-tx mask |
| 201 | + uint8_t ctsuchtrc4; ///< TS32-TS39 mutual-tx mask |
| 202 | + ctsu_element_cfg_t const * p_elements; ///< Pointer to elements configuration array |
| 203 | + uint8_t num_rx; ///< Number of receive terminals |
| 204 | + uint8_t num_tx; ///< Number of transmit terminals |
| 205 | + uint16_t num_moving_average; ///< Number of moving average for measurement data |
| 206 | + bool tunning_enable; ///< Initial offset tuning flag |
| 207 | + void (* p_callback)(ctsu_callback_args_t * p_args); ///< Callback provided when CTSUFN ISR occurs. |
| 208 | + transfer_instance_t const * p_transfer_tx; ///< DTC instance for transmit at CTSUWR. Set to NULL if unused. |
| 209 | + transfer_instance_t const * p_transfer_rx; ///< DTC instance for receive at CTSURD. Set to NULL if unused. |
| 210 | + adc_instance_t const * p_adc_instance; ///< ADC instance for temperature correction. |
| 211 | + IRQn_Type write_irq; ///< CTSU_CTSUWR interrupt vector |
| 212 | + IRQn_Type read_irq; ///< CTSU_CTSURD interrupt vector |
| 213 | + IRQn_Type end_irq; ///< CTSU_CTSUFN interrupt vector |
| 214 | + void const * p_context; ///< User defined context passed into callback function. |
| 215 | + void const * p_extend; ///< Pointer to extended configuration by instance of interface. |
| 216 | + uint16_t tuning_self_target_value; ///< Target self value for initial offset tuning |
| 217 | + uint16_t tuning_mutual_target_value; ///< Target mutual value for initial offset tuning |
| 218 | + ctsu_auto_button_cfg_t const * p_ctsu_auto_buttons; ///< Pointer to array of automatic judgement button configuration. |
| 219 | + uint8_t majority_mode; ///< Software majority mode selection |
| 220 | +} ctsu_cfg_t; |
| 221 | + |
| 222 | +/** Functions implemented at the HAL layer will follow this API. */ |
| 223 | +typedef struct st_ctsu_api |
| 224 | +{ |
| 225 | + /** Open driver. |
| 226 | + * |
| 227 | + * @param[in] p_ctrl Pointer to control structure. |
| 228 | + * @param[in] p_cfg Pointer to pin configuration structure. |
| 229 | + */ |
| 230 | + fsp_err_t (* open)(ctsu_ctrl_t * const p_ctrl, ctsu_cfg_t const * const p_cfg); |
| 231 | + |
| 232 | + /** Scan start. |
| 233 | + * |
| 234 | + * @param[in] p_ctrl Pointer to control structure. |
| 235 | + */ |
| 236 | + fsp_err_t (* scanStart)(ctsu_ctrl_t * const p_ctrl); |
| 237 | + |
| 238 | + /** Data get. |
| 239 | + * |
| 240 | + * @param[in] p_ctrl Pointer to control structure. |
| 241 | + * @param[out] p_data Pointer to get data array. |
| 242 | + */ |
| 243 | + fsp_err_t (* dataGet)(ctsu_ctrl_t * const p_ctrl, uint16_t * p_data); |
| 244 | + |
| 245 | + /** ScanStop. |
| 246 | + * |
| 247 | + * @param[in] p_ctrl Pointer to control structure. |
| 248 | + */ |
| 249 | + fsp_err_t (* scanStop)(ctsu_ctrl_t * const p_ctrl); |
| 250 | + |
| 251 | + /** Diagnosis. |
| 252 | + * |
| 253 | + * @param[in] p_ctrl Pointer to control structure. |
| 254 | + */ |
| 255 | + fsp_err_t (* diagnosis)(ctsu_ctrl_t * const p_ctrl); |
| 256 | + |
| 257 | + /** Specify callback function and optional context pointer and working memory pointer. |
| 258 | + * |
| 259 | + * @param[in] p_ctrl Pointer to the CTSU control block. |
| 260 | + * @param[in] p_callback Callback function |
| 261 | + * @param[in] p_context Pointer to send to callback function |
| 262 | + * @param[in] p_working_memory Pointer to volatile memory where callback structure can be allocated. |
| 263 | + * Callback arguments allocated here are only valid during the callback. |
| 264 | + */ |
| 265 | + fsp_err_t (* callbackSet)(ctsu_ctrl_t * const p_ctrl, void (* p_callback)(ctsu_callback_args_t *), |
| 266 | + void const * const p_context, ctsu_callback_args_t * const p_callback_memory); |
| 267 | + |
| 268 | + /** Close driver. |
| 269 | + * |
| 270 | + * @param[in] p_ctrl Pointer to control structure. |
| 271 | + */ |
| 272 | + fsp_err_t (* close)(ctsu_ctrl_t * const p_ctrl); |
| 273 | + |
| 274 | + /** Specific Data get. |
| 275 | + * |
| 276 | + * @param[in] p_ctrl Pointer to control structure. |
| 277 | + * @param[out] p_specific_data Pointer to get specific data array. |
| 278 | + * @param[in] specific_data_type Specific data type |
| 279 | + */ |
| 280 | + fsp_err_t (* specificDataGet)(ctsu_ctrl_t * const p_ctrl, uint16_t * p_specific_data, |
| 281 | + ctsu_specific_data_type_t specific_data_type); |
| 282 | + |
| 283 | + /** Data Insert. |
| 284 | + * |
| 285 | + * @param[in] p_ctrl Pointer to control structure. |
| 286 | + * @param[in] p_insert_data Pointer to insert data. |
| 287 | + */ |
| 288 | + fsp_err_t (* dataInsert)(ctsu_ctrl_t * const p_ctrl, uint16_t * p_insert_data); |
| 289 | + |
| 290 | + /** Initialize automatic judgement and get button status. |
| 291 | + * |
| 292 | + * @param[in] p_ctrl Pointer to control structure. |
| 293 | + * @param[out] p_button_status Pointer to get button status array. |
| 294 | + */ |
| 295 | + fsp_err_t (* autoJudgementDataGet)(ctsu_ctrl_t * const p_ctrl, uint64_t * p_button_status); |
| 296 | + |
| 297 | + /** Adjust the offset value to tune the sensor. |
| 298 | + * |
| 299 | + * @param[in] p_ctrl Pointer to control structure. |
| 300 | + */ |
| 301 | + fsp_err_t (* offsetTuning)(ctsu_ctrl_t * const p_ctrl); |
| 302 | +} ctsu_api_t; |
| 303 | + |
| 304 | +/** This structure encompasses everything that is needed to use an instance of this interface. */ |
| 305 | +typedef struct st_ctsu_instance |
| 306 | +{ |
| 307 | + ctsu_ctrl_t * p_ctrl; ///< Pointer to the control structure for this instance |
| 308 | + ctsu_cfg_t const * p_cfg; ///< Pointer to the configuration structure for this instance |
| 309 | + ctsu_api_t const * p_api; ///< Pointer to the API structure for this instance |
| 310 | +} ctsu_instance_t; |
| 311 | + |
| 312 | +/** Common macro for FSP header files. There is also a corresponding FSP_HEADER macro at the top of this file. */ |
| 313 | +FSP_FOOTER |
| 314 | + |
| 315 | +#endif /* R_CTSU_API_H */ |
| 316 | + |
| 317 | +/*******************************************************************************************************************//** |
| 318 | + * @} (end addtogroup CTSU_API) |
| 319 | + **********************************************************************************************************************/ |
0 commit comments