diff --git a/ra/board/ra4w1_ek/board_leds.c b/ra/board/ra4w1_ek/board_leds.c index a299d2b08..eca267db7 100644 --- a/ra/board/ra4w1_ek/board_leds.c +++ b/ra/board/ra4w1_ek/board_leds.c @@ -56,7 +56,6 @@ static const uint16_t g_bsp_prv_leds[] = /** Structure with LED information for this board. */ -/*LDRA_INSPECTED 27 D This structure must be accessible in user code. It cannot be static. */ const bsp_leds_t g_bsp_leds = { .led_count = (uint16_t) ((sizeof(g_bsp_prv_leds) / sizeof(g_bsp_prv_leds[0]))), diff --git a/ra/board/ra6t1_rssk/board.h b/ra/board/ra6t1_rssk/board.h new file mode 100644 index 000000000..63b856b6e --- /dev/null +++ b/ra/board/ra6t1_rssk/board.h @@ -0,0 +1,66 @@ +/*********************************************************************************************************************** + * Copyright [2020] Renesas Electronics Corporation and/or its affiliates. All Rights Reserved. + * + * This software and documentation are supplied by Renesas Electronics America Inc. and may only be used with products + * of Renesas Electronics Corp. and its affiliates ("Renesas"). No other uses are authorized. Renesas products are + * sold pursuant to Renesas terms and conditions of sale. Purchasers are solely responsible for the selection and use + * of Renesas products and Renesas assumes no liability. No license, express or implied, to any intellectual property + * right is granted by Renesas. This software is protected under all applicable laws, including copyright laws. Renesas + * reserves the right to change or discontinue this software and/or this documentation. THE SOFTWARE AND DOCUMENTATION + * IS DELIVERED TO YOU "AS IS," AND RENESAS MAKES NO REPRESENTATIONS OR WARRANTIES, AND TO THE FULLEST EXTENT + * PERMISSIBLE UNDER APPLICABLE LAW, DISCLAIMS ALL WARRANTIES, WHETHER EXPLICITLY OR IMPLICITLY, INCLUDING WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NONINFRINGEMENT, WITH RESPECT TO THE SOFTWARE OR + * DOCUMENTATION. RENESAS SHALL HAVE NO LIABILITY ARISING OUT OF ANY SECURITY VULNERABILITY OR BREACH. TO THE MAXIMUM + * EXTENT PERMITTED BY LAW, IN NO EVENT WILL RENESAS BE LIABLE TO YOU IN CONNECTION WITH THE SOFTWARE OR DOCUMENTATION + * (OR ANY PERSON OR ENTITY CLAIMING RIGHTS DERIVED FROM YOU) FOR ANY LOSS, DAMAGES, OR CLAIMS WHATSOEVER, INCLUDING, + * WITHOUT LIMITATION, ANY DIRECT, CONSEQUENTIAL, SPECIAL, INDIRECT, PUNITIVE, OR INCIDENTAL DAMAGES; ANY LOST PROFITS, + * OTHER ECONOMIC DAMAGE, PROPERTY DAMAGE, OR PERSONAL INJURY; AND EVEN IF RENESAS HAS BEEN ADVISED OF THE POSSIBILITY + * OF SUCH LOSS, DAMAGES, CLAIMS OR COSTS. + **********************************************************************************************************************/ + +/*********************************************************************************************************************** + * File Name : board.h + * Description : Includes and API function available for this board. + **********************************************************************************************************************/ + +/*******************************************************************************************************************//** + * @ingroup BOARDS + * @defgroup BOARD_RA6T1_RSSK + * @brief BSP for the RA6T1_RSSK Board + * + * The RA6T1_RSSK is a development kit for the Renesas RA6T1 microcontroller. + * + * @{ + **********************************************************************************************************************/ + +#ifndef BOARD_H +#define BOARD_H + +/*********************************************************************************************************************** + * Includes , "Project Includes" + **********************************************************************************************************************/ + +/* BSP Board Specific Includes. */ +#include "board_init.h" +#include "board_leds.h" + +/*********************************************************************************************************************** + * Macro definitions + **********************************************************************************************************************/ +#define BOARD_RA6T1_RSSK + +/*********************************************************************************************************************** + * Typedef definitions + **********************************************************************************************************************/ + +/*********************************************************************************************************************** + * Exported global variables + **********************************************************************************************************************/ + +/*********************************************************************************************************************** + * Exported global functions (to be accessed by other files) + **********************************************************************************************************************/ + +/** @} (end defgroup BSP_CONFIG_RA6T1) */ + +#endif diff --git a/ra/board/ra6t1_rssk/board_init.c b/ra/board/ra6t1_rssk/board_init.c new file mode 100644 index 000000000..aad43e7ee --- /dev/null +++ b/ra/board/ra6t1_rssk/board_init.c @@ -0,0 +1,67 @@ +/*********************************************************************************************************************** + * Copyright [2020] Renesas Electronics Corporation and/or its affiliates. All Rights Reserved. + * + * This software and documentation are supplied by Renesas Electronics America Inc. and may only be used with products + * of Renesas Electronics Corp. and its affiliates ("Renesas"). No other uses are authorized. Renesas products are + * sold pursuant to Renesas terms and conditions of sale. Purchasers are solely responsible for the selection and use + * of Renesas products and Renesas assumes no liability. No license, express or implied, to any intellectual property + * right is granted by Renesas. This software is protected under all applicable laws, including copyright laws. Renesas + * reserves the right to change or discontinue this software and/or this documentation. THE SOFTWARE AND DOCUMENTATION + * IS DELIVERED TO YOU "AS IS," AND RENESAS MAKES NO REPRESENTATIONS OR WARRANTIES, AND TO THE FULLEST EXTENT + * PERMISSIBLE UNDER APPLICABLE LAW, DISCLAIMS ALL WARRANTIES, WHETHER EXPLICITLY OR IMPLICITLY, INCLUDING WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NONINFRINGEMENT, WITH RESPECT TO THE SOFTWARE OR + * DOCUMENTATION. RENESAS SHALL HAVE NO LIABILITY ARISING OUT OF ANY SECURITY VULNERABILITY OR BREACH. TO THE MAXIMUM + * EXTENT PERMITTED BY LAW, IN NO EVENT WILL RENESAS BE LIABLE TO YOU IN CONNECTION WITH THE SOFTWARE OR DOCUMENTATION + * (OR ANY PERSON OR ENTITY CLAIMING RIGHTS DERIVED FROM YOU) FOR ANY LOSS, DAMAGES, OR CLAIMS WHATSOEVER, INCLUDING, + * WITHOUT LIMITATION, ANY DIRECT, CONSEQUENTIAL, SPECIAL, INDIRECT, PUNITIVE, OR INCIDENTAL DAMAGES; ANY LOST PROFITS, + * OTHER ECONOMIC DAMAGE, PROPERTY DAMAGE, OR PERSONAL INJURY; AND EVEN IF RENESAS HAS BEEN ADVISED OF THE POSSIBILITY + * OF SUCH LOSS, DAMAGES, CLAIMS OR COSTS. + **********************************************************************************************************************/ + +/*********************************************************************************************************************** + * File Name : board_init.c + * Description : This module calls any initialization code specific to this BSP. + **********************************************************************************************************************/ + +/*******************************************************************************************************************//** + * @addtogroup BOARD_RA6T1_RSSK_INIT + * + * @{ + **********************************************************************************************************************/ + +/*********************************************************************************************************************** + * Includes , "Project Includes" + **********************************************************************************************************************/ +#include "bsp_api.h" + +#if defined(BOARD_RA6T1_RSSK) + +/*********************************************************************************************************************** + * Macro definitions + **********************************************************************************************************************/ + +/*********************************************************************************************************************** + * Typedef definitions + **********************************************************************************************************************/ + +/*********************************************************************************************************************** + * Exported global variables (to be accessed by other files) + **********************************************************************************************************************/ + +/*********************************************************************************************************************** + * Private global variables and functions + **********************************************************************************************************************/ + +/*******************************************************************************************************************//** + * @brief Performs any initialization specific to this BSP. + * + * @param[in] p_args Pointer to arguments of the user's choice. + **********************************************************************************************************************/ +void bsp_init (void * p_args) +{ + FSP_PARAMETER_NOT_USED(p_args); +} + +#endif + +/** @} (end addtogroup BOARD_RA6T1_RSSK_INIT) */ diff --git a/ra/board/ra6t1_rssk/board_init.h b/ra/board/ra6t1_rssk/board_init.h new file mode 100644 index 000000000..c0e54fd2a --- /dev/null +++ b/ra/board/ra6t1_rssk/board_init.h @@ -0,0 +1,64 @@ +/*********************************************************************************************************************** + * Copyright [2020] Renesas Electronics Corporation and/or its affiliates. All Rights Reserved. + * + * This software and documentation are supplied by Renesas Electronics America Inc. and may only be used with products + * of Renesas Electronics Corp. and its affiliates ("Renesas"). No other uses are authorized. Renesas products are + * sold pursuant to Renesas terms and conditions of sale. Purchasers are solely responsible for the selection and use + * of Renesas products and Renesas assumes no liability. No license, express or implied, to any intellectual property + * right is granted by Renesas. This software is protected under all applicable laws, including copyright laws. Renesas + * reserves the right to change or discontinue this software and/or this documentation. THE SOFTWARE AND DOCUMENTATION + * IS DELIVERED TO YOU "AS IS," AND RENESAS MAKES NO REPRESENTATIONS OR WARRANTIES, AND TO THE FULLEST EXTENT + * PERMISSIBLE UNDER APPLICABLE LAW, DISCLAIMS ALL WARRANTIES, WHETHER EXPLICITLY OR IMPLICITLY, INCLUDING WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NONINFRINGEMENT, WITH RESPECT TO THE SOFTWARE OR + * DOCUMENTATION. RENESAS SHALL HAVE NO LIABILITY ARISING OUT OF ANY SECURITY VULNERABILITY OR BREACH. TO THE MAXIMUM + * EXTENT PERMITTED BY LAW, IN NO EVENT WILL RENESAS BE LIABLE TO YOU IN CONNECTION WITH THE SOFTWARE OR DOCUMENTATION + * (OR ANY PERSON OR ENTITY CLAIMING RIGHTS DERIVED FROM YOU) FOR ANY LOSS, DAMAGES, OR CLAIMS WHATSOEVER, INCLUDING, + * WITHOUT LIMITATION, ANY DIRECT, CONSEQUENTIAL, SPECIAL, INDIRECT, PUNITIVE, OR INCIDENTAL DAMAGES; ANY LOST PROFITS, + * OTHER ECONOMIC DAMAGE, PROPERTY DAMAGE, OR PERSONAL INJURY; AND EVEN IF RENESAS HAS BEEN ADVISED OF THE POSSIBILITY + * OF SUCH LOSS, DAMAGES, CLAIMS OR COSTS. + **********************************************************************************************************************/ + +/*********************************************************************************************************************** + * File Name : board_init.h + * Description : This module calls any initialization code specific to this BSP. + **********************************************************************************************************************/ + +/*******************************************************************************************************************//** + * @ingroup BOARD_RA6T1_RSSK + * @defgroup BOARD_RA6T1_RSSK_INIT + * @brief Board specific code for the RA6T1_RSSK Board + * + * This include file is specific to the RA6T1_RSSK board. + * + * @{ + **********************************************************************************************************************/ + +#ifndef BOARD_INIT_H +#define BOARD_INIT_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 + **********************************************************************************************************************/ + +/*********************************************************************************************************************** + * Typedef definitions + **********************************************************************************************************************/ + +/*********************************************************************************************************************** + * Exported global variables + **********************************************************************************************************************/ + +/*********************************************************************************************************************** + * Exported global functions (to be accessed by other files) + **********************************************************************************************************************/ +void bsp_init(void * p_args); + +/** Common macro for FSP header files. There is also a corresponding FSP_HEADER macro at the top of this file. */ +FSP_FOOTER + +#endif + +/** @} (end defgroup BOARD_RA6T1_RSSK_INIT) */ diff --git a/ra/board/ra6t1_rssk/board_leds.c b/ra/board/ra6t1_rssk/board_leds.c new file mode 100644 index 000000000..ede21821f --- /dev/null +++ b/ra/board/ra6t1_rssk/board_leds.c @@ -0,0 +1,75 @@ +/*********************************************************************************************************************** + * Copyright [2020] Renesas Electronics Corporation and/or its affiliates. All Rights Reserved. + * + * This software and documentation are supplied by Renesas Electronics America Inc. and may only be used with products + * of Renesas Electronics Corp. and its affiliates ("Renesas"). No other uses are authorized. Renesas products are + * sold pursuant to Renesas terms and conditions of sale. Purchasers are solely responsible for the selection and use + * of Renesas products and Renesas assumes no liability. No license, express or implied, to any intellectual property + * right is granted by Renesas. This software is protected under all applicable laws, including copyright laws. Renesas + * reserves the right to change or discontinue this software and/or this documentation. THE SOFTWARE AND DOCUMENTATION + * IS DELIVERED TO YOU "AS IS," AND RENESAS MAKES NO REPRESENTATIONS OR WARRANTIES, AND TO THE FULLEST EXTENT + * PERMISSIBLE UNDER APPLICABLE LAW, DISCLAIMS ALL WARRANTIES, WHETHER EXPLICITLY OR IMPLICITLY, INCLUDING WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NONINFRINGEMENT, WITH RESPECT TO THE SOFTWARE OR + * DOCUMENTATION. RENESAS SHALL HAVE NO LIABILITY ARISING OUT OF ANY SECURITY VULNERABILITY OR BREACH. TO THE MAXIMUM + * EXTENT PERMITTED BY LAW, IN NO EVENT WILL RENESAS BE LIABLE TO YOU IN CONNECTION WITH THE SOFTWARE OR DOCUMENTATION + * (OR ANY PERSON OR ENTITY CLAIMING RIGHTS DERIVED FROM YOU) FOR ANY LOSS, DAMAGES, OR CLAIMS WHATSOEVER, INCLUDING, + * WITHOUT LIMITATION, ANY DIRECT, CONSEQUENTIAL, SPECIAL, INDIRECT, PUNITIVE, OR INCIDENTAL DAMAGES; ANY LOST PROFITS, + * OTHER ECONOMIC DAMAGE, PROPERTY DAMAGE, OR PERSONAL INJURY; AND EVEN IF RENESAS HAS BEEN ADVISED OF THE POSSIBILITY + * OF SUCH LOSS, DAMAGES, CLAIMS OR COSTS. + **********************************************************************************************************************/ + +/*********************************************************************************************************************** + * File Name : board_leds.c + * Description : This module has information about the LEDs on this board. + **********************************************************************************************************************/ + +/*******************************************************************************************************************//** + * @addtogroup BOARD_RA6T1_RSSK_LEDS + * + * @{ + **********************************************************************************************************************/ + +/*********************************************************************************************************************** + * Includes + **********************************************************************************************************************/ +#include "bsp_api.h" +#if defined(BOARD_RA6T1_RSSK) + +/*********************************************************************************************************************** + * Macro definitions + **********************************************************************************************************************/ + +/*********************************************************************************************************************** + * Typedef definitions + **********************************************************************************************************************/ + +/*********************************************************************************************************************** + * Private global variables and functions + **********************************************************************************************************************/ + +/** Array of LED IOPORT pins. */ +static const uint16_t g_bsp_prv_leds[] = +{ + (uint16_t) BSP_IO_PORT_04_PIN_08, ///< LED1 + (uint16_t) BSP_IO_PORT_04_PIN_07, ///< LED2 +}; + +/*********************************************************************************************************************** + * Exported global variables (to be accessed by other files) + **********************************************************************************************************************/ + +/** Structure with LED information for this board. */ + +const bsp_leds_t g_bsp_leds = +{ + .led_count = (uint16_t) ((sizeof(g_bsp_prv_leds) / sizeof(g_bsp_prv_leds[0]))), + .p_leds = &g_bsp_prv_leds[0] +}; + +/*********************************************************************************************************************** + * Exported global variables (to be accessed by other files) + **********************************************************************************************************************/ + +#endif + +/** @} (end addtogroup BOARD_RA6T1_RSSK_LEDS) */ diff --git a/ra/board/ra6t1_rssk/board_leds.h b/ra/board/ra6t1_rssk/board_leds.h new file mode 100644 index 000000000..8c5cc4069 --- /dev/null +++ b/ra/board/ra6t1_rssk/board_leds.h @@ -0,0 +1,79 @@ +/*********************************************************************************************************************** + * Copyright [2020] Renesas Electronics Corporation and/or its affiliates. All Rights Reserved. + * + * This software and documentation are supplied by Renesas Electronics America Inc. and may only be used with products + * of Renesas Electronics Corp. and its affiliates ("Renesas"). No other uses are authorized. Renesas products are + * sold pursuant to Renesas terms and conditions of sale. Purchasers are solely responsible for the selection and use + * of Renesas products and Renesas assumes no liability. No license, express or implied, to any intellectual property + * right is granted by Renesas. This software is protected under all applicable laws, including copyright laws. Renesas + * reserves the right to change or discontinue this software and/or this documentation. THE SOFTWARE AND DOCUMENTATION + * IS DELIVERED TO YOU "AS IS," AND RENESAS MAKES NO REPRESENTATIONS OR WARRANTIES, AND TO THE FULLEST EXTENT + * PERMISSIBLE UNDER APPLICABLE LAW, DISCLAIMS ALL WARRANTIES, WHETHER EXPLICITLY OR IMPLICITLY, INCLUDING WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NONINFRINGEMENT, WITH RESPECT TO THE SOFTWARE OR + * DOCUMENTATION. RENESAS SHALL HAVE NO LIABILITY ARISING OUT OF ANY SECURITY VULNERABILITY OR BREACH. TO THE MAXIMUM + * EXTENT PERMITTED BY LAW, IN NO EVENT WILL RENESAS BE LIABLE TO YOU IN CONNECTION WITH THE SOFTWARE OR DOCUMENTATION + * (OR ANY PERSON OR ENTITY CLAIMING RIGHTS DERIVED FROM YOU) FOR ANY LOSS, DAMAGES, OR CLAIMS WHATSOEVER, INCLUDING, + * WITHOUT LIMITATION, ANY DIRECT, CONSEQUENTIAL, SPECIAL, INDIRECT, PUNITIVE, OR INCIDENTAL DAMAGES; ANY LOST PROFITS, + * OTHER ECONOMIC DAMAGE, PROPERTY DAMAGE, OR PERSONAL INJURY; AND EVEN IF RENESAS HAS BEEN ADVISED OF THE POSSIBILITY + * OF SUCH LOSS, DAMAGES, CLAIMS OR COSTS. + **********************************************************************************************************************/ + +/*********************************************************************************************************************** + * File Name : board_leds.h + * Description : This module has information about the LEDs on this board. + **********************************************************************************************************************/ + +/*******************************************************************************************************************//** + * @ingroup BOARD_RA6T1_RSSK + * @defgroup BOARD_RA6T1_RSSK_LEDS Board LEDs + * @brief LED information for this board. + * + * This is code specific to the RA6T1_RSSK board. It includes info on the number of LEDs and which pins are they + * are on. + * + * @{ + **********************************************************************************************************************/ + +#ifndef BOARD_LEDS_H +#define BOARD_LEDS_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 + **********************************************************************************************************************/ + +/*********************************************************************************************************************** + * Typedef definitions + **********************************************************************************************************************/ + +/** Information on how many LEDs and what pins they are on. */ +typedef struct st_bsp_leds +{ + uint16_t led_count; ///< The number of LEDs on this board + uint16_t const * p_leds; ///< Pointer to an array of IOPORT pins for controlling LEDs +} bsp_leds_t; + +/** Available user-controllable LEDs on this board. These enums can be can be used to index into the array of LED pins + * found in the bsp_leds_t structure. */ +typedef enum e_bsp_led +{ + BSP_LED_LED1 = 0, + BSP_LED_LED2 = 0 +} bsp_led_t; + +/*********************************************************************************************************************** + * Exported global variables + **********************************************************************************************************************/ + +/*********************************************************************************************************************** + * Public Functions + **********************************************************************************************************************/ + +/** Common macro for FSP header files. There is also a corresponding FSP_HEADER macro at the top of this file. */ +FSP_FOOTER + +#endif + +/** @} (end defgroup BOARD_RA6T1_RSSK_LEDS) */ diff --git a/ra/fsp/inc/api/r_cgc_api.h b/ra/fsp/inc/api/r_cgc_api.h index 0e2933195..5b0d1eb4b 100644 --- a/ra/fsp/inc/api/r_cgc_api.h +++ b/ra/fsp/inc/api/r_cgc_api.h @@ -58,7 +58,7 @@ FSP_HEADER /* Version Number of API. */ #define CGC_API_VERSION_MAJOR (1U) -#define CGC_API_VERSION_MINOR (0U) +#define CGC_API_VERSION_MINOR (1U) /********************************************************************************************************************** * Typedef definitions @@ -173,9 +173,8 @@ typedef struct st_cgc_pll_cfg * functions. */ typedef union u_cgc_divider_cfg { - uint32_t sckdivcr_w; ///< (@ 0x4001E020) System clock Division control register + uint32_t sckdivcr_w; ///< (@ 0x4001E020) System clock Division control register - /*LDRA_INSPECTED 381 S Anonymous structures and unions are allowed in FSP code. */ struct { cgc_sys_clock_div_t pclkd_div : 3; ///< Divider value for PCLKD diff --git a/ra/fsp/inc/api/r_ctsu_api.h b/ra/fsp/inc/api/r_ctsu_api.h index 58f6132e7..4e89c38fb 100644 --- a/ra/fsp/inc/api/r_ctsu_api.h +++ b/ra/fsp/inc/api/r_ctsu_api.h @@ -51,7 +51,7 @@ FSP_HEADER * Macro definitions **********************************************************************************************************************/ #define CTSU_API_VERSION_MAJOR (1U) -#define CTSU_API_VERSION_MINOR (1U) +#define CTSU_API_VERSION_MINOR (2U) /********************************************************************************************************************** * Typedef definitions @@ -191,6 +191,7 @@ typedef struct st_ctsu_cfg uint8_t num_tx; ///< Number of transmit terminals uint16_t num_moving_average; ///< Number of moving average for measurement data bool tunning_enable; ///< Initial offset tuning flag + bool judge_multifreq_disable; ///< Disable to judge multi frequency void (* p_callback)(ctsu_callback_args_t * p_args); ///< Callback provided when CTSUFN ISR occurs. transfer_instance_t const * p_transfer_tx; ///< DTC instance for transmit at CTSUWR. Set to NULL if unused. transfer_instance_t const * p_transfer_rx; ///< DTC instance for receive at CTSURD. Set to NULL if unused. diff --git a/ra/fsp/inc/api/r_usb_basic_api.h b/ra/fsp/inc/api/r_usb_basic_api.h index bf437f31d..74d00975a 100644 --- a/ra/fsp/inc/api/r_usb_basic_api.h +++ b/ra/fsp/inc/api/r_usb_basic_api.h @@ -58,7 +58,7 @@ FSP_HEADER ******************************************************************************/ /* USB Version Info */ -#define USB_API_VERSION_MINOR (1U) ///< Minor version of the API. +#define USB_API_VERSION_MINOR (2U) ///< Minor version of the API. #define USB_API_VERSION_MAJOR (1U) ///< Major version of the API. /* USB Request Type Register */ @@ -225,6 +225,8 @@ typedef enum e_usb_class { USB_CLASS_PCDC = 0x80, ///< PCDC Class USB_CLASS_PCDCC, ///< PCDCC Class + USB_CLASS_PCDC2, ///< PCDC2 Class + USB_CLASS_PCDCC2, ///< PCDCC2 Class USB_CLASS_PHID, ///< PHID Class USB_CLASS_PVND, ///< PVND Class USB_CLASS_HCDC, ///< HCDC Class diff --git a/ra/fsp/inc/api/rm_motor_angle_api.h b/ra/fsp/inc/api/rm_motor_angle_api.h new file mode 100644 index 000000000..ea143fd4a --- /dev/null +++ b/ra/fsp/inc/api/rm_motor_angle_api.h @@ -0,0 +1,197 @@ +/*********************************************************************************************************************** + * Copyright [2020] Renesas Electronics Corporation and/or its affiliates. All Rights Reserved. + * + * This software and documentation are supplied by Renesas Electronics America Inc. and may only be used with products + * of Renesas Electronics Corp. and its affiliates ("Renesas"). No other uses are authorized. Renesas products are + * sold pursuant to Renesas terms and conditions of sale. Purchasers are solely responsible for the selection and use + * of Renesas products and Renesas assumes no liability. No license, express or implied, to any intellectual property + * right is granted by Renesas. This software is protected under all applicable laws, including copyright laws. Renesas + * reserves the right to change or discontinue this software and/or this documentation. THE SOFTWARE AND DOCUMENTATION + * IS DELIVERED TO YOU "AS IS," AND RENESAS MAKES NO REPRESENTATIONS OR WARRANTIES, AND TO THE FULLEST EXTENT + * PERMISSIBLE UNDER APPLICABLE LAW, DISCLAIMS ALL WARRANTIES, WHETHER EXPLICITLY OR IMPLICITLY, INCLUDING WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NONINFRINGEMENT, WITH RESPECT TO THE SOFTWARE OR + * DOCUMENTATION. RENESAS SHALL HAVE NO LIABILITY ARISING OUT OF ANY SECURITY VULNERABILITY OR BREACH. TO THE MAXIMUM + * EXTENT PERMITTED BY LAW, IN NO EVENT WILL RENESAS BE LIABLE TO YOU IN CONNECTION WITH THE SOFTWARE OR DOCUMENTATION + * (OR ANY PERSON OR ENTITY CLAIMING RIGHTS DERIVED FROM YOU) FOR ANY LOSS, DAMAGES, OR CLAIMS WHATSOEVER, INCLUDING, + * WITHOUT LIMITATION, ANY DIRECT, CONSEQUENTIAL, SPECIAL, INDIRECT, PUNITIVE, OR INCIDENTAL DAMAGES; ANY LOST PROFITS, + * OTHER ECONOMIC DAMAGE, PROPERTY DAMAGE, OR PERSONAL INJURY; AND EVEN IF RENESAS HAS BEEN ADVISED OF THE POSSIBILITY + * OF SUCH LOSS, DAMAGES, CLAIMS OR COSTS. + **********************************************************************************************************************/ + +/*******************************************************************************************************************//** + * @ingroup RENESAS_INTERFACES + * @defgroup MOTOR_ANGLE_API Motor angle Interface + * @brief Interface for motor angle and speed calculation functions. + * + * @section MOTOR_ANGLE_API_Summary Summary + * The Motor angle interface calculates the rotor angle and rotational speed from other data. + * + * The motor angle interface can be implemented by: + * - @ref MOTOR_ESTIMATE + * + * @{ + **********************************************************************************************************************/ + +#ifndef RM_MOTOR_ANGLE_API_H +#define RM_MOTOR_ANGLE_API_H + +/*********************************************************************************************************************** + * Includes + **********************************************************************************************************************/ + +/* Includes board and MCU related header files. */ +#include "bsp_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 + **********************************************************************************************************************/ +#define MOTOR_ANGLE_API_VERSION_MAJOR (1U) +#define MOTOR_ANGLE_API_VERSION_MINOR (0U) + +/********************************************************************************************************************** + * Typedef definitions + **********************************************************************************************************************/ + +/** Motor Angle Control block. Allocate an instance specific control block to pass into the API calls. + * @par Implemented as + * - motor_angle_ctrl_t + */ +typedef void motor_angle_ctrl_t; + +/** Configuration parameters. */ +typedef struct st_motor_angle_cfg +{ + void const * p_context; + void const * p_extend; +} motor_angle_cfg_t; + +/** Interface structure **/ +typedef struct st_motor_angle_current +{ + float id; ///< d-axis current + float iq; ///< q-axis current +} motor_angle_current_t; + +typedef struct st_motor_angle_voltage_reference +{ + float vd; ///< d-axis voltage reference + float vq; ///< q-axis voltage reference +} motor_angle_voltage_reference_t; + +/** Functions implemented as application interface will follow these APIs. */ +typedef struct st_motor_angle_api +{ + /** Initialize the Motor_Angle. + * @par Implemented as + * - @ref RM_MOTOR_ESTIMATE_Open() + * + * @param[in] p_ctrl Pointer to control structure. + * @param[in] p_cfg Pointer to configuration structure. + */ + fsp_err_t (* open)(motor_angle_ctrl_t * const p_ctrl, motor_angle_cfg_t const * const p_cfg); + + /** Close (Finish) the Motor_Angle. + * @par Implemented as + * - @ref RM_MOTOR_ESTIMATE_Close() + * + * @param[in] p_ctrl Pointer to control structure. + */ + fsp_err_t (* close)(motor_angle_ctrl_t * const p_ctrl); + + /** Reset the Motor_Angle. + * @par Implemented as + * - @ref RM_MOTOR_ESTIMATE_Reset() + * + * @param[in] p_ctrl Pointer to control structure. + */ + fsp_err_t (* reset)(motor_angle_ctrl_t * const p_ctrl); + + /** Set (Input) Current & Voltage Reference data into the Motor_Angle. + * @par Implemented as + * - @ref RM_MOTOR_ESTIMATE_CurrentSet() + * + * @param[in] p_ctrl Pointer to control structure. + * @param[in] p_st_current Pointer to current structure + * @param[in] p_st_voltage Pointer to voltage Reference structure + */ + fsp_err_t (* currentSet)(motor_angle_ctrl_t * const p_ctrl, motor_angle_current_t * const p_st_current, + motor_angle_voltage_reference_t * const p_st_voltage); + + /** Set (Input) Speed Information into the Motor_Angle. + * @par Implemented as + * - @ref RM_MOTOR_ESTIMATE_SpeedSet() + * + * @param[in] p_ctrl Pointer to control structure. + * @param[in] speed_ctrl Control reference of rotational speed [rad/s] + * @param[in] damp_speed Damping rotational speed [rad/s] + */ + fsp_err_t (* speedSet)(motor_angle_ctrl_t * const p_ctrl, float const speed_ctrl, float const damp_speed); + + /** Set the flag of PI Control runs. + * @par Implemented as + * - @ref RM_MOTOR_ESTIMATE_FlagPiCtrlSet() + * + * @param[in] p_ctrl Pointer to control structure. + * @param[in] flag_pi The flag of PI control runs + */ + fsp_err_t (* flagPiCtrlSet)(motor_angle_ctrl_t * const p_ctrl, uint32_t const flag_pi); + + /** Get rotor angle and rotational speed from the Motor_Angle. + * @par Implemented as + * - @ref RM_MOTOR_ESTIMATE_AngleSpeedGet() + * + * @param[in] p_ctrl Pointer to control structure. + * @param[out] p_angl Memory address to get rotor angle data + * @param[out] p_speed Memory address to get rotational speed data + * @param[out] p_phase_err Memory address to get phase(angle) error data + */ + fsp_err_t (* angleSpeedGet)(motor_angle_ctrl_t * const p_ctrl, float * const p_angle, float * const p_speed, + float * const p_phase_err); + + /** Get estimated d/q-axis component from the Motor_Angle. + * @par Implemented as + * - @ref RM_MOTOR_ESTIMATE_EstimatedComponentGet() + * + * @param[in] p_ctrl Pointer to control structure. + * @param[out] p_ed Memory address to get estimated d-axis component + * @param[out] p_eq Memory address to get estimated q-axis component + */ + fsp_err_t (* estimatedComponentGet)(motor_angle_ctrl_t * const p_ctrl, float * const p_ed, float * const p_eq); + + /** Update Parameters for the calculation in the Motor_Angle. + * @par Implemented as + * - @ref RM_MOTOR_ESTIMATE_ParameterUpdate() + * + * @param[in] p_ctrl Pointer to control structure. + * @param[in] p_cfg Pointer to configuration structure include update parameters. + */ + fsp_err_t (* parameterUpdate)(motor_angle_ctrl_t * const p_ctrl, motor_angle_cfg_t const * p_cfg); + + /** Return the version of the driver. + * @par Implemented as + * - @ref RM_MOTOR_ESTIMATE_VersionGet() + * + * @param[out] p_version Memory address to return version information to. + */ + fsp_err_t (* versionGet)(fsp_version_t * const p_version); +} motor_angle_api_t; + +/** This structure encompasses everything that is needed to use an instance of this interface. */ +typedef struct st_motor_angle_instance +{ + motor_angle_ctrl_t * p_ctrl; ///< Pointer to the control structure for this instance + motor_angle_cfg_t const * p_cfg; ///< Pointer to the configuration structure for this instance + motor_angle_api_t const * p_api; ///< Pointer to the API structure for this instance +} motor_angle_instance_t; + +/*******************************************************************************************************************//** + * @} (end addtogroup MOTOR_ANGLE_API) + **********************************************************************************************************************/ + +/* Common macro for FSP header files. There is also a corresponding FSP_HEADER macro at the top of this file. */ +FSP_FOOTER + +#endif /* RM_MOTOR_ANGLE_API_H */ diff --git a/ra/fsp/inc/api/rm_motor_api.h b/ra/fsp/inc/api/rm_motor_api.h new file mode 100644 index 000000000..f912f91b2 --- /dev/null +++ b/ra/fsp/inc/api/rm_motor_api.h @@ -0,0 +1,215 @@ +/*********************************************************************************************************************** + * Copyright [2020] Renesas Electronics Corporation and/or its affiliates. All Rights Reserved. + * + * This software and documentation are supplied by Renesas Electronics America Inc. and may only be used with products + * of Renesas Electronics Corp. and its affiliates ("Renesas"). No other uses are authorized. Renesas products are + * sold pursuant to Renesas terms and conditions of sale. Purchasers are solely responsible for the selection and use + * of Renesas products and Renesas assumes no liability. No license, express or implied, to any intellectual property + * right is granted by Renesas. This software is protected under all applicable laws, including copyright laws. Renesas + * reserves the right to change or discontinue this software and/or this documentation. THE SOFTWARE AND DOCUMENTATION + * IS DELIVERED TO YOU "AS IS," AND RENESAS MAKES NO REPRESENTATIONS OR WARRANTIES, AND TO THE FULLEST EXTENT + * PERMISSIBLE UNDER APPLICABLE LAW, DISCLAIMS ALL WARRANTIES, WHETHER EXPLICITLY OR IMPLICITLY, INCLUDING WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NONINFRINGEMENT, WITH RESPECT TO THE SOFTWARE OR + * DOCUMENTATION. RENESAS SHALL HAVE NO LIABILITY ARISING OUT OF ANY SECURITY VULNERABILITY OR BREACH. TO THE MAXIMUM + * EXTENT PERMITTED BY LAW, IN NO EVENT WILL RENESAS BE LIABLE TO YOU IN CONNECTION WITH THE SOFTWARE OR DOCUMENTATION + * (OR ANY PERSON OR ENTITY CLAIMING RIGHTS DERIVED FROM YOU) FOR ANY LOSS, DAMAGES, OR CLAIMS WHATSOEVER, INCLUDING, + * WITHOUT LIMITATION, ANY DIRECT, CONSEQUENTIAL, SPECIAL, INDIRECT, PUNITIVE, OR INCIDENTAL DAMAGES; ANY LOST PROFITS, + * OTHER ECONOMIC DAMAGE, PROPERTY DAMAGE, OR PERSONAL INJURY; AND EVEN IF RENESAS HAS BEEN ADVISED OF THE POSSIBILITY + * OF SUCH LOSS, DAMAGES, CLAIMS OR COSTS. + **********************************************************************************************************************/ + +/*******************************************************************************************************************//** + * @ingroup RENESAS_INTERFACES + * @defgroup MOTOR_API Motor Interface + * @brief Interface for Motor functions. + * + * @section MOTOR_API_Summary Summary + * The Motor interface provides Motor functionality. + * + * Implemented by: + * - @ref MOTOR_SENSORLESS + * + * @{ + **********************************************************************************************************************/ + +#ifndef RM_MOTOR_API_H +#define RM_MOTOR_API_H + +/*********************************************************************************************************************** + * Includes + **********************************************************************************************************************/ + +/* Register definitions, common services and error codes. */ +#include "bsp_api.h" +#include "rm_motor_speed_api.h" +#include "rm_motor_current_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 + **********************************************************************************************************************/ +#define MOTOR_API_VERSION_MAJOR (1U) +#define MOTOR_API_VERSION_MINOR (0U) + +/********************************************************************************************************************** + * Typedef definitions + **********************************************************************************************************************/ + +/* Error information */ +typedef enum e_rm_motor_error +{ + MOTOR_ERROR_NONE = 0x0000, + MOTOR_ERROR_OVER_CURRENT_HW = 0x0001, + MOTOR_ERROR_OVER_VOLTAGE = 0x0002, + MOTOR_ERROR_OVER_SPEED = 0x0004, + MOTOR_ERROR_HALL_TIMEOUT = 0x0008, + MOTOR_ERROR_BEMF_TIMEOUT = 0x0010, + MOTOR_ERROR_HALL_PATTERN = 0x0020, + MOTOR_ERROR_BEMF_PATTERN = 0x0040, + MOTOR_ERROR_LOW_VOLTAGE = 0x0080, + MOTOR_ERROR_OVER_CURRENT_SW = 0x0100, + MOTOR_ERROR_UNKNOWN = 0xFFFF, +} motor_error_t; + +/** Motor Control block. Allocate an instance specific control block to pass into the API calls. + * @par Implemented as + * - motor_instance_ctrl_t + */ +typedef void motor_ctrl_t; + +/** Configuration parameters. */ +typedef struct st_motor_cfg +{ + motor_speed_instance_t const * p_motor_speed_instance; ///< Speed Instance + motor_current_instance_t const * p_motor_current_instance; ///< Current Instance + + /** Placeholder for user data. Passed to the user callback in motor_callback_args_t. */ + void const * p_context; + void const * p_extend; ///< Placeholder for user extension. +} motor_cfg_t; + +/** Functions implemented at the HAL layer will follow this API. */ +typedef struct st_motor_api +{ + /** Open driver. + * @par Implemented as + * - @ref RM_MOTOR_SENSORLESS_Open() + * + * @param[in] p_ctrl Pointer to control structure. + * @param[in] p_cfg Pointer to configuration structure. + */ + fsp_err_t (* open)(motor_ctrl_t * const p_ctrl, motor_cfg_t const * const p_cfg); + + /** Close driver. + * @par Implemented as + * - @ref RM_MOTOR_SENSORLESS_Close() + * + * @param[in] p_ctrl Pointer to control structure. + */ + fsp_err_t (* close)(motor_ctrl_t * const p_ctrl); + + /** Run the motor. (Start the motor rotation.) + * @par Implemented as + * - @ref RM_MOTOR_SENSORLESS_Run() + * + * @param[in] p_ctrl Pointer to control structure. + */ + fsp_err_t (* run)(motor_ctrl_t * const p_ctrl); + + /** Stop the motor. (Stop the motor rotation.) + * @par Implemented as + * - @ref RM_MOTOR_SENSORLESS_Stop() + * + * @param[in] p_ctrl Pointer to control structure. + */ + fsp_err_t (* stop)(motor_ctrl_t * const p_ctrl); + + /** Reset the motor control. (Recover from the error status.) + * @par Implemented as + * - @ref RM_MOTOR_SENSORLESS_Reset() + * + * @param[in] p_ctrl Pointer to control structure. + */ + fsp_err_t (* reset)(motor_ctrl_t * const p_ctrl); + + /** Set Error Information. + * @par Implemented as + * - @ref RM_MOTOR_SENSORLESS_ErrorSet() + * + * @param[in] p_ctrl Pointer to control structure. + * @param[in] error Happend error code + */ + fsp_err_t (* errorSet)(motor_ctrl_t * const p_ctrl, motor_error_t const error); + + /** Set rotation speed. + * @par Implemented as + * - @ref RM_MOTOR_SENSORLESS_SpeedSet() + * + * @param[in] p_ctrl Pointer to control structure. + * @param[in] speed_rpm Required rotation speed [rpm] + */ + fsp_err_t (* speedSet)(motor_ctrl_t * const p_ctrl, float const speed_rpm); + + /** Get the motor control status. + * @par Implemented as + * - @ref RM_MOTOR_SENSORLESS_StatusGet() + * + * @param[in] p_ctrl Pointer to control structure. + * @param[out] p_status Pointer to get the motor control status + */ + fsp_err_t (* statusGet)(motor_ctrl_t * const p_ctrl, uint8_t * const p_status); + + /** Get the rotor angle. + * @par Implemented as + * - @ref RM_MOTOR_SENSORLESS_AngleGet() + * + * @param[in] p_ctrl Pointer to control structure. + * @param[out] p_angle_rad Pointer to get the rotor angle [rad] + */ + fsp_err_t (* angleGet)(motor_ctrl_t * const p_ctrl, float * const p_angle_rad); + + /** Get the rotation speed. + * @par Implemented as + * - @ref RM_MOTOR_SENSORLESS_SpeedGet() + * + * @param[in] p_ctrl Pointer to control structure. + * @param[out] p_speed_rpm Pointer to get the rotation speed [rpm] + */ + fsp_err_t (* speedGet)(motor_ctrl_t * const p_ctrl, float * const p_speed_rpm); + + /** Check the error occurrence + * @par Implemented as + * - @ref RM_MOTOR_SENSORLESS_ErrorCheck() + * + * @param[in] p_ctrl Pointer to control structure. + * @param[out] p_error Pointer to get occured error + */ + fsp_err_t (* errorCheck)(motor_ctrl_t * const p_ctrl, uint16_t * const p_error); + + /** Return the version of the driver. + * @par Implemented as + * - @ref RM_MOTOR_SENSORLESS_VersionGet() + * + * @param[out] p_version Memory address to return version information to. + */ + fsp_err_t (* versionGet)(fsp_version_t * const p_version); +} motor_api_t; + +/** This structure encompasses everything that is needed to use an instance of this interface. */ +typedef struct st_motor_instance +{ + motor_ctrl_t * p_ctrl; ///< Pointer to the control structure for this instance + motor_cfg_t const * p_cfg; ///< Pointer to the configuration structure for this instance + motor_api_t const * p_api; ///< Pointer to the API structure for this instance +} motor_instance_t; + +/*******************************************************************************************************************//** + * @} (end addtogroup MOTOR_API) + **********************************************************************************************************************/ + +/* Common macro for FSP header files. There is also a corresponding FSP_HEADER macro at the top of this file. */ +FSP_FOOTER + +#endif /* RM_MOTOR_API_H */ diff --git a/ra/fsp/inc/api/rm_motor_current_api.h b/ra/fsp/inc/api/rm_motor_current_api.h new file mode 100644 index 000000000..ad3e96a8d --- /dev/null +++ b/ra/fsp/inc/api/rm_motor_current_api.h @@ -0,0 +1,287 @@ +/*********************************************************************************************************************** + * Copyright [2020] Renesas Electronics Corporation and/or its affiliates. All Rights Reserved. + * + * This software and documentation are supplied by Renesas Electronics America Inc. and may only be used with products + * of Renesas Electronics Corp. and its affiliates ("Renesas"). No other uses are authorized. Renesas products are + * sold pursuant to Renesas terms and conditions of sale. Purchasers are solely responsible for the selection and use + * of Renesas products and Renesas assumes no liability. No license, express or implied, to any intellectual property + * right is granted by Renesas. This software is protected under all applicable laws, including copyright laws. Renesas + * reserves the right to change or discontinue this software and/or this documentation. THE SOFTWARE AND DOCUMENTATION + * IS DELIVERED TO YOU "AS IS," AND RENESAS MAKES NO REPRESENTATIONS OR WARRANTIES, AND TO THE FULLEST EXTENT + * PERMISSIBLE UNDER APPLICABLE LAW, DISCLAIMS ALL WARRANTIES, WHETHER EXPLICITLY OR IMPLICITLY, INCLUDING WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NONINFRINGEMENT, WITH RESPECT TO THE SOFTWARE OR + * DOCUMENTATION. RENESAS SHALL HAVE NO LIABILITY ARISING OUT OF ANY SECURITY VULNERABILITY OR BREACH. TO THE MAXIMUM + * EXTENT PERMITTED BY LAW, IN NO EVENT WILL RENESAS BE LIABLE TO YOU IN CONNECTION WITH THE SOFTWARE OR DOCUMENTATION + * (OR ANY PERSON OR ENTITY CLAIMING RIGHTS DERIVED FROM YOU) FOR ANY LOSS, DAMAGES, OR CLAIMS WHATSOEVER, INCLUDING, + * WITHOUT LIMITATION, ANY DIRECT, CONSEQUENTIAL, SPECIAL, INDIRECT, PUNITIVE, OR INCIDENTAL DAMAGES; ANY LOST PROFITS, + * OTHER ECONOMIC DAMAGE, PROPERTY DAMAGE, OR PERSONAL INJURY; AND EVEN IF RENESAS HAS BEEN ADVISED OF THE POSSIBILITY + * OF SUCH LOSS, DAMAGES, CLAIMS OR COSTS. + **********************************************************************************************************************/ + +/*******************************************************************************************************************//** + * @ingroup RENESAS_INTERFACES + * @defgroup MOTOR_CURRENT_API Motor current Interface + * @brief Interface for motor current functions. + * + * @section MOTOR_CURRENT_API_Summary Summary + * The Motor current interface for getting the PWM modulation duty from electric current and speed + * + * The motor current control interface can be implemented by: + * - @ref MOTOR_CURRENT + * + * @{ + **********************************************************************************************************************/ + +#ifndef RM_MOTOR_CURRENT_API_H +#define RM_MOTOR_CURRENT_API_H + +/*********************************************************************************************************************** + * Includes + **********************************************************************************************************************/ + +/* Register definitions, common services and error codes. */ +#include "bsp_api.h" +#include "rm_motor_driver_api.h" +#include "rm_motor_angle_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 + **********************************************************************************************************************/ +#define MOTOR_CURRENT_API_VERSION_MAJOR (1U) +#define MOTOR_CURRENT_API_VERSION_MINOR (0U) + +/********************************************************************************************************************** + * Typedef definitions + **********************************************************************************************************************/ + +/** Events that can trigger a callback function */ +typedef enum e_motor_current_event +{ + MOTOR_CURRENT_EVENT_FORWARD = 1, ///< Event forward Speed Control + MOTOR_CURRENT_EVENT_DATA_SET, ///< Event Set Speed Control Output Data + MOTOR_CURRENT_EVENT_BACKWARD, ///< Event backward Speed Control +} motor_current_event_t; + +typedef struct st_motor_current_callback_args +{ + void const * p_context; + motor_current_event_t event; +} motor_current_callback_args_t; + +/* Structure of interface to speed control */ +/* Output parameters */ +typedef struct st_motor_current_output +{ + float f_id; ///< D-axis current [A] + float f_iq; ///< Q-axis current [A] + float f_vamax; + float f_speed_rad; ///< Speed value [rad/s] + float f_speed_rpm; ///< Speed value [rpm] + float f_rotor_angle; ///< Motor rotor angle [rad] + + float f_ed; ///< Estimated d-axis component[V] of flux due to the permanent magnet + float f_eq; ///< Estimated q-axis component[V] of flux due to the permanent magnet + float f_phase_err_rad; ///< Phase Error [rad] + + uint8_t u1_flag_get_iref; ///< Flag to set d/q-axis current reference +} motor_current_output_t; + +/* Input parameters */ +typedef struct st_motor_current_input +{ + float f_id_ref; ///< D-axis current reference [A] for calculation + float f_iq_ref; ///< Q-axis current reference [A] for calculation + + float f_ref_speed_rad_ctrl; ///< Command speed value for speed PI control[rad/s] + float f_damp_comp_speed; ///< The speed for open-loop damping + + uint8_t u1_flag_pi; ///< PI output started flag +} motor_current_input_t; + +typedef struct st_motor_current_input_current +{ + float iu; ///< U phase current[A] + float iv; ///< V phase current[A] + float iw; ///< W phase current[A] +} motor_current_input_current_t; + +typedef struct st_motor_current_input_voltage +{ + float vdc; ///< Main Line Voltage[V] + float va_max; ///< Maximum magnitude of voltage vector[V] +} motor_current_input_voltage_t; + +typedef struct st_motor_current_get_voltage +{ + float u_voltage; ///< U phase voltage[V] + float v_voltage; ///< V phase voltage[V] + float w_voltage; ///< W phase voltage[V] + float vd_reference; ///< d-axis voltage reference + float vq_reference; ///< q-axis voltage reference +} motor_current_get_voltage_t; + +/** Control block. Allocate an instance specific control block to pass into the API calls. + * @par Implemented as + * - motor_current_ctrl_t + */ +typedef void motor_current_ctrl_t; + +/** Configuration parameters. */ +typedef struct st_motor_current_cfg +{ + /* Motor Driver Access Module */ + motor_driver_instance_t const * p_motor_driver_instance; + + /* Motor Angle Module */ + motor_angle_instance_t const * p_motor_angle_instance; + + /* Callback function */ + void (* p_callback)(motor_current_callback_args_t * p_args); + void const * p_context; + + void const * p_extend; +} motor_current_cfg_t; + +/** Functions implemented at the Motor Current Module will follow these APIs. */ +typedef struct st_motor_current_api +{ + /** Initialize the Motor Current Module. + * @par Implemented as + * - @ref RM_MOTOR_CURRENT_Open() + * + * @param[in] p_ctrl Pointer to control structure. + * @param[in] p_cfg Pointer to configuration structure. + */ + fsp_err_t (* open)(motor_current_ctrl_t * const p_ctrl, motor_current_cfg_t const * const p_cfg); + + /** Close (Finish) the Motor Current Module. + * @par Implemented as + * - @ref RM_MOTOR_CURRENT_Close() + * + * @param[in] p_ctrl Pointer to control structure. + */ + fsp_err_t (* close)(motor_current_ctrl_t * const p_ctrl); + + /** Reset variables for the Motor Current Module. + * @par Implemented as + * - @ref RM_MOTOR_CURRENT_Reset() + * + * @param[in] p_ctrl Pointer to control structure. + */ + fsp_err_t (* reset)(motor_current_ctrl_t * const p_ctrl); + + /** Activate the Motor Current Control. + * @par Implemented as + * - @ref RM_MOTOR_CURRENT_Run() + * + * @param[in] p_ctrl Pointer to control structure. + */ + fsp_err_t (* run)(motor_current_ctrl_t * const p_ctrl); + + /** Set (Input) parameters into the Motor Current Module. + * @par Implemented as + * - @ref RM_MOTOR_CURRENT_ParameterSet() + * + * @param[in] p_ctrl Pointer to control structure. + * @param[in] p_st_input Pointer to input data structure(speed control output data) + */ + fsp_err_t (* parameterSet)(motor_current_ctrl_t * const p_ctrl, motor_current_input_t const * const p_st_input); + + /** Set (Input) Current reference into the Motor Current Module. + * @par Implemented as + * - @ref RM_MOTOR_CURRENT_CurrentReferenceSet() + * + * @param[in] p_ctrl Pointer to control structure. + * @param[in] id_reference D-axis current Reference [A] + * @param[in] iq_reference Q-axis current Reference [A] + */ + fsp_err_t (* currentReferenceSet)(motor_current_ctrl_t * const p_ctrl, float const id_reference, + float const iq_reference); + + /** Set (Input) Speed & Phase data into the Motor Current Module. + * @par Implemented as + * - @ref RM_MOTOR_CURRENT_SpeedPhaseSet() + * + * @param[in] p_ctrl Pointer to control structure. + * @param[in] speed_rad Rotational speed [rad/s] + * @param[in] phase_rad Rotor phase [rad] + */ + fsp_err_t (* speedPhaseSet)(motor_current_ctrl_t * const p_ctrl, float const speed_rad, float const phase_rad); + + /** Set (Input) Current data into the Motor Current Module. + * @par Implemented as + * - @ref RM_MOTOR_CURRENT_CurrentSet() + * + * @param[in] p_ctrl Pointer to control structure. + * @param[in] p_st_current Pointer to input current structure + * @param[in] p_st_voltage Pointer to input voltage structure + */ + fsp_err_t (* currentSet)(motor_current_ctrl_t * const p_ctrl, + motor_current_input_current_t const * const p_st_current, + motor_current_input_voltage_t const * const p_st_voltage); + + /** Get (output) parameters from the Motor Current Module + * @par Implemented as + * - @ref RM_MOTOR_CURRENT_ParameterGet() + * + * @param[in] p_ctrl Pointer to control structure. + * @param[out] p_st_output Pointer to output data structure(speed control input data) + */ + fsp_err_t (* parameterGet)(motor_current_ctrl_t * const p_ctrl, motor_current_output_t * const p_st_output); + + /** Get d/q-axis current + * @par Implemented as + * - @ref RM_MOTOR_CURRENT_CurrentGet() + * + * @param[in] p_ctrl Pointer to control structure. + * @param[out] p_id Pointer to get d-axis current [A] + * @param[out] p_iq Pointer to get q-axis current [A] + */ + fsp_err_t (* currentGet)(motor_current_ctrl_t * const p_ctrl, float * const p_id, float * const p_iq); + + /** Get Phase Output Voltage + * @par Implemented as + * - @ref RM_MOTOR_CURRENT_PhaseVoltageGet() + * + * @param[in] p_ctrl Pointer to control structure. + * @param[out] p_voltage Pointer to get Voltages + */ + fsp_err_t (* phaseVoltageGet)(motor_current_ctrl_t * const p_ctrl, motor_current_get_voltage_t * const p_voltage); + + /** Update Parameters for the calculation in the Motor Current Control. + * @par Implemented as + * - @ref RM_MOTOR_CURRENT_ParameterUpdate() + * + * @param[in] p_ctrl Pointer to control structure. + * @param[in] p_cfg Pointer to configuration structure include update parameters. + */ + fsp_err_t (* parameterUpdate)(motor_current_ctrl_t * const p_ctrl, motor_current_cfg_t const * const p_cfg); + + /** Return the version of the driver. + * @par Implemented as + * - @ref RM_MOTOR_CURRENT_VersionGet() + * + * @param[out] p_version Memory address to return version information to. + */ + fsp_err_t (* versionGet)(fsp_version_t * const p_version); +} motor_current_api_t; + +/** This structure encompasses everything that is needed to use an instance of this interface. */ +typedef struct st_motor_current_instance +{ + motor_current_ctrl_t * p_ctrl; ///< Pointer to the control structure for this instance + motor_current_cfg_t const * p_cfg; ///< Pointer to the configuration structure for this instance + motor_current_api_t const * p_api; ///< Pointer to the API structure for this instance +} motor_current_instance_t; + +/*******************************************************************************************************************//** + * @} (end addtogroup MOTOR_CURRENT_API) + **********************************************************************************************************************/ + +/* Common macro for FSP header files. There is also a corresponding FSP_HEADER macro at the top of this file. */ +FSP_FOOTER + +#endif /* RM_MOTOR_CURRENT_API_H */ diff --git a/ra/fsp/inc/api/rm_motor_driver_api.h b/ra/fsp/inc/api/rm_motor_driver_api.h new file mode 100644 index 000000000..545544a01 --- /dev/null +++ b/ra/fsp/inc/api/rm_motor_driver_api.h @@ -0,0 +1,208 @@ +/*********************************************************************************************************************** + * Copyright [2020] Renesas Electronics Corporation and/or its affiliates. All Rights Reserved. + * + * This software and documentation are supplied by Renesas Electronics America Inc. and may only be used with products + * of Renesas Electronics Corp. and its affiliates ("Renesas"). No other uses are authorized. Renesas products are + * sold pursuant to Renesas terms and conditions of sale. Purchasers are solely responsible for the selection and use + * of Renesas products and Renesas assumes no liability. No license, express or implied, to any intellectual property + * right is granted by Renesas. This software is protected under all applicable laws, including copyright laws. Renesas + * reserves the right to change or discontinue this software and/or this documentation. THE SOFTWARE AND DOCUMENTATION + * IS DELIVERED TO YOU "AS IS," AND RENESAS MAKES NO REPRESENTATIONS OR WARRANTIES, AND TO THE FULLEST EXTENT + * PERMISSIBLE UNDER APPLICABLE LAW, DISCLAIMS ALL WARRANTIES, WHETHER EXPLICITLY OR IMPLICITLY, INCLUDING WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NONINFRINGEMENT, WITH RESPECT TO THE SOFTWARE OR + * DOCUMENTATION. RENESAS SHALL HAVE NO LIABILITY ARISING OUT OF ANY SECURITY VULNERABILITY OR BREACH. TO THE MAXIMUM + * EXTENT PERMITTED BY LAW, IN NO EVENT WILL RENESAS BE LIABLE TO YOU IN CONNECTION WITH THE SOFTWARE OR DOCUMENTATION + * (OR ANY PERSON OR ENTITY CLAIMING RIGHTS DERIVED FROM YOU) FOR ANY LOSS, DAMAGES, OR CLAIMS WHATSOEVER, INCLUDING, + * WITHOUT LIMITATION, ANY DIRECT, CONSEQUENTIAL, SPECIAL, INDIRECT, PUNITIVE, OR INCIDENTAL DAMAGES; ANY LOST PROFITS, + * OTHER ECONOMIC DAMAGE, PROPERTY DAMAGE, OR PERSONAL INJURY; AND EVEN IF RENESAS HAS BEEN ADVISED OF THE POSSIBILITY + * OF SUCH LOSS, DAMAGES, CLAIMS OR COSTS. + **********************************************************************************************************************/ + +/*******************************************************************************************************************//** + * @ingroup RENESAS_INTERFACES + * @defgroup MOTOR_DRIVER_API Motor driver Interface + * @brief Interface for motor driver functions. + * + * @section MOTOR_DRIVER_API_Summary Summary + * The Motor driver interface for setting the PWM modulation duty + * + * The motor current control interface can be implemented by: + * - @ref MOTOR_DRIVER + * + * @{ + **********************************************************************************************************************/ + +#ifndef RM_MOTOR_DRIVER_API_H +#define RM_MOTOR_DRIVER_API_H + +/*********************************************************************************************************************** + * Includes + **********************************************************************************************************************/ + +/* Register definitions, common services and error codes. */ +#include "bsp_api.h" +#include "r_adc_api.h" +#include "r_three_phase_api.h" +#include "r_elc_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 + **********************************************************************************************************************/ +#define MOTOR_DRIVER_API_VERSION_MAJOR (1U) +#define MOTOR_DRIVER_API_VERSION_MINOR (0U) + +/********************************************************************************************************************** + * Typedef definitions + **********************************************************************************************************************/ + +/** Events that can trigger a callback function */ +typedef enum e_motor_driver_event +{ + MOTOR_DRIVER_EVENT_FORWARD = 1, ///< Event before Motor Driver Process (before Current Control timing) + MOTOR_DRIVER_EVENT_CURRENT, ///< Event Current Control timing + MOTOR_DRIVER_EVENT_BACKWARD, ///< Event after Motor Driver Process (after PWM duty setting) +} motor_driver_event_t; + +/** Callback function parameter data */ +typedef struct st_motor_driver_callback_args +{ + motor_driver_event_t event; ///< Event trigger + void const * p_context; ///< Placeholder for user data. +} motor_driver_callback_args_t; + +/** Current Data Get Structure */ +typedef struct st_motor_driver_current_get +{ + float iu; ///< U phase current [A] + float iw; ///< W phase current [A] + float vdc; ///< Main Line Voltage [V] + float va_max; ///< maximum magnitude of voltage vector +} motor_driver_current_get_t; + +/** Control block. Allocate an instance specific control block to pass into the API calls. + * @par Implemented as + * - motor_driver_ctrl_t + */ +typedef void motor_driver_ctrl_t; + +/** Configuration parameters. */ +typedef struct st_motor_driver_cfg +{ + /* ADC Module */ + adc_instance_t const * p_adc_instance; + adc_channel_t iu_ad_ch; ///< A/D Channel for U Phase Current + adc_channel_t iw_ad_ch; ///< A/D Channel for W Phase Current + adc_channel_t vdc_ad_ch; ///< A/D Channel for Main Line Voltage + + /* PWM Output Module (GPT THREE PHASE) */ + three_phase_instance_t const * p_three_phase_instance; + + void (* p_callback)(motor_driver_callback_args_t * p_args); + void const * p_context; ///< Placeholder for user data. + + void const * p_extend; +} motor_driver_cfg_t; + +/** Functions implemented at the HAL layer will follow these APIs. */ +typedef struct st_motor_driver_api +{ + /** Initialize the Motor Driver Module. + * @par Implemented as + * - @ref RM_MOTOR_DRIVER_Open() + * + * @param[in] p_ctrl Pointer to control structure. + * @param[in] p_cfg Pointer to configuration structure. + */ + fsp_err_t (* open)(motor_driver_ctrl_t * const p_ctrl, motor_driver_cfg_t const * const p_cfg); + + /** Close the Motor Driver Module + * @par Implemented as + * - @ref RM_MOTOR_DRIVER_Close() + * + * @param[in] p_ctrl Pointer to control structure. + */ + fsp_err_t (* close)(motor_driver_ctrl_t * const p_ctrl); + + /** Reset variables of the Motor Driver Module + * @par Implemented as + * - @ref RM_MOTOR_DRIVER_Reset() + * + * @param[in] p_ctrl Pointer to control structure. + */ + fsp_err_t (* reset)(motor_driver_ctrl_t * const p_ctrl); + + /** Set (Input) Phase Voltage data into the Motor Driver Module + * @par Implemented as + * - @ref RM_MOTOR_DRIVER_PhaseVoltageSet() + * + * @param[in] p_ctrl Pointer to control structure. + * @param[in] u_voltage U phase voltage [V] + * @param[in] v_voltage V phase voltage [V] + * @param[in] w_voltage W phase voltage [V] + */ + fsp_err_t (* phaseVoltageSet)(motor_driver_ctrl_t * const p_ctrl, float const u_voltage, float const v_voltage, + float const w_voltage); + + /** Get Phase current, Vdc and Va_max data from the Motor Driver Module + * @par Implemented as + * - @ref RM_MOTOR_DRIVER_CurrentGet() + * + * @param[in] p_ctrl Pointer to control structure. + * @param[out] p_current_get Pointer to get data structure. + */ + fsp_err_t (* currentGet)(motor_driver_ctrl_t * const p_ctrl, motor_driver_current_get_t * const p_current_get); + + /** Get the flag of finish current offset detection from the Motor Driver Module + * @par Implemented as + * - @ref RM_MOTOR_DRIVER_FlagCurrentOffsetGet() + * + * @param[in] p_ctrl Pointer to control structure. + * @param[out] p_flag_offset Flag of finish current offset detection + */ + fsp_err_t (* flagCurrentOffsetGet)(motor_driver_ctrl_t * const p_ctrl, uint8_t * const p_flag_offset); + + /** Restart current offset detection + * @par Implemented as + * - @ref RM_MOTOR_DRIVER_CurrentOffsetRestart() + * + * @param[in] p_ctrl Pointer to control structure. + */ + fsp_err_t (* currentOffsetRestart)(motor_driver_ctrl_t * const p_ctrl); + + /** Update Configuration Parameters for the calculation in the Motor Driver Module + * @par Implemented as + * - @ref RM_MOTOR_DRIVER_ParameterUpdate() + * + * @param[in] p_ctrl Pointer to control structure. + * @param[in] p_cfg Pointer to configuration structure include update parameters. + */ + fsp_err_t (* parameterUpdate)(motor_driver_ctrl_t * const p_ctrl, motor_driver_cfg_t const * const p_cfg); + + /** Return the version of the driver. + * @par Implemented as + * - @ref RM_MOTOR_DRIVER_VersionGet() + * + * @param[out] p_version Memory address to return version information to. + */ + fsp_err_t (* versionGet)(fsp_version_t * const p_version); +} motor_driver_api_t; + +/** This structure encompasses everything that is needed to use an instance of this interface. */ +typedef struct st_motor_driver_instance +{ + motor_driver_ctrl_t * p_ctrl; ///< Pointer to the control structure for this instance + motor_driver_cfg_t const * p_cfg; ///< Pointer to the configuration structure for this instance + motor_driver_api_t const * p_api; ///< Pointer to the API structure for this instance +} motor_driver_instance_t; + +/*******************************************************************************************************************//** + * @} (end addtogroup MOTOR_DRIVER_API) + **********************************************************************************************************************/ + +/* Common macro for FSP header files. There is also a corresponding FSP_HEADER macro at the top of this file. */ +FSP_FOOTER + +#endif /* RM_MOTOR_DRIVER_API_H */ diff --git a/ra/fsp/inc/api/rm_motor_speed_api.h b/ra/fsp/inc/api/rm_motor_speed_api.h new file mode 100644 index 000000000..4166af15a --- /dev/null +++ b/ra/fsp/inc/api/rm_motor_speed_api.h @@ -0,0 +1,225 @@ +/*********************************************************************************************************************** + * Copyright [2020] Renesas Electronics Corporation and/or its affiliates. All Rights Reserved. + * + * This software and documentation are supplied by Renesas Electronics America Inc. and may only be used with products + * of Renesas Electronics Corp. and its affiliates ("Renesas"). No other uses are authorized. Renesas products are + * sold pursuant to Renesas terms and conditions of sale. Purchasers are solely responsible for the selection and use + * of Renesas products and Renesas assumes no liability. No license, express or implied, to any intellectual property + * right is granted by Renesas. This software is protected under all applicable laws, including copyright laws. Renesas + * reserves the right to change or discontinue this software and/or this documentation. THE SOFTWARE AND DOCUMENTATION + * IS DELIVERED TO YOU "AS IS," AND RENESAS MAKES NO REPRESENTATIONS OR WARRANTIES, AND TO THE FULLEST EXTENT + * PERMISSIBLE UNDER APPLICABLE LAW, DISCLAIMS ALL WARRANTIES, WHETHER EXPLICITLY OR IMPLICITLY, INCLUDING WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NONINFRINGEMENT, WITH RESPECT TO THE SOFTWARE OR + * DOCUMENTATION. RENESAS SHALL HAVE NO LIABILITY ARISING OUT OF ANY SECURITY VULNERABILITY OR BREACH. TO THE MAXIMUM + * EXTENT PERMITTED BY LAW, IN NO EVENT WILL RENESAS BE LIABLE TO YOU IN CONNECTION WITH THE SOFTWARE OR DOCUMENTATION + * (OR ANY PERSON OR ENTITY CLAIMING RIGHTS DERIVED FROM YOU) FOR ANY LOSS, DAMAGES, OR CLAIMS WHATSOEVER, INCLUDING, + * WITHOUT LIMITATION, ANY DIRECT, CONSEQUENTIAL, SPECIAL, INDIRECT, PUNITIVE, OR INCIDENTAL DAMAGES; ANY LOST PROFITS, + * OTHER ECONOMIC DAMAGE, PROPERTY DAMAGE, OR PERSONAL INJURY; AND EVEN IF RENESAS HAS BEEN ADVISED OF THE POSSIBILITY + * OF SUCH LOSS, DAMAGES, CLAIMS OR COSTS. + **********************************************************************************************************************/ + +/*******************************************************************************************************************//** + * @ingroup RENESAS_INTERFACES + * @defgroup MOTOR_SPEED_API Motor speed Interface + * @brief Interface for motor speed functions. + * + * @section MOTOR_SPEED_API_Summary Summary + * The Motor speed interface for getting the current references from electric current and rotational speed + * + * The motor speed interface can be implemented by: + * - @ref MOTOR_SPEED + * + * @{ + **********************************************************************************************************************/ + +#ifndef RM_MOTOR_SPEED_API_H +#define RM_MOTOR_SPEED_API_H + +/*********************************************************************************************************************** + * Includes + **********************************************************************************************************************/ + +/* Register definitions, common services and error codes. */ +#include "bsp_api.h" +#include "r_timer_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 + **********************************************************************************************************************/ +#define MOTOR_SPEED_API_VERSION_MAJOR (1U) +#define MOTOR_SPEED_API_VERSION_MINOR (0U) + +/********************************************************************************************************************** + * Typedef definitions + **********************************************************************************************************************/ + +/** Events that can trigger a callback function */ +typedef enum e_motor_speed_event +{ + MOTOR_SPEED_EVENT_FORWARD = 1, ///< Event forward Speed Control + MOTOR_SPEED_EVENT_BACKWARD, ///< Event backward Speed Control +} motor_speed_event_t; + +/** Callback function parameter data */ +typedef struct st_rm_motor_speed_callback_args +{ + void const * p_context; ///< Placeholder for user data. + motor_speed_event_t event; +} motor_speed_callback_args_t; + +/* Structure of input parameters */ +typedef struct st_motor_speed_input +{ + float f_id; ///< D-axis current [A] + float f_iq; ///< Q-axis current [A] + float f_vamax; + float f_speed_rad; ///< Speed value [rad/s] + + float f_ed; ///< Estimated d-axis component[V] of flux due to the permanent magnet + float f_eq; ///< Estimated q-axis component[V] of flux due to the permanent magnet + float f_phase_err_rad; ///< Phase Error [rad] + + uint8_t u1_flag_get_iref; ///< Flag to set d/q-axis current reference +} motor_speed_input_t; + +/* Structure of output parameters */ +typedef struct st_motor_speed_output +{ + float f_id_ref; ///< D-axis current reference [A] for calculation + float f_iq_ref; ///< Q-axis current reference [A] for calculation + + float f_ref_speed_rad_ctrl; ///< Command speed value for speed PI control[rad/s] + float f_damp_comp_speed; ///< The speed for open-loop damping + + uint8_t u1_flag_pi; ///< PI output started flag +} motor_speed_output_t; + +/** Control block. Allocate an instance specific control block to pass into the API calls. + * @par Implemented as + * - motor_speed_ctrl_t + */ +typedef void motor_speed_ctrl_t; + +/** Configuration parameters. */ +typedef struct st_motor_speed_cfg +{ + motor_speed_input_t * st_input; ///< Input data structure for automatic set + motor_speed_output_t * st_output; ///< Output data structure for automatic receive + + timer_instance_t const * p_timer_instance; + + /* Callback function to perform at cyclic timer interrupt */ + void (* p_callback)(motor_speed_callback_args_t * p_args); + void const * p_context; ///< Placeholder for user data. + + void const * p_extend; +} motor_speed_cfg_t; + +/** Functions implemented at the HAL layer will follow these APIs. */ +typedef struct st_motor_speed_api +{ + /** Initialize the Motor Speed Module. + * @par Implemented as + * - @ref RM_MOTOR_SPEED_Open() + * + * @param[in] p_ctrl Pointer to control structure. + * @param[in] p_cfg Pointer to configuration structure. + */ + fsp_err_t (* open)(motor_speed_ctrl_t * const p_ctrl, motor_speed_cfg_t const * const p_cfg); + + /** Close (Finish) the Motor Speed Module. + * @par Implemented as + * - @ref RM_MOTOR_SPEED_Close() + * + * @param[in] p_ctrl Pointer to control structure. + */ + fsp_err_t (* close)(motor_speed_ctrl_t * const p_ctrl); + + /** Reset(Stop) the Motor Speed Module. + * @par Implemented as + * - @ref RM_MOTOR_SPEED_Reset() + * + * @param[in] p_ctrl Pointer to control structure. + */ + fsp_err_t (* reset)(motor_speed_ctrl_t * const p_ctrl); + + /** Activate the Motor Speed Control. + * @par Implemented as + * - @ref RM_MOTOR_SPEED_Run() + * + * @param[in] p_ctrl Pointer to control structure. + */ + fsp_err_t (* run)(motor_speed_ctrl_t * const p_ctrl); + + /** Set (Input) Speed reference into the Motor Speed Module. + * @par Implemented as + * - @ref RM_MOTOR_SPEED_SpeedReferenceSet() + * + * @param[in] p_ctrl Pointer to control structure. + * @param[in] speed_refernce_rpm Speed reference [rpm] + */ + fsp_err_t (* speedReferenceSet)(motor_speed_ctrl_t * const p_ctrl, float const speed_reference_rpm); + + /** Set (Input) Speed Parameters into the Motor Speed Module. + * @par Implemented as + * - @ref RM_MOTOR_SPEED_ParameterSet() + * + * @param[in] p_ctrl Pointer to control structure. + * @param[in] p_st_input Pointer to structure to input parameters. + */ + fsp_err_t (* parameterSet)(motor_speed_ctrl_t * const p_ctrl, motor_speed_input_t const * const p_st_input); + + /** Calculate Current Reference + * @par Implemented as + * - @ref RM_MOTOR_SPEED_SpeedControl() + * + * @param[in] p_ctrl Pointer to control structure. + */ + fsp_err_t (* speedControl)(motor_speed_ctrl_t * const p_ctrl); + + /** Get Speed Control Output Parameters + * @par Implemented as + * - @ref RM_MOTOR_SPEED_ParameterGet() + * + * @param[in] p_ctrl Pointer to control structure. + * @param[out] p_st_output Pointer to get speed control parameters + */ + fsp_err_t (* parameterGet)(motor_speed_ctrl_t * const p_ctrl, motor_speed_output_t * const p_st_output); + + /** Update Parameters for the calculation in the Motor Speed Module. + * @par Implemented as + * - @ref RM_MOTOR_SPEED_ParameterUpdate() + * + * @param[in] p_ctrl Pointer to control structure. + * @param[in] p_cfg Pointer to configuration structure include update parameters. + */ + fsp_err_t (* parameterUpdate)(motor_speed_ctrl_t * const p_ctrl, motor_speed_cfg_t const * const p_cfg); + + /** Return the version of the driver. + * @par Implemented as + * - @ref RM_MOTOR_SPEED_VersionGet() + * + * @param[out] p_version Memory address to return version information to. + */ + fsp_err_t (* versionGet)(fsp_version_t * const p_version); +} motor_speed_api_t; + +/** This structure encompasses everything that is needed to use an instance of this interface. */ +typedef struct st_motor_speed_instance +{ + motor_speed_ctrl_t * p_ctrl; ///< Pointer to the control structure for this instance + motor_speed_cfg_t const * p_cfg; ///< Pointer to the configuration structure for this instance + motor_speed_api_t const * p_api; ///< Pointer to the API structure for this instance +} motor_speed_instance_t; + +/*******************************************************************************************************************//** + * @} (end addtogroup MOTOR_SPEED_API) + **********************************************************************************************************************/ + +/* Common macro for FSP header files. There is also a corresponding FSP_HEADER macro at the top of this file. */ +FSP_FOOTER + +#endif /* RM_MOTOR_SPEED_API_H */ diff --git a/ra/fsp/inc/api/rm_vee_api.h b/ra/fsp/inc/api/rm_vee_api.h index 46734320a..c5c2536ce 100644 --- a/ra/fsp/inc/api/rm_vee_api.h +++ b/ra/fsp/inc/api/rm_vee_api.h @@ -50,7 +50,7 @@ FSP_HEADER * Macro definitions **********************************************************************************************************************/ #define RM_VEE_API_VERSION_MAJOR (1U) -#define RM_VEE_API_VERSION_MINOR (0U) +#define RM_VEE_API_VERSION_MINOR (1U) /********************************************************************************************************************** * Typedef definitions @@ -76,6 +76,7 @@ typedef struct st_rm_vee_callback_args /** User configuration structure, used in open function */ typedef struct st_rm_vee_cfg { + uint32_t start_addr; ///< Start address to be used for Virtual EEPROM memory uint32_t num_segments; ///< Number of segments to divide the volume into uint32_t total_size; ///< Total size of the volume uint32_t ref_data_size; ///< Size of the reference data stored at the end of the segment diff --git a/ra/fsp/inc/fsp_common_api.h b/ra/fsp/inc/fsp_common_api.h index 52fd68ba7..63f447259 100644 --- a/ra/fsp/inc/fsp_common_api.h +++ b/ra/fsp/inc/fsp_common_api.h @@ -44,7 +44,6 @@ /** 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. */ -/*LDRA_INSPECTED 340 s */ #define FSP_PARAMETER_NOT_USED(p) (void) ((p)) /** Determine if a C++ compiler is being used. @@ -267,6 +266,7 @@ typedef enum e_fsp_err 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. diff --git a/ra/fsp/inc/fsp_version.h b/ra/fsp/inc/fsp_version.h index 1e1ea659e..115985dbc 100644 --- a/ra/fsp/inc/fsp_version.h +++ b/ra/fsp/inc/fsp_version.h @@ -41,7 +41,7 @@ #define FSP_VERSION_MAJOR (2U) /** FSP pack minor version. */ -#define FSP_VERSION_MINOR (0U) +#define FSP_VERSION_MINOR (1U) /** FSP pack patch version. */ #define FSP_VERSION_PATCH (0U) @@ -50,10 +50,10 @@ #define FSP_VERSION_BUILD (0U) /** Public FSP version name. */ -#define FSP_VERSION_STRING ("2.0.0") +#define FSP_VERSION_STRING ("2.1.0") /** Unique FSP version ID. */ -#define FSP_VERSION_BUILD_STRING ("Built with Renesas Advanced Flexible Software Package version 2.0.0") +#define FSP_VERSION_BUILD_STRING ("Built with Renesas Advanced Flexible Software Package version 2.1.0") /********************************************************************************************************************** * Typedef definitions @@ -66,7 +66,6 @@ typedef union st_fsp_pack_version uint32_t version_id; /** Code version parameters, little endian order. */ - /*LDRA_INSPECTED 381 S Anonymous structures and unions are allowed in FSP code. */ struct { uint8_t build; ///< Build version of FSP Pack diff --git a/ra/fsp/inc/instances/r_adc.h b/ra/fsp/inc/instances/r_adc.h index 63ee7619c..c75f6b61b 100644 --- a/ra/fsp/inc/instances/r_adc.h +++ b/ra/fsp/inc/instances/r_adc.h @@ -49,7 +49,7 @@ FSP_HEADER /* Version of code that implements the API defined in this file */ #define ADC_CODE_VERSION_MAJOR (1U) -#define ADC_CODE_VERSION_MINOR (0U) +#define ADC_CODE_VERSION_MINOR (1U) /* Typical values that can be used to modify the sample states. * The minimum sample state count value is either 6 or 7 depending on the clock ratios. @@ -245,7 +245,7 @@ extern const adc_api_t g_adc_on_adc; * Public APIs **********************************************************************************************************************/ fsp_err_t R_ADC_Open(adc_ctrl_t * p_ctrl, adc_cfg_t const * const p_cfg); -fsp_err_t R_ADC_ScanCfg(adc_ctrl_t * p_ctrl, void const * const p_extend); +fsp_err_t R_ADC_ScanCfg(adc_ctrl_t * p_ctrl, void const * const p_channel_cfg); fsp_err_t R_ADC_InfoGet(adc_ctrl_t * p_ctrl, adc_info_t * p_adc_info); fsp_err_t R_ADC_ScanStart(adc_ctrl_t * p_ctrl); fsp_err_t R_ADC_ScanStop(adc_ctrl_t * p_ctrl); diff --git a/ra/fsp/inc/instances/r_agt.h b/ra/fsp/inc/instances/r_agt.h index 09afed5a2..464d035e9 100644 --- a/ra/fsp/inc/instances/r_agt.h +++ b/ra/fsp/inc/instances/r_agt.h @@ -37,7 +37,7 @@ FSP_HEADER /* Leading zeroes removed to avoid coding standards violation. */ #define AGT_CODE_VERSION_MAJOR (1U) -#define AGT_CODE_VERSION_MINOR (1U) +#define AGT_CODE_VERSION_MINOR (2U) /** Maximum number of clock counts in 16 bit timer. */ #define AGT_MAX_CLOCK_COUNTS (UINT16_MAX) diff --git a/ra/fsp/inc/instances/r_cac.h b/ra/fsp/inc/instances/r_cac.h index 591cd86fa..1519e3761 100644 --- a/ra/fsp/inc/instances/r_cac.h +++ b/ra/fsp/inc/instances/r_cac.h @@ -40,7 +40,7 @@ FSP_HEADER * Macro definitions **********************************************************************************************************************/ #define CAC_CODE_VERSION_MAJOR (1U) -#define CAC_CODE_VERSION_MINOR (0U) +#define CAC_CODE_VERSION_MINOR (1U) /*********************************************************************************************************************** * Typedef definitions diff --git a/ra/fsp/inc/instances/r_can.h b/ra/fsp/inc/instances/r_can.h index 7b8fe8764..ff608f753 100644 --- a/ra/fsp/inc/instances/r_can.h +++ b/ra/fsp/inc/instances/r_can.h @@ -40,7 +40,7 @@ FSP_HEADER * Macro definitions **********************************************************************************************************************/ #define CAN_CODE_VERSION_MAJOR (1U) -#define CAN_CODE_VERSION_MINOR (0U) +#define CAN_CODE_VERSION_MINOR (1U) /********************************************************************************************************************** * Typedef definitions diff --git a/ra/fsp/inc/instances/r_cgc.h b/ra/fsp/inc/instances/r_cgc.h index 8230d8915..53ec66214 100644 --- a/ra/fsp/inc/instances/r_cgc.h +++ b/ra/fsp/inc/instances/r_cgc.h @@ -40,7 +40,7 @@ FSP_HEADER * Macro definitions **********************************************************************************************************************/ #define CGC_CODE_VERSION_MAJOR (1U) -#define CGC_CODE_VERSION_MINOR (0U) +#define CGC_CODE_VERSION_MINOR (1U) /*********************************************************************************************************************** * Typedef definitions diff --git a/ra/fsp/inc/instances/r_ctsu.h b/ra/fsp/inc/instances/r_ctsu.h index ca14b391b..759f782b2 100644 --- a/ra/fsp/inc/instances/r_ctsu.h +++ b/ra/fsp/inc/instances/r_ctsu.h @@ -40,7 +40,7 @@ FSP_HEADER * Macro definitions **********************************************************************************************************************/ #define CTSU_CODE_VERSION_MAJOR (1U) -#define CTSU_CODE_VERSION_MINOR (1U) +#define CTSU_CODE_VERSION_MINOR (2U) #if (BSP_FEATURE_CTSU_VERSION == 2) #define CTSU_CORRECTION_POINT_NUM (12) ///< number of correction table @@ -188,11 +188,8 @@ typedef struct st_ctsu_instance_ctrl 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_work; ///< Pointer to Self work buffer. g_ctsu_self_work[] is set by Open API. uint16_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_work; ///< Pointer to Mutual primary work buffer. g_ctsu_mutual_pri_work[] is set by Open API. - uint16_t * p_mutual_snd_work; ///< Pointer to Mutual secondary work buffer. g_ctsu_mutual_snd_work[] is set by Open API. uint16_t * p_mutual_pri_data; ///< Pointer to Mutual primary moving average data. g_ctsu_mutual_pri_data[] is set by Open API. uint16_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 diff --git a/ra/fsp/inc/instances/r_doc.h b/ra/fsp/inc/instances/r_doc.h index 36569dd0b..8ff4da957 100644 --- a/ra/fsp/inc/instances/r_doc.h +++ b/ra/fsp/inc/instances/r_doc.h @@ -35,7 +35,7 @@ FSP_HEADER * Macro definitions **********************************************************************************************************************/ #define DOC_CODE_VERSION_MAJOR (1U) -#define DOC_CODE_VERSION_MINOR (0U) +#define DOC_CODE_VERSION_MINOR (1U) /*********************************************************************************************************************** * Typedef definitions diff --git a/ra/fsp/inc/instances/r_ether.h b/ra/fsp/inc/instances/r_ether.h index 7cd088858..30f423b40 100644 --- a/ra/fsp/inc/instances/r_ether.h +++ b/ra/fsp/inc/instances/r_ether.h @@ -41,7 +41,7 @@ FSP_HEADER * Macro definitions **********************************************************************************************************************/ #define ETHER_CODE_VERSION_MAJOR (1U) -#define ETHER_CODE_VERSION_MINOR (0U) +#define ETHER_CODE_VERSION_MINOR (1U) /*********************************************************************************************************************** * Typedef definitions diff --git a/ra/fsp/inc/instances/r_ether_phy.h b/ra/fsp/inc/instances/r_ether_phy.h index ca1caaadc..473a83df5 100644 --- a/ra/fsp/inc/instances/r_ether_phy.h +++ b/ra/fsp/inc/instances/r_ether_phy.h @@ -41,7 +41,7 @@ FSP_HEADER * Macro definitions **********************************************************************************************************************/ #define ETHER_PHY_CODE_VERSION_MAJOR (1U) -#define ETHER_PHY_CODE_VERSION_MINOR (1U) +#define ETHER_PHY_CODE_VERSION_MINOR (2U) #define ETHER_PHY_CFG_USE_PHY_KSZ8091RNB (1) #define ETHER_PHY_CFG_USE_PHY_KSZ8041 (2) diff --git a/ra/fsp/inc/instances/r_flash_hp.h b/ra/fsp/inc/instances/r_flash_hp.h index 607d7952c..2dccaf80b 100644 --- a/ra/fsp/inc/instances/r_flash_hp.h +++ b/ra/fsp/inc/instances/r_flash_hp.h @@ -42,7 +42,7 @@ FSP_HEADER * Macro definitions **********************************************************************************************************************/ #define FLASH_HP_CODE_VERSION_MAJOR (1U) -#define FLASH_HP_CODE_VERSION_MINOR (1U) +#define FLASH_HP_CODE_VERSION_MINOR (2U) /* If Code Flash programming is enabled, then all API functions must execute out of RAM. */ #if (FLASH_HP_CFG_CODE_FLASH_PROGRAMMING_ENABLE == 1) diff --git a/ra/fsp/inc/instances/r_gpt.h b/ra/fsp/inc/instances/r_gpt.h index 3dd3b5bd3..073b67ea1 100644 --- a/ra/fsp/inc/instances/r_gpt.h +++ b/ra/fsp/inc/instances/r_gpt.h @@ -39,7 +39,7 @@ FSP_HEADER * Macro definitions **********************************************************************************************************************/ #define GPT_CODE_VERSION_MAJOR (1U) -#define GPT_CODE_VERSION_MINOR (0U) +#define GPT_CODE_VERSION_MINOR (1U) /*********************************************************************************************************************** * Typedef definitions diff --git a/ra/fsp/inc/instances/r_icu.h b/ra/fsp/inc/instances/r_icu.h index 4d4c54740..016bd207f 100644 --- a/ra/fsp/inc/instances/r_icu.h +++ b/ra/fsp/inc/instances/r_icu.h @@ -39,7 +39,7 @@ FSP_HEADER * Macro definitions **********************************************************************************************************************/ #define ICU_CODE_VERSION_MAJOR (1U) -#define ICU_CODE_VERSION_MINOR (0U) +#define ICU_CODE_VERSION_MINOR (1U) /********************************************************************************************************************* * Typedef definitions diff --git a/ra/fsp/inc/instances/r_iic_master.h b/ra/fsp/inc/instances/r_iic_master.h index 20eca30a5..af53ed0a6 100644 --- a/ra/fsp/inc/instances/r_iic_master.h +++ b/ra/fsp/inc/instances/r_iic_master.h @@ -37,7 +37,7 @@ FSP_HEADER * Macro definitions **********************************************************************************************************************/ #define IIC_MASTER_CODE_VERSION_MAJOR (1U) -#define IIC_MASTER_CODE_VERSION_MINOR (1U) +#define IIC_MASTER_CODE_VERSION_MINOR (2U) /*********************************************************************************************************************** * Typedef definitions diff --git a/ra/fsp/inc/instances/r_iic_slave.h b/ra/fsp/inc/instances/r_iic_slave.h index 264bfa9d5..cd87a209b 100644 --- a/ra/fsp/inc/instances/r_iic_slave.h +++ b/ra/fsp/inc/instances/r_iic_slave.h @@ -34,7 +34,7 @@ * Macro definitions ***********************************************************************************************************************/ #define IIC_SLAVE_CODE_VERSION_MAJOR (1U) -#define IIC_SLAVE_CODE_VERSION_MINOR (0U) +#define IIC_SLAVE_CODE_VERSION_MINOR (1U) /*********************************************************************************************************************** * Typedef definitions diff --git a/ra/fsp/inc/instances/r_iwdt.h b/ra/fsp/inc/instances/r_iwdt.h index 173917557..a3bda3154 100644 --- a/ra/fsp/inc/instances/r_iwdt.h +++ b/ra/fsp/inc/instances/r_iwdt.h @@ -38,7 +38,7 @@ FSP_HEADER * Macro definitions **********************************************************************************************************************/ #define IWDT_CODE_VERSION_MAJOR (1U) -#define IWDT_CODE_VERSION_MINOR (1U) +#define IWDT_CODE_VERSION_MINOR (2U) /*********************************************************************************************************************** * Typedef definitions diff --git a/ra/fsp/inc/instances/r_lvd.h b/ra/fsp/inc/instances/r_lvd.h index f94f9da91..c9d8edcde 100644 --- a/ra/fsp/inc/instances/r_lvd.h +++ b/ra/fsp/inc/instances/r_lvd.h @@ -40,7 +40,7 @@ FSP_HEADER * Macro definitions **********************************************************************************************************************/ #define LVD_CODE_VERSION_MAJOR (1U) -#define LVD_CODE_VERSION_MINOR (0U) +#define LVD_CODE_VERSION_MINOR (1U) /*********************************************************************************************************************** * Typedef definitions diff --git a/ra/fsp/inc/instances/r_opamp.h b/ra/fsp/inc/instances/r_opamp.h index 74389c047..cc11e1fe1 100644 --- a/ra/fsp/inc/instances/r_opamp.h +++ b/ra/fsp/inc/instances/r_opamp.h @@ -42,7 +42,7 @@ FSP_HEADER /** Version of code that implements the API defined in this file */ #define OPAMP_CODE_VERSION_MAJOR (1U) -#define OPAMP_CODE_VERSION_MINOR (1U) +#define OPAMP_CODE_VERSION_MINOR (2U) #define OPAMP_MASK_CHANNEL_0 (1U << 0) #define OPAMP_MASK_CHANNEL_1 (1U << 1) diff --git a/ra/fsp/inc/instances/r_ospi.h b/ra/fsp/inc/instances/r_ospi.h index 1d0c731a3..4887398fc 100644 --- a/ra/fsp/inc/instances/r_ospi.h +++ b/ra/fsp/inc/instances/r_ospi.h @@ -40,7 +40,7 @@ FSP_HEADER * Macro definitions **********************************************************************************************************************/ #define OSPI_CODE_VERSION_MAJOR (1U) -#define OSPI_CODE_VERSION_MINOR (0U) +#define OSPI_CODE_VERSION_MINOR (1U) /*********************************************************************************************************************** * Typedef definitions diff --git a/ra/fsp/inc/instances/r_poeg.h b/ra/fsp/inc/instances/r_poeg.h index b93b27c04..46b1b6915 100644 --- a/ra/fsp/inc/instances/r_poeg.h +++ b/ra/fsp/inc/instances/r_poeg.h @@ -39,7 +39,7 @@ FSP_HEADER * Macro definitions **********************************************************************************************************************/ #define POEG_CODE_VERSION_MAJOR (1U) -#define POEG_CODE_VERSION_MINOR (0U) +#define POEG_CODE_VERSION_MINOR (1U) /*********************************************************************************************************************** * Typedef definitions diff --git a/ra/fsp/inc/instances/r_qspi.h b/ra/fsp/inc/instances/r_qspi.h index 063a86460..7b5240b25 100644 --- a/ra/fsp/inc/instances/r_qspi.h +++ b/ra/fsp/inc/instances/r_qspi.h @@ -41,7 +41,7 @@ FSP_HEADER * Macro definitions **********************************************************************************************************************/ #define QSPI_CODE_VERSION_MAJOR (1U) -#define QSPI_CODE_VERSION_MINOR (0U) +#define QSPI_CODE_VERSION_MINOR (1U) #define QSPI_DEVICE_START_ADDRESS (0x60000000) diff --git a/ra/fsp/inc/instances/r_rtc.h b/ra/fsp/inc/instances/r_rtc.h index 836bd506b..8e23baad4 100644 --- a/ra/fsp/inc/instances/r_rtc.h +++ b/ra/fsp/inc/instances/r_rtc.h @@ -40,7 +40,7 @@ FSP_HEADER * Macro definitions **********************************************************************************************************************/ #define RTC_CODE_VERSION_MAJOR (1U) -#define RTC_CODE_VERSION_MINOR (0U) +#define RTC_CODE_VERSION_MINOR (1U) /* Counting mode */ #define RTC_CALENDAR_MODE (0) diff --git a/ra/fsp/inc/instances/r_sci_i2c.h b/ra/fsp/inc/instances/r_sci_i2c.h index cdd24232e..039b1d625 100644 --- a/ra/fsp/inc/instances/r_sci_i2c.h +++ b/ra/fsp/inc/instances/r_sci_i2c.h @@ -37,7 +37,7 @@ FSP_HEADER * Macro definitions **********************************************************************************************************************/ #define SCI_I2C_MASTER_CODE_VERSION_MAJOR (1U) -#define SCI_I2C_MASTER_CODE_VERSION_MINOR (0U) +#define SCI_I2C_MASTER_CODE_VERSION_MINOR (1U) /*********************************************************************************************************************** * Typedef definitions diff --git a/ra/fsp/inc/instances/r_sci_spi.h b/ra/fsp/inc/instances/r_sci_spi.h index 42488999e..795711285 100644 --- a/ra/fsp/inc/instances/r_sci_spi.h +++ b/ra/fsp/inc/instances/r_sci_spi.h @@ -38,7 +38,7 @@ FSP_HEADER * Macro definitions **********************************************************************************************************************/ #define SCI_SPI_CODE_VERSION_MAJOR (1U) -#define SCI_SPI_CODE_VERSION_MINOR (1U) +#define SCI_SPI_CODE_VERSION_MINOR (2U) /*********************************************************************************************************************** * Typedef definitions diff --git a/ra/fsp/inc/instances/r_sci_uart.h b/ra/fsp/inc/instances/r_sci_uart.h index c307beaa7..7e9bae853 100644 --- a/ra/fsp/inc/instances/r_sci_uart.h +++ b/ra/fsp/inc/instances/r_sci_uart.h @@ -40,7 +40,7 @@ FSP_HEADER * Macro definitions **********************************************************************************************************************/ #define SCI_UART_CODE_VERSION_MAJOR (1U) -#define SCI_UART_CODE_VERSION_MINOR (1U) +#define SCI_UART_CODE_VERSION_MINOR (2U) /********************************************************************************************************************** * Typedef definitions diff --git a/ra/fsp/inc/instances/r_sdhi.h b/ra/fsp/inc/instances/r_sdhi.h index 59a65b55c..c107452bb 100644 --- a/ra/fsp/inc/instances/r_sdhi.h +++ b/ra/fsp/inc/instances/r_sdhi.h @@ -39,7 +39,7 @@ FSP_HEADER * Macro definitions **********************************************************************************************************************/ #define SDHI_CODE_VERSION_MAJOR (1U) -#define SDHI_CODE_VERSION_MINOR (0U) +#define SDHI_CODE_VERSION_MINOR (1U) #define SDHI_MAX_BLOCK_SIZE (512U) @@ -60,7 +60,6 @@ typedef union { uint32_t word; - /*LDRA_INSPECTED 381 S Anonymous structures and unions are allowed in FSP code. */ struct s_sdhi_event_type { uint32_t response_end : 1; // < response end detected diff --git a/ra/fsp/inc/instances/r_spi.h b/ra/fsp/inc/instances/r_spi.h index 0132d00e4..8da2cec98 100644 --- a/ra/fsp/inc/instances/r_spi.h +++ b/ra/fsp/inc/instances/r_spi.h @@ -39,7 +39,7 @@ FSP_HEADER **********************************************************************************************************************/ #define SPI_CODE_VERSION_MAJOR (1U) -#define SPI_CODE_VERSION_MINOR (1U) +#define SPI_CODE_VERSION_MINOR (2U) /************************************************************************************************* * Type defines for the SPI interface API diff --git a/ra/fsp/inc/instances/r_ssi.h b/ra/fsp/inc/instances/r_ssi.h index 3bfefc870..1cfebe7c2 100644 --- a/ra/fsp/inc/instances/r_ssi.h +++ b/ra/fsp/inc/instances/r_ssi.h @@ -38,7 +38,7 @@ FSP_HEADER * Macro definitions **********************************************************************************************************************/ #define SSI_CODE_VERSION_MAJOR (1U) -#define SSI_CODE_VERSION_MINOR (0U) +#define SSI_CODE_VERSION_MINOR (1U) /*********************************************************************************************************************** * Typedef definitions diff --git a/ra/fsp/inc/instances/r_usb_basic.h b/ra/fsp/inc/instances/r_usb_basic.h index 50563850a..ad84a1ebc 100644 --- a/ra/fsp/inc/instances/r_usb_basic.h +++ b/ra/fsp/inc/instances/r_usb_basic.h @@ -37,7 +37,7 @@ FSP_HEADER * Macro definitions **********************************************************************************************************************/ #define USB_CODE_VERSION_MAJOR (1U) -#define USB_CODE_VERSION_MINOR (1U) +#define USB_CODE_VERSION_MINOR (2U) /*********************************************************************************************************************** * Typedef definitions diff --git a/ra/fsp/inc/instances/r_wdt.h b/ra/fsp/inc/instances/r_wdt.h index ec6820c30..a7da66b35 100644 --- a/ra/fsp/inc/instances/r_wdt.h +++ b/ra/fsp/inc/instances/r_wdt.h @@ -38,7 +38,7 @@ FSP_HEADER * Macro definitions **********************************************************************************************************************/ #define WDT_CODE_VERSION_MAJOR (1U) -#define WDT_CODE_VERSION_MINOR (1U) +#define WDT_CODE_VERSION_MINOR (2U) /*********************************************************************************************************************** * Typedef definitions diff --git a/ra/fsp/inc/instances/rm_block_media_sdmmc.h b/ra/fsp/inc/instances/rm_block_media_sdmmc.h index 25afd0954..9019b9a57 100644 --- a/ra/fsp/inc/instances/rm_block_media_sdmmc.h +++ b/ra/fsp/inc/instances/rm_block_media_sdmmc.h @@ -42,7 +42,7 @@ FSP_HEADER * Macro definitions **********************************************************************************************************************/ #define RM_BLOCK_MEDIA_SDMMC_CODE_VERSION_MAJOR (1U) -#define RM_BLOCK_MEDIA_SDMMC_CODE_VERSION_MINOR (0U) +#define RM_BLOCK_MEDIA_SDMMC_CODE_VERSION_MINOR (1U) /*********************************************************************************************************************** * Typedef definitions diff --git a/ra/fsp/inc/instances/rm_motor_current.h b/ra/fsp/inc/instances/rm_motor_current.h new file mode 100644 index 000000000..af7821f6e --- /dev/null +++ b/ra/fsp/inc/instances/rm_motor_current.h @@ -0,0 +1,210 @@ +/*********************************************************************************************************************** + * Copyright [2020] Renesas Electronics Corporation and/or its affiliates. All Rights Reserved. + * + * This software and documentation are supplied by Renesas Electronics America Inc. and may only be used with products + * of Renesas Electronics Corp. and its affiliates ("Renesas"). No other uses are authorized. Renesas products are + * sold pursuant to Renesas terms and conditions of sale. Purchasers are solely responsible for the selection and use + * of Renesas products and Renesas assumes no liability. No license, express or implied, to any intellectual property + * right is granted by Renesas. This software is protected under all applicable laws, including copyright laws. Renesas + * reserves the right to change or discontinue this software and/or this documentation. THE SOFTWARE AND DOCUMENTATION + * IS DELIVERED TO YOU "AS IS," AND RENESAS MAKES NO REPRESENTATIONS OR WARRANTIES, AND TO THE FULLEST EXTENT + * PERMISSIBLE UNDER APPLICABLE LAW, DISCLAIMS ALL WARRANTIES, WHETHER EXPLICITLY OR IMPLICITLY, INCLUDING WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NONINFRINGEMENT, WITH RESPECT TO THE SOFTWARE OR + * DOCUMENTATION. RENESAS SHALL HAVE NO LIABILITY ARISING OUT OF ANY SECURITY VULNERABILITY OR BREACH. TO THE MAXIMUM + * EXTENT PERMITTED BY LAW, IN NO EVENT WILL RENESAS BE LIABLE TO YOU IN CONNECTION WITH THE SOFTWARE OR DOCUMENTATION + * (OR ANY PERSON OR ENTITY CLAIMING RIGHTS DERIVED FROM YOU) FOR ANY LOSS, DAMAGES, OR CLAIMS WHATSOEVER, INCLUDING, + * WITHOUT LIMITATION, ANY DIRECT, CONSEQUENTIAL, SPECIAL, INDIRECT, PUNITIVE, OR INCIDENTAL DAMAGES; ANY LOST PROFITS, + * OTHER ECONOMIC DAMAGE, PROPERTY DAMAGE, OR PERSONAL INJURY; AND EVEN IF RENESAS HAS BEEN ADVISED OF THE POSSIBILITY + * OF SUCH LOSS, DAMAGES, CLAIMS OR COSTS. + **********************************************************************************************************************/ + +/*******************************************************************************************************************//** + * @addtogroup MOTOR_CURRENT + * @{ + **********************************************************************************************************************/ + +#ifndef RM_MOTOR_CURRENT_H +#define RM_MOTOR_CURRENT_H + +/*********************************************************************************************************************** + * Includes + **********************************************************************************************************************/ +#include "bsp_api.h" + +#include "rm_motor_current_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 + **********************************************************************************************************************/ +#define MOTOR_CURRENT_CODE_VERSION_MAJOR (1U) +#define MOTOR_CURRENT_CODE_VERSION_MINOR (0U) + +/* For Voltage Compensation */ +#define MOTOR_CURRENT_VOLTAGE_COMPENSATION_TABLE_ARRAY_SIZE (5) +#define MOTOR_CURRENT_VOLTAGE_COMPENSATION_SLOPE_ARRAY_SIZE (6) +#define MOTOR_CURRENT_VOLTAGE_COMPENSATION_PHASE_ARRAY_SIZE (3) + +/*********************************************************************************************************************** + * Typedef definitions + **********************************************************************************************************************/ +typedef enum e_motor_current_voltage_compensation_select +{ + MOTOR_CURRENT_VOLTAGE_COMPENSATION_SELECT_DISABLE = 0, + MOTOR_CURRENT_VOLTAGE_COMPENSATION_SELECT_ENABLE = 1 +} motor_current_voltage_compensation_select_t; + +typedef struct st_motor_current_pi_params +{ + float f_err; ///< Error + float f_kp; ///< Gain of proportional term + float f_ki; ///< Gain of integral term + float f_refi; ///< Buffer of integral term + float f_ilimit; ///< Limit of integral term +} motor_current_pi_params_t; + +/* Design parameter structure */ +typedef struct motor_current_design_parameter +{ + float f_current_omega; ///< Natural frequency[Hz] for current loop gain design + float f_current_zeta; ///< Damping ratio for current loop gain design +} motor_current_design_parameter_t; + +typedef struct st_motor_current_motor_parameter +{ + uint16_t u2_mtr_pp; ///< Pole pairs + float f4_mtr_r; ///< Resistance [ohm] + float f4_mtr_ld; ///< Inductance for d-axis [H] + float f4_mtr_lq; ///< Inductance for q-axis [H] + float f4_mtr_m; ///< Magnet flux [Wb] + float f4_mtr_j; ///< Rotor inertia [kgm^2] +} motor_current_motor_parameter_t; + +typedef struct st_motor_current_voltage_compensation +{ + float f_comp_v[MOTOR_CURRENT_VOLTAGE_COMPENSATION_TABLE_ARRAY_SIZE]; + float f_comp_i[MOTOR_CURRENT_VOLTAGE_COMPENSATION_TABLE_ARRAY_SIZE]; + float f_slope[MOTOR_CURRENT_VOLTAGE_COMPENSATION_SLOPE_ARRAY_SIZE]; + float f_intcept[MOTOR_CURRENT_VOLTAGE_COMPENSATION_SLOPE_ARRAY_SIZE]; + float f_volt_comp_array[MOTOR_CURRENT_VOLTAGE_COMPENSATION_PHASE_ARRAY_SIZE]; + float f_vdc; + float f_volt_comp_limit; + motor_current_voltage_compensation_select_t u1_volt_err_comp_enable; +} motor_currnt_voltage_compensation_t; + +typedef struct st_motor_current_extended_cfg +{ + float f_comp_v[MOTOR_CURRENT_VOLTAGE_COMPENSATION_TABLE_ARRAY_SIZE]; ///< Voltage error compensation table of voltage + float f_comp_i[MOTOR_CURRENT_VOLTAGE_COMPENSATION_TABLE_ARRAY_SIZE]; ///< Voltage error compensation table of current + motor_current_voltage_compensation_select_t vcomp_enable; ///< Enable/Disable select of voltage error compensation + float f_current_ctrl_period; ///< Current control period [usec] + float f_ilimit; ///< Current limit [A] + + motor_current_motor_parameter_t * p_motor_parameter; ///< Motor Parameters + motor_current_design_parameter_t * p_design_parameter; ///< PI control designed parameters +} motor_current_extended_cfg_t; + +typedef struct st_motor_current_instance_ctrl +{ + uint32_t open; + + uint8_t u1_active; ///< Flag to set active/inactive the current control + float f_vd_ref; ///< Reference d-axis voltage [V] + float f_vq_ref; ///< Reference q-axis voltage [V] + float f_id_ref; ///< Reference d-axis current [A] + float f_iq_ref; ///< Reference q-axis current [A] + float f_iu_ad; ///< U phase current [A] + float f_iv_ad; ///< V phase current [A] + float f_iw_ad; ///< W phase current [A] + float f_id_ad; ///< D-axis current [A] + float f_iq_ad; ///< Q-axis current [A] + float f_vdc_ad; ///< Main Line Voltage [V] + float f_speed_rad; ///< Motor rotational speed [rad/s] + float f_rotor_angle; ///< Motor rotor angle [rad] + float f_refu; ///< U phase output reference Voltage [V] + float f_refv; ///< V phase output reference Voltage [V] + float f_refw; ///< W phase output reference Voltage [V] + float f_va_max; + + /* For Speed Control Interface (to Angle module) */ + float f_ed; + float f_eq; + float f_phase_err; ///< Error of Motor Phase + uint8_t u1_flag_crnt_offset; ///< Finish flag to measure current offset + + motor_current_cfg_t const * p_cfg; + + motor_current_pi_params_t st_pi_id; ///< D-axis current PI controller + motor_current_pi_params_t st_pi_iq; ///< Q-axis current PI controller + + motor_currnt_voltage_compensation_t st_vcomp; ///< Data for Voltage Error Compensation + + motor_current_input_t st_input; ///< Data buffer from Speed Control + + motor_angle_instance_t const * p_angle_instance; + motor_driver_instance_t const * p_driver_instance; +} motor_current_instance_ctrl_t; + +/********************************************************************************************************************** + * Exported global variables + **********************************************************************************************************************/ + +/** @cond INC_HEADER_DEFS_SEC */ +/** Filled in Interface API structure for this Instance. */ +extern const motor_current_api_t g_motor_current_on_motor_current; + +/** @endcond */ + +/*********************************************************************************************************************** + * Exported global functions (to be accessed by other files) + ***********************************************************************************************************************/ + +/********************************************************************************************************************** + * Public Function Prototypes + **********************************************************************************************************************/ +fsp_err_t RM_MOTOR_CURRENT_Open(motor_current_ctrl_t * const p_ctrl, motor_current_cfg_t const * const p_cfg); + +fsp_err_t RM_MOTOR_CURRENT_Close(motor_current_ctrl_t * const p_ctrl); + +fsp_err_t RM_MOTOR_CURRENT_Reset(motor_current_ctrl_t * const p_ctrl); + +fsp_err_t RM_MOTOR_CURRENT_Run(motor_current_ctrl_t * const p_ctrl); + +fsp_err_t RM_MOTOR_CURRENT_ParameterSet(motor_current_ctrl_t * const p_ctrl, + motor_current_input_t const * const p_st_input); + +fsp_err_t RM_MOTOR_CURRENT_CurrentReferenceSet(motor_current_ctrl_t * const p_ctrl, + float const id_reference, + float const iq_reference); + +fsp_err_t RM_MOTOR_CURRENT_SpeedPhaseSet(motor_current_ctrl_t * const p_ctrl, + float const speed_rad, + float const phase_rad); + +fsp_err_t RM_MOTOR_CURRENT_CurrentSet(motor_current_ctrl_t * const p_ctrl, + motor_current_input_current_t const * const p_st_current, + motor_current_input_voltage_t const * const p_st_voltage); + +fsp_err_t RM_MOTOR_CURRENT_ParameterGet(motor_current_ctrl_t * const p_ctrl, + motor_current_output_t * const p_st_output); + +fsp_err_t RM_MOTOR_CURRENT_CurrentGet(motor_current_ctrl_t * const p_ctrl, float * const p_id, float * const p_iq); + +fsp_err_t RM_MOTOR_CURRENT_PhaseVoltageGet(motor_current_ctrl_t * const p_ctrl, + motor_current_get_voltage_t * const p_voltage); + +fsp_err_t RM_MOTOR_CURRENT_ParameterUpdate(motor_current_ctrl_t * const p_ctrl, + motor_current_cfg_t const * const p_cfg); + +fsp_err_t RM_MOTOR_CURRENT_VersionGet(fsp_version_t * const p_version); + +/* Common macro for FSP header files. There is also a corresponding FSP_HEADER macro at the top of this file. */ +FSP_FOOTER + +#endif // RM_MOTOR_CURRENT_H + +/*******************************************************************************************************************//** + * @} (end addtogroup MOTOR_CURRENT) + **********************************************************************************************************************/ diff --git a/ra/fsp/inc/instances/rm_motor_driver.h b/ra/fsp/inc/instances/rm_motor_driver.h new file mode 100644 index 000000000..e508c7144 --- /dev/null +++ b/ra/fsp/inc/instances/rm_motor_driver.h @@ -0,0 +1,151 @@ +/*********************************************************************************************************************** + * Copyright [2020] Renesas Electronics Corporation and/or its affiliates. All Rights Reserved. + * + * This software and documentation are supplied by Renesas Electronics America Inc. and may only be used with products + * of Renesas Electronics Corp. and its affiliates ("Renesas"). No other uses are authorized. Renesas products are + * sold pursuant to Renesas terms and conditions of sale. Purchasers are solely responsible for the selection and use + * of Renesas products and Renesas assumes no liability. No license, express or implied, to any intellectual property + * right is granted by Renesas. This software is protected under all applicable laws, including copyright laws. Renesas + * reserves the right to change or discontinue this software and/or this documentation. THE SOFTWARE AND DOCUMENTATION + * IS DELIVERED TO YOU "AS IS," AND RENESAS MAKES NO REPRESENTATIONS OR WARRANTIES, AND TO THE FULLEST EXTENT + * PERMISSIBLE UNDER APPLICABLE LAW, DISCLAIMS ALL WARRANTIES, WHETHER EXPLICITLY OR IMPLICITLY, INCLUDING WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NONINFRINGEMENT, WITH RESPECT TO THE SOFTWARE OR + * DOCUMENTATION. RENESAS SHALL HAVE NO LIABILITY ARISING OUT OF ANY SECURITY VULNERABILITY OR BREACH. TO THE MAXIMUM + * EXTENT PERMITTED BY LAW, IN NO EVENT WILL RENESAS BE LIABLE TO YOU IN CONNECTION WITH THE SOFTWARE OR DOCUMENTATION + * (OR ANY PERSON OR ENTITY CLAIMING RIGHTS DERIVED FROM YOU) FOR ANY LOSS, DAMAGES, OR CLAIMS WHATSOEVER, INCLUDING, + * WITHOUT LIMITATION, ANY DIRECT, CONSEQUENTIAL, SPECIAL, INDIRECT, PUNITIVE, OR INCIDENTAL DAMAGES; ANY LOST PROFITS, + * OTHER ECONOMIC DAMAGE, PROPERTY DAMAGE, OR PERSONAL INJURY; AND EVEN IF RENESAS HAS BEEN ADVISED OF THE POSSIBILITY + * OF SUCH LOSS, DAMAGES, CLAIMS OR COSTS. + **********************************************************************************************************************/ + +/*******************************************************************************************************************//** + * @addtogroup MOTOR_DRIVER + * @{ + **********************************************************************************************************************/ + +#ifndef RM_MOTOR_DRIVER_H +#define RM_MOTOR_DRIVER_H + +/*********************************************************************************************************************** + * Includes + **********************************************************************************************************************/ +#include "bsp_api.h" + +#include "rm_motor_driver_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 + **********************************************************************************************************************/ +#define MOTOR_DRIVER_CODE_VERSION_MAJOR (1U) +#define MOTOR_DRIVER_CODE_VERSION_MINOR (0U) + +/*********************************************************************************************************************** + * Typedef definitions + **********************************************************************************************************************/ +typedef struct st_motor_driver_modulation +{ + float f4_vdc; ///< Main Line Voltage (Vdc) [V] + float f4_1_div_vdc; ///< 1/Vdc (Inverse Vdc for calculation) + float f4_voltage_error_ratio; ///< The voltage error ratio (VoltageError/Vdc) + float f4_max_duty; ///< Maximum duty cycle + float f4_min_duty; ///< Minimum duty cycle + float f4_neutral_duty; ///< Duty cycle that represents 0[V] + uint8_t u1_sat_flag; ///< Saturation flag +} motor_driver_modulation_t; + +typedef struct st_motor_driverextended_cfg +{ + uint16_t u2_pwm_timer_freq; ///< PWM Timer Frequency [MHz] + uint16_t u2_pwm_carrier_freq; ///< PWM Carrier Frequency [kHz] + uint16_t u2_deadtime; ///< PWM Deadtime [usec] + + float f_current_range; ///< A/D Current measure range (max current) [A] + float f_vdc_range; ///< A/D Main Line Voltage measure range (max voltage) [V] + float f_ad_resolution; ///< A/D Resolution */ + float f_ad_current_offset; ///< A/D Offset (Center value) */ + float f_ad_voltage_conversion; ///< A/D Conversion level */ + + uint16_t u2_offset_calc_count; ///< Calculation counts for current offset + + motor_driver_modulation_t mod_param; +} motor_driver_extended_cfg_t; + +typedef struct st_motor_driver_instance_ctrl +{ + uint32_t open; + + uint16_t u2_carrier_base; ///< PWM Carrier Base Counts + uint16_t u2_deadtime_count; ///< Deadtime counts + + float f_iu_ad; ///< U phase current [A] + float f_iw_ad; ///< W phase current [A] + float f_vdc_ad; ///< Main Line Voltage [V] + float f_refu; ///< Calculated U Phase output Voltage [V] + float f_refv; ///< Calculated V Phase output Voltage [V] + float f_refw; ///< Calculated W Phase output Voltage [V] + + /* for current offset calculation */ + uint8_t u1_flag_offset_calc; ///< The flag represents that the offset measurement is finished + uint16_t u2_offset_calc_times; ///< Calculation times for current offset + float f_offset_iu; ///< U phase current offset value [A] + float f_offset_iw; ///< W phase current offset value [A] + float f_sum_iu_ad; ///< U phase current summation value to calculate offset [A] + float f_sum_iw_ad; ///< W phase current summation value to calculate offset [A] + + motor_driver_modulation_t st_modulation; + motor_driver_cfg_t const * p_cfg; + + /* For ADC callback */ + adc_callback_args_t adc_callback_args; ///< For call ADC callbackSet function +} motor_driver_instance_ctrl_t; + +/********************************************************************************************************************** + * Exported global variables + **********************************************************************************************************************/ + +/** @cond INC_HEADER_DEFS_SEC */ +/** Filled in Interface API structure for this Instance. */ +extern const motor_driver_api_t g_motor_driver_on_motor_driver; + +/** @endcond */ + +/*********************************************************************************************************************** + * Exported global functions (to be accessed by other files) + ***********************************************************************************************************************/ + +/********************************************************************************************************************** + * Public Function Prototypes + **********************************************************************************************************************/ +fsp_err_t RM_MOTOR_DRIVER_Open(motor_driver_ctrl_t * const p_ctrl, motor_driver_cfg_t const * const p_cfg); + +fsp_err_t RM_MOTOR_DRIVER_Close(motor_driver_ctrl_t * const p_ctrl); + +fsp_err_t RM_MOTOR_DRIVER_Reset(motor_driver_ctrl_t * const p_ctrl); + +fsp_err_t RM_MOTOR_DRIVER_PhaseVoltageSet(motor_driver_ctrl_t * const p_ctrl, + float const u_voltage, + float const v_voltage, + float const w_voltage); + +fsp_err_t RM_MOTOR_DRIVER_CurrentGet(motor_driver_ctrl_t * const p_ctrl, + motor_driver_current_get_t * const p_current_get); + +fsp_err_t RM_MOTOR_DRIVER_FlagCurrentOffsetGet(motor_driver_ctrl_t * const p_ctrl, uint8_t * const p_flag_offset); + +fsp_err_t RM_MOTOR_DRIVER_CurrentOffsetRestart(motor_driver_ctrl_t * const p_ctrl); + +fsp_err_t RM_MOTOR_DRIVER_ParameterUpdate(motor_driver_ctrl_t * const p_ctrl, motor_driver_cfg_t const * const p_cfg); + +fsp_err_t RM_MOTOR_DRIVER_VersionGet(fsp_version_t * const p_version); + +/* Common macro for FSP header files. There is also a corresponding FSP_HEADER macro at the top of this file. */ +FSP_FOOTER + +#endif // RM_MOTOR_DRIVER_H + +/*******************************************************************************************************************//** + * @} (end addtogroup MOTOR_DRIVER) + **********************************************************************************************************************/ diff --git a/ra/fsp/inc/instances/rm_motor_estimate.h b/ra/fsp/inc/instances/rm_motor_estimate.h new file mode 100644 index 000000000..0e89561bd --- /dev/null +++ b/ra/fsp/inc/instances/rm_motor_estimate.h @@ -0,0 +1,178 @@ +/*********************************************************************************************************************** + * Copyright [2020] Renesas Electronics Corporation and/or its affiliates. All Rights Reserved. + * + * This software and documentation are supplied by Renesas Electronics America Inc. and may only be used with products + * of Renesas Electronics Corp. and its affiliates ("Renesas"). No other uses are authorized. Renesas products are + * sold pursuant to Renesas terms and conditions of sale. Purchasers are solely responsible for the selection and use + * of Renesas products and Renesas assumes no liability. No license, express or implied, to any intellectual property + * right is granted by Renesas. This software is protected under all applicable laws, including copyright laws. Renesas + * reserves the right to change or discontinue this software and/or this documentation. THE SOFTWARE AND DOCUMENTATION + * IS DELIVERED TO YOU "AS IS," AND RENESAS MAKES NO REPRESENTATIONS OR WARRANTIES, AND TO THE FULLEST EXTENT + * PERMISSIBLE UNDER APPLICABLE LAW, DISCLAIMS ALL WARRANTIES, WHETHER EXPLICITLY OR IMPLICITLY, INCLUDING WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NONINFRINGEMENT, WITH RESPECT TO THE SOFTWARE OR + * DOCUMENTATION. RENESAS SHALL HAVE NO LIABILITY ARISING OUT OF ANY SECURITY VULNERABILITY OR BREACH. TO THE MAXIMUM + * EXTENT PERMITTED BY LAW, IN NO EVENT WILL RENESAS BE LIABLE TO YOU IN CONNECTION WITH THE SOFTWARE OR DOCUMENTATION + * (OR ANY PERSON OR ENTITY CLAIMING RIGHTS DERIVED FROM YOU) FOR ANY LOSS, DAMAGES, OR CLAIMS WHATSOEVER, INCLUDING, + * WITHOUT LIMITATION, ANY DIRECT, CONSEQUENTIAL, SPECIAL, INDIRECT, PUNITIVE, OR INCIDENTAL DAMAGES; ANY LOST PROFITS, + * OTHER ECONOMIC DAMAGE, PROPERTY DAMAGE, OR PERSONAL INJURY; AND EVEN IF RENESAS HAS BEEN ADVISED OF THE POSSIBILITY + * OF SUCH LOSS, DAMAGES, CLAIMS OR COSTS. + **********************************************************************************************************************/ + +/*******************************************************************************************************************//** + * @addtogroup MOTOR_ESTIMATE + * @{ + **********************************************************************************************************************/ + +#ifndef RM_MOTOR_ESTIMATE_H +#define RM_MOTOR_ESTIMATE_H + +/*********************************************************************************************************************** + * Includes + **********************************************************************************************************************/ +#include "bsp_api.h" + +#include "rm_motor_angle_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 + **********************************************************************************************************************/ +#define MOTOR_ESTIMATE_CODE_VERSION_MAJOR (1U) +#define MOTOR_ESTIMATE_CODE_VERSION_MINOR (0U) + +/*********************************************************************************************************************** + * Typedef definitions + **********************************************************************************************************************/ + +typedef enum e_motor_estimate_openloop_damping +{ + MOTOR_ESTIMATE_OPENLOOP_DAMPING_DISABLE = 0, + MOTOR_ESTIMATE_OPENLOOP_DAMPING_ENABLE = 1 +} motor_estimate_openloop_damping_t; + +typedef struct st_motor_estimate_bemf_obs_axis +{ + float f4_k_e_obs_1; ///< BEMF observer gain 1 + float f4_k_e_obs_2; ///< BEMF observer gain 2 + float f4_i_pre; ///< Previous value of current + float f4_i_est_pre; ///< Previous value of estimated current + float f4_d_est; ///< Estimated voltage disturbance + float f4_d_est_pre; ///< Previous value of voltage disturbance +} motor_estimate_bemf_obs_axis_t; + +typedef struct st_motor_estimate_motor_parameter +{ + uint16_t u2_mtr_pp; ///< Pole pairs + float f4_mtr_r; ///< Resistance [ohm] + float f4_mtr_ld; ///< Inductance for d-axis [H] + float f4_mtr_lq; ///< Inductance for q-axis [H] + float f4_mtr_m; ///< Magnet flux [Wb] + float f4_mtr_j; ///< Rotor inertia [kgm^2] +} motor_estimate_motor_parameter_t; + +typedef struct st_motor_estimate_bemf_observer +{ + float f4_dt; ///< Control period + motor_estimate_motor_parameter_t st_motor_params; ///< Motor parameters + motor_estimate_bemf_obs_axis_t st_d_axis; ///< D-axis observer + motor_estimate_bemf_obs_axis_t st_q_axis; ///< Q-axis observer +} motor_estimate_bemf_observer_t; + +typedef struct st_motor_estimate_pll_est +{ + float f4_kp_est_speed; ///< The proportional gain for PLL + float f4_ki_est_speed; ///< The integral gain for PLL + float f4_i_est_speed; ///< The integrator for PLL +} motor_estimate_pll_est_t; + +typedef struct st_motor_estimate_input +{ + float f_vd_ref; ///< d-axis Voltage Reference [V] + float f_vq_ref; ///< q-axis Voltage Reference [V] + float f_id; ///< d-axis current [A] + float f_iq; ///< q-axis current [A] + float f4_ref_speed_rad_ctrl; ///< Speed Reference [rad/sec] + float f4_damp_comp_speed; +} motor_estimate_input_t; + +typedef struct st_motor_estimate_extended_cfg +{ + motor_estimate_openloop_damping_t openloop_damping; + float f_e_obs_omega; ///< Natural frequency of BEMF observer + float f_e_obs_zeta; ///< Damping ratio of BEMF observer + float f_pll_est_omega; ///< Natural frequency of PLL Speed estimate loop + float f_pll_est_zeta; ///< Damping ratio of PLL Speed estimate loop + float f4_ctrl_period; ///< Period of Control Process [sec] + motor_estimate_motor_parameter_t st_motor_params; ///< Motor parameters +} motor_estimate_extended_cfg_t; + +typedef struct st_motor_estimate_instance_ctrl +{ + uint32_t open; + float f4_ed; + float f4_eq; + float f4_speed_rad; + float f4_phase_err_rad; + float f4_angle_rad; + uint8_t u1_flg_pi_ctrl; + uint8_t u1_flg_pll_start; + motor_estimate_bemf_observer_t st_bemf_obs; ///< BEMF observer structure + motor_estimate_pll_est_t st_pll_est; ///< PLL estimator structure + motor_estimate_input_t st_input; ///< Input parameter structure + motor_angle_cfg_t const * p_cfg; +} motor_estimate_instance_ctrl_t; + +/********************************************************************************************************************** + * Exported global variables + **********************************************************************************************************************/ + +/** @cond INC_HEADER_DEFS_SEC */ +/** Filled in Interface API structure for this Instance. */ +extern const motor_angle_api_t g_motor_angle_on_motor_estimate; + +/** @endcond */ + +/*********************************************************************************************************************** + * Exported global functions (to be accessed by other files) + ***********************************************************************************************************************/ + +/********************************************************************************************************************** + * Public Function Prototypes + **********************************************************************************************************************/ +fsp_err_t RM_MOTOR_ESTIMATE_Open(motor_angle_ctrl_t * const p_ctrl, motor_angle_cfg_t const * const p_cfg); + +fsp_err_t RM_MOTOR_ESTIMATE_Close(motor_angle_ctrl_t * const p_ctrl); + +fsp_err_t RM_MOTOR_ESTIMATE_Reset(motor_angle_ctrl_t * const p_ctrl); + +fsp_err_t RM_MOTOR_ESTIMATE_CurrentSet(motor_angle_ctrl_t * const p_ctrl, + motor_angle_current_t * const p_st_current, + motor_angle_voltage_reference_t * const p_st_voltage); + +fsp_err_t RM_MOTOR_ESTIMATE_SpeedSet(motor_angle_ctrl_t * const p_ctrl, float const speed_ctrl, float const damp_speed); + +fsp_err_t RM_MOTOR_ESTIMATE_FlagPiCtrlSet(motor_angle_ctrl_t * const p_ctrl, uint32_t const flag_pi); + +fsp_err_t RM_MOTOR_ESTIMATE_AngleSpeedGet(motor_angle_ctrl_t * const p_ctrl, + float * const p_angle, + float * const p_speed, + float * const p_phase_err); + +fsp_err_t RM_MOTOR_ESTIMATE_EstimatedComponentGet(motor_angle_ctrl_t * const p_ctrl, + float * const p_ed, + float * const p_eq); + +fsp_err_t RM_MOTOR_ESTIMATE_ParameterUpdate(motor_angle_ctrl_t * const p_ctrl, motor_angle_cfg_t const * p_cfg); + +fsp_err_t RM_MOTOR_ESTIMATE_VersionGet(fsp_version_t * const p_version); + +/* Common macro for FSP header files. There is also a corresponding FSP_HEADER macro at the top of this file. */ +FSP_FOOTER + +#endif // RM_MOTOR_ESTIMATE_H + +/*******************************************************************************************************************//** + * @} (end addtogroup MOTOR_ESTIMATE) + **********************************************************************************************************************/ diff --git a/ra/fsp/inc/instances/rm_motor_sensorless.h b/ra/fsp/inc/instances/rm_motor_sensorless.h new file mode 100644 index 000000000..07a2c3df1 --- /dev/null +++ b/ra/fsp/inc/instances/rm_motor_sensorless.h @@ -0,0 +1,165 @@ +/*********************************************************************************************************************** + * Copyright [2020] Renesas Electronics Corporation and/or its affiliates. All Rights Reserved. + * + * This software and documentation are supplied by Renesas Electronics America Inc. and may only be used with products + * of Renesas Electronics Corp. and its affiliates ("Renesas"). No other uses are authorized. Renesas products are + * sold pursuant to Renesas terms and conditions of sale. Purchasers are solely responsible for the selection and use + * of Renesas products and Renesas assumes no liability. No license, express or implied, to any intellectual property + * right is granted by Renesas. This software is protected under all applicable laws, including copyright laws. Renesas + * reserves the right to change or discontinue this software and/or this documentation. THE SOFTWARE AND DOCUMENTATION + * IS DELIVERED TO YOU "AS IS," AND RENESAS MAKES NO REPRESENTATIONS OR WARRANTIES, AND TO THE FULLEST EXTENT + * PERMISSIBLE UNDER APPLICABLE LAW, DISCLAIMS ALL WARRANTIES, WHETHER EXPLICITLY OR IMPLICITLY, INCLUDING WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NONINFRINGEMENT, WITH RESPECT TO THE SOFTWARE OR + * DOCUMENTATION. RENESAS SHALL HAVE NO LIABILITY ARISING OUT OF ANY SECURITY VULNERABILITY OR BREACH. TO THE MAXIMUM + * EXTENT PERMITTED BY LAW, IN NO EVENT WILL RENESAS BE LIABLE TO YOU IN CONNECTION WITH THE SOFTWARE OR DOCUMENTATION + * (OR ANY PERSON OR ENTITY CLAIMING RIGHTS DERIVED FROM YOU) FOR ANY LOSS, DAMAGES, OR CLAIMS WHATSOEVER, INCLUDING, + * WITHOUT LIMITATION, ANY DIRECT, CONSEQUENTIAL, SPECIAL, INDIRECT, PUNITIVE, OR INCIDENTAL DAMAGES; ANY LOST PROFITS, + * OTHER ECONOMIC DAMAGE, PROPERTY DAMAGE, OR PERSONAL INJURY; AND EVEN IF RENESAS HAS BEEN ADVISED OF THE POSSIBILITY + * OF SUCH LOSS, DAMAGES, CLAIMS OR COSTS. + **********************************************************************************************************************/ + +/*******************************************************************************************************************//** + * @addtogroup MOTOR_SENSORLESS + * @{ + **********************************************************************************************************************/ + +#ifndef RM_MOTOR_SENSORLESS_H +#define RM_MOTOR_SENSORLESS_H + +/*********************************************************************************************************************** + * Includes + **********************************************************************************************************************/ +#include "bsp_api.h" +#include "rm_motor_api.h" +#include "rm_motor_speed.h" +#include "rm_motor_current.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 MOTOR_SENSORLESS_CODE_VERSION_MAJOR (1U) +#define MOTOR_SENSORLESS_CODE_VERSION_MINOR (0U) + +/*********************************************************************************************************************** + * Typedef definitions + **********************************************************************************************************************/ +typedef enum e_motor_sensorless_ctrl_status +{ + MOTOR_SENSORLESS_CTRL_STOP = 0, + MOTOR_SENSORLESS_CTRL_RUN = 1, + MOTOR_SENSORLESS_CTRL_ERROR = 2, +} motor_sensorless_ctrl_status_t; + +typedef enum e_motor_sensorless_ctrl_event +{ + MOTOR_SENSORLESS_CTRL_EVENT_STOP = 0, + MOTOR_SENSORLESS_CTRL_EVENT_RUN = 1, + MOTOR_SENSORLESS_CTRL_EVENT_ERROR = 2, + MOTOR_SENSORLESS_CTRL_EVENT_RESET = 3 +} motor_sensorless_ctrl_event_t; + +typedef struct rm_motor_sensorless_statemachine +{ + motor_sensorless_ctrl_status_t u1_status; ///< The current system status + motor_sensorless_ctrl_status_t u1_status_next; ///< The next system status + motor_sensorless_ctrl_event_t u1_current_event; ///< The current event index + + uint16_t u2_error_status; ///< The error information +} motor_sensorless_statemachine_t; + +/** Events that can trigger a callback function */ +typedef enum e_motor_sensorless_callback_event +{ + MOTOR_SENSORLESS_CALLBACK_EVENT_SPEED_FORWARD = 1, ///< Event forward Speed Control + MOTOR_SENSORLESS_CALLBACK_EVENT_SPEED_BACKWARD, ///< Event backward Speed Control + MOTOR_SENSORLESS_CALLBACK_EVENT_CURRENT_FORWARD, ///< Event forward Current Control + MOTOR_SENSORLESS_CALLBACK_EVENT_CURRENT_BACKWARD, ///< Event backward Current Control +} motor_sensorless_callback_event_t; + +/** Callback function parameter data */ +typedef struct st_rm_motor_sensorless_callback_args +{ + void const * p_context; ///< Placeholder for user data. + motor_sensorless_callback_event_t event; +} motor_sensorless_callback_args_t; + +typedef struct st_motor_sensorless_extended_cfg +{ + void (* p_callback)(motor_sensorless_callback_args_t * p_args); + void const * p_context; ///< Placeholder for user data. + + float f_overcurrent_limit; ///< Over-current limit [A] + float f_overvoltage_limit; ///< Over-voltage limit [V] + float f_overspeed_limit; ///< Over-speed limit [rad/s] + float f_lowvoltage_limit; ///< Low-voltage limit [V] +} motor_sensorless_extended_cfg_t; + +typedef struct st_motor_sensorless_instance_ctrl +{ + uint32_t open; ///< Used to determine if the channel is configured + + uint16_t u2_error_info; ///< Happened error + + motor_sensorless_statemachine_t st_statem; + + /* Speed control <=> Current control interface */ + motor_speed_input_t st_speed_input; + motor_speed_output_t st_speed_output; + motor_current_input_t st_current_input; + motor_current_output_t st_current_output; + + motor_cfg_t const * p_cfg; +} motor_sensorless_instance_ctrl_t; + +/********************************************************************************************************************** + * Exported global variables + **********************************************************************************************************************/ + +/** @cond INC_HEADER_DEFS_SEC */ +/** Filled in Interface API structure for this Instance. */ +extern const motor_api_t g_motor_on_sensorless; + +/** @endcond */ + +/*********************************************************************************************************************** + * Exported global functions (to be accessed by other files) + ***********************************************************************************************************************/ + +/********************************************************************************************************************** + * Public Function Prototypes + **********************************************************************************************************************/ +fsp_err_t RM_MOTOR_SENSORLESS_Open(motor_ctrl_t * const p_ctrl, motor_cfg_t const * const p_cfg); + +fsp_err_t RM_MOTOR_SENSORLESS_Close(motor_ctrl_t * const p_ctrl); + +fsp_err_t RM_MOTOR_SENSORLESS_Run(motor_ctrl_t * const p_ctrl); + +fsp_err_t RM_MOTOR_SENSORLESS_Stop(motor_ctrl_t * const p_ctrl); + +fsp_err_t RM_MOTOR_SENSORLESS_Reset(motor_ctrl_t * const p_ctrl); + +fsp_err_t RM_MOTOR_SENSORLESS_ErrorSet(motor_ctrl_t * const p_ctrl, motor_error_t const error); + +fsp_err_t RM_MOTOR_SENSORLESS_SpeedSet(motor_ctrl_t * const p_ctrl, float const speed_rpm); + +fsp_err_t RM_MOTOR_SENSORLESS_StatusGet(motor_ctrl_t * const p_ctrl, uint8_t * const p_status); + +fsp_err_t RM_MOTOR_SENSORLESS_AngleGet(motor_ctrl_t * const p_ctrl, float * const p_angle_rad); + +fsp_err_t RM_MOTOR_SENSORLESS_SpeedGet(motor_ctrl_t * const p_ctrl, float * const p_speed_rpm); + +fsp_err_t RM_MOTOR_SENSORLESS_ErrorCheck(motor_ctrl_t * const p_ctrl, uint16_t * const p_error); + +fsp_err_t RM_MOTOR_SENSORLESS_VersionGet(fsp_version_t * const p_version); + +/*******************************************************************************************************************//** + * @} (end addtogroup MOTOR_SENSORLESS) + **********************************************************************************************************************/ + +/* Common macro for SSP header files. There is also a corresponding FSP_HEADER macro at the top of this file. */ +FSP_FOOTER + +#endif // RM_MOTOR_SENSORLESS_H diff --git a/ra/fsp/inc/instances/rm_motor_speed.h b/ra/fsp/inc/instances/rm_motor_speed.h new file mode 100644 index 000000000..6424c2aaf --- /dev/null +++ b/ra/fsp/inc/instances/rm_motor_speed.h @@ -0,0 +1,264 @@ +/*********************************************************************************************************************** + * Copyright [2020] Renesas Electronics Corporation and/or its affiliates. All Rights Reserved. + * + * This software and documentation are supplied by Renesas Electronics America Inc. and may only be used with products + * of Renesas Electronics Corp. and its affiliates ("Renesas"). No other uses are authorized. Renesas products are + * sold pursuant to Renesas terms and conditions of sale. Purchasers are solely responsible for the selection and use + * of Renesas products and Renesas assumes no liability. No license, express or implied, to any intellectual property + * right is granted by Renesas. This software is protected under all applicable laws, including copyright laws. Renesas + * reserves the right to change or discontinue this software and/or this documentation. THE SOFTWARE AND DOCUMENTATION + * IS DELIVERED TO YOU "AS IS," AND RENESAS MAKES NO REPRESENTATIONS OR WARRANTIES, AND TO THE FULLEST EXTENT + * PERMISSIBLE UNDER APPLICABLE LAW, DISCLAIMS ALL WARRANTIES, WHETHER EXPLICITLY OR IMPLICITLY, INCLUDING WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NONINFRINGEMENT, WITH RESPECT TO THE SOFTWARE OR + * DOCUMENTATION. RENESAS SHALL HAVE NO LIABILITY ARISING OUT OF ANY SECURITY VULNERABILITY OR BREACH. TO THE MAXIMUM + * EXTENT PERMITTED BY LAW, IN NO EVENT WILL RENESAS BE LIABLE TO YOU IN CONNECTION WITH THE SOFTWARE OR DOCUMENTATION + * (OR ANY PERSON OR ENTITY CLAIMING RIGHTS DERIVED FROM YOU) FOR ANY LOSS, DAMAGES, OR CLAIMS WHATSOEVER, INCLUDING, + * WITHOUT LIMITATION, ANY DIRECT, CONSEQUENTIAL, SPECIAL, INDIRECT, PUNITIVE, OR INCIDENTAL DAMAGES; ANY LOST PROFITS, + * OTHER ECONOMIC DAMAGE, PROPERTY DAMAGE, OR PERSONAL INJURY; AND EVEN IF RENESAS HAS BEEN ADVISED OF THE POSSIBILITY + * OF SUCH LOSS, DAMAGES, CLAIMS OR COSTS. + **********************************************************************************************************************/ + +/*******************************************************************************************************************//** + * @addtogroup MOTOR_SPEED + * @{ + **********************************************************************************************************************/ + +#ifndef RM_MOTOR_SPEED_H +#define RM_MOTOR_SPEED_H + +/*********************************************************************************************************************** + * Includes + **********************************************************************************************************************/ +#include "bsp_api.h" + +#include "rm_motor_speed_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 + **********************************************************************************************************************/ +#define MOTOR_SPEED_CODE_VERSION_MAJOR (1U) +#define MOTOR_SPEED_CODE_VERSION_MINOR (0U) + +/*********************************************************************************************************************** + * Library Macro definitions + **********************************************************************************************************************/ +#define MOTOR_SPEED_FLUXWKN_DEF_VFWRATIO (0.95F) /* Defines default flux-weakening voltage ratio */ +#define MOTOR_SPEED_FLUXWKN_DEF_VFWRATIO_MIN (0.5F) /* Defines default minimum flux-weakening voltage ratio */ + +/* + * Flux-weakening-states + */ +#define MOTOR_SPEED_FLUXWKN_STATE_BYPASSED (0x0000) /* Flux-weakening control is not necessary, module is bypassed */ +#define MOTOR_SPEED_FLUXWKN_STATE_FLUXWKN (0x0001) /* Normal flux-weakening state */ +#define MOTOR_SPEED_FLUXWKN_STATE_IDSAT (0x0002) /* D-axis current has already saturated */ +#define MOTOR_SPEED_FLUXWKN_STATE_ERROR (0x8000) /* Flux-weakening general/runtime error */ +#define MOTOR_SPEED_FLUXWKN_STATE_INVALID_MOTOR (0x9001) /* Motor pointer is null or either of Ld, Lq, flux is below 0 */ +#define MOTOR_SPEED_FLUXWKN_STATE_INVALID_IAMAX (0x9002) /* Invalid maximum Ia value (Iamax < 0.0) */ +#define MOTOR_SPEED_FLUXWKN_STATE_INVALID_VAMAX (0x9003) /* Invalid maximum Va value (Vamax < 0.0) */ +#define MOTOR_SPEED_FLUXWKN_STATE_INVALID_VFWRATIO (0x9004) /* Invalid flux-weakening maximum voltage ratio, that f4_vfw_ratio + * is out of range [VFWRATIO_MIN, 1.0] */ +#define MOTOR_SPEED_FLUXWKN_CHK_MASK (0xF000) /* Mask value to check error */ +#define MOTOR_SPEED_FLUXWKN_INVALID_CHK (0x9000) /* Value to check Invalidate */ + +/*********************************************************************************************************************** + * Typedef definitions + **********************************************************************************************************************/ +typedef struct st_motor_speed_pi_params +{ + float f_err; ///< Error + float f_kp; ///< Gain of proportional term + float f_ki; ///< Gain of integral term + float f_refi; ///< Buffer of integral term + float f_ilimit; ///< Limit of integral term +} motor_speed_pi_params_t; + +/* Design parameter structure */ +typedef struct motor_speed_design_params +{ + float f_speed_omega; ///< Natural frequency[Hz] for speed loop gain design + float f_speed_zeta; ///< Damping ratio for speed loop gain design + float f_ed_hpf_omega; ///< Natural frequency[Hz] for HPF in open-loop damping gain design + float f_ol_damping_zeta; ///< Damping ratio for open-loop damping gain design + float f_phase_err_lpf_cut_freq; ///< The cut-off frequency [Hz] of phase error LPF gain design +} motor_speed_design_params_t; + +/* First order LPF structure */ +typedef struct motor_speed_lpf +{ + float f_pre_output; ///< Previous value of output + float f_pre_input; ///< Previous value of input + float f_omega_t; ///< Calculate value + float f_gain_ka; ///< LPF gain + float f_gain_kb; ///< LPF gain +} motor_speed_lpf_t; + +/* For Openloop damping */ +typedef enum e_motor_speed_openloop_damping +{ + MOTOR_SPEED_OPENLOOP_DAMPING_DISABLE = 0, + MOTOR_SPEED_OPENLOOP_DAMPING_ENABLE = 1 +} motor_speed_openloop_damping_t; + +typedef struct st_motor_speed_oldamp_sub +{ + motor_speed_lpf_t st_ed_lpf; ///< Ed LPF structure + float f_damp_comp_gain; ///< The gain for open-loop damping + float f_fb_speed_limit_rate; ///< The limit of speed feed-back rate +} motor_speed_oldamp_sub_t; + +typedef struct st_motor_speed_oldamp +{ + float f4_ol_id_up_step; ///< The d-axis current reference ramping up rate [A/ms] + float f4_ol_id_down_step; ///< The d-axis current reference ramping down rate [A/ms] + float f4_ol_iq_down_step_ratio; ///< The Iq reference ramping down proportion to Iqref before open-loop + float f4_ol_id_ref; ///< The d-axis current reference in open-loop drive + float f4_id_down_speed_rad; ///< The speed threshold[rad/s] to ramp down the d-axis current + float f4_id_up_speed_rad; ///< The speed threshold[rad/s] to ramp up the d-axis current + float f4_opl2less_sw_time; ///< Time to switch open-loop to sensor-less [s] + float f4_switch_phase_err_rad; ///< Phase error to decide sensor-less switch timing [rad] +} motor_speed_oldamp_t; + +/* For Flux Weakening */ +typedef enum e_motor_speed_flux_weaken +{ + MOTOR_SPEED_FLUX_WEAKEN_DISABLE = 0, + MOTOR_SPEED_FLUX_WEAKEN_ENABLE = 1 +} motor_speed_flux_weaken_t; + +typedef struct st_motor_speed_motor_parameter +{ + uint16_t u2_mtr_pp; ///< Pole pairs + float f4_mtr_r; ///< Resistance [ohm] + float f4_mtr_ld; ///< Inductance for d-axis [H] + float f4_mtr_lq; ///< Inductance for q-axis [H] + float f4_mtr_m; ///< Magnet flux [Wb] + float f4_mtr_j; ///< Rotor inertia [kgm^2] +} motor_speed_motor_parameter_t; + +typedef struct st_motor_speed_flux_weakening +{ + /* User configurations */ + const motor_speed_motor_parameter_t * pmotor; ///< The pointer to the motor parameter data structure + float f4_ia_max; ///< The maximum magnitude of current vector + float f4_va_max; ///< The maximum magnitude of voltage vector + float f4_vfw_ratio; ///< The maximum ratio of voltage used by flux-weakening module + + /* Internal data */ + float f4_id_demag; ///< The demagnetization current derived by (-flux/Ld) + float f4_id_min; ///< The minimum Id output, min(-Ia,Id_demag) + float f4_v_fw; ///< The target abs. limit value of voltage vector due to the total flux + uint16_t u2_fw_status; ///< The status of flux-weakening +} motor_speed_flux_weakening_t; + +typedef enum e_motor_speed_less_switch +{ + MOTOR_SPEED_LESS_SWITCH_DISABLE = 0, + MOTOR_SPEED_LESS_SWITCH_ENABLE = 1 +} motor_speed_less_switch_t; + +typedef struct st_motor_speed_extended_cfg +{ + float f_speed_ctrl_period; ///< Speed control period [usec] + float f_limit_speed_change; ///< Reference speed max change limit [rad/s] + float f_max_speed_rad; ///< Maximum speed command value [rad/s] + float f_omega_t; ///< Speed LPF design value + float f_id_up_speed_rad; ///< The speed threshold[rad/s] to ramp up the d-axis current + float f_iq_limit; ///< Limit of q-axis current [A] + + float f_ol_fb_speed_limit_rate; ///< Rate of reference speed for feedback speed limitter + + motor_speed_openloop_damping_t u1_openloop_damping; ///< Openloop Damping Active Flag + motor_speed_flux_weaken_t u1_flux_weakening; ///< Flux Weakening Active Flag + motor_speed_less_switch_t u1_less_switch; ///< Soft switching Active Flag + + motor_speed_oldamp_t ol_param; + motor_speed_oldamp_sub_t ol_sub_param; + motor_speed_design_params_t d_param; + motor_speed_motor_parameter_t mtr_param; +} motor_speed_extended_cfg_t; + +typedef struct st_motor_speed_instance_ctrl +{ + uint32_t open; + + uint8_t u1_active; ///< Flag to set active/inactive the speed control + uint8_t u1_state_speed_ref; ///< The speed control status + uint8_t u1_flag_get_iref; ///< Flag to get d/q-axis current reference + uint8_t u1_state_id_ref; ///< The d-axis current command status + uint8_t u1_state_iq_ref; ///< The q-axis current command status + float f_id_ref; ///< D-axis current reference [A] for calculation + float f_iq_ref; ///< Q-axis current reference [A] for calculation + float f_rpm2rad; ///< Coeficient to translate [rpm] to [rad/s] + float f_ref_speed_rad_ctrl; ///< Command speed value for speed PI control[rad/s] + float f_ref_speed_rad; ///< Reference speed value [rad/s] + float f_speed_lpf_rad; ///< Speed processed by LPF + + /* Openloop damping related valiable */ + uint8_t u1_flag_down_to_ol; ///< The open-loop drive flag + float f_ol_iq_down_step; ///< The q-axis current reference ramping down rate [A/ms] + float f_phase_err_rad_lpf; ///< LPF value of phase error[rad] + float f_init_phase_err_rad; ///< Initialization value of phase error rate + float f_opl_torque_current; ///< The torque current (Iq) in sensor-less switching + float f_damp_comp_speed; ///< The speed for open-loop damping + float f_damp_comp_gain; ///< The gain for open-loop damping + float f_fb_speed_limit_rate; ///< The limit of speed feed-back rate + + /* Flux Weakening related valiable */ + uint8_t u1_enable_flux_weakning; ///< The flag for enable/disable flux weakening process + motor_speed_flux_weakening_t st_flxwkn; + + motor_speed_cfg_t const * p_cfg; + motor_speed_pi_params_t pi_param; + motor_speed_input_t st_input; + motor_speed_lpf_t st_speed_lpf; + motor_speed_lpf_t st_phase_err_lpf; +} motor_speed_instance_ctrl_t; + +/********************************************************************************************************************** + * Exported global variables + **********************************************************************************************************************/ + +/** @cond INC_HEADER_DEFS_SEC */ +/** Filled in Interface API structure for this Instance. */ +extern const motor_speed_api_t g_motor_speed_on_motor_speed; + +/** @endcond */ + +/*********************************************************************************************************************** + * Exported global functions (to be accessed by other files) + ***********************************************************************************************************************/ + +/********************************************************************************************************************** + * Public Function Prototypes + **********************************************************************************************************************/ +fsp_err_t RM_MOTOR_SPEED_Open(motor_speed_ctrl_t * const p_ctrl, motor_speed_cfg_t const * const p_cfg); + +fsp_err_t RM_MOTOR_SPEED_Close(motor_speed_ctrl_t * const p_ctrl); + +fsp_err_t RM_MOTOR_SPEED_Reset(motor_speed_ctrl_t * const p_ctrl); + +fsp_err_t RM_MOTOR_SPEED_Run(motor_speed_ctrl_t * const p_ctrl); + +fsp_err_t RM_MOTOR_SPEED_SpeedReferenceSet(motor_speed_ctrl_t * const p_ctrl, float const speed_reference_rpm); + +fsp_err_t RM_MOTOR_SPEED_ParameterSet(motor_speed_ctrl_t * const p_ctrl, motor_speed_input_t const * const p_st_input); + +fsp_err_t RM_MOTOR_SPEED_SpeedControl(motor_speed_ctrl_t * const p_ctrl); + +fsp_err_t RM_MOTOR_SPEED_ParameterGet(motor_speed_ctrl_t * const p_ctrl, motor_speed_output_t * const p_st_output); + +fsp_err_t RM_MOTOR_SPEED_ParameterUpdate(motor_speed_ctrl_t * const p_ctrl, motor_speed_cfg_t const * const p_cfg); + +fsp_err_t RM_MOTOR_SPEED_VersionGet(fsp_version_t * const p_version); + +/* Common macro for FSP header files. There is also a corresponding FSP_HEADER macro at the top of this file. */ +FSP_FOOTER + +#endif // RM_MOTOR_SPEED_H + +/*******************************************************************************************************************//** + * @} (end addtogroup MOTOR_SPEED) + **********************************************************************************************************************/ diff --git a/ra/fsp/inc/instances/rm_touch.h b/ra/fsp/inc/instances/rm_touch.h index a2b58aab7..2262007b6 100644 --- a/ra/fsp/inc/instances/rm_touch.h +++ b/ra/fsp/inc/instances/rm_touch.h @@ -39,7 +39,7 @@ FSP_HEADER * Macro definitions **********************************************************************************************************************/ #define TOUCH_CODE_VERSION_MAJOR (1U) -#define TOUCH_CODE_VERSION_MINOR (1U) +#define TOUCH_CODE_VERSION_MINOR (2U) /*********************************************************************************************************************** * Typedef definitions diff --git a/ra/fsp/inc/instances/rm_vee_flash.h b/ra/fsp/inc/instances/rm_vee_flash.h index 14fb4c8ad..48b19ab1d 100644 --- a/ra/fsp/inc/instances/rm_vee_flash.h +++ b/ra/fsp/inc/instances/rm_vee_flash.h @@ -40,7 +40,7 @@ FSP_HEADER * Macro definitions **********************************************************************************************************************/ #define RM_VEE_FLASH_CODE_VERSION_MAJOR (1U) -#define RM_VEE_FLASH_CODE_VERSION_MINOR (0U) +#define RM_VEE_FLASH_CODE_VERSION_MINOR (1U) /*********************************************************************************************************************** * Typedef definitions diff --git a/ra/fsp/inc/instances/rm_wifi_onchip_silex.h b/ra/fsp/inc/instances/rm_wifi_onchip_silex.h index 3dd9c4f2b..e827e7f52 100644 --- a/ra/fsp/inc/instances/rm_wifi_onchip_silex.h +++ b/ra/fsp/inc/instances/rm_wifi_onchip_silex.h @@ -59,7 +59,7 @@ FSP_HEADER **********************************************************************************************************************/ #define WIFI_ONCHIP_SILEX_RETURN_TEXT_LENGTH (13 + 1) // Length of the tailing buffer used by command parser #define WIFI_ONCHIP_SILEX_CODE_VERSION_MAJOR (1U) -#define WIFI_ONCHIP_SILEX_CODE_VERSION_MINOR (1U) +#define WIFI_ONCHIP_SILEX_CODE_VERSION_MINOR (2U) /*********************************************************************************************************************** * Typedef definitions diff --git a/ra/fsp/lib/r_ble/cm4_ac6/all/libr_ble.a b/ra/fsp/lib/r_ble/cm4_ac6/all/libr_ble.a index cdc61f849..e3dde5a93 100644 Binary files a/ra/fsp/lib/r_ble/cm4_ac6/all/libr_ble.a and b/ra/fsp/lib/r_ble/cm4_ac6/all/libr_ble.a differ diff --git a/ra/fsp/lib/r_ble/cm4_ac6/all_freertos/libr_ble.a b/ra/fsp/lib/r_ble/cm4_ac6/all_freertos/libr_ble.a index a9f6d290b..b183ba856 100644 Binary files a/ra/fsp/lib/r_ble/cm4_ac6/all_freertos/libr_ble.a and b/ra/fsp/lib/r_ble/cm4_ac6/all_freertos/libr_ble.a differ diff --git a/ra/fsp/lib/r_ble/cm4_gcc/all/libr_ble.a b/ra/fsp/lib/r_ble/cm4_gcc/all/libr_ble.a index fec4e7600..347b278d5 100644 Binary files a/ra/fsp/lib/r_ble/cm4_gcc/all/libr_ble.a and b/ra/fsp/lib/r_ble/cm4_gcc/all/libr_ble.a differ diff --git a/ra/fsp/lib/r_ble/cm4_gcc/all_freertos/libr_ble.a b/ra/fsp/lib/r_ble/cm4_gcc/all_freertos/libr_ble.a index fcce9616d..d961a9fc5 100644 Binary files a/ra/fsp/lib/r_ble/cm4_gcc/all_freertos/libr_ble.a and b/ra/fsp/lib/r_ble/cm4_gcc/all_freertos/libr_ble.a differ diff --git a/ra/fsp/lib/r_ble/cm4_iar/all/libr_ble.a b/ra/fsp/lib/r_ble/cm4_iar/all/libr_ble.a index 4b151fb7d..6ef541a02 100644 Binary files a/ra/fsp/lib/r_ble/cm4_iar/all/libr_ble.a and b/ra/fsp/lib/r_ble/cm4_iar/all/libr_ble.a differ diff --git a/ra/fsp/lib/r_ble/cm4_iar/all_freertos/libr_ble.a b/ra/fsp/lib/r_ble/cm4_iar/all_freertos/libr_ble.a index aa03f009a..fc184c5d4 100644 Binary files a/ra/fsp/lib/r_ble/cm4_iar/all_freertos/libr_ble.a and b/ra/fsp/lib/r_ble/cm4_iar/all_freertos/libr_ble.a differ diff --git a/ra/fsp/lib/rm_motor_current/cm4_ac6/librm_motor_current.a b/ra/fsp/lib/rm_motor_current/cm4_ac6/librm_motor_current.a new file mode 100644 index 000000000..fc5b0648c Binary files /dev/null and b/ra/fsp/lib/rm_motor_current/cm4_ac6/librm_motor_current.a differ diff --git a/ra/fsp/lib/rm_motor_current/cm4_gcc/librm_motor_current.a b/ra/fsp/lib/rm_motor_current/cm4_gcc/librm_motor_current.a new file mode 100644 index 000000000..9506b368d Binary files /dev/null and b/ra/fsp/lib/rm_motor_current/cm4_gcc/librm_motor_current.a differ diff --git a/ra/fsp/lib/rm_motor_current/cm4_iar/librm_motor_current.a b/ra/fsp/lib/rm_motor_current/cm4_iar/librm_motor_current.a new file mode 100644 index 000000000..2311a48cc Binary files /dev/null and b/ra/fsp/lib/rm_motor_current/cm4_iar/librm_motor_current.a differ diff --git a/ra/fsp/lib/rm_motor_estimate/cm4_ac6/librm_motor_estimate.a b/ra/fsp/lib/rm_motor_estimate/cm4_ac6/librm_motor_estimate.a new file mode 100644 index 000000000..710ae7d0b Binary files /dev/null and b/ra/fsp/lib/rm_motor_estimate/cm4_ac6/librm_motor_estimate.a differ diff --git a/ra/fsp/lib/rm_motor_estimate/cm4_gcc/librm_motor_estimate.a b/ra/fsp/lib/rm_motor_estimate/cm4_gcc/librm_motor_estimate.a new file mode 100644 index 000000000..66185fa4a Binary files /dev/null and b/ra/fsp/lib/rm_motor_estimate/cm4_gcc/librm_motor_estimate.a differ diff --git a/ra/fsp/lib/rm_motor_estimate/cm4_iar/librm_motor_estimate.a b/ra/fsp/lib/rm_motor_estimate/cm4_iar/librm_motor_estimate.a new file mode 100644 index 000000000..565489548 Binary files /dev/null and b/ra/fsp/lib/rm_motor_estimate/cm4_iar/librm_motor_estimate.a differ diff --git a/ra/fsp/lib/rm_motor_speed/cm4_ac6/librm_motor_speed.a b/ra/fsp/lib/rm_motor_speed/cm4_ac6/librm_motor_speed.a new file mode 100644 index 000000000..b7fa49691 Binary files /dev/null and b/ra/fsp/lib/rm_motor_speed/cm4_ac6/librm_motor_speed.a differ diff --git a/ra/fsp/lib/rm_motor_speed/cm4_gcc/librm_motor_speed.a b/ra/fsp/lib/rm_motor_speed/cm4_gcc/librm_motor_speed.a new file mode 100644 index 000000000..291b5fad5 Binary files /dev/null and b/ra/fsp/lib/rm_motor_speed/cm4_gcc/librm_motor_speed.a differ diff --git a/ra/fsp/lib/rm_motor_speed/cm4_iar/librm_motor_speed.a b/ra/fsp/lib/rm_motor_speed/cm4_iar/librm_motor_speed.a new file mode 100644 index 000000000..b0ad1d80d Binary files /dev/null and b/ra/fsp/lib/rm_motor_speed/cm4_iar/librm_motor_speed.a differ diff --git a/ra/fsp/src/bsp/mcu/all/bsp_clocks.c b/ra/fsp/src/bsp/mcu/all/bsp_clocks.c index 0855b1a0b..c532c2e3d 100644 --- a/ra/fsp/src/bsp/mcu/all/bsp_clocks.c +++ b/ra/fsp/src/bsp/mcu/all/bsp_clocks.c @@ -1022,22 +1022,12 @@ void bsp_clock_init (void) /* Set the OCTASPI clock if it exists on the MCU (See section 8.2.30 of the RA6M4 hardware manual R01UH0890EJ0050). */ #if BSP_FEATURE_BSP_HAS_OCTASPI_CLOCK && BSP_CFG_OCTA_SOURCE != BSP_CLOCKS_CLOCK_DISABLED - - /* Request to change the OCTASPI Clock. */ - R_SYSTEM->OCTACKCR_b.OCTACKSREQ = 1; - - /* Wait for the clock to be stopped. */ - FSP_HARDWARE_REGISTER_WAIT(R_SYSTEM->OCTACKCR_b.OCTACKSRDY, 1U); - - /* Write the settings. */ - R_SYSTEM->OCTACKDIVCR = BSP_CFG_OCTA_DIV; - R_SYSTEM->OCTACKCR = BSP_CFG_OCTA_SOURCE | R_SYSTEM_OCTACKCR_OCTACKSREQ_Msk; - - /* Start the OCTASPI Clock. */ - R_SYSTEM->OCTACKCR = BSP_CFG_OCTA_SOURCE; - - /* Wait for the OCTASPI Clock to be started. */ - FSP_HARDWARE_REGISTER_WAIT(R_SYSTEM->OCTACKCR_b.OCTACKSRDY, 0U); + bsp_octaclk_settings_t octaclk_settings = + { + .source_clock = (bsp_clocks_source_t) BSP_CFG_OCTA_SOURCE, + .divider = (bsp_clocks_octaclk_div_t) BSP_CFG_OCTA_DIV + }; + R_BSP_OctaclkUpdate(&octaclk_settings); #endif /* BSP_FEATURE_BSP_HAS_OCTASPI_CLOCK && BSP_CFG_OCTASPI_CLOCK_ENABLE */ /* Lock CGC and LPM protection registers. */ @@ -1185,4 +1175,43 @@ static void bsp_clock_set_postchange (uint32_t updated_freq_hz, uint8_t new_rom_ #endif } +/*******************************************************************************************************************//** + * Octa-SPI clock update. + * @param[in] p_octaclk_setting Pointer to Octaclk setting structure which provides information regarding + * Octaclk source and divider settings to be applied. + * @note The requested Octaclk source must be started before calling this function. + **********************************************************************************************************************/ +void R_BSP_OctaclkUpdate (bsp_octaclk_settings_t * p_octaclk_setting) +{ +#if BSP_FEATURE_BSP_HAS_OCTASPI_CLOCK + + /* Store initial value of CGC and LPM protection registers. */ + uint16_t bsp_prv_prcr_orig = R_SYSTEM->PRCR; + + /* Unlock CGC and LPM protection registers. */ + R_SYSTEM->PRCR = (uint16_t) BSP_PRV_PRCR_UNLOCK; + + /* Request to change the OCTASPI Clock. */ + R_SYSTEM->OCTACKCR_b.OCTACKSREQ = 1; + + /* Wait for the clock to be stopped. */ + FSP_HARDWARE_REGISTER_WAIT(R_SYSTEM->OCTACKCR_b.OCTACKSRDY, 1U); + + /* Write the settings. */ + R_SYSTEM->OCTACKDIVCR = (uint8_t) p_octaclk_setting->divider; + R_SYSTEM->OCTACKCR = (uint8_t) (p_octaclk_setting->source_clock | R_SYSTEM_OCTACKCR_OCTACKSREQ_Msk); + + /* Start the OCTASPI Clock. */ + R_SYSTEM->OCTACKCR = BSP_CLOCKS_SOURCE_CLOCK_PLL2; + + /* Wait for the OCTASPI Clock to be started. */ + FSP_HARDWARE_REGISTER_WAIT(R_SYSTEM->OCTACKCR_b.OCTACKSRDY, 0U); + + /* Restore CGC and LPM protection registers. */ + R_SYSTEM->PRCR = bsp_prv_prcr_orig; +#else + FSP_PARAMETER_NOT_USED(p_octaclk_setting); +#endif +} + /** @} (end addtogroup BSP_MCU_PRV) */ diff --git a/ra/fsp/src/bsp/mcu/all/bsp_clocks.h b/ra/fsp/src/bsp/mcu/all/bsp_clocks.h index 0491b67b9..ca4764ebe 100644 --- a/ra/fsp/src/bsp/mcu/all/bsp_clocks.h +++ b/ra/fsp/src/bsp/mcu/all/bsp_clocks.h @@ -262,8 +262,8 @@ typedef struct } bsp_clock_update_callback_args_t; #if defined(__ARMCC_VERSION) || defined(__ICCARM__) -typedef void (BSP_CMSE_NONSECURE_CALL * volatile bsp_clock_update_callback_t)(bsp_clock_update_callback_args_t * - p_callback_args); +typedef void (BSP_CMSE_NONSECURE_CALL * bsp_clock_update_callback_t)(bsp_clock_update_callback_args_t * + p_callback_args); #elif defined(__GNUC__) typedef BSP_CMSE_NONSECURE_CALL void (*volatile bsp_clock_update_callback_t)(bsp_clock_update_callback_args_t * p_callback_args); diff --git a/ra/fsp/src/bsp/mcu/all/bsp_common.h b/ra/fsp/src/bsp/mcu/all/bsp_common.h index 62214275d..cdf2f8fe7 100644 --- a/ra/fsp/src/bsp/mcu/all/bsp_common.h +++ b/ra/fsp/src/bsp/mcu/all/bsp_common.h @@ -54,7 +54,7 @@ FSP_HEADER /* Version of this module's code and API. */ #define BSP_CODE_VERSION_MAJOR (1U) -#define BSP_CODE_VERSION_MINOR (1U) +#define BSP_CODE_VERSION_MINOR (2U) #define BSP_API_VERSION_MAJOR (1U) #define BSP_API_VERSION_MINOR (0U) diff --git a/ra/fsp/src/bsp/mcu/all/bsp_irq.c b/ra/fsp/src/bsp/mcu/all/bsp_irq.c index 5a7fe5b6e..c77f80259 100644 --- a/ra/fsp/src/bsp/mcu/all/bsp_irq.c +++ b/ra/fsp/src/bsp/mcu/all/bsp_irq.c @@ -45,7 +45,10 @@ void * gp_renesas_isr_context[BSP_ICU_VECTOR_MAX_ENTRIES]; /*********************************************************************************************************************** * Private global variables and functions **********************************************************************************************************************/ -const bsp_interrupt_event_t g_interrupt_event_link_select[BSP_ICU_VECTOR_MAX_ENTRIES] BSP_WEAK_REFERENCE; +const bsp_interrupt_event_t g_interrupt_event_link_select[BSP_ICU_VECTOR_MAX_ENTRIES] BSP_WEAK_REFERENCE = +{ + (bsp_interrupt_event_t) 0 +}; /*******************************************************************************************************************//** * @addtogroup BSP_MCU diff --git a/ra/fsp/src/bsp/mcu/all/bsp_mcu_api.h b/ra/fsp/src/bsp/mcu/all/bsp_mcu_api.h index f0c07c6fd..78a4c484e 100644 --- a/ra/fsp/src/bsp/mcu/all/bsp_mcu_api.h +++ b/ra/fsp/src/bsp/mcu/all/bsp_mcu_api.h @@ -30,9 +30,36 @@ typedef struct st_bsp_event_info elc_event_t event; } bsp_event_info_t; +typedef enum e_bsp_clocks_octaclk_div +{ + BSP_CLOCKS_OCTACLK_DIV_1 = 0, ///< Divide OCTA source clock by 1 + BSP_CLOCKS_OCTACLK_DIV_2, ///< Divide OCTA source clock by 2 + BSP_CLOCKS_OCTACLK_DIV_4, ///< Divide OCTA source clock by 4 + BSP_CLOCKS_OCTACLK_DIV_6, ///< Divide OCTA source clock by 6 + BSP_CLOCKS_OCTACLK_DIV_8 ///< Divide OCTA source clock by 8 +} bsp_clocks_octaclk_div_t; + +typedef enum e_bsp_clocks_source +{ + BSP_CLOCKS_CLOCK_HOCO = 0, ///< The high speed on chip oscillator. + BSP_CLOCKS_CLOCK_MOCO, ///< The middle speed on chip oscillator. + BSP_CLOCKS_CLOCK_LOCO, ///< The low speed on chip oscillator. + BSP_CLOCKS_CLOCK_MAIN_OSC, ///< The main oscillator. + BSP_CLOCKS_CLOCK_SUBCLOCK, ///< The subclock oscillator. + BSP_CLOCKS_CLOCK_PLL, ///< The PLL oscillator. + BSP_CLOCKS_CLOCK_PLL2, ///< The PLL2 oscillator. +} bsp_clocks_source_t; + +typedef struct st_bsp_octaclk_settings +{ + bsp_clocks_source_t source_clock; ///< OCTACLK source clock + bsp_clocks_octaclk_div_t divider; ///< OCTACLK divider +} bsp_octaclk_settings_t; + void R_BSP_RegisterProtectEnable(bsp_reg_protect_t regs_to_protect); void R_BSP_RegisterProtectDisable(bsp_reg_protect_t regs_to_unprotect); fsp_err_t R_BSP_GroupIrqWrite(bsp_grp_irq_t irq, void (* p_callback)(bsp_grp_irq_t irq)); +void R_BSP_OctaclkUpdate(bsp_octaclk_settings_t * p_octaclk_setting); void R_BSP_SoftwareDelay(uint32_t delay, bsp_delay_units_t units); fsp_err_t R_BSP_VersionGet(fsp_version_t * p_version); diff --git a/ra/fsp/src/bsp/mcu/all/bsp_security.c b/ra/fsp/src/bsp/mcu/all/bsp_security.c index 537b07b60..0742dfb18 100644 --- a/ra/fsp/src/bsp/mcu/all/bsp_security.c +++ b/ra/fsp/src/bsp/mcu/all/bsp_security.c @@ -49,7 +49,7 @@ void R_BSP_ElcCfgSecurityInit(void); extern const fsp_vector_t g_vector_table[BSP_ICU_VECTOR_MAX_ENTRIES]; #if defined(__ARMCC_VERSION) || defined(__ICCARM__) -typedef void (BSP_CMSE_NONSECURE_CALL * volatile bsp_nonsecure_func_t)(void); +typedef void (BSP_CMSE_NONSECURE_CALL * bsp_nonsecure_func_t)(void); #elif defined(__GNUC__) typedef BSP_CMSE_NONSECURE_CALL void (*volatile bsp_nonsecure_func_t)(void); #endif @@ -63,9 +63,11 @@ typedef BSP_CMSE_NONSECURE_CALL void (*volatile bsp_nonsecure_func_t)(void); #pragma section=".tz_sdram_ns_start" #pragma section=".tz_qspi_flash_ns_start" -/* &__tz__C is the address of the non-secure callable section. */ +/* &__tz__C is the address of the non-secure callable section. Must assign value to this variable or + * linker will give error. */ + /* &__tz__N is the start address of the non-secure region. */ -BSP_DONT_REMOVE void const * const __tz_FLASH_C BSP_ALIGN_VARIABLE(1024) @".tz_flash_nsc_start"; +BSP_DONT_REMOVE void const * const __tz_FLASH_C BSP_ALIGN_VARIABLE(1024) @".tz_flash_nsc_start" = 0; BSP_DONT_REMOVE void const * const __tz_FLASH_N BSP_ALIGN_VARIABLE(32768) @".tz_flash_ns_start"; BSP_DONT_REMOVE void const * const __tz_RAM_C BSP_ALIGN_VARIABLE(1024) @".tz_ram_nsc_start"; BSP_DONT_REMOVE void const * const __tz_RAM_N BSP_ALIGN_VARIABLE(8192) @".tz_ram_ns_start"; diff --git a/ra/fsp/src/bsp/mcu/ra6t1/bsp_elc.h b/ra/fsp/src/bsp/mcu/ra6t1/bsp_elc.h new file mode 100644 index 000000000..1f4b551e6 --- /dev/null +++ b/ra/fsp/src/bsp/mcu/ra6t1/bsp_elc.h @@ -0,0 +1,318 @@ +/*********************************************************************************************************************** + * Copyright [2020] Renesas Electronics Corporation and/or its affiliates. All Rights Reserved. + * + * This software and documentation are supplied by Renesas Electronics America Inc. and may only be used with products + * of Renesas Electronics Corp. and its affiliates ("Renesas"). No other uses are authorized. Renesas products are + * sold pursuant to Renesas terms and conditions of sale. Purchasers are solely responsible for the selection and use + * of Renesas products and Renesas assumes no liability. No license, express or implied, to any intellectual property + * right is granted by Renesas. This software is protected under all applicable laws, including copyright laws. Renesas + * reserves the right to change or discontinue this software and/or this documentation. THE SOFTWARE AND DOCUMENTATION + * IS DELIVERED TO YOU "AS IS," AND RENESAS MAKES NO REPRESENTATIONS OR WARRANTIES, AND TO THE FULLEST EXTENT + * PERMISSIBLE UNDER APPLICABLE LAW, DISCLAIMS ALL WARRANTIES, WHETHER EXPLICITLY OR IMPLICITLY, INCLUDING WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NONINFRINGEMENT, WITH RESPECT TO THE SOFTWARE OR + * DOCUMENTATION. RENESAS SHALL HAVE NO LIABILITY ARISING OUT OF ANY SECURITY VULNERABILITY OR BREACH. TO THE MAXIMUM + * EXTENT PERMITTED BY LAW, IN NO EVENT WILL RENESAS BE LIABLE TO YOU IN CONNECTION WITH THE SOFTWARE OR DOCUMENTATION + * (OR ANY PERSON OR ENTITY CLAIMING RIGHTS DERIVED FROM YOU) FOR ANY LOSS, DAMAGES, OR CLAIMS WHATSOEVER, INCLUDING, + * WITHOUT LIMITATION, ANY DIRECT, CONSEQUENTIAL, SPECIAL, INDIRECT, PUNITIVE, OR INCIDENTAL DAMAGES; ANY LOST PROFITS, + * OTHER ECONOMIC DAMAGE, PROPERTY DAMAGE, OR PERSONAL INJURY; AND EVEN IF RENESAS HAS BEEN ADVISED OF THE POSSIBILITY + * OF SUCH LOSS, DAMAGES, CLAIMS OR COSTS. + **********************************************************************************************************************/ + +#ifndef BSP_ELC_H +#define BSP_ELC_H + +/*********************************************************************************************************************** + * Macro definitions + **********************************************************************************************************************/ + +/*********************************************************************************************************************** + * Typedef definitions + **********************************************************************************************************************/ + +/*********************************************************************************************************************** + * Exported global variables + **********************************************************************************************************************/ + +/*********************************************************************************************************************** + * Exported global functions (to be accessed by other files) + **********************************************************************************************************************/ + +/*******************************************************************************************************************//** + * @addtogroup BSP_MCU_RA6T1 + * @{ + **********************************************************************************************************************/ + +/** Sources of event signals to be linked to other peripherals or the CPU + * @note This list may change based on based on the device. + * */ +typedef enum e_elc_event_ra6t1 +{ + ELC_EVENT_NONE = (0), // Link disabled + ELC_EVENT_ICU_IRQ0 = (0x001), // External pin interrupt 0 + ELC_EVENT_ICU_IRQ1 = (0x002), // External pin interrupt 1 + ELC_EVENT_ICU_IRQ2 = (0x003), // External pin interrupt 2 + ELC_EVENT_ICU_IRQ3 = (0x004), // External pin interrupt 3 + ELC_EVENT_ICU_IRQ4 = (0x005), // External pin interrupt 4 + ELC_EVENT_ICU_IRQ5 = (0x006), // External pin interrupt 5 + ELC_EVENT_ICU_IRQ6 = (0x007), // External pin interrupt 6 + ELC_EVENT_ICU_IRQ7 = (0x008), // External pin interrupt 7 + ELC_EVENT_ICU_IRQ8 = (0x009), // External pin interrupt 8 + ELC_EVENT_ICU_IRQ9 = (0x00A), // External pin interrupt 9 + ELC_EVENT_ICU_IRQ10 = (0x00B), // External pin interrupt 10 + ELC_EVENT_ICU_IRQ11 = (0x00C), // External pin interrupt 11 + ELC_EVENT_ICU_IRQ12 = (0x00D), // External pin interrupt 12 + ELC_EVENT_ICU_IRQ13 = (0x00E), // External pin interrupt 13 + ELC_EVENT_DMAC0_INT = (0x020), // DMAC transfer end 0 + ELC_EVENT_DMAC1_INT = (0x021), // DMAC transfer end 1 + ELC_EVENT_DMAC2_INT = (0x022), // DMAC transfer end 2 + ELC_EVENT_DMAC3_INT = (0x023), // DMAC transfer end 3 + ELC_EVENT_DMAC4_INT = (0x024), // DMAC transfer end 4 + ELC_EVENT_DMAC5_INT = (0x025), // DMAC transfer end 5 + ELC_EVENT_DMAC6_INT = (0x026), // DMAC transfer end 6 + ELC_EVENT_DMAC7_INT = (0x027), // DMAC transfer end 7 + ELC_EVENT_DTC_COMPLETE = (0x02A), // DTC last transfer + ELC_EVENT_ICU_SNOOZE_CANCEL = (0x02D), // Canceling from Snooze mode + ELC_EVENT_FCU_FIFERR = (0x030), // Flash access error interrupt + ELC_EVENT_FCU_FRDYI = (0x031), // Flash ready interrupt + ELC_EVENT_LVD_LVD1 = (0x038), // Voltage monitor 1 interrupt + ELC_EVENT_LVD_LVD2 = (0x039), // Voltage monitor 2 interrupt + ELC_EVENT_CGC_MOSC_STOP = (0x03B), // Main Clock oscillation stop + ELC_EVENT_LPM_SNOOZE_REQUEST = (0x03C), // Snooze entry + ELC_EVENT_AGT0_INT = (0x040), // AGT interrupt + ELC_EVENT_AGT0_COMPARE_A = (0x041), // Compare match A + ELC_EVENT_AGT0_COMPARE_B = (0x042), // Compare match B + ELC_EVENT_AGT1_INT = (0x043), // AGT interrupt + ELC_EVENT_AGT1_COMPARE_A = (0x044), // Compare match A + ELC_EVENT_AGT1_COMPARE_B = (0x045), // Compare match B + ELC_EVENT_IWDT_UNDERFLOW = (0x046), // IWDT underflow + ELC_EVENT_WDT_UNDERFLOW = (0x047), // WDT underflow + ELC_EVENT_ADC0_SCAN_END = (0x04B), // A/D scan end interrupt + ELC_EVENT_ADC0_SCAN_END_B = (0x04C), // A/D scan end interrupt for group B + ELC_EVENT_ADC0_WINDOW_A = (0x04D), // Window A Compare match + ELC_EVENT_ADC0_WINDOW_B = (0x04E), // Window B Compare match + ELC_EVENT_ADC0_COMPARE_MATCH = (0x04F), // Compare match + ELC_EVENT_ADC0_COMPARE_MISMATCH = (0x050), // Compare mismatch + ELC_EVENT_ADC1_SCAN_END = (0x051), // A/D scan end interrupt + ELC_EVENT_ADC1_SCAN_END_B = (0x052), // A/D scan end interrupt for group B + ELC_EVENT_ADC1_WINDOW_A = (0x053), // Window A Compare match + ELC_EVENT_ADC1_WINDOW_B = (0x054), // Window B Compare match + ELC_EVENT_ADC1_COMPARE_MATCH = (0x055), // Compare match + ELC_EVENT_ADC1_COMPARE_MISMATCH = (0x056), // Compare mismatch + ELC_EVENT_ACMPHS0_INT = (0x057), // Comparator interrupt 0 + ELC_EVENT_ACMPHS1_INT = (0x058), // Comparator interrupt 1 + ELC_EVENT_ACMPHS2_INT = (0x059), // Comparator interrupt 2 + ELC_EVENT_ACMPHS3_INT = (0x05A), // Comparator interrupt 3 + ELC_EVENT_ACMPHS4_INT = (0x05B), // Comparator interrupt 4 + ELC_EVENT_ACMPHS5_INT = (0x05C), // Comparator interrupt 5 + ELC_EVENT_IIC0_RXI = (0x063), // Receive data full + ELC_EVENT_IIC0_TXI = (0x064), // Transmit data empty + ELC_EVENT_IIC0_TEI = (0x065), // Transmit end + ELC_EVENT_IIC0_ERI = (0x066), // Transfer error + ELC_EVENT_IIC0_WUI = (0x067), // Slave address match + ELC_EVENT_IIC1_RXI = (0x068), // Receive data full + ELC_EVENT_IIC1_TXI = (0x069), // Transmit data empty + ELC_EVENT_IIC1_TEI = (0x06A), // Transmit end + ELC_EVENT_IIC1_ERI = (0x06B), // Transfer error + ELC_EVENT_KEY_INT = (0x085), // Key interrupt + ELC_EVENT_DOC_INT = (0x086), // Data operation circuit interrupt + ELC_EVENT_CAC_FREQUENCY_ERROR = (0x087), // Frequency error interrupt + ELC_EVENT_CAC_MEASUREMENT_END = (0x088), // Measurement end interrupt + ELC_EVENT_CAC_OVERFLOW = (0x089), // Overflow interrupt + ELC_EVENT_CAN0_ERROR = (0x08A), // Error interrupt + ELC_EVENT_CAN0_FIFO_RX = (0x08B), // Receive FIFO interrupt + ELC_EVENT_CAN0_FIFO_TX = (0x08C), // Transmit FIFO interrupt + ELC_EVENT_CAN0_MAILBOX_RX = (0x08D), // Reception complete interrupt + ELC_EVENT_CAN0_MAILBOX_TX = (0x08E), // Transmission complete interrupt + ELC_EVENT_IOPORT_EVENT_1 = (0x094), // Port 1 event + ELC_EVENT_IOPORT_EVENT_2 = (0x095), // Port 2 event + ELC_EVENT_IOPORT_EVENT_3 = (0x096), // Port 3 event + ELC_EVENT_IOPORT_EVENT_4 = (0x097), // Port 4 event + ELC_EVENT_ELC_SOFTWARE_EVENT_0 = (0x098), // Software event 0 + ELC_EVENT_ELC_SOFTWARE_EVENT_1 = (0x099), // Software event 1 + ELC_EVENT_POEG0_EVENT = (0x09A), // Port Output disable interrupt A + ELC_EVENT_POEG1_EVENT = (0x09B), // Port Output disable interrupt B + ELC_EVENT_POEG2_EVENT = (0x09C), // Port Output disable interrupt C + ELC_EVENT_POEG3_EVENT = (0x09D), // Port Output disable interrupt D + ELC_EVENT_GPT0_CAPTURE_COMPARE_A = (0x0B0), // Compare match A + ELC_EVENT_GPT0_CAPTURE_COMPARE_B = (0x0B1), // Compare match B + ELC_EVENT_GPT0_COMPARE_C = (0x0B2), // Compare match C + ELC_EVENT_GPT0_COMPARE_D = (0x0B3), // Compare match D + ELC_EVENT_GPT0_COMPARE_E = (0x0B4), // Compare match E + ELC_EVENT_GPT0_COMPARE_F = (0x0B5), // Compare match F + ELC_EVENT_GPT0_COUNTER_OVERFLOW = (0x0B6), // Overflow + ELC_EVENT_GPT0_COUNTER_UNDERFLOW = (0x0B7), // Underflow + ELC_EVENT_GPT0_AD_TRIG_A = (0x0B8), // A/D converter start request A + ELC_EVENT_GPT0_AD_TRIG_B = (0x0B9), // A/D converter start request B + ELC_EVENT_GPT1_CAPTURE_COMPARE_A = (0x0BA), // Compare match A + ELC_EVENT_GPT1_CAPTURE_COMPARE_B = (0x0BB), // Compare match B + ELC_EVENT_GPT1_COMPARE_C = (0x0BC), // Compare match C + ELC_EVENT_GPT1_COMPARE_D = (0x0BD), // Compare match D + ELC_EVENT_GPT1_COMPARE_E = (0x0BE), // Compare match E + ELC_EVENT_GPT1_COMPARE_F = (0x0BF), // Compare match F + ELC_EVENT_GPT1_COUNTER_OVERFLOW = (0x0C0), // Overflow + ELC_EVENT_GPT1_COUNTER_UNDERFLOW = (0x0C1), // Underflow + ELC_EVENT_GPT1_AD_TRIG_A = (0x0C2), // A/D converter start request A + ELC_EVENT_GPT1_AD_TRIG_B = (0x0C3), // A/D converter start request B + ELC_EVENT_GPT2_CAPTURE_COMPARE_A = (0x0C4), // Compare match A + ELC_EVENT_GPT2_CAPTURE_COMPARE_B = (0x0C5), // Compare match B + ELC_EVENT_GPT2_COMPARE_C = (0x0C6), // Compare match C + ELC_EVENT_GPT2_COMPARE_D = (0x0C7), // Compare match D + ELC_EVENT_GPT2_COMPARE_E = (0x0C8), // Compare match E + ELC_EVENT_GPT2_COMPARE_F = (0x0C9), // Compare match F + ELC_EVENT_GPT2_COUNTER_OVERFLOW = (0x0CA), // Overflow + ELC_EVENT_GPT2_COUNTER_UNDERFLOW = (0x0CB), // Underflow + ELC_EVENT_GPT2_AD_TRIG_A = (0x0CC), // A/D converter start request A + ELC_EVENT_GPT2_AD_TRIG_B = (0x0CD), // A/D converter start request B + ELC_EVENT_GPT3_CAPTURE_COMPARE_A = (0x0CE), // Compare match A + ELC_EVENT_GPT3_CAPTURE_COMPARE_B = (0x0CF), // Compare match B + ELC_EVENT_GPT3_COMPARE_C = (0x0D0), // Compare match C + ELC_EVENT_GPT3_COMPARE_D = (0x0D1), // Compare match D + ELC_EVENT_GPT3_COMPARE_E = (0x0D2), // Compare match E + ELC_EVENT_GPT3_COMPARE_F = (0x0D3), // Compare match F + ELC_EVENT_GPT3_COUNTER_OVERFLOW = (0x0D4), // Overflow + ELC_EVENT_GPT3_COUNTER_UNDERFLOW = (0x0D5), // Underflow + ELC_EVENT_GPT3_AD_TRIG_A = (0x0D6), // A/D converter start request A + ELC_EVENT_GPT3_AD_TRIG_B = (0x0D7), // A/D converter start request B + ELC_EVENT_GPT4_CAPTURE_COMPARE_A = (0x0D8), // Compare match A + ELC_EVENT_GPT4_CAPTURE_COMPARE_B = (0x0D9), // Compare match B + ELC_EVENT_GPT4_COMPARE_C = (0x0DA), // Compare match C + ELC_EVENT_GPT4_COMPARE_D = (0x0DB), // Compare match D + ELC_EVENT_GPT4_COMPARE_E = (0x0DC), // Compare match E + ELC_EVENT_GPT4_COMPARE_F = (0x0DD), // Compare match F + ELC_EVENT_GPT4_COUNTER_OVERFLOW = (0x0DE), // Overflow + ELC_EVENT_GPT4_COUNTER_UNDERFLOW = (0x0DF), // Underflow + ELC_EVENT_GPT4_AD_TRIG_A = (0x0E0), // A/D converter start request A + ELC_EVENT_GPT4_AD_TRIG_B = (0x0E1), // A/D converter start request B + ELC_EVENT_GPT5_CAPTURE_COMPARE_A = (0x0E2), // Compare match A + ELC_EVENT_GPT5_CAPTURE_COMPARE_B = (0x0E3), // Compare match B + ELC_EVENT_GPT5_COMPARE_C = (0x0E4), // Compare match C + ELC_EVENT_GPT5_COMPARE_D = (0x0E5), // Compare match D + ELC_EVENT_GPT5_COMPARE_E = (0x0E6), // Compare match E + ELC_EVENT_GPT5_COMPARE_F = (0x0E7), // Compare match F + ELC_EVENT_GPT5_COUNTER_OVERFLOW = (0x0E8), // Overflow + ELC_EVENT_GPT5_COUNTER_UNDERFLOW = (0x0E9), // Underflow + ELC_EVENT_GPT5_AD_TRIG_A = (0x0EA), // A/D converter start request A + ELC_EVENT_GPT5_AD_TRIG_B = (0x0EB), // A/D converter start request B + ELC_EVENT_GPT6_CAPTURE_COMPARE_A = (0x0EC), // Compare match A + ELC_EVENT_GPT6_CAPTURE_COMPARE_B = (0x0ED), // Compare match B + ELC_EVENT_GPT6_COMPARE_C = (0x0EE), // Compare match C + ELC_EVENT_GPT6_COMPARE_D = (0x0EF), // Compare match D + ELC_EVENT_GPT6_COMPARE_E = (0x0F0), // Compare match E + ELC_EVENT_GPT6_COMPARE_F = (0x0F1), // Compare match F + ELC_EVENT_GPT6_COUNTER_OVERFLOW = (0x0F2), // Overflow + ELC_EVENT_GPT6_COUNTER_UNDERFLOW = (0x0F3), // Underflow + ELC_EVENT_GPT6_AD_TRIG_A = (0x0F4), // A/D converter start request A + ELC_EVENT_GPT6_AD_TRIG_B = (0x0F5), // A/D converter start request B + ELC_EVENT_GPT7_CAPTURE_COMPARE_A = (0x0F6), // Compare match A + ELC_EVENT_GPT7_CAPTURE_COMPARE_B = (0x0F7), // Compare match B + ELC_EVENT_GPT7_COMPARE_C = (0x0F8), // Compare match C + ELC_EVENT_GPT7_COMPARE_D = (0x0F9), // Compare match D + ELC_EVENT_GPT7_COMPARE_E = (0x0FA), // Compare match E + ELC_EVENT_GPT7_COMPARE_F = (0x0FB), // Compare match F + ELC_EVENT_GPT7_COUNTER_OVERFLOW = (0x0FC), // Overflow + ELC_EVENT_GPT7_COUNTER_UNDERFLOW = (0x0FD), // Underflow + ELC_EVENT_GPT7_AD_TRIG_A = (0x0FE), // A/D converter start request A + ELC_EVENT_GPT7_AD_TRIG_B = (0x0FF), // A/D converter start request B + ELC_EVENT_GPT8_CAPTURE_COMPARE_A = (0x100), // Compare match A + ELC_EVENT_GPT8_CAPTURE_COMPARE_B = (0x101), // Compare match B + ELC_EVENT_GPT8_COMPARE_C = (0x102), // Compare match C + ELC_EVENT_GPT8_COMPARE_D = (0x103), // Compare match D + ELC_EVENT_GPT8_COMPARE_E = (0x104), // Compare match E + ELC_EVENT_GPT8_COMPARE_F = (0x105), // Compare match F + ELC_EVENT_GPT8_COUNTER_OVERFLOW = (0x106), // Overflow + ELC_EVENT_GPT8_COUNTER_UNDERFLOW = (0x107), // Underflow + ELC_EVENT_GPT9_CAPTURE_COMPARE_A = (0x10A), // Compare match A + ELC_EVENT_GPT9_CAPTURE_COMPARE_B = (0x10B), // Compare match B + ELC_EVENT_GPT9_COMPARE_C = (0x10C), // Compare match C + ELC_EVENT_GPT9_COMPARE_D = (0x10D), // Compare match D + ELC_EVENT_GPT9_COMPARE_E = (0x10E), // Compare match E + ELC_EVENT_GPT9_COMPARE_F = (0x10F), // Compare match F + ELC_EVENT_GPT9_COUNTER_OVERFLOW = (0x110), // Overflow + ELC_EVENT_GPT9_COUNTER_UNDERFLOW = (0x111), // Underflow + ELC_EVENT_GPT10_CAPTURE_COMPARE_A = (0x114), // Compare match A + ELC_EVENT_GPT10_CAPTURE_COMPARE_B = (0x115), // Compare match B + ELC_EVENT_GPT10_COMPARE_C = (0x116), // Compare match C + ELC_EVENT_GPT10_COMPARE_D = (0x117), // Compare match D + ELC_EVENT_GPT10_COMPARE_E = (0x118), // Compare match E + ELC_EVENT_GPT10_COMPARE_F = (0x119), // Compare match F + ELC_EVENT_GPT10_COUNTER_OVERFLOW = (0x11A), // Overflow + ELC_EVENT_GPT10_COUNTER_UNDERFLOW = (0x11B), // Underflow + ELC_EVENT_GPT11_CAPTURE_COMPARE_A = (0x11E), // Compare match A + ELC_EVENT_GPT11_CAPTURE_COMPARE_B = (0x11F), // Compare match B + ELC_EVENT_GPT11_COMPARE_C = (0x120), // Compare match C + ELC_EVENT_GPT11_COMPARE_D = (0x121), // Compare match D + ELC_EVENT_GPT11_COMPARE_E = (0x122), // Compare match E + ELC_EVENT_GPT11_COMPARE_F = (0x123), // Compare match F + ELC_EVENT_GPT11_COUNTER_OVERFLOW = (0x124), // Overflow + ELC_EVENT_GPT11_COUNTER_UNDERFLOW = (0x125), // Underflow + ELC_EVENT_GPT12_CAPTURE_COMPARE_A = (0x128), // Compare match A + ELC_EVENT_GPT12_CAPTURE_COMPARE_B = (0x129), // Compare match B + ELC_EVENT_GPT12_COMPARE_C = (0x12A), // Compare match C + ELC_EVENT_GPT12_COMPARE_D = (0x12B), // Compare match D + ELC_EVENT_GPT12_COMPARE_E = (0x12C), // Compare match E + ELC_EVENT_GPT12_COMPARE_F = (0x12D), // Compare match F + ELC_EVENT_GPT12_COUNTER_OVERFLOW = (0x12E), // Overflow + ELC_EVENT_GPT12_COUNTER_UNDERFLOW = (0x12F), // Underflow + ELC_EVENT_OPS_UVW_EDGE = (0x150), // UVW edge event + ELC_EVENT_SCI0_RXI = (0x174), // Receive data full + ELC_EVENT_SCI0_TXI = (0x175), // Transmit data empty + ELC_EVENT_SCI0_TEI = (0x176), // Transmit end + ELC_EVENT_SCI0_ERI = (0x177), // Receive error + ELC_EVENT_SCI0_AM = (0x178), // Address match event + ELC_EVENT_SCI0_RXI_OR_ERI = (0x179), // Receive data full/Receive + ELC_EVENT_SCI1_RXI = (0x17A), // Received data full + ELC_EVENT_SCI1_TXI = (0x17B), // Transmit data empty + ELC_EVENT_SCI1_TEI = (0x17C), // Transmit end + ELC_EVENT_SCI1_ERI = (0x17D), // Receive error + ELC_EVENT_SCI1_AM = (0x17E), // Address match event + ELC_EVENT_SCI2_RXI = (0x180), // Received data full + ELC_EVENT_SCI2_TXI = (0x181), // Transmit data empty + ELC_EVENT_SCI2_TEI = (0x182), // Transmit end + ELC_EVENT_SCI2_ERI = (0x183), // Receive error + ELC_EVENT_SCI2_AM = (0x184), // Address match event + ELC_EVENT_SCI3_RXI = (0x186), // Received data full + ELC_EVENT_SCI3_TXI = (0x187), // Transmit data empty + ELC_EVENT_SCI3_TEI = (0x188), // Transmit end + ELC_EVENT_SCI3_ERI = (0x189), // Receive error + ELC_EVENT_SCI3_AM = (0x18A), // Address match event + ELC_EVENT_SCI4_RXI = (0x18C), // Received data full + ELC_EVENT_SCI4_TXI = (0x18D), // Transmit data empty + ELC_EVENT_SCI4_TEI = (0x18E), // Transmit end + ELC_EVENT_SCI4_ERI = (0x18F), // Receive error + ELC_EVENT_SCI4_AM = (0x190), // Address match event + ELC_EVENT_SCI8_RXI = (0x1A4), // Received data full + ELC_EVENT_SCI8_TXI = (0x1A5), // Transmit data empty + ELC_EVENT_SCI8_TEI = (0x1A6), // Transmit end + ELC_EVENT_SCI8_ERI = (0x1A7), // Receive error + ELC_EVENT_SCI8_AM = (0x1A8), // Address match event + ELC_EVENT_SCI9_RXI = (0x1AA), // Received data full + ELC_EVENT_SCI9_TXI = (0x1AB), // Transmit data empty + ELC_EVENT_SCI9_TEI = (0x1AC), // Transmit end + ELC_EVENT_SCI9_ERI = (0x1AD), // Receive error + ELC_EVENT_SCI9_AM = (0x1AE), // Address match event + ELC_EVENT_SPI0_RXI = (0x1BC), // Receive buffer full + ELC_EVENT_SPI0_TXI = (0x1BD), // Transmit buffer empty + ELC_EVENT_SPI0_IDLE = (0x1BE), // Idle + ELC_EVENT_SPI0_ERI = (0x1BF), // Error + ELC_EVENT_SPI0_TEI = (0x1C0), // Transmission complete event + ELC_EVENT_SPI1_RXI = (0x1C1), // Receive buffer full + ELC_EVENT_SPI1_TXI = (0x1C2), // Transmit buffer empty + ELC_EVENT_SPI1_IDLE = (0x1C3), // Idle + ELC_EVENT_SPI1_ERI = (0x1C4), // Error + ELC_EVENT_SPI1_TEI = (0x1C5), // Transmission complete event + ELC_EVENT_SCE_PROC_BUSY = (0x1E1), + ELC_EVENT_SCE_ROMOK = (0x1E2), + ELC_EVENT_SCE_LONG_PLG = (0x1E3), + ELC_EVENT_SCE_TEST_BUSY = (0x1E4), + ELC_EVENT_SCE_WRRDY_0 = (0x1E5), + ELC_EVENT_SCE_WRRDY_1 = (0x1E6), + ELC_EVENT_SCE_WRRDY_4 = (0x1E7), + ELC_EVENT_SCE_RDRDY_0 = (0x1E8), + ELC_EVENT_SCE_RDRDY_1 = (0x1E9), + ELC_EVENT_SCE_INTEGRATE_WRRDY = (0x1EA), + ELC_EVENT_SCE_INTEGRATE_RDRDY = (0x1EB), +} elc_event_t; + +/** @} (end addtogroup BSP_MCU_RA6T1) */ + +#endif diff --git a/ra/fsp/src/bsp/mcu/ra6t1/bsp_feature.h b/ra/fsp/src/bsp/mcu/ra6t1/bsp_feature.h new file mode 100644 index 000000000..856e91c2f --- /dev/null +++ b/ra/fsp/src/bsp/mcu/ra6t1/bsp_feature.h @@ -0,0 +1,286 @@ +/*********************************************************************************************************************** + * Copyright [2020] Renesas Electronics Corporation and/or its affiliates. All Rights Reserved. + * + * This software and documentation are supplied by Renesas Electronics America Inc. and may only be used with products + * of Renesas Electronics Corp. and its affiliates ("Renesas"). No other uses are authorized. Renesas products are + * sold pursuant to Renesas terms and conditions of sale. Purchasers are solely responsible for the selection and use + * of Renesas products and Renesas assumes no liability. No license, express or implied, to any intellectual property + * right is granted by Renesas. This software is protected under all applicable laws, including copyright laws. Renesas + * reserves the right to change or discontinue this software and/or this documentation. THE SOFTWARE AND DOCUMENTATION + * IS DELIVERED TO YOU "AS IS," AND RENESAS MAKES NO REPRESENTATIONS OR WARRANTIES, AND TO THE FULLEST EXTENT + * PERMISSIBLE UNDER APPLICABLE LAW, DISCLAIMS ALL WARRANTIES, WHETHER EXPLICITLY OR IMPLICITLY, INCLUDING WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NONINFRINGEMENT, WITH RESPECT TO THE SOFTWARE OR + * DOCUMENTATION. RENESAS SHALL HAVE NO LIABILITY ARISING OUT OF ANY SECURITY VULNERABILITY OR BREACH. TO THE MAXIMUM + * EXTENT PERMITTED BY LAW, IN NO EVENT WILL RENESAS BE LIABLE TO YOU IN CONNECTION WITH THE SOFTWARE OR DOCUMENTATION + * (OR ANY PERSON OR ENTITY CLAIMING RIGHTS DERIVED FROM YOU) FOR ANY LOSS, DAMAGES, OR CLAIMS WHATSOEVER, INCLUDING, + * WITHOUT LIMITATION, ANY DIRECT, CONSEQUENTIAL, SPECIAL, INDIRECT, PUNITIVE, OR INCIDENTAL DAMAGES; ANY LOST PROFITS, + * OTHER ECONOMIC DAMAGE, PROPERTY DAMAGE, OR PERSONAL INJURY; AND EVEN IF RENESAS HAS BEEN ADVISED OF THE POSSIBILITY + * OF SUCH LOSS, DAMAGES, CLAIMS OR COSTS. + **********************************************************************************************************************/ + +#ifndef BSP_FEATURE_H +#define BSP_FEATURE_H + +/*********************************************************************************************************************** + * Includes , "Project Includes" + **********************************************************************************************************************/ + +/*********************************************************************************************************************** + * Macro definitions + **********************************************************************************************************************/ + +/** The main oscillator drive value is based upon the oscillator frequency selected in the configuration */ +#if (BSP_CFG_XTAL_HZ > (19999999)) + #define CGC_MAINCLOCK_DRIVE (0x00U) +#elif (BSP_CFG_XTAL_HZ > (15999999)) && (BSP_CFG_XTAL_HZ < (20000000)) + #define CGC_MAINCLOCK_DRIVE (0x01U) +#elif (BSP_CFG_XTAL_HZ > (7999999)) && (BSP_CFG_XTAL_HZ < (16000000)) + #define CGC_MAINCLOCK_DRIVE (0x02U) +#else + #define CGC_MAINCLOCK_DRIVE (0x03U) +#endif + +/*********************************************************************************************************************** + * Typedef definitions + **********************************************************************************************************************/ + +/*********************************************************************************************************************** + * Exported global variables (to be accessed by other files) + **********************************************************************************************************************/ + +/*********************************************************************************************************************** + * Private global variables and functions + **********************************************************************************************************************/ + +#define BSP_FEATURE_ACMPHS_MIN_WAIT_TIME_US (1U) // This comes from the Electrical Characteristics in the hardware manual. Rounding up to nearest microsecond. +#define BSP_FEATURE_ACMPHS_VREF (ACMPHS_REFERENCE_IVREF2) + +#define BSP_FEATURE_ACMPLP_HAS_COMPSEL_REGISTERS (0) // Feature not available on this MCU +#define BSP_FEATURE_ACMPLP_MIN_WAIT_TIME_US (0) // Feature not available on this MCU + +#define BSP_FEATURE_ADC_ADDITION_SUPPORTED (1U) +#define BSP_FEATURE_ADC_CALIBRATION_REG_AVAILABLE (0U) +#define BSP_FEATURE_ADC_CLOCK_SOURCE (FSP_PRIV_CLOCK_PCLKC) +#define BSP_FEATURE_ADC_GROUP_B_SENSORS_ALLOWED (1U) +#define BSP_FEATURE_ADC_HAS_ADCER_ADPRC (1U) +#define BSP_FEATURE_ADC_HAS_ADCER_ADRFMT (1U) +#define BSP_FEATURE_ADC_HAS_PGA (1U) +#define BSP_FEATURE_ADC_HAS_SAMPLE_HOLD_REG (1U) +#define BSP_FEATURE_ADC_HAS_VREFAMPCNT (0U) +#define BSP_FEATURE_ADC_MAX_RESOLUTION_BITS (12U) +#define BSP_FEATURE_ADC_SENSORS_EXCLUSIVE (0U) +#define BSP_FEATURE_ADC_SENSOR_MIN_SAMPLING_TIME (4150U) +#define BSP_FEATURE_ADC_TSN_CALIBRATION32_AVAILABLE (1U) +#define BSP_FEATURE_ADC_TSN_CALIBRATION32_MASK (0x00000FFFU) +#define BSP_FEATURE_ADC_TSN_CALIBRATION_AVAILABLE (1U) +#define BSP_FEATURE_ADC_TSN_CONTROL_AVAILABLE (1U) +#define BSP_FEATURE_ADC_TSN_SLOPE (4000) +#define BSP_FEATURE_ADC_UNIT_0_CHANNELS (0x1700EF) // 0 to 3, 5 to 7, 16 to 18, and 20 in unit 0 and 0 to 2, 5 to 7, 16 to 17 in unit 1 +#define BSP_FEATURE_ADC_UNIT_1_CHANNELS (0x300E7) +#define BSP_FEATURE_ADC_VALID_UNIT_MASK (3U) + +#define BSP_FEATURE_AGT_VALID_CHANNEL_MASK (0x03) + +#define BSP_FEATURE_BSP_FLASH_CACHE (1) +#define BSP_FEATURE_BSP_FLASH_CACHE_DISABLE_OPM (1U) +#define BSP_FEATURE_BSP_FLASH_PREFETCH_BUFFER (0) +#define BSP_FEATURE_BSP_HAS_OCTASPI_CLOCK (0U) +#define BSP_FEATURE_BSP_HAS_SCE5 (0) // Feature not available on this MCU +#define BSP_FEATURE_BSP_HAS_SCE_ON_RA2 (0) // Feature not available on this MCU +#define BSP_FEATURE_BSP_HAS_SECURITY_MPU (1U) +#define BSP_FEATURE_BSP_HAS_SP_MON (1U) +#define BSP_FEATURE_BSP_HAS_USBCKDIVCR (0U) +#define BSP_FEATURE_BSP_HAS_USB_CLOCK_DIV (1U) +#define BSP_FEATURE_BSP_HAS_USB_CLOCK_REQ (0U) // On the RA6M4 there is a request bit that must be set before changing USB clock settings. +#define BSP_FEATURE_BSP_HAS_USB_CLOCK_SEL (0U) +#define BSP_FEATURE_BSP_HAS_USB_CLOCK_SEL_ALT (0U) +#define BSP_FEATURE_BSP_MCU_INFO_POINTER_LOCATION (0x407FB19C) +#define BSP_FEATURE_BSP_MPU_REGION0_MASK (0x00FFFFFFU) +#define BSP_FEATURE_BSP_MSTP_GPT_MSTPD5_MAX_CH (7U) // Largest channel number associated with lower MSTP bit for GPT on this MCU. +#define BSP_FEATURE_BSP_MSTP_HAS_MSTPCRE (0U) +#define BSP_FEATURE_BSP_OFS1_HOCOFRQ_MASK (0xFFFFF9FFU) +#define BSP_FEATURE_BSP_OFS1_HOCOFRQ_OFFSET (9U) +#define BSP_FEATURE_BSP_OSIS_PADDING (0U) +#define BSP_FEATURE_BSP_POWER_CHANGE_MSTP_REQUIRED (1U) +#define BSP_FEATURE_BSP_RESET_TRNG (0U) +#define BSP_FEATURE_BSP_SYS_CLOCK_FREQ_NO_RAM_WAITS (60000000U) // The maximum frequency allowed without having RAM wait state enabled in SRAMWTSC. +#define BSP_FEATURE_BSP_SYS_CLOCK_FREQ_ONE_ROM_WAITS (40000000U) // The maximum frequency allowed without having one ROM wait cycle. +#define BSP_FEATURE_BSP_SYS_CLOCK_FREQ_THREE_ROM_WAITS (0U) // The maximum frequency allowed without having three ROM wait cycles (Set to zero if this is not an option). +#define BSP_FEATURE_BSP_SYS_CLOCK_FREQ_TWO_ROM_WAITS (800000000U) // The maximum frequency allowed without having two ROM wait cycles. +#define BSP_FEATURE_BSP_UNIQUE_ID_OFFSET (0x14) +#define BSP_FEATURE_BSP_UNIQUE_ID_POINTER ((*(uint32_t *) BSP_FEATURE_BSP_MCU_INFO_POINTER_LOCATION) + \ + BSP_FEATURE_BSP_UNIQUE_ID_OFFSET) +#define BSP_FEATURE_BSP_VBATT_HAS_VBTCR1_BPWSWSTP (0U) + +#define BSP_FEATURE_CAN_CHECK_PCLKB_RATIO (0U) +#define BSP_FEATURE_CAN_CLOCK (0U) +#define BSP_FEATURE_CAN_MCLOCK_ONLY (0U) +#define BSP_FEATURE_CAN_NUM_CHANNELS (2U) + +#define BSP_FEATURE_CGC_HAS_BCLK (1U) +#define BSP_FEATURE_CGC_HAS_FCLK (1U) +#define BSP_FEATURE_CGC_HAS_FLDWAITR (0U) +#define BSP_FEATURE_CGC_HAS_FLWT (1U) +#define BSP_FEATURE_CGC_HAS_HOCOWTCR (1U) +#define BSP_FEATURE_CGC_HAS_MEMWAIT (0U) +#define BSP_FEATURE_CGC_HAS_PCLKA (1U) +#define BSP_FEATURE_CGC_HAS_PCLKB (1U) +#define BSP_FEATURE_CGC_HAS_PCLKC (1U) +#define BSP_FEATURE_CGC_HAS_PCLKD (1U) +#define BSP_FEATURE_CGC_HAS_PLL (1U) +#define BSP_FEATURE_CGC_HAS_PLL2 (0U) // On the RA6M4 there is another PLL that can be used as a clock source for USB and OCTASPI. +#define BSP_FEATURE_CGC_HAS_SRAMPRCR2 (0U) // On the RA6M4 there is another register to enable write access for SRAMWTSC. +#define BSP_FEATURE_CGC_HAS_SRAMWTSC (1U) +#define BSP_FEATURE_CGC_HOCOSF_BEFORE_OPCCR (0U) +#define BSP_FEATURE_CGC_HOCOWTCR_64MHZ_ONLY (0U) +#define BSP_FEATURE_CGC_ICLK_DIV_RESET (BSP_CLOCKS_SYS_CLOCK_DIV_4) +#define BSP_FEATURE_CGC_LOCO_STABILIZATION_MAX_US (61U) +#define BSP_FEATURE_CGC_LOW_SPEED_MAX_FREQ_HZ (1000000U) ///< This MCU does have Low Speed Mode, up to 1MHz +#define BSP_FEATURE_CGC_LOW_VOLTAGE_MAX_FREQ_HZ (0U) ///< This MCU does not have Low Voltage Mode +#define BSP_FEATURE_CGC_MIDDLE_SPEED_MAX_FREQ_HZ (0U) ///< This MCU does not have Middle Speed Mode +#define BSP_FEATURE_CGC_MOCO_STABILIZATION_MAX_US (15U) +#define BSP_FEATURE_CGC_MODRV_MASK (0x30U) +#define BSP_FEATURE_CGC_MODRV_SHIFT (0x4U) +#define BSP_FEATURE_CGC_PLLCCR_TYPE (1U) +#define BSP_FEATURE_CGC_PLLCCR_WAIT_US (0U) // No wait between setting PLLCCR and clearing PLLSTP +#define BSP_FEATURE_CGC_PLLCCR_MAX_HZ (240000000U) +#define BSP_FEATURE_CGC_SCKDIVCR_BCLK_MATCHES_PCLKB (1U) +#define BSP_FEATURE_CGC_SODRV_MASK (0x02U) +#define BSP_FEATURE_CGC_SODRV_SHIFT (0x1U) +#define BSP_FEATURE_CGC_STARTUP_OPCCR_MODE (0) + +#define BSP_FEATURE_CRYPTO_HAS_AES (1) +#define BSP_FEATURE_CRYPTO_HAS_AES_WRAPPED (1) +#define BSP_FEATURE_CRYPTO_HAS_ECC (1) +#define BSP_FEATURE_CRYPTO_HAS_ECC_WRAPPED (1) +#define BSP_FEATURE_CRYPTO_HAS_HASH (1) +#define BSP_FEATURE_CRYPTO_HAS_RSA (1) +#define BSP_FEATURE_CRYPTO_HAS_RSA_WRAPPED (1) + +#define BSP_FEATURE_CTSU_CTSUCHAC_REGISTER_COUNT (0) // Feature not available on this MCU +#define BSP_FEATURE_CTSU_CTSUCHTRC_REGISTER_COUNT (0) // Feature not available on this MCU +#define BSP_FEATURE_CTSU_HAS_TXVSEL (0) // Feature not available on this MCU +#define BSP_FEATURE_CTSU_VERSION (0) // Feature not available on this MCU + +#define BSP_FEATURE_DAC8_HAS_CHARGEPUMP (0) // Feature not available on this MCU +#define BSP_FEATURE_DAC8_HAS_DA_AD_SYNCHRONIZE (0) // Feature not available on this MCU +#define BSP_FEATURE_DAC8_HAS_REALTIME_MODE (0) // Feature not available on this MCU +#define BSP_FEATURE_DAC8_MAX_CHANNELS (0) // Feature not available on this MCU + +#define BSP_FEATURE_DAC_HAS_CHARGEPUMP (0U) +#define BSP_FEATURE_DAC_HAS_DAVREFCR (0U) +#define BSP_FEATURE_DAC_HAS_OUTPUT_AMPLIFIER (1U) +#define BSP_FEATURE_DAC_MAX_CHANNELS (2U) + +#define BSP_FEATURE_DMAC_MAX_CHANNEL (8U) + +#define BSP_FEATURE_DWT_CYCCNT (1U) // RA6M1 has Data Watchpoint Cycle Count Register + +#define BSP_FEATURE_ELC_PERIPHERAL_MASK (0x0003FFFFU) // Positions of event link set registers (ELSRs) available on this MCU + +#define BSP_FEATURE_ETHER_FIFO_DEPTH (0) // Feature not available on this MCU +#define BSP_FEATURE_ETHER_MAX_CHANNELS (0) // Feature not available on this MCU + +#define BSP_FEATURE_FLASH_DATA_FLASH_START (0x40100000U) +#define BSP_FEATURE_FLASH_HP_CF_REGION0_BLOCK_SIZE (0x2000U) +#define BSP_FEATURE_FLASH_HP_CF_REGION0_SIZE (0x10000U) +#define BSP_FEATURE_FLASH_HP_CF_REGION1_BLOCK_SIZE (0x8000U) +#define BSP_FEATURE_FLASH_HP_CF_WRITE_SIZE (128U) +#define BSP_FEATURE_FLASH_HP_DF_BLOCK_SIZE (64U) +#define BSP_FEATURE_FLASH_HP_DF_WRITE_SIZE (4U) +#define BSP_FEATURE_FLASH_HP_HAS_FMEPROT (0) // Feature not available on this MCU +#define BSP_FEATURE_FLASH_HP_VERSION (40U) +#define BSP_FEATURE_FLASH_LP_AWS_FAW_MASK (0) // Feature not available on this MCU +#define BSP_FEATURE_FLASH_LP_AWS_FAW_SHIFT (0) // Feature not available on this MCU +#define BSP_FEATURE_FLASH_LP_CF_BLOCK_SIZE (0) // Feature not available on this MCU +#define BSP_FEATURE_FLASH_LP_CF_WRITE_SIZE (0) // Feature not available on this MCU +#define BSP_FEATURE_FLASH_LP_DF_BLOCK_SIZE (0) // Feature not available on this MCU +#define BSP_FEATURE_FLASH_LP_DF_WRITE_SIZE (0) // Feature not available on this MCU +#define BSP_FEATURE_FLASH_LP_FLASH_CLOCK_SRC (0) // Feature not available on this MCU +#define BSP_FEATURE_FLASH_LP_VERSION (0) // Feature not available on this MCU +#define BSP_FEATURE_FLASH_SUPPORTS_ACCESS_WINDOW (1) +#define BSP_FEATURE_FLASH_SUPPORTS_ID_CODE (1) + +#define BSP_FEATURE_GPTEH_CHANNEL_MASK (0xF) + +#define BSP_FEATURE_GPTE_CHANNEL_MASK (0xF0) + +#define BSP_FEATURE_GPT_32BIT_CHANNEL_MASK (0x1FFF) +#define BSP_FEATURE_GPT_VALID_CHANNEL_MASK (0x1FFF) + +#define BSP_FEATURE_ICU_HAS_WUPEN1 (0U) +#define BSP_FEATURE_ICU_IRQ_CHANNELS_MASK (0x3FFFU) +#define BSP_FEATURE_ICU_WUPEN_MASK (0xF04F3FFFU) + +#define BSP_FEATURE_IIC_FAST_MODE_PLUS (1U << 0U) +#define BSP_FEATURE_IIC_VALID_CHANNEL_MASK (0x07) + +#define BSP_FEATURE_IOPORT_ELC_PORTS (4) +#define BSP_FEATURE_IOPORT_HAS_ETHERNET (0U) + +#define BSP_FEATURE_LPM_CHANGE_MSTP_ARRAY {{1, 31}, {2, 5}} +#define BSP_FEATURE_LPM_CHANGE_MSTP_REQUIRED (1U) +#define BSP_FEATURE_LPM_DPSIEGR_MASK (0x00131FF3U) +#define BSP_FEATURE_LPM_DPSIER_MASK (0x04131FF3U) +#define BSP_FEATURE_LPM_HAS_DEEP_STANDBY (1U) +#define BSP_FEATURE_LPM_HAS_SBYCR_OPE (1U) +#define BSP_FEATURE_LPM_HAS_SNZEDCR1 (0U) +#define BSP_FEATURE_LPM_HAS_SNZREQCR1 (0U) +#define BSP_FEATURE_LPM_HAS_STCONR (1U) +#define BSP_FEATURE_LPM_SBYCR_WRITE1_B14 (0) +#define BSP_FEATURE_LPM_SNZEDCR_MASK (0x000000FFU) +#define BSP_FEATURE_LPM_SNZREQCR_MASK (0x70423FFFU) + +#define BSP_FEATURE_LVD_HAS_DIGITAL_FILTER (1U) +#define BSP_FEATURE_LVD_HAS_LVDLVLR (1U) +#define BSP_FEATURE_LVD_MONITOR_1_HI_THRESHOLD (LVD_THRESHOLD_MONITOR_1_LEVEL_2_99V) // 2.99V +#define BSP_FEATURE_LVD_MONITOR_1_LOW_THRESHOLD (LVD_THRESHOLD_MONITOR_1_LEVEL_2_85V) // 2.85V +#define BSP_FEATURE_LVD_MONITOR_2_HI_THRESHOLD (LVD_THRESHOLD_MONITOR_2_LEVEL_2_99V) // 2.99V +#define BSP_FEATURE_LVD_MONITOR_2_LOW_THRESHOLD (LVD_THRESHOLD_MONITOR_2_LEVEL_2_85V) // 2.85V +#define BSP_FEATURE_LVD_STABILIZATION_TIME_US (10U) // Time in microseconds required for LVD to stabilize + +#define BSP_FEATURE_OPAMP_BASE_ADDRESS (0U) +#define BSP_FEATURE_OPAMP_HAS_MIDDLE_SPEED (0) // Feature not available on this MCU +#define BSP_FEATURE_OPAMP_HAS_SWITCHES (0U) +#define BSP_FEATURE_OPAMP_HAS_THIRD_CHANNEL (0U) +#define BSP_FEATURE_OPAMP_MIN_WAIT_TIME_HS_US (0) // Feature not available on this MCU +#define BSP_FEATURE_OPAMP_MIN_WAIT_TIME_LP_US (0) // Feature not available on this MCU +#define BSP_FEATURE_OPAMP_MIN_WAIT_TIME_MS_US (0) // Feature not available on this MCU +#define BSP_FEATURE_OPAMP_TRIM_CAPABLE (0U) +#define BSP_FEATURE_OPAMP_VARIANT_CHANNEL_MASK (0U) + +#define BSP_FEATURE_OSPI_DEVICE_0_START_ADDRESS (0x0U) +#define BSP_FEATURE_OSPI_DEVICE_1_START_ADDRESS (0x0U) + +#define BSP_FEATURE_POEG_CHANNEL_MASK (0xFU) + +#define BSP_FEATURE_SCI_ADDRESS_MATCH_CHANNELS (BSP_FEATURE_SCI_CHANNELS) +#define BSP_FEATURE_SCI_CHANNELS (0x31FU) +#define BSP_FEATURE_SCI_CLOCK (FSP_PRIV_CLOCK_PCLKA) +#define BSP_FEATURE_SCI_UART_FIFO_CHANNELS (0x31FU) +#define BSP_FEATURE_SCI_UART_FIFO_DEPTH (16U) + +#define BSP_FEATURE_SDHI_HAS_CARD_DETECTION (0) // Feature not available on this MCU +#define BSP_FEATURE_SDHI_SUPPORTS_8_BIT_MMC (0) // Feature not available on this MCU +#define BSP_FEATURE_SDHI_VALID_CHANNEL_MASK (0) // Feature not available on this MCU +#define BSP_FEATURE_SDHI_CLOCK (FSP_PRIV_CLOCK_PCLKA) +#define BSP_FEATURE_SDHI_MIN_CLOCK_DIVISION_SHIFT (0) // Feature not available on this MCU + +#define BSP_FEATURE_SDRAM_START_ADDRESS (0x0U) + +#define BSP_FEATURE_SLCDC_HAS_8_TIME_SLICE (0) // Feature not available on this MCU +#define BSP_FEATURE_SLCDC_HAS_INTERNAL_VOLT_GEN (0) // Feature not available on this MCU +#define BSP_FEATURE_SLCDC_MAX_NUM_SEG (0) // Feature not available on this MCU + +#define BSP_FEATURE_SPI_CLK (FSP_PRIV_CLOCK_PCLKA) +#define BSP_FEATURE_SPI_HAS_BYTE_SWAP (1U) +#define BSP_FEATURE_SPI_HAS_SPCR3 (0U) +#define BSP_FEATURE_SPI_HAS_SSL_LEVEL_KEEP (1U) +#define BSP_FEATURE_SPI_MAX_CHANNEL (2U) + +#define BSP_FEATURE_SSI_FIFO_NUM_STAGES (0U) +#define BSP_FEATURE_SSI_VALID_CHANNEL_MASK (0U) + +#define BSP_FEATURE_TZ_HAS_TRUSTZONE (0U) + +#endif diff --git a/ra/fsp/src/bsp/mcu/ra6t1/bsp_mcu_info.h b/ra/fsp/src/bsp/mcu/ra6t1/bsp_mcu_info.h new file mode 100644 index 000000000..b9d1b58c3 --- /dev/null +++ b/ra/fsp/src/bsp/mcu/ra6t1/bsp_mcu_info.h @@ -0,0 +1,58 @@ +/*********************************************************************************************************************** + * Copyright [2020] Renesas Electronics Corporation and/or its affiliates. All Rights Reserved. + * + * This software and documentation are supplied by Renesas Electronics America Inc. and may only be used with products + * of Renesas Electronics Corp. and its affiliates ("Renesas"). No other uses are authorized. Renesas products are + * sold pursuant to Renesas terms and conditions of sale. Purchasers are solely responsible for the selection and use + * of Renesas products and Renesas assumes no liability. No license, express or implied, to any intellectual property + * right is granted by Renesas. This software is protected under all applicable laws, including copyright laws. Renesas + * reserves the right to change or discontinue this software and/or this documentation. THE SOFTWARE AND DOCUMENTATION + * IS DELIVERED TO YOU "AS IS," AND RENESAS MAKES NO REPRESENTATIONS OR WARRANTIES, AND TO THE FULLEST EXTENT + * PERMISSIBLE UNDER APPLICABLE LAW, DISCLAIMS ALL WARRANTIES, WHETHER EXPLICITLY OR IMPLICITLY, INCLUDING WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NONINFRINGEMENT, WITH RESPECT TO THE SOFTWARE OR + * DOCUMENTATION. RENESAS SHALL HAVE NO LIABILITY ARISING OUT OF ANY SECURITY VULNERABILITY OR BREACH. TO THE MAXIMUM + * EXTENT PERMITTED BY LAW, IN NO EVENT WILL RENESAS BE LIABLE TO YOU IN CONNECTION WITH THE SOFTWARE OR DOCUMENTATION + * (OR ANY PERSON OR ENTITY CLAIMING RIGHTS DERIVED FROM YOU) FOR ANY LOSS, DAMAGES, OR CLAIMS WHATSOEVER, INCLUDING, + * WITHOUT LIMITATION, ANY DIRECT, CONSEQUENTIAL, SPECIAL, INDIRECT, PUNITIVE, OR INCIDENTAL DAMAGES; ANY LOST PROFITS, + * OTHER ECONOMIC DAMAGE, PROPERTY DAMAGE, OR PERSONAL INJURY; AND EVEN IF RENESAS HAS BEEN ADVISED OF THE POSSIBILITY + * OF SUCH LOSS, DAMAGES, CLAIMS OR COSTS. + **********************************************************************************************************************/ + +/*******************************************************************************************************************//** + * @ingroup BSP_MCU + * @defgroup BSP_MCU_RA6T1 RA6T1 + * @includedoc config_bsp_ra6t1_fsp.html + * @{ + **********************************************************************************************************************/ + +/** @} (end defgroup BSP_MCU_RA6T1) */ + +#ifndef BSP_MCU_INFO_H +#define BSP_MCU_INFO_H + +/*********************************************************************************************************************** + * Includes , "Project Includes" + **********************************************************************************************************************/ + +/* BSP MCU Specific Includes. */ +#include "bsp_elc.h" +#include "bsp_feature.h" + +/*********************************************************************************************************************** + * Macro definitions + **********************************************************************************************************************/ + +/*********************************************************************************************************************** + * Typedef definitions + **********************************************************************************************************************/ +typedef elc_event_t bsp_interrupt_event_t; + +/*********************************************************************************************************************** + * Exported global variables + **********************************************************************************************************************/ + +/*********************************************************************************************************************** + * Exported global functions (to be accessed by other files) + **********************************************************************************************************************/ + +#endif diff --git a/ra/fsp/src/r_adc/r_adc.c b/ra/fsp/src/r_adc/r_adc.c index eab37b810..479bc1c7d 100644 --- a/ra/fsp/src/r_adc/r_adc.c +++ b/ra/fsp/src/r_adc/r_adc.c @@ -93,7 +93,7 @@ typedef enum e_adc_elc_trigger } adc_elc_trigger_t; #if defined(__ARMCC_VERSION) || defined(__ICCARM__) -typedef void (BSP_CMSE_NONSECURE_CALL * volatile adc_prv_ns_callback)(adc_callback_args_t * p_args); +typedef void (BSP_CMSE_NONSECURE_CALL * adc_prv_ns_callback)(adc_callback_args_t * p_args); #elif defined(__GNUC__) typedef BSP_CMSE_NONSECURE_CALL void (*volatile adc_prv_ns_callback)(adc_callback_args_t * p_args); #endif @@ -280,7 +280,7 @@ fsp_err_t R_ADC_Open (adc_ctrl_t * p_ctrl, adc_cfg_t const * const p_cfg) /*******************************************************************************************************************//** * Configures the ADC scan parameters. Channel specific settings are set in this function. Pass a pointer to - * @ref adc_channel_cfg_t to p_extend. + * @ref adc_channel_cfg_t to p_channel_cfg. * * @note This starts group B scans if adc_channel_cfg_t::priority_group_a is set to ADC_GROUP_A_GROUP_B_CONTINUOUS_SCAN. * @@ -288,26 +288,26 @@ fsp_err_t R_ADC_Open (adc_ctrl_t * p_ctrl, adc_cfg_t const * const p_cfg) * @retval FSP_ERR_ASSERTION An input argument is invalid. * @retval FSP_ERR_NOT_OPEN Unit is not open. **********************************************************************************************************************/ -fsp_err_t R_ADC_ScanCfg (adc_ctrl_t * p_ctrl, void const * const p_extend) +fsp_err_t R_ADC_ScanCfg (adc_ctrl_t * p_ctrl, void const * const p_channel_cfg) { - adc_channel_cfg_t const * p_channel_cfg = (adc_channel_cfg_t const *) p_extend; - adc_instance_ctrl_t * p_instance_ctrl = (adc_instance_ctrl_t *) p_ctrl; - fsp_err_t err = FSP_SUCCESS; + adc_channel_cfg_t const * p_adc_channel_cfg = (adc_channel_cfg_t const *) p_channel_cfg; + adc_instance_ctrl_t * p_instance_ctrl = (adc_instance_ctrl_t *) p_ctrl; + fsp_err_t err = FSP_SUCCESS; #if ADC_CFG_PARAM_CHECKING_ENABLE FSP_ASSERT(NULL != p_instance_ctrl); - FSP_ASSERT(NULL != p_channel_cfg); + FSP_ASSERT(NULL != p_adc_channel_cfg); FSP_ERROR_RETURN(ADC_OPEN == p_instance_ctrl->opened, FSP_ERR_NOT_OPEN); - err = r_adc_scan_cfg_check(p_instance_ctrl, p_channel_cfg); + err = r_adc_scan_cfg_check(p_instance_ctrl, p_adc_channel_cfg); FSP_ERROR_RETURN(FSP_SUCCESS == err, err); #endif /* Configure the hardware based on the configuration */ - r_adc_scan_cfg(p_instance_ctrl, p_channel_cfg); + r_adc_scan_cfg(p_instance_ctrl, p_adc_channel_cfg); /* Save the scan mask locally; this is required for the infoGet function. */ - p_instance_ctrl->scan_mask = p_channel_cfg->scan_mask; + p_instance_ctrl->scan_mask = p_adc_channel_cfg->scan_mask; /* Return the error code */ return err; diff --git a/ra/fsp/src/r_agt/r_agt.c b/ra/fsp/src/r_agt/r_agt.c index a7862559c..1de29d479 100644 --- a/ra/fsp/src/r_agt/r_agt.c +++ b/ra/fsp/src/r_agt/r_agt.c @@ -56,7 +56,7 @@ * Typedef definitions **********************************************************************************************************************/ #if defined(__ARMCC_VERSION) || defined(__ICCARM__) -typedef void (BSP_CMSE_NONSECURE_CALL * volatile agt_prv_ns_callback)(timer_callback_args_t * p_args); +typedef void (BSP_CMSE_NONSECURE_CALL * agt_prv_ns_callback)(timer_callback_args_t * p_args); #elif defined(__GNUC__) typedef BSP_CMSE_NONSECURE_CALL void (*volatile agt_prv_ns_callback)(timer_callback_args_t * p_args); #endif diff --git a/ra/fsp/src/r_cac/r_cac.c b/ra/fsp/src/r_cac/r_cac.c index 29affd41b..5b3a12238 100644 --- a/ra/fsp/src/r_cac/r_cac.c +++ b/ra/fsp/src/r_cac/r_cac.c @@ -80,7 +80,7 @@ * Typedef definitions **********************************************************************************************************************/ #if defined(__ARMCC_VERSION) || defined(__ICCARM__) -typedef void (BSP_CMSE_NONSECURE_CALL * volatile cac_prv_ns_callback)(cac_callback_args_t * p_args); +typedef void (BSP_CMSE_NONSECURE_CALL * cac_prv_ns_callback)(cac_callback_args_t * p_args); #elif defined(__GNUC__) typedef BSP_CMSE_NONSECURE_CALL void (*volatile cac_prv_ns_callback)(cac_callback_args_t * p_args); #endif diff --git a/ra/fsp/src/r_can/r_can.c b/ra/fsp/src/r_can/r_can.c index ca4559aa7..06d791cff 100644 --- a/ra/fsp/src/r_can/r_can.c +++ b/ra/fsp/src/r_can/r_can.c @@ -103,7 +103,7 @@ typedef union } can_error_interrrupt_status_t; #if defined(__ARMCC_VERSION) || defined(__ICCARM__) -typedef void (BSP_CMSE_NONSECURE_CALL * volatile can_prv_ns_callback)(can_callback_args_t * p_args); +typedef void (BSP_CMSE_NONSECURE_CALL * can_prv_ns_callback)(can_callback_args_t * p_args); #elif defined(__GNUC__) typedef BSP_CMSE_NONSECURE_CALL void (*volatile can_prv_ns_callback)(can_callback_args_t * p_args); #endif diff --git a/ra/fsp/src/r_cgc/r_cgc.c b/ra/fsp/src/r_cgc/r_cgc.c index d409f080f..e10a1af3d 100644 --- a/ra/fsp/src/r_cgc/r_cgc.c +++ b/ra/fsp/src/r_cgc/r_cgc.c @@ -121,7 +121,7 @@ typedef enum e_cgc_prv_change } cgc_prv_change_t; #if defined(__ARMCC_VERSION) || defined(__ICCARM__) -typedef void (BSP_CMSE_NONSECURE_CALL * volatile cgc_prv_ns_callback)(cgc_callback_args_t * p_args); +typedef void (BSP_CMSE_NONSECURE_CALL * cgc_prv_ns_callback)(cgc_callback_args_t * p_args); #elif defined(__GNUC__) typedef BSP_CMSE_NONSECURE_CALL void (*volatile cgc_prv_ns_callback)(cgc_callback_args_t * p_args); #endif @@ -237,7 +237,6 @@ static const uint32_t g_sckdivcr_upper_bits = * Global Variables **********************************************************************************************************************/ -/*LDRA_INSPECTED 27 D This structure must be accessible in user code. It cannot be static. */ const cgc_api_t g_cgc_on_cgc = { .open = R_CGC_Open, @@ -333,7 +332,6 @@ fsp_err_t R_CGC_Open (cgc_ctrl_t * const p_ctrl, cgc_cfg_t const * const p_cfg) * * @retval FSP_SUCCESS Clock configuration applied successfully. * @retval FSP_ERR_ASSERTION Invalid input argument. - * @retval FSP_ERR_UNSUPPORTED PLL/PLL2 is not available on this MCU. * @retval FSP_ERR_NOT_OPEN Module is not open. * @retval FSP_ERR_IN_USE Attempt to stop the current system clock or the PLL source clock. * @retval FSP_ERR_CLOCK_ACTIVE PLL configuration cannot be changed while PLL is running. @@ -370,14 +368,6 @@ fsp_err_t R_CGC_ClocksCfg (cgc_ctrl_t * const p_ctrl, cgc_clocks_cfg_t const * c options[CGC_CLOCK_MAIN_OSC] = p_clock_cfg->mainosc_state; options[CGC_CLOCK_SUBCLOCK] = CGC_CLOCK_CHANGE_NONE; #if CGC_CFG_PARAM_CHECKING_ENABLE - #if !BSP_PRV_PLL_SUPPORTED - FSP_ERROR_RETURN(CGC_CLOCK_CHANGE_START != p_clock_cfg->pll_state, FSP_ERR_UNSUPPORTED); - #endif - - #if !BSP_PRV_PLL2_SUPPORTED - FSP_ERROR_RETURN(CGC_CLOCK_CHANGE_START != p_clock_cfg->pll2_state, FSP_ERR_UNSUPPORTED); - #endif - #if BSP_CFG_USE_LOW_VOLTAGE_MODE /* HOCO must be running in low voltage mode. */ diff --git a/ra/fsp/src/r_ctsu/r_ctsu.c b/ra/fsp/src/r_ctsu/r_ctsu.c index 433deed0d..961783c60 100644 --- a/ra/fsp/src/r_ctsu/r_ctsu.c +++ b/ra/fsp/src/r_ctsu/r_ctsu.c @@ -163,15 +163,15 @@ typedef struct st_ctsu_correction_calc typedef struct st_ctsu_correction_multi { - uint16_t pri[CTSU_CFG_NUM_SUMULTI]; - uint16_t snd[CTSU_CFG_NUM_SUMULTI]; - uint32_t offset[CTSU_CFG_NUM_SUMULTI]; - int32_t offset_error[CTSU_CFG_NUM_SUMULTI]; - ctsu_correction_calc_t calc; + uint16_t pri[CTSU_CFG_NUM_SUMULTI]; + uint16_t snd[CTSU_CFG_NUM_SUMULTI]; + uint32_t offset[CTSU_CFG_NUM_SUMULTI]; + int32_t offset_error[CTSU_CFG_NUM_SUMULTI]; + bool judge_disable; } ctsu_correction_multi_t; #if defined(__ARMCC_VERSION) || defined(__ICCARM__) -typedef void (BSP_CMSE_NONSECURE_CALL * volatile ctsu_prv_ns_callback)(ctsu_callback_args_t * p_args); +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 @@ -186,7 +186,6 @@ static fsp_err_t ctsu_transfer_configure(ctsu_instance_ctrl_t * const p_instance #endif static void ctsu_initial_offset_tuning(ctsu_instance_ctrl_t * const p_instance_ctrl); -static void ctsu_noise_reduction(ctsu_instance_ctrl_t * const p_instance_ctrl); static void ctsu_moving_average(uint16_t * p_average, uint16_t new_data, uint16_t average_num); void ctsu_write_isr(void); void ctsu_read_isr(void); @@ -235,16 +234,13 @@ static ctsu_ctsuwr_t g_ctsu_ctsuwr[(CTSU_CFG_NUM_SELF_ELEMENTS + CTSU_CFG_NUM_MU #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_work[CTSU_CFG_NUM_SELF_ELEMENTS]; -static uint16_t g_ctsu_self_data[CTSU_CFG_NUM_SELF_ELEMENTS]; +static uint16_t g_ctsu_self_data[CTSU_CFG_NUM_SELF_ELEMENTS * CTSU_CFG_NUM_SUMULTI]; #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_work[CTSU_CFG_NUM_MUTUAL_ELEMENTS]; -static uint16_t g_ctsu_mutual_snd_work[CTSU_CFG_NUM_MUTUAL_ELEMENTS]; -static uint16_t g_ctsu_mutual_pri_data[CTSU_CFG_NUM_MUTUAL_ELEMENTS]; -static uint16_t g_ctsu_mutual_snd_data[CTSU_CFG_NUM_MUTUAL_ELEMENTS]; +static uint16_t g_ctsu_mutual_pri_data[CTSU_CFG_NUM_MUTUAL_ELEMENTS * CTSU_CFG_NUM_SUMULTI]; +static uint16_t g_ctsu_mutual_snd_data[CTSU_CFG_NUM_MUTUAL_ELEMENTS * CTSU_CFG_NUM_SUMULTI]; #endif static ctsu_correction_info_t g_ctsu_correction_info; #if (CTSU_CFG_NUM_CFC != 0) @@ -342,8 +338,7 @@ fsp_err_t R_CTSU_Open (ctsu_ctrl_t * const p_ctrl, ctsu_cfg_t const * const p_cf 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_work = &g_ctsu_self_work[g_ctsu_self_element_index]; - p_instance_ctrl->p_self_data = &g_ctsu_self_data[g_ctsu_self_element_index]; + p_instance_ctrl->p_self_data = &g_ctsu_self_data[g_ctsu_self_element_index * CTSU_CFG_NUM_SUMULTI]; p_instance_ctrl->num_elements = p_cfg->num_rx; g_ctsu_self_element_index = (uint8_t) (g_ctsu_self_element_index + p_instance_ctrl->num_elements); } @@ -352,12 +347,12 @@ fsp_err_t R_CTSU_Open (ctsu_ctrl_t * const p_ctrl, ctsu_cfg_t const * const p_cf 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_work = &g_ctsu_mutual_pri_work[g_ctsu_mutual_element_index]; - p_instance_ctrl->p_mutual_snd_work = &g_ctsu_mutual_snd_work[g_ctsu_mutual_element_index]; - p_instance_ctrl->p_mutual_pri_data = &g_ctsu_mutual_pri_data[g_ctsu_mutual_element_index]; - p_instance_ctrl->p_mutual_snd_data = &g_ctsu_mutual_snd_data[g_ctsu_mutual_element_index]; - p_instance_ctrl->num_elements = (uint8_t) (p_cfg->num_rx * p_cfg->num_tx); - g_ctsu_mutual_element_index = (uint8_t) (g_ctsu_mutual_element_index + p_instance_ctrl->num_elements); + p_instance_ctrl->p_mutual_pri_data = + &g_ctsu_mutual_pri_data[g_ctsu_mutual_element_index * CTSU_CFG_NUM_SUMULTI]; + p_instance_ctrl->p_mutual_snd_data = + &g_ctsu_mutual_snd_data[g_ctsu_mutual_element_index * CTSU_CFG_NUM_SUMULTI]; + p_instance_ctrl->num_elements = (uint8_t) (p_cfg->num_rx * p_cfg->num_tx); + g_ctsu_mutual_element_index = (uint8_t) (g_ctsu_mutual_element_index + p_instance_ctrl->num_elements); } #endif p_instance_ctrl->p_tuning_count = &g_ctsu_tuning_count[g_ctsu_element_index]; @@ -850,14 +845,25 @@ fsp_err_t R_CTSU_DataGet (ctsu_ctrl_t * const p_ctrl, uint16_t * p_data) #endif if (CTSU_STATE_SCANNED == p_instance_ctrl->state) { - ctsu_correction_exec(p_instance_ctrl); - if (CTSU_TUNING_COMPLETE == p_instance_ctrl->tuning) { - /* Make Touch count value */ - ctsu_noise_reduction(p_instance_ctrl); + 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; + } } - else + + ctsu_correction_exec(p_instance_ctrl); + + if (CTSU_TUNING_INCOMPLETE == p_instance_ctrl->tuning) { if ((CTSU_MODE_SELF_MULTI_SCAN == p_instance_ctrl->p_ctsu_cfg->md) || (CTSU_MODE_MUTUAL_FULL_SCAN == p_instance_ctrl->p_ctsu_cfg->md)) @@ -874,22 +880,46 @@ fsp_err_t R_CTSU_DataGet (ctsu_ctrl_t * const p_ctrl, uint16_t * p_data) #if (CTSU_CFG_NUM_SELF_ELEMENTS != 0) if (CTSU_MODE_SELF_MULTI_SCAN == (CTSU_MODE_MUTUAL_FULL_SCAN & p_instance_ctrl->p_ctsu_cfg->md)) { - for (element_id = 0; element_id < p_instance_ctrl->num_elements; element_id++) + if (true == p_instance_ctrl->p_ctsu_cfg->judge_multifreq_disable) { - *p_data = *(p_instance_ctrl->p_self_data + element_id); - p_data++; + for (element_id = 0; element_id < (p_instance_ctrl->num_elements * CTSU_CFG_NUM_SUMULTI); element_id++) + { + *p_data = *(p_instance_ctrl->p_self_data + element_id); + p_data++; + } + } + else + { + for (element_id = 0; element_id < p_instance_ctrl->num_elements; element_id++) + { + *p_data = *(p_instance_ctrl->p_self_data + (element_id * CTSU_CFG_NUM_SUMULTI)); + p_data++; + } } } #endif #if (CTSU_CFG_NUM_MUTUAL_ELEMENTS != 0) if (CTSU_MODE_MUTUAL_FULL_SCAN == (CTSU_MODE_MUTUAL_FULL_SCAN & p_instance_ctrl->p_ctsu_cfg->md)) { - for (element_id = 0; element_id < p_instance_ctrl->num_elements; element_id++) + if (true == p_instance_ctrl->p_ctsu_cfg->judge_multifreq_disable) { - *p_data = *(p_instance_ctrl->p_mutual_pri_data + element_id); - p_data++; - *p_data = *(p_instance_ctrl->p_mutual_snd_data + element_id); - p_data++; + for (element_id = 0; element_id < (p_instance_ctrl->num_elements * CTSU_CFG_NUM_SUMULTI); element_id++) + { + *p_data = *(p_instance_ctrl->p_mutual_pri_data + element_id); + p_data++; + *p_data = *(p_instance_ctrl->p_mutual_snd_data + element_id); + p_data++; + } + } + else + { + for (element_id = 0; element_id < p_instance_ctrl->num_elements; element_id++) + { + *p_data = *(p_instance_ctrl->p_mutual_pri_data + (element_id * CTSU_CFG_NUM_SUMULTI)); + p_data++; + *p_data = *(p_instance_ctrl->p_mutual_snd_data + (element_id * CTSU_CFG_NUM_SUMULTI)); + p_data++; + } } } #endif @@ -1284,13 +1314,13 @@ void ctsu_initial_offset_tuning (ctsu_instance_ctrl_t * const p_instance_ctrl) #if (CTSU_CFG_NUM_SELF_ELEMENTS != 0) if (CTSU_MODE_SELF_MULTI_SCAN == p_instance_ctrl->p_ctsu_cfg->md) { - diff = *(p_instance_ctrl->p_self_work + element_id) - CTSU_TUNING_VALUE_SELF; + diff = *(p_instance_ctrl->p_self_data + element_id) - CTSU_TUNING_VALUE_SELF; } #endif #if (CTSU_CFG_NUM_MUTUAL_ELEMENTS != 0) if (CTSU_MODE_MUTUAL_FULL_SCAN == p_instance_ctrl->p_ctsu_cfg->md) { - diff = *(p_instance_ctrl->p_mutual_pri_work + element_id) - CTSU_TUNING_VALUE_MUTUAL; + diff = *(p_instance_ctrl->p_mutual_pri_data + element_id) - CTSU_TUNING_VALUE_MUTUAL; } #endif ctsuso = (p_instance_ctrl->p_ctsuwr[element_id].ctsuso0 & CTSU_TUNING_MAX); @@ -1357,13 +1387,13 @@ void ctsu_initial_offset_tuning (ctsu_instance_ctrl_t * const p_instance_ctrl) #if (CTSU_CFG_NUM_SELF_ELEMENTS != 0) if (CTSU_MODE_SELF_MULTI_SCAN == p_instance_ctrl->p_ctsu_cfg->md) { - corr_data[i] = p_instance_ctrl->p_self_raw[element_top + i]; + corr_data[i] = p_instance_ctrl->p_self_data[element_top + i]; } #endif #if (CTSU_CFG_NUM_MUTUAL_ELEMENTS != 0) if (CTSU_MODE_MUTUAL_FULL_SCAN == p_instance_ctrl->p_ctsu_cfg->md) { - corr_data[i] = p_instance_ctrl->p_mutual_raw[(element_top * 2) + (i * 2)]; + corr_data[i] = p_instance_ctrl->p_mutual_pri_data[(element_top + i)]; } #endif @@ -1426,81 +1456,6 @@ void ctsu_initial_offset_tuning (ctsu_instance_ctrl_t * const p_instance_ctrl) } } -/*********************************************************************************************************************** - * ctsu_noise_reduction - ***********************************************************************************************************************/ -void ctsu_noise_reduction (ctsu_instance_ctrl_t * const p_instance_ctrl) -{ - uint16_t element_id; - uint16_t * p_average_data; -#if (CTSU_CFG_NUM_MUTUAL_ELEMENTS != 0) - uint16_t * p_average_data2; -#endif - - 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; - } - - for (element_id = 0; element_id < p_instance_ctrl->num_elements; element_id++) - { -#if (CTSU_CFG_NUM_SELF_ELEMENTS != 0) - if (CTSU_MODE_SELF_MULTI_SCAN == (CTSU_MODE_MUTUAL_FULL_SCAN & p_instance_ctrl->p_ctsu_cfg->md)) - { - p_average_data = p_instance_ctrl->p_self_data + element_id; - if (1 == p_instance_ctrl->average) - { - *p_average_data = *(p_instance_ctrl->p_self_work + element_id); - } - else - { - ctsu_moving_average(p_average_data, - *(p_instance_ctrl->p_self_work + element_id), - p_instance_ctrl->average); - } - } -#endif -#if (CTSU_CFG_NUM_MUTUAL_ELEMENTS != 0) - if (CTSU_MODE_MUTUAL_FULL_SCAN == (CTSU_MODE_MUTUAL_FULL_SCAN & p_instance_ctrl->p_ctsu_cfg->md)) - { - /* Primary */ - p_average_data = p_instance_ctrl->p_mutual_pri_data + element_id; - if (1 == p_instance_ctrl->average) - { - *p_average_data = *(p_instance_ctrl->p_mutual_pri_work + element_id); - } - else - { - ctsu_moving_average(p_average_data, - *(p_instance_ctrl->p_mutual_pri_work + element_id), - p_instance_ctrl->average); - } - - /* Secondary */ - p_average_data2 = p_instance_ctrl->p_mutual_snd_data + element_id; - if (1 == p_instance_ctrl->average) - { - *p_average_data2 = *(p_instance_ctrl->p_mutual_snd_work + element_id); - } - else - { - ctsu_moving_average(p_average_data2, - *(p_instance_ctrl->p_mutual_snd_work + element_id), - p_instance_ctrl->average); - } - } -#endif - } -} - /*********************************************************************************************************************** * ctsu_moving_average ***********************************************************************************************************************/ @@ -2099,7 +2054,12 @@ void ctsu_correction_scan_start (void) if (g_ctsu_correction_info.scan_index < CTSU_CORRECTION_POINT_NUM) { + /* Dummy setting */ + R_CTSU->CTSUCHACA = 1; + R_CTSU->CTSUCHACB = 0; + /* Step3 : Measure by inputting each constant current from internal DAC to ICO. */ + R_CTSU->CTSUCRB_b.SSCNT = 0; R_CTSU->CTSUCALIB_b.CCOCLK = 0; R_CTSU->CTSUCALIB_b.CCOCALIB = 1; R_CTSU->CTSUCALIB_b.TSOC = 1; @@ -2119,10 +2079,6 @@ void ctsu_correction_scan_start (void) } R_CTSU->CTSUCRA_b.SDPSEL = 1; - - /* Dummy setting */ - R_CTSU->CTSUCHACA = 1; - R_CTSU->CTSUCHACB = 0; } else { @@ -2498,34 +2454,53 @@ void ctsu_correction_calc (uint16_t * correction_data, uint16_t raw_data, ctsu_c void ctsu_correction_exec (ctsu_instance_ctrl_t * const p_instance_ctrl) { uint16_t element_id; -#if (CTSU_CFG_NUM_SELF_ELEMENTS != 0) - uint16_t * p_self_work; -#endif -#if (CTSU_CFG_NUM_MUTUAL_ELEMENTS != 0) - uint16_t * p_pri_work; - uint16_t * p_snd_work; -#endif -#if (BSP_FEATURE_CTSU_VERSION == 1) ctsu_correction_calc_t calc; +#if (BSP_FEATURE_CTSU_VERSION == 1) + #if (CTSU_CFG_NUM_SELF_ELEMENTS != 0) + uint16_t * p_self_data; + uint16_t average_self; + #endif + #if (CTSU_CFG_NUM_MUTUAL_ELEMENTS != 0) + uint16_t * p_pri_data; + uint16_t * p_snd_data; + uint16_t average_pri; + uint16_t average_snd; + #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) - p_self_work = (p_instance_ctrl->p_self_work + element_id); if (CTSU_MODE_SELF_MULTI_SCAN == p_instance_ctrl->p_ctsu_cfg->md) { - ctsu_correction_calc(p_self_work, (p_instance_ctrl->p_self_raw + element_id)->sen, &calc); + p_self_data = (p_instance_ctrl->p_self_data + element_id); + average_self = *p_self_data; + ctsu_correction_calc(p_self_data, (p_instance_ctrl->p_self_raw + element_id)->sen, &calc); + if (1 < p_instance_ctrl->average) + { + ctsu_moving_average(&average_self, *p_self_data, p_instance_ctrl->average); + *p_self_data = average_self; + } } #endif #if (CTSU_CFG_NUM_MUTUAL_ELEMENTS != 0) - p_pri_work = (p_instance_ctrl->p_mutual_pri_work + element_id); - p_snd_work = (p_instance_ctrl->p_mutual_snd_work + element_id); + 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->p_ctsu_cfg->md) { - ctsu_correction_calc(p_pri_work, (p_instance_ctrl->p_mutual_raw + element_id)->pri_sen, &calc); - ctsu_correction_calc(p_snd_work, (p_instance_ctrl->p_mutual_raw + element_id)->snd_sen, &calc); + average_pri = *p_pri_data; + average_snd = *p_snd_data; + ctsu_correction_calc(p_pri_data, (p_instance_ctrl->p_mutual_raw + element_id)->pri_sen, &calc); + ctsu_correction_calc(p_snd_data, (p_instance_ctrl->p_mutual_raw + element_id)->snd_sen, &calc); + if (1 < p_instance_ctrl->average) + { + ctsu_moving_average(&average_pri, *p_pri_data, p_instance_ctrl->average); + ctsu_moving_average(&average_snd, *p_snd_data, p_instance_ctrl->average); + *p_pri_data = average_pri; + *p_snd_data = average_snd; + } } #endif } @@ -2535,7 +2510,16 @@ void ctsu_correction_exec (ctsu_instance_ctrl_t * const p_instance_ctrl) uint16_t i; uint32_t ctsuso; ctsu_correction_multi_t multi; + #if (CTSU_CFG_NUM_SELF_ELEMENTS != 0) + uint16_t * p_self_data; + uint16_t average_self[CTSU_CFG_NUM_SUMULTI]; + #endif #if (CTSU_CFG_NUM_MUTUAL_ELEMENTS != 0) + uint16_t * p_pri_data; + uint16_t * p_snd_data; + uint16_t average_pri[CTSU_CFG_NUM_SUMULTI]; + uint16_t average_snd[CTSU_CFG_NUM_SUMULTI]; + #if (CTSU_CFG_NUM_CFC != 0) uint8_t ts_id; uint8_t table_id; @@ -2561,16 +2545,18 @@ void ctsu_correction_exec (ctsu_instance_ctrl_t * const p_instance_ctrl) } #endif #endif - multi.calc.range = p_instance_ctrl->range; - multi.calc.md = p_instance_ctrl->p_ctsu_cfg->md; + calc.range = p_instance_ctrl->range; + calc.md = p_instance_ctrl->p_ctsu_cfg->md; + multi.judge_disable = p_instance_ctrl->p_ctsu_cfg->judge_multifreq_disable; + for (element_id = 0; element_id < p_instance_ctrl->num_elements; element_id++) { - multi.calc.snum = (p_instance_ctrl->p_ctsuwr[(element_id * CTSU_CFG_NUM_SUMULTI)].ctsuso >> 10) & CTSU_SNUM_MAX; + 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); - multi.offset[i] = (ctsuso * (uint32_t) (CTSU_CORRECTION_OFFSET_UNIT >> multi.calc.range)); + multi.offset[i] = (ctsuso * (uint32_t) (CTSU_CORRECTION_OFFSET_UNIT >> calc.range)); #if (CTSU_CFG_TEMP_CORRECTION_SUPPORT == 1) #if (CTSU_CFG_CALIB_RTRIM_SUPPORT == 1) @@ -2586,7 +2572,6 @@ void ctsu_correction_exec (ctsu_instance_ctrl_t * const p_instance_ctrl) } #if (CTSU_CFG_NUM_SELF_ELEMENTS != 0) - p_self_work = (p_instance_ctrl->p_self_work + element_id); if (CTSU_MODE_SELF_MULTI_SCAN == p_instance_ctrl->p_ctsu_cfg->md) { for (i = 0; i < CTSU_CFG_NUM_SUMULTI; i++) @@ -2600,101 +2585,152 @@ void ctsu_correction_exec (ctsu_instance_ctrl_t * const p_instance_ctrl) #endif #endif multi.snd[i] = 0; + ctsu_correction_calc(&multi.pri[i], multi.pri[i], &calc); } - if (CTSU_TUNING_COMPLETE == p_instance_ctrl->tuning) + p_self_data = (p_instance_ctrl->p_self_data + (element_id * CTSU_CFG_NUM_SUMULTI)); + + if (0 == p_instance_ctrl->average) { - ctsu_correction_multi(&multi, p_self_work, NULL); + /* Store corrected data in p_pri_data[i] for initial offset tuning */ + for (i = 0; i < CTSU_CFG_NUM_SUMULTI; i++) + { + p_self_data[i] = multi.pri[i]; + } } else { + /* Store last moving averaged data */ + for (i = 0; i < CTSU_CFG_NUM_SUMULTI; i++) + { + average_self[i] = p_self_data[i]; + } + + /* Matching values */ + ctsu_correction_multi(&multi, p_self_data, NULL); + + /* Update moving averaged data */ for (i = 0; i < CTSU_CFG_NUM_SUMULTI; i++) { - ctsu_correction_calc(&p_instance_ctrl->p_self_raw[(element_id * CTSU_CFG_NUM_SUMULTI) + i], - multi.pri[i], - &multi.calc); + ctsu_moving_average(&average_self[i], p_self_data[i], p_instance_ctrl->average); + p_self_data[i] = average_self[i]; } } } if (CTSU_MODE_CURRENT_SCAN == p_instance_ctrl->p_ctsu_cfg->md) { + p_self_data = (p_instance_ctrl->p_self_data + (element_id * CTSU_CFG_NUM_SUMULTI)); multi.pri[0] = p_instance_ctrl->p_self_raw[element_id]; #if (CTSU_CFG_TEMP_CORRECTION_SUPPORT == 1) #if (CTSU_CFG_CALIB_RTRIM_SUPPORT == 1) - ctsu_correction_offset_adjust(&multi.pri[0], p_instance_ctrl->p_self_raw[element_id], + ctsu_correction_offset_adjust(&multi.pri[0], + p_instance_ctrl->p_self_raw[element_id], multi.offset_error[0]); #endif #endif - ctsu_correction_calc(p_self_work, multi.pri[0], &multi.calc); + + /* Store last moving averaged data */ + average_self[0] = p_self_data[0]; + + /* Correction */ + ctsu_correction_calc(p_self_data, multi.pri[0], &calc); + + /* Update moving averaged data */ + if (1 < p_instance_ctrl->average) + { + ctsu_moving_average(&average_self[0], p_self_data[0], p_instance_ctrl->average); + p_self_data[0] = average_self[0]; + } } #endif #if (CTSU_CFG_NUM_MUTUAL_ELEMENTS != 0) - p_pri_work = (p_instance_ctrl->p_mutual_pri_work + element_id); - p_snd_work = (p_instance_ctrl->p_mutual_snd_work + element_id); - if (CTSU_MODE_MUTUAL_FULL_SCAN == p_instance_ctrl->p_ctsu_cfg->md) + if (CTSU_MODE_MUTUAL_FULL_SCAN == (CTSU_MODE_MUTUAL_FULL_SCAN & p_instance_ctrl->p_ctsu_cfg->md)) { + #if (CTSU_CFG_NUM_CFC != 0) + if (CTSU_MODE_MUTUAL_CFC_SCAN == p_instance_ctrl->p_ctsu_cfg->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]; + #endif + for (i = 0; i < CTSU_CFG_NUM_SUMULTI; i++) { - multi.pri[i] = p_instance_ctrl->p_mutual_raw[(element_id * CTSU_MUTUAL_BUF_SIZE) + (i * 2)]; - multi.snd[i] = p_instance_ctrl->p_mutual_raw[(element_id * CTSU_MUTUAL_BUF_SIZE) + (i * 2) + 1]; + #if (CTSU_CFG_NUM_CFC != 0) + if (CTSU_MODE_MUTUAL_CFC_SCAN == p_instance_ctrl->p_ctsu_cfg->md) + { + multi.pri[i] = p_instance_ctrl->p_mutual_raw[offset + cfc_id + (num_rx * i * 2)]; + multi.snd[i] = p_instance_ctrl->p_mutual_raw[offset + cfc_id + (num_rx * i * 2) + num_rx]; + } + #endif + if (CTSU_MODE_MUTUAL_FULL_SCAN == p_instance_ctrl->p_ctsu_cfg->md) + { + multi.pri[i] = p_instance_ctrl->p_mutual_raw[(element_id * CTSU_MUTUAL_BUF_SIZE) + (i * 2)]; + multi.snd[i] = p_instance_ctrl->p_mutual_raw[(element_id * CTSU_MUTUAL_BUF_SIZE) + (i * 2) + 1]; #if (CTSU_CFG_TEMP_CORRECTION_SUPPORT == 1) #if (CTSU_CFG_CALIB_RTRIM_SUPPORT == 1) - ctsu_correction_offset_adjust(&multi.pri[i], - p_instance_ctrl->p_mutual_raw[(element_id * CTSU_MUTUAL_BUF_SIZE) + - (i * 2)], - multi.offset_error[i]); - ctsu_correction_offset_adjust(&multi.snd[i], - p_instance_ctrl->p_mutual_raw[(element_id * CTSU_MUTUAL_BUF_SIZE) + - (i * 2) + 1], - multi.offset_error[i]); + ctsu_correction_offset_adjust(&multi.pri[i], + p_instance_ctrl->p_mutual_raw[(element_id * CTSU_MUTUAL_BUF_SIZE) + + (i * 2)], + multi.offset_error[i]); + ctsu_correction_offset_adjust(&multi.snd[i], + p_instance_ctrl->p_mutual_raw[(element_id * CTSU_MUTUAL_BUF_SIZE) + + (i * 2) + 1], + multi.offset_error[i]); #endif #endif - } + } - if (CTSU_TUNING_COMPLETE == p_instance_ctrl->tuning) - { - ctsu_correction_multi(&multi, p_pri_work, p_snd_work); + ctsu_correction_calc(&multi.pri[i], multi.pri[i], &calc); + ctsu_correction_calc(&multi.snd[i], multi.snd[i], &calc); } - else + + p_pri_data = (p_instance_ctrl->p_mutual_pri_data + (element_id * CTSU_CFG_NUM_SUMULTI)); + p_snd_data = (p_instance_ctrl->p_mutual_snd_data + (element_id * CTSU_CFG_NUM_SUMULTI)); + + if (0 == p_instance_ctrl->average) { + /* Store corrected data in p_pri_data[i] for initial offset tuning */ for (i = 0; i < CTSU_CFG_NUM_SUMULTI; i++) { - ctsu_correction_calc(&p_instance_ctrl->p_mutual_raw[(element_id * CTSU_MUTUAL_BUF_SIZE) + (i * 2)], - multi.pri[i], - &multi.calc); + p_pri_data[i] = multi.pri[i]; } } - } - - #if (CTSU_CFG_NUM_CFC != 0) - if (CTSU_MODE_MUTUAL_CFC_SCAN == p_instance_ctrl->p_ctsu_cfg->md) - { - num_rx = p_instance_ctrl->p_ctsu_cfg->num_rx; - cfc_id = (uint16_t) (element_id % num_rx); - if (0 == cfc_id) + else { - if (0 == element_id) + /* Store last moving averaged data */ + for (i = 0; i < CTSU_CFG_NUM_SUMULTI; i++) { - offset = 0; + average_pri[i] = p_pri_data[i]; + average_snd[i] = p_snd_data[i]; } - else + + /* Matching values */ + ctsu_correction_multi(&multi, p_pri_data, p_snd_data); + + /* Update moving averaged data */ + for (i = 0; i < CTSU_CFG_NUM_SUMULTI; i++) { - offset = (uint16_t) (offset + (num_rx * CTSU_MUTUAL_BUF_SIZE)); + ctsu_moving_average(&average_pri[i], p_pri_data[i], p_instance_ctrl->average); + p_pri_data[i] = average_pri[i]; + ctsu_moving_average(&average_snd[i], p_snd_data[i], p_instance_ctrl->average); + p_snd_data[i] = average_snd[i]; } } - - for (i = 0; i < CTSU_CFG_NUM_SUMULTI; i++) - { - multi.pri[i] = p_instance_ctrl->p_mutual_raw[offset + cfc_id + (num_rx * i * 2)]; - multi.snd[i] = p_instance_ctrl->p_mutual_raw[offset + cfc_id + (num_rx * i * 2) + num_rx]; - } - - multi.calc.cfc = cfc_ts_table[cfc_id]; - - ctsu_correction_multi(&multi, p_pri_work, p_snd_work); } - #endif #endif } #endif @@ -2714,7 +2750,6 @@ void ctsu_correction_multi (ctsu_correction_multi_t * p_multi, uint16_t * p_pri, uint32_t add_pri; uint32_t add_snd; #if CTSU_CFG_NUM_SUMULTI >= 3 - int32_t min; int32_t diff[CTSU_CFG_NUM_SUMULTI]; #endif @@ -2726,18 +2761,14 @@ void ctsu_correction_multi (ctsu_correction_multi_t * p_multi, uint16_t * p_pri, sumulti[2] = CTSU_CFG_SUMULTI2 + 1; #endif - for (i = 0; i < CTSU_CFG_NUM_SUMULTI; i++) - { - ctsu_correction_calc(&pri_calc[i], p_multi->pri[i], &p_multi->calc); - } - + pri_calc[0] = p_multi->pri[0]; for (i = 1; i < CTSU_CFG_NUM_SUMULTI; i++) { pri_calc[i] = - (uint16_t) ((((pri_calc[i] + p_multi->offset[i]) * sumulti[0]) / sumulti[i]) - p_multi->offset[0]); + (uint16_t) ((((p_multi->pri[i] + p_multi->offset[i]) * sumulti[0]) / sumulti[i]) - p_multi->offset[0]); } - if (CTSU_MODE_SELF_MULTI_SCAN == p_multi->calc.md) + if (NULL == p_snd) { for (i = 0; i < CTSU_CFG_NUM_SUMULTI; i++) { @@ -2746,83 +2777,90 @@ void ctsu_correction_multi (ctsu_correction_multi_t * p_multi, uint16_t * p_pri, } else { - for (i = 0; i < CTSU_CFG_NUM_SUMULTI; i++) - { - ctsu_correction_calc(&snd_calc[i], p_multi->snd[i], &p_multi->calc); - } - + snd_calc[0] = p_multi->snd[0]; for (i = 1; i < CTSU_CFG_NUM_SUMULTI; i++) { snd_calc[i] = - (uint16_t) ((((snd_calc[i] + p_multi->offset[i]) * sumulti[0]) / sumulti[i]) - p_multi->offset[0]); + (uint16_t) ((((p_multi->snd[i] + p_multi->offset[i]) * sumulti[0]) / sumulti[i]) - p_multi->offset[0]); } } + if (true == p_multi->judge_disable) + { + for (i = 0; i < CTSU_CFG_NUM_SUMULTI; i++) + { + p_pri[i] = pri_calc[i]; + if (NULL != p_snd) + { + p_snd[i] = snd_calc[i]; + } + } + } + else + { #if CTSU_CFG_NUM_SUMULTI == 1 - add_pri = (uint32_t) (pri_calc[0]); - add_snd = (uint32_t) (snd_calc[0]); + add_pri = (uint32_t) (pri_calc[0]); + add_snd = (uint32_t) (snd_calc[0]); #endif #if CTSU_CFG_NUM_SUMULTI == 2 - add_pri = (uint32_t) (pri_calc[0] + pri_calc[1]); - add_snd = (uint32_t) (snd_calc[0] + snd_calc[1]); + add_pri = (uint32_t) (pri_calc[0] + pri_calc[1]); + add_snd = (uint32_t) (snd_calc[0] + snd_calc[1]); #endif #if CTSU_CFG_NUM_SUMULTI >= 3 - 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]); + 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) + for (i = 0; i < CTSU_CFG_NUM_SUMULTI; i++) { - diff[i] = -diff[i]; + if (diff[i] < 0) + { + diff[i] = -diff[i]; + } } - } - - min = diff[0]; - if (diff[1] < min) - { - min = diff[1]; - } - if (diff[2] < min) - { - add_pri = (uint32_t) (pri_calc[1] + pri_calc[2]); - add_snd = (uint32_t) (snd_calc[1] + snd_calc[2]); - } - else - { - if (min == diff[0]) + /* 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 = (uint32_t) (pri_calc[0] + pri_calc[1]); add_snd = (uint32_t) (snd_calc[0] + snd_calc[1]); } else { - add_pri = (uint32_t) (pri_calc[0] + pri_calc[2]); - add_snd = (uint32_t) (snd_calc[0] + snd_calc[2]); + if (diff[1] < diff[2]) + { + add_pri = (uint32_t) (pri_calc[0] + pri_calc[2]); + add_snd = (uint32_t) (snd_calc[0] + snd_calc[2]); + } + else + { + add_pri = (uint32_t) (pri_calc[1] + pri_calc[2]); + add_snd = (uint32_t) (snd_calc[1] + snd_calc[2]); + } } - } #endif - if (CTSU_COUNT_MAX < add_pri) - { - *p_pri = CTSU_COUNT_MAX; - } - else - { - *p_pri = (uint16_t) add_pri; - } - - if (NULL != p_snd) - { - if (CTSU_COUNT_MAX < add_snd) + if (CTSU_COUNT_MAX < add_pri) { - *p_snd = CTSU_COUNT_MAX; + *p_pri = CTSU_COUNT_MAX; } else { - *p_snd = (uint16_t) add_snd; + *p_pri = (uint16_t) add_pri; + } + + if (NULL != p_snd) + { + if (CTSU_COUNT_MAX < add_snd) + { + *p_snd = CTSU_COUNT_MAX; + } + else + { + *p_snd = (uint16_t) add_snd; + } } } } diff --git a/ra/fsp/src/r_doc/r_doc.c b/ra/fsp/src/r_doc/r_doc.c index 6cd3715cb..f4855d4ea 100644 --- a/ra/fsp/src/r_doc/r_doc.c +++ b/ra/fsp/src/r_doc/r_doc.c @@ -38,7 +38,7 @@ **********************************************************************************************************************/ #if defined(__ARMCC_VERSION) || defined(__ICCARM__) -typedef void (BSP_CMSE_NONSECURE_CALL * volatile doc_prv_ns_callback)(doc_callback_args_t * p_args); +typedef void (BSP_CMSE_NONSECURE_CALL * doc_prv_ns_callback)(doc_callback_args_t * p_args); #elif defined(__GNUC__) typedef BSP_CMSE_NONSECURE_CALL void (*volatile doc_prv_ns_callback)(doc_callback_args_t * p_args); #endif diff --git a/ra/fsp/src/r_ether/r_ether.c b/ra/fsp/src/r_ether/r_ether.c index b4d865aaf..2a12d8717 100644 --- a/ra/fsp/src/r_ether/r_ether.c +++ b/ra/fsp/src/r_ether/r_ether.c @@ -30,7 +30,6 @@ ***********************************************************************************************************************/ #ifndef ETHER_ERROR_RETURN -/*LDRA_INSPECTED 77 S This macro does not work when surrounded by parentheses. */ #define ETHER_ERROR_RETURN(a, err) FSP_ERROR_RETURN((a), (err)) #endif @@ -309,8 +308,7 @@ fsp_err_t R_ETHER_Open (ether_ctrl_t * const p_ctrl, ether_cfg_t const * const p i < (p_instance_ctrl->p_ether_cfg->num_tx_descriptors + p_instance_ctrl->p_ether_cfg->num_rx_descriptors); i++) { - memset(p_instance_ctrl->p_ether_cfg->pp_ether_buffers[i], - 0x00, + memset(p_instance_ctrl->p_ether_cfg->pp_ether_buffers[i], 0x00, p_instance_ctrl->p_ether_cfg->ether_buffer_size); } @@ -1531,9 +1529,7 @@ static fsp_err_t ether_do_link (ether_instance_ctrl_t * const p_instance_ctrl, c * Enable PAUSE for full duplex link depending on * the pause resolution results */ - ether_pause_resolution(local_pause_bits, - partner_pause_bits, - &transmit_pause_set, + ether_pause_resolution(local_pause_bits, partner_pause_bits, &transmit_pause_set, &receive_pause_set); if (ETHER_PAUSE_XMIT_ON == transmit_pause_set) diff --git a/ra/fsp/src/r_ether_phy/r_ether_phy.c b/ra/fsp/src/r_ether_phy/r_ether_phy.c index e58456dc8..4881d66a5 100644 --- a/ra/fsp/src/r_ether_phy/r_ether_phy.c +++ b/ra/fsp/src/r_ether_phy/r_ether_phy.c @@ -26,122 +26,120 @@ #include "bsp_api.h" #include "r_ether_phy.h" - /*********************************************************************************************************************** - Macro definitions + * Macro definitions ***********************************************************************************************************************/ #ifndef ETHER_PHY_ERROR_RETURN -/*LDRA_INSPECTED 77 S This macro does not work when surrounded by parentheses. */ #define ETHER_PHY_ERROR_RETURN(a, err) FSP_ERROR_RETURN((a), (err)) #endif -#define ETHERC_REG_SIZE (0x400UL) +#define ETHERC_REG_SIZE (0x400UL) /** "RPHY" in ASCII. Used to determine if the control block is open. */ -#define ETHER_PHY_OPEN (0x52504859U) +#define ETHER_PHY_OPEN (0x52504859U) /* Media Independent Interface */ -#define ETHER_PHY_MII_ST (1) -#define ETHER_PHY_MII_READ (2) -#define ETHER_PHY_MII_WRITE (1) +#define ETHER_PHY_MII_ST (1) +#define ETHER_PHY_MII_READ (2) +#define ETHER_PHY_MII_WRITE (1) /* Standard PHY Registers */ -#define ETHER_PHY_REG_CONTROL (0) -#define ETHER_PHY_REG_STATUS (1) -#define ETHER_PHY_REG_IDENTIFIER1 (2) -#define ETHER_PHY_REG_IDENTIFIER2 (3) -#define ETHER_PHY_REG_AN_ADVERTISEMENT (4) -#define ETHER_PHY_REG_AN_LINK_PARTNER (5) -#define ETHER_PHY_REG_AN_EXPANSION (6) +#define ETHER_PHY_REG_CONTROL (0) +#define ETHER_PHY_REG_STATUS (1) +#define ETHER_PHY_REG_IDENTIFIER1 (2) +#define ETHER_PHY_REG_IDENTIFIER2 (3) +#define ETHER_PHY_REG_AN_ADVERTISEMENT (4) +#define ETHER_PHY_REG_AN_LINK_PARTNER (5) +#define ETHER_PHY_REG_AN_EXPANSION (6) /* Basic Mode Control Register Bit Definitions */ -#define ETHER_PHY_CONTROL_RESET (1 << 15) -#define ETHER_PHY_CONTROL_LOOPBACK (1 << 14) -#define ETHER_PHY_CONTROL_100_MBPS (1 << 13) -#define ETHER_PHY_CONTROL_AN_ENABLE (1 << 12) -#define ETHER_PHY_CONTROL_POWER_DOWN (1 << 11) -#define ETHER_PHY_CONTROL_ISOLATE (1 << 10) -#define ETHER_PHY_CONTROL_AN_RESTART (1 << 9) -#define ETHER_PHY_CONTROL_FULL_DUPLEX (1 << 8) -#define ETHER_PHY_CONTROL_COLLISION (1 << 7) +#define ETHER_PHY_CONTROL_RESET (1 << 15) +#define ETHER_PHY_CONTROL_LOOPBACK (1 << 14) +#define ETHER_PHY_CONTROL_100_MBPS (1 << 13) +#define ETHER_PHY_CONTROL_AN_ENABLE (1 << 12) +#define ETHER_PHY_CONTROL_POWER_DOWN (1 << 11) +#define ETHER_PHY_CONTROL_ISOLATE (1 << 10) +#define ETHER_PHY_CONTROL_AN_RESTART (1 << 9) +#define ETHER_PHY_CONTROL_FULL_DUPLEX (1 << 8) +#define ETHER_PHY_CONTROL_COLLISION (1 << 7) /* Basic Mode Status Register Bit Definitions */ -#define ETHER_PHY_STATUS_100_T4 (1 << 15) -#define ETHER_PHY_STATUS_100F (1 << 14) -#define ETHER_PHY_STATUS_100H (1 << 13) -#define ETHER_PHY_STATUS_10F (1 << 12) -#define ETHER_PHY_STATUS_10H (1 << 11) -#define ETHER_PHY_STATUS_AN_COMPLETE (1 << 5) -#define ETHER_PHY_STATUS_RM_FAULT (1 << 4) -#define ETHER_PHY_STATUS_AN_ABILITY (1 << 3) -#define ETHER_PHY_STATUS_LINK_UP (1 << 2) -#define ETHER_PHY_STATUS_JABBER (1 << 1) -#define ETHER_PHY_STATUS_EX_CAPABILITY (1 << 0) +#define ETHER_PHY_STATUS_100_T4 (1 << 15) +#define ETHER_PHY_STATUS_100F (1 << 14) +#define ETHER_PHY_STATUS_100H (1 << 13) +#define ETHER_PHY_STATUS_10F (1 << 12) +#define ETHER_PHY_STATUS_10H (1 << 11) +#define ETHER_PHY_STATUS_AN_COMPLETE (1 << 5) +#define ETHER_PHY_STATUS_RM_FAULT (1 << 4) +#define ETHER_PHY_STATUS_AN_ABILITY (1 << 3) +#define ETHER_PHY_STATUS_LINK_UP (1 << 2) +#define ETHER_PHY_STATUS_JABBER (1 << 1) +#define ETHER_PHY_STATUS_EX_CAPABILITY (1 << 0) /* Auto Negotiation Advertisement Bit Definitions */ -#define ETHER_PHY_AN_ADVERTISEMENT_NEXT_PAGE (1 << 15) -#define ETHER_PHY_AN_ADVERTISEMENT_RM_FAULT (1 << 13) -#define ETHER_PHY_AN_ADVERTISEMENT_ASM_DIR (1 << 11) -#define ETHER_PHY_AN_ADVERTISEMENT_PAUSE (1 << 10) -#define ETHER_PHY_AN_ADVERTISEMENT_100_T4 (1 << 9) -#define ETHER_PHY_AN_ADVERTISEMENT_100F (1 << 8) -#define ETHER_PHY_AN_ADVERTISEMENT_100H (1 << 7) -#define ETHER_PHY_AN_ADVERTISEMENT_10F (1 << 6) -#define ETHER_PHY_AN_ADVERTISEMENT_10H (1 << 5) -#define ETHER_PHY_AN_ADVERTISEMENT_SELECTOR (1 << 0) +#define ETHER_PHY_AN_ADVERTISEMENT_NEXT_PAGE (1 << 15) +#define ETHER_PHY_AN_ADVERTISEMENT_RM_FAULT (1 << 13) +#define ETHER_PHY_AN_ADVERTISEMENT_ASM_DIR (1 << 11) +#define ETHER_PHY_AN_ADVERTISEMENT_PAUSE (1 << 10) +#define ETHER_PHY_AN_ADVERTISEMENT_100_T4 (1 << 9) +#define ETHER_PHY_AN_ADVERTISEMENT_100F (1 << 8) +#define ETHER_PHY_AN_ADVERTISEMENT_100H (1 << 7) +#define ETHER_PHY_AN_ADVERTISEMENT_10F (1 << 6) +#define ETHER_PHY_AN_ADVERTISEMENT_10H (1 << 5) +#define ETHER_PHY_AN_ADVERTISEMENT_SELECTOR (1 << 0) /* Auto Negotiate Link Partner Ability Bit Definitions */ -#define ETHER_PHY_AN_LINK_PARTNER_NEXT_PAGE (1 << 15) -#define ETHER_PHY_AN_LINK_PARTNER_ACK (1 << 14) -#define ETHER_PHY_AN_LINK_PARTNER_RM_FAULT (1 << 13) -#define ETHER_PHY_AN_LINK_PARTNER_ASM_DIR (1 << 11) -#define ETHER_PHY_AN_LINK_PARTNER_PAUSE (1 << 10) -#define ETHER_PHY_AN_LINK_PARTNER_100_T4 (1 << 9) -#define ETHER_PHY_AN_LINK_PARTNER_100F (1 << 8) -#define ETHER_PHY_AN_LINK_PARTNER_100H (1 << 7) -#define ETHER_PHY_AN_LINK_PARTNER_10F (1 << 6) -#define ETHER_PHY_AN_LINK_PARTNER_10H (1 << 5) -#define ETHER_PHY_AN_LINK_PARTNER_SELECTOR (1 << 0) - -#define ETHER_PHY_PIR_MDI_MASK (1 << 3) -#define ETHER_PHY_PIR_MDO_HIGH (0x04) -#define ETHER_PHY_PIR_MDO_LOW (0x00) -#define ETHER_PHY_PIR_MMD_WRITE (0x02) -#define ETHER_PHY_PIR_MMD_READ (0x00) -#define ETHER_PHY_PIR_MDC_HIGH (0x01) -#define ETHER_PHY_PIR_MDC_LOW (0x00) - -#define ETHER_PHY_PREAMBLE_LENGTH (32U) -#define ETHER_PHY_WRITE_DATA_BIT_MASK (0x8000) +#define ETHER_PHY_AN_LINK_PARTNER_NEXT_PAGE (1 << 15) +#define ETHER_PHY_AN_LINK_PARTNER_ACK (1 << 14) +#define ETHER_PHY_AN_LINK_PARTNER_RM_FAULT (1 << 13) +#define ETHER_PHY_AN_LINK_PARTNER_ASM_DIR (1 << 11) +#define ETHER_PHY_AN_LINK_PARTNER_PAUSE (1 << 10) +#define ETHER_PHY_AN_LINK_PARTNER_100_T4 (1 << 9) +#define ETHER_PHY_AN_LINK_PARTNER_100F (1 << 8) +#define ETHER_PHY_AN_LINK_PARTNER_100H (1 << 7) +#define ETHER_PHY_AN_LINK_PARTNER_10F (1 << 6) +#define ETHER_PHY_AN_LINK_PARTNER_10H (1 << 5) +#define ETHER_PHY_AN_LINK_PARTNER_SELECTOR (1 << 0) + +#define ETHER_PHY_PIR_MDI_MASK (1 << 3) +#define ETHER_PHY_PIR_MDO_HIGH (0x04) +#define ETHER_PHY_PIR_MDO_LOW (0x00) +#define ETHER_PHY_PIR_MMD_WRITE (0x02) +#define ETHER_PHY_PIR_MMD_READ (0x00) +#define ETHER_PHY_PIR_MDC_HIGH (0x01) +#define ETHER_PHY_PIR_MDC_LOW (0x00) + +#define ETHER_PHY_PREAMBLE_LENGTH (32U) +#define ETHER_PHY_WRITE_DATA_BIT_MASK (0x8000) /*********************************************************************************************************************** - Typedef definitions + * Typedef definitions ***********************************************************************************************************************/ /*********************************************************************************************************************** - Exported global variables (to be accessed by other files) + * Exported global variables (to be accessed by other files) ***********************************************************************************************************************/ /*********************************************************************************************************************** - Exported global function + * Exported global function ***********************************************************************************************************************/ -uint32_t ether_phy_read (ether_phy_instance_ctrl_t * p_instance_ctrl, uint32_t reg_addr); -void ether_phy_write (ether_phy_instance_ctrl_t * p_instance_ctrl, uint32_t reg_addr, uint32_t data); -void ether_phy_targets_initialize (ether_phy_instance_ctrl_t * p_instance_ctrl) __attribute__((weak)); +uint32_t ether_phy_read(ether_phy_instance_ctrl_t * p_instance_ctrl, uint32_t reg_addr); +void ether_phy_write(ether_phy_instance_ctrl_t * p_instance_ctrl, uint32_t reg_addr, uint32_t data); +void ether_phy_targets_initialize(ether_phy_instance_ctrl_t * p_instance_ctrl) __attribute__((weak)); /*********************************************************************************************************************** - Private global variables and functions + * Private global variables and functions ***********************************************************************************************************************/ -static void ether_phy_preamble (ether_phy_instance_ctrl_t * p_instance_ctrl); -static void ether_phy_reg_set (ether_phy_instance_ctrl_t * p_instance_ctrl, uint32_t reg_addr, int32_t option); -static void ether_phy_reg_read (ether_phy_instance_ctrl_t * p_instance_ctrl, uint32_t *pdata); -static void ether_phy_reg_write (ether_phy_instance_ctrl_t * p_instance_ctrl, uint32_t data); -static void ether_phy_trans_zto0 (ether_phy_instance_ctrl_t * p_instance_ctrl); -static void ether_phy_trans_1to0 (ether_phy_instance_ctrl_t * p_instance_ctrl); -static void ether_phy_mii_write1 (ether_phy_instance_ctrl_t * p_instance_ctrl); -static void ether_phy_mii_write0 (ether_phy_instance_ctrl_t * p_instance_ctrl); +static void ether_phy_preamble(ether_phy_instance_ctrl_t * p_instance_ctrl); +static void ether_phy_reg_set(ether_phy_instance_ctrl_t * p_instance_ctrl, uint32_t reg_addr, int32_t option); +static void ether_phy_reg_read(ether_phy_instance_ctrl_t * p_instance_ctrl, uint32_t * pdata); +static void ether_phy_reg_write(ether_phy_instance_ctrl_t * p_instance_ctrl, uint32_t data); +static void ether_phy_trans_zto0(ether_phy_instance_ctrl_t * p_instance_ctrl); +static void ether_phy_trans_1to0(ether_phy_instance_ctrl_t * p_instance_ctrl); +static void ether_phy_mii_write1(ether_phy_instance_ctrl_t * p_instance_ctrl); +static void ether_phy_mii_write0(ether_phy_instance_ctrl_t * p_instance_ctrl); /** ETHER_PHYHAL module version data structure */ static const fsp_version_t module_version = @@ -156,12 +154,12 @@ static const fsp_version_t module_version = /*LDRA_INSPECTED 27 D This structure must be accessible in user code. It cannot be static. */ const ether_phy_api_t g_ether_phy_on_ether_phy = { - .open = R_ETHER_PHY_Open, - .close = R_ETHER_PHY_Close, - .startAutoNegotiate = R_ETHER_PHY_StartAutoNegotiate, - .linkPartnerAbilityGet = R_ETHER_PHY_LinkPartnerAbilityGet, - .linkStatusGet = R_ETHER_PHY_LinkStatusGet, - .versionGet = R_ETHER_PHY_VersionGet + .open = R_ETHER_PHY_Open, + .close = R_ETHER_PHY_Close, + .startAutoNegotiate = R_ETHER_PHY_StartAutoNegotiate, + .linkPartnerAbilityGet = R_ETHER_PHY_LinkPartnerAbilityGet, + .linkStatusGet = R_ETHER_PHY_LinkStatusGet, + .versionGet = R_ETHER_PHY_VersionGet }; /*******************************************************************************************************************//** @@ -175,7 +173,7 @@ const ether_phy_api_t g_ether_phy_on_ether_phy = /********************************************************************************************************************//** * @brief Resets Ethernet PHY device. Implements @ref ether_phy_api_t::open. - * + * * @retval FSP_SUCCESS Channel opened successfully. * @retval FSP_ERR_ASSERTION Pointer to ETHER_PHY control block or configuration structure is NULL. * @retval FSP_ERR_ALREADY_OPEN Control block has already been opened or channel is being used by another @@ -186,9 +184,9 @@ const ether_phy_api_t g_ether_phy_on_ether_phy = ***********************************************************************************************************************/ fsp_err_t R_ETHER_PHY_Open (ether_phy_ctrl_t * const p_ctrl, ether_phy_cfg_t const * const p_cfg) { - fsp_err_t err = FSP_SUCCESS; + fsp_err_t err = FSP_SUCCESS; ether_phy_instance_ctrl_t * p_instance_ctrl = (ether_phy_instance_ctrl_t *) p_ctrl; - R_ETHERC0_Type * p_reg_etherc; + R_ETHERC0_Type * p_reg_etherc; uint32_t reg; uint32_t count = 0; @@ -197,12 +195,11 @@ fsp_err_t R_ETHER_PHY_Open (ether_phy_ctrl_t * const p_ctrl, ether_phy_cfg_t con ETHER_PHY_ERROR_RETURN(NULL != p_cfg, FSP_ERR_INVALID_POINTER); ETHER_PHY_ERROR_RETURN((ETHER_PHY_OPEN != p_instance_ctrl->open), FSP_ERR_ALREADY_OPEN); ETHER_PHY_ERROR_RETURN((BSP_FEATURE_ETHER_MAX_CHANNELS > p_cfg->channel), FSP_ERR_INVALID_CHANNEL); - #endif /** Make sure this channel exists. */ - p_reg_etherc = ((R_ETHERC0_Type *) (R_ETHERC0_BASE + (ETHERC_REG_SIZE * p_cfg->channel))); - p_instance_ctrl->p_reg_pir = (uint32_t *) &p_reg_etherc->PIR; + p_reg_etherc = ((R_ETHERC0_Type *) (R_ETHERC0_BASE + (ETHERC_REG_SIZE * p_cfg->channel))); + p_instance_ctrl->p_reg_pir = (uint32_t *) &p_reg_etherc->PIR; p_instance_ctrl->local_advertise = 0; /* Initialize configuration of ethernet phy module. */ @@ -220,7 +217,7 @@ fsp_err_t R_ETHER_PHY_Open (ether_phy_ctrl_t * const p_ctrl, ether_phy_cfg_t con if (count < p_cfg->phy_reset_wait_time) { - ether_phy_targets_initialize (p_instance_ctrl); + ether_phy_targets_initialize(p_instance_ctrl); p_instance_ctrl->open = ETHER_PHY_OPEN; @@ -232,7 +229,7 @@ fsp_err_t R_ETHER_PHY_Open (ether_phy_ctrl_t * const p_ctrl, ether_phy_cfg_t con } return err; -} /* End of function R_ETHER_PHY_Open() */ +} /* End of function R_ETHER_PHY_Open() */ /********************************************************************************************************************//** * @brief Close Ethernet PHY device. Implements @ref ether_phy_api_t::close. @@ -244,7 +241,7 @@ fsp_err_t R_ETHER_PHY_Open (ether_phy_ctrl_t * const p_ctrl, ether_phy_cfg_t con ***********************************************************************************************************************/ fsp_err_t R_ETHER_PHY_Close (ether_phy_ctrl_t * const p_ctrl) { - fsp_err_t err = FSP_SUCCESS; + fsp_err_t err = FSP_SUCCESS; ether_phy_instance_ctrl_t * p_instance_ctrl = (ether_phy_instance_ctrl_t *) p_ctrl; #if (ETHER_PHY_CFG_PARAM_CHECKING_ENABLE) @@ -255,12 +252,12 @@ fsp_err_t R_ETHER_PHY_Close (ether_phy_ctrl_t * const p_ctrl) /** Clear configure block parameters. */ p_instance_ctrl->p_ether_phy_cfg = NULL; p_instance_ctrl->local_advertise = 0; - p_instance_ctrl->p_reg_pir = NULL; + p_instance_ctrl->p_reg_pir = NULL; p_instance_ctrl->open = 0; return err; -} /* End of function R_ETHER_PHY_Close() */ +} /* End of function R_ETHER_PHY_Close() */ /********************************************************************************************************************//** * @brief Starts auto-negotiate. Implements @ref ether_phy_api_t::startAutoNegotiate. @@ -270,9 +267,8 @@ fsp_err_t R_ETHER_PHY_Close (ether_phy_ctrl_t * const p_ctrl) * @retval FSP_ERR_NOT_OPEN The control block has not been opened * ***********************************************************************************************************************/ -fsp_err_t R_ETHER_PHY_StartAutoNegotiate(ether_phy_ctrl_t * const p_ctrl) +fsp_err_t R_ETHER_PHY_StartAutoNegotiate (ether_phy_ctrl_t * const p_ctrl) { - ether_phy_instance_ctrl_t * p_instance_ctrl = (ether_phy_instance_ctrl_t *) p_ctrl; #if (ETHER_PHY_CFG_PARAM_CHECKING_ENABLE) @@ -285,34 +281,34 @@ fsp_err_t R_ETHER_PHY_StartAutoNegotiate(ether_phy_ctrl_t * const p_ctrl) if (ETHER_PHY_FLOW_CONTROL_DISABLE == p_instance_ctrl->p_ether_phy_cfg->flow_control) { p_instance_ctrl->local_advertise = ((((ETHER_PHY_AN_ADVERTISEMENT_100F | - ETHER_PHY_AN_ADVERTISEMENT_100H) | - ETHER_PHY_AN_ADVERTISEMENT_10F) | - ETHER_PHY_AN_ADVERTISEMENT_10H) | - ETHER_PHY_AN_ADVERTISEMENT_SELECTOR); - + ETHER_PHY_AN_ADVERTISEMENT_100H) | + ETHER_PHY_AN_ADVERTISEMENT_10F) | + ETHER_PHY_AN_ADVERTISEMENT_10H) | + ETHER_PHY_AN_ADVERTISEMENT_SELECTOR); } - /* When pause frame is used */ else { p_instance_ctrl->local_advertise = ((((((ETHER_PHY_AN_ADVERTISEMENT_ASM_DIR | - ETHER_PHY_AN_ADVERTISEMENT_PAUSE) | - ETHER_PHY_AN_ADVERTISEMENT_100F) | - ETHER_PHY_AN_ADVERTISEMENT_100H) | - ETHER_PHY_AN_ADVERTISEMENT_10F) | - ETHER_PHY_AN_ADVERTISEMENT_10H) | - ETHER_PHY_AN_ADVERTISEMENT_SELECTOR); + ETHER_PHY_AN_ADVERTISEMENT_PAUSE) | + ETHER_PHY_AN_ADVERTISEMENT_100F) | + ETHER_PHY_AN_ADVERTISEMENT_100H) | + ETHER_PHY_AN_ADVERTISEMENT_10F) | + ETHER_PHY_AN_ADVERTISEMENT_10H) | + ETHER_PHY_AN_ADVERTISEMENT_SELECTOR); } /* Configure what the PHY and the Ethernet controller on this board supports */ ether_phy_write(p_instance_ctrl, ETHER_PHY_REG_AN_ADVERTISEMENT, p_instance_ctrl->local_advertise); - ether_phy_write(p_instance_ctrl, ETHER_PHY_REG_CONTROL, (ETHER_PHY_CONTROL_AN_ENABLE | - ETHER_PHY_CONTROL_AN_RESTART)); + ether_phy_write(p_instance_ctrl, + ETHER_PHY_REG_CONTROL, + (ETHER_PHY_CONTROL_AN_ENABLE | + ETHER_PHY_CONTROL_AN_RESTART)); ether_phy_read(p_instance_ctrl, ETHER_PHY_REG_AN_ADVERTISEMENT); return FSP_SUCCESS; -} /* End of function R_ETHER_PHY_StartAutoNegotiate() */ +} /* End of function R_ETHER_PHY_StartAutoNegotiate() */ /********************************************************************************************************************//** * @brief Reports the other side's physical capability. Implements @ref ether_phy_api_t::linkPartnerAbilityGet. @@ -324,8 +320,10 @@ fsp_err_t R_ETHER_PHY_StartAutoNegotiate(ether_phy_ctrl_t * const p_ctrl) * @retval FSP_ERR_ETHER_PHY_ERROR_LINK PHY-LSI is not link up. * @retval FSP_ERR_ETHER_PHY_NOT_READY The auto-negotiation isn't completed ***********************************************************************************************************************/ -fsp_err_t R_ETHER_PHY_LinkPartnerAbilityGet(ether_phy_ctrl_t * const p_ctrl, uint32_t * const p_line_speed_duplex, - uint32_t * const p_local_pause, uint32_t * const p_partner_pause) +fsp_err_t R_ETHER_PHY_LinkPartnerAbilityGet (ether_phy_ctrl_t * const p_ctrl, + uint32_t * const p_line_speed_duplex, + uint32_t * const p_local_pause, + uint32_t * const p_partner_pause) { ether_phy_instance_ctrl_t * p_instance_ctrl = (ether_phy_instance_ctrl_t *) p_ctrl; uint32_t reg; @@ -357,7 +355,8 @@ fsp_err_t R_ETHER_PHY_LinkPartnerAbilityGet(ether_phy_ctrl_t * const p_ctrl, uin } /* When the auto-negotiation isn't completed, return error */ - ETHER_PHY_ERROR_RETURN(ETHER_PHY_STATUS_AN_COMPLETE == (reg & ETHER_PHY_STATUS_AN_COMPLETE), FSP_ERR_ETHER_PHY_NOT_READY); + ETHER_PHY_ERROR_RETURN(ETHER_PHY_STATUS_AN_COMPLETE == (reg & ETHER_PHY_STATUS_AN_COMPLETE), + FSP_ERR_ETHER_PHY_NOT_READY); /* Get the link partner response */ reg = ether_phy_read(p_instance_ctrl, ETHER_PHY_REG_AN_LINK_PARTNER); @@ -395,8 +394,7 @@ fsp_err_t R_ETHER_PHY_LinkPartnerAbilityGet(ether_phy_ctrl_t * const p_ctrl, uin } return FSP_SUCCESS; - -} /* End of function R_ETHER_PHY_LinkPartnerAbilityGet() */ +} /* End of function R_ETHER_PHY_LinkPartnerAbilityGet() */ /********************************************************************************************************************//** * @brief Returns the status of the physical link. Implements @ref ether_phy_api_t::linkStatusGet. @@ -406,11 +404,11 @@ fsp_err_t R_ETHER_PHY_LinkPartnerAbilityGet(ether_phy_ctrl_t * const p_ctrl, uin * @retval FSP_ERR_NOT_OPEN The control block has not been opened * @retval FSP_ERR_ETHER_PHY_ERROR_LINK PHY-LSI is not link up. ***********************************************************************************************************************/ -fsp_err_t R_ETHER_PHY_LinkStatusGet(ether_phy_ctrl_t * const p_ctrl) +fsp_err_t R_ETHER_PHY_LinkStatusGet (ether_phy_ctrl_t * const p_ctrl) { ether_phy_instance_ctrl_t * p_instance_ctrl = (ether_phy_instance_ctrl_t *) p_ctrl; - uint32_t reg; - fsp_err_t err = FSP_SUCCESS; + uint32_t reg; + fsp_err_t err = FSP_SUCCESS; #if (ETHER_PHY_CFG_PARAM_CHECKING_ENABLE) FSP_ASSERT(p_instance_ctrl); @@ -434,9 +432,7 @@ fsp_err_t R_ETHER_PHY_LinkStatusGet(ether_phy_ctrl_t * const p_ctrl) } return err; - -} /* End of function R_ETHER_PHY_LinkStatusGet() */ - +} /* End of function R_ETHER_PHY_LinkStatusGet() */ /********************************************************************************************************************//** * @brief Provides API and code version in the user provided pointer. Implements @ref ether_phy_api_t::versionGet. @@ -480,8 +476,8 @@ uint32_t ether_phy_read (ether_phy_instance_ctrl_t * p_instance_ctrl, uint32_t r uint32_t data; /* - * The value is read from the PHY register by the frame format of MII Management Interface provided - * for by Table 22-12 of 22.2.4.5 of IEEE 802.3-2008_section2. + * The value is read from the PHY register by the frame format of MII Management Interface provided + * for by Table 22-12 of 22.2.4.5 of IEEE 802.3-2008_section2. */ ether_phy_preamble(p_instance_ctrl); ether_phy_reg_set(p_instance_ctrl, reg_addr, ETHER_PHY_MII_READ); @@ -489,8 +485,8 @@ uint32_t ether_phy_read (ether_phy_instance_ctrl_t * p_instance_ctrl, uint32_t r ether_phy_reg_read(p_instance_ctrl, &data); ether_phy_trans_zto0(p_instance_ctrl); - return (data); -} /* End of function ether_phy_read() */ + return data; +} /* End of function ether_phy_read() */ /*********************************************************************************************************************** * Function Name: ether_phy_write @@ -507,19 +503,19 @@ void ether_phy_write (ether_phy_instance_ctrl_t * p_instance_ctrl, uint32_t reg_ { /* * The value is read from the PHY register by the frame format of MII Management Interface provided - * for by Table 22-12 of 22.2.4.5 of IEEE 802.3-2008_section2. + * for by Table 22-12 of 22.2.4.5 of IEEE 802.3-2008_section2. */ ether_phy_preamble(p_instance_ctrl); ether_phy_reg_set(p_instance_ctrl, reg_addr, ETHER_PHY_MII_WRITE); ether_phy_trans_1to0(p_instance_ctrl); ether_phy_reg_write(p_instance_ctrl, data); ether_phy_trans_zto0(p_instance_ctrl); -} /* End of function ether_phy_write() */ +} /* End of function ether_phy_write() */ /*********************************************************************************************************************** * Function Name: phy_preamble * Description : As preliminary preparation for access to the PHY module register, - * "1" is output via the MII management interface. + * "1" is output via the MII management interface. * Arguments : ether_channel - * Ethernet channel number * Return Value : none @@ -529,7 +525,7 @@ static void ether_phy_preamble (ether_phy_instance_ctrl_t * p_instance_ctrl) int16_t i; /* - * The processing of PRE (preamble) about the frame format of MII Management Interface which is + * The processing of PRE (preamble) about the frame format of MII Management Interface which is * provided by "Table 22-12" of "22.2.4.5" of "IEEE 802.3-2008_section2". */ i = ETHER_PHY_PREAMBLE_LENGTH; @@ -538,7 +534,7 @@ static void ether_phy_preamble (ether_phy_instance_ctrl_t * p_instance_ctrl) ether_phy_mii_write1(p_instance_ctrl); i--; } -} /* End of function ether_phy_preamble() */ +} /* End of function ether_phy_preamble() */ /*********************************************************************************************************************** * Function Name: ether_phy_reg_set @@ -553,28 +549,28 @@ static void ether_phy_preamble (ether_phy_instance_ctrl_t * p_instance_ctrl) ***********************************************************************************************************************/ static void ether_phy_reg_set (ether_phy_instance_ctrl_t * p_instance_ctrl, uint32_t reg_addr, int32_t option) { - int32_t i; + int32_t i; uint32_t data = 0; /* - * The processing of ST (start of frame),OP (operation code), PHYAD (PHY Address), and - * REGAD (Register Address) about the frame format of MII Management Interface which is + * The processing of ST (start of frame),OP (operation code), PHYAD (PHY Address), and + * REGAD (Register Address) about the frame format of MII Management Interface which is * provided by "Table 22-12" of "22.2.4.5" of "IEEE 802.3-2008_section2". */ - data = (ETHER_PHY_MII_ST << 14); /* ST code */ + data = (ETHER_PHY_MII_ST << 14); /* ST code */ if (ETHER_PHY_MII_READ == option) { - data |= (ETHER_PHY_MII_READ << 12); /* OP code(RD) */ + data |= (ETHER_PHY_MII_READ << 12); /* OP code(RD) */ } else { - data |= (ETHER_PHY_MII_WRITE << 12); /* OP code(WT) */ + data |= (ETHER_PHY_MII_WRITE << 12); /* OP code(WT) */ } data |= (uint32_t) (p_instance_ctrl->p_ether_phy_cfg->phy_lsi_address << 7); /* PHY Address */ - data |= (reg_addr << 2); /* Reg Address */ + data |= (reg_addr << 2); /* Reg Address */ i = 14; while (i > 0) @@ -587,10 +583,11 @@ static void ether_phy_reg_set (ether_phy_instance_ctrl_t * p_instance_ctrl, uint { ether_phy_mii_write1(p_instance_ctrl); } + data = (data << 1); i--; } -} /* End of function ether_phy_reg_set() */ +} /* End of function ether_phy_reg_set() */ /*********************************************************************************************************************** * Function Name: ether_phy_reg_read @@ -601,10 +598,10 @@ static void ether_phy_reg_set (ether_phy_instance_ctrl_t * p_instance_ctrl, uint * pointer to store the data read * Return Value : none ***********************************************************************************************************************/ -static void ether_phy_reg_read (ether_phy_instance_ctrl_t * p_instance_ctrl, uint32_t *pdata) +static void ether_phy_reg_read (ether_phy_instance_ctrl_t * p_instance_ctrl, uint32_t * pdata) { - int32_t i; - int32_t j; + int32_t i; + int32_t j; uint32_t reg_data; volatile uint32_t * petherc_pir; @@ -612,40 +609,42 @@ static void ether_phy_reg_read (ether_phy_instance_ctrl_t * p_instance_ctrl, uin petherc_pir = p_instance_ctrl->p_reg_pir; /* - * The processing of DATA (data) about reading of the frame format of MII Management Interface which is + * The processing of DATA (data) about reading of the frame format of MII Management Interface which is * provided by "Table 22-12" of "22.2.4.5" of "IEEE 802.3-2008_section2". */ reg_data = 0; - i = 16; + i = 16; while (i > 0) { for (j = p_instance_ctrl->p_ether_phy_cfg->mii_bit_access_wait_time; j > 0; j--) { - (*petherc_pir) = (ETHER_PHY_PIR_MDO_LOW|ETHER_PHY_PIR_MMD_READ|ETHER_PHY_PIR_MDC_LOW); + (*petherc_pir) = (ETHER_PHY_PIR_MDO_LOW | ETHER_PHY_PIR_MMD_READ | ETHER_PHY_PIR_MDC_LOW); } for (j = p_instance_ctrl->p_ether_phy_cfg->mii_bit_access_wait_time; j > 0; j--) { - (*petherc_pir) = (ETHER_PHY_PIR_MDO_LOW|ETHER_PHY_PIR_MMD_READ|ETHER_PHY_PIR_MDC_HIGH); + (*petherc_pir) = (ETHER_PHY_PIR_MDO_LOW | ETHER_PHY_PIR_MMD_READ | ETHER_PHY_PIR_MDC_HIGH); } + reg_data = (reg_data << 1); reg_data |= (uint32_t) (((*petherc_pir) & ETHER_PHY_PIR_MDI_MASK) >> 3); /* MDI read */ for (j = p_instance_ctrl->p_ether_phy_cfg->mii_bit_access_wait_time; j > 0; j--) { - (*petherc_pir) = (ETHER_PHY_PIR_MDO_LOW|ETHER_PHY_PIR_MMD_READ|ETHER_PHY_PIR_MDC_HIGH); + (*petherc_pir) = (ETHER_PHY_PIR_MDO_LOW | ETHER_PHY_PIR_MMD_READ | ETHER_PHY_PIR_MDC_HIGH); } for (j = p_instance_ctrl->p_ether_phy_cfg->mii_bit_access_wait_time; j > 0; j--) { - (*petherc_pir) = (ETHER_PHY_PIR_MDO_LOW|ETHER_PHY_PIR_MMD_READ|ETHER_PHY_PIR_MDC_LOW); + (*petherc_pir) = (ETHER_PHY_PIR_MDO_LOW | ETHER_PHY_PIR_MMD_READ | ETHER_PHY_PIR_MDC_LOW); } i--; } + (*pdata) = reg_data; -} /* End of function ether_phy_reg_read() */ +} /* End of function ether_phy_reg_read() */ /*********************************************************************************************************************** * Function Name: ether_phy_reg_write @@ -661,7 +660,7 @@ static void ether_phy_reg_write (ether_phy_instance_ctrl_t * p_instance_ctrl, ui int32_t i; /* - * The processing of DATA (data) about writing of the frame format of MII Management Interface which is + * The processing of DATA (data) about writing of the frame format of MII Management Interface which is * provided by "Table 22-12" of "22.2.4.5" of "IEEE 802.3-2008_section2". */ i = 16; @@ -675,15 +674,16 @@ static void ether_phy_reg_write (ether_phy_instance_ctrl_t * p_instance_ctrl, ui { ether_phy_mii_write1(p_instance_ctrl); } + i--; data = (data << 1); } -} /* End of function ether_phy_reg_write() */ +} /* End of function ether_phy_reg_write() */ /*********************************************************************************************************************** * Function Name: ether_phy_trans_zto0 * Description : Performs bus release so that PHY can drive data - * : for read operation + * : for read operation * Arguments : ether_channel - * Ethernet channel number * Return Value : none @@ -697,35 +697,34 @@ static void ether_phy_trans_zto0 (ether_phy_instance_ctrl_t * p_instance_ctrl) petherc_pir = p_instance_ctrl->p_reg_pir; /* - * The processing of TA (turnaround) about reading of the frame format of MII Management Interface which is + * The processing of TA (turnaround) about reading of the frame format of MII Management Interface which is * provided by "Table 22-12" of "22.2.4.5" of "IEEE 802.3-2008_section2". */ for (j = p_instance_ctrl->p_ether_phy_cfg->mii_bit_access_wait_time; j > 0; j--) { - (*petherc_pir) = (ETHER_PHY_PIR_MDO_LOW|ETHER_PHY_PIR_MMD_READ|ETHER_PHY_PIR_MDC_LOW); + (*petherc_pir) = (ETHER_PHY_PIR_MDO_LOW | ETHER_PHY_PIR_MMD_READ | ETHER_PHY_PIR_MDC_LOW); } for (j = p_instance_ctrl->p_ether_phy_cfg->mii_bit_access_wait_time; j > 0; j--) { - (*petherc_pir) = (ETHER_PHY_PIR_MDO_LOW|ETHER_PHY_PIR_MMD_READ|ETHER_PHY_PIR_MDC_HIGH); + (*petherc_pir) = (ETHER_PHY_PIR_MDO_LOW | ETHER_PHY_PIR_MMD_READ | ETHER_PHY_PIR_MDC_HIGH); } for (j = p_instance_ctrl->p_ether_phy_cfg->mii_bit_access_wait_time; j > 0; j--) { - (*petherc_pir) = (ETHER_PHY_PIR_MDO_LOW|ETHER_PHY_PIR_MMD_READ|ETHER_PHY_PIR_MDC_HIGH); + (*petherc_pir) = (ETHER_PHY_PIR_MDO_LOW | ETHER_PHY_PIR_MMD_READ | ETHER_PHY_PIR_MDC_HIGH); } for (j = p_instance_ctrl->p_ether_phy_cfg->mii_bit_access_wait_time; j > 0; j--) { - (*petherc_pir) = (ETHER_PHY_PIR_MDO_LOW|ETHER_PHY_PIR_MMD_READ|ETHER_PHY_PIR_MDC_LOW); + (*petherc_pir) = (ETHER_PHY_PIR_MDO_LOW | ETHER_PHY_PIR_MMD_READ | ETHER_PHY_PIR_MDC_LOW); } - -} /* End of function ether_phy_trans_zto0() */ +} /* End of function ether_phy_trans_zto0() */ /*********************************************************************************************************************** * Function Name: phy_trans_1to0 * Description : Switches data bus so MII interface can drive data - * : for write operation + * : for write operation * Arguments : ether_channel - * Ethernet channel number * Return Value : none @@ -738,89 +737,87 @@ static void ether_phy_trans_1to0 (ether_phy_instance_ctrl_t * p_instance_ctrl) */ ether_phy_mii_write1(p_instance_ctrl); ether_phy_mii_write0(p_instance_ctrl); -} /* End of function ether_phy_trans_1to0() */ +} /* End of function ether_phy_trans_1to0() */ /*********************************************************************************************************************** * Function Name: ether_phy_mii_write1 - * Description : Outputs 1 to the MII interface + * Description : Outputs 1 to the MII interface * Arguments : ether_channel - * Ethernet channel number * Return Value : none ***********************************************************************************************************************/ static void ether_phy_mii_write1 (ether_phy_instance_ctrl_t * p_instance_ctrl) { - int32_t j; + int32_t j; volatile uint32_t * petherc_pir; petherc_pir = p_instance_ctrl->p_reg_pir; /* - * The processing of one bit about frame format of MII Management Interface which is - * provided by "Table 22-12" of "22.2.4.5" of "IEEE 802.3-2008_section2". - * The data that 1 is output. + * The processing of one bit about frame format of MII Management Interface which is + * provided by "Table 22-12" of "22.2.4.5" of "IEEE 802.3-2008_section2". + * The data that 1 is output. */ for (j = p_instance_ctrl->p_ether_phy_cfg->mii_bit_access_wait_time; j > 0; j--) { - (*petherc_pir) = (ETHER_PHY_PIR_MDO_HIGH|ETHER_PHY_PIR_MMD_WRITE|ETHER_PHY_PIR_MDC_LOW); + (*petherc_pir) = (ETHER_PHY_PIR_MDO_HIGH | ETHER_PHY_PIR_MMD_WRITE | ETHER_PHY_PIR_MDC_LOW); } for (j = p_instance_ctrl->p_ether_phy_cfg->mii_bit_access_wait_time; j > 0; j--) { - (*petherc_pir) = (ETHER_PHY_PIR_MDO_HIGH|ETHER_PHY_PIR_MMD_WRITE|ETHER_PHY_PIR_MDC_HIGH); + (*petherc_pir) = (ETHER_PHY_PIR_MDO_HIGH | ETHER_PHY_PIR_MMD_WRITE | ETHER_PHY_PIR_MDC_HIGH); } for (j = p_instance_ctrl->p_ether_phy_cfg->mii_bit_access_wait_time; j > 0; j--) { - (*petherc_pir) = (ETHER_PHY_PIR_MDO_HIGH|ETHER_PHY_PIR_MMD_WRITE|ETHER_PHY_PIR_MDC_HIGH); + (*petherc_pir) = (ETHER_PHY_PIR_MDO_HIGH | ETHER_PHY_PIR_MMD_WRITE | ETHER_PHY_PIR_MDC_HIGH); } for (j = p_instance_ctrl->p_ether_phy_cfg->mii_bit_access_wait_time; j > 0; j--) { - (*petherc_pir) = (ETHER_PHY_PIR_MDO_HIGH|ETHER_PHY_PIR_MMD_WRITE|ETHER_PHY_PIR_MDC_LOW); + (*petherc_pir) = (ETHER_PHY_PIR_MDO_HIGH | ETHER_PHY_PIR_MMD_WRITE | ETHER_PHY_PIR_MDC_LOW); } - -} /* End of function ether_phy_mii_write1() */ +} /* End of function ether_phy_mii_write1() */ /*********************************************************************************************************************** * Function Name: ether_phy_mii_write0 - * Description : Outputs 0 to the MII interface + * Description : Outputs 0 to the MII interface * Arguments : ether_channel - * Ethernet channel number * Return Value : none ***********************************************************************************************************************/ static void ether_phy_mii_write0 (ether_phy_instance_ctrl_t * p_instance_ctrl) { - int32_t j; + int32_t j; volatile uint32_t * petherc_pir; - petherc_pir = p_instance_ctrl->p_reg_pir; + petherc_pir = p_instance_ctrl->p_reg_pir; /* - * The processing of one bit about frame format of MII Management Interface which is - * provided by "Table 22-12" of "22.2.4.5" of "IEEE 802.3-2008_section2". - * The data that 0 is output. + * The processing of one bit about frame format of MII Management Interface which is + * provided by "Table 22-12" of "22.2.4.5" of "IEEE 802.3-2008_section2". + * The data that 0 is output. */ for (j = p_instance_ctrl->p_ether_phy_cfg->mii_bit_access_wait_time; j > 0; j--) { - (*petherc_pir) = (ETHER_PHY_PIR_MDO_LOW|ETHER_PHY_PIR_MMD_WRITE|ETHER_PHY_PIR_MDC_LOW); + (*petherc_pir) = (ETHER_PHY_PIR_MDO_LOW | ETHER_PHY_PIR_MMD_WRITE | ETHER_PHY_PIR_MDC_LOW); } for (j = p_instance_ctrl->p_ether_phy_cfg->mii_bit_access_wait_time; j > 0; j--) { - (*petherc_pir) = (ETHER_PHY_PIR_MDO_LOW|ETHER_PHY_PIR_MMD_WRITE|ETHER_PHY_PIR_MDC_HIGH); + (*petherc_pir) = (ETHER_PHY_PIR_MDO_LOW | ETHER_PHY_PIR_MMD_WRITE | ETHER_PHY_PIR_MDC_HIGH); } for (j = p_instance_ctrl->p_ether_phy_cfg->mii_bit_access_wait_time; j > 0; j--) { - (*petherc_pir) = (ETHER_PHY_PIR_MDO_LOW|ETHER_PHY_PIR_MMD_WRITE|ETHER_PHY_PIR_MDC_HIGH); + (*petherc_pir) = (ETHER_PHY_PIR_MDO_LOW | ETHER_PHY_PIR_MMD_WRITE | ETHER_PHY_PIR_MDC_HIGH); } for (j = p_instance_ctrl->p_ether_phy_cfg->mii_bit_access_wait_time; j > 0; j--) { - (*petherc_pir) = (ETHER_PHY_PIR_MDO_LOW|ETHER_PHY_PIR_MMD_WRITE|ETHER_PHY_PIR_MDC_LOW); + (*petherc_pir) = (ETHER_PHY_PIR_MDO_LOW | ETHER_PHY_PIR_MMD_WRITE | ETHER_PHY_PIR_MDC_LOW); } - -} /* End of function ether_phy_mii_write0() */ +} /* End of function ether_phy_mii_write0() */ /*********************************************************************************************************************** * Function Name: ether_phy_targets_initialize @@ -831,8 +828,5 @@ static void ether_phy_mii_write0 (ether_phy_instance_ctrl_t * p_instance_ctrl) ***********************************************************************************************************************/ void ether_phy_targets_initialize (ether_phy_instance_ctrl_t * p_instance_ctrl) { - (void) p_instance_ctrl; - -} /* End of function ether_phy_targets_initialize() */ - +} /* End of function ether_phy_targets_initialize() */ diff --git a/ra/fsp/src/r_flash_hp/r_flash_hp.c b/ra/fsp/src/r_flash_hp/r_flash_hp.c index e5db8e648..c673dd2c9 100644 --- a/ra/fsp/src/r_flash_hp/r_flash_hp.c +++ b/ra/fsp/src/r_flash_hp/r_flash_hp.c @@ -33,7 +33,7 @@ **********************************************************************************************************************/ #if defined(__ARMCC_VERSION) || defined(__ICCARM__) -typedef void (BSP_CMSE_NONSECURE_CALL * volatile flash_hp_prv_ns_callback)(flash_callback_args_t * p_args); +typedef void (BSP_CMSE_NONSECURE_CALL * flash_hp_prv_ns_callback)(flash_callback_args_t * p_args); #elif defined(__GNUC__) typedef BSP_CMSE_NONSECURE_CALL void (*volatile flash_hp_prv_ns_callback)(flash_callback_args_t * p_args); #endif diff --git a/ra/fsp/src/r_gpt/r_gpt.c b/ra/fsp/src/r_gpt/r_gpt.c index b53ba0af4..604bcb25e 100644 --- a/ra/fsp/src/r_gpt/r_gpt.c +++ b/ra/fsp/src/r_gpt/r_gpt.c @@ -91,7 +91,7 @@ typedef enum e_gpt_prv_capture_event } gpt_prv_capture_event_t; #if defined(__ARMCC_VERSION) || defined(__ICCARM__) -typedef void (BSP_CMSE_NONSECURE_CALL * volatile gpt_prv_ns_callback)(timer_callback_args_t * p_args); +typedef void (BSP_CMSE_NONSECURE_CALL * gpt_prv_ns_callback)(timer_callback_args_t * p_args); #elif defined(__GNUC__) typedef BSP_CMSE_NONSECURE_CALL void (*volatile gpt_prv_ns_callback)(timer_callback_args_t * p_args); #endif diff --git a/ra/fsp/src/r_icu/r_icu.c b/ra/fsp/src/r_icu/r_icu.c index ed1d207a3..4a3e3f2c7 100644 --- a/ra/fsp/src/r_icu/r_icu.c +++ b/ra/fsp/src/r_icu/r_icu.c @@ -40,7 +40,7 @@ **********************************************************************************************************************/ #if defined(__ARMCC_VERSION) || defined(__ICCARM__) -typedef void (BSP_CMSE_NONSECURE_CALL * volatile icu_prv_ns_callback)(external_irq_callback_args_t * p_args); +typedef void (BSP_CMSE_NONSECURE_CALL * icu_prv_ns_callback)(external_irq_callback_args_t * p_args); #elif defined(__GNUC__) typedef BSP_CMSE_NONSECURE_CALL void (*volatile icu_prv_ns_callback)(external_irq_callback_args_t * p_args); #endif diff --git a/ra/fsp/src/r_iic_master/r_iic_master.c b/ra/fsp/src/r_iic_master/r_iic_master.c index 53903d916..9fbead2e3 100644 --- a/ra/fsp/src/r_iic_master/r_iic_master.c +++ b/ra/fsp/src/r_iic_master/r_iic_master.c @@ -134,7 +134,7 @@ typedef enum e_iic_master_transfer_dir_option } iic_master_transfer_dir_t; #if defined(__ARMCC_VERSION) || defined(__ICCARM__) -typedef void (BSP_CMSE_NONSECURE_CALL * volatile iic_master_prv_ns_callback)(i2c_master_callback_args_t * p_args); +typedef void (BSP_CMSE_NONSECURE_CALL * iic_master_prv_ns_callback)(i2c_master_callback_args_t * p_args); #elif defined(__GNUC__) typedef BSP_CMSE_NONSECURE_CALL void (*volatile iic_master_prv_ns_callback)(i2c_master_callback_args_t * p_args); #endif diff --git a/ra/fsp/src/r_iic_slave/r_iic_slave.c b/ra/fsp/src/r_iic_slave/r_iic_slave.c index c1dbc322d..593edb9db 100644 --- a/ra/fsp/src/r_iic_slave/r_iic_slave.c +++ b/ra/fsp/src/r_iic_slave/r_iic_slave.c @@ -115,7 +115,7 @@ * Typedef definitions *********************************************************************************************************************/ #if defined(__ARMCC_VERSION) || defined(__ICCARM__) -typedef void (BSP_CMSE_NONSECURE_CALL * volatile iic_slave_prv_ns_callback)(i2c_slave_callback_args_t * p_args); +typedef void (BSP_CMSE_NONSECURE_CALL * iic_slave_prv_ns_callback)(i2c_slave_callback_args_t * p_args); #elif defined(__GNUC__) typedef BSP_CMSE_NONSECURE_CALL void (*volatile iic_slave_prv_ns_callback)(i2c_slave_callback_args_t * p_args); #endif diff --git a/ra/fsp/src/r_iwdt/r_iwdt.c b/ra/fsp/src/r_iwdt/r_iwdt.c index 7bd281901..b5d36cf97 100644 --- a/ra/fsp/src/r_iwdt/r_iwdt.c +++ b/ra/fsp/src/r_iwdt/r_iwdt.c @@ -68,7 +68,7 @@ **********************************************************************************************************************/ #if defined(__ARMCC_VERSION) || defined(__ICCARM__) -typedef void (BSP_CMSE_NONSECURE_CALL * volatile iwdt_prv_ns_callback)(wdt_callback_args_t * p_args); +typedef void (BSP_CMSE_NONSECURE_CALL * iwdt_prv_ns_callback)(wdt_callback_args_t * p_args); #elif defined(__GNUC__) typedef BSP_CMSE_NONSECURE_CALL void (*volatile iwdt_prv_ns_callback)(wdt_callback_args_t * p_args); #endif diff --git a/ra/fsp/src/r_jpeg/r_jpeg.c b/ra/fsp/src/r_jpeg/r_jpeg.c index f79c00713..d9ce6aa72 100644 --- a/ra/fsp/src/r_jpeg/r_jpeg.c +++ b/ra/fsp/src/r_jpeg/r_jpeg.c @@ -598,7 +598,7 @@ fsp_err_t R_JPEG_DecodeLinesDecodedGet (jpeg_ctrl_t * p_api_ctrl, uint32_t * p_l * Configure horizontal and vertical subsampling. * @note This function can be used to scale the output of decoded image data. * - * @retval FSP_SUCCESS Horizontal stride value is properly configured. + * @retval FSP_SUCCESS Horizontal subsample value is properly configured. * @retval FSP_ERR_ASSERTION Pointer to the control block is NULL. * @retval FSP_ERR_NOT_OPEN JPEG not opened. **********************************************************************************************************************/ @@ -825,7 +825,7 @@ fsp_err_t R_JPEG_ModeSet (jpeg_ctrl_t * const p_api_ctrl, jpeg_mode_t mode) /*******************************************************************************************************************//** * Cancel an outstanding JPEG codec operation and close the device. * - * @retval FSP_SUCCESS The input data buffer is properly assigned to JPEG Codec device. + * @retval FSP_SUCCESS The input data buffer is closed. * @retval FSP_ERR_ASSERTION Pointer to the control block is NULL. * @retval FSP_ERR_NOT_OPEN JPEG not opened. **********************************************************************************************************************/ diff --git a/ra/fsp/src/r_kint/r_kint.c b/ra/fsp/src/r_kint/r_kint.c index a37fcf944..c39d56077 100644 --- a/ra/fsp/src/r_kint/r_kint.c +++ b/ra/fsp/src/r_kint/r_kint.c @@ -152,7 +152,7 @@ fsp_err_t R_KINT_Close (keymatrix_ctrl_t * const p_api_ctrl) * This function enables interrupts for the KINT peripheral after clearing any pending requests. * Implements @ref keymatrix_api_t::enable. * - * @retval FSP_SUCCESS Interrupt disabled successfully. + * @retval FSP_SUCCESS Interrupt enabled successfully. * @retval FSP_ERR_ASSERTION The p_ctrl parameter was null. * @retval FSP_ERR_NOT_OPEN The peripheral is not opened. **********************************************************************************************************************/ diff --git a/ra/fsp/src/r_lpm/r_lpm.c b/ra/fsp/src/r_lpm/r_lpm.c index eaeacddf6..6157fa145 100644 --- a/ra/fsp/src/r_lpm/r_lpm.c +++ b/ra/fsp/src/r_lpm/r_lpm.c @@ -277,7 +277,7 @@ fsp_err_t R_LPM_IoKeepClear (lpm_ctrl_t * const p_api_ctrl) /*******************************************************************************************************************//** * Close the LPM Instance * - * @retval FSP_SUCCESS LPM Software lock initialized + * @retval FSP_SUCCESS LPM mode closed * @retval FSP_ERR_NOT_OPEN LPM instance is not open * @retval FSP_ERR_ASSERTION Null Pointer **********************************************************************************************************************/ @@ -297,7 +297,7 @@ fsp_err_t R_LPM_Close (lpm_ctrl_t * const p_api_ctrl) /*******************************************************************************************************************//** * Get the driver version based on compile time macros. * - * @retval FSP_SUCCESS Successful close. + * @retval FSP_SUCCESS Successfully got version. * @retval FSP_ERR_ASSERTION p_version is NULL. **********************************************************************************************************************/ fsp_err_t R_LPM_VersionGet (fsp_version_t * const p_version) diff --git a/ra/fsp/src/r_lvd/r_lvd.c b/ra/fsp/src/r_lvd/r_lvd.c index 1cc383a1d..a4711b36b 100644 --- a/ra/fsp/src/r_lvd/r_lvd.c +++ b/ra/fsp/src/r_lvd/r_lvd.c @@ -77,7 +77,7 @@ * Typedef definitions **********************************************************************************************************************/ #if defined(__ARMCC_VERSION) || defined(__ICCARM__) -typedef void (BSP_CMSE_NONSECURE_CALL * volatile lvd_prv_ns_callback)(lvd_callback_args_t * p_args); +typedef void (BSP_CMSE_NONSECURE_CALL * lvd_prv_ns_callback)(lvd_callback_args_t * p_args); #elif defined(__GNUC__) typedef BSP_CMSE_NONSECURE_CALL void (*volatile lvd_prv_ns_callback)(lvd_callback_args_t * p_args); #endif diff --git a/ra/fsp/src/r_opamp/r_opamp.c b/ra/fsp/src/r_opamp/r_opamp.c index 409d155bf..af4aa7f60 100644 --- a/ra/fsp/src/r_opamp/r_opamp.c +++ b/ra/fsp/src/r_opamp/r_opamp.c @@ -88,7 +88,6 @@ static const uint32_t g_opamp_stabilization_lookup[] = **********************************************************************************************************************/ /** OPAMP Implementation of OPAMP interface. */ -/*LDRA_INSPECTED 27 D This structure must be accessible in user code. It cannot be static. */ const opamp_api_t g_opamp_on_opamp = { .open = R_OPAMP_Open, diff --git a/ra/fsp/src/r_ospi/r_ospi.c b/ra/fsp/src/r_ospi/r_ospi.c index 44072e5e0..a3f4056ad 100644 --- a/ra/fsp/src/r_ospi/r_ospi.c +++ b/ra/fsp/src/r_ospi/r_ospi.c @@ -652,6 +652,9 @@ static fsp_err_t r_ospi_spi_protocol_specific_settings (ospi_instance_ctrl_t * p /* Clear auto-calibration value */ R_OSPI->MDTR = OSPI_PRV_RMW_MASKED(R_OSPI->MDTR, R_OSPI_MDTR_DV0DEL_Msk, 0U, channel); + + /* Set read/write dummy clocks to 0 in SPI mode. */ + R_OSPI->MDLR = OSPI_PRV_RMW_MASKED(R_OSPI->MDLR, R_OSPI_MDLR_DV0RDL_Msk, 0U, channel); } return ret; diff --git a/ra/fsp/src/r_pdc/r_pdc.c b/ra/fsp/src/r_pdc/r_pdc.c index 298de0db3..a4259a818 100644 --- a/ra/fsp/src/r_pdc/r_pdc.c +++ b/ra/fsp/src/r_pdc/r_pdc.c @@ -132,7 +132,7 @@ const pdc_api_t g_pdc_on_pdc = * @retval FSP_ERR_ASSERTION One or more of the following parameters is NULL * 1. p_cfg is NULL * 2. p_api_ctrl is NULL - * 3 The pointer to the transfer interface in the p_cfg parameter is NULL + * 3. The pointer to the transfer interface in the p_cfg parameter is NULL * 4. Callback parameter is NULL. * 5. Invalid IRQ number assigned * @retval FSP_ERR_INVALID_ARGUMENT One or more of the following parameters is incorrect diff --git a/ra/fsp/src/r_poeg/r_poeg.c b/ra/fsp/src/r_poeg/r_poeg.c index 375137c6c..516e33b85 100644 --- a/ra/fsp/src/r_poeg/r_poeg.c +++ b/ra/fsp/src/r_poeg/r_poeg.c @@ -42,7 +42,7 @@ **********************************************************************************************************************/ #if defined(__ARMCC_VERSION) || defined(__ICCARM__) -typedef void (BSP_CMSE_NONSECURE_CALL * volatile poeg_prv_ns_callback)(poeg_callback_args_t * p_args); +typedef void (BSP_CMSE_NONSECURE_CALL * poeg_prv_ns_callback)(poeg_callback_args_t * p_args); #elif defined(__GNUC__) typedef BSP_CMSE_NONSECURE_CALL void (*volatile poeg_prv_ns_callback)(poeg_callback_args_t * p_args); #endif diff --git a/ra/fsp/src/r_qspi/r_qspi.c b/ra/fsp/src/r_qspi/r_qspi.c index 6da0443d2..9579707c6 100644 --- a/ra/fsp/src/r_qspi/r_qspi.c +++ b/ra/fsp/src/r_qspi/r_qspi.c @@ -108,7 +108,6 @@ static void(*const gp_qspi_prv_byte_write[3]) (uint8_t byte) = * Global Variables **********************************************************************************************************************/ -/*LDRA_INSPECTED 27 D This structure must be accessible in user code. It cannot be static. */ const spi_flash_api_t g_qspi_on_spi_flash = { .open = R_QSPI_Open, @@ -576,7 +575,10 @@ fsp_err_t R_QSPI_SpiProtocolSet (spi_flash_ctrl_t * p_ctrl, spi_flash_protocol_t #endif /* Update the SPI protocol. */ - R_QSPI->SFMSPC_b.SFMSPI = spi_protocol; + + /* The cast is used to prevent a warning that not all values of spi_flash_protocol_t fit in SFMSPI. spi_protocol is + * verified to fit in SFMSPI in parameter checking above. */ + R_QSPI->SFMSPC_b.SFMSPI = (uint32_t) spi_protocol & 3U; return FSP_SUCCESS; } diff --git a/ra/fsp/src/r_rtc/r_rtc.c b/ra/fsp/src/r_rtc/r_rtc.c index f8a2c6b64..23c3850aa 100644 --- a/ra/fsp/src/r_rtc/r_rtc.c +++ b/ra/fsp/src/r_rtc/r_rtc.c @@ -87,7 +87,7 @@ **********************************************************************************************************************/ #if defined(__ARMCC_VERSION) || defined(__ICCARM__) -typedef void (BSP_CMSE_NONSECURE_CALL * volatile rtc_prv_ns_callback)(rtc_callback_args_t * p_args); +typedef void (BSP_CMSE_NONSECURE_CALL * rtc_prv_ns_callback)(rtc_callback_args_t * p_args); #elif defined(__GNUC__) typedef BSP_CMSE_NONSECURE_CALL void (*volatile rtc_prv_ns_callback)(rtc_callback_args_t * p_args); #endif diff --git a/ra/fsp/src/r_sci_i2c/r_sci_i2c.c b/ra/fsp/src/r_sci_i2c/r_sci_i2c.c index 5066ddf4c..65ba5731a 100644 --- a/ra/fsp/src/r_sci_i2c/r_sci_i2c.c +++ b/ra/fsp/src/r_sci_i2c/r_sci_i2c.c @@ -112,7 +112,7 @@ typedef enum e_sci_i2c_dtc_interrupt_trigger } sci_i2c_dtc_interrupt_trigger_t; #if defined(__ARMCC_VERSION) || defined(__ICCARM__) -typedef void (BSP_CMSE_NONSECURE_CALL * volatile sci_i2c_prv_ns_callback)(i2c_master_callback_args_t * p_args); +typedef void (BSP_CMSE_NONSECURE_CALL * sci_i2c_prv_ns_callback)(i2c_master_callback_args_t * p_args); #elif defined(__GNUC__) typedef BSP_CMSE_NONSECURE_CALL void (*volatile sci_i2c_prv_ns_callback)(i2c_master_callback_args_t * p_args); #endif diff --git a/ra/fsp/src/r_sci_spi/r_sci_spi.c b/ra/fsp/src/r_sci_spi/r_sci_spi.c index c214d6f2c..2a43f5357 100644 --- a/ra/fsp/src/r_sci_spi/r_sci_spi.c +++ b/ra/fsp/src/r_sci_spi/r_sci_spi.c @@ -74,7 +74,7 @@ const spi_api_t g_spi_on_sci = **********************************************************************************************************************/ #if defined(__ARMCC_VERSION) || defined(__ICCARM__) -typedef void (BSP_CMSE_NONSECURE_CALL * volatile sci_spi_prv_ns_callback)(spi_callback_args_t * p_args); +typedef void (BSP_CMSE_NONSECURE_CALL * sci_spi_prv_ns_callback)(spi_callback_args_t * p_args); #elif defined(__GNUC__) typedef BSP_CMSE_NONSECURE_CALL void (*volatile sci_spi_prv_ns_callback)(spi_callback_args_t * p_args); #endif diff --git a/ra/fsp/src/r_sci_uart/r_sci_uart.c b/ra/fsp/src/r_sci_uart/r_sci_uart.c index 0f6c13708..d92825b8b 100644 --- a/ra/fsp/src/r_sci_uart/r_sci_uart.c +++ b/ra/fsp/src/r_sci_uart/r_sci_uart.c @@ -153,7 +153,7 @@ typedef enum e_noise_cancel_lvl } noise_cancel_lvl_t; #if defined(__ARMCC_VERSION) || defined(__ICCARM__) -typedef void (BSP_CMSE_NONSECURE_CALL * volatile sci_uart_prv_ns_callback)(uart_callback_args_t * p_args); +typedef void (BSP_CMSE_NONSECURE_CALL * sci_uart_prv_ns_callback)(uart_callback_args_t * p_args); #elif defined(__GNUC__) typedef BSP_CMSE_NONSECURE_CALL void (*volatile sci_uart_prv_ns_callback)(uart_callback_args_t * p_args); #endif diff --git a/ra/fsp/src/r_sdhi/r_sdhi.c b/ra/fsp/src/r_sdhi/r_sdhi.c index bf128b655..fb75bda6d 100644 --- a/ra/fsp/src/r_sdhi/r_sdhi.c +++ b/ra/fsp/src/r_sdhi/r_sdhi.c @@ -183,7 +183,7 @@ * Typedef definitions **********************************************************************************************************************/ #if defined(__ARMCC_VERSION) || defined(__ICCARM__) -typedef void (BSP_CMSE_NONSECURE_CALL * volatile sdhi_prv_ns_callback)(sdmmc_callback_args_t * p_args); +typedef void (BSP_CMSE_NONSECURE_CALL * sdhi_prv_ns_callback)(sdmmc_callback_args_t * p_args); #elif defined(__GNUC__) typedef BSP_CMSE_NONSECURE_CALL void (*volatile sdhi_prv_ns_callback)(sdmmc_callback_args_t * p_args); #endif diff --git a/ra/fsp/src/r_spi/r_spi.c b/ra/fsp/src/r_spi/r_spi.c index 4426f349e..31b792f88 100644 --- a/ra/fsp/src/r_spi/r_spi.c +++ b/ra/fsp/src/r_spi/r_spi.c @@ -80,7 +80,7 @@ * Typedef definitions **********************************************************************************************************************/ #if defined(__ARMCC_VERSION) || defined(__ICCARM__) -typedef void (BSP_CMSE_NONSECURE_CALL * volatile spi_prv_ns_callback)(spi_callback_args_t * p_args); +typedef void (BSP_CMSE_NONSECURE_CALL * spi_prv_ns_callback)(spi_callback_args_t * p_args); #elif defined(__GNUC__) typedef BSP_CMSE_NONSECURE_CALL void (*volatile spi_prv_ns_callback)(spi_callback_args_t * p_args); #endif diff --git a/ra/fsp/src/r_ssi/r_ssi.c b/ra/fsp/src/r_ssi/r_ssi.c index e07d9c8e8..422e8e552 100644 --- a/ra/fsp/src/r_ssi/r_ssi.c +++ b/ra/fsp/src/r_ssi/r_ssi.c @@ -79,7 +79,7 @@ **********************************************************************************************************************/ #if defined(__ARMCC_VERSION) || defined(__ICCARM__) -typedef void (BSP_CMSE_NONSECURE_CALL * volatile i2s_prv_ns_callback)(i2s_callback_args_t * p_args); +typedef void (BSP_CMSE_NONSECURE_CALL * i2s_prv_ns_callback)(i2s_callback_args_t * p_args); #elif defined(__GNUC__) typedef BSP_CMSE_NONSECURE_CALL void (*volatile i2s_prv_ns_callback)(i2s_callback_args_t * p_args); #endif @@ -160,7 +160,6 @@ static const fsp_version_t g_version = **********************************************************************************************************************/ /** SSI Implementation of I2S interface. */ -/*LDRA_INSPECTED 27 D This structure must be accessible in user code. It cannot be static. */ const i2s_api_t g_i2s_on_ssi = { .open = R_SSI_Open, @@ -1214,21 +1213,15 @@ void ssi_txi_isr (void) /* Clear the IR flag in the ICU */ R_BSP_IrqStatusClear(irq); - bool call_callback = true; - if (NULL != p_instance_ctrl->p_tx_src) { - /* If there is more data to send, don't call the callback. */ - if (p_instance_ctrl->tx_src_samples > 0) - { - call_callback = false; - } - /* If transfer is not used, write data. */ r_ssi_fifo_write(p_instance_ctrl); } - if (call_callback) + /* If there are more samples to write to the FIFO or the FIFO is above the watermark, don't call the callback. */ + if ((p_instance_ctrl->tx_src_samples == 0) && + (p_instance_ctrl->p_reg->SSIFSR_b.TDC > (BSP_FEATURE_SSI_FIFO_NUM_STAGES / 2U))) { r_ssi_call_callback(p_instance_ctrl, I2S_EVENT_TX_EMPTY); } diff --git a/ra/fsp/src/r_usb_basic/r_usb_basic.c b/ra/fsp/src/r_usb_basic/r_usb_basic.c index 2c66b5dac..cd1fd1560 100644 --- a/ra/fsp/src/r_usb_basic/r_usb_basic.c +++ b/ra/fsp/src/r_usb_basic/r_usb_basic.c @@ -566,15 +566,32 @@ fsp_err_t R_USB_Open (usb_ctrl_t * const p_api_ctrl, usb_cfg_t const * const p_c if (FSP_SUCCESS == err) { - g_usb_open_class[p_ctrl->module_number] |= (uint16_t) (1 << p_ctrl->type); /* Set USB Open device class */ - if (USB_CLASS_INTERNAL_PCDC == (usb_class_internal_t) p_ctrl->type) + if (USB_MODE_HOST == p_cfg->usb_mode) { - g_usb_open_class[p_ctrl->module_number] |= (1 << USB_CLASS_INTERNAL_PCDCC); /* Set USB Open device class */ + g_usb_open_class[p_ctrl->module_number] |= (uint16_t) (1 << p_ctrl->type); /* Set USB Open device class */ + if (USB_CLASS_INTERNAL_HCDC == (usb_class_internal_t) p_ctrl->type) + { + g_usb_open_class[p_ctrl->module_number] |= (1 << USB_CLASS_INTERNAL_HCDCC); /* Set USB Open device class */ + } } - - if (USB_CLASS_INTERNAL_HCDC == (usb_class_internal_t) p_ctrl->type) + else { - g_usb_open_class[p_ctrl->module_number] |= (1 << USB_CLASS_INTERNAL_HCDCC); /* Set USB Open device class */ +#if defined(USB_CFG_PCDC_USE) + g_usb_open_class[p_ctrl->module_number] |= (uint16_t) (1 << USB_CLASS_INTERNAL_PCDC); + g_usb_open_class[p_ctrl->module_number] |= (uint16_t) (1 << USB_CLASS_INTERNAL_PCDCC); + #if ((USB_CFG_PCDC_BULK_IN2 != USB_NULL) || (USB_CFG_PCDC_BULK_OUT2 != USB_NULL)) + g_usb_open_class[p_ctrl->module_number] |= (uint16_t) (1 << USB_CLASS_INTERNAL_PCDC2); + g_usb_open_class[p_ctrl->module_number] |= (uint16_t) (1 << USB_CLASS_INTERNAL_PCDCC2); + #endif /* ((USB_CFG_PCDC_BULK_IN2 != USB_NULL) || (USB_CFG_PCDC_BULK_OUT2 != USB_NULL)) */ +#endif /* defined(USB_CFG_PCDC_USE) */ + +#if defined(USB_CFG_PHID_USE) + g_usb_open_class[p_ctrl->module_number] |= (uint16_t) (1 << USB_CLASS_INTERNAL_PHID); +#endif /* defined(USB_CFG_PHID_USE) */ + +#if defined(USB_CFG_PMSC_USE) + g_usb_open_class[p_ctrl->module_number] |= (uint16_t) (1 << USB_CLASS_INTERNAL_PMSC); +#endif /* defined(USB_CFG_PMSC_USE) */ } } @@ -700,24 +717,8 @@ fsp_err_t R_USB_Close (usb_ctrl_t * const p_api_ctrl) #endif /* (USB_CFG_MODE & USB_CFG_PERI) == USB_CFG_PERI */ } - g_usb_open_class[p_ctrl->module_number] = - (uint16_t) (g_usb_open_class[p_ctrl->module_number] & (~(1 << p_ctrl->type))); /* Clear USB Open device class */ - -#if ((USB_CFG_MODE & USB_CFG_PERI) == USB_CFG_PERI) - if (USB_CLASS_INTERNAL_PCDC == (usb_class_internal_t) p_ctrl->type) - { - g_usb_open_class[p_ctrl->module_number] = (uint16_t) (g_usb_open_class[p_ctrl->module_number] & - (~(1 << USB_CLASS_INTERNAL_PCDCC))); /* Clear USB Open device class */ - } -#endif /* USB_CFG_MODE & USB_CFG_PERI) == USB_CFG_PERI */ + g_usb_open_class[p_ctrl->module_number] = 0; -#if ((USB_CFG_MODE & USB_CFG_HOST) == USB_CFG_HOST) - if (USB_CLASS_INTERNAL_HCDC == (usb_class_internal_t) p_ctrl->type) - { - g_usb_open_class[p_ctrl->module_number] = (uint16_t) (g_usb_open_class[p_ctrl->module_number] & - (~(1 << USB_CLASS_INTERNAL_HCDCC))); /* Clear USB Open device class */ - } -#endif /* (USB_CFG_MODE & USB_CFG_HOST) == USB_CFG_HOST */ #if defined(USB_CFG_PMSC_USE) ret_code = r_usb_pmsc_media_close(); #endif /* defined(USB_CFG_PMSC_USE) */ @@ -2152,6 +2153,7 @@ fsp_err_t R_USB_PipeInfoGet (usb_ctrl_t * const p_api_ctrl, usb_pipe_t * p_info, } pipe_type = usb_cstd_get_pipe_type(&utr, p_ctrl->pipe); + switch (pipe_type) { case USB_TYPFIELD_ISO: diff --git a/ra/fsp/src/r_usb_basic/src/driver/inc/r_usb_extern.h b/ra/fsp/src/r_usb_basic/src/driver/inc/r_usb_extern.h index 5dd310a17..0b44586aa 100644 --- a/ra/fsp/src/r_usb_basic/src/driver/inc/r_usb_extern.h +++ b/ra/fsp/src/r_usb_basic/src/driver/inc/r_usb_extern.h @@ -264,11 +264,11 @@ void usb_pstd_fifo_to_buf(uint16_t pipe, uint16_t useport, usb_utr_t * p_utr uint16_t usb_pstd_read_data(uint16_t pipe, uint16_t pipemode, usb_utr_t * p_utr); void usb_pstd_data_end(uint16_t pipe, uint16_t status, usb_utr_t * p_utr); -uint8_t usb_pstd_set_pipe_table(uint8_t * descriptor, usb_utr_t * p_utr); +uint8_t usb_pstd_set_pipe_table(uint8_t * descriptor, usb_utr_t * p_utr, uint8_t class_info); void usb_pstd_clr_pipe_table(uint8_t usb_ip); void usb_pstd_set_pipe_reg(usb_utr_t * p_utr); void usb_pstd_clr_pipe_reg(usb_utr_t * p_utr); -uint8_t usb_pstd_get_pipe_no(uint8_t type, uint8_t dir, usb_utr_t * p_utr); +uint8_t usb_pstd_get_pipe_no(uint8_t type, uint8_t dir, usb_utr_t * p_utr, uint8_t class_info); /* r_usb_cintfifo.c */ void usb_pstd_nrdy_pipe_process(usb_utr_t * p_utr, uint16_t bitsts); diff --git a/ra/fsp/src/r_usb_basic/src/driver/inc/r_usb_typedef.h b/ra/fsp/src/r_usb_basic/src/driver/inc/r_usb_typedef.h index 1bad293bb..895028bf2 100644 --- a/ra/fsp/src/r_usb_basic/src/driver/inc/r_usb_typedef.h +++ b/ra/fsp/src/r_usb_basic/src/driver/inc/r_usb_typedef.h @@ -229,6 +229,8 @@ typedef enum e_usb_class_internal { USB_CLASS_INTERNAL_PCDC = 0, ///< PCDC Class USB_CLASS_INTERNAL_PCDCC, ///< PCDCC Class + USB_CLASS_INTERNAL_PCDC2, ///< PCDC2 Class + USB_CLASS_INTERNAL_PCDCC2, ///< PCDCC2 Class USB_CLASS_INTERNAL_PHID, ///< PHID Class USB_CLASS_INTERNAL_PVND, ///< PVND Class USB_CLASS_INTERNAL_HCDC, ///< HCDC Class diff --git a/ra/fsp/src/r_usb_basic/src/driver/r_usb_cdataio.c b/ra/fsp/src/r_usb_basic/src/driver/r_usb_cdataio.c index 3311b8628..39e9654e1 100644 --- a/ra/fsp/src/r_usb_basic/src/driver/r_usb_cdataio.c +++ b/ra/fsp/src/r_usb_basic/src/driver/r_usb_cdataio.c @@ -113,17 +113,21 @@ static const uint8_t g_usb_pipe_peri[] = { /* OUT pipe */ /* IN pipe */ #if defined(USB_CFG_PCDC_USE) - USB_CFG_PCDC_BULK_OUT, USB_CFG_PCDC_BULK_IN, /* USB_PCDC */ - USB_NULL, USB_CFG_PCDC_INT_IN, /* USB_PCDCC */ - #else /* defined(USB_CFG_PCDC_USE) */ - USB_NULL, USB_NULL, - USB_NULL, USB_NULL, + USB_CFG_PCDC_BULK_OUT, USB_CFG_PCDC_BULK_IN, /* USB_PCDC */ + USB_NULL, USB_CFG_PCDC_INT_IN, /* USB_PCDCC */ + USB_CFG_PCDC_BULK_OUT2, USB_CFG_PCDC_BULK_IN2, /* USB_PCDC2 */ + USB_NULL, USB_CFG_PCDC_INT_IN2, /* USB_PCDCC2 */ + #else /* defined(USB_CFG_PCDC_USE) */ + USB_NULL, USB_NULL, + USB_NULL, USB_NULL, + USB_NULL, USB_NULL, + USB_NULL, USB_NULL, #endif /* defined(USB_CFG_PCDC_USE) */ #if defined(USB_CFG_PHID_USE) - USB_CFG_PHID_INT_OUT, USB_CFG_PHID_INT_IN, /* USB_PHID */ + USB_CFG_PHID_INT_OUT, USB_CFG_PHID_INT_IN, /* USB_PHID */ #else /* defined(USB_CFG_PHID_USE) */ - USB_NULL, USB_NULL, + USB_NULL, USB_NULL, #endif /* defined(USB_CFG_PHID_USE) */ }; #endif /* (USB_CFG_MODE & USB_CFG_PERI) == USB_CFG_PERI */ @@ -152,9 +156,13 @@ void (* g_usb_callback[])(usb_utr_t *, uint16_t, uint16_t) = #if defined(USB_CFG_PCDC_USE) usb_pcdc_read_complete, usb_pcdc_write_complete, /* USB_PCDC (0) */ USB_NULL, usb_pcdc_write_complete, /* USB_PCDCC (1) */ + usb_pcdc_read_complete, usb_pcdc_write_complete, /* USB_PCDC2 (0) */ + USB_NULL, usb_pcdc_write_complete, /* USB_PCDCC2 (1) */ #else USB_NULL, USB_NULL, /* USB_PCDC (0) */ USB_NULL, USB_NULL, /* USB_PCDCC (1) */ + USB_NULL, USB_NULL, /* USB_PCDC2 (0) */ + USB_NULL, USB_NULL, /* USB_PCDCC2 (1) */ #endif /* PHID */ diff --git a/ra/fsp/src/r_usb_basic/src/driver/r_usb_pdriver.c b/ra/fsp/src/r_usb_basic/src/driver/r_usb_pdriver.c index 68fa5aff0..3a097d0f0 100644 --- a/ra/fsp/src/r_usb_basic/src/driver/r_usb_pdriver.c +++ b/ra/fsp/src/r_usb_basic/src/driver/r_usb_pdriver.c @@ -1743,6 +1743,7 @@ uint16_t usb_peri_pipe_info (uint8_t * table, uint16_t speed, uint16_t length, u uint16_t ofdsc; uint16_t retval = USB_ERROR; uint8_t pipe_no; + uint8_t class_info = 0; FSP_PARAMETER_NOT_USED(speed); @@ -1752,11 +1753,16 @@ uint16_t usb_peri_pipe_info (uint8_t * table, uint16_t speed, uint16_t length, u /* WAIT_LOOP */ while (ofdsc < length) { + if (USB_DT_INTERFACE == table[ofdsc + USB_EP_B_DESCRIPTORTYPE]) + { + class_info = table[ofdsc + USB_IF_B_INTERFACECLASS]; + } + /* Endpoint Descriptor */ if (USB_DT_ENDPOINT == table[ofdsc + USB_EP_B_DESCRIPTORTYPE]) { /* EP Table pipe Information set */ - pipe_no = usb_pstd_set_pipe_table(&table[ofdsc], p_utr); + pipe_no = usb_pstd_set_pipe_table(&table[ofdsc], p_utr, class_info); if (USB_NULL != pipe_no) { retval = USB_OK; diff --git a/ra/fsp/src/r_usb_basic/src/driver/r_usb_plibusbip.c b/ra/fsp/src/r_usb_basic/src/driver/r_usb_plibusbip.c index 5e62730f1..330e9e135 100644 --- a/ra/fsp/src/r_usb_basic/src/driver/r_usb_plibusbip.c +++ b/ra/fsp/src/r_usb_basic/src/driver/r_usb_plibusbip.c @@ -1016,7 +1016,7 @@ void usb_pstd_bemp_pipe_process (usb_utr_t * p_utr, uint16_t bitsts) * Arguments : Address for Endpoint descriptor * Return value : Pipe no (USB_PIPE1->USB_PIPE9:OK, USB_NULL:Error) ******************************************************************************/ -uint8_t usb_pstd_set_pipe_table (uint8_t * descriptor, usb_utr_t * p_utr) +uint8_t usb_pstd_set_pipe_table (uint8_t * descriptor, usb_utr_t * p_utr, uint8_t class_info) { uint8_t pipe_no; uint16_t pipe_cfg; @@ -1041,13 +1041,13 @@ uint8_t usb_pstd_set_pipe_table (uint8_t * descriptor, usb_utr_t * p_utr) if (USB_EP_IN == (descriptor[USB_EP_B_ENDPOINTADDRESS] & USB_EP_DIRMASK)) { /* IN(send) */ - pipe_no = usb_pstd_get_pipe_no(USB_EP_BULK, USB_PIPE_DIR_IN, p_utr); + pipe_no = usb_pstd_get_pipe_no(USB_EP_BULK, USB_PIPE_DIR_IN, p_utr, class_info); pipe_cfg = (uint16_t) (USB_TYPFIELD_BULK | USB_CFG_DBLB | USB_DIR_P_IN); } else { /* OUT(receive) */ - pipe_no = usb_pstd_get_pipe_no(USB_EP_BULK, USB_PIPE_DIR_OUT, p_utr); + pipe_no = usb_pstd_get_pipe_no(USB_EP_BULK, USB_PIPE_DIR_OUT, p_utr, class_info); pipe_cfg = (uint16_t) (USB_TYPFIELD_BULK | USB_CFG_DBLB | USB_SHTNAKFIELD | USB_DIR_P_OUT); } @@ -1067,13 +1067,13 @@ uint8_t usb_pstd_set_pipe_table (uint8_t * descriptor, usb_utr_t * p_utr) if (USB_EP_IN == (descriptor[USB_EP_B_ENDPOINTADDRESS] & USB_EP_DIRMASK)) { /* IN(send) */ - pipe_no = usb_pstd_get_pipe_no(USB_EP_INT, USB_PIPE_DIR_IN, p_utr); + pipe_no = usb_pstd_get_pipe_no(USB_EP_INT, USB_PIPE_DIR_IN, p_utr, class_info); pipe_cfg = (uint16_t) (USB_TYPFIELD_INT | USB_DIR_P_IN); } else { /* OUT(receive) */ - pipe_no = usb_pstd_get_pipe_no(USB_EP_INT, USB_PIPE_DIR_OUT, p_utr); + pipe_no = usb_pstd_get_pipe_no(USB_EP_INT, USB_PIPE_DIR_OUT, p_utr, class_info); pipe_cfg = (uint16_t) (USB_TYPFIELD_INT | USB_DIR_P_OUT); } @@ -1227,93 +1227,148 @@ void usb_pstd_clr_pipe_reg (usb_utr_t * p_utr) * : uint8_t dir : (USB_PIPE_DIR_IN/USB_PIPE_DIR_OUT) * Return value : Pipe no (USB_PIPE1->USB_PIPE9:OK, USB_NULL:Error) ******************************************************************************/ -uint8_t usb_pstd_get_pipe_no (uint8_t type, uint8_t dir, usb_utr_t * p_utr) +uint8_t usb_pstd_get_pipe_no (uint8_t type, uint8_t dir, usb_utr_t * p_utr, uint8_t class_info) { uint8_t pipe_no = USB_NULL; (void) type; (void) dir; - (void) *p_utr; + (void) p_utr; + (void) class_info; #if defined(USB_CFG_PVND_USE) uint16_t pipe; #endif /* defined(USB_CFG_PVND_USE) */ #if defined(USB_CFG_PCDC_USE) - if (USB_EP_BULK == type) + if (USB_IFCLS_CDCD == class_info) { - if (USB_PIPE_DIR_IN == dir) - { - pipe_no = USB_CFG_PCDC_BULK_IN; - } - else + if (USB_EP_BULK == type) { - pipe_no = USB_CFG_PCDC_BULK_OUT; + if (USB_PIPE_DIR_IN == dir) + { + if (USB_FALSE == g_usb_pipe_table[p_utr->ip][USB_CFG_PCDC_BULK_IN].use_flag) + { + pipe_no = USB_CFG_PCDC_BULK_IN; /* Set Free pipe */ + } +#if (USB_NULL != USB_CFG_PCDC_BULK_IN2) + else if (USB_FALSE == g_usb_pipe_table[p_utr->ip][USB_CFG_PCDC_BULK_IN2].use_flag) + { + pipe_no = USB_CFG_PCDC_BULK_IN2; /* Set Free pipe */ + } + else + { + /* Error */ + } +#endif /* (USB_NULL != USB_CFG_PCDC_BULK_IN2) */ + } + else + { + if (USB_FALSE == g_usb_pipe_table[p_utr->ip][USB_CFG_PCDC_BULK_OUT].use_flag) + { + pipe_no = USB_CFG_PCDC_BULK_OUT; /* Set Free pipe */ + } +#if (USB_NULL != USB_CFG_PCDC_BULK_OUT2) + else if (USB_FALSE == g_usb_pipe_table[p_utr->ip][USB_CFG_PCDC_BULK_OUT2].use_flag) + { + pipe_no = USB_CFG_PCDC_BULK_OUT2; /* Set Free pipe */ + } + else + { + /* Error */ + } +#endif /* #if (USB_NULL != USB_CFG_PCDC_BULK_OUT2) */ + } } } - if (USB_EP_INT == type) + if (USB_IFCLS_CDCC == class_info) { - if (USB_PIPE_DIR_IN == dir) + if (USB_EP_INT == type) { - pipe_no = USB_CFG_PCDC_INT_IN; + if (USB_PIPE_DIR_IN == dir) + { + if (USB_FALSE == g_usb_pipe_table[p_utr->ip][USB_CFG_PCDC_INT_IN].use_flag) + { + pipe_no = USB_CFG_PCDC_INT_IN; /* Set Free pipe */ + } +#if (USB_NULL != USB_CFG_PCDC_INT_IN2) + else if (USB_FALSE == g_usb_pipe_table[p_utr->ip][USB_CFG_PCDC_INT_IN2].use_flag) + { + pipe_no = USB_CFG_PCDC_INT_IN2; /* Set Free pipe */ + } + else + { + /* Error */ + } +#endif /* #if (USB_NULL != USB_CFG_PCDC_INT_IN2) */ + } } } #endif /* defined(USB_CFG_PCDC_USE) */ #if defined(USB_CFG_PHID_USE) - if (USB_EP_INT == type) + if (USB_IFCLS_HID == class_info) { - if (USB_PIPE_DIR_IN == dir) - { - pipe_no = USB_CFG_PHID_INT_IN; - } - else + if (USB_EP_INT == type) { - pipe_no = USB_CFG_PHID_INT_OUT; + if (USB_PIPE_DIR_IN == dir) + { + pipe_no = USB_CFG_PHID_INT_IN; + } + else + { + pipe_no = USB_CFG_PHID_INT_OUT; + } } } #endif /* defined(USB_CFG_PHID_USE) */ #if defined(USB_CFG_PMSC_USE) - if (USB_EP_BULK == type) + if (USB_IFCLS_MAS == class_info) { - if (USB_PIPE_DIR_IN == dir) + if (USB_EP_BULK == type) { - pipe_no = USB_CFG_PMSC_BULK_IN; - } - else - { - pipe_no = USB_CFG_PMSC_BULK_OUT; + if (USB_PIPE_DIR_IN == dir) + { + pipe_no = USB_CFG_PMSC_BULK_IN; + } + else + { + pipe_no = USB_CFG_PMSC_BULK_OUT; + } } } #endif /* defined(USB_CFG_PMSC_USE) */ #if defined(USB_CFG_PVND_USE) - if (USB_EP_BULK == type) + if (USB_IFCLS_VEN == class_info) { - /* BULK PIPE Loop */ - /* WAIT_LOOP */ - for (pipe = USB_BULK_PIPE_START; pipe < (USB_BULK_PIPE_END + 1); pipe++) + if (USB_EP_BULK == type) { - /* Check Free pipe */ - if (USB_FALSE == g_usb_pipe_table[p_utr->ip][pipe].use_flag) + /* BULK PIPE Loop */ + /* WAIT_LOOP */ + for (pipe = USB_BULK_PIPE_START; pipe < (USB_BULK_PIPE_END + 1); pipe++) { - pipe_no = pipe; /* Set Free pipe */ - break; + /* Check Free pipe */ + if (USB_FALSE == g_usb_pipe_table[p_utr->ip][pipe].use_flag) + { + pipe_no = pipe; /* Set Free pipe */ + break; + } } } - } - if (USB_EP_INT == type) - { - /* Interrupt PIPE Loop */ - /* WAIT_LOOP */ - for (pipe = USB_INT_PIPE_START; pipe < (USB_INT_PIPE_END + 1); pipe++) + if (USB_EP_INT == type) { - /* Check Free pipe */ - if (USB_FALSE == g_usb_pipe_table[p_utr->ip][pipe].use_flag) + /* Interrupt PIPE Loop */ + /* WAIT_LOOP */ + for (pipe = USB_INT_PIPE_START; pipe < (USB_INT_PIPE_END + 1); pipe++) { - pipe_no = pipe; /* Set Free pipe */ - break; + /* Check Free pipe */ + if (USB_FALSE == g_usb_pipe_table[p_utr->ip][pipe].use_flag) + { + pipe_no = pipe; /* Set Free pipe */ + break; + } } } } diff --git a/ra/fsp/src/r_usb_basic/src/driver/r_usb_pstdrequest.c b/ra/fsp/src/r_usb_basic/src/driver/r_usb_pstdrequest.c index 1e92b3ddc..161189197 100644 --- a/ra/fsp/src/r_usb_basic/src/driver/r_usb_pstdrequest.c +++ b/ra/fsp/src/r_usb_basic/src/driver/r_usb_pstdrequest.c @@ -1471,11 +1471,7 @@ void usb_peri_class_request (usb_setup_t * preq, uint16_t ctsq, usb_utr_t * p_ut case USB_CS_WRDS: { - #if defined(USB_CFG_PMSC_USE) - usb_peri_class_request_wds(p_utr); /* class request (control write data stage) */ - #else /* defined(USB_CFG_PMSC_USE) */ usb_peri_class_request_rwds(preq, p_utr); /* class request (control write data stage) */ - #endif /* defined(USB_CFG_PMSC_USE) */ break; } @@ -1537,11 +1533,12 @@ void usb_peri_class_request_ioss (usb_setup_t * req) ******************************************************************************/ void usb_peri_class_request_rwds (usb_setup_t * req, usb_utr_t * p_utr) { - #if defined(USB_CFG_PMSC_USE) usb_instance_ctrl_t ctrl; + #if defined(USB_CFG_PMSC_USE) + /* Is a request receive target Interface? */ - if ((0 == req->request_index) && (USB_INTERFACE == (req->request_type & USB_BMREQUESTTYPERECIP))) + if (USB_INTERFACE == (req->request_type & USB_BMREQUESTTYPERECIP)) { if (USB_GET_MAX_LUN == (req->request_type & USB_BREQUEST)) { @@ -1549,7 +1546,7 @@ void usb_peri_class_request_rwds (usb_setup_t * req, usb_utr_t * p_utr) } else { - /* Get Line Coding Request */ + /* Get Line Coding Request etc */ ctrl.module_number = p_utr->ip; ctrl.setup = *req; /* Save setup data. */ ctrl.data_size = 0; @@ -1563,9 +1560,7 @@ void usb_peri_class_request_rwds (usb_setup_t * req, usb_utr_t * p_utr) /* Set Stall */ usb_pstd_set_stall_pipe0(p_utr); /* Req Error */ } - #else /* defined(USB_CFG_PMSC_USE) */ - usb_instance_ctrl_t ctrl; - + #else /* defined(USB_CFG_PMSC_USE) */ /* Is a request receive target Interface? */ ctrl.module_number = p_utr->ip; ctrl.setup = *req; /* Save setup data. */ @@ -1576,21 +1571,6 @@ void usb_peri_class_request_rwds (usb_setup_t * req, usb_utr_t * p_utr) #endif /* defined(USB_CFG_PMSC_USE) */ } /* End of function usb_peri_class_request_rwds */ - #if defined(USB_CFG_PMSC_USE) - -/****************************************************************************** - * Function Name : usb_peri_class_request_wds - * Description : Class request processing (control write data stage) - * Arguments : usb_utr_t *p_utr : Pointer to usb_utr_t structure. - * Return value : none - ******************************************************************************/ -void usb_peri_class_request_wds (usb_utr_t * p_utr) -{ - usb_pstd_set_stall_pipe0(p_utr); -} /* End of function usb_peri_class_request_wds */ - - #endif /* defined(USB_CFG_PMSC_USE) */ - /****************************************************************************** * Function Name : usb_peri_other_request * Description : Processing to notify the reception of the USB request @@ -1617,10 +1597,12 @@ void usb_peri_other_request (usb_setup_t * req, usb_utr_t * p_utr) ******************************************************************************/ void usb_peri_class_request_wnss (usb_setup_t * req, usb_utr_t * p_utr) { + usb_instance_ctrl_t ctrl; + #if defined(USB_CFG_PMSC_USE) /* Is a request receive target Interface? */ - if ((0 == req->request_index) && (USB_INTERFACE == (req->request_type & USB_BMREQUESTTYPERECIP))) + if (USB_INTERFACE == (req->request_type & USB_BMREQUESTTYPERECIP)) { if (USB_MASS_STORAGE_RESET == (req->request_type & USB_BREQUEST)) { @@ -1628,7 +1610,12 @@ void usb_peri_class_request_wnss (usb_setup_t * req, usb_utr_t * p_utr) } else { - usb_pstd_set_stall_pipe0(p_utr); /* Req Error */ + ctrl.setup = *req; /* Save setup data. */ + ctrl.module_number = p_utr->ip; + ctrl.data_size = 0; + ctrl.status = USB_SETUP_STATUS_ACK; + ctrl.type = USB_CLASS_REQUEST; + usb_set_event(USB_STATUS_REQUEST, &ctrl); } } else @@ -1640,11 +1627,9 @@ void usb_peri_class_request_wnss (usb_setup_t * req, usb_utr_t * p_utr) { usb_pstd_ctrl_end((uint16_t) USB_CTRL_END, p_utr); /* End control transfer. */ } - #else /* defined(USB_CFG_PMSC_USE) */ - usb_instance_ctrl_t ctrl; - + #else /* defined(USB_CFG_PMSC_USE) */ /* Is a request receive target Interface? */ - ctrl.setup = *req; /* Save setup data. */ + ctrl.setup = *req; /* Save setup data. */ ctrl.module_number = p_utr->ip; ctrl.data_size = 0; ctrl.status = USB_SETUP_STATUS_ACK; diff --git a/ra/fsp/src/r_usb_composite/r_usb_pcdc_pcdc_descriptor.c.template b/ra/fsp/src/r_usb_composite/r_usb_pcdc_pcdc_descriptor.c.template new file mode 100644 index 000000000..d3972f0eb --- /dev/null +++ b/ra/fsp/src/r_usb_composite/r_usb_pcdc_pcdc_descriptor.c.template @@ -0,0 +1,669 @@ +/* ${REA_DISCLAIMER_PLACEHOLDER} */ +/****************************************************************************** + Includes , "Project Includes" + ******************************************************************************/ +#include "r_usb_basic.h" +#include "r_usb_basic_api.h" + +/****************************************************************************** + Macro definitions + ******************************************************************************/ +/* bcdUSB */ +#define USB_BCDNUM (0x0200U) +/* Release Number */ +#define USB_RELEASE (0x0200U) +/* DCP max packet size */ +#define USB_DCPMAXP (64U) +/* Configuration number */ +#define USB_CONFIGNUM (1U) +/* Vendor ID */ +#define USB_VENDORID (0x0000U) +/* Product ID */ +#define USB_PRODUCTID (0x0002U) + +/* Miscellaneous Device Class */ +#define USB_MISC_CLASS (0xEF) +/* Common Class */ +#define USB_COMMON_CLASS (0x02) +/* Interface Association Descriptor */ +#define USB_IAD_DESC (0x01) +/* Interface Association Descriptor Type */ +#define USB_IAD_TYPE (0x0B) + + +/* Class-Specific Configuration Descriptors */ +#define USB_PCDC_CS_INTERFACE (0x24U) + + +/* bDescriptor SubType in Communications Class Functional Descriptors */ +/* Header Functional Descriptor */ +#define USB_PCDC_DT_SUBTYPE_HEADER_FUNC (0x00U) +/* Call Management Functional Descriptor. */ +#define USB_PCDC_DT_SUBTYPE_CALL_MANAGE_FUNC (0x01U) +/* Abstract Control Management Functional Descriptor. */ +#define USB_PCDC_DT_SUBTYPE_ABSTRACT_CTR_MANAGE_FUNC (0x02U) +/* Union Functional Descriptor */ +#define USB_PCDC_DT_SUBTYPE_UNION_FUNC (0x06U) + +/* Communications Class Subclass Codes */ +#define USB_PCDC_CLASS_SUBCLASS_CODE_ABS_CTR_MDL (0x02U) + +/* USB Class Definitions for Communications Devices Specification + release number in binary-coded decimal. */ +#define USB_PCDC_BCD_CDC (0x0110U) + +/* Descriptor length */ +#define USB_DD_BLENGTH (18U) +#define USB_DQD_LEN (10U) +#define USB_PCDC_PCDC_CD_LEN (141U) +#define STRING_DESCRIPTOR0_LEN (4U) +#define STRING_DESCRIPTOR1_LEN (16U) +#define STRING_DESCRIPTOR2_LEN (36U) +#define STRING_DESCRIPTOR3_LEN (36U) +#define STRING_DESCRIPTOR4_LEN (22U) +#define STRING_DESCRIPTOR5_LEN (18U) +#define STRING_DESCRIPTOR6_LEN (28U) +#define NUM_STRING_DESCRIPTOR (7U) + +/* Descriptor data Mask */ +#define USB_UCHAR_MAX (0xffU) +#define USB_W_TOTAL_LENGTH_MASK (256U) +#define USB_W_MAX_PACKET_SIZE_MASK (64U) +#define USB_PCDC_BCD_CDC_MASK (256U) + +/****************************************************************************** + Private global variables and functions + ******************************************************************************/ + +/****************************************************************************** + Exported global variables + ******************************************************************************/ + +/****************************************************************************** + Exported global functions (to be accessed by other files) + ******************************************************************************/ + +/* Standard Device Descriptor */ +uint8_t g_apl_device[USB_DD_BLENGTH + ( USB_DD_BLENGTH % 2)] = +{ + USB_DD_BLENGTH, /* 0:bLength */ + USB_DT_DEVICE, /* 1:bDescriptorType */ + (USB_BCDNUM & (uint8_t) USB_UCHAR_MAX), /* 2:bcdUSB_lo */ + ((uint8_t) (USB_BCDNUM >> 8) & (uint8_t) USB_UCHAR_MAX), /* 3:bcdUSB_hi */ + USB_MISC_CLASS, /* 4:bDeviceClass */ + USB_COMMON_CLASS, /* 5:bDeviceSubClass */ + USB_IAD_DESC, /* 6:bDeviceProtocol */ + (uint8_t) USB_DCPMAXP, /* 7:bMAXPacketSize(for DCP) */ + (USB_VENDORID & (uint8_t) USB_UCHAR_MAX), /* 8:idVendor_lo */ + ((uint8_t) (USB_VENDORID >> 8) & (uint8_t) USB_UCHAR_MAX), /* 9:idVendor_hi */ + ((uint16_t) USB_PRODUCTID & (uint8_t) USB_UCHAR_MAX), /* 10:idProduct_lo */ + ((uint8_t) (USB_PRODUCTID >> 8) & (uint8_t) USB_UCHAR_MAX), /* 11:idProduct_hi */ + (USB_RELEASE & (uint8_t) USB_UCHAR_MAX), /* 12:bcdDevice_lo */ + ((uint8_t) (USB_RELEASE >> 8) & (uint8_t) USB_UCHAR_MAX), /* 13:bcdDevice_hi */ + 1, /* 14:iManufacturer */ + 2, /* 15:iProduct */ + 6, /* 16:iSerialNumber */ + USB_CONFIGNUM /* 17:bNumConfigurations */ +}; + +/************************************************************ + * Device Qualifier Descriptor * + ************************************************************/ +uint8_t g_apl_qualifier_descriptor[USB_DQD_LEN + (USB_DQD_LEN % 2)] = +{ + USB_DQD_LEN, /* 0:bLength */ + USB_DT_DEVICE_QUALIFIER, /* 1:bDescriptorType */ + (USB_BCDNUM & (uint8_t) USB_UCHAR_MAX), /* 2:bcdUSB_lo */ + ((uint8_t) (USB_BCDNUM >> 8) & (uint8_t) USB_UCHAR_MAX), /* 3:bcdUSB_hi */ + 0, /* 4:bDeviceClass */ + 0, /* 5:bDeviceSubClass */ + 0, /* 6:bDeviceProtocol */ + (uint8_t) USB_DCPMAXP, /* 7:bMAXPacketSize(for DCP) */ + USB_CONFIGNUM, /* 8:bNumConfigurations */ + 0 /* 9:bReserved */ +}; + +/************************************************************ + * Configuration Or Other_Speed_Configuration Descriptor * + ************************************************************/ +/* For Full-Speed */ +uint8_t g_apl_configuration[USB_PCDC_PCDC_CD_LEN + (USB_PCDC_PCDC_CD_LEN % 2)] = +{ + USB_CD_BLENGTH, /* 0:bLength */ + USB_SOFT_CHANGE, /* 1:bDescriptorType */ + USB_PCDC_PCDC_CD_LEN % USB_W_TOTAL_LENGTH_MASK, /* 2:wTotalLength(L) */ + USB_PCDC_PCDC_CD_LEN / USB_W_TOTAL_LENGTH_MASK, /* 3:wTotalLength(H) */ + 4, /* 4:bNumInterfaces */ + 1, /* 5:bConfigurationValue */ + 0, /* 6:iConfiguration */ + USB_CF_RESERVED | USB_CF_SELFP, /* 7:bmAttributes */ + (10 / 2), /* 8:MAXPower (2mA unit) */ + + /* Interface Association Descriptor (IAD) */ + 0x08, /* 0:bLength */ + USB_IAD_TYPE, /* 1:bDescriptorType */ + 0x00, /* 2:bFirstInterface */ + 0x02, /* 3:bInterfaceCount */ + USB_IFCLS_CDCC, /* 4:bFunctionClass */ + USB_PCDC_CLASS_SUBCLASS_CODE_ABS_CTR_MDL, /* 5:bFunctionSubClass */ + 0x01, /* 6:bFunctionProtocol */ + 0x00, /* 7:iFunction */ + +/* Communication Device Class for channel 1 */ + + /* Interface Descriptor */ + USB_ID_BLENGTH, /* 0:bLength */ + USB_DT_INTERFACE, /* 1:bDescriptor */ + 0, /* 2:bInterfaceNumber */ + 0, /* 3:bAlternateSetting */ + 1, /* 4:bNumEndpoints */ + USB_IFCLS_CDCC, /* 5:bInterfaceClass */ + USB_PCDC_CLASS_SUBCLASS_CODE_ABS_CTR_MDL, /* 6:bInterfaceSubClass */ + 1, /* 7:bInterfaceProtocol */ + 0, /* 8:iInterface */ + + /* Communications Class Functional Descriptorss */ + 5, /* 0:bLength */ + USB_PCDC_CS_INTERFACE, /* 1:bDescriptorType */ + USB_PCDC_DT_SUBTYPE_HEADER_FUNC, /* 2:bDescriptorSubtype */ + USB_PCDC_BCD_CDC % USB_W_TOTAL_LENGTH_MASK, /* 3:bcdCDC_lo */ + USB_PCDC_BCD_CDC / USB_W_TOTAL_LENGTH_MASK, /* 4:bcdCDC_hi */ + + /* Communications Class Functional Descriptorss */ + 4, /* 0:bLength */ + USB_PCDC_CS_INTERFACE, /* 1:bDescriptorType */ + USB_PCDC_DT_SUBTYPE_ABSTRACT_CTR_MANAGE_FUNC, /* 2:bDescriptorSubtype */ + 2, /* 3:bmCapabilities */ + + /* Communications Class Functional Descriptorss */ + 5, /* 0:bLength */ + USB_PCDC_CS_INTERFACE, /* 1:bDescriptorType */ + USB_PCDC_DT_SUBTYPE_UNION_FUNC, /* 2:bDescriptorSubtype */ + 0, /* 3:bMasterInterface */ + 1, /* 4:bSlaveInterface0 */ + + /* Communications Class Functional Descriptorss */ + 5, /* 0:bLength */ + USB_PCDC_CS_INTERFACE, /* 1:bDescriptorType */ + USB_PCDC_DT_SUBTYPE_CALL_MANAGE_FUNC, /* 2:bDescriptorSubtype */ + /* D1:1-Device can send/receive call management + information over a Data Class interface. */ + /* D0:1-Device handles call management itself. */ + 3, /* 3:bmCapabilities */ + 1, /* 4:bDataInterface */ + + /* Endpoint Descriptor 0 */ + 7, /* 0:bLength */ + USB_DT_ENDPOINT, /* 1:bDescriptorType */ + USB_EP_IN | USB_EP3, /* 2:bEndpointAddress */ + USB_EP_INT, /* 3:bmAttribute */ + 16, /* 4:wMAXPacketSize_lo */ + 0, /* 5:wMAXPacketSize_hi */ + 0x10, /* 6:bInterval */ + + /* Interface Descriptor */ + USB_ID_BLENGTH, /* 0:bLength */ + USB_DT_INTERFACE, /* 1:bDescriptor */ + 1, /* 2:bInterfaceNumber */ + 0, /* 3:bAlternateSetting */ + 2, /* 4:bNumEndpoints */ + USB_IFCLS_CDCD, /* 5:bInterfaceClass */ + 0, /* 6:bInterfaceSubClass */ + 0, /* 7:bInterfaceProtocol */ + 0, /* 8:iInterface */ + + /* Endpoint Descriptor 0 */ + USB_ED_BLENGTH, /* 0:bLength */ + USB_DT_ENDPOINT, /* 1:bDescriptorType */ + USB_EP_IN | USB_EP1, /* 2:bEndpointAddress */ + USB_EP_BULK, /* 3:bmAttribute */ + USB_W_MAX_PACKET_SIZE_MASK, /* 4:wMAXPacketSize_lo */ + 0, /* 5:wMAXPacketSize_hi */ + 0, /* 6:bInterval */ + + /* Endpoint Descriptor 1 */ + USB_ED_BLENGTH, /* 0:bLength */ + USB_DT_ENDPOINT, /* 1:bDescriptorType */ + USB_EP_OUT | USB_EP2, /* 2:bEndpointAddress */ + USB_EP_BULK, /* 3:bmAttribute */ + USB_W_MAX_PACKET_SIZE_MASK, /* 4:wMAXPacketSize_lo */ + 0, /* 5:wMAXPacketSize_hi */ + 0, /* 6:bInterval */ + + /* Interface Association Descriptor (IAD) */ + 0x08, /* 0:bLength */ + USB_IAD_TYPE, /* 1:bDescriptorType */ + 0x02, /* 2:bFirstInterface */ + 0x02, /* 3:bInterfaceCount */ + USB_IFCLS_CDCC, /* 4:bFunctionClass */ + USB_PCDC_CLASS_SUBCLASS_CODE_ABS_CTR_MDL, /* 5:bFunctionSubClass */ + 0x01, /* 6:bFunctionProtocol */ + 0x00, /* 7:iFunction */ + +/* Communication Device Class for channel 2 */ + + /* Interface Descriptor */ + USB_ID_BLENGTH, /* 0:bLength */ + USB_DT_INTERFACE, /* 1:bDescriptor */ + 2, /* 2:bInterfaceNumber */ + 0, /* 3:bAlternateSetting */ + 1, /* 4:bNumEndpoints */ + USB_IFCLS_CDCC, /* 5:bInterfaceClass */ + USB_PCDC_CLASS_SUBCLASS_CODE_ABS_CTR_MDL, /* 6:bInterfaceSubClass */ + 1, /* 7:bInterfaceProtocol */ + 0, /* 8:iInterface */ + + /* Communications Class Functional Descriptorss */ + 5, /* 0:bLength */ + USB_PCDC_CS_INTERFACE, /* 1:bDescriptorType */ + USB_PCDC_DT_SUBTYPE_HEADER_FUNC, /* 2:bDescriptorSubtype */ + USB_PCDC_BCD_CDC % USB_W_TOTAL_LENGTH_MASK, /* 3:bcdCDC_lo */ + USB_PCDC_BCD_CDC / USB_W_TOTAL_LENGTH_MASK, /* 4:bcdCDC_hi */ + + /* Communications Class Functional Descriptorss */ + 4, /* 0:bLength */ + USB_PCDC_CS_INTERFACE, /* 1:bDescriptorType */ + USB_PCDC_DT_SUBTYPE_ABSTRACT_CTR_MANAGE_FUNC, /* 2:bDescriptorSubtype */ + 2, /* 3:bmCapabilities */ + + /* Communications Class Functional Descriptorss */ + 5, /* 0:bLength */ + USB_PCDC_CS_INTERFACE, /* 1:bDescriptorType */ + USB_PCDC_DT_SUBTYPE_UNION_FUNC, /* 2:bDescriptorSubtype */ + 0, /* 3:bMasterInterface */ + 1, /* 4:bSlaveInterface0 */ + + /* Communications Class Functional Descriptorss */ + 5, /* 0:bLength */ + USB_PCDC_CS_INTERFACE, /* 1:bDescriptorType */ + USB_PCDC_DT_SUBTYPE_CALL_MANAGE_FUNC, /* 2:bDescriptorSubtype */ + /* D1:1-Device can send/receive call management + information over a Data Class interface. */ + /* D0:1-Device handles call management itself. */ + 3, /* 3:bmCapabilities */ + 1, /* 4:bDataInterface */ + + /* Endpoint Descriptor 0 */ + 7, /* 0:bLength */ + USB_DT_ENDPOINT, /* 1:bDescriptorType */ + USB_EP_IN | USB_EP6, /* 2:bEndpointAddress */ + USB_EP_INT, /* 3:bmAttribute */ + 16, /* 4:wMAXPacketSize_lo */ + 0, /* 5:wMAXPacketSize_hi */ + 0x10, /* 6:bInterval */ + + /* Interface Descriptor */ + USB_ID_BLENGTH, /* 0:bLength */ + USB_DT_INTERFACE, /* 1:bDescriptor */ + 3, /* 2:bInterfaceNumber */ + 0, /* 3:bAlternateSetting */ + 2, /* 4:bNumEndpoints */ + USB_IFCLS_CDCD, /* 5:bInterfaceClass */ + 0, /* 6:bInterfaceSubClass */ + 0, /* 7:bInterfaceProtocol */ + 0, /* 8:iInterface */ + + /* Endpoint Descriptor 0 */ + USB_ED_BLENGTH, /* 0:bLength */ + USB_DT_ENDPOINT, /* 1:bDescriptorType */ + USB_EP_IN | USB_EP4, /* 2:bEndpointAddress */ + USB_EP_BULK, /* 3:bmAttribute */ + USB_W_MAX_PACKET_SIZE_MASK, /* 4:wMAXPacketSize_lo */ + 0, /* 5:wMAXPacketSize_hi */ + 0, /* 6:bInterval */ + + /* Endpoint Descriptor 1 */ + USB_ED_BLENGTH, /* 0:bLength */ + USB_DT_ENDPOINT, /* 1:bDescriptorType */ + USB_EP_OUT | USB_EP5, /* 2:bEndpointAddress */ + USB_EP_BULK, /* 3:bmAttribute */ + USB_W_MAX_PACKET_SIZE_MASK, /* 4:wMAXPacketSize_lo */ + 0, /* 5:wMAXPacketSize_hi */ + 0, /* 6:bInterval */ +}; + +/* For High-Speed */ +uint8_t g_apl_hs_configuration[USB_PCDC_PCDC_CD_LEN + (USB_PCDC_PCDC_CD_LEN % 2)] = +{ + 9, /* 0:bLength */ + USB_SOFT_CHANGE, /* 1:bDescriptorType */ + USB_PCDC_PCDC_CD_LEN % USB_W_TOTAL_LENGTH_MASK, /* 2:wTotalLength(L) */ + USB_PCDC_PCDC_CD_LEN / USB_W_TOTAL_LENGTH_MASK, /* 3:wTotalLength(H) */ + 4, /* 4:bNumInterfaces */ + 1, /* 5:bConfigurationValue */ + 0, /* 6:iConfiguration */ + USB_CF_RESERVED | USB_CF_SELFP, /* 7:bmAttributes */ + (10 / 2), /* 8:MAXPower (2mA unit) */ + + /* Interface Association Descriptor (IAD) */ + 0x08, /* 0:bLength */ + USB_IAD_TYPE, /* 1:bDescriptorType */ + 0x00, /* 2:bFirstInterface */ + 0x02, /* 3:bInterfaceCount */ + USB_IFCLS_CDCC, /* 4:bFunctionClass */ + USB_PCDC_CLASS_SUBCLASS_CODE_ABS_CTR_MDL, /* 5:bFunctionSubClass */ + 0x01, /* 6:bFunctionProtocol */ + 0x00, /* 7:iFunction */ + +/* Communication Device Class for channel 1 */ + + /* Interface Descriptor */ + 9, /* 0:bLength */ + USB_DT_INTERFACE, /* 1:bDescriptor */ + 0, /* 2:bInterfaceNumber */ + 0, /* 3:bAlternateSetting */ + 1, /* 4:bNumEndpoints */ + USB_IFCLS_CDCC, /* 5:bInterfaceClass */ + USB_PCDC_CLASS_SUBCLASS_CODE_ABS_CTR_MDL, /* 6:bInterfaceSubClass */ + 1, /* 7:bInterfaceProtocol */ + 0, /* 8:iInterface */ + + /* Communications Class Functional Descriptorss */ + 5, /* 0:bLength */ + USB_PCDC_CS_INTERFACE, /* 1:bDescriptorType */ + USB_PCDC_DT_SUBTYPE_HEADER_FUNC, /* 2:bDescriptorSubtype */ + USB_PCDC_BCD_CDC % USB_PCDC_BCD_CDC_MASK, /* 3:bcdCDC_lo */ + USB_PCDC_BCD_CDC / USB_PCDC_BCD_CDC_MASK, /* 4:bcdCDC_hi */ + + /* Communications Class Functional Descriptorss */ + 4, /* 0:bLength */ + USB_PCDC_CS_INTERFACE, /* 1:bDescriptorType */ + USB_PCDC_DT_SUBTYPE_ABSTRACT_CTR_MANAGE_FUNC, /* 2:bDescriptorSubtype */ + 2, /* 3:bmCapabilities */ + + /* Communications Class Functional Descriptorss */ + 5, /* 0:bLength */ + USB_PCDC_CS_INTERFACE, /* 1:bDescriptorType */ + USB_PCDC_DT_SUBTYPE_UNION_FUNC, /* 2:bDescriptorSubtype */ + 0, /* 3:bMasterInterface */ + 1, /* 4:bSlaveInterface0 */ + + /* Communications Class Functional Descriptorss */ + 5, /* 0:bLength */ + USB_PCDC_CS_INTERFACE, /* 1:bDescriptorType */ + USB_PCDC_DT_SUBTYPE_CALL_MANAGE_FUNC, /* 2:bDescriptorSubtype */ + /* D1:1-Device can send/receive call management + information over a Data Class interface. */ + /* D0:1-Device handles call management itself. */ + 3, /* 3:bmCapabilities */ + 1, /* 4:bDataInterface */ + + /* Endpoint Descriptor 0 */ + 7, /* 0:bLength */ + USB_DT_ENDPOINT, /* 1:bDescriptorType */ + USB_EP_IN | USB_EP3, /* 2:bEndpointAddress */ + USB_EP_INT, /* 3:bmAttribute */ + 16, /* 4:wMAXPacketSize_lo */ + 0, /* 5:wMAXPacketSize_hi */ + 0x10, /* 6:bInterval */ + + /* Interface Descriptor */ + 9, /* 0:bLength */ + USB_DT_INTERFACE, /* 1:bDescriptor */ + 1, /* 2:bInterfaceNumber */ + 0, /* 3:bAlternateSetting */ + 2, /* 4:bNumEndpoints */ + USB_IFCLS_CDCD, /* 5:bInterfaceClass */ + 0, /* 6:bInterfaceSubClass */ + 0, /* 7:bInterfaceProtocol */ + 0, /* 8:iInterface */ + + /* Endpoint Descriptor 0 */ + 7, /* 0:bLength */ + USB_DT_ENDPOINT, /* 1:bDescriptorType */ + USB_EP_IN | USB_EP1, /* 2:bEndpointAddress */ + USB_EP_BULK, /* 3:bmAttribute */ + 0, /* 4:wMAXPacketSize_lo */ + 2, /* 5:wMAXPacketSize_hi */ + 0, /* 6:bInterval */ + + /* Endpoint Descriptor 1 */ + 7, /* 0:bLength */ + USB_DT_ENDPOINT, /* 1:bDescriptorType */ + USB_EP_OUT | USB_EP2, /* 2:bEndpointAddress */ + USB_EP_BULK, /* 3:bmAttribute */ + 0, /* 4:wMAXPacketSize_lo */ + 2, /* 5:wMAXPacketSize_hi */ + 0, /* 6:bInterval */ + + /* Interface Association Descriptor (IAD) */ + 0x08, /* 0:bLength */ + USB_IAD_TYPE, /* 1:bDescriptorType */ + 0x02, /* 2:bFirstInterface */ + 0x02, /* 3:bInterfaceCount */ + USB_IFCLS_CDCC, /* 4:bFunctionClass */ + USB_PCDC_CLASS_SUBCLASS_CODE_ABS_CTR_MDL, /* 5:bFunctionSubClass */ + 0x01, /* 6:bFunctionProtocol */ + 0x00, /* 7:iFunction */ + +/* Communication Device Class for channel 2 */ + + /* Interface Descriptor */ + 9, /* 0:bLength */ + USB_DT_INTERFACE, /* 1:bDescriptor */ + 2, /* 2:bInterfaceNumber */ + 0, /* 3:bAlternateSetting */ + 1, /* 4:bNumEndpoints */ + USB_IFCLS_CDCC, /* 5:bInterfaceClass */ + USB_PCDC_CLASS_SUBCLASS_CODE_ABS_CTR_MDL, /* 6:bInterfaceSubClass */ + 1, /* 7:bInterfaceProtocol */ + 0, /* 8:iInterface */ + + /* Communications Class Functional Descriptorss */ + 5, /* 0:bLength */ + USB_PCDC_CS_INTERFACE, /* 1:bDescriptorType */ + USB_PCDC_DT_SUBTYPE_HEADER_FUNC, /* 2:bDescriptorSubtype */ + USB_PCDC_BCD_CDC % USB_PCDC_BCD_CDC_MASK, /* 3:bcdCDC_lo */ + USB_PCDC_BCD_CDC / USB_PCDC_BCD_CDC_MASK, /* 4:bcdCDC_hi */ + + /* Communications Class Functional Descriptorss */ + 4, /* 0:bLength */ + USB_PCDC_CS_INTERFACE, /* 1:bDescriptorType */ + USB_PCDC_DT_SUBTYPE_ABSTRACT_CTR_MANAGE_FUNC, /* 2:bDescriptorSubtype */ + 2, /* 3:bmCapabilities */ + + /* Communications Class Functional Descriptorss */ + 5, /* 0:bLength */ + USB_PCDC_CS_INTERFACE, /* 1:bDescriptorType */ + USB_PCDC_DT_SUBTYPE_UNION_FUNC, /* 2:bDescriptorSubtype */ + 0, /* 3:bMasterInterface */ + 1, /* 4:bSlaveInterface0 */ + + /* Communications Class Functional Descriptorss */ + 5, /* 0:bLength */ + USB_PCDC_CS_INTERFACE, /* 1:bDescriptorType */ + USB_PCDC_DT_SUBTYPE_CALL_MANAGE_FUNC, /* 2:bDescriptorSubtype */ + /* D1:1-Device can send/receive call management + information over a Data Class interface. */ + /* D0:1-Device handles call management itself. */ + 3, /* 3:bmCapabilities */ + 1, /* 4:bDataInterface */ + + /* Endpoint Descriptor 0 */ + 7, /* 0:bLength */ + USB_DT_ENDPOINT, /* 1:bDescriptorType */ + USB_EP_IN | USB_EP6, /* 2:bEndpointAddress */ + USB_EP_INT, /* 3:bmAttribute */ + 16, /* 4:wMAXPacketSize_lo */ + 0, /* 5:wMAXPacketSize_hi */ + 0x10, /* 6:bInterval */ + + /* Interface Descriptor */ + 9, /* 0:bLength */ + USB_DT_INTERFACE, /* 1:bDescriptor */ + 3, /* 2:bInterfaceNumber */ + 0, /* 3:bAlternateSetting */ + 2, /* 4:bNumEndpoints */ + USB_IFCLS_CDCD, /* 5:bInterfaceClass */ + 0, /* 6:bInterfaceSubClass */ + 0, /* 7:bInterfaceProtocol */ + 0, /* 8:iInterface */ + + /* Endpoint Descriptor 0 */ + 7, /* 0:bLength */ + USB_DT_ENDPOINT, /* 1:bDescriptorType */ + USB_EP_IN | USB_EP4, /* 2:bEndpointAddress */ + USB_EP_BULK, /* 3:bmAttribute */ + 0, /* 4:wMAXPacketSize_lo */ + 2, /* 5:wMAXPacketSize_hi */ + 0, /* 6:bInterval */ + + /* Endpoint Descriptor 1 */ + 7, /* 0:bLength */ + USB_DT_ENDPOINT, /* 1:bDescriptorType */ + USB_EP_OUT | USB_EP5, /* 2:bEndpointAddress */ + USB_EP_BULK, /* 3:bmAttribute */ + 0, /* 4:wMAXPacketSize_lo */ + 2, /* 5:wMAXPacketSize_hi */ + 0, /* 6:bInterval */ +}; + +/************************************* + * String Descriptor * + *************************************/ +/* UNICODE 0x0409 English (United States) */ +uint8_t g_apl_string_descriptor0[STRING_DESCRIPTOR0_LEN + ( STRING_DESCRIPTOR0_LEN % 2)] = +{ + STRING_DESCRIPTOR0_LEN, /* 0:bLength */ + USB_DT_STRING, /* 1:bDescriptorType */ + 0x09, 0x04 /* 2:wLANGID[0] */ +}; + +/* iManufacturer */ +uint8_t g_apl_string_descriptor1[STRING_DESCRIPTOR1_LEN + ( STRING_DESCRIPTOR1_LEN % 2)] = +{ + STRING_DESCRIPTOR1_LEN, /* 0:bLength */ + USB_DT_STRING, /* 1:bDescriptorType */ + 'R', 0x00, /* 2:wLANGID[0] */ + 'E', 0x00, + 'N', 0x00, + 'E', 0x00, + 'S', 0x00, + 'A', 0x00, + 'S', 0x00, +}; + +/* iProduct */ +uint8_t g_apl_string_descriptor2[STRING_DESCRIPTOR2_LEN + ( STRING_DESCRIPTOR2_LEN % 2)] = +{ + STRING_DESCRIPTOR2_LEN, /* 0:bLength */ + USB_DT_STRING, /* 1:bDescriptorType */ + 'U', 0x00, + 'S', 0x00, + 'B', 0x00, + ' ', 0x00, + 'D', 0x00, + 'e', 0x00, + 'm', 0x00, + 'o', 0x00, + 'n', 0x00, + 's', 0x00, + 't', 0x00, + 'r', 0x00, + 'a', 0x00, + 't', 0x00, + 'i', 0x00, + 'o', 0x00, + 'n', 0x00, +}; + +/* iInterface */ +uint8_t g_apl_string_descriptor3[STRING_DESCRIPTOR3_LEN + ( STRING_DESCRIPTOR3_LEN % 2)] = +{ + STRING_DESCRIPTOR3_LEN, /* 0:bLength */ + USB_DT_STRING, /* 1:bDescriptorType */ + 'C', 0x00, + 'o', 0x00, + 'm', 0x00, + 'p', 0x00, + 'o', 0x00, + 's', 0x00, + 'i', 0x00, + 't', 0x00, + 'e', 0x00, + ' ', 0x00, + 'D', 0x00, + 'e', 0x00, + 'v', 0x00, + 'i', 0x00, + 'c', 0x00, + 'e', 0x00, + 's', 0x00 +}; + +/* iConfiguration */ +uint8_t g_apl_string_descriptor4[STRING_DESCRIPTOR4_LEN + ( STRING_DESCRIPTOR4_LEN % 2)] = +{ + STRING_DESCRIPTOR4_LEN, /* 0:bLength */ + USB_DT_STRING, /* 1:bDescriptorType */ + 'F', 0x00, /* 2:wLANGID[0] */ + 'u', 0x00, + 'l', 0x00, + 'l', 0x00, + '-', 0x00, + 'S', 0x00, + 'p', 0x00, + 'e', 0x00, + 'e', 0x00, + 'd', 0x00 +}; + +/* iConfiguration */ +uint8_t g_apl_string_descriptor5[STRING_DESCRIPTOR5_LEN + ( STRING_DESCRIPTOR5_LEN % 2)] = +{ + STRING_DESCRIPTOR5_LEN, /* 0:bLength */ + USB_DT_STRING, /* 1:bDescriptorType */ + 'H', 0x00, /* 2:wLANGID[0] */ + 'i', 0x00, + '-', 0x00, + 'S', 0x00, + 'p', 0x00, + 'e', 0x00, + 'e', 0x00, + 'd', 0x00 +}; + +/* iSerialNumber */ +uint8_t g_apl_string_descriptor6[STRING_DESCRIPTOR6_LEN + ( STRING_DESCRIPTOR6_LEN % 2)] = +{ + STRING_DESCRIPTOR6_LEN, /* 0:bLength */ + USB_DT_STRING, /* 1:bDescriptorType */ + '0', 0x00, /* 2:wLANGID[0] */ + '0', 0x00, + '0', 0x00, + '0', 0x00, + '0', 0x00, + '0', 0x00, + '0', 0x00, + '0', 0x00, + '0', 0x00, + '0', 0x00, + '0', 0x00, + '0', 0x00, + '1', 0x00, +}; + +uint8_t *g_apl_string_table[] = +{ + g_apl_string_descriptor0, + g_apl_string_descriptor1, + g_apl_string_descriptor2, + g_apl_string_descriptor3, + g_apl_string_descriptor4, + g_apl_string_descriptor5, + g_apl_string_descriptor6 +}; + +const usb_descriptor_t g_usb_descriptor = +{ + g_apl_device, /* Pointer to the device descriptor */ + g_apl_configuration, /* Pointer to the configuration descriptor for Full-speed */ + g_apl_hs_configuration, /* Pointer to the configuration descriptor for Hi-speed */ + g_apl_qualifier_descriptor, /* Pointer to the qualifier descriptor */ + g_apl_string_table, /* Pointer to the string descriptor table */ + NUM_STRING_DESCRIPTOR +}; + +/****************************************************************************** + End Of File + ******************************************************************************/ diff --git a/ra/fsp/src/r_usb_composite/r_usb_pcdc_phid_descriptor.c.template b/ra/fsp/src/r_usb_composite/r_usb_pcdc_phid_descriptor.c.template new file mode 100644 index 000000000..50713b54d --- /dev/null +++ b/ra/fsp/src/r_usb_composite/r_usb_pcdc_phid_descriptor.c.template @@ -0,0 +1,450 @@ +/* ${REA_DISCLAIMER_PLACEHOLDER} */ +/****************************************************************************** + Includes , "Project Includes" + ******************************************************************************/ +#include "r_usb_basic.h" +#include "r_usb_basic_api.h" +#include "r_usb_phid_api.h" + +/****************************************************************************** + Macro definitions + ******************************************************************************/ +/* bcdUSB */ +#define USB_BCDNUM (0x0200U) +/* Release Number */ +#define USB_RELEASE (0x0200U) +/* DCP max packet size */ +#define USB_DCPMAXP (64U) +/* Configuration number */ +#define USB_CONFIGNUM (1U) +/* Vendor ID */ +#define USB_VENDORID (0x0000U) +/* Product ID */ +#define USB_PRODUCTID (0x0002U) + +/* Miscellaneous Device Class */ +#define USB_MISC_CLASS (0xEF) +/* Common Class */ +#define USB_COMMON_CLASS (0x02) +/* Interface Association Descriptor */ +#define USB_IAD_DESC (0x01) +/* Interface Association Descriptor Type */ +#define USB_IAD_TYPE (0x0B) + + +/* Class-Specific Configuration Descriptors */ +#define USB_PCDC_CS_INTERFACE (0x24U) + +/* bDescriptor SubType in Communications Class Functional Descriptors */ +/* Header Functional Descriptor */ +#define USB_PCDC_DT_SUBTYPE_HEADER_FUNC (0x00U) +/* Call Management Functional Descriptor. */ +#define USB_PCDC_DT_SUBTYPE_CALL_MANAGE_FUNC (0x01U) +/* Abstract Control Management Functional Descriptor. */ +#define USB_PCDC_DT_SUBTYPE_ABSTRACT_CTR_MANAGE_FUNC (0x02U) +/* Union Functional Descriptor */ +#define USB_PCDC_DT_SUBTYPE_UNION_FUNC (0x06U) + +/* Communications Class Subclass Codes */ +#define USB_PCDC_CLASS_SUBCLASS_CODE_ABS_CTR_MDL (0x02U) + +#define USB_IFPROTOCOL (USB_IFPRO_KBD) +#define ITEM_LEN (76U) +#define HID_MXPS (8U) +#define NUM_EP (1U) + +/* USB Class Definitions for Communications Devices Specification + release number in binary-coded decimal. */ +#define USB_PCDC_BCD_CDC (0x0110U) + +/* Descriptor length */ +#define USB_PCDC_QD_LEN (10U) +#define USB_DD_BLENGTH (18U) +#define USB_PCDC_PHID_CD_LEN (100U) +#define STRING_DESCRIPTOR0_LEN (4U) +#define STRING_DESCRIPTOR1_LEN (16U) +#define STRING_DESCRIPTOR2_LEN (36U) +#define STRING_DESCRIPTOR3_LEN (36U) +#define STRING_DESCRIPTOR4_LEN (22U) +#define STRING_DESCRIPTOR5_LEN (18U) +#define STRING_DESCRIPTOR6_LEN (28U) +#define NUM_STRING_DESCRIPTOR (7U) + +/* Descriptor data Mask */ +#define USB_UCHAR_MAX (0xffU) +#define USB_W_TOTAL_LENGTH_MASK (256U) +#define USB_W_MAX_PACKET_SIZE_MASK (64U) +#define USB_PCDC_BCD_CDC_MASK (256U) + +/****************************************************************************** + Private global variables and functions + ******************************************************************************/ + +/****************************************************************************** + Exported global variables + ******************************************************************************/ + +/****************************************************************************** + Exported global functions (to be accessed by other files) + ******************************************************************************/ + +/* Standard Device Descriptor */ +uint8_t g_apl_device[USB_DD_BLENGTH + ( USB_DD_BLENGTH % 2)] = +{ + USB_DD_BLENGTH, /* 0:bLength */ + USB_DT_DEVICE, /* 1:bDescriptorType */ + (USB_BCDNUM & (uint8_t) USB_UCHAR_MAX), /* 2:bcdUSB_lo */ + ((uint8_t) (USB_BCDNUM >> 8) & (uint8_t) USB_UCHAR_MAX), /* 3:bcdUSB_hi */ + USB_MISC_CLASS, /* 4:bDeviceClass */ + USB_COMMON_CLASS, /* 5:bDeviceSubClass */ + USB_IAD_DESC, /* 6:bDeviceProtocol */ + (uint8_t) USB_DCPMAXP, /* 7:bMAXPacketSize(for DCP) */ + (USB_VENDORID & (uint8_t) USB_UCHAR_MAX), /* 8:idVendor_lo */ + ((uint8_t) (USB_VENDORID >> 8) & (uint8_t) USB_UCHAR_MAX), /* 9:idVendor_hi */ + ((uint16_t) USB_PRODUCTID & (uint8_t) USB_UCHAR_MAX), /* 10:idProduct_lo */ + ((uint8_t) (USB_PRODUCTID >> 8) & (uint8_t) USB_UCHAR_MAX), /* 11:idProduct_hi */ + (USB_RELEASE & (uint8_t) USB_UCHAR_MAX), /* 12:bcdDevice_lo */ + ((uint8_t) (USB_RELEASE >> 8) & (uint8_t) USB_UCHAR_MAX), /* 13:bcdDevice_hi */ + 1, /* 14:iManufacturer */ + 2, /* 15:iProduct */ + 6, /* 16:iSerialNumber */ + USB_CONFIGNUM /* 17:bNumConfigurations */ +}; + +/************************************************************ + * Configuration Or Other_Speed_Configuration Descriptor * + ************************************************************/ +/* For Full-Speed */ +uint8_t g_apl_configuration[USB_PCDC_PHID_CD_LEN + (USB_PCDC_PHID_CD_LEN % 2)] = +{ + USB_CD_BLENGTH, /* 0:bLength */ + USB_SOFT_CHANGE, /* 1:bDescriptorType */ + USB_PCDC_PHID_CD_LEN % USB_W_TOTAL_LENGTH_MASK, /* 2:wTotalLength(L) */ + USB_PCDC_PHID_CD_LEN / USB_W_TOTAL_LENGTH_MASK, /* 3:wTotalLength(H) */ + 3, /* 4:bNumInterfaces */ + 1, /* 5:bConfigurationValue */ + 0, /* 6:iConfiguration */ + USB_CF_RESERVED | USB_CF_SELFP, /* 7:bmAttributes */ + (10 / 2), /* 8:MAXPower (2mA unit) */ + +/* Communication Device Class */ + + /* Interface Association Descriptor (IAD) */ + 0x08, /* 0:bLength */ + USB_IAD_TYPE, /* 1:bDescriptorType */ + 0x00, /* 2:bFirstInterface */ + 0x02, /* 3:bInterfaceCount */ + USB_IFCLS_CDCC, /* 4:bFunctionClass */ + USB_PCDC_CLASS_SUBCLASS_CODE_ABS_CTR_MDL, /* 5:bFunctionSubClass */ + 0x01, /* 6:bFunctionProtocol */ + 0x00, /* 7:iFunction */ + + /* Interface Descriptor */ + USB_ID_BLENGTH, /* 0:bLength */ + USB_DT_INTERFACE, /* 1:bDescriptor */ + 0, /* 2:bInterfaceNumber */ + 0, /* 3:bAlternateSetting */ + 1, /* 4:bNumEndpoints */ + USB_IFCLS_CDCC, /* 5:bInterfaceClass */ + USB_PCDC_CLASS_SUBCLASS_CODE_ABS_CTR_MDL, /* 6:bInterfaceSubClass */ + 1, /* 7:bInterfaceProtocol */ + 0, /* 8:iInterface */ + + /* Communications Class Functional Descriptorss */ + 5, /* 0:bLength */ + USB_PCDC_CS_INTERFACE, /* 1:bDescriptorType */ + USB_PCDC_DT_SUBTYPE_HEADER_FUNC, /* 2:bDescriptorSubtype */ + USB_PCDC_BCD_CDC % USB_W_TOTAL_LENGTH_MASK, /* 3:bcdCDC_lo */ + USB_PCDC_BCD_CDC / USB_W_TOTAL_LENGTH_MASK, /* 4:bcdCDC_hi */ + + /* Communications Class Functional Descriptorss */ + 4, /* 0:bLength */ + USB_PCDC_CS_INTERFACE, /* 1:bDescriptorType */ + USB_PCDC_DT_SUBTYPE_ABSTRACT_CTR_MANAGE_FUNC, /* 2:bDescriptorSubtype */ + 2, /* 3:bmCapabilities */ + + /* Communications Class Functional Descriptorss */ + 5, /* 0:bLength */ + USB_PCDC_CS_INTERFACE, /* 1:bDescriptorType */ + USB_PCDC_DT_SUBTYPE_UNION_FUNC, /* 2:bDescriptorSubtype */ + 0, /* 3:bMasterInterface */ + 1, /* 4:bSlaveInterface0 */ + + /* Communications Class Functional Descriptorss */ + 5, /* 0:bLength */ + USB_PCDC_CS_INTERFACE, /* 1:bDescriptorType */ + USB_PCDC_DT_SUBTYPE_CALL_MANAGE_FUNC, /* 2:bDescriptorSubtype */ + /* D1:1-Device can send/receive call management + information over a Data Class interface. */ + /* D0:1-Device handles call management itself. */ + 3, /* 3:bmCapabilities */ + 1, /* 4:bDataInterface */ + + /* Endpoint Descriptor 0 */ + 7, /* 0:bLength */ + USB_DT_ENDPOINT, /* 1:bDescriptorType */ + USB_EP_IN | USB_EP3, /* 2:bEndpointAddress */ + USB_EP_INT, /* 3:bmAttribute */ + 16, /* 4:wMAXPacketSize_lo */ + 0, /* 5:wMAXPacketSize_hi */ + 0x10, /* 6:bInterval */ + + /* Interface Descriptor */ + USB_ID_BLENGTH, /* 0:bLength */ + USB_DT_INTERFACE, /* 1:bDescriptor */ + 1, /* 2:bInterfaceNumber */ + 0, /* 3:bAlternateSetting */ + 2, /* 4:bNumEndpoints */ + USB_IFCLS_CDCD, /* 5:bInterfaceClass */ + 0, /* 6:bInterfaceSubClass */ + 0, /* 7:bInterfaceProtocol */ + 0, /* 8:iInterface */ + + /* Endpoint Descriptor 0 */ + USB_ED_BLENGTH, /* 0:bLength */ + USB_DT_ENDPOINT, /* 1:bDescriptorType */ + USB_EP_IN | USB_EP1, /* 2:bEndpointAddress */ + USB_EP_BULK, /* 3:bmAttribute */ + USB_W_MAX_PACKET_SIZE_MASK, /* 4:wMAXPacketSize_lo */ + 0, /* 5:wMAXPacketSize_hi */ + 0, /* 6:bInterval */ + + /* Endpoint Descriptor 1 */ + USB_ED_BLENGTH, /* 0:bLength */ + USB_DT_ENDPOINT, /* 1:bDescriptorType */ + USB_EP_OUT | USB_EP2, /* 2:bEndpointAddress */ + USB_EP_BULK, /* 3:bmAttribute */ + USB_W_MAX_PACKET_SIZE_MASK, /* 4:wMAXPacketSize_lo */ + 0, /* 5:wMAXPacketSize_hi */ + 0, /* 6:bInterval */ + +/* Human Interface Device Class */ + + /* Interface Descriptor */ + USB_ID_BLENGTH, /* 0:bLength */ + USB_DT_INTERFACE, /* 1:bDescriptor */ + 2, /* 2:bInterfaceNumber */ + 0, /* 3:bAlternateSetting */ + NUM_EP, /* 4:bNumEndpoints */ + USB_IFCLS_HID, /* 5:bInterfaceClass(HID) */ + USB_IFSUB_NOBOOT, /* 6:bInterfaceSubClass(NonBOOT) */ + USB_IFPROTOCOL, /* 7:bInterfaceProtocol */ + 0, /* 8:iInterface */ + + /* HID Descriptor */ + 9, /* 0:bLength */ + USB_DT_TYPE_HIDDESCRIPTOR, /* 1:bDescriptor */ + 0x00, /* 2:HID Ver */ + 0x01, /* 3:HID Ver */ + 0x00, /* 4:bCountryCode */ + 0x01, /* 5:bNumDescriptors */ + 0x22, /* 6:bDescriptorType */ + ITEM_LEN, /* 7:wItemLength(L) */ + 0x00, /* 8:wItemLength(H) */ + + /* Endpoint Descriptor 0 */ + USB_ED_BLENGTH, /* 0:bLength */ + USB_DT_ENDPOINT, /* 1:bDescriptorType */ + (uint8_t) (USB_EP_IN | USB_EP4), /* 2:bEndpointAddress */ + USB_EP_INT, /* 3:bmAttribute */ + HID_MXPS, /* 4:wMaxPacketSize_lo */ + 0, /* 5:wMaxPacketSize_hi */ + 0x0A, /* 6:bInterval */ +}; + +/************************************* + * String Descriptor * + *************************************/ +/* UNICODE 0x0409 English (United States) */ +uint8_t g_apl_string_descriptor0[STRING_DESCRIPTOR0_LEN + ( STRING_DESCRIPTOR0_LEN % 2)] = +{ + STRING_DESCRIPTOR0_LEN, /* 0:bLength */ + USB_DT_STRING, /* 1:bDescriptorType */ + 0x09, 0x04 /* 2:wLANGID[0] */ +}; + +/* iManufacturer */ +uint8_t g_apl_string_descriptor1[STRING_DESCRIPTOR1_LEN + ( STRING_DESCRIPTOR1_LEN % 2)] = +{ + STRING_DESCRIPTOR1_LEN, /* 0:bLength */ + USB_DT_STRING, /* 1:bDescriptorType */ + 'R', 0x00, /* 2:wLANGID[0] */ + 'E', 0x00, + 'N', 0x00, + 'E', 0x00, + 'S', 0x00, + 'A', 0x00, + 'S', 0x00, +}; + +/* iProduct */ +uint8_t g_apl_string_descriptor2[STRING_DESCRIPTOR2_LEN + ( STRING_DESCRIPTOR2_LEN % 2)] = +{ + STRING_DESCRIPTOR2_LEN, /* 0:bLength */ + USB_DT_STRING, /* 1:bDescriptorType */ + 'U', 0x00, + 'S', 0x00, + 'B', 0x00, + ' ', 0x00, + 'D', 0x00, + 'e', 0x00, + 'm', 0x00, + 'o', 0x00, + 'n', 0x00, + 's', 0x00, + 't', 0x00, + 'r', 0x00, + 'a', 0x00, + 't', 0x00, + 'i', 0x00, + 'o', 0x00, + 'n', 0x00, +}; + +/* iInterface */ +uint8_t g_apl_string_descriptor3[STRING_DESCRIPTOR3_LEN + ( STRING_DESCRIPTOR3_LEN % 2)] = +{ + STRING_DESCRIPTOR3_LEN, /* 0:bLength */ + USB_DT_STRING, /* 1:bDescriptorType */ + 'C', 0x00, + 'o', 0x00, + 'm', 0x00, + 'p', 0x00, + 'o', 0x00, + 's', 0x00, + 'i', 0x00, + 't', 0x00, + 'e', 0x00, + ' ', 0x00, + 'D', 0x00, + 'e', 0x00, + 'v', 0x00, + 'i', 0x00, + 'c', 0x00, + 'e', 0x00, + 's', 0x00 +}; + +/* iConfiguration */ +uint8_t g_apl_string_descriptor4[STRING_DESCRIPTOR4_LEN + ( STRING_DESCRIPTOR4_LEN % 2)] = +{ + STRING_DESCRIPTOR4_LEN, /* 0:bLength */ + USB_DT_STRING, /* 1:bDescriptorType */ + 'F', 0x00, /* 2:wLANGID[0] */ + 'u', 0x00, + 'l', 0x00, + 'l', 0x00, + '-', 0x00, + 'S', 0x00, + 'p', 0x00, + 'e', 0x00, + 'e', 0x00, + 'd', 0x00 +}; + +/* iConfiguration */ +uint8_t g_apl_string_descriptor5[STRING_DESCRIPTOR5_LEN + ( STRING_DESCRIPTOR5_LEN % 2)] = +{ + STRING_DESCRIPTOR5_LEN, /* 0:bLength */ + USB_DT_STRING, /* 1:bDescriptorType */ + 'H', 0x00, /* 2:wLANGID[0] */ + 'i', 0x00, + '-', 0x00, + 'S', 0x00, + 'p', 0x00, + 'e', 0x00, + 'e', 0x00, + 'd', 0x00 +}; + +/* iSerialNumber */ +uint8_t g_apl_string_descriptor6[STRING_DESCRIPTOR6_LEN + ( STRING_DESCRIPTOR6_LEN % 2)] = +{ + STRING_DESCRIPTOR6_LEN, /* 0:bLength */ + USB_DT_STRING, /* 1:bDescriptorType */ + '0', 0x00, /* 2:wLANGID[0] */ + '0', 0x00, + '0', 0x00, + '0', 0x00, + '0', 0x00, + '0', 0x00, + '0', 0x00, + '0', 0x00, + '0', 0x00, + '0', 0x00, + '0', 0x00, + '0', 0x00, + '1', 0x00, +}; + +uint8_t *g_apl_string_table[] = +{ + g_apl_string_descriptor0, + g_apl_string_descriptor1, + g_apl_string_descriptor2, + g_apl_string_descriptor3, + g_apl_string_descriptor4, + g_apl_string_descriptor5, + g_apl_string_descriptor6 +}; + +/************************************************************ + * HID Report Discriptor for Keyboard * + ************************************************************/ +const uint8_t g_apl_report[] = +{ + 0x05, 0x01, /* Usage Page (Generic Desktop) */ + 0x09, 0x06, /* Usage (Keyboard) */ + 0xA1, 0x01, /* Collection (Application) */ + 0x05, 0x07, /* Usage Page (Key Codes); */ + 0x19, 0xE0, /* Usage Minimum (224) */ + 0x29, 0xE7, /* Usage Maximum (231) */ + 0x15, 0x00, /* Logical Minimum (0) */ + 0x25, 0x01, /* Logical Maximum (1) */ + 0x75, 0x01, /* Report Size (1) */ + 0x95, 0x08, /* Report Count (8) */ + 0x81, 0x02, /* Input (Data , Variable , Absolute) ;Modifier byte */ + 0x95, 0x01, /* Report Count (1) */ + 0x75, 0x08, /* Report Size (8) */ + 0x81, 0x01, /* Input (Constant) ;Reserved byte */ + 0x95, 0x05, /* Report Count (5) */ + 0x75, 0x01, /* Report Size (1) */ + 0x05, 0x08, /* Usage Page (Page# for LEDs) */ + 0x19, 0x01, /* Usage Minimum (1) */ + 0x29, 0x05, /* Usage Maximum (5) */ + 0x91, 0x02, /* Output (Data , Variable , Absolute) ;LED report */ + 0x95, 0x01, /* Report Count (1) */ + 0x75, 0x03, /* Report Size (3) */ + 0x91, 0x01, /* Output (Constant) ;LED report padding */ + 0x95, 0x06, /* Report Count (6) */ + 0x75, 0x08, /* Report Size (8) */ + 0x15, 0x00, /* Logical Minimum (0) */ + 0x25, 0x65, /* Logical Maximum(101) */ + 0x05, 0x07, /* Usage Page (Key Codes) */ + 0x19, 0x00, /* Usage Minimum (0) */ + 0x29, 0x65, /* Usage Maximum (101) */ + 0x81, 0x00, /* Input (Data , Array) ;Key arrays (6 bytes) */ + + /* *** The OUTPUR REPORT *** */ + 0x09, 0x00, /* Usage ID within this page (Vendor defined)*/ + 0x15, 0x00, /* Logical Min 0 */ + 0x26, USB_UCHAR_MAX, 0x00, /* Logical Max 255 */ + 0x75, 0x08, /* Size 8 Bits (Each Field will be 8bits) */ + 0x95, 0x01, /* Count (Number of fields(bytes) in OUTPUT report) */ + 0x91, 0x02, /* Output Report - type variable data */ + 0xC0, /* End Collection */ +}; + +const usb_descriptor_t g_usb_descriptor = +{ + g_apl_device, /* Pointer to the device descriptor */ + g_apl_configuration, /* Pointer to the configuration descriptor for Full-speed */ + NULL, /* Pointer to the configuration descriptor for Hi-speed */ + NULL, /* Pointer to the qualifier descriptor */ + g_apl_string_table, /* Pointer to the string descriptor table */ + NUM_STRING_DESCRIPTOR +}; + +/****************************************************************************** + End Of File + ******************************************************************************/ diff --git a/ra/fsp/src/r_usb_composite/r_usb_pcdc_pmsc_descriptor.c.template b/ra/fsp/src/r_usb_composite/r_usb_pcdc_pmsc_descriptor.c.template new file mode 100644 index 000000000..95a8b03e4 --- /dev/null +++ b/ra/fsp/src/r_usb_composite/r_usb_pcdc_pmsc_descriptor.c.template @@ -0,0 +1,562 @@ +/* ${REA_DISCLAIMER_PLACEHOLDER} */ +/****************************************************************************** + Includes , "Project Includes" + ******************************************************************************/ +#include "r_usb_basic.h" +#include "r_usb_basic_api.h" + +/****************************************************************************** + Macro definitions + ******************************************************************************/ +/* bcdUSB */ +#define USB_BCDNUM (0x0200U) +/* Release Number */ +#define USB_RELEASE (0x0200U) +/* DCP max packet size */ +#define USB_DCPMAXP (64U) +/* Configuration number */ +#define USB_CONFIGNUM (1U) +/* Vendor ID */ +#define USB_VENDORID (0x0000U) +/* Product ID */ +#define USB_PRODUCTID (0x0002U) + +/* Miscellaneous Device Class */ +#define USB_MISC_CLASS (0xEF) +/* Common Class */ +#define USB_COMMON_CLASS (0x02) +/* Interface Association Descriptor */ +#define USB_IAD_DESC (0x01) +/* Interface Association Descriptor Type */ +#define USB_IAD_TYPE (0x0B) + +/* Class-Specific Configuration Descriptors */ +#define USB_PCDC_CS_INTERFACE (0x24U) + +/* bDescriptor SubType in Communications Class Functional Descriptors */ +/* Header Functional Descriptor */ +#define USB_PCDC_DT_SUBTYPE_HEADER_FUNC (0x00U) +/* Call Management Functional Descriptor. */ +#define USB_PCDC_DT_SUBTYPE_CALL_MANAGE_FUNC (0x01U) +/* Abstract Control Management Functional Descriptor. */ +#define USB_PCDC_DT_SUBTYPE_ABSTRACT_CTR_MANAGE_FUNC (0x02U) +/* Union Functional Descriptor */ +#define USB_PCDC_DT_SUBTYPE_UNION_FUNC (0x06U) + +/* Communications Class Subclass Codes */ +#define USB_PCDC_CLASS_SUBCLASS_CODE_ABS_CTR_MDL (0x02U) + +/* USB Class Definitions for Communications Devices Specification + release number in binary-coded decimal. */ +#define USB_PCDC_BCD_CDC (0x0110U) + +/* Descriptor length */ +#define USB_DQD_LEN (10U) +#define USB_DD_BLENGTH (18U) +#define USB_PCDC_PMSC_CD_LEN (98U) +#define STRING_DESCRIPTOR0_LEN (4U) +#define STRING_DESCRIPTOR1_LEN (16U) +#define STRING_DESCRIPTOR2_LEN (36U) +#define STRING_DESCRIPTOR3_LEN (36U) +#define STRING_DESCRIPTOR4_LEN (22U) +#define STRING_DESCRIPTOR5_LEN (18U) +#define STRING_DESCRIPTOR6_LEN (28U) +#define NUM_STRING_DESCRIPTOR (7U) + +/* Descriptor data Mask */ +#define USB_UCHAR_MAX (0xffU) +#define USB_W_TOTAL_LENGTH_MASK (256U) +#define USB_W_MAX_PACKET_SIZE_MASK (64U) +#define USB_PCDC_BCD_CDC_MASK (256U) + +/* Sub_class code */ +#define USB_ATAPI ((uint8_t) 0x05U) +#define USB_SCSI ((uint8_t) 0x06U) + +/* Protocol code */ +#define USB_BOTP ((uint8_t) 0x50U) +#define USB_TOTALEP ((uint8_t) 0x02U) + +/* Select SubClass */ + +// #define USB_INTERFACE_SUBCLASS (USB_SCSI) +#define USB_INTERFACE_SUBCLASS (USB_ATAPI) + +#define USB_VALUE_64 (64) + +/****************************************************************************** + Private global variables and functions + ******************************************************************************/ + +/****************************************************************************** + Exported global variables + ******************************************************************************/ + +/****************************************************************************** + Exported global functions (to be accessed by other files) + ******************************************************************************/ + +/* Standard Device Descriptor */ +uint8_t g_apl_device[USB_DD_BLENGTH + ( USB_DD_BLENGTH % 2)] = +{ + USB_DD_BLENGTH, /* 0:bLength */ + USB_DT_DEVICE, /* 1:bDescriptorType */ + (USB_BCDNUM & (uint8_t) USB_UCHAR_MAX), /* 2:bcdUSB_lo */ + ((uint8_t) (USB_BCDNUM >> 8) & (uint8_t) USB_UCHAR_MAX), /* 3:bcdUSB_hi */ + USB_MISC_CLASS, /* 4:bDeviceClass */ + USB_COMMON_CLASS, /* 5:bDeviceSubClass */ + USB_IAD_DESC, /* 6:bDeviceProtocol */ + (uint8_t) USB_DCPMAXP, /* 7:bMAXPacketSize(for DCP) */ + (USB_VENDORID & (uint8_t) USB_UCHAR_MAX), /* 8:idVendor_lo */ + ((uint8_t) (USB_VENDORID >> 8) & (uint8_t) USB_UCHAR_MAX), /* 9:idVendor_hi */ + ((uint16_t) USB_PRODUCTID & (uint8_t) USB_UCHAR_MAX), /* 10:idProduct_lo */ + ((uint8_t) (USB_PRODUCTID >> 8) & (uint8_t) USB_UCHAR_MAX), /* 11:idProduct_hi */ + (USB_RELEASE & (uint8_t) USB_UCHAR_MAX), /* 12:bcdDevice_lo */ + ((uint8_t) (USB_RELEASE >> 8) & (uint8_t) USB_UCHAR_MAX), /* 13:bcdDevice_hi */ + 1, /* 14:iManufacturer */ + 2, /* 15:iProduct */ + 6, /* 16:iSerialNumber */ + USB_CONFIGNUM /* 17:bNumConfigurations */ +}; + +/************************************************************ + * Device Qualifier Descriptor * + ************************************************************/ +uint8_t g_apl_qualifier_descriptor[USB_DQD_LEN + (USB_DQD_LEN % 2)] = +{ + USB_DQD_LEN, /* 0:bLength */ + USB_DT_DEVICE_QUALIFIER, /* 1:bDescriptorType */ + (USB_BCDNUM & (uint8_t) USB_UCHAR_MAX), /* 2:bcdUSB_lo */ + ((uint8_t) (USB_BCDNUM >> 8) & (uint8_t) USB_UCHAR_MAX), /* 3:bcdUSB_hi */ + 0, /* 4:bDeviceClass */ + 0, /* 5:bDeviceSubClass */ + 0, /* 6:bDeviceProtocol */ + (uint8_t) USB_DCPMAXP, /* 7:bMAXPacketSize(for DCP) */ + USB_CONFIGNUM, /* 8:bNumConfigurations */ + 0 /* 9:bReserved */ +}; + +/************************************************************ + * Configuration Or Other_Speed_Configuration Descriptor * + ************************************************************/ +/* For Full-Speed */ +uint8_t g_apl_configuration[USB_PCDC_PMSC_CD_LEN + (USB_PCDC_PMSC_CD_LEN % 2)] = +{ + USB_CD_BLENGTH, /* 0:bLength */ + USB_SOFT_CHANGE, /* 1:bDescriptorType */ + USB_PCDC_PMSC_CD_LEN % USB_W_TOTAL_LENGTH_MASK, /* 2:wTotalLength(L) */ + USB_PCDC_PMSC_CD_LEN / USB_W_TOTAL_LENGTH_MASK, /* 3:wTotalLength(H) */ + 3, /* 4:bNumInterfaces */ + 1, /* 5:bConfigurationValue */ + 0, /* 6:iConfiguration */ + USB_CF_RESERVED | USB_CF_SELFP, /* 7:bmAttributes */ + (10 / 2), /* 8:MAXPower (2mA unit) */ + +/* Communication Device Class */ + + /* Interface Association Descriptor (IAD) */ + 0x08, /* 0:bLength */ + USB_IAD_TYPE, /* 1:bDescriptorType */ + 0x00, /* 2:bFirstInterface */ + 0x02, /* 3:bInterfaceCount */ + USB_IFCLS_CDCC, /* 4:bFunctionClass */ + USB_PCDC_CLASS_SUBCLASS_CODE_ABS_CTR_MDL, /* 5:bFunctionSubClass */ + 0x01, /* 6:bFunctionProtocol */ + 0x00, /* 7:iFunction */ + + /* Interface Descriptor */ + USB_ID_BLENGTH, /* 0:bLength */ + USB_DT_INTERFACE, /* 1:bDescriptor */ + 0, /* 2:bInterfaceNumber */ + 0, /* 3:bAlternateSetting */ + 1, /* 4:bNumEndpoints */ + USB_IFCLS_CDCC, /* 5:bInterfaceClass */ + USB_PCDC_CLASS_SUBCLASS_CODE_ABS_CTR_MDL, /* 6:bInterfaceSubClass */ + 1, /* 7:bInterfaceProtocol */ + 0, /* 8:iInterface */ + + /* Communications Class Functional Descriptorss */ + 5, /* 0:bLength */ + USB_PCDC_CS_INTERFACE, /* 1:bDescriptorType */ + USB_PCDC_DT_SUBTYPE_HEADER_FUNC, /* 2:bDescriptorSubtype */ + USB_PCDC_BCD_CDC % USB_W_TOTAL_LENGTH_MASK, /* 3:bcdCDC_lo */ + USB_PCDC_BCD_CDC / USB_W_TOTAL_LENGTH_MASK, /* 4:bcdCDC_hi */ + + /* Communications Class Functional Descriptorss */ + 4, /* 0:bLength */ + USB_PCDC_CS_INTERFACE, /* 1:bDescriptorType */ + USB_PCDC_DT_SUBTYPE_ABSTRACT_CTR_MANAGE_FUNC, /* 2:bDescriptorSubtype */ + 2, /* 3:bmCapabilities */ + + /* Communications Class Functional Descriptorss */ + 5, /* 0:bLength */ + USB_PCDC_CS_INTERFACE, /* 1:bDescriptorType */ + USB_PCDC_DT_SUBTYPE_UNION_FUNC, /* 2:bDescriptorSubtype */ + 0, /* 3:bMasterInterface */ + 1, /* 4:bSlaveInterface0 */ + + /* Communications Class Functional Descriptorss */ + 5, /* 0:bLength */ + USB_PCDC_CS_INTERFACE, /* 1:bDescriptorType */ + USB_PCDC_DT_SUBTYPE_CALL_MANAGE_FUNC, /* 2:bDescriptorSubtype */ + /* D1:1-Device can send/receive call management + information over a Data Class interface. */ + /* D0:1-Device handles call management itself. */ + 3, /* 3:bmCapabilities */ + 1, /* 4:bDataInterface */ + + /* Endpoint Descriptor 0 */ + 7, /* 0:bLength */ + USB_DT_ENDPOINT, /* 1:bDescriptorType */ + USB_EP_IN | USB_EP3, /* 2:bEndpointAddress */ + USB_EP_INT, /* 3:bmAttribute */ + 16, /* 4:wMAXPacketSize_lo */ + 0, /* 5:wMAXPacketSize_hi */ + 0x10, /* 6:bInterval */ + + /* Interface Descriptor */ + USB_ID_BLENGTH, /* 0:bLength */ + USB_DT_INTERFACE, /* 1:bDescriptor */ + 1, /* 2:bInterfaceNumber */ + 0, /* 3:bAlternateSetting */ + 2, /* 4:bNumEndpoints */ + USB_IFCLS_CDCD, /* 5:bInterfaceClass */ + 0, /* 6:bInterfaceSubClass */ + 0, /* 7:bInterfaceProtocol */ + 0, /* 8:iInterface */ + + /* Endpoint Descriptor 0 */ + USB_ED_BLENGTH, /* 0:bLength */ + USB_DT_ENDPOINT, /* 1:bDescriptorType */ + USB_EP_IN | USB_EP1, /* 2:bEndpointAddress */ + USB_EP_BULK, /* 3:bmAttribute */ + USB_W_MAX_PACKET_SIZE_MASK, /* 4:wMAXPacketSize_lo */ + 0, /* 5:wMAXPacketSize_hi */ + 0, /* 6:bInterval */ + + /* Endpoint Descriptor 1 */ + USB_ED_BLENGTH, /* 0:bLength */ + USB_DT_ENDPOINT, /* 1:bDescriptorType */ + USB_EP_OUT | USB_EP2, /* 2:bEndpointAddress */ + USB_EP_BULK, /* 3:bmAttribute */ + USB_W_MAX_PACKET_SIZE_MASK, /* 4:wMAXPacketSize_lo */ + 0, /* 5:wMAXPacketSize_hi */ + 0, /* 6:bInterval */ + +/* Mass Storage Class */ + + /* Interface Descriptor */ + USB_ID_BLENGTH, /* 0:bLength */ + USB_DT_INTERFACE, /* 1:bDescriptor */ + 2, /* 2:bInterfaceNumber */ + 0, /* 3:bAlternateSetting */ + 2, /* 4:bNumEndpoints */ + USB_IFCLS_MAS, /* 5:bInterfaceClass */ + USB_INTERFACE_SUBCLASS, /* 6:bInterfaceSubClass */ + USB_BOTP, /* 7:bInterfaceProtocol */ + 3, /* 8:iInterface */ + + /* Endpoint Descriptor 0 */ + USB_ED_BLENGTH, /* 0:bLength */ + USB_DT_ENDPOINT, /* 1:bDescriptorType */ + USB_EP_IN | USB_EP4, /* 2:bEndpointAddress */ + USB_EP_BULK, /* 3:bmAttribute */ + USB_VALUE_64, /* 4:wMAXPacketSize_lo */ + 0, /* 5:wMAXPacketSize_hi */ + 0, /* 6:bInterval */ + + /* Endpoint Descriptor 1 */ + USB_ED_BLENGTH, /* 0:bLength */ + USB_DT_ENDPOINT, /* 1:bDescriptorType */ + USB_EP_OUT | USB_EP5, /* 2:bEndpointAddress */ + USB_EP_BULK, /* 3:bmAttribute */ + USB_VALUE_64, /* 4:wMAXPacketSize_lo */ + 0, /* 5:wMAXPacketSize_hi */ + 1, /* 6:bInterval */ +}; + +/* For High-Speed */ +uint8_t g_apl_hs_configuration[USB_PCDC_PMSC_CD_LEN + (USB_PCDC_PMSC_CD_LEN % 2)] = +{ + 9, /* 0:bLength */ + USB_SOFT_CHANGE, /* 1:bDescriptorType */ + USB_PCDC_PMSC_CD_LEN % USB_W_TOTAL_LENGTH_MASK, /* 2:wTotalLength(L) */ + USB_PCDC_PMSC_CD_LEN / USB_W_TOTAL_LENGTH_MASK, /* 3:wTotalLength(H) */ + 3, /* 4:bNumInterfaces */ + 1, /* 5:bConfigurationValue */ + 0, /* 6:iConfiguration */ + USB_CF_RESERVED | USB_CF_SELFP, /* 7:bmAttributes */ + (10 / 2), /* 8:MAXPower (2mA unit) */ + +/* Communication Device Class */ + + /* Interface Association Descriptor (IAD) */ + 0x08, /* 0:bLength */ + USB_IAD_TYPE, /* 1:bDescriptorType */ + 0x00, /* 2:bFirstInterface */ + 0x02, /* 3:bInterfaceCount */ + USB_IFCLS_CDCC, /* 4:bFunctionClass */ + USB_PCDC_CLASS_SUBCLASS_CODE_ABS_CTR_MDL, /* 5:bFunctionSubClass */ + 0x01, /* 6:bFunctionProtocol */ + 0x00, /* 7:iFunction */ + + /* Interface Descriptor */ + 9, /* 0:bLength */ + USB_DT_INTERFACE, /* 1:bDescriptor */ + 0, /* 2:bInterfaceNumber */ + 0, /* 3:bAlternateSetting */ + 1, /* 4:bNumEndpoints */ + USB_IFCLS_CDCC, /* 5:bInterfaceClass */ + USB_PCDC_CLASS_SUBCLASS_CODE_ABS_CTR_MDL, /* 6:bInterfaceSubClass */ + 1, /* 7:bInterfaceProtocol */ + 0, /* 8:iInterface */ + + /* Communications Class Functional Descriptorss */ + 5, /* 0:bLength */ + USB_PCDC_CS_INTERFACE, /* 1:bDescriptorType */ + USB_PCDC_DT_SUBTYPE_HEADER_FUNC, /* 2:bDescriptorSubtype */ + USB_PCDC_BCD_CDC % USB_PCDC_BCD_CDC_MASK, /* 3:bcdCDC_lo */ + USB_PCDC_BCD_CDC / USB_PCDC_BCD_CDC_MASK, /* 4:bcdCDC_hi */ + + /* Communications Class Functional Descriptorss */ + 4, /* 0:bLength */ + USB_PCDC_CS_INTERFACE, /* 1:bDescriptorType */ + USB_PCDC_DT_SUBTYPE_ABSTRACT_CTR_MANAGE_FUNC, /* 2:bDescriptorSubtype */ + 2, /* 3:bmCapabilities */ + + /* Communications Class Functional Descriptorss */ + 5, /* 0:bLength */ + USB_PCDC_CS_INTERFACE, /* 1:bDescriptorType */ + USB_PCDC_DT_SUBTYPE_UNION_FUNC, /* 2:bDescriptorSubtype */ + 0, /* 3:bMasterInterface */ + 1, /* 4:bSlaveInterface0 */ + + /* Communications Class Functional Descriptorss */ + 5, /* 0:bLength */ + USB_PCDC_CS_INTERFACE, /* 1:bDescriptorType */ + USB_PCDC_DT_SUBTYPE_CALL_MANAGE_FUNC, /* 2:bDescriptorSubtype */ + /* D1:1-Device can send/receive call management + information over a Data Class interface. */ + /* D0:1-Device handles call management itself. */ + 3, /* 3:bmCapabilities */ + 1, /* 4:bDataInterface */ + + /* Endpoint Descriptor 0 */ + 7, /* 0:bLength */ + USB_DT_ENDPOINT, /* 1:bDescriptorType */ + USB_EP_IN | USB_EP3, /* 2:bEndpointAddress */ + USB_EP_INT, /* 3:bmAttribute */ + 16, /* 4:wMAXPacketSize_lo */ + 0, /* 5:wMAXPacketSize_hi */ + 0x10, /* 6:bInterval */ + + /* Interface Descriptor */ + 9, /* 0:bLength */ + USB_DT_INTERFACE, /* 1:bDescriptor */ + 1, /* 2:bInterfaceNumber */ + 0, /* 3:bAlternateSetting */ + 2, /* 4:bNumEndpoints */ + USB_IFCLS_CDCD, /* 5:bInterfaceClass */ + 0, /* 6:bInterfaceSubClass */ + 0, /* 7:bInterfaceProtocol */ + 0, /* 8:iInterface */ + + /* Endpoint Descriptor 0 */ + 7, /* 0:bLength */ + USB_DT_ENDPOINT, /* 1:bDescriptorType */ + USB_EP_IN | USB_EP1, /* 2:bEndpointAddress */ + USB_EP_BULK, /* 3:bmAttribute */ + 0, /* 4:wMAXPacketSize_lo */ + 2, /* 5:wMAXPacketSize_hi */ + 0, /* 6:bInterval */ + + /* Endpoint Descriptor 1 */ + 7, /* 0:bLength */ + USB_DT_ENDPOINT, /* 1:bDescriptorType */ + USB_EP_OUT | USB_EP2, /* 2:bEndpointAddress */ + USB_EP_BULK, /* 3:bmAttribute */ + 0, /* 4:wMAXPacketSize_lo */ + 2, /* 5:wMAXPacketSize_hi */ + 0, /* 6:bInterval */ + +/* Mass Storage Class */ + + /* Interface Descriptor */ + USB_ID_BLENGTH, /* 0:bLength */ + USB_DT_INTERFACE, /* 1:bDescriptor */ + 2, /* 2:bInterfaceNumber */ + 0, /* 3:bAlternateSetting */ + 2, /* 4:bNumEndpoints */ + USB_IFCLS_MAS, /* 5:bInterfaceClass */ + USB_INTERFACE_SUBCLASS, /* 6:bInterfaceSubClass */ + USB_BOTP, /* 7:bInterfaceProtocol */ + 3, /* 8:iInterface */ + + /* Endpoint Descriptor 0 */ + USB_ED_BLENGTH, /* 0:bLength */ + USB_DT_ENDPOINT, /* 1:bDescriptorType */ + USB_EP_IN | USB_EP4, /* 2:bEndpointAddress */ + USB_EP_BULK, /* 3:bmAttribute */ + 0, /* 4:wMAXPacketSize_lo */ + 2, /* 5:wMAXPacketSize_hi */ + 0, /* 6:bInterval */ + + /* Endpoint Descriptor 1 */ + USB_ED_BLENGTH, /* 0:bLength */ + USB_DT_ENDPOINT, /* 1:bDescriptorType */ + USB_EP_OUT | USB_EP5, /* 2:bEndpointAddress */ + USB_EP_BULK, /* 3:bmAttribute */ + 0, /* 4:wMAXPacketSize_lo */ + 2, /* 5:wMAXPacketSize_hi */ + 1, /* 6:bInterval */ +}; + +/************************************* + * String Descriptor * + *************************************/ +/* UNICODE 0x0409 English (United States) */ +uint8_t g_apl_string_descriptor0[STRING_DESCRIPTOR0_LEN + ( STRING_DESCRIPTOR0_LEN % 2)] = +{ + STRING_DESCRIPTOR0_LEN, /* 0:bLength */ + USB_DT_STRING, /* 1:bDescriptorType */ + 0x09, 0x04 /* 2:wLANGID[0] */ +}; + +/* iManufacturer */ +uint8_t g_apl_string_descriptor1[STRING_DESCRIPTOR1_LEN + ( STRING_DESCRIPTOR1_LEN % 2)] = +{ + STRING_DESCRIPTOR1_LEN, /* 0:bLength */ + USB_DT_STRING, /* 1:bDescriptorType */ + 'R', 0x00, /* 2:wLANGID[0] */ + 'E', 0x00, + 'N', 0x00, + 'E', 0x00, + 'S', 0x00, + 'A', 0x00, + 'S', 0x00, +}; + +/* iProduct */ +uint8_t g_apl_string_descriptor2[STRING_DESCRIPTOR2_LEN + ( STRING_DESCRIPTOR2_LEN % 2)] = +{ + STRING_DESCRIPTOR2_LEN, /* 0:bLength */ + USB_DT_STRING, /* 1:bDescriptorType */ + 'U', 0x00, + 'S', 0x00, + 'B', 0x00, + ' ', 0x00, + 'D', 0x00, + 'e', 0x00, + 'm', 0x00, + 'o', 0x00, + 'n', 0x00, + 's', 0x00, + 't', 0x00, + 'r', 0x00, + 'a', 0x00, + 't', 0x00, + 'i', 0x00, + 'o', 0x00, + 'n', 0x00, +}; + +/* iInterface */ +uint8_t g_apl_string_descriptor3[STRING_DESCRIPTOR3_LEN + ( STRING_DESCRIPTOR3_LEN % 2)] = +{ + STRING_DESCRIPTOR3_LEN, /* 0:bLength */ + USB_DT_STRING, /* 1:bDescriptorType */ + 'C', 0x00, + 'o', 0x00, + 'm', 0x00, + 'p', 0x00, + 'o', 0x00, + 's', 0x00, + 'i', 0x00, + 't', 0x00, + 'e', 0x00, + ' ', 0x00, + 'D', 0x00, + 'e', 0x00, + 'v', 0x00, + 'i', 0x00, + 'c', 0x00, + 'e', 0x00, + 's', 0x00 +}; + +/* iConfiguration */ +uint8_t g_apl_string_descriptor4[STRING_DESCRIPTOR4_LEN + ( STRING_DESCRIPTOR4_LEN % 2)] = +{ + STRING_DESCRIPTOR4_LEN, /* 0:bLength */ + USB_DT_STRING, /* 1:bDescriptorType */ + 'F', 0x00, /* 2:wLANGID[0] */ + 'u', 0x00, + 'l', 0x00, + 'l', 0x00, + '-', 0x00, + 'S', 0x00, + 'p', 0x00, + 'e', 0x00, + 'e', 0x00, + 'd', 0x00 +}; + +/* iConfiguration */ +uint8_t g_apl_string_descriptor5[STRING_DESCRIPTOR5_LEN + ( STRING_DESCRIPTOR5_LEN % 2)] = +{ + STRING_DESCRIPTOR5_LEN, /* 0:bLength */ + USB_DT_STRING, /* 1:bDescriptorType */ + 'H', 0x00, /* 2:wLANGID[0] */ + 'i', 0x00, + '-', 0x00, + 'S', 0x00, + 'p', 0x00, + 'e', 0x00, + 'e', 0x00, + 'd', 0x00 +}; + +/* iSerialNumber */ +uint8_t g_apl_string_descriptor6[STRING_DESCRIPTOR6_LEN + ( STRING_DESCRIPTOR6_LEN % 2)] = +{ + STRING_DESCRIPTOR6_LEN, /* 0:bLength */ + USB_DT_STRING, /* 1:bDescriptorType */ + '0', 0x00, /* 2:wLANGID[0] */ + '0', 0x00, + '0', 0x00, + '0', 0x00, + '0', 0x00, + '0', 0x00, + '0', 0x00, + '0', 0x00, + '0', 0x00, + '0', 0x00, + '0', 0x00, + '0', 0x00, + '1', 0x00, +}; + +uint8_t *g_apl_string_table[] = +{ + g_apl_string_descriptor0, + g_apl_string_descriptor1, + g_apl_string_descriptor2, + g_apl_string_descriptor3, + g_apl_string_descriptor4, + g_apl_string_descriptor5, + g_apl_string_descriptor6 +}; + +const usb_descriptor_t g_usb_descriptor = +{ + g_apl_device, /* Pointer to the device descriptor */ + g_apl_configuration, /* Pointer to the configuration descriptor for Full-speed */ + g_apl_hs_configuration, /* Pointer to the configuration descriptor for Hi-speed */ + g_apl_qualifier_descriptor, /* Pointer to the qualifier descriptor */ + g_apl_string_table, /* Pointer to the string descriptor table */ + NUM_STRING_DESCRIPTOR +}; + +/****************************************************************************** + End Of File + ******************************************************************************/ diff --git a/ra/fsp/src/r_usb_composite/r_usb_phid_pmsc_descriptor.c.template b/ra/fsp/src/r_usb_composite/r_usb_phid_pmsc_descriptor.c.template new file mode 100644 index 000000000..b5c668d14 --- /dev/null +++ b/ra/fsp/src/r_usb_composite/r_usb_phid_pmsc_descriptor.c.template @@ -0,0 +1,361 @@ +/* ${REA_DISCLAIMER_PLACEHOLDER} */ +/****************************************************************************** + Includes , "Project Includes" + ******************************************************************************/ +#include "r_usb_basic.h" +#include "r_usb_basic_api.h" +#include "r_usb_phid_api.h" + +/****************************************************************************** + Macro definitions + ******************************************************************************/ +#define USB_BCDNUM (0x0200U) /* bcdUSB */ +#define USB_RELEASE (0x0200U) /* Release Number */ +#define USB_CONFIGNUM (1U) /* Configuration number */ +#define USB_DCPMAXP (64U) /* DCP max packet size */ + +#define USB_IFPROTOCOL (USB_IFPRO_KBD) +#define HID_ITEM_LEN (76U) +#define HID_MXPS (8U) +#define HID_NUM_EP (1U) + +/* [Vendor ID & Product ID setting] */ +#define USB_VENDORID (0x0000U) +#define USB_PRODUCTID (0x0013U) + +/* Configuration Descriptor Length */ +#define USB_PMSC_PHID_CD_LEN (57U) +#define USB_DD_BLENGTH (18U) +#define STRING_DESCRIPTOR0_LEN (4U) +#define STRING_DESCRIPTOR1_LEN (16U) +#define STRING_DESCRIPTOR2_LEN (36U) +#define STRING_DESCRIPTOR3_LEN (36U) +#define STRING_DESCRIPTOR4_LEN (22U) +#define STRING_DESCRIPTOR5_LEN (18U) +#define STRING_DESCRIPTOR6_LEN (28U) +#define NUM_STRING_DESCRIPTOR (7U) + +/* Descriptor data Mask */ +#define USB_UCHAR_MAX (0xffU) +#define USB_W_TOTAL_LENGTH_MASK (256U) + +/* Sub_class code */ +#define USB_ATAPI ((uint8_t) 0x05U) +#define USB_SCSI ((uint8_t) 0x06U) + +/* Protocol code */ +#define USB_BOTP ((uint8_t) 0x50U) +#define USB_TOTALEP ((uint8_t) 0x02U) + +/* Select SubClass */ + +// #define USB_INTERFACE_SUBCLASS (USB_SCSI) +#define USB_INTERFACE_SUBCLASS (USB_ATAPI) + +#define USB_VALUE_64 (64) + +/************************************************************ + * Device Descriptor * + ************************************************************/ +uint8_t g_apl_device[USB_DD_BLENGTH + ( USB_DD_BLENGTH % 2)] = +{ + USB_DD_BLENGTH, /* 0:bLength */ + USB_DT_DEVICE, /* 1:bDescriptorType */ + (uint8_t) ( USB_BCDNUM & (uint8_t) USB_UCHAR_MAX), /* 2:bcdUSB_lo */ + (uint8_t) ((uint8_t) (USB_BCDNUM >> 8) & (uint8_t) USB_UCHAR_MAX), /* 3:bcdUSB_hi */ + 0x00, /* 4:bDeviceClass */ + 0x00, /* 5:bDeviceSubClass */ + 0x00, /* 6:bDeviceProtocol */ + (uint8_t) USB_DCPMAXP, /* 7:bMAXPacketSize(for DCP) */ + (uint8_t) (USB_VENDORID & (uint8_t) USB_UCHAR_MAX), /* 8:idVendor_lo */ + (uint8_t) ((uint8_t) (USB_VENDORID >> 8) & (uint8_t) USB_UCHAR_MAX), /* 9:idVendor_hi */ + (uint8_t) (USB_PRODUCTID & (uint8_t) USB_UCHAR_MAX), /* 10:idProduct_lo */ + (uint8_t) ((uint8_t) (USB_PRODUCTID >> 8) & (uint8_t) USB_UCHAR_MAX),/* 11:idProduct_hi */ + (uint8_t) (USB_RELEASE & (uint8_t) USB_UCHAR_MAX), /* 12:bcdDevice_lo */ + (uint8_t) ((uint8_t) (USB_RELEASE >> 8) & (uint8_t) USB_UCHAR_MAX), /* 13:bcdDevice_hi */ + 1, /* 14:iManufacturer */ + 2, /* 15:iProduct */ + 3, /* 16:iSerialNumber */ + USB_CONFIGNUM, /* 17:bNumConfigurations */ +}; + +/************************************************************ + * Configuration Descriptor * + ************************************************************/ +uint8_t g_apl_configuration[USB_PMSC_PHID_CD_LEN + (USB_PMSC_PHID_CD_LEN % 2)] = +{ + USB_CD_BLENGTH, /* 0:bLength */ + USB_DT_CONFIGURATION, /* 1:bDescriptorType */ + (uint8_t) (USB_PMSC_PHID_CD_LEN % USB_W_TOTAL_LENGTH_MASK), /* 2:wTotalLength(L) */ + (uint8_t) (USB_PMSC_PHID_CD_LEN / USB_W_TOTAL_LENGTH_MASK), /* 3:wTotalLength(H) */ + 2, /* 4:bNumInterfaces */ + 1, /* 5:bConfigurationValue */ + 4, /* 6:iConfiguration */ + (uint8_t) (USB_CF_RESERVED | USB_CF_SELFP | USB_CF_RWUPON), /* 7:bmAttributes */ + (uint8_t) (100 / 2), /* 8:bMaxPower (2mA unit) */ + +/* Mass Storage Class */ + + /* Interface Descriptor */ + USB_ID_BLENGTH, /* 0:bLength */ + USB_DT_INTERFACE, /* 1:bDescriptor */ + 0, /* 2:bInterfaceNumber */ + 0, /* 3:bAlternateSetting */ + 2, /* 4:bNumEndpoints */ + USB_IFCLS_MAS, /* 5:bInterfaceClass */ + USB_INTERFACE_SUBCLASS, /* 6:bInterfaceSubClass */ + USB_BOTP, /* 7:bInterfaceProtocol */ + 3, /* 8:iInterface */ + + /* Endpoint Descriptor 0 */ + USB_ED_BLENGTH, /* 0:bLength */ + USB_DT_ENDPOINT, /* 1:bDescriptorType */ + USB_EP_IN | USB_EP1, /* 2:bEndpointAddress */ + USB_EP_BULK, /* 3:bmAttribute */ + USB_VALUE_64, /* 4:wMAXPacketSize_lo */ + 0, /* 5:wMAXPacketSize_hi */ + 0, /* 6:bInterval */ + + /* Endpoint Descriptor 1 */ + USB_ED_BLENGTH, /* 0:bLength */ + USB_DT_ENDPOINT, /* 1:bDescriptorType */ + USB_EP_OUT | USB_EP2, /* 2:bEndpointAddress */ + USB_EP_BULK, /* 3:bmAttribute */ + USB_VALUE_64, /* 4:wMAXPacketSize_lo */ + 0, /* 5:wMAXPacketSize_hi */ + 1, /* 6:bInterval */ + +/* Human Iterface Device Class */ + + /* Interface Descriptor */ + USB_ID_BLENGTH, /* 0:bLength */ + USB_DT_INTERFACE, /* 1:bDescriptor */ + 1, /* 2:bInterfaceNumber */ + 0, /* 3:bAlternateSetting */ + HID_NUM_EP, /* 4:bNumEndpoints */ + USB_IFCLS_HID, /* 5:bInterfaceClass(HID) */ + USB_IFSUB_NOBOOT, /* 6:bInterfaceSubClass(NonBOOT) */ + USB_IFPROTOCOL, /* 7:bInterfaceProtocol */ + 0, /* 8:iInterface */ + + /* HID Descriptor */ + 9, /* 0:bLength */ + USB_DT_TYPE_HIDDESCRIPTOR, /* 1:bDescriptor */ + 0x00, /* 2:HID Ver */ + 0x01, /* 3:HID Ver */ + 0x00, /* 4:bCountryCode */ + 0x01, /* 5:bNumDescriptors */ + 0x22, /* 6:bDescriptorType */ + HID_ITEM_LEN, /* 7:wItemLength(L) */ + 0x00, /* 8:wItemLength(H) */ + + /* Endpoint Descriptor 0 */ + USB_ED_BLENGTH, /* 0:bLength */ + USB_DT_ENDPOINT, /* 1:bDescriptorType */ + (uint8_t) (USB_EP_IN | USB_EP3), /* 2:bEndpointAddress */ + USB_EP_INT, /* 3:bmAttribute */ + HID_MXPS, /* 4:wMaxPacketSize_lo */ + 0, /* 5:wMaxPacketSize_hi */ + 0x0A, /* 6:bInterval */ +} ; + +/************************************************************ + * String Descriptor 0 * + ************************************************************/ +/* UNICODE 0x0409 English (United States) */ +uint8_t g_apl_string_descriptor0[STRING_DESCRIPTOR0_LEN + ( STRING_DESCRIPTOR0_LEN % 2)] = +{ + 4, /* 0:bLength */ + USB_DT_STRING, /* 1:bDescriptorType */ + 0x09, 0x04 /* 2:wLANGID[0] */ +}; + +/************************************************************ + * String Descriptor 1 * + ************************************************************/ +/* 14:iManufacturer */ +uint8_t g_apl_string_descriptor1[STRING_DESCRIPTOR1_LEN + ( STRING_DESCRIPTOR1_LEN % 2)] = +{ + 16, /* 0:bLength */ + USB_DT_STRING, /* 1:bDescriptorType */ + 'R', 0x00, + 'e', 0x00, + 'n', 0x00, + 'e', 0x00, + 's', 0x00, + 'a', 0x00, + 's', 0x00, +}; + +/* iProduct */ +uint8_t g_apl_string_descriptor2[STRING_DESCRIPTOR2_LEN + ( STRING_DESCRIPTOR2_LEN % 2)] = +{ + STRING_DESCRIPTOR2_LEN, /* 0:bLength */ + USB_DT_STRING, /* 1:bDescriptorType */ + 'U', 0x00, + 'S', 0x00, + 'B', 0x00, + ' ', 0x00, + 'D', 0x00, + 'e', 0x00, + 'm', 0x00, + 'o', 0x00, + 'n', 0x00, + 's', 0x00, + 't', 0x00, + 'r', 0x00, + 'a', 0x00, + 't', 0x00, + 'i', 0x00, + 'o', 0x00, + 'n', 0x00, +}; + +/* iInterface */ +uint8_t g_apl_string_descriptor3[STRING_DESCRIPTOR3_LEN + ( STRING_DESCRIPTOR3_LEN % 2)] = +{ + STRING_DESCRIPTOR3_LEN, /* 0:bLength */ + USB_DT_STRING, /* 1:bDescriptorType */ + 'C', 0x00, + 'o', 0x00, + 'm', 0x00, + 'p', 0x00, + 'o', 0x00, + 's', 0x00, + 'i', 0x00, + 't', 0x00, + 'e', 0x00, + ' ', 0x00, + 'D', 0x00, + 'e', 0x00, + 'v', 0x00, + 'i', 0x00, + 'c', 0x00, + 'e', 0x00, + 's', 0x00 +}; + +/* iConfiguration */ +uint8_t g_apl_string_descriptor4[STRING_DESCRIPTOR4_LEN + ( STRING_DESCRIPTOR4_LEN % 2)] = +{ + STRING_DESCRIPTOR4_LEN, /* 0:bLength */ + USB_DT_STRING, /* 1:bDescriptorType */ + 'F', 0x00, /* 2:wLANGID[0] */ + 'u', 0x00, + 'l', 0x00, + 'l', 0x00, + '-', 0x00, + 'S', 0x00, + 'p', 0x00, + 'e', 0x00, + 'e', 0x00, + 'd', 0x00 +}; + +/* iConfiguration */ +uint8_t g_apl_string_descriptor5[STRING_DESCRIPTOR5_LEN + ( STRING_DESCRIPTOR5_LEN % 2)] = +{ + STRING_DESCRIPTOR5_LEN, /* 0:bLength */ + USB_DT_STRING, /* 1:bDescriptorType */ + 'H', 0x00, /* 2:wLANGID[0] */ + 'i', 0x00, + '-', 0x00, + 'S', 0x00, + 'p', 0x00, + 'e', 0x00, + 'e', 0x00, + 'd', 0x00 +}; + +/* iSerialNumber */ +uint8_t g_apl_string_descriptor6[STRING_DESCRIPTOR6_LEN + ( STRING_DESCRIPTOR6_LEN % 2)] = +{ + STRING_DESCRIPTOR6_LEN, /* 0:bLength */ + USB_DT_STRING, /* 1:bDescriptorType */ + '0', 0x00, /* 2:wLANGID[0] */ + '0', 0x00, + '0', 0x00, + '0', 0x00, + '0', 0x00, + '0', 0x00, + '0', 0x00, + '0', 0x00, + '0', 0x00, + '0', 0x00, + '0', 0x00, + '0', 0x00, + '1', 0x00, +}; + +uint8_t *g_apl_string_table[] = +{ + g_apl_string_descriptor0, + g_apl_string_descriptor1, + g_apl_string_descriptor2, + g_apl_string_descriptor3, + g_apl_string_descriptor4, + g_apl_string_descriptor5, + g_apl_string_descriptor6 +}; + +/************************************************************ + * HID Report Discriptor for Keyboard * + ************************************************************/ +const uint8_t g_apl_report[] = +{ + 0x05, 0x01, /* Usage Page (Generic Desktop) */ + 0x09, 0x06, /* Usage (Keyboard) */ + 0xA1, 0x01, /* Collection (Application) */ + 0x05, 0x07, /* Usage Page (Key Codes); */ + 0x19, 0xE0, /* Usage Minimum (224) */ + 0x29, 0xE7, /* Usage Maximum (231) */ + 0x15, 0x00, /* Logical Minimum (0) */ + 0x25, 0x01, /* Logical Maximum (1) */ + 0x75, 0x01, /* Report Size (1) */ + 0x95, 0x08, /* Report Count (8) */ + 0x81, 0x02, /* Input (Data , Variable , Absolute) ;Modifier byte */ + 0x95, 0x01, /* Report Count (1) */ + 0x75, 0x08, /* Report Size (8) */ + 0x81, 0x01, /* Input (Constant) ;Reserved byte */ + 0x95, 0x05, /* Report Count (5) */ + 0x75, 0x01, /* Report Size (1) */ + 0x05, 0x08, /* Usage Page (Page# for LEDs) */ + 0x19, 0x01, /* Usage Minimum (1) */ + 0x29, 0x05, /* Usage Maximum (5) */ + 0x91, 0x02, /* Output (Data , Variable , Absolute) ;LED report */ + 0x95, 0x01, /* Report Count (1) */ + 0x75, 0x03, /* Report Size (3) */ + 0x91, 0x01, /* Output (Constant) ;LED report padding */ + 0x95, 0x06, /* Report Count (6) */ + 0x75, 0x08, /* Report Size (8) */ + 0x15, 0x00, /* Logical Minimum (0) */ + 0x25, 0x65, /* Logical Maximum(101) */ + 0x05, 0x07, /* Usage Page (Key Codes) */ + 0x19, 0x00, /* Usage Minimum (0) */ + 0x29, 0x65, /* Usage Maximum (101) */ + 0x81, 0x00, /* Input (Data , Array) ;Key arrays (6 bytes) */ + + /* *** The OUTPUR REPORT *** */ + 0x09, 0x00, /* Usage ID within this page (Vendor defined)*/ + 0x15, 0x00, /* Logical Min 0 */ + 0x26, USB_UCHAR_MAX, 0x00, /* Logical Max 255 */ + 0x75, 0x08, /* Size 8 Bits (Each Field will be 8bits) */ + 0x95, 0x01, /* Count (Number of fields(bytes) in OUTPUT report) */ + 0x91, 0x02, /* Output Report - type variable data */ + 0xC0, /* End Collection */ +}; + +const usb_descriptor_t g_usb_descriptor = +{ + g_apl_device, /* Pointer to the device descriptor */ + g_apl_configuration, /* Pointer to the configuration descriptor for Full-speed */ + NULL, /* Pointer to the configuration descriptor for Hi-speed */ + NULL, /* Pointer to the qualifier descriptor */ + g_apl_string_table, /* Pointer to the string descriptor table */ + NUM_STRING_DESCRIPTOR +}; + + +/****************************************************************************** + End Of File + ******************************************************************************/ diff --git a/ra/fsp/src/r_usb_pcdc/r_usb_pcdc_descriptor.c.template b/ra/fsp/src/r_usb_pcdc/r_usb_pcdc_descriptor.c.template index 160849edf..09896df81 100644 --- a/ra/fsp/src/r_usb_pcdc/r_usb_pcdc_descriptor.c.template +++ b/ra/fsp/src/r_usb_pcdc/r_usb_pcdc_descriptor.c.template @@ -5,7 +5,6 @@ #include "r_usb_basic.h" #include "r_usb_basic_api.h" #include "r_usb_basic_cfg.h" -#include "hal_data.h" /****************************************************************************** Macro definitions diff --git a/ra/fsp/src/r_usb_pcdc/src/r_usb_pcdc_driver.c b/ra/fsp/src/r_usb_pcdc/src/r_usb_pcdc_driver.c index bd85a523d..2827c9efc 100644 --- a/ra/fsp/src/r_usb_pcdc/src/r_usb_pcdc_driver.c +++ b/ra/fsp/src/r_usb_pcdc/src/r_usb_pcdc_driver.c @@ -73,7 +73,14 @@ void usb_pcdc_read_complete (usb_utr_t * mess, uint16_t data1, uint16_t data2) /* Set Receive data length */ ctrl.data_size = mess->read_req_len - mess->tranlen; ctrl.pipe = (uint8_t) mess->keyword; /* Pipe number setting */ - ctrl.type = USB_CLASS_PCDC; /* Device class setting */ + if (USB_CFG_PCDC_BULK_OUT == ctrl.pipe) + { + ctrl.type = USB_CLASS_PCDC; /* CDC Data class */ + } + else + { + ctrl.type = USB_CLASS_PCDC2; /* CDC Data class */ + } switch (mess->status) { @@ -149,6 +156,16 @@ void usb_pcdc_write_complete (usb_utr_t * mess, uint16_t data1, uint16_t data2) { ctrl.status = FSP_SUCCESS; } + else if (USB_CFG_PCDC_BULK_IN2 == ctrl.pipe) + { + ctrl.type = USB_CLASS_PCDC2; /* CDC Data class */ + } + + /* USB_CFG_PCDC_INT_IN2 */ + else if (USB_CFG_PCDC_INT_IN2 == ctrl.pipe) + { + ctrl.type = USB_CLASS_PCDCC2; /* CDC Control class */ + } else { ctrl.status = FSP_ERR_USB_FAILED; diff --git a/ra/fsp/src/r_usb_pmsc/r_usb_pmsc_descriptor.c.template b/ra/fsp/src/r_usb_pmsc/r_usb_pmsc_descriptor.c.template index e5034b60d..3a32e3cf5 100644 --- a/ra/fsp/src/r_usb_pmsc/r_usb_pmsc_descriptor.c.template +++ b/ra/fsp/src/r_usb_pmsc/r_usb_pmsc_descriptor.c.template @@ -6,7 +6,9 @@ #include /* HMSC Sample use */ #include /* HMSC Sample use */ -#include "r_usb_pmsc_apl.h" +#include "bsp_api.h" +#include "r_usb_basic_api.h" +#include "r_usb_pmsc_api.h" /****************************************************************************** * Macro definitions diff --git a/ra/fsp/src/r_wdt/r_wdt.c b/ra/fsp/src/r_wdt/r_wdt.c index 00f04f45a..9d4647f50 100644 --- a/ra/fsp/src/r_wdt/r_wdt.c +++ b/ra/fsp/src/r_wdt/r_wdt.c @@ -102,7 +102,7 @@ **********************************************************************************************************************/ #if defined(__ARMCC_VERSION) || defined(__ICCARM__) -typedef void (BSP_CMSE_NONSECURE_CALL * volatile wdt_prv_ns_callback)(wdt_callback_args_t * p_args); +typedef void (BSP_CMSE_NONSECURE_CALL * wdt_prv_ns_callback)(wdt_callback_args_t * p_args); #elif defined(__GNUC__) typedef BSP_CMSE_NONSECURE_CALL void (*volatile wdt_prv_ns_callback)(wdt_callback_args_t * p_args); #endif @@ -179,7 +179,6 @@ static volatile wdt_instance_ctrl_t * gp_wdt_ctrl = NULL; **********************************************************************************************************************/ /** Watchdog implementation of WDT Driver */ -/*LDRA_INSPECTED 27 D This structure must be accessible in user code. It cannot be static. */ const wdt_api_t g_wdt_on_wdt = { .open = R_WDT_Open, diff --git a/ra/fsp/src/rm_aws_pkcs11_pal_littlefs/rm_aws_pkcs11_pal_littlefs.c b/ra/fsp/src/rm_aws_pkcs11_pal_littlefs/rm_aws_pkcs11_pal_littlefs.c index aa0b7a232..bb7408717 100644 --- a/ra/fsp/src/rm_aws_pkcs11_pal_littlefs/rm_aws_pkcs11_pal_littlefs.c +++ b/ra/fsp/src/rm_aws_pkcs11_pal_littlefs/rm_aws_pkcs11_pal_littlefs.c @@ -205,7 +205,7 @@ CK_OBJECT_HANDLE PKCS11_PAL_FindObject (CK_BYTE_PTR pxLabel, CK_ULONG usLength) * @param[out] pIsPrivate Boolean indicating if value is private (CK_TRUE) * or exportable (CK_FALSE) * - * @return CKR_OK if operation was successful. CKR_KEY_HANDLE_INVALID ifshit + * @return CKR_OK if operation was successful. CKR_KEY_HANDLE_INVALID if * no such object handle was found, CKR_DEVICE_MEMORY if memory for * buffer could not be allocated, CKR_FUNCTION_FAILED for device driver * error. @@ -216,7 +216,7 @@ CK_RV PKCS11_PAL_GetObjectValue (CK_OBJECT_HANDLE xHandle, CK_ULONG_PTR pulDataSize, CK_BBOOL * pIsPrivate) { - BaseType_t xReturn = CKR_FUNCTION_FAILED; + CK_RV xReturn = CKR_FUNCTION_FAILED; CK_OBJECT_HANDLE xHandleStorage = xHandle; if (xHandle != eInvalidHandle) diff --git a/ra/fsp/src/rm_block_media_sdmmc/rm_block_media_sdmmc.c b/ra/fsp/src/rm_block_media_sdmmc/rm_block_media_sdmmc.c index 7456df670..d27f0726b 100644 --- a/ra/fsp/src/rm_block_media_sdmmc/rm_block_media_sdmmc.c +++ b/ra/fsp/src/rm_block_media_sdmmc/rm_block_media_sdmmc.c @@ -37,8 +37,8 @@ * Typedef definitions **********************************************************************************************************************/ #if defined(__ARMCC_VERSION) || defined(__ICCARM__) -typedef void (BSP_CMSE_NONSECURE_CALL * volatile rm_block_media_sdmmc_prv_ns_callback)(rm_block_media_callback_args_t * - p_args); +typedef void (BSP_CMSE_NONSECURE_CALL * rm_block_media_sdmmc_prv_ns_callback)(rm_block_media_callback_args_t * + p_args); #elif defined(__GNUC__) typedef BSP_CMSE_NONSECURE_CALL void (*volatile rm_block_media_sdmmc_prv_ns_callback)(rm_block_media_callback_args_t * p_args); diff --git a/ra/fsp/src/rm_freertos_plus_fat/rm_freertos_plus_fat.c b/ra/fsp/src/rm_freertos_plus_fat/rm_freertos_plus_fat.c index 717cb1681..057f3f40d 100644 --- a/ra/fsp/src/rm_freertos_plus_fat/rm_freertos_plus_fat.c +++ b/ra/fsp/src/rm_freertos_plus_fat/rm_freertos_plus_fat.c @@ -79,7 +79,6 @@ const fsp_version_t g_rm_freertos_plus_fat_version = }; /** FAT HAL API mapping for FreeRTOS_plus_FAT Controller interface */ -/*LDRA_INSPECTED 27 D This structure must be accessible in user code. It cannot be static. */ const rm_freertos_plus_fat_api_t g_fat_on_freertos = { .open = RM_FREERTOS_PLUS_FAT_Open, diff --git a/ra/fsp/src/rm_motor_current/rm_motor_current.c b/ra/fsp/src/rm_motor_current/rm_motor_current.c new file mode 100644 index 000000000..5bcbb0e80 --- /dev/null +++ b/ra/fsp/src/rm_motor_current/rm_motor_current.c @@ -0,0 +1,984 @@ +/*********************************************************************************************************************** + * Copyright [2020] Renesas Electronics Corporation and/or its affiliates. All Rights Reserved. + * + * This software and documentation are supplied by Renesas Electronics America Inc. and may only be used with products + * of Renesas Electronics Corp. and its affiliates ("Renesas"). No other uses are authorized. Renesas products are + * sold pursuant to Renesas terms and conditions of sale. Purchasers are solely responsible for the selection and use + * of Renesas products and Renesas assumes no liability. No license, express or implied, to any intellectual property + * right is granted by Renesas. This software is protected under all applicable laws, including copyright laws. Renesas + * reserves the right to change or discontinue this software and/or this documentation. THE SOFTWARE AND DOCUMENTATION + * IS DELIVERED TO YOU "AS IS," AND RENESAS MAKES NO REPRESENTATIONS OR WARRANTIES, AND TO THE FULLEST EXTENT + * PERMISSIBLE UNDER APPLICABLE LAW, DISCLAIMS ALL WARRANTIES, WHETHER EXPLICITLY OR IMPLICITLY, INCLUDING WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NONINFRINGEMENT, WITH RESPECT TO THE SOFTWARE OR + * DOCUMENTATION. RENESAS SHALL HAVE NO LIABILITY ARISING OUT OF ANY SECURITY VULNERABILITY OR BREACH. TO THE MAXIMUM + * EXTENT PERMITTED BY LAW, IN NO EVENT WILL RENESAS BE LIABLE TO YOU IN CONNECTION WITH THE SOFTWARE OR DOCUMENTATION + * (OR ANY PERSON OR ENTITY CLAIMING RIGHTS DERIVED FROM YOU) FOR ANY LOSS, DAMAGES, OR CLAIMS WHATSOEVER, INCLUDING, + * WITHOUT LIMITATION, ANY DIRECT, CONSEQUENTIAL, SPECIAL, INDIRECT, PUNITIVE, OR INCIDENTAL DAMAGES; ANY LOST PROFITS, + * OTHER ECONOMIC DAMAGE, PROPERTY DAMAGE, OR PERSONAL INJURY; AND EVEN IF RENESAS HAS BEEN ADVISED OF THE POSSIBILITY + * OF SUCH LOSS, DAMAGES, CLAIMS OR COSTS. + **********************************************************************************************************************/ + +/*********************************************************************************************************************** + * Includes + **********************************************************************************************************************/ +#include +#include +#include "rm_motor_current.h" +#include "rm_motor_current_library.h" +#include "bsp_api.h" +#include "bsp_cfg.h" + +/*********************************************************************************************************************** + * Macro definitions + **********************************************************************************************************************/ + +#define MOTOR_CURRENT_OPEN (0X4D435043L) + +#define MOTOR_CURRENT_FLG_CLR (0) /* For flag clear */ +#define MOTOR_CURRENT_FLG_SET (1) /* For flag set */ + +#define MOTOR_CURRENT_TWOPI (3.14159265358979F * 2.0F) +#define MOTOR_CURRENT_60_TWOPI (60.0F / MOTOR_CURRENT_TWOPI) /* To translate rad/s => rpm */ +#define MOTOR_CURRENT_SQRT_2 (1.41421356F) /* Sqrt(2) */ +#define MOTOR_CURRENT_SQRT_3 (1.7320508F) /* Sqrt(3) */ +#define MOTOR_CURRENT_DIV_KHZ (0.001F) + +#ifndef MOTOR_CURRENT_ERROR_RETURN + #define MOTOR_CURRENT_ERROR_RETURN(a, err) FSP_ERROR_RETURN((a), (err)) +#endif + +/*********************************************************************************************************************** + * Typedef definitions + **********************************************************************************************************************/ + +/*********************************************************************************************************************** + * Private function prototypes + **********************************************************************************************************************/ + +/*******************************************/ +/* A/D conversion finish interrupt routine */ +/*******************************************/ +/* Process from "A/D conversion" to "PWM Moduration" */ +void rm_motor_current_cyclic(motor_driver_callback_args_t * p_args); + +/* Process to get rotor angle and speed information from angle module */ +static void motor_current_angle_cyclic(motor_current_instance_t * p_instance); + +/* static functions */ +static void motor_current_reset(motor_current_instance_ctrl_t * p_ctrl); +static float motor_current_limit_abs(float f4_value, float f4_limit_value); +static void motor_current_pi_calculation(motor_current_instance_ctrl_t * p_ctrl); +static float motor_current_pi_control(motor_current_pi_params_t * pi_ctrl); +static void motor_current_decoupling(motor_current_instance_ctrl_t * p_ctrl, + float f_speed_rad, + const motor_current_motor_parameter_t * p_mtr); +static void motor_current_voltage_limit(motor_current_instance_ctrl_t * p_ctrl); +static void motor_current_transform_uvw_dq_abs(const float f_angle, const float * f_uvw, float * f_dq); +static void motor_current_transform_dq_uvw_abs(const float f_angle, const float * f_dq, float * f_uvw); + +/*********************************************************************************************************************** + * Private global variables + **********************************************************************************************************************/ + +/* Version data structure. */ +static const fsp_version_t g_motor_current_version = +{ + .api_version_minor = MOTOR_CURRENT_API_VERSION_MINOR, + .api_version_major = MOTOR_CURRENT_API_VERSION_MAJOR, + .code_version_major = MOTOR_CURRENT_CODE_VERSION_MAJOR, + .code_version_minor = MOTOR_CURRENT_CODE_VERSION_MINOR +}; + +/*********************************************************************************************************************** + * Global variables + **********************************************************************************************************************/ +const motor_current_api_t g_motor_current_on_motor_current = +{ + .open = RM_MOTOR_CURRENT_Open, + .close = RM_MOTOR_CURRENT_Close, + .reset = RM_MOTOR_CURRENT_Reset, + .run = RM_MOTOR_CURRENT_Run, + .parameterSet = RM_MOTOR_CURRENT_ParameterSet, + .currentReferenceSet = RM_MOTOR_CURRENT_CurrentReferenceSet, + .speedPhaseSet = RM_MOTOR_CURRENT_SpeedPhaseSet, + .currentSet = RM_MOTOR_CURRENT_CurrentSet, + .parameterGet = RM_MOTOR_CURRENT_ParameterGet, + .currentGet = RM_MOTOR_CURRENT_CurrentGet, + .phaseVoltageGet = RM_MOTOR_CURRENT_PhaseVoltageGet, + .parameterUpdate = RM_MOTOR_CURRENT_ParameterUpdate, + .versionGet = RM_MOTOR_CURRENT_VersionGet +}; + +/*******************************************************************************************************************//** + * @addtogroup MOTOR_CURRENT + * @{ + **********************************************************************************************************************/ + +/*********************************************************************************************************************** + * Functions + **********************************************************************************************************************/ + +/*******************************************************************************************************************//** + * @brief Opens and configures the Motor Current Module. Implements @ref motor_current_api_t::open. + * + * @retval FSP_SUCCESS Motor Current 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_MOTOR_CURRENT_Open (motor_current_ctrl_t * const p_ctrl, motor_current_cfg_t const * const p_cfg) +{ + fsp_err_t err = FSP_SUCCESS; + motor_current_instance_ctrl_t * p_instance_ctrl = (motor_current_instance_ctrl_t *) p_ctrl; + +#if MOTOR_CURRENT_CFG_PARAM_CHECKING_ENABLE + FSP_ASSERT(NULL != p_instance_ctrl); + FSP_ASSERT(NULL != p_cfg); +#endif + + motor_current_extended_cfg_t * p_extended_cfg = (motor_current_extended_cfg_t *) p_cfg->p_extend; + +#if MOTOR_CURRENT_CFG_PARAM_CHECKING_ENABLE + FSP_ASSERT(NULL != p_extended_cfg); + float check_period = 0.0F; + check_period = p_extended_cfg->f_current_ctrl_period * MOTOR_CURRENT_DIV_KHZ; + + MOTOR_CURRENT_ERROR_RETURN(check_period >= 0.0F, FSP_ERR_INVALID_ARGUMENT); +#endif + + p_instance_ctrl->p_driver_instance = p_cfg->p_motor_driver_instance; + p_instance_ctrl->p_angle_instance = p_cfg->p_motor_angle_instance; + + FSP_ERROR_RETURN(MOTOR_CURRENT_OPEN != p_instance_ctrl->open, FSP_ERR_ALREADY_OPEN); + + p_instance_ctrl->p_cfg = p_cfg; + + motor_current_reset(p_instance_ctrl); + + p_instance_ctrl->st_pi_id.f_ilimit = p_extended_cfg->f_ilimit; + p_instance_ctrl->st_pi_iq.f_ilimit = p_extended_cfg->f_ilimit; + + rm_motor_current_pi_gain_calc(p_extended_cfg->p_motor_parameter, + p_extended_cfg->p_design_parameter, + &(p_instance_ctrl->st_pi_id), + &(p_instance_ctrl->st_pi_iq), + p_extended_cfg->f_current_ctrl_period * MOTOR_CURRENT_DIV_KHZ); + + p_instance_ctrl->st_vcomp.f_comp_v[0] = p_extended_cfg->f_comp_v[0]; + p_instance_ctrl->st_vcomp.f_comp_v[1] = p_extended_cfg->f_comp_v[1]; + p_instance_ctrl->st_vcomp.f_comp_v[2] = p_extended_cfg->f_comp_v[2]; + p_instance_ctrl->st_vcomp.f_comp_v[3] = p_extended_cfg->f_comp_v[3]; + p_instance_ctrl->st_vcomp.f_comp_v[4] = p_extended_cfg->f_comp_v[4]; + p_instance_ctrl->st_vcomp.f_comp_i[0] = p_extended_cfg->f_comp_i[0]; + p_instance_ctrl->st_vcomp.f_comp_i[1] = p_extended_cfg->f_comp_i[1]; + p_instance_ctrl->st_vcomp.f_comp_i[2] = p_extended_cfg->f_comp_i[2]; + p_instance_ctrl->st_vcomp.f_comp_i[3] = p_extended_cfg->f_comp_i[3]; + p_instance_ctrl->st_vcomp.f_comp_i[4] = p_extended_cfg->f_comp_i[4]; + p_instance_ctrl->st_vcomp.u1_volt_err_comp_enable = p_extended_cfg->vcomp_enable; + rm_motor_voltage_error_compensation_init(&(p_instance_ctrl->st_vcomp)); + + /* Open Motor Driver Access Module */ + if (p_instance_ctrl->p_driver_instance != NULL) + { + p_instance_ctrl->p_driver_instance->p_api->open(p_instance_ctrl->p_driver_instance->p_ctrl, + p_instance_ctrl->p_driver_instance->p_cfg); + } + + /* Open Motor Angle Module */ + if (p_instance_ctrl->p_angle_instance != NULL) + { + p_instance_ctrl->p_angle_instance->p_api->open(p_instance_ctrl->p_angle_instance->p_ctrl, + p_instance_ctrl->p_angle_instance->p_cfg); + } + + /* Mark driver as open */ + p_instance_ctrl->open = MOTOR_CURRENT_OPEN; + + return err; +} + +/*******************************************************************************************************************//** + * @brief Disables specified Motor Current Module. Implements @ref motor_current_api_t::close. + * + * @retval FSP_SUCCESS Successfully closed. + * @retval FSP_ERR_ASSERTION Null pointer. + * @retval FSP_ERR_NOT_OPEN Module is not open. + **********************************************************************************************************************/ +fsp_err_t RM_MOTOR_CURRENT_Close (motor_current_ctrl_t * const p_ctrl) +{ + fsp_err_t err = FSP_SUCCESS; + motor_current_instance_ctrl_t * p_instance_ctrl = (motor_current_instance_ctrl_t *) p_ctrl; + +#if MOTOR_CURRENT_CFG_PARAM_CHECKING_ENABLE + FSP_ASSERT(p_instance_ctrl); + MOTOR_CURRENT_ERROR_RETURN(MOTOR_CURRENT_OPEN == p_instance_ctrl->open, FSP_ERR_NOT_OPEN); +#endif + motor_current_reset(p_instance_ctrl); + + rm_motor_voltage_error_compensation_init(&(p_instance_ctrl->st_vcomp)); + + /* close motor driver access */ + if (p_instance_ctrl->p_driver_instance != NULL) + { + p_instance_ctrl->p_driver_instance->p_api->close(p_instance_ctrl->p_driver_instance->p_ctrl); + } + + /* close motor angle */ + if (p_instance_ctrl->p_angle_instance != NULL) + { + p_instance_ctrl->p_angle_instance->p_api->close(p_instance_ctrl->p_angle_instance->p_ctrl); + } + + p_instance_ctrl->open = 0U; + + return err; +} + +/*******************************************************************************************************************//** + * @brief Reset variables of Motor Current Module. Implements @ref motor_current_api_t::reset. + * + * @retval FSP_SUCCESS Successfully reset. + * @retval FSP_ERR_ASSERTION Null pointer. + * @retval FSP_ERR_NOT_OPEN Module is not open. + **********************************************************************************************************************/ +fsp_err_t RM_MOTOR_CURRENT_Reset (motor_current_ctrl_t * const p_ctrl) +{ + fsp_err_t err = FSP_SUCCESS; + motor_current_instance_ctrl_t * p_instance_ctrl = (motor_current_instance_ctrl_t *) p_ctrl; + +#if MOTOR_CURRENT_CFG_PARAM_CHECKING_ENABLE + FSP_ASSERT(p_instance_ctrl); + MOTOR_CURRENT_ERROR_RETURN(MOTOR_CURRENT_OPEN == p_instance_ctrl->open, FSP_ERR_NOT_OPEN); +#endif + + p_instance_ctrl->u1_active = MOTOR_CURRENT_FLG_CLR; + + /* reset motor driver access */ + if (p_instance_ctrl->p_driver_instance != NULL) + { + p_instance_ctrl->p_driver_instance->p_api->reset(p_instance_ctrl->p_driver_instance->p_ctrl); + } + + motor_current_reset(p_instance_ctrl); + + rm_motor_voltage_error_compensation_reset(&(p_instance_ctrl->st_vcomp)); + + /* reset motor angle */ + if (p_instance_ctrl->p_angle_instance != NULL) + { + p_instance_ctrl->p_angle_instance->p_api->reset(p_instance_ctrl->p_angle_instance->p_ctrl); + } + + return err; +} + +/*******************************************************************************************************************//** + * @brief Run(Start) the Current Control. Implements @ref motor_current_api_t::run + * + * @retval FSP_SUCCESS Successfully run. + * @retval FSP_ERR_ASSERTION Null pointer. + * @retval FSP_ERR_NOT_OPEN Module is not open. + **********************************************************************************************************************/ +fsp_err_t RM_MOTOR_CURRENT_Run (motor_current_ctrl_t * const p_ctrl) +{ + fsp_err_t err = FSP_SUCCESS; + motor_current_instance_ctrl_t * p_instance_ctrl = (motor_current_instance_ctrl_t *) p_ctrl; + +#if MOTOR_CURRENT_CFG_PARAM_CHECKING_ENABLE + FSP_ASSERT(p_instance_ctrl); + MOTOR_CURRENT_ERROR_RETURN(MOTOR_CURRENT_OPEN == p_instance_ctrl->open, FSP_ERR_NOT_OPEN); +#endif + + p_instance_ctrl->u1_active = MOTOR_CURRENT_FLG_SET; + + return err; +} + +/*******************************************************************************************************************//** + * @brief Set (Input) Parameter Data. Implements @ref motor_current_api_t::parameterSet + * + * @retval FSP_SUCCESS Successfully data is set. + * @retval FSP_ERR_ASSERTION Null pointer. + * @retval FSP_ERR_NOT_OPEN Module is not open. + * @retval FSP_ERR_INVALID_ARGUMENT Input argument error. + **********************************************************************************************************************/ +fsp_err_t RM_MOTOR_CURRENT_ParameterSet (motor_current_ctrl_t * const p_ctrl, + motor_current_input_t const * const p_st_input) +{ + fsp_err_t err = FSP_SUCCESS; + motor_current_instance_ctrl_t * p_instance_ctrl = (motor_current_instance_ctrl_t *) p_ctrl; + +#if MOTOR_CURRENT_CFG_PARAM_CHECKING_ENABLE + FSP_ASSERT(p_instance_ctrl); + MOTOR_CURRENT_ERROR_RETURN(MOTOR_CURRENT_OPEN == p_instance_ctrl->open, FSP_ERR_NOT_OPEN); + MOTOR_CURRENT_ERROR_RETURN(p_st_input != NULL, FSP_ERR_INVALID_ARGUMENT); +#endif + + p_instance_ctrl->st_input = *p_st_input; + + return err; +} + +/*******************************************************************************************************************//** + * @brief Set Current Reference Data. Implements @ref motor_current_api_t::currentReferenceSet + * + * @retval FSP_SUCCESS Successfully data is set. + * @retval FSP_ERR_ASSERTION Null pointer. + * @retval FSP_ERR_NOT_OPEN Module is not open. + **********************************************************************************************************************/ +fsp_err_t RM_MOTOR_CURRENT_CurrentReferenceSet (motor_current_ctrl_t * const p_ctrl, + float const id_reference, + float const iq_reference) +{ + fsp_err_t err = FSP_SUCCESS; + motor_current_instance_ctrl_t * p_instance_ctrl = (motor_current_instance_ctrl_t *) p_ctrl; + +#if MOTOR_CURRENT_CFG_PARAM_CHECKING_ENABLE + FSP_ASSERT(p_instance_ctrl); + MOTOR_CURRENT_ERROR_RETURN(MOTOR_CURRENT_OPEN == p_instance_ctrl->open, FSP_ERR_NOT_OPEN); +#endif + + p_instance_ctrl->f_id_ref = id_reference; + p_instance_ctrl->f_iq_ref = iq_reference; + + return err; +} + +/*******************************************************************************************************************//** + * @brief Set Current Speed & rotor phase Data. Implements @ref motor_current_api_t::speedPhaseSet + * + * @retval FSP_SUCCESS Successfully data is set. + * @retval FSP_ERR_ASSERTION Null pointer. + * @retval FSP_ERR_NOT_OPEN Module is not open. + **********************************************************************************************************************/ +fsp_err_t RM_MOTOR_CURRENT_SpeedPhaseSet (motor_current_ctrl_t * const p_ctrl, float const speed, float const phase) +{ + fsp_err_t err = FSP_SUCCESS; + motor_current_instance_ctrl_t * p_instance_ctrl = (motor_current_instance_ctrl_t *) p_ctrl; + +#if MOTOR_CURRENT_CFG_PARAM_CHECKING_ENABLE + FSP_ASSERT(p_instance_ctrl); + MOTOR_CURRENT_ERROR_RETURN(MOTOR_CURRENT_OPEN == p_instance_ctrl->open, FSP_ERR_NOT_OPEN); +#endif + + p_instance_ctrl->f_speed_rad = speed; + p_instance_ctrl->f_rotor_angle = phase; + + return err; +} + +/*******************************************************************************************************************//** + * @brief Set d/q-axis Current & Voltage Data. Implements @ref motor_current_api_t::currentSet + * + * @retval FSP_SUCCESS Successfully data is set. + * @retval FSP_ERR_ASSERTION Null pointer. + * @retval FSP_ERR_NOT_OPEN Module is not open. + * @retval FSP_ERR_INVALID_ARGUMENT Input parameter error. + **********************************************************************************************************************/ +fsp_err_t RM_MOTOR_CURRENT_CurrentSet (motor_current_ctrl_t * const p_ctrl, + motor_current_input_current_t const * const p_st_current, + motor_current_input_voltage_t const * const p_st_voltage) +{ + fsp_err_t err = FSP_SUCCESS; + motor_current_instance_ctrl_t * p_instance_ctrl = (motor_current_instance_ctrl_t *) p_ctrl; + +#if MOTOR_CURRENT_CFG_PARAM_CHECKING_ENABLE + FSP_ASSERT(p_instance_ctrl); + MOTOR_CURRENT_ERROR_RETURN(MOTOR_CURRENT_OPEN == p_instance_ctrl->open, FSP_ERR_NOT_OPEN); + MOTOR_CURRENT_ERROR_RETURN(p_st_voltage->vdc >= 0.0F, FSP_ERR_INVALID_ARGUMENT); +#endif + + p_instance_ctrl->f_iu_ad = p_st_current->iu; + p_instance_ctrl->f_iv_ad = p_st_current->iv; + p_instance_ctrl->f_iw_ad = p_st_current->iw; + p_instance_ctrl->f_vdc_ad = p_st_voltage->vdc; + p_instance_ctrl->f_va_max = p_st_voltage->va_max; + + return err; +} + +/*******************************************************************************************************************//** + * @brief Get Output Parameters. Implements @ref motor_current_api_t::parameterGet + * + * @retval FSP_SUCCESS Successful data get. + * @retval FSP_ERR_ASSERTION Null pointer. + * @retval FSP_ERR_NOT_OPEN Module is not open. + * @retval FSP_ERR_INVALID_ARGUMENT Input parameter error. + **********************************************************************************************************************/ +fsp_err_t RM_MOTOR_CURRENT_ParameterGet (motor_current_ctrl_t * const p_ctrl, + motor_current_output_t * const p_st_output) +{ + fsp_err_t err = FSP_SUCCESS; + motor_current_instance_ctrl_t * p_instance_ctrl = (motor_current_instance_ctrl_t *) p_ctrl; + +#if MOTOR_CURRENT_CFG_PARAM_CHECKING_ENABLE + FSP_ASSERT(p_instance_ctrl); + MOTOR_CURRENT_ERROR_RETURN(MOTOR_CURRENT_OPEN == p_instance_ctrl->open, FSP_ERR_NOT_OPEN); + MOTOR_CURRENT_ERROR_RETURN(p_st_output != NULL, FSP_ERR_INVALID_ARGUMENT); +#endif + + motor_current_extended_cfg_t * p_extended_cfg = + (motor_current_extended_cfg_t *) p_instance_ctrl->p_cfg->p_extend; + + p_st_output->f_id = p_instance_ctrl->f_id_ad; + p_st_output->f_iq = p_instance_ctrl->f_iq_ad; + p_st_output->f_vamax = p_instance_ctrl->f_va_max; + p_st_output->f_speed_rad = p_instance_ctrl->f_speed_rad; + p_st_output->f_speed_rpm = p_instance_ctrl->f_speed_rad / p_extended_cfg->p_motor_parameter->u2_mtr_pp * + MOTOR_CURRENT_60_TWOPI; + p_st_output->f_rotor_angle = p_instance_ctrl->f_rotor_angle; + p_st_output->f_ed = p_instance_ctrl->f_ed; + p_st_output->f_eq = p_instance_ctrl->f_eq; + p_st_output->f_phase_err_rad = p_instance_ctrl->f_phase_err; + p_st_output->u1_flag_get_iref = p_instance_ctrl->u1_flag_crnt_offset; + + return err; +} + +/*******************************************************************************************************************//** + * @brief Get d/q-axis Current. Implements @ref motor_current_api_t::currentGet + * + * @retval FSP_SUCCESS Successful data get. + * @retval FSP_ERR_ASSERTION Null pointer. + * @retval FSP_ERR_NOT_OPEN Module is not open. + * @retval FSP_ERR_INVALID_ARGUMENT Input parameter error. + **********************************************************************************************************************/ +fsp_err_t RM_MOTOR_CURRENT_CurrentGet (motor_current_ctrl_t * const p_ctrl, float * const p_id, float * const p_iq) +{ + fsp_err_t err = FSP_SUCCESS; + motor_current_instance_ctrl_t * p_instance_ctrl = (motor_current_instance_ctrl_t *) p_ctrl; + +#if MOTOR_CURRENT_CFG_PARAM_CHECKING_ENABLE + FSP_ASSERT(p_instance_ctrl); + MOTOR_CURRENT_ERROR_RETURN(MOTOR_CURRENT_OPEN == p_instance_ctrl->open, FSP_ERR_NOT_OPEN); + MOTOR_CURRENT_ERROR_RETURN(p_id != NULL, FSP_ERR_INVALID_ARGUMENT); + MOTOR_CURRENT_ERROR_RETURN(p_iq != NULL, FSP_ERR_INVALID_ARGUMENT); +#endif + + motor_current_transform_uvw_dq_abs(p_instance_ctrl->f_rotor_angle, + &(p_instance_ctrl->f_iu_ad), + &(p_instance_ctrl->f_id_ad)); + + *p_id = p_instance_ctrl->f_id_ad; + *p_iq = p_instance_ctrl->f_iq_ad; + + return err; +} + +/*******************************************************************************************************************//** + * @brief Gets the set phase voltage. Implements @ref motor_current_api_t::phaseVoltageGet + * + * @retval FSP_SUCCESS Successful data calculation. + * @retval FSP_ERR_ASSERTION Null pointer. + * @retval FSP_ERR_NOT_OPEN Module is not open. + * @retval FSP_ERR_INVALID_ARGUMENT Input parameter error. + **********************************************************************************************************************/ +fsp_err_t RM_MOTOR_CURRENT_PhaseVoltageGet (motor_current_ctrl_t * const p_ctrl, + motor_current_get_voltage_t * const p_voltage) +{ + float f4_iuvw_ref[3] = {0.0F}; + fsp_err_t err = FSP_SUCCESS; + motor_current_instance_ctrl_t * p_instance_ctrl = (motor_current_instance_ctrl_t *) p_ctrl; + +#if MOTOR_CURRENT_CFG_PARAM_CHECKING_ENABLE + FSP_ASSERT(p_instance_ctrl); + MOTOR_CURRENT_ERROR_RETURN(MOTOR_CURRENT_OPEN == p_instance_ctrl->open, FSP_ERR_NOT_OPEN); + MOTOR_CURRENT_ERROR_RETURN(p_voltage != NULL, FSP_ERR_INVALID_ARGUMENT); +#endif + + motor_current_extended_cfg_t * p_extended_cfg = + (motor_current_extended_cfg_t *) p_instance_ctrl->p_cfg->p_extend; + + /*====================================*/ + /* Feedback control (Current) */ + /*====================================*/ + motor_current_pi_calculation(p_instance_ctrl); + + /*============================*/ + /* Decoupling control */ + /*============================*/ + motor_current_decoupling(p_instance_ctrl, p_instance_ctrl->f_speed_rad, p_extended_cfg->p_motor_parameter); + + /*============================*/ + /* Limit voltage vector */ + /*============================*/ + motor_current_voltage_limit(p_instance_ctrl); + p_voltage->vd_reference = p_instance_ctrl->f_vd_ref; + p_voltage->vq_reference = p_instance_ctrl->f_vq_ref; + + /*=================================================*/ + /* Coordinate transformation (dq->uvw) */ + /*=================================================*/ + motor_current_transform_dq_uvw_abs(p_instance_ctrl->f_rotor_angle, + &(p_instance_ctrl->f_vd_ref), + &(p_instance_ctrl->f_refu)); + + /*====================================*/ + /* Voltage error compensation */ + /*====================================*/ + motor_current_transform_dq_uvw_abs(p_instance_ctrl->f_rotor_angle, &(p_instance_ctrl->f_id_ref), &(f4_iuvw_ref[0])); + + rm_motor_voltage_error_compensation_main(&(p_instance_ctrl->st_vcomp), + &(p_instance_ctrl->f_refu), + &(f4_iuvw_ref[0]), + p_instance_ctrl->f_vdc_ad); + + p_voltage->u_voltage = p_instance_ctrl->f_refu; + p_voltage->v_voltage = p_instance_ctrl->f_refv; + p_voltage->w_voltage = p_instance_ctrl->f_refw; + + return err; +} + +/*******************************************************************************************************************//** + * @brief Update the parameters of Current Control. Implements @ref motor_current_api_t::parameterUpdate + * + * @retval FSP_SUCCESS Successfully data was updated. + * @retval FSP_ERR_ASSERTION Null pointer. + * @retval FSP_ERR_NOT_OPEN Module is not open. + **********************************************************************************************************************/ +fsp_err_t RM_MOTOR_CURRENT_ParameterUpdate (motor_current_ctrl_t * const p_ctrl, + motor_current_cfg_t const * const p_cfg) +{ + fsp_err_t err = FSP_SUCCESS; + motor_current_instance_ctrl_t * p_instance_ctrl = (motor_current_instance_ctrl_t *) p_ctrl; + +#if MOTOR_CURRENT_CFG_PARAM_CHECKING_ENABLE + FSP_ASSERT(p_instance_ctrl); + MOTOR_CURRENT_ERROR_RETURN(MOTOR_CURRENT_OPEN == p_instance_ctrl->open, FSP_ERR_NOT_OPEN); + FSP_ASSERT(p_cfg); +#endif + motor_current_extended_cfg_t * p_extended_cfg = (motor_current_extended_cfg_t *) p_cfg->p_extend; + + p_instance_ctrl->p_cfg = p_cfg; + + p_instance_ctrl->st_pi_id.f_ilimit = p_extended_cfg->f_ilimit; + p_instance_ctrl->st_pi_iq.f_ilimit = p_extended_cfg->f_ilimit; + + rm_motor_current_pi_gain_calc(p_extended_cfg->p_motor_parameter, + p_extended_cfg->p_design_parameter, + &(p_instance_ctrl->st_pi_id), + &(p_instance_ctrl->st_pi_iq), + p_extended_cfg->f_current_ctrl_period * MOTOR_CURRENT_DIV_KHZ); + + rm_motor_voltage_error_compensation_init(&(p_instance_ctrl->st_vcomp)); + + return err; +} + +/*******************************************************************************************************************//** + * Return Motor Speed Control module version. Implements @ref motor_current_api_t::versionGet. + * + * @retval FSP_SUCCESS Version information successfully read. + * @retval FSP_ERR_ASSERTION Null pointer passed as a parameter + **********************************************************************************************************************/ +fsp_err_t RM_MOTOR_CURRENT_VersionGet (fsp_version_t * const p_version) +{ +#if MOTOR_CURRENT_CFG_PARAM_CHECKING_ENABLE + + /* Verify parameters are valid */ + FSP_ASSERT(NULL != p_version); +#endif + + p_version->version_id = g_motor_current_version.version_id; + + return FSP_SUCCESS; +} + +/*******************************************************************************************************************//** + * @} (end addtogroup MOTOR_CURRENT) + **********************************************************************************************************************/ + +/*********************************************************************************************************************** + * Private Functions + **********************************************************************************************************************/ + +/*********************************************************************************************************************** + * Function Name : rm_motor_current_cyclic + * Description : Cyclic Process of Current Control (Performed at ADC Finish Interrupt) + * Arguments : p_args - The pointer to used arguments + * Return Value : None + **********************************************************************************************************************/ +void rm_motor_current_cyclic (motor_driver_callback_args_t * p_args) +{ + float f_iu_ad = 0.0F; + float f_iv_ad = 0.0F; + float f_iw_ad = 0.0F; + float f_vdc_ad = 0.0F; + float f_ref[3] = {0.0F}; + motor_driver_current_get_t temp_drv_crnt_get; + + motor_current_instance_t * p_instance = (motor_current_instance_t *) p_args->p_context; + motor_current_instance_ctrl_t * p_instance_ctrl = (motor_current_instance_ctrl_t *) p_instance->p_ctrl; + motor_driver_instance_t const * p_driver_instance = p_instance_ctrl->p_driver_instance; + + motor_current_callback_args_t temp_args_t; + + motor_current_input_current_t temp_input_current; + motor_current_input_voltage_t temp_input_voltage; + motor_current_get_voltage_t temp_get_voltage; + + switch (p_args->event) + { + case MOTOR_DRIVER_EVENT_FORWARD: + { + /* Invoke the callback function if it is set. */ + if (NULL != p_instance->p_cfg->p_callback) + { + temp_args_t.event = MOTOR_CURRENT_EVENT_FORWARD; + temp_args_t.p_context = p_instance->p_cfg->p_context; + (p_instance->p_cfg->p_callback)(&temp_args_t); + } + + break; + } + + /* Current Control Timing */ + case MOTOR_DRIVER_EVENT_CURRENT: + { + /* Get A/D coverted data */ + p_driver_instance->p_api->currentGet(p_driver_instance->p_ctrl, &temp_drv_crnt_get); + f_iu_ad = temp_drv_crnt_get.iu; + f_iw_ad = temp_drv_crnt_get.iw; + f_vdc_ad = temp_drv_crnt_get.vdc; + p_instance_ctrl->f_va_max = temp_drv_crnt_get.va_max; + + f_iv_ad = -(f_iu_ad + f_iw_ad); + + /**********************************/ + /* Active Current Control */ + /**********************************/ + if (MOTOR_CURRENT_FLG_SET == p_instance_ctrl->u1_active) + { + /* Measure current offset values */ + p_driver_instance->p_api->flagCurrentOffsetGet(p_driver_instance->p_ctrl, + &(p_instance_ctrl->u1_flag_crnt_offset)); + + /* After current offset was measured */ + if (MOTOR_CURRENT_FLG_SET == p_instance_ctrl->u1_flag_crnt_offset) + { + /* Invoke the callback function if it is set. */ + if (NULL != p_instance->p_cfg->p_callback) + { + temp_args_t.event = MOTOR_CURRENT_EVENT_DATA_SET; + temp_args_t.p_context = p_instance->p_cfg->p_context; + (p_instance->p_cfg->p_callback)(&temp_args_t); + } + + /*=============================================*/ + /* Coordinate transformation (UVW->dq) */ + /*=============================================*/ + temp_input_current.iu = f_iu_ad; + temp_input_current.iv = f_iv_ad; + temp_input_current.iw = f_iw_ad; + temp_input_voltage.vdc = f_vdc_ad; + temp_input_voltage.va_max = p_instance_ctrl->f_va_max; + + p_instance->p_api->currentSet(p_instance_ctrl, &temp_input_current, &temp_input_voltage); + + p_instance->p_api->currentGet(p_instance_ctrl, + &(p_instance_ctrl->f_id_ad), + &(p_instance_ctrl->f_iq_ad)); + + /*==============================*/ + /* Angle & speed process */ + /*==============================*/ + motor_current_angle_cyclic(p_instance); + + p_instance->p_api->speedPhaseSet(p_instance_ctrl, + p_instance_ctrl->f_speed_rad, + p_instance_ctrl->f_rotor_angle); + + /*=========================*/ + /* Current control */ + /*=========================*/ + p_instance->p_api->currentReferenceSet(p_instance_ctrl, + p_instance_ctrl->st_input.f_id_ref, + p_instance_ctrl->st_input.f_iq_ref); + + p_instance->p_api->phaseVoltageGet(p_instance_ctrl, &temp_get_voltage); + + f_ref[0] = temp_get_voltage.u_voltage; + f_ref[1] = temp_get_voltage.v_voltage; + f_ref[2] = temp_get_voltage.w_voltage; + + /*===============================*/ + /* Space vector modulation */ + /*===============================*/ + p_driver_instance->p_api->phaseVoltageSet(p_driver_instance->p_ctrl, f_ref[0], f_ref[1], f_ref[2]); + } + } + + break; + } + + case MOTOR_DRIVER_EVENT_BACKWARD: + { + /* Invoke the callback function if it is set. */ + if (NULL != p_instance->p_cfg->p_callback) + { + temp_args_t.event = MOTOR_CURRENT_EVENT_BACKWARD; + temp_args_t.p_context = p_instance->p_cfg->p_context; + (p_instance->p_cfg->p_callback)(&temp_args_t); + } + + break; + } + + default: + { + break; + } + } +} /* End of function rm_motor_current_cyclic() */ + +/*********************************************************************************************************************** + * Function Name : motor_current_angle_cyclic + * Description : Angle/Speed Process in Cyclic Process of Current Control + * Arguments : p_ctrl - The pointer to current control module control instance + * Return Value : None + **********************************************************************************************************************/ +static void motor_current_angle_cyclic (motor_current_instance_t * p_instance) +{ + motor_current_instance_ctrl_t * p_ctrl = (motor_current_instance_ctrl_t *) p_instance->p_ctrl; + + motor_angle_instance_t const * p_angle = p_ctrl->p_angle_instance; + motor_angle_current_t temp_current; + motor_angle_voltage_reference_t temp_vol_ref; + + temp_current.id = p_ctrl->f_id_ad; + temp_current.iq = p_ctrl->f_iq_ad; + temp_vol_ref.vd = p_ctrl->f_vd_ref; + temp_vol_ref.vq = p_ctrl->f_vq_ref; + + p_angle->p_api->flagPiCtrlSet(p_angle->p_ctrl, p_ctrl->st_input.u1_flag_pi); + p_angle->p_api->speedSet(p_angle->p_ctrl, + p_ctrl->st_input.f_ref_speed_rad_ctrl, + p_ctrl->st_input.f_damp_comp_speed); + p_angle->p_api->currentSet(p_angle->p_ctrl, &temp_current, &temp_vol_ref); + p_angle->p_api->angleSpeedGet(p_angle->p_ctrl, + &(p_ctrl->f_rotor_angle), + &(p_ctrl->f_speed_rad), + &(p_ctrl->f_phase_err)); + p_angle->p_api->estimatedComponentGet(p_angle->p_ctrl, &(p_ctrl->f_ed), &(p_ctrl->f_eq)); +} /* End of function motor_current_angle_cyclic */ + +/*********************************************************************************************************************** + * Function Name : motor_current_reset + * Description : Resets FOC current control module + * Arguments : p_ctrl - The pointer to the FOC current control instance + * Return Value : None + **********************************************************************************************************************/ +static void motor_current_reset (motor_current_instance_ctrl_t * p_ctrl) +{ + p_ctrl->u1_active = MOTOR_CURRENT_FLG_CLR; + p_ctrl->f_vd_ref = 0.0F; + p_ctrl->f_vq_ref = 0.0F; + p_ctrl->f_id_ref = 0.0F; + p_ctrl->f_iq_ref = 0.0F; + p_ctrl->f_id_ad = 0.0F; + p_ctrl->f_iq_ad = 0.0F; + p_ctrl->f_vdc_ad = 0.0F; + p_ctrl->f_speed_rad = 0.0F; + p_ctrl->f_rotor_angle = 0.0F; + p_ctrl->f_refu = 0.0F; + p_ctrl->f_refv = 0.0F; + p_ctrl->f_refw = 0.0F; + + p_ctrl->st_pi_id.f_err = 0.0F; + p_ctrl->st_pi_iq.f_err = 0.0F; + p_ctrl->st_pi_id.f_refi = 0.0F; + p_ctrl->st_pi_iq.f_refi = 0.0F; + + p_ctrl->u1_flag_crnt_offset = MOTOR_CURRENT_FLG_CLR; +} /* End of function motor_current_reset */ + +/*********************************************************************************************************************** + * Function Name : motor_current_limit_abs + * Description : Limit with absolute value + * Arguments : f4_value - Target value + * f4_limit_value - Limit + * Return Value : Limited value + **********************************************************************************************************************/ +static float motor_current_limit_abs (float f4_value, float f4_limit_value) +{ + float f4_temp0; + + f4_temp0 = fabsf(f4_value); + if (f4_temp0 > f4_limit_value) + { + f4_value = copysignf(f4_limit_value, f4_value); + } + + return f4_value; +} /* End of function motor_current_limit_abs */ + +/*********************************************************************************************************************** + * Function Name : motor_current_pi_calculation + * Description : Current PI control + * Calculates the output voltage vector from current vector command and actual current vector + * Arguments : p_ctrl - The pointer to the FOC current control structure + * Return Value : None + **********************************************************************************************************************/ +static void motor_current_pi_calculation (motor_current_instance_ctrl_t * p_ctrl) +{ + /* The d-axis */ + p_ctrl->st_pi_id.f_err = p_ctrl->f_id_ref - p_ctrl->f_id_ad; + p_ctrl->f_vd_ref = motor_current_pi_control(&(p_ctrl->st_pi_id)); + + /* The q-axis */ + p_ctrl->st_pi_iq.f_err = p_ctrl->f_iq_ref - p_ctrl->f_iq_ad; + p_ctrl->f_vq_ref = motor_current_pi_control(&(p_ctrl->st_pi_iq)); +} /* End of function motor_current_pi_calculation */ + +/*********************************************************************************************************************** + * Function Name : motor_current_pi_control + * Description : PI control + * Arguments : pi_ctrl - The pointer to the PI control structure + * Return Value : PI control output value + **********************************************************************************************************************/ +static float motor_current_pi_control (motor_current_pi_params_t * pi_ctrl) +{ + float f4_err; + float f4_kp; + float f4_ki; + float f4_refp; + float f4_refi; + float f4_ref; + float f4_ilimit; + + f4_err = pi_ctrl->f_err; + f4_kp = pi_ctrl->f_kp; + f4_ki = pi_ctrl->f_ki; + f4_refi = pi_ctrl->f_refi; + f4_ilimit = pi_ctrl->f_ilimit; + + f4_refp = f4_err * f4_kp; /* Proportional part */ + f4_refi += (f4_err * f4_ki); /* Integral part */ + + /*** Integral part limit ***/ + f4_refi = motor_current_limit_abs(f4_refi, f4_ilimit); + pi_ctrl->f_refi = f4_refi; + + f4_ref = f4_refp + f4_refi; /* PI output */ + + return f4_ref; +} /* End of function motor_current_pi_control */ + +/*********************************************************************************************************************** + * Function Name : motor_current_decoupling + * Description : Decoupling control, c ancels the interference voltage due to the magnet and inductance + * Arguments : p_ctrl - The pointer to the FOC current control instance + * f_speed_rad - The electrical speed [rad/s] + * p_mtr - The pointer to the motor parameter data structure + * Return Value : None + **********************************************************************************************************************/ +static void motor_current_decoupling (motor_current_instance_ctrl_t * p_ctrl, + float f_speed_rad, + const motor_current_motor_parameter_t * p_mtr) +{ + float f4_temp0; + + f4_temp0 = p_mtr->f4_mtr_lq * p_ctrl->f_iq_ad; /* Lq * Iq */ + f4_temp0 = f_speed_rad * f4_temp0; /* Speed * Lq * Iq */ + p_ctrl->f_vd_ref -= f4_temp0; /* Vd - Speed * Lq * Iq */ + + f4_temp0 = p_mtr->f4_mtr_ld * p_ctrl->f_id_ad; /* Ld * id */ + f4_temp0 = f4_temp0 + p_mtr->f4_mtr_m; /* Ld * Id + Flux */ + f4_temp0 = f_speed_rad * f4_temp0; /* Speed * (Ld * id + Flux) */ + p_ctrl->f_vq_ref += f4_temp0; /* Vq + Speed*(Ld * id + Flux) */ +} /* End of function motor_current_decoupling */ + +/*********************************************************************************************************************** + * Function Name : motor_current_voltage_limit + * Description : Limit voltage vector, d-axis voltage has higher priority than q-axis voltage + * Arguments : p_ctrl - The pointer to the FOC current control structure + * Return Value : None + **********************************************************************************************************************/ +static void motor_current_voltage_limit (motor_current_instance_ctrl_t * p_ctrl) +{ + float f4_vq_lim; + float f4_vd_ref; + float f4_va_max; + + f4_vd_ref = p_ctrl->f_vd_ref; + f4_va_max = p_ctrl->f_va_max; + + /* The d-axis voltage has high priority than the q-axis */ + /* If absolute value of D-axis voltage exceeds maximum voltage, limit it to the maximum voltage */ + if (f4_vd_ref > f4_va_max) + { + p_ctrl->f_vd_ref = f4_va_max; + p_ctrl->f_vq_ref = 0.0F; + } + else if (f4_vd_ref < -f4_va_max) + { + p_ctrl->f_vd_ref = -f4_va_max; + p_ctrl->f_vq_ref = 0.0F; + } + else + { + f4_vq_lim = sqrtf((f4_va_max * f4_va_max) - (f4_vd_ref * f4_vd_ref)); + p_ctrl->f_vq_ref = motor_current_limit_abs(p_ctrl->f_vq_ref, f4_vq_lim); + } +} /* End of function motor_current_voltage_limit */ + +/*********************************************************************************************************************** + * Function Name : motor_current_transform_uvw_dq_abs + * Description : Coordinate transform UVW to dq (absolute transform) + * Arguments : f_angle - rotor angle + * f_uvw - the pointer to the UVW-phase array in [U,V,W] format + * f_dq - where to store the [d,q] formated array on dq coordinates + * Return Value : None + **********************************************************************************************************************/ +static void motor_current_transform_uvw_dq_abs (const float f_angle, const float * f_uvw, float * f_dq) +{ + float f4_temp0; + float f4_temp1; + float f4_temp2; + float f4_temp3; + float f4_u = f_uvw[0]; + float f4_v_sub_w = f_uvw[1] - f_uvw[2]; + float f4_cos = cosf(f_angle); + float f4_sin = sinf(f_angle); + + f4_temp0 = f4_cos * (1.0F / MOTOR_CURRENT_SQRT_2); + f4_temp1 = f4_sin * (1.0F / MOTOR_CURRENT_SQRT_2); + f4_temp2 = MOTOR_CURRENT_SQRT_3 * f4_temp0; + f4_temp3 = MOTOR_CURRENT_SQRT_3 * f4_temp1; + f_dq[0] = (f4_temp2 * f4_u) + (f4_temp1 * f4_v_sub_w); + f_dq[1] = ((-f4_temp3) * f4_u) + (f4_temp0 * f4_v_sub_w); +} /* End of function motor_current_transform_uvw_dq_abs */ + +/*********************************************************************************************************************** + * Function Name : motor_current_transform_dq_uvw_abs + * Description : Coordinate transform dq to UVW 3-phase (absolute transform) + * Arguments : angle - rotor angle + * f_dq - the pointer to the dq-axis value array in [D,Q] format + * f_uvw - where to store the [U,V,W] formated 3-phase quantities array + * Return Value : None + **********************************************************************************************************************/ +static void motor_current_transform_dq_uvw_abs (const float f_angle, const float * f_dq, float * f_uvw) +{ + float f4_cos_div_sqrt3; + float f4_sin_div_sqrt3; + float f4_output_d; + float f4_output_q; + float f4_input_d = f_dq[0]; + float f4_input_q = f_dq[1]; + float f4_cos = cosf(f_angle); + float f4_sin = sinf(f_angle); + + f4_cos_div_sqrt3 = f4_cos * (1.0F / MOTOR_CURRENT_SQRT_3); + f4_sin_div_sqrt3 = f4_sin * (1.0F / MOTOR_CURRENT_SQRT_3); + + f_uvw[0] = MOTOR_CURRENT_SQRT_2 * ((f4_cos_div_sqrt3 * f4_input_d) - (f4_sin_div_sqrt3 * f4_input_q)); + + f4_output_d = ((-f4_cos_div_sqrt3) + f4_sin) * f4_input_d; + f4_output_q = (f4_sin_div_sqrt3 + f4_cos) * f4_input_q; + f_uvw[1] = (f4_output_d + f4_output_q) * (1.0F / MOTOR_CURRENT_SQRT_2); + + f4_output_d = ((-f4_cos_div_sqrt3) - f4_sin) * f4_input_d; + f4_output_q = (f4_sin_div_sqrt3 - f4_cos) * f4_input_q; + f_uvw[2] = (f4_output_d + f4_output_q) * (1.0F / MOTOR_CURRENT_SQRT_2); +} /* End of function motor_current_transform_dq_uvw_abs */ diff --git a/ra/fsp/src/rm_motor_current/rm_motor_current_library.h b/ra/fsp/src/rm_motor_current/rm_motor_current_library.h new file mode 100644 index 000000000..8ed84e7a3 --- /dev/null +++ b/ra/fsp/src/rm_motor_current/rm_motor_current_library.h @@ -0,0 +1,40 @@ +/*********************************************************************************************************************** + * Copyright [2020] Renesas Electronics Corporation and/or its affiliates. All Rights Reserved. + * + * This software and documentation are supplied by Renesas Electronics America Inc. and may only be used with products + * of Renesas Electronics Corp. and its affiliates ("Renesas"). No other uses are authorized. Renesas products are + * sold pursuant to Renesas terms and conditions of sale. Purchasers are solely responsible for the selection and use + * of Renesas products and Renesas assumes no liability. No license, express or implied, to any intellectual property + * right is granted by Renesas. This software is protected under all applicable laws, including copyright laws. Renesas + * reserves the right to change or discontinue this software and/or this documentation. THE SOFTWARE AND DOCUMENTATION + * IS DELIVERED TO YOU "AS IS," AND RENESAS MAKES NO REPRESENTATIONS OR WARRANTIES, AND TO THE FULLEST EXTENT + * PERMISSIBLE UNDER APPLICABLE LAW, DISCLAIMS ALL WARRANTIES, WHETHER EXPLICITLY OR IMPLICITLY, INCLUDING WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NONINFRINGEMENT, WITH RESPECT TO THE SOFTWARE OR + * DOCUMENTATION. RENESAS SHALL HAVE NO LIABILITY ARISING OUT OF ANY SECURITY VULNERABILITY OR BREACH. TO THE MAXIMUM + * EXTENT PERMITTED BY LAW, IN NO EVENT WILL RENESAS BE LIABLE TO YOU IN CONNECTION WITH THE SOFTWARE OR DOCUMENTATION + * (OR ANY PERSON OR ENTITY CLAIMING RIGHTS DERIVED FROM YOU) FOR ANY LOSS, DAMAGES, OR CLAIMS WHATSOEVER, INCLUDING, + * WITHOUT LIMITATION, ANY DIRECT, CONSEQUENTIAL, SPECIAL, INDIRECT, PUNITIVE, OR INCIDENTAL DAMAGES; ANY LOST PROFITS, + * OTHER ECONOMIC DAMAGE, PROPERTY DAMAGE, OR PERSONAL INJURY; AND EVEN IF RENESAS HAS BEEN ADVISED OF THE POSSIBILITY + * OF SUCH LOSS, DAMAGES, CLAIMS OR COSTS. + **********************************************************************************************************************/ + +#ifndef RM_MOTOR_CURRENT_LIBRARY_H +#define RM_MOTOR_CURRENT_LIBRARY_H + +/*********************************************************************************************************************** + * Prototype definition + ***********************************************************************************************************************/ +void rm_motor_current_pi_gain_calc(motor_current_motor_parameter_t * st_motor, + motor_current_design_parameter_t * st_design_params, + motor_current_pi_params_t * st_pi_id, + motor_current_pi_params_t * st_pi_iq, + float f4_ctrl_period); + +void rm_motor_voltage_error_compensation_init(motor_currnt_voltage_compensation_t * st_volt_comp); +void rm_motor_voltage_error_compensation_reset(motor_currnt_voltage_compensation_t * st_volt_comp); +void rm_motor_voltage_error_compensation_main(motor_currnt_voltage_compensation_t * st_volt_comp, + float * p_f4_v_array, + float * p_f4_i_array, + float f4_vdc); + +#endif /* RM_MOTOR_CURRENT_LIBRARY_H */ diff --git a/ra/fsp/src/rm_motor_driver/rm_motor_driver.c b/ra/fsp/src/rm_motor_driver/rm_motor_driver.c new file mode 100644 index 000000000..c15a34c80 --- /dev/null +++ b/ra/fsp/src/rm_motor_driver/rm_motor_driver.c @@ -0,0 +1,851 @@ +/*********************************************************************************************************************** + * Copyright [2020] Renesas Electronics Corporation and/or its affiliates. All Rights Reserved. + * + * This software and documentation are supplied by Renesas Electronics America Inc. and may only be used with products + * of Renesas Electronics Corp. and its affiliates ("Renesas"). No other uses are authorized. Renesas products are + * sold pursuant to Renesas terms and conditions of sale. Purchasers are solely responsible for the selection and use + * of Renesas products and Renesas assumes no liability. No license, express or implied, to any intellectual property + * right is granted by Renesas. This software is protected under all applicable laws, including copyright laws. Renesas + * reserves the right to change or discontinue this software and/or this documentation. THE SOFTWARE AND DOCUMENTATION + * IS DELIVERED TO YOU "AS IS," AND RENESAS MAKES NO REPRESENTATIONS OR WARRANTIES, AND TO THE FULLEST EXTENT + * PERMISSIBLE UNDER APPLICABLE LAW, DISCLAIMS ALL WARRANTIES, WHETHER EXPLICITLY OR IMPLICITLY, INCLUDING WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NONINFRINGEMENT, WITH RESPECT TO THE SOFTWARE OR + * DOCUMENTATION. RENESAS SHALL HAVE NO LIABILITY ARISING OUT OF ANY SECURITY VULNERABILITY OR BREACH. TO THE MAXIMUM + * EXTENT PERMITTED BY LAW, IN NO EVENT WILL RENESAS BE LIABLE TO YOU IN CONNECTION WITH THE SOFTWARE OR DOCUMENTATION + * (OR ANY PERSON OR ENTITY CLAIMING RIGHTS DERIVED FROM YOU) FOR ANY LOSS, DAMAGES, OR CLAIMS WHATSOEVER, INCLUDING, + * WITHOUT LIMITATION, ANY DIRECT, CONSEQUENTIAL, SPECIAL, INDIRECT, PUNITIVE, OR INCIDENTAL DAMAGES; ANY LOST PROFITS, + * OTHER ECONOMIC DAMAGE, PROPERTY DAMAGE, OR PERSONAL INJURY; AND EVEN IF RENESAS HAS BEEN ADVISED OF THE POSSIBILITY + * OF SUCH LOSS, DAMAGES, CLAIMS OR COSTS. + **********************************************************************************************************************/ + +/*********************************************************************************************************************** + * Includes + **********************************************************************************************************************/ +#include +#include +#include "rm_motor_driver.h" +#include "bsp_api.h" +#include "bsp_cfg.h" + +/*********************************************************************************************************************** + * Macro definitions + **********************************************************************************************************************/ + +#define MOTOR_DRIVER_OPEN (0X4D445241L) + +#define MOTOR_DRIVER_FLG_CLR (0) /* For flag clear */ +#define MOTOR_DRIVER_FLG_SET (1) /* For flag set */ + +#define MOTOR_DRIVER_MULTIPLE_2 (2.0F) /* x2 */ +#define MOTOR_DRIVER_DEV_HALF (0.5F) /* 1/2 */ + +#define MOTOR_DRIVER_KHZ_TRANS (1000U) /* x1000 */ + +#define MOTOR_DRIVER_DEF_HALF (0.5F) +#define MOTOR_DRIVER_MULTIPLE_TWO (2.0F) + +/* Select SVPWM as default method when MOD_METHOD is undefined */ +#define MOTOR_DRIVER_DEFAULT_MAX_DUTY (1.0F) /* Default maximum duty cycle */ +#define MOTOR_DRIVER_METHOD_SPWM (0) /* Sinusoidal pulse-width-modulation */ +#define MOTOR_DRIVER_METHOD_SVPWM (1) /* Space vector pulse-width-modulation */ +#define MOTOR_DRIVER_SATFLAG_BITU (1 << 0) /* Saturation flag bit for U phase */ +#define MOTOR_DRIVER_SATFLAG_BITV (1 << 1) /* Saturation flag bit for V phase */ +#define MOTOR_DRIVER_SATFLAG_BITW (1 << 2) /* Saturation flag bit for W phase */ + +#ifndef MOTOR_DRIVER_METHOD + #define MOTOR_DRIVER_METHOD (MOTOR_DRIVER_METHOD_SVPWM) +#endif + +/* + * Vamax in this module is calculated by the following equation + * SVPWM : Vdc * (MOD_VDC_TO_VAMAX_MULT) * (Max duty - Min duty) * (MOD_SVPWM_MULT) + * SPWM : Vdc * (MOD_VDC_TO_VAMAX_MULT) * (Max duty - Min duty) + */ +#define MOTOR_DRIVER_VDC_TO_VAMAX_MULT (0.6124F) /* The basic coefficient used to convert Vdc to Vamax */ +#define MOTOR_DRIVER_SVPWM_MULT (1.155F) /* The usable voltage is multiplied by sqrt(4/3) when using SVPWM */ + +#ifndef MOTOR_DRIVER_ERROR_RETURN + + #define MOTOR_DRIVER_ERROR_RETURN(a, err) FSP_ERROR_RETURN((a), (err)) +#endif + +/*********************************************************************************************************************** + * Typedef definitions + **********************************************************************************************************************/ + +/*********************************************************************************************************************** + * Private function prototypes + **********************************************************************************************************************/ +void rm_motor_driver_cyclic(adc_callback_args_t * p_args); + +static void rm_motor_driver_reset(motor_driver_instance_ctrl_t * p_ctrl); +static void rm_motor_driver_set_uvw_duty(motor_driver_instance_ctrl_t * p_ctrl, + float f_duty_u, + float f_duty_v, + float f_duty_w); +static void rm_motor_driver_current_get(motor_driver_instance_ctrl_t * p_ctrl); +static void rm_motor_driver_modulation(motor_driver_instance_ctrl_t * p_ctrl); + +/* Modulation functions */ +static void rm_motor_driver_mod_run(motor_driver_modulation_t * p_mod, const float * p_f4_v_in, float * p_f4_duty_out); +static void rm_motor_driver_mod_set_max_duty(motor_driver_modulation_t * p_mod, float f4_max_duty); +static void rm_motor_driver_mod_set_min_duty(motor_driver_modulation_t * p_mod, float f4_min_duty); +static float rm_motor_driver_mod_get_vamax(motor_driver_modulation_t * p_mod); + +/*********************************************************************************************************************** + * Private global variables + **********************************************************************************************************************/ + +/* Version data structure. */ +static const fsp_version_t g_motor_driver_version = +{ + .api_version_minor = MOTOR_DRIVER_API_VERSION_MINOR, + .api_version_major = MOTOR_DRIVER_API_VERSION_MAJOR, + .code_version_major = MOTOR_DRIVER_CODE_VERSION_MAJOR, + .code_version_minor = MOTOR_DRIVER_CODE_VERSION_MINOR +}; + +/*********************************************************************************************************************** + * Global variables + **********************************************************************************************************************/ +const motor_driver_api_t g_motor_driver_on_motor_driver = +{ + .open = RM_MOTOR_DRIVER_Open, + .close = RM_MOTOR_DRIVER_Close, + .reset = RM_MOTOR_DRIVER_Reset, + .phaseVoltageSet = RM_MOTOR_DRIVER_PhaseVoltageSet, + .currentGet = RM_MOTOR_DRIVER_CurrentGet, + .flagCurrentOffsetGet = RM_MOTOR_DRIVER_FlagCurrentOffsetGet, + .currentOffsetRestart = RM_MOTOR_DRIVER_CurrentOffsetRestart, + .parameterUpdate = RM_MOTOR_DRIVER_ParameterUpdate, + .versionGet = RM_MOTOR_DRIVER_VersionGet +}; + +/*******************************************************************************************************************//** + * @addtogroup MOTOR_DRIVER + * @{ + **********************************************************************************************************************/ + +/*********************************************************************************************************************** + * Functions + **********************************************************************************************************************/ + +/*******************************************************************************************************************//** + * @brief Opens and configures the Motor Driver module. Implements @ref motor_driver_api_t::open. + * + * @retval FSP_SUCCESS Motor Driver 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. + **********************************************************************************************************************/ +fsp_err_t RM_MOTOR_DRIVER_Open (motor_driver_ctrl_t * const p_ctrl, motor_driver_cfg_t const * const p_cfg) +{ + motor_driver_instance_ctrl_t * p_instance_ctrl = (motor_driver_instance_ctrl_t *) p_ctrl; + +#if MOTOR_DRIVER_CFG_PARAM_CHECKING_ENABLE + FSP_ASSERT(NULL != p_instance_ctrl); + FSP_ASSERT(NULL != p_cfg); +#endif + + motor_driver_extended_cfg_t * p_extended_cfg = (motor_driver_extended_cfg_t *) p_cfg->p_extend; + +#if MOTOR_DRIVER_CFG_PARAM_CHECKING_ENABLE + FSP_ASSERT(NULL != p_extended_cfg); +#endif + + FSP_ERROR_RETURN(MOTOR_DRIVER_OPEN != p_instance_ctrl->open, FSP_ERR_ALREADY_OPEN); + + p_instance_ctrl->p_cfg = p_cfg; + + p_instance_ctrl->u2_carrier_base = + (uint16_t) (p_extended_cfg->u2_pwm_timer_freq * MOTOR_DRIVER_KHZ_TRANS / + p_extended_cfg->u2_pwm_carrier_freq / (uint16_t) MOTOR_DRIVER_MULTIPLE_2); + p_instance_ctrl->u2_deadtime_count = + (uint16_t) (p_extended_cfg->u2_deadtime * p_extended_cfg->u2_pwm_timer_freq); + + rm_motor_driver_reset(p_instance_ctrl); + + p_instance_ctrl->st_modulation = p_extended_cfg->mod_param; + rm_motor_driver_mod_set_max_duty(&(p_instance_ctrl->st_modulation), p_extended_cfg->mod_param.f4_max_duty); + rm_motor_driver_mod_set_min_duty(&(p_instance_ctrl->st_modulation), p_extended_cfg->mod_param.f4_min_duty); + + /* Start ADC module */ + if (p_cfg->p_adc_instance != NULL) + { + p_cfg->p_adc_instance->p_api->open(p_cfg->p_adc_instance->p_ctrl, p_cfg->p_adc_instance->p_cfg); + p_cfg->p_adc_instance->p_api->scanCfg(p_cfg->p_adc_instance->p_ctrl, p_cfg->p_adc_instance->p_channel_cfg); + p_cfg->p_adc_instance->p_api->callbackSet(p_cfg->p_adc_instance->p_ctrl, + rm_motor_driver_cyclic, + p_instance_ctrl, + &(p_instance_ctrl->adc_callback_args)); + p_cfg->p_adc_instance->p_api->scanStart(p_cfg->p_adc_instance->p_ctrl); + } + + /* Start GPT Three Phase Module */ + if (p_cfg->p_three_phase_instance != NULL) + { + p_cfg->p_three_phase_instance->p_api->open(p_cfg->p_three_phase_instance->p_ctrl, + p_cfg->p_three_phase_instance->p_cfg); + rm_motor_driver_set_uvw_duty(p_instance_ctrl, + p_instance_ctrl->st_modulation.f4_neutral_duty, + p_instance_ctrl->st_modulation.f4_neutral_duty, + p_instance_ctrl->st_modulation.f4_neutral_duty); + p_cfg->p_three_phase_instance->p_api->start(p_cfg->p_three_phase_instance->p_ctrl); + } + + /* Mark driver as open */ + p_instance_ctrl->open = MOTOR_DRIVER_OPEN; + + return FSP_SUCCESS; +} + +/*******************************************************************************************************************//** + * @brief Disables specified Motor Driver Module. Implements @ref motor_driver_api_t::close. + * + * @retval FSP_SUCCESS Successfully closed. + * @retval FSP_ERR_ASSERTION Null pointer. + * @retval FSP_ERR_NOT_OPEN Module is not open. + **********************************************************************************************************************/ +fsp_err_t RM_MOTOR_DRIVER_Close (motor_driver_ctrl_t * const p_ctrl) +{ + motor_driver_instance_ctrl_t * p_instance_ctrl = (motor_driver_instance_ctrl_t *) p_ctrl; + +#if MOTOR_DRIVER_CFG_PARAM_CHECKING_ENABLE + FSP_ASSERT(p_instance_ctrl); + MOTOR_DRIVER_ERROR_RETURN(MOTOR_DRIVER_OPEN == p_instance_ctrl->open, FSP_ERR_NOT_OPEN); +#endif + + motor_driver_cfg_t * p_cfg = (motor_driver_cfg_t *) p_instance_ctrl->p_cfg; + + rm_motor_driver_reset(p_instance_ctrl); + + /* Close ADC Module */ + if (p_cfg->p_adc_instance != NULL) + { + p_cfg->p_adc_instance->p_api->close(p_cfg->p_adc_instance->p_ctrl); + } + + /* Close GPT Three Phase Module */ + if (p_cfg->p_three_phase_instance != NULL) + { + p_cfg->p_three_phase_instance->p_api->close(p_cfg->p_three_phase_instance->p_ctrl); + } + + p_instance_ctrl->open = 0U; + + return FSP_SUCCESS; +} + +/*******************************************************************************************************************//** + * @brief Reset variables of Motor Driver Module. Implements @ref motor_driver_api_t::reset. + * + * @retval FSP_SUCCESS Successfully reset. + * @retval FSP_ERR_ASSERTION Null pointer. + * @retval FSP_ERR_NOT_OPEN Module is not open. + **********************************************************************************************************************/ +fsp_err_t RM_MOTOR_DRIVER_Reset (motor_driver_ctrl_t * const p_ctrl) +{ + motor_driver_instance_ctrl_t * p_instance_ctrl = (motor_driver_instance_ctrl_t *) p_ctrl; + +#if MOTOR_DRIVER_CFG_PARAM_CHECKING_ENABLE + FSP_ASSERT(p_instance_ctrl); + MOTOR_DRIVER_ERROR_RETURN(MOTOR_DRIVER_OPEN == p_instance_ctrl->open, FSP_ERR_NOT_OPEN); +#endif + + p_instance_ctrl->f_refu = p_instance_ctrl->st_modulation.f4_neutral_duty; + p_instance_ctrl->f_refv = p_instance_ctrl->st_modulation.f4_neutral_duty; + p_instance_ctrl->f_refw = p_instance_ctrl->st_modulation.f4_neutral_duty; + + rm_motor_driver_set_uvw_duty(p_instance_ctrl, + p_instance_ctrl->st_modulation.f4_neutral_duty, + p_instance_ctrl->st_modulation.f4_neutral_duty, + p_instance_ctrl->st_modulation.f4_neutral_duty); + rm_motor_driver_reset(p_instance_ctrl); + + return FSP_SUCCESS; +} + +/*******************************************************************************************************************//** + * @brief Set Phase Voltage Data to calculate PWM duty. Implements @ref motor_driver_api_t::phaseVoltageSet + * + * @retval FSP_SUCCESS Successfully data is set. + * @retval FSP_ERR_ASSERTION Null pointer. + * @retval FSP_ERR_NOT_OPEN Module is not open. + **********************************************************************************************************************/ +fsp_err_t RM_MOTOR_DRIVER_PhaseVoltageSet (motor_driver_ctrl_t * const p_ctrl, + float const u_voltage, + float const v_voltage, + float const w_voltage) +{ + motor_driver_instance_ctrl_t * p_instance_ctrl = (motor_driver_instance_ctrl_t *) p_ctrl; + +#if MOTOR_DRIVER_CFG_PARAM_CHECKING_ENABLE + FSP_ASSERT(p_instance_ctrl); + MOTOR_DRIVER_ERROR_RETURN(MOTOR_DRIVER_OPEN == p_instance_ctrl->open, FSP_ERR_NOT_OPEN); +#endif + + p_instance_ctrl->f_refu = u_voltage; + p_instance_ctrl->f_refv = v_voltage; + p_instance_ctrl->f_refw = w_voltage; + + return FSP_SUCCESS; +} + +/*******************************************************************************************************************//** + * @brief Get calculated phase Current, Vdc & Va_max data. Implements @ref motor_driver_api_t::currentGet + * + * @retval FSP_SUCCESS Successful data get. + * @retval FSP_ERR_ASSERTION Null pointer. + * @retval FSP_ERR_NOT_OPEN Module is not open. + * @retval FSP_ERR_INVALID_ARGUMENT Input parameter error. + **********************************************************************************************************************/ +fsp_err_t RM_MOTOR_DRIVER_CurrentGet (motor_driver_ctrl_t * const p_ctrl, + motor_driver_current_get_t * const p_current_get) +{ + motor_driver_instance_ctrl_t * p_instance_ctrl = (motor_driver_instance_ctrl_t *) p_ctrl; + +#if MOTOR_DRIVER_CFG_PARAM_CHECKING_ENABLE + FSP_ASSERT(p_instance_ctrl); + MOTOR_DRIVER_ERROR_RETURN(MOTOR_DRIVER_OPEN == p_instance_ctrl->open, FSP_ERR_NOT_OPEN); + MOTOR_DRIVER_ERROR_RETURN(p_current_get != NULL, FSP_ERR_INVALID_ARGUMENT); +#endif + + p_current_get->iu = p_instance_ctrl->f_iu_ad - p_instance_ctrl->f_offset_iu; + p_current_get->iw = p_instance_ctrl->f_iw_ad - p_instance_ctrl->f_offset_iw; + p_current_get->vdc = p_instance_ctrl->f_vdc_ad; + p_current_get->va_max = rm_motor_driver_mod_get_vamax(&(p_instance_ctrl->st_modulation)); + + return FSP_SUCCESS; +} + +/*******************************************************************************************************************//** + * @brief Get the flag of finish current offset detection. Implements @ref motor_driver_api_t::flagCurrentOffsetGet + * + * @retval FSP_SUCCESS Successful data get. + * @retval FSP_ERR_ASSERTION Null pointer. + * @retval FSP_ERR_NOT_OPEN Module is not open. + * @retval FSP_ERR_INVALID_ARGUMENT Input parameter error. + **********************************************************************************************************************/ +fsp_err_t RM_MOTOR_DRIVER_FlagCurrentOffsetGet (motor_driver_ctrl_t * const p_ctrl, uint8_t * const p_flag_offset) +{ + uint16_t u2_temp_offset_count = 0U; + motor_driver_instance_ctrl_t * p_instance_ctrl = (motor_driver_instance_ctrl_t *) p_ctrl; + +#if MOTOR_DRIVER_CFG_PARAM_CHECKING_ENABLE + FSP_ASSERT(p_ctrl); + FSP_ASSERT(p_instance_ctrl); + MOTOR_DRIVER_ERROR_RETURN(MOTOR_DRIVER_OPEN == p_instance_ctrl->open, FSP_ERR_NOT_OPEN); + MOTOR_DRIVER_ERROR_RETURN(p_flag_offset != NULL, FSP_ERR_INVALID_ARGUMENT); +#endif + + motor_driver_extended_cfg_t * p_extended_cfg = + (motor_driver_extended_cfg_t *) p_instance_ctrl->p_cfg->p_extend; + + /* Measure Current A/D Offset */ + if (MOTOR_DRIVER_FLG_CLR == p_instance_ctrl->u1_flag_offset_calc) + { + /* Output Neautral PWM */ + rm_motor_driver_set_uvw_duty(p_instance_ctrl, + p_instance_ctrl->st_modulation.f4_neutral_duty, + p_instance_ctrl->st_modulation.f4_neutral_duty, + p_instance_ctrl->st_modulation.f4_neutral_duty); + + u2_temp_offset_count = p_extended_cfg->u2_offset_calc_count; + if (MOTOR_DRIVER_FLG_CLR == p_instance_ctrl->u1_flag_offset_calc) + { + if (p_instance_ctrl->u2_offset_calc_times < u2_temp_offset_count) + { + p_instance_ctrl->f_sum_iu_ad += p_instance_ctrl->f_iu_ad; + p_instance_ctrl->f_sum_iw_ad += p_instance_ctrl->f_iw_ad; + p_instance_ctrl->u2_offset_calc_times++; + } + else + { + p_instance_ctrl->f_offset_iu = p_instance_ctrl->f_sum_iu_ad / + u2_temp_offset_count; + p_instance_ctrl->f_offset_iw = p_instance_ctrl->f_sum_iw_ad / + u2_temp_offset_count; + p_instance_ctrl->u1_flag_offset_calc = MOTOR_DRIVER_FLG_SET; + } + } + } + + *p_flag_offset = p_instance_ctrl->u1_flag_offset_calc; + + return FSP_SUCCESS; +} + +/*******************************************************************************************************************//** + * @brief Restart the current offset detection. Implements @ref motor_driver_api_t::currentOffsetRestart + * + * @retval FSP_SUCCESS Successfully restarted. + * @retval FSP_ERR_ASSERTION Null pointer. + * @retval FSP_ERR_NOT_OPEN Module is not open. + **********************************************************************************************************************/ +fsp_err_t RM_MOTOR_DRIVER_CurrentOffsetRestart (motor_driver_ctrl_t * const p_ctrl) +{ + motor_driver_instance_ctrl_t * p_instance_ctrl = (motor_driver_instance_ctrl_t *) p_ctrl; + +#if MOTOR_DRIVER_CFG_PARAM_CHECKING_ENABLE + FSP_ASSERT(p_instance_ctrl); + MOTOR_DRIVER_ERROR_RETURN(MOTOR_DRIVER_OPEN == p_instance_ctrl->open, FSP_ERR_NOT_OPEN); +#endif + + p_instance_ctrl->u1_flag_offset_calc = MOTOR_DRIVER_FLG_CLR; + p_instance_ctrl->u2_offset_calc_times = 0U; + p_instance_ctrl->f_sum_iu_ad = 0.0F; + p_instance_ctrl->f_sum_iw_ad = 0.0F; + + return FSP_SUCCESS; +} + +/*******************************************************************************************************************//** + * @brief Update the parameters of Driver Module. Implements @ref motor_driver_api_t::parameterUpdate + * + * @retval FSP_SUCCESS Successfully data was updated. + * @retval FSP_ERR_ASSERTION Null pointer. + * @retval FSP_ERR_NOT_OPEN Module is not open. + **********************************************************************************************************************/ +fsp_err_t RM_MOTOR_DRIVER_ParameterUpdate (motor_driver_ctrl_t * const p_ctrl, motor_driver_cfg_t const * const p_cfg) +{ + motor_driver_instance_ctrl_t * p_instance_ctrl = (motor_driver_instance_ctrl_t *) p_ctrl; + +#if MOTOR_DRIVER_CFG_PARAM_CHECKING_ENABLE + FSP_ASSERT(p_instance_ctrl); + MOTOR_DRIVER_ERROR_RETURN(MOTOR_DRIVER_OPEN == p_instance_ctrl->open, FSP_ERR_NOT_OPEN); + FSP_ASSERT(p_cfg); +#endif + + motor_driver_extended_cfg_t * p_extended_cfg = (motor_driver_extended_cfg_t *) p_cfg->p_extend; + + p_instance_ctrl->p_cfg = p_cfg; + + p_instance_ctrl->u2_carrier_base = + (uint16_t) (p_extended_cfg->u2_pwm_timer_freq * MOTOR_DRIVER_KHZ_TRANS / + p_extended_cfg->u2_pwm_carrier_freq / (uint16_t) MOTOR_DRIVER_MULTIPLE_2); + p_instance_ctrl->u2_deadtime_count = + (uint16_t) (p_extended_cfg->u2_deadtime * p_extended_cfg->u2_pwm_timer_freq); + + p_instance_ctrl->st_modulation = p_extended_cfg->mod_param; + rm_motor_driver_mod_set_max_duty(&(p_instance_ctrl->st_modulation), p_extended_cfg->mod_param.f4_max_duty); + rm_motor_driver_mod_set_min_duty(&(p_instance_ctrl->st_modulation), p_extended_cfg->mod_param.f4_min_duty); + + return FSP_SUCCESS; +} + +/*******************************************************************************************************************//** + * Return Motor Driver Module version. Implements @ref motor_driver_api_t::versionGet. + * + * @retval FSP_SUCCESS Version information successfully read. + * @retval FSP_ERR_ASSERTION Null pointer passed as a parameter + **********************************************************************************************************************/ +fsp_err_t RM_MOTOR_DRIVER_VersionGet (fsp_version_t * const p_version) +{ +#if MOTOR_DRIVER_CFG_PARAM_CHECKING_ENABLE + + /* Verify parameters are valid */ + FSP_ASSERT(NULL != p_version); +#endif + + p_version->version_id = g_motor_driver_version.version_id; + + return FSP_SUCCESS; +} + +/*******************************************************************************************************************//** + * @} (end addtogroup MOTOR_DRIVER) + **********************************************************************************************************************/ + +/*********************************************************************************************************************** + * Private Functions + **********************************************************************************************************************/ + +/*********************************************************************************************************************** + * Function Name : rm_motor_driver_reset + * Description : Resets driver variables + * Arguments : p_ctrl - The pointer to the motor driver instance + * Return Value : None + **********************************************************************************************************************/ +static void rm_motor_driver_reset (motor_driver_instance_ctrl_t * p_ctrl) +{ + p_ctrl->f_iu_ad = 0.0F; + p_ctrl->f_iw_ad = 0.0F; + p_ctrl->f_vdc_ad = 0.0F; + p_ctrl->f_refu = 0.0F; + p_ctrl->f_refv = 0.0F; + p_ctrl->f_refw = 0.0F; + + p_ctrl->u1_flag_offset_calc = MOTOR_DRIVER_FLG_CLR; + p_ctrl->u2_offset_calc_times = 0U; + p_ctrl->f_offset_iu = 0.0F; + p_ctrl->f_offset_iw = 0.0F; + p_ctrl->f_sum_iu_ad = 0.0F; + p_ctrl->f_sum_iw_ad = 0.0F; + + p_ctrl->st_modulation.u1_sat_flag = 0U; +} /* End of function rm_motor_driver_reset */ + +/*********************************************************************************************************************** + * Function Name : rm_motor_driver_set_uvw_duty + * Description : PWM duty setting + * Arguments : p_instance - Pointer to Motor Driver instance + * f_duty_u - The duty cycle of Phase-U (0.0 - 1.0) + * f_duty_v - The duty cycle of Phase-V (0.0 - 1.0) + * f_duty_w - The duty cycle of Phase-W (0.0 - 1.0) + * Return Value : None + **********************************************************************************************************************/ +static void rm_motor_driver_set_uvw_duty (motor_driver_instance_ctrl_t * p_ctrl, + float f_duty_u, + float f_duty_v, + float f_duty_w) +{ + three_phase_instance_t const * p_three_phase = p_ctrl->p_cfg->p_three_phase_instance; + uint16_t u2_count_u = 0U; + uint16_t u2_count_v = 0U; + uint16_t u2_count_w = 0U; + three_phase_duty_cycle_t temp_duty; + uint16_t u2_temp_base = p_ctrl->u2_carrier_base; + uint16_t u2_temp_deadt = p_ctrl->u2_deadtime_count; + + u2_count_u = (uint16_t) ((u2_temp_base * (1.0F - f_duty_u)) + (u2_temp_deadt * MOTOR_DRIVER_DEV_HALF)); + u2_count_v = (uint16_t) ((u2_temp_base * (1.0F - f_duty_v)) + (u2_temp_deadt * MOTOR_DRIVER_DEV_HALF)); + u2_count_w = (uint16_t) ((u2_temp_base * (1.0F - f_duty_w)) + (u2_temp_deadt * MOTOR_DRIVER_DEV_HALF)); + + temp_duty.duty[0] = (uint32_t) u2_count_u; + temp_duty.duty[1] = (uint32_t) u2_count_v; + temp_duty.duty[2] = (uint32_t) u2_count_w; + if (p_three_phase != NULL) + { + p_three_phase->p_api->dutyCycleSet(p_three_phase->p_ctrl, &temp_duty); + } +} /* End of function rm_motor_driver_set_uvw_duty */ + +/*********************************************************************************************************************** + * Function Name : rm_motor_driver_current_get + * Description : Get Iu/Iw & Vdc process + * Arguments : p_ctrl - The pointer to the motor driver module instance + * Return Value : None + **********************************************************************************************************************/ +static void rm_motor_driver_current_get (motor_driver_instance_ctrl_t * p_ctrl) +{ + uint16_t u2_addata[3] = {0U}; + float f_addata[2] = {0.0F}; + motor_driver_cfg_t const * p_cfg = p_ctrl->p_cfg; + motor_driver_extended_cfg_t * p_extend_cfg = (motor_driver_extended_cfg_t *) p_cfg->p_extend; + + /* Read A/D converted data */ + if (p_cfg->p_adc_instance != NULL) + { + p_cfg->p_adc_instance->p_api->read(p_cfg->p_adc_instance->p_ctrl, p_cfg->iu_ad_ch, &u2_addata[0]); + p_cfg->p_adc_instance->p_api->read(p_cfg->p_adc_instance->p_ctrl, p_cfg->iw_ad_ch, &u2_addata[1]); + p_cfg->p_adc_instance->p_api->read(p_cfg->p_adc_instance->p_ctrl, p_cfg->vdc_ad_ch, &u2_addata[2]); + } + + f_addata[0] = (float) u2_addata[0]; + f_addata[1] = (float) u2_addata[1]; + f_addata[0] = f_addata[0] - p_extend_cfg->f_ad_current_offset; + f_addata[1] = f_addata[1] - p_extend_cfg->f_ad_current_offset; + + p_ctrl->f_iu_ad = -(f_addata[0]) * (p_extend_cfg->f_current_range / p_extend_cfg->f_ad_resolution); + p_ctrl->f_iw_ad = -(f_addata[1]) * (p_extend_cfg->f_current_range / p_extend_cfg->f_ad_resolution); + + p_ctrl->f_vdc_ad = (float) u2_addata[2] * (p_extend_cfg->f_vdc_range / p_extend_cfg->f_ad_resolution) * + p_extend_cfg->f_ad_voltage_conversion; +} /* End of function rm_motor_driver_current_get */ + +/*********************************************************************************************************************** + * Function Name : rm_motor_driver_modulation + * Description : Perform PWM modulation + * Arguments : p_ctrl - The pointer to the motor driver module instance + * Return Value : None + **********************************************************************************************************************/ +static void rm_motor_driver_modulation (motor_driver_instance_ctrl_t * p_ctrl) +{ + float f_v_in[3] = {0.0F}; + float f_mod_out[3] = {0.0F}; + + p_ctrl->st_modulation.f4_vdc = p_ctrl->f_vdc_ad; + p_ctrl->st_modulation.f4_1_div_vdc = 1.0F / p_ctrl->f_vdc_ad; + f_v_in[0] = p_ctrl->f_refu; + f_v_in[1] = p_ctrl->f_refv; + f_v_in[2] = p_ctrl->f_refw; + + rm_motor_driver_mod_run(&(p_ctrl->st_modulation), &(f_v_in[0]), &(f_mod_out[0])); + + rm_motor_driver_set_uvw_duty(p_ctrl, f_mod_out[0], f_mod_out[1], f_mod_out[2]); +} /* End of function rm_motor_driver_modulation */ + +/*********************************************************************************************************************** + * Function Name: rm_motor_driver_mod_svpwm + * Description : Space vector modulation + * Arguments : p_f4_v_in - + * Input data, in an array [Vu,Vv,Vw] + * p_f4_v_out - + * Where to store output data, in an array [Vu,Vv,Vw] + * Return Value : None + ***********************************************************************************************************************/ +static void rm_motor_driver_mod_svpwm (const float * p_f4_v_in, float * p_f4_v_out) +{ + float f4_v_max; + float f4_v_min; + float f4_v_com; + + /* Sort vu vv vw */ + if (p_f4_v_in[0] > p_f4_v_in[1]) + { + f4_v_max = p_f4_v_in[0]; + f4_v_min = p_f4_v_in[1]; + } + else + { + f4_v_max = p_f4_v_in[1]; + f4_v_min = p_f4_v_in[0]; + } + + /* Vcom = (Vmin + Vmax)/2 */ + if (p_f4_v_in[2] > f4_v_max) + { + f4_v_com = (p_f4_v_in[2] + f4_v_min) * MOTOR_DRIVER_DEF_HALF; + } + else if (p_f4_v_in[2] < f4_v_min) + { + f4_v_com = (f4_v_max + p_f4_v_in[2]) * MOTOR_DRIVER_DEF_HALF; + } + else + { + f4_v_com = (f4_v_max + f4_v_min) * MOTOR_DRIVER_DEF_HALF; + } + + p_f4_v_out[0] = p_f4_v_in[0] - f4_v_com; + p_f4_v_out[1] = p_f4_v_in[1] - f4_v_com; + p_f4_v_out[2] = p_f4_v_in[2] - f4_v_com; +} /* End of function rm_motor_driver_mod_svpwm() */ + +/*********************************************************************************************************************** + * Function Name: rm_motor_driver_mod_limit + * Description : Limits the duty cycle, and detect saturation (if function enabled) + * Arguments : p_mod - + * Pointer to the modulation data structure + * p_f4_duty - + * Pointer to the input and output duty cycle array, array length = 3 + * Return Value : None + ***********************************************************************************************************************/ +static inline void rm_motor_driver_mod_limit (motor_driver_modulation_t * p_mod, float * p_f4_duty) +{ + float f4_min_duty = p_mod->f4_min_duty; + float f4_max_duty = p_mod->f4_max_duty; + + if (p_f4_duty[0] > f4_max_duty) + { + p_f4_duty[0] = f4_max_duty; +#if (MOD_DETECT_SATURATION == 1) + p_mod->u1_sat_flag |= MOTOR_DRIVER_SATFLAG_BITU; +#endif + } + else if (p_f4_duty[0] < f4_min_duty) + { + p_f4_duty[0] = f4_min_duty; +#if (MOD_DETECT_SATURATION == 1) + p_mod->u1_sat_flag |= MOTOR_DRIVER_SATFLAG_BITU; +#endif + } + else + { + /* Clear correspond saturation flag bit */ +#if (MOD_DETECT_SATURATION == 1) + p_mod->u1_sat_flag &= (~MOTOR_DRIVER_SATFLAG_BITU); +#endif + } + + if (p_f4_duty[1] > f4_max_duty) + { + p_f4_duty[1] = f4_max_duty; +#if (MOD_DETECT_SATURATION == 1) + p_mod->u1_sat_flag |= MOTOR_DRIVER_SATFLAG_BITV; +#endif + } + else if (p_f4_duty[1] < f4_min_duty) + { + p_f4_duty[1] = f4_min_duty; +#if (MOD_DETECT_SATURATION == 1) + p_mod->u1_sat_flag |= MOTOR_DRIVER_SATFLAG_BITV; +#endif + } + else + { + /* Clear correspond saturation flag bit */ +#if (MOD_DETECT_SATURATION == 1) + p_mod->u1_sat_flag &= (~MOTOR_DRIVER_SATFLAG_BITV); +#endif + } + + if (p_f4_duty[2] > f4_max_duty) + { + p_f4_duty[2] = f4_max_duty; +#if (MOD_DETECT_SATURATION == 1) + p_mod->u1_sat_flag |= MOTOR_DRIVER_SATFLAG_BITW; +#endif + } + else if (p_f4_duty[2] < f4_min_duty) + { + p_f4_duty[2] = f4_min_duty; +#if (MOD_DETECT_SATURATION == 1) + p_mod->u1_sat_flag |= MOTOR_DRIVER_SATFLAG_BITW; +#endif + } + else + { + /* Clear correspond saturation flag bit */ +#if (MOD_DETECT_SATURATION == 1) + p_mod->u1_sat_flag &= (~MOTOR_DRIVER_SATFLAG_BITW); +#endif + } +} /* End of function rm_motor_driver_mod_limit() */ + +/*********************************************************************************************************************** + * Function Name: rm_motor_driver_mod_run + * Description : Calculates duty cycle from input 3-phase voltage (bipolar) + * Arguments : p_mod - + * Pointer to the modulation data structure + * : p_f4_v_in - + * Pointer to the 3-phase input voltage + * : p_f4_duty_out - + * Where to store the 3-phase output duty cycle + * Return Value : None + ***********************************************************************************************************************/ +static void rm_motor_driver_mod_run (motor_driver_modulation_t * p_mod, const float * p_f4_v_in, float * p_f4_duty_out) +{ + float f4_v_out[3]; + + if ((0 == p_mod) || (0 == p_f4_duty_out)) + { + return; + } + +#if (MOTOR_DRIVER_METHOD == MOTOR_DRIVER_METHOD_SVPWM) + rm_motor_driver_mod_svpwm(p_f4_v_in, f4_v_out); +#else + f4_v_out[0] = p_f4_v_in[0]; + f4_v_out[1] = p_f4_v_in[1]; + f4_v_out[2] = p_f4_v_in[2]; +#endif + + p_f4_duty_out[0] = (f4_v_out[0] * p_mod->f4_1_div_vdc) + p_mod->f4_neutral_duty; + p_f4_duty_out[1] = (f4_v_out[1] * p_mod->f4_1_div_vdc) + p_mod->f4_neutral_duty; + p_f4_duty_out[2] = (f4_v_out[2] * p_mod->f4_1_div_vdc) + p_mod->f4_neutral_duty; + + rm_motor_driver_mod_limit(p_mod, p_f4_duty_out); +} /* End of function rm_motor_driver_mod_run() */ + +/*********************************************************************************************************************** + * Function Name: rm_motor_driver_mod_set_max_duty + * Description : Sets the maximum duty cycle + * Arguments : p_mod - + * Pointer to the modulation data structure + * f4_max_duty - + * Maximum duty cycle to set + * Return Value : None + ***********************************************************************************************************************/ +static void rm_motor_driver_mod_set_max_duty (motor_driver_modulation_t * p_mod, float f4_max_duty) +{ + if ((0 != p_mod) && ((f4_max_duty > 0.0F) && (f4_max_duty <= 1.0F))) + { + p_mod->f4_max_duty = f4_max_duty; + p_mod->f4_neutral_duty = (p_mod->f4_max_duty + p_mod->f4_min_duty) * MOTOR_DRIVER_DEF_HALF; + } +} /* End of function rm_motor_driver_mod_set_max_duty() */ + +/*********************************************************************************************************************** + * Function Name: rm_motor_driver_mod_set_min_duty + * Description : Sets the minimum duty cycle + * Arguments : p_mod - + * Pointer to the modulation data structure + * f4_max_duty - + * Minimum duty cycle to set + * Return Value : None + ***********************************************************************************************************************/ +static void rm_motor_driver_mod_set_min_duty (motor_driver_modulation_t * p_mod, float f4_min_duty) +{ + if ((0 != p_mod) && (f4_min_duty > 0.0F)) + { + p_mod->f4_min_duty = f4_min_duty; + p_mod->f4_neutral_duty = (p_mod->f4_max_duty + p_mod->f4_min_duty) * MOTOR_DRIVER_DEF_HALF; + } +} /* End of function rm_motor_driver_mod_set_min_duty() */ + +/*********************************************************************************************************************** + * Function Name: rm_motor_driver_mod_get_voltage_multiplier + * Description : Gets the voltage multiplier + * Arguments : p_mod - + * Pointer to the modulation data structure + * Return Value : Voltage multiplier + ***********************************************************************************************************************/ +static float rm_motor_driver_mod_get_voltage_multiplier (motor_driver_modulation_t * p_mod) +{ + float f4_usable_duty_cycle; + + f4_usable_duty_cycle = (p_mod->f4_max_duty - p_mod->f4_min_duty) - + (MOTOR_DRIVER_MULTIPLE_TWO * p_mod->f4_voltage_error_ratio); + +#if (MOTOR_DRIVER_METHOD == MOTOR_DRIVER_METHOD_SVPWM) + f4_usable_duty_cycle = f4_usable_duty_cycle * MOTOR_DRIVER_SVPWM_MULT; +#endif + + return f4_usable_duty_cycle; +} /* End of function rm_motor_driver_mod_get_voltage_multiplier */ + +/*********************************************************************************************************************** + * Function Name: rm_motor_driver_mod_get_vamax + * Description : Gets the maximum magnitude of voltage vector + * Arguments : p_mod - + * The pointer to the modulation data structure + * Return Value : The maximum magnitude of voltage vector + ***********************************************************************************************************************/ +static float rm_motor_driver_mod_get_vamax (motor_driver_modulation_t * p_mod) +{ + return (MOTOR_DRIVER_VDC_TO_VAMAX_MULT * p_mod->f4_vdc) * rm_motor_driver_mod_get_voltage_multiplier(p_mod); +} /* End of function rm_motor_driver_mod_get_vamax */ + +/*********************************************************************************************************************** + * Function Name : rm_motor_driver_cyclic + * Description : Cyclic process for driver accsess (Call at A/D conversion finish interrupt) + * Arguments : p_args - The pointer to arguments of A/D conversion finish intterupt callback + * Return Value : None + **********************************************************************************************************************/ +void rm_motor_driver_cyclic (adc_callback_args_t * p_args) +{ + motor_driver_instance_ctrl_t * p_instance = (motor_driver_instance_ctrl_t *) p_args->p_context; + motor_driver_callback_args_t temp_args_t; + + /* Get A/D converted data (Phase Current & Main Line Voltage) */ + rm_motor_driver_current_get(p_instance); + + /* Invoke the callback function if it is set. */ + if (NULL != p_instance->p_cfg->p_callback) + { + temp_args_t.event = MOTOR_DRIVER_EVENT_FORWARD; + temp_args_t.p_context = p_instance->p_cfg->p_context; + (p_instance->p_cfg->p_callback)(&temp_args_t); + } + + /* Perform Current Control Process (if set) */ + if (NULL != p_instance->p_cfg->p_callback) + { + temp_args_t.event = MOTOR_DRIVER_EVENT_CURRENT; + temp_args_t.p_context = p_instance->p_cfg->p_context; + (p_instance->p_cfg->p_callback)(&temp_args_t); + } + + /* PWM Modulation */ + if (MOTOR_DRIVER_FLG_SET == p_instance->u1_flag_offset_calc) + { + rm_motor_driver_modulation(p_instance); + } + + /* Invoke the callback function if it is set. */ + if (NULL != p_instance->p_cfg->p_callback) + { + temp_args_t.event = MOTOR_DRIVER_EVENT_BACKWARD; + temp_args_t.p_context = p_instance->p_cfg->p_context; + (p_instance->p_cfg->p_callback)(&temp_args_t); + } +} diff --git a/ra/fsp/src/rm_motor_estimate/rm_motor_estimate.c b/ra/fsp/src/rm_motor_estimate/rm_motor_estimate.c new file mode 100644 index 000000000..d1ed1328f --- /dev/null +++ b/ra/fsp/src/rm_motor_estimate/rm_motor_estimate.c @@ -0,0 +1,475 @@ +/*********************************************************************************************************************** + * Copyright [2020] Renesas Electronics Corporation and/or its affiliates. All Rights Reserved. + * + * This software and documentation are supplied by Renesas Electronics America Inc. and may only be used with products + * of Renesas Electronics Corp. and its affiliates ("Renesas"). No other uses are authorized. Renesas products are + * sold pursuant to Renesas terms and conditions of sale. Purchasers are solely responsible for the selection and use + * of Renesas products and Renesas assumes no liability. No license, express or implied, to any intellectual property + * right is granted by Renesas. This software is protected under all applicable laws, including copyright laws. Renesas + * reserves the right to change or discontinue this software and/or this documentation. THE SOFTWARE AND DOCUMENTATION + * IS DELIVERED TO YOU "AS IS," AND RENESAS MAKES NO REPRESENTATIONS OR WARRANTIES, AND TO THE FULLEST EXTENT + * PERMISSIBLE UNDER APPLICABLE LAW, DISCLAIMS ALL WARRANTIES, WHETHER EXPLICITLY OR IMPLICITLY, INCLUDING WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NONINFRINGEMENT, WITH RESPECT TO THE SOFTWARE OR + * DOCUMENTATION. RENESAS SHALL HAVE NO LIABILITY ARISING OUT OF ANY SECURITY VULNERABILITY OR BREACH. TO THE MAXIMUM + * EXTENT PERMITTED BY LAW, IN NO EVENT WILL RENESAS BE LIABLE TO YOU IN CONNECTION WITH THE SOFTWARE OR DOCUMENTATION + * (OR ANY PERSON OR ENTITY CLAIMING RIGHTS DERIVED FROM YOU) FOR ANY LOSS, DAMAGES, OR CLAIMS WHATSOEVER, INCLUDING, + * WITHOUT LIMITATION, ANY DIRECT, CONSEQUENTIAL, SPECIAL, INDIRECT, PUNITIVE, OR INCIDENTAL DAMAGES; ANY LOST PROFITS, + * OTHER ECONOMIC DAMAGE, PROPERTY DAMAGE, OR PERSONAL INJURY; AND EVEN IF RENESAS HAS BEEN ADVISED OF THE POSSIBILITY + * OF SUCH LOSS, DAMAGES, CLAIMS OR COSTS. + **********************************************************************************************************************/ + +/*********************************************************************************************************************** + * Includes + **********************************************************************************************************************/ +#include +#include +#include "rm_motor_estimate.h" +#include "rm_motor_estimate_library.h" +#include "bsp_api.h" +#include "bsp_cfg.h" + +/*********************************************************************************************************************** + * Macro definitions + **********************************************************************************************************************/ + +#define MOTOR_ESTIMATE_OPEN (0X4D414553ULL) + +#define MOTOR_ESTIMATE_FLG_CLR (0U) +#define MOTOR_ESTIMATE_FLG_SET (1U) + +#define MOTOR_ESTIMATE_TWOPI (3.14159265358979F * 2.0F) + +#ifndef MTR_ESTIMATE_ERROR_RETURN + + #define MTR_ESTIMATE_ERROR_RETURN(a, err) FSP_ERROR_RETURN((a), (err)) +#endif + +/*********************************************************************************************************************** + * Typedef definitions + **********************************************************************************************************************/ + +/*********************************************************************************************************************** + * Private function prototypes + **********************************************************************************************************************/ + +/*********************************************************************************************************************** + * Private global variables + **********************************************************************************************************************/ + +/* Version data structure. */ +static const fsp_version_t g_motor_estimate_version = +{ + .api_version_major = MOTOR_ANGLE_API_VERSION_MAJOR, + .api_version_minor = MOTOR_ANGLE_API_VERSION_MINOR, + .code_version_major = MOTOR_ESTIMATE_CODE_VERSION_MAJOR, + .code_version_minor = MOTOR_ESTIMATE_CODE_VERSION_MINOR +}; + +/*********************************************************************************************************************** + * Global variables + **********************************************************************************************************************/ +const motor_angle_api_t g_motor_angle_on_motor_estimate = +{ + .open = RM_MOTOR_ESTIMATE_Open, + .close = RM_MOTOR_ESTIMATE_Close, + .reset = RM_MOTOR_ESTIMATE_Reset, + .currentSet = RM_MOTOR_ESTIMATE_CurrentSet, + .speedSet = RM_MOTOR_ESTIMATE_SpeedSet, + .flagPiCtrlSet = RM_MOTOR_ESTIMATE_FlagPiCtrlSet, + .angleSpeedGet = RM_MOTOR_ESTIMATE_AngleSpeedGet, + .estimatedComponentGet = RM_MOTOR_ESTIMATE_EstimatedComponentGet, + .parameterUpdate = RM_MOTOR_ESTIMATE_ParameterUpdate, + .versionGet = RM_MOTOR_ESTIMATE_VersionGet +}; + +/*******************************************************************************************************************//** + * @addtogroup MOTOR_ESTIMATE + * @{ + **********************************************************************************************************************/ + +/*********************************************************************************************************************** + * Functions + **********************************************************************************************************************/ + +/*******************************************************************************************************************//** + * @brief Opens and configures the Angle Estimation module. Implements @ref motor_angle_api_t::open. + * + * @retval FSP_SUCCESS MTR_ANGL_EST 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_MOTOR_ESTIMATE_Open (motor_angle_ctrl_t * const p_ctrl, motor_angle_cfg_t const * const p_cfg) +{ + fsp_err_t err = FSP_SUCCESS; + motor_estimate_instance_ctrl_t * p_instance_ctrl = (motor_estimate_instance_ctrl_t *) p_ctrl; + +#if (MOTOR_ESTIMATE_CFG_PARAM_CHECKING_ENABLE) + FSP_ASSERT(p_ctrl != NULL); + FSP_ASSERT(p_instance_ctrl != NULL); + FSP_ASSERT(p_cfg != NULL); +#endif + motor_estimate_extended_cfg_t * p_extended_cfg = (motor_estimate_extended_cfg_t *) p_cfg->p_extend; + +#if (MOTOR_ESTIMATE_CFG_PARAM_CHECKING_ENABLE) + FSP_ASSERT(p_extended_cfg != NULL); + FSP_ERROR_RETURN(0.0F <= p_extended_cfg->f_e_obs_omega, FSP_ERR_INVALID_ARGUMENT); + FSP_ERROR_RETURN(0.0F <= p_extended_cfg->f_e_obs_zeta, FSP_ERR_INVALID_ARGUMENT); + FSP_ERROR_RETURN(0.0F <= p_extended_cfg->f_pll_est_omega, FSP_ERR_INVALID_ARGUMENT); + FSP_ERROR_RETURN(0.0F <= p_extended_cfg->f_pll_est_zeta, FSP_ERR_INVALID_ARGUMENT); +#endif + + FSP_ERROR_RETURN(MOTOR_ESTIMATE_OPEN != p_instance_ctrl->open, FSP_ERR_ALREADY_OPEN); + + p_instance_ctrl->p_cfg = p_cfg; + + /* Mark driver as open */ + + rm_motor_estimate_bemf_obs_init(&(p_instance_ctrl->st_bemf_obs), &(p_extended_cfg->st_motor_params)); + + rm_motor_estimate_bemf_observer_gain_calc(&(p_extended_cfg->st_motor_params), + p_extended_cfg, + &(p_instance_ctrl->st_bemf_obs), + p_extended_cfg->f4_ctrl_period); + + rm_motor_estimate_speed_gain_calc(p_extended_cfg, &(p_instance_ctrl->st_pll_est), p_extended_cfg->f4_ctrl_period); + + rm_motor_estimate_speed_reset(&(p_instance_ctrl->st_pll_est)); + + p_instance_ctrl->f4_angle_rad = 0.0F; + p_instance_ctrl->u1_flg_pi_ctrl = MOTOR_ESTIMATE_FLG_CLR; + p_instance_ctrl->u1_flg_pll_start = MOTOR_ESTIMATE_FLG_SET; + + p_instance_ctrl->open = MOTOR_ESTIMATE_OPEN; + + return err; +} + +/*******************************************************************************************************************//** + * @brief Disables specified Angle Estimation module. Implements @ref motor_angle_api_t::close. + * + * @retval FSP_SUCCESS Successfully closed. + * @retval FSP_ERR_ASSERTION Null pointer. + * @retval FSP_ERR_NOT_OPEN Module is not open. + **********************************************************************************************************************/ +fsp_err_t RM_MOTOR_ESTIMATE_Close (motor_angle_ctrl_t * const p_ctrl) +{ + fsp_err_t err = FSP_SUCCESS; + motor_estimate_instance_ctrl_t * p_instance_ctrl = (motor_estimate_instance_ctrl_t *) p_ctrl; + +#if (MOTOR_ESTIMATE_CFG_PARAM_CHECKING_ENABLE) + FSP_ASSERT(p_instance_ctrl != NULL); + MTR_ESTIMATE_ERROR_RETURN(MOTOR_ESTIMATE_OPEN == p_instance_ctrl->open, FSP_ERR_NOT_OPEN); +#endif + + rm_motor_estimate_bemf_obs_reset(&(p_instance_ctrl->st_bemf_obs)); + rm_motor_estimate_speed_reset(&(p_instance_ctrl->st_pll_est)); + p_instance_ctrl->u1_flg_pi_ctrl = MOTOR_ESTIMATE_FLG_CLR; + + p_instance_ctrl->open = 0U; + + return err; +} + +/*******************************************************************************************************************//** + * @brief Reset variables of Angle Estimation module. Implements @ref motor_angle_api_t::reset + * + * @retval FSP_SUCCESS Successfully reset. + * @retval FSP_ERR_ASSERTION Null pointer. + * @retval FSP_ERR_NOT_OPEN Module is not open. + **********************************************************************************************************************/ +fsp_err_t RM_MOTOR_ESTIMATE_Reset (motor_angle_ctrl_t * const p_ctrl) +{ + fsp_err_t err = FSP_SUCCESS; + motor_estimate_instance_ctrl_t * p_instance_ctrl = (motor_estimate_instance_ctrl_t *) p_ctrl; + +#if (MOTOR_ESTIMATE_CFG_PARAM_CHECKING_ENABLE) + FSP_ASSERT(p_instance_ctrl != NULL); + MTR_ESTIMATE_ERROR_RETURN(MOTOR_ESTIMATE_OPEN == p_instance_ctrl->open, FSP_ERR_NOT_OPEN); +#endif + + rm_motor_estimate_bemf_obs_reset(&(p_instance_ctrl->st_bemf_obs)); + rm_motor_estimate_speed_reset(&(p_instance_ctrl->st_pll_est)); + p_instance_ctrl->u1_flg_pi_ctrl = MOTOR_ESTIMATE_FLG_CLR; + p_instance_ctrl->u1_flg_pll_start = MOTOR_ESTIMATE_FLG_SET; + + return err; +} + +/*******************************************************************************************************************//** + * @brief Set d/q-axis Current Data & Voltage Reference. Implements @ref motor_angle_api_t::currentSet + * + * @retval FSP_SUCCESS Successfully set. + * @retval FSP_ERR_ASSERTION Null pointer. + * @retval FSP_ERR_NOT_OPEN Module is not open. + **********************************************************************************************************************/ +fsp_err_t RM_MOTOR_ESTIMATE_CurrentSet (motor_angle_ctrl_t * const p_ctrl, + motor_angle_current_t * const p_st_current, + motor_angle_voltage_reference_t * const p_st_voltage) + +{ + fsp_err_t err = FSP_SUCCESS; + motor_estimate_instance_ctrl_t * p_instance_ctrl = (motor_estimate_instance_ctrl_t *) p_ctrl; + +#if (MOTOR_ESTIMATE_CFG_PARAM_CHECKING_ENABLE) + FSP_ASSERT(p_instance_ctrl != NULL); + FSP_ERROR_RETURN(MOTOR_ESTIMATE_OPEN == p_instance_ctrl->open, FSP_ERR_NOT_OPEN); +#endif + + if (FSP_SUCCESS == err) + { + p_instance_ctrl->st_input.f_id = p_st_current->id; + p_instance_ctrl->st_input.f_iq = p_st_current->iq; + p_instance_ctrl->st_input.f_vd_ref = p_st_voltage->vd; + p_instance_ctrl->st_input.f_vq_ref = p_st_voltage->vq; + } + + return err; +} + +/*******************************************************************************************************************//** + * @brief Set Speed Information. Implements @ref motor_angle_api_t::speedSet + * + * @retval FSP_SUCCESS Successfully set. + * @retval FSP_ERR_ASSERTION Null pointer. + * @retval FSP_ERR_NOT_OPEN Module is not open. + **********************************************************************************************************************/ +fsp_err_t RM_MOTOR_ESTIMATE_SpeedSet (motor_angle_ctrl_t * const p_ctrl, float const speed_ctrl, float const damp_speed) +{ + fsp_err_t err = FSP_SUCCESS; + motor_estimate_instance_ctrl_t * p_instance_ctrl = (motor_estimate_instance_ctrl_t *) p_ctrl; + +#if (MOTOR_ESTIMATE_CFG_PARAM_CHECKING_ENABLE) + FSP_ASSERT(p_instance_ctrl != NULL); + MTR_ESTIMATE_ERROR_RETURN(MOTOR_ESTIMATE_OPEN == p_instance_ctrl->open, FSP_ERR_NOT_OPEN); +#endif + + if (FSP_SUCCESS == err) + { + p_instance_ctrl->st_input.f4_ref_speed_rad_ctrl = speed_ctrl; + p_instance_ctrl->st_input.f4_damp_comp_speed = damp_speed; + } + + return err; +} + +/*******************************************************************************************************************//** + * @brief Set the flag of PI Control runs. Implements @ref motor_angle_api_t::flagPiCtrlSet + * + * @retval FSP_SUCCESS Successfully set. + * @retval FSP_ERR_ASSERTION Null pointer. + * @retval FSP_ERR_NOT_OPEN Module is not open. + **********************************************************************************************************************/ +fsp_err_t RM_MOTOR_ESTIMATE_FlagPiCtrlSet (motor_angle_ctrl_t * const p_ctrl, uint32_t const flag_pi) +{ + fsp_err_t err = FSP_SUCCESS; + motor_estimate_instance_ctrl_t * p_instance_ctrl = (motor_estimate_instance_ctrl_t *) p_ctrl; + +#if (MOTOR_ESTIMATE_CFG_PARAM_CHECKING_ENABLE) + FSP_ASSERT(p_instance_ctrl != NULL); + FSP_ERROR_RETURN(MOTOR_ESTIMATE_OPEN == p_instance_ctrl->open, FSP_ERR_NOT_OPEN); +#endif + + if (FSP_SUCCESS == err) + { + p_instance_ctrl->u1_flg_pi_ctrl = (uint8_t) flag_pi; + } + + return err; +} + +/*******************************************************************************************************************//** + * @brief Gets the current rotor's angle and rotation speed. Implements @ref motor_angle_api_t::angleSpeedGet + * + * @retval FSP_SUCCESS Successful data get. + * @retval FSP_ERR_ASSERTION Null pointer. + * @retval FSP_ERR_NOT_OPEN Module is not open. + **********************************************************************************************************************/ +fsp_err_t RM_MOTOR_ESTIMATE_AngleSpeedGet (motor_angle_ctrl_t * const p_ctrl, + float * const p_angle, + float * const p_speed, + float * const p_phase_err) +{ + float f_temp_angle_rad; + fsp_err_t err = FSP_SUCCESS; + motor_estimate_instance_ctrl_t * p_instance_ctrl = (motor_estimate_instance_ctrl_t *) p_ctrl; + +#if (MOTOR_ESTIMATE_CFG_PARAM_CHECKING_ENABLE) + FSP_ASSERT(p_instance_ctrl != NULL); + FSP_ASSERT(p_angle); + FSP_ASSERT(p_speed); + FSP_ASSERT(p_phase_err); + FSP_ERROR_RETURN(MOTOR_ESTIMATE_OPEN == p_instance_ctrl->open, FSP_ERR_NOT_OPEN); +#endif + + if (FSP_SUCCESS == err) + { + motor_estimate_extended_cfg_t * p_extended_cfg = + (motor_estimate_extended_cfg_t *) p_instance_ctrl->p_cfg->p_extend; + + /* Estimate voltage disturbance */ + rm_motor_estimate_bemf_observer(&(p_instance_ctrl->st_bemf_obs), + p_instance_ctrl->st_input.f_vd_ref, + p_instance_ctrl->st_input.f_vq_ref, + p_instance_ctrl->st_input.f_id, + p_instance_ctrl->st_input.f_iq); + + /* Calculate BEMF */ + p_instance_ctrl->f4_ed = rm_motor_estimate_bemf_calc_d(&(p_instance_ctrl->st_bemf_obs), + p_instance_ctrl->f4_speed_rad, + p_instance_ctrl->st_input.f_iq); + p_instance_ctrl->f4_eq = rm_motor_estimate_bemf_calc_q(&(p_instance_ctrl->st_bemf_obs), + p_instance_ctrl->f4_speed_rad, + p_instance_ctrl->st_input.f_id); + + /* Calculate phase error */ + p_instance_ctrl->f4_phase_err_rad = atanf(p_instance_ctrl->f4_ed / p_instance_ctrl->f4_eq); + *p_phase_err = p_instance_ctrl->f4_phase_err_rad; + + /* Prevent phase error becoming NAN when eq is 0 */ + if (isnan(p_instance_ctrl->f4_phase_err_rad)) + { + p_instance_ctrl->f4_phase_err_rad = 0.0F; + } + + if (MOTOR_ESTIMATE_FLG_SET == p_instance_ctrl->u1_flg_pi_ctrl) + { + if (MOTOR_ESTIMATE_FLG_SET == p_instance_ctrl->u1_flg_pll_start) + { + p_instance_ctrl->st_pll_est.f4_i_est_speed = p_instance_ctrl->st_input.f4_ref_speed_rad_ctrl; + p_instance_ctrl->u1_flg_pll_start = MOTOR_ESTIMATE_FLG_CLR; + } + + /* Estimate angle and speed */ + rm_motor_estimate_speed_pll(&(p_instance_ctrl->st_pll_est), + p_instance_ctrl->f4_phase_err_rad, + &(p_instance_ctrl->f4_speed_rad)); + } + else + { + p_instance_ctrl->u1_flg_pll_start = MOTOR_ESTIMATE_FLG_SET; + + if (MOTOR_ESTIMATE_OPENLOOP_DAMPING_ENABLE == p_extended_cfg->openloop_damping) + { + p_instance_ctrl->f4_speed_rad = + p_instance_ctrl->st_input.f4_ref_speed_rad_ctrl - p_instance_ctrl->st_input.f4_damp_comp_speed; + } + else + { + p_instance_ctrl->f4_speed_rad = p_instance_ctrl->st_input.f4_ref_speed_rad_ctrl; + } + } + + *p_speed = p_instance_ctrl->f4_speed_rad; + + f_temp_angle_rad = p_instance_ctrl->f4_angle_rad; + f_temp_angle_rad += (p_instance_ctrl->f4_speed_rad * (p_extended_cfg->f4_ctrl_period)); + + if (f_temp_angle_rad >= MOTOR_ESTIMATE_TWOPI) + { + f_temp_angle_rad = f_temp_angle_rad - MOTOR_ESTIMATE_TWOPI; + } + else if (f_temp_angle_rad < 0.0F) + { + f_temp_angle_rad = f_temp_angle_rad + MOTOR_ESTIMATE_TWOPI; + } + else + { + /* Do nothing */ + } + + p_instance_ctrl->f4_angle_rad = f_temp_angle_rad; + *p_angle = f_temp_angle_rad; + } + + return err; +} + +/*******************************************************************************************************************//** + * @brief Gets estimated d/q-axis component. Implements @ref motor_angle_api_t::estimatedComponentGet + * + * @retval FSP_SUCCESS Successfully data gotten. + * @retval FSP_ERR_ASSERTION Null pointer. + * @retval FSP_ERR_NOT_OPEN Module is not open. + **********************************************************************************************************************/ +fsp_err_t RM_MOTOR_ESTIMATE_EstimatedComponentGet (motor_angle_ctrl_t * const p_ctrl, + float * const p_ed, + float * const p_eq) +{ + fsp_err_t err = FSP_SUCCESS; + motor_estimate_instance_ctrl_t * p_instance_ctrl = (motor_estimate_instance_ctrl_t *) p_ctrl; + +#if (MOTOR_ESTIMATE_CFG_PARAM_CHECKING_ENABLE) + FSP_ASSERT(p_instance_ctrl != NULL); + FSP_ASSERT(p_ed != NULL); + FSP_ASSERT(p_eq != NULL); + FSP_ERROR_RETURN(MOTOR_ESTIMATE_OPEN == p_instance_ctrl->open, FSP_ERR_NOT_OPEN); +#endif + + if (FSP_SUCCESS == err) + { + *p_ed = p_instance_ctrl->f4_ed; + *p_eq = p_instance_ctrl->f4_eq; + } + + return err; +} + +/*******************************************************************************************************************//** + * @brief Update the parameters of Angle&Speed Estimation. Implements @ref motor_angle_api_t::parameterUpdate + * + * @retval FSP_SUCCESS Successfully data is update. + * @retval FSP_ERR_ASSERTION Null pointer. + * @retval FSP_ERR_NOT_OPEN Module is not open. + **********************************************************************************************************************/ +fsp_err_t RM_MOTOR_ESTIMATE_ParameterUpdate (motor_angle_ctrl_t * const p_ctrl, motor_angle_cfg_t const * const p_cfg) +{ + fsp_err_t err = FSP_SUCCESS; + motor_estimate_instance_ctrl_t * p_instance_ctrl = (motor_estimate_instance_ctrl_t *) p_ctrl; + +#if (MOTOR_ESTIMATE_CFG_PARAM_CHECKING_ENABLE) + FSP_ASSERT(p_instance_ctrl != NULL); + FSP_ASSERT(p_cfg != NULL); + FSP_ERROR_RETURN(MOTOR_ESTIMATE_OPEN == p_instance_ctrl->open, FSP_ERR_NOT_OPEN); +#endif + motor_estimate_extended_cfg_t * p_extended_cfg = (motor_estimate_extended_cfg_t *) p_cfg->p_extend; + + if (FSP_SUCCESS == err) + { + p_instance_ctrl->p_cfg = p_cfg; + + rm_motor_estimate_bemf_observer_gain_calc(&(p_extended_cfg->st_motor_params), + p_extended_cfg, + &(p_instance_ctrl->st_bemf_obs), + p_extended_cfg->f4_ctrl_period); + + rm_motor_estimate_speed_gain_calc(p_extended_cfg, + &(p_instance_ctrl->st_pll_est), + p_extended_cfg->f4_ctrl_period); + } + + return err; +} + +/*******************************************************************************************************************//** + * Return Motor Angle Estimation Middle module version. Implements @ref motor_angle_api_t::versionGet. + * + * @retval FSP_SUCCESS Version information successfully read. + * @retval FSP_ERR_ASSERTION Null pointer passed as a parameter + **********************************************************************************************************************/ +fsp_err_t RM_MOTOR_ESTIMATE_VersionGet (fsp_version_t * const p_version) +{ +#if (MOTOR_ESTIMATE_CFG_PARAM_CHECKING_ENABLE) + + /* Verify parameters are valid */ + FSP_ASSERT(p_version != NULL); +#endif + + p_version->version_id = g_motor_estimate_version.version_id; + + return FSP_SUCCESS; +} + +/*******************************************************************************************************************//** + * @} (end addtogroup MOTOR_ESTIMATE) + **********************************************************************************************************************/ diff --git a/ra/fsp/src/rm_motor_estimate/rm_motor_estimate_library.h b/ra/fsp/src/rm_motor_estimate/rm_motor_estimate_library.h new file mode 100644 index 000000000..add0e0dc2 --- /dev/null +++ b/ra/fsp/src/rm_motor_estimate/rm_motor_estimate_library.h @@ -0,0 +1,62 @@ +/*********************************************************************************************************************** + * DISCLAIMER + * This software is supplied by Renesas Electronics Corporation and is only intended for use with Renesas products. No + * other uses are authorized. This software is owned by Renesas Electronics Corporation and is protected under all + * applicable laws, including copyright laws. + * THIS SOFTWARE IS PROVIDED "AS IS" AND RENESAS MAKES NO WARRANTIES REGARDING + * THIS SOFTWARE, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. ALL SUCH WARRANTIES ARE EXPRESSLY DISCLAIMED. TO THE MAXIMUM + * EXTENT PERMITTED NOT PROHIBITED BY LAW, NEITHER RENESAS ELECTRONICS CORPORATION NOR ANY OF ITS AFFILIATED COMPANIES + * SHALL BE LIABLE FOR ANY DIRECT, INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANY REASON RELATED TO THIS + * SOFTWARE, EVEN IF RENESAS OR ITS AFFILIATES HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. + * Renesas reserves the right, without notice, to make changes to this software and to discontinue the availability of + * this software. By using this software, you agree to the additional terms and conditions found by accessing the + * following link: + * http://www.renesas.com/disclaimer + * + * Copyright (C) 2020 Renesas Electronics Corporation. All rights reserved. + ***********************************************************************************************************************/ + +/*********************************************************************************************************************** + * File Name : rm_motor_estimate_library.h + * Description : Header for BEMF Ovserver functions in library + ***********************************************************************************************************************/ + +/********************************************************************************************************************** + * History : DD.MM.YYYY Version + * : 01.09.2020 1.00 + ***********************************************************************************************************************/ + +#ifndef RM_MOTOR_ESTIMATE_LIBRARY_H +#define RM_MOTOR_ESTIMATE_LIBRARY_H + +/* Library functions */ +void rm_motor_estimate_bemf_observer(motor_estimate_bemf_observer_t * p_bemf_obs, + float f4_vd_ref, + float f4_vq_ref, + float f4_id, + float f4_iq); + +float rm_motor_estimate_bemf_calc_d(motor_estimate_bemf_observer_t * p_bemf_obs, float f4_speed_rad, float f4_iq); + +float rm_motor_estimate_bemf_calc_q(motor_estimate_bemf_observer_t * p_bemf_obs, float f4_speed_rad, float f4_id); + +void rm_motor_estimate_bemf_obs_init(motor_estimate_bemf_observer_t * p_bemf_obs, + motor_estimate_motor_parameter_t * p_motor_params); + +void rm_motor_estimate_bemf_observer_gain_calc(motor_estimate_motor_parameter_t * p_motor, + motor_estimate_extended_cfg_t * p_design_params, + motor_estimate_bemf_observer_t * p_bemf_obs, + float f4_ctrl_period); + +void rm_motor_estimate_bemf_obs_reset(motor_estimate_bemf_observer_t * p_bemf_obs); + +void rm_motor_estimate_speed_pll(motor_estimate_pll_est_t * p_pll_est, float f4_phase_err, float * p_f4_speed); + +void rm_motor_estimate_speed_gain_calc(motor_estimate_extended_cfg_t * p_design_params, + motor_estimate_pll_est_t * p_pll_est, + float f4_ctrl_period); + +void rm_motor_estimate_speed_reset(motor_estimate_pll_est_t * p_pll_est); + +#endif /* RM_MOTOR_ESTIMATE_LIBRARY_H */ diff --git a/ra/fsp/src/rm_motor_sensorless/rm_motor_sensorless.c b/ra/fsp/src/rm_motor_sensorless/rm_motor_sensorless.c new file mode 100644 index 000000000..57c72f106 --- /dev/null +++ b/ra/fsp/src/rm_motor_sensorless/rm_motor_sensorless.c @@ -0,0 +1,1160 @@ +/*********************************************************************************************************************** + * Copyright [2020] Renesas Electronics Corporation and/or its affiliates. All Rights Reserved. + * + * This software and documentation are supplied by Renesas Electronics America Inc. and may only be used with products + * of Renesas Electronics Corp. and its affiliates ("Renesas"). No other uses are authorized. Renesas products are + * sold pursuant to Renesas terms and conditions of sale. Purchasers are solely responsible for the selection and use + * of Renesas products and Renesas assumes no liability. No license, express or implied, to any intellectual property + * right is granted by Renesas. This software is protected under all applicable laws, including copyright laws. Renesas + * reserves the right to change or discontinue this software and/or this documentation. THE SOFTWARE AND DOCUMENTATION + * IS DELIVERED TO YOU "AS IS," AND RENESAS MAKES NO REPRESENTATIONS OR WARRANTIES, AND TO THE FULLEST EXTENT + * PERMISSIBLE UNDER APPLICABLE LAW, DISCLAIMS ALL WARRANTIES, WHETHER EXPLICITLY OR IMPLICITLY, INCLUDING WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NONINFRINGEMENT, WITH RESPECT TO THE SOFTWARE OR + * DOCUMENTATION. RENESAS SHALL HAVE NO LIABILITY ARISING OUT OF ANY SECURITY VULNERABILITY OR BREACH. TO THE MAXIMUM + * EXTENT PERMITTED BY LAW, IN NO EVENT WILL RENESAS BE LIABLE TO YOU IN CONNECTION WITH THE SOFTWARE OR DOCUMENTATION + * (OR ANY PERSON OR ENTITY CLAIMING RIGHTS DERIVED FROM YOU) FOR ANY LOSS, DAMAGES, OR CLAIMS WHATSOEVER, INCLUDING, + * WITHOUT LIMITATION, ANY DIRECT, CONSEQUENTIAL, SPECIAL, INDIRECT, PUNITIVE, OR INCIDENTAL DAMAGES; ANY LOST PROFITS, + * OTHER ECONOMIC DAMAGE, PROPERTY DAMAGE, OR PERSONAL INJURY; AND EVEN IF RENESAS HAS BEEN ADVISED OF THE POSSIBILITY + * OF SUCH LOSS, DAMAGES, CLAIMS OR COSTS. + **********************************************************************************************************************/ + +/*********************************************************************************************************************** + * Includes + **********************************************************************************************************************/ +#include +#include "rm_motor_sensorless.h" +#include "bsp_api.h" +#include "bsp_cfg.h" + +/*********************************************************************************************************************** + * Macro definitions + **********************************************************************************************************************/ + +#define MOTOR_SENSORLESS_OPEN (0X4D4F544ULL) + +#define MOTOR_SENSORLESS_RAD2RPM (30.0F / 3.14159265359F) + +#define MOTOR_SENSORLESS_FLG_CLR (0) +#define MOTOR_SENSORLESS_FLG_SET (1) + +/* For Statemachine */ +#define MOTOR_SENSORLESS_STATEMACHINE_SIZE_STATE (3) +#define MOTOR_SENSORLESS_STATEMACHINE_SIZE_EVENT (4) + +/* State machine error flags */ +#define MOTOR_SENSORLESS_STATEMACHINE_ERROR_NONE (0x00) /* None error flag */ +#define MOTOR_SENSORLESS_STATEMACHINE_ERROR_EVENTOUTBOUND (0x01) /* The event index is out of bound */ +#define MOTOR_SENSORLESS_STATEMACHINE_ERROR_STATEOUTBOUND (0x02) /* The state index is out of bound */ +#define MOTOR_SENSORLESS_STATEMACHINE_ERROR_ACTIONEXCEPTION (0x04) /* The action function returns failure */ + +#ifndef MOTOR_SENSORLESS_ERROR_RETURN + + #define MOTOR_SENSORLESS_ERROR_RETURN(a, err) FSP_ERROR_RETURN((a), (err)) +#endif + +/*********************************************************************************************************************** + * Typedef definitions + **********************************************************************************************************************/ + +/*********************************************************************************************************************** + * Private function prototypes + **********************************************************************************************************************/ + +/* Speed Control <=> Current Control Interface functions */ +void rm_motor_sensorless_current_callback(motor_current_callback_args_t * p_args); +void rm_motor_sensorless_speed_callback(motor_speed_callback_args_t * p_args); + +static uint16_t rm_motor_sensorless_error_check(motor_sensorless_instance_ctrl_t * p_ctrl, + float f_iu, + float f_iv, + float f_iw, + float f_vdc, + float f_speed); +static void rm_motor_sensorless_init_speed_input(motor_speed_input_t * st_input); +static void rm_motor_sensorless_init_speed_output(motor_speed_output_t * st_output); +static void rm_motor_sensorless_copy_speed_current(motor_speed_output_t * st_output, + motor_current_input_t * st_input); +static void rm_motor_sensorless_copy_current_speed(motor_current_output_t * st_output, + motor_speed_input_t * st_input); + +/* action functions */ +static uint8_t rm_motor_sensorless_active(motor_sensorless_instance_ctrl_t * p_ctrl); +static uint8_t rm_motor_sensorless_inactive(motor_sensorless_instance_ctrl_t * p_ctrl); +static uint8_t rm_motor_sensorless_reset(motor_sensorless_instance_ctrl_t * p_ctrl); +static uint8_t rm_motor_sensorless_error(motor_sensorless_instance_ctrl_t * p_ctrl); +static uint8_t rm_motor_sensorless_nowork(motor_sensorless_instance_ctrl_t * p_ctrl); + +static const uint8_t state_transition_table[MOTOR_SENSORLESS_STATEMACHINE_SIZE_EVENT][ + MOTOR_SENSORLESS_STATEMACHINE_SIZE_STATE +] = +{ +/* State 0:MOTOR_SENSORLESS_CTRL_STOP, 1:MOTOR_SENSORLESS_CTRL_RUN, 2:MOTOR_SENSORLESS_CTRL_ERROR */ +/* Event */ +/* 0:EVENT_STOP */ {MOTOR_SENSORLESS_CTRL_STOP, MOTOR_SENSORLESS_CTRL_STOP, MOTOR_SENSORLESS_CTRL_ERROR }, + +/* 1:EVENT_RUN */ {MOTOR_SENSORLESS_CTRL_RUN, MOTOR_SENSORLESS_CTRL_RUN, MOTOR_SENSORLESS_CTRL_ERROR }, + +/* 2:EVENT_ERROR */ {MOTOR_SENSORLESS_CTRL_ERROR, MOTOR_SENSORLESS_CTRL_ERROR, MOTOR_SENSORLESS_CTRL_ERROR }, + +/* 3:EVENT_RESET */ {MOTOR_SENSORLESS_CTRL_STOP, MOTOR_SENSORLESS_CTRL_ERROR, MOTOR_SENSORLESS_CTRL_STOP }, +}; + +typedef uint8_t (* motor_sensorless_action_t)(motor_sensorless_instance_ctrl_t * p_ctrl); + +static const motor_sensorless_action_t motor_sensorless_action_table[MOTOR_SENSORLESS_STATEMACHINE_SIZE_EVENT][ + MOTOR_SENSORLESS_STATEMACHINE_SIZE_STATE] = +{ +/* State 0:STOP, 1:RUN, 2:ERROR */ +/* Event */ +/* 0:EVENT_STOP */ {rm_motor_sensorless_inactive, rm_motor_sensorless_inactive, rm_motor_sensorless_nowork }, + +/* 1:EVENT_RUN */ {rm_motor_sensorless_active, rm_motor_sensorless_nowork, rm_motor_sensorless_nowork }, + +/* 2:EVENT_ERROR */ {rm_motor_sensorless_error, rm_motor_sensorless_error, rm_motor_sensorless_nowork }, + +/* 3:EVENT_RESET */ {rm_motor_sensorless_reset, rm_motor_sensorless_error, rm_motor_sensorless_reset }, +}; + +/* statemachine functions */ +static void rm_motor_sensorless_statemachine_init(motor_sensorless_statemachine_t * p_state_machine); +static void rm_motor_sensorless_statemachine_reset(motor_sensorless_statemachine_t * p_state_machine); +static uint16_t rm_motor_sensorless_statemachine_event(motor_sensorless_instance_ctrl_t * p_ctrl, + motor_sensorless_ctrl_event_t u1_event); + +/*********************************************************************************************************************** + * Private global variables + **********************************************************************************************************************/ + +/* Version data structure. */ +static const fsp_version_t g_motor_sensorless_version = +{ + .api_version_major = MOTOR_API_VERSION_MAJOR, + .api_version_minor = MOTOR_API_VERSION_MINOR, + .code_version_major = MOTOR_SENSORLESS_CODE_VERSION_MAJOR, + .code_version_minor = MOTOR_SENSORLESS_CODE_VERSION_MINOR +}; + +/*********************************************************************************************************************** + * Global variables + **********************************************************************************************************************/ +const motor_api_t g_motor_on_sensorless = +{ + .open = RM_MOTOR_SENSORLESS_Open, + .close = RM_MOTOR_SENSORLESS_Close, + .run = RM_MOTOR_SENSORLESS_Run, + .stop = RM_MOTOR_SENSORLESS_Stop, + .reset = RM_MOTOR_SENSORLESS_Reset, + .errorSet = RM_MOTOR_SENSORLESS_ErrorSet, + .speedSet = RM_MOTOR_SENSORLESS_SpeedSet, + .statusGet = RM_MOTOR_SENSORLESS_StatusGet, + .angleGet = RM_MOTOR_SENSORLESS_AngleGet, + .speedGet = RM_MOTOR_SENSORLESS_SpeedGet, + .errorCheck = RM_MOTOR_SENSORLESS_ErrorCheck, + .versionGet = RM_MOTOR_SENSORLESS_VersionGet, +}; + +/*******************************************************************************************************************//** + * @addtogroup MOTOR_SENSORLESS + * @{ + **********************************************************************************************************************/ + +/*********************************************************************************************************************** + * Functions + **********************************************************************************************************************/ + +/*******************************************************************************************************************//** + * Configure the MOTOR in register start mode. Implements @ref motor_api_t::open. + * + * This function should only be called once as MOTOR configuration registers can only be written to once so subsequent + * calls will have no effect. + * + * Example: + * @snippet rm_motor_sensorless_example.c RM_MOTOR_SENSORLESS_Open + * + * @retval FSP_SUCCESS MOTOR 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 + * + **********************************************************************************************************************/ +fsp_err_t RM_MOTOR_SENSORLESS_Open (motor_ctrl_t * const p_ctrl, motor_cfg_t const * const p_cfg) +{ + fsp_err_t err = FSP_SUCCESS; + motor_sensorless_instance_ctrl_t * p_instance_ctrl = (motor_sensorless_instance_ctrl_t *) p_ctrl; + +#if MOTOR_SENSORLESS_CFG_PARAM_CHECKING_ENABLE + FSP_ASSERT(NULL != p_instance_ctrl); + FSP_ASSERT(NULL != p_cfg); + MOTOR_SENSORLESS_ERROR_RETURN(MOTOR_SENSORLESS_OPEN != p_instance_ctrl->open, FSP_ERR_ALREADY_OPEN); + + /* using modules' instance check */ + MOTOR_SENSORLESS_ERROR_RETURN(NULL != p_cfg->p_motor_speed_instance, FSP_ERR_ASSERTION); + MOTOR_SENSORLESS_ERROR_RETURN(NULL != p_cfg->p_motor_current_instance, FSP_ERR_ASSERTION); + + MOTOR_SENSORLESS_ERROR_RETURN(NULL != p_cfg->p_extend, FSP_ERR_ASSERTION); + + motor_sensorless_extended_cfg_t * p_extended_cfg = (motor_sensorless_extended_cfg_t *) p_cfg->p_extend; + + MOTOR_SENSORLESS_ERROR_RETURN(p_extended_cfg->f_overcurrent_limit >= 0.0F, FSP_ERR_INVALID_ARGUMENT); + MOTOR_SENSORLESS_ERROR_RETURN(p_extended_cfg->f_overvoltage_limit >= 0.0F, FSP_ERR_INVALID_ARGUMENT); + MOTOR_SENSORLESS_ERROR_RETURN(p_extended_cfg->f_overspeed_limit >= 0.0F, FSP_ERR_INVALID_ARGUMENT); + MOTOR_SENSORLESS_ERROR_RETURN(p_extended_cfg->f_lowvoltage_limit >= 0.0F, FSP_ERR_INVALID_ARGUMENT); +#endif + + p_cfg->p_motor_speed_instance->p_api->open(p_cfg->p_motor_speed_instance->p_ctrl, + p_cfg->p_motor_speed_instance->p_cfg); + + p_cfg->p_motor_current_instance->p_api->open(p_cfg->p_motor_current_instance->p_ctrl, + p_cfg->p_motor_current_instance->p_cfg); + + p_instance_ctrl->p_cfg = p_cfg; + + p_instance_ctrl->u2_error_info = MOTOR_ERROR_NONE; + + rm_motor_sensorless_statemachine_init(&(p_instance_ctrl->st_statem)); + + rm_motor_sensorless_init_speed_input(&(p_instance_ctrl->st_speed_input)); + rm_motor_sensorless_init_speed_output(&(p_instance_ctrl->st_speed_output)); + + /* Mark driver as open */ + p_instance_ctrl->open = MOTOR_SENSORLESS_OPEN; + + return err; +} + +/*******************************************************************************************************************//** + * @brief Disables specified Motor Sensorless Control block. Implements @ref motor_api_t::close. + * + * Example: + * @snippet rm_motor_sensorless_example.c RM_MOTOR_SENSORLESS_Close + * + * @retval FSP_SUCCESS Successfully closed. + * @retval FSP_ERR_ASSERTION Null pointer. + * @retval FSP_ERR_NOT_OPEN Module is not open. + * + * @note + * + **********************************************************************************************************************/ +fsp_err_t RM_MOTOR_SENSORLESS_Close (motor_ctrl_t * const p_ctrl) +{ + fsp_err_t err = FSP_SUCCESS; + motor_sensorless_instance_ctrl_t * p_instance_ctrl = (motor_sensorless_instance_ctrl_t *) p_ctrl; + +#if MOTOR_SENSORLESS_CFG_PARAM_CHECKING_ENABLE + FSP_ASSERT(NULL != p_instance_ctrl); + MOTOR_SENSORLESS_ERROR_RETURN(MOTOR_SENSORLESS_OPEN == p_instance_ctrl->open, FSP_ERR_NOT_OPEN); + + /* using modules' instance check */ + MOTOR_SENSORLESS_ERROR_RETURN(NULL != p_instance_ctrl->p_cfg->p_motor_speed_instance, FSP_ERR_ASSERTION); + MOTOR_SENSORLESS_ERROR_RETURN(NULL != p_instance_ctrl->p_cfg->p_motor_current_instance, FSP_ERR_ASSERTION); +#endif + + /* close using modules */ + p_instance_ctrl->p_cfg->p_motor_speed_instance->p_api->close( + p_instance_ctrl->p_cfg->p_motor_speed_instance->p_ctrl); + + p_instance_ctrl->p_cfg->p_motor_current_instance->p_api->close( + p_instance_ctrl->p_cfg->p_motor_current_instance->p_ctrl); + + rm_motor_sensorless_init_speed_input(&(p_instance_ctrl->st_speed_input)); + rm_motor_sensorless_init_speed_output(&(p_instance_ctrl->st_speed_output)); + + p_instance_ctrl->open = 0; + + return err; +} + +/*******************************************************************************************************************//** + * @brief Reset Motor Sensorless Control block. Implements @ref motor_api_t::reset. + * + * Example: + * @snippet rm_motor_sensorless_example.c RM_MOTOR_SENSORLESS_Reset + * + * @retval FSP_SUCCESS Successfully resetted. + * @retval FSP_ERR_ASSERTION Null pointer. + * @retval FSP_ERR_NOT_OPEN Module is not open. + * + * @note + * + **********************************************************************************************************************/ +fsp_err_t RM_MOTOR_SENSORLESS_Reset (motor_ctrl_t * const p_ctrl) +{ + fsp_err_t err = FSP_SUCCESS; + motor_sensorless_instance_ctrl_t * p_instance_ctrl = (motor_sensorless_instance_ctrl_t *) p_ctrl; + +#if MOTOR_SENSORLESS_CFG_PARAM_CHECKING_ENABLE + FSP_ASSERT(NULL != p_instance_ctrl); + MOTOR_SENSORLESS_ERROR_RETURN(MOTOR_SENSORLESS_OPEN == p_instance_ctrl->open, FSP_ERR_NOT_OPEN); +#endif + + /* Reset is valid only at "ERROR" status */ + if (MOTOR_ERROR_NONE != p_instance_ctrl->u2_error_info) + { + rm_motor_sensorless_statemachine_event(p_instance_ctrl, MOTOR_SENSORLESS_CTRL_EVENT_RESET); + + rm_motor_sensorless_init_speed_input(&(p_instance_ctrl->st_speed_input)); + rm_motor_sensorless_init_speed_output(&(p_instance_ctrl->st_speed_output)); + + /* clear error info */ + p_instance_ctrl->u2_error_info = MOTOR_ERROR_NONE; + } + + return err; +} + +/*******************************************************************************************************************//** + * @brief Run Motor (Start motor rotation). Implements @ref motor_api_t::run. + * + * Example: + * @snippet rm_motor_sensorless_example.c RM_MOTOR_SENSORLESS_Run + * + * @retval FSP_SUCCESS Successfully resetted. + * @retval FSP_ERR_ASSERTION Null pointer. + * @retval FSP_ERR_NOT_OPEN Module is not open. + * + * @note + * + **********************************************************************************************************************/ +fsp_err_t RM_MOTOR_SENSORLESS_Run (motor_ctrl_t * const p_ctrl) +{ + fsp_err_t err = FSP_SUCCESS; + motor_sensorless_instance_ctrl_t * p_instance_ctrl = (motor_sensorless_instance_ctrl_t *) p_ctrl; + +#if MOTOR_SENSORLESS_CFG_PARAM_CHECKING_ENABLE + FSP_ASSERT(NULL != p_instance_ctrl); + MOTOR_SENSORLESS_ERROR_RETURN(MOTOR_SENSORLESS_OPEN == p_instance_ctrl->open, FSP_ERR_NOT_OPEN); +#endif + + rm_motor_sensorless_statemachine_event(p_instance_ctrl, MOTOR_SENSORLESS_CTRL_EVENT_RUN); + + return err; +} + +/*******************************************************************************************************************//** + * @brief Stop Motor (Stop motor rotation). Implements @ref motor_api_t::stop. + * + * Example: + * @snippet rm_motor_sensorless_example.c RM_MOTOR_SENSORLESS_Stop + * + * @retval FSP_SUCCESS Successfully resetted. + * @retval FSP_ERR_ASSERTION Null pointer. + * @retval FSP_ERR_NOT_OPEN Module is not open. + * + * @note + * + **********************************************************************************************************************/ +fsp_err_t RM_MOTOR_SENSORLESS_Stop (motor_ctrl_t * const p_ctrl) +{ + fsp_err_t err = FSP_SUCCESS; + motor_sensorless_instance_ctrl_t * p_instance_ctrl = (motor_sensorless_instance_ctrl_t *) p_ctrl; + +#if MOTOR_SENSORLESS_CFG_PARAM_CHECKING_ENABLE + FSP_ASSERT(NULL != p_instance_ctrl); + MOTOR_SENSORLESS_ERROR_RETURN(MOTOR_SENSORLESS_OPEN == p_instance_ctrl->open, FSP_ERR_NOT_OPEN); +#endif + + rm_motor_sensorless_statemachine_event(p_instance_ctrl, MOTOR_SENSORLESS_CTRL_EVENT_STOP); + + return err; +} + +/*******************************************************************************************************************//** + * @brief Set error information. Implements @ref motor_api_t::errorSet. + * + * Example: + * @snippet rm_motor_sensorless_example.c RM_MOTOR_SENSORLESS_ErrorSet + * + * @retval FSP_SUCCESS Successfully resetted. + * @retval FSP_ERR_ASSERTION Null pointer. + * @retval FSP_ERR_NOT_OPEN Module is not open. + * + * @note + * + **********************************************************************************************************************/ +fsp_err_t RM_MOTOR_SENSORLESS_ErrorSet (motor_ctrl_t * const p_ctrl, motor_error_t const error) +{ + fsp_err_t err = FSP_SUCCESS; + motor_sensorless_instance_ctrl_t * p_instance_ctrl = (motor_sensorless_instance_ctrl_t *) p_ctrl; + +#if MOTOR_SENSORLESS_CFG_PARAM_CHECKING_ENABLE + FSP_ASSERT(NULL != p_instance_ctrl); + MOTOR_SENSORLESS_ERROR_RETURN(MOTOR_SENSORLESS_OPEN == p_instance_ctrl->open, FSP_ERR_NOT_OPEN); +#endif + + p_instance_ctrl->u2_error_info |= (uint16_t) error; + rm_motor_sensorless_statemachine_event(p_instance_ctrl, MOTOR_SENSORLESS_CTRL_EVENT_ERROR); + + return err; +} + +/*******************************************************************************************************************//** + * @brief Set speed reference[rpm]. Implements @ref motor_api_t::speedSet. + * + * Example: + * @snippet rm_motor_sensorless_example.c RM_MOTOR_SENSORLESS_SpeedSet + * + * @retval FSP_SUCCESS Successfully resetted. + * @retval FSP_ERR_ASSERTION Null pointer. + * @retval FSP_ERR_NOT_OPEN Module is not open. + * + * @note + * + **********************************************************************************************************************/ +fsp_err_t RM_MOTOR_SENSORLESS_SpeedSet (motor_ctrl_t * const p_ctrl, float const speed_rpm) +{ + fsp_err_t err = FSP_SUCCESS; + motor_sensorless_instance_ctrl_t * p_instance_ctrl = (motor_sensorless_instance_ctrl_t *) p_ctrl; + +#if MOTOR_SENSORLESS_CFG_PARAM_CHECKING_ENABLE + FSP_ASSERT(NULL != p_instance_ctrl); + MOTOR_SENSORLESS_ERROR_RETURN(MOTOR_SENSORLESS_OPEN == p_instance_ctrl->open, FSP_ERR_NOT_OPEN); +#endif + + if (p_instance_ctrl->p_cfg->p_motor_speed_instance != NULL) + { + err = p_instance_ctrl->p_cfg->p_motor_speed_instance->p_api->speedReferenceSet( + p_instance_ctrl->p_cfg->p_motor_speed_instance->p_ctrl, + speed_rpm); + } + + return err; +} + +/*******************************************************************************************************************//** + * @brief Get current control status. Implements @ref motor_api_t::statusGet. + * + * Example: + * @snippet rm_motor_sensorless_example.c RM_MOTOR_SENSORLESS_StatusGet + * + * @retval FSP_SUCCESS Successfully resetted. + * @retval FSP_ERR_ASSERTION Null pointer. + * @retval FSP_ERR_NOT_OPEN Module is not open. + * @retval FSP_ERR_INVALID_ARGUMENT Data received pointer is invalid.. + * + * @note + * + **********************************************************************************************************************/ +fsp_err_t RM_MOTOR_SENSORLESS_StatusGet (motor_ctrl_t * const p_ctrl, uint8_t * const p_status) +{ + fsp_err_t err = FSP_SUCCESS; + motor_sensorless_instance_ctrl_t * p_instance_ctrl = (motor_sensorless_instance_ctrl_t *) p_ctrl; + +#if MOTOR_SENSORLESS_CFG_PARAM_CHECKING_ENABLE + FSP_ASSERT(p_instance_ctrl); + MOTOR_SENSORLESS_ERROR_RETURN(MOTOR_SENSORLESS_OPEN == p_instance_ctrl->open, FSP_ERR_NOT_OPEN); + MOTOR_SENSORLESS_ERROR_RETURN(p_status != NULL, FSP_ERR_INVALID_ARGUMENT); +#endif + + *p_status = (uint8_t) (p_instance_ctrl->st_statem.u1_status); + + return err; +} + +/*******************************************************************************************************************//** + * @brief Get current rotor angle. Implements @ref motor_api_t::angleGet. + * + * Example: + * @snippet rm_motor_sensorless_example.c RM_MOTOR_SENSORLESS_AngleGet + * + * @retval FSP_SUCCESS Successfully resetted. + * @retval FSP_ERR_ASSERTION Null pointer. + * @retval FSP_ERR_NOT_OPEN Module is not open. + * @retval FSP_ERR_INVALID_ARGUMENT Data received pointer is invalid.. + * + * @note + * + **********************************************************************************************************************/ +fsp_err_t RM_MOTOR_SENSORLESS_AngleGet (motor_ctrl_t * const p_ctrl, float * const p_angle_rad) +{ + motor_current_output_t temp_current_output; + fsp_err_t err = FSP_SUCCESS; + motor_sensorless_instance_ctrl_t * p_instance_ctrl = (motor_sensorless_instance_ctrl_t *) p_ctrl; + temp_current_output.f_rotor_angle = 0.0F; + +#if MOTOR_SENSORLESS_CFG_PARAM_CHECKING_ENABLE + FSP_ASSERT(p_instance_ctrl); + MOTOR_SENSORLESS_ERROR_RETURN(MOTOR_SENSORLESS_OPEN == p_instance_ctrl->open, FSP_ERR_NOT_OPEN); + MOTOR_SENSORLESS_ERROR_RETURN(p_angle_rad != NULL, FSP_ERR_INVALID_ARGUMENT); +#endif + + motor_current_instance_t const * p_current_instance = + p_instance_ctrl->p_cfg->p_motor_current_instance; + + if (p_current_instance != NULL) + { + err = p_current_instance->p_api->parameterGet(p_current_instance->p_ctrl, &temp_current_output); + } + + *p_angle_rad = temp_current_output.f_rotor_angle; + + return err; +} + +/*******************************************************************************************************************//** + * @brief Get rotational speed. Implements @ref motor_api_t::speedGet. + * + * Example: + * @snippet rm_motor_sensorless_example.c RM_MOTOR_SENSORLESS_SpeedGet + * + * @retval FSP_SUCCESS Successfully resetted. + * @retval FSP_ERR_ASSERTION Null pointer. + * @retval FSP_ERR_NOT_OPEN Module is not open. + * @retval FSP_ERR_INVALID_ARGUMENT Data received pointer is invalid.. + * + * @note + * + **********************************************************************************************************************/ +fsp_err_t RM_MOTOR_SENSORLESS_SpeedGet (motor_ctrl_t * const p_ctrl, float * const p_speed_rpm) +{ + motor_current_output_t temp_current_output; + fsp_err_t err = FSP_SUCCESS; + motor_sensorless_instance_ctrl_t * p_instance_ctrl = (motor_sensorless_instance_ctrl_t *) p_ctrl; + temp_current_output.f_speed_rpm = 0.0F; + +#if MOTOR_SENSORLESS_CFG_PARAM_CHECKING_ENABLE + FSP_ASSERT(p_instance_ctrl); + MOTOR_SENSORLESS_ERROR_RETURN(MOTOR_SENSORLESS_OPEN == p_instance_ctrl->open, FSP_ERR_NOT_OPEN); + MOTOR_SENSORLESS_ERROR_RETURN(p_speed_rpm != NULL, FSP_ERR_INVALID_ARGUMENT); +#endif + + motor_current_instance_t const * p_current_instance = + p_instance_ctrl->p_cfg->p_motor_current_instance; + + if (p_current_instance != NULL) + { + err = p_current_instance->p_api->parameterGet(p_current_instance->p_ctrl, &temp_current_output); + } + + *p_speed_rpm = temp_current_output.f_speed_rpm; + + return err; +} + +/*******************************************************************************************************************//** + * @brief Check the occurunce of Error. Implements @ref motor_api_t::errorCheck. + * + * Example: + * @snippet rm_motor_sensorless_example.c RM_MOTOR_SENSORLESS_ErrorCheck + * + * @retval FSP_SUCCESS Successfully resetted. + * @retval FSP_ERR_ASSERTION Null pointer. + * @retval FSP_ERR_NOT_OPEN Module is not open. + * @retval FSP_ERR_INVALID_ARGUMENT Data received pointer is invalid.. + * + * @note + * + **********************************************************************************************************************/ +fsp_err_t RM_MOTOR_SENSORLESS_ErrorCheck (motor_ctrl_t * const p_ctrl, uint16_t * const p_error) +{ + motor_current_output_t temp_current_output; + float temp_iu = 0.0F; + float temp_iv = 0.0F; + float temp_iw = 0.0F; + motor_driver_current_get_t temp_crnt_get; + temp_crnt_get.vdc = 0.0F; + + fsp_err_t err = FSP_SUCCESS; + motor_sensorless_instance_ctrl_t * p_instance_ctrl = (motor_sensorless_instance_ctrl_t *) p_ctrl; + +#if MOTOR_SENSORLESS_CFG_PARAM_CHECKING_ENABLE + FSP_ASSERT(p_instance_ctrl); + MOTOR_SENSORLESS_ERROR_RETURN(MOTOR_SENSORLESS_OPEN == p_instance_ctrl->open, FSP_ERR_NOT_OPEN); + MOTOR_SENSORLESS_ERROR_RETURN(p_error != NULL, FSP_ERR_INVALID_ARGUMENT); +#endif + + motor_current_instance_t const * p_current_instance = + p_instance_ctrl->p_cfg->p_motor_current_instance; + motor_driver_instance_t const * p_driver_instance = + p_instance_ctrl->p_cfg->p_motor_current_instance->p_cfg->p_motor_driver_instance; + + motor_current_instance_ctrl_t * p_current_instance_ctrl = + (motor_current_instance_ctrl_t *) p_instance_ctrl->p_cfg->p_motor_current_instance->p_ctrl; + + if (p_current_instance != NULL) + { + err = p_current_instance->p_api->parameterGet(p_current_instance->p_ctrl, &temp_current_output); + } + + if (p_driver_instance != NULL) + { + err = p_driver_instance->p_api->currentGet(p_driver_instance->p_ctrl, &temp_crnt_get); + if (MOTOR_SENSORLESS_FLG_SET == p_current_instance_ctrl->u1_flag_crnt_offset) + { + temp_iu = temp_crnt_get.iu; + temp_iw = temp_crnt_get.iw; + temp_iv = -(temp_iu + temp_iw); + } + } + + p_instance_ctrl->u2_error_info |= + rm_motor_sensorless_error_check(p_instance_ctrl, + temp_iu, + temp_iv, + temp_iw, + temp_crnt_get.vdc, + temp_current_output.f_speed_rpm); + + *p_error = p_instance_ctrl->u2_error_info; + + if (MOTOR_ERROR_NONE != p_instance_ctrl->u2_error_info) + { + rm_motor_sensorless_statemachine_event(p_instance_ctrl, MOTOR_SENSORLESS_CTRL_EVENT_ERROR); + } + + return err; +} + +/*******************************************************************************************************************//** + * Return MOTOR Sensorless driver version. Implements @ref motor_api_t::versionGet. + * + * @retval FSP_SUCCESS Version information successfully read. + * @retval FSP_ERR_ASSERTION Null pointer passed as a parameter + **********************************************************************************************************************/ +fsp_err_t RM_MOTOR_SENSORLESS_VersionGet (fsp_version_t * const p_version) +{ +#if MOTOR_SENSORLESS_CFG_PARAM_CHECKING_ENABLE + FSP_ASSERT(NULL != p_version); +#endif + + p_version->version_id = g_motor_sensorless_version.version_id; + + return FSP_SUCCESS; +} + +/*******************************************************************************************************************//** + * @} (end addtogroup MOTOR_SENSORLESS) + **********************************************************************************************************************/ + +/*********************************************************************************************************************** + * Private Functions + **********************************************************************************************************************/ + +/***** For Status Transition *****/ + +/*********************************************************************************************************************** + * Function Name : rm_motor_sensorless_active + * Description : Activates the motor control system and enables PWM output + * Arguments : p_ctrl - Pointer to Sensorless Motor control structure + * Return Value : error information + **********************************************************************************************************************/ +static uint8_t rm_motor_sensorless_active (motor_sensorless_instance_ctrl_t * p_ctrl) +{ + fsp_err_t err = FSP_SUCCESS; + if (p_ctrl->p_cfg->p_motor_speed_instance != NULL) + { + err = p_ctrl->p_cfg->p_motor_speed_instance->p_api->run(p_ctrl->p_cfg->p_motor_speed_instance->p_ctrl); + } + + if (p_ctrl->p_cfg->p_motor_current_instance != NULL) + { + err = p_ctrl->p_cfg->p_motor_current_instance->p_api->run(p_ctrl->p_cfg->p_motor_current_instance->p_ctrl); + } + + return (uint8_t) err; +} /* End of function rm_motor_sensorless_active() */ + +/*********************************************************************************************************************** + * Function Name : rm_motor_sensorless_inactive + * Description : Deactivates the motor control system and disables PWM output + * Arguments : p_ctrl - Pointer to Sensorless Motor control structure + * Return Value : error information + **********************************************************************************************************************/ +static uint8_t rm_motor_sensorless_inactive (motor_sensorless_instance_ctrl_t * p_ctrl) +{ + fsp_err_t err = FSP_SUCCESS; + if (p_ctrl->p_cfg->p_motor_speed_instance != NULL) + { + err = p_ctrl->p_cfg->p_motor_speed_instance->p_api->reset(p_ctrl->p_cfg->p_motor_speed_instance->p_ctrl); + } + + if (p_ctrl->p_cfg->p_motor_current_instance != NULL) + { + err = p_ctrl->p_cfg->p_motor_current_instance->p_api->reset(p_ctrl->p_cfg->p_motor_current_instance->p_ctrl); + } + + return (uint8_t) err; +} /* End of function rm_motor_sensorless_inactive() */ + +/*********************************************************************************************************************** + * Function Name : rm_motor_sensorless_nowork + * Description : The empty dummy function used to fill the blank in action table + * Arguments : p_ctrl - Pointer to Sensorless Motor control structure + * Return Value : Always success (0) + **********************************************************************************************************************/ +static uint8_t rm_motor_sensorless_nowork (motor_sensorless_instance_ctrl_t * p_ctrl) +{ + /* Do Nothing */ + + if (p_ctrl->p_cfg->p_motor_speed_instance != NULL) + { + } + + return 0; +} /* End of function rm_motor_sensorless_nowork() */ + +/*********************************************************************************************************************** + * Function Name : rm_motor_sensorless_reset + * Description : Resets the configurations to default and clear error flags + * Arguments : p_ctrl - Pointer to Sensorless Motor control structure + * Return Value : The result of action, Always success(0) + **********************************************************************************************************************/ +static uint8_t rm_motor_sensorless_reset (motor_sensorless_instance_ctrl_t * p_ctrl) +{ + if (p_ctrl->p_cfg->p_motor_speed_instance != NULL) + { + p_ctrl->p_cfg->p_motor_speed_instance->p_api->reset(p_ctrl->p_cfg->p_motor_speed_instance->p_ctrl); + } + + if (p_ctrl->p_cfg->p_motor_current_instance != NULL) + { + p_ctrl->p_cfg->p_motor_current_instance->p_api->reset(p_ctrl->p_cfg->p_motor_current_instance->p_ctrl); + } + + return 0; +} /* End of function rm_motor_sensorless_reset() */ + +/*********************************************************************************************************************** + * Function Name : rm_motor_sensorless_error + * Description : Executes the post-processing (include stopping the PWM output) when an error has been detected + * Arguments : p_ctrl - Pointer to Sensorless Motor control structure + * Return Value : The result of action, Always success(0) + **********************************************************************************************************************/ +static uint8_t rm_motor_sensorless_error (motor_sensorless_instance_ctrl_t * p_ctrl) +{ + if (p_ctrl->p_cfg->p_motor_speed_instance != NULL) + { + p_ctrl->p_cfg->p_motor_speed_instance->p_api->reset(p_ctrl->p_cfg->p_motor_speed_instance->p_ctrl); + } + + if (p_ctrl->p_cfg->p_motor_current_instance != NULL) + { + p_ctrl->p_cfg->p_motor_current_instance->p_api->reset(p_ctrl->p_cfg->p_motor_current_instance->p_ctrl); + } + + return 0; +} /* End of function mtr_act_error */ + +/*********************************************************************************************************************** + * Function Name : rm_motor_sensorless_statemachine_init + * Description : Initializes state machine for motor drive system + * Arguments : p_state_machine - the pointer to the state machine data structure + * Return Value : None + **********************************************************************************************************************/ +static void rm_motor_sensorless_statemachine_init (motor_sensorless_statemachine_t * p_state_machine) +{ + rm_motor_sensorless_statemachine_reset(p_state_machine); +} /* End of function rm_motor_sensorless_statemachine_init */ + +/*********************************************************************************************************************** + * Function Name : rm_motor_sensorless_statemachine_reset + * Description : Resets state machine + * Arguments : p_state_machine - the pointer to the state machine data structure + * Return Value : None + **********************************************************************************************************************/ +static void rm_motor_sensorless_statemachine_reset (motor_sensorless_statemachine_t * p_state_machine) +{ + p_state_machine->u1_status = MOTOR_SENSORLESS_CTRL_STOP; + p_state_machine->u1_status_next = MOTOR_SENSORLESS_CTRL_STOP; + p_state_machine->u2_error_status = MOTOR_SENSORLESS_STATEMACHINE_ERROR_NONE; +} /* End of function rm_motor_sensorless_statemachine_reset */ + +/*********************************************************************************************************************** + * Function Name : rm_motor_sensorless_statemachine_event + * Description : Performs state transition and execute corresponding action when + * specified event happen + * Arguments : p_ctrl - the pointer to the structure of motor senserless control + * u1_event - the event index to be executed + * MOTOR_SENSORLESS_CTRL_EVENT_STOP Stop the motor drive system + * MOTOR_SENSORLESS_CTRL_EVENT_RUN Activate the motor drive system + * MOTOR_SENSORLESS_CTRL_EVENT_ERROR Throw an error and stop driving + * MOTOR_SENSORLESS_CTRL_EVENT_RESET Reset the configurations of motor drive system + * Return Value : The error flags of the state machine + * BIT0: Event index is out of bound, please check the u1_event + * BIT1: State index is out of bound + * BIT2: Action error (value other than 0 returned from action) + **********************************************************************************************************************/ +static uint16_t rm_motor_sensorless_statemachine_event (motor_sensorless_instance_ctrl_t * p_ctrl, + motor_sensorless_ctrl_event_t u1_event) +{ + motor_sensorless_action_t func_action; + uint8_t action_ret; + + p_ctrl->st_statem.u2_error_status = MOTOR_SENSORLESS_STATEMACHINE_ERROR_NONE; + + /* Check if accessing state transition table out of bound */ + if (MOTOR_SENSORLESS_STATEMACHINE_SIZE_EVENT <= u1_event) + { + /* Event is out of bound */ + u1_event = MOTOR_SENSORLESS_CTRL_EVENT_ERROR; + p_ctrl->st_statem.u2_error_status |= MOTOR_SENSORLESS_STATEMACHINE_ERROR_EVENTOUTBOUND; + } + + if (MOTOR_SENSORLESS_STATEMACHINE_SIZE_STATE <= p_ctrl->st_statem.u1_status) + { + /* State is out of bound */ + u1_event = MOTOR_SENSORLESS_CTRL_EVENT_ERROR; + p_ctrl->st_statem.u1_status = MOTOR_SENSORLESS_CTRL_STOP; + p_ctrl->st_statem.u2_error_status |= MOTOR_SENSORLESS_STATEMACHINE_ERROR_STATEOUTBOUND; + } + + /* + * u1_current_event : Event happening + * u1_status : Current status + * u1_status_next : Status after action executed + */ + p_ctrl->st_statem.u1_current_event = u1_event; + p_ctrl->st_statem.u1_status_next = + (motor_sensorless_ctrl_status_t) (state_transition_table[u1_event][p_ctrl->st_statem.u1_status]); + + /* Get action function from action table and execute action */ + func_action = motor_sensorless_action_table[u1_event][p_ctrl->st_statem.u1_status]; + action_ret = func_action(p_ctrl); + + /* If return value is not zero, set the Action Exception flag */ + if (action_ret != 0U) + { + p_ctrl->st_statem.u2_error_status |= MOTOR_SENSORLESS_STATEMACHINE_ERROR_ACTIONEXCEPTION; + } + + p_ctrl->st_statem.u1_status = p_ctrl->st_statem.u1_status_next; + + return p_ctrl->st_statem.u2_error_status; +} /* End of function rm_motor_sensorless_statemachine_event */ + +/***** For Status Transition *****/ + +/*********************************************************************************************************************** + * Function Name : rm_motor_check_over_speed_error + * Description : Checks over-speed error + * Arguments : f4_speed_rad - The electrical speed[rad/s] + * f4_speed_limit_rad - The speed[rad/s] threshold of the over-speed error, should be a positive value + * Return Value : The over-speed error flag + **********************************************************************************************************************/ +static inline uint16_t rm_motor_check_over_speed_error (float f4_speed_rad, float f4_speed_limit_rad) +{ + float f4_temp0; + uint16_t u2_temp0; + + u2_temp0 = MOTOR_ERROR_NONE; + + f4_temp0 = fabsf(f4_speed_rad); + if (f4_temp0 > f4_speed_limit_rad) + { + u2_temp0 = MOTOR_ERROR_OVER_SPEED; + } + + return u2_temp0; +} /* End of function rm_motor_check_over_speed_error */ + +/*********************************************************************************************************************** + * Function Name : rm_motor_check_over_voltage_error + * Description : Checks over-voltage error + * Arguments : f4_vdc - The actual Vdc value [V] + * f4_overvoltage_limit - The threshold voltage[V] of over-voltage error + * Return Value : The flag of over-voltage error + **********************************************************************************************************************/ +static inline uint16_t rm_motor_check_over_voltage_error (float f4_vdc, float f4_overvoltage_limit) +{ + uint16_t u2_temp0; + + u2_temp0 = MOTOR_ERROR_NONE; + + if (f4_vdc > f4_overvoltage_limit) + { + u2_temp0 = MOTOR_ERROR_OVER_VOLTAGE; + } + + return u2_temp0; +} /* End of function rm_motor_check_over_voltage_error */ + +/*********************************************************************************************************************** + * Function Name : rm_motor_check_low_voltage_error + * Description : Checks low-voltage error + * Arguments : f4_vdc - The actual Vdc value [V] + * f4_lowvoltage_limit - The threshold voltage[V] of low-voltage error + * Return Value : The flag of low-voltage error + **********************************************************************************************************************/ +static inline uint16_t rm_motor_check_low_voltage_error (float f4_vdc, float f4_lowvoltage_limit) +{ + uint16_t u2_temp0; + + u2_temp0 = MOTOR_ERROR_NONE; + + if (f4_vdc < f4_lowvoltage_limit) + { + u2_temp0 = MOTOR_ERROR_LOW_VOLTAGE; + } + + return u2_temp0; +} /* End of function rm_motor_check_low_voltage_error */ + +/*********************************************************************************************************************** + * Function Name : rm_motor_check_over_current_error + * Description : Checks over-current error + * Arguments : f4_iu - The actual U-phase current value [A] + * f4_iv - The actual V-phase current value [A] + * f4_iw - The actual W-phase current value [A] + * f4_oc_limit - The threshold current[A] of over-current error + * Return Value : The flag of over-current error + **********************************************************************************************************************/ +static inline uint16_t rm_motor_check_over_current_error (float f4_iu, float f4_iv, float f4_iw, float f4_oc_limit) +{ + float f4_temp0 = 0.0F; + uint16_t u2_temp0 = 0U; + + u2_temp0 = MOTOR_ERROR_NONE; /* Initialize */ + + f4_temp0 = fabsf(f4_iu); + if (f4_temp0 > f4_oc_limit) + { + u2_temp0 = 1U; + } + + f4_temp0 = fabsf(f4_iv); + if (f4_temp0 > f4_oc_limit) + { + u2_temp0 = 1U; + } + + f4_temp0 = fabsf(f4_iw); + if (f4_temp0 > f4_oc_limit) + { + u2_temp0 = 1U; + } + + if (1U == u2_temp0) + { + u2_temp0 = MOTOR_ERROR_OVER_CURRENT_SW; + } + + return u2_temp0; +} /* End of function rm_motor_check_over_current_error */ + +/*********************************************************************************************************************** + * Function Name : rm_motor_sensorless_error_check + * Description : Checks the errors + * Arguments : p_ctrl - Pointer to Sensorless Motor Control Structure + * : f_iu - U Phase current + * : f_iv - V Phase current + * : f_iw - W Phase current + * : f_vdc - Main Line Voltage + * : f_speed - Motor Rotational Speed [rpm] + * Return Value : Detected error information + **********************************************************************************************************************/ +static uint16_t rm_motor_sensorless_error_check (motor_sensorless_instance_ctrl_t * p_ctrl, + float f_iu, + float f_iv, + float f_iw, + float f_vdc, + float f_speed) +{ + uint16_t u2_error_flags = 0U; + motor_sensorless_extended_cfg_t * p_extended_cfg = (motor_sensorless_extended_cfg_t *) p_ctrl->p_cfg->p_extend; + + /*==================================*/ + /* Over current error check */ + /*==================================*/ + u2_error_flags |= rm_motor_check_over_current_error(f_iu, f_iv, f_iw, p_extended_cfg->f_overcurrent_limit); + + /*==================================*/ + /* Over voltage error check */ + /*==================================*/ + u2_error_flags |= rm_motor_check_over_voltage_error(f_vdc, p_extended_cfg->f_overvoltage_limit); + + /*==================================*/ + /* Low voltage error check */ + /*==================================*/ + u2_error_flags |= rm_motor_check_low_voltage_error(f_vdc, p_extended_cfg->f_lowvoltage_limit); + + /*================================*/ + /* Over speed error check */ + /*================================*/ + u2_error_flags |= rm_motor_check_over_speed_error(f_speed, p_extended_cfg->f_overspeed_limit); + + return u2_error_flags; +} /* End of function rm_motor_sensorless_error_check */ + +/*********************************************************************************************************************** + * Function Name : rm_motor_sensorless_init_speed_input + * Description : Initialize speed control input data(structure) + * Arguments : st_input - Pointer to the structure of Speed Control input + * Return Value : None + **********************************************************************************************************************/ +static void rm_motor_sensorless_init_speed_input (motor_speed_input_t * st_input) +{ + st_input->f_id = 0.0F; + st_input->f_iq = 0.0F; + st_input->f_vamax = 0.0F; + st_input->f_speed_rad = 0.0F; + st_input->f_ed = 0.0F; + st_input->f_eq = 0.0F; + st_input->f_phase_err_rad = 0.0F; + + st_input->u1_flag_get_iref = MOTOR_SENSORLESS_FLG_CLR; +} /* End of function rm_motor_sensorless_init_speed_input() */ + +/*********************************************************************************************************************** + * Function Name : rm_motor_sensorless_init_speed_output + * Description : Initialize speed control output data(structure) + * Arguments : st_output - Pointer to the structure of Speed Control output + * Return Value : None + **********************************************************************************************************************/ +static void rm_motor_sensorless_init_speed_output (motor_speed_output_t * st_output) +{ + st_output->f_id_ref = 0.0F; + st_output->f_iq_ref = 0.0F; + st_output->f_ref_speed_rad_ctrl = 0.0F; + st_output->f_damp_comp_speed = 0.0F; + + st_output->u1_flag_pi = MOTOR_SENSORLESS_FLG_CLR; +} /* End of function rm_motor_sensorless_init_speed_output() */ + +/*********************************************************************************************************************** + * Function Name : rm_motor_sensorless_copy_speed_current + * Description : Copy speed output data to current input data + * Arguments : st_output - Pointer to the structure of Speed Control output + * st_input - Pointer to the structure of Current Control input + * Return Value : None + **********************************************************************************************************************/ +static void rm_motor_sensorless_copy_speed_current (motor_speed_output_t * st_output, motor_current_input_t * st_input) +{ + st_input->f_id_ref = st_output->f_id_ref; + st_input->f_iq_ref = st_output->f_iq_ref; + st_input->f_ref_speed_rad_ctrl = st_output->f_ref_speed_rad_ctrl; + st_input->f_damp_comp_speed = st_output->f_damp_comp_speed; + st_input->u1_flag_pi = st_output->u1_flag_pi; +} /* End of function rm_motor_sensorless_copy_speed_current() */ + +/*********************************************************************************************************************** + * Function Name : rm_motor_sensorless_copy_current_speed + * Description : Copy current output data to speed input data + * Arguments : st_output - Pointer to the structure of Current Control output + * st_input - Pointer to the structure of Speed Control input + * Return Value : None + **********************************************************************************************************************/ +static void rm_motor_sensorless_copy_current_speed (motor_current_output_t * st_output, motor_speed_input_t * st_input) +{ + st_input->f_id = st_output->f_id; + st_input->f_iq = st_output->f_iq; + st_input->f_vamax = st_output->f_vamax; + st_input->f_speed_rad = st_output->f_speed_rad; + st_input->f_ed = st_output->f_ed; + st_input->f_eq = st_output->f_eq; + st_input->f_phase_err_rad = st_output->f_phase_err_rad; + st_input->u1_flag_get_iref = st_output->u1_flag_get_iref; +} /* End of function rm_motor_sensorless_copy_current_speed() */ + +/***** Callback Function *****/ + +/*********************************************************************************************************************** + * Function Name : rm_motor_sensorless_current_callback + * Description : Set speed control output data(structure) into current control + * Arguments : p_args - current control interface argument + * Return Value : None + **********************************************************************************************************************/ +void rm_motor_sensorless_current_callback (motor_current_callback_args_t * p_args) +{ + motor_instance_t * p_instance = (motor_instance_t *) p_args->p_context; + motor_sensorless_instance_ctrl_t * p_ctrl = (motor_sensorless_instance_ctrl_t *) p_instance->p_ctrl; + motor_sensorless_extended_cfg_t const * p_extend = p_ctrl->p_cfg->p_extend; + motor_sensorless_callback_args_t temp_args_t; + + switch (p_args->event) + { + case MOTOR_CURRENT_EVENT_FORWARD: + { + /* Invoke the callback function if it is set. */ + if (NULL != p_extend->p_callback) + { + temp_args_t.event = MOTOR_SENSORLESS_CALLBACK_EVENT_CURRENT_FORWARD; + temp_args_t.p_context = p_extend->p_context; + (p_extend->p_callback)(&temp_args_t); + } + + break; + } + + case MOTOR_CURRENT_EVENT_DATA_SET: + { + rm_motor_sensorless_copy_speed_current(&(p_ctrl->st_speed_output), &(p_ctrl->st_current_input)); + p_ctrl->p_cfg->p_motor_current_instance->p_api->parameterSet( + p_ctrl->p_cfg->p_motor_current_instance->p_ctrl, + &(p_ctrl->st_current_input)); + break; + } + + case MOTOR_CURRENT_EVENT_BACKWARD: + { + /* Invoke the callback function if it is set. */ + if (NULL != p_extend->p_callback) + { + temp_args_t.event = MOTOR_SENSORLESS_CALLBACK_EVENT_CURRENT_BACKWARD; + temp_args_t.p_context = p_extend->p_context; + (p_extend->p_callback)(&temp_args_t); + } + + break; + } + + default: + { + break; + } + } +} /* End of function rm_motor_sensorless_current_callback() */ + +/*********************************************************************************************************************** + * Function Name : rm_motor_sensorless_speed_callback + * Description : Speed Control Callback function (Perform at timer cyclic interrupt) + * Arguments : p_args - speed control interface argument + * Return Value : None + **********************************************************************************************************************/ +void rm_motor_sensorless_speed_callback (motor_speed_callback_args_t * p_args) +{ + motor_instance_t * p_instance = (motor_instance_t *) p_args->p_context; + motor_sensorless_instance_ctrl_t * p_ctrl = (motor_sensorless_instance_ctrl_t *) p_instance->p_ctrl; + motor_sensorless_extended_cfg_t const * p_extend = p_ctrl->p_cfg->p_extend; + motor_sensorless_callback_args_t temp_args_t; + + switch (p_args->event) + { + case MOTOR_SPEED_EVENT_FORWARD: + { + /* Get speed control input data from current control */ + p_ctrl->p_cfg->p_motor_current_instance->p_api->parameterGet( + p_ctrl->p_cfg->p_motor_current_instance->p_ctrl, + &(p_ctrl->st_current_output)); + rm_motor_sensorless_copy_current_speed(&(p_ctrl->st_current_output), &(p_ctrl->st_speed_input)); + + /* Invoke the callback function if it is set. */ + if (NULL != p_extend->p_callback) + { + temp_args_t.event = MOTOR_SENSORLESS_CALLBACK_EVENT_SPEED_FORWARD; + temp_args_t.p_context = p_extend->p_context; + (p_extend->p_callback)(&temp_args_t); + } + + break; + } + + case MOTOR_SPEED_EVENT_BACKWARD: + { + /* Invoke the callback function if it is set. */ + if (NULL != p_extend->p_callback) + { + temp_args_t.event = MOTOR_SENSORLESS_CALLBACK_EVENT_SPEED_BACKWARD; + temp_args_t.p_context = p_extend->p_context; + (p_extend->p_callback)(&temp_args_t); + } + + break; + } + + default: + { + break; + } + } +} /* End of function rm_motor_sensorless_speed_callback() */ diff --git a/ra/fsp/src/rm_motor_speed/rm_motor_speed.c b/ra/fsp/src/rm_motor_speed/rm_motor_speed.c new file mode 100644 index 000000000..b3953380f --- /dev/null +++ b/ra/fsp/src/rm_motor_speed/rm_motor_speed.c @@ -0,0 +1,1006 @@ +/*********************************************************************************************************************** + * Copyright [2020] Renesas Electronics Corporation and/or its affiliates. All Rights Reserved. + * + * This software and documentation are supplied by Renesas Electronics America Inc. and may only be used with products + * of Renesas Electronics Corp. and its affiliates ("Renesas"). No other uses are authorized. Renesas products are + * sold pursuant to Renesas terms and conditions of sale. Purchasers are solely responsible for the selection and use + * of Renesas products and Renesas assumes no liability. No license, express or implied, to any intellectual property + * right is granted by Renesas. This software is protected under all applicable laws, including copyright laws. Renesas + * reserves the right to change or discontinue this software and/or this documentation. THE SOFTWARE AND DOCUMENTATION + * IS DELIVERED TO YOU "AS IS," AND RENESAS MAKES NO REPRESENTATIONS OR WARRANTIES, AND TO THE FULLEST EXTENT + * PERMISSIBLE UNDER APPLICABLE LAW, DISCLAIMS ALL WARRANTIES, WHETHER EXPLICITLY OR IMPLICITLY, INCLUDING WARRANTIES + * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NONINFRINGEMENT, WITH RESPECT TO THE SOFTWARE OR + * DOCUMENTATION. RENESAS SHALL HAVE NO LIABILITY ARISING OUT OF ANY SECURITY VULNERABILITY OR BREACH. TO THE MAXIMUM + * EXTENT PERMITTED BY LAW, IN NO EVENT WILL RENESAS BE LIABLE TO YOU IN CONNECTION WITH THE SOFTWARE OR DOCUMENTATION + * (OR ANY PERSON OR ENTITY CLAIMING RIGHTS DERIVED FROM YOU) FOR ANY LOSS, DAMAGES, OR CLAIMS WHATSOEVER, INCLUDING, + * WITHOUT LIMITATION, ANY DIRECT, CONSEQUENTIAL, SPECIAL, INDIRECT, PUNITIVE, OR INCIDENTAL DAMAGES; ANY LOST PROFITS, + * OTHER ECONOMIC DAMAGE, PROPERTY DAMAGE, OR PERSONAL INJURY; AND EVEN IF RENESAS HAS BEEN ADVISED OF THE POSSIBILITY + * OF SUCH LOSS, DAMAGES, CLAIMS OR COSTS. + **********************************************************************************************************************/ + +/*********************************************************************************************************************** + * Includes + **********************************************************************************************************************/ +#include +#include +#include "rm_motor_speed.h" +#include "rm_motor_speed_library.h" +#include "bsp_api.h" +#include "bsp_cfg.h" + +/*********************************************************************************************************************** + * Macro definitions + **********************************************************************************************************************/ + +#define MOTOR_SPEED_OPEN (0X4D535043L) + +#define MOTOR_SPEED_FLG_CLR (0) /* For flag clear */ +#define MOTOR_SPEED_FLG_SET (1) /* For flag set */ + +#define MOTOR_SPEED_MULTIPLE_2 (2.0F) +#define MOTOR_SPEED_TWOPI (3.14159265358979F * 2.0F) +#define MOTOR_SPEED_TWOPI_60 (MOTOR_SPEED_TWOPI / 60.0F) /* To translate rpm => rad/s */ +#define MOTOR_SPEED_DIV_8BIT (1.0F / 256.0F) +#define MOTOR_SPEED_RAD_TRANS (3.14159265359F / 180.0F) +#define MOTOR_SPEED_ROOT3 (1.7320508F) + +/* Speed reference status */ +#define MOTOR_SPEED_SPEED_ZERO_CONST (0) +#define MOTOR_SPEED_SPEED_CHANGE (2) + +/* Id reference status */ +#define MOTOR_SPEED_ID_UP (0) +#define MOTOR_SPEED_ID_CONST (1) +#define MOTOR_SPEED_ID_DOWN (2) +#define MOTOR_SPEED_ID_ZERO_CONST (3) +#define MOTOR_SPEED_ID_FLUXWKN (4) + +/* Iq reference status */ +#define MOTOR_SPEED_IQ_ZERO_CONST (0) +#define MOTOR_SPEED_IQ_SPEED_PI_OUTPUT (1) +#define MOTOR_SPEED_IQ_AUTO_ADJ (2) +#define MOTOR_SPEED_IQ_DOWN (3) + +#ifndef MOTOR_SPEED_ERROR_RETURN + + #define MOTOR_SPEED_ERROR_RETURN(a, err) FSP_ERROR_RETURN((a), (err)) +#endif + +/*********************************************************************************************************************** + * Typedef definitions + **********************************************************************************************************************/ + +/*********************************************************************************************************************** + * Private function prototypes + **********************************************************************************************************************/ +void rm_motor_speed_cyclic(timer_callback_args_t * p_args); + +static void rm_motor_speed_set_param_ref_speed(motor_speed_instance_ctrl_t * p_ctrl, float f_ref_speed_rpm); +static float rm_motor_speed_speed_rate_limit(motor_speed_instance_ctrl_t * p_ctrl); +static float rm_motor_speed_set_iq_ref(motor_speed_instance_ctrl_t * p_ctrl); +static float rm_motor_speed_set_id_ref(motor_speed_instance_ctrl_t * p_ctrl); +static float rm_motor_speed_speed_pi(motor_speed_instance_ctrl_t * p_ctrl, float f_speed_rad); +static float rm_motor_speed_set_speed_ref(motor_speed_instance_ctrl_t * p_ctrl); +static float rm_motor_speed_pi_control(motor_speed_pi_params_t * pi_ctrl); +static void rm_motor_speed_first_order_lpf_init(motor_speed_lpf_t * st_lpf); +static void rm_motor_speed_first_order_lpf_gain_calc(motor_speed_lpf_t * st_lpf, float f_omega, float f_ctrl_period); + +/*********************************************************************************************************************** + * Private global variables + **********************************************************************************************************************/ + +/* Version data structure. */ +static const fsp_version_t g_motor_speed_version = +{ + .api_version_minor = MOTOR_SPEED_API_VERSION_MINOR, + .api_version_major = MOTOR_SPEED_API_VERSION_MAJOR, + .code_version_major = MOTOR_SPEED_CODE_VERSION_MAJOR, + .code_version_minor = MOTOR_SPEED_CODE_VERSION_MINOR +}; + +/*********************************************************************************************************************** + * Global variables + **********************************************************************************************************************/ +const motor_speed_api_t g_motor_speed_on_motor_speed = +{ + .open = RM_MOTOR_SPEED_Open, + .close = RM_MOTOR_SPEED_Close, + .reset = RM_MOTOR_SPEED_Reset, + .run = RM_MOTOR_SPEED_Run, + .speedReferenceSet = RM_MOTOR_SPEED_SpeedReferenceSet, + .parameterSet = RM_MOTOR_SPEED_ParameterSet, + .speedControl = RM_MOTOR_SPEED_SpeedControl, + .parameterGet = RM_MOTOR_SPEED_ParameterGet, + .parameterUpdate = RM_MOTOR_SPEED_ParameterUpdate, + .versionGet = RM_MOTOR_SPEED_VersionGet +}; + +/*******************************************************************************************************************//** + * @addtogroup MOTOR_SPEED + * @{ + **********************************************************************************************************************/ + +/*********************************************************************************************************************** + * Functions + **********************************************************************************************************************/ + +/*******************************************************************************************************************//** + * @brief Opens and configures the Motor Speed Module. Implements @ref motor_speed_api_t::open. + * + * @retval FSP_SUCCESS Motor Speed Module 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_MOTOR_SPEED_Open (motor_speed_ctrl_t * const p_ctrl, motor_speed_cfg_t const * const p_cfg) +{ + motor_speed_instance_ctrl_t * p_instance_ctrl = (motor_speed_instance_ctrl_t *) p_ctrl; + +#if MOTOR_SPEED_CFG_PARAM_CHECKING_ENABLE + FSP_ASSERT(NULL != p_instance_ctrl); + FSP_ASSERT(NULL != p_cfg); +#endif + motor_speed_extended_cfg_t * p_extended_cfg = (motor_speed_extended_cfg_t *) p_cfg->p_extend; + +#if MOTOR_SPEED_CFG_PARAM_CHECKING_ENABLE + FSP_ASSERT(NULL != p_extended_cfg); + + FSP_ERROR_RETURN(MOTOR_SPEED_OPEN != p_instance_ctrl->open, FSP_ERR_ALREADY_OPEN); + + FSP_ERROR_RETURN(0.0F <= p_extended_cfg->f_speed_ctrl_period, FSP_ERR_INVALID_ARGUMENT); + FSP_ERROR_RETURN(0.0F <= p_extended_cfg->f_limit_speed_change, FSP_ERR_INVALID_ARGUMENT); + FSP_ERROR_RETURN(0.0F <= p_extended_cfg->f_max_speed_rad, FSP_ERR_INVALID_ARGUMENT); +#endif + + p_instance_ctrl->p_cfg = p_cfg; + + p_instance_ctrl->u1_active = MOTOR_SPEED_FLG_CLR; + p_instance_ctrl->u1_state_speed_ref = MOTOR_SPEED_SPEED_ZERO_CONST; + p_instance_ctrl->st_input.u1_flag_get_iref = MOTOR_SPEED_FLG_CLR; + p_instance_ctrl->u1_state_id_ref = MOTOR_SPEED_ID_UP; + p_instance_ctrl->u1_state_iq_ref = MOTOR_SPEED_IQ_ZERO_CONST; + + p_instance_ctrl->f_rpm2rad = (float) (p_extended_cfg->mtr_param.u2_mtr_pp) * MOTOR_SPEED_TWOPI_60; + + p_instance_ctrl->f_ref_speed_rad_ctrl = 0.0F; + p_instance_ctrl->st_input.f_speed_rad = 0.0F; + p_instance_ctrl->f_speed_lpf_rad = 0.0F; + p_instance_ctrl->u1_flag_down_to_ol = MOTOR_SPEED_FLG_CLR; + + p_instance_ctrl->pi_param.f_ilimit = p_extended_cfg->f_iq_limit * MOTOR_SPEED_ROOT3; + rm_motor_speed_pi_gain_calc(&(p_extended_cfg->mtr_param), + &(p_extended_cfg->d_param), + &(p_instance_ctrl->pi_param), + p_extended_cfg->f_speed_ctrl_period); + + rm_motor_speed_first_order_lpf_init(&(p_instance_ctrl->st_phase_err_lpf)); + rm_motor_speed_first_order_lpf_gain_calc(&(p_instance_ctrl->st_phase_err_lpf), + p_extended_cfg->d_param.f_phase_err_lpf_cut_freq, + p_extended_cfg->f_speed_ctrl_period); + + rm_motor_speed_first_order_lpf_init(&(p_instance_ctrl->st_speed_lpf)); + rm_motor_speed_first_order_lpf_gain_calc(&(p_instance_ctrl->st_speed_lpf), + p_extended_cfg->f_omega_t, + p_extended_cfg->f_speed_ctrl_period); + + rm_motor_speed_fluxwkn_init(&(p_instance_ctrl->st_flxwkn), + p_extended_cfg->f_iq_limit * MOTOR_SPEED_ROOT3, + 1.0F, + &(p_extended_cfg->mtr_param)); + p_instance_ctrl->u1_enable_flux_weakning = MOTOR_SPEED_FLG_CLR; + + rm_motor_speed_opl_damp_init(&(p_extended_cfg->ol_sub_param), p_extended_cfg->f_ol_fb_speed_limit_rate); + rm_motor_speed_opl_damp_r_set_gain(&(p_extended_cfg->ol_sub_param), + p_extended_cfg->mtr_param.u2_mtr_pp, + p_extended_cfg->mtr_param.f4_mtr_m, + p_extended_cfg->mtr_param.f4_mtr_j, + p_extended_cfg->d_param.f_ol_damping_zeta, + p_extended_cfg->d_param.f_ed_hpf_omega, + p_extended_cfg->ol_param.f4_ol_id_ref, + p_extended_cfg->ol_param.f4_id_down_speed_rad * MOTOR_SPEED_TWOPI_60, + p_extended_cfg->f_speed_ctrl_period); + + /* Set used timer instance */ + if (p_cfg->p_timer_instance != NULL) + { + p_cfg->p_timer_instance->p_api->open(p_cfg->p_timer_instance->p_ctrl, p_cfg->p_timer_instance->p_cfg); + p_cfg->p_timer_instance->p_api->start(p_cfg->p_timer_instance->p_ctrl); + } + + /* Mark driver as open */ + p_instance_ctrl->open = MOTOR_SPEED_OPEN; + + return FSP_SUCCESS; +} + +/*******************************************************************************************************************//** + * @brief Disables specified Motor Speed Module. Implements @ref motor_speed_api_t::close. + * + * @retval FSP_SUCCESS Successfully closed. + * @retval FSP_ERR_ASSERTION Null pointer. + * @retval FSP_ERR_NOT_OPEN Module is not open. + **********************************************************************************************************************/ +fsp_err_t RM_MOTOR_SPEED_Close (motor_speed_ctrl_t * const p_ctrl) +{ + motor_speed_instance_ctrl_t * p_instance_ctrl = (motor_speed_instance_ctrl_t *) p_ctrl; + +#if MOTOR_SPEED_CFG_PARAM_CHECKING_ENABLE + FSP_ASSERT(p_instance_ctrl); + MOTOR_SPEED_ERROR_RETURN(MOTOR_SPEED_OPEN == p_instance_ctrl->open, FSP_ERR_NOT_OPEN); +#endif + + motor_speed_cfg_t * p_cfg = (motor_speed_cfg_t *) p_instance_ctrl->p_cfg; + + p_instance_ctrl->u1_active = MOTOR_SPEED_FLG_CLR; + + /* Close used timer instance */ + if (p_cfg->p_timer_instance != NULL) + { + p_cfg->p_timer_instance->p_api->close(p_cfg->p_timer_instance->p_ctrl); + } + + p_instance_ctrl->open = 0U; + + return FSP_SUCCESS; +} + +/*******************************************************************************************************************//** + * @brief Reset the variables of Motor Speed Module. Implements @ref motor_speed_api_t::reset + * + * @retval FSP_SUCCESS Successfully reset. + * @retval FSP_ERR_ASSERTION Null pointer. + * @retval FSP_ERR_NOT_OPEN Module is not open. + **********************************************************************************************************************/ +fsp_err_t RM_MOTOR_SPEED_Reset (motor_speed_ctrl_t * const p_ctrl) +{ + motor_speed_instance_ctrl_t * p_instance_ctrl = (motor_speed_instance_ctrl_t *) p_ctrl; + +#if MOTOR_SPEED_CFG_PARAM_CHECKING_ENABLE + FSP_ASSERT(p_instance_ctrl); + MOTOR_SPEED_ERROR_RETURN(MOTOR_SPEED_OPEN == p_instance_ctrl->open, FSP_ERR_NOT_OPEN); +#endif + + motor_speed_extended_cfg_t * p_extended_cfg = + (motor_speed_extended_cfg_t *) p_instance_ctrl->p_cfg->p_extend; + + p_instance_ctrl->u1_active = MOTOR_SPEED_FLG_CLR; + p_instance_ctrl->u1_state_speed_ref = MOTOR_SPEED_SPEED_ZERO_CONST; + p_instance_ctrl->st_input.u1_flag_get_iref = MOTOR_SPEED_FLG_CLR; + p_instance_ctrl->u1_state_id_ref = MOTOR_SPEED_ID_UP; + p_instance_ctrl->u1_state_iq_ref = MOTOR_SPEED_IQ_ZERO_CONST; + + p_instance_ctrl->f_ref_speed_rad_ctrl = 0.0F; + p_instance_ctrl->st_input.f_speed_rad = 0.0F; + p_instance_ctrl->f_speed_lpf_rad = 0.0F; + + p_instance_ctrl->st_phase_err_lpf.f_pre_output = 0.0F; + p_instance_ctrl->st_phase_err_lpf.f_pre_input = 0.0F; + + p_instance_ctrl->st_speed_lpf.f_pre_output = 0.0F; + p_instance_ctrl->st_speed_lpf.f_pre_input = 0.0F; + + p_instance_ctrl->u1_enable_flux_weakning = MOTOR_SPEED_FLG_CLR; + rm_motor_speed_fluxwkn_reset(&(p_instance_ctrl->st_flxwkn)); + + rm_motor_speed_opl_damp_reset(&(p_extended_cfg->ol_sub_param)); + + return FSP_SUCCESS; +} + +/*******************************************************************************************************************//** + * @brief Run(Start) the Motor Speed Control. Implements @ref motor_speed_api_t::run + * + * @retval FSP_SUCCESS Successfully start. + * @retval FSP_ERR_ASSERTION Null pointer. + * @retval FSP_ERR_NOT_OPEN Module is not open. + **********************************************************************************************************************/ +fsp_err_t RM_MOTOR_SPEED_Run (motor_speed_ctrl_t * const p_ctrl) +{ + motor_speed_instance_ctrl_t * p_instance_ctrl = (motor_speed_instance_ctrl_t *) p_ctrl; + +#if MOTOR_SPEED_CFG_PARAM_CHECKING_ENABLE + FSP_ASSERT(p_instance_ctrl); + MOTOR_SPEED_ERROR_RETURN(MOTOR_SPEED_OPEN == p_instance_ctrl->open, FSP_ERR_NOT_OPEN); +#endif + + p_instance_ctrl->u1_active = MOTOR_SPEED_FLG_SET; + + return FSP_SUCCESS; +} + +/*******************************************************************************************************************//** + * @brief Set Speed Reference Data. Implements @ref motor_speed_api_t::speedReferenceSet + * + * @retval FSP_SUCCESS Successfully data is set. + * @retval FSP_ERR_ASSERTION Null pointer. + * @retval FSP_ERR_NOT_OPEN Module is not open. + **********************************************************************************************************************/ +fsp_err_t RM_MOTOR_SPEED_SpeedReferenceSet (motor_speed_ctrl_t * const p_ctrl, float const speed_reference_rpm) +{ + motor_speed_instance_ctrl_t * p_instance_ctrl = (motor_speed_instance_ctrl_t *) p_ctrl; + +#if MOTOR_SPEED_CFG_PARAM_CHECKING_ENABLE + FSP_ASSERT(p_instance_ctrl); + MOTOR_SPEED_ERROR_RETURN(MOTOR_SPEED_OPEN == p_instance_ctrl->open, FSP_ERR_NOT_OPEN); +#endif + + rm_motor_speed_set_param_ref_speed(p_instance_ctrl, speed_reference_rpm); + + return FSP_SUCCESS; +} + +/*******************************************************************************************************************//** + * @brief Set Input parameters. Implements @ref motor_speed_api_t::parameterSet + * + * @retval FSP_SUCCESS Successfully data is set. + * @retval FSP_ERR_ASSERTION Null pointer. + * @retval FSP_ERR_NOT_OPEN Module is not open. + * @retval FSP_ERR_INVALID_ARGUMENT Output pointer is NULL. + **********************************************************************************************************************/ +fsp_err_t RM_MOTOR_SPEED_ParameterSet (motor_speed_ctrl_t * const p_ctrl, motor_speed_input_t const * const p_st_input) + +{ + motor_speed_instance_ctrl_t * p_instance_ctrl = (motor_speed_instance_ctrl_t *) p_ctrl; + +#if MOTOR_SPEED_CFG_PARAM_CHECKING_ENABLE + FSP_ASSERT(p_instance_ctrl); + MOTOR_SPEED_ERROR_RETURN(MOTOR_SPEED_OPEN == p_instance_ctrl->open, FSP_ERR_NOT_OPEN); + MOTOR_SPEED_ERROR_RETURN(NULL != p_st_input, FSP_ERR_INVALID_ARGUMENT); +#endif + + p_instance_ctrl->st_input = *p_st_input; + + return FSP_SUCCESS; +} + +/*******************************************************************************************************************//** + * @brief Calculates the d/q-axis current reference.(Main process of Speed Control) Implements @ref motor_speed_api_t::speedControl + * + * @retval FSP_SUCCESS Successful data calculation. + * @retval FSP_ERR_ASSERTION Null pointer. + * @retval FSP_ERR_NOT_OPEN Module is not open. + **********************************************************************************************************************/ +fsp_err_t RM_MOTOR_SPEED_SpeedControl (motor_speed_ctrl_t * const p_ctrl) +{ + float f4_temp0 = 0.0F; + float f4_idq_ref[2] = {0.0F}; + motor_speed_instance_ctrl_t * p_instance_ctrl = (motor_speed_instance_ctrl_t *) p_ctrl; + +#if MOTOR_SPEED_CFG_PARAM_CHECKING_ENABLE + FSP_ASSERT(p_instance_ctrl); + MOTOR_SPEED_ERROR_RETURN(MOTOR_SPEED_OPEN == p_instance_ctrl->open, FSP_ERR_NOT_OPEN); +#endif + + motor_speed_extended_cfg_t * p_extended_cfg = + (motor_speed_extended_cfg_t *) p_instance_ctrl->p_cfg->p_extend; + + /***** Sensor-less to open-loop *****/ + /* Filter for phase error */ + p_instance_ctrl->f_phase_err_rad_lpf = + rm_motor_speed_first_order_lpf(&(p_instance_ctrl->st_phase_err_lpf), p_instance_ctrl->st_input.f_phase_err_rad); + + /* LPF for speed */ + p_instance_ctrl->f_speed_lpf_rad = + rm_motor_speed_first_order_lpf(&(p_instance_ctrl->st_speed_lpf), p_instance_ctrl->st_input.f_speed_rad); + + if (MOTOR_SPEED_IQ_SPEED_PI_OUTPUT == p_instance_ctrl->u1_state_iq_ref) + { + /* f4_temp0 : The absolute value of speed command [rad/s] */ + f4_temp0 = fabsf(p_instance_ctrl->f_speed_lpf_rad); + if (f4_temp0 < (p_extended_cfg->f_id_up_speed_rad) * MOTOR_SPEED_TWOPI_60) + { + p_instance_ctrl->u1_flag_down_to_ol = MOTOR_SPEED_FLG_SET; + p_instance_ctrl->f_ref_speed_rad_ctrl = p_instance_ctrl->f_speed_lpf_rad; + } + } + + /***** Id, Iq, speed reference setting *****/ + if (MOTOR_SPEED_FLG_SET == p_instance_ctrl->st_input.u1_flag_get_iref) + { + p_instance_ctrl->f_ref_speed_rad_ctrl = rm_motor_speed_set_speed_ref(p_instance_ctrl); + f4_idq_ref[1] = rm_motor_speed_set_iq_ref(p_instance_ctrl); + f4_idq_ref[0] = rm_motor_speed_set_id_ref(p_instance_ctrl); + } + + p_instance_ctrl->u1_flag_down_to_ol = MOTOR_SPEED_FLG_CLR; + + if (MOTOR_SPEED_FLUX_WEAKEN_ENABLE == p_extended_cfg->u1_flux_weakening) + { + /* This function will over-write the dq-axis current command */ + if (MOTOR_SPEED_FLG_SET == p_instance_ctrl->u1_enable_flux_weakning) + { + rm_motor_speed_fluxwkn_set_vamax(&(p_instance_ctrl->st_flxwkn), p_instance_ctrl->st_input.f_vamax); + rm_motor_speed_fluxwkn_run(&(p_instance_ctrl->st_flxwkn), + p_instance_ctrl->f_speed_lpf_rad, + &(p_instance_ctrl->st_input.f_id), + &(f4_idq_ref[0])); + } + } + + p_instance_ctrl->f_id_ref = f4_idq_ref[0]; + p_instance_ctrl->f_iq_ref = f4_idq_ref[1]; + + return FSP_SUCCESS; +} + +/*******************************************************************************************************************//** + * @brief Get Speed Control Parameters. Implements @ref motor_speed_api_t::parameterGet + * + * @retval FSP_SUCCESS Successfully the flag is gotten. + * @retval FSP_ERR_ASSERTION Null pointer. + * @retval FSP_ERR_NOT_OPEN Module is not open. + * @retval FSP_ERR_INVALID_ARGUMENT Output pointer is NULL. + **********************************************************************************************************************/ +fsp_err_t RM_MOTOR_SPEED_ParameterGet (motor_speed_ctrl_t * const p_ctrl, motor_speed_output_t * const p_st_output) +{ + motor_speed_instance_ctrl_t * p_instance_ctrl = (motor_speed_instance_ctrl_t *) p_ctrl; + +#if MOTOR_SPEED_CFG_PARAM_CHECKING_ENABLE + FSP_ASSERT(p_instance_ctrl); + MOTOR_SPEED_ERROR_RETURN(MOTOR_SPEED_OPEN == p_instance_ctrl->open, FSP_ERR_NOT_OPEN); + MOTOR_SPEED_ERROR_RETURN(NULL != p_st_output, FSP_ERR_INVALID_ARGUMENT); +#endif + + p_st_output->f_id_ref = p_instance_ctrl->f_id_ref; + p_st_output->f_iq_ref = p_instance_ctrl->f_iq_ref; + p_st_output->f_ref_speed_rad_ctrl = p_instance_ctrl->f_ref_speed_rad_ctrl; + p_st_output->f_damp_comp_speed = p_instance_ctrl->f_damp_comp_speed; + + p_st_output->u1_flag_pi = MOTOR_SPEED_FLG_CLR; + if (MOTOR_SPEED_IQ_SPEED_PI_OUTPUT == p_instance_ctrl->u1_state_iq_ref) + { + p_st_output->u1_flag_pi = MOTOR_SPEED_FLG_SET; + } + + return FSP_SUCCESS; +} + +/*******************************************************************************************************************//** + * @brief Update the parameters of Speed Control Calculation. Implements @ref motor_speed_api_t::parameterUpdate + * + * @retval FSP_SUCCESS Successfully data was updated. + * @retval FSP_ERR_ASSERTION Null pointer. + * @retval FSP_ERR_NOT_OPEN Module is not open. + * @retval FSP_ERR_INVALID_ARGUMENT Configuration parameter error. + **********************************************************************************************************************/ +fsp_err_t RM_MOTOR_SPEED_ParameterUpdate (motor_speed_ctrl_t * const p_ctrl, motor_speed_cfg_t const * const p_cfg) +{ + motor_speed_instance_ctrl_t * p_instance_ctrl = (motor_speed_instance_ctrl_t *) p_ctrl; + +#if MOTOR_SPEED_CFG_PARAM_CHECKING_ENABLE + FSP_ASSERT(p_instance_ctrl); + FSP_ASSERT(p_cfg); + MOTOR_SPEED_ERROR_RETURN(MOTOR_SPEED_OPEN == p_instance_ctrl->open, FSP_ERR_NOT_OPEN); +#endif + + motor_speed_extended_cfg_t * p_extended_cfg = (motor_speed_extended_cfg_t *) p_cfg->p_extend; + +#if MOTOR_SPEED_CFG_PARAM_CHECKING_ENABLE + FSP_ERROR_RETURN(0.0F <= p_extended_cfg->f_speed_ctrl_period, FSP_ERR_INVALID_ARGUMENT); + FSP_ERROR_RETURN(0.0F <= p_extended_cfg->f_limit_speed_change, FSP_ERR_INVALID_ARGUMENT); + FSP_ERROR_RETURN(0.0F <= p_extended_cfg->f_max_speed_rad, FSP_ERR_INVALID_ARGUMENT); +#endif + + p_instance_ctrl->p_cfg = p_cfg; + + p_instance_ctrl->f_rpm2rad = (float) (p_extended_cfg->mtr_param.u2_mtr_pp) * MOTOR_SPEED_TWOPI_60; + + p_instance_ctrl->pi_param.f_ilimit = p_extended_cfg->f_iq_limit * MOTOR_SPEED_ROOT3; + rm_motor_speed_pi_gain_calc(&(p_extended_cfg->mtr_param), + &(p_extended_cfg->d_param), + &(p_instance_ctrl->pi_param), + p_extended_cfg->f_speed_ctrl_period); + + rm_motor_speed_first_order_lpf_gain_calc(&(p_instance_ctrl->st_phase_err_lpf), + p_extended_cfg->d_param.f_phase_err_lpf_cut_freq, + p_extended_cfg->f_speed_ctrl_period); + + rm_motor_speed_first_order_lpf_gain_calc(&(p_instance_ctrl->st_speed_lpf), + p_extended_cfg->f_omega_t, + p_extended_cfg->f_speed_ctrl_period); + + rm_motor_speed_fluxwkn_init(&(p_instance_ctrl->st_flxwkn), + p_extended_cfg->f_iq_limit * MOTOR_SPEED_ROOT3, + 1.0F, + &(p_extended_cfg->mtr_param)); + p_instance_ctrl->u1_enable_flux_weakning = MOTOR_SPEED_FLG_CLR; + + rm_motor_speed_opl_damp_init(&(p_extended_cfg->ol_sub_param), p_extended_cfg->f_ol_fb_speed_limit_rate); + rm_motor_speed_opl_damp_r_set_gain(&(p_extended_cfg->ol_sub_param), + p_extended_cfg->mtr_param.u2_mtr_pp, + p_extended_cfg->mtr_param.f4_mtr_m, + p_extended_cfg->mtr_param.f4_mtr_j, + p_extended_cfg->d_param.f_ol_damping_zeta, + p_extended_cfg->d_param.f_ed_hpf_omega, + p_extended_cfg->ol_param.f4_ol_id_ref, + p_extended_cfg->ol_param.f4_id_down_speed_rad * MOTOR_SPEED_TWOPI_60, + p_extended_cfg->f_speed_ctrl_period); + + return FSP_SUCCESS; +} + +/*******************************************************************************************************************//** + * Return Motor Speed Control module version. Implements @ref motor_speed_api_t::versionGet. + * + * @retval FSP_SUCCESS Version information successfully read. + * @retval FSP_ERR_ASSERTION Null pointer passed as a parameter + **********************************************************************************************************************/ +fsp_err_t RM_MOTOR_SPEED_VersionGet (fsp_version_t * const p_version) +{ +#if MOTOR_SPEED_CFG_PARAM_CHECKING_ENABLE + + /* Verify parameters are valid */ + FSP_ASSERT(NULL != p_version); +#endif + + p_version->version_id = g_motor_speed_version.version_id; + + return FSP_SUCCESS; +} + +/*******************************************************************************************************************//** + * @} (end addtogroup MOTOR_SPEED) + **********************************************************************************************************************/ + +/*********************************************************************************************************************** + * Private Functions + **********************************************************************************************************************/ + +/*********************************************************************************************************************** + * Function Name : rm_motor_speed_cyclic + * Description : Cyclic process of Speed Control (Call at timer interrupt) + * Arguments : p_args - The pointer to used arguments + * Return Value : None + **********************************************************************************************************************/ +void rm_motor_speed_cyclic (timer_callback_args_t * p_args) +{ + motor_speed_instance_t * p_instance = (motor_speed_instance_t *) p_args->p_context; + motor_speed_instance_ctrl_t * p_instance_ctrl = (motor_speed_instance_ctrl_t *) p_instance->p_ctrl; + motor_speed_callback_args_t temp_args_t; + + /* Invoke the callback function if it is set. */ + if (NULL != p_instance->p_cfg->p_callback) + { + temp_args_t.event = MOTOR_SPEED_EVENT_FORWARD; + temp_args_t.p_context = p_instance->p_cfg->p_context; + (p_instance->p_cfg->p_callback)(&temp_args_t); + } + + /* if automatic input data is set, perform automatic set input */ + if (NULL != p_instance->p_cfg->st_input) + { + p_instance->p_api->parameterSet(p_instance->p_ctrl, p_instance->p_cfg->st_input); + } + + /* Perform Speed Control Process */ + if (MOTOR_SPEED_FLG_SET == p_instance_ctrl->u1_active) + { + p_instance->p_api->speedControl(p_instance->p_ctrl); + } + + /* if automatic output data is set, perform automatic get output */ + if (NULL != p_instance->p_cfg->st_output) + { + p_instance->p_api->parameterGet(p_instance->p_ctrl, p_instance->p_cfg->st_output); + } + + /* Invoke the callback function if it is set. */ + if (NULL != p_instance->p_cfg->p_callback) + { + temp_args_t.event = MOTOR_SPEED_EVENT_BACKWARD; + temp_args_t.p_context = p_instance->p_cfg->p_context; + (p_instance->p_cfg->p_callback)(&temp_args_t); + } +} + +/*********************************************************************************************************************** + * Function Name : rm_motor_speed_set_param_ref_speed + * Description : Sets parameter for reference speed + * Arguments : p_ctrl - The pointer to speed control instance + * f_ref_speed_rpm - reference speed [rpm] + * Return Value : None + **********************************************************************************************************************/ +static void rm_motor_speed_set_param_ref_speed (motor_speed_instance_ctrl_t * p_ctrl, float f_ref_speed_rpm) +{ + /* [rpm]->[rad/s] */ + p_ctrl->f_ref_speed_rad = f_ref_speed_rpm * p_ctrl->f_rpm2rad; +} /* End of function rm_motor_speed_set_param_ref_speed */ + +/*********************************************************************************************************************** + * Function Name : rm_motor_speed_speed_rate_limit + * Description : Limits the rate of change of speed reference + * Arguments : p_ctrl - The pointer to speed control instance + * Return Value : Limited speed reference + **********************************************************************************************************************/ +static float rm_motor_speed_speed_rate_limit (motor_speed_instance_ctrl_t * p_ctrl) +{ + float f4_temp0; + float f4_temp1; + float f4_speed_ref_calc_rad; + + motor_speed_extended_cfg_t * p_extended_cfg = + (motor_speed_extended_cfg_t *) p_ctrl->p_cfg->p_extend; + + f4_temp0 = p_ctrl->f_ref_speed_rad - p_ctrl->f_ref_speed_rad_ctrl; + f4_temp1 = fminf((p_extended_cfg->f_limit_speed_change) * MOTOR_SPEED_TWOPI_60, fabsf(f4_temp0)); + f4_speed_ref_calc_rad = p_ctrl->f_ref_speed_rad_ctrl + copysignf(f4_temp1, f4_temp0); + + return f4_speed_ref_calc_rad; +} /* End of function rm_motor_speed_speed_rate_limit */ + +/*********************************************************************************************************************** + * Function Name : rm_motor_speed_speed_pi + * Description : Speed PI control + * Arguments : p_ctrl - The pointer to speed control instance + * f4_speed_rad - The electrical speed [rad/s] + * Return Value : The Iq reference + **********************************************************************************************************************/ +static float rm_motor_speed_speed_pi (motor_speed_instance_ctrl_t * p_ctrl, float f_speed_rad) +{ + float f4_iq_ref_calc; + + p_ctrl->pi_param.f_err = p_ctrl->f_ref_speed_rad_ctrl - f_speed_rad; + f4_iq_ref_calc = rm_motor_speed_pi_control(&(p_ctrl->pi_param)); + + return f4_iq_ref_calc; +} /* End of function rm_motor_speed_speed_pi */ + +/*********************************************************************************************************************** + * Function Name : rm_motor_speed_set_speed_ref + * Description : Updates the speed reference + * Arguments : p_ctrl - The pointer to the FOC data instance + * Return Value : Speed reference + **********************************************************************************************************************/ +static float rm_motor_speed_set_speed_ref (motor_speed_instance_ctrl_t * p_ctrl) +{ + float f4_speed_rad_ref_buff = 0.0F; + + motor_speed_extended_cfg_t * p_extended_cfg = (motor_speed_extended_cfg_t *) p_ctrl->p_cfg->p_extend; + + switch (p_ctrl->u1_state_speed_ref) + { + case MOTOR_SPEED_SPEED_ZERO_CONST: + { + f4_speed_rad_ref_buff = 0.0F; + if (MOTOR_SPEED_ID_CONST == p_ctrl->u1_state_id_ref) + { + p_ctrl->u1_state_speed_ref = MOTOR_SPEED_SPEED_CHANGE; + } + + break; + } + + case MOTOR_SPEED_SPEED_CHANGE: + { + f4_speed_rad_ref_buff = rm_motor_speed_speed_rate_limit(p_ctrl); + break; + } + + default: + { + /* Do noting */ + break; + } + } + + /* Speed reference limit */ + f4_speed_rad_ref_buff = rm_motor_speed_limitfabs(f4_speed_rad_ref_buff, + (p_extended_cfg->f_max_speed_rad) * MOTOR_SPEED_TWOPI_60); + + /* Return speed reference */ + return f4_speed_rad_ref_buff; +} /* End of function rm_motor_speed_set_speed_ref */ + +/********************************************************************************************************************** + * Set Current Reference + **********************************************************************************************************************/ + +/*********************************************************************************************************************** + * Function Name : rm_motor_speed_set_iq_ref + * Description : Updates the q-axis current reference + * Arguments : p_ctrl - The pointer to the ctrl instance + * Return Value : Iq reference + **********************************************************************************************************************/ +static float rm_motor_speed_set_iq_ref (motor_speed_instance_ctrl_t * p_ctrl) +{ + float f4_iq_ref_buff = 0.0F; + float f4_temp0 = 0.0F; + uint8_t u1_temp0 = MOTOR_SPEED_FLG_CLR; + + motor_speed_extended_cfg_t * p_extended_cfg = + (motor_speed_extended_cfg_t *) p_ctrl->p_cfg->p_extend; + + if (MOTOR_SPEED_FLG_SET == p_ctrl->u1_flag_down_to_ol) + { + p_ctrl->u1_state_iq_ref = MOTOR_SPEED_IQ_DOWN; + p_ctrl->f_ol_iq_down_step = (p_extended_cfg->ol_param.f4_ol_iq_down_step_ratio) * MOTOR_SPEED_DIV_8BIT * + p_ctrl->f_iq_ref; + } + + switch (p_ctrl->u1_state_iq_ref) + { + case MOTOR_SPEED_IQ_ZERO_CONST: + { + /* Iq command will always be zero in this state */ + f4_iq_ref_buff = 0.0F; + + f4_temp0 = fabsf(p_ctrl->f_ref_speed_rad_ctrl); + + if (MOTOR_SPEED_OPENLOOP_DAMPING_ENABLE == p_extended_cfg->u1_openloop_damping) + { + /* Open loop damping control*/ + p_ctrl->f_damp_comp_speed = + rm_motor_speed_opl_damp_ctrl(&(p_extended_cfg->ol_sub_param), + p_ctrl->st_input.f_ed, + p_ctrl->f_ref_speed_rad_ctrl); + } + + if (f4_temp0 >= (p_extended_cfg->ol_param.f4_id_down_speed_rad) * MOTOR_SPEED_TWOPI_60) + { + /* State transient to next state */ + if (MOTOR_SPEED_LESS_SWITCH_ENABLE == p_extended_cfg->u1_less_switch) + { + p_ctrl->u1_state_iq_ref = MOTOR_SPEED_IQ_AUTO_ADJ; + p_ctrl->f_opl_torque_current = + rm_motor_speed_opl2less_torque_current_calc(&p_extended_cfg->mtr_param, + p_extended_cfg->ol_param.f4_opl2less_sw_time, + p_extended_cfg->ol_param.f4_ol_id_ref, + p_ctrl->f_phase_err_rad_lpf); + } + else + { + p_ctrl->u1_state_iq_ref = MOTOR_SPEED_IQ_SPEED_PI_OUTPUT; + } + + /* Openloop Damping is active */ + if (MOTOR_SPEED_FLG_SET == p_extended_cfg->u1_openloop_damping) + { + /* Open-loop damping control reset */ + rm_motor_speed_opl_damp_reset(&(p_extended_cfg->ol_sub_param)); + p_ctrl->f_damp_comp_speed = 0.0F; + } + } + + break; + } + + case MOTOR_SPEED_IQ_AUTO_ADJ: + { + /* Iq auto adjustment */ + f4_iq_ref_buff = rm_motor_speed_opl2less_iq_calc(p_ctrl->st_input.f_ed, + p_ctrl->st_input.f_eq, + p_ctrl->f_id_ref, + p_ctrl->f_opl_torque_current, + p_ctrl->st_input.f_phase_err_rad); + + /* Limit Iq reference */ + f4_iq_ref_buff = rm_motor_speed_limitfabs(f4_iq_ref_buff, p_extended_cfg->f_iq_limit * MOTOR_SPEED_ROOT3); + + if (0.0F <= p_ctrl->f_speed_lpf_rad) + { + u1_temp0 = + (p_ctrl->st_input.f_phase_err_rad <= + (p_extended_cfg->ol_param.f4_switch_phase_err_rad) * MOTOR_SPEED_RAD_TRANS); + } + else if (0.0F > p_ctrl->f_speed_lpf_rad) + { + u1_temp0 = + (p_ctrl->st_input.f_phase_err_rad >= + -(p_extended_cfg->ol_param.f4_switch_phase_err_rad) * MOTOR_SPEED_RAD_TRANS); + } + else + { + /* Do nothing */ + } + + if (MOTOR_SPEED_FLG_SET == u1_temp0) + { + p_ctrl->u1_state_iq_ref = MOTOR_SPEED_IQ_SPEED_PI_OUTPUT; + p_ctrl->f_init_phase_err_rad = 0.0F; + + /* Preset for suppress speed hunting */ + p_ctrl->f_ref_speed_rad_ctrl = p_ctrl->f_speed_lpf_rad; + + p_ctrl->pi_param.f_refi = f4_iq_ref_buff; + } + + break; + } + + case MOTOR_SPEED_IQ_SPEED_PI_OUTPUT: + { + /* Speed PI control */ + f4_iq_ref_buff = rm_motor_speed_speed_pi(p_ctrl, p_ctrl->st_input.f_speed_rad); + + /* Iq reference limit */ + f4_iq_ref_buff = rm_motor_speed_limitfabs(f4_iq_ref_buff, p_extended_cfg->f_iq_limit * MOTOR_SPEED_ROOT3); + break; + } + + case MOTOR_SPEED_IQ_DOWN: + { + f4_iq_ref_buff = p_ctrl->f_iq_ref - p_ctrl->f_ol_iq_down_step; + f4_temp0 = p_ctrl->f_ol_iq_down_step * f4_iq_ref_buff; /* Check sign */ + if (f4_temp0 <= 0.0F) + { + f4_iq_ref_buff = 0.0F; + p_ctrl->u1_state_iq_ref = MOTOR_SPEED_IQ_ZERO_CONST; + } + + break; + } + + default: + { + /* Do noting */ + break; + } + } + + /* Return iq reference */ + return f4_iq_ref_buff; +} /* End of function rm_motor_speed_set_iq_ref */ + +/*********************************************************************************************************************** + * Function Name : rm_motor_speed_set_id_ref + * Description : Updates the d-axis current reference + * Arguments : p_ctrl - The pointer of speed control instance + * Return Value : Id reference + **********************************************************************************************************************/ +static float rm_motor_speed_set_id_ref (motor_speed_instance_ctrl_t * p_ctrl) +{ + float f4_temp0 = 0.0F; + float f4_id_ref_buff = 0.0F; + + motor_speed_extended_cfg_t * p_extended_cfg = + (motor_speed_extended_cfg_t *) p_ctrl->p_cfg->p_extend; + + if (MOTOR_SPEED_FLG_SET == p_ctrl->u1_flag_down_to_ol) + { + p_ctrl->u1_state_id_ref = MOTOR_SPEED_ID_UP; + } + + if (MOTOR_SPEED_FLUX_WEAKEN_ENABLE == p_extended_cfg->u1_flux_weakening) + { + /* Flux-weakening process should be ignored unless d-axis current command in certain state */ + p_ctrl->u1_enable_flux_weakning = MOTOR_SPEED_FLG_CLR; + } + + switch (p_ctrl->u1_state_id_ref) + { + case MOTOR_SPEED_ID_UP: + { + f4_id_ref_buff = p_ctrl->f_id_ref + p_extended_cfg->ol_param.f4_ol_id_up_step; + if (f4_id_ref_buff >= p_extended_cfg->ol_param.f4_ol_id_ref) + { + f4_id_ref_buff = p_extended_cfg->ol_param.f4_ol_id_ref; + p_ctrl->u1_state_id_ref = MOTOR_SPEED_ID_CONST; + } + + break; + } + + case MOTOR_SPEED_ID_CONST: + { + f4_id_ref_buff = p_ctrl->f_id_ref; + f4_temp0 = fabsf(p_ctrl->f_ref_speed_rad_ctrl); + if (f4_temp0 >= (p_extended_cfg->ol_param.f4_id_down_speed_rad) * MOTOR_SPEED_TWOPI_60) + { + p_ctrl->u1_state_id_ref = MOTOR_SPEED_ID_DOWN; + } + + break; + } + + case MOTOR_SPEED_ID_DOWN: + { + f4_id_ref_buff = p_ctrl->f_id_ref - p_extended_cfg->ol_param.f4_ol_id_down_step; + if (0.0F >= f4_id_ref_buff) + { + f4_id_ref_buff = 0.0F; + p_ctrl->u1_state_id_ref = MOTOR_SPEED_ID_ZERO_CONST; + } + + break; + } + + case MOTOR_SPEED_ID_ZERO_CONST: + case MOTOR_SPEED_ID_FLUXWKN: + { + f4_id_ref_buff = 0.0F; + if (MOTOR_SPEED_FLUX_WEAKEN_ENABLE == p_extended_cfg->u1_flux_weakening) + { + /* Enables the Flux-weakening when the open-loop has completely ended */ + p_ctrl->u1_enable_flux_weakning = MOTOR_SPEED_FLG_SET; + if (1 == rm_motor_speed_fluxwkn_check_bypass(&(p_ctrl->st_flxwkn))) + { + p_ctrl->u1_state_id_ref = MOTOR_SPEED_ID_FLUXWKN; + } + else + { + p_ctrl->u1_state_id_ref = MOTOR_SPEED_ID_ZERO_CONST; + } + } + + break; + } + + default: + { + /* Do nothing */ + break; + } + } + + /* Return id reference */ + return f4_id_ref_buff; +} /* End of function rm_motor_speed_set_id_ref */ + +/********************************************************************************************************************** + * Low Pass Filter for Speed + **********************************************************************************************************************/ + +/*********************************************************************************************************************** + * Function Name : rm_motor_speed_first_order_lpf_init + * Description : Initialize First Order LPF + * Arguments : st_lpf - First order LPF structure (pointer) + * Return Value : None + **********************************************************************************************************************/ +static void rm_motor_speed_first_order_lpf_init (motor_speed_lpf_t * st_lpf) +{ + st_lpf->f_pre_output = 0.0F; + st_lpf->f_pre_input = 0.0F; + st_lpf->f_omega_t = 0.0F; + st_lpf->f_gain_ka = 0.0F; + st_lpf->f_gain_kb = 0.0F; +} /* End of function rm_motor_speed_first_order_lpf_init */ + +/*********************************************************************************************************************** + * Function Name : rm_motor_speed_first_order_lpf_gain_calc + * Description : Calculate the gain of First Order LPF + * Arguments : st_lpf - First order LPF structure (pointer) + * f4_omega - Natural frequency + * f4_ctrl_period - Control period + * Return Value : None + **********************************************************************************************************************/ +static void rm_motor_speed_first_order_lpf_gain_calc (motor_speed_lpf_t * st_lpf, float f_omega, float f_ctrl_period) +{ + st_lpf->f_omega_t = (MOTOR_SPEED_TWOPI * f_omega) * f_ctrl_period; + st_lpf->f_gain_ka = (MOTOR_SPEED_MULTIPLE_2 - st_lpf->f_omega_t) / + (st_lpf->f_omega_t + MOTOR_SPEED_MULTIPLE_2); + st_lpf->f_gain_kb = st_lpf->f_omega_t / (st_lpf->f_omega_t + MOTOR_SPEED_MULTIPLE_2); +} /* End of function rm_motor_speed_first_order_lpf_gain_calc */ + +/*********************************************************************************************************************** + * Function Name : rm_motor_speed_pi_control + * Description : PI control + * Arguments : pi_ctrl - The pointer to the PI control structure + * Return Value : PI control output value + **********************************************************************************************************************/ +static float rm_motor_speed_pi_control (motor_speed_pi_params_t * pi_ctrl) +{ + float f4_err; + float f4_kp; + float f4_ki; + float f4_refp; + float f4_refi; + float f4_ref; + float f4_ilimit; + + f4_err = pi_ctrl->f_err; + f4_kp = pi_ctrl->f_kp; + f4_ki = pi_ctrl->f_ki; + f4_refi = pi_ctrl->f_refi; + f4_ilimit = pi_ctrl->f_ilimit; + + f4_refp = f4_err * f4_kp; /* Proportional part */ + f4_refi += (f4_err * f4_ki); /* Integral part */ + + /*** Integral part limit ***/ + f4_refi = rm_motor_speed_limitfabs(f4_refi, f4_ilimit); + pi_ctrl->f_refi = f4_refi; + + f4_ref = f4_refp + f4_refi; /* PI output */ + + return f4_ref; +} /* End of function rm_motor_speed_pi_control */ diff --git a/ra/fsp/src/rm_motor_speed/rm_motor_speed_library.h b/ra/fsp/src/rm_motor_speed/rm_motor_speed_library.h new file mode 100644 index 000000000..73d8fd14d --- /dev/null +++ b/ra/fsp/src/rm_motor_speed/rm_motor_speed_library.h @@ -0,0 +1,77 @@ +/*********************************************************************************************************************** + * DISCLAIMER + * This software is supplied by Renesas Electronics Corporation and is only intended for use with Renesas products. No + * other uses are authorized. This software is owned by Renesas Electronics Corporation and is protected under all + * applicable laws, including copyright laws. + * THIS SOFTWARE IS PROVIDED "AS IS" AND RENESAS MAKES NO WARRANTIES REGARDING + * THIS SOFTWARE, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. ALL SUCH WARRANTIES ARE EXPRESSLY DISCLAIMED. TO THE MAXIMUM + * EXTENT PERMITTED NOT PROHIBITED BY LAW, NEITHER RENESAS ELECTRONICS CORPORATION NOR ANY OF ITS AFFILIATED COMPANIES + * SHALL BE LIABLE FOR ANY DIRECT, INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANY REASON RELATED TO THIS + * SOFTWARE, EVEN IF RENESAS OR ITS AFFILIATES HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. + * Renesas reserves the right, without notice, to make changes to this software and to discontinue the availability of + * this software. By using this software, you agree to the additional terms and conditions found by accessing the + * following link: + * http://www.renesas.com/disclaimer + * + * Copyright (C) 2020 Renesas Electronics Corporation. All rights reserved. + ***********************************************************************************************************************/ + +/*********************************************************************************************************************** + * File Name : rm_motor_speed_library.h + * Description : Definitions for Motor Speed library functions + ***********************************************************************************************************************/ + +/********************************************************************************************************************** + * History : DD.MM.YYYY Version + * : 01.09.2020 1.00 + ***********************************************************************************************************************/ + +#ifndef RM_MOTOR_SPEED_LIBRARY_H +#define RM_MOTOR_SPEED_LIBRARY_H + +void rm_motor_speed_pi_gain_calc(motor_speed_motor_parameter_t * p_motor, + motor_speed_design_params_t * p_design_params, + motor_speed_pi_params_t * p_pi_speed, + float f4_speed_ctrl_period); + +void rm_motor_speed_fluxwkn_init(motor_speed_flux_weakening_t * p_fluxwkn, + float f4_ia_max, + float f4_va_max, + const motor_speed_motor_parameter_t * p_mtr); +void rm_motor_speed_fluxwkn_reset(motor_speed_flux_weakening_t * p_fluxwkn); +void rm_motor_speed_fluxwkn_set_motor(motor_speed_flux_weakening_t * p_fluxwkn, + const motor_speed_motor_parameter_t * p_mtr); +void rm_motor_speed_fluxwkn_set_vamax(motor_speed_flux_weakening_t * p_fluxwkn, float f4_va_max); +void rm_motor_speed_fluxwkn_set_iamax(motor_speed_flux_weakening_t * p_fluxwkn, float f4_ia_max); +uint16_t rm_motor_speed_fluxwkn_run(motor_speed_flux_weakening_t * p_fluxwkn, + float f4_speed_rad, + const float * p_f4_idq, + float * p_f4_idq_ref); +uint8_t rm_motor_speed_fluxwkn_check_bypass(motor_speed_flux_weakening_t * p_fluxwkn); + +float rm_motor_speed_opl_damp_ctrl(motor_speed_oldamp_sub_t * p_opl_damp, float f4_ed, float f4_speed_ref); +void rm_motor_speed_opl_damp_init(motor_speed_oldamp_sub_t * p_opl_damp, float f4_fb_speed_limit_rate); +void rm_motor_speed_opl_damp_reset(motor_speed_oldamp_sub_t * p_opl_damp); +void rm_motor_speed_opl_damp_r_set_gain(motor_speed_oldamp_sub_t * p_opl_damp, + uint16_t u2_pp, + float f4_ke, + float f4_j, + float f4_zeta, + float f4_ed_hpf_fc, + float f4_opl_current, + float f4_id_down_speed, + float f4_tc); +void rm_motor_speed_opl_damp_set_limit(motor_speed_oldamp_sub_t * p_opl_damp, float f4_limit_rate); + +float rm_motor_speed_opl2less_torque_current_calc(const motor_speed_motor_parameter_t * p_motor, + float f4_opl2less_sw_time, + float f4_ol_id_ref, + float f4_phase_err_rad_lpf); +float rm_motor_speed_opl2less_iq_calc(float f4_ed, float f4_eq, float f4_id, float f4_torque_current, + float f4_phase_err); + +float rm_motor_speed_limitfabs(float f4_value, float f4_limit_value); +float rm_motor_speed_first_order_lpf(motor_speed_lpf_t * p_lpf, float f_input); + +#endif /* RM_MOTOR_SPEED_LIBRARY_H */ diff --git a/ra/fsp/src/rm_psa_crypto/ctr_drbg_alt.c b/ra/fsp/src/rm_psa_crypto/ctr_drbg_alt.c index 014ed743a..73933b3a2 100644 --- a/ra/fsp/src/rm_psa_crypto/ctr_drbg_alt.c +++ b/ra/fsp/src/rm_psa_crypto/ctr_drbg_alt.c @@ -65,7 +65,7 @@ static int mbedtls_aes_crypt_ecb(mbedtls_aes_context * ctx, static void mbedtls_aes_init(mbedtls_aes_context * ctx); static int mbedtls_aes_setkey_enc(mbedtls_aes_context * ctx, const unsigned char * key, unsigned int keybits); - #if defined(__ICCARM__) /* IAR Compiler */ + #if defined(__ICCARM__) && defined(MBEDTLS_FS_IO) /* IAR Compiler */ static int ferror(FILE * f); int ferror (FILE * f) @@ -174,7 +174,7 @@ static int block_cipher_df( unsigned char *output, * (Total is padded to a multiple of 16-bytes with zeroes) */ p = buf + MBEDTLS_CTR_DRBG_BLOCKSIZE; - *p++ = ( data_len >> 24 ) & 0xff; + *p++ = (uint8_t) (( data_len >> 24 ) & 0xff); *p++ = ( data_len >> 16 ) & 0xff; *p++ = ( data_len >> 8 ) & 0xff; *p++ = ( data_len ) & 0xff; @@ -186,7 +186,7 @@ static int block_cipher_df( unsigned char *output, buf_len = MBEDTLS_CTR_DRBG_BLOCKSIZE + 8 + data_len + 1; for( i = 0; i < MBEDTLS_CTR_DRBG_KEYSIZE; i++ ) - key[i] = i; + key[i] = (uint8_t) i; if( ( ret = mbedtls_aes_setkey_enc( &aes_ctx, key, MBEDTLS_CTR_DRBG_KEYBITS ) ) != 0 ) @@ -1550,6 +1550,7 @@ static int mbedtls_aes_crypt_ecb (mbedtls_aes_context * ctx, AES_VALIDATE_RET(output != NULL); AES_VALIDATE_RET(mode == MBEDTLS_AES_ENCRYPT || mode == MBEDTLS_AES_DECRYPT); + (void) mode; // Unused in some build configurations #if defined(MBEDTLS_AESNI_C) && defined(MBEDTLS_HAVE_X86_64) if (mbedtls_aesni_has_support(MBEDTLS_AESNI_AES)) diff --git a/ra/fsp/src/rm_psa_crypto/ecdh_alt.c b/ra/fsp/src/rm_psa_crypto/ecdh_alt.c new file mode 100644 index 000000000..e467873ae --- /dev/null +++ b/ra/fsp/src/rm_psa_crypto/ecdh_alt.c @@ -0,0 +1,740 @@ +/* + * Elliptic curve Diffie-Hellman + * + * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * This file is part of mbed TLS (https://tls.mbed.org) + */ + +/* + * References: + * + * SEC1 http://www.secg.org/index.php?action=secg,docs_secg + * RFC 4492 + */ + +#if !defined(MBEDTLS_CONFIG_FILE) +#include "mbedtls/config.h" +#else +#include MBEDTLS_CONFIG_FILE +#endif + +#if defined(MBEDTLS_ECDH_C) + +#include "mbedtls/ecdh.h" +#include "mbedtls/platform_util.h" +#include "mbedtls/error.h" + +#include + +#if defined(MBEDTLS_ECDH_ALT) + +/* Parameter validation macros based on platform_util.h */ +#define ECDH_VALIDATE_RET( cond ) \ + MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_ECP_BAD_INPUT_DATA ) +#define ECDH_VALIDATE( cond ) \ + MBEDTLS_INTERNAL_VALIDATE( cond ) + +#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT) +typedef mbedtls_ecdh_context mbedtls_ecdh_context_mbed; +#endif + +static mbedtls_ecp_group_id mbedtls_ecdh_grp_id( + const mbedtls_ecdh_context *ctx ) +{ +#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT) + return( ctx->grp.id ); +#else + return( ctx->grp_id ); +#endif +} + +int mbedtls_ecdh_can_do( mbedtls_ecp_group_id gid ) +{ + /* At this time, all groups support ECDH. */ + (void) gid; + return( 1 ); +} + +#if !defined(MBEDTLS_ECDH_GEN_PUBLIC_ALT) +/* + * Generate public key (restartable version) + * + * Note: this internal function relies on its caller preserving the value of + * the output parameter 'd' across continuation calls. This would not be + * acceptable for a public function but is OK here as we control call sites. + */ +static int ecdh_gen_public_restartable( mbedtls_ecp_group *grp, + mbedtls_mpi *d, mbedtls_ecp_point *Q, + int (*f_rng)(void *, unsigned char *, size_t), + void *p_rng, + mbedtls_ecp_restart_ctx *rs_ctx ) +{ + int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; + + /* If multiplication is in progress, we already generated a privkey */ +#if defined(MBEDTLS_ECP_RESTARTABLE) + if( rs_ctx == NULL || rs_ctx->rsm == NULL ) +#endif + MBEDTLS_MPI_CHK( mbedtls_ecp_gen_privkey( grp, d, f_rng, p_rng ) ); + + MBEDTLS_MPI_CHK( mbedtls_ecp_mul_restartable( grp, Q, d, &grp->G, + f_rng, p_rng, rs_ctx ) ); + +cleanup: + return( ret ); +} + +/* + * Generate public key + */ +int mbedtls_ecdh_gen_public( mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q, + int (*f_rng)(void *, unsigned char *, size_t), + void *p_rng ) +{ + ECDH_VALIDATE_RET( grp != NULL ); + ECDH_VALIDATE_RET( d != NULL ); + ECDH_VALIDATE_RET( Q != NULL ); + ECDH_VALIDATE_RET( f_rng != NULL ); + return( ecdh_gen_public_restartable( grp, d, Q, f_rng, p_rng, NULL ) ); +} +#endif /* !MBEDTLS_ECDH_GEN_PUBLIC_ALT */ + +#if !defined(MBEDTLS_ECDH_COMPUTE_SHARED_ALT) +/* + * Compute shared secret (SEC1 3.3.1) + */ +static int ecdh_compute_shared_restartable( mbedtls_ecp_group *grp, + mbedtls_mpi *z, + const mbedtls_ecp_point *Q, const mbedtls_mpi *d, + int (*f_rng)(void *, unsigned char *, size_t), + void *p_rng, + mbedtls_ecp_restart_ctx *rs_ctx ) +{ + int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; + mbedtls_ecp_point P; + + mbedtls_ecp_point_init( &P ); + + MBEDTLS_MPI_CHK( mbedtls_ecp_mul_restartable( grp, &P, d, Q, + f_rng, p_rng, rs_ctx ) ); + + if( mbedtls_ecp_is_zero( &P ) ) + { + ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA; + goto cleanup; + } + + MBEDTLS_MPI_CHK( mbedtls_mpi_copy( z, &P.X ) ); + +cleanup: + mbedtls_ecp_point_free( &P ); + + return( ret ); +} + +/* + * Compute shared secret (SEC1 3.3.1) + */ +int mbedtls_ecdh_compute_shared( mbedtls_ecp_group *grp, mbedtls_mpi *z, + const mbedtls_ecp_point *Q, const mbedtls_mpi *d, + int (*f_rng)(void *, unsigned char *, size_t), + void *p_rng ) +{ + ECDH_VALIDATE_RET( grp != NULL ); + ECDH_VALIDATE_RET( Q != NULL ); + ECDH_VALIDATE_RET( d != NULL ); + ECDH_VALIDATE_RET( z != NULL ); + return( ecdh_compute_shared_restartable( grp, z, Q, d, + f_rng, p_rng, NULL ) ); +} +#endif /* !MBEDTLS_ECDH_COMPUTE_SHARED_ALT */ + +static void ecdh_init_internal( mbedtls_ecdh_context_mbed *ctx ) +{ + mbedtls_ecp_group_init( &ctx->grp ); + mbedtls_mpi_init( &ctx->d ); + mbedtls_ecp_point_init( &ctx->Q ); + mbedtls_ecp_point_init( &ctx->Qp ); + mbedtls_mpi_init( &ctx->z ); + +#if defined(MBEDTLS_ECP_RESTARTABLE) + mbedtls_ecp_restart_init( &ctx->rs ); +#endif +} + +/* + * Initialize context + */ +void mbedtls_ecdh_init( mbedtls_ecdh_context *ctx ) +{ + ECDH_VALIDATE( ctx != NULL ); + +#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT) + ecdh_init_internal( ctx ); + mbedtls_ecp_point_init( &ctx->Vi ); + mbedtls_ecp_point_init( &ctx->Vf ); + mbedtls_mpi_init( &ctx->_d ); +#else + memset( ctx, 0, sizeof( mbedtls_ecdh_context ) ); + + ctx->var = MBEDTLS_ECDH_VARIANT_NONE; +#endif + ctx->point_format = MBEDTLS_ECP_PF_UNCOMPRESSED; +#if defined(MBEDTLS_ECP_RESTARTABLE) + ctx->restart_enabled = 0; +#endif +} + +static int ecdh_setup_internal( mbedtls_ecdh_context_mbed *ctx, + mbedtls_ecp_group_id grp_id ) +{ + int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; + + ret = mbedtls_ecp_group_load( &ctx->grp, grp_id ); + if( ret != 0 ) + { + return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE ); + } + + return( 0 ); +} + +/* + * Setup context + */ +int mbedtls_ecdh_setup( mbedtls_ecdh_context *ctx, mbedtls_ecp_group_id grp_id ) +{ + ECDH_VALIDATE_RET( ctx != NULL ); + +#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT) + return( ecdh_setup_internal( ctx, grp_id ) ); +#else + switch( grp_id ) + { +#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED) + case MBEDTLS_ECP_DP_CURVE25519: + ctx->point_format = MBEDTLS_ECP_PF_COMPRESSED; + ctx->var = MBEDTLS_ECDH_VARIANT_EVEREST; + ctx->grp_id = grp_id; + return( mbedtls_everest_setup( &ctx->ctx.everest_ecdh, grp_id ) ); +#endif + default: + ctx->point_format = MBEDTLS_ECP_PF_UNCOMPRESSED; + ctx->var = MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0; + ctx->grp_id = grp_id; + ecdh_init_internal( &ctx->ctx.mbed_ecdh ); + return( ecdh_setup_internal( &ctx->ctx.mbed_ecdh, grp_id ) ); + } +#endif +} + +static void ecdh_free_internal( mbedtls_ecdh_context_mbed *ctx ) +{ + mbedtls_ecp_group_free( &ctx->grp ); + mbedtls_mpi_free( &ctx->d ); + mbedtls_ecp_point_free( &ctx->Q ); + mbedtls_ecp_point_free( &ctx->Qp ); + mbedtls_mpi_free( &ctx->z ); + +#if defined(MBEDTLS_ECP_RESTARTABLE) + mbedtls_ecp_restart_free( &ctx->rs ); +#endif +} + +#if defined(MBEDTLS_ECP_RESTARTABLE) +/* + * Enable restartable operations for context + */ +void mbedtls_ecdh_enable_restart( mbedtls_ecdh_context *ctx ) +{ + ECDH_VALIDATE( ctx != NULL ); + + ctx->restart_enabled = 1; +} +#endif + +/* + * Free context + */ +void mbedtls_ecdh_free( mbedtls_ecdh_context *ctx ) +{ + if( ctx == NULL ) + return; + +#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT) + mbedtls_ecp_point_free( &ctx->Vi ); + mbedtls_ecp_point_free( &ctx->Vf ); + mbedtls_mpi_free( &ctx->_d ); + ecdh_free_internal( ctx ); +#else + switch( ctx->var ) + { +#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED) + case MBEDTLS_ECDH_VARIANT_EVEREST: + mbedtls_everest_free( &ctx->ctx.everest_ecdh ); + break; +#endif + case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0: + ecdh_free_internal( &ctx->ctx.mbed_ecdh ); + break; + default: + break; + } + + ctx->point_format = MBEDTLS_ECP_PF_UNCOMPRESSED; + ctx->var = MBEDTLS_ECDH_VARIANT_NONE; + ctx->grp_id = MBEDTLS_ECP_DP_NONE; +#endif +} + +static int ecdh_make_params_internal( mbedtls_ecdh_context_mbed *ctx, + size_t *olen, int point_format, + unsigned char *buf, size_t blen, + int (*f_rng)(void *, + unsigned char *, + size_t), + void *p_rng, + int restart_enabled ) +{ + int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; + size_t grp_len, pt_len; +#if defined(MBEDTLS_ECP_RESTARTABLE) + mbedtls_ecp_restart_ctx *rs_ctx = NULL; +#endif + + if( ctx->grp.pbits == 0 ) + return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); + +#if defined(MBEDTLS_ECP_RESTARTABLE) + if( restart_enabled ) + rs_ctx = &ctx->rs; +#else + (void) restart_enabled; +#endif + + +#if defined(MBEDTLS_ECP_RESTARTABLE) + if( ( ret = ecdh_gen_public_restartable( &ctx->grp, &ctx->d, &ctx->Q, + f_rng, p_rng, rs_ctx ) ) != 0 ) + return( ret ); +#else + if( ( ret = mbedtls_ecdh_gen_public( &ctx->grp, &ctx->d, &ctx->Q, + f_rng, p_rng ) ) != 0 ) + return( ret ); +#endif /* MBEDTLS_ECP_RESTARTABLE */ + + if( ( ret = mbedtls_ecp_tls_write_group( &ctx->grp, &grp_len, buf, + blen ) ) != 0 ) + return( ret ); + + buf += grp_len; + blen -= grp_len; + + if( ( ret = mbedtls_ecp_tls_write_point( &ctx->grp, &ctx->Q, point_format, + &pt_len, buf, blen ) ) != 0 ) + return( ret ); + + *olen = grp_len + pt_len; + return( 0 ); +} + +/* + * Setup and write the ServerKeyExchange parameters (RFC 4492) + * struct { + * ECParameters curve_params; + * ECPoint public; + * } ServerECDHParams; + */ +int mbedtls_ecdh_make_params( mbedtls_ecdh_context *ctx, size_t *olen, + unsigned char *buf, size_t blen, + int (*f_rng)(void *, unsigned char *, size_t), + void *p_rng ) +{ + int restart_enabled = 0; + ECDH_VALIDATE_RET( ctx != NULL ); + ECDH_VALIDATE_RET( olen != NULL ); + ECDH_VALIDATE_RET( buf != NULL ); + ECDH_VALIDATE_RET( f_rng != NULL ); + +#if defined(MBEDTLS_ECP_RESTARTABLE) + restart_enabled = ctx->restart_enabled; +#else + (void) restart_enabled; +#endif + +#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT) + return( ecdh_make_params_internal( ctx, olen, ctx->point_format, buf, blen, + f_rng, p_rng, restart_enabled ) ); +#else + switch( ctx->var ) + { +#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED) + case MBEDTLS_ECDH_VARIANT_EVEREST: + return( mbedtls_everest_make_params( &ctx->ctx.everest_ecdh, olen, + buf, blen, f_rng, p_rng ) ); +#endif + case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0: + return( ecdh_make_params_internal( &ctx->ctx.mbed_ecdh, olen, + ctx->point_format, buf, blen, + f_rng, p_rng, + restart_enabled ) ); + default: + return MBEDTLS_ERR_ECP_BAD_INPUT_DATA; + } +#endif +} + +static int ecdh_read_params_internal( mbedtls_ecdh_context_mbed *ctx, + const unsigned char **buf, + const unsigned char *end ) +{ + return( mbedtls_ecp_tls_read_point( &ctx->grp, &ctx->Qp, buf, + end - *buf ) ); +} + +/* + * Read the ServerKeyExhange parameters (RFC 4492) + * struct { + * ECParameters curve_params; + * ECPoint public; + * } ServerECDHParams; + */ +int mbedtls_ecdh_read_params( mbedtls_ecdh_context *ctx, + const unsigned char **buf, + const unsigned char *end ) +{ + int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; + mbedtls_ecp_group_id grp_id; + ECDH_VALIDATE_RET( ctx != NULL ); + ECDH_VALIDATE_RET( buf != NULL ); + ECDH_VALIDATE_RET( *buf != NULL ); + ECDH_VALIDATE_RET( end != NULL ); + + if( ( ret = mbedtls_ecp_tls_read_group_id( &grp_id, buf, end - *buf ) ) + != 0 ) + return( ret ); + + if( ( ret = mbedtls_ecdh_setup( ctx, grp_id ) ) != 0 ) + return( ret ); + +#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT) + return( ecdh_read_params_internal( ctx, buf, end ) ); +#else + switch( ctx->var ) + { +#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED) + case MBEDTLS_ECDH_VARIANT_EVEREST: + return( mbedtls_everest_read_params( &ctx->ctx.everest_ecdh, + buf, end) ); +#endif + case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0: + return( ecdh_read_params_internal( &ctx->ctx.mbed_ecdh, + buf, end ) ); + default: + return MBEDTLS_ERR_ECP_BAD_INPUT_DATA; + } +#endif +} + +static int ecdh_get_params_internal( mbedtls_ecdh_context_mbed *ctx, + const mbedtls_ecp_keypair *key, + mbedtls_ecdh_side side ) +{ + int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; + + /* If it's not our key, just import the public part as Qp */ + if( side == MBEDTLS_ECDH_THEIRS ) + return( mbedtls_ecp_copy( &ctx->Qp, &key->Q ) ); + + /* Our key: import public (as Q) and private parts */ + if( side != MBEDTLS_ECDH_OURS ) + return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); + + ctx->grp.vendor_ctx = key->grp.vendor_ctx; + + if( ( ret = mbedtls_ecp_copy( &ctx->Q, &key->Q ) ) != 0 || + ( ret = mbedtls_mpi_copy( &ctx->d, &key->d ) ) != 0 ) + return( ret ); + + return( 0 ); +} + +/* + * Get parameters from a keypair + */ +int mbedtls_ecdh_get_params( mbedtls_ecdh_context *ctx, + const mbedtls_ecp_keypair *key, + mbedtls_ecdh_side side ) +{ + int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; + ECDH_VALIDATE_RET( ctx != NULL ); + ECDH_VALIDATE_RET( key != NULL ); + ECDH_VALIDATE_RET( side == MBEDTLS_ECDH_OURS || + side == MBEDTLS_ECDH_THEIRS ); + + if( mbedtls_ecdh_grp_id( ctx ) == MBEDTLS_ECP_DP_NONE ) + { + /* This is the first call to get_params(). Set up the context + * for use with the group. */ + if( ( ret = mbedtls_ecdh_setup( ctx, key->grp.id ) ) != 0 ) + return( ret ); + } + else + { + /* This is not the first call to get_params(). Check that the + * current key's group is the same as the context's, which was set + * from the first key's group. */ + if( mbedtls_ecdh_grp_id( ctx ) != key->grp.id ) + return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); + } + +#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT) + return( ecdh_get_params_internal( ctx, key, side ) ); +#else + switch( ctx->var ) + { +#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED) + case MBEDTLS_ECDH_VARIANT_EVEREST: + { + mbedtls_everest_ecdh_side s = side == MBEDTLS_ECDH_OURS ? + MBEDTLS_EVEREST_ECDH_OURS : + MBEDTLS_EVEREST_ECDH_THEIRS; + return( mbedtls_everest_get_params( &ctx->ctx.everest_ecdh, + key, s) ); + } +#endif + case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0: + return( ecdh_get_params_internal( &ctx->ctx.mbed_ecdh, + key, side ) ); + default: + return MBEDTLS_ERR_ECP_BAD_INPUT_DATA; + } +#endif +} + +static int ecdh_make_public_internal( mbedtls_ecdh_context_mbed *ctx, + size_t *olen, int point_format, + unsigned char *buf, size_t blen, + int (*f_rng)(void *, + unsigned char *, + size_t), + void *p_rng, + int restart_enabled ) +{ + int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; +#if defined(MBEDTLS_ECP_RESTARTABLE) + mbedtls_ecp_restart_ctx *rs_ctx = NULL; +#endif + + if( ctx->grp.pbits == 0 ) + return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); + +#if defined(MBEDTLS_ECP_RESTARTABLE) + if( restart_enabled ) + rs_ctx = &ctx->rs; +#else + (void) restart_enabled; +#endif + +#if defined(MBEDTLS_ECP_RESTARTABLE) + if( ( ret = ecdh_gen_public_restartable( &ctx->grp, &ctx->d, &ctx->Q, + f_rng, p_rng, rs_ctx ) ) != 0 ) + return( ret ); +#else + if( ( ret = mbedtls_ecdh_gen_public( &ctx->grp, &ctx->d, &ctx->Q, + f_rng, p_rng ) ) != 0 ) + return( ret ); +#endif /* MBEDTLS_ECP_RESTARTABLE */ + + return mbedtls_ecp_tls_write_point( &ctx->grp, &ctx->Q, point_format, olen, + buf, blen ); +} + +/* + * Setup and export the client public value + */ +int mbedtls_ecdh_make_public( mbedtls_ecdh_context *ctx, size_t *olen, + unsigned char *buf, size_t blen, + int (*f_rng)(void *, unsigned char *, size_t), + void *p_rng ) +{ + int restart_enabled = 0; + ECDH_VALIDATE_RET( ctx != NULL ); + ECDH_VALIDATE_RET( olen != NULL ); + ECDH_VALIDATE_RET( buf != NULL ); + ECDH_VALIDATE_RET( f_rng != NULL ); + +#if defined(MBEDTLS_ECP_RESTARTABLE) + restart_enabled = ctx->restart_enabled; +#endif + +#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT) + return( ecdh_make_public_internal( ctx, olen, ctx->point_format, buf, blen, + f_rng, p_rng, restart_enabled ) ); +#else + switch( ctx->var ) + { +#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED) + case MBEDTLS_ECDH_VARIANT_EVEREST: + return( mbedtls_everest_make_public( &ctx->ctx.everest_ecdh, olen, + buf, blen, f_rng, p_rng ) ); +#endif + case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0: + return( ecdh_make_public_internal( &ctx->ctx.mbed_ecdh, olen, + ctx->point_format, buf, blen, + f_rng, p_rng, + restart_enabled ) ); + default: + return MBEDTLS_ERR_ECP_BAD_INPUT_DATA; + } +#endif +} + +static int ecdh_read_public_internal( mbedtls_ecdh_context_mbed *ctx, + const unsigned char *buf, size_t blen ) +{ + int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; + const unsigned char *p = buf; + + if( ( ret = mbedtls_ecp_tls_read_point( &ctx->grp, &ctx->Qp, &p, + blen ) ) != 0 ) + return( ret ); + + if( (size_t)( p - buf ) != blen ) + return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); + + return( 0 ); +} + +/* + * Parse and import the client's public value + */ +int mbedtls_ecdh_read_public( mbedtls_ecdh_context *ctx, + const unsigned char *buf, size_t blen ) +{ + ECDH_VALIDATE_RET( ctx != NULL ); + ECDH_VALIDATE_RET( buf != NULL ); + +#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT) + return( ecdh_read_public_internal( ctx, buf, blen ) ); +#else + switch( ctx->var ) + { +#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED) + case MBEDTLS_ECDH_VARIANT_EVEREST: + return( mbedtls_everest_read_public( &ctx->ctx.everest_ecdh, + buf, blen ) ); +#endif + case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0: + return( ecdh_read_public_internal( &ctx->ctx.mbed_ecdh, + buf, blen ) ); + default: + return MBEDTLS_ERR_ECP_BAD_INPUT_DATA; + } +#endif +} + +static int ecdh_calc_secret_internal( mbedtls_ecdh_context_mbed *ctx, + size_t *olen, unsigned char *buf, + size_t blen, + int (*f_rng)(void *, + unsigned char *, + size_t), + void *p_rng, + int restart_enabled ) +{ + int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; +#if defined(MBEDTLS_ECP_RESTARTABLE) + mbedtls_ecp_restart_ctx *rs_ctx = NULL; +#endif + + if( ctx == NULL || ctx->grp.pbits == 0 ) + return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); + +#if defined(MBEDTLS_ECP_RESTARTABLE) + if( restart_enabled ) + rs_ctx = &ctx->rs; +#else + (void) restart_enabled; +#endif + +#if defined(MBEDTLS_ECP_RESTARTABLE) + if( ( ret = ecdh_compute_shared_restartable( &ctx->grp, &ctx->z, &ctx->Qp, + &ctx->d, f_rng, p_rng, + rs_ctx ) ) != 0 ) + { + return( ret ); + } +#else + if( ( ret = mbedtls_ecdh_compute_shared( &ctx->grp, &ctx->z, &ctx->Qp, + &ctx->d, f_rng, p_rng ) ) != 0 ) + { + return( ret ); + } +#endif /* MBEDTLS_ECP_RESTARTABLE */ + + if( mbedtls_mpi_size( &ctx->z ) > blen ) + return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); + + *olen = ctx->grp.pbits / 8 + ( ( ctx->grp.pbits % 8 ) != 0 ); + + if( mbedtls_ecp_get_type( &ctx->grp ) == MBEDTLS_ECP_TYPE_MONTGOMERY ) + return mbedtls_mpi_write_binary_le( &ctx->z, buf, *olen ); + + return mbedtls_mpi_write_binary( &ctx->z, buf, *olen ); +} + +/* + * Derive and export the shared secret + */ +int mbedtls_ecdh_calc_secret( mbedtls_ecdh_context *ctx, size_t *olen, + unsigned char *buf, size_t blen, + int (*f_rng)(void *, unsigned char *, size_t), + void *p_rng ) +{ + int restart_enabled = 0; + ECDH_VALIDATE_RET( ctx != NULL ); + ECDH_VALIDATE_RET( olen != NULL ); + ECDH_VALIDATE_RET( buf != NULL ); + +#if defined(MBEDTLS_ECP_RESTARTABLE) + restart_enabled = ctx->restart_enabled; +#endif + +#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT) + return( ecdh_calc_secret_internal( ctx, olen, buf, blen, f_rng, p_rng, + restart_enabled ) ); +#else + switch( ctx->var ) + { +#if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED) + case MBEDTLS_ECDH_VARIANT_EVEREST: + return( mbedtls_everest_calc_secret( &ctx->ctx.everest_ecdh, olen, + buf, blen, f_rng, p_rng ) ); +#endif + case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0: + return( ecdh_calc_secret_internal( &ctx->ctx.mbed_ecdh, olen, buf, + blen, f_rng, p_rng, + restart_enabled ) ); + default: + return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA ); + } +#endif +} + +#endif /* MBEDTLS_ECDH_ALT */ +#endif /* MBEDTLS_ECDH_C */ diff --git a/ra/fsp/src/rm_touch/rm_touch.c b/ra/fsp/src/rm_touch/rm_touch.c index 7fa08acc7..3f73746e8 100644 --- a/ra/fsp/src/rm_touch/rm_touch.c +++ b/ra/fsp/src/rm_touch/rm_touch.c @@ -32,7 +32,6 @@ **********************************************************************************************************************/ #ifndef TOUCH_ERROR_RETURN -/*LDRA_INSPECTED 77 S This macro does not work when surrounded by parentheses. */ #define TOUCH_ERROR_RETURN(a, err) FSP_ERROR_RETURN((a), (err)) #endif @@ -74,11 +73,9 @@ #define TOUCH_MONITOR_WHEEL_SIZE (4) #if (TOUCH_CFG_PAD_ENABLE) #define TOUCH_MONITOR_PAD_ELEMENT_SIZE (2) - #define TOUCH_MONITOR_PAD_TOUCH_MAX (10) - #define TOUCH_MONITOR_PAD_COORD_SIZE (4) + #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_TOUCH_MAX) + \ - (TOUCH_MONITOR_PAD_SIZE) + #define TOUCH_MONITOR_PAD_TOTAL_SIZE (TOUCH_MONITOR_PAD_COORD_SIZE + TOUCH_MONITOR_PAD_SIZE) #else #define TOUCH_MONITOR_PAD_TOTAL_SIZE (0) #endif @@ -142,7 +139,7 @@ /* UART Receive Buffer Size */ #define TOUCH_UART_RECIEVE_BUF_SIZE (13) -/* Register mask and bitshift */ +/* Register mask and bit-shift */ #if (BSP_FEATURE_CTSU_VERSION == 2) #define TOUCH_UART_CTSUSO_MASK (0x3FF) #define TOUCH_UART_CTSUSNUM_MASK (0xFF) @@ -165,7 +162,6 @@ #if (TOUCH_CFG_PAD_ENABLE) #define TOUCH_MAP_X (3) #define TOUCH_MAP_Y (3) - #define TOUCH_PAD_NOT_TOUCH (0xFFFF) #define TOUCH_PAD_TEMP_VALUE_OVERFLOW_BIT (0x8000) #define TOUCH_PAD_MONITOR_TOUCH_NUM_MAX (10) #endif @@ -174,16 +170,6 @@ * Typedef definitions **********************************************************************************************************************/ -#if (TOUCH_CFG_PAD_ENABLE) -typedef struct st_touch_pad_drift_id -{ - uint16_t i; - uint16_t j; - uint16_t num_rx; - uint16_t num_tx; -} touch_pad_drift_id_t; -#endif - /*********************************************************************************************************************** * Private function prototypes **********************************************************************************************************************/ @@ -340,119 +326,132 @@ fsp_err_t RM_TOUCH_Open (touch_ctrl_t * const p_ctrl, touch_cfg_t const * const /** Parameter Setting */ #if (TOUCH_CFG_NUM_BUTTONS != 0) - if (TOUCH_CFG_NUM_BUTTONS >= g_touch_button_index) - { - p_instance_ctrl->binfo.p_reference = &g_touch_button_reference[g_touch_button_index]; - p_instance_ctrl->binfo.p_on_count = &g_touch_button_on_count[g_touch_button_index]; - p_instance_ctrl->binfo.p_off_count = &g_touch_button_off_count[g_touch_button_index]; - p_instance_ctrl->binfo.p_drift_buf = &g_touch_button_drift_buf[g_touch_button_index]; - p_instance_ctrl->binfo.p_drift_count = &g_touch_button_drift_count[g_touch_button_index]; - p_instance_ctrl->binfo.p_threshold = &g_touch_button_threshold[g_touch_button_index]; - p_instance_ctrl->binfo.p_hysteresis = &g_touch_button_hysteresis[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->on_freq) - { - err = FSP_ERR_INVALID_ARGUMENT; - } - - if (0 == p_cfg->off_freq) + if (NULL != p_cfg->p_buttons) { - err = FSP_ERR_INVALID_ARGUMENT; - } + if (TOUCH_CFG_NUM_BUTTONS >= g_touch_button_index) + { + p_instance_ctrl->binfo.p_reference = &g_touch_button_reference[g_touch_button_index]; + p_instance_ctrl->binfo.p_on_count = &g_touch_button_on_count[g_touch_button_index]; + p_instance_ctrl->binfo.p_off_count = &g_touch_button_off_count[g_touch_button_index]; + p_instance_ctrl->binfo.p_drift_buf = &g_touch_button_drift_buf[g_touch_button_index]; + p_instance_ctrl->binfo.p_drift_count = &g_touch_button_drift_count[g_touch_button_index]; + p_instance_ctrl->binfo.p_threshold = &g_touch_button_threshold[g_touch_button_index]; + p_instance_ctrl->binfo.p_hysteresis = &g_touch_button_hysteresis[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) + if (0 == 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; - for (id = 0; id < p_cfg->num_buttons; id++) - { - if (p_cfg->p_buttons[id].threshold < p_cfg->p_buttons[id].hysteresis) + if (0 == p_cfg->off_freq) { err = FSP_ERR_INVALID_ARGUMENT; } - *(p_instance_ctrl->binfo.p_reference + id) = 0; - *(p_instance_ctrl->binfo.p_on_count + id) = 0; - *(p_instance_ctrl->binfo.p_off_count + id) = 0; - *(p_instance_ctrl->binfo.p_drift_buf + id) = 0; - *(p_instance_ctrl->binfo.p_drift_count + id) = 0; - *(p_instance_ctrl->binfo.p_threshold + id) = p_cfg->p_buttons[id].threshold; - *(p_instance_ctrl->binfo.p_hysteresis + id) = p_cfg->p_buttons[id].hysteresis; + 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; + + for (id = 0; id < p_cfg->num_buttons; id++) + { + if (p_cfg->p_buttons[id].threshold < p_cfg->p_buttons[id].hysteresis) + { + err = FSP_ERR_INVALID_ARGUMENT; + } + + *(p_instance_ctrl->binfo.p_reference + id) = 0; + *(p_instance_ctrl->binfo.p_on_count + id) = 0; + *(p_instance_ctrl->binfo.p_off_count + id) = 0; + *(p_instance_ctrl->binfo.p_drift_buf + id) = 0; + *(p_instance_ctrl->binfo.p_drift_count + id) = 0; + *(p_instance_ctrl->binfo.p_threshold + id) = p_cfg->p_buttons[id].threshold; + *(p_instance_ctrl->binfo.p_hysteresis + id) = p_cfg->p_buttons[id].hysteresis; + } } #endif #if (TOUCH_CFG_NUM_SLIDERS != 0) - 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 (NULL != p_cfg->p_sliders) { - if ((TOUCH_SLIDER_ELEMENTS_MIN > p_cfg->p_sliders[id].num_elements) || - (TOUCH_SLIDER_ELEMENTS_MAX < p_cfg->p_sliders[id].num_elements)) + if (TOUCH_CFG_NUM_SLIDERS >= g_touch_slider_index) { - err = FSP_ERR_INVALID_ARGUMENT; + 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); } - *(p_instance_ctrl->sinfo.p_threshold + id) = p_cfg->p_sliders[id].threshold; + 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 (TOUCH_CFG_NUM_WHEELS >= g_touch_wheel_index) + if (NULL != p_cfg->p_wheels) { - 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)) + if (TOUCH_CFG_NUM_WHEELS >= g_touch_wheel_index) { - err = FSP_ERR_INVALID_ARGUMENT; + 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); } - *(p_instance_ctrl->winfo.p_threshold + id) = p_cfg->p_wheels[id].threshold; + 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) - 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; + 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) @@ -475,7 +474,7 @@ fsp_err_t RM_TOUCH_Open (touch_ctrl_t * const p_ctrl, touch_cfg_t const * const } #endif #if (TOUCH_CFG_NUM_BUTTONS != 0) - if (0 < p_cfg->num_buttons) + if (NULL != p_cfg->p_buttons) { g_touch_monitor_size[num] = (uint16_t) (g_touch_monitor_size[num] + TOUCH_MONITOR_BHEAD_SIZE + @@ -483,7 +482,7 @@ fsp_err_t RM_TOUCH_Open (touch_ctrl_t * const p_ctrl, touch_cfg_t const * const } #endif #if (TOUCH_CFG_NUM_SLIDERS != 0) - if (0 < p_cfg->num_sliders) + if (NULL != p_cfg->p_sliders) { g_touch_monitor_size[num] = (uint16_t) (g_touch_monitor_size[num] + TOUCH_MONITOR_SHEAD_SIZE + @@ -491,7 +490,7 @@ fsp_err_t RM_TOUCH_Open (touch_ctrl_t * const p_ctrl, touch_cfg_t const * const } #endif #if (TOUCH_CFG_NUM_WHEELS != 0) - if (0 < p_cfg->num_wheels) + if (NULL != p_cfg->p_wheels) { g_touch_monitor_size[num] = (uint16_t) (g_touch_monitor_size[num] + TOUCH_MONITOR_WHEAD_SIZE + @@ -499,11 +498,11 @@ fsp_err_t RM_TOUCH_Open (touch_ctrl_t * const p_ctrl, touch_cfg_t const * const } #endif #if (TOUCH_CFG_PAD_ENABLE) - if (CTSU_MODE_MUTUAL_CFC_SCAN == (p_instance_ctrl->p_ctsu_instance->p_cfg->md)) + 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_SIZE + - (TOUCH_MONITOR_PAD_ELEMENT_SIZE * p_cfg->p_pad->num_elements)); + (uint16_t) (g_touch_monitor_size[num] + TOUCH_MONITOR_PAD_TOTAL_SIZE); } #endif @@ -855,7 +854,7 @@ fsp_err_t RM_TOUCH_PadDataGet (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; #if (TOUCH_CFG_PAD_ENABLE) - static uint16_t count_get[CTSU_CFG_NUM_CFC * CTSU_CFG_NUM_CFC_TX * 2]; + static uint16_t pad_buf[CTSU_CFG_NUM_CFC * CTSU_CFG_NUM_CFC_TX * 2]; static uint8_t base_flag = 0; uint16_t i; uint16_t j; @@ -883,12 +882,11 @@ fsp_err_t RM_TOUCH_PadDataGet (touch_ctrl_t * const p_ctrl, 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 */ - uint16_t second_half; + 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; - uint8_t element_num = 0; + uint16_t index = 0; #endif #if (TOUCH_CFG_PARAM_CHECKING_ENABLE == 1) @@ -900,40 +898,35 @@ fsp_err_t RM_TOUCH_PadDataGet (touch_ctrl_t * const p_ctrl, #endif /* initialize touch number */ - *p_pad_num_touch = 0; - - /* check for max touch */ - if (*(p_instance_ctrl->pinfo.p_max_touch) > ((CTSU_CFG_NUM_CFC * CTSU_CFG_NUM_CFC_TX) / 9)) - { - max_touch = (CTSU_CFG_NUM_CFC * CTSU_CFG_NUM_CFC_TX) / 9; - } - else - { - max_touch = *(p_instance_ctrl->pinfo.p_max_touch); - } + *(p_instance_ctrl->pinfo.p_num_touch) = 0; /* Get local variable (TS number & data pinch) */ num_x = p_instance_ctrl->p_touch_cfg->p_ctsu_instance->p_cfg->num_rx; num_y = p_instance_ctrl->p_touch_cfg->p_ctsu_instance->p_cfg->num_tx; - second_half = (uint16_t) (num_x * num_y); + element_num = (uint16_t) (num_x * num_y); pitch_x = (uint16_t) (*(p_instance_ctrl->pinfo.p_rx_pixel) / num_x); pitch_y = (uint16_t) (*(p_instance_ctrl->pinfo.p_tx_pixel) / num_y); /* Data get */ - err = p_instance_ctrl->p_ctsu_instance->p_api->dataGet(p_instance_ctrl->p_ctsu_instance->p_ctrl, count_get); + err = p_instance_ctrl->p_ctsu_instance->p_api->dataGet(p_instance_ctrl->p_ctsu_instance->p_ctrl, pad_buf); FSP_ERROR_RETURN(FSP_ERR_CTSU_SCANNING != err, FSP_ERR_CTSU_SCANNING); - if (FSP_SUCCESS != err) + /* 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 { - return err; + max_touch = *(p_instance_ctrl->pinfo.p_max_touch); } /* make difference value = secondary - primary */ - for (i = 0; i < CTSU_CFG_NUM_CFC * CTSU_CFG_NUM_CFC_TX; i++) + for (i = 0; i < element_num; i++) { /* save to buffer in the first half */ - count_get[i] = (uint16_t) (count_get[(i * 2) + 1] - count_get[i * 2]); + pad_buf[i] = (uint16_t) (pad_buf[(i * 2) + 1] - pad_buf[i * 2]); } /* Data get section */ @@ -945,8 +938,8 @@ fsp_err_t RM_TOUCH_PadDataGet (touch_ctrl_t * const p_ctrl, for (i = 0; i < num_y; i++) { *(p_instance_ctrl->pinfo.p_base_buf + j + (i * num_x)) = - (count_get[*(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)]); + (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)]); } } @@ -960,96 +953,29 @@ fsp_err_t RM_TOUCH_PadDataGet (touch_ctrl_t * const p_ctrl, for (i = 0; i < num_y; i++) { /* get count data ,and changing the order */ - tmp_value = count_get[*(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)]; + tmp_value = 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 = *(p_instance_ctrl->pinfo.p_base_buf + j + (i * num_x)) - tmp_value; /* save difference value to buffer in the second half */ - count_get[second_half + j + (i * num_x)] = (uint16_t) tmp_diff; + pad_buf[element_num + j + (i * num_x)] = (uint16_t) tmp_diff; } } } - /* 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]); - } - } - - if (!g_touch_uart_transmit_flag) - { - 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; - 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; - } - #else /* TOUCH_CFG_UART_MONITOR_SUPPORT */ - for (i = 0; i < p_instance_ctrl->p_touch_cfg->number; i++) + /* copy from second half to first half for monitor */ + for (i = 0; i < element_num; i++) { - index = (uint16_t) (index + g_touch_monitor_size[i]); + pad_buf[i] = pad_buf[element_num + 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; - 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; - #endif - - #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) || - (TOUCH_UART_MONITOR_NONE == g_touch_uart_monitor_num))) - { - #endif /* TOUCH_CFG_UART_MONITOR_SUPPORT */ - for (i = 0; i < element_num; i++) - { - tmp_value = count_get[second_half + i]; - if (tmp_value & TOUCH_PAD_TEMP_VALUE_OVERFLOW_BIT) - { - 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 */ - #if (TOUCH_CFG_UART_MONITOR_SUPPORT == 1) - } - #endif - #endif /* TOUCH_CFG_MONITOR_ENABLE */ + *(p_instance_ctrl->pinfo.p_rx_coordinate) = TOUCH_OFF_VALUE; + *(p_instance_ctrl->pinfo.p_tx_coordinate) = TOUCH_OFF_VALUE; if (base_flag) { @@ -1069,225 +995,218 @@ fsp_err_t RM_TOUCH_PadDataGet (touch_ctrl_t * const p_ctrl, } /* Maximum value search */ - for (j = 0; j < (num_x); j++) + for (j = 0; j < num_x; j++) { - for (i = 0; i < (num_y); i++) + for (i = 0; i < num_y; i++) { - if ((int16_t) count_get[second_half + j + (i * num_x)] > max_diff) + if ((int16_t) pad_buf[element_num + j + (i * num_x)] > max_diff) { - max_diff = (int16_t) count_get[second_half + j + (i * num_x)]; + max_diff = (int16_t) pad_buf[element_num + j + (i * num_x)]; max_y = i; max_x = j; } } } - /* 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 + /* touch check */ + if (max_diff >= *(p_instance_ctrl->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 - { - } + 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++) + /* make heat mapping */ + for (i = 0; i < TOUCH_MAP_X; i++) { - if (use_map[j + (i * TOUCH_MAP_X)]) + for (j = 0; j < TOUCH_MAP_Y; j++) { - heat_map[j + (i * TOUCH_MAP_X)] = - (int16_t) count_get[second_half + (max_x - 1 + j) + (max_y - 1 + i) * num_x]; + if (use_map[j + (i * TOUCH_MAP_X)]) + { + heat_map[j + (i * TOUCH_MAP_X)] = + (int16_t) 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 */ - } + /* 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 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 + 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 */ - } + /* 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]); - } + 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 */ - } + /* 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_pad_rx_coordinate + loop) = (uint16_t) ((pitch_x * (max_x + 1) - pitch_x / 2) + x_parameter); + /* Coordinate x value based on pitch */ + *(p_instance_ctrl->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]); - } + /* 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 */ - } + /* 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_pad_tx_coordinate + loop) = (uint16_t) ((pitch_y * (max_y + 1) - pitch_y / 2) + y_parameter); + /* Coordinate y value based on pitch */ + *(p_instance_ctrl->pinfo.p_tx_coordinate + loop) = + (uint16_t) ((pitch_y * (max_y + 1) - pitch_y / 2) + y_parameter); - /* touch check */ - if (max_diff >= *(p_instance_ctrl->pinfo.p_threshold)) - { - /* If touching ,then counter's being incremented */ - (*p_pad_num_touch)++; + /* If touching ,then counter increment */ + (*(p_instance_ctrl->pinfo.p_num_touch))++; - /* Clear heat map area */ + /* 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]) { - count_get[second_half + (max_x - 1 + j) + (max_y - 1 + i) * num_x] = 0; + pad_buf[element_num + (max_x - 1 + j) + (max_y - 1 + i) * num_x] = 0; } } } } else { - *(p_pad_rx_coordinate + loop) = TOUCH_PAD_NOT_TOUCH; - *(p_pad_tx_coordinate + loop) = TOUCH_PAD_NOT_TOUCH; - } - - if (loop < TOUCH_PAD_MONITOR_TOUCH_NUM_MAX) - { - *(p_instance_ctrl->pinfo.p_rx_coordinate + loop) = *(p_pad_rx_coordinate + loop); - *(p_instance_ctrl->pinfo.p_tx_coordinate + loop) = *(p_pad_tx_coordinate + loop); - - FSP_PARAMETER_NOT_USED(p_instance_ctrl->pinfo.p_rx_coordinate); - FSP_PARAMETER_NOT_USED(p_instance_ctrl->pinfo.p_tx_coordinate); + *(p_instance_ctrl->pinfo.p_rx_coordinate + loop) = TOUCH_OFF_VALUE; + *(p_instance_ctrl->pinfo.p_tx_coordinate + loop) = TOUCH_OFF_VALUE; } } /* the drift correction process. */ if (0 < p_instance_ctrl->pinfo.num_drift) { - if (0 == *p_pad_num_touch) + 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))++; @@ -1297,7 +1216,7 @@ fsp_err_t RM_TOUCH_PadDataGet (touch_ctrl_t * const p_ctrl, for (j = 0; j < num_x; j++) { /* It is an addition for the drift correction average calculation */ - tmp_count = (int16_t) count_get[second_half + j + (i * num_x)]; + tmp_count = (int16_t) pad_buf[element_num + j + (i * num_x)]; *(p_instance_ctrl->pinfo.p_drift_buf + (j + (i * num_x))) += (int32_t) tmp_count; @@ -1325,28 +1244,87 @@ fsp_err_t RM_TOUCH_PadDataGet (touch_ctrl_t * const p_ctrl, } } - *(p_instance_ctrl->pinfo.p_num_touch) = *p_pad_num_touch; - - for (i = *p_pad_num_touch; i < TOUCH_PAD_MONITOR_TOUCH_NUM_MAX; i++) + /* Set user data */ + loop = 0; + do { - g_touch_pad_rx_coordinate[i] = TOUCH_PAD_NOT_TOUCH; - g_touch_pad_tx_coordinate[i] = TOUCH_PAD_NOT_TOUCH; - } + *(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); - /* Monitor part of result */ + *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))) { - g_touch_monitor_buf[index++] = *p_pad_num_touch; /* number of touch */ + 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 = pad_buf[i]; + if (tmp_value & TOUCH_PAD_TEMP_VALUE_OVERFLOW_BIT) + { + 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++) { - 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); + 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)); @@ -1359,38 +1337,20 @@ fsp_err_t RM_TOUCH_PadDataGet (touch_ctrl_t * const p_ctrl, g_touch_monitor_buf[index++] = (p_instance_ctrl->pinfo.num_drift); g_touch_monitor_buf[index++] = g_touch_monitor_id++; - 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 const * const) &g_touch_monitor_buf, - index); - } + #if (TOUCH_CFG_UART_MONITOR_SUPPORT == 1) } - #else /* TOUCH_CFG_UART_MONITOR_SUPPORT */ - g_touch_monitor_buf[index++] = *p_pad_num_touch; /* number of touch */ - for (i = 0; i < TOUCH_PAD_MONITOR_TOUCH_NUM_MAX; i++) + if (p_instance_ctrl->p_touch_cfg->number == g_touch_uart_monitor_num) { - 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); + 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 const * const) &g_touch_monitor_buf, + index); } - - 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++; - #endif /* TOUCH_CFG_UART_MONITOR_SUPPORT */ + #endif #endif /* TOUCH_CFG_MONITOR_ENABLE */ #else /* TOUCH_CFG_PAD_ENABLE */ #if (TOUCH_CFG_PARAM_CHECKING_ENABLE == 1) diff --git a/ra/fsp/src/rm_vee_flash/rm_vee_flash.c b/ra/fsp/src/rm_vee_flash/rm_vee_flash.c index faa1f6fa9..a842aa82a 100644 --- a/ra/fsp/src/rm_vee_flash/rm_vee_flash.c +++ b/ra/fsp/src/rm_vee_flash/rm_vee_flash.c @@ -29,45 +29,40 @@ * Macro definitions **********************************************************************************************************************/ -#ifdef RM_VEE_FLASH_DATA_START -static const uint32_t rm_vee_flash_data_start = RM_VEE_FLASH_DATA_START; -#else -static const uint32_t rm_vee_flash_data_start = BSP_FEATURE_FLASH_DATA_FLASH_START; -#endif - /* INTERRUPT TIMEOUT VALUES (for Open() to catch user global interrupt disable) */ #ifndef RM_VEE_FLASH_TIMEOUT_US - #define RM_VEE_FLASH_TIMEOUT_US (1000000) + #define RM_VEE_FLASH_TIMEOUT_US (1000000) #endif #ifndef RM_VEE_FLASH_TIMEOUT_REFRESH_US - #define RM_VEE_FLASH_TIMEOUT_REFRESH_US (5000000) + #define RM_VEE_FLASH_TIMEOUT_REFRESH_US (5000000) #endif -#define RM_VEE_FLASH_OPEN (0x52564545U) -#define RM_VEE_FLASH_VALID_CODE (0xBEAD) -#define RM_VEE_FLASH_ID_INVALID (UINT16_MAX) -#define RM_VEE_FLASH_DF_END_ADDRESS (rm_vee_flash_data_start + p_ctrl->p_cfg->total_size) -#define RM_VEE_FLASH_REC_OVERHEAD (sizeof(rm_vee_rec_hdr_t) + sizeof(rm_vee_rec_end_t)) -#define RM_VEE_FLASH_REF_DATA_COUNT (2) +#define RM_VEE_FLASH_OPEN (0x52564545U) +#define RM_VEE_FLASH_VALID_CODE (0xBEAD) +#define RM_VEE_FLASH_ID_INVALID (UINT16_MAX) +#define RM_VEE_FLASH_LOGICAL_END_ADDRESS (p_ctrl->p_cfg->start_addr + p_ctrl->p_cfg->total_size) +#define RM_VEE_FLASH_PHYSICAL_END_ADDRESS (BSP_FEATURE_FLASH_DATA_FLASH_START + BSP_DATA_FLASH_SIZE_BYTES) +#define RM_VEE_FLASH_REC_OVERHEAD (sizeof(rm_vee_rec_hdr_t) + sizeof(rm_vee_rec_end_t)) +#define RM_VEE_FLASH_REF_DATA_COUNT (2) #if RM_VEE_FLASH_CFG_REF_DATA_SUPPORT - #define RM_VEE_FLASH_REF_DATA_AREA_SIZE ((sizeof(rm_vee_ref_hdr_t) + \ - (p_ctrl->p_cfg->ref_data_size * RM_VEE_FLASH_REF_DATA_COUNT))) + #define RM_VEE_FLASH_REF_DATA_AREA_SIZE ((sizeof(rm_vee_ref_hdr_t) + \ + (p_ctrl->p_cfg->ref_data_size * RM_VEE_FLASH_REF_DATA_COUNT))) #else - #define RM_VEE_FLASH_REF_DATA_AREA_SIZE (0) + #define RM_VEE_FLASH_REF_DATA_AREA_SIZE (0) #endif /* Use appropriate macros for Flash HP or LP */ #if (BSP_FEATURE_FLASH_HP_DF_BLOCK_SIZE > 0) - #define RM_VEE_FLASH_DF_BLOCK_SIZE (BSP_FEATURE_FLASH_HP_DF_BLOCK_SIZE) + #define RM_VEE_FLASH_DF_BLOCK_SIZE (BSP_FEATURE_FLASH_HP_DF_BLOCK_SIZE) #else - #define RM_VEE_FLASH_DF_BLOCK_SIZE (BSP_FEATURE_FLASH_LP_DF_BLOCK_SIZE) + #define RM_VEE_FLASH_DF_BLOCK_SIZE (BSP_FEATURE_FLASH_LP_DF_BLOCK_SIZE) #endif -#define RM_VEE_FLASH_DF_WRITE_SIZE (4) +#define RM_VEE_FLASH_DF_WRITE_SIZE (4) -#define RM_VEE_FLASH_REC_DATA_MAX_SIZE (p_ctrl->segment_size - \ - (sizeof(rm_vee_seg_hdr_t) + (p_ctrl->p_cfg->ref_data_size * 2) + \ - sizeof(rm_vee_ref_hdr_t) + RM_VEE_FLASH_REC_OVERHEAD)) +#define RM_VEE_FLASH_REC_DATA_MAX_SIZE (p_ctrl->segment_size - \ + (sizeof(rm_vee_seg_hdr_t) + (p_ctrl->p_cfg->ref_data_size * 2) + \ + sizeof(rm_vee_ref_hdr_t) + RM_VEE_FLASH_REC_OVERHEAD)) /* Version data structure used by error logger macro. */ static const fsp_version_t g_vee_version = @@ -115,7 +110,7 @@ typedef enum e_rm_vee_flash_refresh_refresh } rm_vee_flash_refresh_refresh_t; #if defined(__ARMCC_VERSION) || defined(__ICCARM__) -typedef void (BSP_CMSE_NONSECURE_CALL * volatile rm_vee_flash_prv_ns_callback)(rm_vee_callback_args_t * p_args); +typedef void (BSP_CMSE_NONSECURE_CALL * rm_vee_flash_prv_ns_callback)(rm_vee_callback_args_t * p_args); #elif defined(__GNUC__) typedef BSP_CMSE_NONSECURE_CALL void (*volatile rm_vee_flash_prv_ns_callback)(rm_vee_callback_args_t * p_args); #endif @@ -217,9 +212,22 @@ fsp_err_t RM_VEE_FLASH_Open (rm_vee_ctrl_t * const p_api_ctrl, rm_vee_cfg_t cons #if RM_VEE_FLASH_CFG_PARAM_CHECKING_ENABLE == 1 FSP_ASSERT(p_ctrl); FSP_ASSERT(p_cfg); + + /* Interface isn't already open and is configured for more than one segment */ FSP_ERROR_RETURN(RM_VEE_FLASH_OPEN != p_ctrl->open, FSP_ERR_ALREADY_OPEN); FSP_ERROR_RETURN(2 <= p_cfg->num_segments, FSP_ERR_INVALID_ARGUMENT); - FSP_ERROR_RETURN(BSP_DATA_FLASH_SIZE_BYTES >= p_cfg->total_size, FSP_ERR_INVALID_ARGUMENT); + + /* Start and end adress are valid */ + FSP_ERROR_RETURN(BSP_FEATURE_FLASH_DATA_FLASH_START <= p_cfg->start_addr, FSP_ERR_INVALID_ARGUMENT); + FSP_ERROR_RETURN(RM_VEE_FLASH_PHYSICAL_END_ADDRESS >= (p_cfg->start_addr + p_cfg->total_size), + FSP_ERR_INVALID_ARGUMENT); + + /* All memory segments are correctly aligned */ + FSP_ERROR_RETURN(0 == (p_cfg->start_addr - BSP_FEATURE_FLASH_DATA_FLASH_START) % RM_VEE_FLASH_DF_BLOCK_SIZE, + FSP_ERR_INVALID_ARGUMENT); + FSP_ERROR_RETURN(0 == p_cfg->total_size % RM_VEE_FLASH_DF_BLOCK_SIZE, FSP_ERR_INVALID_ARGUMENT); + FSP_ERROR_RETURN(0 == (p_cfg->total_size / RM_VEE_FLASH_DF_BLOCK_SIZE) % p_cfg->num_segments, + FSP_ERR_INVALID_ARGUMENT); FSP_ERROR_RETURN(0 == (p_cfg->total_size % p_cfg->num_segments), FSP_ERR_INVALID_ARGUMENT); FSP_ERROR_RETURN(0 == ((p_cfg->total_size / p_cfg->num_segments) % 4), FSP_ERR_INVALID_ARGUMENT); FSP_ERROR_RETURN(0 == (p_cfg->ref_data_size % RM_VEE_FLASH_DF_WRITE_SIZE), FSP_ERR_INVALID_ARGUMENT); @@ -596,7 +604,8 @@ fsp_err_t RM_VEE_FLASH_Format (rm_vee_ctrl_t * const p_api_ctrl, uint8_t const * #endif p_ctrl->state = RM_VEE_FLASH_PRV_STATES_UNINITIALIZED; fsp_err_t err = - rm_vee_blocking_erase(p_ctrl, rm_vee_flash_data_start, + rm_vee_blocking_erase(p_ctrl, + p_ctrl->p_cfg->start_addr, (p_ctrl->p_cfg->total_size / RM_VEE_FLASH_DF_BLOCK_SIZE)); #if RM_VEE_FLASH_CFG_REF_DATA_SUPPORT @@ -605,7 +614,7 @@ fsp_err_t RM_VEE_FLASH_Format (rm_vee_ctrl_t * const p_api_ctrl, uint8_t const * err = rm_vee_blocking_write(p_ctrl, (uint32_t) p_ref_data, - (RM_VEE_FLASH_DF_END_ADDRESS - p_ctrl->p_cfg->ref_data_size), + (RM_VEE_FLASH_LOGICAL_END_ADDRESS - p_ctrl->p_cfg->ref_data_size), p_ctrl->p_cfg->ref_data_size); } @@ -830,7 +839,7 @@ static fsp_err_t rm_vee_inspect_segments (rm_vee_flash_instance_ctrl_t * const p /* Loop through each segment */ for (uint32_t i = 0; i < p_ctrl->p_cfg->num_segments; i++) { - addr = rm_vee_flash_data_start + (i * p_ctrl->segment_size); + addr = p_ctrl->p_cfg->start_addr + (i * p_ctrl->segment_size); #if RM_VEE_FLASH_CFG_REF_DATA_SUPPORT @@ -923,7 +932,7 @@ static fsp_err_t rm_vee_inspect_segments (rm_vee_flash_instance_ctrl_t * const p /* If no active segment found, set highest flash segment as active in driver control structure. */ if (0 == p_ctrl->active_seg_addr) { - p_ctrl->active_seg_addr = RM_VEE_FLASH_DF_END_ADDRESS - p_ctrl->segment_size; + p_ctrl->active_seg_addr = RM_VEE_FLASH_LOGICAL_END_ADDRESS - p_ctrl->segment_size; /* Make segment active in flash if no errors were encountered */ if (FSP_SUCCESS == err) @@ -933,7 +942,7 @@ static fsp_err_t rm_vee_inspect_segments (rm_vee_flash_instance_ctrl_t * const p err = rm_vee_blocking_write(p_ctrl, (uint32_t) &p_ctrl->seg_hdr, - RM_VEE_FLASH_DF_END_ADDRESS - p_ctrl->segment_size, + RM_VEE_FLASH_LOGICAL_END_ADDRESS - p_ctrl->segment_size, sizeof(rm_vee_seg_hdr_t)); } } @@ -1231,19 +1240,14 @@ static fsp_err_t rm_vee_blocking_erase (rm_vee_flash_instance_ctrl_t * const p_c /* Clear interrupt-occurred flag */ p_ctrl->irq_flag = false; - p_ctrl->p_flash->p_api->erase(p_ctrl->p_flash->p_ctrl, addr, num_blocks); + err = p_ctrl->p_flash->p_api->erase(p_ctrl->p_flash->p_ctrl, addr, num_blocks); FSP_ERROR_RETURN(FSP_SUCCESS == err, err); /* Poll for interrupt complete. Do timeout check in case app disabled interrupts. * Erase is for (p_ctrl->segment_size/RM_VEE_FLASH_DF_BLOCK_SIZE). */ - for (i = 0; i < RM_VEE_FLASH_TIMEOUT_US; i++) + for (i = 0; (i < RM_VEE_FLASH_TIMEOUT_US) && (p_ctrl->irq_flag != true); i++) { - if (p_ctrl->irq_flag == true) - { - break; - } - R_BSP_SoftwareDelay(1, BSP_DELAY_UNITS_MICROSECONDS); } @@ -1441,9 +1445,9 @@ static fsp_err_t rm_vee_start_seg_refresh (rm_vee_flash_instance_ctrl_t * const /* Get refresh segment address. Handle wrap-around case. */ new_seg_addr = p_ctrl->active_seg_addr + p_ctrl->segment_size; - if (RM_VEE_FLASH_DF_END_ADDRESS == new_seg_addr) + if (RM_VEE_FLASH_LOGICAL_END_ADDRESS == new_seg_addr) { - new_seg_addr = rm_vee_flash_data_start; + new_seg_addr = p_ctrl->p_cfg->start_addr; } /* Setup control structure for new segment */ diff --git a/ra/fsp/src/rm_wifi_onchip_silex/rm_wifi_onchip_silex.c b/ra/fsp/src/rm_wifi_onchip_silex/rm_wifi_onchip_silex.c index 9ba955535..6e37da7ac 100644 --- a/ra/fsp/src/rm_wifi_onchip_silex/rm_wifi_onchip_silex.c +++ b/ra/fsp/src/rm_wifi_onchip_silex/rm_wifi_onchip_silex.c @@ -294,6 +294,12 @@ size_t xStreamBufferReceiveAlternate(StreamBufferHandle_t xStreamBuffer, size_t xBufferLengthBytes, TickType_t xTicksToWait); +static fsp_err_t rm_wifi_onchip_silex_send_scan(wifi_onchip_silex_instance_ctrl_t * p_instance_ctrl, + uint32_t serial_ch_id, + bool send_scan_cmd, + uint32_t byte_timeout, + uint32_t timeout_ms); + /*******************************************************************************************************************//** * @addtogroup WIFI_ONCHIP_SILEX WIFI_ONCHIP_SILEX * @{ @@ -396,12 +402,12 @@ fsp_err_t rm_wifi_onchip_silex_open (wifi_onchip_silex_cfg_t const * const p_cfg vTaskDelay(pdMS_TO_TICKS(WIFI_ONCHIP_SILEX_TIMEOUT_1SEC)); - // Create memory copy of uart extended configuration and then copy new configuration values in. + /* Create memory copy of uart extended configuration and then copy new configuration values in. */ memcpy((void *) &uart0_cfg_extended_115200, (void *) p_instance_ctrl->uart_instance_objects[0]->p_cfg->p_extend, sizeof(sci_uart_extended_cfg_t)); - // Create memory copy of uart configuration and update with new extended configuration structure. + /* Create memory copy of uart configuration and update with new extended configuration structure. */ memcpy((void *) &uart0_cfg_115200, p_instance_ctrl->uart_instance_objects[0]->p_cfg, sizeof(uart_cfg_t)); R_SCI_UART_BaudCalculate(WIFI_ONCHIP_SILEX_DEFAULT_BAUDRATE, @@ -416,7 +422,7 @@ fsp_err_t rm_wifi_onchip_silex_open (wifi_onchip_silex_cfg_t const * const p_cfg uart0_cfg_115200.p_extend = (void *) &uart0_cfg_extended_115200; - // call uart open() + /* call uart open() */ p_uart = p_instance_ctrl->uart_instance_objects[WIFI_ONCHIP_SILEX_UART_INITIAL_PORT]; err = p_uart->p_api->open(p_uart->p_ctrl, &uart0_cfg_115200); @@ -448,8 +454,8 @@ fsp_err_t rm_wifi_onchip_silex_open (wifi_onchip_silex_cfg_t const * const p_cfg FSP_ERROR_RETURN(FSP_SUCCESS == err, FSP_ERR_WIFI_FAILED); - // create string for wifi modem baud rate change. - // using currently unused socket RX buffer for temp string. + /* create string for wifi modem baud rate change. + * using currently unused socket RX buffer for temp string. */ memset(p_instance_ctrl->sockets[0].socket_recv_buff, 0, sizeof(p_instance_ctrl->sockets[0].socket_recv_buff)); strncat((char *) p_instance_ctrl->sockets[0].socket_recv_buff, "ATB=", 5); strncat((char *) p_instance_ctrl->sockets[0].socket_recv_buff, g_wifi_onchip_silex_uart_cmd_baud, 10); @@ -492,7 +498,7 @@ fsp_err_t rm_wifi_onchip_silex_open (wifi_onchip_silex_cfg_t const * const p_cfg p_uart = p_instance_ctrl->uart_instance_objects[WIFI_ONCHIP_SILEX_UART_INITIAL_PORT]; err = p_uart->p_api->open(p_uart->p_ctrl, p_uart->p_cfg); - // Set uart enabled value + /* Set uart enabled value */ if (FSP_SUCCESS != err) { rm_wifi_onchip_silex_cleanup_open(p_instance_ctrl); @@ -563,7 +569,7 @@ fsp_err_t rm_wifi_onchip_silex_open (wifi_onchip_silex_cfg_t const * const p_cfg p_uart = p_instance_ctrl->uart_instance_objects[WIFI_ONCHIP_SILEX_UART_SECOND_PORT]; err = p_uart->p_api->open(p_uart->p_ctrl, p_uart->p_cfg); - // Set uart enabled value + /* Set uart enabled value */ if (FSP_SUCCESS != err) { rm_wifi_onchip_silex_cleanup_open(p_instance_ctrl); @@ -730,6 +736,18 @@ fsp_err_t rm_wifi_onchip_silex_close () p_instance_ctrl->open = 0; + /* If only one UART is used then send escape sequence command to exit transparent mode */ + if ((1 == p_instance_ctrl->num_uarts) && + (p_instance_ctrl->sockets[0].socket_status == WIFI_ONCHIP_SILEX_SOCKET_STATUS_CONNECTED)) + { + rm_wifi_onchip_silex_send_basic(p_instance_ctrl, + p_instance_ctrl->curr_cmd_port, + "+++", + WIFI_ONCHIP_SILEX_TIMEOUT_3MS, + WIFI_ONCHIP_SILEX_TIMEOUT_1SEC, + WIFI_ONCHIP_SILEX_RETURN_OK); + } + /* Tell wifi module to disconnect from the current AP */ err = rm_wifi_onchip_silex_send_basic(p_instance_ctrl, p_instance_ctrl->curr_cmd_port, @@ -737,6 +755,7 @@ fsp_err_t rm_wifi_onchip_silex_close () WIFI_ONCHIP_SILEX_TIMEOUT_3MS, WIFI_ONCHIP_SILEX_TIMEOUT_1SEC, WIFI_ONCHIP_SILEX_RETURN_OK); + if (FSP_SUCCESS != err) { rm_wifi_onchip_silex_wifi_module_reset(p_instance_ctrl); @@ -772,6 +791,18 @@ fsp_err_t rm_wifi_onchip_silex_disconnect () FSP_ERROR_RETURN(FSP_SUCCESS == rm_wifi_onchip_silex_send_basic_take_mutex(p_instance_ctrl, mutex_flag), FSP_ERR_WIFI_FAILED); + /* If only one UART is used then send escape sequence command to exit transparent mode */ + if ((1 == p_instance_ctrl->num_uarts) && + (p_instance_ctrl->sockets[0].socket_status == WIFI_ONCHIP_SILEX_SOCKET_STATUS_CONNECTED)) + { + rm_wifi_onchip_silex_send_basic(p_instance_ctrl, + p_instance_ctrl->curr_cmd_port, + "+++", + WIFI_ONCHIP_SILEX_TIMEOUT_3MS, + WIFI_ONCHIP_SILEX_TIMEOUT_1SEC, + WIFI_ONCHIP_SILEX_RETURN_OK); + } + /* Tell wifi module to disconnect from the current AP */ rm_wifi_onchip_silex_send_basic(p_instance_ctrl, p_instance_ctrl->curr_cmd_port, @@ -1060,45 +1091,56 @@ fsp_err_t rm_wifi_onchip_silex_mac_addr_get (uint8_t * p_macaddr) return FSP_ERR_WIFI_FAILED; } - ret = rm_wifi_onchip_silex_send_basic(p_instance_ctrl, - p_instance_ctrl->curr_cmd_port, - "ATW\r", - WIFI_ONCHIP_SILEX_TIMEOUT_100MS, - WIFI_ONCHIP_SILEX_TIMEOUT_3SEC, - WIFI_ONCHIP_SILEX_RETURN_OK); - if (FSP_SUCCESS != ret) + /* Return an error if a socket is connected */ + if ((1 == p_instance_ctrl->num_uarts) && + (p_instance_ctrl->sockets[0].socket_status == WIFI_ONCHIP_SILEX_SOCKET_STATUS_CONNECTED)) { rm_wifi_onchip_silex_send_basic_give_mutex(p_instance_ctrl, mutex_flag); return FSP_ERR_WIFI_FAILED; } - // NOLINTNEXTLINE(cert-err34-c) Disable warning about the use of sscanf - err = sscanf((const char *) p_instance_ctrl->cmd_rx_buff, - "%*[^\n]\n%*[^\n]\n%*[^\n]\nMac Addr = %2x:%2x:%2x:%2x:%2x:%2x\r", - &macaddr[0], - &macaddr[1], - &macaddr[2], - &macaddr[3], - &macaddr[4], - &macaddr[5]); + ret = rm_wifi_onchip_silex_send_basic(p_instance_ctrl, + p_instance_ctrl->curr_cmd_port, + "ATW\r", + WIFI_ONCHIP_SILEX_TIMEOUT_100MS, + WIFI_ONCHIP_SILEX_TIMEOUT_3SEC, + WIFI_ONCHIP_SILEX_RETURN_OK); - if (6 == err) + if (FSP_SUCCESS == ret) { - for (int i = 0; i < 6; i++) + // NOLINTNEXTLINE(cert-err34-c) Disable warning about the use of sscanf + err = sscanf((const char *) p_instance_ctrl->cmd_rx_buff, + "%*[^\n]\n%*[^\n]\n%*[^\n]\nMac Addr = %2x:%2x:%2x:%2x:%2x:%2x\r", + &macaddr[0], + &macaddr[1], + &macaddr[2], + &macaddr[3], + &macaddr[4], + &macaddr[5]); + + if (6 == err) + { + for (int i = 0; i < 6; i++) + { + p_macaddr[i] = (uint8_t) macaddr[i]; + } + } + else { - p_macaddr[i] = (uint8_t) macaddr[i]; + ret = FSP_ERR_WIFI_FAILED; } - - rm_wifi_onchip_silex_send_basic_give_mutex(p_instance_ctrl, mutex_flag); } - else + + if (FSP_SUCCESS != ret) { rm_wifi_onchip_silex_send_basic_give_mutex(p_instance_ctrl, mutex_flag); return FSP_ERR_WIFI_FAILED; } + rm_wifi_onchip_silex_send_basic_give_mutex(p_instance_ctrl, mutex_flag); + return FSP_SUCCESS; } @@ -1108,10 +1150,11 @@ fsp_err_t rm_wifi_onchip_silex_mac_addr_get (uint8_t * p_macaddr) * @param[out] p_results Pointer to a structure array holding scanned Access Points. * @param[in] maxNetworks Size of the structure array for holding APs. * - * @retval FSP_SUCCESS Function completed successfully. - * @retval FSP_ERR_WIFI_FAILED Error occurred with command to Wifi module. - * @retval FSP_ERR_ASSERTION Assertion error occurred. - * @retval FSP_ERR_NOT_OPEN The instance has not been opened. + * @retval FSP_SUCCESS Function completed successfully. + * @retval FSP_ERR_WIFI_FAILED Error occurred with command to Wifi module. + * @retval FSP_ERR_ASSERTION Assertion error occurred. + * @retval FSP_ERR_NOT_OPEN The instance has not been opened. + * @retval FSP_ERR_WIFI_SCAN_COMPLETE Wifi scan has completed. **********************************************************************************************************************/ fsp_err_t rm_wifi_onchip_silex_scan (WIFIScanResult_t * p_results, uint32_t maxNetworks) { @@ -1138,13 +1181,22 @@ fsp_err_t rm_wifi_onchip_silex_scan (WIFIScanResult_t * p_results, uint32_t maxN FSP_ERROR_RETURN(FSP_SUCCESS == rm_wifi_onchip_silex_send_basic_take_mutex(p_instance_ctrl, mutex_flag), FSP_ERR_WIFI_FAILED); - ret = rm_wifi_onchip_silex_send_basic(p_instance_ctrl, - p_instance_ctrl->curr_cmd_port, - "ATWS\r", - WIFI_ONCHIP_SILEX_TIMEOUT_1SEC, - WIFI_ONCHIP_SILEX_TIMEOUT_8SEC, - WIFI_ONCHIP_SILEX_RETURN_OK); + /* Return an error if a socket is connected */ + if ((1 == p_instance_ctrl->num_uarts) && + (p_instance_ctrl->sockets[0].socket_status == WIFI_ONCHIP_SILEX_SOCKET_STATUS_CONNECTED)) + { + rm_wifi_onchip_silex_send_basic_give_mutex(p_instance_ctrl, mutex_flag); + + return FSP_ERR_WIFI_FAILED; + } + ret = rm_wifi_onchip_silex_send_scan(p_instance_ctrl, + p_instance_ctrl->curr_cmd_port, + true, + WIFI_ONCHIP_SILEX_TIMEOUT_1SEC, + WIFI_ONCHIP_SILEX_TIMEOUT_8SEC); + + /* Test if AT commands were successful */ if (FSP_SUCCESS != ret) { rm_wifi_onchip_silex_send_basic_give_mutex(p_instance_ctrl, mutex_flag); @@ -1159,10 +1211,6 @@ fsp_err_t rm_wifi_onchip_silex_scan (WIFIScanResult_t * p_results, uint32_t maxN int32_t test_ssid = strncmp(ptr, "ssid =", 6); if (0 != test_ssid) { - if(0 == idx) - { - ret = FSP_ERR_WIFI_FAILED; // Could not find first access point entry - } break; } @@ -1174,7 +1222,7 @@ fsp_err_t rm_wifi_onchip_silex_scan (WIFIScanResult_t * p_results, uint32_t maxN // NOLINTNEXTLINE(cert-err34-c) Disable warning about the use of sscanf err = sscanf(ptr, string_build, p_results[idx].cSSID); - if(1 != err) + if (1 != err) { ret = FSP_ERR_WIFI_FAILED; break; @@ -1207,7 +1255,7 @@ fsp_err_t rm_wifi_onchip_silex_scan (WIFIScanResult_t * p_results, uint32_t maxN &bssid2[4], &bssid2[5]); FSP_ERROR_RETURN(6 == err, FSP_ERR_WIFI_FAILED); - if(6 != err) + if (6 != err) { ret = FSP_ERR_WIFI_FAILED; break; @@ -1235,7 +1283,7 @@ fsp_err_t rm_wifi_onchip_silex_scan (WIFIScanResult_t * p_results, uint32_t maxN // NOLINTNEXTLINE(cert-err34-c) Disable warning about the use of sscanf err = sscanf(ptr, "channel = %d\r", (int *) &temp_val); - if(1 != err) + if (1 != err) { ret = FSP_ERR_WIFI_FAILED; break; @@ -1257,7 +1305,7 @@ fsp_err_t rm_wifi_onchip_silex_scan (WIFIScanResult_t * p_results, uint32_t maxN // NOLINTNEXTLINE(cert-err34-c) Disable warning about the use of sscanf err = sscanf(ptr, "indicator = %d\r", (int *) &temp_val); - if(1 != err) + if (1 != err) { ret = FSP_ERR_WIFI_FAILED; break; @@ -1346,9 +1394,24 @@ fsp_err_t rm_wifi_onchip_silex_scan (WIFIScanResult_t * p_results, uint32_t maxN { break; } - + + rm_wifi_onchip_silex_send_scan(p_instance_ctrl, + p_instance_ctrl->curr_cmd_port, + false, + WIFI_ONCHIP_SILEX_TIMEOUT_1SEC, + WIFI_ONCHIP_SILEX_TIMEOUT_8SEC); + + ptr = (char *) (p_instance_ctrl->cmd_rx_buff); } while (++idx < maxNetworks); - + + /* Clear out the rest of the access points returned from the module */ + while (FSP_ERR_WIFI_FAILED != + rm_wifi_onchip_silex_send_scan(p_instance_ctrl, p_instance_ctrl->curr_cmd_port, false, + WIFI_ONCHIP_SILEX_TIMEOUT_1SEC, WIFI_ONCHIP_SILEX_TIMEOUT_8SEC)) + { + ; + } + rm_wifi_onchip_silex_send_basic_give_mutex(p_instance_ctrl, mutex_flag); return ret; @@ -1370,7 +1433,7 @@ fsp_err_t rm_wifi_onchip_silex_ping (uint8_t * p_ip_addr, uint32_t count, uint32 { FSP_PARAMETER_NOT_USED(interval_ms); - fsp_err_t ret; + fsp_err_t ret = FSP_SUCCESS; uint32_t i; uint32_t mutex_flag; @@ -1392,6 +1455,15 @@ fsp_err_t rm_wifi_onchip_silex_ping (uint8_t * p_ip_addr, uint32_t count, uint32 p_ip_addr[2], p_ip_addr[3]); + /* Return an error if a socket is connected */ + if ((1 == p_instance_ctrl->num_uarts) && + (p_instance_ctrl->sockets[0].socket_status == WIFI_ONCHIP_SILEX_SOCKET_STATUS_CONNECTED)) + { + rm_wifi_onchip_silex_send_basic_give_mutex(p_instance_ctrl, mutex_flag); + + return FSP_ERR_WIFI_FAILED; + } + for (i = 0; i < count; i++) { ret = rm_wifi_onchip_silex_send_basic(p_instance_ctrl, @@ -1402,12 +1474,17 @@ fsp_err_t rm_wifi_onchip_silex_ping (uint8_t * p_ip_addr, uint32_t count, uint32 WIFI_ONCHIP_SILEX_RETURN_OK); if (FSP_SUCCESS != ret) { - rm_wifi_onchip_silex_send_basic_give_mutex(p_instance_ctrl, mutex_flag); - - return FSP_ERR_WIFI_FAILED; + break; } } + if (FSP_SUCCESS != ret) + { + rm_wifi_onchip_silex_send_basic_give_mutex(p_instance_ctrl, mutex_flag); + + return FSP_ERR_WIFI_FAILED; + } + rm_wifi_onchip_silex_send_basic_give_mutex(p_instance_ctrl, mutex_flag); return FSP_SUCCESS; @@ -1451,46 +1528,56 @@ fsp_err_t rm_wifi_onchip_silex_ip_addr_get (uint8_t * p_ip_addr) return FSP_ERR_WIFI_FAILED; } + /* Return an error if a socket is connected */ + if ((1 == p_instance_ctrl->num_uarts) && + (p_instance_ctrl->sockets[0].socket_status == WIFI_ONCHIP_SILEX_SOCKET_STATUS_CONNECTED)) + { + rm_wifi_onchip_silex_send_basic_give_mutex(p_instance_ctrl, mutex_flag); + + return FSP_ERR_WIFI_FAILED; + } + ret = rm_wifi_onchip_silex_send_basic(p_instance_ctrl, p_instance_ctrl->curr_cmd_port, "ATNSET?\r", WIFI_ONCHIP_SILEX_TIMEOUT_30MS, WIFI_ONCHIP_SILEX_TIMEOUT_5SEC, WIFI_ONCHIP_SILEX_RETURN_OK); - if (FSP_SUCCESS != ret) - { - rm_wifi_onchip_silex_send_basic_give_mutex(p_instance_ctrl, mutex_flag); - return FSP_ERR_WIFI_FAILED; - } - - if ((buff[0] == '\n') && (buff[1] == '\0')) + if (FSP_SUCCESS == ret) { - buff += 2; - } + if ((buff[0] == '\n') && (buff[1] == '\0')) + { + buff += 2; + } - // NOLINTNEXTLINE(cert-err34-c) Disable warning about the use of sscanf - err = sscanf((const char *) buff, "IP:%u.%u.%u.%u,", &temp_addr[0], &temp_addr[1], &temp_addr[2], &temp_addr[3]); + err = - if (4 != err) - { - rm_wifi_onchip_silex_send_basic_give_mutex(p_instance_ctrl, mutex_flag); + // NOLINTNEXTLINE(cert-err34-c) Disable warning about the use of sscanf + sscanf((const char *) buff, "IP:%u.%u.%u.%u,", &temp_addr[0], &temp_addr[1], &temp_addr[2], &temp_addr[3]); - return FSP_ERR_WIFI_FAILED; + if (4 == err) + { + for (i = 0; i < 4; i++) + { + if (temp_addr[i] <= UINT8_MAX) + { + p_ip_addr[i] = (uint8_t) temp_addr[i]; + } + else + { + ret = FSP_ERR_WIFI_FAILED; + break; + } + } + } } - for (i = 0; i < 4; i++) + if (FSP_SUCCESS != ret) { - if (temp_addr[i] <= UINT8_MAX) - { - p_ip_addr[i] = (uint8_t) temp_addr[i]; - } - else - { - rm_wifi_onchip_silex_send_basic_give_mutex(p_instance_ctrl, mutex_flag); + rm_wifi_onchip_silex_send_basic_give_mutex(p_instance_ctrl, mutex_flag); - return FSP_ERR_WIFI_FAILED; - } + return FSP_ERR_WIFI_FAILED; } rm_wifi_onchip_silex_send_basic_give_mutex(p_instance_ctrl, mutex_flag); @@ -1780,7 +1867,7 @@ int32_t rm_wifi_onchip_silex_tcp_send (uint32_t socket_no, const uint8_t * p_dat FSP_ERROR_RETURN(WIFI_OPEN == p_instance_ctrl->open, FSP_ERR_NOT_OPEN); #endif - // if socket write has been disabled by shutdown call then return 0 bytes sent. + /* if socket write has been disabled by shutdown call then return 0 bytes sent. */ if (!(p_instance_ctrl->sockets[socket_no].socket_read_write_flag & WIFI_ONCHIP_SILEX_SOCKET_WRITE)) { return 0; @@ -1887,13 +1974,13 @@ int32_t rm_wifi_onchip_silex_tcp_recv (uint32_t socket_no, uint8_t * p_data, uin FSP_ERROR_RETURN(WIFI_OPEN == p_instance_ctrl->open, FSP_ERR_NOT_OPEN); #endif - // if socket read has been disabled by shutdown call then return 0 bytes received. + /* if socket read has been disabled by shutdown call then return 0 bytes received. */ if (!(p_instance_ctrl->sockets[socket_no].socket_read_write_flag & WIFI_ONCHIP_SILEX_SOCKET_READ)) { return 0; } - /*Take the receive mutex */ + /* Take the receive mutex */ mutex_flag = (WIFI_ONCHIP_SILEX_MUTEX_RX); FSP_ERROR_RETURN(FSP_SUCCESS == rm_wifi_onchip_silex_send_basic_take_mutex(p_instance_ctrl, mutex_flag), WIFI_ONCHIP_SILEX_ERR_ERROR); @@ -2080,7 +2167,8 @@ fsp_err_t rm_wifi_onchip_silex_socket_disconnect (uint32_t socket_no) } /* If only one UART is used then send escape sequence command */ - if (p_instance_ctrl->num_uarts == 1) + if ((1 == p_instance_ctrl->num_uarts) && + (p_instance_ctrl->sockets[0].socket_status == WIFI_ONCHIP_SILEX_SOCKET_STATUS_CONNECTED)) { rm_wifi_onchip_silex_send_basic(p_instance_ctrl, p_instance_ctrl->curr_cmd_port, @@ -2169,6 +2257,15 @@ fsp_err_t rm_wifi_onchip_silex_dns_query (const char * p_textstring, uint8_t * p sprintf((char *) p_instance_ctrl->cmd_tx_buff + strlen((char *) p_instance_ctrl->cmd_tx_buff), "%s\r", p_textstring); + /* Return an error if a socket is connected */ + if ((1 == p_instance_ctrl->num_uarts) && + (p_instance_ctrl->sockets[0].socket_status == WIFI_ONCHIP_SILEX_SOCKET_STATUS_CONNECTED)) + { + rm_wifi_onchip_silex_send_basic_give_mutex(p_instance_ctrl, mutex_flag); + + return FSP_ERR_WIFI_FAILED; + } + func_ret = rm_wifi_onchip_silex_send_basic(p_instance_ctrl, p_instance_ctrl->curr_cmd_port, (char *) p_instance_ctrl->cmd_tx_buff, @@ -2176,43 +2273,45 @@ fsp_err_t rm_wifi_onchip_silex_dns_query (const char * p_textstring, uint8_t * p WIFI_ONCHIP_SILEX_TIMEOUT_3SEC, WIFI_ONCHIP_SILEX_RETURN_OK); - if (FSP_SUCCESS != func_ret) + if (FSP_SUCCESS == func_ret) { - rm_wifi_onchip_silex_send_basic_give_mutex(p_instance_ctrl, mutex_flag); + // NOLINTNEXTLINE(cert-err34-c) Disable warning about the use of sscanf + scanf_ret = sscanf((const char *) p_instance_ctrl->cmd_rx_buff, + "%u\r\n%u.%u.%u.%u\r\n", + &result, + &temp_addr[0], + &temp_addr[1], + &temp_addr[2], + &temp_addr[3]); - return FSP_ERR_WIFI_FAILED; + if ((scanf_ret == 5) && (result == 1)) + { + for (i = 0; i < 4; i++) + { + if (temp_addr[i] <= UINT8_MAX) + { + p_ip_addr[i] = (uint8_t) temp_addr[i]; + } + else + { + func_ret = FSP_ERR_WIFI_FAILED; + break; + } + } + } + else + { + func_ret = FSP_ERR_WIFI_FAILED; + } } - // NOLINTNEXTLINE(cert-err34-c) Disable warning about the use of sscanf - scanf_ret = sscanf((const char *) p_instance_ctrl->cmd_rx_buff, - "%u\r\n%u.%u.%u.%u\r\n", - &result, - &temp_addr[0], - &temp_addr[1], - &temp_addr[2], - &temp_addr[3]); - - if ((scanf_ret != 5) || (result != 1)) + if (FSP_SUCCESS != func_ret) { rm_wifi_onchip_silex_send_basic_give_mutex(p_instance_ctrl, mutex_flag); return FSP_ERR_WIFI_FAILED; } - for (i = 0; i < 4; i++) - { - if (temp_addr[i] <= UINT8_MAX) - { - p_ip_addr[i] = (uint8_t) temp_addr[i]; - } - else - { - rm_wifi_onchip_silex_send_basic_give_mutex(p_instance_ctrl, mutex_flag); - - return FSP_ERR_WIFI_FAILED; - } - } - rm_wifi_onchip_silex_send_basic_give_mutex(p_instance_ctrl, mutex_flag); return FSP_SUCCESS; @@ -2576,6 +2675,104 @@ fsp_err_t rm_wifi_onchip_silex_send_basic (wifi_onchip_silex_instance_ctrl_t * p return FSP_SUCCESS; } +/*******************************************************************************************************************//** + * Send and receive wifi scan command. + * + * @param[in] p_instance_ctrl Pointer to array holding URL to query from DNS. + * @param[in] serial_ch_id Uart channel ID. + * @param[in] send_scan_cmd Enable or disable the AT command transmit. + * @param[in] byte_timeout Timeout value between bytes received. + * @param[in] timeout_ms Timeout value before first byte received. + * + * @retval FSP_SUCCESS Function completed successfully. + * @retval FSP_ERR_WIFI_FAILED Error occurred with command to Wifi module. + * @retval FSP_ERR_WIFI_SCAN_COMPLETE All scan data have been received from the module. + * + **********************************************************************************************************************/ +static fsp_err_t rm_wifi_onchip_silex_send_scan (wifi_onchip_silex_instance_ctrl_t * p_instance_ctrl, + uint32_t serial_ch_id, + bool send_scan_cmd, + uint32_t byte_timeout, + uint32_t timeout_ms) +{ + fsp_err_t err; + uint32_t recvcnt = 0; + uint8_t receive_data; + + memset(&p_instance_ctrl->cmd_rx_buff, 0, sizeof(p_instance_ctrl->cmd_rx_buff)); + + if (true == send_scan_cmd) + { + recvcnt = 0; + + if (uxQueueMessagesWaiting((QueueHandle_t) p_instance_ctrl->uart_state_info[serial_ch_id].uart_tei_sem) != + 0) + { + return FSP_ERR_WIFI_FAILED; + } + + err = + p_instance_ctrl->uart_instance_objects[serial_ch_id]->p_api->write(p_instance_ctrl->uart_instance_objects[ + serial_ch_id]->p_ctrl, + (uint8_t *) "ATWS\r", + 5); + + FSP_ERROR_RETURN(FSP_SUCCESS == err, FSP_ERR_WIFI_FAILED); + + FSP_ERROR_RETURN(pdTRUE == + xSemaphoreTake(p_instance_ctrl->uart_state_info[serial_ch_id].uart_tei_sem, + (timeout_ms / portTICK_PERIOD_MS)), + FSP_ERR_WIFI_FAILED); + } + + /* Detect the first transmitted byte from the message */ + size_t xReceivedBytes = xStreamBufferReceiveAlternate(p_instance_ctrl->socket_byteq_hdl, + &receive_data, + 1, + pdMS_TO_TICKS(timeout_ms)); + if (xReceivedBytes == 1) + { + p_instance_ctrl->cmd_rx_buff[recvcnt++] = receive_data; + + while (xReceivedBytes == 1) + { + xReceivedBytes = xStreamBufferReceiveAlternate(p_instance_ctrl->socket_byteq_hdl, + &receive_data, + 1, + pdMS_TO_TICKS(byte_timeout)); + + if (xReceivedBytes == 1) + { + p_instance_ctrl->cmd_rx_buff[recvcnt++] = receive_data; + + /* Return success if a access point has been returned. */ + if ((recvcnt >= 4) && + (0 == strncmp((const char *) &p_instance_ctrl->cmd_rx_buff[recvcnt - 4], "\r\n\r\n", 4))) + { + return FSP_SUCCESS; + } + + /* Return success if the last access point has been returned. */ + if ((recvcnt >= 3) && + (0 == strncmp((const char *) &p_instance_ctrl->cmd_rx_buff[recvcnt - 3], "\r\n0", 3))) + { + return FSP_ERR_WIFI_SCAN_COMPLETE; + } + } + else + { + return FSP_ERR_WIFI_FAILED; + } + } + } + else + { + return FSP_ERR_WIFI_FAILED; + } + + return FSP_SUCCESS; +} + /*******************************************************************************************************************//** * Change the socket index. *