From c2354ab7d04f9f59ff44f260ebda8d315bf82bce Mon Sep 17 00:00:00 2001 From: Quy Tran Date: Tue, 1 Jul 2025 15:14:48 +0700 Subject: [PATCH 1/2] hal: renesas: rx: Add CTSU driver support for RX130 target Add CTSU HAL driver source support for HAL Renesas RX Signed-off-by: Quy Tran --- drivers/rx/CMakeLists.txt | 11 + .../rx/rdp/src/r_bsp/mcu/all/fsp_common_api.h | 351 + .../rx/rdp/src/r_bsp/mcu/all/r_fsp_error.h | 60 + drivers/rx/rdp/src/r_ctsu_qe/inc/r_ctsu_qe.h | 473 + .../rx/rdp/src/r_ctsu_qe/inc/r_ctsu_qe_api.h | 343 + .../rdp/src/r_ctsu_qe/inc/r_ctsu_qe_pinset.h | 30 + drivers/rx/rdp/src/r_ctsu_qe/r_ctsu_qe_if.h | 41 + drivers/rx/rdp/src/r_ctsu_qe/src/r_ctsu_qe.c | 8073 +++++++++++++++++ .../rdp/src/r_ctsu_qe/src/r_ctsu_qe_pinset.c | 94 + zephyr/rx/rdp_cfg/r_config/r_ctsu_qe_config.h | 255 + 10 files changed, 9731 insertions(+) create mode 100644 drivers/rx/rdp/src/r_bsp/mcu/all/fsp_common_api.h create mode 100644 drivers/rx/rdp/src/r_bsp/mcu/all/r_fsp_error.h create mode 100644 drivers/rx/rdp/src/r_ctsu_qe/inc/r_ctsu_qe.h create mode 100644 drivers/rx/rdp/src/r_ctsu_qe/inc/r_ctsu_qe_api.h create mode 100644 drivers/rx/rdp/src/r_ctsu_qe/inc/r_ctsu_qe_pinset.h create mode 100644 drivers/rx/rdp/src/r_ctsu_qe/r_ctsu_qe_if.h create mode 100644 drivers/rx/rdp/src/r_ctsu_qe/src/r_ctsu_qe.c create mode 100644 drivers/rx/rdp/src/r_ctsu_qe/src/r_ctsu_qe_pinset.c create mode 100644 zephyr/rx/rdp_cfg/r_config/r_ctsu_qe_config.h diff --git a/drivers/rx/CMakeLists.txt b/drivers/rx/CMakeLists.txt index df0949a2..68cda01c 100644 --- a/drivers/rx/CMakeLists.txt +++ b/drivers/rx/CMakeLists.txt @@ -136,3 +136,14 @@ if(CONFIG_USE_RX_RDP_LVD) rdp/src/r_lvd_rx/src rdp/src/r_lvd_rx/src/targets/${CONFIG_SOC_SERIES}) endif() + +if(CONFIG_USE_RX_RDP_CTSU) + zephyr_library_sources( + rdp/src/r_ctsu_qe/src/r_ctsu_qe.c + rdp/src/r_ctsu_qe/src/r_ctsu_qe_pinset.c + ) + zephyr_include_directories( + rdp/src/r_ctsu_qe + rdp/src/r_ctsu_qe/inc + ) +endif() diff --git a/drivers/rx/rdp/src/r_bsp/mcu/all/fsp_common_api.h b/drivers/rx/rdp/src/r_bsp/mcu/all/fsp_common_api.h new file mode 100644 index 00000000..c784a37d --- /dev/null +++ b/drivers/rx/rdp/src/r_bsp/mcu/all/fsp_common_api.h @@ -0,0 +1,351 @@ +/* +* Copyright (c) 2011 Renesas Electronics Corporation and/or its affiliates +* +* SPDX-License-Identifier: BSD-3-Clause +*/ + +#ifndef FSP_COMMON_API_H +#define FSP_COMMON_API_H + +/*********************************************************************************************************************** + * Includes + **********************************************************************************************************************/ +#include +#include + +/*******************************************************************************************************************//** + * @ingroup RENESAS_COMMON + * @defgroup RENESAS_ERROR_CODES Common Error Codes + * All FSP modules share these common error codes. + * @{ + **********************************************************************************************************************/ + +/********************************************************************************************************************** + * Macro definitions + **********************************************************************************************************************/ + +/** This macro is used to suppress compiler messages about a parameter not being used in a function. The nice thing + * about using this implementation is that it does not take any extra RAM or ROM. */ + +#define FSP_PARAMETER_NOT_USED(p) (void) ((p)) + +/** Determine if a C++ compiler is being used. + * If so, ensure that standard C is used to process the API information. */ +#if defined(__cplusplus) + #define FSP_CPP_HEADER extern "C" { + #define FSP_CPP_FOOTER } +#else + #define FSP_CPP_HEADER + #define FSP_CPP_FOOTER +#endif + +/** FSP Header and Footer definitions */ +#define FSP_HEADER FSP_CPP_HEADER +#define FSP_FOOTER FSP_CPP_FOOTER + +/** Macro to be used when argument to function is ignored since function call is NSC and the parameter is statically + * defined on the Secure side. */ +#define FSP_SECURE_ARGUMENT (NULL) + +/********************************************************************************************************************** + * Typedef definitions + **********************************************************************************************************************/ + +/** Common error codes */ +typedef enum e_fsp_err +{ + FSP_SUCCESS = 0, + + FSP_ERR_ASSERTION = 1, ///< A critical assertion has failed + FSP_ERR_INVALID_POINTER = 2, ///< Pointer points to invalid memory location + FSP_ERR_INVALID_ARGUMENT = 3, ///< Invalid input parameter + FSP_ERR_INVALID_CHANNEL = 4, ///< Selected channel does not exist + FSP_ERR_INVALID_MODE = 5, ///< Unsupported or incorrect mode + FSP_ERR_UNSUPPORTED = 6, ///< Selected mode not supported by this API + FSP_ERR_NOT_OPEN = 7, ///< Requested channel is not configured or API not open + FSP_ERR_IN_USE = 8, ///< Channel/peripheral is running/busy + FSP_ERR_OUT_OF_MEMORY = 9, ///< Allocate more memory in the driver's cfg.h + FSP_ERR_HW_LOCKED = 10, ///< Hardware is locked + FSP_ERR_IRQ_BSP_DISABLED = 11, ///< IRQ not enabled in BSP + FSP_ERR_OVERFLOW = 12, ///< Hardware overflow + FSP_ERR_UNDERFLOW = 13, ///< Hardware underflow + FSP_ERR_ALREADY_OPEN = 14, ///< Requested channel is already open in a different configuration + FSP_ERR_APPROXIMATION = 15, ///< Could not set value to exact result + FSP_ERR_CLAMPED = 16, ///< Value had to be limited for some reason + FSP_ERR_INVALID_RATE = 17, ///< Selected rate could not be met + FSP_ERR_ABORTED = 18, ///< An operation was aborted + FSP_ERR_NOT_ENABLED = 19, ///< Requested operation is not enabled + FSP_ERR_TIMEOUT = 20, ///< Timeout error + FSP_ERR_INVALID_BLOCKS = 21, ///< Invalid number of blocks supplied + FSP_ERR_INVALID_ADDRESS = 22, ///< Invalid address supplied + FSP_ERR_INVALID_SIZE = 23, ///< Invalid size/length supplied for operation + FSP_ERR_WRITE_FAILED = 24, ///< Write operation failed + FSP_ERR_ERASE_FAILED = 25, ///< Erase operation failed + FSP_ERR_INVALID_CALL = 26, ///< Invalid function call is made + FSP_ERR_INVALID_HW_CONDITION = 27, ///< Detected hardware is in invalid condition + FSP_ERR_INVALID_FACTORY_FLASH = 28, ///< Factory flash is not available on this MCU + FSP_ERR_INVALID_STATE = 30, ///< API or command not valid in the current state + FSP_ERR_NOT_ERASED = 31, ///< Erase verification failed + FSP_ERR_SECTOR_RELEASE_FAILED = 32, ///< Sector release failed + FSP_ERR_NOT_INITIALIZED = 33, ///< Required initialization not complete + FSP_ERR_NOT_FOUND = 34, ///< The requested item could not be found + FSP_ERR_NO_CALLBACK_MEMORY = 35, ///< Non-secure callback memory not provided for non-secure callback + FSP_ERR_BUFFER_EMPTY = 36, ///< No data available in buffer + + /* Start of RTOS only error codes */ + FSP_ERR_INTERNAL = 100, ///< Internal error + FSP_ERR_WAIT_ABORTED = 101, ///< Wait aborted + + /* Start of UART specific */ + FSP_ERR_FRAMING = 200, ///< Framing error occurs + FSP_ERR_BREAK_DETECT = 201, ///< Break signal detects + FSP_ERR_PARITY = 202, ///< Parity error occurs + FSP_ERR_RXBUF_OVERFLOW = 203, ///< Receive queue overflow + FSP_ERR_QUEUE_UNAVAILABLE = 204, ///< Can't open s/w queue + FSP_ERR_INSUFFICIENT_SPACE = 205, ///< Not enough space in transmission circular buffer + FSP_ERR_INSUFFICIENT_DATA = 206, ///< Not enough data in receive circular buffer + + /* Start of SPI specific */ + FSP_ERR_TRANSFER_ABORTED = 300, ///< The data transfer was aborted. + FSP_ERR_MODE_FAULT = 301, ///< Mode fault error. + FSP_ERR_READ_OVERFLOW = 302, ///< Read overflow. + FSP_ERR_SPI_PARITY = 303, ///< Parity error. + FSP_ERR_OVERRUN = 304, ///< Overrun error. + + /* Start of CGC Specific */ + FSP_ERR_CLOCK_INACTIVE = 400, ///< Inactive clock specified as system clock. + FSP_ERR_CLOCK_ACTIVE = 401, ///< Active clock source cannot be modified without stopping first. + FSP_ERR_NOT_STABILIZED = 403, ///< Clock has not stabilized after its been turned on/off + FSP_ERR_PLL_SRC_INACTIVE = 404, ///< PLL initialization attempted when PLL source is turned off + FSP_ERR_OSC_STOP_DET_ENABLED = 405, ///< Illegal attempt to stop LOCO when Oscillation stop is enabled + FSP_ERR_OSC_STOP_DETECTED = 406, ///< The Oscillation stop detection status flag is set + FSP_ERR_OSC_STOP_CLOCK_ACTIVE = 407, ///< Attempt to clear Oscillation Stop Detect Status with PLL/MAIN_OSC active + FSP_ERR_CLKOUT_EXCEEDED = 408, ///< Output on target output clock pin exceeds maximum supported limit + FSP_ERR_USB_MODULE_ENABLED = 409, ///< USB clock configure request with USB Module enabled + FSP_ERR_HARDWARE_TIMEOUT = 410, ///< A register read or write timed out + FSP_ERR_LOW_VOLTAGE_MODE = 411, ///< Invalid clock setting attempted in low voltage mode + + /* Start of FLASH Specific */ + FSP_ERR_PE_FAILURE = 500, ///< Unable to enter Programming mode. + FSP_ERR_CMD_LOCKED = 501, ///< Peripheral in command locked state + FSP_ERR_FCLK = 502, ///< FCLK must be >= 4 MHz + FSP_ERR_INVALID_LINKED_ADDRESS = 503, ///< Function or data are linked at an invalid region of memory + FSP_ERR_BLANK_CHECK_FAILED = 504, ///< Blank check operation failed + + /* Start of CAC Specific */ + FSP_ERR_INVALID_CAC_REF_CLOCK = 600, ///< Measured clock rate < reference clock rate + + /* Start of GLCD Specific */ + FSP_ERR_CLOCK_GENERATION = 1000, ///< Clock cannot be specified as system clock + FSP_ERR_INVALID_TIMING_SETTING = 1001, ///< Invalid timing parameter + FSP_ERR_INVALID_LAYER_SETTING = 1002, ///< Invalid layer parameter + FSP_ERR_INVALID_ALIGNMENT = 1003, ///< Invalid memory alignment found + FSP_ERR_INVALID_GAMMA_SETTING = 1004, ///< Invalid gamma correction parameter + FSP_ERR_INVALID_LAYER_FORMAT = 1005, ///< Invalid color format in layer + FSP_ERR_INVALID_UPDATE_TIMING = 1006, ///< Invalid timing for register update + FSP_ERR_INVALID_CLUT_ACCESS = 1007, ///< Invalid access to CLUT entry + FSP_ERR_INVALID_FADE_SETTING = 1008, ///< Invalid fade-in/fade-out setting + FSP_ERR_INVALID_BRIGHTNESS_SETTING = 1009, ///< Invalid gamma correction parameter + + /* Start of JPEG Specific */ + FSP_ERR_JPEG_ERR = 1100, ///< JPEG error + FSP_ERR_JPEG_SOI_NOT_DETECTED = 1101, ///< SOI not detected until EOI detected. + FSP_ERR_JPEG_SOF1_TO_SOFF_DETECTED = 1102, ///< SOF1 to SOFF detected. + FSP_ERR_JPEG_UNSUPPORTED_PIXEL_FORMAT = 1103, ///< Unprovided pixel format detected. + FSP_ERR_JPEG_SOF_ACCURACY_ERROR = 1104, ///< SOF accuracy error: other than 8 detected. + FSP_ERR_JPEG_DQT_ACCURACY_ERROR = 1105, ///< DQT accuracy error: other than 0 detected. + FSP_ERR_JPEG_COMPONENT_ERROR1 = 1106, ///< Component error 1: the number of SOF0 header components detected is other than 1, 3, or 4. + FSP_ERR_JPEG_COMPONENT_ERROR2 = 1107, ///< Component error 2: the number of components differs between SOF0 header and SOS. + FSP_ERR_JPEG_SOF0_DQT_DHT_NOT_DETECTED = 1108, ///< SOF0, DQT, and DHT not detected when SOS detected. + FSP_ERR_JPEG_SOS_NOT_DETECTED = 1109, ///< SOS not detected: SOS not detected until EOI detected. + FSP_ERR_JPEG_EOI_NOT_DETECTED = 1110, ///< EOI not detected (default) + FSP_ERR_JPEG_RESTART_INTERVAL_DATA_NUMBER_ERROR = 1111, ///< Restart interval data number error detected. + FSP_ERR_JPEG_IMAGE_SIZE_ERROR = 1112, ///< Image size error detected. + FSP_ERR_JPEG_LAST_MCU_DATA_NUMBER_ERROR = 1113, ///< Last MCU data number error detected. + FSP_ERR_JPEG_BLOCK_DATA_NUMBER_ERROR = 1114, ///< Block data number error detected. + FSP_ERR_JPEG_BUFFERSIZE_NOT_ENOUGH = 1115, ///< User provided buffer size not enough + FSP_ERR_JPEG_UNSUPPORTED_IMAGE_SIZE = 1116, ///< JPEG Image size is not aligned with MCU + + /* Start of touch panel framework specific */ + FSP_ERR_CALIBRATE_FAILED = 1200, ///< Calibration failed + + /* Start of IP specific */ + FSP_ERR_IP_HARDWARE_NOT_PRESENT = 1400, ///< Requested IP does not exist on this device + FSP_ERR_IP_UNIT_NOT_PRESENT = 1401, ///< Requested unit does not exist on this device + FSP_ERR_IP_CHANNEL_NOT_PRESENT = 1402, ///< Requested channel does not exist on this device + + /* Start of USB specific */ + FSP_ERR_USB_FAILED = 1500, + FSP_ERR_USB_BUSY = 1501, + FSP_ERR_USB_SIZE_SHORT = 1502, + FSP_ERR_USB_SIZE_OVER = 1503, + FSP_ERR_USB_NOT_OPEN = 1504, + FSP_ERR_USB_NOT_SUSPEND = 1505, + FSP_ERR_USB_PARAMETER = 1506, + + /* Start of Message framework specific */ + FSP_ERR_NO_MORE_BUFFER = 2000, ///< No more buffer found in the memory block pool + FSP_ERR_ILLEGAL_BUFFER_ADDRESS = 2001, ///< Buffer address is out of block memory pool + FSP_ERR_INVALID_WORKBUFFER_SIZE = 2002, ///< Work buffer size is invalid + FSP_ERR_INVALID_MSG_BUFFER_SIZE = 2003, ///< Message buffer size is invalid + FSP_ERR_TOO_MANY_BUFFERS = 2004, ///< Number of buffer is too many + FSP_ERR_NO_SUBSCRIBER_FOUND = 2005, ///< No message subscriber found + FSP_ERR_MESSAGE_QUEUE_EMPTY = 2006, ///< No message found in the message queue + FSP_ERR_MESSAGE_QUEUE_FULL = 2007, ///< No room for new message in the message queue + FSP_ERR_ILLEGAL_SUBSCRIBER_LISTS = 2008, ///< Message subscriber lists is illegal + FSP_ERR_BUFFER_RELEASED = 2009, ///< Buffer has been released + + /* Start of 2DG Driver specific */ + FSP_ERR_D2D_ERROR_INIT = 3000, ///< D/AVE 2D has an error in the initialization + FSP_ERR_D2D_ERROR_DEINIT = 3001, ///< D/AVE 2D has an error in the initialization + FSP_ERR_D2D_ERROR_RENDERING = 3002, ///< D/AVE 2D has an error in the rendering + FSP_ERR_D2D_ERROR_SIZE = 3003, ///< D/AVE 2D has an error in the rendering + + /* Start of ETHER Driver specific */ + FSP_ERR_ETHER_ERROR_NO_DATA = 4000, ///< No Data in Receive buffer. + FSP_ERR_ETHER_ERROR_LINK = 4001, ///< ETHERC/EDMAC has an error in the Auto-negotiation + FSP_ERR_ETHER_ERROR_MAGIC_PACKET_MODE = 4002, ///< As a Magic Packet is being detected, and transmission/reception is not enabled + FSP_ERR_ETHER_ERROR_TRANSMIT_BUFFER_FULL = 4003, ///< Transmit buffer is not empty + FSP_ERR_ETHER_ERROR_FILTERING = 4004, ///< Detect multicast frame when multicast frame filtering enable + FSP_ERR_ETHER_ERROR_PHY_COMMUNICATION = 4005, ///< ETHERC/EDMAC has an error in the phy communication + FSP_ERR_ETHER_RECEIVE_BUFFER_ACTIVE = 4006, ///< Receive buffer is active. + + /* Start of ETHER_PHY Driver specific */ + FSP_ERR_ETHER_PHY_ERROR_LINK = 5000, ///< PHY is not link up. + FSP_ERR_ETHER_PHY_NOT_READY = 5001, ///< PHY has an error in the Auto-negotiation + + /* Start of BYTEQ library specific */ + FSP_ERR_QUEUE_FULL = 10000, ///< Queue is full, cannot queue another data + FSP_ERR_QUEUE_EMPTY = 10001, ///< Queue is empty, no data to dequeue + + /* Start of CTSU Driver specific */ + FSP_ERR_CTSU_SCANNING = 6000, ///< Scanning. + FSP_ERR_CTSU_NOT_GET_DATA = 6001, ///< Not processed previous scan data. + FSP_ERR_CTSU_INCOMPLETE_TUNING = 6002, ///< Incomplete initial offset tuning. + FSP_ERR_CTSU_DIAG_NOT_YET = 6003, ///< Diagnosis of data collected no yet. + FSP_ERR_CTSU_DIAG_LDO_OVER_VOLTAGE = 6004, ///< Diagnosis of LDO over voltage failed. + FSP_ERR_CTSU_DIAG_CCO_HIGH = 6005, ///< Diagnosis of CCO into 19.2uA failed. + FSP_ERR_CTSU_DIAG_CCO_LOW = 6006, ///< Diagnosis of CCO into 2.4uA failed. + FSP_ERR_CTSU_DIAG_SSCG = 6007, ///< Diagnosis of SSCG frequency failed. + FSP_ERR_CTSU_DIAG_DAC = 6008, ///< Diagnosis of non-touch count value failed. + FSP_ERR_CTSU_DIAG_OUTPUT_VOLTAGE = 6009, ///< Diagnosis of LDO output voltage failed. + FSP_ERR_CTSU_DIAG_OVER_VOLTAGE = 6010, ///< Diagnosis of over voltage detection circuit failed. + FSP_ERR_CTSU_DIAG_OVER_CURRENT = 6011, ///< Diagnosis of over current detection circuit failed. + FSP_ERR_CTSU_DIAG_LOAD_RESISTANCE = 6012, ///< Diagnosis of LDO internal resistance value failed. + FSP_ERR_CTSU_DIAG_CURRENT_SOURCE = 6013, ///< Diagnosis of Current source value failed. + FSP_ERR_CTSU_DIAG_SENSCLK_GAIN = 6014, ///< Diagnosis of SENSCLK frequency gain failed. + FSP_ERR_CTSU_DIAG_SUCLK_GAIN = 6015, ///< Diagnosis of SUCLK frequency gain failed. + FSP_ERR_CTSU_DIAG_CLOCK_RECOVERY = 6016, ///< Diagnosis of SUCLK clock recovery function failed. + FSP_ERR_CTSU_DIAG_CFC_GAIN = 6017, ///< Diagnosis of CFC oscillator gain failed. + + /* Start of SDMMC specific */ + FSP_ERR_CARD_INIT_FAILED = 40000, ///< SD card or eMMC device failed to initialize. + FSP_ERR_CARD_NOT_INSERTED = 40001, ///< SD card not installed. + FSP_ERR_DEVICE_BUSY = 40002, ///< Device is holding DAT0 low or another operation is ongoing. + FSP_ERR_CARD_NOT_INITIALIZED = 40004, ///< SD card was removed. + FSP_ERR_CARD_WRITE_PROTECTED = 40005, ///< Media is write protected. + FSP_ERR_TRANSFER_BUSY = 40006, ///< Transfer in progress. + FSP_ERR_RESPONSE = 40007, ///< Card did not respond or responded with an error. + + /* Start of FX_IO specific */ + FSP_ERR_MEDIA_FORMAT_FAILED = 50000, ///< Media format failed. + FSP_ERR_MEDIA_OPEN_FAILED = 50001, ///< Media open failed. + + /* Start of CAN specific */ + FSP_ERR_CAN_DATA_UNAVAILABLE = 60000, ///< No data available. + FSP_ERR_CAN_MODE_SWITCH_FAILED = 60001, ///< Switching operation modes failed. + FSP_ERR_CAN_INIT_FAILED = 60002, ///< Hardware initialization failed. + FSP_ERR_CAN_TRANSMIT_NOT_READY = 60003, ///< Transmit in progress. + FSP_ERR_CAN_RECEIVE_MAILBOX = 60004, ///< Mailbox is setup as a receive mailbox. + FSP_ERR_CAN_TRANSMIT_MAILBOX = 60005, ///< Mailbox is setup as a transmit mailbox. + FSP_ERR_CAN_MESSAGE_LOST = 60006, ///< Receive message has been overwritten or overrun. + + /* Start of SF_WIFI Specific */ + FSP_ERR_WIFI_CONFIG_FAILED = 70000, ///< WiFi module Configuration failed. + FSP_ERR_WIFI_INIT_FAILED = 70001, ///< WiFi module initialization failed. + FSP_ERR_WIFI_TRANSMIT_FAILED = 70002, ///< Transmission failed + FSP_ERR_WIFI_INVALID_MODE = 70003, ///< API called when provisioned in client mode + FSP_ERR_WIFI_FAILED = 70004, ///< WiFi Failed. + FSP_ERR_WIFI_SCAN_COMPLETE = 70005, ///< Wifi scan has completed. + + /* Start of SF_CELLULAR Specific */ + FSP_ERR_CELLULAR_CONFIG_FAILED = 80000, ///< Cellular module Configuration failed. + FSP_ERR_CELLULAR_INIT_FAILED = 80001, ///< Cellular module initialization failed. + FSP_ERR_CELLULAR_TRANSMIT_FAILED = 80002, ///< Transmission failed + FSP_ERR_CELLULAR_FW_UPTODATE = 80003, ///< Firmware is uptodate + FSP_ERR_CELLULAR_FW_UPGRADE_FAILED = 80004, ///< Firmware upgrade failed + FSP_ERR_CELLULAR_FAILED = 80005, ///< Cellular Failed. + FSP_ERR_CELLULAR_INVALID_STATE = 80006, ///< API Called in invalid state. + FSP_ERR_CELLULAR_REGISTRATION_FAILED = 80007, ///< Cellular Network registration failed + + /* Start of SF_BLE specific */ + FSP_ERR_BLE_FAILED = 90001, ///< BLE operation failed + FSP_ERR_BLE_INIT_FAILED = 90002, ///< BLE device initialization failed + FSP_ERR_BLE_CONFIG_FAILED = 90003, ///< BLE device configuration failed + FSP_ERR_BLE_PRF_ALREADY_ENABLED = 90004, ///< BLE device Profile already enabled + FSP_ERR_BLE_PRF_NOT_ENABLED = 90005, ///< BLE device not enabled + + /* Start of SF_BLE_ABS specific */ + FSP_ERR_BLE_ABS_INVALID_OPERATION = 91001, ///< Invalid operation is executed. + FSP_ERR_BLE_ABS_NOT_FOUND = 91002, ///< Valid data or free space is not found. + + /* Start of Crypto specific (0x10000) @note Refer to sf_cryoto_err.h for Crypto error code. */ + FSP_ERR_CRYPTO_CONTINUE = 0x10000, ///< Continue executing function + FSP_ERR_CRYPTO_SCE_RESOURCE_CONFLICT = 0x10001, ///< Hardware resource busy + FSP_ERR_CRYPTO_SCE_FAIL = 0x10002, ///< Internal I/O buffer is not empty + FSP_ERR_CRYPTO_SCE_HRK_INVALID_INDEX = 0x10003, ///< Invalid index + FSP_ERR_CRYPTO_SCE_RETRY = 0x10004, ///< Retry + FSP_ERR_CRYPTO_SCE_VERIFY_FAIL = 0x10005, ///< Verify is failed + FSP_ERR_CRYPTO_SCE_ALREADY_OPEN = 0x10006, ///< HW SCE module is already opened + FSP_ERR_CRYPTO_NOT_OPEN = 0x10007, ///< Hardware module is not initialized + FSP_ERR_CRYPTO_UNKNOWN = 0x10008, ///< Some unknown error occurred + FSP_ERR_CRYPTO_NULL_POINTER = 0x10009, ///< Null pointer input as a parameter + FSP_ERR_CRYPTO_NOT_IMPLEMENTED = 0x1000a, ///< Algorithm/size not implemented + FSP_ERR_CRYPTO_RNG_INVALID_PARAM = 0x1000b, ///< An invalid parameter is specified + FSP_ERR_CRYPTO_RNG_FATAL_ERROR = 0x1000c, ///< A fatal error occurred + FSP_ERR_CRYPTO_INVALID_SIZE = 0x1000d, ///< Size specified is invalid + FSP_ERR_CRYPTO_INVALID_STATE = 0x1000e, ///< Function used in an valid state + FSP_ERR_CRYPTO_ALREADY_OPEN = 0x1000f, ///< control block is already opened + FSP_ERR_CRYPTO_INSTALL_KEY_FAILED = 0x10010, ///< Specified input key is invalid. + FSP_ERR_CRYPTO_AUTHENTICATION_FAILED = 0x10011, ///< Authentication failed + FSP_ERR_CRYPTO_SCE_KEY_SET_FAIL = 0x10012, ///< Failure to Init Cipher + FSP_ERR_CRYPTO_SCE_AUTHENTICATION = 0x10013, ///< Authentication failed + FSP_ERR_CRYPTO_SCE_PARAMETER = 0x10014, ///< Input date is illegal. + FSP_ERR_CRYPTO_SCE_PROHIBIT_FUNCTION = 0x10015, ///< An invalid function call occurred. + + /* Start of Crypto RSIP specific (0x10100) */ + FSP_ERR_CRYPTO_RSIP_RESOURCE_CONFLICT = 0x10100, ///< Hardware resource is busy + FSP_ERR_CRYPTO_RSIP_FATAL = 0x10101, ///< Hardware fatal error or unexpected return + FSP_ERR_CRYPTO_RSIP_FAIL = 0x10102, ///< Internal error + FSP_ERR_CRYPTO_RSIP_KEY_SET_FAIL = 0x10103, ///< Input key type is illegal + FSP_ERR_CRYPTO_RSIP_AUTHENTICATION = 0x10104, ///< Authentication failed + + /* Start of SF_CRYPTO specific */ + FSP_ERR_CRYPTO_COMMON_NOT_OPENED = 0x20000, ///< Crypto Framework Common is not opened + FSP_ERR_CRYPTO_HAL_ERROR = 0x20001, ///< Cryoto HAL module returned an error + FSP_ERR_CRYPTO_KEY_BUF_NOT_ENOUGH = 0x20002, ///< Key buffer size is not enough to generate a key + FSP_ERR_CRYPTO_BUF_OVERFLOW = 0x20003, ///< Attempt to write data larger than what the buffer can hold + FSP_ERR_CRYPTO_INVALID_OPERATION_MODE = 0x20004, ///< Invalid operation mode. + FSP_ERR_MESSAGE_TOO_LONG = 0x20005, ///< Message for RSA encryption is too long. + FSP_ERR_RSA_DECRYPTION_ERROR = 0x20006, ///< RSA Decryption error. + + /** @note SF_CRYPTO APIs may return an error code starting from 0x10000 which is of Crypto module. + * Refer to sf_cryoto_err.h for Crypto error codes. + */ + + /* Start of Sensor specific */ + FSP_ERR_SENSOR_INVALID_DATA = 0x30000, ///< Data is invalid. + FSP_ERR_SENSOR_IN_STABILIZATION = 0x30001, ///< Sensor is stabilizing. + FSP_ERR_SENSOR_MEASUREMENT_NOT_FINISHED = 0x30002, ///< Measurement is not finished. + + /* Start of COMMS specific */ + FSP_ERR_COMMS_BUS_NOT_OPEN = 0x40000, ///< Bus is not open. +} fsp_err_t; + +/** @} */ + +/*********************************************************************************************************************** + * Function prototypes + **********************************************************************************************************************/ + +#endif diff --git a/drivers/rx/rdp/src/r_bsp/mcu/all/r_fsp_error.h b/drivers/rx/rdp/src/r_bsp/mcu/all/r_fsp_error.h new file mode 100644 index 00000000..9bbfa963 --- /dev/null +++ b/drivers/rx/rdp/src/r_bsp/mcu/all/r_fsp_error.h @@ -0,0 +1,60 @@ +/* +* Copyright (c) 2011 Renesas Electronics Corporation and/or its affiliates +* +* SPDX-License-Identifier: BSD-3-Clause +*/ +/*********************************************************************************************************************** +* File Name : r_fsp_error.h +* Description : The user chooses which MCU and board they are developing for in this file. If the board you are using +* is not listed below, please add your own or use the default 'User Board'. +***********************************************************************************************************************/ +/*********************************************************************************************************************** +* History : DD.MM.YYYY Version Description +* : 23.04.2021 1.00 First Release +* : 26.02.2025 1.01 Changed the disclaimer. +***********************************************************************************************************************/ +#ifndef R_FSP_ERROR_H_ +#define R_FSP_ERROR_H_ + + +/*********************************************************************************************************************** + * Includes + **********************************************************************************************************************/ +#include "fsp_common_api.h" + +/*********************************************************************************************************************** + * Macro definitions + **********************************************************************************************************************/ +/** Macro to log and return error without an assertion. */ +#ifndef FSP_RETURN + + #define FSP_RETURN(err) FSP_ERROR_LOG((err)); \ + return err; +#endif + +/** This function is called before returning an error code. To stop on a runtime error, define fsp_error_log in + * user code and do required debugging (breakpoints, stack dump, etc) in this function.*/ + #define FSP_ERROR_LOG(err) + +/** Default assertion calls ::FSP_ERROR_RETURN if condition "a" is false. Used to identify incorrect use of API's in FSP + * functions. */ + #define FSP_ASSERT(a) FSP_ERROR_RETURN((a), FSP_ERR_ASSERTION) + +/** All FSP error codes are returned using this macro. Calls ::FSP_ERROR_LOG function if condition "a" is false. Used + * to identify runtime errors in FSP functions. */ + +#define FSP_ERROR_RETURN(a, err) \ + { \ + if ((a)) \ + { \ + (void) 0; /* Do nothing */ \ + } \ + else \ + { \ + FSP_ERROR_LOG(err); \ + return err; \ + } \ + } + + +#endif /* R_FSP_ERROR_H_ */ diff --git a/drivers/rx/rdp/src/r_ctsu_qe/inc/r_ctsu_qe.h b/drivers/rx/rdp/src/r_ctsu_qe/inc/r_ctsu_qe.h new file mode 100644 index 00000000..d18dda6f --- /dev/null +++ b/drivers/rx/rdp/src/r_ctsu_qe/inc/r_ctsu_qe.h @@ -0,0 +1,473 @@ +/* +* Copyright (c) 2018 Renesas Electronics Corporation and/or its affiliates +* +* SPDX-License-Identifier: BSD-3-Clause +*/ +/*********************************************************************************************************************** +* File Name : r_ctsu_qe.h +* Description : This file contains the CTSU API and should be included by the application which uses this API. +***********************************************************************************************************************/ + +/*******************************************************************************************************************//** + * @addtogroup CTSU + * @{ + **********************************************************************************************************************/ + +#ifndef R_CTSU_H +#define R_CTSU_H + +#include "platform.h" +#ifdef QE_TOUCH_CONFIGURATION + #include "qe_touch_define.h" +#endif +#include "r_ctsu_qe_config.h" +#include "r_ctsu_qe_api.h" + +/* Common macro for FSP header files. There is also a corresponding FSP_FOOTER macro at the end of this file. */ +FSP_HEADER + +/*********************************************************************************************************************** + * Macro definitions + **********************************************************************************************************************/ +#if defined(BSP_MCU_RX140) || defined(BSP_MCU_RX261) || defined(BSP_MCU_RX260) +#define BSP_FEATURE_CTSU_VERSION (2) +#else +#define BSP_FEATURE_CTSU_VERSION (1) + #endif + + #if (defined(BSP_MCU_RX113) || defined(BSP_MCU_RX130) || defined(BSP_MCU_RX23_ALL)) +#define CTSU_CFG_MCU_PROCESS_MF3 (1) + #endif + #if defined(BSP_MCU_RX671) +#define CTSU_CFG_MCU_PROCESS_40N_PHASE2 (1) + #endif + +#if (defined(BSP_MCU_RX113) || defined(BSP_MCU_RX130)) +#define BSP_FEATURE_CTSU_HAS_TRMR (1) +#endif + +#if (defined(BSP_MCU_RX130)) +#define BSP_FEATURE_CTSU_HAS_TXVSEL (1) +#endif + +/* For parts with CTSUCHAC2/3/4 and CTSUTRC2/3/4 registers (more than 16 TS pins) */ +#if defined(BSP_MCU_RX671) +#define BSP_FEATURE_CTSU_CTSUCHAC_REGISTER_COUNT (3) +#define BSP_FEATURE_CTSU_CTSUCHTRC_REGISTER_COUNT (3) +#endif + +#if (defined(BSP_MCU_RX23_ALL) || defined(BSP_MCU_RX130)) +#define BSP_FEATURE_CTSU_CTSUCHAC_REGISTER_COUNT (5) +#define BSP_FEATURE_CTSU_CTSUCHTRC_REGISTER_COUNT (5) +#endif + + +#if (BSP_FEATURE_CTSU_VERSION == 2) + #define CTSU_CORRECTION_POINT_NUM (12) ///< number of correction table + #define CTSU_CORRCFC_POINT_NUM (5) ///< number of CFC correction table + + #define CTSU_DIAG_HIGH_CURRENT_SOURCE (16) ///< number of high current source table at Diagnosis + #define CTSU_DIAG_LOW_CURRENT_SOURCE (10) ///< number of low current source table at Diagnosis +#endif + +#define CTSU_VALUE_MAJORITY_MODE (0x01) +#define CTSU_JUDGEMENT_MAJORITY_MODE (0x02) + +#ifndef CTSU_CFG_MAJORITY_MODE + #define CTSU_CFG_MAJORITY_MODE (CTSU_VALUE_MAJORITY_MODE) +#endif +#if (BSP_FEATURE_CTSU_VERSION == 2) + #if (CTSU_CFG_MAJORITY_MODE & CTSU_JUDGEMENT_MAJORITY_MODE) + + #define CTSU_MAJORITY_MODE_ELEMENTS (CTSU_CFG_NUM_SUMULTI) + #else + #define CTSU_MAJORITY_MODE_ELEMENTS (1) + #endif +#endif + +#if (BSP_FEATURE_CTSU_VERSION == 1) + #define CTSU_MAJORITY_MODE_ELEMENTS (1) +#endif + +#ifdef QE_TOUCH_CONFIGURATION + #if (CTSU_CFG_AUTO_JUDGE_ENABLE == 1) + #if (QE_TOUCH_VERSION < 0x0400) + #error "Error! For Autojudge measurement with CTSU FIT v3.00 or later, please use QE V4.0.0 or later." + #endif + #endif +#endif + +/*********************************************************************************************************************** + * Typedef definitions + **********************************************************************************************************************/ + +/** CTSU run state */ +typedef enum e_ctsu_state +{ + CTSU_STATE_INIT, ///< Not open. + CTSU_STATE_IDLE, ///< Opened. + CTSU_STATE_SCANNING, ///< Scanning now. + CTSU_STATE_SCANNED ///< Scan end. +} ctsu_state_t; + +/** CTSU Initial offset tuning status */ +typedef enum e_ctsu_tuning +{ + CTSU_TUNING_INCOMPLETE, ///< Initial offset tuning incomplete + CTSU_TUNING_COMPLETE ///< Initial offset tuning complete +} ctsu_tuning_t; + +/** CTSU Correction status */ +typedef enum e_ctsu_correction_status +{ + CTSU_CORRECTION_INIT, ///< Correction initial status. + CTSU_CORRECTION_RUN, ///< Correction scan running. + CTSU_CORRECTION_COMPLETE, ///< Correction complete. + CTSU_CORRECTION_ERROR ///< Correction error. +} ctsu_correction_status_t; + +#if (BSP_FEATURE_CTSU_VERSION == 1) + #if (CTSU_CFG_DIAG_SUPPORT_ENABLE == 1) + +/** CTSU Diagnosis status */ +typedef enum e_ctsu_diagnosis_status +{ + CTSU_DIAG_INIT, ///< Diagnosis initial status. + CTSU_DIAG_OVER_VOLTAGE, ///< Diagnosis of over_voltage running. + CTSU_DIAG_CCO_HIGH, ///< Diagnosis of oscillator 19.2uA running. + CTSU_DIAG_CCO_LOW, ///< Diagnosis of oscillator 2.4uA running. + CTSU_DIAG_SSCG, ///< Diagnosis of sscg_oscillator running. + CTSU_DIAG_DAC, ///< Diagnosis of dac running. + CTSU_DIAG_COMPLETE ///< Diagnosis complete. +} ctsu_diagnosis_status_t; + #endif +#endif + +#if (BSP_FEATURE_CTSU_VERSION == 2) + #if (CTSU_CFG_DIAG_SUPPORT_ENABLE == 1) + +/** CTSU Diagnosis status */ +typedef enum e_ctsu_diagnosis_state +{ + CTSU_DIAG_INIT, ///< Diagnosis initial status. + CTSU_DIAG_OUTPUT_VOLTAGE, ///< Diagnosis of output voltage running. + CTSU_DIAG_OVER_VOLTAGE, ///< Diagnosis of over voltage running. + CTSU_DIAG_OVER_CURRENT, ///< Diagnosis of over current running. + CTSU_DIAG_LOAD_RESISTANCE, ///< Diagnosis of load resistance running. + CTSU_DIAG_CURRENT_SOURCE, ///< Diagnosis of current source running. + CTSU_DIAG_SENSCLK, ///< Diagnosis of SENCLK running. + CTSU_DIAG_SUCLK, ///< Diagnosis of SUCLK running. + CTSU_DIAG_CLOCK_RECOVERY, ///< Diagnosis of clock recovery running. + CTSU_DIAG_CFC, ///< Diagnosis of CFC running. + CTSU_DIAG_COMPLETE ///< Diagnosis complete. +} ctsu_diagnosis_state_t; + #endif +#endif + +/** CTSU range definition */ +typedef enum e_ctsu_range +{ + 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 +} ctsu_range_t; + +/** CTSUWR write register value */ +typedef struct st_ctsu_wr +{ +#if (BSP_FEATURE_CTSU_VERSION == 2) + uint32_t ctsuso; ///< Copy from ((sdpa << 24) | (ssdiv << 20) | (snum << 10) | so) by Open API. +#else + 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. +#endif +} ctsu_ctsuwr_t; + +/** Scan buffer data formats (Self) */ +#if (BSP_FEATURE_CTSU_VERSION == 2) +typedef uint16_t ctsu_self_buf_t; +#else +typedef struct st_ctsu_self_buf +{ + uint16_t sen; ///< Sensor counter data + uint16_t ref; ///< Reference counter data (Not used) +} ctsu_self_buf_t; +#endif + +typedef struct st_ctsu_data +{ + uint16_t decimal_point_data; + uint16_t int_data; +} ctsu_data_t; + +/** Scan buffer data formats (Mutual) */ +#if (BSP_FEATURE_CTSU_VERSION == 2) +typedef uint16_t ctsu_mutual_buf_t; +#else +typedef struct st_ctsu_mutual_buf +{ + 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_mutual_buf_t; +#endif + +/** Correction information */ +typedef struct st_ctsu_correction_info +{ + 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 +#if (BSP_FEATURE_CTSU_VERSION == 2) + #if (CTSU_CFG_TEMP_CORRECTION_SUPPORT == 1) + uint16_t scan_index; ///< Scan point index + uint16_t update_counter; ///< Coefficient update counter + uint16_t ex_base_value; ///< Value of external registance measurement + uint8_t suadj0; ///< Stored SUADJ0 value + #endif + uint16_t base_value[CTSU_RANGE_NUM]; ///< Value of internal registance measurement + uint16_t error_rate[CTSU_RANGE_NUM]; ///< Error rate of base vs DAC + uint16_t range_ratio[CTSU_RANGE_NUM - 1]; ///< Ratio between 160uA range and other ranges + uint16_t dac_value[CTSU_RANGE_NUM][CTSU_CORRECTION_POINT_NUM]; ///< Value of internal DAC measurement with VDC path correction + uint16_t coef[CTSU_RANGE_NUM][CTSU_CORRECTION_POINT_NUM]; ///< Correction coefficients +#else + 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] +#endif +} ctsu_correction_info_t; + +#if (BSP_FEATURE_CTSU_VERSION == 2) + #if (CTSU_CFG_NUM_CFC != 0) + +/** CFC correction information */ +typedef struct st_ctsu_corrcfc_info +{ + ctsu_correction_status_t status; ///< Correction status + ctsu_ctsuwr_t ctsuwr; ///< Correction scan parameter + volatile ctsu_self_buf_t scanbuf[CTSU_CFG_NUM_CFC]; ///< Correction scan buffer + uint16_t base_value[CTSU_CFG_NUM_CFC]; ///< Value of CFC circuit measurement + uint16_t error_rate[CTSU_CFG_NUM_CFC]; ///< Error rate of base vs DAC + uint16_t dac_value[CTSU_CFG_NUM_CFC][CTSU_CORRCFC_POINT_NUM]; ///< Value of internal DAC measurement + uint16_t ref_value[CTSU_CFG_NUM_CFC][CTSU_CORRCFC_POINT_NUM]; ///< Value of reference + uint8_t ts_table[CTSU_CFG_NUM_CFC]; ///< Number of TS terminal + uint8_t index; ///< Index of ts_table + uint8_t num_ts; ///< Number of CFC-TS for instance + uint64_t stored_rx_bitmap; ///< Bitmap of registered CFC terminal +} ctsu_corrcfc_info_t; + #endif +#endif + +#if (BSP_FEATURE_CTSU_VERSION == 1) + #if (CTSU_CFG_DIAG_SUPPORT_ENABLE == 1) + +/** Correction information */ +typedef struct st_ctsu_diag_info +{ + volatile ctsu_diagnosis_status_t state; ///< Diagnosis state + ctsu_ctsuwr_t ctsuwr; ///< Correction scan parameter + uint8_t loop_count; ///< Diagnosis loop counter + volatile ctsu_self_buf_t scanbuf; ///< Diagnosis scan buffer + uint16_t correct_data; ///< Diagnosis correct value + volatile uint8_t icomp; ///< Diagnosis icomp flag + uint16_t cco_high; ///< Diagnosis cco high count + uint16_t cco_low; ///< Diagnosis cco low count + uint16_t sscg; ///< Diagnosis sscg count + uint16_t dac_cnt[6]; ///< Diagnosis dac count + uint16_t so0_4uc_val; ///< Diagnosis dac value + uint16_t dac_init; ///< Diagnosis dac + ctsu_tuning_t tuning; ///< Diagnosis dac initial tuning flag + int32_t tuning_diff; ///< Diagnosis +} ctsu_diag_info_t; + #endif +#endif + +#if (BSP_FEATURE_CTSU_VERSION == 2) + #if (CTSU_CFG_DIAG_SUPPORT_ENABLE == 1) + +/** Correction information */ +typedef struct st_ctsu_diag_info +{ + volatile ctsu_diagnosis_state_t state; ///< Diagnosis state + ctsu_ctsuwr_t ctsuwr; ///< Correction scan parameter + uint8_t lvmode; ///< Diagnosis lv mode flag + uint8_t loop_count; ///< Diagnosis loop counter + uint32_t ctsuscnt[3]; ///< Diagnosis raw data (suclk count value & sens count value) + uint32_t error_registance[CTSU_RANGE_NUM]; ///< Diagnosis error regista + uint16_t output_voltage_cnt[CTSU_RANGE_NUM * 2]; ///< Diagnosis load resistance count value + uint16_t icomp0_value; ///< Diagnosis icomp0 register value in over voltage test + uint16_t icomp1_value; ///< Diagnosis icomp1 register value in over current test + uint16_t load_resistance[CTSU_RANGE_NUM]; ///< Diagnosis load resistance count value + uint16_t current_source[CTSU_DIAG_HIGH_CURRENT_SOURCE + CTSU_DIAG_LOW_CURRENT_SOURCE]; ///< Diagnosis current source count value + uint16_t sensclk_cnt[CTSU_CORRECTION_POINT_NUM]; ///< Diagnosis sensclk count value + uint16_t suclk_cnt[CTSU_CORRECTION_POINT_NUM]; ///< Diagnosis suclk count value + uint16_t suclk_count_clk_recv[3]; ///< Diagnosis clock recovery suclk count value + uint16_t cfc_cnt[CTSU_CORRCFC_POINT_NUM]; ///< Diagnosis cfc count value + uint32_t chaca; ///< Diagnosis CHACA + uint32_t chacb; ///< Diagnosis CHACB +} ctsu_diag_info_t; + #endif + + #if (CTSU_CFG_AUTO_JUDGE_ENABLE == 1) +typedef struct st_ctsu_auto_judge +{ + uint32_t ajthr; ///< Automatic judgement Threshold + uint32_t ajmmar; ///< Automatic judgement Measurement moving average result + uint32_t ajblact; ///< Automatic judgement Baseline average calculation + uint32_t ajblar; ///< Automatic judgement Baseline average results + uint32_t ajrr; ///< Automatic judgement Result +} ctsu_auto_judge_t; + #endif +#endif + +/** CTSU private control block. DO NOT MODIFY. Initialization occurs when R_CTSU_Open() is called. */ +typedef struct st_ctsu_instance_ctrl +{ + 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. +#if (BSP_FEATURE_CTSU_VERSION == 2) + uint8_t * p_frequency_complete_flag; ///< Pointer to complete flag of each frequency. g_ctsu_frequency_complete_flag[] is set by Open API. +#endif + 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_t * p_ctsuwr; ///< CTSUWR write register value. g_ctsu_ctsuwr[] is set by Open API. + ctsu_self_buf_t * p_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_t * p_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 primary multi frequency correction data. g_ctsu_pri_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_t * p_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 Only) + 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. +#if (BSP_FEATURE_CTSU_VERSION == 2) + uint8_t * p_selected_freq_self; ///< Frequency selected by self-capacity + uint8_t * p_selected_freq_mutual; ///< Frequency selected by mutual-capacity +#endif +#if (BSP_FEATURE_CTSU_VERSION == 1) + #if (CTSU_CFG_DIAG_SUPPORT_ENABLE == 1) + ctsu_diag_info_t * p_diag_info; ///< pointer to diagnosis info + #endif +#endif + +#if (BSP_FEATURE_CTSU_VERSION == 2) + ctsu_range_t range; ///< According to atune12. (20uA : 0, 40uA : 1, 80uA : 2, 160uA : 3) + uint8_t ctsucr2; ///< Copy from (posel, atune1, md) by Open API. FCMODE and SDPSEL and LOAD is set by HAL driver. + #if (CTSU_CFG_NUM_CFC != 0) + uint64_t cfc_rx_bitmap; ///< Bitmap of CFC receive terminal. + ctsu_corrcfc_info_t * p_corrcfc_info; ///< pointer to CFC correction info + #endif + #if (CTSU_CFG_DIAG_SUPPORT_ENABLE == 1) + ctsu_diag_info_t * p_diag_info; ///< pointer to diagnosis info + #endif + #if (CTSU_CFG_AUTO_JUDGE_ENABLE == 1) + ctsu_auto_judge_t * p_auto_judge; ///< Array of automatic judgement register write variables. g_ctsu_auto_judge[] is set by Open API. + uint32_t adress_auto_judge; ///< Automatic judgement Variable start address + uint32_t adress_ctsuwr; ///< CTSUWR variable start address for automatic judgement + uint32_t adress_self_raw; ///< Self raw variable start address for automatic judgement + uint32_t adress_mutual_raw; ///< Mutual raw variable start address for automatic judgement + uint32_t count_auto_judge; ///< Automatic judgement transfer count + uint32_t count_ctsuwr_self_mutual; ///< CTSUWR, Self raw, Mutual raw transfer count for automatic judgement + uint8_t blini_flag; ///< Flags for controlling baseline initialization bit for automatic judgement + uint8_t ajmmat; ///< Copy from config by Open API for automatic judgement + uint8_t ajbmat; ///< Copy from config by Open for automatic judgement + #if (CTSU_CFG_AUTO_MULTI_CLOCK_CORRECTION_ENABLE == 1) + uint32_t adress_mcact1; ///< CTSUMCACT1 Variable start address for automatic judgement + uint32_t adress_mcact2; ///< CTSUMCACT2 Variable start address for automatic judgement + uint32_t count_mcact1; ///< CTSUMCACT1 transfer count for automatic judgement + uint32_t count_mcact2; ///< CTSUMCACT2 transfer count for automatic judgement + #endif + #endif + #if (CTSU_CFG_AUTO_MULTI_CLOCK_CORRECTION_ENABLE == 1) + uint32_t * p_mcact1; ///< Array of CTSUMCACT1 register write variables. g_ctsu_mcact1[] is set by Open API. + uint32_t * p_mcact2; ///< Array of CTSUMCACT2 register write variables. g_ctsu_mcact2[] is set by Open API. + uint8_t mcact_flag; ///< Flags for controlling automatic frequency correction setting + #endif +#endif + ctsu_cfg_t const * p_ctsu_cfg; ///< Pointer to initial configurations. + 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_t * p_callback_memory; ///< Pointer to non-secure memory that can be used to pass arguments to a callback in non-secure memory. + void const * 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. +} ctsu_instance_ctrl_t; + +/********************************************************************************************************************** + * Exported global variables + **********************************************************************************************************************/ + +/** @cond INC_HEADER_DEFS_SEC */ +/** Filled in Interface API structure for this Instance. */ +extern const ctsu_api_t g_ctsu_on_ctsu; + +/** @endcond */ + +/********************************************************************************************************************** + * Public Function Prototypes + **********************************************************************************************************************/ +fsp_err_t R_CTSU_Open(ctsu_ctrl_t * const p_ctrl, ctsu_cfg_t const * const p_cfg); +fsp_err_t R_CTSU_ScanStart(ctsu_ctrl_t * const p_ctrl); +fsp_err_t R_CTSU_DataGet(ctsu_ctrl_t * const p_ctrl, uint16_t * p_data); +fsp_err_t R_CTSU_AutoJudgementDataGet(ctsu_ctrl_t * const p_ctrl, uint64_t * p_button_status); +fsp_err_t R_CTSU_ScanStop(ctsu_ctrl_t * const p_ctrl); +fsp_err_t R_CTSU_CallbackSet(ctsu_ctrl_t * const p_api_ctrl, + void ( * p_callback)(ctsu_callback_args_t *), + void const * const p_context, + ctsu_callback_args_t * const p_callback_memory); +fsp_err_t R_CTSU_Diagnosis(ctsu_ctrl_t * const p_ctrl); +fsp_err_t R_CTSU_Close(ctsu_ctrl_t * const p_ctrl); +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); +fsp_err_t R_CTSU_DataInsert(ctsu_ctrl_t * const p_ctrl, uint16_t * p_insert_data); +fsp_err_t R_CTSU_OffsetTuning(ctsu_ctrl_t * const p_ctrl); + +/* Common macro for FSP header files. There is also a corresponding FSP_HEADER macro at the top of this file. */ +FSP_FOOTER + +#endif // R_CTSU_H + +/*******************************************************************************************************************//** + * @} (end addtogroup CTSU) + **********************************************************************************************************************/ diff --git a/drivers/rx/rdp/src/r_ctsu_qe/inc/r_ctsu_qe_api.h b/drivers/rx/rdp/src/r_ctsu_qe/inc/r_ctsu_qe_api.h new file mode 100644 index 00000000..f8feebc4 --- /dev/null +++ b/drivers/rx/rdp/src/r_ctsu_qe/inc/r_ctsu_qe_api.h @@ -0,0 +1,343 @@ +/* +* Copyright (c) 2018 Renesas Electronics Corporation and/or its affiliates +* +* SPDX-License-Identifier: BSD-3-Clause +*/ +/*********************************************************************************************************************** +* File Name : r_ctsu_qe_api.h +* Description : This file contains the CTSU API and should be included by the application which uses this API. +***********************************************************************************************************************/ + +/*******************************************************************************************************************//** + * @ingroup RENESAS_INTERFACES + * @defgroup CTSU_API CTSU Interface + * @brief Interface for Capacitive Touch Sensing Unit (CTSU) functions. + * + * @section CTSU_API_Summary Summary + * The CTSU interface provides CTSU functionality. + * + * The CTSU interface can be implemented by: + * - @ref CTSU + * + * @{ + **********************************************************************************************************************/ + +#ifndef R_CTSU_API_H +#define R_CTSU_API_H + +/*********************************************************************************************************************** + * Includes + **********************************************************************************************************************/ + +/* Register definitions, common services and error codes. */ +#include "platform.h" +#include "mcu/all/fsp_common_api.h" +#include "mcu/all/r_fsp_error.h" + +/* Common macro for FSP header files. There is also a corresponding FSP_FOOTER macro at the end of this file. */ +FSP_HEADER + +/********************************************************************************************************************** + * Macro definitions + **********************************************************************************************************************/ +#define CTSU_COUNT_MAX (0xFFFF) + +#define CTSU_TARGET_VALUE_CONFIG_SUPPORT (1) + +/********************************************************************************************************************** + * Typedef definitions + **********************************************************************************************************************/ + +/** CTSU Events for callback function */ +typedef enum e_ctsu_event +{ + CTSU_EVENT_SCAN_COMPLETE = 0x00, ///< Normal end + CTSU_EVENT_OVERFLOW = 0x01, ///< Sensor counter overflow (CTSUST.CTSUSOVF set) + CTSU_EVENT_ICOMP = 0x02, ///< Abnormal TSCAP voltage (CTSUERRS.CTSUICOMP set) + CTSU_EVENT_ICOMP1 = 0x04 ///< Abnormal sensor current (CTSUSR.ICOMP1 set) +} ctsu_event_t; + +/** CTSU Scan Start Trigger Select */ +typedef enum e_ctsu_cap +{ + CTSU_CAP_SOFTWARE, ///< Scan start by software trigger + CTSU_CAP_EXTERNAL ///< Scan start by external trigger +} ctsu_cap_t; + +/** CTSU Transmission Power Supply Select */ +typedef enum e_ctsu_txvsel +{ + CTSU_TXVSEL_VCC, ///< VCC selected + CTSU_TXVSEL_INTERNAL_POWER ///< Internal logic power supply selected +} ctsu_txvsel_t; + +/** CTSU Transmission Power Supply Select 2 (CTSU2 Only) */ +typedef enum e_ctsu_txvsel2 +{ + CTSU_TXVSEL_MODE, ///< Follow TXVSEL setting + CTSU_TXVSEL_VCC_PRIVATE, ///< VCC private selected +} ctsu_txvsel2_t; + +/** CTSU Power Supply Capacity Adjustment (CTSU Only) */ +typedef enum e_ctsu_atune1 +{ + CTSU_ATUNE1_NORMAL, ///< Normal output (40uA) + CTSU_ATUNE1_HIGH ///< High-current output (80uA) +} ctsu_atune1_t; + +/** CTSU Power Supply Capacity Adjustment (CTSU2 Only) */ +typedef enum e_ctsu_atune12 +{ + CTSU_ATUNE12_80UA, ///< High-current output (80uA) + CTSU_ATUNE12_40UA, ///< Normal output (40uA) + CTSU_ATUNE12_20UA, ///< Low-current output (20uA) + CTSU_ATUNE12_160UA ///< Very high-current output (160uA) +} ctsu_atune12_t; + +/** CTSU Measurement Mode Select */ +typedef enum e_ctsu_mode +{ + CTSU_MODE_SELF_MULTI_SCAN = 1, ///< Self-capacitance multi scan mode + CTSU_MODE_MUTUAL_FULL_SCAN = 3, ///< Mutual capacitance full scan mode + CTSU_MODE_MUTUAL_CFC_SCAN = 7, ///< Mutual capacitance cfc scan mode (CTSU2 Only) + CTSU_MODE_CURRENT_SCAN = 9, ///< Current scan mode (CTSU2 Only) + CTSU_MODE_CORRECTION_SCAN = 17, ///< Correction scan mode (CTSU2 Only) + CTSU_MODE_DIAGNOSIS_SCAN = 33 ///< Diagnosis scan mode +} ctsu_md_t; + +/** CTSU Non-Measured Channel Output Select (CTSU2 Only) */ +typedef enum e_ctsu_posel +{ + CTSU_POSEL_LOW_GPIO, ///< Output low through GPIO + CTSU_POSEL_HI_Z, ///< Hi-Z + CTSU_POSEL_LOW, ///< Setting prohibited + CTSU_POSEL_SAME_PULSE ///< Same phase pulse output as transmission channel through the power setting by the TXVSEL[1:0] bits +} ctsu_posel_t; + +/** CTSU Spectrum Diffusion Frequency Division Setting (CTSU Only) */ +typedef enum e_ctsu_ssdiv +{ + CTSU_SSDIV_4000, ///< 4.00 <= Base clock frequency (MHz) + CTSU_SSDIV_2000, ///< 2.00 <= Base clock frequency (MHz) < 4.00 + CTSU_SSDIV_1330, ///< 1.33 <= Base clock frequency (MHz) < 2.00 + CTSU_SSDIV_1000, ///< 1.00 <= Base clock frequency (MHz) < 1.33 + CTSU_SSDIV_0800, ///< 0.80 <= Base clock frequency (MHz) < 1.00 + CTSU_SSDIV_0670, ///< 0.67 <= Base clock frequency (MHz) < 0.80 + CTSU_SSDIV_0570, ///< 0.57 <= Base clock frequency (MHz) < 0.67 + CTSU_SSDIV_0500, ///< 0.50 <= Base clock frequency (MHz) < 0.57 + CTSU_SSDIV_0440, ///< 0.44 <= Base clock frequency (MHz) < 0.50 + CTSU_SSDIV_0400, ///< 0.40 <= Base clock frequency (MHz) < 0.44 + CTSU_SSDIV_0360, ///< 0.36 <= Base clock frequency (MHz) < 0.40 + CTSU_SSDIV_0330, ///< 0.33 <= Base clock frequency (MHz) < 0.36 + CTSU_SSDIV_0310, ///< 0.31 <= Base clock frequency (MHz) < 0.33 + CTSU_SSDIV_0290, ///< 0.29 <= Base clock frequency (MHz) < 0.31 + CTSU_SSDIV_0270, ///< 0.27 <= Base clock frequency (MHz) < 0.29 + CTSU_SSDIV_0000 ///< 0.00 <= Base clock frequency (MHz) < 0.27 +} ctsu_ssdiv_t; + +/** CTSU select data type for slect data get */ +typedef enum e_ctsu_specific_data_type +{ + CTSU_SPECIFIC_RAW_DATA, + CTSU_SPECIFIC_CCO_CORRECTION_DATA, + CTSU_SPECIFIC_CORRECTION_DATA, + CTSU_SPECIFIC_SELECTED_FREQ, +} ctsu_specific_data_type_t; + +/** Callback function parameter data */ +typedef struct st_ctsu_callback_args +{ + ctsu_event_t event; ///< The event can be used to identify what caused the callback. + void const * p_context; ///< Placeholder for user data. Set in ctsu_api_t::open function in ::ctsu_cfg_t. +} ctsu_callback_args_t; + +/** CTSU Control block. Allocate an instance specific control block to pass into the API calls. + * @par Implemented as + * - ctsu_instance_ctrl_t + */ +typedef void ctsu_ctrl_t; + +/** CTSU Configuration parameters. */ +/** Element Configuration */ +typedef struct st_ctsu_element +{ + ctsu_ssdiv_t ssdiv; ///< CTSU Spectrum Diffusion Frequency Division Setting (CTSU Only) + uint16_t so; ///< CTSU Sensor Offset Adjustment + uint8_t snum; ///< CTSU Measurement Count Setting + uint8_t sdpa; ///< CTSU Base Clock Setting +} ctsu_element_cfg_t; + +/** Configuration of each automatic judgement button */ +typedef struct st_ctsu_auto_button_cfg +{ + uint8_t elem_index; ///< Element number used by this button for automatic judgement. + uint16_t threshold; ///< Touch/non-touch judgement threshold for automatic judgement. + uint16_t hysteresis; ///< Threshold hysteresis for chattering prevention for automatic judgement. +} ctsu_auto_button_cfg_t; + +/** User configuration structure, used in open function */ +typedef struct st_ctsu_cfg +{ + ctsu_cap_t cap; ///< CTSU Scan Start Trigger Select + ctsu_txvsel_t txvsel; ///< CTSU Transmission Power Supply Select + ctsu_txvsel2_t txvsel2; ///< CTSU Transmission Power Supply Select 2 (CTSU2 Only) + ctsu_atune1_t atune1; ///< CTSU Power Supply Capacity Adjustment (CTSU Only) + ctsu_atune12_t atune12; ///< CTSU Power Supply Capacity Adjustment (CTSU2 Only) + ctsu_md_t md; ///< CTSU Measurement Mode Select + ctsu_posel_t posel; ///< CTSU Non-Measured Channel Output Select (CTSU2 Only) + uint8_t tsod; ///< TS all terminal output control for multi electrode scan + uint8_t mec_ts; ///< TS number used when using the MEC function + uint8_t mec_shield_ts; ///< TS number of active shield used when using MEC function + uint8_t tlot; ///< Number of consecutive judgements exceeding the threshold L for automatic judgement + uint8_t thot; ///< Number of consecutive judgements exceeding the threshold H for automatic judgement + uint8_t jc; ///< judgement condition for automatic judgement + uint8_t ajmmat; ///< Measured value moving average number of times for automatic judgement + uint8_t ajbmat; ///< Average number of baselines for automatic judgement + uint8_t mtucfen; ///< Mutual capacity operation for automatic judgement + uint8_t ajfen; ///< Automatic judgement function enabled for automatic judgement + uint8_t autojudge_monitor_num; ///< Method number for QE monitor for automatic judgement + uint8_t majirimd; ///< JMM or VMM for automatic judgement + 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 + ctsu_element_cfg_t const * p_elements; ///< Pointer to elements configuration array + uint8_t num_rx; ///< Number of receive terminals + uint8_t num_tx; ///< Number of transmit terminals + uint16_t num_moving_average; ///< Number of moving average for measurement data + bool tuning_enable; ///< Initial offset tuning flag + void (* p_callback)(ctsu_callback_args_t * p_args); ///< Callback provided when CTSUFN ISR occurs. + void const * p_context; ///< User defined context passed into callback function. + void const * p_extend; ///< Pointer to extended configuration by instance of interface. + 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 + ctsu_auto_button_cfg_t const * p_ctsu_auto_buttons; ///< Pointer to array of automatic judgement button configuration. + uint8_t majority_mode; ///< Software majority mode selection +} ctsu_cfg_t; + +/** Functions implemented at the HAL layer will follow this API. */ +typedef struct st_ctsu_api +{ + /** Open driver. + * @par Implemented as + * - @ref R_CTSU_Open() + * + * @param[in] p_ctrl Pointer to control structure. + * @param[in] p_cfg Pointer to pin configuration structure. + */ + fsp_err_t (* open)(ctsu_ctrl_t * const p_ctrl, ctsu_cfg_t const * const p_cfg); + + /** Scan start. + * @par Implemented as + * - @ref R_CTSU_ScanStart() + * + * @param[in] p_ctrl Pointer to control structure. + */ + fsp_err_t (* scanStart)(ctsu_ctrl_t * const p_ctrl); + + /** Data get. + * @par Implemented as + * - @ref R_CTSU_DataGet() + * + * @param[in] p_ctrl Pointer to control structure. + * @param[out] p_data Pointer to get data array. + */ + fsp_err_t (* dataGet)(ctsu_ctrl_t * const p_ctrl, uint16_t * p_data); + + /** ScanStop. + * @par Implemented as + * - @ref R_CTSU_ScanStop() + * + * @param[in] p_ctrl Pointer to control structure. + */ + fsp_err_t (* scanStop)(ctsu_ctrl_t * const p_ctrl); + + /** Diagnosis. + * @par Implemented as + * - @ref R_CTSU_Diagnosis() + * + * @param[in] p_ctrl Pointer to control structure. + */ + fsp_err_t (* diagnosis)(ctsu_ctrl_t * const p_ctrl); + + /** Specify callback function and optional context pointer and working memory pointer. + * @par Implemented as + * - @ref R_CTSU_CallbackSet() + * + * @param[in] p_ctrl Pointer to the CTSU control block. + * @param[in] p_callback Callback function + * @param[in] p_context Pointer to send to callback function + * @param[in] p_working_memory Pointer to volatile memory where callback structure can be allocated. + * Callback arguments allocated here are only valid during the callback. + */ + fsp_err_t (* callbackSet)(ctsu_ctrl_t * const p_api_ctrl, void (* p_callback)(ctsu_callback_args_t *), + void const * const p_context, ctsu_callback_args_t * const p_callback_memory); + + /** Close driver. + * @par Implemented as + * - @ref R_CTSU_Close() + * + * @param[in] p_ctrl Pointer to control structure. + */ + fsp_err_t (* close)(ctsu_ctrl_t * const p_ctrl); + + /** Specific Data get. + * @par Implemented as + * - @ref R_CTSU_SpecificDataGet() + * + * @param[in] p_ctrl Pointer to control structure. + * @param[out] p_specific_data Pointer to get specific data array. + * @param[in] specific_data_type Specific data type + */ + fsp_err_t (* specificDataGet)(ctsu_ctrl_t * const p_ctrl, uint16_t * p_specific_data, + ctsu_specific_data_type_t specific_data_type); + + /** Data Insert. + * @par Implemented as + * - @ref R_CTSU_DataInsert() + * + * @param[in] p_ctrl Pointer to control structure. + * @param[in] p_insert_data Pointer to insert data. + */ + fsp_err_t (* dataInsert)(ctsu_ctrl_t * const p_ctrl, uint16_t * p_insert_data); + + /** Initialize automatic judgement and get button status. + * @par Implemented as + * - @ref R_CTSU_AutoJudgementDataGet() + * + * @param[in] p_ctrl Pointer to control structure. + * @param[out] p_button_status Pointer to get button status array. + */ + fsp_err_t (* autoJudgementDataGet)(ctsu_ctrl_t * const p_ctrl, uint64_t * p_button_status); + + /** Adjust the offset value to tune the sensor. + * @par Implemented as + * - @ref R_CTSU_OffsetTuning() + * + * @param[in] p_ctrl Pointer to control structure. + */ + fsp_err_t (* offsetTuning)(ctsu_ctrl_t * const p_ctrl); +} ctsu_api_t; + +/** This structure encompasses everything that is needed to use an instance of this interface. */ +typedef struct st_ctsu_instance +{ + ctsu_ctrl_t * p_ctrl; ///< Pointer to the control structure for this instance + ctsu_cfg_t const * p_cfg; ///< Pointer to the configuration structure for this instance + ctsu_api_t const * p_api; ///< Pointer to the API structure for this instance +} ctsu_instance_t; + +/** Common macro for FSP header files. There is also a corresponding FSP_HEADER macro at the top of this file. */ +FSP_FOOTER + +#endif /* R_CTSU_API_H */ + +/*******************************************************************************************************************//** + * @} (end addtogroup CTSU_API) + **********************************************************************************************************************/ diff --git a/drivers/rx/rdp/src/r_ctsu_qe/inc/r_ctsu_qe_pinset.h b/drivers/rx/rdp/src/r_ctsu_qe/inc/r_ctsu_qe_pinset.h new file mode 100644 index 00000000..3c7cb887 --- /dev/null +++ b/drivers/rx/rdp/src/r_ctsu_qe/inc/r_ctsu_qe_pinset.h @@ -0,0 +1,30 @@ +/* +* Copyright (c) 2018 Renesas Electronics Corporation and/or its affiliates +* +* SPDX-License-Identifier: BSD-3-Clause +*/ + +/*********************************************************************************************************************** +* File Name : r_ctsu_qe_pinset.h +* Version : 1.0.2 +* Device(s) : R5F51308AxFP +* Tool-Chain : RXC toolchain +* Description : Setting of port and mpc registers +***********************************************************************************************************************/ + +#ifndef R_CTSU_QE_H +#define R_CTSU_QE_H + +/*********************************************************************************************************************** +Includes +***********************************************************************************************************************/ + +/*********************************************************************************************************************** +Global variables and functions +***********************************************************************************************************************/ + +void R_CTSU_PinSetInit(); +void R_Set_CTSU_TSCAP_Discharge(); +void R_Set_CTSU_TSCAP_Charge(); + +#endif diff --git a/drivers/rx/rdp/src/r_ctsu_qe/r_ctsu_qe_if.h b/drivers/rx/rdp/src/r_ctsu_qe/r_ctsu_qe_if.h new file mode 100644 index 00000000..f9de5cdf --- /dev/null +++ b/drivers/rx/rdp/src/r_ctsu_qe/r_ctsu_qe_if.h @@ -0,0 +1,41 @@ +/* +* Copyright (c) 2018 Renesas Electronics Corporation and/or its affiliates +* +* SPDX-License-Identifier: BSD-3-Clause +*/ +/*********************************************************************************************************************** +* File Name : r_ctsu_qe_if.h +* Description : This file contains the CTSU API and should be included by the application which uses this API. +***********************************************************************************************************************/ +#ifndef R_CTSU_QE_IF_H +#define R_CTSU_QE_IF_H + +#include "r_ctsu_qe.h" +#include "r_ctsu_qe_api.h" +#include "r_ctsu_qe_config.h" + + +/*********************************************************************************************************************** +* Macro definitions +***********************************************************************************************************************/ +/* Driver Version Number. */ +#define QECTSU_RX_VERSION_MAJOR (1) +#define QECTSU_RX_VERSION_MINOR (20) + + +/*********************************************************************************************************************** +* Typedef definitions +***********************************************************************************************************************/ + + +/*********************************************************************************************************************** +* Exported global variables +***********************************************************************************************************************/ + + +/*********************************************************************************************************************** +* Exported global functions +***********************************************************************************************************************/ + + +#endif // QETOUCH_RX_IF_H_FILE diff --git a/drivers/rx/rdp/src/r_ctsu_qe/src/r_ctsu_qe.c b/drivers/rx/rdp/src/r_ctsu_qe/src/r_ctsu_qe.c new file mode 100644 index 00000000..d084ab23 --- /dev/null +++ b/drivers/rx/rdp/src/r_ctsu_qe/src/r_ctsu_qe.c @@ -0,0 +1,8073 @@ +/* +* Copyright (c) 2018 Renesas Electronics Corporation and/or its affiliates +* +* SPDX-License-Identifier: BSD-3-Clause +*/ +/*********************************************************************************************************************** +* File Name : r_ctsu_qe.c +* Description : This file implements the QE CTSU API +***********************************************************************************************************************/ + +/*********************************************************************************************************************** + * Includes + **********************************************************************************************************************/ +#ifdef QE_TOUCH_CONFIGURATION + #include "qe_touch_define.h" + #include "qe_touch_config.h" +#endif +#include "r_ctsu_qe_if.h" +#include "r_ctsu_qe_pinset.h" +#if (CTSU_CFG_DTC_SUPPORT_ENABLE == 1) +#include "r_dtc_rx_if.h" +#endif +#if ((CTSU_CFG_DIAG_SUPPORT_ENABLE == 1) || (CTSU_CFG_CALIB_RTRIM_SUPPORT==1)) + #if (BSP_FEATURE_CTSU_VERSION == 2) + #include "r_s12ad_rx_if.h" + #endif +#endif + +/*********************************************************************************************************************** + * Macro definitions + **********************************************************************************************************************/ + +/** "CTSU" in ASCII, used to determine if device is open. */ +#define CTSU_OPEN (0x43545355U) + +/* Macro definitions for register setting */ +#define CTSU_PON_OFF (0) // CTSU hardware macro power off +#define CTSU_PON_ON (1) // CTSU hardware macro power on +#define CTSU_CSW_OFF (0) // Capacitance switch turned off +#define CTSU_CSW_ON (1) // Capacitance switch turned on + +#define CTSU_CR1_MODIFY_BIT (0xC8) // MD1, MD0, ATUNE1 +#define CTSU_SOVF (0x20) // Overflow bit +#define CTSU_CORRECTION_AVERAGE (32) +#define CTSU_SHIFT_AMOUNT (15) + +#define CTSU_PCLKB_FREQ_MHZ (1000000) +#define CTSU_PCLKB_FREQ_RANGE1 (32) +#define CTSU_PCLKB_FREQ_RANGE2 (64) +#define CTSU_PCLKB_FREQ_RANGE3 (128) +#define CTSU_WAIT_TIME (500) + +/* Macro definitions for initial offset tuning */ +#define CTSU_TUNING_MAX (0x03FF) +#define CTSU_TUNING_MIN (0x0000) +#define CTSU_TUNING_VALUE_SELF (15360) +#define CTSU_TUNING_VALUE_MUTUAL (10240) +#define CTSU_TUNING_OT_COUNT (25) + +#define CTSU_CFG_DECIMAL_POINT (16) + +#if (BSP_FEATURE_CTSU_VERSION == 2) + #define CTSU_SST_RECOMMEND (0x1F) // The recommend value of SST + #define CTSU_SST_RECOMMEND_CURRENT (0x3F) // The recommend value of SST with current + #define CTSU_SNUM_RECOMMEND (0x07) // The value of SNUM should be fixed + #define CTSU_SNUM_MAX (0xFF) // The maximum value of SNUM + #define CTSU_ICOMP0 (0x80) // ICOMP0 bit + #define CTSU_ICOMP1 (0x40) // ICOMP1 bit + #define CTSU_ICOMPRST (0x20) // ICOMPRST bit + #define CTSU_CR0_MODIFY_BIT (0xC0) // TXVSEL + #define CTSU_CR2_MODIFY_BIT (0x33) // POSEL, ATUNE2, MD2 + #define CTSU_SUADJ_MAX (0xFF) // The maximum value of SUADJx + #define CTSU_SUADJ_SSCNT_ADJ (0x20) // The value of Adjusting SCADJx by SSCNT + #define CTSU_MUTUAL_BUF_SIZE (CTSU_CFG_NUM_SUMULTI * 2) + #define CTSU_SUMULTI_BASE (0x3F) // SUCLK base value (32MHz) + +/* Macro definitions for correction */ + #if (CTSU_CFG_LOW_VOLTAGE_MODE == 0) + #define CTSU_CORRECTION_STD_VAL (19200) // 20UC standard value + #define CTSU_CORRECTION_STD_UNIT (1920) // 2UC value + #define CTSU_CORRECTION_STD_EXREG (14400) // External registance standard value + #define CTSU_CORRECTION_OFFSET_UNIT (120) // (7680 / 64) + #else + #define CTSU_CORRECTION_STD_VAL (15360) // 20UC standard value * 0.8 + #define CTSU_CORRECTION_STD_UNIT (1536) // 2UC value * 0.8 + #define CTSU_CORRECTION_STD_EXREG (11520) // External registance standard value + #define CTSU_CORRECTION_OFFSET_UNIT (96) // (7680 / 64) * 0.8 + #endif + #define CTSU_CORRECTION_SUMULTI (0x20) // SUMULTI step + #define CTSU_CORRECTION_TRIMB_MAX (0xFF) + #define CTSU_CORRECTION_TRIMB_SIGN_BIT (0x80) + #define CTSU_CORRECTION_RTRIM_THRESHOLD1 (0xA0) + #define CTSU_CORRECTION_RTRIM_THRESHOLD2 (0x50) + #define CTSU_CORRECTION_TRIMB_THRESHOLD1 (0xC0) + #define CTSU_CORRECTION_TRIMB_THRESHOLD2 (0x3F) + #define CTSU_CORRECTION_BIT16 (0x10000) + #define CTSU_CORRECTION_BIT10 (0x0400) + #define CTSU_CORRECTION_BIT9 (0x0200) + #define CTSU_CORRECTION_BIT8 (0x0100) + #define CTSU_CORRECTION_BIT7 (0x0080) + #define CTSU_CORRECTION_BIT6_0 (0x007F) + #define CTSU_CORRECTION_DIV_PRECISION (12) + + #if (CTSU_CFG_NUM_CFC != 0) + #define CTSU_CORRCFC_CENTER_POINT ((CTSU_CORRCFC_POINT_NUM - 1) / 2) // number of center point + #define CTSU_CORRCFC_TS_MAX (36) // Maximum number of TS terminal + #define CTSU_CORRCFC_SHIFT8 (8) // Definition of 8bit shift + #endif + + #if (CTSU_CFG_CALIB_RTRIM_SUPPORT == 1) + #if (CTSU_CFG_LOW_VOLTAGE_MODE == 0) + #define CTSU_CALIB_REF ((6144000 * 10) / CTSU_CFG_VCC_MV) // 1.5V Reference value (4096 * 1500 * 10) + #else + #define CTSU_CALIB_REF ((4915200 * 10) / CTSU_CFG_VCC_MV) // 1.2V Reference value (4096 * 1200 * 10) + #endif + #define CTSU_CALIB_AVERAGE_TIME (64) // ADC average time + #define CTSU_CALIB_THRESHOLD ((0x1000 * 4) / CTSU_CFG_VCC_MV) // RTRIM calib threshold + #define CTSU_CALIB_CTSUSO (0x3C0) // 150uA offset + #define CTSU_CALIB_ADSSTRL (0x3F) // Sampling time + #endif + +#endif +#if (BSP_FEATURE_CTSU_VERSION == 1) + #define CTSU_SST_RECOMMEND (0x10) // The value of SST should be fixed to 00010000b + #define CTSU_SNUM_MAX (0x3F) // The maximum value of SNUM + #define CTSU_SDPA_MAX (0x1F) // The maximum value of SDPA + #define CTSU_PRRATIO_RECOMMEND (3) // Recommended setting value + #define CTSU_PRMODE_62_PULSES (2) // 62 pulses (recommended setting value) + #define CTSU_SOFF_ON (0) // High-pass noise reduction function turned on + #define CTSU_SSMOD (0) // The value of SSMOD should be fixed to 00b + #define CTSU_SSCNT (3) // The value of SSCNT should be fixed to 11b + #define CTSU_RICOA_RECOMMEND (0x0F) // Recommended setting value + #define CTSU_ICOG_100 (0) // ICOG = 100% + #define CTSU_ICOG_66 (1) // ICOG = 66% + #define CTSU_ICOG_50 (2) // ICOG = 50% + #define CTSU_ICOG_40 (3) // ICOG = 40% + #define CTSU_MUTUAL_BUF_SIZE (1) + +/* Macro definitions for correction */ + #if (CTSU_CFG_MCU_PROCESS_MF3 == 1) + #if (CTSU_CFG_LOW_VOLTAGE_MODE == 0) + #define CTSU_CORRECTION_1ST_STD_VAL (40960UL) // ICOG = 66% + #define CTSU_CORRECTION_2ND_STD_VAL (24824) // ICOG = 40%, (x = 40960 * 40 / 66) + #else + #define CTSU_CORRECTION_1ST_STD_VAL (32768UL) // ICOG = 66% + #define CTSU_CORRECTION_2ND_STD_VAL (19859) // ICOG = 40%, (x = 40960 * 40 / 66) + #endif + #define CTSU_WAFER_PARAMETER (0.96523525) + #define CTSU_ICOG_RECOMMEND (CTSU_ICOG_66) // Recommended setting value + #endif + #if (CTSU_CFG_MCU_PROCESS_40N_PHASE2 == 1) + #define CTSU_CORRECTION_1ST_STD_VAL (27306UL) // ICOG = 66%, (x = 40960 * 66 / 100) + #define CTSU_CORRECTION_2ND_STD_VAL (16384) // ICOG = 40%, (x = 40960 * 40 / 100) + #define CTSU_WAFER_PARAMETER (1) + #define CTSU_ICOG_RECOMMEND (CTSU_ICOG_100) // Recommended setting value + #endif +#endif + +#if (BSP_FEATURE_CTSU_VERSION == 1) + #if (CTSU_CFG_DIAG_SUPPORT_ENABLE == 1) + + #define CTSU_DIAG_DAC_1UC (0x10) // 0x10 for so dac value + #define CTSU_DIAG_DAC_2UC (0x20) // 0x20 for so dac value + #define CTSU_DIAG_DAC_4UC (0x40) // 0x40 for so dac value + #define CTSU_DIAG_DAC_8UC (0x80) // 0x80 for so dac value + #define CTSU_DIAG_DAC_16UC (0x100) // 0x100 for so dac value + + #define CTSU_DIAG_DAC_SO_MAX (0x3FF) // so dac max + + #define CTSU_DIAG_TSOC_BIT (0x80) // TSOC bit 1 + #define CTSU_DIAG_CLKSEL1_BIT (0x40) // clksel bit 1 + #define CTSU_DIAG_DAC_INIT_VALUE (241) // SO value of dac test + #define CTSU_DIAG_DAC_TARGET_VALUE (15360) // 6UC value dac test target + #define CTSU_DIAG_DAC_START_VALUE (0x100) // so value dac test tuning + #define CTSU_DAC_TEST_ATUNE1 (0x08) // ATUNE1 bit 1 + #endif +#endif + +#if (BSP_FEATURE_CTSU_VERSION == 2) + #if (CTSU_CFG_DIAG_SUPPORT_ENABLE == 1) + #define CTSU_DIAG_ADSSTRL (0xff) // ADC scan ADSSTAL register + #define CTSU_DIAG_DAC_INIT_0 (0x0) // so dac Lower current source0 init + #define CTSU_DIAG_DAC_INIT_1 (0x100) // so dac Lower current source1 init + #define CTSU_DIAG_DAC_INIT_2 (0x200) // so dac Lower current source2 init + #define CTSU_DIAG_DAC_INIT_3 (0x300) // so dac Lower current source3 init + + #define CTSU_DIAG_DAC_DATA_MAX_0 (0x0ff) // so dac Lower current source0 max num + #define CTSU_DIAG_DAC_DATA_MAX_1 (0x1ff) // so dac Lower current source1 max num + #define CTSU_DIAG_DAC_DATA_MAX_2 (0x2ff) // so dac Lower current source2 max num + #define CTSU_DIAG_DAC_DATA_MAX_3 (0x3ff) // so dac Lower current source3 max num + + #define CTSU_DIAG_CURRENT_CLIB_REG (0x818) // current test calib register + + #define CTSU_DIAG_DELAY_MS (5) // delay time (ms) + #define CTSU_DIAG_SUCLK0_REG1 (0xff00) // cco gain test SUCLK0 + #define CTSU_DIAG_SUCLK0_REG2 (0x20) // cco gain test SUCLK0 + #define CTSU_DIAG_STCLK_FREQ (500) // stclk freqency (Hz) + #define CTSU_DIAG_LVM_MASK (0x400) // LVmode mask + #define CTSU_DIAG_CFC_SDPA_REG (0x3F) // cfc scan sdpa + #define CTSU_DIAG_CHACA_TSMAX (32) // ts max chaca byte + + #define CTSU_DIAG_DAC_0BIT (0x1) // 0x1 for SO register + #define CTSU_DIAG_DAC_1BIT (0x2) // 0x2 for SO register + #define CTSU_DIAG_DAC_2BIT (0x4) // 0x4 for SO register + #define CTSU_DIAG_DAC_3BIT (0x8) // 0x8 for SO register + #define CTSU_DIAG_DAC_4BIT (0x10) // 0x10 for SO register + #define CTSU_DIAG_DAC_5BIT (0x20) // 0x20 for SO register + #define CTSU_DIAG_DAC_6BIT (0x40) // 0x40 for SO register + #define CTSU_DIAG_DAC_7BIT (0x80) // 0x80 for SO register + + #endif +#endif + +#if (CTSU_CFG_AUTO_JUDGE_ENABLE == 1) + #define CTSU_AUTO_JUDGE_ELEMENT_NUM (CTSU_CFG_NUM_AUTOJUDGE_SELF_ELEMENTS + CTSU_CFG_NUM_AUTOJUDGE_MUTUAL_ELEMENTS) + #define CTSU_AUTO_JUDGE_BLOCK_SIZE (5) // Block size of automatic judgement register to be transferred by DTC + #define CTSU_AUTO_CTSUSO_BLOCK_SIZE (1) // Block size of CTSUSO register to be transferred by DTC for automatic judgement(same for CTSUSCNT register) + #define CTSU_AUTO_FINAL_JUDGE_BIT (4) // Definition for bit-shifting the final judgement bit of the automatic judgement result register + #if (CTSU_CFG_AUTO_MULTI_CLOCK_CORRECTION_ENABLE == 1) + #define CTSU_AUTO_MCACT_BLOCK_SIZE (1) // Block size of CTSUMCACT register to be transferred by DTC + #endif +#endif + +#if (CTSU_CFG_AUTO_CORRECTION_ENABLE == 1) + #define CTSU_AUTO_DECIMAL_POINT_NUM (12) // Number of bits after the decimal point of the sensor counter auto correction table register +#endif + +#if (CTSU_CFG_AUTO_MULTI_CLOCK_CORRECTION_ENABLE == 1) + #define CTSU_AUTO_CURRENT_MODE_20UA (20) + #define CTSU_AUTO_CURRENT_MODE_40UA (40) + #define CTSU_AUTO_CURRENT_MODE_80UA (80) + #define CTSU_AUTO_CURRENT_MODE_160UA (160) + #define CTSU_AUTO_REF_COEFFICIENT (15) // RefFullScaleCount / OffsetDAC Resolution (15360 / 1024) + #if (CTSU_CFG_LOW_VOLTAGE_MODE == 0) + #define CTSU_AUTO_CORRECTION_OFFSET_DAC_MAX (160) // OffsetDAC maximum output current [uA] + #else + #define CTSU_AUTO_CORRECTION_OFFSET_DAC_MAX (128) // 160 * 0.8 + #endif + #define CTSU_AUTO_INT32_OVERFLOW_VALUE (2147483648) + #define CTSU_AUTO_INT16_OVERFLOW_VALUE (32767) + #define CTSU_AUTO_INT16_UNDERFLOW_VALUE (-32768) +#endif + +#if (CTSU_CFG_NUM_MUTUAL_ELEMENTS != 0) + #define CTSU_SERIAL_TUNING_ELEMENT (1) // To avoid reading outside the array in the case of mutual 1 x N configuration +#else + #define CTSU_SERIAL_TUNING_ELEMENT (0) // No additional buffer required +#endif + +/*********************************************************************************************************************** + * Typedef definitions + ***********************************************************************************************************************/ +typedef struct st_ctsu_correction_calc +{ + ctsu_range_t range; + uint16_t snum; + uint16_t sdpa; + uint16_t cfc; + ctsu_md_t md; +} ctsu_correction_calc_t; + +#if (BSP_FEATURE_CTSU_VERSION == 1) + #if (CTSU_CFG_DIAG_SUPPORT_ENABLE == 1) +typedef struct st_ctsu_diag_save_reg +{ + uint8_t ctsucr0; + uint8_t ctsucr1; + uint8_t ctsusdprs; + uint8_t ctsusst; + uint8_t ctsuchac0; + uint8_t ctsuchac1; + uint8_t ctsuchac2; + uint8_t ctsuchtrc0; + uint8_t ctsuchtrc1; + uint8_t ctsuchtrc2; + uint8_t ctsudclkc; + uint16_t ctsuerrs; +} ctsu_diag_save_reg_t; + #endif +#endif + +#if (BSP_FEATURE_CTSU_VERSION == 2) + #if (CTSU_CFG_DIAG_SUPPORT_ENABLE == 1) +typedef struct st_ctsu_diag_save_reg +{ + uint32_t ctsucra; + uint32_t ctsucrb; + uint32_t ctsuchaca; + uint32_t ctsuchacb; + uint32_t ctsuchtrca; + uint32_t ctsuchtrcb; + uint32_t ctsumch; + uint32_t ctsucalib; + uint32_t ctsusuclka; + uint32_t ctsusuclkb; +} ctsu_diag_save_reg_t; + #endif +#endif + +#if defined(__CCRX__) || defined(__ICCRX__) || defined(__RX__) +#elif defined(__CCRL__) || defined(__ICCRL__) || defined(__RL78__) +#else + #if defined(__ARMCC_VERSION) || defined(__ICCARM__) +typedef void (BSP_CMSE_NONSECURE_CALL * ctsu_prv_ns_callback)(ctsu_callback_args_t * p_args); + #elif defined(__GNUC__) +typedef BSP_CMSE_NONSECURE_CALL void (*volatile ctsu_prv_ns_callback)(ctsu_callback_args_t * p_args); + #endif +#endif + +/*********************************************************************************************************************** + * Private function prototypes + **********************************************************************************************************************/ +#if (CTSU_CFG_DTC_SUPPORT_ENABLE == 1) +static fsp_err_t ctsu_transfer_open(ctsu_instance_ctrl_t * const p_instance_ctrl); +static fsp_err_t ctsu_transfer_configure(ctsu_instance_ctrl_t * const p_instance_ctrl); + #if (BSP_FEATURE_CTSU_VERSION == 2) +static dtc_err_t ctsu_transfer_normal (ctsu_instance_ctrl_t * const p_instance_ctrl); +static dtc_err_t ctsu_transfer_normal_ctsuwr (ctsu_instance_ctrl_t * const p_instance_ctrl); +static dtc_err_t ctsu_transfer_normal_ctsurd (ctsu_instance_ctrl_t * const p_instance_ctrl); + #if (CTSU_CFG_AUTO_JUDGE_ENABLE == 1) +static dtc_err_t ctsu_transfer_autojudge (ctsu_instance_ctrl_t * const p_instance_ctrl); +static dtc_err_t ctsu_transfer_autojudge_ctsuwr (ctsu_instance_ctrl_t * const p_instance_ctrl); +static dtc_err_t ctsu_transfer_autojudge_ctsurd (ctsu_instance_ctrl_t * const p_instance_ctrl); +static void ctsu_transfer_autojudge_value_set (uint8_t array_number, dtc_repeat_block_side_t set_repeat_block_side, uint32_t set_transfer_count); +static void ctsu_transfer_autojudge_repeat_set (uint8_t array_number, dtc_chain_transfer_t set_chain_transfer_enable, dtc_repeat_block_side_t set_repeat_block_side); + #endif + #if (CTSU_CFG_AUTO_MULTI_CLOCK_CORRECTION_ENABLE == 1) +static void ctsu_transfer_mcact_set (uint8_t array_number, uint32_t set_transfer_count); + #endif + #endif + #if (BSP_FEATURE_CTSU_VERSION == 1) +static dtc_err_t ctsu_transfer_ctsu1 (ctsu_instance_ctrl_t * const p_instance_ctrl); +static dtc_err_t ctsu_transfer_ctsu1_ctsuwr (ctsu_instance_ctrl_t * const p_instance_ctrl); +static dtc_err_t ctsu_transfer_ctsu1_ctsurd (ctsu_instance_ctrl_t * const p_instance_ctrl); + #endif +static void ctsu_transfer_ctsuso_set (uint8_t array_number, dtc_chain_transfer_t set_chain_transfer_enable, dtc_chain_transfer_mode_t set_chain_transfer_mode, uint32_t set_transfer_count); +static void ctsu_transfer_ctsuscnt_set (uint8_t array_number, dtc_chain_transfer_t set_chain_transfer_enable, dtc_chain_transfer_mode_t set_chain_transfer_mode, uint32_t set_transfer_count); +static void ctsu_transfer_address_set (uint8_t array_number, uint32_t * p_set_source_addr, uint32_t * p_set_dest_addr); +static fsp_err_t ctsu_transfer_close(ctsu_instance_ctrl_t * const p_instance_ctrl); +#endif +static void ctsu_initial_offset_tuning(ctsu_instance_ctrl_t * const p_instance_ctrl); +static void ctsu_moving_average(ctsu_data_t * p_average, uint16_t new_data, uint16_t average_num); +static void ctsu_end_interrupt(ctsu_instance_ctrl_t * const p_instance_ctrl); +static void ctsu_correction_process(ctsu_instance_ctrl_t * const p_instance_ctrl); +static void ctsu_correction_measurement(ctsu_instance_ctrl_t * const p_instance_ctrl, uint16_t * data); + +static void ctsu_correction_exec(ctsu_instance_ctrl_t * const p_instance_ctrl); + +#if (CTSU_CFG_AUTO_CORRECTION_ENABLE == 0) +static void ctsu_correction_calc(uint16_t * correction_data, uint16_t raw_data, ctsu_correction_calc_t * p_calc); + +#endif +#if (BSP_FEATURE_CTSU_VERSION == 1) +static void ctsu_correction_ctsu1_exec(ctsu_instance_ctrl_t * const p_instance_ctrl); + +#endif +#if (BSP_FEATURE_CTSU_VERSION == 2) +static void ctsu_correction_ctsu2_exec(ctsu_instance_ctrl_t * const p_instance_ctrl); + +#endif +#if (BSP_FEATURE_CTSU_VERSION == 2) + #if (CTSU_CFG_AUTO_MULTI_CLOCK_CORRECTION_ENABLE == 0) +static void ctsu_correction_freq(uint16_t * p_corr, uint32_t * p_so_value, uint16_t * p_mfc); + + #endif +static uint8_t ctsu_correction_multi(uint16_t * p_pri_mfc, + uint16_t * p_snd_mfc, + uint16_t * p_pri_data, + uint16_t * p_snd_data); + + #if (CTSU_CFG_TEMP_CORRECTION_SUPPORT == 1) +static void ctsu_correction_scan_start(void); +static fsp_err_t ctsu_correction_data_get(ctsu_instance_ctrl_t * const p_instance_ctrl, uint16_t * p_data); + + #if (CTSU_CFG_CALIB_RTRIM_SUPPORT == 1) +static fsp_err_t ctsu_correction_calib_rtrim(ctsu_instance_ctrl_t * const p_instance_ctrl, uint16_t * p_data); + + #endif + #endif + #if (CTSU_CFG_NUM_CFC != 0) +static void ctsu_corrcfc_process(ctsu_instance_ctrl_t * const p_instance_ctrl); +static void ctsu_corrcfc_measurement(ctsu_instance_ctrl_t * const p_instance_ctrl, uint16_t * data, uint8_t point_num); + + #endif + + #if (CTSU_CFG_AUTO_CORRECTION_ENABLE == 1) +static void ctsu_auto_correction_register_set(ctsu_instance_ctrl_t * const p_instance_ctrl); + + #endif + + #if (CTSU_CFG_AUTO_JUDGE_ENABLE == 1) +static void ctsu_auto_judge_threshold_calc(ctsu_instance_ctrl_t * const p_instance_ctrl); + + #endif + + #if (CTSU_CFG_AUTO_MULTI_CLOCK_CORRECTION_ENABLE == 1) +static void ctsu_multiclock_auto_correction_register_set(ctsu_instance_ctrl_t * const p_instance_ctrl); +static int16_t ctsu_multiclock_auto_correction_calc(ctsu_instance_ctrl_t * const p_instance_ctrl, + uint16_t element_id, + uint8_t freq); + + #endif +#endif + + + +#if (BSP_FEATURE_CTSU_VERSION == 1) + #if (CTSU_CFG_DIAG_SUPPORT_ENABLE == 1) +static void ctsu_diag_dac_initial_tuning(void); + +static void ctsu_diag_ldo_over_voltage_scan_start(void); +static void ctsu_diag_oscillator_high_scan_start(void); +static void ctsu_diag_oscillator_low_scan_start(void); +static void ctsu_diag_sscg_scan_start(void); +static void ctsu_diag_dac_scan_start(ctsu_instance_ctrl_t * const p_instance_ctrl); + +static void ctsu_diag_ldo_over_voltage_data_get(void); +static void ctsu_diag_oscillator_high_data_get(void); +static void ctsu_diag_oscillator_low_data_get(void); +static void ctsu_diag_sscg_data_get(void); +static void ctsu_diag_dac_data_get(void); + +static fsp_err_t ctsu_diag_ldo_over_voltage_result(void); +static fsp_err_t ctsu_diag_oscillator_high_result(void); +static fsp_err_t ctsu_diag_oscillator_low_result(void); +static fsp_err_t ctsu_diag_sscg_result(void); +static fsp_err_t ctsu_diag_dac_result(void); + +static void ctsu_diag_scan_start1(ctsu_instance_ctrl_t * const p_instance_ctrl); +static fsp_err_t ctsu_diag_data_get1(void); + + #endif +#endif + +#if (BSP_FEATURE_CTSU_VERSION == 2) + #if (CTSU_CFG_DIAG_SUPPORT_ENABLE == 1) + +static void ctsu_diag_regi_store2(void); +static void ctsu_diag_regi_restore2(void); + +static fsp_err_t ctsu_diag_output_voltage_scan_start (ctsu_instance_ctrl_t * const p_instance_ctrl); +static fsp_err_t ctsu_diag_output_voltage_result(void); + +static void ctsu_diag_over_voltage_scan_start(void); +static fsp_err_t ctsu_diag_over_voltage_result(void); + +static void ctsu_diag_over_current_scan_start(void); +static fsp_err_t ctsu_diag_over_current_result(void); + +static void ctsu_diag_load_resistance_scan_start(void); +static void ctsu_diag_load_resistance_data_get(void); +static fsp_err_t ctsu_diag_load_resistance_result(void); + +static void ctsu_diag_current_source_scan_start(void); +static void ctsu_diag_current_source_data_get(void); +static fsp_err_t ctsu_diag_current_source_result(void); + +static void ctsu_diag_cco_gain_scan_start(void); +static void ctsu_diag_cco_gain_data_get(void); +static fsp_err_t ctsu_diag_cco_gain_result(void); + +static void ctsu_diag_clock_recovery_scan_start(void); +static void ctsu_diag_clock_recovery_data_get(void); +static fsp_err_t ctsu_diag_clock_recovery_result(void); + + #if (CTSU_CFG_NUM_CFC != 0) +static fsp_err_t ctsu_diag_cfc_gain_result(void); +static void ctsu_diag_cfc_gain_scan_start(void); +static void ctsu_diag_cfc_gain_data_get(void); + + #endif + +static fsp_err_t ctsu_diag_scan_start2(ctsu_instance_ctrl_t * const p_instance_ctrl); +static fsp_err_t ctsu_diag_data_get2(uint16_t * p_data); + + #endif +#endif + +/*********************************************************************************************************************** + * Private global variables + **********************************************************************************************************************/ + +static uint16_t g_ctsu_element_index = 0; +static uint8_t g_ctsu_element_complete_flag[CTSU_CFG_NUM_SELF_ELEMENTS + CTSU_CFG_NUM_MUTUAL_ELEMENTS]; +#if (BSP_FEATURE_CTSU_VERSION == 2) +static uint8_t g_ctsu_frequency_complete_flag[CTSU_CFG_NUM_SELF_ELEMENTS + CTSU_CFG_NUM_MUTUAL_ELEMENTS]; +#endif +static int32_t g_ctsu_tuning_diff[CTSU_CFG_NUM_SELF_ELEMENTS + CTSU_CFG_NUM_MUTUAL_ELEMENTS]; +static ctsu_ctsuwr_t g_ctsu_ctsuwr[(CTSU_CFG_NUM_SELF_ELEMENTS + CTSU_CFG_NUM_MUTUAL_ELEMENTS + + CTSU_SERIAL_TUNING_ELEMENT) * CTSU_CFG_NUM_SUMULTI]; +#if (CTSU_CFG_NUM_SELF_ELEMENTS != 0) +static uint16_t g_ctsu_self_element_index = 0; +static ctsu_self_buf_t g_ctsu_self_raw[CTSU_CFG_NUM_SELF_ELEMENTS * CTSU_CFG_NUM_SUMULTI]; +static uint16_t g_ctsu_self_corr[CTSU_CFG_NUM_SELF_ELEMENTS * CTSU_CFG_NUM_SUMULTI]; +static uint16_t g_ctsu_self_mfc[CTSU_CFG_NUM_SELF_ELEMENTS * CTSU_CFG_NUM_SUMULTI]; +static ctsu_data_t g_ctsu_self_data[CTSU_CFG_NUM_SELF_ELEMENTS * CTSU_MAJORITY_MODE_ELEMENTS]; +#endif +#if (CTSU_CFG_NUM_MUTUAL_ELEMENTS != 0) +static uint16_t g_ctsu_mutual_element_index = 0; +static ctsu_mutual_buf_t g_ctsu_mutual_raw[CTSU_CFG_NUM_MUTUAL_ELEMENTS * CTSU_MUTUAL_BUF_SIZE]; +static uint16_t g_ctsu_mutual_pri_corr[CTSU_CFG_NUM_MUTUAL_ELEMENTS * CTSU_CFG_NUM_SUMULTI]; +static uint16_t g_ctsu_mutual_snd_corr[CTSU_CFG_NUM_MUTUAL_ELEMENTS * CTSU_CFG_NUM_SUMULTI]; +static uint16_t g_ctsu_mutual_pri_mfc[CTSU_CFG_NUM_MUTUAL_ELEMENTS * CTSU_CFG_NUM_SUMULTI]; +static uint16_t g_ctsu_mutual_snd_mfc[CTSU_CFG_NUM_MUTUAL_ELEMENTS * CTSU_CFG_NUM_SUMULTI]; +static ctsu_data_t g_ctsu_mutual_pri_data[CTSU_CFG_NUM_MUTUAL_ELEMENTS * CTSU_MAJORITY_MODE_ELEMENTS]; +static ctsu_data_t g_ctsu_mutual_snd_data[CTSU_CFG_NUM_MUTUAL_ELEMENTS * CTSU_MAJORITY_MODE_ELEMENTS]; +#endif +static ctsu_correction_info_t g_ctsu_correction_info; + +#if (CTSU_CFG_DTC_SUPPORT_ENABLE == 1) +static dtc_cmd_arg_t g_ctsu_dtc_cmd_arg; /* DTC command argument */ + #if (CTSU_CFG_AUTO_JUDGE_ENABLE == 1) + #if (CTSU_CFG_AUTO_MULTI_CLOCK_CORRECTION_ENABLE == 1) +static dtc_transfer_data_cfg_t g_ctsu_dtc_info[12]; /* table of DTC setting information */ +static dtc_transfer_data_t g_transfer_data_tx[12]; + #else +static dtc_transfer_data_cfg_t g_ctsu_dtc_info[6]; /* table of DTC setting information */ +static dtc_transfer_data_t g_transfer_data_tx[6]; + #endif +static dtc_transfer_data_t g_transfer_data_rx[6]; + #else + #if (CTSU_CFG_AUTO_MULTI_CLOCK_CORRECTION_ENABLE == 1) +static dtc_transfer_data_cfg_t g_ctsu_dtc_info[2]; /* table of DTC setting information */ +static dtc_transfer_data_t g_transfer_data_tx[2]; + #else +static dtc_transfer_data_cfg_t g_ctsu_dtc_info[1]; /* table of DTC setting information */ +static dtc_transfer_data_t g_transfer_data_tx[1]; + #endif +static dtc_transfer_data_t g_transfer_data_rx[1]; + #endif +#endif + +#if (BSP_FEATURE_CTSU_VERSION == 1) + #if (CTSU_CFG_DIAG_SUPPORT_ENABLE == 1) +static ctsu_diag_info_t g_ctsu_diag_info; +static ctsu_diag_save_reg_t g_ctsu_diag_reg; + #endif +#endif + +#if (BSP_FEATURE_CTSU_VERSION == 2) + #if (CTSU_CFG_NUM_CFC != 0) +static ctsu_corrcfc_info_t g_ctsu_corrcfc_info; + #endif + + #if (CTSU_CFG_DIAG_SUPPORT_ENABLE == 1) +static ctsu_diag_info_t g_ctsu_diag_info; +static ctsu_diag_save_reg_t g_ctsu_diag_reg; + #endif + #if (CTSU_CFG_TEMP_CORRECTION_SUPPORT == 1) +static uint32_t g_ctsu_temp_reg_ctsucra; + #endif + #if (CTSU_CFG_AUTO_JUDGE_ENABLE == 1) +static ctsu_auto_judge_t g_ctsu_auto_judge[CTSU_AUTO_JUDGE_ELEMENT_NUM * CTSU_MAJORITY_MODE_ELEMENTS]; + #endif + #if (CTSU_CFG_AUTO_MULTI_CLOCK_CORRECTION_ENABLE == 1) +static uint32_t g_ctsu_mcact1[(CTSU_CFG_NUM_SELF_ELEMENTS + CTSU_CFG_NUM_MUTUAL_ELEMENTS) * CTSU_CFG_NUM_SUMULTI]; +static uint32_t g_ctsu_mcact2[(CTSU_CFG_NUM_SELF_ELEMENTS + CTSU_CFG_NUM_MUTUAL_ELEMENTS) * CTSU_CFG_NUM_SUMULTI]; + #endif + + #if (CTSU_CFG_NUM_SELF_ELEMENTS != 0) +uint8_t g_ctsu_selected_freq_self[CTSU_CFG_NUM_SELF_ELEMENTS]; + #endif + #if (CTSU_CFG_NUM_MUTUAL_ELEMENTS != 0) +uint8_t g_ctsu_selected_freq_mutual[CTSU_CFG_NUM_MUTUAL_ELEMENTS]; + #endif + +#endif + + +#if (BSP_FEATURE_CTSU_VERSION == 1) + #if (CTSU_CFG_DIAG_SUPPORT_ENABLE == 1) +static const uint16_t dac_oscil_table[6][2] = +{ + {CTSU_CFG_DIAG_DAC1_MAX, CTSU_CFG_DIAG_DAC1_MIN}, + {CTSU_CFG_DIAG_DAC2_MAX, CTSU_CFG_DIAG_DAC2_MIN}, + {CTSU_CFG_DIAG_DAC3_MAX, CTSU_CFG_DIAG_DAC3_MIN}, + {CTSU_CFG_DIAG_DAC4_MAX, CTSU_CFG_DIAG_DAC4_MIN}, + {CTSU_CFG_DIAG_DAC5_MAX, CTSU_CFG_DIAG_DAC5_MIN}, + {CTSU_CFG_DIAG_DAC6_MAX, CTSU_CFG_DIAG_DAC6_MIN}, +}; + #endif +#endif + +#if (BSP_FEATURE_CTSU_VERSION == 2) + #if (CTSU_CFG_DIAG_SUPPORT_ENABLE == 1) +static const uint16_t cco_gain_table[12][2] = +{ + {CTSU_CFG_DIAG_DAC1_MIN, CTSU_CFG_DIAG_DAC1_MAX }, + {CTSU_CFG_DIAG_DAC2_MIN, CTSU_CFG_DIAG_DAC2_MAX }, + {CTSU_CFG_DIAG_DAC3_MIN, CTSU_CFG_DIAG_DAC3_MAX }, + {CTSU_CFG_DIAG_DAC4_MIN, CTSU_CFG_DIAG_DAC4_MAX }, + {CTSU_CFG_DIAG_DAC5_MIN, CTSU_CFG_DIAG_DAC5_MAX }, + {CTSU_CFG_DIAG_DAC6_MIN, CTSU_CFG_DIAG_DAC6_MAX }, + {CTSU_CFG_DIAG_DAC7_MIN, CTSU_CFG_DIAG_DAC7_MAX }, + {CTSU_CFG_DIAG_DAC8_MIN, CTSU_CFG_DIAG_DAC8_MAX }, + {CTSU_CFG_DIAG_DAC9_MIN, CTSU_CFG_DIAG_DAC9_MAX }, + {CTSU_CFG_DIAG_DAC10_MIN, CTSU_CFG_DIAG_DAC10_MAX }, + {CTSU_CFG_DIAG_DAC11_MIN, CTSU_CFG_DIAG_DAC11_MAX }, + {CTSU_CFG_DIAG_DAC12_MIN, CTSU_CFG_DIAG_DAC12_MAX }, +}; + +static const uint16_t cco_gain_diff_table[11][2] = +{ + {CTSU_CFG_DIAG_DAC1_2_DIFF_MIN, CTSU_CFG_DIAG_DAC1_2_DIFF_MAX }, + {CTSU_CFG_DIAG_DAC2_3_DIFF_MIN, CTSU_CFG_DIAG_DAC2_3_DIFF_MAX }, + {CTSU_CFG_DIAG_DAC3_4_DIFF_MIN, CTSU_CFG_DIAG_DAC3_4_DIFF_MAX }, + {CTSU_CFG_DIAG_DAC4_5_DIFF_MIN, CTSU_CFG_DIAG_DAC4_5_DIFF_MAX }, + {CTSU_CFG_DIAG_DAC5_6_DIFF_MIN, CTSU_CFG_DIAG_DAC5_6_DIFF_MAX }, + {CTSU_CFG_DIAG_DAC6_7_DIFF_MIN, CTSU_CFG_DIAG_DAC6_7_DIFF_MAX }, + {CTSU_CFG_DIAG_DAC7_8_DIFF_MIN, CTSU_CFG_DIAG_DAC7_8_DIFF_MAX }, + {CTSU_CFG_DIAG_DAC8_9_DIFF_MIN, CTSU_CFG_DIAG_DAC8_9_DIFF_MAX }, + {CTSU_CFG_DIAG_DAC9_10_DIFF_MIN, CTSU_CFG_DIAG_DAC9_10_DIFF_MAX }, + {CTSU_CFG_DIAG_DAC10_11_DIFF_MIN, CTSU_CFG_DIAG_DAC10_11_DIFF_MAX }, + {CTSU_CFG_DIAG_DAC11_12_DIFF_MIN, CTSU_CFG_DIAG_DAC11_12_DIFF_MAX }, +}; + #endif +#endif + +/*********************************************************************************************************************** + * Global variables + **********************************************************************************************************************/ +const ctsu_api_t g_ctsu_on_ctsu = +{ + .open = R_CTSU_Open, + .scanStart = R_CTSU_ScanStart, + .dataGet = R_CTSU_DataGet, + .scanStop = R_CTSU_ScanStop, + .diagnosis = R_CTSU_Diagnosis, + .close = R_CTSU_Close, + .callbackSet = R_CTSU_CallbackSet, + .specificDataGet = R_CTSU_SpecificDataGet, + .dataInsert = R_CTSU_DataInsert, + .offsetTuning = R_CTSU_OffsetTuning, +}; + +ctsu_instance_ctrl_t * gp_ctsu_isr_context; + +/*******************************************************************************************************************//** + * @addtogroup CTSU + * @{ + **********************************************************************************************************************/ + +/*********************************************************************************************************************** + * Functions + **********************************************************************************************************************/ + +/*******************************************************************************************************************//** + * @brief Opens and configures the CTSU driver module. Implements @ref ctsu_api_t::open. + * + * Example: + * @snippet r_ctsu_example.c R_CTSU_Open + * + * @retval FSP_SUCCESS CTSU successfully configured. + * @retval FSP_ERR_ASSERTION Null pointer, or one or more configuration options is invalid. + * @retval FSP_ERR_ALREADY_OPEN Module is already open. This module can only be opened once. + * @retval FSP_ERR_INVALID_ARGUMENT Configuration parameter error. + * + * @note In the first Open, measurement for correction works, and it takes several tens of milliseconds. + **********************************************************************************************************************/ +fsp_err_t R_CTSU_Open (ctsu_ctrl_t * const p_ctrl, ctsu_cfg_t const * const p_cfg) +{ + ctsu_instance_ctrl_t * p_instance_ctrl = (ctsu_instance_ctrl_t *) p_ctrl; + fsp_err_t err = FSP_SUCCESS; + uint16_t element_id; + const ctsu_element_cfg_t * element_cfgs; +#if (BSP_FEATURE_CTSU_VERSION == 2) + uint16_t i; + uint32_t pclkb_mhz; + uint16_t suadj[3]; +#endif + +#if (CTSU_CFG_PARAM_CHECKING_ENABLE == 1) + FSP_ASSERT(p_instance_ctrl); + FSP_ASSERT(p_cfg); +#endif + FSP_ERROR_RETURN(CTSU_OPEN != p_instance_ctrl->open, FSP_ERR_ALREADY_OPEN); + + /* Check element number */ +#if (CTSU_CFG_NUM_SELF_ELEMENTS != 0) + if (CTSU_MODE_SELF_MULTI_SCAN == (CTSU_MODE_MUTUAL_FULL_SCAN & p_cfg->md)) + { + FSP_ERROR_RETURN(CTSU_CFG_NUM_SELF_ELEMENTS >= + (uint8_t) (g_ctsu_self_element_index + p_cfg->num_rx), + FSP_ERR_INVALID_ARGUMENT); + } +#endif +#if (CTSU_CFG_NUM_MUTUAL_ELEMENTS != 0) + if (CTSU_MODE_MUTUAL_FULL_SCAN == (CTSU_MODE_MUTUAL_FULL_SCAN & p_cfg->md)) + { + FSP_ERROR_RETURN(CTSU_CFG_NUM_MUTUAL_ELEMENTS >= + (uint8_t) (g_ctsu_mutual_element_index + (p_cfg->num_rx * p_cfg->num_tx)), + FSP_ERR_INVALID_ARGUMENT); + } +#endif + p_instance_ctrl->state = CTSU_STATE_INIT; + + /* Save configurations. */ + p_instance_ctrl->p_ctsu_cfg = p_cfg; + + /* Initialize driver control structure (address setting) */ +#if (CTSU_CFG_NUM_SELF_ELEMENTS != 0) + if (CTSU_MODE_SELF_MULTI_SCAN == (CTSU_MODE_MUTUAL_FULL_SCAN & p_cfg->md)) + { + p_instance_ctrl->p_self_raw = &g_ctsu_self_raw[g_ctsu_self_element_index * CTSU_CFG_NUM_SUMULTI]; + p_instance_ctrl->p_self_corr = &g_ctsu_self_corr[g_ctsu_self_element_index * CTSU_CFG_NUM_SUMULTI]; + p_instance_ctrl->p_self_mfc = &g_ctsu_self_mfc[g_ctsu_self_element_index * CTSU_CFG_NUM_SUMULTI]; + p_instance_ctrl->p_self_data = &g_ctsu_self_data[g_ctsu_self_element_index * CTSU_MAJORITY_MODE_ELEMENTS]; + p_instance_ctrl->num_elements = p_cfg->num_rx; + #if (BSP_FEATURE_CTSU_VERSION == 2) + p_instance_ctrl->p_selected_freq_self = &g_ctsu_selected_freq_self[g_ctsu_self_element_index]; + #endif + g_ctsu_self_element_index = (uint8_t) (g_ctsu_self_element_index + p_instance_ctrl->num_elements); + p_instance_ctrl->self_elem_index = g_ctsu_self_element_index; + + #if (CTSU_CFG_NUM_MUTUAL_ELEMENTS != 0) + p_instance_ctrl->p_mutual_raw = &g_ctsu_mutual_raw[0]; + p_instance_ctrl->p_mutual_pri_corr = &g_ctsu_mutual_pri_corr[0]; + p_instance_ctrl->p_mutual_snd_corr = &g_ctsu_mutual_snd_corr[0]; + p_instance_ctrl->p_mutual_pri_mfc = &g_ctsu_mutual_pri_mfc[0]; + p_instance_ctrl->p_mutual_snd_mfc = &g_ctsu_mutual_snd_mfc[0]; + p_instance_ctrl->p_mutual_pri_data = &g_ctsu_mutual_pri_data[0]; + p_instance_ctrl->p_mutual_snd_data = &g_ctsu_mutual_snd_data[0]; + #endif + } +#endif +#if (CTSU_CFG_NUM_MUTUAL_ELEMENTS != 0) + if (CTSU_MODE_MUTUAL_FULL_SCAN == (CTSU_MODE_MUTUAL_FULL_SCAN & p_cfg->md)) + { + p_instance_ctrl->p_mutual_raw = &g_ctsu_mutual_raw[g_ctsu_mutual_element_index * CTSU_MUTUAL_BUF_SIZE]; + p_instance_ctrl->p_mutual_pri_corr = + &g_ctsu_mutual_pri_corr[g_ctsu_mutual_element_index * CTSU_CFG_NUM_SUMULTI]; + p_instance_ctrl->p_mutual_snd_corr = + &g_ctsu_mutual_snd_corr[g_ctsu_mutual_element_index * CTSU_CFG_NUM_SUMULTI]; + p_instance_ctrl->p_mutual_pri_mfc = + &g_ctsu_mutual_pri_mfc[g_ctsu_mutual_element_index * CTSU_CFG_NUM_SUMULTI]; + p_instance_ctrl->p_mutual_snd_mfc = + &g_ctsu_mutual_snd_mfc[g_ctsu_mutual_element_index * CTSU_CFG_NUM_SUMULTI]; + p_instance_ctrl->p_mutual_pri_data = + &g_ctsu_mutual_pri_data[g_ctsu_mutual_element_index * CTSU_MAJORITY_MODE_ELEMENTS]; + p_instance_ctrl->p_mutual_snd_data = + &g_ctsu_mutual_snd_data[g_ctsu_mutual_element_index * CTSU_MAJORITY_MODE_ELEMENTS]; + p_instance_ctrl->num_elements = (uint8_t) (p_cfg->num_rx * p_cfg->num_tx); + #if (BSP_FEATURE_CTSU_VERSION == 2) + p_instance_ctrl->p_selected_freq_mutual = &g_ctsu_selected_freq_mutual[g_ctsu_mutual_element_index]; + #endif + g_ctsu_mutual_element_index = (uint8_t) (g_ctsu_mutual_element_index + p_instance_ctrl->num_elements); + p_instance_ctrl->mutual_elem_index = g_ctsu_mutual_element_index; + + #if (CTSU_CFG_NUM_SELF_ELEMENTS != 0) + p_instance_ctrl->p_self_raw = &g_ctsu_self_raw[0]; + p_instance_ctrl->p_self_corr = &g_ctsu_self_corr[0]; + p_instance_ctrl->p_self_mfc = &g_ctsu_self_mfc[0]; + p_instance_ctrl->p_self_data = &g_ctsu_self_data[0]; + #endif + } +#endif + + p_instance_ctrl->p_element_complete_flag = &g_ctsu_element_complete_flag[g_ctsu_element_index]; +#if (BSP_FEATURE_CTSU_VERSION == 2) + p_instance_ctrl->p_frequency_complete_flag = &g_ctsu_frequency_complete_flag[g_ctsu_element_index]; +#endif + p_instance_ctrl->p_tuning_diff = &g_ctsu_tuning_diff[g_ctsu_element_index]; + p_instance_ctrl->p_ctsuwr = &g_ctsu_ctsuwr[g_ctsu_element_index * CTSU_CFG_NUM_SUMULTI]; +#if (BSP_FEATURE_CTSU_VERSION == 2) + #if (CTSU_CFG_AUTO_JUDGE_ENABLE == 1) + + /* Secure DTC transfer area for automatic judgement, store start address and transfer count */ + p_instance_ctrl->p_auto_judge = &g_ctsu_auto_judge[g_ctsu_element_index * CTSU_MAJORITY_MODE_ELEMENTS]; + p_instance_ctrl->adress_auto_judge = (uint32_t)p_instance_ctrl->p_auto_judge; + p_instance_ctrl->adress_ctsuwr = (uint32_t)p_instance_ctrl->p_ctsuwr; + p_instance_ctrl->adress_self_raw = (uint32_t)p_instance_ctrl->p_self_raw; + #if (CTSU_CFG_NUM_MUTUAL_ELEMENTS != 0) + p_instance_ctrl->adress_mutual_raw = (uint32_t)p_instance_ctrl->p_mutual_raw; + #endif + #if (CTSU_CFG_AUTO_MULTI_CLOCK_CORRECTION_ENABLE == 1) + if (1 == p_instance_ctrl->p_ctsu_cfg->majirimd) + { + p_instance_ctrl->count_auto_judge = (uint32_t)((CTSU_AUTO_JUDGE_BLOCK_SIZE << 24) | (CTSU_AUTO_JUDGE_BLOCK_SIZE << 16) | (p_instance_ctrl->num_elements)); + p_instance_ctrl->count_ctsuwr_self_mutual = (uint32_t)((CTSU_AUTO_CTSUSO_BLOCK_SIZE << 24) | (CTSU_AUTO_CTSUSO_BLOCK_SIZE << 16) | (p_instance_ctrl->num_elements)); + p_instance_ctrl->adress_mcact1 = (uint32_t)p_instance_ctrl->p_mcact1; + p_instance_ctrl->count_mcact1 = (uint32_t)((CTSU_AUTO_MCACT_BLOCK_SIZE << 24) | (CTSU_AUTO_MCACT_BLOCK_SIZE << 16) | (p_instance_ctrl->num_elements)); + p_instance_ctrl->adress_mcact2 = (uint32_t)p_instance_ctrl->p_mcact2; + p_instance_ctrl->count_mcact2 = (uint32_t)((CTSU_AUTO_MCACT_BLOCK_SIZE << 24) | (CTSU_AUTO_MCACT_BLOCK_SIZE << 16) | (p_instance_ctrl->num_elements)); + } + else + #endif + { + p_instance_ctrl->count_auto_judge = (uint32_t)((CTSU_AUTO_JUDGE_BLOCK_SIZE << 24) | (CTSU_AUTO_JUDGE_BLOCK_SIZE << 16) | (p_instance_ctrl->num_elements * CTSU_CFG_NUM_SUMULTI)); + p_instance_ctrl->count_ctsuwr_self_mutual = (uint32_t)((CTSU_AUTO_CTSUSO_BLOCK_SIZE << 24) | (CTSU_AUTO_CTSUSO_BLOCK_SIZE << 16) | (p_instance_ctrl->num_elements * CTSU_CFG_NUM_SUMULTI)); + } + + /* Copy to ctrl variable for setting change for automatic judgement */ + p_instance_ctrl->ajmmat = p_cfg->ajmmat; + p_instance_ctrl->ajbmat = p_cfg->ajbmat; + #endif + #if (CTSU_CFG_AUTO_MULTI_CLOCK_CORRECTION_ENABLE == 1) + p_instance_ctrl->p_mcact1 = &g_ctsu_mcact1[g_ctsu_element_index * CTSU_CFG_NUM_SUMULTI]; + p_instance_ctrl->p_mcact2 = &g_ctsu_mcact2[g_ctsu_element_index * CTSU_CFG_NUM_SUMULTI]; + p_instance_ctrl->mcact_flag = 0; + #endif +#endif + g_ctsu_element_index = (uint8_t) (g_ctsu_element_index + p_instance_ctrl->num_elements); + p_instance_ctrl->ctsu_elem_index = g_ctsu_element_index; + + /* Set Value */ + p_instance_ctrl->cap = p_cfg->cap; + p_instance_ctrl->num_moving_average = p_cfg->num_moving_average; + p_instance_ctrl->average = 0; + if (true == p_cfg->tuning_enable) + { + p_instance_ctrl->tuning = CTSU_TUNING_INCOMPLETE; + } + else + { + p_instance_ctrl->tuning = CTSU_TUNING_COMPLETE; + } + +#if (BSP_FEATURE_CTSU_VERSION == 2) + #if (CTSU_CFG_NUM_CFC != 0) + if (CTSU_MODE_MUTUAL_CFC_SCAN == p_cfg->md) + { + p_instance_ctrl->tuning = CTSU_TUNING_COMPLETE; + } + #endif + + if (CTSU_MODE_CURRENT_SCAN == p_cfg->md) + { + p_instance_ctrl->tuning = CTSU_TUNING_COMPLETE; + } +#endif + +#if (BSP_FEATURE_CTSU_VERSION == 1) + p_instance_ctrl->ctsucr1 = (uint8_t) (p_cfg->atune1 << 3); +#endif +#if (BSP_FEATURE_CTSU_VERSION == 2) + p_instance_ctrl->ctsucr1 = (uint8_t) ((p_cfg->atune12 & 0x01) << 3); +#endif + p_instance_ctrl->ctsucr1 |= (uint8_t) (p_cfg->md << 6); + + p_instance_ctrl->txvsel = p_instance_ctrl->p_ctsu_cfg->txvsel; + p_instance_ctrl->txvsel2 = p_instance_ctrl->p_ctsu_cfg->txvsel2; + + p_instance_ctrl->ctsuchac0 = p_instance_ctrl->p_ctsu_cfg->ctsuchac0; + p_instance_ctrl->ctsuchac1 = p_instance_ctrl->p_ctsu_cfg->ctsuchac1; + p_instance_ctrl->ctsuchac2 = p_instance_ctrl->p_ctsu_cfg->ctsuchac2; + p_instance_ctrl->ctsuchac3 = p_instance_ctrl->p_ctsu_cfg->ctsuchac3; + p_instance_ctrl->ctsuchac4 = p_instance_ctrl->p_ctsu_cfg->ctsuchac4; + + p_instance_ctrl->ctsuchtrc0 = p_instance_ctrl->p_ctsu_cfg->ctsuchtrc0; + p_instance_ctrl->ctsuchtrc1 = p_instance_ctrl->p_ctsu_cfg->ctsuchtrc1; + p_instance_ctrl->ctsuchtrc2 = p_instance_ctrl->p_ctsu_cfg->ctsuchtrc2; + p_instance_ctrl->ctsuchtrc3 = p_instance_ctrl->p_ctsu_cfg->ctsuchtrc3; + p_instance_ctrl->ctsuchtrc4 = p_instance_ctrl->p_ctsu_cfg->ctsuchtrc4; + + p_instance_ctrl->md = p_instance_ctrl->p_ctsu_cfg->md; + +#if (BSP_FEATURE_CTSU_VERSION == 2) + p_instance_ctrl->ctsucr2 = (uint8_t) (p_cfg->atune12 & 0x02); + p_instance_ctrl->ctsucr2 |= (uint8_t) ((p_cfg->md & 0x04) >> 2); + p_instance_ctrl->ctsucr2 |= (uint8_t) (p_cfg->posel << 4); + + if (CTSU_ATUNE12_80UA == p_cfg->atune12) + { + p_instance_ctrl->range = CTSU_RANGE_80UA; + } + else if (CTSU_ATUNE12_40UA == p_cfg->atune12) + { + p_instance_ctrl->range = CTSU_RANGE_40UA; + } + else if (CTSU_ATUNE12_20UA == p_cfg->atune12) + { + p_instance_ctrl->range = CTSU_RANGE_20UA; + } + else if (CTSU_ATUNE12_160UA == p_cfg->atune12) + { + p_instance_ctrl->range = CTSU_RANGE_160UA; + } + else + { + } + + #if (CTSU_CFG_MULTIPLE_ELECTRODE_CONNECTION_ENABLE == 1) + /* Copy the variables used for the MEC function to the ctrl instance */ + p_instance_ctrl->tsod = p_instance_ctrl->p_ctsu_cfg->tsod; + p_instance_ctrl->mec_ts = p_instance_ctrl->p_ctsu_cfg->mec_ts; + p_instance_ctrl->mec_shield_ts = p_instance_ctrl->p_ctsu_cfg->mec_shield_ts; + #endif +#endif + for (element_id = 0; element_id < p_instance_ctrl->num_elements; element_id++) + { + p_instance_ctrl->p_element_complete_flag[element_id] = 0; +#if (BSP_FEATURE_CTSU_VERSION == 2) + p_instance_ctrl->p_frequency_complete_flag[element_id] = 0; +#endif + p_instance_ctrl->p_tuning_diff[element_id] = 0; + element_cfgs = (p_cfg->p_elements + element_id); +#if (BSP_FEATURE_CTSU_VERSION == 2) + if (CTSU_MODE_CURRENT_SCAN == p_cfg->md) + { + /* Current scan does not run multiple frequency */ + p_instance_ctrl->p_ctsuwr[element_id].ctsuso = + (uint32_t) (((uint32_t) element_cfgs->sdpa << (uint32_t) 24) | + ((uint32_t) element_cfgs->snum << (uint32_t) 10) | element_cfgs->so); + } + else + { + if (CTSU_MODE_MUTUAL_CFC_SCAN != p_cfg->md) + { + p_instance_ctrl->p_ctsuwr[element_id * CTSU_CFG_NUM_SUMULTI].ctsuso = + (uint32_t) (((uint32_t) element_cfgs->sdpa << (uint32_t) 24) | + ((uint32_t) element_cfgs->snum << (uint32_t) 10) | element_cfgs->so); + } + + #if (CTSU_CFG_NUM_CFC != 0) + else + { + /* CFC scan does not use CTSUSO */ + p_instance_ctrl->p_ctsuwr[element_id * CTSU_CFG_NUM_SUMULTI].ctsuso = + (uint32_t) (((uint32_t) element_cfgs->sdpa << (uint32_t) 24) | + ((uint32_t) element_cfgs->snum << (uint32_t) 10)); + } + #endif + + for (i = 1; i < CTSU_CFG_NUM_SUMULTI; i++) + { + p_instance_ctrl->p_ctsuwr[(element_id * CTSU_CFG_NUM_SUMULTI) + i].ctsuso = + p_instance_ctrl->p_ctsuwr[element_id * CTSU_CFG_NUM_SUMULTI].ctsuso; + } + } +#endif +#if (BSP_FEATURE_CTSU_VERSION == 1) + p_instance_ctrl->p_ctsuwr[element_id].ctsussc = (uint16_t) (element_cfgs->ssdiv << 8); + p_instance_ctrl->p_ctsuwr[element_id].ctsuso0 = (uint16_t) ((element_cfgs->snum << 10) | element_cfgs->so); + p_instance_ctrl->p_ctsuwr[element_id].ctsuso1 = + (uint16_t) ((CTSU_ICOG_RECOMMEND << 13) | (element_cfgs->sdpa << 8) | CTSU_RICOA_RECOMMEND); +#endif + } + + /* Enable interrupts for write, read, & scan done */ + IR(CTSU,CTSUWR)= 0; + IR(CTSU,CTSURD)= 0; + IR(CTSU,CTSUFN)= 0; + IPR(CTSU,CTSUWR)= CTSU_CFG_INTCTSUWR_PRIORITY_LEVEL; + IPR(CTSU,CTSURD)= CTSU_CFG_INTCTSURD_PRIORITY_LEVEL; + IPR(CTSU,CTSUFN)= CTSU_CFG_INTCTSUFN_PRIORITY_LEVEL; + R_BSP_InterruptRequestEnable(VECT(CTSU,CTSUWR)); + R_BSP_InterruptRequestEnable(VECT(CTSU,CTSURD)); + R_BSP_InterruptRequestEnable(VECT(CTSU,CTSUFN)); + + gp_ctsu_isr_context = p_instance_ctrl; + + p_instance_ctrl->interrupt_reverse_flag = 0; + + if (p_instance_ctrl->num_elements == g_ctsu_element_index) + { + /* TSCAP discharge process */ + R_Set_CTSU_TSCAP_Discharge(); + R_BSP_SoftwareDelay(30,BSP_DELAY_MICROSECS); + R_Set_CTSU_TSCAP_Charge(); + + /* Get CTSU out of stop state (supply power/clock) */ + R_BSP_RegisterProtectDisable (BSP_REG_PROTECT_LPC_CGC_SWR); + MSTP(CTSU) = 0; + R_BSP_RegisterProtectEnable (BSP_REG_PROTECT_LPC_CGC_SWR); + +#if (CTSU_CFG_DTC_SUPPORT_ENABLE == 1) + err = ctsu_transfer_open(p_instance_ctrl); +#endif +#if (BSP_FEATURE_CTSU_VERSION == 2) + CTSU.CTSUCRA.BIT.ATUNE0 = CTSU_CFG_LOW_VOLTAGE_MODE; + CTSU.CTSUCRA.BIT.PUMPON = 1; + + pclkb_mhz = BSP_PCLKB_HZ / CTSU_PCLKB_FREQ_MHZ; + + if (CTSU_PCLKB_FREQ_RANGE1 >= pclkb_mhz) + { + CTSU.CTSUCRA.BIT.CLK = 0; + CTSU.CTSUCRA.BIT.STCLK = (uint8_t) (pclkb_mhz - 1); + } + else if ((CTSU_PCLKB_FREQ_RANGE1 < pclkb_mhz) && (CTSU_PCLKB_FREQ_RANGE2 >= pclkb_mhz)) + { + CTSU.CTSUCRA.BIT.CLK = 1; + CTSU.CTSUCRA.BIT.STCLK = (uint8_t) ((pclkb_mhz >> 1) - 1); + } + else if ((CTSU_PCLKB_FREQ_RANGE2 < pclkb_mhz) && (CTSU_PCLKB_FREQ_RANGE3 >= pclkb_mhz)) + { + CTSU.CTSUCRA.BIT.CLK = 2; + CTSU.CTSUCRA.BIT.STCLK = (uint8_t) ((pclkb_mhz >> 2) - 1); + } + else + { + CTSU.CTSUCRA.BIT.CLK = 3; + CTSU.CTSUCRA.BIT.STCLK = (uint8_t) ((pclkb_mhz >> 3) - 1); + } +#endif +#if (BSP_FEATURE_CTSU_VERSION == 1) + + /* Set power on */ + CTSU.CTSUCR1.BYTE = ((CTSU_CFG_LOW_VOLTAGE_MODE << 2) | (CTSU_CSW_ON << 1) | CTSU_PON_ON); + + /* Synchronous Noise Reduction Setting */ + CTSU.CTSUSDPRS.BYTE = ((CTSU_SOFF_ON << 6) | (CTSU_PRMODE_62_PULSES << 4) | CTSU_PRRATIO_RECOMMEND); + + /* High Pass Noise Reduction- ALWAYS 0x30 as per HW Manual */ + CTSU.CTSUDCLKC.BYTE = ((CTSU_SSCNT << 4) | CTSU_SSMOD); + + /* Sensor Stabilization- ALWAYS 0x10 as per HW Manual */ + CTSU.CTSUSST.BYTE = (CTSU_SST_RECOMMEND); + + if (CTSU_CORRECTION_INIT == g_ctsu_correction_info.status) + { + ctsu_correction_process(p_instance_ctrl); + } + + /* Since CLK is rewritten by correction, set here. */ + CTSU.CTSUCR1.BYTE |= (uint8_t) (CTSU_CFG_PCLK_DIVISION << 4); +#endif + } + +#if (BSP_FEATURE_CTSU_VERSION == 2) + if (CTSU_MODE_MUTUAL_CFC_SCAN != p_cfg->md) + { + /* Set power on */ + CTSU.CTSUCRA.BIT.CSW = CTSU_CSW_ON; + CTSU.CTSUCRA.BIT.PON = CTSU_PON_ON; + R_BSP_SoftwareDelay(30, BSP_DELAY_MICROSECS); + } + + #if (CTSU_CFG_NUM_CFC != 0) + else + { + R_CTSU->CTSUCALIB_b.CFCRDMD = 1; + R_CTSU->CTSUCRA_b.CFCON = 1; + R_BSP_SoftwareDelay(30, BSP_DELAY_UNITS_MICROSECONDS); + } + #endif + + /* High resolution pulse mode */ + CTSU.CTSUCRA.BIT.SDPSEL = 1; + CTSU.CTSUCRA.BIT.PCSEL = 1; + CTSU.CTSUCRB.BIT.SST = CTSU_SST_RECOMMEND; + CTSU.CTSUCALIB.BIT.CCOCLK = 0; + CTSU.CTSUCALIB.BIT.SUCLKEN = 0; + + if (CTSU_MODE_MUTUAL_CFC_SCAN != p_cfg->md) + { + #if (CTSU_CFG_TEMP_CORRECTION_SUPPORT == 1) + if (CTSU_MODE_CORRECTION_SCAN == p_cfg->md) + { + g_ctsu_correction_info.scan_index = CTSU_CORRECTION_POINT_NUM; + } + #endif + if (CTSU_CORRECTION_INIT == g_ctsu_correction_info.status) + { + ctsu_correction_process(p_instance_ctrl); + } + } + + #if (CTSU_CFG_NUM_CFC != 0) + else + { + ctsu_corrcfc_process(p_instance_ctrl); + } + p_instance_ctrl->p_corrcfc_info = &g_ctsu_corrcfc_info; + #endif + + CTSU.CTSUCRA.BIT.LOAD = 1; + CTSU.CTSUCRB.BIT.SSCNT = 1; + CTSU.CTSUCALIB.BIT.SUCARRY = 0; + CTSU.CTSUCALIB.BIT.CCOCALIB = 0; + CTSU.CTSUCALIB.BIT.CCOCLK = 1; + CTSU.CTSUCALIB.BIT.TSOC = 0; + CTSU.CTSUCALIB.BIT.SUCLKEN = 1; + + /* Read SUADJD byte */ + suadj[0] = (uint16_t) ((CTSU.CTSUTRIMA.BIT.SUADJD * (CTSU_CFG_SUMULTI0 + 1)) / (CTSU_SUMULTI_BASE + 1)); + + /* Adjust multi freq */ + suadj[1] = (uint16_t) ((suadj[0] * (CTSU_CFG_SUMULTI1 + 1)) / (CTSU_CFG_SUMULTI0 + 1)); + suadj[2] = (uint16_t) ((suadj[0] * (CTSU_CFG_SUMULTI2 + 1)) / (CTSU_CFG_SUMULTI0 + 1)); + + /* Adjust SSCNT setting */ + suadj[0] = (uint16_t) (suadj[0] - (CTSU_SUADJ_SSCNT_ADJ * CTSU.CTSUCRB.BIT.SSCNT)); + suadj[1] = (uint16_t) (suadj[1] - (CTSU_SUADJ_SSCNT_ADJ * CTSU.CTSUCRB.BIT.SSCNT)); + suadj[2] = (uint16_t) (suadj[2] - (CTSU_SUADJ_SSCNT_ADJ * CTSU.CTSUCRB.BIT.SSCNT)); + + /* Set CTSUSUCLK register */ + CTSU.CTSUCRA.BIT.SDPSEL = 0; + CTSU.CTSUSUCLKA.BIT.SUADJ0 = (uint8_t) suadj[0]; + CTSU.CTSUSUCLKA.BIT.SUMULTI0 = CTSU_CFG_SUMULTI0; + + CTSU.CTSUSUCLKA.BIT.SUADJ1 = (uint8_t) suadj[1]; + CTSU.CTSUSUCLKA.BIT.SUMULTI1 = CTSU_CFG_SUMULTI1; + + CTSU.CTSUSUCLKB.BIT.SUADJ2 = (uint8_t) suadj[2]; + CTSU.CTSUSUCLKB.BIT.SUMULTI2 = CTSU_CFG_SUMULTI2; + + CTSU.CTSUCRA.BIT.SDPSEL = 1; + + #if (CTSU_CFG_AUTO_JUDGE_ENABLE == 1) + if (1 == p_instance_ctrl->p_ctsu_cfg->ajfen) + { + /* Fixed parameter setting for automatic judgement */ + CTSU.CTSUAJCR.BIT.TLOT = p_instance_ctrl->p_ctsu_cfg->tlot; + CTSU.CTSUAJCR.BIT.THOT = p_instance_ctrl->p_ctsu_cfg->thot; + + p_instance_ctrl->blini_flag = 1; + } + #endif +#endif + +#if (BSP_FEATURE_CTSU_VERSION == 1) + #if (CTSU_CFG_DIAG_SUPPORT_ENABLE == 1) + if (CTSU_MODE_DIAGNOSIS_SCAN == p_instance_ctrl->md) + { + /* Initialize diagnosis information */ + p_instance_ctrl->p_diag_info = &g_ctsu_diag_info; + g_ctsu_diag_info.state = CTSU_DIAG_INIT; + g_ctsu_diag_info.ctsuwr.ctsussc = 0; + g_ctsu_diag_info.ctsuwr.ctsuso0 = 0; + g_ctsu_diag_info.ctsuwr.ctsuso1 = 0; + g_ctsu_diag_info.loop_count = 0; + g_ctsu_diag_info.scanbuf.sen = 0; + g_ctsu_diag_info.scanbuf.ref = 0; + g_ctsu_diag_info.correct_data = 0; + g_ctsu_diag_info.icomp = 0; + g_ctsu_diag_info.cco_high = 0; + g_ctsu_diag_info.cco_low = 0; + g_ctsu_diag_info.sscg = 0; + g_ctsu_diag_info.dac_cnt[0] = 0; + g_ctsu_diag_info.dac_cnt[1] = 0; + g_ctsu_diag_info.dac_cnt[2] = 0; + g_ctsu_diag_info.dac_cnt[3] = 0; + g_ctsu_diag_info.dac_cnt[4] = 0; + g_ctsu_diag_info.dac_cnt[5] = 0; + g_ctsu_diag_info.so0_4uc_val = 0; + g_ctsu_diag_info.dac_init = 0; + g_ctsu_diag_info.tuning = CTSU_TUNING_INCOMPLETE; + g_ctsu_diag_info.tuning_diff = 0; + } + #endif +#endif + +#if (BSP_FEATURE_CTSU_VERSION == 2) + #if (CTSU_CFG_DIAG_SUPPORT_ENABLE == 1) + if (CTSU_MODE_DIAGNOSIS_SCAN == p_instance_ctrl->md) + { + p_instance_ctrl->p_diag_info = &g_ctsu_diag_info; + g_ctsu_diag_info.lvmode = CTSU_CFG_LOW_VOLTAGE_MODE; + g_ctsu_diag_info.state = CTSU_DIAG_INIT; + } + else + { + /* Once chac is set, it will not be set after that */ + if ((0 == g_ctsu_diag_info.chaca) && (0 == g_ctsu_diag_info.chacb)) + { + /* From the CHAC information configured in the normal measurement, */ + /* find the minimum TS pin on the CTSU peripheral for the OverCuurent test and clock recovery test. */ + + /* Get CHACA register info */ + ((uint8_t *) &(g_ctsu_diag_info.chaca))[0] = p_instance_ctrl->p_ctsu_cfg->ctsuchac0; + ((uint8_t *) &(g_ctsu_diag_info.chaca))[1] = p_instance_ctrl->p_ctsu_cfg->ctsuchac1; + ((uint8_t *) &(g_ctsu_diag_info.chaca))[2] = p_instance_ctrl->p_ctsu_cfg->ctsuchac2; + ((uint8_t *) &(g_ctsu_diag_info.chaca))[3] = p_instance_ctrl->p_ctsu_cfg->ctsuchac3; + + /* Get CHACB register info */ + g_ctsu_diag_info.chacb = (uint32_t) (p_instance_ctrl->p_ctsu_cfg->ctsuchac4); + + /* Get the number of measurable elements from enabled CHACA */ + if (0 != g_ctsu_diag_info.chaca) + { + /* Get the number of measurable elements from enabled CHACA */ + for (i = 0; i < CTSU_DIAG_CHACA_TSMAX; i++) + { + if ((g_ctsu_diag_info.chaca >> i) & (uint32_t) 0x00000001) + { + g_ctsu_diag_info.chaca = (uint32_t) (0x00000001 << i); + g_ctsu_diag_info.chacb = 0; + break; + } + } + } + else /* Check the measurable elements from enabled CHACB. */ + { + for (i = 0; i < 8; i++) + { + /* Get the number of measurable elements from enabled CHACB */ + if ((g_ctsu_diag_info.chacb >> i) & (uint32_t) 0x00000001) + { + g_ctsu_diag_info.chaca = 0; + g_ctsu_diag_info.chacb = (uint32_t) (0x00000001 << i); + break; + } + } + } + } + } + #endif +#endif + + p_instance_ctrl->p_correction_info = &g_ctsu_correction_info; + p_instance_ctrl->rd_index = 0; + p_instance_ctrl->wr_index = 0; + p_instance_ctrl->state = CTSU_STATE_IDLE; + + p_instance_ctrl->p_callback = p_cfg->p_callback; + p_instance_ctrl->p_context = p_cfg->p_context; + p_instance_ctrl->p_callback_memory = NULL; + + p_instance_ctrl->serial_tuning_enable = 0; + p_instance_ctrl->serial_tuning_mutual_cnt = 0; + +#if defined(CTSU_CFG_TARGET_VALUE_QE_SUPPORT) + p_instance_ctrl->tuning_self_target_value = p_cfg->tuning_self_target_value; + p_instance_ctrl->tuning_mutual_target_value = p_cfg->tuning_mutual_target_value; +#else + p_instance_ctrl->tuning_self_target_value = CTSU_TUNING_VALUE_SELF; + p_instance_ctrl->tuning_mutual_target_value = CTSU_TUNING_VALUE_MUTUAL; +#endif + + /* Mark driver as open */ + p_instance_ctrl->open = CTSU_OPEN; + + return err; +} + +/*******************************************************************************************************************//** + * @brief 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 @ref ctsu_api_t::scanStart. + * + * Example: + * @snippet r_ctsu_example.c R_CTSU_ScanStart + * + * @retval FSP_SUCCESS CTSU successfully configured. + * @retval FSP_ERR_ASSERTION Null pointer passed as a parameter. + * @retval FSP_ERR_NOT_OPEN Module is not open. + * @retval FSP_ERR_CTSU_SCANNING Scanning this instance or other. + * @retval FSP_ERR_CTSU_NOT_GET_DATA The previous data has not been retrieved by DataGet. + **********************************************************************************************************************/ +fsp_err_t R_CTSU_ScanStart (ctsu_ctrl_t * const p_ctrl) +{ + fsp_err_t err = FSP_SUCCESS; + ctsu_instance_ctrl_t * p_instance_ctrl = (ctsu_instance_ctrl_t *) p_ctrl; +#if (BSP_FEATURE_CTSU_VERSION == 1) + uint8_t temp; +#endif +#if (CTSU_CFG_PARAM_CHECKING_ENABLE == 1) + FSP_ASSERT(p_instance_ctrl); + FSP_ERROR_RETURN(CTSU_OPEN == p_instance_ctrl->open, FSP_ERR_NOT_OPEN); +#endif + if (CTSU_CAP_SOFTWARE == p_instance_ctrl->cap) + { +#if (BSP_FEATURE_CTSU_VERSION == 2) + /* Can be checked if the previous measurement was a software trigger */ + if (0 == (0x02 & CTSU.CTSUCRA.LONG)) + { + FSP_ERROR_RETURN(0x01 != (CTSU.CTSUCRA.LONG & 0x01), FSP_ERR_CTSU_SCANNING); + } +#endif +#if (BSP_FEATURE_CTSU_VERSION == 1) + /* Can be checked if the previous measurement was a software trigger */ + if (0 == (0x02 & CTSU.CTSUCR0.BYTE)) + { + FSP_ERROR_RETURN(0x01 != (CTSU.CTSUCR0.BYTE & 0x01), FSP_ERR_CTSU_SCANNING); + } +#endif + } + + FSP_ERROR_RETURN(CTSU_STATE_SCANNED != p_instance_ctrl->state, FSP_ERR_CTSU_NOT_GET_DATA); + + gp_ctsu_isr_context = p_instance_ctrl; + +#if (BSP_FEATURE_CTSU_VERSION == 2) + #if (CTSU_CFG_AUTO_CORRECTION_ENABLE == 1) + + ctsu_auto_correction_register_set(p_instance_ctrl); + + /* Sensor counter auto correction enabled */ + CTSU.CTSUOPT.BIT.CCOCFEN = 1; + #endif +#endif + +#if (CTSU_CFG_AUTO_MULTI_CLOCK_CORRECTION_ENABLE == 1) + if (((1 == p_instance_ctrl->p_ctsu_cfg->ajfen) && (1 == p_instance_ctrl->p_ctsu_cfg->majirimd)) || + ((0 == p_instance_ctrl->p_ctsu_cfg->ajfen) && (0 == p_instance_ctrl->p_ctsu_cfg->majority_mode))) + { + if (0 == p_instance_ctrl->mcact_flag) + { + if (CTSU_TUNING_COMPLETE == p_instance_ctrl->tuning) + { + ctsu_multiclock_auto_correction_register_set(p_instance_ctrl); + p_instance_ctrl->mcact_flag = 1; + CTSU.CTSUOPT.BIT.MCACFEN = 1; + CTSU.CTSUOPT.BIT.MAJIRIMD = p_instance_ctrl->p_ctsu_cfg->majirimd; + } + else + { + CTSU.CTSUOPT.BIT.MAJIRIMD = 0; + CTSU.CTSUOPT.BIT.MCACFEN = 0; + } + } + else + { + CTSU.CTSUOPT.BIT.MCACFEN = 1; + CTSU.CTSUOPT.BIT.MAJIRIMD = p_instance_ctrl->p_ctsu_cfg->majirimd; + } + } + else + { + CTSU.CTSUOPT.BIT.MAJIRIMD = 0; + CTSU.CTSUOPT.BIT.MCACFEN = 0; + } +#endif + +#if (CTSU_CFG_DTC_SUPPORT_ENABLE == 1) + err = ctsu_transfer_configure(p_instance_ctrl); + FSP_ERROR_RETURN(FSP_SUCCESS == err, err); +#endif + + +#if (BSP_FEATURE_CTSU_VERSION == 2) + + if (CTSU_CAP_SOFTWARE == p_instance_ctrl->cap) + { + /* specify Software trigger usage */ + CTSU.CTSUCRA.BIT.CAP = 0; + CTSU.CTSUCRA.BIT.SNZ = 0; + } + else + { + /* specify external trigger usage and enable power saving */ + CTSU.CTSUCRA.BIT.CAP = 1; + CTSU.CTSUCRA.BIT.SNZ = 1; + CTSU.CTSUCRA.BIT.STRT = 0; // To write CTSUCR1 + } + + /* Write CTSU Control Register 1 and save mode */ + + if ((CTSU_MODE_CORRECTION_SCAN != p_instance_ctrl->md) && + (CTSU_MODE_DIAGNOSIS_SCAN != p_instance_ctrl->md)) + { + CTSU.CTSUCRA.BIT.TXVSEL = (p_instance_ctrl->p_ctsu_cfg->txvsel << 1 ) | p_instance_ctrl->p_ctsu_cfg->txvsel2; + + CTSU.CTSUCRA.BIT.ATUNE1 = ((p_instance_ctrl->ctsucr1 >> 3) & 0x01); + CTSU.CTSUCRA.BIT.MD0 = ((p_instance_ctrl->ctsucr1 >> 6) & 0x01); + CTSU.CTSUCRA.BIT.MD1 = ((p_instance_ctrl->ctsucr1 >> 7) & 0x01); + CTSU.CTSUCRA.BIT.ATUNE2 = ((p_instance_ctrl->ctsucr2 >> 1) & 0x01); + CTSU.CTSUCRA.BIT.POSEL = ((p_instance_ctrl->ctsucr2 >> 4) & 0x03); + if (CTSU_MODE_CURRENT_SCAN == p_instance_ctrl->md) + { + CTSU.CTSUCRB.BIT.SST = CTSU_SST_RECOMMEND_CURRENT; + CTSU.CTSUCRA.BIT.DCMODE = 1; + CTSU.CTSUCRA.BIT.DCBACK = 1; + CTSU.CTSUMCH.BIT.MCA0 = 1; + CTSU.CTSUMCH.BIT.MCA1 = 0; + CTSU.CTSUMCH.BIT.MCA2 = 0; + CTSU.CTSUMCH.BIT.MCA3 = 0; + } + else + { + CTSU.CTSUCRB.BIT.SST = CTSU_SST_RECOMMEND; + CTSU.CTSUCRA.BIT.DCMODE = 0; + CTSU.CTSUCRA.BIT.DCBACK = 0; + CTSU.CTSUMCH.BIT.MCA0 = 1; + #if (CTSU_CFG_NUM_SUMULTI >= 2) + CTSU.CTSUMCH.BIT.MCA1 = 1; + #endif + #if (CTSU_CFG_NUM_SUMULTI >= 3) + CTSU.CTSUMCH.BIT.MCA2 = 1; + #endif + } + + /* Write Channel setting */ + CTSU.CTSUCHACA.LONG = (uint32_t)((p_instance_ctrl->ctsuchac3 << 24) | + (p_instance_ctrl->ctsuchac2 << 16) | + (p_instance_ctrl->ctsuchac1 << 8 ) | + (p_instance_ctrl->ctsuchac0)); + CTSU.CTSUCHACB.LONG = (uint32_t)(p_instance_ctrl->ctsuchac4); + + CTSU.CTSUCHTRCA.LONG = (uint32_t)((p_instance_ctrl->ctsuchtrc3 << 24) | + (p_instance_ctrl->ctsuchtrc2 << 16) | + (p_instance_ctrl->ctsuchtrc1 << 8 ) | + (p_instance_ctrl->ctsuchtrc0)); + CTSU.CTSUCHTRCB.LONG = (uint32_t)(p_instance_ctrl->ctsuchtrc4); + + } + + #if (CTSU_CFG_DIAG_SUPPORT_ENABLE == 1) + if (CTSU_MODE_DIAGNOSIS_SCAN == p_instance_ctrl->md) + { + err = (fsp_err_t)ctsu_diag_scan_start2(p_instance_ctrl); + } + #endif + #if (CTSU_CFG_TEMP_CORRECTION_SUPPORT == 1) + if (CTSU_MODE_CORRECTION_SCAN == p_instance_ctrl->md) + { + CTSU.CTSUCRB.BIT.SST = CTSU_SST_RECOMMEND; + ctsu_correction_scan_start(); + #if (CTSU_CFG_AUTO_CORRECTION_ENABLE == 1) + /* Disable automatic correction during temperature correction */ + CTSU.CTSUOPT.BIT.CCOCFEN = 0; + #endif + } + #endif + + #if (CTSU_CFG_AUTO_JUDGE_ENABLE == 1) + if (CTSU_TUNING_COMPLETE == p_instance_ctrl->tuning) + { + /* Enable / disable switching of automatic judgement */ + CTSU.CTSUOPT.BIT.MTUCFEN = p_instance_ctrl->p_ctsu_cfg->mtucfen; + CTSU.CTSUOPT.BIT.AJFEN = p_instance_ctrl->p_ctsu_cfg->ajfen; + + if (1 == p_instance_ctrl->p_ctsu_cfg->ajfen) + { + /* Setting the number of moving averages and the number of baseline averages for automatic judgement */ + CTSU.CTSUAJCR.BIT.AJMMAT = p_instance_ctrl->ajmmat; + CTSU.CTSUAJCR.BIT.AJBMAT = p_instance_ctrl->ajbmat; + CTSU.CTSUAJCR.BIT.JC = p_instance_ctrl->p_ctsu_cfg->jc; + CTSU.CTSUAJRR.BIT.TJR0 = 0; + CTSU.CTSUAJRR.BIT.TJR1 = 0; + CTSU.CTSUAJRR.BIT.TJR2 = 0; + } + } + else + { + /* If tuning incomplete, auto judgement register set OFF */ + CTSU.CTSUOPT.BIT.MTUCFEN = 0; + CTSU.CTSUOPT.BIT.AJFEN = 0; + } + + if (1 == p_instance_ctrl->blini_flag) + { + /* Set blini to 1 at the first judgement of automatic judgement */ + CTSU.CTSUAJCR.BIT.BLINI = 1; + } + #endif + + #if (CTSU_CFG_MULTIPLE_ELECTRODE_CONNECTION_ENABLE == 1) + /* Parameter setting for multiple electrode connection */ + CTSU.CTSUCALIB.BIT.TSOD = p_instance_ctrl->tsod; + CTSU.CTSUCALIB.BIT.IOCSEL = 0; + if (1 == p_instance_ctrl->tsod) + { + /* When using MEC, MD0 bit is set to single scan mode. */ + CTSU.CTSUCRA.BIT.MD0 = 0; + CTSU.CTSUMCH.BIT.MCH0 = p_instance_ctrl->mec_ts; + + /* Set MCH1 when using both MEC and Active Shield.*/ + if((0 != p_instance_ctrl->ctsuchtrc0) + || (0 != p_instance_ctrl->ctsuchtrc1) + || (0 != p_instance_ctrl->ctsuchtrc2) + || (0 != p_instance_ctrl->ctsuchtrc3) + || (0 != p_instance_ctrl->ctsuchtrc4)) + { + CTSU.CTSUMCH.BIT.MCH1 = p_instance_ctrl->mec_shield_ts; + } + } + #endif + + p_instance_ctrl->state = CTSU_STATE_SCANNING; + + /* Set CTSU_STRT bit to start scan */ + CTSU.CTSUCRA.BIT.STRT = 1; +#endif +#if (BSP_FEATURE_CTSU_VERSION == 1) + + if (CTSU_CAP_SOFTWARE == p_instance_ctrl->cap) + { + /* specify Software trigger usage */ + CTSU.CTSUCR0.BIT.CTSUCAP = 0; + CTSU.CTSUCR0.BIT.CTSUSNZ = 0; + } + else + { + /* specify external trigger usage and enable power saving */ + CTSU.CTSUCR0.BIT.CTSUCAP = 1; + CTSU.CTSUCR0.BIT.CTSUSNZ = 1; + CTSU.CTSUCR0.BIT.CTSUSTRT = 0; // To write CTSUCR1 + } + + + if (CTSU_MODE_DIAGNOSIS_SCAN != p_instance_ctrl->md) + { + temp = (uint8_t) (CTSU.CTSUCR1.BYTE & ~(CTSU_CR1_MODIFY_BIT)); + CTSU.CTSUCR1.BYTE = (uint8_t) (temp | (p_instance_ctrl->ctsucr1 & CTSU_CR1_MODIFY_BIT)); // MD1, MD0, ATUNE1 + + #if BSP_FEATURE_CTSU_HAS_TXVSEL + CTSU.CTSUCR0.BIT.CTSUTXVSEL = p_instance_ctrl->p_ctsu_cfg->txvsel; + #endif + + /* Write Channel setting */ + CTSU.CTSUCHAC0.BYTE = p_instance_ctrl->ctsuchac0; + CTSU.CTSUCHAC1.BYTE = p_instance_ctrl->ctsuchac1; + #if (BSP_FEATURE_CTSU_CTSUCHAC_REGISTER_COUNT >= 3) + CTSU.CTSUCHAC2.BYTE = p_instance_ctrl->ctsuchac2; + #endif + #if (BSP_FEATURE_CTSU_CTSUCHAC_REGISTER_COUNT >= 4) + CTSU.CTSUCHAC3.BYTE = p_instance_ctrl->ctsuchac3; + #endif + #if (BSP_FEATURE_CTSU_CTSUCHAC_REGISTER_COUNT >= 5) + CTSU.CTSUCHAC4.BYTE = p_instance_ctrl->ctsuchac4; + #endif + + #if (CTSU_CFG_NUM_MUTUAL_ELEMENTS != 0) + CTSU.CTSUCHTRC0.BYTE = p_instance_ctrl->ctsuchtrc0; + CTSU.CTSUCHTRC1.BYTE = p_instance_ctrl->ctsuchtrc1; + #if (BSP_FEATURE_CTSU_CTSUCHTRC_REGISTER_COUNT >= 3) + CTSU.CTSUCHTRC2.BYTE = p_instance_ctrl->ctsuchtrc2; + #endif + #if (BSP_FEATURE_CTSU_CTSUCHTRC_REGISTER_COUNT >= 4) + CTSU.CTSUCHTRC3.BYTE = p_instance_ctrl->ctsuchtrc3; + #endif + #if (BSP_FEATURE_CTSU_CTSUCHTRC_REGISTER_COUNT >= 5) + CTSU.CTSUCHTRC4.BYTE = p_instance_ctrl->ctsuchtrc4; + #endif + #endif + } + + #if (CTSU_CFG_DIAG_SUPPORT_ENABLE == 1) + if (CTSU_MODE_DIAGNOSIS_SCAN == p_instance_ctrl->md) + { + ctsu_diag_scan_start1(p_instance_ctrl); + } + #endif + + p_instance_ctrl->state = CTSU_STATE_SCANNING; + + /* Set CTSU_STRT bit to start scan */ + CTSU.CTSUCR0.BIT.CTSUSTRT = 1; +#endif + + + + return err; +} + +/*******************************************************************************************************************//** + * @brief 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. + * Implements @ref ctsu_api_t::dataGet. + * + * Example: + * @snippet r_ctsu_example.c R_CTSU_DataGet + * + * @retval FSP_SUCCESS CTSU successfully configured. + * @retval FSP_ERR_ASSERTION Null pointer passed as a parameter. + * @retval FSP_ERR_NOT_OPEN Module is not open. + * @retval FSP_ERR_CTSU_SCANNING Scanning this instance. + * @retval FSP_ERR_CTSU_INCOMPLETE_TUNING Incomplete initial offset tuning. + * @retval FSP_ERR_CTSU_DIAG_NOT_YET Diagnosis of data collected no yet. + * @retval FSP_ERR_INVALID_MODE The mode of automatic judgement on is invalid. + * @retval FSP_ERR_ABORTED Operate error of Diagnosis ADC data collection ,since ADC use other + **********************************************************************************************************************/ +fsp_err_t R_CTSU_DataGet (ctsu_ctrl_t * const p_ctrl, uint16_t * p_data) +{ + fsp_err_t err = FSP_SUCCESS; + ctsu_instance_ctrl_t * p_instance_ctrl = (ctsu_instance_ctrl_t *) p_ctrl; + uint16_t element_id; + uint16_t majority_mode_id; + +#if (CTSU_CFG_PARAM_CHECKING_ENABLE == 1) + FSP_ASSERT(p_instance_ctrl); + FSP_ASSERT(p_data); + FSP_ERROR_RETURN(CTSU_OPEN == p_instance_ctrl->open, FSP_ERR_NOT_OPEN); +#endif + FSP_ERROR_RETURN(CTSU_STATE_SCANNING != p_instance_ctrl->state, FSP_ERR_CTSU_SCANNING); + FSP_ERROR_RETURN(0 == p_instance_ctrl->p_ctsu_cfg->ajfen, FSP_ERR_INVALID_MODE); + +#if (BSP_FEATURE_CTSU_VERSION == 2) + #if (CTSU_CFG_TEMP_CORRECTION_SUPPORT == 1) + if (CTSU_MODE_CORRECTION_SCAN == p_instance_ctrl->md) + { + err = ctsu_correction_data_get(p_instance_ctrl, p_data); + #if (CTSU_CFG_AUTO_CORRECTION_ENABLE == 1) + + /* Enable automatic correction after temperature correction is completed */ + CTSU.CTSUOPT.BIT.CCOCFEN = 1; + #endif + p_instance_ctrl->state = CTSU_STATE_IDLE; + + return err; + } + #endif + + #if (CTSU_CFG_DIAG_SUPPORT_ENABLE == 1) + if (CTSU_MODE_DIAGNOSIS_SCAN == p_instance_ctrl->md) + { + err = ctsu_diag_data_get2(p_data); + p_instance_ctrl->state = CTSU_STATE_IDLE; + if (FSP_ERR_CTSU_DIAG_NOT_YET == err) + { + err = FSP_ERR_CTSU_DIAG_NOT_YET; + } + else if (FSP_ERR_ABORTED == err) + { + err = FSP_ERR_ABORTED; + } + else + { + err = FSP_SUCCESS; + } + + return err; + } + #endif +#endif + +#if (BSP_FEATURE_CTSU_VERSION == 1) + #if (CTSU_CFG_DIAG_SUPPORT_ENABLE == 1) + if (CTSU_MODE_DIAGNOSIS_SCAN == p_instance_ctrl->md) + { + err = ctsu_diag_data_get1(); + p_instance_ctrl->state = CTSU_STATE_IDLE; + if (FSP_ERR_CTSU_DIAG_NOT_YET == err) + { + err = FSP_ERR_CTSU_DIAG_NOT_YET; + } + else + { + err = FSP_SUCCESS; + } + + return err; + } + #endif +#endif + + if (CTSU_STATE_SCANNED == p_instance_ctrl->state) + { + if (CTSU_TUNING_COMPLETE == p_instance_ctrl->tuning) + { + if (p_instance_ctrl->average == p_instance_ctrl->num_moving_average) + { + /* Do nothing */ + } + else if (p_instance_ctrl->average < p_instance_ctrl->num_moving_average) + { + (p_instance_ctrl->average)++; + } + else + { + p_instance_ctrl->average = p_instance_ctrl->num_moving_average; + } + } + + ctsu_correction_exec(p_instance_ctrl); + + if (CTSU_TUNING_INCOMPLETE == p_instance_ctrl->tuning) + { + if ((CTSU_MODE_SELF_MULTI_SCAN == p_instance_ctrl->md) || + (CTSU_MODE_MUTUAL_FULL_SCAN == p_instance_ctrl->md)) + { + ctsu_initial_offset_tuning(p_instance_ctrl); + } + } + + p_instance_ctrl->state = CTSU_STATE_IDLE; + } + + FSP_ERROR_RETURN(0 < p_instance_ctrl->average, FSP_ERR_CTSU_INCOMPLETE_TUNING); + +#if (CTSU_CFG_NUM_SELF_ELEMENTS != 0) + if (CTSU_MODE_SELF_MULTI_SCAN == (CTSU_MODE_MUTUAL_FULL_SCAN & p_instance_ctrl->md)) + { + for (element_id = 0; element_id < p_instance_ctrl->num_elements; element_id++) + { + for (majority_mode_id = 0; majority_mode_id < CTSU_MAJORITY_MODE_ELEMENTS; majority_mode_id++) + { + *p_data = + (p_instance_ctrl->p_self_data + element_id * CTSU_MAJORITY_MODE_ELEMENTS + + majority_mode_id)->int_data; + p_data++; + } + } + } +#endif +#if (CTSU_CFG_NUM_MUTUAL_ELEMENTS != 0) + if (CTSU_MODE_MUTUAL_FULL_SCAN == (CTSU_MODE_MUTUAL_FULL_SCAN & p_instance_ctrl->md)) + { + if (true == p_instance_ctrl->serial_tuning_enable) + { + /* Serial Tuning Phase1 */ + if ((0 == p_instance_ctrl->ctsuchtrc0) && (0 == p_instance_ctrl->ctsuchtrc1) && + (0 == p_instance_ctrl->ctsuchtrc2) && (0 == p_instance_ctrl->ctsuchtrc3) && + (0 == p_instance_ctrl->ctsuchtrc4)) + { + return FSP_SUCCESS; + } + } + + for (element_id = 0; element_id < p_instance_ctrl->num_elements; element_id++) + { + for (majority_mode_id = 0; majority_mode_id < CTSU_MAJORITY_MODE_ELEMENTS; majority_mode_id++) + { + *p_data = + (p_instance_ctrl->p_mutual_pri_data + element_id * CTSU_MAJORITY_MODE_ELEMENTS + + majority_mode_id)->int_data; + p_data++; + *p_data = + (p_instance_ctrl->p_mutual_snd_data + element_id * CTSU_MAJORITY_MODE_ELEMENTS + + majority_mode_id)->int_data; + p_data++; + } + } + } +#endif + + return err; +} + +/*******************************************************************************************************************//** + * @brief 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 @ref ctsu_api_t::autoJudgementDataGet. + * + * Example: + * @snippet r_ctsu_example.c R_CTSU_AutoJudgementDataGet + * + * @retval FSP_SUCCESS CTSU successfully configured. + * @retval FSP_ERR_ASSERTION Null pointer passed as a parameter. + * @retval FSP_ERR_NOT_OPEN Module is not open. + * @retval FSP_ERR_CTSU_SCANNING Scanning this instance. + * @retval FSP_ERR_INVALID_MODE The mode of automatic judgement off is invalid. + **********************************************************************************************************************/ +fsp_err_t R_CTSU_AutoJudgementDataGet (ctsu_ctrl_t * const p_ctrl, uint64_t * p_button_status) +{ + fsp_err_t err = FSP_SUCCESS; + ctsu_instance_ctrl_t * p_instance_ctrl = (ctsu_instance_ctrl_t *) p_ctrl; +#if (BSP_FEATURE_CTSU_VERSION == 2) + #if (CTSU_CFG_AUTO_JUDGE_ENABLE == 1) + uint16_t element_id; + #endif +#endif + +#if (CTSU_CFG_PARAM_CHECKING_ENABLE == 1) + FSP_ASSERT(p_instance_ctrl); + FSP_ASSERT(p_button_status); + FSP_ERROR_RETURN(CTSU_OPEN == p_instance_ctrl->open, FSP_ERR_NOT_OPEN); +#endif + FSP_ERROR_RETURN(CTSU_STATE_SCANNING != p_instance_ctrl->state, FSP_ERR_CTSU_SCANNING); + FSP_ERROR_RETURN(1 == p_instance_ctrl->p_ctsu_cfg->ajfen, FSP_ERR_INVALID_MODE); + +#if ((BSP_FEATURE_CTSU_VERSION == 2) && (CTSU_CFG_AUTO_JUDGE_ENABLE == 1)) + if (1 == p_instance_ctrl->blini_flag) + { + p_instance_ctrl->blini_flag = 0; + + /* After the initial judgement of automatic judgement, set the baseline initialization bit to 0. */ + CTSU.CTSUAJCR.BIT.BLINI = 0; + + ctsu_auto_judge_threshold_calc(p_instance_ctrl); + } + + if (CTSU_STATE_SCANNED == p_instance_ctrl->state) + { + for (element_id = 0; element_id < p_instance_ctrl->num_elements; element_id++) + { + #if (CTSU_CFG_AUTO_MULTI_CLOCK_CORRECTION_ENABLE == 1) + if (1 == p_instance_ctrl->p_ctsu_cfg->majirimd) + { + /* Reflect the final result bit of automatic judgement in buttun_status */ + if (1 == (((p_instance_ctrl->p_auto_judge[element_id].ajrr) >> CTSU_AUTO_FINAL_JUDGE_BIT) & 0x1)) + { + *p_button_status |= ((uint64_t) 1 << element_id); + } + else + { + *p_button_status &= ~((uint64_t) 1 << element_id); + } + } + else + #endif + { + /* Reflect the final result bit of automatic judgement in buttun_status */ + /* Since the final result of the automatic judgement is output to the FJR bit of the final multi-clock, */ + /* the result of the final multi-clock is taken out. */ + if (1 == (((p_instance_ctrl->p_auto_judge[(element_id * CTSU_MAJORITY_MODE_ELEMENTS) + (CTSU_MAJORITY_MODE_ELEMENTS - 1)].ajrr) >> CTSU_AUTO_FINAL_JUDGE_BIT) & 0x1)) + { + *p_button_status |= ((uint64_t) 1 << element_id); + } + else + { + *p_button_status &= ~((uint64_t) 1 << element_id); + } + } + } + + p_instance_ctrl->state = CTSU_STATE_IDLE; + } +#endif + + return err; +} + +/*******************************************************************************************************************//** + * @brief 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 @ref ctsu_api_t::offsetTuning. + * + * Example: + * @snippet r_ctsu_example.c R_CTSU_OffsetTuning + * + * @retval FSP_SUCCESS CTSU successfully configured. + * @retval FSP_ERR_ASSERTION Null pointer passed as a parameter. + * @retval FSP_ERR_NOT_OPEN Module is not open. + * @retval FSP_ERR_CTSU_SCANNING Scanning this instance. + * @retval FSP_ERR_CTSU_INCOMPLETE_TUNING Incomplete initial offset tuning. + **********************************************************************************************************************/ +fsp_err_t R_CTSU_OffsetTuning (ctsu_ctrl_t * const p_ctrl) +{ + fsp_err_t err = FSP_SUCCESS; + ctsu_instance_ctrl_t * p_instance_ctrl = (ctsu_instance_ctrl_t *) p_ctrl; + uint16_t element_id; +#if (BSP_FEATURE_CTSU_VERSION == 2) + #if (CTSU_CFG_AUTO_JUDGE_ENABLE == 1) + uint16_t i; + #endif +#endif + +#if (CTSU_CFG_PARAM_CHECKING_ENABLE == 1) + FSP_ASSERT(p_instance_ctrl); + FSP_ERROR_RETURN(CTSU_OPEN == p_instance_ctrl->open, FSP_ERR_NOT_OPEN); +#endif + FSP_ERROR_RETURN(CTSU_STATE_SCANNING != p_instance_ctrl->state, FSP_ERR_CTSU_SCANNING); + + if (CTSU_TUNING_COMPLETE == p_instance_ctrl->tuning) + { + for (element_id = 0; element_id < p_instance_ctrl->num_elements; element_id++) + { + /* Counter clear for re-offset tuning */ + *(p_instance_ctrl->p_element_complete_flag + element_id) = 0; +#if (BSP_FEATURE_CTSU_VERSION == 2) + *(p_instance_ctrl->p_frequency_complete_flag + element_id) = 0; +#endif + *(p_instance_ctrl->p_tuning_diff + element_id) = 0; + } + } + + p_instance_ctrl->tuning = CTSU_TUNING_INCOMPLETE; + + if (CTSU_STATE_SCANNED == p_instance_ctrl->state) + { + if (0 == p_instance_ctrl->p_ctsu_cfg->ajfen) + { + ctsu_correction_exec(p_instance_ctrl); + } + +#if (BSP_FEATURE_CTSU_VERSION == 2) + #if (CTSU_CFG_AUTO_JUDGE_ENABLE == 1) + else + { + CTSU.CTSUOPT.BIT.AJFEN = 0; + CTSU.CTSUOPT.BIT.MTUCFEN = 0; + + for (element_id = 0; element_id < p_instance_ctrl->num_elements; element_id++) + { + for (i = 0; i < CTSU_CFG_NUM_SUMULTI; i++) + { + if (CTSU_MODE_SELF_MULTI_SCAN == p_instance_ctrl->md) + { + p_instance_ctrl->p_self_corr[(element_id * CTSU_CFG_NUM_SUMULTI) + + i] = + p_instance_ctrl->p_self_raw[(element_id * CTSU_CFG_NUM_SUMULTI) + i]; + } + + #if (CTSU_CFG_NUM_MUTUAL_ELEMENTS != 0) + else + { + p_instance_ctrl->p_mutual_pri_corr[(element_id * CTSU_CFG_NUM_SUMULTI) + + i] = + p_instance_ctrl->p_mutual_raw[(element_id * CTSU_MUTUAL_BUF_SIZE) + (i * 2)]; + } + #endif + #if (CTSU_CFG_MAJORITY_MODE & CTSU_JUDGEMENT_MAJORITY_MODE) + + /* Initial value setting of DTC transfer information for automatic judgement */ + p_instance_ctrl->p_auto_judge[(element_id * CTSU_MAJORITY_MODE_ELEMENTS) + i].ajthr = 0; + p_instance_ctrl->p_auto_judge[(element_id * CTSU_MAJORITY_MODE_ELEMENTS) + i].ajmmar = 0; + p_instance_ctrl->p_auto_judge[(element_id * CTSU_MAJORITY_MODE_ELEMENTS) + i].ajblact = 0; + p_instance_ctrl->p_auto_judge[(element_id * CTSU_MAJORITY_MODE_ELEMENTS) + i].ajblar = 0; + p_instance_ctrl->p_auto_judge[(element_id * CTSU_MAJORITY_MODE_ELEMENTS) + i].ajrr = 0; + #endif + } + + #if ((CTSU_CFG_MAJORITY_MODE & CTSU_JUDGEMENT_MAJORITY_MODE) == 0) + p_instance_ctrl->p_auto_judge[element_id].ajthr = 0; + p_instance_ctrl->p_auto_judge[element_id].ajmmar = 0; + p_instance_ctrl->p_auto_judge[element_id].ajblact = 0; + p_instance_ctrl->p_auto_judge[element_id].ajblar = 0; + p_instance_ctrl->p_auto_judge[element_id].ajrr = 0; + #endif + } + } + #endif +#endif + + if (CTSU_TUNING_INCOMPLETE == p_instance_ctrl->tuning) + { + if ((CTSU_MODE_SELF_MULTI_SCAN == p_instance_ctrl->md) || + (CTSU_MODE_MUTUAL_FULL_SCAN == p_instance_ctrl->md)) + { + ctsu_initial_offset_tuning(p_instance_ctrl); + } + } + +#if (BSP_FEATURE_CTSU_VERSION == 2) + #if (CTSU_CFG_AUTO_JUDGE_ENABLE == 1) + if (1 == p_instance_ctrl->p_ctsu_cfg->ajfen) + { + if (CTSU_TUNING_COMPLETE == p_instance_ctrl->tuning) + { + /* Initialization of baseline mean for automatic judgement */ + p_instance_ctrl->blini_flag = 1; + } + } + #endif + #if (CTSU_CFG_AUTO_MULTI_CLOCK_CORRECTION_ENABLE == 1) + p_instance_ctrl->mcact_flag = 0; + #endif +#endif + p_instance_ctrl->state = CTSU_STATE_IDLE; + } + + FSP_ERROR_RETURN(CTSU_TUNING_COMPLETE == p_instance_ctrl->tuning, FSP_ERR_CTSU_INCOMPLETE_TUNING); + + return err; +} + +/*******************************************************************************************************************//** + * @brief This function scan stops the sensor as scanning by the CTSU. + * Implements @ref ctsu_api_t::scanStop. + * @retval FSP_SUCCESS CTSU successfully scan stop. + * @retval FSP_ERR_ASSERTION Null pointer passed as a parameter. + * @retval FSP_ERR_NOT_OPEN Module is not open. + **********************************************************************************************************************/ +fsp_err_t R_CTSU_ScanStop (ctsu_ctrl_t * const p_ctrl) +{ + fsp_err_t err = FSP_SUCCESS; + ctsu_instance_ctrl_t * p_instance_ctrl = (ctsu_instance_ctrl_t *) p_ctrl; +#if (CTSU_CFG_PARAM_CHECKING_ENABLE) + FSP_ASSERT(p_ctrl); + FSP_ERROR_RETURN(CTSU_OPEN == p_instance_ctrl->open, FSP_ERR_NOT_OPEN); +#endif + + if (CTSU_STATE_SCANNING == p_instance_ctrl->state) + { +#if (BSP_FEATURE_CTSU_VERSION == 2) + CTSU.CTSUCRA.LONG ^= 0x11; +#else + CTSU.CTSUCR0.BYTE ^= 0x11; +#endif + + IR(CTSU,CTSUWR)= 0; + IR(CTSU,CTSURD)= 0; + IR(CTSU,CTSUFN)= 0; + + p_instance_ctrl->state = CTSU_STATE_IDLE; + p_instance_ctrl->wr_index = 0; + p_instance_ctrl->rd_index = 0; + } + + return err; +} + +/*******************************************************************************************************************//** + * Updates the user callback and has option of providing memory for callback structure. + * Implements ctsu_api_t::callbackSet + * + * @retval FSP_SUCCESS Callback updated successfully. + * @retval FSP_ERR_ASSERTION A required pointer is NULL. + * @retval FSP_ERR_NOT_OPEN The control block has not been opened. + * @retval FSP_ERR_NO_CALLBACK_MEMORY p_callback is non-secure and p_callback_memory is either secure or NULL. + **********************************************************************************************************************/ +fsp_err_t R_CTSU_CallbackSet (ctsu_ctrl_t * const p_api_ctrl, + void ( * p_callback)(ctsu_callback_args_t *), + void const * const p_context, + ctsu_callback_args_t * const p_callback_memory) +{ + ctsu_instance_ctrl_t * p_ctrl = (ctsu_instance_ctrl_t *) p_api_ctrl; + +#if (CTSU_CFG_PARAM_CHECKING_ENABLE) + FSP_ASSERT(p_ctrl); + FSP_ASSERT(p_callback); + FSP_ERROR_RETURN(CTSU_OPEN == p_ctrl->open, FSP_ERR_NOT_OPEN); +#endif + +#if BSP_TZ_SECURE_BUILD + + /* Get security state of p_callback */ + bool callback_is_secure = + (NULL == cmse_check_address_range((void *) p_callback, sizeof(void *), CMSE_AU_NONSECURE)); + + #if CTSU_CFG_PARAM_CHECKING_ENABLE + + /* In secure projects, p_callback_memory must be provided in non-secure space if p_callback is non-secure */ + ctsu_callback_args_t * const p_callback_memory_checked = cmse_check_pointed_object(p_callback_memory, + CMSE_AU_NONSECURE); + FSP_ERROR_RETURN(callback_is_secure || (NULL != p_callback_memory_checked), FSP_ERR_NO_CALLBACK_MEMORY); + #endif +#endif + + /* Store callback and context */ +#if BSP_TZ_SECURE_BUILD + p_ctrl->p_callback = callback_is_secure ? p_callback : + (void (*)(ctsu_callback_args_t *))cmse_nsfptr_create(p_callback); +#else + p_ctrl->p_callback = p_callback; +#endif + p_ctrl->p_context = p_context; + p_ctrl->p_callback_memory = p_callback_memory; + + return FSP_SUCCESS; +} + +/*******************************************************************************************************************//** + * @brief Disables specified CTSU control block. Implements @ref ctsu_api_t::close. + * + * @retval FSP_SUCCESS CTSU successfully configured. + * @retval FSP_ERR_ASSERTION Null pointer passed as a parameter. + * @retval FSP_ERR_NOT_OPEN Module is not open. + **********************************************************************************************************************/ +fsp_err_t R_CTSU_Close (ctsu_ctrl_t * const p_ctrl) +{ + fsp_err_t err = FSP_SUCCESS; + ctsu_instance_ctrl_t * p_instance_ctrl = (ctsu_instance_ctrl_t *) p_ctrl; + + FSP_ASSERT(p_instance_ctrl); + FSP_ERROR_RETURN(CTSU_OPEN == p_instance_ctrl->open, FSP_ERR_NOT_OPEN); + + g_ctsu_element_index = (uint8_t) (g_ctsu_element_index - p_instance_ctrl->num_elements); +#if (CTSU_CFG_NUM_SELF_ELEMENTS != 0) + if (CTSU_MODE_SELF_MULTI_SCAN == (CTSU_MODE_MUTUAL_FULL_SCAN & p_instance_ctrl->md)) + { + g_ctsu_self_element_index = (uint8_t) (g_ctsu_self_element_index - p_instance_ctrl->num_elements); + } +#endif +#if (CTSU_CFG_NUM_MUTUAL_ELEMENTS != 0) + if (CTSU_MODE_MUTUAL_FULL_SCAN == (CTSU_MODE_MUTUAL_FULL_SCAN & p_instance_ctrl->md)) + { + g_ctsu_mutual_element_index = (uint8_t) (g_ctsu_mutual_element_index - p_instance_ctrl->num_elements); + } +#endif + if (0 == g_ctsu_element_index) + { +#if (CTSU_CFG_DTC_SUPPORT_ENABLE == 1) + err = ctsu_transfer_close(p_instance_ctrl); +#endif +#if (BSP_FEATURE_CTSU_VERSION == 2) + CTSU.CTSUCRA.BIT.STRT = 0; +#else + CTSU.CTSUCR0.BIT.CTSUSTRT = 0; +#endif + + /* Get CTSU out of stop state (supply power/clock) */ + R_BSP_InterruptRequestDisable(VECT(CTSU,CTSUWR)); + R_BSP_InterruptRequestDisable(VECT(CTSU,CTSURD)); + R_BSP_InterruptRequestDisable(VECT(CTSU,CTSUFN)); + + R_BSP_RegisterProtectDisable (BSP_REG_PROTECT_LPC_CGC_SWR); + MSTP(CTSU) = 1; + R_BSP_RegisterProtectEnable (BSP_REG_PROTECT_LPC_CGC_SWR); + } + + p_instance_ctrl->state = CTSU_STATE_INIT; + p_instance_ctrl->open = false; + + return err; +} + +/*******************************************************************************************************************//** + * @brief 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. + * + * 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 Only) + * The bitmap is shown as follows. + * + * | 2bit | 1bit | 0bit | + * |---------------------|---------------------|---------------------| + * | 3rd frequency value | 2nd frequency value | 1st frequency value | + * + * Implements @ref ctsu_api_t::specificDataGet. + * + * Example: + * @snippet r_ctsu_example.c R_CTSU_SpecificDataGet + * + * + * @retval FSP_SUCCESS CTSU successfully configured. + * @retval FSP_ERR_ASSERTION Null pointer passed as a parameter. + * @retval FSP_ERR_NOT_OPEN Module is not open. + * @retval FSP_ERR_CTSU_SCANNING Scanning this instance. + * @retval FSP_ERR_CTSU_INCOMPLETE_TUNING Incomplete initial offset tuning. + * @retval FSP_ERR_NOT_ENABLED CTSU_SPECIFIC_SELECTED_FREQ is not enabled in CTSU1.(CTSU2 Only) + **********************************************************************************************************************/ +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) +{ + fsp_err_t err = FSP_SUCCESS; + uint16_t element_id; + uint16_t i; + ctsu_instance_ctrl_t * p_instance_ctrl = (ctsu_instance_ctrl_t *) p_ctrl; + +#if (CTSU_CFG_PARAM_CHECKING_ENABLE == 1) + FSP_ASSERT(p_instance_ctrl); + FSP_ASSERT(p_specific_data); + FSP_ERROR_RETURN(CTSU_OPEN == p_instance_ctrl->open, FSP_ERR_NOT_OPEN); +#endif + FSP_ERROR_RETURN(CTSU_STATE_SCANNING != p_instance_ctrl->state, FSP_ERR_CTSU_SCANNING); + FSP_ERROR_RETURN(CTSU_TUNING_INCOMPLETE != p_instance_ctrl->tuning, FSP_ERR_CTSU_INCOMPLETE_TUNING); + +#if (BSP_FEATURE_CTSU_VERSION == 1) + FSP_ERROR_RETURN(CTSU_SPECIFIC_SELECTED_FREQ != specific_data_type, FSP_ERR_NOT_ENABLED); +#endif +#if (BSP_FEATURE_CTSU_VERSION == 2) + #if (CTSU_CFG_MAJORITY_MODE & CTSU_JUDGEMENT_MAJORITY_MODE) + FSP_ERROR_RETURN(CTSU_SPECIFIC_SELECTED_FREQ != specific_data_type, FSP_ERR_NOT_ENABLED); + FSP_ERROR_RETURN(CTSU_SPECIFIC_CORRECTION_DATA != specific_data_type, FSP_ERR_NOT_ENABLED); + #endif + #if (CTSU_CFG_AUTO_CORRECTION_ENABLE == 1) + FSP_ERROR_RETURN(CTSU_SPECIFIC_RAW_DATA != specific_data_type, FSP_ERR_NOT_ENABLED); + #endif + + #if (CTSU_CFG_MAJORITY_MODE & (CTSU_JUDGEMENT_MAJORITY_MODE | CTSU_VALUE_MAJORITY_MODE)) + if (p_instance_ctrl->p_ctsu_cfg->majority_mode == 1) + { + FSP_ERROR_RETURN(CTSU_SPECIFIC_CORRECTION_DATA != specific_data_type, FSP_ERR_NOT_ENABLED); + } + #endif + + #if (CTSU_CFG_AUTO_MULTI_CLOCK_CORRECTION_ENABLE == 1) + if (1 == p_instance_ctrl->p_ctsu_cfg->ajfen) + { + if (1 == p_instance_ctrl->p_ctsu_cfg->majirimd) + { + FSP_ERROR_RETURN(CTSU_SPECIFIC_CCO_CORRECTION_DATA != specific_data_type, FSP_ERR_NOT_ENABLED); + FSP_ERROR_RETURN(CTSU_SPECIFIC_CORRECTION_DATA != specific_data_type, FSP_ERR_NOT_ENABLED); + FSP_ERROR_RETURN(CTSU_SPECIFIC_SELECTED_FREQ != specific_data_type, FSP_ERR_NOT_ENABLED); + } + } + else + { + if (0 == p_instance_ctrl->p_ctsu_cfg->majority_mode) + { + FSP_ERROR_RETURN(CTSU_SPECIFIC_CCO_CORRECTION_DATA != specific_data_type, FSP_ERR_NOT_ENABLED); + } + } + #endif + + #if (CTSU_CFG_AUTO_JUDGE_ENABLE == 1) + if (1 == p_instance_ctrl->p_ctsu_cfg->ajfen) + { + if (0 == p_instance_ctrl->p_ctsu_cfg->majirimd) + { + FSP_ERROR_RETURN(CTSU_SPECIFIC_CORRECTION_DATA != specific_data_type, FSP_ERR_NOT_ENABLED); + FSP_ERROR_RETURN(CTSU_SPECIFIC_SELECTED_FREQ != specific_data_type, FSP_ERR_NOT_ENABLED); + } + } + #endif +#endif + + if (CTSU_SPECIFIC_RAW_DATA == specific_data_type) + { +#if (CTSU_CFG_NUM_SELF_ELEMENTS != 0) + if (CTSU_MODE_SELF_MULTI_SCAN == p_instance_ctrl->md) + { + for (element_id = 0; element_id < p_instance_ctrl->num_elements; element_id++) + { + #if (BSP_FEATURE_CTSU_VERSION == 1) + *p_specific_data = (p_instance_ctrl->p_self_raw + element_id)->sen; + p_specific_data++; + #endif + #if (BSP_FEATURE_CTSU_VERSION == 2) + for (i = 0; i < CTSU_CFG_NUM_SUMULTI; i++) + { + *p_specific_data = *(p_instance_ctrl->p_self_raw + (element_id * CTSU_CFG_NUM_SUMULTI) + i); + p_specific_data++; + } + #endif + } + } +#endif +#if (CTSU_CFG_NUM_MUTUAL_ELEMENTS != 0) + if (CTSU_MODE_MUTUAL_FULL_SCAN == (CTSU_MODE_MUTUAL_FULL_SCAN & p_instance_ctrl->md)) + { + for (element_id = 0; element_id < p_instance_ctrl->num_elements; element_id++) + { + #if (BSP_FEATURE_CTSU_VERSION == 1) + *p_specific_data = (p_instance_ctrl->p_mutual_raw + element_id)->pri_sen; + p_specific_data++; + *p_specific_data = (p_instance_ctrl->p_mutual_raw + element_id)->snd_sen; + p_specific_data++; + #endif + #if (BSP_FEATURE_CTSU_VERSION == 2) + for (i = 0; i < CTSU_CFG_NUM_SUMULTI; i++) + { + *p_specific_data = + *(p_instance_ctrl->p_mutual_raw + (element_id * CTSU_CFG_NUM_SUMULTI * 2) + (i * 2)); + p_specific_data++; + *p_specific_data = + *(p_instance_ctrl->p_mutual_raw + (element_id * CTSU_CFG_NUM_SUMULTI * 2) + (i * 2) + 1); + p_specific_data++; + } + #endif + } + } +#endif + } + else if (CTSU_SPECIFIC_CCO_CORRECTION_DATA == specific_data_type) + { +#if (CTSU_CFG_NUM_SELF_ELEMENTS != 0) + if (CTSU_MODE_SELF_MULTI_SCAN == p_instance_ctrl->md) + { + for (element_id = 0; element_id < (p_instance_ctrl->num_elements); element_id++) + { + for (i = 0; i < CTSU_CFG_NUM_SUMULTI; i++) + { + #if (BSP_FEATURE_CTSU_VERSION == 2) + #if (CTSU_CFG_AUTO_JUDGE_ENABLE == 1) + if (1 == p_instance_ctrl->p_ctsu_cfg->ajfen) + { + *p_specific_data = + *(p_instance_ctrl->p_self_raw + (element_id * CTSU_CFG_NUM_SUMULTI) + i); + p_specific_data++; + } + else + #endif + #endif + { + *p_specific_data = + *(p_instance_ctrl->p_self_corr + (element_id * CTSU_CFG_NUM_SUMULTI) + i); + p_specific_data++; + } + } + } + } +#endif +#if (CTSU_CFG_NUM_MUTUAL_ELEMENTS != 0) + if (CTSU_MODE_MUTUAL_FULL_SCAN == (CTSU_MODE_MUTUAL_FULL_SCAN & p_instance_ctrl->md)) + { + for (element_id = 0; element_id < (p_instance_ctrl->num_elements); element_id++) + { + for (i = 0; i < CTSU_CFG_NUM_SUMULTI; i++) + { + #if (BSP_FEATURE_CTSU_VERSION == 2) + #if (CTSU_CFG_AUTO_JUDGE_ENABLE == 1) + if (1 == p_instance_ctrl->p_ctsu_cfg->ajfen) + { + *p_specific_data = + *(p_instance_ctrl->p_mutual_raw + + ((element_id * CTSU_MUTUAL_BUF_SIZE) + (i * 2))); + p_specific_data++; + *p_specific_data = + *(p_instance_ctrl->p_mutual_raw + + ((element_id * CTSU_MUTUAL_BUF_SIZE) + (i * 2) + 1)); + p_specific_data++; + } + else + #endif + #endif + { + *p_specific_data = + *(p_instance_ctrl->p_mutual_pri_corr + (element_id * CTSU_CFG_NUM_SUMULTI) + + i); + p_specific_data++; + *p_specific_data = + *(p_instance_ctrl->p_mutual_snd_corr + (element_id * CTSU_CFG_NUM_SUMULTI) + + i); + p_specific_data++; + } + } + } + } +#endif + } + +#if (BSP_FEATURE_CTSU_VERSION == 2) + else if (CTSU_SPECIFIC_CORRECTION_DATA == specific_data_type) + { + #if (CTSU_CFG_NUM_SELF_ELEMENTS != 0) + if (CTSU_MODE_SELF_MULTI_SCAN == p_instance_ctrl->md) + { + for (element_id = 0; element_id < (p_instance_ctrl->num_elements); element_id++) + { + for (i = 0; i < CTSU_CFG_NUM_SUMULTI; i++) + { + *p_specific_data = *(p_instance_ctrl->p_self_mfc + (element_id * CTSU_CFG_NUM_SUMULTI) + i); + p_specific_data++; + } + } + } + #endif + #if (CTSU_CFG_NUM_MUTUAL_ELEMENTS != 0) + if (CTSU_MODE_MUTUAL_FULL_SCAN == (CTSU_MODE_MUTUAL_FULL_SCAN & p_instance_ctrl->md)) + { + for (element_id = 0; element_id < (p_instance_ctrl->num_elements); element_id++) + { + for (i = 0; i < CTSU_CFG_NUM_SUMULTI; i++) + { + *p_specific_data = *(p_instance_ctrl->p_mutual_pri_mfc + (element_id * CTSU_CFG_NUM_SUMULTI) + i); + p_specific_data++; + *p_specific_data = *(p_instance_ctrl->p_mutual_snd_mfc + (element_id * CTSU_CFG_NUM_SUMULTI) + i); + p_specific_data++; + } + } + } + #endif + } + else if (CTSU_SPECIFIC_SELECTED_FREQ == specific_data_type) + { + #if (CTSU_CFG_NUM_SELF_ELEMENTS != 0) + if (CTSU_MODE_SELF_MULTI_SCAN == p_instance_ctrl->md) + { + for (element_id = 0; element_id < (p_instance_ctrl->num_elements); element_id++) + { + *p_specific_data = *(p_instance_ctrl->p_selected_freq_self + element_id); + p_specific_data++; + } + } + #endif + #if (CTSU_CFG_NUM_MUTUAL_ELEMENTS != 0) + if (CTSU_MODE_MUTUAL_FULL_SCAN == (CTSU_MODE_MUTUAL_FULL_SCAN & p_instance_ctrl->md)) + { + for (element_id = 0; element_id < (p_instance_ctrl->num_elements); element_id++) + { + *p_specific_data = *(p_instance_ctrl->p_selected_freq_mutual + element_id); + p_specific_data++; + } + } + #endif + } +#endif + else + { + } + + return err; +} + +/*******************************************************************************************************************//** + * @brief 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 @ref ctsu_api_t::dataInsert. + * + * Example: + * @snippet r_ctsu_example.c R_CTSU_DataInsert + * + * @retval FSP_SUCCESS CTSU successfully configured. + * @retval FSP_ERR_ASSERTION Null pointer passed as a parameter. + * @retval FSP_ERR_NOT_OPEN Module is not open. + * @retval FSP_ERR_CTSU_SCANNING Scanning this instance. + * @retval FSP_ERR_CTSU_INCOMPLETE_TUNING Incomplete initial offset tuning. + **********************************************************************************************************************/ +fsp_err_t R_CTSU_DataInsert (ctsu_ctrl_t * const p_ctrl, uint16_t * p_insert_data) +{ + fsp_err_t err = FSP_SUCCESS; + ctsu_instance_ctrl_t * p_instance_ctrl = (ctsu_instance_ctrl_t *) p_ctrl; + uint16_t element_id; + uint16_t majority_mode_elem_id; +#if (CTSU_CFG_PARAM_CHECKING_ENABLE == 1) + FSP_ASSERT(p_instance_ctrl); + FSP_ASSERT(p_insert_data); + FSP_ERROR_RETURN(CTSU_OPEN == p_instance_ctrl->open, FSP_ERR_NOT_OPEN); +#endif + FSP_ERROR_RETURN(CTSU_STATE_SCANNING != p_instance_ctrl->state, FSP_ERR_CTSU_SCANNING); + FSP_ERROR_RETURN(CTSU_TUNING_INCOMPLETE != p_instance_ctrl->tuning, FSP_ERR_CTSU_INCOMPLETE_TUNING); + +#if (CTSU_CFG_NUM_SELF_ELEMENTS != 0) + if (CTSU_MODE_SELF_MULTI_SCAN == (CTSU_MODE_MUTUAL_FULL_SCAN & p_instance_ctrl->md)) + { + /* Data output */ + for (element_id = 0; element_id < p_instance_ctrl->num_elements; element_id++) + { + for (majority_mode_elem_id = 0; + majority_mode_elem_id < CTSU_MAJORITY_MODE_ELEMENTS; + majority_mode_elem_id++) + { + (p_instance_ctrl->p_self_data + (element_id * CTSU_MAJORITY_MODE_ELEMENTS) + + majority_mode_elem_id)->int_data = *p_insert_data; + p_insert_data++; + } + } + } +#endif +#if (CTSU_CFG_NUM_MUTUAL_ELEMENTS != 0) + if (CTSU_MODE_MUTUAL_FULL_SCAN == (CTSU_MODE_MUTUAL_FULL_SCAN & p_instance_ctrl->md)) + { + for (element_id = 0; element_id < p_instance_ctrl->num_elements; element_id++) + { + for (majority_mode_elem_id = 0; + majority_mode_elem_id < CTSU_MAJORITY_MODE_ELEMENTS; + majority_mode_elem_id++) + { + (p_instance_ctrl->p_mutual_pri_data + (element_id * CTSU_MAJORITY_MODE_ELEMENTS) + + majority_mode_elem_id)->int_data = *p_insert_data; + p_insert_data++; + (p_instance_ctrl->p_mutual_snd_data + (element_id * CTSU_MAJORITY_MODE_ELEMENTS) + + majority_mode_elem_id)->int_data = *p_insert_data; + p_insert_data++; + } + } + } +#endif + + return err; +} + +/*******************************************************************************************************************//** + * @} (end addtogroup CTSU) + **********************************************************************************************************************/ + +/*********************************************************************************************************************** + * Private Functions + **********************************************************************************************************************/ + +/*******************************************************************************************************************//** + * Internal ctsu private function. + **********************************************************************************************************************/ + +#if (CTSU_CFG_DTC_SUPPORT_ENABLE == 1) +/*********************************************************************************************************************** + * ctsu_transfer_open + ***********************************************************************************************************************/ +fsp_err_t ctsu_transfer_open (ctsu_instance_ctrl_t * const p_instance_ctrl) +{ + R_DTC_Open(); + + R_DTC_Control(DTC_CMD_DTC_START, NULL, NULL); + + return FSP_SUCCESS; +} + +/*********************************************************************************************************************** + * ctsu_transfer_close + ***********************************************************************************************************************/ +fsp_err_t ctsu_transfer_close (ctsu_instance_ctrl_t * const p_instance_ctrl) +{ + R_DTC_Close(); + + return FSP_SUCCESS; +} + +/*********************************************************************************************************************** + * ctsu_transfer_configure + ***********************************************************************************************************************/ +fsp_err_t ctsu_transfer_configure (ctsu_instance_ctrl_t * const p_instance_ctrl) +{ + dtc_err_t dtc_err; + + #if (BSP_FEATURE_CTSU_VERSION == 2) + /* After the initial offset tuning is completed, set the DTC for automatic judgement. */ + /* In addition, the method for which automatic judgement is disabled does not set the DTC for automatic judgement. */ + /* Alternatively, DTC for automatic judgment will not be set during correction measurement. */ + if ((0 == p_instance_ctrl->p_ctsu_cfg->ajfen) || (CTSU_CORRECTION_RUN == g_ctsu_correction_info.status) || (CTSU_TUNING_INCOMPLETE == p_instance_ctrl->tuning)) + { + dtc_err = ctsu_transfer_normal(p_instance_ctrl); + if (dtc_err != DTC_SUCCESS) + { + return FSP_ERR_ASSERTION; + } + } + #if (CTSU_CFG_AUTO_JUDGE_ENABLE == 1) + else /* DTC setting for automatic judgement */ + { + dtc_err = ctsu_transfer_autojudge(p_instance_ctrl); + if (dtc_err != DTC_SUCCESS) + { + return FSP_ERR_ASSERTION; + } + } + #endif + #endif + + #if (BSP_FEATURE_CTSU_VERSION == 1) + dtc_err = ctsu_transfer_ctsu1(p_instance_ctrl); + if (dtc_err != DTC_SUCCESS) + { + return FSP_ERR_ASSERTION; + } + #endif + + return FSP_SUCCESS; +} + + #if (BSP_FEATURE_CTSU_VERSION == 2) +/*********************************************************************************************************************** + * ctsu_transfer_normal + ***********************************************************************************************************************/ +dtc_err_t ctsu_transfer_normal (ctsu_instance_ctrl_t * const p_instance_ctrl) +{ + dtc_err_t dtc_err; + + dtc_err = ctsu_transfer_normal_ctsuwr(p_instance_ctrl); + + dtc_err = ctsu_transfer_normal_ctsurd(p_instance_ctrl); + + return dtc_err; +} + +/*********************************************************************************************************************** + * ctsu_transfer_normal_ctsuwr + ***********************************************************************************************************************/ +dtc_err_t ctsu_transfer_normal_ctsuwr (ctsu_instance_ctrl_t * const p_instance_ctrl) +{ + dtc_err_t dtc_err; + uint8_t ien_bk; + uint32_t calc_transfer_count; + + calc_transfer_count = p_instance_ctrl->num_elements * CTSU_CFG_NUM_SUMULTI; + + g_ctsu_dtc_cmd_arg.act_src = (dtc_activation_source_t)IR_CTSU_CTSUWR; + + R_DTC_Control(DTC_CMD_ACT_SRC_DISABLE, NULL, &g_ctsu_dtc_cmd_arg); + + ctsu_transfer_ctsuso_set(0, DTC_CHAIN_TRANSFER_DISABLE, DTC_CHAIN_TRANSFER_CONTINUOUSLY, calc_transfer_count); + ctsu_transfer_address_set(0, (uint32_t *)p_instance_ctrl->p_ctsuwr, (uint32_t *)&(CTSU.CTSUSO.LONG)); + + if (CTSU_CORRECTION_RUN == g_ctsu_correction_info.status) + { + g_ctsu_dtc_info[0].transfer_count = 1; + g_ctsu_dtc_info[0].source_addr = (uint32_t)&(g_ctsu_correction_info.ctsuwr); + } + + #if (CTSU_CFG_DIAG_SUPPORT_ENABLE == 1) + else if (CTSU_MODE_DIAGNOSIS_SCAN == p_instance_ctrl->md) + { + if (CTSU_DIAG_CLOCK_RECOVERY == g_ctsu_diag_info.state) + { + g_ctsu_dtc_info[0].src_addr_mode = DTC_SRC_ADDR_FIXED; + g_ctsu_dtc_info[0].transfer_count = 3; + } + else + { + g_ctsu_dtc_info[0].src_addr_mode = DTC_SRC_ADDR_INCR; + g_ctsu_dtc_info[0].transfer_count = 1; + } + + g_ctsu_dtc_info[0].source_addr = (uint32_t)&(g_ctsu_diag_info.ctsuwr); + } + #endif + else if (CTSU_MODE_CURRENT_SCAN == p_instance_ctrl->md) + { + g_ctsu_dtc_info[0].transfer_count = p_instance_ctrl->num_elements; + } + else + { + #if (CTSU_CFG_AUTO_MULTI_CLOCK_CORRECTION_ENABLE == 1) + if ((0 == p_instance_ctrl->p_ctsu_cfg->majority_mode) && (CTSU_TUNING_INCOMPLETE != p_instance_ctrl->tuning)) + { + ctsu_transfer_mcact_set(0, calc_transfer_count); + ctsu_transfer_address_set(0, (uint32_t *)p_instance_ctrl->p_mcact1, (uint32_t *)&(CTSU.CTSUMCACT1.LONG)); + + ctsu_transfer_ctsuso_set(1, DTC_CHAIN_TRANSFER_DISABLE, DTC_CHAIN_TRANSFER_CONTINUOUSLY, calc_transfer_count); + ctsu_transfer_address_set(1, (uint32_t *)p_instance_ctrl->p_ctsuwr, (uint32_t *)&(CTSU.CTSUSO.LONG)); + + ien_bk = IEN(CTSU,CTSUWR); + IEN(CTSU,CTSUWR) = 0U; + + dtc_err = R_DTC_Create(g_ctsu_dtc_cmd_arg.act_src, g_transfer_data_tx, g_ctsu_dtc_info, 1); + + IEN(CTSU,CTSUWR) = ien_bk; + + R_DTC_Control(DTC_CMD_ACT_SRC_ENABLE, NULL, &g_ctsu_dtc_cmd_arg); + + return dtc_err; + } + #endif + } + + ien_bk = IEN(CTSU,CTSUWR); + IEN(CTSU,CTSUWR) = 0U; + + dtc_err = R_DTC_Create(g_ctsu_dtc_cmd_arg.act_src, g_transfer_data_tx, g_ctsu_dtc_info, 0); + + IEN(CTSU,CTSUWR) = ien_bk; + + R_DTC_Control(DTC_CMD_ACT_SRC_ENABLE, NULL, &g_ctsu_dtc_cmd_arg); + + return dtc_err; +} + +/*********************************************************************************************************************** + * ctsu_transfer_normal_ctsurd + ***********************************************************************************************************************/ +dtc_err_t ctsu_transfer_normal_ctsurd (ctsu_instance_ctrl_t * const p_instance_ctrl) +{ + dtc_err_t dtc_err; + uint8_t ien_bk; + uint32_t calc_transfer_count; + + calc_transfer_count = p_instance_ctrl->num_elements * CTSU_CFG_NUM_SUMULTI; + + g_ctsu_dtc_cmd_arg.act_src = (dtc_activation_source_t)IR_CTSU_CTSURD; + + R_DTC_Control(DTC_CMD_ACT_SRC_DISABLE, NULL, &g_ctsu_dtc_cmd_arg); + + ctsu_transfer_ctsuscnt_set(0, DTC_CHAIN_TRANSFER_DISABLE, DTC_CHAIN_TRANSFER_CONTINUOUSLY, calc_transfer_count); + ctsu_transfer_address_set(0, (uint32_t *)&(CTSU.CTSUSCNT.LONG), (uint32_t *)p_instance_ctrl->p_self_raw); + + if (CTSU_CORRECTION_RUN == g_ctsu_correction_info.status) + { + g_ctsu_dtc_info[0].transfer_count = 1; + g_ctsu_dtc_info[0].dest_addr = (uint32_t)&g_ctsu_correction_info.scanbuf; + } + + #if (CTSU_CFG_DIAG_SUPPORT_ENABLE == 1) + else if (CTSU_MODE_DIAGNOSIS_SCAN == p_instance_ctrl->md) + { + g_ctsu_dtc_info[0].data_size = DTC_DATA_SIZE_LWORD; + g_ctsu_dtc_info[0].block_size = 1; + if (CTSU_DIAG_CLOCK_RECOVERY == g_ctsu_diag_info.state) + { + g_ctsu_dtc_info[0].dest_addr_mode = DTC_DES_ADDR_INCR; + g_ctsu_dtc_info[0].transfer_count = 3; + } + else + { + g_ctsu_dtc_info[0].transfer_count = 1; + } + + g_ctsu_dtc_info[0].dest_addr = (uint32_t)&(g_ctsu_diag_info.ctsuscnt[0]); + g_ctsu_dtc_info[0].source_addr = (uint32_t)&(CTSU.CTSUSCNT.LONG); + } + #endif + else + { + if (CTSU_MODE_CURRENT_SCAN == p_instance_ctrl->md) + { + g_ctsu_dtc_info[0].transfer_count = p_instance_ctrl->num_elements; + } + #if (CTSU_CFG_NUM_MUTUAL_ELEMENTS != 0) + if (CTSU_MODE_MUTUAL_FULL_SCAN == (CTSU_MODE_MUTUAL_FULL_SCAN & p_instance_ctrl->md)) + { + g_ctsu_dtc_info[0].dest_addr = (uint32_t)p_instance_ctrl->p_mutual_raw; + g_ctsu_dtc_info[0].transfer_count = (uint16_t) (g_ctsu_dtc_info[0].transfer_count * 2); + } + #endif + } + + ien_bk = IEN(CTSU,CTSUWR); + IEN(CTSU,CTSUWR) = 0U; + + dtc_err = R_DTC_Create(g_ctsu_dtc_cmd_arg.act_src, g_transfer_data_rx, g_ctsu_dtc_info, 0); + + IEN(CTSU,CTSUWR) = ien_bk; + + R_DTC_Control(DTC_CMD_ACT_SRC_ENABLE, NULL, &g_ctsu_dtc_cmd_arg); + + return dtc_err; +} + + #if (CTSU_CFG_AUTO_JUDGE_ENABLE == 1) +/*********************************************************************************************************************** + * ctsu_transfer_autojudge + ***********************************************************************************************************************/ +dtc_err_t ctsu_transfer_autojudge (ctsu_instance_ctrl_t * const p_instance_ctrl) +{ + dtc_err_t dtc_err; + + dtc_err = ctsu_transfer_autojudge_ctsuwr(p_instance_ctrl); + + dtc_err = ctsu_transfer_autojudge_ctsurd(p_instance_ctrl); + + return dtc_err; +} + +/*********************************************************************************************************************** + * ctsu_transfer_autojudge_ctsuwr + ***********************************************************************************************************************/ +dtc_err_t ctsu_transfer_autojudge_ctsuwr (ctsu_instance_ctrl_t * const p_instance_ctrl) +{ + dtc_err_t dtc_err; + uint8_t ien_bk; + uint32_t calc_transfer_count; + + g_ctsu_dtc_cmd_arg.act_src = (dtc_activation_source_t)IR_CTSU_CTSUWR; + + R_DTC_Control(DTC_CMD_ACT_SRC_DISABLE, NULL, &g_ctsu_dtc_cmd_arg); + + #if (CTSU_CFG_AUTO_MULTI_CLOCK_CORRECTION_ENABLE == 1) + if (1 == p_instance_ctrl->p_ctsu_cfg->majirimd) + { + calc_transfer_count = p_instance_ctrl->num_elements; + + /* For automatic judgement register transfer */ + ctsu_transfer_autojudge_value_set(0, DTC_REPEAT_BLOCK_DESTINATION, calc_transfer_count); + ctsu_transfer_address_set(0, (uint32_t *)p_instance_ctrl->p_auto_judge, (uint32_t *)&(CTSU.CTSUAJTHR.LONG)); + + /* For CTSUMCACT1 register transfer */ + ctsu_transfer_mcact_set(1, calc_transfer_count); + ctsu_transfer_address_set(1, (uint32_t *)p_instance_ctrl->p_mcact1, (uint32_t *)&(CTSU.CTSUMCACT1.LONG)); + + /* For CTSUMCACT2 register transfer */ + ctsu_transfer_mcact_set(2, calc_transfer_count); + ctsu_transfer_address_set(2, (uint32_t *)p_instance_ctrl->p_mcact2, (uint32_t *)&(CTSU.CTSUMCACT2.LONG)); + + /* For CTSUSO register transfer */ + ctsu_transfer_ctsuso_set(3, DTC_CHAIN_TRANSFER_ENABLE, DTC_CHAIN_TRANSFER_NORMAL, calc_transfer_count); + ctsu_transfer_address_set(3, (uint32_t *)p_instance_ctrl->p_ctsuwr, (uint32_t *)&(CTSU.CTSUSO.LONG)); + + /* For transfer of the start address of variables for automatic judgement */ + ctsu_transfer_autojudge_repeat_set(4, DTC_CHAIN_TRANSFER_ENABLE, DTC_REPEAT_BLOCK_DESTINATION); + ctsu_transfer_address_set(4, (uint32_t *)&p_instance_ctrl->adress_auto_judge, (uint32_t *)&g_transfer_data_tx[0].lw2); + + /* Number of transfers in the automatic judgement register For forwarding */ + ctsu_transfer_autojudge_repeat_set(5, DTC_CHAIN_TRANSFER_ENABLE, DTC_REPEAT_BLOCK_DESTINATION); + ctsu_transfer_address_set(5, (uint32_t *)&p_instance_ctrl->count_auto_judge, (uint32_t *)&g_transfer_data_tx[0].lw4); + + /* For transfer of the start address of variables for CTSUMCACT1 */ + ctsu_transfer_autojudge_repeat_set(6, DTC_CHAIN_TRANSFER_ENABLE, DTC_REPEAT_BLOCK_DESTINATION); + ctsu_transfer_address_set(6, (uint32_t *)&p_instance_ctrl->adress_mcact1, (uint32_t *)&g_transfer_data_tx[1].lw2); + + /* Number of transfers of CTSUMCACT1 register For forwarding */ + ctsu_transfer_autojudge_repeat_set(7, DTC_CHAIN_TRANSFER_ENABLE, DTC_REPEAT_BLOCK_DESTINATION); + ctsu_transfer_address_set(7, (uint32_t *)&p_instance_ctrl->count_mcact1, (uint32_t *)&g_transfer_data_tx[1].lw4); + + /* For transfer of the start address of variables for CTSUMCACT2 */ + ctsu_transfer_autojudge_repeat_set(8, DTC_CHAIN_TRANSFER_ENABLE, DTC_REPEAT_BLOCK_DESTINATION); + ctsu_transfer_address_set(8, (uint32_t *)&p_instance_ctrl->adress_mcact2, (uint32_t *)&g_transfer_data_tx[2].lw2); + + /* Number of transfers of CTSUMCACT2 register For forwarding */ + ctsu_transfer_autojudge_repeat_set(9, DTC_CHAIN_TRANSFER_ENABLE, DTC_REPEAT_BLOCK_DESTINATION); + ctsu_transfer_address_set(9, (uint32_t *)&p_instance_ctrl->count_mcact2, (uint32_t *)&g_transfer_data_tx[2].lw4); + + /* For transfer of the start address of variables for CTSUSO */ + ctsu_transfer_autojudge_repeat_set(10, DTC_CHAIN_TRANSFER_ENABLE, DTC_REPEAT_BLOCK_DESTINATION); + ctsu_transfer_address_set(10, (uint32_t *)&p_instance_ctrl->adress_ctsuwr, (uint32_t *)&g_transfer_data_tx[3].lw2); + + /* Number of transfers of CTSUSO register For forwarding */ + ctsu_transfer_autojudge_repeat_set(11, DTC_CHAIN_TRANSFER_DISABLE, DTC_REPEAT_BLOCK_DESTINATION); + ctsu_transfer_address_set(11, (uint32_t *)&p_instance_ctrl->count_ctsuwr_self_mutual, (uint32_t *)&g_transfer_data_tx[3].lw4); + + ien_bk = IEN(CTSU,CTSUWR); + IEN(CTSU,CTSUWR) = 0U; + + dtc_err = R_DTC_Create(g_ctsu_dtc_cmd_arg.act_src, g_transfer_data_tx, g_ctsu_dtc_info, 11); + } + else + #endif + { + calc_transfer_count = p_instance_ctrl->num_elements * CTSU_CFG_NUM_SUMULTI; + + /* For automatic judgement register transfer */ + ctsu_transfer_autojudge_value_set(0, DTC_REPEAT_BLOCK_DESTINATION, calc_transfer_count); + ctsu_transfer_address_set(0, (uint32_t *)p_instance_ctrl->p_auto_judge, (uint32_t *)&(CTSU.CTSUAJTHR.LONG)); + + /* For CTSUSO register transfer */ + ctsu_transfer_ctsuso_set(1, DTC_CHAIN_TRANSFER_ENABLE, DTC_CHAIN_TRANSFER_NORMAL, calc_transfer_count); + ctsu_transfer_address_set(1, (uint32_t *)p_instance_ctrl->p_ctsuwr, (uint32_t *)&(CTSU.CTSUSO.LONG)); + + /* For transfer of the start address of variables for automatic judgement */ + ctsu_transfer_autojudge_repeat_set(2, DTC_CHAIN_TRANSFER_ENABLE, DTC_REPEAT_BLOCK_DESTINATION); + ctsu_transfer_address_set(2, (uint32_t *)&p_instance_ctrl->adress_auto_judge, (uint32_t *)&g_transfer_data_tx[0].lw2); + + /* Number of transfers in the automatic judgement register For forwarding */ + ctsu_transfer_autojudge_repeat_set(3, DTC_CHAIN_TRANSFER_ENABLE, DTC_REPEAT_BLOCK_DESTINATION); + ctsu_transfer_address_set(3, (uint32_t *)&p_instance_ctrl->count_auto_judge, (uint32_t *)&g_transfer_data_tx[0].lw4); + + /* For transfer of the start address of variables for CTSUSO */ + ctsu_transfer_autojudge_repeat_set(4, DTC_CHAIN_TRANSFER_ENABLE, DTC_REPEAT_BLOCK_DESTINATION); + ctsu_transfer_address_set(4, (uint32_t *)&p_instance_ctrl->adress_ctsuwr, (uint32_t *)&g_transfer_data_tx[1].lw2); + + /* Number of transfers of CTSUSO register For forwarding */ + ctsu_transfer_autojudge_repeat_set(5, DTC_CHAIN_TRANSFER_DISABLE, DTC_REPEAT_BLOCK_DESTINATION); + ctsu_transfer_address_set(5, (uint32_t *)&p_instance_ctrl->count_ctsuwr_self_mutual, (uint32_t *)&g_transfer_data_tx[1].lw4); + + ien_bk = IEN(CTSU,CTSUWR); + IEN(CTSU,CTSUWR) = 0U; + + dtc_err = R_DTC_Create(g_ctsu_dtc_cmd_arg.act_src, g_transfer_data_tx, g_ctsu_dtc_info, 5); + } + + IEN(CTSU,CTSUWR) = ien_bk; + + R_DTC_Control(DTC_CMD_ACT_SRC_ENABLE, NULL, &g_ctsu_dtc_cmd_arg); + + return dtc_err; +} + +/*********************************************************************************************************************** + * ctsu_transfer_autojudge_ctsurd + ***********************************************************************************************************************/ +dtc_err_t ctsu_transfer_autojudge_ctsurd (ctsu_instance_ctrl_t * const p_instance_ctrl) +{ + dtc_err_t dtc_err; + uint8_t ien_bk; + uint32_t calc_transfer_count; + + g_ctsu_dtc_cmd_arg.act_src = (dtc_activation_source_t)IR_CTSU_CTSURD; + + R_DTC_Control(DTC_CMD_ACT_SRC_DISABLE, NULL, &g_ctsu_dtc_cmd_arg); + + #if (CTSU_CFG_AUTO_MULTI_CLOCK_CORRECTION_ENABLE == 1) + if (1 == p_instance_ctrl->p_ctsu_cfg->majirimd) + { + calc_transfer_count = p_instance_ctrl->num_elements; + } + else + #endif + { + calc_transfer_count = p_instance_ctrl->num_elements * CTSU_CFG_NUM_SUMULTI; + } + + /* For automatic judgement register transfer */ + ctsu_transfer_autojudge_value_set(0, DTC_REPEAT_BLOCK_SOURCE, calc_transfer_count); + ctsu_transfer_address_set(0, (uint32_t *)&(CTSU.CTSUAJTHR.LONG), (uint32_t *)p_instance_ctrl->p_auto_judge); + + /* For CTSUSCNT register transfer */ + ctsu_transfer_ctsuscnt_set(1, DTC_CHAIN_TRANSFER_ENABLE, DTC_CHAIN_TRANSFER_NORMAL, calc_transfer_count); + ctsu_transfer_address_set(1, (uint32_t *)&(CTSU.CTSUSCNT.LONG), (uint32_t *)p_instance_ctrl->p_self_raw); + #if (CTSU_CFG_NUM_MUTUAL_ELEMENTS != 0) + if (CTSU_MODE_MUTUAL_FULL_SCAN == (CTSU_MODE_MUTUAL_FULL_SCAN & p_instance_ctrl->md)) + { + g_ctsu_dtc_info[1].dest_addr = (uint32_t)p_instance_ctrl->p_mutual_raw; + } + #endif + + /* For transfer of the start address of variables for automatic judgement */ + ctsu_transfer_autojudge_repeat_set(2, DTC_CHAIN_TRANSFER_ENABLE, DTC_REPEAT_BLOCK_SOURCE); + ctsu_transfer_address_set(2, (uint32_t *)&p_instance_ctrl->adress_auto_judge, (uint32_t *)&g_transfer_data_rx[0].lw3); + + /* Number of transfers in the automatic judgement register For forwarding */ + ctsu_transfer_autojudge_repeat_set(3, DTC_CHAIN_TRANSFER_ENABLE, DTC_REPEAT_BLOCK_SOURCE); + ctsu_transfer_address_set(3, (uint32_t *)&p_instance_ctrl->count_auto_judge, (uint32_t *)&g_transfer_data_rx[0].lw4); + + /* For transfer of the start address of variables for CTSUSCNT */ + ctsu_transfer_autojudge_repeat_set(4, DTC_CHAIN_TRANSFER_ENABLE, DTC_REPEAT_BLOCK_SOURCE); + ctsu_transfer_address_set(4, (uint32_t *)&p_instance_ctrl->adress_self_raw, (uint32_t *)&g_transfer_data_rx[1].lw3); + #if (CTSU_CFG_NUM_MUTUAL_ELEMENTS != 0) + if (CTSU_MODE_MUTUAL_FULL_SCAN == (CTSU_MODE_MUTUAL_FULL_SCAN & p_instance_ctrl->md)) + { + g_ctsu_dtc_info[4].source_addr = (uint32_t)&p_instance_ctrl->adress_mutual_raw; + } + #endif + + /* Number of transfers of CTSUSCNT register For forwarding */ + ctsu_transfer_autojudge_repeat_set(5, DTC_CHAIN_TRANSFER_DISABLE, DTC_REPEAT_BLOCK_SOURCE); + ctsu_transfer_address_set(5, (uint32_t *)&p_instance_ctrl->count_ctsuwr_self_mutual, (uint32_t *)&g_transfer_data_rx[1].lw4); + + ien_bk = IEN(CTSU,CTSUWR); + IEN(CTSU,CTSUWR) = 0U; + + dtc_err = R_DTC_Create(g_ctsu_dtc_cmd_arg.act_src, g_transfer_data_rx, g_ctsu_dtc_info, 5); + + IEN(CTSU,CTSUWR) = ien_bk; + + R_DTC_Control(DTC_CMD_ACT_SRC_ENABLE, NULL, &g_ctsu_dtc_cmd_arg); + + return dtc_err; +} + #endif + #endif + + #if (BSP_FEATURE_CTSU_VERSION == 1) +/*********************************************************************************************************************** + * ctsu_transfer_ctsu1 + ***********************************************************************************************************************/ +dtc_err_t ctsu_transfer_ctsu1 (ctsu_instance_ctrl_t * const p_instance_ctrl) +{ + dtc_err_t dtc_err; + + dtc_err = ctsu_transfer_ctsu1_ctsuwr(p_instance_ctrl); + + dtc_err = ctsu_transfer_ctsu1_ctsurd(p_instance_ctrl); + + return dtc_err; +} + +/*********************************************************************************************************************** + * ctsu_transfer_ctsu1_ctsuwr + ***********************************************************************************************************************/ +dtc_err_t ctsu_transfer_ctsu1_ctsuwr (ctsu_instance_ctrl_t * const p_instance_ctrl) +{ + dtc_err_t dtc_err; + uint8_t ien_bk; + uint32_t calc_transfer_count; + + calc_transfer_count = p_instance_ctrl->num_elements * CTSU_CFG_NUM_SUMULTI; + + g_ctsu_dtc_cmd_arg.act_src = (dtc_activation_source_t)IR_CTSU_CTSUWR; + + R_DTC_Control(DTC_CMD_ACT_SRC_DISABLE, NULL, &g_ctsu_dtc_cmd_arg); + + ctsu_transfer_ctsuso_set(0, DTC_CHAIN_TRANSFER_DISABLE, DTC_CHAIN_TRANSFER_CONTINUOUSLY, calc_transfer_count); + ctsu_transfer_address_set(0, (uint32_t *)p_instance_ctrl->p_ctsuwr, (uint32_t *)&(CTSU.CTSUSSC)); + + if (CTSU_CORRECTION_RUN == g_ctsu_correction_info.status) + { + g_ctsu_dtc_info[0].transfer_count = 1; + g_ctsu_dtc_info[0].source_addr = (uint32_t)&(g_ctsu_correction_info.ctsuwr); + } + + #if (CTSU_CFG_DIAG_SUPPORT_ENABLE == 1) + else if (CTSU_MODE_DIAGNOSIS_SCAN == p_instance_ctrl->md) + { + g_ctsu_dtc_info[0].transfer_count = 1; + g_ctsu_dtc_info[0].source_addr = (uint32_t)&(g_ctsu_diag_info.ctsuwr); + } + #endif + else if ((CTSU_CORRECTION_RUN != g_ctsu_correction_info.status) && + (CTSU_MODE_DIAGNOSIS_SCAN != p_instance_ctrl->md)) + { + + #if (CTSU_CFG_NUM_MUTUAL_ELEMENTS != 0) + if (CTSU_MODE_MUTUAL_FULL_SCAN == (CTSU_MODE_MUTUAL_FULL_SCAN & p_instance_ctrl->md)) + { + if (true == p_instance_ctrl->serial_tuning_enable) + { + if (0 == ((p_instance_ctrl->ctsucr1 >> 7) & 0x01)) + { + g_ctsu_dtc_info[0].transfer_count = (uint16_t) (p_instance_ctrl->num_elements * 2); + } + } + } + #endif + } + else + { + } + + ien_bk = IEN(CTSU,CTSUWR); + IEN(CTSU,CTSUWR) = 0U; + + dtc_err = R_DTC_Create(g_ctsu_dtc_cmd_arg.act_src, g_transfer_data_tx, g_ctsu_dtc_info, 0); + + IEN(CTSU,CTSUWR) = ien_bk; + + R_DTC_Control(DTC_CMD_ACT_SRC_ENABLE, NULL, &g_ctsu_dtc_cmd_arg); + + return dtc_err; +} + +/*********************************************************************************************************************** + * ctsu_transfer_ctsu1_ctsurd + ***********************************************************************************************************************/ +dtc_err_t ctsu_transfer_ctsu1_ctsurd (ctsu_instance_ctrl_t * const p_instance_ctrl) +{ + dtc_err_t dtc_err; + uint8_t ien_bk; + uint32_t calc_transfer_count; + + calc_transfer_count = p_instance_ctrl->num_elements * CTSU_CFG_NUM_SUMULTI; + + g_ctsu_dtc_cmd_arg.act_src = (dtc_activation_source_t)IR_CTSU_CTSURD; + + R_DTC_Control(DTC_CMD_ACT_SRC_DISABLE, NULL, &g_ctsu_dtc_cmd_arg); + + ctsu_transfer_ctsuscnt_set(0, DTC_CHAIN_TRANSFER_DISABLE, DTC_CHAIN_TRANSFER_CONTINUOUSLY, calc_transfer_count); + ctsu_transfer_address_set(0, (uint32_t *)&(CTSU.CTSUSC.WORD), (uint32_t *)p_instance_ctrl->p_self_raw); + + if (CTSU_CORRECTION_RUN == g_ctsu_correction_info.status) + { + g_ctsu_dtc_info[0].transfer_count = 1; + g_ctsu_dtc_info[0].dest_addr = (uint32_t)&g_ctsu_correction_info.scanbuf; + } + + #if (CTSU_CFG_DIAG_SUPPORT_ENABLE == 1) + else if (CTSU_MODE_DIAGNOSIS_SCAN == p_instance_ctrl->md) + { + g_ctsu_dtc_info[0].transfer_count = 1; + g_ctsu_dtc_info[0].dest_addr = (uint32_t)&g_ctsu_diag_info.scanbuf; + } + #endif + else if ((CTSU_CORRECTION_RUN != g_ctsu_correction_info.status) && + (CTSU_MODE_DIAGNOSIS_SCAN != p_instance_ctrl->md)) + { + #if (CTSU_CFG_NUM_MUTUAL_ELEMENTS != 0) + if (CTSU_MODE_MUTUAL_FULL_SCAN == (CTSU_MODE_MUTUAL_FULL_SCAN & p_instance_ctrl->md)) + { + g_ctsu_dtc_info[0].dest_addr = (uint32_t)p_instance_ctrl->p_mutual_raw; + g_ctsu_dtc_info[0].transfer_count = (uint16_t) (g_ctsu_dtc_info[0].transfer_count * 2); + + if (true == p_instance_ctrl->serial_tuning_enable) + { + if (0 == ((p_instance_ctrl->ctsucr1 >> 7) & 0x01)) + { + g_ctsu_dtc_info[0].transfer_count = (uint16_t) (p_instance_ctrl->num_elements * 2); + } + } + } + #endif + } + else + { + } + + ien_bk = IEN(CTSU,CTSUWR); + IEN(CTSU,CTSUWR) = 0U; + + dtc_err = R_DTC_Create(g_ctsu_dtc_cmd_arg.act_src, g_transfer_data_rx, g_ctsu_dtc_info, 0); + + IEN(CTSU,CTSUWR) = ien_bk; + + R_DTC_Control(DTC_CMD_ACT_SRC_ENABLE, NULL, &g_ctsu_dtc_cmd_arg); + + return dtc_err; +} + #endif + +/*********************************************************************************************************************** + * ctsu_transfer_ctsuso_set + ***********************************************************************************************************************/ +void ctsu_transfer_ctsuso_set (uint8_t array_number, dtc_chain_transfer_t set_chain_transfer_enable, dtc_chain_transfer_mode_t set_chain_transfer_mode, uint32_t set_transfer_count) +{ + g_ctsu_dtc_info[array_number].chain_transfer_enable = set_chain_transfer_enable; + g_ctsu_dtc_info[array_number].chain_transfer_mode = set_chain_transfer_mode; + g_ctsu_dtc_info[array_number].src_addr_mode = DTC_SRC_ADDR_INCR; + #if (BSP_FEATURE_CTSU_VERSION == 2) + g_ctsu_dtc_info[array_number].dest_addr_mode = DTC_DES_ADDR_FIXED; + #endif + #if (BSP_FEATURE_CTSU_VERSION == 1) + g_ctsu_dtc_info[array_number].dest_addr_mode = DTC_DES_ADDR_INCR; + #endif + g_ctsu_dtc_info[array_number].response_interrupt = DTC_INTERRUPT_AFTER_ALL_COMPLETE; + g_ctsu_dtc_info[array_number].transfer_mode = DTC_TRANSFER_MODE_BLOCK; + g_ctsu_dtc_info[array_number].repeat_block_side = DTC_REPEAT_BLOCK_DESTINATION; + #if (BSP_FEATURE_CTSU_VERSION == 2) + g_ctsu_dtc_info[array_number].data_size = DTC_DATA_SIZE_LWORD; + #endif + #if (BSP_FEATURE_CTSU_VERSION == 1) + g_ctsu_dtc_info[array_number].data_size = DTC_DATA_SIZE_WORD; + #endif + #if (BSP_FEATURE_CTSU_VERSION == 2) + g_ctsu_dtc_info[array_number].block_size = 1; + #endif + #if (BSP_FEATURE_CTSU_VERSION == 1) + g_ctsu_dtc_info[array_number].block_size = 3; + #endif + g_ctsu_dtc_info[array_number].transfer_count = set_transfer_count; +} + +/*********************************************************************************************************************** + * ctsu_transfer_ctsuscnt_set + ***********************************************************************************************************************/ +void ctsu_transfer_ctsuscnt_set (uint8_t array_number, dtc_chain_transfer_t set_chain_transfer_enable, dtc_chain_transfer_mode_t set_chain_transfer_mode, uint32_t set_transfer_count) +{ + g_ctsu_dtc_info[array_number].chain_transfer_enable = set_chain_transfer_enable; + g_ctsu_dtc_info[array_number].chain_transfer_mode = set_chain_transfer_mode; + #if (BSP_FEATURE_CTSU_VERSION == 2) + g_ctsu_dtc_info[array_number].src_addr_mode = DTC_SRC_ADDR_FIXED; + #endif + #if (BSP_FEATURE_CTSU_VERSION == 1) + g_ctsu_dtc_info[array_number].src_addr_mode = DTC_SRC_ADDR_INCR; + #endif + g_ctsu_dtc_info[array_number].dest_addr_mode = DTC_DES_ADDR_INCR; + g_ctsu_dtc_info[array_number].response_interrupt = DTC_INTERRUPT_AFTER_ALL_COMPLETE; + g_ctsu_dtc_info[array_number].transfer_mode = DTC_TRANSFER_MODE_BLOCK; + g_ctsu_dtc_info[array_number].repeat_block_side = DTC_REPEAT_BLOCK_SOURCE; + g_ctsu_dtc_info[array_number].data_size = DTC_DATA_SIZE_WORD; + #if (BSP_FEATURE_CTSU_VERSION == 2) + g_ctsu_dtc_info[array_number].block_size = 1; + #endif + #if (BSP_FEATURE_CTSU_VERSION == 1) + g_ctsu_dtc_info[array_number].block_size = 2; + #endif + g_ctsu_dtc_info[array_number].transfer_count = set_transfer_count; +} + +/*********************************************************************************************************************** + * ctsu_transfer_address_set + ***********************************************************************************************************************/ +void ctsu_transfer_address_set (uint8_t array_number, uint32_t * p_set_source_addr, uint32_t * p_set_dest_addr) +{ + g_ctsu_dtc_info[array_number].source_addr = (uint32_t)p_set_source_addr; + g_ctsu_dtc_info[array_number].dest_addr = (uint32_t)p_set_dest_addr; +} + + #if (CTSU_CFG_AUTO_JUDGE_ENABLE == 1) +/*********************************************************************************************************************** +* ctsu_transfer_autojudge_value_set +***********************************************************************************************************************/ +void ctsu_transfer_autojudge_value_set (uint8_t array_number, dtc_repeat_block_side_t set_repeat_block_side, uint32_t set_transfer_count) +{ + g_ctsu_dtc_info[array_number].chain_transfer_enable = DTC_CHAIN_TRANSFER_ENABLE; + g_ctsu_dtc_info[array_number].chain_transfer_mode = DTC_CHAIN_TRANSFER_CONTINUOUSLY; + g_ctsu_dtc_info[array_number].src_addr_mode = DTC_SRC_ADDR_INCR; + g_ctsu_dtc_info[array_number].dest_addr_mode = DTC_DES_ADDR_INCR; + g_ctsu_dtc_info[array_number].response_interrupt = DTC_INTERRUPT_AFTER_ALL_COMPLETE; + g_ctsu_dtc_info[array_number].transfer_mode = DTC_TRANSFER_MODE_BLOCK; + g_ctsu_dtc_info[array_number].repeat_block_side = set_repeat_block_side; + g_ctsu_dtc_info[array_number].data_size = DTC_DATA_SIZE_LWORD; + g_ctsu_dtc_info[array_number].block_size = 5; + g_ctsu_dtc_info[array_number].transfer_count = set_transfer_count; +} + +/*********************************************************************************************************************** + * ctsu_transfer_autojudge_repeat_set + ***********************************************************************************************************************/ +void ctsu_transfer_autojudge_repeat_set (uint8_t array_number, dtc_chain_transfer_t set_chain_transfer_enable, dtc_repeat_block_side_t set_repeat_block_side) +{ + g_ctsu_dtc_info[array_number].chain_transfer_enable = set_chain_transfer_enable; + g_ctsu_dtc_info[array_number].chain_transfer_mode = DTC_CHAIN_TRANSFER_CONTINUOUSLY; + g_ctsu_dtc_info[array_number].src_addr_mode = DTC_SRC_ADDR_FIXED; + g_ctsu_dtc_info[array_number].dest_addr_mode = DTC_DES_ADDR_FIXED; + g_ctsu_dtc_info[array_number].response_interrupt = DTC_INTERRUPT_AFTER_ALL_COMPLETE; + g_ctsu_dtc_info[array_number].transfer_mode = DTC_TRANSFER_MODE_REPEAT; + g_ctsu_dtc_info[array_number].repeat_block_side = set_repeat_block_side; + g_ctsu_dtc_info[array_number].data_size = DTC_DATA_SIZE_LWORD; + g_ctsu_dtc_info[array_number].block_size = 1; + g_ctsu_dtc_info[array_number].transfer_count = 1; +} + #endif + + #if (CTSU_CFG_AUTO_MULTI_CLOCK_CORRECTION_ENABLE == 1) +/*********************************************************************************************************************** + * ctsu_transfer_mcact_set + ***********************************************************************************************************************/ +void ctsu_transfer_mcact_set (uint8_t array_number, uint32_t set_transfer_count) +{ + g_ctsu_dtc_info[array_number].chain_transfer_enable = DTC_CHAIN_TRANSFER_ENABLE; + g_ctsu_dtc_info[array_number].chain_transfer_mode = DTC_CHAIN_TRANSFER_CONTINUOUSLY; + g_ctsu_dtc_info[array_number].src_addr_mode = DTC_SRC_ADDR_INCR; + g_ctsu_dtc_info[array_number].dest_addr_mode = DTC_DES_ADDR_FIXED; + g_ctsu_dtc_info[array_number].response_interrupt = DTC_INTERRUPT_AFTER_ALL_COMPLETE; + g_ctsu_dtc_info[array_number].transfer_mode = DTC_TRANSFER_MODE_BLOCK; + g_ctsu_dtc_info[array_number].repeat_block_side = DTC_REPEAT_BLOCK_DESTINATION; + g_ctsu_dtc_info[array_number].data_size = DTC_DATA_SIZE_LWORD; + g_ctsu_dtc_info[array_number].block_size = 1; + g_ctsu_dtc_info[array_number].transfer_count = set_transfer_count; +} + #endif +#endif + +/*********************************************************************************************************************** + * ctsu_initial_offset_tuning + ***********************************************************************************************************************/ +void ctsu_initial_offset_tuning (ctsu_instance_ctrl_t * const p_instance_ctrl) +{ + uint16_t element_id; + int32_t diff = 0; + uint32_t complete_flag = 0; + uint32_t num_complete = 0; + uint16_t target_val; +#if (BSP_FEATURE_CTSU_VERSION == 1) + uint16_t ctsuso; + ctsu_correction_calc_t calc; +#endif +#if (BSP_FEATURE_CTSU_VERSION == 2) + uint16_t i; + uint16_t element_top; + uint16_t corr_data[CTSU_CFG_NUM_SUMULTI]; + int32_t ctsuso; + uint32_t snum; + int32_t offset_unit; +#endif + + /* element_id through each element for control block */ + for (element_id = 0; element_id < p_instance_ctrl->num_elements; element_id++) + { + if (0 == *(p_instance_ctrl->p_element_complete_flag + element_id)) + { +#if (BSP_FEATURE_CTSU_VERSION == 1) + if (CTSU_MODE_SELF_MULTI_SCAN == p_instance_ctrl->md) + { + target_val = (p_instance_ctrl->tuning_self_target_value); + } + else + { + target_val = (p_instance_ctrl->tuning_mutual_target_value); + } + + calc.snum = (p_instance_ctrl->p_ctsuwr[element_id].ctsuso0 >> 10) & CTSU_SNUM_MAX; + calc.sdpa = (p_instance_ctrl->p_ctsuwr[element_id].ctsuso1 >> 8) & CTSU_SDPA_MAX; + target_val = (uint16_t) (target_val * + (uint32_t) ((calc.snum + 1) * (calc.sdpa + 1)) / + g_ctsu_correction_info.ctsu_clock); + #if (CTSU_CFG_NUM_SELF_ELEMENTS != 0) + if (CTSU_MODE_SELF_MULTI_SCAN == p_instance_ctrl->md) + { + diff = (int32_t)(p_instance_ctrl->p_self_data + element_id)->int_data - (int32_t)target_val; + } + #endif + #if (CTSU_CFG_NUM_MUTUAL_ELEMENTS != 0) + if (CTSU_MODE_MUTUAL_FULL_SCAN == p_instance_ctrl->md) + { + diff = (int32_t)(p_instance_ctrl->p_mutual_pri_data + element_id)->int_data - (int32_t)target_val; + } + #endif + ctsuso = (p_instance_ctrl->p_ctsuwr[element_id].ctsuso0 & CTSU_TUNING_MAX); + if (0 < diff) + { + if (*(p_instance_ctrl->p_tuning_diff + element_id) < 0) + { + if ((-diff) > *(p_instance_ctrl->p_tuning_diff + element_id)) + { + ctsuso++; ///< Decrease count + } + + complete_flag = 1; + } + else + { + if (CTSU_TUNING_MAX == ctsuso) /* CTSUSO limit check */ + { + complete_flag = 1; + } + else + { + ctsuso++; ///< Decrease count + (*(p_instance_ctrl->p_tuning_diff + element_id)) = diff; ///< Plus + } + } + } + else if (0 == diff) + { + complete_flag = 1; + } + else + { + if (*(p_instance_ctrl->p_tuning_diff + element_id) > 0) + { + if ((-diff) > *(p_instance_ctrl->p_tuning_diff + element_id)) + { + ctsuso--; ///< Increase count + } + + complete_flag = 1; + } + else + { + if (CTSU_TUNING_MIN == ctsuso) /* CTSUSO limit check */ + { + complete_flag = 1; + } + else + { + ctsuso--; ///< Increase count + (*(p_instance_ctrl->p_tuning_diff + element_id)) = diff; ///< Minus + } + } + } + + p_instance_ctrl->p_ctsuwr[element_id].ctsuso0 &= (uint16_t) (~CTSU_TUNING_MAX); + p_instance_ctrl->p_ctsuwr[element_id].ctsuso0 |= ctsuso; +#endif +#if (BSP_FEATURE_CTSU_VERSION == 2) + element_top = (uint16_t) (element_id * CTSU_CFG_NUM_SUMULTI); + for (i = 0; i < CTSU_CFG_NUM_SUMULTI; i++) + { + /* Adjust only frequencies for which offset tuning is not completed */ + if (0 == (p_instance_ctrl->p_frequency_complete_flag[element_id] & (1 << i))) + { + if (CTSU_MODE_SELF_MULTI_SCAN == p_instance_ctrl->md) + { + target_val = (p_instance_ctrl->tuning_self_target_value / 2); + } + else + { + target_val = (p_instance_ctrl->tuning_mutual_target_value / 2); + } + + if (CTSU_MODE_SELF_MULTI_SCAN == p_instance_ctrl->md) + { + corr_data[i] = p_instance_ctrl->p_self_corr[element_top + i]; + } + else + { + corr_data[i] = p_instance_ctrl->p_mutual_pri_corr[element_top + i]; + } + + snum = (p_instance_ctrl->p_ctsuwr[(element_id * CTSU_CFG_NUM_SUMULTI)].ctsuso >> 10) & + CTSU_SNUM_MAX; + target_val = (uint16_t) (((uint32_t) target_val * (snum + 1)) / (CTSU_SNUM_RECOMMEND + 1)); + offset_unit = (int32_t) ((CTSU_CORRECTION_OFFSET_UNIT * (snum + 1)) / (CTSU_SNUM_RECOMMEND + 1)); + + /* Calculate CTSUSO equivalent difference between current value and target value */ + diff = (int32_t) ((int32_t) corr_data[i] - (int32_t) target_val) / + (offset_unit >> p_instance_ctrl->range); + + ctsuso = (int32_t) (p_instance_ctrl->p_ctsuwr[element_top + i].ctsuso & CTSU_TUNING_MAX); + ctsuso += diff; + + /* If the CTSUSO exceeds the minimum value or the maximum value, tuning complete */ + if (ctsuso < 0) + { + ctsuso = 0; + p_instance_ctrl->p_frequency_complete_flag[element_id] += (uint8_t) (1 << i); + } + else if (ctsuso > CTSU_TUNING_MAX) + { + ctsuso = CTSU_TUNING_MAX; + p_instance_ctrl->p_frequency_complete_flag[element_id] += (uint8_t) (1 << i); + } + else + { + /* If the difference is large, tuning value may not be able to match, so create the next opportunity */ + if (0 == diff) + { + p_instance_ctrl->p_frequency_complete_flag[element_id] += (uint8_t) (1 << i); + } + } + + /* Set the result of the calculated CTSUSO */ + p_instance_ctrl->p_ctsuwr[element_top + i].ctsuso &= (uint32_t) (~CTSU_TUNING_MAX); + p_instance_ctrl->p_ctsuwr[element_top + i].ctsuso |= (uint32_t) ctsuso; + } + + /* Add completion status for each frequency */ + complete_flag += ((p_instance_ctrl->p_frequency_complete_flag[element_id] >> i) & 1); + } +#endif + } + else + { + complete_flag = CTSU_CFG_NUM_SUMULTI; + } + + if (CTSU_CFG_NUM_SUMULTI == complete_flag) + { + num_complete++; + *(p_instance_ctrl->p_element_complete_flag + element_id) = 1; + } + + complete_flag = 0; + } + + if (num_complete == p_instance_ctrl->num_elements) + { + p_instance_ctrl->tuning = CTSU_TUNING_COMPLETE; + } +} + +/*********************************************************************************************************************** + * ctsu_moving_average + ***********************************************************************************************************************/ +void ctsu_moving_average (ctsu_data_t * p_average, uint16_t new_data, uint16_t average_num) +{ + uint32_t work; + + work = (uint32_t)(((uint32_t) p_average->int_data << CTSU_CFG_DECIMAL_POINT) + p_average->decimal_point_data); + work -= (uint32_t)(work / average_num); + work += (uint32_t)(((uint32_t) new_data << CTSU_CFG_DECIMAL_POINT) / average_num); + + p_average->int_data = (uint16_t)(work >> CTSU_CFG_DECIMAL_POINT); + p_average->decimal_point_data = (uint16_t)(work & 0x0000FFFF); +} + +/*********************************************************************************************************************** + * CTSUWR interrupt handler. This service routine sets the tuning for the next element to be scanned by hardware. + ***********************************************************************************************************************/ +// R_BSP_PRAGMA_INTERRUPT(ctsu_ctsuwr_isr, VECT(CTSU,CTSUWR)) +R_BSP_ATTRIB_INTERRUPT void ctsu_ctsuwr_isr(void); +R_BSP_ATTRIB_INTERRUPT void ctsu_ctsuwr_isr(void) +{ +#if (CTSU_CFG_DTC_SUPPORT_ENABLE == 0) + ctsu_instance_ctrl_t * p_instance_ctrl = gp_ctsu_isr_context; + + /* Write settings for current element */ + if (CTSU_CORRECTION_RUN == g_ctsu_correction_info.status) + { + #if (BSP_FEATURE_CTSU_VERSION == 2) + CTSU.CTSUSO.LONG = g_ctsu_correction_info.ctsuwr.ctsuso; + #endif + #if (BSP_FEATURE_CTSU_VERSION == 1) + CTSU.CTSUSSC.WORD = g_ctsu_correction_info.ctsuwr.ctsussc; + CTSU.CTSUSO0.WORD = g_ctsu_correction_info.ctsuwr.ctsuso0; + CTSU.CTSUSO1.WORD = g_ctsu_correction_info.ctsuwr.ctsuso1; + #endif + } + + #if (BSP_FEATURE_CTSU_VERSION == 2) + #if (CTSU_CFG_NUM_CFC != 0) + else if ((CTSU_CORRECTION_RUN == g_ctsu_corrcfc_info.status)) + { + R_CTSU->CTSUSO = g_ctsu_corrcfc_info.ctsuwr.ctsuso; + } + #endif + #endif + else if (CTSU_MODE_DIAGNOSIS_SCAN == p_instance_ctrl->md) + { + #if (BSP_FEATURE_CTSU_VERSION == 2) + #if (CTSU_CFG_DIAG_SUPPORT_ENABLE == 1) + CTSU.CTSUSO.LONG = g_ctsu_diag_info.ctsuwr.ctsuso; + p_instance_ctrl->wr_index++; + #endif + #endif + #if (BSP_FEATURE_CTSU_VERSION == 1) + #if (CTSU_CFG_DIAG_SUPPORT_ENABLE == 1) + CTSU.CTSUSSC.WORD = g_ctsu_diag_info.ctsuwr.ctsussc; + CTSU.CTSUSO0.WORD = g_ctsu_diag_info.ctsuwr.ctsuso0; + CTSU.CTSUSO1.WORD = g_ctsu_diag_info.ctsuwr.ctsuso1; + #endif + #endif + } + else + { + #if (BSP_FEATURE_CTSU_VERSION == 2) + CTSU.CTSUSO.LONG = p_instance_ctrl->p_ctsuwr[p_instance_ctrl->wr_index].ctsuso; + #endif + #if (BSP_FEATURE_CTSU_VERSION == 1) + CTSU.CTSUSSC.WORD = p_instance_ctrl->p_ctsuwr[p_instance_ctrl->wr_index].ctsussc; + CTSU.CTSUSO0.WORD = p_instance_ctrl->p_ctsuwr[p_instance_ctrl->wr_index].ctsuso0; + CTSU.CTSUSO1.WORD = p_instance_ctrl->p_ctsuwr[p_instance_ctrl->wr_index].ctsuso1; + #endif + p_instance_ctrl->wr_index++; + } +#endif +} + +/*********************************************************************************************************************** + * CTSURD interrupt handler. This service routine reads the sensor count and reference counter for + * the current element and places the value in the scan data buffer. Note that the reference counter + * does not work properly but is saved anyway for backward compatibility and potential future use. + * Additionally, the SC register cannot be read again until RC is read. + ***********************************************************************************************************************/ +// R_BSP_PRAGMA_INTERRUPT(ctsu_ctsurd_isr, VECT(CTSU,CTSURD)) +R_BSP_ATTRIB_INTERRUPT void ctsu_ctsurd_isr(void); +R_BSP_ATTRIB_INTERRUPT void ctsu_ctsurd_isr(void) +{ +#if (CTSU_CFG_DTC_SUPPORT_ENABLE == 0) + ctsu_instance_ctrl_t * p_instance_ctrl = gp_ctsu_isr_context; + + /* read current channel/element value */ + /* Store the reference counter for possible future use. Register must be read or scan will hang. */ + + #if (BSP_FEATURE_CTSU_VERSION == 1) + if (CTSU_CORRECTION_RUN == g_ctsu_correction_info.status) + { + g_ctsu_correction_info.scanbuf.sen = CTSU.CTSUSC.WORD; + g_ctsu_correction_info.scanbuf.ref = CTSU.CTSURC.WORD; + } + + #if (BSP_FEATURE_CTSU_VERSION == 1) + #if (CTSU_CFG_DIAG_SUPPORT_ENABLE == 1) + else if (CTSU_MODE_DIAGNOSIS_SCAN == p_instance_ctrl->md) + { + g_ctsu_diag_info.scanbuf.sen = CTSU.CTSUSC.WORD; + g_ctsu_diag_info.scanbuf.ref = CTSU.CTSURC.WORD; + } + #endif + #endif + else + { + #if (CTSU_CFG_NUM_SELF_ELEMENTS != 0) + if (CTSU_MODE_SELF_MULTI_SCAN == p_instance_ctrl->md) + { + (p_instance_ctrl->p_self_raw + p_instance_ctrl->rd_index)->sen = CTSU.CTSUSC.WORD; + (p_instance_ctrl->p_self_raw + p_instance_ctrl->rd_index)->ref = CTSU.CTSURC.WORD; + p_instance_ctrl->rd_index++; + } + #endif + #if (CTSU_CFG_NUM_MUTUAL_ELEMENTS != 0) + if (CTSU_MODE_MUTUAL_FULL_SCAN == p_instance_ctrl->md) + { + if (false == p_instance_ctrl->serial_tuning_enable) + { + if (1 == CTSU.CTSUST.BIT.CTSUPS) + { + (p_instance_ctrl->p_mutual_raw + p_instance_ctrl->rd_index)->pri_sen = CTSU.CTSUSC.WORD; + (p_instance_ctrl->p_mutual_raw + p_instance_ctrl->rd_index)->pri_ref = CTSU.CTSURC.WORD; + } + else + { + (p_instance_ctrl->p_mutual_raw + p_instance_ctrl->rd_index)->snd_sen = CTSU.CTSUSC.WORD; + (p_instance_ctrl->p_mutual_raw + p_instance_ctrl->rd_index)->snd_ref = CTSU.CTSURC.WORD; + p_instance_ctrl->rd_index++; + } + } + else + { + p_instance_ctrl->serial_tuning_mutual_cnt++; + if (p_instance_ctrl->serial_tuning_mutual_cnt % 2) + { + (p_instance_ctrl->p_mutual_raw + p_instance_ctrl->rd_index)->pri_sen = CTSU.CTSUSC.WORD; + (p_instance_ctrl->p_mutual_raw + p_instance_ctrl->rd_index)->pri_ref = CTSU.CTSURC.WORD; + } + else + { + (p_instance_ctrl->p_mutual_raw + p_instance_ctrl->rd_index)->snd_sen = CTSU.CTSUSC.WORD; + (p_instance_ctrl->p_mutual_raw + p_instance_ctrl->rd_index)->snd_ref = CTSU.CTSURC.WORD; + p_instance_ctrl->rd_index++; + } + } + } + #endif + } + #endif + + #if (BSP_FEATURE_CTSU_VERSION == 2) + #if (CTSU_CFG_NUM_CFC != 0) + uint16_t i; + #endif + if (CTSU_CORRECTION_RUN == g_ctsu_correction_info.status) + { + g_ctsu_correction_info.scanbuf = CTSU.CTSUSCNT.BIT.SC; + } + + #if (CTSU_CFG_NUM_CFC != 0) + else if (CTSU_CORRECTION_RUN == g_ctsu_corrcfc_info.status) + { + for (i = 0; i < g_ctsu_corrcfc_info.num_ts; i++) + { + g_ctsu_corrcfc_info.scanbuf[i] = R_CTSU->CTSUCFCCNT_b.CFCCNT; + } + } + #endif + #if (CTSU_CFG_DIAG_SUPPORT_ENABLE == 1) + else if (CTSU_MODE_DIAGNOSIS_SCAN == p_instance_ctrl->md) + { + if (CTSU_DIAG_CFC == g_ctsu_diag_info.state) + { +/* g_ctsu_diag_info.ctsuscnt[p_instance_ctrl->rd_index] = R_CTSU->CTSUCFCCNT; */ + p_instance_ctrl->rd_index++; + } + else + { + g_ctsu_diag_info.ctsuscnt[p_instance_ctrl->rd_index] = CTSU.CTSUSCNT.LONG; + p_instance_ctrl->rd_index++; + } + } + #endif + else + { + #if (CTSU_CFG_NUM_SELF_ELEMENTS != 0) + if (CTSU_MODE_SELF_MULTI_SCAN == (CTSU_MODE_MUTUAL_FULL_SCAN & p_instance_ctrl->md)) + { + p_instance_ctrl->p_self_raw[p_instance_ctrl->rd_index] = CTSU.CTSUSCNT.BIT.SC; + p_instance_ctrl->rd_index++; + + /* Interim */ + if (0 == p_instance_ctrl->rd_index % 3) + { + CTSU.CTSUSR.BIT.MFC = 0; + } + } + #endif + #if (CTSU_CFG_NUM_MUTUAL_ELEMENTS != 0) + if (CTSU_MODE_MUTUAL_FULL_SCAN == p_instance_ctrl->md) + { + p_instance_ctrl->p_mutual_raw[p_instance_ctrl->rd_index] = CTSU.CTSUSCNT.BIT.SC; + p_instance_ctrl->rd_index++; + } + + #if (CTSU_CFG_NUM_CFC != 0) + if (CTSU_MODE_MUTUAL_CFC_SCAN == p_instance_ctrl->md) + { + for (i = 0; i < p_instance_ctrl->p_ctsu_cfg->num_rx; i++) + { + p_instance_ctrl->p_mutual_raw[p_instance_ctrl->rd_index] = R_CTSU->CTSUCFCCNT_b.CFCCNT; + p_instance_ctrl->rd_index++; + } + } + #endif + #endif + } + #endif + if (1 == p_instance_ctrl->interrupt_reverse_flag) + { + p_instance_ctrl->interrupt_reverse_flag = 0; + + ctsu_end_interrupt(p_instance_ctrl); + } +#endif +} + +/*********************************************************************************************************************** + * CTSUFN interrupt handler. This service routine occurs when all elements have been scanned (finished). + * The user's callback function is called if available. + ***********************************************************************************************************************/ +// R_BSP_PRAGMA_INTERRUPT(ctsu_ctsuend_isr, VECT(CTSU,CTSUFN)) +R_BSP_ATTRIB_INTERRUPT void ctsu_ctsuend_isr(void); +R_BSP_ATTRIB_INTERRUPT void ctsu_ctsuend_isr(void) +{ + ctsu_instance_ctrl_t * p_instance_ctrl = gp_ctsu_isr_context; + uint16_t rd_index; + + /* In CTSU1 and CTSU2 self-capacity and current measurement mode and diagnostic mode, */ + /* rd_index has the same value as wr_index. */ + rd_index = p_instance_ctrl->rd_index; + +#if (BSP_FEATURE_CTSU_VERSION == 1) + if (CTSU_MODE_MUTUAL_FULL_SCAN == p_instance_ctrl->md) + { + if (true == p_instance_ctrl->serial_tuning_enable) + { + if (0 == ((p_instance_ctrl->ctsucr1 >> 7) & 0x01)) + { + rd_index = (uint16_t) (p_instance_ctrl->serial_tuning_mutual_cnt); + } + } + } +#endif + +#if (BSP_FEATURE_CTSU_VERSION == 2) + #if (CTSU_CFG_NUM_MUTUAL_ELEMENTS != 0) + if (CTSU_MODE_MUTUAL_FULL_SCAN == p_instance_ctrl->md) + { + if (true == p_instance_ctrl->serial_tuning_enable) + { + if (0 == ((p_instance_ctrl->ctsucr1 >> 7) & 0x01)) + { + rd_index = p_instance_ctrl->rd_index; + } + else + { + rd_index = p_instance_ctrl->rd_index / 2; + } + } + else + { + /* In the mutual capacity of CTSU2, the value of rd_index is twice the value of wr_index. */ + rd_index = p_instance_ctrl->rd_index / 2; + } + } + + #if (CTSU_CFG_NUM_CFC != 0) + if (CTSU_MODE_MUTUAL_CFC_SCAN == p_instance_ctrl->md) + { + if (true == p_instance_ctrl->serial_tuning_enable) + { + if (0 == ((p_instance_ctrl->ctsucr1 >> 7) & 0x01)) + { + rd_index = p_instance_ctrl->rd_index; + } + else + { + rd_index = (uint16_t) (p_instance_ctrl->rd_index / (p_instance_ctrl->p_ctsu_cfg->num_rx * 2)); + } + } + else + { + /* In the CFC mutual capacity of CTSU2, rd_index is twice the number of RX terminals in each wr_index. */ + rd_index = (uint16_t) (p_instance_ctrl->rd_index / (p_instance_ctrl->p_ctsu_cfg->num_rx * 2)); + } + } + #endif + #endif +#endif + + /* Countermeasure for the problem that RD interrupt and FN interrupt are reversed. */ + if (rd_index != p_instance_ctrl->wr_index) + { + p_instance_ctrl->interrupt_reverse_flag = 1; + + return; + } + + ctsu_end_interrupt(p_instance_ctrl); +} + +void ctsu_end_interrupt (ctsu_instance_ctrl_t * const p_instance_ctrl) +{ + ctsu_callback_args_t args; + + /* Store callback arguments in memory provided by user if available. This allows callback arguments to be + * stored in non-secure memory so they can be accessed by a non-secure callback function. */ + ctsu_callback_args_t * p_args = p_instance_ctrl->p_callback_memory; + if (NULL == p_args) + { + /* Store on stack */ + p_args = &args; + } + else + { + /* Save current arguments on the stack in case this is a nested interrupt. */ + args = *p_args; + } + +#if (BSP_FEATURE_CTSU_VERSION == 1) + #if (CTSU_CFG_DIAG_SUPPORT_ENABLE == 1) + if (CTSU_DIAG_OVER_VOLTAGE == g_ctsu_diag_info.state) + { + if (CTSU.CTSUERRS.BIT.CTSUICOMP == 1) + { + g_ctsu_diag_info.icomp = 1; + } + } + #endif +#endif + +#if (BSP_FEATURE_CTSU_VERSION == 2) + #if (CTSU_CFG_DIAG_SUPPORT_ENABLE == 1) + if (CTSU_MODE_DIAGNOSIS_SCAN == p_instance_ctrl->md) + { + if (CTSU_DIAG_OVER_CURRENT == g_ctsu_diag_info.state) + { + if (CTSU.CTSUSR.BIT.ICOMP1 == 1) + { + g_ctsu_diag_info.icomp1_value = CTSU.CTSUSR.BIT.ICOMP1; + } + } + } + #endif +#endif + + p_args->event = CTSU_EVENT_SCAN_COMPLETE; + +#if (BSP_FEATURE_CTSU_VERSION == 2) + if (CTSU.CTSUSR.BIT.SCOVF) + { + p_args->event |= CTSU_EVENT_OVERFLOW; + CTSU.CTSUSR.BIT.SCOVF = 0; + } + + if ((1 == CTSU.CTSUSR.BIT.ICOMP0) || (1 == CTSU.CTSUSR.BIT.ICOMP1)) + { + if (CTSU.CTSUSR.BIT.ICOMP0) + { + p_args->event |= CTSU_EVENT_ICOMP; + } + if (CTSU.CTSUSR.BIT.ICOMP1) + { + p_args->event |= CTSU_EVENT_ICOMP1; + } + + CTSU.CTSUSR.BIT.ICOMPRST = 1; + } +#endif +#if (BSP_FEATURE_CTSU_VERSION == 1) + + if (CTSU.CTSUST.BIT.CTSUSOVF) + { + p_args->event |= CTSU_EVENT_OVERFLOW; + CTSU.CTSUST.BIT.CTSUSOVF = 0; + } + + if (CTSU.CTSUERRS.BIT.CTSUICOMP == 1) + { + CTSU.CTSUCR1.BYTE &= (uint8_t) (~0x01); + R_BSP_NOP(); + R_BSP_NOP(); + CTSU.CTSUCR1.BYTE |= 0x01; + p_args->event |= CTSU_EVENT_ICOMP; + } +#endif +#if (BSP_FEATURE_CTSU_VERSION == 2) + #if (CTSU_CFG_TEMP_CORRECTION_SUPPORT == 1) + if (CTSU_CORRECTION_RUN != g_ctsu_correction_info.status) + { + if (CTSU_MODE_CORRECTION_SCAN == p_instance_ctrl->md) + { + CTSU.CTSUCRA.BIT.SDPSEL = 0; + CTSU.CTSUSUCLKA.BIT.SUADJ0 = g_ctsu_correction_info.suadj0; + CTSU.CTSUSUCLKA.BIT.SUMULTI0 = CTSU_CFG_SUMULTI0; + CTSU.CTSUCRB.BIT.SSCNT = 1; + CTSU.CTSUCALIB.BIT.SUCARRY = 0; + CTSU.CTSUCALIB.BIT.CCOCALIB = 0; + CTSU.CTSUCALIB.BIT.CCOCLK = 1; + CTSU.CTSUCALIB.BIT.TSOC = 0; + CTSU.CTSUCRA.BIT.SDPSEL = 1; + } + } + #endif +#endif + +#if (CTSU_CFG_MULTIPLE_ELECTRODE_CONNECTION_ENABLE == 1) + if ((1 == p_instance_ctrl->tsod) && (CTSU_CAP_EXTERNAL == p_instance_ctrl->cap)) + { + /* When using MEC, MD0 bit is set to single scan mode. */ + CTSU.CTSUMCH.BIT.MCH0 = p_instance_ctrl->mec_ts; + + /* Set MCH1 when using both MEC and Active Shield.*/ + if((0 != p_instance_ctrl->ctsuchtrc0) + || (0 != p_instance_ctrl->ctsuchtrc1) + || (0 != p_instance_ctrl->ctsuchtrc2) + || (0 != p_instance_ctrl->ctsuchtrc3) + || (0 != p_instance_ctrl->ctsuchtrc4)) + { + CTSU.CTSUMCH.BIT.MCH1 = p_instance_ctrl->mec_shield_ts; + } + } +#endif + + p_instance_ctrl->state = CTSU_STATE_SCANNED; + p_instance_ctrl->error_status = args.event; + p_args->p_context = p_instance_ctrl->p_context; + + /* If a callback was provided, call it with the argument */ + if (NULL != p_instance_ctrl->p_callback) + { +#if BSP_TZ_SECURE_BUILD + + /* p_callback can point to a secure function or a non-secure function. */ + if (!cmse_is_nsfptr(p_instance_ctrl->p_callback)) + { + /* If p_callback is secure, then the project does not need to change security state. */ + p_instance_ctrl->p_callback(p_args); + } + else + { + /* If p_callback is Non-secure, then the project must change to Non-secure state in order to call the callback. */ + ctsu_prv_ns_callback p_callback = (ctsu_prv_ns_callback) (p_instance_ctrl->p_callback); + p_callback(p_args); + } + +#else + + /* If the project is not Trustzone Secure, then it will never need to change security state in order to call the callback. */ + p_instance_ctrl->p_callback(p_args); +#endif + } + + if (NULL != p_instance_ctrl->p_callback_memory) + { + /* Restore callback memory in case this is a nested interrupt. */ + *p_instance_ctrl->p_callback_memory = args; + } + + /* reset indexes */ + p_instance_ctrl->wr_index = 0; + p_instance_ctrl->rd_index = 0; +#if (CTSU_CFG_NUM_MUTUAL_ELEMENTS != 0) + p_instance_ctrl->serial_tuning_mutual_cnt = 0; +#endif +} + +/*********************************************************************************************************************** + * ctsu_correction_process + ***********************************************************************************************************************/ +void ctsu_correction_process (ctsu_instance_ctrl_t * const p_instance_ctrl) +{ +#if (BSP_FEATURE_CTSU_VERSION == 2) + uint32_t i; + uint32_t j; + uint32_t trimb; + uint8_t rtrim; + uint16_t x1; + uint16_t x0; + uint16_t y0; + uint16_t y1; + uint16_t dac2vdc; + uint8_t trimb_byte; + uint16_t dac_value; + uint32_t ref_value; + uint16_t c0; + uint16_t c1; + + g_ctsu_correction_info.status = CTSU_CORRECTION_RUN; + + CTSU.CTSUCRA.BIT.MD0 = 1; + CTSU.CTSUCRA.BIT.MD1 = 0; + CTSU.CTSUMCH.BIT.MCA0 = 1; + CTSU.CTSUMCH.BIT.MCA1 = 0; + CTSU.CTSUMCH.BIT.MCA2 = 0; + CTSU.CTSUMCH.BIT.MCA3 = 0; + CTSU.CTSUCRA.BIT.LOAD = 1; + + /* Setting time of measurement */ + g_ctsu_correction_info.ctsuwr.ctsuso = (CTSU_SNUM_RECOMMEND << 10); + + /* Measure the current input to the ICO by passing current through the internal resistance in each range. */ + /* The theoretical value of the current is 12.5uA. */ + CTSU.CTSUCHACA.LONG = 1; + CTSU.CTSUCHACB.LONG = 0; + CTSU.CTSUCALIB.BIT.TSOC = 1; + for (i = 0; i < CTSU_RANGE_NUM; i++) + { + if (CTSU_RANGE_20UA == i) + { + CTSU.CTSUCRA.BIT.ATUNE1 = 0; + CTSU.CTSUCRA.BIT.ATUNE2 = 1; + } + else if (CTSU_RANGE_40UA == i) + { + CTSU.CTSUCRA.BIT.ATUNE1 = 1; + CTSU.CTSUCRA.BIT.ATUNE2 = 0; + } + else if (CTSU_RANGE_80UA == i) + { + CTSU.CTSUCRA.BIT.ATUNE1 = 0; + CTSU.CTSUCRA.BIT.ATUNE2 = 0; + } + else + { + CTSU.CTSUCRA.BIT.ATUNE1 = 1; + CTSU.CTSUCRA.BIT.ATUNE2 = 1; + } + + CTSU.CTSUCRA.BIT.LOAD = 3; + ctsu_correction_measurement(p_instance_ctrl, &g_ctsu_correction_info.base_value[i]); + CTSU.CTSUCRA.BIT.LOAD = 1; + } + + /* Measure by inputting each constant current from internal DAC to ICO. */ + CTSU.CTSUCRB.BIT.SSCNT = 0; + CTSU.CTSUCALIB.BIT.CCOCLK = 0; + CTSU.CTSUCALIB.BIT.CCOCALIB = 1; + + /* 2.5uA * (j + 1), SUCARRY and SSCNT = 3 are required for greater than 20uA */ + for (j = 0; j < CTSU_CORRECTION_POINT_NUM; j++) + { + CTSU.CTSUCRA.BIT.SDPSEL = 0; + if (8 > j) + { + CTSU.CTSUSUCLKA.BIT.SUADJ0 = (uint8_t) (((j + 1) * CTSU_CORRECTION_SUMULTI) - 1); + } + else + { + CTSU.CTSUCRB.BIT.SSCNT = 3; + CTSU.CTSUCALIB.BIT.SUCARRY = 1; + CTSU.CTSUSUCLKA.BIT.SUADJ0 = (uint8_t) (((j - 3) * CTSU_CORRECTION_SUMULTI) - 1); + } + + CTSU.CTSUCRA.BIT.SDPSEL = 1; + + // store CCODAC path value to tha last array of dac_value + ctsu_correction_measurement(p_instance_ctrl, &(g_ctsu_correction_info.dac_value[CTSU_RANGE_NUM - 1][j])); + } + + /* Calculate the coefficient between step2 and step3 in each range. */ + trimb = CTSU.CTSUTRIMB.LONG; + rtrim = CTSU.CTSUTRIMA.BIT.RTRIM; + + for (i = 0; i < CTSU_RANGE_NUM; i++) + { + /* Get resistance value from TRIMB register. */ + /* Error rate calculation on VCD path as UQ1.9 */ + trimb_byte = (uint8_t) ((trimb >> ((CTSU_RANGE_NUM - 1 - i) * 8)) & CTSU_CORRECTION_TRIMB_MAX); + + switch (trimb_byte & CTSU_CORRECTION_TRIMB_THRESHOLD1) + { + case CTSU_CORRECTION_TRIMB_THRESHOLD1: + { + if (rtrim > CTSU_CORRECTION_RTRIM_THRESHOLD1) + { + x1 = CTSU_CORRECTION_BIT8; /* 0.05 */ + } + else + { + x1 = CTSU_CORRECTION_BIT9; /* 1.00 */ + } + + break; + } + + case 0x00: + { + if (rtrim < CTSU_CORRECTION_RTRIM_THRESHOLD2) + { + x1 = (CTSU_CORRECTION_BIT7 | CTSU_CORRECTION_BIT8); /* 0.25 and 0.50 */ + } + else + { + x1 = (CTSU_CORRECTION_BIT7 | CTSU_CORRECTION_BIT9); /* 0.25 and 1.00 */ + } + + break; + } + + case CTSU_CORRECTION_TRIMB_SIGN_BIT: + { + x1 = CTSU_CORRECTION_BIT9; /* 1.00 */ + break; + } + + default: /* 0x40 */ + { + x1 = (CTSU_CORRECTION_BIT7 | CTSU_CORRECTION_BIT8); /* 0.25 and 0.50 */ + break; + } + } + + g_ctsu_correction_info.error_rate[i] = x1 + (trimb_byte & CTSU_CORRECTION_BIT6_0); + + #if (CTSU_CFG_DIAG_SUPPORT_ENABLE == 1) + + /* Store error rate (6bit left shift) for diagnosis feature */ + g_ctsu_diag_info.error_registance[i] = (uint32_t) (g_ctsu_correction_info.error_rate[i] << 6); + #endif + + // searching for upper index of CCODAC path value containing VDC path value + // Possibility of base_value > g_ctsu_correction_info.dac_value[][CTSU_CORRECTION_POINT_NUM - 1] + + // Theoretical value at VDC path measurement + x1 = (uint16_t) (((uint32_t) CTSU_CORRECTION_STD_VAL << 9) / g_ctsu_correction_info.error_rate[i]); + + j = x1 / CTSU_CORRECTION_STD_UNIT; // upper index of theoretical value + + x0 = (uint16_t) (CTSU_CORRECTION_STD_UNIT * j); // lower side Theoretical value + y0 = g_ctsu_correction_info.dac_value[CTSU_RANGE_NUM - 1][j - 1]; + y1 = g_ctsu_correction_info.dac_value[CTSU_RANGE_NUM - 1][j]; + + // Calculate CCODAC path value on VDC path current by linear interpolation + dac2vdc = (uint16_t) (y0 + ((uint32_t) (y1 - y0) * (x1 - x0)) / CTSU_CORRECTION_STD_UNIT); + + // calculate VDC path conversion ratio + dac2vdc = + (uint16_t) (((int32_t) g_ctsu_correction_info.base_value[i] << CTSU_CORRECTION_DIV_PRECISION) / dac2vdc); + + // Correct CCODAC path value using conversion ratio, and make correction coefficients + for (j = 0; j < CTSU_CORRECTION_POINT_NUM; j++) + { + dac_value = + (uint16_t) (((uint32_t) g_ctsu_correction_info.dac_value[CTSU_RANGE_NUM - 1][j] * dac2vdc) >> + CTSU_CORRECTION_DIV_PRECISION); + ref_value = (CTSU_CORRECTION_STD_UNIT * (j + 1)); + + g_ctsu_correction_info.dac_value[i][j] = dac_value; + g_ctsu_correction_info.coef[i][j] = + (uint16_t) ((ref_value << CTSU_CORRECTION_DIV_PRECISION) / dac_value); + } + + // expand correction range + c0 = g_ctsu_correction_info.coef[i][0]; + c1 = g_ctsu_correction_info.coef[i][1]; + x0 = g_ctsu_correction_info.dac_value[i][0]; + x1 = g_ctsu_correction_info.dac_value[i][1]; + + g_ctsu_correction_info.coef[i][0] = (uint16_t) (c1 - ((uint32_t) (c1 - c0) * x1) / (x1 - x0)); + g_ctsu_correction_info.dac_value[i][0] = 0; + + c0 = g_ctsu_correction_info.coef[i][10]; + c1 = g_ctsu_correction_info.coef[i][11]; + x0 = g_ctsu_correction_info.dac_value[i][10]; + x1 = g_ctsu_correction_info.dac_value[i][11]; + + g_ctsu_correction_info.coef[i][11] = + (uint16_t) (c1 + ((uint32_t) (c1 - c0) * (CTSU_COUNT_MAX - x1)) / (x1 - x0)); + g_ctsu_correction_info.dac_value[i][11] = CTSU_COUNT_MAX; + } + + #if (CTSU_CFG_TEMP_CORRECTION_SUPPORT == 1) + for (i = 0; i < (CTSU_RANGE_NUM - 1); i++) + { + g_ctsu_correction_info.range_ratio[i] = + (uint16_t) (((uint32_t) g_ctsu_correction_info.error_rate[i] << CTSU_SHIFT_AMOUNT) / + g_ctsu_correction_info.error_rate[CTSU_RANGE_160UA]); + } + #endif + g_ctsu_correction_info.status = CTSU_CORRECTION_COMPLETE; +#endif +#if (BSP_FEATURE_CTSU_VERSION == 1) + uint16_t second_std_val; + uint32_t ctsu_sdpa; + uint32_t pclkb_mhz; + + #if BSP_FEATURE_CTSU_HAS_TRMR + uint8_t ctsutrimr_def; + #endif + + g_ctsu_correction_info.status = CTSU_CORRECTION_RUN; + + pclkb_mhz = BSP_PCLKB_HZ / CTSU_PCLKB_FREQ_MHZ; + if (CTSU_PCLKB_FREQ_RANGE1 >= pclkb_mhz) + { + CTSU.CTSUCR1.BYTE |= (0 << 4); + ctsu_sdpa = pclkb_mhz - 1; + } + else if ((CTSU_PCLKB_FREQ_RANGE1 < pclkb_mhz) && (CTSU_PCLKB_FREQ_RANGE2 >= pclkb_mhz)) + { + CTSU.CTSUCR1.BYTE |= (1 << 4); + ctsu_sdpa = (pclkb_mhz / 2) - 1; + } + else + { + CTSU.CTSUCR1.BYTE |= (2 << 4); + ctsu_sdpa = (pclkb_mhz / 4) - 1; + } + + g_ctsu_correction_info.ctsu_clock = pclkb_mhz >> CTSU_CFG_PCLK_DIVISION; + + CTSU.CTSUCR1.BYTE |= (CTSU_MODE_SELF_MULTI_SCAN << 6); + CTSU.CTSUCHAC0.BYTE = 0x01; + + g_ctsu_correction_info.ctsuwr.ctsussc = (CTSU_SSDIV_0500 << 8); + g_ctsu_correction_info.ctsuwr.ctsuso0 = 0x0000; + + /* Set CTSUSO1 */ + g_ctsu_correction_info.ctsuwr.ctsuso1 = (uint16_t) ((CTSU_ICOG_66 << 13) | (ctsu_sdpa << 8) | CTSU_RICOA_RECOMMEND); + + /* Correction measurement setting */ + CTSU.CTSUERRS.BIT.CTSUSPMD = 2; + CTSU.CTSUERRS.BIT.CTSUTSOC = 1; + R_BSP_SoftwareDelay(CTSU_WAIT_TIME, BSP_DELAY_MICROSECS); + + /* First value measurement */ + ctsu_correction_measurement(p_instance_ctrl, &g_ctsu_correction_info.first_val); + + /* Second standard value create */ + #if BSP_FEATURE_CTSU_HAS_TRMR + uint32_t work; + + /* Current trimming value storage */ + ctsutrimr_def = CTSU.CTSUTRMR; + + /* ctsutrimr_def + 273 ((ctsutrimr_def + 273) * 2560 * 128) */ + /* second_std_val = ------------------- * 40960 = ------------------------------------ + 64 */ + /* 528 33 * 128 */ + + work = (ctsutrimr_def + 273) * 9930 + 64; + second_std_val = (uint16_t) (work >> 7); + + /* 0xFF set in the current trimming register */ + CTSU.CTSUTRMR = 0xFF; + #else + second_std_val = (uint16_t) (CTSU_CORRECTION_2ND_STD_VAL * CTSU_WAFER_PARAMETER); + g_ctsu_correction_info.ctsuwr.ctsuso1 |= (uint16_t) (CTSU_ICOG_40 << 13); /* ICO gain 66% -> 40% */ + #endif + + /* Second value measurement */ + ctsu_correction_measurement(p_instance_ctrl, &g_ctsu_correction_info.second_val); + #if BSP_FEATURE_CTSU_HAS_TRMR + + /* Return the current trimming register to the initial value */ + CTSU.CTSUTRMR = ctsutrimr_def; + #endif + + /* Normal measurement setting */ + CTSU.CTSUERRS.BIT.CTSUTSOC = 0; + CTSU.CTSUERRS.BIT.CTSUSPMD = 0; + + R_BSP_SoftwareDelay(CTSU_WAIT_TIME, BSP_DELAY_MICROSECS); + + if ((0 != g_ctsu_correction_info.first_val) && (0 != g_ctsu_correction_info.second_val)) + { + if (g_ctsu_correction_info.second_val < g_ctsu_correction_info.first_val) + { + /* 1st coefficient create */ + g_ctsu_correction_info.first_coefficient = (CTSU_CORRECTION_1ST_STD_VAL << CTSU_SHIFT_AMOUNT) / + g_ctsu_correction_info.first_val; + + /* 2nd coefficient create */ + g_ctsu_correction_info.second_coefficient = ((uint32_t) second_std_val << CTSU_SHIFT_AMOUNT) / + g_ctsu_correction_info.second_val; + + g_ctsu_correction_info.status = CTSU_CORRECTION_COMPLETE; + } + else + { + g_ctsu_correction_info.status = CTSU_CORRECTION_ERROR; + } + } + else + { + g_ctsu_correction_info.status = CTSU_CORRECTION_ERROR; + } +#endif +} + +/*********************************************************************************************************************** + * ctsu_correction_measurement + ***********************************************************************************************************************/ +void ctsu_correction_measurement (ctsu_instance_ctrl_t * const p_instance_ctrl, uint16_t * data) +{ + uint16_t i; + uint32_t sum = 0; + + for (i = 0; i < CTSU_CORRECTION_AVERAGE; i++) + { +#if (CTSU_CFG_DTC_SUPPORT_ENABLE == 1) + ctsu_transfer_configure(p_instance_ctrl); +#else + FSP_PARAMETER_NOT_USED(p_instance_ctrl); +#endif + p_instance_ctrl->state = CTSU_STATE_SCANNING; +#if (BSP_FEATURE_CTSU_VERSION == 2) + CTSU.CTSUCRA.LONG |= 0x01; + while (p_instance_ctrl->state != CTSU_STATE_SCANNED) + { + } + + sum += g_ctsu_correction_info.scanbuf; +#endif +#if (BSP_FEATURE_CTSU_VERSION == 1) + CTSU.CTSUCR0.BYTE |= 0x01; + while (p_instance_ctrl->state != CTSU_STATE_SCANNED) + { + } + sum += g_ctsu_correction_info.scanbuf.sen; +#endif + } + + *data = (uint16_t) (sum / CTSU_CORRECTION_AVERAGE); +} + +#if (BSP_FEATURE_CTSU_VERSION == 2) + #if (CTSU_CFG_TEMP_CORRECTION_SUPPORT == 1) + +/*********************************************************************************************************************** + * ctsu_correction_scan_start + ***********************************************************************************************************************/ +void ctsu_correction_scan_start (void) +{ + g_ctsu_temp_reg_ctsucra = CTSU.CTSUCRA.LONG; + + CTSU.CTSUCRA.BIT.MD0 = 1; + CTSU.CTSUCRA.BIT.MD1 = 0; + CTSU.CTSUMCH.BIT.MCA0 = 1; + CTSU.CTSUMCH.BIT.MCA1 = 0; + CTSU.CTSUMCH.BIT.MCA2 = 0; + CTSU.CTSUMCH.BIT.MCA3 = 0; + + /* Setting time of measurement */ + g_ctsu_correction_info.ctsuwr.ctsuso = (CTSU_SNUM_RECOMMEND << 10); + g_ctsu_correction_info.suadj0 = (uint8_t) (CTSU.CTSUSUCLKA.BIT.SUADJ0 & CTSU_SUADJ_MAX); + + if (g_ctsu_correction_info.scan_index < CTSU_CORRECTION_POINT_NUM) + { + /* Dummy setting */ + CTSU.CTSUCHACA.LONG = 1; + CTSU.CTSUCHACB.LONG = 0; + CTSU.CTSUCHTRCA.LONG = 0; + CTSU.CTSUCHTRCB.LONG = 0; + + /* Step3 : Measure by inputting each constant current from internal DAC to ICO. */ + CTSU.CTSUCRB.BIT.SSCNT = 0; + CTSU.CTSUCALIB.BIT.CCOCLK = 0; + CTSU.CTSUCALIB.BIT.CCOCALIB = 1; + CTSU.CTSUCALIB.BIT.TSOC = 1; + + CTSU.CTSUCRA.BIT.SDPSEL = 0; + if (8 > g_ctsu_correction_info.scan_index) + { + CTSU.CTSUSUCLKA.BIT.SUADJ0 = (uint16_t) (((g_ctsu_correction_info.scan_index + 1) * CTSU_CORRECTION_SUMULTI) - 1); + } + else + { + /* SUCARRY is required for greater than 10uA */ + CTSU.CTSUCRB.BIT.SSCNT = 3; + CTSU.CTSUCALIB.BIT.SUCARRY = 1; + CTSU.CTSUSUCLKA.BIT.SUADJ0 = + (uint16_t) (((g_ctsu_correction_info.scan_index - 3) * CTSU_CORRECTION_SUMULTI) - 1); + } + + CTSU.CTSUCRA.BIT.SDPSEL = 1; + } + else + { + /* Step2-b : Measure the current input to the ICO by passing current through the external resistance. */ + /* The theoretical value of the current is 9.375uA. */ + CTSU.CTSUCRA.BIT.ATUNE1 = 1; + CTSU.CTSUCRA.BIT.ATUNE2 = 1; + #if (CTSU_CFG_TEMP_CORRECTION_TS < 32) + CTSU.CTSUCHACA.LONG = (uint32_t) (1 << CTSU_CFG_TEMP_CORRECTION_TS); + CTSU.CTSUCHACB.LONG = 0; + #else + CTSU.CTSUCHACA.LONG = 0; + CTSU.CTSUCHACB.LONG = (uint32_t) (1 << (CTSU_CFG_TEMP_CORRECTION_TS - 32)); + #endif + CTSU.CTSUCRA.BIT.DCMODE = 1; + CTSU.CTSUCRA.BIT.DCBACK = 1; + CTSU.CTSUSO.BIT.SO = 0; + } +} + +/*********************************************************************************************************************** + * ctsu_correction_data_get + ***********************************************************************************************************************/ +fsp_err_t ctsu_correction_data_get (ctsu_instance_ctrl_t * const p_instance_ctrl, uint16_t * p_data) +{ + uint32_t i; + uint32_t j; + uint16_t x0; + uint16_t x1; + uint16_t y0; + uint16_t y1; + uint16_t dac2vdc; + uint16_t dac_value; + uint32_t ref_value; + uint16_t c0; + uint16_t c1; + ctsu_data_t temp_avg_data = {0, 0}; + fsp_err_t err = FSP_SUCCESS; + + if (g_ctsu_correction_info.scan_index < CTSU_CORRECTION_POINT_NUM) + { + temp_avg_data.int_data = g_ctsu_correction_info.dac_value[CTSU_RANGE_160UA][g_ctsu_correction_info.scan_index]; + ctsu_moving_average(&temp_avg_data, *p_instance_ctrl->p_self_raw, 4); + g_ctsu_correction_info.dac_value[CTSU_RANGE_160UA][g_ctsu_correction_info.scan_index] = temp_avg_data.int_data; + g_ctsu_correction_info.scan_index++; + } + else + { + if (0 == g_ctsu_correction_info.ex_base_value) + { + g_ctsu_correction_info.ex_base_value = *p_instance_ctrl->p_self_raw; + g_ctsu_correction_info.update_counter = CTSU_CFG_TEMP_CORRECTION_TIME; + } + else + { + temp_avg_data.int_data = g_ctsu_correction_info.ex_base_value; + ctsu_moving_average(&temp_avg_data, *p_instance_ctrl->p_self_raw, 4); + g_ctsu_correction_info.ex_base_value = temp_avg_data.int_data; + } + + g_ctsu_correction_info.scan_index = 0; + g_ctsu_correction_info.update_counter++; + } + + /* Step4 : Calculate the coefficient between step2 and step3. */ + if (g_ctsu_correction_info.update_counter > CTSU_CFG_TEMP_CORRECTION_TIME) + { + // Theoretical value at VDC path measurement + x1 = + (uint16_t) (((uint32_t) CTSU_CORRECTION_STD_VAL << 9) / + g_ctsu_correction_info.error_rate[CTSU_RANGE_160UA]); + + j = x1 / CTSU_CORRECTION_STD_UNIT; // upper index of theoretical value + + x0 = (uint16_t) (CTSU_CORRECTION_STD_UNIT * j); // lower side Theoretical value + y0 = g_ctsu_correction_info.dac_value[CTSU_RANGE_160UA][j - 1]; + y1 = g_ctsu_correction_info.dac_value[CTSU_RANGE_160UA][j]; + + // Calculate CCODAC path value on VDC path current by linear interpolation + dac2vdc = (uint16_t) (y0 + ((uint32_t) (y1 - y0) * (x1 - x0)) / CTSU_CORRECTION_STD_UNIT); + + // calculate VDC path conversion ratio + dac2vdc = + (uint16_t) (((int32_t) g_ctsu_correction_info.base_value[CTSU_RANGE_160UA] << + CTSU_CORRECTION_DIV_PRECISION) / + dac2vdc); + + // Correct CCODAC path value using conversion ratio, and make correction coefficients + for (j = 0; j < CTSU_CORRECTION_POINT_NUM; j++) + { + dac_value = + (uint16_t) ((uint32_t) (g_ctsu_correction_info.dac_value[CTSU_RANGE_160UA][j] * dac2vdc) >> + CTSU_CORRECTION_DIV_PRECISION); + ref_value = (CTSU_CORRECTION_STD_UNIT * (j + 1)); + + g_ctsu_correction_info.dac_value[CTSU_RANGE_160UA][j] = dac_value; + g_ctsu_correction_info.coef[CTSU_RANGE_160UA][j] = + (uint16_t) ((ref_value << CTSU_CORRECTION_DIV_PRECISION) / dac_value); + } + + // expand correction range + c0 = g_ctsu_correction_info.coef[CTSU_RANGE_160UA][0]; + c1 = g_ctsu_correction_info.coef[CTSU_RANGE_160UA][1]; + x0 = g_ctsu_correction_info.dac_value[CTSU_RANGE_160UA][0]; + x1 = g_ctsu_correction_info.dac_value[CTSU_RANGE_160UA][1]; + + g_ctsu_correction_info.coef[CTSU_RANGE_160UA][0] = + (uint16_t) (c1 - ((uint32_t) (c1 - c0) * x1) / (x1 - x0)); + g_ctsu_correction_info.dac_value[CTSU_RANGE_160UA][0] = 0; + + c0 = g_ctsu_correction_info.coef[CTSU_RANGE_160UA][10]; + c1 = g_ctsu_correction_info.coef[CTSU_RANGE_160UA][11]; + x0 = g_ctsu_correction_info.dac_value[CTSU_RANGE_160UA][10]; + x1 = g_ctsu_correction_info.dac_value[CTSU_RANGE_160UA][11]; + + g_ctsu_correction_info.coef[CTSU_RANGE_160UA][11] = + (uint16_t) (c1 + ((uint32_t) (c1 - c0) * (CTSU_COUNT_MAX - x1)) / (x1 - x0)); + g_ctsu_correction_info.dac_value[CTSU_RANGE_160UA][11] = CTSU_COUNT_MAX; + + for (j = 1; j < (CTSU_CORRECTION_POINT_NUM - 1); j++) + { + for (i = 0; i < (CTSU_RANGE_NUM - 1); i++) + { + g_ctsu_correction_info.dac_value[i][j] = + (uint16_t) (((uint32_t) g_ctsu_correction_info.dac_value[CTSU_RANGE_160UA][j] * + (uint32_t) g_ctsu_correction_info.range_ratio[i]) >> CTSU_SHIFT_AMOUNT); + g_ctsu_correction_info.coef[i][j] = + (uint16_t) (((uint32_t) g_ctsu_correction_info.coef[CTSU_RANGE_160UA][j] * + (uint32_t) g_ctsu_correction_info.range_ratio[i]) >> CTSU_SHIFT_AMOUNT); + } + } + + #if (CTSU_CFG_AUTO_CORRECTION_ENABLE == 1) + ctsu_auto_correction_register_set(p_instance_ctrl); + #endif + + g_ctsu_correction_info.update_counter = 0; + #if (CTSU_CFG_CALIB_RTRIM_SUPPORT == 1) + err = ctsu_correction_calib_rtrim(p_instance_ctrl, p_data); + if (FSP_ERR_ALREADY_OPEN != err) + { + R_ADC_Close(0); + } + if (FSP_SUCCESS != err) + { + err = FSP_ERR_ABORTED; + } + #endif + } + else + { + /* Indicates that ADC measurement was not performed. */ + *p_data = CTSU_COUNT_MAX; + } + + CTSU.CTSUCRA.LONG = g_ctsu_temp_reg_ctsucra; +#if (CTSU_CFG_TEMP_CORRECTION_TS < 32) + CTSU.CTSUCHACA.LONG &= (uint32_t) ~(1 << CTSU_CFG_TEMP_CORRECTION_TS); +#else + CTSU.CTSUCHACB.LONG &= (uint32_t) ~(1 << (CTSU_CFG_TEMP_CORRECTION_TS - 32)); +#endif + + return err; +} + + #if (CTSU_CFG_CALIB_RTRIM_SUPPORT == 1) + +/*********************************************************************************************************************** + * ctsu_correction_calib_rtrim + ***********************************************************************************************************************/ +fsp_err_t ctsu_correction_calib_rtrim (ctsu_instance_ctrl_t * const p_instance_ctrl, uint16_t * p_data) +{ + uint16_t i; + uint16_t adctdr_result; + uint16_t adctdr_ave; + uint32_t adctdr_sum; + int16_t diff; + int16_t dir = 0; + uint16_t comp = 0; + adc_cfg_t open_cfg; + adc_ch_cfg_t ctrl_cfg; + fsp_err_t err = FSP_SUCCESS; + adc_err_t poll; + + /* TSCAP AD/C AN008 Setting */ + CTSU.CTSUADCC.LONG = 1; + + /* Setting for A/D convert mode */ + open_cfg.conv_speed = ADC_CONVERT_SPEED_DEFAULT; + open_cfg.alignment = ADC_ALIGN_RIGHT; + open_cfg.add_cnt = ADC_ADD_OFF; /* addition is turned off for chans/sensors */ + open_cfg.clearing = ADC_CLEAR_AFTER_READ_OFF; + open_cfg.trigger = ADC_TRIG_SOFTWARE; /* Software trigger */ + open_cfg.trigger_groupb = ADC_TRIG_NONE; /* Trigger none when without group mode */ + open_cfg.priority = 0; /* S12ADIO no interrupt(polling) */ + open_cfg.priority_groupb = 0; /* S12GBADI no interrupt */ + err = (fsp_err_t) R_ADC_Open (0, ADC_MODE_SS_ONE_CH, &open_cfg, FIT_NO_FUNC); + if (ADC_ERR_AD_NOT_CLOSED == (adc_err_t) err) + { + return FSP_ERR_ALREADY_OPEN; + } + FSP_ERROR_RETURN(FSP_SUCCESS == err, err); + + /* choice A/D port(allow A/D convert) */ + ctrl_cfg.chan_mask = ADC_MASK_CH8; + ctrl_cfg.chan_mask_groupb = ADC_MASK_GROUPB_OFF; + ctrl_cfg.priority_groupa = ADC_GRPA_PRIORITY_OFF; + ctrl_cfg.diag_method = ADC_DIAG_OFF; + ctrl_cfg.add_mask = ADC_MASK_ADD_OFF; + ctrl_cfg.signal_elc = ADC_ELC_ALL_SCANS_DONE; + err = (fsp_err_t) R_ADC_Control (0, ADC_CMD_ENABLE_CHANS, &ctrl_cfg); + FSP_ERROR_RETURN(FSP_SUCCESS == err, err); + + /* Self single scan mode */ + CTSU.CTSUCRA.BIT.LOAD = 1; + CTSU.CTSUCRA.BIT.MD0 = 0; + CTSU.CTSUCRA.BIT.MD1 = 0; + + /* Set Channel */ + #if (CTSU_CFG_TEMP_CORRECTION_TS < 32) + CTSU.CTSUCHACA.LONG |= (uint32_t) (1 << CTSU_CFG_TEMP_CORRECTION_TS); + CTSU.CTSUCHTRCA.LONG &= (uint32_t) ~(1 << CTSU_CFG_TEMP_CORRECTION_TS); + #else + CTSU.CTSUCHACB.LONG |= (uint32_t) (1 << (CTSU_CFG_TEMP_CORRECTION_TS - 32)); + CTSU.CTSUCHTRCB.LONG &= (uint32_t) ~(1 << (CTSU_CFG_TEMP_CORRECTION_TS - 32)); + #endif + CTSU.CTSUMCH.BIT.MCH0 = CTSU_CFG_TEMP_CORRECTION_TS; + + /* 150uA current measurement */ + CTSU.CTSUCRA.BIT.DCMODE = 1; + CTSU.CTSUCRA.BIT.DCBACK = 1; + CTSU.CTSUSO.BIT.SO = CTSU_CALIB_CTSUSO; + CTSU.CTSUCRA.BIT.CSW = 0; + CTSU.CTSUCALIB.BIT.DRV = 1; + + /* ADC scan */ + while (!comp) + { + adctdr_sum = 0; + for (i = 0; i < CTSU_CALIB_AVERAGE_TIME; i++) + { + /* Software trigger start scan */ + err = (fsp_err_t) R_ADC_Control (0, ADC_CMD_SCAN_NOW, FIT_NO_PTR); + FSP_ERROR_RETURN(FSP_SUCCESS == err, err); + + /* Polling for scan complete */ + do + { + poll = R_ADC_Control (0, ADC_CMD_CHECK_SCAN_DONE, FIT_NO_PTR); + } while (ADC_ERR_SCAN_NOT_DONE == poll); + + /* Read A/D data then scan normal end */ + err = (fsp_err_t) R_ADC_Read (0, ADC_REG_CH8, &adctdr_result); + FSP_ERROR_RETURN(FSP_SUCCESS == err, err); + adctdr_sum += adctdr_result; + } + + adctdr_ave = (uint16_t) ((adctdr_sum * 10) / CTSU_CALIB_AVERAGE_TIME); + diff = (int16_t) ((adctdr_ave - CTSU_CALIB_REF) / 10); + + /* The change unit of the voltage by the RTRIM register is about 4mV (4096 * 4) = 16.384 */ + if (diff > CTSU_CALIB_THRESHOLD) + { + if (0 <= dir) + { + CTSU.CTSUTRIMA.BIT.RTRIM = (uint8_t) (CTSU.CTSUTRIMA.BIT.RTRIM + 1); + dir = 1; + } + else + { + comp = 1; + } + } + else if (diff < -(CTSU_CALIB_THRESHOLD)) + { + if (0 >= dir) + { + CTSU.CTSUTRIMA.BIT.RTRIM = (uint8_t) (CTSU.CTSUTRIMA.BIT.RTRIM - 1); + dir = -1; + } + else + { + comp = 1; + } + } + else + { + comp = 1; + } + } + + /* Restore register settings */ + CTSU.CTSUCALIB.BIT.DRV = 0; + CTSU.CTSUCRA.BIT.CSW = 1; + CTSU.CTSUCRA.BIT.DCMODE = 0; + CTSU.CTSUCRA.BIT.DCBACK = 0; + + /* Indicates that ADC measurement was performed */ + *p_data = CTSU.CTSUTRIMA.BIT.RTRIM; + + /* Close ADC for CTSU TSCAP */ + R_ADC_Close(0); + + return err; +} + + #endif + #endif +#endif + +#if (CTSU_CFG_AUTO_CORRECTION_ENABLE == 0) + +/*********************************************************************************************************************** + * ctsu_correction_calc + ***********************************************************************************************************************/ +void ctsu_correction_calc (uint16_t * correction_data, uint16_t raw_data, ctsu_correction_calc_t * p_calc) +{ + uint32_t answer = 0; + uint32_t cmp_data; + uint8_t calc_flag = 0; + #if (BSP_FEATURE_CTSU_VERSION == 1) + uint16_t diff_val; + int32_t diff_coefficient; + int32_t mul_diffcoff_diff1valsval; + uint32_t mul_coff1val_diffcorr; + uint16_t coefficient; + #endif + #if (BSP_FEATURE_CTSU_VERSION == 2) + int32_t y0 = 0; + int32_t y1 = 0; + int32_t x0 = 0; + int32_t x1 = 0; + uint16_t i; + uint32_t scaled_data; + uint32_t coef; + bool flag; + #endif + + #if (BSP_FEATURE_CTSU_VERSION == 2) + #if (CTSU_CFG_NUM_CFC != 0) + if ((CTSU_CORRECTION_COMPLETE == g_ctsu_correction_info.status) || + (CTSU_CORRECTION_COMPLETE == g_ctsu_corrcfc_info.status)) + { + calc_flag = 1; + } + + #else + if (CTSU_CORRECTION_COMPLETE == g_ctsu_correction_info.status) + { + calc_flag = 1; + } + #endif + #endif + #if (BSP_FEATURE_CTSU_VERSION == 1) + if (CTSU_CORRECTION_COMPLETE == g_ctsu_correction_info.status) + { + calc_flag = 1; + } + #endif + + if (calc_flag) + { + #if (BSP_FEATURE_CTSU_VERSION == 1) + + /* Since the correction coefficient table is created with the recommended measurement time, */ + /* If the measurement time is different, adjust the value level. */ + cmp_data = (uint32_t) ((raw_data * g_ctsu_correction_info.ctsu_clock) / + (uint32_t) ((p_calc->snum + 1) * (p_calc->sdpa + 1))); + + /* g_mul_coff1val_diffcorr - g_diff_cofficient * (g_ctsu_correction_info.first_val - raw_data) */ + /* coefficient= ------------------------------------------------------------------------------------------ */ + /* g_diff_correct_val */ + /* */ + + diff_val = (uint16_t) (g_ctsu_correction_info.first_val - g_ctsu_correction_info.second_val); + + /* Get multiplication of g_ctsu_correction_info.first_coefficient and difference of Correction value */ + mul_coff1val_diffcorr = g_ctsu_correction_info.first_coefficient * diff_val; + + /* Get difference of Correction coefficient */ + diff_coefficient = + (int32_t) (g_ctsu_correction_info.first_coefficient - g_ctsu_correction_info.second_coefficient); + + /* Get multiplication of g_diff_cofficient and (g_ctsu_correction_info.first_val - raw_data_coff) */ + mul_diffcoff_diff1valsval = (diff_coefficient * (int32_t) (g_ctsu_correction_info.first_val - cmp_data)); + + /* Get correction coefficient of scan data */ + coefficient = (uint16_t) (((int32_t) mul_coff1val_diffcorr - mul_diffcoff_diff1valsval) / diff_val); + + /* Get output count data */ + answer = (uint32_t) (((uint32_t) raw_data * (uint32_t) coefficient) >> CTSU_SHIFT_AMOUNT); + #endif + #if (BSP_FEATURE_CTSU_VERSION == 2) + + // Normalization measurement data to recommended measurement timed + cmp_data = (uint32_t) raw_data * (CTSU_SNUM_RECOMMEND + 1); + + /* searching for upper index of measurement value containing raw_data by scaled measurement value */ + /* if without scaling, should use binary search */ + /* index search must be stopped when i = 11 because dac_value[11] = 65535 */ + + if (CTSU_MODE_MUTUAL_CFC_SCAN != p_calc->md) + { + for (i = 1; i < CTSU_CORRECTION_POINT_NUM - 1; i++) + { + scaled_data = (uint32_t) (g_ctsu_correction_info.dac_value[p_calc->range][i]) * (p_calc->snum + 1); + if (scaled_data > cmp_data) + { + break; + } + } + + x0 = g_ctsu_correction_info.dac_value[p_calc->range][i - 1]; + x1 = g_ctsu_correction_info.dac_value[p_calc->range][i]; + + // coef + y0 = g_ctsu_correction_info.coef[p_calc->range][i - 1]; + y1 = g_ctsu_correction_info.coef[p_calc->range][i]; + + x0 = (x0 * (p_calc->snum + 1)) / (CTSU_SNUM_RECOMMEND + 1); + x1 = (x1 * (p_calc->snum + 1)) / (CTSU_SNUM_RECOMMEND + 1); + { + coef = (uint32_t) (((y1 - y0) * (raw_data - x0)) / (x1 - x0) + y0); + flag = false; + + if (p_calc->snum < CTSU_SNUM_RECOMMEND) // 1st condition + { + if (raw_data < coef) + { + answer = (coef >> CTSU_CORRECTION_DIV_PRECISION) * raw_data; + } + else + { + answer = coef * (raw_data >> CTSU_CORRECTION_DIV_PRECISION); + } + + if (answer > CTSU_COUNT_MAX) // 2nd condition + { + flag = true; + answer = CTSU_COUNT_MAX; + g_ctsu_correction_info.calculation_error = 1; + } + } + + if (false == flag) + { + answer = (uint32_t) (coef * raw_data) >> CTSU_CORRECTION_DIV_PRECISION; + } + } + } + + #if (CTSU_CFG_NUM_CFC != 0) + else + { + i = 0; + while (1) + { + if ((cmp_data < g_ctsu_corrcfc_info.dac_value[p_calc->cfc][i]) || ((CTSU_CORRCFC_POINT_NUM - 1) == i)) + { + y0 = g_ctsu_corrcfc_info.ref_value[p_calc->cfc][i]; + x0 = g_ctsu_corrcfc_info.dac_value[p_calc->cfc][i]; + if (0 == i) + { + x1 = 0; + y1 = 0; + } + else + { + x1 = g_ctsu_corrcfc_info.dac_value[p_calc->cfc][i - 1]; + y1 = g_ctsu_corrcfc_info.ref_value[p_calc->cfc][i - 1]; + } + + break; + } + + i++; + } + + if (CTSU_SNUM_RECOMMEND != p_calc->snum) + { + x0 = ((x0 * (p_calc->snum + 1)) / (CTSU_SNUM_RECOMMEND + 1)); + y0 = ((y0 * (p_calc->snum + 1)) / (CTSU_SNUM_RECOMMEND + 1)); + x1 = ((x1 * (p_calc->snum + 1)) / (CTSU_SNUM_RECOMMEND + 1)); + y1 = ((y1 * (p_calc->snum + 1)) / (CTSU_SNUM_RECOMMEND + 1)); + } + + answer = (uint32_t) (y0 - (((y0 - y1) * (x0 - raw_data)) / (x0 - x1))); + } + #endif + #endif + + /* Value Overflow Check */ + if (CTSU_COUNT_MAX < answer) + { + *correction_data = CTSU_COUNT_MAX; + g_ctsu_correction_info.calculation_error = 1; + } + else + { + *correction_data = (uint16_t) answer; + } + } + else + { + *correction_data = raw_data; + } +} + +#endif + +/*********************************************************************************************************************** + * ctsu_correction_exec + ***********************************************************************************************************************/ +void ctsu_correction_exec (ctsu_instance_ctrl_t * const p_instance_ctrl) +{ +#if (BSP_FEATURE_CTSU_VERSION == 1) + ctsu_correction_ctsu1_exec(p_instance_ctrl); +#endif +#if (BSP_FEATURE_CTSU_VERSION == 2) + ctsu_correction_ctsu2_exec(p_instance_ctrl); +#endif +} + +#if (BSP_FEATURE_CTSU_VERSION == 1) +void ctsu_correction_ctsu1_exec (ctsu_instance_ctrl_t * const p_instance_ctrl) +{ + uint16_t element_id; + + ctsu_correction_calc_t calc; + #if (CTSU_CFG_NUM_SELF_ELEMENTS != 0) + ctsu_data_t * p_self_data; + ctsu_data_t average_self = {0, 0}; + #endif + #if (CTSU_CFG_NUM_MUTUAL_ELEMENTS != 0) + ctsu_data_t * p_pri_data; + ctsu_data_t * p_snd_data; + ctsu_data_t average_pri = {0, 0}; + ctsu_data_t average_snd = {0, 0}; + #endif + + for (element_id = 0; element_id < p_instance_ctrl->num_elements; element_id++) + { + calc.snum = (p_instance_ctrl->p_ctsuwr[element_id].ctsuso0 >> 10) & CTSU_SNUM_MAX; + calc.sdpa = (p_instance_ctrl->p_ctsuwr[element_id].ctsuso1 >> 8) & CTSU_SDPA_MAX; + #if (CTSU_CFG_NUM_SELF_ELEMENTS != 0) + if (CTSU_MODE_SELF_MULTI_SCAN == p_instance_ctrl->md) + { + p_self_data = (p_instance_ctrl->p_self_data + element_id); + average_self = *p_self_data; + ctsu_correction_calc((p_instance_ctrl->p_self_corr + element_id), + (p_instance_ctrl->p_self_raw + element_id)->sen, + &calc); + p_self_data->int_data = *(p_instance_ctrl->p_self_corr + element_id); + if (1 < p_instance_ctrl->average) + { + ctsu_moving_average(&average_self, p_self_data->int_data, p_instance_ctrl->average); + *p_self_data = average_self; + } + } + #endif + #if (CTSU_CFG_NUM_MUTUAL_ELEMENTS != 0) + p_pri_data = (p_instance_ctrl->p_mutual_pri_data + element_id); + p_snd_data = (p_instance_ctrl->p_mutual_snd_data + element_id); + if (CTSU_MODE_MUTUAL_FULL_SCAN == p_instance_ctrl->md) + { + average_pri = *p_pri_data; + average_snd = *p_snd_data; + ctsu_correction_calc((p_instance_ctrl->p_mutual_pri_corr + element_id), + (p_instance_ctrl->p_mutual_raw + element_id)->pri_sen, &calc); + ctsu_correction_calc((p_instance_ctrl->p_mutual_snd_corr + element_id), + (p_instance_ctrl->p_mutual_raw + element_id)->snd_sen, &calc); + p_pri_data->int_data = *(p_instance_ctrl->p_mutual_pri_corr + element_id); + p_snd_data->int_data = *(p_instance_ctrl->p_mutual_snd_corr + element_id); + + if (1 < p_instance_ctrl->average) + { + ctsu_moving_average(&average_pri, p_pri_data->int_data, p_instance_ctrl->average); + ctsu_moving_average(&average_snd, p_snd_data->int_data, p_instance_ctrl->average); + *p_pri_data = average_pri; + *p_snd_data = average_snd; + } + } + #endif + } +} + +#endif + +#if (BSP_FEATURE_CTSU_VERSION == 2) +void ctsu_correction_ctsu2_exec (ctsu_instance_ctrl_t * const p_instance_ctrl) +{ + uint16_t i; + uint16_t element_id; + uint16_t majority_mode_element_num; + #if (CTSU_CFG_AUTO_MULTI_CLOCK_CORRECTION_ENABLE == 0) + ctsu_correction_calc_t calc; + uint32_t ctsuso; + uint32_t snum; + uint32_t so_value[CTSU_CFG_NUM_SUMULTI]; + #else + uint16_t j; + #endif + + #if (CTSU_CFG_NUM_SELF_ELEMENTS != 0) + uint16_t * p_self_corr; + uint16_t * p_self_mfc; + ctsu_data_t * p_self_data; + ctsu_data_t average_self; + #endif + #if (CTSU_CFG_NUM_MUTUAL_ELEMENTS != 0) + uint16_t * p_pri_corr; + uint16_t * p_snd_corr; + uint16_t * p_pri_mfc; + uint16_t * p_snd_mfc; + ctsu_data_t * p_pri_data; + ctsu_data_t * p_snd_data; + ctsu_data_t average_pri; + ctsu_data_t average_snd; + + #if (CTSU_CFG_NUM_CFC != 0) + uint8_t ts_id; + uint8_t table_id; + uint8_t cfc_ts_table[CTSU_CFG_NUM_CFC]; + uint16_t offset = 0; + uint16_t cfc_id = 0; + uint16_t num_rx = 0; + + /* By inspection, calc.cfc is only used when calc.md == CTSU_MODE_MUTUAL_CFC_SCAN. It is initialized in this case. + * However, GCC expects calc.cfc to be initialized before calling ctsu_correction_calc regardless of calc.md to + * avoid the warning -Werror=maybe-uninitialized. */ + calc.cfc = 0; + + /* Create CFC-Rx table in ascending order at this instance */ + for (ts_id = 0; ts_id < CTSU_CORRCFC_TS_MAX; ts_id++) + { + if (1 == ((p_instance_ctrl->cfc_rx_bitmap >> ts_id) & 1)) + { + for (table_id = 0; table_id < CTSU_CFG_NUM_CFC; table_id++) + { + if (g_ctsu_corrcfc_info.ts_table[table_id] == ts_id) + { + cfc_ts_table[cfc_id] = table_id; + cfc_id++; + } + } + } + } + #endif + #endif + #if (CTSU_CFG_MAJORITY_MODE & CTSU_JUDGEMENT_MAJORITY_MODE) + if (1 == p_instance_ctrl->p_ctsu_cfg->majority_mode) + { + majority_mode_element_num = CTSU_MAJORITY_MODE_ELEMENTS; + } + else + #endif + { + majority_mode_element_num = 1; + } + + #if (CTSU_CFG_AUTO_MULTI_CLOCK_CORRECTION_ENABLE == 0) + calc.range = p_instance_ctrl->range; + calc.md = p_instance_ctrl->md; + #endif + + for (element_id = 0; element_id < p_instance_ctrl->num_elements; element_id++) + { + #if (CTSU_CFG_AUTO_MULTI_CLOCK_CORRECTION_ENABLE == 0) + calc.snum = (p_instance_ctrl->p_ctsuwr[(element_id * CTSU_CFG_NUM_SUMULTI)].ctsuso >> 10) & CTSU_SNUM_MAX; + for (i = 0; i < CTSU_CFG_NUM_SUMULTI; i++) + { + ctsuso = + (p_instance_ctrl->p_ctsuwr[(element_id * CTSU_CFG_NUM_SUMULTI) + i].ctsuso & CTSU_TUNING_MAX); + snum = (p_instance_ctrl->p_ctsuwr[(element_id * CTSU_CFG_NUM_SUMULTI)].ctsuso >> 10) & + CTSU_SNUM_MAX; + so_value[i] = (ctsuso * CTSU_CORRECTION_OFFSET_UNIT * (snum + 1)) >> calc.range; + } + #endif + + if (CTSU_MODE_SELF_MULTI_SCAN == p_instance_ctrl->md) + { + #if (CTSU_CFG_NUM_SELF_ELEMENTS != 0) + p_self_corr = p_instance_ctrl->p_self_corr + (element_id * CTSU_CFG_NUM_SUMULTI); + for (i = 0; i < CTSU_CFG_NUM_SUMULTI; i++) + { + #if (CTSU_CFG_AUTO_CORRECTION_ENABLE == 0) + ctsu_correction_calc(&p_self_corr[i], + p_instance_ctrl->p_self_raw[(element_id * CTSU_CFG_NUM_SUMULTI) + i], &calc); + #else + *(p_self_corr + i) = + p_instance_ctrl->p_self_raw[(element_id * CTSU_CFG_NUM_SUMULTI) + i]; + #endif + } + + for (i = 0; i < majority_mode_element_num; i++) + { + if (0 != p_instance_ctrl->average) + { + p_self_data = (p_instance_ctrl->p_self_data + (element_id * majority_mode_element_num) + i); + + /* Store last moving averaged data */ + average_self.int_data = p_self_data->int_data; + average_self.decimal_point_data = p_self_data->decimal_point_data; + #if (CTSU_CFG_MAJORITY_MODE & CTSU_JUDGEMENT_MAJORITY_MODE) + if (p_instance_ctrl->p_ctsu_cfg->majority_mode == 1) + { + /* Skip the ctsu_correction_multi at Software JMM */ + p_self_data->int_data = p_self_corr[i]; + } + else + #endif + { + p_self_mfc = (p_instance_ctrl->p_self_mfc + (element_id * CTSU_CFG_NUM_SUMULTI)); + + /* Matching values */ + #if (CTSU_CFG_AUTO_MULTI_CLOCK_CORRECTION_ENABLE == 0) + ctsu_correction_freq(p_self_corr, so_value, p_self_mfc); + #else + for (j = 0; j < CTSU_CFG_NUM_SUMULTI; j++) + { + p_self_mfc[j] = p_self_corr[j]; + } + #endif + *(p_instance_ctrl->p_selected_freq_self + element_id) = + ctsu_correction_multi(p_self_mfc, NULL, &(p_self_data->int_data), NULL); + } + + /* Update moving averaged data */ + ctsu_moving_average(&average_self, p_self_data->int_data, p_instance_ctrl->average); + *p_self_data = average_self; + } + } + } + else if (CTSU_MODE_CURRENT_SCAN == p_instance_ctrl->md) + { + p_self_data = (p_instance_ctrl->p_self_data + (element_id * CTSU_CFG_NUM_SUMULTI)); + + /* Store last moving averaged data */ + average_self = *p_self_data; + + /* Correction */ + #if (CTSU_CFG_AUTO_CORRECTION_ENABLE == 0) + ctsu_correction_calc(&(p_self_data->int_data), p_instance_ctrl->p_self_raw[element_id], &calc); + #else + p_self_data->int_data = p_instance_ctrl->p_self_raw[element_id]; + #endif + + /* Update moving averaged data */ + if (1 < p_instance_ctrl->average) + { + ctsu_moving_average(&average_self, p_self_data->int_data, p_instance_ctrl->average); + *p_self_data = average_self; + } + #endif + } + else if (CTSU_MODE_MUTUAL_FULL_SCAN == p_instance_ctrl->md) + { + #if (CTSU_CFG_NUM_MUTUAL_ELEMENTS != 0) + p_pri_corr = p_instance_ctrl->p_mutual_pri_corr + (element_id * CTSU_CFG_NUM_SUMULTI); + p_snd_corr = p_instance_ctrl->p_mutual_snd_corr + (element_id * CTSU_CFG_NUM_SUMULTI); + for (i = 0; i < CTSU_CFG_NUM_SUMULTI; i++) + { + if (true == p_instance_ctrl->serial_tuning_enable) + { + if ((0 == p_instance_ctrl->ctsuchtrc0) && (0 == p_instance_ctrl->ctsuchtrc1) && + (0 == p_instance_ctrl->ctsuchtrc2) && (0 == p_instance_ctrl->ctsuchtrc3) && + (0 == p_instance_ctrl->ctsuchtrc4)) + { + /* Serial tuning Phase1 */ + #if (CTSU_CFG_AUTO_CORRECTION_ENABLE == 0) + ctsu_correction_calc(&p_instance_ctrl->p_mutual_pri_corr[element_id], + p_instance_ctrl->p_mutual_raw[element_id * CTSU_CFG_NUM_SUMULTI], + &calc); + #else + p_instance_ctrl->p_mutual_pri_corr[element_id] = + p_instance_ctrl->p_mutual_raw[element_id * CTSU_CFG_NUM_SUMULTI]; + #endif + break; + } + } + + #if (CTSU_CFG_AUTO_CORRECTION_ENABLE == 0) + ctsu_correction_calc(&p_pri_corr[i], + p_instance_ctrl->p_mutual_raw[(element_id * CTSU_MUTUAL_BUF_SIZE) + (i * 2)], + &calc); + #else + p_pri_corr[i] = + p_instance_ctrl->p_mutual_raw[(element_id * CTSU_MUTUAL_BUF_SIZE) + (i * 2)]; + #endif + + #if (CTSU_CFG_AUTO_CORRECTION_ENABLE == 0) + ctsu_correction_calc(&p_snd_corr[i], + p_instance_ctrl->p_mutual_raw[(element_id * CTSU_MUTUAL_BUF_SIZE) + (i * 2) + 1], + &calc); + #else + p_snd_corr[i] = + p_instance_ctrl->p_mutual_raw[(element_id * CTSU_MUTUAL_BUF_SIZE) + (i * 2) + 1]; + #endif + } + + for (i = 0; i < majority_mode_element_num; i++) + { + if (true == p_instance_ctrl->serial_tuning_enable) + { + /* Serial Tuning Phase1 */ + if ((0 == p_instance_ctrl->ctsuchtrc0) && (0 == p_instance_ctrl->ctsuchtrc1) && + (0 == p_instance_ctrl->ctsuchtrc2) && (0 == p_instance_ctrl->ctsuchtrc3) && + (0 == p_instance_ctrl->ctsuchtrc4)) + { + break; + } + } + + if (0 != p_instance_ctrl->average) + { + p_pri_data = (p_instance_ctrl->p_mutual_pri_data + (element_id * majority_mode_element_num) + i); + p_snd_data = (p_instance_ctrl->p_mutual_snd_data + (element_id * majority_mode_element_num) + i); + + /* Store last moving averaged data */ + average_pri = *p_pri_data; + average_snd = *p_snd_data; + #if (CTSU_CFG_MAJORITY_MODE & CTSU_JUDGEMENT_MAJORITY_MODE) + if (p_instance_ctrl->p_ctsu_cfg->majority_mode == 1) + { + /* Skip the ctsu_correction_multi at Software JMM */ + p_pri_data->int_data = p_pri_corr[i]; + p_snd_data->int_data = p_snd_corr[i]; + } + else + #endif + { + p_pri_mfc = (p_instance_ctrl->p_mutual_pri_mfc + (element_id * CTSU_CFG_NUM_SUMULTI)); + p_snd_mfc = (p_instance_ctrl->p_mutual_snd_mfc + (element_id * CTSU_CFG_NUM_SUMULTI)); + + /* Matching values */ + #if (CTSU_CFG_AUTO_MULTI_CLOCK_CORRECTION_ENABLE == 0) + ctsu_correction_freq(p_pri_corr, so_value, p_pri_mfc); + ctsu_correction_freq(p_snd_corr, so_value, p_snd_mfc); + #else + for (j = 0; j < CTSU_CFG_NUM_SUMULTI; j++) + { + p_pri_mfc[j] = p_pri_corr[j]; + p_snd_mfc[j] = p_snd_corr[j]; + } + #endif + *(p_instance_ctrl->p_selected_freq_mutual + element_id) = + ctsu_correction_multi(p_pri_mfc, p_snd_mfc, &(p_pri_data->int_data), + &(p_snd_data->int_data)); + } + + /* Update moving averaged data */ + ctsu_moving_average(&average_pri, p_pri_data->int_data, p_instance_ctrl->average); + *p_pri_data = average_pri; + ctsu_moving_average(&average_snd, p_snd_data->int_data, p_instance_ctrl->average); + *p_snd_data = average_snd; + } + } + #endif + } + else if (CTSU_MODE_MUTUAL_CFC_SCAN == p_instance_ctrl->md) + { + #if (CTSU_CFG_NUM_MUTUAL_ELEMENTS != 0) + #if (CTSU_CFG_NUM_CFC != 0) + if (CTSU_MODE_MUTUAL_CFC_SCAN == p_instance_ctrl->md) + { + num_rx = p_instance_ctrl->p_ctsu_cfg->num_rx; + cfc_id = (uint16_t) (element_id % num_rx); + if (0 == cfc_id) + { + if (0 == element_id) + { + offset = 0; + } + else + { + offset = (uint16_t) (offset + (num_rx * CTSU_MUTUAL_BUF_SIZE)); + } + } + } + + calc.cfc = cfc_ts_table[cfc_id]; + + p_pri_corr = p_instance_ctrl->p_mutual_pri_corr + (element_id * CTSU_CFG_NUM_SUMULTI); + p_snd_corr = p_instance_ctrl->p_mutual_snd_corr + (element_id * CTSU_CFG_NUM_SUMULTI); + + for (i = 0; i < CTSU_CFG_NUM_SUMULTI; i++) + { + ctsu_correction_calc(&p_pri_corr[i], + p_instance_ctrl->p_mutual_raw[offset + cfc_id + (num_rx * i * 2)], + &calc); + + ctsu_correction_calc(&p_snd_corr[i], + p_instance_ctrl->p_mutual_raw[offset + cfc_id + (num_rx * i * 2) + num_rx], &calc); + } + + if (0 != p_instance_ctrl->average) + { + p_pri_data = (p_instance_ctrl->p_mutual_pri_data + element_id); + p_snd_data = (p_instance_ctrl->p_mutual_snd_data + element_id); + + /* Store last moving averaged data */ + average_pri = *p_pri_data; + average_snd = *p_snd_data; + + p_pri_mfc = (p_instance_ctrl->p_mutual_pri_mfc + (element_id * CTSU_CFG_NUM_SUMULTI)); + p_snd_mfc = (p_instance_ctrl->p_mutual_snd_mfc + (element_id * CTSU_CFG_NUM_SUMULTI)); + + /* Matching values */ + ctsu_correction_freq(p_pri_corr, so_value, p_pri_mfc); + ctsu_correction_freq(p_snd_corr, so_value, p_snd_mfc); + *(p_instance_ctrl->p_selected_freq_mutual + element_id) = + ctsu_correction_multi(p_pri_mfc, p_snd_mfc, &(p_pri_data->int_data), &(p_snd_data->int_data)); + + /* Update moving averaged data */ + ctsu_moving_average(&average_pri, p_pri_data->int_data, p_instance_ctrl->average); + *p_pri_data = average_pri; + ctsu_moving_average(&average_snd, p_snd_data->int_data, p_instance_ctrl->average); + *p_snd_data = average_snd; + } + #endif + #endif + } + else + { + } + } +} + +#endif +#if (BSP_FEATURE_CTSU_VERSION == 2) + #if (CTSU_CFG_AUTO_MULTI_CLOCK_CORRECTION_ENABLE == 0) + +/*********************************************************************************************************************** + * ctsu_correction_freq + ***********************************************************************************************************************/ +void ctsu_correction_freq (uint16_t * p_corr, uint32_t * p_so_value, uint16_t * p_mfc) +{ + uint32_t i; + uint32_t sumulti[CTSU_CFG_NUM_SUMULTI]; + int32_t calc[CTSU_CFG_NUM_SUMULTI]; + uint32_t total; + + sumulti[0] = CTSU_CFG_SUMULTI0 + 1; + #if CTSU_CFG_NUM_SUMULTI >= 2 + sumulti[1] = CTSU_CFG_SUMULTI1 + 1; + #endif + #if CTSU_CFG_NUM_SUMULTI >= 3 + sumulti[2] = CTSU_CFG_SUMULTI2 + 1; + #endif + + p_mfc[0] = p_corr[0]; + + for (i = 1; i < CTSU_CFG_NUM_SUMULTI; i++) + { + total = ((p_corr[i] * (CTSU_SNUM_RECOMMEND + 1) + p_so_value[i]) * sumulti[0]) / sumulti[i]; + + if (total > p_so_value[0]) + { + calc[i] = (int32_t) ((total - p_so_value[0]) / (CTSU_SNUM_RECOMMEND + 1)); + if (calc[i] < CTSU_COUNT_MAX) + { + p_mfc[i] = (uint16_t) calc[i]; + } + else + { + /* over flow */ + p_mfc[i] = CTSU_COUNT_MAX; + } + } + else + { + /* under flow */ + calc[i] = 0; + p_mfc[i] = 0; + } + } +} + + #endif + +/*********************************************************************************************************************** + * ctsu_correction_multi + ***********************************************************************************************************************/ +uint8_t ctsu_correction_multi (uint16_t * p_pri_mfc, uint16_t * p_snd_mfc, uint16_t * p_pri_data, uint16_t * p_snd_data) +{ + uint8_t selected_freq; + int32_t add_pri; + int32_t add_snd; + #if CTSU_CFG_NUM_SUMULTI >= 3 + uint32_t i; + int32_t pri_calc[CTSU_CFG_NUM_SUMULTI]; + int32_t snd_calc[CTSU_CFG_NUM_SUMULTI]; + int32_t diff[CTSU_CFG_NUM_SUMULTI]; + #endif + + #if CTSU_CFG_NUM_SUMULTI == 1 + add_pri = p_pri_mfc[0]; + if (NULL != p_snd_mfc) + { + add_snd = p_snd_mfc[0]; + } + #endif + #if CTSU_CFG_NUM_SUMULTI == 2 + add_pri = p_pri_mfc[0] + p_pri_mfc[1]; + if (NULL != p_snd_mfc) + { + add_snd = p_snd_mfc[0] + p_snd_mfc[1]; + } + #endif + #if CTSU_CFG_NUM_SUMULTI >= 3 + for (i = 0; i < CTSU_CFG_NUM_SUMULTI; i++) + { + pri_calc[i] = (int32_t) p_pri_mfc[i]; + if (NULL == p_snd_mfc) + { + snd_calc[i] = 0; + } + else + { + snd_calc[i] = (int32_t) p_snd_mfc[i]; + } + } + + diff[0] = (snd_calc[1] - pri_calc[1]) - (snd_calc[0] - pri_calc[0]); + diff[1] = (snd_calc[2] - pri_calc[2]) - (snd_calc[0] - pri_calc[0]); + diff[2] = (snd_calc[2] - pri_calc[2]) - (snd_calc[1] - pri_calc[1]); + + for (i = 0; i < CTSU_CFG_NUM_SUMULTI; i++) + { + if (diff[i] < 0) + { + diff[i] = -diff[i]; + } + } + + /* Normally select freq0 and freq1 addition */ + /* If the following conditions are false, it is estimated to be a noise environment. */ + /* Compare with the combination with the other frequency difference (including margin). */ + if ((diff[0] < (diff[1] * 2)) && (diff[0] < ((diff[2] * 3) / 2))) + { + add_pri = pri_calc[0] + pri_calc[1]; + add_snd = snd_calc[0] + snd_calc[1]; + + selected_freq = 0x3; + } + else + { + if (diff[1] < diff[2]) + { + add_pri = pri_calc[0] + pri_calc[2]; + add_snd = snd_calc[0] + snd_calc[2]; + + selected_freq = 0x5; + } + else + { + add_pri = pri_calc[1] + pri_calc[2]; + add_snd = snd_calc[1] + snd_calc[2]; + + selected_freq = 0x6; + } + } + #endif + + if (CTSU_COUNT_MAX < add_pri) + { + *p_pri_data = CTSU_COUNT_MAX; + } + else + { + *p_pri_data = (uint16_t) add_pri; + } + + if (NULL != p_snd_data) + { + if (CTSU_COUNT_MAX < add_snd) + { + *p_snd_data = CTSU_COUNT_MAX; + } + else + { + *p_snd_data = (uint16_t) add_snd; + } + } + + return selected_freq; +} + + #if (CTSU_CFG_NUM_CFC != 0) + +/*********************************************************************************************************************** + * ctsu_corrcfc_process + ***********************************************************************************************************************/ +void ctsu_corrcfc_process (ctsu_instance_ctrl_t * const p_instance_ctrl) +{ + uint8_t i; + uint8_t j; + uint8_t ts_id; + uint8_t index; + uint64_t new_bitmap; + ctsu_cfg_t const * p_cfg; + uint16_t base_value; + uint16_t base_conv_dac; + int32_t x0; + int32_t x1; + int32_t y0; + + g_ctsu_corrcfc_info.status = CTSU_CORRECTION_RUN; + + p_cfg = p_instance_ctrl->p_ctsu_cfg; + index = g_ctsu_corrcfc_info.index; + + /* Get CFC-Rx bitmap at this instance. */ + p_instance_ctrl->cfc_rx_bitmap = + ((uint64_t) (p_cfg->ctsuchac0 & (~p_cfg->ctsuchtrc0)) | + ((uint64_t) (p_cfg->ctsuchac1 & (~p_cfg->ctsuchtrc1)) << (CTSU_CORRCFC_SHIFT8)) | + ((uint64_t) (p_cfg->ctsuchac2 & (~p_cfg->ctsuchtrc2)) << (CTSU_CORRCFC_SHIFT8 * 2)) | + ((uint64_t) (p_cfg->ctsuchac3 & (~p_cfg->ctsuchtrc3)) << (CTSU_CORRCFC_SHIFT8 * 3)) | + ((uint64_t) (p_cfg->ctsuchac4 & (~p_cfg->ctsuchtrc4)) << (CTSU_CORRCFC_SHIFT8 * 4))); + + /* Get RX bitmap at this correction. */ + new_bitmap = p_instance_ctrl->cfc_rx_bitmap & (~g_ctsu_corrcfc_info.stored_rx_bitmap); + + /* Get TS id and number of TS in this instance. */ + g_ctsu_corrcfc_info.num_ts = 0; + for (ts_id = 0; ts_id < CTSU_CORRCFC_TS_MAX; ts_id++) + { + if (1 == ((new_bitmap >> ts_id) & 1)) + { + g_ctsu_corrcfc_info.ts_table[index + g_ctsu_corrcfc_info.num_ts] = ts_id; + g_ctsu_corrcfc_info.num_ts++; + } + } + + /* Add the bitmap of this instance to the entire CFC correction information. */ + g_ctsu_corrcfc_info.stored_rx_bitmap |= p_instance_ctrl->cfc_rx_bitmap; + + /* Write Channel setting */ + R_CTSU->CTSUCHAC0 = (uint8_t) (new_bitmap); + R_CTSU->CTSUCHAC1 = (uint8_t) (new_bitmap >> (CTSU_CORRCFC_SHIFT8)); + R_CTSU->CTSUCHAC2 = (uint8_t) (new_bitmap >> (CTSU_CORRCFC_SHIFT8 * 2)); + R_CTSU->CTSUCHAC3 = (uint8_t) (new_bitmap >> (CTSU_CORRCFC_SHIFT8 * 3)); + R_CTSU->CTSUCHAC4 = (uint8_t) (new_bitmap >> (CTSU_CORRCFC_SHIFT8 * 4)); + + /* Set register of CFC self mode */ + R_CTSU->CTSUCRA_b.MD0 = 1; + R_CTSU->CTSUCRA_b.MD1 = 0; + R_CTSU->CTSUCRA_b.MD2 = 1; + R_CTSU->CTSUMCH_b.MCA0 = 1; + R_CTSU->CTSUMCH_b.MCA1 = 0; + R_CTSU->CTSUMCH_b.MCA2 = 0; + R_CTSU->CTSUMCH_b.MCA3 = 0; + + /* Set time of measurement */ + g_ctsu_corrcfc_info.ctsuwr.ctsuso = (CTSU_SNUM_RECOMMEND << 10); + + /* Step1 : Measure the current input to the CFC-ICO by passing bias current. */ + /* The theoretical value of the current is 6uA. */ + ctsu_corrcfc_measurement(p_instance_ctrl, &g_ctsu_corrcfc_info.base_value[index], 1); + + /* Step2 : Measure by inputting each constant current from internal DAC to CFC-ICO. */ + R_CTSU->CTSUCRB_b.SSCNT = 0; + R_CTSU->CTSUCRA_b.LOAD = 1; + R_CTSU->CTSUCALIB_b.CCOCLK = 0; + R_CTSU->CTSUCALIB_b.CFCMODE = 1; + + for (j = 0; j < CTSU_CORRCFC_POINT_NUM; j++) + { + R_CTSU->CTSUCRA_b.SDPSEL = 0; + R_CTSU->CTSUSUCLK0 = (uint16_t) (((j + CTSU_CORRCFC_CENTER_POINT) * CTSU_CORRECTION_SUMULTI) - 1); + R_CTSU->CTSUCRA_b.SDPSEL = 1; + + ctsu_corrcfc_measurement(p_instance_ctrl, &g_ctsu_corrcfc_info.dac_value[index][j], CTSU_CORRCFC_POINT_NUM); + } + + /* Reset register */ + R_CTSU->CTSUCALIB_b.CFCMODE = 0; + R_CTSU->CTSUCALIB_b.CCOCLK = 1; + + /* Step3 : Calculate the error between step1 and step2. */ + for (i = index; i < (index + g_ctsu_corrcfc_info.num_ts); i++) + { + /* Linear interpolation calculation */ + base_value = g_ctsu_corrcfc_info.base_value[i]; + j = 1; + while (1) + { + if ((base_value < g_ctsu_corrcfc_info.dac_value[index][j]) || ((CTSU_CORRCFC_POINT_NUM - 1) == j)) + { + y0 = (uint16_t) (CTSU_CORRECTION_STD_UNIT * (j + CTSU_CORRCFC_CENTER_POINT)); + x0 = g_ctsu_corrcfc_info.dac_value[index][j]; + x1 = g_ctsu_corrcfc_info.dac_value[index][j - 1]; + break; + } + + j++; + } + + base_conv_dac = (uint16_t) (y0 - ((CTSU_CORRECTION_STD_UNIT * (x0 - base_value)) / (x0 - x1))); + + /* Error rate calculation */ + g_ctsu_corrcfc_info.error_rate[i] = + (uint16_t) (((CTSU_CORRECTION_STD_UNIT * 4) << CTSU_SHIFT_AMOUNT) / base_conv_dac); + + for (j = 0; j < CTSU_CORRCFC_POINT_NUM; j++) + { + g_ctsu_corrcfc_info.ref_value[i][j] = + (uint16_t) ((CTSU_CORRECTION_STD_UNIT * (j + CTSU_CORRCFC_CENTER_POINT) * + g_ctsu_corrcfc_info.error_rate[i]) >> CTSU_SHIFT_AMOUNT); + } + } + + g_ctsu_corrcfc_info.index = (uint8_t) (index + g_ctsu_corrcfc_info.num_ts); + + g_ctsu_corrcfc_info.status = CTSU_CORRECTION_COMPLETE; +} + +/*********************************************************************************************************************** + * ctsu_corrcfc_measurement + ***********************************************************************************************************************/ +void ctsu_corrcfc_measurement (ctsu_instance_ctrl_t * const p_instance_ctrl, uint16_t * data, uint8_t point_num) +{ + uint16_t i; + uint8_t cfc_id; + uint32_t sum[CTSU_CFG_NUM_CFC]; + + /* initialize sum array */ + for (cfc_id = 0; cfc_id < CTSU_CFG_NUM_CFC; cfc_id++) + { + sum[cfc_id] = 0; + } + + for (i = 0; i < CTSU_CORRECTION_AVERAGE; i++) + { + #if (CTSU_CFG_DTC_SUPPORT_ENABLE == 1) + ctsu_transfer_configure(p_instance_ctrl); + #else + FSP_PARAMETER_NOT_USED(p_instance_ctrl); + #endif + p_instance_ctrl->state = CTSU_STATE_SCANNING; + R_CTSU->CTSUCR0 |= 0x01; + while (p_instance_ctrl->state != CTSU_STATE_SCANNED) + { + } + + /* Get sum */ + for (cfc_id = 0; cfc_id < CTSU_CFG_NUM_CFC; cfc_id++) + { + sum[cfc_id] += g_ctsu_corrcfc_info.scanbuf[cfc_id]; + } + } + + /* Average sum data */ + for (cfc_id = 0; cfc_id < g_ctsu_corrcfc_info.num_ts; cfc_id++) + { + *(data + (cfc_id * point_num)) = (uint16_t) (sum[cfc_id] / CTSU_CORRECTION_AVERAGE); + } +} + + #endif + + #if (CTSU_CFG_AUTO_CORRECTION_ENABLE == 1) + +/*********************************************************************************************************************** + * ctsu_auto_correction_register_set + ***********************************************************************************************************************/ +void ctsu_auto_correction_register_set (ctsu_instance_ctrl_t * const p_instance_ctrl) +{ + uint16_t j; + uint32_t corr_dac; + uint32_t corr_coef; + int16_t range; + + /* Initialization of sensor counter auto correction table register number */ + CTSU.CTSUOPT.BIT.SCACTB = 0; + + /* Enter the correction factor and dac_value in CTSUSCNTACT for 12 correction tables */ + range = (int16_t) p_instance_ctrl->range; + for (j = 0; j < CTSU_CORRECTION_POINT_NUM; j++) + { + corr_dac = g_ctsu_correction_info.dac_value[range][j]; + corr_coef = g_ctsu_correction_info.coef[range][j]; + + CTSU.CTSUSCNTACT.LONG = (corr_dac << 16) | (corr_coef); + } +} + + #endif + + #if (CTSU_CFG_AUTO_MULTI_CLOCK_CORRECTION_ENABLE == 1) + +/*********************************************************************************************************************** + * ctsu_multiclock_auto_correction_register_set + ***********************************************************************************************************************/ +void ctsu_multiclock_auto_correction_register_set (ctsu_instance_ctrl_t * const p_instance_ctrl) +{ + uint16_t element_id; + int16_t offsetcoeff_1; + int16_t offsetcoeff_2; + uint16_t elem_w_sumulti; + + for (element_id = 0; element_id < p_instance_ctrl->num_elements; element_id++) + { + elem_w_sumulti = element_id * CTSU_CFG_NUM_SUMULTI; + + /* Calculating the Second Frequency */ + offsetcoeff_1 = ctsu_multiclock_auto_correction_calc(p_instance_ctrl, element_id, 2); + + /* Calculating the Third Frequency */ + offsetcoeff_2 = ctsu_multiclock_auto_correction_calc(p_instance_ctrl, element_id, 3); + + if (0 == p_instance_ctrl->p_ctsu_cfg->ajfen) + { + p_instance_ctrl->p_mcact1[elem_w_sumulti + 0] = 0; + p_instance_ctrl->p_mcact1[elem_w_sumulti + 1] = ((uint32_t) offsetcoeff_1 << 16) | + (p_instance_ctrl->p_ctsuwr[elem_w_sumulti + 1].ctsuso & + CTSU_TUNING_MAX); + + p_instance_ctrl->p_mcact1[elem_w_sumulti + 2] = ((uint32_t) offsetcoeff_2 << 16) | + (p_instance_ctrl->p_ctsuwr[elem_w_sumulti + 2].ctsuso & + CTSU_TUNING_MAX); + } + else + { + p_instance_ctrl->p_mcact1[element_id] = ((uint32_t) offsetcoeff_1 << 16) | + (p_instance_ctrl->p_ctsuwr[elem_w_sumulti + 1].ctsuso & + CTSU_TUNING_MAX); + + p_instance_ctrl->p_mcact2[element_id] = ((uint32_t) offsetcoeff_2 << 16) | + (p_instance_ctrl->p_ctsuwr[elem_w_sumulti + 2].ctsuso & + CTSU_TUNING_MAX); + } + } +} + +/*********************************************************************************************************************** + * ctsu_multiclock_auto_correction_calc + ***********************************************************************************************************************/ +int16_t ctsu_multiclock_auto_correction_calc (ctsu_instance_ctrl_t * const p_instance_ctrl, + uint16_t element_id, + uint8_t freq) +{ + uint8_t current_mode; + uint8_t first_sumulti; + uint8_t freq_sumulti; + uint16_t first_so; + uint16_t freq_so; + uint8_t snum_recommend; + uint8_t snum; + int32_t numerator_1; // ((SUMULTIn + 1)SO0 - (SUMULTI0 + 1)SOn) + int32_t numerator_2; // ((Gcco * 20uA * 128us) / 1024)(OffsetDACmax / CurrentMode)) + int32_t numerator_3; // (SNUM + 1) + int32_t denominator; // ((SUMULTIn + 1)(SNUM0 + 1) + int16_t offsetcoeff; + int32_t offsetcoeff_tmp; + uint16_t elem_w_sumulti; + elem_w_sumulti = element_id * CTSU_CFG_NUM_SUMULTI; + + if (CTSU_ATUNE12_80UA == p_instance_ctrl->p_ctsu_cfg->atune12) + { + current_mode = CTSU_AUTO_CURRENT_MODE_80UA; + } + else if (CTSU_ATUNE12_40UA == p_instance_ctrl->p_ctsu_cfg->atune12) + { + current_mode = CTSU_AUTO_CURRENT_MODE_40UA; + } + else if (CTSU_ATUNE12_20UA == p_instance_ctrl->p_ctsu_cfg->atune12) + { + current_mode = CTSU_AUTO_CURRENT_MODE_20UA; + } + else // (CTSU_ATUNE12_160UA == p_instance_ctrl->p_ctsu_cfg->atune12) + { + current_mode = CTSU_AUTO_CURRENT_MODE_160UA; + } + + if (2 == freq) + { + freq_sumulti = CTSU_CFG_SUMULTI1 + 1; + freq_so = p_instance_ctrl->p_ctsuwr[elem_w_sumulti + 1].ctsuso & CTSU_TUNING_MAX; + } + else // 3 == freq + { + freq_sumulti = CTSU_CFG_SUMULTI2 + 1; + freq_so = p_instance_ctrl->p_ctsuwr[elem_w_sumulti + 2].ctsuso & CTSU_TUNING_MAX; + } + + first_so = p_instance_ctrl->p_ctsuwr[elem_w_sumulti].ctsuso & CTSU_TUNING_MAX; + first_sumulti = CTSU_CFG_SUMULTI0 + 1; + snum_recommend = CTSU_SNUM_RECOMMEND + 1; + snum = (p_instance_ctrl->p_ctsuwr[elem_w_sumulti].ctsuso >> 10) & CTSU_SNUM_MAX; + + numerator_1 = ((int32_t) freq_sumulti * first_so) - ((int32_t) first_sumulti * freq_so); + numerator_2 = (int32_t) (CTSU_AUTO_REF_COEFFICIENT * CTSU_AUTO_CORRECTION_OFFSET_DAC_MAX / current_mode); + numerator_3 = (int32_t) snum + 1; + denominator = (int32_t) (freq_sumulti * snum_recommend); + + /* overflow check int32 */ + if ((CTSU_AUTO_INT32_OVERFLOW_VALUE / numerator_2) <= (numerator_1 * numerator_3)) + { + offsetcoeff = CTSU_AUTO_INT16_OVERFLOW_VALUE; + p_instance_ctrl->p_correction_info->calculation_error = 1; + } + else if (-(CTSU_AUTO_INT32_OVERFLOW_VALUE / numerator_2) >= (numerator_1 * numerator_3)) + { + offsetcoeff = CTSU_AUTO_INT16_UNDERFLOW_VALUE; + p_instance_ctrl->p_correction_info->calculation_error = 1; + } + else + { + offsetcoeff_tmp = (numerator_1 * numerator_2 * numerator_3 / denominator); + + /* overflow check int16 */ + if (CTSU_AUTO_INT16_OVERFLOW_VALUE <= offsetcoeff_tmp) + { + offsetcoeff = CTSU_AUTO_INT16_OVERFLOW_VALUE; + p_instance_ctrl->p_correction_info->calculation_error = 1; + } + else if (CTSU_AUTO_INT16_UNDERFLOW_VALUE >= offsetcoeff_tmp) + { + offsetcoeff = CTSU_AUTO_INT16_UNDERFLOW_VALUE; + p_instance_ctrl->p_correction_info->calculation_error = 1; + } + else + { + offsetcoeff = (int16_t) offsetcoeff_tmp; + } + } + + return offsetcoeff; +} + + #endif + + #if (CTSU_CFG_AUTO_JUDGE_ENABLE == 1) +/*********************************************************************************************************************** + * ctsu_auto_judge_threshold_calc + ***********************************************************************************************************************/ +void ctsu_auto_judge_threshold_calc (ctsu_instance_ctrl_t * const p_instance_ctrl) +{ + uint8_t element_id; + uint8_t i; + #if (CTSU_CFG_NUM_MUTUAL_ELEMENTS != 0) + int16_t ajthr_h; + int16_t ajthr_l; + #endif + + for (element_id = 0; element_id < p_instance_ctrl->num_elements; element_id++) + { + #if (CTSU_CFG_AUTO_MULTI_CLOCK_CORRECTION_ENABLE == 1) + if (1 == p_instance_ctrl->p_ctsu_cfg->majirimd) + { + #if (CTSU_CFG_NUM_MUTUAL_ELEMENTS != 0) + if(CTSU_MODE_SELF_MULTI_SCAN == p_instance_ctrl->p_ctsu_cfg->md) + { + #endif + p_instance_ctrl->p_auto_judge[element_id].ajthr = ((uint32_t)p_instance_ctrl->p_ctsu_cfg->p_ctsu_auto_buttons[element_id].threshold << 16) | + (p_instance_ctrl->p_ctsu_cfg->p_ctsu_auto_buttons[element_id].threshold - p_instance_ctrl->p_ctsu_cfg->p_ctsu_auto_buttons[element_id].hysteresis); + #if (CTSU_CFG_NUM_MUTUAL_ELEMENTS != 0) + } + else + { + ajthr_h = -(p_instance_ctrl->p_ctsu_cfg->p_ctsu_auto_buttons[element_id].threshold) + p_instance_ctrl->p_ctsu_cfg->p_ctsu_auto_buttons[element_id].hysteresis; + ajthr_l = -(p_instance_ctrl->p_ctsu_cfg->p_ctsu_auto_buttons[element_id].threshold); + p_instance_ctrl->p_auto_judge[element_id].ajthr = ((uint32_t)ajthr_h << 16) | (ajthr_l & 0x0000FFFF); + } + #endif + } + else + #endif + { + for (i = 0; i < CTSU_CFG_NUM_SUMULTI; i++) + { + #if (CTSU_CFG_NUM_MUTUAL_ELEMENTS != 0) + if(CTSU_MODE_SELF_MULTI_SCAN == p_instance_ctrl->p_ctsu_cfg->md) + { + #endif + p_instance_ctrl->p_auto_judge[(element_id * CTSU_MAJORITY_MODE_ELEMENTS) + i].ajthr = + ((uint32_t)p_instance_ctrl->p_ctsu_cfg->p_ctsu_auto_buttons[(element_id * CTSU_MAJORITY_MODE_ELEMENTS) + i].threshold << 16) | + (p_instance_ctrl->p_ctsu_cfg->p_ctsu_auto_buttons[(element_id * CTSU_MAJORITY_MODE_ELEMENTS) + i].threshold - + p_instance_ctrl->p_ctsu_cfg->p_ctsu_auto_buttons[(element_id * CTSU_MAJORITY_MODE_ELEMENTS) + i].hysteresis); + #if (CTSU_CFG_NUM_MUTUAL_ELEMENTS != 0) + } + else + { + ajthr_h = -(p_instance_ctrl->p_ctsu_cfg->p_ctsu_auto_buttons[(element_id * CTSU_MAJORITY_MODE_ELEMENTS) + i].threshold) + + p_instance_ctrl->p_ctsu_cfg->p_ctsu_auto_buttons[(element_id * CTSU_MAJORITY_MODE_ELEMENTS) + i].hysteresis; + ajthr_l = -(p_instance_ctrl->p_ctsu_cfg->p_ctsu_auto_buttons[(element_id * CTSU_MAJORITY_MODE_ELEMENTS) + i].threshold); + p_instance_ctrl->p_auto_judge[(element_id * CTSU_MAJORITY_MODE_ELEMENTS) + i].ajthr = + ((uint32_t)ajthr_h << 16) | (ajthr_l & 0x0000FFFF); + } + #endif + } + } + } +} + #endif + +#endif + +/*******************************************************************************************************************//** + * @brief Diagnosis the CTSU peripheral. + * Implements @ref ctsu_api_t::diagnosis. + * + * + * Example: + * @snippet r_ctsu_example.c R_CTSU_Diagnosis + * + * @retval FSP_SUCCESS CTSU successfully configured. + * @retval FSP_ERR_ASSERTION Null pointer passed as a parameter. + * @retval FSP_ERR_NOT_OPEN Module is not open. + * @retval FSP_ERR_CTSU_NOT_GET_DATA The previous data has not been retrieved by DataGet. + * @retval FSP_ERR_CTSU_DIAG_LDO_OVER_VOLTAGE Diagnosis of LDO over voltage failed. + * @retval FSP_ERR_CTSU_DIAG_CCO_HIGH Diagnosis of CCO into 19.2uA failed. + * @retval FSP_ERR_CTSU_DIAG_CCO_LOW Diagnosis of CCO into 2.4uA failed. + * @retval FSP_ERR_CTSU_DIAG_SSCG Diagnosis of SSCG frequency failed. + * @retval FSP_ERR_CTSU_DIAG_DAC Diagnosis of non-touch count value failed. + * @retval FSP_ERR_CTSU_DIAG_OUTPUT_VOLTAGE Diagnosis of LDO output voltage failed. + * @retval FSP_ERR_CTSU_DIAG_OVER_VOLTAGE Diagnosis of over voltage detection circuit failed. + * @retval FSP_ERR_CTSU_DIAG_OVER_CURRENT Diagnosis of over current detection circuit failed. + * @retval FSP_ERR_CTSU_DIAG_LOAD_RESISTANCE Diagnosis of LDO internal resistance value failed. + * @retval FSP_ERR_CTSU_DIAG_CURRENT_SOURCE Diagnosis of LDO internal resistance value failed. + * @retval FSP_ERR_CTSU_DIAG_SENSCLK_GAIN Diagnosis of SENSCLK frequency gain failed. + * @retval FSP_ERR_CTSU_DIAG_SUCLK_GAIN Diagnosis of SUCLK frequency gain failed. + * @retval FSP_ERR_CTSU_DIAG_CLOCK_RECOVERY Diagnosis of SUCLK clock recovery function failed. + * @retval FSP_ERR_CTSU_DIAG_CFC_GAIN Diagnosis of CFC oscillator gain failed. + **********************************************************************************************************************/ + +fsp_err_t R_CTSU_Diagnosis (ctsu_ctrl_t * const p_ctrl) +{ +#if (CTSU_CFG_DIAG_SUPPORT_ENABLE == 1) + fsp_err_t diag_err; +#endif + ctsu_instance_ctrl_t * p_instance_ctrl = (ctsu_instance_ctrl_t *) p_ctrl; + +#if (CTSU_CFG_PARAM_CHECKING_ENABLE == 1) + FSP_ASSERT(p_instance_ctrl); + FSP_ERROR_RETURN(CTSU_OPEN == p_instance_ctrl->open, FSP_ERR_NOT_OPEN); +#endif + FSP_ERROR_RETURN(CTSU_STATE_SCANNED != p_instance_ctrl->state, FSP_ERR_CTSU_NOT_GET_DATA); + +#if (BSP_FEATURE_CTSU_VERSION == 1) + #if (CTSU_CFG_DIAG_SUPPORT_ENABLE == 1) + if (CTSU_DIAG_COMPLETE == g_ctsu_diag_info.state) + { + diag_err = ctsu_diag_ldo_over_voltage_result(); + if (FSP_SUCCESS != diag_err) + { + g_ctsu_diag_info.state = CTSU_DIAG_INIT; + + return FSP_ERR_CTSU_DIAG_LDO_OVER_VOLTAGE; + } + + diag_err = ctsu_diag_oscillator_high_result(); + if (FSP_SUCCESS != diag_err) + { + g_ctsu_diag_info.state = CTSU_DIAG_INIT; + + return FSP_ERR_CTSU_DIAG_CCO_HIGH; + } + + diag_err = ctsu_diag_oscillator_low_result(); + if (FSP_SUCCESS != diag_err) + { + g_ctsu_diag_info.state = CTSU_DIAG_INIT; + + return FSP_ERR_CTSU_DIAG_CCO_LOW; + } + + diag_err = ctsu_diag_sscg_result(); + if (FSP_SUCCESS != diag_err) + { + g_ctsu_diag_info.state = CTSU_DIAG_INIT; + + return FSP_ERR_CTSU_DIAG_SSCG; + } + + diag_err = ctsu_diag_dac_result(); + if (FSP_SUCCESS != diag_err) + { + g_ctsu_diag_info.state = CTSU_DIAG_INIT; + + return FSP_ERR_CTSU_DIAG_DAC; + } + + g_ctsu_diag_info.state = CTSU_DIAG_INIT; + } + #endif +#endif + +#if (BSP_FEATURE_CTSU_VERSION == 2) + #if (CTSU_CFG_DIAG_SUPPORT_ENABLE == 1) + if (CTSU_DIAG_COMPLETE == g_ctsu_diag_info.state) + { + ctsu_diag_regi_store2(); + + g_ctsu_diag_info.state = CTSU_DIAG_OUTPUT_VOLTAGE; + diag_err = ctsu_diag_output_voltage_result(); + if (FSP_SUCCESS != diag_err) + { + ctsu_diag_regi_restore2(); + g_ctsu_diag_info.state = CTSU_DIAG_INIT; + + return FSP_ERR_CTSU_DIAG_OUTPUT_VOLTAGE; + } + + g_ctsu_diag_info.state = CTSU_DIAG_OVER_VOLTAGE; + diag_err = ctsu_diag_over_voltage_result(); + if (FSP_SUCCESS != diag_err) + { + ctsu_diag_regi_restore2(); + g_ctsu_diag_info.state = CTSU_DIAG_INIT; + + return FSP_ERR_CTSU_DIAG_OVER_VOLTAGE; + } + + g_ctsu_diag_info.state = CTSU_DIAG_OVER_CURRENT; + diag_err = ctsu_diag_over_current_result(); + if (FSP_SUCCESS != diag_err) + { + ctsu_diag_regi_restore2(); + g_ctsu_diag_info.state = CTSU_DIAG_INIT; + + return FSP_ERR_CTSU_DIAG_OVER_CURRENT; + } + + g_ctsu_diag_info.state = CTSU_DIAG_LOAD_RESISTANCE; + diag_err = ctsu_diag_load_resistance_result(); + if (FSP_SUCCESS != diag_err) + { + ctsu_diag_regi_restore2(); + g_ctsu_diag_info.state = CTSU_DIAG_INIT; + + return FSP_ERR_CTSU_DIAG_LOAD_RESISTANCE; + } + + g_ctsu_diag_info.state = CTSU_DIAG_CURRENT_SOURCE; + diag_err = ctsu_diag_current_source_result(); + if (FSP_SUCCESS != diag_err) + { + ctsu_diag_regi_restore2(); + g_ctsu_diag_info.state = CTSU_DIAG_INIT; + + return FSP_ERR_CTSU_DIAG_CURRENT_SOURCE; + } + + g_ctsu_diag_info.state = CTSU_DIAG_SENSCLK; + diag_err = ctsu_diag_cco_gain_result(); + if (FSP_SUCCESS != diag_err) + { + ctsu_diag_regi_restore2(); + g_ctsu_diag_info.state = CTSU_DIAG_INIT; + + return FSP_ERR_CTSU_DIAG_SENSCLK_GAIN; + } + + g_ctsu_diag_info.state = CTSU_DIAG_SUCLK; + diag_err = ctsu_diag_cco_gain_result(); + if (FSP_SUCCESS != diag_err) + { + ctsu_diag_regi_restore2(); + g_ctsu_diag_info.state = CTSU_DIAG_INIT; + + return FSP_ERR_CTSU_DIAG_SUCLK_GAIN; + } + + g_ctsu_diag_info.state = CTSU_DIAG_CLOCK_RECOVERY; + diag_err = ctsu_diag_clock_recovery_result(); + if (FSP_SUCCESS != diag_err) + { + ctsu_diag_regi_restore2(); + g_ctsu_diag_info.state = CTSU_DIAG_INIT; + + return FSP_ERR_CTSU_DIAG_CLOCK_RECOVERY; + } + + #if (CTSU_CFG_NUM_CFC != 0) + g_ctsu_diag_info.state = CTSU_DIAG_CFC; + diag_err = ctsu_diag_cfc_gain_result(); + if (FSP_SUCCESS != diag_err) + { + ctsu_diag_regi_restore2(); + g_ctsu_diag_info.state = CTSU_DIAG_INIT; + + return FSP_ERR_CTSU_DIAG_CFC_GAIN; + } + #endif + + ctsu_diag_regi_restore2(); + g_ctsu_diag_info.state = CTSU_DIAG_INIT; + } + #endif +#endif + + /* if all checks passed to this point, return success */ + return FSP_SUCCESS; +} + +#if (BSP_FEATURE_CTSU_VERSION == 1) + #if (CTSU_CFG_DIAG_SUPPORT_ENABLE == 1) + +/*********************************************************************************************************************** + * ctsu_diag_scan_start1 + ***********************************************************************************************************************/ +static void ctsu_diag_scan_start1 (ctsu_instance_ctrl_t * const p_instance_ctrl) +{ + if (CTSU_DIAG_INIT == g_ctsu_diag_info.state) + { + g_ctsu_diag_info.state = CTSU_DIAG_OVER_VOLTAGE; + } + + /* ctsu normal scan register save */ + g_ctsu_diag_reg.ctsucr0 = CTSU.CTSUCR0.BYTE; + g_ctsu_diag_reg.ctsucr1 = CTSU.CTSUCR1.BYTE; + g_ctsu_diag_reg.ctsusdprs = CTSU.CTSUSDPRS.BYTE; + g_ctsu_diag_reg.ctsusst = CTSU.CTSUSST.BYTE; + g_ctsu_diag_reg.ctsuchac0 = CTSU.CTSUCHAC0.BYTE; + g_ctsu_diag_reg.ctsuchac1 = CTSU.CTSUCHAC1.BYTE; +#if (BSP_FEATURE_CTSU_CTSUCHAC_REGISTER_COUNT >= 3) + g_ctsu_diag_reg.ctsuchac2 = CTSU.CTSUCHAC2.BYTE; +#endif + g_ctsu_diag_reg.ctsuchtrc0 = CTSU.CTSUCHTRC0.BYTE; + g_ctsu_diag_reg.ctsuchtrc1 = CTSU.CTSUCHTRC1.BYTE; +#if (BSP_FEATURE_CTSU_CTSUCHAC_REGISTER_COUNT >= 3) + g_ctsu_diag_reg.ctsuchtrc2 = CTSU.CTSUCHTRC2.BYTE; +#endif + g_ctsu_diag_reg.ctsudclkc = CTSU.CTSUDCLKC.BYTE; + g_ctsu_diag_reg.ctsuerrs = CTSU.CTSUERRS.WORD; + + /* scan register setting */ + if (CTSU_DIAG_OVER_VOLTAGE == g_ctsu_diag_info.state) + { + ctsu_diag_ldo_over_voltage_scan_start(); + } + + if (CTSU_DIAG_CCO_HIGH == g_ctsu_diag_info.state) + { + ctsu_diag_oscillator_high_scan_start(); + } + + if (CTSU_DIAG_CCO_LOW == g_ctsu_diag_info.state) + { + ctsu_diag_oscillator_low_scan_start(); + } + + if (CTSU_DIAG_SSCG == g_ctsu_diag_info.state) + { + ctsu_diag_sscg_scan_start(); + } + + if (CTSU_DIAG_DAC == g_ctsu_diag_info.state) + { + ctsu_diag_dac_scan_start(p_instance_ctrl); + } +} + +/*********************************************************************************************************************** + * ctsu_diag_data_get1 + ***********************************************************************************************************************/ +static fsp_err_t ctsu_diag_data_get1 (void) +{ + fsp_err_t err; + + /* data get */ + if (CTSU_DIAG_OVER_VOLTAGE == g_ctsu_diag_info.state) + { + ctsu_diag_ldo_over_voltage_data_get(); + + g_ctsu_diag_info.state = CTSU_DIAG_CCO_HIGH; + } + else if (CTSU_DIAG_CCO_HIGH == g_ctsu_diag_info.state) + { + ctsu_diag_oscillator_high_data_get(); + + g_ctsu_diag_info.state = CTSU_DIAG_CCO_LOW; + } + else if (CTSU_DIAG_CCO_LOW == g_ctsu_diag_info.state) + { + ctsu_diag_oscillator_low_data_get(); + + g_ctsu_diag_info.state = CTSU_DIAG_SSCG; + } + else if (CTSU_DIAG_SSCG == g_ctsu_diag_info.state) + { + ctsu_diag_sscg_data_get(); + + g_ctsu_diag_info.state = CTSU_DIAG_DAC; + } + else if (CTSU_DIAG_DAC == g_ctsu_diag_info.state) + { + ctsu_diag_dac_data_get(); + if (CTSU_TUNING_INCOMPLETE == g_ctsu_diag_info.tuning) + { + g_ctsu_diag_info.state = CTSU_DIAG_DAC; + } + else + { + g_ctsu_diag_info.loop_count++; + if (6 <= g_ctsu_diag_info.loop_count) + { + g_ctsu_diag_info.state = CTSU_DIAG_COMPLETE; + g_ctsu_diag_info.loop_count = 0; + } + } + } + else + { + } + + /* register restore */ + CTSU.CTSUCR0.BYTE = g_ctsu_diag_reg.ctsucr0; + CTSU.CTSUCR1.BYTE = g_ctsu_diag_reg.ctsucr1; + CTSU.CTSUSDPRS.BYTE = g_ctsu_diag_reg.ctsusdprs; + CTSU.CTSUSST.BYTE = g_ctsu_diag_reg.ctsusst; + CTSU.CTSUCHAC0.BYTE = g_ctsu_diag_reg.ctsuchac0; + CTSU.CTSUCHAC1.BYTE = g_ctsu_diag_reg.ctsuchac1; +#if (BSP_FEATURE_CTSU_CTSUCHAC_REGISTER_COUNT >= 3) + CTSU.CTSUCHAC2.BYTE = g_ctsu_diag_reg.ctsuchac2; +#endif + CTSU.CTSUCHTRC0.BYTE = g_ctsu_diag_reg.ctsuchtrc0; + CTSU.CTSUCHTRC1.BYTE = g_ctsu_diag_reg.ctsuchtrc1; +#if (BSP_FEATURE_CTSU_CTSUCHAC_REGISTER_COUNT >= 3) + CTSU.CTSUCHTRC2.BYTE = g_ctsu_diag_reg.ctsuchtrc2; +#endif + CTSU.CTSUDCLKC.BYTE = g_ctsu_diag_reg.ctsudclkc; + CTSU.CTSUERRS.WORD = 0; + + if (CTSU_DIAG_COMPLETE == g_ctsu_diag_info.state) + { + err = FSP_SUCCESS; + } + else + { + err = FSP_ERR_CTSU_DIAG_NOT_YET; + } + + return err; +} + +static void ctsu_diag_ldo_over_voltage_scan_start (void) +{ + uint32_t pclkb_mhz; + uint32_t ctsu_sdpa; + + /* Set power on */ + CTSU.CTSUCR1.BYTE = ((CTSU_CFG_LOW_VOLTAGE_MODE << 2) | (CTSU_CSW_ON << 1) | CTSU_PON_ON); + + /* Synchronous Noise Reduction Setting */ + CTSU.CTSUSDPRS.BYTE = ((CTSU_SOFF_ON << 6) | (CTSU_PRMODE_62_PULSES << 4) | CTSU_PRRATIO_RECOMMEND); + + /* High Pass Noise Reduction- ALWAYS 0x30 as per HW Manual */ + CTSU.CTSUDCLKC.BYTE = ((CTSU_SSCNT << 4) | CTSU_SSMOD); + + /* Sensor Stabilization- ALWAYS 0x10 as per HW Manual */ + CTSU.CTSUSST.BYTE = (CTSU_SST_RECOMMEND); + + /* Since CLK is rewritten by correction, set here. */ + CTSU.CTSUCR1.BYTE |= (uint8_t) (CTSU_CFG_PCLK_DIVISION << 4); + + pclkb_mhz = BSP_PCLKB_HZ / CTSU_PCLKB_FREQ_MHZ; + if (CTSU_PCLKB_FREQ_RANGE1 >= pclkb_mhz) + { + CTSU.CTSUCR1.BYTE |= (0 << 4); + ctsu_sdpa = pclkb_mhz - 1; + } + else if ((CTSU_PCLKB_FREQ_RANGE1 < pclkb_mhz) && (CTSU_PCLKB_FREQ_RANGE2 >= pclkb_mhz)) + { + CTSU.CTSUCR1.BYTE |= (1 << 4); + ctsu_sdpa = (pclkb_mhz / 2) - 1; + } + else + { + CTSU.CTSUCR1.BYTE |= (2 << 4); + ctsu_sdpa = (pclkb_mhz / 4) - 1; + } + + CTSU.CTSUCR1.BYTE |= (CTSU_MODE_SELF_MULTI_SCAN << 6); + CTSU.CTSUCHAC0.BYTE = 0x01; + CTSU.CTSUCHAC1.BYTE = 0x00; +#if (BSP_FEATURE_CTSU_CTSUCHAC_REGISTER_COUNT >= 3) + CTSU.CTSUCHAC2.BYTE = 0x00; +#endif +#if (BSP_FEATURE_CTSU_CTSUCHAC_REGISTER_COUNT >= 4) + CTSU.CTSUCHAC3.BYTE = 0x00; +#endif +#if (BSP_FEATURE_CTSU_CTSUCHAC_REGISTER_COUNT >= 5) + CTSU.CTSUCHAC4.BYTE = 0x00; +#endif + CTSU.CTSUCHTRC0.BYTE = 0x00; + CTSU.CTSUCHTRC1.BYTE = 0x00; +#if (BSP_FEATURE_CTSU_CTSUCHAC_REGISTER_COUNT >= 3) + CTSU.CTSUCHTRC2.BYTE = 0x00; +#endif +#if (BSP_FEATURE_CTSU_CTSUCHAC_REGISTER_COUNT >= 4) + CTSU.CTSUCHTRC3.BYTE = 0x00; +#endif +#if (BSP_FEATURE_CTSU_CTSUCHAC_REGISTER_COUNT >= 5) + CTSU.CTSUCHTRC4.BYTE = 0x00; +#endif + + /* Correction measurement setting */ + CTSU.CTSUERRS.BIT.CTSUTSOC = 1; + g_ctsu_diag_info.icomp = 0; + + g_ctsu_diag_info.ctsuwr.ctsussc = (CTSU_SSDIV_0500 << 8); + g_ctsu_diag_info.ctsuwr.ctsuso0 = CTSU_DIAG_DAC_SO_MAX; + g_ctsu_diag_info.ctsuwr.ctsuso1 = (uint16_t) ((CTSU_ICOG_66 << 13) | (ctsu_sdpa << 8) | CTSU_RICOA_RECOMMEND); +} + +static void ctsu_diag_ldo_over_voltage_data_get (void) +{ + /* Nothing */ +} + +static fsp_err_t ctsu_diag_ldo_over_voltage_result (void) +{ + if (1 != g_ctsu_diag_info.icomp) + { + return FSP_ERR_CTSU_DIAG_LDO_OVER_VOLTAGE; + } + + /* if all checks passed to this point, return success */ + return FSP_SUCCESS; +} + +static void ctsu_diag_oscillator_high_scan_start (void) +{ + uint32_t ctsu_sdpa; + uint32_t pclkb_mhz; + + /* Set power on */ + CTSU.CTSUCR1.BYTE = ((CTSU_CFG_LOW_VOLTAGE_MODE << 2) | (CTSU_CSW_ON << 1) | CTSU_PON_ON); + + /* Synchronous Noise Reduction Setting */ + CTSU.CTSUSDPRS.BYTE = ((CTSU_SOFF_ON << 6) | (CTSU_PRMODE_62_PULSES << 4) | CTSU_PRRATIO_RECOMMEND); + + /* High Pass Noise Reduction- ALWAYS 0x30 as per HW Manual */ + CTSU.CTSUDCLKC.BYTE = ((CTSU_SSCNT << 4) | CTSU_SSMOD); + + /* Sensor Stabilization- ALWAYS 0x10 as per HW Manual */ + CTSU.CTSUSST.BYTE = (CTSU_SST_RECOMMEND); + + /* Since CLK is rewritten by correction, set here. */ + CTSU.CTSUCR1.BYTE |= (uint8_t) (CTSU_CFG_PCLK_DIVISION << 4); + + pclkb_mhz = BSP_PCLKB_HZ / CTSU_PCLKB_FREQ_MHZ; + if (CTSU_PCLKB_FREQ_RANGE1 >= pclkb_mhz) + { + CTSU.CTSUCR1.BYTE |= (0 << 4); + ctsu_sdpa = pclkb_mhz - 1; + } + else if ((CTSU_PCLKB_FREQ_RANGE1 < pclkb_mhz) && (CTSU_PCLKB_FREQ_RANGE2 >= pclkb_mhz)) + { + CTSU.CTSUCR1.BYTE |= (1 << 4); + ctsu_sdpa = (pclkb_mhz / 2) - 1; + } + else + { + CTSU.CTSUCR1.BYTE |= (2 << 4); + ctsu_sdpa = (pclkb_mhz / 4) - 1; + } + + CTSU.CTSUCR1.BYTE |= (CTSU_MODE_SELF_MULTI_SCAN << 6); + CTSU.CTSUCHAC0.BYTE = 0x01; + CTSU.CTSUCHAC1.BYTE = 0x00; +#if (BSP_FEATURE_CTSU_CTSUCHAC_REGISTER_COUNT >= 3) + CTSU.CTSUCHAC2.BYTE = 0x00; +#endif +#if (BSP_FEATURE_CTSU_CTSUCHAC_REGISTER_COUNT >= 4) + CTSU.CTSUCHAC3.BYTE = 0x00; +#endif +#if (BSP_FEATURE_CTSU_CTSUCHAC_REGISTER_COUNT >= 5) + CTSU.CTSUCHAC4.BYTE = 0x00; +#endif + CTSU.CTSUCHTRC0.BYTE = 0x00; + CTSU.CTSUCHTRC1.BYTE = 0x00; +#if (BSP_FEATURE_CTSU_CTSUCHAC_REGISTER_COUNT >= 3) + CTSU.CTSUCHTRC2.BYTE = 0x00; +#endif +#if (BSP_FEATURE_CTSU_CTSUCHAC_REGISTER_COUNT >= 4) + CTSU.CTSUCHTRC3.BYTE = 0x00; +#endif +#if (BSP_FEATURE_CTSU_CTSUCHAC_REGISTER_COUNT >= 5) + CTSU.CTSUCHTRC4.BYTE = 0x00; +#endif + + CTSU.CTSUSO0.WORD = 0; + + g_ctsu_diag_info.ctsuwr.ctsussc = (CTSU_SSDIV_0500 << 8); + g_ctsu_diag_info.ctsuwr.ctsuso0 = 0x0000; + g_ctsu_diag_info.ctsuwr.ctsuso1 = (uint16_t) ((CTSU_ICOG_66 << 13) | (ctsu_sdpa << 8) | CTSU_RICOA_RECOMMEND); + + /* Correction measurement setting */ + CTSU.CTSUERRS.BIT.CTSUSPMD = 2; // 0x82 + CTSU.CTSUERRS.BIT.CTSUTSOC = 1; + R_BSP_SoftwareDelay(10, BSP_DELAY_MILLISECS); +} + +static void ctsu_diag_oscillator_high_data_get (void) +{ + g_ctsu_diag_info.cco_high = g_ctsu_diag_info.scanbuf.sen; +} + +static fsp_err_t ctsu_diag_oscillator_high_result (void) +{ + if ((g_ctsu_diag_info.cco_high < CTSU_CFG_DIAG_CCO_HIGH_MAX) && + (g_ctsu_diag_info.cco_high > CTSU_CFG_DIAG_CCO_HIGH_MIN)) + { + } + else + { + return FSP_ERR_CTSU_DIAG_CCO_HIGH; + } + + /* if all checks passed to this point, return success */ + return FSP_SUCCESS; +} + +static void ctsu_diag_oscillator_low_scan_start (void) +{ + uint32_t ctsu_sdpa; + uint32_t pclkb_mhz; + + /* Set power on */ + CTSU.CTSUCR1.BYTE = ((CTSU_CFG_LOW_VOLTAGE_MODE << 2) | (CTSU_CSW_ON << 1) | CTSU_PON_ON); + + /* Synchronous Noise Reduction Setting */ + CTSU.CTSUSDPRS.BYTE = ((CTSU_SOFF_ON << 6) | (CTSU_PRMODE_62_PULSES << 4) | CTSU_PRRATIO_RECOMMEND); + + /* High Pass Noise Reduction- ALWAYS 0x30 as per HW Manual */ + CTSU.CTSUDCLKC.BYTE = ((CTSU_SSCNT << 4) | CTSU_SSMOD); + + /* Sensor Stabilization- ALWAYS 0x10 as per HW Manual */ + CTSU.CTSUSST.BYTE = (CTSU_SST_RECOMMEND); + + /* Since CLK is rewritten by correction, set here. */ + CTSU.CTSUCR1.BYTE |= (uint8_t) (CTSU_CFG_PCLK_DIVISION << 4); + + pclkb_mhz = BSP_PCLKB_HZ / CTSU_PCLKB_FREQ_MHZ; + if (CTSU_PCLKB_FREQ_RANGE1 >= pclkb_mhz) + { + CTSU.CTSUCR1.BYTE |= (0 << 4); + ctsu_sdpa = pclkb_mhz - 1; + } + else if ((CTSU_PCLKB_FREQ_RANGE1 < pclkb_mhz) && (CTSU_PCLKB_FREQ_RANGE2 >= pclkb_mhz)) + { + CTSU.CTSUCR1.BYTE |= (1 << 4); + ctsu_sdpa = (pclkb_mhz / 2) - 1; + } + else + { + CTSU.CTSUCR1.BYTE |= (2 << 4); + ctsu_sdpa = (pclkb_mhz / 4) - 1; + } + + CTSU.CTSUCR1.BYTE |= (CTSU_MODE_SELF_MULTI_SCAN << 6); + CTSU.CTSUCHAC0.BYTE = 0x01; + CTSU.CTSUCHAC1.BYTE = 0x00; +#if (BSP_FEATURE_CTSU_CTSUCHAC_REGISTER_COUNT >= 3) + CTSU.CTSUCHAC2.BYTE = 0x00; +#endif +#if (BSP_FEATURE_CTSU_CTSUCHAC_REGISTER_COUNT >= 4) + CTSU.CTSUCHAC3.BYTE = 0x00; +#endif +#if (BSP_FEATURE_CTSU_CTSUCHAC_REGISTER_COUNT >= 5) + CTSU.CTSUCHAC4.BYTE = 0x00; +#endif + CTSU.CTSUCHTRC0.BYTE = 0x00; + CTSU.CTSUCHTRC1.BYTE = 0x00; +#if (BSP_FEATURE_CTSU_CTSUCHAC_REGISTER_COUNT >= 3) + CTSU.CTSUCHTRC2.BYTE = 0x00; +#endif +#if (BSP_FEATURE_CTSU_CTSUCHAC_REGISTER_COUNT >= 4) + CTSU.CTSUCHTRC3.BYTE = 0x00; +#endif +#if (BSP_FEATURE_CTSU_CTSUCHAC_REGISTER_COUNT >= 5) + CTSU.CTSUCHTRC4.BYTE = 0x00; +#endif + + CTSU.CTSUSO0.WORD = 0; + + g_ctsu_diag_info.ctsuwr.ctsussc = (CTSU_SSDIV_0500 << 8); + g_ctsu_diag_info.ctsuwr.ctsuso0 = 0x0000; + g_ctsu_diag_info.ctsuwr.ctsuso1 = (uint16_t) ((CTSU_ICOG_66 << 13) | (ctsu_sdpa << 8) | CTSU_RICOA_RECOMMEND); + + /* Correction measurement setting */ + CTSU.CTSUERRS.BIT.CTSUSPMD = 0; // 0x80 + CTSU.CTSUERRS.BIT.CTSUTSOC = 1; + R_BSP_SoftwareDelay(10, BSP_DELAY_MILLISECS); +} + +static void ctsu_diag_oscillator_low_data_get (void) +{ + g_ctsu_diag_info.cco_low = g_ctsu_diag_info.scanbuf.sen; +} + +static fsp_err_t ctsu_diag_oscillator_low_result (void) +{ + if ((g_ctsu_diag_info.cco_low < CTSU_CFG_DIAG_CCO_LOW_MAX) && + (g_ctsu_diag_info.cco_low > CTSU_CFG_DIAG_CCO_LOW_MIN)) + { + } + else + { + return FSP_ERR_CTSU_DIAG_CCO_LOW; + } + + /* if all checks passed to this point, return success */ + return FSP_SUCCESS; +} + +static void ctsu_diag_sscg_scan_start (void) +{ + uint32_t ctsu_sdpa; + uint32_t pclkb_mhz; + + /* Set power on */ + CTSU.CTSUCR1.BYTE = ((CTSU_CFG_LOW_VOLTAGE_MODE << 2) | (CTSU_CSW_ON << 1) | CTSU_PON_ON); + + /* Synchronous Noise Reduction Setting */ + CTSU.CTSUSDPRS.BYTE = ((CTSU_SOFF_ON << 6) | (CTSU_PRMODE_62_PULSES << 4) | CTSU_PRRATIO_RECOMMEND); + + /* High Pass Noise Reduction- ALWAYS 0x30 as per HW Manual */ + CTSU.CTSUDCLKC.BYTE = ((CTSU_SSCNT << 4) | CTSU_SSMOD); + + /* Sensor Stabilization- ALWAYS 0x10 as per HW Manual */ + CTSU.CTSUSST.BYTE = (CTSU_SST_RECOMMEND); + + /* Since CLK is rewritten by correction, set here. */ + CTSU.CTSUCR1.BYTE |= (uint8_t) (CTSU_CFG_PCLK_DIVISION << 4); + + pclkb_mhz = BSP_PCLKB_HZ / CTSU_PCLKB_FREQ_MHZ; + if (CTSU_PCLKB_FREQ_RANGE1 >= pclkb_mhz) + { + CTSU.CTSUCR1.BYTE |= (0 << 4); + ctsu_sdpa = pclkb_mhz - 1; + } + else if ((CTSU_PCLKB_FREQ_RANGE1 < pclkb_mhz) && (CTSU_PCLKB_FREQ_RANGE2 >= pclkb_mhz)) + { + CTSU.CTSUCR1.BYTE |= (1 << 4); + ctsu_sdpa = (pclkb_mhz / 2) - 1; + } + else + { + CTSU.CTSUCR1.BYTE |= (2 << 4); + ctsu_sdpa = (pclkb_mhz / 4) - 1; + } + + CTSU.CTSUCR1.BYTE |= (CTSU_MODE_SELF_MULTI_SCAN << 6); + + CTSU.CTSUCHAC0.BYTE = 0x01; + CTSU.CTSUCHAC1.BYTE = 0x00; +#if (BSP_FEATURE_CTSU_CTSUCHAC_REGISTER_COUNT >= 3) + CTSU.CTSUCHAC2.BYTE = 0x00; +#endif +#if (BSP_FEATURE_CTSU_CTSUCHAC_REGISTER_COUNT >= 4) + CTSU.CTSUCHAC3.BYTE = 0x00; +#endif +#if (BSP_FEATURE_CTSU_CTSUCHAC_REGISTER_COUNT >= 5) + CTSU.CTSUCHAC4.BYTE = 0x00; +#endif + CTSU.CTSUCHTRC0.BYTE = 0x00; + CTSU.CTSUCHTRC1.BYTE = 0x00; +#if (BSP_FEATURE_CTSU_CTSUCHAC_REGISTER_COUNT >= 3) + CTSU.CTSUCHTRC2.BYTE = 0x00; +#endif +#if (BSP_FEATURE_CTSU_CTSUCHAC_REGISTER_COUNT >= 4) + CTSU.CTSUCHTRC3.BYTE = 0x00; +#endif +#if (BSP_FEATURE_CTSU_CTSUCHAC_REGISTER_COUNT >= 5) + CTSU.CTSUCHTRC4.BYTE = 0x00; +#endif + + CTSU.CTSUSO0.WORD = 0; + + g_ctsu_diag_info.ctsuwr.ctsussc = (CTSU_SSDIV_0500 << 8); + g_ctsu_diag_info.ctsuwr.ctsuso0 = 0x0000; + g_ctsu_diag_info.ctsuwr.ctsuso1 = (uint16_t) ((CTSU_ICOG_66 << 13) | (ctsu_sdpa << 8) | CTSU_RICOA_RECOMMEND); + + /* Correction measurement setting */ + CTSU.CTSUERRS.BIT.CTSUSPMD = 0; // 0x80 + CTSU.CTSUERRS.BIT.CTSUTSOC = 1; + + CTSU.CTSUERRS.WORD = CTSU_DIAG_TSOC_BIT | CTSU_DIAG_CLKSEL1_BIT; +} + +static void ctsu_diag_sscg_data_get (void) +{ + g_ctsu_diag_info.sscg = g_ctsu_diag_info.scanbuf.ref; +} + +static fsp_err_t ctsu_diag_sscg_result (void) +{ + if ((g_ctsu_diag_info.sscg > CTSU_CFG_DIAG_SSCG_MAX) || (g_ctsu_diag_info.sscg < CTSU_CFG_DIAG_SSCG_MIN)) + { + return FSP_ERR_CTSU_DIAG_SSCG; + } + + /* if all checks passed to this point, return success */ + return FSP_SUCCESS; +} + +static void ctsu_diag_dac_initial_tuning (void) +{ + int32_t diff = 0; + uint32_t complete_flag = 0; + uint16_t ctsuso; + + diff = g_ctsu_diag_info.correct_data - CTSU_DIAG_DAC_TARGET_VALUE; + + ctsuso = g_ctsu_diag_info.ctsuwr.ctsuso0 & CTSU_TUNING_MAX; + if (0 < diff) + { + if (g_ctsu_diag_info.tuning_diff < 0) + { + if ((-diff) > g_ctsu_diag_info.tuning_diff) + { + ctsuso++; + } + + complete_flag = 1; + } + else + { + if (CTSU_TUNING_MAX == ctsuso) + { + complete_flag = 1; + } + else + { + ctsuso++; + g_ctsu_diag_info.tuning_diff = diff; + } + } + } + else if (0 == diff) + { + complete_flag = 1; + } + else + { + if (g_ctsu_diag_info.tuning_diff > 0) + { + if ((-diff) > g_ctsu_diag_info.tuning_diff) + { + ctsuso--; + } + + complete_flag = 1; + } + else + { + complete_flag = 1; + } + } + + g_ctsu_diag_info.ctsuwr.ctsuso0 &= (uint16_t) (~CTSU_TUNING_MAX); + g_ctsu_diag_info.ctsuwr.ctsuso0 |= ctsuso; + + if (CTSU_CFG_NUM_SUMULTI == complete_flag) + { + g_ctsu_diag_info.tuning_diff = 0; + g_ctsu_diag_info.so0_4uc_val = ctsuso; + g_ctsu_diag_info.dac_init = 3; + g_ctsu_diag_info.tuning = CTSU_TUNING_COMPLETE; + g_ctsu_diag_info.loop_count = 0; + g_ctsu_diag_info.dac_cnt[0] = g_ctsu_diag_info.correct_data; + } +} + +static void ctsu_diag_dac_scan_start (ctsu_instance_ctrl_t * const p_instance_ctrl) +{ + uint8_t temp; + + /* Set power on */ + CTSU.CTSUCR1.BYTE = ((CTSU_CFG_LOW_VOLTAGE_MODE << 2) | (CTSU_CSW_ON << 1) | CTSU_PON_ON); + + /* Synchronous Noise Reduction Setting */ + CTSU.CTSUSDPRS.BYTE = ((CTSU_SOFF_ON << 6) | (CTSU_PRMODE_62_PULSES << 4) | CTSU_PRRATIO_RECOMMEND); + + /* High Pass Noise Reduction- ALWAYS 0x30 as per HW Manual */ + CTSU.CTSUDCLKC.BYTE = ((CTSU_SSCNT << 4) | CTSU_SSMOD); + + /* Sensor Stabilization- ALWAYS 0x10 as per HW Manual */ + CTSU.CTSUSST.BYTE = (CTSU_SST_RECOMMEND); + + /* Since CLK is rewritten by correction, set here. */ + CTSU.CTSUCR1.BYTE |= (uint8_t) (CTSU_CFG_PCLK_DIVISION << 4); + + temp = (uint8_t) (CTSU.CTSUCR1.BYTE & ~(CTSU_CR1_MODIFY_BIT)); + CTSU.CTSUCR1.BYTE = (uint8_t) (temp | (p_instance_ctrl->ctsucr1 & CTSU_CR1_MODIFY_BIT) | CTSU_DAC_TEST_ATUNE1); // MD1, MD0, ATUNE1=1 + #if BSP_FEATURE_CTSU_HAS_TXVSEL + CTSU.CTSUCR0.BIT.CTSUTXVSEL = p_instance_ctrl->p_ctsu_cfg->txvsel; + #endif + + /* Write Channel setting */ + #if (CTSU_CFG_DIAG_DAC_TS >= 0) && (CTSU_CFG_DIAG_DAC_TS < 8) + CTSU.CTSUCHAC0.BYTE = 1 << CTSU_CFG_DIAG_DAC_TS; + #else + CTSU.CTSUCHAC0.BYTE = 0; + #endif + #if (CTSU_CFG_DIAG_DAC_TS >= 8) && (CTSU_CFG_DIAG_DAC_TS < 16) + CTSU.CTSUCHAC1.BYTE = 1 << (CTSU_CFG_DIAG_DAC_TS - 8); + #else + CTSU.CTSUCHAC1.BYTE = 0; + #endif + #if (BSP_FEATURE_CTSU_CTSUCHAC_REGISTER_COUNT >= 3) + #if (CTSU_CFG_DIAG_DAC_TS >= 16) && (CTSU_CFG_DIAG_DAC_TS < 24) + CTSU.CTSUCHAC2.BYTE = 1 << (CTSU_CFG_DIAG_DAC_TS - 16); + #else + CTSU.CTSUCHAC2.BYTE = 0; + #endif + #endif + #if (BSP_FEATURE_CTSU_CTSUCHAC_REGISTER_COUNT >= 4) + #if (CTSU_CFG_DIAG_DAC_TS >= 24) && (CTSU_CFG_DIAG_DAC_TS < 32) + CTSU.CTSUCHAC3.BYTE = 1 << (CTSU_CFG_DIAG_DAC_TS - 24); + #else + CTSU.CTSUCHAC3.BYTE = 0; + #endif + #endif + #if (BSP_FEATURE_CTSU_CTSUCHAC_REGISTER_COUNT >= 5) + #if (CTSU_CFG_DIAG_DAC_TS >= 32) + CTSU.CTSUCHAC4.BYTE = 1 << (CTSU_CFG_DIAG_DAC_TS - 32); + #else + CTSU.CTSUCHAC4.BYTE = 0; + #endif + #endif + CTSU.CTSUCHTRC0.BYTE = 0; + CTSU.CTSUCHTRC1.BYTE = 0; + #if (BSP_FEATURE_CTSU_CTSUCHAC_REGISTER_COUNT >= 3) + CTSU.CTSUCHTRC2.BYTE = 0; + #endif + #if (BSP_FEATURE_CTSU_CTSUCHAC_REGISTER_COUNT >= 4) + CTSU.CTSUCHTRC3.BYTE = 0; + #endif + #if (BSP_FEATURE_CTSU_CTSUCHAC_REGISTER_COUNT >= 5) + CTSU.CTSUCHTRC4.BYTE = 0; + #endif + + g_ctsu_diag_info.ctsuwr.ctsussc = (uint16_t) (CTSU_SSDIV_1330 << 8); + g_ctsu_diag_info.ctsuwr.ctsuso1 = (uint16_t) ((CTSU_ICOG_RECOMMEND << 13) | (7 << 8) | CTSU_RICOA_RECOMMEND); + + if (g_ctsu_diag_info.dac_init > 2) + { + /* Apply DAC current */ + if (0 == g_ctsu_diag_info.loop_count) + { + g_ctsu_diag_info.ctsuwr.ctsuso0 = (uint16_t) ((3 << 10) | g_ctsu_diag_info.so0_4uc_val); + } + else if (1 == g_ctsu_diag_info.loop_count) + { + g_ctsu_diag_info.ctsuwr.ctsuso0 = + (uint16_t) ((3 << 10) | (g_ctsu_diag_info.so0_4uc_val - CTSU_DIAG_DAC_1UC)); + } + else if (2 == g_ctsu_diag_info.loop_count) + { + g_ctsu_diag_info.ctsuwr.ctsuso0 = + (uint16_t) ((3 << 10) | (g_ctsu_diag_info.so0_4uc_val - CTSU_DIAG_DAC_2UC)); + } + else if (3 == g_ctsu_diag_info.loop_count) + { + g_ctsu_diag_info.ctsuwr.ctsuso0 = + (uint16_t) ((3 << 10) | (g_ctsu_diag_info.so0_4uc_val - CTSU_DIAG_DAC_4UC)); + } + else if (4 == g_ctsu_diag_info.loop_count) + { + g_ctsu_diag_info.ctsuwr.ctsuso0 = + (uint16_t) ((3 << 10) | (g_ctsu_diag_info.so0_4uc_val - CTSU_DIAG_DAC_8UC)); + } + else if (5 == g_ctsu_diag_info.loop_count) + { + g_ctsu_diag_info.ctsuwr.ctsuso0 = + (uint16_t) ((3 << 10) | (g_ctsu_diag_info.so0_4uc_val - CTSU_DIAG_DAC_16UC)); + } + else + { + } + } + + if (g_ctsu_diag_info.dac_init == 0) + { + g_ctsu_diag_info.dac_init = 1; + g_ctsu_diag_info.so0_4uc_val = 0; + g_ctsu_diag_info.tuning_diff = 0; + g_ctsu_diag_info.ctsuwr.ctsuso0 = (3 << 10) + CTSU_DIAG_DAC_START_VALUE; + } +} + +static void ctsu_diag_dac_data_get (void) +{ + ctsu_correction_calc_t calc; + + calc.snum = (g_ctsu_diag_info.ctsuwr.ctsuso0 >> 10) & CTSU_SNUM_MAX; + + if (CTSU_DIAG_DAC == g_ctsu_diag_info.state) + { + calc.snum = 3; + } + + calc.sdpa = (g_ctsu_diag_info.ctsuwr.ctsuso1 >> 8) & CTSU_SDPA_MAX; + + /* Correction process */ + ctsu_correction_calc(&g_ctsu_diag_info.correct_data, g_ctsu_diag_info.scanbuf.sen, &calc); + + if (g_ctsu_diag_info.tuning == CTSU_TUNING_COMPLETE) + { + g_ctsu_diag_info.dac_cnt[g_ctsu_diag_info.loop_count] = g_ctsu_diag_info.correct_data; + } + else + { + ctsu_diag_dac_initial_tuning(); + } +} + +static fsp_err_t ctsu_diag_dac_result (void) +{ + uint8_t k; + if (g_ctsu_diag_info.tuning == CTSU_TUNING_COMPLETE) + { + for (k = 0; k < 6; k++) + { + if ((g_ctsu_diag_info.dac_cnt[k] > dac_oscil_table[k][0]) || + (g_ctsu_diag_info.dac_cnt[k] < dac_oscil_table[k][1])) + { + return FSP_ERR_CTSU_DIAG_DAC; + } + } + } + + /* if all checks passed to this point, return success */ + return FSP_SUCCESS; +} + + #endif +#endif + +#if (BSP_FEATURE_CTSU_VERSION == 2) + #if (CTSU_CFG_DIAG_SUPPORT_ENABLE == 1) + +/*********************************************************************************************************************** + * ctsu_diag_scan_start2 + ***********************************************************************************************************************/ +static fsp_err_t ctsu_diag_scan_start2 (ctsu_instance_ctrl_t * const p_instance_ctrl) +{ + fsp_err_t err = FSP_SUCCESS; + + /* initial state change*/ + if (CTSU_DIAG_INIT == g_ctsu_diag_info.state) + { + g_ctsu_diag_info.state = CTSU_DIAG_OUTPUT_VOLTAGE; + } + + /* ctsu normal scan register save */ + ctsu_diag_regi_store2(); + + /* scan register setting */ + if (CTSU_DIAG_OUTPUT_VOLTAGE == g_ctsu_diag_info.state) + { + err = ctsu_diag_output_voltage_scan_start(p_instance_ctrl); + if (FSP_SUCCESS == err) + { + g_ctsu_diag_info.state = CTSU_DIAG_OVER_VOLTAGE; + err = FSP_SUCCESS; + } + else + { + /* if ADC is already opened */ + if (FSP_ERR_ALREADY_OPEN != err) + { + R_ADC_Close(0); + } + err = FSP_SUCCESS; + + g_ctsu_diag_info.state = CTSU_DIAG_OUTPUT_VOLTAGE; + CTSU.CTSUCRA.BIT.ATUNE1 = 0; + CTSU.CTSUCRA.BIT.ATUNE2 = 0; + CTSU.CTSUCRA.BIT.LOAD = 0; + CTSU.CTSUMCH.BIT.MCA0 = 1; + CTSU.CTSUMCH.BIT.MCA1 = 0; + CTSU.CTSUMCH.BIT.MCA2 = 0; + CTSU.CTSUMCH.BIT.MCA3 = 0; + CTSU.CTSUCHACA.LONG = g_ctsu_diag_info.chaca; + CTSU.CTSUCHACB.LONG = g_ctsu_diag_info.chacb; + CTSU.CTSUCHTRCA.LONG = 0; + CTSU.CTSUCHTRCB.LONG = 0; + } + } + + if (CTSU_DIAG_OVER_VOLTAGE == g_ctsu_diag_info.state) + { + ctsu_diag_over_voltage_scan_start(); + g_ctsu_diag_info.state = CTSU_DIAG_OVER_CURRENT; + } + + if (CTSU_DIAG_OVER_CURRENT == g_ctsu_diag_info.state) + { + ctsu_diag_over_current_scan_start(); + } + + if (CTSU_DIAG_LOAD_RESISTANCE == g_ctsu_diag_info.state) + { + ctsu_diag_load_resistance_scan_start(); + } + + if (CTSU_DIAG_CURRENT_SOURCE == g_ctsu_diag_info.state) + { + ctsu_diag_current_source_scan_start(); + } + + if (CTSU_DIAG_SENSCLK == g_ctsu_diag_info.state) + { + ctsu_diag_cco_gain_scan_start(); + } + + if (CTSU_DIAG_SUCLK == g_ctsu_diag_info.state) + { + ctsu_diag_cco_gain_scan_start(); + } + + if (CTSU_DIAG_CLOCK_RECOVERY == g_ctsu_diag_info.state) + { + ctsu_diag_clock_recovery_scan_start(); + } + + #if (CTSU_CFG_NUM_CFC != 0) + if (CTSU_DIAG_CFC == g_ctsu_diag_info.state) + { + ctsu_diag_cfc_gain_scan_start(); + } + #endif + + return err; +} + +/*********************************************************************************************************************** + * ctsu_diag_data_get2 + ***********************************************************************************************************************/ +static fsp_err_t ctsu_diag_data_get2 (uint16_t * p_data) +{ + fsp_err_t err; + + if (CTSU_DIAG_LOAD_RESISTANCE == g_ctsu_diag_info.state) + { + ctsu_diag_load_resistance_data_get(); + } + + if (CTSU_DIAG_CURRENT_SOURCE == g_ctsu_diag_info.state) + { + ctsu_diag_current_source_data_get(); + } + + if (CTSU_DIAG_SENSCLK == g_ctsu_diag_info.state) + { + ctsu_diag_cco_gain_data_get(); + } + + if (CTSU_DIAG_SUCLK == g_ctsu_diag_info.state) + { + ctsu_diag_cco_gain_data_get(); + } + + if (CTSU_DIAG_CLOCK_RECOVERY == g_ctsu_diag_info.state) + { + ctsu_diag_clock_recovery_data_get(); + } + + #if (CTSU_CFG_NUM_CFC != 0) + if (CTSU_DIAG_CFC == g_ctsu_diag_info.state) + { + ctsu_diag_cfc_gain_data_get(); + } + #endif + + /* DIagnosis state transition */ + if (CTSU_DIAG_OVER_CURRENT == g_ctsu_diag_info.state) + { + g_ctsu_diag_info.loop_count = 0; + g_ctsu_diag_info.state = CTSU_DIAG_LOAD_RESISTANCE; + } + else if ((CTSU_DIAG_LOAD_RESISTANCE == g_ctsu_diag_info.state) && (CTSU_RANGE_NUM <= g_ctsu_diag_info.loop_count)) + { + g_ctsu_diag_info.loop_count = 0; + g_ctsu_diag_info.state = CTSU_DIAG_CURRENT_SOURCE; + } + else if ((CTSU_DIAG_CURRENT_SOURCE == g_ctsu_diag_info.state) && + ((CTSU_DIAG_HIGH_CURRENT_SOURCE + CTSU_DIAG_LOW_CURRENT_SOURCE) <= g_ctsu_diag_info.loop_count)) + { + g_ctsu_diag_info.loop_count = 0; + g_ctsu_diag_info.state = CTSU_DIAG_SENSCLK; + } + else if ((CTSU_DIAG_SENSCLK == g_ctsu_diag_info.state) && + (CTSU_CORRECTION_POINT_NUM <= g_ctsu_diag_info.loop_count)) + { + g_ctsu_diag_info.loop_count = 0; + g_ctsu_diag_info.state = CTSU_DIAG_SUCLK; + } + else if ((CTSU_DIAG_SUCLK == g_ctsu_diag_info.state) && (CTSU_CORRECTION_POINT_NUM <= g_ctsu_diag_info.loop_count)) + { + g_ctsu_diag_info.loop_count = 0; + g_ctsu_diag_info.state = CTSU_DIAG_CLOCK_RECOVERY; + } + else if (CTSU_DIAG_CLOCK_RECOVERY == g_ctsu_diag_info.state) + { + #if (CTSU_CFG_NUM_CFC != 0) + g_ctsu_diag_info.state = CTSU_DIAG_CFC; + #else + g_ctsu_diag_info.state = CTSU_DIAG_COMPLETE; + #endif + } + + #if (CTSU_CFG_NUM_CFC != 0) + else if ((CTSU_DIAG_CFC == g_ctsu_diag_info.state) && (CTSU_CORRCFC_POINT_NUM <= g_ctsu_diag_info.loop_count)) + { + g_ctsu_diag_info.loop_count = 0; + g_ctsu_diag_info.state = CTSU_DIAG_COMPLETE; + } + #endif + else + { + } + + if ((CTSU_DIAG_OUTPUT_VOLTAGE <= g_ctsu_diag_info.state) && (CTSU_DIAG_OVER_CURRENT >= g_ctsu_diag_info.state)) + { + *p_data = g_ctsu_diag_info.output_voltage_cnt[0]; + } + else + { + /* Indicates that ADC measurement was not performed. */ + *p_data = CTSU_COUNT_MAX; + } + + /* register restore */ + ctsu_diag_regi_restore2(); + + if (CTSU_DIAG_COMPLETE == g_ctsu_diag_info.state) + { + err = FSP_SUCCESS; + } + else if (CTSU_DIAG_OUTPUT_VOLTAGE == g_ctsu_diag_info.state) + { + err = FSP_ERR_ABORTED; + g_ctsu_diag_info.state = CTSU_DIAG_INIT; + } + else + { + err = FSP_ERR_CTSU_DIAG_NOT_YET; + } + + return err; +} + +static void ctsu_diag_regi_store2 (void) +{ + g_ctsu_diag_reg.ctsucra = CTSU.CTSUCRA.LONG; + g_ctsu_diag_reg.ctsucrb = CTSU.CTSUCRB.LONG; + g_ctsu_diag_reg.ctsuchaca = CTSU.CTSUCHACA.LONG; + g_ctsu_diag_reg.ctsuchacb = CTSU.CTSUCHACB.LONG; + g_ctsu_diag_reg.ctsuchtrca = CTSU.CTSUCHTRCA.LONG; + g_ctsu_diag_reg.ctsuchtrcb = CTSU.CTSUCHTRCB.LONG; + g_ctsu_diag_reg.ctsumch = CTSU.CTSUMCH.LONG; + g_ctsu_diag_reg.ctsucalib = CTSU.CTSUCALIB.LONG; + g_ctsu_diag_reg.ctsusuclka = CTSU.CTSUSUCLKA.LONG; + g_ctsu_diag_reg.ctsusuclkb = CTSU.CTSUSUCLKB.LONG; +} + +static void ctsu_diag_regi_restore2 (void) +{ + /* register restore */ + CTSU.CTSUCRA.LONG = g_ctsu_diag_reg.ctsucra; + CTSU.CTSUCRB.LONG = g_ctsu_diag_reg.ctsucrb; + CTSU.CTSUCHACA.LONG = g_ctsu_diag_reg.ctsuchaca; + CTSU.CTSUCHACB.LONG = g_ctsu_diag_reg.ctsuchacb; + CTSU.CTSUCHTRCA.LONG = g_ctsu_diag_reg.ctsuchtrca; + CTSU.CTSUCHTRCB.LONG = g_ctsu_diag_reg.ctsuchtrcb; + CTSU.CTSUMCH.LONG = g_ctsu_diag_reg.ctsumch; + CTSU.CTSUCALIB.LONG = g_ctsu_diag_reg.ctsucalib; + CTSU.CTSUSUCLKA.LONG = g_ctsu_diag_reg.ctsusuclka; + CTSU.CTSUSUCLKB.LONG = g_ctsu_diag_reg.ctsusuclkb; +} + +static fsp_err_t ctsu_diag_output_voltage_scan_start (ctsu_instance_ctrl_t * const p_instance_ctrl) +{ + uint8_t k; + adc_cfg_t open_cfg; + adc_ch_cfg_t ctrl_cfg; + fsp_err_t err; + adc_err_t poll; + + /* TSCAP AD/C AN008 Setting */ + CTSU.CTSUADCC.LONG = 1; + + /* Setting for A/D convert mode */ + open_cfg.conv_speed = ADC_CONVERT_SPEED_DEFAULT; + open_cfg.alignment = ADC_ALIGN_RIGHT; + open_cfg.add_cnt = ADC_ADD_OFF; /* addition is turned off for chans/sensors */ + open_cfg.clearing = ADC_CLEAR_AFTER_READ_OFF; + open_cfg.trigger = ADC_TRIG_SOFTWARE; /* Software trigger */ + open_cfg.trigger_groupb = ADC_TRIG_NONE; /* Trigger none when without group mode */ + open_cfg.priority = 0; /* S12ADIO no interrupt(polling) */ + open_cfg.priority_groupb = 0; /* S12GBADI no interrupt */ + err = (fsp_err_t) R_ADC_Open (0, ADC_MODE_SS_ONE_CH, &open_cfg, FIT_NO_FUNC); + if (ADC_ERR_AD_NOT_CLOSED == (adc_err_t) err) + { + return FSP_ERR_ALREADY_OPEN; + } + FSP_ERROR_RETURN(FSP_SUCCESS == err, err); + + /* choice A/D port(allow A/D convert) */ + ctrl_cfg.chan_mask = ADC_MASK_CH8; + ctrl_cfg.chan_mask_groupb = ADC_MASK_GROUPB_OFF; + ctrl_cfg.priority_groupa = ADC_GRPA_PRIORITY_OFF; + ctrl_cfg.diag_method = ADC_DIAG_OFF; + ctrl_cfg.add_mask = ADC_MASK_ADD_OFF; + ctrl_cfg.signal_elc = ADC_ELC_ALL_SCANS_DONE; + err = (fsp_err_t) R_ADC_Control (0, ADC_CMD_ENABLE_CHANS, &ctrl_cfg); + FSP_ERROR_RETURN(FSP_SUCCESS == err, err); + + /* CTSU setting */ + CTSU.CTSUCRA.BIT.PUMPON = 1; + CTSU.CTSUCRA.BIT.CSW = 1; + CTSU.CTSUCRA.BIT.PON = 1; + CTSU.CTSUCALIB.LONG = 0; + + /* Self single scan mode */ + CTSU.CTSUCRA.BIT.MD0 = 0; + CTSU.CTSUCRA.BIT.MD1 = 0; + CTSU.CTSUSO.LONG = CTSU_DIAG_DAC_INIT_0; + CTSU.CTSUSO.LONG = CTSU_DIAG_DAC_INIT_1; + CTSU.CTSUSO.LONG = CTSU_DIAG_DAC_INIT_2; + CTSU.CTSUSO.LONG = CTSU_DIAG_DAC_INIT_3; + + /* LDO setting */ + CTSU.CTSUCALIB.BIT.DRV = 0; + + for (k = 0; k < 8; k++) + { + switch (k) + { + case 0: + { + /*select normal mode */ + CTSU.CTSUCRA.BIT.LOAD = 0; + + /* LDO Setting (20uA)*/ + CTSU.CTSUCRA.BIT.ATUNE1 = 0; + CTSU.CTSUCRA.BIT.ATUNE2 = 1; + break; + } + + case 1: + { + /*select normal mode */ + CTSU.CTSUCRA.BIT.LOAD = 0; + + /* LDO Setting (40uA) */ + CTSU.CTSUCRA.BIT.ATUNE1 = 1; + CTSU.CTSUCRA.BIT.ATUNE2 = 0; + break; + } + + case 2: + { + /*select normal mode */ + CTSU.CTSUCRA.BIT.LOAD = 0; + + /* LDO Setting (80uA) */ + CTSU.CTSUCRA.BIT.ATUNE1 = 0; + CTSU.CTSUCRA.BIT.ATUNE2 = 0; + break; + } + + case 3: + { + /*select normal mode */ + CTSU.CTSUCRA.BIT.LOAD = 0; + + /* LDO Setting (160uA) */ + CTSU.CTSUCRA.BIT.ATUNE1 = 1; + CTSU.CTSUCRA.BIT.ATUNE2 = 1; + break; + } + + case 4: + { + /*select normal mode */ + CTSU.CTSUCRA.BIT.LOAD = 1; + CTSU.CTSUCRA.BIT.LOAD = 3; + + /* LDO Setting (20uA) */ + CTSU.CTSUCRA.BIT.ATUNE1 = 0; + CTSU.CTSUCRA.BIT.ATUNE2 = 1; + break; + } + + case 5: + { + /* select Resistive load mode */ + CTSU.CTSUCRA.BIT.LOAD = 1; + CTSU.CTSUCRA.BIT.LOAD = 3; + + /* LDO Setting (40uA) */ + CTSU.CTSUCRA.BIT.ATUNE1 = 1; + CTSU.CTSUCRA.BIT.ATUNE2 = 0; + break; + } + + case 6: + { + /* select Resistive load mode */ + CTSU.CTSUCRA.BIT.LOAD = 1; + CTSU.CTSUCRA.BIT.LOAD = 3; + + /* LDO Setting (80uA) */ + CTSU.CTSUCRA.BIT.ATUNE1 = 0; + CTSU.CTSUCRA.BIT.ATUNE2 = 0; + break; + } + + case 7: + { + /* select Resistive load mode */ + CTSU.CTSUCRA.BIT.LOAD = 1; + CTSU.CTSUCRA.BIT.LOAD = 3; + + /* LDO Setting (160uA) */ + CTSU.CTSUCRA.BIT.ATUNE1 = 1; + CTSU.CTSUCRA.BIT.ATUNE2 = 1; + break; + } + + default: + { + break; + } + } + + CTSU.CTSUCALIB.BIT.DRV = 1; + + /* Software trigger start scan */ + err = (fsp_err_t) R_ADC_Control (0, ADC_CMD_SCAN_NOW, FIT_NO_PTR); + FSP_ERROR_RETURN(FSP_SUCCESS == err, err); + + /* Polling for scan complete */ + do + { + poll = R_ADC_Control (0, ADC_CMD_CHECK_SCAN_DONE, FIT_NO_PTR); + } while (ADC_ERR_SCAN_NOT_DONE == poll); + + /* Read A/D data then scan normal end */ + err = (fsp_err_t) R_ADC_Read (0, ADC_REG_CH8, &g_ctsu_diag_info.output_voltage_cnt[k]); + FSP_ERROR_RETURN(FSP_SUCCESS == err, err); + } + R_ADC_Close(0); + + return err; +} + +static fsp_err_t ctsu_diag_output_voltage_result (void) +{ + uint8_t k; + + for (k = 0; k < 8; k++) + { + if ((g_ctsu_diag_info.output_voltage_cnt[k] <= CTSU_DIAG_TSCAP_RANGE_LOW) || + (g_ctsu_diag_info.output_voltage_cnt[k] >= CTSU_DIAG_TSCAP_RANGE_HIGH)) + { + return FSP_ERR_CTSU_DIAG_OUTPUT_VOLTAGE; + } + } + + /* if all checks passed to this point, return success */ + return FSP_SUCCESS; +} + +static void ctsu_diag_over_voltage_scan_start (void) +{ + g_ctsu_diag_info.icomp0_value = 1; + + CTSU.CTSUCRA.BIT.PUMPON = 1; + CTSU.CTSUCRA.BIT.CSW = 1; + CTSU.CTSUCRA.BIT.PON = 1; + CTSU.CTSUCALIB.LONG = 0; + CTSU.CTSUCALIB.BIT.DRV = 1; + CTSU.CTSUCALIB.BIT.DCOFF = 1; + + CTSU.CTSUCRA.BIT.LOAD = 1; + CTSU.CTSUCRA.BIT.ATUNE1 = 1; + CTSU.CTSUCRA.BIT.ATUNE2 = 0; + CTSU.CTSUCRA.BIT.LOAD = 3; + + CTSU.CTSUSR.BIT.ICOMPRST = 1; + CTSU.CTSUCALIB.LONG = CTSU_DIAG_CURRENT_CLIB_REG; + CTSU.CTSUSO.LONG = 0; + CTSU.CTSUSO.LONG = 0; + + R_BSP_SoftwareDelay(1, BSP_DELAY_MILLISECS); + + if (CTSU.CTSUSR.BIT.ICOMP0 != 0) + { + g_ctsu_diag_info.icomp0_value = 0; + } + + CTSU.CTSUCRA.BIT.LOAD = 1; + CTSU.CTSUCRA.BIT.ATUNE1 = 1; + CTSU.CTSUCRA.BIT.ATUNE2 = 1; + CTSU.CTSUCRA.BIT.LOAD = 3; + + CTSU.CTSUCALIB.LONG = CTSU_DIAG_CURRENT_CLIB_REG; + + CTSU.CTSUCRA.BIT.LOAD = 2; + CTSU.CTSUSO.LONG = CTSU_DIAG_DAC_DATA_MAX_2; + CTSU.CTSUSO.LONG = CTSU_DIAG_DAC_DATA_MAX_3; + + R_BSP_SoftwareDelay(1, BSP_DELAY_MILLISECS); + + if (CTSU.CTSUSR.BIT.ICOMP0 != 1) + { + g_ctsu_diag_info.icomp0_value = 0; + } + + CTSU.CTSUSR.BIT.ICOMPRST = 1; + + R_BSP_SoftwareDelay(1, BSP_DELAY_MILLISECS); + + if (CTSU.CTSUSR.BIT.ICOMP0 != 1) + { + g_ctsu_diag_info.icomp0_value = 0; + } + + /* 30kohm setting */ + CTSU.CTSUCRA.BIT.ATUNE1 = 1; + CTSU.CTSUCRA.BIT.ATUNE2 = 0; + CTSU.CTSUCRA.BIT.LOAD = 1; + CTSU.CTSUCALIB.LONG = CTSU_DIAG_CURRENT_CLIB_REG; + + /* 0uA setting */ + CTSU.CTSUSO.LONG = 0x00000000; + CTSU.CTSUSO.LONG = 0x00000000; + + R_BSP_SoftwareDelay(1, BSP_DELAY_MILLISECS); + + if (CTSU.CTSUSR.BIT.ICOMP0 != 1) + { + g_ctsu_diag_info.icomp0_value = 0; + } + + CTSU.CTSUSR.BIT.ICOMPRST = 1; + + R_BSP_SoftwareDelay(1, BSP_DELAY_MILLISECS); + + if (CTSU.CTSUSR.BIT.ICOMP0 != 0) + { + g_ctsu_diag_info.icomp0_value = 0; + } +} + +static fsp_err_t ctsu_diag_over_voltage_result (void) +{ + if (0 == g_ctsu_diag_info.icomp0_value) + { + return FSP_ERR_CTSU_DIAG_OVER_VOLTAGE; + } + + /* if all checks passed to this point, return success */ + return FSP_SUCCESS; +} + +static void ctsu_diag_over_current_scan_start (void) +{ + CTSU.CTSUCALIB.LONG = 0; + CTSU.CTSUCALIB.BIT.DRV = 0; + CTSU.CTSUCALIB.BIT.DCOFF = 0; + CTSU.CTSUCRA.BIT.ATUNE1 = 0; + CTSU.CTSUCRA.BIT.ATUNE2 = 0; + CTSU.CTSUCRA.BIT.LOAD = 0; + CTSU.CTSUSR.BIT.ICOMPRST = 1; + + CTSU.CTSUMCH.BIT.MCA0 = 1; + CTSU.CTSUMCH.BIT.MCA1 = 0; + CTSU.CTSUMCH.BIT.MCA2 = 0; + CTSU.CTSUMCH.BIT.MCA3 = 0; + + CTSU.CTSUCRA.BIT.MD0 = 1; + CTSU.CTSUCRA.BIT.MD1 = 0; + + + CTSU.CTSUCHACA.LONG = g_ctsu_diag_info.chaca; + CTSU.CTSUCHACB.LONG = g_ctsu_diag_info.chacb; + CTSU.CTSUCHTRCA.LONG = 0; + CTSU.CTSUCHTRCB.LONG = 0; + + CTSU.CTSUCRA.BIT.CSW = 0; + CTSU.CTSUCRA.BIT.PON = 0; + + /* TSCAP discharge process */ + R_Set_CTSU_TSCAP_Discharge(); + R_BSP_SoftwareDelay(10,BSP_DELAY_MICROSECS); + R_Set_CTSU_TSCAP_Charge(); + + CTSU.CTSUCRA.BIT.CSW = 1; + CTSU.CTSUCRA.BIT.PON = 1; +} + +static fsp_err_t ctsu_diag_over_current_result (void) +{ + if (g_ctsu_diag_info.icomp1_value == 0) + { + return FSP_ERR_CTSU_DIAG_OVER_CURRENT; + } + + /* if all checks passed to this point, return success */ + return FSP_SUCCESS; +} + +static void ctsu_diag_load_resistance_scan_start (void) +{ + /* CTSU setting */ + CTSU.CTSUCRA.BIT.PUMPON = 1; + CTSU.CTSUCRA.BIT.CSW = 1; + CTSU.CTSUCRA.BIT.PON = 1; + + CTSU.CTSUCRA.BIT.SDPSEL = 1; + CTSU.CTSUCRA.BIT.PCSEL = 1; + CTSU.CTSUCRB.BIT.SST = CTSU_SST_RECOMMEND; + CTSU.CTSUCALIB.BIT.CCOCLK = 0; + CTSU.CTSUCALIB.BIT.SUCLKEN = 0; + + CTSU.CTSUCRA.BIT.MD0 = 1; + CTSU.CTSUCRA.BIT.MD1 = 0; + CTSU.CTSUMCH.BIT.MCA0 = 1; + CTSU.CTSUMCH.BIT.MCA1 = 0; + CTSU.CTSUMCH.BIT.MCA2 = 0; + CTSU.CTSUMCH.BIT.MCA3 = 0; + CTSU.CTSUCRA.BIT.LOAD = 1; + + /* Setting time of measurement */ + g_ctsu_diag_info.ctsuwr.ctsuso = (CTSU_SNUM_RECOMMEND << 10); + + CTSU.CTSUCHACA.LONG = 1; + CTSU.CTSUCHACB.LONG = 0; + CTSU.CTSUCHTRCA.LONG = 0; + CTSU.CTSUCHTRCB.LONG = 0; + + CTSU.CTSUCALIB.BIT.TSOC = 1; + + switch (g_ctsu_diag_info.loop_count) + { + case 0: + { + CTSU.CTSUCRA.BIT.ATUNE1 = 1; + CTSU.CTSUCRA.BIT.ATUNE2 = 1; // 7.5Kohm 160uA + break; + } + + case 1: + { + CTSU.CTSUCRA.BIT.ATUNE1 = 0; + CTSU.CTSUCRA.BIT.ATUNE2 = 0; // 15Kohm 80uA + break; + } + + case 2: + { + CTSU.CTSUCRA.BIT.ATUNE1 = 1; + CTSU.CTSUCRA.BIT.ATUNE2 = 0; // 30Kohm 40uA + break; + } + + case 3: + { + CTSU.CTSUCRA.BIT.ATUNE1 = 0; + CTSU.CTSUCRA.BIT.ATUNE2 = 1; // 60Kohm 20uA + break; + } + + default: + { + break; + } + } + + CTSU.CTSUCRA.BIT.LOAD = 3; +} + +static void ctsu_diag_load_resistance_data_get (void) +{ + g_ctsu_diag_info.load_resistance[g_ctsu_diag_info.loop_count] = (uint16_t) g_ctsu_diag_info.ctsuscnt[0]; + + g_ctsu_diag_info.load_resistance[g_ctsu_diag_info.loop_count] = + (uint16_t) ((((uint32_t) g_ctsu_diag_info.load_resistance[g_ctsu_diag_info.loop_count]) * + g_ctsu_diag_info.error_registance[3 - g_ctsu_diag_info.loop_count]) >> (CTSU_SHIFT_AMOUNT)); + +#if (CTSU_CFG_AUTO_CORRECTION_ENABLE == 0) + ctsu_correction_calc_t calc; + calc.snum = CTSU_SNUM_RECOMMEND; + calc.range = CTSU_RANGE_40UA; + calc.md = CTSU_MODE_DIAGNOSIS_SCAN; + ctsu_correction_calc(&g_ctsu_diag_info.load_resistance[g_ctsu_diag_info.loop_count], + g_ctsu_diag_info.load_resistance[g_ctsu_diag_info.loop_count], + &calc); +#endif + g_ctsu_diag_info.loop_count++; +} + +static fsp_err_t ctsu_diag_load_resistance_result (void) +{ + uint8_t k; + + for (k = 0; k < 4; k++) + { + if ((g_ctsu_diag_info.load_resistance[k] > CTSU_CFG_DIAG_LOAD_REISTER_MIN) && + (g_ctsu_diag_info.load_resistance[k] < CTSU_CFG_DIAG_LOAD_REISTER_MAX)) + { + } + else + { + return FSP_ERR_CTSU_DIAG_LOAD_RESISTANCE; + } + } + + /* if all checks passed to this point, return success */ + return FSP_SUCCESS; +} + +static void ctsu_diag_current_source_scan_start (void) +{ + /* CTSU setting */ + CTSU.CTSUCRA.BIT.PUMPON = 1; + CTSU.CTSUCRA.BIT.CSW = 1; + CTSU.CTSUCRA.BIT.PON = 1; + + CTSU.CTSUCRA.BIT.SDPSEL = 1; + CTSU.CTSUCRA.BIT.PCSEL = 1; + CTSU.CTSUCRB.BIT.SST = CTSU_SST_RECOMMEND; + CTSU.CTSUCALIB.BIT.CCOCLK = 0; + CTSU.CTSUCALIB.BIT.SUCLKEN = 0; + + CTSU.CTSUCRA.BIT.MD0 = 1; + CTSU.CTSUCRA.BIT.MD1 = 0; + CTSU.CTSUMCH.BIT.MCA0 = 1; + CTSU.CTSUMCH.BIT.MCA1 = 0; + CTSU.CTSUMCH.BIT.MCA2 = 0; + CTSU.CTSUMCH.BIT.MCA3 = 0; + + CTSU.CTSUCHACA.LONG = 1; + CTSU.CTSUCHACB.LONG = 0; + CTSU.CTSUCHTRCA.LONG = 0; + CTSU.CTSUCHTRCB.LONG = 0; + + CTSU.CTSUSO.LONG = CTSU_DIAG_DAC_INIT_0; + CTSU.CTSUSO.LONG = CTSU_DIAG_DAC_INIT_1; + CTSU.CTSUSO.LONG = CTSU_DIAG_DAC_INIT_2; + CTSU.CTSUSO.LONG = CTSU_DIAG_DAC_INIT_3; + + /* LDO Nch setting (40uAmode) */ + CTSU.CTSUCRA.BIT.LOAD = 1; + CTSU.CTSUCALIB.BIT.TSOC = 1; + CTSU.CTSUCRA.BIT.ATUNE1 = 1; + CTSU.CTSUCRA.BIT.ATUNE2 = 0; + CTSU.CTSUCRA.BIT.LOAD = 3; + + /* DAC initial setting */ + CTSU.CTSUCALIB.BIT.DACMSEL = 1; + CTSU.CTSUCALIB.BIT.DACCARRY = 1; + + if (15 >= g_ctsu_diag_info.loop_count) + { + /* Upper Current source setting (10uA) */ + CTSU.CTSUSO.LONG = CTSU_DIAG_DAC_INIT_0; + CTSU.CTSUSO.LONG = CTSU_DIAG_DAC_INIT_1; + + switch (g_ctsu_diag_info.loop_count) + { + case 0: + { + CTSU.CTSUSO.LONG = CTSU_DIAG_DAC_INIT_2 + CTSU_DIAG_DAC_0BIT; + CTSU.CTSUSO.LONG = CTSU_DIAG_DAC_INIT_3; + + /* Setting time of measurement */ + g_ctsu_diag_info.ctsuwr.ctsuso = (CTSU_SNUM_RECOMMEND << 10) | CTSU_DIAG_DAC_INIT_3; + break; + } + + case 1: + { + CTSU.CTSUSO.LONG = CTSU_DIAG_DAC_INIT_2 + CTSU_DIAG_DAC_1BIT; + CTSU.CTSUSO.LONG = CTSU_DIAG_DAC_INIT_3; + g_ctsu_diag_info.ctsuwr.ctsuso = (CTSU_SNUM_RECOMMEND << 10) | CTSU_DIAG_DAC_INIT_3; + break; + } + + case 2: + { + CTSU.CTSUSO.LONG = CTSU_DIAG_DAC_INIT_2 + CTSU_DIAG_DAC_2BIT; + CTSU.CTSUSO.LONG = CTSU_DIAG_DAC_INIT_3; + g_ctsu_diag_info.ctsuwr.ctsuso = (CTSU_SNUM_RECOMMEND << 10) | CTSU_DIAG_DAC_INIT_3; + break; + } + + case 3: + { + CTSU.CTSUSO.LONG = CTSU_DIAG_DAC_INIT_2 + CTSU_DIAG_DAC_3BIT; + CTSU.CTSUSO.LONG = CTSU_DIAG_DAC_INIT_3; + g_ctsu_diag_info.ctsuwr.ctsuso = (CTSU_SNUM_RECOMMEND << 10) | CTSU_DIAG_DAC_INIT_3; + break; + } + + case 4: + { + CTSU.CTSUSO.LONG = CTSU_DIAG_DAC_INIT_2 + CTSU_DIAG_DAC_4BIT; + CTSU.CTSUSO.LONG = CTSU_DIAG_DAC_INIT_3; + g_ctsu_diag_info.ctsuwr.ctsuso = (CTSU_SNUM_RECOMMEND << 10) | CTSU_DIAG_DAC_INIT_3; + break; + } + + case 5: + { + CTSU.CTSUSO.LONG = CTSU_DIAG_DAC_INIT_2 + CTSU_DIAG_DAC_5BIT; + CTSU.CTSUSO.LONG = CTSU_DIAG_DAC_INIT_3; + g_ctsu_diag_info.ctsuwr.ctsuso = (CTSU_SNUM_RECOMMEND << 10) | CTSU_DIAG_DAC_INIT_3; + break; + } + + case 6: + { + CTSU.CTSUSO.LONG = CTSU_DIAG_DAC_INIT_2 + CTSU_DIAG_DAC_6BIT; + CTSU.CTSUSO.LONG = CTSU_DIAG_DAC_INIT_3; + g_ctsu_diag_info.ctsuwr.ctsuso = (CTSU_SNUM_RECOMMEND << 10) | CTSU_DIAG_DAC_INIT_3; + break; + } + + case 7: + { + CTSU.CTSUSO.LONG = CTSU_DIAG_DAC_INIT_2 + CTSU_DIAG_DAC_7BIT; + CTSU.CTSUSO.LONG = CTSU_DIAG_DAC_INIT_3; + g_ctsu_diag_info.ctsuwr.ctsuso = (CTSU_SNUM_RECOMMEND << 10) | CTSU_DIAG_DAC_INIT_3; + break; + } + + case 8: + { + CTSU.CTSUSO.LONG = CTSU_DIAG_DAC_INIT_2; + CTSU.CTSUSO.LONG = CTSU_DIAG_DAC_INIT_3 + CTSU_DIAG_DAC_0BIT; + g_ctsu_diag_info.ctsuwr.ctsuso = (CTSU_SNUM_RECOMMEND << 10) | + (CTSU_DIAG_DAC_INIT_3 + CTSU_DIAG_DAC_0BIT); + break; + } + + case 9: + { + CTSU.CTSUSO.LONG = CTSU_DIAG_DAC_INIT_2; + CTSU.CTSUSO.LONG = CTSU_DIAG_DAC_INIT_3 + CTSU_DIAG_DAC_1BIT; + g_ctsu_diag_info.ctsuwr.ctsuso = (CTSU_SNUM_RECOMMEND << 10) | + (CTSU_DIAG_DAC_INIT_3 + CTSU_DIAG_DAC_1BIT); + break; + } + + case 10: + { + CTSU.CTSUSO.LONG = CTSU_DIAG_DAC_INIT_2; + CTSU.CTSUSO.LONG = CTSU_DIAG_DAC_INIT_3 + CTSU_DIAG_DAC_2BIT; + g_ctsu_diag_info.ctsuwr.ctsuso = (CTSU_SNUM_RECOMMEND << 10) | + (CTSU_DIAG_DAC_INIT_3 + CTSU_DIAG_DAC_2BIT); + break; + } + + case 11: + { + CTSU.CTSUSO.LONG = CTSU_DIAG_DAC_INIT_2; + CTSU.CTSUSO.LONG = CTSU_DIAG_DAC_INIT_3 + CTSU_DIAG_DAC_3BIT; + g_ctsu_diag_info.ctsuwr.ctsuso = (CTSU_SNUM_RECOMMEND << 10) | + (CTSU_DIAG_DAC_INIT_3 + CTSU_DIAG_DAC_3BIT); + break; + } + + case 12: + { + CTSU.CTSUSO.LONG = CTSU_DIAG_DAC_INIT_2; + CTSU.CTSUSO.LONG = CTSU_DIAG_DAC_INIT_3 + CTSU_DIAG_DAC_4BIT; + g_ctsu_diag_info.ctsuwr.ctsuso = (CTSU_SNUM_RECOMMEND << 10) | + (CTSU_DIAG_DAC_INIT_3 + CTSU_DIAG_DAC_4BIT); + break; + } + + case 13: + { + CTSU.CTSUSO.LONG = CTSU_DIAG_DAC_INIT_2; + CTSU.CTSUSO.LONG = CTSU_DIAG_DAC_INIT_3 + CTSU_DIAG_DAC_5BIT; + g_ctsu_diag_info.ctsuwr.ctsuso = (CTSU_SNUM_RECOMMEND << 10) | + (CTSU_DIAG_DAC_INIT_3 + CTSU_DIAG_DAC_5BIT); + break; + } + + case 14: + { + CTSU.CTSUSO.LONG = CTSU_DIAG_DAC_INIT_2; + CTSU.CTSUSO.LONG = CTSU_DIAG_DAC_INIT_3 + CTSU_DIAG_DAC_6BIT; + g_ctsu_diag_info.ctsuwr.ctsuso = (CTSU_SNUM_RECOMMEND << 10) | + (CTSU_DIAG_DAC_INIT_3 + CTSU_DIAG_DAC_6BIT); + break; + } + + case 15: + { + CTSU.CTSUSO.LONG = CTSU_DIAG_DAC_INIT_2; + CTSU.CTSUSO.LONG = CTSU_DIAG_DAC_INIT_3 + CTSU_DIAG_DAC_7BIT; + g_ctsu_diag_info.ctsuwr.ctsuso = (CTSU_SNUM_RECOMMEND << 10) | + (CTSU_DIAG_DAC_INIT_3 + CTSU_DIAG_DAC_7BIT); + break; + } + } + } + + if (16 <= g_ctsu_diag_info.loop_count) + { + /* Lower Current source setting (10uA) */ + switch (g_ctsu_diag_info.loop_count) + { + case 16: + { + CTSU.CTSUSO.LONG = CTSU_DIAG_DAC_DATA_MAX_0; + CTSU.CTSUSO.LONG = CTSU_DIAG_DAC_INIT_1; + break; + } + + case 17: + { + CTSU.CTSUSO.LONG = CTSU_DIAG_DAC_DATA_MAX_0 - CTSU_DIAG_DAC_0BIT; + CTSU.CTSUSO.LONG = CTSU_DIAG_DAC_INIT_1 + CTSU_DIAG_DAC_0BIT; + break; + } + + case 18: + { + CTSU.CTSUSO.LONG = CTSU_DIAG_DAC_DATA_MAX_0 - (CTSU_DIAG_DAC_0BIT + CTSU_DIAG_DAC_1BIT); + CTSU.CTSUSO.LONG = CTSU_DIAG_DAC_INIT_1 + (CTSU_DIAG_DAC_0BIT + CTSU_DIAG_DAC_1BIT); + break; + } + + case 19: + { + CTSU.CTSUSO.LONG = CTSU_DIAG_DAC_DATA_MAX_0 - (CTSU_DIAG_DAC_1BIT + CTSU_DIAG_DAC_2BIT); + CTSU.CTSUSO.LONG = CTSU_DIAG_DAC_INIT_1 + (CTSU_DIAG_DAC_0BIT + CTSU_DIAG_DAC_1BIT); + break; + } + + case 20: + { + CTSU.CTSUSO.LONG = CTSU_DIAG_DAC_DATA_MAX_0 - (CTSU_DIAG_DAC_2BIT + CTSU_DIAG_DAC_3BIT); + CTSU.CTSUSO.LONG = CTSU_DIAG_DAC_INIT_1 + (CTSU_DIAG_DAC_0BIT + CTSU_DIAG_DAC_1BIT); + break; + } + + case 21: + { + CTSU.CTSUSO.LONG = CTSU_DIAG_DAC_DATA_MAX_0 - (CTSU_DIAG_DAC_3BIT + CTSU_DIAG_DAC_4BIT); + CTSU.CTSUSO.LONG = CTSU_DIAG_DAC_INIT_1 + (CTSU_DIAG_DAC_0BIT + CTSU_DIAG_DAC_1BIT); + break; + } + + case 22: + { + CTSU.CTSUSO.LONG = CTSU_DIAG_DAC_DATA_MAX_0 - (CTSU_DIAG_DAC_4BIT + CTSU_DIAG_DAC_5BIT); + CTSU.CTSUSO.LONG = CTSU_DIAG_DAC_INIT_1 + (CTSU_DIAG_DAC_0BIT + CTSU_DIAG_DAC_1BIT); + break; + } + + case 23: + { + CTSU.CTSUSO.LONG = CTSU_DIAG_DAC_DATA_MAX_0 - (CTSU_DIAG_DAC_5BIT + CTSU_DIAG_DAC_6BIT); + CTSU.CTSUSO.LONG = CTSU_DIAG_DAC_INIT_1 + (CTSU_DIAG_DAC_0BIT + CTSU_DIAG_DAC_1BIT); + break; + } + + case 24: + { + CTSU.CTSUSO.LONG = CTSU_DIAG_DAC_DATA_MAX_0 - (CTSU_DIAG_DAC_6BIT + CTSU_DIAG_DAC_7BIT); + CTSU.CTSUSO.LONG = CTSU_DIAG_DAC_INIT_1 + (CTSU_DIAG_DAC_0BIT + CTSU_DIAG_DAC_1BIT); + break; + } + + case 25: + { + CTSU.CTSUSO.LONG = CTSU_DIAG_DAC_DATA_MAX_0 - CTSU_DIAG_DAC_7BIT; + CTSU.CTSUSO.LONG = CTSU_DIAG_DAC_INIT_1 + CTSU_DIAG_DAC_1BIT; + break; + } + } + + CTSU.CTSUSO.LONG = CTSU_DIAG_DAC_INIT_2; + CTSU.CTSUSO.LONG = CTSU_DIAG_DAC_INIT_3; + g_ctsu_diag_info.ctsuwr.ctsuso = (CTSU_SNUM_RECOMMEND << 10) | CTSU_DIAG_DAC_INIT_3; + } +} + +static void ctsu_diag_current_source_data_get (void) +{ + g_ctsu_diag_info.current_source[g_ctsu_diag_info.loop_count] = (uint16_t) g_ctsu_diag_info.ctsuscnt[0]; + + g_ctsu_diag_info.current_source[g_ctsu_diag_info.loop_count] = + (uint16_t) ((((uint32_t) g_ctsu_diag_info.current_source[g_ctsu_diag_info.loop_count]) * + g_ctsu_diag_info.error_registance[2]) >> (CTSU_SHIFT_AMOUNT)); + +#if (CTSU_CFG_AUTO_CORRECTION_ENABLE == 0) + ctsu_correction_calc_t calc; + calc.snum = CTSU_SNUM_RECOMMEND; + calc.range = CTSU_RANGE_40UA; + calc.md = CTSU_MODE_DIAGNOSIS_SCAN; + ctsu_correction_calc(&g_ctsu_diag_info.current_source[g_ctsu_diag_info.loop_count], + g_ctsu_diag_info.current_source[g_ctsu_diag_info.loop_count], + &calc); +#endif + + g_ctsu_diag_info.loop_count++; +} + +static fsp_err_t ctsu_diag_current_source_result (void) +{ + uint8_t k; + uint16_t current_source_diff; + + for (k = 0; k < (CTSU_DIAG_HIGH_CURRENT_SOURCE + CTSU_DIAG_LOW_CURRENT_SOURCE); k++) + { + current_source_diff = (uint16_t) (g_ctsu_diag_info.load_resistance[2] - g_ctsu_diag_info.current_source[k]); + if ((current_source_diff < CTSU_CFG_DIAG_CURRENT_SOURCE_DIFF_MAX) && + (current_source_diff > CTSU_CFG_DIAG_CURRENT_SOURCE_DIFF_MIN)) + { + } + else + { + return FSP_ERR_CTSU_DIAG_CURRENT_SOURCE; + } + } + + /* if all checks passed to this point, return success */ + return FSP_SUCCESS; +} + +static void ctsu_diag_cco_gain_scan_start (void) +{ + /* CTSU Setting */ + CTSU.CTSUCRA.BIT.MD0 = 0; + CTSU.CTSUCRA.BIT.MD1 = 0; + CTSU.CTSUCRA.BIT.LOAD = 1; + CTSU.CTSUCRA.BIT.PON = 1; + CTSU.CTSUCRA.BIT.CSW = 1; + CTSU.CTSUCRA.BIT.PUMPON = 1; + + CTSU.CTSUMCH.BIT.MCA0 = 1; + CTSU.CTSUMCH.BIT.MCA1 = 0; + CTSU.CTSUMCH.BIT.MCA2 = 0; + CTSU.CTSUMCH.BIT.MCA3 = 0; + + CTSU.CTSUCRB.BIT.SSMOD = 4; + CTSU.CTSUCRB.BIT.SSCNT = 0; + CTSU.CTSUCRB.BIT.SST = CTSU_SST_RECOMMEND; + + CTSU.CTSUCRA.BIT.PCSEL = 1; + CTSU.CTSUCRA.BIT.SDPSEL = 1; + + CTSU.CTSUCALIB.LONG = 0; + CTSU.CTSUCALIB.BIT.SUCARRY = 0; + +#if (CTSU_CFG_AUTO_CORRECTION_ENABLE == 1) + /* Sensor counter auto correction enabled */ + CTSU.CTSUOPT.BIT.CCOCFEN = 0; +#endif + + if (CTSU_DIAG_SENSCLK == g_ctsu_diag_info.state) + { + CTSU.CTSUCALIB.BIT.CCOCALIB = 1; + } + + if (CTSU_DIAG_SUCLK == g_ctsu_diag_info.state) + { + CTSU.CTSUCALIB.BIT.CCOCALIB = 0; + } + + CTSU.CTSUCALIB.BIT.CCOCLK = 0; + + CTSU.CTSUCHACA.LONG = 1; + CTSU.CTSUCHACB.LONG = 0; + CTSU.CTSUCHTRCA.LONG = 0; + CTSU.CTSUCHTRCB.LONG = 0; + + /* Setting time of measurement */ + g_ctsu_diag_info.ctsuwr.ctsuso = (CTSU_SNUM_RECOMMEND << 10); + + CTSU.CTSUMCH.BIT.MCH0 = 0; + + if (g_ctsu_diag_info.loop_count < 8) + { + CTSU.CTSUSUCLKA.BIT.SUADJ0 = (0x1f + (g_ctsu_diag_info.loop_count * CTSU_DIAG_SUCLK0_REG2)); + } + else if ((8 <= g_ctsu_diag_info.loop_count) && (g_ctsu_diag_info.loop_count < 11)) + { + CTSU.CTSUSUCLKA.BIT.SUADJ0 = (0x1f + (7 * CTSU_DIAG_SUCLK0_REG2)); + if (8 == g_ctsu_diag_info.loop_count) + { + CTSU.CTSUCRB.BIT.SSCNT = 0; + } + else if (9 == g_ctsu_diag_info.loop_count) + { + CTSU.CTSUCRB.BIT.SSCNT = 1; + } + else if (10 == g_ctsu_diag_info.loop_count) + { + CTSU.CTSUCRB.BIT.SSCNT = 2; + } + else + { + } + + CTSU.CTSUCALIB.BIT.SUCARRY = 0; + if (CTSU_DIAG_SENSCLK == g_ctsu_diag_info.state) + { + CTSU.CTSUCALIB.BIT.CCOCALIB = 1; + } + + if (CTSU_DIAG_SUCLK == g_ctsu_diag_info.state) + { + CTSU.CTSUCALIB.BIT.CCOCALIB = 0; + } + + CTSU.CTSUCALIB.BIT.CCOCLK = 0; + CTSU.CTSUCRB.BIT.SSMOD = 1; + CTSU.CTSUCRB.BIT.SST = CTSU_SST_RECOMMEND_CURRENT; + if (8 == g_ctsu_diag_info.loop_count) + { + CTSU.CTSUCRB.BIT.SSCNT = 1; + } + else if (9 == g_ctsu_diag_info.loop_count) + { + CTSU.CTSUCRB.BIT.SSCNT = 2; + } + else if (10 == g_ctsu_diag_info.loop_count) + { + CTSU.CTSUCRB.BIT.SSCNT = 3; + } + else + { + } + } + else if (11 == g_ctsu_diag_info.loop_count) + { + CTSU.CTSUSUCLKA.BIT.SUADJ0 = (0x1f + (7 * CTSU_DIAG_SUCLK0_REG2)); + + CTSU.CTSUCRB.BIT.SSCNT = 2; + CTSU.CTSUCALIB.BIT.SUCARRY = 1; + if (CTSU_DIAG_SENSCLK == g_ctsu_diag_info.state) + { + CTSU.CTSUCALIB.BIT.CCOCALIB = 1; + } + + if (CTSU_DIAG_SUCLK == g_ctsu_diag_info.state) + { + CTSU.CTSUCALIB.BIT.CCOCALIB = 0; + } + + CTSU.CTSUCALIB.BIT.CCOCLK = 0; + CTSU.CTSUCRB.BIT.SSMOD = 1; + CTSU.CTSUCRB.BIT.SST = CTSU_SST_RECOMMEND_CURRENT; + CTSU.CTSUCRB.BIT.SSCNT = 3; + } + else + { + } +} + +static void ctsu_diag_cco_gain_data_get (void) +{ + if (CTSU_DIAG_SENSCLK == g_ctsu_diag_info.state) + { + g_ctsu_diag_info.sensclk_cnt[g_ctsu_diag_info.loop_count] = (uint16_t) g_ctsu_diag_info.ctsuscnt[0]; + } + + if (CTSU_DIAG_SUCLK == g_ctsu_diag_info.state) + { + g_ctsu_diag_info.suclk_cnt[g_ctsu_diag_info.loop_count] = (uint16_t) (g_ctsu_diag_info.ctsuscnt[0] >> 16); + } + + g_ctsu_diag_info.loop_count++; + +#if (CTSU_CFG_AUTO_CORRECTION_ENABLE == 1) + /* Sensor counter auto correction enabled */ + CTSU.CTSUOPT.BIT.CCOCFEN = 1; +#endif + +} + +static fsp_err_t ctsu_diag_cco_gain_result (void) +{ + uint32_t k; + volatile uint16_t cco_gain_data; + uint16_t cco_gain_data_pre; + uint16_t cco_gain_diff; + uint16_t cco_gain_max; + uint16_t cco_gain_min; + uint16_t cco_gain_diff_max; + uint16_t cco_gain_diff_min; + + /* read counter data */ + for (k = 0; k < CTSU_CORRECTION_POINT_NUM; k++) + { + if (CTSU_DIAG_SENSCLK == g_ctsu_diag_info.state) + { + cco_gain_data = g_ctsu_diag_info.sensclk_cnt[k]; + } + + if (CTSU_DIAG_SUCLK == g_ctsu_diag_info.state) + { + cco_gain_data = g_ctsu_diag_info.suclk_cnt[k]; + } + + cco_gain_max = cco_gain_table[k][1]; + cco_gain_min = cco_gain_table[k][0]; + + /* check measurement result */ + if ((cco_gain_data <= cco_gain_max) && (cco_gain_data >= cco_gain_min)) + { + } + else + { + if (CTSU_DIAG_SENSCLK == g_ctsu_diag_info.state) + { + return FSP_ERR_CTSU_DIAG_SENSCLK_GAIN; + } + + if (CTSU_DIAG_SUCLK == g_ctsu_diag_info.state) + { + return FSP_ERR_CTSU_DIAG_SUCLK_GAIN; + } + } + + if (0 < k) + { + cco_gain_diff_max = cco_gain_diff_table[k - 1][1]; + cco_gain_diff_min = cco_gain_diff_table[k - 1][0]; + + if (cco_gain_data > cco_gain_data_pre) + { + cco_gain_diff = (uint16_t) (cco_gain_data - cco_gain_data_pre); + } + else + { + cco_gain_diff = (uint16_t) (cco_gain_data_pre - cco_gain_data); + } + + /* check measurement result */ + if ((cco_gain_diff > cco_gain_diff_max) || (cco_gain_diff < cco_gain_diff_min)) + { + if (CTSU_DIAG_SENSCLK == g_ctsu_diag_info.state) + { + return FSP_ERR_CTSU_DIAG_SENSCLK_GAIN; + } + + if (CTSU_DIAG_SUCLK == g_ctsu_diag_info.state) + { + return FSP_ERR_CTSU_DIAG_SUCLK_GAIN; + } + } + } + + cco_gain_data_pre = cco_gain_data; + } + + /* if all checks passed to this point, return success */ + return FSP_SUCCESS; +} + +static void ctsu_diag_clock_recovery_scan_start (void) +{ + CTSU.CTSUCRA.LONG = g_ctsu_diag_reg.ctsucra; + CTSU.CTSUCRA.BIT.MD0 = 1; + CTSU.CTSUCRA.BIT.MD1 = 0; + CTSU.CTSUMCH.LONG = g_ctsu_diag_reg.ctsumch; + CTSU.CTSUCRB.LONG = g_ctsu_diag_reg.ctsucrb; + CTSU.CTSUCRA.BIT.LOAD = 1; + CTSU.CTSUCRB.BIT.SSCNT = 1; + CTSU.CTSUCALIB.BIT.SUCARRY = 0; + CTSU.CTSUCALIB.BIT.CCOCALIB = 0; + CTSU.CTSUCALIB.BIT.CCOCLK = 1; + CTSU.CTSUCALIB.BIT.TSOC = 0; + CTSU.CTSUCALIB.BIT.SUCLKEN = 1; + + CTSU.CTSUCRA.BIT.SDPSEL = 0; + CTSU.CTSUSUCLKA.LONG = g_ctsu_diag_reg.ctsusuclka; + CTSU.CTSUSUCLKB.LONG = g_ctsu_diag_reg.ctsusuclkb; + CTSU.CTSUCRA.BIT.SDPSEL = 1; + + CTSU.CTSUMCH.BIT.MCA0 = 1; + CTSU.CTSUMCH.BIT.MCA1 = 1; + CTSU.CTSUMCH.BIT.MCA2 = 1; + CTSU.CTSUMCH.BIT.MCA3 = 0; + + CTSU.CTSUCHACA.LONG = g_ctsu_diag_info.chaca; + CTSU.CTSUCHACB.LONG = g_ctsu_diag_info.chacb; + CTSU.CTSUCHTRCA.LONG = 0; + CTSU.CTSUCHTRCB.LONG = 0; + +#if (CTSU_CFG_AUTO_CORRECTION_ENABLE == 1) + /* Sensor counter auto correction enabled */ + CTSU.CTSUOPT.BIT.CCOCFEN = 0; +#endif + + /* Setting time of measurement */ + + g_ctsu_diag_info.ctsuwr.ctsuso = (uint32_t) ((0xf << 24) | (CTSU_SNUM_RECOMMEND << 10)); +} + +static void ctsu_diag_clock_recovery_data_get (void) +{ + uint8_t i; + for (i = 0; i < 3; i++) + { + g_ctsu_diag_info.suclk_count_clk_recv[i] = (uint16_t) (g_ctsu_diag_info.ctsuscnt[i] >> 16); + } + +#if (CTSU_CFG_AUTO_CORRECTION_ENABLE == 1) + /* Sensor counter auto correction enabled */ + CTSU.CTSUOPT.BIT.CCOCFEN = 1; +#endif +} + +static fsp_err_t ctsu_diag_clock_recovery_result (void) +{ + uint8_t i; + uint32_t suclk[3]; + static uint32_t suclk_cnt[3]; + + suclk[0] = (CTSU_DIAG_STCLK_FREQ * (CTSU_CFG_SUMULTI0 + 1)); + suclk[1] = (CTSU_DIAG_STCLK_FREQ * (CTSU_CFG_SUMULTI1 + 1)); + suclk[2] = (CTSU_DIAG_STCLK_FREQ * (CTSU_CFG_SUMULTI2 + 1)); + + for (i = 0; i < 3; i++) + { + suclk_cnt[i % 3] = ((suclk[i % 3] * (CTSU_SNUM_RECOMMEND + 1)) / CTSU_DIAG_STCLK_FREQ) * 8 * 2; + + /* suclk check */ + if ((suclk_cnt[i % 3] + CTSU_CFG_DIAG_CLOCK_RECOV_RANGE) < g_ctsu_diag_info.suclk_count_clk_recv[i]) + { + return FSP_ERR_CTSU_DIAG_CLOCK_RECOVERY; + } + + if ((suclk_cnt[i % 3] - CTSU_CFG_DIAG_CLOCK_RECOV_RANGE) > g_ctsu_diag_info.suclk_count_clk_recv[i]) + { + return FSP_ERR_CTSU_DIAG_CLOCK_RECOVERY; + } + } + + /* if all checks passed to this point, return success */ + return FSP_SUCCESS; +} + + #if (CTSU_CFG_NUM_CFC != 0) +static void ctsu_diag_cfc_gain_scan_start (void) +{ + /* CTSU Setting */ + CTSU.CTSUCRA.BIT.MD0 = 1; + CTSU.CTSUCRA.BIT.MD1 = 0; + CTSU.CTSUMCH.BIT.MCA0 = 1; + CTSU.CTSUMCH.BIT.MCA1 = 0; + CTSU.CTSUMCH.BIT.MCA2 = 0; + CTSU.CTSUMCH.BIT.MCA3 = 0; + CTSU.CTSUCRA.BIT.LOAD = 1; + CTSU.CTSUCRA.BIT.CFCON = 1; + CTSU.CTSUCRA.BIT.PUMPON = 1; + CTSU.CTSUCRA.BIT.TXVSEL = 1; + + CTSU.CTSUCRB.BIT.SSMOD = 0; + CTSU.CTSUCRB.BIT.SSCNT = 0; + CTSU.CTSUCRB.BIT.SST = 0x1F; + + CTSU.CTSUCRA.BIT.PCSEL = 1; + CTSU.CTSUCRA.BIT.SDPSEL = 1; + + CTSU.CTSUCALIB = 0; + CTSU.CTSUCALIB.BIT.SUCARRY = 0; + CTSU.CTSUCALIB.BIT.CCOCALIB = 0; + CTSU.CTSUCALIB.BIT.CCOCLK = 0; + CTSU.CTSUCALIB.BIT.CFCMODE = 1; + CTSU.CTSUCALIB.BIT.CFCRDMD = 1; + + if (CTSU_DIAG_CHACA_TSMAX > g_ctsu_corrcfc_info.ts_table[0]) + { + CTSU.CTSUCHACA.LONG = (uint32_t) (1 << g_ctsu_corrcfc_info.ts_table[0]); + CTSU.CTSUCHACB.LONG = 0; + } + else + { + CTSU.CTSUCHACA.LONG = 0; + CTSU.CTSUCHACB.LONG = (uint32_t) (1 << (g_ctsu_corrcfc_info.ts_table[0] - CTSU_DIAG_CHACA_TSMAX)); + } + + CTSU.CTSUCHTRC0 = 0; + CTSU.CTSUCHTRC1 = 0; + CTSU.CTSUCHTRC2 = 0; + CTSU.CTSUCHTRC3 = 0; + CTSU.CTSUCHTRC4 = 0; + + CTSU.CTSUMCH.BIT.MCH0 = 0; + + CTSU.CTSUCRA.BIT.SDPSEL = 0; + CTSU.CTSUSUCLK0 = + (uint16_t) (((g_ctsu_diag_info.loop_count + CTSU_CORRCFC_CENTER_POINT) * CTSU_CORRECTION_SUMULTI) - 1); + CTSU.CTSUCRA.BIT.SDPSEL = 1; + + g_ctsu_diag_info.ctsuwr.ctsuso = (uint32_t) (CTSU_SNUM_RECOMMEND << 10); +} + +static void ctsu_diag_cfc_gain_data_get (void) +{ + g_ctsu_diag_info.cfc_cnt[g_ctsu_diag_info.loop_count] = (uint16_t) g_ctsu_diag_info.ctsuscnt[0]; + + g_ctsu_diag_info.loop_count++; +} + +static fsp_err_t ctsu_diag_cfc_gain_result (void) +{ + uint8_t k; + + for (k = 0; k < (CTSU_CORRCFC_POINT_NUM - 1); k++) + { + if (g_ctsu_diag_info.cfc_cnt[k + 1] > g_ctsu_diag_info.cfc_cnt[k]) + { + /* PASS */ + } + else + { + + /* FAIL */ + return FSP_ERR_CTSU_DIAG_CFC_GAIN; + } + } + + return FSP_SUCCESS; +} + + #endif + #endif +#endif diff --git a/drivers/rx/rdp/src/r_ctsu_qe/src/r_ctsu_qe_pinset.c b/drivers/rx/rdp/src/r_ctsu_qe/src/r_ctsu_qe_pinset.c new file mode 100644 index 00000000..50b9c9c3 --- /dev/null +++ b/drivers/rx/rdp/src/r_ctsu_qe/src/r_ctsu_qe_pinset.c @@ -0,0 +1,94 @@ +/* +* Copyright (c) 2018 Renesas Electronics Corporation and/or its affiliates +* +* SPDX-License-Identifier: BSD-3-Clause +*/ +/*********************************************************************************************************************** +* File Name : r_ctsu_qe_pinset.c +* Version : 1.0.2 +* Device(s) : R5F51308AxFP +* Tool-Chain : RXC toolchain +* Description : Setting of port and mpc registers +***********************************************************************************************************************/ + +/*********************************************************************************************************************** +Includes +***********************************************************************************************************************/ +#include "r_ctsu_qe_pinset.h" +#include "platform.h" + +/*********************************************************************************************************************** +Global variables and functions +***********************************************************************************************************************/ +/*********************************************************************************************************************** +* Function Name: R_CTSU_PinSetInit +* Description : This function initializes pins for r_ctsu_qe module +* Arguments : none +* Return Value : none +***********************************************************************************************************************/ +void R_CTSU_PinSetInit() +{ + R_BSP_RegisterProtectDisable(BSP_REG_PROTECT_MPC); + + /* Set TSCAP pin */ + MPC.PC4PFS.BYTE = 0x19U; + PORTC.PMR.BIT.B4 = 1U; + + /* Set TS7 pin */ + MPC.PH3PFS.BYTE = 0x19U; + PORTH.PMR.BIT.B3 = 1U; + + /* Set TS8 pin */ + MPC.PH2PFS.BYTE = 0x19U; + PORTH.PMR.BIT.B2 = 1U; + + /* Set TS9 pin */ + MPC.PH1PFS.BYTE = 0x19U; + PORTH.PMR.BIT.B1 = 1U; + + /* Set TS10 pin */ + MPC.PH0PFS.BYTE = 0x19U; + PORTH.PMR.BIT.B0 = 1U; + + /* Set TS11 pin */ + MPC.P55PFS.BYTE = 0x19U; + PORT5.PMR.BIT.B5 = 1U; + + /* Set TS12 pin */ + MPC.P54PFS.BYTE = 0x19U; + PORT5.PMR.BIT.B4 = 1U; + + R_BSP_RegisterProtectEnable(BSP_REG_PROTECT_MPC); +} + +/*********************************************************************************************************************** +* Function Name: R_Set_CTSU_TSCAP_Discharge +* Description : This function discharges TSCAP pin +* Arguments : none +* Return Value : none +***********************************************************************************************************************/ +void R_Set_CTSU_TSCAP_Discharge() +{ + /* Set TSCAP pin */ + PORTC.PMR.BIT.B4 = 0U; + PORTC.PODR.BIT.B4 = 0U; + PORTC.PDR.BIT.B4 = 1U; +} + +/*********************************************************************************************************************** +* Function Name: R_Set_CTSU_TSCAP_Charge +* Description : This function charges TSCAP pin +* Arguments : none +* Return Value : none +***********************************************************************************************************************/ +void R_Set_CTSU_TSCAP_Charge() +{ + R_BSP_RegisterProtectDisable(BSP_REG_PROTECT_MPC); + + /* Set TSCAP pin */ + MPC.PC4PFS.BYTE = 0x19U; + PORTC.PMR.BIT.B4 = 1U; + + R_BSP_RegisterProtectEnable(BSP_REG_PROTECT_MPC); +} + diff --git a/zephyr/rx/rdp_cfg/r_config/r_ctsu_qe_config.h b/zephyr/rx/rdp_cfg/r_config/r_ctsu_qe_config.h new file mode 100644 index 00000000..809b10e4 --- /dev/null +++ b/zephyr/rx/rdp_cfg/r_config/r_ctsu_qe_config.h @@ -0,0 +1,255 @@ +/*********************************************************************************************************************** +* Copyright (c) 2013 - 2025 Renesas Electronics Corporation and/or its affiliates +* +* SPDX-License-Identifier: BSD-3-Clause +***********************************************************************************************************************/ +/*********************************************************************************************************************** + * File Name : r_ctsu_qe_config.h + * Description : Configures the QE CTSU API module. + ***********************************************************************************************************************/ +#ifndef CTSU_CONFIG_HEADER_FILE +#define CTSU_CONFIG_HEADER_FILE + + +/*********************************************************************************************************************** + Configuration Options + ***********************************************************************************************************************/ +/* SPECIFY WHETHER TO INCLUDE CODE FOR API PARAMETER CHECKING + * Setting to BSP_CFG_PARAM_CHECKING_ENABLE utilizes the system default setting + * Setting to 1 includes parameter checking; 0 compiles out parameter checking + */ +#define CTSU_CFG_PARAM_CHECKING_ENABLE (BSP_CFG_PARAM_CHECKING_ENABLE) + +/* Data transfer of INTCTSUWR and INTCTSURD. To use DTC, set this to 1. */ +#define CTSU_CFG_DTC_SUPPORT_ENABLE (0) + +/* Overall automatic judgement setting. To use, set this to 1. */ +#define CTSU_CFG_AUTO_JUDGE_ENABLE (0) + +/* Interrupt priority level */ +#define CTSU_CFG_INTCTSUWR_PRIORITY_LEVEL (2) +#define CTSU_CFG_INTCTSURD_PRIORITY_LEVEL (2) +#define CTSU_CFG_INTCTSUFN_PRIORITY_LEVEL (2) + +/* The following configurations are set according to the touch interface configuration, + * so they are not set in Smart Configurator. + * Set when using QE for Capacitive Touch. In that case, QE_TOUCH_CONFIGURATION is defined in the project, + * the definition in r_ctsu_config.h is invalid, and instead it is defined in qe_touch_define.h. + */ + +#ifndef QE_TOUCH_CONFIGURATION +#if(TOUCH_CFG_SERIAL_TUNING_SUPPORT == 1) +#define CTSU_CFG_NUM_SELF_ELEMENTS (35) +#define CTSU_CFG_NUM_MUTUAL_ELEMENTS (255) +#else +#define CTSU_CFG_NUM_SELF_ELEMENTS (DT_PROP(DT_NODELABEL(ctsu), max_num_sensors)) +#define CTSU_CFG_NUM_MUTUAL_ELEMENTS (0) +#endif +#define CTSU_CFG_LOW_VOLTAGE_MODE (DT_PROP(DT_NODELABEL(ctsu), low_voltage_mode)) +#define CTSU_CFG_PCLK_DIVISION (CONFIG_INPUT_RENESAS_RX_CTSU_PCLK_DIVISION) +#define CTSU_CFG_TSCAP_PORT \ + (DT_PROP(DT_GPIO_CTLR(DT_INST(0, renesas_rx_ctsu), tscap_gpios), port) << 8 | \ + DT_GPIO_PIN(DT_INST(0, renesas_ra_ctsu), tscap_gpios)) +#define CTSU_CFG_VCC_MV (3300) +#define CTSU_CFG_DIAG_SUPPORT_ENABLE (0) +#if (BSP_MCU_RX140) +#define CTSU_CFG_NUM_CFC (0) +#define CTSU_CFG_NUM_CFC_TX (0) +#if(TOUCH_CFG_SERIAL_TUNING_SUPPORT == 1) +#define CTSU_CFG_NUM_SUMULTI (1) +#else +#define CTSU_CFG_NUM_SUMULTI (3) +#endif +#define CTSU_CFG_SUMULTI0 (0x3F) +#define CTSU_CFG_SUMULTI1 (0x36) +#define CTSU_CFG_SUMULTI2 (0x48) +#define CTSU_CFG_TEMP_CORRECTION_SUPPORT (0) +#define CTSU_CFG_TEMP_CORRECTION_TS (0) +#define CTSU_CFG_TEMP_CORRECTION_TIME (0) +#define CTSU_CFG_CALIB_RTRIM_SUPPORT (0) +#else +#define CTSU_CFG_NUM_SUMULTI (1) +#endif +#endif + +#if (CTSU_CFG_DIAG_SUPPORT_ENABLE == 1) +#if (defined(BSP_MCU_RX113) || defined(BSP_MCU_RX130) || defined(BSP_MCU_RX23_ALL)) +#if (CTSU_CFG_LOW_VOLTAGE_MODE == 0) +#define CTSU_CFG_DIAG_CCO_HIGH_MAX (54888) +#define CTSU_CFG_DIAG_CCO_HIGH_MIN (29062) +#define CTSU_CFG_DIAG_CCO_LOW_MAX (3269) +#define CTSU_CFG_DIAG_CCO_LOW_MIN (705) +#define CTSU_CFG_DIAG_SSCG_MAX (21813) +#define CTSU_CFG_DIAG_SSCG_MIN (11782) +#define CTSU_CFG_DIAG_DAC1_MAX (20422) +#define CTSU_CFG_DIAG_DAC2_MAX (21532) +#define CTSU_CFG_DIAG_DAC3_MAX (23015) +#define CTSU_CFG_DIAG_DAC4_MAX (25923) +#define CTSU_CFG_DIAG_DAC5_MAX (32016) +#define CTSU_CFG_DIAG_DAC6_MAX (45795) +#define CTSU_CFG_DIAG_DAC1_MIN (12492) +#define CTSU_CFG_DIAG_DAC2_MIN (14053) +#define CTSU_CFG_DIAG_DAC3_MIN (15322) +#define CTSU_CFG_DIAG_DAC4_MIN (18070) +#define CTSU_CFG_DIAG_DAC5_MIN (23262) +#define CTSU_CFG_DIAG_DAC6_MIN (33446) +#else +#define CTSU_CFG_DIAG_CCO_HIGH_MAX (43910) +#define CTSU_CFG_DIAG_CCO_HIGH_MIN (23249) +#define CTSU_CFG_DIAG_CCO_LOW_MAX (2615) +#define CTSU_CFG_DIAG_CCO_LOW_MIN (564) +#define CTSU_CFG_DIAG_SSCG_MAX (21813) +#define CTSU_CFG_DIAG_SSCG_MIN (11782) +#define CTSU_CFG_DIAG_DAC1_MAX (16599) +#define CTSU_CFG_DIAG_DAC2_MAX (17226) +#define CTSU_CFG_DIAG_DAC3_MAX (18412) +#define CTSU_CFG_DIAG_DAC4_MAX (20738) +#define CTSU_CFG_DIAG_DAC5_MAX (25613) +#define CTSU_CFG_DIAG_DAC6_MAX (36636) +#define CTSU_CFG_DIAG_DAC1_MIN (9994) +#define CTSU_CFG_DIAG_DAC2_MIN (11242) +#define CTSU_CFG_DIAG_DAC3_MIN (12258) +#define CTSU_CFG_DIAG_DAC4_MIN (14456) +#define CTSU_CFG_DIAG_DAC5_MIN (18610) +#define CTSU_CFG_DIAG_DAC6_MIN (26757) +#endif +#endif +#if defined(BSP_MCU_RX671) +#define CTSU_CFG_DIAG_CCO_HIGH_MAX (36873) +#define CTSU_CFG_DIAG_CCO_HIGH_MIN (24433) +#define CTSU_CFG_DIAG_CCO_LOW_MAX (1781) +#define CTSU_CFG_DIAG_CCO_LOW_MIN (105) +#define CTSU_CFG_DIAG_SSCG_MAX (17795) +#define CTSU_CFG_DIAG_SSCG_MIN (9610) +#define CTSU_CFG_DIAG_DAC1_MAX (20422) +#define CTSU_CFG_DIAG_DAC2_MAX (21532) +#define CTSU_CFG_DIAG_DAC3_MAX (23015) +#define CTSU_CFG_DIAG_DAC4_MAX (25923) +#define CTSU_CFG_DIAG_DAC5_MAX (32016) +#define CTSU_CFG_DIAG_DAC6_MAX (45795) +#define CTSU_CFG_DIAG_DAC1_MIN (12492) +#define CTSU_CFG_DIAG_DAC2_MIN (14053) +#define CTSU_CFG_DIAG_DAC3_MIN (15322) +#define CTSU_CFG_DIAG_DAC4_MIN (18070) +#define CTSU_CFG_DIAG_DAC5_MIN (23262) +#define CTSU_CFG_DIAG_DAC6_MIN (33446) +#endif + +#if defined(BSP_MCU_RX140) +#define CTSU_DIAG_TSCAP_RANGE_LOW (1050) +#define CTSU_DIAG_TSCAP_RANGE_HIGH (1419) + +#define CTSU_CFG_DIAG_LOAD_REISTER_MIN (11951) +#define CTSU_CFG_DIAG_LOAD_REISTER_MAX (23957) + +#define CTSU_CFG_DIAG_CURRENT_SOURCE_DIFF_MIN (2890) +#define CTSU_CFG_DIAG_CURRENT_SOURCE_DIFF_MAX (5703) + +#if (CTSU_CFG_LOW_VOLTAGE_MODE == 0) +#define CTSU_CFG_DIAG_DAC1_MIN (1486) +#define CTSU_CFG_DIAG_DAC2_MIN (2890) +#define CTSU_CFG_DIAG_DAC3_MIN (4202) +#define CTSU_CFG_DIAG_DAC4_MIN (5448) +#define CTSU_CFG_DIAG_DAC5_MIN (6641) +#define CTSU_CFG_DIAG_DAC6_MIN (7787) +#define CTSU_CFG_DIAG_DAC7_MIN (8891) +#define CTSU_CFG_DIAG_DAC8_MIN (9954) +#define CTSU_CFG_DIAG_DAC9_MIN (10974) +#define CTSU_CFG_DIAG_DAC10_MIN (11951) +#define CTSU_CFG_DIAG_DAC11_MIN (12880) +#define CTSU_CFG_DIAG_DAC12_MIN (13761) + +#define CTSU_CFG_DIAG_DAC1_MAX (3057) +#define CTSU_CFG_DIAG_DAC2_MAX (5703) +#define CTSU_CFG_DIAG_DAC3_MAX (8292) +#define CTSU_CFG_DIAG_DAC4_MAX (10838) +#define CTSU_CFG_DIAG_DAC5_MAX (13269) +#define CTSU_CFG_DIAG_DAC6_MAX (15593) +#define CTSU_CFG_DIAG_DAC7_MAX (17816) +#define CTSU_CFG_DIAG_DAC8_MAX (19948) +#define CTSU_CFG_DIAG_DAC9_MAX (21992) +#define CTSU_CFG_DIAG_DAC10_MAX (23957) +#define CTSU_CFG_DIAG_DAC11_MAX (25843) +#define CTSU_CFG_DIAG_DAC12_MAX (27651) + +#define CTSU_CFG_DIAG_DAC1_2_DIFF_MIN (1384) +#define CTSU_CFG_DIAG_DAC2_3_DIFF_MIN (1293) +#define CTSU_CFG_DIAG_DAC3_4_DIFF_MIN (1228) +#define CTSU_CFG_DIAG_DAC4_5_DIFF_MIN (1176) +#define CTSU_CFG_DIAG_DAC5_6_DIFF_MIN (1129) +#define CTSU_CFG_DIAG_DAC6_7_DIFF_MIN (1083) +#define CTSU_CFG_DIAG_DAC7_8_DIFF_MIN (1037) +#define CTSU_CFG_DIAG_DAC8_9_DIFF_MIN (987) +#define CTSU_CFG_DIAG_DAC9_10_DIFF_MIN (929) +#define CTSU_CFG_DIAG_DAC10_11_DIFF_MIN (867) +#define CTSU_CFG_DIAG_DAC11_12_DIFF_MIN (807) + +#define CTSU_CFG_DIAG_DAC1_2_DIFF_MAX (2781) +#define CTSU_CFG_DIAG_DAC2_3_DIFF_MAX (2678) +#define CTSU_CFG_DIAG_DAC3_4_DIFF_MAX (2567) +#define CTSU_CFG_DIAG_DAC4_5_DIFF_MAX (2458) +#define CTSU_CFG_DIAG_DAC5_6_DIFF_MAX (2355) +#define CTSU_CFG_DIAG_DAC6_7_DIFF_MAX (2255) +#define CTSU_CFG_DIAG_DAC7_8_DIFF_MAX (2166) +#define CTSU_CFG_DIAG_DAC8_9_DIFF_MAX (2077) +#define CTSU_CFG_DIAG_DAC9_10_DIFF_MAX (1998) +#define CTSU_CFG_DIAG_DAC10_11_DIFF_MAX (1921) +#define CTSU_CFG_DIAG_DAC11_12_DIFF_MAX (1841) +#else + +#define CTSU_CFG_DIAG_DAC1_MIN (1039) +#define CTSU_CFG_DIAG_DAC2_MIN (2113) +#define CTSU_CFG_DIAG_DAC3_MIN (3130) +#define CTSU_CFG_DIAG_DAC4_MIN (4108) +#define CTSU_CFG_DIAG_DAC5_MIN (5049) +#define CTSU_CFG_DIAG_DAC6_MIN (5961) +#define CTSU_CFG_DIAG_DAC7_MIN (6848) +#define CTSU_CFG_DIAG_DAC8_MIN (7713) +#define CTSU_CFG_DIAG_DAC9_MIN (8554) +#define CTSU_CFG_DIAG_DAC10_MIN (9374) +#define CTSU_CFG_DIAG_DAC11_MIN (10176) +#define CTSU_CFG_DIAG_DAC12_MIN (10945) + +#define CTSU_CFG_DIAG_DAC1_MAX (2627) +#define CTSU_CFG_DIAG_DAC2_MAX (4899) +#define CTSU_CFG_DIAG_DAC3_MAX (6974) +#define CTSU_CFG_DIAG_DAC4_MAX (8907) +#define CTSU_CFG_DIAG_DAC5_MAX (10729) +#define CTSU_CFG_DIAG_DAC6_MAX (12476) +#define CTSU_CFG_DIAG_DAC7_MAX (14314) +#define CTSU_CFG_DIAG_DAC8_MAX (16089) +#define CTSU_CFG_DIAG_DAC9_MAX (17802) +#define CTSU_CFG_DIAG_DAC10_MAX (19462) +#define CTSU_CFG_DIAG_DAC11_MAX (21067) +#define CTSU_CFG_DIAG_DAC12_MAX (22624) + +#define CTSU_CFG_DIAG_DAC1_2_DIFF_MIN (1081) +#define CTSU_CFG_DIAG_DAC2_3_DIFF_MIN (1013) +#define CTSU_CFG_DIAG_DAC3_4_DIFF_MIN (967) +#define CTSU_CFG_DIAG_DAC4_5_DIFF_MIN (929) +#define CTSU_CFG_DIAG_DAC5_6_DIFF_MIN (899) +#define CTSU_CFG_DIAG_DAC6_7_DIFF_MIN (867) +#define CTSU_CFG_DIAG_DAC7_8_DIFF_MIN (844) +#define CTSU_CFG_DIAG_DAC8_9_DIFF_MIN (821) +#define CTSU_CFG_DIAG_DAC9_10_DIFF_MIN (794) +#define CTSU_CFG_DIAG_DAC10_11_DIFF_MIN (762) +#define CTSU_CFG_DIAG_DAC11_12_DIFF_MIN (733) + +#define CTSU_CFG_DIAG_DAC1_2_DIFF_MAX (2273) +#define CTSU_CFG_DIAG_DAC2_3_DIFF_MAX (2118) +#define CTSU_CFG_DIAG_DAC3_4_DIFF_MAX (2044) +#define CTSU_CFG_DIAG_DAC4_5_DIFF_MAX (1975) +#define CTSU_CFG_DIAG_DAC5_6_DIFF_MAX (1909) +#define CTSU_CFG_DIAG_DAC6_7_DIFF_MAX (1846) +#define CTSU_CFG_DIAG_DAC7_8_DIFF_MAX (1786) +#define CTSU_CFG_DIAG_DAC8_9_DIFF_MAX (1731) +#define CTSU_CFG_DIAG_DAC9_10_DIFF_MAX (1679) +#define CTSU_CFG_DIAG_DAC10_11_DIFF_MAX (1632) +#define CTSU_CFG_DIAG_DAC11_12_DIFF_MAX (1581) +#endif +#define CTSU_CFG_DIAG_CLOCK_RECOV_RANGE (20) +#endif +#endif + + +#endif /* TOUCH_CONFIG_HEADER_FILE */ From ce75b270809741c3d3cbfc05929c6c99bb08f249 Mon Sep 17 00:00:00 2001 From: Minh Tang Date: Mon, 15 Sep 2025 11:49:11 +0700 Subject: [PATCH 2/2] hal: renesas: rx: Add Touch Driver support for RX Add rm_touch source files support for CTSU driver Signed-off-by: Minh Tang --- drivers/rx/CMakeLists.txt | 5 + .../rx/rdp/src/rm_touch_qe/inc/rm_touch_qe.h | 199 + .../rdp/src/rm_touch_qe/inc/rm_touch_qe_api.h | 259 + .../rx/rdp/src/rm_touch_qe/rm_touch_qe_if.h | 36 + .../rx/rdp/src/rm_touch_qe/src/rm_touch_qe.c | 4997 +++++++++++++++++ .../rx/rdp_cfg/r_config/rm_touch_qe_config.h | 41 + 6 files changed, 5537 insertions(+) create mode 100644 drivers/rx/rdp/src/rm_touch_qe/inc/rm_touch_qe.h create mode 100644 drivers/rx/rdp/src/rm_touch_qe/inc/rm_touch_qe_api.h create mode 100644 drivers/rx/rdp/src/rm_touch_qe/rm_touch_qe_if.h create mode 100644 drivers/rx/rdp/src/rm_touch_qe/src/rm_touch_qe.c create mode 100644 zephyr/rx/rdp_cfg/r_config/rm_touch_qe_config.h diff --git a/drivers/rx/CMakeLists.txt b/drivers/rx/CMakeLists.txt index 68cda01c..a367f372 100644 --- a/drivers/rx/CMakeLists.txt +++ b/drivers/rx/CMakeLists.txt @@ -141,9 +141,14 @@ if(CONFIG_USE_RX_RDP_CTSU) zephyr_library_sources( rdp/src/r_ctsu_qe/src/r_ctsu_qe.c rdp/src/r_ctsu_qe/src/r_ctsu_qe_pinset.c + rdp/src/rm_touch_qe/src/rm_touch_qe.c ) zephyr_include_directories( rdp/src/r_ctsu_qe rdp/src/r_ctsu_qe/inc + rdp/src/rm_touch_qe + rdp/src/rm_touch_qe/inc ) endif() + +zephyr_compile_definitions_ifdef(CONFIG_INPUT_RENESAS_RX_QE_TOUCH_CFG QE_TOUCH_CONFIGURATION) diff --git a/drivers/rx/rdp/src/rm_touch_qe/inc/rm_touch_qe.h b/drivers/rx/rdp/src/rm_touch_qe/inc/rm_touch_qe.h new file mode 100644 index 00000000..fe27bde0 --- /dev/null +++ b/drivers/rx/rdp/src/rm_touch_qe/inc/rm_touch_qe.h @@ -0,0 +1,199 @@ +/* +* Copyright (c) 2018 Renesas Electronics Corporation and/or its affiliates +* +* SPDX-License-Identifier: BSD-3-Clause +*/ +/*********************************************************************************************************************** +* File Name : rm_touch_qe.h +* Description : This file contains the TOUCH API and should be included by the application which uses this API. +***********************************************************************************************************************/ + +/*******************************************************************************************************************//** + * @addtogroup TOUCH + * @{ + **********************************************************************************************************************/ + +#ifndef RM_TOUCH_H +#define RM_TOUCH_H + +#include "platform.h" + +#include "rm_touch_qe_config.h" +#include "rm_touch_qe_api.h" +#include "r_ctsu_qe.h" + +/* Common macro for FSP header files. There is also a corresponding FSP_FOOTER macro at the end of this file. */ +FSP_HEADER + +/*********************************************************************************************************************** + * Macro definitions + **********************************************************************************************************************/ +#if (CTSU_CFG_AUTO_JUDGE_ENABLE == 1) +#define TOUCH_AUTO_JUDGE_MONITOR (1) +#endif + +/*********************************************************************************************************************** + * Typedef definitions + **********************************************************************************************************************/ + +#if (TOUCH_CFG_UART_TUNING_SUPPORT == 1) + +/** CTSU state number */ +typedef enum e_touch_tuning_state +{ + TOUCH_TUNING_STATE_RESET, + TOUCH_TUNING_STATE_INITIALIZE, + TOUCH_TUNING_STATE_SCAN, + TOUCH_TUNING_STATE_COMPLETE, + TOUCH_TUNING_STATE_STOP +} touch_tuning_state_t; + +typedef enum e_touch_tuning_mode +{ + TOUCH_TUNING_MODE_CORRECTION_1ST_VALUE, + TOUCH_TUNING_MODE_CORRECTION_2ND_VALUE, + TOUCH_TUNING_MODE_MEASURE_PHASE1, + TOUCH_TUNING_MODE_MEASURE_PHASE2, + TOUCH_TUNING_MODE_MEASURE_PHASE3, + TOUCH_TUNING_MODE_QE_WATTING, + TOUCH_TUNING_MODE_START, + TOUCH_TUNING_MODE_FINISH, +} touch_tuning_mode_t; + +typedef enum e_touch_tuning_scan +{ + TOUCH_TUNING_SCAN_SELF, + TOUCH_TUNING_SCAN_MUTUAL, + TOUCH_TUNING_SCAN_MUTUAL_CFC, + TOUCH_TUNING_SCAN_CURRENT, +} touch_tuning_scan_t; + +typedef struct st_ico +{ + uint16_t pri_sen; + uint16_t snd_sen; +} touch_tuning_ico_t; + +typedef struct +{ + uint32_t mska; + uint32_t mskb; +} touch_tuning_ts_msk_t; +#endif + +/** Information of button */ +typedef struct +{ + uint64_t status; ///< Touch result bitmap. + uint16_t * p_threshold; ///< Pointer to Threshold value array. g_touch_button_threshold[] is set by Open API. + uint16_t * p_hysteresis; ///< Pointer to Hysteresis value array. g_touch_button_hysteresis[] is set by Open API. + uint16_t * p_reference; ///< Pointer to Reference value array. g_touch_button_reference[] is set by Open API. + uint16_t * p_on_count; ///< Continuous touch counter. g_touch_button_on_count[] is set by Open API. + uint16_t * p_off_count; ///< Continuous non-touch counter. g_touch_button_off_count[] is set by Open API. + uint32_t * p_drift_buf; ///< Drift reference value. g_touch_button_drift_buf[] is set by Open API. + uint16_t * p_drift_count; ///< Drift counter. g_touch_button_drift_count[] is set by Open API. + uint8_t on_freq; ///< Copy from config by Open API. + uint8_t off_freq; ///< Copy from config by Open API. + uint16_t drift_freq; ///< Copy from config by Open API. + uint16_t cancel_freq; ///< Copy from config by Open API. +} touch_button_info_t; + +/** Information of slider */ +typedef struct +{ + uint16_t * p_position; ///< Calculated Position data. g_touch_slider_position[] is set by Open API. + uint16_t * p_threshold; ///< Copy from config by Open API. g_touch_slider_threshold[] is set by Open API. +} touch_slider_info_t; + +/** Information of wheel */ +typedef struct +{ + uint16_t * p_position; ///< Calculated Position data. g_touch_wheel_position[] is set by Open API. + uint16_t * p_threshold; ///< Copy from config by Open API. g_touch_wheel_threshold[] is set by Open API. +} touch_wheel_info_t; + +/** Information of pad */ +typedef struct +{ + uint16_t * p_rx_coordinate; ///< RX coordinate + uint16_t * p_tx_coordinate; ///< TX coordinate + uint16_t * p_num_touch; ///< number of touch + uint16_t * p_threshold; ///< Coordinate calculation threshold value. + uint16_t * p_base_buf; ///< ScanData Base Value Buffer. + uint16_t * p_rx_pixel; ///< X coordinate resolution + uint16_t * p_tx_pixel; ///< Y coordinate resolution + uint8_t * p_max_touch; ///< Maximum number of touch judgments used by the pad. + int32_t * p_drift_buf; ///< Drift reference value. g_touch_button_drift_buf[] is set by Open API. + uint16_t * p_drift_count; ///< Drift counter. g_touch_button_drift_count[] is set by Open API. + uint8_t num_drift; ///< Copy from config by Open API. +} touch_pad_info_t; + +/** Information of touch button judge(CTSU2) */ +typedef struct st_touch_mm_info +{ + uint8_t id; + uint8_t mm_index; + uint16_t mm_result[CTSU_MAJORITY_MODE_ELEMENTS]; + uint8_t majority_mode; +} touch_mm_info_t; + +/** TOUCH private control block. DO NOT MODIFY. Initialization occurs when RM_TOUCH_Open() is called. */ +typedef struct st_touch_instance_ctrl +{ + uint32_t open; ///< Whether or not driver is open. + touch_button_info_t binfo; ///< Information of button. + touch_slider_info_t sinfo; ///< Information of slider. + touch_wheel_info_t winfo; ///< Information of wheel. + bool serial_tuning_enable; ///< Flag of serial tuning status. + touch_pad_info_t pinfo; ///< Information of pad. + touch_cfg_t const * p_touch_cfg; ///< Pointer to initial configurations. + ctsu_instance_t const * p_ctsu_instance; ///< Pointer to CTSU instance. + touch_mm_info_t * p_touch_mm_info; ///< Pointer to information of touch button judge +} touch_instance_ctrl_t; + + +/********************************************************************************************************************** + * Exported global variables + **********************************************************************************************************************/ + +/** @cond INC_HEADER_DEFS_SEC */ +/** Filled in Interface API structure for this Instance. */ +extern const touch_api_t g_touch_on_ctsu; + +/** @endcond */ + +/********************************************************************************************************************** + * Public Function Prototypes + **********************************************************************************************************************/ +fsp_err_t RM_TOUCH_Open(touch_ctrl_t * const p_ctrl, touch_cfg_t const * const p_cfg); +fsp_err_t RM_TOUCH_ScanStart(touch_ctrl_t * const p_ctrl); +fsp_err_t RM_TOUCH_DataGet(touch_ctrl_t * const p_ctrl, + uint64_t * p_button_status, + uint16_t * p_slider_position, + uint16_t * p_wheel_position); +fsp_err_t RM_TOUCH_PadDataGet(touch_ctrl_t * const p_ctrl, + uint16_t * p_pad_rx_coordinate, + uint16_t * p_pad_tx_coordinate, + uint8_t * p_pad_num_touch); +fsp_err_t RM_TOUCH_ScanStop(touch_ctrl_t * const p_ctrl); +fsp_err_t RM_TOUCH_CallbackSet(touch_ctrl_t * const p_api_ctrl, + void ( * p_callback)(touch_callback_args_t *), + void const * const p_context, + touch_callback_args_t * const p_callback_memory); +fsp_err_t RM_TOUCH_Close(touch_ctrl_t * const p_ctrl); +fsp_err_t RM_TOUCH_SensitivityRatioGet(touch_ctrl_t * const p_ctrl, + touch_sensitivity_info_t * p_touch_sensitivity_info); +fsp_err_t RM_TOUCH_ThresholdAdjust(touch_ctrl_t * const p_ctrl, touch_sensitivity_info_t * p_touch_sensitivity_info); +fsp_err_t RM_TOUCH_DriftControl(touch_ctrl_t * const p_ctrl, uint16_t input_drift_freq); +fsp_err_t RM_TOUCH_MonitorAddressGet (touch_ctrl_t * const p_ctrl, + uint8_t ** pp_monitor_buf, + uint8_t ** pp_monitor_id, + uint16_t ** pp_monitor_size); +/* Common macro for FSP header files. There is also a corresponding FSP_HEADER macro at the top of this file. */ +FSP_FOOTER + +#endif // RM_TOUCH_H + +/*******************************************************************************************************************//** + * @} (end addtogroup TOUCH) + **********************************************************************************************************************/ diff --git a/drivers/rx/rdp/src/rm_touch_qe/inc/rm_touch_qe_api.h b/drivers/rx/rdp/src/rm_touch_qe/inc/rm_touch_qe_api.h new file mode 100644 index 00000000..062548a6 --- /dev/null +++ b/drivers/rx/rdp/src/rm_touch_qe/inc/rm_touch_qe_api.h @@ -0,0 +1,259 @@ +/* +* Copyright (c) 2018 Renesas Electronics Corporation and/or its affiliates +* +* SPDX-License-Identifier: BSD-3-Clause +*/ +/*********************************************************************************************************************** +* File Name : rm_touch_qe_api.h +* Description : This file contains the TOUCH API and should be included by the application which uses this API. +***********************************************************************************************************************/ + +/*******************************************************************************************************************//** + * @ingroup RENESAS_INTERFACES + * @defgroup TOUCH_API Touch Middleware Interface + * @brief Interface for Touch Middleware functions. + * + * @section TOUCH_API_Summary Summary + * The TOUCH interface provides TOUCH functionality. + * + * The TOUCH interface can be implemented by: + * - @ref TOUCH + * + * @{ + **********************************************************************************************************************/ + +#ifndef RM_TOUCH_API_H +#define RM_TOUCH_API_H + +/*********************************************************************************************************************** + * Includes + **********************************************************************************************************************/ + +/* Register definitions, common services and error codes. */ +#include "platform.h" +#include "r_ctsu_qe_api.h" +#if (TOUCH_CFG_UART_MONITOR_SUPPORT == 1) + #if (TOUCH_CFG_UART_MODULE_TYPE == 0) + #include "r_sci_rx_if.h" + #elif (TOUCH_CFG_UART_MODULE_TYPE == 1) + #include "r_rsci_rx_if.h" + #endif +#endif +/* Common macro for FSP header files. There is also a corresponding FSP_FOOTER macro at the end of this file. */ +FSP_HEADER + +/********************************************************************************************************************** + * Macro definitions + **********************************************************************************************************************/ + +#define TOUCH_COUNT_MAX CTSU_COUNT_MAX ///< Value of Maximum count +#define TOUCH_OFF_VALUE (0xFFFF) ///< Value of Non-touch + +/********************************************************************************************************************** + * Typedef definitions + **********************************************************************************************************************/ + +/** Control block. Allocate an instance specific control block to pass into the API calls. + * @par Implemented as + * - touch_instance_ctrl_t + */ +typedef void touch_ctrl_t; + +/** Configuration of each button */ +typedef struct st_touch_button_cfg +{ + uint8_t elem_index; ///< Element number used by this button. + uint16_t threshold; ///< Touch/non-touch judgment threshold + uint16_t hysteresis; ///< Threshold hysteresis for chattering prevention. +} touch_button_cfg_t; + +/** Configuration of each slider */ +typedef struct st_touch_slider_cfg +{ + uint8_t const * p_elem_index; ///< Element number array used by this slider. + uint8_t num_elements; ///< Number of elements used by this slider. + uint16_t threshold; ///< Position calculation start threshold value. +} touch_slider_cfg_t; + +/** Configuration of each wheel */ +typedef struct st_touch_wheel_cfg_t +{ + uint8_t const * p_elem_index; ///< Element number array used by this wheel. + uint8_t num_elements; ///< Number of elements used by this wheel. + uint16_t threshold; ///< Position calculation start threshold value. +} touch_wheel_cfg_t; + +/** Configuration of each pads */ +typedef struct st_touch_pad_cfg_t +{ + uint8_t const * p_elem_index_rx; ///< RX of element number arrays used by this pad. + uint8_t const * p_elem_index_tx; ///< TX of element number arrays used by this pad. + uint8_t num_elements; ///< Number of elements used by this pad. + uint16_t threshold; ///< Coordinate calculation threshold value. + uint16_t rx_pixel; ///< rx coordinate resolution + uint16_t tx_pixel; ///< tx coordinate resolution + uint8_t max_touch; ///< Maximum number of touch judgments used by the pad. + uint8_t num_drift; ///< Number of pad drift. +} touch_pad_cfg_t; + +/** Callback function parameter data */ +typedef struct st_ctsu_callback_args touch_callback_args_t; + +/** User configuration structure, used in open function */ +typedef struct st_touch_cfg +{ + touch_button_cfg_t const * p_buttons; ///< Pointer to array of button configuration. + touch_slider_cfg_t const * p_sliders; ///< Pointer to array of slider configuration. + touch_wheel_cfg_t const * p_wheels; ///< Pointer to array of wheel configuration. + touch_pad_cfg_t const * p_pad; ///< Pointer of pad configuration. + uint8_t num_buttons; ///< Number of buttons. + uint8_t num_sliders; ///< Number of sliders. + uint8_t num_wheels; ///< Number of wheels. + uint8_t on_freq; ///< The cumulative number of determinations of ON. + uint8_t off_freq; ///< The cumulative number of determinations of OFF. + uint16_t drift_freq; ///< Base value drift frequency. [0 : no use] + uint16_t cancel_freq; ///< Maximum continuous ON. [0 : no use] + uint8_t number; ///< Configuration number for QE monitor. + ctsu_instance_t const * p_ctsu_instance; ///< Pointer to CTSU instance. + void const * p_context; ///< User defined context passed into callback function. + void const * p_extend; ///< Pointer to extended configuration by instance of interface. +} touch_cfg_t; + +/** Configuration of each touch sensitivity information */ +typedef struct st_touch_sensitivity_info +{ + uint16_t * p_touch_sensitivity_ratio; ///< Pointer to sensitivity ratio array. + uint16_t old_threshold_ratio; ///< Old threshold ratio. + uint16_t new_threshold_ratio; ///< New threshold ratio. + uint8_t new_hysteresis_ratio; ///< New hysteresis ratio. +} touch_sensitivity_info_t; + +/** Functions implemented at the HAL layer will follow this API. */ +typedef struct st_touch_api +{ + /** Open driver. + * @par Implemented as + * - @ref RM_TOUCH_Open() + * + * @param[in] p_ctrl Pointer to control structure. + * @param[in] p_cfg Pointer to pin configuration structure. + */ + fsp_err_t (* open)(touch_ctrl_t * const p_ctrl, touch_cfg_t const * const p_cfg); + + /** Scan start. + * @par Implemented as + * - @ref RM_TOUCH_ScanStart() + * + * @param[in] p_ctrl Pointer to control structure. + */ + fsp_err_t (* scanStart)(touch_ctrl_t * const p_ctrl); + + /** Data get. + * @par Implemented as + * - @ref RM_TOUCH_DataGet() + * + * @param[in] p_ctrl Pointer to control structure. + * @param[out] p_button_status Pointer to get data bitmap. + * @param[out] p_slider_position Pointer to get data array. + * @param[out] p_wheel_position Pointer to get data array. + */ + fsp_err_t (* dataGet)(touch_ctrl_t * const p_ctrl, uint64_t * p_button_status, uint16_t * p_slider_position, + uint16_t * p_wheel_position); + + /** ScanStop. + * @par Implemented as + * - @ref RM_TOUCH_ScanStop() + * + * @param[in] p_ctrl Pointer to control structure. + */ + fsp_err_t (* scanStop)(ctsu_ctrl_t * const p_ctrl); + + /** pad data get. + * @par Implemented as + * - @ref RM_TOUCH_PadDataGet() + * + * @param[in] p_ctrl Pointer to control structure. + * @param[out] p_pad_rx_coordinate Pointer to get coordinate of receiver side. + * @param[out] p_pad_tx_coordinate Pointer to get coordinate of transmitter side. + * @param[out] p_pad_num_touch Pointer to get touch count. + */ + fsp_err_t (* padDataGet)(touch_ctrl_t * const p_ctrl, uint16_t * p_pad_rx_coordinate, + uint16_t * p_pad_tx_coordinate, uint8_t * p_pad_num_touch); + + /** Specify callback function and optional context pointer and working memory pointer. + * @par Implemented as + * - @ref RM_TOUCH_CallbackSet() + * + * @param[in] p_ctrl Pointer to the CTSU control block. + * @param[in] p_callback Callback function + * @param[in] p_context Pointer to send to callback function + * @param[in] p_working_memory Pointer to volatile memory where callback structure can be allocated. + * Callback arguments allocated here are only valid during the callback. + */ + fsp_err_t (* callbackSet)(touch_ctrl_t * const p_api_ctrl, void (* p_callback)(touch_callback_args_t *), + void const * const p_context, touch_callback_args_t * const p_callback_memory); + + /** Close driver. + * @par Implemented as + * - @ref RM_TOUCH_Close() + * + * @param[in] p_ctrl Pointer to control structure. + */ + fsp_err_t (* close)(touch_ctrl_t * const p_ctrl); + + /** Sensitivity ratio get. + * @par Implemented as + * - @ref RM_TOUCH_SensitivityRatioGet() + * + * @param[in] p_ctrl Pointer to control structure. + * @param[in,out] p_touch_sensitivity_info Pointer to touch sensitivity structure. + */ + fsp_err_t (* sensitivityRatioGet)(touch_ctrl_t * const p_ctrl, touch_sensitivity_info_t * p_touch_sensitivity_info); + + /** Threshold adjust. + * @par Implemented as + * - @ref RM_TOUCH_ThresholdAdjust() + * + * @param[in] p_ctrl Pointer to control structure. + * @param[in] p_touch_sensitivity_info Pointer to touch sensitivity structure. + */ + fsp_err_t (* thresholdAdjust)(touch_ctrl_t * const p_ctrl, touch_sensitivity_info_t * p_touch_sensitivity_info); + + /** Drift control. + * @par Implemented as + * - @ref RM_TOUCH_DriftControl() + * + * @param[in] p_ctrl Pointer to control structure. + * @param[in] input_drift_freq Drift frequency value. + */ + fsp_err_t (* driftControl)(touch_ctrl_t * const p_ctrl, uint16_t input_drift_freq); + + /** Monitor Address Get. + * @par Implemented as + * - @ref RM_TOUCH_MonitorAddressGet() + * + * @param[in] p_ctrl Pointer to control structure. + * @param[out] pp_monitor_buf monitoring buffer address. + * @param[out] pp_monitor_id monitoring ID variable address. + * @param[out] pp_monitor_size monitoring size array address. + */ + fsp_err_t (* monitorAddressGet)(touch_ctrl_t * const p_ctrl, uint8_t ** pp_monitor_buf, + uint8_t ** pp_monitor_id, uint16_t ** pp_monitor_size); +} touch_api_t; + +/** This structure encompasses everything that is needed to use an instance of this interface. */ +typedef struct st_touch_instance +{ + touch_ctrl_t * p_ctrl; ///< Pointer to the control structure for this instance + touch_cfg_t const * p_cfg; ///< Pointer to the configuration structure for this instance + touch_api_t const * p_api; ///< Pointer to the API structure for this instance +} touch_instance_t; + +/* Common macro for FSP header files. There is also a corresponding FSP_HEADER macro at the top of this file. */ +FSP_FOOTER + +#endif /* RM_TOUCH_API_H */ + +/*******************************************************************************************************************//** + * @} (end addtogroup TOUCH_API) + **********************************************************************************************************************/ diff --git a/drivers/rx/rdp/src/rm_touch_qe/rm_touch_qe_if.h b/drivers/rx/rdp/src/rm_touch_qe/rm_touch_qe_if.h new file mode 100644 index 00000000..c0ca2af7 --- /dev/null +++ b/drivers/rx/rdp/src/rm_touch_qe/rm_touch_qe_if.h @@ -0,0 +1,36 @@ +/* +* Copyright (c) 2018 Renesas Electronics Corporation and/or its affiliates +* +* SPDX-License-Identifier: BSD-3-Clause +*/ +/*********************************************************************************************************************** +* File Name : rm_touch_qe_if.h +* Description : This file contains the TOUCH API and should be included by the application which uses this API. +***********************************************************************************************************************/ +#ifndef QETOUCH_RX_IF_H_FILE +#define QETOUCH_RX_IF_H_FILE + +#include "rm_touch_qe_api.h" +#include "rm_touch_qe.h" + + +/*********************************************************************************************************************** +* Macro definitions +***********************************************************************************************************************/ + +/*********************************************************************************************************************** +* Typedef definitions +***********************************************************************************************************************/ + + +/*********************************************************************************************************************** +* Exported global variables +***********************************************************************************************************************/ + + +/*********************************************************************************************************************** +* Exported global functions +***********************************************************************************************************************/ + + +#endif // QETOUCH_RX_IF_H_FILE diff --git a/drivers/rx/rdp/src/rm_touch_qe/src/rm_touch_qe.c b/drivers/rx/rdp/src/rm_touch_qe/src/rm_touch_qe.c new file mode 100644 index 00000000..997fa7fe --- /dev/null +++ b/drivers/rx/rdp/src/rm_touch_qe/src/rm_touch_qe.c @@ -0,0 +1,4997 @@ +/* +* Copyright (c) 2018 Renesas Electronics Corporation and/or its affiliates +* +* SPDX-License-Identifier: BSD-3-Clause +*/ +/*********************************************************************************************************************** +* File Name : rm_touch_qe.c +* Description : This module implements the QE TOUCH API +***********************************************************************************************************************/ + +/*********************************************************************************************************************** + * Includes + **********************************************************************************************************************/ +#ifdef QE_TOUCH_CONFIGURATION + #include "qe_touch_define.h" +#endif +#include "rm_touch_qe_if.h" +#include "r_ctsu_qe_api.h" +#include "r_ctsu_qe_config.h" +#if ((TOUCH_CFG_UART_MONITOR_SUPPORT == 1) || (TOUCH_CFG_UART_TUNING_SUPPORT == 1)) + #if (TOUCH_CFG_UART_MODULE_TYPE == 0) + #include "r_sci_rx_if.h" + #elif (TOUCH_CFG_UART_MODULE_TYPE == 1) + #include "r_rsci_rx_if.h" + #endif +#endif + +/*********************************************************************************************************************** + * Macro definitions + **********************************************************************************************************************/ +#ifndef TOUCH_ERROR_RETURN + + #define TOUCH_ERROR_RETURN(a, err) FSP_ERROR_RETURN((a), (err)) +#endif + +/** "TOUC" in ASCII, used to determine if device is open. */ +#define TOUCH_OPEN (0x544F5543ULL) + +#define TOUCH_SLIDER_ELEMENTS_MIN (3) +#define TOUCH_SLIDER_ELEMENTS_MAX (10) +#define TOUCH_WHEEL_ELEMENTS_SMALL (4) +#define TOUCH_WHEEL_ELEMENTS_LARGE (8) +#define TOUCH_SLIDER_RESOLUTION (100) +#define TOUCH_WHEEL_RESOLUTION (360) +#define TOUCH_DECIMAL_POINT_PRECISION (100) + +#if (CTSU_CFG_MAJORITY_MODE & CTSU_JUDGEMENT_MAJORITY_MODE) + #define TOUCH_CFG_MAJORITY_NUM (2) +#endif + +#if TOUCH_CFG_MONITOR_ENABLE + #define TOUCH_MONITOR_BLOCK_MAX (8) + #define TOUCH_MONITOR_HEADER_SIZE (4) + #define TOUCH_MONITOR_SELF_ELEMENT_SIZE (2) + #define TOUCH_MONITOR_MUTUAL_ELEMENT_SIZE (4) + #if (TOUCH_CFG_NUM_BUTTONS != 0) + #define TOUCH_MONITOR_BHEAD_SIZE (2) + #else + #define TOUCH_MONITOR_BHEAD_SIZE (0) + #endif + #if (TOUCH_CFG_NUM_SLIDERS != 0) + #define TOUCH_MONITOR_SHEAD_SIZE (2) + #else + #define TOUCH_MONITOR_SHEAD_SIZE (0) + #endif + #if (TOUCH_CFG_NUM_WHEELS != 0) + #define TOUCH_MONITOR_WHEAD_SIZE (2) + #else + #define TOUCH_MONITOR_WHEAD_SIZE (0) + #endif + #if (CTSU_CFG_MAJORITY_MODE & CTSU_JUDGEMENT_MAJORITY_MODE) + #define TOUCH_MONITOR_BUTTON_SIZE (1) + #define TOUCH_MONITOR_BUTTON_OPTION_SIZE (6) + #else + #define TOUCH_MONITOR_BUTTON_SIZE (7) + #endif + #define TOUCH_MONITOR_SLIDER_SIZE (4) + #define TOUCH_MONITOR_WHEEL_SIZE (4) + #if (TOUCH_CFG_PAD_ENABLE) + #define TOUCH_MONITOR_PAD_ELEMENT_SIZE (2) + #define TOUCH_MONITOR_PAD_COORD_SIZE (40) + #define TOUCH_MONITOR_PAD_SIZE (11) + #define TOUCH_MONITOR_PAD_TOTAL_SIZE (TOUCH_MONITOR_PAD_COORD_SIZE + TOUCH_MONITOR_PAD_SIZE) + #else + #define TOUCH_MONITOR_PAD_TOTAL_SIZE (0) + #endif + #define TOUCH_MONITOR_FOOTER_SIZE (1) + #if (CTSU_CFG_MAJORITY_MODE & CTSU_JUDGEMENT_MAJORITY_MODE) + #define TOUCH_MONITOR_BUFFER_SIZE ((TOUCH_MONITOR_HEADER_SIZE * TOUCH_MONITOR_BLOCK_MAX) + \ + (TOUCH_MONITOR_SELF_ELEMENT_SIZE * CTSU_CFG_NUM_SELF_ELEMENTS * \ + CTSU_MAJORITY_MODE_ELEMENTS) + \ + (TOUCH_MONITOR_MUTUAL_ELEMENT_SIZE * CTSU_CFG_NUM_MUTUAL_ELEMENTS * \ + CTSU_MAJORITY_MODE_ELEMENTS) + \ + (TOUCH_MONITOR_BHEAD_SIZE * TOUCH_MONITOR_BLOCK_MAX) + \ + (TOUCH_MONITOR_BUTTON_SIZE * TOUCH_CFG_NUM_BUTTONS) + \ + (TOUCH_MONITOR_BUTTON_OPTION_SIZE * TOUCH_CFG_NUM_BUTTONS * \ + CTSU_MAJORITY_MODE_ELEMENTS) + \ + (TOUCH_MONITOR_SHEAD_SIZE * TOUCH_MONITOR_BLOCK_MAX) + \ + (TOUCH_MONITOR_SLIDER_SIZE * TOUCH_CFG_NUM_SLIDERS) + \ + (TOUCH_MONITOR_WHEAD_SIZE * TOUCH_MONITOR_BLOCK_MAX) + \ + (TOUCH_MONITOR_WHEEL_SIZE * TOUCH_CFG_NUM_WHEELS) + \ + (TOUCH_MONITOR_PAD_TOTAL_SIZE) + \ + TOUCH_MONITOR_FOOTER_SIZE * TOUCH_MONITOR_BLOCK_MAX) + #else + #define TOUCH_MONITOR_BUFFER_SIZE ((TOUCH_MONITOR_HEADER_SIZE * TOUCH_MONITOR_BLOCK_MAX) + \ + (TOUCH_MONITOR_SELF_ELEMENT_SIZE * CTSU_CFG_NUM_SELF_ELEMENTS) + \ + (TOUCH_MONITOR_MUTUAL_ELEMENT_SIZE * CTSU_CFG_NUM_MUTUAL_ELEMENTS) + \ + (TOUCH_MONITOR_BHEAD_SIZE * TOUCH_MONITOR_BLOCK_MAX) + \ + (TOUCH_MONITOR_BUTTON_SIZE * TOUCH_CFG_NUM_BUTTONS) + \ + (TOUCH_MONITOR_SHEAD_SIZE * TOUCH_MONITOR_BLOCK_MAX) + \ + (TOUCH_MONITOR_SLIDER_SIZE * TOUCH_CFG_NUM_SLIDERS) + \ + (TOUCH_MONITOR_WHEAD_SIZE * TOUCH_MONITOR_BLOCK_MAX) + \ + (TOUCH_MONITOR_WHEEL_SIZE * TOUCH_CFG_NUM_WHEELS) + \ + (TOUCH_MONITOR_PAD_TOTAL_SIZE) + \ + TOUCH_MONITOR_FOOTER_SIZE * TOUCH_MONITOR_BLOCK_MAX) + #endif + + #if (TOUCH_CFG_UART_MONITOR_SUPPORT == 1) + +/* Monitor none value */ + #define TOUCH_UART_MONITOR_NONE (0xFF) + +/* Control byte */ + #define TOUCH_UART_HEADER (0x55) + #define TOUCH_UART_FOOTER (0x0A) + +/* Command ID */ + #define TOUCH_UART_COMMAND_START (0x11) + #define TOUCH_UART_COMMAND_STOP (0x12) + #define TOUCH_UART_COMMAND_READ (0x13) + #define TOUCH_UART_COMMAND_WRITE (0x14) + #define TOUCH_UART_COMMAND_VERSION (0x15) + #define TOUCH_UART_COMMAND_THREE_FREQ_READ (0x16) + + #define TOUCH_UART_COMMAND_TOP_NUM (0x1) + +/* Response ID */ + #define TOUCH_UART_RESPONSE_BIT (0x80) + #define TOUCH_UART_RESPONSE_ERROR_BIT (0xA0) + #define TOUCH_UART_RESPONSE_MONITOR (0xA1) + #define TOUCH_UART_RESPONSE_MONITOR2 (0xA2) + +/* Write Request Type */ + #define TOUCH_UART_WRITE_DRIFT (0x01) + #define TOUCH_UART_WRITE_CANCEL (0x02) + #define TOUCH_UART_WRITE_POSITIVE (0x03) + #define TOUCH_UART_WRITE_NEGATIVE (0x04) + #define TOUCH_UART_WRITE_MOVING_AVG (0x05) + #define TOUCH_UART_WRITE_THRESHOLD (0x06) + #define TOUCH_UART_WRITE_HYSTERESIS (0x07) + #define TOUCH_UART_WRITE_THRESH_SLDR (0x08) + #define TOUCH_UART_WRITE_THRESH_WHEL (0x09) + #define TOUCH_UART_WRITE_CTSUSO (0x0A) + #define TOUCH_UART_WRITE_CTSUSNUM (0x0B) + #define TOUCH_UART_WRITE_CTSUSDPA (0x0C) + #define TOUCH_UART_WRTIE_PAD_THRESHOLD (0x0D) + #define TOUCH_UART_WRTIE_PAD_RX_PIXEL (0x0E) + #define TOUCH_UART_WRTIE_PAD_TX_PIXEL (0x0F) + #define TOUCH_UART_WRTIE_PAD_MAX_TOUCH (0x10) + #define TOUCH_UART_WRTIE_PAD_DRIFT (0x11) + #define TOUCH_UART_WRITE_THRESHOLD2 (0x12) + #define TOUCH_UART_WRITE_HYSTERESIS2 (0x13) + #define TOUCH_UART_WRITE_THRESHOLD3 (0x16) + #define TOUCH_UART_WRITE_HYSTERESIS3 (0x17) + +/* Method Number Maximum */ + #define TOUCH_UART_INSTANCE_MAX (32) + +/* UART Receive Buffer Size */ + #define TOUCH_UART_RECIEVE_BUF_SIZE (13) + +/* UART Command version */ + #define TOUCH_UART_VERSION_MAJOR ((uint8_t) 0x01U) + #define TOUCH_UART_VERSION_MINOR ((uint8_t) 0x00U) + + #endif + +/* Method Number Maximum */ + #define TOUCH_UART_INSTANCE_MAX (32) +#endif +#if (TOUCH_CFG_PAD_ENABLE) + #define TOUCH_MAP_X (3) + #define TOUCH_MAP_Y (3) + #define TOUCH_PAD_TEMP_VALUE_OVERFLOW_BIT (0x8000) + #define TOUCH_PAD_MONITOR_TOUCH_NUM_MAX (10) +#endif + +#if (CTSU_CFG_SUMULTI0 == 0x3F) +#define TOUCH_CFG_PHASE1_SDPA_VALUE (0x1F) +#endif +#if (CTSU_CFG_SUMULTI0 == 0x2F) +#define TOUCH_CFG_PHASE1_SDPA_VALUE (0x17) +#endif + +#define TOUCH_RATIO_CALC(a) ((uint16_t) (a / 100)) + +#if ((TOUCH_CFG_UART_MONITOR_SUPPORT == 1) || (TOUCH_CFG_UART_TUNING_SUPPORT == 1)) + #if (TOUCH_CFG_UART_MODULE_TYPE == 1) + #define sci_err_t rsci_err_t + #define sci_cfg_t rsci_cfg_t + #define sci_hdl_t rsci_hdl_t + #define parity_en parity_type + #define sci_cb_args_t rsci_cb_args_t + #define SCI_EVT_TEI RSCI_EVT_TEI + #define SCI_EVT_RX_CHAR RSCI_EVT_RX_CHAR + #define SCI_CLK_INT RSCI_CLK_INT + #define SCI_DATA_8BIT RSCI_DATA_8BIT + #define SCI_PARITY_OFF RSCI_NONE_PARITY + #define SCI_EVEN_PARITY RSCI_NONE_PARITY + #define SCI_STOPBITS_1 RSCI_STOPBITS_1 + #define SCI_MODE_ASYNC RSCI_MODE_ASYNC + #define R_SCI_Open R_RSCI_Open + #define R_SCI_Close R_RSCI_Close + #define R_SCI_Send R_RSCI_Send + #define R_SCI_Receive R_RSCI_Receive + #endif +#endif + +#if (TOUCH_CFG_UART_TUNING_SUPPORT == 1) + #define TOUCH_TUNING_CH_REG_MAX_NUM (32) + #define TOUCH_TUNING_KEY_MAX_NUM (CTSU_CFG_NUM_SELF_ELEMENTS + CTSU_CFG_NUM_MUTUAL_ELEMENTS * 2) + + #define TOUCH_UART_TSCHAC_MASK (0xFF) + #define TOUCH_UART_TSCHTRC_MASK (0xFF) + #if (BSP_FEATURE_CTSU_VERSION == 1) + #define TOUCH_TUNING_PCLKB_FREQ_MHZ (1000000) + #define TOUCH_TUNING_PCLKB_FREQ_32MHZ (32000000) + #define TOUCH_TUNING_PCLKB_FREQ_64MHZ (64000000) + #endif + +/* UART Receive Buffer Size */ + #define TOUCH_TUNING_TRANSMIT_BUF_SIZE (0xC) + +/* Control byte */ + #define TOUCH_TUNING_HEADER (0x55) + #define TOUCH_TUNING_FOOTER (0x0A) + +/* Command ID */ + #define TOUCH_TUNING_COMMAND_START (0x21) + #define TOUCH_TUNING_COMMAND_STOP (0x22) + #define TOUCH_TUNING_COMMAND_VARIABLE_WRITE (0x23) + #define TOUCH_TUNING_COMMAND_VARIABLE_READ (0x24) + #define TOUCH_TUNING_COMMAND_ADDRESS_WRITE (0x25) + #define TOUCH_TUNING_COMMAND_ADDRESS_READ (0x26) + #define TOUCH_TUNING_COMMAND_PHASE_RUN (0x27) + #define TOUCH_TUNING_COMMAND_VERSION (0x28) + + #define TOUCH_TUNING_COMMAND_TOP_NUM (0x2) + +/* Response ID */ + #define TOUCH_TUNING_RESPONSE_BIT (0x60) + #define TOUCH_TUNING_RESPONSE_ERROR_BIT (0xA0) + +/*variable command ID */ + #define TOUCH_TUNING_VARIABLE_MCU_GROUP (0x1) + #define TOUCH_TUNING_VARIABLE_PCLKB_FREQUENCY (0x2) + #define TOUCH_TUNING_VARIABLE_TUNING_MODE (0x3) + #define TOUCH_TUNING_VARIABLE_CTSU_STATE (0x4) + #define TOUCH_TUNING_VARIABLE_CTSU_SCAN_MODE (0x5) + #define TOUCH_TUNING_VARIABLE_TS_CHAC_MASK_BOTTOM (0x6) + #define TOUCH_TUNING_VARIABLE_TS_CHAC_MASK_TOP (0x7) + #define TOUCH_TUNING_VARIABLE_TS_CHTRC_MASK_BOTTOM (0x8) + #define TOUCH_TUNING_VARIABLE_TS_CHTRC_MASK_TOP (0x9) + #define TOUCH_TUNING_VARIABLE_QE_TXVSEL (0xA) + #define TOUCH_TUNING_VARIABLE_QE_ATUNE (0xB) + #define TOUCH_TUNING_VARIABLE_QE_POSEL (0xC) + #define TOUCH_TUNING_VARIABLE_ERR_EVENT (0xD) + #define TOUCH_TUNING_VARIABLE_CTSU_ICO_DATA_PRI (0xE) + #define TOUCH_TUNING_VARIABLE_CTSU_ICO_DATA_SND (0xF) + #define TOUCH_TUNING_VARIABLE_CTSU_MUTUAL_DATA (0x10) + #define TOUCH_TUNING_VARIABLE_ELEMENT_CFGS_SO (0x11) + #define TOUCH_TUNING_VARIABLE_ELEMENT_CFGS_SNUM (0x12) + #define TOUCH_TUNING_VARIABLE_ELEMENT_CFGS_SDPA (0x13) + #if (BSP_FEATURE_CTSU_VERSION == 1) + #define TOUCH_TUNING_VARIABLE_CTSU_WRITE_SSC (0x14) + #define TOUCH_TUNING_VARIABLE_CTSU_WRITE_SO0 (0x15) + #define TOUCH_TUNING_VARIABLE_CTSU_WRITE_SO1 (0x16) + #endif + #define TOUCH_TUNING_VARIABLE_SELF_TARGET_VALUE (0x17) + #define TOUCH_TUNING_VARIABLE_MUTUAL_TARGET_VALUE (0x18) + #if (BSP_FEATURE_CTSU_VERSION == 2) + #if (CTSU_CFG_MULTIPLE_ELECTRODE_CONNECTION_ENABLE == 1) + #define TOUCH_TUNING_VARIABLE_QE_TSOD (0x19) + #define TOUCH_TUNING_VARIABLE_QE_MEC_TS (0x1A) + #define TOUCH_TUNING_VARIABLE_QE_MEC_SHIELD_TS (0x1B) + #endif + #endif +#endif + +#if ((TOUCH_CFG_MONITOR_ENABLE && (TOUCH_CFG_UART_MONITOR_SUPPORT == 1)) || (TOUCH_CFG_UART_TUNING_SUPPORT == 1)) + +/* Register mask and bit-shift */ + #if (BSP_FEATURE_CTSU_VERSION == 2) + #define TOUCH_UART_CTSUSO_MASK (0x3FF) + #define TOUCH_UART_CTSUSNUM_MASK (0xFF) + #define TOUCH_UART_CTSUSDPA_MASK (0xFF) + #define TOUCH_UART_CTSUSNUM_SHIFT (10) + #define TOUCH_UART_CTSUSDPA_SHIFT (24) + #endif + #if (BSP_FEATURE_CTSU_VERSION == 1) + #define TOUCH_UART_CTSUSO_MASK (0x3FF) + #define TOUCH_UART_CTSUSNUM_MASK (0x3F) + #define TOUCH_UART_CTSUSDPA_MASK (0x1F) + #define TOUCH_UART_CTSUSNUM_SHIFT (10) + #define TOUCH_UART_CTSUSDPA_SHIFT (8) + #endif + + #define TOUCH_TUNING_COMMAND_BUF_NUM (0x2) + #define TOUCH_TUNING_RECIEVE_BUF_SIZE (0xF) + #define TOUCH_TUNING_ICOG_100 (0) // ICOG = 100% + #define TOUCH_TUNING_ICOG_66 (1) // ICOG = 66% + #define TOUCH_TUNING_RICOA_RECOMMEND (0xFF) // ICO input current + #if (CTSU_CFG_MCU_PROCESS_MF3 == 1) + #define TOUCH_TUNING_ICOG_RECOMMEND (TOUCH_TUNING_ICOG_66) // Recommended setting value + #endif + #if (CTSU_CFG_MCU_PROCESS_40N_PHASE2 == 1) + #define TOUCH_TUNING_ICOG_RECOMMEND (TOUCH_TUNING_ICOG_100) // Recommended setting value + #endif + +#endif + +#if (CTSU_CFG_NUM_MUTUAL_ELEMENTS != 0) + #define TOUCH_MUTUAL_SECONDARY_MAX (45000) // Mutual Secondary count MAX + #define TOUCH_MUTUAL_INT16_MAX (32767) // int16 Max Value +#endif + +/*********************************************************************************************************************** + * Typedef definitions + **********************************************************************************************************************/ + +/*********************************************************************************************************************** + * Private function prototypes + **********************************************************************************************************************/ +#if (TOUCH_CFG_NUM_BUTTONS != 0) +static fsp_err_t touch_button_process(touch_instance_ctrl_t * p_instance_ctrl, uint16_t * p_data, uint64_t * p_status); + + #if (CTSU_CFG_NUM_SELF_ELEMENTS != 0) +static void touch_button_self_decode(touch_button_info_t * p_binfo, uint16_t value, touch_mm_info_t * p_button_mm_info); +static void touch_button_drift(touch_button_info_t * p_binfo, uint16_t value, touch_mm_info_t button_mm_info); + + #endif + #if (CTSU_CFG_NUM_MUTUAL_ELEMENTS != 0) +static void touch_button_mutual_decode(touch_button_info_t * p_binfo, int16_t value, + touch_mm_info_t * p_button_mm_info); +static void touch_button_mutual_drift(touch_button_info_t * p_binfo, int16_t value, touch_mm_info_t button_mm_info); + + #endif +#endif + +#if (TOUCH_CFG_NUM_SLIDERS != 0) +static void touch_slider_decode(touch_slider_info_t * p_sinfo, + uint16_t * slider_data, + uint8_t num_elements, + uint8_t slider_id); + +#endif +#if (TOUCH_CFG_NUM_WHEELS != 0) +static void touch_wheel_decode(touch_wheel_info_t * p_winfo, + uint16_t * wheel_data, + uint8_t num_elements, + uint8_t wheel_id); + +#endif + +#if (TOUCH_CFG_PAD_ENABLE) +static void touch_pad_decode(touch_pad_info_t * p_pinfo, uint8_t num_x, uint8_t num_y, uint8_t max_touch); + +#endif + +#if ((TOUCH_CFG_MONITOR_ENABLE && (TOUCH_CFG_UART_MONITOR_SUPPORT == 1)) || (TOUCH_CFG_UART_TUNING_SUPPORT == 1)) +void touch_uart_callback(void *pargs); + +#endif + +#if (TOUCH_CFG_UART_TUNING_SUPPORT == 1) +void touch_serial_tuning(touch_instance_ctrl_t * const p_instance_ctrl); + +void touch_tuning_uart_connect(void); + +void touch_tuning_initialize(touch_instance_ctrl_t * const p_instance_ctrl); +void touch_tuning_scan_mode_select(touch_instance_ctrl_t * const p_instance_ctrl); +void touch_tuning_scan_register_setting(touch_instance_ctrl_t * const p_instance_ctrl); +void touch_tuning_ts_setup(touch_instance_ctrl_t * const p_instance_ctrl); +void touch_tuning_count_element(uint32_t element_mask, uint16_t * num_element); +void touch_tuning_qe_get_cfg(touch_instance_ctrl_t * const p_instance_ctrl); +void touch_tuning_qe_get_value(touch_instance_ctrl_t * const p_instance_ctrl); + +void touch_tuning_open(touch_instance_ctrl_t * const p_instance_ctrl); +void touch_tuning_dataget(touch_instance_ctrl_t * const p_instance_ctrl); +void touch_tuning_scanstart(touch_instance_ctrl_t * const p_instance_ctrl); + +void touch_tuning_get32(uint32_t * p_val, uint16_t index); +void touch_tuning_get16(uint16_t * p_val, uint16_t index); +void touch_tuning_get8(uint8_t * p_val, uint16_t index); + +void touch_tuning_send32(uint32_t value, uint16_t index); +void touch_tuning_send16(uint16_t value, uint16_t index); +void touch_tuning_send8(uint8_t value, uint16_t index); + + #if (BSP_FEATURE_CTSU_VERSION == 1) +void touch_tuning_pclkb_get(volatile uint32_t * pclkb_frequency); + + #endif + +/* Reconnection due to UART baud rate difference. */ +void touch_tuning_baudrate_err_reconnect(void); + +#endif + +/*********************************************************************************************************************** + * Private global variables + **********************************************************************************************************************/ +static uint16_t g_data[(CTSU_CFG_NUM_SELF_ELEMENTS + (CTSU_CFG_NUM_MUTUAL_ELEMENTS * 2)) * + CTSU_MAJORITY_MODE_ELEMENTS]; +#if (TOUCH_CFG_NUM_BUTTONS != 0) +static uint8_t g_touch_button_index = 0; +static uint16_t g_touch_button_threshold[TOUCH_CFG_NUM_BUTTONS * CTSU_MAJORITY_MODE_ELEMENTS]; +static uint16_t g_touch_button_hysteresis[TOUCH_CFG_NUM_BUTTONS * CTSU_MAJORITY_MODE_ELEMENTS]; +static uint16_t g_touch_button_reference[TOUCH_CFG_NUM_BUTTONS * CTSU_MAJORITY_MODE_ELEMENTS]; +static uint16_t g_touch_button_on_count[TOUCH_CFG_NUM_BUTTONS * CTSU_MAJORITY_MODE_ELEMENTS]; +static uint16_t g_touch_button_off_count[TOUCH_CFG_NUM_BUTTONS * CTSU_MAJORITY_MODE_ELEMENTS]; +static uint32_t g_touch_button_drift_buf[TOUCH_CFG_NUM_BUTTONS * CTSU_MAJORITY_MODE_ELEMENTS]; +static uint16_t g_touch_button_drift_count[TOUCH_CFG_NUM_BUTTONS * CTSU_MAJORITY_MODE_ELEMENTS]; +static touch_mm_info_t g_button_mm_info[TOUCH_CFG_NUM_BUTTONS]; +#endif +#if (TOUCH_CFG_NUM_SLIDERS != 0) +static uint8_t g_touch_slider_index = 0; +static uint16_t g_touch_slider_position[TOUCH_CFG_NUM_SLIDERS]; +static uint16_t g_touch_slider_threshold[TOUCH_CFG_NUM_SLIDERS]; +#endif +#if (TOUCH_CFG_NUM_WHEELS != 0) +static uint8_t g_touch_wheel_index = 0; +static uint16_t g_touch_wheel_position[TOUCH_CFG_NUM_WHEELS]; +static uint16_t g_touch_wheel_threshold[TOUCH_CFG_NUM_WHEELS]; +#endif +#if (TOUCH_CFG_PAD_ENABLE) +static uint16_t g_touch_pad_rx_coordinate[TOUCH_PAD_MONITOR_TOUCH_NUM_MAX]; +static uint16_t g_touch_pad_tx_coordinate[TOUCH_PAD_MONITOR_TOUCH_NUM_MAX]; +static uint16_t g_touch_pad_num_touch; +static uint16_t g_touch_pad_threshold; +static uint16_t g_touch_pad_rx_pixsel; +static uint16_t g_touch_pad_tx_pixsel; +static uint8_t g_touch_pad_max_touch; +static uint16_t g_touch_pad_drift_count; +static int32_t g_touch_pad_drift_buf[CTSU_CFG_NUM_CFC * CTSU_CFG_NUM_CFC_TX]; +static uint16_t g_touch_pad_base[CTSU_CFG_NUM_CFC * CTSU_CFG_NUM_CFC_TX]; +static int16_t g_touch_pad_buf[CTSU_CFG_NUM_CFC * CTSU_CFG_NUM_CFC_TX * 2]; +static uint8_t g_touch_base_set_falg = 0; +#endif +#if TOUCH_CFG_MONITOR_ENABLE + #if ((TOUCH_CFG_UART_MONITOR_SUPPORT == 1) || (TOUCH_CFG_UART_TUNING_SUPPORT == 1)) +static uint8_t g_touch_monitor_buf[TOUCH_MONITOR_BUFFER_SIZE]; + #else +static volatile uint8_t g_touch_monitor_buf[TOUCH_MONITOR_BUFFER_SIZE]; + #endif +static uint8_t g_touch_monitor_id; +static uint16_t g_touch_monitor_size[TOUCH_MONITOR_BLOCK_MAX]; + #if (TOUCH_CFG_UART_MONITOR_SUPPORT == 1) +static touch_instance_ctrl_t * gp_touch_ctrl_list[TOUCH_UART_INSTANCE_MAX]; + +/* Detect method number */ +static uint32_t g_touch_uart_open_bitmap; + +/* Monitoring method number */ +static uint8_t g_touch_uart_monitor_num; + #endif +#endif + +#if (TOUCH_CFG_UART_TUNING_SUPPORT == 1) + +/* QE send Data */ +static volatile uint32_t g_touch_tuning_pclkb_frequency; +static volatile uint8_t g_touch_tuning_mcu_group; +static volatile touch_tuning_state_t g_touch_tuning_state; +static volatile touch_tuning_mode_t g_touch_tuning_mode; +static volatile touch_tuning_scan_t g_touch_tuning_scan_mode; +static touch_tuning_ts_msk_t g_touch_tuning_ts_chac_mask; +static touch_tuning_ts_msk_t g_touch_tuning_ts_chtrc_mask; +static volatile ctsu_event_t g_touch_tuning_err_event; + #if (BSP_FEATURE_CTSU_VERSION == 2) +static volatile ctsu_atune12_t g_touch_tuning_qe_atune; + #endif + #if (BSP_FEATURE_CTSU_VERSION == 1) +static volatile ctsu_atune1_t g_touch_tuning_qe_atune; + #endif +static volatile ctsu_posel_t g_touch_tuning_qe_posel; +static volatile uint8_t g_touch_tuning_qe_txvsel; +static volatile uint8_t g_touch_tuning_phase_run; +static uint16_t g_touch_tuning_self_target_value; +static uint16_t g_touch_tuning_mutual_target_value; + #if (BSP_FEATURE_CTSU_VERSION == 2) + #if (CTSU_CFG_MULTIPLE_ELECTRODE_CONNECTION_ENABLE == 1) +static volatile uint8_t g_touch_tuning_qe_tsod; +static volatile uint8_t g_touch_tuning_qe_mec_ts; +static volatile uint8_t g_touch_tuning_qe_mec_shield_ts; + #endif + #endif + +static volatile touch_tuning_mode_t g_touch_tuning_mode_save; + +/* interrupt datas (QE get this data) */ +static uint16_t g_touch_tuning_ico_data[TOUCH_TUNING_KEY_MAX_NUM]; + #if (CTSU_CFG_NUM_MUTUAL_ELEMENTS != 0) +static uint16_t g_touch_tuning_mutual_data[CTSU_CFG_NUM_MUTUAL_ELEMENTS]; + #endif + #if (BSP_FEATURE_CTSU_VERSION == 2) +static ctsu_element_cfg_t g_touch_tuning_element_cfgs[TOUCH_TUNING_KEY_MAX_NUM]; + #endif + #if (BSP_FEATURE_CTSU_VERSION == 1) +static ctsu_ctsuwr_t g_touch_tuning_ctsu_write[TOUCH_TUNING_KEY_MAX_NUM]; + #endif +static ctsu_md_t g_touch_tuning_md; + +static uint8_t g_touch_tuning_tx_buf[TOUCH_TUNING_TRANSMIT_BUF_SIZE]; +#endif + +#if ((TOUCH_CFG_MONITOR_ENABLE && (TOUCH_CFG_UART_MONITOR_SUPPORT == 1)) || (TOUCH_CFG_UART_TUNING_SUPPORT == 1)) + +/* data transmit flag */ +volatile uint8_t g_touch_uart_transmit_flag; +volatile uint8_t g_touch_uart_command_top; + +static uint8_t g_touch_uart_rx_buf[TOUCH_TUNING_RECIEVE_BUF_SIZE]; + + /* UART receive interrupt count number */ + volatile uint8_t g_receive_intterupt_count = 0; + static sci_cfg_t g_touch_uart_config; + static sci_hdl_t g_touch_uart_control; + + /* Reconnection due to UART baud rate difference. */ + static uint8_t g_touch_uart_baudrate_err_flag = 0; +#endif + +/*********************************************************************************************************************** + * Global variables + **********************************************************************************************************************/ +const touch_api_t g_touch_on_ctsu = +{ + .open = RM_TOUCH_Open, + .scanStart = RM_TOUCH_ScanStart, + .dataGet = RM_TOUCH_DataGet, + .padDataGet = RM_TOUCH_PadDataGet, + .callbackSet = RM_TOUCH_CallbackSet, + .scanStop = RM_TOUCH_ScanStop, + .close = RM_TOUCH_Close, + .sensitivityRatioGet = RM_TOUCH_SensitivityRatioGet, + .thresholdAdjust = RM_TOUCH_ThresholdAdjust, + .driftControl = RM_TOUCH_DriftControl, + .monitorAddressGet = RM_TOUCH_MonitorAddressGet, +}; + +touch_instance_ctrl_t * gp_touch_isr_context; + +/*******************************************************************************************************************//** + * @addtogroup TOUCH + * @{ + **********************************************************************************************************************/ + +/*********************************************************************************************************************** + * Functions + **********************************************************************************************************************/ + +/*******************************************************************************************************************//** + * @brief Opens and configures the TOUCH Middle module. Implements @ref touch_api_t::open. + * + * Example: + * @snippet rm_touch_example.c RM_TOUCH_Open + * + * @retval FSP_SUCCESS TOUCH successfully configured. + * @retval FSP_ERR_ASSERTION Null pointer, or one or more configuration options is invalid. + * @retval FSP_ERR_ALREADY_OPEN Module is already open. This module can only be opened once. + * @retval FSP_ERR_INVALID_ARGUMENT Configuration parameter error. + **********************************************************************************************************************/ +fsp_err_t RM_TOUCH_Open (touch_ctrl_t * const p_ctrl, touch_cfg_t const * const p_cfg) +{ + touch_instance_ctrl_t * p_instance_ctrl = (touch_instance_ctrl_t *) p_ctrl; +#if ((TOUCH_CFG_NUM_BUTTONS != 0) || (TOUCH_CFG_NUM_SLIDERS != 0) || (TOUCH_CFG_NUM_WHEELS != 0) || \ + (TOUCH_CFG_PAD_ENABLE)) + uint8_t id; + uint8_t majority_mode_id; + uint8_t freq_num; +#endif + fsp_err_t err = FSP_SUCCESS; +#if TOUCH_CFG_MONITOR_ENABLE + uint8_t num; +#endif + +#if (TOUCH_CFG_PARAM_CHECKING_ENABLE == 1) + FSP_ASSERT(p_instance_ctrl); + FSP_ASSERT(p_cfg); +#endif + TOUCH_ERROR_RETURN(TOUCH_OPEN != p_instance_ctrl->open, FSP_ERR_ALREADY_OPEN); + + p_instance_ctrl->p_touch_cfg = p_cfg; + + /** Parameter Setting */ +#if (TOUCH_CFG_NUM_BUTTONS != 0) + if (NULL != p_cfg->p_buttons) + { + if (TOUCH_CFG_NUM_BUTTONS >= g_touch_button_index) + { + p_instance_ctrl->binfo.p_reference = + &g_touch_button_reference[g_touch_button_index * CTSU_MAJORITY_MODE_ELEMENTS]; + p_instance_ctrl->binfo.p_on_count = + &g_touch_button_on_count[g_touch_button_index * CTSU_MAJORITY_MODE_ELEMENTS]; + p_instance_ctrl->binfo.p_off_count = + &g_touch_button_off_count[g_touch_button_index * CTSU_MAJORITY_MODE_ELEMENTS]; + p_instance_ctrl->binfo.p_drift_buf = + &g_touch_button_drift_buf[g_touch_button_index * CTSU_MAJORITY_MODE_ELEMENTS]; + p_instance_ctrl->binfo.p_drift_count = + &g_touch_button_drift_count[g_touch_button_index * CTSU_MAJORITY_MODE_ELEMENTS]; + p_instance_ctrl->binfo.p_threshold = + &g_touch_button_threshold[g_touch_button_index * CTSU_MAJORITY_MODE_ELEMENTS]; + p_instance_ctrl->binfo.p_hysteresis = + &g_touch_button_hysteresis[g_touch_button_index * CTSU_MAJORITY_MODE_ELEMENTS]; + p_instance_ctrl->p_touch_mm_info = &g_button_mm_info[g_touch_button_index]; + g_touch_button_index = + (uint8_t) (g_touch_button_index + p_instance_ctrl->p_touch_cfg->num_buttons); + } + + if (0 != p_cfg->cancel_freq) + { + if (p_cfg->cancel_freq <= p_cfg->on_freq) + { + err = FSP_ERR_INVALID_ARGUMENT; + } + } + + p_instance_ctrl->binfo.on_freq = p_cfg->on_freq; + p_instance_ctrl->binfo.off_freq = p_cfg->off_freq; + p_instance_ctrl->binfo.drift_freq = p_cfg->drift_freq; + p_instance_ctrl->binfo.cancel_freq = p_cfg->cancel_freq; + + #if (CTSU_CFG_MAJORITY_MODE & CTSU_JUDGEMENT_MAJORITY_MODE) + if (p_instance_ctrl->p_touch_cfg->p_ctsu_instance->p_cfg->majority_mode == 1) + { + freq_num = CTSU_MAJORITY_MODE_ELEMENTS; + } + else + #endif + { + freq_num = 1; + } + + for (id = 0; id < p_cfg->num_buttons; id++) + { + for (majority_mode_id = 0; majority_mode_id < freq_num; majority_mode_id++) + { + if (p_cfg->p_buttons[id * freq_num + majority_mode_id].threshold < + p_cfg->p_buttons[id * freq_num + majority_mode_id].hysteresis) + { + err = FSP_ERR_INVALID_ARGUMENT; + } + + *(p_instance_ctrl->binfo.p_reference + id * freq_num + majority_mode_id) = 0; + *(p_instance_ctrl->binfo.p_on_count + id * freq_num + majority_mode_id) = 0; + *(p_instance_ctrl->binfo.p_off_count + id * freq_num + majority_mode_id) = 0; + *(p_instance_ctrl->binfo.p_drift_buf + id * freq_num + majority_mode_id) = 0; + *(p_instance_ctrl->binfo.p_drift_count + id * freq_num + majority_mode_id) = 0; + *(p_instance_ctrl->binfo.p_threshold + id * freq_num + + majority_mode_id) = p_cfg->p_buttons[id * freq_num + majority_mode_id].threshold; + *(p_instance_ctrl->binfo.p_hysteresis + id * freq_num + + majority_mode_id) = p_cfg->p_buttons[id * freq_num + majority_mode_id].hysteresis; + *((p_instance_ctrl->p_touch_mm_info + id)->mm_result + majority_mode_id) = 0; + } + } + } +#endif +#if (TOUCH_CFG_NUM_SLIDERS != 0) + if (NULL != p_cfg->p_sliders) + { + if (TOUCH_CFG_NUM_SLIDERS >= g_touch_slider_index) + { + p_instance_ctrl->sinfo.p_position = &g_touch_slider_position[g_touch_slider_index]; + p_instance_ctrl->sinfo.p_threshold = &g_touch_slider_threshold[g_touch_slider_index]; + g_touch_slider_index = (uint8_t) (g_touch_slider_index + p_cfg->num_sliders); + } + + for (id = 0; id < p_cfg->num_sliders; id++) + { + if ((TOUCH_SLIDER_ELEMENTS_MIN > p_cfg->p_sliders[id].num_elements) || + (TOUCH_SLIDER_ELEMENTS_MAX < p_cfg->p_sliders[id].num_elements)) + { + err = FSP_ERR_INVALID_ARGUMENT; + } + + *(p_instance_ctrl->sinfo.p_threshold + id) = p_cfg->p_sliders[id].threshold; + } + } +#endif +#if (TOUCH_CFG_NUM_WHEELS != 0) + if (NULL != p_cfg->p_wheels) + { + if (TOUCH_CFG_NUM_WHEELS >= g_touch_wheel_index) + { + p_instance_ctrl->winfo.p_position = &g_touch_wheel_position[g_touch_wheel_index]; + p_instance_ctrl->winfo.p_threshold = &g_touch_wheel_threshold[g_touch_wheel_index]; + g_touch_wheel_index = (uint8_t) (g_touch_wheel_index + p_cfg->num_wheels); + } + + for (id = 0; id < p_cfg->num_wheels; id++) + { + if ((TOUCH_WHEEL_ELEMENTS_LARGE != p_cfg->p_wheels[id].num_elements) && + (TOUCH_WHEEL_ELEMENTS_SMALL != p_cfg->p_wheels[id].num_elements)) + { + err = FSP_ERR_INVALID_ARGUMENT; + } + + *(p_instance_ctrl->winfo.p_threshold + id) = p_cfg->p_wheels[id].threshold; + } + } +#endif +#if (TOUCH_CFG_PAD_ENABLE) + if (NULL != p_cfg->p_pad) + { + p_instance_ctrl->pinfo.p_rx_coordinate = &g_touch_pad_rx_coordinate[0]; + p_instance_ctrl->pinfo.p_tx_coordinate = &g_touch_pad_tx_coordinate[0]; + p_instance_ctrl->pinfo.p_num_touch = &g_touch_pad_num_touch; + p_instance_ctrl->pinfo.p_threshold = &g_touch_pad_threshold; + p_instance_ctrl->pinfo.p_rx_pixel = &g_touch_pad_rx_pixsel; + p_instance_ctrl->pinfo.p_tx_pixel = &g_touch_pad_tx_pixsel; + p_instance_ctrl->pinfo.p_max_touch = &g_touch_pad_max_touch; + p_instance_ctrl->pinfo.p_drift_buf = &g_touch_pad_drift_buf[0]; + p_instance_ctrl->pinfo.p_drift_count = &g_touch_pad_drift_count; + p_instance_ctrl->pinfo.p_base_buf = &g_touch_pad_base[0]; + + *(p_instance_ctrl->pinfo.p_threshold) = p_cfg->p_pad->threshold; + *(p_instance_ctrl->pinfo.p_rx_pixel) = p_cfg->p_pad->rx_pixel; + *(p_instance_ctrl->pinfo.p_tx_pixel) = p_cfg->p_pad->tx_pixel; + *(p_instance_ctrl->pinfo.p_max_touch) = p_cfg->p_pad->max_touch; + p_instance_ctrl->pinfo.num_drift = p_cfg->p_pad->num_drift; + + for (id = 0; id < (CTSU_CFG_NUM_CFC * CTSU_CFG_NUM_CFC_TX); id++) + { + *(p_instance_ctrl->pinfo.p_drift_buf + id) = 0; + } + + *(p_instance_ctrl->pinfo.p_drift_count) = 0; + } +#endif + + if (FSP_SUCCESS == err) + { + p_instance_ctrl->p_ctsu_instance = p_cfg->p_ctsu_instance; + err = p_instance_ctrl->p_ctsu_instance->p_api->open(p_cfg->p_ctsu_instance->p_ctrl, + p_cfg->p_ctsu_instance->p_cfg); + } + +#if TOUCH_CFG_MONITOR_ENABLE + num = p_cfg->number; + #if (CTSU_CFG_MAJORITY_MODE & CTSU_JUDGEMENT_MAJORITY_MODE) + if (p_instance_ctrl->p_touch_cfg->p_ctsu_instance->p_cfg->majority_mode == 1) + { + g_touch_monitor_size[num] = + (uint16_t) (TOUCH_MONITOR_SELF_ELEMENT_SIZE * p_cfg->p_ctsu_instance->p_cfg->num_rx * + CTSU_MAJORITY_MODE_ELEMENTS); + #if (CTSU_CFG_NUM_MUTUAL_ELEMENTS != 0) + if (CTSU_MODE_MUTUAL_FULL_SCAN == (CTSU_MODE_MUTUAL_FULL_SCAN & p_instance_ctrl->p_ctsu_instance->p_cfg->md)) + { + g_touch_monitor_size[num] = + (uint16_t) (TOUCH_MONITOR_MUTUAL_ELEMENT_SIZE * + p_cfg->p_ctsu_instance->p_cfg->num_rx * p_cfg->p_ctsu_instance->p_cfg->num_tx * + CTSU_MAJORITY_MODE_ELEMENTS); + } + #endif + #if (TOUCH_CFG_NUM_BUTTONS != 0) + if (NULL != p_cfg->p_buttons) + { + g_touch_monitor_size[num] = + (uint16_t) (g_touch_monitor_size[num] + TOUCH_MONITOR_BHEAD_SIZE + + ((TOUCH_MONITOR_BUTTON_SIZE + + (TOUCH_MONITOR_BUTTON_OPTION_SIZE * CTSU_MAJORITY_MODE_ELEMENTS)) * p_cfg->num_buttons)); + } + #endif + } + else + #endif + { + g_touch_monitor_size[num] = + (uint16_t) (TOUCH_MONITOR_SELF_ELEMENT_SIZE * p_cfg->p_ctsu_instance->p_cfg->num_rx); + + #if (CTSU_CFG_NUM_MUTUAL_ELEMENTS != 0) + if (CTSU_MODE_MUTUAL_FULL_SCAN == (CTSU_MODE_MUTUAL_FULL_SCAN & p_instance_ctrl->p_ctsu_instance->p_cfg->md)) + { + g_touch_monitor_size[num] = + (uint16_t) (TOUCH_MONITOR_MUTUAL_ELEMENT_SIZE * + p_cfg->p_ctsu_instance->p_cfg->num_rx * p_cfg->p_ctsu_instance->p_cfg->num_tx); + } + #endif + #if (TOUCH_CFG_NUM_BUTTONS != 0) + if (NULL != p_cfg->p_buttons) + { + g_touch_monitor_size[num] = + (uint16_t) (g_touch_monitor_size[num] + TOUCH_MONITOR_BHEAD_SIZE + + (TOUCH_MONITOR_BUTTON_SIZE * p_cfg->num_buttons)); + #if (CTSU_CFG_MAJORITY_MODE & CTSU_JUDGEMENT_MAJORITY_MODE) + g_touch_monitor_size[num] = + (uint16_t) (g_touch_monitor_size[num] + + (TOUCH_MONITOR_BUTTON_OPTION_SIZE * p_cfg->num_buttons)); + #endif + } + #endif + } + + #if (TOUCH_CFG_NUM_SLIDERS != 0) + if (NULL != p_cfg->p_sliders) + { + g_touch_monitor_size[num] = + (uint16_t) (g_touch_monitor_size[num] + TOUCH_MONITOR_SHEAD_SIZE + + (TOUCH_MONITOR_SLIDER_SIZE * p_cfg->num_sliders)); + } + #endif + #if (TOUCH_CFG_NUM_WHEELS != 0) + if (NULL != p_cfg->p_wheels) + { + g_touch_monitor_size[num] = + (uint16_t) (g_touch_monitor_size[num] + TOUCH_MONITOR_WHEAD_SIZE + + (TOUCH_MONITOR_WHEEL_SIZE * p_cfg->num_wheels)); + } + #endif + #if (TOUCH_CFG_PAD_ENABLE) + if (NULL != p_cfg->p_pad) + { + g_touch_monitor_size[num] = (uint16_t) (g_touch_monitor_size[num] / 2); + g_touch_monitor_size[num] = + (uint16_t) (g_touch_monitor_size[num] + TOUCH_MONITOR_PAD_TOTAL_SIZE); + } + #endif + + g_touch_monitor_size[num] = + (uint16_t) (g_touch_monitor_size[num] + TOUCH_MONITOR_HEADER_SIZE + TOUCH_MONITOR_FOOTER_SIZE); + + #if (TOUCH_CFG_UART_MONITOR_SUPPORT == 1) + if (0 == g_touch_uart_open_bitmap) + { + g_touch_uart_config.async.baud_rate = TOUCH_CFG_UART_BAUDRATE; + g_touch_uart_config.async.clk_src = SCI_CLK_INT; + g_touch_uart_config.async.data_size = SCI_DATA_8BIT; + g_touch_uart_config.async.parity_en = SCI_PARITY_OFF; + g_touch_uart_config.async.parity_type = SCI_EVEN_PARITY; + g_touch_uart_config.async.stop_bits = SCI_STOPBITS_1; + g_touch_uart_config.async.int_priority = TOUCH_CFG_UART_PRIORITY; + + R_SCI_Open(TOUCH_CFG_UART_NUMBER, SCI_MODE_ASYNC, &g_touch_uart_config, touch_uart_callback, &g_touch_uart_control); + + R_SCI_Receive(g_touch_uart_control, &g_touch_uart_rx_buf[0] ,TOUCH_UART_RECIEVE_BUF_SIZE); + + g_touch_uart_monitor_num = TOUCH_UART_MONITOR_NONE; + } + + g_touch_uart_open_bitmap |= ((uint32_t) 1 << num); + gp_touch_ctrl_list[num] = p_instance_ctrl; + #endif +#endif + +#if (TOUCH_CFG_UART_TUNING_SUPPORT == 1) + touch_tuning_uart_connect(); +#endif + + gp_touch_isr_context = p_instance_ctrl; + + /* Mark driver as open */ + p_instance_ctrl->open = TOUCH_OPEN; + + return err; +} + +/*******************************************************************************************************************//** + * @brief 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 RM_TOUCH_DataGet(). + * If a different control block scan should be run, check the scan is complete before executing. + * Implements @ref touch_api_t::scanStart. + * + * @retval FSP_SUCCESS Successfully started. + * @retval FSP_ERR_ASSERTION Null pointer passed as a parameter. + * @retval FSP_ERR_NOT_OPEN Module is not open. + * @retval FSP_ERR_CTSU_SCANNING Scanning this instance or other. + * @retval FSP_ERR_CTSU_NOT_GET_DATA The previous data has not been retrieved by DataGet. + **********************************************************************************************************************/ +fsp_err_t RM_TOUCH_ScanStart (touch_ctrl_t * const p_ctrl) +{ + fsp_err_t err = FSP_SUCCESS; + touch_instance_ctrl_t * p_instance_ctrl = (touch_instance_ctrl_t *) p_ctrl; + gp_touch_isr_context = p_instance_ctrl; + +#if (TOUCH_CFG_PARAM_CHECKING_ENABLE == 1) + FSP_ASSERT(p_instance_ctrl); + TOUCH_ERROR_RETURN(TOUCH_OPEN == p_instance_ctrl->open, FSP_ERR_NOT_OPEN); +#endif + + err = p_instance_ctrl->p_ctsu_instance->p_api->scanStart(p_instance_ctrl->p_ctsu_instance->p_ctrl); + FSP_ERROR_RETURN(FSP_ERR_CTSU_SCANNING != err, FSP_ERR_CTSU_SCANNING); + FSP_ERROR_RETURN(FSP_ERR_CTSU_NOT_GET_DATA != err, FSP_ERR_CTSU_NOT_GET_DATA); + + return err; +} + +/*******************************************************************************************************************//** + * @brief Gets the 64-bit mask indicating which buttons are pressed. + * Also, this function gets the current position of where slider or wheel is being pressed. + * If initial offset tuning is enabled, The first several calls are used to tuning for the sensors. + * Implements @ref touch_api_t::dataGet. + * + * @retval FSP_SUCCESS Successfully data decoded. + * @retval FSP_ERR_ASSERTION Null pointer passed as a parameter. + * @retval FSP_ERR_NOT_OPEN Module is not open. + * @retval FSP_ERR_CTSU_SCANNING Scanning this instance. + * @retval FSP_ERR_CTSU_INCOMPLETE_TUNING Incomplete initial offset tuning. + * @retval FSP_ERR_INVALID_HW_CONDITION Values scanned by CTSU show abnormal values + **********************************************************************************************************************/ +fsp_err_t RM_TOUCH_DataGet (touch_ctrl_t * const p_ctrl, + uint64_t * p_button_status, + uint16_t * p_slider_position, + uint16_t * p_wheel_position) +{ + fsp_err_t err = FSP_SUCCESS; + touch_instance_ctrl_t * p_instance_ctrl = (touch_instance_ctrl_t *) p_ctrl; + +#if ((TOUCH_CFG_NUM_SLIDERS != 0) || (TOUCH_CFG_NUM_WHEELS != 0)) + uint16_t sensor_val = 0; +#endif +#if (TOUCH_CFG_NUM_SLIDERS != 0) + const touch_slider_cfg_t * p_slider; + uint8_t slider_id; + uint16_t slider_data[TOUCH_SLIDER_ELEMENTS_MAX]; +#endif +#if (TOUCH_CFG_NUM_WHEELS != 0) + const touch_wheel_cfg_t * p_wheel; + uint8_t wheel_id; + uint16_t wheel_data[TOUCH_WHEEL_ELEMENTS_LARGE]; +#endif +#if ((TOUCH_CFG_NUM_SLIDERS != 0) || (TOUCH_CFG_NUM_WHEELS != 0)) + uint8_t element_id; +#endif +#if TOUCH_CFG_MONITOR_ENABLE + uint16_t i; + uint16_t index = 0; + uint8_t element_num = 0; + #if (CTSU_CFG_MAJORITY_MODE & CTSU_JUDGEMENT_MAJORITY_MODE) + uint8_t j; + uint8_t button_all_status = 0; + #endif +#endif + +#if (TOUCH_CFG_PARAM_CHECKING_ENABLE == 1) + FSP_ASSERT(p_instance_ctrl); + #if (TOUCH_CFG_NUM_BUTTONS != 0) + if (0 != p_instance_ctrl->p_touch_cfg->num_buttons) + { + FSP_ASSERT(p_button_status); + } + #endif + #if (TOUCH_CFG_NUM_SLIDERS != 0) + if (0 != p_instance_ctrl->p_touch_cfg->num_sliders) + { + FSP_ASSERT(p_slider_position); + } + #endif + #if (TOUCH_CFG_NUM_WHEELS != 0) + if (0 != p_instance_ctrl->p_touch_cfg->num_wheels) + { + FSP_ASSERT(p_wheel_position); + } + #endif + TOUCH_ERROR_RETURN(TOUCH_OPEN == p_instance_ctrl->open, FSP_ERR_NOT_OPEN); +#endif + +#if (TOUCH_CFG_UART_TUNING_SUPPORT == 1) + if (true == p_instance_ctrl->serial_tuning_enable) + { + touch_serial_tuning(p_instance_ctrl); + } + + /* Reconnection due to UART baud rate difference. */ + touch_tuning_baudrate_err_reconnect (); +#endif + + /* get results from previous scan */ + err = p_instance_ctrl->p_ctsu_instance->p_api->dataGet(p_instance_ctrl->p_ctsu_instance->p_ctrl, g_data); + FSP_ERROR_RETURN(FSP_ERR_CTSU_SCANNING != err, FSP_ERR_CTSU_SCANNING); + FSP_ERROR_RETURN(FSP_ERR_CTSU_INCOMPLETE_TUNING != err, FSP_ERR_CTSU_INCOMPLETE_TUNING); + +#if (TOUCH_CFG_NUM_BUTTONS != 0) + + /* Create button status */ + err = touch_button_process(p_instance_ctrl, &g_data[0], p_button_status); + FSP_ERROR_RETURN(FSP_ERR_INVALID_HW_CONDITION != err, FSP_ERR_INVALID_HW_CONDITION); +#else + FSP_PARAMETER_NOT_USED(p_button_status); +#endif + +#if (TOUCH_CFG_NUM_SLIDERS != 0) + + /* Create slider position */ + for (slider_id = 0; slider_id < p_instance_ctrl->p_touch_cfg->num_sliders; slider_id++) + { + p_slider = (p_instance_ctrl->p_touch_cfg->p_sliders + slider_id); + for (element_id = 0; element_id < p_slider->num_elements; element_id++) + { + sensor_val = *(g_data + p_slider->p_elem_index[element_id]); + slider_data[element_id] = sensor_val; + } + + touch_slider_decode(&p_instance_ctrl->sinfo, slider_data, p_slider->num_elements, slider_id); + + if (p_slider_position != NULL) + { + *p_slider_position = *(p_instance_ctrl->sinfo.p_position + slider_id); + p_slider_position++; + } + } + +#else + FSP_PARAMETER_NOT_USED(p_slider_position); +#endif + +#if (TOUCH_CFG_NUM_WHEELS != 0) + for (wheel_id = 0; wheel_id < p_instance_ctrl->p_touch_cfg->num_wheels; wheel_id++) + { + p_wheel = (p_instance_ctrl->p_touch_cfg->p_wheels + wheel_id); + for (element_id = 0; element_id < p_wheel->num_elements; element_id++) + { + sensor_val = *(g_data + p_wheel->p_elem_index[element_id]); + wheel_data[element_id] = sensor_val; + } + + touch_wheel_decode(&p_instance_ctrl->winfo, wheel_data, p_wheel->num_elements, wheel_id); + + if (p_wheel_position != NULL) + { + *p_wheel_position = *(p_instance_ctrl->winfo.p_position + wheel_id); + p_wheel_position++; + } + } + +#else + FSP_PARAMETER_NOT_USED(p_wheel_position); +#endif + +#if TOUCH_CFG_MONITOR_ENABLE + #if (TOUCH_CFG_UART_MONITOR_SUPPORT == 1) + if (!g_touch_uart_transmit_flag && (p_instance_ctrl->p_touch_cfg->number == g_touch_uart_monitor_num)) + { + /* Monitor Data Notification */ + g_touch_monitor_buf[index++] = TOUCH_UART_HEADER; + g_touch_monitor_buf[index++] = TOUCH_UART_RESPONSE_MONITOR; + g_touch_monitor_buf[index++] = 0; /* Temporarily input the size */ + g_touch_monitor_buf[index++] = 0; /* Temporarily input the size */ + } + + if (!g_touch_uart_transmit_flag && + ((p_instance_ctrl->p_touch_cfg->number == g_touch_uart_monitor_num) || + (TOUCH_UART_MONITOR_NONE == g_touch_uart_monitor_num))) + { + if (TOUCH_UART_MONITOR_NONE == g_touch_uart_monitor_num) + { + for (i = 0; i < p_instance_ctrl->p_touch_cfg->number; i++) + { + index = (uint16_t) (index + g_touch_monitor_size[i]); + } + } + + #else + for (i = 0; i < p_instance_ctrl->p_touch_cfg->number; i++) + { + index = (uint16_t) (index + g_touch_monitor_size[i]); + } + #endif + + g_touch_monitor_buf[index++] = g_touch_monitor_id; + g_touch_monitor_buf[index++] = p_instance_ctrl->p_touch_cfg->number; + #if (CTSU_CFG_MAJORITY_MODE & CTSU_JUDGEMENT_MAJORITY_MODE) + if (p_instance_ctrl->p_touch_cfg->p_ctsu_instance->p_cfg->majority_mode == 1) + { + if ((CTSU_MODE_SELF_MULTI_SCAN == p_instance_ctrl->p_ctsu_instance->p_cfg->md) || + (CTSU_MODE_CURRENT_SCAN == p_instance_ctrl->p_ctsu_instance->p_cfg->md)) + { + g_touch_monitor_buf[index++] = 0x03; + element_num = p_instance_ctrl->p_touch_cfg->p_ctsu_instance->p_cfg->num_rx; + g_touch_monitor_buf[index++] = element_num; + for (i = 0; i < element_num; i++) + { + for (j = 0; j < CTSU_MAJORITY_MODE_ELEMENTS; j++) + { + g_touch_monitor_buf[index++] = (uint8_t) (g_data[(i * CTSU_MAJORITY_MODE_ELEMENTS) + j]); + g_touch_monitor_buf[index++] = (uint8_t) (g_data[(i * CTSU_MAJORITY_MODE_ELEMENTS) + j] >> 8); + } + } + } + else + { + g_touch_monitor_buf[index++] = 0x04; + element_num = (uint8_t) (p_instance_ctrl->p_touch_cfg->p_ctsu_instance->p_cfg->num_rx * + p_instance_ctrl->p_touch_cfg->p_ctsu_instance->p_cfg->num_tx); + g_touch_monitor_buf[index++] = element_num; + for (i = 0; i < (element_num * 2); i++) + { + for (j = 0; j < CTSU_MAJORITY_MODE_ELEMENTS; j++) + { + g_touch_monitor_buf[index++] = (uint8_t) (g_data[(i * CTSU_MAJORITY_MODE_ELEMENTS) + j]); + g_touch_monitor_buf[index++] = (uint8_t) (g_data[(i * CTSU_MAJORITY_MODE_ELEMENTS) + j] >> 8); + } + } + } + + #if (TOUCH_CFG_NUM_BUTTONS != 0) + if (0 < p_instance_ctrl->p_touch_cfg->num_buttons) + { + g_touch_monitor_buf[index++] = 0x00; + g_touch_monitor_buf[index++] = p_instance_ctrl->p_touch_cfg->num_buttons; + for (i = 0; i < p_instance_ctrl->p_touch_cfg->num_buttons; i++) + { + for (j = 0; j < CTSU_MAJORITY_MODE_ELEMENTS; j++) + { + if (1 == p_instance_ctrl->p_touch_mm_info[i].mm_result[j]) + { + button_all_status |= (uint8_t) (1 << j); + } + else + { + button_all_status &= (uint8_t) ~(1 << j); + } + } + + if (1 == ((p_instance_ctrl->binfo.status >> i) & 0x01)) + { + button_all_status |= ((uint8_t) 1 << CTSU_MAJORITY_MODE_ELEMENTS); + } + else + { + button_all_status &= (uint8_t) ~(1 << CTSU_MAJORITY_MODE_ELEMENTS); + } + + g_touch_monitor_buf[index++] = button_all_status; + for (j = 0; j < CTSU_MAJORITY_MODE_ELEMENTS; j++) + { + g_touch_monitor_buf[index++] = + (uint8_t) (p_instance_ctrl->binfo.p_reference[(i * CTSU_MAJORITY_MODE_ELEMENTS) + j]); + g_touch_monitor_buf[index++] = + (uint8_t) (p_instance_ctrl->binfo.p_reference[(i * CTSU_MAJORITY_MODE_ELEMENTS) + j] >> 8); + } + + for (j = 0; j < CTSU_MAJORITY_MODE_ELEMENTS; j++) + { + g_touch_monitor_buf[index++] = + (uint8_t) (p_instance_ctrl->binfo.p_threshold[(i * CTSU_MAJORITY_MODE_ELEMENTS) + j]); + g_touch_monitor_buf[index++] = + (uint8_t) (p_instance_ctrl->binfo.p_threshold[(i * CTSU_MAJORITY_MODE_ELEMENTS) + j] >> 8); + } + + for (j = 0; j < CTSU_MAJORITY_MODE_ELEMENTS; j++) + { + g_touch_monitor_buf[index++] = + (uint8_t) (p_instance_ctrl->binfo.p_hysteresis[(i * CTSU_MAJORITY_MODE_ELEMENTS) + j]); + g_touch_monitor_buf[index++] = + (uint8_t) (p_instance_ctrl->binfo.p_hysteresis[(i * CTSU_MAJORITY_MODE_ELEMENTS) + j] >> 8); + } + } + } + #endif + } + else + #endif + { + if ((CTSU_MODE_SELF_MULTI_SCAN == p_instance_ctrl->p_ctsu_instance->p_cfg->md) || + (CTSU_MODE_CURRENT_SCAN == p_instance_ctrl->p_ctsu_instance->p_cfg->md)) + { + g_touch_monitor_buf[index++] = 0x00; + element_num = p_instance_ctrl->p_touch_cfg->p_ctsu_instance->p_cfg->num_rx; + g_touch_monitor_buf[index++] = element_num; + for (i = 0; i < element_num; i++) + { + g_touch_monitor_buf[index++] = (uint8_t) (g_data[i]); + g_touch_monitor_buf[index++] = (uint8_t) (g_data[i] >> 8); + } + } + else + { + g_touch_monitor_buf[index++] = 0x01; + element_num = (uint8_t) (p_instance_ctrl->p_touch_cfg->p_ctsu_instance->p_cfg->num_rx * + p_instance_ctrl->p_touch_cfg->p_ctsu_instance->p_cfg->num_tx); + g_touch_monitor_buf[index++] = element_num; + for (i = 0; i < (element_num * 2); i++) + { + g_touch_monitor_buf[index++] = (uint8_t) (g_data[i]); + g_touch_monitor_buf[index++] = (uint8_t) (g_data[i] >> 8); + } + } + + #if (TOUCH_CFG_NUM_BUTTONS != 0) + if (0 < p_instance_ctrl->p_touch_cfg->num_buttons) + { + g_touch_monitor_buf[index++] = 0x00; + g_touch_monitor_buf[index++] = p_instance_ctrl->p_touch_cfg->num_buttons; + for (i = 0; i < p_instance_ctrl->p_touch_cfg->num_buttons; i++) + { + g_touch_monitor_buf[index++] = (uint8_t) ((p_instance_ctrl->binfo.status >> i) & 0x01); + g_touch_monitor_buf[index++] = (uint8_t) (p_instance_ctrl->binfo.p_reference[i]); + g_touch_monitor_buf[index++] = (uint8_t) (p_instance_ctrl->binfo.p_reference[i] >> 8); + g_touch_monitor_buf[index++] = (uint8_t) (p_instance_ctrl->binfo.p_threshold[i]); + g_touch_monitor_buf[index++] = (uint8_t) (p_instance_ctrl->binfo.p_threshold[i] >> 8); + g_touch_monitor_buf[index++] = (uint8_t) (p_instance_ctrl->binfo.p_hysteresis[i]); + g_touch_monitor_buf[index++] = (uint8_t) (p_instance_ctrl->binfo.p_hysteresis[i] >> 8); + } + } + #endif + } + + #if (TOUCH_CFG_NUM_SLIDERS != 0) + if (0 < p_instance_ctrl->p_touch_cfg->num_sliders) + { + g_touch_monitor_buf[index++] = 0x01; + g_touch_monitor_buf[index++] = p_instance_ctrl->p_touch_cfg->num_sliders; + for (i = 0; i < p_instance_ctrl->p_touch_cfg->num_sliders; i++) + { + g_touch_monitor_buf[index++] = (uint8_t) (p_instance_ctrl->sinfo.p_position[i]); + g_touch_monitor_buf[index++] = (uint8_t) (p_instance_ctrl->sinfo.p_position[i] >> 8); + g_touch_monitor_buf[index++] = (uint8_t) (p_instance_ctrl->sinfo.p_threshold[i]); + g_touch_monitor_buf[index++] = (uint8_t) (p_instance_ctrl->sinfo.p_threshold[i] >> 8); + } + } + #endif + #if (TOUCH_CFG_NUM_WHEELS != 0) + if (0 < p_instance_ctrl->p_touch_cfg->num_wheels) + { + g_touch_monitor_buf[index++] = 0x02; + g_touch_monitor_buf[index++] = p_instance_ctrl->p_touch_cfg->num_wheels; + for (i = 0; i < p_instance_ctrl->p_touch_cfg->num_wheels; i++) + { + g_touch_monitor_buf[index++] = (uint8_t) (p_instance_ctrl->winfo.p_position[i]); + g_touch_monitor_buf[index++] = (uint8_t) (p_instance_ctrl->winfo.p_position[i] >> 8); + g_touch_monitor_buf[index++] = (uint8_t) (p_instance_ctrl->winfo.p_threshold[i]); + g_touch_monitor_buf[index++] = (uint8_t) (p_instance_ctrl->winfo.p_threshold[i] >> 8); + } + } + #endif + g_touch_monitor_buf[index++] = g_touch_monitor_id++; + + #if (TOUCH_CFG_UART_MONITOR_SUPPORT == 1) + } + + if (!g_touch_uart_transmit_flag && (p_instance_ctrl->p_touch_cfg->number == g_touch_uart_monitor_num)) + { + g_touch_monitor_buf[index++] = TOUCH_UART_FOOTER; + g_touch_monitor_buf[2] = (uint8_t) (index); + g_touch_monitor_buf[3] = (uint8_t) (index >> 8); + g_touch_uart_transmit_flag = 1; + R_SCI_Send(g_touch_uart_control, &g_touch_monitor_buf[0] ,index); + + } + #endif +#endif + + return FSP_SUCCESS; +} + +/*******************************************************************************************************************//** + * @brief This function gets the current position of pad is being pressed. + * Implements @ref touch_api_t::padDataGet , g_touch_on_ctsu + * + * @retval FSP_SUCCESS Successfully data decoded. + * @retval FSP_ERR_ASSERTION Null pointer. + * @retval FSP_ERR_NOT_OPEN Module is not open. + * @retval FSP_ERR_CTSU_SCANNING Scanning this instance. + * @retval FSP_ERR_INVALID_HW_CONDITION Values scanned by CTSU show abnormal values + **********************************************************************************************************************/ +fsp_err_t RM_TOUCH_PadDataGet (touch_ctrl_t * const p_ctrl, + uint16_t * p_pad_rx_coordinate, + uint16_t * p_pad_tx_coordinate, + uint8_t * p_pad_num_touch) +{ + fsp_err_t err = FSP_SUCCESS; + touch_instance_ctrl_t * p_instance_ctrl = (touch_instance_ctrl_t *) p_ctrl; +#if (TOUCH_CFG_PAD_ENABLE) + uint16_t i; + uint16_t j; + uint8_t loop; + uint8_t num_x; + uint8_t num_y; + int16_t tmp_count; + int16_t tmp_value; + int32_t tmp_diff; + uint16_t element_num; + int32_t drift_diff; + uint8_t max_touch; /* number of max touch */ + #if (TOUCH_CFG_MONITOR_ENABLE) + uint16_t index = 0; + #endif + int32_t primary; + int32_t secondary; + int32_t mutual_diff; + + #if (TOUCH_CFG_PARAM_CHECKING_ENABLE == 1) + FSP_ASSERT(p_instance_ctrl); + FSP_ASSERT(p_pad_rx_coordinate); + FSP_ASSERT(p_pad_tx_coordinate); + FSP_ASSERT(p_pad_num_touch); + TOUCH_ERROR_RETURN(TOUCH_OPEN == p_instance_ctrl->open, FSP_ERR_NOT_OPEN); + #endif + + /* initialize touch number and corrdinate */ + *(p_instance_ctrl->pinfo.p_num_touch) = 0; + *(p_instance_ctrl->pinfo.p_rx_coordinate) = TOUCH_OFF_VALUE; + *(p_instance_ctrl->pinfo.p_tx_coordinate) = TOUCH_OFF_VALUE; + + /* Get local variable (TS number & data pinch) */ + num_x = p_instance_ctrl->p_touch_cfg->p_ctsu_instance->p_cfg->num_rx; + TOUCH_ERROR_RETURN(0 != num_x, FSP_ERR_ASSERTION); + num_y = p_instance_ctrl->p_touch_cfg->p_ctsu_instance->p_cfg->num_tx; + TOUCH_ERROR_RETURN(0 != num_y, FSP_ERR_ASSERTION); + element_num = (uint16_t) (num_x * num_y); + + /* Data get */ + err = p_instance_ctrl->p_ctsu_instance->p_api->dataGet(p_instance_ctrl->p_ctsu_instance->p_ctrl, (uint16_t *)g_touch_pad_buf); + FSP_ERROR_RETURN(FSP_ERR_CTSU_SCANNING != err, FSP_ERR_CTSU_SCANNING); + + /* check for max touch */ + if (*(p_instance_ctrl->pinfo.p_max_touch) > TOUCH_PAD_MONITOR_TOUCH_NUM_MAX) + { + max_touch = TOUCH_PAD_MONITOR_TOUCH_NUM_MAX; + } + else + { + max_touch = *(p_instance_ctrl->pinfo.p_max_touch); + } + + /* make difference value = secondary - primary */ + for (i = 0; i < element_num; i++) + { + primary = g_touch_pad_buf[i * 2]; + secondary = g_touch_pad_buf[(i * 2) + 1]; + mutual_diff = secondary - primary; + FSP_ERROR_RETURN(secondary < TOUCH_MUTUAL_SECONDARY_MAX, FSP_ERR_INVALID_HW_CONDITION); + FSP_ERROR_RETURN(mutual_diff < TOUCH_MUTUAL_INT16_MAX, FSP_ERR_INVALID_HW_CONDITION); + FSP_ERROR_RETURN(mutual_diff > -TOUCH_MUTUAL_INT16_MAX, FSP_ERR_INVALID_HW_CONDITION); + + /* save to buffer in the first half */ + g_touch_pad_buf[i] = (int16_t) mutual_diff; + } + + /* Data get section */ + if (!g_touch_base_set_falg) + { + /* format base value , changing the order */ + for (j = 0; j < num_x; j++) + { + for (i = 0; i < num_y; i++) + { + *(p_instance_ctrl->pinfo.p_base_buf + j + (i * num_x)) = (uint16_t) + (g_touch_pad_buf[*(p_instance_ctrl->p_touch_cfg->p_pad->p_elem_index_rx + j) + + (*(p_instance_ctrl->p_touch_cfg->p_pad->p_elem_index_tx + i) * num_x)]); + } + } + + g_touch_base_set_falg = 1; + } + else + { + /* format difference value */ + for (j = 0; j < num_x; j++) + { + for (i = 0; i < num_y; i++) + { + /* get count data ,and changing the order */ + tmp_value = g_touch_pad_buf[*(p_instance_ctrl->p_touch_cfg->p_pad->p_elem_index_rx + j) + + (*(p_instance_ctrl->p_touch_cfg->p_pad->p_elem_index_tx + i) * num_x)]; + + /* make difference from base value */ + tmp_diff = (int32_t)(*(p_instance_ctrl->pinfo.p_base_buf + j + (i * num_x))); + tmp_diff = tmp_diff - tmp_value; + + /* save difference value to buffer in the second half */ + g_touch_pad_buf[element_num + j + (i * num_x)] = (int16_t) tmp_diff; + } + } + } + + #if (TOUCH_CFG_MONITOR_ENABLE) + + /* copy from second half to first half for monitor */ + for (i = 0; i < element_num; i++) + { + g_touch_pad_buf[i] = g_touch_pad_buf[element_num + i]; + } + #endif + + if (g_touch_base_set_falg) + { + touch_pad_decode(&p_instance_ctrl->pinfo, num_x, num_y, max_touch); + + /* the drift correction process. */ + if (0 < p_instance_ctrl->pinfo.num_drift) + { + if (0 == *(p_instance_ctrl->pinfo.p_num_touch)) + { + /* If not touching, drift correction counter's being incremented */ + (*(p_instance_ctrl->pinfo.p_drift_count))++; + + for (i = 0; i < num_y; i++) + { + for (j = 0; j < num_x; j++) + { + /* It is an addition for the drift correction average calculation */ + tmp_count = g_touch_pad_buf[element_num + j + (i * num_x)]; + + drift_diff = (int32_t)(*(p_instance_ctrl->pinfo.p_drift_buf + (j + (i * num_x)))); + drift_diff += (int32_t) tmp_count; + *(p_instance_ctrl->pinfo.p_drift_buf + (j + (i * num_x))) = (uint32_t)drift_diff; + + if (*(p_instance_ctrl->pinfo.p_drift_count) >= p_instance_ctrl->pinfo.num_drift) + { + drift_diff = (int32_t) (*(p_instance_ctrl->pinfo.p_drift_buf + j + (i * num_x)) / + *(p_instance_ctrl->pinfo.p_drift_count)); + + *(p_instance_ctrl->pinfo.p_base_buf + j + (i * num_x)) = + (uint16_t) ((int32_t) (*(p_instance_ctrl->pinfo.p_base_buf + j + (i * num_x))) - + drift_diff); + + /* Clear total value for the average */ + *(p_instance_ctrl->pinfo.p_drift_buf + (j + (i * num_x))) = 0; + } + } + } + + if (*(p_instance_ctrl->pinfo.p_drift_count) >= p_instance_ctrl->pinfo.num_drift) + { + /* Count clear */ + *(p_instance_ctrl->pinfo.p_drift_count) = 0; + } + } + } + } + + /* Set user data */ + loop = 0; + do + { + *(p_pad_rx_coordinate + loop) = *(p_instance_ctrl->pinfo.p_rx_coordinate + loop); + *(p_pad_tx_coordinate + loop) = *(p_instance_ctrl->pinfo.p_tx_coordinate + loop); + loop++; + } while (loop < max_touch); + + *p_pad_num_touch = (uint8_t) (*(p_instance_ctrl->pinfo.p_num_touch)); + + /* Monitor part of ready */ + #if (TOUCH_CFG_MONITOR_ENABLE) + #if (TOUCH_CFG_UART_MONITOR_SUPPORT == 1) + if (!g_touch_uart_transmit_flag && (p_instance_ctrl->p_touch_cfg->number == g_touch_uart_monitor_num)) + { + /* Monitor Data Notification */ + g_touch_monitor_buf[index++] = TOUCH_UART_HEADER; + g_touch_monitor_buf[index++] = TOUCH_UART_RESPONSE_MONITOR2; + g_touch_monitor_buf[index++] = 0; /* Temporarily input the size */ + g_touch_monitor_buf[index++] = 0; /* Temporarily input the size */ + } + + if (!g_touch_uart_transmit_flag && + ((p_instance_ctrl->p_touch_cfg->number == g_touch_uart_monitor_num) || + (TOUCH_UART_MONITOR_NONE == g_touch_uart_monitor_num))) + { + if (TOUCH_UART_MONITOR_NONE == g_touch_uart_monitor_num) + { + for (i = 0; i < p_instance_ctrl->p_touch_cfg->number; i++) + { + index = (uint16_t) (index + g_touch_monitor_size[i]); + } + } + + #else /* TOUCH_CFG_UART_MONITOR_SUPPORT */ + for (i = 0; i < p_instance_ctrl->p_touch_cfg->number; i++) + { + index = (uint16_t) (index + g_touch_monitor_size[i]); + } + #endif + + g_touch_monitor_buf[index++] = g_touch_monitor_id; + g_touch_monitor_buf[index++] = p_instance_ctrl->p_touch_cfg->number; + + /* Pad data */ + g_touch_monitor_buf[index++] = 0x02; + g_touch_monitor_buf[index++] = (uint8_t) element_num; + + for (i = 0; i < element_num; i++) + { + tmp_value = g_touch_pad_buf[i]; + if (tmp_value < 0) + { + tmp_value = 0; + } + g_touch_monitor_buf[index++] = (uint8_t) (tmp_value); + g_touch_monitor_buf[index++] = (uint8_t) (tmp_value >> 8); + } + + g_touch_monitor_buf[index++] = 0x03; /* id of Pad */ + g_touch_monitor_buf[index++] = 0x01; /* number of Pad */ + + g_touch_monitor_buf[index++] = (uint8_t) (*(p_instance_ctrl->pinfo.p_num_touch)); /* number of touch */ + for (i = 0; i < TOUCH_PAD_MONITOR_TOUCH_NUM_MAX; i++) + { + if (i <= max_touch) + { + g_touch_monitor_buf[index++] = (uint8_t) (p_instance_ctrl->pinfo.p_rx_coordinate[i]); + g_touch_monitor_buf[index++] = (uint8_t) (p_instance_ctrl->pinfo.p_rx_coordinate[i] >> 8); + g_touch_monitor_buf[index++] = (uint8_t) (p_instance_ctrl->pinfo.p_tx_coordinate[i]); + g_touch_monitor_buf[index++] = (uint8_t) (p_instance_ctrl->pinfo.p_tx_coordinate[i] >> 8); + } + else + { + g_touch_monitor_buf[index++] = (uint8_t) (TOUCH_OFF_VALUE); + g_touch_monitor_buf[index++] = (uint8_t) (TOUCH_OFF_VALUE >> 8); + g_touch_monitor_buf[index++] = (uint8_t) (TOUCH_OFF_VALUE); + g_touch_monitor_buf[index++] = (uint8_t) (TOUCH_OFF_VALUE >> 8); + } + } + + g_touch_monitor_buf[index++] = (uint8_t) (*(p_instance_ctrl->pinfo.p_threshold)); + g_touch_monitor_buf[index++] = (uint8_t) ((*(p_instance_ctrl->pinfo.p_threshold)) >> 8); + g_touch_monitor_buf[index++] = (uint8_t) (*(p_instance_ctrl->pinfo.p_rx_pixel)); + g_touch_monitor_buf[index++] = (uint8_t) ((*(p_instance_ctrl->pinfo.p_rx_pixel)) >> 8); + g_touch_monitor_buf[index++] = (uint8_t) (*(p_instance_ctrl->pinfo.p_tx_pixel)); + g_touch_monitor_buf[index++] = (uint8_t) ((*(p_instance_ctrl->pinfo.p_tx_pixel)) >> 8); + g_touch_monitor_buf[index++] = (*(p_instance_ctrl->pinfo.p_max_touch)); + g_touch_monitor_buf[index++] = (p_instance_ctrl->pinfo.num_drift); + g_touch_monitor_buf[index++] = g_touch_monitor_id++; + + #if (TOUCH_CFG_UART_MONITOR_SUPPORT == 1) + } + + if (p_instance_ctrl->p_touch_cfg->number == g_touch_uart_monitor_num) + { + g_touch_monitor_buf[index++] = TOUCH_UART_FOOTER; + g_touch_monitor_buf[2] = (uint8_t) (index); + g_touch_monitor_buf[3] = (uint8_t) (index >> 8); + g_touch_uart_transmit_flag = 1; + gp_touch_uart_instance->p_api->write(gp_touch_uart_instance->p_ctrl, + (uint8_t *) g_touch_monitor_buf, + index); + } + #endif + #endif /* TOUCH_CFG_MONITOR_ENABLE */ +#else /* TOUCH_CFG_PAD_ENABLE */ + #if (TOUCH_CFG_PARAM_CHECKING_ENABLE == 1) + FSP_ASSERT(p_instance_ctrl); + FSP_ASSERT(p_pad_rx_coordinate); + FSP_ASSERT(p_pad_tx_coordinate); + FSP_ASSERT(p_pad_num_touch); + TOUCH_ERROR_RETURN(TOUCH_OPEN == p_instance_ctrl->open, FSP_ERR_NOT_OPEN); + #endif + FSP_PARAMETER_NOT_USED(p_pad_rx_coordinate); + FSP_PARAMETER_NOT_USED(p_pad_tx_coordinate); + FSP_PARAMETER_NOT_USED(p_pad_num_touch); + FSP_PARAMETER_NOT_USED(p_instance_ctrl); + FSP_PARAMETER_NOT_USED(FSP_ERR_CTSU_SCANNING); +#endif /* TOUCH_CFG_PAD_ENABLE */ + return err; +} + +/*******************************************************************************************************************//** + * @brief Scan stop specified TOUCH control block. Implements @ref touch_api_t::scanStop. + * + * @retval FSP_SUCCESS Successfully closed. + * @retval FSP_ERR_ASSERTION Null pointer passed as a parameter. + * @retval FSP_ERR_NOT_OPEN Module is not open. + **********************************************************************************************************************/ +fsp_err_t RM_TOUCH_ScanStop (touch_ctrl_t * const p_ctrl) +{ + touch_instance_ctrl_t * p_instance_ctrl = (touch_instance_ctrl_t *) p_ctrl; + fsp_err_t err = FSP_SUCCESS; + + FSP_ASSERT(p_instance_ctrl); + TOUCH_ERROR_RETURN(TOUCH_OPEN == p_instance_ctrl->open, FSP_ERR_NOT_OPEN); + + err = p_instance_ctrl->p_ctsu_instance->p_api->scanStop(p_instance_ctrl->p_ctsu_instance->p_ctrl); + + return err; +} + +/*******************************************************************************************************************//** + * Updates the user callback and has option of providing memory for callback structure. + * Implements touch_api_t::callbackSet + * + * @retval FSP_SUCCESS Callback updated successfully. + * @retval FSP_ERR_ASSERTION A required pointer is NULL. + * @retval FSP_ERR_NOT_OPEN The control block has not been opened. + **********************************************************************************************************************/ +fsp_err_t RM_TOUCH_CallbackSet (touch_ctrl_t * const p_api_ctrl, + void ( * p_callback)(touch_callback_args_t *), + void const * const p_context, + touch_callback_args_t * const p_callback_memory) +{ + fsp_err_t err = FSP_SUCCESS; + touch_instance_ctrl_t * p_ctrl = (touch_instance_ctrl_t *) p_api_ctrl; + +#if (TOUCH_CFG_PARAM_CHECKING_ENABLE == 1) + FSP_ASSERT(p_ctrl); + FSP_ASSERT(p_callback); + FSP_ERROR_RETURN(TOUCH_OPEN == p_ctrl->open, FSP_ERR_NOT_OPEN); +#endif + + err = p_ctrl->p_ctsu_instance->p_api->callbackSet(p_ctrl->p_ctsu_instance->p_ctrl, + p_callback, + p_context, + p_callback_memory); + + return err; +} + +/*******************************************************************************************************************//** + * @brief Disables specified TOUCH control block. Implements @ref touch_api_t::close. + * + * @retval FSP_SUCCESS Successfully closed. + * @retval FSP_ERR_ASSERTION Null pointer passed as a parameter. + * @retval FSP_ERR_NOT_OPEN Module is not open. + **********************************************************************************************************************/ +fsp_err_t RM_TOUCH_Close (touch_ctrl_t * const p_ctrl) +{ + touch_instance_ctrl_t * p_instance_ctrl = (touch_instance_ctrl_t *) p_ctrl; + fsp_err_t err = FSP_SUCCESS; +#if (TOUCH_CFG_UART_TUNING_SUPPORT == 1) + uint8_t num = 0; +#endif + + FSP_ASSERT(p_instance_ctrl); + TOUCH_ERROR_RETURN(TOUCH_OPEN == p_instance_ctrl->open, FSP_ERR_NOT_OPEN); + +#if (TOUCH_CFG_NUM_BUTTONS != 0) + g_touch_button_index = (uint8_t) (g_touch_button_index - p_instance_ctrl->p_touch_cfg->num_buttons); +#endif +#if (TOUCH_CFG_NUM_SLIDERS != 0) + g_touch_slider_index = (uint8_t) (g_touch_slider_index - p_instance_ctrl->p_touch_cfg->num_sliders); +#endif +#if (TOUCH_CFG_NUM_WHEELS != 0) + g_touch_wheel_index = (uint8_t) (g_touch_wheel_index - p_instance_ctrl->p_touch_cfg->num_wheels); +#endif + + err = p_instance_ctrl->p_ctsu_instance->p_api->close(p_instance_ctrl->p_ctsu_instance->p_ctrl); + p_instance_ctrl->open = false; + +#if TOUCH_CFG_MONITOR_ENABLE + #if (TOUCH_CFG_UART_MONITOR_SUPPORT == 1) + g_touch_uart_open_bitmap &= ~((uint32_t) 1 << p_instance_ctrl->p_touch_cfg->number); + gp_touch_ctrl_list[p_instance_ctrl->p_touch_cfg->number] = p_instance_ctrl; + if (0 == g_touch_uart_open_bitmap) + { + R_SCI_Close(g_touch_uart_control); + g_touch_uart_control = NULL; + g_touch_uart_monitor_num = TOUCH_UART_MONITOR_NONE; + } + #endif +#endif + +#if (TOUCH_CFG_UART_TUNING_SUPPORT == 1) + if (NULL != g_touch_uart_control) + { + #if (TOUCH_CFG_NUM_BUTTONS != 0) + num = g_touch_button_index; + #endif + #if (TOUCH_CFG_NUM_SLIDERS != 0) + num = (uint8_t) (num + g_touch_slider_index); + #endif + #if (TOUCH_CFG_NUM_WHEELS != 0) + num = (uint8_t) (num + g_touch_wheel_index); + #endif + if (0 == num) + { + R_SCI_Close(g_touch_uart_control); + g_touch_uart_control = NULL; + } + } +#endif + + return err; +} + +/*******************************************************************************************************************//** + * @brief Get the touch sensitivity ratio. Implements @ref touch_api_t::sensitivityRatioGet. + * + * @retval FSP_SUCCESS Successfully touch sensitivity ratio got. + * @retval FSP_ERR_ASSERTION Null pointer passed as a parameter. + * @retval FSP_ERR_NOT_OPEN Module is not open. + * @retval FSP_ERR_CTSU_SCANNING Scanning this instance. + * @retval FSP_ERR_CTSU_INCOMPLETE_TUNING Incomplete initial offset tuning. + **********************************************************************************************************************/ +fsp_err_t RM_TOUCH_SensitivityRatioGet (touch_ctrl_t * const p_ctrl, + touch_sensitivity_info_t * p_touch_sensitivity_info) +{ + fsp_err_t err = FSP_SUCCESS; + touch_instance_ctrl_t * p_instance_ctrl = (touch_instance_ctrl_t *) p_ctrl; + int16_t elem_num; + uint16_t data[CTSU_CFG_NUM_SELF_ELEMENTS + (CTSU_CFG_NUM_MUTUAL_ELEMENTS * 2) * + CTSU_MAJORITY_MODE_ELEMENTS]; + uint8_t button_id; + uint8_t i; +#if (CTSU_CFG_NUM_MUTUAL_ELEMENTS != 0) + uint16_t diff; +#endif + +#if (TOUCH_CFG_PARAM_CHECKING_ENABLE == 1) + FSP_ASSERT(p_instance_ctrl); + FSP_ASSERT(p_touch_sensitivity_info); + TOUCH_ERROR_RETURN(TOUCH_OPEN == p_instance_ctrl->open, FSP_ERR_NOT_OPEN); +#endif +#if (CTSU_CFG_MAJORITY_MODE & CTSU_JUDGEMENT_MAJORITY_MODE) + if (p_instance_ctrl->p_ctsu_instance->p_cfg->majority_mode == 1) + { + elem_num = CTSU_MAJORITY_MODE_ELEMENTS; + } + else +#endif + { + elem_num = 1; + } + + err = p_instance_ctrl->p_ctsu_instance->p_api->dataGet(p_instance_ctrl->p_ctsu_instance->p_ctrl, data); + FSP_ERROR_RETURN(FSP_ERR_CTSU_SCANNING != err, FSP_ERR_CTSU_SCANNING); + FSP_ERROR_RETURN(FSP_ERR_CTSU_INCOMPLETE_TUNING != err, FSP_ERR_CTSU_INCOMPLETE_TUNING); + + for (button_id = 0; button_id < p_instance_ctrl->p_touch_cfg->num_buttons; button_id++) + { + for (i = 0; i < elem_num; i++) + { +#if (CTSU_CFG_NUM_SELF_ELEMENTS != 0) + if (CTSU_MODE_SELF_MULTI_SCAN == p_instance_ctrl->p_ctsu_instance->p_cfg->md) + { + if (0 > + ((int32_t) data[(button_id * elem_num) + i] - + p_instance_ctrl->binfo.p_reference[(button_id * elem_num) + i])) + { + p_touch_sensitivity_info->p_touch_sensitivity_ratio[(button_id * elem_num) + i] = 0; + } + else + { + p_touch_sensitivity_info->p_touch_sensitivity_ratio[(button_id * elem_num) + i] = + (uint16_t) (((int32_t) data[(button_id * elem_num) + i] - + p_instance_ctrl->binfo.p_reference[(button_id * elem_num) + i]) * + p_touch_sensitivity_info->new_threshold_ratio / + p_instance_ctrl->binfo.p_threshold[(button_id * elem_num) + i]); + } + } +#endif +#if (CTSU_CFG_NUM_MUTUAL_ELEMENTS != 0) + if (CTSU_MODE_MUTUAL_FULL_SCAN == p_instance_ctrl->p_ctsu_instance->p_cfg->md) + { + diff = data[(button_id * elem_num + i) * 2 + 1] - + data[(button_id * elem_num + i) * 2]; + if (0 > + ((int32_t) p_instance_ctrl->binfo.p_reference[(button_id * elem_num + i)] - diff)) + { + p_touch_sensitivity_info->p_touch_sensitivity_ratio[(button_id * elem_num) + i] = 0; + } + else + { + p_touch_sensitivity_info->p_touch_sensitivity_ratio[(button_id * elem_num) + i] = + (uint16_t) (((int32_t) p_instance_ctrl->binfo.p_reference[(button_id * elem_num) + i] - + diff) * + p_touch_sensitivity_info->new_threshold_ratio / + p_instance_ctrl->binfo.p_threshold[(button_id * elem_num) + i]); + } + } +#endif + else + { + /* Not supported */ + } + } + } + + return FSP_SUCCESS; +} + +/*******************************************************************************************************************//** + * @brief Adjust the touch judgment threshold. Implements @ref touch_api_t::thresholdAdjust. + * + * @retval FSP_SUCCESS Successfully touch judgment threshold was adjusted. + * @retval FSP_ERR_ASSERTION Null pointer passed as a parameter. + * @retval FSP_ERR_NOT_OPEN Module is not open. + **********************************************************************************************************************/ +fsp_err_t RM_TOUCH_ThresholdAdjust (touch_ctrl_t * const p_ctrl, touch_sensitivity_info_t * p_touch_sensitivity_info) +{ + touch_instance_ctrl_t * p_instance_ctrl = (touch_instance_ctrl_t *) p_ctrl; + uint8_t button_id; + uint32_t touch_sensitivity_ratio_32bit; + int16_t elem_num; + uint8_t i; + +#if (TOUCH_CFG_PARAM_CHECKING_ENABLE == 1) + FSP_ASSERT(p_instance_ctrl); + FSP_ASSERT(p_touch_sensitivity_info); + FSP_ASSERT(p_touch_sensitivity_info->old_threshold_ratio); + TOUCH_ERROR_RETURN(TOUCH_OPEN == p_instance_ctrl->open, FSP_ERR_NOT_OPEN); +#endif +#if (CTSU_CFG_MAJORITY_MODE & CTSU_JUDGEMENT_MAJORITY_MODE) + if (p_instance_ctrl->p_touch_cfg->p_ctsu_instance->p_cfg->majority_mode == 1) + { + elem_num = CTSU_MAJORITY_MODE_ELEMENTS; + } + else +#endif + { + elem_num = 1; + } + + /* The threshold value for a touch condition is calculated by the QE Tool to be 60% of the + * Touch Count - No Touch Count. + * Similarly, the hysteresis is set by QE tool as 5% of the threshold value. + * This threshold value (as a percentage) can be modified by passing in the parameters: + * old_threshold_ratio : value set by QE tool + * new_threshold_ratio : required value + * new_hysteresis_ratio : required hysteresis + */ + for (button_id = 0; button_id < p_instance_ctrl->p_touch_cfg->num_buttons; button_id++) + { + for (i = 0; i < elem_num; i++) + { + /* Modify the threshold and hysteresis for each button */ + p_instance_ctrl->binfo.p_threshold[(button_id * elem_num) + i] = + (uint16_t) ((uint32_t) p_instance_ctrl->p_touch_cfg->p_buttons[(button_id * elem_num) + i].threshold * + p_touch_sensitivity_info->new_threshold_ratio / + p_touch_sensitivity_info->old_threshold_ratio); + p_instance_ctrl->binfo.p_hysteresis[(button_id * elem_num) + + i] = + TOUCH_RATIO_CALC(p_instance_ctrl->binfo.p_threshold[(button_id * elem_num) + i] * + p_touch_sensitivity_info->new_hysteresis_ratio); + + /* Now adjust these values with the threshold offset values that are passed in via p_modifier */ + touch_sensitivity_ratio_32bit = + (uint32_t) p_touch_sensitivity_info->p_touch_sensitivity_ratio[(button_id * elem_num) + i]; + touch_sensitivity_ratio_32bit = TOUCH_RATIO_CALC( + touch_sensitivity_ratio_32bit * p_instance_ctrl->binfo.p_threshold[(button_id * elem_num) + i]); + p_instance_ctrl->binfo.p_threshold[(button_id * elem_num) + i] = (uint16_t) touch_sensitivity_ratio_32bit; + p_instance_ctrl->binfo.p_hysteresis[(button_id * elem_num) + i] = TOUCH_RATIO_CALC( + touch_sensitivity_ratio_32bit * p_touch_sensitivity_info->new_hysteresis_ratio); + } + } + + return FSP_SUCCESS; +} + +/*******************************************************************************************************************//** + * @brief Control drift correction. Implements @ref touch_api_t::driftControl. + * + * @retval FSP_SUCCESS Successfully drift correction was controlled. + * @retval FSP_ERR_ASSERTION Null pointer passed as a parameter. + * @retval FSP_ERR_NOT_OPEN Module is not open. + **********************************************************************************************************************/ +fsp_err_t RM_TOUCH_DriftControl (touch_ctrl_t * const p_ctrl, uint16_t input_drift_freq) +{ + touch_instance_ctrl_t * p_instance_ctrl = (touch_instance_ctrl_t *) p_ctrl; + +#if (TOUCH_CFG_PARAM_CHECKING_ENABLE == 1) + FSP_ASSERT(p_instance_ctrl); + TOUCH_ERROR_RETURN(TOUCH_OPEN == p_instance_ctrl->open, FSP_ERR_NOT_OPEN); +#endif + + p_instance_ctrl->binfo.drift_freq = input_drift_freq; + + return FSP_SUCCESS; +} + +/*******************************************************************************************************************//** + * @brief This function get the variable address of QE Monitor. + * This function is used for QE Monitor when both automatic judgement and software judgement are operated. + * Implements @ref touch_api_t::monitorAddressGet + * + * @retval FSP_SUCCESS Successfully QE monitor variable address was got. + * @retval FSP_ERR_ASSERTION Null pointer passed as a parameter. + * @retval FSP_ERR_NOT_OPEN Module is not open. + * @retval FSP_ERR_NOT_ENABLED Requested operation is not enabled + **********************************************************************************************************************/ +fsp_err_t RM_TOUCH_MonitorAddressGet (touch_ctrl_t * const p_ctrl, + uint8_t ** pp_monitor_buf, + uint8_t ** pp_monitor_id, + uint16_t ** pp_monitor_size) +{ +#if (CTSU_CFG_AUTO_JUDGE_ENABLE == 1) + #if TOUCH_CFG_MONITOR_ENABLE + touch_instance_ctrl_t * p_instance_ctrl = (touch_instance_ctrl_t *) p_ctrl; + + #if (TOUCH_CFG_PARAM_CHECKING_ENABLE == 1) + FSP_ASSERT(p_instance_ctrl); + TOUCH_ERROR_RETURN(TOUCH_OPEN == p_instance_ctrl->open, FSP_ERR_NOT_OPEN); + #endif + + * pp_monitor_buf = (uint8_t *)g_touch_monitor_buf; + * pp_monitor_id = &g_touch_monitor_id; + * pp_monitor_size = g_touch_monitor_size; + return FSP_SUCCESS; + #else + FSP_PARAMETER_NOT_USED(pp_monitor_buf); + FSP_PARAMETER_NOT_USED(pp_monitor_id); + FSP_PARAMETER_NOT_USED(pp_monitor_size); + FSP_PARAMETER_NOT_USED(p_ctrl); + return FSP_ERR_NOT_ENABLED; + #endif +#else + FSP_PARAMETER_NOT_USED(pp_monitor_buf); + FSP_PARAMETER_NOT_USED(pp_monitor_id); + FSP_PARAMETER_NOT_USED(pp_monitor_size); + FSP_PARAMETER_NOT_USED(p_ctrl); + return FSP_ERR_NOT_ENABLED; +#endif +} + +/*******************************************************************************************************************//** + * @} (end addtogroup TOUCH) + **********************************************************************************************************************/ + +/*********************************************************************************************************************** + * Private Functions + **********************************************************************************************************************/ + +/*******************************************************************************************************************//** + * Internal touch private function. + **********************************************************************************************************************/ +#if (TOUCH_CFG_NUM_BUTTONS != 0) +static fsp_err_t touch_button_process (touch_instance_ctrl_t * p_instance_ctrl, uint16_t * p_data, uint64_t * p_status) +{ + fsp_err_t err = FSP_SUCCESS; + uint16_t sensor_val = 0; + touch_mm_info_t * p_mm_info; + #if (CTSU_CFG_NUM_MUTUAL_ELEMENTS != 0) + int16_t sensor_mutual_val = 0; + int32_t primary; + int32_t secondary; + int32_t mutual_diff; + #endif + + uint8_t button_id; + uint8_t tmp_id; + uint8_t majority_mode_elems; + uint8_t majority_mode_num; + + p_mm_info = (p_instance_ctrl->p_touch_mm_info); + #if (CTSU_CFG_MAJORITY_MODE & CTSU_JUDGEMENT_MAJORITY_MODE) + if (p_instance_ctrl->p_ctsu_instance->p_cfg->majority_mode == 1) + { + majority_mode_num = CTSU_MAJORITY_MODE_ELEMENTS; + } + else + #endif + { + majority_mode_num = 1; + } + + for (button_id = 0; button_id < p_instance_ctrl->p_touch_cfg->num_buttons; button_id++) + { + p_mm_info[button_id].majority_mode = p_instance_ctrl->p_ctsu_instance->p_cfg->majority_mode; + for (majority_mode_elems = 0; majority_mode_elems < majority_mode_num; majority_mode_elems++) + { + tmp_id = (uint8_t) (button_id * majority_mode_num + majority_mode_elems); + + #if (CTSU_CFG_NUM_SELF_ELEMENTS != 0) + if (CTSU_MODE_SELF_MULTI_SCAN == p_instance_ctrl->p_ctsu_instance->p_cfg->md) + { + sensor_val = *(p_data + ((p_instance_ctrl->p_touch_cfg->p_buttons[tmp_id].elem_index) * + majority_mode_num) + majority_mode_elems); + } + #endif + #if (CTSU_CFG_NUM_MUTUAL_ELEMENTS != 0) + if (CTSU_MODE_MUTUAL_FULL_SCAN == + (CTSU_MODE_MUTUAL_FULL_SCAN & p_instance_ctrl->p_ctsu_instance->p_cfg->md)) + { + primary = (int32_t) *(p_data + ((p_instance_ctrl->p_touch_cfg->p_buttons[tmp_id].elem_index * 2) * + majority_mode_num) + (majority_mode_elems * 2)); + secondary = (int32_t) *(p_data + ((p_instance_ctrl->p_touch_cfg->p_buttons[tmp_id].elem_index * 2) * + majority_mode_num) + (majority_mode_elems * 2) + 1); + mutual_diff = secondary - primary; + + FSP_ERROR_RETURN(secondary < TOUCH_MUTUAL_SECONDARY_MAX, FSP_ERR_INVALID_HW_CONDITION); + FSP_ERROR_RETURN(mutual_diff < TOUCH_MUTUAL_INT16_MAX, FSP_ERR_INVALID_HW_CONDITION); + FSP_ERROR_RETURN(mutual_diff > -TOUCH_MUTUAL_INT16_MAX, FSP_ERR_INVALID_HW_CONDITION); + + /* The value of Primary count minus secondary count */ + sensor_mutual_val = (int16_t) mutual_diff; + sensor_val = (uint16_t) sensor_mutual_val; + } + #endif + if (0 == + *(p_instance_ctrl->binfo.p_reference + (majority_mode_num * button_id + majority_mode_elems))) + { + *(p_instance_ctrl->binfo.p_reference + + (majority_mode_num * button_id + majority_mode_elems)) = sensor_val; + } + else + { + /* Create button status */ + #if (CTSU_CFG_NUM_SELF_ELEMENTS != 0) + if (CTSU_MODE_SELF_MULTI_SCAN == p_instance_ctrl->p_ctsu_instance->p_cfg->md) + { + p_mm_info[button_id].id = button_id; + p_mm_info[button_id].mm_index = majority_mode_elems; + touch_button_self_decode(&p_instance_ctrl->binfo, sensor_val, &p_mm_info[button_id]); + touch_button_drift(&p_instance_ctrl->binfo, sensor_val, p_mm_info[button_id]); + } + #endif + #if (CTSU_CFG_NUM_MUTUAL_ELEMENTS != 0) + if (CTSU_MODE_MUTUAL_FULL_SCAN == + (CTSU_MODE_MUTUAL_FULL_SCAN & p_instance_ctrl->p_ctsu_instance->p_cfg->md)) + { + p_mm_info[button_id].id = button_id; + p_mm_info[button_id].mm_index = majority_mode_elems; + touch_button_mutual_decode(&p_instance_ctrl->binfo, sensor_mutual_val, &p_mm_info[button_id]); + touch_button_mutual_drift(&p_instance_ctrl->binfo, sensor_mutual_val, p_mm_info[button_id]); + } + #endif + } + } + } + + if ((p_instance_ctrl->p_touch_cfg->num_buttons != 0) && (p_status != NULL)) + { + /** status is 64-bitmap */ + *p_status = p_instance_ctrl->binfo.status; + } + + return err; +} + + #if (CTSU_CFG_NUM_SELF_ELEMENTS != 0) + +/*********************************************************************************************************************** + * Function Name: touch_button_self_decode + * Description : Self Touch Button decoding + * Arguments : touch_button_info_t p_binfo : Pointer to Button Information structure + * : uint16_t value : Sensor value from CTSU + * : touch_mm_info_t p_button_mm_info : Button infomation structure + * Return Value : None + ***********************************************************************************************************************/ +void touch_button_self_decode (touch_button_info_t * p_binfo, uint16_t value, touch_mm_info_t * p_button_mm_info) +{ + uint32_t threshold; + uint16_t threshold_sub_hys; + uint8_t button_id = p_button_mm_info->id; + uint8_t jmm_id = p_button_mm_info->mm_index; + #if ((CTSU_CFG_MAJORITY_MODE & CTSU_JUDGEMENT_MAJORITY_MODE) || \ + (TOUCH_CFG_CHATTERING_SUPPRESSION_TYPE == 1)) + uint8_t majority_mode_elems; + #endif + #if (CTSU_CFG_MAJORITY_MODE & CTSU_JUDGEMENT_MAJORITY_MODE) + uint8_t result = 0; + uint8_t id_in_elem = CTSU_MAJORITY_MODE_ELEMENTS - 1; + #endif + uint8_t majority_mode_num; + if (p_button_mm_info->majority_mode == 1) + { + majority_mode_num = CTSU_MAJORITY_MODE_ELEMENTS; + } + else + { + majority_mode_num = 1; + } + + threshold = (uint32_t) (*(p_binfo->p_reference + (majority_mode_num * button_id + jmm_id))) + + (uint32_t) (*(p_binfo->p_threshold + (majority_mode_num * button_id + jmm_id))); + + if (TOUCH_COUNT_MAX < threshold) + { + return; + } + + threshold_sub_hys = (uint16_t) (threshold - (*(p_binfo->p_hysteresis + + (majority_mode_num * button_id + jmm_id)))); + + /* threshold_sub_hys < scan value = Touch */ + if (threshold < value) + { + (*(p_binfo->p_off_count + button_id * majority_mode_num + jmm_id)) = 0; /* non_touch count reset */ + /* positive noise filter */ + if (p_binfo->on_freq <= (*(p_binfo->p_on_count + button_id * majority_mode_num + jmm_id))) + { + p_button_mm_info->mm_result[jmm_id] = 1; + + if (p_binfo->cancel_freq > p_binfo->on_freq) + { + /* press and hold to cancel*/ + if (p_binfo->cancel_freq <= + (*(p_binfo->p_on_count + button_id * majority_mode_num + jmm_id))) + { + (*(p_binfo->p_on_count + button_id * majority_mode_num + jmm_id)) = 0; /* touch count reset */ + p_button_mm_info->mm_result[jmm_id] = 0; + *(p_binfo->p_reference + button_id * majority_mode_num + jmm_id) = value; + } + else + { + (*(p_binfo->p_on_count + button_id * majority_mode_num + jmm_id))++; + } + } + } + else + { + (*(p_binfo->p_on_count + button_id * majority_mode_num + jmm_id))++; + } + } + else if (threshold_sub_hys > value) + { + (*(p_binfo->p_on_count + button_id * majority_mode_num + jmm_id)) = 0; /* touch count reset */ + /* negative noise filter */ + if (p_binfo->off_freq <= (*(p_binfo->p_off_count + button_id * majority_mode_num + jmm_id))) + { + p_button_mm_info->mm_result[jmm_id] = 0; + } + else + { + (*(p_binfo->p_off_count + button_id * majority_mode_num + jmm_id))++; + } + } + + #if (TOUCH_CFG_CHATTERING_SUPPRESSION_TYPE == 0) + else + { + /* Do nothing during hysteresis */ + } + #else + else + { + /* touch count reset during hysteresis */ + for (majority_mode_elems = 0; majority_mode_elems < majority_mode_num; majority_mode_elems++) + { + (*(p_binfo->p_on_count + button_id * majority_mode_num + majority_mode_elems)) = 0; + (*(p_binfo->p_off_count + button_id * majority_mode_num + majority_mode_elems)) = 0; + } + } + #endif + #if (CTSU_CFG_MAJORITY_MODE & CTSU_JUDGEMENT_MAJORITY_MODE) + if (p_button_mm_info->majority_mode == 1) + { + /*Create result of Software JMM*/ + if (id_in_elem <= jmm_id) + { + for (majority_mode_elems = 0; majority_mode_elems < CTSU_MAJORITY_MODE_ELEMENTS; majority_mode_elems++) + { + if (1 == p_button_mm_info->mm_result[majority_mode_elems]) + { + result++; + } + } + + if (TOUCH_CFG_MAJORITY_NUM <= result) + { + /* ===== touch ON result ===== */ + p_binfo->status |= ((uint64_t) 1 << button_id); + } + else + { + /* ===== touch OFF result ===== */ + p_binfo->status &= ~(((uint64_t) 1 << button_id)); + } + } + } + else + #endif + { + /*Create result of Software VMM */ + if (1 == p_button_mm_info->mm_result[jmm_id]) + { + /* ===== touch ON result ===== */ + p_binfo->status |= ((uint64_t) 1 << button_id); + } + else + { + /* ===== touch OFF result ===== */ + p_binfo->status &= ~(((uint64_t) 1 << button_id)); + } + } +} + + #endif + + #if (CTSU_CFG_NUM_MUTUAL_ELEMENTS != 0) + +/*********************************************************************************************************************** + * Function Name: touch_button_mutual_decode + * Description : Mutual Touch Button decoding + * Arguments : touch_button_info_t p_binfo : Pointer to Button Information structure + * : int16_t value : Sensor value from CTSU + * : touch_mm_info_t p_button_mm_info : Button infomation structure + * Return Value : None + ***********************************************************************************************************************/ +void touch_button_mutual_decode (touch_button_info_t * p_binfo, int16_t value, touch_mm_info_t * p_button_mm_info) +{ + int16_t threshold; + int16_t reference; + int16_t hysteresis; + int32_t hysteresis_32; + uint8_t button_id = p_button_mm_info->id; + uint8_t jmm_id = p_button_mm_info->mm_index; + #if ((CTSU_CFG_MAJORITY_MODE & CTSU_JUDGEMENT_MAJORITY_MODE) || \ + (TOUCH_CFG_CHATTERING_SUPPRESSION_TYPE == 1)) + uint8_t majority_mode_elems; + #endif + #if (CTSU_CFG_MAJORITY_MODE & CTSU_JUDGEMENT_MAJORITY_MODE) + uint8_t result = 0; + uint8_t id_in_elem = CTSU_MAJORITY_MODE_ELEMENTS - 1; + #endif + uint8_t majority_mode_num; + if (p_button_mm_info->majority_mode == 1) + { + majority_mode_num = CTSU_MAJORITY_MODE_ELEMENTS; + } + else + { + majority_mode_num = 1; + } + + reference = (int16_t) *(p_binfo->p_reference + (majority_mode_num * button_id + jmm_id)); + threshold = (int16_t) *(p_binfo->p_threshold + (majority_mode_num * button_id + jmm_id)); + threshold = (int16_t) (reference - threshold); + + /* get current threshold_add_hys */ + hysteresis_32 = (int32_t) *(p_binfo->p_hysteresis + (majority_mode_num * button_id + jmm_id)); + hysteresis_32 = (int32_t) threshold + hysteresis_32; + + /* error process (touch_cnt is 16bit) */ + if ((TOUCH_MUTUAL_INT16_MAX < hysteresis_32) || + (-TOUCH_MUTUAL_INT16_MAX > hysteresis_32)) + { + return; + } + + hysteresis = (int16_t) hysteresis_32; + + /* threshold_add_hys > scan value = Touch */ + if (threshold > value) + { + (*(p_binfo->p_off_count + button_id * majority_mode_num + jmm_id)) = 0; /* non_touch count reset */ + /* positive noise filter */ + if (p_binfo->on_freq <= (*(p_binfo->p_on_count + button_id * majority_mode_num + jmm_id))) + { + p_button_mm_info->mm_result[jmm_id] = 1; + + if (p_binfo->cancel_freq > p_binfo->on_freq) + { + /* press and hold to cancel */ + if (p_binfo->cancel_freq <= + (*(p_binfo->p_on_count + button_id * majority_mode_num + jmm_id))) + { + (*(p_binfo->p_on_count + button_id * majority_mode_num + jmm_id)) = 0; /* touch count reset */ + p_button_mm_info->mm_result[jmm_id] = 0; + *(p_binfo->p_reference + button_id * majority_mode_num + jmm_id) = (uint16_t) value; + } + else + { + (*(p_binfo->p_on_count + button_id * majority_mode_num + jmm_id))++; + } + } + } + else + { + (*(p_binfo->p_on_count + button_id * majority_mode_num + jmm_id))++; + } + } + else if (hysteresis < value) + { + (*(p_binfo->p_on_count + button_id * majority_mode_num + jmm_id)) = 0; /* touch count reset */ + /* negative noise filter */ + if (p_binfo->off_freq <= (*(p_binfo->p_off_count + button_id * majority_mode_num + jmm_id))) + { + p_button_mm_info->mm_result[jmm_id] = 0; + } + else + { + (*(p_binfo->p_off_count + button_id * majority_mode_num + jmm_id))++; + } + } + + #if (TOUCH_CFG_CHATTERING_SUPPRESSION_TYPE == 0) + else + { + /* Do nothing during hysteresis */ + } + #else + else + { + /* touch count reset during hysteresis */ + for (majority_mode_elems = 0; majority_mode_elems < majority_mode_num; majority_mode_elems++) + { + (*(p_binfo->p_on_count + (majority_mode_num * button_id + majority_mode_elems))) = 0; + (*(p_binfo->p_off_count + (majority_mode_num * button_id + majority_mode_elems))) = 0; + } + } + #endif + #if (CTSU_CFG_MAJORITY_MODE & CTSU_JUDGEMENT_MAJORITY_MODE) + if (p_button_mm_info->majority_mode == 1) + { + /*Create result of Software JMM*/ + if (id_in_elem <= jmm_id) + { + for (majority_mode_elems = 0; majority_mode_elems < majority_mode_num; majority_mode_elems++) + { + if (1 == p_button_mm_info->mm_result[majority_mode_elems]) + { + result++; + } + } + + if (TOUCH_CFG_MAJORITY_NUM <= result) + { + /* ===== touch ON result ===== */ + p_binfo->status |= ((uint64_t) 1 << button_id); + } + else + { + /* ===== touch OFF result ===== */ + p_binfo->status &= ~(((uint64_t) 1 << button_id)); + } + } + } + else + #endif + { + /*Create result of Software VMM */ + if (1 == p_button_mm_info->mm_result[jmm_id]) + { + /* ===== touch ON result ===== */ + p_binfo->status |= ((uint64_t) 1 << button_id); + } + else + { + /* ===== touch OFF result ===== */ + p_binfo->status &= ~(((uint64_t) 1 << button_id)); + } + } +} /* End of function touch_button_decode() */ + + #endif + + #if (CTSU_CFG_NUM_SELF_ELEMENTS != 0) + +/*********************************************************************************************************************** + * Function Name: touch_button_drift + * Description : Touch Button drift process + * Arguments : touch_button_info_t p_binfo : Pointer to Button Information structure + * : uint16_t value : Sensor value from CTSU + * : touch_mm_info_t button_mm_info : Button frq infomation structure + * Return Value : None + ***********************************************************************************************************************/ +void touch_button_drift (touch_button_info_t * p_binfo, uint16_t value, touch_mm_info_t button_mm_info) +{ + uint8_t button_id = button_mm_info.id; + uint8_t jmm_id = button_mm_info.mm_index; + uint8_t majority_mode_num; + if (button_mm_info.majority_mode == 1) + { + majority_mode_num = CTSU_MAJORITY_MODE_ELEMENTS; + } + else + { + majority_mode_num = 1; + } + + if (0 != p_binfo->drift_freq) + { + /* In case of doing drift correction being and moreover On/Off judgment result 1=OFF */ + if (0 == (p_binfo->status & (((uint64_t) 1 << button_id)))) + { + /* It is an addition for the drift correction average calculation */ + *(p_binfo->p_drift_buf + button_id * majority_mode_num + jmm_id) += value; + + /* Drift correction counter's being incremented */ + (*(p_binfo->p_drift_count + button_id * majority_mode_num + jmm_id))++; + + if (*(p_binfo->p_drift_count + button_id * majority_mode_num + jmm_id) >= p_binfo->drift_freq) + { + /* If reaching the correction number of times */ + *(p_binfo->p_reference + button_id * majority_mode_num + jmm_id) = + (uint16_t) (*(p_binfo->p_drift_buf + button_id * majority_mode_num + jmm_id) / + p_binfo->drift_freq); + + /* To REF of the average */ + *(p_binfo->p_drift_buf + button_id * majority_mode_num + jmm_id) = 0; + + /* Work clear */ + *(p_binfo->p_drift_count + button_id * majority_mode_num + jmm_id) = 0; + } + } + else + { + /* To REF of the average */ + *(p_binfo->p_drift_buf + button_id * majority_mode_num + jmm_id) = 0; + + /* Work clear */ + *(p_binfo->p_drift_count + button_id * majority_mode_num + jmm_id) = 0; + } + } +} + + #endif + + #if (CTSU_CFG_NUM_MUTUAL_ELEMENTS != 0) + +/*********************************************************************************************************************** + * Function Name: touch_button_mutual_drift + * Description : Touch Button drift process + * Arguments : touch_button_info_t p_binfo : Pointer to Button Information structure + * : int16_t value : Sensor value from CTSU + * : uint8_t button_id : Button ID + * Return Value : None + ***********************************************************************************************************************/ +void touch_button_mutual_drift (touch_button_info_t * p_binfo, int16_t value, touch_mm_info_t button_mm_info) +{ + uint32_t * p_drift_buf; + uint16_t * p_drift_count; + uint16_t * p_reference; + int32_t drift_mutual; + uint8_t button_id = button_mm_info.id; + uint8_t jmm_id = button_mm_info.mm_index; + uint8_t majority_mode_num; + if (button_mm_info.majority_mode == 1) + { + majority_mode_num = CTSU_MAJORITY_MODE_ELEMENTS; + } + else + { + majority_mode_num = 1; + } + + if (0 != p_binfo->drift_freq) + { + p_drift_buf = (p_binfo->p_drift_buf + button_id * majority_mode_num + jmm_id); + p_drift_count = (p_binfo->p_drift_count + button_id * majority_mode_num + jmm_id); + p_reference = (p_binfo->p_reference + button_id * majority_mode_num + jmm_id); + + /* In case of doing drift correction being and moreover On/Off judgment result 1=OFF */ + if (0 == (p_binfo->status & (((uint64_t) 1 << button_id)))) + { + /* It is an addition for the drift correction average calculation */ + drift_mutual = (int32_t) *p_drift_buf + (int32_t) value; + *p_drift_buf = (uint32_t) drift_mutual; + + /* Drift correction counter's being incremented */ + (*p_drift_count)++; + + if (*p_drift_count >= p_binfo->drift_freq) + { + /* If reaching the correction number of times */ + *p_reference = (uint16_t) (drift_mutual / p_binfo->drift_freq); + + /* To REF of the average */ + *p_drift_buf = 0; + + /* Work clear */ + *p_drift_count = 0; + } + } + else + { + /* To REF of the average */ + *p_drift_buf = 0; + + /* Work clear */ + *p_drift_count = 0; + } + } +} + + #endif +#endif + +#if (TOUCH_CFG_NUM_SLIDERS != 0) + +/*********************************************************************************************************************** + * Function Name: touch_slider_decode + * Description : Slider decode function + * Arguments : touch_slider_info_t p_sinfo : Pointer to Slider Information structure + * : uint16_t *slider_data : Pointer to Slider data array + * : uint8_t num_elements : Number of element on slider + * : uint8_t slider_id : Slider ID + * Return Value : None + ***********************************************************************************************************************/ +void touch_slider_decode (touch_slider_info_t * p_sinfo, uint16_t * slider_data, uint8_t num_elements, + uint8_t slider_id) +{ + uint8_t loop; + uint8_t max_data_num; + uint16_t d1; + uint16_t d2; + uint16_t d3; + uint16_t slider_rpos; + uint16_t resol_plus; + uint16_t dsum; + + if (num_elements < 3) + { + return; + } + + /* Search max data in slider */ + max_data_num = 0; + for (loop = 0; loop < (num_elements - 1); loop++) + { + if (slider_data[max_data_num] < slider_data[loop + 1]) + { + max_data_num = (uint8_t) (loop + 1); + } + } + + /* Array making for slider operation-------------*/ + /* | Maximum change CH_No -----> Array"0" */ + /* | Maximum change CH_No + 1 -> Array"2" */ + /* | Maximum change CH_No - 1 -> Array"1" */ + if (0 == max_data_num) + { + d1 = (uint16_t) (slider_data[0] - slider_data[2]); + d2 = (uint16_t) (slider_data[0] - slider_data[1]); + } + else if ((num_elements - 1) == max_data_num) + { + d1 = (uint16_t) (slider_data[num_elements - 1] - slider_data[num_elements - 2]); + d2 = (uint16_t) (slider_data[num_elements - 1] - slider_data[num_elements - 3]); + } + else + { + d1 = (uint16_t) (slider_data[max_data_num] - slider_data[max_data_num - 1]); + d2 = (uint16_t) (slider_data[max_data_num] - slider_data[max_data_num + 1]); + } + + dsum = (uint16_t) (d1 + d2); + + /* Constant decision for operation of angle of slider */ + /* Scale results to be 0-TOUCH_SLIDER_RESOLUTION */ + if (dsum > *(p_sinfo->p_threshold + slider_id)) + { + if (0 == d1) + { + d1 = 1; + } + + /* x : y = d1 : d2 */ + d3 = (uint16_t) (TOUCH_DECIMAL_POINT_PRECISION + ((d2 * TOUCH_DECIMAL_POINT_PRECISION) / d1)); + + slider_rpos = (uint16_t) (((TOUCH_DECIMAL_POINT_PRECISION * TOUCH_SLIDER_RESOLUTION) / d3) + + (TOUCH_SLIDER_RESOLUTION * max_data_num)); + + resol_plus = (uint16_t) (TOUCH_SLIDER_RESOLUTION * (num_elements - 1)); + + if (0 == slider_rpos) + { + slider_rpos = 1; + } + else if (slider_rpos >= resol_plus) + { + slider_rpos = (uint16_t) (((slider_rpos - resol_plus) * 2) + resol_plus); + if (slider_rpos > (TOUCH_SLIDER_RESOLUTION * num_elements)) + { + slider_rpos = TOUCH_SLIDER_RESOLUTION; + } + else + { + slider_rpos = (uint16_t) (slider_rpos / num_elements); + } + } + else if (slider_rpos <= TOUCH_SLIDER_RESOLUTION) + { + if (slider_rpos < (TOUCH_SLIDER_RESOLUTION / 2)) + { + slider_rpos = 1; + } + else + { + slider_rpos = (uint16_t) (slider_rpos - (TOUCH_SLIDER_RESOLUTION / 2)); + if (0 == slider_rpos) + { + slider_rpos = 1; + } + else + { + slider_rpos = (uint16_t) ((slider_rpos * 2) / num_elements); + } + } + } + else + { + slider_rpos = (uint16_t) (slider_rpos / num_elements); + } + } + else + { + slider_rpos = TOUCH_OFF_VALUE; + } + + *(p_sinfo->p_position + slider_id) = slider_rpos; +} /* End of function touch_slider_decode() */ + +#endif // SLIDER_USE + +#if (TOUCH_CFG_NUM_WHEELS != 0) + +/*********************************************************************************************************************** + * Function Name: touch_wheel_decode + * Description : Wheel Decode function + * Arguments : touch_wheel_info_t p_sinfo : Pointer to Wheel Information structure + * : uint16_t *wheel_data : Pointer to Wheel data array + * : uint8_t num_elements : Number of element on wheel + * : uint8_t wheel_id : Wheel ID + * Return Value : None + ***********************************************************************************************************************/ +void touch_wheel_decode (touch_wheel_info_t * p_winfo, uint16_t * wheel_data, uint8_t num_elements, uint8_t wheel_id) +{ + uint8_t loop; + uint8_t max_data_num; + uint16_t d1; + uint16_t d2; + uint16_t d3; + uint16_t wheel_rpos; + uint16_t dsum; + uint16_t unit; + + if (num_elements < 3) + { + return; + } + + /* Search max data in slider */ + max_data_num = 0; + for (loop = 0; loop < (num_elements - 1); loop++) + { + if (wheel_data[max_data_num] < wheel_data[loop + 1]) + { + max_data_num = (uint8_t) (loop + 1); + } + } + + /* Array making for wheel operation */ + /* Maximum change CH_No -----> Array"0" */ + /* Maximum change CH_No + 1 -> Array"2" */ + /* Maximum change CH_No - 1 -> Array"1" */ + if (0 == max_data_num) + { + d1 = (uint16_t) (wheel_data[0] - wheel_data[num_elements - 1]); + d2 = (uint16_t) (wheel_data[0] - wheel_data[1]); + } + else if ((num_elements - 1) == max_data_num) + { + d1 = (uint16_t) (wheel_data[num_elements - 1] - wheel_data[num_elements - 2]); + d2 = (uint16_t) (wheel_data[num_elements - 1] - wheel_data[0]); + } + else + { + d1 = (uint16_t) (wheel_data[max_data_num] - wheel_data[max_data_num - 1]); + d2 = (uint16_t) (wheel_data[max_data_num] - wheel_data[max_data_num + 1]); + } + + dsum = (uint16_t) (d1 + d2); + + if (0 == d1) + { + d1 = 1; + } + + /* Constant decision for operation of angle of wheel */ + if (dsum > *(p_winfo->p_threshold + wheel_id)) + { + d3 = (uint16_t) (TOUCH_DECIMAL_POINT_PRECISION + ((d2 * TOUCH_DECIMAL_POINT_PRECISION) / d1)); + + unit = (uint16_t) (TOUCH_WHEEL_RESOLUTION / num_elements); + wheel_rpos = (uint16_t) (((unit * TOUCH_DECIMAL_POINT_PRECISION) / d3) + (unit * max_data_num)); + + /* Angle division output */ + /* diff_angle_ch = 0 -> 359 ------ diff_angle_ch output 1 to 360 */ + if (0 == wheel_rpos) + { + wheel_rpos = TOUCH_WHEEL_RESOLUTION; + } + else if ((TOUCH_WHEEL_RESOLUTION + 1) < wheel_rpos) + { + wheel_rpos = 1; + } + else + { + /* Do Nothing */ + } + } + else + { + wheel_rpos = TOUCH_OFF_VALUE; + } + + *(p_winfo->p_position + wheel_id) = wheel_rpos; +} /* End of function touch_wheel_decode() */ + +#endif + +#if (TOUCH_CFG_PAD_ENABLE) + +/*********************************************************************************************************************** + * Function Name: touch_pad_decode + * Description : Pad Decode function + * Arguments : touch_pad_info_t p_pinfo : Pointer to Pad Information structure + * : uint16_t *wheel_data : Pointer to Wheel data array + * : uint8_t num_elements : Number of element on wheel + * : uint8_t wheel_id : Wheel ID + * Return Value : None + ***********************************************************************************************************************/ +void touch_pad_decode (touch_pad_info_t * p_pinfo, uint8_t num_x, uint8_t num_y, uint8_t max_touch) +{ + uint16_t i; + uint16_t j; + uint8_t loop; + uint16_t pitch_x; + uint16_t pitch_y; + uint16_t element_num; + int32_t max_diff; + uint16_t max_x; + uint16_t max_y; + int32_t x_parameter; + int32_t y_parameter; + int16_t heat_map[TOUCH_MAP_X * TOUCH_MAP_Y]; + uint8_t use_map[TOUCH_MAP_X * TOUCH_MAP_Y]; + int32_t tmp_heat; + int32_t tmp_x1; /* Work for calculating x parameter1 */ + int32_t tmp_x2; /* Work for calculating x parameter2 */ + int32_t tmp_x3; /* Work for calculating x parameter3 */ + int32_t tmp_x4; /* Work for calculating x parameter4 */ + int32_t tmp_y1; /* Work for calculating y parameter1 */ + int32_t tmp_y2; /* Work for calculating y parameter2 */ + int32_t tmp_y3; /* Work for calculating y parameter3 */ + int32_t tmp_y4; /* Work for calculating y parameter4 */ + + element_num = (uint16_t) (num_x * num_y); + pitch_x = (uint16_t) (*(p_pinfo->p_rx_pixel) / num_x); + pitch_y = (uint16_t) (*(p_pinfo->p_tx_pixel) / num_y); + + /* Get coordinate data for the Max touch */ + for (loop = 0; loop < max_touch; loop++) + { + /* initialize heat-map variables */ + max_diff = 0; + max_y = 0; + max_x = 0; + + /* Clear heat map ,and initial use map */ + for (i = 0; i < (TOUCH_MAP_X * TOUCH_MAP_Y); i++) + { + heat_map[i] = 0; + use_map[i] = 1; + } + + /* Maximum value search */ + for (j = 0; j < num_x; j++) + { + for (i = 0; i < num_y; i++) + { + if (g_touch_pad_buf[element_num + j + (i * num_x)] > max_diff) + { + max_diff = g_touch_pad_buf[element_num + j + (i * num_x)]; + max_y = i; + max_x = j; + } + } + } + + /* touch check */ + if (max_diff >= *(p_pinfo->p_threshold)) + { + /* make use map */ + if (0 == max_y) + { + /* If map position is Top. */ + use_map[0] = 0; + use_map[1] = 0; + use_map[2] = 0; + } + else if ((num_y - 1) == max_y) + { + /* If map position is Bottom. */ + use_map[6] = 0; + use_map[7] = 0; + use_map[8] = 0; + } + else + { + } + + if (0 == max_x) + { + /* If map position is Left. */ + use_map[0] = 0; + use_map[3] = 0; + use_map[6] = 0; + } + else if ((num_x - 1) == max_x) + { + /* If map position is Right. */ + use_map[2] = 0; + use_map[5] = 0; + use_map[8] = 0; + } + else + { + } + + /* make heat mapping */ + for (i = 0; i < TOUCH_MAP_X; i++) + { + for (j = 0; j < TOUCH_MAP_Y; j++) + { + if (use_map[j + (i * TOUCH_MAP_X)]) + { + heat_map[j + (i * TOUCH_MAP_X)] = + g_touch_pad_buf[element_num + (max_x - 1 + j) + (max_y - 1 + i) * num_x]; + } + } + } + + /* get x value */ + /* Calculate right + bottom value. (x1/y1) */ + tmp_heat = heat_map[5] + heat_map[7]; + if (tmp_heat == 0) + { + /* When dividing by zero, set the calculation result to zero */ + tmp_x1 = 0; /* x parameter1 */ + tmp_y1 = 0; /* y parameter1 */ + } + else + { + tmp_x1 = (heat_map[8] * heat_map[5]) / tmp_heat; /* x parameter1 */ + tmp_y1 = (heat_map[8] * heat_map[7]) / tmp_heat; /* y parameter1 */ + } + + /* Calculate right + up value. (x2/y4) */ + tmp_heat = heat_map[5] + heat_map[1]; + if (tmp_heat == 0) + { + /* When dividing by zero, set the calculation result to zero */ + tmp_x2 = 0; /* x parameter2 */ + tmp_y4 = 0; /* y parameter4 */ + } + else + { + tmp_x2 = (heat_map[2] * heat_map[5]) / tmp_heat; /* x parameter2 */ + tmp_y4 = (heat_map[2] * heat_map[1]) / tmp_heat; /* y parameter4 */ + } + + /* Calculate left + up value. (x3/y3) */ + tmp_heat = heat_map[3] + heat_map[1]; + if (tmp_heat == 0) + { + /* When dividing by zero, set the calculation result to zero */ + tmp_x3 = 0; /* x parameter3 */ + tmp_y3 = 0; /* y parameter3 */ + } + else + { + tmp_x3 = (heat_map[0] * heat_map[3]) / tmp_heat; /* x parameter3 */ + tmp_y3 = (heat_map[0] * heat_map[1]) / tmp_heat; /* y parameter3 */ + } + + /* Calculate left + down value. (x4/y2) */ + tmp_heat = heat_map[3] + heat_map[7]; + if (tmp_heat == 0) + { + /* When dividing by zero, set the calculation result to zero */ + tmp_x4 = 0; /* x parameter4 */ + tmp_y2 = 0; /* y parameter2 */ + } + else + { + tmp_x4 = (heat_map[6] * heat_map[3]) / tmp_heat; /* x parameter4 */ + tmp_y2 = (heat_map[6] * heat_map[7]) / tmp_heat; /* y parameter2 */ + } + + if (heat_map[4] == 0) + { + x_parameter = 0; + } + else + { + /* x coordinate value calculation*/ + x_parameter = ((pitch_x / 2) * + (heat_map[5] + tmp_x1 + tmp_x2 - + heat_map[3] - tmp_x3 - tmp_x4) / + heat_map[4]); + } + + /* Fit to pitch */ + if (x_parameter > pitch_x / 2) + { + x_parameter = pitch_x / 2; + } + else if (x_parameter < -(pitch_x / 2)) + { + x_parameter = -(pitch_x / 2); + } + else + { + /* no operation */ + } + + /* Coordinate x value based on pitch */ + *(p_pinfo->p_rx_coordinate + loop) = + (uint16_t) ((pitch_x * (max_x + 1) - pitch_x / 2) + x_parameter); + + /* get y value */ + if (heat_map[4] == 0) + { + y_parameter = 0; + } + else + { + /* y coordinate value calculation*/ + y_parameter = ((pitch_y / 2) * + (heat_map[7] + tmp_y1 + tmp_y2 - + heat_map[1] - tmp_y3 - tmp_y4) / + heat_map[4]); + } + + /* Fit to pitch */ + if (y_parameter > pitch_y / 2) + { + y_parameter = pitch_y / 2; + } + else if (y_parameter < -(pitch_y / 2)) + { + y_parameter = -(pitch_y / 2); + } + else + { + /* no operation */ + } + + /* Coordinate y value based on pitch */ + *(p_pinfo->p_tx_coordinate + loop) = + (uint16_t) ((pitch_y * (max_y + 1) - pitch_y / 2) + y_parameter); + + /* If touching ,then counter increment */ + (*(p_pinfo->p_num_touch))++; + + /* Clear the processed heat map area */ + for (i = 0; i < TOUCH_MAP_X; i++) + { + for (j = 0; j < TOUCH_MAP_Y; j++) + { + if (use_map[i * TOUCH_MAP_X + j]) + { + g_touch_pad_buf[element_num + (max_x - 1 + j) + (max_y - 1 + i) * num_x] = 0; + } + } + } + } + else + { + *(p_pinfo->p_rx_coordinate + loop) = TOUCH_OFF_VALUE; + *(p_pinfo->p_tx_coordinate + loop) = TOUCH_OFF_VALUE; + } + } +} + +#endif + +#if ((TOUCH_CFG_MONITOR_ENABLE && (TOUCH_CFG_UART_MONITOR_SUPPORT == 1)) || TOUCH_CFG_UART_TUNING_SUPPORT == 1) + +/*********************************************************************************************************************** + * Function Name: user_uart_qe_callback + * Description : QE UART callback function + * Arguments : uart_callback_args_t * p_args : callback parameters + * Return Value : None + ***********************************************************************************************************************/ +void touch_uart_callback (void *pargs) +{ + sci_cb_args_t *args; + uint16_t index; + + #if (TOUCH_CFG_MONITOR_ENABLE && (TOUCH_CFG_UART_MONITOR_SUPPORT == 1)) + ctsu_instance_ctrl_t * p_ctsu_ctrl; + uint16_t write_data; + uint16_t ctsuso; + #if (BSP_FEATURE_CTSU_VERSION == 2) + uint16_t ctsuso2; + uint16_t ctsuso3; + #endif + uint8_t ctsusdpa; + uint8_t ctsusnum; + #if (BSP_FEATURE_CTSU_VERSION == 2) + uint32_t * p_ctsuso; + uint32_t * p_ctsuso2; + uint32_t * p_ctsuso3; + uint32_t * p_ctsusdpa; + uint32_t * p_ctsusnum; + #endif + #if (BSP_FEATURE_CTSU_VERSION == 1) + uint16_t * p_ctsuso; + uint16_t * p_ctsusdpa; + uint16_t * p_ctsusnum; + #endif + uint8_t num; + uint8_t element; + int16_t elem_num; + #if (CTSU_CFG_MAJORITY_MODE & CTSU_JUDGEMENT_MAJORITY_MODE) + elem_num = CTSU_MAJORITY_MODE_ELEMENTS; + #else + elem_num = 1; + #endif + #endif + + #if (TOUCH_CFG_UART_TUNING_SUPPORT == 1) + touch_instance_ctrl_t * p_instance_ctrl = gp_touch_isr_context; + uint16_t element_id; + uint8_t * port_adress_8; + uint16_t * port_adress_16; + uint32_t * port_adress_32; + uint8_t write_byte; + #endif + + args = (sci_cb_args_t *)pargs; + + if (args->event == SCI_EVT_TEI) + { + g_touch_uart_transmit_flag = 0; + + return; + } + + if (args->event == SCI_EVT_RX_CHAR) + { + g_receive_intterupt_count++; + } + +#if ((TOUCH_CFG_MONITOR_ENABLE &&(TOUCH_CFG_UART_MONITOR_SUPPORT == 1)) || (TOUCH_CFG_UART_TUNING_SUPPORT == 1)) + + if ((args->event == SCI_EVT_RX_CHAR) && (TOUCH_TUNING_COMMAND_BUF_NUM == g_receive_intterupt_count)) + { + /* Restart reception */ + R_SCI_Receive(g_touch_uart_control, &g_touch_uart_rx_buf[0] ,TOUCH_TUNING_COMMAND_BUF_NUM); + + g_touch_uart_command_top = g_touch_uart_rx_buf[1] >> 4; + + #if (TOUCH_CFG_UART_TUNING_SUPPORT == 1) + /* Reconnection due to UART baud rate difference. */ + if (TOUCH_TUNING_HEADER != g_touch_uart_rx_buf[0]) + { + g_touch_uart_baudrate_err_flag = 1; + g_receive_intterupt_count = 0; + } + #endif + return; + } + +#endif + +#if (TOUCH_CFG_MONITOR_ENABLE &&(TOUCH_CFG_UART_MONITOR_SUPPORT == 1)) + if ((g_touch_uart_command_top == TOUCH_UART_COMMAND_TOP_NUM) && + (args->event == SCI_EVT_RX_CHAR) && + (TOUCH_UART_RECIEVE_BUF_SIZE == g_receive_intterupt_count)) + { + /* Restart reception */ + R_SCI_Receive(g_touch_uart_control, + &g_touch_uart_rx_buf[TOUCH_TUNING_COMMAND_BUF_NUM] , + (TOUCH_UART_RECIEVE_BUF_SIZE - TOUCH_TUNING_COMMAND_BUF_NUM)); + g_receive_intterupt_count = 0; + + if (1 == g_touch_uart_transmit_flag) + { + g_touch_uart_transmit_flag = 0; + } + + /* Set fixed header */ + g_touch_monitor_buf[0] = TOUCH_UART_HEADER; + + /* Create response command */ + if ((g_touch_uart_rx_buf[0] == TOUCH_UART_HEADER) && (g_touch_uart_rx_buf[12] == TOUCH_UART_FOOTER)) + { + g_touch_monitor_buf[1] = g_touch_uart_rx_buf[1] | TOUCH_UART_RESPONSE_BIT; + } + else + { + g_touch_monitor_buf[1] = g_touch_uart_rx_buf[1] | TOUCH_UART_RESPONSE_ERROR_BIT; + g_touch_uart_rx_buf[1] = g_touch_monitor_buf[1]; + } + + /* Set same ID */ + g_touch_monitor_buf[4] = g_touch_uart_rx_buf[4]; + + /* Set index, Fixed data up to index 4. After index 5, it depends on the command. */ + index = 5; + + if (g_touch_uart_rx_buf[1] == TOUCH_UART_COMMAND_START) + { + num = g_touch_uart_rx_buf[5]; + if (0 == (((uint32_t) 1 << num) & g_touch_uart_open_bitmap)) + { + /* Unopened methods generate errors */ + g_touch_monitor_buf[1] = g_touch_uart_rx_buf[1] | TOUCH_UART_RESPONSE_ERROR_BIT; + } + else + { + g_touch_uart_monitor_num = num; + } + + g_touch_monitor_buf[index++] = (uint8_t) g_touch_uart_open_bitmap; + g_touch_monitor_buf[index++] = (uint8_t) (g_touch_uart_open_bitmap >> 8); + g_touch_monitor_buf[index++] = (uint8_t) (g_touch_uart_open_bitmap >> 16); + g_touch_monitor_buf[index++] = (uint8_t) (g_touch_uart_open_bitmap >> 24); + } + else if (g_touch_uart_rx_buf[1] == TOUCH_UART_COMMAND_STOP) + { + g_touch_uart_monitor_num = TOUCH_UART_MONITOR_NONE; + } + else if ((g_touch_uart_rx_buf[1] == TOUCH_UART_COMMAND_READ) || + (g_touch_uart_rx_buf[1] == TOUCH_UART_COMMAND_WRITE) || + (g_touch_uart_rx_buf[1] == TOUCH_UART_COMMAND_THREE_FREQ_READ)) + + { + num = g_touch_uart_rx_buf[5]; + element = g_touch_uart_rx_buf[7]; + + if (0 == (((uint32_t) 1 << num) & g_touch_uart_open_bitmap)) + { + /* Unopened methods generate errors */ + g_touch_monitor_buf[1] = g_touch_uart_rx_buf[1] | TOUCH_UART_RESPONSE_ERROR_BIT; + } + else + { + p_ctsu_ctrl = (ctsu_instance_ctrl_t *) gp_touch_ctrl_list[num]->p_ctsu_instance->p_ctrl; + #if (BSP_FEATURE_CTSU_VERSION == 2) + p_ctsuso = &(p_ctsu_ctrl->p_ctsuwr[element].ctsuso); + if (g_touch_uart_rx_buf[1] == TOUCH_UART_COMMAND_THREE_FREQ_READ) + { + p_ctsuso2 = &(p_ctsu_ctrl->p_ctsuwr[element + 1].ctsuso); + p_ctsuso3 = &(p_ctsu_ctrl->p_ctsuwr[element + 2].ctsuso); + } + + p_ctsusnum = &(p_ctsu_ctrl->p_ctsuwr[element].ctsuso); + p_ctsusdpa = &(p_ctsu_ctrl->p_ctsuwr[element].ctsuso); + #endif + #if (BSP_FEATURE_CTSU_VERSION == 1) + p_ctsuso = &(p_ctsu_ctrl->p_ctsuwr[element].ctsuso0); + p_ctsusnum = &(p_ctsu_ctrl->p_ctsuwr[element].ctsuso0); + p_ctsusdpa = &(p_ctsu_ctrl->p_ctsuwr[element].ctsuso1); + #endif + ctsuso = (uint16_t) (*p_ctsuso & TOUCH_UART_CTSUSO_MASK); + #if (BSP_FEATURE_CTSU_VERSION == 2) + if (g_touch_uart_rx_buf[1] == TOUCH_UART_COMMAND_THREE_FREQ_READ) + { + ctsuso2 = (uint16_t) (*p_ctsuso2 & TOUCH_UART_CTSUSO_MASK); + ctsuso3 = (uint16_t) (*p_ctsuso3 & TOUCH_UART_CTSUSO_MASK); + } + + #endif + ctsusnum = (uint8_t) ((*p_ctsusnum >> TOUCH_UART_CTSUSNUM_SHIFT) & TOUCH_UART_CTSUSNUM_MASK); + ctsusdpa = (uint8_t) ((*p_ctsusdpa >> TOUCH_UART_CTSUSDPA_SHIFT) & TOUCH_UART_CTSUSDPA_MASK); + if ((g_touch_uart_rx_buf[1] == TOUCH_UART_COMMAND_THREE_FREQ_READ) || + (g_touch_uart_rx_buf[1] == TOUCH_UART_COMMAND_READ)) + { + /* Drift Correction */ + g_touch_monitor_buf[index++] = (uint8_t) (gp_touch_ctrl_list[num]->binfo.drift_freq); + g_touch_monitor_buf[index++] = (uint8_t) (gp_touch_ctrl_list[num]->binfo.drift_freq >> 8); + + /* Long Press Cancel */ + g_touch_monitor_buf[index++] = (uint8_t) (gp_touch_ctrl_list[num]->binfo.cancel_freq); + g_touch_monitor_buf[index++] = (uint8_t) (gp_touch_ctrl_list[num]->binfo.cancel_freq >> 8); + + /* Positive Noise Filter */ + g_touch_monitor_buf[index++] = gp_touch_ctrl_list[num]->binfo.on_freq; + g_touch_monitor_buf[index++] = 0x00; + + /* Negative Noise Filter */ + g_touch_monitor_buf[index++] = gp_touch_ctrl_list[num]->binfo.off_freq; + g_touch_monitor_buf[index++] = 0x00; + + /* Depth of Moving Average Filter */ + g_touch_monitor_buf[index++] = (uint8_t) (p_ctsu_ctrl->num_moving_average); + g_touch_monitor_buf[index++] = (uint8_t) (p_ctsu_ctrl->num_moving_average >> 8); + + /* CTSUSO (element freq1) */ + g_touch_monitor_buf[index++] = (uint8_t) (ctsuso); + g_touch_monitor_buf[index++] = (uint8_t) (ctsuso >> 8); +#if (BSP_FEATURE_CTSU_VERSION == 2) + if (g_touch_uart_rx_buf[1] == TOUCH_UART_COMMAND_THREE_FREQ_READ) + { + /* CTSUSO (element freq2) */ + g_touch_monitor_buf[index++] = (uint8_t) (ctsuso2); + g_touch_monitor_buf[index++] = (uint8_t) (ctsuso2 >> 8); + + /* CTSUSO (element freq3) */ + g_touch_monitor_buf[index++] = (uint8_t) (ctsuso3); + g_touch_monitor_buf[index++] = (uint8_t) (ctsuso3 >> 8); + } + + #endif + + /* CTSUSNUM (element) */ + g_touch_monitor_buf[index++] = ctsusnum; + + /* CTSUSDPA (element) */ + g_touch_monitor_buf[index++] = ctsusdpa; + } + else + { + /* Write Action */ + write_data = + (uint16_t) (((uint16_t) g_touch_uart_rx_buf[9] << 8) | (uint16_t) g_touch_uart_rx_buf[8]); + switch (g_touch_uart_rx_buf[6]) /* data type */ + { + case TOUCH_UART_WRITE_DRIFT: + { + gp_touch_ctrl_list[num]->binfo.drift_freq = write_data; + break; + } + + case TOUCH_UART_WRITE_CANCEL: + { + gp_touch_ctrl_list[num]->binfo.cancel_freq = write_data; + break; + } + + case TOUCH_UART_WRITE_POSITIVE: + { + gp_touch_ctrl_list[num]->binfo.on_freq = (uint8_t) write_data; + break; + } + + case TOUCH_UART_WRITE_NEGATIVE: + { + gp_touch_ctrl_list[num]->binfo.off_freq = (uint8_t) write_data; + break; + } + + case TOUCH_UART_WRITE_MOVING_AVG: + { + p_ctsu_ctrl->num_moving_average = write_data; + break; + } + + case TOUCH_UART_WRITE_THRESHOLD: + { + gp_touch_ctrl_list[num]->binfo.p_threshold[element * elem_num] = write_data; + break; + } + + case TOUCH_UART_WRITE_HYSTERESIS: + { + gp_touch_ctrl_list[num]->binfo.p_hysteresis[element * elem_num] = write_data; + break; + } + + case TOUCH_UART_WRITE_THRESH_SLDR: + { + gp_touch_ctrl_list[num]->sinfo.p_threshold[element * elem_num] = write_data; + break; + } + + case TOUCH_UART_WRITE_THRESH_WHEL: + { + gp_touch_ctrl_list[num]->winfo.p_threshold[element * elem_num] = write_data; + break; + } + + case TOUCH_UART_WRITE_CTSUSO: + { + #if (BSP_FEATURE_CTSU_VERSION == 2) + *p_ctsuso &= (uint32_t) (~TOUCH_UART_CTSUSO_MASK); + *p_ctsuso |= write_data; + #endif + #if (BSP_FEATURE_CTSU_VERSION == 1) + *p_ctsuso &= (uint16_t) (~TOUCH_UART_CTSUSO_MASK); + *p_ctsuso |= write_data; + #endif + break; + } + + case TOUCH_UART_WRITE_CTSUSNUM: + { + #if (BSP_FEATURE_CTSU_VERSION == 2) + *p_ctsusnum &= (~((uint32_t) TOUCH_UART_CTSUSNUM_MASK << TOUCH_UART_CTSUSNUM_SHIFT)); + *p_ctsusnum |= ((uint32_t) write_data << TOUCH_UART_CTSUSNUM_SHIFT); + #endif + #if (BSP_FEATURE_CTSU_VERSION == 1) + *p_ctsusnum &= (uint16_t) (~(TOUCH_UART_CTSUSNUM_MASK << TOUCH_UART_CTSUSNUM_SHIFT)); + *p_ctsusnum |= (uint16_t) (write_data << TOUCH_UART_CTSUSNUM_SHIFT); + #endif + break; + } + + case TOUCH_UART_WRITE_CTSUSDPA: + { + #if (BSP_FEATURE_CTSU_VERSION == 2) + *p_ctsusdpa &= (~((uint32_t) TOUCH_UART_CTSUSDPA_MASK << TOUCH_UART_CTSUSDPA_SHIFT)); + *p_ctsusdpa |= ((uint32_t) write_data << TOUCH_UART_CTSUSDPA_SHIFT); + #endif + #if (BSP_FEATURE_CTSU_VERSION == 1) + *p_ctsusdpa &= (uint16_t) (~(TOUCH_UART_CTSUSDPA_MASK << TOUCH_UART_CTSUSDPA_SHIFT)); + *p_ctsusdpa |= (uint16_t) (write_data << TOUCH_UART_CTSUSDPA_SHIFT); + #endif + break; + } + + #if (TOUCH_CFG_PAD_ENABLE) + + /* write command */ + case TOUCH_UART_WRTIE_PAD_THRESHOLD: + { + *gp_touch_ctrl_list[num]->pinfo.p_threshold = write_data; + break; + } + + case TOUCH_UART_WRTIE_PAD_RX_PIXEL: + { + *gp_touch_ctrl_list[num]->pinfo.p_rx_pixel = write_data; + break; + } + + case TOUCH_UART_WRTIE_PAD_TX_PIXEL: + { + *gp_touch_ctrl_list[num]->pinfo.p_tx_pixel = write_data; + break; + } + + case TOUCH_UART_WRTIE_PAD_MAX_TOUCH: + { + *gp_touch_ctrl_list[num]->pinfo.p_max_touch = (uint8_t) write_data; + break; + } + + case TOUCH_UART_WRTIE_PAD_DRIFT: + { + gp_touch_ctrl_list[num]->pinfo.num_drift = (uint8_t) write_data; + break; + } + #endif /* TOUCH_CFG_PAD_ENABLE */ + #if (BSP_FEATURE_CTSU_VERSION == 2) + #if (CTSU_CFG_MAJORITY_MODE & CTSU_JUDGEMENT_MAJORITY_MODE) + case TOUCH_UART_WRITE_THRESHOLD2: + { + gp_touch_ctrl_list[num]->binfo.p_threshold[(element * elem_num) + 1] = + write_data; + break; + } + + case TOUCH_UART_WRITE_HYSTERESIS2: + { + gp_touch_ctrl_list[num]->binfo.p_hysteresis[(element * elem_num) + 1] = + write_data; + break; + } + + case TOUCH_UART_WRITE_THRESHOLD3: + { + gp_touch_ctrl_list[num]->binfo.p_threshold[(element * elem_num) + 2] = + write_data; + break; + } + + case TOUCH_UART_WRITE_HYSTERESIS3: + { + gp_touch_ctrl_list[num]->binfo.p_hysteresis[(element * elem_num) + 2] = + write_data; + break; + } + #endif + #endif + default: + { + break; + } + } + } + } + } + else if (g_touch_uart_rx_buf[1] == TOUCH_UART_COMMAND_VERSION) + { + g_touch_monitor_buf[index++] = TOUCH_UART_VERSION_MINOR; + g_touch_monitor_buf[index++] = TOUCH_UART_VERSION_MAJOR; + } + else + { + } + + /* Set fixed footer */ + g_touch_monitor_buf[index++] = TOUCH_UART_FOOTER; + + /* Set data size */ + g_touch_monitor_buf[2] = (uint8_t) index; + g_touch_monitor_buf[3] = (uint8_t) (index >> 8); + + /* Start transmission */ + g_touch_uart_transmit_flag = 1; + R_SCI_Send(g_touch_uart_control, &g_touch_monitor_buf[0] ,index); + return; + } + #endif + +#if (TOUCH_CFG_UART_TUNING_SUPPORT == 1) + if ((g_touch_uart_command_top == TOUCH_TUNING_COMMAND_TOP_NUM) && + (args->event == SCI_EVT_RX_CHAR) && + (TOUCH_TUNING_RECIEVE_BUF_SIZE == g_receive_intterupt_count)) + { + /* Restart reception */ + R_SCI_Receive(g_touch_uart_control, + &g_touch_uart_rx_buf[TOUCH_TUNING_COMMAND_BUF_NUM] , + (TOUCH_TUNING_RECIEVE_BUF_SIZE - TOUCH_TUNING_COMMAND_BUF_NUM)); + g_receive_intterupt_count = 0; + + if (1 == g_touch_uart_transmit_flag) + { + g_touch_uart_transmit_flag = 0; + } + + /* Set fixed header */ + g_touch_tuning_tx_buf[0] = TOUCH_TUNING_HEADER; + + /* Create response command */ + if ((g_touch_uart_rx_buf[0] == TOUCH_TUNING_HEADER) && (g_touch_uart_rx_buf[14] == TOUCH_TUNING_FOOTER)) + { + g_touch_tuning_tx_buf[1] = (uint8_t) (g_touch_uart_rx_buf[1] + TOUCH_TUNING_RESPONSE_BIT); + } + else + { + g_touch_tuning_tx_buf[1] = (uint8_t) (g_touch_uart_rx_buf[1] + TOUCH_TUNING_RESPONSE_ERROR_BIT); + g_touch_uart_rx_buf[1] = g_touch_tuning_tx_buf[1]; + } + + /* Set same ID */ + g_touch_tuning_tx_buf[4] = g_touch_uart_rx_buf[4]; + + /* Set index, Fixed data up to index 4. After index 5, it depends on the command. */ + index = 5; + + /* Run command */ + if (g_touch_uart_rx_buf[1] == TOUCH_TUNING_COMMAND_START) + { + /* Data length */ + g_touch_tuning_tx_buf[2] = 0x6; + g_touch_tuning_tx_buf[3] = 0x0; + p_instance_ctrl->serial_tuning_enable = true; + ctsu_instance_ctrl_t * p_ctsu_instance_ctrl = + (ctsu_instance_ctrl_t *) p_instance_ctrl->p_ctsu_instance->p_ctrl; + p_ctsu_instance_ctrl->serial_tuning_enable = true; + g_touch_tuning_mode = TOUCH_TUNING_MODE_START; + } + else if (g_touch_uart_rx_buf[1] == TOUCH_TUNING_COMMAND_STOP) + { + /* Data length */ + g_touch_tuning_tx_buf[2] = 0x6; + g_touch_tuning_tx_buf[3] = 0x0; + + p_instance_ctrl->serial_tuning_enable = false; + ctsu_instance_ctrl_t * p_ctsu_instance_ctrl = + (ctsu_instance_ctrl_t *) p_instance_ctrl->p_ctsu_instance->p_ctrl; + p_ctsu_instance_ctrl->serial_tuning_enable = false; + g_touch_tuning_mode = TOUCH_TUNING_MODE_FINISH; + } + else if (g_touch_uart_rx_buf[1] == TOUCH_TUNING_COMMAND_VARIABLE_WRITE) + { + /* Data length */ + g_touch_tuning_tx_buf[2] = 0x6; + g_touch_tuning_tx_buf[3] = 0x0; + + switch (g_touch_uart_rx_buf[5]) /* data type */ + { + case TOUCH_TUNING_VARIABLE_MCU_GROUP: + { + touch_tuning_get8((uint8_t *) &g_touch_tuning_mcu_group, 7); + break; + } + + case TOUCH_TUNING_VARIABLE_PCLKB_FREQUENCY: + { + touch_tuning_get32((uint32_t *) &g_touch_tuning_pclkb_frequency, 7); + break; + } + + case TOUCH_TUNING_VARIABLE_TUNING_MODE: + { + touch_tuning_get8((uint8_t *) &g_touch_tuning_mode, 7); + break; + } + + case TOUCH_TUNING_VARIABLE_CTSU_STATE: + { + touch_tuning_get8((uint8_t *) &g_touch_tuning_state, 7); + break; + } + + case TOUCH_TUNING_VARIABLE_CTSU_SCAN_MODE: + { + touch_tuning_get8((uint8_t *) &g_touch_tuning_scan_mode, 7); + break; + } + + case TOUCH_TUNING_VARIABLE_TS_CHAC_MASK_BOTTOM: + { + touch_tuning_get32(&g_touch_tuning_ts_chac_mask.mska, 7); + break; + } + + case TOUCH_TUNING_VARIABLE_TS_CHAC_MASK_TOP: + { + touch_tuning_get32(&g_touch_tuning_ts_chac_mask.mskb, 7); + break; + } + + case TOUCH_TUNING_VARIABLE_TS_CHTRC_MASK_BOTTOM: + { + touch_tuning_get32(&g_touch_tuning_ts_chtrc_mask.mska, 7); + break; + } + + case TOUCH_TUNING_VARIABLE_TS_CHTRC_MASK_TOP: + { + touch_tuning_get32(&g_touch_tuning_ts_chtrc_mask.mskb, 7); + break; + } + + case TOUCH_TUNING_VARIABLE_QE_TXVSEL: + { + touch_tuning_get8((uint8_t *) &g_touch_tuning_qe_txvsel, 7); + break; + } + + case TOUCH_TUNING_VARIABLE_QE_ATUNE: + { + touch_tuning_get8((uint8_t *) &g_touch_tuning_qe_atune, 7); + break; + } + + case TOUCH_TUNING_VARIABLE_QE_POSEL: + { + touch_tuning_get8((uint8_t *) &g_touch_tuning_qe_posel, 7); + break; + } + + case TOUCH_TUNING_VARIABLE_ERR_EVENT: + { + touch_tuning_get8((uint8_t *) &g_touch_tuning_err_event, 7); + break; + } + + case TOUCH_TUNING_VARIABLE_CTSU_ICO_DATA_PRI: + { + element_id = g_touch_uart_rx_buf[6]; + if (TOUCH_TUNING_MODE_MEASURE_PHASE1 == g_touch_tuning_mode_save) + { + touch_tuning_get16(&g_touch_tuning_ico_data[element_id], 7); + } + else + { + if (TOUCH_TUNING_SCAN_SELF == g_touch_tuning_scan_mode) + { + touch_tuning_get16(&g_touch_tuning_ico_data[element_id], 7); + } + else if (TOUCH_TUNING_SCAN_MUTUAL == g_touch_tuning_scan_mode) + { + touch_tuning_get16(&g_touch_tuning_ico_data[element_id * 2], 7); + } + + #if (BSP_FEATURE_CTSU_VERSION == 2) + #if (CTSU_CFG_NUM_CFC != 0) + else if (TOUCH_TUNING_SCAN_MUTUAL_CFC == g_touch_tuning_scan_mode) + { + touch_tuning_get16(&g_touch_tuning_ico_data[element_id * 2], 7); + } + #endif + #endif + else + { + } + } + + break; + } + + case TOUCH_TUNING_VARIABLE_CTSU_ICO_DATA_SND: + { + element_id = g_touch_uart_rx_buf[6]; + touch_tuning_get16(&g_touch_tuning_ico_data[element_id * 2 + 1], 7); + break; + } + + #if (CTSU_CFG_NUM_MUTUAL_ELEMENTS != 0) + case TOUCH_TUNING_VARIABLE_CTSU_MUTUAL_DATA: + { + element_id = g_touch_uart_rx_buf[6]; + touch_tuning_get16(&g_touch_tuning_mutual_data[element_id], 7); + break; + } + #endif + + #if (BSP_FEATURE_CTSU_VERSION == 2) + case TOUCH_TUNING_VARIABLE_ELEMENT_CFGS_SO: + { + element_id = g_touch_uart_rx_buf[6]; + touch_tuning_get16(&g_touch_tuning_element_cfgs[element_id].so, 7); + break; + } + + case TOUCH_TUNING_VARIABLE_ELEMENT_CFGS_SNUM: + { + element_id = g_touch_uart_rx_buf[6]; + touch_tuning_get8(&g_touch_tuning_element_cfgs[element_id].snum, 7); + break; + } + + case TOUCH_TUNING_VARIABLE_ELEMENT_CFGS_SDPA: + { + element_id = g_touch_uart_rx_buf[6]; + touch_tuning_get8(&g_touch_tuning_element_cfgs[element_id].sdpa, 7); + break; + } + #endif + + #if (BSP_FEATURE_CTSU_VERSION == 1) + case TOUCH_TUNING_VARIABLE_CTSU_WRITE_SSC: + { + element_id = g_touch_uart_rx_buf[6]; + touch_tuning_get16(&g_touch_tuning_ctsu_write[element_id].ctsussc, 7); + break; + } + + case TOUCH_TUNING_VARIABLE_CTSU_WRITE_SO0: + { + element_id = g_touch_uart_rx_buf[6]; + touch_tuning_get16(&g_touch_tuning_ctsu_write[element_id].ctsuso0, 7); + break; + } + + case TOUCH_TUNING_VARIABLE_CTSU_WRITE_SO1: + { + element_id = g_touch_uart_rx_buf[6]; + touch_tuning_get16(&g_touch_tuning_ctsu_write[element_id].ctsuso1, 7); + break; + } + #endif + case TOUCH_TUNING_VARIABLE_SELF_TARGET_VALUE: + { + touch_tuning_get16(&g_touch_tuning_self_target_value, 7); + break; + } + + case TOUCH_TUNING_VARIABLE_MUTUAL_TARGET_VALUE: + { + touch_tuning_get16(&g_touch_tuning_mutual_target_value, 7); + break; + } + #if (BSP_FEATURE_CTSU_VERSION == 2) + #if (CTSU_CFG_MULTIPLE_ELECTRODE_CONNECTION_ENABLE == 1) + case TOUCH_TUNING_VARIABLE_QE_TSOD: + { + touch_tuning_get8((uint8_t *)&g_touch_tuning_qe_tsod, 7); + break; + } + + case TOUCH_TUNING_VARIABLE_QE_MEC_TS: + { + touch_tuning_get8((uint8_t *)&g_touch_tuning_qe_mec_ts, 7); + break; + } + + case TOUCH_TUNING_VARIABLE_QE_MEC_SHIELD_TS : + { + touch_tuning_get8((uint8_t *)&g_touch_tuning_qe_mec_shield_ts, 7); + break; + } + #endif + #endif + + default: + { + break; + } + } + } + else if (g_touch_uart_rx_buf[1] == TOUCH_TUNING_COMMAND_VARIABLE_READ) + { + /* Data length */ + g_touch_tuning_tx_buf[2] = 0xC; + g_touch_tuning_tx_buf[3] = 0x0; + + index = 11; + + g_touch_tuning_tx_buf[5] = g_touch_uart_rx_buf[5]; + + switch (g_touch_uart_rx_buf[5]) /* data type */ + { + case TOUCH_TUNING_VARIABLE_MCU_GROUP: + { + g_touch_tuning_tx_buf[6] = 0; + touch_tuning_send8(g_touch_tuning_mcu_group, 7); + break; + } + + case TOUCH_TUNING_VARIABLE_PCLKB_FREQUENCY: + { + g_touch_tuning_tx_buf[6] = 0; + touch_tuning_send32(g_touch_tuning_pclkb_frequency, 7); + break; + } + + case TOUCH_TUNING_VARIABLE_TUNING_MODE: + { + g_touch_tuning_tx_buf[6] = 0; + touch_tuning_send8((uint8_t) g_touch_tuning_mode, 7); + break; + } + + case TOUCH_TUNING_VARIABLE_CTSU_STATE: + { + g_touch_tuning_tx_buf[6] = 0; + touch_tuning_send8((uint8_t) g_touch_tuning_state, 7); + break; + } + + case TOUCH_TUNING_VARIABLE_CTSU_SCAN_MODE: + { + g_touch_tuning_tx_buf[6] = 0; + touch_tuning_send8((uint8_t) g_touch_tuning_scan_mode, 7); + break; + } + + case TOUCH_TUNING_VARIABLE_TS_CHAC_MASK_BOTTOM: + { + g_touch_tuning_tx_buf[6] = 0; + touch_tuning_send32(g_touch_tuning_ts_chac_mask.mska, 7); + break; + } + + case TOUCH_TUNING_VARIABLE_TS_CHAC_MASK_TOP: + { + g_touch_tuning_tx_buf[6] = 0; + touch_tuning_send32(g_touch_tuning_ts_chac_mask.mskb, 7); + break; + } + + case TOUCH_TUNING_VARIABLE_TS_CHTRC_MASK_BOTTOM: + { + g_touch_tuning_tx_buf[6] = 0; + touch_tuning_send32(g_touch_tuning_ts_chtrc_mask.mska, 7); + break; + } + + case TOUCH_TUNING_VARIABLE_TS_CHTRC_MASK_TOP: + { + g_touch_tuning_tx_buf[6] = 0; + touch_tuning_send32(g_touch_tuning_ts_chtrc_mask.mskb, 7); + break; + } + + case TOUCH_TUNING_VARIABLE_QE_TXVSEL: + { + g_touch_tuning_tx_buf[6] = 0; + touch_tuning_send8(g_touch_tuning_qe_txvsel, 7); + break; + } + + case TOUCH_TUNING_VARIABLE_QE_ATUNE: + { + g_touch_tuning_tx_buf[6] = 0; + touch_tuning_send8((uint8_t) g_touch_tuning_qe_atune, 7); + break; + } + + case TOUCH_TUNING_VARIABLE_QE_POSEL: + { + g_touch_tuning_tx_buf[6] = 0; + touch_tuning_send8((uint8_t) g_touch_tuning_qe_posel, 7); + break; + } + + case TOUCH_TUNING_VARIABLE_ERR_EVENT: + { + g_touch_tuning_tx_buf[6] = 0; + touch_tuning_send8((uint8_t) g_touch_tuning_err_event, 7); + break; + } + + case TOUCH_TUNING_VARIABLE_CTSU_ICO_DATA_PRI: + { + g_touch_tuning_tx_buf[5] = TOUCH_TUNING_VARIABLE_CTSU_ICO_DATA_PRI; + element_id = g_touch_uart_rx_buf[6]; + if (TOUCH_TUNING_MODE_MEASURE_PHASE1 == g_touch_tuning_mode_save) + { + g_touch_tuning_tx_buf[6] = (uint8_t) element_id; + touch_tuning_send16(g_touch_tuning_ico_data[element_id], 7); + } + else + { + if (TOUCH_TUNING_SCAN_SELF == g_touch_tuning_scan_mode) + { + g_touch_tuning_tx_buf[6] = (uint8_t) element_id; + touch_tuning_send16(g_touch_tuning_ico_data[element_id], 7); + } + else if (TOUCH_TUNING_SCAN_MUTUAL == g_touch_tuning_scan_mode) + { + g_touch_tuning_tx_buf[6] = (uint8_t) element_id; + touch_tuning_send16(g_touch_tuning_ico_data[element_id * 2], 7); + } + + #if (BSP_FEATURE_CTSU_VERSION == 2) + #if (CTSU_CFG_NUM_CFC != 0) + else if (TOUCH_TUNING_SCAN_MUTUAL_CFC == g_touch_tuning_scan_mode) + { + g_touch_tuning_tx_buf[6] = (uint8_t) element_id; + touch_tuning_send16(g_touch_tuning_ico_data[element_id * 2], 7); + } + #endif + #endif + else + { + } + } + + break; + } + + case TOUCH_TUNING_VARIABLE_CTSU_ICO_DATA_SND: + { + element_id = g_touch_uart_rx_buf[6]; + g_touch_tuning_tx_buf[6] = (uint8_t) element_id; + touch_tuning_send32(g_touch_tuning_ico_data[element_id * 2 + 1], 7); + break; + } + + case TOUCH_TUNING_VARIABLE_CTSU_MUTUAL_DATA: + { + element_id = g_touch_uart_rx_buf[6]; + g_touch_tuning_tx_buf[6] = (uint8_t) element_id; + #if (CTSU_CFG_NUM_MUTUAL_ELEMENTS != 0) + touch_tuning_send32(g_touch_tuning_mutual_data[element_id], 7); + #endif + break; + } + + #if (BSP_FEATURE_CTSU_VERSION == 2) + case TOUCH_TUNING_VARIABLE_ELEMENT_CFGS_SO: + { + element_id = g_touch_uart_rx_buf[6]; + g_touch_tuning_tx_buf[6] = (uint8_t) element_id; + touch_tuning_send16(g_touch_tuning_element_cfgs[element_id].so, 7); + break; + } + + case TOUCH_TUNING_VARIABLE_ELEMENT_CFGS_SNUM: + { + element_id = g_touch_uart_rx_buf[6]; + g_touch_tuning_tx_buf[6] = (uint8_t) element_id; + touch_tuning_send8(g_touch_tuning_element_cfgs[element_id].snum, 7); + break; + } + + case TOUCH_TUNING_VARIABLE_ELEMENT_CFGS_SDPA: + { + g_touch_tuning_tx_buf[5] = TOUCH_TUNING_VARIABLE_ELEMENT_CFGS_SDPA; + element_id = g_touch_uart_rx_buf[6]; + + g_touch_tuning_tx_buf[6] = (uint8_t) element_id; + touch_tuning_send8(g_touch_tuning_element_cfgs[element_id].sdpa, 7); + break; + } + #endif + #if (BSP_FEATURE_CTSU_VERSION == 1) + case TOUCH_TUNING_VARIABLE_CTSU_WRITE_SSC: + { + element_id = g_touch_uart_rx_buf[6]; + g_touch_tuning_tx_buf[6] = (uint8_t) element_id; + touch_tuning_send16(g_touch_tuning_ctsu_write[element_id].ctsussc, 7); + break; + } + + case TOUCH_TUNING_VARIABLE_CTSU_WRITE_SO0: + { + element_id = g_touch_uart_rx_buf[6]; + g_touch_tuning_tx_buf[6] = (uint8_t) element_id; + touch_tuning_send16(g_touch_tuning_ctsu_write[element_id].ctsuso0, 7); + break; + } + + case TOUCH_TUNING_VARIABLE_CTSU_WRITE_SO1: + { + element_id = g_touch_uart_rx_buf[6]; + g_touch_tuning_tx_buf[6] = (uint8_t) element_id; + touch_tuning_send16(g_touch_tuning_ctsu_write[element_id].ctsuso1, 7); + break; + } + #endif + case TOUCH_TUNING_VARIABLE_SELF_TARGET_VALUE: + { + element_id = g_touch_uart_rx_buf[6]; + g_touch_tuning_tx_buf[6] = (uint8_t) element_id; + touch_tuning_send16(g_touch_tuning_self_target_value, 7); + break; + } + + case TOUCH_TUNING_VARIABLE_MUTUAL_TARGET_VALUE: + { + element_id = g_touch_uart_rx_buf[6]; + g_touch_tuning_tx_buf[6] = (uint8_t) element_id; + touch_tuning_send16(g_touch_tuning_mutual_target_value, 7); + break; + } + + #if (BSP_FEATURE_CTSU_VERSION == 2) + #if (CTSU_CFG_MULTIPLE_ELECTRODE_CONNECTION_ENABLE == 1) + case TOUCH_TUNING_VARIABLE_QE_TSOD: + { + element_id = g_touch_uart_rx_buf[6]; + g_touch_tuning_tx_buf[6] = (uint8_t) element_id; + touch_tuning_send8(g_touch_tuning_qe_tsod, 7); + break; + } + + case TOUCH_TUNING_VARIABLE_QE_MEC_TS: + { + element_id = g_touch_uart_rx_buf[6]; + g_touch_tuning_tx_buf[6] = (uint8_t) element_id; + touch_tuning_send8((uint8_t )g_touch_tuning_qe_mec_ts, 7); + break; + } + + case TOUCH_TUNING_VARIABLE_QE_MEC_SHIELD_TS : + { + element_id = g_touch_uart_rx_buf[6]; + g_touch_tuning_tx_buf[6] = (uint8_t) element_id; + touch_tuning_send8((uint8_t )g_touch_tuning_qe_mec_shield_ts, 7); + break; + } + #endif + #endif + default: + { + break; + } + } + } + else if (g_touch_uart_rx_buf[1] == TOUCH_TUNING_COMMAND_ADDRESS_WRITE) + { + index = 5; + + write_byte = g_touch_uart_rx_buf[9]; + + if (1 == write_byte) + { + port_adress_8 = (uint8_t *) (g_touch_uart_rx_buf[5] | + (g_touch_uart_rx_buf[6] << 8) | + (g_touch_uart_rx_buf[7] << 16) | + (g_touch_uart_rx_buf[8] << 24)); + + (*port_adress_8) |= (g_touch_uart_rx_buf[10]); + } + else if (2 == write_byte) + { + port_adress_16 = (uint16_t *) (g_touch_uart_rx_buf[5] | + (g_touch_uart_rx_buf[6] << 8) | + (g_touch_uart_rx_buf[7] << 16) | + (g_touch_uart_rx_buf[8] << 24)); + + (*port_adress_16) |= (uint16_t) (g_touch_uart_rx_buf[10] | + (g_touch_uart_rx_buf[11] << 8)); + } + else + { + port_adress_32 = (uint32_t *) (g_touch_uart_rx_buf[5] | + (g_touch_uart_rx_buf[6] << 8) | + (g_touch_uart_rx_buf[7] << 16) | + (g_touch_uart_rx_buf[8] << 24)); + + (*port_adress_32) |= (uint32_t) (g_touch_uart_rx_buf[10] | + (g_touch_uart_rx_buf[11] << 8) | + (g_touch_uart_rx_buf[12] << 16) | + (g_touch_uart_rx_buf[13] << 24)); + } + } + else if (g_touch_uart_rx_buf[1] == TOUCH_TUNING_COMMAND_ADDRESS_READ) + { + index = 10; + + write_byte = g_touch_uart_rx_buf[9]; + + if (1 == write_byte) + { + port_adress_8 = (uint8_t *) (g_touch_uart_rx_buf[5] | + (g_touch_uart_rx_buf[6] << 8) | + (g_touch_uart_rx_buf[7] << 16) | + (g_touch_uart_rx_buf[8] << 24)); + + touch_tuning_send8((*port_adress_8), 6); + } + else if (2 == write_byte) + { + port_adress_16 = (uint16_t *) (g_touch_uart_rx_buf[5] | + (g_touch_uart_rx_buf[6] << 8) | + (g_touch_uart_rx_buf[7] << 16) | + (g_touch_uart_rx_buf[8] << 24)); + + touch_tuning_send16((*port_adress_16), 6); + } + else + { + port_adress_32 = (uint32_t *) (g_touch_uart_rx_buf[5] | + (g_touch_uart_rx_buf[6] << 8) | + (g_touch_uart_rx_buf[7] << 16) | + (g_touch_uart_rx_buf[8] << 24)); + + touch_tuning_send32((*port_adress_32), 6); + } + } + else if (g_touch_uart_rx_buf[1] == TOUCH_TUNING_COMMAND_PHASE_RUN) + { + g_touch_tuning_phase_run = 1; + } + else if (g_touch_uart_rx_buf[1] == TOUCH_TUNING_COMMAND_VERSION) + { + g_touch_tuning_tx_buf[index++] = 1; + g_touch_tuning_tx_buf[index++] = 1; + } + else + { + } + + /* Set fixed footer */ + g_touch_tuning_tx_buf[index++] = TOUCH_TUNING_FOOTER; + + /* Set data size */ + g_touch_tuning_tx_buf[2] = (uint8_t) index; + g_touch_tuning_tx_buf[3] = (uint8_t) (index >> 8); + + /* Start transmission */ + g_touch_uart_transmit_flag = 1; + R_SCI_Send(g_touch_uart_control, &g_touch_tuning_tx_buf[0] ,index); + return; + } + #endif + + /* End user code. Do not edit comment generated here */ +} + +#endif + +#if (TOUCH_CFG_UART_TUNING_SUPPORT == 1) +void touch_serial_tuning (touch_instance_ctrl_t * const p_instance_ctrl) +{ + /* init tuning state */ + g_touch_tuning_state = TOUCH_TUNING_STATE_RESET; + + /* connect QE with UART */ + touch_tuning_uart_connect(); + + touch_tuning_open(p_instance_ctrl); + + while (1U) + { + switch (g_touch_tuning_mode) + { + case TOUCH_TUNING_MODE_CORRECTION_1ST_VALUE: + { + switch (g_touch_tuning_state) + { + case TOUCH_TUNING_STATE_RESET: + { + break; + } + + case TOUCH_TUNING_STATE_INITIALIZE: + { + if (1 == g_touch_tuning_phase_run) + { + g_touch_tuning_mode_save = g_touch_tuning_mode; + #if (BSP_FEATURE_CTSU_VERSION == 1) + touch_tuning_pclkb_get(&g_touch_tuning_pclkb_frequency); + #endif + g_touch_tuning_phase_run = 0; + g_touch_tuning_state = TOUCH_TUNING_STATE_COMPLETE; + } + + break; + } + + case TOUCH_TUNING_STATE_SCAN: + { + break; + } + + case TOUCH_TUNING_STATE_COMPLETE: + { + g_touch_tuning_mode = TOUCH_TUNING_MODE_QE_WATTING; + g_touch_tuning_state = TOUCH_TUNING_STATE_STOP; + break; + } + + case TOUCH_TUNING_STATE_STOP: + { + break; + } + } + + break; + } + + case TOUCH_TUNING_MODE_CORRECTION_2ND_VALUE: + { + /* TOUCH_TUNING_MODE_CORRECTION_2ND_VALUE does not have this part of processing */ + break; + } + + case TOUCH_TUNING_MODE_MEASURE_PHASE1: + { + switch (g_touch_tuning_state) + { + case TOUCH_TUNING_STATE_RESET: + { + break; + } + + case TOUCH_TUNING_STATE_INITIALIZE: + { + if (1 == g_touch_tuning_phase_run) + { + g_touch_tuning_mode_save = g_touch_tuning_mode; + touch_tuning_initialize(p_instance_ctrl); + g_touch_tuning_phase_run = 0; + g_touch_tuning_state = TOUCH_TUNING_STATE_SCAN; + } + + break; + } + + case TOUCH_TUNING_STATE_SCAN: + { + touch_tuning_dataget(p_instance_ctrl); + touch_tuning_scanstart(p_instance_ctrl); + break; + } + + case TOUCH_TUNING_STATE_COMPLETE: + { + touch_tuning_qe_get_value(p_instance_ctrl); + g_touch_tuning_mode = TOUCH_TUNING_MODE_QE_WATTING; + g_touch_tuning_state = TOUCH_TUNING_STATE_STOP; + break; + } + + case TOUCH_TUNING_STATE_STOP: + { + break; + } + } + + break; + } + + case TOUCH_TUNING_MODE_MEASURE_PHASE2: + { + switch (g_touch_tuning_state) + { + case TOUCH_TUNING_STATE_RESET: + { + break; + } + + case TOUCH_TUNING_STATE_INITIALIZE: + { + if (1 == g_touch_tuning_phase_run) + { + g_touch_tuning_mode_save = g_touch_tuning_mode; + touch_tuning_initialize(p_instance_ctrl); + g_touch_tuning_phase_run = 0; + g_touch_tuning_state = TOUCH_TUNING_STATE_SCAN; + } + + break; + } + + case TOUCH_TUNING_STATE_SCAN: + { + touch_tuning_dataget(p_instance_ctrl); + touch_tuning_scanstart(p_instance_ctrl); + break; + } + + case TOUCH_TUNING_STATE_COMPLETE: + { + touch_tuning_qe_get_value(p_instance_ctrl); + g_touch_tuning_mode = TOUCH_TUNING_MODE_QE_WATTING; + g_touch_tuning_state = TOUCH_TUNING_STATE_STOP; + break; + } + + case TOUCH_TUNING_STATE_STOP: + { + break; + } + } + + break; + } + + case TOUCH_TUNING_MODE_MEASURE_PHASE3: + { + switch (g_touch_tuning_state) + { + case TOUCH_TUNING_STATE_RESET: + { + break; + } + + case TOUCH_TUNING_STATE_INITIALIZE: + { + if (1 == g_touch_tuning_phase_run) + { + g_touch_tuning_mode_save = g_touch_tuning_mode; + touch_tuning_initialize(p_instance_ctrl); + g_touch_tuning_phase_run = 0; + g_touch_tuning_state = TOUCH_TUNING_STATE_SCAN; + } + + break; + } + + case TOUCH_TUNING_STATE_SCAN: + { + touch_tuning_dataget(p_instance_ctrl); + touch_tuning_scanstart(p_instance_ctrl); + break; + } + + case TOUCH_TUNING_STATE_COMPLETE: + { + break; + } + + case TOUCH_TUNING_STATE_STOP: + { + break; + } + } + + break; + } + + case TOUCH_TUNING_MODE_QE_WATTING: + { + break; + } + + case TOUCH_TUNING_MODE_START: + { + p_instance_ctrl->p_ctsu_instance->p_api->scanStop(p_instance_ctrl->p_ctsu_instance->p_ctrl); + + g_touch_tuning_phase_run = 0; + g_touch_tuning_mode = TOUCH_TUNING_MODE_QE_WATTING; + break; + } + + case TOUCH_TUNING_MODE_FINISH: + { + g_touch_tuning_phase_run = 0; + g_touch_tuning_mode = TOUCH_TUNING_MODE_QE_WATTING; + break; + } + } + + /* Reconnection due to UART baud rate difference. */ + touch_tuning_baudrate_err_reconnect (); + } +} /* End of function main() */ + +/*********************************************************************************************************************** + * touch_tuning_qe_get_value + ***********************************************************************************************************************/ +void touch_tuning_qe_get_value (touch_instance_ctrl_t * const p_instance_ctrl) +{ + uint16_t i; + uint16_t num_elements; + ctsu_instance_ctrl_t * p_ctsu_instance_ctrl = (ctsu_instance_ctrl_t *) p_instance_ctrl->p_ctsu_instance->p_ctrl; + + num_elements = p_ctsu_instance_ctrl->num_elements; + #if (BSP_FEATURE_CTSU_VERSION == 1) + if ((TOUCH_TUNING_SCAN_MUTUAL == g_touch_tuning_scan_mode) && + (TOUCH_TUNING_MODE_MEASURE_PHASE1 == g_touch_tuning_mode)) + { + num_elements = (uint16_t) (num_elements * 2); + } + #endif + + for (i = 0; i < num_elements; i++) + { + #if (BSP_FEATURE_CTSU_VERSION == 2) + + /* CTSU measurement condition data setting */ + g_touch_tuning_element_cfgs[i].so = + p_ctsu_instance_ctrl->p_ctsuwr[i * CTSU_CFG_NUM_SUMULTI].ctsuso & TOUCH_UART_CTSUSO_MASK; + g_touch_tuning_element_cfgs[i].snum = + (p_ctsu_instance_ctrl->p_ctsuwr[i * CTSU_CFG_NUM_SUMULTI].ctsuso >> 10) & TOUCH_UART_CTSUSNUM_MASK; + g_touch_tuning_element_cfgs[i].sdpa = + (uint8_t) ((p_ctsu_instance_ctrl->p_ctsuwr[i * CTSU_CFG_NUM_SUMULTI].ctsuso >> 24) & + TOUCH_UART_CTSUSDPA_MASK); + #endif + #if (BSP_FEATURE_CTSU_VERSION == 1) + g_touch_tuning_ctsu_write[i].ctsussc = p_ctsu_instance_ctrl->p_ctsuwr[i].ctsussc; + g_touch_tuning_ctsu_write[i].ctsuso0 = p_ctsu_instance_ctrl->p_ctsuwr[i].ctsuso0; + g_touch_tuning_ctsu_write[i].ctsuso1 = p_ctsu_instance_ctrl->p_ctsuwr[i].ctsuso1; + #endif + } +} + +/*********************************************************************************************************************** + * touch_tuning_initialize + ***********************************************************************************************************************/ +void touch_tuning_initialize (touch_instance_ctrl_t * const p_instance_ctrl) +{ + uint16_t i; + ctsu_instance_ctrl_t * p_ctsu_instance_ctrl = (ctsu_instance_ctrl_t *) p_instance_ctrl->p_ctsu_instance->p_ctrl; + + /* Variable initialization processing */ + p_ctsu_instance_ctrl->wr_index = 0; + p_ctsu_instance_ctrl->rd_index = 0; + for (i = 0; i < TOUCH_TUNING_KEY_MAX_NUM; i++) + { + g_touch_tuning_ico_data[i] = 0; + } + + p_ctsu_instance_ctrl->state = CTSU_STATE_IDLE; + + touch_tuning_scan_mode_select(p_instance_ctrl); + touch_tuning_ts_setup(p_instance_ctrl); + touch_tuning_qe_get_cfg(p_instance_ctrl); + touch_tuning_scan_register_setting(p_instance_ctrl); +} + +/*********************************************************************************************************************** + * touch_tuning_qe_get_cfg + ***********************************************************************************************************************/ +void touch_tuning_qe_get_cfg (touch_instance_ctrl_t * const p_instance_ctrl) +{ + uint16_t i; + uint16_t num_elements; + #if (BSP_FEATURE_CTSU_VERSION == 2) + uint8_t loop; + #endif + #if (BSP_FEATURE_CTSU_VERSION == 1) + uint16_t ctsu_sdpa; + #endif + ctsu_instance_ctrl_t * p_ctsu_instance_ctrl = (ctsu_instance_ctrl_t *) p_instance_ctrl->p_ctsu_instance->p_ctrl; + + p_ctsu_instance_ctrl->txvsel = (ctsu_txvsel_t) (g_touch_tuning_qe_txvsel & 0x01); + #if (BSP_FEATURE_CTSU_VERSION == 2) + p_ctsu_instance_ctrl->txvsel2 = (ctsu_txvsel2_t) ((g_touch_tuning_qe_txvsel >> 1) & 0x01); + #endif + num_elements = p_ctsu_instance_ctrl->num_elements; + + #if (BSP_FEATURE_CTSU_VERSION == 1) + if ((TOUCH_TUNING_SCAN_MUTUAL == g_touch_tuning_scan_mode) && + (TOUCH_TUNING_MODE_MEASURE_PHASE1 == g_touch_tuning_mode)) + { + num_elements = (uint16_t) (num_elements * 2); + } + #endif + + for (i = 0; i < num_elements; i++) + { + if (TOUCH_TUNING_MODE_MEASURE_PHASE1 == g_touch_tuning_mode) + { + #if (BSP_FEATURE_CTSU_VERSION == 2) + g_touch_tuning_element_cfgs[i].snum = 0x07; + g_touch_tuning_element_cfgs[i].sdpa = TOUCH_CFG_PHASE1_SDPA_VALUE; + g_touch_tuning_element_cfgs[i].so = 0x00; + #endif + #if (BSP_FEATURE_CTSU_VERSION == 1) + + /* Get SDPA value */ + touch_tuning_pclkb_get(&g_touch_tuning_pclkb_frequency); + ctsu_sdpa = (uint16_t) ((g_touch_tuning_pclkb_frequency / TOUCH_TUNING_PCLKB_FREQ_MHZ) - 1); + + /* CTSU measurement condition data setting */ + g_touch_tuning_ctsu_write[i].ctsussc = CTSU_SSDIV_0500 << 8; + g_touch_tuning_ctsu_write[i].ctsuso0 = 0x0000; + g_touch_tuning_ctsu_write[i].ctsuso1 = + (uint16_t) ((TOUCH_TUNING_ICOG_RECOMMEND << 13) | (ctsu_sdpa << 8) | TOUCH_TUNING_RICOA_RECOMMEND); + #endif + } + else if (TOUCH_TUNING_MODE_MEASURE_PHASE2 == g_touch_tuning_mode) + { + *(p_ctsu_instance_ctrl->p_element_complete_flag + i) = 0; + #if (BSP_FEATURE_CTSU_VERSION == 2) + *(p_ctsu_instance_ctrl->p_frequency_complete_flag + i) = 0; + #endif + #if (BSP_FEATURE_CTSU_VERSION == 1) + *(p_ctsu_instance_ctrl->p_tuning_diff + i) = 0; + #endif + #if defined(CTSU_CFG_TARGET_VALUE_QE_SUPPORT) + p_ctsu_instance_ctrl->tuning_self_target_value = g_touch_tuning_self_target_value; + p_ctsu_instance_ctrl->tuning_mutual_target_value = g_touch_tuning_mutual_target_value; + #endif + } + else if (TOUCH_TUNING_MODE_MEASURE_PHASE3 == g_touch_tuning_mode) + { + } + else + { + } + + #if (BSP_FEATURE_CTSU_VERSION == 2) + for (loop = 0; loop < CTSU_CFG_NUM_SUMULTI; loop++) + { + p_ctsu_instance_ctrl->p_ctsuwr[i * CTSU_CFG_NUM_SUMULTI + loop].ctsuso = + ((uint32_t) g_touch_tuning_element_cfgs[i].sdpa << 24) | + ((uint32_t) g_touch_tuning_element_cfgs[i].snum << 10) | + ((uint32_t) g_touch_tuning_element_cfgs[i].so); + } + #endif + #if (BSP_FEATURE_CTSU_VERSION == 1) + p_ctsu_instance_ctrl->p_ctsuwr[i].ctsussc = g_touch_tuning_ctsu_write[i].ctsussc; + p_ctsu_instance_ctrl->p_ctsuwr[i].ctsuso0 = g_touch_tuning_ctsu_write[i].ctsuso0; + p_ctsu_instance_ctrl->p_ctsuwr[i].ctsuso1 = g_touch_tuning_ctsu_write[i].ctsuso1 | TOUCH_TUNING_RICOA_RECOMMEND; + #endif + } + + /* Moving average reset */ + p_ctsu_instance_ctrl->average = 0; + + if (TOUCH_TUNING_MODE_MEASURE_PHASE1 == g_touch_tuning_mode) + { + p_ctsu_instance_ctrl->tuning = CTSU_TUNING_COMPLETE; + } + + if (TOUCH_TUNING_MODE_MEASURE_PHASE2 == g_touch_tuning_mode) + { + p_ctsu_instance_ctrl->tuning = CTSU_TUNING_INCOMPLETE; + } +} + +/*********************************************************************************************************************** + * touch_tuning_scan_mode_select + ***********************************************************************************************************************/ +void touch_tuning_scan_mode_select (touch_instance_ctrl_t * const p_instance_ctrl) +{ + ctsu_instance_ctrl_t * p_ctsu_instance_ctrl = (ctsu_instance_ctrl_t *) p_instance_ctrl->p_ctsu_instance->p_ctrl; + + /* MD register */ + if (TOUCH_TUNING_SCAN_SELF == g_touch_tuning_scan_mode) + { + g_touch_tuning_md = CTSU_MODE_SELF_MULTI_SCAN; + p_ctsu_instance_ctrl->md = CTSU_MODE_SELF_MULTI_SCAN; + + #if (BSP_FEATURE_CTSU_VERSION == 1) + if (TOUCH_TUNING_MODE_MEASURE_PHASE1 == g_touch_tuning_mode) + { + g_touch_tuning_qe_atune = CTSU_ATUNE1_HIGH; + } + else + { + g_touch_tuning_qe_atune = CTSU_ATUNE1_NORMAL; + } + #endif + } + else if (TOUCH_TUNING_SCAN_MUTUAL == g_touch_tuning_scan_mode) + { + #if (BSP_FEATURE_CTSU_VERSION == 1) + g_touch_tuning_qe_atune = CTSU_ATUNE1_HIGH; + #endif + if (TOUCH_TUNING_MODE_MEASURE_PHASE1 == g_touch_tuning_mode) + { + g_touch_tuning_md = CTSU_MODE_SELF_MULTI_SCAN; + p_ctsu_instance_ctrl->md = CTSU_MODE_MUTUAL_FULL_SCAN; + #if (BSP_FEATURE_CTSU_VERSION == 2) + #if (CTSU_CFG_NUM_CFC != 0) + R_CTSU->CTSUCRA_b.CSW = 1; + R_CTSU->CTSUCRA_b.PON = 1; + R_BSP_SoftwareDelay(30, BSP_DELAY_UNITS_MICROSECONDS); + #endif + #endif + } + else + { + g_touch_tuning_md = CTSU_MODE_MUTUAL_FULL_SCAN; + p_ctsu_instance_ctrl->md = CTSU_MODE_MUTUAL_FULL_SCAN; + } + } + + #if (BSP_FEATURE_CTSU_VERSION == 2) + else /* CFC */ + { + if (TOUCH_TUNING_MODE_MEASURE_PHASE1 == g_touch_tuning_mode) + { + g_touch_tuning_md = CTSU_MODE_SELF_MULTI_SCAN; + p_ctsu_instance_ctrl->md = CTSU_MODE_MUTUAL_FULL_SCAN; + } + else + { + g_touch_tuning_md = CTSU_MODE_MUTUAL_CFC_SCAN; + p_ctsu_instance_ctrl->md = CTSU_MODE_MUTUAL_CFC_SCAN; + } + } + #else + else + { + } + #endif + + if (TOUCH_TUNING_MODE_MEASURE_PHASE1 == g_touch_tuning_mode) + { + #if (BSP_FEATURE_CTSU_VERSION == 2) + g_touch_tuning_qe_atune = CTSU_ATUNE12_80UA; + #endif + #if (BSP_FEATURE_CTSU_VERSION == 1) + g_touch_tuning_qe_atune = CTSU_ATUNE1_HIGH; + #endif + } + + #if (BSP_FEATURE_CTSU_VERSION == 2) + #if (CTSU_CFG_NUM_CFC != 0) + /* CFCON register */ + if ((TOUCH_TUNING_SCAN_MUTUAL_CFC == g_touch_tuning_scan_mode) && + (TOUCH_TUNING_MODE_MEASURE_PHASE1 != g_touch_tuning_mode)) + { + R_CTSU->CTSUCALIB_b.CFCRDMD = 1; + R_CTSU->CTSUCRA_b.CFCON = 1; + } + else if ((TOUCH_TUNING_SCAN_MUTUAL_CFC == g_touch_tuning_scan_mode) && + (TOUCH_TUNING_MODE_MEASURE_PHASE1 == g_touch_tuning_mode)) + { + R_CTSU->CTSUCALIB_b.CFCRDMD = 0; + R_CTSU->CTSUCRA_b.CFCON = 0; + R_CTSU->CTSUCRA_b.CSW = 1; + R_CTSU->CTSUCRA_b.PON = 1; + R_BSP_SoftwareDelay(30, BSP_DELAY_UNITS_MICROSECONDS); + } + else + { + R_CTSU->CTSUCALIB_b.CFCRDMD = 0; + R_CTSU->CTSUCRA_b.CFCON = 0; + } + #endif + #endif +} + +void touch_tuning_scan_register_setting (touch_instance_ctrl_t * const p_instance_ctrl) +{ + ctsu_instance_ctrl_t * p_ctsu_instance_ctrl = (ctsu_instance_ctrl_t *) p_instance_ctrl->p_ctsu_instance->p_ctrl; + + p_ctsu_instance_ctrl->ctsucr1 = (uint8_t) ((g_touch_tuning_qe_atune & 0x01) << 3); + p_ctsu_instance_ctrl->ctsucr1 |= (uint8_t) (g_touch_tuning_md << 6); + + #if (BSP_FEATURE_CTSU_VERSION == 2) + p_ctsu_instance_ctrl->ctsucr2 = (uint8_t) (g_touch_tuning_qe_atune & 0x02); + p_ctsu_instance_ctrl->ctsucr2 |= (uint8_t) ((g_touch_tuning_md & 0x04) >> 2); + p_ctsu_instance_ctrl->ctsucr2 |= (uint8_t) (g_touch_tuning_qe_posel << 4); + + #if (CTSU_CFG_MULTIPLE_ELECTRODE_CONNECTION_ENABLE == 1) + p_ctsu_instance_ctrl->tsod = g_touch_tuning_qe_tsod; + p_ctsu_instance_ctrl->mec_ts = g_touch_tuning_qe_mec_ts; + p_ctsu_instance_ctrl->mec_shield_ts = g_touch_tuning_qe_mec_shield_ts; + #endif + #endif + + #if (BSP_FEATURE_CTSU_VERSION == 2) + if (CTSU_ATUNE12_80UA == g_touch_tuning_qe_atune) + { + p_ctsu_instance_ctrl->range = CTSU_RANGE_80UA; + } + else if (CTSU_ATUNE12_40UA == g_touch_tuning_qe_atune) + { + p_ctsu_instance_ctrl->range = CTSU_RANGE_40UA; + } + else if (CTSU_ATUNE12_20UA == g_touch_tuning_qe_atune) + { + p_ctsu_instance_ctrl->range = CTSU_RANGE_20UA; + } + else if (CTSU_ATUNE12_160UA == g_touch_tuning_qe_atune) + { + p_ctsu_instance_ctrl->range = CTSU_RANGE_160UA; + } + else + { + } + #endif +} + +/*********************************************************************************************************************** + * touch_tuning_ts_setup + ***********************************************************************************************************************/ +void touch_tuning_ts_setup (touch_instance_ctrl_t * const p_instance_ctrl) +{ + ctsu_instance_ctrl_t * p_ctsu_instance_ctrl = (ctsu_instance_ctrl_t *) p_instance_ctrl->p_ctsu_instance->p_ctrl; + uint32_t element_maska; + uint32_t element_maskb; + uint16_t rx_element; + uint16_t tx_element; + + /* initialize variable*/ + p_ctsu_instance_ctrl->num_elements = 0; + rx_element = 0; + tx_element = 0; + + /* Count number of element */ + if (TOUCH_TUNING_SCAN_SELF == g_touch_tuning_scan_mode) + { + if (TOUCH_TUNING_MODE_MEASURE_PHASE1 == g_touch_tuning_mode) + { + g_touch_tuning_ts_chtrc_mask.mska = 0; + g_touch_tuning_ts_chtrc_mask.mskb = 0; + } + else + { + #if (BSP_FEATURE_CTSU_VERSION == 1) + g_touch_tuning_ts_chtrc_mask.mska = 0; + g_touch_tuning_ts_chtrc_mask.mskb = 0; + #endif + } + + /* Excludes active shield from CHAC & CHACB */ + element_maska = g_touch_tuning_ts_chac_mask.mska - g_touch_tuning_ts_chtrc_mask.mska; + element_maskb = g_touch_tuning_ts_chac_mask.mskb - g_touch_tuning_ts_chtrc_mask.mskb; + + /* Get the number of measurable elements */ + touch_tuning_count_element(element_maska, &p_ctsu_instance_ctrl->num_elements); + touch_tuning_count_element(element_maskb, &p_ctsu_instance_ctrl->num_elements); + + #if (BSP_FEATURE_CTSU_VERSION == 2) + #if (CTSU_CFG_MULTIPLE_ELECTRODE_CONNECTION_ENABLE == 1) + if (1 == g_touch_tuning_qe_tsod) + { + p_ctsu_instance_ctrl->num_elements = 1; + } + #endif + #endif + + } + else if (TOUCH_TUNING_SCAN_MUTUAL == g_touch_tuning_scan_mode) + { + if (TOUCH_TUNING_MODE_MEASURE_PHASE1 == g_touch_tuning_mode) + { + /* Get the number of measurable elements */ + touch_tuning_count_element(g_touch_tuning_ts_chac_mask.mska, &p_ctsu_instance_ctrl->num_elements); + touch_tuning_count_element(g_touch_tuning_ts_chac_mask.mskb, &p_ctsu_instance_ctrl->num_elements); + + #if (BSP_FEATURE_CTSU_VERSION == 1) + if (0 == p_ctsu_instance_ctrl->num_elements % 2) + { + p_ctsu_instance_ctrl->num_elements = p_ctsu_instance_ctrl->num_elements / 2; + } + else + { + p_ctsu_instance_ctrl->num_elements = (uint16_t) (p_ctsu_instance_ctrl->num_elements / 2 + 1); + } + #endif + + g_touch_tuning_ts_chtrc_mask.mska = 0; + g_touch_tuning_ts_chtrc_mask.mskb = 0; + } + else + { + /* Store rx element mask */ + element_maska = g_touch_tuning_ts_chac_mask.mska - g_touch_tuning_ts_chtrc_mask.mska; + element_maskb = g_touch_tuning_ts_chac_mask.mskb - g_touch_tuning_ts_chtrc_mask.mskb; + + /* Get the number of measurable elements from enabled TS RX */ + touch_tuning_count_element(element_maska, &rx_element); + touch_tuning_count_element(element_maskb, &rx_element); + + /* Get the number of measurable elements from enabled TS TX */ + touch_tuning_count_element(g_touch_tuning_ts_chtrc_mask.mska, &tx_element); + touch_tuning_count_element(g_touch_tuning_ts_chtrc_mask.mskb, &tx_element); + + /* Get the number of measurable element (RX * TX) */ + p_ctsu_instance_ctrl->num_elements = (uint16_t) (rx_element * tx_element); + } + } + + #if (BSP_FEATURE_CTSU_VERSION == 2) + #if (CTSU_CFG_NUM_CFC != 0) + else if (TOUCH_TUNING_SCAN_MUTUAL_CFC == g_touch_tuning_scan_mode) + { + if (TOUCH_TUNING_MODE_MEASURE_PHASE1 == g_touch_tuning_mode) + { + /* Get the number of measurable elements */ + touch_tuning_count_element(g_touch_tuning_ts_chtrc_mask.mska, &p_ctsu_instance_ctrl->num_elements); + touch_tuning_count_element(g_touch_tuning_ts_chtrc_mask.mskb, &p_ctsu_instance_ctrl->num_elements); + + g_touch_tuning_ts_chac_mask.mska = g_touch_tuning_ts_chtrc_mask.mska; + g_touch_tuning_ts_chac_mask.mskb = g_touch_tuning_ts_chtrc_mask.mskb; + + g_touch_tuning_ts_chtrc_mask.mska = 0; + g_touch_tuning_ts_chtrc_mask.mskb = 0; + } + else + { + /* Store rx element mask */ + element_maska = g_touch_tuning_ts_chac_mask.mska - g_touch_tuning_ts_chtrc_mask.mska; + element_maskb = g_touch_tuning_ts_chac_mask.mskb - g_touch_tuning_ts_chtrc_mask.mskb; + + /* Get the number of measurable elements from enabled TS RX */ + touch_tuning_count_element(element_maska, &rx_element); + touch_tuning_count_element(element_maskb, &rx_element); + + /* Get the number of measurable elements from enabled TS TX */ + touch_tuning_count_element(g_touch_tuning_ts_chtrc_mask.mska, &tx_element); + touch_tuning_count_element(g_touch_tuning_ts_chtrc_mask.mskb, &tx_element); + + /* Get the number of measurable element (RX * TX) */ + p_ctsu_instance_ctrl->num_elements = (uint16_t) (rx_element * tx_element); + } + } + #endif + #endif + else + { + } + + /* get chac */ + p_ctsu_instance_ctrl->ctsuchac0 = (uint8_t) (g_touch_tuning_ts_chac_mask.mska & TOUCH_UART_TSCHAC_MASK); + p_ctsu_instance_ctrl->ctsuchac1 = (uint8_t) ((g_touch_tuning_ts_chac_mask.mska >> 8) & TOUCH_UART_TSCHAC_MASK); + p_ctsu_instance_ctrl->ctsuchac2 = (uint8_t) ((g_touch_tuning_ts_chac_mask.mska >> 16) & TOUCH_UART_TSCHAC_MASK); + p_ctsu_instance_ctrl->ctsuchac3 = (uint8_t) ((g_touch_tuning_ts_chac_mask.mska >> 24) & TOUCH_UART_TSCHAC_MASK); + p_ctsu_instance_ctrl->ctsuchac4 = (uint8_t) (g_touch_tuning_ts_chac_mask.mskb & TOUCH_UART_TSCHAC_MASK); + + /* get chtrc */ + p_ctsu_instance_ctrl->ctsuchtrc0 = (uint8_t) (g_touch_tuning_ts_chtrc_mask.mska & TOUCH_UART_TSCHTRC_MASK); + p_ctsu_instance_ctrl->ctsuchtrc1 = (uint8_t) ((g_touch_tuning_ts_chtrc_mask.mska >> 8) & TOUCH_UART_TSCHTRC_MASK); + p_ctsu_instance_ctrl->ctsuchtrc2 = (uint8_t) ((g_touch_tuning_ts_chtrc_mask.mska >> 16) & TOUCH_UART_TSCHTRC_MASK); + p_ctsu_instance_ctrl->ctsuchtrc3 = (uint8_t) ((g_touch_tuning_ts_chtrc_mask.mska >> 24) & TOUCH_UART_TSCHTRC_MASK); + p_ctsu_instance_ctrl->ctsuchtrc4 = (uint8_t) (g_touch_tuning_ts_chtrc_mask.mskb & TOUCH_UART_TSCHTRC_MASK); +} + +void touch_tuning_count_element (uint32_t element_mask, uint16_t * num_element) +{ + uint8_t n; + + /* Get the number of measurable elements from enabled CHAC or CHTRC */ + for (n = 0; n < TOUCH_TUNING_CH_REG_MAX_NUM; n++) + { + if (0x00000001 == ((element_mask >> n) & 0x00000001)) + { + (*num_element)++; + } + } +} + +void touch_tuning_uart_connect (void) +{ + volatile sci_err_t err; + g_touch_uart_config.async.baud_rate = TOUCH_CFG_UART_BAUDRATE; + g_touch_uart_config.async.clk_src = SCI_CLK_INT; + g_touch_uart_config.async.data_size = SCI_DATA_8BIT; + g_touch_uart_config.async.parity_en = SCI_PARITY_OFF; + g_touch_uart_config.async.parity_type = SCI_EVEN_PARITY; + g_touch_uart_config.async.stop_bits = SCI_STOPBITS_1; + g_touch_uart_config.async.int_priority = TOUCH_CFG_UART_PRIORITY; + + err = R_SCI_Open(TOUCH_CFG_UART_NUMBER, SCI_MODE_ASYNC, &g_touch_uart_config, touch_uart_callback, &g_touch_uart_control); + + err = R_SCI_Receive(g_touch_uart_control, &g_touch_uart_rx_buf[0] ,TOUCH_TUNING_RECIEVE_BUF_SIZE); + + return; +} + +void touch_tuning_open (touch_instance_ctrl_t * const p_instance_ctrl) +{ + ctsu_instance_ctrl_t * p_ctsu_instance_ctrl = (ctsu_instance_ctrl_t *) p_instance_ctrl->p_ctsu_instance->p_ctrl; + #if (CTSU_CFG_NUM_SELF_ELEMENTS != 0) + if (p_ctsu_instance_ctrl->md == CTSU_MODE_SELF_MULTI_SCAN) + { + p_ctsu_instance_ctrl->p_self_raw = + (p_ctsu_instance_ctrl->p_self_raw - + ((p_ctsu_instance_ctrl->self_elem_index - p_ctsu_instance_ctrl->num_elements) * CTSU_CFG_NUM_SUMULTI)); + p_ctsu_instance_ctrl->p_self_data = + (p_ctsu_instance_ctrl->p_self_data - + ((p_ctsu_instance_ctrl->self_elem_index - p_ctsu_instance_ctrl->num_elements))); + p_ctsu_instance_ctrl->p_self_corr = + (p_ctsu_instance_ctrl->p_self_corr - + ((p_ctsu_instance_ctrl->self_elem_index - p_ctsu_instance_ctrl->num_elements) * CTSU_CFG_NUM_SUMULTI)); +#if (BSP_FEATURE_CTSU_VERSION == 2) + p_ctsu_instance_ctrl->p_self_mfc = + (p_ctsu_instance_ctrl->p_self_mfc - + ((p_ctsu_instance_ctrl->self_elem_index - p_ctsu_instance_ctrl->num_elements) * CTSU_CFG_NUM_SUMULTI)); + p_ctsu_instance_ctrl->p_selected_freq_self = + (p_ctsu_instance_ctrl->p_selected_freq_self - + ((p_ctsu_instance_ctrl->ctsu_elem_index - p_ctsu_instance_ctrl->num_elements))); +#endif + } + #endif + + #if (CTSU_CFG_NUM_MUTUAL_ELEMENTS != 0) + if (p_ctsu_instance_ctrl->md == CTSU_MODE_MUTUAL_FULL_SCAN) + { + p_ctsu_instance_ctrl->p_mutual_raw = + (p_ctsu_instance_ctrl->p_mutual_raw - + ((p_ctsu_instance_ctrl->mutual_elem_index - p_ctsu_instance_ctrl->num_elements) * CTSU_CFG_NUM_SUMULTI * + 2)); + p_ctsu_instance_ctrl->p_mutual_pri_corr = + (p_ctsu_instance_ctrl->p_mutual_pri_corr - + ((p_ctsu_instance_ctrl->mutual_elem_index - p_ctsu_instance_ctrl->num_elements) * CTSU_CFG_NUM_SUMULTI)); + p_ctsu_instance_ctrl->p_mutual_snd_corr = + (p_ctsu_instance_ctrl->p_mutual_snd_corr - + ((p_ctsu_instance_ctrl->mutual_elem_index - p_ctsu_instance_ctrl->num_elements) * CTSU_CFG_NUM_SUMULTI)); + p_ctsu_instance_ctrl->p_mutual_pri_data = + (p_ctsu_instance_ctrl->p_mutual_pri_data - + ((p_ctsu_instance_ctrl->mutual_elem_index - p_ctsu_instance_ctrl->num_elements))); + p_ctsu_instance_ctrl->p_mutual_snd_data = + (p_ctsu_instance_ctrl->p_mutual_snd_data - + ((p_ctsu_instance_ctrl->mutual_elem_index - p_ctsu_instance_ctrl->num_elements))); +#if (BSP_FEATURE_CTSU_VERSION == 2) + p_ctsu_instance_ctrl->p_mutual_pri_mfc = + (p_ctsu_instance_ctrl->p_mutual_pri_mfc - + ((p_ctsu_instance_ctrl->mutual_elem_index - p_ctsu_instance_ctrl->num_elements) * CTSU_CFG_NUM_SUMULTI)); + p_ctsu_instance_ctrl->p_mutual_snd_mfc = + (p_ctsu_instance_ctrl->p_mutual_snd_mfc - + ((p_ctsu_instance_ctrl->mutual_elem_index - p_ctsu_instance_ctrl->num_elements) * CTSU_CFG_NUM_SUMULTI)); + p_ctsu_instance_ctrl->p_selected_freq_mutual = + (p_ctsu_instance_ctrl->p_selected_freq_mutual - + ((p_ctsu_instance_ctrl->ctsu_elem_index - p_ctsu_instance_ctrl->num_elements))); +#endif + + } + #endif + p_ctsu_instance_ctrl->p_element_complete_flag = + (p_ctsu_instance_ctrl->p_element_complete_flag - + (p_ctsu_instance_ctrl->ctsu_elem_index - p_ctsu_instance_ctrl->num_elements)); + #if (BSP_FEATURE_CTSU_VERSION == 2) + p_ctsu_instance_ctrl->p_frequency_complete_flag = + (p_ctsu_instance_ctrl->p_frequency_complete_flag - + (p_ctsu_instance_ctrl->ctsu_elem_index - p_ctsu_instance_ctrl->num_elements)); + #endif + p_ctsu_instance_ctrl->p_tuning_diff = + (p_ctsu_instance_ctrl->p_tuning_diff - + (p_ctsu_instance_ctrl->ctsu_elem_index - p_ctsu_instance_ctrl->num_elements)); + p_ctsu_instance_ctrl->p_ctsuwr = + (p_ctsu_instance_ctrl->p_ctsuwr - + ((p_ctsu_instance_ctrl->ctsu_elem_index - p_ctsu_instance_ctrl->num_elements) * CTSU_CFG_NUM_SUMULTI)); +} + +void touch_tuning_dataget (touch_instance_ctrl_t * const p_instance_ctrl) +{ + uint16_t i; + ctsu_instance_ctrl_t * p_ctsu_instance_ctrl = (ctsu_instance_ctrl_t *) p_instance_ctrl->p_ctsu_instance->p_ctrl; + + if (CTSU_STATE_SCANNED == p_ctsu_instance_ctrl->state) + { + p_instance_ctrl->p_ctsu_instance->p_api->dataGet(p_instance_ctrl->p_ctsu_instance->p_ctrl, + &g_touch_tuning_ico_data[0]); + + g_touch_tuning_err_event = p_ctsu_instance_ctrl->error_status; + if (TOUCH_TUNING_MODE_MEASURE_PHASE3 != g_touch_tuning_mode) + { + g_touch_tuning_state = TOUCH_TUNING_STATE_COMPLETE; + } + + if (TOUCH_TUNING_MODE_MEASURE_PHASE1 == g_touch_tuning_mode) + { + for (i = 0; i < p_ctsu_instance_ctrl->num_elements; i++) + { + if (TOUCH_TUNING_SCAN_SELF == g_touch_tuning_scan_mode) + { + g_touch_tuning_ico_data[i] = p_ctsu_instance_ctrl->p_self_corr[i * CTSU_CFG_NUM_SUMULTI]; + } + else if (TOUCH_TUNING_SCAN_MUTUAL == g_touch_tuning_scan_mode) + { + /* Due to different buffer compositions for CTSU1 and CTSU2 */ +#if (BSP_FEATURE_CTSU_VERSION == 1) + g_touch_tuning_ico_data[i * 2] = p_ctsu_instance_ctrl->p_mutual_pri_corr[i]; + g_touch_tuning_ico_data[i * 2 + 1] = p_ctsu_instance_ctrl->p_mutual_snd_corr[i]; +#endif +#if (BSP_FEATURE_CTSU_VERSION == 2) + g_touch_tuning_ico_data[i] = p_ctsu_instance_ctrl->p_mutual_pri_corr[i]; +#endif + } + + #if (BSP_FEATURE_CTSU_VERSION == 2) + #if (CTSU_CFG_NUM_CFC != 0) + else if (TOUCH_TUNING_SCAN_MUTUAL_CFC == g_touch_tuning_scan_mode) + { + g_touch_tuning_ico_data[i] = p_ctsu_instance_ctrl->p_mutual_raw[i]; + } + #endif + #endif + else + { + } + } + } + else if (TOUCH_TUNING_MODE_MEASURE_PHASE2 == g_touch_tuning_mode) + { + if (p_ctsu_instance_ctrl->tuning == CTSU_TUNING_INCOMPLETE) + { + g_touch_tuning_state = TOUCH_TUNING_STATE_SCAN; + } + } + else if (TOUCH_TUNING_MODE_MEASURE_PHASE3 == g_touch_tuning_mode) + { + #if (CTSU_CFG_NUM_MUTUAL_ELEMENTS != 0) + if (TOUCH_TUNING_SCAN_MUTUAL == g_touch_tuning_scan_mode) + { + for (i = 0; i < CTSU_CFG_NUM_MUTUAL_ELEMENTS; i++) + { + g_touch_tuning_mutual_data[i] = + (uint16_t) (g_touch_tuning_ico_data[i * 2 + 1] - g_touch_tuning_ico_data[i * 2]); + } + } + + #if (BSP_FEATURE_CTSU_VERSION == 2) + #if (CTSU_CFG_NUM_CFC != 0) + else if (TOUCH_TUNING_SCAN_MUTUAL_CFC == g_touch_tuning_scan_mode) + { + for (i = 0; i < CTSU_CFG_NUM_MUTUAL_ELEMENTS; i++) + { + g_touch_tuning_mutual_data[i] = + (uint16_t) (g_touch_tuning_ico_data[i * 2 + 1] - g_touch_tuning_ico_data[i * 2]); + } + } + #endif + #endif + else + { + } + #endif + } + else + { + } + } +} + +void touch_tuning_scanstart (touch_instance_ctrl_t * const p_instance_ctrl) +{ + ctsu_instance_ctrl_t * p_ctsu_instance_ctrl = (ctsu_instance_ctrl_t *) p_instance_ctrl->p_ctsu_instance->p_ctrl; + + if ((CTSU_STATE_IDLE == p_ctsu_instance_ctrl->state) && (TOUCH_TUNING_STATE_COMPLETE != g_touch_tuning_state)) + { + /* CTSU measurement start processing */ + p_instance_ctrl->p_ctsu_instance->p_api->scanStart(p_instance_ctrl->p_ctsu_instance->p_ctrl); + } +} + +void touch_tuning_get32 (uint32_t * p_val, uint16_t index) +{ + *p_val = ((uint32_t) g_touch_uart_rx_buf[index + 3] << 24) | + ((uint32_t) g_touch_uart_rx_buf[index + 2] << 16) | + ((uint32_t) g_touch_uart_rx_buf[index + 1] << 8) | + ((uint32_t) g_touch_uart_rx_buf[index]); +} + +void touch_tuning_get16 (uint16_t * p_val, uint16_t index) +{ + *p_val = (uint16_t) ((g_touch_uart_rx_buf[index + 1] << 8) | + g_touch_uart_rx_buf[index]); +} + +void touch_tuning_get8 (uint8_t * p_val, uint16_t index) +{ + *p_val = (g_touch_uart_rx_buf[index]); +} + +void touch_tuning_send32 (uint32_t value, uint16_t index) +{ + g_touch_tuning_tx_buf[index] = (uint8_t) (value); + g_touch_tuning_tx_buf[index + 1] = (uint8_t) (value >> 8); + g_touch_tuning_tx_buf[index + 2] = (uint8_t) (value >> 16); + g_touch_tuning_tx_buf[index + 3] = (uint8_t) (value >> 24); +} + +void touch_tuning_send16 (uint16_t value, uint16_t index) +{ + g_touch_tuning_tx_buf[index] = (uint8_t) (value); + g_touch_tuning_tx_buf[index + 1] = (uint8_t) (value >> 8); + g_touch_tuning_tx_buf[index + 2] = 0; + g_touch_tuning_tx_buf[index + 3] = 0; +} + +void touch_tuning_send8 (uint8_t value, uint16_t index) +{ + g_touch_tuning_tx_buf[index] = (value); + g_touch_tuning_tx_buf[index + 1] = 0; + g_touch_tuning_tx_buf[index + 2] = 0; + g_touch_tuning_tx_buf[index + 3] = 0; +} + + #if (BSP_FEATURE_CTSU_VERSION == 1) +void touch_tuning_pclkb_get (volatile uint32_t * pclkb_frequency) +{ + if (TOUCH_TUNING_PCLKB_FREQ_32MHZ >= *pclkb_frequency) + { + } + else if ((TOUCH_TUNING_PCLKB_FREQ_32MHZ < *pclkb_frequency) && (TOUCH_TUNING_PCLKB_FREQ_64MHZ >= *pclkb_frequency)) + { + *pclkb_frequency = (uint32_t) (*pclkb_frequency / 2); + } + else + { + *pclkb_frequency = (uint32_t) (*pclkb_frequency / 4); + } +} + + #endif + +/* Reconnection due to UART baud rate difference. */ +void touch_tuning_baudrate_err_reconnect (void) +{ + if (1 == g_touch_uart_baudrate_err_flag) + { + g_touch_uart_baudrate_err_flag = 0; + R_BSP_SoftwareDelay(10, BSP_DELAY_MILLISECS); + g_receive_intterupt_count = 0; + R_SCI_Close(g_touch_uart_control); + R_SCI_Open(TOUCH_CFG_UART_NUMBER, SCI_MODE_ASYNC, &g_touch_uart_config, touch_uart_callback, &g_touch_uart_control); + } +} + +#endif diff --git a/zephyr/rx/rdp_cfg/r_config/rm_touch_qe_config.h b/zephyr/rx/rdp_cfg/r_config/rm_touch_qe_config.h new file mode 100644 index 00000000..91ce0881 --- /dev/null +++ b/zephyr/rx/rdp_cfg/r_config/rm_touch_qe_config.h @@ -0,0 +1,41 @@ +/* +* Copyright (c) 2018 Renesas Electronics Corporation and/or its affiliates +* +* SPDX-License-Identifier: BSD-3-Clause +*/ +/*********************************************************************************************************************** + * File Name : rm_touch_qe_config.h + * Description : Configures the QE Touch API module. + ***********************************************************************************************************************/ +#ifndef TOUCH_CONFIG_HEADER_FILE +#define TOUCH_CONFIG_HEADER_FILE + + +/*********************************************************************************************************************** + Configuration Options + ***********************************************************************************************************************/ +#define TOUCH_CFG_PARAM_CHECKING_ENABLE (BSP_CFG_PARAM_CHECKING_ENABLE) + +#define TOUCH_CFG_UART_MONITOR_SUPPORT (0) + +#define TOUCH_CFG_UART_TUNING_SUPPORT (0) + +#define TOUCH_CFG_UART_MODULE_TYPE (0) + +#define TOUCH_CFG_UART_NUMBER (0) + +#define TOUCH_CFG_UART_BAUDRATE (115200) + +#define TOUCH_CFG_UART_PRIORITY (0) + +#define TOUCH_CFG_CHATTERING_SUPPRESSION_TYPE (0) + +#ifndef QE_TOUCH_CONFIGURATION +#define TOUCH_CFG_NUM_BUTTONS (DT_NUM_INST_STATUS_OKAY(renesas_rx_ctsu_button)) +#define TOUCH_CFG_NUM_SLIDERS (DT_NUM_INST_STATUS_OKAY(renesas_rx_ctsu_slider)) +#define TOUCH_CFG_NUM_WHEELS (DT_NUM_INST_STATUS_OKAY(renesas_rx_ctsu_wheel)) +#if (BSP_FEATURE_CTSU_VERSION == 2) +#define TOUCH_CFG_PAD_ENABLE (0) +#endif +#endif +#endif /* TOUCH_CONFIG_HEADER_FILE */