RA Flexible Software Package Documentation  Release v6.5.1

 
CTSU (r_ctsu)

Functions

fsp_err_t R_CTSU_Open (ctsu_ctrl_t *const p_ctrl, ctsu_cfg_t const *const p_cfg)
 Opens and configures the CTSU driver module. Implements ctsu_api_t::open. More...
 
fsp_err_t R_CTSU_ScanStart (ctsu_ctrl_t *const p_ctrl)
 This function should be called each time a periodic timer expires. If initial offset tuning is enabled, The first several calls are used to tuning for the sensors. Before starting the next scan, first get the data with R_CTSU_DataGet(). If a different control block scan should be run, check the scan is complete before executing. Implements ctsu_api_t::scanStart. More...
 
fsp_err_t R_CTSU_DataGet (ctsu_ctrl_t *const p_ctrl, uint16_t *p_data)
 This function gets the sensor values as scanned by the CTSU. If initial offset tuning is enabled, The first several calls are used to tuning for the sensors. This function reads all previously corrected measured values into the specified buffer(p_data). CTSU1: The value passed through sensor CCO correction and moving average. CTSU2 JMM: The value passed through sensor CCO correction and moving average. CTSU2 VMM: Sensor passed through sensor CCO correction, frequency correction and moving average. The required buffer size varies depending on the measurement mode. Prepare the number of TS for the self-capacitance measurement and current measurement modes, and twice the number of matrixes for the mutual-capacitance measurement mode. In the case of CTSU2 JMM, data of 3 frequencies is stored, so prepare 3 times more. In diagnosis mode, if data collection has not been completed, the function returns FSP_ERR_CTSU_DIAG_NOT_YET. If data collection is completed, it will return FSP_SUCCESS, so please call R_CTSU_Diagnosis() to check the diagnosis result. In addition, if an error occurs in ADC measurement during the output‑voltage diagnosis, the function returns FSP_ERR_ABORTED. Implements ctsu_api_t::dataGet. More...
 
fsp_err_t R_CTSU_AutoJudgementDataGet (ctsu_ctrl_t *const p_ctrl, uint64_t *p_button_status)
 This function gets the result of automatic judgement button. Call after the scan is completed. The result is 64-bit bitmaps and is stored in order of TS number for specified ctsu control. After the initial judgement, the baseline initialization bit is set and the automatic judgement threshold is set. This function is called only for automatic judgement. Implements ctsu_api_t::autoJudgementDataGet. More...
 
fsp_err_t R_CTSU_OffsetTuning (ctsu_ctrl_t *const p_ctrl)
 This function tunes the offset register(SO). Call after the measurement is completed. If the return value is FSP_ERR_CTSU_INCOMPLETE_TUNING, tuning is not complete. Execute the measurement and this function call routine until the return value becomes FSP_SUCCESS. It is recommended to run this routine after R_CTSU_Open(). It can be recalled and tuned again. When the automatic judgement is enabled, after the offset tuning is completed,the baseline initialization bit flag is set. Implements ctsu_api_t::offsetTuning. More...
 
fsp_err_t R_CTSU_ScanStop (ctsu_ctrl_t *const p_ctrl)
 This function scan stops the sensor as scanning by the CTSU. Implements ctsu_api_t::scanStop. More...
 
fsp_err_t R_CTSU_CallbackSet (ctsu_ctrl_t *const p_api_ctrl, void(*p_callback)(ctsu_callback_args_t *), void *const p_context, ctsu_callback_args_t *const p_callback_memory)
 
fsp_err_t R_CTSU_Close (ctsu_ctrl_t *const p_ctrl)
 Disables specified CTSU control block. Implements ctsu_api_t::close. More...
 
fsp_err_t R_CTSU_SpecificDataGet (ctsu_ctrl_t *const p_ctrl, uint16_t *p_specific_data, ctsu_specific_data_type_t specific_data_type)
 This function gets the sensor specific data values as scanned by the CTSU. Call this function after calling the R_CTSU_DataGet() function. More...
 
fsp_err_t R_CTSU_DataInsert (ctsu_ctrl_t *const p_ctrl, uint16_t *p_insert_data)
 This function inserts the value of the second argument as the measurement result value. Call this function after calling the R_CTSU_DataInsert() function. Implements ctsu_api_t::dataInsert. More...
 
fsp_err_t R_CTSU_Diagnosis (ctsu_ctrl_t *const p_ctrl)
 Diagnosis the CTSU peripheral. Please call the function when the return value of R_CTSU_DataGet is FSP_SUCCESS. If an abnormality is detected in any of the diagnosis items, the corresponding diagnosis error is returned. If all diagnosis is complete normally, FSP_SUCCESS is returned. Implements ctsu_api_t::diagnosis. More...
 

Detailed Description

This HAL driver supports the Capacitive Touch Sensing Unit (CTSU). It implements the CTSU Interface.

Overview

The capacitive touch sensing unit HAL driver (r_ctsu) provides an API to control the CTSU peripheral. This module performs capacitance measurement based on various settings defined by the configuration. This module is configured via the QE for Capacitive Touch.

Features

Supported Devices

Device GroupDevices
RA0RA0L1
RA2RA2A1, RA2E1, RA2L1
RA4RA4L1, RA4M1, RA4M2, RA4M3, RA4W1
RA6RA6M1, RA6M2, RA6M3, RA6M4, RA6M5

Configuration

Note
This module is configured via the QE for Capacitive Touch. For information on how to use the QE tool, once the tool is installed click Help -> Help Contents in e² studio and search for "QE".

Build Time Configurations for r_ctsu

The following build time configurations are defined in fsp_cfg/r_ctsu_cfg.h:

ConfigurationOptionsDefaultDescription
Parameter Checking
  • Default (BSP)
  • Enabled
  • Disabled
Default (BSP) If selected code for parameter checking is included in the build.
Support for using DTC
  • Enabled
  • Disabled
Disabled Enable DTC support for the CTSU module.
Enable automatic judgment mode
  • Enabled
  • Disabled
Disabled Enable or Disable automatic judgment mode. This feature is only for RA4L1 and RA0L1.
Interrupt priority levelMCU Specific OptionsPriority level of all CTSU interrupt (CSTU_WR,CTSU_RD,CTSU_FN)

Configurations for CapTouch > CTSU (r_ctsu)

This module can be added to the Stacks tab via New Stack > CapTouch > CTSU (r_ctsu). Non-secure callable guard functions can be generated for this module by right clicking the module in the RA Configuration tool and checking the "Non-secure Callable" box.

ConfigurationOptionsDefaultDescription
Scan Start TriggerMCU Specific OptionsCTSU Scan Start Trigger Select

Interrupt Configuration

The first R_CTSU_Open function call sets the CTSU peripheral interrupts. The user should provide a callback function to be invoked at the end of CTSU scan sequence. The callback argument will contain information about the scan status.

Clock Configuration

The CTSU peripheral module uses PCLKB as its clock source. You can set the PCLKB frequency using the Clocks tab of the RA Configuration editor or by using the CGC Interface at run-time.

Note
CTSU Drive pulse will be calculated and set by the tooling depending on the selected transfer rate.

Pin Configuration

The TSn pins are sensor pins for CTSU.

The TSCAP pin is used for an internal low-pass filter and must be connected to an external decoupling capacitor.

Usage Notes

CTSU module is a CTSU driver for Touch module. CTSU module assumes the access from Touch middleware layer, and it is also accessible from an user application.
The CTSU peripheral installed on RA family has three versions: CTSU, CTSU2, and CTSU2SLa. Each MCU device is equipped with the following version of the CTSU peripherals.

CTSU2SLa : RA4L1, RA0L1
CTSU2    : RA2E1, RA2L1
CTSU     : RA2A1, RA4M1, RA4M2, RA4M3, RA4W1, RA6M1, RA6M2, RA6M3, RA6M4, RA6M5

CTSU, CTSU2 and CTSU2SLa are functionally different, so these are described in this application note as below.

Common description for CTSU, CTSU2 and CTSULSa -> CTSU
Description only for CTSU                      -> CTSU1
Common description for CTSU2 only and CTSU2SLa -> CTSU2
Description only for CTSU2                     -> CTSU2 only
Description only for CTSU2SLa                  -> CTSU2SLa
Without mention, it means the common description for CTSU, CTSU2 and CTSU2SLa.

To understand this document, it is recommended to refer to Capacitive Sensor Microcontrollers CTSU Capacitive Touch Introduction Guide and Capacitive Touch Software Overview beforehand.

When developing an application using the module, it is recommended to use QE for Capacitive Touch.

What is CTSU module

CTSU module is a driver for controlling the CTSU peripheral.

By using configuration settings, CTSU module performs measurements corresponding to the electrodes connected to TS terminals and obtains the capacitance measurement values. Main functions are as follows:

Position of CTSU module

The software configuration of a touch system is shown below.

Normally, CTSU module controls the CTSU peripheral and returns corrected measurement values. Therefore, to obtain button ON/OFF status, slider/wheel touch positions or pad touch coordinates in an application, CTSU module should be used in combination with TOUCH module.

In that case, TOUCH module calls CTSU module and converts the corrected measurement values obtained from CTSU module into button ON/OFF status, slider/wheel touch positions and pad touch coordinates. For details of the specific operation, refer to the usage note for TOUCH module.

If you want to perform your own touch judgment based on the corrected measurement values obtained from CTSU module, refer to the subsequent chapters.

Furthermore, by combining the hardware judgment implemented in CTSU2SLa peripheral (automatic judgment mode) with CTSU module, you can obtain touch ON/OFF results using CTSU module.

CTSU module can also be used to diagnose the internal circuits of the CTSU peripheral and obtain the diagnosis results.

r_ctsu_Relationship_between_CTSU_module_and_user_application.png
Relationship between CTSU module and user application

How to use CTSU module

Figure Overview of software system shows an overview of the software system that illustrates how to use CTSU module.

CTSU module defines measurement modes using a combination of API call groups and the associated structure information. The API call groups used in self-capacitance / mutual-capacitance / current measurement modes are treated as the basic operation model. For the operation models of mutual-capacitance parallel measurement, automatic judgment and diagnosis modes, refer to Measurement Mode.

In the basic operation model, you first call R_CTSU_Open() to initialize the CTSU peripheral and to read the configuration settings and create the configuration information in the control structure. After that, by periodically calling R_CTSU_ScanStart() (measurement start processing) and R_CTSU_DataGet() (measurement result acquisition processing), you can perform periodic measurement processing.

During measurement, the following interrupts occur: INTCTSUWR (register setting request interrupt), INTCTSURD (measurement result readout request interrupt), and INTCTSUFN (measurement completion interrupt). These interrupts are generated by the CTSU peripheral and processed by the module. After processing of INTCTSUFN completes, the module notifies the application of measurement completion via the callback function. After receiving the notification, perform the measurement result acquisition processing.

Note that INTCTSUWR and INTCTSURD processing can also use the DTC. For details on using the DTC, refer to Data Transfer.

r_ctsu_Overview_of_software_system.png
Overview of software system

In this module, data management is structured around two key data structures: the configuration structure and the control structure.

The configuration structure is used to set the measurement targets and measurement conditions. The target of a capacitance measurement is referred to as an element. In self-capacitance measurement mode, TS terminal serves as the measurement target, while in mutual-capacitance measurement mode, the targets are the matrix of transmitter TS terminals and receiver TS terminals. The measurement conditions include various parameters required for executing measurements using the CTSU peripheral, such as measurement mode, measurement time, measurement range, and trigger settings.

The measurement information defined in the configuration structure is copied into the control structure during the execution of R_CTSU_Open(). At the same time, buffers for storing measurement results are allocated, and internal flags and state information for managing measurement states are initialized. After initialization, only the control structure is referenced during the measurement process, enabling unified management of measurement settings, measurement states, and measurement results.

By preparing multiple sets of these structures, it becomes possible to run different measurement modes independently and in parallel, or to operate multiple measurement processes independently and in parallel even within the same measurement mode, provided the measurement targets and measurement conditions differ.

To simplify the configuration of these measurement settings, Renesas provides a development support tool called QE for Capacitive Touch. When creating applications that use this driver, it is recommended to use the touch interface configuration generated by QE for Capacitive Touch.

Measurement Mode

The term measurement mode collectively refers to the measurement processing determined by the measurement target, the measurement method, and the associated conditions. The available measurement modes differ depending on the functions of the CTSU peripheral.

The CTSU peripheral supports self-capacitance measurement mode, mutual-capacitance measurement mode, and diagnosis mode.
In addition to these, CTSU2 peripheral supports current measurement mode, the "CTSU2 only" peripheral supports mutual-capacitance parallel measurement mode, and CTSU2SLa peripheral supports automatic judgment mode.

Details of each measurement mode are described below.

Self-capacitance Measurement Mode

The self-capacitance measurement mode is used to measure the capacitance of each terminal (TS).

The CTSU peripheral measures the terminals in ascending order according to TS numbers, then stores the data. For example, even if you want to use TS5, TS8, TS2, TS3 and TS6 in your application in that order, they will still be measured and stored in the order of TS2, TS3, TS5, TS6, and TS8. Therefore, you will need to reference buffer indexes [2], [4], [0], [1], and [3].

[CTSU1]

In default settings, the measurement period for each TS is wait-time plus approximately 526us.

r_ctsu_Self-capacitance_Measurement_Period.png
Self-capacitance Measurement Period (CTSU1)

[CTSU2]

In default settings, the measurement period for each TS is approximately 576us.

r_ctsu_Self_capacitance_Measurement_Period_ctsu2.png
Self-capacitance Measurement Period (CTSU2)

Mutual-capacitance Measurement Mode

The mutual-capacitance measurement mode is used to measure the capacitance generated between the receive TS (Rx) and transmit TS (Tx), and therefore requires at least two terminals.

CTSU2 peripheral measures all specified combinations of Rx and Tx. For example, when Rx is TS10 and TS3, and Tx is TS2, TS7 and TS4, the combinations are measured in the following order and the data is stored.
TS3-TS2, TS3-TS4, TS3-TS7, TS10-TS2, TS10-TS4, TS10-TS7
To measure the mutual-capacitance measurement generated between electrodes, CTSU2 peripheral performs the measurement process on the same electrode twice.

The mutual-capacitance measurement is obtained by inverting the phase relationship of the pulse output and switched capacitor in the primary and secondary corrected measurement values, and calculating the difference between the two measurements. This module does not calculate the difference, but outputs both primary and secondary corrected measurement result.

[CTSU1]
In default settings, the measurement period for each TS is twice of wait-time plus approximately 526us.

[CTSU2]
In default settings, the measurement period for each TS is approximately 1152us.

r_ctsu_Mutual_capacitance_Measurement_Period.png
Mutual-capacitance Measurement Period (CTSU2)

Current Measurement Mode (CTSU2)

The current measurement mode is used to measure the minute current input to TS terminal.
The order of measurement and data storage is the same as that of the self-capacitance measurement mode.

As this does not involve the switched capacitor operation, the measurement is only performed once. The measurement period for one TS under default settings is approximately 256us. The current measurement mode requires a longer stable wait time than the other modes, so the SST is set to 63.

r_ctsu_Current_Measurement_Period.png
Current Measurement Period

Mutual-capacitance parallel Measurement Mode (CTSU2 only)

This mode provides fast measurement time by parallel scanning the RX lines with a CFC circuit. Operation is otherwise identical to normal CTSU mutual scanning.

Automatic Judgment Mode (CTSU2SLa)

In automatic judgment mode, CTSU2SLa peripheral is controlled to obtain touch judgment results. Since the touch judgment processing is performed by CTSU2SLa peripheral, touch judgment can be executed without using CPU processing. The DTC module is mandatory because DTC transfer are necessary to process the interrupts that occur during measurement. For details on the DTC, refer to Cooperation with other modules.

The operation flow of automatic judgment mode is shown below. First, initial offset tuning is performed using R_CTSU_ScanStart() and R_CTSU_OffsetTuning(). For details on initial offset tuning, refer to Initial Offset Adjustment. After initial offset tuning completes, automatic judgment measurement processing is performed using R_CTSU_ScanStart() and R_CTSU_AutoJudgementDataGet(). Note that the processing for the first measurement is different from that for the second and subsequent measurements:

First measurement:

The baseline for touch judgment is set. The first corrected measurement value is used as the baseline, and the touch judgment result is OFF.

Second and subsequent measurements:

Measurements are performed according to the measurement settings in the control structure, and touch judgment results are obtained.
By combining automatic judgment mode with measurement started by external triggers, you can create applications that perform touch judgment with low-power consumption. For more details and notes in that case, refer to Notes for low-power touch judgment. If you need more details on automatic judgment, refer to Functions and settings.

r_ctsu_Basic_operation_model_of_automatic_judgment_mode.png
Basic operation model of automatic judgment mode

In low-power touch applications that combine CPU Snooze mode or Software Standby mode with automatic judgment mode, it is possible to keep the CPU in a low-power state while performing touch judgment and wake up the CPU only when a touch judgment result is ON.
However, CPU processing is required until the baseline is set. Therefore, switch to the low-power operation mode after executing the first R_CTSU_ScanStart() and R_CTSU_AutoJudgementDataGet(). For concrete operation examples, refer to "Smart Wakeup Solution" application note.

When using the automatic judgment mode, in addition to the standard measurement settings, it is necessary to modify certain macro definitions and configure the configuration structure for automatic judgment mode. These settings differ depending on the automatic judgment method, JMM or VMM.

First, to enable the automatic judgment feature, set CTSU_CFG_AUTO_JUDGE_ENABLE = 1. At the same time, enable the automatic CCO correction function by setting CTSU_CFG_AUTO_CORRECTION_ENABLE = 1. For VMM, also enable the automatic frequency correction function by setting CTSU_CFG_AUTO_MULTI_CLOCK_CORRECTION_ENABLE = 1.

Next, an example of configuration structure settings required in addition to the standard measurement settings is shown below for JMM. When using VMM, set jc = 0 and majirimd = 1.

.tlot     = 2, // Non-touch judgment continuous count : 3 times
.thot     = 2, // Touch judgment continuous count : 3 times
.jc       = 1, // Judgment by two or more frequency
.ajmmat   = 2, // Moving average : 2^2 times
.ajbmat   = 7, // Baseline average count : 2^(7+1) times
.majirimd = 0, // JMM
.mtucfen  = 1, // Enable mutual-capacitance calculation
.ajfen    = 1, // Enable automatic judgment

The following (a) ~ (e) describe the automatic judgment and its setting. In the case of JMM, (a) ~ (e) settings are set for each multi-clock measurement.

Diagnosis Mode

The CTSU peripheral has a function to diagnose its internal circuits. Diagnosis mode provides APIs that diagnose whether the internal circuits are operating correctly.
In this mode, CTSU internal circuits are diagnosed and, if an abnormality occurs, the result can be obtained as an error.
The diagnosis contents differ between CTSU1 and CTSU2, and therefore the operation model (API call sequence) also differs.
To enable diagnosis mode, set CTSU_CFG_DIAG_SUPPORT_ENABLE = 1.

[CTSU1]

OrderDiagnosis items
1Over Voltage Detection Diagnosis
2CCO High Diagnosis
3CCO Low Diagnosis
4SSCG Oscillator Diagnosis
5Current Offset Diagnosis

[CTSU2]

OrderDiagnosis itemsOnetime / Repeat (※1)
1Output Voltage DiagnosisRepeat
2Over Voltage Detection DiagnosisOne-time
3Over Current Detection DiagnosisOne-time
4Load Resistance DiagnosisRepeat
5Current Offset DiagnosisOne-time
6SENSCLK Frequency DiagnosisRepeat
7SUCLK Frequency DiagnosisRepeat
8SUCLK Clock Recovery DiagnosisRepeat
9CFC Oscillator Gain Diagnosis (※2)Repeat

※1 "One-time" assumes diagnosis is performed once after reset. "Repeat" assumes periodic measurements.
※2 This diagnosis can only be used during Mutual-capacitance parallel Measurement Mode (CTSU2 only).

|Member Name|Setting| |--------—|----—| |ctsu_element_cfg_t| |so|0x00| |snum|0x07| |sdpa|Measurement setting for overcurrent detection| |ctsu_cfg_t| |md|CTSU_MODE_DIAGNOSIS_SCAN| |ctsuchacN (N=0~4)|Measurement terminal for overcurrent detection|

$$ I = fCV $$

$$ SDPA_1 = ((SDPA_0 + 1)*I_0 / 100) - 1 $$

Make sure that the sensor‑drive pulse frequency set by SDPA1 provides enough time to charge and discharge the target TS terminal.

Functions

CTSU module supports the following functions.

Touch Judgment Type

CTSU2 has two judgment type, which differ in processing and output data.
It is common to get three raw value for each element and calculate CCO correction, and then performs different processing.

  1. Value Majority Mode(VMM)
    Calculate three frequency correction value from three CCO correction values. Calculate the sum of the two closest values of the three frequency correction value. Outputs the moving average of this added value.
  2. Judgment Majority Mode (JMM)
    This mode is supported from FSP V5.6.0 or later.
    Outputs the moving average of three CCO correction values.
r_ctsu_JMM_vs_VMM.png
Data flow Comparison of JMM and VMM

Measurements and Obtaining Data

Measurements can be started by a software trigger or by an external event triggered by Event Link Controller (ELC).
As the measurement process is carried out by CTSU2 peripheral, it does not use up main processor processing time.

CTSU module processes INTCTSUWR and INTCTSURD if generated during a measurement. Data Transfer Controller (DTC) can also be used for these processes.
When the measurement complete interrupt (INTCTSUFN) process is complete, the application is notified in a callback function. Make sure you obtain the measurement results before the next measurement is started as internal processes are also executed when a measurement is completed.

Start the measurement with API function R_CTSU_ScanStart().
Obtain the measurement results with API function R_CTSU_DataGet().

The number of arrays in the second argument of R_CTSU_DataGet() is different between VMM and JMM.
VMM returns one measurement result for each element.
JMM returns three measurement results for each element.

Random Pulse Frequency Measurement (CTSU1)

The drive pulse used for the measurement is a pulse with phase shifting and frequency spreading applied to the configured base clock.
The base-cloclk setting generally uses the value tuned with QE for Capacitive Touch.
This module is fixed at initialization and sets the following.
CTSUSOFF = 0, CTSUSSMOD = 0, CTSUSSCNT = 3
The drive frequency is calculated as below.

It is determined by PCLK frequency input to CTSU, CTSU Count Source Select bit(CTSUCLK), and CTSU Sensor Drive pulse Division Control bit(CTSUSDPA). For example, If it is set PCLK =32MHz, CTSUCLK = PLCK/2, and CTSUSDPA = 1/16, then drive frequency is 0.5MHz. CTSUSDPA can change for each TS port.

r_ctsu_Base_clock_settings.png
Base clock settings

Multi-clock Measurements (CTSU2)

CTSU2 peripheral can measure in one of four drive frequencies to avoid synchronous noise.

By default, this module measures at three different frequencies and makes a majority judgment on the three measurement results obtained.

r_ctsu_Multi-clock_Mesurements.png
Multi-clock Measurements

There are two types of majority judgment modes for the three measurement results: JMM (Judgment Majority Mode) and VMM (Value Majority Mode). JMM only supports self-capacitance buttons and mutual-capacitance buttons.

The figure below shows the flowchart of JMM and VMM with Touch module.

r_ctsu_Flowchart_of_JMM_and_VMM.png
Flowchart of JMM and VMM

JMM performs CCO correction on the three measurement values, then makes a touch judgment for each of them, and determines the final touch result by majority vote of the three judgments.

VMM performs CCO correction on the three measurement values, applies frequency correction to convert them to the measurement value at the first frequency, and then selects the two values that are close to each other. These two values are added together, and the result is treated as a measurement value with double the measurement time. Touch judgment is made based on this corrected measurement value.

Example VMM Calculations
From the frequency-corrected measurement values 1, 2, and 3, the difference values 1, 2, and 3 for each pair are calculated, and the smaller pair is selected by comparing the absolute values of the difference values. To prevent variation in the corrected measurement values, a combination of value 1 and value 2 is given a weight to be selected. When comparing value 3, multiply the difference value 2 by 2 and multiply the difference value 3 by 1.5.

Corrected Measurement Value 1Corrected Measurement Value 2Corrected Measurement Value 3Difference value 1Difference value 2Difference value 3ResultAdded Value
77347734766307171Value 1+215468
768977397666502373Value 1+315355
773476797664557015Value 2+315343
77217719769422725Value 1+215440
771677477693312354Value 1+215463

You can set JMM or VMM for each touch interface configuration. If the ctsu_cfg_t member "majority_mode" is set to 1, it works in JMM, and if it is set to 0, it works in VMM.

R_CTSU_DataGet() can get the data after conducting the moving average. To retrieve the data for each of the previous processes R_CTSU_SpecificDataGet() use. These data can also be used to determine the data with its own noise filter in Touch module. See R_CTSU_DataGet() and R_CTSU_SpecificDataGet() for more information.

Drive pulse frequency is determined based on the config settings. The module sets registers according to the config settings, and sets the three drive frequencies.

Drive pulse frequency is calculated in the following equation:
$$ f_{DrivePulse} = F_{PCLKB} / (2^{CLK}*2*(STCLK + 1)) * (SUMULTI_n + 1) / (2*(SDPA + 1)), n = 0, 1, 2 $$ The figure below shows the settings for generating a 2MHz drive pulse frequency when PCLKB frequency is 32 MHz. SDPA can be set for each touch interface configuration.

r_ctsu_Drive_Pulse_Frequency_Settings.png
Drive Pulse Frequency Settings

Sensor CCO Correction function

The CTSU peripheral has a built-in correction circuit to handle the potential microvariations related to the manufacturing process of the sensor CCO MCU.

This module uses the correction circuit during initialization after power-on to generate a correction coefficient to ensure accurate sensor measurement values. This correction coefficient is used to correct the measurement value.

Initial Offset Adjustment

The CTSU peripheral was designed with a built-in offset current circuit in consideration of the amount of change in current due to touch. The offset current circuit cancels enough of the parasitic capacitance for it to fit within the sensor CCO dynamic range.

This module adjusts the offset current setting so that the corrected measurement value reaches the target value. As the adjustment uses the normal measurement process, R_CTSU_ScanStart() and R_CTSU_DataGet() must be repeated several times after startup. Because the ctsu_element_cfg_t member "so" is the starting point for adjustments, you can set the appropriate value for "so" in order to reduce the number of times the two functions must be run to complete the adjustment. Normally, the value used for "so" is a value adjusted by QE for Capacitive Touch.

For CTSU2, this feature can be turned off in the config.

Default target value (CTSU)

ModeCTSU1 target valueCTSU2 target value
Self-capacitance15360 (37.5%)11520 (37.5%)
Self-capacitance using active shield-4608 (15%)
Mutual-capacitance10240 (25%)7680 (25%)

The percentage is based on 100% being the maximum input current applied to the CCO.
CTSU1: 100% is the measured value 40960 when the measurement time is 526us(base time).
CTSU2: 100% is the measured value 30720 when the measurement time is 256us(base time).
When the measurement time is changed, the target value is adjusted by the ratio with the base time.

Example of target value in combination of CTSUSNUM and CTSUSDPA

Target valueCTSUSNUMCTSUSDPAMeasurement time
153600x30x7526usec
307200x70x71052usec
307200x30xF1052usec
76800x10x7263usec
76800x30x3263usec

The measurement time changes depending on the combination of CTSUSNUM and CTSUSDPA.
Recommended CTSUPRRATIO and CTSUPRMODE are used. Changing this value is deprecated. For detail, refer to the hardware manual of each capacitive touch sensor.

Target valueTarget value (multi-clock)CTSUSNUMMeasurement time
576011520 (128us + 128us)0x7128usec
1152023040 (256us + 256us) 0xF256usec
28805460 (64us + 64us)0x364usec

The measurement time changes depending on CTSUSNUM. If STCLK cannot be set to 0.5MHz, it will not support the table above. Regarding STCLK, refer to the hardware manual.

Moving Average

This function calculates the moving average of the measured results.
Set the number of times the moving average should be calculated in the config settings.

Callback function

When an abnormal condition is detected during measurement, the CTSU peripheral sets the corresponding status register.
In the measurement-completion interrupt handler, the module reads CTSUSOVF from the status register and CTSUICOMP from the error status register for CTSU1, and reads ICOMP1, ICOMP0, and SENSOVF from the status register for CTSU2. After the registers are read, the status registers are cleared. The details of the abnormal condition can be checked by referring to the member "event" of the ctsu_callback_args_t structure passed to the callback function.

Shield Function (CTSU2)

CTSU2 peripheral has a built-in function that outputs a shield signal in phase with the drive pulse from the shield terminal and the non-measurement terminal in order to shield against external influences while suppressing any increase in parasitic capacitance. This function can only be used during Self-capacitance Measurement.

This module allows the user to set a shield for each touch interface configuration.

For example, for the electrode configuration shown below, the members of ctsu_cfg_t should be set as follows. Other members have been omitted for the example.

.txvsel     = CTSU_TXVSEL_INTERNAL_POWER,
.txvsel2    = CTSU_TXVSEL_MODE,
.md         = CTSU_MODE_SELF_MULTI_SCAN,
.posel      = CTSU_POSEL_SAME_PULSE,
.ctsuchac0  = 0x0F,
.ctsuchtrc0 = 0x08,
r_ctsu_Shield_Electrode_Structure.png
Example of Shield Electrode Structure

MEC Function (CTSU2SLa)

CTSU2SLa peripheral has MEC (Multiple Electrode Connection) function that connects multiple electrodes and measures them as a single electrode. This feature is only available in Self-capacitance Measurement Mode.

This is an example when using three electrodes. In normal times, normal measurement is performed, and 3 channels are measured to get each corrected measured value. In power saving, MEC measurement is performed, and one channel is measured by combining three channels to acquire one corrected measured value.

The figure below shows a compare of time of normal measurement and MEC measurement. Since multi channels are measured at the same time, the measurement time is shortened.

r_ctsu_Compare_Normal_measure_vs_MEC_measure.png
Comparison of time between normal measurement and MEC measurement

To enable the code for MEC feature, set CTSU_CFG_MULTIPLE_ELECTRODE_CONNECTION_ENABLE to 1.

When using MEC, create a touch interface configuration different from the normal touch interface configuration for the same TS. The following settings are required for the touch interface configuration for MEC measurement.

To enable MEC for touch interface configurations by setting tsod in ctsu_cfg_t to 1.

Set mec_ts of ctsu_cfg_t to one of TS numbers to be measured.

If you want to use the shield function at the same time, set TS number of the shield terminal in mec_shield_ts of ctsu_cfg_t. In this case, only one TS can be used as a shield terminal.

Set num_rx of ctsu_cfg_t to 1.

For example, in the case of the electrode configuration shown below, set the members of ctsu_cfg_t as shown below. Other members are omitted here.

.tsod          = 1,
.mec_ts        = 0,
.mec_shield_ts = 3,
.num_rx        = 1,
r_ctsu_MEC_Shield_electrode.png
Example of MEC and shield electrode configuration

Automatic CCO Correction (CTSU2SLa)

The automatic CCO correction function is a feature that performs sensor CCO correction calculations inside CTSU2SLa peripheral. For details on sensor CCO correction, refer to Sensor CCO Correction function.

Because CTSU2SLa peripheral processes the sensor CCO correction calculation internally, the corrected data can be obtained without using software‑based correction processing, and no processing time is consumed on the main processor.

To enable the automatic CCO correction function, set CTSU_CFG_AUTO_CORRECTION_ENABLE to 1.

Automatic Frequency Correction (CTSU2SLa)

The automatic frequency‑correction function is a feature that performs multi-clock correction inside CTSU2SLa peripheral.

Because CTSU2SLa peripheral processes the multi-clock correction calculation internally, the corrected data can be obtained without using software‑based correction processing, and no processing time is consumed on the main processor.

To enable the automatic frequency correction function, set CTSU_CFG_AUTO_MULTI_CLOCK_CORRECTION_ENABLE to 1.

When using the automatic frequency‑correction function, also enable the automatic CCO correction function.

Cooperation with other modules

Some functions of CTSU module require cooperation with other modules.

ModuleApplicationIntegration Method
Timers (r_agt, r_tau, ect.)Start measurement via external triggerAdd FSP module for the timer used (such as AGT). Configure it in the application as appropriate.
ELC (r_elc)Start measurement via external triggerAdd ELC module. Configure it in the application according to the timer used.
LPM (r_lpm)Low power touch operationAdd LPM module.
Transfer (r_dtc)Data transfer for INTCTSUWR and INTCTSURD during measurementAdd DTC module. CTSU module already uses DTC APIs internally.
ADC (r_adc/r_adc_d)Diagnose Output voltage in CTSU2 diagnosis functionAdd ADC module. CTSU module already uses ADC APIs internally.

Trigger for Measurement Start

Measurement can be started by either a software trigger or an external trigger. Select according to your system requirements.

Software trigger

Measurement starts when R_CTSU_ScanStart() sets the measurement‑start bit to 1. Since configuration for the touch interface is performed before setting this bit, there is a small delay between the API call and the actual start of measurement. However, this delay is very small compared with the measurement time, so the software trigger, which is simpler to configure, is generally recommended.

External trigger

After R_CTSU_ScanStart() sets the measurement‑start trigger‑selection bit and the measurement‑start bit to 1, CTSU enters a trigger‑wait state. Measurement starts upon receiving an event. Generally, timers are used to measure periodically. Use this method when the small delay of the software trigger cannot be tolerated or when low‑power operation is required. When using multiple measurement modes with external triggers, configure the next measurement mode by calling R_CTSU_ScanStart() after measurement of one mode completes.

When using an external trigger, configure the settings in the Stacks tab as follows:

Low-power modes

LPM module reduces power consumption by managing transitions between operating control modes and controlling low‑power states. By combining the LPM module with the automatic judgment mode of CTSU2LSa, low‑power touch operation can be achieved.

When using the LPM module with CTSU, configure the settings in the Stacks tab as follows:

Data Transfer

In CTSU module, DTC can be used instead of CPU to handle INTCTSUWR and INTCTSURD processing during measurement. In automatic judgment mode, DTC module must be used. In other measurement modes, DTC is optional.

When using DTC module with CTSU, configure the settings in the Stacks tab as follows:

Diagnosis

In CTSU2 diagnosis mode, ADC measurements are required for certain steps. Therefore, add ADC module to the Stacks tab and configure the settings as follows:

For RA0L1 (using the r_adc_d module):

For other devices (using the r_adc module):

If you also use ADC in the application outside diagnosis mode, release ADC resources by closing ADC module at the following timings:

ADC module can be closed by calling R_ADC_Close().

If you do not close ADC module, R_CTSU_Diagnosis() returns FSP_ERR_ABORTED. In that case, close ADC and ensure that ADC measurement by CTSU module can be performed in the next diagnosis mode execution.

Measurement Timing

Measurements are initiated by a software trigger or an external event which is triggered by Event Link Controller (ELC).

The most common method is using a timer to carry out periodic measurements. Make sure to set the timer interval to allow the measurement and internal value update processes to complete before the next measurement period. The measurement period differs according to touch interface configuration and measurement mode.

The execution timing of software triggers and external triggers differ slightly.

Since a software trigger sets the start flag after setting the touch interface configuration with R_CTSU_ScanStart(), there is a slight delay after the timer event occurrence. However, as the delay is much smaller than the measurement period, a software trigger is recommended for most instances as it is easy to set.

An external trigger is recommended for applications in which this slight delay is not acceptable or that require low-power consumption operations. When using an external trigger with multiple touch interface configurations, use R_CTSU_ScanStart() to set another touch interface configuration after one measurement is completed.

TrustZone Support

In r_ctsu and rm_touch module, Non-Secure Callable Guard Functions are only generated from QE for Capacitive Touch. QE can be used for tuning in secure or flat project, but not in non-secure project. If you want to use in non-secure project, copy the output file from secure or flat project. Refer to QE Help for more information.

Data flow

The flow of storing data in RAM is as follows.

(CTSU1)

  1. Read registers and stored in RAM as raw data.
  2. ICO correction calculation of raw data and stored in RAM as correction data.
  3. The correction data is calculated by moving average and stored in RAM as measurement results.

(CTSU2 VMM)

  1. Reads a register and stores raw data measured at three different frequencies in RAM.
  2. Calculate CCO correction from three raw data and stored three CCO correction values in RAM.
  3. Calculate three frequency correction values from three CCO correction values.
  4. Three frequency correction data are calculated by majority decision and moving average, and stored in RAM as measurement results.

(CTSU2 JMM)

  1. Reads a register and stores raw data measured at three different frequencies in RAM.
  2. Calculate CCO correction from three raw data and stored three CCO correction values in RAM.
  3. Three CCO correction data is calculated by moving average and stored three measurement results in RAM.

Add user's filter

There are two ways to add the user's filter.

  1. Instead of filter calculation of R_CTSU_DataGet(), perform user filter calculation and use R_CTSU_DataInsert() to input user filter calculation result.
    R_CTSU_DataInsert()
    In VMM, the second argument p_insert_data stores one data for each element.
    In JMM, the second argument p_insert_data stores three data for each element.
  2. Using the correction data obtained by R_CTSU_SpecificDataGet(), instead of majority decision calculation and filter calculation of R_CTSU_DataGet(), perform user majority decision calculation & filter calculation and use R_CTSU_DataInsert() to input user majority decision calculation & filter calculation result.

Please check example.
User's filter additional Example

Limitations

Examples

Basic Example

This is a basic example of minimal use of CTSU in an application.

volatile bool g_scan_flag = false;
void ctsu_callback (ctsu_callback_args_t * p_args)
{
{
g_scan_flag = true;
}
}
void ctsu_basic_example (void)
{
fsp_err_t err = FSP_SUCCESS;
uint16_t data[CTSU_CFG_NUM_SELF_ELEMENTS];
err = R_CTSU_Open(&g_ctsu_ctrl, &g_ctsu_cfg);
/* Handle any errors. This function should be defined by the user. */
assert(FSP_SUCCESS == err);
while (true)
{
err = R_CTSU_ScanStart(&g_ctsu_ctrl);
assert(FSP_SUCCESS == err);
while (!g_scan_flag)
{
/* Wait for scan end callback */
}
g_scan_flag = false;
err = R_CTSU_DataGet(&g_ctsu_ctrl, data);
if (FSP_SUCCESS == err)
{
/* Application specific data processing. */
}
}
}

Multi-configuration Example

This is a optional example of using both Self-capacitance and Mutual-capacitance configurations in the same project.

void ctsu_optional_example (void)
{
fsp_err_t err = FSP_SUCCESS;
uint16_t data[CTSU_CFG_NUM_SELF_ELEMENTS + (CTSU_CFG_NUM_MUTUAL_ELEMENTS * 2)];
err = R_CTSU_Open(&g_ctsu_ctrl, &g_ctsu_cfg);
assert(FSP_SUCCESS == err);
err = R_CTSU_Open(&g_ctsu_ctrl_mutual, &g_ctsu_cfg_mutual);
assert(FSP_SUCCESS == err);
while (true)
{
R_CTSU_ScanStart(&g_ctsu_ctrl);
while (!g_scan_flag)
{
/* Wait for scan end callback */
}
g_scan_flag = false;
R_CTSU_ScanStart(&g_ctsu_ctrl_mutual);
while (!g_scan_flag)
{
/* Wait for scan end callback */
}
g_scan_flag = false;
err = R_CTSU_DataGet(&g_ctsu_ctrl, data);
assert(FSP_SUCCESS == err);
if (FSP_SUCCESS == err)
{
/* Application specific data processing. */
}
err = R_CTSU_DataGet(&g_ctsu_ctrl_mutual, data);
assert(FSP_SUCCESS == err);
if (FSP_SUCCESS == err)
{
/* Application specific data processing. */
}
}
}

Offset Adjustment Example

This is an example of offset adjustment using R_CTSU_OffsetTuning().
After completing R_CTSU_Open (), perform initial offset adjustment.
Offset adjustment is performed again when the parasitic capacitance changes significantly due to changes in the surrounding environment and the count value becomes an abnormal value.

void ctsu_offsettuning_example (void)
{
fsp_err_t err = FSP_SUCCESS;
uint16_t data[CTSU_CFG_NUM_SELF_ELEMENTS];
err = R_CTSU_Open(&g_ctsu_ctrl, &g_ctsu_cfg);
assert(FSP_SUCCESS == err);
/* Initial offset tuning */
do
{
R_CTSU_ScanStart(&g_ctsu_ctrl);
while (!g_scan_flag)
{
/* Wait for scan end callback */
}
g_scan_flag = false;
err = R_CTSU_OffsetTuning(&g_ctsu_ctrl);
} while (FSP_SUCCESS != err);
while (true)
{
R_CTSU_ScanStart(&g_ctsu_ctrl);
while (!g_scan_flag)
{
/* Wait for scan end callback */
}
g_scan_flag = false;
err = R_CTSU_DataGet(&g_ctsu_ctrl, data);
assert(FSP_SUCCESS == err);
if (FSP_SUCCESS == err)
{
/* Re-offset tuning is performed when the parasitic capacitance changes significantly due */
/* to changes in the surrounding environment and the count value becomes an abnormal value. */
/* */
/* if (abnormal value detection conditions) */
/* { */
/* Re-offset tuning */
do
{
R_CTSU_ScanStart(&g_ctsu_ctrl);
while (!g_scan_flag)
{
/* Wait for scan end callback */
}
g_scan_flag = false;
err = R_CTSU_OffsetTuning(&g_ctsu_ctrl);
} while (FSP_SUCCESS != err);
/* } */
}
}
}

Diagnosis function Example

This is a Diagnosis function example of using the configuration in the basic example.

CTSU1 Diagnosis function example:

void ctsu_diag_example (void)
{
fsp_err_t err = FSP_SUCCESS;
uint16_t data[CTSU_CFG_NUM_SELF_ELEMENTS];
uint16_t dummy;
err = R_CTSU_Open(&g_ctsu_ctrl, &g_ctsu_cfg);
assert(FSP_SUCCESS == err);
err = R_CTSU_Open(&g_ctsu_ctrl_diagnosis, &g_ctsu_cfg_diagnosis);
assert(FSP_SUCCESS == err);
while (true)
{
err = R_CTSU_ScanStart(&g_ctsu_ctrl);
assert(FSP_SUCCESS == err);
while (!g_scan_flag)
{
/* Wait for scan end callback */
}
g_scan_flag = false;
err = R_CTSU_DataGet(&g_ctsu_ctrl, data);
assert(FSP_SUCCESS == err);
err = R_CTSU_ScanStart(&g_ctsu_ctrl_diagnosis);
assert(FSP_SUCCESS == err);
while (!g_scan_flag)
{
/* Wait for scan end callback */
}
g_scan_flag = false;
err = R_CTSU_DataGet(&g_ctsu_ctrl_diagnosis, &dummy);
assert(FSP_SUCCESS == err);
if (FSP_SUCCESS == err)
{
err = R_CTSU_Diagnosis(&g_ctsu_ctrl_diagnosis);
assert(FSP_SUCCESS == err);
if (FSP_SUCCESS == err)
{
break;
}
}
}
}

CTSU2 Diagnosis function example:

void ctsu2_diag_example (void)
{
fsp_err_t err = FSP_SUCCESS;
uint16_t data[CTSU_CFG_NUM_SELF_ELEMENTS];
uint16_t dummy;
uint8_t initial_diag_flag = 0;
err = R_CTSU_Open(&g_ctsu_ctrl, &g_ctsu_cfg);
assert(FSP_SUCCESS == err);
err = R_CTSU_Open(&g_ctsu_ctrl_diagnosis, &g_ctsu_cfg_diagnosis);
assert(FSP_SUCCESS == err);
/*Initial Diagnosis loop */
while (0 == initial_diag_flag)
{
/* For Diagnosis configuration */
err = R_CTSU_ScanStart(&g_ctsu_ctrl_diagnosis);
assert(FSP_SUCCESS == err);
while (!g_scan_flag)
{
/* Wait for scan end callback */
}
g_scan_flag = false;
err = R_CTSU_DataGet(&g_ctsu_ctrl_diagnosis, &dummy);
assert(FSP_SUCCESS == err);
if (FSP_SUCCESS == err)
{
err = R_CTSU_Diagnosis(&g_ctsu_ctrl_diagnosis);
if (FSP_SUCCESS == err)
{
initial_diag_flag = 1;
}
else
{
assert(FSP_SUCCESS == err);
}
}
}
while (true)
{
err = R_CTSU_ScanStart(&g_ctsu_ctrl);
assert(FSP_SUCCESS == err);
while (!g_scan_flag)
{
/* Wait for scan end callback */
}
g_scan_flag = false;
err = R_CTSU_DataGet(&g_ctsu_ctrl, data);
assert(FSP_SUCCESS == err);
err = R_CTSU_ScanStart(&g_ctsu_ctrl_diagnosis);
assert(FSP_SUCCESS == err);
while (!g_scan_flag)
{
/* Wait for scan end callback */
}
g_scan_flag = false;
err = R_CTSU_DataGet(&g_ctsu_ctrl_diagnosis, &dummy);
assert(FSP_SUCCESS == err);
if (FSP_SUCCESS == err)
{
err = R_CTSU_Diagnosis(&g_ctsu_ctrl_diagnosis);
assert(FSP_SUCCESS == err);
if (FSP_SUCCESS == err)
{
break;
}
}
}
}

User's filter additional Example

This is a user's filter additiional example of using the configuration in the basic example.
To perform user's filter calculation, change the num_moving_average of the element in the target ctsu_cfg_t to 1.

Perform user filter calculation and use R_CTSU_DataInsert() to input user filter calculation result.

void ctsu_user_filter_example (void)
{
fsp_err_t err = FSP_SUCCESS;
uint16_t data[CTSU_CFG_NUM_SELF_ELEMENTS];
uint16_t filter_data[CTSU_CFG_NUM_SELF_ELEMENTS];
/* If you want to make a touch judgment, call RM_TOUCH_Open()instead of the following. */
err = R_CTSU_Open(&g_ctsu_ctrl, &g_ctsu_cfg);
/* Handle any errors. This function should be defined by the user. */
assert(FSP_SUCCESS == err);
while (true)
{
/* If you want to make a touch judgment, call RM_TOUCH_ScanStart()instead of the following. */
err = R_CTSU_ScanStart(&g_ctsu_ctrl);
assert(FSP_SUCCESS == err);
while (!g_scan_flag)
{
/* Wait for scan end callback */
}
g_scan_flag = false;
err = R_CTSU_DataGet(&g_ctsu_ctrl, data);
if (FSP_SUCCESS == err)
{
/* User original function. */
ctsu_user_filter(data, filter_data);
err = R_CTSU_DataInsert(&g_ctsu_ctrl, filter_data);
assert(FSP_SUCCESS == err);
/* Call RM_TOUCH_DataGet() to make a touch decision. */
}
}
}

Using the correction data obtained by R_CTSU_SpecificDataGet(). Perform user majority decision calculation & filter calculation and use R_CTSU_DataInsert() to input user majority decision calculation & filter calculation result.

void ctsu_user_majority_decition_example (void)
{
fsp_err_t err = FSP_SUCCESS;
uint16_t data[CTSU_CFG_NUM_SELF_ELEMENTS];
uint16_t corr_data[CTSU_CFG_NUM_SELF_ELEMENTS * CTSU_CFG_NUM_SUMULTI];
uint16_t filter_data[CTSU_CFG_NUM_SELF_ELEMENTS];
/* If you want to make a touch judgment, call RM_TOUCH_Open()instead of the following. */
err = R_CTSU_Open(&g_ctsu_ctrl, &g_ctsu_cfg);
/* Handle any errors. This function should be defined by the user. */
assert(FSP_SUCCESS == err);
while (true)
{
/* If you want to make a touch judgment, call RM_TOUCH_ScanStart()instead of the following. */
err = R_CTSU_ScanStart(&g_ctsu_ctrl);
assert(FSP_SUCCESS == err);
while (!g_scan_flag)
{
/* Wait for scan end callback */
}
g_scan_flag = false;
err = R_CTSU_DataGet(&g_ctsu_ctrl, data);
if (FSP_SUCCESS == err)
{
err = R_CTSU_SpecificDataGet(&g_ctsu_ctrl, corr_data, CTSU_SPECIFIC_CORRECTION_DATA);
assert(FSP_SUCCESS == err);
/* User original function */
ctsu_user_filter(corr_data, filter_data);
err = R_CTSU_DataInsert(&g_ctsu_ctrl, filter_data);
assert(FSP_SUCCESS == err);
/* Call RM_TOUCH_DataGet() to make a touch decision. */
}
}
}

Data Structures

struct  ctsu_ctsuwr_t
 
struct  ctsu_self_buf_t
 
struct  ctsu_mutual_buf_t
 
struct  ctsu_correction_info_t
 
struct  ctsu_instance_ctrl_t
 

Enumerations

enum  ctsu_state_t
 
enum  ctsu_tuning_t
 
enum  ctsu_correction_status_t
 
enum  ctsu_range_t
 

Data Structure Documentation

◆ ctsu_ctsuwr_t

struct ctsu_ctsuwr_t

CTSUWR write register value

Data Fields
uint16_t ctsussc Copy from (ssdiv << 8) by Open API.
uint16_t ctsuso0 Copy from ((snum << 10) | so) by Open API.
uint16_t ctsuso1 Copy from (sdpa << 8) by Open API. ICOG and RICOA is set recommend value.

◆ ctsu_self_buf_t

struct ctsu_self_buf_t

Scan buffer data formats (Self)

Data Fields
uint16_t sen Sensor counter data.
uint16_t ref Reference counter data (Not used)

◆ ctsu_mutual_buf_t

struct ctsu_mutual_buf_t

Scan buffer data formats (Mutual)

Data Fields
uint16_t pri_sen Primary sensor data.
uint16_t pri_ref Primary reference data (Not used)
uint16_t snd_sen Secondary sensor data.
uint16_t snd_ref Secondary reference data (Not used)

◆ ctsu_correction_info_t

struct ctsu_correction_info_t

Correction information

Data Fields
ctsu_correction_status_t status Correction status.
ctsu_ctsuwr_t ctsuwr Correction scan parameter.
volatile ctsu_self_buf_t scanbuf Correction scan buffer.
uint8_t calculation_error Overflow or underflow in correction calclation.
uint16_t first_val 1st correction value
uint16_t second_val 2nd correction value
uint32_t first_coefficient 1st correction coefficient
uint32_t second_coefficient 2nd correction coefficient
uint32_t ctsu_clock CTSU clock [MHz].

◆ ctsu_instance_ctrl_t

struct ctsu_instance_ctrl_t

CTSU private control block. DO NOT MODIFY. Initialization occurs when R_CTSU_Open() is called.

Data Fields

uint32_t open
 Whether or not driver is open.
 
volatile ctsu_state_t state
 CTSU run state.
 
ctsu_cap_t cap
 CTSU Scan Start Trigger Select.
 
ctsu_md_t md
 CTSU Measurement Mode Select(copy to cfg)
 
ctsu_tuning_t tuning
 CTSU Initial offset tuning status.
 
uint16_t num_elements
 Number of elements to scan.
 
uint16_t wr_index
 Word index into ctsuwr register array.
 
uint16_t rd_index
 Word index into scan data buffer.
 
uint8_t * p_element_complete_flag
 Pointer to complete flag of each element. g_ctsu_element_complete_flag[] is set by Open API.
 
int32_t * p_tuning_diff
 Pointer to difference from base value of each element. g_ctsu_tuning_diff[] is set by Open API.
 
uint16_t average
 CTSU Moving average counter.
 
uint16_t num_moving_average
 Copy from config by Open API.
 
uint8_t ctsucr1
 Copy from (atune1 << 3, md << 6) by Open API. CLK, ATUNE0, CSW, and PON is set by HAL driver.
 
ctsu_ctsuwr_tp_ctsuwr
 CTSUWR write register value. g_ctsu_ctsuwr[] is set by Open API.
 
ctsu_self_buf_tp_self_raw
 Pointer to Self raw data. g_ctsu_self_raw[] is set by Open API.
 
uint16_t * p_self_corr
 Pointer to Self correction data. g_ctsu_self_corr[] is set by Open API.
 
uint16_t * p_self_mfc
 Pointer to Self multi frequency correction data. g_ctsu_self_mfc[] is set by Open API.
 
ctsu_data_t * p_self_data
 Pointer to Self moving average data. g_ctsu_self_data[] is set by Open API.
 
ctsu_mutual_buf_tp_mutual_raw
 Pointer to Mutual raw data. g_ctsu_mutual_raw[] is set by Open API.
 
uint16_t * p_mutual_pri_corr
 Pointer to Mutual primary correction data. g_ctsu_mutual_pri_corr[] is set by Open API.
 
uint16_t * p_mutual_snd_corr
 Pointer to Mutual secondary correction data. g_ctsu_mutual_snd_corr[] is set by Open API.
 
uint16_t * p_mutual_pri_mfc
 Pointer to Mutual primary multi frequency correction data. g_ctsu_pri_mutual_mfc[] is set by Open API.
 
uint16_t * p_mutual_snd_mfc
 Pointer to Mutual secondary multi frequency correction data. g_ctsu_snd_mutual_mfc[] is set by Open API.
 
ctsu_data_t * p_mutual_pri_data
 Pointer to Mutual primary moving average data. g_ctsu_mutual_pri_data[] is set by Open API.
 
ctsu_data_t * p_mutual_snd_data
 Pointer to Mutual secondary moving average data. g_ctsu_mutual_snd_data[] is set by Open API.
 
ctsu_correction_info_tp_correction_info
 Pointer to correction info.
 
ctsu_txvsel_t txvsel
 CTSU Transmission Power Supply Select.
 
ctsu_txvsel2_t txvsel2
 CTSU Transmission Power Supply Select 2 (CTSU2)
 
uint8_t ctsuchac0
 TS00-TS07 enable mask.
 
uint8_t ctsuchac1
 TS08-TS15 enable mask.
 
uint8_t ctsuchac2
 TS16-TS23 enable mask.
 
uint8_t ctsuchac3
 TS24-TS31 enable mask.
 
uint8_t ctsuchac4
 TS32-TS39 enable mask.
 
uint8_t ctsuchtrc0
 TS00-TS07 mutual-tx mask.
 
uint8_t ctsuchtrc1
 TS08-TS15 mutual-tx mask.
 
uint8_t ctsuchtrc2
 TS16-TS23 mutual-tx mask.
 
uint8_t ctsuchtrc3
 TS24-TS31 mutual-tx mask.
 
uint8_t ctsuchtrc4
 TS32-TS39 mutual-tx mask.
 
uint16_t self_elem_index
 self element index number for Current instance.
 
uint16_t mutual_elem_index
 mutual element index number for Current instance.
 
uint16_t ctsu_elem_index
 CTSU element index number for Current instance.
 
ctsu_cfg_t const * p_ctsu_cfg
 Pointer to initial configurations.
 
IRQn_Type write_irq
 Copy from config by Open API. CTSU_CTSUWR interrupt vector.
 
IRQn_Type read_irq
 Copy from config by Open API. CTSU_CTSURD interrupt vector.
 
IRQn_Type end_irq
 Copy from config by Open API. CTSU_CTSUFN interrupt vector.
 
void(* p_callback )(ctsu_callback_args_t *)
 Callback provided when a CTSUFN occurs.
 
uint8_t interrupt_reverse_flag
 Flag in which read interrupt and end interrupt are reversed.
 
ctsu_event_t error_status
 error status variable to send to QE for serial tuning.
 
ctsu_callback_args_tp_callback_memory
 Pointer to non-secure memory that can be used to pass arguments to a callback in non-secure memory.
 
void * p_context
 Placeholder for user data.
 
bool serial_tuning_enable
 Flag of serial tuning status.
 
uint16_t serial_tuning_mutual_cnt
 Word index into ctsuwr register array.
 
uint16_t tuning_self_target_value
 Target self value for initial offset tuning.
 
uint16_t tuning_mutual_target_value
 Target mutual value for initial offset tuning.
 
uint8_t tsod
 Copy from tsod by Open API.
 
uint8_t mec_ts
 Copy from mec_ts by Open API.
 
uint8_t mec_shield_ts
 Copy from mec_shield_ts by Open API.
 

Enumeration Type Documentation

◆ ctsu_state_t

CTSU run state

Enumerator
CTSU_STATE_INIT 

Not open.

CTSU_STATE_IDLE 

Opened.

CTSU_STATE_SCANNING 

Scanning now.

CTSU_STATE_SCANNED 

Scan end.

◆ ctsu_tuning_t

CTSU Initial offset tuning status

Enumerator
CTSU_TUNING_INCOMPLETE 

Initial offset tuning incomplete.

CTSU_TUNING_COMPLETE 

Initial offset tuning complete.

◆ ctsu_correction_status_t

CTSU Correction status

Enumerator
CTSU_CORRECTION_INIT 

Correction initial status.

CTSU_CORRECTION_RUN 

Correction scan running.

CTSU_CORRECTION_COMPLETE 

Correction complete.

CTSU_CORRECTION_ERROR 

Correction error.

◆ ctsu_range_t

CTSU range definition

Enumerator
CTSU_RANGE_20UA 

20uA mode

CTSU_RANGE_40UA 

40uA mode

CTSU_RANGE_80UA 

80uA mode

CTSU_RANGE_160UA 

160uA mode

CTSU_RANGE_NUM 

number of range

Function Documentation

◆ R_CTSU_Open()

fsp_err_t R_CTSU_Open ( ctsu_ctrl_t *const  p_ctrl,
ctsu_cfg_t const *const  p_cfg 
)

Opens and configures the CTSU driver module. Implements ctsu_api_t::open.

Example:

err = R_CTSU_Open(&g_ctsu_ctrl, &g_ctsu_cfg);
Return values
FSP_SUCCESSCTSU successfully configured.
FSP_ERR_ASSERTIONNull pointer, or one or more configuration options is invalid.
FSP_ERR_ALREADY_OPENModule is already open. This module can only be opened once.
FSP_ERR_INVALID_ARGUMENTConfiguration parameter error.
Note
  • In the first Open, measurement for correction works, and it takes several tens of milliseconds.
  • When the touch interface configuration is in diagnosis mode, execute the R_CTSU_Open() of the other touch interface configuration first.

◆ R_CTSU_ScanStart()

fsp_err_t R_CTSU_ScanStart ( ctsu_ctrl_t *const  p_ctrl)

This function should be called each time a periodic timer expires. If initial offset tuning is enabled, The first several calls are used to tuning for the sensors. Before starting the next scan, first get the data with R_CTSU_DataGet(). If a different control block scan should be run, check the scan is complete before executing. Implements ctsu_api_t::scanStart.

Example:

while (true)
{
err = R_CTSU_ScanStart(&g_ctsu_ctrl);
assert(FSP_SUCCESS == err);
while (!g_scan_flag)
{
/* Wait for scan end callback */
}
g_scan_flag = false;
err = R_CTSU_DataGet(&g_ctsu_ctrl, data);
if (FSP_SUCCESS == err)
{
/* Application specific data processing. */
}
}
Return values
FSP_SUCCESSCTSU successfully configured.
FSP_ERR_ASSERTIONNull pointer passed as a parameter.
FSP_ERR_NOT_OPENModule is not open.
FSP_ERR_CTSU_SCANNINGScanning this instance or other.
FSP_ERR_CTSU_NOT_GET_DATAThe previous data has not been retrieved by DataGet.

◆ R_CTSU_DataGet()

fsp_err_t R_CTSU_DataGet ( ctsu_ctrl_t *const  p_ctrl,
uint16_t *  p_data 
)

This function gets the sensor values as scanned by the CTSU. If initial offset tuning is enabled, The first several calls are used to tuning for the sensors. This function reads all previously corrected measured values into the specified buffer(p_data). CTSU1: The value passed through sensor CCO correction and moving average. CTSU2 JMM: The value passed through sensor CCO correction and moving average. CTSU2 VMM: Sensor passed through sensor CCO correction, frequency correction and moving average. The required buffer size varies depending on the measurement mode. Prepare the number of TS for the self-capacitance measurement and current measurement modes, and twice the number of matrixes for the mutual-capacitance measurement mode. In the case of CTSU2 JMM, data of 3 frequencies is stored, so prepare 3 times more. In diagnosis mode, if data collection has not been completed, the function returns FSP_ERR_CTSU_DIAG_NOT_YET. If data collection is completed, it will return FSP_SUCCESS, so please call R_CTSU_Diagnosis() to check the diagnosis result. In addition, if an error occurs in ADC measurement during the output‑voltage diagnosis, the function returns FSP_ERR_ABORTED. Implements ctsu_api_t::dataGet.

Example:

while (true)
{
err = R_CTSU_ScanStart(&g_ctsu_ctrl);
assert(FSP_SUCCESS == err);
while (!g_scan_flag)
{
/* Wait for scan end callback */
}
g_scan_flag = false;
err = R_CTSU_DataGet(&g_ctsu_ctrl, data);
if (FSP_SUCCESS == err)
{
/* Application specific data processing. */
}
}
Return values
FSP_SUCCESSCTSU successfully configured.
FSP_ERR_ASSERTIONNull pointer passed as a parameter.
FSP_ERR_NOT_OPENModule is not open.
FSP_ERR_CTSU_SCANNINGScanning this instance.
FSP_ERR_CTSU_INCOMPLETE_TUNINGIncomplete initial offset tuning.
FSP_ERR_CTSU_DIAG_NOT_YETDiagnosis of data collected no yet.
FSP_ERR_INVALID_MODEThe mode of automatic judgement on is invalid.
FSP_ERR_ABORTEDOperate error of Diagnosis ADC data collection, since ADC use other
Note
In diagnosis mode, calling R_CTSU_DataGet() executes internal diagnostic processing, and a dummy value is returned in p_data.

◆ R_CTSU_AutoJudgementDataGet()

fsp_err_t R_CTSU_AutoJudgementDataGet ( ctsu_ctrl_t *const  p_ctrl,
uint64_t *  p_button_status 
)

This function gets the result of automatic judgement button. Call after the scan is completed. The result is 64-bit bitmaps and is stored in order of TS number for specified ctsu control. After the initial judgement, the baseline initialization bit is set and the automatic judgement threshold is set. This function is called only for automatic judgement. Implements ctsu_api_t::autoJudgementDataGet.

Example:

/* Main loop */
while (true)
{
R_CTSU_ScanStart(&g_ctsu_ctrl);
while (!g_scan_flag)
{
/* Wait for scan end callback */
}
g_scan_flag = false;
err = R_CTSU_AutoJudgementDataGet(&g_ctsu_ctrl, &button_status);
if (FSP_SUCCESS == err)
{
/* Application specific data processing. */
}
}
Return values
FSP_SUCCESSCTSU successfully configured.
FSP_ERR_ASSERTIONNull pointer passed as a parameter.
FSP_ERR_NOT_OPENModule is not open.
FSP_ERR_CTSU_SCANNINGScanning this instance.
FSP_ERR_INVALID_MODEThe mode of automatic judgement off is invalid.
Note
This function is only supported by CTSU2SLa.

◆ R_CTSU_OffsetTuning()

fsp_err_t R_CTSU_OffsetTuning ( ctsu_ctrl_t *const  p_ctrl)

This function tunes the offset register(SO). Call after the measurement is completed. If the return value is FSP_ERR_CTSU_INCOMPLETE_TUNING, tuning is not complete. Execute the measurement and this function call routine until the return value becomes FSP_SUCCESS. It is recommended to run this routine after R_CTSU_Open(). It can be recalled and tuned again. When the automatic judgement is enabled, after the offset tuning is completed,the baseline initialization bit flag is set. Implements ctsu_api_t::offsetTuning.

Example:

/* Initial offset tuning */
do
{
R_CTSU_ScanStart(&g_ctsu_ctrl);
while (!g_scan_flag)
{
/* Wait for scan end callback */
}
g_scan_flag = false;
err = R_CTSU_OffsetTuning(&g_ctsu_ctrl);
} while (FSP_SUCCESS != err);
while (true)
{
R_CTSU_ScanStart(&g_ctsu_ctrl);
while (!g_scan_flag)
{
/* Wait for scan end callback */
}
g_scan_flag = false;
err = R_CTSU_DataGet(&g_ctsu_ctrl, data);
assert(FSP_SUCCESS == err);
if (FSP_SUCCESS == err)
{
/* Re-offset tuning is performed when the parasitic capacitance changes significantly due */
/* to changes in the surrounding environment and the count value becomes an abnormal value. */
/* */
/* if (abnormal value detection conditions) */
/* { */
/* Re-offset tuning */
do
{
R_CTSU_ScanStart(&g_ctsu_ctrl);
while (!g_scan_flag)
{
/* Wait for scan end callback */
}
g_scan_flag = false;
err = R_CTSU_OffsetTuning(&g_ctsu_ctrl);
} while (FSP_SUCCESS != err);
/* } */
}
}
Return values
FSP_SUCCESSCTSU successfully configured.
FSP_ERR_ASSERTIONNull pointer passed as a parameter.
FSP_ERR_NOT_OPENModule is not open.
FSP_ERR_CTSU_SCANNINGScanning this instance.
FSP_ERR_CTSU_INCOMPLETE_TUNINGIncomplete initial offset tuning.

◆ R_CTSU_ScanStop()

fsp_err_t R_CTSU_ScanStop ( ctsu_ctrl_t *const  p_ctrl)

This function scan stops the sensor as scanning by the CTSU. Implements ctsu_api_t::scanStop.

Return values
FSP_SUCCESSCTSU successfully scan stop.
FSP_ERR_ASSERTIONNull pointer passed as a parameter.
FSP_ERR_NOT_OPENModule is not open.
Note
When using this API together with the diagnosis mode, please note the following: Do not call this API between executing R_CTSU_ScanStart() and calling R_CTSU_DataGet().

◆ R_CTSU_CallbackSet()

fsp_err_t R_CTSU_CallbackSet ( ctsu_ctrl_t *const  p_api_ctrl,
void(*)(ctsu_callback_args_t *)  p_callback,
void *const  p_context,
ctsu_callback_args_t *const  p_callback_memory 
)

Updates the user callback and has option of providing memory for callback structure. Implements ctsu_api_t::callbackSet

Return values
FSP_SUCCESSCallback updated successfully.
FSP_ERR_ASSERTIONA required pointer is NULL.
FSP_ERR_NOT_OPENThe control block has not been opened.
FSP_ERR_NO_CALLBACK_MEMORYp_callback is non-secure and p_callback_memory is either secure or NULL.

◆ R_CTSU_Close()

fsp_err_t R_CTSU_Close ( ctsu_ctrl_t *const  p_ctrl)

Disables specified CTSU control block. Implements ctsu_api_t::close.

Return values
FSP_SUCCESSCTSU successfully configured.
FSP_ERR_ASSERTIONNull pointer passed as a parameter.
FSP_ERR_NOT_OPENModule is not open.
Note
When using this API together with the diagnosis mode, please note the following: Do not call this API between executing R_CTSU_ScanStart() and calling R_CTSU_DataGet().

◆ R_CTSU_SpecificDataGet()

fsp_err_t R_CTSU_SpecificDataGet ( ctsu_ctrl_t *const  p_ctrl,
uint16_t *  p_specific_data,
ctsu_specific_data_type_t  specific_data_type 
)

This function gets the sensor specific data values as scanned by the CTSU. Call this function after calling the R_CTSU_DataGet() function.

By setting the third argument to CTSU_SPECIFIC_RAW_DATA, RAW data can be output from the second argument.

By setting the third argument to CTSU_SPECIFIC_CCO_CORRECTION_DATA, the cco corrected data can be output from the second argument.

By setting the third argument to CTSU_SPECIFIC_CORRECTION_DATA, the frequency corrected data can be output from the second argument. (CTSU2 VMM)

By setting the third argument to CTSU_SPECIFIC_SELECTED_FREQ, Get bitmap of the frequency values used in majority decision from the second argument. (CTSU2 VMM) The bitmap is shown as follows.

2bit 1bit 0bit
3rd frequency value 2nd frequency value 1st frequency value

Implements ctsu_api_t::specificDataGet.

Example:

err = R_CTSU_SpecificDataGet(&g_ctsu_ctrl, corr_data, CTSU_SPECIFIC_CORRECTION_DATA);
Return values
FSP_SUCCESSCTSU successfully configured.
FSP_ERR_ASSERTIONNull pointer passed as a parameter.
FSP_ERR_NOT_OPENModule is not open.
FSP_ERR_CTSU_SCANNINGScanning this instance.
FSP_ERR_CTSU_INCOMPLETE_TUNINGIncomplete initial offset tuning.
FSP_ERR_NOT_ENABLEDSpecify unsupported types.
Note
When the specific_data_type is set to something other than CTSU_SPECIFIC_RAW_DATA, execute this API after calling R_CTSU_DataGet().

◆ R_CTSU_DataInsert()

fsp_err_t R_CTSU_DataInsert ( ctsu_ctrl_t *const  p_ctrl,
uint16_t *  p_insert_data 
)

This function inserts the value of the second argument as the measurement result value. Call this function after calling the R_CTSU_DataInsert() function. Implements ctsu_api_t::dataInsert.

Example:

err = R_CTSU_DataInsert(&g_ctsu_ctrl, filter_data);
Return values
FSP_SUCCESSCTSU successfully configured.
FSP_ERR_ASSERTIONNull pointer passed as a parameter.
FSP_ERR_NOT_OPENModule is not open.
FSP_ERR_CTSU_SCANNINGScanning this instance.
FSP_ERR_CTSU_INCOMPLETE_TUNINGIncomplete initial offset tuning.

◆ R_CTSU_Diagnosis()

fsp_err_t R_CTSU_Diagnosis ( ctsu_ctrl_t *const  p_ctrl)

Diagnosis the CTSU peripheral. Please call the function when the return value of R_CTSU_DataGet is FSP_SUCCESS. If an abnormality is detected in any of the diagnosis items, the corresponding diagnosis error is returned. If all diagnosis is complete normally, FSP_SUCCESS is returned. Implements ctsu_api_t::diagnosis.

Example:

err = R_CTSU_Diagnosis(&g_ctsu_ctrl_diagnosis);
assert(FSP_SUCCESS == err);
Return values
FSP_SUCCESSCTSU successfully configured.
FSP_ERR_ASSERTIONNull pointer passed as a parameter.
FSP_ERR_NOT_OPENModule is not open.
FSP_ERR_CTSU_NOT_GET_DATAThe previous data has not been retrieved by DataGet.
FSP_ERR_CTSU_DIAG_LDO_OVER_VOLTAGEDiagnosis of LDO over voltage failed.
FSP_ERR_CTSU_DIAG_CCO_HIGHDiagnosis of CCO into 19.2uA failed.
FSP_ERR_CTSU_DIAG_CCO_LOWDiagnosis of CCO into 2.4uA failed.
FSP_ERR_CTSU_DIAG_SSCGDiagnosis of SSCG frequency failed.
FSP_ERR_CTSU_DIAG_DACDiagnosis of non-touch count value failed.
FSP_ERR_CTSU_DIAG_OUTPUT_VOLTAGEDiagnosis of LDO output voltage failed.
FSP_ERR_CTSU_DIAG_OVER_VOLTAGEDiagnosis of over voltage detection circuit failed.
FSP_ERR_CTSU_DIAG_OVER_CURRENTDiagnosis of over current detection circuit failed.
FSP_ERR_CTSU_DIAG_LOAD_RESISTANCEDiagnosis of LDO internal resistance value failed.
FSP_ERR_CTSU_DIAG_CURRENT_SOURCEDiagnosis of LDO internal resistance value failed.
FSP_ERR_CTSU_DIAG_SENSCLK_GAINDiagnosis of SENSCLK frequency gain failed.
FSP_ERR_CTSU_DIAG_SUCLK_GAINDiagnosis of SUCLK frequency gain failed.
FSP_ERR_CTSU_DIAG_CLOCK_RECOVERYDiagnosis of SUCLK clock recovery function failed.
FSP_ERR_CTSU_DIAG_CFC_GAINDiagnosis of CFC oscillator gain failed.