From de4f71649de316b13517139a6b191300b7545f97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B8=A9=E8=80=81=E5=B8=88?= <1161625498@qq.com> Date: Wed, 14 May 2025 10:59:13 +0800 Subject: [PATCH 1/5] add demo --- vendor/yibaina_3061M/demo/10025_FOC/readme.md | 20 + .../10025_FOC/user/generatecode/feature.h | 94 ++ .../demo/10025_FOC/user/generatecode/main.h | 98 ++ .../10025_FOC/user/generatecode/system_init.c | 677 +++++++++++ .../user/main.c} | 136 ++- .../inc/mcs_carrier.h | 142 +++ .../inc/mcs_chip_config.h | 76 ++ .../inc/mcs_ctlmode_config.h | 73 ++ .../inc/mcs_motor_process.h} | 75 +- .../inc/mcs_status.h | 188 +++ .../inc/mcs_user_config.h | 121 ++ .../protection/common/mcs_prot_cmm.c | 43 + .../protection/common/mcs_prot_cmm.h | 136 +++ .../protection/common/mcs_prot_user.c | 137 +++ .../protection/common/mcs_prot_user.h | 54 + .../protection/common/mcs_prot_user_config.h | 125 ++ .../protection/mcs_curr_prot.c | 267 +++++ .../protection/mcs_curr_prot.h | 52 + .../protection/mcs_dc_volt_prot.c | 495 ++++++++ .../protection/mcs_dc_volt_prot.h | 69 ++ .../protection/mcs_motor_stalling.c | 112 ++ .../protection/mcs_motor_stalling.h | 45 + .../protection/mcs_temp_prot.c | 262 +++++ .../protection/mcs_temp_prot.h | 49 + .../user/pmsm_sensorless_2shunt_foc/readme.md | 10 + .../src/mcs_carrier.c | 144 +++ .../src/mcs_motor_process.c | 950 +++++++++++++++ .../user_interface/cust_process.c | 718 +++++++++++ .../user_interface/cust_process.h | 36 + .../user_interface/hmi_module.c | 57 + .../user_interface/hmi_module.h | 37 + .../user_interface/protocol.c | 174 +++ .../user_interface/protocol.h | 121 ++ .../user_interface/uart_module.c | 184 +++ .../user_interface/uart_module.h | 47 + .../demo/10025_Six_Step_Wave/readme.md | 20 + .../user/generatecode/feature.h | 94 ++ .../user/generatecode/main.h | 98 ++ .../user/generatecode/system_init.c | 677 +++++++++++ .../demo/10025_Six_Step_Wave/user/main.c | 71 ++ .../inc/mcs_carrier.h | 142 +++ .../inc/mcs_chip_config.h | 76 ++ .../inc/mcs_ctlmode_config.h | 73 ++ .../inc/mcs_motor_process.h | 45 + .../inc/mcs_status.h | 188 +++ .../inc/mcs_user_config.h | 108 ++ .../protection/common/mcs_prot_cmm.c | 43 + .../protection/common/mcs_prot_cmm.h | 136 +++ .../protection/common/mcs_prot_user.c | 137 +++ .../protection/common/mcs_prot_user.h | 54 + .../protection/common/mcs_prot_user_config.h | 125 ++ .../protection/mcs_curr_prot.c | 267 +++++ .../protection/mcs_curr_prot.h | 52 + .../protection/mcs_dc_volt_prot.c | 495 ++++++++ .../protection/mcs_dc_volt_prot.h | 69 ++ .../protection/mcs_motor_stalling.c | 112 ++ .../protection/mcs_motor_stalling.h | 45 + .../protection/mcs_temp_prot.c | 262 +++++ .../protection/mcs_temp_prot.h | 49 + .../user/pmsm_sensorless_2shunt_foc/readme.md | 10 + .../src/mcs_carrier.c | 144 +++ .../src/mcs_motor_process.c | 950 +++++++++++++++ .../user_interface/cust_process.c | 718 +++++++++++ .../user_interface/cust_process.h | 36 + .../user_interface/hmi_module.c | 57 + .../user_interface/hmi_module.h | 37 + .../user_interface/protocol.c | 174 +++ .../user_interface/protocol.h | 121 ++ .../user_interface/uart_module.c | 184 +++ .../user_interface/uart_module.h | 47 + vendor/yibaina_3061M/demo/E7050_FOC/readme.md | 20 + .../E7050_FOC/user/generatecode/feature.h | 94 ++ .../demo/E7050_FOC/user/generatecode/main.h | 98 ++ .../E7050_FOC/user/generatecode/system_init.c | 677 +++++++++++ .../yibaina_3061M/demo/E7050_FOC/user/main.c | 71 ++ .../inc/mcs_carrier.h | 142 +++ .../inc/mcs_chip_config.h | 76 ++ .../inc/mcs_ctlmode_config.h | 73 ++ .../inc/mcs_motor_process.h | 45 + .../inc/mcs_status.h | 188 +++ .../inc/mcs_user_config.h | 108 ++ .../protection/common/mcs_prot_cmm.c | 43 + .../protection/common/mcs_prot_cmm.h | 136 +++ .../protection/common/mcs_prot_user.c | 137 +++ .../protection/common/mcs_prot_user.h | 54 + .../protection/common/mcs_prot_user_config.h | 125 ++ .../protection/mcs_curr_prot.c | 267 +++++ .../protection/mcs_curr_prot.h | 52 + .../protection/mcs_dc_volt_prot.c | 495 ++++++++ .../protection/mcs_dc_volt_prot.h | 69 ++ .../protection/mcs_motor_stalling.c | 112 ++ .../protection/mcs_motor_stalling.h | 45 + .../protection/mcs_temp_prot.c | 262 +++++ .../protection/mcs_temp_prot.h | 49 + .../user/pmsm_sensorless_2shunt_foc/readme.md | 10 + .../src/mcs_carrier.c | 144 +++ .../src/mcs_motor_process.c | 950 +++++++++++++++ .../user_interface/cust_process.c | 718 +++++++++++ .../user_interface/cust_process.h | 36 + .../user_interface/hmi_module.c | 57 + .../user_interface/hmi_module.h | 37 + .../user_interface/protocol.c | 174 +++ .../user_interface/protocol.h | 121 ++ .../user_interface/uart_module.c | 184 +++ .../user_interface/uart_module.h | 47 + .../demo/E7050_Six_Step_Wave/readme.md | 20 + .../user/generatecode/feature.h | 94 ++ .../user}/generatecode/main.h | 27 +- .../user/generatecode/system_init.c | 530 +++++++++ .../user}/main.c | 12 +- .../application/mcs_motor_process.c | 562 +++++++++ .../application/mcs_motor_process.h | 47 + .../application/mcs_user_config.h | 94 ++ .../common/mcs_carrier.c | 224 ++++ .../common/mcs_carrier.h | 91 ++ .../common/mcs_fsm.h | 55 + .../func/mcs_six_step.c | 126 ++ .../func/mcs_six_step.h | 79 ++ .../func/mcs_status.h | 188 +++ .../demo/GIM4310-10_FOC/readme.md | 20 + .../user/generatecode/feature.h | 94 ++ .../GIM4310-10_FOC/user/generatecode/main.h | 98 ++ .../user/generatecode/system_init.c | 677 +++++++++++ .../demo/GIM4310-10_FOC/user/main.c | 71 ++ .../inc/mcs_carrier.h | 142 +++ .../inc/mcs_chip_config.h | 76 ++ .../inc/mcs_ctlmode_config.h | 73 ++ .../inc/mcs_motor_process.h | 45 + .../inc/mcs_status.h | 188 +++ .../inc/mcs_user_config.h | 120 ++ .../protection/common/mcs_prot_cmm.c | 43 + .../protection/common/mcs_prot_cmm.h | 136 +++ .../protection/common/mcs_prot_user.c | 137 +++ .../protection/common/mcs_prot_user.h | 54 + .../protection/common/mcs_prot_user_config.h | 125 ++ .../protection/mcs_curr_prot.c | 267 +++++ .../protection/mcs_curr_prot.h | 52 + .../protection/mcs_dc_volt_prot.c | 495 ++++++++ .../protection/mcs_dc_volt_prot.h | 69 ++ .../protection/mcs_motor_stalling.c | 112 ++ .../protection/mcs_motor_stalling.h | 45 + .../protection/mcs_temp_prot.c | 262 +++++ .../protection/mcs_temp_prot.h | 49 + .../user/pmsm_sensorless_2shunt_foc/readme.md | 10 + .../src/mcs_carrier.c | 144 +++ .../src/mcs_motor_process.c | 951 +++++++++++++++ .../user_interface/cust_process.c | 718 +++++++++++ .../user_interface/cust_process.h | 36 + .../user_interface/hmi_module.c | 57 + .../user_interface/hmi_module.h | 37 + .../user_interface/protocol.c | 174 +++ .../user_interface/protocol.h | 121 ++ .../user_interface/uart_module.c | 184 +++ .../user_interface/uart_module.h | 47 + .../demo/GIM4310-10_Six_Step_Wave/readme.md | 20 + .../user/generatecode/feature.h | 94 ++ .../user/generatecode/main.h | 84 ++ .../user/generatecode/system_init.c | 530 +++++++++ .../demo/GIM4310-10_Six_Step_Wave/user/main.c | 67 ++ .../application/mcs_motor_process.c | 562 +++++++++ .../application/mcs_motor_process.h | 47 + .../application/mcs_user_config.h | 94 ++ .../common/mcs_carrier.c | 224 ++++ .../common/mcs_carrier.h | 91 ++ .../common/mcs_fsm.h | 55 + .../func/mcs_six_step.c | 126 ++ .../func/mcs_six_step.h | 79 ++ .../func/mcs_status.h | 188 +++ .../demo/pmsm_pos_qdm_foc/readme.md | 22 + .../user}/generatecode/feature.h | 0 .../pmsm_pos_qdm_foc/user/generatecode/main.h | 101 ++ .../user/generatecode/system_init.c | 775 ++++++++++++ .../demo/pmsm_pos_qdm_foc/user/main.c | 81 ++ .../inc/mcs_carrier.h | 153 +++ .../inc/mcs_chip_config.h | 81 ++ .../inc/mcs_ctlmode_config.h | 75 ++ .../inc/mcs_ex_common.h | 78 ++ .../inc/mcs_inc_enc.h | 126 ++ .../inc/mcs_motor_process.h | 45 + .../inc/mcs_status.h | 188 +++ .../inc/mcs_user_config.h | 143 +++ .../protection/common/mcs_prot_cmm.c | 43 + .../protection/common/mcs_prot_cmm.h | 136 +++ .../protection/common/mcs_prot_user.c | 137 +++ .../protection/common/mcs_prot_user.h | 54 + .../protection/common/mcs_prot_user_config.h | 130 ++ .../protection/mcs_curr_prot.c | 267 +++++ .../protection/mcs_curr_prot.h | 52 + .../protection/mcs_dc_volt_prot.c | 495 ++++++++ .../protection/mcs_dc_volt_prot.h | 69 ++ .../protection/mcs_motor_stalling.c | 112 ++ .../protection/mcs_motor_stalling.h | 45 + .../protection/mcs_temp_prot.c | 262 +++++ .../protection/mcs_temp_prot.h | 49 + .../user/pmsm_encode_qdm_2shunt_foc/readme.md | 12 + .../src/mcs_carrier.c | 136 +++ .../src/mcs_inc_enc.c | 236 ++++ .../src/mcs_motor_process.c | 1047 +++++++++++++++++ .../user_interface/cust_process.c | 639 ++++++++++ .../user_interface/cust_process.h | 36 + .../user_interface/hmi_module.c | 58 + .../user_interface/hmi_module.h | 37 + .../user_interface/protocol.c | 174 +++ .../user_interface/protocol.h | 121 ++ .../user_interface/uart_module.c | 234 ++++ .../user_interface/uart_module.h | 48 + .../generatecode/system_init.c | 124 -- .../demo/sample_gpio_key/readme.md | 78 -- 208 files changed, 34594 insertions(+), 300 deletions(-) create mode 100644 vendor/yibaina_3061M/demo/10025_FOC/readme.md create mode 100644 vendor/yibaina_3061M/demo/10025_FOC/user/generatecode/feature.h create mode 100644 vendor/yibaina_3061M/demo/10025_FOC/user/generatecode/main.h create mode 100644 vendor/yibaina_3061M/demo/10025_FOC/user/generatecode/system_init.c rename vendor/yibaina_3061M/demo/{sample_gpio_key/gpio_key_sample.c => 10025_FOC/user/main.c} (55%) create mode 100644 vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_carrier.h create mode 100644 vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_chip_config.h create mode 100644 vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_ctlmode_config.h rename vendor/yibaina_3061M/demo/{sample_gpio_key/gpio_key_sample.h => 10025_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_motor_process.h} (76%) create mode 100644 vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_status.h create mode 100644 vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_user_config.h create mode 100644 vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_cmm.c create mode 100644 vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_cmm.h create mode 100644 vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user.c create mode 100644 vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user.h create mode 100644 vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user_config.h create mode 100644 vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_curr_prot.c create mode 100644 vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_curr_prot.h create mode 100644 vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_dc_volt_prot.c create mode 100644 vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_dc_volt_prot.h create mode 100644 vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_motor_stalling.c create mode 100644 vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_motor_stalling.h create mode 100644 vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_temp_prot.c create mode 100644 vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_temp_prot.h create mode 100644 vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/readme.md create mode 100644 vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/src/mcs_carrier.c create mode 100644 vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/src/mcs_motor_process.c create mode 100644 vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/cust_process.c create mode 100644 vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/cust_process.h create mode 100644 vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/hmi_module.c create mode 100644 vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/hmi_module.h create mode 100644 vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/protocol.c create mode 100644 vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/protocol.h create mode 100644 vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/uart_module.c create mode 100644 vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/uart_module.h create mode 100644 vendor/yibaina_3061M/demo/10025_Six_Step_Wave/readme.md create mode 100644 vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/generatecode/feature.h create mode 100644 vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/generatecode/main.h create mode 100644 vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/generatecode/system_init.c create mode 100644 vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/main.c create mode 100644 vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/inc/mcs_carrier.h create mode 100644 vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/inc/mcs_chip_config.h create mode 100644 vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/inc/mcs_ctlmode_config.h create mode 100644 vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/inc/mcs_motor_process.h create mode 100644 vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/inc/mcs_status.h create mode 100644 vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/inc/mcs_user_config.h create mode 100644 vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_cmm.c create mode 100644 vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_cmm.h create mode 100644 vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user.c create mode 100644 vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user.h create mode 100644 vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user_config.h create mode 100644 vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/mcs_curr_prot.c create mode 100644 vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/mcs_curr_prot.h create mode 100644 vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/mcs_dc_volt_prot.c create mode 100644 vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/mcs_dc_volt_prot.h create mode 100644 vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/mcs_motor_stalling.c create mode 100644 vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/mcs_motor_stalling.h create mode 100644 vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/mcs_temp_prot.c create mode 100644 vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/mcs_temp_prot.h create mode 100644 vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/readme.md create mode 100644 vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/src/mcs_carrier.c create mode 100644 vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/src/mcs_motor_process.c create mode 100644 vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/user_interface/cust_process.c create mode 100644 vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/user_interface/cust_process.h create mode 100644 vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/user_interface/hmi_module.c create mode 100644 vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/user_interface/hmi_module.h create mode 100644 vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/user_interface/protocol.c create mode 100644 vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/user_interface/protocol.h create mode 100644 vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/user_interface/uart_module.c create mode 100644 vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/user_interface/uart_module.h create mode 100644 vendor/yibaina_3061M/demo/E7050_FOC/readme.md create mode 100644 vendor/yibaina_3061M/demo/E7050_FOC/user/generatecode/feature.h create mode 100644 vendor/yibaina_3061M/demo/E7050_FOC/user/generatecode/main.h create mode 100644 vendor/yibaina_3061M/demo/E7050_FOC/user/generatecode/system_init.c create mode 100644 vendor/yibaina_3061M/demo/E7050_FOC/user/main.c create mode 100644 vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_carrier.h create mode 100644 vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_chip_config.h create mode 100644 vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_ctlmode_config.h create mode 100644 vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_motor_process.h create mode 100644 vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_status.h create mode 100644 vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_user_config.h create mode 100644 vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_cmm.c create mode 100644 vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_cmm.h create mode 100644 vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user.c create mode 100644 vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user.h create mode 100644 vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user_config.h create mode 100644 vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_curr_prot.c create mode 100644 vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_curr_prot.h create mode 100644 vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_dc_volt_prot.c create mode 100644 vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_dc_volt_prot.h create mode 100644 vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_motor_stalling.c create mode 100644 vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_motor_stalling.h create mode 100644 vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_temp_prot.c create mode 100644 vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_temp_prot.h create mode 100644 vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/readme.md create mode 100644 vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/src/mcs_carrier.c create mode 100644 vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/src/mcs_motor_process.c create mode 100644 vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/cust_process.c create mode 100644 vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/cust_process.h create mode 100644 vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/hmi_module.c create mode 100644 vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/hmi_module.h create mode 100644 vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/protocol.c create mode 100644 vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/protocol.h create mode 100644 vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/uart_module.c create mode 100644 vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/uart_module.h create mode 100644 vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/readme.md create mode 100644 vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/generatecode/feature.h rename vendor/yibaina_3061M/demo/{sample_gpio_key => E7050_Six_Step_Wave/user}/generatecode/main.h (78%) create mode 100644 vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/generatecode/system_init.c rename vendor/yibaina_3061M/demo/{sample_gpio_key => E7050_Six_Step_Wave/user}/main.c (92%) create mode 100644 vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/application/mcs_motor_process.c create mode 100644 vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/application/mcs_motor_process.h create mode 100644 vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/application/mcs_user_config.h create mode 100644 vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/common/mcs_carrier.c create mode 100644 vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/common/mcs_carrier.h create mode 100644 vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/common/mcs_fsm.h create mode 100644 vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/func/mcs_six_step.c create mode 100644 vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/func/mcs_six_step.h create mode 100644 vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/func/mcs_status.h create mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_FOC/readme.md create mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/generatecode/feature.h create mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/generatecode/main.h create mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/generatecode/system_init.c create mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/main.c create mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_carrier.h create mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_chip_config.h create mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_ctlmode_config.h create mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_motor_process.h create mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_status.h create mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_user_config.h create mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_cmm.c create mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_cmm.h create mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user.c create mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user.h create mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user_config.h create mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_curr_prot.c create mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_curr_prot.h create mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_dc_volt_prot.c create mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_dc_volt_prot.h create mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_motor_stalling.c create mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_motor_stalling.h create mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_temp_prot.c create mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_temp_prot.h create mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/readme.md create mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/src/mcs_carrier.c create mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/src/mcs_motor_process.c create mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/cust_process.c create mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/cust_process.h create mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/hmi_module.c create mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/hmi_module.h create mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/protocol.c create mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/protocol.h create mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/uart_module.c create mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/uart_module.h create mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/readme.md create mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/generatecode/feature.h create mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/generatecode/main.h create mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/generatecode/system_init.c create mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/main.c create mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/application/mcs_motor_process.c create mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/application/mcs_motor_process.h create mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/application/mcs_user_config.h create mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/common/mcs_carrier.c create mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/common/mcs_carrier.h create mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/common/mcs_fsm.h create mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/func/mcs_six_step.c create mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/func/mcs_six_step.h create mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/func/mcs_status.h create mode 100644 vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/readme.md rename vendor/yibaina_3061M/demo/{sample_gpio_key => pmsm_pos_qdm_foc/user}/generatecode/feature.h (100%) create mode 100644 vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/generatecode/main.h create mode 100644 vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/generatecode/system_init.c create mode 100644 vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/main.c create mode 100644 vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/inc/mcs_carrier.h create mode 100644 vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/inc/mcs_chip_config.h create mode 100644 vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/inc/mcs_ctlmode_config.h create mode 100644 vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/inc/mcs_ex_common.h create mode 100644 vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/inc/mcs_inc_enc.h create mode 100644 vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/inc/mcs_motor_process.h create mode 100644 vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/inc/mcs_status.h create mode 100644 vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/inc/mcs_user_config.h create mode 100644 vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/common/mcs_prot_cmm.c create mode 100644 vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/common/mcs_prot_cmm.h create mode 100644 vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/common/mcs_prot_user.c create mode 100644 vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/common/mcs_prot_user.h create mode 100644 vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/common/mcs_prot_user_config.h create mode 100644 vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/mcs_curr_prot.c create mode 100644 vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/mcs_curr_prot.h create mode 100644 vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/mcs_dc_volt_prot.c create mode 100644 vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/mcs_dc_volt_prot.h create mode 100644 vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/mcs_motor_stalling.c create mode 100644 vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/mcs_motor_stalling.h create mode 100644 vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/mcs_temp_prot.c create mode 100644 vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/mcs_temp_prot.h create mode 100644 vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/readme.md create mode 100644 vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/src/mcs_carrier.c create mode 100644 vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/src/mcs_inc_enc.c create mode 100644 vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/src/mcs_motor_process.c create mode 100644 vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/user_interface/cust_process.c create mode 100644 vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/user_interface/cust_process.h create mode 100644 vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/user_interface/hmi_module.c create mode 100644 vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/user_interface/hmi_module.h create mode 100644 vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/user_interface/protocol.c create mode 100644 vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/user_interface/protocol.h create mode 100644 vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/user_interface/uart_module.c create mode 100644 vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/user_interface/uart_module.h delete mode 100644 vendor/yibaina_3061M/demo/sample_gpio_key/generatecode/system_init.c delete mode 100644 vendor/yibaina_3061M/demo/sample_gpio_key/readme.md diff --git a/vendor/yibaina_3061M/demo/10025_FOC/readme.md b/vendor/yibaina_3061M/demo/10025_FOC/readme.md new file mode 100644 index 00000000..0085a9e2 --- /dev/null +++ b/vendor/yibaina_3061M/demo/10025_FOC/readme.md @@ -0,0 +1,20 @@ +# 10025_foc + +**【功能描述】** + +- 基于ECMCU105H/ECBMCU201MPC单板的单电机双电阻采样的无感Foc应用 + +**【环境要求】** + +- 所有单板电源改制为演示用的24V低压,电机选用PM120(10025) +- 电机(黄绿蓝)相线分别对应功率板上U/V/W通道接口 + +**【配置方法】** + +- 将user文件夹替换原工程user文件夹即可 + +【操作说明】 + +- 点击开发板按键启动电机 + +- 开发板蓝色电位器,可以调节电机转动的快慢。(1-29HZ) \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_FOC/user/generatecode/feature.h b/vendor/yibaina_3061M/demo/10025_FOC/user/generatecode/feature.h new file mode 100644 index 00000000..a18d8c21 --- /dev/null +++ b/vendor/yibaina_3061M/demo/10025_FOC/user/generatecode/feature.h @@ -0,0 +1,94 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file feature.h + * @author MCU Driver Team + * @brief This file contains macro configurations related to the project. This file is generated by the IDE tool. + * @date 2025-04-17 14:22:06 + */ + +#ifndef McuMagicTag_FEATURE_H +#define McuMagicTag_FEATURE_H + +/* Macro definitions --------------------------------------------------------- */ +#define CHIP_3061MNPICA MACRO_ENABLE + +#define MACRO_ENABLE 1 +#define MACRO_DISABLE 0 + +/* Macro switch */ +#define BASE_DEFINE_USE_ASSERT MACRO_ENABLE +#ifndef FLASH_CRC_CONFIG +#define FLASH_CRC_CONFIG +#endif /* #ifndef FLASH_CRC_CONFIG */ +#define BASE_MATH_SINCOS_MIDDLE_TABLE MACRO_ENABLE /**< This macro is used to control the table type when the + BASE_MATH_GetSinCos() queries the table. When the value of + this macro is MACRO_ENABLE, the error value obtained by the + BASE_MATH_GetSinCos() is relatively small, and the return + value of the function may be greater than or less than the + actual value. When the value of this macro is MACRO_DISABLE, + the error value obtained by the BASE_MATH_GetSinCos() is + relatively large. However, in the range [0°, 180°) and + [180°, 360°), the return value of the function is either + greater than or less than the actual value. */ + +/* Peripheral module macro switch--------------------------------------------- */ +#define BOARD_DIM_NUM 1 /**< Number of dimming handle arrays. */ + +#define BOARD_KEY_NUM 10 /**< Number of key handle arrays. */ +#define BOARD_KEY_PRESS_ON GPIO_HIGH_LEVEL /**< GPIO status corresponding to long press valid. */ +#define BOARD_KEY_PRESS_OFF GPIO_LOW_LEVEL /**< GPIO status corresponding to short press valid. */ + +#define BOARD_LED_SEG_NUM 4 /**< Number of segments. */ +#define BOARD_LED_SEGMENT_ON GPIO_HIGH_LEVEL /**< GPIO level status corresponding to valid segments. */ +#define BOARD_LED_SEGMENT_OFF GPIO_LOW_LEVEL /**< GPIO level status corresponding to invalid segments. */ + +#define BOARD_MKEY_SCHEME_NUMBER BOARD_MKEY_SCHEME_NUMBER_ONE /**< Define the scheme to be adopted. */ +#define BOARD_MKEY_OUT_NUM 4 /**< Number of GPIO pins used as output during scanning. */ +#define BOARD_MKEY_IN_NUM 4 /**< Number of GPIO pins used as input during scanning. */ +#define BOARD_MKEY_OUT_PIN_VALID GPIO_LOW_LEVEL /**< GPIO level status corresponding to the valid \ + status of the output GPIO in the key matrix. */ +#define BOARD_MKEY_OUT_PIN_INVALID GPIO_HIGH_LEVEL /**< GPIO level status corresponding to the \ + invalid status of the output GPIO in the key matrix. */ +#define BOARD_MKEY_IN_PIN_VALID GPIO_LOW_LEVEL /**< Indicates the GPIO level corresponding to the \ + valid status of the input GPIO in the key matrix. */ +#define BOARD_MKEY_IN_PIN_INVALID GPIO_HIGH_LEVEL /**< Indicates the GPIO level corresponding to the \ + invalid status of the input GPIO in the key matrix. */ + +#define BOARD_PULSES_NUM 2 /**< Number of pulse handles. */ + +#define BASE_DEFINE_SLIPAVERAGE_NUM 2 /**< Sliding average array length. */ + +#define LISTNODE_MAX 20 + +#define BASE_DEFINE_DMA_QUICKSTART + +#define XTRAIL_FREQ 30000000U + +#define DBG_USE_NO_PRINTF 0U +#define DBG_USE_UART_PRINTF 1U + +#define DBG_PRINTF_USE DBG_USE_UART_PRINTF +#if (DBG_PRINTF_USE == DBG_USE_UART_PRINTF) +#define DBG_PRINTF_UART_PORT UART0 +#endif + +/* USER CODE BEGIN 0 */ +/* USER CODE 区域内代码不会被覆盖,区域外会被生成的默认代码覆盖(其余USER CODE 区域同理) */ +/* USER CODE END 0 */ + +#endif /* McuMagicTag_FEATURE_H */ \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_FOC/user/generatecode/main.h b/vendor/yibaina_3061M/demo/10025_FOC/user/generatecode/main.h new file mode 100644 index 00000000..1c8caefb --- /dev/null +++ b/vendor/yibaina_3061M/demo/10025_FOC/user/generatecode/main.h @@ -0,0 +1,98 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file main.h + * @author MCU Driver Team + * @brief This file contains driver init functions. + * @date 2025-04-17 14:22:06 + */ + +/* Define to prevent recursive inclusion ------------------------------------- */ +#ifndef McuMagicTag_SYSTEM_INIT_H +#define McuMagicTag_SYSTEM_INIT_H + +#include "adc.h" +#include "adc_ex.h" +#include "acmp.h" +#include "acmp_ex.h" +#include "apt.h" +#include "uart.h" +#include "uart_ex.h" +#include "gpio.h" +#include "timer.h" +#include "timer_ex.h" +#include "pga.h" +#include "crg.h" +#include "dma.h" +#include "dma_ex.h" +#include "iocmg.h" + +#define IO_SPEED_FAST 0x00U +#define IO_SPEED_SLOW 0x01U + +#define IO_DRV_LEVEL4 0x00U +#define IO_DRV_LEVEL3 0x01U +#define IO_DRV_LEVEL2 0x02U +#define IO_DRV_LEVEL1 0x03U + +#define XTAL_DRV_LEVEL4 0x03U +#define XTAL_DRV_LEVEL3 0x02U +#define XTAL_DRV_LEVEL2 0x01U +#define XTAL_DRV_LEVEL1 0x00U + +#define GpioStartStop_PIN GPIO_PIN_4 +#define GpioStartStop_HANDLE g_gpio2 + +extern ACMP_Handle g_acmp0; +extern PGA_Handle g_pga0; +extern PGA_Handle g_pga1; +extern TIMER_Handle g_timer0; +extern TIMER_Handle g_timer1; +extern UART_Handle g_uart0; +extern APT_Handle g_apt0; +extern APT_Handle g_apt1; +extern APT_Handle g_apt2; +extern ADC_Handle g_adc0; + +extern DMA_Handle g_dmac; + +extern GPIO_Handle g_gpio2; +extern GPIO_Handle g_gpio1; + +BASE_StatusType CRG_Config(CRG_CoreClkSelect *coreClkSelect); +void SystemInit(void); + +void UART0WriteInterruptCallback(void *handle); +void UART0ReadInterruptCallback(void *handle); + +void UART0InterruptErrorCallback(void *handle); +void MotorStatemachineCallBack(void *handle); +void TIMER1_DMAOverFlow_InterruptProcess(void *handle); +void CheckPotentiometerValueCallback(void *handle); +void TIMER0_DMAOverFlow_InterruptProcess(void *handle); +void MotorCarrierProcessCallback(void *aptHandle); +void MotorSysErrCallback(void *aptHandle); + +void UART0_TXDMACallback(void *handle); + +void MotorStartStopKeyCallback(void *param); + +/* USER CODE BEGIN 0 */ +/* USER CODE 区域内代码不会被覆盖,区域外会被生成的默认代码覆盖(其余USER CODE 区域同理) */ +/* USER CODE END 0 */ + +#endif /* McuMagicTag_SYSTEM_INIT_H */ \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_FOC/user/generatecode/system_init.c b/vendor/yibaina_3061M/demo/10025_FOC/user/generatecode/system_init.c new file mode 100644 index 00000000..3c5e593c --- /dev/null +++ b/vendor/yibaina_3061M/demo/10025_FOC/user/generatecode/system_init.c @@ -0,0 +1,677 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file system_init.c + * @author MCU Driver Team + * @brief This file contains driver init functions. + * @date 2025-04-17 14:22:06 + */ + +#include "main.h" +#include "ioconfig.h" +#include "iocmg_ip.h" + +#define UART0_BAUD_RATE 1843200 + +BASE_StatusType CRG_Config(CRG_CoreClkSelect *coreClkSelect) +{ + CRG_Handle crg; + crg.baseAddress = CRG; + crg.pllRefClkSelect = CRG_PLL_REF_CLK_SELECT_HOSC; + crg.pllPreDiv = CRG_PLL_PREDIV_4; + crg.pllFbDiv = 48; /* PLL Multiplier 48 */ + crg.pllPostDiv = CRG_PLL_POSTDIV_2; + crg.coreClkSelect = CRG_CORE_CLK_SELECT_PLL; + crg.handleEx.pllPostDiv2 = CRG_PLL_POSTDIV2_3; + crg.handleEx.clk1MSelect = CRG_1M_CLK_SELECT_HOSC; + crg.handleEx.clk1MDiv = (25 - 1); /* The 1 MHz freq is equal to the input clock frequency / (clk_1m_div + 1). 25 is the div of the clk_1m in CLOCK. */ + + if (HAL_CRG_Init(&crg) != BASE_STATUS_OK) { + return BASE_STATUS_ERROR; + } + *coreClkSelect = crg.coreClkSelect; + return BASE_STATUS_OK; +} + +static void DMA_Channel0Init(void *handle) +{ + DMA_ChannelParam dma_param; + dma_param.direction = DMA_MEMORY_TO_PERIPH_BY_DMAC; + dma_param.srcAddrInc = DMA_ADDR_INCREASE; + dma_param.destAddrInc = DMA_ADDR_UNALTERED; + dma_param.srcPeriph = DMA_REQUEST_MEM; + dma_param.destPeriph = DMA_REQUEST_UART0_TX; + dma_param.srcWidth = DMA_TRANSWIDTH_BYTE; + dma_param.destWidth = DMA_TRANSWIDTH_BYTE; + dma_param.srcBurst = DMA_BURST_LENGTH_1; + dma_param.destBurst = DMA_BURST_LENGTH_1; + dma_param.pHandle = handle; + HAL_DMA_InitChannel(&g_dmac, &dma_param, DMA_CHANNEL_ZERO); +} + +static void DMA_Init(void) +{ + HAL_CRG_IpEnableSet(DMA_BASE, IP_CLK_ENABLE); + g_dmac.baseAddress = DMA; + IRQ_Register(IRQ_DMA_TC, HAL_DMA_IrqHandlerTc, &g_dmac); + IRQ_Register(IRQ_DMA_ERR, HAL_DMA_IrqHandlerError, &g_dmac); + IRQ_EnableN(IRQ_DMA_TC); + IRQ_EnableN(IRQ_DMA_ERR); + HAL_DMA_Init(&g_dmac); + DMA_Channel0Init((void *)(&g_uart0)); + HAL_DMA_SetChannelPriorityEx(&g_dmac, DMA_CHANNEL_ZERO, DMA_PRIORITY_HIGHEST); +} + +static void ACMP0_Init(void) +{ + HAL_CRG_IpEnableSet(ACMP0_BASE, IP_CLK_ENABLE); /* ACMP clock bit reset. */ + g_acmp0.baseAddress = ACMP0_BASE; + g_acmp0.inOutConfig.inputNNum = ACMP_INPUT_N_SELECT2; + g_acmp0.inOutConfig.inputPNum = ACMP_INPUT_P_SELECT2; + g_acmp0.inOutConfig.polarity = ACMP_OUT_NOT_INVERT; + g_acmp0.filterCtrl.filterMode = ACMP_FILTER_NONE; + g_acmp0.hysteresisVol = ACMP_HYS_VOL_30MV; + g_acmp0.interruptEn = BASE_CFG_UNSET; + HAL_ACMP_Init(&g_acmp0); +} + +static void ADC0_Init(void) +{ + HAL_CRG_IpEnableSet(ADC0_BASE, IP_CLK_ENABLE); + HAL_CRG_IpClkSelectSet(ADC0_BASE, CRG_ADC_CLK_ASYN_PLL_DIV); + HAL_CRG_IpClkDivSet(ADC0_BASE, CRG_ADC_DIV_1); + + g_adc0.baseAddress = ADC0; + g_adc0.socPriority = ADC_PRIMODE_ALL_ROUND; + + HAL_ADC_Init(&g_adc0); + + SOC_Param socParam = {0}; + socParam.adcInput = ADC_CH_ADCINA0; /* PGA0_OUT(ADC AIN0) */ + socParam.sampleTotalTime = ADC_SOCSAMPLE_22CLK; /* adc sample total time 22 adc_clk */ + socParam.trigSource = ADC_TRIGSOC_APT0_SOCA; + socParam.continueMode = BASE_CFG_DISABLE; + socParam.finishMode = ADC_SOCFINISH_NONE; + HAL_ADC_ConfigureSoc(&g_adc0, ADC_SOC_NUM1, &socParam); + + socParam.adcInput = ADC_CH_ADCINA1; /* PGA1_OUT(ADC AIN1) */ + socParam.sampleTotalTime = ADC_SOCSAMPLE_22CLK; /* adc sample total time 22 adc_clk */ + socParam.trigSource = ADC_TRIGSOC_APT0_SOCA; + socParam.continueMode = BASE_CFG_DISABLE; + socParam.finishMode = ADC_SOCFINISH_NONE; + HAL_ADC_ConfigureSoc(&g_adc0, ADC_SOC_NUM6, &socParam); + + socParam.adcInput = ADC_CH_ADCINA9; /* PIN7(ADC AIN9) */ + socParam.sampleTotalTime = ADC_SOCSAMPLE_22CLK; /* adc sample total time 22 adc_clk */ + socParam.trigSource = ADC_TRIGSOC_SOFT; + socParam.continueMode = BASE_CFG_DISABLE; + socParam.finishMode = ADC_SOCFINISH_NONE; + HAL_ADC_ConfigureSoc(&g_adc0, ADC_SOC_NUM0, &socParam); + + socParam.adcInput = ADC_CH_ADCINA5; /* PIN2(ADC AIN5) */ + socParam.sampleTotalTime = ADC_SOCSAMPLE_22CLK; /* adc sample total time 22 adc_clk */ + socParam.trigSource = ADC_TRIGSOC_SOFT; + socParam.continueMode = BASE_CFG_DISABLE; + socParam.finishMode = ADC_SOCFINISH_NONE; + HAL_ADC_ConfigureSoc(&g_adc0, ADC_SOC_NUM3, &socParam); + + socParam.adcInput = ADC_CH_ADCINA15; /* PIN14(ADC AIN15) */ + socParam.sampleTotalTime = ADC_SOCSAMPLE_22CLK; /* adc sample total time 22 adc_clk */ + socParam.trigSource = ADC_TRIGSOC_SOFT; + socParam.continueMode = BASE_CFG_DISABLE; + socParam.finishMode = ADC_SOCFINISH_NONE; + HAL_ADC_ConfigureSoc(&g_adc0, ADC_SOC_NUM4, &socParam); +} + +__weak void MotorSysErrCallback(void *aptHandle) +{ + BASE_FUNC_UNUSED(aptHandle); + /* USER CODE BEGIN APT0_EVENT_INTERRUPT */ + /* USER CODE END APT0_EVENT_INTERRUPT */ +} + +__weak void MotorCarrierProcessCallback(void *aptHandle) +{ + BASE_FUNC_UNUSED(aptHandle); + /* USER CODE BEGIN APT0_TIMER_INTERRUPT */ + /* USER CODE END APT0_TIMER_INTERRUPT */ +} + +static void APT0_ProtectInit(void) +{ + APT_OutCtrlProtectEx protectApt = {0}; + protectApt.ocEventEnEx = BASE_CFG_ENABLE; + protectApt.ocEventModeEx = APT_OUT_CTRL_ONE_SHOT; + protectApt.ocActionEx = APT_OUT_CTRL_ACTION_LOW; + protectApt.ocActionBEx = APT_OUT_CTRL_ACTION_LOW; + protectApt.ocEvtInterruptEnEx = BASE_CFG_ENABLE; + protectApt.ocSysEvent = APT_SYS_EVT_DEBUG | APT_SYS_EVT_CLK | APT_SYS_EVT_MEM; + protectApt.originalEvtEx = APT_EM_ORIGINAL_SRC_ACMP0; + protectApt.evtPolarityMaskEx = APT_EM_ACMP0_INVERT_BIT; + protectApt.filterCycleNumEx = 0; + HAL_APT_ProtectInitEx(&g_apt0, &protectApt); +} + +static void APT0_Init(void) +{ + HAL_CRG_IpEnableSet(APT0_BASE, IP_CLK_ENABLE); + + g_apt0.baseAddress = APT0; + + /* Clock Settings */ + g_apt0.waveform.dividerFactor = 1 - 1; + /* Timer Settings */ + g_apt0.waveform.timerPeriod = 7500; /* 7500 is count period of APT time-base timer */ + g_apt0.waveform.cntMode = APT_COUNT_MODE_UP_DOWN; + + /* Wave Form */ + g_apt0.waveform.basicType = APT_PWM_BASIC_A_HIGH_B_LOW; + g_apt0.waveform.chAOutType = APT_PWM_OUT_BASIC_TYPE; + g_apt0.waveform.chBOutType = APT_PWM_OUT_BASIC_TYPE; + g_apt0.waveform.divInitVal = 0; + g_apt0.waveform.cntInitVal = 0; + g_apt0.waveform.cntCmpLeftEdge = 500; /* 500 is count compare point of the left edge of PWM waveform */ + g_apt0.waveform.cntCmpRightEdge = 4000; /* 4000 is count compare point of the right edge of PWM waveform */ + g_apt0.waveform.cntCmpLoadMode = APT_BUFFER_INDEPENDENT_LOAD; + g_apt0.waveform.cntCmpLoadEvt = APT_COMPARE_LOAD_EVENT_ZERO; + g_apt0.waveform.deadBandCnt = 225; /* 225 is dead-band value */ + + /* ADC Trigger SOCA */ + g_apt0.adcTrg.trgEnSOCA = BASE_CFG_ENABLE; + g_apt0.adcTrg.cntCmpSOCA = 375; /* 375 is count compare point of ADC trigger source SOCA when using CMPA */ + g_apt0.adcTrg.trgSrcSOCA = APT_CS_SRC_CNTR_CMPA_DOWN; + g_apt0.adcTrg.trgScaleSOCA = 1; + + /* ADC Trigger SOCB */ + g_apt0.adcTrg.trgEnSOCB = BASE_CFG_ENABLE; + g_apt0.adcTrg.cntCmpSOCB = 1; + g_apt0.adcTrg.trgSrcSOCB = APT_CS_SRC_CNTR_CMPB_DOWN; + g_apt0.adcTrg.trgScaleSOCB = 1; + + g_apt0.adcTrg.cntCmpLoadMode = APT_BUFFER_INDEPENDENT_LOAD; + g_apt0.adcTrg.cntCmpLoadEvt = APT_COMPARE_LOAD_EVENT_ZERO; + + /* Timer Trigger */ + g_apt0.tmrInterrupt.tmrInterruptEn = BASE_CFG_ENABLE; + g_apt0.tmrInterrupt.tmrInterruptSrc = APT_INT_SRC_CNTR_ZERO; + g_apt0.tmrInterrupt.tmrInterruptScale = 1; + + APT0_ProtectInit(); + + HAL_APT_PWMInit(&g_apt0); + HAL_APT_RegisterCallBack(&g_apt0, APT_EVENT_INTERRUPT, MotorSysErrCallback); + IRQ_SetPriority(IRQ_APT0_EVT, 7); /* 7 is priority value */ + IRQ_Register(IRQ_APT0_EVT, HAL_APT_EventIrqHandler, &g_apt0); + IRQ_EnableN(IRQ_APT0_EVT); + HAL_APT_RegisterCallBack(&g_apt0, APT_TIMER_INTERRUPT, MotorCarrierProcessCallback); + IRQ_SetPriority(IRQ_APT0_TMR, 5); /* 5 is priority value */ + IRQ_Register(IRQ_APT0_TMR, HAL_APT_TimerIrqHandler, &g_apt0); + IRQ_EnableN(IRQ_APT0_TMR); +} + +static void APT1_ProtectInit(void) +{ + APT_OutCtrlProtectEx protectApt = {0}; + protectApt.ocEventEnEx = BASE_CFG_ENABLE; + protectApt.ocEventModeEx = APT_OUT_CTRL_ONE_SHOT; + protectApt.ocActionEx = APT_OUT_CTRL_ACTION_LOW; + protectApt.ocActionBEx = APT_OUT_CTRL_ACTION_LOW; + protectApt.ocEvtInterruptEnEx = BASE_CFG_DISABLE; + protectApt.ocSysEvent = APT_SYS_EVT_DEBUG | APT_SYS_EVT_CLK | APT_SYS_EVT_MEM; + protectApt.originalEvtEx = APT_EM_ORIGINAL_SRC_ACMP0; + protectApt.evtPolarityMaskEx = APT_EM_ACMP0_INVERT_BIT; + protectApt.filterCycleNumEx = 0; + HAL_APT_ProtectInitEx(&g_apt1, &protectApt); +} + +static void APT1_Init(void) +{ + HAL_CRG_IpEnableSet(APT1_BASE, IP_CLK_ENABLE); + + g_apt1.baseAddress = APT1; + + /* Clock Settings */ + g_apt1.waveform.dividerFactor = 1 - 1; + /* Timer Settings */ + g_apt1.waveform.timerPeriod = 7500; /* 7500 is count period of APT time-base timer */ + g_apt1.waveform.cntMode = APT_COUNT_MODE_UP_DOWN; + + /* Wave Form */ + g_apt1.waveform.basicType = APT_PWM_BASIC_A_HIGH_B_LOW; + g_apt1.waveform.chAOutType = APT_PWM_OUT_BASIC_TYPE; + g_apt1.waveform.chBOutType = APT_PWM_OUT_BASIC_TYPE; + g_apt1.waveform.divInitVal = 0; + g_apt1.waveform.cntInitVal = 0; + g_apt1.waveform.cntCmpLeftEdge = 500; /* 500 is count compare point of the left edge of PWM waveform */ + g_apt1.waveform.cntCmpRightEdge = 4000; /* 4000 is count compare point of the right edge of PWM waveform */ + g_apt1.waveform.cntCmpLoadMode = APT_BUFFER_INDEPENDENT_LOAD; + g_apt1.waveform.cntCmpLoadEvt = APT_COMPARE_LOAD_EVENT_ZERO; + g_apt1.waveform.deadBandCnt = 225; /* 225 is dead-band value */ + + APT1_ProtectInit(); + + HAL_APT_PWMInit(&g_apt1); +} + +static void APT2_ProtectInit(void) +{ + APT_OutCtrlProtectEx protectApt = {0}; + protectApt.ocEventEnEx = BASE_CFG_ENABLE; + protectApt.ocEventModeEx = APT_OUT_CTRL_ONE_SHOT; + protectApt.ocActionEx = APT_OUT_CTRL_ACTION_LOW; + protectApt.ocActionBEx = APT_OUT_CTRL_ACTION_LOW; + protectApt.ocEvtInterruptEnEx = BASE_CFG_DISABLE; + protectApt.ocSysEvent = APT_SYS_EVT_DEBUG | APT_SYS_EVT_CLK | APT_SYS_EVT_MEM; + protectApt.originalEvtEx = APT_EM_ORIGINAL_SRC_ACMP0; + protectApt.evtPolarityMaskEx = APT_EM_ACMP0_INVERT_BIT; + protectApt.filterCycleNumEx = 0; + HAL_APT_ProtectInitEx(&g_apt2, &protectApt); +} + +static void APT2_Init(void) +{ + HAL_CRG_IpEnableSet(APT2_BASE, IP_CLK_ENABLE); + + g_apt2.baseAddress = APT2; + + /* Clock Settings */ + g_apt2.waveform.dividerFactor = 1 - 1; + /* Timer Settings */ + g_apt2.waveform.timerPeriod = 7500; /* 7500 is count period of APT time-base timer */ + g_apt2.waveform.cntMode = APT_COUNT_MODE_UP_DOWN; + + /* Wave Form */ + g_apt2.waveform.basicType = APT_PWM_BASIC_A_HIGH_B_LOW; + g_apt2.waveform.chAOutType = APT_PWM_OUT_BASIC_TYPE; + g_apt2.waveform.chBOutType = APT_PWM_OUT_BASIC_TYPE; + g_apt2.waveform.divInitVal = 0; + g_apt2.waveform.cntInitVal = 0; + g_apt2.waveform.cntCmpLeftEdge = 500; /* 500 is count compare point of the left edge of PWM waveform */ + g_apt2.waveform.cntCmpRightEdge = 4000; /* 4000 is count compare point of the right edge of PWM waveform */ + g_apt2.waveform.cntCmpLoadMode = APT_BUFFER_INDEPENDENT_LOAD; + g_apt2.waveform.cntCmpLoadEvt = APT_COMPARE_LOAD_EVENT_ZERO; + g_apt2.waveform.deadBandCnt = 225; /* 225 is dead-band value */ + + APT2_ProtectInit(); + + HAL_APT_PWMInit(&g_apt2); +} + +__weak void MotorStartStopKeyCallback(void *param) +{ + GPIO_Handle *handle = (GPIO_Handle *)param; + BASE_FUNC_UNUSED(handle); +} + +static void GPIO_Init(void) +{ + HAL_CRG_IpEnableSet(GPIO2_BASE, IP_CLK_ENABLE); + g_gpio2.baseAddress = GPIO2; + + g_gpio2.pins = GPIO_PIN_3; + HAL_GPIO_Init(&g_gpio2); + HAL_GPIO_SetDirection(&g_gpio2, g_gpio2.pins, GPIO_OUTPUT_MODE); + HAL_GPIO_SetValue(&g_gpio2, g_gpio2.pins, GPIO_HIGH_LEVEL); + HAL_GPIO_SetIrqType(&g_gpio2, g_gpio2.pins, GPIO_INT_TYPE_NONE); + + g_gpio2.pins = GPIO_PIN_4; + HAL_GPIO_Init(&g_gpio2); + HAL_GPIO_SetDirection(&g_gpio2, g_gpio2.pins, GPIO_INPUT_MODE); + HAL_GPIO_SetValue(&g_gpio2, g_gpio2.pins, GPIO_HIGH_LEVEL); + HAL_GPIO_SetIrqType(&g_gpio2, g_gpio2.pins, GPIO_INT_TYPE_LOW_LEVEL); + + HAL_CRG_IpEnableSet(GPIO1_BASE, IP_CLK_ENABLE); + g_gpio1.baseAddress = GPIO1; + + g_gpio1.pins = GPIO_PIN_0; + HAL_GPIO_Init(&g_gpio1); + HAL_GPIO_SetDirection(&g_gpio1, g_gpio1.pins, GPIO_OUTPUT_MODE); + HAL_GPIO_SetValue(&g_gpio1, g_gpio1.pins, GPIO_HIGH_LEVEL); + HAL_GPIO_SetIrqType(&g_gpio1, g_gpio1.pins, GPIO_INT_TYPE_NONE); + + HAL_GPIO_RegisterCallBack(&g_gpio2, GPIO_PIN_4, MotorStartStopKeyCallback); + IRQ_Register(IRQ_GPIO2, HAL_GPIO_IrqHandler, &g_gpio2); + IRQ_SetPriority(IRQ_GPIO2, 1); /* set gpio1 interrupt priority to 1, 1~15. 1 is priority value */ + IRQ_EnableN(IRQ_GPIO2); /* gpio interrupt enable */ + + return; +} + +static void PGA0_Init(void) +{ + HAL_CRG_IpEnableSet(PGA0_BASE, IP_CLK_ENABLE); + + g_pga0.baseAddress = PGA0_BASE; + g_pga0.externalResistorMode = BASE_CFG_ENABLE; + g_pga0.handleEx.extCapCompensation = PGA_EXT_COMPENSATION_2X; + HAL_PGA_Init(&g_pga0); +} + +static void PGA1_Init(void) +{ + HAL_CRG_IpEnableSet(PGA1_BASE, IP_CLK_ENABLE); + + g_pga1.baseAddress = PGA1_BASE; + g_pga1.externalResistorMode = BASE_CFG_ENABLE; + g_pga1.handleEx.extCapCompensation = PGA_EXT_COMPENSATION_2X; + HAL_PGA_Init(&g_pga1); +} + +__weak void CheckPotentiometerValueCallback(void *handle) +{ + BASE_FUNC_UNUSED(handle); + /* USER CODE BEGIN CheckPotentiometerValueCallback */ + /* USER CODE END CheckPotentiometerValueCallback */ +} + +static void TIMER0_Init(void) +{ + HAL_CRG_IpEnableSet(TIMER0_BASE, IP_CLK_ENABLE); /* TIMER0 clock enable. */ + unsigned int load = ((float)HAL_CRG_GetIpFreq((void *)TIMER0) / (1u << (TIMERPRESCALER_NO_DIV * 4)) / 1000000u) * 1000000; + + g_timer0.baseAddress = TIMER0; + g_timer0.load = (load < 1) ? 0 : (load - 1); /* Set timer value immediately */ + g_timer0.bgLoad = (load < 1) ? 0 : (load - 1); /* Set timer value */ + g_timer0.mode = TIMER_MODE_RUN_PERIODIC; /* Run in period mode */ + g_timer0.prescaler = TIMERPRESCALER_NO_DIV; /* Don't frequency division */ + g_timer0.size = TIMER_SIZE_32BIT; /* 1 for 32bit, 0 for 16bit */ + g_timer0.interruptEn = BASE_CFG_ENABLE; + g_timer0.adcSocReqEnable = BASE_CFG_DISABLE; + g_timer0.dmaReqEnable = BASE_CFG_DISABLE; + HAL_TIMER_Init(&g_timer0); + IRQ_Register(IRQ_TIMER0, HAL_TIMER_IrqHandler, &g_timer0); + + HAL_TIMER_RegisterCallback(&g_timer0, TIMER_PERIOD_FIN, CheckPotentiometerValueCallback); + IRQ_SetPriority(IRQ_TIMER0, 1); /* 1 is priority value */ + IRQ_EnableN(IRQ_TIMER0); +} + +__weak void MotorStatemachineCallBack(void *handle) +{ + BASE_FUNC_UNUSED(handle); + /* USER CODE BEGIN MotorStatemachineCallBack */ + /* USER CODE END MotorStatemachineCallBack */ +} + +static void TIMER1_Init(void) +{ + HAL_CRG_IpEnableSet(TIMER1_BASE, IP_CLK_ENABLE); /* TIMER1 clock enable. */ + unsigned int load = ((float)HAL_CRG_GetIpFreq((void *)TIMER1) / (1u << (TIMERPRESCALER_NO_DIV * 4)) / 1000000u) * 500; + + g_timer1.baseAddress = TIMER1; + g_timer1.load = (load < 1) ? 0 : (load - 1); /* Set timer value immediately */ + g_timer1.bgLoad = (load < 1) ? 0 : (load - 1); /* Set timer value */ + g_timer1.mode = TIMER_MODE_RUN_PERIODIC; /* Run in period mode */ + g_timer1.prescaler = TIMERPRESCALER_NO_DIV; /* Don't frequency division */ + g_timer1.size = TIMER_SIZE_32BIT; /* 1 for 32bit, 0 for 16bit */ + g_timer1.interruptEn = BASE_CFG_ENABLE; + g_timer1.adcSocReqEnable = BASE_CFG_DISABLE; + g_timer1.dmaReqEnable = BASE_CFG_DISABLE; + HAL_TIMER_Init(&g_timer1); + IRQ_Register(IRQ_TIMER1, HAL_TIMER_IrqHandler, &g_timer1); + + HAL_TIMER_RegisterCallback(&g_timer1, TIMER_PERIOD_FIN, MotorStatemachineCallBack); + IRQ_SetPriority(IRQ_TIMER1, 1); /* 1 is priority value */ + IRQ_EnableN(IRQ_TIMER1); +} + +__weak void UART0InterruptErrorCallback(void *handle) +{ + BASE_FUNC_UNUSED(handle); + /* USER CODE BEGIN UART0_TRNS_IT_ERROR */ + /* USER CODE END UART0_TRNS_IT_ERROR */ +} + +__weak void UART0_TXDMACallback(void *handle) +{ + BASE_FUNC_UNUSED(handle); + /* USER CODE BEGIN UART0_WRITE_DMA_FINISH */ + /* USER CODE END UART0_WRITE_DMA_FINISH */ +} + +__weak void UART0WriteInterruptCallback(void *handle) +{ + BASE_FUNC_UNUSED(handle); + /* USER CODE BEGIN UART0_WRITE_IT_FINISH */ + /* USER CODE END UART0_WRITE_IT_FINISH */ +} + +__weak void UART0ReadInterruptCallback(void *handle) +{ + BASE_FUNC_UNUSED(handle); + /* USER CODE BEGIN UART0_READ_IT_FINISH */ + /* USER CODE END UART0_READ_IT_FINISH */ +} + +static void UART0_Init(void) +{ + HAL_CRG_IpEnableSet(UART0_BASE, IP_CLK_ENABLE); /* UART0 clock enable. */ + g_uart0.baseAddress = UART0; + + g_uart0.baudRate = UART0_BAUD_RATE; + g_uart0.dataLength = UART_DATALENGTH_8BIT; + g_uart0.stopBits = UART_STOPBITS_ONE; + g_uart0.parity = UART_PARITY_NONE; + g_uart0.txMode = UART_MODE_DMA; + g_uart0.rxMode = UART_MODE_INTERRUPT; + g_uart0.fifoMode = BASE_CFG_ENABLE; + g_uart0.fifoTxThr = UART_FIFODEPTH_SIZE8; + g_uart0.fifoRxThr = UART_FIFODEPTH_SIZE8; + g_uart0.hwFlowCtr = BASE_CFG_DISABLE; + g_uart0.handleEx.overSampleMultiple = UART_OVERSAMPLING_16X; + g_uart0.handleEx.msbFirst = BASE_CFG_DISABLE; + HAL_UART_Init(&g_uart0); + HAL_UART_RegisterCallBack(&g_uart0, UART_TRNS_IT_ERROR, (UART_CallbackType)UART0InterruptErrorCallback); + HAL_UART_RegisterCallBack(&g_uart0, UART_READ_IT_FINISH, (UART_CallbackType)UART0ReadInterruptCallback); + IRQ_Register(IRQ_UART0, HAL_UART_IrqHandler, &g_uart0); + IRQ_SetPriority(IRQ_UART0, 6); /* 6 is priority value */ + IRQ_EnableN(IRQ_UART0); + g_uart0.dmaHandle = &g_dmac; + g_uart0.uartDmaTxChn = 0; + HAL_UART_RegisterCallBack(&g_uart0, UART_WRITE_DMA_FINISH, (UART_CallbackType)UART0_TXDMACallback); + HAL_UART_RegisterCallBack(&g_uart0, UART_WRITE_IT_FINISH, (UART_CallbackType)UART0WriteInterruptCallback); +} + +static void IOConfig(void) +{ + SYSCTRL0->SC_SYS_STAT.BIT.update_mode = 0; + SYSCTRL0->SC_SYS_STAT.BIT.update_mode_clear = 1; + /* Config PIN39 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO0_3_AS_UART0_TXD); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO0_3_AS_UART0_TXD, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO0_3_AS_UART0_TXD, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO0_3_AS_UART0_TXD, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO0_3_AS_UART0_TXD, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN40 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO0_4_AS_UART0_RXD); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO0_4_AS_UART0_RXD, PULL_UP); /* Pull-up and Pull-down, UART RX recommend PULL_UP */ + HAL_IOCMG_SetPinSchmidtMode(GPIO0_4_AS_UART0_RXD, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO0_4_AS_UART0_RXD, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO0_4_AS_UART0_RXD, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN1 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO0_7_AS_ACMP0_OUT); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO0_7_AS_ACMP0_OUT, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO0_7_AS_ACMP0_OUT, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO0_7_AS_ACMP0_OUT, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO0_7_AS_ACMP0_OUT, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN48 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO0_6_AS_ACMP_N2); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO0_6_AS_ACMP_N2, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO0_6_AS_ACMP_N2, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO0_6_AS_ACMP_N2, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO0_6_AS_ACMP_N2, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN47 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO0_5_AS_ACMP_P2); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO0_5_AS_ACMP_P2, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO0_5_AS_ACMP_P2, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO0_5_AS_ACMP_P2, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO0_5_AS_ACMP_P2, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN4 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO2_6_AS_PGA0_N0); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO2_6_AS_PGA0_N0, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO2_6_AS_PGA0_N0, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO2_6_AS_PGA0_N0, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO2_6_AS_PGA0_N0, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN5 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO2_5_AS_PGA0_P0); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO2_5_AS_PGA0_P0, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO2_5_AS_PGA0_P0, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO2_5_AS_PGA0_P0, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO2_5_AS_PGA0_P0, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN3 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO2_7_AS_PGA0_OUT); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO2_7_AS_PGA0_OUT, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO2_7_AS_PGA0_OUT, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO2_7_AS_PGA0_OUT, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO2_7_AS_PGA0_OUT, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN7 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO3_7_AS_ADC_AIN9); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO3_7_AS_ADC_AIN9, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO3_7_AS_ADC_AIN9, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO3_7_AS_ADC_AIN9, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO3_7_AS_ADC_AIN9, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN2 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO5_1_AS_ADC_AIN5); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO5_1_AS_ADC_AIN5, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO5_1_AS_ADC_AIN5, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO5_1_AS_ADC_AIN5, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO5_1_AS_ADC_AIN5, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN14 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO4_7_AS_ADC_AIN15); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO4_7_AS_ADC_AIN15, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO4_7_AS_ADC_AIN15, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO4_7_AS_ADC_AIN15, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO4_7_AS_ADC_AIN15, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN11 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO1_5_AS_PGA1_P0); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO1_5_AS_PGA1_P0, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO1_5_AS_PGA1_P0, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO1_5_AS_PGA1_P0, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO1_5_AS_PGA1_P0, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN12 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO1_6_AS_PGA1_N0); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO1_6_AS_PGA1_N0, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO1_6_AS_PGA1_N0, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO1_6_AS_PGA1_N0, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO1_6_AS_PGA1_N0, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN13 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO1_7_AS_PGA1_OUT); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO1_7_AS_PGA1_OUT, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO1_7_AS_PGA1_OUT, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO1_7_AS_PGA1_OUT, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO1_7_AS_PGA1_OUT, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN35 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO2_3_AS_GPIO2_3); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO2_3_AS_GPIO2_3, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO2_3_AS_GPIO2_3, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO2_3_AS_GPIO2_3, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO2_3_AS_GPIO2_3, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN27 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO1_0_AS_GPIO1_0); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO1_0_AS_GPIO1_0, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO1_0_AS_GPIO1_0, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO1_0_AS_GPIO1_0, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO1_0_AS_GPIO1_0, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN41 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO2_4_AS_GPIO2_4); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO2_4_AS_GPIO2_4, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO2_4_AS_GPIO2_4, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO2_4_AS_GPIO2_4, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO2_4_AS_GPIO2_4, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN19 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO3_0_AS_APT0_PWMA); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO3_0_AS_APT0_PWMA, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO3_0_AS_APT0_PWMA, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO3_0_AS_APT0_PWMA, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO3_0_AS_APT0_PWMA, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN23 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO4_0_AS_APT0_PWMB); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO4_0_AS_APT0_PWMB, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO4_0_AS_APT0_PWMB, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO4_0_AS_APT0_PWMB, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO4_0_AS_APT0_PWMB, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN20 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO3_1_AS_APT1_PWMA); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO3_1_AS_APT1_PWMA, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO3_1_AS_APT1_PWMA, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO3_1_AS_APT1_PWMA, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO3_1_AS_APT1_PWMA, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN24 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO4_1_AS_APT1_PWMB); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO4_1_AS_APT1_PWMB, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO4_1_AS_APT1_PWMB, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO4_1_AS_APT1_PWMB, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO4_1_AS_APT1_PWMB, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN21 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO3_2_AS_APT2_PWMA); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO3_2_AS_APT2_PWMA, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO3_2_AS_APT2_PWMA, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO3_2_AS_APT2_PWMA, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO3_2_AS_APT2_PWMA, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN25 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO4_2_AS_APT2_PWMB); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO4_2_AS_APT2_PWMB, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO4_2_AS_APT2_PWMB, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO4_2_AS_APT2_PWMB, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO4_2_AS_APT2_PWMB, DRIVER_RATE_2); /* Output signal edge fast/slow */ +} + +static void APT_SyncMasterInit(void) +{ + HAL_APT_MasterSyncInit(&g_apt0, APT_SYNC_OUT_ON_CNTR_ZERO); +} + +static void APT_SyncSlaveInit(void) +{ + APT_SlaveSyncIn aptSlave; + + aptSlave.cntPhase = 0; /* counter phase value */ + aptSlave.syncCntMode = APT_COUNT_MODE_AFTER_SYNC_UP; + aptSlave.syncInSrc = APT_SYNCIN_SRC_APT0_SYNCOUT; /* sync source selection */ + aptSlave.cntrSyncSrc = APT_CNTR_SYNC_SRC_SYNCIN; + HAL_APT_SlaveSyncInit(&g_apt1, &aptSlave); + + aptSlave.cntPhase = 0; /* counter phase value */ + aptSlave.syncCntMode = APT_COUNT_MODE_AFTER_SYNC_UP; + aptSlave.syncInSrc = APT_SYNCIN_SRC_APT0_SYNCOUT; /* sync source selection */ + aptSlave.cntrSyncSrc = APT_CNTR_SYNC_SRC_SYNCIN; + HAL_APT_SlaveSyncInit(&g_apt2, &aptSlave); + +} + +void SystemInit(void) +{ + IOConfig(); + DMA_Init(); + UART0_Init(); + ACMP0_Init(); + APT0_Init(); + APT1_Init(); + APT2_Init(); + ADC0_Init(); + PGA0_Init(); + PGA1_Init(); + TIMER0_Init(); + TIMER1_Init(); + GPIO_Init(); + + APT_SyncMasterInit(); + APT_SyncSlaveInit(); + + /* USER CODE BEGIN system_init */ + /* USER CODE END system_init */ +} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/sample_gpio_key/gpio_key_sample.c b/vendor/yibaina_3061M/demo/10025_FOC/user/main.c similarity index 55% rename from vendor/yibaina_3061M/demo/sample_gpio_key/gpio_key_sample.c rename to vendor/yibaina_3061M/demo/10025_FOC/user/main.c index 83fc35d9..1dfa2fc3 100644 --- a/vendor/yibaina_3061M/demo/sample_gpio_key/gpio_key_sample.c +++ b/vendor/yibaina_3061M/demo/10025_FOC/user/main.c @@ -1,65 +1,71 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file gpio_key_sample.c - * @author MCU Driver Team - * @brief GPIO module realize a key function sample - * @details GPIO Capturing Key Triggered Interrupt Service Function. If the hardware environment does \ - * not support this function, you need to set up an environment for verification. - */ - -/* Includes ------------------------------------------------------------------*/ -#include "debug.h" -#include "gpio.h" -#include "main.h" -#include "gpio_key_sample.h" - -#define PREVENT_SWIPE_SCREEN_TIME 50 -#define CYCLE_INTERVAL_TIME 500 - -/* prototype functions -------------------------------------------------------*/ -void GPIO_CallBackFunc(void *param); - -/* ---------------------------------- Sample Parameters ---------------------- */ -/** - * @brief GPIO key test sample. - * @param None - * @retval Value of @ref BASE_StatusType. - */ -BASE_StatusType GPIO_KeySample(void) -{ - SystemInit(); - /* Waiting for the key to come. */ - while (1) { - DBG_PRINTF("Wait key \r\n"); - BASE_FUNC_DELAY_MS(CYCLE_INTERVAL_TIME); - } - - return BASE_STATUS_OK; -} - -/** - * @brief GPIO register interrupt callback function. - * @param param Value of @ref GPIO_Handle. - * @retval None - */ -void GPIO_CallBackFunc(void *param) -{ - BASE_FUNC_UNUSED(param); - DBG_PRINTF("in GPIO Key Handler \r\n"); - BASE_FUNC_DELAY_MS(PREVENT_SWIPE_SCREEN_TIME); -} \ No newline at end of file +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file main.c + * @author MCU Driver Team + * @brief Main program body. + * @date 2025-04-17 14:22:06 + */ + +#include "typedefs.h" +#include "feature.h" +#include "mcs_motor_process.h" +#include "main.h" +/* USER CODE BEGIN 0 */ +/* USER CODE 区域内代码不会被覆盖,区域外会被生成的默认代码覆盖(其余USER CODE 区域同理) */ +/* 建议用户放置头文件 */ +/* USER CODE END 0 */ +ACMP_Handle g_acmp0; +PGA_Handle g_pga0; +PGA_Handle g_pga1; +TIMER_Handle g_timer0; +TIMER_Handle g_timer1; +UART_Handle g_uart0; +APT_Handle g_apt0; +APT_Handle g_apt1; +APT_Handle g_apt2; +ADC_Handle g_adc0; +DMA_Handle g_dmac; +GPIO_Handle g_gpio2; +GPIO_Handle g_gpio1; +/* USER CODE BEGIN 1 */ +/* 建议用户定义全局变量、结构体、宏定义或函数声明等 */ +/* USER CODE END 1 */ + +int main(void) +{ + /* USER CODE BEGIN 2 */ + /* 建议用户放置初始化代码或启动代码等 */ + /* USER CODE END 2 */ + MotorMainProcess(); + /* USER CODE BEGIN 3 */ + /* 建议用户放置初始配置代码 */ + /* USER CODE END 3 */ + while (1) { + /* USER CODE BEGIN 4 */ + /* 建议用户放置周期性执行代码 */ + /* USER CODE END 4 */ + } + /* USER CODE BEGIN 5 */ + /* 建议用户放置代码流程 */ + /* USER CODE END 5 */ + return BASE_STATUS_OK; +} + +/* USER CODE BEGIN 6 */ +/* 建议用户放置自定义函数 */ +/* USER CODE END 6 */ \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_carrier.h b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_carrier.h new file mode 100644 index 00000000..dea3ad66 --- /dev/null +++ b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_carrier.h @@ -0,0 +1,142 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_carrier.h + * @author MCU Algorithm Team + * @brief This file provides functions declaration for carrier interrupt processing function. + */ +#ifndef McuMagicTag_MCS_CARRIER_H +#define McuMagicTag_MCS_CARRIER_H + +#include "mcs_status.h" +#include "mcs_mtr_param.h" +#include "mcs_svpwm.h" +#include "mcs_curr_ctrl.h" +#include "mcs_if_ctrl.h" +#include "mcs_ramp_mgmt.h" +#include "mcs_spd_ctrl.h" +#include "mcs_fosmo.h" +#include "mcs_smo_4th.h" +#include "mcs_pll.h" +#include "mcs_startup.h" +#include "mcs_r1_svpwm.h" +#include "mcs_fw_ctrl.h" +#include "mcs_prot_user.h" + +typedef void (*MCS_ReadCurrUvwCb)(UvwAxis *CurrUvw); +typedef void (*MCS_SetPwmDutyCb)(UvwAxis *dutyUvwLeft, UvwAxis *dutyUvwRight); +typedef void (*MCS_SetADCTriggerTimeCb)(unsigned short cntCmpSOCA, unsigned short cntCmpSOCB); + +/** + * @brief motor control FSM state define. + * @details motor control FSM state type: + * + FSM_IDLE -- IDLE state, system startup default. + * + FSM_OFFSET_CALIB -- Self calibrate, for ADC init. + * + FSM_CAP_CHARGE -- IPM cap charge. + * + FSM_CLEAR -- Clear before every run. + * + FSM_LOCATE -- Rotor position pre-locate. + * + FSM_STARTUP -- Start up. + * + FSM_SWITCH -- Transition state, control from open to closed loop. + * + FSM_RUN -- Normal running state. + * + FSM_WAIT_STOP -- Wait stopping. + * + FSM_STOP -- Normal stop. + * + FSM_FAULT -- Fault state, waiting for user process. + */ +typedef enum { + FSM_IDLE = 0, + FSM_OFFSET_CALIB, + FSM_CAP_CHARGE, + FSM_CLEAR, + FSM_LOCATE, + FSM_STARTUP, + FSM_SWITCH, + FSM_RUN, + FSM_WAIT_STOP, + FSM_STOP, + FSM_FAULT +} FsmState; + +/** + * @brief Sampling mode. + */ +typedef enum { + DUAL_RESISTORS = 0, + SINGLE_RESISTOR = 1 +} SampleMode; + +/** + * @brief Motor control data structure + */ +typedef struct { + unsigned char motorStateFlag; /**< Motor start/stop status flag. */ + float spdCmdHz; /**< External input speed command value. */ + float axisAngle; /**< Angle of the synchronous coordinate system. */ + float spdRefHz; /**< Command value after speed ramp management. */ + float currCtrlPeriod; /**< current loop control period. */ + float adc0Compensate; /**< ADC0 softwaretrim compensate value. */ + float adc1Compensate; /**< ADC1 softwaretrim compensate value. */ + float udc; /**< Bus voltage. */ + float powerBoardTemp; /**< Power boart surface temperature. */ + unsigned short aptMaxcntCmp; /**< Apt Maximum Comparison Count. */ + float adcCurrCofe; /**< Adc current sampling cofeature. */ + + unsigned short sysTickCnt; /**< System Timer Tick Count. */ + unsigned short capChargeTickNum; /**< Bootstrap Capacitor Charge Tick Count. */ + volatile unsigned int msTickCnt; /**< Millisecond-level counter, which can be used in 1-ms and 5-ms tasks. */ + unsigned short msTickNum; /**< Number of ticks corresponding to 1 ms. */ + char obserType; /**< Set Observer Type. */ + char controlMode; /**< Set foc control or sixstep bldc control mode or others. */ + char spdAdjustMode; /**< Set speed adjust mode. */ + char uartConnectFlag; /**< Uart connect success flag. */ + short uartHeartDetCnt; /**< Uart connect heart detect count. */ + float uartTimeStamp; /**< Uart data time stamp. */ + SysStatusReg statusReg; /**< System status. */ + FsmState stateMachine; /**< Motor Control State Machine. */ + + SampleMode sampleMode; /**< Current sampling mode setting: single resistor, double resistor. */ + MOTOR_Param mtrParam; /**< Motor parameters. */ + FOSMO_Handle smo; /**< SMO observer handle. */ + SMO4TH_Handle smo4th; /**< SMO 4th observer handle. */ + IF_Handle ifCtrl; /**< I/F control handle. */ + SVPWM_Handle sv; /**< Dual-resistance/three-resistance sampling SVPWM Handle. */ + R1SVPWM_Handle r1Sv; /**< Single-resistance phase-shifted SVPWM handle. */ + RMG_Handle spdRmg; /**< Ramp management struct for the speed controller input reference. */ + SPDCTRL_Handle spdCtrl; /**< Speed loop Control Handle. */ + CURRCTRL_Handle currCtrl; /**< Current loop control handle. */ + STARTUP_Handle startup; /**< Startup Switch Handle. */ + FW_Handle fw; /**< Flux-Weakening Handle. */ + + DqAxis idqRef; /**< Command value of the dq axis current. */ + UvwAxis currUvw; /**< Three-phase current sampling value. */ + AlbeAxis iabFbk; /**< αβ-axis current feedback value. */ + DqAxis idqFbk; /**< Current feedback value of the dq axis. */ + DqAxis vdqRef; /**< Current loop output dq voltage. */ + AlbeAxis vabRef; /**< Current loop output voltage αβ. */ + UvwAxis dutyUvw; /**< UVW three-phase duty cycle. */ + UvwAxis dutyUvwLeft; /**< Single Resistor UVW Three-Phase Left Duty Cycle. */ + UvwAxis dutyUvwRight; /**< Single Resistor UVW Three-Phase Right Duty Cycle. */ + + MCS_ReadCurrUvwCb readCurrUvwCb; /**< Read current callback function. */ + MCS_SetPwmDutyCb setPwmDutyCb; /**< Set the duty cycle callback function. */ + MCS_SetADCTriggerTimeCb setADCTriggerTimeCb; /**< Sets the ADC trigger point callback function. */ + + MotorProtStatus_Handle prot; /**< Motor protect status detection flag bit handle. */ +} MTRCTRL_Handle; + +void MCS_CarrierProcess(MTRCTRL_Handle *mtrCtrl); + +#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_chip_config.h b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_chip_config.h new file mode 100644 index 00000000..38c00b5d --- /dev/null +++ b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_chip_config.h @@ -0,0 +1,76 @@ +/** + * @ Copyright (c) HiSilicon (Shanghai) Technologies Co., Ltd. 2022-2023. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_chip_config.h + * @author MCU Algorithm Team + * @brief This file provides config macros for ECMCU105H/ECBMCU201MPC app. + */ + +#ifndef McuMagicTag_MCS_CHIPCONFIG_H +#define McuMagicTag_MCS_CHIPCONFIG_H + +#include "feature.h" + +#if defined (CHIP_3061MNPICA) || defined (CHIP_3061MNPIKA) || defined (CHIP_3061MNNICA) || \ + defined (CHIP_3061MNNIKA) || defined (CHIP_3061MNPIC8) || defined(CHIP_3061MNNIC8) || \ + defined (CHIP_3061MNPIK8) || defined (CHIP_3061MNNIK8) + + #define ADCPTT_HANDLE g_adc0 + #define ADCRESIS_HANDLE g_adc0 + #define ADCUDC_HANDLE g_adc0 + #define ADCU_HANDLE g_adc0 + #define ADCW_HANDLE g_adc0 + #define LED1_HANDLE g_gpio2 + #define LED2_HANDLE g_gpio1 + #define LED2_PIN GPIO_PIN_0 + #define LED1_PIN GPIO_PIN_3 + #define ADCPTTSOCNUM ADC_SOC_NUM0 + #define ADCUSOCNUM ADC_SOC_NUM1 + #define ADCRESISSOCNUM ADC_SOC_NUM3 + #define ADCUDCSOCNUM ADC_SOC_NUM4 + #define ADCWSOCNUM ADC_SOC_NUM6 + + #define ADC0COMPENSATE 2037.0f + #define ADC1COMPENSATE 2027.0f + +#endif + +#if defined (CHIP_3065HRPIRZ) || defined (CHIP_3065ARPIRZ) || defined (CHIP_3066MNPIRH) || \ + defined (CHIP_3065PNPIRH) || defined (CHIP_3065PNPIRE) || defined (CHIP_3065PNPIRA) + + #define ADCU_HANDLE g_adc0 + #define ADCW_HANDLE g_adc1 + #define ADCRESIS_HANDLE g_adc2 + #define ADCUDC_HANDLE g_adc2 + #define ADCPTT_HANDLE g_adc2 + #define LED1_HANDLE g_gpio0 + #define LED2_HANDLE g_gpio0 + #define LED2_PIN GPIO_PIN_6 + #define LED1_PIN GPIO_PIN_7 + #define ADCPTTSOCNUM ADC_SOC_NUM0 + #define ADCRESISSOCNUM ADC_SOC_NUM1 + #define ADCUSOCNUM ADC_SOC_NUM8 + #define ADCWSOCNUM ADC_SOC_NUM8 + #define ADCUDCSOCNUM ADC_SOC_NUM14 + + #define ADC0COMPENSATE 2033.0f + #define ADC1COMPENSATE 2070.0f + +#endif + + +#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_ctlmode_config.h b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_ctlmode_config.h new file mode 100644 index 00000000..2a6f38cc --- /dev/null +++ b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_ctlmode_config.h @@ -0,0 +1,73 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_ctlmode_config.h + * @author MCU Algorithm Team + * @brief This file provides config macros for ECMCU105H app. + */ + + /* Define to prevent recursive inclusion ------------------------------------------------------- */ +#ifndef McuMagicTag_MCS_CTLMODECONFIG_H +#define McuMagicTag_MCS_CTLMODECONFIG_H + +#include "debug.h" +#include "typedefs.h" + +typedef enum { + FOC_STARTUP_IF = 0, + FOC_STARTUP_VF, + FOC_STARTUP_HFI +} MOTOR_STARTUPMODE_CONFIG; + +typedef enum { + FOC_OBSERVERTYPE_SMO1TH = 0, + FOC_OBSERVERTYPE_SMO1TH_PLL, + FOC_OBSERVERTYPE_SMO4TH, + FOC_OBSERVERTYPE_SMO4TH_PLL, + FOC_OBSERVERTYPE_LUNBORG, + FOC_OBSERVERTYPE_FLUX +} MOTOR_OBSERVERTYPE_CONFIG; + +typedef enum { + FOC_CONTROLMODE_SPEED = 0, + FOC_CONTROLMODE_TORQUE +} MOTOR_CONTROLMODE_CONFIG; + +typedef enum { + FOC_CURQAXISPID_PARAMS = 0, + FOC_CURDAXISPID_PARAMS, + FOC_SPDPID_PARAMS +} MOTOR_PID_SET; + +typedef enum { + MOTOR_PARAMS_BASE = 0, + MOTOR_PARAMS_SPECIAL, + MOTOR_PARAMS_BOARD +} MOTOR_PARAMS_SET; + +typedef enum { + CUST_SPEED_ADJUST = 0, + HOST_SPEED_ADJUST +} MODE_ADSPEED_CONFIG; + +typedef enum { + CONNECTING = 0, + CONNECTED, + DISCONNECT +} UART_STATUS; + +#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/sample_gpio_key/gpio_key_sample.h b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_motor_process.h similarity index 76% rename from vendor/yibaina_3061M/demo/sample_gpio_key/gpio_key_sample.h rename to vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_motor_process.h index 17ff5146..783bf214 100644 --- a/vendor/yibaina_3061M/demo/sample_gpio_key/gpio_key_sample.h +++ b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_motor_process.h @@ -1,30 +1,45 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file gpio_key_sample.h - * @author MCU Driver Team - * @brief GPIO module sample - * @details This file provides sample code for users to help use - * the trigger an interrupt based on the key connected to the GPIO. - */ - -#ifndef GPIO_KEY_SAMPLE_H -#define GPIO_KEY_SAMPLE_H - -BASE_StatusType GPIO_KeySample(void); - -#endif /* GPIO_KEY_SAMPLE_H */ \ No newline at end of file +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_motor_process.h + * @author MCU Algorithm Team + * @brief This file provides motor sample functions declaration for ECMCU105H board. + */ +#ifndef McuMagicTag_MCS_MOTOR_PROCESS_H +#define McuMagicTag_MCS_MOTOR_PROCESS_H + +/** + * @brief phase sequence. + */ +typedef enum { + PHASE_U = 0, + PHASE_V, + PHASE_W, + PHASE_MAX_NUM +} MCS_PhaseEnum; + +/** + * @brief key state. + */ +typedef enum { + KEY_DOWN = 0, + KEY_UP = 1, +} KEY_State; + +int MotorMainProcess(void); + +#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_status.h b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_status.h new file mode 100644 index 00000000..19510c78 --- /dev/null +++ b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_status.h @@ -0,0 +1,188 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_status.h + * @author MCU Algorithm Team + * @brief This file provides functions declaration of system status. + */ + +/* Define to prevent recursive inclusion ------------------------------------------------------- */ +#ifndef McuMagicTag_MCS_STATUS_H +#define McuMagicTag_MCS_STATUS_H + +/* Includes ------------------------------------------------------------------------------------ */ +#include "typedefs.h" +#include "mcs_assert.h" + +/* Typedef definitions ------------------------------------------------------------------------- */ +/** + * @brief System status define + */ +typedef union { + unsigned short all; + struct { + unsigned short cmdStart : 1; /**< Indicates that a start system command has been received. */ + unsigned short cmdStop : 1; /**< Indicates that a stop system command has been received. */ + unsigned short isRunning : 1; /**< Indicates that the system is running (enable signal) */ + unsigned short sysError : 1; /**< Indicates that the system reports an error. */ + unsigned short poweron : 1; /**< Indicates that the power-on initialization phase is complete. */ + unsigned short capcharge : 1; /**< Indicates that the bootstrap capacitor charging phase is complete. */ + unsigned short adczero : 1; /**< The current sampling point is reset to zero after power-on. */ + } Bit; +} SysStatusReg; + +/** + * @brief Get status of Bit cmdStart. + * @param sysStatus System status register handle. + * @retval Status of Bit cmdStart. + */ +static inline bool SysGetCmdStart(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + bool ret; + ret = (sysStatus->Bit.cmdStart == 1) ? true : false; + return ret; +} + +/** + * @brief Set Bit cmdStart. + * @param sysStatus System status register handle. + * @retval None. + */ +static inline void SysCmdStartSet(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + sysStatus->Bit.cmdStart = 1; +} + +/** + * @brief Clear Bit cmdStart. + * @param handle System status register handle. + * @retval None. + */ +static inline void SysCmdStartClr(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + sysStatus->Bit.cmdStart = 0; +} + +/** + * @brief Get status of Bit cmdStop. + * @param sysStatus System status register handle. + * @retval Status of Bit cmdStart. + */ +static inline bool SysGetCmdStop(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + bool ret; + ret = (sysStatus->Bit.cmdStop == 1) ? true : false; + return ret; +} + +/** + * @brief Set Bit cmdStop. + * @param sysStatus System status register handle. + * @retval None. + */ +static inline void SysCmdStopSet(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + sysStatus->Bit.cmdStop = 1; +} + +/** + * @brief Clear Bit cmdStop. + * @param sysStatus System status register handle. + * @retval None. + */ +static inline void SysCmdStopClr(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + sysStatus->Bit.cmdStop = 0; +} + +/** + * @brief Get status of Bit isRunning. + * @param sysStatus System status register handle. + * @retval Status of Bit isRunning. + */ +static inline bool SysIsRunning(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + bool ret; + ret = (sysStatus->Bit.isRunning == 1) ? true : false; + return ret; +} + +/** + * @brief Set Bit isRuning. + * @param sysStatus System status register handle. + * @retval None. + */ +static inline void SysRunningSet(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + sysStatus->Bit.isRunning = 1; +} + +/** + * @brief Clear Bit isRuning. + * @param sysStatus System status register handle. + * @retval None. + */ +static inline void SysRunningClr(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + sysStatus->Bit.isRunning = 0; +} + +/** + * @brief Get status of Bit sysError. + * @param sysStatus System status register handle. + * @retval Status of Bit sysError. + */ +static inline bool SysIsError(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + bool ret; + ret = (sysStatus->Bit.sysError == 1) ? true : false; + return ret; +} + +/** + * @brief Set Bit sysError. + * @param sysStatus System status register handle. + * @retval None. + */ +static inline void SysErrorSet(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + sysStatus->Bit.sysError = 1; +} + +/** + * @brief Clear Bit sysError. + * @param sysStatus System status register handle. + * @retval None. + */ +static inline void SysErrorClr(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + sysStatus->Bit.sysError = 0; +} + +#endif diff --git a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_user_config.h b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_user_config.h new file mode 100644 index 00000000..0b9c66a6 --- /dev/null +++ b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_user_config.h @@ -0,0 +1,121 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_user_config.h + * @author MCU Algorithm Team + * @brief This file provides functions declaration of user config parameters. + */ +#ifndef McuMagicTag_MCS_CONFIG_H +#define McuMagicTag_MCS_CONFIG_H + +#include "debug.h" +#include "typedefs.h" + +#define SMO4TH + +#define SYSTICK_PERIOD_US 500u /* systick period. */ + +#define INV_CAP_CHARGE_MS 3u /* Capacitor charging time. */ + +#define INV_VOLTAGE_BUS 24.0f /* Bus voltage, V. */ + +#define CTRL_CURR_PERIOD 0.0001f /* carrier ISR period, 100us. */ +#define CTRL_SYSTICK_PERIOD 0.0005f /* systick control period, 500us. */ + +/* Duty of sample window, the real time is 0.06*50us = 3us. */ +#define SAMPLE_WINDOW_DUTY 0.06f + +/* Duty of sample point shift as flip point, the real time is 0.008*50us = 0.4us. */ +#define SAMPLE_POINT_SHIFT 0.008f + +/* Sampling resistance 200mOhm 0.0013295 */ +#define ADC_CURR_COFFI 0.0013295f /* 3.3/4096/3.03/0.2 pga: 3.03, 200mohm. */ + +#define APT_U APT0_BASE /* Base address of U phase APT module. */ +#define APT_V APT1_BASE /* Base address of V phase APT module. */ +#define APT_W APT2_BASE /* Base address of W phase APT module. */ + +/* FOSMO */ +#define FOSMO_GAIN 4.0f /* SMO gain */ +#define FOSMO_LAMBDA 2.0f /* SMO coefficient of cut-off frequency, its value = lambda * we. */ +#define FOSMO_EMF_CUTOFF_FREQ 2.0f /* SMO back emf cutoff frequency. */ +#define SPEED_FILTER_CUTOFF_FREQUENCY 40.0f /* SMO speed cutoff frequency of speed filter. */ +#define FOSMO_PLL_BDW 30.0f /* SMO PLL bandwidth. */ + +/* SMO4TH */ +#define SMO4TH_PLL_BDW 30.0f /* SMO4TH PLL Bandwidth. */ +#define SMO4TH_KD 300.0f /* SMO4TH parameters KD. */ +#define SMO4TH_KQ 600.0f /* SMO4TH parameters KQ. */ +#define SMO4TH_SPD_FILTER_CUTOFF_FREQ 40.0f /* SMO4TH speed cutoff frequency of speed filter. */ + +/* User_Commond */ +#define CTRL_IF_CURR_AMP_A 0.8f /* IF control current amplitude. */ +#define USER_TARGET_SPD_HZ 100.0f /* User-defined target speed value. */ +#define USER_SWITCH_SPDBEGIN_HZ 30.0f /* Start of handover interval. */ +#define USER_SWITCH_SPDEND_HZ (USER_SWITCH_SPDBEGIN_HZ + 3.0f) /* End of handover period. */ +#define USER_MAX_SPD_HZ 29.0f /* User-defined maximum speed value. */ +#define USER_MIN_SPD_HZ 1.0f /* User-defined minimum speed value. */ +#define USER_SPD_SLOPE 50.0f /* slope of velocity change. */ +#define USER_CURR_SLOPE (CTRL_IF_CURR_AMP_A * 10.0f) /* Current change slope. */ + +/* PID PARAMS */ +#define CURRQAXIS_KP 5.023202f /* Current loop Q axis Kp. */ +#define CURRQAXIS_KI 20612.84f /* Current loop Q axis Ki. */ +#define CURRDAXIS_KP 3.477114f /* Current loop D axis Kp. */ +#define CURRDAXIS_KI 20612.84f /* Current loop D axis Ki. */ +/* Current loop PID output lower limit. */ +#define CURR_LOWERLIM (-INV_VOLTAGE_BUS * ONE_DIV_SQRT3 * 0.98f) +/* Current loop PID output upper limit. */ +#define CURR_UPPERLIM (INV_VOLTAGE_BUS * ONE_DIV_SQRT3 * 0.98f) + +#define SPD_KP 0.00505f /* Speed loop Kp. */ +#define SPD_KI 0.012f /* Speed loop Ki. */ +#define SPD_LOWERLIM (-0.105f) /* Speed loop PID output lower limit. */ +#define SPD_UPPERLIM 0.105f /* Speed loop PID output upper limit. */ + +/* MOTOR PARAMS */ +/* Np, Rs, Ld, Lq, Psif, J, Nmax, Currmax, PPMR, zShift. */ +/* mtrPsif & mtrJ parameter is not used in this project, temporarily set to 0. */ +// #define MOTORPARAM_DEFAULTS { \ +// .mtrNp = 7, \ +// .mtrRs = 5.1f, \ +// .mtrLd = 0.00133f, \ +// .mtrLq = 0.00133f, \ +// .mtrPsif = 0.0f, \ +// .mtrJ = 0.0f, \ +// .maxElecSpd = 180.25f, \ +// .maxCurr = 0.105f, \ +// .busVolt = INV_VOLTAGE_BUS, \ +// } + +#define MOTORPARAM_DEFAULTS { \ + .mtrNp = 21, \ + .mtrRs = 2.5f, \ + .mtrLd = 0.066f, \ + .mtrLq = 0.066f, \ + .mtrPsif = 0.0f, \ + .mtrJ = 0.0f, \ + .maxElecSpd = 49.0f, \ + .maxCurr = 0.8f, \ + .busVolt = INV_VOLTAGE_BUS, \ +} + + +/* Bus voltage sampling conversion factor. */ +#define ADC_UDC_COFFI 0.01289f /* 0.01289 = 3.3/4096*192/12. */ + +#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_cmm.c b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_cmm.c new file mode 100644 index 00000000..99766bdf --- /dev/null +++ b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_cmm.c @@ -0,0 +1,43 @@ +/** + * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_prot_cmm.c + * @author MCU Algorithm Team + * @brief This file contains protection common api definition. + */ + +/* Includes ------------------------------------------------------------------------------------ */ +#include "mcs_prot_cmm.h" +#include "apt.h" +#include "mcs_assert.h" + +/** + * @brief Safty-pulse-off function execution to turn off all the power devices. + * @param aptAddr Three-phase APT address pointer. + * @retval None. + */ +void ProtSpo_Exec(APT_RegStruct **aptAddr) +{ + MCS_ASSERT_PARAM(aptAddr != NULL); + /**< Turn off all the six power devices of the inverter. */ + for (unsigned int i = 0; i < MOTOR_PHASE_NUMBER; i++) { + APT_RegStruct *aptx = (APT_RegStruct *)(aptAddr[i]); + DCL_APT_EnableSwContPWMAction(aptx, APT_PWM_CHANNEL_A); + DCL_APT_EnableSwContPWMAction(aptx, APT_PWM_CHANNEL_B); + DCL_APT_ForcePWMOutputLow(aptx); + } +} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_cmm.h b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_cmm.h new file mode 100644 index 00000000..104a5dec --- /dev/null +++ b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_cmm.h @@ -0,0 +1,136 @@ +/** + * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_prot_cmm.h + * @author MCU Algorithm Team + * @brief This file contains protection function common data struct and api declaration. + */ + +/* Define to prevent recursive inclusion ------------------------------------------------------- */ +#ifndef McuMagicTag_MCS_PROT_CMM_H +#define McuMagicTag_MCS_PROT_CMM_H + +#include "typedefs.h" +#include "apt_ip.h" + +/* Macro definitions --------------------------------------------------------------------------- */ +#define MOTOR_PHASE_NUMBER (3) + +#define PROT_VAL_THRESHOLD_NUMS (4) +#define PROT_VAL_THRESHOLD_0 (0) +#define PROT_VAL_THRESHOLD_1 (1) +#define PROT_VAL_THRESHOLD_2 (2) +#define PROT_VAL_THRESHOLD_3 (3) + +#define PROT_LIMIT_TIME_NUMS (3) +#define PROT_LIMIT_TIME_0 (0) +#define PROT_LIMIT_TIME_1 (1) +#define PROT_LIMIT_TIME_2 (2) + +#define MOTOR_PHASE_NUMBER (3) + +/**< Motor error status definition. */ +typedef union { + int all; + struct { + unsigned short overCurrErr : 1; /**< Indicates that phase current(s) is over protected value. */ + unsigned short overVoltErr : 1; /**< Indicates that dc-link voltage is over protected value. */ + unsigned short lowerVoltErr : 1; /**< Indicates that dc-link voltage is lower than protected value */ + unsigned short overIpmTempErr : 1; /**< Indicates that IPM temperature is over protected value. */ + unsigned short revRotErr : 1; /**< Indicates that motor negtive direction. */ + unsigned short motorStalling : 1; /**< Indicates that rotor is stalling. */ + unsigned short overMotorTempErr : 1; /**< Indicates that three phase currents is out-of-balance. */ + unsigned short posSnsrCommsErr : 1; /**< Indicates that position sensor communication is lost with MCU. */ + unsigned short posSnsrFuncErr : 1; /**< Indicates that position sensor reports function error. */ + unsigned short posSnsrCalibrErr : 1; /**< Indicates that position sensor fails to calibrate itself. */ + unsigned short currOutOfBalance : 1; /**< Indicates that the rotor is reverse rotation.*/ + unsigned short phsOpenErr : 1; /**< Indicates that phase winding(s) is open. */ + unsigned short phsU : 1; /**< Indicates that u phase fails when phsOpenErr occurs. */ + unsigned short phsV : 1; /**< Indicates that v phase fails when phsOpenErr occurs. */ + unsigned short phsW : 1; /**< Indicates that w phase fails when phsOpenErr occurs. */ + unsigned short multiPhs : 1; /**< Indicates that multi-phases fail when phsOpenErr occurs.*/ + } Bit; +} MotorErrStatusReg; + +/**< Protection Status Bit Definition */ +typedef enum { + OCP_ERR_BIT, + OVP_ERR_BIT, + LVP_ERR_BIT, + OTP_IPM_ERR_BIT, + OTP_MOTOR_ERR_BIT, + STALLING_ERR_BIT, + CURR_OUT_BALANCE_ERR_BIT, + POS_COMMS_ERR_BIT, + POS_FUNC_ERR_BIT, + POS_CALIB_ERR_BIT, + REV_ROT_ERR_BIT, + PHS_OPEN_ERR_BIT, + PHS_U_ERR_BIT, + PHS_V_ERR_BIT, + PHS_W_ERR_BIT, + PHS_MULTI_ERR_BIT, +} PROT_ErrBit; + +/**< Motor error protection level. */ +typedef enum { + PROT_LEVEL_0 = 0, + PROT_LEVEL_1, + PROT_LEVEL_2, + PROT_LEVEL_3, + PROT_LEVEL_4 /**< The greater level number, the severe error is. */ +} PROT_Level; + +/** + * @brief Obtains the status of a bit of data. + * @param data data. + * @param bits Number of digits. + * @retval Bit status. + */ +static inline bool GetBit(int data, unsigned short bit) +{ + bool ret; + ret = ((data >> bit) & 1); + return ret; +} + +/** + * @brief Sets the status of a bit of data. + * @param data data. + * @param bit The setted bit. + * @retval None. + */ +static inline void SetBit(int *data, unsigned char bit) +{ + *data |= (1 << bit); +} + +/** + * @brief Clear the status of a bit of data. + * @param data data. + * @param bit The Clear bit. + * @retval None. + */ +static inline void ClearBit(int *data, unsigned char bit) +{ + *data &= ~(1 << bit); +} + +/**< Protection action. */ +void ProtSpo_Exec(APT_RegStruct **aptAddr); + +#endif diff --git a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user.c b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user.c new file mode 100644 index 00000000..e97876b7 --- /dev/null +++ b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user.c @@ -0,0 +1,137 @@ +/** + * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_prot_user.c + * @author MCU Algorithm Team + * @brief This file contains user protection data struct, inquiry api and initialization api definition. + */ + +/* Includes ------------------------------------------------------------------------------------ */ +#include "mcs_prot_user.h" +#include "mcs_prot_user_config.h" +#include "mcs_assert.h" + +/** + * @brief Get motor over current error status. + * @param motorErrStatus Motor error status. + * @retval Error status. + */ +bool IsMotorOverCurrErr(MotorErrStatusReg motorErrStatus) +{ + if (motorErrStatus.Bit.overCurrErr) { + return true; + } else { + return false; + } +} + +/** + * @brief Get motor over dc-link voltage error status. + * @param motorErrStatus Motor error status. + * @retval Error status. + */ +bool IsDcLinkOverVoltErr(MotorErrStatusReg motorErrStatus) +{ + if (motorErrStatus.Bit.overVoltErr) { + return true; + } else { + return false; + } +} + +/** + * @brief Get motor lower dc-link voltage error status. + * @param motorErrStatus Motor error status. + * @retval Error status. + */ +bool IsDcLinkLowerVoltErr(MotorErrStatusReg motorErrStatus) +{ + if (motorErrStatus.Bit.lowerVoltErr) { + return true; + } else { + return false; + } +} + +/** + * @brief Get motor over Ipm temperature error status. + * @param motorErrStatus Motor error status. + * @retval Error status. + */ +bool IsIpmOverTempErr(MotorErrStatusReg motorErrStatus) +{ + if (motorErrStatus.Bit.overIpmTempErr) { + return true; + } else { + return false; + } +} + +/** + * @brief Get motor over Motor temperature error status. + * @param motorErrStatus Motor error status. + * @retval Error status. + */ +bool IsMotorOverTempErr(MotorErrStatusReg motorErrStatus) +{ + if (motorErrStatus.Bit.overMotorTempErr) { + return true; + } else { + return false; + } +} + +/** + * @brief Get motor stalling error status. + * @param motorErrStatus Motor error status. + * @retval Error status. + */ +bool IsMotorStallingErr(MotorErrStatusReg motorErrStatus) +{ + if (motorErrStatus.Bit.motorStalling) { + return true; + } else { + return false; + } +} + +/** + * @brief Clear the motor error status. + * @param motorProt Motor protection handle. + * @retval Error status. + */ +void ClearMotorErrStatus(MotorProtStatus_Handle *motorProt) +{ + MCS_ASSERT_PARAM(motorProt != NULL); + /* Clear the motor error status. */ + motorProt->motorErrStatus.all = 0x00; + OCP_Clear(&motorProt->ocp); + OVP_Clear(&motorProt->ovp); + LVP_Clear(&motorProt->lvp); + OTP_Clear(&motorProt->otp); +} + +/** + * @brief Motor protection function initialization. + * @param motorProt Motor protection handle. + * @retval Error status. + */ +void MotorProt_Init(MotorProtStatus_Handle *motorProt) +{ + MCS_ASSERT_PARAM(motorProt != NULL); + motorProt->motorErrStatus.all = 0x00; +} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user.h b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user.h new file mode 100644 index 00000000..365110e1 --- /dev/null +++ b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user.h @@ -0,0 +1,54 @@ +/** + * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_prot_user.h + * @author MCU Algorithm Team + * @brief This file contains user protection data struct, inquiry api and initialization api declaration. + */ + +/* Define to prevent recursive inclusion ------------------------------------------------------- */ +#ifndef McuMagicTag_MCS_PROT_USER_H +#define McuMagicTag_MCS_PROT_USER_H + +/* Includes ------------------------------------------------------------------------------------ */ +#include "mcs_prot_cmm.h" +#include "mcs_curr_prot.h" +#include "mcs_dc_volt_prot.h" +#include "mcs_temp_prot.h" +#include "mcs_motor_stalling.h" +#include "typedefs.h" + +typedef struct { + MotorErrStatusReg motorErrStatus; /**< Motor error status. */ + OCP_Handle ocp; /**< Over current protection. */ + OVP_Handle ovp; /**< Over dc-link voltage protection. */ + LVP_Handle lvp; /**< Lower dc-link voltage protection. */ + OTP_Handle otp; /**< Over IPM temperature protection. */ + STP_Handle stall; /**< Motor stalling protection. */ +} MotorProtStatus_Handle; + +void MotorProt_Init(MotorProtStatus_Handle *motorProt); + +/**< Inquiry motor error status */ +bool IsMotorOverCurrErr(MotorErrStatusReg motorErrStatus); +bool IsDcLinkOverVoltErr(MotorErrStatusReg motorErrStatus); +bool IsDcLinkLowerVoltErr(MotorErrStatusReg motorErrStatus); +bool IsIpmOverTempErr(MotorErrStatusReg motorErrStatus); +bool IsMotorOverTempErr(MotorErrStatusReg motorErrStatus); +bool IsMotorStallingErr(MotorErrStatusReg motorErrStatus); +void ClearMotorErrStatus(MotorProtStatus_Handle *motorProt); +#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user_config.h b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user_config.h new file mode 100644 index 00000000..832cd1be --- /dev/null +++ b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user_config.h @@ -0,0 +1,125 @@ +/** + * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_prot_user_config.h + * @author MCU Algorithm Team + * @brief This file contans user macro definition of the protection function. + */ + +/* Define to prevent recursive inclusion ------------------------------------------------------- */ +#ifndef McuMagicTag_MCS_PROT_USER_CONFIG_H +#define McuMagicTag_MCS_PROT_USER_CONFIG_H + +/* Macro definitions --------------------------------------------------------------------------- */ +/**< COMMON */ +/**< Peak phase current (A) of the motor or IPM under continuous operations. */ +#define PROT_MOTOR_RATED_CURR (1.0f) +/**< Only several continuous fault detection can trigger error status. */ +#define PROT_CNT_LIMIT (100) +/**< Only several contunuous none fault dectection can trigger elimination of error status. */ +#define RECY_CNT_LIMIT (10000) +/**< Only several contunuous none fault dectection can trigger elimination of error status. */ +#define OVER_VOLT_RECY_CNT_LIMIT (100) +/**< Only several contunuous none fault dectection can trigger elimination of error status. */ +#define LOWER_VOLT_RECY_CNT_LIMIT (100) + +/**< Over current protection */ +/**< Over current trigger value (A) when in level 1. */ +#define PROT_OVER_CURR_POW_DN1 (2.0f * PROT_MOTOR_RATED_CURR) +/**< Over current trigger value (A) when in level 2. */ +#define PROT_OVER_CURR_POW_DN2 (2.0f * PROT_MOTOR_RATED_CURR) +/**< Over current trigger value (A) when in level 3. */ +#define PROT_OVER_CURR_POW_DN3 (2.0f * PROT_MOTOR_RATED_CURR) +/**< Over current trigger value (A) when in level 4. */ +#define PROT_OVER_CURR_POW_OFF (2.0f * PROT_MOTOR_RATED_CURR) +#define PROT_OVER_CURR_RECY_DELTA (0.0035f) /**< Current gap (A) when recovers from protection status. */ +#define PROT_OVER_CURR_LIMIT1_TIME_SEC (30.0f) /**< 20% overload can last maximum time: 30 sec. */ +#define PROT_OVER_CURR_LIMIT2_TIME_SEC (10.0f) /**< 20% overload can last maximum time: 10 sec. */ +#define PROT_OVER_CURR_LIMIT3_TIME_SEC (2.0f) /**< 20% overload can last maximum time: 2 sec. */ + +/**< Over voltage protection */ +#define PROT_OVER_VOLT_BRK_ON1 (26.0f) /**<处于1级时,直流链路电压触发值(V)过高*/ +#define PROT_OVER_VOLT_BRK_ON2 (27.0f) /**< Over dc-link voltage trigger value (V) when in level 2. */ +#define PROT_OVER_VOLT_BRK_ON3 (28.0f) /**< Over dc-link voltage trigger value (V) when in level 3. */ +#define PROT_OVER_VOLT_BRK_ALL (30.0f) /**< Over dc-link voltage trigger value (V) when in level 4. */ +#define PROT_OVER_VOLT_RECY_DELTA (0.5f) /**<从保护状态恢复时的电压间隙(V)*/ +#define PROT_OVER_VOLT_LIMIT1_TIME_SEC (5.0f) /** 1; One-cycle mode: < 0.5 */ +#define POS_SNSR_IPD_INJ_PERIOD (4) + +#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_curr_prot.c b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_curr_prot.c new file mode 100644 index 00000000..da4e56f8 --- /dev/null +++ b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_curr_prot.c @@ -0,0 +1,267 @@ +/** + * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_curr_prot.c + * @author MCU Algorithm Team + * @brief This file contains current protecion api definition. + */ + +/* Includes ------------------------------------------------------------------------------------ */ +#include "mcs_curr_prot.h" +#include "mcs_math.h" +#include "mcs_prot_user_config.h" +#include "mcs_assert.h" + +/** + * @brief Initilization over current protection function. + * @param ocp Over current protection handle. + * @param ts Ctrl period (s). + * @retval None. + */ +void OCP_Init(OCP_Handle *ocp, float ts) +{ + MCS_ASSERT_PARAM(ocp != NULL); + MCS_ASSERT_PARAM(ts > 0.0f); + ocp->ts = ts; + OCP_Clear(ocp); + + ocp->protCntLimit = PROT_CNT_LIMIT; + ocp->recyCntLimit = RECY_CNT_LIMIT; + /* Configuring four levels of current protection thresholds. */ + ocp->protValThr[PROT_VAL_THRESHOLD_0] = PROT_OVER_CURR_POW_DN1; + ocp->protValThr[PROT_VAL_THRESHOLD_1] = PROT_OVER_CURR_POW_DN2; + ocp->protValThr[PROT_VAL_THRESHOLD_2] = PROT_OVER_CURR_POW_DN3; + ocp->protValThr[PROT_VAL_THRESHOLD_3] = PROT_OVER_CURR_POW_OFF; + /* Configure the protection limit time. */ + ocp->protLimitTime[PROT_LIMIT_TIME_0] = PROT_OVER_CURR_LIMIT1_TIME_SEC; + ocp->protLimitTime[PROT_LIMIT_TIME_1] = PROT_OVER_CURR_LIMIT2_TIME_SEC; + ocp->protLimitTime[PROT_LIMIT_TIME_2] = PROT_OVER_CURR_LIMIT3_TIME_SEC; + ocp->recyDelta = PROT_OVER_CURR_RECY_DELTA; +} + +/** + * @brief Over current protection detection. + * @param ocp Over current protection handle. + * @param motorErrStatus Motor error status. + * @param idq DQ-axis feedback currents. + * @retval None. + */ +void OCP_Det(OCP_Handle *ocp, MotorErrStatusReg *motorErrStatus, DqAxis idq) +{ + MCS_ASSERT_PARAM(ocp != NULL); + MCS_ASSERT_PARAM(motorErrStatus != NULL); + /* Calculate current amplitude. */ + ocp->currAmp = Sqrt(idq.d * idq.d + idq.q * idq.q); + + /* Check if value goes over threshold for continuous cycles. */ + if (ocp->currAmp < ocp->protValThr[PROT_VAL_THRESHOLD_0]) { + ocp->protCnt = 0; + return; + } + + if (ocp->protCnt < ocp->protCntLimit) { + ocp->protCnt++; + return; + } + + /* Check which protection level should be activated. */ + /* Once enter error status, error status can only be changed by recover api. */ + /* protect level: level 4. */ + if (ocp->currAmp >= ocp->protValThr[PROT_VAL_THRESHOLD_3] && ocp->protLevel < PROT_LEVEL_4) { + ocp->protLevel = PROT_LEVEL_4; + motorErrStatus->Bit.overCurrErr = 1; + ocp->protCnt = 0; + return; + } + + /* Protect level: level 3. */ + if (ocp->currAmp >= ocp->protValThr[PROT_VAL_THRESHOLD_2] && ocp->protLevel < PROT_LEVEL_3) { + ocp->protLevel = PROT_LEVEL_3; + motorErrStatus->Bit.overCurrErr = 1; + ocp->protCnt = 0; + ocp->timer = 0.0f; + return; + } + + /* Protect level: level 2. */ + if (ocp->currAmp >= ocp->protValThr[PROT_VAL_THRESHOLD_1] && ocp->protLevel < PROT_LEVEL_2) { + ocp->protLevel = PROT_LEVEL_2; + motorErrStatus->Bit.overCurrErr = 1; + ocp->protCnt = 0; + ocp->timer = 0.0f; + return; + } + + /* Protect level: level 1. */ + if (ocp->currAmp >= ocp->protValThr[PROT_VAL_THRESHOLD_0] && ocp->protLevel < PROT_LEVEL_1) { + ocp->protLevel = PROT_LEVEL_1; + motorErrStatus->Bit.overCurrErr = 1; + ocp->protCnt = 0; + ocp->timer = 0.0f; + return; + } +} + +/** + * @brief Over current protection execution. + * @param ocp Over current protection handle. + * @param idqRef DQ-axis current references. + * @param aptAddr Three-phase APT address pointer. + * @retval None. + */ +void OCP_Exec(OCP_Handle *ocp, DqAxis *idqRef, APT_RegStruct **aptAddr) +{ + MCS_ASSERT_PARAM(ocp != NULL); + MCS_ASSERT_PARAM(idqRef != NULL); + MCS_ASSERT_PARAM(aptAddr != NULL); + + float id = idqRef->d; + float iq = idqRef->q; + float idqAmp = ocp->currAmp; + /* According to protect level, take corresponding action. */ + switch (ocp->protLevel) { + /* level 4: disable all PWM output. */ + case PROT_LEVEL_4: + /* Disable three-phase pwm output. */ + ProtSpo_Exec(aptAddr); + idqRef->d = 0.0f; + idqRef->q = 0.0f; + break; + + /* level 3: derate speed reference. */ + case PROT_LEVEL_3: + ocp->timer += ocp->ts; + if (ocp->timer > ocp->protLimitTime[PROT_LIMIT_TIME_2]) { + idqRef->d = id / idqAmp * PROT_MOTOR_RATED_CURR; + idqRef->q = iq / idqAmp * PROT_MOTOR_RATED_CURR; + } + break; + + /* level 2: derate speed reference. */ + case PROT_LEVEL_2: + ocp->timer += ocp->ts; + if (ocp->timer > ocp->protLimitTime[PROT_LIMIT_TIME_1]) { + idqRef->d = id / idqAmp * PROT_MOTOR_RATED_CURR; + idqRef->q = iq / idqAmp * PROT_MOTOR_RATED_CURR; + } + break; + + /* level 1: derate speed reference. */ + case PROT_LEVEL_1: + ocp->timer += ocp->ts; + if (ocp->timer > ocp->protLimitTime[PROT_LIMIT_TIME_0]) { + idqRef->d = id / idqAmp * PROT_MOTOR_RATED_CURR; + idqRef->q = iq / idqAmp * PROT_MOTOR_RATED_CURR; + } + break; + + /* level 0: take no protection action. */ + case PROT_LEVEL_0: + break; + + default: + break; + } +} + +/** + * @brief Over current protection recovery. + * @param ocp Over current protection handle. + * @param motorErrStatus Motor error status. + * @retval None. + */ +void OCP_Recy(OCP_Handle *ocp, MotorErrStatusReg *motorErrStatus) +{ + MCS_ASSERT_PARAM(ocp != NULL); + /* If not under error state, just return without any operation. */ + if (!motorErrStatus->Bit.overCurrErr) { + return; + } + + /* Calculate current amplitude. */ + float currAmp = ocp->currAmp; + + /* According to protection level, take corresponding recovery action. */ + switch (ocp->protLevel) { + /* level 4 */ + case PROT_LEVEL_4: + if (currAmp < ocp->protValThr[PROT_VAL_THRESHOLD_3] - ocp->recyDelta) { + ocp->recyCnt++; + if (ocp->recyCnt > ocp->recyCntLimit) { + ocp->protLevel = PROT_LEVEL_3; + ocp->recyCnt = 0; + } + } + break; + + /* level 3 */ + case PROT_LEVEL_3: + if (currAmp < ocp->protValThr[PROT_VAL_THRESHOLD_2] - ocp->recyDelta) { + ocp->recyCnt++; + if (ocp->recyCnt > ocp->recyCntLimit) { + ocp->protLevel = PROT_LEVEL_2; + ocp->recyCnt = 0; + } + } + break; + + /* level 2 */ + case PROT_LEVEL_2: + if (currAmp < ocp->protValThr[PROT_VAL_THRESHOLD_1] - ocp->recyDelta) { + ocp->recyCnt++; + if (ocp->recyCnt > ocp->recyCntLimit) { + ocp->protLevel = PROT_LEVEL_1; + ocp->recyCnt = 0; + } + } + break; + + /* level 1 */ + case PROT_LEVEL_1: + if (currAmp < ocp->protValThr[PROT_VAL_THRESHOLD_0] - ocp->recyDelta) { + ocp->recyCnt++; + if (ocp->recyCnt > ocp->recyCntLimit) { + ocp->protLevel = PROT_LEVEL_0; + ocp->recyCnt = 0; + } + } + break; + + /* level 0 */ + case PROT_LEVEL_0: + motorErrStatus->Bit.overCurrErr = 0; + break; + + default: + break; + } +} + +/** + * @brief Over current protection error status clear. + * @param ocp Over current protection handle. + * @retval None. + */ +void OCP_Clear(OCP_Handle *ocp) +{ + MCS_ASSERT_PARAM(ocp != NULL); + /* Clear the history value. */ + ocp->protCnt = 0; + ocp->protLevel = PROT_LEVEL_0; + ocp->recyCnt = 0; + ocp->timer = 0.0f; +} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_curr_prot.h b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_curr_prot.h new file mode 100644 index 00000000..e9916fe4 --- /dev/null +++ b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_curr_prot.h @@ -0,0 +1,52 @@ +/** + * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_curr_prot.h + * @author MCU Algorithm Team + * @brief This file contains current protection data struct and api declaration. + */ + +/* Define to prevent recursive inclusion ------------------------------------- */ +#ifndef McuMagicTag_MCS_CURR_PROT_H +#define McuMagicTag_MCS_CURR_PROT_H + +/* Includes ------------------------------------------------------------------------------------ */ +#include "mcs_prot_cmm.h" +#include "mcs_typedef.h" +#include "apt_ip.h" + +typedef struct { + unsigned int protCnt; + unsigned int recyCnt; + unsigned int protCntLimit; + unsigned int recyCntLimit; + float currAmp; + float protValThr[PROT_VAL_THRESHOLD_NUMS]; /* from low current level to high. */ + float protLimitTime[PROT_LIMIT_TIME_NUMS]; /* from level 3 to level 1. */ + float recyDelta; + float timer; + float ts; + PROT_Level protLevel; +} OCP_Handle; + +void OCP_Init(OCP_Handle *ocp, float ts); +void OCP_Det(OCP_Handle *ocp, MotorErrStatusReg *motorErrStatus, DqAxis idq); +void OCP_Exec(OCP_Handle *ocp, DqAxis *idqRef, APT_RegStruct **aptAddr); +void OCP_Recy(OCP_Handle *ocp, MotorErrStatusReg *motorErrStatus); +void OCP_Clear(OCP_Handle *ocp); + +#endif diff --git a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_dc_volt_prot.c b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_dc_volt_prot.c new file mode 100644 index 00000000..b0541a2e --- /dev/null +++ b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_dc_volt_prot.c @@ -0,0 +1,495 @@ +/** + * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_dc_volt_prot.c + * @author MCU Algorithm Team + * @brief This file contains dc-link voltage protection api declaration. + */ + +/* Includes ------------------------------------------------------------------------------------ */ +#include "mcs_dc_volt_prot.h" +#include "mcs_prot_user_config.h" +#include "mcs_assert.h" + +/** + * @brief Initilization over dc-link voltage protection function. + * @param ovp Over dc-link voltage protection handle. + * @param ts Ctrl period (s). + * @retval None. + */ +void OVP_Init(OVP_Handle *ovp, float ts) +{ + MCS_ASSERT_PARAM(ovp != NULL); + MCS_ASSERT_PARAM(ts > 0.0f); + ovp->ts = ts; + OVP_Clear(ovp); + ovp->protCntLimit = PROT_CNT_LIMIT; + ovp->recyCntLimit = OVER_VOLT_RECY_CNT_LIMIT; + /* Configuring four levels of overvoltage protection thresholds. */ + ovp->protValThr[PROT_VAL_THRESHOLD_0] = PROT_OVER_VOLT_BRK_ON1; + ovp->protValThr[PROT_VAL_THRESHOLD_1] = PROT_OVER_VOLT_BRK_ON2; + ovp->protValThr[PROT_VAL_THRESHOLD_2] = PROT_OVER_VOLT_BRK_ON3; + ovp->protValThr[PROT_VAL_THRESHOLD_3] = PROT_OVER_VOLT_BRK_ALL; + /* Configure the protection limit time. */ + ovp->protLimitTime[PROT_LIMIT_TIME_0] = PROT_OVER_VOLT_LIMIT1_TIME_SEC; + ovp->protLimitTime[PROT_LIMIT_TIME_1] = PROT_OVER_VOLT_LIMIT2_TIME_SEC; + ovp->protLimitTime[PROT_LIMIT_TIME_2] = PROT_OVER_VOLT_LIMIT3_TIME_SEC; + ovp->recyDelta = PROT_OVER_VOLT_RECY_DELTA; +} + +/** + * @brief Initilization lower dc-link voltage protection function. + * @param lvp Lower dc-link voltage protection handle. + * @param ts Ctrl period (s). + * @retval None. + */ +void LVP_Init(LVP_Handle *lvp, float ts) +{ + MCS_ASSERT_PARAM(lvp != NULL); + MCS_ASSERT_PARAM(ts > 0.0f); + + lvp->ts = ts; + LVP_Clear(lvp); + + lvp->protCntLimit = PROT_CNT_LIMIT; + lvp->recyCntLimit = LOWER_VOLT_RECY_CNT_LIMIT; + /* Configuring four levels of lower voltage protection thresholds. */ + lvp->protValThr[PROT_VAL_THRESHOLD_0] = PROT_LOWER_VOLT_POW_DN1; + lvp->protValThr[PROT_VAL_THRESHOLD_1] = PROT_LOWER_VOLT_POW_DN2; + lvp->protValThr[PROT_VAL_THRESHOLD_2] = PROT_LOWER_VOLT_POW_DN3; + lvp->protValThr[PROT_VAL_THRESHOLD_3] = PROT_LOWER_VOLT_POW_OFF; + /* Configure the protection limit time. */ + lvp->protLimitTime[PROT_LIMIT_TIME_0] = PROT_LOWER_VOLT_LIMIT1_TIME_SEC; + lvp->protLimitTime[PROT_LIMIT_TIME_1] = PROT_LOWER_VOLT_LIMIT2_TIME_SEC; + lvp->protLimitTime[PROT_LIMIT_TIME_2] = PROT_LOWER_VOLT_LIMIT3_TIME_SEC; + lvp->recyDelta = PROT_OVER_VOLT_RECY_DELTA; +} + +/** + * @brief Over dc-link voltage protection detection. + * @param ovp Over dc-link voltage protection handle. + * @param motorErrStatus Motor error status. + * @param udc DC-link voltage feedback (V). + * @retval None. + */ +void OVP_Det(OVP_Handle *ovp, MotorErrStatusReg *motorErrStatus, float udc) +{ + MCS_ASSERT_PARAM(ovp != NULL); + MCS_ASSERT_PARAM(motorErrStatus != NULL); + MCS_ASSERT_PARAM(udc > 0.0f); + /* Check if value goes over threshold for continuous cycles. */ + if (udc < ovp->protValThr[PROT_VAL_THRESHOLD_0]) { + ovp->protCnt = 0; + return; + } + + if (ovp->protCnt < ovp->protCntLimit) { + ovp->protCnt++; + return; + } + + /* Check which protection level should be activated. */ + /* Once enter error status, error status can only be changed by recover api. */ + /* protect level: level 4. */ + if (udc >= ovp->protValThr[PROT_VAL_THRESHOLD_3] && ovp->protLevel < PROT_LEVEL_4) { + ovp->protLevel = PROT_LEVEL_4; + motorErrStatus->Bit.overVoltErr = 1; + ovp->protCnt = 0; + return; + } + + /* Protect level: level 3. */ + if (udc >= ovp->protValThr[PROT_VAL_THRESHOLD_2] && ovp->protLevel < PROT_LEVEL_3) { + ovp->protLevel = PROT_LEVEL_3; + motorErrStatus->Bit.overVoltErr = 1; + ovp->protCnt = 0; + ovp->timer = 0.0f; + return; + } + + /* Protect level: level 2. */ + if (udc >= ovp->protValThr[PROT_VAL_THRESHOLD_1] && ovp->protLevel < PROT_LEVEL_2) { + ovp->protLevel = PROT_LEVEL_2; + motorErrStatus->Bit.overVoltErr = 1; + ovp->protCnt = 0; + ovp->timer = 0.0f; + return; + } + + /* Protect level: level 1. */ + if (udc >= ovp->protValThr[PROT_VAL_THRESHOLD_0] && ovp->protLevel < PROT_LEVEL_1) { + ovp->protLevel = PROT_LEVEL_1; + motorErrStatus->Bit.overVoltErr = 1; + ovp->protCnt = 0; + ovp->timer = 0.0f; + return; + } +} + +/** + * @brief Lower dc-link voltage protection detection. + * @param lvp Lower dc-link voltage protection handle. + * @param motorErrStatus Motor error status. + * @param udc DC-link voltage feedback (V). + * @retval None. + */ +void LVP_Det(LVP_Handle *lvp, MotorErrStatusReg *motorErrStatus, float udc) +{ + MCS_ASSERT_PARAM(lvp != NULL); + MCS_ASSERT_PARAM(motorErrStatus != NULL); + MCS_ASSERT_PARAM(udc > 0.0f); + /* Check if value goes over threshold for continuous cycles. */ + if (udc > lvp->protValThr[PROT_VAL_THRESHOLD_0]) { + lvp->protCnt = 0; + return; + } + + if (lvp->protCnt < lvp->protCntLimit) { + lvp->protCnt++; + return; + } + + /* Check which protection level should be activated. */ + /* Once enter error status, error status can only be changed by recover api. */ + /* protect level: level 4. */ + if (udc <= lvp->protValThr[PROT_VAL_THRESHOLD_3] && lvp->protLevel < PROT_LEVEL_4) { + lvp->protLevel = PROT_LEVEL_4; + motorErrStatus->Bit.lowerVoltErr = 1; + lvp->protCnt = 0; + return; + } + + /* Protect level: level 3. */ + if (udc <= lvp->protValThr[PROT_VAL_THRESHOLD_2] && lvp->protLevel < PROT_LEVEL_3) { + lvp->protLevel = PROT_LEVEL_3; + motorErrStatus->Bit.lowerVoltErr = 1; + lvp->protCnt = 0; + lvp->timer = 0.0f; + return; + } + + /* Protect level: level 2. */ + if (udc <= lvp->protValThr[PROT_VAL_THRESHOLD_1] && lvp->protLevel < PROT_LEVEL_2) { + lvp->protLevel = PROT_LEVEL_2; + motorErrStatus->Bit.lowerVoltErr = 1; + lvp->protCnt = 0; + lvp->timer = 0.0f; + return; + } + + /* Protect level: level 1. */ + if (udc <= lvp->protValThr[PROT_VAL_THRESHOLD_0] && lvp->protLevel < PROT_LEVEL_1) { + lvp->protLevel = PROT_LEVEL_1; + motorErrStatus->Bit.lowerVoltErr = 1; + lvp->protCnt = 0; + lvp->timer = 0.0f; + return; + } +} + +/** + * @brief Over dc-link voltage protection execution. + * @param ovp Over dc-link voltage protection handle. + * @param duty Brake loop output duty (0-1). + * @param aptAddr Three-phase APT address pointer. + * @retval None. + */ +void OVP_Exec(OVP_Handle *ovp, float *duty, APT_RegStruct **aptAddr) +{ + MCS_ASSERT_PARAM(ovp != NULL); + MCS_ASSERT_PARAM(duty != NULL); + MCS_ASSERT_PARAM(aptAddr != NULL); + /* According to protect level, take corresponding action. */ + switch (ovp->protLevel) { + /* level 4: brake loop duty maximum. */ + case PROT_LEVEL_4: + *duty = PROT_OVER_VOLT_BRK_DUTY4; + /* Disable three-phase pwm output. */ + ProtSpo_Exec(aptAddr); + break; + + /* level 3: brake loop duty level 3. */ + case PROT_LEVEL_3: + *duty = PROT_OVER_VOLT_BRK_DUTY2; + ovp->timer += ovp->ts; + if (ovp->timer > ovp->protLimitTime[PROT_LIMIT_TIME_2]) { + *duty = PROT_OVER_VOLT_BRK_DUTY3; + } + break; + + /* level 2: brake loop duty level 2. */ + case PROT_LEVEL_2: + *duty = PROT_OVER_VOLT_BRK_DUTY1; + ovp->timer += ovp->ts; + if (ovp->timer > ovp->protLimitTime[PROT_LIMIT_TIME_1]) { + *duty = PROT_OVER_VOLT_BRK_DUTY2; + } + break; + + /* level 1: brake loop duty level 1. */ + case PROT_LEVEL_1: + ovp->timer += ovp->ts; + if (ovp->timer > ovp->protLimitTime[PROT_LIMIT_TIME_0]) { + *duty = PROT_OVER_VOLT_BRK_DUTY1; + } + break; + + /* level 0: take no protection action. */ + case PROT_LEVEL_0: + break; + + default: + break; + } + return; +} + +/** + * @brief Lower dc-link voltage protection execution. + * @param lvp Lower dc-link voltage protection handle. + * @param spdRef Speed Reference (Hz). + * @param aptAddr Three-phase APT address pointer. + * @retval None. + */ +void LVP_Exec(LVP_Handle *lvp, float *spdRef, APT_RegStruct **aptAddr) +{ + MCS_ASSERT_PARAM(lvp != NULL); + MCS_ASSERT_PARAM(spdRef != NULL); + MCS_ASSERT_PARAM(aptAddr != NULL); + /* According to protect level, take corresponding action. */ + switch (lvp->protLevel) { + /* level 4: disable all PWM output. */ + case PROT_LEVEL_4: + /* Disable three-phase pwm output. */ + ProtSpo_Exec(aptAddr); + *spdRef *= 0.0f; + break; + + /* level 3: derate speed reference. */ + case PROT_LEVEL_3: + *spdRef *= PROT_POW_DN2_PCT; + lvp->timer += lvp->ts; + if (lvp->timer > lvp->protLimitTime[PROT_LIMIT_TIME_2]) { + *spdRef *= PROT_POW_DN3_PCT; + } + break; + + /* level 2: derate speed reference. */ + case PROT_LEVEL_2: + *spdRef *= PROT_POW_DN1_PCT; + lvp->timer += lvp->ts; + if (lvp->timer > lvp->protLimitTime[PROT_LIMIT_TIME_1]) { + *spdRef *= PROT_POW_DN2_PCT; + } + break; + + /* level 1: derate speed reference. */ + case PROT_LEVEL_1: + lvp->timer += lvp->ts; + if (lvp->timer > lvp->protLimitTime[PROT_LIMIT_TIME_0]) { + *spdRef *= PROT_POW_DN1_PCT; + } + break; + + /* level 0: take no protection action. */ + case PROT_LEVEL_0: + break; + + default: + break; + } + return; +} + +/** + * @brief Over dc-link voltage protection recovery. + * @param ovp Over dc-link voltage protection handle. + * @param motorErrStatus Motor error status. + * @param udc DC-link voltage (V). + * @retval None. + */ +void OVP_Recy(OVP_Handle *ovp, MotorErrStatusReg *motorErrStatus, float udc) +{ + MCS_ASSERT_PARAM(ovp != NULL); + /* If not under error state, just return without any operation. */ + if (!motorErrStatus->Bit.overVoltErr) { + return; + } + + /* According to protection level, take corresponding recovery action. */ + switch (ovp->protLevel) { + /* level 4 */ + case PROT_LEVEL_4: + if (udc < ovp->protValThr[PROT_VAL_THRESHOLD_3] - ovp->recyDelta) { + ovp->recyCnt++; + if (ovp->recyCnt > ovp->recyCntLimit) { + ovp->protLevel = PROT_LEVEL_3; + ovp->recyCnt = 0; + } + } + break; + + /* level 3 */ + case PROT_LEVEL_3: + /* If the dc-link voltage is less than threshold 2, level-2 protection is restored. */ + if (udc < ovp->protValThr[PROT_VAL_THRESHOLD_2] - ovp->recyDelta) { + ovp->recyCnt++; + if (ovp->recyCnt > ovp->recyCntLimit) { + ovp->protLevel = PROT_LEVEL_2; + ovp->recyCnt = 0; + } + } + break; + + /* level 2 */ + case PROT_LEVEL_2: + /* If the dc-link voltage is less than threshold 1, level-1 protection is restored. */ + if (udc < ovp->protValThr[PROT_VAL_THRESHOLD_1] - ovp->recyDelta) { + ovp->recyCnt++; + if (ovp->recyCnt > ovp->recyCntLimit) { + ovp->protLevel = PROT_LEVEL_1; + ovp->recyCnt = 0; + } + } + break; + + /* level 1 */ + case PROT_LEVEL_1: + /* If the dc-link voltage is less than threshold 0, level-0 protection is restored. */ + if (udc < ovp->protValThr[PROT_VAL_THRESHOLD_0] - ovp->recyDelta) { + ovp->recyCnt++; + if (ovp->recyCnt > ovp->recyCntLimit) { + ovp->protLevel = PROT_LEVEL_0; + ovp->recyCnt = 0; + } + } + break; + /* level 0 Fault-free state. */ + case PROT_LEVEL_0: + motorErrStatus->Bit.overVoltErr = 0; + break; + + default: + break; + } + return; +} + +/** + * @brief Lower dc-link voltage protection recovery. + * @param lvp Lower dc-link voltage protection handle. + * @param motorErrStatus Motor error status. + * @param udc DC-link voltage (V). + * @retval None. + */ +void LVP_Recy(LVP_Handle *lvp, MotorErrStatusReg *motorErrStatus, float udc) +{ + MCS_ASSERT_PARAM(lvp != NULL); + /* If not under error state, just return without any operation. */ + if (!motorErrStatus->Bit.lowerVoltErr) { + return; + } + + /* According to protection level, take corresponding recovery action. */ + switch (lvp->protLevel) { + /* level 4 */ + case PROT_LEVEL_4: + /* If the dc-link voltage is greater than threshold 3, level-3 protection is restored. */ + if (udc > lvp->protValThr[PROT_VAL_THRESHOLD_3] + lvp->recyDelta) { + lvp->recyCnt++; + if (lvp->recyCnt > lvp->recyCntLimit) { + lvp->protLevel = PROT_LEVEL_3; + lvp->recyCnt = 0; + } + } + break; + + /* level 3 */ + case PROT_LEVEL_3: + /* If the dc-link voltage is greater than threshold 2, level-2 protection is restored. */ + if (udc > lvp->protValThr[PROT_VAL_THRESHOLD_2] + lvp->recyDelta) { + lvp->recyCnt++; + if (lvp->recyCnt > lvp->recyCntLimit) { + lvp->protLevel = PROT_LEVEL_2; + lvp->recyCnt = 0; + } + } + break; + + /* level 2 */ + case PROT_LEVEL_2: + /* If the dc-link voltage is greater than threshold 1, level-1 protection is restored. */ + if (udc > lvp->protValThr[PROT_VAL_THRESHOLD_1] + lvp->recyDelta) { + lvp->recyCnt++; + if (lvp->recyCnt > lvp->recyCntLimit) { + lvp->protLevel = PROT_LEVEL_1; + lvp->recyCnt = 0; + } + } + break; + + /* level 1 */ + case PROT_LEVEL_1: + /* If the dc-link voltage is greater than threshold 0, level-0 protection is restored. */ + if (udc > lvp->protValThr[PROT_VAL_THRESHOLD_0] + lvp->recyDelta) { + lvp->recyCnt++; + if (lvp->recyCnt > lvp->recyCntLimit) { + lvp->protLevel = PROT_LEVEL_0; + lvp->recyCnt = 0; + } + } + break; + /* level 0 Fault-free state. */ + case PROT_LEVEL_0: + motorErrStatus->Bit.lowerVoltErr = 0; + break; + + default: + break; + } + return; +} + +/** + * @brief Over dc-link voltage protection error status clear. + * @param ovp Over voltage protection handle. + * @retval None. + */ +void OVP_Clear(OVP_Handle *ovp) +{ + MCS_ASSERT_PARAM(ovp != NULL); + /* Clear the history value. */ + ovp->protCnt = 0; + ovp->protLevel = PROT_LEVEL_0; + ovp->recyCnt = 0; + ovp->timer = 0.0f; +} + +/** + * @brief Lower dc-link voltage protection error status clear. + * @param lvp Lower voltage protection handle. + * @retval None. + */ +void LVP_Clear(LVP_Handle *lvp) +{ + MCS_ASSERT_PARAM(lvp != NULL); + /* Clear the history value. */ + lvp->protCnt = 0; + lvp->protLevel = PROT_LEVEL_0; + lvp->recyCnt = 0; + lvp->timer = 0.0f; +} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_dc_volt_prot.h b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_dc_volt_prot.h new file mode 100644 index 00000000..0435156a --- /dev/null +++ b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_dc_volt_prot.h @@ -0,0 +1,69 @@ +/** + * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_dc_volt_prot.h + * @author MCU Algorithm Team + * @brief This file contains dc-link voltage protection data struct and api declaration. + */ + +/* Define to prevent recursive inclusion ------------------------------------------------------- */ +#ifndef McuMagicTag_MCS_DC_VOLT_PROT_H +#define McuMagicTag_MCS_DC_VOLT_PROT_H + +/* Includes ------------------------------------------------------------------------------------ */ +#include "mcs_prot_cmm.h" +#include "apt_ip.h" + +typedef struct { + unsigned int protCnt; + unsigned int recyCnt; + unsigned int protCntLimit; + unsigned int recyCntLimit; + float protValThr[PROT_VAL_THRESHOLD_NUMS]; /* from low voltage level to high. */ + float protLimitTime[PROT_LIMIT_TIME_NUMS]; /* from level 3 to level 1. */ + float recyDelta; + float timer; + float ts; + PROT_Level protLevel; +} OVP_Handle; + +typedef struct { + unsigned int protCnt; + unsigned int recyCnt; + unsigned int protCntLimit; + unsigned int recyCntLimit; + float protValThr[PROT_VAL_THRESHOLD_NUMS]; /* from high voltage level to low. */ + float protLimitTime[PROT_LIMIT_TIME_NUMS]; /* from level 3 to level 1. */ + float recyDelta; + float timer; + float ts; + PROT_Level protLevel; +} LVP_Handle; + +void OVP_Init(OVP_Handle *ovp, float ts); +void OVP_Det(OVP_Handle *ovp, MotorErrStatusReg *motorErrStatus, float udc); +void OVP_Exec(OVP_Handle *ovp, float *duty, APT_RegStruct **aptAddr); +void OVP_Recy(OVP_Handle *ovp, MotorErrStatusReg *motorErrStatus, float udc); +void OVP_Clear(OVP_Handle *ovp); + +void LVP_Init(LVP_Handle *lvp, float ts); +void LVP_Det(LVP_Handle *lvp, MotorErrStatusReg *motorErrStatus, float udc); +void LVP_Exec(LVP_Handle *lvp, float *spdRef, APT_RegStruct **aptAddr); +void LVP_Recy(LVP_Handle *lvp, MotorErrStatusReg *motorErrStatus, float udc); +void LVP_Clear(LVP_Handle *lvp); + +#endif diff --git a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_motor_stalling.c b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_motor_stalling.c new file mode 100644 index 00000000..9dee4d2d --- /dev/null +++ b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_motor_stalling.c @@ -0,0 +1,112 @@ +/** + * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_motor_stalling.c + * @author MCU Algorithm Team + * @brief This file contains motor stalling protection data struct and api definition. + */ + +/* Includes ------------------------------------------------------------------------------------ */ +#include "mcs_motor_stalling.h" +#include "mcs_prot_user_config.h" +#include "mcs_math.h" +#include "mcs_math_const.h" +#include "mcs_assert.h" + +/** + * @brief Initilization motor stalling protection function. + * @param stall Motor stalling handle. + * @param ts Ctrl period (s). + * @param currLimit The current amplitude that triggers fault. (A). + * @param spdLimit The speed amplitude that triggers fault. (Hz). + * @param timeLimit The threshold time that current amplitude over the limit (s). + * @retval None. + */ +void STP_Init(STP_Handle *stall, float ts, float currLimit, float spdLimit, float timeLimit) +{ + MCS_ASSERT_PARAM(stall != NULL); + MCS_ASSERT_PARAM(ts > 0.0f); + MCS_ASSERT_PARAM(currLimit > 0.0f); + MCS_ASSERT_PARAM(spdLimit > 0.0f); + MCS_ASSERT_PARAM(timeLimit > 0.0f); + /* Configuring parameters for stalling detection. */ + stall->ts = ts; + /* Current threshold and speed threshold for stalling fault. */ + stall->currAmpLimit = currLimit; + stall->spdLimit = spdLimit; + stall->timeLimit = timeLimit; + stall->timer = 0.0f; +} + +/** + * @brief Motor stalling detection. + * @param stall Motor stalling handle. + * @param motorErrStatus Motor error status. + * @param spd Speed feedback (Hz). + * @param idq Dq-axis current feedback (A). + * @retval None. + */ +void STP_Det_ByCurrSpd(STP_Handle *stall, MotorErrStatusReg *motorErrStatus, float spd, DqAxis idq) +{ + MCS_ASSERT_PARAM(stall != NULL); + MCS_ASSERT_PARAM(motorErrStatus != NULL); + MCS_ASSERT_PARAM(Abs(spd) >= 0.0f); + /* Calculate current amplitude. */ + float currAmp = Sqrt(idq.d * idq.d + idq.q * idq.q); + float spdAbs = Abs(spd); + /* Check if value goes over threshold for continuous cycles. */ + if (currAmp < stall->currAmpLimit || spdAbs > stall->spdLimit) { + stall->timer = 0.0f; + return; + } + /* Time accumulation. */ + if (stall->timer < stall->timeLimit) { + stall->timer += stall->ts; + return; + } + motorErrStatus->Bit.motorStalling = 1; +} + +/** + * @brief Motor stalling protection execution. + * @param motorErrStatus Motor error status. + * @param aptAddr Three-phase APT address pointer. + * @retval None. + */ +void STP_Exec(MotorErrStatusReg *motorErrStatus, APT_RegStruct **aptAddr) +{ + MCS_ASSERT_PARAM(motorErrStatus != NULL); + MCS_ASSERT_PARAM(aptAddr != NULL); + if (motorErrStatus->Bit.motorStalling == 0) { + return; + } + + /* Disable three-phase pwm output. */ + ProtSpo_Exec(aptAddr); + return; +} + +/** + * @brief Motor stalling protection error status clear. + * @param stall Motor stalling handle. + * @retval None. + */ +void STP_Clear(STP_Handle *stall) +{ + MCS_ASSERT_PARAM(stall != NULL); + stall->timer = 0.0f; +} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_motor_stalling.h b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_motor_stalling.h new file mode 100644 index 00000000..a1d1e4be --- /dev/null +++ b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_motor_stalling.h @@ -0,0 +1,45 @@ +/** + * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_motor_stalling.h + * @author MCU Algorithm Team + * @brief This file contains motor stalling protection data struct and api declaration. + */ + +/* Define to prevent recursive inclusion ------------------------------------- */ +#ifndef McuMagicTag_MCS_MOTOR_STALLING_PROT_H +#define McuMagicTag_MCS_MOTOR_STALLING_PROT_H + +/* Includes ------------------------------------------------------------------------------------ */ +#include "mcs_prot_cmm.h" +#include "apt_ip.h" +#include "mcs_typedef.h" + +typedef struct { + float currAmpLimit; /**< Feedback current higher than this value triggers fault. (A). */ + float spdLimit; /**< Feedback speed lower than this value triggers fault (Hz). */ + float timeLimit; /**< The threshold time that current and speed feedback over ranges (s). */ + float timer; /**< Timer to get speed and current over range time. */ + float ts; /**< Ctrl period (s). */ +} STP_Handle; + +void STP_Init(STP_Handle *stall, float ts, float currLimit, float spdLimit, float timeLimit); +void STP_Det_ByCurrSpd(STP_Handle *stall, MotorErrStatusReg *motorErrStatus, float spd, DqAxis idq); +void STP_Exec(MotorErrStatusReg *motorErrStatus, APT_RegStruct **aptAddr); +void STP_Clear(STP_Handle *stall); + +#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_temp_prot.c b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_temp_prot.c new file mode 100644 index 00000000..184ba4a3 --- /dev/null +++ b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_temp_prot.c @@ -0,0 +1,262 @@ +/** + * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_temp_prot.c + * @author MCU Algorithm Team + * @brief This file contains over temperature protection api definition. + */ + +/* Includes ------------------------------------------------------------------------------------ */ +#include "mcs_temp_prot.h" +#include "mcs_prot_user_config.h" +#include "mcs_assert.h" + +/** + * @brief Initilization over temperation protection function. + * @param otp Over temperature protection handle. + * @param ts Ctrl period. + * @retval None. + */ +void OTP_Init(OTP_Handle *otp, float ts) +{ + MCS_ASSERT_PARAM(otp != NULL); + MCS_ASSERT_PARAM(ts > 0.0f); + otp->ts = ts; + otp->protCntLimit = PROT_CNT_LIMIT; + otp->recyCntLimit = RECY_CNT_LIMIT; + /* Configuring the temperature protection threshold. */ + otp->protValThr[PROT_VAL_THRESHOLD_0] = PROT_OVER_IPM_TEMP_POW_DN1; + otp->protValThr[PROT_VAL_THRESHOLD_1] = PROT_OVER_IPM_TEMP_POW_DN2; + otp->protValThr[PROT_VAL_THRESHOLD_2] = PROT_OVER_IPM_TEMP_POW_DN3; + otp->protValThr[PROT_VAL_THRESHOLD_3] = PROT_OVER_IPM_TEMP_POW_OFF; + /* Configuring the protection limiting time. */ + otp->protLimitTime[PROT_LIMIT_TIME_0] = PROT_OVER_TEMP_LIMIT1_TIME_SEC; + otp->protLimitTime[PROT_LIMIT_TIME_1] = PROT_OVER_TEMP_LIMIT2_TIME_SEC; + otp->protLimitTime[PROT_LIMIT_TIME_2] = PROT_OVER_TEMP_LIMIT3_TIME_SEC; + otp->recyDelta = PROT_OVER_IPM_TEMP_RECY_DELTA; + OTP_Clear(otp); +} + +/** + * @brief Over temperatre protection detection. + * @param otp Over temperature protection handle. + * @param tempErrBit Temperature error status bit. + * @param idq DQ-axis feedback currents. + * @retval None. + */ +void OTP_Det(OTP_Handle *otp, MotorErrStatusReg *motorErrStatus, PROT_ErrBit protBit, float temp) +{ + MCS_ASSERT_PARAM(otp != NULL); + MCS_ASSERT_PARAM(motorErrStatus != NULL); + MCS_ASSERT_PARAM(temp > 0.0f); + /* Check if value goes over threshold for continuous cycles. */ + if (temp < otp->protValThr[PROT_VAL_THRESHOLD_0]) { + otp->protCnt = 0; + ClearBit(&motorErrStatus->all, protBit); + return; + } + + if (otp->protCnt < otp->protCntLimit) { + otp->protCnt++; + return; + } + + /* Check which protection level should be activated. */ + /* Once enter error status, error status can only be changed by recover api. */ + /* protect level: level 4. */ + if (temp >= otp->protValThr[PROT_VAL_THRESHOLD_3] && otp->protLevel < PROT_LEVEL_4) { + otp->protLevel = PROT_LEVEL_4; + SetBit(&motorErrStatus->all, protBit); + otp->protCnt = 0; + return; + } + + /* Protect level: level 3. */ + if (temp >= otp->protValThr[PROT_VAL_THRESHOLD_2] && otp->protLevel < PROT_LEVEL_3) { + otp->protLevel = PROT_LEVEL_3; + SetBit(&motorErrStatus->all, protBit); + otp->protCnt = 0; + otp->timer = 0.0f; + return; + } + + /* Protect level: level 2. */ + if (temp >= otp->protValThr[PROT_VAL_THRESHOLD_1] && otp->protLevel < PROT_LEVEL_2) { + otp->protLevel = PROT_LEVEL_2; + SetBit(&motorErrStatus->all, protBit); + otp->protCnt = 0; + otp->timer = 0.0f; + return; + } + + /* Protect level: level 1. */ + if (temp >= otp->protValThr[PROT_VAL_THRESHOLD_0] && otp->protLevel < PROT_LEVEL_1) { + otp->protLevel = PROT_LEVEL_1; + SetBit(&motorErrStatus->all, protBit); + otp->protCnt = 0; + otp->timer = 0.0f; + return; + } +} + +/** + * @brief Over temperature protection execution. + * @param otp Over temperature protection handle. + * @param spdRef Speed reference (Hz). + * @param aptAddr Three-phase APT address pointer. + * @retval None. + */ +void OTP_Exec(OTP_Handle *otp, float *spdRef, APT_RegStruct **aptAddr) +{ + MCS_ASSERT_PARAM(otp != NULL); + MCS_ASSERT_PARAM(spdRef != NULL); + MCS_ASSERT_PARAM(aptAddr != NULL); + /* According to protect level, take corresponding action. */ + switch (otp->protLevel) { + /* level 4: disable all PWM output. */ + case PROT_LEVEL_4: + /* Disable three-phase pwm output. */ + ProtSpo_Exec(aptAddr); + *spdRef *= 0.0f; + break; + + /* level 3: derate speed reference. */ + case PROT_LEVEL_3: + *spdRef *= PROT_POW_DN2_PCT; + otp->timer += otp->ts; + if (otp->timer > otp->protLimitTime[PROT_LIMIT_TIME_2]) { + *spdRef *= PROT_POW_DN3_PCT; + } + break; + + /* level 2: derate speed reference. */ + case PROT_LEVEL_2: + /* Reducte motor speed to level 2 */ + *spdRef *= PROT_POW_DN1_PCT; + otp->timer += otp->ts; + if (otp->timer > otp->protLimitTime[PROT_LIMIT_TIME_1]) { + *spdRef *= PROT_POW_DN2_PCT; + } + break; + + /* level 0: take no protection action. */ + case PROT_LEVEL_0: + break; + + case PROT_LEVEL_1: + /* Reducte motor speed to level 0 */ + otp->timer += otp->ts; + if (otp->timer > otp->protLimitTime[PROT_LIMIT_TIME_0]) { + /* level 1: derate speed reference. */ + *spdRef *= PROT_POW_DN1_PCT; + } + break; + + default: + break; + } + return; +} + +/** + * @brief Over temperature protection recovery. + * @param otp Over temperature protection handle. + * @param tempErrBit Temperature error status bit. + * @param temp Temperature (celsius). + * @retval None. + */ +void OTP_Recy(OTP_Handle *otp, MotorErrStatusReg *motorErrStatus, PROT_ErrBit protBit, float temp) +{ + MCS_ASSERT_PARAM(otp != NULL); + MCS_ASSERT_PARAM(motorErrStatus != NULL); + MCS_ASSERT_PARAM(temp > 0.0f); + /* If not under error state, just return without any operation. */ + if (otp->protLevel == PROT_LEVEL_0) { + motorErrStatus->all &= (~(1 >> protBit)); + return; + } + + /* According to protection level, take corresponding recovery action. */ + switch (otp->protLevel) { + /* level 4 */ + case PROT_LEVEL_4: + /* If the temperature is less than threshold 3, level-3 protection is restored. */ + if (temp < otp->protValThr[PROT_VAL_THRESHOLD_3] - otp->recyDelta) { + otp->recyCnt++; + if (otp->recyCnt > otp->recyCntLimit) { + otp->protLevel = PROT_LEVEL_3; + otp->recyCnt = 0; + } + } + break; + + /* level 3 */ + case PROT_LEVEL_3: + /* If the temperature is less than threshold 2, level-2 protection is restored. */ + if (temp < otp->protValThr[PROT_VAL_THRESHOLD_2] - otp->recyDelta) { + otp->recyCnt++; + if (otp->recyCnt > otp->recyCntLimit) { + otp->protLevel = PROT_LEVEL_2; + otp->recyCnt = 0; + } + } + break; + + /* level 2 */ + case PROT_LEVEL_2: + /* If the temperature is less than threshold 1, level-1 protection is restored. */ + if (temp < otp->protValThr[PROT_VAL_THRESHOLD_1] - otp->recyDelta) { + otp->recyCnt++; + if (otp->recyCnt > otp->recyCntLimit) { + otp->protLevel = PROT_LEVEL_1; + otp->recyCnt = 0; + } + } + break; + + /* level 1 */ + case PROT_LEVEL_1: + /* If the temperature is less than threshold 0, level-0 protection is restored. */ + if (temp < otp->protValThr[PROT_VAL_THRESHOLD_0] - otp->recyDelta) { + otp->recyCnt++; + if (otp->recyCnt > otp->recyCntLimit) { + otp->protLevel = PROT_LEVEL_0; + otp->recyCnt = 0; + } + } + break; + + default: + break; + } + return; +} + +/** + * @brief Over temperature protection error status clear. + * @param otp Over temperature protection handle. + * @retval None. + */ +void OTP_Clear(OTP_Handle *otp) +{ + MCS_ASSERT_PARAM(otp != NULL); + /* Clear the history value. */ + otp->protCnt = 0; + otp->protLevel = PROT_LEVEL_0; + otp->recyCnt = 0; + otp->timer = 0.0f; +} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_temp_prot.h b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_temp_prot.h new file mode 100644 index 00000000..2b9d99a7 --- /dev/null +++ b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_temp_prot.h @@ -0,0 +1,49 @@ +/** + * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_temp_prot.h + * @author MCU Algorithm Team + * @brief his file contains over temperature protection api declaration. + */ + +/* Define to prevent recursive inclusion ------------------------------------- */ +#ifndef McuMagicTag_MCS_TEMP_PROT_H +#define McuMagicTag_MCS_TEMP_PROT_H + +/* Includes ------------------------------------------------------------------------------------ */ +#include "mcs_prot_cmm.h" +#include "apt_ip.h" + +typedef struct { + unsigned int protCnt; + unsigned int recyCnt; + unsigned int protCntLimit; + unsigned int recyCntLimit; + float protValThr[PROT_VAL_THRESHOLD_NUMS]; /* from low current level to high. */ + float protLimitTime[PROT_LIMIT_TIME_NUMS]; /* from level 3 to level 1. */ + float recyDelta; + float ts; + float timer; + PROT_Level protLevel; +} OTP_Handle; + +void OTP_Init(OTP_Handle *otp, float ts); +void OTP_Det(OTP_Handle *otp, MotorErrStatusReg *motorErrStatus, PROT_ErrBit protBit, float temp); +void OTP_Exec(OTP_Handle *otp, float *spdRef, APT_RegStruct **aptAddr); +void OTP_Recy(OTP_Handle *otp, MotorErrStatusReg *motorErrStatus, PROT_ErrBit protBit, float temp); +void OTP_Clear(OTP_Handle *otp); +#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/readme.md b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/readme.md new file mode 100644 index 00000000..98b2a068 --- /dev/null +++ b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/readme.md @@ -0,0 +1,10 @@ +# pmsm_sensorless_2shunt_foc + +**【功能描述】** ++ 基于ECMCU105H/ECBMCU201MPC单板的单电机双电阻采样Foc应用 + +**【环境要求】** ++ 所有单板电源改制为演示用的12V低压,电机选用Gimbal GBM2804H-100T + +**【IDE配置方法】** ++ chipConfig中的Sample栏目里面选中pmsm sensorless 2shunt foc示例,然后点击生成代码即可 diff --git a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/src/mcs_carrier.c b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/src/mcs_carrier.c new file mode 100644 index 00000000..9fc8afac --- /dev/null +++ b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/src/mcs_carrier.c @@ -0,0 +1,144 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_carrier.c + * @author MCU Algorithm Team + * @brief This file provides carrier interrupt application for motor control. + */ + +#include "mcs_carrier.h" +#include "mcs_math.h" +#include "typedefs.h" +#include "mcs_assert.h" +#include "mcs_user_config.h" +#include "mcs_ctlmode_config.h" + +/** + * @brief Synchronous rotation coordinate system angle. + * @param mtrCtrl The motor control handle. + * @retval None. + */ +static void MCS_SyncCoorAngle(MTRCTRL_Handle *mtrCtrl) +{ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + /* Synchronous rotation coordinate system angle. */ + switch (mtrCtrl->stateMachine) { + case FSM_STARTUP: + /* Current ramp angle is 0. */ + if (mtrCtrl->startup.stage == STARTUP_STAGE_CURR) { + mtrCtrl->axisAngle = 0; + } else if (mtrCtrl->startup.stage == STARTUP_STAGE_SPD) { /* IF control phase angle self-addition. */ + mtrCtrl->axisAngle = IF_CurrAngleCalc(&mtrCtrl->ifCtrl, mtrCtrl->spdRefHz); + } else if (mtrCtrl->startup.stage == STARTUP_STAGE_SWITCH) { /* Switch Angle */ + mtrCtrl->axisAngle = mtrCtrl->smo.elecAngle; + } + break; + + case FSM_RUN: + mtrCtrl->axisAngle = mtrCtrl->smo.elecAngle; + break; + + default: + mtrCtrl->axisAngle = 0; + break; + } +} + +/** + * @brief PWM waveform setting and sampling point setting for a single resistors and dual resistors. + * @param mtrCtrl The motor control handle. + * @retval None. + */ +static void MCS_PwmAdcSet(MTRCTRL_Handle *mtrCtrl) +{ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + SampleMode sampleMode = mtrCtrl->sampleMode; + /* Set the duty cycle according to the sampling mode. */ + if (sampleMode == DUAL_RESISTORS) { + SVPWM_Exec(&mtrCtrl->sv, &mtrCtrl->vabRef, &mtrCtrl->dutyUvw); + mtrCtrl->setPwmDutyCb(&mtrCtrl->dutyUvw, &mtrCtrl->dutyUvw); + } else if (sampleMode == SINGLE_RESISTOR) { + R1SVPWM_Exec(&mtrCtrl->r1Sv, &mtrCtrl->vabRef, &mtrCtrl->dutyUvwLeft, &mtrCtrl->dutyUvwRight); + mtrCtrl->setPwmDutyCb(&mtrCtrl->dutyUvwLeft, &mtrCtrl->dutyUvwRight); + /* The ADC sampling point position needs to be set based on the phase shift of a single resistors. */ + mtrCtrl->setADCTriggerTimeCb(mtrCtrl->r1Sv.samplePoint[0] * mtrCtrl->aptMaxcntCmp, \ + mtrCtrl->r1Sv.samplePoint[1] * mtrCtrl->aptMaxcntCmp); + } +} + +/** + * @brief Carrier interrupt function. + * @param mtrCtrl The motor control handle. + * @retval None. + */ +void MCS_CarrierProcess(MTRCTRL_Handle *mtrCtrl) +{ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + UvwAxis *currUvw = &mtrCtrl->currUvw; + AlbeAxis *currAlbe = &mtrCtrl->iabFbk; + AlbeAxis *vab = &mtrCtrl->vabRef; + SampleMode sampleMode = mtrCtrl->sampleMode; + /* sample mode verify */ + if (sampleMode > SINGLE_RESISTOR || mtrCtrl->setADCTriggerTimeCb == NULL) { + return; + } + /* param verify */ + if (mtrCtrl->setPwmDutyCb == NULL || mtrCtrl->readCurrUvwCb == NULL) { + return; + } + /* Read the three-phase current value. */ + mtrCtrl->readCurrUvwCb(currUvw); + /* Clark Calc */ + ClarkeCalc(currUvw, currAlbe); + /* Smo observation */ + if (mtrCtrl->obserType == FOC_OBSERVERTYPE_SMO1TH) { + /* Smo observation */ + FOSMO_Exec(&mtrCtrl->smo, currAlbe, vab, mtrCtrl->spdRefHz); + } else if (mtrCtrl->obserType == FOC_OBSERVERTYPE_SMO4TH) { + /* Smo4th observation */ + SMO4TH_Exec(&mtrCtrl->smo4th, currAlbe, vab); + mtrCtrl->smo.spdEst = mtrCtrl->smo4th.spdEst; + mtrCtrl->smo.elecAngle = mtrCtrl->smo4th.elecAngle; + } + /* Synchronization angle */ + MCS_SyncCoorAngle(mtrCtrl); + + /* Park transformation */ + ParkCalc(currAlbe, mtrCtrl->axisAngle, &mtrCtrl->idqFbk); + + /* statemachine */ + switch (mtrCtrl->stateMachine) { + case FSM_STARTUP: + case FSM_RUN: + CURRCTRL_Exec(&mtrCtrl->currCtrl, &mtrCtrl->vdqRef, mtrCtrl->smo.spdEst, 0); + InvParkCalc(&mtrCtrl->vdqRef, mtrCtrl->axisAngle, vab); + MCS_PwmAdcSet(mtrCtrl); + break; + + case FSM_CAP_CHARGE: + case FSM_CLEAR: + case FSM_IDLE: + mtrCtrl->smo4th.spdEst = 0.0f; + break; + + default: + vab->alpha = 0.0f; + vab->beta = 0.0f; + MCS_PwmAdcSet(mtrCtrl); + break; + } +} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/src/mcs_motor_process.c b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/src/mcs_motor_process.c new file mode 100644 index 00000000..79bc3949 --- /dev/null +++ b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/src/mcs_motor_process.c @@ -0,0 +1,950 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_motor_process.c + * @author MCU Algorithm Team + * @brief This file provides motor application. + * @details Single FOC application based on the ECMCU105H/ECBMCU201MPC board + * 1) Motor model is Gimbal GBM2804H-100T. + * 2) Select the pmsm sensorless 2shunt foc example in the sample column + of chipConfig and click Generate Code. + * 3) It's power supply must be changed to 12V. + */ +#include "main.h" +#include "mcs_user_config.h" +#include "mcs_math.h" +#include "hmi_module.h" +#include "mcs_ctlmode_config.h" +#include "mcs_prot_user.h" +#include "mcs_prot_user_config.h" +#include "mcs_math_const.h" +#include "mcs_motor_process.h" +#include "mcs_chip_config.h" +#include + +/*------------------------------- Macro Definition -----------------------------------------------*/ +#define US_PER_MS 1000 +#define ANGLE_RANGE_ABS 65536 +#define ANGLE_360_F 65536.0f /* 0 - 65536 indicates 0 to 360. */ +#define APT_FULL_DUTY 1.0f +#define TEMP_3 3.0f +#define TEMP_15 15.0f +#define TEMP_30 30.0f +#define TEMP_45 45.0f +#define TEMP_60 60.0f +#define TEMP_RES_15 78.327f +#define TEMP_RES_30 36.776f +#define TEMP_RES_45 18.301f +#define TEMP_RES_60 9.607f +#define CNT_10 10 +#define CNT_5000 5000 +#define LEVEL_4 4 +#define MOTOR_START_DELAY 2 +#define ADC_READINIT_DELAY 1 +#define ADC_READINIT_TIMES 20 +#define ADC_TRIMVALUE_MIN 1800.0f +#define ADC_TRIMVALUE_MAX 2200.0f +/*------------------------------- Param Definition -----------------------------------------------*/ +/* Motor parameters. */ +/* Np, Rs, Ld, Lq, Psif, J, Nmax, Currmax, PPMR, zShift */ +static MOTOR_Param g_motorParam = MOTORPARAM_DEFAULTS; +static APT_RegStruct* g_apt[PHASE_MAX_NUM] = {APT_U, APT_V, APT_W}; +/* Motor control handle */ +static MTRCTRL_Handle g_mc = {0}; + +/* Motor speed loop PI param. */ +static void SPDCTRL_InitWrapper(SPDCTRL_Handle *spdHandle, float ts) +{ + /* Speed loop param assignment. */ + PI_Param spdPi = { + .kp = SPD_KP, + .ki = SPD_KI, + .lowerLim = SPD_LOWERLIM, + .upperLim = SPD_UPPERLIM, + }; + /* Speed loop param init. */ + SPDCTRL_Init(spdHandle, &g_motorParam, spdPi, ts); +} + +/* Motor current Loop PI param. */ +static void CURRCTRL_InitWrapper(CURRCTRL_Handle *currHandle, DqAxis *idqRef, DqAxis *idqFbk, float ts) +{ + /* Axis-D current loop param assignment. */ + PI_Param dCurrPi = { + .kp = CURRDAXIS_KP, + .ki = CURRDAXIS_KI, + .lowerLim = CURR_LOWERLIM, + .upperLim = CURR_UPPERLIM, + }; + /* Axis-Q current loop param assignment. */ + PI_Param qCurrPi = { + .kp = CURRQAXIS_KP, + .ki = CURRQAXIS_KI, + .lowerLim = CURR_LOWERLIM, + .upperLim = CURR_UPPERLIM, + }; + /* Current loop param init. */ + CURRCTRL_Init(currHandle, &g_motorParam, idqRef, idqFbk, dCurrPi, qCurrPi, ts); +} + +/* First order smo param. */ +static void FOSMO_InitWrapper(FOSMO_Handle *fosmo, float ts) +{ + /* Smo param assignment. */ + FOSMO_Param fosmoParam = { + .gain = FOSMO_GAIN, + .lambda = FOSMO_LAMBDA, + .fcEmf = FOSMO_EMF_CUTOFF_FREQ, + .fcLpf = SPEED_FILTER_CUTOFF_FREQUENCY, + .pllBdw = FOSMO_PLL_BDW, + }; + /* Init smo param. */ + FOSMO_Init(fosmo, fosmoParam, &g_motorParam, ts); +} + +/* Smo4th param. */ +static void SMO4TH_InitWrapper(SMO4TH_Handle *smo4TH) +{ + /* Smo4th param assignment. */ + SMO4TH_Param smo4thParam = { + .kd = SMO4TH_KD, + .kq = SMO4TH_KQ, + .pllBdw = SMO4TH_PLL_BDW, + .fcLpf = SMO4TH_SPD_FILTER_CUTOFF_FREQ, + }; + /* Init smo param. */ + SMO4TH_Init(smo4TH, smo4thParam, &g_motorParam, CTRL_CURR_PERIOD); +} + +/*------------------------------- Function Definition -----------------------------------------------*/ +/** + * @brief Initialzer of system tick. + * @param mtrCtrl Motor control struct handle. + * @retval None. + */ +static void TimerTickInit(MTRCTRL_Handle *mtrCtrl) +{ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + mtrCtrl->sysTickCnt = 0; + mtrCtrl->msTickNum = US_PER_MS / SYSTICK_PERIOD_US; + mtrCtrl->capChargeTickNum = (INV_CAP_CHARGE_MS * US_PER_MS / SYSTICK_PERIOD_US); +} + +/** + * @brief Init motor control task. + * @retval None. + */ +static void TSK_Init(void) +{ + g_mc.motorStateFlag = 0; + g_mc.uartHeartDetCnt = 0; + g_mc.uartTimeStamp = 0; + g_mc.stateMachine = FSM_IDLE; + g_mc.currCtrlPeriod = CTRL_CURR_PERIOD; /* Init current controller */ + g_mc.aptMaxcntCmp = g_apt0.waveform.timerPeriod; + g_mc.sampleMode = DUAL_RESISTORS; + g_mc.obserType = FOC_OBSERVERTYPE_SMO4TH; /* Init foc observe mode */ + g_mc.controlMode = FOC_CONTROLMODE_SPEED; /* Init motor control mode */ + g_mc.adcCurrCofe = ADC_CURR_COFFI; + g_mc.spdAdjustMode = CUST_SPEED_ADJUST; + g_mc.uartConnectFlag = DISCONNECT; + g_mc.spdCmdHz = USER_MIN_SPD_HZ; /* Motor initialization speed */ + + g_mc.adc0Compensate = ADC0COMPENSATE; /* Phase-u current init adc shift trim value */ + g_mc.adc1Compensate = ADC1COMPENSATE; /* Phase-w current init adc shift trim value */ + + IF_Init(&g_mc.ifCtrl, CTRL_IF_CURR_AMP_A, USER_CURR_SLOPE, CTRL_SYSTICK_PERIOD, CTRL_CURR_PERIOD); + RMG_Init(&g_mc.spdRmg, CTRL_SYSTICK_PERIOD, USER_SPD_SLOPE); /* Init speed slope */ + MtrParamInit(&g_mc.mtrParam, g_motorParam); + + TimerTickInit(&g_mc); + if (g_mc.sampleMode == DUAL_RESISTORS) { + SVPWM_Init(&g_mc.sv, INV_VOLTAGE_BUS * ONE_DIV_SQRT3); /* Dual resistors SVPWM init. */ + } else if (g_mc.sampleMode == SINGLE_RESISTOR) { + /* Single resistor SVPWM init. */ + R1SVPWM_Init(&g_mc.r1Sv, INV_VOLTAGE_BUS * ONE_DIV_SQRT3, SAMPLE_POINT_SHIFT, SAMPLE_WINDOW_DUTY); + } + + SPDCTRL_InitWrapper(&g_mc.spdCtrl, CTRL_SYSTICK_PERIOD); + CURRCTRL_InitWrapper(&g_mc.currCtrl, &g_mc.idqRef, &g_mc.idqFbk, CTRL_CURR_PERIOD); + FOSMO_InitWrapper(&g_mc.smo, CTRL_CURR_PERIOD); + SMO4TH_InitWrapper(&g_mc.smo4th); + + STARTUP_Init(&g_mc.startup, USER_SWITCH_SPDBEGIN_HZ, USER_SWITCH_SPDBEGIN_HZ + TEMP_3); + + MotorProt_Init(&g_mc.prot); /* Init protect state comond */ + OCP_Init(&g_mc.prot.ocp, CTRL_CURR_PERIOD); + OVP_Init(&g_mc.prot.ovp, CTRL_SYSTICK_PERIOD); + LVP_Init(&g_mc.prot.lvp, CTRL_SYSTICK_PERIOD); + OTP_Init(&g_mc.prot.otp, CTRL_SYSTICK_PERIOD); + STP_Init(&g_mc.prot.stall, CTRL_SYSTICK_PERIOD, PROT_STALLING_CURR_AMP_LIMIT, + PROT_STALLING_SPD_LIMIT, PROT_STALLING_TIME_LIMIT); +} + +/** + * @brief Clear historical values of all controller before start-up. + * @param mtrCtrl The motor control handle. + * @retval None. + */ +static void ClearBeforeStartup(MTRCTRL_Handle *mtrCtrl) +{ + /* Verify Parameters */ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + /* The initial angle is 0. */ + mtrCtrl->axisAngle = 0; + + mtrCtrl->spdRefHz = 0.0f; + /* The initial dq-axis reference current is 0. */ + mtrCtrl->idqRef.d = 0.0f; + mtrCtrl->idqRef.q = 0.0f; + + mtrCtrl->vdqRef.d = 0.0f; + mtrCtrl->vdqRef.q = 0.0f; + /* Clear Duty Cycle Value. The initial duty cycle is 0.5. */ + mtrCtrl->dutyUvwLeft.u = 0.5f; + mtrCtrl->dutyUvwLeft.v = 0.5f; + mtrCtrl->dutyUvwLeft.w = 0.5f; + mtrCtrl->dutyUvwRight.u = 0.5f; + mtrCtrl->dutyUvwRight.v = 0.5f; + mtrCtrl->dutyUvwRight.w = 0.5f; + + mtrCtrl->prot.motorErrStatus.all = 0x00; + + RMG_Clear(&mtrCtrl->spdRmg); /* Clear the history value of speed slope control */ + CURRCTRL_Clear(&mtrCtrl->currCtrl); + IF_Clear(&mtrCtrl->ifCtrl); + SPDCTRL_Clear(&mtrCtrl->spdCtrl); + FOSMO_Clear(&mtrCtrl->smo); + SMO4TH_Clear(&mtrCtrl->smo4th); + STARTUP_Clear(&mtrCtrl->startup); + R1SVPWM_Clear(&mtrCtrl->r1Sv); + + OTP_Clear(&mtrCtrl->prot.otp); + OCP_Clear(&mtrCtrl->prot.ocp); + OVP_Clear(&mtrCtrl->prot.ovp); + LVP_Clear(&mtrCtrl->prot.lvp); +} + +/** + * @brief To set the comparison value of the IGBT single-resistance ADC sampling trigger position. + * @param aptx The APT register struct handle. + * @param cntCmpA A Count compare reference of time-base counter. + * @param cntCmpB B Count compare reference of time-base counter. + * @param maxCntCmp Maximum Comparison Value + * @retval None. + */ +static void MCS_SetAdcCompareR1(APT_RegStruct *aptx, unsigned short cntCmpA, + unsigned short cntCmpB, unsigned short maxCntCmp) +{ + unsigned short tmp; + /* Sets the A Count compare reference of time-base counter. */ + tmp = (unsigned short)Clamp((float)(cntCmpA), (float)(maxCntCmp - 1), 1.0f); + DCL_APT_SetCounterCompare(aptx, APT_COMPARE_REFERENCE_A, tmp); + /* Sets the B Count compare reference of time-base counter. */ + tmp = (unsigned short)Clamp((float)(cntCmpB), (float)(maxCntCmp - 1), 1.0f); + DCL_APT_SetCounterCompare(aptx, APT_COMPARE_REFERENCE_B, tmp); +} + +/** + * @brief Open the three-phase lower pipe. + * @param aptAddr Three-phase APT address pointer. + * @param maxDutyCnt Max duty count. + * @retval None. + */ +static void AptTurnOnLowSidePwm(APT_RegStruct **aptAddr, unsigned short maxDutyCnt) +{ + MCS_ASSERT_PARAM(aptAddr != NULL); + MCS_ASSERT_PARAM(maxDutyCnt != 0); + unsigned short dutyCnt; + dutyCnt = (unsigned short)(maxDutyCnt * APT_FULL_DUTY); + /* Open the three-phase lower pipe */ + for (unsigned int i = 0; i < PHASE_MAX_NUM; i++) { + APT_RegStruct *aptx = (APT_RegStruct *)(aptAddr[i]); + DCL_APT_SetCounterCompare(aptx, APT_COMPARE_REFERENCE_C, dutyCnt); + DCL_APT_SetCounterCompare(aptx, APT_COMPARE_REFERENCE_D, dutyCnt); + } +} + +/** + * @brief Enable three-phase pwm output. + * @param aptAddr Three-phase APT address pointer. + * @retval None. + */ +static void MotorPwmOutputEnable(APT_RegStruct **aptAddr) +{ + MCS_ASSERT_PARAM(aptAddr != NULL); + /* Enable three-phase pwm output */ + for (unsigned int i = 0; i < PHASE_MAX_NUM; i++) { + APT_RegStruct *aptx = (APT_RegStruct *)(aptAddr[i]); + aptx->PG_OUT_FRC.BIT.rg_pga_frc_en = BASE_CFG_UNSET; + aptx->PG_OUT_FRC.BIT.rg_pgb_frc_en = BASE_CFG_UNSET; + } +} + +/** + * @brief Disable three-phase pwm output. + * @param aptAddr Three-phase APT address pointer. + * @retval None. + */ +static void MotorPwmOutputDisable(APT_RegStruct **aptAddr) +{ + MCS_ASSERT_PARAM(aptAddr != NULL); + /* Disable three-phase pwm output. */ + for (unsigned int i = 0; i < PHASE_MAX_NUM; i++) { + APT_RegStruct *aptx = (APT_RegStruct *)(aptAddr[i]); + aptx->PG_OUT_FRC.BIT.rg_pga_frc_en = BASE_CFG_SET; + aptx->PG_OUT_FRC.BIT.rg_pgb_frc_en = BASE_CFG_SET; + DCL_APT_ForcePWMOutputLow(aptx); + } +} + +/** + * @brief Smo IF angle difference calculation. + * @param smoElecAngle Smo electrical angle. + * @param ifCtrlAngle IF control angle. + * @retval signed short angle difference. + */ +static float SmoIfAngleDiffCalc(float smoElecAngle, float ifCtrlAngle) +{ + float diff = AngleSub(smoElecAngle, ifCtrlAngle); + /* Smo IF angle difference calculation */ + return diff; +} + +/** + * @brief Construct a new mcs startupswitch object. + * @param mtrCtrl The motor control handle. + * @retval None. + */ +static void MCS_StartupSwitch(MTRCTRL_Handle *mtrCtrl) +{ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + STARTUP_Handle *startup = &mtrCtrl->startup; + DqAxis *idqRef = &mtrCtrl->idqRef; + float iftargetAmp = mtrCtrl->ifCtrl.targetAmp; + float spdRefHz = mtrCtrl->spdRefHz; + + switch (startup->stage) { + case STARTUP_STAGE_CURR: + if (mtrCtrl->ifCtrl.curAmp >= iftargetAmp) { + /* Stage change */ + idqRef->q = iftargetAmp; + startup->stage = STARTUP_STAGE_SPD; + } else { + /* current amplitude increase */ + idqRef->q = IF_CurrAmpCalc(&mtrCtrl->ifCtrl); + spdRefHz = 0.0f; + } + break; + case STARTUP_STAGE_SPD: + /* current frequency increase */ + if (Abs(spdRefHz) >= startup->spdBegin) { + /* Stage change */ + startup->stage = STARTUP_STAGE_SWITCH; + TrigVal localTrigVal; + TrigCalc(&localTrigVal, SmoIfAngleDiffCalc(mtrCtrl->smo.elecAngle, mtrCtrl->ifCtrl.angle)); + idqRef->d = 0.0f; + mtrCtrl->spdCtrl.spdPi.integral = iftargetAmp * localTrigVal.cos; + } else { + /* Speed rmg */ + spdRefHz = RMG_Exec(&mtrCtrl->spdRmg, mtrCtrl->spdCmdHz); + } + break; + + case STARTUP_STAGE_SWITCH: + /* Switch from IF to SMO */ + spdRefHz = RMG_Exec(&mtrCtrl->spdRmg, mtrCtrl->spdCmdHz); + idqRef->q = SPDCTRL_Exec(&mtrCtrl->spdCtrl, mtrCtrl->spdRefHz, mtrCtrl->smo.spdEst); + /* Transitional stage, if current reference speed > critical speed, change to next stage */ + if (spdRefHz >= startup->spdBegin + TEMP_3) { + /* Stage change */ + mtrCtrl->stateMachine = FSM_RUN; + } + break; + + default: + break; + } + mtrCtrl->spdRefHz = spdRefHz; +} + +/** + * @brief Pre-processing of motor status. + * @param statusReg System status. + * @param stateMachine Motor Control Status. + * @retval None. + */ +static void MotorStatePerProc(SysStatusReg *statusReg, volatile FsmState *stateMachine) +{ + MCS_ASSERT_PARAM(statusReg != NULL); + MCS_ASSERT_PARAM(stateMachine != NULL); + /* Get system status */ + if (SysIsError(statusReg)) { + *stateMachine = FSM_FAULT; + } + if (SysGetCmdStop(statusReg)) { + SysCmdStopClr(statusReg); + *stateMachine = FSM_STOP; + } +} + +/** + * @brief Check over current status. + * @param statusReg System status. + * @param stateMachine Motor Control Status. + * @retval None. + */ +static void CheckOverCurrentState(SysStatusReg *statusReg, FsmState *stateMachine) +{ + MCS_ASSERT_PARAM(statusReg != NULL); + MCS_ASSERT_PARAM(stateMachine != NULL); + /* check systerm error status */ + if (SysIsError(statusReg) == false) { + *stateMachine = FSM_IDLE; + } +} + +/** + * @brief Check bootstrap capacitor charge time. + * @param mtrCtrl The motor control handle. + * @param stateMachine Motor Control Status. + * @retval None. + */ +static void CheckBootstrpCapChargeTime(MTRCTRL_Handle *mtrCtrl, FsmState *stateMachine) +{ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + MCS_ASSERT_PARAM(stateMachine != NULL); + mtrCtrl->sysTickCnt++; + /* check bootstrap capacitor charge time */ + if (mtrCtrl->sysTickCnt == mtrCtrl->capChargeTickNum) { + *stateMachine = FSM_CLEAR; + } +} + +/** + * @brief Check systerm cmd start signal. + * @param mtrCtrl The motor control handle. + * @param aptAddr Three-phase APT address pointer. + * @param statusReg System status. + * @param stateMachine Motor Control Status. + * @retval None. + */ +static void CheckSysCmdStart(MTRCTRL_Handle *mtrCtrl, + APT_RegStruct **aptAddr, + SysStatusReg *statusReg, + FsmState *stateMachine) +{ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + MCS_ASSERT_PARAM(aptAddr != NULL); + MCS_ASSERT_PARAM(statusReg != NULL); + MCS_ASSERT_PARAM(stateMachine != NULL); + /* check start cmd */ + if (SysGetCmdStart(statusReg)) { + SysRunningSet(statusReg); + SysCmdStartClr(statusReg); + mtrCtrl->sysTickCnt = 0; + *stateMachine = FSM_CAP_CHARGE; + /* Preparation for charging the bootstrap capacitor. */ + AptTurnOnLowSidePwm(aptAddr, mtrCtrl->aptMaxcntCmp); + /* Out put pwm */ + MotorPwmOutputEnable(aptAddr); + } +} + +/** + * @brief System timer tick task. + * @param mtrCtrl The motor control handle. + * @param aptAddr Three-phase APT address pointer. + * @retval None. + */ +static void TSK_SystickIsr(MTRCTRL_Handle *mtrCtrl, APT_RegStruct **aptAddr) +{ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + MCS_ASSERT_PARAM(aptAddr != NULL); + SysStatusReg *statusReg = &mtrCtrl->statusReg; + FsmState *stateMachine = &mtrCtrl->stateMachine; + mtrCtrl->msTickCnt++; + /* Pre-processing of motor status. */ + MotorStatePerProc(statusReg, stateMachine); + /* statemachine */ + switch (*stateMachine) { + case FSM_IDLE: + /* Set smo estimate speed before motor start-up */ + g_mc.smo.spdEst = 0.0f; + CheckSysCmdStart(mtrCtrl, aptAddr, statusReg, stateMachine); + break; + case FSM_CAP_CHARGE: + /* Bootstrap Capacitor Charging Timing */ + CheckBootstrpCapChargeTime(mtrCtrl, stateMachine); + break; + /* Clear parameter before start */ + case FSM_CLEAR: + ClearBeforeStartup(mtrCtrl); + *stateMachine = FSM_STARTUP; + break; + case FSM_STARTUP: + MCS_StartupSwitch(mtrCtrl); + break; + case FSM_RUN: + /* Speed ramp control */ + mtrCtrl->spdRefHz = RMG_Exec(&mtrCtrl->spdRmg, mtrCtrl->spdCmdHz); + /* Speed loop control */ + mtrCtrl->idqRef.q = SPDCTRL_Exec(&mtrCtrl->spdCtrl, mtrCtrl->spdRefHz, mtrCtrl->smo.spdEst); + break; + case FSM_STOP: + mtrCtrl->spdRefHz = 0.0f; + MotorPwmOutputDisable(aptAddr); + SysRunningClr(statusReg); + *stateMachine = FSM_IDLE; + break; + case FSM_FAULT: /* Overcurrent state */ + CheckOverCurrentState(statusReg, stateMachine); + break; + default: + break; + } +} + +/** + * @brief Read the ADC initialize bias trim value. + * @param mtrCtrl The motor control handle. + * @retval None. + */ +static void TrimInitAdcShiftValue(MTRCTRL_Handle *mtrCtrl) +{ + float adc0SampleTemp = 0.0f; /* Current bias value for temp store */ + float adc1SampleTemp = 0.0f; + float adc0TempSum = 0.0f; + float adc1TempSum = 0.0f; /* Current bias sum value for 20 times */ + float adcSampleTimes = 0.0f; /* ADC sample times */ + for (int i = 0; i < ADC_READINIT_TIMES; i++) { + adc0SampleTemp = (float)HAL_ADC_GetConvResult(&ADCU_HANDLE, ADCUSOCNUM); + adc1SampleTemp = (float)HAL_ADC_GetConvResult(&ADCW_HANDLE, ADCWSOCNUM); + BASE_FUNC_DELAY_US(200); /* 200 is delay count, delay 200us triger adc sampling */ + if (adc0SampleTemp > 1000.0f && adc1SampleTemp > 1000.0f) { + adcSampleTimes++; + adc0TempSum += adc0SampleTemp; + adc1TempSum += adc1SampleTemp; + } + } + if (adcSampleTimes < 1.0f) { + adcSampleTimes = 1.0f; /* Prevent divide-by-zero errors */ + } + adc0SampleTemp = adc0TempSum / adcSampleTimes; + adc1SampleTemp = adc1TempSum / adcSampleTimes; + /* Force convert to float */ + mtrCtrl->adc0Compensate = (float) adc0SampleTemp; + mtrCtrl->adc1Compensate = (float) adc1SampleTemp; + /* The normal value scope: 1800 < adc0Compensate < 2200 */ + if(g_mc.adc0Compensate < ADC_TRIMVALUE_MIN || g_mc.adc0Compensate > ADC_TRIMVALUE_MAX \ + || g_mc.adc1Compensate < ADC_TRIMVALUE_MIN || g_mc.adc1Compensate > ADC_TRIMVALUE_MAX) { + DBG_PRINTF("ADC trim value error,please reset!"); + HAL_GPIO_SetValue(&LED2_HANDLE, LED2_PIN, GPIO_LOW_LEVEL); + } + adcSampleTimes = 0; + adc0TempSum = 0; + adc1TempSum = 0; +} + +/** + * @brief Read the ADC current sampling value. + * @param CurrUvw Three-phase current. + * @retval None. + */ +static void ReadCurrUvw(UvwAxis *CurrUvw) +{ + MCS_ASSERT_PARAM(CurrUvw != NULL); + float adc0 = (float)HAL_ADC_GetConvResult(&ADCU_HANDLE, ADCUSOCNUM); + float adc1 = (float)HAL_ADC_GetConvResult(&ADCW_HANDLE, ADCWSOCNUM); + /* Convert adc sample value to current value */ + CurrUvw->u = -(adc0 - g_mc.adc0Compensate) * g_mc.adcCurrCofe; + CurrUvw->w = -(adc1 - g_mc.adc1Compensate) * g_mc.adcCurrCofe; + CurrUvw->v = -CurrUvw->u - CurrUvw->w; +} + +/** + * @brief Setting the APT Output Duty Cycle. + * @param aptx APT register base address. + * @param leftDuty Left duty cycle. + * @param rightDuty Right duty cycle. + * @retval None. + */ +static void SetPwmDuty(APT_Handle *aptx, float leftDuty, float rightDuty) +{ + MCS_ASSERT_PARAM(aptx != NULL); + MCS_ASSERT_PARAM(leftDuty > 0); + MCS_ASSERT_PARAM(rightDuty > 0); + unsigned short maxPeriodCnt = aptx->waveform.timerPeriod; + unsigned short cntCmpLeftEdge = (unsigned short)(leftDuty * maxPeriodCnt); + unsigned short cntCmpRightEdge = (unsigned short)(rightDuty * maxPeriodCnt); + /* avoid overflowing */ + cntCmpLeftEdge = (cntCmpLeftEdge > maxPeriodCnt) ? maxPeriodCnt : cntCmpLeftEdge; + cntCmpRightEdge = (cntCmpRightEdge > maxPeriodCnt) ? maxPeriodCnt : cntCmpRightEdge; + HAL_APT_SetPWMDuty(aptx, cntCmpLeftEdge, cntCmpRightEdge); +} + +/** + * @brief Duty Cycle Setting. + * @param dutyUvwLeft Three-phase left duty cycle. + * @param dutyUvwRight Three-phase right duty cycle. + * @retval None. + */ +static void SetPwmDutyCp(UvwAxis *dutyUvwLeft, UvwAxis *dutyUvwRight) +{ + MCS_ASSERT_PARAM(dutyUvwLeft != NULL); + MCS_ASSERT_PARAM(dutyUvwRight != NULL); + /* Setting the Three-Phase Duty Cycle */ + SetPwmDuty(&g_apt0, dutyUvwLeft->u, dutyUvwRight->u); + SetPwmDuty(&g_apt1, dutyUvwLeft->v, dutyUvwRight->v); + SetPwmDuty(&g_apt2, dutyUvwLeft->w, dutyUvwRight->w); +} + +/** + * @brief To set the ADC sampling trigger comparison value. + * @param cntCmpSOCA Soca Compare Count Value. + * @param cntCmpSOCB Socb Compare Count Value. + * @retval None. + */ +static void SetADCTriggerTime(unsigned short cntCmpSOCA, unsigned short cntCmpSOCB) +{ + MCS_SetAdcCompareR1(g_apt[PHASE_U], cntCmpSOCA, cntCmpSOCB, g_mc.aptMaxcntCmp); +} + +/** + * @brief Temprature table, the temprature detect range is 15 ~ 60 degree. + * @param tempResisValue Temperature sensor resistance. + * @retval None. + */ +static float TempTable(float tempResisValue) +{ + float boardTemp = 0.0f; + /* Temperatures between 15 and 30. */ + if (tempResisValue > TEMP_RES_30 && tempResisValue <= TEMP_RES_15) { + boardTemp = TEMP_15 + (TEMP_30 - TEMP_15) * (TEMP_RES_15 - tempResisValue) / (TEMP_RES_15 - TEMP_RES_30); + } else if (tempResisValue > TEMP_RES_45 && tempResisValue <= TEMP_RES_30) { /* Temperature between 30 and 45. */ + boardTemp = TEMP_30 + (TEMP_45 - TEMP_30) * (TEMP_RES_30 - tempResisValue) / (TEMP_RES_30 - TEMP_RES_45); + } else if (tempResisValue > TEMP_RES_60 && tempResisValue <= TEMP_RES_45) { /* Temperature between 45 and 50. */ + boardTemp = TEMP_45 + (TEMP_60 - TEMP_45) * (TEMP_RES_45 - tempResisValue) / (TEMP_RES_45 - TEMP_RES_60); + } else if (tempResisValue <= TEMP_RES_60) { + boardTemp = TEMP_60; /* If temperature is over 60, set temperature as 60. */ + } else if (tempResisValue >= TEMP_RES_15) { + boardTemp = TEMP_15; /* If temperature is lower 15, set temperature as 15. */ + } + return boardTemp; +} + +/** + * @brief Read power board temperature and udc. + * @retval None. + */ +static void ReadBoardTempAndUdc(void) +{ + HAL_ADC_SoftTrigSample(&ADCRESIS_HANDLE, ADCRESISSOCNUM); + HAL_ADC_SoftTrigSample(&ADCUDC_HANDLE, ADCUDCSOCNUM); + BASE_FUNC_DELAY_US(CNT_10); /* Delay 10 us. */ + /* Force convert to float type. */ + float resisAdcValue = (float)HAL_ADC_GetConvResult(&ADCRESIS_HANDLE, ADCRESISSOCNUM); + /* 10 / (x + 10) * 3.3 = resisAdcValue / 4096 * 3.3, x is resisValue, 10kohm is resistor divider value. */ + float resisValue = (4096.0f * 10.0f - 10.0f * resisAdcValue) / resisAdcValue; + g_mc.powerBoardTemp = TempTable(resisValue); + g_mc.udc = ((float)HAL_ADC_GetConvResult(&ADCUDC_HANDLE, ADCUDCSOCNUM)) * ADC_UDC_COFFI; +} + +/** + * @brief Execut abnormal feedback speed protect motion. + * @retval None. + */ +static void SpdFbkErrorProt_Exec(void) +{ + if (g_mc.prot.motorErrStatus.Bit.motorStalling == 0 && + g_mc.prot.motorErrStatus.Bit.overVoltErr == 0 && + g_mc.prot.motorErrStatus.Bit.lowerVoltErr == 0 && + g_mc.prot.motorErrStatus.Bit.overIpmTempErr == 0 && + g_mc.prot.motorErrStatus.Bit.overCurrErr == 0) { + g_mc.prot.motorErrStatus.Bit.revRotErr = 1; + /* If revRotErr, execute protect motion. */ + ProtSpo_Exec(g_apt); + } +} + +/** + * @brief Execut nan data protect motion. + * @retval None. + */ +static void NanDataDetect(void) +{ + static short errorSpdStatus = 0; + /* Detect the nan observer speed or current value. */ + if (isnan(g_mc.smo.spdEst) || isnan(g_mc.idqRef.q)) { + errorSpdStatus++; + } else { + errorSpdStatus = 0; + } + /* If the data is nan & continuous counting value is over 500 times, execute protect motion. + the detect time is 500 * 500us = 250ms. */ + if (errorSpdStatus >= 500) { + errorSpdStatus = 0; + SpdFbkErrorProt_Exec(); + } +} + +/** + * @brief Check abnormal feedback speed. + * @retval None. + */ +static void CheckSpdFbkStatus(void) +{ + static short errorCurrStatus = 0; + static short errorDeltaSpdStatus = 0; + NanDataDetect(); + if (g_mc.stateMachine == FSM_RUN) { + /* Detect the abnormal idq feedback current. */ + if (Abs(g_mc.idqRef.q - g_mc.idqFbk.q) >= CTRL_IF_CURR_AMP_A) { + errorCurrStatus++; + } else { + errorCurrStatus = 0; + } + /* Detect the abnormal feedback speed, the normal speed is > 0, if smo.spdEst < -10 && + delta speed error > USER_MIN_SPD_HZ + 10.0f at FSM_RUN stage, set the motor motion as error */ + if (g_mc.smo.spdEst < -10.0f && (g_mc.spdRefHz - g_mc.smo.spdEst > USER_MIN_SPD_HZ + 10.0f)) { + errorDeltaSpdStatus++; + } + } + /* Execute protect motion if count over 500 times, this error status caused by abnormal speed + or cabnormal urrent feedback, the detect time is 500 * 500us = 250ms. */ + if (errorCurrStatus >= 500) { + errorCurrStatus = 0; + SpdFbkErrorProt_Exec(); + } + /* This error statu caused by motor stalling, the detect time is 2 * 500us = 1ms. */ + if (errorDeltaSpdStatus >= 2) { + errorDeltaSpdStatus = 0; + g_mc.prot.motorErrStatus.Bit.motorStalling = 1; + } +} + +/** + * @brief Check Potentiometer Value callback function. + * @param param The TIMER_Handle. + * @retval None. + */ +void CheckPotentiometerValueCallback(void *param) +{ + MCS_ASSERT_PARAM(param != NULL); + BASE_FUNC_UNUSED(param); + static float potentiomitorAdcValue = 0.0f; + static float spdCmdHz = 0; + static float spdCmdHzLast = USER_MIN_SPD_HZ; /* 35.0 is spdCmdHzLast init value */ + HAL_ADC_SoftTrigSample(&ADCPTT_HANDLE, ADCPTTSOCNUM); + BASE_FUNC_DELAY_US(10); /* Delay 10 us. */ + potentiomitorAdcValue = (float)HAL_ADC_GetConvResult(&ADCPTT_HANDLE, ADCPTTSOCNUM); + /* 4045.0 is adc sample max value of potentiomitor, convert max spd to 180.25Hz */ + spdCmdHz = potentiomitorAdcValue / 4045.0f * USER_MAX_SPD_HZ; + if (Abs(spdCmdHzLast - spdCmdHz) < 1.0f) { /* Ignore changes less than 1. */ + return; + } + spdCmdHzLast = spdCmdHz; + if (spdCmdHz < USER_MIN_SPD_HZ) { /* 35.0 is spdCmdHz lower limit */ + spdCmdHz = USER_MIN_SPD_HZ; /* 35.0 is spdCmdHz lower limit */ + } + if (spdCmdHz > g_mc.mtrParam.maxElecSpd) { /* spdCmdHz upper limit */ + spdCmdHz = g_mc.mtrParam.maxElecSpd; /* spdCmdHz upper limit */ + } + if (g_mc.spdAdjustMode == CUST_SPEED_ADJUST) { + g_mc.spdCmdHz = spdCmdHz; + } +} + +/** + * @brief System timer ISR for Motor Statemachine CallBack function. + * @param param The systick timer handle. + * @retval None. + */ +void MotorStatemachineCallBack(void *param) +{ + /* Verify Parameters */ + MCS_ASSERT_PARAM(param != NULL); + BASE_FUNC_UNUSED(param); + /* Read power board temprature and voltage. */ + ReadBoardTempAndUdc(); + /* Motor speed loop state machine. */ + TSK_SystickIsr(&g_mc, g_apt); + + /* Motor error speed feedback check. */ + CheckSpdFbkStatus(); + /* Motor stalling detect. */ + STP_Det_ByCurrSpd(&g_mc.prot.stall, &g_mc.prot.motorErrStatus, g_mc.smo.spdEst, g_mc.idqFbk); + STP_Exec(&g_mc.prot.motorErrStatus, g_apt); + + /* Motor over voltage detect. */ + OVP_Det(&g_mc.prot.ovp, &g_mc.prot.motorErrStatus, g_mc.udc); + OVP_Exec(&g_mc.prot.ovp, &g_mc.spdRefHz, g_apt); + OVP_Recy(&g_mc.prot.ovp, &g_mc.prot.motorErrStatus, g_mc.udc); + /* Motor lower voltage detect. */ + LVP_Det(&g_mc.prot.lvp, &g_mc.prot.motorErrStatus, g_mc.udc); + LVP_Exec(&g_mc.prot.lvp, &g_mc.spdRefHz, g_apt); + LVP_Recy(&g_mc.prot.lvp, &g_mc.prot.motorErrStatus, g_mc.udc); + /* Power board over temperature detect. */ + OTP_Det(&g_mc.prot.otp, &g_mc.prot.motorErrStatus, OTP_IPM_ERR_BIT, g_mc.powerBoardTemp); + OTP_Exec(&g_mc.prot.otp, &g_mc.spdRefHz, g_apt); + OTP_Recy(&g_mc.prot.otp, &g_mc.prot.motorErrStatus, OTP_IPM_ERR_BIT, g_mc.powerBoardTemp); + + /* If protect level == 4, set motor state as stop. */ + if (g_mc.prot.ovp.protLevel == LEVEL_4 || g_mc.prot.lvp.protLevel == LEVEL_4 \ + || g_mc.prot.otp.protLevel == LEVEL_4) { + SysCmdStopSet(&g_mc.statusReg); + } +} + +/** + * @brief The carrier ISR wrapper function. + * @param aptHandle The APT handle. + * @retval None. + */ +void MotorCarrierProcessCallback(void *aptHandle) +{ + MCS_ASSERT_PARAM(aptHandle != NULL); + BASE_FUNC_UNUSED(aptHandle); + /* the carrierprocess of motor */ + MCS_CarrierProcess(&g_mc); + /* Over current protect */ + if (g_mc.stateMachine == FSM_RUN || g_mc.stateMachine == FSM_STARTUP) { + OCP_Det(&g_mc.prot.ocp, &g_mc.prot.motorErrStatus, g_mc.idqFbk); + OCP_Exec(&g_mc.prot.ocp, &g_mc.idqFbk, g_apt); /* Execute over current protect motion */ + if (g_mc.prot.ocp.protLevel < LEVEL_4) { + OCP_Recy(&g_mc.prot.ocp, &g_mc.prot.motorErrStatus); + } + } +} + +/** + * @brief Event interrupt callback function of APT module. + * @param para APT module handle. + * @retval None. + */ +void MotorSysErrCallback(void *para) +{ + MCS_ASSERT_PARAM(para != NULL); + APT_Handle *handle = (APT_Handle *)para; + /* The IPM overcurrent triggers and disables the three-phase PWM output. */ + MotorPwmOutputDisable(g_apt); + DCL_APT_ClearOutCtrlEventFlag((APT_RegStruct *)g_apt[PHASE_U], APT_OC_COMBINE_EVENT_A1); + DCL_APT_ClearOutCtrlEventFlag((APT_RegStruct *)g_apt[PHASE_V], APT_OC_COMBINE_EVENT_A1); + DCL_APT_ClearOutCtrlEventFlag((APT_RegStruct *)g_apt[PHASE_W], APT_OC_COMBINE_EVENT_A1); + /* Status setting error */ + SysErrorSet(&g_mc.statusReg); + DBG_PRINTF("APT error! \r\n"); + HAL_GPIO_SetValue(&LED2_HANDLE, LED2_PIN, GPIO_LOW_LEVEL); + BASE_FUNC_UNUSED(handle); +} + +/** + * @brief Init motor controller's data structure. + * @retval None. + */ +static void InitSoftware(void) +{ + /* Initializing motor control param */ + TSK_Init(); + /* Read phase-uvw current */ + g_mc.readCurrUvwCb = ReadCurrUvw; + g_mc.setPwmDutyCb = SetPwmDutyCp; + g_mc.setADCTriggerTimeCb = SetADCTriggerTime; +} + +/** + * @brief Config the KEY func. + * @param handle The GPIO handle. + * @retval None. + */ +static KEY_State Key_StateRead(GPIO_Handle *handle) +{ + if (HAL_GPIO_GetPinValue(handle, handle->pins) == 0) { + BASE_FUNC_DELAY_MS(30); /* delay 30ms for deshake */ + if (HAL_GPIO_GetPinValue(handle, handle->pins) == 0) { + while (HAL_GPIO_GetPinValue(handle, handle->pins) == 0) { + } + return KEY_DOWN; + } + } + return KEY_UP; +} + +/** + * @brief Control motor start and stop state by key func. + * @param param The GPIO handle. + * @retval None. + */ +void MotorStartStopKeyCallback(void *param) +{ + GPIO_Handle *handle = (GPIO_Handle *)param; + if (Key_StateRead(handle) == KEY_DOWN) { + if (g_mc.motorStateFlag == 0) { /* start motor */ + g_mc.motorStateFlag = 1; + SysCmdStartSet(&g_mc.statusReg); + } else if (g_mc.motorStateFlag == 1) { /* stop motor */ + g_mc.motorStateFlag = 0; + SysCmdStopSet(&g_mc.statusReg); + } + } +} + +/** + * @brief User application main entry function. + * @retval BSP_OK. + */ +int MotorMainProcess(void) +{ + unsigned int tickNum1Ms = 2; /* 1ms tick */ + static unsigned int tickCnt1Ms = 0; + unsigned int tickNum500Ms = 1000; /* 500ms tick */ + static unsigned int tickCnt500Ms = 0; + SystemInit(); + HMI_Init(); /* Init uart interrupt */ + HAL_TIMER_Start(&g_timer0); + HAL_TIMER_Start(&g_timer1); + + /* Disable PWM output before startup. */ + MotorPwmOutputDisable(g_apt); + /* Software initialization. */ + InitSoftware(); + /* Start the PWM clock. */ + HAL_APT_StartModule(RUN_APT0 | RUN_APT1 | RUN_APT2); + /* System Timer clock. */ + BASE_FUNC_DELAY_MS(ADC_READINIT_DELAY); + TrimInitAdcShiftValue(&g_mc); + BASE_FUNC_DELAY_MS(MOTOR_START_DELAY); + while (1) { + /* Cycling send data to host */ + HMI_Process_Tx(&g_mc); + if (g_mc.msTickCnt - tickCnt1Ms >= tickNum1Ms) { + tickCnt1Ms = g_mc.msTickCnt; + /* User Code 1ms Event */ + HMI_Process_Rx(&g_mc); + /* User Code 1ms Event */ + } + + if (g_mc.msTickCnt - tickCnt500Ms >= tickNum500Ms) { + if (SysIsError(&g_mc.statusReg) != true) { + /* LED toggle in normal status. */ + HAL_GPIO_TogglePin(&LED1_HANDLE, LED1_PIN); + } + tickCnt500Ms = g_mc.msTickCnt; + } + } + return 0; +} diff --git a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/cust_process.c b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/cust_process.c new file mode 100644 index 00000000..7c4f7525 --- /dev/null +++ b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/cust_process.c @@ -0,0 +1,718 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file cust_process.c + * @author MCU Algorithm Team + * @brief This file provides functions declaration of cust process interface. + */ + +#include "cust_process.h" +#include "mcs_ctlmode_config.h" +#include "mcs_math_const.h" +#include "mcs_user_config.h" +#include "mcs_assert.h" +#include "main.h" +#include "uart_module.h" +#include "mcs_mtr_param.h" +/* Macro definitions --------------------------------------------------------------------------- */ +/* Constant value. */ +#define CONST_VALUE_60 60.0f /* Constant value 60. */ +#define CONST_VALUE_DIV_1000 0.001f /* Constant value 1/1000. */ + +/* Data array index. */ +#define DATA_SEGMENT_ONE 0 /* First element of the data segment */ +#define DATA_SEGMENT_TWO 1 /* Second element of the data segment */ +#define DATA_SEGMENT_THREE 2 /* Third element of the data segment */ +#define DATA_SEGMENT_FOUR 3 /* Fourth element of the data segment */ +#define CUSTACKCODELEN 10 /* Ack code length */ + +/* Command code. */ +#define SET_PID_KP 0x01 /* Set Pid Kp Command Params */ +#define SET_PID_KI 0x02 /* Set Pid Ki Command Params */ +#define SET_PID_LIMIT 0x03 /* Set Pid limit Command Params */ + +#define SET_SMO1TH_PLL_BDW 0x01 /* Set Smo1th Pll BandWidth Command Params */ +#define SET_SMO1TH_SPDFLITER_FC 0x02 /* Set Smo1th Fc Command Params */ +#define SET_SMO1TH_FILCOMPANGLE 0x03 /* Set Smo1th FillComp Command Params */ + +#define SET_SMO4TH_KD 0x01 /* Set Smo4th Kd Command Params */ +#define SET_SMO4TH_KP 0x02 /* Set Smo4th Kq Command Params */ + +#define SET_SPD_COMMAND_HZ 0x01 /* Set Target Speed Command Params */ +#define SET_SPD_RMG_SLOPE 0x02 /* Set Speed Slope Command Params */ + +#define SET_MAX_ELEC_SPD 0x01 /* Set Max Motor Speed Command Params */ +#define SET_MOTOR_NUM_OF_PAIRS 0x02 /* Set Motor Command Params */ + +#define SET_MOTOR_RES_OF_STATOR 0x01 /* Set Motor Res Command Params */ +#define SET_MOTOR_DAXIS_INDUCTANCE 0x02 /* Set Motor Daxis Inductance Command Params */ +#define SET_MOTOR_QAXIS_INDUCTANCE 0x03 /* Set Motor Qaxis Inductance Command Params */ + +#define SET_SVPWM_VOLTAGE_PER_UNIT 0x01 /* Set Svpwm Voltage Params */ +#define SET_ADC_CURRENT_SAMPLE_COFE 0x02 /* Set Adc coffe Params */ +#define SET_CURRENT_LOOP_CONTROL_PERIOD 0x03 /* Set Current loop period Command Params */ + +#define SET_IF_TARGET_CURRENT_VALUE 0x01 /* Set If Target Params */ +#define SET_INCREMENT_OF_IF_CURRENT 0x02 /* Set If Step Command Params */ +#define SET_SPEED_RING_BEGIN_SPEED 0x03 /* Set If to Smo Start Speed Command Params */ + +static unsigned char ackCode = 0; +static unsigned char g_uartTxBuf[CUSTACKCODELEN] = {0}; + +/** + * @brief Set observer type. + * @param mtrCtrl The motor control handle. + * @param rxData Receive buffer + */ +static void CMDCODE_SetObserverType(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) +{ + /* Get function code. */ + int funcCode = (int)(rxData->data[DATA_SEGMENT_ONE].typeF); + if (funcCode == FOC_OBSERVERTYPE_SMO1TH) { + ackCode = 0X01; + mtrCtrl->obserType = FOC_OBSERVERTYPE_SMO1TH; + CUST_AckCode(g_uartTxBuf, ackCode, mtrCtrl->obserType); + } else if (funcCode == FOC_OBSERVERTYPE_SMO4TH) { + ackCode = 0X02; + mtrCtrl->obserType = FOC_OBSERVERTYPE_SMO4TH; + CUST_AckCode(g_uartTxBuf, ackCode, mtrCtrl->obserType); + } +} + +/** + * @brief Set pid parameter ack code. + * @param funcCode Received data funccode. + */ +static unsigned char SetPidAckCode(int funcCode) +{ + switch (funcCode) { + /* Set current loop D-Axis PID parameter ack code. */ + case FOC_CURDAXISPID_PARAMS: + ackCode = 0xE0; + break; + /* Set current loop Q-Axis PID parameter ack code. */ + case FOC_CURQAXISPID_PARAMS: + ackCode = 0xE3; + break; + /* Set speed loop PID parameter ack code. */ + case FOC_SPDPID_PARAMS: + ackCode = 0xE6; + break; + default: + break; + } + return ackCode; +} + +/** + * @brief Set pid parameters. + * @param pidHandle The pid control handle. + * @param rxData Receive buffer + */ +static void SetPidParams(PID_Handle *pidHandle, CUSTDATATYPE_DEF *rxData) +{ + /* Get function code. */ + int funcCode = (int)(rxData->data[DATA_SEGMENT_ONE].typeF); + /* Get command code. */ + int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); + switch (cmdCode) { + case SET_PID_KP: /* Set the P parameter. */ + PID_SetKp(pidHandle, rxData->data[DATA_SEGMENT_THREE].typeF); + ackCode = SetPidAckCode(funcCode); + CUST_AckCode(g_uartTxBuf, (unsigned char)(ackCode + SET_PID_KP), rxData->data[DATA_SEGMENT_THREE].typeF); + break; + case SET_PID_KI: /* Set the I parameter. */ + PID_SetKi(pidHandle, rxData->data[DATA_SEGMENT_THREE].typeF); + ackCode = SetPidAckCode(funcCode); + CUST_AckCode(g_uartTxBuf, (unsigned char)(ackCode + SET_PID_KI), rxData->data[DATA_SEGMENT_THREE].typeF); + break; + case SET_PID_LIMIT: /* Set the pid limit. */ + PID_SetLimit(pidHandle, rxData->data[DATA_SEGMENT_THREE].typeF); + ackCode = SetPidAckCode(funcCode); + CUST_AckCode(g_uartTxBuf, (unsigned char)(ackCode + SET_PID_LIMIT), rxData->data[DATA_SEGMENT_THREE].typeF); + break; + default: + ackCode = 0X77; + CUST_AckCode(g_uartTxBuf, ackCode, 0); + break; + } +} + +/** + * @brief Set motor pid parameters. + * @param mtrCtrl The motor control handle. + * @param rxData Receive buffer + */ +static void CMDCODE_SetMotorPidParams(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) +{ + /* Get function code. */ + int funcCode = (int)(rxData->data[DATA_SEGMENT_ONE].typeF); + + if (funcCode == FOC_CURDAXISPID_PARAMS) { + SetPidParams(&mtrCtrl->currCtrl.dAxisPi, rxData); /* Set Curr loop Daxis pid params */ + } else if (funcCode == FOC_CURQAXISPID_PARAMS) { + SetPidParams(&mtrCtrl->currCtrl.qAxisPi, rxData); /* Set Curr loop Qaxis pid params */ + mtrCtrl->currCtrl.dAxisPi.upperLimit = mtrCtrl->currCtrl.qAxisPi.upperLimit; + mtrCtrl->currCtrl.dAxisPi.lowerLimit = mtrCtrl->currCtrl.qAxisPi.lowerLimit; + } else if (funcCode == FOC_SPDPID_PARAMS) { + SetPidParams(&mtrCtrl->spdCtrl.spdPi, rxData); /* Set Speed loop params */ + } +} + +/** + * @brief Set first order sliding mode observer parameters. + * @param smoHandle The observer control handle. + * @param rxData Receive buffer + */ +static void SetObserverSmo1thParams(FOSMO_Handle *smoHandle, CUSTDATATYPE_DEF *rxData) +{ + smoHandle->kSmo = rxData->data[DATA_SEGMENT_TWO].typeF; + ackCode = 0X09; + CUST_AckCode(g_uartTxBuf, ackCode, smoHandle->kSmo); +} + +/** + * @brief Set first order sliding mode observer's phase-locked loop parameters. + * @param smoHandle The observer control handle. + * @param rxData Receive buffer + */ +static void SetObserverSmo1thPLLParams(FOSMO_Handle *smoHandle, CUSTDATATYPE_DEF *rxData) +{ + /* Get command code. */ + int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); + float pllBdw = 0.0f; + switch (cmdCode) { + case SET_SMO1TH_PLL_BDW: /* Set the bandwidth. */ + pllBdw = rxData->data[DATA_SEGMENT_THREE].typeF; + smoHandle->pll.pi.kp = 2.0f * pllBdw; /* kp = 2.0f * pllBdw */ + smoHandle->pll.pi.ki = pllBdw * pllBdw; /* ki = pllBdw * pllBdw */ + ackCode = 0X0A; + CUST_AckCode(g_uartTxBuf, ackCode, pllBdw); + break; + case SET_SMO1TH_SPDFLITER_FC: /* Set the cutoff frequency. */ + smoHandle->spdFilter.fc = rxData->data[DATA_SEGMENT_THREE].typeF; + smoHandle->spdFilter.a1 = 1.0f / (1.0f + DOUBLE_PI * smoHandle->spdFilter.fc * CTRL_CURR_PERIOD); + smoHandle->spdFilter.b1 = 1.0f - smoHandle->spdFilter.a1; + ackCode = 0X0B; + CUST_AckCode(g_uartTxBuf, ackCode, smoHandle->spdFilter.fc); + break; + case SET_SMO1TH_FILCOMPANGLE: /* Set the compensation angle. */ + smoHandle->filCompAngle = rxData->data[DATA_SEGMENT_THREE].typeF; + ackCode = 0X0C; + CUST_AckCode(g_uartTxBuf, ackCode, smoHandle->filCompAngle); + break; + default: + ackCode = 0X77; + CUST_AckCode(g_uartTxBuf, ackCode, 0); + break; + } +} + +/** + * @brief Set fourth order sliding mode observer parameters. + * @param smo4thHandle The observer control handle. + * @param rxData Receive buffer + */ +static void SetObserverSmo4thParams(SMO4TH_Handle *smo4thHandle, CUSTDATATYPE_DEF *rxData) +{ + /* Get command code. */ + int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); + + switch (cmdCode) { + case SET_SMO4TH_KD: /* Set d axis gain. */ + smo4thHandle->kd = rxData->data[DATA_SEGMENT_THREE].typeF; + ackCode = 0X0D; + CUST_AckCode(g_uartTxBuf, ackCode, smo4thHandle->kd); + break; + case SET_SMO4TH_KP: /* Set q axis gain. */ + smo4thHandle->kq = rxData->data[DATA_SEGMENT_THREE].typeF; + ackCode = 0X0E; + CUST_AckCode(g_uartTxBuf, ackCode, smo4thHandle->kq); + break; + default: + break; + } +} + +/** + * @brief Set fourth order sliding mode observer's phase-locked loop parameters. + * @param smo4thHandle The observer control handle. + * @param rxData Receive buffer + */ +static void SetObserverSmo4thPLLParams(SMO4TH_Handle *smo4thHandle, CUSTDATATYPE_DEF *rxData) +{ + float pllBdw = rxData->data[DATA_SEGMENT_TWO].typeF; + smo4thHandle->pll.pi.kp = (2.0f) * pllBdw; /* kp = 2.0f * pllBdw */ + smo4thHandle->pll.pi.ki = pllBdw * pllBdw; + ackCode = 0X11; + CUST_AckCode(g_uartTxBuf, ackCode, pllBdw); +} + +/** + * @brief Set observer parameters. + * @param mtrCtrl The motor control handle. + * @param rxData Receive buffer + */ +static void CMDCODE_SetObserverParams(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) +{ + /* Get function code. */ + int funcCode = (int)(rxData->data[DATA_SEGMENT_ONE].typeF); + + if (funcCode == FOC_OBSERVERTYPE_SMO1TH) { + SetObserverSmo1thParams(&mtrCtrl->smo, rxData); + } else if (funcCode == FOC_OBSERVERTYPE_SMO1TH_PLL) { + SetObserverSmo1thPLLParams(&mtrCtrl->smo, rxData); + } else if (funcCode == FOC_OBSERVERTYPE_SMO4TH) { + SetObserverSmo4thParams(&mtrCtrl->smo4th, rxData); + } else if (funcCode == FOC_OBSERVERTYPE_SMO4TH_PLL) { + SetObserverSmo4thPLLParams(&mtrCtrl->smo4th, rxData); + } +} + +/** + * @brief Set motor speed and speed slope. + * @param mtrCtrl The motor control handle. + * @param rxData Receive buffer + */ +static void CMDCODE_SetMotorSpdAndSlope(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) +{ + /* Get command code. */ + int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); + + switch (cmdCode) { + case SET_SPD_COMMAND_HZ: /* Set target speed(hz). */ + mtrCtrl->spdCmdHz = rxData->data[DATA_SEGMENT_THREE].typeF * mtrCtrl->mtrParam.mtrNp / CONST_VALUE_60; + /* Judgement the value > 0.00001, make sure denominator != 0. */ + if (rxData->data[DATA_SEGMENT_FOUR].typeF > 0.00001f) { + mtrCtrl->spdRmg.delta = mtrCtrl->spdCmdHz / rxData->data[DATA_SEGMENT_FOUR].typeF * CTRL_SYSTICK_PERIOD; + } + ackCode = 0X16; + CUST_AckCode(g_uartTxBuf, ackCode, mtrCtrl->spdCmdHz); + break; + case SET_SPD_RMG_SLOPE: /* Set speed slope. */ + mtrCtrl->spdRmg.delta = mtrCtrl->spdCmdHz / rxData->data[DATA_SEGMENT_THREE].typeF * CTRL_SYSTICK_PERIOD; + ackCode = 0X17; + CUST_AckCode(g_uartTxBuf, ackCode, rxData->data[DATA_SEGMENT_THREE].typeF); + break; + default: + break; + } +} + +/** + * @brief Set motor base parameters. + * @param mtrParamHandle The motor parameters handle. + * @param rxData Receive buffer + */ +static void SetMotorBaseParams(MOTOR_Param *mtrParamHandle, CUSTDATATYPE_DEF *rxData) +{ + /* Get command code. */ + int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); + + switch (cmdCode) { + case SET_MAX_ELEC_SPD: /* Set max electric speed. */ + mtrParamHandle->maxElecSpd = rxData->data[DATA_SEGMENT_THREE].typeF / + CONST_VALUE_60 * mtrParamHandle->mtrNp; + ackCode = 0X18; + CUST_AckCode(g_uartTxBuf, ackCode, rxData->data[DATA_SEGMENT_THREE].typeF); + break; + case SET_MOTOR_NUM_OF_PAIRS: /* Set the number of motor pole pairs. */ + mtrParamHandle->mtrNp = (unsigned short)(rxData->data[DATA_SEGMENT_THREE].typeF); + ackCode = 0X19; + CUST_AckCode(g_uartTxBuf, ackCode, (float)mtrParamHandle->mtrNp); + break; + default: + break; + } +} + +/** + * @brief Set motor special parameters. + * @param mtrParamHandle The motor parameters handle. + * @param rxData Receive buffer + */ +static void SetMotorSpecialParams(MOTOR_Param *mtrParamHandle, CUSTDATATYPE_DEF *rxData) +{ + /* Get command code. */ + int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); + + switch (cmdCode) { + case SET_MOTOR_RES_OF_STATOR: /* Set the resistor of stator. */ + mtrParamHandle->mtrRs = rxData->data[DATA_SEGMENT_THREE].typeF; + ackCode = 0X1A; + CUST_AckCode(g_uartTxBuf, ackCode, mtrParamHandle->mtrRs); + break; + case SET_MOTOR_DAXIS_INDUCTANCE: /* Set the d axis inductance. */ + mtrParamHandle->mtrLd = rxData->data[DATA_SEGMENT_THREE].typeF; + ackCode = 0X1B; + CUST_AckCode(g_uartTxBuf, ackCode, mtrParamHandle->mtrLd); + break; + case SET_MOTOR_QAXIS_INDUCTANCE: /* Set the q axis inductance. */ + mtrParamHandle->mtrLq = rxData->data[DATA_SEGMENT_THREE].typeF; + ackCode = 0X1C; + CUST_AckCode(g_uartTxBuf, ackCode, mtrParamHandle->mtrLq); + break; + default: + ackCode = 0X77; + CUST_AckCode(g_uartTxBuf, ackCode, 0); + break; + } +} + +/** + * @brief Set motor board parameters. + * @param mtrCtrl The motor control handle. + * @param rxData Receive buffer + */ +static void SetMotorBoardParams(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) +{ + /* Get command code. */ + int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); + + switch (cmdCode) { + case SET_SVPWM_VOLTAGE_PER_UNIT: /* Set svpwm voltage per unit. */ + mtrCtrl->sv.voltPu = rxData->data[DATA_SEGMENT_THREE].typeF * ONE_DIV_SQRT3; + mtrCtrl->currCtrl.outLimit = mtrCtrl->sv.voltPu * ONE_DIV_SQRT3; + ackCode = 0X1D; + CUST_AckCode(g_uartTxBuf, ackCode, rxData->data[DATA_SEGMENT_THREE].typeF); + break; + case SET_ADC_CURRENT_SAMPLE_COFE: /* Set adc current sample cofeature. */ + mtrCtrl->adcCurrCofe = rxData->data[DATA_SEGMENT_THREE].typeF; + ackCode = 0X1E; + CUST_AckCode(g_uartTxBuf, ackCode, mtrCtrl->adcCurrCofe); + break; + case SET_CURRENT_LOOP_CONTROL_PERIOD: /* Set current loop control period. */ + mtrCtrl->currCtrlPeriod = 1 / rxData->data[DATA_SEGMENT_THREE].typeF * CONST_VALUE_DIV_1000; + ackCode = 0X1F; + CUST_AckCode(g_uartTxBuf, ackCode, rxData->data[DATA_SEGMENT_THREE].typeF); + break; + default: + break; + } +} + +/** + * @brief Set motor parameters. + * @param mtrCtrl The motor control handle. + * @param rxData Receive buffer + */ +static void CMDCODE_SetMotorParams(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) +{ + /* Get function code. */ + int funcCode = (int)(rxData->data[DATA_SEGMENT_ONE].typeF); + + if (funcCode == MOTOR_PARAMS_BASE) { + SetMotorBaseParams(&mtrCtrl->mtrParam, rxData); + } else if (funcCode == MOTOR_PARAMS_SPECIAL) { + SetMotorSpecialParams(&mtrCtrl->mtrParam, rxData); + } else if (funcCode == MOTOR_PARAMS_BOARD) { + SetMotorBoardParams(mtrCtrl, rxData); + } +} + +/** + * @brief Motor start. + * @param mtrCtrl The motor control handle. + */ +static void CMDCODE_MotorStart(MTRCTRL_Handle *mtrCtrl) +{ + if (mtrCtrl->stateMachine != FSM_RUN) { + SysCmdStartSet(&mtrCtrl->statusReg); /* start motor. */ + mtrCtrl->motorStateFlag = 1; + ackCode = 0X24; /* send ackcode to host. */ + CUST_AckCode(g_uartTxBuf, ackCode, 1); + } +} + +/** + * @brief Motor stop. + * @param mtrCtrl The motor control handle. + */ +static void CMDCODE_MotorStop(MTRCTRL_Handle *mtrCtrl) +{ + SysCmdStopSet(&mtrCtrl->statusReg); + mtrCtrl->motorStateFlag = 0; + ackCode = 0X25; + CUST_AckCode(g_uartTxBuf, ackCode, 0); +} + +/** + * @brief Motor state reset. + * @param mtrCtrl The motor control handle. + */ +static void CMDCODE_MotorReset(MTRCTRL_Handle *mtrCtrl) +{ + BASE_FUNC_UNUSED(mtrCtrl); + BASE_FUNC_SoftReset(); +} + +/** + * @brief Set IF-Startup parameters. + * @param mtrCtrl The motor control handle. + * @param rxData Receive buffer + */ +static void SetStartupIFParams(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) +{ + /* Get command code. */ + int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); + + switch (cmdCode) { + case SET_IF_TARGET_CURRENT_VALUE: /* Set I/F start up target current value. */ + mtrCtrl->ifCtrl.targetAmp = rxData->data[DATA_SEGMENT_THREE].typeF; + ackCode = 0X26; + CUST_AckCode(g_uartTxBuf, ackCode, mtrCtrl->ifCtrl.targetAmp); + break; + case SET_INCREMENT_OF_IF_CURRENT: /* Set increment of I/F start up current. */ + mtrCtrl->ifCtrl.stepAmp = mtrCtrl->ifCtrl.targetAmp / rxData->data[DATA_SEGMENT_THREE].typeF * + CTRL_SYSTICK_PERIOD; + ackCode = 0X27; + CUST_AckCode(g_uartTxBuf, ackCode, mtrCtrl->ifCtrl.stepAmp); + break; + case SET_SPEED_RING_BEGIN_SPEED: /* Set speed ring begin speed. */ + mtrCtrl->startup.spdBegin = rxData->data[DATA_SEGMENT_THREE].typeF / + CONST_VALUE_60 * mtrCtrl->mtrParam.mtrNp; + ackCode = 0X28; + CUST_AckCode(g_uartTxBuf, ackCode, rxData->data[DATA_SEGMENT_THREE].typeF); + break; + default: + break; + } +} + +/** + * @brief Set start up parameters. + * @param mtrCtrl The motor control handle. + * @param rxData Receive buffer + */ +static void CMDCODE_SetStartupParams(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) +{ + /* Get function code. */ + int funcCode = (int)(rxData->data[DATA_SEGMENT_ONE].typeF); + + if (funcCode == FOC_STARTUP_IF) { + SetStartupIFParams(mtrCtrl, rxData); + } +} + +/** + * @brief Set adjust speed mode. + * @param mtrCtrl The motor control handle. + * @param rxData Receive buffer + */ +static void CMDCODE_SetAdjustSpdMode(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) +{ + /* Get function code. */ + int funcCode = (int)(rxData->data[DATA_SEGMENT_ONE].typeF); + /* Get commond code. */ + int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); + if (funcCode == HOST_SPEED_ADJUST) { + mtrCtrl->spdAdjustMode = HOST_SPEED_ADJUST; + /* Uart connect success. */ + mtrCtrl->uartConnectFlag = CONNECTING; + ackCode = 0X2A; + CUST_AckCode(g_uartTxBuf, ackCode, (float)mtrCtrl->spdAdjustMode); + } else if (funcCode == CUST_SPEED_ADJUST) { + if (cmdCode == 1) { /* If uart connection disconnected & stop motor commond. */ + SysCmdStopSet(&mtrCtrl->statusReg); + mtrCtrl->motorStateFlag = 0; + } + mtrCtrl->spdAdjustMode = CUST_SPEED_ADJUST; + ackCode = 0X2B; + CUST_AckCode(g_uartTxBuf, ackCode, (float)mtrCtrl->spdAdjustMode); + /* Uart disconnect. */ + mtrCtrl->uartConnectFlag = DISCONNECT; + } +} + +/** + * @brief Check uart connect. + * @param mtrCtrl The motor control handle. + * @param rxData Receive buffer + */ +static void CMDCODE_UartHandShake(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) +{ + CMDCODE_SetAdjustSpdMode(mtrCtrl, rxData); +} + +/** + * @brief Set Motor Initial Status Parameters. + * @param mtrCtrl The motor control handle. + * @param rxData Receive buffer + * @param code Instruction code. + */ +static void CMDCODE_EXE_SetMotorInitParams(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData, unsigned char code) +{ + switch (code) { + case CMDCODE_SET_OBSERVER_TYPE: { /* Set observer type. */ + CMDCODE_SetObserverType(mtrCtrl, rxData); + } + break; + case CMDCODE_SET_PID_PARAMS: { /* Set motor pid parameters. */ + CMDCODE_SetMotorPidParams(mtrCtrl, rxData); + } + break; + case CMDCODE_SET_OBSERVER_PARAMS: { /* Set observer parameters. */ + CMDCODE_SetObserverParams(mtrCtrl, rxData); + } + break; + case CMDCODE_SET_MOTOR_TARGETSPD: { /* Set motor speed and speed slope. */ + CMDCODE_SetMotorSpdAndSlope(mtrCtrl, rxData); + } + break; + case CMDCODE_SET_MOTOR_PARAMS: { /* Set motor parameters. */ + CMDCODE_SetMotorParams(mtrCtrl, rxData); + } + break; + case CMDCODE_SET_ADJUSTSPD_MODE: /* Set adjust speed mode. */ + CMDCODE_SetAdjustSpdMode(mtrCtrl, rxData); + break; + default: + break; + } +} + +/** + * @brief Set Motor Control System Status. + * @param mtrCtrl The motor control handle. + * @param rxData Receive buffer + * @param code Instruction code. + */ +static void CMDCODE_EXE_SetMotorState(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData, unsigned char code) +{ + BASE_FUNC_UNUSED(rxData); + switch (code) { + case CMDCODE_MOTOR_START: { /* Motor start command */ + CMDCODE_MotorStart(mtrCtrl); + } + break; + case CMDCODE_MOTOR_STOP: { /* Motor stop command */ + CMDCODE_MotorStop(mtrCtrl); + } + break; + case CMDCODE_MOTORSTATE_RESET: { /* Motor reset command */ + CMDCODE_MotorReset(mtrCtrl); + } + break; + default: + break; + } +} + +/** + * @brief Set Startup and Uart Link Handshake Flag Parameters. + * @param mtrCtrl The motor control handle. + * @param rxData Receive buffer + * @param code Instruction code. + */ +static void CMDCODE_EXE_SetOtherParams(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData, unsigned char code) +{ + switch (code) { + case CMDCODE_SET_STARTUP_PARAMS: /* Set start up parameters. */ + CMDCODE_SetStartupParams(mtrCtrl, rxData); + break; + case CMDCODE_UART_HANDSHAKE: { /* Check uart hand shake. */ + CMDCODE_UartHandShake(mtrCtrl, rxData); + } + break; + case CMDCODE_UART_HEARTDETECT: { /* Check uart hand shake. */ + mtrCtrl->uartHeartDetCnt++; + } + break; + default: + break; + } +} +/** + * @brief Instruction code executor. + * @param mtrCtrl The motor control handle. + * @param rxData Receive buffer + * @param code Instruction code. + */ +static void CMDCODE_EXE_Process(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData, unsigned char code) +{ + CMDCODE_EXE_SetMotorInitParams(mtrCtrl, rxData, code); + CMDCODE_EXE_SetMotorState(mtrCtrl, rxData, code); + CMDCODE_EXE_SetOtherParams(mtrCtrl, rxData, code); +} + +/** + * @brief Host data download callback and data parsing. + * @param mtrCtrl The motor control handle. + * @param rxBuf Receive buffer + */ +void CUST_UartDataProcess(MTRCTRL_Handle *mtrCtrl, unsigned char *rxBuf) +{ + /* Verify Parameters */ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + MCS_ASSERT_PARAM(rxBuf != NULL); + + /* Uart data storage struct */ + CUSTDATATYPE_DEF data; + volatile unsigned char *ptrCnt = &rxBuf[FRAME_CHECK_BEGIN + 1]; + volatile unsigned char *strCnt = &data.data[0].typeCh[0]; + for (unsigned int j = 0; j < FRAME_RECV_DATA_LENTH * FRAME_ONE_DATA_LENTH; j++) { + *strCnt++ = *ptrCnt++; + } + /* Message function code. */ + data.code = rxBuf[FRAME_CHECK_BEGIN]; + CMDCODE_EXE_Process(mtrCtrl, &data, data.code); +} + +/** + * @brief The host computer displays data transmission. + * @param mtrCtrl The motor control handle. + * @param txData Message content. + * @param stage Message status function code. + */ +void CUST_SetTxMsg(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *txData) +{ + /* Verify Parameters */ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + MCS_ASSERT_PARAM(txData != NULL); + if (mtrCtrl->stateMachine == FSM_IDLE) { + mtrCtrl->smo.spdEst = 0.0f; + } + /* Data send to host. */ + txData->data[CURRDQ_Q].typeF = mtrCtrl->idqFbk.q; + txData->data[CURRDQ_D].typeF = mtrCtrl->idqFbk.d; + txData->data[CURRREFDQ_Q].typeF = mtrCtrl->idqRef.q; + txData->data[CURRREFDQ_D].typeF = mtrCtrl->idqRef.d; + /* Motor current speed. */ + txData->data[CURRSPD].typeF = mtrCtrl->smo.spdEst * CONST_VALUE_60 / mtrCtrl->mtrParam.mtrNp; + /* Motor commond speed. */ + txData->data[SPDCMDHZ].typeF = mtrCtrl->spdCmdHz * CONST_VALUE_60 / mtrCtrl->mtrParam.mtrNp; + /* Bus voltage. */ + txData->data[UDC].typeF = mtrCtrl->udc; + /* Power board temprature. */ + txData->data[POWERBOARDTEMP].typeF = mtrCtrl->powerBoardTemp; + /* Motor protection status flag. */ + txData->data[CUST_ERR_CODE].typeI = mtrCtrl->prot.motorErrStatus.all; + /* Three phase current. */ + txData->data[CURRUVW_U].typeF = mtrCtrl->currUvw.u; + txData->data[CURRUVW_V].typeF = mtrCtrl->currUvw.v; + txData->data[CURRUVW_W].typeF = mtrCtrl->currUvw.w; + /* Three phase pwm duty. */ + txData->data[PWMDUTYUVW_U].typeF = mtrCtrl->dutyUvw.u; + txData->data[PWMDUTYUVW_V].typeF = mtrCtrl->dutyUvw.v; + txData->data[PWMDUTYUVW_W].typeF = mtrCtrl->dutyUvw.w; + /* Motor electric angle. */ + txData->data[AXISANGLE].typeF = mtrCtrl->axisAngle; + txData->data[VDQ_Q].typeF = mtrCtrl->vdqRef.q; + txData->data[VDQ_D].typeF = mtrCtrl->vdqRef.d; + txData->data[SPDREFHZ].typeF = mtrCtrl->spdRefHz * CONST_VALUE_60 / mtrCtrl->mtrParam.mtrNp; + txData->data[SENDTIMESTAMP].typeF = mtrCtrl->uartTimeStamp; +} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/cust_process.h b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/cust_process.h new file mode 100644 index 00000000..60fe69bb --- /dev/null +++ b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/cust_process.h @@ -0,0 +1,36 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file cust_process.h + * @author MCU Algorithm Team + * @brief This file provides functions declaration of system status. + */ + +/* Define to prevent recursive inclusion ------------------------------------------------------- */ +#ifndef McsMagicTag_HMI_MOUDLE_H +#define McsMagicTag_HMI_MOUDLE_H + +/* Includes ------------------------------------------------------------------------------------ */ +#include "protocol.h" +/* Typedef definitions ------------------------------------------------------------------------- */ + + +void CUST_UartDataProcess(MTRCTRL_Handle *mtrCtrl, unsigned char *rxBuf); + +void CUST_SetTxMsg(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *txData); + +#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/hmi_module.c b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/hmi_module.c new file mode 100644 index 00000000..58bf15a7 --- /dev/null +++ b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/hmi_module.c @@ -0,0 +1,57 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file hmi_module.c + * @author MCU Algorithm Team + * @brief This file provides functions declaration of human-machine interface. + */ + +#include "hmi_module.h" +#include "mcs_assert.h" + +/** + * @brief HMI Initializatio. + * @retval None. + */ +void HMI_Init(void) +{ + UartRecvInit(); +} + +/** + * @brief HMI processing. + * @param mtrCtrl The motor control handle.. + * @retval None. + */ +void HMI_Process_Rx(MTRCTRL_Handle *mtrCtrl) +{ + /* Verify Parameters */ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + UartModuleProcess_Rx(mtrCtrl); +} + +/** + * @brief HMI processing. + * @param mtrCtrl The motor control handle.. + * @retval None. + */ +void HMI_Process_Tx(MTRCTRL_Handle *mtrCtrl) +{ + /* Verify Parameters */ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + UartModuleProcess_Tx(mtrCtrl); +} diff --git a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/hmi_module.h b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/hmi_module.h new file mode 100644 index 00000000..056f8d73 --- /dev/null +++ b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/hmi_module.h @@ -0,0 +1,37 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file hmi_module.h + * @author MCU Algorithm Team + * @brief This file provides functions declaration of system status. + */ + +/* Define to prevent recursive inclusion ------------------------------------------------------- */ +#ifndef McsMagicTag_HMI_MOUDLE_H +#define McsMagicTag_HMI_MOUDLE_H + +/* Includes ------------------------------------------------------------------------------------ */ +#include "uart_module.h" +/* Typedef definitions ------------------------------------------------------------------------- */ + + +void HMI_Init(void); + +void HMI_Process_Rx(MTRCTRL_Handle *mtrCtrl); +void HMI_Process_Tx(MTRCTRL_Handle *mtrCtrl); + +#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/protocol.c b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/protocol.c new file mode 100644 index 00000000..56f0c0bb --- /dev/null +++ b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/protocol.c @@ -0,0 +1,174 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file protocol.c + * @author MCU Algorithm Team + * @brief This file provides functions declaration of port communication. + */ + +#include "protocol.h" +#include "apt.h" +#include "typedefs.h" +#include "main.h" +#include "mcs_assert.h" +#include "cust_process.h" + +/** + * @brief Callback function for receiving data analysis and processing. + * @param rxBuf Receive buffer. + */ +__weak void CUST_UartDataProcess(MTRCTRL_Handle *mtrCtrl, unsigned char *rxBuf) +{ + BASE_FUNC_UNUSED(mtrCtrl); + BASE_FUNC_UNUSED(rxBuf); +} +/** + * @brief User-defined protocol message sending function (weak function). + * @param rxData Sending Messages.. + */ +__weak void CUST_SetTxMsg(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *txData) +{ + BASE_FUNC_UNUSED(mtrCtrl); + BASE_FUNC_UNUSED(*txData); +} + +static void (*g_ptrDataProcess)(MTRCTRL_Handle *mtrCtrl, unsigned char *rxBuf) = CUST_UartDataProcess; + +/** + * @brief Frame checksum. + * @param ptr Pointer to the data to be checked + * @param num Number of bytes + * @retval unsigned char Checksum + */ +static unsigned char CheckSum(unsigned char *ptr, unsigned char num) +{ + unsigned char sum = 0; + unsigned char *p = ptr; + /* Calculate the sum of received data. */ + for (unsigned char i = 0; i < num; i++) { + sum += (unsigned char)*p; + p++; + } + return sum; +} + +/** + * @brief Transmitting Data Frames. + * @param mtrCtrl The motor control handle. + * @param txBuf Sending Messages. + */ +unsigned int CUST_TransmitData(MTRCTRL_Handle *mtrCtrl, unsigned char *txBuf) +{ + /* Verify Parameters */ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + MCS_ASSERT_PARAM(txBuf != NULL); + unsigned int dataLen = FRAME_ONE_DATA_LENTH * SEND_FRAME_DATA_NUM; + unsigned char i = 0; + CUSTDATATYPE_DEF txData = {0}; + CUST_SetTxMsg(mtrCtrl, &txData); + txBuf[i++] = FRAME_START; + /* Message function code */ + txBuf[i++] = FRAME_SENT; + /* Message data */ + for (unsigned char x = 0; x < SEND_FRAME_DATA_NUM; x++) { + unsigned char floatIndex = 0; + unsigned char byteOffset = i; + txBuf[x * FRAME_ONE_DATA_LENTH + byteOffset++] = txData.data[x].typeCh[floatIndex++]; + txBuf[x * FRAME_ONE_DATA_LENTH + byteOffset++] = txData.data[x].typeCh[floatIndex++]; + txBuf[x * FRAME_ONE_DATA_LENTH + byteOffset++] = txData.data[x].typeCh[floatIndex++]; + txBuf[x * FRAME_ONE_DATA_LENTH + byteOffset++] = txData.data[x].typeCh[floatIndex++]; + } + /* Message verification domain */ + txBuf[dataLen + i++] = CheckSum((unsigned char*)&txBuf[FRAME_CHECK_BEGIN], dataLen + 1); + /* End of Message */ + txBuf[dataLen + i++] = FRAME_END; + return dataLen + i; +} + +/** + * @brief Transmitting Data Frames. + * @param txBuf Sending Cust Ack Code. + * @param ackCode Ack Code. + * @param varParams Host set parameter. + */ +void CUST_AckCode(unsigned char *txBuf, unsigned char ackCode, float varParams) +{ + /* Verify Parameters */ + MCS_ASSERT_PARAM(txBuf != NULL); + CUSTDATATYPE_DEF txData = {0}; + int dataIndex = 0; + unsigned int i = 0; + unsigned int txLen = 0; + unsigned char dataLen = FRAME_ONE_CHAR_LENTH + FRAME_ONE_DATA_LENTH; + + txData.data[0].typeF = varParams; + txBuf[i++] = FRAME_START; + /* Message function code */ + txBuf[i++] = FRAME_CUSTACK; + /* Message ack code */ + txBuf[i++] = ackCode; + /* Message data */ + txBuf[i++] = txData.data[0].typeCh[dataIndex++]; + txBuf[i++] = txData.data[0].typeCh[dataIndex++]; + txBuf[i++] = txData.data[0].typeCh[dataIndex++]; + txBuf[i++] = txData.data[0].typeCh[dataIndex++]; + + /* Message verification domain */ + txBuf[FRAME_ONE_CHAR_LENTH + i++] = CheckSum((unsigned char*)&txBuf[FRAME_CHECK_BEGIN], dataLen + 1); + /* End of Message */ + txBuf[FRAME_ONE_CHAR_LENTH + i++] = FRAME_END; + txLen = FRAME_ONE_CHAR_LENTH + i++; + HAL_UART_WriteDMA(&g_uart0, txBuf, txLen); +} + +/** + * @brief Cust receive data process. + * @param mtrCtrl The motor control handle. + * @param rxBuf Receive buffer + */ +void CUST_DataReceProcss(MTRCTRL_Handle *mtrCtrl, unsigned char *rxBuf) +{ + /* Verify Parameters */ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + MCS_ASSERT_PARAM(rxBuf != NULL); + unsigned char g_uartTxBuf[10] = {0}; + unsigned char ackCode = 0; + /* Frame header check */ + if (rxBuf[0] != FRAME_START) { + ackCode = 0X78; + CUST_AckCode(g_uartTxBuf, ackCode, 0); + return; + } + /* Frame trailer check */ + if (rxBuf[FRAME_LENTH - 1] != FRAME_END) { + ackCode = 0X79; + CUST_AckCode(g_uartTxBuf, ackCode, 0); + return; + } + /* Checksum */ + if (CheckSum((unsigned char*)&rxBuf[FRAME_CHECK_BEGIN], FRAME_CHECK_NUM) != rxBuf[FRAME_CHECKSUM]) { + ackCode = 0X7A; + CUST_AckCode(g_uartTxBuf, ackCode, 0); + return; + } else { + if (g_ptrDataProcess == NULL) { + return; + } else { + g_ptrDataProcess(mtrCtrl, rxBuf); + } + } +} diff --git a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/protocol.h b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/protocol.h new file mode 100644 index 00000000..900a00d8 --- /dev/null +++ b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/protocol.h @@ -0,0 +1,121 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file protocol.h + * @author MCU Algorithm Team + * @brief This file provides functions declaration of port communication. + */ +#ifndef McsMagicTag_PORTOCOL_H +#define McsMagicTag_PORTOCOL_H + +#include "uart.h" +#include "mcs_carrier.h" + +#define RS485_SEND_ENABLE GPIO6->GPIO_DATA[GPIO_PIN_7].reg = GPIO_PIN_7 +#define RS485_SEND_DISABLE GPIO6->GPIO_DATA[GPIO_PIN_7].reg = BASE_CFG_UNSET + +#define RX_BUF_LEN (16) +#define SEND_FRAME_DATA_NUM (CUSTDATA_MAX) +/* Service Uart0 Communication Deal */ +#define FRAME_ONE_DATA_LENTH 4 +#define FRAME_ONE_CHAR_LENTH 1 +#define FRAME_RECV_DATA_LENTH 4 +#define FRAME_LENTH 20 /* Data length */ +#define FRAME_SENT 0X8F +#define FRAME_CUSTACK 0X8A +#define FRAME_START 0x0F /* Start frame */ +#define FRAME_END '/' /* StOP frame */ +#define FRAME_CHECK_BEGIN 1 /* Check frame */ +#define FRAME_CHECKSUM 18 /* Check sum */ +#define FRAME_CHECK_NUM 17 +#define CMDCODE_IDLE_FRAME 0x55 /* Fill frame */ +#define CMDCODE_GET_MOTOR_PARAMS 0x01 +#define CMDCODE_SEND_MOTOR_PARAMS 0x02 +#define CMDCODE_SET_MOTOR_CTLMODE 0x03 +#define CMDCODE_SET_OBSERVER_TYPE 0x04 +#define CMDCODE_SET_STARTUP_MODE 0x05 +#define CMDCODE_SET_PID_PARAMS 0x06 +#define CMDCODE_SET_STARTUP_PARAMS 0x07 +#define CMDCODE_SET_OBSERVER_PARAMS 0x08 +#define CMDCODE_SET_MOTOR_TARGETSPD 0x09 +#define CMDCODE_SET_MOTOR_PARAMS 0x0A +#define CMDCODE_MOTOR_START 0x0B +#define CMDCODE_MOTOR_STOP 0x0C +#define CMDCODE_MOTORSTATE_RESET 0x0D +#define CMDCODE_SEND_FIRMVERSION 0x0E +#define CMDCODE_SET_ADJUSTSPD_MODE 0x11 +#define CMDCODE_UART_HANDSHAKE 0x12 +#define CMDCODE_UART_HEARTDETECT 0x13 + +typedef union { + unsigned char typeCh[4]; + float typeF; + int typeI; +} UNIONDATATYPE_DEF; + +typedef enum { + OFFLINE_RES = 0, + OFFLINE_LD, + OFFLINE_LQ, + OFFLINE_PSIF, + OFFLINE_JS, + OFFLINE_NP, + OFFLINE_B, + OFFLINE_KPD, + OFFLINE_KID, + OFFLINE_KPQ, + OFFLINE_KIQ, + OFFLINE_KPS, + OFFLINE_KIS, + OFFLINE_SPEED, + OFLINE_MAX +} OFFLINE_IDEN_TYPE; + +typedef enum { + CURRDQ_Q = 0, + CURRDQ_D, + CURRREFDQ_Q, + CURRREFDQ_D, + CURRSPD, + SPDCMDHZ, + UDC, + POWERBOARDTEMP, + CUST_ERR_CODE, + CURRUVW_U, + CURRUVW_V, + CURRUVW_W, + PWMDUTYUVW_U, + PWMDUTYUVW_V, + PWMDUTYUVW_W, + AXISANGLE, + VDQ_Q, + VDQ_D, + SPDREFHZ, + SENDTIMESTAMP, + CUSTDATA_MAX +} SENDTOHOSTPARAMS; + +typedef struct { + volatile unsigned char code; + volatile UNIONDATATYPE_DEF data[SEND_FRAME_DATA_NUM]; +} CUSTDATATYPE_DEF; + + +void CUST_DataReceProcss(MTRCTRL_Handle *mtrCtrl, unsigned char *rxBuf); +unsigned int CUST_TransmitData(MTRCTRL_Handle *mtrCtrl, unsigned char *txBuf); +void CUST_AckCode(unsigned char *txBuf, unsigned char ackCode, float varParams); +#endif /* McsMagicTag_PORTOCOL_H */ diff --git a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/uart_module.c b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/uart_module.c new file mode 100644 index 00000000..df5e9186 --- /dev/null +++ b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/uart_module.c @@ -0,0 +1,184 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the + * following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file uart_module.c + * @author MCU Algorithm Team + * @brief This file provides functions declaration of Serial port communication. + */ +#include "uart_module.h" +#include "debug.h" +#include "main.h" +#include "baseinc.h" + +/* Buffer size */ +#define UI_TX_BUF_LEN (96) +#define UI_RX_BUF_LEN (96) + +/* Receiving Timeout Interval */ +#define UART_TIME_OUT_MS (100) + +/* Start sending data to host delay after uart connect success */ +#define UART_UPDATA_DELAY_TIME_MS (50) + +/* Data buffer */ +unsigned char g_uartRxBuf[UI_RX_BUF_LEN] = {0}; +unsigned char g_uartTxBuf[UI_TX_BUF_LEN] = {0}; +static unsigned int getdeltaSystickCnt = 0; +static FRAME_Handle g_uartFrame; +/** + * @brief Receive Data Clear. + * @param uartFrame Receice Data. + * @retval None. + */ +static void FrameRecvClear(FRAME_Handle *uartFrame) +{ + /* Clear buffer lenth. */ + uartFrame->buffLen = 0; + uartFrame->timeOutCnt = 0; + uartFrame->frameFlag = 0; + /* Clear received data lenth. */ + uartFrame->rxLen = 0; + /* Clear received flag. */ + uartFrame->rxFlag = 0; + uartFrame->upDataCnt = 0; + uartFrame->rxAckFlag = 0; +} + +/** + * @brief Set Dma status. + * @param mtrCtrl The motor control handle. + * @retval None. + */ +static void SetUartDmaStatus(MTRCTRL_Handle *mtrCtrl) +{ + /* Delay 50ms start uart Tx DMA . */ + if (mtrCtrl->uartConnectFlag == CONNECTING && g_uartFrame.upDataDelayCnt++ > UART_UPDATA_DELAY_TIME_MS) { + g_uartFrame.txFlag = 1; /* Start send data flag. */ + mtrCtrl->uartConnectFlag = CONNECTED; + g_uartFrame.upDataDelayCnt = 0; + } + if (mtrCtrl->uartConnectFlag == DISCONNECT) { + g_uartFrame.txFlag = 0; /* Stop send data flag. */ + mtrCtrl->uartTimeStamp = 0; + } +} + +/** + * @brief Uart Dma interupt callback func. + * @param handle Uart handle. + * @retval None. + */ +void UART0_TXDMACallback(void *handle) +{ + BASE_FUNC_UNUSED(handle); + unsigned int getCurSystickCnt = 0; + static unsigned int getlastSystickCnt = 0; + /* USER CODE BEGIN UART0_WRITE_DMA_FINISH */ + g_uartFrame.txFlag = 1; + /* Received information answered, information update reserved. */ + if (g_uartFrame.rxAckFlag == 1) { + g_uartFrame.uartItTxFlag = 1; + g_uartFrame.rxAckFlag = 0; + } + getCurSystickCnt = DCL_SYSTICK_GetTick(); + if (getlastSystickCnt != 0) { + /* Calculate unit frame data send time */ + getdeltaSystickCnt = getCurSystickCnt - getlastSystickCnt; + } + getlastSystickCnt = getCurSystickCnt; + /* USER CODE END UART0_WRITE_DMA_FINISH */ +} + +/** + * @brief Uart0 Interruput Read CallBack Function. + * @param handle Uart handle. + * @retval None. + */ +void UART0ReadInterruptCallback(void *handle) +{ + BASE_FUNC_UNUSED(handle); + /* USER CODE BEGIN UART0_READ_IT_FINISH */ + if (g_uartFrame.rxLen >= UI_RX_BUF_LEN - 1) { + g_uartFrame.rxLen = 0; + } + HAL_UART_ReadIT(handle, &g_uartFrame.rxData, 1); + g_uartRxBuf[g_uartFrame.rxLen] = g_uartFrame.rxData; + g_uartFrame.rxLen++; + g_uartFrame.rxFlag = 1; + g_uartFrame.uartItTxFlag = 0; + return; + /* USER CODE END UART0_READ_IT_FINISH */ +} + +/** + * @brief Uart Read Data Init Function. + * @param void. + * @retval None. + */ +void UartRecvInit(void) +{ + /* Uart reception initialization */ + FrameRecvClear(&g_uartFrame); + HAL_UART_ReadIT(&g_uart0, &g_uartFrame.rxData, 1); +} + +/** + * @brief Uart Read Data Process Function. + * @param mtrCtrl The motor control handle. + * @retval None. + */ +void UartModuleProcess_Rx(MTRCTRL_Handle *mtrCtrl) +{ + /* Verify Parameters */ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + SetUartDmaStatus(mtrCtrl); + if (g_uartFrame.rxFlag == 1) { /* Receive data flag. */ + if (g_uartFrame.timeOutCnt++ > UART_TIME_OUT_MS) { + /* Received data from the host. */ + g_uartFrame.frameFlag = 1; + g_uartFrame.rxFlag = 0; + g_uartFrame.timeOutCnt = 0; + /* Execute data process. */ + CUST_DataReceProcss(mtrCtrl, g_uartRxBuf); + g_uartFrame.rxAckFlag = 1; + g_uartFrame.rxLen = 0; + } + } + g_uartFrame.frameFlag = 0; +} + +/** + * @brief Uart Write Data Process Function. + * @param mtrCtrl The motor control handle. + * @retval None. + */ +void UartModuleProcess_Tx(MTRCTRL_Handle *mtrCtrl) +{ + /* Verify Parameters */ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + if (g_uartFrame.txFlag == 1) { /* Send data flag. */ + mtrCtrl->uartTimeStamp = (float)getdeltaSystickCnt; /* Unit data time stamp */ + g_uartFrame.upDataCnt = 0; + g_uartFrame.txFlag = 0; + /* Send data to host. */ + unsigned int txLen = CUST_TransmitData(mtrCtrl, g_uartTxBuf); + /* If txIT mode send data finish, convert to DMA mode */ + if (g_uartFrame.uartItTxFlag == 1) { + HAL_UART_WriteDMA(&g_uart0, g_uartTxBuf, txLen); + } + } +} diff --git a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/uart_module.h b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/uart_module.h new file mode 100644 index 00000000..f1f8f899 --- /dev/null +++ b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/uart_module.h @@ -0,0 +1,47 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file uart_module.h + * @author MCU Algorithm Team + * @brief This file provides functions declaration of Serial port communication. + */ +#ifndef McsMagicTag_UART_MODULE_H +#define McsMagicTag_UART_MODULE_H + +#include "protocol.h" +#include "mcs_ctlmode_config.h" + +typedef struct { + unsigned int buffLen; + unsigned int timeOutCnt; + unsigned char frameFlag; + unsigned int rxLen; + unsigned char rxFlag; + unsigned char txFlag; + unsigned char rxData; + unsigned int upDataCnt; + unsigned int upDataDelayCnt; + unsigned char uartItTxFlag; + unsigned char rxAckFlag; +} FRAME_Handle; + + +void UartRecvInit(void); +void UartModuleProcess_Rx(MTRCTRL_Handle *mtrCtrl); +void UartModuleProcess_Tx(MTRCTRL_Handle *mtrCtrl); + +#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/readme.md b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/readme.md new file mode 100644 index 00000000..d0e25b7d --- /dev/null +++ b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/readme.md @@ -0,0 +1,20 @@ +# 10025_Six_Step_Wave + +**【功能描述】** + +- 基于ECMCU105H/ECBMCU201MPC单板的反电动势采样的无感电机六步方波调速系统应用 + +**【环境要求】** + +- 所有单板电源改制为演示用的24V低压,电机选用云台PM120(10025) +- 电机(黄绿蓝)相线分别对应功率板上U/V/W通道接口 + +**【配置方法】** + +- 将user文件夹替换原工程user文件夹即可 + +【操作说明】 + +- 点击开发板按键启动电机 + +- 开发板蓝色电位器,可以调节电机转动的快慢。(15-78HZ) \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/generatecode/feature.h b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/generatecode/feature.h new file mode 100644 index 00000000..715371be --- /dev/null +++ b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/generatecode/feature.h @@ -0,0 +1,94 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file feature.h + * @author MCU Driver Team + * @brief This file contains macro configurations related to the project. This file is generated by the IDE tool. + * @date 2025-04-14 14:05:10 + */ + +#ifndef McuMagicTag_FEATURE_H +#define McuMagicTag_FEATURE_H + +/* Macro definitions --------------------------------------------------------- */ +#define CHIP_3061MNPICA MACRO_ENABLE + +#define MACRO_ENABLE 1 +#define MACRO_DISABLE 0 + +/* Macro switch */ +#define BASE_DEFINE_USE_ASSERT MACRO_ENABLE +#ifndef FLASH_CRC_CONFIG +#define FLASH_CRC_CONFIG +#endif /* #ifndef FLASH_CRC_CONFIG */ +#define BASE_MATH_SINCOS_MIDDLE_TABLE MACRO_ENABLE /**< This macro is used to control the table type when the + BASE_MATH_GetSinCos() queries the table. When the value of + this macro is MACRO_ENABLE, the error value obtained by the + BASE_MATH_GetSinCos() is relatively small, and the return + value of the function may be greater than or less than the + actual value. When the value of this macro is MACRO_DISABLE, + the error value obtained by the BASE_MATH_GetSinCos() is + relatively large. However, in the range [0°, 180°) and + [180°, 360°), the return value of the function is either + greater than or less than the actual value. */ + +/* Peripheral module macro switch--------------------------------------------- */ +#define BOARD_DIM_NUM 1 /**< Number of dimming handle arrays. */ + +#define BOARD_KEY_NUM 10 /**< Number of key handle arrays. */ +#define BOARD_KEY_PRESS_ON GPIO_HIGH_LEVEL /**< GPIO status corresponding to long press valid. */ +#define BOARD_KEY_PRESS_OFF GPIO_LOW_LEVEL /**< GPIO status corresponding to short press valid. */ + +#define BOARD_LED_SEG_NUM 4 /**< Number of segments. */ +#define BOARD_LED_SEGMENT_ON GPIO_HIGH_LEVEL /**< GPIO level status corresponding to valid segments. */ +#define BOARD_LED_SEGMENT_OFF GPIO_LOW_LEVEL /**< GPIO level status corresponding to invalid segments. */ + +#define BOARD_MKEY_SCHEME_NUMBER BOARD_MKEY_SCHEME_NUMBER_ONE /**< Define the scheme to be adopted. */ +#define BOARD_MKEY_OUT_NUM 4 /**< Number of GPIO pins used as output during scanning. */ +#define BOARD_MKEY_IN_NUM 4 /**< Number of GPIO pins used as input during scanning. */ +#define BOARD_MKEY_OUT_PIN_VALID GPIO_LOW_LEVEL /**< GPIO level status corresponding to the valid \ + status of the output GPIO in the key matrix. */ +#define BOARD_MKEY_OUT_PIN_INVALID GPIO_HIGH_LEVEL /**< GPIO level status corresponding to the \ + invalid status of the output GPIO in the key matrix. */ +#define BOARD_MKEY_IN_PIN_VALID GPIO_LOW_LEVEL /**< Indicates the GPIO level corresponding to the \ + valid status of the input GPIO in the key matrix. */ +#define BOARD_MKEY_IN_PIN_INVALID GPIO_HIGH_LEVEL /**< Indicates the GPIO level corresponding to the \ + invalid status of the input GPIO in the key matrix. */ + +#define BOARD_PULSES_NUM 2 /**< Number of pulse handles. */ + +#define BASE_DEFINE_SLIPAVERAGE_NUM 2 /**< Sliding average array length. */ + +#define LISTNODE_MAX 20 + +#define BASE_DEFINE_DMA_QUICKSTART + +#define XTRAIL_FREQ 30000000U + +#define DBG_USE_NO_PRINTF 0U +#define DBG_USE_UART_PRINTF 1U + +#define DBG_PRINTF_USE DBG_USE_UART_PRINTF +#if (DBG_PRINTF_USE == DBG_USE_UART_PRINTF) +#define DBG_PRINTF_UART_PORT UART0 +#endif + +/* USER CODE BEGIN 0 */ +/* USER CODE 区域内代码不会被覆盖,区域外会被生成的默认代码覆盖(其余USER CODE 区域同理) */ +/* USER CODE END 0 */ + +#endif /* McuMagicTag_FEATURE_H */ \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/generatecode/main.h b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/generatecode/main.h new file mode 100644 index 00000000..40d65b1d --- /dev/null +++ b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/generatecode/main.h @@ -0,0 +1,98 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file main.h + * @author MCU Driver Team + * @brief This file contains driver init functions. + * @date 2025-04-14 14:05:10 + */ + +/* Define to prevent recursive inclusion ------------------------------------- */ +#ifndef McuMagicTag_SYSTEM_INIT_H +#define McuMagicTag_SYSTEM_INIT_H + +#include "adc.h" +#include "adc_ex.h" +#include "acmp.h" +#include "acmp_ex.h" +#include "apt.h" +#include "uart.h" +#include "uart_ex.h" +#include "gpio.h" +#include "timer.h" +#include "timer_ex.h" +#include "pga.h" +#include "crg.h" +#include "dma.h" +#include "dma_ex.h" +#include "iocmg.h" + +#define IO_SPEED_FAST 0x00U +#define IO_SPEED_SLOW 0x01U + +#define IO_DRV_LEVEL4 0x00U +#define IO_DRV_LEVEL3 0x01U +#define IO_DRV_LEVEL2 0x02U +#define IO_DRV_LEVEL1 0x03U + +#define XTAL_DRV_LEVEL4 0x03U +#define XTAL_DRV_LEVEL3 0x02U +#define XTAL_DRV_LEVEL2 0x01U +#define XTAL_DRV_LEVEL1 0x00U + +#define GpioStartStop_PIN GPIO_PIN_4 +#define GpioStartStop_HANDLE g_gpio2 + +extern ACMP_Handle g_acmp0; +extern PGA_Handle g_pga0; +extern PGA_Handle g_pga1; +extern TIMER_Handle g_timer0; +extern TIMER_Handle g_timer1; +extern UART_Handle g_uart0; +extern APT_Handle g_apt0; +extern APT_Handle g_apt1; +extern APT_Handle g_apt2; +extern ADC_Handle g_adc0; + +extern DMA_Handle g_dmac; + +extern GPIO_Handle g_gpio2; +extern GPIO_Handle g_gpio1; + +BASE_StatusType CRG_Config(CRG_CoreClkSelect *coreClkSelect); +void SystemInit(void); + +void UART0WriteInterruptCallback(void *handle); +void UART0ReadInterruptCallback(void *handle); + +void UART0InterruptErrorCallback(void *handle); +void MotorStatemachineCallBack(void *handle); +void TIMER1_DMAOverFlow_InterruptProcess(void *handle); +void CheckPotentiometerValueCallback(void *handle); +void TIMER0_DMAOverFlow_InterruptProcess(void *handle); +void MotorCarrierProcessCallback(void *aptHandle); +void MotorSysErrCallback(void *aptHandle); + +void UART0_TXDMACallback(void *handle); + +void MotorStartStopKeyCallback(void *param); + +/* USER CODE BEGIN 0 */ +/* USER CODE 区域内代码不会被覆盖,区域外会被生成的默认代码覆盖(其余USER CODE 区域同理) */ +/* USER CODE END 0 */ + +#endif /* McuMagicTag_SYSTEM_INIT_H */ \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/generatecode/system_init.c b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/generatecode/system_init.c new file mode 100644 index 00000000..d49cd860 --- /dev/null +++ b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/generatecode/system_init.c @@ -0,0 +1,677 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file system_init.c + * @author MCU Driver Team + * @brief This file contains driver init functions. + * @date 2025-04-14 14:05:10 + */ + +#include "main.h" +#include "ioconfig.h" +#include "iocmg_ip.h" + +#define UART0_BAUD_RATE 1843200 + +BASE_StatusType CRG_Config(CRG_CoreClkSelect *coreClkSelect) +{ + CRG_Handle crg; + crg.baseAddress = CRG; + crg.pllRefClkSelect = CRG_PLL_REF_CLK_SELECT_HOSC; + crg.pllPreDiv = CRG_PLL_PREDIV_4; + crg.pllFbDiv = 48; /* PLL Multiplier 48 */ + crg.pllPostDiv = CRG_PLL_POSTDIV_2; + crg.coreClkSelect = CRG_CORE_CLK_SELECT_PLL; + crg.handleEx.pllPostDiv2 = CRG_PLL_POSTDIV2_3; + crg.handleEx.clk1MSelect = CRG_1M_CLK_SELECT_HOSC; + crg.handleEx.clk1MDiv = (25 - 1); /* The 1 MHz freq is equal to the input clock frequency / (clk_1m_div + 1). 25 is the div of the clk_1m in CLOCK. */ + + if (HAL_CRG_Init(&crg) != BASE_STATUS_OK) { + return BASE_STATUS_ERROR; + } + *coreClkSelect = crg.coreClkSelect; + return BASE_STATUS_OK; +} + +static void DMA_Channel0Init(void *handle) +{ + DMA_ChannelParam dma_param; + dma_param.direction = DMA_MEMORY_TO_PERIPH_BY_DMAC; + dma_param.srcAddrInc = DMA_ADDR_INCREASE; + dma_param.destAddrInc = DMA_ADDR_UNALTERED; + dma_param.srcPeriph = DMA_REQUEST_MEM; + dma_param.destPeriph = DMA_REQUEST_UART0_TX; + dma_param.srcWidth = DMA_TRANSWIDTH_BYTE; + dma_param.destWidth = DMA_TRANSWIDTH_BYTE; + dma_param.srcBurst = DMA_BURST_LENGTH_1; + dma_param.destBurst = DMA_BURST_LENGTH_1; + dma_param.pHandle = handle; + HAL_DMA_InitChannel(&g_dmac, &dma_param, DMA_CHANNEL_ZERO); +} + +static void DMA_Init(void) +{ + HAL_CRG_IpEnableSet(DMA_BASE, IP_CLK_ENABLE); + g_dmac.baseAddress = DMA; + IRQ_Register(IRQ_DMA_TC, HAL_DMA_IrqHandlerTc, &g_dmac); + IRQ_Register(IRQ_DMA_ERR, HAL_DMA_IrqHandlerError, &g_dmac); + IRQ_EnableN(IRQ_DMA_TC); + IRQ_EnableN(IRQ_DMA_ERR); + HAL_DMA_Init(&g_dmac); + DMA_Channel0Init((void *)(&g_uart0)); + HAL_DMA_SetChannelPriorityEx(&g_dmac, DMA_CHANNEL_ZERO, DMA_PRIORITY_HIGHEST); +} + +static void ACMP0_Init(void) +{ + HAL_CRG_IpEnableSet(ACMP0_BASE, IP_CLK_ENABLE); /* ACMP clock bit reset. */ + g_acmp0.baseAddress = ACMP0_BASE; + g_acmp0.inOutConfig.inputNNum = ACMP_INPUT_N_SELECT2; + g_acmp0.inOutConfig.inputPNum = ACMP_INPUT_P_SELECT2; + g_acmp0.inOutConfig.polarity = ACMP_OUT_NOT_INVERT; + g_acmp0.filterCtrl.filterMode = ACMP_FILTER_NONE; + g_acmp0.hysteresisVol = ACMP_HYS_VOL_30MV; + g_acmp0.interruptEn = BASE_CFG_UNSET; + HAL_ACMP_Init(&g_acmp0); +} + +static void ADC0_Init(void) +{ + HAL_CRG_IpEnableSet(ADC0_BASE, IP_CLK_ENABLE); + HAL_CRG_IpClkSelectSet(ADC0_BASE, CRG_ADC_CLK_ASYN_PLL_DIV); + HAL_CRG_IpClkDivSet(ADC0_BASE, CRG_ADC_DIV_1); + + g_adc0.baseAddress = ADC0; + g_adc0.socPriority = ADC_PRIMODE_ALL_ROUND; + + HAL_ADC_Init(&g_adc0); + + SOC_Param socParam = {0}; + socParam.adcInput = ADC_CH_ADCINA0; /* PGA0_OUT(ADC AIN0) */ + socParam.sampleTotalTime = ADC_SOCSAMPLE_22CLK; /* adc sample total time 22 adc_clk */ + socParam.trigSource = ADC_TRIGSOC_APT0_SOCA; + socParam.continueMode = BASE_CFG_DISABLE; + socParam.finishMode = ADC_SOCFINISH_NONE; + HAL_ADC_ConfigureSoc(&g_adc0, ADC_SOC_NUM1, &socParam); + + socParam.adcInput = ADC_CH_ADCINA1; /* PGA1_OUT(ADC AIN1) */ + socParam.sampleTotalTime = ADC_SOCSAMPLE_22CLK; /* adc sample total time 22 adc_clk */ + socParam.trigSource = ADC_TRIGSOC_APT0_SOCA; + socParam.continueMode = BASE_CFG_DISABLE; + socParam.finishMode = ADC_SOCFINISH_NONE; + HAL_ADC_ConfigureSoc(&g_adc0, ADC_SOC_NUM6, &socParam); + + socParam.adcInput = ADC_CH_ADCINA9; /* PIN7(ADC AIN9) */ + socParam.sampleTotalTime = ADC_SOCSAMPLE_22CLK; /* adc sample total time 22 adc_clk */ + socParam.trigSource = ADC_TRIGSOC_SOFT; + socParam.continueMode = BASE_CFG_DISABLE; + socParam.finishMode = ADC_SOCFINISH_NONE; + HAL_ADC_ConfigureSoc(&g_adc0, ADC_SOC_NUM0, &socParam); + + socParam.adcInput = ADC_CH_ADCINA5; /* PIN2(ADC AIN5) */ + socParam.sampleTotalTime = ADC_SOCSAMPLE_22CLK; /* adc sample total time 22 adc_clk */ + socParam.trigSource = ADC_TRIGSOC_SOFT; + socParam.continueMode = BASE_CFG_DISABLE; + socParam.finishMode = ADC_SOCFINISH_NONE; + HAL_ADC_ConfigureSoc(&g_adc0, ADC_SOC_NUM3, &socParam); + + socParam.adcInput = ADC_CH_ADCINA15; /* PIN14(ADC AIN15) */ + socParam.sampleTotalTime = ADC_SOCSAMPLE_22CLK; /* adc sample total time 22 adc_clk */ + socParam.trigSource = ADC_TRIGSOC_SOFT; + socParam.continueMode = BASE_CFG_DISABLE; + socParam.finishMode = ADC_SOCFINISH_NONE; + HAL_ADC_ConfigureSoc(&g_adc0, ADC_SOC_NUM4, &socParam); +} + +__weak void MotorSysErrCallback(void *aptHandle) +{ + BASE_FUNC_UNUSED(aptHandle); + /* USER CODE BEGIN APT0_EVENT_INTERRUPT */ + /* USER CODE END APT0_EVENT_INTERRUPT */ +} + +__weak void MotorCarrierProcessCallback(void *aptHandle) +{ + BASE_FUNC_UNUSED(aptHandle); + /* USER CODE BEGIN APT0_TIMER_INTERRUPT */ + /* USER CODE END APT0_TIMER_INTERRUPT */ +} + +static void APT0_ProtectInit(void) +{ + APT_OutCtrlProtectEx protectApt = {0}; + protectApt.ocEventEnEx = BASE_CFG_ENABLE; + protectApt.ocEventModeEx = APT_OUT_CTRL_ONE_SHOT; + protectApt.ocActionEx = APT_OUT_CTRL_ACTION_LOW; + protectApt.ocActionBEx = APT_OUT_CTRL_ACTION_LOW; + protectApt.ocEvtInterruptEnEx = BASE_CFG_ENABLE; + protectApt.ocSysEvent = APT_SYS_EVT_DEBUG | APT_SYS_EVT_CLK | APT_SYS_EVT_MEM; + protectApt.originalEvtEx = APT_EM_ORIGINAL_SRC_ACMP0; + protectApt.evtPolarityMaskEx = APT_EM_ACMP0_INVERT_BIT; + protectApt.filterCycleNumEx = 0; + HAL_APT_ProtectInitEx(&g_apt0, &protectApt); +} + +static void APT0_Init(void) +{ + HAL_CRG_IpEnableSet(APT0_BASE, IP_CLK_ENABLE); + + g_apt0.baseAddress = APT0; + + /* Clock Settings */ + g_apt0.waveform.dividerFactor = 1 - 1; + /* Timer Settings */ + g_apt0.waveform.timerPeriod = 7500; /* 7500 is count period of APT time-base timer */ + g_apt0.waveform.cntMode = APT_COUNT_MODE_UP_DOWN; + + /* Wave Form */ + g_apt0.waveform.basicType = APT_PWM_BASIC_A_HIGH_B_LOW; + g_apt0.waveform.chAOutType = APT_PWM_OUT_BASIC_TYPE; + g_apt0.waveform.chBOutType = APT_PWM_OUT_BASIC_TYPE; + g_apt0.waveform.divInitVal = 0; + g_apt0.waveform.cntInitVal = 0; + g_apt0.waveform.cntCmpLeftEdge = 500; /* 500 is count compare point of the left edge of PWM waveform */ + g_apt0.waveform.cntCmpRightEdge = 4000; /* 4000 is count compare point of the right edge of PWM waveform */ + g_apt0.waveform.cntCmpLoadMode = APT_BUFFER_INDEPENDENT_LOAD; + g_apt0.waveform.cntCmpLoadEvt = APT_COMPARE_LOAD_EVENT_ZERO; + g_apt0.waveform.deadBandCnt = 225; /* 225 is dead-band value */ + + /* ADC Trigger SOCA */ + g_apt0.adcTrg.trgEnSOCA = BASE_CFG_ENABLE; + g_apt0.adcTrg.cntCmpSOCA = 375; /* 375 is count compare point of ADC trigger source SOCA when using CMPA */ + g_apt0.adcTrg.trgSrcSOCA = APT_CS_SRC_CNTR_CMPA_DOWN; + g_apt0.adcTrg.trgScaleSOCA = 1; + + /* ADC Trigger SOCB */ + g_apt0.adcTrg.trgEnSOCB = BASE_CFG_ENABLE; + g_apt0.adcTrg.cntCmpSOCB = 1; + g_apt0.adcTrg.trgSrcSOCB = APT_CS_SRC_CNTR_CMPB_DOWN; + g_apt0.adcTrg.trgScaleSOCB = 1; + + g_apt0.adcTrg.cntCmpLoadMode = APT_BUFFER_INDEPENDENT_LOAD; + g_apt0.adcTrg.cntCmpLoadEvt = APT_COMPARE_LOAD_EVENT_ZERO; + + /* Timer Trigger */ + g_apt0.tmrInterrupt.tmrInterruptEn = BASE_CFG_ENABLE; + g_apt0.tmrInterrupt.tmrInterruptSrc = APT_INT_SRC_CNTR_ZERO; + g_apt0.tmrInterrupt.tmrInterruptScale = 1; + + APT0_ProtectInit(); + + HAL_APT_PWMInit(&g_apt0); + HAL_APT_RegisterCallBack(&g_apt0, APT_EVENT_INTERRUPT, MotorSysErrCallback); + IRQ_SetPriority(IRQ_APT0_EVT, 7); /* 7 is priority value */ + IRQ_Register(IRQ_APT0_EVT, HAL_APT_EventIrqHandler, &g_apt0); + IRQ_EnableN(IRQ_APT0_EVT); + HAL_APT_RegisterCallBack(&g_apt0, APT_TIMER_INTERRUPT, MotorCarrierProcessCallback); + IRQ_SetPriority(IRQ_APT0_TMR, 5); /* 5 is priority value */ + IRQ_Register(IRQ_APT0_TMR, HAL_APT_TimerIrqHandler, &g_apt0); + IRQ_EnableN(IRQ_APT0_TMR); +} + +static void APT1_ProtectInit(void) +{ + APT_OutCtrlProtectEx protectApt = {0}; + protectApt.ocEventEnEx = BASE_CFG_ENABLE; + protectApt.ocEventModeEx = APT_OUT_CTRL_ONE_SHOT; + protectApt.ocActionEx = APT_OUT_CTRL_ACTION_LOW; + protectApt.ocActionBEx = APT_OUT_CTRL_ACTION_LOW; + protectApt.ocEvtInterruptEnEx = BASE_CFG_DISABLE; + protectApt.ocSysEvent = APT_SYS_EVT_DEBUG | APT_SYS_EVT_CLK | APT_SYS_EVT_MEM; + protectApt.originalEvtEx = APT_EM_ORIGINAL_SRC_ACMP0; + protectApt.evtPolarityMaskEx = APT_EM_ACMP0_INVERT_BIT; + protectApt.filterCycleNumEx = 0; + HAL_APT_ProtectInitEx(&g_apt1, &protectApt); +} + +static void APT1_Init(void) +{ + HAL_CRG_IpEnableSet(APT1_BASE, IP_CLK_ENABLE); + + g_apt1.baseAddress = APT1; + + /* Clock Settings */ + g_apt1.waveform.dividerFactor = 1 - 1; + /* Timer Settings */ + g_apt1.waveform.timerPeriod = 7500; /* 7500 is count period of APT time-base timer */ + g_apt1.waveform.cntMode = APT_COUNT_MODE_UP_DOWN; + + /* Wave Form */ + g_apt1.waveform.basicType = APT_PWM_BASIC_A_HIGH_B_LOW; + g_apt1.waveform.chAOutType = APT_PWM_OUT_BASIC_TYPE; + g_apt1.waveform.chBOutType = APT_PWM_OUT_BASIC_TYPE; + g_apt1.waveform.divInitVal = 0; + g_apt1.waveform.cntInitVal = 0; + g_apt1.waveform.cntCmpLeftEdge = 500; /* 500 is count compare point of the left edge of PWM waveform */ + g_apt1.waveform.cntCmpRightEdge = 4000; /* 4000 is count compare point of the right edge of PWM waveform */ + g_apt1.waveform.cntCmpLoadMode = APT_BUFFER_INDEPENDENT_LOAD; + g_apt1.waveform.cntCmpLoadEvt = APT_COMPARE_LOAD_EVENT_ZERO; + g_apt1.waveform.deadBandCnt = 225; /* 225 is dead-band value */ + + APT1_ProtectInit(); + + HAL_APT_PWMInit(&g_apt1); +} + +static void APT2_ProtectInit(void) +{ + APT_OutCtrlProtectEx protectApt = {0}; + protectApt.ocEventEnEx = BASE_CFG_ENABLE; + protectApt.ocEventModeEx = APT_OUT_CTRL_ONE_SHOT; + protectApt.ocActionEx = APT_OUT_CTRL_ACTION_LOW; + protectApt.ocActionBEx = APT_OUT_CTRL_ACTION_LOW; + protectApt.ocEvtInterruptEnEx = BASE_CFG_DISABLE; + protectApt.ocSysEvent = APT_SYS_EVT_DEBUG | APT_SYS_EVT_CLK | APT_SYS_EVT_MEM; + protectApt.originalEvtEx = APT_EM_ORIGINAL_SRC_ACMP0; + protectApt.evtPolarityMaskEx = APT_EM_ACMP0_INVERT_BIT; + protectApt.filterCycleNumEx = 0; + HAL_APT_ProtectInitEx(&g_apt2, &protectApt); +} + +static void APT2_Init(void) +{ + HAL_CRG_IpEnableSet(APT2_BASE, IP_CLK_ENABLE); + + g_apt2.baseAddress = APT2; + + /* Clock Settings */ + g_apt2.waveform.dividerFactor = 1 - 1; + /* Timer Settings */ + g_apt2.waveform.timerPeriod = 7500; /* 7500 is count period of APT time-base timer */ + g_apt2.waveform.cntMode = APT_COUNT_MODE_UP_DOWN; + + /* Wave Form */ + g_apt2.waveform.basicType = APT_PWM_BASIC_A_HIGH_B_LOW; + g_apt2.waveform.chAOutType = APT_PWM_OUT_BASIC_TYPE; + g_apt2.waveform.chBOutType = APT_PWM_OUT_BASIC_TYPE; + g_apt2.waveform.divInitVal = 0; + g_apt2.waveform.cntInitVal = 0; + g_apt2.waveform.cntCmpLeftEdge = 500; /* 500 is count compare point of the left edge of PWM waveform */ + g_apt2.waveform.cntCmpRightEdge = 4000; /* 4000 is count compare point of the right edge of PWM waveform */ + g_apt2.waveform.cntCmpLoadMode = APT_BUFFER_INDEPENDENT_LOAD; + g_apt2.waveform.cntCmpLoadEvt = APT_COMPARE_LOAD_EVENT_ZERO; + g_apt2.waveform.deadBandCnt = 225; /* 225 is dead-band value */ + + APT2_ProtectInit(); + + HAL_APT_PWMInit(&g_apt2); +} + +__weak void MotorStartStopKeyCallback(void *param) +{ + GPIO_Handle *handle = (GPIO_Handle *)param; + BASE_FUNC_UNUSED(handle); +} + +static void GPIO_Init(void) +{ + HAL_CRG_IpEnableSet(GPIO2_BASE, IP_CLK_ENABLE); + g_gpio2.baseAddress = GPIO2; + + g_gpio2.pins = GPIO_PIN_3; + HAL_GPIO_Init(&g_gpio2); + HAL_GPIO_SetDirection(&g_gpio2, g_gpio2.pins, GPIO_OUTPUT_MODE); + HAL_GPIO_SetValue(&g_gpio2, g_gpio2.pins, GPIO_HIGH_LEVEL); + HAL_GPIO_SetIrqType(&g_gpio2, g_gpio2.pins, GPIO_INT_TYPE_NONE); + + g_gpio2.pins = GPIO_PIN_4; + HAL_GPIO_Init(&g_gpio2); + HAL_GPIO_SetDirection(&g_gpio2, g_gpio2.pins, GPIO_INPUT_MODE); + HAL_GPIO_SetValue(&g_gpio2, g_gpio2.pins, GPIO_HIGH_LEVEL); + HAL_GPIO_SetIrqType(&g_gpio2, g_gpio2.pins, GPIO_INT_TYPE_LOW_LEVEL); + + HAL_CRG_IpEnableSet(GPIO1_BASE, IP_CLK_ENABLE); + g_gpio1.baseAddress = GPIO1; + + g_gpio1.pins = GPIO_PIN_0; + HAL_GPIO_Init(&g_gpio1); + HAL_GPIO_SetDirection(&g_gpio1, g_gpio1.pins, GPIO_OUTPUT_MODE); + HAL_GPIO_SetValue(&g_gpio1, g_gpio1.pins, GPIO_HIGH_LEVEL); + HAL_GPIO_SetIrqType(&g_gpio1, g_gpio1.pins, GPIO_INT_TYPE_NONE); + + HAL_GPIO_RegisterCallBack(&g_gpio2, GPIO_PIN_4, MotorStartStopKeyCallback); + IRQ_Register(IRQ_GPIO2, HAL_GPIO_IrqHandler, &g_gpio2); + IRQ_SetPriority(IRQ_GPIO2, 1); /* set gpio1 interrupt priority to 1, 1~15. 1 is priority value */ + IRQ_EnableN(IRQ_GPIO2); /* gpio interrupt enable */ + + return; +} + +static void PGA0_Init(void) +{ + HAL_CRG_IpEnableSet(PGA0_BASE, IP_CLK_ENABLE); + + g_pga0.baseAddress = PGA0_BASE; + g_pga0.externalResistorMode = BASE_CFG_ENABLE; + g_pga0.handleEx.extCapCompensation = PGA_EXT_COMPENSATION_2X; + HAL_PGA_Init(&g_pga0); +} + +static void PGA1_Init(void) +{ + HAL_CRG_IpEnableSet(PGA1_BASE, IP_CLK_ENABLE); + + g_pga1.baseAddress = PGA1_BASE; + g_pga1.externalResistorMode = BASE_CFG_ENABLE; + g_pga1.handleEx.extCapCompensation = PGA_EXT_COMPENSATION_2X; + HAL_PGA_Init(&g_pga1); +} + +__weak void CheckPotentiometerValueCallback(void *handle) +{ + BASE_FUNC_UNUSED(handle); + /* USER CODE BEGIN CheckPotentiometerValueCallback */ + /* USER CODE END CheckPotentiometerValueCallback */ +} + +static void TIMER0_Init(void) +{ + HAL_CRG_IpEnableSet(TIMER0_BASE, IP_CLK_ENABLE); /* TIMER0 clock enable. */ + unsigned int load = ((float)HAL_CRG_GetIpFreq((void *)TIMER0) / (1u << (TIMERPRESCALER_NO_DIV * 4)) / 1000000u) * 1000000; + + g_timer0.baseAddress = TIMER0; + g_timer0.load = (load < 1) ? 0 : (load - 1); /* Set timer value immediately */ + g_timer0.bgLoad = (load < 1) ? 0 : (load - 1); /* Set timer value */ + g_timer0.mode = TIMER_MODE_RUN_PERIODIC; /* Run in period mode */ + g_timer0.prescaler = TIMERPRESCALER_NO_DIV; /* Don't frequency division */ + g_timer0.size = TIMER_SIZE_32BIT; /* 1 for 32bit, 0 for 16bit */ + g_timer0.interruptEn = BASE_CFG_ENABLE; + g_timer0.adcSocReqEnable = BASE_CFG_DISABLE; + g_timer0.dmaReqEnable = BASE_CFG_DISABLE; + HAL_TIMER_Init(&g_timer0); + IRQ_Register(IRQ_TIMER0, HAL_TIMER_IrqHandler, &g_timer0); + + HAL_TIMER_RegisterCallback(&g_timer0, TIMER_PERIOD_FIN, CheckPotentiometerValueCallback); + IRQ_SetPriority(IRQ_TIMER0, 1); /* 1 is priority value */ + IRQ_EnableN(IRQ_TIMER0); +} + +__weak void MotorStatemachineCallBack(void *handle) +{ + BASE_FUNC_UNUSED(handle); + /* USER CODE BEGIN MotorStatemachineCallBack */ + /* USER CODE END MotorStatemachineCallBack */ +} + +static void TIMER1_Init(void) +{ + HAL_CRG_IpEnableSet(TIMER1_BASE, IP_CLK_ENABLE); /* TIMER1 clock enable. */ + unsigned int load = ((float)HAL_CRG_GetIpFreq((void *)TIMER1) / (1u << (TIMERPRESCALER_NO_DIV * 4)) / 1000000u) * 500; + + g_timer1.baseAddress = TIMER1; + g_timer1.load = (load < 1) ? 0 : (load - 1); /* Set timer value immediately */ + g_timer1.bgLoad = (load < 1) ? 0 : (load - 1); /* Set timer value */ + g_timer1.mode = TIMER_MODE_RUN_PERIODIC; /* Run in period mode */ + g_timer1.prescaler = TIMERPRESCALER_NO_DIV; /* Don't frequency division */ + g_timer1.size = TIMER_SIZE_32BIT; /* 1 for 32bit, 0 for 16bit */ + g_timer1.interruptEn = BASE_CFG_ENABLE; + g_timer1.adcSocReqEnable = BASE_CFG_DISABLE; + g_timer1.dmaReqEnable = BASE_CFG_DISABLE; + HAL_TIMER_Init(&g_timer1); + IRQ_Register(IRQ_TIMER1, HAL_TIMER_IrqHandler, &g_timer1); + + HAL_TIMER_RegisterCallback(&g_timer1, TIMER_PERIOD_FIN, MotorStatemachineCallBack); + IRQ_SetPriority(IRQ_TIMER1, 1); /* 1 is priority value */ + IRQ_EnableN(IRQ_TIMER1); +} + +__weak void UART0InterruptErrorCallback(void *handle) +{ + BASE_FUNC_UNUSED(handle); + /* USER CODE BEGIN UART0_TRNS_IT_ERROR */ + /* USER CODE END UART0_TRNS_IT_ERROR */ +} + +__weak void UART0_TXDMACallback(void *handle) +{ + BASE_FUNC_UNUSED(handle); + /* USER CODE BEGIN UART0_WRITE_DMA_FINISH */ + /* USER CODE END UART0_WRITE_DMA_FINISH */ +} + +__weak void UART0WriteInterruptCallback(void *handle) +{ + BASE_FUNC_UNUSED(handle); + /* USER CODE BEGIN UART0_WRITE_IT_FINISH */ + /* USER CODE END UART0_WRITE_IT_FINISH */ +} + +__weak void UART0ReadInterruptCallback(void *handle) +{ + BASE_FUNC_UNUSED(handle); + /* USER CODE BEGIN UART0_READ_IT_FINISH */ + /* USER CODE END UART0_READ_IT_FINISH */ +} + +static void UART0_Init(void) +{ + HAL_CRG_IpEnableSet(UART0_BASE, IP_CLK_ENABLE); /* UART0 clock enable. */ + g_uart0.baseAddress = UART0; + + g_uart0.baudRate = UART0_BAUD_RATE; + g_uart0.dataLength = UART_DATALENGTH_8BIT; + g_uart0.stopBits = UART_STOPBITS_ONE; + g_uart0.parity = UART_PARITY_NONE; + g_uart0.txMode = UART_MODE_DMA; + g_uart0.rxMode = UART_MODE_INTERRUPT; + g_uart0.fifoMode = BASE_CFG_ENABLE; + g_uart0.fifoTxThr = UART_FIFODEPTH_SIZE8; + g_uart0.fifoRxThr = UART_FIFODEPTH_SIZE8; + g_uart0.hwFlowCtr = BASE_CFG_DISABLE; + g_uart0.handleEx.overSampleMultiple = UART_OVERSAMPLING_16X; + g_uart0.handleEx.msbFirst = BASE_CFG_DISABLE; + HAL_UART_Init(&g_uart0); + HAL_UART_RegisterCallBack(&g_uart0, UART_TRNS_IT_ERROR, (UART_CallbackType)UART0InterruptErrorCallback); + HAL_UART_RegisterCallBack(&g_uart0, UART_READ_IT_FINISH, (UART_CallbackType)UART0ReadInterruptCallback); + IRQ_Register(IRQ_UART0, HAL_UART_IrqHandler, &g_uart0); + IRQ_SetPriority(IRQ_UART0, 6); /* 6 is priority value */ + IRQ_EnableN(IRQ_UART0); + g_uart0.dmaHandle = &g_dmac; + g_uart0.uartDmaTxChn = 0; + HAL_UART_RegisterCallBack(&g_uart0, UART_WRITE_DMA_FINISH, (UART_CallbackType)UART0_TXDMACallback); + HAL_UART_RegisterCallBack(&g_uart0, UART_WRITE_IT_FINISH, (UART_CallbackType)UART0WriteInterruptCallback); +} + +static void IOConfig(void) +{ + SYSCTRL0->SC_SYS_STAT.BIT.update_mode = 0; + SYSCTRL0->SC_SYS_STAT.BIT.update_mode_clear = 1; + /* Config PIN39 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO0_3_AS_UART0_TXD); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO0_3_AS_UART0_TXD, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO0_3_AS_UART0_TXD, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO0_3_AS_UART0_TXD, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO0_3_AS_UART0_TXD, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN40 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO0_4_AS_UART0_RXD); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO0_4_AS_UART0_RXD, PULL_UP); /* Pull-up and Pull-down, UART RX recommend PULL_UP */ + HAL_IOCMG_SetPinSchmidtMode(GPIO0_4_AS_UART0_RXD, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO0_4_AS_UART0_RXD, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO0_4_AS_UART0_RXD, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN1 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO0_7_AS_ACMP0_OUT); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO0_7_AS_ACMP0_OUT, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO0_7_AS_ACMP0_OUT, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO0_7_AS_ACMP0_OUT, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO0_7_AS_ACMP0_OUT, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN48 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO0_6_AS_ACMP_N2); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO0_6_AS_ACMP_N2, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO0_6_AS_ACMP_N2, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO0_6_AS_ACMP_N2, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO0_6_AS_ACMP_N2, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN47 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO0_5_AS_ACMP_P2); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO0_5_AS_ACMP_P2, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO0_5_AS_ACMP_P2, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO0_5_AS_ACMP_P2, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO0_5_AS_ACMP_P2, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN4 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO2_6_AS_PGA0_N0); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO2_6_AS_PGA0_N0, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO2_6_AS_PGA0_N0, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO2_6_AS_PGA0_N0, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO2_6_AS_PGA0_N0, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN5 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO2_5_AS_PGA0_P0); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO2_5_AS_PGA0_P0, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO2_5_AS_PGA0_P0, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO2_5_AS_PGA0_P0, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO2_5_AS_PGA0_P0, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN3 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO2_7_AS_PGA0_OUT); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO2_7_AS_PGA0_OUT, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO2_7_AS_PGA0_OUT, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO2_7_AS_PGA0_OUT, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO2_7_AS_PGA0_OUT, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN7 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO3_7_AS_ADC_AIN9); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO3_7_AS_ADC_AIN9, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO3_7_AS_ADC_AIN9, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO3_7_AS_ADC_AIN9, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO3_7_AS_ADC_AIN9, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN2 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO5_1_AS_ADC_AIN5); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO5_1_AS_ADC_AIN5, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO5_1_AS_ADC_AIN5, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO5_1_AS_ADC_AIN5, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO5_1_AS_ADC_AIN5, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN14 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO4_7_AS_ADC_AIN15); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO4_7_AS_ADC_AIN15, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO4_7_AS_ADC_AIN15, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO4_7_AS_ADC_AIN15, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO4_7_AS_ADC_AIN15, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN11 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO1_5_AS_PGA1_P0); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO1_5_AS_PGA1_P0, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO1_5_AS_PGA1_P0, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO1_5_AS_PGA1_P0, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO1_5_AS_PGA1_P0, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN12 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO1_6_AS_PGA1_N0); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO1_6_AS_PGA1_N0, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO1_6_AS_PGA1_N0, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO1_6_AS_PGA1_N0, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO1_6_AS_PGA1_N0, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN13 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO1_7_AS_PGA1_OUT); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO1_7_AS_PGA1_OUT, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO1_7_AS_PGA1_OUT, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO1_7_AS_PGA1_OUT, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO1_7_AS_PGA1_OUT, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN35 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO2_3_AS_GPIO2_3); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO2_3_AS_GPIO2_3, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO2_3_AS_GPIO2_3, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO2_3_AS_GPIO2_3, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO2_3_AS_GPIO2_3, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN27 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO1_0_AS_GPIO1_0); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO1_0_AS_GPIO1_0, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO1_0_AS_GPIO1_0, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO1_0_AS_GPIO1_0, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO1_0_AS_GPIO1_0, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN41 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO2_4_AS_GPIO2_4); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO2_4_AS_GPIO2_4, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO2_4_AS_GPIO2_4, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO2_4_AS_GPIO2_4, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO2_4_AS_GPIO2_4, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN19 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO3_0_AS_APT0_PWMA); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO3_0_AS_APT0_PWMA, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO3_0_AS_APT0_PWMA, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO3_0_AS_APT0_PWMA, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO3_0_AS_APT0_PWMA, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN23 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO4_0_AS_APT0_PWMB); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO4_0_AS_APT0_PWMB, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO4_0_AS_APT0_PWMB, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO4_0_AS_APT0_PWMB, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO4_0_AS_APT0_PWMB, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN20 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO3_1_AS_APT1_PWMA); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO3_1_AS_APT1_PWMA, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO3_1_AS_APT1_PWMA, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO3_1_AS_APT1_PWMA, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO3_1_AS_APT1_PWMA, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN24 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO4_1_AS_APT1_PWMB); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO4_1_AS_APT1_PWMB, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO4_1_AS_APT1_PWMB, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO4_1_AS_APT1_PWMB, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO4_1_AS_APT1_PWMB, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN21 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO3_2_AS_APT2_PWMA); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO3_2_AS_APT2_PWMA, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO3_2_AS_APT2_PWMA, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO3_2_AS_APT2_PWMA, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO3_2_AS_APT2_PWMA, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN25 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO4_2_AS_APT2_PWMB); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO4_2_AS_APT2_PWMB, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO4_2_AS_APT2_PWMB, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO4_2_AS_APT2_PWMB, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO4_2_AS_APT2_PWMB, DRIVER_RATE_2); /* Output signal edge fast/slow */ +} + +static void APT_SyncMasterInit(void) +{ + HAL_APT_MasterSyncInit(&g_apt0, APT_SYNC_OUT_ON_CNTR_ZERO); +} + +static void APT_SyncSlaveInit(void) +{ + APT_SlaveSyncIn aptSlave; + + aptSlave.cntPhase = 0; /* counter phase value */ + aptSlave.syncCntMode = APT_COUNT_MODE_AFTER_SYNC_UP; + aptSlave.syncInSrc = APT_SYNCIN_SRC_APT0_SYNCOUT; /* sync source selection */ + aptSlave.cntrSyncSrc = APT_CNTR_SYNC_SRC_SYNCIN; + HAL_APT_SlaveSyncInit(&g_apt1, &aptSlave); + + aptSlave.cntPhase = 0; /* counter phase value */ + aptSlave.syncCntMode = APT_COUNT_MODE_AFTER_SYNC_UP; + aptSlave.syncInSrc = APT_SYNCIN_SRC_APT0_SYNCOUT; /* sync source selection */ + aptSlave.cntrSyncSrc = APT_CNTR_SYNC_SRC_SYNCIN; + HAL_APT_SlaveSyncInit(&g_apt2, &aptSlave); + +} + +void SystemInit(void) +{ + IOConfig(); + DMA_Init(); + UART0_Init(); + ACMP0_Init(); + APT0_Init(); + APT1_Init(); + APT2_Init(); + ADC0_Init(); + PGA0_Init(); + PGA1_Init(); + TIMER0_Init(); + TIMER1_Init(); + GPIO_Init(); + + APT_SyncMasterInit(); + APT_SyncSlaveInit(); + + /* USER CODE BEGIN system_init */ + /* USER CODE END system_init */ +} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/main.c b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/main.c new file mode 100644 index 00000000..d6ab767c --- /dev/null +++ b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/main.c @@ -0,0 +1,71 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file main.c + * @author MCU Driver Team + * @brief Main program body. + * @date 2025-04-14 14:05:10 + */ + +#include "typedefs.h" +#include "feature.h" +#include "mcs_motor_process.h" +#include "main.h" +/* USER CODE BEGIN 0 */ +/* USER CODE 区域内代码不会被覆盖,区域外会被生成的默认代码覆盖(其余USER CODE 区域同理) */ +/* 建议用户放置头文件 */ +/* USER CODE END 0 */ +ACMP_Handle g_acmp0; +PGA_Handle g_pga0; +PGA_Handle g_pga1; +TIMER_Handle g_timer0; +TIMER_Handle g_timer1; +UART_Handle g_uart0; +APT_Handle g_apt0; +APT_Handle g_apt1; +APT_Handle g_apt2; +ADC_Handle g_adc0; +DMA_Handle g_dmac; +GPIO_Handle g_gpio2; +GPIO_Handle g_gpio1; +/* USER CODE BEGIN 1 */ +/* 建议用户定义全局变量、结构体、宏定义或函数声明等 */ +/* USER CODE END 1 */ + +int main(void) +{ + /* USER CODE BEGIN 2 */ + /* 建议用户放置初始化代码或启动代码等 */ + /* USER CODE END 2 */ + MotorMainProcess(); + /* USER CODE BEGIN 3 */ + /* 建议用户放置初始配置代码 */ + /* USER CODE END 3 */ + while (1) { + /* USER CODE BEGIN 4 */ + /* 建议用户放置周期性执行代码 */ + /* USER CODE END 4 */ + } + /* USER CODE BEGIN 5 */ + /* 建议用户放置代码流程 */ + /* USER CODE END 5 */ + return BASE_STATUS_OK; +} + +/* USER CODE BEGIN 6 */ +/* 建议用户放置自定义函数 */ +/* USER CODE END 6 */ \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/inc/mcs_carrier.h b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/inc/mcs_carrier.h new file mode 100644 index 00000000..dea3ad66 --- /dev/null +++ b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/inc/mcs_carrier.h @@ -0,0 +1,142 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_carrier.h + * @author MCU Algorithm Team + * @brief This file provides functions declaration for carrier interrupt processing function. + */ +#ifndef McuMagicTag_MCS_CARRIER_H +#define McuMagicTag_MCS_CARRIER_H + +#include "mcs_status.h" +#include "mcs_mtr_param.h" +#include "mcs_svpwm.h" +#include "mcs_curr_ctrl.h" +#include "mcs_if_ctrl.h" +#include "mcs_ramp_mgmt.h" +#include "mcs_spd_ctrl.h" +#include "mcs_fosmo.h" +#include "mcs_smo_4th.h" +#include "mcs_pll.h" +#include "mcs_startup.h" +#include "mcs_r1_svpwm.h" +#include "mcs_fw_ctrl.h" +#include "mcs_prot_user.h" + +typedef void (*MCS_ReadCurrUvwCb)(UvwAxis *CurrUvw); +typedef void (*MCS_SetPwmDutyCb)(UvwAxis *dutyUvwLeft, UvwAxis *dutyUvwRight); +typedef void (*MCS_SetADCTriggerTimeCb)(unsigned short cntCmpSOCA, unsigned short cntCmpSOCB); + +/** + * @brief motor control FSM state define. + * @details motor control FSM state type: + * + FSM_IDLE -- IDLE state, system startup default. + * + FSM_OFFSET_CALIB -- Self calibrate, for ADC init. + * + FSM_CAP_CHARGE -- IPM cap charge. + * + FSM_CLEAR -- Clear before every run. + * + FSM_LOCATE -- Rotor position pre-locate. + * + FSM_STARTUP -- Start up. + * + FSM_SWITCH -- Transition state, control from open to closed loop. + * + FSM_RUN -- Normal running state. + * + FSM_WAIT_STOP -- Wait stopping. + * + FSM_STOP -- Normal stop. + * + FSM_FAULT -- Fault state, waiting for user process. + */ +typedef enum { + FSM_IDLE = 0, + FSM_OFFSET_CALIB, + FSM_CAP_CHARGE, + FSM_CLEAR, + FSM_LOCATE, + FSM_STARTUP, + FSM_SWITCH, + FSM_RUN, + FSM_WAIT_STOP, + FSM_STOP, + FSM_FAULT +} FsmState; + +/** + * @brief Sampling mode. + */ +typedef enum { + DUAL_RESISTORS = 0, + SINGLE_RESISTOR = 1 +} SampleMode; + +/** + * @brief Motor control data structure + */ +typedef struct { + unsigned char motorStateFlag; /**< Motor start/stop status flag. */ + float spdCmdHz; /**< External input speed command value. */ + float axisAngle; /**< Angle of the synchronous coordinate system. */ + float spdRefHz; /**< Command value after speed ramp management. */ + float currCtrlPeriod; /**< current loop control period. */ + float adc0Compensate; /**< ADC0 softwaretrim compensate value. */ + float adc1Compensate; /**< ADC1 softwaretrim compensate value. */ + float udc; /**< Bus voltage. */ + float powerBoardTemp; /**< Power boart surface temperature. */ + unsigned short aptMaxcntCmp; /**< Apt Maximum Comparison Count. */ + float adcCurrCofe; /**< Adc current sampling cofeature. */ + + unsigned short sysTickCnt; /**< System Timer Tick Count. */ + unsigned short capChargeTickNum; /**< Bootstrap Capacitor Charge Tick Count. */ + volatile unsigned int msTickCnt; /**< Millisecond-level counter, which can be used in 1-ms and 5-ms tasks. */ + unsigned short msTickNum; /**< Number of ticks corresponding to 1 ms. */ + char obserType; /**< Set Observer Type. */ + char controlMode; /**< Set foc control or sixstep bldc control mode or others. */ + char spdAdjustMode; /**< Set speed adjust mode. */ + char uartConnectFlag; /**< Uart connect success flag. */ + short uartHeartDetCnt; /**< Uart connect heart detect count. */ + float uartTimeStamp; /**< Uart data time stamp. */ + SysStatusReg statusReg; /**< System status. */ + FsmState stateMachine; /**< Motor Control State Machine. */ + + SampleMode sampleMode; /**< Current sampling mode setting: single resistor, double resistor. */ + MOTOR_Param mtrParam; /**< Motor parameters. */ + FOSMO_Handle smo; /**< SMO observer handle. */ + SMO4TH_Handle smo4th; /**< SMO 4th observer handle. */ + IF_Handle ifCtrl; /**< I/F control handle. */ + SVPWM_Handle sv; /**< Dual-resistance/three-resistance sampling SVPWM Handle. */ + R1SVPWM_Handle r1Sv; /**< Single-resistance phase-shifted SVPWM handle. */ + RMG_Handle spdRmg; /**< Ramp management struct for the speed controller input reference. */ + SPDCTRL_Handle spdCtrl; /**< Speed loop Control Handle. */ + CURRCTRL_Handle currCtrl; /**< Current loop control handle. */ + STARTUP_Handle startup; /**< Startup Switch Handle. */ + FW_Handle fw; /**< Flux-Weakening Handle. */ + + DqAxis idqRef; /**< Command value of the dq axis current. */ + UvwAxis currUvw; /**< Three-phase current sampling value. */ + AlbeAxis iabFbk; /**< αβ-axis current feedback value. */ + DqAxis idqFbk; /**< Current feedback value of the dq axis. */ + DqAxis vdqRef; /**< Current loop output dq voltage. */ + AlbeAxis vabRef; /**< Current loop output voltage αβ. */ + UvwAxis dutyUvw; /**< UVW three-phase duty cycle. */ + UvwAxis dutyUvwLeft; /**< Single Resistor UVW Three-Phase Left Duty Cycle. */ + UvwAxis dutyUvwRight; /**< Single Resistor UVW Three-Phase Right Duty Cycle. */ + + MCS_ReadCurrUvwCb readCurrUvwCb; /**< Read current callback function. */ + MCS_SetPwmDutyCb setPwmDutyCb; /**< Set the duty cycle callback function. */ + MCS_SetADCTriggerTimeCb setADCTriggerTimeCb; /**< Sets the ADC trigger point callback function. */ + + MotorProtStatus_Handle prot; /**< Motor protect status detection flag bit handle. */ +} MTRCTRL_Handle; + +void MCS_CarrierProcess(MTRCTRL_Handle *mtrCtrl); + +#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/inc/mcs_chip_config.h b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/inc/mcs_chip_config.h new file mode 100644 index 00000000..38c00b5d --- /dev/null +++ b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/inc/mcs_chip_config.h @@ -0,0 +1,76 @@ +/** + * @ Copyright (c) HiSilicon (Shanghai) Technologies Co., Ltd. 2022-2023. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_chip_config.h + * @author MCU Algorithm Team + * @brief This file provides config macros for ECMCU105H/ECBMCU201MPC app. + */ + +#ifndef McuMagicTag_MCS_CHIPCONFIG_H +#define McuMagicTag_MCS_CHIPCONFIG_H + +#include "feature.h" + +#if defined (CHIP_3061MNPICA) || defined (CHIP_3061MNPIKA) || defined (CHIP_3061MNNICA) || \ + defined (CHIP_3061MNNIKA) || defined (CHIP_3061MNPIC8) || defined(CHIP_3061MNNIC8) || \ + defined (CHIP_3061MNPIK8) || defined (CHIP_3061MNNIK8) + + #define ADCPTT_HANDLE g_adc0 + #define ADCRESIS_HANDLE g_adc0 + #define ADCUDC_HANDLE g_adc0 + #define ADCU_HANDLE g_adc0 + #define ADCW_HANDLE g_adc0 + #define LED1_HANDLE g_gpio2 + #define LED2_HANDLE g_gpio1 + #define LED2_PIN GPIO_PIN_0 + #define LED1_PIN GPIO_PIN_3 + #define ADCPTTSOCNUM ADC_SOC_NUM0 + #define ADCUSOCNUM ADC_SOC_NUM1 + #define ADCRESISSOCNUM ADC_SOC_NUM3 + #define ADCUDCSOCNUM ADC_SOC_NUM4 + #define ADCWSOCNUM ADC_SOC_NUM6 + + #define ADC0COMPENSATE 2037.0f + #define ADC1COMPENSATE 2027.0f + +#endif + +#if defined (CHIP_3065HRPIRZ) || defined (CHIP_3065ARPIRZ) || defined (CHIP_3066MNPIRH) || \ + defined (CHIP_3065PNPIRH) || defined (CHIP_3065PNPIRE) || defined (CHIP_3065PNPIRA) + + #define ADCU_HANDLE g_adc0 + #define ADCW_HANDLE g_adc1 + #define ADCRESIS_HANDLE g_adc2 + #define ADCUDC_HANDLE g_adc2 + #define ADCPTT_HANDLE g_adc2 + #define LED1_HANDLE g_gpio0 + #define LED2_HANDLE g_gpio0 + #define LED2_PIN GPIO_PIN_6 + #define LED1_PIN GPIO_PIN_7 + #define ADCPTTSOCNUM ADC_SOC_NUM0 + #define ADCRESISSOCNUM ADC_SOC_NUM1 + #define ADCUSOCNUM ADC_SOC_NUM8 + #define ADCWSOCNUM ADC_SOC_NUM8 + #define ADCUDCSOCNUM ADC_SOC_NUM14 + + #define ADC0COMPENSATE 2033.0f + #define ADC1COMPENSATE 2070.0f + +#endif + + +#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/inc/mcs_ctlmode_config.h b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/inc/mcs_ctlmode_config.h new file mode 100644 index 00000000..2a6f38cc --- /dev/null +++ b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/inc/mcs_ctlmode_config.h @@ -0,0 +1,73 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_ctlmode_config.h + * @author MCU Algorithm Team + * @brief This file provides config macros for ECMCU105H app. + */ + + /* Define to prevent recursive inclusion ------------------------------------------------------- */ +#ifndef McuMagicTag_MCS_CTLMODECONFIG_H +#define McuMagicTag_MCS_CTLMODECONFIG_H + +#include "debug.h" +#include "typedefs.h" + +typedef enum { + FOC_STARTUP_IF = 0, + FOC_STARTUP_VF, + FOC_STARTUP_HFI +} MOTOR_STARTUPMODE_CONFIG; + +typedef enum { + FOC_OBSERVERTYPE_SMO1TH = 0, + FOC_OBSERVERTYPE_SMO1TH_PLL, + FOC_OBSERVERTYPE_SMO4TH, + FOC_OBSERVERTYPE_SMO4TH_PLL, + FOC_OBSERVERTYPE_LUNBORG, + FOC_OBSERVERTYPE_FLUX +} MOTOR_OBSERVERTYPE_CONFIG; + +typedef enum { + FOC_CONTROLMODE_SPEED = 0, + FOC_CONTROLMODE_TORQUE +} MOTOR_CONTROLMODE_CONFIG; + +typedef enum { + FOC_CURQAXISPID_PARAMS = 0, + FOC_CURDAXISPID_PARAMS, + FOC_SPDPID_PARAMS +} MOTOR_PID_SET; + +typedef enum { + MOTOR_PARAMS_BASE = 0, + MOTOR_PARAMS_SPECIAL, + MOTOR_PARAMS_BOARD +} MOTOR_PARAMS_SET; + +typedef enum { + CUST_SPEED_ADJUST = 0, + HOST_SPEED_ADJUST +} MODE_ADSPEED_CONFIG; + +typedef enum { + CONNECTING = 0, + CONNECTED, + DISCONNECT +} UART_STATUS; + +#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/inc/mcs_motor_process.h b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/inc/mcs_motor_process.h new file mode 100644 index 00000000..783bf214 --- /dev/null +++ b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/inc/mcs_motor_process.h @@ -0,0 +1,45 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_motor_process.h + * @author MCU Algorithm Team + * @brief This file provides motor sample functions declaration for ECMCU105H board. + */ +#ifndef McuMagicTag_MCS_MOTOR_PROCESS_H +#define McuMagicTag_MCS_MOTOR_PROCESS_H + +/** + * @brief phase sequence. + */ +typedef enum { + PHASE_U = 0, + PHASE_V, + PHASE_W, + PHASE_MAX_NUM +} MCS_PhaseEnum; + +/** + * @brief key state. + */ +typedef enum { + KEY_DOWN = 0, + KEY_UP = 1, +} KEY_State; + +int MotorMainProcess(void); + +#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/inc/mcs_status.h b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/inc/mcs_status.h new file mode 100644 index 00000000..19510c78 --- /dev/null +++ b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/inc/mcs_status.h @@ -0,0 +1,188 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_status.h + * @author MCU Algorithm Team + * @brief This file provides functions declaration of system status. + */ + +/* Define to prevent recursive inclusion ------------------------------------------------------- */ +#ifndef McuMagicTag_MCS_STATUS_H +#define McuMagicTag_MCS_STATUS_H + +/* Includes ------------------------------------------------------------------------------------ */ +#include "typedefs.h" +#include "mcs_assert.h" + +/* Typedef definitions ------------------------------------------------------------------------- */ +/** + * @brief System status define + */ +typedef union { + unsigned short all; + struct { + unsigned short cmdStart : 1; /**< Indicates that a start system command has been received. */ + unsigned short cmdStop : 1; /**< Indicates that a stop system command has been received. */ + unsigned short isRunning : 1; /**< Indicates that the system is running (enable signal) */ + unsigned short sysError : 1; /**< Indicates that the system reports an error. */ + unsigned short poweron : 1; /**< Indicates that the power-on initialization phase is complete. */ + unsigned short capcharge : 1; /**< Indicates that the bootstrap capacitor charging phase is complete. */ + unsigned short adczero : 1; /**< The current sampling point is reset to zero after power-on. */ + } Bit; +} SysStatusReg; + +/** + * @brief Get status of Bit cmdStart. + * @param sysStatus System status register handle. + * @retval Status of Bit cmdStart. + */ +static inline bool SysGetCmdStart(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + bool ret; + ret = (sysStatus->Bit.cmdStart == 1) ? true : false; + return ret; +} + +/** + * @brief Set Bit cmdStart. + * @param sysStatus System status register handle. + * @retval None. + */ +static inline void SysCmdStartSet(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + sysStatus->Bit.cmdStart = 1; +} + +/** + * @brief Clear Bit cmdStart. + * @param handle System status register handle. + * @retval None. + */ +static inline void SysCmdStartClr(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + sysStatus->Bit.cmdStart = 0; +} + +/** + * @brief Get status of Bit cmdStop. + * @param sysStatus System status register handle. + * @retval Status of Bit cmdStart. + */ +static inline bool SysGetCmdStop(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + bool ret; + ret = (sysStatus->Bit.cmdStop == 1) ? true : false; + return ret; +} + +/** + * @brief Set Bit cmdStop. + * @param sysStatus System status register handle. + * @retval None. + */ +static inline void SysCmdStopSet(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + sysStatus->Bit.cmdStop = 1; +} + +/** + * @brief Clear Bit cmdStop. + * @param sysStatus System status register handle. + * @retval None. + */ +static inline void SysCmdStopClr(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + sysStatus->Bit.cmdStop = 0; +} + +/** + * @brief Get status of Bit isRunning. + * @param sysStatus System status register handle. + * @retval Status of Bit isRunning. + */ +static inline bool SysIsRunning(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + bool ret; + ret = (sysStatus->Bit.isRunning == 1) ? true : false; + return ret; +} + +/** + * @brief Set Bit isRuning. + * @param sysStatus System status register handle. + * @retval None. + */ +static inline void SysRunningSet(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + sysStatus->Bit.isRunning = 1; +} + +/** + * @brief Clear Bit isRuning. + * @param sysStatus System status register handle. + * @retval None. + */ +static inline void SysRunningClr(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + sysStatus->Bit.isRunning = 0; +} + +/** + * @brief Get status of Bit sysError. + * @param sysStatus System status register handle. + * @retval Status of Bit sysError. + */ +static inline bool SysIsError(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + bool ret; + ret = (sysStatus->Bit.sysError == 1) ? true : false; + return ret; +} + +/** + * @brief Set Bit sysError. + * @param sysStatus System status register handle. + * @retval None. + */ +static inline void SysErrorSet(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + sysStatus->Bit.sysError = 1; +} + +/** + * @brief Clear Bit sysError. + * @param sysStatus System status register handle. + * @retval None. + */ +static inline void SysErrorClr(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + sysStatus->Bit.sysError = 0; +} + +#endif diff --git a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/inc/mcs_user_config.h b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/inc/mcs_user_config.h new file mode 100644 index 00000000..8d4cb216 --- /dev/null +++ b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/inc/mcs_user_config.h @@ -0,0 +1,108 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_user_config.h + * @author MCU Algorithm Team + * @brief This file provides functions declaration of user config parameters. + */ +#ifndef McuMagicTag_MCS_CONFIG_H +#define McuMagicTag_MCS_CONFIG_H + +#include "debug.h" +#include "typedefs.h" + +#define SMO4TH + +#define SYSTICK_PERIOD_US 500u /* systick period. */ + +#define INV_CAP_CHARGE_MS 3u /* Capacitor charging time. */ + +#define INV_VOLTAGE_BUS 24.0f /* Bus voltage, V. */ + +#define CTRL_CURR_PERIOD 0.0001f /* carrier ISR period, 100us. */ +#define CTRL_SYSTICK_PERIOD 0.0005f /* systick control period, 500us. */ + +/* Duty of sample window, the real time is 0.06*50us = 3us. */ +#define SAMPLE_WINDOW_DUTY 0.06f + +/* Duty of sample point shift as flip point, the real time is 0.008*50us = 0.4us. */ +#define SAMPLE_POINT_SHIFT 0.008f + +/* Sampling resistance 200mOhm 0.0013295 */ +#define ADC_CURR_COFFI 0.0013295f /* 3.3/4096/3.03/0.2 pga: 3.03, 200mohm. */ + +#define APT_U APT0_BASE /* Base address of U phase APT module. */ +#define APT_V APT1_BASE /* Base address of V phase APT module. */ +#define APT_W APT2_BASE /* Base address of W phase APT module. */ + +/* FOSMO */ +#define FOSMO_GAIN 4.0f /* SMO gain */ +#define FOSMO_LAMBDA 2.0f /* SMO coefficient of cut-off frequency, its value = lambda * we. */ +#define FOSMO_EMF_CUTOFF_FREQ 2.0f /* SMO back emf cutoff frequency. */ +#define SPEED_FILTER_CUTOFF_FREQUENCY 40.0f /* SMO speed cutoff frequency of speed filter. */ +#define FOSMO_PLL_BDW 30.0f /* SMO PLL bandwidth. */ + +/* SMO4TH */ +#define SMO4TH_PLL_BDW 30.0f /* SMO4TH PLL Bandwidth. */ +#define SMO4TH_KD 300.0f /* SMO4TH parameters KD. */ +#define SMO4TH_KQ 600.0f /* SMO4TH parameters KQ. */ +#define SMO4TH_SPD_FILTER_CUTOFF_FREQ 40.0f /* SMO4TH speed cutoff frequency of speed filter. */ + +/* User_Commond */ +#define CTRL_IF_CURR_AMP_A 0.7f /* IF control current amplitude. */ +#define USER_TARGET_SPD_HZ 100.0f /* User-defined target speed value. */ +#define USER_SWITCH_SPDBEGIN_HZ 30.0f /* Start of handover interval. */ +#define USER_SWITCH_SPDEND_HZ (USER_SWITCH_SPDBEGIN_HZ + 3.0f) /* End of handover period. */ +#define USER_MAX_SPD_HZ 29.0f /* User-defined maximum speed value. */ +#define USER_MIN_SPD_HZ 5.0f /* User-defined minimum speed value. */ +#define USER_SPD_SLOPE 50.0f /* slope of velocity change. */ +#define USER_CURR_SLOPE (CTRL_IF_CURR_AMP_A * 10.0f) /* Current change slope. */ + +/* PID PARAMS */ +#define CURRQAXIS_KP 5.023202f /* Current loop Q axis Kp. */ +#define CURRQAXIS_KI 20612.84f /* Current loop Q axis Ki. */ +#define CURRDAXIS_KP 3.477114f /* Current loop D axis Kp. */ +#define CURRDAXIS_KI 20612.84f /* Current loop D axis Ki. */ +/* Current loop PID output lower limit. */ +#define CURR_LOWERLIM (-INV_VOLTAGE_BUS * ONE_DIV_SQRT3 * 0.98f) +/* Current loop PID output upper limit. */ +#define CURR_UPPERLIM (INV_VOLTAGE_BUS * ONE_DIV_SQRT3 * 0.98f) + +#define SPD_KP 0.00505f /* Speed loop Kp. */ +#define SPD_KI 0.012f /* Speed loop Ki. */ +#define SPD_LOWERLIM (-0.105f) /* Speed loop PID output lower limit. */ +#define SPD_UPPERLIM 0.105f /* Speed loop PID output upper limit. */ + +/* MOTOR PARAMS */ +/* Np, Rs, Ld, Lq, Psif, J, Nmax, Currmax, PPMR, zShift. */ +/* mtrPsif & mtrJ parameter is not used in this project, temporarily set to 0. */ +#define MOTORPARAM_DEFAULTS { \ + .mtrNp = 14, \ + .mtrRs = 10.1f, \ + .mtrLd = 0.010f, \ + .mtrLq = 0.010f, \ + .mtrPsif = 0.0f, \ + .mtrJ = 0.0f, \ + .maxElecSpd = 29.0f, \ + .maxCurr = 1.0f, \ + .busVolt = INV_VOLTAGE_BUS, \ +} + +/* Bus voltage sampling conversion factor. */ +#define ADC_UDC_COFFI 0.01289f /* 0.01289 = 3.3/4096*192/12. */ + +#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_cmm.c b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_cmm.c new file mode 100644 index 00000000..99766bdf --- /dev/null +++ b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_cmm.c @@ -0,0 +1,43 @@ +/** + * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_prot_cmm.c + * @author MCU Algorithm Team + * @brief This file contains protection common api definition. + */ + +/* Includes ------------------------------------------------------------------------------------ */ +#include "mcs_prot_cmm.h" +#include "apt.h" +#include "mcs_assert.h" + +/** + * @brief Safty-pulse-off function execution to turn off all the power devices. + * @param aptAddr Three-phase APT address pointer. + * @retval None. + */ +void ProtSpo_Exec(APT_RegStruct **aptAddr) +{ + MCS_ASSERT_PARAM(aptAddr != NULL); + /**< Turn off all the six power devices of the inverter. */ + for (unsigned int i = 0; i < MOTOR_PHASE_NUMBER; i++) { + APT_RegStruct *aptx = (APT_RegStruct *)(aptAddr[i]); + DCL_APT_EnableSwContPWMAction(aptx, APT_PWM_CHANNEL_A); + DCL_APT_EnableSwContPWMAction(aptx, APT_PWM_CHANNEL_B); + DCL_APT_ForcePWMOutputLow(aptx); + } +} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_cmm.h b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_cmm.h new file mode 100644 index 00000000..104a5dec --- /dev/null +++ b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_cmm.h @@ -0,0 +1,136 @@ +/** + * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_prot_cmm.h + * @author MCU Algorithm Team + * @brief This file contains protection function common data struct and api declaration. + */ + +/* Define to prevent recursive inclusion ------------------------------------------------------- */ +#ifndef McuMagicTag_MCS_PROT_CMM_H +#define McuMagicTag_MCS_PROT_CMM_H + +#include "typedefs.h" +#include "apt_ip.h" + +/* Macro definitions --------------------------------------------------------------------------- */ +#define MOTOR_PHASE_NUMBER (3) + +#define PROT_VAL_THRESHOLD_NUMS (4) +#define PROT_VAL_THRESHOLD_0 (0) +#define PROT_VAL_THRESHOLD_1 (1) +#define PROT_VAL_THRESHOLD_2 (2) +#define PROT_VAL_THRESHOLD_3 (3) + +#define PROT_LIMIT_TIME_NUMS (3) +#define PROT_LIMIT_TIME_0 (0) +#define PROT_LIMIT_TIME_1 (1) +#define PROT_LIMIT_TIME_2 (2) + +#define MOTOR_PHASE_NUMBER (3) + +/**< Motor error status definition. */ +typedef union { + int all; + struct { + unsigned short overCurrErr : 1; /**< Indicates that phase current(s) is over protected value. */ + unsigned short overVoltErr : 1; /**< Indicates that dc-link voltage is over protected value. */ + unsigned short lowerVoltErr : 1; /**< Indicates that dc-link voltage is lower than protected value */ + unsigned short overIpmTempErr : 1; /**< Indicates that IPM temperature is over protected value. */ + unsigned short revRotErr : 1; /**< Indicates that motor negtive direction. */ + unsigned short motorStalling : 1; /**< Indicates that rotor is stalling. */ + unsigned short overMotorTempErr : 1; /**< Indicates that three phase currents is out-of-balance. */ + unsigned short posSnsrCommsErr : 1; /**< Indicates that position sensor communication is lost with MCU. */ + unsigned short posSnsrFuncErr : 1; /**< Indicates that position sensor reports function error. */ + unsigned short posSnsrCalibrErr : 1; /**< Indicates that position sensor fails to calibrate itself. */ + unsigned short currOutOfBalance : 1; /**< Indicates that the rotor is reverse rotation.*/ + unsigned short phsOpenErr : 1; /**< Indicates that phase winding(s) is open. */ + unsigned short phsU : 1; /**< Indicates that u phase fails when phsOpenErr occurs. */ + unsigned short phsV : 1; /**< Indicates that v phase fails when phsOpenErr occurs. */ + unsigned short phsW : 1; /**< Indicates that w phase fails when phsOpenErr occurs. */ + unsigned short multiPhs : 1; /**< Indicates that multi-phases fail when phsOpenErr occurs.*/ + } Bit; +} MotorErrStatusReg; + +/**< Protection Status Bit Definition */ +typedef enum { + OCP_ERR_BIT, + OVP_ERR_BIT, + LVP_ERR_BIT, + OTP_IPM_ERR_BIT, + OTP_MOTOR_ERR_BIT, + STALLING_ERR_BIT, + CURR_OUT_BALANCE_ERR_BIT, + POS_COMMS_ERR_BIT, + POS_FUNC_ERR_BIT, + POS_CALIB_ERR_BIT, + REV_ROT_ERR_BIT, + PHS_OPEN_ERR_BIT, + PHS_U_ERR_BIT, + PHS_V_ERR_BIT, + PHS_W_ERR_BIT, + PHS_MULTI_ERR_BIT, +} PROT_ErrBit; + +/**< Motor error protection level. */ +typedef enum { + PROT_LEVEL_0 = 0, + PROT_LEVEL_1, + PROT_LEVEL_2, + PROT_LEVEL_3, + PROT_LEVEL_4 /**< The greater level number, the severe error is. */ +} PROT_Level; + +/** + * @brief Obtains the status of a bit of data. + * @param data data. + * @param bits Number of digits. + * @retval Bit status. + */ +static inline bool GetBit(int data, unsigned short bit) +{ + bool ret; + ret = ((data >> bit) & 1); + return ret; +} + +/** + * @brief Sets the status of a bit of data. + * @param data data. + * @param bit The setted bit. + * @retval None. + */ +static inline void SetBit(int *data, unsigned char bit) +{ + *data |= (1 << bit); +} + +/** + * @brief Clear the status of a bit of data. + * @param data data. + * @param bit The Clear bit. + * @retval None. + */ +static inline void ClearBit(int *data, unsigned char bit) +{ + *data &= ~(1 << bit); +} + +/**< Protection action. */ +void ProtSpo_Exec(APT_RegStruct **aptAddr); + +#endif diff --git a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user.c b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user.c new file mode 100644 index 00000000..e97876b7 --- /dev/null +++ b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user.c @@ -0,0 +1,137 @@ +/** + * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_prot_user.c + * @author MCU Algorithm Team + * @brief This file contains user protection data struct, inquiry api and initialization api definition. + */ + +/* Includes ------------------------------------------------------------------------------------ */ +#include "mcs_prot_user.h" +#include "mcs_prot_user_config.h" +#include "mcs_assert.h" + +/** + * @brief Get motor over current error status. + * @param motorErrStatus Motor error status. + * @retval Error status. + */ +bool IsMotorOverCurrErr(MotorErrStatusReg motorErrStatus) +{ + if (motorErrStatus.Bit.overCurrErr) { + return true; + } else { + return false; + } +} + +/** + * @brief Get motor over dc-link voltage error status. + * @param motorErrStatus Motor error status. + * @retval Error status. + */ +bool IsDcLinkOverVoltErr(MotorErrStatusReg motorErrStatus) +{ + if (motorErrStatus.Bit.overVoltErr) { + return true; + } else { + return false; + } +} + +/** + * @brief Get motor lower dc-link voltage error status. + * @param motorErrStatus Motor error status. + * @retval Error status. + */ +bool IsDcLinkLowerVoltErr(MotorErrStatusReg motorErrStatus) +{ + if (motorErrStatus.Bit.lowerVoltErr) { + return true; + } else { + return false; + } +} + +/** + * @brief Get motor over Ipm temperature error status. + * @param motorErrStatus Motor error status. + * @retval Error status. + */ +bool IsIpmOverTempErr(MotorErrStatusReg motorErrStatus) +{ + if (motorErrStatus.Bit.overIpmTempErr) { + return true; + } else { + return false; + } +} + +/** + * @brief Get motor over Motor temperature error status. + * @param motorErrStatus Motor error status. + * @retval Error status. + */ +bool IsMotorOverTempErr(MotorErrStatusReg motorErrStatus) +{ + if (motorErrStatus.Bit.overMotorTempErr) { + return true; + } else { + return false; + } +} + +/** + * @brief Get motor stalling error status. + * @param motorErrStatus Motor error status. + * @retval Error status. + */ +bool IsMotorStallingErr(MotorErrStatusReg motorErrStatus) +{ + if (motorErrStatus.Bit.motorStalling) { + return true; + } else { + return false; + } +} + +/** + * @brief Clear the motor error status. + * @param motorProt Motor protection handle. + * @retval Error status. + */ +void ClearMotorErrStatus(MotorProtStatus_Handle *motorProt) +{ + MCS_ASSERT_PARAM(motorProt != NULL); + /* Clear the motor error status. */ + motorProt->motorErrStatus.all = 0x00; + OCP_Clear(&motorProt->ocp); + OVP_Clear(&motorProt->ovp); + LVP_Clear(&motorProt->lvp); + OTP_Clear(&motorProt->otp); +} + +/** + * @brief Motor protection function initialization. + * @param motorProt Motor protection handle. + * @retval Error status. + */ +void MotorProt_Init(MotorProtStatus_Handle *motorProt) +{ + MCS_ASSERT_PARAM(motorProt != NULL); + motorProt->motorErrStatus.all = 0x00; +} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user.h b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user.h new file mode 100644 index 00000000..365110e1 --- /dev/null +++ b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user.h @@ -0,0 +1,54 @@ +/** + * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_prot_user.h + * @author MCU Algorithm Team + * @brief This file contains user protection data struct, inquiry api and initialization api declaration. + */ + +/* Define to prevent recursive inclusion ------------------------------------------------------- */ +#ifndef McuMagicTag_MCS_PROT_USER_H +#define McuMagicTag_MCS_PROT_USER_H + +/* Includes ------------------------------------------------------------------------------------ */ +#include "mcs_prot_cmm.h" +#include "mcs_curr_prot.h" +#include "mcs_dc_volt_prot.h" +#include "mcs_temp_prot.h" +#include "mcs_motor_stalling.h" +#include "typedefs.h" + +typedef struct { + MotorErrStatusReg motorErrStatus; /**< Motor error status. */ + OCP_Handle ocp; /**< Over current protection. */ + OVP_Handle ovp; /**< Over dc-link voltage protection. */ + LVP_Handle lvp; /**< Lower dc-link voltage protection. */ + OTP_Handle otp; /**< Over IPM temperature protection. */ + STP_Handle stall; /**< Motor stalling protection. */ +} MotorProtStatus_Handle; + +void MotorProt_Init(MotorProtStatus_Handle *motorProt); + +/**< Inquiry motor error status */ +bool IsMotorOverCurrErr(MotorErrStatusReg motorErrStatus); +bool IsDcLinkOverVoltErr(MotorErrStatusReg motorErrStatus); +bool IsDcLinkLowerVoltErr(MotorErrStatusReg motorErrStatus); +bool IsIpmOverTempErr(MotorErrStatusReg motorErrStatus); +bool IsMotorOverTempErr(MotorErrStatusReg motorErrStatus); +bool IsMotorStallingErr(MotorErrStatusReg motorErrStatus); +void ClearMotorErrStatus(MotorProtStatus_Handle *motorProt); +#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user_config.h b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user_config.h new file mode 100644 index 00000000..832cd1be --- /dev/null +++ b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user_config.h @@ -0,0 +1,125 @@ +/** + * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_prot_user_config.h + * @author MCU Algorithm Team + * @brief This file contans user macro definition of the protection function. + */ + +/* Define to prevent recursive inclusion ------------------------------------------------------- */ +#ifndef McuMagicTag_MCS_PROT_USER_CONFIG_H +#define McuMagicTag_MCS_PROT_USER_CONFIG_H + +/* Macro definitions --------------------------------------------------------------------------- */ +/**< COMMON */ +/**< Peak phase current (A) of the motor or IPM under continuous operations. */ +#define PROT_MOTOR_RATED_CURR (1.0f) +/**< Only several continuous fault detection can trigger error status. */ +#define PROT_CNT_LIMIT (100) +/**< Only several contunuous none fault dectection can trigger elimination of error status. */ +#define RECY_CNT_LIMIT (10000) +/**< Only several contunuous none fault dectection can trigger elimination of error status. */ +#define OVER_VOLT_RECY_CNT_LIMIT (100) +/**< Only several contunuous none fault dectection can trigger elimination of error status. */ +#define LOWER_VOLT_RECY_CNT_LIMIT (100) + +/**< Over current protection */ +/**< Over current trigger value (A) when in level 1. */ +#define PROT_OVER_CURR_POW_DN1 (2.0f * PROT_MOTOR_RATED_CURR) +/**< Over current trigger value (A) when in level 2. */ +#define PROT_OVER_CURR_POW_DN2 (2.0f * PROT_MOTOR_RATED_CURR) +/**< Over current trigger value (A) when in level 3. */ +#define PROT_OVER_CURR_POW_DN3 (2.0f * PROT_MOTOR_RATED_CURR) +/**< Over current trigger value (A) when in level 4. */ +#define PROT_OVER_CURR_POW_OFF (2.0f * PROT_MOTOR_RATED_CURR) +#define PROT_OVER_CURR_RECY_DELTA (0.0035f) /**< Current gap (A) when recovers from protection status. */ +#define PROT_OVER_CURR_LIMIT1_TIME_SEC (30.0f) /**< 20% overload can last maximum time: 30 sec. */ +#define PROT_OVER_CURR_LIMIT2_TIME_SEC (10.0f) /**< 20% overload can last maximum time: 10 sec. */ +#define PROT_OVER_CURR_LIMIT3_TIME_SEC (2.0f) /**< 20% overload can last maximum time: 2 sec. */ + +/**< Over voltage protection */ +#define PROT_OVER_VOLT_BRK_ON1 (26.0f) /**<处于1级时,直流链路电压触发值(V)过高*/ +#define PROT_OVER_VOLT_BRK_ON2 (27.0f) /**< Over dc-link voltage trigger value (V) when in level 2. */ +#define PROT_OVER_VOLT_BRK_ON3 (28.0f) /**< Over dc-link voltage trigger value (V) when in level 3. */ +#define PROT_OVER_VOLT_BRK_ALL (30.0f) /**< Over dc-link voltage trigger value (V) when in level 4. */ +#define PROT_OVER_VOLT_RECY_DELTA (0.5f) /**<从保护状态恢复时的电压间隙(V)*/ +#define PROT_OVER_VOLT_LIMIT1_TIME_SEC (5.0f) /** 1; One-cycle mode: < 0.5 */ +#define POS_SNSR_IPD_INJ_PERIOD (4) + +#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/mcs_curr_prot.c b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/mcs_curr_prot.c new file mode 100644 index 00000000..da4e56f8 --- /dev/null +++ b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/mcs_curr_prot.c @@ -0,0 +1,267 @@ +/** + * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_curr_prot.c + * @author MCU Algorithm Team + * @brief This file contains current protecion api definition. + */ + +/* Includes ------------------------------------------------------------------------------------ */ +#include "mcs_curr_prot.h" +#include "mcs_math.h" +#include "mcs_prot_user_config.h" +#include "mcs_assert.h" + +/** + * @brief Initilization over current protection function. + * @param ocp Over current protection handle. + * @param ts Ctrl period (s). + * @retval None. + */ +void OCP_Init(OCP_Handle *ocp, float ts) +{ + MCS_ASSERT_PARAM(ocp != NULL); + MCS_ASSERT_PARAM(ts > 0.0f); + ocp->ts = ts; + OCP_Clear(ocp); + + ocp->protCntLimit = PROT_CNT_LIMIT; + ocp->recyCntLimit = RECY_CNT_LIMIT; + /* Configuring four levels of current protection thresholds. */ + ocp->protValThr[PROT_VAL_THRESHOLD_0] = PROT_OVER_CURR_POW_DN1; + ocp->protValThr[PROT_VAL_THRESHOLD_1] = PROT_OVER_CURR_POW_DN2; + ocp->protValThr[PROT_VAL_THRESHOLD_2] = PROT_OVER_CURR_POW_DN3; + ocp->protValThr[PROT_VAL_THRESHOLD_3] = PROT_OVER_CURR_POW_OFF; + /* Configure the protection limit time. */ + ocp->protLimitTime[PROT_LIMIT_TIME_0] = PROT_OVER_CURR_LIMIT1_TIME_SEC; + ocp->protLimitTime[PROT_LIMIT_TIME_1] = PROT_OVER_CURR_LIMIT2_TIME_SEC; + ocp->protLimitTime[PROT_LIMIT_TIME_2] = PROT_OVER_CURR_LIMIT3_TIME_SEC; + ocp->recyDelta = PROT_OVER_CURR_RECY_DELTA; +} + +/** + * @brief Over current protection detection. + * @param ocp Over current protection handle. + * @param motorErrStatus Motor error status. + * @param idq DQ-axis feedback currents. + * @retval None. + */ +void OCP_Det(OCP_Handle *ocp, MotorErrStatusReg *motorErrStatus, DqAxis idq) +{ + MCS_ASSERT_PARAM(ocp != NULL); + MCS_ASSERT_PARAM(motorErrStatus != NULL); + /* Calculate current amplitude. */ + ocp->currAmp = Sqrt(idq.d * idq.d + idq.q * idq.q); + + /* Check if value goes over threshold for continuous cycles. */ + if (ocp->currAmp < ocp->protValThr[PROT_VAL_THRESHOLD_0]) { + ocp->protCnt = 0; + return; + } + + if (ocp->protCnt < ocp->protCntLimit) { + ocp->protCnt++; + return; + } + + /* Check which protection level should be activated. */ + /* Once enter error status, error status can only be changed by recover api. */ + /* protect level: level 4. */ + if (ocp->currAmp >= ocp->protValThr[PROT_VAL_THRESHOLD_3] && ocp->protLevel < PROT_LEVEL_4) { + ocp->protLevel = PROT_LEVEL_4; + motorErrStatus->Bit.overCurrErr = 1; + ocp->protCnt = 0; + return; + } + + /* Protect level: level 3. */ + if (ocp->currAmp >= ocp->protValThr[PROT_VAL_THRESHOLD_2] && ocp->protLevel < PROT_LEVEL_3) { + ocp->protLevel = PROT_LEVEL_3; + motorErrStatus->Bit.overCurrErr = 1; + ocp->protCnt = 0; + ocp->timer = 0.0f; + return; + } + + /* Protect level: level 2. */ + if (ocp->currAmp >= ocp->protValThr[PROT_VAL_THRESHOLD_1] && ocp->protLevel < PROT_LEVEL_2) { + ocp->protLevel = PROT_LEVEL_2; + motorErrStatus->Bit.overCurrErr = 1; + ocp->protCnt = 0; + ocp->timer = 0.0f; + return; + } + + /* Protect level: level 1. */ + if (ocp->currAmp >= ocp->protValThr[PROT_VAL_THRESHOLD_0] && ocp->protLevel < PROT_LEVEL_1) { + ocp->protLevel = PROT_LEVEL_1; + motorErrStatus->Bit.overCurrErr = 1; + ocp->protCnt = 0; + ocp->timer = 0.0f; + return; + } +} + +/** + * @brief Over current protection execution. + * @param ocp Over current protection handle. + * @param idqRef DQ-axis current references. + * @param aptAddr Three-phase APT address pointer. + * @retval None. + */ +void OCP_Exec(OCP_Handle *ocp, DqAxis *idqRef, APT_RegStruct **aptAddr) +{ + MCS_ASSERT_PARAM(ocp != NULL); + MCS_ASSERT_PARAM(idqRef != NULL); + MCS_ASSERT_PARAM(aptAddr != NULL); + + float id = idqRef->d; + float iq = idqRef->q; + float idqAmp = ocp->currAmp; + /* According to protect level, take corresponding action. */ + switch (ocp->protLevel) { + /* level 4: disable all PWM output. */ + case PROT_LEVEL_4: + /* Disable three-phase pwm output. */ + ProtSpo_Exec(aptAddr); + idqRef->d = 0.0f; + idqRef->q = 0.0f; + break; + + /* level 3: derate speed reference. */ + case PROT_LEVEL_3: + ocp->timer += ocp->ts; + if (ocp->timer > ocp->protLimitTime[PROT_LIMIT_TIME_2]) { + idqRef->d = id / idqAmp * PROT_MOTOR_RATED_CURR; + idqRef->q = iq / idqAmp * PROT_MOTOR_RATED_CURR; + } + break; + + /* level 2: derate speed reference. */ + case PROT_LEVEL_2: + ocp->timer += ocp->ts; + if (ocp->timer > ocp->protLimitTime[PROT_LIMIT_TIME_1]) { + idqRef->d = id / idqAmp * PROT_MOTOR_RATED_CURR; + idqRef->q = iq / idqAmp * PROT_MOTOR_RATED_CURR; + } + break; + + /* level 1: derate speed reference. */ + case PROT_LEVEL_1: + ocp->timer += ocp->ts; + if (ocp->timer > ocp->protLimitTime[PROT_LIMIT_TIME_0]) { + idqRef->d = id / idqAmp * PROT_MOTOR_RATED_CURR; + idqRef->q = iq / idqAmp * PROT_MOTOR_RATED_CURR; + } + break; + + /* level 0: take no protection action. */ + case PROT_LEVEL_0: + break; + + default: + break; + } +} + +/** + * @brief Over current protection recovery. + * @param ocp Over current protection handle. + * @param motorErrStatus Motor error status. + * @retval None. + */ +void OCP_Recy(OCP_Handle *ocp, MotorErrStatusReg *motorErrStatus) +{ + MCS_ASSERT_PARAM(ocp != NULL); + /* If not under error state, just return without any operation. */ + if (!motorErrStatus->Bit.overCurrErr) { + return; + } + + /* Calculate current amplitude. */ + float currAmp = ocp->currAmp; + + /* According to protection level, take corresponding recovery action. */ + switch (ocp->protLevel) { + /* level 4 */ + case PROT_LEVEL_4: + if (currAmp < ocp->protValThr[PROT_VAL_THRESHOLD_3] - ocp->recyDelta) { + ocp->recyCnt++; + if (ocp->recyCnt > ocp->recyCntLimit) { + ocp->protLevel = PROT_LEVEL_3; + ocp->recyCnt = 0; + } + } + break; + + /* level 3 */ + case PROT_LEVEL_3: + if (currAmp < ocp->protValThr[PROT_VAL_THRESHOLD_2] - ocp->recyDelta) { + ocp->recyCnt++; + if (ocp->recyCnt > ocp->recyCntLimit) { + ocp->protLevel = PROT_LEVEL_2; + ocp->recyCnt = 0; + } + } + break; + + /* level 2 */ + case PROT_LEVEL_2: + if (currAmp < ocp->protValThr[PROT_VAL_THRESHOLD_1] - ocp->recyDelta) { + ocp->recyCnt++; + if (ocp->recyCnt > ocp->recyCntLimit) { + ocp->protLevel = PROT_LEVEL_1; + ocp->recyCnt = 0; + } + } + break; + + /* level 1 */ + case PROT_LEVEL_1: + if (currAmp < ocp->protValThr[PROT_VAL_THRESHOLD_0] - ocp->recyDelta) { + ocp->recyCnt++; + if (ocp->recyCnt > ocp->recyCntLimit) { + ocp->protLevel = PROT_LEVEL_0; + ocp->recyCnt = 0; + } + } + break; + + /* level 0 */ + case PROT_LEVEL_0: + motorErrStatus->Bit.overCurrErr = 0; + break; + + default: + break; + } +} + +/** + * @brief Over current protection error status clear. + * @param ocp Over current protection handle. + * @retval None. + */ +void OCP_Clear(OCP_Handle *ocp) +{ + MCS_ASSERT_PARAM(ocp != NULL); + /* Clear the history value. */ + ocp->protCnt = 0; + ocp->protLevel = PROT_LEVEL_0; + ocp->recyCnt = 0; + ocp->timer = 0.0f; +} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/mcs_curr_prot.h b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/mcs_curr_prot.h new file mode 100644 index 00000000..e9916fe4 --- /dev/null +++ b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/mcs_curr_prot.h @@ -0,0 +1,52 @@ +/** + * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_curr_prot.h + * @author MCU Algorithm Team + * @brief This file contains current protection data struct and api declaration. + */ + +/* Define to prevent recursive inclusion ------------------------------------- */ +#ifndef McuMagicTag_MCS_CURR_PROT_H +#define McuMagicTag_MCS_CURR_PROT_H + +/* Includes ------------------------------------------------------------------------------------ */ +#include "mcs_prot_cmm.h" +#include "mcs_typedef.h" +#include "apt_ip.h" + +typedef struct { + unsigned int protCnt; + unsigned int recyCnt; + unsigned int protCntLimit; + unsigned int recyCntLimit; + float currAmp; + float protValThr[PROT_VAL_THRESHOLD_NUMS]; /* from low current level to high. */ + float protLimitTime[PROT_LIMIT_TIME_NUMS]; /* from level 3 to level 1. */ + float recyDelta; + float timer; + float ts; + PROT_Level protLevel; +} OCP_Handle; + +void OCP_Init(OCP_Handle *ocp, float ts); +void OCP_Det(OCP_Handle *ocp, MotorErrStatusReg *motorErrStatus, DqAxis idq); +void OCP_Exec(OCP_Handle *ocp, DqAxis *idqRef, APT_RegStruct **aptAddr); +void OCP_Recy(OCP_Handle *ocp, MotorErrStatusReg *motorErrStatus); +void OCP_Clear(OCP_Handle *ocp); + +#endif diff --git a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/mcs_dc_volt_prot.c b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/mcs_dc_volt_prot.c new file mode 100644 index 00000000..b0541a2e --- /dev/null +++ b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/mcs_dc_volt_prot.c @@ -0,0 +1,495 @@ +/** + * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_dc_volt_prot.c + * @author MCU Algorithm Team + * @brief This file contains dc-link voltage protection api declaration. + */ + +/* Includes ------------------------------------------------------------------------------------ */ +#include "mcs_dc_volt_prot.h" +#include "mcs_prot_user_config.h" +#include "mcs_assert.h" + +/** + * @brief Initilization over dc-link voltage protection function. + * @param ovp Over dc-link voltage protection handle. + * @param ts Ctrl period (s). + * @retval None. + */ +void OVP_Init(OVP_Handle *ovp, float ts) +{ + MCS_ASSERT_PARAM(ovp != NULL); + MCS_ASSERT_PARAM(ts > 0.0f); + ovp->ts = ts; + OVP_Clear(ovp); + ovp->protCntLimit = PROT_CNT_LIMIT; + ovp->recyCntLimit = OVER_VOLT_RECY_CNT_LIMIT; + /* Configuring four levels of overvoltage protection thresholds. */ + ovp->protValThr[PROT_VAL_THRESHOLD_0] = PROT_OVER_VOLT_BRK_ON1; + ovp->protValThr[PROT_VAL_THRESHOLD_1] = PROT_OVER_VOLT_BRK_ON2; + ovp->protValThr[PROT_VAL_THRESHOLD_2] = PROT_OVER_VOLT_BRK_ON3; + ovp->protValThr[PROT_VAL_THRESHOLD_3] = PROT_OVER_VOLT_BRK_ALL; + /* Configure the protection limit time. */ + ovp->protLimitTime[PROT_LIMIT_TIME_0] = PROT_OVER_VOLT_LIMIT1_TIME_SEC; + ovp->protLimitTime[PROT_LIMIT_TIME_1] = PROT_OVER_VOLT_LIMIT2_TIME_SEC; + ovp->protLimitTime[PROT_LIMIT_TIME_2] = PROT_OVER_VOLT_LIMIT3_TIME_SEC; + ovp->recyDelta = PROT_OVER_VOLT_RECY_DELTA; +} + +/** + * @brief Initilization lower dc-link voltage protection function. + * @param lvp Lower dc-link voltage protection handle. + * @param ts Ctrl period (s). + * @retval None. + */ +void LVP_Init(LVP_Handle *lvp, float ts) +{ + MCS_ASSERT_PARAM(lvp != NULL); + MCS_ASSERT_PARAM(ts > 0.0f); + + lvp->ts = ts; + LVP_Clear(lvp); + + lvp->protCntLimit = PROT_CNT_LIMIT; + lvp->recyCntLimit = LOWER_VOLT_RECY_CNT_LIMIT; + /* Configuring four levels of lower voltage protection thresholds. */ + lvp->protValThr[PROT_VAL_THRESHOLD_0] = PROT_LOWER_VOLT_POW_DN1; + lvp->protValThr[PROT_VAL_THRESHOLD_1] = PROT_LOWER_VOLT_POW_DN2; + lvp->protValThr[PROT_VAL_THRESHOLD_2] = PROT_LOWER_VOLT_POW_DN3; + lvp->protValThr[PROT_VAL_THRESHOLD_3] = PROT_LOWER_VOLT_POW_OFF; + /* Configure the protection limit time. */ + lvp->protLimitTime[PROT_LIMIT_TIME_0] = PROT_LOWER_VOLT_LIMIT1_TIME_SEC; + lvp->protLimitTime[PROT_LIMIT_TIME_1] = PROT_LOWER_VOLT_LIMIT2_TIME_SEC; + lvp->protLimitTime[PROT_LIMIT_TIME_2] = PROT_LOWER_VOLT_LIMIT3_TIME_SEC; + lvp->recyDelta = PROT_OVER_VOLT_RECY_DELTA; +} + +/** + * @brief Over dc-link voltage protection detection. + * @param ovp Over dc-link voltage protection handle. + * @param motorErrStatus Motor error status. + * @param udc DC-link voltage feedback (V). + * @retval None. + */ +void OVP_Det(OVP_Handle *ovp, MotorErrStatusReg *motorErrStatus, float udc) +{ + MCS_ASSERT_PARAM(ovp != NULL); + MCS_ASSERT_PARAM(motorErrStatus != NULL); + MCS_ASSERT_PARAM(udc > 0.0f); + /* Check if value goes over threshold for continuous cycles. */ + if (udc < ovp->protValThr[PROT_VAL_THRESHOLD_0]) { + ovp->protCnt = 0; + return; + } + + if (ovp->protCnt < ovp->protCntLimit) { + ovp->protCnt++; + return; + } + + /* Check which protection level should be activated. */ + /* Once enter error status, error status can only be changed by recover api. */ + /* protect level: level 4. */ + if (udc >= ovp->protValThr[PROT_VAL_THRESHOLD_3] && ovp->protLevel < PROT_LEVEL_4) { + ovp->protLevel = PROT_LEVEL_4; + motorErrStatus->Bit.overVoltErr = 1; + ovp->protCnt = 0; + return; + } + + /* Protect level: level 3. */ + if (udc >= ovp->protValThr[PROT_VAL_THRESHOLD_2] && ovp->protLevel < PROT_LEVEL_3) { + ovp->protLevel = PROT_LEVEL_3; + motorErrStatus->Bit.overVoltErr = 1; + ovp->protCnt = 0; + ovp->timer = 0.0f; + return; + } + + /* Protect level: level 2. */ + if (udc >= ovp->protValThr[PROT_VAL_THRESHOLD_1] && ovp->protLevel < PROT_LEVEL_2) { + ovp->protLevel = PROT_LEVEL_2; + motorErrStatus->Bit.overVoltErr = 1; + ovp->protCnt = 0; + ovp->timer = 0.0f; + return; + } + + /* Protect level: level 1. */ + if (udc >= ovp->protValThr[PROT_VAL_THRESHOLD_0] && ovp->protLevel < PROT_LEVEL_1) { + ovp->protLevel = PROT_LEVEL_1; + motorErrStatus->Bit.overVoltErr = 1; + ovp->protCnt = 0; + ovp->timer = 0.0f; + return; + } +} + +/** + * @brief Lower dc-link voltage protection detection. + * @param lvp Lower dc-link voltage protection handle. + * @param motorErrStatus Motor error status. + * @param udc DC-link voltage feedback (V). + * @retval None. + */ +void LVP_Det(LVP_Handle *lvp, MotorErrStatusReg *motorErrStatus, float udc) +{ + MCS_ASSERT_PARAM(lvp != NULL); + MCS_ASSERT_PARAM(motorErrStatus != NULL); + MCS_ASSERT_PARAM(udc > 0.0f); + /* Check if value goes over threshold for continuous cycles. */ + if (udc > lvp->protValThr[PROT_VAL_THRESHOLD_0]) { + lvp->protCnt = 0; + return; + } + + if (lvp->protCnt < lvp->protCntLimit) { + lvp->protCnt++; + return; + } + + /* Check which protection level should be activated. */ + /* Once enter error status, error status can only be changed by recover api. */ + /* protect level: level 4. */ + if (udc <= lvp->protValThr[PROT_VAL_THRESHOLD_3] && lvp->protLevel < PROT_LEVEL_4) { + lvp->protLevel = PROT_LEVEL_4; + motorErrStatus->Bit.lowerVoltErr = 1; + lvp->protCnt = 0; + return; + } + + /* Protect level: level 3. */ + if (udc <= lvp->protValThr[PROT_VAL_THRESHOLD_2] && lvp->protLevel < PROT_LEVEL_3) { + lvp->protLevel = PROT_LEVEL_3; + motorErrStatus->Bit.lowerVoltErr = 1; + lvp->protCnt = 0; + lvp->timer = 0.0f; + return; + } + + /* Protect level: level 2. */ + if (udc <= lvp->protValThr[PROT_VAL_THRESHOLD_1] && lvp->protLevel < PROT_LEVEL_2) { + lvp->protLevel = PROT_LEVEL_2; + motorErrStatus->Bit.lowerVoltErr = 1; + lvp->protCnt = 0; + lvp->timer = 0.0f; + return; + } + + /* Protect level: level 1. */ + if (udc <= lvp->protValThr[PROT_VAL_THRESHOLD_0] && lvp->protLevel < PROT_LEVEL_1) { + lvp->protLevel = PROT_LEVEL_1; + motorErrStatus->Bit.lowerVoltErr = 1; + lvp->protCnt = 0; + lvp->timer = 0.0f; + return; + } +} + +/** + * @brief Over dc-link voltage protection execution. + * @param ovp Over dc-link voltage protection handle. + * @param duty Brake loop output duty (0-1). + * @param aptAddr Three-phase APT address pointer. + * @retval None. + */ +void OVP_Exec(OVP_Handle *ovp, float *duty, APT_RegStruct **aptAddr) +{ + MCS_ASSERT_PARAM(ovp != NULL); + MCS_ASSERT_PARAM(duty != NULL); + MCS_ASSERT_PARAM(aptAddr != NULL); + /* According to protect level, take corresponding action. */ + switch (ovp->protLevel) { + /* level 4: brake loop duty maximum. */ + case PROT_LEVEL_4: + *duty = PROT_OVER_VOLT_BRK_DUTY4; + /* Disable three-phase pwm output. */ + ProtSpo_Exec(aptAddr); + break; + + /* level 3: brake loop duty level 3. */ + case PROT_LEVEL_3: + *duty = PROT_OVER_VOLT_BRK_DUTY2; + ovp->timer += ovp->ts; + if (ovp->timer > ovp->protLimitTime[PROT_LIMIT_TIME_2]) { + *duty = PROT_OVER_VOLT_BRK_DUTY3; + } + break; + + /* level 2: brake loop duty level 2. */ + case PROT_LEVEL_2: + *duty = PROT_OVER_VOLT_BRK_DUTY1; + ovp->timer += ovp->ts; + if (ovp->timer > ovp->protLimitTime[PROT_LIMIT_TIME_1]) { + *duty = PROT_OVER_VOLT_BRK_DUTY2; + } + break; + + /* level 1: brake loop duty level 1. */ + case PROT_LEVEL_1: + ovp->timer += ovp->ts; + if (ovp->timer > ovp->protLimitTime[PROT_LIMIT_TIME_0]) { + *duty = PROT_OVER_VOLT_BRK_DUTY1; + } + break; + + /* level 0: take no protection action. */ + case PROT_LEVEL_0: + break; + + default: + break; + } + return; +} + +/** + * @brief Lower dc-link voltage protection execution. + * @param lvp Lower dc-link voltage protection handle. + * @param spdRef Speed Reference (Hz). + * @param aptAddr Three-phase APT address pointer. + * @retval None. + */ +void LVP_Exec(LVP_Handle *lvp, float *spdRef, APT_RegStruct **aptAddr) +{ + MCS_ASSERT_PARAM(lvp != NULL); + MCS_ASSERT_PARAM(spdRef != NULL); + MCS_ASSERT_PARAM(aptAddr != NULL); + /* According to protect level, take corresponding action. */ + switch (lvp->protLevel) { + /* level 4: disable all PWM output. */ + case PROT_LEVEL_4: + /* Disable three-phase pwm output. */ + ProtSpo_Exec(aptAddr); + *spdRef *= 0.0f; + break; + + /* level 3: derate speed reference. */ + case PROT_LEVEL_3: + *spdRef *= PROT_POW_DN2_PCT; + lvp->timer += lvp->ts; + if (lvp->timer > lvp->protLimitTime[PROT_LIMIT_TIME_2]) { + *spdRef *= PROT_POW_DN3_PCT; + } + break; + + /* level 2: derate speed reference. */ + case PROT_LEVEL_2: + *spdRef *= PROT_POW_DN1_PCT; + lvp->timer += lvp->ts; + if (lvp->timer > lvp->protLimitTime[PROT_LIMIT_TIME_1]) { + *spdRef *= PROT_POW_DN2_PCT; + } + break; + + /* level 1: derate speed reference. */ + case PROT_LEVEL_1: + lvp->timer += lvp->ts; + if (lvp->timer > lvp->protLimitTime[PROT_LIMIT_TIME_0]) { + *spdRef *= PROT_POW_DN1_PCT; + } + break; + + /* level 0: take no protection action. */ + case PROT_LEVEL_0: + break; + + default: + break; + } + return; +} + +/** + * @brief Over dc-link voltage protection recovery. + * @param ovp Over dc-link voltage protection handle. + * @param motorErrStatus Motor error status. + * @param udc DC-link voltage (V). + * @retval None. + */ +void OVP_Recy(OVP_Handle *ovp, MotorErrStatusReg *motorErrStatus, float udc) +{ + MCS_ASSERT_PARAM(ovp != NULL); + /* If not under error state, just return without any operation. */ + if (!motorErrStatus->Bit.overVoltErr) { + return; + } + + /* According to protection level, take corresponding recovery action. */ + switch (ovp->protLevel) { + /* level 4 */ + case PROT_LEVEL_4: + if (udc < ovp->protValThr[PROT_VAL_THRESHOLD_3] - ovp->recyDelta) { + ovp->recyCnt++; + if (ovp->recyCnt > ovp->recyCntLimit) { + ovp->protLevel = PROT_LEVEL_3; + ovp->recyCnt = 0; + } + } + break; + + /* level 3 */ + case PROT_LEVEL_3: + /* If the dc-link voltage is less than threshold 2, level-2 protection is restored. */ + if (udc < ovp->protValThr[PROT_VAL_THRESHOLD_2] - ovp->recyDelta) { + ovp->recyCnt++; + if (ovp->recyCnt > ovp->recyCntLimit) { + ovp->protLevel = PROT_LEVEL_2; + ovp->recyCnt = 0; + } + } + break; + + /* level 2 */ + case PROT_LEVEL_2: + /* If the dc-link voltage is less than threshold 1, level-1 protection is restored. */ + if (udc < ovp->protValThr[PROT_VAL_THRESHOLD_1] - ovp->recyDelta) { + ovp->recyCnt++; + if (ovp->recyCnt > ovp->recyCntLimit) { + ovp->protLevel = PROT_LEVEL_1; + ovp->recyCnt = 0; + } + } + break; + + /* level 1 */ + case PROT_LEVEL_1: + /* If the dc-link voltage is less than threshold 0, level-0 protection is restored. */ + if (udc < ovp->protValThr[PROT_VAL_THRESHOLD_0] - ovp->recyDelta) { + ovp->recyCnt++; + if (ovp->recyCnt > ovp->recyCntLimit) { + ovp->protLevel = PROT_LEVEL_0; + ovp->recyCnt = 0; + } + } + break; + /* level 0 Fault-free state. */ + case PROT_LEVEL_0: + motorErrStatus->Bit.overVoltErr = 0; + break; + + default: + break; + } + return; +} + +/** + * @brief Lower dc-link voltage protection recovery. + * @param lvp Lower dc-link voltage protection handle. + * @param motorErrStatus Motor error status. + * @param udc DC-link voltage (V). + * @retval None. + */ +void LVP_Recy(LVP_Handle *lvp, MotorErrStatusReg *motorErrStatus, float udc) +{ + MCS_ASSERT_PARAM(lvp != NULL); + /* If not under error state, just return without any operation. */ + if (!motorErrStatus->Bit.lowerVoltErr) { + return; + } + + /* According to protection level, take corresponding recovery action. */ + switch (lvp->protLevel) { + /* level 4 */ + case PROT_LEVEL_4: + /* If the dc-link voltage is greater than threshold 3, level-3 protection is restored. */ + if (udc > lvp->protValThr[PROT_VAL_THRESHOLD_3] + lvp->recyDelta) { + lvp->recyCnt++; + if (lvp->recyCnt > lvp->recyCntLimit) { + lvp->protLevel = PROT_LEVEL_3; + lvp->recyCnt = 0; + } + } + break; + + /* level 3 */ + case PROT_LEVEL_3: + /* If the dc-link voltage is greater than threshold 2, level-2 protection is restored. */ + if (udc > lvp->protValThr[PROT_VAL_THRESHOLD_2] + lvp->recyDelta) { + lvp->recyCnt++; + if (lvp->recyCnt > lvp->recyCntLimit) { + lvp->protLevel = PROT_LEVEL_2; + lvp->recyCnt = 0; + } + } + break; + + /* level 2 */ + case PROT_LEVEL_2: + /* If the dc-link voltage is greater than threshold 1, level-1 protection is restored. */ + if (udc > lvp->protValThr[PROT_VAL_THRESHOLD_1] + lvp->recyDelta) { + lvp->recyCnt++; + if (lvp->recyCnt > lvp->recyCntLimit) { + lvp->protLevel = PROT_LEVEL_1; + lvp->recyCnt = 0; + } + } + break; + + /* level 1 */ + case PROT_LEVEL_1: + /* If the dc-link voltage is greater than threshold 0, level-0 protection is restored. */ + if (udc > lvp->protValThr[PROT_VAL_THRESHOLD_0] + lvp->recyDelta) { + lvp->recyCnt++; + if (lvp->recyCnt > lvp->recyCntLimit) { + lvp->protLevel = PROT_LEVEL_0; + lvp->recyCnt = 0; + } + } + break; + /* level 0 Fault-free state. */ + case PROT_LEVEL_0: + motorErrStatus->Bit.lowerVoltErr = 0; + break; + + default: + break; + } + return; +} + +/** + * @brief Over dc-link voltage protection error status clear. + * @param ovp Over voltage protection handle. + * @retval None. + */ +void OVP_Clear(OVP_Handle *ovp) +{ + MCS_ASSERT_PARAM(ovp != NULL); + /* Clear the history value. */ + ovp->protCnt = 0; + ovp->protLevel = PROT_LEVEL_0; + ovp->recyCnt = 0; + ovp->timer = 0.0f; +} + +/** + * @brief Lower dc-link voltage protection error status clear. + * @param lvp Lower voltage protection handle. + * @retval None. + */ +void LVP_Clear(LVP_Handle *lvp) +{ + MCS_ASSERT_PARAM(lvp != NULL); + /* Clear the history value. */ + lvp->protCnt = 0; + lvp->protLevel = PROT_LEVEL_0; + lvp->recyCnt = 0; + lvp->timer = 0.0f; +} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/mcs_dc_volt_prot.h b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/mcs_dc_volt_prot.h new file mode 100644 index 00000000..0435156a --- /dev/null +++ b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/mcs_dc_volt_prot.h @@ -0,0 +1,69 @@ +/** + * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_dc_volt_prot.h + * @author MCU Algorithm Team + * @brief This file contains dc-link voltage protection data struct and api declaration. + */ + +/* Define to prevent recursive inclusion ------------------------------------------------------- */ +#ifndef McuMagicTag_MCS_DC_VOLT_PROT_H +#define McuMagicTag_MCS_DC_VOLT_PROT_H + +/* Includes ------------------------------------------------------------------------------------ */ +#include "mcs_prot_cmm.h" +#include "apt_ip.h" + +typedef struct { + unsigned int protCnt; + unsigned int recyCnt; + unsigned int protCntLimit; + unsigned int recyCntLimit; + float protValThr[PROT_VAL_THRESHOLD_NUMS]; /* from low voltage level to high. */ + float protLimitTime[PROT_LIMIT_TIME_NUMS]; /* from level 3 to level 1. */ + float recyDelta; + float timer; + float ts; + PROT_Level protLevel; +} OVP_Handle; + +typedef struct { + unsigned int protCnt; + unsigned int recyCnt; + unsigned int protCntLimit; + unsigned int recyCntLimit; + float protValThr[PROT_VAL_THRESHOLD_NUMS]; /* from high voltage level to low. */ + float protLimitTime[PROT_LIMIT_TIME_NUMS]; /* from level 3 to level 1. */ + float recyDelta; + float timer; + float ts; + PROT_Level protLevel; +} LVP_Handle; + +void OVP_Init(OVP_Handle *ovp, float ts); +void OVP_Det(OVP_Handle *ovp, MotorErrStatusReg *motorErrStatus, float udc); +void OVP_Exec(OVP_Handle *ovp, float *duty, APT_RegStruct **aptAddr); +void OVP_Recy(OVP_Handle *ovp, MotorErrStatusReg *motorErrStatus, float udc); +void OVP_Clear(OVP_Handle *ovp); + +void LVP_Init(LVP_Handle *lvp, float ts); +void LVP_Det(LVP_Handle *lvp, MotorErrStatusReg *motorErrStatus, float udc); +void LVP_Exec(LVP_Handle *lvp, float *spdRef, APT_RegStruct **aptAddr); +void LVP_Recy(LVP_Handle *lvp, MotorErrStatusReg *motorErrStatus, float udc); +void LVP_Clear(LVP_Handle *lvp); + +#endif diff --git a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/mcs_motor_stalling.c b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/mcs_motor_stalling.c new file mode 100644 index 00000000..9dee4d2d --- /dev/null +++ b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/mcs_motor_stalling.c @@ -0,0 +1,112 @@ +/** + * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_motor_stalling.c + * @author MCU Algorithm Team + * @brief This file contains motor stalling protection data struct and api definition. + */ + +/* Includes ------------------------------------------------------------------------------------ */ +#include "mcs_motor_stalling.h" +#include "mcs_prot_user_config.h" +#include "mcs_math.h" +#include "mcs_math_const.h" +#include "mcs_assert.h" + +/** + * @brief Initilization motor stalling protection function. + * @param stall Motor stalling handle. + * @param ts Ctrl period (s). + * @param currLimit The current amplitude that triggers fault. (A). + * @param spdLimit The speed amplitude that triggers fault. (Hz). + * @param timeLimit The threshold time that current amplitude over the limit (s). + * @retval None. + */ +void STP_Init(STP_Handle *stall, float ts, float currLimit, float spdLimit, float timeLimit) +{ + MCS_ASSERT_PARAM(stall != NULL); + MCS_ASSERT_PARAM(ts > 0.0f); + MCS_ASSERT_PARAM(currLimit > 0.0f); + MCS_ASSERT_PARAM(spdLimit > 0.0f); + MCS_ASSERT_PARAM(timeLimit > 0.0f); + /* Configuring parameters for stalling detection. */ + stall->ts = ts; + /* Current threshold and speed threshold for stalling fault. */ + stall->currAmpLimit = currLimit; + stall->spdLimit = spdLimit; + stall->timeLimit = timeLimit; + stall->timer = 0.0f; +} + +/** + * @brief Motor stalling detection. + * @param stall Motor stalling handle. + * @param motorErrStatus Motor error status. + * @param spd Speed feedback (Hz). + * @param idq Dq-axis current feedback (A). + * @retval None. + */ +void STP_Det_ByCurrSpd(STP_Handle *stall, MotorErrStatusReg *motorErrStatus, float spd, DqAxis idq) +{ + MCS_ASSERT_PARAM(stall != NULL); + MCS_ASSERT_PARAM(motorErrStatus != NULL); + MCS_ASSERT_PARAM(Abs(spd) >= 0.0f); + /* Calculate current amplitude. */ + float currAmp = Sqrt(idq.d * idq.d + idq.q * idq.q); + float spdAbs = Abs(spd); + /* Check if value goes over threshold for continuous cycles. */ + if (currAmp < stall->currAmpLimit || spdAbs > stall->spdLimit) { + stall->timer = 0.0f; + return; + } + /* Time accumulation. */ + if (stall->timer < stall->timeLimit) { + stall->timer += stall->ts; + return; + } + motorErrStatus->Bit.motorStalling = 1; +} + +/** + * @brief Motor stalling protection execution. + * @param motorErrStatus Motor error status. + * @param aptAddr Three-phase APT address pointer. + * @retval None. + */ +void STP_Exec(MotorErrStatusReg *motorErrStatus, APT_RegStruct **aptAddr) +{ + MCS_ASSERT_PARAM(motorErrStatus != NULL); + MCS_ASSERT_PARAM(aptAddr != NULL); + if (motorErrStatus->Bit.motorStalling == 0) { + return; + } + + /* Disable three-phase pwm output. */ + ProtSpo_Exec(aptAddr); + return; +} + +/** + * @brief Motor stalling protection error status clear. + * @param stall Motor stalling handle. + * @retval None. + */ +void STP_Clear(STP_Handle *stall) +{ + MCS_ASSERT_PARAM(stall != NULL); + stall->timer = 0.0f; +} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/mcs_motor_stalling.h b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/mcs_motor_stalling.h new file mode 100644 index 00000000..a1d1e4be --- /dev/null +++ b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/mcs_motor_stalling.h @@ -0,0 +1,45 @@ +/** + * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_motor_stalling.h + * @author MCU Algorithm Team + * @brief This file contains motor stalling protection data struct and api declaration. + */ + +/* Define to prevent recursive inclusion ------------------------------------- */ +#ifndef McuMagicTag_MCS_MOTOR_STALLING_PROT_H +#define McuMagicTag_MCS_MOTOR_STALLING_PROT_H + +/* Includes ------------------------------------------------------------------------------------ */ +#include "mcs_prot_cmm.h" +#include "apt_ip.h" +#include "mcs_typedef.h" + +typedef struct { + float currAmpLimit; /**< Feedback current higher than this value triggers fault. (A). */ + float spdLimit; /**< Feedback speed lower than this value triggers fault (Hz). */ + float timeLimit; /**< The threshold time that current and speed feedback over ranges (s). */ + float timer; /**< Timer to get speed and current over range time. */ + float ts; /**< Ctrl period (s). */ +} STP_Handle; + +void STP_Init(STP_Handle *stall, float ts, float currLimit, float spdLimit, float timeLimit); +void STP_Det_ByCurrSpd(STP_Handle *stall, MotorErrStatusReg *motorErrStatus, float spd, DqAxis idq); +void STP_Exec(MotorErrStatusReg *motorErrStatus, APT_RegStruct **aptAddr); +void STP_Clear(STP_Handle *stall); + +#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/mcs_temp_prot.c b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/mcs_temp_prot.c new file mode 100644 index 00000000..184ba4a3 --- /dev/null +++ b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/mcs_temp_prot.c @@ -0,0 +1,262 @@ +/** + * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_temp_prot.c + * @author MCU Algorithm Team + * @brief This file contains over temperature protection api definition. + */ + +/* Includes ------------------------------------------------------------------------------------ */ +#include "mcs_temp_prot.h" +#include "mcs_prot_user_config.h" +#include "mcs_assert.h" + +/** + * @brief Initilization over temperation protection function. + * @param otp Over temperature protection handle. + * @param ts Ctrl period. + * @retval None. + */ +void OTP_Init(OTP_Handle *otp, float ts) +{ + MCS_ASSERT_PARAM(otp != NULL); + MCS_ASSERT_PARAM(ts > 0.0f); + otp->ts = ts; + otp->protCntLimit = PROT_CNT_LIMIT; + otp->recyCntLimit = RECY_CNT_LIMIT; + /* Configuring the temperature protection threshold. */ + otp->protValThr[PROT_VAL_THRESHOLD_0] = PROT_OVER_IPM_TEMP_POW_DN1; + otp->protValThr[PROT_VAL_THRESHOLD_1] = PROT_OVER_IPM_TEMP_POW_DN2; + otp->protValThr[PROT_VAL_THRESHOLD_2] = PROT_OVER_IPM_TEMP_POW_DN3; + otp->protValThr[PROT_VAL_THRESHOLD_3] = PROT_OVER_IPM_TEMP_POW_OFF; + /* Configuring the protection limiting time. */ + otp->protLimitTime[PROT_LIMIT_TIME_0] = PROT_OVER_TEMP_LIMIT1_TIME_SEC; + otp->protLimitTime[PROT_LIMIT_TIME_1] = PROT_OVER_TEMP_LIMIT2_TIME_SEC; + otp->protLimitTime[PROT_LIMIT_TIME_2] = PROT_OVER_TEMP_LIMIT3_TIME_SEC; + otp->recyDelta = PROT_OVER_IPM_TEMP_RECY_DELTA; + OTP_Clear(otp); +} + +/** + * @brief Over temperatre protection detection. + * @param otp Over temperature protection handle. + * @param tempErrBit Temperature error status bit. + * @param idq DQ-axis feedback currents. + * @retval None. + */ +void OTP_Det(OTP_Handle *otp, MotorErrStatusReg *motorErrStatus, PROT_ErrBit protBit, float temp) +{ + MCS_ASSERT_PARAM(otp != NULL); + MCS_ASSERT_PARAM(motorErrStatus != NULL); + MCS_ASSERT_PARAM(temp > 0.0f); + /* Check if value goes over threshold for continuous cycles. */ + if (temp < otp->protValThr[PROT_VAL_THRESHOLD_0]) { + otp->protCnt = 0; + ClearBit(&motorErrStatus->all, protBit); + return; + } + + if (otp->protCnt < otp->protCntLimit) { + otp->protCnt++; + return; + } + + /* Check which protection level should be activated. */ + /* Once enter error status, error status can only be changed by recover api. */ + /* protect level: level 4. */ + if (temp >= otp->protValThr[PROT_VAL_THRESHOLD_3] && otp->protLevel < PROT_LEVEL_4) { + otp->protLevel = PROT_LEVEL_4; + SetBit(&motorErrStatus->all, protBit); + otp->protCnt = 0; + return; + } + + /* Protect level: level 3. */ + if (temp >= otp->protValThr[PROT_VAL_THRESHOLD_2] && otp->protLevel < PROT_LEVEL_3) { + otp->protLevel = PROT_LEVEL_3; + SetBit(&motorErrStatus->all, protBit); + otp->protCnt = 0; + otp->timer = 0.0f; + return; + } + + /* Protect level: level 2. */ + if (temp >= otp->protValThr[PROT_VAL_THRESHOLD_1] && otp->protLevel < PROT_LEVEL_2) { + otp->protLevel = PROT_LEVEL_2; + SetBit(&motorErrStatus->all, protBit); + otp->protCnt = 0; + otp->timer = 0.0f; + return; + } + + /* Protect level: level 1. */ + if (temp >= otp->protValThr[PROT_VAL_THRESHOLD_0] && otp->protLevel < PROT_LEVEL_1) { + otp->protLevel = PROT_LEVEL_1; + SetBit(&motorErrStatus->all, protBit); + otp->protCnt = 0; + otp->timer = 0.0f; + return; + } +} + +/** + * @brief Over temperature protection execution. + * @param otp Over temperature protection handle. + * @param spdRef Speed reference (Hz). + * @param aptAddr Three-phase APT address pointer. + * @retval None. + */ +void OTP_Exec(OTP_Handle *otp, float *spdRef, APT_RegStruct **aptAddr) +{ + MCS_ASSERT_PARAM(otp != NULL); + MCS_ASSERT_PARAM(spdRef != NULL); + MCS_ASSERT_PARAM(aptAddr != NULL); + /* According to protect level, take corresponding action. */ + switch (otp->protLevel) { + /* level 4: disable all PWM output. */ + case PROT_LEVEL_4: + /* Disable three-phase pwm output. */ + ProtSpo_Exec(aptAddr); + *spdRef *= 0.0f; + break; + + /* level 3: derate speed reference. */ + case PROT_LEVEL_3: + *spdRef *= PROT_POW_DN2_PCT; + otp->timer += otp->ts; + if (otp->timer > otp->protLimitTime[PROT_LIMIT_TIME_2]) { + *spdRef *= PROT_POW_DN3_PCT; + } + break; + + /* level 2: derate speed reference. */ + case PROT_LEVEL_2: + /* Reducte motor speed to level 2 */ + *spdRef *= PROT_POW_DN1_PCT; + otp->timer += otp->ts; + if (otp->timer > otp->protLimitTime[PROT_LIMIT_TIME_1]) { + *spdRef *= PROT_POW_DN2_PCT; + } + break; + + /* level 0: take no protection action. */ + case PROT_LEVEL_0: + break; + + case PROT_LEVEL_1: + /* Reducte motor speed to level 0 */ + otp->timer += otp->ts; + if (otp->timer > otp->protLimitTime[PROT_LIMIT_TIME_0]) { + /* level 1: derate speed reference. */ + *spdRef *= PROT_POW_DN1_PCT; + } + break; + + default: + break; + } + return; +} + +/** + * @brief Over temperature protection recovery. + * @param otp Over temperature protection handle. + * @param tempErrBit Temperature error status bit. + * @param temp Temperature (celsius). + * @retval None. + */ +void OTP_Recy(OTP_Handle *otp, MotorErrStatusReg *motorErrStatus, PROT_ErrBit protBit, float temp) +{ + MCS_ASSERT_PARAM(otp != NULL); + MCS_ASSERT_PARAM(motorErrStatus != NULL); + MCS_ASSERT_PARAM(temp > 0.0f); + /* If not under error state, just return without any operation. */ + if (otp->protLevel == PROT_LEVEL_0) { + motorErrStatus->all &= (~(1 >> protBit)); + return; + } + + /* According to protection level, take corresponding recovery action. */ + switch (otp->protLevel) { + /* level 4 */ + case PROT_LEVEL_4: + /* If the temperature is less than threshold 3, level-3 protection is restored. */ + if (temp < otp->protValThr[PROT_VAL_THRESHOLD_3] - otp->recyDelta) { + otp->recyCnt++; + if (otp->recyCnt > otp->recyCntLimit) { + otp->protLevel = PROT_LEVEL_3; + otp->recyCnt = 0; + } + } + break; + + /* level 3 */ + case PROT_LEVEL_3: + /* If the temperature is less than threshold 2, level-2 protection is restored. */ + if (temp < otp->protValThr[PROT_VAL_THRESHOLD_2] - otp->recyDelta) { + otp->recyCnt++; + if (otp->recyCnt > otp->recyCntLimit) { + otp->protLevel = PROT_LEVEL_2; + otp->recyCnt = 0; + } + } + break; + + /* level 2 */ + case PROT_LEVEL_2: + /* If the temperature is less than threshold 1, level-1 protection is restored. */ + if (temp < otp->protValThr[PROT_VAL_THRESHOLD_1] - otp->recyDelta) { + otp->recyCnt++; + if (otp->recyCnt > otp->recyCntLimit) { + otp->protLevel = PROT_LEVEL_1; + otp->recyCnt = 0; + } + } + break; + + /* level 1 */ + case PROT_LEVEL_1: + /* If the temperature is less than threshold 0, level-0 protection is restored. */ + if (temp < otp->protValThr[PROT_VAL_THRESHOLD_0] - otp->recyDelta) { + otp->recyCnt++; + if (otp->recyCnt > otp->recyCntLimit) { + otp->protLevel = PROT_LEVEL_0; + otp->recyCnt = 0; + } + } + break; + + default: + break; + } + return; +} + +/** + * @brief Over temperature protection error status clear. + * @param otp Over temperature protection handle. + * @retval None. + */ +void OTP_Clear(OTP_Handle *otp) +{ + MCS_ASSERT_PARAM(otp != NULL); + /* Clear the history value. */ + otp->protCnt = 0; + otp->protLevel = PROT_LEVEL_0; + otp->recyCnt = 0; + otp->timer = 0.0f; +} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/mcs_temp_prot.h b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/mcs_temp_prot.h new file mode 100644 index 00000000..2b9d99a7 --- /dev/null +++ b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/mcs_temp_prot.h @@ -0,0 +1,49 @@ +/** + * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_temp_prot.h + * @author MCU Algorithm Team + * @brief his file contains over temperature protection api declaration. + */ + +/* Define to prevent recursive inclusion ------------------------------------- */ +#ifndef McuMagicTag_MCS_TEMP_PROT_H +#define McuMagicTag_MCS_TEMP_PROT_H + +/* Includes ------------------------------------------------------------------------------------ */ +#include "mcs_prot_cmm.h" +#include "apt_ip.h" + +typedef struct { + unsigned int protCnt; + unsigned int recyCnt; + unsigned int protCntLimit; + unsigned int recyCntLimit; + float protValThr[PROT_VAL_THRESHOLD_NUMS]; /* from low current level to high. */ + float protLimitTime[PROT_LIMIT_TIME_NUMS]; /* from level 3 to level 1. */ + float recyDelta; + float ts; + float timer; + PROT_Level protLevel; +} OTP_Handle; + +void OTP_Init(OTP_Handle *otp, float ts); +void OTP_Det(OTP_Handle *otp, MotorErrStatusReg *motorErrStatus, PROT_ErrBit protBit, float temp); +void OTP_Exec(OTP_Handle *otp, float *spdRef, APT_RegStruct **aptAddr); +void OTP_Recy(OTP_Handle *otp, MotorErrStatusReg *motorErrStatus, PROT_ErrBit protBit, float temp); +void OTP_Clear(OTP_Handle *otp); +#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/readme.md b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/readme.md new file mode 100644 index 00000000..98b2a068 --- /dev/null +++ b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/readme.md @@ -0,0 +1,10 @@ +# pmsm_sensorless_2shunt_foc + +**【功能描述】** ++ 基于ECMCU105H/ECBMCU201MPC单板的单电机双电阻采样Foc应用 + +**【环境要求】** ++ 所有单板电源改制为演示用的12V低压,电机选用Gimbal GBM2804H-100T + +**【IDE配置方法】** ++ chipConfig中的Sample栏目里面选中pmsm sensorless 2shunt foc示例,然后点击生成代码即可 diff --git a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/src/mcs_carrier.c b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/src/mcs_carrier.c new file mode 100644 index 00000000..9fc8afac --- /dev/null +++ b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/src/mcs_carrier.c @@ -0,0 +1,144 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_carrier.c + * @author MCU Algorithm Team + * @brief This file provides carrier interrupt application for motor control. + */ + +#include "mcs_carrier.h" +#include "mcs_math.h" +#include "typedefs.h" +#include "mcs_assert.h" +#include "mcs_user_config.h" +#include "mcs_ctlmode_config.h" + +/** + * @brief Synchronous rotation coordinate system angle. + * @param mtrCtrl The motor control handle. + * @retval None. + */ +static void MCS_SyncCoorAngle(MTRCTRL_Handle *mtrCtrl) +{ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + /* Synchronous rotation coordinate system angle. */ + switch (mtrCtrl->stateMachine) { + case FSM_STARTUP: + /* Current ramp angle is 0. */ + if (mtrCtrl->startup.stage == STARTUP_STAGE_CURR) { + mtrCtrl->axisAngle = 0; + } else if (mtrCtrl->startup.stage == STARTUP_STAGE_SPD) { /* IF control phase angle self-addition. */ + mtrCtrl->axisAngle = IF_CurrAngleCalc(&mtrCtrl->ifCtrl, mtrCtrl->spdRefHz); + } else if (mtrCtrl->startup.stage == STARTUP_STAGE_SWITCH) { /* Switch Angle */ + mtrCtrl->axisAngle = mtrCtrl->smo.elecAngle; + } + break; + + case FSM_RUN: + mtrCtrl->axisAngle = mtrCtrl->smo.elecAngle; + break; + + default: + mtrCtrl->axisAngle = 0; + break; + } +} + +/** + * @brief PWM waveform setting and sampling point setting for a single resistors and dual resistors. + * @param mtrCtrl The motor control handle. + * @retval None. + */ +static void MCS_PwmAdcSet(MTRCTRL_Handle *mtrCtrl) +{ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + SampleMode sampleMode = mtrCtrl->sampleMode; + /* Set the duty cycle according to the sampling mode. */ + if (sampleMode == DUAL_RESISTORS) { + SVPWM_Exec(&mtrCtrl->sv, &mtrCtrl->vabRef, &mtrCtrl->dutyUvw); + mtrCtrl->setPwmDutyCb(&mtrCtrl->dutyUvw, &mtrCtrl->dutyUvw); + } else if (sampleMode == SINGLE_RESISTOR) { + R1SVPWM_Exec(&mtrCtrl->r1Sv, &mtrCtrl->vabRef, &mtrCtrl->dutyUvwLeft, &mtrCtrl->dutyUvwRight); + mtrCtrl->setPwmDutyCb(&mtrCtrl->dutyUvwLeft, &mtrCtrl->dutyUvwRight); + /* The ADC sampling point position needs to be set based on the phase shift of a single resistors. */ + mtrCtrl->setADCTriggerTimeCb(mtrCtrl->r1Sv.samplePoint[0] * mtrCtrl->aptMaxcntCmp, \ + mtrCtrl->r1Sv.samplePoint[1] * mtrCtrl->aptMaxcntCmp); + } +} + +/** + * @brief Carrier interrupt function. + * @param mtrCtrl The motor control handle. + * @retval None. + */ +void MCS_CarrierProcess(MTRCTRL_Handle *mtrCtrl) +{ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + UvwAxis *currUvw = &mtrCtrl->currUvw; + AlbeAxis *currAlbe = &mtrCtrl->iabFbk; + AlbeAxis *vab = &mtrCtrl->vabRef; + SampleMode sampleMode = mtrCtrl->sampleMode; + /* sample mode verify */ + if (sampleMode > SINGLE_RESISTOR || mtrCtrl->setADCTriggerTimeCb == NULL) { + return; + } + /* param verify */ + if (mtrCtrl->setPwmDutyCb == NULL || mtrCtrl->readCurrUvwCb == NULL) { + return; + } + /* Read the three-phase current value. */ + mtrCtrl->readCurrUvwCb(currUvw); + /* Clark Calc */ + ClarkeCalc(currUvw, currAlbe); + /* Smo observation */ + if (mtrCtrl->obserType == FOC_OBSERVERTYPE_SMO1TH) { + /* Smo observation */ + FOSMO_Exec(&mtrCtrl->smo, currAlbe, vab, mtrCtrl->spdRefHz); + } else if (mtrCtrl->obserType == FOC_OBSERVERTYPE_SMO4TH) { + /* Smo4th observation */ + SMO4TH_Exec(&mtrCtrl->smo4th, currAlbe, vab); + mtrCtrl->smo.spdEst = mtrCtrl->smo4th.spdEst; + mtrCtrl->smo.elecAngle = mtrCtrl->smo4th.elecAngle; + } + /* Synchronization angle */ + MCS_SyncCoorAngle(mtrCtrl); + + /* Park transformation */ + ParkCalc(currAlbe, mtrCtrl->axisAngle, &mtrCtrl->idqFbk); + + /* statemachine */ + switch (mtrCtrl->stateMachine) { + case FSM_STARTUP: + case FSM_RUN: + CURRCTRL_Exec(&mtrCtrl->currCtrl, &mtrCtrl->vdqRef, mtrCtrl->smo.spdEst, 0); + InvParkCalc(&mtrCtrl->vdqRef, mtrCtrl->axisAngle, vab); + MCS_PwmAdcSet(mtrCtrl); + break; + + case FSM_CAP_CHARGE: + case FSM_CLEAR: + case FSM_IDLE: + mtrCtrl->smo4th.spdEst = 0.0f; + break; + + default: + vab->alpha = 0.0f; + vab->beta = 0.0f; + MCS_PwmAdcSet(mtrCtrl); + break; + } +} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/src/mcs_motor_process.c b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/src/mcs_motor_process.c new file mode 100644 index 00000000..79bc3949 --- /dev/null +++ b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/src/mcs_motor_process.c @@ -0,0 +1,950 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_motor_process.c + * @author MCU Algorithm Team + * @brief This file provides motor application. + * @details Single FOC application based on the ECMCU105H/ECBMCU201MPC board + * 1) Motor model is Gimbal GBM2804H-100T. + * 2) Select the pmsm sensorless 2shunt foc example in the sample column + of chipConfig and click Generate Code. + * 3) It's power supply must be changed to 12V. + */ +#include "main.h" +#include "mcs_user_config.h" +#include "mcs_math.h" +#include "hmi_module.h" +#include "mcs_ctlmode_config.h" +#include "mcs_prot_user.h" +#include "mcs_prot_user_config.h" +#include "mcs_math_const.h" +#include "mcs_motor_process.h" +#include "mcs_chip_config.h" +#include + +/*------------------------------- Macro Definition -----------------------------------------------*/ +#define US_PER_MS 1000 +#define ANGLE_RANGE_ABS 65536 +#define ANGLE_360_F 65536.0f /* 0 - 65536 indicates 0 to 360. */ +#define APT_FULL_DUTY 1.0f +#define TEMP_3 3.0f +#define TEMP_15 15.0f +#define TEMP_30 30.0f +#define TEMP_45 45.0f +#define TEMP_60 60.0f +#define TEMP_RES_15 78.327f +#define TEMP_RES_30 36.776f +#define TEMP_RES_45 18.301f +#define TEMP_RES_60 9.607f +#define CNT_10 10 +#define CNT_5000 5000 +#define LEVEL_4 4 +#define MOTOR_START_DELAY 2 +#define ADC_READINIT_DELAY 1 +#define ADC_READINIT_TIMES 20 +#define ADC_TRIMVALUE_MIN 1800.0f +#define ADC_TRIMVALUE_MAX 2200.0f +/*------------------------------- Param Definition -----------------------------------------------*/ +/* Motor parameters. */ +/* Np, Rs, Ld, Lq, Psif, J, Nmax, Currmax, PPMR, zShift */ +static MOTOR_Param g_motorParam = MOTORPARAM_DEFAULTS; +static APT_RegStruct* g_apt[PHASE_MAX_NUM] = {APT_U, APT_V, APT_W}; +/* Motor control handle */ +static MTRCTRL_Handle g_mc = {0}; + +/* Motor speed loop PI param. */ +static void SPDCTRL_InitWrapper(SPDCTRL_Handle *spdHandle, float ts) +{ + /* Speed loop param assignment. */ + PI_Param spdPi = { + .kp = SPD_KP, + .ki = SPD_KI, + .lowerLim = SPD_LOWERLIM, + .upperLim = SPD_UPPERLIM, + }; + /* Speed loop param init. */ + SPDCTRL_Init(spdHandle, &g_motorParam, spdPi, ts); +} + +/* Motor current Loop PI param. */ +static void CURRCTRL_InitWrapper(CURRCTRL_Handle *currHandle, DqAxis *idqRef, DqAxis *idqFbk, float ts) +{ + /* Axis-D current loop param assignment. */ + PI_Param dCurrPi = { + .kp = CURRDAXIS_KP, + .ki = CURRDAXIS_KI, + .lowerLim = CURR_LOWERLIM, + .upperLim = CURR_UPPERLIM, + }; + /* Axis-Q current loop param assignment. */ + PI_Param qCurrPi = { + .kp = CURRQAXIS_KP, + .ki = CURRQAXIS_KI, + .lowerLim = CURR_LOWERLIM, + .upperLim = CURR_UPPERLIM, + }; + /* Current loop param init. */ + CURRCTRL_Init(currHandle, &g_motorParam, idqRef, idqFbk, dCurrPi, qCurrPi, ts); +} + +/* First order smo param. */ +static void FOSMO_InitWrapper(FOSMO_Handle *fosmo, float ts) +{ + /* Smo param assignment. */ + FOSMO_Param fosmoParam = { + .gain = FOSMO_GAIN, + .lambda = FOSMO_LAMBDA, + .fcEmf = FOSMO_EMF_CUTOFF_FREQ, + .fcLpf = SPEED_FILTER_CUTOFF_FREQUENCY, + .pllBdw = FOSMO_PLL_BDW, + }; + /* Init smo param. */ + FOSMO_Init(fosmo, fosmoParam, &g_motorParam, ts); +} + +/* Smo4th param. */ +static void SMO4TH_InitWrapper(SMO4TH_Handle *smo4TH) +{ + /* Smo4th param assignment. */ + SMO4TH_Param smo4thParam = { + .kd = SMO4TH_KD, + .kq = SMO4TH_KQ, + .pllBdw = SMO4TH_PLL_BDW, + .fcLpf = SMO4TH_SPD_FILTER_CUTOFF_FREQ, + }; + /* Init smo param. */ + SMO4TH_Init(smo4TH, smo4thParam, &g_motorParam, CTRL_CURR_PERIOD); +} + +/*------------------------------- Function Definition -----------------------------------------------*/ +/** + * @brief Initialzer of system tick. + * @param mtrCtrl Motor control struct handle. + * @retval None. + */ +static void TimerTickInit(MTRCTRL_Handle *mtrCtrl) +{ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + mtrCtrl->sysTickCnt = 0; + mtrCtrl->msTickNum = US_PER_MS / SYSTICK_PERIOD_US; + mtrCtrl->capChargeTickNum = (INV_CAP_CHARGE_MS * US_PER_MS / SYSTICK_PERIOD_US); +} + +/** + * @brief Init motor control task. + * @retval None. + */ +static void TSK_Init(void) +{ + g_mc.motorStateFlag = 0; + g_mc.uartHeartDetCnt = 0; + g_mc.uartTimeStamp = 0; + g_mc.stateMachine = FSM_IDLE; + g_mc.currCtrlPeriod = CTRL_CURR_PERIOD; /* Init current controller */ + g_mc.aptMaxcntCmp = g_apt0.waveform.timerPeriod; + g_mc.sampleMode = DUAL_RESISTORS; + g_mc.obserType = FOC_OBSERVERTYPE_SMO4TH; /* Init foc observe mode */ + g_mc.controlMode = FOC_CONTROLMODE_SPEED; /* Init motor control mode */ + g_mc.adcCurrCofe = ADC_CURR_COFFI; + g_mc.spdAdjustMode = CUST_SPEED_ADJUST; + g_mc.uartConnectFlag = DISCONNECT; + g_mc.spdCmdHz = USER_MIN_SPD_HZ; /* Motor initialization speed */ + + g_mc.adc0Compensate = ADC0COMPENSATE; /* Phase-u current init adc shift trim value */ + g_mc.adc1Compensate = ADC1COMPENSATE; /* Phase-w current init adc shift trim value */ + + IF_Init(&g_mc.ifCtrl, CTRL_IF_CURR_AMP_A, USER_CURR_SLOPE, CTRL_SYSTICK_PERIOD, CTRL_CURR_PERIOD); + RMG_Init(&g_mc.spdRmg, CTRL_SYSTICK_PERIOD, USER_SPD_SLOPE); /* Init speed slope */ + MtrParamInit(&g_mc.mtrParam, g_motorParam); + + TimerTickInit(&g_mc); + if (g_mc.sampleMode == DUAL_RESISTORS) { + SVPWM_Init(&g_mc.sv, INV_VOLTAGE_BUS * ONE_DIV_SQRT3); /* Dual resistors SVPWM init. */ + } else if (g_mc.sampleMode == SINGLE_RESISTOR) { + /* Single resistor SVPWM init. */ + R1SVPWM_Init(&g_mc.r1Sv, INV_VOLTAGE_BUS * ONE_DIV_SQRT3, SAMPLE_POINT_SHIFT, SAMPLE_WINDOW_DUTY); + } + + SPDCTRL_InitWrapper(&g_mc.spdCtrl, CTRL_SYSTICK_PERIOD); + CURRCTRL_InitWrapper(&g_mc.currCtrl, &g_mc.idqRef, &g_mc.idqFbk, CTRL_CURR_PERIOD); + FOSMO_InitWrapper(&g_mc.smo, CTRL_CURR_PERIOD); + SMO4TH_InitWrapper(&g_mc.smo4th); + + STARTUP_Init(&g_mc.startup, USER_SWITCH_SPDBEGIN_HZ, USER_SWITCH_SPDBEGIN_HZ + TEMP_3); + + MotorProt_Init(&g_mc.prot); /* Init protect state comond */ + OCP_Init(&g_mc.prot.ocp, CTRL_CURR_PERIOD); + OVP_Init(&g_mc.prot.ovp, CTRL_SYSTICK_PERIOD); + LVP_Init(&g_mc.prot.lvp, CTRL_SYSTICK_PERIOD); + OTP_Init(&g_mc.prot.otp, CTRL_SYSTICK_PERIOD); + STP_Init(&g_mc.prot.stall, CTRL_SYSTICK_PERIOD, PROT_STALLING_CURR_AMP_LIMIT, + PROT_STALLING_SPD_LIMIT, PROT_STALLING_TIME_LIMIT); +} + +/** + * @brief Clear historical values of all controller before start-up. + * @param mtrCtrl The motor control handle. + * @retval None. + */ +static void ClearBeforeStartup(MTRCTRL_Handle *mtrCtrl) +{ + /* Verify Parameters */ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + /* The initial angle is 0. */ + mtrCtrl->axisAngle = 0; + + mtrCtrl->spdRefHz = 0.0f; + /* The initial dq-axis reference current is 0. */ + mtrCtrl->idqRef.d = 0.0f; + mtrCtrl->idqRef.q = 0.0f; + + mtrCtrl->vdqRef.d = 0.0f; + mtrCtrl->vdqRef.q = 0.0f; + /* Clear Duty Cycle Value. The initial duty cycle is 0.5. */ + mtrCtrl->dutyUvwLeft.u = 0.5f; + mtrCtrl->dutyUvwLeft.v = 0.5f; + mtrCtrl->dutyUvwLeft.w = 0.5f; + mtrCtrl->dutyUvwRight.u = 0.5f; + mtrCtrl->dutyUvwRight.v = 0.5f; + mtrCtrl->dutyUvwRight.w = 0.5f; + + mtrCtrl->prot.motorErrStatus.all = 0x00; + + RMG_Clear(&mtrCtrl->spdRmg); /* Clear the history value of speed slope control */ + CURRCTRL_Clear(&mtrCtrl->currCtrl); + IF_Clear(&mtrCtrl->ifCtrl); + SPDCTRL_Clear(&mtrCtrl->spdCtrl); + FOSMO_Clear(&mtrCtrl->smo); + SMO4TH_Clear(&mtrCtrl->smo4th); + STARTUP_Clear(&mtrCtrl->startup); + R1SVPWM_Clear(&mtrCtrl->r1Sv); + + OTP_Clear(&mtrCtrl->prot.otp); + OCP_Clear(&mtrCtrl->prot.ocp); + OVP_Clear(&mtrCtrl->prot.ovp); + LVP_Clear(&mtrCtrl->prot.lvp); +} + +/** + * @brief To set the comparison value of the IGBT single-resistance ADC sampling trigger position. + * @param aptx The APT register struct handle. + * @param cntCmpA A Count compare reference of time-base counter. + * @param cntCmpB B Count compare reference of time-base counter. + * @param maxCntCmp Maximum Comparison Value + * @retval None. + */ +static void MCS_SetAdcCompareR1(APT_RegStruct *aptx, unsigned short cntCmpA, + unsigned short cntCmpB, unsigned short maxCntCmp) +{ + unsigned short tmp; + /* Sets the A Count compare reference of time-base counter. */ + tmp = (unsigned short)Clamp((float)(cntCmpA), (float)(maxCntCmp - 1), 1.0f); + DCL_APT_SetCounterCompare(aptx, APT_COMPARE_REFERENCE_A, tmp); + /* Sets the B Count compare reference of time-base counter. */ + tmp = (unsigned short)Clamp((float)(cntCmpB), (float)(maxCntCmp - 1), 1.0f); + DCL_APT_SetCounterCompare(aptx, APT_COMPARE_REFERENCE_B, tmp); +} + +/** + * @brief Open the three-phase lower pipe. + * @param aptAddr Three-phase APT address pointer. + * @param maxDutyCnt Max duty count. + * @retval None. + */ +static void AptTurnOnLowSidePwm(APT_RegStruct **aptAddr, unsigned short maxDutyCnt) +{ + MCS_ASSERT_PARAM(aptAddr != NULL); + MCS_ASSERT_PARAM(maxDutyCnt != 0); + unsigned short dutyCnt; + dutyCnt = (unsigned short)(maxDutyCnt * APT_FULL_DUTY); + /* Open the three-phase lower pipe */ + for (unsigned int i = 0; i < PHASE_MAX_NUM; i++) { + APT_RegStruct *aptx = (APT_RegStruct *)(aptAddr[i]); + DCL_APT_SetCounterCompare(aptx, APT_COMPARE_REFERENCE_C, dutyCnt); + DCL_APT_SetCounterCompare(aptx, APT_COMPARE_REFERENCE_D, dutyCnt); + } +} + +/** + * @brief Enable three-phase pwm output. + * @param aptAddr Three-phase APT address pointer. + * @retval None. + */ +static void MotorPwmOutputEnable(APT_RegStruct **aptAddr) +{ + MCS_ASSERT_PARAM(aptAddr != NULL); + /* Enable three-phase pwm output */ + for (unsigned int i = 0; i < PHASE_MAX_NUM; i++) { + APT_RegStruct *aptx = (APT_RegStruct *)(aptAddr[i]); + aptx->PG_OUT_FRC.BIT.rg_pga_frc_en = BASE_CFG_UNSET; + aptx->PG_OUT_FRC.BIT.rg_pgb_frc_en = BASE_CFG_UNSET; + } +} + +/** + * @brief Disable three-phase pwm output. + * @param aptAddr Three-phase APT address pointer. + * @retval None. + */ +static void MotorPwmOutputDisable(APT_RegStruct **aptAddr) +{ + MCS_ASSERT_PARAM(aptAddr != NULL); + /* Disable three-phase pwm output. */ + for (unsigned int i = 0; i < PHASE_MAX_NUM; i++) { + APT_RegStruct *aptx = (APT_RegStruct *)(aptAddr[i]); + aptx->PG_OUT_FRC.BIT.rg_pga_frc_en = BASE_CFG_SET; + aptx->PG_OUT_FRC.BIT.rg_pgb_frc_en = BASE_CFG_SET; + DCL_APT_ForcePWMOutputLow(aptx); + } +} + +/** + * @brief Smo IF angle difference calculation. + * @param smoElecAngle Smo electrical angle. + * @param ifCtrlAngle IF control angle. + * @retval signed short angle difference. + */ +static float SmoIfAngleDiffCalc(float smoElecAngle, float ifCtrlAngle) +{ + float diff = AngleSub(smoElecAngle, ifCtrlAngle); + /* Smo IF angle difference calculation */ + return diff; +} + +/** + * @brief Construct a new mcs startupswitch object. + * @param mtrCtrl The motor control handle. + * @retval None. + */ +static void MCS_StartupSwitch(MTRCTRL_Handle *mtrCtrl) +{ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + STARTUP_Handle *startup = &mtrCtrl->startup; + DqAxis *idqRef = &mtrCtrl->idqRef; + float iftargetAmp = mtrCtrl->ifCtrl.targetAmp; + float spdRefHz = mtrCtrl->spdRefHz; + + switch (startup->stage) { + case STARTUP_STAGE_CURR: + if (mtrCtrl->ifCtrl.curAmp >= iftargetAmp) { + /* Stage change */ + idqRef->q = iftargetAmp; + startup->stage = STARTUP_STAGE_SPD; + } else { + /* current amplitude increase */ + idqRef->q = IF_CurrAmpCalc(&mtrCtrl->ifCtrl); + spdRefHz = 0.0f; + } + break; + case STARTUP_STAGE_SPD: + /* current frequency increase */ + if (Abs(spdRefHz) >= startup->spdBegin) { + /* Stage change */ + startup->stage = STARTUP_STAGE_SWITCH; + TrigVal localTrigVal; + TrigCalc(&localTrigVal, SmoIfAngleDiffCalc(mtrCtrl->smo.elecAngle, mtrCtrl->ifCtrl.angle)); + idqRef->d = 0.0f; + mtrCtrl->spdCtrl.spdPi.integral = iftargetAmp * localTrigVal.cos; + } else { + /* Speed rmg */ + spdRefHz = RMG_Exec(&mtrCtrl->spdRmg, mtrCtrl->spdCmdHz); + } + break; + + case STARTUP_STAGE_SWITCH: + /* Switch from IF to SMO */ + spdRefHz = RMG_Exec(&mtrCtrl->spdRmg, mtrCtrl->spdCmdHz); + idqRef->q = SPDCTRL_Exec(&mtrCtrl->spdCtrl, mtrCtrl->spdRefHz, mtrCtrl->smo.spdEst); + /* Transitional stage, if current reference speed > critical speed, change to next stage */ + if (spdRefHz >= startup->spdBegin + TEMP_3) { + /* Stage change */ + mtrCtrl->stateMachine = FSM_RUN; + } + break; + + default: + break; + } + mtrCtrl->spdRefHz = spdRefHz; +} + +/** + * @brief Pre-processing of motor status. + * @param statusReg System status. + * @param stateMachine Motor Control Status. + * @retval None. + */ +static void MotorStatePerProc(SysStatusReg *statusReg, volatile FsmState *stateMachine) +{ + MCS_ASSERT_PARAM(statusReg != NULL); + MCS_ASSERT_PARAM(stateMachine != NULL); + /* Get system status */ + if (SysIsError(statusReg)) { + *stateMachine = FSM_FAULT; + } + if (SysGetCmdStop(statusReg)) { + SysCmdStopClr(statusReg); + *stateMachine = FSM_STOP; + } +} + +/** + * @brief Check over current status. + * @param statusReg System status. + * @param stateMachine Motor Control Status. + * @retval None. + */ +static void CheckOverCurrentState(SysStatusReg *statusReg, FsmState *stateMachine) +{ + MCS_ASSERT_PARAM(statusReg != NULL); + MCS_ASSERT_PARAM(stateMachine != NULL); + /* check systerm error status */ + if (SysIsError(statusReg) == false) { + *stateMachine = FSM_IDLE; + } +} + +/** + * @brief Check bootstrap capacitor charge time. + * @param mtrCtrl The motor control handle. + * @param stateMachine Motor Control Status. + * @retval None. + */ +static void CheckBootstrpCapChargeTime(MTRCTRL_Handle *mtrCtrl, FsmState *stateMachine) +{ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + MCS_ASSERT_PARAM(stateMachine != NULL); + mtrCtrl->sysTickCnt++; + /* check bootstrap capacitor charge time */ + if (mtrCtrl->sysTickCnt == mtrCtrl->capChargeTickNum) { + *stateMachine = FSM_CLEAR; + } +} + +/** + * @brief Check systerm cmd start signal. + * @param mtrCtrl The motor control handle. + * @param aptAddr Three-phase APT address pointer. + * @param statusReg System status. + * @param stateMachine Motor Control Status. + * @retval None. + */ +static void CheckSysCmdStart(MTRCTRL_Handle *mtrCtrl, + APT_RegStruct **aptAddr, + SysStatusReg *statusReg, + FsmState *stateMachine) +{ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + MCS_ASSERT_PARAM(aptAddr != NULL); + MCS_ASSERT_PARAM(statusReg != NULL); + MCS_ASSERT_PARAM(stateMachine != NULL); + /* check start cmd */ + if (SysGetCmdStart(statusReg)) { + SysRunningSet(statusReg); + SysCmdStartClr(statusReg); + mtrCtrl->sysTickCnt = 0; + *stateMachine = FSM_CAP_CHARGE; + /* Preparation for charging the bootstrap capacitor. */ + AptTurnOnLowSidePwm(aptAddr, mtrCtrl->aptMaxcntCmp); + /* Out put pwm */ + MotorPwmOutputEnable(aptAddr); + } +} + +/** + * @brief System timer tick task. + * @param mtrCtrl The motor control handle. + * @param aptAddr Three-phase APT address pointer. + * @retval None. + */ +static void TSK_SystickIsr(MTRCTRL_Handle *mtrCtrl, APT_RegStruct **aptAddr) +{ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + MCS_ASSERT_PARAM(aptAddr != NULL); + SysStatusReg *statusReg = &mtrCtrl->statusReg; + FsmState *stateMachine = &mtrCtrl->stateMachine; + mtrCtrl->msTickCnt++; + /* Pre-processing of motor status. */ + MotorStatePerProc(statusReg, stateMachine); + /* statemachine */ + switch (*stateMachine) { + case FSM_IDLE: + /* Set smo estimate speed before motor start-up */ + g_mc.smo.spdEst = 0.0f; + CheckSysCmdStart(mtrCtrl, aptAddr, statusReg, stateMachine); + break; + case FSM_CAP_CHARGE: + /* Bootstrap Capacitor Charging Timing */ + CheckBootstrpCapChargeTime(mtrCtrl, stateMachine); + break; + /* Clear parameter before start */ + case FSM_CLEAR: + ClearBeforeStartup(mtrCtrl); + *stateMachine = FSM_STARTUP; + break; + case FSM_STARTUP: + MCS_StartupSwitch(mtrCtrl); + break; + case FSM_RUN: + /* Speed ramp control */ + mtrCtrl->spdRefHz = RMG_Exec(&mtrCtrl->spdRmg, mtrCtrl->spdCmdHz); + /* Speed loop control */ + mtrCtrl->idqRef.q = SPDCTRL_Exec(&mtrCtrl->spdCtrl, mtrCtrl->spdRefHz, mtrCtrl->smo.spdEst); + break; + case FSM_STOP: + mtrCtrl->spdRefHz = 0.0f; + MotorPwmOutputDisable(aptAddr); + SysRunningClr(statusReg); + *stateMachine = FSM_IDLE; + break; + case FSM_FAULT: /* Overcurrent state */ + CheckOverCurrentState(statusReg, stateMachine); + break; + default: + break; + } +} + +/** + * @brief Read the ADC initialize bias trim value. + * @param mtrCtrl The motor control handle. + * @retval None. + */ +static void TrimInitAdcShiftValue(MTRCTRL_Handle *mtrCtrl) +{ + float adc0SampleTemp = 0.0f; /* Current bias value for temp store */ + float adc1SampleTemp = 0.0f; + float adc0TempSum = 0.0f; + float adc1TempSum = 0.0f; /* Current bias sum value for 20 times */ + float adcSampleTimes = 0.0f; /* ADC sample times */ + for (int i = 0; i < ADC_READINIT_TIMES; i++) { + adc0SampleTemp = (float)HAL_ADC_GetConvResult(&ADCU_HANDLE, ADCUSOCNUM); + adc1SampleTemp = (float)HAL_ADC_GetConvResult(&ADCW_HANDLE, ADCWSOCNUM); + BASE_FUNC_DELAY_US(200); /* 200 is delay count, delay 200us triger adc sampling */ + if (adc0SampleTemp > 1000.0f && adc1SampleTemp > 1000.0f) { + adcSampleTimes++; + adc0TempSum += adc0SampleTemp; + adc1TempSum += adc1SampleTemp; + } + } + if (adcSampleTimes < 1.0f) { + adcSampleTimes = 1.0f; /* Prevent divide-by-zero errors */ + } + adc0SampleTemp = adc0TempSum / adcSampleTimes; + adc1SampleTemp = adc1TempSum / adcSampleTimes; + /* Force convert to float */ + mtrCtrl->adc0Compensate = (float) adc0SampleTemp; + mtrCtrl->adc1Compensate = (float) adc1SampleTemp; + /* The normal value scope: 1800 < adc0Compensate < 2200 */ + if(g_mc.adc0Compensate < ADC_TRIMVALUE_MIN || g_mc.adc0Compensate > ADC_TRIMVALUE_MAX \ + || g_mc.adc1Compensate < ADC_TRIMVALUE_MIN || g_mc.adc1Compensate > ADC_TRIMVALUE_MAX) { + DBG_PRINTF("ADC trim value error,please reset!"); + HAL_GPIO_SetValue(&LED2_HANDLE, LED2_PIN, GPIO_LOW_LEVEL); + } + adcSampleTimes = 0; + adc0TempSum = 0; + adc1TempSum = 0; +} + +/** + * @brief Read the ADC current sampling value. + * @param CurrUvw Three-phase current. + * @retval None. + */ +static void ReadCurrUvw(UvwAxis *CurrUvw) +{ + MCS_ASSERT_PARAM(CurrUvw != NULL); + float adc0 = (float)HAL_ADC_GetConvResult(&ADCU_HANDLE, ADCUSOCNUM); + float adc1 = (float)HAL_ADC_GetConvResult(&ADCW_HANDLE, ADCWSOCNUM); + /* Convert adc sample value to current value */ + CurrUvw->u = -(adc0 - g_mc.adc0Compensate) * g_mc.adcCurrCofe; + CurrUvw->w = -(adc1 - g_mc.adc1Compensate) * g_mc.adcCurrCofe; + CurrUvw->v = -CurrUvw->u - CurrUvw->w; +} + +/** + * @brief Setting the APT Output Duty Cycle. + * @param aptx APT register base address. + * @param leftDuty Left duty cycle. + * @param rightDuty Right duty cycle. + * @retval None. + */ +static void SetPwmDuty(APT_Handle *aptx, float leftDuty, float rightDuty) +{ + MCS_ASSERT_PARAM(aptx != NULL); + MCS_ASSERT_PARAM(leftDuty > 0); + MCS_ASSERT_PARAM(rightDuty > 0); + unsigned short maxPeriodCnt = aptx->waveform.timerPeriod; + unsigned short cntCmpLeftEdge = (unsigned short)(leftDuty * maxPeriodCnt); + unsigned short cntCmpRightEdge = (unsigned short)(rightDuty * maxPeriodCnt); + /* avoid overflowing */ + cntCmpLeftEdge = (cntCmpLeftEdge > maxPeriodCnt) ? maxPeriodCnt : cntCmpLeftEdge; + cntCmpRightEdge = (cntCmpRightEdge > maxPeriodCnt) ? maxPeriodCnt : cntCmpRightEdge; + HAL_APT_SetPWMDuty(aptx, cntCmpLeftEdge, cntCmpRightEdge); +} + +/** + * @brief Duty Cycle Setting. + * @param dutyUvwLeft Three-phase left duty cycle. + * @param dutyUvwRight Three-phase right duty cycle. + * @retval None. + */ +static void SetPwmDutyCp(UvwAxis *dutyUvwLeft, UvwAxis *dutyUvwRight) +{ + MCS_ASSERT_PARAM(dutyUvwLeft != NULL); + MCS_ASSERT_PARAM(dutyUvwRight != NULL); + /* Setting the Three-Phase Duty Cycle */ + SetPwmDuty(&g_apt0, dutyUvwLeft->u, dutyUvwRight->u); + SetPwmDuty(&g_apt1, dutyUvwLeft->v, dutyUvwRight->v); + SetPwmDuty(&g_apt2, dutyUvwLeft->w, dutyUvwRight->w); +} + +/** + * @brief To set the ADC sampling trigger comparison value. + * @param cntCmpSOCA Soca Compare Count Value. + * @param cntCmpSOCB Socb Compare Count Value. + * @retval None. + */ +static void SetADCTriggerTime(unsigned short cntCmpSOCA, unsigned short cntCmpSOCB) +{ + MCS_SetAdcCompareR1(g_apt[PHASE_U], cntCmpSOCA, cntCmpSOCB, g_mc.aptMaxcntCmp); +} + +/** + * @brief Temprature table, the temprature detect range is 15 ~ 60 degree. + * @param tempResisValue Temperature sensor resistance. + * @retval None. + */ +static float TempTable(float tempResisValue) +{ + float boardTemp = 0.0f; + /* Temperatures between 15 and 30. */ + if (tempResisValue > TEMP_RES_30 && tempResisValue <= TEMP_RES_15) { + boardTemp = TEMP_15 + (TEMP_30 - TEMP_15) * (TEMP_RES_15 - tempResisValue) / (TEMP_RES_15 - TEMP_RES_30); + } else if (tempResisValue > TEMP_RES_45 && tempResisValue <= TEMP_RES_30) { /* Temperature between 30 and 45. */ + boardTemp = TEMP_30 + (TEMP_45 - TEMP_30) * (TEMP_RES_30 - tempResisValue) / (TEMP_RES_30 - TEMP_RES_45); + } else if (tempResisValue > TEMP_RES_60 && tempResisValue <= TEMP_RES_45) { /* Temperature between 45 and 50. */ + boardTemp = TEMP_45 + (TEMP_60 - TEMP_45) * (TEMP_RES_45 - tempResisValue) / (TEMP_RES_45 - TEMP_RES_60); + } else if (tempResisValue <= TEMP_RES_60) { + boardTemp = TEMP_60; /* If temperature is over 60, set temperature as 60. */ + } else if (tempResisValue >= TEMP_RES_15) { + boardTemp = TEMP_15; /* If temperature is lower 15, set temperature as 15. */ + } + return boardTemp; +} + +/** + * @brief Read power board temperature and udc. + * @retval None. + */ +static void ReadBoardTempAndUdc(void) +{ + HAL_ADC_SoftTrigSample(&ADCRESIS_HANDLE, ADCRESISSOCNUM); + HAL_ADC_SoftTrigSample(&ADCUDC_HANDLE, ADCUDCSOCNUM); + BASE_FUNC_DELAY_US(CNT_10); /* Delay 10 us. */ + /* Force convert to float type. */ + float resisAdcValue = (float)HAL_ADC_GetConvResult(&ADCRESIS_HANDLE, ADCRESISSOCNUM); + /* 10 / (x + 10) * 3.3 = resisAdcValue / 4096 * 3.3, x is resisValue, 10kohm is resistor divider value. */ + float resisValue = (4096.0f * 10.0f - 10.0f * resisAdcValue) / resisAdcValue; + g_mc.powerBoardTemp = TempTable(resisValue); + g_mc.udc = ((float)HAL_ADC_GetConvResult(&ADCUDC_HANDLE, ADCUDCSOCNUM)) * ADC_UDC_COFFI; +} + +/** + * @brief Execut abnormal feedback speed protect motion. + * @retval None. + */ +static void SpdFbkErrorProt_Exec(void) +{ + if (g_mc.prot.motorErrStatus.Bit.motorStalling == 0 && + g_mc.prot.motorErrStatus.Bit.overVoltErr == 0 && + g_mc.prot.motorErrStatus.Bit.lowerVoltErr == 0 && + g_mc.prot.motorErrStatus.Bit.overIpmTempErr == 0 && + g_mc.prot.motorErrStatus.Bit.overCurrErr == 0) { + g_mc.prot.motorErrStatus.Bit.revRotErr = 1; + /* If revRotErr, execute protect motion. */ + ProtSpo_Exec(g_apt); + } +} + +/** + * @brief Execut nan data protect motion. + * @retval None. + */ +static void NanDataDetect(void) +{ + static short errorSpdStatus = 0; + /* Detect the nan observer speed or current value. */ + if (isnan(g_mc.smo.spdEst) || isnan(g_mc.idqRef.q)) { + errorSpdStatus++; + } else { + errorSpdStatus = 0; + } + /* If the data is nan & continuous counting value is over 500 times, execute protect motion. + the detect time is 500 * 500us = 250ms. */ + if (errorSpdStatus >= 500) { + errorSpdStatus = 0; + SpdFbkErrorProt_Exec(); + } +} + +/** + * @brief Check abnormal feedback speed. + * @retval None. + */ +static void CheckSpdFbkStatus(void) +{ + static short errorCurrStatus = 0; + static short errorDeltaSpdStatus = 0; + NanDataDetect(); + if (g_mc.stateMachine == FSM_RUN) { + /* Detect the abnormal idq feedback current. */ + if (Abs(g_mc.idqRef.q - g_mc.idqFbk.q) >= CTRL_IF_CURR_AMP_A) { + errorCurrStatus++; + } else { + errorCurrStatus = 0; + } + /* Detect the abnormal feedback speed, the normal speed is > 0, if smo.spdEst < -10 && + delta speed error > USER_MIN_SPD_HZ + 10.0f at FSM_RUN stage, set the motor motion as error */ + if (g_mc.smo.spdEst < -10.0f && (g_mc.spdRefHz - g_mc.smo.spdEst > USER_MIN_SPD_HZ + 10.0f)) { + errorDeltaSpdStatus++; + } + } + /* Execute protect motion if count over 500 times, this error status caused by abnormal speed + or cabnormal urrent feedback, the detect time is 500 * 500us = 250ms. */ + if (errorCurrStatus >= 500) { + errorCurrStatus = 0; + SpdFbkErrorProt_Exec(); + } + /* This error statu caused by motor stalling, the detect time is 2 * 500us = 1ms. */ + if (errorDeltaSpdStatus >= 2) { + errorDeltaSpdStatus = 0; + g_mc.prot.motorErrStatus.Bit.motorStalling = 1; + } +} + +/** + * @brief Check Potentiometer Value callback function. + * @param param The TIMER_Handle. + * @retval None. + */ +void CheckPotentiometerValueCallback(void *param) +{ + MCS_ASSERT_PARAM(param != NULL); + BASE_FUNC_UNUSED(param); + static float potentiomitorAdcValue = 0.0f; + static float spdCmdHz = 0; + static float spdCmdHzLast = USER_MIN_SPD_HZ; /* 35.0 is spdCmdHzLast init value */ + HAL_ADC_SoftTrigSample(&ADCPTT_HANDLE, ADCPTTSOCNUM); + BASE_FUNC_DELAY_US(10); /* Delay 10 us. */ + potentiomitorAdcValue = (float)HAL_ADC_GetConvResult(&ADCPTT_HANDLE, ADCPTTSOCNUM); + /* 4045.0 is adc sample max value of potentiomitor, convert max spd to 180.25Hz */ + spdCmdHz = potentiomitorAdcValue / 4045.0f * USER_MAX_SPD_HZ; + if (Abs(spdCmdHzLast - spdCmdHz) < 1.0f) { /* Ignore changes less than 1. */ + return; + } + spdCmdHzLast = spdCmdHz; + if (spdCmdHz < USER_MIN_SPD_HZ) { /* 35.0 is spdCmdHz lower limit */ + spdCmdHz = USER_MIN_SPD_HZ; /* 35.0 is spdCmdHz lower limit */ + } + if (spdCmdHz > g_mc.mtrParam.maxElecSpd) { /* spdCmdHz upper limit */ + spdCmdHz = g_mc.mtrParam.maxElecSpd; /* spdCmdHz upper limit */ + } + if (g_mc.spdAdjustMode == CUST_SPEED_ADJUST) { + g_mc.spdCmdHz = spdCmdHz; + } +} + +/** + * @brief System timer ISR for Motor Statemachine CallBack function. + * @param param The systick timer handle. + * @retval None. + */ +void MotorStatemachineCallBack(void *param) +{ + /* Verify Parameters */ + MCS_ASSERT_PARAM(param != NULL); + BASE_FUNC_UNUSED(param); + /* Read power board temprature and voltage. */ + ReadBoardTempAndUdc(); + /* Motor speed loop state machine. */ + TSK_SystickIsr(&g_mc, g_apt); + + /* Motor error speed feedback check. */ + CheckSpdFbkStatus(); + /* Motor stalling detect. */ + STP_Det_ByCurrSpd(&g_mc.prot.stall, &g_mc.prot.motorErrStatus, g_mc.smo.spdEst, g_mc.idqFbk); + STP_Exec(&g_mc.prot.motorErrStatus, g_apt); + + /* Motor over voltage detect. */ + OVP_Det(&g_mc.prot.ovp, &g_mc.prot.motorErrStatus, g_mc.udc); + OVP_Exec(&g_mc.prot.ovp, &g_mc.spdRefHz, g_apt); + OVP_Recy(&g_mc.prot.ovp, &g_mc.prot.motorErrStatus, g_mc.udc); + /* Motor lower voltage detect. */ + LVP_Det(&g_mc.prot.lvp, &g_mc.prot.motorErrStatus, g_mc.udc); + LVP_Exec(&g_mc.prot.lvp, &g_mc.spdRefHz, g_apt); + LVP_Recy(&g_mc.prot.lvp, &g_mc.prot.motorErrStatus, g_mc.udc); + /* Power board over temperature detect. */ + OTP_Det(&g_mc.prot.otp, &g_mc.prot.motorErrStatus, OTP_IPM_ERR_BIT, g_mc.powerBoardTemp); + OTP_Exec(&g_mc.prot.otp, &g_mc.spdRefHz, g_apt); + OTP_Recy(&g_mc.prot.otp, &g_mc.prot.motorErrStatus, OTP_IPM_ERR_BIT, g_mc.powerBoardTemp); + + /* If protect level == 4, set motor state as stop. */ + if (g_mc.prot.ovp.protLevel == LEVEL_4 || g_mc.prot.lvp.protLevel == LEVEL_4 \ + || g_mc.prot.otp.protLevel == LEVEL_4) { + SysCmdStopSet(&g_mc.statusReg); + } +} + +/** + * @brief The carrier ISR wrapper function. + * @param aptHandle The APT handle. + * @retval None. + */ +void MotorCarrierProcessCallback(void *aptHandle) +{ + MCS_ASSERT_PARAM(aptHandle != NULL); + BASE_FUNC_UNUSED(aptHandle); + /* the carrierprocess of motor */ + MCS_CarrierProcess(&g_mc); + /* Over current protect */ + if (g_mc.stateMachine == FSM_RUN || g_mc.stateMachine == FSM_STARTUP) { + OCP_Det(&g_mc.prot.ocp, &g_mc.prot.motorErrStatus, g_mc.idqFbk); + OCP_Exec(&g_mc.prot.ocp, &g_mc.idqFbk, g_apt); /* Execute over current protect motion */ + if (g_mc.prot.ocp.protLevel < LEVEL_4) { + OCP_Recy(&g_mc.prot.ocp, &g_mc.prot.motorErrStatus); + } + } +} + +/** + * @brief Event interrupt callback function of APT module. + * @param para APT module handle. + * @retval None. + */ +void MotorSysErrCallback(void *para) +{ + MCS_ASSERT_PARAM(para != NULL); + APT_Handle *handle = (APT_Handle *)para; + /* The IPM overcurrent triggers and disables the three-phase PWM output. */ + MotorPwmOutputDisable(g_apt); + DCL_APT_ClearOutCtrlEventFlag((APT_RegStruct *)g_apt[PHASE_U], APT_OC_COMBINE_EVENT_A1); + DCL_APT_ClearOutCtrlEventFlag((APT_RegStruct *)g_apt[PHASE_V], APT_OC_COMBINE_EVENT_A1); + DCL_APT_ClearOutCtrlEventFlag((APT_RegStruct *)g_apt[PHASE_W], APT_OC_COMBINE_EVENT_A1); + /* Status setting error */ + SysErrorSet(&g_mc.statusReg); + DBG_PRINTF("APT error! \r\n"); + HAL_GPIO_SetValue(&LED2_HANDLE, LED2_PIN, GPIO_LOW_LEVEL); + BASE_FUNC_UNUSED(handle); +} + +/** + * @brief Init motor controller's data structure. + * @retval None. + */ +static void InitSoftware(void) +{ + /* Initializing motor control param */ + TSK_Init(); + /* Read phase-uvw current */ + g_mc.readCurrUvwCb = ReadCurrUvw; + g_mc.setPwmDutyCb = SetPwmDutyCp; + g_mc.setADCTriggerTimeCb = SetADCTriggerTime; +} + +/** + * @brief Config the KEY func. + * @param handle The GPIO handle. + * @retval None. + */ +static KEY_State Key_StateRead(GPIO_Handle *handle) +{ + if (HAL_GPIO_GetPinValue(handle, handle->pins) == 0) { + BASE_FUNC_DELAY_MS(30); /* delay 30ms for deshake */ + if (HAL_GPIO_GetPinValue(handle, handle->pins) == 0) { + while (HAL_GPIO_GetPinValue(handle, handle->pins) == 0) { + } + return KEY_DOWN; + } + } + return KEY_UP; +} + +/** + * @brief Control motor start and stop state by key func. + * @param param The GPIO handle. + * @retval None. + */ +void MotorStartStopKeyCallback(void *param) +{ + GPIO_Handle *handle = (GPIO_Handle *)param; + if (Key_StateRead(handle) == KEY_DOWN) { + if (g_mc.motorStateFlag == 0) { /* start motor */ + g_mc.motorStateFlag = 1; + SysCmdStartSet(&g_mc.statusReg); + } else if (g_mc.motorStateFlag == 1) { /* stop motor */ + g_mc.motorStateFlag = 0; + SysCmdStopSet(&g_mc.statusReg); + } + } +} + +/** + * @brief User application main entry function. + * @retval BSP_OK. + */ +int MotorMainProcess(void) +{ + unsigned int tickNum1Ms = 2; /* 1ms tick */ + static unsigned int tickCnt1Ms = 0; + unsigned int tickNum500Ms = 1000; /* 500ms tick */ + static unsigned int tickCnt500Ms = 0; + SystemInit(); + HMI_Init(); /* Init uart interrupt */ + HAL_TIMER_Start(&g_timer0); + HAL_TIMER_Start(&g_timer1); + + /* Disable PWM output before startup. */ + MotorPwmOutputDisable(g_apt); + /* Software initialization. */ + InitSoftware(); + /* Start the PWM clock. */ + HAL_APT_StartModule(RUN_APT0 | RUN_APT1 | RUN_APT2); + /* System Timer clock. */ + BASE_FUNC_DELAY_MS(ADC_READINIT_DELAY); + TrimInitAdcShiftValue(&g_mc); + BASE_FUNC_DELAY_MS(MOTOR_START_DELAY); + while (1) { + /* Cycling send data to host */ + HMI_Process_Tx(&g_mc); + if (g_mc.msTickCnt - tickCnt1Ms >= tickNum1Ms) { + tickCnt1Ms = g_mc.msTickCnt; + /* User Code 1ms Event */ + HMI_Process_Rx(&g_mc); + /* User Code 1ms Event */ + } + + if (g_mc.msTickCnt - tickCnt500Ms >= tickNum500Ms) { + if (SysIsError(&g_mc.statusReg) != true) { + /* LED toggle in normal status. */ + HAL_GPIO_TogglePin(&LED1_HANDLE, LED1_PIN); + } + tickCnt500Ms = g_mc.msTickCnt; + } + } + return 0; +} diff --git a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/user_interface/cust_process.c b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/user_interface/cust_process.c new file mode 100644 index 00000000..7c4f7525 --- /dev/null +++ b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/user_interface/cust_process.c @@ -0,0 +1,718 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file cust_process.c + * @author MCU Algorithm Team + * @brief This file provides functions declaration of cust process interface. + */ + +#include "cust_process.h" +#include "mcs_ctlmode_config.h" +#include "mcs_math_const.h" +#include "mcs_user_config.h" +#include "mcs_assert.h" +#include "main.h" +#include "uart_module.h" +#include "mcs_mtr_param.h" +/* Macro definitions --------------------------------------------------------------------------- */ +/* Constant value. */ +#define CONST_VALUE_60 60.0f /* Constant value 60. */ +#define CONST_VALUE_DIV_1000 0.001f /* Constant value 1/1000. */ + +/* Data array index. */ +#define DATA_SEGMENT_ONE 0 /* First element of the data segment */ +#define DATA_SEGMENT_TWO 1 /* Second element of the data segment */ +#define DATA_SEGMENT_THREE 2 /* Third element of the data segment */ +#define DATA_SEGMENT_FOUR 3 /* Fourth element of the data segment */ +#define CUSTACKCODELEN 10 /* Ack code length */ + +/* Command code. */ +#define SET_PID_KP 0x01 /* Set Pid Kp Command Params */ +#define SET_PID_KI 0x02 /* Set Pid Ki Command Params */ +#define SET_PID_LIMIT 0x03 /* Set Pid limit Command Params */ + +#define SET_SMO1TH_PLL_BDW 0x01 /* Set Smo1th Pll BandWidth Command Params */ +#define SET_SMO1TH_SPDFLITER_FC 0x02 /* Set Smo1th Fc Command Params */ +#define SET_SMO1TH_FILCOMPANGLE 0x03 /* Set Smo1th FillComp Command Params */ + +#define SET_SMO4TH_KD 0x01 /* Set Smo4th Kd Command Params */ +#define SET_SMO4TH_KP 0x02 /* Set Smo4th Kq Command Params */ + +#define SET_SPD_COMMAND_HZ 0x01 /* Set Target Speed Command Params */ +#define SET_SPD_RMG_SLOPE 0x02 /* Set Speed Slope Command Params */ + +#define SET_MAX_ELEC_SPD 0x01 /* Set Max Motor Speed Command Params */ +#define SET_MOTOR_NUM_OF_PAIRS 0x02 /* Set Motor Command Params */ + +#define SET_MOTOR_RES_OF_STATOR 0x01 /* Set Motor Res Command Params */ +#define SET_MOTOR_DAXIS_INDUCTANCE 0x02 /* Set Motor Daxis Inductance Command Params */ +#define SET_MOTOR_QAXIS_INDUCTANCE 0x03 /* Set Motor Qaxis Inductance Command Params */ + +#define SET_SVPWM_VOLTAGE_PER_UNIT 0x01 /* Set Svpwm Voltage Params */ +#define SET_ADC_CURRENT_SAMPLE_COFE 0x02 /* Set Adc coffe Params */ +#define SET_CURRENT_LOOP_CONTROL_PERIOD 0x03 /* Set Current loop period Command Params */ + +#define SET_IF_TARGET_CURRENT_VALUE 0x01 /* Set If Target Params */ +#define SET_INCREMENT_OF_IF_CURRENT 0x02 /* Set If Step Command Params */ +#define SET_SPEED_RING_BEGIN_SPEED 0x03 /* Set If to Smo Start Speed Command Params */ + +static unsigned char ackCode = 0; +static unsigned char g_uartTxBuf[CUSTACKCODELEN] = {0}; + +/** + * @brief Set observer type. + * @param mtrCtrl The motor control handle. + * @param rxData Receive buffer + */ +static void CMDCODE_SetObserverType(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) +{ + /* Get function code. */ + int funcCode = (int)(rxData->data[DATA_SEGMENT_ONE].typeF); + if (funcCode == FOC_OBSERVERTYPE_SMO1TH) { + ackCode = 0X01; + mtrCtrl->obserType = FOC_OBSERVERTYPE_SMO1TH; + CUST_AckCode(g_uartTxBuf, ackCode, mtrCtrl->obserType); + } else if (funcCode == FOC_OBSERVERTYPE_SMO4TH) { + ackCode = 0X02; + mtrCtrl->obserType = FOC_OBSERVERTYPE_SMO4TH; + CUST_AckCode(g_uartTxBuf, ackCode, mtrCtrl->obserType); + } +} + +/** + * @brief Set pid parameter ack code. + * @param funcCode Received data funccode. + */ +static unsigned char SetPidAckCode(int funcCode) +{ + switch (funcCode) { + /* Set current loop D-Axis PID parameter ack code. */ + case FOC_CURDAXISPID_PARAMS: + ackCode = 0xE0; + break; + /* Set current loop Q-Axis PID parameter ack code. */ + case FOC_CURQAXISPID_PARAMS: + ackCode = 0xE3; + break; + /* Set speed loop PID parameter ack code. */ + case FOC_SPDPID_PARAMS: + ackCode = 0xE6; + break; + default: + break; + } + return ackCode; +} + +/** + * @brief Set pid parameters. + * @param pidHandle The pid control handle. + * @param rxData Receive buffer + */ +static void SetPidParams(PID_Handle *pidHandle, CUSTDATATYPE_DEF *rxData) +{ + /* Get function code. */ + int funcCode = (int)(rxData->data[DATA_SEGMENT_ONE].typeF); + /* Get command code. */ + int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); + switch (cmdCode) { + case SET_PID_KP: /* Set the P parameter. */ + PID_SetKp(pidHandle, rxData->data[DATA_SEGMENT_THREE].typeF); + ackCode = SetPidAckCode(funcCode); + CUST_AckCode(g_uartTxBuf, (unsigned char)(ackCode + SET_PID_KP), rxData->data[DATA_SEGMENT_THREE].typeF); + break; + case SET_PID_KI: /* Set the I parameter. */ + PID_SetKi(pidHandle, rxData->data[DATA_SEGMENT_THREE].typeF); + ackCode = SetPidAckCode(funcCode); + CUST_AckCode(g_uartTxBuf, (unsigned char)(ackCode + SET_PID_KI), rxData->data[DATA_SEGMENT_THREE].typeF); + break; + case SET_PID_LIMIT: /* Set the pid limit. */ + PID_SetLimit(pidHandle, rxData->data[DATA_SEGMENT_THREE].typeF); + ackCode = SetPidAckCode(funcCode); + CUST_AckCode(g_uartTxBuf, (unsigned char)(ackCode + SET_PID_LIMIT), rxData->data[DATA_SEGMENT_THREE].typeF); + break; + default: + ackCode = 0X77; + CUST_AckCode(g_uartTxBuf, ackCode, 0); + break; + } +} + +/** + * @brief Set motor pid parameters. + * @param mtrCtrl The motor control handle. + * @param rxData Receive buffer + */ +static void CMDCODE_SetMotorPidParams(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) +{ + /* Get function code. */ + int funcCode = (int)(rxData->data[DATA_SEGMENT_ONE].typeF); + + if (funcCode == FOC_CURDAXISPID_PARAMS) { + SetPidParams(&mtrCtrl->currCtrl.dAxisPi, rxData); /* Set Curr loop Daxis pid params */ + } else if (funcCode == FOC_CURQAXISPID_PARAMS) { + SetPidParams(&mtrCtrl->currCtrl.qAxisPi, rxData); /* Set Curr loop Qaxis pid params */ + mtrCtrl->currCtrl.dAxisPi.upperLimit = mtrCtrl->currCtrl.qAxisPi.upperLimit; + mtrCtrl->currCtrl.dAxisPi.lowerLimit = mtrCtrl->currCtrl.qAxisPi.lowerLimit; + } else if (funcCode == FOC_SPDPID_PARAMS) { + SetPidParams(&mtrCtrl->spdCtrl.spdPi, rxData); /* Set Speed loop params */ + } +} + +/** + * @brief Set first order sliding mode observer parameters. + * @param smoHandle The observer control handle. + * @param rxData Receive buffer + */ +static void SetObserverSmo1thParams(FOSMO_Handle *smoHandle, CUSTDATATYPE_DEF *rxData) +{ + smoHandle->kSmo = rxData->data[DATA_SEGMENT_TWO].typeF; + ackCode = 0X09; + CUST_AckCode(g_uartTxBuf, ackCode, smoHandle->kSmo); +} + +/** + * @brief Set first order sliding mode observer's phase-locked loop parameters. + * @param smoHandle The observer control handle. + * @param rxData Receive buffer + */ +static void SetObserverSmo1thPLLParams(FOSMO_Handle *smoHandle, CUSTDATATYPE_DEF *rxData) +{ + /* Get command code. */ + int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); + float pllBdw = 0.0f; + switch (cmdCode) { + case SET_SMO1TH_PLL_BDW: /* Set the bandwidth. */ + pllBdw = rxData->data[DATA_SEGMENT_THREE].typeF; + smoHandle->pll.pi.kp = 2.0f * pllBdw; /* kp = 2.0f * pllBdw */ + smoHandle->pll.pi.ki = pllBdw * pllBdw; /* ki = pllBdw * pllBdw */ + ackCode = 0X0A; + CUST_AckCode(g_uartTxBuf, ackCode, pllBdw); + break; + case SET_SMO1TH_SPDFLITER_FC: /* Set the cutoff frequency. */ + smoHandle->spdFilter.fc = rxData->data[DATA_SEGMENT_THREE].typeF; + smoHandle->spdFilter.a1 = 1.0f / (1.0f + DOUBLE_PI * smoHandle->spdFilter.fc * CTRL_CURR_PERIOD); + smoHandle->spdFilter.b1 = 1.0f - smoHandle->spdFilter.a1; + ackCode = 0X0B; + CUST_AckCode(g_uartTxBuf, ackCode, smoHandle->spdFilter.fc); + break; + case SET_SMO1TH_FILCOMPANGLE: /* Set the compensation angle. */ + smoHandle->filCompAngle = rxData->data[DATA_SEGMENT_THREE].typeF; + ackCode = 0X0C; + CUST_AckCode(g_uartTxBuf, ackCode, smoHandle->filCompAngle); + break; + default: + ackCode = 0X77; + CUST_AckCode(g_uartTxBuf, ackCode, 0); + break; + } +} + +/** + * @brief Set fourth order sliding mode observer parameters. + * @param smo4thHandle The observer control handle. + * @param rxData Receive buffer + */ +static void SetObserverSmo4thParams(SMO4TH_Handle *smo4thHandle, CUSTDATATYPE_DEF *rxData) +{ + /* Get command code. */ + int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); + + switch (cmdCode) { + case SET_SMO4TH_KD: /* Set d axis gain. */ + smo4thHandle->kd = rxData->data[DATA_SEGMENT_THREE].typeF; + ackCode = 0X0D; + CUST_AckCode(g_uartTxBuf, ackCode, smo4thHandle->kd); + break; + case SET_SMO4TH_KP: /* Set q axis gain. */ + smo4thHandle->kq = rxData->data[DATA_SEGMENT_THREE].typeF; + ackCode = 0X0E; + CUST_AckCode(g_uartTxBuf, ackCode, smo4thHandle->kq); + break; + default: + break; + } +} + +/** + * @brief Set fourth order sliding mode observer's phase-locked loop parameters. + * @param smo4thHandle The observer control handle. + * @param rxData Receive buffer + */ +static void SetObserverSmo4thPLLParams(SMO4TH_Handle *smo4thHandle, CUSTDATATYPE_DEF *rxData) +{ + float pllBdw = rxData->data[DATA_SEGMENT_TWO].typeF; + smo4thHandle->pll.pi.kp = (2.0f) * pllBdw; /* kp = 2.0f * pllBdw */ + smo4thHandle->pll.pi.ki = pllBdw * pllBdw; + ackCode = 0X11; + CUST_AckCode(g_uartTxBuf, ackCode, pllBdw); +} + +/** + * @brief Set observer parameters. + * @param mtrCtrl The motor control handle. + * @param rxData Receive buffer + */ +static void CMDCODE_SetObserverParams(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) +{ + /* Get function code. */ + int funcCode = (int)(rxData->data[DATA_SEGMENT_ONE].typeF); + + if (funcCode == FOC_OBSERVERTYPE_SMO1TH) { + SetObserverSmo1thParams(&mtrCtrl->smo, rxData); + } else if (funcCode == FOC_OBSERVERTYPE_SMO1TH_PLL) { + SetObserverSmo1thPLLParams(&mtrCtrl->smo, rxData); + } else if (funcCode == FOC_OBSERVERTYPE_SMO4TH) { + SetObserverSmo4thParams(&mtrCtrl->smo4th, rxData); + } else if (funcCode == FOC_OBSERVERTYPE_SMO4TH_PLL) { + SetObserverSmo4thPLLParams(&mtrCtrl->smo4th, rxData); + } +} + +/** + * @brief Set motor speed and speed slope. + * @param mtrCtrl The motor control handle. + * @param rxData Receive buffer + */ +static void CMDCODE_SetMotorSpdAndSlope(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) +{ + /* Get command code. */ + int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); + + switch (cmdCode) { + case SET_SPD_COMMAND_HZ: /* Set target speed(hz). */ + mtrCtrl->spdCmdHz = rxData->data[DATA_SEGMENT_THREE].typeF * mtrCtrl->mtrParam.mtrNp / CONST_VALUE_60; + /* Judgement the value > 0.00001, make sure denominator != 0. */ + if (rxData->data[DATA_SEGMENT_FOUR].typeF > 0.00001f) { + mtrCtrl->spdRmg.delta = mtrCtrl->spdCmdHz / rxData->data[DATA_SEGMENT_FOUR].typeF * CTRL_SYSTICK_PERIOD; + } + ackCode = 0X16; + CUST_AckCode(g_uartTxBuf, ackCode, mtrCtrl->spdCmdHz); + break; + case SET_SPD_RMG_SLOPE: /* Set speed slope. */ + mtrCtrl->spdRmg.delta = mtrCtrl->spdCmdHz / rxData->data[DATA_SEGMENT_THREE].typeF * CTRL_SYSTICK_PERIOD; + ackCode = 0X17; + CUST_AckCode(g_uartTxBuf, ackCode, rxData->data[DATA_SEGMENT_THREE].typeF); + break; + default: + break; + } +} + +/** + * @brief Set motor base parameters. + * @param mtrParamHandle The motor parameters handle. + * @param rxData Receive buffer + */ +static void SetMotorBaseParams(MOTOR_Param *mtrParamHandle, CUSTDATATYPE_DEF *rxData) +{ + /* Get command code. */ + int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); + + switch (cmdCode) { + case SET_MAX_ELEC_SPD: /* Set max electric speed. */ + mtrParamHandle->maxElecSpd = rxData->data[DATA_SEGMENT_THREE].typeF / + CONST_VALUE_60 * mtrParamHandle->mtrNp; + ackCode = 0X18; + CUST_AckCode(g_uartTxBuf, ackCode, rxData->data[DATA_SEGMENT_THREE].typeF); + break; + case SET_MOTOR_NUM_OF_PAIRS: /* Set the number of motor pole pairs. */ + mtrParamHandle->mtrNp = (unsigned short)(rxData->data[DATA_SEGMENT_THREE].typeF); + ackCode = 0X19; + CUST_AckCode(g_uartTxBuf, ackCode, (float)mtrParamHandle->mtrNp); + break; + default: + break; + } +} + +/** + * @brief Set motor special parameters. + * @param mtrParamHandle The motor parameters handle. + * @param rxData Receive buffer + */ +static void SetMotorSpecialParams(MOTOR_Param *mtrParamHandle, CUSTDATATYPE_DEF *rxData) +{ + /* Get command code. */ + int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); + + switch (cmdCode) { + case SET_MOTOR_RES_OF_STATOR: /* Set the resistor of stator. */ + mtrParamHandle->mtrRs = rxData->data[DATA_SEGMENT_THREE].typeF; + ackCode = 0X1A; + CUST_AckCode(g_uartTxBuf, ackCode, mtrParamHandle->mtrRs); + break; + case SET_MOTOR_DAXIS_INDUCTANCE: /* Set the d axis inductance. */ + mtrParamHandle->mtrLd = rxData->data[DATA_SEGMENT_THREE].typeF; + ackCode = 0X1B; + CUST_AckCode(g_uartTxBuf, ackCode, mtrParamHandle->mtrLd); + break; + case SET_MOTOR_QAXIS_INDUCTANCE: /* Set the q axis inductance. */ + mtrParamHandle->mtrLq = rxData->data[DATA_SEGMENT_THREE].typeF; + ackCode = 0X1C; + CUST_AckCode(g_uartTxBuf, ackCode, mtrParamHandle->mtrLq); + break; + default: + ackCode = 0X77; + CUST_AckCode(g_uartTxBuf, ackCode, 0); + break; + } +} + +/** + * @brief Set motor board parameters. + * @param mtrCtrl The motor control handle. + * @param rxData Receive buffer + */ +static void SetMotorBoardParams(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) +{ + /* Get command code. */ + int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); + + switch (cmdCode) { + case SET_SVPWM_VOLTAGE_PER_UNIT: /* Set svpwm voltage per unit. */ + mtrCtrl->sv.voltPu = rxData->data[DATA_SEGMENT_THREE].typeF * ONE_DIV_SQRT3; + mtrCtrl->currCtrl.outLimit = mtrCtrl->sv.voltPu * ONE_DIV_SQRT3; + ackCode = 0X1D; + CUST_AckCode(g_uartTxBuf, ackCode, rxData->data[DATA_SEGMENT_THREE].typeF); + break; + case SET_ADC_CURRENT_SAMPLE_COFE: /* Set adc current sample cofeature. */ + mtrCtrl->adcCurrCofe = rxData->data[DATA_SEGMENT_THREE].typeF; + ackCode = 0X1E; + CUST_AckCode(g_uartTxBuf, ackCode, mtrCtrl->adcCurrCofe); + break; + case SET_CURRENT_LOOP_CONTROL_PERIOD: /* Set current loop control period. */ + mtrCtrl->currCtrlPeriod = 1 / rxData->data[DATA_SEGMENT_THREE].typeF * CONST_VALUE_DIV_1000; + ackCode = 0X1F; + CUST_AckCode(g_uartTxBuf, ackCode, rxData->data[DATA_SEGMENT_THREE].typeF); + break; + default: + break; + } +} + +/** + * @brief Set motor parameters. + * @param mtrCtrl The motor control handle. + * @param rxData Receive buffer + */ +static void CMDCODE_SetMotorParams(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) +{ + /* Get function code. */ + int funcCode = (int)(rxData->data[DATA_SEGMENT_ONE].typeF); + + if (funcCode == MOTOR_PARAMS_BASE) { + SetMotorBaseParams(&mtrCtrl->mtrParam, rxData); + } else if (funcCode == MOTOR_PARAMS_SPECIAL) { + SetMotorSpecialParams(&mtrCtrl->mtrParam, rxData); + } else if (funcCode == MOTOR_PARAMS_BOARD) { + SetMotorBoardParams(mtrCtrl, rxData); + } +} + +/** + * @brief Motor start. + * @param mtrCtrl The motor control handle. + */ +static void CMDCODE_MotorStart(MTRCTRL_Handle *mtrCtrl) +{ + if (mtrCtrl->stateMachine != FSM_RUN) { + SysCmdStartSet(&mtrCtrl->statusReg); /* start motor. */ + mtrCtrl->motorStateFlag = 1; + ackCode = 0X24; /* send ackcode to host. */ + CUST_AckCode(g_uartTxBuf, ackCode, 1); + } +} + +/** + * @brief Motor stop. + * @param mtrCtrl The motor control handle. + */ +static void CMDCODE_MotorStop(MTRCTRL_Handle *mtrCtrl) +{ + SysCmdStopSet(&mtrCtrl->statusReg); + mtrCtrl->motorStateFlag = 0; + ackCode = 0X25; + CUST_AckCode(g_uartTxBuf, ackCode, 0); +} + +/** + * @brief Motor state reset. + * @param mtrCtrl The motor control handle. + */ +static void CMDCODE_MotorReset(MTRCTRL_Handle *mtrCtrl) +{ + BASE_FUNC_UNUSED(mtrCtrl); + BASE_FUNC_SoftReset(); +} + +/** + * @brief Set IF-Startup parameters. + * @param mtrCtrl The motor control handle. + * @param rxData Receive buffer + */ +static void SetStartupIFParams(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) +{ + /* Get command code. */ + int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); + + switch (cmdCode) { + case SET_IF_TARGET_CURRENT_VALUE: /* Set I/F start up target current value. */ + mtrCtrl->ifCtrl.targetAmp = rxData->data[DATA_SEGMENT_THREE].typeF; + ackCode = 0X26; + CUST_AckCode(g_uartTxBuf, ackCode, mtrCtrl->ifCtrl.targetAmp); + break; + case SET_INCREMENT_OF_IF_CURRENT: /* Set increment of I/F start up current. */ + mtrCtrl->ifCtrl.stepAmp = mtrCtrl->ifCtrl.targetAmp / rxData->data[DATA_SEGMENT_THREE].typeF * + CTRL_SYSTICK_PERIOD; + ackCode = 0X27; + CUST_AckCode(g_uartTxBuf, ackCode, mtrCtrl->ifCtrl.stepAmp); + break; + case SET_SPEED_RING_BEGIN_SPEED: /* Set speed ring begin speed. */ + mtrCtrl->startup.spdBegin = rxData->data[DATA_SEGMENT_THREE].typeF / + CONST_VALUE_60 * mtrCtrl->mtrParam.mtrNp; + ackCode = 0X28; + CUST_AckCode(g_uartTxBuf, ackCode, rxData->data[DATA_SEGMENT_THREE].typeF); + break; + default: + break; + } +} + +/** + * @brief Set start up parameters. + * @param mtrCtrl The motor control handle. + * @param rxData Receive buffer + */ +static void CMDCODE_SetStartupParams(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) +{ + /* Get function code. */ + int funcCode = (int)(rxData->data[DATA_SEGMENT_ONE].typeF); + + if (funcCode == FOC_STARTUP_IF) { + SetStartupIFParams(mtrCtrl, rxData); + } +} + +/** + * @brief Set adjust speed mode. + * @param mtrCtrl The motor control handle. + * @param rxData Receive buffer + */ +static void CMDCODE_SetAdjustSpdMode(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) +{ + /* Get function code. */ + int funcCode = (int)(rxData->data[DATA_SEGMENT_ONE].typeF); + /* Get commond code. */ + int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); + if (funcCode == HOST_SPEED_ADJUST) { + mtrCtrl->spdAdjustMode = HOST_SPEED_ADJUST; + /* Uart connect success. */ + mtrCtrl->uartConnectFlag = CONNECTING; + ackCode = 0X2A; + CUST_AckCode(g_uartTxBuf, ackCode, (float)mtrCtrl->spdAdjustMode); + } else if (funcCode == CUST_SPEED_ADJUST) { + if (cmdCode == 1) { /* If uart connection disconnected & stop motor commond. */ + SysCmdStopSet(&mtrCtrl->statusReg); + mtrCtrl->motorStateFlag = 0; + } + mtrCtrl->spdAdjustMode = CUST_SPEED_ADJUST; + ackCode = 0X2B; + CUST_AckCode(g_uartTxBuf, ackCode, (float)mtrCtrl->spdAdjustMode); + /* Uart disconnect. */ + mtrCtrl->uartConnectFlag = DISCONNECT; + } +} + +/** + * @brief Check uart connect. + * @param mtrCtrl The motor control handle. + * @param rxData Receive buffer + */ +static void CMDCODE_UartHandShake(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) +{ + CMDCODE_SetAdjustSpdMode(mtrCtrl, rxData); +} + +/** + * @brief Set Motor Initial Status Parameters. + * @param mtrCtrl The motor control handle. + * @param rxData Receive buffer + * @param code Instruction code. + */ +static void CMDCODE_EXE_SetMotorInitParams(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData, unsigned char code) +{ + switch (code) { + case CMDCODE_SET_OBSERVER_TYPE: { /* Set observer type. */ + CMDCODE_SetObserverType(mtrCtrl, rxData); + } + break; + case CMDCODE_SET_PID_PARAMS: { /* Set motor pid parameters. */ + CMDCODE_SetMotorPidParams(mtrCtrl, rxData); + } + break; + case CMDCODE_SET_OBSERVER_PARAMS: { /* Set observer parameters. */ + CMDCODE_SetObserverParams(mtrCtrl, rxData); + } + break; + case CMDCODE_SET_MOTOR_TARGETSPD: { /* Set motor speed and speed slope. */ + CMDCODE_SetMotorSpdAndSlope(mtrCtrl, rxData); + } + break; + case CMDCODE_SET_MOTOR_PARAMS: { /* Set motor parameters. */ + CMDCODE_SetMotorParams(mtrCtrl, rxData); + } + break; + case CMDCODE_SET_ADJUSTSPD_MODE: /* Set adjust speed mode. */ + CMDCODE_SetAdjustSpdMode(mtrCtrl, rxData); + break; + default: + break; + } +} + +/** + * @brief Set Motor Control System Status. + * @param mtrCtrl The motor control handle. + * @param rxData Receive buffer + * @param code Instruction code. + */ +static void CMDCODE_EXE_SetMotorState(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData, unsigned char code) +{ + BASE_FUNC_UNUSED(rxData); + switch (code) { + case CMDCODE_MOTOR_START: { /* Motor start command */ + CMDCODE_MotorStart(mtrCtrl); + } + break; + case CMDCODE_MOTOR_STOP: { /* Motor stop command */ + CMDCODE_MotorStop(mtrCtrl); + } + break; + case CMDCODE_MOTORSTATE_RESET: { /* Motor reset command */ + CMDCODE_MotorReset(mtrCtrl); + } + break; + default: + break; + } +} + +/** + * @brief Set Startup and Uart Link Handshake Flag Parameters. + * @param mtrCtrl The motor control handle. + * @param rxData Receive buffer + * @param code Instruction code. + */ +static void CMDCODE_EXE_SetOtherParams(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData, unsigned char code) +{ + switch (code) { + case CMDCODE_SET_STARTUP_PARAMS: /* Set start up parameters. */ + CMDCODE_SetStartupParams(mtrCtrl, rxData); + break; + case CMDCODE_UART_HANDSHAKE: { /* Check uart hand shake. */ + CMDCODE_UartHandShake(mtrCtrl, rxData); + } + break; + case CMDCODE_UART_HEARTDETECT: { /* Check uart hand shake. */ + mtrCtrl->uartHeartDetCnt++; + } + break; + default: + break; + } +} +/** + * @brief Instruction code executor. + * @param mtrCtrl The motor control handle. + * @param rxData Receive buffer + * @param code Instruction code. + */ +static void CMDCODE_EXE_Process(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData, unsigned char code) +{ + CMDCODE_EXE_SetMotorInitParams(mtrCtrl, rxData, code); + CMDCODE_EXE_SetMotorState(mtrCtrl, rxData, code); + CMDCODE_EXE_SetOtherParams(mtrCtrl, rxData, code); +} + +/** + * @brief Host data download callback and data parsing. + * @param mtrCtrl The motor control handle. + * @param rxBuf Receive buffer + */ +void CUST_UartDataProcess(MTRCTRL_Handle *mtrCtrl, unsigned char *rxBuf) +{ + /* Verify Parameters */ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + MCS_ASSERT_PARAM(rxBuf != NULL); + + /* Uart data storage struct */ + CUSTDATATYPE_DEF data; + volatile unsigned char *ptrCnt = &rxBuf[FRAME_CHECK_BEGIN + 1]; + volatile unsigned char *strCnt = &data.data[0].typeCh[0]; + for (unsigned int j = 0; j < FRAME_RECV_DATA_LENTH * FRAME_ONE_DATA_LENTH; j++) { + *strCnt++ = *ptrCnt++; + } + /* Message function code. */ + data.code = rxBuf[FRAME_CHECK_BEGIN]; + CMDCODE_EXE_Process(mtrCtrl, &data, data.code); +} + +/** + * @brief The host computer displays data transmission. + * @param mtrCtrl The motor control handle. + * @param txData Message content. + * @param stage Message status function code. + */ +void CUST_SetTxMsg(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *txData) +{ + /* Verify Parameters */ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + MCS_ASSERT_PARAM(txData != NULL); + if (mtrCtrl->stateMachine == FSM_IDLE) { + mtrCtrl->smo.spdEst = 0.0f; + } + /* Data send to host. */ + txData->data[CURRDQ_Q].typeF = mtrCtrl->idqFbk.q; + txData->data[CURRDQ_D].typeF = mtrCtrl->idqFbk.d; + txData->data[CURRREFDQ_Q].typeF = mtrCtrl->idqRef.q; + txData->data[CURRREFDQ_D].typeF = mtrCtrl->idqRef.d; + /* Motor current speed. */ + txData->data[CURRSPD].typeF = mtrCtrl->smo.spdEst * CONST_VALUE_60 / mtrCtrl->mtrParam.mtrNp; + /* Motor commond speed. */ + txData->data[SPDCMDHZ].typeF = mtrCtrl->spdCmdHz * CONST_VALUE_60 / mtrCtrl->mtrParam.mtrNp; + /* Bus voltage. */ + txData->data[UDC].typeF = mtrCtrl->udc; + /* Power board temprature. */ + txData->data[POWERBOARDTEMP].typeF = mtrCtrl->powerBoardTemp; + /* Motor protection status flag. */ + txData->data[CUST_ERR_CODE].typeI = mtrCtrl->prot.motorErrStatus.all; + /* Three phase current. */ + txData->data[CURRUVW_U].typeF = mtrCtrl->currUvw.u; + txData->data[CURRUVW_V].typeF = mtrCtrl->currUvw.v; + txData->data[CURRUVW_W].typeF = mtrCtrl->currUvw.w; + /* Three phase pwm duty. */ + txData->data[PWMDUTYUVW_U].typeF = mtrCtrl->dutyUvw.u; + txData->data[PWMDUTYUVW_V].typeF = mtrCtrl->dutyUvw.v; + txData->data[PWMDUTYUVW_W].typeF = mtrCtrl->dutyUvw.w; + /* Motor electric angle. */ + txData->data[AXISANGLE].typeF = mtrCtrl->axisAngle; + txData->data[VDQ_Q].typeF = mtrCtrl->vdqRef.q; + txData->data[VDQ_D].typeF = mtrCtrl->vdqRef.d; + txData->data[SPDREFHZ].typeF = mtrCtrl->spdRefHz * CONST_VALUE_60 / mtrCtrl->mtrParam.mtrNp; + txData->data[SENDTIMESTAMP].typeF = mtrCtrl->uartTimeStamp; +} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/user_interface/cust_process.h b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/user_interface/cust_process.h new file mode 100644 index 00000000..60fe69bb --- /dev/null +++ b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/user_interface/cust_process.h @@ -0,0 +1,36 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file cust_process.h + * @author MCU Algorithm Team + * @brief This file provides functions declaration of system status. + */ + +/* Define to prevent recursive inclusion ------------------------------------------------------- */ +#ifndef McsMagicTag_HMI_MOUDLE_H +#define McsMagicTag_HMI_MOUDLE_H + +/* Includes ------------------------------------------------------------------------------------ */ +#include "protocol.h" +/* Typedef definitions ------------------------------------------------------------------------- */ + + +void CUST_UartDataProcess(MTRCTRL_Handle *mtrCtrl, unsigned char *rxBuf); + +void CUST_SetTxMsg(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *txData); + +#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/user_interface/hmi_module.c b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/user_interface/hmi_module.c new file mode 100644 index 00000000..58bf15a7 --- /dev/null +++ b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/user_interface/hmi_module.c @@ -0,0 +1,57 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file hmi_module.c + * @author MCU Algorithm Team + * @brief This file provides functions declaration of human-machine interface. + */ + +#include "hmi_module.h" +#include "mcs_assert.h" + +/** + * @brief HMI Initializatio. + * @retval None. + */ +void HMI_Init(void) +{ + UartRecvInit(); +} + +/** + * @brief HMI processing. + * @param mtrCtrl The motor control handle.. + * @retval None. + */ +void HMI_Process_Rx(MTRCTRL_Handle *mtrCtrl) +{ + /* Verify Parameters */ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + UartModuleProcess_Rx(mtrCtrl); +} + +/** + * @brief HMI processing. + * @param mtrCtrl The motor control handle.. + * @retval None. + */ +void HMI_Process_Tx(MTRCTRL_Handle *mtrCtrl) +{ + /* Verify Parameters */ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + UartModuleProcess_Tx(mtrCtrl); +} diff --git a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/user_interface/hmi_module.h b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/user_interface/hmi_module.h new file mode 100644 index 00000000..056f8d73 --- /dev/null +++ b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/user_interface/hmi_module.h @@ -0,0 +1,37 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file hmi_module.h + * @author MCU Algorithm Team + * @brief This file provides functions declaration of system status. + */ + +/* Define to prevent recursive inclusion ------------------------------------------------------- */ +#ifndef McsMagicTag_HMI_MOUDLE_H +#define McsMagicTag_HMI_MOUDLE_H + +/* Includes ------------------------------------------------------------------------------------ */ +#include "uart_module.h" +/* Typedef definitions ------------------------------------------------------------------------- */ + + +void HMI_Init(void); + +void HMI_Process_Rx(MTRCTRL_Handle *mtrCtrl); +void HMI_Process_Tx(MTRCTRL_Handle *mtrCtrl); + +#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/user_interface/protocol.c b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/user_interface/protocol.c new file mode 100644 index 00000000..56f0c0bb --- /dev/null +++ b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/user_interface/protocol.c @@ -0,0 +1,174 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file protocol.c + * @author MCU Algorithm Team + * @brief This file provides functions declaration of port communication. + */ + +#include "protocol.h" +#include "apt.h" +#include "typedefs.h" +#include "main.h" +#include "mcs_assert.h" +#include "cust_process.h" + +/** + * @brief Callback function for receiving data analysis and processing. + * @param rxBuf Receive buffer. + */ +__weak void CUST_UartDataProcess(MTRCTRL_Handle *mtrCtrl, unsigned char *rxBuf) +{ + BASE_FUNC_UNUSED(mtrCtrl); + BASE_FUNC_UNUSED(rxBuf); +} +/** + * @brief User-defined protocol message sending function (weak function). + * @param rxData Sending Messages.. + */ +__weak void CUST_SetTxMsg(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *txData) +{ + BASE_FUNC_UNUSED(mtrCtrl); + BASE_FUNC_UNUSED(*txData); +} + +static void (*g_ptrDataProcess)(MTRCTRL_Handle *mtrCtrl, unsigned char *rxBuf) = CUST_UartDataProcess; + +/** + * @brief Frame checksum. + * @param ptr Pointer to the data to be checked + * @param num Number of bytes + * @retval unsigned char Checksum + */ +static unsigned char CheckSum(unsigned char *ptr, unsigned char num) +{ + unsigned char sum = 0; + unsigned char *p = ptr; + /* Calculate the sum of received data. */ + for (unsigned char i = 0; i < num; i++) { + sum += (unsigned char)*p; + p++; + } + return sum; +} + +/** + * @brief Transmitting Data Frames. + * @param mtrCtrl The motor control handle. + * @param txBuf Sending Messages. + */ +unsigned int CUST_TransmitData(MTRCTRL_Handle *mtrCtrl, unsigned char *txBuf) +{ + /* Verify Parameters */ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + MCS_ASSERT_PARAM(txBuf != NULL); + unsigned int dataLen = FRAME_ONE_DATA_LENTH * SEND_FRAME_DATA_NUM; + unsigned char i = 0; + CUSTDATATYPE_DEF txData = {0}; + CUST_SetTxMsg(mtrCtrl, &txData); + txBuf[i++] = FRAME_START; + /* Message function code */ + txBuf[i++] = FRAME_SENT; + /* Message data */ + for (unsigned char x = 0; x < SEND_FRAME_DATA_NUM; x++) { + unsigned char floatIndex = 0; + unsigned char byteOffset = i; + txBuf[x * FRAME_ONE_DATA_LENTH + byteOffset++] = txData.data[x].typeCh[floatIndex++]; + txBuf[x * FRAME_ONE_DATA_LENTH + byteOffset++] = txData.data[x].typeCh[floatIndex++]; + txBuf[x * FRAME_ONE_DATA_LENTH + byteOffset++] = txData.data[x].typeCh[floatIndex++]; + txBuf[x * FRAME_ONE_DATA_LENTH + byteOffset++] = txData.data[x].typeCh[floatIndex++]; + } + /* Message verification domain */ + txBuf[dataLen + i++] = CheckSum((unsigned char*)&txBuf[FRAME_CHECK_BEGIN], dataLen + 1); + /* End of Message */ + txBuf[dataLen + i++] = FRAME_END; + return dataLen + i; +} + +/** + * @brief Transmitting Data Frames. + * @param txBuf Sending Cust Ack Code. + * @param ackCode Ack Code. + * @param varParams Host set parameter. + */ +void CUST_AckCode(unsigned char *txBuf, unsigned char ackCode, float varParams) +{ + /* Verify Parameters */ + MCS_ASSERT_PARAM(txBuf != NULL); + CUSTDATATYPE_DEF txData = {0}; + int dataIndex = 0; + unsigned int i = 0; + unsigned int txLen = 0; + unsigned char dataLen = FRAME_ONE_CHAR_LENTH + FRAME_ONE_DATA_LENTH; + + txData.data[0].typeF = varParams; + txBuf[i++] = FRAME_START; + /* Message function code */ + txBuf[i++] = FRAME_CUSTACK; + /* Message ack code */ + txBuf[i++] = ackCode; + /* Message data */ + txBuf[i++] = txData.data[0].typeCh[dataIndex++]; + txBuf[i++] = txData.data[0].typeCh[dataIndex++]; + txBuf[i++] = txData.data[0].typeCh[dataIndex++]; + txBuf[i++] = txData.data[0].typeCh[dataIndex++]; + + /* Message verification domain */ + txBuf[FRAME_ONE_CHAR_LENTH + i++] = CheckSum((unsigned char*)&txBuf[FRAME_CHECK_BEGIN], dataLen + 1); + /* End of Message */ + txBuf[FRAME_ONE_CHAR_LENTH + i++] = FRAME_END; + txLen = FRAME_ONE_CHAR_LENTH + i++; + HAL_UART_WriteDMA(&g_uart0, txBuf, txLen); +} + +/** + * @brief Cust receive data process. + * @param mtrCtrl The motor control handle. + * @param rxBuf Receive buffer + */ +void CUST_DataReceProcss(MTRCTRL_Handle *mtrCtrl, unsigned char *rxBuf) +{ + /* Verify Parameters */ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + MCS_ASSERT_PARAM(rxBuf != NULL); + unsigned char g_uartTxBuf[10] = {0}; + unsigned char ackCode = 0; + /* Frame header check */ + if (rxBuf[0] != FRAME_START) { + ackCode = 0X78; + CUST_AckCode(g_uartTxBuf, ackCode, 0); + return; + } + /* Frame trailer check */ + if (rxBuf[FRAME_LENTH - 1] != FRAME_END) { + ackCode = 0X79; + CUST_AckCode(g_uartTxBuf, ackCode, 0); + return; + } + /* Checksum */ + if (CheckSum((unsigned char*)&rxBuf[FRAME_CHECK_BEGIN], FRAME_CHECK_NUM) != rxBuf[FRAME_CHECKSUM]) { + ackCode = 0X7A; + CUST_AckCode(g_uartTxBuf, ackCode, 0); + return; + } else { + if (g_ptrDataProcess == NULL) { + return; + } else { + g_ptrDataProcess(mtrCtrl, rxBuf); + } + } +} diff --git a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/user_interface/protocol.h b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/user_interface/protocol.h new file mode 100644 index 00000000..900a00d8 --- /dev/null +++ b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/user_interface/protocol.h @@ -0,0 +1,121 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file protocol.h + * @author MCU Algorithm Team + * @brief This file provides functions declaration of port communication. + */ +#ifndef McsMagicTag_PORTOCOL_H +#define McsMagicTag_PORTOCOL_H + +#include "uart.h" +#include "mcs_carrier.h" + +#define RS485_SEND_ENABLE GPIO6->GPIO_DATA[GPIO_PIN_7].reg = GPIO_PIN_7 +#define RS485_SEND_DISABLE GPIO6->GPIO_DATA[GPIO_PIN_7].reg = BASE_CFG_UNSET + +#define RX_BUF_LEN (16) +#define SEND_FRAME_DATA_NUM (CUSTDATA_MAX) +/* Service Uart0 Communication Deal */ +#define FRAME_ONE_DATA_LENTH 4 +#define FRAME_ONE_CHAR_LENTH 1 +#define FRAME_RECV_DATA_LENTH 4 +#define FRAME_LENTH 20 /* Data length */ +#define FRAME_SENT 0X8F +#define FRAME_CUSTACK 0X8A +#define FRAME_START 0x0F /* Start frame */ +#define FRAME_END '/' /* StOP frame */ +#define FRAME_CHECK_BEGIN 1 /* Check frame */ +#define FRAME_CHECKSUM 18 /* Check sum */ +#define FRAME_CHECK_NUM 17 +#define CMDCODE_IDLE_FRAME 0x55 /* Fill frame */ +#define CMDCODE_GET_MOTOR_PARAMS 0x01 +#define CMDCODE_SEND_MOTOR_PARAMS 0x02 +#define CMDCODE_SET_MOTOR_CTLMODE 0x03 +#define CMDCODE_SET_OBSERVER_TYPE 0x04 +#define CMDCODE_SET_STARTUP_MODE 0x05 +#define CMDCODE_SET_PID_PARAMS 0x06 +#define CMDCODE_SET_STARTUP_PARAMS 0x07 +#define CMDCODE_SET_OBSERVER_PARAMS 0x08 +#define CMDCODE_SET_MOTOR_TARGETSPD 0x09 +#define CMDCODE_SET_MOTOR_PARAMS 0x0A +#define CMDCODE_MOTOR_START 0x0B +#define CMDCODE_MOTOR_STOP 0x0C +#define CMDCODE_MOTORSTATE_RESET 0x0D +#define CMDCODE_SEND_FIRMVERSION 0x0E +#define CMDCODE_SET_ADJUSTSPD_MODE 0x11 +#define CMDCODE_UART_HANDSHAKE 0x12 +#define CMDCODE_UART_HEARTDETECT 0x13 + +typedef union { + unsigned char typeCh[4]; + float typeF; + int typeI; +} UNIONDATATYPE_DEF; + +typedef enum { + OFFLINE_RES = 0, + OFFLINE_LD, + OFFLINE_LQ, + OFFLINE_PSIF, + OFFLINE_JS, + OFFLINE_NP, + OFFLINE_B, + OFFLINE_KPD, + OFFLINE_KID, + OFFLINE_KPQ, + OFFLINE_KIQ, + OFFLINE_KPS, + OFFLINE_KIS, + OFFLINE_SPEED, + OFLINE_MAX +} OFFLINE_IDEN_TYPE; + +typedef enum { + CURRDQ_Q = 0, + CURRDQ_D, + CURRREFDQ_Q, + CURRREFDQ_D, + CURRSPD, + SPDCMDHZ, + UDC, + POWERBOARDTEMP, + CUST_ERR_CODE, + CURRUVW_U, + CURRUVW_V, + CURRUVW_W, + PWMDUTYUVW_U, + PWMDUTYUVW_V, + PWMDUTYUVW_W, + AXISANGLE, + VDQ_Q, + VDQ_D, + SPDREFHZ, + SENDTIMESTAMP, + CUSTDATA_MAX +} SENDTOHOSTPARAMS; + +typedef struct { + volatile unsigned char code; + volatile UNIONDATATYPE_DEF data[SEND_FRAME_DATA_NUM]; +} CUSTDATATYPE_DEF; + + +void CUST_DataReceProcss(MTRCTRL_Handle *mtrCtrl, unsigned char *rxBuf); +unsigned int CUST_TransmitData(MTRCTRL_Handle *mtrCtrl, unsigned char *txBuf); +void CUST_AckCode(unsigned char *txBuf, unsigned char ackCode, float varParams); +#endif /* McsMagicTag_PORTOCOL_H */ diff --git a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/user_interface/uart_module.c b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/user_interface/uart_module.c new file mode 100644 index 00000000..df5e9186 --- /dev/null +++ b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/user_interface/uart_module.c @@ -0,0 +1,184 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the + * following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file uart_module.c + * @author MCU Algorithm Team + * @brief This file provides functions declaration of Serial port communication. + */ +#include "uart_module.h" +#include "debug.h" +#include "main.h" +#include "baseinc.h" + +/* Buffer size */ +#define UI_TX_BUF_LEN (96) +#define UI_RX_BUF_LEN (96) + +/* Receiving Timeout Interval */ +#define UART_TIME_OUT_MS (100) + +/* Start sending data to host delay after uart connect success */ +#define UART_UPDATA_DELAY_TIME_MS (50) + +/* Data buffer */ +unsigned char g_uartRxBuf[UI_RX_BUF_LEN] = {0}; +unsigned char g_uartTxBuf[UI_TX_BUF_LEN] = {0}; +static unsigned int getdeltaSystickCnt = 0; +static FRAME_Handle g_uartFrame; +/** + * @brief Receive Data Clear. + * @param uartFrame Receice Data. + * @retval None. + */ +static void FrameRecvClear(FRAME_Handle *uartFrame) +{ + /* Clear buffer lenth. */ + uartFrame->buffLen = 0; + uartFrame->timeOutCnt = 0; + uartFrame->frameFlag = 0; + /* Clear received data lenth. */ + uartFrame->rxLen = 0; + /* Clear received flag. */ + uartFrame->rxFlag = 0; + uartFrame->upDataCnt = 0; + uartFrame->rxAckFlag = 0; +} + +/** + * @brief Set Dma status. + * @param mtrCtrl The motor control handle. + * @retval None. + */ +static void SetUartDmaStatus(MTRCTRL_Handle *mtrCtrl) +{ + /* Delay 50ms start uart Tx DMA . */ + if (mtrCtrl->uartConnectFlag == CONNECTING && g_uartFrame.upDataDelayCnt++ > UART_UPDATA_DELAY_TIME_MS) { + g_uartFrame.txFlag = 1; /* Start send data flag. */ + mtrCtrl->uartConnectFlag = CONNECTED; + g_uartFrame.upDataDelayCnt = 0; + } + if (mtrCtrl->uartConnectFlag == DISCONNECT) { + g_uartFrame.txFlag = 0; /* Stop send data flag. */ + mtrCtrl->uartTimeStamp = 0; + } +} + +/** + * @brief Uart Dma interupt callback func. + * @param handle Uart handle. + * @retval None. + */ +void UART0_TXDMACallback(void *handle) +{ + BASE_FUNC_UNUSED(handle); + unsigned int getCurSystickCnt = 0; + static unsigned int getlastSystickCnt = 0; + /* USER CODE BEGIN UART0_WRITE_DMA_FINISH */ + g_uartFrame.txFlag = 1; + /* Received information answered, information update reserved. */ + if (g_uartFrame.rxAckFlag == 1) { + g_uartFrame.uartItTxFlag = 1; + g_uartFrame.rxAckFlag = 0; + } + getCurSystickCnt = DCL_SYSTICK_GetTick(); + if (getlastSystickCnt != 0) { + /* Calculate unit frame data send time */ + getdeltaSystickCnt = getCurSystickCnt - getlastSystickCnt; + } + getlastSystickCnt = getCurSystickCnt; + /* USER CODE END UART0_WRITE_DMA_FINISH */ +} + +/** + * @brief Uart0 Interruput Read CallBack Function. + * @param handle Uart handle. + * @retval None. + */ +void UART0ReadInterruptCallback(void *handle) +{ + BASE_FUNC_UNUSED(handle); + /* USER CODE BEGIN UART0_READ_IT_FINISH */ + if (g_uartFrame.rxLen >= UI_RX_BUF_LEN - 1) { + g_uartFrame.rxLen = 0; + } + HAL_UART_ReadIT(handle, &g_uartFrame.rxData, 1); + g_uartRxBuf[g_uartFrame.rxLen] = g_uartFrame.rxData; + g_uartFrame.rxLen++; + g_uartFrame.rxFlag = 1; + g_uartFrame.uartItTxFlag = 0; + return; + /* USER CODE END UART0_READ_IT_FINISH */ +} + +/** + * @brief Uart Read Data Init Function. + * @param void. + * @retval None. + */ +void UartRecvInit(void) +{ + /* Uart reception initialization */ + FrameRecvClear(&g_uartFrame); + HAL_UART_ReadIT(&g_uart0, &g_uartFrame.rxData, 1); +} + +/** + * @brief Uart Read Data Process Function. + * @param mtrCtrl The motor control handle. + * @retval None. + */ +void UartModuleProcess_Rx(MTRCTRL_Handle *mtrCtrl) +{ + /* Verify Parameters */ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + SetUartDmaStatus(mtrCtrl); + if (g_uartFrame.rxFlag == 1) { /* Receive data flag. */ + if (g_uartFrame.timeOutCnt++ > UART_TIME_OUT_MS) { + /* Received data from the host. */ + g_uartFrame.frameFlag = 1; + g_uartFrame.rxFlag = 0; + g_uartFrame.timeOutCnt = 0; + /* Execute data process. */ + CUST_DataReceProcss(mtrCtrl, g_uartRxBuf); + g_uartFrame.rxAckFlag = 1; + g_uartFrame.rxLen = 0; + } + } + g_uartFrame.frameFlag = 0; +} + +/** + * @brief Uart Write Data Process Function. + * @param mtrCtrl The motor control handle. + * @retval None. + */ +void UartModuleProcess_Tx(MTRCTRL_Handle *mtrCtrl) +{ + /* Verify Parameters */ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + if (g_uartFrame.txFlag == 1) { /* Send data flag. */ + mtrCtrl->uartTimeStamp = (float)getdeltaSystickCnt; /* Unit data time stamp */ + g_uartFrame.upDataCnt = 0; + g_uartFrame.txFlag = 0; + /* Send data to host. */ + unsigned int txLen = CUST_TransmitData(mtrCtrl, g_uartTxBuf); + /* If txIT mode send data finish, convert to DMA mode */ + if (g_uartFrame.uartItTxFlag == 1) { + HAL_UART_WriteDMA(&g_uart0, g_uartTxBuf, txLen); + } + } +} diff --git a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/user_interface/uart_module.h b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/user_interface/uart_module.h new file mode 100644 index 00000000..f1f8f899 --- /dev/null +++ b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/user_interface/uart_module.h @@ -0,0 +1,47 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file uart_module.h + * @author MCU Algorithm Team + * @brief This file provides functions declaration of Serial port communication. + */ +#ifndef McsMagicTag_UART_MODULE_H +#define McsMagicTag_UART_MODULE_H + +#include "protocol.h" +#include "mcs_ctlmode_config.h" + +typedef struct { + unsigned int buffLen; + unsigned int timeOutCnt; + unsigned char frameFlag; + unsigned int rxLen; + unsigned char rxFlag; + unsigned char txFlag; + unsigned char rxData; + unsigned int upDataCnt; + unsigned int upDataDelayCnt; + unsigned char uartItTxFlag; + unsigned char rxAckFlag; +} FRAME_Handle; + + +void UartRecvInit(void); +void UartModuleProcess_Rx(MTRCTRL_Handle *mtrCtrl); +void UartModuleProcess_Tx(MTRCTRL_Handle *mtrCtrl); + +#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/E7050_FOC/readme.md b/vendor/yibaina_3061M/demo/E7050_FOC/readme.md new file mode 100644 index 00000000..55601995 --- /dev/null +++ b/vendor/yibaina_3061M/demo/E7050_FOC/readme.md @@ -0,0 +1,20 @@ +# E7050_foc + +**【功能描述】** + +- 基于ECMCU105H/ECBMCU201MPC单板的单电机双电阻采样的无感Foc应用 + +**【环境要求】** + +- 所有单板电源改制为演示用的24V低压,电机选用E7050 +- 电机(黄绿蓝)相线分别对应功率板上U/V/W通道接口 + +**【配置方法】** + +- 将user文件夹替换原工程user文件夹即可 + +【操作说明】 + +- 点击开发板按键启动电机 + +- 开发板蓝色电位器,可以调节电机转动的快慢。(5-29HZ) \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/E7050_FOC/user/generatecode/feature.h b/vendor/yibaina_3061M/demo/E7050_FOC/user/generatecode/feature.h new file mode 100644 index 00000000..715371be --- /dev/null +++ b/vendor/yibaina_3061M/demo/E7050_FOC/user/generatecode/feature.h @@ -0,0 +1,94 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file feature.h + * @author MCU Driver Team + * @brief This file contains macro configurations related to the project. This file is generated by the IDE tool. + * @date 2025-04-14 14:05:10 + */ + +#ifndef McuMagicTag_FEATURE_H +#define McuMagicTag_FEATURE_H + +/* Macro definitions --------------------------------------------------------- */ +#define CHIP_3061MNPICA MACRO_ENABLE + +#define MACRO_ENABLE 1 +#define MACRO_DISABLE 0 + +/* Macro switch */ +#define BASE_DEFINE_USE_ASSERT MACRO_ENABLE +#ifndef FLASH_CRC_CONFIG +#define FLASH_CRC_CONFIG +#endif /* #ifndef FLASH_CRC_CONFIG */ +#define BASE_MATH_SINCOS_MIDDLE_TABLE MACRO_ENABLE /**< This macro is used to control the table type when the + BASE_MATH_GetSinCos() queries the table. When the value of + this macro is MACRO_ENABLE, the error value obtained by the + BASE_MATH_GetSinCos() is relatively small, and the return + value of the function may be greater than or less than the + actual value. When the value of this macro is MACRO_DISABLE, + the error value obtained by the BASE_MATH_GetSinCos() is + relatively large. However, in the range [0°, 180°) and + [180°, 360°), the return value of the function is either + greater than or less than the actual value. */ + +/* Peripheral module macro switch--------------------------------------------- */ +#define BOARD_DIM_NUM 1 /**< Number of dimming handle arrays. */ + +#define BOARD_KEY_NUM 10 /**< Number of key handle arrays. */ +#define BOARD_KEY_PRESS_ON GPIO_HIGH_LEVEL /**< GPIO status corresponding to long press valid. */ +#define BOARD_KEY_PRESS_OFF GPIO_LOW_LEVEL /**< GPIO status corresponding to short press valid. */ + +#define BOARD_LED_SEG_NUM 4 /**< Number of segments. */ +#define BOARD_LED_SEGMENT_ON GPIO_HIGH_LEVEL /**< GPIO level status corresponding to valid segments. */ +#define BOARD_LED_SEGMENT_OFF GPIO_LOW_LEVEL /**< GPIO level status corresponding to invalid segments. */ + +#define BOARD_MKEY_SCHEME_NUMBER BOARD_MKEY_SCHEME_NUMBER_ONE /**< Define the scheme to be adopted. */ +#define BOARD_MKEY_OUT_NUM 4 /**< Number of GPIO pins used as output during scanning. */ +#define BOARD_MKEY_IN_NUM 4 /**< Number of GPIO pins used as input during scanning. */ +#define BOARD_MKEY_OUT_PIN_VALID GPIO_LOW_LEVEL /**< GPIO level status corresponding to the valid \ + status of the output GPIO in the key matrix. */ +#define BOARD_MKEY_OUT_PIN_INVALID GPIO_HIGH_LEVEL /**< GPIO level status corresponding to the \ + invalid status of the output GPIO in the key matrix. */ +#define BOARD_MKEY_IN_PIN_VALID GPIO_LOW_LEVEL /**< Indicates the GPIO level corresponding to the \ + valid status of the input GPIO in the key matrix. */ +#define BOARD_MKEY_IN_PIN_INVALID GPIO_HIGH_LEVEL /**< Indicates the GPIO level corresponding to the \ + invalid status of the input GPIO in the key matrix. */ + +#define BOARD_PULSES_NUM 2 /**< Number of pulse handles. */ + +#define BASE_DEFINE_SLIPAVERAGE_NUM 2 /**< Sliding average array length. */ + +#define LISTNODE_MAX 20 + +#define BASE_DEFINE_DMA_QUICKSTART + +#define XTRAIL_FREQ 30000000U + +#define DBG_USE_NO_PRINTF 0U +#define DBG_USE_UART_PRINTF 1U + +#define DBG_PRINTF_USE DBG_USE_UART_PRINTF +#if (DBG_PRINTF_USE == DBG_USE_UART_PRINTF) +#define DBG_PRINTF_UART_PORT UART0 +#endif + +/* USER CODE BEGIN 0 */ +/* USER CODE 区域内代码不会被覆盖,区域外会被生成的默认代码覆盖(其余USER CODE 区域同理) */ +/* USER CODE END 0 */ + +#endif /* McuMagicTag_FEATURE_H */ \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/E7050_FOC/user/generatecode/main.h b/vendor/yibaina_3061M/demo/E7050_FOC/user/generatecode/main.h new file mode 100644 index 00000000..40d65b1d --- /dev/null +++ b/vendor/yibaina_3061M/demo/E7050_FOC/user/generatecode/main.h @@ -0,0 +1,98 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file main.h + * @author MCU Driver Team + * @brief This file contains driver init functions. + * @date 2025-04-14 14:05:10 + */ + +/* Define to prevent recursive inclusion ------------------------------------- */ +#ifndef McuMagicTag_SYSTEM_INIT_H +#define McuMagicTag_SYSTEM_INIT_H + +#include "adc.h" +#include "adc_ex.h" +#include "acmp.h" +#include "acmp_ex.h" +#include "apt.h" +#include "uart.h" +#include "uart_ex.h" +#include "gpio.h" +#include "timer.h" +#include "timer_ex.h" +#include "pga.h" +#include "crg.h" +#include "dma.h" +#include "dma_ex.h" +#include "iocmg.h" + +#define IO_SPEED_FAST 0x00U +#define IO_SPEED_SLOW 0x01U + +#define IO_DRV_LEVEL4 0x00U +#define IO_DRV_LEVEL3 0x01U +#define IO_DRV_LEVEL2 0x02U +#define IO_DRV_LEVEL1 0x03U + +#define XTAL_DRV_LEVEL4 0x03U +#define XTAL_DRV_LEVEL3 0x02U +#define XTAL_DRV_LEVEL2 0x01U +#define XTAL_DRV_LEVEL1 0x00U + +#define GpioStartStop_PIN GPIO_PIN_4 +#define GpioStartStop_HANDLE g_gpio2 + +extern ACMP_Handle g_acmp0; +extern PGA_Handle g_pga0; +extern PGA_Handle g_pga1; +extern TIMER_Handle g_timer0; +extern TIMER_Handle g_timer1; +extern UART_Handle g_uart0; +extern APT_Handle g_apt0; +extern APT_Handle g_apt1; +extern APT_Handle g_apt2; +extern ADC_Handle g_adc0; + +extern DMA_Handle g_dmac; + +extern GPIO_Handle g_gpio2; +extern GPIO_Handle g_gpio1; + +BASE_StatusType CRG_Config(CRG_CoreClkSelect *coreClkSelect); +void SystemInit(void); + +void UART0WriteInterruptCallback(void *handle); +void UART0ReadInterruptCallback(void *handle); + +void UART0InterruptErrorCallback(void *handle); +void MotorStatemachineCallBack(void *handle); +void TIMER1_DMAOverFlow_InterruptProcess(void *handle); +void CheckPotentiometerValueCallback(void *handle); +void TIMER0_DMAOverFlow_InterruptProcess(void *handle); +void MotorCarrierProcessCallback(void *aptHandle); +void MotorSysErrCallback(void *aptHandle); + +void UART0_TXDMACallback(void *handle); + +void MotorStartStopKeyCallback(void *param); + +/* USER CODE BEGIN 0 */ +/* USER CODE 区域内代码不会被覆盖,区域外会被生成的默认代码覆盖(其余USER CODE 区域同理) */ +/* USER CODE END 0 */ + +#endif /* McuMagicTag_SYSTEM_INIT_H */ \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/E7050_FOC/user/generatecode/system_init.c b/vendor/yibaina_3061M/demo/E7050_FOC/user/generatecode/system_init.c new file mode 100644 index 00000000..d49cd860 --- /dev/null +++ b/vendor/yibaina_3061M/demo/E7050_FOC/user/generatecode/system_init.c @@ -0,0 +1,677 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file system_init.c + * @author MCU Driver Team + * @brief This file contains driver init functions. + * @date 2025-04-14 14:05:10 + */ + +#include "main.h" +#include "ioconfig.h" +#include "iocmg_ip.h" + +#define UART0_BAUD_RATE 1843200 + +BASE_StatusType CRG_Config(CRG_CoreClkSelect *coreClkSelect) +{ + CRG_Handle crg; + crg.baseAddress = CRG; + crg.pllRefClkSelect = CRG_PLL_REF_CLK_SELECT_HOSC; + crg.pllPreDiv = CRG_PLL_PREDIV_4; + crg.pllFbDiv = 48; /* PLL Multiplier 48 */ + crg.pllPostDiv = CRG_PLL_POSTDIV_2; + crg.coreClkSelect = CRG_CORE_CLK_SELECT_PLL; + crg.handleEx.pllPostDiv2 = CRG_PLL_POSTDIV2_3; + crg.handleEx.clk1MSelect = CRG_1M_CLK_SELECT_HOSC; + crg.handleEx.clk1MDiv = (25 - 1); /* The 1 MHz freq is equal to the input clock frequency / (clk_1m_div + 1). 25 is the div of the clk_1m in CLOCK. */ + + if (HAL_CRG_Init(&crg) != BASE_STATUS_OK) { + return BASE_STATUS_ERROR; + } + *coreClkSelect = crg.coreClkSelect; + return BASE_STATUS_OK; +} + +static void DMA_Channel0Init(void *handle) +{ + DMA_ChannelParam dma_param; + dma_param.direction = DMA_MEMORY_TO_PERIPH_BY_DMAC; + dma_param.srcAddrInc = DMA_ADDR_INCREASE; + dma_param.destAddrInc = DMA_ADDR_UNALTERED; + dma_param.srcPeriph = DMA_REQUEST_MEM; + dma_param.destPeriph = DMA_REQUEST_UART0_TX; + dma_param.srcWidth = DMA_TRANSWIDTH_BYTE; + dma_param.destWidth = DMA_TRANSWIDTH_BYTE; + dma_param.srcBurst = DMA_BURST_LENGTH_1; + dma_param.destBurst = DMA_BURST_LENGTH_1; + dma_param.pHandle = handle; + HAL_DMA_InitChannel(&g_dmac, &dma_param, DMA_CHANNEL_ZERO); +} + +static void DMA_Init(void) +{ + HAL_CRG_IpEnableSet(DMA_BASE, IP_CLK_ENABLE); + g_dmac.baseAddress = DMA; + IRQ_Register(IRQ_DMA_TC, HAL_DMA_IrqHandlerTc, &g_dmac); + IRQ_Register(IRQ_DMA_ERR, HAL_DMA_IrqHandlerError, &g_dmac); + IRQ_EnableN(IRQ_DMA_TC); + IRQ_EnableN(IRQ_DMA_ERR); + HAL_DMA_Init(&g_dmac); + DMA_Channel0Init((void *)(&g_uart0)); + HAL_DMA_SetChannelPriorityEx(&g_dmac, DMA_CHANNEL_ZERO, DMA_PRIORITY_HIGHEST); +} + +static void ACMP0_Init(void) +{ + HAL_CRG_IpEnableSet(ACMP0_BASE, IP_CLK_ENABLE); /* ACMP clock bit reset. */ + g_acmp0.baseAddress = ACMP0_BASE; + g_acmp0.inOutConfig.inputNNum = ACMP_INPUT_N_SELECT2; + g_acmp0.inOutConfig.inputPNum = ACMP_INPUT_P_SELECT2; + g_acmp0.inOutConfig.polarity = ACMP_OUT_NOT_INVERT; + g_acmp0.filterCtrl.filterMode = ACMP_FILTER_NONE; + g_acmp0.hysteresisVol = ACMP_HYS_VOL_30MV; + g_acmp0.interruptEn = BASE_CFG_UNSET; + HAL_ACMP_Init(&g_acmp0); +} + +static void ADC0_Init(void) +{ + HAL_CRG_IpEnableSet(ADC0_BASE, IP_CLK_ENABLE); + HAL_CRG_IpClkSelectSet(ADC0_BASE, CRG_ADC_CLK_ASYN_PLL_DIV); + HAL_CRG_IpClkDivSet(ADC0_BASE, CRG_ADC_DIV_1); + + g_adc0.baseAddress = ADC0; + g_adc0.socPriority = ADC_PRIMODE_ALL_ROUND; + + HAL_ADC_Init(&g_adc0); + + SOC_Param socParam = {0}; + socParam.adcInput = ADC_CH_ADCINA0; /* PGA0_OUT(ADC AIN0) */ + socParam.sampleTotalTime = ADC_SOCSAMPLE_22CLK; /* adc sample total time 22 adc_clk */ + socParam.trigSource = ADC_TRIGSOC_APT0_SOCA; + socParam.continueMode = BASE_CFG_DISABLE; + socParam.finishMode = ADC_SOCFINISH_NONE; + HAL_ADC_ConfigureSoc(&g_adc0, ADC_SOC_NUM1, &socParam); + + socParam.adcInput = ADC_CH_ADCINA1; /* PGA1_OUT(ADC AIN1) */ + socParam.sampleTotalTime = ADC_SOCSAMPLE_22CLK; /* adc sample total time 22 adc_clk */ + socParam.trigSource = ADC_TRIGSOC_APT0_SOCA; + socParam.continueMode = BASE_CFG_DISABLE; + socParam.finishMode = ADC_SOCFINISH_NONE; + HAL_ADC_ConfigureSoc(&g_adc0, ADC_SOC_NUM6, &socParam); + + socParam.adcInput = ADC_CH_ADCINA9; /* PIN7(ADC AIN9) */ + socParam.sampleTotalTime = ADC_SOCSAMPLE_22CLK; /* adc sample total time 22 adc_clk */ + socParam.trigSource = ADC_TRIGSOC_SOFT; + socParam.continueMode = BASE_CFG_DISABLE; + socParam.finishMode = ADC_SOCFINISH_NONE; + HAL_ADC_ConfigureSoc(&g_adc0, ADC_SOC_NUM0, &socParam); + + socParam.adcInput = ADC_CH_ADCINA5; /* PIN2(ADC AIN5) */ + socParam.sampleTotalTime = ADC_SOCSAMPLE_22CLK; /* adc sample total time 22 adc_clk */ + socParam.trigSource = ADC_TRIGSOC_SOFT; + socParam.continueMode = BASE_CFG_DISABLE; + socParam.finishMode = ADC_SOCFINISH_NONE; + HAL_ADC_ConfigureSoc(&g_adc0, ADC_SOC_NUM3, &socParam); + + socParam.adcInput = ADC_CH_ADCINA15; /* PIN14(ADC AIN15) */ + socParam.sampleTotalTime = ADC_SOCSAMPLE_22CLK; /* adc sample total time 22 adc_clk */ + socParam.trigSource = ADC_TRIGSOC_SOFT; + socParam.continueMode = BASE_CFG_DISABLE; + socParam.finishMode = ADC_SOCFINISH_NONE; + HAL_ADC_ConfigureSoc(&g_adc0, ADC_SOC_NUM4, &socParam); +} + +__weak void MotorSysErrCallback(void *aptHandle) +{ + BASE_FUNC_UNUSED(aptHandle); + /* USER CODE BEGIN APT0_EVENT_INTERRUPT */ + /* USER CODE END APT0_EVENT_INTERRUPT */ +} + +__weak void MotorCarrierProcessCallback(void *aptHandle) +{ + BASE_FUNC_UNUSED(aptHandle); + /* USER CODE BEGIN APT0_TIMER_INTERRUPT */ + /* USER CODE END APT0_TIMER_INTERRUPT */ +} + +static void APT0_ProtectInit(void) +{ + APT_OutCtrlProtectEx protectApt = {0}; + protectApt.ocEventEnEx = BASE_CFG_ENABLE; + protectApt.ocEventModeEx = APT_OUT_CTRL_ONE_SHOT; + protectApt.ocActionEx = APT_OUT_CTRL_ACTION_LOW; + protectApt.ocActionBEx = APT_OUT_CTRL_ACTION_LOW; + protectApt.ocEvtInterruptEnEx = BASE_CFG_ENABLE; + protectApt.ocSysEvent = APT_SYS_EVT_DEBUG | APT_SYS_EVT_CLK | APT_SYS_EVT_MEM; + protectApt.originalEvtEx = APT_EM_ORIGINAL_SRC_ACMP0; + protectApt.evtPolarityMaskEx = APT_EM_ACMP0_INVERT_BIT; + protectApt.filterCycleNumEx = 0; + HAL_APT_ProtectInitEx(&g_apt0, &protectApt); +} + +static void APT0_Init(void) +{ + HAL_CRG_IpEnableSet(APT0_BASE, IP_CLK_ENABLE); + + g_apt0.baseAddress = APT0; + + /* Clock Settings */ + g_apt0.waveform.dividerFactor = 1 - 1; + /* Timer Settings */ + g_apt0.waveform.timerPeriod = 7500; /* 7500 is count period of APT time-base timer */ + g_apt0.waveform.cntMode = APT_COUNT_MODE_UP_DOWN; + + /* Wave Form */ + g_apt0.waveform.basicType = APT_PWM_BASIC_A_HIGH_B_LOW; + g_apt0.waveform.chAOutType = APT_PWM_OUT_BASIC_TYPE; + g_apt0.waveform.chBOutType = APT_PWM_OUT_BASIC_TYPE; + g_apt0.waveform.divInitVal = 0; + g_apt0.waveform.cntInitVal = 0; + g_apt0.waveform.cntCmpLeftEdge = 500; /* 500 is count compare point of the left edge of PWM waveform */ + g_apt0.waveform.cntCmpRightEdge = 4000; /* 4000 is count compare point of the right edge of PWM waveform */ + g_apt0.waveform.cntCmpLoadMode = APT_BUFFER_INDEPENDENT_LOAD; + g_apt0.waveform.cntCmpLoadEvt = APT_COMPARE_LOAD_EVENT_ZERO; + g_apt0.waveform.deadBandCnt = 225; /* 225 is dead-band value */ + + /* ADC Trigger SOCA */ + g_apt0.adcTrg.trgEnSOCA = BASE_CFG_ENABLE; + g_apt0.adcTrg.cntCmpSOCA = 375; /* 375 is count compare point of ADC trigger source SOCA when using CMPA */ + g_apt0.adcTrg.trgSrcSOCA = APT_CS_SRC_CNTR_CMPA_DOWN; + g_apt0.adcTrg.trgScaleSOCA = 1; + + /* ADC Trigger SOCB */ + g_apt0.adcTrg.trgEnSOCB = BASE_CFG_ENABLE; + g_apt0.adcTrg.cntCmpSOCB = 1; + g_apt0.adcTrg.trgSrcSOCB = APT_CS_SRC_CNTR_CMPB_DOWN; + g_apt0.adcTrg.trgScaleSOCB = 1; + + g_apt0.adcTrg.cntCmpLoadMode = APT_BUFFER_INDEPENDENT_LOAD; + g_apt0.adcTrg.cntCmpLoadEvt = APT_COMPARE_LOAD_EVENT_ZERO; + + /* Timer Trigger */ + g_apt0.tmrInterrupt.tmrInterruptEn = BASE_CFG_ENABLE; + g_apt0.tmrInterrupt.tmrInterruptSrc = APT_INT_SRC_CNTR_ZERO; + g_apt0.tmrInterrupt.tmrInterruptScale = 1; + + APT0_ProtectInit(); + + HAL_APT_PWMInit(&g_apt0); + HAL_APT_RegisterCallBack(&g_apt0, APT_EVENT_INTERRUPT, MotorSysErrCallback); + IRQ_SetPriority(IRQ_APT0_EVT, 7); /* 7 is priority value */ + IRQ_Register(IRQ_APT0_EVT, HAL_APT_EventIrqHandler, &g_apt0); + IRQ_EnableN(IRQ_APT0_EVT); + HAL_APT_RegisterCallBack(&g_apt0, APT_TIMER_INTERRUPT, MotorCarrierProcessCallback); + IRQ_SetPriority(IRQ_APT0_TMR, 5); /* 5 is priority value */ + IRQ_Register(IRQ_APT0_TMR, HAL_APT_TimerIrqHandler, &g_apt0); + IRQ_EnableN(IRQ_APT0_TMR); +} + +static void APT1_ProtectInit(void) +{ + APT_OutCtrlProtectEx protectApt = {0}; + protectApt.ocEventEnEx = BASE_CFG_ENABLE; + protectApt.ocEventModeEx = APT_OUT_CTRL_ONE_SHOT; + protectApt.ocActionEx = APT_OUT_CTRL_ACTION_LOW; + protectApt.ocActionBEx = APT_OUT_CTRL_ACTION_LOW; + protectApt.ocEvtInterruptEnEx = BASE_CFG_DISABLE; + protectApt.ocSysEvent = APT_SYS_EVT_DEBUG | APT_SYS_EVT_CLK | APT_SYS_EVT_MEM; + protectApt.originalEvtEx = APT_EM_ORIGINAL_SRC_ACMP0; + protectApt.evtPolarityMaskEx = APT_EM_ACMP0_INVERT_BIT; + protectApt.filterCycleNumEx = 0; + HAL_APT_ProtectInitEx(&g_apt1, &protectApt); +} + +static void APT1_Init(void) +{ + HAL_CRG_IpEnableSet(APT1_BASE, IP_CLK_ENABLE); + + g_apt1.baseAddress = APT1; + + /* Clock Settings */ + g_apt1.waveform.dividerFactor = 1 - 1; + /* Timer Settings */ + g_apt1.waveform.timerPeriod = 7500; /* 7500 is count period of APT time-base timer */ + g_apt1.waveform.cntMode = APT_COUNT_MODE_UP_DOWN; + + /* Wave Form */ + g_apt1.waveform.basicType = APT_PWM_BASIC_A_HIGH_B_LOW; + g_apt1.waveform.chAOutType = APT_PWM_OUT_BASIC_TYPE; + g_apt1.waveform.chBOutType = APT_PWM_OUT_BASIC_TYPE; + g_apt1.waveform.divInitVal = 0; + g_apt1.waveform.cntInitVal = 0; + g_apt1.waveform.cntCmpLeftEdge = 500; /* 500 is count compare point of the left edge of PWM waveform */ + g_apt1.waveform.cntCmpRightEdge = 4000; /* 4000 is count compare point of the right edge of PWM waveform */ + g_apt1.waveform.cntCmpLoadMode = APT_BUFFER_INDEPENDENT_LOAD; + g_apt1.waveform.cntCmpLoadEvt = APT_COMPARE_LOAD_EVENT_ZERO; + g_apt1.waveform.deadBandCnt = 225; /* 225 is dead-band value */ + + APT1_ProtectInit(); + + HAL_APT_PWMInit(&g_apt1); +} + +static void APT2_ProtectInit(void) +{ + APT_OutCtrlProtectEx protectApt = {0}; + protectApt.ocEventEnEx = BASE_CFG_ENABLE; + protectApt.ocEventModeEx = APT_OUT_CTRL_ONE_SHOT; + protectApt.ocActionEx = APT_OUT_CTRL_ACTION_LOW; + protectApt.ocActionBEx = APT_OUT_CTRL_ACTION_LOW; + protectApt.ocEvtInterruptEnEx = BASE_CFG_DISABLE; + protectApt.ocSysEvent = APT_SYS_EVT_DEBUG | APT_SYS_EVT_CLK | APT_SYS_EVT_MEM; + protectApt.originalEvtEx = APT_EM_ORIGINAL_SRC_ACMP0; + protectApt.evtPolarityMaskEx = APT_EM_ACMP0_INVERT_BIT; + protectApt.filterCycleNumEx = 0; + HAL_APT_ProtectInitEx(&g_apt2, &protectApt); +} + +static void APT2_Init(void) +{ + HAL_CRG_IpEnableSet(APT2_BASE, IP_CLK_ENABLE); + + g_apt2.baseAddress = APT2; + + /* Clock Settings */ + g_apt2.waveform.dividerFactor = 1 - 1; + /* Timer Settings */ + g_apt2.waveform.timerPeriod = 7500; /* 7500 is count period of APT time-base timer */ + g_apt2.waveform.cntMode = APT_COUNT_MODE_UP_DOWN; + + /* Wave Form */ + g_apt2.waveform.basicType = APT_PWM_BASIC_A_HIGH_B_LOW; + g_apt2.waveform.chAOutType = APT_PWM_OUT_BASIC_TYPE; + g_apt2.waveform.chBOutType = APT_PWM_OUT_BASIC_TYPE; + g_apt2.waveform.divInitVal = 0; + g_apt2.waveform.cntInitVal = 0; + g_apt2.waveform.cntCmpLeftEdge = 500; /* 500 is count compare point of the left edge of PWM waveform */ + g_apt2.waveform.cntCmpRightEdge = 4000; /* 4000 is count compare point of the right edge of PWM waveform */ + g_apt2.waveform.cntCmpLoadMode = APT_BUFFER_INDEPENDENT_LOAD; + g_apt2.waveform.cntCmpLoadEvt = APT_COMPARE_LOAD_EVENT_ZERO; + g_apt2.waveform.deadBandCnt = 225; /* 225 is dead-band value */ + + APT2_ProtectInit(); + + HAL_APT_PWMInit(&g_apt2); +} + +__weak void MotorStartStopKeyCallback(void *param) +{ + GPIO_Handle *handle = (GPIO_Handle *)param; + BASE_FUNC_UNUSED(handle); +} + +static void GPIO_Init(void) +{ + HAL_CRG_IpEnableSet(GPIO2_BASE, IP_CLK_ENABLE); + g_gpio2.baseAddress = GPIO2; + + g_gpio2.pins = GPIO_PIN_3; + HAL_GPIO_Init(&g_gpio2); + HAL_GPIO_SetDirection(&g_gpio2, g_gpio2.pins, GPIO_OUTPUT_MODE); + HAL_GPIO_SetValue(&g_gpio2, g_gpio2.pins, GPIO_HIGH_LEVEL); + HAL_GPIO_SetIrqType(&g_gpio2, g_gpio2.pins, GPIO_INT_TYPE_NONE); + + g_gpio2.pins = GPIO_PIN_4; + HAL_GPIO_Init(&g_gpio2); + HAL_GPIO_SetDirection(&g_gpio2, g_gpio2.pins, GPIO_INPUT_MODE); + HAL_GPIO_SetValue(&g_gpio2, g_gpio2.pins, GPIO_HIGH_LEVEL); + HAL_GPIO_SetIrqType(&g_gpio2, g_gpio2.pins, GPIO_INT_TYPE_LOW_LEVEL); + + HAL_CRG_IpEnableSet(GPIO1_BASE, IP_CLK_ENABLE); + g_gpio1.baseAddress = GPIO1; + + g_gpio1.pins = GPIO_PIN_0; + HAL_GPIO_Init(&g_gpio1); + HAL_GPIO_SetDirection(&g_gpio1, g_gpio1.pins, GPIO_OUTPUT_MODE); + HAL_GPIO_SetValue(&g_gpio1, g_gpio1.pins, GPIO_HIGH_LEVEL); + HAL_GPIO_SetIrqType(&g_gpio1, g_gpio1.pins, GPIO_INT_TYPE_NONE); + + HAL_GPIO_RegisterCallBack(&g_gpio2, GPIO_PIN_4, MotorStartStopKeyCallback); + IRQ_Register(IRQ_GPIO2, HAL_GPIO_IrqHandler, &g_gpio2); + IRQ_SetPriority(IRQ_GPIO2, 1); /* set gpio1 interrupt priority to 1, 1~15. 1 is priority value */ + IRQ_EnableN(IRQ_GPIO2); /* gpio interrupt enable */ + + return; +} + +static void PGA0_Init(void) +{ + HAL_CRG_IpEnableSet(PGA0_BASE, IP_CLK_ENABLE); + + g_pga0.baseAddress = PGA0_BASE; + g_pga0.externalResistorMode = BASE_CFG_ENABLE; + g_pga0.handleEx.extCapCompensation = PGA_EXT_COMPENSATION_2X; + HAL_PGA_Init(&g_pga0); +} + +static void PGA1_Init(void) +{ + HAL_CRG_IpEnableSet(PGA1_BASE, IP_CLK_ENABLE); + + g_pga1.baseAddress = PGA1_BASE; + g_pga1.externalResistorMode = BASE_CFG_ENABLE; + g_pga1.handleEx.extCapCompensation = PGA_EXT_COMPENSATION_2X; + HAL_PGA_Init(&g_pga1); +} + +__weak void CheckPotentiometerValueCallback(void *handle) +{ + BASE_FUNC_UNUSED(handle); + /* USER CODE BEGIN CheckPotentiometerValueCallback */ + /* USER CODE END CheckPotentiometerValueCallback */ +} + +static void TIMER0_Init(void) +{ + HAL_CRG_IpEnableSet(TIMER0_BASE, IP_CLK_ENABLE); /* TIMER0 clock enable. */ + unsigned int load = ((float)HAL_CRG_GetIpFreq((void *)TIMER0) / (1u << (TIMERPRESCALER_NO_DIV * 4)) / 1000000u) * 1000000; + + g_timer0.baseAddress = TIMER0; + g_timer0.load = (load < 1) ? 0 : (load - 1); /* Set timer value immediately */ + g_timer0.bgLoad = (load < 1) ? 0 : (load - 1); /* Set timer value */ + g_timer0.mode = TIMER_MODE_RUN_PERIODIC; /* Run in period mode */ + g_timer0.prescaler = TIMERPRESCALER_NO_DIV; /* Don't frequency division */ + g_timer0.size = TIMER_SIZE_32BIT; /* 1 for 32bit, 0 for 16bit */ + g_timer0.interruptEn = BASE_CFG_ENABLE; + g_timer0.adcSocReqEnable = BASE_CFG_DISABLE; + g_timer0.dmaReqEnable = BASE_CFG_DISABLE; + HAL_TIMER_Init(&g_timer0); + IRQ_Register(IRQ_TIMER0, HAL_TIMER_IrqHandler, &g_timer0); + + HAL_TIMER_RegisterCallback(&g_timer0, TIMER_PERIOD_FIN, CheckPotentiometerValueCallback); + IRQ_SetPriority(IRQ_TIMER0, 1); /* 1 is priority value */ + IRQ_EnableN(IRQ_TIMER0); +} + +__weak void MotorStatemachineCallBack(void *handle) +{ + BASE_FUNC_UNUSED(handle); + /* USER CODE BEGIN MotorStatemachineCallBack */ + /* USER CODE END MotorStatemachineCallBack */ +} + +static void TIMER1_Init(void) +{ + HAL_CRG_IpEnableSet(TIMER1_BASE, IP_CLK_ENABLE); /* TIMER1 clock enable. */ + unsigned int load = ((float)HAL_CRG_GetIpFreq((void *)TIMER1) / (1u << (TIMERPRESCALER_NO_DIV * 4)) / 1000000u) * 500; + + g_timer1.baseAddress = TIMER1; + g_timer1.load = (load < 1) ? 0 : (load - 1); /* Set timer value immediately */ + g_timer1.bgLoad = (load < 1) ? 0 : (load - 1); /* Set timer value */ + g_timer1.mode = TIMER_MODE_RUN_PERIODIC; /* Run in period mode */ + g_timer1.prescaler = TIMERPRESCALER_NO_DIV; /* Don't frequency division */ + g_timer1.size = TIMER_SIZE_32BIT; /* 1 for 32bit, 0 for 16bit */ + g_timer1.interruptEn = BASE_CFG_ENABLE; + g_timer1.adcSocReqEnable = BASE_CFG_DISABLE; + g_timer1.dmaReqEnable = BASE_CFG_DISABLE; + HAL_TIMER_Init(&g_timer1); + IRQ_Register(IRQ_TIMER1, HAL_TIMER_IrqHandler, &g_timer1); + + HAL_TIMER_RegisterCallback(&g_timer1, TIMER_PERIOD_FIN, MotorStatemachineCallBack); + IRQ_SetPriority(IRQ_TIMER1, 1); /* 1 is priority value */ + IRQ_EnableN(IRQ_TIMER1); +} + +__weak void UART0InterruptErrorCallback(void *handle) +{ + BASE_FUNC_UNUSED(handle); + /* USER CODE BEGIN UART0_TRNS_IT_ERROR */ + /* USER CODE END UART0_TRNS_IT_ERROR */ +} + +__weak void UART0_TXDMACallback(void *handle) +{ + BASE_FUNC_UNUSED(handle); + /* USER CODE BEGIN UART0_WRITE_DMA_FINISH */ + /* USER CODE END UART0_WRITE_DMA_FINISH */ +} + +__weak void UART0WriteInterruptCallback(void *handle) +{ + BASE_FUNC_UNUSED(handle); + /* USER CODE BEGIN UART0_WRITE_IT_FINISH */ + /* USER CODE END UART0_WRITE_IT_FINISH */ +} + +__weak void UART0ReadInterruptCallback(void *handle) +{ + BASE_FUNC_UNUSED(handle); + /* USER CODE BEGIN UART0_READ_IT_FINISH */ + /* USER CODE END UART0_READ_IT_FINISH */ +} + +static void UART0_Init(void) +{ + HAL_CRG_IpEnableSet(UART0_BASE, IP_CLK_ENABLE); /* UART0 clock enable. */ + g_uart0.baseAddress = UART0; + + g_uart0.baudRate = UART0_BAUD_RATE; + g_uart0.dataLength = UART_DATALENGTH_8BIT; + g_uart0.stopBits = UART_STOPBITS_ONE; + g_uart0.parity = UART_PARITY_NONE; + g_uart0.txMode = UART_MODE_DMA; + g_uart0.rxMode = UART_MODE_INTERRUPT; + g_uart0.fifoMode = BASE_CFG_ENABLE; + g_uart0.fifoTxThr = UART_FIFODEPTH_SIZE8; + g_uart0.fifoRxThr = UART_FIFODEPTH_SIZE8; + g_uart0.hwFlowCtr = BASE_CFG_DISABLE; + g_uart0.handleEx.overSampleMultiple = UART_OVERSAMPLING_16X; + g_uart0.handleEx.msbFirst = BASE_CFG_DISABLE; + HAL_UART_Init(&g_uart0); + HAL_UART_RegisterCallBack(&g_uart0, UART_TRNS_IT_ERROR, (UART_CallbackType)UART0InterruptErrorCallback); + HAL_UART_RegisterCallBack(&g_uart0, UART_READ_IT_FINISH, (UART_CallbackType)UART0ReadInterruptCallback); + IRQ_Register(IRQ_UART0, HAL_UART_IrqHandler, &g_uart0); + IRQ_SetPriority(IRQ_UART0, 6); /* 6 is priority value */ + IRQ_EnableN(IRQ_UART0); + g_uart0.dmaHandle = &g_dmac; + g_uart0.uartDmaTxChn = 0; + HAL_UART_RegisterCallBack(&g_uart0, UART_WRITE_DMA_FINISH, (UART_CallbackType)UART0_TXDMACallback); + HAL_UART_RegisterCallBack(&g_uart0, UART_WRITE_IT_FINISH, (UART_CallbackType)UART0WriteInterruptCallback); +} + +static void IOConfig(void) +{ + SYSCTRL0->SC_SYS_STAT.BIT.update_mode = 0; + SYSCTRL0->SC_SYS_STAT.BIT.update_mode_clear = 1; + /* Config PIN39 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO0_3_AS_UART0_TXD); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO0_3_AS_UART0_TXD, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO0_3_AS_UART0_TXD, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO0_3_AS_UART0_TXD, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO0_3_AS_UART0_TXD, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN40 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO0_4_AS_UART0_RXD); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO0_4_AS_UART0_RXD, PULL_UP); /* Pull-up and Pull-down, UART RX recommend PULL_UP */ + HAL_IOCMG_SetPinSchmidtMode(GPIO0_4_AS_UART0_RXD, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO0_4_AS_UART0_RXD, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO0_4_AS_UART0_RXD, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN1 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO0_7_AS_ACMP0_OUT); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO0_7_AS_ACMP0_OUT, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO0_7_AS_ACMP0_OUT, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO0_7_AS_ACMP0_OUT, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO0_7_AS_ACMP0_OUT, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN48 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO0_6_AS_ACMP_N2); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO0_6_AS_ACMP_N2, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO0_6_AS_ACMP_N2, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO0_6_AS_ACMP_N2, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO0_6_AS_ACMP_N2, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN47 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO0_5_AS_ACMP_P2); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO0_5_AS_ACMP_P2, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO0_5_AS_ACMP_P2, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO0_5_AS_ACMP_P2, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO0_5_AS_ACMP_P2, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN4 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO2_6_AS_PGA0_N0); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO2_6_AS_PGA0_N0, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO2_6_AS_PGA0_N0, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO2_6_AS_PGA0_N0, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO2_6_AS_PGA0_N0, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN5 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO2_5_AS_PGA0_P0); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO2_5_AS_PGA0_P0, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO2_5_AS_PGA0_P0, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO2_5_AS_PGA0_P0, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO2_5_AS_PGA0_P0, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN3 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO2_7_AS_PGA0_OUT); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO2_7_AS_PGA0_OUT, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO2_7_AS_PGA0_OUT, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO2_7_AS_PGA0_OUT, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO2_7_AS_PGA0_OUT, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN7 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO3_7_AS_ADC_AIN9); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO3_7_AS_ADC_AIN9, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO3_7_AS_ADC_AIN9, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO3_7_AS_ADC_AIN9, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO3_7_AS_ADC_AIN9, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN2 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO5_1_AS_ADC_AIN5); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO5_1_AS_ADC_AIN5, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO5_1_AS_ADC_AIN5, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO5_1_AS_ADC_AIN5, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO5_1_AS_ADC_AIN5, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN14 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO4_7_AS_ADC_AIN15); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO4_7_AS_ADC_AIN15, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO4_7_AS_ADC_AIN15, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO4_7_AS_ADC_AIN15, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO4_7_AS_ADC_AIN15, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN11 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO1_5_AS_PGA1_P0); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO1_5_AS_PGA1_P0, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO1_5_AS_PGA1_P0, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO1_5_AS_PGA1_P0, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO1_5_AS_PGA1_P0, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN12 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO1_6_AS_PGA1_N0); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO1_6_AS_PGA1_N0, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO1_6_AS_PGA1_N0, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO1_6_AS_PGA1_N0, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO1_6_AS_PGA1_N0, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN13 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO1_7_AS_PGA1_OUT); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO1_7_AS_PGA1_OUT, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO1_7_AS_PGA1_OUT, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO1_7_AS_PGA1_OUT, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO1_7_AS_PGA1_OUT, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN35 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO2_3_AS_GPIO2_3); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO2_3_AS_GPIO2_3, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO2_3_AS_GPIO2_3, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO2_3_AS_GPIO2_3, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO2_3_AS_GPIO2_3, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN27 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO1_0_AS_GPIO1_0); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO1_0_AS_GPIO1_0, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO1_0_AS_GPIO1_0, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO1_0_AS_GPIO1_0, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO1_0_AS_GPIO1_0, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN41 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO2_4_AS_GPIO2_4); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO2_4_AS_GPIO2_4, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO2_4_AS_GPIO2_4, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO2_4_AS_GPIO2_4, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO2_4_AS_GPIO2_4, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN19 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO3_0_AS_APT0_PWMA); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO3_0_AS_APT0_PWMA, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO3_0_AS_APT0_PWMA, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO3_0_AS_APT0_PWMA, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO3_0_AS_APT0_PWMA, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN23 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO4_0_AS_APT0_PWMB); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO4_0_AS_APT0_PWMB, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO4_0_AS_APT0_PWMB, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO4_0_AS_APT0_PWMB, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO4_0_AS_APT0_PWMB, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN20 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO3_1_AS_APT1_PWMA); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO3_1_AS_APT1_PWMA, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO3_1_AS_APT1_PWMA, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO3_1_AS_APT1_PWMA, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO3_1_AS_APT1_PWMA, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN24 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO4_1_AS_APT1_PWMB); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO4_1_AS_APT1_PWMB, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO4_1_AS_APT1_PWMB, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO4_1_AS_APT1_PWMB, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO4_1_AS_APT1_PWMB, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN21 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO3_2_AS_APT2_PWMA); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO3_2_AS_APT2_PWMA, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO3_2_AS_APT2_PWMA, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO3_2_AS_APT2_PWMA, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO3_2_AS_APT2_PWMA, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN25 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO4_2_AS_APT2_PWMB); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO4_2_AS_APT2_PWMB, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO4_2_AS_APT2_PWMB, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO4_2_AS_APT2_PWMB, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO4_2_AS_APT2_PWMB, DRIVER_RATE_2); /* Output signal edge fast/slow */ +} + +static void APT_SyncMasterInit(void) +{ + HAL_APT_MasterSyncInit(&g_apt0, APT_SYNC_OUT_ON_CNTR_ZERO); +} + +static void APT_SyncSlaveInit(void) +{ + APT_SlaveSyncIn aptSlave; + + aptSlave.cntPhase = 0; /* counter phase value */ + aptSlave.syncCntMode = APT_COUNT_MODE_AFTER_SYNC_UP; + aptSlave.syncInSrc = APT_SYNCIN_SRC_APT0_SYNCOUT; /* sync source selection */ + aptSlave.cntrSyncSrc = APT_CNTR_SYNC_SRC_SYNCIN; + HAL_APT_SlaveSyncInit(&g_apt1, &aptSlave); + + aptSlave.cntPhase = 0; /* counter phase value */ + aptSlave.syncCntMode = APT_COUNT_MODE_AFTER_SYNC_UP; + aptSlave.syncInSrc = APT_SYNCIN_SRC_APT0_SYNCOUT; /* sync source selection */ + aptSlave.cntrSyncSrc = APT_CNTR_SYNC_SRC_SYNCIN; + HAL_APT_SlaveSyncInit(&g_apt2, &aptSlave); + +} + +void SystemInit(void) +{ + IOConfig(); + DMA_Init(); + UART0_Init(); + ACMP0_Init(); + APT0_Init(); + APT1_Init(); + APT2_Init(); + ADC0_Init(); + PGA0_Init(); + PGA1_Init(); + TIMER0_Init(); + TIMER1_Init(); + GPIO_Init(); + + APT_SyncMasterInit(); + APT_SyncSlaveInit(); + + /* USER CODE BEGIN system_init */ + /* USER CODE END system_init */ +} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/E7050_FOC/user/main.c b/vendor/yibaina_3061M/demo/E7050_FOC/user/main.c new file mode 100644 index 00000000..d6ab767c --- /dev/null +++ b/vendor/yibaina_3061M/demo/E7050_FOC/user/main.c @@ -0,0 +1,71 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file main.c + * @author MCU Driver Team + * @brief Main program body. + * @date 2025-04-14 14:05:10 + */ + +#include "typedefs.h" +#include "feature.h" +#include "mcs_motor_process.h" +#include "main.h" +/* USER CODE BEGIN 0 */ +/* USER CODE 区域内代码不会被覆盖,区域外会被生成的默认代码覆盖(其余USER CODE 区域同理) */ +/* 建议用户放置头文件 */ +/* USER CODE END 0 */ +ACMP_Handle g_acmp0; +PGA_Handle g_pga0; +PGA_Handle g_pga1; +TIMER_Handle g_timer0; +TIMER_Handle g_timer1; +UART_Handle g_uart0; +APT_Handle g_apt0; +APT_Handle g_apt1; +APT_Handle g_apt2; +ADC_Handle g_adc0; +DMA_Handle g_dmac; +GPIO_Handle g_gpio2; +GPIO_Handle g_gpio1; +/* USER CODE BEGIN 1 */ +/* 建议用户定义全局变量、结构体、宏定义或函数声明等 */ +/* USER CODE END 1 */ + +int main(void) +{ + /* USER CODE BEGIN 2 */ + /* 建议用户放置初始化代码或启动代码等 */ + /* USER CODE END 2 */ + MotorMainProcess(); + /* USER CODE BEGIN 3 */ + /* 建议用户放置初始配置代码 */ + /* USER CODE END 3 */ + while (1) { + /* USER CODE BEGIN 4 */ + /* 建议用户放置周期性执行代码 */ + /* USER CODE END 4 */ + } + /* USER CODE BEGIN 5 */ + /* 建议用户放置代码流程 */ + /* USER CODE END 5 */ + return BASE_STATUS_OK; +} + +/* USER CODE BEGIN 6 */ +/* 建议用户放置自定义函数 */ +/* USER CODE END 6 */ \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_carrier.h b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_carrier.h new file mode 100644 index 00000000..dea3ad66 --- /dev/null +++ b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_carrier.h @@ -0,0 +1,142 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_carrier.h + * @author MCU Algorithm Team + * @brief This file provides functions declaration for carrier interrupt processing function. + */ +#ifndef McuMagicTag_MCS_CARRIER_H +#define McuMagicTag_MCS_CARRIER_H + +#include "mcs_status.h" +#include "mcs_mtr_param.h" +#include "mcs_svpwm.h" +#include "mcs_curr_ctrl.h" +#include "mcs_if_ctrl.h" +#include "mcs_ramp_mgmt.h" +#include "mcs_spd_ctrl.h" +#include "mcs_fosmo.h" +#include "mcs_smo_4th.h" +#include "mcs_pll.h" +#include "mcs_startup.h" +#include "mcs_r1_svpwm.h" +#include "mcs_fw_ctrl.h" +#include "mcs_prot_user.h" + +typedef void (*MCS_ReadCurrUvwCb)(UvwAxis *CurrUvw); +typedef void (*MCS_SetPwmDutyCb)(UvwAxis *dutyUvwLeft, UvwAxis *dutyUvwRight); +typedef void (*MCS_SetADCTriggerTimeCb)(unsigned short cntCmpSOCA, unsigned short cntCmpSOCB); + +/** + * @brief motor control FSM state define. + * @details motor control FSM state type: + * + FSM_IDLE -- IDLE state, system startup default. + * + FSM_OFFSET_CALIB -- Self calibrate, for ADC init. + * + FSM_CAP_CHARGE -- IPM cap charge. + * + FSM_CLEAR -- Clear before every run. + * + FSM_LOCATE -- Rotor position pre-locate. + * + FSM_STARTUP -- Start up. + * + FSM_SWITCH -- Transition state, control from open to closed loop. + * + FSM_RUN -- Normal running state. + * + FSM_WAIT_STOP -- Wait stopping. + * + FSM_STOP -- Normal stop. + * + FSM_FAULT -- Fault state, waiting for user process. + */ +typedef enum { + FSM_IDLE = 0, + FSM_OFFSET_CALIB, + FSM_CAP_CHARGE, + FSM_CLEAR, + FSM_LOCATE, + FSM_STARTUP, + FSM_SWITCH, + FSM_RUN, + FSM_WAIT_STOP, + FSM_STOP, + FSM_FAULT +} FsmState; + +/** + * @brief Sampling mode. + */ +typedef enum { + DUAL_RESISTORS = 0, + SINGLE_RESISTOR = 1 +} SampleMode; + +/** + * @brief Motor control data structure + */ +typedef struct { + unsigned char motorStateFlag; /**< Motor start/stop status flag. */ + float spdCmdHz; /**< External input speed command value. */ + float axisAngle; /**< Angle of the synchronous coordinate system. */ + float spdRefHz; /**< Command value after speed ramp management. */ + float currCtrlPeriod; /**< current loop control period. */ + float adc0Compensate; /**< ADC0 softwaretrim compensate value. */ + float adc1Compensate; /**< ADC1 softwaretrim compensate value. */ + float udc; /**< Bus voltage. */ + float powerBoardTemp; /**< Power boart surface temperature. */ + unsigned short aptMaxcntCmp; /**< Apt Maximum Comparison Count. */ + float adcCurrCofe; /**< Adc current sampling cofeature. */ + + unsigned short sysTickCnt; /**< System Timer Tick Count. */ + unsigned short capChargeTickNum; /**< Bootstrap Capacitor Charge Tick Count. */ + volatile unsigned int msTickCnt; /**< Millisecond-level counter, which can be used in 1-ms and 5-ms tasks. */ + unsigned short msTickNum; /**< Number of ticks corresponding to 1 ms. */ + char obserType; /**< Set Observer Type. */ + char controlMode; /**< Set foc control or sixstep bldc control mode or others. */ + char spdAdjustMode; /**< Set speed adjust mode. */ + char uartConnectFlag; /**< Uart connect success flag. */ + short uartHeartDetCnt; /**< Uart connect heart detect count. */ + float uartTimeStamp; /**< Uart data time stamp. */ + SysStatusReg statusReg; /**< System status. */ + FsmState stateMachine; /**< Motor Control State Machine. */ + + SampleMode sampleMode; /**< Current sampling mode setting: single resistor, double resistor. */ + MOTOR_Param mtrParam; /**< Motor parameters. */ + FOSMO_Handle smo; /**< SMO observer handle. */ + SMO4TH_Handle smo4th; /**< SMO 4th observer handle. */ + IF_Handle ifCtrl; /**< I/F control handle. */ + SVPWM_Handle sv; /**< Dual-resistance/three-resistance sampling SVPWM Handle. */ + R1SVPWM_Handle r1Sv; /**< Single-resistance phase-shifted SVPWM handle. */ + RMG_Handle spdRmg; /**< Ramp management struct for the speed controller input reference. */ + SPDCTRL_Handle spdCtrl; /**< Speed loop Control Handle. */ + CURRCTRL_Handle currCtrl; /**< Current loop control handle. */ + STARTUP_Handle startup; /**< Startup Switch Handle. */ + FW_Handle fw; /**< Flux-Weakening Handle. */ + + DqAxis idqRef; /**< Command value of the dq axis current. */ + UvwAxis currUvw; /**< Three-phase current sampling value. */ + AlbeAxis iabFbk; /**< αβ-axis current feedback value. */ + DqAxis idqFbk; /**< Current feedback value of the dq axis. */ + DqAxis vdqRef; /**< Current loop output dq voltage. */ + AlbeAxis vabRef; /**< Current loop output voltage αβ. */ + UvwAxis dutyUvw; /**< UVW three-phase duty cycle. */ + UvwAxis dutyUvwLeft; /**< Single Resistor UVW Three-Phase Left Duty Cycle. */ + UvwAxis dutyUvwRight; /**< Single Resistor UVW Three-Phase Right Duty Cycle. */ + + MCS_ReadCurrUvwCb readCurrUvwCb; /**< Read current callback function. */ + MCS_SetPwmDutyCb setPwmDutyCb; /**< Set the duty cycle callback function. */ + MCS_SetADCTriggerTimeCb setADCTriggerTimeCb; /**< Sets the ADC trigger point callback function. */ + + MotorProtStatus_Handle prot; /**< Motor protect status detection flag bit handle. */ +} MTRCTRL_Handle; + +void MCS_CarrierProcess(MTRCTRL_Handle *mtrCtrl); + +#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_chip_config.h b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_chip_config.h new file mode 100644 index 00000000..38c00b5d --- /dev/null +++ b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_chip_config.h @@ -0,0 +1,76 @@ +/** + * @ Copyright (c) HiSilicon (Shanghai) Technologies Co., Ltd. 2022-2023. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_chip_config.h + * @author MCU Algorithm Team + * @brief This file provides config macros for ECMCU105H/ECBMCU201MPC app. + */ + +#ifndef McuMagicTag_MCS_CHIPCONFIG_H +#define McuMagicTag_MCS_CHIPCONFIG_H + +#include "feature.h" + +#if defined (CHIP_3061MNPICA) || defined (CHIP_3061MNPIKA) || defined (CHIP_3061MNNICA) || \ + defined (CHIP_3061MNNIKA) || defined (CHIP_3061MNPIC8) || defined(CHIP_3061MNNIC8) || \ + defined (CHIP_3061MNPIK8) || defined (CHIP_3061MNNIK8) + + #define ADCPTT_HANDLE g_adc0 + #define ADCRESIS_HANDLE g_adc0 + #define ADCUDC_HANDLE g_adc0 + #define ADCU_HANDLE g_adc0 + #define ADCW_HANDLE g_adc0 + #define LED1_HANDLE g_gpio2 + #define LED2_HANDLE g_gpio1 + #define LED2_PIN GPIO_PIN_0 + #define LED1_PIN GPIO_PIN_3 + #define ADCPTTSOCNUM ADC_SOC_NUM0 + #define ADCUSOCNUM ADC_SOC_NUM1 + #define ADCRESISSOCNUM ADC_SOC_NUM3 + #define ADCUDCSOCNUM ADC_SOC_NUM4 + #define ADCWSOCNUM ADC_SOC_NUM6 + + #define ADC0COMPENSATE 2037.0f + #define ADC1COMPENSATE 2027.0f + +#endif + +#if defined (CHIP_3065HRPIRZ) || defined (CHIP_3065ARPIRZ) || defined (CHIP_3066MNPIRH) || \ + defined (CHIP_3065PNPIRH) || defined (CHIP_3065PNPIRE) || defined (CHIP_3065PNPIRA) + + #define ADCU_HANDLE g_adc0 + #define ADCW_HANDLE g_adc1 + #define ADCRESIS_HANDLE g_adc2 + #define ADCUDC_HANDLE g_adc2 + #define ADCPTT_HANDLE g_adc2 + #define LED1_HANDLE g_gpio0 + #define LED2_HANDLE g_gpio0 + #define LED2_PIN GPIO_PIN_6 + #define LED1_PIN GPIO_PIN_7 + #define ADCPTTSOCNUM ADC_SOC_NUM0 + #define ADCRESISSOCNUM ADC_SOC_NUM1 + #define ADCUSOCNUM ADC_SOC_NUM8 + #define ADCWSOCNUM ADC_SOC_NUM8 + #define ADCUDCSOCNUM ADC_SOC_NUM14 + + #define ADC0COMPENSATE 2033.0f + #define ADC1COMPENSATE 2070.0f + +#endif + + +#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_ctlmode_config.h b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_ctlmode_config.h new file mode 100644 index 00000000..2a6f38cc --- /dev/null +++ b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_ctlmode_config.h @@ -0,0 +1,73 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_ctlmode_config.h + * @author MCU Algorithm Team + * @brief This file provides config macros for ECMCU105H app. + */ + + /* Define to prevent recursive inclusion ------------------------------------------------------- */ +#ifndef McuMagicTag_MCS_CTLMODECONFIG_H +#define McuMagicTag_MCS_CTLMODECONFIG_H + +#include "debug.h" +#include "typedefs.h" + +typedef enum { + FOC_STARTUP_IF = 0, + FOC_STARTUP_VF, + FOC_STARTUP_HFI +} MOTOR_STARTUPMODE_CONFIG; + +typedef enum { + FOC_OBSERVERTYPE_SMO1TH = 0, + FOC_OBSERVERTYPE_SMO1TH_PLL, + FOC_OBSERVERTYPE_SMO4TH, + FOC_OBSERVERTYPE_SMO4TH_PLL, + FOC_OBSERVERTYPE_LUNBORG, + FOC_OBSERVERTYPE_FLUX +} MOTOR_OBSERVERTYPE_CONFIG; + +typedef enum { + FOC_CONTROLMODE_SPEED = 0, + FOC_CONTROLMODE_TORQUE +} MOTOR_CONTROLMODE_CONFIG; + +typedef enum { + FOC_CURQAXISPID_PARAMS = 0, + FOC_CURDAXISPID_PARAMS, + FOC_SPDPID_PARAMS +} MOTOR_PID_SET; + +typedef enum { + MOTOR_PARAMS_BASE = 0, + MOTOR_PARAMS_SPECIAL, + MOTOR_PARAMS_BOARD +} MOTOR_PARAMS_SET; + +typedef enum { + CUST_SPEED_ADJUST = 0, + HOST_SPEED_ADJUST +} MODE_ADSPEED_CONFIG; + +typedef enum { + CONNECTING = 0, + CONNECTED, + DISCONNECT +} UART_STATUS; + +#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_motor_process.h b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_motor_process.h new file mode 100644 index 00000000..783bf214 --- /dev/null +++ b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_motor_process.h @@ -0,0 +1,45 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_motor_process.h + * @author MCU Algorithm Team + * @brief This file provides motor sample functions declaration for ECMCU105H board. + */ +#ifndef McuMagicTag_MCS_MOTOR_PROCESS_H +#define McuMagicTag_MCS_MOTOR_PROCESS_H + +/** + * @brief phase sequence. + */ +typedef enum { + PHASE_U = 0, + PHASE_V, + PHASE_W, + PHASE_MAX_NUM +} MCS_PhaseEnum; + +/** + * @brief key state. + */ +typedef enum { + KEY_DOWN = 0, + KEY_UP = 1, +} KEY_State; + +int MotorMainProcess(void); + +#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_status.h b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_status.h new file mode 100644 index 00000000..19510c78 --- /dev/null +++ b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_status.h @@ -0,0 +1,188 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_status.h + * @author MCU Algorithm Team + * @brief This file provides functions declaration of system status. + */ + +/* Define to prevent recursive inclusion ------------------------------------------------------- */ +#ifndef McuMagicTag_MCS_STATUS_H +#define McuMagicTag_MCS_STATUS_H + +/* Includes ------------------------------------------------------------------------------------ */ +#include "typedefs.h" +#include "mcs_assert.h" + +/* Typedef definitions ------------------------------------------------------------------------- */ +/** + * @brief System status define + */ +typedef union { + unsigned short all; + struct { + unsigned short cmdStart : 1; /**< Indicates that a start system command has been received. */ + unsigned short cmdStop : 1; /**< Indicates that a stop system command has been received. */ + unsigned short isRunning : 1; /**< Indicates that the system is running (enable signal) */ + unsigned short sysError : 1; /**< Indicates that the system reports an error. */ + unsigned short poweron : 1; /**< Indicates that the power-on initialization phase is complete. */ + unsigned short capcharge : 1; /**< Indicates that the bootstrap capacitor charging phase is complete. */ + unsigned short adczero : 1; /**< The current sampling point is reset to zero after power-on. */ + } Bit; +} SysStatusReg; + +/** + * @brief Get status of Bit cmdStart. + * @param sysStatus System status register handle. + * @retval Status of Bit cmdStart. + */ +static inline bool SysGetCmdStart(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + bool ret; + ret = (sysStatus->Bit.cmdStart == 1) ? true : false; + return ret; +} + +/** + * @brief Set Bit cmdStart. + * @param sysStatus System status register handle. + * @retval None. + */ +static inline void SysCmdStartSet(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + sysStatus->Bit.cmdStart = 1; +} + +/** + * @brief Clear Bit cmdStart. + * @param handle System status register handle. + * @retval None. + */ +static inline void SysCmdStartClr(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + sysStatus->Bit.cmdStart = 0; +} + +/** + * @brief Get status of Bit cmdStop. + * @param sysStatus System status register handle. + * @retval Status of Bit cmdStart. + */ +static inline bool SysGetCmdStop(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + bool ret; + ret = (sysStatus->Bit.cmdStop == 1) ? true : false; + return ret; +} + +/** + * @brief Set Bit cmdStop. + * @param sysStatus System status register handle. + * @retval None. + */ +static inline void SysCmdStopSet(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + sysStatus->Bit.cmdStop = 1; +} + +/** + * @brief Clear Bit cmdStop. + * @param sysStatus System status register handle. + * @retval None. + */ +static inline void SysCmdStopClr(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + sysStatus->Bit.cmdStop = 0; +} + +/** + * @brief Get status of Bit isRunning. + * @param sysStatus System status register handle. + * @retval Status of Bit isRunning. + */ +static inline bool SysIsRunning(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + bool ret; + ret = (sysStatus->Bit.isRunning == 1) ? true : false; + return ret; +} + +/** + * @brief Set Bit isRuning. + * @param sysStatus System status register handle. + * @retval None. + */ +static inline void SysRunningSet(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + sysStatus->Bit.isRunning = 1; +} + +/** + * @brief Clear Bit isRuning. + * @param sysStatus System status register handle. + * @retval None. + */ +static inline void SysRunningClr(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + sysStatus->Bit.isRunning = 0; +} + +/** + * @brief Get status of Bit sysError. + * @param sysStatus System status register handle. + * @retval Status of Bit sysError. + */ +static inline bool SysIsError(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + bool ret; + ret = (sysStatus->Bit.sysError == 1) ? true : false; + return ret; +} + +/** + * @brief Set Bit sysError. + * @param sysStatus System status register handle. + * @retval None. + */ +static inline void SysErrorSet(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + sysStatus->Bit.sysError = 1; +} + +/** + * @brief Clear Bit sysError. + * @param sysStatus System status register handle. + * @retval None. + */ +static inline void SysErrorClr(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + sysStatus->Bit.sysError = 0; +} + +#endif diff --git a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_user_config.h b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_user_config.h new file mode 100644 index 00000000..8d4cb216 --- /dev/null +++ b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_user_config.h @@ -0,0 +1,108 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_user_config.h + * @author MCU Algorithm Team + * @brief This file provides functions declaration of user config parameters. + */ +#ifndef McuMagicTag_MCS_CONFIG_H +#define McuMagicTag_MCS_CONFIG_H + +#include "debug.h" +#include "typedefs.h" + +#define SMO4TH + +#define SYSTICK_PERIOD_US 500u /* systick period. */ + +#define INV_CAP_CHARGE_MS 3u /* Capacitor charging time. */ + +#define INV_VOLTAGE_BUS 24.0f /* Bus voltage, V. */ + +#define CTRL_CURR_PERIOD 0.0001f /* carrier ISR period, 100us. */ +#define CTRL_SYSTICK_PERIOD 0.0005f /* systick control period, 500us. */ + +/* Duty of sample window, the real time is 0.06*50us = 3us. */ +#define SAMPLE_WINDOW_DUTY 0.06f + +/* Duty of sample point shift as flip point, the real time is 0.008*50us = 0.4us. */ +#define SAMPLE_POINT_SHIFT 0.008f + +/* Sampling resistance 200mOhm 0.0013295 */ +#define ADC_CURR_COFFI 0.0013295f /* 3.3/4096/3.03/0.2 pga: 3.03, 200mohm. */ + +#define APT_U APT0_BASE /* Base address of U phase APT module. */ +#define APT_V APT1_BASE /* Base address of V phase APT module. */ +#define APT_W APT2_BASE /* Base address of W phase APT module. */ + +/* FOSMO */ +#define FOSMO_GAIN 4.0f /* SMO gain */ +#define FOSMO_LAMBDA 2.0f /* SMO coefficient of cut-off frequency, its value = lambda * we. */ +#define FOSMO_EMF_CUTOFF_FREQ 2.0f /* SMO back emf cutoff frequency. */ +#define SPEED_FILTER_CUTOFF_FREQUENCY 40.0f /* SMO speed cutoff frequency of speed filter. */ +#define FOSMO_PLL_BDW 30.0f /* SMO PLL bandwidth. */ + +/* SMO4TH */ +#define SMO4TH_PLL_BDW 30.0f /* SMO4TH PLL Bandwidth. */ +#define SMO4TH_KD 300.0f /* SMO4TH parameters KD. */ +#define SMO4TH_KQ 600.0f /* SMO4TH parameters KQ. */ +#define SMO4TH_SPD_FILTER_CUTOFF_FREQ 40.0f /* SMO4TH speed cutoff frequency of speed filter. */ + +/* User_Commond */ +#define CTRL_IF_CURR_AMP_A 0.7f /* IF control current amplitude. */ +#define USER_TARGET_SPD_HZ 100.0f /* User-defined target speed value. */ +#define USER_SWITCH_SPDBEGIN_HZ 30.0f /* Start of handover interval. */ +#define USER_SWITCH_SPDEND_HZ (USER_SWITCH_SPDBEGIN_HZ + 3.0f) /* End of handover period. */ +#define USER_MAX_SPD_HZ 29.0f /* User-defined maximum speed value. */ +#define USER_MIN_SPD_HZ 5.0f /* User-defined minimum speed value. */ +#define USER_SPD_SLOPE 50.0f /* slope of velocity change. */ +#define USER_CURR_SLOPE (CTRL_IF_CURR_AMP_A * 10.0f) /* Current change slope. */ + +/* PID PARAMS */ +#define CURRQAXIS_KP 5.023202f /* Current loop Q axis Kp. */ +#define CURRQAXIS_KI 20612.84f /* Current loop Q axis Ki. */ +#define CURRDAXIS_KP 3.477114f /* Current loop D axis Kp. */ +#define CURRDAXIS_KI 20612.84f /* Current loop D axis Ki. */ +/* Current loop PID output lower limit. */ +#define CURR_LOWERLIM (-INV_VOLTAGE_BUS * ONE_DIV_SQRT3 * 0.98f) +/* Current loop PID output upper limit. */ +#define CURR_UPPERLIM (INV_VOLTAGE_BUS * ONE_DIV_SQRT3 * 0.98f) + +#define SPD_KP 0.00505f /* Speed loop Kp. */ +#define SPD_KI 0.012f /* Speed loop Ki. */ +#define SPD_LOWERLIM (-0.105f) /* Speed loop PID output lower limit. */ +#define SPD_UPPERLIM 0.105f /* Speed loop PID output upper limit. */ + +/* MOTOR PARAMS */ +/* Np, Rs, Ld, Lq, Psif, J, Nmax, Currmax, PPMR, zShift. */ +/* mtrPsif & mtrJ parameter is not used in this project, temporarily set to 0. */ +#define MOTORPARAM_DEFAULTS { \ + .mtrNp = 14, \ + .mtrRs = 10.1f, \ + .mtrLd = 0.010f, \ + .mtrLq = 0.010f, \ + .mtrPsif = 0.0f, \ + .mtrJ = 0.0f, \ + .maxElecSpd = 29.0f, \ + .maxCurr = 1.0f, \ + .busVolt = INV_VOLTAGE_BUS, \ +} + +/* Bus voltage sampling conversion factor. */ +#define ADC_UDC_COFFI 0.01289f /* 0.01289 = 3.3/4096*192/12. */ + +#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_cmm.c b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_cmm.c new file mode 100644 index 00000000..99766bdf --- /dev/null +++ b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_cmm.c @@ -0,0 +1,43 @@ +/** + * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_prot_cmm.c + * @author MCU Algorithm Team + * @brief This file contains protection common api definition. + */ + +/* Includes ------------------------------------------------------------------------------------ */ +#include "mcs_prot_cmm.h" +#include "apt.h" +#include "mcs_assert.h" + +/** + * @brief Safty-pulse-off function execution to turn off all the power devices. + * @param aptAddr Three-phase APT address pointer. + * @retval None. + */ +void ProtSpo_Exec(APT_RegStruct **aptAddr) +{ + MCS_ASSERT_PARAM(aptAddr != NULL); + /**< Turn off all the six power devices of the inverter. */ + for (unsigned int i = 0; i < MOTOR_PHASE_NUMBER; i++) { + APT_RegStruct *aptx = (APT_RegStruct *)(aptAddr[i]); + DCL_APT_EnableSwContPWMAction(aptx, APT_PWM_CHANNEL_A); + DCL_APT_EnableSwContPWMAction(aptx, APT_PWM_CHANNEL_B); + DCL_APT_ForcePWMOutputLow(aptx); + } +} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_cmm.h b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_cmm.h new file mode 100644 index 00000000..104a5dec --- /dev/null +++ b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_cmm.h @@ -0,0 +1,136 @@ +/** + * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_prot_cmm.h + * @author MCU Algorithm Team + * @brief This file contains protection function common data struct and api declaration. + */ + +/* Define to prevent recursive inclusion ------------------------------------------------------- */ +#ifndef McuMagicTag_MCS_PROT_CMM_H +#define McuMagicTag_MCS_PROT_CMM_H + +#include "typedefs.h" +#include "apt_ip.h" + +/* Macro definitions --------------------------------------------------------------------------- */ +#define MOTOR_PHASE_NUMBER (3) + +#define PROT_VAL_THRESHOLD_NUMS (4) +#define PROT_VAL_THRESHOLD_0 (0) +#define PROT_VAL_THRESHOLD_1 (1) +#define PROT_VAL_THRESHOLD_2 (2) +#define PROT_VAL_THRESHOLD_3 (3) + +#define PROT_LIMIT_TIME_NUMS (3) +#define PROT_LIMIT_TIME_0 (0) +#define PROT_LIMIT_TIME_1 (1) +#define PROT_LIMIT_TIME_2 (2) + +#define MOTOR_PHASE_NUMBER (3) + +/**< Motor error status definition. */ +typedef union { + int all; + struct { + unsigned short overCurrErr : 1; /**< Indicates that phase current(s) is over protected value. */ + unsigned short overVoltErr : 1; /**< Indicates that dc-link voltage is over protected value. */ + unsigned short lowerVoltErr : 1; /**< Indicates that dc-link voltage is lower than protected value */ + unsigned short overIpmTempErr : 1; /**< Indicates that IPM temperature is over protected value. */ + unsigned short revRotErr : 1; /**< Indicates that motor negtive direction. */ + unsigned short motorStalling : 1; /**< Indicates that rotor is stalling. */ + unsigned short overMotorTempErr : 1; /**< Indicates that three phase currents is out-of-balance. */ + unsigned short posSnsrCommsErr : 1; /**< Indicates that position sensor communication is lost with MCU. */ + unsigned short posSnsrFuncErr : 1; /**< Indicates that position sensor reports function error. */ + unsigned short posSnsrCalibrErr : 1; /**< Indicates that position sensor fails to calibrate itself. */ + unsigned short currOutOfBalance : 1; /**< Indicates that the rotor is reverse rotation.*/ + unsigned short phsOpenErr : 1; /**< Indicates that phase winding(s) is open. */ + unsigned short phsU : 1; /**< Indicates that u phase fails when phsOpenErr occurs. */ + unsigned short phsV : 1; /**< Indicates that v phase fails when phsOpenErr occurs. */ + unsigned short phsW : 1; /**< Indicates that w phase fails when phsOpenErr occurs. */ + unsigned short multiPhs : 1; /**< Indicates that multi-phases fail when phsOpenErr occurs.*/ + } Bit; +} MotorErrStatusReg; + +/**< Protection Status Bit Definition */ +typedef enum { + OCP_ERR_BIT, + OVP_ERR_BIT, + LVP_ERR_BIT, + OTP_IPM_ERR_BIT, + OTP_MOTOR_ERR_BIT, + STALLING_ERR_BIT, + CURR_OUT_BALANCE_ERR_BIT, + POS_COMMS_ERR_BIT, + POS_FUNC_ERR_BIT, + POS_CALIB_ERR_BIT, + REV_ROT_ERR_BIT, + PHS_OPEN_ERR_BIT, + PHS_U_ERR_BIT, + PHS_V_ERR_BIT, + PHS_W_ERR_BIT, + PHS_MULTI_ERR_BIT, +} PROT_ErrBit; + +/**< Motor error protection level. */ +typedef enum { + PROT_LEVEL_0 = 0, + PROT_LEVEL_1, + PROT_LEVEL_2, + PROT_LEVEL_3, + PROT_LEVEL_4 /**< The greater level number, the severe error is. */ +} PROT_Level; + +/** + * @brief Obtains the status of a bit of data. + * @param data data. + * @param bits Number of digits. + * @retval Bit status. + */ +static inline bool GetBit(int data, unsigned short bit) +{ + bool ret; + ret = ((data >> bit) & 1); + return ret; +} + +/** + * @brief Sets the status of a bit of data. + * @param data data. + * @param bit The setted bit. + * @retval None. + */ +static inline void SetBit(int *data, unsigned char bit) +{ + *data |= (1 << bit); +} + +/** + * @brief Clear the status of a bit of data. + * @param data data. + * @param bit The Clear bit. + * @retval None. + */ +static inline void ClearBit(int *data, unsigned char bit) +{ + *data &= ~(1 << bit); +} + +/**< Protection action. */ +void ProtSpo_Exec(APT_RegStruct **aptAddr); + +#endif diff --git a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user.c b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user.c new file mode 100644 index 00000000..e97876b7 --- /dev/null +++ b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user.c @@ -0,0 +1,137 @@ +/** + * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_prot_user.c + * @author MCU Algorithm Team + * @brief This file contains user protection data struct, inquiry api and initialization api definition. + */ + +/* Includes ------------------------------------------------------------------------------------ */ +#include "mcs_prot_user.h" +#include "mcs_prot_user_config.h" +#include "mcs_assert.h" + +/** + * @brief Get motor over current error status. + * @param motorErrStatus Motor error status. + * @retval Error status. + */ +bool IsMotorOverCurrErr(MotorErrStatusReg motorErrStatus) +{ + if (motorErrStatus.Bit.overCurrErr) { + return true; + } else { + return false; + } +} + +/** + * @brief Get motor over dc-link voltage error status. + * @param motorErrStatus Motor error status. + * @retval Error status. + */ +bool IsDcLinkOverVoltErr(MotorErrStatusReg motorErrStatus) +{ + if (motorErrStatus.Bit.overVoltErr) { + return true; + } else { + return false; + } +} + +/** + * @brief Get motor lower dc-link voltage error status. + * @param motorErrStatus Motor error status. + * @retval Error status. + */ +bool IsDcLinkLowerVoltErr(MotorErrStatusReg motorErrStatus) +{ + if (motorErrStatus.Bit.lowerVoltErr) { + return true; + } else { + return false; + } +} + +/** + * @brief Get motor over Ipm temperature error status. + * @param motorErrStatus Motor error status. + * @retval Error status. + */ +bool IsIpmOverTempErr(MotorErrStatusReg motorErrStatus) +{ + if (motorErrStatus.Bit.overIpmTempErr) { + return true; + } else { + return false; + } +} + +/** + * @brief Get motor over Motor temperature error status. + * @param motorErrStatus Motor error status. + * @retval Error status. + */ +bool IsMotorOverTempErr(MotorErrStatusReg motorErrStatus) +{ + if (motorErrStatus.Bit.overMotorTempErr) { + return true; + } else { + return false; + } +} + +/** + * @brief Get motor stalling error status. + * @param motorErrStatus Motor error status. + * @retval Error status. + */ +bool IsMotorStallingErr(MotorErrStatusReg motorErrStatus) +{ + if (motorErrStatus.Bit.motorStalling) { + return true; + } else { + return false; + } +} + +/** + * @brief Clear the motor error status. + * @param motorProt Motor protection handle. + * @retval Error status. + */ +void ClearMotorErrStatus(MotorProtStatus_Handle *motorProt) +{ + MCS_ASSERT_PARAM(motorProt != NULL); + /* Clear the motor error status. */ + motorProt->motorErrStatus.all = 0x00; + OCP_Clear(&motorProt->ocp); + OVP_Clear(&motorProt->ovp); + LVP_Clear(&motorProt->lvp); + OTP_Clear(&motorProt->otp); +} + +/** + * @brief Motor protection function initialization. + * @param motorProt Motor protection handle. + * @retval Error status. + */ +void MotorProt_Init(MotorProtStatus_Handle *motorProt) +{ + MCS_ASSERT_PARAM(motorProt != NULL); + motorProt->motorErrStatus.all = 0x00; +} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user.h b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user.h new file mode 100644 index 00000000..365110e1 --- /dev/null +++ b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user.h @@ -0,0 +1,54 @@ +/** + * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_prot_user.h + * @author MCU Algorithm Team + * @brief This file contains user protection data struct, inquiry api and initialization api declaration. + */ + +/* Define to prevent recursive inclusion ------------------------------------------------------- */ +#ifndef McuMagicTag_MCS_PROT_USER_H +#define McuMagicTag_MCS_PROT_USER_H + +/* Includes ------------------------------------------------------------------------------------ */ +#include "mcs_prot_cmm.h" +#include "mcs_curr_prot.h" +#include "mcs_dc_volt_prot.h" +#include "mcs_temp_prot.h" +#include "mcs_motor_stalling.h" +#include "typedefs.h" + +typedef struct { + MotorErrStatusReg motorErrStatus; /**< Motor error status. */ + OCP_Handle ocp; /**< Over current protection. */ + OVP_Handle ovp; /**< Over dc-link voltage protection. */ + LVP_Handle lvp; /**< Lower dc-link voltage protection. */ + OTP_Handle otp; /**< Over IPM temperature protection. */ + STP_Handle stall; /**< Motor stalling protection. */ +} MotorProtStatus_Handle; + +void MotorProt_Init(MotorProtStatus_Handle *motorProt); + +/**< Inquiry motor error status */ +bool IsMotorOverCurrErr(MotorErrStatusReg motorErrStatus); +bool IsDcLinkOverVoltErr(MotorErrStatusReg motorErrStatus); +bool IsDcLinkLowerVoltErr(MotorErrStatusReg motorErrStatus); +bool IsIpmOverTempErr(MotorErrStatusReg motorErrStatus); +bool IsMotorOverTempErr(MotorErrStatusReg motorErrStatus); +bool IsMotorStallingErr(MotorErrStatusReg motorErrStatus); +void ClearMotorErrStatus(MotorProtStatus_Handle *motorProt); +#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user_config.h b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user_config.h new file mode 100644 index 00000000..832cd1be --- /dev/null +++ b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user_config.h @@ -0,0 +1,125 @@ +/** + * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_prot_user_config.h + * @author MCU Algorithm Team + * @brief This file contans user macro definition of the protection function. + */ + +/* Define to prevent recursive inclusion ------------------------------------------------------- */ +#ifndef McuMagicTag_MCS_PROT_USER_CONFIG_H +#define McuMagicTag_MCS_PROT_USER_CONFIG_H + +/* Macro definitions --------------------------------------------------------------------------- */ +/**< COMMON */ +/**< Peak phase current (A) of the motor or IPM under continuous operations. */ +#define PROT_MOTOR_RATED_CURR (1.0f) +/**< Only several continuous fault detection can trigger error status. */ +#define PROT_CNT_LIMIT (100) +/**< Only several contunuous none fault dectection can trigger elimination of error status. */ +#define RECY_CNT_LIMIT (10000) +/**< Only several contunuous none fault dectection can trigger elimination of error status. */ +#define OVER_VOLT_RECY_CNT_LIMIT (100) +/**< Only several contunuous none fault dectection can trigger elimination of error status. */ +#define LOWER_VOLT_RECY_CNT_LIMIT (100) + +/**< Over current protection */ +/**< Over current trigger value (A) when in level 1. */ +#define PROT_OVER_CURR_POW_DN1 (2.0f * PROT_MOTOR_RATED_CURR) +/**< Over current trigger value (A) when in level 2. */ +#define PROT_OVER_CURR_POW_DN2 (2.0f * PROT_MOTOR_RATED_CURR) +/**< Over current trigger value (A) when in level 3. */ +#define PROT_OVER_CURR_POW_DN3 (2.0f * PROT_MOTOR_RATED_CURR) +/**< Over current trigger value (A) when in level 4. */ +#define PROT_OVER_CURR_POW_OFF (2.0f * PROT_MOTOR_RATED_CURR) +#define PROT_OVER_CURR_RECY_DELTA (0.0035f) /**< Current gap (A) when recovers from protection status. */ +#define PROT_OVER_CURR_LIMIT1_TIME_SEC (30.0f) /**< 20% overload can last maximum time: 30 sec. */ +#define PROT_OVER_CURR_LIMIT2_TIME_SEC (10.0f) /**< 20% overload can last maximum time: 10 sec. */ +#define PROT_OVER_CURR_LIMIT3_TIME_SEC (2.0f) /**< 20% overload can last maximum time: 2 sec. */ + +/**< Over voltage protection */ +#define PROT_OVER_VOLT_BRK_ON1 (26.0f) /**<处于1级时,直流链路电压触发值(V)过高*/ +#define PROT_OVER_VOLT_BRK_ON2 (27.0f) /**< Over dc-link voltage trigger value (V) when in level 2. */ +#define PROT_OVER_VOLT_BRK_ON3 (28.0f) /**< Over dc-link voltage trigger value (V) when in level 3. */ +#define PROT_OVER_VOLT_BRK_ALL (30.0f) /**< Over dc-link voltage trigger value (V) when in level 4. */ +#define PROT_OVER_VOLT_RECY_DELTA (0.5f) /**<从保护状态恢复时的电压间隙(V)*/ +#define PROT_OVER_VOLT_LIMIT1_TIME_SEC (5.0f) /** 1; One-cycle mode: < 0.5 */ +#define POS_SNSR_IPD_INJ_PERIOD (4) + +#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_curr_prot.c b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_curr_prot.c new file mode 100644 index 00000000..da4e56f8 --- /dev/null +++ b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_curr_prot.c @@ -0,0 +1,267 @@ +/** + * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_curr_prot.c + * @author MCU Algorithm Team + * @brief This file contains current protecion api definition. + */ + +/* Includes ------------------------------------------------------------------------------------ */ +#include "mcs_curr_prot.h" +#include "mcs_math.h" +#include "mcs_prot_user_config.h" +#include "mcs_assert.h" + +/** + * @brief Initilization over current protection function. + * @param ocp Over current protection handle. + * @param ts Ctrl period (s). + * @retval None. + */ +void OCP_Init(OCP_Handle *ocp, float ts) +{ + MCS_ASSERT_PARAM(ocp != NULL); + MCS_ASSERT_PARAM(ts > 0.0f); + ocp->ts = ts; + OCP_Clear(ocp); + + ocp->protCntLimit = PROT_CNT_LIMIT; + ocp->recyCntLimit = RECY_CNT_LIMIT; + /* Configuring four levels of current protection thresholds. */ + ocp->protValThr[PROT_VAL_THRESHOLD_0] = PROT_OVER_CURR_POW_DN1; + ocp->protValThr[PROT_VAL_THRESHOLD_1] = PROT_OVER_CURR_POW_DN2; + ocp->protValThr[PROT_VAL_THRESHOLD_2] = PROT_OVER_CURR_POW_DN3; + ocp->protValThr[PROT_VAL_THRESHOLD_3] = PROT_OVER_CURR_POW_OFF; + /* Configure the protection limit time. */ + ocp->protLimitTime[PROT_LIMIT_TIME_0] = PROT_OVER_CURR_LIMIT1_TIME_SEC; + ocp->protLimitTime[PROT_LIMIT_TIME_1] = PROT_OVER_CURR_LIMIT2_TIME_SEC; + ocp->protLimitTime[PROT_LIMIT_TIME_2] = PROT_OVER_CURR_LIMIT3_TIME_SEC; + ocp->recyDelta = PROT_OVER_CURR_RECY_DELTA; +} + +/** + * @brief Over current protection detection. + * @param ocp Over current protection handle. + * @param motorErrStatus Motor error status. + * @param idq DQ-axis feedback currents. + * @retval None. + */ +void OCP_Det(OCP_Handle *ocp, MotorErrStatusReg *motorErrStatus, DqAxis idq) +{ + MCS_ASSERT_PARAM(ocp != NULL); + MCS_ASSERT_PARAM(motorErrStatus != NULL); + /* Calculate current amplitude. */ + ocp->currAmp = Sqrt(idq.d * idq.d + idq.q * idq.q); + + /* Check if value goes over threshold for continuous cycles. */ + if (ocp->currAmp < ocp->protValThr[PROT_VAL_THRESHOLD_0]) { + ocp->protCnt = 0; + return; + } + + if (ocp->protCnt < ocp->protCntLimit) { + ocp->protCnt++; + return; + } + + /* Check which protection level should be activated. */ + /* Once enter error status, error status can only be changed by recover api. */ + /* protect level: level 4. */ + if (ocp->currAmp >= ocp->protValThr[PROT_VAL_THRESHOLD_3] && ocp->protLevel < PROT_LEVEL_4) { + ocp->protLevel = PROT_LEVEL_4; + motorErrStatus->Bit.overCurrErr = 1; + ocp->protCnt = 0; + return; + } + + /* Protect level: level 3. */ + if (ocp->currAmp >= ocp->protValThr[PROT_VAL_THRESHOLD_2] && ocp->protLevel < PROT_LEVEL_3) { + ocp->protLevel = PROT_LEVEL_3; + motorErrStatus->Bit.overCurrErr = 1; + ocp->protCnt = 0; + ocp->timer = 0.0f; + return; + } + + /* Protect level: level 2. */ + if (ocp->currAmp >= ocp->protValThr[PROT_VAL_THRESHOLD_1] && ocp->protLevel < PROT_LEVEL_2) { + ocp->protLevel = PROT_LEVEL_2; + motorErrStatus->Bit.overCurrErr = 1; + ocp->protCnt = 0; + ocp->timer = 0.0f; + return; + } + + /* Protect level: level 1. */ + if (ocp->currAmp >= ocp->protValThr[PROT_VAL_THRESHOLD_0] && ocp->protLevel < PROT_LEVEL_1) { + ocp->protLevel = PROT_LEVEL_1; + motorErrStatus->Bit.overCurrErr = 1; + ocp->protCnt = 0; + ocp->timer = 0.0f; + return; + } +} + +/** + * @brief Over current protection execution. + * @param ocp Over current protection handle. + * @param idqRef DQ-axis current references. + * @param aptAddr Three-phase APT address pointer. + * @retval None. + */ +void OCP_Exec(OCP_Handle *ocp, DqAxis *idqRef, APT_RegStruct **aptAddr) +{ + MCS_ASSERT_PARAM(ocp != NULL); + MCS_ASSERT_PARAM(idqRef != NULL); + MCS_ASSERT_PARAM(aptAddr != NULL); + + float id = idqRef->d; + float iq = idqRef->q; + float idqAmp = ocp->currAmp; + /* According to protect level, take corresponding action. */ + switch (ocp->protLevel) { + /* level 4: disable all PWM output. */ + case PROT_LEVEL_4: + /* Disable three-phase pwm output. */ + ProtSpo_Exec(aptAddr); + idqRef->d = 0.0f; + idqRef->q = 0.0f; + break; + + /* level 3: derate speed reference. */ + case PROT_LEVEL_3: + ocp->timer += ocp->ts; + if (ocp->timer > ocp->protLimitTime[PROT_LIMIT_TIME_2]) { + idqRef->d = id / idqAmp * PROT_MOTOR_RATED_CURR; + idqRef->q = iq / idqAmp * PROT_MOTOR_RATED_CURR; + } + break; + + /* level 2: derate speed reference. */ + case PROT_LEVEL_2: + ocp->timer += ocp->ts; + if (ocp->timer > ocp->protLimitTime[PROT_LIMIT_TIME_1]) { + idqRef->d = id / idqAmp * PROT_MOTOR_RATED_CURR; + idqRef->q = iq / idqAmp * PROT_MOTOR_RATED_CURR; + } + break; + + /* level 1: derate speed reference. */ + case PROT_LEVEL_1: + ocp->timer += ocp->ts; + if (ocp->timer > ocp->protLimitTime[PROT_LIMIT_TIME_0]) { + idqRef->d = id / idqAmp * PROT_MOTOR_RATED_CURR; + idqRef->q = iq / idqAmp * PROT_MOTOR_RATED_CURR; + } + break; + + /* level 0: take no protection action. */ + case PROT_LEVEL_0: + break; + + default: + break; + } +} + +/** + * @brief Over current protection recovery. + * @param ocp Over current protection handle. + * @param motorErrStatus Motor error status. + * @retval None. + */ +void OCP_Recy(OCP_Handle *ocp, MotorErrStatusReg *motorErrStatus) +{ + MCS_ASSERT_PARAM(ocp != NULL); + /* If not under error state, just return without any operation. */ + if (!motorErrStatus->Bit.overCurrErr) { + return; + } + + /* Calculate current amplitude. */ + float currAmp = ocp->currAmp; + + /* According to protection level, take corresponding recovery action. */ + switch (ocp->protLevel) { + /* level 4 */ + case PROT_LEVEL_4: + if (currAmp < ocp->protValThr[PROT_VAL_THRESHOLD_3] - ocp->recyDelta) { + ocp->recyCnt++; + if (ocp->recyCnt > ocp->recyCntLimit) { + ocp->protLevel = PROT_LEVEL_3; + ocp->recyCnt = 0; + } + } + break; + + /* level 3 */ + case PROT_LEVEL_3: + if (currAmp < ocp->protValThr[PROT_VAL_THRESHOLD_2] - ocp->recyDelta) { + ocp->recyCnt++; + if (ocp->recyCnt > ocp->recyCntLimit) { + ocp->protLevel = PROT_LEVEL_2; + ocp->recyCnt = 0; + } + } + break; + + /* level 2 */ + case PROT_LEVEL_2: + if (currAmp < ocp->protValThr[PROT_VAL_THRESHOLD_1] - ocp->recyDelta) { + ocp->recyCnt++; + if (ocp->recyCnt > ocp->recyCntLimit) { + ocp->protLevel = PROT_LEVEL_1; + ocp->recyCnt = 0; + } + } + break; + + /* level 1 */ + case PROT_LEVEL_1: + if (currAmp < ocp->protValThr[PROT_VAL_THRESHOLD_0] - ocp->recyDelta) { + ocp->recyCnt++; + if (ocp->recyCnt > ocp->recyCntLimit) { + ocp->protLevel = PROT_LEVEL_0; + ocp->recyCnt = 0; + } + } + break; + + /* level 0 */ + case PROT_LEVEL_0: + motorErrStatus->Bit.overCurrErr = 0; + break; + + default: + break; + } +} + +/** + * @brief Over current protection error status clear. + * @param ocp Over current protection handle. + * @retval None. + */ +void OCP_Clear(OCP_Handle *ocp) +{ + MCS_ASSERT_PARAM(ocp != NULL); + /* Clear the history value. */ + ocp->protCnt = 0; + ocp->protLevel = PROT_LEVEL_0; + ocp->recyCnt = 0; + ocp->timer = 0.0f; +} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_curr_prot.h b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_curr_prot.h new file mode 100644 index 00000000..e9916fe4 --- /dev/null +++ b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_curr_prot.h @@ -0,0 +1,52 @@ +/** + * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_curr_prot.h + * @author MCU Algorithm Team + * @brief This file contains current protection data struct and api declaration. + */ + +/* Define to prevent recursive inclusion ------------------------------------- */ +#ifndef McuMagicTag_MCS_CURR_PROT_H +#define McuMagicTag_MCS_CURR_PROT_H + +/* Includes ------------------------------------------------------------------------------------ */ +#include "mcs_prot_cmm.h" +#include "mcs_typedef.h" +#include "apt_ip.h" + +typedef struct { + unsigned int protCnt; + unsigned int recyCnt; + unsigned int protCntLimit; + unsigned int recyCntLimit; + float currAmp; + float protValThr[PROT_VAL_THRESHOLD_NUMS]; /* from low current level to high. */ + float protLimitTime[PROT_LIMIT_TIME_NUMS]; /* from level 3 to level 1. */ + float recyDelta; + float timer; + float ts; + PROT_Level protLevel; +} OCP_Handle; + +void OCP_Init(OCP_Handle *ocp, float ts); +void OCP_Det(OCP_Handle *ocp, MotorErrStatusReg *motorErrStatus, DqAxis idq); +void OCP_Exec(OCP_Handle *ocp, DqAxis *idqRef, APT_RegStruct **aptAddr); +void OCP_Recy(OCP_Handle *ocp, MotorErrStatusReg *motorErrStatus); +void OCP_Clear(OCP_Handle *ocp); + +#endif diff --git a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_dc_volt_prot.c b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_dc_volt_prot.c new file mode 100644 index 00000000..b0541a2e --- /dev/null +++ b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_dc_volt_prot.c @@ -0,0 +1,495 @@ +/** + * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_dc_volt_prot.c + * @author MCU Algorithm Team + * @brief This file contains dc-link voltage protection api declaration. + */ + +/* Includes ------------------------------------------------------------------------------------ */ +#include "mcs_dc_volt_prot.h" +#include "mcs_prot_user_config.h" +#include "mcs_assert.h" + +/** + * @brief Initilization over dc-link voltage protection function. + * @param ovp Over dc-link voltage protection handle. + * @param ts Ctrl period (s). + * @retval None. + */ +void OVP_Init(OVP_Handle *ovp, float ts) +{ + MCS_ASSERT_PARAM(ovp != NULL); + MCS_ASSERT_PARAM(ts > 0.0f); + ovp->ts = ts; + OVP_Clear(ovp); + ovp->protCntLimit = PROT_CNT_LIMIT; + ovp->recyCntLimit = OVER_VOLT_RECY_CNT_LIMIT; + /* Configuring four levels of overvoltage protection thresholds. */ + ovp->protValThr[PROT_VAL_THRESHOLD_0] = PROT_OVER_VOLT_BRK_ON1; + ovp->protValThr[PROT_VAL_THRESHOLD_1] = PROT_OVER_VOLT_BRK_ON2; + ovp->protValThr[PROT_VAL_THRESHOLD_2] = PROT_OVER_VOLT_BRK_ON3; + ovp->protValThr[PROT_VAL_THRESHOLD_3] = PROT_OVER_VOLT_BRK_ALL; + /* Configure the protection limit time. */ + ovp->protLimitTime[PROT_LIMIT_TIME_0] = PROT_OVER_VOLT_LIMIT1_TIME_SEC; + ovp->protLimitTime[PROT_LIMIT_TIME_1] = PROT_OVER_VOLT_LIMIT2_TIME_SEC; + ovp->protLimitTime[PROT_LIMIT_TIME_2] = PROT_OVER_VOLT_LIMIT3_TIME_SEC; + ovp->recyDelta = PROT_OVER_VOLT_RECY_DELTA; +} + +/** + * @brief Initilization lower dc-link voltage protection function. + * @param lvp Lower dc-link voltage protection handle. + * @param ts Ctrl period (s). + * @retval None. + */ +void LVP_Init(LVP_Handle *lvp, float ts) +{ + MCS_ASSERT_PARAM(lvp != NULL); + MCS_ASSERT_PARAM(ts > 0.0f); + + lvp->ts = ts; + LVP_Clear(lvp); + + lvp->protCntLimit = PROT_CNT_LIMIT; + lvp->recyCntLimit = LOWER_VOLT_RECY_CNT_LIMIT; + /* Configuring four levels of lower voltage protection thresholds. */ + lvp->protValThr[PROT_VAL_THRESHOLD_0] = PROT_LOWER_VOLT_POW_DN1; + lvp->protValThr[PROT_VAL_THRESHOLD_1] = PROT_LOWER_VOLT_POW_DN2; + lvp->protValThr[PROT_VAL_THRESHOLD_2] = PROT_LOWER_VOLT_POW_DN3; + lvp->protValThr[PROT_VAL_THRESHOLD_3] = PROT_LOWER_VOLT_POW_OFF; + /* Configure the protection limit time. */ + lvp->protLimitTime[PROT_LIMIT_TIME_0] = PROT_LOWER_VOLT_LIMIT1_TIME_SEC; + lvp->protLimitTime[PROT_LIMIT_TIME_1] = PROT_LOWER_VOLT_LIMIT2_TIME_SEC; + lvp->protLimitTime[PROT_LIMIT_TIME_2] = PROT_LOWER_VOLT_LIMIT3_TIME_SEC; + lvp->recyDelta = PROT_OVER_VOLT_RECY_DELTA; +} + +/** + * @brief Over dc-link voltage protection detection. + * @param ovp Over dc-link voltage protection handle. + * @param motorErrStatus Motor error status. + * @param udc DC-link voltage feedback (V). + * @retval None. + */ +void OVP_Det(OVP_Handle *ovp, MotorErrStatusReg *motorErrStatus, float udc) +{ + MCS_ASSERT_PARAM(ovp != NULL); + MCS_ASSERT_PARAM(motorErrStatus != NULL); + MCS_ASSERT_PARAM(udc > 0.0f); + /* Check if value goes over threshold for continuous cycles. */ + if (udc < ovp->protValThr[PROT_VAL_THRESHOLD_0]) { + ovp->protCnt = 0; + return; + } + + if (ovp->protCnt < ovp->protCntLimit) { + ovp->protCnt++; + return; + } + + /* Check which protection level should be activated. */ + /* Once enter error status, error status can only be changed by recover api. */ + /* protect level: level 4. */ + if (udc >= ovp->protValThr[PROT_VAL_THRESHOLD_3] && ovp->protLevel < PROT_LEVEL_4) { + ovp->protLevel = PROT_LEVEL_4; + motorErrStatus->Bit.overVoltErr = 1; + ovp->protCnt = 0; + return; + } + + /* Protect level: level 3. */ + if (udc >= ovp->protValThr[PROT_VAL_THRESHOLD_2] && ovp->protLevel < PROT_LEVEL_3) { + ovp->protLevel = PROT_LEVEL_3; + motorErrStatus->Bit.overVoltErr = 1; + ovp->protCnt = 0; + ovp->timer = 0.0f; + return; + } + + /* Protect level: level 2. */ + if (udc >= ovp->protValThr[PROT_VAL_THRESHOLD_1] && ovp->protLevel < PROT_LEVEL_2) { + ovp->protLevel = PROT_LEVEL_2; + motorErrStatus->Bit.overVoltErr = 1; + ovp->protCnt = 0; + ovp->timer = 0.0f; + return; + } + + /* Protect level: level 1. */ + if (udc >= ovp->protValThr[PROT_VAL_THRESHOLD_0] && ovp->protLevel < PROT_LEVEL_1) { + ovp->protLevel = PROT_LEVEL_1; + motorErrStatus->Bit.overVoltErr = 1; + ovp->protCnt = 0; + ovp->timer = 0.0f; + return; + } +} + +/** + * @brief Lower dc-link voltage protection detection. + * @param lvp Lower dc-link voltage protection handle. + * @param motorErrStatus Motor error status. + * @param udc DC-link voltage feedback (V). + * @retval None. + */ +void LVP_Det(LVP_Handle *lvp, MotorErrStatusReg *motorErrStatus, float udc) +{ + MCS_ASSERT_PARAM(lvp != NULL); + MCS_ASSERT_PARAM(motorErrStatus != NULL); + MCS_ASSERT_PARAM(udc > 0.0f); + /* Check if value goes over threshold for continuous cycles. */ + if (udc > lvp->protValThr[PROT_VAL_THRESHOLD_0]) { + lvp->protCnt = 0; + return; + } + + if (lvp->protCnt < lvp->protCntLimit) { + lvp->protCnt++; + return; + } + + /* Check which protection level should be activated. */ + /* Once enter error status, error status can only be changed by recover api. */ + /* protect level: level 4. */ + if (udc <= lvp->protValThr[PROT_VAL_THRESHOLD_3] && lvp->protLevel < PROT_LEVEL_4) { + lvp->protLevel = PROT_LEVEL_4; + motorErrStatus->Bit.lowerVoltErr = 1; + lvp->protCnt = 0; + return; + } + + /* Protect level: level 3. */ + if (udc <= lvp->protValThr[PROT_VAL_THRESHOLD_2] && lvp->protLevel < PROT_LEVEL_3) { + lvp->protLevel = PROT_LEVEL_3; + motorErrStatus->Bit.lowerVoltErr = 1; + lvp->protCnt = 0; + lvp->timer = 0.0f; + return; + } + + /* Protect level: level 2. */ + if (udc <= lvp->protValThr[PROT_VAL_THRESHOLD_1] && lvp->protLevel < PROT_LEVEL_2) { + lvp->protLevel = PROT_LEVEL_2; + motorErrStatus->Bit.lowerVoltErr = 1; + lvp->protCnt = 0; + lvp->timer = 0.0f; + return; + } + + /* Protect level: level 1. */ + if (udc <= lvp->protValThr[PROT_VAL_THRESHOLD_0] && lvp->protLevel < PROT_LEVEL_1) { + lvp->protLevel = PROT_LEVEL_1; + motorErrStatus->Bit.lowerVoltErr = 1; + lvp->protCnt = 0; + lvp->timer = 0.0f; + return; + } +} + +/** + * @brief Over dc-link voltage protection execution. + * @param ovp Over dc-link voltage protection handle. + * @param duty Brake loop output duty (0-1). + * @param aptAddr Three-phase APT address pointer. + * @retval None. + */ +void OVP_Exec(OVP_Handle *ovp, float *duty, APT_RegStruct **aptAddr) +{ + MCS_ASSERT_PARAM(ovp != NULL); + MCS_ASSERT_PARAM(duty != NULL); + MCS_ASSERT_PARAM(aptAddr != NULL); + /* According to protect level, take corresponding action. */ + switch (ovp->protLevel) { + /* level 4: brake loop duty maximum. */ + case PROT_LEVEL_4: + *duty = PROT_OVER_VOLT_BRK_DUTY4; + /* Disable three-phase pwm output. */ + ProtSpo_Exec(aptAddr); + break; + + /* level 3: brake loop duty level 3. */ + case PROT_LEVEL_3: + *duty = PROT_OVER_VOLT_BRK_DUTY2; + ovp->timer += ovp->ts; + if (ovp->timer > ovp->protLimitTime[PROT_LIMIT_TIME_2]) { + *duty = PROT_OVER_VOLT_BRK_DUTY3; + } + break; + + /* level 2: brake loop duty level 2. */ + case PROT_LEVEL_2: + *duty = PROT_OVER_VOLT_BRK_DUTY1; + ovp->timer += ovp->ts; + if (ovp->timer > ovp->protLimitTime[PROT_LIMIT_TIME_1]) { + *duty = PROT_OVER_VOLT_BRK_DUTY2; + } + break; + + /* level 1: brake loop duty level 1. */ + case PROT_LEVEL_1: + ovp->timer += ovp->ts; + if (ovp->timer > ovp->protLimitTime[PROT_LIMIT_TIME_0]) { + *duty = PROT_OVER_VOLT_BRK_DUTY1; + } + break; + + /* level 0: take no protection action. */ + case PROT_LEVEL_0: + break; + + default: + break; + } + return; +} + +/** + * @brief Lower dc-link voltage protection execution. + * @param lvp Lower dc-link voltage protection handle. + * @param spdRef Speed Reference (Hz). + * @param aptAddr Three-phase APT address pointer. + * @retval None. + */ +void LVP_Exec(LVP_Handle *lvp, float *spdRef, APT_RegStruct **aptAddr) +{ + MCS_ASSERT_PARAM(lvp != NULL); + MCS_ASSERT_PARAM(spdRef != NULL); + MCS_ASSERT_PARAM(aptAddr != NULL); + /* According to protect level, take corresponding action. */ + switch (lvp->protLevel) { + /* level 4: disable all PWM output. */ + case PROT_LEVEL_4: + /* Disable three-phase pwm output. */ + ProtSpo_Exec(aptAddr); + *spdRef *= 0.0f; + break; + + /* level 3: derate speed reference. */ + case PROT_LEVEL_3: + *spdRef *= PROT_POW_DN2_PCT; + lvp->timer += lvp->ts; + if (lvp->timer > lvp->protLimitTime[PROT_LIMIT_TIME_2]) { + *spdRef *= PROT_POW_DN3_PCT; + } + break; + + /* level 2: derate speed reference. */ + case PROT_LEVEL_2: + *spdRef *= PROT_POW_DN1_PCT; + lvp->timer += lvp->ts; + if (lvp->timer > lvp->protLimitTime[PROT_LIMIT_TIME_1]) { + *spdRef *= PROT_POW_DN2_PCT; + } + break; + + /* level 1: derate speed reference. */ + case PROT_LEVEL_1: + lvp->timer += lvp->ts; + if (lvp->timer > lvp->protLimitTime[PROT_LIMIT_TIME_0]) { + *spdRef *= PROT_POW_DN1_PCT; + } + break; + + /* level 0: take no protection action. */ + case PROT_LEVEL_0: + break; + + default: + break; + } + return; +} + +/** + * @brief Over dc-link voltage protection recovery. + * @param ovp Over dc-link voltage protection handle. + * @param motorErrStatus Motor error status. + * @param udc DC-link voltage (V). + * @retval None. + */ +void OVP_Recy(OVP_Handle *ovp, MotorErrStatusReg *motorErrStatus, float udc) +{ + MCS_ASSERT_PARAM(ovp != NULL); + /* If not under error state, just return without any operation. */ + if (!motorErrStatus->Bit.overVoltErr) { + return; + } + + /* According to protection level, take corresponding recovery action. */ + switch (ovp->protLevel) { + /* level 4 */ + case PROT_LEVEL_4: + if (udc < ovp->protValThr[PROT_VAL_THRESHOLD_3] - ovp->recyDelta) { + ovp->recyCnt++; + if (ovp->recyCnt > ovp->recyCntLimit) { + ovp->protLevel = PROT_LEVEL_3; + ovp->recyCnt = 0; + } + } + break; + + /* level 3 */ + case PROT_LEVEL_3: + /* If the dc-link voltage is less than threshold 2, level-2 protection is restored. */ + if (udc < ovp->protValThr[PROT_VAL_THRESHOLD_2] - ovp->recyDelta) { + ovp->recyCnt++; + if (ovp->recyCnt > ovp->recyCntLimit) { + ovp->protLevel = PROT_LEVEL_2; + ovp->recyCnt = 0; + } + } + break; + + /* level 2 */ + case PROT_LEVEL_2: + /* If the dc-link voltage is less than threshold 1, level-1 protection is restored. */ + if (udc < ovp->protValThr[PROT_VAL_THRESHOLD_1] - ovp->recyDelta) { + ovp->recyCnt++; + if (ovp->recyCnt > ovp->recyCntLimit) { + ovp->protLevel = PROT_LEVEL_1; + ovp->recyCnt = 0; + } + } + break; + + /* level 1 */ + case PROT_LEVEL_1: + /* If the dc-link voltage is less than threshold 0, level-0 protection is restored. */ + if (udc < ovp->protValThr[PROT_VAL_THRESHOLD_0] - ovp->recyDelta) { + ovp->recyCnt++; + if (ovp->recyCnt > ovp->recyCntLimit) { + ovp->protLevel = PROT_LEVEL_0; + ovp->recyCnt = 0; + } + } + break; + /* level 0 Fault-free state. */ + case PROT_LEVEL_0: + motorErrStatus->Bit.overVoltErr = 0; + break; + + default: + break; + } + return; +} + +/** + * @brief Lower dc-link voltage protection recovery. + * @param lvp Lower dc-link voltage protection handle. + * @param motorErrStatus Motor error status. + * @param udc DC-link voltage (V). + * @retval None. + */ +void LVP_Recy(LVP_Handle *lvp, MotorErrStatusReg *motorErrStatus, float udc) +{ + MCS_ASSERT_PARAM(lvp != NULL); + /* If not under error state, just return without any operation. */ + if (!motorErrStatus->Bit.lowerVoltErr) { + return; + } + + /* According to protection level, take corresponding recovery action. */ + switch (lvp->protLevel) { + /* level 4 */ + case PROT_LEVEL_4: + /* If the dc-link voltage is greater than threshold 3, level-3 protection is restored. */ + if (udc > lvp->protValThr[PROT_VAL_THRESHOLD_3] + lvp->recyDelta) { + lvp->recyCnt++; + if (lvp->recyCnt > lvp->recyCntLimit) { + lvp->protLevel = PROT_LEVEL_3; + lvp->recyCnt = 0; + } + } + break; + + /* level 3 */ + case PROT_LEVEL_3: + /* If the dc-link voltage is greater than threshold 2, level-2 protection is restored. */ + if (udc > lvp->protValThr[PROT_VAL_THRESHOLD_2] + lvp->recyDelta) { + lvp->recyCnt++; + if (lvp->recyCnt > lvp->recyCntLimit) { + lvp->protLevel = PROT_LEVEL_2; + lvp->recyCnt = 0; + } + } + break; + + /* level 2 */ + case PROT_LEVEL_2: + /* If the dc-link voltage is greater than threshold 1, level-1 protection is restored. */ + if (udc > lvp->protValThr[PROT_VAL_THRESHOLD_1] + lvp->recyDelta) { + lvp->recyCnt++; + if (lvp->recyCnt > lvp->recyCntLimit) { + lvp->protLevel = PROT_LEVEL_1; + lvp->recyCnt = 0; + } + } + break; + + /* level 1 */ + case PROT_LEVEL_1: + /* If the dc-link voltage is greater than threshold 0, level-0 protection is restored. */ + if (udc > lvp->protValThr[PROT_VAL_THRESHOLD_0] + lvp->recyDelta) { + lvp->recyCnt++; + if (lvp->recyCnt > lvp->recyCntLimit) { + lvp->protLevel = PROT_LEVEL_0; + lvp->recyCnt = 0; + } + } + break; + /* level 0 Fault-free state. */ + case PROT_LEVEL_0: + motorErrStatus->Bit.lowerVoltErr = 0; + break; + + default: + break; + } + return; +} + +/** + * @brief Over dc-link voltage protection error status clear. + * @param ovp Over voltage protection handle. + * @retval None. + */ +void OVP_Clear(OVP_Handle *ovp) +{ + MCS_ASSERT_PARAM(ovp != NULL); + /* Clear the history value. */ + ovp->protCnt = 0; + ovp->protLevel = PROT_LEVEL_0; + ovp->recyCnt = 0; + ovp->timer = 0.0f; +} + +/** + * @brief Lower dc-link voltage protection error status clear. + * @param lvp Lower voltage protection handle. + * @retval None. + */ +void LVP_Clear(LVP_Handle *lvp) +{ + MCS_ASSERT_PARAM(lvp != NULL); + /* Clear the history value. */ + lvp->protCnt = 0; + lvp->protLevel = PROT_LEVEL_0; + lvp->recyCnt = 0; + lvp->timer = 0.0f; +} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_dc_volt_prot.h b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_dc_volt_prot.h new file mode 100644 index 00000000..0435156a --- /dev/null +++ b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_dc_volt_prot.h @@ -0,0 +1,69 @@ +/** + * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_dc_volt_prot.h + * @author MCU Algorithm Team + * @brief This file contains dc-link voltage protection data struct and api declaration. + */ + +/* Define to prevent recursive inclusion ------------------------------------------------------- */ +#ifndef McuMagicTag_MCS_DC_VOLT_PROT_H +#define McuMagicTag_MCS_DC_VOLT_PROT_H + +/* Includes ------------------------------------------------------------------------------------ */ +#include "mcs_prot_cmm.h" +#include "apt_ip.h" + +typedef struct { + unsigned int protCnt; + unsigned int recyCnt; + unsigned int protCntLimit; + unsigned int recyCntLimit; + float protValThr[PROT_VAL_THRESHOLD_NUMS]; /* from low voltage level to high. */ + float protLimitTime[PROT_LIMIT_TIME_NUMS]; /* from level 3 to level 1. */ + float recyDelta; + float timer; + float ts; + PROT_Level protLevel; +} OVP_Handle; + +typedef struct { + unsigned int protCnt; + unsigned int recyCnt; + unsigned int protCntLimit; + unsigned int recyCntLimit; + float protValThr[PROT_VAL_THRESHOLD_NUMS]; /* from high voltage level to low. */ + float protLimitTime[PROT_LIMIT_TIME_NUMS]; /* from level 3 to level 1. */ + float recyDelta; + float timer; + float ts; + PROT_Level protLevel; +} LVP_Handle; + +void OVP_Init(OVP_Handle *ovp, float ts); +void OVP_Det(OVP_Handle *ovp, MotorErrStatusReg *motorErrStatus, float udc); +void OVP_Exec(OVP_Handle *ovp, float *duty, APT_RegStruct **aptAddr); +void OVP_Recy(OVP_Handle *ovp, MotorErrStatusReg *motorErrStatus, float udc); +void OVP_Clear(OVP_Handle *ovp); + +void LVP_Init(LVP_Handle *lvp, float ts); +void LVP_Det(LVP_Handle *lvp, MotorErrStatusReg *motorErrStatus, float udc); +void LVP_Exec(LVP_Handle *lvp, float *spdRef, APT_RegStruct **aptAddr); +void LVP_Recy(LVP_Handle *lvp, MotorErrStatusReg *motorErrStatus, float udc); +void LVP_Clear(LVP_Handle *lvp); + +#endif diff --git a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_motor_stalling.c b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_motor_stalling.c new file mode 100644 index 00000000..9dee4d2d --- /dev/null +++ b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_motor_stalling.c @@ -0,0 +1,112 @@ +/** + * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_motor_stalling.c + * @author MCU Algorithm Team + * @brief This file contains motor stalling protection data struct and api definition. + */ + +/* Includes ------------------------------------------------------------------------------------ */ +#include "mcs_motor_stalling.h" +#include "mcs_prot_user_config.h" +#include "mcs_math.h" +#include "mcs_math_const.h" +#include "mcs_assert.h" + +/** + * @brief Initilization motor stalling protection function. + * @param stall Motor stalling handle. + * @param ts Ctrl period (s). + * @param currLimit The current amplitude that triggers fault. (A). + * @param spdLimit The speed amplitude that triggers fault. (Hz). + * @param timeLimit The threshold time that current amplitude over the limit (s). + * @retval None. + */ +void STP_Init(STP_Handle *stall, float ts, float currLimit, float spdLimit, float timeLimit) +{ + MCS_ASSERT_PARAM(stall != NULL); + MCS_ASSERT_PARAM(ts > 0.0f); + MCS_ASSERT_PARAM(currLimit > 0.0f); + MCS_ASSERT_PARAM(spdLimit > 0.0f); + MCS_ASSERT_PARAM(timeLimit > 0.0f); + /* Configuring parameters for stalling detection. */ + stall->ts = ts; + /* Current threshold and speed threshold for stalling fault. */ + stall->currAmpLimit = currLimit; + stall->spdLimit = spdLimit; + stall->timeLimit = timeLimit; + stall->timer = 0.0f; +} + +/** + * @brief Motor stalling detection. + * @param stall Motor stalling handle. + * @param motorErrStatus Motor error status. + * @param spd Speed feedback (Hz). + * @param idq Dq-axis current feedback (A). + * @retval None. + */ +void STP_Det_ByCurrSpd(STP_Handle *stall, MotorErrStatusReg *motorErrStatus, float spd, DqAxis idq) +{ + MCS_ASSERT_PARAM(stall != NULL); + MCS_ASSERT_PARAM(motorErrStatus != NULL); + MCS_ASSERT_PARAM(Abs(spd) >= 0.0f); + /* Calculate current amplitude. */ + float currAmp = Sqrt(idq.d * idq.d + idq.q * idq.q); + float spdAbs = Abs(spd); + /* Check if value goes over threshold for continuous cycles. */ + if (currAmp < stall->currAmpLimit || spdAbs > stall->spdLimit) { + stall->timer = 0.0f; + return; + } + /* Time accumulation. */ + if (stall->timer < stall->timeLimit) { + stall->timer += stall->ts; + return; + } + motorErrStatus->Bit.motorStalling = 1; +} + +/** + * @brief Motor stalling protection execution. + * @param motorErrStatus Motor error status. + * @param aptAddr Three-phase APT address pointer. + * @retval None. + */ +void STP_Exec(MotorErrStatusReg *motorErrStatus, APT_RegStruct **aptAddr) +{ + MCS_ASSERT_PARAM(motorErrStatus != NULL); + MCS_ASSERT_PARAM(aptAddr != NULL); + if (motorErrStatus->Bit.motorStalling == 0) { + return; + } + + /* Disable three-phase pwm output. */ + ProtSpo_Exec(aptAddr); + return; +} + +/** + * @brief Motor stalling protection error status clear. + * @param stall Motor stalling handle. + * @retval None. + */ +void STP_Clear(STP_Handle *stall) +{ + MCS_ASSERT_PARAM(stall != NULL); + stall->timer = 0.0f; +} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_motor_stalling.h b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_motor_stalling.h new file mode 100644 index 00000000..a1d1e4be --- /dev/null +++ b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_motor_stalling.h @@ -0,0 +1,45 @@ +/** + * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_motor_stalling.h + * @author MCU Algorithm Team + * @brief This file contains motor stalling protection data struct and api declaration. + */ + +/* Define to prevent recursive inclusion ------------------------------------- */ +#ifndef McuMagicTag_MCS_MOTOR_STALLING_PROT_H +#define McuMagicTag_MCS_MOTOR_STALLING_PROT_H + +/* Includes ------------------------------------------------------------------------------------ */ +#include "mcs_prot_cmm.h" +#include "apt_ip.h" +#include "mcs_typedef.h" + +typedef struct { + float currAmpLimit; /**< Feedback current higher than this value triggers fault. (A). */ + float spdLimit; /**< Feedback speed lower than this value triggers fault (Hz). */ + float timeLimit; /**< The threshold time that current and speed feedback over ranges (s). */ + float timer; /**< Timer to get speed and current over range time. */ + float ts; /**< Ctrl period (s). */ +} STP_Handle; + +void STP_Init(STP_Handle *stall, float ts, float currLimit, float spdLimit, float timeLimit); +void STP_Det_ByCurrSpd(STP_Handle *stall, MotorErrStatusReg *motorErrStatus, float spd, DqAxis idq); +void STP_Exec(MotorErrStatusReg *motorErrStatus, APT_RegStruct **aptAddr); +void STP_Clear(STP_Handle *stall); + +#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_temp_prot.c b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_temp_prot.c new file mode 100644 index 00000000..184ba4a3 --- /dev/null +++ b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_temp_prot.c @@ -0,0 +1,262 @@ +/** + * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_temp_prot.c + * @author MCU Algorithm Team + * @brief This file contains over temperature protection api definition. + */ + +/* Includes ------------------------------------------------------------------------------------ */ +#include "mcs_temp_prot.h" +#include "mcs_prot_user_config.h" +#include "mcs_assert.h" + +/** + * @brief Initilization over temperation protection function. + * @param otp Over temperature protection handle. + * @param ts Ctrl period. + * @retval None. + */ +void OTP_Init(OTP_Handle *otp, float ts) +{ + MCS_ASSERT_PARAM(otp != NULL); + MCS_ASSERT_PARAM(ts > 0.0f); + otp->ts = ts; + otp->protCntLimit = PROT_CNT_LIMIT; + otp->recyCntLimit = RECY_CNT_LIMIT; + /* Configuring the temperature protection threshold. */ + otp->protValThr[PROT_VAL_THRESHOLD_0] = PROT_OVER_IPM_TEMP_POW_DN1; + otp->protValThr[PROT_VAL_THRESHOLD_1] = PROT_OVER_IPM_TEMP_POW_DN2; + otp->protValThr[PROT_VAL_THRESHOLD_2] = PROT_OVER_IPM_TEMP_POW_DN3; + otp->protValThr[PROT_VAL_THRESHOLD_3] = PROT_OVER_IPM_TEMP_POW_OFF; + /* Configuring the protection limiting time. */ + otp->protLimitTime[PROT_LIMIT_TIME_0] = PROT_OVER_TEMP_LIMIT1_TIME_SEC; + otp->protLimitTime[PROT_LIMIT_TIME_1] = PROT_OVER_TEMP_LIMIT2_TIME_SEC; + otp->protLimitTime[PROT_LIMIT_TIME_2] = PROT_OVER_TEMP_LIMIT3_TIME_SEC; + otp->recyDelta = PROT_OVER_IPM_TEMP_RECY_DELTA; + OTP_Clear(otp); +} + +/** + * @brief Over temperatre protection detection. + * @param otp Over temperature protection handle. + * @param tempErrBit Temperature error status bit. + * @param idq DQ-axis feedback currents. + * @retval None. + */ +void OTP_Det(OTP_Handle *otp, MotorErrStatusReg *motorErrStatus, PROT_ErrBit protBit, float temp) +{ + MCS_ASSERT_PARAM(otp != NULL); + MCS_ASSERT_PARAM(motorErrStatus != NULL); + MCS_ASSERT_PARAM(temp > 0.0f); + /* Check if value goes over threshold for continuous cycles. */ + if (temp < otp->protValThr[PROT_VAL_THRESHOLD_0]) { + otp->protCnt = 0; + ClearBit(&motorErrStatus->all, protBit); + return; + } + + if (otp->protCnt < otp->protCntLimit) { + otp->protCnt++; + return; + } + + /* Check which protection level should be activated. */ + /* Once enter error status, error status can only be changed by recover api. */ + /* protect level: level 4. */ + if (temp >= otp->protValThr[PROT_VAL_THRESHOLD_3] && otp->protLevel < PROT_LEVEL_4) { + otp->protLevel = PROT_LEVEL_4; + SetBit(&motorErrStatus->all, protBit); + otp->protCnt = 0; + return; + } + + /* Protect level: level 3. */ + if (temp >= otp->protValThr[PROT_VAL_THRESHOLD_2] && otp->protLevel < PROT_LEVEL_3) { + otp->protLevel = PROT_LEVEL_3; + SetBit(&motorErrStatus->all, protBit); + otp->protCnt = 0; + otp->timer = 0.0f; + return; + } + + /* Protect level: level 2. */ + if (temp >= otp->protValThr[PROT_VAL_THRESHOLD_1] && otp->protLevel < PROT_LEVEL_2) { + otp->protLevel = PROT_LEVEL_2; + SetBit(&motorErrStatus->all, protBit); + otp->protCnt = 0; + otp->timer = 0.0f; + return; + } + + /* Protect level: level 1. */ + if (temp >= otp->protValThr[PROT_VAL_THRESHOLD_0] && otp->protLevel < PROT_LEVEL_1) { + otp->protLevel = PROT_LEVEL_1; + SetBit(&motorErrStatus->all, protBit); + otp->protCnt = 0; + otp->timer = 0.0f; + return; + } +} + +/** + * @brief Over temperature protection execution. + * @param otp Over temperature protection handle. + * @param spdRef Speed reference (Hz). + * @param aptAddr Three-phase APT address pointer. + * @retval None. + */ +void OTP_Exec(OTP_Handle *otp, float *spdRef, APT_RegStruct **aptAddr) +{ + MCS_ASSERT_PARAM(otp != NULL); + MCS_ASSERT_PARAM(spdRef != NULL); + MCS_ASSERT_PARAM(aptAddr != NULL); + /* According to protect level, take corresponding action. */ + switch (otp->protLevel) { + /* level 4: disable all PWM output. */ + case PROT_LEVEL_4: + /* Disable three-phase pwm output. */ + ProtSpo_Exec(aptAddr); + *spdRef *= 0.0f; + break; + + /* level 3: derate speed reference. */ + case PROT_LEVEL_3: + *spdRef *= PROT_POW_DN2_PCT; + otp->timer += otp->ts; + if (otp->timer > otp->protLimitTime[PROT_LIMIT_TIME_2]) { + *spdRef *= PROT_POW_DN3_PCT; + } + break; + + /* level 2: derate speed reference. */ + case PROT_LEVEL_2: + /* Reducte motor speed to level 2 */ + *spdRef *= PROT_POW_DN1_PCT; + otp->timer += otp->ts; + if (otp->timer > otp->protLimitTime[PROT_LIMIT_TIME_1]) { + *spdRef *= PROT_POW_DN2_PCT; + } + break; + + /* level 0: take no protection action. */ + case PROT_LEVEL_0: + break; + + case PROT_LEVEL_1: + /* Reducte motor speed to level 0 */ + otp->timer += otp->ts; + if (otp->timer > otp->protLimitTime[PROT_LIMIT_TIME_0]) { + /* level 1: derate speed reference. */ + *spdRef *= PROT_POW_DN1_PCT; + } + break; + + default: + break; + } + return; +} + +/** + * @brief Over temperature protection recovery. + * @param otp Over temperature protection handle. + * @param tempErrBit Temperature error status bit. + * @param temp Temperature (celsius). + * @retval None. + */ +void OTP_Recy(OTP_Handle *otp, MotorErrStatusReg *motorErrStatus, PROT_ErrBit protBit, float temp) +{ + MCS_ASSERT_PARAM(otp != NULL); + MCS_ASSERT_PARAM(motorErrStatus != NULL); + MCS_ASSERT_PARAM(temp > 0.0f); + /* If not under error state, just return without any operation. */ + if (otp->protLevel == PROT_LEVEL_0) { + motorErrStatus->all &= (~(1 >> protBit)); + return; + } + + /* According to protection level, take corresponding recovery action. */ + switch (otp->protLevel) { + /* level 4 */ + case PROT_LEVEL_4: + /* If the temperature is less than threshold 3, level-3 protection is restored. */ + if (temp < otp->protValThr[PROT_VAL_THRESHOLD_3] - otp->recyDelta) { + otp->recyCnt++; + if (otp->recyCnt > otp->recyCntLimit) { + otp->protLevel = PROT_LEVEL_3; + otp->recyCnt = 0; + } + } + break; + + /* level 3 */ + case PROT_LEVEL_3: + /* If the temperature is less than threshold 2, level-2 protection is restored. */ + if (temp < otp->protValThr[PROT_VAL_THRESHOLD_2] - otp->recyDelta) { + otp->recyCnt++; + if (otp->recyCnt > otp->recyCntLimit) { + otp->protLevel = PROT_LEVEL_2; + otp->recyCnt = 0; + } + } + break; + + /* level 2 */ + case PROT_LEVEL_2: + /* If the temperature is less than threshold 1, level-1 protection is restored. */ + if (temp < otp->protValThr[PROT_VAL_THRESHOLD_1] - otp->recyDelta) { + otp->recyCnt++; + if (otp->recyCnt > otp->recyCntLimit) { + otp->protLevel = PROT_LEVEL_1; + otp->recyCnt = 0; + } + } + break; + + /* level 1 */ + case PROT_LEVEL_1: + /* If the temperature is less than threshold 0, level-0 protection is restored. */ + if (temp < otp->protValThr[PROT_VAL_THRESHOLD_0] - otp->recyDelta) { + otp->recyCnt++; + if (otp->recyCnt > otp->recyCntLimit) { + otp->protLevel = PROT_LEVEL_0; + otp->recyCnt = 0; + } + } + break; + + default: + break; + } + return; +} + +/** + * @brief Over temperature protection error status clear. + * @param otp Over temperature protection handle. + * @retval None. + */ +void OTP_Clear(OTP_Handle *otp) +{ + MCS_ASSERT_PARAM(otp != NULL); + /* Clear the history value. */ + otp->protCnt = 0; + otp->protLevel = PROT_LEVEL_0; + otp->recyCnt = 0; + otp->timer = 0.0f; +} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_temp_prot.h b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_temp_prot.h new file mode 100644 index 00000000..2b9d99a7 --- /dev/null +++ b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_temp_prot.h @@ -0,0 +1,49 @@ +/** + * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_temp_prot.h + * @author MCU Algorithm Team + * @brief his file contains over temperature protection api declaration. + */ + +/* Define to prevent recursive inclusion ------------------------------------- */ +#ifndef McuMagicTag_MCS_TEMP_PROT_H +#define McuMagicTag_MCS_TEMP_PROT_H + +/* Includes ------------------------------------------------------------------------------------ */ +#include "mcs_prot_cmm.h" +#include "apt_ip.h" + +typedef struct { + unsigned int protCnt; + unsigned int recyCnt; + unsigned int protCntLimit; + unsigned int recyCntLimit; + float protValThr[PROT_VAL_THRESHOLD_NUMS]; /* from low current level to high. */ + float protLimitTime[PROT_LIMIT_TIME_NUMS]; /* from level 3 to level 1. */ + float recyDelta; + float ts; + float timer; + PROT_Level protLevel; +} OTP_Handle; + +void OTP_Init(OTP_Handle *otp, float ts); +void OTP_Det(OTP_Handle *otp, MotorErrStatusReg *motorErrStatus, PROT_ErrBit protBit, float temp); +void OTP_Exec(OTP_Handle *otp, float *spdRef, APT_RegStruct **aptAddr); +void OTP_Recy(OTP_Handle *otp, MotorErrStatusReg *motorErrStatus, PROT_ErrBit protBit, float temp); +void OTP_Clear(OTP_Handle *otp); +#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/readme.md b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/readme.md new file mode 100644 index 00000000..98b2a068 --- /dev/null +++ b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/readme.md @@ -0,0 +1,10 @@ +# pmsm_sensorless_2shunt_foc + +**【功能描述】** ++ 基于ECMCU105H/ECBMCU201MPC单板的单电机双电阻采样Foc应用 + +**【环境要求】** ++ 所有单板电源改制为演示用的12V低压,电机选用Gimbal GBM2804H-100T + +**【IDE配置方法】** ++ chipConfig中的Sample栏目里面选中pmsm sensorless 2shunt foc示例,然后点击生成代码即可 diff --git a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/src/mcs_carrier.c b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/src/mcs_carrier.c new file mode 100644 index 00000000..9fc8afac --- /dev/null +++ b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/src/mcs_carrier.c @@ -0,0 +1,144 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_carrier.c + * @author MCU Algorithm Team + * @brief This file provides carrier interrupt application for motor control. + */ + +#include "mcs_carrier.h" +#include "mcs_math.h" +#include "typedefs.h" +#include "mcs_assert.h" +#include "mcs_user_config.h" +#include "mcs_ctlmode_config.h" + +/** + * @brief Synchronous rotation coordinate system angle. + * @param mtrCtrl The motor control handle. + * @retval None. + */ +static void MCS_SyncCoorAngle(MTRCTRL_Handle *mtrCtrl) +{ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + /* Synchronous rotation coordinate system angle. */ + switch (mtrCtrl->stateMachine) { + case FSM_STARTUP: + /* Current ramp angle is 0. */ + if (mtrCtrl->startup.stage == STARTUP_STAGE_CURR) { + mtrCtrl->axisAngle = 0; + } else if (mtrCtrl->startup.stage == STARTUP_STAGE_SPD) { /* IF control phase angle self-addition. */ + mtrCtrl->axisAngle = IF_CurrAngleCalc(&mtrCtrl->ifCtrl, mtrCtrl->spdRefHz); + } else if (mtrCtrl->startup.stage == STARTUP_STAGE_SWITCH) { /* Switch Angle */ + mtrCtrl->axisAngle = mtrCtrl->smo.elecAngle; + } + break; + + case FSM_RUN: + mtrCtrl->axisAngle = mtrCtrl->smo.elecAngle; + break; + + default: + mtrCtrl->axisAngle = 0; + break; + } +} + +/** + * @brief PWM waveform setting and sampling point setting for a single resistors and dual resistors. + * @param mtrCtrl The motor control handle. + * @retval None. + */ +static void MCS_PwmAdcSet(MTRCTRL_Handle *mtrCtrl) +{ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + SampleMode sampleMode = mtrCtrl->sampleMode; + /* Set the duty cycle according to the sampling mode. */ + if (sampleMode == DUAL_RESISTORS) { + SVPWM_Exec(&mtrCtrl->sv, &mtrCtrl->vabRef, &mtrCtrl->dutyUvw); + mtrCtrl->setPwmDutyCb(&mtrCtrl->dutyUvw, &mtrCtrl->dutyUvw); + } else if (sampleMode == SINGLE_RESISTOR) { + R1SVPWM_Exec(&mtrCtrl->r1Sv, &mtrCtrl->vabRef, &mtrCtrl->dutyUvwLeft, &mtrCtrl->dutyUvwRight); + mtrCtrl->setPwmDutyCb(&mtrCtrl->dutyUvwLeft, &mtrCtrl->dutyUvwRight); + /* The ADC sampling point position needs to be set based on the phase shift of a single resistors. */ + mtrCtrl->setADCTriggerTimeCb(mtrCtrl->r1Sv.samplePoint[0] * mtrCtrl->aptMaxcntCmp, \ + mtrCtrl->r1Sv.samplePoint[1] * mtrCtrl->aptMaxcntCmp); + } +} + +/** + * @brief Carrier interrupt function. + * @param mtrCtrl The motor control handle. + * @retval None. + */ +void MCS_CarrierProcess(MTRCTRL_Handle *mtrCtrl) +{ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + UvwAxis *currUvw = &mtrCtrl->currUvw; + AlbeAxis *currAlbe = &mtrCtrl->iabFbk; + AlbeAxis *vab = &mtrCtrl->vabRef; + SampleMode sampleMode = mtrCtrl->sampleMode; + /* sample mode verify */ + if (sampleMode > SINGLE_RESISTOR || mtrCtrl->setADCTriggerTimeCb == NULL) { + return; + } + /* param verify */ + if (mtrCtrl->setPwmDutyCb == NULL || mtrCtrl->readCurrUvwCb == NULL) { + return; + } + /* Read the three-phase current value. */ + mtrCtrl->readCurrUvwCb(currUvw); + /* Clark Calc */ + ClarkeCalc(currUvw, currAlbe); + /* Smo observation */ + if (mtrCtrl->obserType == FOC_OBSERVERTYPE_SMO1TH) { + /* Smo observation */ + FOSMO_Exec(&mtrCtrl->smo, currAlbe, vab, mtrCtrl->spdRefHz); + } else if (mtrCtrl->obserType == FOC_OBSERVERTYPE_SMO4TH) { + /* Smo4th observation */ + SMO4TH_Exec(&mtrCtrl->smo4th, currAlbe, vab); + mtrCtrl->smo.spdEst = mtrCtrl->smo4th.spdEst; + mtrCtrl->smo.elecAngle = mtrCtrl->smo4th.elecAngle; + } + /* Synchronization angle */ + MCS_SyncCoorAngle(mtrCtrl); + + /* Park transformation */ + ParkCalc(currAlbe, mtrCtrl->axisAngle, &mtrCtrl->idqFbk); + + /* statemachine */ + switch (mtrCtrl->stateMachine) { + case FSM_STARTUP: + case FSM_RUN: + CURRCTRL_Exec(&mtrCtrl->currCtrl, &mtrCtrl->vdqRef, mtrCtrl->smo.spdEst, 0); + InvParkCalc(&mtrCtrl->vdqRef, mtrCtrl->axisAngle, vab); + MCS_PwmAdcSet(mtrCtrl); + break; + + case FSM_CAP_CHARGE: + case FSM_CLEAR: + case FSM_IDLE: + mtrCtrl->smo4th.spdEst = 0.0f; + break; + + default: + vab->alpha = 0.0f; + vab->beta = 0.0f; + MCS_PwmAdcSet(mtrCtrl); + break; + } +} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/src/mcs_motor_process.c b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/src/mcs_motor_process.c new file mode 100644 index 00000000..79bc3949 --- /dev/null +++ b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/src/mcs_motor_process.c @@ -0,0 +1,950 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_motor_process.c + * @author MCU Algorithm Team + * @brief This file provides motor application. + * @details Single FOC application based on the ECMCU105H/ECBMCU201MPC board + * 1) Motor model is Gimbal GBM2804H-100T. + * 2) Select the pmsm sensorless 2shunt foc example in the sample column + of chipConfig and click Generate Code. + * 3) It's power supply must be changed to 12V. + */ +#include "main.h" +#include "mcs_user_config.h" +#include "mcs_math.h" +#include "hmi_module.h" +#include "mcs_ctlmode_config.h" +#include "mcs_prot_user.h" +#include "mcs_prot_user_config.h" +#include "mcs_math_const.h" +#include "mcs_motor_process.h" +#include "mcs_chip_config.h" +#include + +/*------------------------------- Macro Definition -----------------------------------------------*/ +#define US_PER_MS 1000 +#define ANGLE_RANGE_ABS 65536 +#define ANGLE_360_F 65536.0f /* 0 - 65536 indicates 0 to 360. */ +#define APT_FULL_DUTY 1.0f +#define TEMP_3 3.0f +#define TEMP_15 15.0f +#define TEMP_30 30.0f +#define TEMP_45 45.0f +#define TEMP_60 60.0f +#define TEMP_RES_15 78.327f +#define TEMP_RES_30 36.776f +#define TEMP_RES_45 18.301f +#define TEMP_RES_60 9.607f +#define CNT_10 10 +#define CNT_5000 5000 +#define LEVEL_4 4 +#define MOTOR_START_DELAY 2 +#define ADC_READINIT_DELAY 1 +#define ADC_READINIT_TIMES 20 +#define ADC_TRIMVALUE_MIN 1800.0f +#define ADC_TRIMVALUE_MAX 2200.0f +/*------------------------------- Param Definition -----------------------------------------------*/ +/* Motor parameters. */ +/* Np, Rs, Ld, Lq, Psif, J, Nmax, Currmax, PPMR, zShift */ +static MOTOR_Param g_motorParam = MOTORPARAM_DEFAULTS; +static APT_RegStruct* g_apt[PHASE_MAX_NUM] = {APT_U, APT_V, APT_W}; +/* Motor control handle */ +static MTRCTRL_Handle g_mc = {0}; + +/* Motor speed loop PI param. */ +static void SPDCTRL_InitWrapper(SPDCTRL_Handle *spdHandle, float ts) +{ + /* Speed loop param assignment. */ + PI_Param spdPi = { + .kp = SPD_KP, + .ki = SPD_KI, + .lowerLim = SPD_LOWERLIM, + .upperLim = SPD_UPPERLIM, + }; + /* Speed loop param init. */ + SPDCTRL_Init(spdHandle, &g_motorParam, spdPi, ts); +} + +/* Motor current Loop PI param. */ +static void CURRCTRL_InitWrapper(CURRCTRL_Handle *currHandle, DqAxis *idqRef, DqAxis *idqFbk, float ts) +{ + /* Axis-D current loop param assignment. */ + PI_Param dCurrPi = { + .kp = CURRDAXIS_KP, + .ki = CURRDAXIS_KI, + .lowerLim = CURR_LOWERLIM, + .upperLim = CURR_UPPERLIM, + }; + /* Axis-Q current loop param assignment. */ + PI_Param qCurrPi = { + .kp = CURRQAXIS_KP, + .ki = CURRQAXIS_KI, + .lowerLim = CURR_LOWERLIM, + .upperLim = CURR_UPPERLIM, + }; + /* Current loop param init. */ + CURRCTRL_Init(currHandle, &g_motorParam, idqRef, idqFbk, dCurrPi, qCurrPi, ts); +} + +/* First order smo param. */ +static void FOSMO_InitWrapper(FOSMO_Handle *fosmo, float ts) +{ + /* Smo param assignment. */ + FOSMO_Param fosmoParam = { + .gain = FOSMO_GAIN, + .lambda = FOSMO_LAMBDA, + .fcEmf = FOSMO_EMF_CUTOFF_FREQ, + .fcLpf = SPEED_FILTER_CUTOFF_FREQUENCY, + .pllBdw = FOSMO_PLL_BDW, + }; + /* Init smo param. */ + FOSMO_Init(fosmo, fosmoParam, &g_motorParam, ts); +} + +/* Smo4th param. */ +static void SMO4TH_InitWrapper(SMO4TH_Handle *smo4TH) +{ + /* Smo4th param assignment. */ + SMO4TH_Param smo4thParam = { + .kd = SMO4TH_KD, + .kq = SMO4TH_KQ, + .pllBdw = SMO4TH_PLL_BDW, + .fcLpf = SMO4TH_SPD_FILTER_CUTOFF_FREQ, + }; + /* Init smo param. */ + SMO4TH_Init(smo4TH, smo4thParam, &g_motorParam, CTRL_CURR_PERIOD); +} + +/*------------------------------- Function Definition -----------------------------------------------*/ +/** + * @brief Initialzer of system tick. + * @param mtrCtrl Motor control struct handle. + * @retval None. + */ +static void TimerTickInit(MTRCTRL_Handle *mtrCtrl) +{ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + mtrCtrl->sysTickCnt = 0; + mtrCtrl->msTickNum = US_PER_MS / SYSTICK_PERIOD_US; + mtrCtrl->capChargeTickNum = (INV_CAP_CHARGE_MS * US_PER_MS / SYSTICK_PERIOD_US); +} + +/** + * @brief Init motor control task. + * @retval None. + */ +static void TSK_Init(void) +{ + g_mc.motorStateFlag = 0; + g_mc.uartHeartDetCnt = 0; + g_mc.uartTimeStamp = 0; + g_mc.stateMachine = FSM_IDLE; + g_mc.currCtrlPeriod = CTRL_CURR_PERIOD; /* Init current controller */ + g_mc.aptMaxcntCmp = g_apt0.waveform.timerPeriod; + g_mc.sampleMode = DUAL_RESISTORS; + g_mc.obserType = FOC_OBSERVERTYPE_SMO4TH; /* Init foc observe mode */ + g_mc.controlMode = FOC_CONTROLMODE_SPEED; /* Init motor control mode */ + g_mc.adcCurrCofe = ADC_CURR_COFFI; + g_mc.spdAdjustMode = CUST_SPEED_ADJUST; + g_mc.uartConnectFlag = DISCONNECT; + g_mc.spdCmdHz = USER_MIN_SPD_HZ; /* Motor initialization speed */ + + g_mc.adc0Compensate = ADC0COMPENSATE; /* Phase-u current init adc shift trim value */ + g_mc.adc1Compensate = ADC1COMPENSATE; /* Phase-w current init adc shift trim value */ + + IF_Init(&g_mc.ifCtrl, CTRL_IF_CURR_AMP_A, USER_CURR_SLOPE, CTRL_SYSTICK_PERIOD, CTRL_CURR_PERIOD); + RMG_Init(&g_mc.spdRmg, CTRL_SYSTICK_PERIOD, USER_SPD_SLOPE); /* Init speed slope */ + MtrParamInit(&g_mc.mtrParam, g_motorParam); + + TimerTickInit(&g_mc); + if (g_mc.sampleMode == DUAL_RESISTORS) { + SVPWM_Init(&g_mc.sv, INV_VOLTAGE_BUS * ONE_DIV_SQRT3); /* Dual resistors SVPWM init. */ + } else if (g_mc.sampleMode == SINGLE_RESISTOR) { + /* Single resistor SVPWM init. */ + R1SVPWM_Init(&g_mc.r1Sv, INV_VOLTAGE_BUS * ONE_DIV_SQRT3, SAMPLE_POINT_SHIFT, SAMPLE_WINDOW_DUTY); + } + + SPDCTRL_InitWrapper(&g_mc.spdCtrl, CTRL_SYSTICK_PERIOD); + CURRCTRL_InitWrapper(&g_mc.currCtrl, &g_mc.idqRef, &g_mc.idqFbk, CTRL_CURR_PERIOD); + FOSMO_InitWrapper(&g_mc.smo, CTRL_CURR_PERIOD); + SMO4TH_InitWrapper(&g_mc.smo4th); + + STARTUP_Init(&g_mc.startup, USER_SWITCH_SPDBEGIN_HZ, USER_SWITCH_SPDBEGIN_HZ + TEMP_3); + + MotorProt_Init(&g_mc.prot); /* Init protect state comond */ + OCP_Init(&g_mc.prot.ocp, CTRL_CURR_PERIOD); + OVP_Init(&g_mc.prot.ovp, CTRL_SYSTICK_PERIOD); + LVP_Init(&g_mc.prot.lvp, CTRL_SYSTICK_PERIOD); + OTP_Init(&g_mc.prot.otp, CTRL_SYSTICK_PERIOD); + STP_Init(&g_mc.prot.stall, CTRL_SYSTICK_PERIOD, PROT_STALLING_CURR_AMP_LIMIT, + PROT_STALLING_SPD_LIMIT, PROT_STALLING_TIME_LIMIT); +} + +/** + * @brief Clear historical values of all controller before start-up. + * @param mtrCtrl The motor control handle. + * @retval None. + */ +static void ClearBeforeStartup(MTRCTRL_Handle *mtrCtrl) +{ + /* Verify Parameters */ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + /* The initial angle is 0. */ + mtrCtrl->axisAngle = 0; + + mtrCtrl->spdRefHz = 0.0f; + /* The initial dq-axis reference current is 0. */ + mtrCtrl->idqRef.d = 0.0f; + mtrCtrl->idqRef.q = 0.0f; + + mtrCtrl->vdqRef.d = 0.0f; + mtrCtrl->vdqRef.q = 0.0f; + /* Clear Duty Cycle Value. The initial duty cycle is 0.5. */ + mtrCtrl->dutyUvwLeft.u = 0.5f; + mtrCtrl->dutyUvwLeft.v = 0.5f; + mtrCtrl->dutyUvwLeft.w = 0.5f; + mtrCtrl->dutyUvwRight.u = 0.5f; + mtrCtrl->dutyUvwRight.v = 0.5f; + mtrCtrl->dutyUvwRight.w = 0.5f; + + mtrCtrl->prot.motorErrStatus.all = 0x00; + + RMG_Clear(&mtrCtrl->spdRmg); /* Clear the history value of speed slope control */ + CURRCTRL_Clear(&mtrCtrl->currCtrl); + IF_Clear(&mtrCtrl->ifCtrl); + SPDCTRL_Clear(&mtrCtrl->spdCtrl); + FOSMO_Clear(&mtrCtrl->smo); + SMO4TH_Clear(&mtrCtrl->smo4th); + STARTUP_Clear(&mtrCtrl->startup); + R1SVPWM_Clear(&mtrCtrl->r1Sv); + + OTP_Clear(&mtrCtrl->prot.otp); + OCP_Clear(&mtrCtrl->prot.ocp); + OVP_Clear(&mtrCtrl->prot.ovp); + LVP_Clear(&mtrCtrl->prot.lvp); +} + +/** + * @brief To set the comparison value of the IGBT single-resistance ADC sampling trigger position. + * @param aptx The APT register struct handle. + * @param cntCmpA A Count compare reference of time-base counter. + * @param cntCmpB B Count compare reference of time-base counter. + * @param maxCntCmp Maximum Comparison Value + * @retval None. + */ +static void MCS_SetAdcCompareR1(APT_RegStruct *aptx, unsigned short cntCmpA, + unsigned short cntCmpB, unsigned short maxCntCmp) +{ + unsigned short tmp; + /* Sets the A Count compare reference of time-base counter. */ + tmp = (unsigned short)Clamp((float)(cntCmpA), (float)(maxCntCmp - 1), 1.0f); + DCL_APT_SetCounterCompare(aptx, APT_COMPARE_REFERENCE_A, tmp); + /* Sets the B Count compare reference of time-base counter. */ + tmp = (unsigned short)Clamp((float)(cntCmpB), (float)(maxCntCmp - 1), 1.0f); + DCL_APT_SetCounterCompare(aptx, APT_COMPARE_REFERENCE_B, tmp); +} + +/** + * @brief Open the three-phase lower pipe. + * @param aptAddr Three-phase APT address pointer. + * @param maxDutyCnt Max duty count. + * @retval None. + */ +static void AptTurnOnLowSidePwm(APT_RegStruct **aptAddr, unsigned short maxDutyCnt) +{ + MCS_ASSERT_PARAM(aptAddr != NULL); + MCS_ASSERT_PARAM(maxDutyCnt != 0); + unsigned short dutyCnt; + dutyCnt = (unsigned short)(maxDutyCnt * APT_FULL_DUTY); + /* Open the three-phase lower pipe */ + for (unsigned int i = 0; i < PHASE_MAX_NUM; i++) { + APT_RegStruct *aptx = (APT_RegStruct *)(aptAddr[i]); + DCL_APT_SetCounterCompare(aptx, APT_COMPARE_REFERENCE_C, dutyCnt); + DCL_APT_SetCounterCompare(aptx, APT_COMPARE_REFERENCE_D, dutyCnt); + } +} + +/** + * @brief Enable three-phase pwm output. + * @param aptAddr Three-phase APT address pointer. + * @retval None. + */ +static void MotorPwmOutputEnable(APT_RegStruct **aptAddr) +{ + MCS_ASSERT_PARAM(aptAddr != NULL); + /* Enable three-phase pwm output */ + for (unsigned int i = 0; i < PHASE_MAX_NUM; i++) { + APT_RegStruct *aptx = (APT_RegStruct *)(aptAddr[i]); + aptx->PG_OUT_FRC.BIT.rg_pga_frc_en = BASE_CFG_UNSET; + aptx->PG_OUT_FRC.BIT.rg_pgb_frc_en = BASE_CFG_UNSET; + } +} + +/** + * @brief Disable three-phase pwm output. + * @param aptAddr Three-phase APT address pointer. + * @retval None. + */ +static void MotorPwmOutputDisable(APT_RegStruct **aptAddr) +{ + MCS_ASSERT_PARAM(aptAddr != NULL); + /* Disable three-phase pwm output. */ + for (unsigned int i = 0; i < PHASE_MAX_NUM; i++) { + APT_RegStruct *aptx = (APT_RegStruct *)(aptAddr[i]); + aptx->PG_OUT_FRC.BIT.rg_pga_frc_en = BASE_CFG_SET; + aptx->PG_OUT_FRC.BIT.rg_pgb_frc_en = BASE_CFG_SET; + DCL_APT_ForcePWMOutputLow(aptx); + } +} + +/** + * @brief Smo IF angle difference calculation. + * @param smoElecAngle Smo electrical angle. + * @param ifCtrlAngle IF control angle. + * @retval signed short angle difference. + */ +static float SmoIfAngleDiffCalc(float smoElecAngle, float ifCtrlAngle) +{ + float diff = AngleSub(smoElecAngle, ifCtrlAngle); + /* Smo IF angle difference calculation */ + return diff; +} + +/** + * @brief Construct a new mcs startupswitch object. + * @param mtrCtrl The motor control handle. + * @retval None. + */ +static void MCS_StartupSwitch(MTRCTRL_Handle *mtrCtrl) +{ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + STARTUP_Handle *startup = &mtrCtrl->startup; + DqAxis *idqRef = &mtrCtrl->idqRef; + float iftargetAmp = mtrCtrl->ifCtrl.targetAmp; + float spdRefHz = mtrCtrl->spdRefHz; + + switch (startup->stage) { + case STARTUP_STAGE_CURR: + if (mtrCtrl->ifCtrl.curAmp >= iftargetAmp) { + /* Stage change */ + idqRef->q = iftargetAmp; + startup->stage = STARTUP_STAGE_SPD; + } else { + /* current amplitude increase */ + idqRef->q = IF_CurrAmpCalc(&mtrCtrl->ifCtrl); + spdRefHz = 0.0f; + } + break; + case STARTUP_STAGE_SPD: + /* current frequency increase */ + if (Abs(spdRefHz) >= startup->spdBegin) { + /* Stage change */ + startup->stage = STARTUP_STAGE_SWITCH; + TrigVal localTrigVal; + TrigCalc(&localTrigVal, SmoIfAngleDiffCalc(mtrCtrl->smo.elecAngle, mtrCtrl->ifCtrl.angle)); + idqRef->d = 0.0f; + mtrCtrl->spdCtrl.spdPi.integral = iftargetAmp * localTrigVal.cos; + } else { + /* Speed rmg */ + spdRefHz = RMG_Exec(&mtrCtrl->spdRmg, mtrCtrl->spdCmdHz); + } + break; + + case STARTUP_STAGE_SWITCH: + /* Switch from IF to SMO */ + spdRefHz = RMG_Exec(&mtrCtrl->spdRmg, mtrCtrl->spdCmdHz); + idqRef->q = SPDCTRL_Exec(&mtrCtrl->spdCtrl, mtrCtrl->spdRefHz, mtrCtrl->smo.spdEst); + /* Transitional stage, if current reference speed > critical speed, change to next stage */ + if (spdRefHz >= startup->spdBegin + TEMP_3) { + /* Stage change */ + mtrCtrl->stateMachine = FSM_RUN; + } + break; + + default: + break; + } + mtrCtrl->spdRefHz = spdRefHz; +} + +/** + * @brief Pre-processing of motor status. + * @param statusReg System status. + * @param stateMachine Motor Control Status. + * @retval None. + */ +static void MotorStatePerProc(SysStatusReg *statusReg, volatile FsmState *stateMachine) +{ + MCS_ASSERT_PARAM(statusReg != NULL); + MCS_ASSERT_PARAM(stateMachine != NULL); + /* Get system status */ + if (SysIsError(statusReg)) { + *stateMachine = FSM_FAULT; + } + if (SysGetCmdStop(statusReg)) { + SysCmdStopClr(statusReg); + *stateMachine = FSM_STOP; + } +} + +/** + * @brief Check over current status. + * @param statusReg System status. + * @param stateMachine Motor Control Status. + * @retval None. + */ +static void CheckOverCurrentState(SysStatusReg *statusReg, FsmState *stateMachine) +{ + MCS_ASSERT_PARAM(statusReg != NULL); + MCS_ASSERT_PARAM(stateMachine != NULL); + /* check systerm error status */ + if (SysIsError(statusReg) == false) { + *stateMachine = FSM_IDLE; + } +} + +/** + * @brief Check bootstrap capacitor charge time. + * @param mtrCtrl The motor control handle. + * @param stateMachine Motor Control Status. + * @retval None. + */ +static void CheckBootstrpCapChargeTime(MTRCTRL_Handle *mtrCtrl, FsmState *stateMachine) +{ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + MCS_ASSERT_PARAM(stateMachine != NULL); + mtrCtrl->sysTickCnt++; + /* check bootstrap capacitor charge time */ + if (mtrCtrl->sysTickCnt == mtrCtrl->capChargeTickNum) { + *stateMachine = FSM_CLEAR; + } +} + +/** + * @brief Check systerm cmd start signal. + * @param mtrCtrl The motor control handle. + * @param aptAddr Three-phase APT address pointer. + * @param statusReg System status. + * @param stateMachine Motor Control Status. + * @retval None. + */ +static void CheckSysCmdStart(MTRCTRL_Handle *mtrCtrl, + APT_RegStruct **aptAddr, + SysStatusReg *statusReg, + FsmState *stateMachine) +{ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + MCS_ASSERT_PARAM(aptAddr != NULL); + MCS_ASSERT_PARAM(statusReg != NULL); + MCS_ASSERT_PARAM(stateMachine != NULL); + /* check start cmd */ + if (SysGetCmdStart(statusReg)) { + SysRunningSet(statusReg); + SysCmdStartClr(statusReg); + mtrCtrl->sysTickCnt = 0; + *stateMachine = FSM_CAP_CHARGE; + /* Preparation for charging the bootstrap capacitor. */ + AptTurnOnLowSidePwm(aptAddr, mtrCtrl->aptMaxcntCmp); + /* Out put pwm */ + MotorPwmOutputEnable(aptAddr); + } +} + +/** + * @brief System timer tick task. + * @param mtrCtrl The motor control handle. + * @param aptAddr Three-phase APT address pointer. + * @retval None. + */ +static void TSK_SystickIsr(MTRCTRL_Handle *mtrCtrl, APT_RegStruct **aptAddr) +{ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + MCS_ASSERT_PARAM(aptAddr != NULL); + SysStatusReg *statusReg = &mtrCtrl->statusReg; + FsmState *stateMachine = &mtrCtrl->stateMachine; + mtrCtrl->msTickCnt++; + /* Pre-processing of motor status. */ + MotorStatePerProc(statusReg, stateMachine); + /* statemachine */ + switch (*stateMachine) { + case FSM_IDLE: + /* Set smo estimate speed before motor start-up */ + g_mc.smo.spdEst = 0.0f; + CheckSysCmdStart(mtrCtrl, aptAddr, statusReg, stateMachine); + break; + case FSM_CAP_CHARGE: + /* Bootstrap Capacitor Charging Timing */ + CheckBootstrpCapChargeTime(mtrCtrl, stateMachine); + break; + /* Clear parameter before start */ + case FSM_CLEAR: + ClearBeforeStartup(mtrCtrl); + *stateMachine = FSM_STARTUP; + break; + case FSM_STARTUP: + MCS_StartupSwitch(mtrCtrl); + break; + case FSM_RUN: + /* Speed ramp control */ + mtrCtrl->spdRefHz = RMG_Exec(&mtrCtrl->spdRmg, mtrCtrl->spdCmdHz); + /* Speed loop control */ + mtrCtrl->idqRef.q = SPDCTRL_Exec(&mtrCtrl->spdCtrl, mtrCtrl->spdRefHz, mtrCtrl->smo.spdEst); + break; + case FSM_STOP: + mtrCtrl->spdRefHz = 0.0f; + MotorPwmOutputDisable(aptAddr); + SysRunningClr(statusReg); + *stateMachine = FSM_IDLE; + break; + case FSM_FAULT: /* Overcurrent state */ + CheckOverCurrentState(statusReg, stateMachine); + break; + default: + break; + } +} + +/** + * @brief Read the ADC initialize bias trim value. + * @param mtrCtrl The motor control handle. + * @retval None. + */ +static void TrimInitAdcShiftValue(MTRCTRL_Handle *mtrCtrl) +{ + float adc0SampleTemp = 0.0f; /* Current bias value for temp store */ + float adc1SampleTemp = 0.0f; + float adc0TempSum = 0.0f; + float adc1TempSum = 0.0f; /* Current bias sum value for 20 times */ + float adcSampleTimes = 0.0f; /* ADC sample times */ + for (int i = 0; i < ADC_READINIT_TIMES; i++) { + adc0SampleTemp = (float)HAL_ADC_GetConvResult(&ADCU_HANDLE, ADCUSOCNUM); + adc1SampleTemp = (float)HAL_ADC_GetConvResult(&ADCW_HANDLE, ADCWSOCNUM); + BASE_FUNC_DELAY_US(200); /* 200 is delay count, delay 200us triger adc sampling */ + if (adc0SampleTemp > 1000.0f && adc1SampleTemp > 1000.0f) { + adcSampleTimes++; + adc0TempSum += adc0SampleTemp; + adc1TempSum += adc1SampleTemp; + } + } + if (adcSampleTimes < 1.0f) { + adcSampleTimes = 1.0f; /* Prevent divide-by-zero errors */ + } + adc0SampleTemp = adc0TempSum / adcSampleTimes; + adc1SampleTemp = adc1TempSum / adcSampleTimes; + /* Force convert to float */ + mtrCtrl->adc0Compensate = (float) adc0SampleTemp; + mtrCtrl->adc1Compensate = (float) adc1SampleTemp; + /* The normal value scope: 1800 < adc0Compensate < 2200 */ + if(g_mc.adc0Compensate < ADC_TRIMVALUE_MIN || g_mc.adc0Compensate > ADC_TRIMVALUE_MAX \ + || g_mc.adc1Compensate < ADC_TRIMVALUE_MIN || g_mc.adc1Compensate > ADC_TRIMVALUE_MAX) { + DBG_PRINTF("ADC trim value error,please reset!"); + HAL_GPIO_SetValue(&LED2_HANDLE, LED2_PIN, GPIO_LOW_LEVEL); + } + adcSampleTimes = 0; + adc0TempSum = 0; + adc1TempSum = 0; +} + +/** + * @brief Read the ADC current sampling value. + * @param CurrUvw Three-phase current. + * @retval None. + */ +static void ReadCurrUvw(UvwAxis *CurrUvw) +{ + MCS_ASSERT_PARAM(CurrUvw != NULL); + float adc0 = (float)HAL_ADC_GetConvResult(&ADCU_HANDLE, ADCUSOCNUM); + float adc1 = (float)HAL_ADC_GetConvResult(&ADCW_HANDLE, ADCWSOCNUM); + /* Convert adc sample value to current value */ + CurrUvw->u = -(adc0 - g_mc.adc0Compensate) * g_mc.adcCurrCofe; + CurrUvw->w = -(adc1 - g_mc.adc1Compensate) * g_mc.adcCurrCofe; + CurrUvw->v = -CurrUvw->u - CurrUvw->w; +} + +/** + * @brief Setting the APT Output Duty Cycle. + * @param aptx APT register base address. + * @param leftDuty Left duty cycle. + * @param rightDuty Right duty cycle. + * @retval None. + */ +static void SetPwmDuty(APT_Handle *aptx, float leftDuty, float rightDuty) +{ + MCS_ASSERT_PARAM(aptx != NULL); + MCS_ASSERT_PARAM(leftDuty > 0); + MCS_ASSERT_PARAM(rightDuty > 0); + unsigned short maxPeriodCnt = aptx->waveform.timerPeriod; + unsigned short cntCmpLeftEdge = (unsigned short)(leftDuty * maxPeriodCnt); + unsigned short cntCmpRightEdge = (unsigned short)(rightDuty * maxPeriodCnt); + /* avoid overflowing */ + cntCmpLeftEdge = (cntCmpLeftEdge > maxPeriodCnt) ? maxPeriodCnt : cntCmpLeftEdge; + cntCmpRightEdge = (cntCmpRightEdge > maxPeriodCnt) ? maxPeriodCnt : cntCmpRightEdge; + HAL_APT_SetPWMDuty(aptx, cntCmpLeftEdge, cntCmpRightEdge); +} + +/** + * @brief Duty Cycle Setting. + * @param dutyUvwLeft Three-phase left duty cycle. + * @param dutyUvwRight Three-phase right duty cycle. + * @retval None. + */ +static void SetPwmDutyCp(UvwAxis *dutyUvwLeft, UvwAxis *dutyUvwRight) +{ + MCS_ASSERT_PARAM(dutyUvwLeft != NULL); + MCS_ASSERT_PARAM(dutyUvwRight != NULL); + /* Setting the Three-Phase Duty Cycle */ + SetPwmDuty(&g_apt0, dutyUvwLeft->u, dutyUvwRight->u); + SetPwmDuty(&g_apt1, dutyUvwLeft->v, dutyUvwRight->v); + SetPwmDuty(&g_apt2, dutyUvwLeft->w, dutyUvwRight->w); +} + +/** + * @brief To set the ADC sampling trigger comparison value. + * @param cntCmpSOCA Soca Compare Count Value. + * @param cntCmpSOCB Socb Compare Count Value. + * @retval None. + */ +static void SetADCTriggerTime(unsigned short cntCmpSOCA, unsigned short cntCmpSOCB) +{ + MCS_SetAdcCompareR1(g_apt[PHASE_U], cntCmpSOCA, cntCmpSOCB, g_mc.aptMaxcntCmp); +} + +/** + * @brief Temprature table, the temprature detect range is 15 ~ 60 degree. + * @param tempResisValue Temperature sensor resistance. + * @retval None. + */ +static float TempTable(float tempResisValue) +{ + float boardTemp = 0.0f; + /* Temperatures between 15 and 30. */ + if (tempResisValue > TEMP_RES_30 && tempResisValue <= TEMP_RES_15) { + boardTemp = TEMP_15 + (TEMP_30 - TEMP_15) * (TEMP_RES_15 - tempResisValue) / (TEMP_RES_15 - TEMP_RES_30); + } else if (tempResisValue > TEMP_RES_45 && tempResisValue <= TEMP_RES_30) { /* Temperature between 30 and 45. */ + boardTemp = TEMP_30 + (TEMP_45 - TEMP_30) * (TEMP_RES_30 - tempResisValue) / (TEMP_RES_30 - TEMP_RES_45); + } else if (tempResisValue > TEMP_RES_60 && tempResisValue <= TEMP_RES_45) { /* Temperature between 45 and 50. */ + boardTemp = TEMP_45 + (TEMP_60 - TEMP_45) * (TEMP_RES_45 - tempResisValue) / (TEMP_RES_45 - TEMP_RES_60); + } else if (tempResisValue <= TEMP_RES_60) { + boardTemp = TEMP_60; /* If temperature is over 60, set temperature as 60. */ + } else if (tempResisValue >= TEMP_RES_15) { + boardTemp = TEMP_15; /* If temperature is lower 15, set temperature as 15. */ + } + return boardTemp; +} + +/** + * @brief Read power board temperature and udc. + * @retval None. + */ +static void ReadBoardTempAndUdc(void) +{ + HAL_ADC_SoftTrigSample(&ADCRESIS_HANDLE, ADCRESISSOCNUM); + HAL_ADC_SoftTrigSample(&ADCUDC_HANDLE, ADCUDCSOCNUM); + BASE_FUNC_DELAY_US(CNT_10); /* Delay 10 us. */ + /* Force convert to float type. */ + float resisAdcValue = (float)HAL_ADC_GetConvResult(&ADCRESIS_HANDLE, ADCRESISSOCNUM); + /* 10 / (x + 10) * 3.3 = resisAdcValue / 4096 * 3.3, x is resisValue, 10kohm is resistor divider value. */ + float resisValue = (4096.0f * 10.0f - 10.0f * resisAdcValue) / resisAdcValue; + g_mc.powerBoardTemp = TempTable(resisValue); + g_mc.udc = ((float)HAL_ADC_GetConvResult(&ADCUDC_HANDLE, ADCUDCSOCNUM)) * ADC_UDC_COFFI; +} + +/** + * @brief Execut abnormal feedback speed protect motion. + * @retval None. + */ +static void SpdFbkErrorProt_Exec(void) +{ + if (g_mc.prot.motorErrStatus.Bit.motorStalling == 0 && + g_mc.prot.motorErrStatus.Bit.overVoltErr == 0 && + g_mc.prot.motorErrStatus.Bit.lowerVoltErr == 0 && + g_mc.prot.motorErrStatus.Bit.overIpmTempErr == 0 && + g_mc.prot.motorErrStatus.Bit.overCurrErr == 0) { + g_mc.prot.motorErrStatus.Bit.revRotErr = 1; + /* If revRotErr, execute protect motion. */ + ProtSpo_Exec(g_apt); + } +} + +/** + * @brief Execut nan data protect motion. + * @retval None. + */ +static void NanDataDetect(void) +{ + static short errorSpdStatus = 0; + /* Detect the nan observer speed or current value. */ + if (isnan(g_mc.smo.spdEst) || isnan(g_mc.idqRef.q)) { + errorSpdStatus++; + } else { + errorSpdStatus = 0; + } + /* If the data is nan & continuous counting value is over 500 times, execute protect motion. + the detect time is 500 * 500us = 250ms. */ + if (errorSpdStatus >= 500) { + errorSpdStatus = 0; + SpdFbkErrorProt_Exec(); + } +} + +/** + * @brief Check abnormal feedback speed. + * @retval None. + */ +static void CheckSpdFbkStatus(void) +{ + static short errorCurrStatus = 0; + static short errorDeltaSpdStatus = 0; + NanDataDetect(); + if (g_mc.stateMachine == FSM_RUN) { + /* Detect the abnormal idq feedback current. */ + if (Abs(g_mc.idqRef.q - g_mc.idqFbk.q) >= CTRL_IF_CURR_AMP_A) { + errorCurrStatus++; + } else { + errorCurrStatus = 0; + } + /* Detect the abnormal feedback speed, the normal speed is > 0, if smo.spdEst < -10 && + delta speed error > USER_MIN_SPD_HZ + 10.0f at FSM_RUN stage, set the motor motion as error */ + if (g_mc.smo.spdEst < -10.0f && (g_mc.spdRefHz - g_mc.smo.spdEst > USER_MIN_SPD_HZ + 10.0f)) { + errorDeltaSpdStatus++; + } + } + /* Execute protect motion if count over 500 times, this error status caused by abnormal speed + or cabnormal urrent feedback, the detect time is 500 * 500us = 250ms. */ + if (errorCurrStatus >= 500) { + errorCurrStatus = 0; + SpdFbkErrorProt_Exec(); + } + /* This error statu caused by motor stalling, the detect time is 2 * 500us = 1ms. */ + if (errorDeltaSpdStatus >= 2) { + errorDeltaSpdStatus = 0; + g_mc.prot.motorErrStatus.Bit.motorStalling = 1; + } +} + +/** + * @brief Check Potentiometer Value callback function. + * @param param The TIMER_Handle. + * @retval None. + */ +void CheckPotentiometerValueCallback(void *param) +{ + MCS_ASSERT_PARAM(param != NULL); + BASE_FUNC_UNUSED(param); + static float potentiomitorAdcValue = 0.0f; + static float spdCmdHz = 0; + static float spdCmdHzLast = USER_MIN_SPD_HZ; /* 35.0 is spdCmdHzLast init value */ + HAL_ADC_SoftTrigSample(&ADCPTT_HANDLE, ADCPTTSOCNUM); + BASE_FUNC_DELAY_US(10); /* Delay 10 us. */ + potentiomitorAdcValue = (float)HAL_ADC_GetConvResult(&ADCPTT_HANDLE, ADCPTTSOCNUM); + /* 4045.0 is adc sample max value of potentiomitor, convert max spd to 180.25Hz */ + spdCmdHz = potentiomitorAdcValue / 4045.0f * USER_MAX_SPD_HZ; + if (Abs(spdCmdHzLast - spdCmdHz) < 1.0f) { /* Ignore changes less than 1. */ + return; + } + spdCmdHzLast = spdCmdHz; + if (spdCmdHz < USER_MIN_SPD_HZ) { /* 35.0 is spdCmdHz lower limit */ + spdCmdHz = USER_MIN_SPD_HZ; /* 35.0 is spdCmdHz lower limit */ + } + if (spdCmdHz > g_mc.mtrParam.maxElecSpd) { /* spdCmdHz upper limit */ + spdCmdHz = g_mc.mtrParam.maxElecSpd; /* spdCmdHz upper limit */ + } + if (g_mc.spdAdjustMode == CUST_SPEED_ADJUST) { + g_mc.spdCmdHz = spdCmdHz; + } +} + +/** + * @brief System timer ISR for Motor Statemachine CallBack function. + * @param param The systick timer handle. + * @retval None. + */ +void MotorStatemachineCallBack(void *param) +{ + /* Verify Parameters */ + MCS_ASSERT_PARAM(param != NULL); + BASE_FUNC_UNUSED(param); + /* Read power board temprature and voltage. */ + ReadBoardTempAndUdc(); + /* Motor speed loop state machine. */ + TSK_SystickIsr(&g_mc, g_apt); + + /* Motor error speed feedback check. */ + CheckSpdFbkStatus(); + /* Motor stalling detect. */ + STP_Det_ByCurrSpd(&g_mc.prot.stall, &g_mc.prot.motorErrStatus, g_mc.smo.spdEst, g_mc.idqFbk); + STP_Exec(&g_mc.prot.motorErrStatus, g_apt); + + /* Motor over voltage detect. */ + OVP_Det(&g_mc.prot.ovp, &g_mc.prot.motorErrStatus, g_mc.udc); + OVP_Exec(&g_mc.prot.ovp, &g_mc.spdRefHz, g_apt); + OVP_Recy(&g_mc.prot.ovp, &g_mc.prot.motorErrStatus, g_mc.udc); + /* Motor lower voltage detect. */ + LVP_Det(&g_mc.prot.lvp, &g_mc.prot.motorErrStatus, g_mc.udc); + LVP_Exec(&g_mc.prot.lvp, &g_mc.spdRefHz, g_apt); + LVP_Recy(&g_mc.prot.lvp, &g_mc.prot.motorErrStatus, g_mc.udc); + /* Power board over temperature detect. */ + OTP_Det(&g_mc.prot.otp, &g_mc.prot.motorErrStatus, OTP_IPM_ERR_BIT, g_mc.powerBoardTemp); + OTP_Exec(&g_mc.prot.otp, &g_mc.spdRefHz, g_apt); + OTP_Recy(&g_mc.prot.otp, &g_mc.prot.motorErrStatus, OTP_IPM_ERR_BIT, g_mc.powerBoardTemp); + + /* If protect level == 4, set motor state as stop. */ + if (g_mc.prot.ovp.protLevel == LEVEL_4 || g_mc.prot.lvp.protLevel == LEVEL_4 \ + || g_mc.prot.otp.protLevel == LEVEL_4) { + SysCmdStopSet(&g_mc.statusReg); + } +} + +/** + * @brief The carrier ISR wrapper function. + * @param aptHandle The APT handle. + * @retval None. + */ +void MotorCarrierProcessCallback(void *aptHandle) +{ + MCS_ASSERT_PARAM(aptHandle != NULL); + BASE_FUNC_UNUSED(aptHandle); + /* the carrierprocess of motor */ + MCS_CarrierProcess(&g_mc); + /* Over current protect */ + if (g_mc.stateMachine == FSM_RUN || g_mc.stateMachine == FSM_STARTUP) { + OCP_Det(&g_mc.prot.ocp, &g_mc.prot.motorErrStatus, g_mc.idqFbk); + OCP_Exec(&g_mc.prot.ocp, &g_mc.idqFbk, g_apt); /* Execute over current protect motion */ + if (g_mc.prot.ocp.protLevel < LEVEL_4) { + OCP_Recy(&g_mc.prot.ocp, &g_mc.prot.motorErrStatus); + } + } +} + +/** + * @brief Event interrupt callback function of APT module. + * @param para APT module handle. + * @retval None. + */ +void MotorSysErrCallback(void *para) +{ + MCS_ASSERT_PARAM(para != NULL); + APT_Handle *handle = (APT_Handle *)para; + /* The IPM overcurrent triggers and disables the three-phase PWM output. */ + MotorPwmOutputDisable(g_apt); + DCL_APT_ClearOutCtrlEventFlag((APT_RegStruct *)g_apt[PHASE_U], APT_OC_COMBINE_EVENT_A1); + DCL_APT_ClearOutCtrlEventFlag((APT_RegStruct *)g_apt[PHASE_V], APT_OC_COMBINE_EVENT_A1); + DCL_APT_ClearOutCtrlEventFlag((APT_RegStruct *)g_apt[PHASE_W], APT_OC_COMBINE_EVENT_A1); + /* Status setting error */ + SysErrorSet(&g_mc.statusReg); + DBG_PRINTF("APT error! \r\n"); + HAL_GPIO_SetValue(&LED2_HANDLE, LED2_PIN, GPIO_LOW_LEVEL); + BASE_FUNC_UNUSED(handle); +} + +/** + * @brief Init motor controller's data structure. + * @retval None. + */ +static void InitSoftware(void) +{ + /* Initializing motor control param */ + TSK_Init(); + /* Read phase-uvw current */ + g_mc.readCurrUvwCb = ReadCurrUvw; + g_mc.setPwmDutyCb = SetPwmDutyCp; + g_mc.setADCTriggerTimeCb = SetADCTriggerTime; +} + +/** + * @brief Config the KEY func. + * @param handle The GPIO handle. + * @retval None. + */ +static KEY_State Key_StateRead(GPIO_Handle *handle) +{ + if (HAL_GPIO_GetPinValue(handle, handle->pins) == 0) { + BASE_FUNC_DELAY_MS(30); /* delay 30ms for deshake */ + if (HAL_GPIO_GetPinValue(handle, handle->pins) == 0) { + while (HAL_GPIO_GetPinValue(handle, handle->pins) == 0) { + } + return KEY_DOWN; + } + } + return KEY_UP; +} + +/** + * @brief Control motor start and stop state by key func. + * @param param The GPIO handle. + * @retval None. + */ +void MotorStartStopKeyCallback(void *param) +{ + GPIO_Handle *handle = (GPIO_Handle *)param; + if (Key_StateRead(handle) == KEY_DOWN) { + if (g_mc.motorStateFlag == 0) { /* start motor */ + g_mc.motorStateFlag = 1; + SysCmdStartSet(&g_mc.statusReg); + } else if (g_mc.motorStateFlag == 1) { /* stop motor */ + g_mc.motorStateFlag = 0; + SysCmdStopSet(&g_mc.statusReg); + } + } +} + +/** + * @brief User application main entry function. + * @retval BSP_OK. + */ +int MotorMainProcess(void) +{ + unsigned int tickNum1Ms = 2; /* 1ms tick */ + static unsigned int tickCnt1Ms = 0; + unsigned int tickNum500Ms = 1000; /* 500ms tick */ + static unsigned int tickCnt500Ms = 0; + SystemInit(); + HMI_Init(); /* Init uart interrupt */ + HAL_TIMER_Start(&g_timer0); + HAL_TIMER_Start(&g_timer1); + + /* Disable PWM output before startup. */ + MotorPwmOutputDisable(g_apt); + /* Software initialization. */ + InitSoftware(); + /* Start the PWM clock. */ + HAL_APT_StartModule(RUN_APT0 | RUN_APT1 | RUN_APT2); + /* System Timer clock. */ + BASE_FUNC_DELAY_MS(ADC_READINIT_DELAY); + TrimInitAdcShiftValue(&g_mc); + BASE_FUNC_DELAY_MS(MOTOR_START_DELAY); + while (1) { + /* Cycling send data to host */ + HMI_Process_Tx(&g_mc); + if (g_mc.msTickCnt - tickCnt1Ms >= tickNum1Ms) { + tickCnt1Ms = g_mc.msTickCnt; + /* User Code 1ms Event */ + HMI_Process_Rx(&g_mc); + /* User Code 1ms Event */ + } + + if (g_mc.msTickCnt - tickCnt500Ms >= tickNum500Ms) { + if (SysIsError(&g_mc.statusReg) != true) { + /* LED toggle in normal status. */ + HAL_GPIO_TogglePin(&LED1_HANDLE, LED1_PIN); + } + tickCnt500Ms = g_mc.msTickCnt; + } + } + return 0; +} diff --git a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/cust_process.c b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/cust_process.c new file mode 100644 index 00000000..7c4f7525 --- /dev/null +++ b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/cust_process.c @@ -0,0 +1,718 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file cust_process.c + * @author MCU Algorithm Team + * @brief This file provides functions declaration of cust process interface. + */ + +#include "cust_process.h" +#include "mcs_ctlmode_config.h" +#include "mcs_math_const.h" +#include "mcs_user_config.h" +#include "mcs_assert.h" +#include "main.h" +#include "uart_module.h" +#include "mcs_mtr_param.h" +/* Macro definitions --------------------------------------------------------------------------- */ +/* Constant value. */ +#define CONST_VALUE_60 60.0f /* Constant value 60. */ +#define CONST_VALUE_DIV_1000 0.001f /* Constant value 1/1000. */ + +/* Data array index. */ +#define DATA_SEGMENT_ONE 0 /* First element of the data segment */ +#define DATA_SEGMENT_TWO 1 /* Second element of the data segment */ +#define DATA_SEGMENT_THREE 2 /* Third element of the data segment */ +#define DATA_SEGMENT_FOUR 3 /* Fourth element of the data segment */ +#define CUSTACKCODELEN 10 /* Ack code length */ + +/* Command code. */ +#define SET_PID_KP 0x01 /* Set Pid Kp Command Params */ +#define SET_PID_KI 0x02 /* Set Pid Ki Command Params */ +#define SET_PID_LIMIT 0x03 /* Set Pid limit Command Params */ + +#define SET_SMO1TH_PLL_BDW 0x01 /* Set Smo1th Pll BandWidth Command Params */ +#define SET_SMO1TH_SPDFLITER_FC 0x02 /* Set Smo1th Fc Command Params */ +#define SET_SMO1TH_FILCOMPANGLE 0x03 /* Set Smo1th FillComp Command Params */ + +#define SET_SMO4TH_KD 0x01 /* Set Smo4th Kd Command Params */ +#define SET_SMO4TH_KP 0x02 /* Set Smo4th Kq Command Params */ + +#define SET_SPD_COMMAND_HZ 0x01 /* Set Target Speed Command Params */ +#define SET_SPD_RMG_SLOPE 0x02 /* Set Speed Slope Command Params */ + +#define SET_MAX_ELEC_SPD 0x01 /* Set Max Motor Speed Command Params */ +#define SET_MOTOR_NUM_OF_PAIRS 0x02 /* Set Motor Command Params */ + +#define SET_MOTOR_RES_OF_STATOR 0x01 /* Set Motor Res Command Params */ +#define SET_MOTOR_DAXIS_INDUCTANCE 0x02 /* Set Motor Daxis Inductance Command Params */ +#define SET_MOTOR_QAXIS_INDUCTANCE 0x03 /* Set Motor Qaxis Inductance Command Params */ + +#define SET_SVPWM_VOLTAGE_PER_UNIT 0x01 /* Set Svpwm Voltage Params */ +#define SET_ADC_CURRENT_SAMPLE_COFE 0x02 /* Set Adc coffe Params */ +#define SET_CURRENT_LOOP_CONTROL_PERIOD 0x03 /* Set Current loop period Command Params */ + +#define SET_IF_TARGET_CURRENT_VALUE 0x01 /* Set If Target Params */ +#define SET_INCREMENT_OF_IF_CURRENT 0x02 /* Set If Step Command Params */ +#define SET_SPEED_RING_BEGIN_SPEED 0x03 /* Set If to Smo Start Speed Command Params */ + +static unsigned char ackCode = 0; +static unsigned char g_uartTxBuf[CUSTACKCODELEN] = {0}; + +/** + * @brief Set observer type. + * @param mtrCtrl The motor control handle. + * @param rxData Receive buffer + */ +static void CMDCODE_SetObserverType(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) +{ + /* Get function code. */ + int funcCode = (int)(rxData->data[DATA_SEGMENT_ONE].typeF); + if (funcCode == FOC_OBSERVERTYPE_SMO1TH) { + ackCode = 0X01; + mtrCtrl->obserType = FOC_OBSERVERTYPE_SMO1TH; + CUST_AckCode(g_uartTxBuf, ackCode, mtrCtrl->obserType); + } else if (funcCode == FOC_OBSERVERTYPE_SMO4TH) { + ackCode = 0X02; + mtrCtrl->obserType = FOC_OBSERVERTYPE_SMO4TH; + CUST_AckCode(g_uartTxBuf, ackCode, mtrCtrl->obserType); + } +} + +/** + * @brief Set pid parameter ack code. + * @param funcCode Received data funccode. + */ +static unsigned char SetPidAckCode(int funcCode) +{ + switch (funcCode) { + /* Set current loop D-Axis PID parameter ack code. */ + case FOC_CURDAXISPID_PARAMS: + ackCode = 0xE0; + break; + /* Set current loop Q-Axis PID parameter ack code. */ + case FOC_CURQAXISPID_PARAMS: + ackCode = 0xE3; + break; + /* Set speed loop PID parameter ack code. */ + case FOC_SPDPID_PARAMS: + ackCode = 0xE6; + break; + default: + break; + } + return ackCode; +} + +/** + * @brief Set pid parameters. + * @param pidHandle The pid control handle. + * @param rxData Receive buffer + */ +static void SetPidParams(PID_Handle *pidHandle, CUSTDATATYPE_DEF *rxData) +{ + /* Get function code. */ + int funcCode = (int)(rxData->data[DATA_SEGMENT_ONE].typeF); + /* Get command code. */ + int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); + switch (cmdCode) { + case SET_PID_KP: /* Set the P parameter. */ + PID_SetKp(pidHandle, rxData->data[DATA_SEGMENT_THREE].typeF); + ackCode = SetPidAckCode(funcCode); + CUST_AckCode(g_uartTxBuf, (unsigned char)(ackCode + SET_PID_KP), rxData->data[DATA_SEGMENT_THREE].typeF); + break; + case SET_PID_KI: /* Set the I parameter. */ + PID_SetKi(pidHandle, rxData->data[DATA_SEGMENT_THREE].typeF); + ackCode = SetPidAckCode(funcCode); + CUST_AckCode(g_uartTxBuf, (unsigned char)(ackCode + SET_PID_KI), rxData->data[DATA_SEGMENT_THREE].typeF); + break; + case SET_PID_LIMIT: /* Set the pid limit. */ + PID_SetLimit(pidHandle, rxData->data[DATA_SEGMENT_THREE].typeF); + ackCode = SetPidAckCode(funcCode); + CUST_AckCode(g_uartTxBuf, (unsigned char)(ackCode + SET_PID_LIMIT), rxData->data[DATA_SEGMENT_THREE].typeF); + break; + default: + ackCode = 0X77; + CUST_AckCode(g_uartTxBuf, ackCode, 0); + break; + } +} + +/** + * @brief Set motor pid parameters. + * @param mtrCtrl The motor control handle. + * @param rxData Receive buffer + */ +static void CMDCODE_SetMotorPidParams(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) +{ + /* Get function code. */ + int funcCode = (int)(rxData->data[DATA_SEGMENT_ONE].typeF); + + if (funcCode == FOC_CURDAXISPID_PARAMS) { + SetPidParams(&mtrCtrl->currCtrl.dAxisPi, rxData); /* Set Curr loop Daxis pid params */ + } else if (funcCode == FOC_CURQAXISPID_PARAMS) { + SetPidParams(&mtrCtrl->currCtrl.qAxisPi, rxData); /* Set Curr loop Qaxis pid params */ + mtrCtrl->currCtrl.dAxisPi.upperLimit = mtrCtrl->currCtrl.qAxisPi.upperLimit; + mtrCtrl->currCtrl.dAxisPi.lowerLimit = mtrCtrl->currCtrl.qAxisPi.lowerLimit; + } else if (funcCode == FOC_SPDPID_PARAMS) { + SetPidParams(&mtrCtrl->spdCtrl.spdPi, rxData); /* Set Speed loop params */ + } +} + +/** + * @brief Set first order sliding mode observer parameters. + * @param smoHandle The observer control handle. + * @param rxData Receive buffer + */ +static void SetObserverSmo1thParams(FOSMO_Handle *smoHandle, CUSTDATATYPE_DEF *rxData) +{ + smoHandle->kSmo = rxData->data[DATA_SEGMENT_TWO].typeF; + ackCode = 0X09; + CUST_AckCode(g_uartTxBuf, ackCode, smoHandle->kSmo); +} + +/** + * @brief Set first order sliding mode observer's phase-locked loop parameters. + * @param smoHandle The observer control handle. + * @param rxData Receive buffer + */ +static void SetObserverSmo1thPLLParams(FOSMO_Handle *smoHandle, CUSTDATATYPE_DEF *rxData) +{ + /* Get command code. */ + int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); + float pllBdw = 0.0f; + switch (cmdCode) { + case SET_SMO1TH_PLL_BDW: /* Set the bandwidth. */ + pllBdw = rxData->data[DATA_SEGMENT_THREE].typeF; + smoHandle->pll.pi.kp = 2.0f * pllBdw; /* kp = 2.0f * pllBdw */ + smoHandle->pll.pi.ki = pllBdw * pllBdw; /* ki = pllBdw * pllBdw */ + ackCode = 0X0A; + CUST_AckCode(g_uartTxBuf, ackCode, pllBdw); + break; + case SET_SMO1TH_SPDFLITER_FC: /* Set the cutoff frequency. */ + smoHandle->spdFilter.fc = rxData->data[DATA_SEGMENT_THREE].typeF; + smoHandle->spdFilter.a1 = 1.0f / (1.0f + DOUBLE_PI * smoHandle->spdFilter.fc * CTRL_CURR_PERIOD); + smoHandle->spdFilter.b1 = 1.0f - smoHandle->spdFilter.a1; + ackCode = 0X0B; + CUST_AckCode(g_uartTxBuf, ackCode, smoHandle->spdFilter.fc); + break; + case SET_SMO1TH_FILCOMPANGLE: /* Set the compensation angle. */ + smoHandle->filCompAngle = rxData->data[DATA_SEGMENT_THREE].typeF; + ackCode = 0X0C; + CUST_AckCode(g_uartTxBuf, ackCode, smoHandle->filCompAngle); + break; + default: + ackCode = 0X77; + CUST_AckCode(g_uartTxBuf, ackCode, 0); + break; + } +} + +/** + * @brief Set fourth order sliding mode observer parameters. + * @param smo4thHandle The observer control handle. + * @param rxData Receive buffer + */ +static void SetObserverSmo4thParams(SMO4TH_Handle *smo4thHandle, CUSTDATATYPE_DEF *rxData) +{ + /* Get command code. */ + int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); + + switch (cmdCode) { + case SET_SMO4TH_KD: /* Set d axis gain. */ + smo4thHandle->kd = rxData->data[DATA_SEGMENT_THREE].typeF; + ackCode = 0X0D; + CUST_AckCode(g_uartTxBuf, ackCode, smo4thHandle->kd); + break; + case SET_SMO4TH_KP: /* Set q axis gain. */ + smo4thHandle->kq = rxData->data[DATA_SEGMENT_THREE].typeF; + ackCode = 0X0E; + CUST_AckCode(g_uartTxBuf, ackCode, smo4thHandle->kq); + break; + default: + break; + } +} + +/** + * @brief Set fourth order sliding mode observer's phase-locked loop parameters. + * @param smo4thHandle The observer control handle. + * @param rxData Receive buffer + */ +static void SetObserverSmo4thPLLParams(SMO4TH_Handle *smo4thHandle, CUSTDATATYPE_DEF *rxData) +{ + float pllBdw = rxData->data[DATA_SEGMENT_TWO].typeF; + smo4thHandle->pll.pi.kp = (2.0f) * pllBdw; /* kp = 2.0f * pllBdw */ + smo4thHandle->pll.pi.ki = pllBdw * pllBdw; + ackCode = 0X11; + CUST_AckCode(g_uartTxBuf, ackCode, pllBdw); +} + +/** + * @brief Set observer parameters. + * @param mtrCtrl The motor control handle. + * @param rxData Receive buffer + */ +static void CMDCODE_SetObserverParams(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) +{ + /* Get function code. */ + int funcCode = (int)(rxData->data[DATA_SEGMENT_ONE].typeF); + + if (funcCode == FOC_OBSERVERTYPE_SMO1TH) { + SetObserverSmo1thParams(&mtrCtrl->smo, rxData); + } else if (funcCode == FOC_OBSERVERTYPE_SMO1TH_PLL) { + SetObserverSmo1thPLLParams(&mtrCtrl->smo, rxData); + } else if (funcCode == FOC_OBSERVERTYPE_SMO4TH) { + SetObserverSmo4thParams(&mtrCtrl->smo4th, rxData); + } else if (funcCode == FOC_OBSERVERTYPE_SMO4TH_PLL) { + SetObserverSmo4thPLLParams(&mtrCtrl->smo4th, rxData); + } +} + +/** + * @brief Set motor speed and speed slope. + * @param mtrCtrl The motor control handle. + * @param rxData Receive buffer + */ +static void CMDCODE_SetMotorSpdAndSlope(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) +{ + /* Get command code. */ + int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); + + switch (cmdCode) { + case SET_SPD_COMMAND_HZ: /* Set target speed(hz). */ + mtrCtrl->spdCmdHz = rxData->data[DATA_SEGMENT_THREE].typeF * mtrCtrl->mtrParam.mtrNp / CONST_VALUE_60; + /* Judgement the value > 0.00001, make sure denominator != 0. */ + if (rxData->data[DATA_SEGMENT_FOUR].typeF > 0.00001f) { + mtrCtrl->spdRmg.delta = mtrCtrl->spdCmdHz / rxData->data[DATA_SEGMENT_FOUR].typeF * CTRL_SYSTICK_PERIOD; + } + ackCode = 0X16; + CUST_AckCode(g_uartTxBuf, ackCode, mtrCtrl->spdCmdHz); + break; + case SET_SPD_RMG_SLOPE: /* Set speed slope. */ + mtrCtrl->spdRmg.delta = mtrCtrl->spdCmdHz / rxData->data[DATA_SEGMENT_THREE].typeF * CTRL_SYSTICK_PERIOD; + ackCode = 0X17; + CUST_AckCode(g_uartTxBuf, ackCode, rxData->data[DATA_SEGMENT_THREE].typeF); + break; + default: + break; + } +} + +/** + * @brief Set motor base parameters. + * @param mtrParamHandle The motor parameters handle. + * @param rxData Receive buffer + */ +static void SetMotorBaseParams(MOTOR_Param *mtrParamHandle, CUSTDATATYPE_DEF *rxData) +{ + /* Get command code. */ + int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); + + switch (cmdCode) { + case SET_MAX_ELEC_SPD: /* Set max electric speed. */ + mtrParamHandle->maxElecSpd = rxData->data[DATA_SEGMENT_THREE].typeF / + CONST_VALUE_60 * mtrParamHandle->mtrNp; + ackCode = 0X18; + CUST_AckCode(g_uartTxBuf, ackCode, rxData->data[DATA_SEGMENT_THREE].typeF); + break; + case SET_MOTOR_NUM_OF_PAIRS: /* Set the number of motor pole pairs. */ + mtrParamHandle->mtrNp = (unsigned short)(rxData->data[DATA_SEGMENT_THREE].typeF); + ackCode = 0X19; + CUST_AckCode(g_uartTxBuf, ackCode, (float)mtrParamHandle->mtrNp); + break; + default: + break; + } +} + +/** + * @brief Set motor special parameters. + * @param mtrParamHandle The motor parameters handle. + * @param rxData Receive buffer + */ +static void SetMotorSpecialParams(MOTOR_Param *mtrParamHandle, CUSTDATATYPE_DEF *rxData) +{ + /* Get command code. */ + int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); + + switch (cmdCode) { + case SET_MOTOR_RES_OF_STATOR: /* Set the resistor of stator. */ + mtrParamHandle->mtrRs = rxData->data[DATA_SEGMENT_THREE].typeF; + ackCode = 0X1A; + CUST_AckCode(g_uartTxBuf, ackCode, mtrParamHandle->mtrRs); + break; + case SET_MOTOR_DAXIS_INDUCTANCE: /* Set the d axis inductance. */ + mtrParamHandle->mtrLd = rxData->data[DATA_SEGMENT_THREE].typeF; + ackCode = 0X1B; + CUST_AckCode(g_uartTxBuf, ackCode, mtrParamHandle->mtrLd); + break; + case SET_MOTOR_QAXIS_INDUCTANCE: /* Set the q axis inductance. */ + mtrParamHandle->mtrLq = rxData->data[DATA_SEGMENT_THREE].typeF; + ackCode = 0X1C; + CUST_AckCode(g_uartTxBuf, ackCode, mtrParamHandle->mtrLq); + break; + default: + ackCode = 0X77; + CUST_AckCode(g_uartTxBuf, ackCode, 0); + break; + } +} + +/** + * @brief Set motor board parameters. + * @param mtrCtrl The motor control handle. + * @param rxData Receive buffer + */ +static void SetMotorBoardParams(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) +{ + /* Get command code. */ + int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); + + switch (cmdCode) { + case SET_SVPWM_VOLTAGE_PER_UNIT: /* Set svpwm voltage per unit. */ + mtrCtrl->sv.voltPu = rxData->data[DATA_SEGMENT_THREE].typeF * ONE_DIV_SQRT3; + mtrCtrl->currCtrl.outLimit = mtrCtrl->sv.voltPu * ONE_DIV_SQRT3; + ackCode = 0X1D; + CUST_AckCode(g_uartTxBuf, ackCode, rxData->data[DATA_SEGMENT_THREE].typeF); + break; + case SET_ADC_CURRENT_SAMPLE_COFE: /* Set adc current sample cofeature. */ + mtrCtrl->adcCurrCofe = rxData->data[DATA_SEGMENT_THREE].typeF; + ackCode = 0X1E; + CUST_AckCode(g_uartTxBuf, ackCode, mtrCtrl->adcCurrCofe); + break; + case SET_CURRENT_LOOP_CONTROL_PERIOD: /* Set current loop control period. */ + mtrCtrl->currCtrlPeriod = 1 / rxData->data[DATA_SEGMENT_THREE].typeF * CONST_VALUE_DIV_1000; + ackCode = 0X1F; + CUST_AckCode(g_uartTxBuf, ackCode, rxData->data[DATA_SEGMENT_THREE].typeF); + break; + default: + break; + } +} + +/** + * @brief Set motor parameters. + * @param mtrCtrl The motor control handle. + * @param rxData Receive buffer + */ +static void CMDCODE_SetMotorParams(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) +{ + /* Get function code. */ + int funcCode = (int)(rxData->data[DATA_SEGMENT_ONE].typeF); + + if (funcCode == MOTOR_PARAMS_BASE) { + SetMotorBaseParams(&mtrCtrl->mtrParam, rxData); + } else if (funcCode == MOTOR_PARAMS_SPECIAL) { + SetMotorSpecialParams(&mtrCtrl->mtrParam, rxData); + } else if (funcCode == MOTOR_PARAMS_BOARD) { + SetMotorBoardParams(mtrCtrl, rxData); + } +} + +/** + * @brief Motor start. + * @param mtrCtrl The motor control handle. + */ +static void CMDCODE_MotorStart(MTRCTRL_Handle *mtrCtrl) +{ + if (mtrCtrl->stateMachine != FSM_RUN) { + SysCmdStartSet(&mtrCtrl->statusReg); /* start motor. */ + mtrCtrl->motorStateFlag = 1; + ackCode = 0X24; /* send ackcode to host. */ + CUST_AckCode(g_uartTxBuf, ackCode, 1); + } +} + +/** + * @brief Motor stop. + * @param mtrCtrl The motor control handle. + */ +static void CMDCODE_MotorStop(MTRCTRL_Handle *mtrCtrl) +{ + SysCmdStopSet(&mtrCtrl->statusReg); + mtrCtrl->motorStateFlag = 0; + ackCode = 0X25; + CUST_AckCode(g_uartTxBuf, ackCode, 0); +} + +/** + * @brief Motor state reset. + * @param mtrCtrl The motor control handle. + */ +static void CMDCODE_MotorReset(MTRCTRL_Handle *mtrCtrl) +{ + BASE_FUNC_UNUSED(mtrCtrl); + BASE_FUNC_SoftReset(); +} + +/** + * @brief Set IF-Startup parameters. + * @param mtrCtrl The motor control handle. + * @param rxData Receive buffer + */ +static void SetStartupIFParams(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) +{ + /* Get command code. */ + int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); + + switch (cmdCode) { + case SET_IF_TARGET_CURRENT_VALUE: /* Set I/F start up target current value. */ + mtrCtrl->ifCtrl.targetAmp = rxData->data[DATA_SEGMENT_THREE].typeF; + ackCode = 0X26; + CUST_AckCode(g_uartTxBuf, ackCode, mtrCtrl->ifCtrl.targetAmp); + break; + case SET_INCREMENT_OF_IF_CURRENT: /* Set increment of I/F start up current. */ + mtrCtrl->ifCtrl.stepAmp = mtrCtrl->ifCtrl.targetAmp / rxData->data[DATA_SEGMENT_THREE].typeF * + CTRL_SYSTICK_PERIOD; + ackCode = 0X27; + CUST_AckCode(g_uartTxBuf, ackCode, mtrCtrl->ifCtrl.stepAmp); + break; + case SET_SPEED_RING_BEGIN_SPEED: /* Set speed ring begin speed. */ + mtrCtrl->startup.spdBegin = rxData->data[DATA_SEGMENT_THREE].typeF / + CONST_VALUE_60 * mtrCtrl->mtrParam.mtrNp; + ackCode = 0X28; + CUST_AckCode(g_uartTxBuf, ackCode, rxData->data[DATA_SEGMENT_THREE].typeF); + break; + default: + break; + } +} + +/** + * @brief Set start up parameters. + * @param mtrCtrl The motor control handle. + * @param rxData Receive buffer + */ +static void CMDCODE_SetStartupParams(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) +{ + /* Get function code. */ + int funcCode = (int)(rxData->data[DATA_SEGMENT_ONE].typeF); + + if (funcCode == FOC_STARTUP_IF) { + SetStartupIFParams(mtrCtrl, rxData); + } +} + +/** + * @brief Set adjust speed mode. + * @param mtrCtrl The motor control handle. + * @param rxData Receive buffer + */ +static void CMDCODE_SetAdjustSpdMode(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) +{ + /* Get function code. */ + int funcCode = (int)(rxData->data[DATA_SEGMENT_ONE].typeF); + /* Get commond code. */ + int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); + if (funcCode == HOST_SPEED_ADJUST) { + mtrCtrl->spdAdjustMode = HOST_SPEED_ADJUST; + /* Uart connect success. */ + mtrCtrl->uartConnectFlag = CONNECTING; + ackCode = 0X2A; + CUST_AckCode(g_uartTxBuf, ackCode, (float)mtrCtrl->spdAdjustMode); + } else if (funcCode == CUST_SPEED_ADJUST) { + if (cmdCode == 1) { /* If uart connection disconnected & stop motor commond. */ + SysCmdStopSet(&mtrCtrl->statusReg); + mtrCtrl->motorStateFlag = 0; + } + mtrCtrl->spdAdjustMode = CUST_SPEED_ADJUST; + ackCode = 0X2B; + CUST_AckCode(g_uartTxBuf, ackCode, (float)mtrCtrl->spdAdjustMode); + /* Uart disconnect. */ + mtrCtrl->uartConnectFlag = DISCONNECT; + } +} + +/** + * @brief Check uart connect. + * @param mtrCtrl The motor control handle. + * @param rxData Receive buffer + */ +static void CMDCODE_UartHandShake(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) +{ + CMDCODE_SetAdjustSpdMode(mtrCtrl, rxData); +} + +/** + * @brief Set Motor Initial Status Parameters. + * @param mtrCtrl The motor control handle. + * @param rxData Receive buffer + * @param code Instruction code. + */ +static void CMDCODE_EXE_SetMotorInitParams(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData, unsigned char code) +{ + switch (code) { + case CMDCODE_SET_OBSERVER_TYPE: { /* Set observer type. */ + CMDCODE_SetObserverType(mtrCtrl, rxData); + } + break; + case CMDCODE_SET_PID_PARAMS: { /* Set motor pid parameters. */ + CMDCODE_SetMotorPidParams(mtrCtrl, rxData); + } + break; + case CMDCODE_SET_OBSERVER_PARAMS: { /* Set observer parameters. */ + CMDCODE_SetObserverParams(mtrCtrl, rxData); + } + break; + case CMDCODE_SET_MOTOR_TARGETSPD: { /* Set motor speed and speed slope. */ + CMDCODE_SetMotorSpdAndSlope(mtrCtrl, rxData); + } + break; + case CMDCODE_SET_MOTOR_PARAMS: { /* Set motor parameters. */ + CMDCODE_SetMotorParams(mtrCtrl, rxData); + } + break; + case CMDCODE_SET_ADJUSTSPD_MODE: /* Set adjust speed mode. */ + CMDCODE_SetAdjustSpdMode(mtrCtrl, rxData); + break; + default: + break; + } +} + +/** + * @brief Set Motor Control System Status. + * @param mtrCtrl The motor control handle. + * @param rxData Receive buffer + * @param code Instruction code. + */ +static void CMDCODE_EXE_SetMotorState(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData, unsigned char code) +{ + BASE_FUNC_UNUSED(rxData); + switch (code) { + case CMDCODE_MOTOR_START: { /* Motor start command */ + CMDCODE_MotorStart(mtrCtrl); + } + break; + case CMDCODE_MOTOR_STOP: { /* Motor stop command */ + CMDCODE_MotorStop(mtrCtrl); + } + break; + case CMDCODE_MOTORSTATE_RESET: { /* Motor reset command */ + CMDCODE_MotorReset(mtrCtrl); + } + break; + default: + break; + } +} + +/** + * @brief Set Startup and Uart Link Handshake Flag Parameters. + * @param mtrCtrl The motor control handle. + * @param rxData Receive buffer + * @param code Instruction code. + */ +static void CMDCODE_EXE_SetOtherParams(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData, unsigned char code) +{ + switch (code) { + case CMDCODE_SET_STARTUP_PARAMS: /* Set start up parameters. */ + CMDCODE_SetStartupParams(mtrCtrl, rxData); + break; + case CMDCODE_UART_HANDSHAKE: { /* Check uart hand shake. */ + CMDCODE_UartHandShake(mtrCtrl, rxData); + } + break; + case CMDCODE_UART_HEARTDETECT: { /* Check uart hand shake. */ + mtrCtrl->uartHeartDetCnt++; + } + break; + default: + break; + } +} +/** + * @brief Instruction code executor. + * @param mtrCtrl The motor control handle. + * @param rxData Receive buffer + * @param code Instruction code. + */ +static void CMDCODE_EXE_Process(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData, unsigned char code) +{ + CMDCODE_EXE_SetMotorInitParams(mtrCtrl, rxData, code); + CMDCODE_EXE_SetMotorState(mtrCtrl, rxData, code); + CMDCODE_EXE_SetOtherParams(mtrCtrl, rxData, code); +} + +/** + * @brief Host data download callback and data parsing. + * @param mtrCtrl The motor control handle. + * @param rxBuf Receive buffer + */ +void CUST_UartDataProcess(MTRCTRL_Handle *mtrCtrl, unsigned char *rxBuf) +{ + /* Verify Parameters */ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + MCS_ASSERT_PARAM(rxBuf != NULL); + + /* Uart data storage struct */ + CUSTDATATYPE_DEF data; + volatile unsigned char *ptrCnt = &rxBuf[FRAME_CHECK_BEGIN + 1]; + volatile unsigned char *strCnt = &data.data[0].typeCh[0]; + for (unsigned int j = 0; j < FRAME_RECV_DATA_LENTH * FRAME_ONE_DATA_LENTH; j++) { + *strCnt++ = *ptrCnt++; + } + /* Message function code. */ + data.code = rxBuf[FRAME_CHECK_BEGIN]; + CMDCODE_EXE_Process(mtrCtrl, &data, data.code); +} + +/** + * @brief The host computer displays data transmission. + * @param mtrCtrl The motor control handle. + * @param txData Message content. + * @param stage Message status function code. + */ +void CUST_SetTxMsg(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *txData) +{ + /* Verify Parameters */ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + MCS_ASSERT_PARAM(txData != NULL); + if (mtrCtrl->stateMachine == FSM_IDLE) { + mtrCtrl->smo.spdEst = 0.0f; + } + /* Data send to host. */ + txData->data[CURRDQ_Q].typeF = mtrCtrl->idqFbk.q; + txData->data[CURRDQ_D].typeF = mtrCtrl->idqFbk.d; + txData->data[CURRREFDQ_Q].typeF = mtrCtrl->idqRef.q; + txData->data[CURRREFDQ_D].typeF = mtrCtrl->idqRef.d; + /* Motor current speed. */ + txData->data[CURRSPD].typeF = mtrCtrl->smo.spdEst * CONST_VALUE_60 / mtrCtrl->mtrParam.mtrNp; + /* Motor commond speed. */ + txData->data[SPDCMDHZ].typeF = mtrCtrl->spdCmdHz * CONST_VALUE_60 / mtrCtrl->mtrParam.mtrNp; + /* Bus voltage. */ + txData->data[UDC].typeF = mtrCtrl->udc; + /* Power board temprature. */ + txData->data[POWERBOARDTEMP].typeF = mtrCtrl->powerBoardTemp; + /* Motor protection status flag. */ + txData->data[CUST_ERR_CODE].typeI = mtrCtrl->prot.motorErrStatus.all; + /* Three phase current. */ + txData->data[CURRUVW_U].typeF = mtrCtrl->currUvw.u; + txData->data[CURRUVW_V].typeF = mtrCtrl->currUvw.v; + txData->data[CURRUVW_W].typeF = mtrCtrl->currUvw.w; + /* Three phase pwm duty. */ + txData->data[PWMDUTYUVW_U].typeF = mtrCtrl->dutyUvw.u; + txData->data[PWMDUTYUVW_V].typeF = mtrCtrl->dutyUvw.v; + txData->data[PWMDUTYUVW_W].typeF = mtrCtrl->dutyUvw.w; + /* Motor electric angle. */ + txData->data[AXISANGLE].typeF = mtrCtrl->axisAngle; + txData->data[VDQ_Q].typeF = mtrCtrl->vdqRef.q; + txData->data[VDQ_D].typeF = mtrCtrl->vdqRef.d; + txData->data[SPDREFHZ].typeF = mtrCtrl->spdRefHz * CONST_VALUE_60 / mtrCtrl->mtrParam.mtrNp; + txData->data[SENDTIMESTAMP].typeF = mtrCtrl->uartTimeStamp; +} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/cust_process.h b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/cust_process.h new file mode 100644 index 00000000..60fe69bb --- /dev/null +++ b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/cust_process.h @@ -0,0 +1,36 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file cust_process.h + * @author MCU Algorithm Team + * @brief This file provides functions declaration of system status. + */ + +/* Define to prevent recursive inclusion ------------------------------------------------------- */ +#ifndef McsMagicTag_HMI_MOUDLE_H +#define McsMagicTag_HMI_MOUDLE_H + +/* Includes ------------------------------------------------------------------------------------ */ +#include "protocol.h" +/* Typedef definitions ------------------------------------------------------------------------- */ + + +void CUST_UartDataProcess(MTRCTRL_Handle *mtrCtrl, unsigned char *rxBuf); + +void CUST_SetTxMsg(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *txData); + +#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/hmi_module.c b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/hmi_module.c new file mode 100644 index 00000000..58bf15a7 --- /dev/null +++ b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/hmi_module.c @@ -0,0 +1,57 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file hmi_module.c + * @author MCU Algorithm Team + * @brief This file provides functions declaration of human-machine interface. + */ + +#include "hmi_module.h" +#include "mcs_assert.h" + +/** + * @brief HMI Initializatio. + * @retval None. + */ +void HMI_Init(void) +{ + UartRecvInit(); +} + +/** + * @brief HMI processing. + * @param mtrCtrl The motor control handle.. + * @retval None. + */ +void HMI_Process_Rx(MTRCTRL_Handle *mtrCtrl) +{ + /* Verify Parameters */ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + UartModuleProcess_Rx(mtrCtrl); +} + +/** + * @brief HMI processing. + * @param mtrCtrl The motor control handle.. + * @retval None. + */ +void HMI_Process_Tx(MTRCTRL_Handle *mtrCtrl) +{ + /* Verify Parameters */ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + UartModuleProcess_Tx(mtrCtrl); +} diff --git a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/hmi_module.h b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/hmi_module.h new file mode 100644 index 00000000..056f8d73 --- /dev/null +++ b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/hmi_module.h @@ -0,0 +1,37 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file hmi_module.h + * @author MCU Algorithm Team + * @brief This file provides functions declaration of system status. + */ + +/* Define to prevent recursive inclusion ------------------------------------------------------- */ +#ifndef McsMagicTag_HMI_MOUDLE_H +#define McsMagicTag_HMI_MOUDLE_H + +/* Includes ------------------------------------------------------------------------------------ */ +#include "uart_module.h" +/* Typedef definitions ------------------------------------------------------------------------- */ + + +void HMI_Init(void); + +void HMI_Process_Rx(MTRCTRL_Handle *mtrCtrl); +void HMI_Process_Tx(MTRCTRL_Handle *mtrCtrl); + +#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/protocol.c b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/protocol.c new file mode 100644 index 00000000..56f0c0bb --- /dev/null +++ b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/protocol.c @@ -0,0 +1,174 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file protocol.c + * @author MCU Algorithm Team + * @brief This file provides functions declaration of port communication. + */ + +#include "protocol.h" +#include "apt.h" +#include "typedefs.h" +#include "main.h" +#include "mcs_assert.h" +#include "cust_process.h" + +/** + * @brief Callback function for receiving data analysis and processing. + * @param rxBuf Receive buffer. + */ +__weak void CUST_UartDataProcess(MTRCTRL_Handle *mtrCtrl, unsigned char *rxBuf) +{ + BASE_FUNC_UNUSED(mtrCtrl); + BASE_FUNC_UNUSED(rxBuf); +} +/** + * @brief User-defined protocol message sending function (weak function). + * @param rxData Sending Messages.. + */ +__weak void CUST_SetTxMsg(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *txData) +{ + BASE_FUNC_UNUSED(mtrCtrl); + BASE_FUNC_UNUSED(*txData); +} + +static void (*g_ptrDataProcess)(MTRCTRL_Handle *mtrCtrl, unsigned char *rxBuf) = CUST_UartDataProcess; + +/** + * @brief Frame checksum. + * @param ptr Pointer to the data to be checked + * @param num Number of bytes + * @retval unsigned char Checksum + */ +static unsigned char CheckSum(unsigned char *ptr, unsigned char num) +{ + unsigned char sum = 0; + unsigned char *p = ptr; + /* Calculate the sum of received data. */ + for (unsigned char i = 0; i < num; i++) { + sum += (unsigned char)*p; + p++; + } + return sum; +} + +/** + * @brief Transmitting Data Frames. + * @param mtrCtrl The motor control handle. + * @param txBuf Sending Messages. + */ +unsigned int CUST_TransmitData(MTRCTRL_Handle *mtrCtrl, unsigned char *txBuf) +{ + /* Verify Parameters */ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + MCS_ASSERT_PARAM(txBuf != NULL); + unsigned int dataLen = FRAME_ONE_DATA_LENTH * SEND_FRAME_DATA_NUM; + unsigned char i = 0; + CUSTDATATYPE_DEF txData = {0}; + CUST_SetTxMsg(mtrCtrl, &txData); + txBuf[i++] = FRAME_START; + /* Message function code */ + txBuf[i++] = FRAME_SENT; + /* Message data */ + for (unsigned char x = 0; x < SEND_FRAME_DATA_NUM; x++) { + unsigned char floatIndex = 0; + unsigned char byteOffset = i; + txBuf[x * FRAME_ONE_DATA_LENTH + byteOffset++] = txData.data[x].typeCh[floatIndex++]; + txBuf[x * FRAME_ONE_DATA_LENTH + byteOffset++] = txData.data[x].typeCh[floatIndex++]; + txBuf[x * FRAME_ONE_DATA_LENTH + byteOffset++] = txData.data[x].typeCh[floatIndex++]; + txBuf[x * FRAME_ONE_DATA_LENTH + byteOffset++] = txData.data[x].typeCh[floatIndex++]; + } + /* Message verification domain */ + txBuf[dataLen + i++] = CheckSum((unsigned char*)&txBuf[FRAME_CHECK_BEGIN], dataLen + 1); + /* End of Message */ + txBuf[dataLen + i++] = FRAME_END; + return dataLen + i; +} + +/** + * @brief Transmitting Data Frames. + * @param txBuf Sending Cust Ack Code. + * @param ackCode Ack Code. + * @param varParams Host set parameter. + */ +void CUST_AckCode(unsigned char *txBuf, unsigned char ackCode, float varParams) +{ + /* Verify Parameters */ + MCS_ASSERT_PARAM(txBuf != NULL); + CUSTDATATYPE_DEF txData = {0}; + int dataIndex = 0; + unsigned int i = 0; + unsigned int txLen = 0; + unsigned char dataLen = FRAME_ONE_CHAR_LENTH + FRAME_ONE_DATA_LENTH; + + txData.data[0].typeF = varParams; + txBuf[i++] = FRAME_START; + /* Message function code */ + txBuf[i++] = FRAME_CUSTACK; + /* Message ack code */ + txBuf[i++] = ackCode; + /* Message data */ + txBuf[i++] = txData.data[0].typeCh[dataIndex++]; + txBuf[i++] = txData.data[0].typeCh[dataIndex++]; + txBuf[i++] = txData.data[0].typeCh[dataIndex++]; + txBuf[i++] = txData.data[0].typeCh[dataIndex++]; + + /* Message verification domain */ + txBuf[FRAME_ONE_CHAR_LENTH + i++] = CheckSum((unsigned char*)&txBuf[FRAME_CHECK_BEGIN], dataLen + 1); + /* End of Message */ + txBuf[FRAME_ONE_CHAR_LENTH + i++] = FRAME_END; + txLen = FRAME_ONE_CHAR_LENTH + i++; + HAL_UART_WriteDMA(&g_uart0, txBuf, txLen); +} + +/** + * @brief Cust receive data process. + * @param mtrCtrl The motor control handle. + * @param rxBuf Receive buffer + */ +void CUST_DataReceProcss(MTRCTRL_Handle *mtrCtrl, unsigned char *rxBuf) +{ + /* Verify Parameters */ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + MCS_ASSERT_PARAM(rxBuf != NULL); + unsigned char g_uartTxBuf[10] = {0}; + unsigned char ackCode = 0; + /* Frame header check */ + if (rxBuf[0] != FRAME_START) { + ackCode = 0X78; + CUST_AckCode(g_uartTxBuf, ackCode, 0); + return; + } + /* Frame trailer check */ + if (rxBuf[FRAME_LENTH - 1] != FRAME_END) { + ackCode = 0X79; + CUST_AckCode(g_uartTxBuf, ackCode, 0); + return; + } + /* Checksum */ + if (CheckSum((unsigned char*)&rxBuf[FRAME_CHECK_BEGIN], FRAME_CHECK_NUM) != rxBuf[FRAME_CHECKSUM]) { + ackCode = 0X7A; + CUST_AckCode(g_uartTxBuf, ackCode, 0); + return; + } else { + if (g_ptrDataProcess == NULL) { + return; + } else { + g_ptrDataProcess(mtrCtrl, rxBuf); + } + } +} diff --git a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/protocol.h b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/protocol.h new file mode 100644 index 00000000..900a00d8 --- /dev/null +++ b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/protocol.h @@ -0,0 +1,121 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file protocol.h + * @author MCU Algorithm Team + * @brief This file provides functions declaration of port communication. + */ +#ifndef McsMagicTag_PORTOCOL_H +#define McsMagicTag_PORTOCOL_H + +#include "uart.h" +#include "mcs_carrier.h" + +#define RS485_SEND_ENABLE GPIO6->GPIO_DATA[GPIO_PIN_7].reg = GPIO_PIN_7 +#define RS485_SEND_DISABLE GPIO6->GPIO_DATA[GPIO_PIN_7].reg = BASE_CFG_UNSET + +#define RX_BUF_LEN (16) +#define SEND_FRAME_DATA_NUM (CUSTDATA_MAX) +/* Service Uart0 Communication Deal */ +#define FRAME_ONE_DATA_LENTH 4 +#define FRAME_ONE_CHAR_LENTH 1 +#define FRAME_RECV_DATA_LENTH 4 +#define FRAME_LENTH 20 /* Data length */ +#define FRAME_SENT 0X8F +#define FRAME_CUSTACK 0X8A +#define FRAME_START 0x0F /* Start frame */ +#define FRAME_END '/' /* StOP frame */ +#define FRAME_CHECK_BEGIN 1 /* Check frame */ +#define FRAME_CHECKSUM 18 /* Check sum */ +#define FRAME_CHECK_NUM 17 +#define CMDCODE_IDLE_FRAME 0x55 /* Fill frame */ +#define CMDCODE_GET_MOTOR_PARAMS 0x01 +#define CMDCODE_SEND_MOTOR_PARAMS 0x02 +#define CMDCODE_SET_MOTOR_CTLMODE 0x03 +#define CMDCODE_SET_OBSERVER_TYPE 0x04 +#define CMDCODE_SET_STARTUP_MODE 0x05 +#define CMDCODE_SET_PID_PARAMS 0x06 +#define CMDCODE_SET_STARTUP_PARAMS 0x07 +#define CMDCODE_SET_OBSERVER_PARAMS 0x08 +#define CMDCODE_SET_MOTOR_TARGETSPD 0x09 +#define CMDCODE_SET_MOTOR_PARAMS 0x0A +#define CMDCODE_MOTOR_START 0x0B +#define CMDCODE_MOTOR_STOP 0x0C +#define CMDCODE_MOTORSTATE_RESET 0x0D +#define CMDCODE_SEND_FIRMVERSION 0x0E +#define CMDCODE_SET_ADJUSTSPD_MODE 0x11 +#define CMDCODE_UART_HANDSHAKE 0x12 +#define CMDCODE_UART_HEARTDETECT 0x13 + +typedef union { + unsigned char typeCh[4]; + float typeF; + int typeI; +} UNIONDATATYPE_DEF; + +typedef enum { + OFFLINE_RES = 0, + OFFLINE_LD, + OFFLINE_LQ, + OFFLINE_PSIF, + OFFLINE_JS, + OFFLINE_NP, + OFFLINE_B, + OFFLINE_KPD, + OFFLINE_KID, + OFFLINE_KPQ, + OFFLINE_KIQ, + OFFLINE_KPS, + OFFLINE_KIS, + OFFLINE_SPEED, + OFLINE_MAX +} OFFLINE_IDEN_TYPE; + +typedef enum { + CURRDQ_Q = 0, + CURRDQ_D, + CURRREFDQ_Q, + CURRREFDQ_D, + CURRSPD, + SPDCMDHZ, + UDC, + POWERBOARDTEMP, + CUST_ERR_CODE, + CURRUVW_U, + CURRUVW_V, + CURRUVW_W, + PWMDUTYUVW_U, + PWMDUTYUVW_V, + PWMDUTYUVW_W, + AXISANGLE, + VDQ_Q, + VDQ_D, + SPDREFHZ, + SENDTIMESTAMP, + CUSTDATA_MAX +} SENDTOHOSTPARAMS; + +typedef struct { + volatile unsigned char code; + volatile UNIONDATATYPE_DEF data[SEND_FRAME_DATA_NUM]; +} CUSTDATATYPE_DEF; + + +void CUST_DataReceProcss(MTRCTRL_Handle *mtrCtrl, unsigned char *rxBuf); +unsigned int CUST_TransmitData(MTRCTRL_Handle *mtrCtrl, unsigned char *txBuf); +void CUST_AckCode(unsigned char *txBuf, unsigned char ackCode, float varParams); +#endif /* McsMagicTag_PORTOCOL_H */ diff --git a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/uart_module.c b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/uart_module.c new file mode 100644 index 00000000..df5e9186 --- /dev/null +++ b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/uart_module.c @@ -0,0 +1,184 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the + * following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file uart_module.c + * @author MCU Algorithm Team + * @brief This file provides functions declaration of Serial port communication. + */ +#include "uart_module.h" +#include "debug.h" +#include "main.h" +#include "baseinc.h" + +/* Buffer size */ +#define UI_TX_BUF_LEN (96) +#define UI_RX_BUF_LEN (96) + +/* Receiving Timeout Interval */ +#define UART_TIME_OUT_MS (100) + +/* Start sending data to host delay after uart connect success */ +#define UART_UPDATA_DELAY_TIME_MS (50) + +/* Data buffer */ +unsigned char g_uartRxBuf[UI_RX_BUF_LEN] = {0}; +unsigned char g_uartTxBuf[UI_TX_BUF_LEN] = {0}; +static unsigned int getdeltaSystickCnt = 0; +static FRAME_Handle g_uartFrame; +/** + * @brief Receive Data Clear. + * @param uartFrame Receice Data. + * @retval None. + */ +static void FrameRecvClear(FRAME_Handle *uartFrame) +{ + /* Clear buffer lenth. */ + uartFrame->buffLen = 0; + uartFrame->timeOutCnt = 0; + uartFrame->frameFlag = 0; + /* Clear received data lenth. */ + uartFrame->rxLen = 0; + /* Clear received flag. */ + uartFrame->rxFlag = 0; + uartFrame->upDataCnt = 0; + uartFrame->rxAckFlag = 0; +} + +/** + * @brief Set Dma status. + * @param mtrCtrl The motor control handle. + * @retval None. + */ +static void SetUartDmaStatus(MTRCTRL_Handle *mtrCtrl) +{ + /* Delay 50ms start uart Tx DMA . */ + if (mtrCtrl->uartConnectFlag == CONNECTING && g_uartFrame.upDataDelayCnt++ > UART_UPDATA_DELAY_TIME_MS) { + g_uartFrame.txFlag = 1; /* Start send data flag. */ + mtrCtrl->uartConnectFlag = CONNECTED; + g_uartFrame.upDataDelayCnt = 0; + } + if (mtrCtrl->uartConnectFlag == DISCONNECT) { + g_uartFrame.txFlag = 0; /* Stop send data flag. */ + mtrCtrl->uartTimeStamp = 0; + } +} + +/** + * @brief Uart Dma interupt callback func. + * @param handle Uart handle. + * @retval None. + */ +void UART0_TXDMACallback(void *handle) +{ + BASE_FUNC_UNUSED(handle); + unsigned int getCurSystickCnt = 0; + static unsigned int getlastSystickCnt = 0; + /* USER CODE BEGIN UART0_WRITE_DMA_FINISH */ + g_uartFrame.txFlag = 1; + /* Received information answered, information update reserved. */ + if (g_uartFrame.rxAckFlag == 1) { + g_uartFrame.uartItTxFlag = 1; + g_uartFrame.rxAckFlag = 0; + } + getCurSystickCnt = DCL_SYSTICK_GetTick(); + if (getlastSystickCnt != 0) { + /* Calculate unit frame data send time */ + getdeltaSystickCnt = getCurSystickCnt - getlastSystickCnt; + } + getlastSystickCnt = getCurSystickCnt; + /* USER CODE END UART0_WRITE_DMA_FINISH */ +} + +/** + * @brief Uart0 Interruput Read CallBack Function. + * @param handle Uart handle. + * @retval None. + */ +void UART0ReadInterruptCallback(void *handle) +{ + BASE_FUNC_UNUSED(handle); + /* USER CODE BEGIN UART0_READ_IT_FINISH */ + if (g_uartFrame.rxLen >= UI_RX_BUF_LEN - 1) { + g_uartFrame.rxLen = 0; + } + HAL_UART_ReadIT(handle, &g_uartFrame.rxData, 1); + g_uartRxBuf[g_uartFrame.rxLen] = g_uartFrame.rxData; + g_uartFrame.rxLen++; + g_uartFrame.rxFlag = 1; + g_uartFrame.uartItTxFlag = 0; + return; + /* USER CODE END UART0_READ_IT_FINISH */ +} + +/** + * @brief Uart Read Data Init Function. + * @param void. + * @retval None. + */ +void UartRecvInit(void) +{ + /* Uart reception initialization */ + FrameRecvClear(&g_uartFrame); + HAL_UART_ReadIT(&g_uart0, &g_uartFrame.rxData, 1); +} + +/** + * @brief Uart Read Data Process Function. + * @param mtrCtrl The motor control handle. + * @retval None. + */ +void UartModuleProcess_Rx(MTRCTRL_Handle *mtrCtrl) +{ + /* Verify Parameters */ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + SetUartDmaStatus(mtrCtrl); + if (g_uartFrame.rxFlag == 1) { /* Receive data flag. */ + if (g_uartFrame.timeOutCnt++ > UART_TIME_OUT_MS) { + /* Received data from the host. */ + g_uartFrame.frameFlag = 1; + g_uartFrame.rxFlag = 0; + g_uartFrame.timeOutCnt = 0; + /* Execute data process. */ + CUST_DataReceProcss(mtrCtrl, g_uartRxBuf); + g_uartFrame.rxAckFlag = 1; + g_uartFrame.rxLen = 0; + } + } + g_uartFrame.frameFlag = 0; +} + +/** + * @brief Uart Write Data Process Function. + * @param mtrCtrl The motor control handle. + * @retval None. + */ +void UartModuleProcess_Tx(MTRCTRL_Handle *mtrCtrl) +{ + /* Verify Parameters */ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + if (g_uartFrame.txFlag == 1) { /* Send data flag. */ + mtrCtrl->uartTimeStamp = (float)getdeltaSystickCnt; /* Unit data time stamp */ + g_uartFrame.upDataCnt = 0; + g_uartFrame.txFlag = 0; + /* Send data to host. */ + unsigned int txLen = CUST_TransmitData(mtrCtrl, g_uartTxBuf); + /* If txIT mode send data finish, convert to DMA mode */ + if (g_uartFrame.uartItTxFlag == 1) { + HAL_UART_WriteDMA(&g_uart0, g_uartTxBuf, txLen); + } + } +} diff --git a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/uart_module.h b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/uart_module.h new file mode 100644 index 00000000..f1f8f899 --- /dev/null +++ b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/uart_module.h @@ -0,0 +1,47 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file uart_module.h + * @author MCU Algorithm Team + * @brief This file provides functions declaration of Serial port communication. + */ +#ifndef McsMagicTag_UART_MODULE_H +#define McsMagicTag_UART_MODULE_H + +#include "protocol.h" +#include "mcs_ctlmode_config.h" + +typedef struct { + unsigned int buffLen; + unsigned int timeOutCnt; + unsigned char frameFlag; + unsigned int rxLen; + unsigned char rxFlag; + unsigned char txFlag; + unsigned char rxData; + unsigned int upDataCnt; + unsigned int upDataDelayCnt; + unsigned char uartItTxFlag; + unsigned char rxAckFlag; +} FRAME_Handle; + + +void UartRecvInit(void); +void UartModuleProcess_Rx(MTRCTRL_Handle *mtrCtrl); +void UartModuleProcess_Tx(MTRCTRL_Handle *mtrCtrl); + +#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/readme.md b/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/readme.md new file mode 100644 index 00000000..0414d5ef --- /dev/null +++ b/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/readme.md @@ -0,0 +1,20 @@ +# E7050_Six_Step_Wave + +**【功能描述】** + +- 基于ECMCU105H/ECBMCU201MPC单板的反电动势采样的无感电机六步方波调速系统应用 + +**【环境要求】** + +- 所有单板电源改制为演示用的24V低压,电机选用E7050 +- 电机(黄绿蓝)相线分别对应功率板上U/V/W通道接口 + +**【配置方法】** + +- 将user文件夹替换原工程user文件夹即可 + +【操作说明】 + +- 点击开发板按键启动电机 + +- 开发板蓝色电位器,可以调节电机转动的快慢。(15-58.8HZ) \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/generatecode/feature.h b/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/generatecode/feature.h new file mode 100644 index 00000000..13108bb3 --- /dev/null +++ b/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/generatecode/feature.h @@ -0,0 +1,94 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file feature.h + * @author MCU Driver Team + * @brief This file contains macro configurations related to the project. This file is generated by the IDE tool. + * @date 2025-04-23 14:18:03 + */ + +#ifndef McuMagicTag_FEATURE_H +#define McuMagicTag_FEATURE_H + +/* Macro definitions --------------------------------------------------------- */ +#define CHIP_3061MNPICA MACRO_ENABLE + +#define MACRO_ENABLE 1 +#define MACRO_DISABLE 0 + +/* Macro switch */ +#define BASE_DEFINE_USE_ASSERT MACRO_ENABLE +#ifndef FLASH_CRC_CONFIG +#define FLASH_CRC_CONFIG +#endif /* #ifndef FLASH_CRC_CONFIG */ +#define BASE_MATH_SINCOS_MIDDLE_TABLE MACRO_ENABLE /**< This macro is used to control the table type when the + BASE_MATH_GetSinCos() queries the table. When the value of + this macro is MACRO_ENABLE, the error value obtained by the + BASE_MATH_GetSinCos() is relatively small, and the return + value of the function may be greater than or less than the + actual value. When the value of this macro is MACRO_DISABLE, + the error value obtained by the BASE_MATH_GetSinCos() is + relatively large. However, in the range [0°, 180°) and + [180°, 360°), the return value of the function is either + greater than or less than the actual value. */ + +/* Peripheral module macro switch--------------------------------------------- */ +#define BOARD_DIM_NUM 1 /**< Number of dimming handle arrays. */ + +#define BOARD_KEY_NUM 10 /**< Number of key handle arrays. */ +#define BOARD_KEY_PRESS_ON GPIO_HIGH_LEVEL /**< GPIO status corresponding to long press valid. */ +#define BOARD_KEY_PRESS_OFF GPIO_LOW_LEVEL /**< GPIO status corresponding to short press valid. */ + +#define BOARD_LED_SEG_NUM 4 /**< Number of segments. */ +#define BOARD_LED_SEGMENT_ON GPIO_HIGH_LEVEL /**< GPIO level status corresponding to valid segments. */ +#define BOARD_LED_SEGMENT_OFF GPIO_LOW_LEVEL /**< GPIO level status corresponding to invalid segments. */ + +#define BOARD_MKEY_SCHEME_NUMBER BOARD_MKEY_SCHEME_NUMBER_ONE /**< Define the scheme to be adopted. */ +#define BOARD_MKEY_OUT_NUM 4 /**< Number of GPIO pins used as output during scanning. */ +#define BOARD_MKEY_IN_NUM 4 /**< Number of GPIO pins used as input during scanning. */ +#define BOARD_MKEY_OUT_PIN_VALID GPIO_LOW_LEVEL /**< GPIO level status corresponding to the valid \ + status of the output GPIO in the key matrix. */ +#define BOARD_MKEY_OUT_PIN_INVALID GPIO_HIGH_LEVEL /**< GPIO level status corresponding to the \ + invalid status of the output GPIO in the key matrix. */ +#define BOARD_MKEY_IN_PIN_VALID GPIO_LOW_LEVEL /**< Indicates the GPIO level corresponding to the \ + valid status of the input GPIO in the key matrix. */ +#define BOARD_MKEY_IN_PIN_INVALID GPIO_HIGH_LEVEL /**< Indicates the GPIO level corresponding to the \ + invalid status of the input GPIO in the key matrix. */ + +#define BOARD_PULSES_NUM 2 /**< Number of pulse handles. */ + +#define BASE_DEFINE_SLIPAVERAGE_NUM 2 /**< Sliding average array length. */ + +#define LISTNODE_MAX 20 + +#define BASE_DEFINE_DMA_QUICKSTART + +#define XTRAIL_FREQ 30000000U + +#define DBG_USE_NO_PRINTF 0U +#define DBG_USE_UART_PRINTF 1U + +#define DBG_PRINTF_USE DBG_USE_UART_PRINTF +#if (DBG_PRINTF_USE == DBG_USE_UART_PRINTF) +#define DBG_PRINTF_UART_PORT UART0 +#endif + +/* USER CODE BEGIN 0 */ +/* USER CODE 区域内代码不会被覆盖,区域外会被生成的默认代码覆盖(其余USER CODE 区域同理) */ +/* USER CODE END 0 */ + +#endif /* McuMagicTag_FEATURE_H */ \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/sample_gpio_key/generatecode/main.h b/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/generatecode/main.h similarity index 78% rename from vendor/yibaina_3061M/demo/sample_gpio_key/generatecode/main.h rename to vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/generatecode/main.h index c77e0f2e..55b93f82 100644 --- a/vendor/yibaina_3061M/demo/sample_gpio_key/generatecode/main.h +++ b/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/generatecode/main.h @@ -18,15 +18,23 @@ * @file main.h * @author MCU Driver Team * @brief This file contains driver init functions. + * @date 2025-04-23 14:18:03 */ /* Define to prevent recursive inclusion ------------------------------------- */ #ifndef McuMagicTag_SYSTEM_INIT_H #define McuMagicTag_SYSTEM_INIT_H +#include "adc.h" +#include "adc_ex.h" +#include "acmp.h" +#include "acmp_ex.h" +#include "apt.h" #include "uart.h" #include "uart_ex.h" #include "gpio.h" +#include "timer.h" +#include "timer_ex.h" #include "crg.h" #include "iocmg.h" @@ -43,14 +51,31 @@ #define XTAL_DRV_LEVEL2 0x01U #define XTAL_DRV_LEVEL1 0x00U +#define SYS_LED_PIN GPIO_PIN_3 +#define SYS_LED_HANDLE g_gpio2 +#define GpioStartStop_PIN GPIO_PIN_4 +#define GpioStartStop_HANDLE g_gpio2 + +extern ACMP_Handle g_acmp0; +extern TIMER_Handle g_timer1; extern UART_Handle g_uart0; +extern APT_Handle g_apt0; +extern APT_Handle g_apt1; +extern APT_Handle g_apt2; +extern ADC_Handle g_adc0; +extern GPIO_Handle g_gpio1; extern GPIO_Handle g_gpio2; BASE_StatusType CRG_Config(CRG_CoreClkSelect *coreClkSelect); void SystemInit(void); -void GPIO_CallBackFunc(void *param); +void MotorStatemachineCallBack(void *handle); +void TIMER1_DMAOverFlow_InterruptProcess(void *handle); +void MotorCarrierProcessCallback(void *aptHandle); +void MotorSysErrCallback(void *aptHandle); + +void MotorStartStopKeyCallback(void *param); /* USER CODE BEGIN 0 */ /* USER CODE 区域内代码不会被覆盖,区域外会被生成的默认代码覆盖(其余USER CODE 区域同理) */ diff --git a/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/generatecode/system_init.c b/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/generatecode/system_init.c new file mode 100644 index 00000000..76ec601a --- /dev/null +++ b/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/generatecode/system_init.c @@ -0,0 +1,530 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file system_init.c + * @author MCU Driver Team + * @brief This file contains driver init functions. + * @date 2025-04-23 14:18:03 + */ + +#include "main.h" +#include "ioconfig.h" +#include "iocmg_ip.h" + +#define UART0_BAUD_RATE 115200 + +BASE_StatusType CRG_Config(CRG_CoreClkSelect *coreClkSelect) +{ + CRG_Handle crg; + crg.baseAddress = CRG; + crg.pllRefClkSelect = CRG_PLL_REF_CLK_SELECT_HOSC; + crg.pllPreDiv = CRG_PLL_PREDIV_4; + crg.pllFbDiv = 48; /* PLL Multiplier 48 */ + crg.pllPostDiv = CRG_PLL_POSTDIV_2; + crg.coreClkSelect = CRG_CORE_CLK_SELECT_PLL; + crg.handleEx.pllPostDiv2 = CRG_PLL_POSTDIV2_3; + crg.handleEx.clk1MSelect = CRG_1M_CLK_SELECT_HOSC; + crg.handleEx.clk1MDiv = (25 - 1); /* The 1 MHz freq is equal to the input clock frequency / (clk_1m_div + 1). 25 is the div of the clk_1m in CLOCK. */ + + if (HAL_CRG_Init(&crg) != BASE_STATUS_OK) { + return BASE_STATUS_ERROR; + } + *coreClkSelect = crg.coreClkSelect; + return BASE_STATUS_OK; +} + +static void ACMP0_Init(void) +{ + HAL_CRG_IpEnableSet(ACMP0_BASE, IP_CLK_ENABLE); /* ACMP clock bit reset. */ + g_acmp0.baseAddress = ACMP0_BASE; + g_acmp0.inOutConfig.inputNNum = ACMP_INPUT_N_SELECT2; + g_acmp0.inOutConfig.inputPNum = ACMP_INPUT_P_SELECT2; + g_acmp0.inOutConfig.polarity = ACMP_OUT_NOT_INVERT; + g_acmp0.filterCtrl.filterMode = ACMP_FILTER_NONE; + g_acmp0.hysteresisVol = ACMP_HYS_VOL_30MV; + g_acmp0.interruptEn = BASE_CFG_UNSET; + HAL_ACMP_Init(&g_acmp0); +} + +static void ADC0_Init(void) +{ + HAL_CRG_IpEnableSet(ADC0_BASE, IP_CLK_ENABLE); + HAL_CRG_IpClkSelectSet(ADC0_BASE, CRG_ADC_CLK_ASYN_PLL_DIV); + HAL_CRG_IpClkDivSet(ADC0_BASE, CRG_ADC_DIV_1); + + g_adc0.baseAddress = ADC0; + g_adc0.socPriority = ADC_PRIMODE_ALL_ROUND; + + HAL_ADC_Init(&g_adc0); + + SOC_Param socParam = {0}; + socParam.adcInput = ADC_CH_ADCINA11; /* PIN9(ADC AIN11) */ + socParam.sampleTotalTime = ADC_SOCSAMPLE_5CLK; /* adc sample total time 5 adc_clk */ + socParam.trigSource = ADC_TRIGSOC_APT0_SOCA; + socParam.continueMode = BASE_CFG_DISABLE; + socParam.finishMode = ADC_SOCFINISH_NONE; + HAL_ADC_ConfigureSoc(&g_adc0, ADC_SOC_NUM5, &socParam); + + socParam.adcInput = ADC_CH_ADCINA12; /* PIN10(ADC AIN12) */ + socParam.sampleTotalTime = ADC_SOCSAMPLE_5CLK; /* adc sample total time 5 adc_clk */ + socParam.trigSource = ADC_TRIGSOC_APT0_SOCA; + socParam.continueMode = BASE_CFG_DISABLE; + socParam.finishMode = ADC_SOCFINISH_NONE; + HAL_ADC_ConfigureSoc(&g_adc0, ADC_SOC_NUM2, &socParam); + + socParam.adcInput = ADC_CH_ADCINA10; /* PIN8(ADC AIN10) */ + socParam.sampleTotalTime = ADC_SOCSAMPLE_5CLK; /* adc sample total time 5 adc_clk */ + socParam.trigSource = ADC_TRIGSOC_APT0_SOCA; + socParam.continueMode = BASE_CFG_DISABLE; + socParam.finishMode = ADC_SOCFINISH_NONE; + HAL_ADC_ConfigureSoc(&g_adc0, ADC_SOC_NUM6, &socParam); + + socParam.adcInput = ADC_CH_ADCINA9; /* PIN7(ADC AIN9) */ + socParam.sampleTotalTime = ADC_SOCSAMPLE_5CLK; /* adc sample total time 5 adc_clk */ + socParam.trigSource = ADC_TRIGSOC_SOFT; + socParam.continueMode = BASE_CFG_DISABLE; + socParam.finishMode = ADC_SOCFINISH_NONE; + HAL_ADC_ConfigureSoc(&g_adc0, ADC_SOC_NUM9, &socParam); +} + +__weak void MotorSysErrCallback(void *aptHandle) +{ + BASE_FUNC_UNUSED(aptHandle); + /* USER CODE BEGIN APT0_EVENT_INTERRUPT */ + /* USER CODE END APT0_EVENT_INTERRUPT */ +} + +__weak void MotorCarrierProcessCallback(void *aptHandle) +{ + BASE_FUNC_UNUSED(aptHandle); + /* USER CODE BEGIN APT0_TIMER_INTERRUPT */ + /* USER CODE END APT0_TIMER_INTERRUPT */ +} + +static void APT0_ProtectInit(void) +{ + APT_OutCtrlProtectEx protectApt = {0}; + protectApt.ocEventEnEx = BASE_CFG_ENABLE; + protectApt.ocEventModeEx = APT_OUT_CTRL_ONE_SHOT; + protectApt.ocActionEx = APT_OUT_CTRL_ACTION_LOW; + protectApt.ocActionBEx = APT_OUT_CTRL_ACTION_LOW; + protectApt.ocEvtInterruptEnEx = BASE_CFG_ENABLE; + protectApt.ocSysEvent = APT_SYS_EVT_DEBUG | APT_SYS_EVT_CLK | APT_SYS_EVT_MEM; + protectApt.originalEvtEx = APT_EM_ORIGINAL_SRC_ACMP0; + protectApt.evtPolarityMaskEx = APT_EM_ACMP0_INVERT_BIT; + protectApt.filterCycleNumEx = 0; + HAL_APT_ProtectInitEx(&g_apt0, &protectApt); +} + +static void APT0_Init(void) +{ + HAL_CRG_IpEnableSet(APT0_BASE, IP_CLK_ENABLE); + + g_apt0.baseAddress = APT0; + + /* Clock Settings */ + g_apt0.waveform.dividerFactor = 1 - 1; + /* Timer Settings */ + g_apt0.waveform.timerPeriod = 6250; /* 6250 is count period of APT time-base timer */ + g_apt0.waveform.cntMode = APT_COUNT_MODE_UP_DOWN; + + /* Wave Form */ + g_apt0.waveform.basicType = APT_PWM_BASIC_A_HIGH_B_LOW; + g_apt0.waveform.chAOutType = APT_PWM_OUT_BASIC_TYPE; + g_apt0.waveform.chBOutType = APT_PWM_OUT_BASIC_TYPE; + g_apt0.waveform.divInitVal = 0; + g_apt0.waveform.cntInitVal = 0; + g_apt0.waveform.cntCmpLeftEdge = 3125; /* 3125 is count compare point of the left edge of PWM waveform */ + g_apt0.waveform.cntCmpRightEdge = 3125; /* 3125 is count compare point of the right edge of PWM waveform */ + g_apt0.waveform.cntCmpLoadMode = APT_BUFFER_INDEPENDENT_LOAD; + g_apt0.waveform.cntCmpLoadEvt = APT_COMPARE_LOAD_EVENT_ZERO; + g_apt0.waveform.deadBandCnt = 150; /* 150 is dead-band value */ + + /* ADC Trigger SOCA */ + g_apt0.adcTrg.trgEnSOCA = BASE_CFG_ENABLE; + g_apt0.adcTrg.cntCmpSOCA = 6000; /* 6000 is count compare point of ADC trigger source SOCA when using CMPA */ + g_apt0.adcTrg.trgSrcSOCA = APT_CS_SRC_CNTR_CMPA_DOWN; + g_apt0.adcTrg.trgScaleSOCA = 1; + + /* ADC Trigger SOCB */ + g_apt0.adcTrg.trgEnSOCB = BASE_CFG_ENABLE; + g_apt0.adcTrg.cntCmpSOCB = 1; + g_apt0.adcTrg.trgSrcSOCB = APT_CS_SRC_CNTR_CMPA_DOWN; + g_apt0.adcTrg.trgScaleSOCB = 1; + + g_apt0.adcTrg.cntCmpLoadMode = APT_BUFFER_INDEPENDENT_LOAD; + g_apt0.adcTrg.cntCmpLoadEvt = APT_COMPARE_LOAD_EVENT_ZERO; + + /* Timer Trigger */ + g_apt0.tmrInterrupt.tmrInterruptEn = BASE_CFG_ENABLE; + g_apt0.tmrInterrupt.tmrInterruptSrc = APT_INT_SRC_CNTR_CMPC_UP; + g_apt0.tmrInterrupt.tmrInterruptScale = 1; + + APT0_ProtectInit(); + + HAL_APT_PWMInit(&g_apt0); + HAL_APT_RegisterCallBack(&g_apt0, APT_EVENT_INTERRUPT, MotorSysErrCallback); + IRQ_SetPriority(IRQ_APT0_EVT, 7); /* 7 is priority value */ + IRQ_Register(IRQ_APT0_EVT, HAL_APT_EventIrqHandler, &g_apt0); + IRQ_EnableN(IRQ_APT0_EVT); + HAL_APT_RegisterCallBack(&g_apt0, APT_TIMER_INTERRUPT, MotorCarrierProcessCallback); + IRQ_SetPriority(IRQ_APT0_TMR, 6); /* 6 is priority value */ + IRQ_Register(IRQ_APT0_TMR, HAL_APT_TimerIrqHandler, &g_apt0); + IRQ_EnableN(IRQ_APT0_TMR); +} + +static void APT1_ProtectInit(void) +{ + APT_OutCtrlProtectEx protectApt = {0}; + protectApt.ocEventEnEx = BASE_CFG_ENABLE; + protectApt.ocEventModeEx = APT_OUT_CTRL_ONE_SHOT; + protectApt.ocActionEx = APT_OUT_CTRL_ACTION_LOW; + protectApt.ocActionBEx = APT_OUT_CTRL_ACTION_LOW; + protectApt.ocEvtInterruptEnEx = BASE_CFG_DISABLE; + protectApt.ocSysEvent = APT_SYS_EVT_DEBUG | APT_SYS_EVT_CLK | APT_SYS_EVT_MEM; + protectApt.originalEvtEx = APT_EM_ORIGINAL_SRC_ACMP0; + protectApt.evtPolarityMaskEx = APT_EM_ACMP0_INVERT_BIT; + protectApt.filterCycleNumEx = 0; + HAL_APT_ProtectInitEx(&g_apt1, &protectApt); +} + +static void APT1_Init(void) +{ + HAL_CRG_IpEnableSet(APT1_BASE, IP_CLK_ENABLE); + + g_apt1.baseAddress = APT1; + + /* Clock Settings */ + g_apt1.waveform.dividerFactor = 1 - 1; + /* Timer Settings */ + g_apt1.waveform.timerPeriod = 6250; /* 6250 is count period of APT time-base timer */ + g_apt1.waveform.cntMode = APT_COUNT_MODE_UP_DOWN; + + /* Wave Form */ + g_apt1.waveform.basicType = APT_PWM_BASIC_A_HIGH_B_LOW; + g_apt1.waveform.chAOutType = APT_PWM_OUT_BASIC_TYPE; + g_apt1.waveform.chBOutType = APT_PWM_OUT_BASIC_TYPE; + g_apt1.waveform.divInitVal = 0; + g_apt1.waveform.cntInitVal = 0; + g_apt1.waveform.cntCmpLeftEdge = 3125; /* 3125 is count compare point of the left edge of PWM waveform */ + g_apt1.waveform.cntCmpRightEdge = 3125; /* 3125 is count compare point of the right edge of PWM waveform */ + g_apt1.waveform.cntCmpLoadMode = APT_BUFFER_INDEPENDENT_LOAD; + g_apt1.waveform.cntCmpLoadEvt = APT_COMPARE_LOAD_EVENT_ZERO; + g_apt1.waveform.deadBandCnt = 150; /* 150 is dead-band value */ + + /* ADC Trigger SOCA */ + g_apt1.adcTrg.trgEnSOCA = BASE_CFG_ENABLE; + g_apt1.adcTrg.cntCmpSOCA = 1; /* 1 is count compare point of ADC trigger source SOCA when using CMPA */ + g_apt1.adcTrg.trgSrcSOCA = APT_CS_SRC_CNTR_ZERO; + g_apt1.adcTrg.trgScaleSOCA = 1; + + g_apt1.adcTrg.cntCmpLoadMode = APT_BUFFER_INDEPENDENT_LOAD; + g_apt1.adcTrg.cntCmpLoadEvt = APT_COMPARE_LOAD_EVENT_ZERO; + + APT1_ProtectInit(); + + HAL_APT_PWMInit(&g_apt1); +} + +static void APT2_ProtectInit(void) +{ + APT_OutCtrlProtectEx protectApt = {0}; + protectApt.ocEventEnEx = BASE_CFG_ENABLE; + protectApt.ocEventModeEx = APT_OUT_CTRL_ONE_SHOT; + protectApt.ocActionEx = APT_OUT_CTRL_ACTION_LOW; + protectApt.ocActionBEx = APT_OUT_CTRL_ACTION_LOW; + protectApt.ocEvtInterruptEnEx = BASE_CFG_DISABLE; + protectApt.ocSysEvent = APT_SYS_EVT_DEBUG | APT_SYS_EVT_CLK | APT_SYS_EVT_MEM; + protectApt.originalEvtEx = APT_EM_ORIGINAL_SRC_ACMP0; + protectApt.evtPolarityMaskEx = APT_EM_ACMP0_INVERT_BIT; + protectApt.filterCycleNumEx = 0; + HAL_APT_ProtectInitEx(&g_apt2, &protectApt); +} + +static void APT2_Init(void) +{ + HAL_CRG_IpEnableSet(APT2_BASE, IP_CLK_ENABLE); + + g_apt2.baseAddress = APT2; + + /* Clock Settings */ + g_apt2.waveform.dividerFactor = 1 - 1; + /* Timer Settings */ + g_apt2.waveform.timerPeriod = 6250; /* 6250 is count period of APT time-base timer */ + g_apt2.waveform.cntMode = APT_COUNT_MODE_UP_DOWN; + + /* Wave Form */ + g_apt2.waveform.basicType = APT_PWM_BASIC_A_HIGH_B_LOW; + g_apt2.waveform.chAOutType = APT_PWM_OUT_BASIC_TYPE; + g_apt2.waveform.chBOutType = APT_PWM_OUT_BASIC_TYPE; + g_apt2.waveform.divInitVal = 0; + g_apt2.waveform.cntInitVal = 0; + g_apt2.waveform.cntCmpLeftEdge = 3125; /* 3125 is count compare point of the left edge of PWM waveform */ + g_apt2.waveform.cntCmpRightEdge = 3125; /* 3125 is count compare point of the right edge of PWM waveform */ + g_apt2.waveform.cntCmpLoadMode = APT_BUFFER_INDEPENDENT_LOAD; + g_apt2.waveform.cntCmpLoadEvt = APT_COMPARE_LOAD_EVENT_ZERO; + g_apt2.waveform.deadBandCnt = 150; /* 150 is dead-band value */ + + APT2_ProtectInit(); + + HAL_APT_PWMInit(&g_apt2); +} + +__weak void MotorStartStopKeyCallback(void *param) +{ + GPIO_Handle *handle = (GPIO_Handle *)param; + BASE_FUNC_UNUSED(handle); +} + +static void GPIO_Init(void) +{ + HAL_CRG_IpEnableSet(GPIO1_BASE, IP_CLK_ENABLE); + g_gpio1.baseAddress = GPIO1; + + g_gpio1.pins = GPIO_PIN_0; + HAL_GPIO_Init(&g_gpio1); + HAL_GPIO_SetDirection(&g_gpio1, g_gpio1.pins, GPIO_OUTPUT_MODE); + HAL_GPIO_SetValue(&g_gpio1, g_gpio1.pins, GPIO_HIGH_LEVEL); + HAL_GPIO_SetIrqType(&g_gpio1, g_gpio1.pins, GPIO_INT_TYPE_NONE); + + HAL_CRG_IpEnableSet(GPIO2_BASE, IP_CLK_ENABLE); + g_gpio2.baseAddress = GPIO2; + + g_gpio2.pins = GPIO_PIN_3; + HAL_GPIO_Init(&g_gpio2); + HAL_GPIO_SetDirection(&g_gpio2, g_gpio2.pins, GPIO_OUTPUT_MODE); + HAL_GPIO_SetValue(&g_gpio2, g_gpio2.pins, GPIO_HIGH_LEVEL); + HAL_GPIO_SetIrqType(&g_gpio2, g_gpio2.pins, GPIO_INT_TYPE_NONE); + + g_gpio2.pins = GPIO_PIN_4; + HAL_GPIO_Init(&g_gpio2); + HAL_GPIO_SetDirection(&g_gpio2, g_gpio2.pins, GPIO_INPUT_MODE); + HAL_GPIO_SetValue(&g_gpio2, g_gpio2.pins, GPIO_LOW_LEVEL); + HAL_GPIO_SetIrqType(&g_gpio2, g_gpio2.pins, GPIO_INT_TYPE_FALL_EDGE); + + HAL_GPIO_RegisterCallBack(&g_gpio2, GPIO_PIN_4, MotorStartStopKeyCallback); + IRQ_Register(IRQ_GPIO2, HAL_GPIO_IrqHandler, &g_gpio2); + IRQ_SetPriority(IRQ_GPIO2, 1); /* set gpio1 interrupt priority to 1, 1~15. 1 is priority value */ + IRQ_EnableN(IRQ_GPIO2); /* gpio interrupt enable */ + + return; +} + +__weak void MotorStatemachineCallBack(void *handle) +{ + BASE_FUNC_UNUSED(handle); + /* USER CODE BEGIN MotorStatemachineCallBack */ + /* USER CODE END MotorStatemachineCallBack */ +} + +static void TIMER1_Init(void) +{ + HAL_CRG_IpEnableSet(TIMER1_BASE, IP_CLK_ENABLE); /* TIMER1 clock enable. */ + unsigned int load = ((float)HAL_CRG_GetIpFreq((void *)TIMER1) / (1u << (TIMERPRESCALER_NO_DIV * 4)) / 1000000u) * 500; + + g_timer1.baseAddress = TIMER1; + g_timer1.load = (load < 1) ? 0 : (load - 1); /* Set timer value immediately */ + g_timer1.bgLoad = (load < 1) ? 0 : (load - 1); /* Set timer value */ + g_timer1.mode = TIMER_MODE_RUN_PERIODIC; /* Run in period mode */ + g_timer1.prescaler = TIMERPRESCALER_NO_DIV; /* Don't frequency division */ + g_timer1.size = TIMER_SIZE_32BIT; /* 1 for 32bit, 0 for 16bit */ + g_timer1.interruptEn = BASE_CFG_ENABLE; + g_timer1.adcSocReqEnable = BASE_CFG_DISABLE; + g_timer1.dmaReqEnable = BASE_CFG_DISABLE; + HAL_TIMER_Init(&g_timer1); + IRQ_Register(IRQ_TIMER1, HAL_TIMER_IrqHandler, &g_timer1); + + HAL_TIMER_RegisterCallback(&g_timer1, TIMER_PERIOD_FIN, MotorStatemachineCallBack); + IRQ_SetPriority(IRQ_TIMER1, 2); /* 2 is priority value */ + IRQ_EnableN(IRQ_TIMER1); +} + +static void UART0_Init(void) +{ + HAL_CRG_IpEnableSet(UART0_BASE, IP_CLK_ENABLE); /* UART0 clock enable. */ + g_uart0.baseAddress = UART0; + + g_uart0.baudRate = UART0_BAUD_RATE; + g_uart0.dataLength = UART_DATALENGTH_8BIT; + g_uart0.stopBits = UART_STOPBITS_ONE; + g_uart0.parity = UART_PARITY_NONE; + g_uart0.txMode = UART_MODE_BLOCKING; + g_uart0.rxMode = UART_MODE_BLOCKING; + g_uart0.fifoMode = BASE_CFG_ENABLE; + g_uart0.fifoTxThr = UART_FIFODEPTH_SIZE8; + g_uart0.fifoRxThr = UART_FIFODEPTH_SIZE8; + g_uart0.hwFlowCtr = BASE_CFG_DISABLE; + g_uart0.handleEx.overSampleMultiple = UART_OVERSAMPLING_16X; + g_uart0.handleEx.msbFirst = BASE_CFG_DISABLE; + HAL_UART_Init(&g_uart0); +} + +static void IOConfig(void) +{ + SYSCTRL0->SC_SYS_STAT.BIT.update_mode = 0; + SYSCTRL0->SC_SYS_STAT.BIT.update_mode_clear = 1; + /* Config PIN9 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO3_5_AS_ADC_AIN11); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO3_5_AS_ADC_AIN11, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO3_5_AS_ADC_AIN11, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO3_5_AS_ADC_AIN11, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO3_5_AS_ADC_AIN11, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN10 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO5_3_AS_ADC_AIN12); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO5_3_AS_ADC_AIN12, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO5_3_AS_ADC_AIN12, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO5_3_AS_ADC_AIN12, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO5_3_AS_ADC_AIN12, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN8 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO3_6_AS_ADC_AIN10); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO3_6_AS_ADC_AIN10, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO3_6_AS_ADC_AIN10, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO3_6_AS_ADC_AIN10, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO3_6_AS_ADC_AIN10, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN7 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO3_7_AS_ADC_AIN9); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO3_7_AS_ADC_AIN9, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO3_7_AS_ADC_AIN9, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO3_7_AS_ADC_AIN9, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO3_7_AS_ADC_AIN9, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN48 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO0_6_AS_ACMP_N2); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO0_6_AS_ACMP_N2, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO0_6_AS_ACMP_N2, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO0_6_AS_ACMP_N2, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO0_6_AS_ACMP_N2, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN47 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO0_5_AS_ACMP_P2); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO0_5_AS_ACMP_P2, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO0_5_AS_ACMP_P2, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO0_5_AS_ACMP_P2, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO0_5_AS_ACMP_P2, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN1 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO0_7_AS_ACMP0_OUT); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO0_7_AS_ACMP0_OUT, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO0_7_AS_ACMP0_OUT, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO0_7_AS_ACMP0_OUT, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO0_7_AS_ACMP0_OUT, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN27 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO1_0_AS_GPIO1_0); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO1_0_AS_GPIO1_0, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO1_0_AS_GPIO1_0, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO1_0_AS_GPIO1_0, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO1_0_AS_GPIO1_0, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN35 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO2_3_AS_GPIO2_3); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO2_3_AS_GPIO2_3, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO2_3_AS_GPIO2_3, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO2_3_AS_GPIO2_3, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO2_3_AS_GPIO2_3, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN41 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO2_4_AS_GPIO2_4); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO2_4_AS_GPIO2_4, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO2_4_AS_GPIO2_4, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO2_4_AS_GPIO2_4, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO2_4_AS_GPIO2_4, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN39 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO0_3_AS_UART0_TXD); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO0_3_AS_UART0_TXD, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO0_3_AS_UART0_TXD, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO0_3_AS_UART0_TXD, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO0_3_AS_UART0_TXD, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN40 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO0_4_AS_UART0_RXD); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO0_4_AS_UART0_RXD, PULL_UP); /* Pull-up and Pull-down, UART RX recommend PULL_UP */ + HAL_IOCMG_SetPinSchmidtMode(GPIO0_4_AS_UART0_RXD, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO0_4_AS_UART0_RXD, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO0_4_AS_UART0_RXD, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN19 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO3_0_AS_APT0_PWMA); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO3_0_AS_APT0_PWMA, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO3_0_AS_APT0_PWMA, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO3_0_AS_APT0_PWMA, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO3_0_AS_APT0_PWMA, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN23 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO4_0_AS_APT0_PWMB); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO4_0_AS_APT0_PWMB, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO4_0_AS_APT0_PWMB, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO4_0_AS_APT0_PWMB, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO4_0_AS_APT0_PWMB, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN20 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO3_1_AS_APT1_PWMA); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO3_1_AS_APT1_PWMA, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO3_1_AS_APT1_PWMA, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO3_1_AS_APT1_PWMA, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO3_1_AS_APT1_PWMA, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN24 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO4_1_AS_APT1_PWMB); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO4_1_AS_APT1_PWMB, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO4_1_AS_APT1_PWMB, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO4_1_AS_APT1_PWMB, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO4_1_AS_APT1_PWMB, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN21 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO3_2_AS_APT2_PWMA); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO3_2_AS_APT2_PWMA, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO3_2_AS_APT2_PWMA, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO3_2_AS_APT2_PWMA, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO3_2_AS_APT2_PWMA, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN25 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO4_2_AS_APT2_PWMB); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO4_2_AS_APT2_PWMB, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO4_2_AS_APT2_PWMB, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO4_2_AS_APT2_PWMB, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO4_2_AS_APT2_PWMB, DRIVER_RATE_2); /* Output signal edge fast/slow */ +} + +static void APT_SyncMasterInit(void) +{ + HAL_APT_MasterSyncInit(&g_apt0, APT_SYNC_OUT_ON_CNTR_ZERO); +} + +static void APT_SyncSlaveInit(void) +{ + APT_SlaveSyncIn aptSlave; + + aptSlave.cntPhase = 0; /* counter phase value */ + aptSlave.syncCntMode = APT_COUNT_MODE_AFTER_SYNC_UP; + aptSlave.syncInSrc = APT_SYNCIN_SRC_APT0_SYNCOUT; /* sync source selection */ + aptSlave.cntrSyncSrc = APT_CNTR_SYNC_SRC_SYNCIN; + HAL_APT_SlaveSyncInit(&g_apt1, &aptSlave); + + aptSlave.cntPhase = 0; /* counter phase value */ + aptSlave.syncCntMode = APT_COUNT_MODE_AFTER_SYNC_UP; + aptSlave.syncInSrc = APT_SYNCIN_SRC_APT0_SYNCOUT; /* sync source selection */ + aptSlave.cntrSyncSrc = APT_CNTR_SYNC_SRC_SYNCIN; + HAL_APT_SlaveSyncInit(&g_apt2, &aptSlave); + +} + +void SystemInit(void) +{ + IOConfig(); + UART0_Init(); + ACMP0_Init(); + APT0_Init(); + APT1_Init(); + APT2_Init(); + ADC0_Init(); + TIMER1_Init(); + GPIO_Init(); + + APT_SyncMasterInit(); + APT_SyncSlaveInit(); + + /* USER CODE BEGIN system_init */ + /* USER CODE END system_init */ +} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/sample_gpio_key/main.c b/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/main.c similarity index 92% rename from vendor/yibaina_3061M/demo/sample_gpio_key/main.c rename to vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/main.c index 10f82fbe..b8775e1a 100644 --- a/vendor/yibaina_3061M/demo/sample_gpio_key/main.c +++ b/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/main.c @@ -18,17 +18,25 @@ * @file main.c * @author MCU Driver Team * @brief Main program body. + * @date 2025-04-23 14:18:03 */ #include "typedefs.h" #include "feature.h" -#include "gpio_key_sample.h" +#include "mcs_motor_process.h" #include "main.h" /* USER CODE BEGIN 0 */ /* USER CODE 区域内代码不会被覆盖,区域外会被生成的默认代码覆盖(其余USER CODE 区域同理) */ /* 建议用户放置头文件 */ /* USER CODE END 0 */ +ACMP_Handle g_acmp0; +TIMER_Handle g_timer1; UART_Handle g_uart0; +APT_Handle g_apt0; +APT_Handle g_apt1; +APT_Handle g_apt2; +ADC_Handle g_adc0; +GPIO_Handle g_gpio1; GPIO_Handle g_gpio2; /* USER CODE BEGIN 1 */ /* 建议用户定义全局变量、结构体、宏定义或函数声明等 */ @@ -39,7 +47,7 @@ int main(void) /* USER CODE BEGIN 2 */ /* 建议用户放置初始化代码或启动代码等 */ /* USER CODE END 2 */ - GPIO_KeySample(); + MotorMainProcess(); /* USER CODE BEGIN 3 */ /* 建议用户放置初始配置代码 */ /* USER CODE END 3 */ diff --git a/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/application/mcs_motor_process.c b/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/application/mcs_motor_process.c new file mode 100644 index 00000000..94898dd5 --- /dev/null +++ b/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/application/mcs_motor_process.c @@ -0,0 +1,562 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_motor_process.c + * @author MCU Algorithm Team + * @brief This file provides motor application for ECBMCU201MPC board. + * @details BLDC six-step application based on the ECBMCU201MPC board + * 1) Motor model is Gimbal GBM2804H-100T. + * 2) Select the bldc six-step sensorless example in the sample column + of chipConfig and click Generate Code. + * 3) It's power supply must be changed to 12V. + */ + +#include "debug.h" +#include "mcs_assert.h" +#include "mcs_user_config.h" +#include "mcs_status.h" +#include "mcs_carrier.h" +#include "mcs_motor_process.h" + +/*------------------------------- Macro Definition -----------------------------------------------*/ +#define US_PER_MS 1000 +#define APT_FULL_DUTY 1.0f + +/* Motor control handle for bldc */ +static MtrCtrlHandle g_mc; + +static APT_RegStruct* g_aptCp[PHASE_MAX_NUM] = {BRIDGE_CTR_APT_U, BRIDGE_CTR_APT_V, BRIDGE_CTR_APT_W}; + +/** + * @brief Initialzer of system tick. + * @param mtrCtrl Motor control struct handle. + * @retval None. + */ +static void TimerTickInit(MtrCtrlHandle *mtrCtrl) +{ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + mtrCtrl->sysTickCnt = 0; + mtrCtrl->msTickNum = US_PER_MS / SYSTICK_PERIOD_US; + mtrCtrl->capChargeTickNum = (INV_CAP_CHARGE_MS * US_PER_MS / SYSTICK_PERIOD_US); +} + +/** + * @brief Read the ADC current sampling value of the compressor. + * @param CurrUvw Three-phase current. + * @retval None. + */ +static void ReadBemfUVWMotor(UVWBemf *bemfUVW) +{ + MCS_ASSERT_PARAM(bemfUVW != NULL); + bemfUVW->u = (int)(HAL_ADC_GetConvResult(&ADC_HANDLE, ADC_U_SOC_NUM)&0xFFF); + bemfUVW->v = (int)(HAL_ADC_GetConvResult(&ADC_HANDLE, ADC_V_SOC_NUM)&0xFFF); + bemfUVW->w = (int)(HAL_ADC_GetConvResult(&ADC_HANDLE, ADC_W_SOC_NUM)&0xFFF); +} + +/** + * @brief Init motor control task. + * @retval None. + */ +static void TSK_InitMotor(void) +{ + /* Initialize target speed. */ + g_mc.spdCmdHz = SDP_TARGET_VALUE; + /* zeroPoint = IN_VOLTAGE_BUS / 2.0; 4095/3.3 :ADC value corresponding to 1 V */ + g_mc.zeroPoint = ((((float)IN_VOLTAGE_BUS / 2.0) * VOL_DIVIDER_COEFFICIENT) * 4095 / 3.3); + g_mc.pwmDuty = FORCE_DRAG_MINDUTY; + + /* Sets the number of sample filtering times for zero-crossing sampling. */ + g_mc.sysVar.bemfFilterCnt = FILTER_COUNT; + g_mc.sysVar.dragChangePhaseTime = DRAG_START_INTERVAL; + + g_mc.stateMachine = FSM_IDLE; + g_mc.aptMaxcntCmp = g_apt0.waveform.timerPeriod; + + /* Speed management initialization. */ + RMG_Init(&g_mc.spdRmg, CTRL_SYSTICK_PERIOD, USER_SPD_SLOPE); /* Init speed slope */ + TimerTickInit(&g_mc); + + /* Pid control parameter initialization. */ + g_mc.spdPi.kp = SPD_PID_KP; + g_mc.spdPi.ki = SPD_PID_KI; + g_mc.spdPi.ts = SPD_PID_TS; + g_mc.spdPi.upperLimit = APT_DUTYLIMIT_MAX; + g_mc.spdPi.lowerLimit = APT_DUTYLIMIT_MIN; + + /* BLDC six-step control initialization. */ + g_mc.stepCtrl.phaseStep = STEP1; + g_mc.stepCtrl.controlApt.u = &g_apt0; + g_mc.stepCtrl.controlApt.v = &g_apt1; + g_mc.stepCtrl.controlApt.w = &g_apt2; +} + + +/** + * @brief Software Initialization. + * @retval None. + */ +static void InitSoftware(void) +{ + TSK_InitMotor(); + + g_mc.readBemfUVW = ReadBemfUVWMotor; +} + +/** + * @brief Enable three-phase pwm output. + * @param aptAddr Three-phase APT address pointer. + * @retval None. + */ +static void MotorPwmOutputEnable(APT_RegStruct **aptAddr) +{ + MCS_ASSERT_PARAM(aptAddr != NULL); + /* Enable three-phase pwm output */ + for (unsigned int i = 0; i < PHASE_MAX_NUM; i++) { + APT_RegStruct *aptx = aptAddr[i]; + aptx->PG_OUT_FRC.BIT.rg_pga_frc_en = BASE_CFG_UNSET; + aptx->PG_OUT_FRC.BIT.rg_pgb_frc_en = BASE_CFG_UNSET; + } +} + +/** + * @brief Disable three-phase pwm output. + * @param aptAddr Three-phase APT address pointer. + * @retval None. + */ +static void MotorPwmOutputDisable(APT_RegStruct **aptAddr) +{ + MCS_ASSERT_PARAM(aptAddr != NULL); + /* Disable three-phase pwm output. */ + for (unsigned int i = 0; i < PHASE_MAX_NUM; i++) { + APT_RegStruct *aptx = aptAddr[i]; + aptx->PG_OUT_FRC.BIT.rg_pga_frc_en = BASE_CFG_SET; + aptx->PG_OUT_FRC.BIT.rg_pgb_frc_en = BASE_CFG_SET; + DCL_APT_ForcePWMOutputLow(aptx); + } +} + +/** + * @brief 强制转子对准原位。 + * @param mtrCtrl The motor control handle. + * @retval None. + */ +static void ForceAlign(MtrCtrlHandle *mtrCtrl) +{ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + mtrCtrl->pwmDuty = FORCE_DRAG_MINDUTY; + mtrCtrl->sysVar.dragChangePhaseTime = DRAG_START_INTERVAL; + MCS_SetCtrAptDuty(mtrCtrl, mtrCtrl->pwmDuty); + MotorPwmOutputEnable(g_aptCp); + mtrCtrl->stepCtrl.phaseStep = STEP6; + SixStepPwm(&mtrCtrl->stepCtrl); + BASE_FUNC_DELAY_MS(100); /* Delay 100 ms waiting for rotor alignment. */ + mtrCtrl->stepCtrl.phaseStep = STEP1; + SixStepPwm(&mtrCtrl->stepCtrl); + BASE_FUNC_DELAY_MS(100); /* Delay 100 ms waiting for rotor alignment. */ + mtrCtrl->sysVar.lastZeroPoint = DCL_SYSTICK_GetTick(); +} + +/** + * @brief Pre-processing of motor status. + * @param statusReg System status. + * @param stateMachine Motor Control Status. + * @retval None. + */ +static void MotorStatePerProc(SysStatusReg *statusReg, volatile FsmState *stateMachine) +{ + MCS_ASSERT_PARAM(statusReg != NULL); + MCS_ASSERT_PARAM(stateMachine != NULL); + /* Get system status */ + if (SysIsError(statusReg)) { + *stateMachine = FSM_FAULT; + } + if (SysGetCmdStop(statusReg)) { + SysCmdStopClr(statusReg); + *stateMachine = FSM_WAIT_STOP; + } +} + +/** + * @brief Open the three-phase lower pipe. + * @param aptAddr Three-phase APT address pointer. + * @param maxDutyCnt Max duty count. + * @retval None. + */ +static void AptTurnOnLowSidePwm(APT_RegStruct **aptAddr, unsigned int maxDutyCnt) +{ + MCS_ASSERT_PARAM(aptAddr != NULL); + MCS_ASSERT_PARAM(maxDutyCnt != 0); + unsigned int dutyCnt; + dutyCnt = maxDutyCnt * APT_FULL_DUTY; + /* Open the three-phase lower pipe */ + for (unsigned int i = 0; i < PHASE_MAX_NUM; i++) { + APT_RegStruct *aptx = aptAddr[i]; + aptx->TC_REFC.BIT.rg_cnt_refc = dutyCnt; + aptx->TC_REFD.BIT.rg_cnt_refd = dutyCnt; + } +} + +/** + * @brief Check systerm cmd start signal. + * @param mtrCtrl The motor control handle. + * @param aptAddr Three-phase APT address pointer. + * @param statusReg System status. + * @param stateMachine Motor Control Status. + * @retval None. + */ +static void CheckSysCmdStart(MtrCtrlHandle *mtrCtrl, APT_RegStruct **aptAddr, SysStatusReg *statusReg, + FsmState *stateMachine) +{ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + MCS_ASSERT_PARAM(aptAddr != NULL); + MCS_ASSERT_PARAM(statusReg != NULL); + MCS_ASSERT_PARAM(stateMachine != NULL); + /* check start cmd */ + if (SysGetCmdStart(statusReg)) { + SysRunningSet(statusReg); + SysCmdStartClr(statusReg); + mtrCtrl->sysTickCnt = 0; + *stateMachine = FSM_CAP_CHARGE; + /* Preparation for charging the bootstrap capacitor. */ + AptTurnOnLowSidePwm(aptAddr, mtrCtrl->aptMaxcntCmp); + /* Out put pwm */ + MotorPwmOutputEnable(aptAddr); + } +} + +/** + * @brief Clear historical values of all controller before start-up. + * @param mtrCtrl The motor control handle. + * @retval None. + */ +static void ClearBeforeStartup(MtrCtrlHandle *mtrCtrl) +{ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + mtrCtrl->aptMaxcntCmp = g_apt0.waveform.timerPeriod; + + /* Clear bemf parameters. */ + mtrCtrl->bemf.u = 0; + mtrCtrl->bemf.v = 0; + mtrCtrl->bemf.w = 0; + + /* Clear tickcnt. */ + mtrCtrl->msTickCnt = 0; + + mtrCtrl->pwmDuty = FORCE_DRAG_MINDUTY; + + /* Clear spd parameters. */ + mtrCtrl->spdEstHz = 0; + mtrCtrl->spdRefHz = 0; + + mtrCtrl->stepCtrl.phaseStep = STEP1; + + mtrCtrl->sysTickCnt = 0; + + mtrCtrl->sysVar.dragChangePhaseTime = DRAG_START_INTERVAL; + mtrCtrl->sysVar.changePhaseFlag = 0; + mtrCtrl->sysVar.firstEventFilterFlag = 0; + mtrCtrl->sysVar.stepTimeNum = 0; + mtrCtrl->sysVar.stepTimeFilterEnable = 0; + for (int i = 0; i < STEP_MAX_NUM; i++) { + mtrCtrl->sysVar.stepTime[i] = 0; + } + /* RMG CLEAR */ + RMG_Clear(&mtrCtrl->spdRmg); /* Clear the history value of speed slope control */ + /* SPDCTRL CLEAR */ + PID_Clear(&mtrCtrl->spdPi); +} + +/** + * @brief Check bootstrap capacitor charge time. + * @param mtrCtrl The motor control handle. + * @param stateMachine Motor Control Status. + * @retval None. + */ +static void CheckBootstrpCapChargeTime(MtrCtrlHandle *mtrCtrl, FsmState *stateMachine) +{ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + MCS_ASSERT_PARAM(stateMachine != NULL); + mtrCtrl->sysTickCnt++; + /* check bootstrap capacitor charge time */ + if (mtrCtrl->sysTickCnt == mtrCtrl->capChargeTickNum) { + /* Update Status. */ + *stateMachine = FSM_CLEAR; + } +} + +/** + * @brief Check over current status. + * @param statusReg System status. + * @param stateMachine Motor Control Status. + * @retval None. + */ +static void CheckOverCurrentState(SysStatusReg *statusReg, FsmState *stateMachine) +{ + MCS_ASSERT_PARAM(statusReg != NULL); + MCS_ASSERT_PARAM(stateMachine != NULL); + /* check systerm error status */ + if (SysIsError(statusReg) == false) { + *stateMachine = FSM_IDLE; + } +} + +/** + * @brief Deceleration control phase before stop. + * @param mtrCtrl The motor control handle. + * @param statusReg Motor Control Status. + * @param targetSpd Deceleration target value. + * @param switchSpd Switch to stop state speed. + * @retval None. + */ +static void DecelerateSpeed(MtrCtrlHandle *mtrCtrl, FsmState *stateMachine, float targetSpd, float switchSpd) +{ + /* Reduce speed before stop. */ + mtrCtrl->spdRefHz = RMG_Exec(&mtrCtrl->spdRmg, targetSpd); + mtrCtrl->spdPi.error = mtrCtrl->spdRefHz - mtrCtrl->spdEstHz; + /* Speed loop control */ + mtrCtrl->pwmDuty = PI_Exec(&mtrCtrl->spdPi); + if (mtrCtrl->spdRefHz <= switchSpd) { /* Maximum speed for switching to the FSM_STOP state */ + *stateMachine = FSM_STOP; + } +} + +/** + * @brief System timer tick task. + * @param mtrCtrl The motor control handle. + * @param aptAddr Three-phase APT address pointer. + * @retval None. + */ +static void TSK_SystickIsr(MtrCtrlHandle *mtrCtrl, APT_RegStruct **aptAddr) +{ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + MCS_ASSERT_PARAM(aptAddr != NULL); + SysStatusReg *statusReg = &mtrCtrl->statusReg; + volatile FsmState *stateMachine = &mtrCtrl->stateMachine; + mtrCtrl->msTickCnt++; + /* Pre-processing of motor status. */ + MotorStatePerProc(statusReg, stateMachine); + /* statemachine */ + switch (*stateMachine) { + case FSM_IDLE: + CheckSysCmdStart(mtrCtrl, aptAddr, statusReg, (FsmState *)stateMachine); + break; + case FSM_CAP_CHARGE: + /* Bootstrap Capacitor Charging Timing */ + CheckBootstrpCapChargeTime(mtrCtrl, (FsmState *)stateMachine); + break; + case FSM_CLEAR: + /* Clearing control parameters. */ + ClearBeforeStartup(mtrCtrl); + /* Rotor alignment. */ + ForceAlign(mtrCtrl); + *stateMachine = FSM_STARTUP; + break; + case FSM_STARTUP: + /* Forced drag. */ + break; + case FSM_RUN: + /* Speed ramp control */ + mtrCtrl->spdRefHz = RMG_Exec(&mtrCtrl->spdRmg, mtrCtrl->spdCmdHz); + mtrCtrl->spdPi.error = mtrCtrl->spdRefHz - mtrCtrl->spdEstHz; + /* Speed loop control */ + mtrCtrl->pwmDuty = PI_Exec(&mtrCtrl->spdPi); + break; + case FSM_WAIT_STOP: + /* 5.0f : maximum speed for switching to the FSM_STOP state */ + DecelerateSpeed(mtrCtrl, (FsmState *)stateMachine, 0.0f, 5.0f); + break; + case FSM_STOP: + MotorPwmOutputDisable(aptAddr); + SysRunningClr(statusReg); + *stateMachine = FSM_IDLE; + break; + case FSM_FAULT: + /* Overcurrent state */ + CheckOverCurrentState(statusReg, (FsmState *)stateMachine); + break; + default: + break; + } +} + +/** + * @brief Overcurrent protection. + * @param aptHandle The apt Handle. + * @retval None. + */ +void MotorSysErrCallback(void *aptHandle) +{ + /* Overcurrent protection callback function. */ + BASE_FUNC_UNUSED(aptHandle); + MotorPwmOutputDisable(g_aptCp); + DCL_APT_ClearOutCtrlEventFlag((APT_RegStruct *)g_aptCp[PHASE_U], APT_OC_COMBINE_EVENT_A1); + DCL_APT_ClearOutCtrlEventFlag((APT_RegStruct *)g_aptCp[PHASE_V], APT_OC_COMBINE_EVENT_A1); + DCL_APT_ClearOutCtrlEventFlag((APT_RegStruct *)g_aptCp[PHASE_W], APT_OC_COMBINE_EVENT_A1); + /* Status setting error */ + SysErrorSet(&g_mc.statusReg); + DBG_PRINTF("APT error! \r\n"); + HAL_GPIO_SetValue(&g_gpio2, GPIO_PIN_3, GPIO_LOW_LEVEL); +} + +/** + * @brief Motor blockage protection function. + * @retval None. + */ +static void MotorBlockageProtect(void) +{ + unsigned int currentTick = DCL_SYSTICK_GetTick(); /* Get the current tick value. */ + unsigned int intervalTick = (currentTick >= g_mc.sysVar.lastZeroPoint) + ? currentTick - g_mc.sysVar.lastZeroPoint + : SYSTICK_MAX_VALUE - g_mc.sysVar.lastZeroPoint + currentTick + 1; + if (intervalTick > SYSTICK_GetCRGHZ()) { + MotorPwmOutputDisable(g_aptCp); + SysErrorSet(&g_mc.statusReg); + g_mc.spdEstHz = 0; + return; + } +} + +/** + * @brief Carrier Interruption. + * @param aptHandle The apt Handle. + * @retval None. + */ +void MotorCarrierProcessCallback(void *aptHandle) +{ + BASE_FUNC_UNUSED(aptHandle); + /* USER CODE BEGIN APT0_TIMER_INTERRUPT */ + MCS_CarrierProcess(&g_mc); + if (g_mc.stateMachine == FSM_RUN || g_mc.stateMachine == FSM_WAIT_STOP) { + MotorBlockageProtect(); + } + /* USER CODE END APT0_TIMER_INTERRUPT */ +} + +/** + * @brief Change phase delay callback function. + * @param handle The TIMER1 Handle. + * @retval None. + */ +void MotorStatemachineCallBack(void *handle) +{ + /* TIMER1CallbackFunction */ + BASE_FUNC_UNUSED(handle); + TSK_SystickIsr(&g_mc, g_aptCp); +} + +/** + * @brief Check Potentiometer Value and adjust speed. + * @param None. + * @retval None. + */ +static void AdjustSpeedFunction(void) +{ + static unsigned int potentiomitorAdcValue = 0; + static float spdCmdHz = 0; + static float spdCmdHzLast = SDP_MAX_VALUE; + HAL_ADC_SoftTrigSample(&ADC_HANDLE, ADC_SOC_NUM9); /* Get the speed adjustment resistance. */ + potentiomitorAdcValue = HAL_ADC_GetConvResult(&ADC_HANDLE, ADC_SOC_NUM9); + /* 4045.0 is adc sample max value of potentiomitor */ + spdCmdHz = (float)potentiomitorAdcValue / 4045.0 * SDP_MAX_VALUE; + if (spdCmdHz < SDP_MIN_VALUE) { /* Speed protection. */ + spdCmdHz = SDP_MIN_VALUE; + } + if (spdCmdHz > SDP_MAX_VALUE) { + spdCmdHz = SDP_MAX_VALUE; + } + float delta = spdCmdHzLast > spdCmdHz ? (spdCmdHzLast - spdCmdHz) : (spdCmdHz - spdCmdHzLast); + /* 1.0 : If the speed fluctuation is less than 1.0Hz, no change is made. */ + if (delta < 1.0) { + return; + } + spdCmdHzLast = spdCmdHz; + g_mc.spdCmdHz = spdCmdHz; +} + +/** + * @brief Config the KEY func. + * @param handle The GPIO handle. + * @retval None. + */ +static KEY_State Key_StateRead(GPIO_Handle *handle) +{ + if (HAL_GPIO_GetPinValue(handle, handle->pins) == 0) { + BASE_FUNC_DELAY_MS(30); /* delay 30ms for deshake */ + if (HAL_GPIO_GetPinValue(handle, handle->pins) == 0) { + while (HAL_GPIO_GetPinValue(handle, handle->pins) == 0) { + } + return KEY_DOWN; + } + } + return KEY_UP; +} + +/** + * @brief Control motor start and stop state by key func. + * @param param The GPIO handle. + * @retval None. + */ +void MotorStartStopKeyCallback(void *param) +{ + GPIO_Handle *handle = (GPIO_Handle *)param; + if (Key_StateRead(handle) == KEY_DOWN) { + if (SysIsRunning(&g_mc.statusReg)) { /* stop apt output, motor is off status */ + SysCmdStopSet(&g_mc.statusReg); + } else { /* start apt output, motor is on status */ + HAL_APT_StartModule(RUN_APT0 | RUN_APT1 | RUN_APT2); + SysCmdStartSet(&g_mc.statusReg); + } + } +} + +/** + * @brief User application entry. + * @retval BSP_OK. + */ +int MotorMainProcess(void) +{ + SystemInit(); + /* System Initialization. */ + unsigned int tickNum100Ms = 200; /* 100ms tick */ + static unsigned int tickCnt100Ms = 0; + unsigned int tickNum500Ms = 1000; /* 500ms tick */ + static unsigned int tickCnt500Ms = 0; + + HAL_TIMER_Start(&g_timer1); + /* Disable PWM output before startup. */ + MotorPwmOutputDisable(g_aptCp); + InitSoftware(); + /* Start the PWM clock. */ + HAL_APT_StartModule(RUN_APT0 | RUN_APT1 | RUN_APT2); + + while (true) { + if (g_mc.msTickCnt - tickCnt100Ms >= tickNum100Ms) { + tickCnt100Ms = g_mc.msTickCnt; + /* User Code 100ms Event */ + AdjustSpeedFunction(); + } + if (g_mc.msTickCnt - tickCnt500Ms >= tickNum500Ms) { + if (SysIsError(&g_mc.statusReg) == true) { + break; + } + /* The LED blinks when no status is not error. */ + HAL_GPIO_TogglePin(&SYS_LED_HANDLE, SYS_LED_PIN); + tickCnt500Ms = g_mc.msTickCnt; + } + } + return 0; +} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/application/mcs_motor_process.h b/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/application/mcs_motor_process.h new file mode 100644 index 00000000..a2fdeb2c --- /dev/null +++ b/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/application/mcs_motor_process.h @@ -0,0 +1,47 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_motor_process.h + * @author MCU Algorithm Team + * @brief This file provides motor sample functions declaration for ECBMCU201MPC board. + */ +#ifndef McuMagicTag_MCS_MOTOR_PROCESS_H +#define McuMagicTag_MCS_MOTOR_PROCESS_H + +#include "main.h" + +/** + * @brief phase sequence. + */ +typedef enum { + PHASE_U = 0, + PHASE_V, + PHASE_W, + PHASE_MAX_NUM +} MCS_PhaseEnum; + +/** + * @brief key state. + */ +typedef enum { + KEY_DOWN = 0, + KEY_UP = 1, +} KEY_State; + +int MotorMainProcess(void); + +#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/application/mcs_user_config.h b/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/application/mcs_user_config.h new file mode 100644 index 00000000..f193532a --- /dev/null +++ b/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/application/mcs_user_config.h @@ -0,0 +1,94 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_config.h + * @author MCU Algorithm Team + * @brief This file provides config macros for ECBMCU201MPC app. + */ +#ifndef McuMagicTag_MCS_CONFIG_H +#define McuMagicTag_MCS_CONFIG_H + +#if defined(CHIP_3061MNPICA) || defined(CHIP_3061MNNICA) || defined(CHIP_3061MNPIC8) || defined(CHIP_3061MNNIC8) +#define ADC_U_SOC_NUM ADC_SOC_NUM2 +#define ADC_V_SOC_NUM ADC_SOC_NUM5 +#define ADC_W_SOC_NUM ADC_SOC_NUM6 +#define ADC_SPD_ADJ_SOC_NUM ADC_SOC_NUM9 +#define ADC_HANDLE g_adc0 +#endif + +#if defined (CHIP_3066MNPIRH) || defined (CHIP_3065PNPIRH) || defined (CHIP_3065PNPIRE) || defined (CHIP_3065PNPIRA) +#define ADC_U_SOC_NUM ADC_SOC_NUM3 +#define ADC_V_SOC_NUM ADC_SOC_NUM5 +#define ADC_W_SOC_NUM ADC_SOC_NUM7 +#define ADC_SPD_ADJ_SOC_NUM ADC_SOC_NUM9 + +#define ADC_HANDLE g_adc2 +#endif + +#define APT_PWM_FREQ 12000 /* PWM frequency 12K Hz. */ +#define CTRL_CURR_PERIOD (1.0f / APT_PWM_FREQ) /* carrier ISR period (S) */ +#define CTRL_CURE_PERIOD_US 83 + +#define SYSTICK_PERIOD_US 500u /* systick period. unit : us */ + +#define INV_CAP_CHARGE_MS 3u /* Bootstrap capacitor charge time. unit : ms */ + +#define CTRL_SYSTICK_PERIOD 0.0005f /* systick control period, 500us */ + +#define USER_SPD_SLOPE 60.0f /* slope of velocity change */ + +#define IN_VOLTAGE_BUS 24 /* Motor input voltage */ +#define POLES 14 /* 电机磁极对数 */ +#define MATH_PI 3.14 /* Pi */ +#define MOTOR_K 1.62 /* 转矩系数 */ + +#define RAMP_STP 60 /* Slope of forced draging acceleration */ +#define RAMP_DUTY_PWM 0.5 /* 1.0% */ + +#define FORCE_DRAG_MINDUTY 15 /* APT minimum duty cycle during forced drag */ +#define FORCE_DRAG_MAXDUTY 20 /* APT maximum duty cycle during forced drag */ + +#define DRAG_START_INTERVAL 1000 /* Force drag change phase time */ +#define DRAG_STOP_INTERVAL 400 /* Force drag change phase time */ + +/* Parameters of the motor in the RUN */ +#define FILTER_COUNT 3 /* Filter Times */ +#define PHASE_OFFSET 9000 /* Phase delay due to filtering */ + +#define APT_DUTYLIMIT_MAX 99.9 /* 输出APT的最大占空比 */ +#define APT_DUTYLIMIT_MIN 8.0 /* Minimum duty cycle of the output APT */ + +#define BRIDGE_CTR_APT_U APT0 /* APT address that controls the U phase */ +#define BRIDGE_CTR_APT_V APT1 /* APT address that controls the V phase */ +#define BRIDGE_CTR_APT_W APT2 /* APT address that controls the W phase */ + +#define SDP_MAX_VALUE 58.8 /* 最大相位变化频率 */ +#define SDP_MIN_VALUE 15.0 /* Minimum change phase frequency */ +#define SDP_TARGET_VALUE 58.8 /* 目标相位变化频率 */ + +/* SPD PID Param */ +#define SPD_PID_KP 0.9 /* P parameter of PID control */ +#define SPD_PID_KI 40 /* I parameter of PID control */ +#define SPD_PID_TS 0.0005 /* TS parameter of PID control cycle */ + +#define OP_TO_CL_INTERGRAL 8.0 /* Open-loop switching closed-loop integral term to prevent sudden current change. */ + +#define VOL_DIVIDER_COEFFICIENT 0.09091f /* Division coefficient of the zero-crossing detection sampling circuit */ + +#define SPD_FILTER_FC 30 + +#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/common/mcs_carrier.c b/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/common/mcs_carrier.c new file mode 100644 index 00000000..7779fbef --- /dev/null +++ b/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/common/mcs_carrier.c @@ -0,0 +1,224 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_carrier.c + * @author MCU Algorithm Team + * @brief This file provides carrier interrupt application for motor control. + */ + +#include "mcs_carrier.h" +#include "mcs_user_config.h" +#include "mcs_assert.h" +#include "debug.h" + +#define S_TO_US 1000000 +#define S_TO_SYSTICK 100000000 + +/** + * @brief Sets the duty cycle of the H-bridge APT. + * @param aptHandle APT module handle. + * @param duty PWM duty. Range: 0.1 ~ 99.9. + * @retval None. + */ +void MCS_SetCtrAptDuty(MtrCtrlHandle *mtrCtrl, unsigned int duty) +{ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + MCS_ASSERT_PARAM(duty > 0); + /* Set pwm duty of uvw pahse */ + HAL_APT_SetPWMDutyByNumber(mtrCtrl->stepCtrl.controlApt.u, duty); + HAL_APT_SetPWMDutyByNumber(mtrCtrl->stepCtrl.controlApt.v, duty); + HAL_APT_SetPWMDutyByNumber(mtrCtrl->stepCtrl.controlApt.w, duty); +} + +/** + * @brief Strong drag start. + * @param mtrCtrl The motor control handle. + * @retval None. + */ +static void ForceDragAcc(MtrCtrlHandle *mtrCtrl) +{ + /* Verifying Parameters. */ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + unsigned int voltageDValue; + unsigned int dragChangeFreq; + unsigned int timerperiod; + unsigned int freqLast; + unsigned int aptCountsOneUs; + + mtrCtrl->sysVar.accTimeCnt++; + if (mtrCtrl->sysVar.accTimeCnt < mtrCtrl->sysVar.dragChangePhaseTime) { + return; + } + + mtrCtrl->sysVar.accTimeCnt = 0; + /* Step 1: Calculate the voltage difference. */ + voltageDValue = IN_VOLTAGE_BUS * RAMP_DUTY_PWM; + /* Step 2: Calculate the commutation frequency. */ + freqLast = S_TO_US / (mtrCtrl->sysVar.dragChangePhaseTime * CTRL_CURE_PERIOD_US); + dragChangeFreq = voltageDValue * STEP_MAX_NUM * POLES / (MOTOR_K * 2 * MATH_PI) + freqLast; /* 2*PI = 360° */ + /* Step 3: Convert the commutation frequency to the commutation count value. */ + timerperiod = mtrCtrl->stepCtrl.controlApt.u->waveform.timerPeriod * CTRL_CURE_PERIOD_US; + mtrCtrl->sysVar.dragChangePhaseTime = S_TO_US / (timerperiod * dragChangeFreq); + + /* Determine whether the change phase speed has reached the speed of stopping forced drag. */ + if (mtrCtrl->sysVar.dragChangePhaseTime < DRAG_STOP_INTERVAL) { + mtrCtrl->sysVar.dragChangePhaseTime = DRAG_STOP_INTERVAL; + mtrCtrl->sysVar.accTimeCnt = 0; + mtrCtrl->sysVar.bemfFilterCnt = 0; + mtrCtrl->stateMachine = FSM_RUN; + mtrCtrl->sysVar.lastZeroPoint = DCL_SYSTICK_GetTick(); + mtrCtrl->spdPi.integral = OP_TO_CL_INTERGRAL; + } + + /* Shorten the time interval of forced drag change phase. */ + mtrCtrl->pwmDuty += RAMP_DUTY_PWM; + aptCountsOneUs = HAL_CRG_GetIpFreq(APT0_BASE) / S_TO_US; + mtrCtrl->sysVar.waitTime = (mtrCtrl->sysVar.dragChangePhaseTime * CTRL_CURE_PERIOD_US * aptCountsOneUs) >> 1; + /* Change phase time is 2 the waiting time. */ + mtrCtrl->spdRefHz = (float)(HAL_CRG_GetIpFreq(SYSTICK_BASE) / (mtrCtrl->sysVar.waitTime * 2)) / STEP_MAX_NUM; + mtrCtrl->spdEstHz = mtrCtrl->spdRefHz; + mtrCtrl->spdRmg.yLast = mtrCtrl->spdRefHz; + mtrCtrl->stepCtrl.phaseStep = (mtrCtrl->stepCtrl.phaseStep + 1) % STEP_MAX_NUM; + SixStepPwm(&mtrCtrl->stepCtrl); + /* Limiting the PWM Duty Cycle Range. */ + if (mtrCtrl->pwmDuty > FORCE_DRAG_MAXDUTY) { + mtrCtrl->pwmDuty = FORCE_DRAG_MAXDUTY; + } + if (mtrCtrl->pwmDuty < FORCE_DRAG_MINDUTY) { + mtrCtrl->pwmDuty = FORCE_DRAG_MINDUTY; + } + MCS_SetCtrAptDuty(mtrCtrl, mtrCtrl->pwmDuty); +} + +/** + * @brief Zero-crossing filter. + * @param mtrCtrl The motor control handle. + * @retval None. + */ +static void BemfZeroSampleFilter(MtrCtrlHandle *mtrCtrl) +{ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + mtrCtrl->sysVar.bemfFilterCnt++; + /* Filter the number of samples. */ + if (mtrCtrl->sysVar.bemfFilterCnt >= FILTER_COUNT) { + mtrCtrl->sysVar.bemfFilterCnt = 0; + unsigned int currentTick = DCL_SYSTICK_GetTick(); + if (mtrCtrl->sysVar.firstEventFilterFlag == 0) { + mtrCtrl->sysVar.lastZeroPoint = currentTick; + mtrCtrl->sysVar.changePhaseFlag = 1; + mtrCtrl->sysVar.firstEventFilterFlag = 1; + return; + } + /* Calculate the time interval between the last zero crossing. */ + mtrCtrl->sysVar.stepTime[mtrCtrl->stepCtrl.phaseStep] = currentTick >= mtrCtrl->sysVar.lastZeroPoint + ? currentTick - mtrCtrl->sysVar.lastZeroPoint + : SYSTICK_MAX_VALUE - mtrCtrl->sysVar.lastZeroPoint + currentTick + 1; + mtrCtrl->sysVar.stepTimeNum++; + /* Wait time is half the interval. */ + mtrCtrl->sysVar.waitTime = mtrCtrl->sysVar.stepTime[mtrCtrl->stepCtrl.phaseStep] >> 1; + mtrCtrl->sysVar.waitTime -= PHASE_OFFSET; + mtrCtrl->sysVar.lastZeroPoint = currentTick; + /* Update flag. */ + mtrCtrl->sysVar.changePhaseFlag = 1; + } +} + +/** + * @brief Back EMF zero crossing check. + * @param mtrCtrl The motor control handle. + * @retval None. + */ +static void BemfZeroCheck(MtrCtrlHandle *mtrCtrl) +{ + /* The step corresponds to different conditional judgments. */ + unsigned int bemfCheckTable[STEP_MAX_NUM] = {mtrCtrl->bemf.w < mtrCtrl->zeroPoint ? 1 : 0, + mtrCtrl->bemf.v > mtrCtrl->zeroPoint ? 1 : 0, + mtrCtrl->bemf.u < mtrCtrl->zeroPoint ? 1 : 0, + mtrCtrl->bemf.w > mtrCtrl->zeroPoint ? 1 : 0, + mtrCtrl->bemf.v < mtrCtrl->zeroPoint ? 1 : 0, + mtrCtrl->bemf.u > mtrCtrl->zeroPoint ? 1 : 0}; + + if (bemfCheckTable[mtrCtrl->stepCtrl.phaseStep] == 1) { + BemfZeroSampleFilter(mtrCtrl); + } +} + +/** + * @brief Change phase and speed estimation. + * @param mtrCtrl The motor control handle. + * @retval None. + */ +static void ChangePhase(MtrCtrlHandle *mtrCtrl) +{ + unsigned int currentTick = DCL_SYSTICK_GetTick(); /* Get the current tick value. */ + unsigned int intervalTick = (currentTick >= mtrCtrl->sysVar.lastZeroPoint) + ? currentTick - mtrCtrl->sysVar.lastZeroPoint + : SYSTICK_MAX_VALUE - mtrCtrl->sysVar.lastZeroPoint + currentTick + 1; + /* Start average filtering when the number of recorded data reaches six. */ + if (mtrCtrl->sysVar.stepTimeNum >= STEP_MAX_NUM) { + mtrCtrl->sysVar.stepTimeNum = 0; + mtrCtrl->sysVar.stepTimeFilterEnable = 1; + } + /* If the accumulated time is greater than the waiting commutation time, commutation is performed. */ + if (intervalTick > mtrCtrl->sysVar.waitTime) { + if (mtrCtrl->sysVar.stepTimeFilterEnable) { /* 6-step commutation time averaging if filtering is enabled. */ + unsigned int totalTime = mtrCtrl->sysVar.stepTime[STEP1] + mtrCtrl->sysVar.stepTime[STEP2] + + mtrCtrl->sysVar.stepTime[STEP3] + mtrCtrl->sysVar.stepTime[STEP4] + + mtrCtrl->sysVar.stepTime[STEP5] + mtrCtrl->sysVar.stepTime[STEP6]; + mtrCtrl->spdEstHz = (float)HAL_CRG_GetIpFreq(SYSTICK_BASE) / totalTime; + } else { + mtrCtrl->spdEstHz = (float)HAL_CRG_GetIpFreq(SYSTICK_BASE) / (mtrCtrl->sysVar.waitTime << 1) / STEP_MAX_NUM; + } + /* Change phase. */ + mtrCtrl->stepCtrl.phaseStep = (mtrCtrl->stepCtrl.phaseStep + 1) % STEP_MAX_NUM; + SixStepPwm(&mtrCtrl->stepCtrl); + mtrCtrl->sysVar.changePhaseFlag = 0; + } +} + +/** + * @brief Zero-crossing detection and change phase + * @param mtrCtrl The motor control handle. + * @retval None. + */ +void MCS_CarrierProcess(MtrCtrlHandle *mtrCtrl) +{ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + /* Offset value of the calibration value of the three-phase current */ + + switch (mtrCtrl->stateMachine) { + case FSM_STARTUP: + ForceDragAcc(mtrCtrl); /* Forced drag. */ + break; + + case FSM_RUN: + case FSM_WAIT_STOP: + mtrCtrl->readBemfUVW(&mtrCtrl->bemf); + MCS_SetCtrAptDuty(mtrCtrl, mtrCtrl->pwmDuty); + if (mtrCtrl->sysVar.changePhaseFlag) { + /* Change phase process. */ + ChangePhase(mtrCtrl); + } else { + /* Zero-crossing detection procedure. */ + BemfZeroCheck(mtrCtrl); + } + break; + + default: + break; + } +} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/common/mcs_carrier.h b/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/common/mcs_carrier.h new file mode 100644 index 00000000..05fc2666 --- /dev/null +++ b/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/common/mcs_carrier.h @@ -0,0 +1,91 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_carrier.h + * @author MCU Algorithm Team + * @brief This file provides functions declaration for carrier interrupt processing function. + */ +#ifndef McuMagicTag_MCS_CARRIER_H +#define McuMagicTag_MCS_CARRIER_H + +#include "mcs_status.h" +#include "mcs_six_step.h" +#include "mcs_ramp_mgmt.h" +#include "mcs_spd_ctrl.h" +#include "mcs_fsm.h" + +/** + * @brief Three-phase static coordinate frame variable. + */ +typedef struct { + unsigned int u; /**< Component u of the three-phase static coordinate frame variable. */ + unsigned int v; /**< Component v of the three-phase static coordinate frame variable. */ + unsigned int w; /**< Component w of the three-phase static coordinate frame variable. */ +} UVWBemf; + +typedef void (*ReadBemf)(UVWBemf *bemfUVW); + +/** + * @brief The definition of the systematic global variables + */ +typedef struct { + unsigned char changePhaseFlag; /**< Flag indicating that delay change phase is required */ + unsigned char firstEventFilterFlag; /**< First zero-crossing event filtering flag. */ + unsigned int dragChangePhaseTime; /**< Interval for forced drag acceleration */ + unsigned int accTimeCnt; /**< Acceleration time count */ + + unsigned int bemfFilterCnt; /**< Number of sample filtering times for zero-crossing detection. */ + + unsigned int lastZeroPoint; /**< Time of the last zero point */ + unsigned int waitTime; /**< Time to wait for change phase */ + unsigned int stepTime[STEP_MAX_NUM]; /**< Record the time spent on each step */ + unsigned int stepTimeNum; /**< Number of data records in the steptime array. */ + unsigned int stepTimeFilterEnable; +} SysVariable; + +typedef struct { + volatile float spdCmdHz; /**< Set target change phase frequency */ + float spdRefHz; /**< Command values after speed ramp management */ + float spdEstHz; /**< Actual change phase frequency of feedback */ + float pwmDuty; /**< APT duty cycle */ + unsigned int zeroPoint; /**< Adc value of zero point */ + + unsigned short aptMaxcntCmp; /**< Apt Maximum Comparison Count */ + + unsigned short sysTickCnt; /**< System Timer Tick Count */ + unsigned short capChargeTickNum; /**< Bootstrap Capacitor Charge Tick Count */ + unsigned short msTickNum; /**< Number of ticks corresponding to 1 ms */ + volatile unsigned int msTickCnt; /**< Millisecond-level counter, which can be used in 1-ms and 5-ms tasks. */ + + RMG_Handle spdRmg; /**< Ramp management struct for the speed controller input reference */ + PID_Handle spdPi; /**< PI controller struct in the speed controller. */ + + UVWBemf bemf; /**< Three-phase back electromotive force */ + SixStepHandle stepCtrl; /**< Control structure of six-step square wave */ + + SysVariable sysVar; /**< System Variables */ + SysStatusReg statusReg; /**< System Status */ + FsmState stateMachine; /**< BLDC Motor Control State Machine */ + + ReadBemf readBemfUVW; /**< Function interface for obtaining the three-phase electromotive force */ +} MtrCtrlHandle; + +void MCS_CarrierProcess(MtrCtrlHandle *mtrCtrl); + +void MCS_SetCtrAptDuty(MtrCtrlHandle *mtrCtrl, unsigned int duty); + +#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/common/mcs_fsm.h b/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/common/mcs_fsm.h new file mode 100644 index 00000000..b3c79ea2 --- /dev/null +++ b/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/common/mcs_fsm.h @@ -0,0 +1,55 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_fsm.h + * @author MCU Algorithm Team + * @brief This file provides the definition of finite statemachine (FSM). + */ + +#ifndef McuMagicTag_MCS_FSM_H +#define McuMagicTag_MCS_FSM_H + +/** + * @brief motor control FSM state define. + * @details motor control FSM state type: + * + FSM_IDLE -- IDLE state, system startup default. + * + FSM_OFFSET_CALIB -- Self calibrate, for ADC init. + * + FSM_CAP_CHARGE -- IPM cap charge. + * + FSM_CLEAR -- Clear before every run. + * + FSM_LOCATE -- Rotor position pre-locate. + * + FSM_STARTUP -- Start up. + * + FSM_SWITCH -- Transition state, control from open to closed loop. + * + FSM_RUN -- Normal running state. + * + FSM_WAIT_STOP -- Wait stopping. + * + FSM_STOP -- Normal stop. + * + FSM_FAULT -- Fault state, waiting for user process. + */ +typedef enum { + FSM_IDLE = 0, + FSM_OFFSET_CALIB, + FSM_CAP_CHARGE, + FSM_CLEAR, + FSM_LOCATE, + FSM_STARTUP, + FSM_SWITCH, + FSM_RUN, + FSM_WAIT_STOP, + FSM_STOP, + FSM_FAULT +} FsmState; + +#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/func/mcs_six_step.c b/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/func/mcs_six_step.c new file mode 100644 index 00000000..c86e609c --- /dev/null +++ b/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/func/mcs_six_step.c @@ -0,0 +1,126 @@ +/** + * @ Copyright (c) HiSilicon (Shanghai) Technologies Co., Ltd. 2022-2023. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_six_step.c + * @author MCU Algorithm Team + * @brief This file provides functions to control the H bridge. + */ + +#include "mcs_assert.h" +#include "mcs_six_step.h" + +/* Mode for disabling or enabling software-based forced output */ +#define APT_PWM_MODE 0 +#define APT_FRC_MODE 1 + +#define PWM_SIGNAL 1 + +#define FRC_LOW APT_OUT_CTRL_ACTION_LOW +#define FRC_HIGH APT_OUT_CTRL_ACTION_HIGH + +/** + * @brief Force PWM output by APT + * @param APTx The APT to use + * @param PwmAout Force to be high or low + * @param PwmAenble PWMA signal software force mode + * @param PwmBout Force to be high or low + * @param PwmBenble PWMB signal software force mode + */ +static void APT_ForcePwmByApt(APT_RegStruct *aptx, APT_Act actMode) +{ + MCS_ASSERT_PARAM(aptx != NULL); + switch (actMode) { + case APT_CHA_PWM_CHB_LOW: + /* Channel A: 0 means not force output enable, channel A output PWM. */ + DCL_APT_DisableSwContPWMAction(aptx, APT_PWM_CHANNEL_A); + DCL_APT_SetSwContPWMAction(aptx, APT_PWM_CHANNEL_A, APT_PWM_CONTINUOUS_ACTION_LOW); + /* Channel B: 1 means force output enable. */ + DCL_APT_EnableSwContPWMAction(aptx, APT_PWM_CHANNEL_B); + /* Channel B: 2 means channel B force output LOW due to the A_H_B_L invert. */ + DCL_APT_SetSwContPWMAction(aptx, APT_PWM_CHANNEL_B, APT_PWM_CONTINUOUS_ACTION_HIGH); + break; + case APT_CHA_LOW_CHB_HIGH: + /* Channel A: 1 means force output enable. */ + /* Channel A: 1 means channel A force output LOW. */ + DCL_APT_EnableSwContPWMAction(aptx, APT_PWM_CHANNEL_A); + DCL_APT_SetSwContPWMAction(aptx, APT_PWM_CHANNEL_A, APT_PWM_CONTINUOUS_ACTION_LOW); + /* Channel B: 1 means force output enable. */ + /* Channel B: 2 means channel B force output HIGH. */ + DCL_APT_EnableSwContPWMAction(aptx, APT_PWM_CHANNEL_B); + DCL_APT_SetSwContPWMAction(aptx, APT_PWM_CHANNEL_B, APT_PWM_CONTINUOUS_ACTION_HIGH); + break; + case APT_CHA_LOW_CHB_LOW: + /* Channel A: 1 means force output enable. */ + /* Channel A: 1 means channel A force output LOW. */ + DCL_APT_EnableSwContPWMAction(aptx, APT_PWM_CHANNEL_A); + DCL_APT_SetSwContPWMAction(aptx, APT_PWM_CHANNEL_A, APT_PWM_CONTINUOUS_ACTION_LOW); + /* Channel B: 1 means force output enable. */ + /* Channel B: 1 means channel A force output HIGH due to the A_H_B_L invert. */ + DCL_APT_EnableSwContPWMAction(aptx, APT_PWM_CHANNEL_B); + DCL_APT_SetSwContPWMAction(aptx, APT_PWM_CHANNEL_B, APT_PWM_CONTINUOUS_ACTION_LOW); + break; + default: + break; + } +} + +/** + * @brief Output six-step square wave. + * @param handle The SixStepHandle. + * @retval None. + */ +void SixStepPwm(const SixStepHandle *handle) +{ + MCS_ASSERT_PARAM(handle != NULL); + APT_RegStruct *aptU = handle->controlApt.u->baseAddress; /* Get apt u\v\w baseaddress. */ + APT_RegStruct *aptV = handle->controlApt.v->baseAddress; + APT_RegStruct *aptW = handle->controlApt.w->baseAddress; + switch (handle->phaseStep) { + case STEP1: /* U+, V- */ + APT_ForcePwmByApt(aptU, APT_CHA_PWM_CHB_LOW); + APT_ForcePwmByApt(aptV, APT_CHA_LOW_CHB_LOW); + APT_ForcePwmByApt(aptW, APT_CHA_LOW_CHB_HIGH); + break; + case STEP2: /* U+, W- */ + APT_ForcePwmByApt(aptU, APT_CHA_PWM_CHB_LOW); + APT_ForcePwmByApt(aptV, APT_CHA_LOW_CHB_HIGH); + APT_ForcePwmByApt(aptW, APT_CHA_LOW_CHB_LOW); + break; + case STEP3: /* V+, W- */ + APT_ForcePwmByApt(aptU, APT_CHA_LOW_CHB_HIGH); + APT_ForcePwmByApt(aptV, APT_CHA_PWM_CHB_LOW); + APT_ForcePwmByApt(aptW, APT_CHA_LOW_CHB_LOW); + break; + case STEP4: /* V+, U- */ + APT_ForcePwmByApt(aptU, APT_CHA_LOW_CHB_LOW); + APT_ForcePwmByApt(aptV, APT_CHA_PWM_CHB_LOW); + APT_ForcePwmByApt(aptW, APT_CHA_LOW_CHB_HIGH); + break; + case STEP5: /* W+, U- */ + APT_ForcePwmByApt(aptU, APT_CHA_LOW_CHB_LOW); + APT_ForcePwmByApt(aptV, APT_CHA_LOW_CHB_HIGH); + APT_ForcePwmByApt(aptW, APT_CHA_PWM_CHB_LOW); + break; + case STEP6: /* W+, V- */ + APT_ForcePwmByApt(aptU, APT_CHA_LOW_CHB_HIGH); + APT_ForcePwmByApt(aptV, APT_CHA_LOW_CHB_LOW); + APT_ForcePwmByApt(aptW, APT_CHA_PWM_CHB_LOW); + break; + default: + break; + } +} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/func/mcs_six_step.h b/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/func/mcs_six_step.h new file mode 100644 index 00000000..08198c4c --- /dev/null +++ b/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/func/mcs_six_step.h @@ -0,0 +1,79 @@ +/** + * @ Copyright (c) HiSilicon (Shanghai) Technologies Co., Ltd. 2022-2023. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_six_step.c + * @author MCU Algorithm Team + * @brief The header file contains the following declaration: + * + StepEnum enum definition. + * + PhaseEnum enum definition. + * + UVW_AptReg structure definition. + * + SixStepHandle handle structure definition. + * + AptOutCombination structure definition. + * + Six Step Pwm Function And Six Step Pwm Overlap Function. + */ + +#ifndef MCS_SIX_STEP_H +#define MCS_SIX_STEP_H + +#include "apt.h" + +typedef enum { + STEP1 = 0, + STEP2, + STEP3, + STEP4, + STEP5, + STEP6, + STEP_MAX_NUM +} StepEnum; + +typedef enum { + U = 0, + V, + W, + PHASE_MAX_NUMS +} PhaseEnum; + +typedef enum { + APT_CHA_PWM_CHB_LOW, + APT_CHA_LOW_CHB_HIGH, + APT_CHA_LOW_CHB_LOW +} APT_Act; + +/** + * @brief Three-phase static coordinate frame variable. + */ +typedef struct { + APT_Handle *u; /**< Apt handle for controlling phase U. */ + APT_Handle *v; /**< Apt handle for controlling phase V. */ + APT_Handle *w; /**< Apt handle for controlling phase W. */ +} UVW_AptReg; + +typedef struct { + unsigned char phaseStep; /* Current step. */ + UVW_AptReg controlApt; /* Apt handles for controlling H bridge. */ +} SixStepHandle; + +typedef struct { + unsigned int upperout; /* High tube level */ + unsigned int upperstate; /* High tube state */ + unsigned int lowerout; /* Low tube level */ + unsigned int lowerstate; /* Low tube state */ +} AptOutCombination; + +void SixStepPwm(const SixStepHandle *handle); +#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/func/mcs_status.h b/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/func/mcs_status.h new file mode 100644 index 00000000..d92ed312 --- /dev/null +++ b/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/func/mcs_status.h @@ -0,0 +1,188 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_status.h + * @author MCU Algorithm Team + * @brief This file provides functions declaration of system status. + */ + +/* Define to prevent recursive inclusion ------------------------------------------------------- */ +#ifndef McuMagicTag_MCS_STATUS_H +#define McuMagicTag_MCS_STATUS_H + +/* Includes ------------------------------------------------------------------------------------ */ +#include "typedefs.h" +#include "mcs_assert.h" + +/* Typedef definitions ------------------------------------------------------------------------- */ +/** + * @brief System status define + */ +typedef union { + unsigned short all; + struct { + unsigned short cmdStart : 1; /**< Indicates that a start system command has been received. */ + unsigned short cmdStop : 1; /**< Indicates that a stop system command has been received. */ + unsigned short isRunning : 1; /**< Indicates that the system is running (enable signal) */ + unsigned short sysError : 1; /**< Indicates that the system reports an error. */ + unsigned short poweron : 1; /**< Indicates that the power-on initialization phase is complete. */ + unsigned short capcharge : 1; /**< Indicates that the bootstrap capacitor charging phase is complete. */ + unsigned short adczero : 1; /**< The current sampling point is reset to zero after power-on. */ + } BIT; +} SysStatusReg; + +/** + * @brief Get status of BIT cmdStart. + * @param sysStatus System status register handle. + * @retval Status of BIT cmdStart. + */ +static inline bool SysGetCmdStart(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + bool ret; + ret = (sysStatus->BIT.cmdStart == 1) ? true : false; + return ret; +} + +/** + * @brief Set BIT cmdStart. + * @param sysStatus System status register handle. + * @retval None. + */ +static inline void SysCmdStartSet(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + sysStatus->BIT.cmdStart = 1; +} + +/** + * @brief Clear BIT cmdStart. + * @param handle System status register handle. + * @retval None. + */ +static inline void SysCmdStartClr(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + sysStatus->BIT.cmdStart = 0; +} + +/** + * @brief Get status of BIT cmdStop. + * @param sysStatus System status register handle. + * @retval Status of BIT cmdStart. + */ +static inline bool SysGetCmdStop(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + bool ret; + ret = (sysStatus->BIT.cmdStop == 1) ? true : false; + return ret; +} + +/** + * @brief Set BIT cmdStop. + * @param sysStatus System status register handle. + * @retval None. + */ +static inline void SysCmdStopSet(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + sysStatus->BIT.cmdStop = 1; +} + +/** + * @brief Clear BIT cmdStop. + * @param sysStatus System status register handle. + * @retval None. + */ +static inline void SysCmdStopClr(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + sysStatus->BIT.cmdStop = 0; +} + +/** + * @brief Get status of BIT isRunning. + * @param sysStatus System status register handle. + * @retval Status of BIT isRunning. + */ +static inline bool SysIsRunning(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + bool ret; + ret = (sysStatus->BIT.isRunning == 1) ? true : false; + return ret; +} + +/** + * @brief Set BIT isRuning. + * @param sysStatus System status register handle. + * @retval None. + */ +static inline void SysRunningSet(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + sysStatus->BIT.isRunning = 1; +} + +/** + * @brief Clear BIT isRuning. + * @param sysStatus System status register handle. + * @retval None. + */ +static inline void SysRunningClr(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + sysStatus->BIT.isRunning = 0; +} + +/** + * @brief Get status of BIT sysError. + * @param sysStatus System status register handle. + * @retval Status of BIT sysError. + */ +static inline bool SysIsError(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + bool ret; + ret = (sysStatus->BIT.sysError == 1) ? true : false; + return ret; +} + +/** + * @brief Set BIT sysError. + * @param sysStatus System status register handle. + * @retval None. + */ +static inline void SysErrorSet(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + sysStatus->BIT.sysError = 1; +} + +/** + * @brief Clear BIT sysError. + * @param sysStatus System status register handle. + * @retval None. + */ +static inline void SysErrorClr(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + sysStatus->BIT.sysError = 0; +} + +#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/readme.md b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/readme.md new file mode 100644 index 00000000..d692ed23 --- /dev/null +++ b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/readme.md @@ -0,0 +1,20 @@ +# GIM4310-10_FOC + +**【功能描述】** + +- 基于ECMCU105H/ECBMCU201MPC单板的单电机双电阻采样的无感Foc应用 + +**【环境要求】** + +- 所有单板电源改制为演示用的12V低压,电机选用GIM4310-10 +- 电机(黄绿蓝)相线分别对应功率板上U/V/W通道接口 + +**【配置方法】** + +- 将user文件夹替换原工程user文件夹即可 + +【操作说明】 + +- 点击开发板按键启动电机 + +- 开发板蓝色电位器,可以调节电机转动的快慢。(1-24HZ) \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/generatecode/feature.h b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/generatecode/feature.h new file mode 100644 index 00000000..fe14fc27 --- /dev/null +++ b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/generatecode/feature.h @@ -0,0 +1,94 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file feature.h + * @author MCU Driver Team + * @brief This file contains macro configurations related to the project. This file is generated by the IDE tool. + * @date 2025-04-17 15:09:50 + */ + +#ifndef McuMagicTag_FEATURE_H +#define McuMagicTag_FEATURE_H + +/* Macro definitions --------------------------------------------------------- */ +#define CHIP_3061MNPICA MACRO_ENABLE + +#define MACRO_ENABLE 1 +#define MACRO_DISABLE 0 + +/* Macro switch */ +#define BASE_DEFINE_USE_ASSERT MACRO_ENABLE +#ifndef FLASH_CRC_CONFIG +#define FLASH_CRC_CONFIG +#endif /* #ifndef FLASH_CRC_CONFIG */ +#define BASE_MATH_SINCOS_MIDDLE_TABLE MACRO_ENABLE /**< This macro is used to control the table type when the + BASE_MATH_GetSinCos() queries the table. When the value of + this macro is MACRO_ENABLE, the error value obtained by the + BASE_MATH_GetSinCos() is relatively small, and the return + value of the function may be greater than or less than the + actual value. When the value of this macro is MACRO_DISABLE, + the error value obtained by the BASE_MATH_GetSinCos() is + relatively large. However, in the range [0°, 180°) and + [180°, 360°), the return value of the function is either + greater than or less than the actual value. */ + +/* Peripheral module macro switch--------------------------------------------- */ +#define BOARD_DIM_NUM 1 /**< Number of dimming handle arrays. */ + +#define BOARD_KEY_NUM 10 /**< Number of key handle arrays. */ +#define BOARD_KEY_PRESS_ON GPIO_HIGH_LEVEL /**< GPIO status corresponding to long press valid. */ +#define BOARD_KEY_PRESS_OFF GPIO_LOW_LEVEL /**< GPIO status corresponding to short press valid. */ + +#define BOARD_LED_SEG_NUM 4 /**< Number of segments. */ +#define BOARD_LED_SEGMENT_ON GPIO_HIGH_LEVEL /**< GPIO level status corresponding to valid segments. */ +#define BOARD_LED_SEGMENT_OFF GPIO_LOW_LEVEL /**< GPIO level status corresponding to invalid segments. */ + +#define BOARD_MKEY_SCHEME_NUMBER BOARD_MKEY_SCHEME_NUMBER_ONE /**< Define the scheme to be adopted. */ +#define BOARD_MKEY_OUT_NUM 4 /**< Number of GPIO pins used as output during scanning. */ +#define BOARD_MKEY_IN_NUM 4 /**< Number of GPIO pins used as input during scanning. */ +#define BOARD_MKEY_OUT_PIN_VALID GPIO_LOW_LEVEL /**< GPIO level status corresponding to the valid \ + status of the output GPIO in the key matrix. */ +#define BOARD_MKEY_OUT_PIN_INVALID GPIO_HIGH_LEVEL /**< GPIO level status corresponding to the \ + invalid status of the output GPIO in the key matrix. */ +#define BOARD_MKEY_IN_PIN_VALID GPIO_LOW_LEVEL /**< Indicates the GPIO level corresponding to the \ + valid status of the input GPIO in the key matrix. */ +#define BOARD_MKEY_IN_PIN_INVALID GPIO_HIGH_LEVEL /**< Indicates the GPIO level corresponding to the \ + invalid status of the input GPIO in the key matrix. */ + +#define BOARD_PULSES_NUM 2 /**< Number of pulse handles. */ + +#define BASE_DEFINE_SLIPAVERAGE_NUM 2 /**< Sliding average array length. */ + +#define LISTNODE_MAX 20 + +#define BASE_DEFINE_DMA_QUICKSTART + +#define XTRAIL_FREQ 30000000U + +#define DBG_USE_NO_PRINTF 0U +#define DBG_USE_UART_PRINTF 1U + +#define DBG_PRINTF_USE DBG_USE_UART_PRINTF +#if (DBG_PRINTF_USE == DBG_USE_UART_PRINTF) +#define DBG_PRINTF_UART_PORT UART0 +#endif + +/* USER CODE BEGIN 0 */ +/* USER CODE 区域内代码不会被覆盖,区域外会被生成的默认代码覆盖(其余USER CODE 区域同理) */ +/* USER CODE END 0 */ + +#endif /* McuMagicTag_FEATURE_H */ \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/generatecode/main.h b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/generatecode/main.h new file mode 100644 index 00000000..27f92d07 --- /dev/null +++ b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/generatecode/main.h @@ -0,0 +1,98 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file main.h + * @author MCU Driver Team + * @brief This file contains driver init functions. + * @date 2025-04-17 15:09:50 + */ + +/* Define to prevent recursive inclusion ------------------------------------- */ +#ifndef McuMagicTag_SYSTEM_INIT_H +#define McuMagicTag_SYSTEM_INIT_H + +#include "adc.h" +#include "adc_ex.h" +#include "acmp.h" +#include "acmp_ex.h" +#include "apt.h" +#include "uart.h" +#include "uart_ex.h" +#include "gpio.h" +#include "timer.h" +#include "timer_ex.h" +#include "pga.h" +#include "crg.h" +#include "dma.h" +#include "dma_ex.h" +#include "iocmg.h" + +#define IO_SPEED_FAST 0x00U +#define IO_SPEED_SLOW 0x01U + +#define IO_DRV_LEVEL4 0x00U +#define IO_DRV_LEVEL3 0x01U +#define IO_DRV_LEVEL2 0x02U +#define IO_DRV_LEVEL1 0x03U + +#define XTAL_DRV_LEVEL4 0x03U +#define XTAL_DRV_LEVEL3 0x02U +#define XTAL_DRV_LEVEL2 0x01U +#define XTAL_DRV_LEVEL1 0x00U + +#define GpioStartStop_PIN GPIO_PIN_4 +#define GpioStartStop_HANDLE g_gpio2 + +extern ACMP_Handle g_acmp0; +extern PGA_Handle g_pga0; +extern PGA_Handle g_pga1; +extern TIMER_Handle g_timer0; +extern TIMER_Handle g_timer1; +extern UART_Handle g_uart0; +extern APT_Handle g_apt0; +extern APT_Handle g_apt1; +extern APT_Handle g_apt2; +extern ADC_Handle g_adc0; + +extern DMA_Handle g_dmac; + +extern GPIO_Handle g_gpio2; +extern GPIO_Handle g_gpio1; + +BASE_StatusType CRG_Config(CRG_CoreClkSelect *coreClkSelect); +void SystemInit(void); + +void UART0WriteInterruptCallback(void *handle); +void UART0ReadInterruptCallback(void *handle); + +void UART0InterruptErrorCallback(void *handle); +void MotorStatemachineCallBack(void *handle); +void TIMER1_DMAOverFlow_InterruptProcess(void *handle); +void CheckPotentiometerValueCallback(void *handle); +void TIMER0_DMAOverFlow_InterruptProcess(void *handle); +void MotorCarrierProcessCallback(void *aptHandle); +void MotorSysErrCallback(void *aptHandle); + +void UART0_TXDMACallback(void *handle); + +void MotorStartStopKeyCallback(void *param); + +/* USER CODE BEGIN 0 */ +/* USER CODE 区域内代码不会被覆盖,区域外会被生成的默认代码覆盖(其余USER CODE 区域同理) */ +/* USER CODE END 0 */ + +#endif /* McuMagicTag_SYSTEM_INIT_H */ \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/generatecode/system_init.c b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/generatecode/system_init.c new file mode 100644 index 00000000..4ebd5b96 --- /dev/null +++ b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/generatecode/system_init.c @@ -0,0 +1,677 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file system_init.c + * @author MCU Driver Team + * @brief This file contains driver init functions. + * @date 2025-04-17 15:09:50 + */ + +#include "main.h" +#include "ioconfig.h" +#include "iocmg_ip.h" + +#define UART0_BAUD_RATE 1843200 + +BASE_StatusType CRG_Config(CRG_CoreClkSelect *coreClkSelect) +{ + CRG_Handle crg; + crg.baseAddress = CRG; + crg.pllRefClkSelect = CRG_PLL_REF_CLK_SELECT_HOSC; + crg.pllPreDiv = CRG_PLL_PREDIV_4; + crg.pllFbDiv = 48; /* PLL Multiplier 48 */ + crg.pllPostDiv = CRG_PLL_POSTDIV_2; + crg.coreClkSelect = CRG_CORE_CLK_SELECT_PLL; + crg.handleEx.pllPostDiv2 = CRG_PLL_POSTDIV2_3; + crg.handleEx.clk1MSelect = CRG_1M_CLK_SELECT_HOSC; + crg.handleEx.clk1MDiv = (25 - 1); /* The 1 MHz freq is equal to the input clock frequency / (clk_1m_div + 1). 25 is the div of the clk_1m in CLOCK. */ + + if (HAL_CRG_Init(&crg) != BASE_STATUS_OK) { + return BASE_STATUS_ERROR; + } + *coreClkSelect = crg.coreClkSelect; + return BASE_STATUS_OK; +} + +static void DMA_Channel0Init(void *handle) +{ + DMA_ChannelParam dma_param; + dma_param.direction = DMA_MEMORY_TO_PERIPH_BY_DMAC; + dma_param.srcAddrInc = DMA_ADDR_INCREASE; + dma_param.destAddrInc = DMA_ADDR_UNALTERED; + dma_param.srcPeriph = DMA_REQUEST_MEM; + dma_param.destPeriph = DMA_REQUEST_UART0_TX; + dma_param.srcWidth = DMA_TRANSWIDTH_BYTE; + dma_param.destWidth = DMA_TRANSWIDTH_BYTE; + dma_param.srcBurst = DMA_BURST_LENGTH_1; + dma_param.destBurst = DMA_BURST_LENGTH_1; + dma_param.pHandle = handle; + HAL_DMA_InitChannel(&g_dmac, &dma_param, DMA_CHANNEL_ZERO); +} + +static void DMA_Init(void) +{ + HAL_CRG_IpEnableSet(DMA_BASE, IP_CLK_ENABLE); + g_dmac.baseAddress = DMA; + IRQ_Register(IRQ_DMA_TC, HAL_DMA_IrqHandlerTc, &g_dmac); + IRQ_Register(IRQ_DMA_ERR, HAL_DMA_IrqHandlerError, &g_dmac); + IRQ_EnableN(IRQ_DMA_TC); + IRQ_EnableN(IRQ_DMA_ERR); + HAL_DMA_Init(&g_dmac); + DMA_Channel0Init((void *)(&g_uart0)); + HAL_DMA_SetChannelPriorityEx(&g_dmac, DMA_CHANNEL_ZERO, DMA_PRIORITY_HIGHEST); +} + +static void ACMP0_Init(void) +{ + HAL_CRG_IpEnableSet(ACMP0_BASE, IP_CLK_ENABLE); /* ACMP clock bit reset. */ + g_acmp0.baseAddress = ACMP0_BASE; + g_acmp0.inOutConfig.inputNNum = ACMP_INPUT_N_SELECT2; + g_acmp0.inOutConfig.inputPNum = ACMP_INPUT_P_SELECT2; + g_acmp0.inOutConfig.polarity = ACMP_OUT_NOT_INVERT; + g_acmp0.filterCtrl.filterMode = ACMP_FILTER_NONE; + g_acmp0.hysteresisVol = ACMP_HYS_VOL_30MV; + g_acmp0.interruptEn = BASE_CFG_UNSET; + HAL_ACMP_Init(&g_acmp0); +} + +static void ADC0_Init(void) +{ + HAL_CRG_IpEnableSet(ADC0_BASE, IP_CLK_ENABLE); + HAL_CRG_IpClkSelectSet(ADC0_BASE, CRG_ADC_CLK_ASYN_PLL_DIV); + HAL_CRG_IpClkDivSet(ADC0_BASE, CRG_ADC_DIV_1); + + g_adc0.baseAddress = ADC0; + g_adc0.socPriority = ADC_PRIMODE_ALL_ROUND; + + HAL_ADC_Init(&g_adc0); + + SOC_Param socParam = {0}; + socParam.adcInput = ADC_CH_ADCINA0; /* PGA0_OUT(ADC AIN0) */ + socParam.sampleTotalTime = ADC_SOCSAMPLE_22CLK; /* adc sample total time 22 adc_clk */ + socParam.trigSource = ADC_TRIGSOC_APT0_SOCA; + socParam.continueMode = BASE_CFG_DISABLE; + socParam.finishMode = ADC_SOCFINISH_NONE; + HAL_ADC_ConfigureSoc(&g_adc0, ADC_SOC_NUM1, &socParam); + + socParam.adcInput = ADC_CH_ADCINA1; /* PGA1_OUT(ADC AIN1) */ + socParam.sampleTotalTime = ADC_SOCSAMPLE_22CLK; /* adc sample total time 22 adc_clk */ + socParam.trigSource = ADC_TRIGSOC_APT0_SOCA; + socParam.continueMode = BASE_CFG_DISABLE; + socParam.finishMode = ADC_SOCFINISH_NONE; + HAL_ADC_ConfigureSoc(&g_adc0, ADC_SOC_NUM6, &socParam); + + socParam.adcInput = ADC_CH_ADCINA9; /* PIN7(ADC AIN9) */ + socParam.sampleTotalTime = ADC_SOCSAMPLE_22CLK; /* adc sample total time 22 adc_clk */ + socParam.trigSource = ADC_TRIGSOC_SOFT; + socParam.continueMode = BASE_CFG_DISABLE; + socParam.finishMode = ADC_SOCFINISH_NONE; + HAL_ADC_ConfigureSoc(&g_adc0, ADC_SOC_NUM0, &socParam); + + socParam.adcInput = ADC_CH_ADCINA5; /* PIN2(ADC AIN5) */ + socParam.sampleTotalTime = ADC_SOCSAMPLE_22CLK; /* adc sample total time 22 adc_clk */ + socParam.trigSource = ADC_TRIGSOC_SOFT; + socParam.continueMode = BASE_CFG_DISABLE; + socParam.finishMode = ADC_SOCFINISH_NONE; + HAL_ADC_ConfigureSoc(&g_adc0, ADC_SOC_NUM3, &socParam); + + socParam.adcInput = ADC_CH_ADCINA15; /* PIN14(ADC AIN15) */ + socParam.sampleTotalTime = ADC_SOCSAMPLE_22CLK; /* adc sample total time 22 adc_clk */ + socParam.trigSource = ADC_TRIGSOC_SOFT; + socParam.continueMode = BASE_CFG_DISABLE; + socParam.finishMode = ADC_SOCFINISH_NONE; + HAL_ADC_ConfigureSoc(&g_adc0, ADC_SOC_NUM4, &socParam); +} + +__weak void MotorSysErrCallback(void *aptHandle) +{ + BASE_FUNC_UNUSED(aptHandle); + /* USER CODE BEGIN APT0_EVENT_INTERRUPT */ + /* USER CODE END APT0_EVENT_INTERRUPT */ +} + +__weak void MotorCarrierProcessCallback(void *aptHandle) +{ + BASE_FUNC_UNUSED(aptHandle); + /* USER CODE BEGIN APT0_TIMER_INTERRUPT */ + /* USER CODE END APT0_TIMER_INTERRUPT */ +} + +static void APT0_ProtectInit(void) +{ + APT_OutCtrlProtectEx protectApt = {0}; + protectApt.ocEventEnEx = BASE_CFG_ENABLE; + protectApt.ocEventModeEx = APT_OUT_CTRL_ONE_SHOT; + protectApt.ocActionEx = APT_OUT_CTRL_ACTION_LOW; + protectApt.ocActionBEx = APT_OUT_CTRL_ACTION_LOW; + protectApt.ocEvtInterruptEnEx = BASE_CFG_ENABLE; + protectApt.ocSysEvent = APT_SYS_EVT_DEBUG | APT_SYS_EVT_CLK | APT_SYS_EVT_MEM; + protectApt.originalEvtEx = APT_EM_ORIGINAL_SRC_ACMP0; + protectApt.evtPolarityMaskEx = APT_EM_ACMP0_INVERT_BIT; + protectApt.filterCycleNumEx = 0; + HAL_APT_ProtectInitEx(&g_apt0, &protectApt); +} + +static void APT0_Init(void) +{ + HAL_CRG_IpEnableSet(APT0_BASE, IP_CLK_ENABLE); + + g_apt0.baseAddress = APT0; + + /* Clock Settings */ + g_apt0.waveform.dividerFactor = 1 - 1; + /* Timer Settings */ + g_apt0.waveform.timerPeriod = 7500; /* 7500 is count period of APT time-base timer */ + g_apt0.waveform.cntMode = APT_COUNT_MODE_UP_DOWN; + + /* Wave Form */ + g_apt0.waveform.basicType = APT_PWM_BASIC_A_HIGH_B_LOW; + g_apt0.waveform.chAOutType = APT_PWM_OUT_BASIC_TYPE; + g_apt0.waveform.chBOutType = APT_PWM_OUT_BASIC_TYPE; + g_apt0.waveform.divInitVal = 0; + g_apt0.waveform.cntInitVal = 0; + g_apt0.waveform.cntCmpLeftEdge = 500; /* 500 is count compare point of the left edge of PWM waveform */ + g_apt0.waveform.cntCmpRightEdge = 4000; /* 4000 is count compare point of the right edge of PWM waveform */ + g_apt0.waveform.cntCmpLoadMode = APT_BUFFER_INDEPENDENT_LOAD; + g_apt0.waveform.cntCmpLoadEvt = APT_COMPARE_LOAD_EVENT_ZERO; + g_apt0.waveform.deadBandCnt = 225; /* 225 is dead-band value */ + + /* ADC Trigger SOCA */ + g_apt0.adcTrg.trgEnSOCA = BASE_CFG_ENABLE; + g_apt0.adcTrg.cntCmpSOCA = 375; /* 375 is count compare point of ADC trigger source SOCA when using CMPA */ + g_apt0.adcTrg.trgSrcSOCA = APT_CS_SRC_CNTR_CMPA_DOWN; + g_apt0.adcTrg.trgScaleSOCA = 1; + + /* ADC Trigger SOCB */ + g_apt0.adcTrg.trgEnSOCB = BASE_CFG_ENABLE; + g_apt0.adcTrg.cntCmpSOCB = 1; + g_apt0.adcTrg.trgSrcSOCB = APT_CS_SRC_CNTR_CMPB_DOWN; + g_apt0.adcTrg.trgScaleSOCB = 1; + + g_apt0.adcTrg.cntCmpLoadMode = APT_BUFFER_INDEPENDENT_LOAD; + g_apt0.adcTrg.cntCmpLoadEvt = APT_COMPARE_LOAD_EVENT_ZERO; + + /* Timer Trigger */ + g_apt0.tmrInterrupt.tmrInterruptEn = BASE_CFG_ENABLE; + g_apt0.tmrInterrupt.tmrInterruptSrc = APT_INT_SRC_CNTR_ZERO; + g_apt0.tmrInterrupt.tmrInterruptScale = 1; + + APT0_ProtectInit(); + + HAL_APT_PWMInit(&g_apt0); + HAL_APT_RegisterCallBack(&g_apt0, APT_EVENT_INTERRUPT, MotorSysErrCallback); + IRQ_SetPriority(IRQ_APT0_EVT, 7); /* 7 is priority value */ + IRQ_Register(IRQ_APT0_EVT, HAL_APT_EventIrqHandler, &g_apt0); + IRQ_EnableN(IRQ_APT0_EVT); + HAL_APT_RegisterCallBack(&g_apt0, APT_TIMER_INTERRUPT, MotorCarrierProcessCallback); + IRQ_SetPriority(IRQ_APT0_TMR, 5); /* 5 is priority value */ + IRQ_Register(IRQ_APT0_TMR, HAL_APT_TimerIrqHandler, &g_apt0); + IRQ_EnableN(IRQ_APT0_TMR); +} + +static void APT1_ProtectInit(void) +{ + APT_OutCtrlProtectEx protectApt = {0}; + protectApt.ocEventEnEx = BASE_CFG_ENABLE; + protectApt.ocEventModeEx = APT_OUT_CTRL_ONE_SHOT; + protectApt.ocActionEx = APT_OUT_CTRL_ACTION_LOW; + protectApt.ocActionBEx = APT_OUT_CTRL_ACTION_LOW; + protectApt.ocEvtInterruptEnEx = BASE_CFG_DISABLE; + protectApt.ocSysEvent = APT_SYS_EVT_DEBUG | APT_SYS_EVT_CLK | APT_SYS_EVT_MEM; + protectApt.originalEvtEx = APT_EM_ORIGINAL_SRC_ACMP0; + protectApt.evtPolarityMaskEx = APT_EM_ACMP0_INVERT_BIT; + protectApt.filterCycleNumEx = 0; + HAL_APT_ProtectInitEx(&g_apt1, &protectApt); +} + +static void APT1_Init(void) +{ + HAL_CRG_IpEnableSet(APT1_BASE, IP_CLK_ENABLE); + + g_apt1.baseAddress = APT1; + + /* Clock Settings */ + g_apt1.waveform.dividerFactor = 1 - 1; + /* Timer Settings */ + g_apt1.waveform.timerPeriod = 7500; /* 7500 is count period of APT time-base timer */ + g_apt1.waveform.cntMode = APT_COUNT_MODE_UP_DOWN; + + /* Wave Form */ + g_apt1.waveform.basicType = APT_PWM_BASIC_A_HIGH_B_LOW; + g_apt1.waveform.chAOutType = APT_PWM_OUT_BASIC_TYPE; + g_apt1.waveform.chBOutType = APT_PWM_OUT_BASIC_TYPE; + g_apt1.waveform.divInitVal = 0; + g_apt1.waveform.cntInitVal = 0; + g_apt1.waveform.cntCmpLeftEdge = 500; /* 500 is count compare point of the left edge of PWM waveform */ + g_apt1.waveform.cntCmpRightEdge = 4000; /* 4000 is count compare point of the right edge of PWM waveform */ + g_apt1.waveform.cntCmpLoadMode = APT_BUFFER_INDEPENDENT_LOAD; + g_apt1.waveform.cntCmpLoadEvt = APT_COMPARE_LOAD_EVENT_ZERO; + g_apt1.waveform.deadBandCnt = 225; /* 225 is dead-band value */ + + APT1_ProtectInit(); + + HAL_APT_PWMInit(&g_apt1); +} + +static void APT2_ProtectInit(void) +{ + APT_OutCtrlProtectEx protectApt = {0}; + protectApt.ocEventEnEx = BASE_CFG_ENABLE; + protectApt.ocEventModeEx = APT_OUT_CTRL_ONE_SHOT; + protectApt.ocActionEx = APT_OUT_CTRL_ACTION_LOW; + protectApt.ocActionBEx = APT_OUT_CTRL_ACTION_LOW; + protectApt.ocEvtInterruptEnEx = BASE_CFG_DISABLE; + protectApt.ocSysEvent = APT_SYS_EVT_DEBUG | APT_SYS_EVT_CLK | APT_SYS_EVT_MEM; + protectApt.originalEvtEx = APT_EM_ORIGINAL_SRC_ACMP0; + protectApt.evtPolarityMaskEx = APT_EM_ACMP0_INVERT_BIT; + protectApt.filterCycleNumEx = 0; + HAL_APT_ProtectInitEx(&g_apt2, &protectApt); +} + +static void APT2_Init(void) +{ + HAL_CRG_IpEnableSet(APT2_BASE, IP_CLK_ENABLE); + + g_apt2.baseAddress = APT2; + + /* Clock Settings */ + g_apt2.waveform.dividerFactor = 1 - 1; + /* Timer Settings */ + g_apt2.waveform.timerPeriod = 7500; /* 7500 is count period of APT time-base timer */ + g_apt2.waveform.cntMode = APT_COUNT_MODE_UP_DOWN; + + /* Wave Form */ + g_apt2.waveform.basicType = APT_PWM_BASIC_A_HIGH_B_LOW; + g_apt2.waveform.chAOutType = APT_PWM_OUT_BASIC_TYPE; + g_apt2.waveform.chBOutType = APT_PWM_OUT_BASIC_TYPE; + g_apt2.waveform.divInitVal = 0; + g_apt2.waveform.cntInitVal = 0; + g_apt2.waveform.cntCmpLeftEdge = 500; /* 500 is count compare point of the left edge of PWM waveform */ + g_apt2.waveform.cntCmpRightEdge = 4000; /* 4000 is count compare point of the right edge of PWM waveform */ + g_apt2.waveform.cntCmpLoadMode = APT_BUFFER_INDEPENDENT_LOAD; + g_apt2.waveform.cntCmpLoadEvt = APT_COMPARE_LOAD_EVENT_ZERO; + g_apt2.waveform.deadBandCnt = 225; /* 225 is dead-band value */ + + APT2_ProtectInit(); + + HAL_APT_PWMInit(&g_apt2); +} + +__weak void MotorStartStopKeyCallback(void *param) +{ + GPIO_Handle *handle = (GPIO_Handle *)param; + BASE_FUNC_UNUSED(handle); +} + +static void GPIO_Init(void) +{ + HAL_CRG_IpEnableSet(GPIO2_BASE, IP_CLK_ENABLE); + g_gpio2.baseAddress = GPIO2; + + g_gpio2.pins = GPIO_PIN_3; + HAL_GPIO_Init(&g_gpio2); + HAL_GPIO_SetDirection(&g_gpio2, g_gpio2.pins, GPIO_OUTPUT_MODE); + HAL_GPIO_SetValue(&g_gpio2, g_gpio2.pins, GPIO_HIGH_LEVEL); + HAL_GPIO_SetIrqType(&g_gpio2, g_gpio2.pins, GPIO_INT_TYPE_NONE); + + g_gpio2.pins = GPIO_PIN_4; + HAL_GPIO_Init(&g_gpio2); + HAL_GPIO_SetDirection(&g_gpio2, g_gpio2.pins, GPIO_INPUT_MODE); + HAL_GPIO_SetValue(&g_gpio2, g_gpio2.pins, GPIO_HIGH_LEVEL); + HAL_GPIO_SetIrqType(&g_gpio2, g_gpio2.pins, GPIO_INT_TYPE_LOW_LEVEL); + + HAL_CRG_IpEnableSet(GPIO1_BASE, IP_CLK_ENABLE); + g_gpio1.baseAddress = GPIO1; + + g_gpio1.pins = GPIO_PIN_0; + HAL_GPIO_Init(&g_gpio1); + HAL_GPIO_SetDirection(&g_gpio1, g_gpio1.pins, GPIO_OUTPUT_MODE); + HAL_GPIO_SetValue(&g_gpio1, g_gpio1.pins, GPIO_HIGH_LEVEL); + HAL_GPIO_SetIrqType(&g_gpio1, g_gpio1.pins, GPIO_INT_TYPE_NONE); + + HAL_GPIO_RegisterCallBack(&g_gpio2, GPIO_PIN_4, MotorStartStopKeyCallback); + IRQ_Register(IRQ_GPIO2, HAL_GPIO_IrqHandler, &g_gpio2); + IRQ_SetPriority(IRQ_GPIO2, 1); /* set gpio1 interrupt priority to 1, 1~15. 1 is priority value */ + IRQ_EnableN(IRQ_GPIO2); /* gpio interrupt enable */ + + return; +} + +static void PGA0_Init(void) +{ + HAL_CRG_IpEnableSet(PGA0_BASE, IP_CLK_ENABLE); + + g_pga0.baseAddress = PGA0_BASE; + g_pga0.externalResistorMode = BASE_CFG_ENABLE; + g_pga0.handleEx.extCapCompensation = PGA_EXT_COMPENSATION_2X; + HAL_PGA_Init(&g_pga0); +} + +static void PGA1_Init(void) +{ + HAL_CRG_IpEnableSet(PGA1_BASE, IP_CLK_ENABLE); + + g_pga1.baseAddress = PGA1_BASE; + g_pga1.externalResistorMode = BASE_CFG_ENABLE; + g_pga1.handleEx.extCapCompensation = PGA_EXT_COMPENSATION_2X; + HAL_PGA_Init(&g_pga1); +} + +__weak void CheckPotentiometerValueCallback(void *handle) +{ + BASE_FUNC_UNUSED(handle); + /* USER CODE BEGIN CheckPotentiometerValueCallback */ + /* USER CODE END CheckPotentiometerValueCallback */ +} + +static void TIMER0_Init(void) +{ + HAL_CRG_IpEnableSet(TIMER0_BASE, IP_CLK_ENABLE); /* TIMER0 clock enable. */ + unsigned int load = ((float)HAL_CRG_GetIpFreq((void *)TIMER0) / (1u << (TIMERPRESCALER_NO_DIV * 4)) / 1000000u) * 1000000; + + g_timer0.baseAddress = TIMER0; + g_timer0.load = (load < 1) ? 0 : (load - 1); /* Set timer value immediately */ + g_timer0.bgLoad = (load < 1) ? 0 : (load - 1); /* Set timer value */ + g_timer0.mode = TIMER_MODE_RUN_PERIODIC; /* Run in period mode */ + g_timer0.prescaler = TIMERPRESCALER_NO_DIV; /* Don't frequency division */ + g_timer0.size = TIMER_SIZE_32BIT; /* 1 for 32bit, 0 for 16bit */ + g_timer0.interruptEn = BASE_CFG_ENABLE; + g_timer0.adcSocReqEnable = BASE_CFG_DISABLE; + g_timer0.dmaReqEnable = BASE_CFG_DISABLE; + HAL_TIMER_Init(&g_timer0); + IRQ_Register(IRQ_TIMER0, HAL_TIMER_IrqHandler, &g_timer0); + + HAL_TIMER_RegisterCallback(&g_timer0, TIMER_PERIOD_FIN, CheckPotentiometerValueCallback); + IRQ_SetPriority(IRQ_TIMER0, 1); /* 1 is priority value */ + IRQ_EnableN(IRQ_TIMER0); +} + +__weak void MotorStatemachineCallBack(void *handle) +{ + BASE_FUNC_UNUSED(handle); + /* USER CODE BEGIN MotorStatemachineCallBack */ + /* USER CODE END MotorStatemachineCallBack */ +} + +static void TIMER1_Init(void) +{ + HAL_CRG_IpEnableSet(TIMER1_BASE, IP_CLK_ENABLE); /* TIMER1 clock enable. */ + unsigned int load = ((float)HAL_CRG_GetIpFreq((void *)TIMER1) / (1u << (TIMERPRESCALER_NO_DIV * 4)) / 1000000u) * 500; + + g_timer1.baseAddress = TIMER1; + g_timer1.load = (load < 1) ? 0 : (load - 1); /* Set timer value immediately */ + g_timer1.bgLoad = (load < 1) ? 0 : (load - 1); /* Set timer value */ + g_timer1.mode = TIMER_MODE_RUN_PERIODIC; /* Run in period mode */ + g_timer1.prescaler = TIMERPRESCALER_NO_DIV; /* Don't frequency division */ + g_timer1.size = TIMER_SIZE_32BIT; /* 1 for 32bit, 0 for 16bit */ + g_timer1.interruptEn = BASE_CFG_ENABLE; + g_timer1.adcSocReqEnable = BASE_CFG_DISABLE; + g_timer1.dmaReqEnable = BASE_CFG_DISABLE; + HAL_TIMER_Init(&g_timer1); + IRQ_Register(IRQ_TIMER1, HAL_TIMER_IrqHandler, &g_timer1); + + HAL_TIMER_RegisterCallback(&g_timer1, TIMER_PERIOD_FIN, MotorStatemachineCallBack); + IRQ_SetPriority(IRQ_TIMER1, 1); /* 1 is priority value */ + IRQ_EnableN(IRQ_TIMER1); +} + +__weak void UART0InterruptErrorCallback(void *handle) +{ + BASE_FUNC_UNUSED(handle); + /* USER CODE BEGIN UART0_TRNS_IT_ERROR */ + /* USER CODE END UART0_TRNS_IT_ERROR */ +} + +__weak void UART0_TXDMACallback(void *handle) +{ + BASE_FUNC_UNUSED(handle); + /* USER CODE BEGIN UART0_WRITE_DMA_FINISH */ + /* USER CODE END UART0_WRITE_DMA_FINISH */ +} + +__weak void UART0WriteInterruptCallback(void *handle) +{ + BASE_FUNC_UNUSED(handle); + /* USER CODE BEGIN UART0_WRITE_IT_FINISH */ + /* USER CODE END UART0_WRITE_IT_FINISH */ +} + +__weak void UART0ReadInterruptCallback(void *handle) +{ + BASE_FUNC_UNUSED(handle); + /* USER CODE BEGIN UART0_READ_IT_FINISH */ + /* USER CODE END UART0_READ_IT_FINISH */ +} + +static void UART0_Init(void) +{ + HAL_CRG_IpEnableSet(UART0_BASE, IP_CLK_ENABLE); /* UART0 clock enable. */ + g_uart0.baseAddress = UART0; + + g_uart0.baudRate = UART0_BAUD_RATE; + g_uart0.dataLength = UART_DATALENGTH_8BIT; + g_uart0.stopBits = UART_STOPBITS_ONE; + g_uart0.parity = UART_PARITY_NONE; + g_uart0.txMode = UART_MODE_DMA; + g_uart0.rxMode = UART_MODE_INTERRUPT; + g_uart0.fifoMode = BASE_CFG_ENABLE; + g_uart0.fifoTxThr = UART_FIFODEPTH_SIZE8; + g_uart0.fifoRxThr = UART_FIFODEPTH_SIZE8; + g_uart0.hwFlowCtr = BASE_CFG_DISABLE; + g_uart0.handleEx.overSampleMultiple = UART_OVERSAMPLING_16X; + g_uart0.handleEx.msbFirst = BASE_CFG_DISABLE; + HAL_UART_Init(&g_uart0); + HAL_UART_RegisterCallBack(&g_uart0, UART_TRNS_IT_ERROR, (UART_CallbackType)UART0InterruptErrorCallback); + HAL_UART_RegisterCallBack(&g_uart0, UART_READ_IT_FINISH, (UART_CallbackType)UART0ReadInterruptCallback); + IRQ_Register(IRQ_UART0, HAL_UART_IrqHandler, &g_uart0); + IRQ_SetPriority(IRQ_UART0, 6); /* 6 is priority value */ + IRQ_EnableN(IRQ_UART0); + g_uart0.dmaHandle = &g_dmac; + g_uart0.uartDmaTxChn = 0; + HAL_UART_RegisterCallBack(&g_uart0, UART_WRITE_DMA_FINISH, (UART_CallbackType)UART0_TXDMACallback); + HAL_UART_RegisterCallBack(&g_uart0, UART_WRITE_IT_FINISH, (UART_CallbackType)UART0WriteInterruptCallback); +} + +static void IOConfig(void) +{ + SYSCTRL0->SC_SYS_STAT.BIT.update_mode = 0; + SYSCTRL0->SC_SYS_STAT.BIT.update_mode_clear = 1; + /* Config PIN39 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO0_3_AS_UART0_TXD); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO0_3_AS_UART0_TXD, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO0_3_AS_UART0_TXD, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO0_3_AS_UART0_TXD, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO0_3_AS_UART0_TXD, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN40 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO0_4_AS_UART0_RXD); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO0_4_AS_UART0_RXD, PULL_UP); /* Pull-up and Pull-down, UART RX recommend PULL_UP */ + HAL_IOCMG_SetPinSchmidtMode(GPIO0_4_AS_UART0_RXD, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO0_4_AS_UART0_RXD, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO0_4_AS_UART0_RXD, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN1 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO0_7_AS_ACMP0_OUT); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO0_7_AS_ACMP0_OUT, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO0_7_AS_ACMP0_OUT, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO0_7_AS_ACMP0_OUT, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO0_7_AS_ACMP0_OUT, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN48 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO0_6_AS_ACMP_N2); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO0_6_AS_ACMP_N2, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO0_6_AS_ACMP_N2, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO0_6_AS_ACMP_N2, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO0_6_AS_ACMP_N2, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN47 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO0_5_AS_ACMP_P2); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO0_5_AS_ACMP_P2, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO0_5_AS_ACMP_P2, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO0_5_AS_ACMP_P2, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO0_5_AS_ACMP_P2, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN4 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO2_6_AS_PGA0_N0); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO2_6_AS_PGA0_N0, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO2_6_AS_PGA0_N0, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO2_6_AS_PGA0_N0, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO2_6_AS_PGA0_N0, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN5 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO2_5_AS_PGA0_P0); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO2_5_AS_PGA0_P0, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO2_5_AS_PGA0_P0, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO2_5_AS_PGA0_P0, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO2_5_AS_PGA0_P0, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN3 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO2_7_AS_PGA0_OUT); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO2_7_AS_PGA0_OUT, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO2_7_AS_PGA0_OUT, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO2_7_AS_PGA0_OUT, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO2_7_AS_PGA0_OUT, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN7 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO3_7_AS_ADC_AIN9); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO3_7_AS_ADC_AIN9, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO3_7_AS_ADC_AIN9, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO3_7_AS_ADC_AIN9, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO3_7_AS_ADC_AIN9, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN2 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO5_1_AS_ADC_AIN5); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO5_1_AS_ADC_AIN5, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO5_1_AS_ADC_AIN5, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO5_1_AS_ADC_AIN5, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO5_1_AS_ADC_AIN5, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN14 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO4_7_AS_ADC_AIN15); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO4_7_AS_ADC_AIN15, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO4_7_AS_ADC_AIN15, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO4_7_AS_ADC_AIN15, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO4_7_AS_ADC_AIN15, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN11 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO1_5_AS_PGA1_P0); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO1_5_AS_PGA1_P0, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO1_5_AS_PGA1_P0, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO1_5_AS_PGA1_P0, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO1_5_AS_PGA1_P0, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN12 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO1_6_AS_PGA1_N0); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO1_6_AS_PGA1_N0, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO1_6_AS_PGA1_N0, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO1_6_AS_PGA1_N0, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO1_6_AS_PGA1_N0, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN13 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO1_7_AS_PGA1_OUT); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO1_7_AS_PGA1_OUT, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO1_7_AS_PGA1_OUT, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO1_7_AS_PGA1_OUT, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO1_7_AS_PGA1_OUT, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN35 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO2_3_AS_GPIO2_3); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO2_3_AS_GPIO2_3, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO2_3_AS_GPIO2_3, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO2_3_AS_GPIO2_3, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO2_3_AS_GPIO2_3, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN27 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO1_0_AS_GPIO1_0); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO1_0_AS_GPIO1_0, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO1_0_AS_GPIO1_0, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO1_0_AS_GPIO1_0, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO1_0_AS_GPIO1_0, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN41 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO2_4_AS_GPIO2_4); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO2_4_AS_GPIO2_4, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO2_4_AS_GPIO2_4, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO2_4_AS_GPIO2_4, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO2_4_AS_GPIO2_4, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN19 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO3_0_AS_APT0_PWMA); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO3_0_AS_APT0_PWMA, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO3_0_AS_APT0_PWMA, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO3_0_AS_APT0_PWMA, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO3_0_AS_APT0_PWMA, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN23 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO4_0_AS_APT0_PWMB); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO4_0_AS_APT0_PWMB, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO4_0_AS_APT0_PWMB, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO4_0_AS_APT0_PWMB, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO4_0_AS_APT0_PWMB, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN20 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO3_1_AS_APT1_PWMA); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO3_1_AS_APT1_PWMA, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO3_1_AS_APT1_PWMA, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO3_1_AS_APT1_PWMA, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO3_1_AS_APT1_PWMA, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN24 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO4_1_AS_APT1_PWMB); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO4_1_AS_APT1_PWMB, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO4_1_AS_APT1_PWMB, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO4_1_AS_APT1_PWMB, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO4_1_AS_APT1_PWMB, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN21 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO3_2_AS_APT2_PWMA); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO3_2_AS_APT2_PWMA, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO3_2_AS_APT2_PWMA, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO3_2_AS_APT2_PWMA, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO3_2_AS_APT2_PWMA, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN25 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO4_2_AS_APT2_PWMB); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO4_2_AS_APT2_PWMB, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO4_2_AS_APT2_PWMB, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO4_2_AS_APT2_PWMB, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO4_2_AS_APT2_PWMB, DRIVER_RATE_2); /* Output signal edge fast/slow */ +} + +static void APT_SyncMasterInit(void) +{ + HAL_APT_MasterSyncInit(&g_apt0, APT_SYNC_OUT_ON_CNTR_ZERO); +} + +static void APT_SyncSlaveInit(void) +{ + APT_SlaveSyncIn aptSlave; + + aptSlave.cntPhase = 0; /* counter phase value */ + aptSlave.syncCntMode = APT_COUNT_MODE_AFTER_SYNC_UP; + aptSlave.syncInSrc = APT_SYNCIN_SRC_APT0_SYNCOUT; /* sync source selection */ + aptSlave.cntrSyncSrc = APT_CNTR_SYNC_SRC_SYNCIN; + HAL_APT_SlaveSyncInit(&g_apt1, &aptSlave); + + aptSlave.cntPhase = 0; /* counter phase value */ + aptSlave.syncCntMode = APT_COUNT_MODE_AFTER_SYNC_UP; + aptSlave.syncInSrc = APT_SYNCIN_SRC_APT0_SYNCOUT; /* sync source selection */ + aptSlave.cntrSyncSrc = APT_CNTR_SYNC_SRC_SYNCIN; + HAL_APT_SlaveSyncInit(&g_apt2, &aptSlave); + +} + +void SystemInit(void) +{ + IOConfig(); + DMA_Init(); + UART0_Init(); + ACMP0_Init(); + APT0_Init(); + APT1_Init(); + APT2_Init(); + ADC0_Init(); + PGA0_Init(); + PGA1_Init(); + TIMER0_Init(); + TIMER1_Init(); + GPIO_Init(); + + APT_SyncMasterInit(); + APT_SyncSlaveInit(); + + /* USER CODE BEGIN system_init */ + /* USER CODE END system_init */ +} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/main.c b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/main.c new file mode 100644 index 00000000..f16312ae --- /dev/null +++ b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/main.c @@ -0,0 +1,71 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file main.c + * @author MCU Driver Team + * @brief Main program body. + * @date 2025-04-17 15:09:50 + */ + +#include "typedefs.h" +#include "feature.h" +#include "mcs_motor_process.h" +#include "main.h" +/* USER CODE BEGIN 0 */ +/* USER CODE 区域内代码不会被覆盖,区域外会被生成的默认代码覆盖(其余USER CODE 区域同理) */ +/* 建议用户放置头文件 */ +/* USER CODE END 0 */ +ACMP_Handle g_acmp0; +PGA_Handle g_pga0; +PGA_Handle g_pga1; +TIMER_Handle g_timer0; +TIMER_Handle g_timer1; +UART_Handle g_uart0; +APT_Handle g_apt0; +APT_Handle g_apt1; +APT_Handle g_apt2; +ADC_Handle g_adc0; +DMA_Handle g_dmac; +GPIO_Handle g_gpio2; +GPIO_Handle g_gpio1; +/* USER CODE BEGIN 1 */ +/* 建议用户定义全局变量、结构体、宏定义或函数声明等 */ +/* USER CODE END 1 */ + +int main(void) +{ + /* USER CODE BEGIN 2 */ + /* 建议用户放置初始化代码或启动代码等 */ + /* USER CODE END 2 */ + MotorMainProcess(); + /* USER CODE BEGIN 3 */ + /* 建议用户放置初始配置代码 */ + /* USER CODE END 3 */ + while (1) { + /* USER CODE BEGIN 4 */ + /* 建议用户放置周期性执行代码 */ + /* USER CODE END 4 */ + } + /* USER CODE BEGIN 5 */ + /* 建议用户放置代码流程 */ + /* USER CODE END 5 */ + return BASE_STATUS_OK; +} + +/* USER CODE BEGIN 6 */ +/* 建议用户放置自定义函数 */ +/* USER CODE END 6 */ \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_carrier.h b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_carrier.h new file mode 100644 index 00000000..dea3ad66 --- /dev/null +++ b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_carrier.h @@ -0,0 +1,142 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_carrier.h + * @author MCU Algorithm Team + * @brief This file provides functions declaration for carrier interrupt processing function. + */ +#ifndef McuMagicTag_MCS_CARRIER_H +#define McuMagicTag_MCS_CARRIER_H + +#include "mcs_status.h" +#include "mcs_mtr_param.h" +#include "mcs_svpwm.h" +#include "mcs_curr_ctrl.h" +#include "mcs_if_ctrl.h" +#include "mcs_ramp_mgmt.h" +#include "mcs_spd_ctrl.h" +#include "mcs_fosmo.h" +#include "mcs_smo_4th.h" +#include "mcs_pll.h" +#include "mcs_startup.h" +#include "mcs_r1_svpwm.h" +#include "mcs_fw_ctrl.h" +#include "mcs_prot_user.h" + +typedef void (*MCS_ReadCurrUvwCb)(UvwAxis *CurrUvw); +typedef void (*MCS_SetPwmDutyCb)(UvwAxis *dutyUvwLeft, UvwAxis *dutyUvwRight); +typedef void (*MCS_SetADCTriggerTimeCb)(unsigned short cntCmpSOCA, unsigned short cntCmpSOCB); + +/** + * @brief motor control FSM state define. + * @details motor control FSM state type: + * + FSM_IDLE -- IDLE state, system startup default. + * + FSM_OFFSET_CALIB -- Self calibrate, for ADC init. + * + FSM_CAP_CHARGE -- IPM cap charge. + * + FSM_CLEAR -- Clear before every run. + * + FSM_LOCATE -- Rotor position pre-locate. + * + FSM_STARTUP -- Start up. + * + FSM_SWITCH -- Transition state, control from open to closed loop. + * + FSM_RUN -- Normal running state. + * + FSM_WAIT_STOP -- Wait stopping. + * + FSM_STOP -- Normal stop. + * + FSM_FAULT -- Fault state, waiting for user process. + */ +typedef enum { + FSM_IDLE = 0, + FSM_OFFSET_CALIB, + FSM_CAP_CHARGE, + FSM_CLEAR, + FSM_LOCATE, + FSM_STARTUP, + FSM_SWITCH, + FSM_RUN, + FSM_WAIT_STOP, + FSM_STOP, + FSM_FAULT +} FsmState; + +/** + * @brief Sampling mode. + */ +typedef enum { + DUAL_RESISTORS = 0, + SINGLE_RESISTOR = 1 +} SampleMode; + +/** + * @brief Motor control data structure + */ +typedef struct { + unsigned char motorStateFlag; /**< Motor start/stop status flag. */ + float spdCmdHz; /**< External input speed command value. */ + float axisAngle; /**< Angle of the synchronous coordinate system. */ + float spdRefHz; /**< Command value after speed ramp management. */ + float currCtrlPeriod; /**< current loop control period. */ + float adc0Compensate; /**< ADC0 softwaretrim compensate value. */ + float adc1Compensate; /**< ADC1 softwaretrim compensate value. */ + float udc; /**< Bus voltage. */ + float powerBoardTemp; /**< Power boart surface temperature. */ + unsigned short aptMaxcntCmp; /**< Apt Maximum Comparison Count. */ + float adcCurrCofe; /**< Adc current sampling cofeature. */ + + unsigned short sysTickCnt; /**< System Timer Tick Count. */ + unsigned short capChargeTickNum; /**< Bootstrap Capacitor Charge Tick Count. */ + volatile unsigned int msTickCnt; /**< Millisecond-level counter, which can be used in 1-ms and 5-ms tasks. */ + unsigned short msTickNum; /**< Number of ticks corresponding to 1 ms. */ + char obserType; /**< Set Observer Type. */ + char controlMode; /**< Set foc control or sixstep bldc control mode or others. */ + char spdAdjustMode; /**< Set speed adjust mode. */ + char uartConnectFlag; /**< Uart connect success flag. */ + short uartHeartDetCnt; /**< Uart connect heart detect count. */ + float uartTimeStamp; /**< Uart data time stamp. */ + SysStatusReg statusReg; /**< System status. */ + FsmState stateMachine; /**< Motor Control State Machine. */ + + SampleMode sampleMode; /**< Current sampling mode setting: single resistor, double resistor. */ + MOTOR_Param mtrParam; /**< Motor parameters. */ + FOSMO_Handle smo; /**< SMO observer handle. */ + SMO4TH_Handle smo4th; /**< SMO 4th observer handle. */ + IF_Handle ifCtrl; /**< I/F control handle. */ + SVPWM_Handle sv; /**< Dual-resistance/three-resistance sampling SVPWM Handle. */ + R1SVPWM_Handle r1Sv; /**< Single-resistance phase-shifted SVPWM handle. */ + RMG_Handle spdRmg; /**< Ramp management struct for the speed controller input reference. */ + SPDCTRL_Handle spdCtrl; /**< Speed loop Control Handle. */ + CURRCTRL_Handle currCtrl; /**< Current loop control handle. */ + STARTUP_Handle startup; /**< Startup Switch Handle. */ + FW_Handle fw; /**< Flux-Weakening Handle. */ + + DqAxis idqRef; /**< Command value of the dq axis current. */ + UvwAxis currUvw; /**< Three-phase current sampling value. */ + AlbeAxis iabFbk; /**< αβ-axis current feedback value. */ + DqAxis idqFbk; /**< Current feedback value of the dq axis. */ + DqAxis vdqRef; /**< Current loop output dq voltage. */ + AlbeAxis vabRef; /**< Current loop output voltage αβ. */ + UvwAxis dutyUvw; /**< UVW three-phase duty cycle. */ + UvwAxis dutyUvwLeft; /**< Single Resistor UVW Three-Phase Left Duty Cycle. */ + UvwAxis dutyUvwRight; /**< Single Resistor UVW Three-Phase Right Duty Cycle. */ + + MCS_ReadCurrUvwCb readCurrUvwCb; /**< Read current callback function. */ + MCS_SetPwmDutyCb setPwmDutyCb; /**< Set the duty cycle callback function. */ + MCS_SetADCTriggerTimeCb setADCTriggerTimeCb; /**< Sets the ADC trigger point callback function. */ + + MotorProtStatus_Handle prot; /**< Motor protect status detection flag bit handle. */ +} MTRCTRL_Handle; + +void MCS_CarrierProcess(MTRCTRL_Handle *mtrCtrl); + +#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_chip_config.h b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_chip_config.h new file mode 100644 index 00000000..38c00b5d --- /dev/null +++ b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_chip_config.h @@ -0,0 +1,76 @@ +/** + * @ Copyright (c) HiSilicon (Shanghai) Technologies Co., Ltd. 2022-2023. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_chip_config.h + * @author MCU Algorithm Team + * @brief This file provides config macros for ECMCU105H/ECBMCU201MPC app. + */ + +#ifndef McuMagicTag_MCS_CHIPCONFIG_H +#define McuMagicTag_MCS_CHIPCONFIG_H + +#include "feature.h" + +#if defined (CHIP_3061MNPICA) || defined (CHIP_3061MNPIKA) || defined (CHIP_3061MNNICA) || \ + defined (CHIP_3061MNNIKA) || defined (CHIP_3061MNPIC8) || defined(CHIP_3061MNNIC8) || \ + defined (CHIP_3061MNPIK8) || defined (CHIP_3061MNNIK8) + + #define ADCPTT_HANDLE g_adc0 + #define ADCRESIS_HANDLE g_adc0 + #define ADCUDC_HANDLE g_adc0 + #define ADCU_HANDLE g_adc0 + #define ADCW_HANDLE g_adc0 + #define LED1_HANDLE g_gpio2 + #define LED2_HANDLE g_gpio1 + #define LED2_PIN GPIO_PIN_0 + #define LED1_PIN GPIO_PIN_3 + #define ADCPTTSOCNUM ADC_SOC_NUM0 + #define ADCUSOCNUM ADC_SOC_NUM1 + #define ADCRESISSOCNUM ADC_SOC_NUM3 + #define ADCUDCSOCNUM ADC_SOC_NUM4 + #define ADCWSOCNUM ADC_SOC_NUM6 + + #define ADC0COMPENSATE 2037.0f + #define ADC1COMPENSATE 2027.0f + +#endif + +#if defined (CHIP_3065HRPIRZ) || defined (CHIP_3065ARPIRZ) || defined (CHIP_3066MNPIRH) || \ + defined (CHIP_3065PNPIRH) || defined (CHIP_3065PNPIRE) || defined (CHIP_3065PNPIRA) + + #define ADCU_HANDLE g_adc0 + #define ADCW_HANDLE g_adc1 + #define ADCRESIS_HANDLE g_adc2 + #define ADCUDC_HANDLE g_adc2 + #define ADCPTT_HANDLE g_adc2 + #define LED1_HANDLE g_gpio0 + #define LED2_HANDLE g_gpio0 + #define LED2_PIN GPIO_PIN_6 + #define LED1_PIN GPIO_PIN_7 + #define ADCPTTSOCNUM ADC_SOC_NUM0 + #define ADCRESISSOCNUM ADC_SOC_NUM1 + #define ADCUSOCNUM ADC_SOC_NUM8 + #define ADCWSOCNUM ADC_SOC_NUM8 + #define ADCUDCSOCNUM ADC_SOC_NUM14 + + #define ADC0COMPENSATE 2033.0f + #define ADC1COMPENSATE 2070.0f + +#endif + + +#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_ctlmode_config.h b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_ctlmode_config.h new file mode 100644 index 00000000..2a6f38cc --- /dev/null +++ b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_ctlmode_config.h @@ -0,0 +1,73 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_ctlmode_config.h + * @author MCU Algorithm Team + * @brief This file provides config macros for ECMCU105H app. + */ + + /* Define to prevent recursive inclusion ------------------------------------------------------- */ +#ifndef McuMagicTag_MCS_CTLMODECONFIG_H +#define McuMagicTag_MCS_CTLMODECONFIG_H + +#include "debug.h" +#include "typedefs.h" + +typedef enum { + FOC_STARTUP_IF = 0, + FOC_STARTUP_VF, + FOC_STARTUP_HFI +} MOTOR_STARTUPMODE_CONFIG; + +typedef enum { + FOC_OBSERVERTYPE_SMO1TH = 0, + FOC_OBSERVERTYPE_SMO1TH_PLL, + FOC_OBSERVERTYPE_SMO4TH, + FOC_OBSERVERTYPE_SMO4TH_PLL, + FOC_OBSERVERTYPE_LUNBORG, + FOC_OBSERVERTYPE_FLUX +} MOTOR_OBSERVERTYPE_CONFIG; + +typedef enum { + FOC_CONTROLMODE_SPEED = 0, + FOC_CONTROLMODE_TORQUE +} MOTOR_CONTROLMODE_CONFIG; + +typedef enum { + FOC_CURQAXISPID_PARAMS = 0, + FOC_CURDAXISPID_PARAMS, + FOC_SPDPID_PARAMS +} MOTOR_PID_SET; + +typedef enum { + MOTOR_PARAMS_BASE = 0, + MOTOR_PARAMS_SPECIAL, + MOTOR_PARAMS_BOARD +} MOTOR_PARAMS_SET; + +typedef enum { + CUST_SPEED_ADJUST = 0, + HOST_SPEED_ADJUST +} MODE_ADSPEED_CONFIG; + +typedef enum { + CONNECTING = 0, + CONNECTED, + DISCONNECT +} UART_STATUS; + +#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_motor_process.h b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_motor_process.h new file mode 100644 index 00000000..783bf214 --- /dev/null +++ b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_motor_process.h @@ -0,0 +1,45 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_motor_process.h + * @author MCU Algorithm Team + * @brief This file provides motor sample functions declaration for ECMCU105H board. + */ +#ifndef McuMagicTag_MCS_MOTOR_PROCESS_H +#define McuMagicTag_MCS_MOTOR_PROCESS_H + +/** + * @brief phase sequence. + */ +typedef enum { + PHASE_U = 0, + PHASE_V, + PHASE_W, + PHASE_MAX_NUM +} MCS_PhaseEnum; + +/** + * @brief key state. + */ +typedef enum { + KEY_DOWN = 0, + KEY_UP = 1, +} KEY_State; + +int MotorMainProcess(void); + +#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_status.h b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_status.h new file mode 100644 index 00000000..19510c78 --- /dev/null +++ b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_status.h @@ -0,0 +1,188 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_status.h + * @author MCU Algorithm Team + * @brief This file provides functions declaration of system status. + */ + +/* Define to prevent recursive inclusion ------------------------------------------------------- */ +#ifndef McuMagicTag_MCS_STATUS_H +#define McuMagicTag_MCS_STATUS_H + +/* Includes ------------------------------------------------------------------------------------ */ +#include "typedefs.h" +#include "mcs_assert.h" + +/* Typedef definitions ------------------------------------------------------------------------- */ +/** + * @brief System status define + */ +typedef union { + unsigned short all; + struct { + unsigned short cmdStart : 1; /**< Indicates that a start system command has been received. */ + unsigned short cmdStop : 1; /**< Indicates that a stop system command has been received. */ + unsigned short isRunning : 1; /**< Indicates that the system is running (enable signal) */ + unsigned short sysError : 1; /**< Indicates that the system reports an error. */ + unsigned short poweron : 1; /**< Indicates that the power-on initialization phase is complete. */ + unsigned short capcharge : 1; /**< Indicates that the bootstrap capacitor charging phase is complete. */ + unsigned short adczero : 1; /**< The current sampling point is reset to zero after power-on. */ + } Bit; +} SysStatusReg; + +/** + * @brief Get status of Bit cmdStart. + * @param sysStatus System status register handle. + * @retval Status of Bit cmdStart. + */ +static inline bool SysGetCmdStart(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + bool ret; + ret = (sysStatus->Bit.cmdStart == 1) ? true : false; + return ret; +} + +/** + * @brief Set Bit cmdStart. + * @param sysStatus System status register handle. + * @retval None. + */ +static inline void SysCmdStartSet(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + sysStatus->Bit.cmdStart = 1; +} + +/** + * @brief Clear Bit cmdStart. + * @param handle System status register handle. + * @retval None. + */ +static inline void SysCmdStartClr(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + sysStatus->Bit.cmdStart = 0; +} + +/** + * @brief Get status of Bit cmdStop. + * @param sysStatus System status register handle. + * @retval Status of Bit cmdStart. + */ +static inline bool SysGetCmdStop(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + bool ret; + ret = (sysStatus->Bit.cmdStop == 1) ? true : false; + return ret; +} + +/** + * @brief Set Bit cmdStop. + * @param sysStatus System status register handle. + * @retval None. + */ +static inline void SysCmdStopSet(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + sysStatus->Bit.cmdStop = 1; +} + +/** + * @brief Clear Bit cmdStop. + * @param sysStatus System status register handle. + * @retval None. + */ +static inline void SysCmdStopClr(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + sysStatus->Bit.cmdStop = 0; +} + +/** + * @brief Get status of Bit isRunning. + * @param sysStatus System status register handle. + * @retval Status of Bit isRunning. + */ +static inline bool SysIsRunning(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + bool ret; + ret = (sysStatus->Bit.isRunning == 1) ? true : false; + return ret; +} + +/** + * @brief Set Bit isRuning. + * @param sysStatus System status register handle. + * @retval None. + */ +static inline void SysRunningSet(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + sysStatus->Bit.isRunning = 1; +} + +/** + * @brief Clear Bit isRuning. + * @param sysStatus System status register handle. + * @retval None. + */ +static inline void SysRunningClr(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + sysStatus->Bit.isRunning = 0; +} + +/** + * @brief Get status of Bit sysError. + * @param sysStatus System status register handle. + * @retval Status of Bit sysError. + */ +static inline bool SysIsError(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + bool ret; + ret = (sysStatus->Bit.sysError == 1) ? true : false; + return ret; +} + +/** + * @brief Set Bit sysError. + * @param sysStatus System status register handle. + * @retval None. + */ +static inline void SysErrorSet(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + sysStatus->Bit.sysError = 1; +} + +/** + * @brief Clear Bit sysError. + * @param sysStatus System status register handle. + * @retval None. + */ +static inline void SysErrorClr(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + sysStatus->Bit.sysError = 0; +} + +#endif diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_user_config.h b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_user_config.h new file mode 100644 index 00000000..19e1f89f --- /dev/null +++ b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_user_config.h @@ -0,0 +1,120 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_user_config.h + * @author MCU Algorithm Team + * @brief This file provides functions declaration of user config parameters. + */ +#ifndef McuMagicTag_MCS_CONFIG_H +#define McuMagicTag_MCS_CONFIG_H + +#include "debug.h" +#include "typedefs.h" + +#define SMO4TH + +#define SYSTICK_PERIOD_US 500u /* systick period. */ + +#define INV_CAP_CHARGE_MS 3u /* Capacitor charging time. */ + +#define INV_VOLTAGE_BUS 24.0f /* Bus voltage, V. */ + +#define CTRL_CURR_PERIOD 0.0001f /* carrier ISR period, 100us. */ +#define CTRL_SYSTICK_PERIOD 0.0005f /* systick control period, 500us. */ + +/* Duty of sample window, the real time is 0.06*50us = 3us. */ +#define SAMPLE_WINDOW_DUTY 0.06f + +/* Duty of sample point shift as flip point, the real time is 0.008*50us = 0.4us. */ +#define SAMPLE_POINT_SHIFT 0.008f + +/* Sampling resistance 200mOhm 0.0013295 */ +#define ADC_CURR_COFFI 0.0013295f /* 3.3/4096/3.03/0.2 pga: 3.03, 200mohm. */ + +#define APT_U APT0_BASE /* Base address of U phase APT module. */ +#define APT_V APT1_BASE /* Base address of V phase APT module. */ +#define APT_W APT2_BASE /* Base address of W phase APT module. */ + +/* FOSMO */ +#define FOSMO_GAIN 4.0f /* SMO gain */ +#define FOSMO_LAMBDA 2.0f /* SMO coefficient of cut-off frequency, its value = lambda * we. */ +#define FOSMO_EMF_CUTOFF_FREQ 2.0f /* SMO back emf cutoff frequency. */ +#define SPEED_FILTER_CUTOFF_FREQUENCY 40.0f /* SMO speed cutoff frequency of speed filter. */ +#define FOSMO_PLL_BDW 30.0f /* SMO PLL bandwidth. */ + +/* SMO4TH */ +#define SMO4TH_PLL_BDW 30.0f /* SMO4TH PLL Bandwidth. */ +#define SMO4TH_KD 300.0f /* SMO4TH parameters KD. */ +#define SMO4TH_KQ 600.0f /* SMO4TH parameters KQ. */ +#define SMO4TH_SPD_FILTER_CUTOFF_FREQ 40.0f /* SMO4TH speed cutoff frequency of speed filter. */ + +/* User_Commond */ +#define CTRL_IF_CURR_AMP_A 0.8f /* IF control current amplitude. */ +#define USER_TARGET_SPD_HZ 30.0f /* User-defined target speed value. */ +#define USER_SWITCH_SPDBEGIN_HZ 30.0f /* Start of handover interval. */ +#define USER_SWITCH_SPDEND_HZ (USER_SWITCH_SPDBEGIN_HZ + 3.0f) /* End of handover period. */ +#define USER_MAX_SPD_HZ 24.0f /* User-defined maximum speed value. */ +#define USER_MIN_SPD_HZ 5.0f /* User-defined minimum speed value. */ +#define USER_SPD_SLOPE 50.0f /* slope of velocity change. */ +#define USER_CURR_SLOPE (CTRL_IF_CURR_AMP_A * 10.0f) /* Current change slope. */ + +/* PID PARAMS */ +#define CURRQAXIS_KP 5.023202f /* Current loop Q axis Kp. */ +#define CURRQAXIS_KI 20612.84f /* Current loop Q axis Ki. */ +#define CURRDAXIS_KP 3.477114f /* Current loop D axis Kp. */ +#define CURRDAXIS_KI 20612.84f /* Current loop D axis Ki. */ +/* Current loop PID output lower limit. */ +#define CURR_LOWERLIM (-INV_VOLTAGE_BUS * ONE_DIV_SQRT3 * 0.98f) +/* Current loop PID output upper limit. */ +#define CURR_UPPERLIM (INV_VOLTAGE_BUS * ONE_DIV_SQRT3 * 0.98f) + +#define SPD_KP 0.00505f /* Speed loop Kp. */ +#define SPD_KI 0.012f /* Speed loop Ki. */ +#define SPD_LOWERLIM (-0.105f) /* Speed loop PID output lower limit. */ +#define SPD_UPPERLIM 0.105f /* Speed loop PID output upper limit. */ + +/* MOTOR PARAMS */ +/* Np, Rs, Ld, Lq, Psif, J, Nmax, Currmax, PPMR, zShift. */ +/* mtrPsif & mtrJ parameter is not used in this project, temporarily set to 0. */ +// #define MOTORPARAM_DEFAULTS { \ +// .mtrNp = 7, \ +// .mtrRs = 5.1f, \ +// .mtrLd = 0.00133f, \ +// .mtrLq = 0.00133f, \ +// .mtrPsif = 0.0f, \ +// .mtrJ = 0.0f, \ +// .maxElecSpd = 180.25f, \ +// .maxCurr = 0.105f, \ +// .busVolt = INV_VOLTAGE_BUS, \ +// } + +// // GIM4310-10 +#define MOTORPARAM_DEFAULTS { \ + .mtrNp = 14, \ + .mtrRs = 1.046f, \ + .mtrLd = 0.000344f, \ + .mtrLq = 0.000344f, \ + .mtrPsif = 0.0f, \ + .mtrJ = 0.0f, \ + .maxElecSpd = 24.0f, \ + .maxCurr = 0.8f, \ + .busVolt = INV_VOLTAGE_BUS, \ +} +/* Bus voltage sampling conversion factor. */ +#define ADC_UDC_COFFI 0.01289f /* 0.01289 = 3.3/4096*192/12. */ + +#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_cmm.c b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_cmm.c new file mode 100644 index 00000000..99766bdf --- /dev/null +++ b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_cmm.c @@ -0,0 +1,43 @@ +/** + * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_prot_cmm.c + * @author MCU Algorithm Team + * @brief This file contains protection common api definition. + */ + +/* Includes ------------------------------------------------------------------------------------ */ +#include "mcs_prot_cmm.h" +#include "apt.h" +#include "mcs_assert.h" + +/** + * @brief Safty-pulse-off function execution to turn off all the power devices. + * @param aptAddr Three-phase APT address pointer. + * @retval None. + */ +void ProtSpo_Exec(APT_RegStruct **aptAddr) +{ + MCS_ASSERT_PARAM(aptAddr != NULL); + /**< Turn off all the six power devices of the inverter. */ + for (unsigned int i = 0; i < MOTOR_PHASE_NUMBER; i++) { + APT_RegStruct *aptx = (APT_RegStruct *)(aptAddr[i]); + DCL_APT_EnableSwContPWMAction(aptx, APT_PWM_CHANNEL_A); + DCL_APT_EnableSwContPWMAction(aptx, APT_PWM_CHANNEL_B); + DCL_APT_ForcePWMOutputLow(aptx); + } +} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_cmm.h b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_cmm.h new file mode 100644 index 00000000..104a5dec --- /dev/null +++ b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_cmm.h @@ -0,0 +1,136 @@ +/** + * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_prot_cmm.h + * @author MCU Algorithm Team + * @brief This file contains protection function common data struct and api declaration. + */ + +/* Define to prevent recursive inclusion ------------------------------------------------------- */ +#ifndef McuMagicTag_MCS_PROT_CMM_H +#define McuMagicTag_MCS_PROT_CMM_H + +#include "typedefs.h" +#include "apt_ip.h" + +/* Macro definitions --------------------------------------------------------------------------- */ +#define MOTOR_PHASE_NUMBER (3) + +#define PROT_VAL_THRESHOLD_NUMS (4) +#define PROT_VAL_THRESHOLD_0 (0) +#define PROT_VAL_THRESHOLD_1 (1) +#define PROT_VAL_THRESHOLD_2 (2) +#define PROT_VAL_THRESHOLD_3 (3) + +#define PROT_LIMIT_TIME_NUMS (3) +#define PROT_LIMIT_TIME_0 (0) +#define PROT_LIMIT_TIME_1 (1) +#define PROT_LIMIT_TIME_2 (2) + +#define MOTOR_PHASE_NUMBER (3) + +/**< Motor error status definition. */ +typedef union { + int all; + struct { + unsigned short overCurrErr : 1; /**< Indicates that phase current(s) is over protected value. */ + unsigned short overVoltErr : 1; /**< Indicates that dc-link voltage is over protected value. */ + unsigned short lowerVoltErr : 1; /**< Indicates that dc-link voltage is lower than protected value */ + unsigned short overIpmTempErr : 1; /**< Indicates that IPM temperature is over protected value. */ + unsigned short revRotErr : 1; /**< Indicates that motor negtive direction. */ + unsigned short motorStalling : 1; /**< Indicates that rotor is stalling. */ + unsigned short overMotorTempErr : 1; /**< Indicates that three phase currents is out-of-balance. */ + unsigned short posSnsrCommsErr : 1; /**< Indicates that position sensor communication is lost with MCU. */ + unsigned short posSnsrFuncErr : 1; /**< Indicates that position sensor reports function error. */ + unsigned short posSnsrCalibrErr : 1; /**< Indicates that position sensor fails to calibrate itself. */ + unsigned short currOutOfBalance : 1; /**< Indicates that the rotor is reverse rotation.*/ + unsigned short phsOpenErr : 1; /**< Indicates that phase winding(s) is open. */ + unsigned short phsU : 1; /**< Indicates that u phase fails when phsOpenErr occurs. */ + unsigned short phsV : 1; /**< Indicates that v phase fails when phsOpenErr occurs. */ + unsigned short phsW : 1; /**< Indicates that w phase fails when phsOpenErr occurs. */ + unsigned short multiPhs : 1; /**< Indicates that multi-phases fail when phsOpenErr occurs.*/ + } Bit; +} MotorErrStatusReg; + +/**< Protection Status Bit Definition */ +typedef enum { + OCP_ERR_BIT, + OVP_ERR_BIT, + LVP_ERR_BIT, + OTP_IPM_ERR_BIT, + OTP_MOTOR_ERR_BIT, + STALLING_ERR_BIT, + CURR_OUT_BALANCE_ERR_BIT, + POS_COMMS_ERR_BIT, + POS_FUNC_ERR_BIT, + POS_CALIB_ERR_BIT, + REV_ROT_ERR_BIT, + PHS_OPEN_ERR_BIT, + PHS_U_ERR_BIT, + PHS_V_ERR_BIT, + PHS_W_ERR_BIT, + PHS_MULTI_ERR_BIT, +} PROT_ErrBit; + +/**< Motor error protection level. */ +typedef enum { + PROT_LEVEL_0 = 0, + PROT_LEVEL_1, + PROT_LEVEL_2, + PROT_LEVEL_3, + PROT_LEVEL_4 /**< The greater level number, the severe error is. */ +} PROT_Level; + +/** + * @brief Obtains the status of a bit of data. + * @param data data. + * @param bits Number of digits. + * @retval Bit status. + */ +static inline bool GetBit(int data, unsigned short bit) +{ + bool ret; + ret = ((data >> bit) & 1); + return ret; +} + +/** + * @brief Sets the status of a bit of data. + * @param data data. + * @param bit The setted bit. + * @retval None. + */ +static inline void SetBit(int *data, unsigned char bit) +{ + *data |= (1 << bit); +} + +/** + * @brief Clear the status of a bit of data. + * @param data data. + * @param bit The Clear bit. + * @retval None. + */ +static inline void ClearBit(int *data, unsigned char bit) +{ + *data &= ~(1 << bit); +} + +/**< Protection action. */ +void ProtSpo_Exec(APT_RegStruct **aptAddr); + +#endif diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user.c b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user.c new file mode 100644 index 00000000..e97876b7 --- /dev/null +++ b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user.c @@ -0,0 +1,137 @@ +/** + * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_prot_user.c + * @author MCU Algorithm Team + * @brief This file contains user protection data struct, inquiry api and initialization api definition. + */ + +/* Includes ------------------------------------------------------------------------------------ */ +#include "mcs_prot_user.h" +#include "mcs_prot_user_config.h" +#include "mcs_assert.h" + +/** + * @brief Get motor over current error status. + * @param motorErrStatus Motor error status. + * @retval Error status. + */ +bool IsMotorOverCurrErr(MotorErrStatusReg motorErrStatus) +{ + if (motorErrStatus.Bit.overCurrErr) { + return true; + } else { + return false; + } +} + +/** + * @brief Get motor over dc-link voltage error status. + * @param motorErrStatus Motor error status. + * @retval Error status. + */ +bool IsDcLinkOverVoltErr(MotorErrStatusReg motorErrStatus) +{ + if (motorErrStatus.Bit.overVoltErr) { + return true; + } else { + return false; + } +} + +/** + * @brief Get motor lower dc-link voltage error status. + * @param motorErrStatus Motor error status. + * @retval Error status. + */ +bool IsDcLinkLowerVoltErr(MotorErrStatusReg motorErrStatus) +{ + if (motorErrStatus.Bit.lowerVoltErr) { + return true; + } else { + return false; + } +} + +/** + * @brief Get motor over Ipm temperature error status. + * @param motorErrStatus Motor error status. + * @retval Error status. + */ +bool IsIpmOverTempErr(MotorErrStatusReg motorErrStatus) +{ + if (motorErrStatus.Bit.overIpmTempErr) { + return true; + } else { + return false; + } +} + +/** + * @brief Get motor over Motor temperature error status. + * @param motorErrStatus Motor error status. + * @retval Error status. + */ +bool IsMotorOverTempErr(MotorErrStatusReg motorErrStatus) +{ + if (motorErrStatus.Bit.overMotorTempErr) { + return true; + } else { + return false; + } +} + +/** + * @brief Get motor stalling error status. + * @param motorErrStatus Motor error status. + * @retval Error status. + */ +bool IsMotorStallingErr(MotorErrStatusReg motorErrStatus) +{ + if (motorErrStatus.Bit.motorStalling) { + return true; + } else { + return false; + } +} + +/** + * @brief Clear the motor error status. + * @param motorProt Motor protection handle. + * @retval Error status. + */ +void ClearMotorErrStatus(MotorProtStatus_Handle *motorProt) +{ + MCS_ASSERT_PARAM(motorProt != NULL); + /* Clear the motor error status. */ + motorProt->motorErrStatus.all = 0x00; + OCP_Clear(&motorProt->ocp); + OVP_Clear(&motorProt->ovp); + LVP_Clear(&motorProt->lvp); + OTP_Clear(&motorProt->otp); +} + +/** + * @brief Motor protection function initialization. + * @param motorProt Motor protection handle. + * @retval Error status. + */ +void MotorProt_Init(MotorProtStatus_Handle *motorProt) +{ + MCS_ASSERT_PARAM(motorProt != NULL); + motorProt->motorErrStatus.all = 0x00; +} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user.h b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user.h new file mode 100644 index 00000000..365110e1 --- /dev/null +++ b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user.h @@ -0,0 +1,54 @@ +/** + * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_prot_user.h + * @author MCU Algorithm Team + * @brief This file contains user protection data struct, inquiry api and initialization api declaration. + */ + +/* Define to prevent recursive inclusion ------------------------------------------------------- */ +#ifndef McuMagicTag_MCS_PROT_USER_H +#define McuMagicTag_MCS_PROT_USER_H + +/* Includes ------------------------------------------------------------------------------------ */ +#include "mcs_prot_cmm.h" +#include "mcs_curr_prot.h" +#include "mcs_dc_volt_prot.h" +#include "mcs_temp_prot.h" +#include "mcs_motor_stalling.h" +#include "typedefs.h" + +typedef struct { + MotorErrStatusReg motorErrStatus; /**< Motor error status. */ + OCP_Handle ocp; /**< Over current protection. */ + OVP_Handle ovp; /**< Over dc-link voltage protection. */ + LVP_Handle lvp; /**< Lower dc-link voltage protection. */ + OTP_Handle otp; /**< Over IPM temperature protection. */ + STP_Handle stall; /**< Motor stalling protection. */ +} MotorProtStatus_Handle; + +void MotorProt_Init(MotorProtStatus_Handle *motorProt); + +/**< Inquiry motor error status */ +bool IsMotorOverCurrErr(MotorErrStatusReg motorErrStatus); +bool IsDcLinkOverVoltErr(MotorErrStatusReg motorErrStatus); +bool IsDcLinkLowerVoltErr(MotorErrStatusReg motorErrStatus); +bool IsIpmOverTempErr(MotorErrStatusReg motorErrStatus); +bool IsMotorOverTempErr(MotorErrStatusReg motorErrStatus); +bool IsMotorStallingErr(MotorErrStatusReg motorErrStatus); +void ClearMotorErrStatus(MotorProtStatus_Handle *motorProt); +#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user_config.h b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user_config.h new file mode 100644 index 00000000..c0564a22 --- /dev/null +++ b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user_config.h @@ -0,0 +1,125 @@ +/** + * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_prot_user_config.h + * @author MCU Algorithm Team + * @brief This file contans user macro definition of the protection function. + */ + +/* Define to prevent recursive inclusion ------------------------------------------------------- */ +#ifndef McuMagicTag_MCS_PROT_USER_CONFIG_H +#define McuMagicTag_MCS_PROT_USER_CONFIG_H + +/* Macro definitions --------------------------------------------------------------------------- */ +/**< COMMON */ +/**< Peak phase current (A) of the motor or IPM under continuous operations. */ +#define PROT_MOTOR_RATED_CURR (1.5f) +/**< Only several continuous fault detection can trigger error status. */ +#define PROT_CNT_LIMIT (100) +/**< Only several contunuous none fault dectection can trigger elimination of error status. */ +#define RECY_CNT_LIMIT (10000) +/**< Only several contunuous none fault dectection can trigger elimination of error status. */ +#define OVER_VOLT_RECY_CNT_LIMIT (100) +/**< Only several contunuous none fault dectection can trigger elimination of error status. */ +#define LOWER_VOLT_RECY_CNT_LIMIT (100) + +/**< Over current protection */ +/**< Over current trigger value (A) when in level 1. */ +#define PROT_OVER_CURR_POW_DN1 (2.0f * PROT_MOTOR_RATED_CURR) +/**< Over current trigger value (A) when in level 2. */ +#define PROT_OVER_CURR_POW_DN2 (2.0f * PROT_MOTOR_RATED_CURR) +/**< Over current trigger value (A) when in level 3. */ +#define PROT_OVER_CURR_POW_DN3 (2.0f * PROT_MOTOR_RATED_CURR) +/**< Over current trigger value (A) when in level 4. */ +#define PROT_OVER_CURR_POW_OFF (2.0f * PROT_MOTOR_RATED_CURR) +#define PROT_OVER_CURR_RECY_DELTA (0.0035f) /**< Current gap (A) when recovers from protection status. */ +#define PROT_OVER_CURR_LIMIT1_TIME_SEC (30.0f) /**< 20% overload can last maximum time: 30 sec. */ +#define PROT_OVER_CURR_LIMIT2_TIME_SEC (10.0f) /**< 20% overload can last maximum time: 10 sec. */ +#define PROT_OVER_CURR_LIMIT3_TIME_SEC (2.0f) /**< 20% overload can last maximum time: 2 sec. */ + +/**< Over voltage protection */ +#define PROT_OVER_VOLT_BRK_ON1 (26.0f) /**<处于1级时,直流链路电压触发值(V)过高*/ +#define PROT_OVER_VOLT_BRK_ON2 (27.0f) /**< Over dc-link voltage trigger value (V) when in level 2. */ +#define PROT_OVER_VOLT_BRK_ON3 (28.0f) /**< Over dc-link voltage trigger value (V) when in level 3. */ +#define PROT_OVER_VOLT_BRK_ALL (30.0f) /**< Over dc-link voltage trigger value (V) when in level 4. */ +#define PROT_OVER_VOLT_RECY_DELTA (0.5f) /**<从保护状态恢复时的电压间隙(V)*/ +#define PROT_OVER_VOLT_LIMIT1_TIME_SEC (5.0f) /** 1; One-cycle mode: < 0.5 */ +#define POS_SNSR_IPD_INJ_PERIOD (4) + +#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_curr_prot.c b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_curr_prot.c new file mode 100644 index 00000000..da4e56f8 --- /dev/null +++ b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_curr_prot.c @@ -0,0 +1,267 @@ +/** + * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_curr_prot.c + * @author MCU Algorithm Team + * @brief This file contains current protecion api definition. + */ + +/* Includes ------------------------------------------------------------------------------------ */ +#include "mcs_curr_prot.h" +#include "mcs_math.h" +#include "mcs_prot_user_config.h" +#include "mcs_assert.h" + +/** + * @brief Initilization over current protection function. + * @param ocp Over current protection handle. + * @param ts Ctrl period (s). + * @retval None. + */ +void OCP_Init(OCP_Handle *ocp, float ts) +{ + MCS_ASSERT_PARAM(ocp != NULL); + MCS_ASSERT_PARAM(ts > 0.0f); + ocp->ts = ts; + OCP_Clear(ocp); + + ocp->protCntLimit = PROT_CNT_LIMIT; + ocp->recyCntLimit = RECY_CNT_LIMIT; + /* Configuring four levels of current protection thresholds. */ + ocp->protValThr[PROT_VAL_THRESHOLD_0] = PROT_OVER_CURR_POW_DN1; + ocp->protValThr[PROT_VAL_THRESHOLD_1] = PROT_OVER_CURR_POW_DN2; + ocp->protValThr[PROT_VAL_THRESHOLD_2] = PROT_OVER_CURR_POW_DN3; + ocp->protValThr[PROT_VAL_THRESHOLD_3] = PROT_OVER_CURR_POW_OFF; + /* Configure the protection limit time. */ + ocp->protLimitTime[PROT_LIMIT_TIME_0] = PROT_OVER_CURR_LIMIT1_TIME_SEC; + ocp->protLimitTime[PROT_LIMIT_TIME_1] = PROT_OVER_CURR_LIMIT2_TIME_SEC; + ocp->protLimitTime[PROT_LIMIT_TIME_2] = PROT_OVER_CURR_LIMIT3_TIME_SEC; + ocp->recyDelta = PROT_OVER_CURR_RECY_DELTA; +} + +/** + * @brief Over current protection detection. + * @param ocp Over current protection handle. + * @param motorErrStatus Motor error status. + * @param idq DQ-axis feedback currents. + * @retval None. + */ +void OCP_Det(OCP_Handle *ocp, MotorErrStatusReg *motorErrStatus, DqAxis idq) +{ + MCS_ASSERT_PARAM(ocp != NULL); + MCS_ASSERT_PARAM(motorErrStatus != NULL); + /* Calculate current amplitude. */ + ocp->currAmp = Sqrt(idq.d * idq.d + idq.q * idq.q); + + /* Check if value goes over threshold for continuous cycles. */ + if (ocp->currAmp < ocp->protValThr[PROT_VAL_THRESHOLD_0]) { + ocp->protCnt = 0; + return; + } + + if (ocp->protCnt < ocp->protCntLimit) { + ocp->protCnt++; + return; + } + + /* Check which protection level should be activated. */ + /* Once enter error status, error status can only be changed by recover api. */ + /* protect level: level 4. */ + if (ocp->currAmp >= ocp->protValThr[PROT_VAL_THRESHOLD_3] && ocp->protLevel < PROT_LEVEL_4) { + ocp->protLevel = PROT_LEVEL_4; + motorErrStatus->Bit.overCurrErr = 1; + ocp->protCnt = 0; + return; + } + + /* Protect level: level 3. */ + if (ocp->currAmp >= ocp->protValThr[PROT_VAL_THRESHOLD_2] && ocp->protLevel < PROT_LEVEL_3) { + ocp->protLevel = PROT_LEVEL_3; + motorErrStatus->Bit.overCurrErr = 1; + ocp->protCnt = 0; + ocp->timer = 0.0f; + return; + } + + /* Protect level: level 2. */ + if (ocp->currAmp >= ocp->protValThr[PROT_VAL_THRESHOLD_1] && ocp->protLevel < PROT_LEVEL_2) { + ocp->protLevel = PROT_LEVEL_2; + motorErrStatus->Bit.overCurrErr = 1; + ocp->protCnt = 0; + ocp->timer = 0.0f; + return; + } + + /* Protect level: level 1. */ + if (ocp->currAmp >= ocp->protValThr[PROT_VAL_THRESHOLD_0] && ocp->protLevel < PROT_LEVEL_1) { + ocp->protLevel = PROT_LEVEL_1; + motorErrStatus->Bit.overCurrErr = 1; + ocp->protCnt = 0; + ocp->timer = 0.0f; + return; + } +} + +/** + * @brief Over current protection execution. + * @param ocp Over current protection handle. + * @param idqRef DQ-axis current references. + * @param aptAddr Three-phase APT address pointer. + * @retval None. + */ +void OCP_Exec(OCP_Handle *ocp, DqAxis *idqRef, APT_RegStruct **aptAddr) +{ + MCS_ASSERT_PARAM(ocp != NULL); + MCS_ASSERT_PARAM(idqRef != NULL); + MCS_ASSERT_PARAM(aptAddr != NULL); + + float id = idqRef->d; + float iq = idqRef->q; + float idqAmp = ocp->currAmp; + /* According to protect level, take corresponding action. */ + switch (ocp->protLevel) { + /* level 4: disable all PWM output. */ + case PROT_LEVEL_4: + /* Disable three-phase pwm output. */ + ProtSpo_Exec(aptAddr); + idqRef->d = 0.0f; + idqRef->q = 0.0f; + break; + + /* level 3: derate speed reference. */ + case PROT_LEVEL_3: + ocp->timer += ocp->ts; + if (ocp->timer > ocp->protLimitTime[PROT_LIMIT_TIME_2]) { + idqRef->d = id / idqAmp * PROT_MOTOR_RATED_CURR; + idqRef->q = iq / idqAmp * PROT_MOTOR_RATED_CURR; + } + break; + + /* level 2: derate speed reference. */ + case PROT_LEVEL_2: + ocp->timer += ocp->ts; + if (ocp->timer > ocp->protLimitTime[PROT_LIMIT_TIME_1]) { + idqRef->d = id / idqAmp * PROT_MOTOR_RATED_CURR; + idqRef->q = iq / idqAmp * PROT_MOTOR_RATED_CURR; + } + break; + + /* level 1: derate speed reference. */ + case PROT_LEVEL_1: + ocp->timer += ocp->ts; + if (ocp->timer > ocp->protLimitTime[PROT_LIMIT_TIME_0]) { + idqRef->d = id / idqAmp * PROT_MOTOR_RATED_CURR; + idqRef->q = iq / idqAmp * PROT_MOTOR_RATED_CURR; + } + break; + + /* level 0: take no protection action. */ + case PROT_LEVEL_0: + break; + + default: + break; + } +} + +/** + * @brief Over current protection recovery. + * @param ocp Over current protection handle. + * @param motorErrStatus Motor error status. + * @retval None. + */ +void OCP_Recy(OCP_Handle *ocp, MotorErrStatusReg *motorErrStatus) +{ + MCS_ASSERT_PARAM(ocp != NULL); + /* If not under error state, just return without any operation. */ + if (!motorErrStatus->Bit.overCurrErr) { + return; + } + + /* Calculate current amplitude. */ + float currAmp = ocp->currAmp; + + /* According to protection level, take corresponding recovery action. */ + switch (ocp->protLevel) { + /* level 4 */ + case PROT_LEVEL_4: + if (currAmp < ocp->protValThr[PROT_VAL_THRESHOLD_3] - ocp->recyDelta) { + ocp->recyCnt++; + if (ocp->recyCnt > ocp->recyCntLimit) { + ocp->protLevel = PROT_LEVEL_3; + ocp->recyCnt = 0; + } + } + break; + + /* level 3 */ + case PROT_LEVEL_3: + if (currAmp < ocp->protValThr[PROT_VAL_THRESHOLD_2] - ocp->recyDelta) { + ocp->recyCnt++; + if (ocp->recyCnt > ocp->recyCntLimit) { + ocp->protLevel = PROT_LEVEL_2; + ocp->recyCnt = 0; + } + } + break; + + /* level 2 */ + case PROT_LEVEL_2: + if (currAmp < ocp->protValThr[PROT_VAL_THRESHOLD_1] - ocp->recyDelta) { + ocp->recyCnt++; + if (ocp->recyCnt > ocp->recyCntLimit) { + ocp->protLevel = PROT_LEVEL_1; + ocp->recyCnt = 0; + } + } + break; + + /* level 1 */ + case PROT_LEVEL_1: + if (currAmp < ocp->protValThr[PROT_VAL_THRESHOLD_0] - ocp->recyDelta) { + ocp->recyCnt++; + if (ocp->recyCnt > ocp->recyCntLimit) { + ocp->protLevel = PROT_LEVEL_0; + ocp->recyCnt = 0; + } + } + break; + + /* level 0 */ + case PROT_LEVEL_0: + motorErrStatus->Bit.overCurrErr = 0; + break; + + default: + break; + } +} + +/** + * @brief Over current protection error status clear. + * @param ocp Over current protection handle. + * @retval None. + */ +void OCP_Clear(OCP_Handle *ocp) +{ + MCS_ASSERT_PARAM(ocp != NULL); + /* Clear the history value. */ + ocp->protCnt = 0; + ocp->protLevel = PROT_LEVEL_0; + ocp->recyCnt = 0; + ocp->timer = 0.0f; +} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_curr_prot.h b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_curr_prot.h new file mode 100644 index 00000000..e9916fe4 --- /dev/null +++ b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_curr_prot.h @@ -0,0 +1,52 @@ +/** + * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_curr_prot.h + * @author MCU Algorithm Team + * @brief This file contains current protection data struct and api declaration. + */ + +/* Define to prevent recursive inclusion ------------------------------------- */ +#ifndef McuMagicTag_MCS_CURR_PROT_H +#define McuMagicTag_MCS_CURR_PROT_H + +/* Includes ------------------------------------------------------------------------------------ */ +#include "mcs_prot_cmm.h" +#include "mcs_typedef.h" +#include "apt_ip.h" + +typedef struct { + unsigned int protCnt; + unsigned int recyCnt; + unsigned int protCntLimit; + unsigned int recyCntLimit; + float currAmp; + float protValThr[PROT_VAL_THRESHOLD_NUMS]; /* from low current level to high. */ + float protLimitTime[PROT_LIMIT_TIME_NUMS]; /* from level 3 to level 1. */ + float recyDelta; + float timer; + float ts; + PROT_Level protLevel; +} OCP_Handle; + +void OCP_Init(OCP_Handle *ocp, float ts); +void OCP_Det(OCP_Handle *ocp, MotorErrStatusReg *motorErrStatus, DqAxis idq); +void OCP_Exec(OCP_Handle *ocp, DqAxis *idqRef, APT_RegStruct **aptAddr); +void OCP_Recy(OCP_Handle *ocp, MotorErrStatusReg *motorErrStatus); +void OCP_Clear(OCP_Handle *ocp); + +#endif diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_dc_volt_prot.c b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_dc_volt_prot.c new file mode 100644 index 00000000..b0541a2e --- /dev/null +++ b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_dc_volt_prot.c @@ -0,0 +1,495 @@ +/** + * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_dc_volt_prot.c + * @author MCU Algorithm Team + * @brief This file contains dc-link voltage protection api declaration. + */ + +/* Includes ------------------------------------------------------------------------------------ */ +#include "mcs_dc_volt_prot.h" +#include "mcs_prot_user_config.h" +#include "mcs_assert.h" + +/** + * @brief Initilization over dc-link voltage protection function. + * @param ovp Over dc-link voltage protection handle. + * @param ts Ctrl period (s). + * @retval None. + */ +void OVP_Init(OVP_Handle *ovp, float ts) +{ + MCS_ASSERT_PARAM(ovp != NULL); + MCS_ASSERT_PARAM(ts > 0.0f); + ovp->ts = ts; + OVP_Clear(ovp); + ovp->protCntLimit = PROT_CNT_LIMIT; + ovp->recyCntLimit = OVER_VOLT_RECY_CNT_LIMIT; + /* Configuring four levels of overvoltage protection thresholds. */ + ovp->protValThr[PROT_VAL_THRESHOLD_0] = PROT_OVER_VOLT_BRK_ON1; + ovp->protValThr[PROT_VAL_THRESHOLD_1] = PROT_OVER_VOLT_BRK_ON2; + ovp->protValThr[PROT_VAL_THRESHOLD_2] = PROT_OVER_VOLT_BRK_ON3; + ovp->protValThr[PROT_VAL_THRESHOLD_3] = PROT_OVER_VOLT_BRK_ALL; + /* Configure the protection limit time. */ + ovp->protLimitTime[PROT_LIMIT_TIME_0] = PROT_OVER_VOLT_LIMIT1_TIME_SEC; + ovp->protLimitTime[PROT_LIMIT_TIME_1] = PROT_OVER_VOLT_LIMIT2_TIME_SEC; + ovp->protLimitTime[PROT_LIMIT_TIME_2] = PROT_OVER_VOLT_LIMIT3_TIME_SEC; + ovp->recyDelta = PROT_OVER_VOLT_RECY_DELTA; +} + +/** + * @brief Initilization lower dc-link voltage protection function. + * @param lvp Lower dc-link voltage protection handle. + * @param ts Ctrl period (s). + * @retval None. + */ +void LVP_Init(LVP_Handle *lvp, float ts) +{ + MCS_ASSERT_PARAM(lvp != NULL); + MCS_ASSERT_PARAM(ts > 0.0f); + + lvp->ts = ts; + LVP_Clear(lvp); + + lvp->protCntLimit = PROT_CNT_LIMIT; + lvp->recyCntLimit = LOWER_VOLT_RECY_CNT_LIMIT; + /* Configuring four levels of lower voltage protection thresholds. */ + lvp->protValThr[PROT_VAL_THRESHOLD_0] = PROT_LOWER_VOLT_POW_DN1; + lvp->protValThr[PROT_VAL_THRESHOLD_1] = PROT_LOWER_VOLT_POW_DN2; + lvp->protValThr[PROT_VAL_THRESHOLD_2] = PROT_LOWER_VOLT_POW_DN3; + lvp->protValThr[PROT_VAL_THRESHOLD_3] = PROT_LOWER_VOLT_POW_OFF; + /* Configure the protection limit time. */ + lvp->protLimitTime[PROT_LIMIT_TIME_0] = PROT_LOWER_VOLT_LIMIT1_TIME_SEC; + lvp->protLimitTime[PROT_LIMIT_TIME_1] = PROT_LOWER_VOLT_LIMIT2_TIME_SEC; + lvp->protLimitTime[PROT_LIMIT_TIME_2] = PROT_LOWER_VOLT_LIMIT3_TIME_SEC; + lvp->recyDelta = PROT_OVER_VOLT_RECY_DELTA; +} + +/** + * @brief Over dc-link voltage protection detection. + * @param ovp Over dc-link voltage protection handle. + * @param motorErrStatus Motor error status. + * @param udc DC-link voltage feedback (V). + * @retval None. + */ +void OVP_Det(OVP_Handle *ovp, MotorErrStatusReg *motorErrStatus, float udc) +{ + MCS_ASSERT_PARAM(ovp != NULL); + MCS_ASSERT_PARAM(motorErrStatus != NULL); + MCS_ASSERT_PARAM(udc > 0.0f); + /* Check if value goes over threshold for continuous cycles. */ + if (udc < ovp->protValThr[PROT_VAL_THRESHOLD_0]) { + ovp->protCnt = 0; + return; + } + + if (ovp->protCnt < ovp->protCntLimit) { + ovp->protCnt++; + return; + } + + /* Check which protection level should be activated. */ + /* Once enter error status, error status can only be changed by recover api. */ + /* protect level: level 4. */ + if (udc >= ovp->protValThr[PROT_VAL_THRESHOLD_3] && ovp->protLevel < PROT_LEVEL_4) { + ovp->protLevel = PROT_LEVEL_4; + motorErrStatus->Bit.overVoltErr = 1; + ovp->protCnt = 0; + return; + } + + /* Protect level: level 3. */ + if (udc >= ovp->protValThr[PROT_VAL_THRESHOLD_2] && ovp->protLevel < PROT_LEVEL_3) { + ovp->protLevel = PROT_LEVEL_3; + motorErrStatus->Bit.overVoltErr = 1; + ovp->protCnt = 0; + ovp->timer = 0.0f; + return; + } + + /* Protect level: level 2. */ + if (udc >= ovp->protValThr[PROT_VAL_THRESHOLD_1] && ovp->protLevel < PROT_LEVEL_2) { + ovp->protLevel = PROT_LEVEL_2; + motorErrStatus->Bit.overVoltErr = 1; + ovp->protCnt = 0; + ovp->timer = 0.0f; + return; + } + + /* Protect level: level 1. */ + if (udc >= ovp->protValThr[PROT_VAL_THRESHOLD_0] && ovp->protLevel < PROT_LEVEL_1) { + ovp->protLevel = PROT_LEVEL_1; + motorErrStatus->Bit.overVoltErr = 1; + ovp->protCnt = 0; + ovp->timer = 0.0f; + return; + } +} + +/** + * @brief Lower dc-link voltage protection detection. + * @param lvp Lower dc-link voltage protection handle. + * @param motorErrStatus Motor error status. + * @param udc DC-link voltage feedback (V). + * @retval None. + */ +void LVP_Det(LVP_Handle *lvp, MotorErrStatusReg *motorErrStatus, float udc) +{ + MCS_ASSERT_PARAM(lvp != NULL); + MCS_ASSERT_PARAM(motorErrStatus != NULL); + MCS_ASSERT_PARAM(udc > 0.0f); + /* Check if value goes over threshold for continuous cycles. */ + if (udc > lvp->protValThr[PROT_VAL_THRESHOLD_0]) { + lvp->protCnt = 0; + return; + } + + if (lvp->protCnt < lvp->protCntLimit) { + lvp->protCnt++; + return; + } + + /* Check which protection level should be activated. */ + /* Once enter error status, error status can only be changed by recover api. */ + /* protect level: level 4. */ + if (udc <= lvp->protValThr[PROT_VAL_THRESHOLD_3] && lvp->protLevel < PROT_LEVEL_4) { + lvp->protLevel = PROT_LEVEL_4; + motorErrStatus->Bit.lowerVoltErr = 1; + lvp->protCnt = 0; + return; + } + + /* Protect level: level 3. */ + if (udc <= lvp->protValThr[PROT_VAL_THRESHOLD_2] && lvp->protLevel < PROT_LEVEL_3) { + lvp->protLevel = PROT_LEVEL_3; + motorErrStatus->Bit.lowerVoltErr = 1; + lvp->protCnt = 0; + lvp->timer = 0.0f; + return; + } + + /* Protect level: level 2. */ + if (udc <= lvp->protValThr[PROT_VAL_THRESHOLD_1] && lvp->protLevel < PROT_LEVEL_2) { + lvp->protLevel = PROT_LEVEL_2; + motorErrStatus->Bit.lowerVoltErr = 1; + lvp->protCnt = 0; + lvp->timer = 0.0f; + return; + } + + /* Protect level: level 1. */ + if (udc <= lvp->protValThr[PROT_VAL_THRESHOLD_0] && lvp->protLevel < PROT_LEVEL_1) { + lvp->protLevel = PROT_LEVEL_1; + motorErrStatus->Bit.lowerVoltErr = 1; + lvp->protCnt = 0; + lvp->timer = 0.0f; + return; + } +} + +/** + * @brief Over dc-link voltage protection execution. + * @param ovp Over dc-link voltage protection handle. + * @param duty Brake loop output duty (0-1). + * @param aptAddr Three-phase APT address pointer. + * @retval None. + */ +void OVP_Exec(OVP_Handle *ovp, float *duty, APT_RegStruct **aptAddr) +{ + MCS_ASSERT_PARAM(ovp != NULL); + MCS_ASSERT_PARAM(duty != NULL); + MCS_ASSERT_PARAM(aptAddr != NULL); + /* According to protect level, take corresponding action. */ + switch (ovp->protLevel) { + /* level 4: brake loop duty maximum. */ + case PROT_LEVEL_4: + *duty = PROT_OVER_VOLT_BRK_DUTY4; + /* Disable three-phase pwm output. */ + ProtSpo_Exec(aptAddr); + break; + + /* level 3: brake loop duty level 3. */ + case PROT_LEVEL_3: + *duty = PROT_OVER_VOLT_BRK_DUTY2; + ovp->timer += ovp->ts; + if (ovp->timer > ovp->protLimitTime[PROT_LIMIT_TIME_2]) { + *duty = PROT_OVER_VOLT_BRK_DUTY3; + } + break; + + /* level 2: brake loop duty level 2. */ + case PROT_LEVEL_2: + *duty = PROT_OVER_VOLT_BRK_DUTY1; + ovp->timer += ovp->ts; + if (ovp->timer > ovp->protLimitTime[PROT_LIMIT_TIME_1]) { + *duty = PROT_OVER_VOLT_BRK_DUTY2; + } + break; + + /* level 1: brake loop duty level 1. */ + case PROT_LEVEL_1: + ovp->timer += ovp->ts; + if (ovp->timer > ovp->protLimitTime[PROT_LIMIT_TIME_0]) { + *duty = PROT_OVER_VOLT_BRK_DUTY1; + } + break; + + /* level 0: take no protection action. */ + case PROT_LEVEL_0: + break; + + default: + break; + } + return; +} + +/** + * @brief Lower dc-link voltage protection execution. + * @param lvp Lower dc-link voltage protection handle. + * @param spdRef Speed Reference (Hz). + * @param aptAddr Three-phase APT address pointer. + * @retval None. + */ +void LVP_Exec(LVP_Handle *lvp, float *spdRef, APT_RegStruct **aptAddr) +{ + MCS_ASSERT_PARAM(lvp != NULL); + MCS_ASSERT_PARAM(spdRef != NULL); + MCS_ASSERT_PARAM(aptAddr != NULL); + /* According to protect level, take corresponding action. */ + switch (lvp->protLevel) { + /* level 4: disable all PWM output. */ + case PROT_LEVEL_4: + /* Disable three-phase pwm output. */ + ProtSpo_Exec(aptAddr); + *spdRef *= 0.0f; + break; + + /* level 3: derate speed reference. */ + case PROT_LEVEL_3: + *spdRef *= PROT_POW_DN2_PCT; + lvp->timer += lvp->ts; + if (lvp->timer > lvp->protLimitTime[PROT_LIMIT_TIME_2]) { + *spdRef *= PROT_POW_DN3_PCT; + } + break; + + /* level 2: derate speed reference. */ + case PROT_LEVEL_2: + *spdRef *= PROT_POW_DN1_PCT; + lvp->timer += lvp->ts; + if (lvp->timer > lvp->protLimitTime[PROT_LIMIT_TIME_1]) { + *spdRef *= PROT_POW_DN2_PCT; + } + break; + + /* level 1: derate speed reference. */ + case PROT_LEVEL_1: + lvp->timer += lvp->ts; + if (lvp->timer > lvp->protLimitTime[PROT_LIMIT_TIME_0]) { + *spdRef *= PROT_POW_DN1_PCT; + } + break; + + /* level 0: take no protection action. */ + case PROT_LEVEL_0: + break; + + default: + break; + } + return; +} + +/** + * @brief Over dc-link voltage protection recovery. + * @param ovp Over dc-link voltage protection handle. + * @param motorErrStatus Motor error status. + * @param udc DC-link voltage (V). + * @retval None. + */ +void OVP_Recy(OVP_Handle *ovp, MotorErrStatusReg *motorErrStatus, float udc) +{ + MCS_ASSERT_PARAM(ovp != NULL); + /* If not under error state, just return without any operation. */ + if (!motorErrStatus->Bit.overVoltErr) { + return; + } + + /* According to protection level, take corresponding recovery action. */ + switch (ovp->protLevel) { + /* level 4 */ + case PROT_LEVEL_4: + if (udc < ovp->protValThr[PROT_VAL_THRESHOLD_3] - ovp->recyDelta) { + ovp->recyCnt++; + if (ovp->recyCnt > ovp->recyCntLimit) { + ovp->protLevel = PROT_LEVEL_3; + ovp->recyCnt = 0; + } + } + break; + + /* level 3 */ + case PROT_LEVEL_3: + /* If the dc-link voltage is less than threshold 2, level-2 protection is restored. */ + if (udc < ovp->protValThr[PROT_VAL_THRESHOLD_2] - ovp->recyDelta) { + ovp->recyCnt++; + if (ovp->recyCnt > ovp->recyCntLimit) { + ovp->protLevel = PROT_LEVEL_2; + ovp->recyCnt = 0; + } + } + break; + + /* level 2 */ + case PROT_LEVEL_2: + /* If the dc-link voltage is less than threshold 1, level-1 protection is restored. */ + if (udc < ovp->protValThr[PROT_VAL_THRESHOLD_1] - ovp->recyDelta) { + ovp->recyCnt++; + if (ovp->recyCnt > ovp->recyCntLimit) { + ovp->protLevel = PROT_LEVEL_1; + ovp->recyCnt = 0; + } + } + break; + + /* level 1 */ + case PROT_LEVEL_1: + /* If the dc-link voltage is less than threshold 0, level-0 protection is restored. */ + if (udc < ovp->protValThr[PROT_VAL_THRESHOLD_0] - ovp->recyDelta) { + ovp->recyCnt++; + if (ovp->recyCnt > ovp->recyCntLimit) { + ovp->protLevel = PROT_LEVEL_0; + ovp->recyCnt = 0; + } + } + break; + /* level 0 Fault-free state. */ + case PROT_LEVEL_0: + motorErrStatus->Bit.overVoltErr = 0; + break; + + default: + break; + } + return; +} + +/** + * @brief Lower dc-link voltage protection recovery. + * @param lvp Lower dc-link voltage protection handle. + * @param motorErrStatus Motor error status. + * @param udc DC-link voltage (V). + * @retval None. + */ +void LVP_Recy(LVP_Handle *lvp, MotorErrStatusReg *motorErrStatus, float udc) +{ + MCS_ASSERT_PARAM(lvp != NULL); + /* If not under error state, just return without any operation. */ + if (!motorErrStatus->Bit.lowerVoltErr) { + return; + } + + /* According to protection level, take corresponding recovery action. */ + switch (lvp->protLevel) { + /* level 4 */ + case PROT_LEVEL_4: + /* If the dc-link voltage is greater than threshold 3, level-3 protection is restored. */ + if (udc > lvp->protValThr[PROT_VAL_THRESHOLD_3] + lvp->recyDelta) { + lvp->recyCnt++; + if (lvp->recyCnt > lvp->recyCntLimit) { + lvp->protLevel = PROT_LEVEL_3; + lvp->recyCnt = 0; + } + } + break; + + /* level 3 */ + case PROT_LEVEL_3: + /* If the dc-link voltage is greater than threshold 2, level-2 protection is restored. */ + if (udc > lvp->protValThr[PROT_VAL_THRESHOLD_2] + lvp->recyDelta) { + lvp->recyCnt++; + if (lvp->recyCnt > lvp->recyCntLimit) { + lvp->protLevel = PROT_LEVEL_2; + lvp->recyCnt = 0; + } + } + break; + + /* level 2 */ + case PROT_LEVEL_2: + /* If the dc-link voltage is greater than threshold 1, level-1 protection is restored. */ + if (udc > lvp->protValThr[PROT_VAL_THRESHOLD_1] + lvp->recyDelta) { + lvp->recyCnt++; + if (lvp->recyCnt > lvp->recyCntLimit) { + lvp->protLevel = PROT_LEVEL_1; + lvp->recyCnt = 0; + } + } + break; + + /* level 1 */ + case PROT_LEVEL_1: + /* If the dc-link voltage is greater than threshold 0, level-0 protection is restored. */ + if (udc > lvp->protValThr[PROT_VAL_THRESHOLD_0] + lvp->recyDelta) { + lvp->recyCnt++; + if (lvp->recyCnt > lvp->recyCntLimit) { + lvp->protLevel = PROT_LEVEL_0; + lvp->recyCnt = 0; + } + } + break; + /* level 0 Fault-free state. */ + case PROT_LEVEL_0: + motorErrStatus->Bit.lowerVoltErr = 0; + break; + + default: + break; + } + return; +} + +/** + * @brief Over dc-link voltage protection error status clear. + * @param ovp Over voltage protection handle. + * @retval None. + */ +void OVP_Clear(OVP_Handle *ovp) +{ + MCS_ASSERT_PARAM(ovp != NULL); + /* Clear the history value. */ + ovp->protCnt = 0; + ovp->protLevel = PROT_LEVEL_0; + ovp->recyCnt = 0; + ovp->timer = 0.0f; +} + +/** + * @brief Lower dc-link voltage protection error status clear. + * @param lvp Lower voltage protection handle. + * @retval None. + */ +void LVP_Clear(LVP_Handle *lvp) +{ + MCS_ASSERT_PARAM(lvp != NULL); + /* Clear the history value. */ + lvp->protCnt = 0; + lvp->protLevel = PROT_LEVEL_0; + lvp->recyCnt = 0; + lvp->timer = 0.0f; +} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_dc_volt_prot.h b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_dc_volt_prot.h new file mode 100644 index 00000000..0435156a --- /dev/null +++ b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_dc_volt_prot.h @@ -0,0 +1,69 @@ +/** + * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_dc_volt_prot.h + * @author MCU Algorithm Team + * @brief This file contains dc-link voltage protection data struct and api declaration. + */ + +/* Define to prevent recursive inclusion ------------------------------------------------------- */ +#ifndef McuMagicTag_MCS_DC_VOLT_PROT_H +#define McuMagicTag_MCS_DC_VOLT_PROT_H + +/* Includes ------------------------------------------------------------------------------------ */ +#include "mcs_prot_cmm.h" +#include "apt_ip.h" + +typedef struct { + unsigned int protCnt; + unsigned int recyCnt; + unsigned int protCntLimit; + unsigned int recyCntLimit; + float protValThr[PROT_VAL_THRESHOLD_NUMS]; /* from low voltage level to high. */ + float protLimitTime[PROT_LIMIT_TIME_NUMS]; /* from level 3 to level 1. */ + float recyDelta; + float timer; + float ts; + PROT_Level protLevel; +} OVP_Handle; + +typedef struct { + unsigned int protCnt; + unsigned int recyCnt; + unsigned int protCntLimit; + unsigned int recyCntLimit; + float protValThr[PROT_VAL_THRESHOLD_NUMS]; /* from high voltage level to low. */ + float protLimitTime[PROT_LIMIT_TIME_NUMS]; /* from level 3 to level 1. */ + float recyDelta; + float timer; + float ts; + PROT_Level protLevel; +} LVP_Handle; + +void OVP_Init(OVP_Handle *ovp, float ts); +void OVP_Det(OVP_Handle *ovp, MotorErrStatusReg *motorErrStatus, float udc); +void OVP_Exec(OVP_Handle *ovp, float *duty, APT_RegStruct **aptAddr); +void OVP_Recy(OVP_Handle *ovp, MotorErrStatusReg *motorErrStatus, float udc); +void OVP_Clear(OVP_Handle *ovp); + +void LVP_Init(LVP_Handle *lvp, float ts); +void LVP_Det(LVP_Handle *lvp, MotorErrStatusReg *motorErrStatus, float udc); +void LVP_Exec(LVP_Handle *lvp, float *spdRef, APT_RegStruct **aptAddr); +void LVP_Recy(LVP_Handle *lvp, MotorErrStatusReg *motorErrStatus, float udc); +void LVP_Clear(LVP_Handle *lvp); + +#endif diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_motor_stalling.c b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_motor_stalling.c new file mode 100644 index 00000000..9dee4d2d --- /dev/null +++ b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_motor_stalling.c @@ -0,0 +1,112 @@ +/** + * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_motor_stalling.c + * @author MCU Algorithm Team + * @brief This file contains motor stalling protection data struct and api definition. + */ + +/* Includes ------------------------------------------------------------------------------------ */ +#include "mcs_motor_stalling.h" +#include "mcs_prot_user_config.h" +#include "mcs_math.h" +#include "mcs_math_const.h" +#include "mcs_assert.h" + +/** + * @brief Initilization motor stalling protection function. + * @param stall Motor stalling handle. + * @param ts Ctrl period (s). + * @param currLimit The current amplitude that triggers fault. (A). + * @param spdLimit The speed amplitude that triggers fault. (Hz). + * @param timeLimit The threshold time that current amplitude over the limit (s). + * @retval None. + */ +void STP_Init(STP_Handle *stall, float ts, float currLimit, float spdLimit, float timeLimit) +{ + MCS_ASSERT_PARAM(stall != NULL); + MCS_ASSERT_PARAM(ts > 0.0f); + MCS_ASSERT_PARAM(currLimit > 0.0f); + MCS_ASSERT_PARAM(spdLimit > 0.0f); + MCS_ASSERT_PARAM(timeLimit > 0.0f); + /* Configuring parameters for stalling detection. */ + stall->ts = ts; + /* Current threshold and speed threshold for stalling fault. */ + stall->currAmpLimit = currLimit; + stall->spdLimit = spdLimit; + stall->timeLimit = timeLimit; + stall->timer = 0.0f; +} + +/** + * @brief Motor stalling detection. + * @param stall Motor stalling handle. + * @param motorErrStatus Motor error status. + * @param spd Speed feedback (Hz). + * @param idq Dq-axis current feedback (A). + * @retval None. + */ +void STP_Det_ByCurrSpd(STP_Handle *stall, MotorErrStatusReg *motorErrStatus, float spd, DqAxis idq) +{ + MCS_ASSERT_PARAM(stall != NULL); + MCS_ASSERT_PARAM(motorErrStatus != NULL); + MCS_ASSERT_PARAM(Abs(spd) >= 0.0f); + /* Calculate current amplitude. */ + float currAmp = Sqrt(idq.d * idq.d + idq.q * idq.q); + float spdAbs = Abs(spd); + /* Check if value goes over threshold for continuous cycles. */ + if (currAmp < stall->currAmpLimit || spdAbs > stall->spdLimit) { + stall->timer = 0.0f; + return; + } + /* Time accumulation. */ + if (stall->timer < stall->timeLimit) { + stall->timer += stall->ts; + return; + } + motorErrStatus->Bit.motorStalling = 1; +} + +/** + * @brief Motor stalling protection execution. + * @param motorErrStatus Motor error status. + * @param aptAddr Three-phase APT address pointer. + * @retval None. + */ +void STP_Exec(MotorErrStatusReg *motorErrStatus, APT_RegStruct **aptAddr) +{ + MCS_ASSERT_PARAM(motorErrStatus != NULL); + MCS_ASSERT_PARAM(aptAddr != NULL); + if (motorErrStatus->Bit.motorStalling == 0) { + return; + } + + /* Disable three-phase pwm output. */ + ProtSpo_Exec(aptAddr); + return; +} + +/** + * @brief Motor stalling protection error status clear. + * @param stall Motor stalling handle. + * @retval None. + */ +void STP_Clear(STP_Handle *stall) +{ + MCS_ASSERT_PARAM(stall != NULL); + stall->timer = 0.0f; +} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_motor_stalling.h b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_motor_stalling.h new file mode 100644 index 00000000..a1d1e4be --- /dev/null +++ b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_motor_stalling.h @@ -0,0 +1,45 @@ +/** + * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_motor_stalling.h + * @author MCU Algorithm Team + * @brief This file contains motor stalling protection data struct and api declaration. + */ + +/* Define to prevent recursive inclusion ------------------------------------- */ +#ifndef McuMagicTag_MCS_MOTOR_STALLING_PROT_H +#define McuMagicTag_MCS_MOTOR_STALLING_PROT_H + +/* Includes ------------------------------------------------------------------------------------ */ +#include "mcs_prot_cmm.h" +#include "apt_ip.h" +#include "mcs_typedef.h" + +typedef struct { + float currAmpLimit; /**< Feedback current higher than this value triggers fault. (A). */ + float spdLimit; /**< Feedback speed lower than this value triggers fault (Hz). */ + float timeLimit; /**< The threshold time that current and speed feedback over ranges (s). */ + float timer; /**< Timer to get speed and current over range time. */ + float ts; /**< Ctrl period (s). */ +} STP_Handle; + +void STP_Init(STP_Handle *stall, float ts, float currLimit, float spdLimit, float timeLimit); +void STP_Det_ByCurrSpd(STP_Handle *stall, MotorErrStatusReg *motorErrStatus, float spd, DqAxis idq); +void STP_Exec(MotorErrStatusReg *motorErrStatus, APT_RegStruct **aptAddr); +void STP_Clear(STP_Handle *stall); + +#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_temp_prot.c b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_temp_prot.c new file mode 100644 index 00000000..184ba4a3 --- /dev/null +++ b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_temp_prot.c @@ -0,0 +1,262 @@ +/** + * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_temp_prot.c + * @author MCU Algorithm Team + * @brief This file contains over temperature protection api definition. + */ + +/* Includes ------------------------------------------------------------------------------------ */ +#include "mcs_temp_prot.h" +#include "mcs_prot_user_config.h" +#include "mcs_assert.h" + +/** + * @brief Initilization over temperation protection function. + * @param otp Over temperature protection handle. + * @param ts Ctrl period. + * @retval None. + */ +void OTP_Init(OTP_Handle *otp, float ts) +{ + MCS_ASSERT_PARAM(otp != NULL); + MCS_ASSERT_PARAM(ts > 0.0f); + otp->ts = ts; + otp->protCntLimit = PROT_CNT_LIMIT; + otp->recyCntLimit = RECY_CNT_LIMIT; + /* Configuring the temperature protection threshold. */ + otp->protValThr[PROT_VAL_THRESHOLD_0] = PROT_OVER_IPM_TEMP_POW_DN1; + otp->protValThr[PROT_VAL_THRESHOLD_1] = PROT_OVER_IPM_TEMP_POW_DN2; + otp->protValThr[PROT_VAL_THRESHOLD_2] = PROT_OVER_IPM_TEMP_POW_DN3; + otp->protValThr[PROT_VAL_THRESHOLD_3] = PROT_OVER_IPM_TEMP_POW_OFF; + /* Configuring the protection limiting time. */ + otp->protLimitTime[PROT_LIMIT_TIME_0] = PROT_OVER_TEMP_LIMIT1_TIME_SEC; + otp->protLimitTime[PROT_LIMIT_TIME_1] = PROT_OVER_TEMP_LIMIT2_TIME_SEC; + otp->protLimitTime[PROT_LIMIT_TIME_2] = PROT_OVER_TEMP_LIMIT3_TIME_SEC; + otp->recyDelta = PROT_OVER_IPM_TEMP_RECY_DELTA; + OTP_Clear(otp); +} + +/** + * @brief Over temperatre protection detection. + * @param otp Over temperature protection handle. + * @param tempErrBit Temperature error status bit. + * @param idq DQ-axis feedback currents. + * @retval None. + */ +void OTP_Det(OTP_Handle *otp, MotorErrStatusReg *motorErrStatus, PROT_ErrBit protBit, float temp) +{ + MCS_ASSERT_PARAM(otp != NULL); + MCS_ASSERT_PARAM(motorErrStatus != NULL); + MCS_ASSERT_PARAM(temp > 0.0f); + /* Check if value goes over threshold for continuous cycles. */ + if (temp < otp->protValThr[PROT_VAL_THRESHOLD_0]) { + otp->protCnt = 0; + ClearBit(&motorErrStatus->all, protBit); + return; + } + + if (otp->protCnt < otp->protCntLimit) { + otp->protCnt++; + return; + } + + /* Check which protection level should be activated. */ + /* Once enter error status, error status can only be changed by recover api. */ + /* protect level: level 4. */ + if (temp >= otp->protValThr[PROT_VAL_THRESHOLD_3] && otp->protLevel < PROT_LEVEL_4) { + otp->protLevel = PROT_LEVEL_4; + SetBit(&motorErrStatus->all, protBit); + otp->protCnt = 0; + return; + } + + /* Protect level: level 3. */ + if (temp >= otp->protValThr[PROT_VAL_THRESHOLD_2] && otp->protLevel < PROT_LEVEL_3) { + otp->protLevel = PROT_LEVEL_3; + SetBit(&motorErrStatus->all, protBit); + otp->protCnt = 0; + otp->timer = 0.0f; + return; + } + + /* Protect level: level 2. */ + if (temp >= otp->protValThr[PROT_VAL_THRESHOLD_1] && otp->protLevel < PROT_LEVEL_2) { + otp->protLevel = PROT_LEVEL_2; + SetBit(&motorErrStatus->all, protBit); + otp->protCnt = 0; + otp->timer = 0.0f; + return; + } + + /* Protect level: level 1. */ + if (temp >= otp->protValThr[PROT_VAL_THRESHOLD_0] && otp->protLevel < PROT_LEVEL_1) { + otp->protLevel = PROT_LEVEL_1; + SetBit(&motorErrStatus->all, protBit); + otp->protCnt = 0; + otp->timer = 0.0f; + return; + } +} + +/** + * @brief Over temperature protection execution. + * @param otp Over temperature protection handle. + * @param spdRef Speed reference (Hz). + * @param aptAddr Three-phase APT address pointer. + * @retval None. + */ +void OTP_Exec(OTP_Handle *otp, float *spdRef, APT_RegStruct **aptAddr) +{ + MCS_ASSERT_PARAM(otp != NULL); + MCS_ASSERT_PARAM(spdRef != NULL); + MCS_ASSERT_PARAM(aptAddr != NULL); + /* According to protect level, take corresponding action. */ + switch (otp->protLevel) { + /* level 4: disable all PWM output. */ + case PROT_LEVEL_4: + /* Disable three-phase pwm output. */ + ProtSpo_Exec(aptAddr); + *spdRef *= 0.0f; + break; + + /* level 3: derate speed reference. */ + case PROT_LEVEL_3: + *spdRef *= PROT_POW_DN2_PCT; + otp->timer += otp->ts; + if (otp->timer > otp->protLimitTime[PROT_LIMIT_TIME_2]) { + *spdRef *= PROT_POW_DN3_PCT; + } + break; + + /* level 2: derate speed reference. */ + case PROT_LEVEL_2: + /* Reducte motor speed to level 2 */ + *spdRef *= PROT_POW_DN1_PCT; + otp->timer += otp->ts; + if (otp->timer > otp->protLimitTime[PROT_LIMIT_TIME_1]) { + *spdRef *= PROT_POW_DN2_PCT; + } + break; + + /* level 0: take no protection action. */ + case PROT_LEVEL_0: + break; + + case PROT_LEVEL_1: + /* Reducte motor speed to level 0 */ + otp->timer += otp->ts; + if (otp->timer > otp->protLimitTime[PROT_LIMIT_TIME_0]) { + /* level 1: derate speed reference. */ + *spdRef *= PROT_POW_DN1_PCT; + } + break; + + default: + break; + } + return; +} + +/** + * @brief Over temperature protection recovery. + * @param otp Over temperature protection handle. + * @param tempErrBit Temperature error status bit. + * @param temp Temperature (celsius). + * @retval None. + */ +void OTP_Recy(OTP_Handle *otp, MotorErrStatusReg *motorErrStatus, PROT_ErrBit protBit, float temp) +{ + MCS_ASSERT_PARAM(otp != NULL); + MCS_ASSERT_PARAM(motorErrStatus != NULL); + MCS_ASSERT_PARAM(temp > 0.0f); + /* If not under error state, just return without any operation. */ + if (otp->protLevel == PROT_LEVEL_0) { + motorErrStatus->all &= (~(1 >> protBit)); + return; + } + + /* According to protection level, take corresponding recovery action. */ + switch (otp->protLevel) { + /* level 4 */ + case PROT_LEVEL_4: + /* If the temperature is less than threshold 3, level-3 protection is restored. */ + if (temp < otp->protValThr[PROT_VAL_THRESHOLD_3] - otp->recyDelta) { + otp->recyCnt++; + if (otp->recyCnt > otp->recyCntLimit) { + otp->protLevel = PROT_LEVEL_3; + otp->recyCnt = 0; + } + } + break; + + /* level 3 */ + case PROT_LEVEL_3: + /* If the temperature is less than threshold 2, level-2 protection is restored. */ + if (temp < otp->protValThr[PROT_VAL_THRESHOLD_2] - otp->recyDelta) { + otp->recyCnt++; + if (otp->recyCnt > otp->recyCntLimit) { + otp->protLevel = PROT_LEVEL_2; + otp->recyCnt = 0; + } + } + break; + + /* level 2 */ + case PROT_LEVEL_2: + /* If the temperature is less than threshold 1, level-1 protection is restored. */ + if (temp < otp->protValThr[PROT_VAL_THRESHOLD_1] - otp->recyDelta) { + otp->recyCnt++; + if (otp->recyCnt > otp->recyCntLimit) { + otp->protLevel = PROT_LEVEL_1; + otp->recyCnt = 0; + } + } + break; + + /* level 1 */ + case PROT_LEVEL_1: + /* If the temperature is less than threshold 0, level-0 protection is restored. */ + if (temp < otp->protValThr[PROT_VAL_THRESHOLD_0] - otp->recyDelta) { + otp->recyCnt++; + if (otp->recyCnt > otp->recyCntLimit) { + otp->protLevel = PROT_LEVEL_0; + otp->recyCnt = 0; + } + } + break; + + default: + break; + } + return; +} + +/** + * @brief Over temperature protection error status clear. + * @param otp Over temperature protection handle. + * @retval None. + */ +void OTP_Clear(OTP_Handle *otp) +{ + MCS_ASSERT_PARAM(otp != NULL); + /* Clear the history value. */ + otp->protCnt = 0; + otp->protLevel = PROT_LEVEL_0; + otp->recyCnt = 0; + otp->timer = 0.0f; +} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_temp_prot.h b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_temp_prot.h new file mode 100644 index 00000000..2b9d99a7 --- /dev/null +++ b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_temp_prot.h @@ -0,0 +1,49 @@ +/** + * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_temp_prot.h + * @author MCU Algorithm Team + * @brief his file contains over temperature protection api declaration. + */ + +/* Define to prevent recursive inclusion ------------------------------------- */ +#ifndef McuMagicTag_MCS_TEMP_PROT_H +#define McuMagicTag_MCS_TEMP_PROT_H + +/* Includes ------------------------------------------------------------------------------------ */ +#include "mcs_prot_cmm.h" +#include "apt_ip.h" + +typedef struct { + unsigned int protCnt; + unsigned int recyCnt; + unsigned int protCntLimit; + unsigned int recyCntLimit; + float protValThr[PROT_VAL_THRESHOLD_NUMS]; /* from low current level to high. */ + float protLimitTime[PROT_LIMIT_TIME_NUMS]; /* from level 3 to level 1. */ + float recyDelta; + float ts; + float timer; + PROT_Level protLevel; +} OTP_Handle; + +void OTP_Init(OTP_Handle *otp, float ts); +void OTP_Det(OTP_Handle *otp, MotorErrStatusReg *motorErrStatus, PROT_ErrBit protBit, float temp); +void OTP_Exec(OTP_Handle *otp, float *spdRef, APT_RegStruct **aptAddr); +void OTP_Recy(OTP_Handle *otp, MotorErrStatusReg *motorErrStatus, PROT_ErrBit protBit, float temp); +void OTP_Clear(OTP_Handle *otp); +#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/readme.md b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/readme.md new file mode 100644 index 00000000..98b2a068 --- /dev/null +++ b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/readme.md @@ -0,0 +1,10 @@ +# pmsm_sensorless_2shunt_foc + +**【功能描述】** ++ 基于ECMCU105H/ECBMCU201MPC单板的单电机双电阻采样Foc应用 + +**【环境要求】** ++ 所有单板电源改制为演示用的12V低压,电机选用Gimbal GBM2804H-100T + +**【IDE配置方法】** ++ chipConfig中的Sample栏目里面选中pmsm sensorless 2shunt foc示例,然后点击生成代码即可 diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/src/mcs_carrier.c b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/src/mcs_carrier.c new file mode 100644 index 00000000..9fc8afac --- /dev/null +++ b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/src/mcs_carrier.c @@ -0,0 +1,144 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_carrier.c + * @author MCU Algorithm Team + * @brief This file provides carrier interrupt application for motor control. + */ + +#include "mcs_carrier.h" +#include "mcs_math.h" +#include "typedefs.h" +#include "mcs_assert.h" +#include "mcs_user_config.h" +#include "mcs_ctlmode_config.h" + +/** + * @brief Synchronous rotation coordinate system angle. + * @param mtrCtrl The motor control handle. + * @retval None. + */ +static void MCS_SyncCoorAngle(MTRCTRL_Handle *mtrCtrl) +{ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + /* Synchronous rotation coordinate system angle. */ + switch (mtrCtrl->stateMachine) { + case FSM_STARTUP: + /* Current ramp angle is 0. */ + if (mtrCtrl->startup.stage == STARTUP_STAGE_CURR) { + mtrCtrl->axisAngle = 0; + } else if (mtrCtrl->startup.stage == STARTUP_STAGE_SPD) { /* IF control phase angle self-addition. */ + mtrCtrl->axisAngle = IF_CurrAngleCalc(&mtrCtrl->ifCtrl, mtrCtrl->spdRefHz); + } else if (mtrCtrl->startup.stage == STARTUP_STAGE_SWITCH) { /* Switch Angle */ + mtrCtrl->axisAngle = mtrCtrl->smo.elecAngle; + } + break; + + case FSM_RUN: + mtrCtrl->axisAngle = mtrCtrl->smo.elecAngle; + break; + + default: + mtrCtrl->axisAngle = 0; + break; + } +} + +/** + * @brief PWM waveform setting and sampling point setting for a single resistors and dual resistors. + * @param mtrCtrl The motor control handle. + * @retval None. + */ +static void MCS_PwmAdcSet(MTRCTRL_Handle *mtrCtrl) +{ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + SampleMode sampleMode = mtrCtrl->sampleMode; + /* Set the duty cycle according to the sampling mode. */ + if (sampleMode == DUAL_RESISTORS) { + SVPWM_Exec(&mtrCtrl->sv, &mtrCtrl->vabRef, &mtrCtrl->dutyUvw); + mtrCtrl->setPwmDutyCb(&mtrCtrl->dutyUvw, &mtrCtrl->dutyUvw); + } else if (sampleMode == SINGLE_RESISTOR) { + R1SVPWM_Exec(&mtrCtrl->r1Sv, &mtrCtrl->vabRef, &mtrCtrl->dutyUvwLeft, &mtrCtrl->dutyUvwRight); + mtrCtrl->setPwmDutyCb(&mtrCtrl->dutyUvwLeft, &mtrCtrl->dutyUvwRight); + /* The ADC sampling point position needs to be set based on the phase shift of a single resistors. */ + mtrCtrl->setADCTriggerTimeCb(mtrCtrl->r1Sv.samplePoint[0] * mtrCtrl->aptMaxcntCmp, \ + mtrCtrl->r1Sv.samplePoint[1] * mtrCtrl->aptMaxcntCmp); + } +} + +/** + * @brief Carrier interrupt function. + * @param mtrCtrl The motor control handle. + * @retval None. + */ +void MCS_CarrierProcess(MTRCTRL_Handle *mtrCtrl) +{ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + UvwAxis *currUvw = &mtrCtrl->currUvw; + AlbeAxis *currAlbe = &mtrCtrl->iabFbk; + AlbeAxis *vab = &mtrCtrl->vabRef; + SampleMode sampleMode = mtrCtrl->sampleMode; + /* sample mode verify */ + if (sampleMode > SINGLE_RESISTOR || mtrCtrl->setADCTriggerTimeCb == NULL) { + return; + } + /* param verify */ + if (mtrCtrl->setPwmDutyCb == NULL || mtrCtrl->readCurrUvwCb == NULL) { + return; + } + /* Read the three-phase current value. */ + mtrCtrl->readCurrUvwCb(currUvw); + /* Clark Calc */ + ClarkeCalc(currUvw, currAlbe); + /* Smo observation */ + if (mtrCtrl->obserType == FOC_OBSERVERTYPE_SMO1TH) { + /* Smo observation */ + FOSMO_Exec(&mtrCtrl->smo, currAlbe, vab, mtrCtrl->spdRefHz); + } else if (mtrCtrl->obserType == FOC_OBSERVERTYPE_SMO4TH) { + /* Smo4th observation */ + SMO4TH_Exec(&mtrCtrl->smo4th, currAlbe, vab); + mtrCtrl->smo.spdEst = mtrCtrl->smo4th.spdEst; + mtrCtrl->smo.elecAngle = mtrCtrl->smo4th.elecAngle; + } + /* Synchronization angle */ + MCS_SyncCoorAngle(mtrCtrl); + + /* Park transformation */ + ParkCalc(currAlbe, mtrCtrl->axisAngle, &mtrCtrl->idqFbk); + + /* statemachine */ + switch (mtrCtrl->stateMachine) { + case FSM_STARTUP: + case FSM_RUN: + CURRCTRL_Exec(&mtrCtrl->currCtrl, &mtrCtrl->vdqRef, mtrCtrl->smo.spdEst, 0); + InvParkCalc(&mtrCtrl->vdqRef, mtrCtrl->axisAngle, vab); + MCS_PwmAdcSet(mtrCtrl); + break; + + case FSM_CAP_CHARGE: + case FSM_CLEAR: + case FSM_IDLE: + mtrCtrl->smo4th.spdEst = 0.0f; + break; + + default: + vab->alpha = 0.0f; + vab->beta = 0.0f; + MCS_PwmAdcSet(mtrCtrl); + break; + } +} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/src/mcs_motor_process.c b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/src/mcs_motor_process.c new file mode 100644 index 00000000..f754d001 --- /dev/null +++ b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/src/mcs_motor_process.c @@ -0,0 +1,951 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_motor_process.c + * @author MCU Algorithm Team + * @brief This file provides motor application. + * @details Single FOC application based on the ECMCU105H/ECBMCU201MPC board + * 1) Motor model is Gimbal GBM2804H-100T. + * 2) Select the pmsm sensorless 2shunt foc example in the sample column + of chipConfig and click Generate Code. + * 3) It's power supply must be changed to 12V. + */ +#include "main.h" +#include "mcs_user_config.h" +#include "mcs_math.h" +#include "hmi_module.h" +#include "mcs_ctlmode_config.h" +#include "mcs_prot_user.h" +#include "mcs_prot_user_config.h" +#include "mcs_math_const.h" +#include "mcs_motor_process.h" +#include "mcs_chip_config.h" +#include + +/*------------------------------- Macro Definition -----------------------------------------------*/ +#define US_PER_MS 1000 +#define ANGLE_RANGE_ABS 65536 +#define ANGLE_360_F 65536.0f /* 0 - 65536 indicates 0 to 360. */ +#define APT_FULL_DUTY 1.0f +#define TEMP_3 3.0f +#define TEMP_15 15.0f +#define TEMP_30 30.0f +#define TEMP_45 45.0f +#define TEMP_60 60.0f +#define TEMP_RES_15 78.327f +#define TEMP_RES_30 36.776f +#define TEMP_RES_45 18.301f +#define TEMP_RES_60 9.607f +#define CNT_10 10 +#define CNT_5000 5000 +#define LEVEL_4 4 +#define MOTOR_START_DELAY 2 +#define ADC_READINIT_DELAY 1 +#define ADC_READINIT_TIMES 20 +#define ADC_TRIMVALUE_MIN 1800.0f +#define ADC_TRIMVALUE_MAX 2200.0f +/*------------------------------- Param Definition -----------------------------------------------*/ +/* Motor parameters. */ +/* Np, Rs, Ld, Lq, Psif, J, Nmax, Currmax, PPMR, zShift */ +static MOTOR_Param g_motorParam = MOTORPARAM_DEFAULTS; +static APT_RegStruct* g_apt[PHASE_MAX_NUM] = {APT_U, APT_V, APT_W}; +/* Motor control handle */ +static MTRCTRL_Handle g_mc = {0}; + +/* Motor speed loop PI param. */ +static void SPDCTRL_InitWrapper(SPDCTRL_Handle *spdHandle, float ts) +{ + /* Speed loop param assignment. */ + PI_Param spdPi = { + .kp = SPD_KP, + .ki = SPD_KI, + .lowerLim = SPD_LOWERLIM, + .upperLim = SPD_UPPERLIM, + }; + /* Speed loop param init. */ + SPDCTRL_Init(spdHandle, &g_motorParam, spdPi, ts); +} + +/* Motor current Loop PI param. */ +static void CURRCTRL_InitWrapper(CURRCTRL_Handle *currHandle, DqAxis *idqRef, DqAxis *idqFbk, float ts) +{ + /* Axis-D current loop param assignment. */ + PI_Param dCurrPi = { + .kp = CURRDAXIS_KP, + .ki = CURRDAXIS_KI, + .lowerLim = CURR_LOWERLIM, + .upperLim = CURR_UPPERLIM, + }; + /* Axis-Q current loop param assignment. */ + PI_Param qCurrPi = { + .kp = CURRQAXIS_KP, + .ki = CURRQAXIS_KI, + .lowerLim = CURR_LOWERLIM, + .upperLim = CURR_UPPERLIM, + }; + /* Current loop param init. */ + CURRCTRL_Init(currHandle, &g_motorParam, idqRef, idqFbk, dCurrPi, qCurrPi, ts); +} + +/* First order smo param. */ +static void FOSMO_InitWrapper(FOSMO_Handle *fosmo, float ts) +{ + /* Smo param assignment. */ + FOSMO_Param fosmoParam = { + .gain = FOSMO_GAIN, + .lambda = FOSMO_LAMBDA, + .fcEmf = FOSMO_EMF_CUTOFF_FREQ, + .fcLpf = SPEED_FILTER_CUTOFF_FREQUENCY, + .pllBdw = FOSMO_PLL_BDW, + }; + /* Init smo param. */ + FOSMO_Init(fosmo, fosmoParam, &g_motorParam, ts); +} + +/* Smo4th param. */ +static void SMO4TH_InitWrapper(SMO4TH_Handle *smo4TH) +{ + /* Smo4th param assignment. */ + SMO4TH_Param smo4thParam = { + .kd = SMO4TH_KD, + .kq = SMO4TH_KQ, + .pllBdw = SMO4TH_PLL_BDW, + .fcLpf = SMO4TH_SPD_FILTER_CUTOFF_FREQ, + }; + /* Init smo param. */ + SMO4TH_Init(smo4TH, smo4thParam, &g_motorParam, CTRL_CURR_PERIOD); +} + +/*------------------------------- Function Definition -----------------------------------------------*/ +/** + * @brief Initialzer of system tick. + * @param mtrCtrl Motor control struct handle. + * @retval None. + */ +static void TimerTickInit(MTRCTRL_Handle *mtrCtrl) +{ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + mtrCtrl->sysTickCnt = 0; + mtrCtrl->msTickNum = US_PER_MS / SYSTICK_PERIOD_US; + mtrCtrl->capChargeTickNum = (INV_CAP_CHARGE_MS * US_PER_MS / SYSTICK_PERIOD_US); +} + +/** + * @brief Init motor control task. + * @retval None. + */ +static void TSK_Init(void) +{ + g_mc.motorStateFlag = 0; + g_mc.uartHeartDetCnt = 0; + g_mc.uartTimeStamp = 0; + g_mc.stateMachine = FSM_IDLE; + g_mc.currCtrlPeriod = CTRL_CURR_PERIOD; /* Init current controller */ + g_mc.aptMaxcntCmp = g_apt0.waveform.timerPeriod; + g_mc.sampleMode = DUAL_RESISTORS; + g_mc.obserType = FOC_OBSERVERTYPE_SMO4TH; /* Init foc observe mode */ + g_mc.controlMode = FOC_CONTROLMODE_SPEED; /* Init motor control mode */ + g_mc.adcCurrCofe = ADC_CURR_COFFI; + g_mc.spdAdjustMode = CUST_SPEED_ADJUST; + g_mc.uartConnectFlag = DISCONNECT; + g_mc.spdCmdHz = USER_MIN_SPD_HZ; /* Motor initialization speed */ + + g_mc.adc0Compensate = ADC0COMPENSATE; /* Phase-u current init adc shift trim value */ + g_mc.adc1Compensate = ADC1COMPENSATE; /* Phase-w current init adc shift trim value */ + + IF_Init(&g_mc.ifCtrl, CTRL_IF_CURR_AMP_A, USER_CURR_SLOPE, CTRL_SYSTICK_PERIOD, CTRL_CURR_PERIOD); + RMG_Init(&g_mc.spdRmg, CTRL_SYSTICK_PERIOD, USER_SPD_SLOPE); /* Init speed slope */ + MtrParamInit(&g_mc.mtrParam, g_motorParam); + + TimerTickInit(&g_mc); + if (g_mc.sampleMode == DUAL_RESISTORS) { + SVPWM_Init(&g_mc.sv, INV_VOLTAGE_BUS * ONE_DIV_SQRT3); /* Dual resistors SVPWM init. */ + } else if (g_mc.sampleMode == SINGLE_RESISTOR) { + /* Single resistor SVPWM init. */ + R1SVPWM_Init(&g_mc.r1Sv, INV_VOLTAGE_BUS * ONE_DIV_SQRT3, SAMPLE_POINT_SHIFT, SAMPLE_WINDOW_DUTY); + } + + SPDCTRL_InitWrapper(&g_mc.spdCtrl, CTRL_SYSTICK_PERIOD); + CURRCTRL_InitWrapper(&g_mc.currCtrl, &g_mc.idqRef, &g_mc.idqFbk, CTRL_CURR_PERIOD); + FOSMO_InitWrapper(&g_mc.smo, CTRL_CURR_PERIOD); + SMO4TH_InitWrapper(&g_mc.smo4th); + + STARTUP_Init(&g_mc.startup, USER_SWITCH_SPDBEGIN_HZ, USER_SWITCH_SPDBEGIN_HZ + TEMP_3); + + MotorProt_Init(&g_mc.prot); /* Init protect state comond */ + OCP_Init(&g_mc.prot.ocp, CTRL_CURR_PERIOD); + OVP_Init(&g_mc.prot.ovp, CTRL_SYSTICK_PERIOD); + LVP_Init(&g_mc.prot.lvp, CTRL_SYSTICK_PERIOD); + OTP_Init(&g_mc.prot.otp, CTRL_SYSTICK_PERIOD); + STP_Init(&g_mc.prot.stall, CTRL_SYSTICK_PERIOD, PROT_STALLING_CURR_AMP_LIMIT, + PROT_STALLING_SPD_LIMIT, PROT_STALLING_TIME_LIMIT); +} + +/** + * @brief Clear historical values of all controller before start-up. + * @param mtrCtrl The motor control handle. + * @retval None. + */ +static void ClearBeforeStartup(MTRCTRL_Handle *mtrCtrl) +{ + /* Verify Parameters */ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + /* The initial angle is 0. */ + mtrCtrl->axisAngle = 0; + + mtrCtrl->spdRefHz = 0.0f; + /* The initial dq-axis reference current is 0. */ + mtrCtrl->idqRef.d = 0.0f; + mtrCtrl->idqRef.q = 0.0f; + + mtrCtrl->vdqRef.d = 0.0f; + mtrCtrl->vdqRef.q = 0.0f; + /* Clear Duty Cycle Value. The initial duty cycle is 0.5. */ + mtrCtrl->dutyUvwLeft.u = 0.5f; + mtrCtrl->dutyUvwLeft.v = 0.5f; + mtrCtrl->dutyUvwLeft.w = 0.5f; + mtrCtrl->dutyUvwRight.u = 0.5f; + mtrCtrl->dutyUvwRight.v = 0.5f; + mtrCtrl->dutyUvwRight.w = 0.5f; + + mtrCtrl->prot.motorErrStatus.all = 0x00; + + RMG_Clear(&mtrCtrl->spdRmg); /* Clear the history value of speed slope control */ + CURRCTRL_Clear(&mtrCtrl->currCtrl); + IF_Clear(&mtrCtrl->ifCtrl); + SPDCTRL_Clear(&mtrCtrl->spdCtrl); + FOSMO_Clear(&mtrCtrl->smo); + SMO4TH_Clear(&mtrCtrl->smo4th); + STARTUP_Clear(&mtrCtrl->startup); + R1SVPWM_Clear(&mtrCtrl->r1Sv); + + OTP_Clear(&mtrCtrl->prot.otp); + OCP_Clear(&mtrCtrl->prot.ocp); + OVP_Clear(&mtrCtrl->prot.ovp); + LVP_Clear(&mtrCtrl->prot.lvp); +} + +/** + * @brief To set the comparison value of the IGBT single-resistance ADC sampling trigger position. + * @param aptx The APT register struct handle. + * @param cntCmpA A Count compare reference of time-base counter. + * @param cntCmpB B Count compare reference of time-base counter. + * @param maxCntCmp Maximum Comparison Value + * @retval None. + */ +static void MCS_SetAdcCompareR1(APT_RegStruct *aptx, unsigned short cntCmpA, + unsigned short cntCmpB, unsigned short maxCntCmp) +{ + unsigned short tmp; + /* Sets the A Count compare reference of time-base counter. */ + tmp = (unsigned short)Clamp((float)(cntCmpA), (float)(maxCntCmp - 1), 1.0f); + DCL_APT_SetCounterCompare(aptx, APT_COMPARE_REFERENCE_A, tmp); + /* Sets the B Count compare reference of time-base counter. */ + tmp = (unsigned short)Clamp((float)(cntCmpB), (float)(maxCntCmp - 1), 1.0f); + DCL_APT_SetCounterCompare(aptx, APT_COMPARE_REFERENCE_B, tmp); +} + +/** + * @brief Open the three-phase lower pipe. + * @param aptAddr Three-phase APT address pointer. + * @param maxDutyCnt Max duty count. + * @retval None. + */ +static void AptTurnOnLowSidePwm(APT_RegStruct **aptAddr, unsigned short maxDutyCnt) +{ + MCS_ASSERT_PARAM(aptAddr != NULL); + MCS_ASSERT_PARAM(maxDutyCnt != 0); + unsigned short dutyCnt; + dutyCnt = (unsigned short)(maxDutyCnt * APT_FULL_DUTY); + /* Open the three-phase lower pipe */ + for (unsigned int i = 0; i < PHASE_MAX_NUM; i++) { + APT_RegStruct *aptx = (APT_RegStruct *)(aptAddr[i]); + DCL_APT_SetCounterCompare(aptx, APT_COMPARE_REFERENCE_C, dutyCnt); + DCL_APT_SetCounterCompare(aptx, APT_COMPARE_REFERENCE_D, dutyCnt); + } +} + +/** + * @brief Enable three-phase pwm output. + * @param aptAddr Three-phase APT address pointer. + * @retval None. + */ +static void MotorPwmOutputEnable(APT_RegStruct **aptAddr) +{ + MCS_ASSERT_PARAM(aptAddr != NULL); + /* Enable three-phase pwm output */ + for (unsigned int i = 0; i < PHASE_MAX_NUM; i++) { + APT_RegStruct *aptx = (APT_RegStruct *)(aptAddr[i]); + aptx->PG_OUT_FRC.BIT.rg_pga_frc_en = BASE_CFG_UNSET; + aptx->PG_OUT_FRC.BIT.rg_pgb_frc_en = BASE_CFG_UNSET; + } +} + +/** + * @brief Disable three-phase pwm output. + * @param aptAddr Three-phase APT address pointer. + * @retval None. + */ +static void MotorPwmOutputDisable(APT_RegStruct **aptAddr) +{ + MCS_ASSERT_PARAM(aptAddr != NULL); + /* Disable three-phase pwm output. */ + for (unsigned int i = 0; i < PHASE_MAX_NUM; i++) { + APT_RegStruct *aptx = (APT_RegStruct *)(aptAddr[i]); + aptx->PG_OUT_FRC.BIT.rg_pga_frc_en = BASE_CFG_SET; + aptx->PG_OUT_FRC.BIT.rg_pgb_frc_en = BASE_CFG_SET; + DCL_APT_ForcePWMOutputLow(aptx); + } +} + +/** + * @brief Smo IF angle difference calculation. + * @param smoElecAngle Smo electrical angle. + * @param ifCtrlAngle IF control angle. + * @retval signed short angle difference. + */ +static float SmoIfAngleDiffCalc(float smoElecAngle, float ifCtrlAngle) +{ + float diff = AngleSub(smoElecAngle, ifCtrlAngle); + /* Smo IF angle difference calculation */ + return diff; +} + +/** + * @brief Construct a new mcs startupswitch object. + * @param mtrCtrl The motor control handle. + * @retval None. + */ +static void MCS_StartupSwitch(MTRCTRL_Handle *mtrCtrl) +{ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + STARTUP_Handle *startup = &mtrCtrl->startup; + DqAxis *idqRef = &mtrCtrl->idqRef; + float iftargetAmp = mtrCtrl->ifCtrl.targetAmp; + float spdRefHz = mtrCtrl->spdRefHz; + + switch (startup->stage) { + case STARTUP_STAGE_CURR: + if (mtrCtrl->ifCtrl.curAmp >= iftargetAmp) { + /* Stage change */ + idqRef->q = iftargetAmp; + startup->stage = STARTUP_STAGE_SPD; + } else { + /* current amplitude increase */ + idqRef->q = IF_CurrAmpCalc(&mtrCtrl->ifCtrl); + spdRefHz = 0.0f; + } + break; + case STARTUP_STAGE_SPD: + /* current frequency increase */ + if (Abs(spdRefHz) >= startup->spdBegin) { + /* Stage change */ + startup->stage = STARTUP_STAGE_SWITCH; + TrigVal localTrigVal; + TrigCalc(&localTrigVal, SmoIfAngleDiffCalc(mtrCtrl->smo.elecAngle, mtrCtrl->ifCtrl.angle)); + idqRef->d = 0.0f; + mtrCtrl->spdCtrl.spdPi.integral = iftargetAmp * localTrigVal.cos; + } else { + /* Speed rmg */ + spdRefHz = RMG_Exec(&mtrCtrl->spdRmg, mtrCtrl->spdCmdHz); + } + break; + + case STARTUP_STAGE_SWITCH: + /* Switch from IF to SMO */ + spdRefHz = RMG_Exec(&mtrCtrl->spdRmg, mtrCtrl->spdCmdHz); + idqRef->q = SPDCTRL_Exec(&mtrCtrl->spdCtrl, mtrCtrl->spdRefHz, mtrCtrl->smo.spdEst); + /* Transitional stage, if current reference speed > critical speed, change to next stage */ + if (spdRefHz >= startup->spdBegin + TEMP_3) { + /* Stage change */ + mtrCtrl->stateMachine = FSM_RUN; + } + break; + + default: + break; + } + mtrCtrl->spdRefHz = spdRefHz; +} + +/** + * @brief Pre-processing of motor status. + * @param statusReg System status. + * @param stateMachine Motor Control Status. + * @retval None. + */ +static void MotorStatePerProc(SysStatusReg *statusReg, volatile FsmState *stateMachine) +{ + MCS_ASSERT_PARAM(statusReg != NULL); + MCS_ASSERT_PARAM(stateMachine != NULL); + /* Get system status */ + if (SysIsError(statusReg)) { + *stateMachine = FSM_FAULT; + } + if (SysGetCmdStop(statusReg)) { + SysCmdStopClr(statusReg); + *stateMachine = FSM_STOP; + } +} + +/** + * @brief Check over current status. + * @param statusReg System status. + * @param stateMachine Motor Control Status. + * @retval None. + */ +static void CheckOverCurrentState(SysStatusReg *statusReg, FsmState *stateMachine) +{ + MCS_ASSERT_PARAM(statusReg != NULL); + MCS_ASSERT_PARAM(stateMachine != NULL); + /* check systerm error status */ + if (SysIsError(statusReg) == false) { + *stateMachine = FSM_IDLE; + } +} + +/** + * @brief Check bootstrap capacitor charge time. + * @param mtrCtrl The motor control handle. + * @param stateMachine Motor Control Status. + * @retval None. + */ +static void CheckBootstrpCapChargeTime(MTRCTRL_Handle *mtrCtrl, FsmState *stateMachine) +{ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + MCS_ASSERT_PARAM(stateMachine != NULL); + mtrCtrl->sysTickCnt++; + /* check bootstrap capacitor charge time */ + if (mtrCtrl->sysTickCnt == mtrCtrl->capChargeTickNum) { + *stateMachine = FSM_CLEAR; + } +} + +/** + * @brief Check systerm cmd start signal. + * @param mtrCtrl The motor control handle. + * @param aptAddr Three-phase APT address pointer. + * @param statusReg System status. + * @param stateMachine Motor Control Status. + * @retval None. + */ +static void CheckSysCmdStart(MTRCTRL_Handle *mtrCtrl, + APT_RegStruct **aptAddr, + SysStatusReg *statusReg, + FsmState *stateMachine) +{ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + MCS_ASSERT_PARAM(aptAddr != NULL); + MCS_ASSERT_PARAM(statusReg != NULL); + MCS_ASSERT_PARAM(stateMachine != NULL); + /* check start cmd */ + if (SysGetCmdStart(statusReg)) { + SysRunningSet(statusReg); + SysCmdStartClr(statusReg); + mtrCtrl->sysTickCnt = 0; + *stateMachine = FSM_CAP_CHARGE; + /* Preparation for charging the bootstrap capacitor. */ + AptTurnOnLowSidePwm(aptAddr, mtrCtrl->aptMaxcntCmp); + /* Out put pwm */ + MotorPwmOutputEnable(aptAddr); + } +} + +/** + * @brief System timer tick task. + * @param mtrCtrl The motor control handle. + * @param aptAddr Three-phase APT address pointer. + * @retval None. + */ +static void TSK_SystickIsr(MTRCTRL_Handle *mtrCtrl, APT_RegStruct **aptAddr) +{ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + MCS_ASSERT_PARAM(aptAddr != NULL); + SysStatusReg *statusReg = &mtrCtrl->statusReg; + FsmState *stateMachine = &mtrCtrl->stateMachine; + mtrCtrl->msTickCnt++; + /* Pre-processing of motor status. */ + MotorStatePerProc(statusReg, stateMachine); + /* statemachine */ + switch (*stateMachine) { + case FSM_IDLE: + /* Set smo estimate speed before motor start-up */ + g_mc.smo.spdEst = 0.0f; + CheckSysCmdStart(mtrCtrl, aptAddr, statusReg, stateMachine); + break; + case FSM_CAP_CHARGE: + /* Bootstrap Capacitor Charging Timing */ + CheckBootstrpCapChargeTime(mtrCtrl, stateMachine); + break; + /* Clear parameter before start */ + case FSM_CLEAR: + ClearBeforeStartup(mtrCtrl); + *stateMachine = FSM_STARTUP; + break; + case FSM_STARTUP: + MCS_StartupSwitch(mtrCtrl); + break; + case FSM_RUN: + /* Speed ramp control */ + mtrCtrl->spdRefHz = RMG_Exec(&mtrCtrl->spdRmg, mtrCtrl->spdCmdHz); + /* Speed loop control */ + mtrCtrl->idqRef.q = SPDCTRL_Exec(&mtrCtrl->spdCtrl, mtrCtrl->spdRefHz, mtrCtrl->smo.spdEst); + break; + case FSM_STOP: + mtrCtrl->spdRefHz = 0.0f; + MotorPwmOutputDisable(aptAddr); + SysRunningClr(statusReg); + *stateMachine = FSM_IDLE; + break; + case FSM_FAULT: /* Overcurrent state */ + CheckOverCurrentState(statusReg, stateMachine); + break; + default: + break; + } +} + +/** + * @brief Read the ADC initialize bias trim value. + * @param mtrCtrl The motor control handle. + * @retval None. + */ +static void TrimInitAdcShiftValue(MTRCTRL_Handle *mtrCtrl) +{ + float adc0SampleTemp = 0.0f; /* Current bias value for temp store */ + float adc1SampleTemp = 0.0f; + float adc0TempSum = 0.0f; + float adc1TempSum = 0.0f; /* Current bias sum value for 20 times */ + float adcSampleTimes = 0.0f; /* ADC sample times */ + for (int i = 0; i < ADC_READINIT_TIMES; i++) { + adc0SampleTemp = (float)HAL_ADC_GetConvResult(&ADCU_HANDLE, ADCUSOCNUM); + adc1SampleTemp = (float)HAL_ADC_GetConvResult(&ADCW_HANDLE, ADCWSOCNUM); + BASE_FUNC_DELAY_US(200); /* 200 is delay count, delay 200us triger adc sampling */ + if (adc0SampleTemp > 1000.0f && adc1SampleTemp > 1000.0f) { + adcSampleTimes++; + adc0TempSum += adc0SampleTemp; + adc1TempSum += adc1SampleTemp; + } + } + if (adcSampleTimes < 1.0f) { + adcSampleTimes = 1.0f; /* Prevent divide-by-zero errors */ + } + adc0SampleTemp = adc0TempSum / adcSampleTimes; + adc1SampleTemp = adc1TempSum / adcSampleTimes; + /* Force convert to float */ + mtrCtrl->adc0Compensate = (float) adc0SampleTemp; + mtrCtrl->adc1Compensate = (float) adc1SampleTemp; + /* The normal value scope: 1800 < adc0Compensate < 2200 */ + if(g_mc.adc0Compensate < ADC_TRIMVALUE_MIN || g_mc.adc0Compensate > ADC_TRIMVALUE_MAX \ + || g_mc.adc1Compensate < ADC_TRIMVALUE_MIN || g_mc.adc1Compensate > ADC_TRIMVALUE_MAX) { + DBG_PRINTF("ADC trim value error,please reset!"); + HAL_GPIO_SetValue(&LED2_HANDLE, LED2_PIN, GPIO_LOW_LEVEL); + } + adcSampleTimes = 0; + adc0TempSum = 0; + adc1TempSum = 0; +} + +/** + * @brief Read the ADC current sampling value. + * @param CurrUvw Three-phase current. + * @retval None. + */ +static void ReadCurrUvw(UvwAxis *CurrUvw) +{ + MCS_ASSERT_PARAM(CurrUvw != NULL); + float adc0 = (float)HAL_ADC_GetConvResult(&ADCU_HANDLE, ADCUSOCNUM); + float adc1 = (float)HAL_ADC_GetConvResult(&ADCW_HANDLE, ADCWSOCNUM); + /* Convert adc sample value to current value */ + CurrUvw->u = -(adc0 - g_mc.adc0Compensate) * g_mc.adcCurrCofe; + CurrUvw->w = -(adc1 - g_mc.adc1Compensate) * g_mc.adcCurrCofe; + CurrUvw->v = -CurrUvw->u - CurrUvw->w; +} + +/** + * @brief Setting the APT Output Duty Cycle. + * @param aptx APT register base address. + * @param leftDuty Left duty cycle. + * @param rightDuty Right duty cycle. + * @retval None. + */ +static void SetPwmDuty(APT_Handle *aptx, float leftDuty, float rightDuty) +{ + MCS_ASSERT_PARAM(aptx != NULL); + MCS_ASSERT_PARAM(leftDuty > 0); + MCS_ASSERT_PARAM(rightDuty > 0); + unsigned short maxPeriodCnt = aptx->waveform.timerPeriod; + unsigned short cntCmpLeftEdge = (unsigned short)(leftDuty * maxPeriodCnt); + unsigned short cntCmpRightEdge = (unsigned short)(rightDuty * maxPeriodCnt); + /* avoid overflowing */ + cntCmpLeftEdge = (cntCmpLeftEdge > maxPeriodCnt) ? maxPeriodCnt : cntCmpLeftEdge; + cntCmpRightEdge = (cntCmpRightEdge > maxPeriodCnt) ? maxPeriodCnt : cntCmpRightEdge; + HAL_APT_SetPWMDuty(aptx, cntCmpLeftEdge, cntCmpRightEdge); +} + +/** + * @brief Duty Cycle Setting. + * @param dutyUvwLeft Three-phase left duty cycle. + * @param dutyUvwRight Three-phase right duty cycle. + * @retval None. + */ +static void SetPwmDutyCp(UvwAxis *dutyUvwLeft, UvwAxis *dutyUvwRight) +{ + MCS_ASSERT_PARAM(dutyUvwLeft != NULL); + MCS_ASSERT_PARAM(dutyUvwRight != NULL); + /* Setting the Three-Phase Duty Cycle */ + SetPwmDuty(&g_apt0, dutyUvwLeft->u, dutyUvwRight->u); + SetPwmDuty(&g_apt1, dutyUvwLeft->v, dutyUvwRight->v); + SetPwmDuty(&g_apt2, dutyUvwLeft->w, dutyUvwRight->w); +} + +/** + * @brief To set the ADC sampling trigger comparison value. + * @param cntCmpSOCA Soca Compare Count Value. + * @param cntCmpSOCB Socb Compare Count Value. + * @retval None. + */ +static void SetADCTriggerTime(unsigned short cntCmpSOCA, unsigned short cntCmpSOCB) +{ + MCS_SetAdcCompareR1(g_apt[PHASE_U], cntCmpSOCA, cntCmpSOCB, g_mc.aptMaxcntCmp); +} + +/** + * @brief Temprature table, the temprature detect range is 15 ~ 60 degree. + * @param tempResisValue Temperature sensor resistance. + * @retval None. + */ +static float TempTable(float tempResisValue) +{ + float boardTemp = 0.0f; + /* Temperatures between 15 and 30. */ + if (tempResisValue > TEMP_RES_30 && tempResisValue <= TEMP_RES_15) { + boardTemp = TEMP_15 + (TEMP_30 - TEMP_15) * (TEMP_RES_15 - tempResisValue) / (TEMP_RES_15 - TEMP_RES_30); + } else if (tempResisValue > TEMP_RES_45 && tempResisValue <= TEMP_RES_30) { /* Temperature between 30 and 45. */ + boardTemp = TEMP_30 + (TEMP_45 - TEMP_30) * (TEMP_RES_30 - tempResisValue) / (TEMP_RES_30 - TEMP_RES_45); + } else if (tempResisValue > TEMP_RES_60 && tempResisValue <= TEMP_RES_45) { /* Temperature between 45 and 50. */ + boardTemp = TEMP_45 + (TEMP_60 - TEMP_45) * (TEMP_RES_45 - tempResisValue) / (TEMP_RES_45 - TEMP_RES_60); + } else if (tempResisValue <= TEMP_RES_60) { + boardTemp = TEMP_60; /* If temperature is over 60, set temperature as 60. */ + } else if (tempResisValue >= TEMP_RES_15) { + boardTemp = TEMP_15; /* If temperature is lower 15, set temperature as 15. */ + } + return boardTemp; +} + +/** + * @brief Read power board temperature and udc. + * @retval None. + */ +static void ReadBoardTempAndUdc(void) +{ + HAL_ADC_SoftTrigSample(&ADCRESIS_HANDLE, ADCRESISSOCNUM); + HAL_ADC_SoftTrigSample(&ADCUDC_HANDLE, ADCUDCSOCNUM); + BASE_FUNC_DELAY_US(CNT_10); /* Delay 10 us. */ + /* Force convert to float type. */ + float resisAdcValue = (float)HAL_ADC_GetConvResult(&ADCRESIS_HANDLE, ADCRESISSOCNUM); + /* 10 / (x + 10) * 3.3 = resisAdcValue / 4096 * 3.3, x is resisValue, 10kohm is resistor divider value. */ + float resisValue = (4096.0f * 10.0f - 10.0f * resisAdcValue) / resisAdcValue; + g_mc.powerBoardTemp = TempTable(resisValue); + g_mc.udc = ((float)HAL_ADC_GetConvResult(&ADCUDC_HANDLE, ADCUDCSOCNUM)) * ADC_UDC_COFFI; +} + +/** + * @brief Execut abnormal feedback speed protect motion. + * @retval None. + */ +static void SpdFbkErrorProt_Exec(void) +{ + if (g_mc.prot.motorErrStatus.Bit.motorStalling == 0 && + g_mc.prot.motorErrStatus.Bit.overVoltErr == 0 && + g_mc.prot.motorErrStatus.Bit.lowerVoltErr == 0 && + g_mc.prot.motorErrStatus.Bit.overIpmTempErr == 0 && + g_mc.prot.motorErrStatus.Bit.overCurrErr == 0) { + g_mc.prot.motorErrStatus.Bit.revRotErr = 1; + /* If revRotErr, execute protect motion. */ + ProtSpo_Exec(g_apt); + } +} + +/** + * @brief Execut nan data protect motion. + * @retval None. + */ +static void NanDataDetect(void) +{ + static short errorSpdStatus = 0; + /* Detect the nan observer speed or current value. */ + if (isnan(g_mc.smo.spdEst) || isnan(g_mc.idqRef.q)) { + errorSpdStatus++; + } else { + errorSpdStatus = 0; + } + /* If the data is nan & continuous counting value is over 500 times, execute protect motion. + the detect time is 500 * 500us = 250ms. */ + if (errorSpdStatus >= 500) { + errorSpdStatus = 0; + SpdFbkErrorProt_Exec(); + } +} + +/** + * @brief Check abnormal feedback speed. + * @retval None. + */ +static void CheckSpdFbkStatus(void) +{ + static short errorCurrStatus = 0; + static short errorDeltaSpdStatus = 0; + NanDataDetect(); + if (g_mc.stateMachine == FSM_RUN) { + /* Detect the abnormal idq feedback current. */ + if (Abs(g_mc.idqRef.q - g_mc.idqFbk.q) >= CTRL_IF_CURR_AMP_A) { + errorCurrStatus++; + } else { + errorCurrStatus = 0; + } + /* Detect the abnormal feedback speed, the normal speed is > 0, if smo.spdEst < -10 && + delta speed error > USER_MIN_SPD_HZ + 10.0f at FSM_RUN stage, set the motor motion as error */ + if (g_mc.smo.spdEst < -10.0f && (g_mc.spdRefHz - g_mc.smo.spdEst > USER_MIN_SPD_HZ + 10.0f)) { + errorDeltaSpdStatus++; + } + } + /* Execute protect motion if count over 500 times, this error status caused by abnormal speed + or cabnormal urrent feedback, the detect time is 500 * 500us = 250ms. */ + if (errorCurrStatus >= 500) { + errorCurrStatus = 0; + SpdFbkErrorProt_Exec(); + } + /* This error statu caused by motor stalling, the detect time is 2 * 500us = 1ms. */ + if (errorDeltaSpdStatus >= 2) { + errorDeltaSpdStatus = 0; + g_mc.prot.motorErrStatus.Bit.motorStalling = 1; + } +} + +/** + * @brief Check Potentiometer Value callback function. + * @param param The TIMER_Handle. + * @retval None. + */ +void CheckPotentiometerValueCallback(void *param) +{ + MCS_ASSERT_PARAM(param != NULL); + BASE_FUNC_UNUSED(param); + static float potentiomitorAdcValue = 0.0f; + static float spdCmdHz = 0; + static float spdCmdHzLast = USER_MIN_SPD_HZ; /* 35.0 is spdCmdHzLast init value */ + HAL_ADC_SoftTrigSample(&ADCPTT_HANDLE, ADCPTTSOCNUM); + BASE_FUNC_DELAY_US(10); /* Delay 10 us. */ + potentiomitorAdcValue = (float)HAL_ADC_GetConvResult(&ADCPTT_HANDLE, ADCPTTSOCNUM); + /* 4045.0 is adc sample max value of potentiomitor, convert max spd to 180.25Hz */ + spdCmdHz = potentiomitorAdcValue / 4045.0f * USER_MAX_SPD_HZ; + if (Abs(spdCmdHzLast - spdCmdHz) < 1.0f) { /* Ignore changes less than 1. */ + return; + } + spdCmdHzLast = spdCmdHz; + if (spdCmdHz < USER_MIN_SPD_HZ) { /* 35.0 is spdCmdHz lower limit */ + spdCmdHz = USER_MIN_SPD_HZ; /* 35.0 is spdCmdHz lower limit */ + } + if (spdCmdHz > g_mc.mtrParam.maxElecSpd) { /* spdCmdHz upper limit */ + spdCmdHz = g_mc.mtrParam.maxElecSpd; /* spdCmdHz upper limit */ + } + if (g_mc.spdAdjustMode == CUST_SPEED_ADJUST) { + g_mc.spdCmdHz = spdCmdHz; + } +} + +/** + * @brief System timer ISR for Motor Statemachine CallBack function. + * @param param The systick timer handle. + * @retval None. + */ +void MotorStatemachineCallBack(void *param) +{ + /* Verify Parameters */ + MCS_ASSERT_PARAM(param != NULL); + BASE_FUNC_UNUSED(param); + /* Read power board temprature and voltage. */ + ReadBoardTempAndUdc(); + /* Motor speed loop state machine. */ + TSK_SystickIsr(&g_mc, g_apt); + + /* Motor error speed feedback check. */ + CheckSpdFbkStatus(); + /* Motor stalling detect. */ + STP_Det_ByCurrSpd(&g_mc.prot.stall, &g_mc.prot.motorErrStatus, g_mc.smo.spdEst, g_mc.idqFbk); + STP_Exec(&g_mc.prot.motorErrStatus, g_apt); + + /* Motor over voltage detect. */ + OVP_Det(&g_mc.prot.ovp, &g_mc.prot.motorErrStatus, g_mc.udc); + OVP_Exec(&g_mc.prot.ovp, &g_mc.spdRefHz, g_apt); + OVP_Recy(&g_mc.prot.ovp, &g_mc.prot.motorErrStatus, g_mc.udc); + /* Motor lower voltage detect. */ + LVP_Det(&g_mc.prot.lvp, &g_mc.prot.motorErrStatus, g_mc.udc); + LVP_Exec(&g_mc.prot.lvp, &g_mc.spdRefHz, g_apt); + LVP_Recy(&g_mc.prot.lvp, &g_mc.prot.motorErrStatus, g_mc.udc); + /* Power board over temperature detect. */ + OTP_Det(&g_mc.prot.otp, &g_mc.prot.motorErrStatus, OTP_IPM_ERR_BIT, g_mc.powerBoardTemp); + OTP_Exec(&g_mc.prot.otp, &g_mc.spdRefHz, g_apt); + OTP_Recy(&g_mc.prot.otp, &g_mc.prot.motorErrStatus, OTP_IPM_ERR_BIT, g_mc.powerBoardTemp); + + /* If protect level == 4, set motor state as stop. */ + if (g_mc.prot.ovp.protLevel == LEVEL_4 || g_mc.prot.lvp.protLevel == LEVEL_4 \ + || g_mc.prot.otp.protLevel == LEVEL_4) { + SysCmdStopSet(&g_mc.statusReg); + } +} + +/** + * @brief The carrier ISR wrapper function. + * @param aptHandle The APT handle. + * @retval None. + */ +void MotorCarrierProcessCallback(void *aptHandle) +{ + MCS_ASSERT_PARAM(aptHandle != NULL); + BASE_FUNC_UNUSED(aptHandle); + /* the carrierprocess of motor */ + MCS_CarrierProcess(&g_mc); + /* Over current protect */ + if (g_mc.stateMachine == FSM_RUN || g_mc.stateMachine == FSM_STARTUP) { + OCP_Det(&g_mc.prot.ocp, &g_mc.prot.motorErrStatus, g_mc.idqFbk); + OCP_Exec(&g_mc.prot.ocp, &g_mc.idqFbk, g_apt); /* Execute over current protect motion */ + if (g_mc.prot.ocp.protLevel < LEVEL_4) { + OCP_Recy(&g_mc.prot.ocp, &g_mc.prot.motorErrStatus); + } + } +} + +/** + * @brief Event interrupt callback function of APT module. + * @param para APT module handle. + * @retval None. + */ +void MotorSysErrCallback(void *para) +{ + MCS_ASSERT_PARAM(para != NULL); + APT_Handle *handle = (APT_Handle *)para; + /* The IPM overcurrent triggers and disables the three-phase PWM output. */ + MotorPwmOutputDisable(g_apt); + DCL_APT_ClearOutCtrlEventFlag((APT_RegStruct *)g_apt[PHASE_U], APT_OC_COMBINE_EVENT_A1); + DCL_APT_ClearOutCtrlEventFlag((APT_RegStruct *)g_apt[PHASE_V], APT_OC_COMBINE_EVENT_A1); + DCL_APT_ClearOutCtrlEventFlag((APT_RegStruct *)g_apt[PHASE_W], APT_OC_COMBINE_EVENT_A1); + /* Status setting error */ + SysErrorSet(&g_mc.statusReg); + DBG_PRINTF("APT error! \r\n"); + HAL_GPIO_SetValue(&LED2_HANDLE, LED2_PIN, GPIO_LOW_LEVEL); + BASE_FUNC_UNUSED(handle); +} + +/** + * @brief Init motor controller's data structure. + * @retval None. + */ +static void InitSoftware(void) +{ + /* Initializing motor control param */ + TSK_Init(); + /* Read phase-uvw current */ + g_mc.readCurrUvwCb = ReadCurrUvw; + g_mc.setPwmDutyCb = SetPwmDutyCp; + g_mc.setADCTriggerTimeCb = SetADCTriggerTime; +} + +/** + * @brief Config the KEY func. + * @param handle The GPIO handle. + * @retval None. + */ +static KEY_State Key_StateRead(GPIO_Handle *handle) +{ + if (HAL_GPIO_GetPinValue(handle, handle->pins) == 0) { + BASE_FUNC_DELAY_MS(30); /* delay 30ms for deshake */ + if (HAL_GPIO_GetPinValue(handle, handle->pins) == 0) { + while (HAL_GPIO_GetPinValue(handle, handle->pins) == 0) { + } + return KEY_DOWN; + } + } + return KEY_UP; +} + +/** + * @brief Control motor start and stop state by key func. + * @param param The GPIO handle. + * @retval None. + */ +void MotorStartStopKeyCallback(void *param) +{ + GPIO_Handle *handle = (GPIO_Handle *)param; + if (Key_StateRead(handle) == KEY_DOWN) { + if (g_mc.motorStateFlag == 0) { /* start motor */ + g_mc.motorStateFlag = 1; + SysCmdStartSet(&g_mc.statusReg); + } else if (g_mc.motorStateFlag == 1) { /* stop motor */ + g_mc.motorStateFlag = 0; + SysCmdStopSet(&g_mc.statusReg); + } + } +} + +/** + * @brief User application main entry function. + * @retval BSP_OK. + */ +int MotorMainProcess(void) +{ + unsigned int tickNum1Ms = 2; /* 1ms tick */ + static unsigned int tickCnt1Ms = 0; + unsigned int tickNum500Ms = 1000; /* 500ms tick */ + static unsigned int tickCnt500Ms = 0; + SystemInit(); + HMI_Init(); /* Init uart interrupt */ + HAL_TIMER_Start(&g_timer0); + HAL_TIMER_Start(&g_timer1); + + /* Disable PWM output before startup. */ + + MotorPwmOutputDisable(g_apt); + /* Software initialization. */ + InitSoftware(); + /* Start the PWM clock. */ + HAL_APT_StartModule(RUN_APT0 | RUN_APT1 | RUN_APT2); + /* System Timer clock. */ + BASE_FUNC_DELAY_MS(ADC_READINIT_DELAY); + TrimInitAdcShiftValue(&g_mc); + BASE_FUNC_DELAY_MS(MOTOR_START_DELAY); + while (1) { + /* Cycling send data to host */ + HMI_Process_Tx(&g_mc); + if (g_mc.msTickCnt - tickCnt1Ms >= tickNum1Ms) { + tickCnt1Ms = g_mc.msTickCnt; + /* User Code 1ms Event */ + HMI_Process_Rx(&g_mc); + /* User Code 1ms Event */ + } + + if (g_mc.msTickCnt - tickCnt500Ms >= tickNum500Ms) { + if (SysIsError(&g_mc.statusReg) != true) { + /* LED toggle in normal status. */ + HAL_GPIO_TogglePin(&LED1_HANDLE, LED1_PIN); + } + tickCnt500Ms = g_mc.msTickCnt; + } + } + return 0; +} diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/cust_process.c b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/cust_process.c new file mode 100644 index 00000000..7c4f7525 --- /dev/null +++ b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/cust_process.c @@ -0,0 +1,718 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file cust_process.c + * @author MCU Algorithm Team + * @brief This file provides functions declaration of cust process interface. + */ + +#include "cust_process.h" +#include "mcs_ctlmode_config.h" +#include "mcs_math_const.h" +#include "mcs_user_config.h" +#include "mcs_assert.h" +#include "main.h" +#include "uart_module.h" +#include "mcs_mtr_param.h" +/* Macro definitions --------------------------------------------------------------------------- */ +/* Constant value. */ +#define CONST_VALUE_60 60.0f /* Constant value 60. */ +#define CONST_VALUE_DIV_1000 0.001f /* Constant value 1/1000. */ + +/* Data array index. */ +#define DATA_SEGMENT_ONE 0 /* First element of the data segment */ +#define DATA_SEGMENT_TWO 1 /* Second element of the data segment */ +#define DATA_SEGMENT_THREE 2 /* Third element of the data segment */ +#define DATA_SEGMENT_FOUR 3 /* Fourth element of the data segment */ +#define CUSTACKCODELEN 10 /* Ack code length */ + +/* Command code. */ +#define SET_PID_KP 0x01 /* Set Pid Kp Command Params */ +#define SET_PID_KI 0x02 /* Set Pid Ki Command Params */ +#define SET_PID_LIMIT 0x03 /* Set Pid limit Command Params */ + +#define SET_SMO1TH_PLL_BDW 0x01 /* Set Smo1th Pll BandWidth Command Params */ +#define SET_SMO1TH_SPDFLITER_FC 0x02 /* Set Smo1th Fc Command Params */ +#define SET_SMO1TH_FILCOMPANGLE 0x03 /* Set Smo1th FillComp Command Params */ + +#define SET_SMO4TH_KD 0x01 /* Set Smo4th Kd Command Params */ +#define SET_SMO4TH_KP 0x02 /* Set Smo4th Kq Command Params */ + +#define SET_SPD_COMMAND_HZ 0x01 /* Set Target Speed Command Params */ +#define SET_SPD_RMG_SLOPE 0x02 /* Set Speed Slope Command Params */ + +#define SET_MAX_ELEC_SPD 0x01 /* Set Max Motor Speed Command Params */ +#define SET_MOTOR_NUM_OF_PAIRS 0x02 /* Set Motor Command Params */ + +#define SET_MOTOR_RES_OF_STATOR 0x01 /* Set Motor Res Command Params */ +#define SET_MOTOR_DAXIS_INDUCTANCE 0x02 /* Set Motor Daxis Inductance Command Params */ +#define SET_MOTOR_QAXIS_INDUCTANCE 0x03 /* Set Motor Qaxis Inductance Command Params */ + +#define SET_SVPWM_VOLTAGE_PER_UNIT 0x01 /* Set Svpwm Voltage Params */ +#define SET_ADC_CURRENT_SAMPLE_COFE 0x02 /* Set Adc coffe Params */ +#define SET_CURRENT_LOOP_CONTROL_PERIOD 0x03 /* Set Current loop period Command Params */ + +#define SET_IF_TARGET_CURRENT_VALUE 0x01 /* Set If Target Params */ +#define SET_INCREMENT_OF_IF_CURRENT 0x02 /* Set If Step Command Params */ +#define SET_SPEED_RING_BEGIN_SPEED 0x03 /* Set If to Smo Start Speed Command Params */ + +static unsigned char ackCode = 0; +static unsigned char g_uartTxBuf[CUSTACKCODELEN] = {0}; + +/** + * @brief Set observer type. + * @param mtrCtrl The motor control handle. + * @param rxData Receive buffer + */ +static void CMDCODE_SetObserverType(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) +{ + /* Get function code. */ + int funcCode = (int)(rxData->data[DATA_SEGMENT_ONE].typeF); + if (funcCode == FOC_OBSERVERTYPE_SMO1TH) { + ackCode = 0X01; + mtrCtrl->obserType = FOC_OBSERVERTYPE_SMO1TH; + CUST_AckCode(g_uartTxBuf, ackCode, mtrCtrl->obserType); + } else if (funcCode == FOC_OBSERVERTYPE_SMO4TH) { + ackCode = 0X02; + mtrCtrl->obserType = FOC_OBSERVERTYPE_SMO4TH; + CUST_AckCode(g_uartTxBuf, ackCode, mtrCtrl->obserType); + } +} + +/** + * @brief Set pid parameter ack code. + * @param funcCode Received data funccode. + */ +static unsigned char SetPidAckCode(int funcCode) +{ + switch (funcCode) { + /* Set current loop D-Axis PID parameter ack code. */ + case FOC_CURDAXISPID_PARAMS: + ackCode = 0xE0; + break; + /* Set current loop Q-Axis PID parameter ack code. */ + case FOC_CURQAXISPID_PARAMS: + ackCode = 0xE3; + break; + /* Set speed loop PID parameter ack code. */ + case FOC_SPDPID_PARAMS: + ackCode = 0xE6; + break; + default: + break; + } + return ackCode; +} + +/** + * @brief Set pid parameters. + * @param pidHandle The pid control handle. + * @param rxData Receive buffer + */ +static void SetPidParams(PID_Handle *pidHandle, CUSTDATATYPE_DEF *rxData) +{ + /* Get function code. */ + int funcCode = (int)(rxData->data[DATA_SEGMENT_ONE].typeF); + /* Get command code. */ + int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); + switch (cmdCode) { + case SET_PID_KP: /* Set the P parameter. */ + PID_SetKp(pidHandle, rxData->data[DATA_SEGMENT_THREE].typeF); + ackCode = SetPidAckCode(funcCode); + CUST_AckCode(g_uartTxBuf, (unsigned char)(ackCode + SET_PID_KP), rxData->data[DATA_SEGMENT_THREE].typeF); + break; + case SET_PID_KI: /* Set the I parameter. */ + PID_SetKi(pidHandle, rxData->data[DATA_SEGMENT_THREE].typeF); + ackCode = SetPidAckCode(funcCode); + CUST_AckCode(g_uartTxBuf, (unsigned char)(ackCode + SET_PID_KI), rxData->data[DATA_SEGMENT_THREE].typeF); + break; + case SET_PID_LIMIT: /* Set the pid limit. */ + PID_SetLimit(pidHandle, rxData->data[DATA_SEGMENT_THREE].typeF); + ackCode = SetPidAckCode(funcCode); + CUST_AckCode(g_uartTxBuf, (unsigned char)(ackCode + SET_PID_LIMIT), rxData->data[DATA_SEGMENT_THREE].typeF); + break; + default: + ackCode = 0X77; + CUST_AckCode(g_uartTxBuf, ackCode, 0); + break; + } +} + +/** + * @brief Set motor pid parameters. + * @param mtrCtrl The motor control handle. + * @param rxData Receive buffer + */ +static void CMDCODE_SetMotorPidParams(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) +{ + /* Get function code. */ + int funcCode = (int)(rxData->data[DATA_SEGMENT_ONE].typeF); + + if (funcCode == FOC_CURDAXISPID_PARAMS) { + SetPidParams(&mtrCtrl->currCtrl.dAxisPi, rxData); /* Set Curr loop Daxis pid params */ + } else if (funcCode == FOC_CURQAXISPID_PARAMS) { + SetPidParams(&mtrCtrl->currCtrl.qAxisPi, rxData); /* Set Curr loop Qaxis pid params */ + mtrCtrl->currCtrl.dAxisPi.upperLimit = mtrCtrl->currCtrl.qAxisPi.upperLimit; + mtrCtrl->currCtrl.dAxisPi.lowerLimit = mtrCtrl->currCtrl.qAxisPi.lowerLimit; + } else if (funcCode == FOC_SPDPID_PARAMS) { + SetPidParams(&mtrCtrl->spdCtrl.spdPi, rxData); /* Set Speed loop params */ + } +} + +/** + * @brief Set first order sliding mode observer parameters. + * @param smoHandle The observer control handle. + * @param rxData Receive buffer + */ +static void SetObserverSmo1thParams(FOSMO_Handle *smoHandle, CUSTDATATYPE_DEF *rxData) +{ + smoHandle->kSmo = rxData->data[DATA_SEGMENT_TWO].typeF; + ackCode = 0X09; + CUST_AckCode(g_uartTxBuf, ackCode, smoHandle->kSmo); +} + +/** + * @brief Set first order sliding mode observer's phase-locked loop parameters. + * @param smoHandle The observer control handle. + * @param rxData Receive buffer + */ +static void SetObserverSmo1thPLLParams(FOSMO_Handle *smoHandle, CUSTDATATYPE_DEF *rxData) +{ + /* Get command code. */ + int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); + float pllBdw = 0.0f; + switch (cmdCode) { + case SET_SMO1TH_PLL_BDW: /* Set the bandwidth. */ + pllBdw = rxData->data[DATA_SEGMENT_THREE].typeF; + smoHandle->pll.pi.kp = 2.0f * pllBdw; /* kp = 2.0f * pllBdw */ + smoHandle->pll.pi.ki = pllBdw * pllBdw; /* ki = pllBdw * pllBdw */ + ackCode = 0X0A; + CUST_AckCode(g_uartTxBuf, ackCode, pllBdw); + break; + case SET_SMO1TH_SPDFLITER_FC: /* Set the cutoff frequency. */ + smoHandle->spdFilter.fc = rxData->data[DATA_SEGMENT_THREE].typeF; + smoHandle->spdFilter.a1 = 1.0f / (1.0f + DOUBLE_PI * smoHandle->spdFilter.fc * CTRL_CURR_PERIOD); + smoHandle->spdFilter.b1 = 1.0f - smoHandle->spdFilter.a1; + ackCode = 0X0B; + CUST_AckCode(g_uartTxBuf, ackCode, smoHandle->spdFilter.fc); + break; + case SET_SMO1TH_FILCOMPANGLE: /* Set the compensation angle. */ + smoHandle->filCompAngle = rxData->data[DATA_SEGMENT_THREE].typeF; + ackCode = 0X0C; + CUST_AckCode(g_uartTxBuf, ackCode, smoHandle->filCompAngle); + break; + default: + ackCode = 0X77; + CUST_AckCode(g_uartTxBuf, ackCode, 0); + break; + } +} + +/** + * @brief Set fourth order sliding mode observer parameters. + * @param smo4thHandle The observer control handle. + * @param rxData Receive buffer + */ +static void SetObserverSmo4thParams(SMO4TH_Handle *smo4thHandle, CUSTDATATYPE_DEF *rxData) +{ + /* Get command code. */ + int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); + + switch (cmdCode) { + case SET_SMO4TH_KD: /* Set d axis gain. */ + smo4thHandle->kd = rxData->data[DATA_SEGMENT_THREE].typeF; + ackCode = 0X0D; + CUST_AckCode(g_uartTxBuf, ackCode, smo4thHandle->kd); + break; + case SET_SMO4TH_KP: /* Set q axis gain. */ + smo4thHandle->kq = rxData->data[DATA_SEGMENT_THREE].typeF; + ackCode = 0X0E; + CUST_AckCode(g_uartTxBuf, ackCode, smo4thHandle->kq); + break; + default: + break; + } +} + +/** + * @brief Set fourth order sliding mode observer's phase-locked loop parameters. + * @param smo4thHandle The observer control handle. + * @param rxData Receive buffer + */ +static void SetObserverSmo4thPLLParams(SMO4TH_Handle *smo4thHandle, CUSTDATATYPE_DEF *rxData) +{ + float pllBdw = rxData->data[DATA_SEGMENT_TWO].typeF; + smo4thHandle->pll.pi.kp = (2.0f) * pllBdw; /* kp = 2.0f * pllBdw */ + smo4thHandle->pll.pi.ki = pllBdw * pllBdw; + ackCode = 0X11; + CUST_AckCode(g_uartTxBuf, ackCode, pllBdw); +} + +/** + * @brief Set observer parameters. + * @param mtrCtrl The motor control handle. + * @param rxData Receive buffer + */ +static void CMDCODE_SetObserverParams(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) +{ + /* Get function code. */ + int funcCode = (int)(rxData->data[DATA_SEGMENT_ONE].typeF); + + if (funcCode == FOC_OBSERVERTYPE_SMO1TH) { + SetObserverSmo1thParams(&mtrCtrl->smo, rxData); + } else if (funcCode == FOC_OBSERVERTYPE_SMO1TH_PLL) { + SetObserverSmo1thPLLParams(&mtrCtrl->smo, rxData); + } else if (funcCode == FOC_OBSERVERTYPE_SMO4TH) { + SetObserverSmo4thParams(&mtrCtrl->smo4th, rxData); + } else if (funcCode == FOC_OBSERVERTYPE_SMO4TH_PLL) { + SetObserverSmo4thPLLParams(&mtrCtrl->smo4th, rxData); + } +} + +/** + * @brief Set motor speed and speed slope. + * @param mtrCtrl The motor control handle. + * @param rxData Receive buffer + */ +static void CMDCODE_SetMotorSpdAndSlope(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) +{ + /* Get command code. */ + int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); + + switch (cmdCode) { + case SET_SPD_COMMAND_HZ: /* Set target speed(hz). */ + mtrCtrl->spdCmdHz = rxData->data[DATA_SEGMENT_THREE].typeF * mtrCtrl->mtrParam.mtrNp / CONST_VALUE_60; + /* Judgement the value > 0.00001, make sure denominator != 0. */ + if (rxData->data[DATA_SEGMENT_FOUR].typeF > 0.00001f) { + mtrCtrl->spdRmg.delta = mtrCtrl->spdCmdHz / rxData->data[DATA_SEGMENT_FOUR].typeF * CTRL_SYSTICK_PERIOD; + } + ackCode = 0X16; + CUST_AckCode(g_uartTxBuf, ackCode, mtrCtrl->spdCmdHz); + break; + case SET_SPD_RMG_SLOPE: /* Set speed slope. */ + mtrCtrl->spdRmg.delta = mtrCtrl->spdCmdHz / rxData->data[DATA_SEGMENT_THREE].typeF * CTRL_SYSTICK_PERIOD; + ackCode = 0X17; + CUST_AckCode(g_uartTxBuf, ackCode, rxData->data[DATA_SEGMENT_THREE].typeF); + break; + default: + break; + } +} + +/** + * @brief Set motor base parameters. + * @param mtrParamHandle The motor parameters handle. + * @param rxData Receive buffer + */ +static void SetMotorBaseParams(MOTOR_Param *mtrParamHandle, CUSTDATATYPE_DEF *rxData) +{ + /* Get command code. */ + int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); + + switch (cmdCode) { + case SET_MAX_ELEC_SPD: /* Set max electric speed. */ + mtrParamHandle->maxElecSpd = rxData->data[DATA_SEGMENT_THREE].typeF / + CONST_VALUE_60 * mtrParamHandle->mtrNp; + ackCode = 0X18; + CUST_AckCode(g_uartTxBuf, ackCode, rxData->data[DATA_SEGMENT_THREE].typeF); + break; + case SET_MOTOR_NUM_OF_PAIRS: /* Set the number of motor pole pairs. */ + mtrParamHandle->mtrNp = (unsigned short)(rxData->data[DATA_SEGMENT_THREE].typeF); + ackCode = 0X19; + CUST_AckCode(g_uartTxBuf, ackCode, (float)mtrParamHandle->mtrNp); + break; + default: + break; + } +} + +/** + * @brief Set motor special parameters. + * @param mtrParamHandle The motor parameters handle. + * @param rxData Receive buffer + */ +static void SetMotorSpecialParams(MOTOR_Param *mtrParamHandle, CUSTDATATYPE_DEF *rxData) +{ + /* Get command code. */ + int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); + + switch (cmdCode) { + case SET_MOTOR_RES_OF_STATOR: /* Set the resistor of stator. */ + mtrParamHandle->mtrRs = rxData->data[DATA_SEGMENT_THREE].typeF; + ackCode = 0X1A; + CUST_AckCode(g_uartTxBuf, ackCode, mtrParamHandle->mtrRs); + break; + case SET_MOTOR_DAXIS_INDUCTANCE: /* Set the d axis inductance. */ + mtrParamHandle->mtrLd = rxData->data[DATA_SEGMENT_THREE].typeF; + ackCode = 0X1B; + CUST_AckCode(g_uartTxBuf, ackCode, mtrParamHandle->mtrLd); + break; + case SET_MOTOR_QAXIS_INDUCTANCE: /* Set the q axis inductance. */ + mtrParamHandle->mtrLq = rxData->data[DATA_SEGMENT_THREE].typeF; + ackCode = 0X1C; + CUST_AckCode(g_uartTxBuf, ackCode, mtrParamHandle->mtrLq); + break; + default: + ackCode = 0X77; + CUST_AckCode(g_uartTxBuf, ackCode, 0); + break; + } +} + +/** + * @brief Set motor board parameters. + * @param mtrCtrl The motor control handle. + * @param rxData Receive buffer + */ +static void SetMotorBoardParams(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) +{ + /* Get command code. */ + int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); + + switch (cmdCode) { + case SET_SVPWM_VOLTAGE_PER_UNIT: /* Set svpwm voltage per unit. */ + mtrCtrl->sv.voltPu = rxData->data[DATA_SEGMENT_THREE].typeF * ONE_DIV_SQRT3; + mtrCtrl->currCtrl.outLimit = mtrCtrl->sv.voltPu * ONE_DIV_SQRT3; + ackCode = 0X1D; + CUST_AckCode(g_uartTxBuf, ackCode, rxData->data[DATA_SEGMENT_THREE].typeF); + break; + case SET_ADC_CURRENT_SAMPLE_COFE: /* Set adc current sample cofeature. */ + mtrCtrl->adcCurrCofe = rxData->data[DATA_SEGMENT_THREE].typeF; + ackCode = 0X1E; + CUST_AckCode(g_uartTxBuf, ackCode, mtrCtrl->adcCurrCofe); + break; + case SET_CURRENT_LOOP_CONTROL_PERIOD: /* Set current loop control period. */ + mtrCtrl->currCtrlPeriod = 1 / rxData->data[DATA_SEGMENT_THREE].typeF * CONST_VALUE_DIV_1000; + ackCode = 0X1F; + CUST_AckCode(g_uartTxBuf, ackCode, rxData->data[DATA_SEGMENT_THREE].typeF); + break; + default: + break; + } +} + +/** + * @brief Set motor parameters. + * @param mtrCtrl The motor control handle. + * @param rxData Receive buffer + */ +static void CMDCODE_SetMotorParams(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) +{ + /* Get function code. */ + int funcCode = (int)(rxData->data[DATA_SEGMENT_ONE].typeF); + + if (funcCode == MOTOR_PARAMS_BASE) { + SetMotorBaseParams(&mtrCtrl->mtrParam, rxData); + } else if (funcCode == MOTOR_PARAMS_SPECIAL) { + SetMotorSpecialParams(&mtrCtrl->mtrParam, rxData); + } else if (funcCode == MOTOR_PARAMS_BOARD) { + SetMotorBoardParams(mtrCtrl, rxData); + } +} + +/** + * @brief Motor start. + * @param mtrCtrl The motor control handle. + */ +static void CMDCODE_MotorStart(MTRCTRL_Handle *mtrCtrl) +{ + if (mtrCtrl->stateMachine != FSM_RUN) { + SysCmdStartSet(&mtrCtrl->statusReg); /* start motor. */ + mtrCtrl->motorStateFlag = 1; + ackCode = 0X24; /* send ackcode to host. */ + CUST_AckCode(g_uartTxBuf, ackCode, 1); + } +} + +/** + * @brief Motor stop. + * @param mtrCtrl The motor control handle. + */ +static void CMDCODE_MotorStop(MTRCTRL_Handle *mtrCtrl) +{ + SysCmdStopSet(&mtrCtrl->statusReg); + mtrCtrl->motorStateFlag = 0; + ackCode = 0X25; + CUST_AckCode(g_uartTxBuf, ackCode, 0); +} + +/** + * @brief Motor state reset. + * @param mtrCtrl The motor control handle. + */ +static void CMDCODE_MotorReset(MTRCTRL_Handle *mtrCtrl) +{ + BASE_FUNC_UNUSED(mtrCtrl); + BASE_FUNC_SoftReset(); +} + +/** + * @brief Set IF-Startup parameters. + * @param mtrCtrl The motor control handle. + * @param rxData Receive buffer + */ +static void SetStartupIFParams(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) +{ + /* Get command code. */ + int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); + + switch (cmdCode) { + case SET_IF_TARGET_CURRENT_VALUE: /* Set I/F start up target current value. */ + mtrCtrl->ifCtrl.targetAmp = rxData->data[DATA_SEGMENT_THREE].typeF; + ackCode = 0X26; + CUST_AckCode(g_uartTxBuf, ackCode, mtrCtrl->ifCtrl.targetAmp); + break; + case SET_INCREMENT_OF_IF_CURRENT: /* Set increment of I/F start up current. */ + mtrCtrl->ifCtrl.stepAmp = mtrCtrl->ifCtrl.targetAmp / rxData->data[DATA_SEGMENT_THREE].typeF * + CTRL_SYSTICK_PERIOD; + ackCode = 0X27; + CUST_AckCode(g_uartTxBuf, ackCode, mtrCtrl->ifCtrl.stepAmp); + break; + case SET_SPEED_RING_BEGIN_SPEED: /* Set speed ring begin speed. */ + mtrCtrl->startup.spdBegin = rxData->data[DATA_SEGMENT_THREE].typeF / + CONST_VALUE_60 * mtrCtrl->mtrParam.mtrNp; + ackCode = 0X28; + CUST_AckCode(g_uartTxBuf, ackCode, rxData->data[DATA_SEGMENT_THREE].typeF); + break; + default: + break; + } +} + +/** + * @brief Set start up parameters. + * @param mtrCtrl The motor control handle. + * @param rxData Receive buffer + */ +static void CMDCODE_SetStartupParams(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) +{ + /* Get function code. */ + int funcCode = (int)(rxData->data[DATA_SEGMENT_ONE].typeF); + + if (funcCode == FOC_STARTUP_IF) { + SetStartupIFParams(mtrCtrl, rxData); + } +} + +/** + * @brief Set adjust speed mode. + * @param mtrCtrl The motor control handle. + * @param rxData Receive buffer + */ +static void CMDCODE_SetAdjustSpdMode(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) +{ + /* Get function code. */ + int funcCode = (int)(rxData->data[DATA_SEGMENT_ONE].typeF); + /* Get commond code. */ + int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); + if (funcCode == HOST_SPEED_ADJUST) { + mtrCtrl->spdAdjustMode = HOST_SPEED_ADJUST; + /* Uart connect success. */ + mtrCtrl->uartConnectFlag = CONNECTING; + ackCode = 0X2A; + CUST_AckCode(g_uartTxBuf, ackCode, (float)mtrCtrl->spdAdjustMode); + } else if (funcCode == CUST_SPEED_ADJUST) { + if (cmdCode == 1) { /* If uart connection disconnected & stop motor commond. */ + SysCmdStopSet(&mtrCtrl->statusReg); + mtrCtrl->motorStateFlag = 0; + } + mtrCtrl->spdAdjustMode = CUST_SPEED_ADJUST; + ackCode = 0X2B; + CUST_AckCode(g_uartTxBuf, ackCode, (float)mtrCtrl->spdAdjustMode); + /* Uart disconnect. */ + mtrCtrl->uartConnectFlag = DISCONNECT; + } +} + +/** + * @brief Check uart connect. + * @param mtrCtrl The motor control handle. + * @param rxData Receive buffer + */ +static void CMDCODE_UartHandShake(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) +{ + CMDCODE_SetAdjustSpdMode(mtrCtrl, rxData); +} + +/** + * @brief Set Motor Initial Status Parameters. + * @param mtrCtrl The motor control handle. + * @param rxData Receive buffer + * @param code Instruction code. + */ +static void CMDCODE_EXE_SetMotorInitParams(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData, unsigned char code) +{ + switch (code) { + case CMDCODE_SET_OBSERVER_TYPE: { /* Set observer type. */ + CMDCODE_SetObserverType(mtrCtrl, rxData); + } + break; + case CMDCODE_SET_PID_PARAMS: { /* Set motor pid parameters. */ + CMDCODE_SetMotorPidParams(mtrCtrl, rxData); + } + break; + case CMDCODE_SET_OBSERVER_PARAMS: { /* Set observer parameters. */ + CMDCODE_SetObserverParams(mtrCtrl, rxData); + } + break; + case CMDCODE_SET_MOTOR_TARGETSPD: { /* Set motor speed and speed slope. */ + CMDCODE_SetMotorSpdAndSlope(mtrCtrl, rxData); + } + break; + case CMDCODE_SET_MOTOR_PARAMS: { /* Set motor parameters. */ + CMDCODE_SetMotorParams(mtrCtrl, rxData); + } + break; + case CMDCODE_SET_ADJUSTSPD_MODE: /* Set adjust speed mode. */ + CMDCODE_SetAdjustSpdMode(mtrCtrl, rxData); + break; + default: + break; + } +} + +/** + * @brief Set Motor Control System Status. + * @param mtrCtrl The motor control handle. + * @param rxData Receive buffer + * @param code Instruction code. + */ +static void CMDCODE_EXE_SetMotorState(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData, unsigned char code) +{ + BASE_FUNC_UNUSED(rxData); + switch (code) { + case CMDCODE_MOTOR_START: { /* Motor start command */ + CMDCODE_MotorStart(mtrCtrl); + } + break; + case CMDCODE_MOTOR_STOP: { /* Motor stop command */ + CMDCODE_MotorStop(mtrCtrl); + } + break; + case CMDCODE_MOTORSTATE_RESET: { /* Motor reset command */ + CMDCODE_MotorReset(mtrCtrl); + } + break; + default: + break; + } +} + +/** + * @brief Set Startup and Uart Link Handshake Flag Parameters. + * @param mtrCtrl The motor control handle. + * @param rxData Receive buffer + * @param code Instruction code. + */ +static void CMDCODE_EXE_SetOtherParams(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData, unsigned char code) +{ + switch (code) { + case CMDCODE_SET_STARTUP_PARAMS: /* Set start up parameters. */ + CMDCODE_SetStartupParams(mtrCtrl, rxData); + break; + case CMDCODE_UART_HANDSHAKE: { /* Check uart hand shake. */ + CMDCODE_UartHandShake(mtrCtrl, rxData); + } + break; + case CMDCODE_UART_HEARTDETECT: { /* Check uart hand shake. */ + mtrCtrl->uartHeartDetCnt++; + } + break; + default: + break; + } +} +/** + * @brief Instruction code executor. + * @param mtrCtrl The motor control handle. + * @param rxData Receive buffer + * @param code Instruction code. + */ +static void CMDCODE_EXE_Process(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData, unsigned char code) +{ + CMDCODE_EXE_SetMotorInitParams(mtrCtrl, rxData, code); + CMDCODE_EXE_SetMotorState(mtrCtrl, rxData, code); + CMDCODE_EXE_SetOtherParams(mtrCtrl, rxData, code); +} + +/** + * @brief Host data download callback and data parsing. + * @param mtrCtrl The motor control handle. + * @param rxBuf Receive buffer + */ +void CUST_UartDataProcess(MTRCTRL_Handle *mtrCtrl, unsigned char *rxBuf) +{ + /* Verify Parameters */ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + MCS_ASSERT_PARAM(rxBuf != NULL); + + /* Uart data storage struct */ + CUSTDATATYPE_DEF data; + volatile unsigned char *ptrCnt = &rxBuf[FRAME_CHECK_BEGIN + 1]; + volatile unsigned char *strCnt = &data.data[0].typeCh[0]; + for (unsigned int j = 0; j < FRAME_RECV_DATA_LENTH * FRAME_ONE_DATA_LENTH; j++) { + *strCnt++ = *ptrCnt++; + } + /* Message function code. */ + data.code = rxBuf[FRAME_CHECK_BEGIN]; + CMDCODE_EXE_Process(mtrCtrl, &data, data.code); +} + +/** + * @brief The host computer displays data transmission. + * @param mtrCtrl The motor control handle. + * @param txData Message content. + * @param stage Message status function code. + */ +void CUST_SetTxMsg(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *txData) +{ + /* Verify Parameters */ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + MCS_ASSERT_PARAM(txData != NULL); + if (mtrCtrl->stateMachine == FSM_IDLE) { + mtrCtrl->smo.spdEst = 0.0f; + } + /* Data send to host. */ + txData->data[CURRDQ_Q].typeF = mtrCtrl->idqFbk.q; + txData->data[CURRDQ_D].typeF = mtrCtrl->idqFbk.d; + txData->data[CURRREFDQ_Q].typeF = mtrCtrl->idqRef.q; + txData->data[CURRREFDQ_D].typeF = mtrCtrl->idqRef.d; + /* Motor current speed. */ + txData->data[CURRSPD].typeF = mtrCtrl->smo.spdEst * CONST_VALUE_60 / mtrCtrl->mtrParam.mtrNp; + /* Motor commond speed. */ + txData->data[SPDCMDHZ].typeF = mtrCtrl->spdCmdHz * CONST_VALUE_60 / mtrCtrl->mtrParam.mtrNp; + /* Bus voltage. */ + txData->data[UDC].typeF = mtrCtrl->udc; + /* Power board temprature. */ + txData->data[POWERBOARDTEMP].typeF = mtrCtrl->powerBoardTemp; + /* Motor protection status flag. */ + txData->data[CUST_ERR_CODE].typeI = mtrCtrl->prot.motorErrStatus.all; + /* Three phase current. */ + txData->data[CURRUVW_U].typeF = mtrCtrl->currUvw.u; + txData->data[CURRUVW_V].typeF = mtrCtrl->currUvw.v; + txData->data[CURRUVW_W].typeF = mtrCtrl->currUvw.w; + /* Three phase pwm duty. */ + txData->data[PWMDUTYUVW_U].typeF = mtrCtrl->dutyUvw.u; + txData->data[PWMDUTYUVW_V].typeF = mtrCtrl->dutyUvw.v; + txData->data[PWMDUTYUVW_W].typeF = mtrCtrl->dutyUvw.w; + /* Motor electric angle. */ + txData->data[AXISANGLE].typeF = mtrCtrl->axisAngle; + txData->data[VDQ_Q].typeF = mtrCtrl->vdqRef.q; + txData->data[VDQ_D].typeF = mtrCtrl->vdqRef.d; + txData->data[SPDREFHZ].typeF = mtrCtrl->spdRefHz * CONST_VALUE_60 / mtrCtrl->mtrParam.mtrNp; + txData->data[SENDTIMESTAMP].typeF = mtrCtrl->uartTimeStamp; +} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/cust_process.h b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/cust_process.h new file mode 100644 index 00000000..60fe69bb --- /dev/null +++ b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/cust_process.h @@ -0,0 +1,36 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file cust_process.h + * @author MCU Algorithm Team + * @brief This file provides functions declaration of system status. + */ + +/* Define to prevent recursive inclusion ------------------------------------------------------- */ +#ifndef McsMagicTag_HMI_MOUDLE_H +#define McsMagicTag_HMI_MOUDLE_H + +/* Includes ------------------------------------------------------------------------------------ */ +#include "protocol.h" +/* Typedef definitions ------------------------------------------------------------------------- */ + + +void CUST_UartDataProcess(MTRCTRL_Handle *mtrCtrl, unsigned char *rxBuf); + +void CUST_SetTxMsg(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *txData); + +#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/hmi_module.c b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/hmi_module.c new file mode 100644 index 00000000..58bf15a7 --- /dev/null +++ b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/hmi_module.c @@ -0,0 +1,57 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file hmi_module.c + * @author MCU Algorithm Team + * @brief This file provides functions declaration of human-machine interface. + */ + +#include "hmi_module.h" +#include "mcs_assert.h" + +/** + * @brief HMI Initializatio. + * @retval None. + */ +void HMI_Init(void) +{ + UartRecvInit(); +} + +/** + * @brief HMI processing. + * @param mtrCtrl The motor control handle.. + * @retval None. + */ +void HMI_Process_Rx(MTRCTRL_Handle *mtrCtrl) +{ + /* Verify Parameters */ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + UartModuleProcess_Rx(mtrCtrl); +} + +/** + * @brief HMI processing. + * @param mtrCtrl The motor control handle.. + * @retval None. + */ +void HMI_Process_Tx(MTRCTRL_Handle *mtrCtrl) +{ + /* Verify Parameters */ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + UartModuleProcess_Tx(mtrCtrl); +} diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/hmi_module.h b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/hmi_module.h new file mode 100644 index 00000000..056f8d73 --- /dev/null +++ b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/hmi_module.h @@ -0,0 +1,37 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file hmi_module.h + * @author MCU Algorithm Team + * @brief This file provides functions declaration of system status. + */ + +/* Define to prevent recursive inclusion ------------------------------------------------------- */ +#ifndef McsMagicTag_HMI_MOUDLE_H +#define McsMagicTag_HMI_MOUDLE_H + +/* Includes ------------------------------------------------------------------------------------ */ +#include "uart_module.h" +/* Typedef definitions ------------------------------------------------------------------------- */ + + +void HMI_Init(void); + +void HMI_Process_Rx(MTRCTRL_Handle *mtrCtrl); +void HMI_Process_Tx(MTRCTRL_Handle *mtrCtrl); + +#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/protocol.c b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/protocol.c new file mode 100644 index 00000000..56f0c0bb --- /dev/null +++ b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/protocol.c @@ -0,0 +1,174 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file protocol.c + * @author MCU Algorithm Team + * @brief This file provides functions declaration of port communication. + */ + +#include "protocol.h" +#include "apt.h" +#include "typedefs.h" +#include "main.h" +#include "mcs_assert.h" +#include "cust_process.h" + +/** + * @brief Callback function for receiving data analysis and processing. + * @param rxBuf Receive buffer. + */ +__weak void CUST_UartDataProcess(MTRCTRL_Handle *mtrCtrl, unsigned char *rxBuf) +{ + BASE_FUNC_UNUSED(mtrCtrl); + BASE_FUNC_UNUSED(rxBuf); +} +/** + * @brief User-defined protocol message sending function (weak function). + * @param rxData Sending Messages.. + */ +__weak void CUST_SetTxMsg(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *txData) +{ + BASE_FUNC_UNUSED(mtrCtrl); + BASE_FUNC_UNUSED(*txData); +} + +static void (*g_ptrDataProcess)(MTRCTRL_Handle *mtrCtrl, unsigned char *rxBuf) = CUST_UartDataProcess; + +/** + * @brief Frame checksum. + * @param ptr Pointer to the data to be checked + * @param num Number of bytes + * @retval unsigned char Checksum + */ +static unsigned char CheckSum(unsigned char *ptr, unsigned char num) +{ + unsigned char sum = 0; + unsigned char *p = ptr; + /* Calculate the sum of received data. */ + for (unsigned char i = 0; i < num; i++) { + sum += (unsigned char)*p; + p++; + } + return sum; +} + +/** + * @brief Transmitting Data Frames. + * @param mtrCtrl The motor control handle. + * @param txBuf Sending Messages. + */ +unsigned int CUST_TransmitData(MTRCTRL_Handle *mtrCtrl, unsigned char *txBuf) +{ + /* Verify Parameters */ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + MCS_ASSERT_PARAM(txBuf != NULL); + unsigned int dataLen = FRAME_ONE_DATA_LENTH * SEND_FRAME_DATA_NUM; + unsigned char i = 0; + CUSTDATATYPE_DEF txData = {0}; + CUST_SetTxMsg(mtrCtrl, &txData); + txBuf[i++] = FRAME_START; + /* Message function code */ + txBuf[i++] = FRAME_SENT; + /* Message data */ + for (unsigned char x = 0; x < SEND_FRAME_DATA_NUM; x++) { + unsigned char floatIndex = 0; + unsigned char byteOffset = i; + txBuf[x * FRAME_ONE_DATA_LENTH + byteOffset++] = txData.data[x].typeCh[floatIndex++]; + txBuf[x * FRAME_ONE_DATA_LENTH + byteOffset++] = txData.data[x].typeCh[floatIndex++]; + txBuf[x * FRAME_ONE_DATA_LENTH + byteOffset++] = txData.data[x].typeCh[floatIndex++]; + txBuf[x * FRAME_ONE_DATA_LENTH + byteOffset++] = txData.data[x].typeCh[floatIndex++]; + } + /* Message verification domain */ + txBuf[dataLen + i++] = CheckSum((unsigned char*)&txBuf[FRAME_CHECK_BEGIN], dataLen + 1); + /* End of Message */ + txBuf[dataLen + i++] = FRAME_END; + return dataLen + i; +} + +/** + * @brief Transmitting Data Frames. + * @param txBuf Sending Cust Ack Code. + * @param ackCode Ack Code. + * @param varParams Host set parameter. + */ +void CUST_AckCode(unsigned char *txBuf, unsigned char ackCode, float varParams) +{ + /* Verify Parameters */ + MCS_ASSERT_PARAM(txBuf != NULL); + CUSTDATATYPE_DEF txData = {0}; + int dataIndex = 0; + unsigned int i = 0; + unsigned int txLen = 0; + unsigned char dataLen = FRAME_ONE_CHAR_LENTH + FRAME_ONE_DATA_LENTH; + + txData.data[0].typeF = varParams; + txBuf[i++] = FRAME_START; + /* Message function code */ + txBuf[i++] = FRAME_CUSTACK; + /* Message ack code */ + txBuf[i++] = ackCode; + /* Message data */ + txBuf[i++] = txData.data[0].typeCh[dataIndex++]; + txBuf[i++] = txData.data[0].typeCh[dataIndex++]; + txBuf[i++] = txData.data[0].typeCh[dataIndex++]; + txBuf[i++] = txData.data[0].typeCh[dataIndex++]; + + /* Message verification domain */ + txBuf[FRAME_ONE_CHAR_LENTH + i++] = CheckSum((unsigned char*)&txBuf[FRAME_CHECK_BEGIN], dataLen + 1); + /* End of Message */ + txBuf[FRAME_ONE_CHAR_LENTH + i++] = FRAME_END; + txLen = FRAME_ONE_CHAR_LENTH + i++; + HAL_UART_WriteDMA(&g_uart0, txBuf, txLen); +} + +/** + * @brief Cust receive data process. + * @param mtrCtrl The motor control handle. + * @param rxBuf Receive buffer + */ +void CUST_DataReceProcss(MTRCTRL_Handle *mtrCtrl, unsigned char *rxBuf) +{ + /* Verify Parameters */ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + MCS_ASSERT_PARAM(rxBuf != NULL); + unsigned char g_uartTxBuf[10] = {0}; + unsigned char ackCode = 0; + /* Frame header check */ + if (rxBuf[0] != FRAME_START) { + ackCode = 0X78; + CUST_AckCode(g_uartTxBuf, ackCode, 0); + return; + } + /* Frame trailer check */ + if (rxBuf[FRAME_LENTH - 1] != FRAME_END) { + ackCode = 0X79; + CUST_AckCode(g_uartTxBuf, ackCode, 0); + return; + } + /* Checksum */ + if (CheckSum((unsigned char*)&rxBuf[FRAME_CHECK_BEGIN], FRAME_CHECK_NUM) != rxBuf[FRAME_CHECKSUM]) { + ackCode = 0X7A; + CUST_AckCode(g_uartTxBuf, ackCode, 0); + return; + } else { + if (g_ptrDataProcess == NULL) { + return; + } else { + g_ptrDataProcess(mtrCtrl, rxBuf); + } + } +} diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/protocol.h b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/protocol.h new file mode 100644 index 00000000..900a00d8 --- /dev/null +++ b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/protocol.h @@ -0,0 +1,121 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file protocol.h + * @author MCU Algorithm Team + * @brief This file provides functions declaration of port communication. + */ +#ifndef McsMagicTag_PORTOCOL_H +#define McsMagicTag_PORTOCOL_H + +#include "uart.h" +#include "mcs_carrier.h" + +#define RS485_SEND_ENABLE GPIO6->GPIO_DATA[GPIO_PIN_7].reg = GPIO_PIN_7 +#define RS485_SEND_DISABLE GPIO6->GPIO_DATA[GPIO_PIN_7].reg = BASE_CFG_UNSET + +#define RX_BUF_LEN (16) +#define SEND_FRAME_DATA_NUM (CUSTDATA_MAX) +/* Service Uart0 Communication Deal */ +#define FRAME_ONE_DATA_LENTH 4 +#define FRAME_ONE_CHAR_LENTH 1 +#define FRAME_RECV_DATA_LENTH 4 +#define FRAME_LENTH 20 /* Data length */ +#define FRAME_SENT 0X8F +#define FRAME_CUSTACK 0X8A +#define FRAME_START 0x0F /* Start frame */ +#define FRAME_END '/' /* StOP frame */ +#define FRAME_CHECK_BEGIN 1 /* Check frame */ +#define FRAME_CHECKSUM 18 /* Check sum */ +#define FRAME_CHECK_NUM 17 +#define CMDCODE_IDLE_FRAME 0x55 /* Fill frame */ +#define CMDCODE_GET_MOTOR_PARAMS 0x01 +#define CMDCODE_SEND_MOTOR_PARAMS 0x02 +#define CMDCODE_SET_MOTOR_CTLMODE 0x03 +#define CMDCODE_SET_OBSERVER_TYPE 0x04 +#define CMDCODE_SET_STARTUP_MODE 0x05 +#define CMDCODE_SET_PID_PARAMS 0x06 +#define CMDCODE_SET_STARTUP_PARAMS 0x07 +#define CMDCODE_SET_OBSERVER_PARAMS 0x08 +#define CMDCODE_SET_MOTOR_TARGETSPD 0x09 +#define CMDCODE_SET_MOTOR_PARAMS 0x0A +#define CMDCODE_MOTOR_START 0x0B +#define CMDCODE_MOTOR_STOP 0x0C +#define CMDCODE_MOTORSTATE_RESET 0x0D +#define CMDCODE_SEND_FIRMVERSION 0x0E +#define CMDCODE_SET_ADJUSTSPD_MODE 0x11 +#define CMDCODE_UART_HANDSHAKE 0x12 +#define CMDCODE_UART_HEARTDETECT 0x13 + +typedef union { + unsigned char typeCh[4]; + float typeF; + int typeI; +} UNIONDATATYPE_DEF; + +typedef enum { + OFFLINE_RES = 0, + OFFLINE_LD, + OFFLINE_LQ, + OFFLINE_PSIF, + OFFLINE_JS, + OFFLINE_NP, + OFFLINE_B, + OFFLINE_KPD, + OFFLINE_KID, + OFFLINE_KPQ, + OFFLINE_KIQ, + OFFLINE_KPS, + OFFLINE_KIS, + OFFLINE_SPEED, + OFLINE_MAX +} OFFLINE_IDEN_TYPE; + +typedef enum { + CURRDQ_Q = 0, + CURRDQ_D, + CURRREFDQ_Q, + CURRREFDQ_D, + CURRSPD, + SPDCMDHZ, + UDC, + POWERBOARDTEMP, + CUST_ERR_CODE, + CURRUVW_U, + CURRUVW_V, + CURRUVW_W, + PWMDUTYUVW_U, + PWMDUTYUVW_V, + PWMDUTYUVW_W, + AXISANGLE, + VDQ_Q, + VDQ_D, + SPDREFHZ, + SENDTIMESTAMP, + CUSTDATA_MAX +} SENDTOHOSTPARAMS; + +typedef struct { + volatile unsigned char code; + volatile UNIONDATATYPE_DEF data[SEND_FRAME_DATA_NUM]; +} CUSTDATATYPE_DEF; + + +void CUST_DataReceProcss(MTRCTRL_Handle *mtrCtrl, unsigned char *rxBuf); +unsigned int CUST_TransmitData(MTRCTRL_Handle *mtrCtrl, unsigned char *txBuf); +void CUST_AckCode(unsigned char *txBuf, unsigned char ackCode, float varParams); +#endif /* McsMagicTag_PORTOCOL_H */ diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/uart_module.c b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/uart_module.c new file mode 100644 index 00000000..df5e9186 --- /dev/null +++ b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/uart_module.c @@ -0,0 +1,184 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the + * following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file uart_module.c + * @author MCU Algorithm Team + * @brief This file provides functions declaration of Serial port communication. + */ +#include "uart_module.h" +#include "debug.h" +#include "main.h" +#include "baseinc.h" + +/* Buffer size */ +#define UI_TX_BUF_LEN (96) +#define UI_RX_BUF_LEN (96) + +/* Receiving Timeout Interval */ +#define UART_TIME_OUT_MS (100) + +/* Start sending data to host delay after uart connect success */ +#define UART_UPDATA_DELAY_TIME_MS (50) + +/* Data buffer */ +unsigned char g_uartRxBuf[UI_RX_BUF_LEN] = {0}; +unsigned char g_uartTxBuf[UI_TX_BUF_LEN] = {0}; +static unsigned int getdeltaSystickCnt = 0; +static FRAME_Handle g_uartFrame; +/** + * @brief Receive Data Clear. + * @param uartFrame Receice Data. + * @retval None. + */ +static void FrameRecvClear(FRAME_Handle *uartFrame) +{ + /* Clear buffer lenth. */ + uartFrame->buffLen = 0; + uartFrame->timeOutCnt = 0; + uartFrame->frameFlag = 0; + /* Clear received data lenth. */ + uartFrame->rxLen = 0; + /* Clear received flag. */ + uartFrame->rxFlag = 0; + uartFrame->upDataCnt = 0; + uartFrame->rxAckFlag = 0; +} + +/** + * @brief Set Dma status. + * @param mtrCtrl The motor control handle. + * @retval None. + */ +static void SetUartDmaStatus(MTRCTRL_Handle *mtrCtrl) +{ + /* Delay 50ms start uart Tx DMA . */ + if (mtrCtrl->uartConnectFlag == CONNECTING && g_uartFrame.upDataDelayCnt++ > UART_UPDATA_DELAY_TIME_MS) { + g_uartFrame.txFlag = 1; /* Start send data flag. */ + mtrCtrl->uartConnectFlag = CONNECTED; + g_uartFrame.upDataDelayCnt = 0; + } + if (mtrCtrl->uartConnectFlag == DISCONNECT) { + g_uartFrame.txFlag = 0; /* Stop send data flag. */ + mtrCtrl->uartTimeStamp = 0; + } +} + +/** + * @brief Uart Dma interupt callback func. + * @param handle Uart handle. + * @retval None. + */ +void UART0_TXDMACallback(void *handle) +{ + BASE_FUNC_UNUSED(handle); + unsigned int getCurSystickCnt = 0; + static unsigned int getlastSystickCnt = 0; + /* USER CODE BEGIN UART0_WRITE_DMA_FINISH */ + g_uartFrame.txFlag = 1; + /* Received information answered, information update reserved. */ + if (g_uartFrame.rxAckFlag == 1) { + g_uartFrame.uartItTxFlag = 1; + g_uartFrame.rxAckFlag = 0; + } + getCurSystickCnt = DCL_SYSTICK_GetTick(); + if (getlastSystickCnt != 0) { + /* Calculate unit frame data send time */ + getdeltaSystickCnt = getCurSystickCnt - getlastSystickCnt; + } + getlastSystickCnt = getCurSystickCnt; + /* USER CODE END UART0_WRITE_DMA_FINISH */ +} + +/** + * @brief Uart0 Interruput Read CallBack Function. + * @param handle Uart handle. + * @retval None. + */ +void UART0ReadInterruptCallback(void *handle) +{ + BASE_FUNC_UNUSED(handle); + /* USER CODE BEGIN UART0_READ_IT_FINISH */ + if (g_uartFrame.rxLen >= UI_RX_BUF_LEN - 1) { + g_uartFrame.rxLen = 0; + } + HAL_UART_ReadIT(handle, &g_uartFrame.rxData, 1); + g_uartRxBuf[g_uartFrame.rxLen] = g_uartFrame.rxData; + g_uartFrame.rxLen++; + g_uartFrame.rxFlag = 1; + g_uartFrame.uartItTxFlag = 0; + return; + /* USER CODE END UART0_READ_IT_FINISH */ +} + +/** + * @brief Uart Read Data Init Function. + * @param void. + * @retval None. + */ +void UartRecvInit(void) +{ + /* Uart reception initialization */ + FrameRecvClear(&g_uartFrame); + HAL_UART_ReadIT(&g_uart0, &g_uartFrame.rxData, 1); +} + +/** + * @brief Uart Read Data Process Function. + * @param mtrCtrl The motor control handle. + * @retval None. + */ +void UartModuleProcess_Rx(MTRCTRL_Handle *mtrCtrl) +{ + /* Verify Parameters */ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + SetUartDmaStatus(mtrCtrl); + if (g_uartFrame.rxFlag == 1) { /* Receive data flag. */ + if (g_uartFrame.timeOutCnt++ > UART_TIME_OUT_MS) { + /* Received data from the host. */ + g_uartFrame.frameFlag = 1; + g_uartFrame.rxFlag = 0; + g_uartFrame.timeOutCnt = 0; + /* Execute data process. */ + CUST_DataReceProcss(mtrCtrl, g_uartRxBuf); + g_uartFrame.rxAckFlag = 1; + g_uartFrame.rxLen = 0; + } + } + g_uartFrame.frameFlag = 0; +} + +/** + * @brief Uart Write Data Process Function. + * @param mtrCtrl The motor control handle. + * @retval None. + */ +void UartModuleProcess_Tx(MTRCTRL_Handle *mtrCtrl) +{ + /* Verify Parameters */ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + if (g_uartFrame.txFlag == 1) { /* Send data flag. */ + mtrCtrl->uartTimeStamp = (float)getdeltaSystickCnt; /* Unit data time stamp */ + g_uartFrame.upDataCnt = 0; + g_uartFrame.txFlag = 0; + /* Send data to host. */ + unsigned int txLen = CUST_TransmitData(mtrCtrl, g_uartTxBuf); + /* If txIT mode send data finish, convert to DMA mode */ + if (g_uartFrame.uartItTxFlag == 1) { + HAL_UART_WriteDMA(&g_uart0, g_uartTxBuf, txLen); + } + } +} diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/uart_module.h b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/uart_module.h new file mode 100644 index 00000000..f1f8f899 --- /dev/null +++ b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/uart_module.h @@ -0,0 +1,47 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file uart_module.h + * @author MCU Algorithm Team + * @brief This file provides functions declaration of Serial port communication. + */ +#ifndef McsMagicTag_UART_MODULE_H +#define McsMagicTag_UART_MODULE_H + +#include "protocol.h" +#include "mcs_ctlmode_config.h" + +typedef struct { + unsigned int buffLen; + unsigned int timeOutCnt; + unsigned char frameFlag; + unsigned int rxLen; + unsigned char rxFlag; + unsigned char txFlag; + unsigned char rxData; + unsigned int upDataCnt; + unsigned int upDataDelayCnt; + unsigned char uartItTxFlag; + unsigned char rxAckFlag; +} FRAME_Handle; + + +void UartRecvInit(void); +void UartModuleProcess_Rx(MTRCTRL_Handle *mtrCtrl); +void UartModuleProcess_Tx(MTRCTRL_Handle *mtrCtrl); + +#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/readme.md b/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/readme.md new file mode 100644 index 00000000..6af5e381 --- /dev/null +++ b/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/readme.md @@ -0,0 +1,20 @@ +# GIM4310-10_Six_Step_Wave + +**【功能描述】** + +- 基于ECMCU105H/ECBMCU201MPC单板的反电动势采样的无感电机六步方波调速系统应用 + +**【环境要求】** + +- 所有单板电源改制为演示用的24V低压,电机选用GIM4310-10 +- 电机(黄绿蓝)相线分别对应功率板上U/V/W通道接口 + +**【配置方法】** + +- 将user文件夹替换原工程user文件夹即可 + +【操作说明】 + +- 点击开发板按键启动电机 + +- 开发板蓝色电位器,可以调节电机转动的快慢。(12-77HZ) \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/generatecode/feature.h b/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/generatecode/feature.h new file mode 100644 index 00000000..d89f618c --- /dev/null +++ b/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/generatecode/feature.h @@ -0,0 +1,94 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file feature.h + * @author MCU Driver Team + * @brief This file contains macro configurations related to the project. This file is generated by the IDE tool. + * @date 2025-04-23 15:45:47 + */ + +#ifndef McuMagicTag_FEATURE_H +#define McuMagicTag_FEATURE_H + +/* Macro definitions --------------------------------------------------------- */ +#define CHIP_3061MNPICA MACRO_ENABLE + +#define MACRO_ENABLE 1 +#define MACRO_DISABLE 0 + +/* Macro switch */ +#define BASE_DEFINE_USE_ASSERT MACRO_ENABLE +#ifndef FLASH_CRC_CONFIG +#define FLASH_CRC_CONFIG +#endif /* #ifndef FLASH_CRC_CONFIG */ +#define BASE_MATH_SINCOS_MIDDLE_TABLE MACRO_ENABLE /**< This macro is used to control the table type when the + BASE_MATH_GetSinCos() queries the table. When the value of + this macro is MACRO_ENABLE, the error value obtained by the + BASE_MATH_GetSinCos() is relatively small, and the return + value of the function may be greater than or less than the + actual value. When the value of this macro is MACRO_DISABLE, + the error value obtained by the BASE_MATH_GetSinCos() is + relatively large. However, in the range [0°, 180°) and + [180°, 360°), the return value of the function is either + greater than or less than the actual value. */ + +/* Peripheral module macro switch--------------------------------------------- */ +#define BOARD_DIM_NUM 1 /**< Number of dimming handle arrays. */ + +#define BOARD_KEY_NUM 10 /**< Number of key handle arrays. */ +#define BOARD_KEY_PRESS_ON GPIO_HIGH_LEVEL /**< GPIO status corresponding to long press valid. */ +#define BOARD_KEY_PRESS_OFF GPIO_LOW_LEVEL /**< GPIO status corresponding to short press valid. */ + +#define BOARD_LED_SEG_NUM 4 /**< Number of segments. */ +#define BOARD_LED_SEGMENT_ON GPIO_HIGH_LEVEL /**< GPIO level status corresponding to valid segments. */ +#define BOARD_LED_SEGMENT_OFF GPIO_LOW_LEVEL /**< GPIO level status corresponding to invalid segments. */ + +#define BOARD_MKEY_SCHEME_NUMBER BOARD_MKEY_SCHEME_NUMBER_ONE /**< Define the scheme to be adopted. */ +#define BOARD_MKEY_OUT_NUM 4 /**< Number of GPIO pins used as output during scanning. */ +#define BOARD_MKEY_IN_NUM 4 /**< Number of GPIO pins used as input during scanning. */ +#define BOARD_MKEY_OUT_PIN_VALID GPIO_LOW_LEVEL /**< GPIO level status corresponding to the valid \ + status of the output GPIO in the key matrix. */ +#define BOARD_MKEY_OUT_PIN_INVALID GPIO_HIGH_LEVEL /**< GPIO level status corresponding to the \ + invalid status of the output GPIO in the key matrix. */ +#define BOARD_MKEY_IN_PIN_VALID GPIO_LOW_LEVEL /**< Indicates the GPIO level corresponding to the \ + valid status of the input GPIO in the key matrix. */ +#define BOARD_MKEY_IN_PIN_INVALID GPIO_HIGH_LEVEL /**< Indicates the GPIO level corresponding to the \ + invalid status of the input GPIO in the key matrix. */ + +#define BOARD_PULSES_NUM 2 /**< Number of pulse handles. */ + +#define BASE_DEFINE_SLIPAVERAGE_NUM 2 /**< Sliding average array length. */ + +#define LISTNODE_MAX 20 + +#define BASE_DEFINE_DMA_QUICKSTART + +#define XTRAIL_FREQ 30000000U + +#define DBG_USE_NO_PRINTF 0U +#define DBG_USE_UART_PRINTF 1U + +#define DBG_PRINTF_USE DBG_USE_UART_PRINTF +#if (DBG_PRINTF_USE == DBG_USE_UART_PRINTF) +#define DBG_PRINTF_UART_PORT UART0 +#endif + +/* USER CODE BEGIN 0 */ +/* USER CODE 区域内代码不会被覆盖,区域外会被生成的默认代码覆盖(其余USER CODE 区域同理) */ +/* USER CODE END 0 */ + +#endif /* McuMagicTag_FEATURE_H */ \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/generatecode/main.h b/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/generatecode/main.h new file mode 100644 index 00000000..8c5ceec4 --- /dev/null +++ b/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/generatecode/main.h @@ -0,0 +1,84 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file main.h + * @author MCU Driver Team + * @brief This file contains driver init functions. + * @date 2025-04-23 15:45:47 + */ + +/* Define to prevent recursive inclusion ------------------------------------- */ +#ifndef McuMagicTag_SYSTEM_INIT_H +#define McuMagicTag_SYSTEM_INIT_H + +#include "adc.h" +#include "adc_ex.h" +#include "acmp.h" +#include "acmp_ex.h" +#include "apt.h" +#include "uart.h" +#include "uart_ex.h" +#include "gpio.h" +#include "timer.h" +#include "timer_ex.h" +#include "crg.h" +#include "iocmg.h" + +#define IO_SPEED_FAST 0x00U +#define IO_SPEED_SLOW 0x01U + +#define IO_DRV_LEVEL4 0x00U +#define IO_DRV_LEVEL3 0x01U +#define IO_DRV_LEVEL2 0x02U +#define IO_DRV_LEVEL1 0x03U + +#define XTAL_DRV_LEVEL4 0x03U +#define XTAL_DRV_LEVEL3 0x02U +#define XTAL_DRV_LEVEL2 0x01U +#define XTAL_DRV_LEVEL1 0x00U + +#define SYS_LED_PIN GPIO_PIN_3 +#define SYS_LED_HANDLE g_gpio2 +#define GpioStartStop_PIN GPIO_PIN_4 +#define GpioStartStop_HANDLE g_gpio2 + +extern ACMP_Handle g_acmp0; +extern TIMER_Handle g_timer1; +extern UART_Handle g_uart0; +extern APT_Handle g_apt0; +extern APT_Handle g_apt1; +extern APT_Handle g_apt2; +extern ADC_Handle g_adc0; + +extern GPIO_Handle g_gpio1; +extern GPIO_Handle g_gpio2; + +BASE_StatusType CRG_Config(CRG_CoreClkSelect *coreClkSelect); +void SystemInit(void); + +void MotorStatemachineCallBack(void *handle); +void TIMER1_DMAOverFlow_InterruptProcess(void *handle); +void MotorCarrierProcessCallback(void *aptHandle); +void MotorSysErrCallback(void *aptHandle); + +void MotorStartStopKeyCallback(void *param); + +/* USER CODE BEGIN 0 */ +/* USER CODE 区域内代码不会被覆盖,区域外会被生成的默认代码覆盖(其余USER CODE 区域同理) */ +/* USER CODE END 0 */ + +#endif /* McuMagicTag_SYSTEM_INIT_H */ \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/generatecode/system_init.c b/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/generatecode/system_init.c new file mode 100644 index 00000000..c737cc23 --- /dev/null +++ b/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/generatecode/system_init.c @@ -0,0 +1,530 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file system_init.c + * @author MCU Driver Team + * @brief This file contains driver init functions. + * @date 2025-04-23 15:45:47 + */ + +#include "main.h" +#include "ioconfig.h" +#include "iocmg_ip.h" + +#define UART0_BAUD_RATE 115200 + +BASE_StatusType CRG_Config(CRG_CoreClkSelect *coreClkSelect) +{ + CRG_Handle crg; + crg.baseAddress = CRG; + crg.pllRefClkSelect = CRG_PLL_REF_CLK_SELECT_HOSC; + crg.pllPreDiv = CRG_PLL_PREDIV_4; + crg.pllFbDiv = 48; /* PLL Multiplier 48 */ + crg.pllPostDiv = CRG_PLL_POSTDIV_2; + crg.coreClkSelect = CRG_CORE_CLK_SELECT_PLL; + crg.handleEx.pllPostDiv2 = CRG_PLL_POSTDIV2_3; + crg.handleEx.clk1MSelect = CRG_1M_CLK_SELECT_HOSC; + crg.handleEx.clk1MDiv = (25 - 1); /* The 1 MHz freq is equal to the input clock frequency / (clk_1m_div + 1). 25 is the div of the clk_1m in CLOCK. */ + + if (HAL_CRG_Init(&crg) != BASE_STATUS_OK) { + return BASE_STATUS_ERROR; + } + *coreClkSelect = crg.coreClkSelect; + return BASE_STATUS_OK; +} + +static void ACMP0_Init(void) +{ + HAL_CRG_IpEnableSet(ACMP0_BASE, IP_CLK_ENABLE); /* ACMP clock bit reset. */ + g_acmp0.baseAddress = ACMP0_BASE; + g_acmp0.inOutConfig.inputNNum = ACMP_INPUT_N_SELECT2; + g_acmp0.inOutConfig.inputPNum = ACMP_INPUT_P_SELECT2; + g_acmp0.inOutConfig.polarity = ACMP_OUT_NOT_INVERT; + g_acmp0.filterCtrl.filterMode = ACMP_FILTER_NONE; + g_acmp0.hysteresisVol = ACMP_HYS_VOL_30MV; + g_acmp0.interruptEn = BASE_CFG_UNSET; + HAL_ACMP_Init(&g_acmp0); +} + +static void ADC0_Init(void) +{ + HAL_CRG_IpEnableSet(ADC0_BASE, IP_CLK_ENABLE); + HAL_CRG_IpClkSelectSet(ADC0_BASE, CRG_ADC_CLK_ASYN_PLL_DIV); + HAL_CRG_IpClkDivSet(ADC0_BASE, CRG_ADC_DIV_1); + + g_adc0.baseAddress = ADC0; + g_adc0.socPriority = ADC_PRIMODE_ALL_ROUND; + + HAL_ADC_Init(&g_adc0); + + SOC_Param socParam = {0}; + socParam.adcInput = ADC_CH_ADCINA11; /* PIN9(ADC AIN11) */ + socParam.sampleTotalTime = ADC_SOCSAMPLE_5CLK; /* adc sample total time 5 adc_clk */ + socParam.trigSource = ADC_TRIGSOC_APT0_SOCA; + socParam.continueMode = BASE_CFG_DISABLE; + socParam.finishMode = ADC_SOCFINISH_NONE; + HAL_ADC_ConfigureSoc(&g_adc0, ADC_SOC_NUM5, &socParam); + + socParam.adcInput = ADC_CH_ADCINA12; /* PIN10(ADC AIN12) */ + socParam.sampleTotalTime = ADC_SOCSAMPLE_5CLK; /* adc sample total time 5 adc_clk */ + socParam.trigSource = ADC_TRIGSOC_APT0_SOCA; + socParam.continueMode = BASE_CFG_DISABLE; + socParam.finishMode = ADC_SOCFINISH_NONE; + HAL_ADC_ConfigureSoc(&g_adc0, ADC_SOC_NUM2, &socParam); + + socParam.adcInput = ADC_CH_ADCINA10; /* PIN8(ADC AIN10) */ + socParam.sampleTotalTime = ADC_SOCSAMPLE_5CLK; /* adc sample total time 5 adc_clk */ + socParam.trigSource = ADC_TRIGSOC_APT0_SOCA; + socParam.continueMode = BASE_CFG_DISABLE; + socParam.finishMode = ADC_SOCFINISH_NONE; + HAL_ADC_ConfigureSoc(&g_adc0, ADC_SOC_NUM6, &socParam); + + socParam.adcInput = ADC_CH_ADCINA9; /* PIN7(ADC AIN9) */ + socParam.sampleTotalTime = ADC_SOCSAMPLE_5CLK; /* adc sample total time 5 adc_clk */ + socParam.trigSource = ADC_TRIGSOC_SOFT; + socParam.continueMode = BASE_CFG_DISABLE; + socParam.finishMode = ADC_SOCFINISH_NONE; + HAL_ADC_ConfigureSoc(&g_adc0, ADC_SOC_NUM9, &socParam); +} + +__weak void MotorSysErrCallback(void *aptHandle) +{ + BASE_FUNC_UNUSED(aptHandle); + /* USER CODE BEGIN APT0_EVENT_INTERRUPT */ + /* USER CODE END APT0_EVENT_INTERRUPT */ +} + +__weak void MotorCarrierProcessCallback(void *aptHandle) +{ + BASE_FUNC_UNUSED(aptHandle); + /* USER CODE BEGIN APT0_TIMER_INTERRUPT */ + /* USER CODE END APT0_TIMER_INTERRUPT */ +} + +static void APT0_ProtectInit(void) +{ + APT_OutCtrlProtectEx protectApt = {0}; + protectApt.ocEventEnEx = BASE_CFG_ENABLE; + protectApt.ocEventModeEx = APT_OUT_CTRL_ONE_SHOT; + protectApt.ocActionEx = APT_OUT_CTRL_ACTION_LOW; + protectApt.ocActionBEx = APT_OUT_CTRL_ACTION_LOW; + protectApt.ocEvtInterruptEnEx = BASE_CFG_ENABLE; + protectApt.ocSysEvent = APT_SYS_EVT_DEBUG | APT_SYS_EVT_CLK | APT_SYS_EVT_MEM; + protectApt.originalEvtEx = APT_EM_ORIGINAL_SRC_ACMP0; + protectApt.evtPolarityMaskEx = APT_EM_ACMP0_INVERT_BIT; + protectApt.filterCycleNumEx = 0; + HAL_APT_ProtectInitEx(&g_apt0, &protectApt); +} + +static void APT0_Init(void) +{ + HAL_CRG_IpEnableSet(APT0_BASE, IP_CLK_ENABLE); + + g_apt0.baseAddress = APT0; + + /* Clock Settings */ + g_apt0.waveform.dividerFactor = 1 - 1; + /* Timer Settings */ + g_apt0.waveform.timerPeriod = 6250; /* 6250 is count period of APT time-base timer */ + g_apt0.waveform.cntMode = APT_COUNT_MODE_UP_DOWN; + + /* Wave Form */ + g_apt0.waveform.basicType = APT_PWM_BASIC_A_HIGH_B_LOW; + g_apt0.waveform.chAOutType = APT_PWM_OUT_BASIC_TYPE; + g_apt0.waveform.chBOutType = APT_PWM_OUT_BASIC_TYPE; + g_apt0.waveform.divInitVal = 0; + g_apt0.waveform.cntInitVal = 0; + g_apt0.waveform.cntCmpLeftEdge = 3125; /* 3125 is count compare point of the left edge of PWM waveform */ + g_apt0.waveform.cntCmpRightEdge = 3125; /* 3125 is count compare point of the right edge of PWM waveform */ + g_apt0.waveform.cntCmpLoadMode = APT_BUFFER_INDEPENDENT_LOAD; + g_apt0.waveform.cntCmpLoadEvt = APT_COMPARE_LOAD_EVENT_ZERO; + g_apt0.waveform.deadBandCnt = 150; /* 150 is dead-band value */ + + /* ADC Trigger SOCA */ + g_apt0.adcTrg.trgEnSOCA = BASE_CFG_ENABLE; + g_apt0.adcTrg.cntCmpSOCA = 6000; /* 6000 is count compare point of ADC trigger source SOCA when using CMPA */ + g_apt0.adcTrg.trgSrcSOCA = APT_CS_SRC_CNTR_CMPA_DOWN; + g_apt0.adcTrg.trgScaleSOCA = 1; + + /* ADC Trigger SOCB */ + g_apt0.adcTrg.trgEnSOCB = BASE_CFG_ENABLE; + g_apt0.adcTrg.cntCmpSOCB = 1; + g_apt0.adcTrg.trgSrcSOCB = APT_CS_SRC_CNTR_CMPA_DOWN; + g_apt0.adcTrg.trgScaleSOCB = 1; + + g_apt0.adcTrg.cntCmpLoadMode = APT_BUFFER_INDEPENDENT_LOAD; + g_apt0.adcTrg.cntCmpLoadEvt = APT_COMPARE_LOAD_EVENT_ZERO; + + /* Timer Trigger */ + g_apt0.tmrInterrupt.tmrInterruptEn = BASE_CFG_ENABLE; + g_apt0.tmrInterrupt.tmrInterruptSrc = APT_INT_SRC_CNTR_CMPC_UP; + g_apt0.tmrInterrupt.tmrInterruptScale = 1; + + APT0_ProtectInit(); + + HAL_APT_PWMInit(&g_apt0); + HAL_APT_RegisterCallBack(&g_apt0, APT_EVENT_INTERRUPT, MotorSysErrCallback); + IRQ_SetPriority(IRQ_APT0_EVT, 7); /* 7 is priority value */ + IRQ_Register(IRQ_APT0_EVT, HAL_APT_EventIrqHandler, &g_apt0); + IRQ_EnableN(IRQ_APT0_EVT); + HAL_APT_RegisterCallBack(&g_apt0, APT_TIMER_INTERRUPT, MotorCarrierProcessCallback); + IRQ_SetPriority(IRQ_APT0_TMR, 6); /* 6 is priority value */ + IRQ_Register(IRQ_APT0_TMR, HAL_APT_TimerIrqHandler, &g_apt0); + IRQ_EnableN(IRQ_APT0_TMR); +} + +static void APT1_ProtectInit(void) +{ + APT_OutCtrlProtectEx protectApt = {0}; + protectApt.ocEventEnEx = BASE_CFG_ENABLE; + protectApt.ocEventModeEx = APT_OUT_CTRL_ONE_SHOT; + protectApt.ocActionEx = APT_OUT_CTRL_ACTION_LOW; + protectApt.ocActionBEx = APT_OUT_CTRL_ACTION_LOW; + protectApt.ocEvtInterruptEnEx = BASE_CFG_DISABLE; + protectApt.ocSysEvent = APT_SYS_EVT_DEBUG | APT_SYS_EVT_CLK | APT_SYS_EVT_MEM; + protectApt.originalEvtEx = APT_EM_ORIGINAL_SRC_ACMP0; + protectApt.evtPolarityMaskEx = APT_EM_ACMP0_INVERT_BIT; + protectApt.filterCycleNumEx = 0; + HAL_APT_ProtectInitEx(&g_apt1, &protectApt); +} + +static void APT1_Init(void) +{ + HAL_CRG_IpEnableSet(APT1_BASE, IP_CLK_ENABLE); + + g_apt1.baseAddress = APT1; + + /* Clock Settings */ + g_apt1.waveform.dividerFactor = 1 - 1; + /* Timer Settings */ + g_apt1.waveform.timerPeriod = 6250; /* 6250 is count period of APT time-base timer */ + g_apt1.waveform.cntMode = APT_COUNT_MODE_UP_DOWN; + + /* Wave Form */ + g_apt1.waveform.basicType = APT_PWM_BASIC_A_HIGH_B_LOW; + g_apt1.waveform.chAOutType = APT_PWM_OUT_BASIC_TYPE; + g_apt1.waveform.chBOutType = APT_PWM_OUT_BASIC_TYPE; + g_apt1.waveform.divInitVal = 0; + g_apt1.waveform.cntInitVal = 0; + g_apt1.waveform.cntCmpLeftEdge = 3125; /* 3125 is count compare point of the left edge of PWM waveform */ + g_apt1.waveform.cntCmpRightEdge = 3125; /* 3125 is count compare point of the right edge of PWM waveform */ + g_apt1.waveform.cntCmpLoadMode = APT_BUFFER_INDEPENDENT_LOAD; + g_apt1.waveform.cntCmpLoadEvt = APT_COMPARE_LOAD_EVENT_ZERO; + g_apt1.waveform.deadBandCnt = 150; /* 150 is dead-band value */ + + /* ADC Trigger SOCA */ + g_apt1.adcTrg.trgEnSOCA = BASE_CFG_ENABLE; + g_apt1.adcTrg.cntCmpSOCA = 1; /* 1 is count compare point of ADC trigger source SOCA when using CMPA */ + g_apt1.adcTrg.trgSrcSOCA = APT_CS_SRC_CNTR_ZERO; + g_apt1.adcTrg.trgScaleSOCA = 1; + + g_apt1.adcTrg.cntCmpLoadMode = APT_BUFFER_INDEPENDENT_LOAD; + g_apt1.adcTrg.cntCmpLoadEvt = APT_COMPARE_LOAD_EVENT_ZERO; + + APT1_ProtectInit(); + + HAL_APT_PWMInit(&g_apt1); +} + +static void APT2_ProtectInit(void) +{ + APT_OutCtrlProtectEx protectApt = {0}; + protectApt.ocEventEnEx = BASE_CFG_ENABLE; + protectApt.ocEventModeEx = APT_OUT_CTRL_ONE_SHOT; + protectApt.ocActionEx = APT_OUT_CTRL_ACTION_LOW; + protectApt.ocActionBEx = APT_OUT_CTRL_ACTION_LOW; + protectApt.ocEvtInterruptEnEx = BASE_CFG_DISABLE; + protectApt.ocSysEvent = APT_SYS_EVT_DEBUG | APT_SYS_EVT_CLK | APT_SYS_EVT_MEM; + protectApt.originalEvtEx = APT_EM_ORIGINAL_SRC_ACMP0; + protectApt.evtPolarityMaskEx = APT_EM_ACMP0_INVERT_BIT; + protectApt.filterCycleNumEx = 0; + HAL_APT_ProtectInitEx(&g_apt2, &protectApt); +} + +static void APT2_Init(void) +{ + HAL_CRG_IpEnableSet(APT2_BASE, IP_CLK_ENABLE); + + g_apt2.baseAddress = APT2; + + /* Clock Settings */ + g_apt2.waveform.dividerFactor = 1 - 1; + /* Timer Settings */ + g_apt2.waveform.timerPeriod = 6250; /* 6250 is count period of APT time-base timer */ + g_apt2.waveform.cntMode = APT_COUNT_MODE_UP_DOWN; + + /* Wave Form */ + g_apt2.waveform.basicType = APT_PWM_BASIC_A_HIGH_B_LOW; + g_apt2.waveform.chAOutType = APT_PWM_OUT_BASIC_TYPE; + g_apt2.waveform.chBOutType = APT_PWM_OUT_BASIC_TYPE; + g_apt2.waveform.divInitVal = 0; + g_apt2.waveform.cntInitVal = 0; + g_apt2.waveform.cntCmpLeftEdge = 3125; /* 3125 is count compare point of the left edge of PWM waveform */ + g_apt2.waveform.cntCmpRightEdge = 3125; /* 3125 is count compare point of the right edge of PWM waveform */ + g_apt2.waveform.cntCmpLoadMode = APT_BUFFER_INDEPENDENT_LOAD; + g_apt2.waveform.cntCmpLoadEvt = APT_COMPARE_LOAD_EVENT_ZERO; + g_apt2.waveform.deadBandCnt = 150; /* 150 is dead-band value */ + + APT2_ProtectInit(); + + HAL_APT_PWMInit(&g_apt2); +} + +__weak void MotorStartStopKeyCallback(void *param) +{ + GPIO_Handle *handle = (GPIO_Handle *)param; + BASE_FUNC_UNUSED(handle); +} + +static void GPIO_Init(void) +{ + HAL_CRG_IpEnableSet(GPIO1_BASE, IP_CLK_ENABLE); + g_gpio1.baseAddress = GPIO1; + + g_gpio1.pins = GPIO_PIN_0; + HAL_GPIO_Init(&g_gpio1); + HAL_GPIO_SetDirection(&g_gpio1, g_gpio1.pins, GPIO_OUTPUT_MODE); + HAL_GPIO_SetValue(&g_gpio1, g_gpio1.pins, GPIO_HIGH_LEVEL); + HAL_GPIO_SetIrqType(&g_gpio1, g_gpio1.pins, GPIO_INT_TYPE_NONE); + + HAL_CRG_IpEnableSet(GPIO2_BASE, IP_CLK_ENABLE); + g_gpio2.baseAddress = GPIO2; + + g_gpio2.pins = GPIO_PIN_3; + HAL_GPIO_Init(&g_gpio2); + HAL_GPIO_SetDirection(&g_gpio2, g_gpio2.pins, GPIO_OUTPUT_MODE); + HAL_GPIO_SetValue(&g_gpio2, g_gpio2.pins, GPIO_HIGH_LEVEL); + HAL_GPIO_SetIrqType(&g_gpio2, g_gpio2.pins, GPIO_INT_TYPE_NONE); + + g_gpio2.pins = GPIO_PIN_4; + HAL_GPIO_Init(&g_gpio2); + HAL_GPIO_SetDirection(&g_gpio2, g_gpio2.pins, GPIO_INPUT_MODE); + HAL_GPIO_SetValue(&g_gpio2, g_gpio2.pins, GPIO_LOW_LEVEL); + HAL_GPIO_SetIrqType(&g_gpio2, g_gpio2.pins, GPIO_INT_TYPE_FALL_EDGE); + + HAL_GPIO_RegisterCallBack(&g_gpio2, GPIO_PIN_4, MotorStartStopKeyCallback); + IRQ_Register(IRQ_GPIO2, HAL_GPIO_IrqHandler, &g_gpio2); + IRQ_SetPriority(IRQ_GPIO2, 1); /* set gpio1 interrupt priority to 1, 1~15. 1 is priority value */ + IRQ_EnableN(IRQ_GPIO2); /* gpio interrupt enable */ + + return; +} + +__weak void MotorStatemachineCallBack(void *handle) +{ + BASE_FUNC_UNUSED(handle); + /* USER CODE BEGIN MotorStatemachineCallBack */ + /* USER CODE END MotorStatemachineCallBack */ +} + +static void TIMER1_Init(void) +{ + HAL_CRG_IpEnableSet(TIMER1_BASE, IP_CLK_ENABLE); /* TIMER1 clock enable. */ + unsigned int load = ((float)HAL_CRG_GetIpFreq((void *)TIMER1) / (1u << (TIMERPRESCALER_NO_DIV * 4)) / 1000000u) * 500; + + g_timer1.baseAddress = TIMER1; + g_timer1.load = (load < 1) ? 0 : (load - 1); /* Set timer value immediately */ + g_timer1.bgLoad = (load < 1) ? 0 : (load - 1); /* Set timer value */ + g_timer1.mode = TIMER_MODE_RUN_PERIODIC; /* Run in period mode */ + g_timer1.prescaler = TIMERPRESCALER_NO_DIV; /* Don't frequency division */ + g_timer1.size = TIMER_SIZE_32BIT; /* 1 for 32bit, 0 for 16bit */ + g_timer1.interruptEn = BASE_CFG_ENABLE; + g_timer1.adcSocReqEnable = BASE_CFG_DISABLE; + g_timer1.dmaReqEnable = BASE_CFG_DISABLE; + HAL_TIMER_Init(&g_timer1); + IRQ_Register(IRQ_TIMER1, HAL_TIMER_IrqHandler, &g_timer1); + + HAL_TIMER_RegisterCallback(&g_timer1, TIMER_PERIOD_FIN, MotorStatemachineCallBack); + IRQ_SetPriority(IRQ_TIMER1, 2); /* 2 is priority value */ + IRQ_EnableN(IRQ_TIMER1); +} + +static void UART0_Init(void) +{ + HAL_CRG_IpEnableSet(UART0_BASE, IP_CLK_ENABLE); /* UART0 clock enable. */ + g_uart0.baseAddress = UART0; + + g_uart0.baudRate = UART0_BAUD_RATE; + g_uart0.dataLength = UART_DATALENGTH_8BIT; + g_uart0.stopBits = UART_STOPBITS_ONE; + g_uart0.parity = UART_PARITY_NONE; + g_uart0.txMode = UART_MODE_BLOCKING; + g_uart0.rxMode = UART_MODE_BLOCKING; + g_uart0.fifoMode = BASE_CFG_ENABLE; + g_uart0.fifoTxThr = UART_FIFODEPTH_SIZE8; + g_uart0.fifoRxThr = UART_FIFODEPTH_SIZE8; + g_uart0.hwFlowCtr = BASE_CFG_DISABLE; + g_uart0.handleEx.overSampleMultiple = UART_OVERSAMPLING_16X; + g_uart0.handleEx.msbFirst = BASE_CFG_DISABLE; + HAL_UART_Init(&g_uart0); +} + +static void IOConfig(void) +{ + SYSCTRL0->SC_SYS_STAT.BIT.update_mode = 0; + SYSCTRL0->SC_SYS_STAT.BIT.update_mode_clear = 1; + /* Config PIN9 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO3_5_AS_ADC_AIN11); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO3_5_AS_ADC_AIN11, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO3_5_AS_ADC_AIN11, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO3_5_AS_ADC_AIN11, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO3_5_AS_ADC_AIN11, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN10 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO5_3_AS_ADC_AIN12); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO5_3_AS_ADC_AIN12, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO5_3_AS_ADC_AIN12, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO5_3_AS_ADC_AIN12, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO5_3_AS_ADC_AIN12, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN8 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO3_6_AS_ADC_AIN10); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO3_6_AS_ADC_AIN10, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO3_6_AS_ADC_AIN10, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO3_6_AS_ADC_AIN10, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO3_6_AS_ADC_AIN10, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN7 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO3_7_AS_ADC_AIN9); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO3_7_AS_ADC_AIN9, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO3_7_AS_ADC_AIN9, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO3_7_AS_ADC_AIN9, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO3_7_AS_ADC_AIN9, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN48 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO0_6_AS_ACMP_N2); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO0_6_AS_ACMP_N2, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO0_6_AS_ACMP_N2, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO0_6_AS_ACMP_N2, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO0_6_AS_ACMP_N2, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN47 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO0_5_AS_ACMP_P2); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO0_5_AS_ACMP_P2, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO0_5_AS_ACMP_P2, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO0_5_AS_ACMP_P2, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO0_5_AS_ACMP_P2, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN1 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO0_7_AS_ACMP0_OUT); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO0_7_AS_ACMP0_OUT, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO0_7_AS_ACMP0_OUT, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO0_7_AS_ACMP0_OUT, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO0_7_AS_ACMP0_OUT, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN27 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO1_0_AS_GPIO1_0); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO1_0_AS_GPIO1_0, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO1_0_AS_GPIO1_0, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO1_0_AS_GPIO1_0, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO1_0_AS_GPIO1_0, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN35 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO2_3_AS_GPIO2_3); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO2_3_AS_GPIO2_3, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO2_3_AS_GPIO2_3, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO2_3_AS_GPIO2_3, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO2_3_AS_GPIO2_3, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN41 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO2_4_AS_GPIO2_4); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO2_4_AS_GPIO2_4, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO2_4_AS_GPIO2_4, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO2_4_AS_GPIO2_4, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO2_4_AS_GPIO2_4, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN39 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO0_3_AS_UART0_TXD); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO0_3_AS_UART0_TXD, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO0_3_AS_UART0_TXD, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO0_3_AS_UART0_TXD, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO0_3_AS_UART0_TXD, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN40 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO0_4_AS_UART0_RXD); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO0_4_AS_UART0_RXD, PULL_UP); /* Pull-up and Pull-down, UART RX recommend PULL_UP */ + HAL_IOCMG_SetPinSchmidtMode(GPIO0_4_AS_UART0_RXD, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO0_4_AS_UART0_RXD, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO0_4_AS_UART0_RXD, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN19 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO3_0_AS_APT0_PWMA); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO3_0_AS_APT0_PWMA, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO3_0_AS_APT0_PWMA, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO3_0_AS_APT0_PWMA, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO3_0_AS_APT0_PWMA, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN23 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO4_0_AS_APT0_PWMB); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO4_0_AS_APT0_PWMB, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO4_0_AS_APT0_PWMB, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO4_0_AS_APT0_PWMB, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO4_0_AS_APT0_PWMB, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN20 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO3_1_AS_APT1_PWMA); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO3_1_AS_APT1_PWMA, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO3_1_AS_APT1_PWMA, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO3_1_AS_APT1_PWMA, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO3_1_AS_APT1_PWMA, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN24 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO4_1_AS_APT1_PWMB); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO4_1_AS_APT1_PWMB, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO4_1_AS_APT1_PWMB, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO4_1_AS_APT1_PWMB, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO4_1_AS_APT1_PWMB, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN21 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO3_2_AS_APT2_PWMA); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO3_2_AS_APT2_PWMA, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO3_2_AS_APT2_PWMA, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO3_2_AS_APT2_PWMA, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO3_2_AS_APT2_PWMA, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN25 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO4_2_AS_APT2_PWMB); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO4_2_AS_APT2_PWMB, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO4_2_AS_APT2_PWMB, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO4_2_AS_APT2_PWMB, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO4_2_AS_APT2_PWMB, DRIVER_RATE_2); /* Output signal edge fast/slow */ +} + +static void APT_SyncMasterInit(void) +{ + HAL_APT_MasterSyncInit(&g_apt0, APT_SYNC_OUT_ON_CNTR_ZERO); +} + +static void APT_SyncSlaveInit(void) +{ + APT_SlaveSyncIn aptSlave; + + aptSlave.cntPhase = 0; /* counter phase value */ + aptSlave.syncCntMode = APT_COUNT_MODE_AFTER_SYNC_UP; + aptSlave.syncInSrc = APT_SYNCIN_SRC_APT0_SYNCOUT; /* sync source selection */ + aptSlave.cntrSyncSrc = APT_CNTR_SYNC_SRC_SYNCIN; + HAL_APT_SlaveSyncInit(&g_apt1, &aptSlave); + + aptSlave.cntPhase = 0; /* counter phase value */ + aptSlave.syncCntMode = APT_COUNT_MODE_AFTER_SYNC_UP; + aptSlave.syncInSrc = APT_SYNCIN_SRC_APT0_SYNCOUT; /* sync source selection */ + aptSlave.cntrSyncSrc = APT_CNTR_SYNC_SRC_SYNCIN; + HAL_APT_SlaveSyncInit(&g_apt2, &aptSlave); + +} + +void SystemInit(void) +{ + IOConfig(); + UART0_Init(); + ACMP0_Init(); + APT0_Init(); + APT1_Init(); + APT2_Init(); + ADC0_Init(); + TIMER1_Init(); + GPIO_Init(); + + APT_SyncMasterInit(); + APT_SyncSlaveInit(); + + /* USER CODE BEGIN system_init */ + /* USER CODE END system_init */ +} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/main.c b/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/main.c new file mode 100644 index 00000000..83b0b133 --- /dev/null +++ b/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/main.c @@ -0,0 +1,67 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file main.c + * @author MCU Driver Team + * @brief Main program body. + * @date 2025-04-23 15:45:47 + */ + +#include "typedefs.h" +#include "feature.h" +#include "mcs_motor_process.h" +#include "main.h" +/* USER CODE BEGIN 0 */ +/* USER CODE 区域内代码不会被覆盖,区域外会被生成的默认代码覆盖(其余USER CODE 区域同理) */ +/* 建议用户放置头文件 */ +/* USER CODE END 0 */ +ACMP_Handle g_acmp0; +TIMER_Handle g_timer1; +UART_Handle g_uart0; +APT_Handle g_apt0; +APT_Handle g_apt1; +APT_Handle g_apt2; +ADC_Handle g_adc0; +GPIO_Handle g_gpio1; +GPIO_Handle g_gpio2; +/* USER CODE BEGIN 1 */ +/* 建议用户定义全局变量、结构体、宏定义或函数声明等 */ +/* USER CODE END 1 */ + +int main(void) +{ + /* USER CODE BEGIN 2 */ + /* 建议用户放置初始化代码或启动代码等 */ + /* USER CODE END 2 */ + MotorMainProcess(); + /* USER CODE BEGIN 3 */ + /* 建议用户放置初始配置代码 */ + /* USER CODE END 3 */ + while (1) { + /* USER CODE BEGIN 4 */ + /* 建议用户放置周期性执行代码 */ + /* USER CODE END 4 */ + } + /* USER CODE BEGIN 5 */ + /* 建议用户放置代码流程 */ + /* USER CODE END 5 */ + return BASE_STATUS_OK; +} + +/* USER CODE BEGIN 6 */ +/* 建议用户放置自定义函数 */ +/* USER CODE END 6 */ \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/application/mcs_motor_process.c b/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/application/mcs_motor_process.c new file mode 100644 index 00000000..d53b28d7 --- /dev/null +++ b/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/application/mcs_motor_process.c @@ -0,0 +1,562 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_motor_process.c + * @author MCU Algorithm Team + * @brief This file provides motor application for ECBMCU201MPC board. + * @details BLDC six-step application based on the ECBMCU201MPC board + * 1) Motor model is Gimbal GBM2804H-100T. + * 2) Select the bldc six-step sensorless example in the sample column + of chipConfig and click Generate Code. + * 3) It's power supply must be changed to 12V. + */ + +#include "debug.h" +#include "mcs_assert.h" +#include "mcs_user_config.h" +#include "mcs_status.h" +#include "mcs_carrier.h" +#include "mcs_motor_process.h" + +/*------------------------------- Macro Definition -----------------------------------------------*/ +#define US_PER_MS 1000 +#define APT_FULL_DUTY 1.0f + +/* Motor control handle for bldc */ +static MtrCtrlHandle g_mc; + +static APT_RegStruct* g_aptCp[PHASE_MAX_NUM] = {BRIDGE_CTR_APT_U, BRIDGE_CTR_APT_V, BRIDGE_CTR_APT_W}; + +/** + * @brief Initialzer of system tick. + * @param mtrCtrl Motor control struct handle. + * @retval None. + */ +static void TimerTickInit(MtrCtrlHandle *mtrCtrl) +{ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + mtrCtrl->sysTickCnt = 0; + mtrCtrl->msTickNum = US_PER_MS / SYSTICK_PERIOD_US; + mtrCtrl->capChargeTickNum = (INV_CAP_CHARGE_MS * US_PER_MS / SYSTICK_PERIOD_US); +} + +/** + * @brief Read the ADC current sampling value of the compressor. + * @param CurrUvw Three-phase current. + * @retval None. + */ +static void ReadBemfUVWMotor(UVWBemf *bemfUVW) +{ + MCS_ASSERT_PARAM(bemfUVW != NULL); + bemfUVW->u = (int)(HAL_ADC_GetConvResult(&ADC_HANDLE, ADC_U_SOC_NUM)&0xFFF); + bemfUVW->v = (int)(HAL_ADC_GetConvResult(&ADC_HANDLE, ADC_V_SOC_NUM)&0xFFF); + bemfUVW->w = (int)(HAL_ADC_GetConvResult(&ADC_HANDLE, ADC_W_SOC_NUM)&0xFFF); +} + +/** + * @brief Init motor control task. + * @retval None. + */ +static void TSK_InitMotor(void) +{ + /* Initialize target speed. */ + g_mc.spdCmdHz = SDP_TARGET_VALUE; + /* zeroPoint = IN_VOLTAGE_BUS / 2.0; 4095/3.3 :ADC value corresponding to 1 V */ + g_mc.zeroPoint = ((((float)IN_VOLTAGE_BUS / 2.0) * VOL_DIVIDER_COEFFICIENT) * 4095 / 3.3); + g_mc.pwmDuty = FORCE_DRAG_MINDUTY; + + /* Sets the number of sample filtering times for zero-crossing sampling. */ + g_mc.sysVar.bemfFilterCnt = FILTER_COUNT; + g_mc.sysVar.dragChangePhaseTime = DRAG_START_INTERVAL; + + g_mc.stateMachine = FSM_IDLE; + g_mc.aptMaxcntCmp = g_apt0.waveform.timerPeriod; + + /* Speed management initialization. */ + RMG_Init(&g_mc.spdRmg, CTRL_SYSTICK_PERIOD, USER_SPD_SLOPE); /* Init speed slope */ + TimerTickInit(&g_mc); + + /* Pid control parameter initialization. */ + g_mc.spdPi.kp = SPD_PID_KP; + g_mc.spdPi.ki = SPD_PID_KI; + g_mc.spdPi.ts = SPD_PID_TS; + g_mc.spdPi.upperLimit = APT_DUTYLIMIT_MAX; + g_mc.spdPi.lowerLimit = APT_DUTYLIMIT_MIN; + + /* BLDC six-step control initialization. */ + g_mc.stepCtrl.phaseStep = STEP1; + g_mc.stepCtrl.controlApt.u = &g_apt0; + g_mc.stepCtrl.controlApt.v = &g_apt1; + g_mc.stepCtrl.controlApt.w = &g_apt2; +} + + +/** + * @brief Software Initialization. + * @retval None. + */ +static void InitSoftware(void) +{ + TSK_InitMotor(); + + g_mc.readBemfUVW = ReadBemfUVWMotor; +} + +/** + * @brief Enable three-phase pwm output. + * @param aptAddr Three-phase APT address pointer. + * @retval None. + */ +static void MotorPwmOutputEnable(APT_RegStruct **aptAddr) +{ + MCS_ASSERT_PARAM(aptAddr != NULL); + /* Enable three-phase pwm output */ + for (unsigned int i = 0; i < PHASE_MAX_NUM; i++) { + APT_RegStruct *aptx = aptAddr[i]; + aptx->PG_OUT_FRC.BIT.rg_pga_frc_en = BASE_CFG_UNSET; + aptx->PG_OUT_FRC.BIT.rg_pgb_frc_en = BASE_CFG_UNSET; + } +} + +/** + * @brief Disable three-phase pwm output. + * @param aptAddr Three-phase APT address pointer. + * @retval None. + */ +static void MotorPwmOutputDisable(APT_RegStruct **aptAddr) +{ + MCS_ASSERT_PARAM(aptAddr != NULL); + /* Disable three-phase pwm output. */ + for (unsigned int i = 0; i < PHASE_MAX_NUM; i++) { + APT_RegStruct *aptx = aptAddr[i]; + aptx->PG_OUT_FRC.BIT.rg_pga_frc_en = BASE_CFG_SET; + aptx->PG_OUT_FRC.BIT.rg_pgb_frc_en = BASE_CFG_SET; + DCL_APT_ForcePWMOutputLow(aptx); + } +} + +/** + * @brief Force rotor alignment to home position. + * @param mtrCtrl The motor control handle. + * @retval None. + */ +static void ForceAlign(MtrCtrlHandle *mtrCtrl) +{ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + mtrCtrl->pwmDuty = FORCE_DRAG_MINDUTY; + mtrCtrl->sysVar.dragChangePhaseTime = DRAG_START_INTERVAL; + MCS_SetCtrAptDuty(mtrCtrl, mtrCtrl->pwmDuty); + MotorPwmOutputEnable(g_aptCp); + mtrCtrl->stepCtrl.phaseStep = STEP6; + SixStepPwm(&mtrCtrl->stepCtrl); + BASE_FUNC_DELAY_MS(100); /* Delay 100 ms waiting for rotor alignment. */ + mtrCtrl->stepCtrl.phaseStep = STEP1; + SixStepPwm(&mtrCtrl->stepCtrl); + BASE_FUNC_DELAY_MS(100); /* Delay 100 ms waiting for rotor alignment. */ + mtrCtrl->sysVar.lastZeroPoint = DCL_SYSTICK_GetTick(); +} + +/** + * @brief Pre-processing of motor status. + * @param statusReg System status. + * @param stateMachine Motor Control Status. + * @retval None. + */ +static void MotorStatePerProc(SysStatusReg *statusReg, volatile FsmState *stateMachine) +{ + MCS_ASSERT_PARAM(statusReg != NULL); + MCS_ASSERT_PARAM(stateMachine != NULL); + /* Get system status */ + if (SysIsError(statusReg)) { + *stateMachine = FSM_FAULT; + } + if (SysGetCmdStop(statusReg)) { + SysCmdStopClr(statusReg); + *stateMachine = FSM_WAIT_STOP; + } +} + +/** + * @brief Open the three-phase lower pipe. + * @param aptAddr Three-phase APT address pointer. + * @param maxDutyCnt Max duty count. + * @retval None. + */ +static void AptTurnOnLowSidePwm(APT_RegStruct **aptAddr, unsigned int maxDutyCnt) +{ + MCS_ASSERT_PARAM(aptAddr != NULL); + MCS_ASSERT_PARAM(maxDutyCnt != 0); + unsigned int dutyCnt; + dutyCnt = maxDutyCnt * APT_FULL_DUTY; + /* Open the three-phase lower pipe */ + for (unsigned int i = 0; i < PHASE_MAX_NUM; i++) { + APT_RegStruct *aptx = aptAddr[i]; + aptx->TC_REFC.BIT.rg_cnt_refc = dutyCnt; + aptx->TC_REFD.BIT.rg_cnt_refd = dutyCnt; + } +} + +/** + * @brief Check systerm cmd start signal. + * @param mtrCtrl The motor control handle. + * @param aptAddr Three-phase APT address pointer. + * @param statusReg System status. + * @param stateMachine Motor Control Status. + * @retval None. + */ +static void CheckSysCmdStart(MtrCtrlHandle *mtrCtrl, APT_RegStruct **aptAddr, SysStatusReg *statusReg, + FsmState *stateMachine) +{ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + MCS_ASSERT_PARAM(aptAddr != NULL); + MCS_ASSERT_PARAM(statusReg != NULL); + MCS_ASSERT_PARAM(stateMachine != NULL); + /* check start cmd */ + if (SysGetCmdStart(statusReg)) { + SysRunningSet(statusReg); + SysCmdStartClr(statusReg); + mtrCtrl->sysTickCnt = 0; + *stateMachine = FSM_CAP_CHARGE; + /* Preparation for charging the bootstrap capacitor. */ + AptTurnOnLowSidePwm(aptAddr, mtrCtrl->aptMaxcntCmp); + /* Out put pwm */ + MotorPwmOutputEnable(aptAddr); + } +} + +/** + * @brief Clear historical values of all controller before start-up. + * @param mtrCtrl The motor control handle. + * @retval None. + */ +static void ClearBeforeStartup(MtrCtrlHandle *mtrCtrl) +{ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + mtrCtrl->aptMaxcntCmp = g_apt0.waveform.timerPeriod; + + /* Clear bemf parameters. */ + mtrCtrl->bemf.u = 0; + mtrCtrl->bemf.v = 0; + mtrCtrl->bemf.w = 0; + + /* Clear tickcnt. */ + mtrCtrl->msTickCnt = 0; + + mtrCtrl->pwmDuty = FORCE_DRAG_MINDUTY; + + /* Clear spd parameters. */ + mtrCtrl->spdEstHz = 0; + mtrCtrl->spdRefHz = 0; + + mtrCtrl->stepCtrl.phaseStep = STEP1; + + mtrCtrl->sysTickCnt = 0; + + mtrCtrl->sysVar.dragChangePhaseTime = DRAG_START_INTERVAL; + mtrCtrl->sysVar.changePhaseFlag = 0; + mtrCtrl->sysVar.firstEventFilterFlag = 0; + mtrCtrl->sysVar.stepTimeNum = 0; + mtrCtrl->sysVar.stepTimeFilterEnable = 0; + for (int i = 0; i < STEP_MAX_NUM; i++) { + mtrCtrl->sysVar.stepTime[i] = 0; + } + /* RMG CLEAR */ + RMG_Clear(&mtrCtrl->spdRmg); /* Clear the history value of speed slope control */ + /* SPDCTRL CLEAR */ + PID_Clear(&mtrCtrl->spdPi); +} + +/** + * @brief Check bootstrap capacitor charge time. + * @param mtrCtrl The motor control handle. + * @param stateMachine Motor Control Status. + * @retval None. + */ +static void CheckBootstrpCapChargeTime(MtrCtrlHandle *mtrCtrl, FsmState *stateMachine) +{ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + MCS_ASSERT_PARAM(stateMachine != NULL); + mtrCtrl->sysTickCnt++; + /* check bootstrap capacitor charge time */ + if (mtrCtrl->sysTickCnt == mtrCtrl->capChargeTickNum) { + /* Update Status. */ + *stateMachine = FSM_CLEAR; + } +} + +/** + * @brief Check over current status. + * @param statusReg System status. + * @param stateMachine Motor Control Status. + * @retval None. + */ +static void CheckOverCurrentState(SysStatusReg *statusReg, FsmState *stateMachine) +{ + MCS_ASSERT_PARAM(statusReg != NULL); + MCS_ASSERT_PARAM(stateMachine != NULL); + /* check systerm error status */ + if (SysIsError(statusReg) == false) { + *stateMachine = FSM_IDLE; + } +} + +/** + * @brief Deceleration control phase before stop. + * @param mtrCtrl The motor control handle. + * @param statusReg Motor Control Status. + * @param targetSpd Deceleration target value. + * @param switchSpd Switch to stop state speed. + * @retval None. + */ +static void DecelerateSpeed(MtrCtrlHandle *mtrCtrl, FsmState *stateMachine, float targetSpd, float switchSpd) +{ + /* Reduce speed before stop. */ + mtrCtrl->spdRefHz = RMG_Exec(&mtrCtrl->spdRmg, targetSpd); + mtrCtrl->spdPi.error = mtrCtrl->spdRefHz - mtrCtrl->spdEstHz; + /* Speed loop control */ + mtrCtrl->pwmDuty = PI_Exec(&mtrCtrl->spdPi); + if (mtrCtrl->spdRefHz <= switchSpd) { /* Maximum speed for switching to the FSM_STOP state */ + *stateMachine = FSM_STOP; + } +} + +/** + * @brief System timer tick task. + * @param mtrCtrl The motor control handle. + * @param aptAddr Three-phase APT address pointer. + * @retval None. + */ +static void TSK_SystickIsr(MtrCtrlHandle *mtrCtrl, APT_RegStruct **aptAddr) +{ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + MCS_ASSERT_PARAM(aptAddr != NULL); + SysStatusReg *statusReg = &mtrCtrl->statusReg; + volatile FsmState *stateMachine = &mtrCtrl->stateMachine; + mtrCtrl->msTickCnt++; + /* Pre-processing of motor status. */ + MotorStatePerProc(statusReg, stateMachine); + /* statemachine */ + switch (*stateMachine) { + case FSM_IDLE: + CheckSysCmdStart(mtrCtrl, aptAddr, statusReg, (FsmState *)stateMachine); + break; + case FSM_CAP_CHARGE: + /* Bootstrap Capacitor Charging Timing */ + CheckBootstrpCapChargeTime(mtrCtrl, (FsmState *)stateMachine); + break; + case FSM_CLEAR: + /* Clearing control parameters. */ + ClearBeforeStartup(mtrCtrl); + /* Rotor alignment. */ + ForceAlign(mtrCtrl); + *stateMachine = FSM_STARTUP; + break; + case FSM_STARTUP: + /* Forced drag. */ + break; + case FSM_RUN: + /* Speed ramp control */ + mtrCtrl->spdRefHz = RMG_Exec(&mtrCtrl->spdRmg, mtrCtrl->spdCmdHz); + mtrCtrl->spdPi.error = mtrCtrl->spdRefHz - mtrCtrl->spdEstHz; + /* Speed loop control */ + mtrCtrl->pwmDuty = PI_Exec(&mtrCtrl->spdPi); + break; + case FSM_WAIT_STOP: + /* 5.0f : maximum speed for switching to the FSM_STOP state */ + DecelerateSpeed(mtrCtrl, (FsmState *)stateMachine, 0.0f, 5.0f); + break; + case FSM_STOP: + MotorPwmOutputDisable(aptAddr); + SysRunningClr(statusReg); + *stateMachine = FSM_IDLE; + break; + case FSM_FAULT: + /* Overcurrent state */ + CheckOverCurrentState(statusReg, (FsmState *)stateMachine); + break; + default: + break; + } +} + +/** + * @brief Overcurrent protection. + * @param aptHandle The apt Handle. + * @retval None. + */ +void MotorSysErrCallback(void *aptHandle) +{ + /* Overcurrent protection callback function. */ + BASE_FUNC_UNUSED(aptHandle); + MotorPwmOutputDisable(g_aptCp); + DCL_APT_ClearOutCtrlEventFlag((APT_RegStruct *)g_aptCp[PHASE_U], APT_OC_COMBINE_EVENT_A1); + DCL_APT_ClearOutCtrlEventFlag((APT_RegStruct *)g_aptCp[PHASE_V], APT_OC_COMBINE_EVENT_A1); + DCL_APT_ClearOutCtrlEventFlag((APT_RegStruct *)g_aptCp[PHASE_W], APT_OC_COMBINE_EVENT_A1); + /* Status setting error */ + SysErrorSet(&g_mc.statusReg); + DBG_PRINTF("APT error! \r\n"); + HAL_GPIO_SetValue(&g_gpio2, GPIO_PIN_3, GPIO_LOW_LEVEL); +} + +/** + * @brief Motor blockage protection function. + * @retval None. + */ +static void MotorBlockageProtect(void) +{ + unsigned int currentTick = DCL_SYSTICK_GetTick(); /* Get the current tick value. */ + unsigned int intervalTick = (currentTick >= g_mc.sysVar.lastZeroPoint) + ? currentTick - g_mc.sysVar.lastZeroPoint + : SYSTICK_MAX_VALUE - g_mc.sysVar.lastZeroPoint + currentTick + 1; + if (intervalTick > SYSTICK_GetCRGHZ()) { + MotorPwmOutputDisable(g_aptCp); + SysErrorSet(&g_mc.statusReg); + g_mc.spdEstHz = 0; + return; + } +} + +/** + * @brief Carrier Interruption. + * @param aptHandle The apt Handle. + * @retval None. + */ +void MotorCarrierProcessCallback(void *aptHandle) +{ + BASE_FUNC_UNUSED(aptHandle); + /* USER CODE BEGIN APT0_TIMER_INTERRUPT */ + MCS_CarrierProcess(&g_mc); + if (g_mc.stateMachine == FSM_RUN || g_mc.stateMachine == FSM_WAIT_STOP) { + MotorBlockageProtect(); + } + /* USER CODE END APT0_TIMER_INTERRUPT */ +} + +/** + * @brief Change phase delay callback function. + * @param handle The TIMER1 Handle. + * @retval None. + */ +void MotorStatemachineCallBack(void *handle) +{ + /* TIMER1CallbackFunction */ + BASE_FUNC_UNUSED(handle); + TSK_SystickIsr(&g_mc, g_aptCp); +} + +/** + * @brief Check Potentiometer Value and adjust speed. + * @param None. + * @retval None. + */ +static void AdjustSpeedFunction(void) +{ + static unsigned int potentiomitorAdcValue = 0; + static float spdCmdHz = 0; + static float spdCmdHzLast = SDP_MAX_VALUE; + HAL_ADC_SoftTrigSample(&ADC_HANDLE, ADC_SOC_NUM9); /* Get the speed adjustment resistance. */ + potentiomitorAdcValue = HAL_ADC_GetConvResult(&ADC_HANDLE, ADC_SOC_NUM9); + /* 4045.0 is adc sample max value of potentiomitor */ + spdCmdHz = (float)potentiomitorAdcValue / 4045.0 * SDP_MAX_VALUE; + if (spdCmdHz < SDP_MIN_VALUE) { /* Speed protection. */ + spdCmdHz = SDP_MIN_VALUE; + } + if (spdCmdHz > SDP_MAX_VALUE) { + spdCmdHz = SDP_MAX_VALUE; + } + float delta = spdCmdHzLast > spdCmdHz ? (spdCmdHzLast - spdCmdHz) : (spdCmdHz - spdCmdHzLast); + /* 1.0 : If the speed fluctuation is less than 1.0Hz, no change is made. */ + if (delta < 1.0) { + return; + } + spdCmdHzLast = spdCmdHz; + g_mc.spdCmdHz = spdCmdHz; +} + +/** + * @brief Config the KEY func. + * @param handle The GPIO handle. + * @retval None. + */ +static KEY_State Key_StateRead(GPIO_Handle *handle) +{ + if (HAL_GPIO_GetPinValue(handle, handle->pins) == 0) { + BASE_FUNC_DELAY_MS(30); /* delay 30ms for deshake */ + if (HAL_GPIO_GetPinValue(handle, handle->pins) == 0) { + while (HAL_GPIO_GetPinValue(handle, handle->pins) == 0) { + } + return KEY_DOWN; + } + } + return KEY_UP; +} + +/** + * @brief Control motor start and stop state by key func. + * @param param The GPIO handle. + * @retval None. + */ +void MotorStartStopKeyCallback(void *param) +{ + GPIO_Handle *handle = (GPIO_Handle *)param; + if (Key_StateRead(handle) == KEY_DOWN) { + if (SysIsRunning(&g_mc.statusReg)) { /* stop apt output, motor is off status */ + SysCmdStopSet(&g_mc.statusReg); + } else { /* start apt output, motor is on status */ + HAL_APT_StartModule(RUN_APT0 | RUN_APT1 | RUN_APT2); + SysCmdStartSet(&g_mc.statusReg); + } + } +} + +/** + * @brief User application entry. + * @retval BSP_OK. + */ +int MotorMainProcess(void) +{ + SystemInit(); + /* System Initialization. */ + unsigned int tickNum100Ms = 200; /* 100ms tick */ + static unsigned int tickCnt100Ms = 0; + unsigned int tickNum500Ms = 1000; /* 500ms tick */ + static unsigned int tickCnt500Ms = 0; + + HAL_TIMER_Start(&g_timer1); + /* Disable PWM output before startup. */ + MotorPwmOutputDisable(g_aptCp); + InitSoftware(); + /* Start the PWM clock. */ + HAL_APT_StartModule(RUN_APT0 | RUN_APT1 | RUN_APT2); + + while (true) { + if (g_mc.msTickCnt - tickCnt100Ms >= tickNum100Ms) { + tickCnt100Ms = g_mc.msTickCnt; + /* User Code 100ms Event */ + AdjustSpeedFunction(); + } + if (g_mc.msTickCnt - tickCnt500Ms >= tickNum500Ms) { + if (SysIsError(&g_mc.statusReg) == true) { + break; + } + /* The LED blinks when no status is not error. */ + HAL_GPIO_TogglePin(&SYS_LED_HANDLE, SYS_LED_PIN); + tickCnt500Ms = g_mc.msTickCnt; + } + } + return 0; +} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/application/mcs_motor_process.h b/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/application/mcs_motor_process.h new file mode 100644 index 00000000..a2fdeb2c --- /dev/null +++ b/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/application/mcs_motor_process.h @@ -0,0 +1,47 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_motor_process.h + * @author MCU Algorithm Team + * @brief This file provides motor sample functions declaration for ECBMCU201MPC board. + */ +#ifndef McuMagicTag_MCS_MOTOR_PROCESS_H +#define McuMagicTag_MCS_MOTOR_PROCESS_H + +#include "main.h" + +/** + * @brief phase sequence. + */ +typedef enum { + PHASE_U = 0, + PHASE_V, + PHASE_W, + PHASE_MAX_NUM +} MCS_PhaseEnum; + +/** + * @brief key state. + */ +typedef enum { + KEY_DOWN = 0, + KEY_UP = 1, +} KEY_State; + +int MotorMainProcess(void); + +#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/application/mcs_user_config.h b/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/application/mcs_user_config.h new file mode 100644 index 00000000..6f88c4c4 --- /dev/null +++ b/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/application/mcs_user_config.h @@ -0,0 +1,94 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_config.h + * @author MCU Algorithm Team + * @brief This file provides config macros for ECBMCU201MPC app. + */ +#ifndef McuMagicTag_MCS_CONFIG_H +#define McuMagicTag_MCS_CONFIG_H + +#if defined(CHIP_3061MNPICA) || defined(CHIP_3061MNNICA) || defined(CHIP_3061MNPIC8) || defined(CHIP_3061MNNIC8) +#define ADC_U_SOC_NUM ADC_SOC_NUM2 +#define ADC_V_SOC_NUM ADC_SOC_NUM5 +#define ADC_W_SOC_NUM ADC_SOC_NUM6 +#define ADC_SPD_ADJ_SOC_NUM ADC_SOC_NUM9 +#define ADC_HANDLE g_adc0 +#endif + +#if defined (CHIP_3066MNPIRH) || defined (CHIP_3065PNPIRH) || defined (CHIP_3065PNPIRE) || defined (CHIP_3065PNPIRA) +#define ADC_U_SOC_NUM ADC_SOC_NUM3 +#define ADC_V_SOC_NUM ADC_SOC_NUM5 +#define ADC_W_SOC_NUM ADC_SOC_NUM7 +#define ADC_SPD_ADJ_SOC_NUM ADC_SOC_NUM9 + +#define ADC_HANDLE g_adc2 +#endif + +#define APT_PWM_FREQ 12000 /* PWM frequency 12K Hz. */ +#define CTRL_CURR_PERIOD (1.0f / APT_PWM_FREQ) /* carrier ISR period (S) */ +#define CTRL_CURE_PERIOD_US 83 + +#define SYSTICK_PERIOD_US 500u /* systick period. unit : us */ + +#define INV_CAP_CHARGE_MS 3u /* Bootstrap capacitor charge time. unit : ms */ + +#define CTRL_SYSTICK_PERIOD 0.0005f /* systick control period, 500us */ + +#define USER_SPD_SLOPE 60.0f /* slope of velocity change */ + +#define IN_VOLTAGE_BUS 24 /* Motor input voltage */ +#define POLES 14 /* logarithm of motor pole */ +#define MATH_PI 3.14 /* Pi */ +#define MOTOR_K 0.66 /* Torque Coefficient */ + +#define RAMP_STP 60 /* Slope of forced draging acceleration */ +#define RAMP_DUTY_PWM 0.5 /* 1.0% */ + +#define FORCE_DRAG_MINDUTY 10 /* APT minimum duty cycle during forced drag */ +#define FORCE_DRAG_MAXDUTY 15 /* APT maximum duty cycle during forced drag */ + +#define DRAG_START_INTERVAL 1000 /* Force drag change phase time */ +#define DRAG_STOP_INTERVAL 400 /* Force drag change phase time */ + +/* Parameters of the motor in the RUN */ +#define FILTER_COUNT 3 /* Filter Times */ +#define PHASE_OFFSET 9000 /* Phase delay due to filtering */ + +#define APT_DUTYLIMIT_MAX 99.9 /* Maximum duty cycle of the output APT */ +#define APT_DUTYLIMIT_MIN 8.0 /* Minimum duty cycle of the output APT */ + +#define BRIDGE_CTR_APT_U APT0 /* APT address that controls the U phase */ +#define BRIDGE_CTR_APT_V APT1 /* APT address that controls the V phase */ +#define BRIDGE_CTR_APT_W APT2 /* APT address that controls the W phase */ + +#define SDP_MAX_VALUE 77.00 /* Maximum change phase frequency */ +#define SDP_MIN_VALUE 12.0 /* Minimum change phase frequency */ +#define SDP_TARGET_VALUE 77.00 /* Target change phase frequency */ + +/* SPD PID Param */ +#define SPD_PID_KP 0.05 /* P parameter of PID control */ +#define SPD_PID_KI 6 /* I parameter of PID control */ +#define SPD_PID_TS 0.0005 /* TS parameter of PID control cycle */ + +#define OP_TO_CL_INTERGRAL 8.0 /* Open-loop switching closed-loop integral term to prevent sudden current change. */ + +#define VOL_DIVIDER_COEFFICIENT 0.09091f /* Division coefficient of the zero-crossing detection sampling circuit */ + +#define SPD_FILTER_FC 30 + +#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/common/mcs_carrier.c b/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/common/mcs_carrier.c new file mode 100644 index 00000000..7779fbef --- /dev/null +++ b/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/common/mcs_carrier.c @@ -0,0 +1,224 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_carrier.c + * @author MCU Algorithm Team + * @brief This file provides carrier interrupt application for motor control. + */ + +#include "mcs_carrier.h" +#include "mcs_user_config.h" +#include "mcs_assert.h" +#include "debug.h" + +#define S_TO_US 1000000 +#define S_TO_SYSTICK 100000000 + +/** + * @brief Sets the duty cycle of the H-bridge APT. + * @param aptHandle APT module handle. + * @param duty PWM duty. Range: 0.1 ~ 99.9. + * @retval None. + */ +void MCS_SetCtrAptDuty(MtrCtrlHandle *mtrCtrl, unsigned int duty) +{ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + MCS_ASSERT_PARAM(duty > 0); + /* Set pwm duty of uvw pahse */ + HAL_APT_SetPWMDutyByNumber(mtrCtrl->stepCtrl.controlApt.u, duty); + HAL_APT_SetPWMDutyByNumber(mtrCtrl->stepCtrl.controlApt.v, duty); + HAL_APT_SetPWMDutyByNumber(mtrCtrl->stepCtrl.controlApt.w, duty); +} + +/** + * @brief Strong drag start. + * @param mtrCtrl The motor control handle. + * @retval None. + */ +static void ForceDragAcc(MtrCtrlHandle *mtrCtrl) +{ + /* Verifying Parameters. */ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + unsigned int voltageDValue; + unsigned int dragChangeFreq; + unsigned int timerperiod; + unsigned int freqLast; + unsigned int aptCountsOneUs; + + mtrCtrl->sysVar.accTimeCnt++; + if (mtrCtrl->sysVar.accTimeCnt < mtrCtrl->sysVar.dragChangePhaseTime) { + return; + } + + mtrCtrl->sysVar.accTimeCnt = 0; + /* Step 1: Calculate the voltage difference. */ + voltageDValue = IN_VOLTAGE_BUS * RAMP_DUTY_PWM; + /* Step 2: Calculate the commutation frequency. */ + freqLast = S_TO_US / (mtrCtrl->sysVar.dragChangePhaseTime * CTRL_CURE_PERIOD_US); + dragChangeFreq = voltageDValue * STEP_MAX_NUM * POLES / (MOTOR_K * 2 * MATH_PI) + freqLast; /* 2*PI = 360° */ + /* Step 3: Convert the commutation frequency to the commutation count value. */ + timerperiod = mtrCtrl->stepCtrl.controlApt.u->waveform.timerPeriod * CTRL_CURE_PERIOD_US; + mtrCtrl->sysVar.dragChangePhaseTime = S_TO_US / (timerperiod * dragChangeFreq); + + /* Determine whether the change phase speed has reached the speed of stopping forced drag. */ + if (mtrCtrl->sysVar.dragChangePhaseTime < DRAG_STOP_INTERVAL) { + mtrCtrl->sysVar.dragChangePhaseTime = DRAG_STOP_INTERVAL; + mtrCtrl->sysVar.accTimeCnt = 0; + mtrCtrl->sysVar.bemfFilterCnt = 0; + mtrCtrl->stateMachine = FSM_RUN; + mtrCtrl->sysVar.lastZeroPoint = DCL_SYSTICK_GetTick(); + mtrCtrl->spdPi.integral = OP_TO_CL_INTERGRAL; + } + + /* Shorten the time interval of forced drag change phase. */ + mtrCtrl->pwmDuty += RAMP_DUTY_PWM; + aptCountsOneUs = HAL_CRG_GetIpFreq(APT0_BASE) / S_TO_US; + mtrCtrl->sysVar.waitTime = (mtrCtrl->sysVar.dragChangePhaseTime * CTRL_CURE_PERIOD_US * aptCountsOneUs) >> 1; + /* Change phase time is 2 the waiting time. */ + mtrCtrl->spdRefHz = (float)(HAL_CRG_GetIpFreq(SYSTICK_BASE) / (mtrCtrl->sysVar.waitTime * 2)) / STEP_MAX_NUM; + mtrCtrl->spdEstHz = mtrCtrl->spdRefHz; + mtrCtrl->spdRmg.yLast = mtrCtrl->spdRefHz; + mtrCtrl->stepCtrl.phaseStep = (mtrCtrl->stepCtrl.phaseStep + 1) % STEP_MAX_NUM; + SixStepPwm(&mtrCtrl->stepCtrl); + /* Limiting the PWM Duty Cycle Range. */ + if (mtrCtrl->pwmDuty > FORCE_DRAG_MAXDUTY) { + mtrCtrl->pwmDuty = FORCE_DRAG_MAXDUTY; + } + if (mtrCtrl->pwmDuty < FORCE_DRAG_MINDUTY) { + mtrCtrl->pwmDuty = FORCE_DRAG_MINDUTY; + } + MCS_SetCtrAptDuty(mtrCtrl, mtrCtrl->pwmDuty); +} + +/** + * @brief Zero-crossing filter. + * @param mtrCtrl The motor control handle. + * @retval None. + */ +static void BemfZeroSampleFilter(MtrCtrlHandle *mtrCtrl) +{ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + mtrCtrl->sysVar.bemfFilterCnt++; + /* Filter the number of samples. */ + if (mtrCtrl->sysVar.bemfFilterCnt >= FILTER_COUNT) { + mtrCtrl->sysVar.bemfFilterCnt = 0; + unsigned int currentTick = DCL_SYSTICK_GetTick(); + if (mtrCtrl->sysVar.firstEventFilterFlag == 0) { + mtrCtrl->sysVar.lastZeroPoint = currentTick; + mtrCtrl->sysVar.changePhaseFlag = 1; + mtrCtrl->sysVar.firstEventFilterFlag = 1; + return; + } + /* Calculate the time interval between the last zero crossing. */ + mtrCtrl->sysVar.stepTime[mtrCtrl->stepCtrl.phaseStep] = currentTick >= mtrCtrl->sysVar.lastZeroPoint + ? currentTick - mtrCtrl->sysVar.lastZeroPoint + : SYSTICK_MAX_VALUE - mtrCtrl->sysVar.lastZeroPoint + currentTick + 1; + mtrCtrl->sysVar.stepTimeNum++; + /* Wait time is half the interval. */ + mtrCtrl->sysVar.waitTime = mtrCtrl->sysVar.stepTime[mtrCtrl->stepCtrl.phaseStep] >> 1; + mtrCtrl->sysVar.waitTime -= PHASE_OFFSET; + mtrCtrl->sysVar.lastZeroPoint = currentTick; + /* Update flag. */ + mtrCtrl->sysVar.changePhaseFlag = 1; + } +} + +/** + * @brief Back EMF zero crossing check. + * @param mtrCtrl The motor control handle. + * @retval None. + */ +static void BemfZeroCheck(MtrCtrlHandle *mtrCtrl) +{ + /* The step corresponds to different conditional judgments. */ + unsigned int bemfCheckTable[STEP_MAX_NUM] = {mtrCtrl->bemf.w < mtrCtrl->zeroPoint ? 1 : 0, + mtrCtrl->bemf.v > mtrCtrl->zeroPoint ? 1 : 0, + mtrCtrl->bemf.u < mtrCtrl->zeroPoint ? 1 : 0, + mtrCtrl->bemf.w > mtrCtrl->zeroPoint ? 1 : 0, + mtrCtrl->bemf.v < mtrCtrl->zeroPoint ? 1 : 0, + mtrCtrl->bemf.u > mtrCtrl->zeroPoint ? 1 : 0}; + + if (bemfCheckTable[mtrCtrl->stepCtrl.phaseStep] == 1) { + BemfZeroSampleFilter(mtrCtrl); + } +} + +/** + * @brief Change phase and speed estimation. + * @param mtrCtrl The motor control handle. + * @retval None. + */ +static void ChangePhase(MtrCtrlHandle *mtrCtrl) +{ + unsigned int currentTick = DCL_SYSTICK_GetTick(); /* Get the current tick value. */ + unsigned int intervalTick = (currentTick >= mtrCtrl->sysVar.lastZeroPoint) + ? currentTick - mtrCtrl->sysVar.lastZeroPoint + : SYSTICK_MAX_VALUE - mtrCtrl->sysVar.lastZeroPoint + currentTick + 1; + /* Start average filtering when the number of recorded data reaches six. */ + if (mtrCtrl->sysVar.stepTimeNum >= STEP_MAX_NUM) { + mtrCtrl->sysVar.stepTimeNum = 0; + mtrCtrl->sysVar.stepTimeFilterEnable = 1; + } + /* If the accumulated time is greater than the waiting commutation time, commutation is performed. */ + if (intervalTick > mtrCtrl->sysVar.waitTime) { + if (mtrCtrl->sysVar.stepTimeFilterEnable) { /* 6-step commutation time averaging if filtering is enabled. */ + unsigned int totalTime = mtrCtrl->sysVar.stepTime[STEP1] + mtrCtrl->sysVar.stepTime[STEP2] + + mtrCtrl->sysVar.stepTime[STEP3] + mtrCtrl->sysVar.stepTime[STEP4] + + mtrCtrl->sysVar.stepTime[STEP5] + mtrCtrl->sysVar.stepTime[STEP6]; + mtrCtrl->spdEstHz = (float)HAL_CRG_GetIpFreq(SYSTICK_BASE) / totalTime; + } else { + mtrCtrl->spdEstHz = (float)HAL_CRG_GetIpFreq(SYSTICK_BASE) / (mtrCtrl->sysVar.waitTime << 1) / STEP_MAX_NUM; + } + /* Change phase. */ + mtrCtrl->stepCtrl.phaseStep = (mtrCtrl->stepCtrl.phaseStep + 1) % STEP_MAX_NUM; + SixStepPwm(&mtrCtrl->stepCtrl); + mtrCtrl->sysVar.changePhaseFlag = 0; + } +} + +/** + * @brief Zero-crossing detection and change phase + * @param mtrCtrl The motor control handle. + * @retval None. + */ +void MCS_CarrierProcess(MtrCtrlHandle *mtrCtrl) +{ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + /* Offset value of the calibration value of the three-phase current */ + + switch (mtrCtrl->stateMachine) { + case FSM_STARTUP: + ForceDragAcc(mtrCtrl); /* Forced drag. */ + break; + + case FSM_RUN: + case FSM_WAIT_STOP: + mtrCtrl->readBemfUVW(&mtrCtrl->bemf); + MCS_SetCtrAptDuty(mtrCtrl, mtrCtrl->pwmDuty); + if (mtrCtrl->sysVar.changePhaseFlag) { + /* Change phase process. */ + ChangePhase(mtrCtrl); + } else { + /* Zero-crossing detection procedure. */ + BemfZeroCheck(mtrCtrl); + } + break; + + default: + break; + } +} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/common/mcs_carrier.h b/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/common/mcs_carrier.h new file mode 100644 index 00000000..05fc2666 --- /dev/null +++ b/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/common/mcs_carrier.h @@ -0,0 +1,91 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_carrier.h + * @author MCU Algorithm Team + * @brief This file provides functions declaration for carrier interrupt processing function. + */ +#ifndef McuMagicTag_MCS_CARRIER_H +#define McuMagicTag_MCS_CARRIER_H + +#include "mcs_status.h" +#include "mcs_six_step.h" +#include "mcs_ramp_mgmt.h" +#include "mcs_spd_ctrl.h" +#include "mcs_fsm.h" + +/** + * @brief Three-phase static coordinate frame variable. + */ +typedef struct { + unsigned int u; /**< Component u of the three-phase static coordinate frame variable. */ + unsigned int v; /**< Component v of the three-phase static coordinate frame variable. */ + unsigned int w; /**< Component w of the three-phase static coordinate frame variable. */ +} UVWBemf; + +typedef void (*ReadBemf)(UVWBemf *bemfUVW); + +/** + * @brief The definition of the systematic global variables + */ +typedef struct { + unsigned char changePhaseFlag; /**< Flag indicating that delay change phase is required */ + unsigned char firstEventFilterFlag; /**< First zero-crossing event filtering flag. */ + unsigned int dragChangePhaseTime; /**< Interval for forced drag acceleration */ + unsigned int accTimeCnt; /**< Acceleration time count */ + + unsigned int bemfFilterCnt; /**< Number of sample filtering times for zero-crossing detection. */ + + unsigned int lastZeroPoint; /**< Time of the last zero point */ + unsigned int waitTime; /**< Time to wait for change phase */ + unsigned int stepTime[STEP_MAX_NUM]; /**< Record the time spent on each step */ + unsigned int stepTimeNum; /**< Number of data records in the steptime array. */ + unsigned int stepTimeFilterEnable; +} SysVariable; + +typedef struct { + volatile float spdCmdHz; /**< Set target change phase frequency */ + float spdRefHz; /**< Command values after speed ramp management */ + float spdEstHz; /**< Actual change phase frequency of feedback */ + float pwmDuty; /**< APT duty cycle */ + unsigned int zeroPoint; /**< Adc value of zero point */ + + unsigned short aptMaxcntCmp; /**< Apt Maximum Comparison Count */ + + unsigned short sysTickCnt; /**< System Timer Tick Count */ + unsigned short capChargeTickNum; /**< Bootstrap Capacitor Charge Tick Count */ + unsigned short msTickNum; /**< Number of ticks corresponding to 1 ms */ + volatile unsigned int msTickCnt; /**< Millisecond-level counter, which can be used in 1-ms and 5-ms tasks. */ + + RMG_Handle spdRmg; /**< Ramp management struct for the speed controller input reference */ + PID_Handle spdPi; /**< PI controller struct in the speed controller. */ + + UVWBemf bemf; /**< Three-phase back electromotive force */ + SixStepHandle stepCtrl; /**< Control structure of six-step square wave */ + + SysVariable sysVar; /**< System Variables */ + SysStatusReg statusReg; /**< System Status */ + FsmState stateMachine; /**< BLDC Motor Control State Machine */ + + ReadBemf readBemfUVW; /**< Function interface for obtaining the three-phase electromotive force */ +} MtrCtrlHandle; + +void MCS_CarrierProcess(MtrCtrlHandle *mtrCtrl); + +void MCS_SetCtrAptDuty(MtrCtrlHandle *mtrCtrl, unsigned int duty); + +#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/common/mcs_fsm.h b/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/common/mcs_fsm.h new file mode 100644 index 00000000..b3c79ea2 --- /dev/null +++ b/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/common/mcs_fsm.h @@ -0,0 +1,55 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_fsm.h + * @author MCU Algorithm Team + * @brief This file provides the definition of finite statemachine (FSM). + */ + +#ifndef McuMagicTag_MCS_FSM_H +#define McuMagicTag_MCS_FSM_H + +/** + * @brief motor control FSM state define. + * @details motor control FSM state type: + * + FSM_IDLE -- IDLE state, system startup default. + * + FSM_OFFSET_CALIB -- Self calibrate, for ADC init. + * + FSM_CAP_CHARGE -- IPM cap charge. + * + FSM_CLEAR -- Clear before every run. + * + FSM_LOCATE -- Rotor position pre-locate. + * + FSM_STARTUP -- Start up. + * + FSM_SWITCH -- Transition state, control from open to closed loop. + * + FSM_RUN -- Normal running state. + * + FSM_WAIT_STOP -- Wait stopping. + * + FSM_STOP -- Normal stop. + * + FSM_FAULT -- Fault state, waiting for user process. + */ +typedef enum { + FSM_IDLE = 0, + FSM_OFFSET_CALIB, + FSM_CAP_CHARGE, + FSM_CLEAR, + FSM_LOCATE, + FSM_STARTUP, + FSM_SWITCH, + FSM_RUN, + FSM_WAIT_STOP, + FSM_STOP, + FSM_FAULT +} FsmState; + +#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/func/mcs_six_step.c b/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/func/mcs_six_step.c new file mode 100644 index 00000000..c86e609c --- /dev/null +++ b/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/func/mcs_six_step.c @@ -0,0 +1,126 @@ +/** + * @ Copyright (c) HiSilicon (Shanghai) Technologies Co., Ltd. 2022-2023. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_six_step.c + * @author MCU Algorithm Team + * @brief This file provides functions to control the H bridge. + */ + +#include "mcs_assert.h" +#include "mcs_six_step.h" + +/* Mode for disabling or enabling software-based forced output */ +#define APT_PWM_MODE 0 +#define APT_FRC_MODE 1 + +#define PWM_SIGNAL 1 + +#define FRC_LOW APT_OUT_CTRL_ACTION_LOW +#define FRC_HIGH APT_OUT_CTRL_ACTION_HIGH + +/** + * @brief Force PWM output by APT + * @param APTx The APT to use + * @param PwmAout Force to be high or low + * @param PwmAenble PWMA signal software force mode + * @param PwmBout Force to be high or low + * @param PwmBenble PWMB signal software force mode + */ +static void APT_ForcePwmByApt(APT_RegStruct *aptx, APT_Act actMode) +{ + MCS_ASSERT_PARAM(aptx != NULL); + switch (actMode) { + case APT_CHA_PWM_CHB_LOW: + /* Channel A: 0 means not force output enable, channel A output PWM. */ + DCL_APT_DisableSwContPWMAction(aptx, APT_PWM_CHANNEL_A); + DCL_APT_SetSwContPWMAction(aptx, APT_PWM_CHANNEL_A, APT_PWM_CONTINUOUS_ACTION_LOW); + /* Channel B: 1 means force output enable. */ + DCL_APT_EnableSwContPWMAction(aptx, APT_PWM_CHANNEL_B); + /* Channel B: 2 means channel B force output LOW due to the A_H_B_L invert. */ + DCL_APT_SetSwContPWMAction(aptx, APT_PWM_CHANNEL_B, APT_PWM_CONTINUOUS_ACTION_HIGH); + break; + case APT_CHA_LOW_CHB_HIGH: + /* Channel A: 1 means force output enable. */ + /* Channel A: 1 means channel A force output LOW. */ + DCL_APT_EnableSwContPWMAction(aptx, APT_PWM_CHANNEL_A); + DCL_APT_SetSwContPWMAction(aptx, APT_PWM_CHANNEL_A, APT_PWM_CONTINUOUS_ACTION_LOW); + /* Channel B: 1 means force output enable. */ + /* Channel B: 2 means channel B force output HIGH. */ + DCL_APT_EnableSwContPWMAction(aptx, APT_PWM_CHANNEL_B); + DCL_APT_SetSwContPWMAction(aptx, APT_PWM_CHANNEL_B, APT_PWM_CONTINUOUS_ACTION_HIGH); + break; + case APT_CHA_LOW_CHB_LOW: + /* Channel A: 1 means force output enable. */ + /* Channel A: 1 means channel A force output LOW. */ + DCL_APT_EnableSwContPWMAction(aptx, APT_PWM_CHANNEL_A); + DCL_APT_SetSwContPWMAction(aptx, APT_PWM_CHANNEL_A, APT_PWM_CONTINUOUS_ACTION_LOW); + /* Channel B: 1 means force output enable. */ + /* Channel B: 1 means channel A force output HIGH due to the A_H_B_L invert. */ + DCL_APT_EnableSwContPWMAction(aptx, APT_PWM_CHANNEL_B); + DCL_APT_SetSwContPWMAction(aptx, APT_PWM_CHANNEL_B, APT_PWM_CONTINUOUS_ACTION_LOW); + break; + default: + break; + } +} + +/** + * @brief Output six-step square wave. + * @param handle The SixStepHandle. + * @retval None. + */ +void SixStepPwm(const SixStepHandle *handle) +{ + MCS_ASSERT_PARAM(handle != NULL); + APT_RegStruct *aptU = handle->controlApt.u->baseAddress; /* Get apt u\v\w baseaddress. */ + APT_RegStruct *aptV = handle->controlApt.v->baseAddress; + APT_RegStruct *aptW = handle->controlApt.w->baseAddress; + switch (handle->phaseStep) { + case STEP1: /* U+, V- */ + APT_ForcePwmByApt(aptU, APT_CHA_PWM_CHB_LOW); + APT_ForcePwmByApt(aptV, APT_CHA_LOW_CHB_LOW); + APT_ForcePwmByApt(aptW, APT_CHA_LOW_CHB_HIGH); + break; + case STEP2: /* U+, W- */ + APT_ForcePwmByApt(aptU, APT_CHA_PWM_CHB_LOW); + APT_ForcePwmByApt(aptV, APT_CHA_LOW_CHB_HIGH); + APT_ForcePwmByApt(aptW, APT_CHA_LOW_CHB_LOW); + break; + case STEP3: /* V+, W- */ + APT_ForcePwmByApt(aptU, APT_CHA_LOW_CHB_HIGH); + APT_ForcePwmByApt(aptV, APT_CHA_PWM_CHB_LOW); + APT_ForcePwmByApt(aptW, APT_CHA_LOW_CHB_LOW); + break; + case STEP4: /* V+, U- */ + APT_ForcePwmByApt(aptU, APT_CHA_LOW_CHB_LOW); + APT_ForcePwmByApt(aptV, APT_CHA_PWM_CHB_LOW); + APT_ForcePwmByApt(aptW, APT_CHA_LOW_CHB_HIGH); + break; + case STEP5: /* W+, U- */ + APT_ForcePwmByApt(aptU, APT_CHA_LOW_CHB_LOW); + APT_ForcePwmByApt(aptV, APT_CHA_LOW_CHB_HIGH); + APT_ForcePwmByApt(aptW, APT_CHA_PWM_CHB_LOW); + break; + case STEP6: /* W+, V- */ + APT_ForcePwmByApt(aptU, APT_CHA_LOW_CHB_HIGH); + APT_ForcePwmByApt(aptV, APT_CHA_LOW_CHB_LOW); + APT_ForcePwmByApt(aptW, APT_CHA_PWM_CHB_LOW); + break; + default: + break; + } +} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/func/mcs_six_step.h b/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/func/mcs_six_step.h new file mode 100644 index 00000000..08198c4c --- /dev/null +++ b/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/func/mcs_six_step.h @@ -0,0 +1,79 @@ +/** + * @ Copyright (c) HiSilicon (Shanghai) Technologies Co., Ltd. 2022-2023. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_six_step.c + * @author MCU Algorithm Team + * @brief The header file contains the following declaration: + * + StepEnum enum definition. + * + PhaseEnum enum definition. + * + UVW_AptReg structure definition. + * + SixStepHandle handle structure definition. + * + AptOutCombination structure definition. + * + Six Step Pwm Function And Six Step Pwm Overlap Function. + */ + +#ifndef MCS_SIX_STEP_H +#define MCS_SIX_STEP_H + +#include "apt.h" + +typedef enum { + STEP1 = 0, + STEP2, + STEP3, + STEP4, + STEP5, + STEP6, + STEP_MAX_NUM +} StepEnum; + +typedef enum { + U = 0, + V, + W, + PHASE_MAX_NUMS +} PhaseEnum; + +typedef enum { + APT_CHA_PWM_CHB_LOW, + APT_CHA_LOW_CHB_HIGH, + APT_CHA_LOW_CHB_LOW +} APT_Act; + +/** + * @brief Three-phase static coordinate frame variable. + */ +typedef struct { + APT_Handle *u; /**< Apt handle for controlling phase U. */ + APT_Handle *v; /**< Apt handle for controlling phase V. */ + APT_Handle *w; /**< Apt handle for controlling phase W. */ +} UVW_AptReg; + +typedef struct { + unsigned char phaseStep; /* Current step. */ + UVW_AptReg controlApt; /* Apt handles for controlling H bridge. */ +} SixStepHandle; + +typedef struct { + unsigned int upperout; /* High tube level */ + unsigned int upperstate; /* High tube state */ + unsigned int lowerout; /* Low tube level */ + unsigned int lowerstate; /* Low tube state */ +} AptOutCombination; + +void SixStepPwm(const SixStepHandle *handle); +#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/func/mcs_status.h b/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/func/mcs_status.h new file mode 100644 index 00000000..d92ed312 --- /dev/null +++ b/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/func/mcs_status.h @@ -0,0 +1,188 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_status.h + * @author MCU Algorithm Team + * @brief This file provides functions declaration of system status. + */ + +/* Define to prevent recursive inclusion ------------------------------------------------------- */ +#ifndef McuMagicTag_MCS_STATUS_H +#define McuMagicTag_MCS_STATUS_H + +/* Includes ------------------------------------------------------------------------------------ */ +#include "typedefs.h" +#include "mcs_assert.h" + +/* Typedef definitions ------------------------------------------------------------------------- */ +/** + * @brief System status define + */ +typedef union { + unsigned short all; + struct { + unsigned short cmdStart : 1; /**< Indicates that a start system command has been received. */ + unsigned short cmdStop : 1; /**< Indicates that a stop system command has been received. */ + unsigned short isRunning : 1; /**< Indicates that the system is running (enable signal) */ + unsigned short sysError : 1; /**< Indicates that the system reports an error. */ + unsigned short poweron : 1; /**< Indicates that the power-on initialization phase is complete. */ + unsigned short capcharge : 1; /**< Indicates that the bootstrap capacitor charging phase is complete. */ + unsigned short adczero : 1; /**< The current sampling point is reset to zero after power-on. */ + } BIT; +} SysStatusReg; + +/** + * @brief Get status of BIT cmdStart. + * @param sysStatus System status register handle. + * @retval Status of BIT cmdStart. + */ +static inline bool SysGetCmdStart(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + bool ret; + ret = (sysStatus->BIT.cmdStart == 1) ? true : false; + return ret; +} + +/** + * @brief Set BIT cmdStart. + * @param sysStatus System status register handle. + * @retval None. + */ +static inline void SysCmdStartSet(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + sysStatus->BIT.cmdStart = 1; +} + +/** + * @brief Clear BIT cmdStart. + * @param handle System status register handle. + * @retval None. + */ +static inline void SysCmdStartClr(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + sysStatus->BIT.cmdStart = 0; +} + +/** + * @brief Get status of BIT cmdStop. + * @param sysStatus System status register handle. + * @retval Status of BIT cmdStart. + */ +static inline bool SysGetCmdStop(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + bool ret; + ret = (sysStatus->BIT.cmdStop == 1) ? true : false; + return ret; +} + +/** + * @brief Set BIT cmdStop. + * @param sysStatus System status register handle. + * @retval None. + */ +static inline void SysCmdStopSet(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + sysStatus->BIT.cmdStop = 1; +} + +/** + * @brief Clear BIT cmdStop. + * @param sysStatus System status register handle. + * @retval None. + */ +static inline void SysCmdStopClr(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + sysStatus->BIT.cmdStop = 0; +} + +/** + * @brief Get status of BIT isRunning. + * @param sysStatus System status register handle. + * @retval Status of BIT isRunning. + */ +static inline bool SysIsRunning(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + bool ret; + ret = (sysStatus->BIT.isRunning == 1) ? true : false; + return ret; +} + +/** + * @brief Set BIT isRuning. + * @param sysStatus System status register handle. + * @retval None. + */ +static inline void SysRunningSet(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + sysStatus->BIT.isRunning = 1; +} + +/** + * @brief Clear BIT isRuning. + * @param sysStatus System status register handle. + * @retval None. + */ +static inline void SysRunningClr(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + sysStatus->BIT.isRunning = 0; +} + +/** + * @brief Get status of BIT sysError. + * @param sysStatus System status register handle. + * @retval Status of BIT sysError. + */ +static inline bool SysIsError(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + bool ret; + ret = (sysStatus->BIT.sysError == 1) ? true : false; + return ret; +} + +/** + * @brief Set BIT sysError. + * @param sysStatus System status register handle. + * @retval None. + */ +static inline void SysErrorSet(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + sysStatus->BIT.sysError = 1; +} + +/** + * @brief Clear BIT sysError. + * @param sysStatus System status register handle. + * @retval None. + */ +static inline void SysErrorClr(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + sysStatus->BIT.sysError = 0; +} + +#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/readme.md b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/readme.md new file mode 100644 index 00000000..8350bef0 --- /dev/null +++ b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/readme.md @@ -0,0 +1,22 @@ +# pmsm_pos_qdm_foc + +**【功能描述】** + +- 基于ECMCU105H/ECBMCU201MPC单板的单电机双电阻采样的位置-速度-电流闭环Foc应用 + +**【环境要求】** + +- 所有单板电源改制为演示用的24V低压,电机选用42JSF630AS-1000 +- 电机ABZ编码器线序分别对应功率板上ABZ通道接口 +- 电机(黄绿蓝)相线分别对应功率板上U/V/W通道接口 + +**【配置方法】** + +- 将user文件夹替换原工程user文件夹即可 + +【操作说明】 + +- 点击开发板按键启动电机 + +- 打开串口小助手,输入期望角度(弧度值),电机会转到相应角度,6.28为一圈 +- 开发板蓝色电位器,可以调节电机转动的快慢 \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/sample_gpio_key/generatecode/feature.h b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/generatecode/feature.h similarity index 100% rename from vendor/yibaina_3061M/demo/sample_gpio_key/generatecode/feature.h rename to vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/generatecode/feature.h diff --git a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/generatecode/main.h b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/generatecode/main.h new file mode 100644 index 00000000..0750df8a --- /dev/null +++ b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/generatecode/main.h @@ -0,0 +1,101 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file main.h + * @author MCU Driver Team + * @brief This file contains driver init functions. + */ + +/* Define to prevent recursive inclusion ------------------------------------- */ +#ifndef McuMagicTag_SYSTEM_INIT_H +#define McuMagicTag_SYSTEM_INIT_H + +#include "adc.h" +#include "adc_ex.h" +#include "acmp.h" +#include "acmp_ex.h" +#include "apt.h" +#include "uart.h" +#include "uart_ex.h" +#include "qdm.h" +#include "gpio.h" +#include "timer.h" +#include "timer_ex.h" +#include "pga.h" +#include "crg.h" +#include "dma.h" +#include "dma_ex.h" +#include "iocmg.h" +#include "i2c.h" + +#define IO_SPEED_FAST 0x00U +#define IO_SPEED_SLOW 0x01U + +#define IO_DRV_LEVEL4 0x00U +#define IO_DRV_LEVEL3 0x01U +#define IO_DRV_LEVEL2 0x02U +#define IO_DRV_LEVEL1 0x03U + +#define XTAL_DRV_LEVEL4 0x03U +#define XTAL_DRV_LEVEL3 0x02U +#define XTAL_DRV_LEVEL2 0x01U +#define XTAL_DRV_LEVEL1 0x00U + +#define GpioStartStop_PIN GPIO_PIN_4 +#define GpioStartStop_HANDLE g_gpio2 + +extern ACMP_Handle g_acmp0; +extern QDM_Handle g_qdm1; +extern PGA_Handle g_pga0; +extern PGA_Handle g_pga1; +extern TIMER_Handle g_timer0; +extern TIMER_Handle g_timer1; +extern UART_Handle g_uart0; +extern APT_Handle g_apt0; +extern APT_Handle g_apt1; +extern APT_Handle g_apt2; +extern ADC_Handle g_adc0; + +extern DMA_Handle g_dmac; + +extern GPIO_Handle g_gpio2; +extern GPIO_Handle g_gpio1; +extern I2C_Handle g_i2c0; +extern TIMER_Handle g_timer2; +BASE_StatusType CRG_Config(CRG_CoreClkSelect *coreClkSelect); +void SystemInit(void); + +void UART0WriteInterruptCallback(void *handle); +void UART0ReadInterruptCallback(void *handle); + +void UART0InterruptErrorCallback(void *handle); +void MotorStatemachineCallBack(void *handle); +void TIMER1_DMAOverFlow_InterruptProcess(void *handle); +void CheckPotentiometerValueCallback(void *handle); +void TIMER0_DMAOverFlow_InterruptProcess(void *handle); +void MotorCarrierProcessCallback(void *aptHandle); +void MotorSysErrCallback(void *aptHandle); + +void UART0_TXDMACallback(void *handle); + +void MotorStartStopKeyCallback(void *param); + +/* USER CODE BEGIN 0 */ +/* USER CODE 区域内代码不会被覆盖,区域外会被生成的默认代码覆盖(其余USER CODE 区域同理) */ +/* USER CODE END 0 */ + +#endif /* McuMagicTag_SYSTEM_INIT_H */ \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/generatecode/system_init.c b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/generatecode/system_init.c new file mode 100644 index 00000000..188e0a86 --- /dev/null +++ b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/generatecode/system_init.c @@ -0,0 +1,775 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file system_init.c + * @author MCU Driver Team + * @brief This file contains driver init functions. + */ + +#include "main.h" +#include "ioconfig.h" +#include "iocmg_ip.h" + +#define UART0_BAND_RATE 115200 + +BASE_StatusType CRG_Config(CRG_CoreClkSelect *coreClkSelect) +{ + CRG_Handle crg; + crg.baseAddress = CRG; + crg.pllRefClkSelect = CRG_PLL_REF_CLK_SELECT_HOSC; + crg.pllPreDiv = CRG_PLL_PREDIV_4; + crg.pllFbDiv = 48; /* PLL Multiplier 48 */ + crg.pllPostDiv = CRG_PLL_POSTDIV_2; + crg.coreClkSelect = CRG_CORE_CLK_SELECT_PLL; + crg.handleEx.pllPostDiv2 = CRG_PLL_POSTDIV2_3; + crg.handleEx.clk1MSelect = CRG_1M_CLK_SELECT_HOSC; + crg.handleEx.clk1MDiv = (25 - 1); /* The 1 MHz freq is equal to the input clock frequency / (clk_1m_div + 1). 25 is the div of the clk_1m in CLOCK. */ + + if (HAL_CRG_Init(&crg) != BASE_STATUS_OK) { + return BASE_STATUS_ERROR; + } + *coreClkSelect = crg.coreClkSelect; + return BASE_STATUS_OK; +} + +static void DMA_Channel0Init(void *handle) +{ + DMA_ChannelParam dma_param; + dma_param.direction = DMA_MEMORY_TO_PERIPH_BY_DMAC; + dma_param.srcAddrInc = DMA_ADDR_INCREASE; + dma_param.destAddrInc = DMA_ADDR_UNALTERED; + dma_param.srcPeriph = DMA_REQUEST_MEM; + dma_param.destPeriph = DMA_REQUEST_UART0_TX; + dma_param.srcWidth = DMA_TRANSWIDTH_BYTE; + dma_param.destWidth = DMA_TRANSWIDTH_BYTE; + dma_param.srcBurst = DMA_BURST_LENGTH_1; + dma_param.destBurst = DMA_BURST_LENGTH_1; + dma_param.pHandle = handle; + HAL_DMA_InitChannel(&g_dmac, &dma_param, DMA_CHANNEL_ZERO); +} + +static void DMA_Init(void) +{ + HAL_CRG_IpEnableSet(DMA_BASE, IP_CLK_ENABLE); + g_dmac.baseAddress = DMA; + IRQ_Register(IRQ_DMA_TC, HAL_DMA_IrqHandlerTc, &g_dmac); + IRQ_Register(IRQ_DMA_ERR, HAL_DMA_IrqHandlerError, &g_dmac); + IRQ_EnableN(IRQ_DMA_TC); + IRQ_EnableN(IRQ_DMA_ERR); + HAL_DMA_Init(&g_dmac); + DMA_Channel0Init((void *)(&g_uart0)); + HAL_DMA_SetChannelPriorityEx(&g_dmac, DMA_CHANNEL_ZERO, DMA_PRIORITY_HIGHEST); +} + +static void ACMP0_Init(void) +{ + HAL_CRG_IpEnableSet(ACMP0_BASE, IP_CLK_ENABLE); /* ACMP clock bit reset. */ + g_acmp0.baseAddress = ACMP0_BASE; + g_acmp0.inOutConfig.inputNNum = ACMP_INPUT_N_SELECT2; + g_acmp0.inOutConfig.inputPNum = ACMP_INPUT_P_SELECT2; + g_acmp0.inOutConfig.polarity = ACMP_OUT_NOT_INVERT; + g_acmp0.filterCtrl.filterMode = ACMP_FILTER_NONE; + g_acmp0.hysteresisVol = ACMP_HYS_VOL_30MV; + g_acmp0.interruptEn = BASE_CFG_UNSET; + HAL_ACMP_Init(&g_acmp0); +} + +static void ADC0_Init(void) +{ + HAL_CRG_IpEnableSet(ADC0_BASE, IP_CLK_ENABLE); + HAL_CRG_IpClkSelectSet(ADC0_BASE, CRG_ADC_CLK_ASYN_PLL_DIV); + HAL_CRG_IpClkDivSet(ADC0_BASE, CRG_ADC_DIV_1); + + g_adc0.baseAddress = ADC0; + g_adc0.socPriority = ADC_PRIMODE_ALL_ROUND; + + HAL_ADC_Init(&g_adc0); + + SOC_Param socParam = {0}; + socParam.adcInput = ADC_CH_ADCINA0; /* PGA0_OUT(ADC AIN0) */ + socParam.sampleTotalTime = ADC_SOCSAMPLE_22CLK; /* adc sample total time 22 adc_clk */ + socParam.trigSource = ADC_TRIGSOC_APT0_SOCA; + socParam.continueMode = BASE_CFG_DISABLE; + socParam.finishMode = ADC_SOCFINISH_NONE; + HAL_ADC_ConfigureSoc(&g_adc0, ADC_SOC_NUM1, &socParam); + + socParam.adcInput = ADC_CH_ADCINA1; /* PGA1_OUT(ADC AIN1) */ + socParam.sampleTotalTime = ADC_SOCSAMPLE_22CLK; /* adc sample total time 22 adc_clk */ + socParam.trigSource = ADC_TRIGSOC_APT0_SOCA; + socParam.continueMode = BASE_CFG_DISABLE; + socParam.finishMode = ADC_SOCFINISH_NONE; + HAL_ADC_ConfigureSoc(&g_adc0, ADC_SOC_NUM6, &socParam); + + socParam.adcInput = ADC_CH_ADCINA9; /* PIN7(ADC AIN9) */ + socParam.sampleTotalTime = ADC_SOCSAMPLE_22CLK; /* adc sample total time 22 adc_clk */ + socParam.trigSource = ADC_TRIGSOC_SOFT; + socParam.continueMode = BASE_CFG_DISABLE; + socParam.finishMode = ADC_SOCFINISH_NONE; + HAL_ADC_ConfigureSoc(&g_adc0, ADC_SOC_NUM0, &socParam); + + socParam.adcInput = ADC_CH_ADCINA5; /* PIN2(ADC AIN5) */ + socParam.sampleTotalTime = ADC_SOCSAMPLE_22CLK; /* adc sample total time 22 adc_clk */ + socParam.trigSource = ADC_TRIGSOC_SOFT; + socParam.continueMode = BASE_CFG_DISABLE; + socParam.finishMode = ADC_SOCFINISH_NONE; + HAL_ADC_ConfigureSoc(&g_adc0, ADC_SOC_NUM3, &socParam); + + socParam.adcInput = ADC_CH_ADCINA15; /* PIN14(ADC AIN15) */ + socParam.sampleTotalTime = ADC_SOCSAMPLE_22CLK; /* adc sample total time 22 adc_clk */ + socParam.trigSource = ADC_TRIGSOC_SOFT; + socParam.continueMode = BASE_CFG_DISABLE; + socParam.finishMode = ADC_SOCFINISH_NONE; + HAL_ADC_ConfigureSoc(&g_adc0, ADC_SOC_NUM4, &socParam); +} + +__weak void MotorSysErrCallback(void *aptHandle) +{ + BASE_FUNC_UNUSED(aptHandle); + /* USER CODE BEGIN APT0_EVENT_INTERRUPT */ + /* USER CODE END APT0_EVENT_INTERRUPT */ +} + +__weak void MotorCarrierProcessCallback(void *aptHandle) +{ + BASE_FUNC_UNUSED(aptHandle); + /* USER CODE BEGIN APT0_TIMER_INTERRUPT */ + /* USER CODE END APT0_TIMER_INTERRUPT */ +} + +static void APT0_ProtectInit(void) +{ + APT_OutCtrlProtectEx protectApt = {0}; + protectApt.ocEventEnEx = BASE_CFG_ENABLE; + protectApt.ocEventModeEx = APT_OUT_CTRL_ONE_SHOT; + protectApt.ocActionEx = APT_OUT_CTRL_ACTION_LOW; + protectApt.ocActionBEx = APT_OUT_CTRL_ACTION_LOW; + protectApt.ocEvtInterruptEnEx = BASE_CFG_ENABLE; + protectApt.ocSysEvent = APT_SYS_EVT_DEBUG | APT_SYS_EVT_CLK | APT_SYS_EVT_MEM; + protectApt.originalEvtEx = APT_EM_ORIGINAL_SRC_ACMP0; + protectApt.evtPolarityMaskEx = APT_EM_ACMP0_INVERT_BIT; + protectApt.filterCycleNumEx = 0; + HAL_APT_ProtectInitEx(&g_apt0, &protectApt); +} + +static void APT0_Init(void) +{ + HAL_CRG_IpEnableSet(APT0_BASE, IP_CLK_ENABLE); + + g_apt0.baseAddress = APT0; + + /* Clock Settings */ + g_apt0.waveform.dividerFactor = 1 - 1; + /* Timer Settings */ + g_apt0.waveform.timerPeriod = 7500; /* 7500 is count period of APT time-base timer */ + g_apt0.waveform.cntMode = APT_COUNT_MODE_UP_DOWN; + + /* Wave Form */ + g_apt0.waveform.basicType = APT_PWM_BASIC_A_HIGH_B_LOW; + g_apt0.waveform.chAOutType = APT_PWM_OUT_BASIC_TYPE; + g_apt0.waveform.chBOutType = APT_PWM_OUT_BASIC_TYPE; + g_apt0.waveform.divInitVal = 0; + g_apt0.waveform.cntInitVal = 0; + g_apt0.waveform.cntCmpLeftEdge = 500; /* 500 is count compare point of the left edge of PWM waveform */ + g_apt0.waveform.cntCmpRightEdge = 4000; /* 4000 is count compare point of the right edge of PWM waveform */ + g_apt0.waveform.cntCmpLoadMode = APT_BUFFER_INDEPENDENT_LOAD; + g_apt0.waveform.cntCmpLoadEvt = APT_COMPARE_LOAD_EVENT_ZERO; + g_apt0.waveform.deadBandCnt = 225; /* 225 is dead-band value */ + + /* ADC Trigger SOCA */ + g_apt0.adcTrg.trgEnSOCA = BASE_CFG_ENABLE; + g_apt0.adcTrg.cntCmpSOCA = 375; /* 375 is count compare point of ADC trigger source SOCA when using CMPA */ + g_apt0.adcTrg.trgSrcSOCA = APT_CS_SRC_CNTR_CMPA_DOWN; + g_apt0.adcTrg.trgScaleSOCA = 1; + + /* ADC Trigger SOCB */ + g_apt0.adcTrg.trgEnSOCB = BASE_CFG_ENABLE; + g_apt0.adcTrg.cntCmpSOCB = 1; + g_apt0.adcTrg.trgSrcSOCB = APT_CS_SRC_CNTR_CMPB_DOWN; + g_apt0.adcTrg.trgScaleSOCB = 1; + + g_apt0.adcTrg.cntCmpLoadMode = APT_BUFFER_INDEPENDENT_LOAD; + g_apt0.adcTrg.cntCmpLoadEvt = APT_COMPARE_LOAD_EVENT_ZERO; + + /* Timer Trigger */ + g_apt0.tmrInterrupt.tmrInterruptEn = BASE_CFG_ENABLE; + g_apt0.tmrInterrupt.tmrInterruptSrc = APT_INT_SRC_CNTR_ZERO; + g_apt0.tmrInterrupt.tmrInterruptScale = 1; + + APT0_ProtectInit(); + + HAL_APT_PWMInit(&g_apt0); + HAL_APT_RegisterCallBack(&g_apt0, APT_EVENT_INTERRUPT, MotorSysErrCallback); + IRQ_SetPriority(IRQ_APT0_EVT, 7); /* 7 is priority value */ + IRQ_Register(IRQ_APT0_EVT, HAL_APT_EventIrqHandler, &g_apt0); + IRQ_EnableN(IRQ_APT0_EVT); + HAL_APT_RegisterCallBack(&g_apt0, APT_TIMER_INTERRUPT, MotorCarrierProcessCallback); + IRQ_SetPriority(IRQ_APT0_TMR, 5); /* 5 is priority value */ + IRQ_Register(IRQ_APT0_TMR, HAL_APT_TimerIrqHandler, &g_apt0); + IRQ_EnableN(IRQ_APT0_TMR); +} + +static void APT1_ProtectInit(void) +{ + APT_OutCtrlProtectEx protectApt = {0}; + protectApt.ocEventEnEx = BASE_CFG_ENABLE; + protectApt.ocEventModeEx = APT_OUT_CTRL_ONE_SHOT; + protectApt.ocActionEx = APT_OUT_CTRL_ACTION_LOW; + protectApt.ocActionBEx = APT_OUT_CTRL_ACTION_LOW; + protectApt.ocEvtInterruptEnEx = BASE_CFG_DISABLE; + protectApt.ocSysEvent = APT_SYS_EVT_DEBUG | APT_SYS_EVT_CLK | APT_SYS_EVT_MEM; + protectApt.originalEvtEx = APT_EM_ORIGINAL_SRC_ACMP0; + protectApt.evtPolarityMaskEx = APT_EM_ACMP0_INVERT_BIT; + protectApt.filterCycleNumEx = 0; + HAL_APT_ProtectInitEx(&g_apt1, &protectApt); +} + +static void APT1_Init(void) +{ + HAL_CRG_IpEnableSet(APT1_BASE, IP_CLK_ENABLE); + + g_apt1.baseAddress = APT1; + + /* Clock Settings */ + g_apt1.waveform.dividerFactor = 1 - 1; + /* Timer Settings */ + g_apt1.waveform.timerPeriod = 7500; /* 7500 is count period of APT time-base timer */ + g_apt1.waveform.cntMode = APT_COUNT_MODE_UP_DOWN; + + /* Wave Form */ + g_apt1.waveform.basicType = APT_PWM_BASIC_A_HIGH_B_LOW; + g_apt1.waveform.chAOutType = APT_PWM_OUT_BASIC_TYPE; + g_apt1.waveform.chBOutType = APT_PWM_OUT_BASIC_TYPE; + g_apt1.waveform.divInitVal = 0; + g_apt1.waveform.cntInitVal = 0; + g_apt1.waveform.cntCmpLeftEdge = 500; /* 500 is count compare point of the left edge of PWM waveform */ + g_apt1.waveform.cntCmpRightEdge = 4000; /* 4000 is count compare point of the right edge of PWM waveform */ + g_apt1.waveform.cntCmpLoadMode = APT_BUFFER_INDEPENDENT_LOAD; + g_apt1.waveform.cntCmpLoadEvt = APT_COMPARE_LOAD_EVENT_ZERO; + g_apt1.waveform.deadBandCnt = 225; /* 225 is dead-band value */ + + APT1_ProtectInit(); + + HAL_APT_PWMInit(&g_apt1); +} + +static void APT2_ProtectInit(void) +{ + APT_OutCtrlProtectEx protectApt = {0}; + protectApt.ocEventEnEx = BASE_CFG_ENABLE; + protectApt.ocEventModeEx = APT_OUT_CTRL_ONE_SHOT; + protectApt.ocActionEx = APT_OUT_CTRL_ACTION_LOW; + protectApt.ocActionBEx = APT_OUT_CTRL_ACTION_LOW; + protectApt.ocEvtInterruptEnEx = BASE_CFG_DISABLE; + protectApt.ocSysEvent = APT_SYS_EVT_DEBUG | APT_SYS_EVT_CLK | APT_SYS_EVT_MEM; + protectApt.originalEvtEx = APT_EM_ORIGINAL_SRC_ACMP0; + protectApt.evtPolarityMaskEx = APT_EM_ACMP0_INVERT_BIT; + protectApt.filterCycleNumEx = 0; + HAL_APT_ProtectInitEx(&g_apt2, &protectApt); +} + +static void APT2_Init(void) +{ + HAL_CRG_IpEnableSet(APT2_BASE, IP_CLK_ENABLE); + + g_apt2.baseAddress = APT2; + + /* Clock Settings */ + g_apt2.waveform.dividerFactor = 1 - 1; + /* Timer Settings */ + g_apt2.waveform.timerPeriod = 7500; /* 7500 is count period of APT time-base timer */ + g_apt2.waveform.cntMode = APT_COUNT_MODE_UP_DOWN; + + /* Wave Form */ + g_apt2.waveform.basicType = APT_PWM_BASIC_A_HIGH_B_LOW; + g_apt2.waveform.chAOutType = APT_PWM_OUT_BASIC_TYPE; + g_apt2.waveform.chBOutType = APT_PWM_OUT_BASIC_TYPE; + g_apt2.waveform.divInitVal = 0; + g_apt2.waveform.cntInitVal = 0; + g_apt2.waveform.cntCmpLeftEdge = 500; /* 500 is count compare point of the left edge of PWM waveform */ + g_apt2.waveform.cntCmpRightEdge = 4000; /* 4000 is count compare point of the right edge of PWM waveform */ + g_apt2.waveform.cntCmpLoadMode = APT_BUFFER_INDEPENDENT_LOAD; + g_apt2.waveform.cntCmpLoadEvt = APT_COMPARE_LOAD_EVENT_ZERO; + g_apt2.waveform.deadBandCnt = 225; /* 225 is dead-band value */ + + APT2_ProtectInit(); + + HAL_APT_PWMInit(&g_apt2); +} + +__weak void MotorStartStopKeyCallback(void *param) +{ + GPIO_Handle *handle = (GPIO_Handle *)param; + BASE_FUNC_UNUSED(handle); +} + +static void GPIO_Init(void) +{ + HAL_CRG_IpEnableSet(GPIO2_BASE, IP_CLK_ENABLE); + g_gpio2.baseAddress = GPIO2; + + g_gpio2.pins = GPIO_PIN_3; + HAL_GPIO_Init(&g_gpio2); + HAL_GPIO_SetDirection(&g_gpio2, g_gpio2.pins, GPIO_OUTPUT_MODE); + HAL_GPIO_SetValue(&g_gpio2, g_gpio2.pins, GPIO_HIGH_LEVEL); + HAL_GPIO_SetIrqType(&g_gpio2, g_gpio2.pins, GPIO_INT_TYPE_NONE); + + g_gpio2.pins = GPIO_PIN_4; + HAL_GPIO_Init(&g_gpio2); + HAL_GPIO_SetDirection(&g_gpio2, g_gpio2.pins, GPIO_INPUT_MODE); + HAL_GPIO_SetValue(&g_gpio2, g_gpio2.pins, GPIO_HIGH_LEVEL); + HAL_GPIO_SetIrqType(&g_gpio2, g_gpio2.pins, GPIO_INT_TYPE_LOW_LEVEL); + + HAL_CRG_IpEnableSet(GPIO1_BASE, IP_CLK_ENABLE); + g_gpio1.baseAddress = GPIO1; + + g_gpio1.pins = GPIO_PIN_0; + HAL_GPIO_Init(&g_gpio1); + HAL_GPIO_SetDirection(&g_gpio1, g_gpio1.pins, GPIO_OUTPUT_MODE); + HAL_GPIO_SetValue(&g_gpio1, g_gpio1.pins, GPIO_HIGH_LEVEL); + HAL_GPIO_SetIrqType(&g_gpio1, g_gpio1.pins, GPIO_INT_TYPE_NONE); + + HAL_GPIO_RegisterCallBack(&g_gpio2, GPIO_PIN_4, MotorStartStopKeyCallback); + IRQ_Register(IRQ_GPIO2, HAL_GPIO_IrqHandler, &g_gpio2); + IRQ_SetPriority(IRQ_GPIO2, 1); /* set gpio1 interrupt priority to 1, 1~15. 1 is priority value */ + IRQ_EnableN(IRQ_GPIO2); /* gpio interrupt enable */ + + return; +} + +static void PGA0_Init(void) +{ + HAL_CRG_IpEnableSet(PGA0_BASE, IP_CLK_ENABLE); + + g_pga0.baseAddress = PGA0_BASE; + g_pga0.externalResistorMode = BASE_CFG_ENABLE; + g_pga0.handleEx.extCapCompensation = PGA_EXT_COMPENSATION_2X; + HAL_PGA_Init(&g_pga0); +} + +static void PGA1_Init(void) +{ + HAL_CRG_IpEnableSet(PGA1_BASE, IP_CLK_ENABLE); + + g_pga1.baseAddress = PGA1_BASE; + g_pga1.externalResistorMode = BASE_CFG_ENABLE; + g_pga1.handleEx.extCapCompensation = PGA_EXT_COMPENSATION_2X; + HAL_PGA_Init(&g_pga1); +} + + +static void I2C0_Init(void) +{ + HAL_CRG_IpEnableSet(I2C0_BASE, IP_CLK_ENABLE); /* I2C0 clock enable. */ + g_i2c0.baseAddress = I2C0; + + g_i2c0.functionMode = I2C_MODE_SELECT_MASTER_ONLY; + g_i2c0.addrMode = I2C_7_BITS; + g_i2c0.sdaHoldTime = 10; /* 10 is sda Hold Time */ + g_i2c0.freq = 100000; /* freqence is 400000 */ + g_i2c0.transferBuff = NULL; + g_i2c0.ignoreAckFlag = BASE_CFG_DISABLE; + g_i2c0.handleEx.spikeFilterTime = 0; + g_i2c0.handleEx.sdaDelayTime = 0; + g_i2c0.timeout = 10000; /* 10000 is time out */ + g_i2c0.state = I2C_STATE_RESET; + HAL_I2C_Init(&g_i2c0); +} + +static void QDM1_Init(void) +{ + HAL_CRG_IpEnableSet(QDM1_BASE, IP_CLK_ENABLE); + g_qdm1.baseAddress = QDM1_BASE; + + /* emulation config */ + g_qdm1.emuMode = QDM_EMULATION_MODE_RUN_FREE; + /* input config */ + g_qdm1.ctrlConfig.decoderMode = QDM_QUADRATURE_COUNT; + + g_qdm1.motorLineNum = 16384; + + g_qdm1.inputFilter.qdmAFilterLevel = 100; + g_qdm1.inputFilter.qdmBFilterLevel = 100; + g_qdm1.inputFilter.qdmZFilterLevel = 100; + g_qdm1.ctrlConfig.polarity = 0; + g_qdm1.ctrlConfig.swap = QDM_SWAP_DISABLE; + g_qdm1.ctrlConfig.resolution = QDM_4X_RESOLUTION; + + g_qdm1.pcntMode = QDM_PCNT_MODE_BY_DIR; + g_qdm1.pcntRstMode = QDM_PCNT_RST_OVF; + g_qdm1.posInit = 0; + g_qdm1.pcntIdxInitMode = QDM_IDX_INIT_DISABLE; + g_qdm1.lock_mode = QDM_LOCK_RISING_INDEX; + g_qdm1.posMax = 4294967295; + + g_qdm1.tsuPrescaler = 0; + g_qdm1.cevtPrescaler = QDM_CEVT_PRESCALER_DIVI1; + g_qdm1.qcMax = 65535; + + g_qdm1.ctrlConfig.ptuMode = QDM_PTU_MODE_CYCLE; + g_qdm1.period = 1; + g_qdm1.ctrlConfig.trgLockMode = QDM_TRG_BY_READ; + g_qdm1.subModeEn = true; + + HAL_QDM_Init(&g_qdm1); +} + +__weak void CheckPotentiometerValueCallback(void *handle) +{ + BASE_FUNC_UNUSED(handle); + /* USER CODE BEGIN CheckPotentiometerValueCallback */ + /* USER CODE END CheckPotentiometerValueCallback */ +} + +static void TIMER0_Init(void) +{ + HAL_CRG_IpEnableSet(TIMER0_BASE, IP_CLK_ENABLE); /* TIMER0 clock enable. */ + unsigned int load = (float)(HAL_CRG_GetIpFreq((void *)TIMER0) / (1u << (TIMERPRESCALER_NO_DIV * 4)) / 1000000u) * 1000000; + + g_timer0.baseAddress = TIMER0; + g_timer0.load = (load < 1) ? 0 : (load - 1); /* Set timer value immediately */ + g_timer0.bgLoad = (load < 1) ? 0 : (load - 1); /* Set timer value */ + g_timer0.mode = TIMER_MODE_RUN_PERIODIC; /* Run in period mode */ + g_timer0.prescaler = TIMERPRESCALER_NO_DIV; /* Don't frequency division */ + g_timer0.size = TIMER_SIZE_32BIT; /* 1 for 32bit, 0 for 16bit */ + g_timer0.interruptEn = BASE_CFG_ENABLE; + g_timer0.adcSocReqEnable = BASE_CFG_DISABLE; + g_timer0.dmaReqEnable = BASE_CFG_DISABLE; + HAL_TIMER_Init(&g_timer0); + IRQ_Register(IRQ_TIMER0, HAL_TIMER_IrqHandler, &g_timer0); + + HAL_TIMER_RegisterCallback(&g_timer0, TIMER_PERIOD_FIN, CheckPotentiometerValueCallback); + IRQ_SetPriority(IRQ_TIMER0, 1); /* 1 is priority value */ + IRQ_EnableN(IRQ_TIMER0); +} + +__weak void MotorStatemachineCallBack(void *handle) +{ + BASE_FUNC_UNUSED(handle); + /* USER CODE BEGIN MotorStatemachineCallBack */ + /* USER CODE END MotorStatemachineCallBack */ +} + +static void TIMER1_Init(void) +{ + HAL_CRG_IpEnableSet(TIMER1_BASE, IP_CLK_ENABLE); /* TIMER1 clock enable. */ + unsigned int load = (float)(HAL_CRG_GetIpFreq((void *)TIMER1) / (1u << (TIMERPRESCALER_NO_DIV * 4)) / 1000000u) * 500; + + g_timer1.baseAddress = TIMER1; + g_timer1.load = (load < 1) ? 0 : (load - 1); /* Set timer value immediately */ + g_timer1.bgLoad = (load < 1) ? 0 : (load - 1); /* Set timer value */ + g_timer1.mode = TIMER_MODE_RUN_PERIODIC; /* Run in period mode */ + g_timer1.prescaler = TIMERPRESCALER_NO_DIV; /* Don't frequency division */ + g_timer1.size = TIMER_SIZE_32BIT; /* 1 for 32bit, 0 for 16bit */ + g_timer1.interruptEn = BASE_CFG_ENABLE; + g_timer1.adcSocReqEnable = BASE_CFG_DISABLE; + g_timer1.dmaReqEnable = BASE_CFG_DISABLE; + HAL_TIMER_Init(&g_timer1); + IRQ_Register(IRQ_TIMER1, HAL_TIMER_IrqHandler, &g_timer1); + + HAL_TIMER_RegisterCallback(&g_timer1, TIMER_PERIOD_FIN, MotorStatemachineCallBack); + IRQ_SetPriority(IRQ_TIMER1, 1); /* 1 is priority value */ + IRQ_EnableN(IRQ_TIMER1); +} + +__weak void TIMER2_InterruptProcess(void *handle) +{ + BASE_FUNC_UNUSED(handle); + /* USER CODE BEGIN TIMER2_InterruptProcess */ + /* USER CODE END TIMER2_InterruptProcess */ +} + + +__weak void UART0InterruptErrorCallback(void *handle) +{ + BASE_FUNC_UNUSED(handle); + /* USER CODE BEGIN UART0_TRNS_IT_ERROR */ + /* USER CODE END UART0_TRNS_IT_ERROR */ +} + +__weak void UART0_TXDMACallback(void *handle) +{ + BASE_FUNC_UNUSED(handle); + /* USER CODE BEGIN UART0_WRITE_DMA_FINISH */ + /* USER CODE END UART0_WRITE_DMA_FINISH */ +} + +__weak void UART0WriteInterruptCallback(void *handle) +{ + BASE_FUNC_UNUSED(handle); + /* USER CODE BEGIN UART0_WRITE_IT_FINISH */ + /* USER CODE END UART0_WRITE_IT_FINISH */ +} + +__weak void UART0ReadInterruptCallback(void *handle) +{ + BASE_FUNC_UNUSED(handle); + /* USER CODE BEGIN UART0_READ_IT_FINISH */ + /* USER CODE END UART0_READ_IT_FINISH */ +} + +static void UART0_Init(void) +{ + HAL_CRG_IpEnableSet(UART0_BASE, IP_CLK_ENABLE); /* UART0 clock enable. */ + g_uart0.baseAddress = UART0; + + g_uart0.baudRate = UART0_BAND_RATE; + g_uart0.dataLength = UART_DATALENGTH_8BIT; + g_uart0.stopBits = UART_STOPBITS_ONE; + g_uart0.parity = UART_PARITY_NONE; + g_uart0.txMode = UART_MODE_DMA; + g_uart0.rxMode = UART_MODE_INTERRUPT; + g_uart0.fifoMode = BASE_CFG_ENABLE; + g_uart0.fifoTxThr = UART_FIFODEPTH_SIZE8; + g_uart0.fifoRxThr = UART_FIFODEPTH_SIZE8; + g_uart0.hwFlowCtr = BASE_CFG_DISABLE; + g_uart0.handleEx.overSampleMultiple = UART_OVERSAMPLING_16X; + g_uart0.handleEx.msbFirst = BASE_CFG_DISABLE; + HAL_UART_Init(&g_uart0); + HAL_UART_RegisterCallBack(&g_uart0, UART_TRNS_IT_ERROR, (UART_CallbackType)UART0InterruptErrorCallback); + HAL_UART_RegisterCallBack(&g_uart0, UART_READ_IT_FINISH, (UART_CallbackType)UART0ReadInterruptCallback); + IRQ_Register(IRQ_UART0, HAL_UART_IrqHandler, &g_uart0); + IRQ_SetPriority(IRQ_UART0, 6); /* 6 is priority value */ + IRQ_EnableN(IRQ_UART0); + g_uart0.dmaHandle = &g_dmac; + g_uart0.uartDmaTxChn = 0; + HAL_UART_RegisterCallBack(&g_uart0, UART_WRITE_DMA_FINISH, (UART_CallbackType)UART0_TXDMACallback); + HAL_UART_RegisterCallBack(&g_uart0, UART_WRITE_IT_FINISH, (UART_CallbackType)UART0WriteInterruptCallback); +} + +static void IOConfig(void) +{ + SYSCTRL0->SC_SYS_STAT.BIT.update_mode = 0; + SYSCTRL0->SC_SYS_STAT.BIT.update_mode_clear = 1; + /* Config PIN39 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO0_3_AS_UART0_TXD); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO0_3_AS_UART0_TXD, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO0_3_AS_UART0_TXD, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO0_3_AS_UART0_TXD, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO0_3_AS_UART0_TXD, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN40 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO0_4_AS_UART0_RXD); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO0_4_AS_UART0_RXD, PULL_UP); /* Pull-up and Pull-down, UART RX recommend PULL_UP */ + HAL_IOCMG_SetPinSchmidtMode(GPIO0_4_AS_UART0_RXD, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO0_4_AS_UART0_RXD, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO0_4_AS_UART0_RXD, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN1 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO0_7_AS_ACMP0_OUT); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO0_7_AS_ACMP0_OUT, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO0_7_AS_ACMP0_OUT, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO0_7_AS_ACMP0_OUT, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO0_7_AS_ACMP0_OUT, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN48 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO0_6_AS_ACMP_N2); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO0_6_AS_ACMP_N2, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO0_6_AS_ACMP_N2, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO0_6_AS_ACMP_N2, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO0_6_AS_ACMP_N2, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN47 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO0_5_AS_ACMP_P2); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO0_5_AS_ACMP_P2, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO0_5_AS_ACMP_P2, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO0_5_AS_ACMP_P2, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO0_5_AS_ACMP_P2, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN4 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO2_6_AS_PGA0_N0); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO2_6_AS_PGA0_N0, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO2_6_AS_PGA0_N0, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO2_6_AS_PGA0_N0, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO2_6_AS_PGA0_N0, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN5 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO2_5_AS_PGA0_P0); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO2_5_AS_PGA0_P0, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO2_5_AS_PGA0_P0, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO2_5_AS_PGA0_P0, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO2_5_AS_PGA0_P0, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN3 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO2_7_AS_PGA0_OUT); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO2_7_AS_PGA0_OUT, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO2_7_AS_PGA0_OUT, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO2_7_AS_PGA0_OUT, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO2_7_AS_PGA0_OUT, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN7 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO3_7_AS_ADC_AIN9); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO3_7_AS_ADC_AIN9, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO3_7_AS_ADC_AIN9, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO3_7_AS_ADC_AIN9, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO3_7_AS_ADC_AIN9, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN2 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO5_1_AS_ADC_AIN5); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO5_1_AS_ADC_AIN5, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO5_1_AS_ADC_AIN5, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO5_1_AS_ADC_AIN5, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO5_1_AS_ADC_AIN5, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN14 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO4_7_AS_ADC_AIN15); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO4_7_AS_ADC_AIN15, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO4_7_AS_ADC_AIN15, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO4_7_AS_ADC_AIN15, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO4_7_AS_ADC_AIN15, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN11 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO1_5_AS_PGA1_P0); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO1_5_AS_PGA1_P0, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO1_5_AS_PGA1_P0, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO1_5_AS_PGA1_P0, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO1_5_AS_PGA1_P0, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN12 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO1_6_AS_PGA1_N0); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO1_6_AS_PGA1_N0, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO1_6_AS_PGA1_N0, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO1_6_AS_PGA1_N0, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO1_6_AS_PGA1_N0, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN13 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO1_7_AS_PGA1_OUT); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO1_7_AS_PGA1_OUT, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO1_7_AS_PGA1_OUT, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO1_7_AS_PGA1_OUT, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO1_7_AS_PGA1_OUT, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN35 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO2_3_AS_GPIO2_3); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO2_3_AS_GPIO2_3, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO2_3_AS_GPIO2_3, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO2_3_AS_GPIO2_3, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO2_3_AS_GPIO2_3, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN27 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO1_0_AS_GPIO1_0); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO1_0_AS_GPIO1_0, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO1_0_AS_GPIO1_0, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO1_0_AS_GPIO1_0, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO1_0_AS_GPIO1_0, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN41 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO2_4_AS_GPIO2_4); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO2_4_AS_GPIO2_4, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO2_4_AS_GPIO2_4, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO2_4_AS_GPIO2_4, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO2_4_AS_GPIO2_4, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN31 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO2_0_AS_QDM1_A); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO2_0_AS_QDM1_A, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO2_0_AS_QDM1_A, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO2_0_AS_QDM1_A, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO2_0_AS_QDM1_A, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN32 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO2_1_AS_QDM1_B); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO2_1_AS_QDM1_B, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO2_1_AS_QDM1_B, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO2_1_AS_QDM1_B, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO2_1_AS_QDM1_B, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN33 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO5_0_AS_QDM1_INDEX); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO5_0_AS_QDM1_INDEX, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO5_0_AS_QDM1_INDEX, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO5_0_AS_QDM1_INDEX, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO5_0_AS_QDM1_INDEX, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN19 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO3_0_AS_APT0_PWMA); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO3_0_AS_APT0_PWMA, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO3_0_AS_APT0_PWMA, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO3_0_AS_APT0_PWMA, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO3_0_AS_APT0_PWMA, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN23 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO4_0_AS_APT0_PWMB); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO4_0_AS_APT0_PWMB, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO4_0_AS_APT0_PWMB, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO4_0_AS_APT0_PWMB, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO4_0_AS_APT0_PWMB, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN20 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO3_1_AS_APT1_PWMA); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO3_1_AS_APT1_PWMA, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO3_1_AS_APT1_PWMA, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO3_1_AS_APT1_PWMA, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO3_1_AS_APT1_PWMA, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN24 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO4_1_AS_APT1_PWMB); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO4_1_AS_APT1_PWMB, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO4_1_AS_APT1_PWMB, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO4_1_AS_APT1_PWMB, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO4_1_AS_APT1_PWMB, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN21 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO3_2_AS_APT2_PWMA); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO3_2_AS_APT2_PWMA, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO3_2_AS_APT2_PWMA, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO3_2_AS_APT2_PWMA, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO3_2_AS_APT2_PWMA, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN25 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO4_2_AS_APT2_PWMB); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO4_2_AS_APT2_PWMB, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO4_2_AS_APT2_PWMB, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO4_2_AS_APT2_PWMB, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO4_2_AS_APT2_PWMB, DRIVER_RATE_2); /* Output signal edge fast/slow */ + + + /* Config PIN15 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO4_5_AS_I2C0_SCL); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO4_5_AS_I2C0_SCL, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO4_5_AS_I2C0_SCL, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO4_5_AS_I2C0_SCL, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO4_5_AS_I2C0_SCL, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN16 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO4_3_AS_I2C0_SDA); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO4_3_AS_I2C0_SDA, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO4_3_AS_I2C0_SDA, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO4_3_AS_I2C0_SDA, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO4_3_AS_I2C0_SDA, DRIVER_RATE_2); /* Output signal edge fast/slow */ + +} + +static void APT_SyncMasterInit(void) +{ + HAL_APT_MasterSyncInit(&g_apt0, APT_SYNC_OUT_ON_CNTR_ZERO); +} + +static void APT_SyncSlaveInit(void) +{ + APT_SlaveSyncIn aptSlave; + + aptSlave.cntPhase = 0; /* counter phase value */ + aptSlave.syncCntMode = APT_COUNT_MODE_AFTER_SYNC_UP; + aptSlave.syncInSrc = APT_SYNCIN_SRC_APT0_SYNCOUT; /* sync source selection */ + aptSlave.cntrSyncSrc = APT_CNTR_SYNC_SRC_SYNCIN; + HAL_APT_SlaveSyncInit(&g_apt1, &aptSlave); + + aptSlave.cntPhase = 0; /* counter phase value */ + aptSlave.syncCntMode = APT_COUNT_MODE_AFTER_SYNC_UP; + aptSlave.syncInSrc = APT_SYNCIN_SRC_APT0_SYNCOUT; /* sync source selection */ + aptSlave.cntrSyncSrc = APT_CNTR_SYNC_SRC_SYNCIN; + HAL_APT_SlaveSyncInit(&g_apt2, &aptSlave); + +} + +void SystemInit(void) +{ + IOConfig(); + DMA_Init(); + UART0_Init(); + ACMP0_Init(); + APT0_Init(); + APT1_Init(); + APT2_Init(); + ADC0_Init(); + PGA0_Init(); + PGA1_Init(); + TIMER0_Init(); + TIMER1_Init(); + QDM1_Init(); + GPIO_Init(); + I2C0_Init(); + APT_SyncMasterInit(); + APT_SyncSlaveInit(); + + /* USER CODE BEGIN system_init */ + /* USER CODE END system_init */ +} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/main.c b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/main.c new file mode 100644 index 00000000..b77983aa --- /dev/null +++ b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/main.c @@ -0,0 +1,81 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file main.c + * @author MCU Driver Team + * @brief Main program body. + */ + +#include "typedefs.h" +#include "feature.h" +#include "mcs_motor_process.h" +#include "main.h" +/* USER CODE BEGIN 0 */ +/* USER CODE 区域内代码不会被覆盖,区域外会被生成的默认代码覆盖(其余USER CODE 区域同理) */ +/* 建议用户放置头文件 */ + +#include "debug.h" +/* USER CODE END 0 */ +QDM_Handle g_qdm1; +ACMP_Handle g_acmp0; +PGA_Handle g_pga0; +PGA_Handle g_pga1; +TIMER_Handle g_timer0; +TIMER_Handle g_timer1; +TIMER_Handle g_timer2; +UART_Handle g_uart0; +APT_Handle g_apt0; +APT_Handle g_apt1; +APT_Handle g_apt2; +ADC_Handle g_adc0; +DMA_Handle g_dmac; +GPIO_Handle g_gpio2; +GPIO_Handle g_gpio1; +I2C_Handle g_i2c0; +/* USER CODE BEGIN 1 */ +/* 建议用户定义全局变量、结构体、宏定义或函数声明等 */ + + +/* USER CODE END 1 */ + +int main(void) +{ + /* USER CODE BEGIN 2 */ + /* 建议用户放置初始化代码或启动代码等 */ + /* USER CODE END 2 */ + MotorMainProcess(); + + // SystemInit(); /* 系统初始化 */ + /* USER CODE BEGIN 3 */ + /* 建议用户放置初始配置代码 */ + /* USER CODE END 3 */ + while (1) { + /* USER CODE BEGIN 4 */ + /* 建议用户放置周期性执行代码 */ + + BASE_FUNC_DELAY_MS(100); + /* USER CODE END 4 */ + } + /* USER CODE BEGIN 5 */ + /* 建议用户放置代码流程 */ + /* USER CODE END 5 */ + return BASE_STATUS_OK; +} + +/* USER CODE BEGIN 6 */ +/* 建议用户放置自定义函数 */ +/* USER CODE END 6 */ \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/inc/mcs_carrier.h b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/inc/mcs_carrier.h new file mode 100644 index 00000000..e09442d2 --- /dev/null +++ b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/inc/mcs_carrier.h @@ -0,0 +1,153 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_carrier.h + * @author MCU Algorithm Team + * @brief This file provides functions declaration for carrier interrupt processing function. + */ +#ifndef McuMagicTag_MCS_CARRIER_H +#define McuMagicTag_MCS_CARRIER_H + +#include "mcs_status.h" +#include "mcs_mtr_param.h" +#include "mcs_svpwm.h" +#include "mcs_curr_ctrl.h" +#include "mcs_if_ctrl.h" +#include "mcs_ramp_mgmt.h" +#include "mcs_spd_ctrl.h" +#include "mcs_pos_ctrl.h" +#include "mcs_fosmo.h" +#include "mcs_smo_4th.h" +#include "mcs_pll.h" +#include "mcs_startup.h" +#include "mcs_r1_svpwm.h" +#include "mcs_fw_ctrl.h" +#include "mcs_prot_user.h" +#include "mcs_inc_enc.h" + +typedef void (*MCS_ReadCurrUvwCb)(UvwAxis *CurrUvw); +typedef void (*MCS_SetPwmDutyCb)(UvwAxis *dutyUvwLeft, UvwAxis *dutyUvwRight); +typedef void (*MCS_SetADCTriggerTimeCb)(unsigned short cntCmpSOCA, unsigned short cntCmpSOCB); +typedef void (*MCS_GetEncAngSpd)(float *speed, float *angle); + +/** + * @brief motor control FSM state define. + * @details motor control FSM state type: + * + FSM_IDLE -- IDLE state, system startup default. + * + FSM_OFFSET_CALIB -- Self calibrate, for ADC init. + * + FSM_CAP_CHARGE -- IPM cap charge. + * + FSM_CLEAR -- Clear before every run. + * + FSM_LOCATE -- Rotor position pre-locate. + * + FSM_STARTUP -- Start up. + * + FSM_SWITCH -- Transition state, control from open to closed loop. + * + FSM_RUN -- Normal running state. + * + FSM_WAIT_STOP -- Wait stopping. + * + FSM_STOP -- Normal stop. + * + FSM_FAULT -- Fault state, waiting for user process. + */ +typedef enum { + FSM_IDLE = 0, + FSM_OFFSET_CALIB, + FSM_CAP_CHARGE, + FSM_CLEAR, + FSM_LOCATE, + FSM_STARTUP, + FSM_SWITCH, + FSM_RUN, + FSM_WAIT_STOP, + FSM_STOP, + FSM_FAULT +} FsmState; + +/** + * @brief Sampling mode. + */ +typedef enum { + DUAL_RESISTORS = 0, + SINGLE_RESISTOR = 1 +} SampleMode; + +/** + * @brief Motor control data structure + */ +typedef struct { + unsigned char motorStateFlag; + float spdCmdHz; /**< External input speed command value */ + float axisAngle; /**< Angle of the synchronous coordinate system */ + short motorSpinPos; /**< IF启动模式下电机旋转位置*/ + float spdRefHz; /**< Command value after speed ramp management */ + float currCtrlPeriod; /**< current loop control period */ + float adc0Compensate; /**< ADC0 softwaretrim compensate value */ + float adc1Compensate; /**< ADC1 softwaretrim compensate value */ + float udc; /**< Bus voltage */ + float powerBoardTemp; /**< Power boart surface temperature */ + unsigned short aptMaxcntCmp; /**< Apt Maximum Comparison Count */ + float adcCurrCofe; /**< Adc current sampling cofeature */ + + unsigned short sysTickCnt; /**< System Timer Tick Count */ + unsigned short capChargeTickNum; /**< Bootstrap Capacitor Charge Tick Count */ + volatile unsigned int msTickCnt; /**< Millisecond-level counter, which can be used in 1-ms and 5-ms tasks. */ + unsigned short msTickNum; /**< Number of ticks corresponding to 1 ms */ + char obserType; /**< Set Observer Type */ + char controlMode; /**< Set foc control or sixstep bldc control mode or others */ + char spdAdjustMode; /**< Set speed adjust mode */ + char uartConnectFlag; /**< Uart connect success flag */ + short uartHeartDetCnt; /**< Uart connect heart detect count */ + float uartTimeStamp; /**< Uart data time stamp */ + SysStatusReg statusReg; /**< System status */ + FsmState stateMachine; /**< Motor Control State Machine */ + + SampleMode sampleMode; /**< sample mode */ + MOTOR_Param mtrParam; /**< Motor parameters */ + FOSMO_Handle smo; /**< SMO observer handle */ + SMO4TH_Handle smo4th; /**< SMO 4th observer handle */ + EncoderHandle *encHandle; /**< Encoder parameter handle */ + IF_Handle ifCtrl; /**< I/F control handle */ + SVPWM_Handle sv; /**< SVPWM Handle */ + R1SVPWM_Handle r1Sv; /**< Single-resistance phase-shifted SVPWM handld */ + RMG_Handle spdRmg; /**< Ramp management struct for the speed controller input reference */ + SPDCTRL_Handle spdCtrl; /**< Speed loop Control Handle */ + POSCTRL_Handle posCtrl; /**< Position controller handle. */ + CURRCTRL_Handle currCtrl; /**< Current loop control handle */ + STARTUP_Handle startup; /**< Startup Switch Handle */ + FW_Handle fw; /**< Flux-Weakening Handle */ + + DqAxis idqRef; /**< Command value of the dq axis current */ + UvwAxis currUvw; /**< Three-phase current sampling value */ + AlbeAxis iabFbk; /**< αβ-axis current feedback value */ + DqAxis idqFbk; /**< Current feedback value of the dq axis */ + DqAxis vdqRef; /**< Current loop output dq voltage */ + AlbeAxis vabRef; /**< Current loop output voltage αβ */ + UvwAxis dutyUvw; /**< UVW three-phase duty cycle */ + UvwAxis dutyUvwLeft; /**< Single Resistor UVW Three-Phase Left Duty Cycle */ + UvwAxis dutyUvwRight; /**< Single Resistor UVW Three-Phase Right Duty Cycle*/ + + MCS_ReadCurrUvwCb readCurrUvwCb; /**< Read current callback function */ + MCS_SetPwmDutyCb setPwmDutyCb; /**< Set the duty cycle callback function. */ + MCS_SetADCTriggerTimeCb setADCTriggerTimeCb; /**< Sets the ADC trigger point callback function. */ + + MotorProtStatus_Handle prot; /**< Protection handle. */ + + MCS_GetEncAngSpd getEncAngSpd; /**< Get the angle and speed of the encoder. */ + short encReady; + float encSpeed; + float encAxisAngle; +} MTRCTRL_Handle; + +void MCS_CarrierProcess(MTRCTRL_Handle *mtrCtrl); + +#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/inc/mcs_chip_config.h b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/inc/mcs_chip_config.h new file mode 100644 index 00000000..2fc50e46 --- /dev/null +++ b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/inc/mcs_chip_config.h @@ -0,0 +1,81 @@ +/** + * @ Copyright (c) HiSilicon (Shanghai) Technologies Co., Ltd. 2022-2023. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_chip_config.h + * @author MCU Algorithm Team + * @brief This file provides config macros for ECMCU105H/ECBMCU201MPC app. + */ + +#ifndef McuMagicTag_MCS_CHIPCONFIG_H +#define McuMagicTag_MCS_CHIPCONFIG_H + +#include "feature.h" + +#ifdef CHIP_3061MNPICA + + #define ADCPTT_HANDLE g_adc0 + #define ADCRESIS_HANDLE g_adc0 + #define ADCUDC_HANDLE g_adc0 + #define ADCU_HANDLE g_adc0 + #define ADCW_HANDLE g_adc0 + #define LED1_HANDLE g_gpio2 + #define LED2_HANDLE g_gpio1 + #define LED2_PIN GPIO_PIN_0 + #define LED1_PIN GPIO_PIN_3 + #define ADCPTTSOCNUM ADC_SOC_NUM0 + #define ADCUSOCNUM ADC_SOC_NUM1 + #define ADCRESISSOCNUM ADC_SOC_NUM3 + #define ADCUDCSOCNUM ADC_SOC_NUM4 + #define ADCWSOCNUM ADC_SOC_NUM6 + + #define ADC0COMPENSATE 2037.0f + #define ADC1COMPENSATE 2027.0f + + #define QDMNUM QDM1 + #define QDMIRQNUM IRQ_QDM1 + #define QDMBASEADDR QDM1_BASE + +#endif + +#if defined (CHIP_3065HRPIRZ) || defined (CHIP_3065ARPIRZ) + + #define ADCU_HANDLE g_adc0 + #define ADCW_HANDLE g_adc1 + #define ADCRESIS_HANDLE g_adc2 + #define ADCUDC_HANDLE g_adc2 + #define ADCPTT_HANDLE g_adc2 + #define LED1_HANDLE g_gpio0 + #define LED2_HANDLE g_gpio0 + #define LED2_PIN GPIO_PIN_6 + #define LED1_PIN GPIO_PIN_7 + #define ADCPTTSOCNUM ADC_SOC_NUM0 + #define ADCRESISSOCNUM ADC_SOC_NUM1 + #define ADCUSOCNUM ADC_SOC_NUM8 + #define ADCWSOCNUM ADC_SOC_NUM8 + #define ADCUDCSOCNUM ADC_SOC_NUM14 + + #define ADC0COMPENSATE 2033.0f + #define ADC1COMPENSATE 2070.0f + + #define QDMNUM QDM0 + #define QDMIRQNUM IRQ_QDM0 + #define QDMBASEADDR QDM0_BASE + +#endif + + +#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/inc/mcs_ctlmode_config.h b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/inc/mcs_ctlmode_config.h new file mode 100644 index 00000000..0c158026 --- /dev/null +++ b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/inc/mcs_ctlmode_config.h @@ -0,0 +1,75 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_ctlmode_config.h + * @author MCU Algorithm Team + * @brief This file provides config macros for ECMCU105H app. + */ + + /* Define to prevent recursive inclusion ------------------------------------------------------- */ +#ifndef McuMagicTag_MCS_CTLMODECONFIG_H +#define McuMagicTag_MCS_CTLMODECONFIG_H + +#include "debug.h" +#include "typedefs.h" + +typedef enum { + FOC_STARTUP_IF = 0, + FOC_STARTUP_VF, + FOC_STARTUP_HFI +} MOTOR_STARTUPMODE_CONFIG; + +typedef enum { + FOC_OBSERVERTYPE_SMO1TH = 0, + FOC_OBSERVERTYPE_SMO1TH_PLL, + FOC_OBSERVERTYPE_ENC, + FOC_OBSERVERTYPE_SMO4TH_PLL, + FOC_OBSERVERTYPE_LUNBORG, + FOC_OBSERVERTYPE_FLUX, + FOC_OBSERVERTYPE_SMO4TH +} MOTOR_OBSERVERTYPE_CONFIG; + +typedef enum { + FOC_CONTROLMODE_SPEED = 0, + FOC_CONTROLMODE_TORQUE, + FOC_CONTROLMODE_POS +} MOTOR_CONTROLMODE_CONFIG; + +typedef enum { + FOC_CURQAXISPID_PARAMS = 0, + FOC_CURDAXISPID_PARAMS, + FOC_SPDPID_PARAMS +} MOTOR_PID_SET; + +typedef enum { + MOTOR_PARAMS_BASE = 0, + MOTOR_PARAMS_SPECIAL, + MOTOR_PARAMS_BOARD +} MOTOR_PARAMS_SET; + +typedef enum { + CUST_SPEED_ADJUST = 0, + HOST_SPEED_ADJUST +} MODE_ADSPEED_CONFIG; + +typedef enum { + CONNECTING = 0, + CONNECTED, + DISCONNECT +} UART_STATUS; + +#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/inc/mcs_ex_common.h b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/inc/mcs_ex_common.h new file mode 100644 index 00000000..fe7117d9 --- /dev/null +++ b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/inc/mcs_ex_common.h @@ -0,0 +1,78 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_ex_common.h + * @author MCU Algorithm Team + * @brief + */ + +/* Define to prevent recursive inclusion ------------------------------------------------------- */ +#ifndef MCU_MAGICTAG_MCS_EX_COMMON_H +#define MCU_MAGICTAG_MCS_EX_COMMON_H + +/* Includes ------------------------------------------------------------------------------------ */ +#include "interrupt_ip.h" +#include "typedefs.h" + +#define CRG_ENABLE_VALUE 3 + +#define CPU_FRQ_HZ 200000000u /* CPU frequency */ +#define CPU_MHZ 1000000u +#define TIMER_FRQ_DIV 2 + +/** + * @defgroup EXTEND EXTEND + * @brief Extend common module. + * @{ + */ + +/** + * @defgroup EXTEND_Common EXTEND Common + * @brief EXTEND common external module. + * @{ + */ + +/* Typedef definitions ------------------------------------------------------------------------- */ + +/** + * @defgroup EXTEND_Param_Def EXTEND Parameters Definition + * @brief Definition of EXTEND parameters. + * @{ + */ + +typedef struct { + IRQ_ID irqNum; /* Interrupt Number */ + void *baseAddr; /* Peripherals baseAddr */ +} NvicHandle; + + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ +#endif diff --git a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/inc/mcs_inc_enc.h b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/inc/mcs_inc_enc.h new file mode 100644 index 00000000..97fbda72 --- /dev/null +++ b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/inc/mcs_inc_enc.h @@ -0,0 +1,126 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_incremental_enc.h + * @author MCU Algorithm Team + * @brief This file provides function of QDM module and encoder speed angle calculation. + */ + +/* Define to prevent recursive inclusion ------------------------------------------------------- */ +#ifndef MCU_MAGICTAG_MCS_INC_ENC_H +#define MCU_MAGICTAG_MCS_INC_ENC_H + +/* Includes ------------------------------------------------------------------------------------ */ +#include "qdm_ip.h" +#include "mcs_ex_common.h" + +/* Macro definitions --------------------------------------------------------------------------- */ +#define SPDBUF_MAXSIZE 20 + +/** + * @brief QDM Peripheral management. + */ +typedef struct { + void *qdmAddr; /* QDM peripheral address */ + NvicHandle zPulsesNvic; +} MCS_QdmHandle; + +/** + * @brief QDM initialization structure. + */ +typedef struct { + void *qdmAddr; /**< QDM peripheral address */ + NvicHandle *zPlusesNvic; /**< z Pulse Interrupt Configuration */ + unsigned int zPlusesIrqPrio; /**< Interrupt priority */ + IRQ_PROC_FUNC zPlusesIrqFunc; /**< Interrupt function */ +} MCS_QdmInitStru; + +/** + * @brief Encoder motor parameter structure. + */ +typedef struct { + signed int mtrPPMR; /**< pulse per mechanical round */ + unsigned int zShift; /**< pulse Z shift */ + unsigned int mtrNp; /**< numbers of pole pairs */ + float ctrlPeriod; /**< The encoder calculates the control period. */ + /**< the times of compute the angle, if reach the specified num, it will be reset to 0 again */ + unsigned int timeNum; +} MCS_EncInitStru; + +/** + * @brief encoder control data structure. + */ +typedef struct { + signed int pulsePerMechRound; /**< pulses of each mechanical round */ + signed int pulsePerElecRound; /**< pulses of each eletricity period */ + signed short zShift; /**< Z-pulse cheap compensation */ + float elecAngle; /**< electricity angle */ + float mechAngle; /**< motor mechine angle */ + unsigned short cntNow; /**< counter for now */ + unsigned short cntPre; /**< counter of last record */ + signed short pulsePos; /**< Number of pulses corresponding to mechanical + position [-32768, 32767] */ + float elecSpeed; /**< elec speed, in HZ */ + signed short speedBuf[SPDBUF_MAXSIZE]; /**< speed buffer */ + signed short speedBufSize; /**< speed buffer size */ + signed short speedBufIndex; /**< speed bufer index */ + float pulseToHzPu; /**< pulse to HZ transition value */ + + unsigned short timeCnt; /**< times of compute the angle, the carrier ISR call times */ + /**< the times of compute the angle, if reach the specified num, it will be reset to 0 again */ + unsigned short timeNum; + + unsigned short pulZCnt; /**< counter of Z pulse */ + unsigned short pulseAngle; /**< Number of pulses corresponding to electrical angle. + [0, pulsePerElecRound-1] */ + float pulseToElecAngle; /**< pulse to electricity angle transition */ + unsigned short testAngle; +} EncoderHandle; +/** + * @} + */ + +/** + * @defgroup MCS_INC_ENC_Declaration incremental encoder. + * @{ + */ +void MCS_QdmInit(MCS_QdmInitStru *qdmInit); +void MCS_GetEncoderCnt(EncoderHandle *handle, QDM_RegStruct *qdm); +void MCS_GetElecAngleByEnc(EncoderHandle *handle); +void MCS_GetElecSpeedByEnc(EncoderHandle *handle); +void MCS_EncoderClear(EncoderHandle *handle); +void MCS_EncoderInit(EncoderHandle *handle, MCS_EncInitStru *encParam); +/** + * @brief Get the QDM position counter. + * @retval unsigned short QDM SCNT. + */ +static inline unsigned short MCS_GetQdmPosCnt(QDM_RegStruct *qdm) +{ + return (unsigned short)(qdm->QPOSCNT); +} +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ +#endif /* McuMagicTag_MCS_INC_ENC_H */ \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/inc/mcs_motor_process.h b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/inc/mcs_motor_process.h new file mode 100644 index 00000000..783bf214 --- /dev/null +++ b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/inc/mcs_motor_process.h @@ -0,0 +1,45 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_motor_process.h + * @author MCU Algorithm Team + * @brief This file provides motor sample functions declaration for ECMCU105H board. + */ +#ifndef McuMagicTag_MCS_MOTOR_PROCESS_H +#define McuMagicTag_MCS_MOTOR_PROCESS_H + +/** + * @brief phase sequence. + */ +typedef enum { + PHASE_U = 0, + PHASE_V, + PHASE_W, + PHASE_MAX_NUM +} MCS_PhaseEnum; + +/** + * @brief key state. + */ +typedef enum { + KEY_DOWN = 0, + KEY_UP = 1, +} KEY_State; + +int MotorMainProcess(void); + +#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/inc/mcs_status.h b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/inc/mcs_status.h new file mode 100644 index 00000000..19510c78 --- /dev/null +++ b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/inc/mcs_status.h @@ -0,0 +1,188 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_status.h + * @author MCU Algorithm Team + * @brief This file provides functions declaration of system status. + */ + +/* Define to prevent recursive inclusion ------------------------------------------------------- */ +#ifndef McuMagicTag_MCS_STATUS_H +#define McuMagicTag_MCS_STATUS_H + +/* Includes ------------------------------------------------------------------------------------ */ +#include "typedefs.h" +#include "mcs_assert.h" + +/* Typedef definitions ------------------------------------------------------------------------- */ +/** + * @brief System status define + */ +typedef union { + unsigned short all; + struct { + unsigned short cmdStart : 1; /**< Indicates that a start system command has been received. */ + unsigned short cmdStop : 1; /**< Indicates that a stop system command has been received. */ + unsigned short isRunning : 1; /**< Indicates that the system is running (enable signal) */ + unsigned short sysError : 1; /**< Indicates that the system reports an error. */ + unsigned short poweron : 1; /**< Indicates that the power-on initialization phase is complete. */ + unsigned short capcharge : 1; /**< Indicates that the bootstrap capacitor charging phase is complete. */ + unsigned short adczero : 1; /**< The current sampling point is reset to zero after power-on. */ + } Bit; +} SysStatusReg; + +/** + * @brief Get status of Bit cmdStart. + * @param sysStatus System status register handle. + * @retval Status of Bit cmdStart. + */ +static inline bool SysGetCmdStart(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + bool ret; + ret = (sysStatus->Bit.cmdStart == 1) ? true : false; + return ret; +} + +/** + * @brief Set Bit cmdStart. + * @param sysStatus System status register handle. + * @retval None. + */ +static inline void SysCmdStartSet(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + sysStatus->Bit.cmdStart = 1; +} + +/** + * @brief Clear Bit cmdStart. + * @param handle System status register handle. + * @retval None. + */ +static inline void SysCmdStartClr(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + sysStatus->Bit.cmdStart = 0; +} + +/** + * @brief Get status of Bit cmdStop. + * @param sysStatus System status register handle. + * @retval Status of Bit cmdStart. + */ +static inline bool SysGetCmdStop(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + bool ret; + ret = (sysStatus->Bit.cmdStop == 1) ? true : false; + return ret; +} + +/** + * @brief Set Bit cmdStop. + * @param sysStatus System status register handle. + * @retval None. + */ +static inline void SysCmdStopSet(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + sysStatus->Bit.cmdStop = 1; +} + +/** + * @brief Clear Bit cmdStop. + * @param sysStatus System status register handle. + * @retval None. + */ +static inline void SysCmdStopClr(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + sysStatus->Bit.cmdStop = 0; +} + +/** + * @brief Get status of Bit isRunning. + * @param sysStatus System status register handle. + * @retval Status of Bit isRunning. + */ +static inline bool SysIsRunning(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + bool ret; + ret = (sysStatus->Bit.isRunning == 1) ? true : false; + return ret; +} + +/** + * @brief Set Bit isRuning. + * @param sysStatus System status register handle. + * @retval None. + */ +static inline void SysRunningSet(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + sysStatus->Bit.isRunning = 1; +} + +/** + * @brief Clear Bit isRuning. + * @param sysStatus System status register handle. + * @retval None. + */ +static inline void SysRunningClr(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + sysStatus->Bit.isRunning = 0; +} + +/** + * @brief Get status of Bit sysError. + * @param sysStatus System status register handle. + * @retval Status of Bit sysError. + */ +static inline bool SysIsError(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + bool ret; + ret = (sysStatus->Bit.sysError == 1) ? true : false; + return ret; +} + +/** + * @brief Set Bit sysError. + * @param sysStatus System status register handle. + * @retval None. + */ +static inline void SysErrorSet(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + sysStatus->Bit.sysError = 1; +} + +/** + * @brief Clear Bit sysError. + * @param sysStatus System status register handle. + * @retval None. + */ +static inline void SysErrorClr(SysStatusReg *sysStatus) +{ + MCS_ASSERT_PARAM(sysStatus != NULL); + sysStatus->Bit.sysError = 0; +} + +#endif diff --git a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/inc/mcs_user_config.h b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/inc/mcs_user_config.h new file mode 100644 index 00000000..95a8aa17 --- /dev/null +++ b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/inc/mcs_user_config.h @@ -0,0 +1,143 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_user_config.h + * @author MCU Algorithm Team + * @brief This file provides functions declaration of user config parameters. + */ +#ifndef McuMagicTag_MCS_CONFIG_H +#define McuMagicTag_MCS_CONFIG_H + +#include "debug.h" +#include "typedefs.h" + +#define SYSTICK_PERIOD_US 500u /* Systick定时器的周期,单位为微秒 */ + +#define INV_CAP_CHARGE_MS 3u /* 反向电容充电时间,单位为毫秒 */ + +#define INV_VOLTAGE_BUS 24.0f // 总线电压,单位为伏特 + +#define CTRL_CURR_PERIOD 0.0001f /* 载波中断服务例程的周期,100微秒 */ +#define CTRL_SYSTICK_PERIOD 0.0005f /* Systick控制周期,500微秒 */ + +/* 采样窗口的占空比,实际时间为0.06*50us = 3us。 */ +#define SAMPLE_WINDOW_DUTY 0.06f + +/* 采样点移位作为翻转点的占空比,实际时间为0.008*50us = 0.4us。 */ +#define SAMPLE_POINT_SHIFT 0.008f + +/* 采样电阻200毫欧姆,0.0013295 */ +#define ADC_CURR_COFFI 0.0013295f /* 3.3/4096/3.03/0.2 pga: 3.03, 200mohm */ +/* APT */ +#define APT_SYNC_IN_SRC APT_SYNCIN_SRC_APT0_SYNCOUT /* APT同步输入源 */ + +#define APT_U APT0_BASE /* U相APT模块的基地址 */ +#define APT_V APT1_BASE /* V相APT模块的基地址 */ +#define APT_W APT2_BASE /* W相APT模块的基地址 */ + +/* User_Commond */ +#define CTRL_IF_CURR_AMP_A 1.0f /* IF控制电流幅值 */ +#define USER_TARGET_SPD_HZ 40.0f /* 用户目标速度,单位为赫兹,括号用于输入负指令 */ + +#define USER_MIN_SPD_HZ 5.0f /* 电机最小速度限制,单位为赫兹 */ +#define USER_MAX_SPD_HZ 200.0f /* 电机最大速度限制,单位为赫兹 */ +#define USER_SPD_SLOPE 30.0f /* 速度变化斜率 */ +#define USER_CURR_SLOPE (CTRL_IF_CURR_AMP_A * 5.0f) /* 电流变化斜率 */ + +/* PID PARAMS */ +#define CURRQAXIS_KP 0.7414f /* Q轴电流PID控制器的比例系数 */ +#define CURRQAXIS_KI 1256.0f /* Q轴电流PID控制器的积分系数 */ +#define CURRDAXIS_KP 0.7414f /* D轴电流PID控制器的比例系数 */ +#define CURRDAXIS_KI 1256.0f /* D轴电流PID控制器的积分系数 */ +#define CURR_LOWERLIM (-INV_VOLTAGE_BUS * ONE_DIV_SQRT3 * 0.95f) /* 电流下限 */ +#define CURR_UPPERLIM (INV_VOLTAGE_BUS * ONE_DIV_SQRT3 * 0.95f) /* 电流上限 */ + +#define SPD_KP 0.01f /* 速度PID控制器的比例系数 */ +#define SPD_KI 0.1f /* 速度PID控制器的积分系数 */ +#define SPD_LOWERLIM -1.0f /* 速度下限 */ +#define SPD_UPPERLIM 1.0f /* 速度上限 */ + +#define POS_KP 5.0f /* 位置PID控制器的比例系数 */ +#define POS_KI 0.3f /* 位置PID控制器的积分系数 */ +#define POS_KD 0.01f /* 位置PID控制器的微分系数 */ +#define POS_NS 10.0f /* 位置环Ns参数 */ +#define POS_LOWERLIM -200.0f /* 位置下限 */ +#define POS_UPPERLIM 200.0f /* 位置上限 */ + +/* MOTOR PARAMS */ +/* Np, Rs, Ld, Lq, Psif, J, Nmax, Currmax, PPMR, zShift */ +/* mtrPsif & mtrJ parameter is not used in this project, temporarily set to 0 */ + +/* 电机参数 */ +/* Np, Rs, Ld, Lq, Psif, J, Nmax, Currmax, PPMR, zShift */ +/* mtrPsif & mtrJ 参数在这个项目中没有使用,暂时设置为0 */ +// 电机的极对数 +/* 电机的电阻,单位为欧姆 */ +/* 电机的直轴电感,单位为亨利 */ +/* 电机的交轴电感,单位为亨利 */ +/* 电机的磁链,本项目未使用 */ +/* 电机的转动惯量,本项目未使用 */ +/* 电机的最大电气转速,单位为赫兹 */ +/* 电机的最大电流,单位为安培 */ +/* 电机的每转脉冲数 */ +/* 电机的零位偏移 */ + +#define MOTORPARAM_DEFAULTS { \ + .mtrNp = 4, \ + .mtrRs = 0.5f, \ + .mtrLd = 0.000295f, \ + .mtrLq = 0.000295f, \ + .mtrPsif = 0.0f, \ + .mtrJ = 0.0f, \ + .maxElecSpd = 200.0f, \ + .maxCurr = 1.5f, \ + .mtrPPMR = 4000, \ + .zShift = 410, \ +} + +// // //1025电机 +// #define MOTORPARAM_DEFAULTS { \ +// .mtrNp = 21, \ +// .mtrRs = 2.5f, \ +// .mtrLd = 0.066f, \ +// .mtrLq = 0.066f, \ +// .mtrPsif = 0.0f, \ +// .mtrJ = 0.0f, \ +// .maxElecSpd = 200.0f, \ +// .maxCurr = 1.5f, \ +// .mtrPPMR = 4000, \ +// .zShift = 410, \ +// } + + +// // GIM4310-10 +// #define MOTORPARAM_DEFAULTS { \ +// .mtrNp = 14, \ +// .mtrRs = 1.046f, \ +// .mtrLd = 0.000344f, \ +// .mtrLq = 0.000344f, \ +// .mtrPsif = 0.0f, \ +// .mtrJ = 0.0f, \ +// .maxElecSpd = 200.0f, \ +// .maxCurr = 1.5f, \ +// .mtrPPMR = 4000, \ +// .zShift = 410, \ +// } + +#define ADC_UDC_COFFI 0.01289f /* 0.01289 = 3.3/4096*192/12 */ +// ADC和UDC的系数,用于将ADC值转换为实际电压值 +#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/common/mcs_prot_cmm.c b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/common/mcs_prot_cmm.c new file mode 100644 index 00000000..99766bdf --- /dev/null +++ b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/common/mcs_prot_cmm.c @@ -0,0 +1,43 @@ +/** + * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_prot_cmm.c + * @author MCU Algorithm Team + * @brief This file contains protection common api definition. + */ + +/* Includes ------------------------------------------------------------------------------------ */ +#include "mcs_prot_cmm.h" +#include "apt.h" +#include "mcs_assert.h" + +/** + * @brief Safty-pulse-off function execution to turn off all the power devices. + * @param aptAddr Three-phase APT address pointer. + * @retval None. + */ +void ProtSpo_Exec(APT_RegStruct **aptAddr) +{ + MCS_ASSERT_PARAM(aptAddr != NULL); + /**< Turn off all the six power devices of the inverter. */ + for (unsigned int i = 0; i < MOTOR_PHASE_NUMBER; i++) { + APT_RegStruct *aptx = (APT_RegStruct *)(aptAddr[i]); + DCL_APT_EnableSwContPWMAction(aptx, APT_PWM_CHANNEL_A); + DCL_APT_EnableSwContPWMAction(aptx, APT_PWM_CHANNEL_B); + DCL_APT_ForcePWMOutputLow(aptx); + } +} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/common/mcs_prot_cmm.h b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/common/mcs_prot_cmm.h new file mode 100644 index 00000000..65e5632f --- /dev/null +++ b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/common/mcs_prot_cmm.h @@ -0,0 +1,136 @@ +/** + * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_prot_cmm.h + * @author MCU Algorithm Team + * @brief This file contains protection function common data struct and api declaration. + */ + +/* Define to prevent recursive inclusion ------------------------------------------------------- */ +#ifndef McuMagicTag_MCS_PROT_CMM_H +#define McuMagicTag_MCS_PROT_CMM_H + +#include "typedefs.h" +#include "apt_ip.h" + +/* Macro definitions --------------------------------------------------------------------------- */ +#define MOTOR_PHASE_NUMBER (3) + +#define PROT_VAL_THRESHOLD_NUMS (4) +#define PROT_VAL_THRESHOLD_0 (0) +#define PROT_VAL_THRESHOLD_1 (1) +#define PROT_VAL_THRESHOLD_2 (2) +#define PROT_VAL_THRESHOLD_3 (3) + +#define PROT_LIMIT_TIME_NUMS (3) +#define PROT_LIMIT_TIME_0 (0) +#define PROT_LIMIT_TIME_1 (1) +#define PROT_LIMIT_TIME_2 (2) + +#define MOTOR_PHASE_NUMBER (3) + +/**< Motor error status definition. */ +typedef union { + int all; + struct { + unsigned short overCurrErr : 1; /**<表示相电流超过保护值*/ + unsigned short overVoltErr : 1; /**<表示直流链路电压超过保护值*/ + unsigned short lowerVoltErr : 1; /**<表示直流链路电压低于保护值*/ + unsigned short overIpmTempErr : 1; /**<表示IPM温度超过保护值*/ + unsigned short revRotErr : 1; /**<表示电机为负方向*/ + unsigned short motorStalling : 1; /**<表示转子失速*/ + unsigned short overMotorTempErr : 1; /**<表示三相电流不平衡*/ + unsigned short posSnsrCommsErr : 1; /**<表示位置传感器与MCU的通信中断*/ + unsigned short posSnsrFuncErr : 1; /**<表示位置传感器报告功能错误*/ + unsigned short posSnsrCalibrErr : 1; /**<表示位置传感器无法自行校准*/ + unsigned short currOutOfBalance : 1; /**<表示转子反向旋转*/ + unsigned short phsOpenErr : 1; /**<表示相绕组断开*/ + unsigned short phsU : 1; /**<表示发生phsOpenErr时u阶段失败*/ + unsigned short phsV : 1; /**<表示发生phsOpenErr时v阶段失败*/ + unsigned short phsW : 1; /**<表示发生phsOpenErr时w阶段失败*/ + unsigned short multiPhs : 1; /**<表示发生phsOpenErr时多阶段失败*/ + } Bit; +} MotorErrStatusReg; + +/**<保护状态位定义*/ +typedef enum { + OCP_ERR_BIT, + OVP_ERR_BIT, + LVP_ERR_BIT, + OTP_IPM_ERR_BIT, + OTP_MOTOR_ERR_BIT, + STALLING_ERR_BIT, + CURR_OUT_BALANCE_ERR_BIT, + POS_COMMS_ERR_BIT, + POS_FUNC_ERR_BIT, + POS_CALIB_ERR_BIT, + REV_ROT_ERR_BIT, + PHS_OPEN_ERR_BIT, + PHS_U_ERR_BIT, + PHS_V_ERR_BIT, + PHS_W_ERR_BIT, + PHS_MULTI_ERR_BIT, +} PROT_ErrBit; + +/**< Motor error protection level. */ +typedef enum { + PROT_LEVEL_0 = 0, + PROT_LEVEL_1, + PROT_LEVEL_2, + PROT_LEVEL_3, + PROT_LEVEL_4 /**< The greater level number, the severe error is. */ +} PROT_Level; + +/** + * @brief Obtains the status of a bit of data. + * @param data data. + * @param bits Number of digits. + * @retval Bit status. + */ +static inline bool GetBit(int data, unsigned short bit) +{ + bool ret; + ret = ((data >> bit) & 1); + return ret; +} + +/** + * @brief Sets the status of a bit of data. + * @param data data. + * @param bit The setted bit. + * @retval None. + */ +static inline void SetBit(int *data, unsigned char bit) +{ + *data |= (1 << bit); +} + +/** + * @brief Clear the status of a bit of data. + * @param data data. + * @param bit The Clear bit. + * @retval None. + */ +static inline void ClearBit(int *data, unsigned char bit) +{ + *data &= ~(1 << bit); +} + +/**< Protection action. */ +void ProtSpo_Exec(APT_RegStruct **aptAddr); + +#endif diff --git a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/common/mcs_prot_user.c b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/common/mcs_prot_user.c new file mode 100644 index 00000000..e97876b7 --- /dev/null +++ b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/common/mcs_prot_user.c @@ -0,0 +1,137 @@ +/** + * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_prot_user.c + * @author MCU Algorithm Team + * @brief This file contains user protection data struct, inquiry api and initialization api definition. + */ + +/* Includes ------------------------------------------------------------------------------------ */ +#include "mcs_prot_user.h" +#include "mcs_prot_user_config.h" +#include "mcs_assert.h" + +/** + * @brief Get motor over current error status. + * @param motorErrStatus Motor error status. + * @retval Error status. + */ +bool IsMotorOverCurrErr(MotorErrStatusReg motorErrStatus) +{ + if (motorErrStatus.Bit.overCurrErr) { + return true; + } else { + return false; + } +} + +/** + * @brief Get motor over dc-link voltage error status. + * @param motorErrStatus Motor error status. + * @retval Error status. + */ +bool IsDcLinkOverVoltErr(MotorErrStatusReg motorErrStatus) +{ + if (motorErrStatus.Bit.overVoltErr) { + return true; + } else { + return false; + } +} + +/** + * @brief Get motor lower dc-link voltage error status. + * @param motorErrStatus Motor error status. + * @retval Error status. + */ +bool IsDcLinkLowerVoltErr(MotorErrStatusReg motorErrStatus) +{ + if (motorErrStatus.Bit.lowerVoltErr) { + return true; + } else { + return false; + } +} + +/** + * @brief Get motor over Ipm temperature error status. + * @param motorErrStatus Motor error status. + * @retval Error status. + */ +bool IsIpmOverTempErr(MotorErrStatusReg motorErrStatus) +{ + if (motorErrStatus.Bit.overIpmTempErr) { + return true; + } else { + return false; + } +} + +/** + * @brief Get motor over Motor temperature error status. + * @param motorErrStatus Motor error status. + * @retval Error status. + */ +bool IsMotorOverTempErr(MotorErrStatusReg motorErrStatus) +{ + if (motorErrStatus.Bit.overMotorTempErr) { + return true; + } else { + return false; + } +} + +/** + * @brief Get motor stalling error status. + * @param motorErrStatus Motor error status. + * @retval Error status. + */ +bool IsMotorStallingErr(MotorErrStatusReg motorErrStatus) +{ + if (motorErrStatus.Bit.motorStalling) { + return true; + } else { + return false; + } +} + +/** + * @brief Clear the motor error status. + * @param motorProt Motor protection handle. + * @retval Error status. + */ +void ClearMotorErrStatus(MotorProtStatus_Handle *motorProt) +{ + MCS_ASSERT_PARAM(motorProt != NULL); + /* Clear the motor error status. */ + motorProt->motorErrStatus.all = 0x00; + OCP_Clear(&motorProt->ocp); + OVP_Clear(&motorProt->ovp); + LVP_Clear(&motorProt->lvp); + OTP_Clear(&motorProt->otp); +} + +/** + * @brief Motor protection function initialization. + * @param motorProt Motor protection handle. + * @retval Error status. + */ +void MotorProt_Init(MotorProtStatus_Handle *motorProt) +{ + MCS_ASSERT_PARAM(motorProt != NULL); + motorProt->motorErrStatus.all = 0x00; +} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/common/mcs_prot_user.h b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/common/mcs_prot_user.h new file mode 100644 index 00000000..365110e1 --- /dev/null +++ b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/common/mcs_prot_user.h @@ -0,0 +1,54 @@ +/** + * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_prot_user.h + * @author MCU Algorithm Team + * @brief This file contains user protection data struct, inquiry api and initialization api declaration. + */ + +/* Define to prevent recursive inclusion ------------------------------------------------------- */ +#ifndef McuMagicTag_MCS_PROT_USER_H +#define McuMagicTag_MCS_PROT_USER_H + +/* Includes ------------------------------------------------------------------------------------ */ +#include "mcs_prot_cmm.h" +#include "mcs_curr_prot.h" +#include "mcs_dc_volt_prot.h" +#include "mcs_temp_prot.h" +#include "mcs_motor_stalling.h" +#include "typedefs.h" + +typedef struct { + MotorErrStatusReg motorErrStatus; /**< Motor error status. */ + OCP_Handle ocp; /**< Over current protection. */ + OVP_Handle ovp; /**< Over dc-link voltage protection. */ + LVP_Handle lvp; /**< Lower dc-link voltage protection. */ + OTP_Handle otp; /**< Over IPM temperature protection. */ + STP_Handle stall; /**< Motor stalling protection. */ +} MotorProtStatus_Handle; + +void MotorProt_Init(MotorProtStatus_Handle *motorProt); + +/**< Inquiry motor error status */ +bool IsMotorOverCurrErr(MotorErrStatusReg motorErrStatus); +bool IsDcLinkOverVoltErr(MotorErrStatusReg motorErrStatus); +bool IsDcLinkLowerVoltErr(MotorErrStatusReg motorErrStatus); +bool IsIpmOverTempErr(MotorErrStatusReg motorErrStatus); +bool IsMotorOverTempErr(MotorErrStatusReg motorErrStatus); +bool IsMotorStallingErr(MotorErrStatusReg motorErrStatus); +void ClearMotorErrStatus(MotorProtStatus_Handle *motorProt); +#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/common/mcs_prot_user_config.h b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/common/mcs_prot_user_config.h new file mode 100644 index 00000000..988c519d --- /dev/null +++ b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/common/mcs_prot_user_config.h @@ -0,0 +1,130 @@ +/** + * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_prot_user_config.h + * @author MCU Algorithm Team + * @brief This file contans user macro definition of the protection function. + */ + +/* Define to prevent recursive inclusion ------------------------------------------------------- */ +#ifndef McuMagicTag_MCS_PROT_USER_CONFIG_H +#define McuMagicTag_MCS_PROT_USER_CONFIG_H + +/* Macro definitions --------------------------------------------------------------------------- */ + +/**< COMMON */ +/**<连续运行时电机或IPM的峰值相电流(A)*/ +#define PROT_MOTOR_RATED_CURR (1.0f) +/**<只有几次连续的故障检测才能触发错误状态*/ +#define PROT_CNT_LIMIT (100) +/**<只有几个连续的无故障检测才能触发错误状态的消除*/ +#define RECY_CNT_LIMIT (10000) +/**<只有几个连续的无故障检测才能触发错误状态的消除*/ +#define OVER_VOLT_RECY_CNT_LIMIT (100) +/**<只有几个连续的无故障检测才能触发错误状态的消除*/ +#define LOWER_VOLT_RECY_CNT_LIMIT (100) + +/**<过电流保护*/ +/**<处于1级时的过电流触发值(A)*/ +#define PROT_OVER_CURR_POW_DN1 (2.0f * PROT_MOTOR_RATED_CURR) +/**<处于2级时的过电流触发值(A)*/ +#define PROT_OVER_CURR_POW_DN2 (2.0f * PROT_MOTOR_RATED_CURR) +/**<处于3级时的过电流触发值(A)*/ +#define PROT_OVER_CURR_POW_DN3 (2.0f * PROT_MOTOR_RATED_CURR) +/**<处于4级时的过电流触发值(A)*/ +#define PROT_OVER_CURR_POW_OFF (2.0f * PROT_MOTOR_RATED_CURR) +#define PROT_OVER_CURR_RECY_DELTA (0.0035f) /*<从保护状态恢复时的电流间隙(A)*/ +#define PROT_OVER_CURR_LIMIT1_TIME_SEC (30.0f) /**<20%过载的最长持续时间:30秒*/ +#define PROT_OVER_CURR_LIMIT2_TIME_SEC (10.0f) /**<20%过载的最长持续时间:10秒*/ +#define PROT_OVER_CURR_LIMIT3_TIME_SEC (2.0f) /**<20%过载的最长持续时间:2秒*/ + +/**< Over voltage protection */ +#define PROT_OVER_VOLT_BRK_ON1 (26.0f) /**<处于1级时,直流链路电压触发值(V)过高*/ +#define PROT_OVER_VOLT_BRK_ON2 (27.0f) /**< Over dc-link voltage trigger value (V) when in level 2. */ +#define PROT_OVER_VOLT_BRK_ON3 (28.0f) /**< Over dc-link voltage trigger value (V) when in level 3. */ +#define PROT_OVER_VOLT_BRK_ALL (30.0f) /**< Over dc-link voltage trigger value (V) when in level 4. */ +#define PROT_OVER_VOLT_RECY_DELTA (0.5f) /**<从保护状态恢复时的电压间隙(V)*/ +#define PROT_OVER_VOLT_LIMIT1_TIME_SEC (5.0f) /**1;单循环模式:<0.5*/ +#define POS_SNSR_IPD_INJ_PERIOD (4) + +#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/mcs_curr_prot.c b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/mcs_curr_prot.c new file mode 100644 index 00000000..da4e56f8 --- /dev/null +++ b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/mcs_curr_prot.c @@ -0,0 +1,267 @@ +/** + * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_curr_prot.c + * @author MCU Algorithm Team + * @brief This file contains current protecion api definition. + */ + +/* Includes ------------------------------------------------------------------------------------ */ +#include "mcs_curr_prot.h" +#include "mcs_math.h" +#include "mcs_prot_user_config.h" +#include "mcs_assert.h" + +/** + * @brief Initilization over current protection function. + * @param ocp Over current protection handle. + * @param ts Ctrl period (s). + * @retval None. + */ +void OCP_Init(OCP_Handle *ocp, float ts) +{ + MCS_ASSERT_PARAM(ocp != NULL); + MCS_ASSERT_PARAM(ts > 0.0f); + ocp->ts = ts; + OCP_Clear(ocp); + + ocp->protCntLimit = PROT_CNT_LIMIT; + ocp->recyCntLimit = RECY_CNT_LIMIT; + /* Configuring four levels of current protection thresholds. */ + ocp->protValThr[PROT_VAL_THRESHOLD_0] = PROT_OVER_CURR_POW_DN1; + ocp->protValThr[PROT_VAL_THRESHOLD_1] = PROT_OVER_CURR_POW_DN2; + ocp->protValThr[PROT_VAL_THRESHOLD_2] = PROT_OVER_CURR_POW_DN3; + ocp->protValThr[PROT_VAL_THRESHOLD_3] = PROT_OVER_CURR_POW_OFF; + /* Configure the protection limit time. */ + ocp->protLimitTime[PROT_LIMIT_TIME_0] = PROT_OVER_CURR_LIMIT1_TIME_SEC; + ocp->protLimitTime[PROT_LIMIT_TIME_1] = PROT_OVER_CURR_LIMIT2_TIME_SEC; + ocp->protLimitTime[PROT_LIMIT_TIME_2] = PROT_OVER_CURR_LIMIT3_TIME_SEC; + ocp->recyDelta = PROT_OVER_CURR_RECY_DELTA; +} + +/** + * @brief Over current protection detection. + * @param ocp Over current protection handle. + * @param motorErrStatus Motor error status. + * @param idq DQ-axis feedback currents. + * @retval None. + */ +void OCP_Det(OCP_Handle *ocp, MotorErrStatusReg *motorErrStatus, DqAxis idq) +{ + MCS_ASSERT_PARAM(ocp != NULL); + MCS_ASSERT_PARAM(motorErrStatus != NULL); + /* Calculate current amplitude. */ + ocp->currAmp = Sqrt(idq.d * idq.d + idq.q * idq.q); + + /* Check if value goes over threshold for continuous cycles. */ + if (ocp->currAmp < ocp->protValThr[PROT_VAL_THRESHOLD_0]) { + ocp->protCnt = 0; + return; + } + + if (ocp->protCnt < ocp->protCntLimit) { + ocp->protCnt++; + return; + } + + /* Check which protection level should be activated. */ + /* Once enter error status, error status can only be changed by recover api. */ + /* protect level: level 4. */ + if (ocp->currAmp >= ocp->protValThr[PROT_VAL_THRESHOLD_3] && ocp->protLevel < PROT_LEVEL_4) { + ocp->protLevel = PROT_LEVEL_4; + motorErrStatus->Bit.overCurrErr = 1; + ocp->protCnt = 0; + return; + } + + /* Protect level: level 3. */ + if (ocp->currAmp >= ocp->protValThr[PROT_VAL_THRESHOLD_2] && ocp->protLevel < PROT_LEVEL_3) { + ocp->protLevel = PROT_LEVEL_3; + motorErrStatus->Bit.overCurrErr = 1; + ocp->protCnt = 0; + ocp->timer = 0.0f; + return; + } + + /* Protect level: level 2. */ + if (ocp->currAmp >= ocp->protValThr[PROT_VAL_THRESHOLD_1] && ocp->protLevel < PROT_LEVEL_2) { + ocp->protLevel = PROT_LEVEL_2; + motorErrStatus->Bit.overCurrErr = 1; + ocp->protCnt = 0; + ocp->timer = 0.0f; + return; + } + + /* Protect level: level 1. */ + if (ocp->currAmp >= ocp->protValThr[PROT_VAL_THRESHOLD_0] && ocp->protLevel < PROT_LEVEL_1) { + ocp->protLevel = PROT_LEVEL_1; + motorErrStatus->Bit.overCurrErr = 1; + ocp->protCnt = 0; + ocp->timer = 0.0f; + return; + } +} + +/** + * @brief Over current protection execution. + * @param ocp Over current protection handle. + * @param idqRef DQ-axis current references. + * @param aptAddr Three-phase APT address pointer. + * @retval None. + */ +void OCP_Exec(OCP_Handle *ocp, DqAxis *idqRef, APT_RegStruct **aptAddr) +{ + MCS_ASSERT_PARAM(ocp != NULL); + MCS_ASSERT_PARAM(idqRef != NULL); + MCS_ASSERT_PARAM(aptAddr != NULL); + + float id = idqRef->d; + float iq = idqRef->q; + float idqAmp = ocp->currAmp; + /* According to protect level, take corresponding action. */ + switch (ocp->protLevel) { + /* level 4: disable all PWM output. */ + case PROT_LEVEL_4: + /* Disable three-phase pwm output. */ + ProtSpo_Exec(aptAddr); + idqRef->d = 0.0f; + idqRef->q = 0.0f; + break; + + /* level 3: derate speed reference. */ + case PROT_LEVEL_3: + ocp->timer += ocp->ts; + if (ocp->timer > ocp->protLimitTime[PROT_LIMIT_TIME_2]) { + idqRef->d = id / idqAmp * PROT_MOTOR_RATED_CURR; + idqRef->q = iq / idqAmp * PROT_MOTOR_RATED_CURR; + } + break; + + /* level 2: derate speed reference. */ + case PROT_LEVEL_2: + ocp->timer += ocp->ts; + if (ocp->timer > ocp->protLimitTime[PROT_LIMIT_TIME_1]) { + idqRef->d = id / idqAmp * PROT_MOTOR_RATED_CURR; + idqRef->q = iq / idqAmp * PROT_MOTOR_RATED_CURR; + } + break; + + /* level 1: derate speed reference. */ + case PROT_LEVEL_1: + ocp->timer += ocp->ts; + if (ocp->timer > ocp->protLimitTime[PROT_LIMIT_TIME_0]) { + idqRef->d = id / idqAmp * PROT_MOTOR_RATED_CURR; + idqRef->q = iq / idqAmp * PROT_MOTOR_RATED_CURR; + } + break; + + /* level 0: take no protection action. */ + case PROT_LEVEL_0: + break; + + default: + break; + } +} + +/** + * @brief Over current protection recovery. + * @param ocp Over current protection handle. + * @param motorErrStatus Motor error status. + * @retval None. + */ +void OCP_Recy(OCP_Handle *ocp, MotorErrStatusReg *motorErrStatus) +{ + MCS_ASSERT_PARAM(ocp != NULL); + /* If not under error state, just return without any operation. */ + if (!motorErrStatus->Bit.overCurrErr) { + return; + } + + /* Calculate current amplitude. */ + float currAmp = ocp->currAmp; + + /* According to protection level, take corresponding recovery action. */ + switch (ocp->protLevel) { + /* level 4 */ + case PROT_LEVEL_4: + if (currAmp < ocp->protValThr[PROT_VAL_THRESHOLD_3] - ocp->recyDelta) { + ocp->recyCnt++; + if (ocp->recyCnt > ocp->recyCntLimit) { + ocp->protLevel = PROT_LEVEL_3; + ocp->recyCnt = 0; + } + } + break; + + /* level 3 */ + case PROT_LEVEL_3: + if (currAmp < ocp->protValThr[PROT_VAL_THRESHOLD_2] - ocp->recyDelta) { + ocp->recyCnt++; + if (ocp->recyCnt > ocp->recyCntLimit) { + ocp->protLevel = PROT_LEVEL_2; + ocp->recyCnt = 0; + } + } + break; + + /* level 2 */ + case PROT_LEVEL_2: + if (currAmp < ocp->protValThr[PROT_VAL_THRESHOLD_1] - ocp->recyDelta) { + ocp->recyCnt++; + if (ocp->recyCnt > ocp->recyCntLimit) { + ocp->protLevel = PROT_LEVEL_1; + ocp->recyCnt = 0; + } + } + break; + + /* level 1 */ + case PROT_LEVEL_1: + if (currAmp < ocp->protValThr[PROT_VAL_THRESHOLD_0] - ocp->recyDelta) { + ocp->recyCnt++; + if (ocp->recyCnt > ocp->recyCntLimit) { + ocp->protLevel = PROT_LEVEL_0; + ocp->recyCnt = 0; + } + } + break; + + /* level 0 */ + case PROT_LEVEL_0: + motorErrStatus->Bit.overCurrErr = 0; + break; + + default: + break; + } +} + +/** + * @brief Over current protection error status clear. + * @param ocp Over current protection handle. + * @retval None. + */ +void OCP_Clear(OCP_Handle *ocp) +{ + MCS_ASSERT_PARAM(ocp != NULL); + /* Clear the history value. */ + ocp->protCnt = 0; + ocp->protLevel = PROT_LEVEL_0; + ocp->recyCnt = 0; + ocp->timer = 0.0f; +} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/mcs_curr_prot.h b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/mcs_curr_prot.h new file mode 100644 index 00000000..e9916fe4 --- /dev/null +++ b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/mcs_curr_prot.h @@ -0,0 +1,52 @@ +/** + * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_curr_prot.h + * @author MCU Algorithm Team + * @brief This file contains current protection data struct and api declaration. + */ + +/* Define to prevent recursive inclusion ------------------------------------- */ +#ifndef McuMagicTag_MCS_CURR_PROT_H +#define McuMagicTag_MCS_CURR_PROT_H + +/* Includes ------------------------------------------------------------------------------------ */ +#include "mcs_prot_cmm.h" +#include "mcs_typedef.h" +#include "apt_ip.h" + +typedef struct { + unsigned int protCnt; + unsigned int recyCnt; + unsigned int protCntLimit; + unsigned int recyCntLimit; + float currAmp; + float protValThr[PROT_VAL_THRESHOLD_NUMS]; /* from low current level to high. */ + float protLimitTime[PROT_LIMIT_TIME_NUMS]; /* from level 3 to level 1. */ + float recyDelta; + float timer; + float ts; + PROT_Level protLevel; +} OCP_Handle; + +void OCP_Init(OCP_Handle *ocp, float ts); +void OCP_Det(OCP_Handle *ocp, MotorErrStatusReg *motorErrStatus, DqAxis idq); +void OCP_Exec(OCP_Handle *ocp, DqAxis *idqRef, APT_RegStruct **aptAddr); +void OCP_Recy(OCP_Handle *ocp, MotorErrStatusReg *motorErrStatus); +void OCP_Clear(OCP_Handle *ocp); + +#endif diff --git a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/mcs_dc_volt_prot.c b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/mcs_dc_volt_prot.c new file mode 100644 index 00000000..b0541a2e --- /dev/null +++ b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/mcs_dc_volt_prot.c @@ -0,0 +1,495 @@ +/** + * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_dc_volt_prot.c + * @author MCU Algorithm Team + * @brief This file contains dc-link voltage protection api declaration. + */ + +/* Includes ------------------------------------------------------------------------------------ */ +#include "mcs_dc_volt_prot.h" +#include "mcs_prot_user_config.h" +#include "mcs_assert.h" + +/** + * @brief Initilization over dc-link voltage protection function. + * @param ovp Over dc-link voltage protection handle. + * @param ts Ctrl period (s). + * @retval None. + */ +void OVP_Init(OVP_Handle *ovp, float ts) +{ + MCS_ASSERT_PARAM(ovp != NULL); + MCS_ASSERT_PARAM(ts > 0.0f); + ovp->ts = ts; + OVP_Clear(ovp); + ovp->protCntLimit = PROT_CNT_LIMIT; + ovp->recyCntLimit = OVER_VOLT_RECY_CNT_LIMIT; + /* Configuring four levels of overvoltage protection thresholds. */ + ovp->protValThr[PROT_VAL_THRESHOLD_0] = PROT_OVER_VOLT_BRK_ON1; + ovp->protValThr[PROT_VAL_THRESHOLD_1] = PROT_OVER_VOLT_BRK_ON2; + ovp->protValThr[PROT_VAL_THRESHOLD_2] = PROT_OVER_VOLT_BRK_ON3; + ovp->protValThr[PROT_VAL_THRESHOLD_3] = PROT_OVER_VOLT_BRK_ALL; + /* Configure the protection limit time. */ + ovp->protLimitTime[PROT_LIMIT_TIME_0] = PROT_OVER_VOLT_LIMIT1_TIME_SEC; + ovp->protLimitTime[PROT_LIMIT_TIME_1] = PROT_OVER_VOLT_LIMIT2_TIME_SEC; + ovp->protLimitTime[PROT_LIMIT_TIME_2] = PROT_OVER_VOLT_LIMIT3_TIME_SEC; + ovp->recyDelta = PROT_OVER_VOLT_RECY_DELTA; +} + +/** + * @brief Initilization lower dc-link voltage protection function. + * @param lvp Lower dc-link voltage protection handle. + * @param ts Ctrl period (s). + * @retval None. + */ +void LVP_Init(LVP_Handle *lvp, float ts) +{ + MCS_ASSERT_PARAM(lvp != NULL); + MCS_ASSERT_PARAM(ts > 0.0f); + + lvp->ts = ts; + LVP_Clear(lvp); + + lvp->protCntLimit = PROT_CNT_LIMIT; + lvp->recyCntLimit = LOWER_VOLT_RECY_CNT_LIMIT; + /* Configuring four levels of lower voltage protection thresholds. */ + lvp->protValThr[PROT_VAL_THRESHOLD_0] = PROT_LOWER_VOLT_POW_DN1; + lvp->protValThr[PROT_VAL_THRESHOLD_1] = PROT_LOWER_VOLT_POW_DN2; + lvp->protValThr[PROT_VAL_THRESHOLD_2] = PROT_LOWER_VOLT_POW_DN3; + lvp->protValThr[PROT_VAL_THRESHOLD_3] = PROT_LOWER_VOLT_POW_OFF; + /* Configure the protection limit time. */ + lvp->protLimitTime[PROT_LIMIT_TIME_0] = PROT_LOWER_VOLT_LIMIT1_TIME_SEC; + lvp->protLimitTime[PROT_LIMIT_TIME_1] = PROT_LOWER_VOLT_LIMIT2_TIME_SEC; + lvp->protLimitTime[PROT_LIMIT_TIME_2] = PROT_LOWER_VOLT_LIMIT3_TIME_SEC; + lvp->recyDelta = PROT_OVER_VOLT_RECY_DELTA; +} + +/** + * @brief Over dc-link voltage protection detection. + * @param ovp Over dc-link voltage protection handle. + * @param motorErrStatus Motor error status. + * @param udc DC-link voltage feedback (V). + * @retval None. + */ +void OVP_Det(OVP_Handle *ovp, MotorErrStatusReg *motorErrStatus, float udc) +{ + MCS_ASSERT_PARAM(ovp != NULL); + MCS_ASSERT_PARAM(motorErrStatus != NULL); + MCS_ASSERT_PARAM(udc > 0.0f); + /* Check if value goes over threshold for continuous cycles. */ + if (udc < ovp->protValThr[PROT_VAL_THRESHOLD_0]) { + ovp->protCnt = 0; + return; + } + + if (ovp->protCnt < ovp->protCntLimit) { + ovp->protCnt++; + return; + } + + /* Check which protection level should be activated. */ + /* Once enter error status, error status can only be changed by recover api. */ + /* protect level: level 4. */ + if (udc >= ovp->protValThr[PROT_VAL_THRESHOLD_3] && ovp->protLevel < PROT_LEVEL_4) { + ovp->protLevel = PROT_LEVEL_4; + motorErrStatus->Bit.overVoltErr = 1; + ovp->protCnt = 0; + return; + } + + /* Protect level: level 3. */ + if (udc >= ovp->protValThr[PROT_VAL_THRESHOLD_2] && ovp->protLevel < PROT_LEVEL_3) { + ovp->protLevel = PROT_LEVEL_3; + motorErrStatus->Bit.overVoltErr = 1; + ovp->protCnt = 0; + ovp->timer = 0.0f; + return; + } + + /* Protect level: level 2. */ + if (udc >= ovp->protValThr[PROT_VAL_THRESHOLD_1] && ovp->protLevel < PROT_LEVEL_2) { + ovp->protLevel = PROT_LEVEL_2; + motorErrStatus->Bit.overVoltErr = 1; + ovp->protCnt = 0; + ovp->timer = 0.0f; + return; + } + + /* Protect level: level 1. */ + if (udc >= ovp->protValThr[PROT_VAL_THRESHOLD_0] && ovp->protLevel < PROT_LEVEL_1) { + ovp->protLevel = PROT_LEVEL_1; + motorErrStatus->Bit.overVoltErr = 1; + ovp->protCnt = 0; + ovp->timer = 0.0f; + return; + } +} + +/** + * @brief Lower dc-link voltage protection detection. + * @param lvp Lower dc-link voltage protection handle. + * @param motorErrStatus Motor error status. + * @param udc DC-link voltage feedback (V). + * @retval None. + */ +void LVP_Det(LVP_Handle *lvp, MotorErrStatusReg *motorErrStatus, float udc) +{ + MCS_ASSERT_PARAM(lvp != NULL); + MCS_ASSERT_PARAM(motorErrStatus != NULL); + MCS_ASSERT_PARAM(udc > 0.0f); + /* Check if value goes over threshold for continuous cycles. */ + if (udc > lvp->protValThr[PROT_VAL_THRESHOLD_0]) { + lvp->protCnt = 0; + return; + } + + if (lvp->protCnt < lvp->protCntLimit) { + lvp->protCnt++; + return; + } + + /* Check which protection level should be activated. */ + /* Once enter error status, error status can only be changed by recover api. */ + /* protect level: level 4. */ + if (udc <= lvp->protValThr[PROT_VAL_THRESHOLD_3] && lvp->protLevel < PROT_LEVEL_4) { + lvp->protLevel = PROT_LEVEL_4; + motorErrStatus->Bit.lowerVoltErr = 1; + lvp->protCnt = 0; + return; + } + + /* Protect level: level 3. */ + if (udc <= lvp->protValThr[PROT_VAL_THRESHOLD_2] && lvp->protLevel < PROT_LEVEL_3) { + lvp->protLevel = PROT_LEVEL_3; + motorErrStatus->Bit.lowerVoltErr = 1; + lvp->protCnt = 0; + lvp->timer = 0.0f; + return; + } + + /* Protect level: level 2. */ + if (udc <= lvp->protValThr[PROT_VAL_THRESHOLD_1] && lvp->protLevel < PROT_LEVEL_2) { + lvp->protLevel = PROT_LEVEL_2; + motorErrStatus->Bit.lowerVoltErr = 1; + lvp->protCnt = 0; + lvp->timer = 0.0f; + return; + } + + /* Protect level: level 1. */ + if (udc <= lvp->protValThr[PROT_VAL_THRESHOLD_0] && lvp->protLevel < PROT_LEVEL_1) { + lvp->protLevel = PROT_LEVEL_1; + motorErrStatus->Bit.lowerVoltErr = 1; + lvp->protCnt = 0; + lvp->timer = 0.0f; + return; + } +} + +/** + * @brief Over dc-link voltage protection execution. + * @param ovp Over dc-link voltage protection handle. + * @param duty Brake loop output duty (0-1). + * @param aptAddr Three-phase APT address pointer. + * @retval None. + */ +void OVP_Exec(OVP_Handle *ovp, float *duty, APT_RegStruct **aptAddr) +{ + MCS_ASSERT_PARAM(ovp != NULL); + MCS_ASSERT_PARAM(duty != NULL); + MCS_ASSERT_PARAM(aptAddr != NULL); + /* According to protect level, take corresponding action. */ + switch (ovp->protLevel) { + /* level 4: brake loop duty maximum. */ + case PROT_LEVEL_4: + *duty = PROT_OVER_VOLT_BRK_DUTY4; + /* Disable three-phase pwm output. */ + ProtSpo_Exec(aptAddr); + break; + + /* level 3: brake loop duty level 3. */ + case PROT_LEVEL_3: + *duty = PROT_OVER_VOLT_BRK_DUTY2; + ovp->timer += ovp->ts; + if (ovp->timer > ovp->protLimitTime[PROT_LIMIT_TIME_2]) { + *duty = PROT_OVER_VOLT_BRK_DUTY3; + } + break; + + /* level 2: brake loop duty level 2. */ + case PROT_LEVEL_2: + *duty = PROT_OVER_VOLT_BRK_DUTY1; + ovp->timer += ovp->ts; + if (ovp->timer > ovp->protLimitTime[PROT_LIMIT_TIME_1]) { + *duty = PROT_OVER_VOLT_BRK_DUTY2; + } + break; + + /* level 1: brake loop duty level 1. */ + case PROT_LEVEL_1: + ovp->timer += ovp->ts; + if (ovp->timer > ovp->protLimitTime[PROT_LIMIT_TIME_0]) { + *duty = PROT_OVER_VOLT_BRK_DUTY1; + } + break; + + /* level 0: take no protection action. */ + case PROT_LEVEL_0: + break; + + default: + break; + } + return; +} + +/** + * @brief Lower dc-link voltage protection execution. + * @param lvp Lower dc-link voltage protection handle. + * @param spdRef Speed Reference (Hz). + * @param aptAddr Three-phase APT address pointer. + * @retval None. + */ +void LVP_Exec(LVP_Handle *lvp, float *spdRef, APT_RegStruct **aptAddr) +{ + MCS_ASSERT_PARAM(lvp != NULL); + MCS_ASSERT_PARAM(spdRef != NULL); + MCS_ASSERT_PARAM(aptAddr != NULL); + /* According to protect level, take corresponding action. */ + switch (lvp->protLevel) { + /* level 4: disable all PWM output. */ + case PROT_LEVEL_4: + /* Disable three-phase pwm output. */ + ProtSpo_Exec(aptAddr); + *spdRef *= 0.0f; + break; + + /* level 3: derate speed reference. */ + case PROT_LEVEL_3: + *spdRef *= PROT_POW_DN2_PCT; + lvp->timer += lvp->ts; + if (lvp->timer > lvp->protLimitTime[PROT_LIMIT_TIME_2]) { + *spdRef *= PROT_POW_DN3_PCT; + } + break; + + /* level 2: derate speed reference. */ + case PROT_LEVEL_2: + *spdRef *= PROT_POW_DN1_PCT; + lvp->timer += lvp->ts; + if (lvp->timer > lvp->protLimitTime[PROT_LIMIT_TIME_1]) { + *spdRef *= PROT_POW_DN2_PCT; + } + break; + + /* level 1: derate speed reference. */ + case PROT_LEVEL_1: + lvp->timer += lvp->ts; + if (lvp->timer > lvp->protLimitTime[PROT_LIMIT_TIME_0]) { + *spdRef *= PROT_POW_DN1_PCT; + } + break; + + /* level 0: take no protection action. */ + case PROT_LEVEL_0: + break; + + default: + break; + } + return; +} + +/** + * @brief Over dc-link voltage protection recovery. + * @param ovp Over dc-link voltage protection handle. + * @param motorErrStatus Motor error status. + * @param udc DC-link voltage (V). + * @retval None. + */ +void OVP_Recy(OVP_Handle *ovp, MotorErrStatusReg *motorErrStatus, float udc) +{ + MCS_ASSERT_PARAM(ovp != NULL); + /* If not under error state, just return without any operation. */ + if (!motorErrStatus->Bit.overVoltErr) { + return; + } + + /* According to protection level, take corresponding recovery action. */ + switch (ovp->protLevel) { + /* level 4 */ + case PROT_LEVEL_4: + if (udc < ovp->protValThr[PROT_VAL_THRESHOLD_3] - ovp->recyDelta) { + ovp->recyCnt++; + if (ovp->recyCnt > ovp->recyCntLimit) { + ovp->protLevel = PROT_LEVEL_3; + ovp->recyCnt = 0; + } + } + break; + + /* level 3 */ + case PROT_LEVEL_3: + /* If the dc-link voltage is less than threshold 2, level-2 protection is restored. */ + if (udc < ovp->protValThr[PROT_VAL_THRESHOLD_2] - ovp->recyDelta) { + ovp->recyCnt++; + if (ovp->recyCnt > ovp->recyCntLimit) { + ovp->protLevel = PROT_LEVEL_2; + ovp->recyCnt = 0; + } + } + break; + + /* level 2 */ + case PROT_LEVEL_2: + /* If the dc-link voltage is less than threshold 1, level-1 protection is restored. */ + if (udc < ovp->protValThr[PROT_VAL_THRESHOLD_1] - ovp->recyDelta) { + ovp->recyCnt++; + if (ovp->recyCnt > ovp->recyCntLimit) { + ovp->protLevel = PROT_LEVEL_1; + ovp->recyCnt = 0; + } + } + break; + + /* level 1 */ + case PROT_LEVEL_1: + /* If the dc-link voltage is less than threshold 0, level-0 protection is restored. */ + if (udc < ovp->protValThr[PROT_VAL_THRESHOLD_0] - ovp->recyDelta) { + ovp->recyCnt++; + if (ovp->recyCnt > ovp->recyCntLimit) { + ovp->protLevel = PROT_LEVEL_0; + ovp->recyCnt = 0; + } + } + break; + /* level 0 Fault-free state. */ + case PROT_LEVEL_0: + motorErrStatus->Bit.overVoltErr = 0; + break; + + default: + break; + } + return; +} + +/** + * @brief Lower dc-link voltage protection recovery. + * @param lvp Lower dc-link voltage protection handle. + * @param motorErrStatus Motor error status. + * @param udc DC-link voltage (V). + * @retval None. + */ +void LVP_Recy(LVP_Handle *lvp, MotorErrStatusReg *motorErrStatus, float udc) +{ + MCS_ASSERT_PARAM(lvp != NULL); + /* If not under error state, just return without any operation. */ + if (!motorErrStatus->Bit.lowerVoltErr) { + return; + } + + /* According to protection level, take corresponding recovery action. */ + switch (lvp->protLevel) { + /* level 4 */ + case PROT_LEVEL_4: + /* If the dc-link voltage is greater than threshold 3, level-3 protection is restored. */ + if (udc > lvp->protValThr[PROT_VAL_THRESHOLD_3] + lvp->recyDelta) { + lvp->recyCnt++; + if (lvp->recyCnt > lvp->recyCntLimit) { + lvp->protLevel = PROT_LEVEL_3; + lvp->recyCnt = 0; + } + } + break; + + /* level 3 */ + case PROT_LEVEL_3: + /* If the dc-link voltage is greater than threshold 2, level-2 protection is restored. */ + if (udc > lvp->protValThr[PROT_VAL_THRESHOLD_2] + lvp->recyDelta) { + lvp->recyCnt++; + if (lvp->recyCnt > lvp->recyCntLimit) { + lvp->protLevel = PROT_LEVEL_2; + lvp->recyCnt = 0; + } + } + break; + + /* level 2 */ + case PROT_LEVEL_2: + /* If the dc-link voltage is greater than threshold 1, level-1 protection is restored. */ + if (udc > lvp->protValThr[PROT_VAL_THRESHOLD_1] + lvp->recyDelta) { + lvp->recyCnt++; + if (lvp->recyCnt > lvp->recyCntLimit) { + lvp->protLevel = PROT_LEVEL_1; + lvp->recyCnt = 0; + } + } + break; + + /* level 1 */ + case PROT_LEVEL_1: + /* If the dc-link voltage is greater than threshold 0, level-0 protection is restored. */ + if (udc > lvp->protValThr[PROT_VAL_THRESHOLD_0] + lvp->recyDelta) { + lvp->recyCnt++; + if (lvp->recyCnt > lvp->recyCntLimit) { + lvp->protLevel = PROT_LEVEL_0; + lvp->recyCnt = 0; + } + } + break; + /* level 0 Fault-free state. */ + case PROT_LEVEL_0: + motorErrStatus->Bit.lowerVoltErr = 0; + break; + + default: + break; + } + return; +} + +/** + * @brief Over dc-link voltage protection error status clear. + * @param ovp Over voltage protection handle. + * @retval None. + */ +void OVP_Clear(OVP_Handle *ovp) +{ + MCS_ASSERT_PARAM(ovp != NULL); + /* Clear the history value. */ + ovp->protCnt = 0; + ovp->protLevel = PROT_LEVEL_0; + ovp->recyCnt = 0; + ovp->timer = 0.0f; +} + +/** + * @brief Lower dc-link voltage protection error status clear. + * @param lvp Lower voltage protection handle. + * @retval None. + */ +void LVP_Clear(LVP_Handle *lvp) +{ + MCS_ASSERT_PARAM(lvp != NULL); + /* Clear the history value. */ + lvp->protCnt = 0; + lvp->protLevel = PROT_LEVEL_0; + lvp->recyCnt = 0; + lvp->timer = 0.0f; +} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/mcs_dc_volt_prot.h b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/mcs_dc_volt_prot.h new file mode 100644 index 00000000..0435156a --- /dev/null +++ b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/mcs_dc_volt_prot.h @@ -0,0 +1,69 @@ +/** + * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_dc_volt_prot.h + * @author MCU Algorithm Team + * @brief This file contains dc-link voltage protection data struct and api declaration. + */ + +/* Define to prevent recursive inclusion ------------------------------------------------------- */ +#ifndef McuMagicTag_MCS_DC_VOLT_PROT_H +#define McuMagicTag_MCS_DC_VOLT_PROT_H + +/* Includes ------------------------------------------------------------------------------------ */ +#include "mcs_prot_cmm.h" +#include "apt_ip.h" + +typedef struct { + unsigned int protCnt; + unsigned int recyCnt; + unsigned int protCntLimit; + unsigned int recyCntLimit; + float protValThr[PROT_VAL_THRESHOLD_NUMS]; /* from low voltage level to high. */ + float protLimitTime[PROT_LIMIT_TIME_NUMS]; /* from level 3 to level 1. */ + float recyDelta; + float timer; + float ts; + PROT_Level protLevel; +} OVP_Handle; + +typedef struct { + unsigned int protCnt; + unsigned int recyCnt; + unsigned int protCntLimit; + unsigned int recyCntLimit; + float protValThr[PROT_VAL_THRESHOLD_NUMS]; /* from high voltage level to low. */ + float protLimitTime[PROT_LIMIT_TIME_NUMS]; /* from level 3 to level 1. */ + float recyDelta; + float timer; + float ts; + PROT_Level protLevel; +} LVP_Handle; + +void OVP_Init(OVP_Handle *ovp, float ts); +void OVP_Det(OVP_Handle *ovp, MotorErrStatusReg *motorErrStatus, float udc); +void OVP_Exec(OVP_Handle *ovp, float *duty, APT_RegStruct **aptAddr); +void OVP_Recy(OVP_Handle *ovp, MotorErrStatusReg *motorErrStatus, float udc); +void OVP_Clear(OVP_Handle *ovp); + +void LVP_Init(LVP_Handle *lvp, float ts); +void LVP_Det(LVP_Handle *lvp, MotorErrStatusReg *motorErrStatus, float udc); +void LVP_Exec(LVP_Handle *lvp, float *spdRef, APT_RegStruct **aptAddr); +void LVP_Recy(LVP_Handle *lvp, MotorErrStatusReg *motorErrStatus, float udc); +void LVP_Clear(LVP_Handle *lvp); + +#endif diff --git a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/mcs_motor_stalling.c b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/mcs_motor_stalling.c new file mode 100644 index 00000000..9dee4d2d --- /dev/null +++ b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/mcs_motor_stalling.c @@ -0,0 +1,112 @@ +/** + * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_motor_stalling.c + * @author MCU Algorithm Team + * @brief This file contains motor stalling protection data struct and api definition. + */ + +/* Includes ------------------------------------------------------------------------------------ */ +#include "mcs_motor_stalling.h" +#include "mcs_prot_user_config.h" +#include "mcs_math.h" +#include "mcs_math_const.h" +#include "mcs_assert.h" + +/** + * @brief Initilization motor stalling protection function. + * @param stall Motor stalling handle. + * @param ts Ctrl period (s). + * @param currLimit The current amplitude that triggers fault. (A). + * @param spdLimit The speed amplitude that triggers fault. (Hz). + * @param timeLimit The threshold time that current amplitude over the limit (s). + * @retval None. + */ +void STP_Init(STP_Handle *stall, float ts, float currLimit, float spdLimit, float timeLimit) +{ + MCS_ASSERT_PARAM(stall != NULL); + MCS_ASSERT_PARAM(ts > 0.0f); + MCS_ASSERT_PARAM(currLimit > 0.0f); + MCS_ASSERT_PARAM(spdLimit > 0.0f); + MCS_ASSERT_PARAM(timeLimit > 0.0f); + /* Configuring parameters for stalling detection. */ + stall->ts = ts; + /* Current threshold and speed threshold for stalling fault. */ + stall->currAmpLimit = currLimit; + stall->spdLimit = spdLimit; + stall->timeLimit = timeLimit; + stall->timer = 0.0f; +} + +/** + * @brief Motor stalling detection. + * @param stall Motor stalling handle. + * @param motorErrStatus Motor error status. + * @param spd Speed feedback (Hz). + * @param idq Dq-axis current feedback (A). + * @retval None. + */ +void STP_Det_ByCurrSpd(STP_Handle *stall, MotorErrStatusReg *motorErrStatus, float spd, DqAxis idq) +{ + MCS_ASSERT_PARAM(stall != NULL); + MCS_ASSERT_PARAM(motorErrStatus != NULL); + MCS_ASSERT_PARAM(Abs(spd) >= 0.0f); + /* Calculate current amplitude. */ + float currAmp = Sqrt(idq.d * idq.d + idq.q * idq.q); + float spdAbs = Abs(spd); + /* Check if value goes over threshold for continuous cycles. */ + if (currAmp < stall->currAmpLimit || spdAbs > stall->spdLimit) { + stall->timer = 0.0f; + return; + } + /* Time accumulation. */ + if (stall->timer < stall->timeLimit) { + stall->timer += stall->ts; + return; + } + motorErrStatus->Bit.motorStalling = 1; +} + +/** + * @brief Motor stalling protection execution. + * @param motorErrStatus Motor error status. + * @param aptAddr Three-phase APT address pointer. + * @retval None. + */ +void STP_Exec(MotorErrStatusReg *motorErrStatus, APT_RegStruct **aptAddr) +{ + MCS_ASSERT_PARAM(motorErrStatus != NULL); + MCS_ASSERT_PARAM(aptAddr != NULL); + if (motorErrStatus->Bit.motorStalling == 0) { + return; + } + + /* Disable three-phase pwm output. */ + ProtSpo_Exec(aptAddr); + return; +} + +/** + * @brief Motor stalling protection error status clear. + * @param stall Motor stalling handle. + * @retval None. + */ +void STP_Clear(STP_Handle *stall) +{ + MCS_ASSERT_PARAM(stall != NULL); + stall->timer = 0.0f; +} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/mcs_motor_stalling.h b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/mcs_motor_stalling.h new file mode 100644 index 00000000..a1d1e4be --- /dev/null +++ b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/mcs_motor_stalling.h @@ -0,0 +1,45 @@ +/** + * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_motor_stalling.h + * @author MCU Algorithm Team + * @brief This file contains motor stalling protection data struct and api declaration. + */ + +/* Define to prevent recursive inclusion ------------------------------------- */ +#ifndef McuMagicTag_MCS_MOTOR_STALLING_PROT_H +#define McuMagicTag_MCS_MOTOR_STALLING_PROT_H + +/* Includes ------------------------------------------------------------------------------------ */ +#include "mcs_prot_cmm.h" +#include "apt_ip.h" +#include "mcs_typedef.h" + +typedef struct { + float currAmpLimit; /**< Feedback current higher than this value triggers fault. (A). */ + float spdLimit; /**< Feedback speed lower than this value triggers fault (Hz). */ + float timeLimit; /**< The threshold time that current and speed feedback over ranges (s). */ + float timer; /**< Timer to get speed and current over range time. */ + float ts; /**< Ctrl period (s). */ +} STP_Handle; + +void STP_Init(STP_Handle *stall, float ts, float currLimit, float spdLimit, float timeLimit); +void STP_Det_ByCurrSpd(STP_Handle *stall, MotorErrStatusReg *motorErrStatus, float spd, DqAxis idq); +void STP_Exec(MotorErrStatusReg *motorErrStatus, APT_RegStruct **aptAddr); +void STP_Clear(STP_Handle *stall); + +#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/mcs_temp_prot.c b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/mcs_temp_prot.c new file mode 100644 index 00000000..184ba4a3 --- /dev/null +++ b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/mcs_temp_prot.c @@ -0,0 +1,262 @@ +/** + * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_temp_prot.c + * @author MCU Algorithm Team + * @brief This file contains over temperature protection api definition. + */ + +/* Includes ------------------------------------------------------------------------------------ */ +#include "mcs_temp_prot.h" +#include "mcs_prot_user_config.h" +#include "mcs_assert.h" + +/** + * @brief Initilization over temperation protection function. + * @param otp Over temperature protection handle. + * @param ts Ctrl period. + * @retval None. + */ +void OTP_Init(OTP_Handle *otp, float ts) +{ + MCS_ASSERT_PARAM(otp != NULL); + MCS_ASSERT_PARAM(ts > 0.0f); + otp->ts = ts; + otp->protCntLimit = PROT_CNT_LIMIT; + otp->recyCntLimit = RECY_CNT_LIMIT; + /* Configuring the temperature protection threshold. */ + otp->protValThr[PROT_VAL_THRESHOLD_0] = PROT_OVER_IPM_TEMP_POW_DN1; + otp->protValThr[PROT_VAL_THRESHOLD_1] = PROT_OVER_IPM_TEMP_POW_DN2; + otp->protValThr[PROT_VAL_THRESHOLD_2] = PROT_OVER_IPM_TEMP_POW_DN3; + otp->protValThr[PROT_VAL_THRESHOLD_3] = PROT_OVER_IPM_TEMP_POW_OFF; + /* Configuring the protection limiting time. */ + otp->protLimitTime[PROT_LIMIT_TIME_0] = PROT_OVER_TEMP_LIMIT1_TIME_SEC; + otp->protLimitTime[PROT_LIMIT_TIME_1] = PROT_OVER_TEMP_LIMIT2_TIME_SEC; + otp->protLimitTime[PROT_LIMIT_TIME_2] = PROT_OVER_TEMP_LIMIT3_TIME_SEC; + otp->recyDelta = PROT_OVER_IPM_TEMP_RECY_DELTA; + OTP_Clear(otp); +} + +/** + * @brief Over temperatre protection detection. + * @param otp Over temperature protection handle. + * @param tempErrBit Temperature error status bit. + * @param idq DQ-axis feedback currents. + * @retval None. + */ +void OTP_Det(OTP_Handle *otp, MotorErrStatusReg *motorErrStatus, PROT_ErrBit protBit, float temp) +{ + MCS_ASSERT_PARAM(otp != NULL); + MCS_ASSERT_PARAM(motorErrStatus != NULL); + MCS_ASSERT_PARAM(temp > 0.0f); + /* Check if value goes over threshold for continuous cycles. */ + if (temp < otp->protValThr[PROT_VAL_THRESHOLD_0]) { + otp->protCnt = 0; + ClearBit(&motorErrStatus->all, protBit); + return; + } + + if (otp->protCnt < otp->protCntLimit) { + otp->protCnt++; + return; + } + + /* Check which protection level should be activated. */ + /* Once enter error status, error status can only be changed by recover api. */ + /* protect level: level 4. */ + if (temp >= otp->protValThr[PROT_VAL_THRESHOLD_3] && otp->protLevel < PROT_LEVEL_4) { + otp->protLevel = PROT_LEVEL_4; + SetBit(&motorErrStatus->all, protBit); + otp->protCnt = 0; + return; + } + + /* Protect level: level 3. */ + if (temp >= otp->protValThr[PROT_VAL_THRESHOLD_2] && otp->protLevel < PROT_LEVEL_3) { + otp->protLevel = PROT_LEVEL_3; + SetBit(&motorErrStatus->all, protBit); + otp->protCnt = 0; + otp->timer = 0.0f; + return; + } + + /* Protect level: level 2. */ + if (temp >= otp->protValThr[PROT_VAL_THRESHOLD_1] && otp->protLevel < PROT_LEVEL_2) { + otp->protLevel = PROT_LEVEL_2; + SetBit(&motorErrStatus->all, protBit); + otp->protCnt = 0; + otp->timer = 0.0f; + return; + } + + /* Protect level: level 1. */ + if (temp >= otp->protValThr[PROT_VAL_THRESHOLD_0] && otp->protLevel < PROT_LEVEL_1) { + otp->protLevel = PROT_LEVEL_1; + SetBit(&motorErrStatus->all, protBit); + otp->protCnt = 0; + otp->timer = 0.0f; + return; + } +} + +/** + * @brief Over temperature protection execution. + * @param otp Over temperature protection handle. + * @param spdRef Speed reference (Hz). + * @param aptAddr Three-phase APT address pointer. + * @retval None. + */ +void OTP_Exec(OTP_Handle *otp, float *spdRef, APT_RegStruct **aptAddr) +{ + MCS_ASSERT_PARAM(otp != NULL); + MCS_ASSERT_PARAM(spdRef != NULL); + MCS_ASSERT_PARAM(aptAddr != NULL); + /* According to protect level, take corresponding action. */ + switch (otp->protLevel) { + /* level 4: disable all PWM output. */ + case PROT_LEVEL_4: + /* Disable three-phase pwm output. */ + ProtSpo_Exec(aptAddr); + *spdRef *= 0.0f; + break; + + /* level 3: derate speed reference. */ + case PROT_LEVEL_3: + *spdRef *= PROT_POW_DN2_PCT; + otp->timer += otp->ts; + if (otp->timer > otp->protLimitTime[PROT_LIMIT_TIME_2]) { + *spdRef *= PROT_POW_DN3_PCT; + } + break; + + /* level 2: derate speed reference. */ + case PROT_LEVEL_2: + /* Reducte motor speed to level 2 */ + *spdRef *= PROT_POW_DN1_PCT; + otp->timer += otp->ts; + if (otp->timer > otp->protLimitTime[PROT_LIMIT_TIME_1]) { + *spdRef *= PROT_POW_DN2_PCT; + } + break; + + /* level 0: take no protection action. */ + case PROT_LEVEL_0: + break; + + case PROT_LEVEL_1: + /* Reducte motor speed to level 0 */ + otp->timer += otp->ts; + if (otp->timer > otp->protLimitTime[PROT_LIMIT_TIME_0]) { + /* level 1: derate speed reference. */ + *spdRef *= PROT_POW_DN1_PCT; + } + break; + + default: + break; + } + return; +} + +/** + * @brief Over temperature protection recovery. + * @param otp Over temperature protection handle. + * @param tempErrBit Temperature error status bit. + * @param temp Temperature (celsius). + * @retval None. + */ +void OTP_Recy(OTP_Handle *otp, MotorErrStatusReg *motorErrStatus, PROT_ErrBit protBit, float temp) +{ + MCS_ASSERT_PARAM(otp != NULL); + MCS_ASSERT_PARAM(motorErrStatus != NULL); + MCS_ASSERT_PARAM(temp > 0.0f); + /* If not under error state, just return without any operation. */ + if (otp->protLevel == PROT_LEVEL_0) { + motorErrStatus->all &= (~(1 >> protBit)); + return; + } + + /* According to protection level, take corresponding recovery action. */ + switch (otp->protLevel) { + /* level 4 */ + case PROT_LEVEL_4: + /* If the temperature is less than threshold 3, level-3 protection is restored. */ + if (temp < otp->protValThr[PROT_VAL_THRESHOLD_3] - otp->recyDelta) { + otp->recyCnt++; + if (otp->recyCnt > otp->recyCntLimit) { + otp->protLevel = PROT_LEVEL_3; + otp->recyCnt = 0; + } + } + break; + + /* level 3 */ + case PROT_LEVEL_3: + /* If the temperature is less than threshold 2, level-2 protection is restored. */ + if (temp < otp->protValThr[PROT_VAL_THRESHOLD_2] - otp->recyDelta) { + otp->recyCnt++; + if (otp->recyCnt > otp->recyCntLimit) { + otp->protLevel = PROT_LEVEL_2; + otp->recyCnt = 0; + } + } + break; + + /* level 2 */ + case PROT_LEVEL_2: + /* If the temperature is less than threshold 1, level-1 protection is restored. */ + if (temp < otp->protValThr[PROT_VAL_THRESHOLD_1] - otp->recyDelta) { + otp->recyCnt++; + if (otp->recyCnt > otp->recyCntLimit) { + otp->protLevel = PROT_LEVEL_1; + otp->recyCnt = 0; + } + } + break; + + /* level 1 */ + case PROT_LEVEL_1: + /* If the temperature is less than threshold 0, level-0 protection is restored. */ + if (temp < otp->protValThr[PROT_VAL_THRESHOLD_0] - otp->recyDelta) { + otp->recyCnt++; + if (otp->recyCnt > otp->recyCntLimit) { + otp->protLevel = PROT_LEVEL_0; + otp->recyCnt = 0; + } + } + break; + + default: + break; + } + return; +} + +/** + * @brief Over temperature protection error status clear. + * @param otp Over temperature protection handle. + * @retval None. + */ +void OTP_Clear(OTP_Handle *otp) +{ + MCS_ASSERT_PARAM(otp != NULL); + /* Clear the history value. */ + otp->protCnt = 0; + otp->protLevel = PROT_LEVEL_0; + otp->recyCnt = 0; + otp->timer = 0.0f; +} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/mcs_temp_prot.h b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/mcs_temp_prot.h new file mode 100644 index 00000000..2b9d99a7 --- /dev/null +++ b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/mcs_temp_prot.h @@ -0,0 +1,49 @@ +/** + * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_temp_prot.h + * @author MCU Algorithm Team + * @brief his file contains over temperature protection api declaration. + */ + +/* Define to prevent recursive inclusion ------------------------------------- */ +#ifndef McuMagicTag_MCS_TEMP_PROT_H +#define McuMagicTag_MCS_TEMP_PROT_H + +/* Includes ------------------------------------------------------------------------------------ */ +#include "mcs_prot_cmm.h" +#include "apt_ip.h" + +typedef struct { + unsigned int protCnt; + unsigned int recyCnt; + unsigned int protCntLimit; + unsigned int recyCntLimit; + float protValThr[PROT_VAL_THRESHOLD_NUMS]; /* from low current level to high. */ + float protLimitTime[PROT_LIMIT_TIME_NUMS]; /* from level 3 to level 1. */ + float recyDelta; + float ts; + float timer; + PROT_Level protLevel; +} OTP_Handle; + +void OTP_Init(OTP_Handle *otp, float ts); +void OTP_Det(OTP_Handle *otp, MotorErrStatusReg *motorErrStatus, PROT_ErrBit protBit, float temp); +void OTP_Exec(OTP_Handle *otp, float *spdRef, APT_RegStruct **aptAddr); +void OTP_Recy(OTP_Handle *otp, MotorErrStatusReg *motorErrStatus, PROT_ErrBit protBit, float temp); +void OTP_Clear(OTP_Handle *otp); +#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/readme.md b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/readme.md new file mode 100644 index 00000000..592524f0 --- /dev/null +++ b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/readme.md @@ -0,0 +1,12 @@ +# pmsm_encode_qdm_2shunt_foc + +**【功能描述】** ++ 基于ECMCU105H/ECBMCU201MPC单板的单电机双电阻采样的有感Foc应用 + +**【环境要求】** ++ 所有单板电源改制为演示用的24V低压,电机选用42JSF630AS-1000 ++ 电机ABZ编码器线序分别对应功率板上ABZ通道接口 ++ 电机(绿蓝黄)相线分别对应功率板上U/V/W通道接口 + +**【IDE配置方法】** ++ chipConfig中的Sample栏目里面选中pmsm encode qdm 2shunt foc示例,然后点击生成代码即可 diff --git a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/src/mcs_carrier.c b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/src/mcs_carrier.c new file mode 100644 index 00000000..4f8c9d37 --- /dev/null +++ b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/src/mcs_carrier.c @@ -0,0 +1,136 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_carrier.c + * @author MCU Algorithm Team + * @brief This file provides carrier interrupt application for motor control. + */ + +#include "mcs_carrier.h" +#include "mcs_math.h" +#include "typedefs.h" +#include "mcs_assert.h" +#include "mcs_user_config.h" +#include "mcs_ctlmode_config.h" + +/** + * @brief 同步旋转坐标系角度。 + * @param mtrCtrl The motor control handle. + * @retval None. + */ +static void MCS_SyncCoorAngle(MTRCTRL_Handle *mtrCtrl) +{ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + /* Synchronous rotation coordinate system angle. */ + switch (mtrCtrl->stateMachine) { + case FSM_STARTUP: + /* 当前斜坡角度为0。 */ + if (mtrCtrl->startup.stage == STARTUP_STAGE_CURR) { + mtrCtrl->axisAngle = 0; + } else if (mtrCtrl->startup.stage == STARTUP_STAGE_SPD) { /* 中频控制相位角自加。 */ + mtrCtrl->axisAngle = IF_CurrAngleCalc(&mtrCtrl->ifCtrl, mtrCtrl->spdRefHz); + } + break; + + case FSM_RUN: + mtrCtrl->axisAngle = mtrCtrl->encAxisAngle; + break; + + default: + mtrCtrl->axisAngle = 0; + break; + } +} + +/** + * @brief 单电阻器和双电阻器的PWM波形设置和采样点设置。 + * @param mtrCtrl The motor control handle. + * @retval None. + */ +static void MCS_PwmAdcSet(MTRCTRL_Handle *mtrCtrl) +{ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + SampleMode sampleMode = mtrCtrl->sampleMode; + /* 根据采样模式设置占空比. */ + if (sampleMode == DUAL_RESISTORS) { + //三相上开关PWM波的占空比为在两相静止坐标系(albe)中计算。 + SVPWM_Exec(&mtrCtrl->sv, &mtrCtrl->vabRef, &mtrCtrl->dutyUvw); + mtrCtrl->setPwmDutyCb(&mtrCtrl->dutyUvw, &mtrCtrl->dutyUvw); + } else if (sampleMode == SINGLE_RESISTOR) { + R1SVPWM_Exec(&mtrCtrl->r1Sv, &mtrCtrl->vabRef, &mtrCtrl->dutyUvwLeft, &mtrCtrl->dutyUvwRight); + mtrCtrl->setPwmDutyCb(&mtrCtrl->dutyUvwLeft, &mtrCtrl->dutyUvwRight); + /* ADC采样点位置需要根据单个电阻器的相移进行设置。*/ + mtrCtrl->setADCTriggerTimeCb(mtrCtrl->r1Sv.samplePoint[0] * mtrCtrl->aptMaxcntCmp, \ + mtrCtrl->r1Sv.samplePoint[1] * mtrCtrl->aptMaxcntCmp); + } +} + +/** + * @brief 载波中断功能。 + * @param mtrCtrl The motor control handle. + * @retval None. + * 电流环 + */ +void MCS_CarrierProcess(MTRCTRL_Handle *mtrCtrl) +{ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + UvwAxis *currUvw = &mtrCtrl->currUvw; + AlbeAxis *currAlbe = &mtrCtrl->iabFbk; + AlbeAxis *vab = &mtrCtrl->vabRef; + SampleMode sampleMode = mtrCtrl->sampleMode; + /* 采样模式验证 */ + if (sampleMode > SINGLE_RESISTOR || mtrCtrl->setADCTriggerTimeCb == NULL) { + return; + } + /* 参数验证 */ + if (mtrCtrl->setPwmDutyCb == NULL || mtrCtrl->readCurrUvwCb == NULL) { + return; + } + /* 读取三相电流值。 */ + mtrCtrl->readCurrUvwCb(currUvw); + mtrCtrl->getEncAngSpd(&mtrCtrl->encSpeed, &mtrCtrl->encAxisAngle);//读取反馈速度与角度 + /* 同步角度 */ + MCS_SyncCoorAngle(mtrCtrl); + + /* 克拉克变化 UVW->A、B */ + ClarkeCalc(currUvw, currAlbe); + /* 帕克变化 A、B +角度->q、d */ + ParkCalc(currAlbe, mtrCtrl->axisAngle, &mtrCtrl->idqFbk); + + /* 状态机 */ + switch (mtrCtrl->stateMachine) { + case FSM_STARTUP: + case FSM_RUN: + CURRCTRL_Exec(&mtrCtrl->currCtrl, &mtrCtrl->vdqRef, mtrCtrl->smo.spdEst, 0); + //帕克逆变化 ,将d,q轴电流+角度逆变换为α,β。 + InvParkCalc(&mtrCtrl->vdqRef, mtrCtrl->axisAngle, vab); + //单电阻器和双电阻器的PWM波形设置和采样点设置。 + MCS_PwmAdcSet(mtrCtrl); + break; + + case FSM_CAP_CHARGE: + case FSM_CLEAR: + case FSM_IDLE: + break; + + default: + vab->alpha = 0.0f; + vab->beta = 0.0f; + MCS_PwmAdcSet(mtrCtrl); + break; + } +} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/src/mcs_inc_enc.c b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/src/mcs_inc_enc.c new file mode 100644 index 00000000..9769c9b5 --- /dev/null +++ b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/src/mcs_inc_enc.c @@ -0,0 +1,236 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_incremental_enc.c + * @author MCU Algorithm Team + * @brief This file provides function of QDM module and encoder speed angle calculation. + */ +#include "mcs_inc_enc.h" +#include "crg.h" +#include "debug.h" +#include "mcs_math_const.h" + +#define FILTER_TIME_A 100 /* Unit: clock cycles */ +#define FILTER_TIME_B 100 /* Unit: clock cycles */ +#define FILTER_TIME_Z 100 /* Unit: clock cycles */ +#define ENC_SPEED_BUF_SIZE 10 +#define ENC_HALF_PPR 5000u +#define ENC_MAX_POS 65536u +/** + * @brief QMD Initialization. + * @param qdmInit MCS_QdmInitStru. + * @retval None. + */ +void MCS_QdmInit(MCS_QdmInitStru *qdmInit) +{ + QDM_RegStruct *qdm = qdmInit->qdmAddr; + + HAL_CRG_IpEnableSet(qdmInit->qdmAddr, CRG_ENABLE_VALUE); + + /* Internal counters are not affected by emulator pause CPU */ + qdm->QEMUMODE.BIT.emu_mode = QDM_EMULATION_MODE_RUN_FREE; + + /* Quadrature input decoding mode. Inputs are quadrature encoder A, B, Z phase signals. */ + qdm->QCTRL.BIT.qdu_mode = QDM_QUADRATURE_COUNT; + +/** + * This parameter is valid only when the quadrature count mode is + * used. In other modes, the frequency is multiplied by 2x. + */ + qdm->QCTRL.BIT.qdu_xclk = QDM_4X_RESOLUTION; + + /* xCLK-xDIRCount mode. */ + qdm->QPPUCTRL.BIT.pcnt_mode = QDM_PPU_COUNT_MODE_CLK_DIR; + + /* QDM position counter reset select: overflow reset (upflow, underflow) */ + qdm->QPPUCTRL.BIT.pcnt_rst_mode = QDM_POSITION_RESET_MAX_POS; + + /* QDM initialization position counter mode: disabled */ + qdm->QPPUCTRL.BIT.pcnt_idx_init_mode = QDM_POSITION_INIT_DO_NOTHING; + + /* QDM Index latch mode select: latch at the rising edge of index */ + qdm->QPPUCTRL.BIT.pcnt_idx_lock_mode = QDM_LOCK_RISING_INDEX; + + qdm->QPOSMAX = 0xFFFF; /* QDM Position Count Max */ + + /* ABZ filtering time */ + qdm->QDMAFT.BIT.qdma_ft_level = FILTER_TIME_A; + qdm->QDMBFT.BIT.qdmb_ft_level = FILTER_TIME_B; + qdm->QDMIFT.BIT.qdmi_ft_level = FILTER_TIME_Z; + +/** + * QDM Position count value. When ppu_en is disabled or counting stops after the emulator is + * connected, software can write QPOSCNT. + */ + qdm->QPOSCNT = 0; + + /* QDM Position processing unit PPU enable 1: PPU position counter starts counting */ + qdm->QCTRL.BIT.ppu_en = BASE_CFG_ENABLE; + + /* Enable z-pulse interrupt */ + if (qdmInit->zPlusesIrqFunc != NULL && qdmInit->zPlusesNvic->baseAddr != NULL) { + IRQ_SetPriority(qdmInit->zPlusesNvic->irqNum, qdmInit->zPlusesIrqPrio); + IRQ_Register(qdmInit->zPlusesNvic->irqNum, qdmInit->zPlusesIrqFunc, qdmInit->zPlusesNvic); + DCL_QDM_EnableInterrupt(qdm, QDM_INT_INDEX_EVNT_LATCH); + IRQ_EnableN(qdmInit->zPlusesNvic->irqNum); + } +} + +/** + * @brief Get the Encoder Cnt object. + * @param enc encoder handle. + * @param qdm QDM_RegStruct. + * @retval None. + */ +void MCS_GetEncoderCnt(EncoderHandle *enc, QDM_RegStruct *qdm) +{ + signed short tmpS16; + + enc->cntNow = qdm->QPOSCNT; + /* Get pulse conut in unit period */ + tmpS16 = (signed short)(enc->cntNow - enc->cntPre); + + enc->pulsePos += tmpS16; /* Total number of pulses since power-on. */ + enc->pulseAngle = (unsigned short)qdm->QPOSCNT; + + enc->cntPre = enc->cntNow; +} + +/** + * @brief Get the Elec Angle By Enc object. + * @param enc Encoder handle. + * @retval signed short elecAngle Angle in electronic, S16degree. + */ +void MCS_GetElecAngleByEnc(EncoderHandle *enc) +{ + signed int tmpS32; + + tmpS32 = enc->pulseAngle - enc->pulZCnt; + /* Align electric angle & enc angle */ + tmpS32 += enc->zShift; + /* Limit max value */ + if (tmpS32 > INT16_MAX) { + tmpS32 -= ENC_MAX_POS; + } + /* Limit min value */ + if (tmpS32 < -INT16_MAX) { + tmpS32 += ENC_MAX_POS; + } + while (tmpS32 >= enc->pulsePerElecRound) { + tmpS32 -= enc->pulsePerElecRound; + } + while (tmpS32 < 0) { + tmpS32 += enc->pulsePerElecRound; + } + float tempElecAngle = (signed short)(tmpS32 * enc->pulseToElecAngle); + /* Convert short to float angle type */ + enc->elecAngle = tempElecAngle * DIGITAL_TO_RAD; +} + +/** + * @brief Get the Elec Speed By Enc object. + * @param enc Encoder handle. + * @retval None. + */ +void MCS_GetElecSpeedByEnc(EncoderHandle *enc) +{ + signed int tmpS32; + signed short halfPPR = ENC_HALF_PPR; + unsigned short index = enc->speedBufIndex; + + enc->timeCnt++; + if (enc->timeCnt >= enc->timeNum) { + enc->timeCnt = 0; + } else { + return; + } + + /* Get pulse conut in unit period */ + tmpS32 = enc->pulsePos - enc->speedBuf[index]; + /* Limit max value */ + if (tmpS32 > halfPPR) { + tmpS32 -= ENC_MAX_POS; + } + /* Limit min value */ + if (tmpS32 < -halfPPR) { + tmpS32 += ENC_MAX_POS; + } + /* Convert unit pulse count to Hz */ + enc->elecSpeed = tmpS32 * enc->pulseToHzPu; + enc->speedBuf[index] = enc->pulsePos; + index++; + /* Store current speed for calcule the average */ + if (index >= enc->speedBufSize) { + enc->speedBufIndex = 0; + } else { + enc->speedBufIndex = index; + } +} + +/** + * @brief Clear the historical cache and time count. + * @param enc Encoder handle. + * @retval None. + */ +void MCS_EncoderClear(EncoderHandle *enc) +{ + enc->speedBufIndex = 0; + for (unsigned short i = 0; i < enc->speedBufSize; i++) { + enc->speedBuf[i] = enc->pulsePos; + } + + enc->timeCnt = 0; +} + +/** + * @brief Initialzer of encoder struct handle. + * @param enc Encoder handle. + * @param encParam Encoder and Motor parameter. + * @retval None. + */ +void MCS_EncoderInit(EncoderHandle *enc, MCS_EncInitStru *encParam) +{ + if (encParam->mtrPPMR == 0 || encParam->mtrNp == 0) { + return; + } + + enc->pulsePerMechRound = encParam->mtrPPMR; + enc->pulsePerElecRound = encParam->mtrPPMR / encParam->mtrNp; + enc->zShift = encParam->zShift; + /* Clear count value */ + enc->cntNow = 0; + enc->cntPre = 0; + + enc->pulsePos = 0; + enc->pulseAngle = 0; + enc->elecAngle = 0; + /* Initial speed buffer */ + enc->speedBufSize = ENC_SPEED_BUF_SIZE; /* Max = 10 */ + enc->speedBufIndex = 0; + for (unsigned short i = 0; i < enc->speedBufSize; i++) { + enc->speedBuf[i] = enc->pulsePos; + } + /* Clear time count value */ + enc->timeCnt = 0; + enc->timeNum = encParam->timeNum; + enc->elecSpeed = 0.0f; /* Hz */ + /* Convert unit pulse count to Hz */ + enc->pulseToHzPu = (float)(1.0f / + (enc->pulsePerElecRound * enc->timeNum * enc->speedBufSize * encParam->ctrlPeriod)); + /* Convert unit pulse count to electric angle */ + enc->pulseToElecAngle = (float)(65536.0f / enc->pulsePerElecRound); +} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/src/mcs_motor_process.c b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/src/mcs_motor_process.c new file mode 100644 index 00000000..6626f9a4 --- /dev/null +++ b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/src/mcs_motor_process.c @@ -0,0 +1,1047 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file mcs_motor_process.c + * @author MCU Algorithm Team + * @brief This file provides motor application. + * @details Single FOC application based on the ECMCU105H/ECBMCU201MPC board + * 1) Motor model is 42JSF630AS-1000. + * 2) Select the pmsm encode qdm 2shunt foc example in the sample column + of chipConfig and click Generate Code. + * 3) It's power supply must be changed to 24V. + */ +#include "main.h" +#include "mcs_user_config.h" +#include "mcs_math.h" +#include "hmi_module.h" +#include "mcs_ctlmode_config.h" +#include "mcs_prot_user.h" +#include "mcs_prot_user_config.h" +#include "mcs_math_const.h" +#include "mcs_motor_process.h" +#include "mcs_chip_config.h" +#include "mcs_inc_enc.h" +#include +#include "debug.h" + + +/*------------------------------- Macro Definition -----------------------------------------------*/ +#define US_PER_MS 1000 +#define ANGLE_RANGE_ABS 65536 +#define ANGLE_360_F 65536.0f /* 0 - 65536 indicates 0 to 360. */ +#define APT_FULL_DUTY 1.0f +#define TEMP_3 3.0f +#define TEMP_15 15.0f +#define TEMP_30 30.0f +#define TEMP_45 45.0f +#define TEMP_60 60.0f +#define TEMP_RES_15 78.327f +#define TEMP_RES_30 36.776f +#define TEMP_RES_45 18.301f +#define TEMP_RES_60 9.607f +#define CNT_10 10 +#define CNT_5000 5000 +#define LEVEL_4 4 +#define MOTOR_START_DELAY 2 +#define ADC_READINIT_DELAY 1 +#define ADC_READINIT_TIMES 20 +#define ADC_TRIMVALUE_MIN 1800.0f +#define ADC_TRIMVALUE_MAX 2200.0f +#define IRQ_QDM0_PRIORITY 7 /* the QDM encoder IRQ priority, highest */ +#define ENC_TIMES_NUM 5 +/*------------------------------- Param Definition -----------------------------------------------*/ +/* Motor parameters. */ +/* Np, Rs, Ld, Lq, Psif, J, Nmax, Currmax, PPMR, zShift */ +static MOTOR_Param g_motorParam = MOTORPARAM_DEFAULTS; +static APT_RegStruct* g_apt[PHASE_MAX_NUM] = {APT_U, APT_V, APT_W}; + +extern __UINT16_TYPE__ last_raw_Angle; +/* QDM */ +typedef struct { + MCS_QdmHandle qdm; +} PeriphHandle; +static PeriphHandle g_periph = {.qdm = {.qdmAddr = QDMBASEADDR, + .zPulsesNvic = {.irqNum = QDMIRQNUM, + .baseAddr = QDMBASEADDR}}}; +/* Motor control handle */ +static MTRCTRL_Handle g_mc = {0}; +/* QDM control handle */ +static EncoderHandle g_enc = {0}; + + + + +/** + * @brief 获取编码器的角度和速度。 + * @param speed 速度指针。 + * @param angle 角度指针。 + * @retval 无。 + */ + +static void GetEncAngSpd(float* speed, float* angle) +{ + MCS_GetEncoderCnt(&g_enc, QDMNUM);// 获取编码器计数值 + // /* 计算电机电角度 -π ~ π */ + MCS_GetElecAngleByEnc(&g_enc); // 根据编码器数据计算电机电角度 + // /* 计算电机速度 */ + MCS_GetElecSpeedByEnc(&g_enc); // 根据编码器数据计算电机速度 + *speed = g_enc.elecSpeed; // 将计算得到的电机速度赋值给传入的速度指针 + *angle = g_enc.elecAngle; // 将计算得到的电机电角度赋值给传入的角度指针 + + + + +} + + +static void ISR_QdmzPulses(void* args) +{ + NvicHandle* qdmHandle = (NvicHandle*)args; // 将传入的参数转换为NvicHandle指针 + /* 清除QDM中断标志 */ + DCL_QDM_ClearInterrupt((QDM_RegStruct*)qdmHandle->baseAddr, QDM_INT_INDEX_EVNT_LATCH); // 清除QDM事件捕获中断标志 + IRQ_ClearN(qdmHandle->irqNum); // 清除中断请求 + + g_enc.pulZCnt = MCS_GetQdmPosCnt(qdmHandle->baseAddr); + g_mc.motorSpinPos++; // 增加电机旋转位置计数 + if (g_mc.encReady == 0) { + /* Z脉冲标志用于确定在初始启动期间IF预设位是否成功使用。 */ + g_mc.encReady = 1; // 如果编码器尚未准备好,设置编码器准备好标志 + } + +} + + + +/* Motor POS loop PID param. */ +static void POSCTRL_InitWrapper(POSCTRL_Handle *posHandle, float ts) +{ + /* Position loop param assignment. */ + PID_Param posPi = { + .kp = POS_KP, + .ki = POS_KI, + .kd = POS_KD, + .ns = POS_NS, + .lowerLim = POS_LOWERLIM, + .upperLim = POS_UPPERLIM, + }; + /* Position loop param init. */ + POSCTRL_Init(posHandle, &posPi, ts); +} + +/* Motor speed loop PI param. */ +static void SPDCTRL_InitWrapper(SPDCTRL_Handle *spdHandle, float ts) +{ + /* Speed loop param assignment. */ + PI_Param spdPi = { + .kp = SPD_KP, + .ki = SPD_KI, + .lowerLim = SPD_LOWERLIM, + .upperLim = SPD_UPPERLIM, + }; + /* Speed loop param init. */ + SPDCTRL_Init(spdHandle, &g_motorParam, spdPi, ts); +} + +/* Motor current Loop PI param. */ +static void CURRCTRL_InitWrapper(CURRCTRL_Handle *currHandle, DqAxis *idqRef, DqAxis *idqFbk, float ts) +{ + /* Axis-D current loop param assignment. */ + PI_Param dCurrPi = { + .kp = CURRDAXIS_KP, + .ki = CURRDAXIS_KI, + .lowerLim = CURR_LOWERLIM, + .upperLim = CURR_UPPERLIM, + }; + /* Axis-Q current loop param assignment. */ + PI_Param qCurrPi = { + .kp = CURRQAXIS_KP, + .ki = CURRQAXIS_KI, + .lowerLim = CURR_LOWERLIM, + .upperLim = CURR_UPPERLIM, + }; + /* Current loop param init. */ + CURRCTRL_Init(currHandle, &g_motorParam, idqRef, idqFbk, dCurrPi, qCurrPi, ts); +} + +/*------------------------------- Function Definition -----------------------------------------------*/ +/** + * @brief 系统标记的初始化器。 + * @param mtrCtrl Motor control struct handle. + * @retval None. + */ +static void TimerTickInit(MTRCTRL_Handle *mtrCtrl) +{ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + mtrCtrl->sysTickCnt = 0; + mtrCtrl->msTickNum = US_PER_MS / SYSTICK_PERIOD_US; + mtrCtrl->capChargeTickNum = (INV_CAP_CHARGE_MS * US_PER_MS / SYSTICK_PERIOD_US); +} + +/** + * @brief Init motor control task. + * @retval None. + */ +static void TSK_Init(void) +{ + g_mc.motorStateFlag = 0; + g_mc.uartHeartDetCnt = 0; + g_mc.uartTimeStamp = 0; + g_mc.stateMachine = FSM_IDLE; + g_mc.currCtrlPeriod = CTRL_CURR_PERIOD; /* 初始化电流控制器 */ + g_mc.aptMaxcntCmp = g_apt0.waveform.timerPeriod; + g_mc.sampleMode = DUAL_RESISTORS; + g_mc.obserType = FOC_OBSERVERTYPE_ENC; /* 启动foc观察模式*/ + g_mc.controlMode = FOC_CONTROLMODE_POS; /* 启动电机控制模式 */ + g_mc.adcCurrCofe = ADC_CURR_COFFI; + g_mc.spdAdjustMode = CUST_SPEED_ADJUST; + g_mc.uartConnectFlag = DISCONNECT; + g_mc.spdCmdHz = 35.0f; /* Motor initialization speed */ + + g_mc.adc0Compensate = ADC0COMPENSATE; /* u相电流初始化adc移位微调值 */ + g_mc.adc1Compensate = ADC1COMPENSATE; /* w相电流初始化adc移位微调值*/ + + IF_Init(&g_mc.ifCtrl, CTRL_IF_CURR_AMP_A, USER_CURR_SLOPE, CTRL_SYSTICK_PERIOD, CTRL_CURR_PERIOD);//IF初始化电流 + RMG_Init(&g_mc.spdRmg, CTRL_SYSTICK_PERIOD, USER_SPD_SLOPE); /* 初始速度斜率*/ + MtrParamInit(&g_mc.mtrParam, g_motorParam); //电机参数初始化 + + TimerTickInit(&g_mc); + SVPWM_Init(&g_mc.sv, INV_VOLTAGE_BUS * ONE_DIV_SQRT3); + R1SVPWM_Init(&g_mc.r1Sv, INV_VOLTAGE_BUS * ONE_DIV_SQRT3, SAMPLE_POINT_SHIFT, SAMPLE_WINDOW_DUTY); + //电机速度回路PI参数。 + SPDCTRL_InitWrapper(&g_mc.spdCtrl, CTRL_SYSTICK_PERIOD); + //电机位置回路PI参数。 + POSCTRL_InitWrapper(&g_mc.posCtrl, CTRL_SYSTICK_PERIOD * 5.0f); /* Position loop control period */ + //电机电流回路PI参数。 + CURRCTRL_InitWrapper(&g_mc.currCtrl, &g_mc.idqRef, &g_mc.idqFbk, CTRL_CURR_PERIOD); + + MotorProt_Init(&g_mc.prot); /* 初始化保护状态通用 */ + OCP_Init(&g_mc.prot.ocp, CTRL_CURR_PERIOD); // 启动过电流保护功能。 + OVP_Init(&g_mc.prot.ovp, CTRL_SYSTICK_PERIOD); //启动直流链路电压保护功能。 + LVP_Init(&g_mc.prot.lvp, CTRL_SYSTICK_PERIOD); //启动直流链路电压保护功能。 + OTP_Init(&g_mc.prot.otp, CTRL_SYSTICK_PERIOD); //初始化超温保护功能。 + STP_Init(&g_mc.prot.stall, CTRL_SYSTICK_PERIOD, PROT_STALLING_CURR_AMP_LIMIT, + PROT_STALLING_SPD_LIMIT, PROT_STALLING_TIME_LIMIT);//启动电机失速保护功能。 +} + +/** + * @brief Clear historical values of all controller before start-up. + * @param mtrCtrl The motor control handle. + * @retval None. + */ +static void ClearBeforeStartup(MTRCTRL_Handle *mtrCtrl) +{ + /* Verify Parameters */ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + /* The initial angle is 0. */ + mtrCtrl->axisAngle = 0.0f; + mtrCtrl->spdRefHz = 0.0f; + mtrCtrl->motorSpinPos = 0; + /* The initial dq-axis reference current is 0. */ + mtrCtrl->idqRef.d = 0.0f; + mtrCtrl->idqRef.q = 0.0f; + + mtrCtrl->vdqRef.d = 0.0f; + mtrCtrl->vdqRef.q = 0.0f; + /* Clear Duty Cycle Value. The initial duty cycle is 0.5. */ + mtrCtrl->dutyUvwLeft.u = 0.5f; + mtrCtrl->dutyUvwLeft.v = 0.5f; + mtrCtrl->dutyUvwLeft.w = 0.5f; + mtrCtrl->dutyUvwRight.u = 0.5f; + mtrCtrl->dutyUvwRight.v = 0.5f; + mtrCtrl->dutyUvwRight.w = 0.5f; + + mtrCtrl->prot.motorErrStatus.all = 0x00; + + RMG_Clear(&mtrCtrl->spdRmg); /* Clear the history value of speed slope control */ + CURRCTRL_Clear(&mtrCtrl->currCtrl); + IF_Clear(&mtrCtrl->ifCtrl); + SPDCTRL_Clear(&mtrCtrl->spdCtrl); + STARTUP_Clear(&mtrCtrl->startup); + R1SVPWM_Clear(&mtrCtrl->r1Sv); + POSCTRL_Clear(&mtrCtrl->posCtrl); + MCS_EncoderClear(&g_enc); + + OTP_Clear(&mtrCtrl->prot.otp); + OCP_Clear(&mtrCtrl->prot.ocp); + OVP_Clear(&mtrCtrl->prot.ovp); + LVP_Clear(&mtrCtrl->prot.lvp); +} + +/** + * @brief To set the comparison value of the IGBT single-resistance ADC sampling trigger position. + * @param aptx The APT register struct handle. + * @param cntCmpA A Count compare reference of time-base counter. + * @param cntCmpB B Count compare reference of time-base counter. + * @param maxCntCmp Maximum Comparison Value + * @retval None. + */ +static void MCS_SetAdcCompareR1(APT_RegStruct *aptx, unsigned short cntCmpA, + unsigned short cntCmpB, unsigned short maxCntCmp) +{ + unsigned short tmp; + /* Sets the A Count compare reference of time-base counter. */ + tmp = (unsigned short)Clamp((float)(cntCmpA), (float)(maxCntCmp - 1), 1.0f); + DCL_APT_SetCounterCompare(aptx, APT_COMPARE_REFERENCE_A, tmp); + /* Sets the B Count compare reference of time-base counter. */ + tmp = (unsigned short)Clamp((float)(cntCmpB), (float)(maxCntCmp - 1), 1.0f); + DCL_APT_SetCounterCompare(aptx, APT_COMPARE_REFERENCE_B, tmp); +} + +/** + * @brief Open the three-phase lower pipe. + * @param aptAddr Three-phase APT address pointer. + * @param maxDutyCnt Max duty count. + * @retval None. + */ +static void AptTurnOnLowSidePwm(APT_RegStruct **aptAddr, unsigned short maxDutyCnt) +{ + MCS_ASSERT_PARAM(aptAddr != NULL); + MCS_ASSERT_PARAM(maxDutyCnt != 0); + unsigned short dutyCnt; + dutyCnt = (unsigned short)(maxDutyCnt * APT_FULL_DUTY); + /* Open the three-phase lower pipe */ + for (unsigned int i = 0; i < PHASE_MAX_NUM; i++) { + APT_RegStruct *aptx = (APT_RegStruct *)(aptAddr[i]); + DCL_APT_SetCounterCompare(aptx, APT_COMPARE_REFERENCE_C, dutyCnt); + DCL_APT_SetCounterCompare(aptx, APT_COMPARE_REFERENCE_D, dutyCnt); + } +} + +/** + * @brief Enable three-phase pwm output. + * @param aptAddr Three-phase APT address pointer. + * @retval None. + */ +static void MotorPwmOutputEnable(APT_RegStruct **aptAddr) +{ + MCS_ASSERT_PARAM(aptAddr != NULL); + /* Enable three-phase pwm output */ + for (unsigned int i = 0; i < PHASE_MAX_NUM; i++) { + APT_RegStruct *aptx = (APT_RegStruct *)(aptAddr[i]); + aptx->PG_OUT_FRC.BIT.rg_pga_frc_en = BASE_CFG_UNSET; + aptx->PG_OUT_FRC.BIT.rg_pgb_frc_en = BASE_CFG_UNSET; + } +} + +/** + * @brief Disable three-phase pwm output. + * @param aptAddr Three-phase APT address pointer. + * @retval None. + */ +static void MotorPwmOutputDisable(APT_RegStruct **aptAddr) +{ + MCS_ASSERT_PARAM(aptAddr != NULL); + /* Disable three-phase pwm output. */ + for (unsigned int i = 0; i < PHASE_MAX_NUM; i++) { + APT_RegStruct *aptx = (APT_RegStruct *)(aptAddr[i]); + aptx->PG_OUT_FRC.BIT.rg_pga_frc_en = BASE_CFG_SET; + aptx->PG_OUT_FRC.BIT.rg_pgb_frc_en = BASE_CFG_SET; + DCL_APT_ForcePWMOutputLow(aptx); + } +} + +/** + * @brief 构造一个新的mcs启动开关对象。 + * @param mtrCtrl The motor control handle. + * @retval None. + */ +static void MCS_StartupSwitch(MTRCTRL_Handle *mtrCtrl) +{ + // 确保传递的电机控制句柄不是NULL + MCS_ASSERT_PARAM(mtrCtrl != NULL); + // 获取启动阶段处理结构体的指针 + STARTUP_Handle *startup = &mtrCtrl->startup; + // 获取dq轴参考值结构体的指针 + DqAxis *idqRef = &mtrCtrl->idqRef; + // 获取目标电流幅值 + float iftargetAmp = mtrCtrl->ifCtrl.targetAmp; + // 获取速度参考值(Hz) + float spdRefHz = mtrCtrl->spdRefHz; + // 根据当前启动阶段执行相应操作 + switch (startup->stage) { + case STARTUP_STAGE_CURR: + if (mtrCtrl->ifCtrl.curAmp >= iftargetAmp) { //IF采样电流大于IF目标电流时。 + /* 阶段转换 */ + // 设置q轴参考电流为目标电流值 + idqRef->q = iftargetAmp; + // 更改启动阶段至速度阶段 + startup->stage = STARTUP_STAGE_SPD; + mtrCtrl->encReady = 0; /* 启动阶段清除Z信号干扰错误 */ + + } else { + /* 增加电流幅值 */ + // 计算并设置新的q轴参考电流 + idqRef->q = IF_CurrAmpCalc(&mtrCtrl->ifCtrl); + // 在此阶段保持速度参考为0 + spdRefHz = 0.0f; + } + break; + + case STARTUP_STAGE_SPD: // 当前处于速度启动阶段 + /* 提高电流频率 */ + // 判断电机是否已经旋转了足够圈数 + if (mtrCtrl->motorSpinPos > 3) { /* 3是If模式下的电机转数 */ + /* 阶段转换 */ + // 转换状态机至运行状态 + mtrCtrl->stateMachine = FSM_RUN; + } else { + /* 速度斜坡生成 */ + // 执行速度斜坡生成函数,并设定一个固定的拖动速度 + spdRefHz = RMG_Exec(&mtrCtrl->spdRmg, 5.0f); /*5.0f为If模式力拖动速度*/ + } + break; + + default: + // 默认情况,不执行任何操作 + break; + } + // 更新电机控制结构中的速度参考值 + mtrCtrl->spdRefHz = spdRefHz; +} + +/** + * @brief Pre-processing of motor status. + * @param statusReg System status. + * @param stateMachine Motor Control Status. + * @retval None. + */ +static void MotorStatePerProc(SysStatusReg *statusReg, volatile FsmState *stateMachine) +{ + MCS_ASSERT_PARAM(statusReg != NULL); + MCS_ASSERT_PARAM(stateMachine != NULL); + /* Get system status */ + if (SysIsError(statusReg)) { + *stateMachine = FSM_FAULT; + } + if (SysGetCmdStop(statusReg)) { + SysCmdStopClr(statusReg); + *stateMachine = FSM_STOP; + } +} + +/** + * @brief Check over current status. + * @param statusReg System status. + * @param stateMachine Motor Control Status. + * @retval None. + */ +static void CheckOverCurrentState(SysStatusReg *statusReg, FsmState *stateMachine) +{ + MCS_ASSERT_PARAM(statusReg != NULL); + MCS_ASSERT_PARAM(stateMachine != NULL); + /* check systerm error status */ + if (SysIsError(statusReg) == false) { + *stateMachine = FSM_IDLE; + } +} + +/** + * @brief Check bootstrap capacitor charge time. + * @param mtrCtrl The motor control handle. + * @param stateMachine Motor Control Status. + * @retval None. + */ +static void CheckBootstrpCapChargeTime(MTRCTRL_Handle *mtrCtrl, FsmState *stateMachine) +{ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + MCS_ASSERT_PARAM(stateMachine != NULL); + mtrCtrl->sysTickCnt++; + /* check bootstrap capacitor charge time */ + if (mtrCtrl->sysTickCnt == mtrCtrl->capChargeTickNum) { + *stateMachine = FSM_CLEAR; + } +} + + + +/** + * @brief Check systerm cmd start signal. + * @param mtrCtrl The motor control handle. + * @param aptAddr Three-phase APT address pointer. + * @param statusReg System status. + * @param stateMachine Motor Control Status. + * @retval None. + */ +static void CheckSysCmdStart(MTRCTRL_Handle *mtrCtrl, + APT_RegStruct **aptAddr, + SysStatusReg *statusReg, + FsmState *stateMachine) +{ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + MCS_ASSERT_PARAM(aptAddr != NULL); + MCS_ASSERT_PARAM(statusReg != NULL); + MCS_ASSERT_PARAM(stateMachine != NULL); + /* check start cmd */ + if (SysGetCmdStart(statusReg)) { + SysRunningSet(statusReg); + SysCmdStartClr(statusReg); + mtrCtrl->sysTickCnt = 0; + *stateMachine = FSM_CAP_CHARGE; + /* 为自举电容器充电做准备。 */ + AptTurnOnLowSidePwm(aptAddr, mtrCtrl->aptMaxcntCmp); + /* Out put pwm */ + MotorPwmOutputEnable(aptAddr); + } +} + +/***************************************** + * @brief 系统计时器勾选任务。 + * @param mtrCtrl The motor control handle. + * @param aptAddr Three-phase APT address pointer. + * @retval None. + * 速度环 + * + * + ****************************************/ +static void TSK_SystickIsr(MTRCTRL_Handle *mtrCtrl, APT_RegStruct **aptAddr) +{ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + MCS_ASSERT_PARAM(aptAddr != NULL); + SysStatusReg *statusReg = &mtrCtrl->statusReg; + FsmState *stateMachine = &mtrCtrl->stateMachine; + mtrCtrl->msTickCnt++; + /* 电机状态的预处理。 */ + MotorStatePerProc(statusReg, stateMachine); + /* statemachine */ + switch (*stateMachine) { + case FSM_IDLE: + /*在电机启动前设置smo估计速度*/ + CheckSysCmdStart(mtrCtrl, aptAddr, statusReg, stateMachine); + break; + case FSM_CAP_CHARGE: + /* Bootstrap电容充电定时 */ + CheckBootstrpCapChargeTime(mtrCtrl, stateMachine); + break; + /* 启动前清除参数 */ + case FSM_CLEAR: + ClearBeforeStartup(mtrCtrl); + *stateMachine = FSM_STARTUP; + break; + case FSM_STARTUP: + // 构造一个新的mcs启动开关对象。 + MCS_StartupSwitch(mtrCtrl); + break; + case FSM_RUN: + if (mtrCtrl->controlMode == FOC_CONTROLMODE_SPEED) { /* Speed control mode */ + /* 速度斜坡控制 */ + mtrCtrl->spdRefHz = RMG_Exec(&mtrCtrl->spdRmg, mtrCtrl->spdCmdHz); + } else if (mtrCtrl->controlMode == FOC_CONTROLMODE_POS) { /* Position control mode */ + mtrCtrl->sysTickCnt++; + POSCTRL_SetSlope(&mtrCtrl->posCtrl, mtrCtrl->spdCmdHz);//设置位置斜坡 + /* 200.0是目标位置,用户可以重新定义 */ + // POSCTRL_SetTarget(&mtrCtrl->posCtrl, 0.5 *DOUBLE_PI * g_motorParam.mtrNp);//定位环目标位置设置。 + + float posFbk = POSCTRL_AngleExpand(&mtrCtrl->posCtrl, mtrCtrl->axisAngle); + + if (mtrCtrl->sysTickCnt % 5 == 0) { /* 5是位置环划分系数。*/ + mtrCtrl->spdRefHz = POSCTRL_Exec(&mtrCtrl->posCtrl, mtrCtrl->posCtrl.posTarget, posFbk); + } + } + /* 速度环路控制 */ + mtrCtrl->idqRef.q = SPDCTRL_Exec(&mtrCtrl->spdCtrl, mtrCtrl->spdRefHz, mtrCtrl->encSpeed);//用速度偏差值,求出所需Q轴电流 + + break; + case FSM_STOP: + mtrCtrl->spdRefHz = 0.0f; + MotorPwmOutputDisable(aptAddr); + SysRunningClr(statusReg); + *stateMachine = FSM_IDLE; + break; + case FSM_FAULT: /* Overcurrent state */ + CheckOverCurrentState(statusReg, stateMachine); + break; + default: + break; + } +} + +/** + * @brief Read the ADC initialize bias trim value. + * @param mtrCtrl The motor control handle. + * @retval None. + */ +static void TrimInitAdcShiftValue(MTRCTRL_Handle *mtrCtrl) +{ + float adc0SampleTemp = 0.0f; /* Current bias value for temp store */ + float adc1SampleTemp = 0.0f; + float adc0TempSum = 0.0f; + float adc1TempSum = 0.0f; /* Current bias sum value for 20 times */ + float adcSampleTimes = 0.0f; /* ADC sample times */ + for (int i = 0; i < ADC_READINIT_TIMES; i++) { + adc0SampleTemp = (float)HAL_ADC_GetConvResult(&ADCU_HANDLE, ADCUSOCNUM); + adc1SampleTemp = (float)HAL_ADC_GetConvResult(&ADCW_HANDLE, ADCWSOCNUM); + BASE_FUNC_DELAY_US(200); /* 200 is delay count, delay 200us triger adc sampling */ + if (adc0SampleTemp > 1000.0f && adc1SampleTemp > 1000.0f) { + adcSampleTimes++; + adc0TempSum += adc0SampleTemp; + adc1TempSum += adc1SampleTemp; + } + } + adc0SampleTemp = adc0TempSum / adcSampleTimes; + adc1SampleTemp = adc1TempSum / adcSampleTimes; + /* Force convert to float */ + mtrCtrl->adc0Compensate = (float) adc0SampleTemp; + mtrCtrl->adc1Compensate = (float) adc1SampleTemp; + /* The normal value scope: 1800 < adc0Compensate < 2200 */ + if(g_mc.adc0Compensate < ADC_TRIMVALUE_MIN || g_mc.adc0Compensate > ADC_TRIMVALUE_MAX \ + || g_mc.adc1Compensate < ADC_TRIMVALUE_MIN || g_mc.adc1Compensate > ADC_TRIMVALUE_MAX) { + DBG_PRINTF("ADC trim value error,please reset!"); + HAL_GPIO_SetValue(&LED2_HANDLE, LED2_PIN, GPIO_LOW_LEVEL); + } + adcSampleTimes = 0; + adc0TempSum = 0; + adc1TempSum = 0; +} + +/** + * @brief Read the ADC current sampling value. + * @param CurrUvw Three-phase current. + * @retval None. + */ +static void ReadCurrUvw(UvwAxis *CurrUvw) +{ + MCS_ASSERT_PARAM(CurrUvw != NULL); + float adc0 = (float)HAL_ADC_GetConvResult(&ADCU_HANDLE, ADCUSOCNUM); + float adc1 = (float)HAL_ADC_GetConvResult(&ADCW_HANDLE, ADCWSOCNUM); + /* Convert adc sample value to current value */ + CurrUvw->u = -(adc0 - g_mc.adc0Compensate) * g_mc.adcCurrCofe; + CurrUvw->w = -(adc1 - g_mc.adc1Compensate) * g_mc.adcCurrCofe; + CurrUvw->v = -CurrUvw->u - CurrUvw->w; +} + +/** + * @brief Setting the APT Output Duty Cycle. + * @param aptx APT register base address. + * @param leftDuty Left duty cycle. + * @param rightDuty Right duty cycle. + * @retval None. + */ +static void SetPwmDuty(APT_Handle *aptx, float leftDuty, float rightDuty) +{ + MCS_ASSERT_PARAM(aptx != NULL); + MCS_ASSERT_PARAM(leftDuty > 0); + MCS_ASSERT_PARAM(rightDuty > 0); + unsigned short maxPeriodCnt = aptx->waveform.timerPeriod; + unsigned short cntCmpLeftEdge = (unsigned short)(leftDuty * maxPeriodCnt); + unsigned short cntCmpRightEdge = (unsigned short)(rightDuty * maxPeriodCnt); + /* avoid overflowing */ + cntCmpLeftEdge = (cntCmpLeftEdge > maxPeriodCnt) ? maxPeriodCnt : cntCmpLeftEdge; + cntCmpRightEdge = (cntCmpRightEdge > maxPeriodCnt) ? maxPeriodCnt : cntCmpRightEdge; + HAL_APT_SetPWMDuty(aptx, cntCmpLeftEdge, cntCmpRightEdge); +} + +/** + * @brief Duty Cycle Setting. + * @param dutyUvwLeft Three-phase left duty cycle. + * @param dutyUvwRight Three-phase right duty cycle. + * @retval None. + */ +static void SetPwmDutyCp(UvwAxis *dutyUvwLeft, UvwAxis *dutyUvwRight) +{ + MCS_ASSERT_PARAM(dutyUvwLeft != NULL); + MCS_ASSERT_PARAM(dutyUvwRight != NULL); + /* Setting the Three-Phase Duty Cycle */ + SetPwmDuty(&g_apt0, dutyUvwLeft->u, dutyUvwRight->u); + SetPwmDuty(&g_apt1, dutyUvwLeft->v, dutyUvwRight->v); + SetPwmDuty(&g_apt2, dutyUvwLeft->w, dutyUvwRight->w); +} + +/** + * @brief To set the ADC sampling trigger comparison value. + * @param cntCmpSOCA Soca Compare Count Value. + * @param cntCmpSOCB Socb Compare Count Value. + * @retval None. + */ +static void SetADCTriggerTime(unsigned short cntCmpSOCA, unsigned short cntCmpSOCB) +{ + MCS_SetAdcCompareR1(g_apt[PHASE_U], cntCmpSOCA, cntCmpSOCB, g_mc.aptMaxcntCmp); +} + +/** + * @brief Temprature table. + * @param tempResisValue Temperature sensor resistance. + * @retval None. + */ +static float TempTable(float tempResisValue) +{ + float boardTemp = 0.0f; + /* Temperatures between 15 and 30. */ + if (tempResisValue > TEMP_RES_30 && tempResisValue <= TEMP_RES_15) { + boardTemp = TEMP_15 + (TEMP_30 - TEMP_15) * (TEMP_RES_15 - tempResisValue) / (TEMP_RES_15 - TEMP_RES_30); + } else if (tempResisValue > TEMP_RES_45 && tempResisValue <= TEMP_RES_30) { /* Temperature between 30 and 45. */ + boardTemp = TEMP_30 + (TEMP_45 - TEMP_30) * (TEMP_RES_30 - tempResisValue) / (TEMP_RES_30 - TEMP_RES_45); + } else if (tempResisValue > TEMP_RES_60 && tempResisValue <= TEMP_RES_45) { /* Temperature between 45 and 50. */ + boardTemp = TEMP_45 + (TEMP_60 - TEMP_45) * (TEMP_RES_45 - tempResisValue) / (TEMP_RES_45 - TEMP_RES_60); + } else if (tempResisValue <= TEMP_RES_60) { + boardTemp = TEMP_60; /* If temperature is over 60, set temperature as 60. */ + } else if (tempResisValue >= TEMP_RES_15) { + boardTemp = TEMP_15; /* If temperature is lower 15, set temperature as 15. */ + } + return boardTemp; +} + +/** + * @brief Read power board temperature and udc. + * @retval None. + */ +static void ReadBoardTempAndUdc(void) +{ + HAL_ADC_SoftTrigSample(&ADCRESIS_HANDLE, ADCRESISSOCNUM); + HAL_ADC_SoftTrigSample(&ADCUDC_HANDLE, ADCUDCSOCNUM); + BASE_FUNC_DELAY_US(CNT_10); /* Delay 10 us. */ + /* Force convert to float type. */ + float resisAdcValue = (float)HAL_ADC_GetConvResult(&ADCRESIS_HANDLE, ADCRESISSOCNUM); + /* 10 / (x + 10) * 3.3 = resisAdcValue / 4096 * 3.3, x is resisValue, 10kohm is resistor divider value. */ + float resisValue = (4096.0f * 10.0f - 10.0f * resisAdcValue) / resisAdcValue; + g_mc.powerBoardTemp = TempTable(resisValue); + g_mc.udc = ((float)HAL_ADC_GetConvResult(&ADCUDC_HANDLE, ADCUDCSOCNUM)) * ADC_UDC_COFFI; +} + +/** + * @brief 执行异常反馈速度保护动作。 + * @retval None. + */ +static void SpdFbkErrorProt_Exec(void) +{ + if (g_mc.prot.motorErrStatus.Bit.motorStalling == 0 && + g_mc.prot.motorErrStatus.Bit.overVoltErr == 0 && + g_mc.prot.motorErrStatus.Bit.lowerVoltErr == 0 && + g_mc.prot.motorErrStatus.Bit.overIpmTempErr == 0 && + g_mc.prot.motorErrStatus.Bit.overCurrErr == 0) { + g_mc.prot.motorErrStatus.Bit.revRotErr = 1; + /* If revRotErr, execute protect motion. */ + ProtSpo_Exec(g_apt); + } +} +/** + * @brief Check abnormal feedback speed. + * @retval None. + */ +static void CheckSpdFbkStatus(void) +{ + static short errorSpdStatus = 0; + static short errorDeltaSpdStatus = 0; + /* Detect the nan value. */ + if (isnan(g_mc.encSpeed) || isnan(g_mc.idqRef.q)) { + errorSpdStatus++; + } else { + errorSpdStatus = 0; + } + if (g_mc.stateMachine == FSM_RUN) { + /* 检测反馈速度异常。定义10为速度误差值,0.5为当前误差值*/ + if (Abs(g_mc.spdRefHz - g_mc.encSpeed) >= CNT_10 && g_mc.idqRef.q <= 0.5f) { + errorDeltaSpdStatus++; + } + } + /*如果计数超过5000次,则执行保护动作. */ + if (errorSpdStatus >= CNT_5000 || errorDeltaSpdStatus >= CNT_5000) { + errorSpdStatus = 0; + errorDeltaSpdStatus = 0; + SpdFbkErrorProt_Exec(); + } +} +/** + * @brief Check Potentiometer Value callback function. + * @param param The TIMER_Handle. + * @retval None. + */ +void CheckPotentiometerValueCallback(void *param) +{ + MCS_ASSERT_PARAM(param != NULL); + BASE_FUNC_UNUSED(param); + static float potentiomitorAdcValue = 0.0f; + static float spdCmdHz = 0; + static float spdCmdHzLast = 35.0f; /* 35.0 is spdCmdHzLast init value */ + HAL_ADC_SoftTrigSample(&ADCPTT_HANDLE, ADCPTTSOCNUM); + BASE_FUNC_DELAY_US(10); /* Delay 10 us. */ + potentiomitorAdcValue = (float)HAL_ADC_GetConvResult(&ADCPTT_HANDLE, ADCPTTSOCNUM); + /* 4045.0 is adc sample max value of potentiomitor, convert max spd to 200.0Hz */ + spdCmdHz = potentiomitorAdcValue / 4045.0f * USER_MAX_SPD_HZ;//电位器控制速度 + // spdCmdHz=uart_spdcmd;//串口控制速度 + + POSCTRL_SetTarget(&g_mc.posCtrl,uart_poscmd * g_motorParam.mtrNp);//串口控制位置转动 + + // POSCTRL_SetTarget(&g_mc.posCtrl,potentiomitorAdcValue / 4045.0f * 6.28 * g_motorParam.mtrNp);//电位器控制电机位置转动 + + + if (Abs(spdCmdHzLast - spdCmdHz) < 1.0f) { /* Ignore changes less than 1. */ + return; + } + spdCmdHzLast = spdCmdHz; + if (spdCmdHz < USER_MIN_SPD_HZ) { /* USER_MIN_SPD_HZ is spdCmdHz lower limit */ + spdCmdHz = USER_MIN_SPD_HZ; /* USER_MIN_SPD_HZ is spdCmdHz lower limit */ + } + if (spdCmdHz > g_mc.mtrParam.maxElecSpd) { /* spdCmdHz upper limit */ + spdCmdHz = g_mc.mtrParam.maxElecSpd; /* spdCmdHz upper limit */ + } + if (g_mc.spdAdjustMode == CUST_SPEED_ADJUST) { + g_mc.spdCmdHz = spdCmdHz; + } +} + +/** + * @brief 用于电机状态机回叫功能的系统定时器ISR。 + * @param param The systick timer handle. + * @retval None. + * + */ +void MotorStatemachineCallBack(void *param) +{ + /* Verify Parameters */ + MCS_ASSERT_PARAM(param != NULL); + BASE_FUNC_UNUSED(param); + /* 读取电源板温度和电压。*/ + ReadBoardTempAndUdc(); + /* 电机错误状态检查. */ + TSK_SystickIsr(&g_mc, g_apt); + if (g_mc.prot.motorErrStatus.all == 0) { + /* 电机错误状态检查 */ + CheckSpdFbkStatus(); + /* 电机失速检测。 */ + STP_Det_ByCurrSpd(&g_mc.prot.stall, &g_mc.prot.motorErrStatus, g_mc.encSpeed, g_mc.idqFbk); + STP_Exec(&g_mc.prot.motorErrStatus, g_apt); + } + /* 电机过电压检测。 */ + OVP_Det(&g_mc.prot.ovp, &g_mc.prot.motorErrStatus, g_mc.udc); + OVP_Exec(&g_mc.prot.ovp, &g_mc.spdRefHz, g_apt); + OVP_Recy(&g_mc.prot.ovp, &g_mc.prot.motorErrStatus, g_mc.udc); + /* 电机低电压检测. */ + LVP_Det(&g_mc.prot.lvp, &g_mc.prot.motorErrStatus, g_mc.udc); + LVP_Exec(&g_mc.prot.lvp, &g_mc.spdRefHz, g_apt); + LVP_Recy(&g_mc.prot.lvp, &g_mc.prot.motorErrStatus, g_mc.udc); + /* 电源板超温检测. */ + OTP_Det(&g_mc.prot.otp, &g_mc.prot.motorErrStatus, OTP_IPM_ERR_BIT, g_mc.powerBoardTemp); + OTP_Exec(&g_mc.prot.otp, &g_mc.spdRefHz, g_apt); + OTP_Recy(&g_mc.prot.otp, &g_mc.prot.motorErrStatus, OTP_IPM_ERR_BIT, g_mc.powerBoardTemp); + + /* 如果保护级别==4,则将电机状态设置为停止。 */ + if (g_mc.prot.ovp.protLevel == LEVEL_4 || g_mc.prot.lvp.protLevel == LEVEL_4 \ + || g_mc.prot.otp.protLevel == LEVEL_4) { + SysCmdStopSet(&g_mc.statusReg); + } +} + +/** + * @brief The carrier ISR wrapper function. + * @param aptHandle The APT handle. + * @retval None. + * + * 载波中断函数, + */ +void MotorCarrierProcessCallback(void *aptHandle) +{ + MCS_ASSERT_PARAM(aptHandle != NULL); + BASE_FUNC_UNUSED(aptHandle); + /* the carrierprocess of motor */ + MCS_CarrierProcess(&g_mc); + /* Over current protect */ + if (g_mc.stateMachine == FSM_RUN || g_mc.stateMachine == FSM_STARTUP) { + OCP_Det(&g_mc.prot.ocp, &g_mc.prot.motorErrStatus, g_mc.idqFbk); + OCP_Exec(&g_mc.prot.ocp, &g_mc.idqFbk, g_apt); /* Execute over current protect motion */ + if (g_mc.prot.ocp.protLevel < LEVEL_4) { + OCP_Recy(&g_mc.prot.ocp, &g_mc.prot.motorErrStatus); + } + } +} + +/** + * @brief Event interrupt callback function of APT module. + * @param para APT module handle. + * @retval None. + */ +void MotorSysErrCallback(void *para) +{ + MCS_ASSERT_PARAM(para != NULL); + APT_Handle *handle = (APT_Handle *)para; + /* The IPM overcurrent triggers and disables the three-phase PWM output. */ + MotorPwmOutputDisable(g_apt); + DCL_APT_ClearOutCtrlEventFlag((APT_RegStruct *)g_apt[PHASE_U], APT_OC_COMBINE_EVENT_A1); + DCL_APT_ClearOutCtrlEventFlag((APT_RegStruct *)g_apt[PHASE_V], APT_OC_COMBINE_EVENT_A1); + DCL_APT_ClearOutCtrlEventFlag((APT_RegStruct *)g_apt[PHASE_W], APT_OC_COMBINE_EVENT_A1); + /* Status setting error */ + SysErrorSet(&g_mc.statusReg); + DBG_PRINTF("APT error! \r\n"); + HAL_GPIO_SetValue(&LED2_HANDLE, LED2_PIN, GPIO_LOW_LEVEL); + BASE_FUNC_UNUSED(handle); +} + +/** + * @brief 初始化电机控制器的数据结构。 + * @retval None. + */ +static void InitSoftware(void) +{ + /* 正在初始化电机控制参数 */ + TSK_Init(); + /* Read phase-uvw current */ + g_mc.readCurrUvwCb = ReadCurrUvw; + g_mc.setPwmDutyCb = SetPwmDutyCp; + g_mc.setADCTriggerTimeCb = SetADCTriggerTime; + g_mc.encHandle = &g_enc; + + MCS_EncInitStru encMotorParam; + EncoderHandle* enc = &g_enc; + encMotorParam.mtrNp = g_motorParam.mtrNp; + encMotorParam.mtrPPMR = g_motorParam.mtrPPMR; + encMotorParam.zShift = g_motorParam.zShift; + encMotorParam.ctrlPeriod = CTRL_CURR_PERIOD; + encMotorParam.timeNum = ENC_TIMES_NUM; + MCS_EncoderInit(enc, &encMotorParam); /* encoder Initializing Parameter Configurations. */ + + /* MCU peripheral configuration function used for initial motor control. */ + g_mc.getEncAngSpd = GetEncAngSpd; /* Callback function for obtaining the encoder speed angle. */ +} + +/** + * @brief Config the master APT. + * @param aptx The master APT handle. + * @retval None. + */ +static void AptMasterSet(APT_Handle *aptx) +{ + MCS_ASSERT_PARAM(aptx != NULL); + /* Config the master APT. */ + HAL_APT_MasterSyncInit(aptx, APT_SYNC_OUT_ON_CNTR_ZERO); +} + +/** + * @brief Config the slave APT. + * @param aptx The slave APT handle. + * @retval None. + */ +static void AptSalveSet(APT_Handle *aptx) +{ + MCS_ASSERT_PARAM(aptx != NULL); + APT_SlaveSyncIn slave; + /* Config the slave APT. */ + slave.divPhase = 0; + slave.cntPhase = 0; + slave.syncCntMode = APT_COUNT_MODE_AFTER_SYNC_UP; + slave.syncInSrc = APT_SYNC_IN_SRC; + slave.cntrSyncSrc = APT_CNTR_SYNC_SRC_SYNCIN; + HAL_APT_SlaveSyncInit(aptx, &slave); +} +/** + * @brief Configuring Master and Slave APTs. + * @retval None. + */ +static void AptMasterSalveSet(void) +{ + /* motor fan APT master/slave synchronization */ + AptMasterSet(&g_apt0); + AptSalveSet(&g_apt1); + AptSalveSet(&g_apt2); +} + +/** + * @brief Config the KEY func. + * @param handle The GPIO handle. + * @retval None. + */ +static KEY_State Key_StateRead(GPIO_Handle *handle) +{ + if (HAL_GPIO_GetPinValue(handle, handle->pins) == 0) { + BASE_FUNC_DELAY_MS(30); /* delay 30ms for deshake */ + if (HAL_GPIO_GetPinValue(handle, handle->pins) == 0) { + while (HAL_GPIO_GetPinValue(handle, handle->pins) == 0) { + } + return KEY_DOWN; + } + } + return KEY_UP; +} + + +/** + * @brief Control motor start and stop state by key func. + * @param param The GPIO handle. + * @retval None. + */ +void MotorStartStopKeyCallback(void *param) +{ + GPIO_Handle *handle = (GPIO_Handle *)param; + if (Key_StateRead(handle) == KEY_DOWN) { + if (g_mc.motorStateFlag == 0) { /* start motor */ + g_mc.motorStateFlag = 1; + SysCmdStartSet(&g_mc.statusReg); + } else if (g_mc.motorStateFlag == 1) { /* stop motor */ + g_mc.motorStateFlag = 0; + SysCmdStopSet(&g_mc.statusReg); + } + } +} + +/** + * @brief 用户应用程序的主入口函数。 + * @retval BSP_OK 返回状态。 + */ +int MotorMainProcess(void) +{ + unsigned int tickNum1Ms = 2; /* 定义1毫秒的计时器滴答数 */ + static unsigned int tickCnt1Ms = 0; /* 静态变量,用于记录1毫秒滴答计数 */ + unsigned int tickNum500Ms = 1000; /* 定义500毫秒的计时器滴答数 */ + static unsigned int tickCnt500Ms = 0; /* 静态变量,用于记录500毫秒滴答计数 */ + + SystemInit(); /* 系统初始化 */ + HMI_Init(); /* 初始化串口中断,用于人机交互接口 */ + HAL_TIMER_Start(&g_timer0); /* 启动定时器0 */ + HAL_TIMER_Start(&g_timer1); /* 启动定时器1 */ + + + AptMasterSalveSet(); /* 设置主从设备 */ + /* 在启动前禁用PWM输出。 */ + MotorPwmOutputDisable(g_apt); /* 禁用PWM输出 */ + /* 软件初始化。 */ + InitSoftware(); /* 初始化软件相关设置 电机参数初始化*/ + + /* QDM使用算法驱动的速度和角度采集 */ + static MCS_QdmInitStru qdmInit; /* QDM初始化结构体 */ + qdmInit.qdmAddr = g_periph.qdm.qdmAddr; /* 设置QDM地址 */ + + qdmInit.zPlusesIrqFunc = ISR_QdmzPulses; /* 设置QDM零脉冲中断服务函数 */ + + qdmInit.zPlusesNvic = &g_periph.qdm.zPulsesNvic; /* 设置QDM零脉冲中断控制器 */ + qdmInit.zPlusesIrqPrio = IRQ_QDM0_PRIORITY; /* 设置QDM零脉冲中断优先级 */ + MCS_QdmInit(&qdmInit); /* 初始化QDM,必须在使能载波中断之前执行。 */ + + /* 启动PWM时钟。 */ + HAL_APT_StartModule(RUN_APT0 | RUN_APT1 | RUN_APT2); /* 启动APT模块 */ + /* 系统定时器时钟延迟。 */ + BASE_FUNC_DELAY_MS(ADC_READINIT_DELAY); /* 延迟,等待ADC初始化 */ + TrimInitAdcShiftValue(&g_mc); /* 初始化ADC移位值 */ + BASE_FUNC_DELAY_MS(MOTOR_START_DELAY); /* 延迟,等待电机启动 */ + while (1) { + + /* 循环发送数据到主机 */ + HMI_Process_Tx(&g_mc); /* 处理人机交互接口的发送数据 */ + if (g_mc.msTickCnt - tickCnt1Ms >= tickNum1Ms) { /* 检查是否达到1毫秒的计时 */ + tickCnt1Ms = g_mc.msTickCnt; /* 更新1毫秒滴答计数 */ + /* 用户代码1毫秒事件 */ + HMI_Process_Rx(&g_mc); /* 处理人机交互接口的接收数据 */ + /* 用户代码1毫秒事件 */ + } + + if (g_mc.msTickCnt - tickCnt500Ms >= tickNum500Ms) { /* 检查是否达到500毫秒的计时 */ + if (SysIsError(&g_mc.statusReg) != true) { /* 检查系统是否出现错误 */ + /* 如果系统状态正常,则切换LED灯的状态。 */ + HAL_GPIO_TogglePin(&LED1_HANDLE, LED1_PIN); /* 切换LED灯的状态 */ + } + tickCnt500Ms = g_mc.msTickCnt; /* 更新500毫秒滴答计数 */ + } + } + return 0; +} diff --git a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/user_interface/cust_process.c b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/user_interface/cust_process.c new file mode 100644 index 00000000..69f95195 --- /dev/null +++ b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/user_interface/cust_process.c @@ -0,0 +1,639 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file cust_process.c + * @author MCU Algorithm Team + * @brief This file provides functions declaration of cust process interface. + */ + +#include "cust_process.h" +#include "mcs_ctlmode_config.h" +#include "mcs_math_const.h" +#include "mcs_user_config.h" +#include "mcs_assert.h" +#include "main.h" +#include "uart_module.h" +#include "mcs_mtr_param.h" +#include "mcs_inc_enc.h" + +/* Macro definitions --------------------------------------------------------------------------- */ +/* Constant value. */ +#define CONST_VALUE_60 60.0f /* Constant value 60. */ +#define CONST_VALUE_DIV_1000 0.001f /* Constant value 1/1000. */ + +/* Data array index. */ +#define DATA_SEGMENT_ONE 0 /* First element of the data segment */ +#define DATA_SEGMENT_TWO 1 /* 数据段的第二个元素 */ +#define DATA_SEGMENT_THREE 2 /* Third element of the data segment */ +#define DATA_SEGMENT_FOUR 3 /* Fourth element of the data segment */ +#define CUSTACKCODELEN 10 /* Ack code length */ + +/* Command code. */ +#define SET_PID_KP 0x01 /* Set Pid Kp Command Params */ +#define SET_PID_KI 0x02 /* Set Pid Ki Command Params */ +#define SET_PID_LIMIT 0x03 /* Set Pid limit Command Params */ + +#define SET_ENC_MTRPPMR 0x01 /* Set encoder lines */ +#define SET_ENC_ZSHIFT 0x02 /* Set encoder zero point shift */ + +#define SET_SPD_COMMAND_HZ 0x01 /* Set Target Speed Command Params */ +#define SET_SPD_RMG_SLOPE 0x02 /* Set Speed Slope Command Params */ + +#define SET_MAX_ELEC_SPD 0x01 /* Set Max Motor Speed Command Params */ +#define SET_MOTOR_NUM_OF_PAIRS 0x02 /* Set Motor Command Params */ + +#define SET_MOTOR_RES_OF_STATOR 0x01 /* Set Motor Res Command Params */ +#define SET_MOTOR_DAXIS_INDUCTANCE 0x02 /* Set Motor Daxis Inductance Command Params */ +#define SET_MOTOR_QAXIS_INDUCTANCE 0x03 /* Set Motor Qaxis Inductance Command Params */ + +#define SET_SVPWM_VOLTAGE_PER_UNIT 0x01 /* Set Svpwm Voltage Params */ +#define SET_ADC_CURRENT_SAMPLE_COFE 0x02 /* Set Adc coffe Params */ +#define SET_CURRENT_LOOP_CONTROL_PERIOD 0x03 /* Set Current loop period Command Params */ + +#define SET_IF_TARGET_CURRENT_VALUE 0x01 /* Set If Target Params */ +#define SET_INCREMENT_OF_IF_CURRENT 0x02 /* Set If Step Command Params */ +#define SET_SPEED_RING_BEGIN_SPEED 0x03 /* Set If to Smo Start Speed Command Params */ + +static unsigned char ackCode = 0; +static unsigned char g_uartTxBuf[CUSTACKCODELEN] = {0}; + +/** + * @brief Set observer type. + * @param mtrCtrl The motor control handle. + * @param rxData Receive buffer + */ +static void CMDCODE_SetObserverType(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) +{ + /* Get function code. */ + int funcCode = (int)(rxData->data[DATA_SEGMENT_ONE].typeF); + if (funcCode == FOC_OBSERVERTYPE_ENC) { + ackCode = 0X03; + mtrCtrl->obserType = FOC_OBSERVERTYPE_ENC; + CUST_AckCode(g_uartTxBuf, ackCode, mtrCtrl->obserType); + } +} + +/** + * @brief Set pid parameter ack code. + * @param funcCode Received data funccode. + */ +static unsigned char SetPidAckCode(int funcCode) +{ + switch (funcCode) { + /* Set current loop D-Axis PID parameter ack code. */ + case FOC_CURDAXISPID_PARAMS: + ackCode = 0xE0; + break; + /* Set current loop Q-Axis PID parameter ack code. */ + case FOC_CURQAXISPID_PARAMS: + ackCode = 0xE3; + break; + /* Set speed loop PID parameter ack code. */ + case FOC_SPDPID_PARAMS: + ackCode = 0xE6; + break; + default: + break; + } + return ackCode; +} + +/** + * @brief Set pid parameters. + * @param pidHandle The pid control handle. + * @param rxData Receive buffer + */ +static void SetPidParams(PID_Handle *pidHandle, CUSTDATATYPE_DEF *rxData) +{ + /* Get function code. */ + int funcCode = (int)(rxData->data[DATA_SEGMENT_ONE].typeF); + /* Get command code. */ + int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); + switch (cmdCode) { + case SET_PID_KP: /* Set the P parameter. */ + PID_SetKp(pidHandle, rxData->data[DATA_SEGMENT_THREE].typeF); + ackCode = SetPidAckCode(funcCode); + CUST_AckCode(g_uartTxBuf, ackCode + SET_PID_KP, rxData->data[DATA_SEGMENT_THREE].typeF); + break; + case SET_PID_KI: /* Set the I parameter. */ + PID_SetKi(pidHandle, rxData->data[DATA_SEGMENT_THREE].typeF); + ackCode = SetPidAckCode(funcCode); + CUST_AckCode(g_uartTxBuf, ackCode + SET_PID_KI, rxData->data[DATA_SEGMENT_THREE].typeF); + break; + case SET_PID_LIMIT: /* Set the pid limit. */ + PID_SetLimit(pidHandle, rxData->data[DATA_SEGMENT_THREE].typeF); + ackCode = SetPidAckCode(funcCode); + CUST_AckCode(g_uartTxBuf, ackCode + SET_PID_LIMIT, rxData->data[DATA_SEGMENT_THREE].typeF); + break; + default: + ackCode = 0X77; + CUST_AckCode(g_uartTxBuf, ackCode, 0); + break; + } +} + +/** + * @brief Set motor pid parameters. + * @param mtrCtrl The motor control handle. + * @param rxData Receive buffer + */ +static void CMDCODE_SetMotorPidParams(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) +{ + /* Get function code. */ + int funcCode = (int)(rxData->data[DATA_SEGMENT_ONE].typeF); + + if (funcCode == FOC_CURDAXISPID_PARAMS) { + SetPidParams(&mtrCtrl->currCtrl.dAxisPi, rxData); /* Set Curr loop Daxis pid params */ + } else if (funcCode == FOC_CURQAXISPID_PARAMS) { + SetPidParams(&mtrCtrl->currCtrl.qAxisPi, rxData); /* Set Curr loop Qaxis pid params */ + mtrCtrl->currCtrl.dAxisPi.upperLimit = mtrCtrl->currCtrl.qAxisPi.upperLimit; + mtrCtrl->currCtrl.dAxisPi.lowerLimit = mtrCtrl->currCtrl.qAxisPi.lowerLimit; + } else if (funcCode == FOC_SPDPID_PARAMS) { + SetPidParams(&mtrCtrl->spdCtrl.spdPi, rxData); /* Set speed loop params */ + } +} + +/** + * @brief Set encoder observer parameters. + * @param encHandle The observer control handle. + * @param rxData Receive buffer + */ +static void SetObserverEncParams(EncoderHandle *encHandle, CUSTDATATYPE_DEF *rxData) +{ + /* Get command code. */ + int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); + switch (cmdCode) { + case SET_ENC_MTRPPMR: /* Set encoder MTRPPMR. */ + encHandle->pulsePerMechRound = rxData->data[DATA_SEGMENT_THREE].typeF; + ackCode = 0X0D; + CUST_AckCode(g_uartTxBuf, ackCode, encHandle->pulsePerMechRound); + break; + case SET_ENC_ZSHIFT: /* Set encoder zero shift. */ + encHandle->zShift = rxData->data[DATA_SEGMENT_THREE].typeF; + ackCode = 0X0E; + CUST_AckCode(g_uartTxBuf, ackCode, encHandle->zShift); + break; + default: + break; + } +} + +/** + * @brief 设置观察者参数。 + * @param mtrCtrl The motor control handle. + * @param rxData Receive buffer + */ +static void CMDCODE_SetObserverParams(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) +{ + /* Get function code. */ + int funcCode = (int)(rxData->data[DATA_SEGMENT_ONE].typeF); + if (funcCode == FOC_OBSERVERTYPE_ENC) { + SetObserverEncParams(mtrCtrl->encHandle, rxData); + mtrCtrl->encHandle->pulsePerElecRound = mtrCtrl->encHandle->pulsePerMechRound / mtrCtrl->mtrParam.mtrNp; + } +} + +/** + * @brief 设置电机速度和速度斜率。 + * @param mtrCtrl The motor control handle. + * @param rxData Receive buffer + */ +static void CMDCODE_SetMotorSpdAndSlope(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) +{ + /* Get command code. */ + int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); + + switch (cmdCode) { + case SET_SPD_COMMAND_HZ: /* Set target speed(hz). */ + mtrCtrl->spdCmdHz = rxData->data[DATA_SEGMENT_THREE].typeF * mtrCtrl->mtrParam.mtrNp / CONST_VALUE_60; + /* Judgement the value > 0.00001, make sure denominator != 0 */ + if (rxData->data[DATA_SEGMENT_FOUR].typeF > 0.00001f) { + mtrCtrl->spdRmg.delta = mtrCtrl->spdCmdHz / rxData->data[DATA_SEGMENT_FOUR].typeF * CTRL_SYSTICK_PERIOD; + } + ackCode = 0X16; + CUST_AckCode(g_uartTxBuf, ackCode, mtrCtrl->spdCmdHz); + break; + case SET_SPD_RMG_SLOPE: /* Set speed slope. */ + mtrCtrl->spdRmg.delta = mtrCtrl->spdCmdHz / rxData->data[DATA_SEGMENT_THREE].typeF * CTRL_SYSTICK_PERIOD; + ackCode = 0X17; + CUST_AckCode(g_uartTxBuf, ackCode, rxData->data[DATA_SEGMENT_THREE].typeF); + break; + default: + break; + } +} + +/** + * @brief Set motor base parameters. + * @param mtrParamHandle The motor parameters handle. + * @param rxData Receive buffer + */ +static void SetMotorBaseParams(MOTOR_Param *mtrParamHandle, CUSTDATATYPE_DEF *rxData) +{ + /* Get command code. */ + int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); + + switch (cmdCode) { + case SET_MAX_ELEC_SPD: /* Set max electric speed. */ + mtrParamHandle->maxElecSpd = rxData->data[DATA_SEGMENT_THREE].typeF / + CONST_VALUE_60 * mtrParamHandle->mtrNp; + ackCode = 0X18; + CUST_AckCode(g_uartTxBuf, ackCode, rxData->data[DATA_SEGMENT_THREE].typeF); + break; + case SET_MOTOR_NUM_OF_PAIRS: /* Set the number of motor pole pairs. */ + mtrParamHandle->mtrNp = (unsigned short)(rxData->data[DATA_SEGMENT_THREE].typeF); + ackCode = 0X19; + CUST_AckCode(g_uartTxBuf, ackCode, (float)mtrParamHandle->mtrNp); + break; + default: + break; + } +} + +/** + * @brief Set motor special parameters. + * @param mtrParamHandle The motor parameters handle. + * @param rxData Receive buffer + */ +static void SetMotorSpecialParams(MOTOR_Param *mtrParamHandle, CUSTDATATYPE_DEF *rxData) +{ + /* Get command code. */ + int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); + + switch (cmdCode) { + case SET_MOTOR_RES_OF_STATOR: /* Set the resistor of stator. */ + mtrParamHandle->mtrRs = rxData->data[DATA_SEGMENT_THREE].typeF; + ackCode = 0X1A; + CUST_AckCode(g_uartTxBuf, ackCode, mtrParamHandle->mtrRs); + break; + case SET_MOTOR_DAXIS_INDUCTANCE: /* Set the d axis inductance. */ + mtrParamHandle->mtrLd = rxData->data[DATA_SEGMENT_THREE].typeF; + ackCode = 0X1B; + CUST_AckCode(g_uartTxBuf, ackCode, mtrParamHandle->mtrLd); + break; + case SET_MOTOR_QAXIS_INDUCTANCE: /* Set the q axis inductance. */ + mtrParamHandle->mtrLq = rxData->data[DATA_SEGMENT_THREE].typeF; + ackCode = 0X1C; + CUST_AckCode(g_uartTxBuf, ackCode, mtrParamHandle->mtrLq); + break; + default: + ackCode = 0X77; + CUST_AckCode(g_uartTxBuf, ackCode, 0); + break; + } +} + +/** + * @brief Set motor board parameters. + * @param mtrCtrl The motor control handle. + * @param rxData Receive buffer + */ +static void SetMotorBoardParams(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) +{ + /* Get command code. */ + int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); + + switch (cmdCode) { + case SET_SVPWM_VOLTAGE_PER_UNIT: /* Set svpwm voltage per unit. */ + mtrCtrl->sv.voltPu = rxData->data[DATA_SEGMENT_THREE].typeF * ONE_DIV_SQRT3; + mtrCtrl->currCtrl.outLimit = mtrCtrl->sv.voltPu * ONE_DIV_SQRT3; + ackCode = 0X1D; + CUST_AckCode(g_uartTxBuf, ackCode, rxData->data[DATA_SEGMENT_THREE].typeF); + break; + case SET_ADC_CURRENT_SAMPLE_COFE: /* Set adc current sample cofeature. */ + mtrCtrl->adcCurrCofe = rxData->data[DATA_SEGMENT_THREE].typeF; + ackCode = 0X1E; + CUST_AckCode(g_uartTxBuf, ackCode, mtrCtrl->adcCurrCofe); + break; + case SET_CURRENT_LOOP_CONTROL_PERIOD: /* Set current loop control period. */ + mtrCtrl->currCtrlPeriod = 1 / rxData->data[DATA_SEGMENT_THREE].typeF * CONST_VALUE_DIV_1000; + ackCode = 0X1F; + CUST_AckCode(g_uartTxBuf, ackCode, rxData->data[DATA_SEGMENT_THREE].typeF); + break; + default: + break; + } +} + +/** + * @brief Set motor parameters. + * @param mtrCtrl The motor control handle. + * @param rxData Receive buffer + */ +static void CMDCODE_SetMotorParams(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) +{ + /* Get function code. */ + int funcCode = (int)(rxData->data[DATA_SEGMENT_ONE].typeF); + + if (funcCode == MOTOR_PARAMS_BASE) { + SetMotorBaseParams(&mtrCtrl->mtrParam, rxData); + } else if (funcCode == MOTOR_PARAMS_SPECIAL) { + SetMotorSpecialParams(&mtrCtrl->mtrParam, rxData); + } else if (funcCode == MOTOR_PARAMS_BOARD) { + SetMotorBoardParams(mtrCtrl, rxData); + } +} + +/** + * @brief Motor start. + * @param mtrCtrl The motor control handle. + */ +static void CMDCODE_MotorStart(MTRCTRL_Handle *mtrCtrl) +{ + if (mtrCtrl->stateMachine != FSM_RUN) { + SysCmdStartSet(&mtrCtrl->statusReg); /* start motor. */ + mtrCtrl->motorStateFlag = 1; + ackCode = 0X24; /* send ackcode to host. */ + CUST_AckCode(g_uartTxBuf, ackCode, 1); + } +} + +/** + * @brief Motor stop. + * @param mtrCtrl The motor control handle. + */ +static void CMDCODE_MotorStop(MTRCTRL_Handle *mtrCtrl) +{ + SysCmdStopSet(&mtrCtrl->statusReg); + mtrCtrl->motorStateFlag = 0; + ackCode = 0X25; + CUST_AckCode(g_uartTxBuf, ackCode, 0); +} + +/** + * @brief Motor state reset. + * @param mtrCtrl The motor control handle. + */ +static void CMDCODE_MotorReset(MTRCTRL_Handle *mtrCtrl) +{ + BASE_FUNC_UNUSED(mtrCtrl); + BASE_FUNC_SoftReset(); +} + +/** + * @brief Set IF-Startup parameters. + * @param mtrCtrl The motor control handle. + * @param rxData Receive buffer + */ +static void SetStartupIFParams(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) +{ + /* Get command code. */ + int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); + + switch (cmdCode) { + case SET_IF_TARGET_CURRENT_VALUE: /* Set I/F start up target current value. */ + mtrCtrl->ifCtrl.targetAmp = rxData->data[DATA_SEGMENT_THREE].typeF; + ackCode = 0X26; + CUST_AckCode(g_uartTxBuf, ackCode, mtrCtrl->ifCtrl.targetAmp); + break; + case SET_INCREMENT_OF_IF_CURRENT: /* Set increment of I/F start up current. */ + mtrCtrl->ifCtrl.stepAmp = mtrCtrl->ifCtrl.targetAmp / rxData->data[DATA_SEGMENT_THREE].typeF * + CTRL_SYSTICK_PERIOD; + ackCode = 0X27; + CUST_AckCode(g_uartTxBuf, ackCode, mtrCtrl->ifCtrl.stepAmp); + break; + case SET_SPEED_RING_BEGIN_SPEED: /* Set speed ring begin speed. */ + mtrCtrl->startup.spdBegin = rxData->data[DATA_SEGMENT_THREE].typeF / + CONST_VALUE_60 * mtrCtrl->mtrParam.mtrNp; + ackCode = 0X28; + CUST_AckCode(g_uartTxBuf, ackCode, rxData->data[DATA_SEGMENT_THREE].typeF); + break; + default: + break; + } +} + +/** + * @brief Set start up parameters. + * @param mtrCtrl The motor control handle. + * @param rxData Receive buffer + */ +static void CMDCODE_SetStartupParams(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) +{ + /* Get function code. */ + int funcCode = (int)(rxData->data[DATA_SEGMENT_ONE].typeF); + + if (funcCode == FOC_STARTUP_IF) { + SetStartupIFParams(mtrCtrl, rxData); + } +} + +/** + * @brief Set adjust speed mode. + * @param mtrCtrl The motor control handle. + * @param rxData Receive buffer + */ +static void CMDCODE_SetAdjustSpdMode(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) +{ + /* Get function code. */ + int funcCode = (int)(rxData->data[DATA_SEGMENT_ONE].typeF); + /* Get commond code. */ + int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); + if (funcCode == HOST_SPEED_ADJUST) { + mtrCtrl->spdAdjustMode = HOST_SPEED_ADJUST; + /* Uart connect success. */ + mtrCtrl->uartConnectFlag = CONNECTING; + ackCode = 0X2A; + CUST_AckCode(g_uartTxBuf, ackCode, (float)mtrCtrl->spdAdjustMode); + } else if (funcCode == CUST_SPEED_ADJUST) { + if (cmdCode == 1) { /* If uart connection disconnected & stop motor commond. */ + SysCmdStopSet(&mtrCtrl->statusReg); + mtrCtrl->motorStateFlag = 0; + } + mtrCtrl->spdAdjustMode = CUST_SPEED_ADJUST; + ackCode = 0X2B; + CUST_AckCode(g_uartTxBuf, ackCode, (float)mtrCtrl->spdAdjustMode); + /* Uart disconnect. */ + mtrCtrl->uartConnectFlag = DISCONNECT; + } +} + +/** + * @brief Check uart connect. + * @param mtrCtrl The motor control handle. + * @param rxData Receive buffer + */ +static void CMDCODE_UartHandShake(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) +{ + CMDCODE_SetAdjustSpdMode(mtrCtrl, rxData); +} + +/** + * @brief Set Motor Initial Status Parameters. + * @param mtrCtrl The motor control handle. + * @param rxData Receive buffer + * @param code Instruction code. + */ +static void CMDCODE_EXE_SetMotorInitParams(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData, unsigned char code) +{ + switch (code) { + case CMDCODE_SET_OBSERVER_TYPE: { /* Set observer type. */ + CMDCODE_SetObserverType(mtrCtrl, rxData); + } + break; + case CMDCODE_SET_PID_PARAMS: { /* Set motor pid parameters. */ + CMDCODE_SetMotorPidParams(mtrCtrl, rxData); + } + break; + case CMDCODE_SET_OBSERVER_PARAMS: { /* Set observer parameters. */ + CMDCODE_SetObserverParams(mtrCtrl, rxData); + } + break; + case CMDCODE_SET_MOTOR_TARGETSPD: { /* Set motor speed and speed slope. */ + CMDCODE_SetMotorSpdAndSlope(mtrCtrl, rxData); + } + break; + case CMDCODE_SET_MOTOR_PARAMS: { /* Set motor parameters. */ + CMDCODE_SetMotorParams(mtrCtrl, rxData); + } + break; + case CMDCODE_SET_ADJUSTSPD_MODE: /* Set adjust speed mode. */ + CMDCODE_SetAdjustSpdMode(mtrCtrl, rxData); + break; + default: + break; + } +} + +/** + * @brief Set Motor Control System Status. + * @param mtrCtrl The motor control handle. + * @param rxData Receive buffer + * @param code Instruction code. + */ +static void CMDCODE_EXE_SetMotorState(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData, unsigned char code) +{ + BASE_FUNC_UNUSED(rxData); + switch (code) { + case CMDCODE_MOTOR_START: { /* Motor start command */ + CMDCODE_MotorStart(mtrCtrl); + } + break; + case CMDCODE_MOTOR_STOP: { /* Motor stop command */ + CMDCODE_MotorStop(mtrCtrl); + } + break; + case CMDCODE_MOTORSTATE_RESET: { /* Motor reset command */ + CMDCODE_MotorReset(mtrCtrl); + } + break; + default: + break; + } +} + +/** + * @brief Set Startup and Uart Link Handshake Flag Parameters. + * @param mtrCtrl The motor control handle. + * @param rxData Receive buffer + * @param code Instruction code. + */ +static void CMDCODE_EXE_SetOtherParams(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData, unsigned char code) +{ + switch (code) { + case CMDCODE_SET_STARTUP_PARAMS: /* Set start up parameters. */ + CMDCODE_SetStartupParams(mtrCtrl, rxData); + break; + case CMDCODE_UART_HANDSHAKE: { /* Check uart hand shake. */ + CMDCODE_UartHandShake(mtrCtrl, rxData); + } + break; + case CMDCODE_UART_HEARTDETECT: { /* Check uart hand shake. */ + mtrCtrl->uartHeartDetCnt++; + } + break; + default: + break; + } +} +/** + * @brief Instruction code executor. + * @param mtrCtrl The motor control handle. + * @param rxData Receive buffer + * @param code Instruction code. + */ +static void CMDCODE_EXE_Process(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData, unsigned char code) +{ + CMDCODE_EXE_SetMotorInitParams(mtrCtrl, rxData, code); + CMDCODE_EXE_SetMotorState(mtrCtrl, rxData, code); + CMDCODE_EXE_SetOtherParams(mtrCtrl, rxData, code); +} + +/** + * @brief Host data download callback and data parsing. + * @param mtrCtrl The motor control handle. + * @param rxBuf Receive buffer + */ +void CUST_UartDataProcess(MTRCTRL_Handle *mtrCtrl, unsigned char *rxBuf) +{ + /* Verify Parameters */ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + MCS_ASSERT_PARAM(rxBuf != NULL); + + /* Uart data storage struct */ + CUSTDATATYPE_DEF data; + volatile unsigned char *ptrCnt = &rxBuf[FRAME_CHECK_BEGIN + 1]; + volatile char *strCnt = &data.data[0].typeCh[0]; + for (unsigned int j = 0; j < FRAME_RECV_DATA_LENTH * FRAME_ONE_DATA_LENTH; j++) { + *strCnt++ = *ptrCnt++; + } + /* Message function code. */ + data.code = rxBuf[FRAME_CHECK_BEGIN]; + CMDCODE_EXE_Process(mtrCtrl, &data, data.code); +} + +/** + * @brief The host computer displays data transmission. + * @param mtrCtrl The motor control handle. + * @param txData Message content. + * @param stage Message status function code. + */ +void CUST_SetTxMsg(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *txData) +{ + /* Verify Parameters */ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + MCS_ASSERT_PARAM(txData != NULL); + /* Data send to host. */ + txData->data[CURRDQ_Q].typeF = mtrCtrl->idqFbk.q; + txData->data[CURRDQ_D].typeF = mtrCtrl->idqFbk.d; + txData->data[CURRREFDQ_Q].typeF = mtrCtrl->idqRef.q; + txData->data[CURRREFDQ_D].typeF = mtrCtrl->idqRef.d; + /* Motor current speed. */ + txData->data[CURRSPD].typeF = mtrCtrl->encSpeed * CONST_VALUE_60 / mtrCtrl->mtrParam.mtrNp; + /* Motor commond speed. */ + txData->data[SPDCMDHZ].typeF = mtrCtrl->spdCmdHz * CONST_VALUE_60 / mtrCtrl->mtrParam.mtrNp; + /* Bus voltage. */ + txData->data[UDC].typeF = mtrCtrl->udc; + /* Power board temprature. */ + txData->data[POWERBOARDTEMP].typeF = mtrCtrl->powerBoardTemp; + /* Motor protection status flag. */ + txData->data[CUST_ERR_CODE].typeI = mtrCtrl->prot.motorErrStatus.all; + /* Three phase current. */ + txData->data[CURRUVW_U].typeF = mtrCtrl->currUvw.u; + txData->data[CURRUVW_V].typeF = mtrCtrl->currUvw.v; + txData->data[CURRUVW_W].typeF = mtrCtrl->currUvw.w; + /* Three phase pwm duty. */ + txData->data[PWMDUTYUVW_U].typeF = mtrCtrl->dutyUvw.u; + txData->data[PWMDUTYUVW_V].typeF = mtrCtrl->dutyUvw.v; + txData->data[PWMDUTYUVW_W].typeF = mtrCtrl->dutyUvw.w; + /* Motor electric angle. */ + txData->data[AXISANGLE].typeF = mtrCtrl->axisAngle; + txData->data[VDQ_Q].typeF = mtrCtrl->vdqRef.q; + txData->data[VDQ_D].typeF = mtrCtrl->vdqRef.d; + txData->data[SPDREFHZ].typeF = mtrCtrl->spdRefHz * CONST_VALUE_60 / mtrCtrl->mtrParam.mtrNp; + txData->data[SENDTIMESTAMP].typeF = mtrCtrl->uartTimeStamp; +} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/user_interface/cust_process.h b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/user_interface/cust_process.h new file mode 100644 index 00000000..60fe69bb --- /dev/null +++ b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/user_interface/cust_process.h @@ -0,0 +1,36 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file cust_process.h + * @author MCU Algorithm Team + * @brief This file provides functions declaration of system status. + */ + +/* Define to prevent recursive inclusion ------------------------------------------------------- */ +#ifndef McsMagicTag_HMI_MOUDLE_H +#define McsMagicTag_HMI_MOUDLE_H + +/* Includes ------------------------------------------------------------------------------------ */ +#include "protocol.h" +/* Typedef definitions ------------------------------------------------------------------------- */ + + +void CUST_UartDataProcess(MTRCTRL_Handle *mtrCtrl, unsigned char *rxBuf); + +void CUST_SetTxMsg(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *txData); + +#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/user_interface/hmi_module.c b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/user_interface/hmi_module.c new file mode 100644 index 00000000..f6f762ad --- /dev/null +++ b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/user_interface/hmi_module.c @@ -0,0 +1,58 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file hmi_module.c + * @author MCU Algorithm Team + * @brief This file provides functions declaration of human-machine interface. + */ + +#include "hmi_module.h" +#include "mcs_assert.h" + +/** + * @brief HMI Initializatio. + * @retval None. + */ +void HMI_Init(void) +{ + UartRecvInit(); + +} + +/** + * @brief HMI processing. + * @param mtrCtrl The motor control handle.. + * @retval None. + */ +void HMI_Process_Rx(MTRCTRL_Handle *mtrCtrl) +{ + /* Verify Parameters */ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + UartModuleProcess_Rx(mtrCtrl); +} + +/** + * @brief HMI processing. + * @param mtrCtrl The motor control handle.. + * @retval None. + */ +void HMI_Process_Tx(MTRCTRL_Handle *mtrCtrl) +{ + /* Verify Parameters */ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + UartModuleProcess_Tx(mtrCtrl); +} diff --git a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/user_interface/hmi_module.h b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/user_interface/hmi_module.h new file mode 100644 index 00000000..056f8d73 --- /dev/null +++ b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/user_interface/hmi_module.h @@ -0,0 +1,37 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file hmi_module.h + * @author MCU Algorithm Team + * @brief This file provides functions declaration of system status. + */ + +/* Define to prevent recursive inclusion ------------------------------------------------------- */ +#ifndef McsMagicTag_HMI_MOUDLE_H +#define McsMagicTag_HMI_MOUDLE_H + +/* Includes ------------------------------------------------------------------------------------ */ +#include "uart_module.h" +/* Typedef definitions ------------------------------------------------------------------------- */ + + +void HMI_Init(void); + +void HMI_Process_Rx(MTRCTRL_Handle *mtrCtrl); +void HMI_Process_Tx(MTRCTRL_Handle *mtrCtrl); + +#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/user_interface/protocol.c b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/user_interface/protocol.c new file mode 100644 index 00000000..987b4c5a --- /dev/null +++ b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/user_interface/protocol.c @@ -0,0 +1,174 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file protocol.c + * @author MCU Algorithm Team + * @brief This file provides functions declaration of port communication. + */ + +#include "protocol.h" +#include "apt.h" +#include "typedefs.h" +#include "main.h" +#include "mcs_assert.h" +#include "cust_process.h" + +/** + * @brief Callback function for receiving data analysis and processing. + * @param rxBuf Receive buffer. + */ +__weak void CUST_UartDataProcess(MTRCTRL_Handle *mtrCtrl, unsigned char *rxBuf) +{ + BASE_FUNC_UNUSED(mtrCtrl); + BASE_FUNC_UNUSED(rxBuf); +} +/** + * @brief User-defined protocol message sending function (weak function). + * @param rxData Sending Messages.. + */ +__weak void CUST_SetTxMsg(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *txData) +{ + BASE_FUNC_UNUSED(mtrCtrl); + BASE_FUNC_UNUSED(*txData); +} + +static void (*g_ptrDataProcess)(MTRCTRL_Handle *mtrCtrl, unsigned char *rxBuf) = CUST_UartDataProcess; + +/** + * @brief Frame checksum. + * @param ptr Pointer to the data to be checked + * @param num Number of bytes + * @retval unsigned char Checksum + */ +static unsigned char CheckSum(unsigned char *ptr, unsigned char num) +{ + unsigned char sum = 0; + unsigned char *p = ptr; + /* Calculate the sum of received data. */ + for (int i = 0; i < num; i++) { + sum += *p; + p++; + } + return sum; +} + +/** + * @brief Transmitting Data Frames. + * @param mtrCtrl The motor control handle. + * @param txBuf Sending Messages. + */ +unsigned int CUST_TransmitData(MTRCTRL_Handle *mtrCtrl, unsigned char *txBuf) +{ + /* Verify Parameters */ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + MCS_ASSERT_PARAM(txBuf != NULL); + unsigned int dataLen = FRAME_ONE_DATA_LENTH * SEND_FRAME_DATA_NUM; + unsigned int i = 0; + CUSTDATATYPE_DEF txData = {0}; + CUST_SetTxMsg(mtrCtrl, &txData); + txBuf[i++] = FRAME_START; + /* Message function code */ + txBuf[i++] = FRAME_SENT; + /* Message data */ + for (unsigned char x = 0; x < SEND_FRAME_DATA_NUM; x++) { + int floatIndex = 0; + int byteOffset = i; + txBuf[x * FRAME_ONE_DATA_LENTH + byteOffset++] = txData.data[x].typeCh[floatIndex++]; + txBuf[x * FRAME_ONE_DATA_LENTH + byteOffset++] = txData.data[x].typeCh[floatIndex++]; + txBuf[x * FRAME_ONE_DATA_LENTH + byteOffset++] = txData.data[x].typeCh[floatIndex++]; + txBuf[x * FRAME_ONE_DATA_LENTH + byteOffset++] = txData.data[x].typeCh[floatIndex++]; + } + /* Message verification domain */ + txBuf[dataLen + i++] = CheckSum((unsigned char*)&txBuf[FRAME_CHECK_BEGIN], dataLen + 1); + /* End of Message */ + txBuf[dataLen + i++] = FRAME_END; + return dataLen + i; +} + +/** + * @brief Transmitting Data Frames. + * @param txBuf Sending Cust Ack Code. + * @param ackCode Ack Code. + * @param varParams Host set parameter. + */ +void CUST_AckCode(unsigned char *txBuf, unsigned char ackCode, float varParams) +{ + /* Verify Parameters */ + MCS_ASSERT_PARAM(txBuf != NULL); + CUSTDATATYPE_DEF txData = {0}; + int dataIndex = 0; + unsigned int i = 0; + unsigned int txLen = 0; + unsigned int dataLen = FRAME_ONE_CHAR_LENTH + FRAME_ONE_DATA_LENTH; + + txData.data[0].typeF = varParams; + txBuf[i++] = FRAME_START; + /* Message function code */ + txBuf[i++] = FRAME_CUSTACK; + /* Message ack code */ + txBuf[i++] = ackCode; + /* Message data */ + txBuf[i++] = txData.data[0].typeCh[dataIndex++]; + txBuf[i++] = txData.data[0].typeCh[dataIndex++]; + txBuf[i++] = txData.data[0].typeCh[dataIndex++]; + txBuf[i++] = txData.data[0].typeCh[dataIndex++]; + + /* Message verification domain */ + txBuf[FRAME_ONE_CHAR_LENTH + i++] = CheckSum((unsigned char*)&txBuf[FRAME_CHECK_BEGIN], dataLen + 1); + /* End of Message */ + txBuf[FRAME_ONE_CHAR_LENTH + i++] = FRAME_END; + txLen = FRAME_ONE_CHAR_LENTH + i++; + HAL_UART_WriteIT(&g_uart0, txBuf, txLen); +} + +/** + * @brief Cust receive data process. + * @param mtrCtrl The motor control handle. + * @param rxBuf Receive buffer + */ +void CUST_DataReceProcss(MTRCTRL_Handle *mtrCtrl, unsigned char *rxBuf) +{ + /* Verify Parameters */ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + MCS_ASSERT_PARAM(rxBuf != NULL); + unsigned char g_uartTxBuf[10] = {0}; + unsigned char ackCode = 0; + /* Frame header check */ + if (rxBuf[0] != FRAME_START) { + ackCode = 0X78; + CUST_AckCode(g_uartTxBuf, ackCode, 0); + return; + } + /* Frame trailer check */ + if (rxBuf[FRAME_LENTH - 1] != FRAME_END) { + ackCode = 0X79; + CUST_AckCode(g_uartTxBuf, ackCode, 0); + return; + } + /* Checksum */ + if (CheckSum((unsigned char*)&rxBuf[FRAME_CHECK_BEGIN], FRAME_CHECK_NUM) != rxBuf[FRAME_CHECKSUM]) { + ackCode = 0X7A; + CUST_AckCode(g_uartTxBuf, ackCode, 0); + return; + } else { + if (g_ptrDataProcess == NULL) { + return; + } else { + g_ptrDataProcess(mtrCtrl, rxBuf); + } + } +} diff --git a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/user_interface/protocol.h b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/user_interface/protocol.h new file mode 100644 index 00000000..33f0f49c --- /dev/null +++ b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/user_interface/protocol.h @@ -0,0 +1,121 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file protocol.h + * @author MCU Algorithm Team + * @brief This file provides functions declaration of port communication. + */ +#ifndef McsMagicTag_PORTOCOL_H +#define McsMagicTag_PORTOCOL_H + +#include "uart.h" +#include "mcs_carrier.h" + +#define RS485_SEND_ENABLE GPIO6->GPIO_DATA[GPIO_PIN_7].reg = GPIO_PIN_7 +#define RS485_SEND_DISABLE GPIO6->GPIO_DATA[GPIO_PIN_7].reg = BASE_CFG_UNSET + +#define RX_BUF_LEN (16) +#define SEND_FRAME_DATA_NUM (CUSTDATA_MAX) +/* Service Uart0 Communication Deal */ +#define FRAME_ONE_DATA_LENTH 4 +#define FRAME_ONE_CHAR_LENTH 1 +#define FRAME_RECV_DATA_LENTH 4 +#define FRAME_LENTH 20 /* Data length */ +#define FRAME_SENT 0X8F +#define FRAME_CUSTACK 0X8A +#define FRAME_START 0x0F /* Start frame */ +#define FRAME_END '/' /* StOP frame */ +#define FRAME_CHECK_BEGIN 1 /* Check frame */ +#define FRAME_CHECKSUM 18 /* Check sum */ +#define FRAME_CHECK_NUM 17 +#define CMDCODE_IDLE_FRAME 0x55 /* Fill frame */ +#define CMDCODE_GET_MOTOR_PARAMS 0x01 +#define CMDCODE_SEND_MOTOR_PARAMS 0x02 +#define CMDCODE_SET_MOTOR_CTLMODE 0x03 +#define CMDCODE_SET_OBSERVER_TYPE 0x04 +#define CMDCODE_SET_STARTUP_MODE 0x05 +#define CMDCODE_SET_PID_PARAMS 0x06 +#define CMDCODE_SET_STARTUP_PARAMS 0x07 +#define CMDCODE_SET_OBSERVER_PARAMS 0x08 +#define CMDCODE_SET_MOTOR_TARGETSPD 0x09 +#define CMDCODE_SET_MOTOR_PARAMS 0x0A +#define CMDCODE_MOTOR_START 0x0B +#define CMDCODE_MOTOR_STOP 0x0C +#define CMDCODE_MOTORSTATE_RESET 0x0D +#define CMDCODE_SEND_FIRMVERSION 0x0E +#define CMDCODE_SET_ADJUSTSPD_MODE 0x11 +#define CMDCODE_UART_HANDSHAKE 0x12 +#define CMDCODE_UART_HEARTDETECT 0x13 + +typedef union { + char typeCh[4]; + float typeF; + int typeI; +} UNIONDATATYPE_DEF; + +typedef enum { + OFFLINE_RES = 0, + OFFLINE_LD, + OFFLINE_LQ, + OFFLINE_PSIF, + OFFLINE_JS, + OFFLINE_NP, + OFFLINE_B, + OFFLINE_KPD, + OFFLINE_KID, + OFFLINE_KPQ, + OFFLINE_KIQ, + OFFLINE_KPS, + OFFLINE_KIS, + OFFLINE_SPEED, + OFLINE_MAX +} OFFLINE_IDEN_TYPE; + +typedef enum { + CURRDQ_Q = 0, + CURRDQ_D, + CURRREFDQ_Q, + CURRREFDQ_D, + CURRSPD, + SPDCMDHZ, + UDC, + POWERBOARDTEMP, + CUST_ERR_CODE, + CURRUVW_U, + CURRUVW_V, + CURRUVW_W, + PWMDUTYUVW_U, + PWMDUTYUVW_V, + PWMDUTYUVW_W, + AXISANGLE, + VDQ_Q, + VDQ_D, + SPDREFHZ, + SENDTIMESTAMP, + CUSTDATA_MAX +} SENDTOHOSTPARAMS; + +typedef struct { + volatile unsigned char code; + volatile UNIONDATATYPE_DEF data[SEND_FRAME_DATA_NUM]; +} CUSTDATATYPE_DEF; + + +void CUST_DataReceProcss(MTRCTRL_Handle *mtrCtrl, unsigned char *rxBuf); +unsigned int CUST_TransmitData(MTRCTRL_Handle *mtrCtrl, unsigned char *txBuf); +void CUST_AckCode(unsigned char *txBuf, unsigned char ackCode, float varParams); +#endif /* McsMagicTag_PORTOCOL_H */ diff --git a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/user_interface/uart_module.c b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/user_interface/uart_module.c new file mode 100644 index 00000000..8ded8180 --- /dev/null +++ b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/user_interface/uart_module.c @@ -0,0 +1,234 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the + * following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file uart_module.c + * @author MCU Algorithm Team + * @brief This file provides functions declaration of Serial port communication. + */ +#include "uart_module.h" +#include "debug.h" +#include "main.h" +#include "baseinc.h" +#include +/* Buffer size */ +#define UI_TX_BUF_LEN (96) +#define UI_RX_BUF_LEN (96) + +/* Receiving Timeout Interval */ +#define UART_TIME_OUT_MS (100) + +/* Start sending data to host delay after uart connect success */ +#define UART_UPDATA_DELAY_TIME_MS (50) + +/* Uart baudrate */ +#define UART0BAUDRATE (115200) + +/* Data buffer */ +unsigned char g_uartRxBuf[UI_RX_BUF_LEN] = {0}; +unsigned char g_uartTxBuf[UI_TX_BUF_LEN] = {0}; +static unsigned int getdeltaSystickCnt = 0; +static FRAME_Handle g_uartFrame; +__UINT32_TYPE__ uart_spdcmd;//串口速度命令 +double uart_poscmd; //串口位置命令 + +/** + * @brief Receive Data Clear. + * @param uartFrame Receice Data. + */ +static void FrameRecvClear(FRAME_Handle *uartFrame) +{ + /* Clear buffer lenth. */ + uartFrame->buffLen = 0; + uartFrame->timeOutCnt = 0; + uartFrame->frameFlag = 0; + /* Clear received data lenth. */ + uartFrame->rxLen = 0; + /* Clear received flag. */ + uartFrame->rxFlag = 0; + uartFrame->upDataCnt = 0; +} + +/** + * @brief Set Dma status. + * @param mtrCtrl The motor control handle. + */ +static void SetUartDmaStatus(MTRCTRL_Handle *mtrCtrl) +{ + /* Delay 50ms start uart Tx DMA . */ + if (mtrCtrl->uartConnectFlag == CONNECTING && g_uartFrame.upDataDelayCnt++ > UART_UPDATA_DELAY_TIME_MS) { + g_uartFrame.txFlag = 1; /* Start send data flag. */ + mtrCtrl->uartConnectFlag = CONNECTED; + g_uartFrame.upDataDelayCnt = 0; + } + if (mtrCtrl->uartConnectFlag == DISCONNECT) { + g_uartFrame.txFlag = 0; /* Stop send data flag. */ + mtrCtrl->uartTimeStamp = 0; + } +} + +/** + * @brief Set uart baudRate. + * @param baudrate Uart baudRate. + */ +static void SetUartBaudRate(unsigned int baudrate) +{ + /* Re_Write uart0 baudrate. */ + g_uart0.baudRate = baudrate; + HAL_UART_Init(&g_uart0); +} + +/** + * @brief Uart Dma interupt callback func. + * @param null. + */ +void UART0_TXDMACallback(void *handle) +{ + BASE_FUNC_UNUSED(handle); + unsigned int getCurSystickCnt = 0; + static unsigned int getlastSystickCnt = 0; + /* USER CODE BEGIN UART0_WRITE_DMA_FINISH */ + g_uartFrame.txFlag = 1; + getCurSystickCnt = DCL_SYSTICK_GetTick(); + if (getlastSystickCnt != 0) { + /* Calculate unit frame data send time */ + getdeltaSystickCnt = getCurSystickCnt - getlastSystickCnt; + } + getlastSystickCnt = getCurSystickCnt; + /* USER CODE END UART0_WRITE_DMA_FINISH */ +} + +/** + * @brief Uart0 interruput Write CallBack Function. + * @param handle Uart handle. + */ +void UART0WriteInterruptCallback(void *handle) +{ + BASE_FUNC_UNUSED(handle); + /* USER CODE BEGIN UART0_WRITE_IT_FINISH */ + g_uartFrame.uartItTxFlag = 1; + g_uartFrame.txFlag = 1; + /* USER CODE END UART0_WRITE_IT_FINISH */ +} + +__UINT32_TYPE__ ascii_to_int_manual(const __UINT8_TYPE__ *buf, __UINT8_TYPE__ len) { + __UINT32_TYPE__ result = 0; + bool is_negative = false; + __UINT8_TYPE__ i = 0; + + // 检查符号位 + if (len > 0 && buf[0] == '-') { + is_negative = true; + i = 1; // 跳过符号位 + } + + // 逐字符转换 + for (; i < len; i++) { + if (buf[i] < '0' || buf[i] > '9') { + break; // 遇到非数字字符停止 + } + + // 检查溢出 + // if (result > (INT32_MAX - (buf[i] - '0')) / 10) { + // return is_negative ? INT32_MIN : INT32_MAX; + // } + + result = result * 10 + (buf[i] - '0'); + } + + return is_negative ? -result : result; +} + + + +/** + * @brief Uart0 Interruput Read CallBack Function. + * @param handle Uart handle. + */ +void UART0ReadInterruptCallback(void *handle) +{ + BASE_FUNC_UNUSED(handle); + /* USER CODE BEGIN UART0_READ_IT_FINISH */ + if (g_uartFrame.rxLen >= UI_RX_BUF_LEN - 1) { + g_uartFrame.rxLen = 0; + } + HAL_UART_ReadIT(handle, &g_uartFrame.rxData, 1); + g_uartRxBuf[g_uartFrame.rxLen] = g_uartFrame.rxData; + + uart_spdcmd=ascii_to_int_manual(g_uartRxBuf, 3);//字符串转整型 + uart_poscmd=atof(g_uartRxBuf);//字符串转浮点 + g_uartFrame.rxLen++; + g_uartFrame.rxFlag = 1; + g_uartFrame.uartItTxFlag = 0; + return; + /* USER CODE END UART0_READ_IT_FINISH */ +} + +/** + * @brief Uart Read Data Init Function. + * @param void. + */ +void UartRecvInit(void) +{ + /* Uart reception initialization */ + FrameRecvClear(&g_uartFrame); + SetUartBaudRate(UART0BAUDRATE); + HAL_UART_ReadIT(&g_uart0, &g_uartFrame.rxData, 1); +} + +/** + * @brief Uart Read Data Process Function. + * @param mtrCtrl The motor control handle. + */ +void UartModuleProcess_Rx(MTRCTRL_Handle *mtrCtrl) +{ + /* Verify Parameters */ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + SetUartDmaStatus(mtrCtrl); + if (g_uartFrame.rxFlag == 1) { /* Receive data flag. */ + if (g_uartFrame.timeOutCnt++ > UART_TIME_OUT_MS) { + /* Received data from the host. */ + g_uartFrame.frameFlag = 1; + g_uartFrame.rxFlag = 0; + g_uartFrame.timeOutCnt = 0; + /* Execute data process. */ + CUST_DataReceProcss(mtrCtrl, g_uartRxBuf); + g_uartFrame.rxLen = 0; + } + } + g_uartFrame.frameFlag = 0; +} + +/** + * @brief Uart Write Data Process Function. + * @param mtrCtrl The motor control handle. + */ +void UartModuleProcess_Tx(MTRCTRL_Handle *mtrCtrl) +{ + /* Verify Parameters */ + MCS_ASSERT_PARAM(mtrCtrl != NULL); + if (g_uartFrame.txFlag == 1) { /* Send data flag. */ + mtrCtrl->uartTimeStamp = (float)getdeltaSystickCnt; /* Unit data time stamp */ + g_uartFrame.upDataCnt = 0; + g_uartFrame.txFlag = 0; + /* Send data to host. */ + unsigned int txLen = CUST_TransmitData(mtrCtrl, g_uartTxBuf); + /* If txIT mode send data finish, convert to DMA mode */ + if (g_uartFrame.uartItTxFlag == 1) { + HAL_UART_WriteDMA(&g_uart0, g_uartTxBuf, txLen); + } + } +} diff --git a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/user_interface/uart_module.h b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/user_interface/uart_module.h new file mode 100644 index 00000000..fce60120 --- /dev/null +++ b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/user_interface/uart_module.h @@ -0,0 +1,48 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file uart_module.h + * @author MCU Algorithm Team + * @brief This file provides functions declaration of Serial port communication. + */ +#ifndef McsMagicTag_UART_MODULE_H +#define McsMagicTag_UART_MODULE_H + +#include "protocol.h" +#include "mcs_ctlmode_config.h" + +extern __UINT32_TYPE__ uart_spdcmd; +extern double uart_poscmd; +typedef struct { + unsigned int buffLen; + unsigned int timeOutCnt; + unsigned char frameFlag; + unsigned int rxLen; + unsigned char rxFlag; + unsigned char txFlag; + unsigned char rxData; + unsigned int upDataCnt; + unsigned int upDataDelayCnt; + unsigned char uartItTxFlag; +} FRAME_Handle; + + +void UartRecvInit(void); +void UartModuleProcess_Rx(MTRCTRL_Handle *mtrCtrl); +void UartModuleProcess_Tx(MTRCTRL_Handle *mtrCtrl); + +#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/sample_gpio_key/generatecode/system_init.c b/vendor/yibaina_3061M/demo/sample_gpio_key/generatecode/system_init.c deleted file mode 100644 index 24a14c9c..00000000 --- a/vendor/yibaina_3061M/demo/sample_gpio_key/generatecode/system_init.c +++ /dev/null @@ -1,124 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file system_init.c - * @author MCU Driver Team - * @brief This file contains driver init functions. - */ - -#include "main.h" -#include "ioconfig.h" -#include "iocmg_ip.h" - -#define UART0_BAND_RATE 115200 - -BASE_StatusType CRG_Config(CRG_CoreClkSelect *coreClkSelect) -{ - CRG_Handle crg; - crg.baseAddress = CRG; - crg.pllRefClkSelect = CRG_PLL_REF_CLK_SELECT_HOSC; - crg.pllPreDiv = CRG_PLL_PREDIV_4; - crg.pllFbDiv = 48; /* PLL Multiplier 48 */ - crg.pllPostDiv = CRG_PLL_POSTDIV_2; - crg.coreClkSelect = CRG_CORE_CLK_SELECT_PLL; - crg.handleEx.pllPostDiv2 = CRG_PLL_POSTDIV2_3; - crg.handleEx.clk1MSelect = CRG_1M_CLK_SELECT_HOSC; - crg.handleEx.clk1MDiv = (25 - 1); /* The 1 MHz freq is equal to the input clock frequency / (clk_1m_div + 1). 25 is the div of the clk_1m in CLOCK. */ - - if (HAL_CRG_Init(&crg) != BASE_STATUS_OK) { - return BASE_STATUS_ERROR; - } - *coreClkSelect = crg.coreClkSelect; - return BASE_STATUS_OK; -} - -__weak void GPIO_CallBackFunc(void *param) -{ - GPIO_Handle *handle = (GPIO_Handle *)param; - BASE_FUNC_UNUSED(handle); -} - -static void GPIO_Init(void) -{ - HAL_CRG_IpEnableSet(GPIO2_BASE, IP_CLK_ENABLE); - g_gpio2.baseAddress = GPIO2; - - g_gpio2.pins = GPIO_PIN_4; - HAL_GPIO_Init(&g_gpio2); - HAL_GPIO_SetDirection(&g_gpio2, g_gpio2.pins, GPIO_INPUT_MODE); - HAL_GPIO_SetValue(&g_gpio2, g_gpio2.pins, GPIO_LOW_LEVEL); - HAL_GPIO_SetIrqType(&g_gpio2, g_gpio2.pins, GPIO_INT_TYPE_RISE_EDGE); - - HAL_GPIO_RegisterCallBack(&g_gpio2, GPIO_PIN_4, GPIO_CallBackFunc); - IRQ_Register(IRQ_GPIO2, HAL_GPIO_IrqHandler, &g_gpio2); - IRQ_SetPriority(IRQ_GPIO2, 1); /* set gpio1 interrupt priority to 1, 1~15. 1 is priority value */ - IRQ_EnableN(IRQ_GPIO2); /* gpio interrupt enable */ - - return; -} - -static void UART0_Init(void) -{ - HAL_CRG_IpEnableSet(UART0_BASE, IP_CLK_ENABLE); /* UART0 clock enable. */ - g_uart0.baseAddress = UART0; - - g_uart0.baudRate = UART0_BAND_RATE; - g_uart0.dataLength = UART_DATALENGTH_8BIT; - g_uart0.stopBits = UART_STOPBITS_ONE; - g_uart0.parity = UART_PARITY_NONE; - g_uart0.txMode = UART_MODE_BLOCKING; - g_uart0.rxMode = UART_MODE_BLOCKING; - g_uart0.fifoMode = BASE_CFG_ENABLE; - g_uart0.fifoTxThr = UART_FIFODEPTH_SIZE8; - g_uart0.fifoRxThr = UART_FIFODEPTH_SIZE8; - g_uart0.hwFlowCtr = BASE_CFG_DISABLE; - g_uart0.handleEx.overSampleMultiple = UART_OVERSAMPLING_16X; - g_uart0.handleEx.msbFirst = BASE_CFG_DISABLE; - HAL_UART_Init(&g_uart0); -} - -static void IOConfig(void) -{ - /* Config PIN21 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO2_4_AS_GPIO2_4); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO2_4_AS_GPIO2_4, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO2_4_AS_GPIO2_4, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO2_4_AS_GPIO2_4, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO2_4_AS_GPIO2_4, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN39 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO0_3_AS_UART0_TXD); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO0_3_AS_UART0_TXD, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO0_3_AS_UART0_TXD, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO0_3_AS_UART0_TXD, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO0_3_AS_UART0_TXD, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN40 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO0_4_AS_UART0_RXD); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO0_4_AS_UART0_RXD, PULL_UP); /* Pull-up and Pull-down, UART RX recommend PULL_UP */ - HAL_IOCMG_SetPinSchmidtMode(GPIO0_4_AS_UART0_RXD, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO0_4_AS_UART0_RXD, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO0_4_AS_UART0_RXD, DRIVER_RATE_2); /* Output signal edge fast/slow */ -} - -void SystemInit(void) -{ - IOConfig(); - UART0_Init(); - GPIO_Init(); - - /* USER CODE BEGIN system_init */ - /* USER CODE END system_init */ -} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/sample_gpio_key/readme.md b/vendor/yibaina_3061M/demo/sample_gpio_key/readme.md deleted file mode 100644 index fb6ae936..00000000 --- a/vendor/yibaina_3061M/demo/sample_gpio_key/readme.md +++ /dev/null @@ -1,78 +0,0 @@ -# sample_gpio_key - -## 1.1 介绍 - -**功能介绍:** 基于HAL接口完成时钟、GPIO控制器初始化和功能配置。在示例代码中通过中断的方式实现对S2按键的检测。 - -**软件概述:** 使用中断方式实现按键功能。 - -**硬件概述:** 核心板。通过板端丝印可以看出S2按键的GPIO对应GPIO2_4。硬件搭建要求如图所示: - - - -![image-20240829165616356](../../../../docs/pic/tools/image-20240829165616356.png) - -## 1.2 约束与限制 - -### 1.2.1 支持应用运行的芯片和开发板 - - 本示例支持开发板:3061M - -### 1.2.2 支持API版本、SDK版本 - - 本示例支持版本号:SolarA2_1.0.1.2 - -### 1.2.3 支持IDE版本 - - 本示例支持IDE版本号:1.0.0.6; - -## 1.3 效果预览 - - 当用户烧录编译后的示例代码后,初始化和配置完成后,示例代码中在按键没有按下时Debug串口会一直打印等待按键的log信息,当连接的按键按下时功能正常时Debug串口打印成功log信息;功能异常时按下按键时Debug串口还是会一直打印等待按键的log信息。 - -![image-20240829172606786](../../../../docs/pic/sample_gpio_key/image-20240829172606786.png) - -## 1.4 接口介绍 - -#### 1.4.1 HAL_GPIO_Init() - -| **定义:** | void HAL_GPIO_Init(GPIO_Handle *handle); | -| ------------ | ----------------------------------------- | -| **功能:** | 初始化GPIO寄存器值 | -| **参数:** | handle:GPIO_Handle的值 | -| **返回值:** | None | -| **依赖:** | drivers\gpio\common\inc\gpio.h | - -## 1.5 具体实现 - - 步骤一:GPIO管脚选择:示例代码中选择GPIO管脚用于通过中断方式进行按键检测。也可以选择其他GPIO管脚用于按键检测功能测试,在"GPIO_Init()"接口中的"g_gpiox.baseAddress"可以配置其它GPIOX,g_gpiox.pins可以配置“GPIO_PIN_0-GPIO_PIN_7”中的任意一个。 - - 步骤二:GPIO管脚初始化:调用接口"HAL_GPIO_Init()”完成对示例代码中GPIO管脚的方向、电平、中断模式配置。 - -## 1.6 实验流程 - -- 步骤一:在xxx\vendor\yibaina_3061M\demo\sample_gpio_key/文件夹里面内容复制替换到xxx/user/目录下。 - - ![image-20240829174750105](../../../../docs/pic/sample_gpio_key/image-20240829174750105.png) - - -- 步骤二:点击编译按钮![image-20240528173107958](../../../../docs/pic/sample_gpio_key/image-20240528173107958-17249242848691.png),编译成功后终端窗口输出如下图所示。 - ![image-20240624165330284](../../../../docs/pic/tools/image-20240624165330284.png) - - - -- 步骤三:进入工程配置界面。打开要烧录的工程后,单击顶部![image-20240624154427635](../../../../docs/pic/sample_gpio_key/image-20240624154427635-17249243742892.png)的图标,进入工程配置界面->进入程序加载。选择传输方式为“swd”,并配置其他参数。(具体[参考tools目录README搭建环境](https://gitee.com/HiSpark/open_mcu/tree/master/tools)) - - ![image-20240829174913995](../../../../docs/pic/sample_gpio_key/image-20240829174913995.png) - -- 步骤四:单击“烧录”![image-20240624162016190](../../../../docs/pic/tools/image-20240624162016190.png) 按钮,开始烧录。烧录成功后终端窗口输出如下图所示。 - - ![image-20240624162037834](../../../../docs/pic/sample_gpio_key/image-20240624162037834-17249245112874.png) - -* 步骤五 : 当用户烧录编译后的示例代码后,初始化和配置完成后,示例代码中在按键没有按下时Debug串口会一直打印等待按键的log信息,当连接的按键按下时功能正常时Debug串口打印成功log信息;功能异常时按下按键时Debug串口还是会一直打印等待按键的log信息。 - - ![image-20240829172606786](../../../../docs/pic/sample_gpio_key/image-20240829172606786.png) - - - - \ No newline at end of file -- Gitee From 74c8669e3d6700ab87ac418329e6d56ea2912630 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B8=A9=E8=80=81=E5=B8=88?= <1161625498@qq.com> Date: Wed, 14 May 2025 15:16:50 +0800 Subject: [PATCH 2/5] add demo --- .../__pycache__/build_gn.cpython-311.pyc | Bin 0 -> 45918 bytes .../mk_prim_xml_step1.cpython-311.pyc | Bin 0 -> 9605 bytes .../mk_prim_xml_step2.cpython-311.pyc | Bin 0 -> 10970 bytes src/build/out/build.log | 0 .../inc/mcs_user_config.h | 14 - .../user/pmsm_sensorless_2shunt_foc/readme.md | 10 - .../demo/10025_Six_Step_Wave/readme.md | 20 - .../user/generatecode/feature.h | 94 -- .../user/generatecode/main.h | 98 -- .../user/generatecode/system_init.c | 677 ----------- .../demo/10025_Six_Step_Wave/user/main.c | 71 -- .../inc/mcs_carrier.h | 142 --- .../inc/mcs_chip_config.h | 76 -- .../inc/mcs_ctlmode_config.h | 73 -- .../inc/mcs_motor_process.h | 45 - .../inc/mcs_status.h | 188 --- .../inc/mcs_user_config.h | 108 -- .../protection/common/mcs_prot_cmm.c | 43 - .../protection/common/mcs_prot_cmm.h | 136 --- .../protection/common/mcs_prot_user.c | 137 --- .../protection/common/mcs_prot_user.h | 54 - .../protection/common/mcs_prot_user_config.h | 125 -- .../protection/mcs_curr_prot.c | 267 ----- .../protection/mcs_curr_prot.h | 52 - .../protection/mcs_dc_volt_prot.c | 495 -------- .../protection/mcs_dc_volt_prot.h | 69 -- .../protection/mcs_motor_stalling.c | 112 -- .../protection/mcs_motor_stalling.h | 45 - .../protection/mcs_temp_prot.c | 262 ----- .../protection/mcs_temp_prot.h | 49 - .../user/pmsm_sensorless_2shunt_foc/readme.md | 10 - .../src/mcs_carrier.c | 144 --- .../src/mcs_motor_process.c | 950 --------------- .../user_interface/cust_process.c | 718 ----------- .../user_interface/cust_process.h | 36 - .../user_interface/hmi_module.c | 57 - .../user_interface/hmi_module.h | 37 - .../user_interface/protocol.c | 174 --- .../user_interface/protocol.h | 121 -- .../user_interface/uart_module.c | 184 --- .../user_interface/uart_module.h | 47 - vendor/yibaina_3061M/demo/E7050_FOC/readme.md | 20 - .../E7050_FOC/user/generatecode/feature.h | 94 -- .../demo/E7050_FOC/user/generatecode/main.h | 98 -- .../E7050_FOC/user/generatecode/system_init.c | 677 ----------- .../yibaina_3061M/demo/E7050_FOC/user/main.c | 71 -- .../inc/mcs_carrier.h | 142 --- .../inc/mcs_chip_config.h | 76 -- .../inc/mcs_ctlmode_config.h | 73 -- .../inc/mcs_motor_process.h | 45 - .../inc/mcs_status.h | 188 --- .../inc/mcs_user_config.h | 108 -- .../protection/common/mcs_prot_cmm.c | 43 - .../protection/common/mcs_prot_cmm.h | 136 --- .../protection/common/mcs_prot_user.c | 137 --- .../protection/common/mcs_prot_user.h | 54 - .../protection/common/mcs_prot_user_config.h | 125 -- .../protection/mcs_curr_prot.c | 267 ----- .../protection/mcs_curr_prot.h | 52 - .../protection/mcs_dc_volt_prot.c | 495 -------- .../protection/mcs_dc_volt_prot.h | 69 -- .../protection/mcs_motor_stalling.c | 112 -- .../protection/mcs_motor_stalling.h | 45 - .../protection/mcs_temp_prot.c | 262 ----- .../protection/mcs_temp_prot.h | 49 - .../user/pmsm_sensorless_2shunt_foc/readme.md | 10 - .../src/mcs_carrier.c | 144 --- .../src/mcs_motor_process.c | 950 --------------- .../user_interface/cust_process.c | 718 ----------- .../user_interface/cust_process.h | 36 - .../user_interface/hmi_module.c | 57 - .../user_interface/hmi_module.h | 37 - .../user_interface/protocol.c | 174 --- .../user_interface/protocol.h | 121 -- .../user_interface/uart_module.c | 184 --- .../user_interface/uart_module.h | 47 - .../demo/E7050_Six_Step_Wave/readme.md | 20 - .../user/generatecode/feature.h | 94 -- .../user/generatecode/main.h | 84 -- .../user/generatecode/system_init.c | 530 --------- .../demo/E7050_Six_Step_Wave/user/main.c | 67 -- .../application/mcs_motor_process.c | 562 --------- .../application/mcs_motor_process.h | 47 - .../application/mcs_user_config.h | 94 -- .../common/mcs_carrier.c | 224 ---- .../common/mcs_carrier.h | 91 -- .../common/mcs_fsm.h | 55 - .../func/mcs_six_step.c | 126 -- .../func/mcs_six_step.h | 79 -- .../func/mcs_status.h | 188 --- .../demo/GIM4310-10_FOC/readme.md | 20 - .../user/generatecode/feature.h | 94 -- .../GIM4310-10_FOC/user/generatecode/main.h | 98 -- .../user/generatecode/system_init.c | 677 ----------- .../demo/GIM4310-10_FOC/user/main.c | 71 -- .../inc/mcs_carrier.h | 142 --- .../inc/mcs_chip_config.h | 76 -- .../inc/mcs_ctlmode_config.h | 73 -- .../inc/mcs_motor_process.h | 45 - .../inc/mcs_status.h | 188 --- .../inc/mcs_user_config.h | 120 -- .../protection/common/mcs_prot_cmm.c | 43 - .../protection/common/mcs_prot_cmm.h | 136 --- .../protection/common/mcs_prot_user.c | 137 --- .../protection/common/mcs_prot_user.h | 54 - .../protection/common/mcs_prot_user_config.h | 125 -- .../protection/mcs_curr_prot.c | 267 ----- .../protection/mcs_curr_prot.h | 52 - .../protection/mcs_dc_volt_prot.c | 495 -------- .../protection/mcs_dc_volt_prot.h | 69 -- .../protection/mcs_motor_stalling.c | 112 -- .../protection/mcs_motor_stalling.h | 45 - .../protection/mcs_temp_prot.c | 262 ----- .../protection/mcs_temp_prot.h | 49 - .../user/pmsm_sensorless_2shunt_foc/readme.md | 10 - .../src/mcs_carrier.c | 144 --- .../src/mcs_motor_process.c | 951 --------------- .../user_interface/cust_process.c | 718 ----------- .../user_interface/cust_process.h | 36 - .../user_interface/hmi_module.c | 57 - .../user_interface/hmi_module.h | 37 - .../user_interface/protocol.c | 174 --- .../user_interface/protocol.h | 121 -- .../user_interface/uart_module.c | 184 --- .../user_interface/uart_module.h | 47 - .../demo/GIM4310-10_Six_Step_Wave/readme.md | 20 - .../user/generatecode/feature.h | 94 -- .../user/generatecode/main.h | 84 -- .../user/generatecode/system_init.c | 530 --------- .../demo/GIM4310-10_Six_Step_Wave/user/main.c | 67 -- .../application/mcs_motor_process.c | 562 --------- .../application/mcs_motor_process.h | 47 - .../application/mcs_user_config.h | 94 -- .../common/mcs_carrier.c | 224 ---- .../common/mcs_carrier.h | 91 -- .../common/mcs_fsm.h | 55 - .../func/mcs_six_step.c | 126 -- .../func/mcs_six_step.h | 79 -- .../func/mcs_status.h | 188 --- .../demo/pmsm_pos_qdm_foc/readme.md | 22 - .../user/generatecode/feature.h | 93 -- .../pmsm_pos_qdm_foc/user/generatecode/main.h | 101 -- .../user/generatecode/system_init.c | 775 ------------ .../demo/pmsm_pos_qdm_foc/user/main.c | 81 -- .../inc/mcs_carrier.h | 153 --- .../inc/mcs_chip_config.h | 81 -- .../inc/mcs_ctlmode_config.h | 75 -- .../inc/mcs_ex_common.h | 78 -- .../inc/mcs_inc_enc.h | 126 -- .../inc/mcs_motor_process.h | 45 - .../inc/mcs_status.h | 188 --- .../inc/mcs_user_config.h | 143 --- .../protection/common/mcs_prot_cmm.c | 43 - .../protection/common/mcs_prot_cmm.h | 136 --- .../protection/common/mcs_prot_user.c | 137 --- .../protection/common/mcs_prot_user.h | 54 - .../protection/common/mcs_prot_user_config.h | 130 -- .../protection/mcs_curr_prot.c | 267 ----- .../protection/mcs_curr_prot.h | 52 - .../protection/mcs_dc_volt_prot.c | 495 -------- .../protection/mcs_dc_volt_prot.h | 69 -- .../protection/mcs_motor_stalling.c | 112 -- .../protection/mcs_motor_stalling.h | 45 - .../protection/mcs_temp_prot.c | 262 ----- .../protection/mcs_temp_prot.h | 49 - .../user/pmsm_encode_qdm_2shunt_foc/readme.md | 12 - .../src/mcs_carrier.c | 136 --- .../src/mcs_inc_enc.c | 236 ---- .../src/mcs_motor_process.c | 1047 ----------------- .../user_interface/cust_process.c | 639 ---------- .../user_interface/cust_process.h | 36 - .../user_interface/hmi_module.c | 58 - .../user_interface/hmi_module.h | 37 - .../user_interface/protocol.c | 174 --- .../user_interface/protocol.h | 121 -- .../user_interface/uart_module.c | 234 ---- .../user_interface/uart_module.h | 48 - 177 files changed, 28897 deletions(-) create mode 100644 src/build/__pycache__/build_gn.cpython-311.pyc create mode 100644 src/build/createxml/__pycache__/mk_prim_xml_step1.cpython-311.pyc create mode 100644 src/build/createxml/__pycache__/mk_prim_xml_step2.cpython-311.pyc create mode 100644 src/build/out/build.log delete mode 100644 vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/readme.md delete mode 100644 vendor/yibaina_3061M/demo/10025_Six_Step_Wave/readme.md delete mode 100644 vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/generatecode/feature.h delete mode 100644 vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/generatecode/main.h delete mode 100644 vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/generatecode/system_init.c delete mode 100644 vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/main.c delete mode 100644 vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/inc/mcs_carrier.h delete mode 100644 vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/inc/mcs_chip_config.h delete mode 100644 vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/inc/mcs_ctlmode_config.h delete mode 100644 vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/inc/mcs_motor_process.h delete mode 100644 vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/inc/mcs_status.h delete mode 100644 vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/inc/mcs_user_config.h delete mode 100644 vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_cmm.c delete mode 100644 vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_cmm.h delete mode 100644 vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user.c delete mode 100644 vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user.h delete mode 100644 vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user_config.h delete mode 100644 vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/mcs_curr_prot.c delete mode 100644 vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/mcs_curr_prot.h delete mode 100644 vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/mcs_dc_volt_prot.c delete mode 100644 vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/mcs_dc_volt_prot.h delete mode 100644 vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/mcs_motor_stalling.c delete mode 100644 vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/mcs_motor_stalling.h delete mode 100644 vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/mcs_temp_prot.c delete mode 100644 vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/mcs_temp_prot.h delete mode 100644 vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/readme.md delete mode 100644 vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/src/mcs_carrier.c delete mode 100644 vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/src/mcs_motor_process.c delete mode 100644 vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/user_interface/cust_process.c delete mode 100644 vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/user_interface/cust_process.h delete mode 100644 vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/user_interface/hmi_module.c delete mode 100644 vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/user_interface/hmi_module.h delete mode 100644 vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/user_interface/protocol.c delete mode 100644 vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/user_interface/protocol.h delete mode 100644 vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/user_interface/uart_module.c delete mode 100644 vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/user_interface/uart_module.h delete mode 100644 vendor/yibaina_3061M/demo/E7050_FOC/readme.md delete mode 100644 vendor/yibaina_3061M/demo/E7050_FOC/user/generatecode/feature.h delete mode 100644 vendor/yibaina_3061M/demo/E7050_FOC/user/generatecode/main.h delete mode 100644 vendor/yibaina_3061M/demo/E7050_FOC/user/generatecode/system_init.c delete mode 100644 vendor/yibaina_3061M/demo/E7050_FOC/user/main.c delete mode 100644 vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_carrier.h delete mode 100644 vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_chip_config.h delete mode 100644 vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_ctlmode_config.h delete mode 100644 vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_motor_process.h delete mode 100644 vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_status.h delete mode 100644 vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_user_config.h delete mode 100644 vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_cmm.c delete mode 100644 vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_cmm.h delete mode 100644 vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user.c delete mode 100644 vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user.h delete mode 100644 vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user_config.h delete mode 100644 vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_curr_prot.c delete mode 100644 vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_curr_prot.h delete mode 100644 vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_dc_volt_prot.c delete mode 100644 vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_dc_volt_prot.h delete mode 100644 vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_motor_stalling.c delete mode 100644 vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_motor_stalling.h delete mode 100644 vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_temp_prot.c delete mode 100644 vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_temp_prot.h delete mode 100644 vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/readme.md delete mode 100644 vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/src/mcs_carrier.c delete mode 100644 vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/src/mcs_motor_process.c delete mode 100644 vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/cust_process.c delete mode 100644 vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/cust_process.h delete mode 100644 vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/hmi_module.c delete mode 100644 vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/hmi_module.h delete mode 100644 vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/protocol.c delete mode 100644 vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/protocol.h delete mode 100644 vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/uart_module.c delete mode 100644 vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/uart_module.h delete mode 100644 vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/readme.md delete mode 100644 vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/generatecode/feature.h delete mode 100644 vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/generatecode/main.h delete mode 100644 vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/generatecode/system_init.c delete mode 100644 vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/main.c delete mode 100644 vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/application/mcs_motor_process.c delete mode 100644 vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/application/mcs_motor_process.h delete mode 100644 vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/application/mcs_user_config.h delete mode 100644 vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/common/mcs_carrier.c delete mode 100644 vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/common/mcs_carrier.h delete mode 100644 vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/common/mcs_fsm.h delete mode 100644 vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/func/mcs_six_step.c delete mode 100644 vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/func/mcs_six_step.h delete mode 100644 vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/func/mcs_status.h delete mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_FOC/readme.md delete mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/generatecode/feature.h delete mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/generatecode/main.h delete mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/generatecode/system_init.c delete mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/main.c delete mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_carrier.h delete mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_chip_config.h delete mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_ctlmode_config.h delete mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_motor_process.h delete mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_status.h delete mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_user_config.h delete mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_cmm.c delete mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_cmm.h delete mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user.c delete mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user.h delete mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user_config.h delete mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_curr_prot.c delete mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_curr_prot.h delete mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_dc_volt_prot.c delete mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_dc_volt_prot.h delete mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_motor_stalling.c delete mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_motor_stalling.h delete mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_temp_prot.c delete mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_temp_prot.h delete mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/readme.md delete mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/src/mcs_carrier.c delete mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/src/mcs_motor_process.c delete mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/cust_process.c delete mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/cust_process.h delete mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/hmi_module.c delete mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/hmi_module.h delete mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/protocol.c delete mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/protocol.h delete mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/uart_module.c delete mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/uart_module.h delete mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/readme.md delete mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/generatecode/feature.h delete mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/generatecode/main.h delete mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/generatecode/system_init.c delete mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/main.c delete mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/application/mcs_motor_process.c delete mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/application/mcs_motor_process.h delete mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/application/mcs_user_config.h delete mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/common/mcs_carrier.c delete mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/common/mcs_carrier.h delete mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/common/mcs_fsm.h delete mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/func/mcs_six_step.c delete mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/func/mcs_six_step.h delete mode 100644 vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/func/mcs_status.h delete mode 100644 vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/readme.md delete mode 100644 vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/generatecode/feature.h delete mode 100644 vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/generatecode/main.h delete mode 100644 vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/generatecode/system_init.c delete mode 100644 vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/main.c delete mode 100644 vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/inc/mcs_carrier.h delete mode 100644 vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/inc/mcs_chip_config.h delete mode 100644 vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/inc/mcs_ctlmode_config.h delete mode 100644 vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/inc/mcs_ex_common.h delete mode 100644 vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/inc/mcs_inc_enc.h delete mode 100644 vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/inc/mcs_motor_process.h delete mode 100644 vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/inc/mcs_status.h delete mode 100644 vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/inc/mcs_user_config.h delete mode 100644 vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/common/mcs_prot_cmm.c delete mode 100644 vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/common/mcs_prot_cmm.h delete mode 100644 vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/common/mcs_prot_user.c delete mode 100644 vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/common/mcs_prot_user.h delete mode 100644 vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/common/mcs_prot_user_config.h delete mode 100644 vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/mcs_curr_prot.c delete mode 100644 vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/mcs_curr_prot.h delete mode 100644 vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/mcs_dc_volt_prot.c delete mode 100644 vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/mcs_dc_volt_prot.h delete mode 100644 vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/mcs_motor_stalling.c delete mode 100644 vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/mcs_motor_stalling.h delete mode 100644 vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/mcs_temp_prot.c delete mode 100644 vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/mcs_temp_prot.h delete mode 100644 vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/readme.md delete mode 100644 vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/src/mcs_carrier.c delete mode 100644 vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/src/mcs_inc_enc.c delete mode 100644 vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/src/mcs_motor_process.c delete mode 100644 vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/user_interface/cust_process.c delete mode 100644 vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/user_interface/cust_process.h delete mode 100644 vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/user_interface/hmi_module.c delete mode 100644 vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/user_interface/hmi_module.h delete mode 100644 vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/user_interface/protocol.c delete mode 100644 vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/user_interface/protocol.h delete mode 100644 vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/user_interface/uart_module.c delete mode 100644 vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/user_interface/uart_module.h diff --git a/src/build/__pycache__/build_gn.cpython-311.pyc b/src/build/__pycache__/build_gn.cpython-311.pyc new file mode 100644 index 0000000000000000000000000000000000000000..b2a2761df3449c4512bab935abfd350a208cf895 GIT binary patch literal 45918 zcmd7532+-(nkEJi0GS{X1W154NP;)Pn-V2e={_h?)Irr$sSc@C#ia;{qKbzm0hJ^a z*sX0>&$8R}wz^ln#hy@4V|uE(S(leH(`_rlN7Q&`CpsMAaV8=o0K?Xnyb~3%ZF|Qo zSC6B%9G>;}zXTGQ34o-gyT+4D{>XfHzRdSu?|=XMnbTQr!twvuIz;L$K~pv1MxMX+93Zr3l;n&rjY4IT{Z>oYH(klz5l5h`SGtD`a9Hb zeu4c5oWG(3lL2#@Ke4oUZEp6tX(1dEaBn{$gr*~*6W3;QH?J(r&!}Z0Un%}A`2V~L zp>LZanz$S4baBacI*A zE&6`lV_GhWICCj(sI|sQ^rbDMH2Tx$H=7n6?WPe^*m}F z&Cp`nN`2z5pBuwuSP5pB0#QrNU;u8p8ITl=) zTSVQ$bYw}mG8PKI9$C71W%uyjJ6EXasfC$aQ_o%r3o}=)-kO^aDsXCcaq#BdbOk=1 z;1t!(DigdYy&M1G{|exq>2J$Tyl3^`TDjzCmmTd%M{mN>D>?dQNB^eDY&&j#Tv035 z4J7ME6Lq5-m!-O+a@|p>;+R}fNmT34)_7KE}W20o)izb23iD|Ge67he<#eu7%hhO5y2KWnT$cF^)?2d&3VW&fSU z3^;>O^`LKvjf#J}#i)p6u@Dm9pw#Tpj{^M{IA(hF_7$XDHZ7Z%OM>Q@=?znu3zpEY zU<+EN`)EWHs*b2pYSpn(|#qJIHfx-2*+ z(0$rGmA2EkUz}bDp%8`w+_UpbSEuKvSOX{|Oz#$0!0#74RJJ`Bnx8_`XBUMI+>hcv z{2u|>2=3^5XWvKrK0Nr|;SUc>?fd2S{hOvT+d&2-u4Se6e(U{9DM!`)S<%siGsV}f zjf;Fc&UMGeZL#Mt&J^!Wd7Cy(7CSNmtc*Q&RsCS!gM)Faosz3dc6F`yCS0SU zYc%Dqd(2m?PW)i@!F+t5RMRKd^d)OX6E&k77p0p0a?O5;KOpl5M0ReLp~S!COm!^} zV&D7vgRduR2NSh}>$e}3OSLED+7qJHn<}eKmNh5Jnx!(oT;`{N-|?6$U$u)>Z4%cm zbM0dGBy`fj5`2y>FwGFi9z>sLw0yn+07{q*$8U}j?u7Ei*_hL0XP5Y%4U}iuq#fV- zZ+O`pGso3Ysx8Y%F*9_^GAYy`S+pD<9C(01p#4Cp_7j`A9Tb|8LZH^At%TeICFu$! zIZ{utPt2v2ha z?v$##Ctkc%$}Fzf?UgSC8MDd|cD^y~PKM_b#NYo}{%dVXa$hmaILpwMVq} zq#O;RwL$1aVLCQNMaaKq2svhoBSrpEpp(H2E|86{whV9D6nux8%8z{z>E?bK{j{)@-=`%;>Y0%dnsQrz% zR{|8xPo~Q<-(I>rva~cGR=!x-NKxqD2+Ct2iOTg{i!nK*Hx%r#BcU?=Dy*#%VT)G{OwiYew_S_;X zE^V8cpI)5(vSe^Dz^5%37HRJ0{B(rawsdLuZa5NJ$c;T^*fpRJ(^fXf2%!@W2W|#$ zb}KSBA5PoCH>Ynerd`+2h^f$>(9EsK^ws$g(0^u#W(QunGBZRqePBn6Ust-LZ043Q zg%+gktcg>o%19BMVGLt6hj1JXJCFbHe*m~=O4W5G>v|J)z3b0z%u97AXh4i zZz5Gzw`nT31^;9?f9!31a4udidHZBxB5;CP3@?;4lIco?U+gm0*0=Zm@ zP-|Xj8A2g18q#tIh5Tp;wIdYrq#?8%p^z^Pp$?KXFZ022Kqr$gD}p?(T|pJz2U?re8SOr)e^Z?cby@0jBYQVZ+4Pbq+7O){$2iO>_2lNFS0Goo1fXzW4 z24hpY{MfC?k|JM5FKod(Q8I`!b2DH;?kc=fm`O}COA9yWFlI=mx+Y*W++Gr1SJVrG+}bC<2NCe)?2 zh25!GiQTDrW9Fb)mD^2V-dmP!F?=4mFtFn6gAyz;+p=Y~(hzIQ#Fik|y6TTvg=)~a zQuRSK7x`>#%p%kxwTk66iA_c(9ZYZ83npEEl^Xjiz(voqS4v!DVZnD|aV|19JwF#! zUf^8VKezbC((9q1|LR?c$k(QC%}11%No5H8(4=VRNkLc=_WKq52)Q_-2*&+48KU;j zTo28>KA5&L)L5o`A0gm+VR|tP?7A3<4rfZp#QDRwu5MizG2Er|OS7{fK_GGliFs}& zlCBEfiGYDwxVf|#T8t=&le@Dp&-mxGb8c~F{ubnXCBm!LGL>sex-u7zXVL&2qk31a93{Z678a&gPFTy<+TvdbTzNVo<> z*FdVJ=RSX*PgORo?UgIr;`52hU1H^~R9oMD*B4J54dArx`%=6+$#*9B&iInV@0Izz zBEL7~s3lS1IWvoFOYm*+gAzY1^TT5H{FK^0DOFvNt1gKAg)LaZI~bJE0h&p_1F71A zK@9|uJz}Ov7Ly}LFHwevw8KAWEzG%VxpX7IZ@$@wL8_vR zZ2Rcf$WrpAn>73jr!}BA-b8XSP@qN!7PRHH1iFP-Ne&$WY3|YGC@uW#MO$HDg7x+3 z@1Vsi+opEzE3MtWP3`4hW$pU9V;YhW*1rMAKU^zPC`|59~qDb)` z(tk&BjW#S-j-H?!p2qoAK26XBPN-s1IEBWfojQ_|wi64_=ts0&7Xv!!zd0>T`y+R6 zhWsE)OM?H+<>-M7Wz(e%`=Lle1|lA$f$E; z1yRjZts${~g~nz`EXR_}ECi#21+pT%c+MI!Wnvj_KSESEW1FWbJ|KTNZ5l*#yRA~ zxpe7Vn8d1pO?a6Sa!}q)F`lK8u5@oO?Nov?FC@()YbdMcH_Bx#?Cnqi^uifyg4Y?I znhPs41kqj%2e3`rx6rcik3smr7?hg44J%yA*C%rIsk-(R=VMp>+Q3I%$u%gu1~*M5 z_FaroIF|ICPWVng_W9#Yl5a@%4MALM8iBOdGy=uDXBVzKyFk;OyPnjx#Fxd|Q4sOE z(Uij%KQB5)HZ8b$;_@Y3{sef0I?2^5yLv@O@1_kAXokBh<3Yzv%eS4|Cy;R>`N>Cqnii1aF|54FFXC`ImC!Kvid1?K8vj0$` z|InlBQvbNzKQ48imOD?Q`JOW*Z30p;XUtEWwQI}bz+t)Ni0GvA|Ho4BOztMJ3GZNN z!zsHC)ALV4v}PR;_#`m&e$9q6IeaWJeC*>MY51%>d{zpalLP1Iu_oDLO|r+DG^5iq z1(nOL1HVB*;1J!ltFg87V($UjcQDn|`)hmx{dgPh0raDL;FE#f@4xikD<8gsmu){| z29rjw>*-zsJ_lqa{_l)=3%2ejOy2u@vU29ai2pZW)O}WZ(+RVg^#x z7OBK2%^bm!!YwwItS5<(ZArb^VP7)sgh+2^mw(|Qo){2GGE1d2lD)+AeFv)@u)$ki)TcFOm&P|ie4&)24GXB`pfZ3HY z$EIfm@SY`b=x z1#QJ}0JJ!|@uwfT5U@(Yik3ykw$a)=e`_&M_Xb{LVR|u`Q`SVAGC_q0@+sJo*uvLC z^Ya3sgQyP!`Wj=+} zG&G}ApkxEdmLdG#0AQq6&`9lmcemu~mR;RMMf*XGMc;v>?|8y@ocN;JAD(}Bp17hu z45p?&;!^sFzaWzbXFtZe%e(seTDMfuCRenrSioq>u9moEeUIoGkz6CPYh=Zm;`r~{ zzhnQlW7V907CY@ZA61?;Kh=#46mM^c_q1j(yyoI(~*KJ4eJGkg7Y+2>*FT z_yH~8i&hS8F$Q_`MpijON(hMk8s$jP64%_J8!`vPeihee!{~QwYLxd?S_{-@`$}sp zEl^|GS6U-ipho*wT4Q;E8XaG0jeLO`onL8<6$NT^eWf+J3)EQomDX5QphnMES|cee zyqHG~FWnM=Xqo0B+Lx1=Nqaz&Y*E2E?TBFVABdKOW35O}IWPiJ{X?L*!CEoPZO12Kt{7tI&*D) zdNyoqsS=u>EBk=lba+9@XwcaM6qbi^4#YvV*BP$Qoo}DR^r> zM7A9?cae6G_9FAK=qy3%74?IXT(6|)(6Y!hqcc??)nB2~@ClF@rk<$vgAh6@*P1o6 z789Q|wg3K0@yTSz-bBaVjW?x^6LQB1scB4Z8Y2Pt6r+Qu7#%zXxxOJjC_0C6>LvQg zWdGqr|KUe(O8sZ${zKRwZ8ke?%%>7a<`Cb*1Hl>29gDh&mN@TqWjNl*g#@t$=}jX zujWi6jg|&E8@r%cHAD7mr4LPB!v|bM4mKD&LrbL@Qn}KM79fR7Q!Mm;is$=4edcAG zc39m|CtzdNm`yK%Lj(4EO#f9n2{W+k0;$Vjt8jfz2x59Cau-b2-LSB<6cMf?MrVpy z8oqVy+T0yx76xmv>tq0ywvyTj$(}=MrlCsPm^FgXMg{X39!W9#w=)$L4Gp8YfN%|| zqjj2ZQ>PTeP&bADIg;EnJ@M9Wno8~XOaRDYw+3q8F~#g&cJ-`}C0xUzYnY668`~Zp zTrZOvhvmlMWaG1m#%CXONR3D2#v?1|nAKMNid45tuG^KYJCLY5kgDs5&u!G>o2);G zFR}jM%EYI9)#}+bK0c?N&*$$%g=F~f^dhYOj;%(4DXG@1 zXUufp{OVCs>4eRz5I&2{*&9Awvq?MmUPR$!(azTgt+^RneXI0$umv#PQ0GbG8XAMQ zo;eB`5b5f&yE->2g;wPM-tAzHX6hGYY3R*j*T&Pi=Wb zUGoLwTy+v4t}(;aw7_7VWo*xV^Xl;+2g+^FtYIM4S7D>AoY|58g{eF|AW#YpMzI3T z*)l^c(j2mx3L%WhFpU=KGj=daFH9mR+M>atYS(B?z?_6k|8oG4dY(vq;N6SLlqb>J zK}M<%C4FNF-x!hV`X63+c!3BtO{6sq5TPCZ^})(H%--c zOu7QBoKJaZO@p%@xR{ws4JY{FjV6gdBJ)Q?{>Z1EnjhSL5KGl|!17kE-S@~L*OG`- z$3!GRROKBfaS3o`>T&x1#cxlp zPOeO*T%P;48TZlB_2cE=UtYf`wd|5xcEN zc4FcI2}(_s)o(k$?_8Ue++DJ}OLTX!I)3o{gO`fi)?jJO3dGur1z4)tPPQ%d8#Ev(X1%JCA#KWh;5PaPrgX(f zjlmoaC|8SIRgw}{k#DXRqs4C#qCu-H%JcX{f3BQuQMiFDfIyjUsPndQZ?2@=RnTEX z^ymRi59Lzo-#COh9*f%WA8?QBzb#T`|7|<8&-7QEFgXv9wEBR+kISQLb*v$yQRaxo1XYh|f|ieXVng*Ot=dXkwLb-?Dt7+J*3r5`|zd-01G;@7Qz=2Fz<2g zPPO-oZa+~jBt9!QAf45zfcvFSJid6T=;^|l^3=b*|NHyb=9D>Yh^5}{C*HcGw>#nO zUN4orL$Y^B^bUQoX{Y?4x1fn7cGA{?ZZHcK4-r-mqDyD{Vi1oA+ixqxXjZl5Vaj4HG^|2n zEz}5BXrU_3ES3dr+IvQE)irf^GL0Yv8u=R3X{H-$d||;WSXNjQ5g|m9Ic}(QsxUQ* zv}RLkMF*p+1AUKdbq9fF3N2SfkHRh2KAXNYvx)VLO3lzSTu!EAJp5a7^^7@a&pi#k2bEUiIm>l1Ds4uLLl;Bqa@m0xUKgX zEzG$>;r)KKsGgC4vNnwAg&8w|C1erEC7oBvY=$Xh(*AXXc`c z$fU%YL<1S#L&>4cU_zff2@_g_d6{q*>C={_t2Y?e@@2`J%S?n7q6in>BoHI8OyKJT zzCnN{7bXLZML7=Po0N*U34t1R%9`N?Co1wHk2@=`t)&WsFFUvG_wucA3DI ztx4Net|VYp#Nc~WqC@FJWzmwVFr+&47BRw4kUCng;U)A8(96U~9Ka)IRoH_cbx5uu zn5hy)+s)+K<4NB{!Z$&3ZL{PXmVLt{*^+^9(=L*1cM~aw5Nmes&XD4+1lJY+y2S03 zIdBimptm9EXu`VDwQG{2Q+B|>&0?Q0WBF2y2kh#Q8Ml}Ri&JwXdm*iU#d`sN=GsjOHkNs`$l>N;06W1@we$GGonlwBi z4^K$`GqRtw_pQu82M~|$WCl8b^zd2uMBnZ!61Pj{c8T1s?3ycI@A`h(2jjdF`vumD z3+9z`)Qa;--k;zxQSh_IpEN#hZGWfsXDvTz`9WhBEn_a`2APGk zqe$;pJyq`;#<~7!^>^sy<3SHbu1RpBZ65UzXhFI9MF+iV3a>7O`jrQ()fCmb6uB?I zBm0cnZ#PcL_Pj&?ea4&e5+ER0qkj^#msB0>0K=&nXW3B3aH?taxxr-Cg2}9l{<+E- z7FI;*v_|8h#Sk2pg~I-mFxyr9VE9jwYoi={s4$#rsXO2|na+h)9E4`z3+-;MsGeN` zO+7T}$~egViaA>@ox&6nKN#&&v_m;HP7&^u__*jZ8SYf4KVu8^wy|H^BIh>tWQ=u< z?k`&LwpL})XN%_D&aRAU*0#u>F_zFM1obOGRC_(8kNufPEzHkH`!ns#B+fRjNb2&; zJEc|nj zhzMwegS&KlhXCXCnb7ekbo-|Seh!efMCWb_ak^zPfhr<-8_p4i_#OuiUP z`pzbNXCYsRts|0em+ad`0>&r_7^5U$>>&YT4{^79XlD(^r6{+fyTy;t0FQuGyqMwWj6?;O__p(&=id^-I$iI?u)`-n8HKdaj zn2Q2a?Di*J?Fm=wV>R*d*h;BXw`0KspcSsAS2{)DSPyDHS;JZ{E}-vfKn?N7Kd zf0=9@NwkjqqWkClk0Ku*`>$_HyT)ZYC&lM3O06%-tuIRMmt^-#B0HbtXWi)7_}!m( zKN^))Vf+Uf{k*e!iZ;hRKP~elNs=(>s1tqtqJz#< z3$4LyIn0E^Eg~A}V{*`$YTG65J|VY_QI?iZ$wnsG*q3PRlNupO4I~@)CK~r{+|Jm^ zu$P2&A53DBYr7?`N9KCO?3phv(EwAl#9&h2R&;C#QV#8WU(U+PDDV_!ZuCrmP`|UX za^BCdK8o6vIku=BVqX3cG??4Kb*st_fq_x=!@5vabciAzBBQFSL=IJS(1ogOH_Ktw z8r$S_w7O80<${Q30b`42%vpsQ9rwZIEOf&+)W<74w+KhIKzbkGUpb&THx<_D>fZ@V zr;0Gy-1M85&K6h28*Msm+l+01S33OZg(0wMF#Rm-1UF+8dQ4@t3Nvs_+eZ_L)Bdcp zOUANk*e7CO)8^aJ?l~-O7a~1vLqm*#8X`w#GuNl*7KfngBj1Iyi*s#(^2tC|+NrQu z86BWz0qB2$!i4{lfI1VG<}OUV{QQNt- z;(59#y%wYuW&*u-rlU){GER%Kjt$e!%yI^$P3iL6!dxT-uW6xhc1BO3i>!s9bBs~2 zF;o1s4Vp-J=qy)SPb_8LgdWl0s8>rcvlEbiNu-jLa3GbuNx|q1j~!L;wQH3eO|qjY z>F7*2IweQ9>>z^)%!0A?pc!wQvZo*Re6`I`%#2C30l7AitlgEU-L$Dd`5#3d#w2$i?wqdGOFs}E++K^VzqoNys@*Tw?oZV=fc#eY zKy)j7WHGom)e?YuO6Hpt*FI=PH?0zP(i|M7-w43aL{VNze4EU-iF_M!B>Cn9-z@Qd znfHslKgHK1`PKyA8t;(XV2rXy=J$yF9x@GtGAs)|qpM%5x4DQySIqTqHfNy(VG{BG z+sMKwId4FtO&z*(}+-YbQfMPXPgs*v*{n| z%F6nh+(}u6*(;P4)|Hj@A-0pU48knRI?xknpxQ!N-C8e@L3`Xv{bf9~_35#c z{*Gn?3(rmF;lm;x)AG}8``!#*E`cE(5M=r2?`(qyGgySCNr1_2L5wB;c9`HGC*hg} zQX0JYJ~}4aL_Qg_D5$3`qsm8MTfqNjIjn?%$L40Dt%hVQ6t-+v1$ewvl44aYq=LMZ z6i8NYxs<{m7pOQV6tU92)UVoy$g?Dn0Bx97iGEJPz}&SS1>rH*Hir)R+n7lQD<~r@ zWTm;jL;kO+JxOtG1G8;vz^m+PImA{C$$^XuPrUiMFyD7%D_!HR)#T(u_CHp z5yiv){Dw`Aojm^HxG~RiIo39pDKk6&E&5TJ=wXr`F*NC_jI>YmAmb`G?Nlns+`eaK z0$7+Zh~lbQ_}BD8v_e$&gUDq0pNTTGE0h7=*WI$CaqU*Z(IGlIw8&WOIxX=NGCv`* zGv%z5olWt&1Y2UHPk2t^&&&LIk)4@@wFzgB=Ns1%0$EHD&;)Y0y!?cpwNi#TPvC)#?S|qMj=2}Ir71KRQt|7rSNSsfGB?sqY z6DbWzt~J56ZkqUZ+h-=;UixbSxYYuMrB=~1dR9g0J|On&l4!+WoE8-c5!|WyilFs8 zRIDul`p0LOuW}+Fc?Y?;)w5sBW_z0&$pr_A`7sMYJ%WU`@!9R$Y!nZO&CId&!fV@? zU^L3pmw=~kV{GyEPh#8RN!qt>N5Lmi#SP&Pwl5)=N3xJdErGqi(pV9Xcm>`c>6*8H z>e4Nq8t<>LQB6MS+KFo!_GY$gkwFv#iL=9S5dVSl=xG%f7iRZg2t^{q?Vn&afeMk! z8T$?93<16@CG7fYdDKq7bhJe+ZT;m@7hPv`dqfPrH2b60wthrcQjDshq`0V?g0i|j z3V~lwMIEc+UYbyclTdi``Z@*(o3>9^X;$oJk+3I20znGlGFc(h8C@7m9Osw9p(&8a znb+acx%8%hWyg1f|C9>UtJlJL3NR6gqD>lpKA!;mzj!0zj{(5gmz(O_R-CEIrj^oE zW!*|CcBWW`<0hMZj~<;qBk|A5{IeqeY^tI8VIMSP&M_!F2;MJE)wle(`S+XGE$gpH z^?T*|y$Eu9Wp`WB9Z0wX>s=dr-y3>#O6ol>xlhRM6Cyjc6~q@LepKd1MV|a~VZQkg zX^0GM$ob<34u5!_&~tkp8LnNxl0Yy(IOXklbUk zdrV{}6Gz$;e7nSV%6zBDccNKIz9YeRNPL&fcZqx#(x@VhrfbkU)IlD!lQlMR}I)igYK9iKS! zP)2a&l=kx#yPi#U9Zhr{{kTTzIxQPGEi&`Slo=05T))g=O-JU;|7cW(iz~hDVvs)sccNavi}oZ*;yHMCuI}a+`8-+b!BH|(w&rz z1zp>e-K|~e2^m!-|M)=ELtDSTSM_&HMkOSWcl`Ycc;S^6`Q>fLdKhCCnvl1v~`A=m?r!W=$B}%BE)8e&|K_a zDX}^cH5bdoR8LMY&BgqWl&@qN+<@IEiJd}J&c8*vB4b~uask=(w3rW8HGYm@=w|jt zNT2|%Z1$H_mv&*TPCeL1s71(<_AMl{!>Pc)hqN}fo2IdxG>wHBEEPQ2LRLGzqpSs` zx1D7zD9>%vV664>_~2vwX=J>{1lK5WO|aw>vA%$0Jb!}oOI(}GwTWCC3#m(RbrRPA zbSlCXxfZW>7MwfL5?OWb!mdn19C*};baK%~{G?=$7 z+kflrDGxe=e9#%JFzmWh9&^kLi~yW0i$%^MmeSrJ+St!K!6XtSacqXmLw|Ufi1tFV=>U zk!vnb7%ZB)CRXwkscU1lr$}8FD}9R8^|7+2NZk;#JVolp821#ZeKGq}q;86pKSk>1 znByr@x5W6TNbQeRJVol(nCB@{w_!A>bGuKGuRZ3>PaRQB`HZum%ifr`@cgS$2m*HX z?ri>5yT-;a3aew)Mdn=dM^JU)1^xOG(W>gWYIF)TpdJO%sjE-aM7S`fV_r!*;N^`r z_ryBHbSbSW3|?jX!(rVfTSrOZj)gF`I1%O+5ScDxD;BTKMy-Q0fUbqtrzjgj2if+c zWy<1;u<#>PDf|I}9|NQ<;H07zG{reJ7o-`C@Zj}mS!R`{FhEJ%>ICl;_D8|&L9`~j z8yA!?x9+elwFYKqX3}L>=ipg%aaPz(<&(-KT{@%qJgLY`EiBwrW?J2>;h9FWrTvw< z5NZ*gQ1NHR?6%dm87S7SLs3u%Fn=HlQ0k<%XU30*NDjKE*O58_) zR+FSlZ{5T$d?DdHh0rWo+B2K6jzsZTTuYwlXVUfLl5a6YzEd#|1b4;C{;`;P5=g8` zJ1HKol{Fq)gRvcDB0?yXu0SbJd1d3^-w&ZvktOOMEU|r#a%Z}pdW`B6{x@pO-%t#* z8_RrM|2u{L9|ZnAf&Y`hCV|fg{9gnOYU8|_$P<_TAAoINl zzoyvCmy4zfe~tSv+1-)wl7A>p9NtBIv|6K)V;>9q`cDzN{t&V!wmYkDT&YM^RzDz3 z9GRr0s+(5IAJ_XI`jYj5M14T2@008MlJ%pB`q7P-AHDv|LsI=Ex&9Jzx!_gFZ0{lW z183JNB)(hbyG6bmt0|I=yAq9{e%8wRa*jER#tV$RUOHyfkf57`s~J=;@FE))k|{KOW117KbZ9I zOZfLavP=Hsvi~?3=*JDM5Brl1y@`fisi9wP!1bO)!=8<~k85e0vzNqIu1gJba>Lw; zE9Juqjrvq|d$PJeQ4JHSM-HiaOs*b-HD2pbvh|rn>oboUKE5QiUXWWah+K23st%P_ zdeAl1^$)&APutGG%E=U8b^pb;zxMsFeecx=uVRKD)B89d*Y`>F19JTUdZ%qT**2PJ z8{N1h9(`VFdqHk{LBuXY&d!yRl%p*^mr(4ep#40a;%bsyQ-W(+yR#1Sx?zbMk+~6( z8`(Yr23Z@;V)IdnJ0^2%XW?vFV#^3Dw8Z8yi90ECCq?e0TAJ9hOB_8ZHlLEXahV$z zxp6gWZF&85v3bA59gw*LB6lDcad*92Y~CetyJc>-$n91m#Fo9{Gv~$TNr}54bLh4U znyBH8A+hTUYo*#~E_F&2Gk=w(<`Vw59#0|*YfXEFz)+ML;yLg4VE{i2M$a^T%AnzfBUOqOQ z#WqyA zG@s0^Y5{eLMn^*0jEnF(M^?a?pdi-+Sr41=J374x1g~u9o7GrQV4gPOF>>pc@MTjg z81xR|x1&hIW*QWAL@dGVW0}%huLk$j-XDdCZG0^*Pu3-zwm;Q%T+N^?WSF>Y#y_bb zQJr!W?*DBy-mj2j5{OMN{AQbo>ffCJaiws33OFJ;453H+tl0-L6-aw=^%9=BG@AtN zuEp$UvHW?0{%Rf4s^*T_Qs@_@ss@jGB_mv$XmaG{ZuL(LZ&@04GG`zeGno7^&t>1g zg7LImw#+TtV_aeJP+vOORr?Opnc^z@uY0~SFy{C^KTt;=Jg1$C*;#u|FP9g7#+bn% zC+90HSdwur*!!@vXuTN`Q0wY^CB2A^VuMGRy6^=}sI^o%|7&x+KxRX?yE z6`sczw7*5_{4G48{(12gZI3UC7`OhzwGp1x&fx#S4%%uMTTroM zlTB=+xgEtASG?uCpdlPDgrp6F2!0Fa=><$5;PDXK^tbXFwhl#kM7yzw~wxx2{2)XMoPe; z2nWv0dLC%?!;x^hN_VS>Zdoae?ZU!mDHYubZ=s~Lorz#%KdU&t}zv!oqN z9#g1d*ntchhF{_IZ9v-5W(}ubfP}tlkmWU%s$zJcuVEJl(#GC9Y}Zw*5@Vf0oi*% zWakraYtq}3@b;|xBrjH9?-kiePHpzVCZMVH;W^^L$Rx@>L@OHVGwZ*75TfG^+AupVDT>-y(=%@zaF=z zc<*=5f9L$SFRWfzxsc+kzdQMzNs0GmmdSf+6)!pQ@%2|F&jHy3+XTe}-IJD{O;d%F z$|HE+q4a}8^7O;-SoZARh$K7*M9+a@;VG{#Roj@X?M~Eoe`H}xG-{>(V{-qoPkM&m z=QbLpo<4z@Y+A=QhDqp}MJf`E?J@XFA#YuNvQPeB3+o zVLw)4^d6CWk9^X%`~7D&&Pshp<-VgRw)Z#|$`ky|R8oC{t^r|hK#)Amsm2sy?~!Eh zsYLIoEsN`8#Q~zYN>&^oiUU+aKC7o*+w{ZOZM&&r;&iiz%60u)eCm1^b-C$3qrwY3 z$^XRYJGOHIZ(`jUuM5RDqa)d>v%h(KfDPrZgWD*`lNh;;oOPATsW&}!&J3jwTVtiM z60EH?uO0(;SDJ01_SaZHqd00y3+KURC@SZrEvra74fYX*Hv%%YLr(|wcbEWpZ#A8< zvbeaNtii~HpXOl|jLMv=sIIu-uo76*xYSkP@c9UK0_6acf$|*E7kCQJ>8Tjv{}<>7 zg^m!;XCxrIaFA3u>GBzdoEa{swZTFa71GM0lo%3l@~oxOEg8W_(@LEbq}%Fno3wvS zn4O*p{z}SCLnAZwE>kF@fvZOk z851nKhLL1?+Z>Kyj3h%83_aAM45&6pX)IJzd6Zn|Ltn5WSZ5}guL1dU8qfa@IT@!` zQ@2vCo&VSIXtj+GVi>Q?R*T@u_@~T|dBFv(rP%*dVHz?U=gFR-jnRZ>pXk|FEF6Al z;eJ-(#9YlZl(p(Cvo+S-f$b0w|FlZ~PmC=tMki zn=)2iNpx4(hcUtIJM~&mmuGdE1h+kU@|!b^PPMc@L<(~yWIllV;~4r$Z&$+G6~C=| zAB6)_ZT|Iu>s>;-hZQ416%sQ4I?R$9ehld{?mbhM->lL})RCAl$ z%={iaM>O63oEc8CHAcu{*MU#E2Y&L#M^`_b{Z~uxFFjh62G7fb=cVpRxqA{g$oU-M zAV9QQg+;Ipyl-bUy6T68Zl%wbA)7Z4x^vkA`yRA0>4I$+8?)?CYS^h1QUr;Cc53(x zJr#u>@hlL9R&K(hDF@3(bD`|W%QqXd0u?-hFDPd=4fq)qOw>WZhG@`q~ z*lTtuZfNu@7v+I5$#YWnoTQ=5oc(KKKfEG&_RF6AYGhsOrm4(X!@zy3!ocKKfNxk6 zo>9>=%7l>Ik`NL&`bOU0%e;7GJa|Zbr{zA{E4`BKl@5qH0O7$y)qbpZ{KE5#mw$dG zx#whJ&q-;|xV&fllhK2Ja{AHB(&$Nf^dz$HVtWh#Vm`5t!OGKwoB*l7(fGL<0rmK3oW)Q zJ8v;z(1wLrnSW68$!$~tV)vveZU5)ykBx3klhDF_kkx^aD;8C zODeH03E=)%L54i|UhKmdG31AcA4gIZ=u~(bat(@&bF`*2kol5xHd~*|I;;vj5TEROi6@oA~hkWWM1)NDgibFpuKk zVX6Cw+`CvauD_uWDO9+CL%#A4D4;R zZAjty$B4>_fy-j~sSM+nRRrZ4gdY9)nKgrl`z@AUcueW}A>pmU{M&D{9r~vPmN)|s zm&Hb?0WSnK{xpM}I_KRME2WK1zNOh7hRW1(vG8>Z50r(uNuy>PBJ0_hIb4EoK+i=x z=-tv$q(2O;&3n>28XJX@SQ+&Gv?m$;npa(9H}LAUj05o~2B-Ov{bQ*jb=9;)!J zv{iw@V%usPX2kVv28&Uw&&rKGZp^tlyLuPBNXyq$ZvMU*8}Dd#M=R1+gEwDL4Xy10 zY3DPCQJT@W0W(E3vonO{65H2VY!h&UhhlvV_)>^1Qf(AiZP;QwXVX#S35(Tcc=7_Z z(T=A>%g!CtQBfFU#SA`<3)G=`W2kN4GJGI!`$WZRtJwNG$*cXD9n|GA=8CjuzX+qn z8dfnAQs?*X&sP!gPD{E9YYUI}SqCFBZOT*@nlS=I~x z4wVbP0*ErF37TU4fku80#!`ipSHuTLYUcrq@Cz3X8S&+xJzX*#6l_eW#OCi@T4YW zvQPwZX;&$+BAENBhE@V?04$OjhfwwGj!ifIDJ3AYaluT_a0FN3Dgf#P{h=6%q!6dm zAfit4nuJEHjv#z^o8cb<cV0R51a3F&sk$Z@kJPuo#JQ>(=F!!@w_p4IYwwP~ za}JyGbsUj9jwCxyCpu35vRUeQMecY-s=g#wUlP5SVV1$XxH%<9m+a_DI)-2Zy-^`K zj>wK9Nyn*#zLeiEZKD?(RJpRho!E|a@S?4 z@oRD;EV6CbJCFdJAb1+yKJ@)V@mk3fkUfF*HrSy~Cn+ z_>0Xlss!GVsy9t;Y^JbgmFuvtGF|Ewt7jzstjv?A)lchuKY9WCGrsolHL0#&uIpc! zNHx-~$xg830DAutSry7g_j^Y^B$m38nD0s?&4MQ_?Q#okmf@tSA%gcETD7rSLZ8%k zSnfMaWw5CvKq@5*pZsZs%jmdqqb7UpkITDaslZAT8 z;=II9%KW6rPwv3vpKp)f#h#TSKm53kjLGZv$aQ-T`?WwHM*K4KzpvS%~FIvqN|Sx{A&Ut0g1q0 z5_p#YS!4@jh$hf1D4XXL{tbnZ9#J3#i9kZ5KxpVa6MGB=FO5+sf!FXKc7h|lr~Yl0 zl-M}DJpiu7MoNASJZ?6XR&ADa*+^kW@G}bn-nxEJs)8|dmkk@Fso**D zkPUWaD!6JsXu}@pDu|gqHaLV-L9gEihhHifGq>8Hz)`^|v)wiTS1nmkQ%|L-VDGRE zyT_>@XfCIxQ9)fFv(rX$w+1$k zvR7LeDqjkHABFu>{sm(6lQw$c6k-5AsqC?xE>fSjumB#!j-v2w3o5BmD4Df0vBueA zn2BhnRg7Z3GAkUgMfWkrt*|eOjF(kExjVn>zjkX;vjLUROhc>%AnLjm4hf3>g)Lho zAAKEoc#Zl;oe>bc6hS>3u%xZ{^{Ab&fYs)Y=6NwHtwW`jsmlK$v%+ay>p*>TI2naX9!jZ_s%`GR;>L|k$$5|crA#$q_IzUM6!mAyG$QKH z{E*GkayMQ?GJjs{o3klhVKb`YFZnRlJ)%m2QU9*H~;_u literal 0 HcmV?d00001 diff --git a/src/build/createxml/__pycache__/mk_prim_xml_step1.cpython-311.pyc b/src/build/createxml/__pycache__/mk_prim_xml_step1.cpython-311.pyc new file mode 100644 index 0000000000000000000000000000000000000000..eef27fd932c3ec83f002ad4261497155b4b2b06c GIT binary patch literal 9605 zcmb_iYit`=cAnwP@HrGoQ4fltZR%l5v>vu>IriF?6IqY=DQ9DEvsRQ$WzI;ZOp)4| zQS30Ijj}Zg)({ZJP$6&NG(=l?m37cn(rn`vTh!g{wtqRopc)LIqCk-!`9~uzkfuM{ zb1%i=L(;Ar^in)?=iK+@J?DJqT>Z@Lb`p^On`yg#q>&*01HBXzy_xy?4rG=Ip5Vzi zF;;kzVud0`Ob7&;AIyz}dBK=t zw7~GxZKATzF)MIk>?*E}XP||hx1w3`HlR-44%EdvfU>;vG7)hZjzLkFkcGj?snlU- z7}CgGA}Ys{i7+onQ86})^2ftbTq_(+CMIK3;mKHB=)tw4WZCr5>?!zueG1P$MkoYl z7UMUw_7o~Bt`_zK-cU=Ncmif-85L1+gf^_R6XL{-WVq0%T#V=VSk!3bC7GL?i^n<4 zO-#%PTs$VphBrQeN;az|=4K+KK}$k>QbJ>gzkPM^`1lVbL6pV^_?cJ&nurr}QXC%= zq#JT_cKqptncRvrP{-qvx(1{`(rL z5*2%E0|wsPQCP<^A)^YLY1-V?thuAbGW1l42T6U|dR{R#5` zq~&2zVu&NeJgJaF#Os&mEwZCnn;@o{VxG4umaMh-R{pF!jg_)8Pbt&_x!9#p*|J%b zT35E^ZB1rT)It(bB&+bL?3093CXCBZtDI}o+4{@ zKOulTMI=wy9qoGaUOf=X6IORSCK-C8A+QwF>jw3@!1B4f=ia}tbm0kOX>55)0Lf5! zD^b^&Iic5u^v2+F>Tc?NWl6~nEWMNS?t{O)4?4v?m&g+|&{IT8ygZFYWGZ;u)~jk} zzJBw~H)Xw<>=I9^g$?@Ufb(>a`gM>#z1#NdW)f($T)<3`{4?0cokX%iP|emUXbU1$ zCOFfk&{c3vLBlO5*%V?BLbG)R*Ays)R4fYh0F=hVl4>O@g$4wiR_Lr97P}=?3gJAX zFyd+1U#d2ZO)h%pEeccF@4Qv9iuDSSMdxx$g4(c-l0qVhXP?9BX;4(~sT>aknc7jr zp(H}5MzOs@!m_=E0|5yqgs31%6XILpSYk3M&P-rK2)QuQ_hHcYW+y3&*ap0x z6~eHCb>)#%ivf5Qk-9UOn_^5BA~wS^IcqpfD`zm1@rfzPU_kr@2{Z>%5VPWL+yo{Z zy)K~>fYu4Apch(^@SAG`Gi9!glR&Y>1v!~Wbyw<&-&4H{#*qFC$O7@7PQvG3a(sCD z{yEjVFU@Rt>z3FL<@<{2?SwbCXYuNvic2@|y|uD;wO(yJsx=;cRHwR6Y3@^?G#rt~ z`*eTP@(XugQ2lM1zb!qa*EZw{+8x;l1n)bt`&W21a99f*&M>+^0J_Q7s@F9vpSgSH z-j(-9mqs(AdgHF;ckaHUHg;%@9T__BApDKEgFFM><1sLKf<}9aNQt*&4r_?tRaHWw z@Q*-9{qW` zLi|g;(ch^zMxo#RLl8$(Wxxu|R5=gcQnJhQmS=3kl(nJCF$Jiq3ZOE?+8FIE#=Z{cxBLJPrqVN)^qq=`2L6D$L+mQ&A_)WDWP^dm_B3Z#Hy zU3x=f+V!QkcFBs-VNv-Q-dPrkK_g+)k$VOZHic4+l z)7ttFvpL5$>+af|dpBy|`9bGTyYF{HXRH?rpg8+|mEBj!>Rku5u7OfdIX3d|2p{5|h5cZ4c9DU1JtT@~l3;knLV!>;G1ll!e>G%1ILTga95 z!bA!>$n%E$)~o=2N>~6r5PnL)UAKC;f1!aX!w0Hvb<|ybU1QGz~AYYs}7=&*m!#OFrnZ*8j5<090t$MnF0gcd`O@7>xwa## zZ>x2KTHRpUv0=JAG1b$dc{-5GTS`y zEugyWHSe)^*gvURtV!4C^$lsKUe}Z-+z!AjKmfgXyqR-%-bufMU7=jKceU?} z^yKF!)rJ?fh8HuG?r+Gvpf|T<&V8f%YBSS!_hj~9u#;;!v^w;~xzEmhdg1d6YG6PM z3}l!B3~F1Uf8qSO^JDfCRt+4{0!J_$%K6(i>UOQy9ayV7uu8A;Urc{C{b~I3cwwQr zxw~%aeO1Hh-ueuaVe%AF+njGI&c2`lO)mwu5qSn!1HK6qf@c6=oi?t#tDt~bSFQoe%yCscl8{Y*33P4A6WUVm8P)*WvT6;IB}2YmKW~+h zcy6YwsXvPx^i-^s`hVW0*dS06R;=4=*zI?9+O!vZm0!kN%ixz?vClgc$M#x`xAFF> z^*gp7EsMT$v!k*N?^LW) zltQPVcZETeck!%ZPk}b%-HHuAYj}_1oU)?Qd6(S0IT3hFoAyv~@!n5-mFoqsmY7Ci z1e~&Eo^z20V>&D6L&8Z}Y%FgFz_NB!L;+R987rtjJ|-I0Xu%D%#S+o@94}z?jzMI~ z(i4Sm%&;>A$lWHhi!)VBFwGKW-u`cPf)r(C&%K3Rh*GUK``ty z6E_4XkVI@I!e~KOQJRg%WP_4~SrP4}K~E=R35<0O$MCJFfTPcbQ<}RrD<+X|ZcyS} zLPT20uujZ^1I>fyjlhTD-!2;LNF-$r8!^K!3p2A2I~HO3Xz#@Hs8ucYpDW_X;!!9N zL#Wh^qs+*Dp#ZZ{yEW)+*rPK%M4rL4AuwQCF4K6i5Hf#L5VN8f%FI?7KL$;u zzuq)hd(sZw-?(%Fxy0F%t4*r^gyuhywr{xHi$~wCL% zD%+~DtvR-J!|q&kWzMMfU4>w>4&JzH*>l&EwXZ}}Z=dGv10z=3u++JH;O>EJWaX+_ zdsM4EnzrdJNAiTvF+@JWG{^4V2)3>V53dCe=lVv};7eNYB|z1lV9wK`djoo?XFYUe zEp+6U*FK;6{Kn&6H8iY+h96(kLTA$#^-u(p^-%9xsCTvP5v_*$wNQWh;zo1pUpD?M z^yi_K>yO-O^N7|w0z>J+-CA(}da!3L*t7Ds8a$x|PZS5+XxNjbweUf;p;v3@&3Stv z=6K)rk>>-?3atjZwLtfZoICPTuJH_*;wKat2Z1lE(cQSE2QClnnxL#EF_48z^HIC}5cN?(pWsImt&_8?@yh0xfJb@sp-dmwl4 zw8{=@>|l-^gpBU-Yo5-Wy>qM0LZyta>;6;8Jd@v_-DIV>l`$ccm-BZ#4y zEe)HLoD)HMN5N@DFteZnUP8GS5t%>)$#jG&$Z`$7als%LUH*typ-e(sYKoPAN`Pn? zTwHO5+aYu*l-gC2u5F6{7+BvlTOG|11@FLDHf=dO`rGO~BK(2R z!DsMWJe&7eX$|Ig@Et4cu=(3=#+1uEu@X{}%ef{l+KefiJ%B|sBf&f1ld5wD$jm%3 z(ApfO+K&Zfw*4q3Qh~iP(++jF_qM8LRJnt^WsVSgE9PjTmH)p{s&J=?aVo||0Un&T zjmuZXvvFLWm^Seu_~N_f8M&p{2SH>_WKrS)o}jBE@!(hPhHEhzeX8*ZPwyBrhmr`B zI&aGG_R=rG&8e_M)Esm{Pjqt1?7{Gvcc#F`NMt@6ZEcWK`VmGSP0|db- zn9{6aFW$Fp36(YN7(-9BmaCl|0`2d^DAHAI;qy)coKkGwbFI&-{-c`zC|GwCEZZrD zjxGPGJEy?TyIM6@E3*9I4Yp?SZ05Sk?$OviId)G0A3&`STkf}ibl`&nD;;X^uogV5 zy81L%-yW4qn?EHt8`!;Mq?P{P`3-qe&A&othW8ucH7_PsyQoqWmjn8Y1&x7%H z_iOI{JV813J#NrLVJ&nlPgvahFv-B=o7TPkYu((+HVK7lQ_voF zg2@xq-BUzLyd85m9{;GldOf#aSc`9tL)PD=91mEg%YCGa*2M z&6x-pxbB3*Tucuj^RW0r%!A{zy?a+#4*{F)Ay9Qa9QqvZ!P-K^5B8r_!(W%6Yj9xRl0*b83|fwX9>Qu1PZdo;Oduj5 zf{YKI;kWjag@brXd>eRZFQvZ$0=MiWnXe&8cEN={I^kHrpR$a50e^JDxqv@9;mZ~O zbOJX+4L37+>m>^bzw`F=3Fbd7FXd^9+z)bCBu;{6)2TJ8DMvNww0&Xd-I3cP3-|-! LBRU9dysrNRx;^7P literal 0 HcmV?d00001 diff --git a/src/build/createxml/__pycache__/mk_prim_xml_step2.cpython-311.pyc b/src/build/createxml/__pycache__/mk_prim_xml_step2.cpython-311.pyc new file mode 100644 index 0000000000000000000000000000000000000000..e6790c1102ab360dc9b3bd9cc775ab13101761dd GIT binary patch literal 10970 zcmc&aTWlLwb~EIV9KJ;AO_6#_woF@=WZ9DChipsM+j<1H)5NL*R^d=KZAw&U#xIr3 zY6}h~o60=_u+8_^V;s$uW

0-_Ruq05*8Sjuh-ln4hC7%J&7im%o_|n(N z-)nk-*h#;Z2Z=P#Z$x-`Oa&9h5ILRJ4@K2$#!3Z^}qtf>ea^Y1tfgjryFP^wNrPah9K* z7a+w5fzX_QEb701=knQ^w}k*N%=B^d!G)k8^0Q)?pBV@Ucf|1G%*l@K2Q%Ttz=Chy zf7f?rM&SK3@81oExEZxw(5#vHJHACeIPZhBPY?r(oo$N`71rkicB0R>RHF5>EsX>UxCw4Bo!bFq|$W5GAjoit0Fh zlwSDTSJ80HYjQFGr-fH?N(4?;_l>{6kyi|ZSFcW8eL?W1clxeQj!q80z*$q6 z(aE8y7pO@T!@yww+ruDy`rv}-)hjHK7yyc$q(b4Fp}@UBNMS+TLnu!IvVc{=o3Xs@ zWUDq zR!@+oaQMg?_!me#rm|=3c1H5)d?^T0QN3tD25N}VoDP?r8gi*4dQN{HGJuB3&;Ztu z!Wwh1CW6&RbUths2@z@o+gT7IdD4mM9^{FgRP+l5FTU>}&Sbumy1X4j^ zftiZ4s0Hqd0YSiagP5h{tZ0_wd`KsTQJ*cjGLn{?(+S0d5P)Usd1?9D+4X=_+ANng z$Jk_fWz4ecsa=1tX_o6cCC^FOb24UGJteWOWP^9JU2ZrFuV*E;Hrd>|IW0H$z-zC> zHYD9pe8rJui+5O8f_1ICB(_mz8{_F;p>&v_1w=Z^Ko%au3}!3;2z48>)v%$-N*aSP zS0FdpGIKjKzj^Z*)!(F|Ohi9Gy>~fkh!{9<1pX>ZzBqkK!<>vT`%8wHB`>!%Io&X3 zw=SKI=pu$(S0Gn@X=-I4!esZBV^BtktQjU1ClNC=PyV@^J}$_=_@5ts_+g1@gI6%a zL&LtI(ec4+UK5YQspvtcD7vtqm_lF=ft+taG_yD>-cp#`;oyST$g_wlBBqcSKD4fi zDaBtgp!^An8Q)V$B9RNT^8o?-h!U5UIXmhwjdH0nBXT(zy--Y80f0uc8I5MLq+;#Z zS@oyYn>V)}N)3H-L!adAm!17_Xa9G}rdF(X z^@_yS$H`BX$ZXj{nIRIn0Et4IozYp5LfOmWb6eAa`8p&Dng`}ulTA*i&9#1V>BRYq zFwyn*z@-8AskiY&TSsGkV8I{ef(ti$8}Ev9$9oztUN}DQff9-}rD?A7fdCN?AR=>bQQaLv#PH#;s4ybXosWdaM z8gc}x6HWtwIV~Rnwc#jUiLOsejw7<;NQ}YP57s%!(Ih*XVob8}Fd6(wrUs*5yqF95PBoKMyN*(5hj0G zqAz+|Tj&v$7cNu{x1lZlG#88``dm)xa*i-yI$$|l$#R|#b3U*f#)&X@Hxy79Tw03h z*6aco3h=N%aVGg@1cwpe5+?JxygsiKboreMVu6I-BC zr3{59GrI+uQ^;cx0IuBH^{8BNJZ61f;aa=B;gTwj$`warR$?`hHAlb{bW5x|S<@1; z$`EU|z$3ATWcE;;J@j?cA#D{VJ9{zxk>f+hx>aJEWwv?ahQzif9qu^meua39z~mQ- z2Z&3S7LPIqh)dq|L=7M=#%#-xFA5Q3Y6^`JLzdS-Z)k!5qoW1T_RzYFUr$p+kTg*_ zz)Iw`Dne{P!~o*X>NE;|gSBkjyYBu}gCX-)evo`pPlgJTGSR!c7Q{ez{rocI&KZDkPfjuB0 zYRD<431~VW;|K-9n_teDQ!cCSOv#85Npsd%js*uO9NPRagM1wH;h%pO=iRSgWnH1X&RrnL%jTA4e6hXS!} zJnB^b3Ia4dd@BIZJ?ONk8ignm!etb3MAe{f5en2wDXNw9{wcjBs>xL?dD*xn5=LQ- zfO^rCO2+CXtrhFslRG=LZHd}8skTF|?TB0PmvmJ>vHsfeOUI@~a-Ec2Cu5_#?xr1g zSHj)3#chv%`9N~NCA;5>UD0i=bwNdue^Q`XEx-E~?&?`6e#_jk^x@(^t`*r&-+c%F%?rz!Lz2iQcaG%{i zBe@4<_ux;e>}#zjR>^frcAeUB^(0(9+tre5NOleFxW*E$F>tqE>gg)aw-kWb=<`=J zzEBt9swcYe6ijvQkpSw&-|2E!e-{fG3e?OTs=UF2gw7~RQEnwfY1yMWjW?zf9`HguO0dMnb@wU7%Z-+0wP0$+V^{gR`#UPVk?8zu;68ZkaPIY=<2X$&5q_(lf^vLub1 zm>NjqY-!x!xurGuj51By`y%<}$QiXM*k6oVkn z@*+lXLBdyJDPrzCn7RtU>r}VrZ2CY&(akL?OenYjyF$oU%=5FN{}!Gwr;~WbXi>39 zrr6bV+KsoVuc`fJsyMWTcVV5_aK%jgeo}|l2LXGSa?-U(Jx%&L7nIPhaPj8n=)feRI3xKJqdvYV*a%TIKRC7tLxwLB8 zt#5p4jvu}FWtmhzB-anE+IQ<3o;-|ud$&cYZa}UZShcB)7q&R5`kY*SZq@v?r*6ai z%>JqUH;$){E%P57zjsI-eR4-%yuMHN^slnX90JQu$E8HaC8?ud?&y!#_sgEkJD$;m zXY{LP$ulK;rl3G|{f7Hl?WeU;!wI?J#Mbeh&cQ_Ipwu}mcMiuJhUMy!RdcegX=7ri zxhv7!B{iRxn@?}sc6!DVJ!4YOxZE=yZyuNHCRT09+QyCZJ547OO(&(MF1e{|EBJ@- z@555}u-rWyZyJ_sM^>%7Ek`%c{N{tFAN*6(c1SvVMLv54&e6bLpR0UH(Jii3cD)J? z*q*m`?X>nMTKm83ky^*))^WhsNltIFr5#dk$=RGdb{bN(lJiLNXcwevBxlQWJZL+f za30^}wnijpuk7rNJ9{CqcMPU$PY+W_=8=GxIHTVp$lwl4efQmW2j-Csp#)^-li}kd zcKug&W~A8kRT&L1=gLjnXWau@*k@(?%iz@&hWsOBI1;XbYdTz&sj$CZ2aQ&+OIfKU z+DSLbcR*PZ}BvMph2+Z4Cnm24Me+l4s!?dj7*s&AG(F4$t?oBbp zFQpT+tWf5dBL}PP5z&&t37`X7m$(m#@oxiA zDu~+0b6`#oeRFq1aB&HD1i}A;saJIO-(O--@q2ZOF}T3N6%_v`P|9o1lIkX8!-`?` zu$jk8CB-=9yE^dBRmC*ryL@%9@4BLYcVGezJ<(=dQxBz$a~zsS{sz(;?(;!0pjhF$ zo)3uPLugNWyVnQ#{3Sv(7n;2(DEbH7yuxCO$z=vTSGec1;$d}quUDS2M_K4!0Y||J zorDGCC|&uBkAC*y&(@bD$5GjF6b`M+E7$&Xqio9*FYl4cd*pJs-ojf6^RN1!j7iSJ zF~e?A@k)68wp8Smi@Y&?vb1vT+~bR%T#PrJlSWD%Ad_cSv=6K@Z;8vbLhu|`T%pM;- zA0D+Me+NMXz~>ZEPQ>F-eI!)L_>GA0HGpO9+0*F^EZhtMU}-}YbUqDO%2=|eD>l^Z zQ2<`*;q}psia0{DOopaC3V@b{h%(jyo`?o27(>e*rGX-=!LvtcpxTC&X`t3&IJ-w_ zpt;fjhED^hbOzAP8nBoQIJf&lwe#3zKm4_znYR+6p7%fB&fVfg)$dL;7TH_o*EMZX zow$E_Y+ykUH6;mkY3h3eO-n(@zq-j)b)fExGet5g8IA(;Oi5~E05@nyI6jEiXYE|z z{ku2Y;X=MWxZrOOJOCAkx9UO8#|7quwp&UC*)HJi*}%2yzM;3r$9-@0T_5pUQi=?& zFa7s9{uH!;?*gEh;4&3uUA5qvaR(Rw)?G0eQW$@D@gZK{lWQ5GqU^$ADCh?VS=Cr9 ze1N2;tgsQl`%$FIyL5vq|HX%R&&&S})cOqm!YlxAm{ARVY+bW1k0#45EKlJ5+Wn6r zA4b+kH=>fGOLlZEkL;F|t+n8->TjE$9(&gQX?whLOlrI$H(rrS#^sXn1goC_CIUfhYo8l2>a0O zD}?!UVwM%o&qJ?`H$qi#uZGOBbd;u#$Xvh&KxRMrpbc!0`U#r4C1U1hgA4pBl>K}7 z3x5Uxr>iP8S&V&$@gx|J#MH=4O`NGoBBnCIR7y;h%v8mhDuSs_Fx3)MD>Jomrj}r; z5=@oEcx1*CXFR)%?dM~U#+J!LW()N)R_*h7%T%dl#2{QG8dDZ|8XCg?H2}O?6mEs! zD--tp;KHon4+i1mlZS#*4n1iLpml7^x++!-1jS#(@=aLYfplDWz70RKgILicFs0sj z0SU?2q0Ewz;G>xOO9VLfWT3uh&#T7c0{;YXC^3Q=AQ(ED-YcSL%d#1tBxPMDPm;1P zlP5`$49wCn8coqMd6JZ4nLJ6#vP_;N)fhkEm!xXq2mJPoZ|P{%%?C#>)x2+WN#+;> s4c0;fP9yEuqcq^87+Y*9p|6VTVZPbrfu9dO8d@gLo}OadfF?urzkyTl!2kdN literal 0 HcmV?d00001 diff --git a/src/build/out/build.log b/src/build/out/build.log new file mode 100644 index 00000000..e69de29b diff --git a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_user_config.h b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_user_config.h index 0b9c66a6..4a8bfaa8 100644 --- a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_user_config.h +++ b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_user_config.h @@ -87,20 +87,6 @@ #define SPD_LOWERLIM (-0.105f) /* Speed loop PID output lower limit. */ #define SPD_UPPERLIM 0.105f /* Speed loop PID output upper limit. */ -/* MOTOR PARAMS */ -/* Np, Rs, Ld, Lq, Psif, J, Nmax, Currmax, PPMR, zShift. */ -/* mtrPsif & mtrJ parameter is not used in this project, temporarily set to 0. */ -// #define MOTORPARAM_DEFAULTS { \ -// .mtrNp = 7, \ -// .mtrRs = 5.1f, \ -// .mtrLd = 0.00133f, \ -// .mtrLq = 0.00133f, \ -// .mtrPsif = 0.0f, \ -// .mtrJ = 0.0f, \ -// .maxElecSpd = 180.25f, \ -// .maxCurr = 0.105f, \ -// .busVolt = INV_VOLTAGE_BUS, \ -// } #define MOTORPARAM_DEFAULTS { \ .mtrNp = 21, \ diff --git a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/readme.md b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/readme.md deleted file mode 100644 index 98b2a068..00000000 --- a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/readme.md +++ /dev/null @@ -1,10 +0,0 @@ -# pmsm_sensorless_2shunt_foc - -**【功能描述】** -+ 基于ECMCU105H/ECBMCU201MPC单板的单电机双电阻采样Foc应用 - -**【环境要求】** -+ 所有单板电源改制为演示用的12V低压,电机选用Gimbal GBM2804H-100T - -**【IDE配置方法】** -+ chipConfig中的Sample栏目里面选中pmsm sensorless 2shunt foc示例,然后点击生成代码即可 diff --git a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/readme.md b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/readme.md deleted file mode 100644 index d0e25b7d..00000000 --- a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/readme.md +++ /dev/null @@ -1,20 +0,0 @@ -# 10025_Six_Step_Wave - -**【功能描述】** - -- 基于ECMCU105H/ECBMCU201MPC单板的反电动势采样的无感电机六步方波调速系统应用 - -**【环境要求】** - -- 所有单板电源改制为演示用的24V低压,电机选用云台PM120(10025) -- 电机(黄绿蓝)相线分别对应功率板上U/V/W通道接口 - -**【配置方法】** - -- 将user文件夹替换原工程user文件夹即可 - -【操作说明】 - -- 点击开发板按键启动电机 - -- 开发板蓝色电位器,可以调节电机转动的快慢。(15-78HZ) \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/generatecode/feature.h b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/generatecode/feature.h deleted file mode 100644 index 715371be..00000000 --- a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/generatecode/feature.h +++ /dev/null @@ -1,94 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file feature.h - * @author MCU Driver Team - * @brief This file contains macro configurations related to the project. This file is generated by the IDE tool. - * @date 2025-04-14 14:05:10 - */ - -#ifndef McuMagicTag_FEATURE_H -#define McuMagicTag_FEATURE_H - -/* Macro definitions --------------------------------------------------------- */ -#define CHIP_3061MNPICA MACRO_ENABLE - -#define MACRO_ENABLE 1 -#define MACRO_DISABLE 0 - -/* Macro switch */ -#define BASE_DEFINE_USE_ASSERT MACRO_ENABLE -#ifndef FLASH_CRC_CONFIG -#define FLASH_CRC_CONFIG -#endif /* #ifndef FLASH_CRC_CONFIG */ -#define BASE_MATH_SINCOS_MIDDLE_TABLE MACRO_ENABLE /**< This macro is used to control the table type when the - BASE_MATH_GetSinCos() queries the table. When the value of - this macro is MACRO_ENABLE, the error value obtained by the - BASE_MATH_GetSinCos() is relatively small, and the return - value of the function may be greater than or less than the - actual value. When the value of this macro is MACRO_DISABLE, - the error value obtained by the BASE_MATH_GetSinCos() is - relatively large. However, in the range [0°, 180°) and - [180°, 360°), the return value of the function is either - greater than or less than the actual value. */ - -/* Peripheral module macro switch--------------------------------------------- */ -#define BOARD_DIM_NUM 1 /**< Number of dimming handle arrays. */ - -#define BOARD_KEY_NUM 10 /**< Number of key handle arrays. */ -#define BOARD_KEY_PRESS_ON GPIO_HIGH_LEVEL /**< GPIO status corresponding to long press valid. */ -#define BOARD_KEY_PRESS_OFF GPIO_LOW_LEVEL /**< GPIO status corresponding to short press valid. */ - -#define BOARD_LED_SEG_NUM 4 /**< Number of segments. */ -#define BOARD_LED_SEGMENT_ON GPIO_HIGH_LEVEL /**< GPIO level status corresponding to valid segments. */ -#define BOARD_LED_SEGMENT_OFF GPIO_LOW_LEVEL /**< GPIO level status corresponding to invalid segments. */ - -#define BOARD_MKEY_SCHEME_NUMBER BOARD_MKEY_SCHEME_NUMBER_ONE /**< Define the scheme to be adopted. */ -#define BOARD_MKEY_OUT_NUM 4 /**< Number of GPIO pins used as output during scanning. */ -#define BOARD_MKEY_IN_NUM 4 /**< Number of GPIO pins used as input during scanning. */ -#define BOARD_MKEY_OUT_PIN_VALID GPIO_LOW_LEVEL /**< GPIO level status corresponding to the valid \ - status of the output GPIO in the key matrix. */ -#define BOARD_MKEY_OUT_PIN_INVALID GPIO_HIGH_LEVEL /**< GPIO level status corresponding to the \ - invalid status of the output GPIO in the key matrix. */ -#define BOARD_MKEY_IN_PIN_VALID GPIO_LOW_LEVEL /**< Indicates the GPIO level corresponding to the \ - valid status of the input GPIO in the key matrix. */ -#define BOARD_MKEY_IN_PIN_INVALID GPIO_HIGH_LEVEL /**< Indicates the GPIO level corresponding to the \ - invalid status of the input GPIO in the key matrix. */ - -#define BOARD_PULSES_NUM 2 /**< Number of pulse handles. */ - -#define BASE_DEFINE_SLIPAVERAGE_NUM 2 /**< Sliding average array length. */ - -#define LISTNODE_MAX 20 - -#define BASE_DEFINE_DMA_QUICKSTART - -#define XTRAIL_FREQ 30000000U - -#define DBG_USE_NO_PRINTF 0U -#define DBG_USE_UART_PRINTF 1U - -#define DBG_PRINTF_USE DBG_USE_UART_PRINTF -#if (DBG_PRINTF_USE == DBG_USE_UART_PRINTF) -#define DBG_PRINTF_UART_PORT UART0 -#endif - -/* USER CODE BEGIN 0 */ -/* USER CODE 区域内代码不会被覆盖,区域外会被生成的默认代码覆盖(其余USER CODE 区域同理) */ -/* USER CODE END 0 */ - -#endif /* McuMagicTag_FEATURE_H */ \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/generatecode/main.h b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/generatecode/main.h deleted file mode 100644 index 40d65b1d..00000000 --- a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/generatecode/main.h +++ /dev/null @@ -1,98 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file main.h - * @author MCU Driver Team - * @brief This file contains driver init functions. - * @date 2025-04-14 14:05:10 - */ - -/* Define to prevent recursive inclusion ------------------------------------- */ -#ifndef McuMagicTag_SYSTEM_INIT_H -#define McuMagicTag_SYSTEM_INIT_H - -#include "adc.h" -#include "adc_ex.h" -#include "acmp.h" -#include "acmp_ex.h" -#include "apt.h" -#include "uart.h" -#include "uart_ex.h" -#include "gpio.h" -#include "timer.h" -#include "timer_ex.h" -#include "pga.h" -#include "crg.h" -#include "dma.h" -#include "dma_ex.h" -#include "iocmg.h" - -#define IO_SPEED_FAST 0x00U -#define IO_SPEED_SLOW 0x01U - -#define IO_DRV_LEVEL4 0x00U -#define IO_DRV_LEVEL3 0x01U -#define IO_DRV_LEVEL2 0x02U -#define IO_DRV_LEVEL1 0x03U - -#define XTAL_DRV_LEVEL4 0x03U -#define XTAL_DRV_LEVEL3 0x02U -#define XTAL_DRV_LEVEL2 0x01U -#define XTAL_DRV_LEVEL1 0x00U - -#define GpioStartStop_PIN GPIO_PIN_4 -#define GpioStartStop_HANDLE g_gpio2 - -extern ACMP_Handle g_acmp0; -extern PGA_Handle g_pga0; -extern PGA_Handle g_pga1; -extern TIMER_Handle g_timer0; -extern TIMER_Handle g_timer1; -extern UART_Handle g_uart0; -extern APT_Handle g_apt0; -extern APT_Handle g_apt1; -extern APT_Handle g_apt2; -extern ADC_Handle g_adc0; - -extern DMA_Handle g_dmac; - -extern GPIO_Handle g_gpio2; -extern GPIO_Handle g_gpio1; - -BASE_StatusType CRG_Config(CRG_CoreClkSelect *coreClkSelect); -void SystemInit(void); - -void UART0WriteInterruptCallback(void *handle); -void UART0ReadInterruptCallback(void *handle); - -void UART0InterruptErrorCallback(void *handle); -void MotorStatemachineCallBack(void *handle); -void TIMER1_DMAOverFlow_InterruptProcess(void *handle); -void CheckPotentiometerValueCallback(void *handle); -void TIMER0_DMAOverFlow_InterruptProcess(void *handle); -void MotorCarrierProcessCallback(void *aptHandle); -void MotorSysErrCallback(void *aptHandle); - -void UART0_TXDMACallback(void *handle); - -void MotorStartStopKeyCallback(void *param); - -/* USER CODE BEGIN 0 */ -/* USER CODE 区域内代码不会被覆盖,区域外会被生成的默认代码覆盖(其余USER CODE 区域同理) */ -/* USER CODE END 0 */ - -#endif /* McuMagicTag_SYSTEM_INIT_H */ \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/generatecode/system_init.c b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/generatecode/system_init.c deleted file mode 100644 index d49cd860..00000000 --- a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/generatecode/system_init.c +++ /dev/null @@ -1,677 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file system_init.c - * @author MCU Driver Team - * @brief This file contains driver init functions. - * @date 2025-04-14 14:05:10 - */ - -#include "main.h" -#include "ioconfig.h" -#include "iocmg_ip.h" - -#define UART0_BAUD_RATE 1843200 - -BASE_StatusType CRG_Config(CRG_CoreClkSelect *coreClkSelect) -{ - CRG_Handle crg; - crg.baseAddress = CRG; - crg.pllRefClkSelect = CRG_PLL_REF_CLK_SELECT_HOSC; - crg.pllPreDiv = CRG_PLL_PREDIV_4; - crg.pllFbDiv = 48; /* PLL Multiplier 48 */ - crg.pllPostDiv = CRG_PLL_POSTDIV_2; - crg.coreClkSelect = CRG_CORE_CLK_SELECT_PLL; - crg.handleEx.pllPostDiv2 = CRG_PLL_POSTDIV2_3; - crg.handleEx.clk1MSelect = CRG_1M_CLK_SELECT_HOSC; - crg.handleEx.clk1MDiv = (25 - 1); /* The 1 MHz freq is equal to the input clock frequency / (clk_1m_div + 1). 25 is the div of the clk_1m in CLOCK. */ - - if (HAL_CRG_Init(&crg) != BASE_STATUS_OK) { - return BASE_STATUS_ERROR; - } - *coreClkSelect = crg.coreClkSelect; - return BASE_STATUS_OK; -} - -static void DMA_Channel0Init(void *handle) -{ - DMA_ChannelParam dma_param; - dma_param.direction = DMA_MEMORY_TO_PERIPH_BY_DMAC; - dma_param.srcAddrInc = DMA_ADDR_INCREASE; - dma_param.destAddrInc = DMA_ADDR_UNALTERED; - dma_param.srcPeriph = DMA_REQUEST_MEM; - dma_param.destPeriph = DMA_REQUEST_UART0_TX; - dma_param.srcWidth = DMA_TRANSWIDTH_BYTE; - dma_param.destWidth = DMA_TRANSWIDTH_BYTE; - dma_param.srcBurst = DMA_BURST_LENGTH_1; - dma_param.destBurst = DMA_BURST_LENGTH_1; - dma_param.pHandle = handle; - HAL_DMA_InitChannel(&g_dmac, &dma_param, DMA_CHANNEL_ZERO); -} - -static void DMA_Init(void) -{ - HAL_CRG_IpEnableSet(DMA_BASE, IP_CLK_ENABLE); - g_dmac.baseAddress = DMA; - IRQ_Register(IRQ_DMA_TC, HAL_DMA_IrqHandlerTc, &g_dmac); - IRQ_Register(IRQ_DMA_ERR, HAL_DMA_IrqHandlerError, &g_dmac); - IRQ_EnableN(IRQ_DMA_TC); - IRQ_EnableN(IRQ_DMA_ERR); - HAL_DMA_Init(&g_dmac); - DMA_Channel0Init((void *)(&g_uart0)); - HAL_DMA_SetChannelPriorityEx(&g_dmac, DMA_CHANNEL_ZERO, DMA_PRIORITY_HIGHEST); -} - -static void ACMP0_Init(void) -{ - HAL_CRG_IpEnableSet(ACMP0_BASE, IP_CLK_ENABLE); /* ACMP clock bit reset. */ - g_acmp0.baseAddress = ACMP0_BASE; - g_acmp0.inOutConfig.inputNNum = ACMP_INPUT_N_SELECT2; - g_acmp0.inOutConfig.inputPNum = ACMP_INPUT_P_SELECT2; - g_acmp0.inOutConfig.polarity = ACMP_OUT_NOT_INVERT; - g_acmp0.filterCtrl.filterMode = ACMP_FILTER_NONE; - g_acmp0.hysteresisVol = ACMP_HYS_VOL_30MV; - g_acmp0.interruptEn = BASE_CFG_UNSET; - HAL_ACMP_Init(&g_acmp0); -} - -static void ADC0_Init(void) -{ - HAL_CRG_IpEnableSet(ADC0_BASE, IP_CLK_ENABLE); - HAL_CRG_IpClkSelectSet(ADC0_BASE, CRG_ADC_CLK_ASYN_PLL_DIV); - HAL_CRG_IpClkDivSet(ADC0_BASE, CRG_ADC_DIV_1); - - g_adc0.baseAddress = ADC0; - g_adc0.socPriority = ADC_PRIMODE_ALL_ROUND; - - HAL_ADC_Init(&g_adc0); - - SOC_Param socParam = {0}; - socParam.adcInput = ADC_CH_ADCINA0; /* PGA0_OUT(ADC AIN0) */ - socParam.sampleTotalTime = ADC_SOCSAMPLE_22CLK; /* adc sample total time 22 adc_clk */ - socParam.trigSource = ADC_TRIGSOC_APT0_SOCA; - socParam.continueMode = BASE_CFG_DISABLE; - socParam.finishMode = ADC_SOCFINISH_NONE; - HAL_ADC_ConfigureSoc(&g_adc0, ADC_SOC_NUM1, &socParam); - - socParam.adcInput = ADC_CH_ADCINA1; /* PGA1_OUT(ADC AIN1) */ - socParam.sampleTotalTime = ADC_SOCSAMPLE_22CLK; /* adc sample total time 22 adc_clk */ - socParam.trigSource = ADC_TRIGSOC_APT0_SOCA; - socParam.continueMode = BASE_CFG_DISABLE; - socParam.finishMode = ADC_SOCFINISH_NONE; - HAL_ADC_ConfigureSoc(&g_adc0, ADC_SOC_NUM6, &socParam); - - socParam.adcInput = ADC_CH_ADCINA9; /* PIN7(ADC AIN9) */ - socParam.sampleTotalTime = ADC_SOCSAMPLE_22CLK; /* adc sample total time 22 adc_clk */ - socParam.trigSource = ADC_TRIGSOC_SOFT; - socParam.continueMode = BASE_CFG_DISABLE; - socParam.finishMode = ADC_SOCFINISH_NONE; - HAL_ADC_ConfigureSoc(&g_adc0, ADC_SOC_NUM0, &socParam); - - socParam.adcInput = ADC_CH_ADCINA5; /* PIN2(ADC AIN5) */ - socParam.sampleTotalTime = ADC_SOCSAMPLE_22CLK; /* adc sample total time 22 adc_clk */ - socParam.trigSource = ADC_TRIGSOC_SOFT; - socParam.continueMode = BASE_CFG_DISABLE; - socParam.finishMode = ADC_SOCFINISH_NONE; - HAL_ADC_ConfigureSoc(&g_adc0, ADC_SOC_NUM3, &socParam); - - socParam.adcInput = ADC_CH_ADCINA15; /* PIN14(ADC AIN15) */ - socParam.sampleTotalTime = ADC_SOCSAMPLE_22CLK; /* adc sample total time 22 adc_clk */ - socParam.trigSource = ADC_TRIGSOC_SOFT; - socParam.continueMode = BASE_CFG_DISABLE; - socParam.finishMode = ADC_SOCFINISH_NONE; - HAL_ADC_ConfigureSoc(&g_adc0, ADC_SOC_NUM4, &socParam); -} - -__weak void MotorSysErrCallback(void *aptHandle) -{ - BASE_FUNC_UNUSED(aptHandle); - /* USER CODE BEGIN APT0_EVENT_INTERRUPT */ - /* USER CODE END APT0_EVENT_INTERRUPT */ -} - -__weak void MotorCarrierProcessCallback(void *aptHandle) -{ - BASE_FUNC_UNUSED(aptHandle); - /* USER CODE BEGIN APT0_TIMER_INTERRUPT */ - /* USER CODE END APT0_TIMER_INTERRUPT */ -} - -static void APT0_ProtectInit(void) -{ - APT_OutCtrlProtectEx protectApt = {0}; - protectApt.ocEventEnEx = BASE_CFG_ENABLE; - protectApt.ocEventModeEx = APT_OUT_CTRL_ONE_SHOT; - protectApt.ocActionEx = APT_OUT_CTRL_ACTION_LOW; - protectApt.ocActionBEx = APT_OUT_CTRL_ACTION_LOW; - protectApt.ocEvtInterruptEnEx = BASE_CFG_ENABLE; - protectApt.ocSysEvent = APT_SYS_EVT_DEBUG | APT_SYS_EVT_CLK | APT_SYS_EVT_MEM; - protectApt.originalEvtEx = APT_EM_ORIGINAL_SRC_ACMP0; - protectApt.evtPolarityMaskEx = APT_EM_ACMP0_INVERT_BIT; - protectApt.filterCycleNumEx = 0; - HAL_APT_ProtectInitEx(&g_apt0, &protectApt); -} - -static void APT0_Init(void) -{ - HAL_CRG_IpEnableSet(APT0_BASE, IP_CLK_ENABLE); - - g_apt0.baseAddress = APT0; - - /* Clock Settings */ - g_apt0.waveform.dividerFactor = 1 - 1; - /* Timer Settings */ - g_apt0.waveform.timerPeriod = 7500; /* 7500 is count period of APT time-base timer */ - g_apt0.waveform.cntMode = APT_COUNT_MODE_UP_DOWN; - - /* Wave Form */ - g_apt0.waveform.basicType = APT_PWM_BASIC_A_HIGH_B_LOW; - g_apt0.waveform.chAOutType = APT_PWM_OUT_BASIC_TYPE; - g_apt0.waveform.chBOutType = APT_PWM_OUT_BASIC_TYPE; - g_apt0.waveform.divInitVal = 0; - g_apt0.waveform.cntInitVal = 0; - g_apt0.waveform.cntCmpLeftEdge = 500; /* 500 is count compare point of the left edge of PWM waveform */ - g_apt0.waveform.cntCmpRightEdge = 4000; /* 4000 is count compare point of the right edge of PWM waveform */ - g_apt0.waveform.cntCmpLoadMode = APT_BUFFER_INDEPENDENT_LOAD; - g_apt0.waveform.cntCmpLoadEvt = APT_COMPARE_LOAD_EVENT_ZERO; - g_apt0.waveform.deadBandCnt = 225; /* 225 is dead-band value */ - - /* ADC Trigger SOCA */ - g_apt0.adcTrg.trgEnSOCA = BASE_CFG_ENABLE; - g_apt0.adcTrg.cntCmpSOCA = 375; /* 375 is count compare point of ADC trigger source SOCA when using CMPA */ - g_apt0.adcTrg.trgSrcSOCA = APT_CS_SRC_CNTR_CMPA_DOWN; - g_apt0.adcTrg.trgScaleSOCA = 1; - - /* ADC Trigger SOCB */ - g_apt0.adcTrg.trgEnSOCB = BASE_CFG_ENABLE; - g_apt0.adcTrg.cntCmpSOCB = 1; - g_apt0.adcTrg.trgSrcSOCB = APT_CS_SRC_CNTR_CMPB_DOWN; - g_apt0.adcTrg.trgScaleSOCB = 1; - - g_apt0.adcTrg.cntCmpLoadMode = APT_BUFFER_INDEPENDENT_LOAD; - g_apt0.adcTrg.cntCmpLoadEvt = APT_COMPARE_LOAD_EVENT_ZERO; - - /* Timer Trigger */ - g_apt0.tmrInterrupt.tmrInterruptEn = BASE_CFG_ENABLE; - g_apt0.tmrInterrupt.tmrInterruptSrc = APT_INT_SRC_CNTR_ZERO; - g_apt0.tmrInterrupt.tmrInterruptScale = 1; - - APT0_ProtectInit(); - - HAL_APT_PWMInit(&g_apt0); - HAL_APT_RegisterCallBack(&g_apt0, APT_EVENT_INTERRUPT, MotorSysErrCallback); - IRQ_SetPriority(IRQ_APT0_EVT, 7); /* 7 is priority value */ - IRQ_Register(IRQ_APT0_EVT, HAL_APT_EventIrqHandler, &g_apt0); - IRQ_EnableN(IRQ_APT0_EVT); - HAL_APT_RegisterCallBack(&g_apt0, APT_TIMER_INTERRUPT, MotorCarrierProcessCallback); - IRQ_SetPriority(IRQ_APT0_TMR, 5); /* 5 is priority value */ - IRQ_Register(IRQ_APT0_TMR, HAL_APT_TimerIrqHandler, &g_apt0); - IRQ_EnableN(IRQ_APT0_TMR); -} - -static void APT1_ProtectInit(void) -{ - APT_OutCtrlProtectEx protectApt = {0}; - protectApt.ocEventEnEx = BASE_CFG_ENABLE; - protectApt.ocEventModeEx = APT_OUT_CTRL_ONE_SHOT; - protectApt.ocActionEx = APT_OUT_CTRL_ACTION_LOW; - protectApt.ocActionBEx = APT_OUT_CTRL_ACTION_LOW; - protectApt.ocEvtInterruptEnEx = BASE_CFG_DISABLE; - protectApt.ocSysEvent = APT_SYS_EVT_DEBUG | APT_SYS_EVT_CLK | APT_SYS_EVT_MEM; - protectApt.originalEvtEx = APT_EM_ORIGINAL_SRC_ACMP0; - protectApt.evtPolarityMaskEx = APT_EM_ACMP0_INVERT_BIT; - protectApt.filterCycleNumEx = 0; - HAL_APT_ProtectInitEx(&g_apt1, &protectApt); -} - -static void APT1_Init(void) -{ - HAL_CRG_IpEnableSet(APT1_BASE, IP_CLK_ENABLE); - - g_apt1.baseAddress = APT1; - - /* Clock Settings */ - g_apt1.waveform.dividerFactor = 1 - 1; - /* Timer Settings */ - g_apt1.waveform.timerPeriod = 7500; /* 7500 is count period of APT time-base timer */ - g_apt1.waveform.cntMode = APT_COUNT_MODE_UP_DOWN; - - /* Wave Form */ - g_apt1.waveform.basicType = APT_PWM_BASIC_A_HIGH_B_LOW; - g_apt1.waveform.chAOutType = APT_PWM_OUT_BASIC_TYPE; - g_apt1.waveform.chBOutType = APT_PWM_OUT_BASIC_TYPE; - g_apt1.waveform.divInitVal = 0; - g_apt1.waveform.cntInitVal = 0; - g_apt1.waveform.cntCmpLeftEdge = 500; /* 500 is count compare point of the left edge of PWM waveform */ - g_apt1.waveform.cntCmpRightEdge = 4000; /* 4000 is count compare point of the right edge of PWM waveform */ - g_apt1.waveform.cntCmpLoadMode = APT_BUFFER_INDEPENDENT_LOAD; - g_apt1.waveform.cntCmpLoadEvt = APT_COMPARE_LOAD_EVENT_ZERO; - g_apt1.waveform.deadBandCnt = 225; /* 225 is dead-band value */ - - APT1_ProtectInit(); - - HAL_APT_PWMInit(&g_apt1); -} - -static void APT2_ProtectInit(void) -{ - APT_OutCtrlProtectEx protectApt = {0}; - protectApt.ocEventEnEx = BASE_CFG_ENABLE; - protectApt.ocEventModeEx = APT_OUT_CTRL_ONE_SHOT; - protectApt.ocActionEx = APT_OUT_CTRL_ACTION_LOW; - protectApt.ocActionBEx = APT_OUT_CTRL_ACTION_LOW; - protectApt.ocEvtInterruptEnEx = BASE_CFG_DISABLE; - protectApt.ocSysEvent = APT_SYS_EVT_DEBUG | APT_SYS_EVT_CLK | APT_SYS_EVT_MEM; - protectApt.originalEvtEx = APT_EM_ORIGINAL_SRC_ACMP0; - protectApt.evtPolarityMaskEx = APT_EM_ACMP0_INVERT_BIT; - protectApt.filterCycleNumEx = 0; - HAL_APT_ProtectInitEx(&g_apt2, &protectApt); -} - -static void APT2_Init(void) -{ - HAL_CRG_IpEnableSet(APT2_BASE, IP_CLK_ENABLE); - - g_apt2.baseAddress = APT2; - - /* Clock Settings */ - g_apt2.waveform.dividerFactor = 1 - 1; - /* Timer Settings */ - g_apt2.waveform.timerPeriod = 7500; /* 7500 is count period of APT time-base timer */ - g_apt2.waveform.cntMode = APT_COUNT_MODE_UP_DOWN; - - /* Wave Form */ - g_apt2.waveform.basicType = APT_PWM_BASIC_A_HIGH_B_LOW; - g_apt2.waveform.chAOutType = APT_PWM_OUT_BASIC_TYPE; - g_apt2.waveform.chBOutType = APT_PWM_OUT_BASIC_TYPE; - g_apt2.waveform.divInitVal = 0; - g_apt2.waveform.cntInitVal = 0; - g_apt2.waveform.cntCmpLeftEdge = 500; /* 500 is count compare point of the left edge of PWM waveform */ - g_apt2.waveform.cntCmpRightEdge = 4000; /* 4000 is count compare point of the right edge of PWM waveform */ - g_apt2.waveform.cntCmpLoadMode = APT_BUFFER_INDEPENDENT_LOAD; - g_apt2.waveform.cntCmpLoadEvt = APT_COMPARE_LOAD_EVENT_ZERO; - g_apt2.waveform.deadBandCnt = 225; /* 225 is dead-band value */ - - APT2_ProtectInit(); - - HAL_APT_PWMInit(&g_apt2); -} - -__weak void MotorStartStopKeyCallback(void *param) -{ - GPIO_Handle *handle = (GPIO_Handle *)param; - BASE_FUNC_UNUSED(handle); -} - -static void GPIO_Init(void) -{ - HAL_CRG_IpEnableSet(GPIO2_BASE, IP_CLK_ENABLE); - g_gpio2.baseAddress = GPIO2; - - g_gpio2.pins = GPIO_PIN_3; - HAL_GPIO_Init(&g_gpio2); - HAL_GPIO_SetDirection(&g_gpio2, g_gpio2.pins, GPIO_OUTPUT_MODE); - HAL_GPIO_SetValue(&g_gpio2, g_gpio2.pins, GPIO_HIGH_LEVEL); - HAL_GPIO_SetIrqType(&g_gpio2, g_gpio2.pins, GPIO_INT_TYPE_NONE); - - g_gpio2.pins = GPIO_PIN_4; - HAL_GPIO_Init(&g_gpio2); - HAL_GPIO_SetDirection(&g_gpio2, g_gpio2.pins, GPIO_INPUT_MODE); - HAL_GPIO_SetValue(&g_gpio2, g_gpio2.pins, GPIO_HIGH_LEVEL); - HAL_GPIO_SetIrqType(&g_gpio2, g_gpio2.pins, GPIO_INT_TYPE_LOW_LEVEL); - - HAL_CRG_IpEnableSet(GPIO1_BASE, IP_CLK_ENABLE); - g_gpio1.baseAddress = GPIO1; - - g_gpio1.pins = GPIO_PIN_0; - HAL_GPIO_Init(&g_gpio1); - HAL_GPIO_SetDirection(&g_gpio1, g_gpio1.pins, GPIO_OUTPUT_MODE); - HAL_GPIO_SetValue(&g_gpio1, g_gpio1.pins, GPIO_HIGH_LEVEL); - HAL_GPIO_SetIrqType(&g_gpio1, g_gpio1.pins, GPIO_INT_TYPE_NONE); - - HAL_GPIO_RegisterCallBack(&g_gpio2, GPIO_PIN_4, MotorStartStopKeyCallback); - IRQ_Register(IRQ_GPIO2, HAL_GPIO_IrqHandler, &g_gpio2); - IRQ_SetPriority(IRQ_GPIO2, 1); /* set gpio1 interrupt priority to 1, 1~15. 1 is priority value */ - IRQ_EnableN(IRQ_GPIO2); /* gpio interrupt enable */ - - return; -} - -static void PGA0_Init(void) -{ - HAL_CRG_IpEnableSet(PGA0_BASE, IP_CLK_ENABLE); - - g_pga0.baseAddress = PGA0_BASE; - g_pga0.externalResistorMode = BASE_CFG_ENABLE; - g_pga0.handleEx.extCapCompensation = PGA_EXT_COMPENSATION_2X; - HAL_PGA_Init(&g_pga0); -} - -static void PGA1_Init(void) -{ - HAL_CRG_IpEnableSet(PGA1_BASE, IP_CLK_ENABLE); - - g_pga1.baseAddress = PGA1_BASE; - g_pga1.externalResistorMode = BASE_CFG_ENABLE; - g_pga1.handleEx.extCapCompensation = PGA_EXT_COMPENSATION_2X; - HAL_PGA_Init(&g_pga1); -} - -__weak void CheckPotentiometerValueCallback(void *handle) -{ - BASE_FUNC_UNUSED(handle); - /* USER CODE BEGIN CheckPotentiometerValueCallback */ - /* USER CODE END CheckPotentiometerValueCallback */ -} - -static void TIMER0_Init(void) -{ - HAL_CRG_IpEnableSet(TIMER0_BASE, IP_CLK_ENABLE); /* TIMER0 clock enable. */ - unsigned int load = ((float)HAL_CRG_GetIpFreq((void *)TIMER0) / (1u << (TIMERPRESCALER_NO_DIV * 4)) / 1000000u) * 1000000; - - g_timer0.baseAddress = TIMER0; - g_timer0.load = (load < 1) ? 0 : (load - 1); /* Set timer value immediately */ - g_timer0.bgLoad = (load < 1) ? 0 : (load - 1); /* Set timer value */ - g_timer0.mode = TIMER_MODE_RUN_PERIODIC; /* Run in period mode */ - g_timer0.prescaler = TIMERPRESCALER_NO_DIV; /* Don't frequency division */ - g_timer0.size = TIMER_SIZE_32BIT; /* 1 for 32bit, 0 for 16bit */ - g_timer0.interruptEn = BASE_CFG_ENABLE; - g_timer0.adcSocReqEnable = BASE_CFG_DISABLE; - g_timer0.dmaReqEnable = BASE_CFG_DISABLE; - HAL_TIMER_Init(&g_timer0); - IRQ_Register(IRQ_TIMER0, HAL_TIMER_IrqHandler, &g_timer0); - - HAL_TIMER_RegisterCallback(&g_timer0, TIMER_PERIOD_FIN, CheckPotentiometerValueCallback); - IRQ_SetPriority(IRQ_TIMER0, 1); /* 1 is priority value */ - IRQ_EnableN(IRQ_TIMER0); -} - -__weak void MotorStatemachineCallBack(void *handle) -{ - BASE_FUNC_UNUSED(handle); - /* USER CODE BEGIN MotorStatemachineCallBack */ - /* USER CODE END MotorStatemachineCallBack */ -} - -static void TIMER1_Init(void) -{ - HAL_CRG_IpEnableSet(TIMER1_BASE, IP_CLK_ENABLE); /* TIMER1 clock enable. */ - unsigned int load = ((float)HAL_CRG_GetIpFreq((void *)TIMER1) / (1u << (TIMERPRESCALER_NO_DIV * 4)) / 1000000u) * 500; - - g_timer1.baseAddress = TIMER1; - g_timer1.load = (load < 1) ? 0 : (load - 1); /* Set timer value immediately */ - g_timer1.bgLoad = (load < 1) ? 0 : (load - 1); /* Set timer value */ - g_timer1.mode = TIMER_MODE_RUN_PERIODIC; /* Run in period mode */ - g_timer1.prescaler = TIMERPRESCALER_NO_DIV; /* Don't frequency division */ - g_timer1.size = TIMER_SIZE_32BIT; /* 1 for 32bit, 0 for 16bit */ - g_timer1.interruptEn = BASE_CFG_ENABLE; - g_timer1.adcSocReqEnable = BASE_CFG_DISABLE; - g_timer1.dmaReqEnable = BASE_CFG_DISABLE; - HAL_TIMER_Init(&g_timer1); - IRQ_Register(IRQ_TIMER1, HAL_TIMER_IrqHandler, &g_timer1); - - HAL_TIMER_RegisterCallback(&g_timer1, TIMER_PERIOD_FIN, MotorStatemachineCallBack); - IRQ_SetPriority(IRQ_TIMER1, 1); /* 1 is priority value */ - IRQ_EnableN(IRQ_TIMER1); -} - -__weak void UART0InterruptErrorCallback(void *handle) -{ - BASE_FUNC_UNUSED(handle); - /* USER CODE BEGIN UART0_TRNS_IT_ERROR */ - /* USER CODE END UART0_TRNS_IT_ERROR */ -} - -__weak void UART0_TXDMACallback(void *handle) -{ - BASE_FUNC_UNUSED(handle); - /* USER CODE BEGIN UART0_WRITE_DMA_FINISH */ - /* USER CODE END UART0_WRITE_DMA_FINISH */ -} - -__weak void UART0WriteInterruptCallback(void *handle) -{ - BASE_FUNC_UNUSED(handle); - /* USER CODE BEGIN UART0_WRITE_IT_FINISH */ - /* USER CODE END UART0_WRITE_IT_FINISH */ -} - -__weak void UART0ReadInterruptCallback(void *handle) -{ - BASE_FUNC_UNUSED(handle); - /* USER CODE BEGIN UART0_READ_IT_FINISH */ - /* USER CODE END UART0_READ_IT_FINISH */ -} - -static void UART0_Init(void) -{ - HAL_CRG_IpEnableSet(UART0_BASE, IP_CLK_ENABLE); /* UART0 clock enable. */ - g_uart0.baseAddress = UART0; - - g_uart0.baudRate = UART0_BAUD_RATE; - g_uart0.dataLength = UART_DATALENGTH_8BIT; - g_uart0.stopBits = UART_STOPBITS_ONE; - g_uart0.parity = UART_PARITY_NONE; - g_uart0.txMode = UART_MODE_DMA; - g_uart0.rxMode = UART_MODE_INTERRUPT; - g_uart0.fifoMode = BASE_CFG_ENABLE; - g_uart0.fifoTxThr = UART_FIFODEPTH_SIZE8; - g_uart0.fifoRxThr = UART_FIFODEPTH_SIZE8; - g_uart0.hwFlowCtr = BASE_CFG_DISABLE; - g_uart0.handleEx.overSampleMultiple = UART_OVERSAMPLING_16X; - g_uart0.handleEx.msbFirst = BASE_CFG_DISABLE; - HAL_UART_Init(&g_uart0); - HAL_UART_RegisterCallBack(&g_uart0, UART_TRNS_IT_ERROR, (UART_CallbackType)UART0InterruptErrorCallback); - HAL_UART_RegisterCallBack(&g_uart0, UART_READ_IT_FINISH, (UART_CallbackType)UART0ReadInterruptCallback); - IRQ_Register(IRQ_UART0, HAL_UART_IrqHandler, &g_uart0); - IRQ_SetPriority(IRQ_UART0, 6); /* 6 is priority value */ - IRQ_EnableN(IRQ_UART0); - g_uart0.dmaHandle = &g_dmac; - g_uart0.uartDmaTxChn = 0; - HAL_UART_RegisterCallBack(&g_uart0, UART_WRITE_DMA_FINISH, (UART_CallbackType)UART0_TXDMACallback); - HAL_UART_RegisterCallBack(&g_uart0, UART_WRITE_IT_FINISH, (UART_CallbackType)UART0WriteInterruptCallback); -} - -static void IOConfig(void) -{ - SYSCTRL0->SC_SYS_STAT.BIT.update_mode = 0; - SYSCTRL0->SC_SYS_STAT.BIT.update_mode_clear = 1; - /* Config PIN39 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO0_3_AS_UART0_TXD); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO0_3_AS_UART0_TXD, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO0_3_AS_UART0_TXD, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO0_3_AS_UART0_TXD, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO0_3_AS_UART0_TXD, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN40 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO0_4_AS_UART0_RXD); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO0_4_AS_UART0_RXD, PULL_UP); /* Pull-up and Pull-down, UART RX recommend PULL_UP */ - HAL_IOCMG_SetPinSchmidtMode(GPIO0_4_AS_UART0_RXD, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO0_4_AS_UART0_RXD, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO0_4_AS_UART0_RXD, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN1 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO0_7_AS_ACMP0_OUT); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO0_7_AS_ACMP0_OUT, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO0_7_AS_ACMP0_OUT, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO0_7_AS_ACMP0_OUT, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO0_7_AS_ACMP0_OUT, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN48 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO0_6_AS_ACMP_N2); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO0_6_AS_ACMP_N2, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO0_6_AS_ACMP_N2, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO0_6_AS_ACMP_N2, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO0_6_AS_ACMP_N2, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN47 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO0_5_AS_ACMP_P2); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO0_5_AS_ACMP_P2, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO0_5_AS_ACMP_P2, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO0_5_AS_ACMP_P2, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO0_5_AS_ACMP_P2, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN4 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO2_6_AS_PGA0_N0); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO2_6_AS_PGA0_N0, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO2_6_AS_PGA0_N0, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO2_6_AS_PGA0_N0, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO2_6_AS_PGA0_N0, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN5 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO2_5_AS_PGA0_P0); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO2_5_AS_PGA0_P0, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO2_5_AS_PGA0_P0, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO2_5_AS_PGA0_P0, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO2_5_AS_PGA0_P0, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN3 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO2_7_AS_PGA0_OUT); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO2_7_AS_PGA0_OUT, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO2_7_AS_PGA0_OUT, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO2_7_AS_PGA0_OUT, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO2_7_AS_PGA0_OUT, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN7 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO3_7_AS_ADC_AIN9); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO3_7_AS_ADC_AIN9, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO3_7_AS_ADC_AIN9, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO3_7_AS_ADC_AIN9, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO3_7_AS_ADC_AIN9, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN2 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO5_1_AS_ADC_AIN5); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO5_1_AS_ADC_AIN5, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO5_1_AS_ADC_AIN5, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO5_1_AS_ADC_AIN5, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO5_1_AS_ADC_AIN5, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN14 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO4_7_AS_ADC_AIN15); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO4_7_AS_ADC_AIN15, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO4_7_AS_ADC_AIN15, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO4_7_AS_ADC_AIN15, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO4_7_AS_ADC_AIN15, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN11 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO1_5_AS_PGA1_P0); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO1_5_AS_PGA1_P0, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO1_5_AS_PGA1_P0, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO1_5_AS_PGA1_P0, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO1_5_AS_PGA1_P0, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN12 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO1_6_AS_PGA1_N0); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO1_6_AS_PGA1_N0, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO1_6_AS_PGA1_N0, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO1_6_AS_PGA1_N0, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO1_6_AS_PGA1_N0, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN13 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO1_7_AS_PGA1_OUT); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO1_7_AS_PGA1_OUT, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO1_7_AS_PGA1_OUT, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO1_7_AS_PGA1_OUT, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO1_7_AS_PGA1_OUT, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN35 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO2_3_AS_GPIO2_3); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO2_3_AS_GPIO2_3, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO2_3_AS_GPIO2_3, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO2_3_AS_GPIO2_3, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO2_3_AS_GPIO2_3, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN27 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO1_0_AS_GPIO1_0); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO1_0_AS_GPIO1_0, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO1_0_AS_GPIO1_0, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO1_0_AS_GPIO1_0, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO1_0_AS_GPIO1_0, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN41 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO2_4_AS_GPIO2_4); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO2_4_AS_GPIO2_4, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO2_4_AS_GPIO2_4, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO2_4_AS_GPIO2_4, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO2_4_AS_GPIO2_4, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN19 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO3_0_AS_APT0_PWMA); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO3_0_AS_APT0_PWMA, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO3_0_AS_APT0_PWMA, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO3_0_AS_APT0_PWMA, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO3_0_AS_APT0_PWMA, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN23 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO4_0_AS_APT0_PWMB); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO4_0_AS_APT0_PWMB, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO4_0_AS_APT0_PWMB, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO4_0_AS_APT0_PWMB, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO4_0_AS_APT0_PWMB, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN20 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO3_1_AS_APT1_PWMA); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO3_1_AS_APT1_PWMA, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO3_1_AS_APT1_PWMA, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO3_1_AS_APT1_PWMA, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO3_1_AS_APT1_PWMA, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN24 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO4_1_AS_APT1_PWMB); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO4_1_AS_APT1_PWMB, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO4_1_AS_APT1_PWMB, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO4_1_AS_APT1_PWMB, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO4_1_AS_APT1_PWMB, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN21 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO3_2_AS_APT2_PWMA); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO3_2_AS_APT2_PWMA, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO3_2_AS_APT2_PWMA, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO3_2_AS_APT2_PWMA, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO3_2_AS_APT2_PWMA, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN25 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO4_2_AS_APT2_PWMB); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO4_2_AS_APT2_PWMB, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO4_2_AS_APT2_PWMB, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO4_2_AS_APT2_PWMB, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO4_2_AS_APT2_PWMB, DRIVER_RATE_2); /* Output signal edge fast/slow */ -} - -static void APT_SyncMasterInit(void) -{ - HAL_APT_MasterSyncInit(&g_apt0, APT_SYNC_OUT_ON_CNTR_ZERO); -} - -static void APT_SyncSlaveInit(void) -{ - APT_SlaveSyncIn aptSlave; - - aptSlave.cntPhase = 0; /* counter phase value */ - aptSlave.syncCntMode = APT_COUNT_MODE_AFTER_SYNC_UP; - aptSlave.syncInSrc = APT_SYNCIN_SRC_APT0_SYNCOUT; /* sync source selection */ - aptSlave.cntrSyncSrc = APT_CNTR_SYNC_SRC_SYNCIN; - HAL_APT_SlaveSyncInit(&g_apt1, &aptSlave); - - aptSlave.cntPhase = 0; /* counter phase value */ - aptSlave.syncCntMode = APT_COUNT_MODE_AFTER_SYNC_UP; - aptSlave.syncInSrc = APT_SYNCIN_SRC_APT0_SYNCOUT; /* sync source selection */ - aptSlave.cntrSyncSrc = APT_CNTR_SYNC_SRC_SYNCIN; - HAL_APT_SlaveSyncInit(&g_apt2, &aptSlave); - -} - -void SystemInit(void) -{ - IOConfig(); - DMA_Init(); - UART0_Init(); - ACMP0_Init(); - APT0_Init(); - APT1_Init(); - APT2_Init(); - ADC0_Init(); - PGA0_Init(); - PGA1_Init(); - TIMER0_Init(); - TIMER1_Init(); - GPIO_Init(); - - APT_SyncMasterInit(); - APT_SyncSlaveInit(); - - /* USER CODE BEGIN system_init */ - /* USER CODE END system_init */ -} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/main.c b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/main.c deleted file mode 100644 index d6ab767c..00000000 --- a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/main.c +++ /dev/null @@ -1,71 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file main.c - * @author MCU Driver Team - * @brief Main program body. - * @date 2025-04-14 14:05:10 - */ - -#include "typedefs.h" -#include "feature.h" -#include "mcs_motor_process.h" -#include "main.h" -/* USER CODE BEGIN 0 */ -/* USER CODE 区域内代码不会被覆盖,区域外会被生成的默认代码覆盖(其余USER CODE 区域同理) */ -/* 建议用户放置头文件 */ -/* USER CODE END 0 */ -ACMP_Handle g_acmp0; -PGA_Handle g_pga0; -PGA_Handle g_pga1; -TIMER_Handle g_timer0; -TIMER_Handle g_timer1; -UART_Handle g_uart0; -APT_Handle g_apt0; -APT_Handle g_apt1; -APT_Handle g_apt2; -ADC_Handle g_adc0; -DMA_Handle g_dmac; -GPIO_Handle g_gpio2; -GPIO_Handle g_gpio1; -/* USER CODE BEGIN 1 */ -/* 建议用户定义全局变量、结构体、宏定义或函数声明等 */ -/* USER CODE END 1 */ - -int main(void) -{ - /* USER CODE BEGIN 2 */ - /* 建议用户放置初始化代码或启动代码等 */ - /* USER CODE END 2 */ - MotorMainProcess(); - /* USER CODE BEGIN 3 */ - /* 建议用户放置初始配置代码 */ - /* USER CODE END 3 */ - while (1) { - /* USER CODE BEGIN 4 */ - /* 建议用户放置周期性执行代码 */ - /* USER CODE END 4 */ - } - /* USER CODE BEGIN 5 */ - /* 建议用户放置代码流程 */ - /* USER CODE END 5 */ - return BASE_STATUS_OK; -} - -/* USER CODE BEGIN 6 */ -/* 建议用户放置自定义函数 */ -/* USER CODE END 6 */ \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/inc/mcs_carrier.h b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/inc/mcs_carrier.h deleted file mode 100644 index dea3ad66..00000000 --- a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/inc/mcs_carrier.h +++ /dev/null @@ -1,142 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_carrier.h - * @author MCU Algorithm Team - * @brief This file provides functions declaration for carrier interrupt processing function. - */ -#ifndef McuMagicTag_MCS_CARRIER_H -#define McuMagicTag_MCS_CARRIER_H - -#include "mcs_status.h" -#include "mcs_mtr_param.h" -#include "mcs_svpwm.h" -#include "mcs_curr_ctrl.h" -#include "mcs_if_ctrl.h" -#include "mcs_ramp_mgmt.h" -#include "mcs_spd_ctrl.h" -#include "mcs_fosmo.h" -#include "mcs_smo_4th.h" -#include "mcs_pll.h" -#include "mcs_startup.h" -#include "mcs_r1_svpwm.h" -#include "mcs_fw_ctrl.h" -#include "mcs_prot_user.h" - -typedef void (*MCS_ReadCurrUvwCb)(UvwAxis *CurrUvw); -typedef void (*MCS_SetPwmDutyCb)(UvwAxis *dutyUvwLeft, UvwAxis *dutyUvwRight); -typedef void (*MCS_SetADCTriggerTimeCb)(unsigned short cntCmpSOCA, unsigned short cntCmpSOCB); - -/** - * @brief motor control FSM state define. - * @details motor control FSM state type: - * + FSM_IDLE -- IDLE state, system startup default. - * + FSM_OFFSET_CALIB -- Self calibrate, for ADC init. - * + FSM_CAP_CHARGE -- IPM cap charge. - * + FSM_CLEAR -- Clear before every run. - * + FSM_LOCATE -- Rotor position pre-locate. - * + FSM_STARTUP -- Start up. - * + FSM_SWITCH -- Transition state, control from open to closed loop. - * + FSM_RUN -- Normal running state. - * + FSM_WAIT_STOP -- Wait stopping. - * + FSM_STOP -- Normal stop. - * + FSM_FAULT -- Fault state, waiting for user process. - */ -typedef enum { - FSM_IDLE = 0, - FSM_OFFSET_CALIB, - FSM_CAP_CHARGE, - FSM_CLEAR, - FSM_LOCATE, - FSM_STARTUP, - FSM_SWITCH, - FSM_RUN, - FSM_WAIT_STOP, - FSM_STOP, - FSM_FAULT -} FsmState; - -/** - * @brief Sampling mode. - */ -typedef enum { - DUAL_RESISTORS = 0, - SINGLE_RESISTOR = 1 -} SampleMode; - -/** - * @brief Motor control data structure - */ -typedef struct { - unsigned char motorStateFlag; /**< Motor start/stop status flag. */ - float spdCmdHz; /**< External input speed command value. */ - float axisAngle; /**< Angle of the synchronous coordinate system. */ - float spdRefHz; /**< Command value after speed ramp management. */ - float currCtrlPeriod; /**< current loop control period. */ - float adc0Compensate; /**< ADC0 softwaretrim compensate value. */ - float adc1Compensate; /**< ADC1 softwaretrim compensate value. */ - float udc; /**< Bus voltage. */ - float powerBoardTemp; /**< Power boart surface temperature. */ - unsigned short aptMaxcntCmp; /**< Apt Maximum Comparison Count. */ - float adcCurrCofe; /**< Adc current sampling cofeature. */ - - unsigned short sysTickCnt; /**< System Timer Tick Count. */ - unsigned short capChargeTickNum; /**< Bootstrap Capacitor Charge Tick Count. */ - volatile unsigned int msTickCnt; /**< Millisecond-level counter, which can be used in 1-ms and 5-ms tasks. */ - unsigned short msTickNum; /**< Number of ticks corresponding to 1 ms. */ - char obserType; /**< Set Observer Type. */ - char controlMode; /**< Set foc control or sixstep bldc control mode or others. */ - char spdAdjustMode; /**< Set speed adjust mode. */ - char uartConnectFlag; /**< Uart connect success flag. */ - short uartHeartDetCnt; /**< Uart connect heart detect count. */ - float uartTimeStamp; /**< Uart data time stamp. */ - SysStatusReg statusReg; /**< System status. */ - FsmState stateMachine; /**< Motor Control State Machine. */ - - SampleMode sampleMode; /**< Current sampling mode setting: single resistor, double resistor. */ - MOTOR_Param mtrParam; /**< Motor parameters. */ - FOSMO_Handle smo; /**< SMO observer handle. */ - SMO4TH_Handle smo4th; /**< SMO 4th observer handle. */ - IF_Handle ifCtrl; /**< I/F control handle. */ - SVPWM_Handle sv; /**< Dual-resistance/three-resistance sampling SVPWM Handle. */ - R1SVPWM_Handle r1Sv; /**< Single-resistance phase-shifted SVPWM handle. */ - RMG_Handle spdRmg; /**< Ramp management struct for the speed controller input reference. */ - SPDCTRL_Handle spdCtrl; /**< Speed loop Control Handle. */ - CURRCTRL_Handle currCtrl; /**< Current loop control handle. */ - STARTUP_Handle startup; /**< Startup Switch Handle. */ - FW_Handle fw; /**< Flux-Weakening Handle. */ - - DqAxis idqRef; /**< Command value of the dq axis current. */ - UvwAxis currUvw; /**< Three-phase current sampling value. */ - AlbeAxis iabFbk; /**< αβ-axis current feedback value. */ - DqAxis idqFbk; /**< Current feedback value of the dq axis. */ - DqAxis vdqRef; /**< Current loop output dq voltage. */ - AlbeAxis vabRef; /**< Current loop output voltage αβ. */ - UvwAxis dutyUvw; /**< UVW three-phase duty cycle. */ - UvwAxis dutyUvwLeft; /**< Single Resistor UVW Three-Phase Left Duty Cycle. */ - UvwAxis dutyUvwRight; /**< Single Resistor UVW Three-Phase Right Duty Cycle. */ - - MCS_ReadCurrUvwCb readCurrUvwCb; /**< Read current callback function. */ - MCS_SetPwmDutyCb setPwmDutyCb; /**< Set the duty cycle callback function. */ - MCS_SetADCTriggerTimeCb setADCTriggerTimeCb; /**< Sets the ADC trigger point callback function. */ - - MotorProtStatus_Handle prot; /**< Motor protect status detection flag bit handle. */ -} MTRCTRL_Handle; - -void MCS_CarrierProcess(MTRCTRL_Handle *mtrCtrl); - -#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/inc/mcs_chip_config.h b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/inc/mcs_chip_config.h deleted file mode 100644 index 38c00b5d..00000000 --- a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/inc/mcs_chip_config.h +++ /dev/null @@ -1,76 +0,0 @@ -/** - * @ Copyright (c) HiSilicon (Shanghai) Technologies Co., Ltd. 2022-2023. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_chip_config.h - * @author MCU Algorithm Team - * @brief This file provides config macros for ECMCU105H/ECBMCU201MPC app. - */ - -#ifndef McuMagicTag_MCS_CHIPCONFIG_H -#define McuMagicTag_MCS_CHIPCONFIG_H - -#include "feature.h" - -#if defined (CHIP_3061MNPICA) || defined (CHIP_3061MNPIKA) || defined (CHIP_3061MNNICA) || \ - defined (CHIP_3061MNNIKA) || defined (CHIP_3061MNPIC8) || defined(CHIP_3061MNNIC8) || \ - defined (CHIP_3061MNPIK8) || defined (CHIP_3061MNNIK8) - - #define ADCPTT_HANDLE g_adc0 - #define ADCRESIS_HANDLE g_adc0 - #define ADCUDC_HANDLE g_adc0 - #define ADCU_HANDLE g_adc0 - #define ADCW_HANDLE g_adc0 - #define LED1_HANDLE g_gpio2 - #define LED2_HANDLE g_gpio1 - #define LED2_PIN GPIO_PIN_0 - #define LED1_PIN GPIO_PIN_3 - #define ADCPTTSOCNUM ADC_SOC_NUM0 - #define ADCUSOCNUM ADC_SOC_NUM1 - #define ADCRESISSOCNUM ADC_SOC_NUM3 - #define ADCUDCSOCNUM ADC_SOC_NUM4 - #define ADCWSOCNUM ADC_SOC_NUM6 - - #define ADC0COMPENSATE 2037.0f - #define ADC1COMPENSATE 2027.0f - -#endif - -#if defined (CHIP_3065HRPIRZ) || defined (CHIP_3065ARPIRZ) || defined (CHIP_3066MNPIRH) || \ - defined (CHIP_3065PNPIRH) || defined (CHIP_3065PNPIRE) || defined (CHIP_3065PNPIRA) - - #define ADCU_HANDLE g_adc0 - #define ADCW_HANDLE g_adc1 - #define ADCRESIS_HANDLE g_adc2 - #define ADCUDC_HANDLE g_adc2 - #define ADCPTT_HANDLE g_adc2 - #define LED1_HANDLE g_gpio0 - #define LED2_HANDLE g_gpio0 - #define LED2_PIN GPIO_PIN_6 - #define LED1_PIN GPIO_PIN_7 - #define ADCPTTSOCNUM ADC_SOC_NUM0 - #define ADCRESISSOCNUM ADC_SOC_NUM1 - #define ADCUSOCNUM ADC_SOC_NUM8 - #define ADCWSOCNUM ADC_SOC_NUM8 - #define ADCUDCSOCNUM ADC_SOC_NUM14 - - #define ADC0COMPENSATE 2033.0f - #define ADC1COMPENSATE 2070.0f - -#endif - - -#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/inc/mcs_ctlmode_config.h b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/inc/mcs_ctlmode_config.h deleted file mode 100644 index 2a6f38cc..00000000 --- a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/inc/mcs_ctlmode_config.h +++ /dev/null @@ -1,73 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_ctlmode_config.h - * @author MCU Algorithm Team - * @brief This file provides config macros for ECMCU105H app. - */ - - /* Define to prevent recursive inclusion ------------------------------------------------------- */ -#ifndef McuMagicTag_MCS_CTLMODECONFIG_H -#define McuMagicTag_MCS_CTLMODECONFIG_H - -#include "debug.h" -#include "typedefs.h" - -typedef enum { - FOC_STARTUP_IF = 0, - FOC_STARTUP_VF, - FOC_STARTUP_HFI -} MOTOR_STARTUPMODE_CONFIG; - -typedef enum { - FOC_OBSERVERTYPE_SMO1TH = 0, - FOC_OBSERVERTYPE_SMO1TH_PLL, - FOC_OBSERVERTYPE_SMO4TH, - FOC_OBSERVERTYPE_SMO4TH_PLL, - FOC_OBSERVERTYPE_LUNBORG, - FOC_OBSERVERTYPE_FLUX -} MOTOR_OBSERVERTYPE_CONFIG; - -typedef enum { - FOC_CONTROLMODE_SPEED = 0, - FOC_CONTROLMODE_TORQUE -} MOTOR_CONTROLMODE_CONFIG; - -typedef enum { - FOC_CURQAXISPID_PARAMS = 0, - FOC_CURDAXISPID_PARAMS, - FOC_SPDPID_PARAMS -} MOTOR_PID_SET; - -typedef enum { - MOTOR_PARAMS_BASE = 0, - MOTOR_PARAMS_SPECIAL, - MOTOR_PARAMS_BOARD -} MOTOR_PARAMS_SET; - -typedef enum { - CUST_SPEED_ADJUST = 0, - HOST_SPEED_ADJUST -} MODE_ADSPEED_CONFIG; - -typedef enum { - CONNECTING = 0, - CONNECTED, - DISCONNECT -} UART_STATUS; - -#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/inc/mcs_motor_process.h b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/inc/mcs_motor_process.h deleted file mode 100644 index 783bf214..00000000 --- a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/inc/mcs_motor_process.h +++ /dev/null @@ -1,45 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_motor_process.h - * @author MCU Algorithm Team - * @brief This file provides motor sample functions declaration for ECMCU105H board. - */ -#ifndef McuMagicTag_MCS_MOTOR_PROCESS_H -#define McuMagicTag_MCS_MOTOR_PROCESS_H - -/** - * @brief phase sequence. - */ -typedef enum { - PHASE_U = 0, - PHASE_V, - PHASE_W, - PHASE_MAX_NUM -} MCS_PhaseEnum; - -/** - * @brief key state. - */ -typedef enum { - KEY_DOWN = 0, - KEY_UP = 1, -} KEY_State; - -int MotorMainProcess(void); - -#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/inc/mcs_status.h b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/inc/mcs_status.h deleted file mode 100644 index 19510c78..00000000 --- a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/inc/mcs_status.h +++ /dev/null @@ -1,188 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_status.h - * @author MCU Algorithm Team - * @brief This file provides functions declaration of system status. - */ - -/* Define to prevent recursive inclusion ------------------------------------------------------- */ -#ifndef McuMagicTag_MCS_STATUS_H -#define McuMagicTag_MCS_STATUS_H - -/* Includes ------------------------------------------------------------------------------------ */ -#include "typedefs.h" -#include "mcs_assert.h" - -/* Typedef definitions ------------------------------------------------------------------------- */ -/** - * @brief System status define - */ -typedef union { - unsigned short all; - struct { - unsigned short cmdStart : 1; /**< Indicates that a start system command has been received. */ - unsigned short cmdStop : 1; /**< Indicates that a stop system command has been received. */ - unsigned short isRunning : 1; /**< Indicates that the system is running (enable signal) */ - unsigned short sysError : 1; /**< Indicates that the system reports an error. */ - unsigned short poweron : 1; /**< Indicates that the power-on initialization phase is complete. */ - unsigned short capcharge : 1; /**< Indicates that the bootstrap capacitor charging phase is complete. */ - unsigned short adczero : 1; /**< The current sampling point is reset to zero after power-on. */ - } Bit; -} SysStatusReg; - -/** - * @brief Get status of Bit cmdStart. - * @param sysStatus System status register handle. - * @retval Status of Bit cmdStart. - */ -static inline bool SysGetCmdStart(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - bool ret; - ret = (sysStatus->Bit.cmdStart == 1) ? true : false; - return ret; -} - -/** - * @brief Set Bit cmdStart. - * @param sysStatus System status register handle. - * @retval None. - */ -static inline void SysCmdStartSet(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - sysStatus->Bit.cmdStart = 1; -} - -/** - * @brief Clear Bit cmdStart. - * @param handle System status register handle. - * @retval None. - */ -static inline void SysCmdStartClr(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - sysStatus->Bit.cmdStart = 0; -} - -/** - * @brief Get status of Bit cmdStop. - * @param sysStatus System status register handle. - * @retval Status of Bit cmdStart. - */ -static inline bool SysGetCmdStop(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - bool ret; - ret = (sysStatus->Bit.cmdStop == 1) ? true : false; - return ret; -} - -/** - * @brief Set Bit cmdStop. - * @param sysStatus System status register handle. - * @retval None. - */ -static inline void SysCmdStopSet(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - sysStatus->Bit.cmdStop = 1; -} - -/** - * @brief Clear Bit cmdStop. - * @param sysStatus System status register handle. - * @retval None. - */ -static inline void SysCmdStopClr(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - sysStatus->Bit.cmdStop = 0; -} - -/** - * @brief Get status of Bit isRunning. - * @param sysStatus System status register handle. - * @retval Status of Bit isRunning. - */ -static inline bool SysIsRunning(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - bool ret; - ret = (sysStatus->Bit.isRunning == 1) ? true : false; - return ret; -} - -/** - * @brief Set Bit isRuning. - * @param sysStatus System status register handle. - * @retval None. - */ -static inline void SysRunningSet(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - sysStatus->Bit.isRunning = 1; -} - -/** - * @brief Clear Bit isRuning. - * @param sysStatus System status register handle. - * @retval None. - */ -static inline void SysRunningClr(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - sysStatus->Bit.isRunning = 0; -} - -/** - * @brief Get status of Bit sysError. - * @param sysStatus System status register handle. - * @retval Status of Bit sysError. - */ -static inline bool SysIsError(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - bool ret; - ret = (sysStatus->Bit.sysError == 1) ? true : false; - return ret; -} - -/** - * @brief Set Bit sysError. - * @param sysStatus System status register handle. - * @retval None. - */ -static inline void SysErrorSet(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - sysStatus->Bit.sysError = 1; -} - -/** - * @brief Clear Bit sysError. - * @param sysStatus System status register handle. - * @retval None. - */ -static inline void SysErrorClr(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - sysStatus->Bit.sysError = 0; -} - -#endif diff --git a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/inc/mcs_user_config.h b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/inc/mcs_user_config.h deleted file mode 100644 index 8d4cb216..00000000 --- a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/inc/mcs_user_config.h +++ /dev/null @@ -1,108 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_user_config.h - * @author MCU Algorithm Team - * @brief This file provides functions declaration of user config parameters. - */ -#ifndef McuMagicTag_MCS_CONFIG_H -#define McuMagicTag_MCS_CONFIG_H - -#include "debug.h" -#include "typedefs.h" - -#define SMO4TH - -#define SYSTICK_PERIOD_US 500u /* systick period. */ - -#define INV_CAP_CHARGE_MS 3u /* Capacitor charging time. */ - -#define INV_VOLTAGE_BUS 24.0f /* Bus voltage, V. */ - -#define CTRL_CURR_PERIOD 0.0001f /* carrier ISR period, 100us. */ -#define CTRL_SYSTICK_PERIOD 0.0005f /* systick control period, 500us. */ - -/* Duty of sample window, the real time is 0.06*50us = 3us. */ -#define SAMPLE_WINDOW_DUTY 0.06f - -/* Duty of sample point shift as flip point, the real time is 0.008*50us = 0.4us. */ -#define SAMPLE_POINT_SHIFT 0.008f - -/* Sampling resistance 200mOhm 0.0013295 */ -#define ADC_CURR_COFFI 0.0013295f /* 3.3/4096/3.03/0.2 pga: 3.03, 200mohm. */ - -#define APT_U APT0_BASE /* Base address of U phase APT module. */ -#define APT_V APT1_BASE /* Base address of V phase APT module. */ -#define APT_W APT2_BASE /* Base address of W phase APT module. */ - -/* FOSMO */ -#define FOSMO_GAIN 4.0f /* SMO gain */ -#define FOSMO_LAMBDA 2.0f /* SMO coefficient of cut-off frequency, its value = lambda * we. */ -#define FOSMO_EMF_CUTOFF_FREQ 2.0f /* SMO back emf cutoff frequency. */ -#define SPEED_FILTER_CUTOFF_FREQUENCY 40.0f /* SMO speed cutoff frequency of speed filter. */ -#define FOSMO_PLL_BDW 30.0f /* SMO PLL bandwidth. */ - -/* SMO4TH */ -#define SMO4TH_PLL_BDW 30.0f /* SMO4TH PLL Bandwidth. */ -#define SMO4TH_KD 300.0f /* SMO4TH parameters KD. */ -#define SMO4TH_KQ 600.0f /* SMO4TH parameters KQ. */ -#define SMO4TH_SPD_FILTER_CUTOFF_FREQ 40.0f /* SMO4TH speed cutoff frequency of speed filter. */ - -/* User_Commond */ -#define CTRL_IF_CURR_AMP_A 0.7f /* IF control current amplitude. */ -#define USER_TARGET_SPD_HZ 100.0f /* User-defined target speed value. */ -#define USER_SWITCH_SPDBEGIN_HZ 30.0f /* Start of handover interval. */ -#define USER_SWITCH_SPDEND_HZ (USER_SWITCH_SPDBEGIN_HZ + 3.0f) /* End of handover period. */ -#define USER_MAX_SPD_HZ 29.0f /* User-defined maximum speed value. */ -#define USER_MIN_SPD_HZ 5.0f /* User-defined minimum speed value. */ -#define USER_SPD_SLOPE 50.0f /* slope of velocity change. */ -#define USER_CURR_SLOPE (CTRL_IF_CURR_AMP_A * 10.0f) /* Current change slope. */ - -/* PID PARAMS */ -#define CURRQAXIS_KP 5.023202f /* Current loop Q axis Kp. */ -#define CURRQAXIS_KI 20612.84f /* Current loop Q axis Ki. */ -#define CURRDAXIS_KP 3.477114f /* Current loop D axis Kp. */ -#define CURRDAXIS_KI 20612.84f /* Current loop D axis Ki. */ -/* Current loop PID output lower limit. */ -#define CURR_LOWERLIM (-INV_VOLTAGE_BUS * ONE_DIV_SQRT3 * 0.98f) -/* Current loop PID output upper limit. */ -#define CURR_UPPERLIM (INV_VOLTAGE_BUS * ONE_DIV_SQRT3 * 0.98f) - -#define SPD_KP 0.00505f /* Speed loop Kp. */ -#define SPD_KI 0.012f /* Speed loop Ki. */ -#define SPD_LOWERLIM (-0.105f) /* Speed loop PID output lower limit. */ -#define SPD_UPPERLIM 0.105f /* Speed loop PID output upper limit. */ - -/* MOTOR PARAMS */ -/* Np, Rs, Ld, Lq, Psif, J, Nmax, Currmax, PPMR, zShift. */ -/* mtrPsif & mtrJ parameter is not used in this project, temporarily set to 0. */ -#define MOTORPARAM_DEFAULTS { \ - .mtrNp = 14, \ - .mtrRs = 10.1f, \ - .mtrLd = 0.010f, \ - .mtrLq = 0.010f, \ - .mtrPsif = 0.0f, \ - .mtrJ = 0.0f, \ - .maxElecSpd = 29.0f, \ - .maxCurr = 1.0f, \ - .busVolt = INV_VOLTAGE_BUS, \ -} - -/* Bus voltage sampling conversion factor. */ -#define ADC_UDC_COFFI 0.01289f /* 0.01289 = 3.3/4096*192/12. */ - -#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_cmm.c b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_cmm.c deleted file mode 100644 index 99766bdf..00000000 --- a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_cmm.c +++ /dev/null @@ -1,43 +0,0 @@ -/** - * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_prot_cmm.c - * @author MCU Algorithm Team - * @brief This file contains protection common api definition. - */ - -/* Includes ------------------------------------------------------------------------------------ */ -#include "mcs_prot_cmm.h" -#include "apt.h" -#include "mcs_assert.h" - -/** - * @brief Safty-pulse-off function execution to turn off all the power devices. - * @param aptAddr Three-phase APT address pointer. - * @retval None. - */ -void ProtSpo_Exec(APT_RegStruct **aptAddr) -{ - MCS_ASSERT_PARAM(aptAddr != NULL); - /**< Turn off all the six power devices of the inverter. */ - for (unsigned int i = 0; i < MOTOR_PHASE_NUMBER; i++) { - APT_RegStruct *aptx = (APT_RegStruct *)(aptAddr[i]); - DCL_APT_EnableSwContPWMAction(aptx, APT_PWM_CHANNEL_A); - DCL_APT_EnableSwContPWMAction(aptx, APT_PWM_CHANNEL_B); - DCL_APT_ForcePWMOutputLow(aptx); - } -} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_cmm.h b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_cmm.h deleted file mode 100644 index 104a5dec..00000000 --- a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_cmm.h +++ /dev/null @@ -1,136 +0,0 @@ -/** - * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_prot_cmm.h - * @author MCU Algorithm Team - * @brief This file contains protection function common data struct and api declaration. - */ - -/* Define to prevent recursive inclusion ------------------------------------------------------- */ -#ifndef McuMagicTag_MCS_PROT_CMM_H -#define McuMagicTag_MCS_PROT_CMM_H - -#include "typedefs.h" -#include "apt_ip.h" - -/* Macro definitions --------------------------------------------------------------------------- */ -#define MOTOR_PHASE_NUMBER (3) - -#define PROT_VAL_THRESHOLD_NUMS (4) -#define PROT_VAL_THRESHOLD_0 (0) -#define PROT_VAL_THRESHOLD_1 (1) -#define PROT_VAL_THRESHOLD_2 (2) -#define PROT_VAL_THRESHOLD_3 (3) - -#define PROT_LIMIT_TIME_NUMS (3) -#define PROT_LIMIT_TIME_0 (0) -#define PROT_LIMIT_TIME_1 (1) -#define PROT_LIMIT_TIME_2 (2) - -#define MOTOR_PHASE_NUMBER (3) - -/**< Motor error status definition. */ -typedef union { - int all; - struct { - unsigned short overCurrErr : 1; /**< Indicates that phase current(s) is over protected value. */ - unsigned short overVoltErr : 1; /**< Indicates that dc-link voltage is over protected value. */ - unsigned short lowerVoltErr : 1; /**< Indicates that dc-link voltage is lower than protected value */ - unsigned short overIpmTempErr : 1; /**< Indicates that IPM temperature is over protected value. */ - unsigned short revRotErr : 1; /**< Indicates that motor negtive direction. */ - unsigned short motorStalling : 1; /**< Indicates that rotor is stalling. */ - unsigned short overMotorTempErr : 1; /**< Indicates that three phase currents is out-of-balance. */ - unsigned short posSnsrCommsErr : 1; /**< Indicates that position sensor communication is lost with MCU. */ - unsigned short posSnsrFuncErr : 1; /**< Indicates that position sensor reports function error. */ - unsigned short posSnsrCalibrErr : 1; /**< Indicates that position sensor fails to calibrate itself. */ - unsigned short currOutOfBalance : 1; /**< Indicates that the rotor is reverse rotation.*/ - unsigned short phsOpenErr : 1; /**< Indicates that phase winding(s) is open. */ - unsigned short phsU : 1; /**< Indicates that u phase fails when phsOpenErr occurs. */ - unsigned short phsV : 1; /**< Indicates that v phase fails when phsOpenErr occurs. */ - unsigned short phsW : 1; /**< Indicates that w phase fails when phsOpenErr occurs. */ - unsigned short multiPhs : 1; /**< Indicates that multi-phases fail when phsOpenErr occurs.*/ - } Bit; -} MotorErrStatusReg; - -/**< Protection Status Bit Definition */ -typedef enum { - OCP_ERR_BIT, - OVP_ERR_BIT, - LVP_ERR_BIT, - OTP_IPM_ERR_BIT, - OTP_MOTOR_ERR_BIT, - STALLING_ERR_BIT, - CURR_OUT_BALANCE_ERR_BIT, - POS_COMMS_ERR_BIT, - POS_FUNC_ERR_BIT, - POS_CALIB_ERR_BIT, - REV_ROT_ERR_BIT, - PHS_OPEN_ERR_BIT, - PHS_U_ERR_BIT, - PHS_V_ERR_BIT, - PHS_W_ERR_BIT, - PHS_MULTI_ERR_BIT, -} PROT_ErrBit; - -/**< Motor error protection level. */ -typedef enum { - PROT_LEVEL_0 = 0, - PROT_LEVEL_1, - PROT_LEVEL_2, - PROT_LEVEL_3, - PROT_LEVEL_4 /**< The greater level number, the severe error is. */ -} PROT_Level; - -/** - * @brief Obtains the status of a bit of data. - * @param data data. - * @param bits Number of digits. - * @retval Bit status. - */ -static inline bool GetBit(int data, unsigned short bit) -{ - bool ret; - ret = ((data >> bit) & 1); - return ret; -} - -/** - * @brief Sets the status of a bit of data. - * @param data data. - * @param bit The setted bit. - * @retval None. - */ -static inline void SetBit(int *data, unsigned char bit) -{ - *data |= (1 << bit); -} - -/** - * @brief Clear the status of a bit of data. - * @param data data. - * @param bit The Clear bit. - * @retval None. - */ -static inline void ClearBit(int *data, unsigned char bit) -{ - *data &= ~(1 << bit); -} - -/**< Protection action. */ -void ProtSpo_Exec(APT_RegStruct **aptAddr); - -#endif diff --git a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user.c b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user.c deleted file mode 100644 index e97876b7..00000000 --- a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user.c +++ /dev/null @@ -1,137 +0,0 @@ -/** - * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_prot_user.c - * @author MCU Algorithm Team - * @brief This file contains user protection data struct, inquiry api and initialization api definition. - */ - -/* Includes ------------------------------------------------------------------------------------ */ -#include "mcs_prot_user.h" -#include "mcs_prot_user_config.h" -#include "mcs_assert.h" - -/** - * @brief Get motor over current error status. - * @param motorErrStatus Motor error status. - * @retval Error status. - */ -bool IsMotorOverCurrErr(MotorErrStatusReg motorErrStatus) -{ - if (motorErrStatus.Bit.overCurrErr) { - return true; - } else { - return false; - } -} - -/** - * @brief Get motor over dc-link voltage error status. - * @param motorErrStatus Motor error status. - * @retval Error status. - */ -bool IsDcLinkOverVoltErr(MotorErrStatusReg motorErrStatus) -{ - if (motorErrStatus.Bit.overVoltErr) { - return true; - } else { - return false; - } -} - -/** - * @brief Get motor lower dc-link voltage error status. - * @param motorErrStatus Motor error status. - * @retval Error status. - */ -bool IsDcLinkLowerVoltErr(MotorErrStatusReg motorErrStatus) -{ - if (motorErrStatus.Bit.lowerVoltErr) { - return true; - } else { - return false; - } -} - -/** - * @brief Get motor over Ipm temperature error status. - * @param motorErrStatus Motor error status. - * @retval Error status. - */ -bool IsIpmOverTempErr(MotorErrStatusReg motorErrStatus) -{ - if (motorErrStatus.Bit.overIpmTempErr) { - return true; - } else { - return false; - } -} - -/** - * @brief Get motor over Motor temperature error status. - * @param motorErrStatus Motor error status. - * @retval Error status. - */ -bool IsMotorOverTempErr(MotorErrStatusReg motorErrStatus) -{ - if (motorErrStatus.Bit.overMotorTempErr) { - return true; - } else { - return false; - } -} - -/** - * @brief Get motor stalling error status. - * @param motorErrStatus Motor error status. - * @retval Error status. - */ -bool IsMotorStallingErr(MotorErrStatusReg motorErrStatus) -{ - if (motorErrStatus.Bit.motorStalling) { - return true; - } else { - return false; - } -} - -/** - * @brief Clear the motor error status. - * @param motorProt Motor protection handle. - * @retval Error status. - */ -void ClearMotorErrStatus(MotorProtStatus_Handle *motorProt) -{ - MCS_ASSERT_PARAM(motorProt != NULL); - /* Clear the motor error status. */ - motorProt->motorErrStatus.all = 0x00; - OCP_Clear(&motorProt->ocp); - OVP_Clear(&motorProt->ovp); - LVP_Clear(&motorProt->lvp); - OTP_Clear(&motorProt->otp); -} - -/** - * @brief Motor protection function initialization. - * @param motorProt Motor protection handle. - * @retval Error status. - */ -void MotorProt_Init(MotorProtStatus_Handle *motorProt) -{ - MCS_ASSERT_PARAM(motorProt != NULL); - motorProt->motorErrStatus.all = 0x00; -} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user.h b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user.h deleted file mode 100644 index 365110e1..00000000 --- a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user.h +++ /dev/null @@ -1,54 +0,0 @@ -/** - * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_prot_user.h - * @author MCU Algorithm Team - * @brief This file contains user protection data struct, inquiry api and initialization api declaration. - */ - -/* Define to prevent recursive inclusion ------------------------------------------------------- */ -#ifndef McuMagicTag_MCS_PROT_USER_H -#define McuMagicTag_MCS_PROT_USER_H - -/* Includes ------------------------------------------------------------------------------------ */ -#include "mcs_prot_cmm.h" -#include "mcs_curr_prot.h" -#include "mcs_dc_volt_prot.h" -#include "mcs_temp_prot.h" -#include "mcs_motor_stalling.h" -#include "typedefs.h" - -typedef struct { - MotorErrStatusReg motorErrStatus; /**< Motor error status. */ - OCP_Handle ocp; /**< Over current protection. */ - OVP_Handle ovp; /**< Over dc-link voltage protection. */ - LVP_Handle lvp; /**< Lower dc-link voltage protection. */ - OTP_Handle otp; /**< Over IPM temperature protection. */ - STP_Handle stall; /**< Motor stalling protection. */ -} MotorProtStatus_Handle; - -void MotorProt_Init(MotorProtStatus_Handle *motorProt); - -/**< Inquiry motor error status */ -bool IsMotorOverCurrErr(MotorErrStatusReg motorErrStatus); -bool IsDcLinkOverVoltErr(MotorErrStatusReg motorErrStatus); -bool IsDcLinkLowerVoltErr(MotorErrStatusReg motorErrStatus); -bool IsIpmOverTempErr(MotorErrStatusReg motorErrStatus); -bool IsMotorOverTempErr(MotorErrStatusReg motorErrStatus); -bool IsMotorStallingErr(MotorErrStatusReg motorErrStatus); -void ClearMotorErrStatus(MotorProtStatus_Handle *motorProt); -#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user_config.h b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user_config.h deleted file mode 100644 index 832cd1be..00000000 --- a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user_config.h +++ /dev/null @@ -1,125 +0,0 @@ -/** - * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_prot_user_config.h - * @author MCU Algorithm Team - * @brief This file contans user macro definition of the protection function. - */ - -/* Define to prevent recursive inclusion ------------------------------------------------------- */ -#ifndef McuMagicTag_MCS_PROT_USER_CONFIG_H -#define McuMagicTag_MCS_PROT_USER_CONFIG_H - -/* Macro definitions --------------------------------------------------------------------------- */ -/**< COMMON */ -/**< Peak phase current (A) of the motor or IPM under continuous operations. */ -#define PROT_MOTOR_RATED_CURR (1.0f) -/**< Only several continuous fault detection can trigger error status. */ -#define PROT_CNT_LIMIT (100) -/**< Only several contunuous none fault dectection can trigger elimination of error status. */ -#define RECY_CNT_LIMIT (10000) -/**< Only several contunuous none fault dectection can trigger elimination of error status. */ -#define OVER_VOLT_RECY_CNT_LIMIT (100) -/**< Only several contunuous none fault dectection can trigger elimination of error status. */ -#define LOWER_VOLT_RECY_CNT_LIMIT (100) - -/**< Over current protection */ -/**< Over current trigger value (A) when in level 1. */ -#define PROT_OVER_CURR_POW_DN1 (2.0f * PROT_MOTOR_RATED_CURR) -/**< Over current trigger value (A) when in level 2. */ -#define PROT_OVER_CURR_POW_DN2 (2.0f * PROT_MOTOR_RATED_CURR) -/**< Over current trigger value (A) when in level 3. */ -#define PROT_OVER_CURR_POW_DN3 (2.0f * PROT_MOTOR_RATED_CURR) -/**< Over current trigger value (A) when in level 4. */ -#define PROT_OVER_CURR_POW_OFF (2.0f * PROT_MOTOR_RATED_CURR) -#define PROT_OVER_CURR_RECY_DELTA (0.0035f) /**< Current gap (A) when recovers from protection status. */ -#define PROT_OVER_CURR_LIMIT1_TIME_SEC (30.0f) /**< 20% overload can last maximum time: 30 sec. */ -#define PROT_OVER_CURR_LIMIT2_TIME_SEC (10.0f) /**< 20% overload can last maximum time: 10 sec. */ -#define PROT_OVER_CURR_LIMIT3_TIME_SEC (2.0f) /**< 20% overload can last maximum time: 2 sec. */ - -/**< Over voltage protection */ -#define PROT_OVER_VOLT_BRK_ON1 (26.0f) /**<处于1级时,直流链路电压触发值(V)过高*/ -#define PROT_OVER_VOLT_BRK_ON2 (27.0f) /**< Over dc-link voltage trigger value (V) when in level 2. */ -#define PROT_OVER_VOLT_BRK_ON3 (28.0f) /**< Over dc-link voltage trigger value (V) when in level 3. */ -#define PROT_OVER_VOLT_BRK_ALL (30.0f) /**< Over dc-link voltage trigger value (V) when in level 4. */ -#define PROT_OVER_VOLT_RECY_DELTA (0.5f) /**<从保护状态恢复时的电压间隙(V)*/ -#define PROT_OVER_VOLT_LIMIT1_TIME_SEC (5.0f) /** 1; One-cycle mode: < 0.5 */ -#define POS_SNSR_IPD_INJ_PERIOD (4) - -#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/mcs_curr_prot.c b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/mcs_curr_prot.c deleted file mode 100644 index da4e56f8..00000000 --- a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/mcs_curr_prot.c +++ /dev/null @@ -1,267 +0,0 @@ -/** - * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_curr_prot.c - * @author MCU Algorithm Team - * @brief This file contains current protecion api definition. - */ - -/* Includes ------------------------------------------------------------------------------------ */ -#include "mcs_curr_prot.h" -#include "mcs_math.h" -#include "mcs_prot_user_config.h" -#include "mcs_assert.h" - -/** - * @brief Initilization over current protection function. - * @param ocp Over current protection handle. - * @param ts Ctrl period (s). - * @retval None. - */ -void OCP_Init(OCP_Handle *ocp, float ts) -{ - MCS_ASSERT_PARAM(ocp != NULL); - MCS_ASSERT_PARAM(ts > 0.0f); - ocp->ts = ts; - OCP_Clear(ocp); - - ocp->protCntLimit = PROT_CNT_LIMIT; - ocp->recyCntLimit = RECY_CNT_LIMIT; - /* Configuring four levels of current protection thresholds. */ - ocp->protValThr[PROT_VAL_THRESHOLD_0] = PROT_OVER_CURR_POW_DN1; - ocp->protValThr[PROT_VAL_THRESHOLD_1] = PROT_OVER_CURR_POW_DN2; - ocp->protValThr[PROT_VAL_THRESHOLD_2] = PROT_OVER_CURR_POW_DN3; - ocp->protValThr[PROT_VAL_THRESHOLD_3] = PROT_OVER_CURR_POW_OFF; - /* Configure the protection limit time. */ - ocp->protLimitTime[PROT_LIMIT_TIME_0] = PROT_OVER_CURR_LIMIT1_TIME_SEC; - ocp->protLimitTime[PROT_LIMIT_TIME_1] = PROT_OVER_CURR_LIMIT2_TIME_SEC; - ocp->protLimitTime[PROT_LIMIT_TIME_2] = PROT_OVER_CURR_LIMIT3_TIME_SEC; - ocp->recyDelta = PROT_OVER_CURR_RECY_DELTA; -} - -/** - * @brief Over current protection detection. - * @param ocp Over current protection handle. - * @param motorErrStatus Motor error status. - * @param idq DQ-axis feedback currents. - * @retval None. - */ -void OCP_Det(OCP_Handle *ocp, MotorErrStatusReg *motorErrStatus, DqAxis idq) -{ - MCS_ASSERT_PARAM(ocp != NULL); - MCS_ASSERT_PARAM(motorErrStatus != NULL); - /* Calculate current amplitude. */ - ocp->currAmp = Sqrt(idq.d * idq.d + idq.q * idq.q); - - /* Check if value goes over threshold for continuous cycles. */ - if (ocp->currAmp < ocp->protValThr[PROT_VAL_THRESHOLD_0]) { - ocp->protCnt = 0; - return; - } - - if (ocp->protCnt < ocp->protCntLimit) { - ocp->protCnt++; - return; - } - - /* Check which protection level should be activated. */ - /* Once enter error status, error status can only be changed by recover api. */ - /* protect level: level 4. */ - if (ocp->currAmp >= ocp->protValThr[PROT_VAL_THRESHOLD_3] && ocp->protLevel < PROT_LEVEL_4) { - ocp->protLevel = PROT_LEVEL_4; - motorErrStatus->Bit.overCurrErr = 1; - ocp->protCnt = 0; - return; - } - - /* Protect level: level 3. */ - if (ocp->currAmp >= ocp->protValThr[PROT_VAL_THRESHOLD_2] && ocp->protLevel < PROT_LEVEL_3) { - ocp->protLevel = PROT_LEVEL_3; - motorErrStatus->Bit.overCurrErr = 1; - ocp->protCnt = 0; - ocp->timer = 0.0f; - return; - } - - /* Protect level: level 2. */ - if (ocp->currAmp >= ocp->protValThr[PROT_VAL_THRESHOLD_1] && ocp->protLevel < PROT_LEVEL_2) { - ocp->protLevel = PROT_LEVEL_2; - motorErrStatus->Bit.overCurrErr = 1; - ocp->protCnt = 0; - ocp->timer = 0.0f; - return; - } - - /* Protect level: level 1. */ - if (ocp->currAmp >= ocp->protValThr[PROT_VAL_THRESHOLD_0] && ocp->protLevel < PROT_LEVEL_1) { - ocp->protLevel = PROT_LEVEL_1; - motorErrStatus->Bit.overCurrErr = 1; - ocp->protCnt = 0; - ocp->timer = 0.0f; - return; - } -} - -/** - * @brief Over current protection execution. - * @param ocp Over current protection handle. - * @param idqRef DQ-axis current references. - * @param aptAddr Three-phase APT address pointer. - * @retval None. - */ -void OCP_Exec(OCP_Handle *ocp, DqAxis *idqRef, APT_RegStruct **aptAddr) -{ - MCS_ASSERT_PARAM(ocp != NULL); - MCS_ASSERT_PARAM(idqRef != NULL); - MCS_ASSERT_PARAM(aptAddr != NULL); - - float id = idqRef->d; - float iq = idqRef->q; - float idqAmp = ocp->currAmp; - /* According to protect level, take corresponding action. */ - switch (ocp->protLevel) { - /* level 4: disable all PWM output. */ - case PROT_LEVEL_4: - /* Disable three-phase pwm output. */ - ProtSpo_Exec(aptAddr); - idqRef->d = 0.0f; - idqRef->q = 0.0f; - break; - - /* level 3: derate speed reference. */ - case PROT_LEVEL_3: - ocp->timer += ocp->ts; - if (ocp->timer > ocp->protLimitTime[PROT_LIMIT_TIME_2]) { - idqRef->d = id / idqAmp * PROT_MOTOR_RATED_CURR; - idqRef->q = iq / idqAmp * PROT_MOTOR_RATED_CURR; - } - break; - - /* level 2: derate speed reference. */ - case PROT_LEVEL_2: - ocp->timer += ocp->ts; - if (ocp->timer > ocp->protLimitTime[PROT_LIMIT_TIME_1]) { - idqRef->d = id / idqAmp * PROT_MOTOR_RATED_CURR; - idqRef->q = iq / idqAmp * PROT_MOTOR_RATED_CURR; - } - break; - - /* level 1: derate speed reference. */ - case PROT_LEVEL_1: - ocp->timer += ocp->ts; - if (ocp->timer > ocp->protLimitTime[PROT_LIMIT_TIME_0]) { - idqRef->d = id / idqAmp * PROT_MOTOR_RATED_CURR; - idqRef->q = iq / idqAmp * PROT_MOTOR_RATED_CURR; - } - break; - - /* level 0: take no protection action. */ - case PROT_LEVEL_0: - break; - - default: - break; - } -} - -/** - * @brief Over current protection recovery. - * @param ocp Over current protection handle. - * @param motorErrStatus Motor error status. - * @retval None. - */ -void OCP_Recy(OCP_Handle *ocp, MotorErrStatusReg *motorErrStatus) -{ - MCS_ASSERT_PARAM(ocp != NULL); - /* If not under error state, just return without any operation. */ - if (!motorErrStatus->Bit.overCurrErr) { - return; - } - - /* Calculate current amplitude. */ - float currAmp = ocp->currAmp; - - /* According to protection level, take corresponding recovery action. */ - switch (ocp->protLevel) { - /* level 4 */ - case PROT_LEVEL_4: - if (currAmp < ocp->protValThr[PROT_VAL_THRESHOLD_3] - ocp->recyDelta) { - ocp->recyCnt++; - if (ocp->recyCnt > ocp->recyCntLimit) { - ocp->protLevel = PROT_LEVEL_3; - ocp->recyCnt = 0; - } - } - break; - - /* level 3 */ - case PROT_LEVEL_3: - if (currAmp < ocp->protValThr[PROT_VAL_THRESHOLD_2] - ocp->recyDelta) { - ocp->recyCnt++; - if (ocp->recyCnt > ocp->recyCntLimit) { - ocp->protLevel = PROT_LEVEL_2; - ocp->recyCnt = 0; - } - } - break; - - /* level 2 */ - case PROT_LEVEL_2: - if (currAmp < ocp->protValThr[PROT_VAL_THRESHOLD_1] - ocp->recyDelta) { - ocp->recyCnt++; - if (ocp->recyCnt > ocp->recyCntLimit) { - ocp->protLevel = PROT_LEVEL_1; - ocp->recyCnt = 0; - } - } - break; - - /* level 1 */ - case PROT_LEVEL_1: - if (currAmp < ocp->protValThr[PROT_VAL_THRESHOLD_0] - ocp->recyDelta) { - ocp->recyCnt++; - if (ocp->recyCnt > ocp->recyCntLimit) { - ocp->protLevel = PROT_LEVEL_0; - ocp->recyCnt = 0; - } - } - break; - - /* level 0 */ - case PROT_LEVEL_0: - motorErrStatus->Bit.overCurrErr = 0; - break; - - default: - break; - } -} - -/** - * @brief Over current protection error status clear. - * @param ocp Over current protection handle. - * @retval None. - */ -void OCP_Clear(OCP_Handle *ocp) -{ - MCS_ASSERT_PARAM(ocp != NULL); - /* Clear the history value. */ - ocp->protCnt = 0; - ocp->protLevel = PROT_LEVEL_0; - ocp->recyCnt = 0; - ocp->timer = 0.0f; -} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/mcs_curr_prot.h b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/mcs_curr_prot.h deleted file mode 100644 index e9916fe4..00000000 --- a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/mcs_curr_prot.h +++ /dev/null @@ -1,52 +0,0 @@ -/** - * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_curr_prot.h - * @author MCU Algorithm Team - * @brief This file contains current protection data struct and api declaration. - */ - -/* Define to prevent recursive inclusion ------------------------------------- */ -#ifndef McuMagicTag_MCS_CURR_PROT_H -#define McuMagicTag_MCS_CURR_PROT_H - -/* Includes ------------------------------------------------------------------------------------ */ -#include "mcs_prot_cmm.h" -#include "mcs_typedef.h" -#include "apt_ip.h" - -typedef struct { - unsigned int protCnt; - unsigned int recyCnt; - unsigned int protCntLimit; - unsigned int recyCntLimit; - float currAmp; - float protValThr[PROT_VAL_THRESHOLD_NUMS]; /* from low current level to high. */ - float protLimitTime[PROT_LIMIT_TIME_NUMS]; /* from level 3 to level 1. */ - float recyDelta; - float timer; - float ts; - PROT_Level protLevel; -} OCP_Handle; - -void OCP_Init(OCP_Handle *ocp, float ts); -void OCP_Det(OCP_Handle *ocp, MotorErrStatusReg *motorErrStatus, DqAxis idq); -void OCP_Exec(OCP_Handle *ocp, DqAxis *idqRef, APT_RegStruct **aptAddr); -void OCP_Recy(OCP_Handle *ocp, MotorErrStatusReg *motorErrStatus); -void OCP_Clear(OCP_Handle *ocp); - -#endif diff --git a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/mcs_dc_volt_prot.c b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/mcs_dc_volt_prot.c deleted file mode 100644 index b0541a2e..00000000 --- a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/mcs_dc_volt_prot.c +++ /dev/null @@ -1,495 +0,0 @@ -/** - * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_dc_volt_prot.c - * @author MCU Algorithm Team - * @brief This file contains dc-link voltage protection api declaration. - */ - -/* Includes ------------------------------------------------------------------------------------ */ -#include "mcs_dc_volt_prot.h" -#include "mcs_prot_user_config.h" -#include "mcs_assert.h" - -/** - * @brief Initilization over dc-link voltage protection function. - * @param ovp Over dc-link voltage protection handle. - * @param ts Ctrl period (s). - * @retval None. - */ -void OVP_Init(OVP_Handle *ovp, float ts) -{ - MCS_ASSERT_PARAM(ovp != NULL); - MCS_ASSERT_PARAM(ts > 0.0f); - ovp->ts = ts; - OVP_Clear(ovp); - ovp->protCntLimit = PROT_CNT_LIMIT; - ovp->recyCntLimit = OVER_VOLT_RECY_CNT_LIMIT; - /* Configuring four levels of overvoltage protection thresholds. */ - ovp->protValThr[PROT_VAL_THRESHOLD_0] = PROT_OVER_VOLT_BRK_ON1; - ovp->protValThr[PROT_VAL_THRESHOLD_1] = PROT_OVER_VOLT_BRK_ON2; - ovp->protValThr[PROT_VAL_THRESHOLD_2] = PROT_OVER_VOLT_BRK_ON3; - ovp->protValThr[PROT_VAL_THRESHOLD_3] = PROT_OVER_VOLT_BRK_ALL; - /* Configure the protection limit time. */ - ovp->protLimitTime[PROT_LIMIT_TIME_0] = PROT_OVER_VOLT_LIMIT1_TIME_SEC; - ovp->protLimitTime[PROT_LIMIT_TIME_1] = PROT_OVER_VOLT_LIMIT2_TIME_SEC; - ovp->protLimitTime[PROT_LIMIT_TIME_2] = PROT_OVER_VOLT_LIMIT3_TIME_SEC; - ovp->recyDelta = PROT_OVER_VOLT_RECY_DELTA; -} - -/** - * @brief Initilization lower dc-link voltage protection function. - * @param lvp Lower dc-link voltage protection handle. - * @param ts Ctrl period (s). - * @retval None. - */ -void LVP_Init(LVP_Handle *lvp, float ts) -{ - MCS_ASSERT_PARAM(lvp != NULL); - MCS_ASSERT_PARAM(ts > 0.0f); - - lvp->ts = ts; - LVP_Clear(lvp); - - lvp->protCntLimit = PROT_CNT_LIMIT; - lvp->recyCntLimit = LOWER_VOLT_RECY_CNT_LIMIT; - /* Configuring four levels of lower voltage protection thresholds. */ - lvp->protValThr[PROT_VAL_THRESHOLD_0] = PROT_LOWER_VOLT_POW_DN1; - lvp->protValThr[PROT_VAL_THRESHOLD_1] = PROT_LOWER_VOLT_POW_DN2; - lvp->protValThr[PROT_VAL_THRESHOLD_2] = PROT_LOWER_VOLT_POW_DN3; - lvp->protValThr[PROT_VAL_THRESHOLD_3] = PROT_LOWER_VOLT_POW_OFF; - /* Configure the protection limit time. */ - lvp->protLimitTime[PROT_LIMIT_TIME_0] = PROT_LOWER_VOLT_LIMIT1_TIME_SEC; - lvp->protLimitTime[PROT_LIMIT_TIME_1] = PROT_LOWER_VOLT_LIMIT2_TIME_SEC; - lvp->protLimitTime[PROT_LIMIT_TIME_2] = PROT_LOWER_VOLT_LIMIT3_TIME_SEC; - lvp->recyDelta = PROT_OVER_VOLT_RECY_DELTA; -} - -/** - * @brief Over dc-link voltage protection detection. - * @param ovp Over dc-link voltage protection handle. - * @param motorErrStatus Motor error status. - * @param udc DC-link voltage feedback (V). - * @retval None. - */ -void OVP_Det(OVP_Handle *ovp, MotorErrStatusReg *motorErrStatus, float udc) -{ - MCS_ASSERT_PARAM(ovp != NULL); - MCS_ASSERT_PARAM(motorErrStatus != NULL); - MCS_ASSERT_PARAM(udc > 0.0f); - /* Check if value goes over threshold for continuous cycles. */ - if (udc < ovp->protValThr[PROT_VAL_THRESHOLD_0]) { - ovp->protCnt = 0; - return; - } - - if (ovp->protCnt < ovp->protCntLimit) { - ovp->protCnt++; - return; - } - - /* Check which protection level should be activated. */ - /* Once enter error status, error status can only be changed by recover api. */ - /* protect level: level 4. */ - if (udc >= ovp->protValThr[PROT_VAL_THRESHOLD_3] && ovp->protLevel < PROT_LEVEL_4) { - ovp->protLevel = PROT_LEVEL_4; - motorErrStatus->Bit.overVoltErr = 1; - ovp->protCnt = 0; - return; - } - - /* Protect level: level 3. */ - if (udc >= ovp->protValThr[PROT_VAL_THRESHOLD_2] && ovp->protLevel < PROT_LEVEL_3) { - ovp->protLevel = PROT_LEVEL_3; - motorErrStatus->Bit.overVoltErr = 1; - ovp->protCnt = 0; - ovp->timer = 0.0f; - return; - } - - /* Protect level: level 2. */ - if (udc >= ovp->protValThr[PROT_VAL_THRESHOLD_1] && ovp->protLevel < PROT_LEVEL_2) { - ovp->protLevel = PROT_LEVEL_2; - motorErrStatus->Bit.overVoltErr = 1; - ovp->protCnt = 0; - ovp->timer = 0.0f; - return; - } - - /* Protect level: level 1. */ - if (udc >= ovp->protValThr[PROT_VAL_THRESHOLD_0] && ovp->protLevel < PROT_LEVEL_1) { - ovp->protLevel = PROT_LEVEL_1; - motorErrStatus->Bit.overVoltErr = 1; - ovp->protCnt = 0; - ovp->timer = 0.0f; - return; - } -} - -/** - * @brief Lower dc-link voltage protection detection. - * @param lvp Lower dc-link voltage protection handle. - * @param motorErrStatus Motor error status. - * @param udc DC-link voltage feedback (V). - * @retval None. - */ -void LVP_Det(LVP_Handle *lvp, MotorErrStatusReg *motorErrStatus, float udc) -{ - MCS_ASSERT_PARAM(lvp != NULL); - MCS_ASSERT_PARAM(motorErrStatus != NULL); - MCS_ASSERT_PARAM(udc > 0.0f); - /* Check if value goes over threshold for continuous cycles. */ - if (udc > lvp->protValThr[PROT_VAL_THRESHOLD_0]) { - lvp->protCnt = 0; - return; - } - - if (lvp->protCnt < lvp->protCntLimit) { - lvp->protCnt++; - return; - } - - /* Check which protection level should be activated. */ - /* Once enter error status, error status can only be changed by recover api. */ - /* protect level: level 4. */ - if (udc <= lvp->protValThr[PROT_VAL_THRESHOLD_3] && lvp->protLevel < PROT_LEVEL_4) { - lvp->protLevel = PROT_LEVEL_4; - motorErrStatus->Bit.lowerVoltErr = 1; - lvp->protCnt = 0; - return; - } - - /* Protect level: level 3. */ - if (udc <= lvp->protValThr[PROT_VAL_THRESHOLD_2] && lvp->protLevel < PROT_LEVEL_3) { - lvp->protLevel = PROT_LEVEL_3; - motorErrStatus->Bit.lowerVoltErr = 1; - lvp->protCnt = 0; - lvp->timer = 0.0f; - return; - } - - /* Protect level: level 2. */ - if (udc <= lvp->protValThr[PROT_VAL_THRESHOLD_1] && lvp->protLevel < PROT_LEVEL_2) { - lvp->protLevel = PROT_LEVEL_2; - motorErrStatus->Bit.lowerVoltErr = 1; - lvp->protCnt = 0; - lvp->timer = 0.0f; - return; - } - - /* Protect level: level 1. */ - if (udc <= lvp->protValThr[PROT_VAL_THRESHOLD_0] && lvp->protLevel < PROT_LEVEL_1) { - lvp->protLevel = PROT_LEVEL_1; - motorErrStatus->Bit.lowerVoltErr = 1; - lvp->protCnt = 0; - lvp->timer = 0.0f; - return; - } -} - -/** - * @brief Over dc-link voltage protection execution. - * @param ovp Over dc-link voltage protection handle. - * @param duty Brake loop output duty (0-1). - * @param aptAddr Three-phase APT address pointer. - * @retval None. - */ -void OVP_Exec(OVP_Handle *ovp, float *duty, APT_RegStruct **aptAddr) -{ - MCS_ASSERT_PARAM(ovp != NULL); - MCS_ASSERT_PARAM(duty != NULL); - MCS_ASSERT_PARAM(aptAddr != NULL); - /* According to protect level, take corresponding action. */ - switch (ovp->protLevel) { - /* level 4: brake loop duty maximum. */ - case PROT_LEVEL_4: - *duty = PROT_OVER_VOLT_BRK_DUTY4; - /* Disable three-phase pwm output. */ - ProtSpo_Exec(aptAddr); - break; - - /* level 3: brake loop duty level 3. */ - case PROT_LEVEL_3: - *duty = PROT_OVER_VOLT_BRK_DUTY2; - ovp->timer += ovp->ts; - if (ovp->timer > ovp->protLimitTime[PROT_LIMIT_TIME_2]) { - *duty = PROT_OVER_VOLT_BRK_DUTY3; - } - break; - - /* level 2: brake loop duty level 2. */ - case PROT_LEVEL_2: - *duty = PROT_OVER_VOLT_BRK_DUTY1; - ovp->timer += ovp->ts; - if (ovp->timer > ovp->protLimitTime[PROT_LIMIT_TIME_1]) { - *duty = PROT_OVER_VOLT_BRK_DUTY2; - } - break; - - /* level 1: brake loop duty level 1. */ - case PROT_LEVEL_1: - ovp->timer += ovp->ts; - if (ovp->timer > ovp->protLimitTime[PROT_LIMIT_TIME_0]) { - *duty = PROT_OVER_VOLT_BRK_DUTY1; - } - break; - - /* level 0: take no protection action. */ - case PROT_LEVEL_0: - break; - - default: - break; - } - return; -} - -/** - * @brief Lower dc-link voltage protection execution. - * @param lvp Lower dc-link voltage protection handle. - * @param spdRef Speed Reference (Hz). - * @param aptAddr Three-phase APT address pointer. - * @retval None. - */ -void LVP_Exec(LVP_Handle *lvp, float *spdRef, APT_RegStruct **aptAddr) -{ - MCS_ASSERT_PARAM(lvp != NULL); - MCS_ASSERT_PARAM(spdRef != NULL); - MCS_ASSERT_PARAM(aptAddr != NULL); - /* According to protect level, take corresponding action. */ - switch (lvp->protLevel) { - /* level 4: disable all PWM output. */ - case PROT_LEVEL_4: - /* Disable three-phase pwm output. */ - ProtSpo_Exec(aptAddr); - *spdRef *= 0.0f; - break; - - /* level 3: derate speed reference. */ - case PROT_LEVEL_3: - *spdRef *= PROT_POW_DN2_PCT; - lvp->timer += lvp->ts; - if (lvp->timer > lvp->protLimitTime[PROT_LIMIT_TIME_2]) { - *spdRef *= PROT_POW_DN3_PCT; - } - break; - - /* level 2: derate speed reference. */ - case PROT_LEVEL_2: - *spdRef *= PROT_POW_DN1_PCT; - lvp->timer += lvp->ts; - if (lvp->timer > lvp->protLimitTime[PROT_LIMIT_TIME_1]) { - *spdRef *= PROT_POW_DN2_PCT; - } - break; - - /* level 1: derate speed reference. */ - case PROT_LEVEL_1: - lvp->timer += lvp->ts; - if (lvp->timer > lvp->protLimitTime[PROT_LIMIT_TIME_0]) { - *spdRef *= PROT_POW_DN1_PCT; - } - break; - - /* level 0: take no protection action. */ - case PROT_LEVEL_0: - break; - - default: - break; - } - return; -} - -/** - * @brief Over dc-link voltage protection recovery. - * @param ovp Over dc-link voltage protection handle. - * @param motorErrStatus Motor error status. - * @param udc DC-link voltage (V). - * @retval None. - */ -void OVP_Recy(OVP_Handle *ovp, MotorErrStatusReg *motorErrStatus, float udc) -{ - MCS_ASSERT_PARAM(ovp != NULL); - /* If not under error state, just return without any operation. */ - if (!motorErrStatus->Bit.overVoltErr) { - return; - } - - /* According to protection level, take corresponding recovery action. */ - switch (ovp->protLevel) { - /* level 4 */ - case PROT_LEVEL_4: - if (udc < ovp->protValThr[PROT_VAL_THRESHOLD_3] - ovp->recyDelta) { - ovp->recyCnt++; - if (ovp->recyCnt > ovp->recyCntLimit) { - ovp->protLevel = PROT_LEVEL_3; - ovp->recyCnt = 0; - } - } - break; - - /* level 3 */ - case PROT_LEVEL_3: - /* If the dc-link voltage is less than threshold 2, level-2 protection is restored. */ - if (udc < ovp->protValThr[PROT_VAL_THRESHOLD_2] - ovp->recyDelta) { - ovp->recyCnt++; - if (ovp->recyCnt > ovp->recyCntLimit) { - ovp->protLevel = PROT_LEVEL_2; - ovp->recyCnt = 0; - } - } - break; - - /* level 2 */ - case PROT_LEVEL_2: - /* If the dc-link voltage is less than threshold 1, level-1 protection is restored. */ - if (udc < ovp->protValThr[PROT_VAL_THRESHOLD_1] - ovp->recyDelta) { - ovp->recyCnt++; - if (ovp->recyCnt > ovp->recyCntLimit) { - ovp->protLevel = PROT_LEVEL_1; - ovp->recyCnt = 0; - } - } - break; - - /* level 1 */ - case PROT_LEVEL_1: - /* If the dc-link voltage is less than threshold 0, level-0 protection is restored. */ - if (udc < ovp->protValThr[PROT_VAL_THRESHOLD_0] - ovp->recyDelta) { - ovp->recyCnt++; - if (ovp->recyCnt > ovp->recyCntLimit) { - ovp->protLevel = PROT_LEVEL_0; - ovp->recyCnt = 0; - } - } - break; - /* level 0 Fault-free state. */ - case PROT_LEVEL_0: - motorErrStatus->Bit.overVoltErr = 0; - break; - - default: - break; - } - return; -} - -/** - * @brief Lower dc-link voltage protection recovery. - * @param lvp Lower dc-link voltage protection handle. - * @param motorErrStatus Motor error status. - * @param udc DC-link voltage (V). - * @retval None. - */ -void LVP_Recy(LVP_Handle *lvp, MotorErrStatusReg *motorErrStatus, float udc) -{ - MCS_ASSERT_PARAM(lvp != NULL); - /* If not under error state, just return without any operation. */ - if (!motorErrStatus->Bit.lowerVoltErr) { - return; - } - - /* According to protection level, take corresponding recovery action. */ - switch (lvp->protLevel) { - /* level 4 */ - case PROT_LEVEL_4: - /* If the dc-link voltage is greater than threshold 3, level-3 protection is restored. */ - if (udc > lvp->protValThr[PROT_VAL_THRESHOLD_3] + lvp->recyDelta) { - lvp->recyCnt++; - if (lvp->recyCnt > lvp->recyCntLimit) { - lvp->protLevel = PROT_LEVEL_3; - lvp->recyCnt = 0; - } - } - break; - - /* level 3 */ - case PROT_LEVEL_3: - /* If the dc-link voltage is greater than threshold 2, level-2 protection is restored. */ - if (udc > lvp->protValThr[PROT_VAL_THRESHOLD_2] + lvp->recyDelta) { - lvp->recyCnt++; - if (lvp->recyCnt > lvp->recyCntLimit) { - lvp->protLevel = PROT_LEVEL_2; - lvp->recyCnt = 0; - } - } - break; - - /* level 2 */ - case PROT_LEVEL_2: - /* If the dc-link voltage is greater than threshold 1, level-1 protection is restored. */ - if (udc > lvp->protValThr[PROT_VAL_THRESHOLD_1] + lvp->recyDelta) { - lvp->recyCnt++; - if (lvp->recyCnt > lvp->recyCntLimit) { - lvp->protLevel = PROT_LEVEL_1; - lvp->recyCnt = 0; - } - } - break; - - /* level 1 */ - case PROT_LEVEL_1: - /* If the dc-link voltage is greater than threshold 0, level-0 protection is restored. */ - if (udc > lvp->protValThr[PROT_VAL_THRESHOLD_0] + lvp->recyDelta) { - lvp->recyCnt++; - if (lvp->recyCnt > lvp->recyCntLimit) { - lvp->protLevel = PROT_LEVEL_0; - lvp->recyCnt = 0; - } - } - break; - /* level 0 Fault-free state. */ - case PROT_LEVEL_0: - motorErrStatus->Bit.lowerVoltErr = 0; - break; - - default: - break; - } - return; -} - -/** - * @brief Over dc-link voltage protection error status clear. - * @param ovp Over voltage protection handle. - * @retval None. - */ -void OVP_Clear(OVP_Handle *ovp) -{ - MCS_ASSERT_PARAM(ovp != NULL); - /* Clear the history value. */ - ovp->protCnt = 0; - ovp->protLevel = PROT_LEVEL_0; - ovp->recyCnt = 0; - ovp->timer = 0.0f; -} - -/** - * @brief Lower dc-link voltage protection error status clear. - * @param lvp Lower voltage protection handle. - * @retval None. - */ -void LVP_Clear(LVP_Handle *lvp) -{ - MCS_ASSERT_PARAM(lvp != NULL); - /* Clear the history value. */ - lvp->protCnt = 0; - lvp->protLevel = PROT_LEVEL_0; - lvp->recyCnt = 0; - lvp->timer = 0.0f; -} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/mcs_dc_volt_prot.h b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/mcs_dc_volt_prot.h deleted file mode 100644 index 0435156a..00000000 --- a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/mcs_dc_volt_prot.h +++ /dev/null @@ -1,69 +0,0 @@ -/** - * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_dc_volt_prot.h - * @author MCU Algorithm Team - * @brief This file contains dc-link voltage protection data struct and api declaration. - */ - -/* Define to prevent recursive inclusion ------------------------------------------------------- */ -#ifndef McuMagicTag_MCS_DC_VOLT_PROT_H -#define McuMagicTag_MCS_DC_VOLT_PROT_H - -/* Includes ------------------------------------------------------------------------------------ */ -#include "mcs_prot_cmm.h" -#include "apt_ip.h" - -typedef struct { - unsigned int protCnt; - unsigned int recyCnt; - unsigned int protCntLimit; - unsigned int recyCntLimit; - float protValThr[PROT_VAL_THRESHOLD_NUMS]; /* from low voltage level to high. */ - float protLimitTime[PROT_LIMIT_TIME_NUMS]; /* from level 3 to level 1. */ - float recyDelta; - float timer; - float ts; - PROT_Level protLevel; -} OVP_Handle; - -typedef struct { - unsigned int protCnt; - unsigned int recyCnt; - unsigned int protCntLimit; - unsigned int recyCntLimit; - float protValThr[PROT_VAL_THRESHOLD_NUMS]; /* from high voltage level to low. */ - float protLimitTime[PROT_LIMIT_TIME_NUMS]; /* from level 3 to level 1. */ - float recyDelta; - float timer; - float ts; - PROT_Level protLevel; -} LVP_Handle; - -void OVP_Init(OVP_Handle *ovp, float ts); -void OVP_Det(OVP_Handle *ovp, MotorErrStatusReg *motorErrStatus, float udc); -void OVP_Exec(OVP_Handle *ovp, float *duty, APT_RegStruct **aptAddr); -void OVP_Recy(OVP_Handle *ovp, MotorErrStatusReg *motorErrStatus, float udc); -void OVP_Clear(OVP_Handle *ovp); - -void LVP_Init(LVP_Handle *lvp, float ts); -void LVP_Det(LVP_Handle *lvp, MotorErrStatusReg *motorErrStatus, float udc); -void LVP_Exec(LVP_Handle *lvp, float *spdRef, APT_RegStruct **aptAddr); -void LVP_Recy(LVP_Handle *lvp, MotorErrStatusReg *motorErrStatus, float udc); -void LVP_Clear(LVP_Handle *lvp); - -#endif diff --git a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/mcs_motor_stalling.c b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/mcs_motor_stalling.c deleted file mode 100644 index 9dee4d2d..00000000 --- a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/mcs_motor_stalling.c +++ /dev/null @@ -1,112 +0,0 @@ -/** - * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_motor_stalling.c - * @author MCU Algorithm Team - * @brief This file contains motor stalling protection data struct and api definition. - */ - -/* Includes ------------------------------------------------------------------------------------ */ -#include "mcs_motor_stalling.h" -#include "mcs_prot_user_config.h" -#include "mcs_math.h" -#include "mcs_math_const.h" -#include "mcs_assert.h" - -/** - * @brief Initilization motor stalling protection function. - * @param stall Motor stalling handle. - * @param ts Ctrl period (s). - * @param currLimit The current amplitude that triggers fault. (A). - * @param spdLimit The speed amplitude that triggers fault. (Hz). - * @param timeLimit The threshold time that current amplitude over the limit (s). - * @retval None. - */ -void STP_Init(STP_Handle *stall, float ts, float currLimit, float spdLimit, float timeLimit) -{ - MCS_ASSERT_PARAM(stall != NULL); - MCS_ASSERT_PARAM(ts > 0.0f); - MCS_ASSERT_PARAM(currLimit > 0.0f); - MCS_ASSERT_PARAM(spdLimit > 0.0f); - MCS_ASSERT_PARAM(timeLimit > 0.0f); - /* Configuring parameters for stalling detection. */ - stall->ts = ts; - /* Current threshold and speed threshold for stalling fault. */ - stall->currAmpLimit = currLimit; - stall->spdLimit = spdLimit; - stall->timeLimit = timeLimit; - stall->timer = 0.0f; -} - -/** - * @brief Motor stalling detection. - * @param stall Motor stalling handle. - * @param motorErrStatus Motor error status. - * @param spd Speed feedback (Hz). - * @param idq Dq-axis current feedback (A). - * @retval None. - */ -void STP_Det_ByCurrSpd(STP_Handle *stall, MotorErrStatusReg *motorErrStatus, float spd, DqAxis idq) -{ - MCS_ASSERT_PARAM(stall != NULL); - MCS_ASSERT_PARAM(motorErrStatus != NULL); - MCS_ASSERT_PARAM(Abs(spd) >= 0.0f); - /* Calculate current amplitude. */ - float currAmp = Sqrt(idq.d * idq.d + idq.q * idq.q); - float spdAbs = Abs(spd); - /* Check if value goes over threshold for continuous cycles. */ - if (currAmp < stall->currAmpLimit || spdAbs > stall->spdLimit) { - stall->timer = 0.0f; - return; - } - /* Time accumulation. */ - if (stall->timer < stall->timeLimit) { - stall->timer += stall->ts; - return; - } - motorErrStatus->Bit.motorStalling = 1; -} - -/** - * @brief Motor stalling protection execution. - * @param motorErrStatus Motor error status. - * @param aptAddr Three-phase APT address pointer. - * @retval None. - */ -void STP_Exec(MotorErrStatusReg *motorErrStatus, APT_RegStruct **aptAddr) -{ - MCS_ASSERT_PARAM(motorErrStatus != NULL); - MCS_ASSERT_PARAM(aptAddr != NULL); - if (motorErrStatus->Bit.motorStalling == 0) { - return; - } - - /* Disable three-phase pwm output. */ - ProtSpo_Exec(aptAddr); - return; -} - -/** - * @brief Motor stalling protection error status clear. - * @param stall Motor stalling handle. - * @retval None. - */ -void STP_Clear(STP_Handle *stall) -{ - MCS_ASSERT_PARAM(stall != NULL); - stall->timer = 0.0f; -} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/mcs_motor_stalling.h b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/mcs_motor_stalling.h deleted file mode 100644 index a1d1e4be..00000000 --- a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/mcs_motor_stalling.h +++ /dev/null @@ -1,45 +0,0 @@ -/** - * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_motor_stalling.h - * @author MCU Algorithm Team - * @brief This file contains motor stalling protection data struct and api declaration. - */ - -/* Define to prevent recursive inclusion ------------------------------------- */ -#ifndef McuMagicTag_MCS_MOTOR_STALLING_PROT_H -#define McuMagicTag_MCS_MOTOR_STALLING_PROT_H - -/* Includes ------------------------------------------------------------------------------------ */ -#include "mcs_prot_cmm.h" -#include "apt_ip.h" -#include "mcs_typedef.h" - -typedef struct { - float currAmpLimit; /**< Feedback current higher than this value triggers fault. (A). */ - float spdLimit; /**< Feedback speed lower than this value triggers fault (Hz). */ - float timeLimit; /**< The threshold time that current and speed feedback over ranges (s). */ - float timer; /**< Timer to get speed and current over range time. */ - float ts; /**< Ctrl period (s). */ -} STP_Handle; - -void STP_Init(STP_Handle *stall, float ts, float currLimit, float spdLimit, float timeLimit); -void STP_Det_ByCurrSpd(STP_Handle *stall, MotorErrStatusReg *motorErrStatus, float spd, DqAxis idq); -void STP_Exec(MotorErrStatusReg *motorErrStatus, APT_RegStruct **aptAddr); -void STP_Clear(STP_Handle *stall); - -#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/mcs_temp_prot.c b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/mcs_temp_prot.c deleted file mode 100644 index 184ba4a3..00000000 --- a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/mcs_temp_prot.c +++ /dev/null @@ -1,262 +0,0 @@ -/** - * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_temp_prot.c - * @author MCU Algorithm Team - * @brief This file contains over temperature protection api definition. - */ - -/* Includes ------------------------------------------------------------------------------------ */ -#include "mcs_temp_prot.h" -#include "mcs_prot_user_config.h" -#include "mcs_assert.h" - -/** - * @brief Initilization over temperation protection function. - * @param otp Over temperature protection handle. - * @param ts Ctrl period. - * @retval None. - */ -void OTP_Init(OTP_Handle *otp, float ts) -{ - MCS_ASSERT_PARAM(otp != NULL); - MCS_ASSERT_PARAM(ts > 0.0f); - otp->ts = ts; - otp->protCntLimit = PROT_CNT_LIMIT; - otp->recyCntLimit = RECY_CNT_LIMIT; - /* Configuring the temperature protection threshold. */ - otp->protValThr[PROT_VAL_THRESHOLD_0] = PROT_OVER_IPM_TEMP_POW_DN1; - otp->protValThr[PROT_VAL_THRESHOLD_1] = PROT_OVER_IPM_TEMP_POW_DN2; - otp->protValThr[PROT_VAL_THRESHOLD_2] = PROT_OVER_IPM_TEMP_POW_DN3; - otp->protValThr[PROT_VAL_THRESHOLD_3] = PROT_OVER_IPM_TEMP_POW_OFF; - /* Configuring the protection limiting time. */ - otp->protLimitTime[PROT_LIMIT_TIME_0] = PROT_OVER_TEMP_LIMIT1_TIME_SEC; - otp->protLimitTime[PROT_LIMIT_TIME_1] = PROT_OVER_TEMP_LIMIT2_TIME_SEC; - otp->protLimitTime[PROT_LIMIT_TIME_2] = PROT_OVER_TEMP_LIMIT3_TIME_SEC; - otp->recyDelta = PROT_OVER_IPM_TEMP_RECY_DELTA; - OTP_Clear(otp); -} - -/** - * @brief Over temperatre protection detection. - * @param otp Over temperature protection handle. - * @param tempErrBit Temperature error status bit. - * @param idq DQ-axis feedback currents. - * @retval None. - */ -void OTP_Det(OTP_Handle *otp, MotorErrStatusReg *motorErrStatus, PROT_ErrBit protBit, float temp) -{ - MCS_ASSERT_PARAM(otp != NULL); - MCS_ASSERT_PARAM(motorErrStatus != NULL); - MCS_ASSERT_PARAM(temp > 0.0f); - /* Check if value goes over threshold for continuous cycles. */ - if (temp < otp->protValThr[PROT_VAL_THRESHOLD_0]) { - otp->protCnt = 0; - ClearBit(&motorErrStatus->all, protBit); - return; - } - - if (otp->protCnt < otp->protCntLimit) { - otp->protCnt++; - return; - } - - /* Check which protection level should be activated. */ - /* Once enter error status, error status can only be changed by recover api. */ - /* protect level: level 4. */ - if (temp >= otp->protValThr[PROT_VAL_THRESHOLD_3] && otp->protLevel < PROT_LEVEL_4) { - otp->protLevel = PROT_LEVEL_4; - SetBit(&motorErrStatus->all, protBit); - otp->protCnt = 0; - return; - } - - /* Protect level: level 3. */ - if (temp >= otp->protValThr[PROT_VAL_THRESHOLD_2] && otp->protLevel < PROT_LEVEL_3) { - otp->protLevel = PROT_LEVEL_3; - SetBit(&motorErrStatus->all, protBit); - otp->protCnt = 0; - otp->timer = 0.0f; - return; - } - - /* Protect level: level 2. */ - if (temp >= otp->protValThr[PROT_VAL_THRESHOLD_1] && otp->protLevel < PROT_LEVEL_2) { - otp->protLevel = PROT_LEVEL_2; - SetBit(&motorErrStatus->all, protBit); - otp->protCnt = 0; - otp->timer = 0.0f; - return; - } - - /* Protect level: level 1. */ - if (temp >= otp->protValThr[PROT_VAL_THRESHOLD_0] && otp->protLevel < PROT_LEVEL_1) { - otp->protLevel = PROT_LEVEL_1; - SetBit(&motorErrStatus->all, protBit); - otp->protCnt = 0; - otp->timer = 0.0f; - return; - } -} - -/** - * @brief Over temperature protection execution. - * @param otp Over temperature protection handle. - * @param spdRef Speed reference (Hz). - * @param aptAddr Three-phase APT address pointer. - * @retval None. - */ -void OTP_Exec(OTP_Handle *otp, float *spdRef, APT_RegStruct **aptAddr) -{ - MCS_ASSERT_PARAM(otp != NULL); - MCS_ASSERT_PARAM(spdRef != NULL); - MCS_ASSERT_PARAM(aptAddr != NULL); - /* According to protect level, take corresponding action. */ - switch (otp->protLevel) { - /* level 4: disable all PWM output. */ - case PROT_LEVEL_4: - /* Disable three-phase pwm output. */ - ProtSpo_Exec(aptAddr); - *spdRef *= 0.0f; - break; - - /* level 3: derate speed reference. */ - case PROT_LEVEL_3: - *spdRef *= PROT_POW_DN2_PCT; - otp->timer += otp->ts; - if (otp->timer > otp->protLimitTime[PROT_LIMIT_TIME_2]) { - *spdRef *= PROT_POW_DN3_PCT; - } - break; - - /* level 2: derate speed reference. */ - case PROT_LEVEL_2: - /* Reducte motor speed to level 2 */ - *spdRef *= PROT_POW_DN1_PCT; - otp->timer += otp->ts; - if (otp->timer > otp->protLimitTime[PROT_LIMIT_TIME_1]) { - *spdRef *= PROT_POW_DN2_PCT; - } - break; - - /* level 0: take no protection action. */ - case PROT_LEVEL_0: - break; - - case PROT_LEVEL_1: - /* Reducte motor speed to level 0 */ - otp->timer += otp->ts; - if (otp->timer > otp->protLimitTime[PROT_LIMIT_TIME_0]) { - /* level 1: derate speed reference. */ - *spdRef *= PROT_POW_DN1_PCT; - } - break; - - default: - break; - } - return; -} - -/** - * @brief Over temperature protection recovery. - * @param otp Over temperature protection handle. - * @param tempErrBit Temperature error status bit. - * @param temp Temperature (celsius). - * @retval None. - */ -void OTP_Recy(OTP_Handle *otp, MotorErrStatusReg *motorErrStatus, PROT_ErrBit protBit, float temp) -{ - MCS_ASSERT_PARAM(otp != NULL); - MCS_ASSERT_PARAM(motorErrStatus != NULL); - MCS_ASSERT_PARAM(temp > 0.0f); - /* If not under error state, just return without any operation. */ - if (otp->protLevel == PROT_LEVEL_0) { - motorErrStatus->all &= (~(1 >> protBit)); - return; - } - - /* According to protection level, take corresponding recovery action. */ - switch (otp->protLevel) { - /* level 4 */ - case PROT_LEVEL_4: - /* If the temperature is less than threshold 3, level-3 protection is restored. */ - if (temp < otp->protValThr[PROT_VAL_THRESHOLD_3] - otp->recyDelta) { - otp->recyCnt++; - if (otp->recyCnt > otp->recyCntLimit) { - otp->protLevel = PROT_LEVEL_3; - otp->recyCnt = 0; - } - } - break; - - /* level 3 */ - case PROT_LEVEL_3: - /* If the temperature is less than threshold 2, level-2 protection is restored. */ - if (temp < otp->protValThr[PROT_VAL_THRESHOLD_2] - otp->recyDelta) { - otp->recyCnt++; - if (otp->recyCnt > otp->recyCntLimit) { - otp->protLevel = PROT_LEVEL_2; - otp->recyCnt = 0; - } - } - break; - - /* level 2 */ - case PROT_LEVEL_2: - /* If the temperature is less than threshold 1, level-1 protection is restored. */ - if (temp < otp->protValThr[PROT_VAL_THRESHOLD_1] - otp->recyDelta) { - otp->recyCnt++; - if (otp->recyCnt > otp->recyCntLimit) { - otp->protLevel = PROT_LEVEL_1; - otp->recyCnt = 0; - } - } - break; - - /* level 1 */ - case PROT_LEVEL_1: - /* If the temperature is less than threshold 0, level-0 protection is restored. */ - if (temp < otp->protValThr[PROT_VAL_THRESHOLD_0] - otp->recyDelta) { - otp->recyCnt++; - if (otp->recyCnt > otp->recyCntLimit) { - otp->protLevel = PROT_LEVEL_0; - otp->recyCnt = 0; - } - } - break; - - default: - break; - } - return; -} - -/** - * @brief Over temperature protection error status clear. - * @param otp Over temperature protection handle. - * @retval None. - */ -void OTP_Clear(OTP_Handle *otp) -{ - MCS_ASSERT_PARAM(otp != NULL); - /* Clear the history value. */ - otp->protCnt = 0; - otp->protLevel = PROT_LEVEL_0; - otp->recyCnt = 0; - otp->timer = 0.0f; -} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/mcs_temp_prot.h b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/mcs_temp_prot.h deleted file mode 100644 index 2b9d99a7..00000000 --- a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/protection/mcs_temp_prot.h +++ /dev/null @@ -1,49 +0,0 @@ -/** - * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_temp_prot.h - * @author MCU Algorithm Team - * @brief his file contains over temperature protection api declaration. - */ - -/* Define to prevent recursive inclusion ------------------------------------- */ -#ifndef McuMagicTag_MCS_TEMP_PROT_H -#define McuMagicTag_MCS_TEMP_PROT_H - -/* Includes ------------------------------------------------------------------------------------ */ -#include "mcs_prot_cmm.h" -#include "apt_ip.h" - -typedef struct { - unsigned int protCnt; - unsigned int recyCnt; - unsigned int protCntLimit; - unsigned int recyCntLimit; - float protValThr[PROT_VAL_THRESHOLD_NUMS]; /* from low current level to high. */ - float protLimitTime[PROT_LIMIT_TIME_NUMS]; /* from level 3 to level 1. */ - float recyDelta; - float ts; - float timer; - PROT_Level protLevel; -} OTP_Handle; - -void OTP_Init(OTP_Handle *otp, float ts); -void OTP_Det(OTP_Handle *otp, MotorErrStatusReg *motorErrStatus, PROT_ErrBit protBit, float temp); -void OTP_Exec(OTP_Handle *otp, float *spdRef, APT_RegStruct **aptAddr); -void OTP_Recy(OTP_Handle *otp, MotorErrStatusReg *motorErrStatus, PROT_ErrBit protBit, float temp); -void OTP_Clear(OTP_Handle *otp); -#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/readme.md b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/readme.md deleted file mode 100644 index 98b2a068..00000000 --- a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/readme.md +++ /dev/null @@ -1,10 +0,0 @@ -# pmsm_sensorless_2shunt_foc - -**【功能描述】** -+ 基于ECMCU105H/ECBMCU201MPC单板的单电机双电阻采样Foc应用 - -**【环境要求】** -+ 所有单板电源改制为演示用的12V低压,电机选用Gimbal GBM2804H-100T - -**【IDE配置方法】** -+ chipConfig中的Sample栏目里面选中pmsm sensorless 2shunt foc示例,然后点击生成代码即可 diff --git a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/src/mcs_carrier.c b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/src/mcs_carrier.c deleted file mode 100644 index 9fc8afac..00000000 --- a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/src/mcs_carrier.c +++ /dev/null @@ -1,144 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_carrier.c - * @author MCU Algorithm Team - * @brief This file provides carrier interrupt application for motor control. - */ - -#include "mcs_carrier.h" -#include "mcs_math.h" -#include "typedefs.h" -#include "mcs_assert.h" -#include "mcs_user_config.h" -#include "mcs_ctlmode_config.h" - -/** - * @brief Synchronous rotation coordinate system angle. - * @param mtrCtrl The motor control handle. - * @retval None. - */ -static void MCS_SyncCoorAngle(MTRCTRL_Handle *mtrCtrl) -{ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - /* Synchronous rotation coordinate system angle. */ - switch (mtrCtrl->stateMachine) { - case FSM_STARTUP: - /* Current ramp angle is 0. */ - if (mtrCtrl->startup.stage == STARTUP_STAGE_CURR) { - mtrCtrl->axisAngle = 0; - } else if (mtrCtrl->startup.stage == STARTUP_STAGE_SPD) { /* IF control phase angle self-addition. */ - mtrCtrl->axisAngle = IF_CurrAngleCalc(&mtrCtrl->ifCtrl, mtrCtrl->spdRefHz); - } else if (mtrCtrl->startup.stage == STARTUP_STAGE_SWITCH) { /* Switch Angle */ - mtrCtrl->axisAngle = mtrCtrl->smo.elecAngle; - } - break; - - case FSM_RUN: - mtrCtrl->axisAngle = mtrCtrl->smo.elecAngle; - break; - - default: - mtrCtrl->axisAngle = 0; - break; - } -} - -/** - * @brief PWM waveform setting and sampling point setting for a single resistors and dual resistors. - * @param mtrCtrl The motor control handle. - * @retval None. - */ -static void MCS_PwmAdcSet(MTRCTRL_Handle *mtrCtrl) -{ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - SampleMode sampleMode = mtrCtrl->sampleMode; - /* Set the duty cycle according to the sampling mode. */ - if (sampleMode == DUAL_RESISTORS) { - SVPWM_Exec(&mtrCtrl->sv, &mtrCtrl->vabRef, &mtrCtrl->dutyUvw); - mtrCtrl->setPwmDutyCb(&mtrCtrl->dutyUvw, &mtrCtrl->dutyUvw); - } else if (sampleMode == SINGLE_RESISTOR) { - R1SVPWM_Exec(&mtrCtrl->r1Sv, &mtrCtrl->vabRef, &mtrCtrl->dutyUvwLeft, &mtrCtrl->dutyUvwRight); - mtrCtrl->setPwmDutyCb(&mtrCtrl->dutyUvwLeft, &mtrCtrl->dutyUvwRight); - /* The ADC sampling point position needs to be set based on the phase shift of a single resistors. */ - mtrCtrl->setADCTriggerTimeCb(mtrCtrl->r1Sv.samplePoint[0] * mtrCtrl->aptMaxcntCmp, \ - mtrCtrl->r1Sv.samplePoint[1] * mtrCtrl->aptMaxcntCmp); - } -} - -/** - * @brief Carrier interrupt function. - * @param mtrCtrl The motor control handle. - * @retval None. - */ -void MCS_CarrierProcess(MTRCTRL_Handle *mtrCtrl) -{ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - UvwAxis *currUvw = &mtrCtrl->currUvw; - AlbeAxis *currAlbe = &mtrCtrl->iabFbk; - AlbeAxis *vab = &mtrCtrl->vabRef; - SampleMode sampleMode = mtrCtrl->sampleMode; - /* sample mode verify */ - if (sampleMode > SINGLE_RESISTOR || mtrCtrl->setADCTriggerTimeCb == NULL) { - return; - } - /* param verify */ - if (mtrCtrl->setPwmDutyCb == NULL || mtrCtrl->readCurrUvwCb == NULL) { - return; - } - /* Read the three-phase current value. */ - mtrCtrl->readCurrUvwCb(currUvw); - /* Clark Calc */ - ClarkeCalc(currUvw, currAlbe); - /* Smo observation */ - if (mtrCtrl->obserType == FOC_OBSERVERTYPE_SMO1TH) { - /* Smo observation */ - FOSMO_Exec(&mtrCtrl->smo, currAlbe, vab, mtrCtrl->spdRefHz); - } else if (mtrCtrl->obserType == FOC_OBSERVERTYPE_SMO4TH) { - /* Smo4th observation */ - SMO4TH_Exec(&mtrCtrl->smo4th, currAlbe, vab); - mtrCtrl->smo.spdEst = mtrCtrl->smo4th.spdEst; - mtrCtrl->smo.elecAngle = mtrCtrl->smo4th.elecAngle; - } - /* Synchronization angle */ - MCS_SyncCoorAngle(mtrCtrl); - - /* Park transformation */ - ParkCalc(currAlbe, mtrCtrl->axisAngle, &mtrCtrl->idqFbk); - - /* statemachine */ - switch (mtrCtrl->stateMachine) { - case FSM_STARTUP: - case FSM_RUN: - CURRCTRL_Exec(&mtrCtrl->currCtrl, &mtrCtrl->vdqRef, mtrCtrl->smo.spdEst, 0); - InvParkCalc(&mtrCtrl->vdqRef, mtrCtrl->axisAngle, vab); - MCS_PwmAdcSet(mtrCtrl); - break; - - case FSM_CAP_CHARGE: - case FSM_CLEAR: - case FSM_IDLE: - mtrCtrl->smo4th.spdEst = 0.0f; - break; - - default: - vab->alpha = 0.0f; - vab->beta = 0.0f; - MCS_PwmAdcSet(mtrCtrl); - break; - } -} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/src/mcs_motor_process.c b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/src/mcs_motor_process.c deleted file mode 100644 index 79bc3949..00000000 --- a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/src/mcs_motor_process.c +++ /dev/null @@ -1,950 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_motor_process.c - * @author MCU Algorithm Team - * @brief This file provides motor application. - * @details Single FOC application based on the ECMCU105H/ECBMCU201MPC board - * 1) Motor model is Gimbal GBM2804H-100T. - * 2) Select the pmsm sensorless 2shunt foc example in the sample column - of chipConfig and click Generate Code. - * 3) It's power supply must be changed to 12V. - */ -#include "main.h" -#include "mcs_user_config.h" -#include "mcs_math.h" -#include "hmi_module.h" -#include "mcs_ctlmode_config.h" -#include "mcs_prot_user.h" -#include "mcs_prot_user_config.h" -#include "mcs_math_const.h" -#include "mcs_motor_process.h" -#include "mcs_chip_config.h" -#include - -/*------------------------------- Macro Definition -----------------------------------------------*/ -#define US_PER_MS 1000 -#define ANGLE_RANGE_ABS 65536 -#define ANGLE_360_F 65536.0f /* 0 - 65536 indicates 0 to 360. */ -#define APT_FULL_DUTY 1.0f -#define TEMP_3 3.0f -#define TEMP_15 15.0f -#define TEMP_30 30.0f -#define TEMP_45 45.0f -#define TEMP_60 60.0f -#define TEMP_RES_15 78.327f -#define TEMP_RES_30 36.776f -#define TEMP_RES_45 18.301f -#define TEMP_RES_60 9.607f -#define CNT_10 10 -#define CNT_5000 5000 -#define LEVEL_4 4 -#define MOTOR_START_DELAY 2 -#define ADC_READINIT_DELAY 1 -#define ADC_READINIT_TIMES 20 -#define ADC_TRIMVALUE_MIN 1800.0f -#define ADC_TRIMVALUE_MAX 2200.0f -/*------------------------------- Param Definition -----------------------------------------------*/ -/* Motor parameters. */ -/* Np, Rs, Ld, Lq, Psif, J, Nmax, Currmax, PPMR, zShift */ -static MOTOR_Param g_motorParam = MOTORPARAM_DEFAULTS; -static APT_RegStruct* g_apt[PHASE_MAX_NUM] = {APT_U, APT_V, APT_W}; -/* Motor control handle */ -static MTRCTRL_Handle g_mc = {0}; - -/* Motor speed loop PI param. */ -static void SPDCTRL_InitWrapper(SPDCTRL_Handle *spdHandle, float ts) -{ - /* Speed loop param assignment. */ - PI_Param spdPi = { - .kp = SPD_KP, - .ki = SPD_KI, - .lowerLim = SPD_LOWERLIM, - .upperLim = SPD_UPPERLIM, - }; - /* Speed loop param init. */ - SPDCTRL_Init(spdHandle, &g_motorParam, spdPi, ts); -} - -/* Motor current Loop PI param. */ -static void CURRCTRL_InitWrapper(CURRCTRL_Handle *currHandle, DqAxis *idqRef, DqAxis *idqFbk, float ts) -{ - /* Axis-D current loop param assignment. */ - PI_Param dCurrPi = { - .kp = CURRDAXIS_KP, - .ki = CURRDAXIS_KI, - .lowerLim = CURR_LOWERLIM, - .upperLim = CURR_UPPERLIM, - }; - /* Axis-Q current loop param assignment. */ - PI_Param qCurrPi = { - .kp = CURRQAXIS_KP, - .ki = CURRQAXIS_KI, - .lowerLim = CURR_LOWERLIM, - .upperLim = CURR_UPPERLIM, - }; - /* Current loop param init. */ - CURRCTRL_Init(currHandle, &g_motorParam, idqRef, idqFbk, dCurrPi, qCurrPi, ts); -} - -/* First order smo param. */ -static void FOSMO_InitWrapper(FOSMO_Handle *fosmo, float ts) -{ - /* Smo param assignment. */ - FOSMO_Param fosmoParam = { - .gain = FOSMO_GAIN, - .lambda = FOSMO_LAMBDA, - .fcEmf = FOSMO_EMF_CUTOFF_FREQ, - .fcLpf = SPEED_FILTER_CUTOFF_FREQUENCY, - .pllBdw = FOSMO_PLL_BDW, - }; - /* Init smo param. */ - FOSMO_Init(fosmo, fosmoParam, &g_motorParam, ts); -} - -/* Smo4th param. */ -static void SMO4TH_InitWrapper(SMO4TH_Handle *smo4TH) -{ - /* Smo4th param assignment. */ - SMO4TH_Param smo4thParam = { - .kd = SMO4TH_KD, - .kq = SMO4TH_KQ, - .pllBdw = SMO4TH_PLL_BDW, - .fcLpf = SMO4TH_SPD_FILTER_CUTOFF_FREQ, - }; - /* Init smo param. */ - SMO4TH_Init(smo4TH, smo4thParam, &g_motorParam, CTRL_CURR_PERIOD); -} - -/*------------------------------- Function Definition -----------------------------------------------*/ -/** - * @brief Initialzer of system tick. - * @param mtrCtrl Motor control struct handle. - * @retval None. - */ -static void TimerTickInit(MTRCTRL_Handle *mtrCtrl) -{ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - mtrCtrl->sysTickCnt = 0; - mtrCtrl->msTickNum = US_PER_MS / SYSTICK_PERIOD_US; - mtrCtrl->capChargeTickNum = (INV_CAP_CHARGE_MS * US_PER_MS / SYSTICK_PERIOD_US); -} - -/** - * @brief Init motor control task. - * @retval None. - */ -static void TSK_Init(void) -{ - g_mc.motorStateFlag = 0; - g_mc.uartHeartDetCnt = 0; - g_mc.uartTimeStamp = 0; - g_mc.stateMachine = FSM_IDLE; - g_mc.currCtrlPeriod = CTRL_CURR_PERIOD; /* Init current controller */ - g_mc.aptMaxcntCmp = g_apt0.waveform.timerPeriod; - g_mc.sampleMode = DUAL_RESISTORS; - g_mc.obserType = FOC_OBSERVERTYPE_SMO4TH; /* Init foc observe mode */ - g_mc.controlMode = FOC_CONTROLMODE_SPEED; /* Init motor control mode */ - g_mc.adcCurrCofe = ADC_CURR_COFFI; - g_mc.spdAdjustMode = CUST_SPEED_ADJUST; - g_mc.uartConnectFlag = DISCONNECT; - g_mc.spdCmdHz = USER_MIN_SPD_HZ; /* Motor initialization speed */ - - g_mc.adc0Compensate = ADC0COMPENSATE; /* Phase-u current init adc shift trim value */ - g_mc.adc1Compensate = ADC1COMPENSATE; /* Phase-w current init adc shift trim value */ - - IF_Init(&g_mc.ifCtrl, CTRL_IF_CURR_AMP_A, USER_CURR_SLOPE, CTRL_SYSTICK_PERIOD, CTRL_CURR_PERIOD); - RMG_Init(&g_mc.spdRmg, CTRL_SYSTICK_PERIOD, USER_SPD_SLOPE); /* Init speed slope */ - MtrParamInit(&g_mc.mtrParam, g_motorParam); - - TimerTickInit(&g_mc); - if (g_mc.sampleMode == DUAL_RESISTORS) { - SVPWM_Init(&g_mc.sv, INV_VOLTAGE_BUS * ONE_DIV_SQRT3); /* Dual resistors SVPWM init. */ - } else if (g_mc.sampleMode == SINGLE_RESISTOR) { - /* Single resistor SVPWM init. */ - R1SVPWM_Init(&g_mc.r1Sv, INV_VOLTAGE_BUS * ONE_DIV_SQRT3, SAMPLE_POINT_SHIFT, SAMPLE_WINDOW_DUTY); - } - - SPDCTRL_InitWrapper(&g_mc.spdCtrl, CTRL_SYSTICK_PERIOD); - CURRCTRL_InitWrapper(&g_mc.currCtrl, &g_mc.idqRef, &g_mc.idqFbk, CTRL_CURR_PERIOD); - FOSMO_InitWrapper(&g_mc.smo, CTRL_CURR_PERIOD); - SMO4TH_InitWrapper(&g_mc.smo4th); - - STARTUP_Init(&g_mc.startup, USER_SWITCH_SPDBEGIN_HZ, USER_SWITCH_SPDBEGIN_HZ + TEMP_3); - - MotorProt_Init(&g_mc.prot); /* Init protect state comond */ - OCP_Init(&g_mc.prot.ocp, CTRL_CURR_PERIOD); - OVP_Init(&g_mc.prot.ovp, CTRL_SYSTICK_PERIOD); - LVP_Init(&g_mc.prot.lvp, CTRL_SYSTICK_PERIOD); - OTP_Init(&g_mc.prot.otp, CTRL_SYSTICK_PERIOD); - STP_Init(&g_mc.prot.stall, CTRL_SYSTICK_PERIOD, PROT_STALLING_CURR_AMP_LIMIT, - PROT_STALLING_SPD_LIMIT, PROT_STALLING_TIME_LIMIT); -} - -/** - * @brief Clear historical values of all controller before start-up. - * @param mtrCtrl The motor control handle. - * @retval None. - */ -static void ClearBeforeStartup(MTRCTRL_Handle *mtrCtrl) -{ - /* Verify Parameters */ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - /* The initial angle is 0. */ - mtrCtrl->axisAngle = 0; - - mtrCtrl->spdRefHz = 0.0f; - /* The initial dq-axis reference current is 0. */ - mtrCtrl->idqRef.d = 0.0f; - mtrCtrl->idqRef.q = 0.0f; - - mtrCtrl->vdqRef.d = 0.0f; - mtrCtrl->vdqRef.q = 0.0f; - /* Clear Duty Cycle Value. The initial duty cycle is 0.5. */ - mtrCtrl->dutyUvwLeft.u = 0.5f; - mtrCtrl->dutyUvwLeft.v = 0.5f; - mtrCtrl->dutyUvwLeft.w = 0.5f; - mtrCtrl->dutyUvwRight.u = 0.5f; - mtrCtrl->dutyUvwRight.v = 0.5f; - mtrCtrl->dutyUvwRight.w = 0.5f; - - mtrCtrl->prot.motorErrStatus.all = 0x00; - - RMG_Clear(&mtrCtrl->spdRmg); /* Clear the history value of speed slope control */ - CURRCTRL_Clear(&mtrCtrl->currCtrl); - IF_Clear(&mtrCtrl->ifCtrl); - SPDCTRL_Clear(&mtrCtrl->spdCtrl); - FOSMO_Clear(&mtrCtrl->smo); - SMO4TH_Clear(&mtrCtrl->smo4th); - STARTUP_Clear(&mtrCtrl->startup); - R1SVPWM_Clear(&mtrCtrl->r1Sv); - - OTP_Clear(&mtrCtrl->prot.otp); - OCP_Clear(&mtrCtrl->prot.ocp); - OVP_Clear(&mtrCtrl->prot.ovp); - LVP_Clear(&mtrCtrl->prot.lvp); -} - -/** - * @brief To set the comparison value of the IGBT single-resistance ADC sampling trigger position. - * @param aptx The APT register struct handle. - * @param cntCmpA A Count compare reference of time-base counter. - * @param cntCmpB B Count compare reference of time-base counter. - * @param maxCntCmp Maximum Comparison Value - * @retval None. - */ -static void MCS_SetAdcCompareR1(APT_RegStruct *aptx, unsigned short cntCmpA, - unsigned short cntCmpB, unsigned short maxCntCmp) -{ - unsigned short tmp; - /* Sets the A Count compare reference of time-base counter. */ - tmp = (unsigned short)Clamp((float)(cntCmpA), (float)(maxCntCmp - 1), 1.0f); - DCL_APT_SetCounterCompare(aptx, APT_COMPARE_REFERENCE_A, tmp); - /* Sets the B Count compare reference of time-base counter. */ - tmp = (unsigned short)Clamp((float)(cntCmpB), (float)(maxCntCmp - 1), 1.0f); - DCL_APT_SetCounterCompare(aptx, APT_COMPARE_REFERENCE_B, tmp); -} - -/** - * @brief Open the three-phase lower pipe. - * @param aptAddr Three-phase APT address pointer. - * @param maxDutyCnt Max duty count. - * @retval None. - */ -static void AptTurnOnLowSidePwm(APT_RegStruct **aptAddr, unsigned short maxDutyCnt) -{ - MCS_ASSERT_PARAM(aptAddr != NULL); - MCS_ASSERT_PARAM(maxDutyCnt != 0); - unsigned short dutyCnt; - dutyCnt = (unsigned short)(maxDutyCnt * APT_FULL_DUTY); - /* Open the three-phase lower pipe */ - for (unsigned int i = 0; i < PHASE_MAX_NUM; i++) { - APT_RegStruct *aptx = (APT_RegStruct *)(aptAddr[i]); - DCL_APT_SetCounterCompare(aptx, APT_COMPARE_REFERENCE_C, dutyCnt); - DCL_APT_SetCounterCompare(aptx, APT_COMPARE_REFERENCE_D, dutyCnt); - } -} - -/** - * @brief Enable three-phase pwm output. - * @param aptAddr Three-phase APT address pointer. - * @retval None. - */ -static void MotorPwmOutputEnable(APT_RegStruct **aptAddr) -{ - MCS_ASSERT_PARAM(aptAddr != NULL); - /* Enable three-phase pwm output */ - for (unsigned int i = 0; i < PHASE_MAX_NUM; i++) { - APT_RegStruct *aptx = (APT_RegStruct *)(aptAddr[i]); - aptx->PG_OUT_FRC.BIT.rg_pga_frc_en = BASE_CFG_UNSET; - aptx->PG_OUT_FRC.BIT.rg_pgb_frc_en = BASE_CFG_UNSET; - } -} - -/** - * @brief Disable three-phase pwm output. - * @param aptAddr Three-phase APT address pointer. - * @retval None. - */ -static void MotorPwmOutputDisable(APT_RegStruct **aptAddr) -{ - MCS_ASSERT_PARAM(aptAddr != NULL); - /* Disable three-phase pwm output. */ - for (unsigned int i = 0; i < PHASE_MAX_NUM; i++) { - APT_RegStruct *aptx = (APT_RegStruct *)(aptAddr[i]); - aptx->PG_OUT_FRC.BIT.rg_pga_frc_en = BASE_CFG_SET; - aptx->PG_OUT_FRC.BIT.rg_pgb_frc_en = BASE_CFG_SET; - DCL_APT_ForcePWMOutputLow(aptx); - } -} - -/** - * @brief Smo IF angle difference calculation. - * @param smoElecAngle Smo electrical angle. - * @param ifCtrlAngle IF control angle. - * @retval signed short angle difference. - */ -static float SmoIfAngleDiffCalc(float smoElecAngle, float ifCtrlAngle) -{ - float diff = AngleSub(smoElecAngle, ifCtrlAngle); - /* Smo IF angle difference calculation */ - return diff; -} - -/** - * @brief Construct a new mcs startupswitch object. - * @param mtrCtrl The motor control handle. - * @retval None. - */ -static void MCS_StartupSwitch(MTRCTRL_Handle *mtrCtrl) -{ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - STARTUP_Handle *startup = &mtrCtrl->startup; - DqAxis *idqRef = &mtrCtrl->idqRef; - float iftargetAmp = mtrCtrl->ifCtrl.targetAmp; - float spdRefHz = mtrCtrl->spdRefHz; - - switch (startup->stage) { - case STARTUP_STAGE_CURR: - if (mtrCtrl->ifCtrl.curAmp >= iftargetAmp) { - /* Stage change */ - idqRef->q = iftargetAmp; - startup->stage = STARTUP_STAGE_SPD; - } else { - /* current amplitude increase */ - idqRef->q = IF_CurrAmpCalc(&mtrCtrl->ifCtrl); - spdRefHz = 0.0f; - } - break; - case STARTUP_STAGE_SPD: - /* current frequency increase */ - if (Abs(spdRefHz) >= startup->spdBegin) { - /* Stage change */ - startup->stage = STARTUP_STAGE_SWITCH; - TrigVal localTrigVal; - TrigCalc(&localTrigVal, SmoIfAngleDiffCalc(mtrCtrl->smo.elecAngle, mtrCtrl->ifCtrl.angle)); - idqRef->d = 0.0f; - mtrCtrl->spdCtrl.spdPi.integral = iftargetAmp * localTrigVal.cos; - } else { - /* Speed rmg */ - spdRefHz = RMG_Exec(&mtrCtrl->spdRmg, mtrCtrl->spdCmdHz); - } - break; - - case STARTUP_STAGE_SWITCH: - /* Switch from IF to SMO */ - spdRefHz = RMG_Exec(&mtrCtrl->spdRmg, mtrCtrl->spdCmdHz); - idqRef->q = SPDCTRL_Exec(&mtrCtrl->spdCtrl, mtrCtrl->spdRefHz, mtrCtrl->smo.spdEst); - /* Transitional stage, if current reference speed > critical speed, change to next stage */ - if (spdRefHz >= startup->spdBegin + TEMP_3) { - /* Stage change */ - mtrCtrl->stateMachine = FSM_RUN; - } - break; - - default: - break; - } - mtrCtrl->spdRefHz = spdRefHz; -} - -/** - * @brief Pre-processing of motor status. - * @param statusReg System status. - * @param stateMachine Motor Control Status. - * @retval None. - */ -static void MotorStatePerProc(SysStatusReg *statusReg, volatile FsmState *stateMachine) -{ - MCS_ASSERT_PARAM(statusReg != NULL); - MCS_ASSERT_PARAM(stateMachine != NULL); - /* Get system status */ - if (SysIsError(statusReg)) { - *stateMachine = FSM_FAULT; - } - if (SysGetCmdStop(statusReg)) { - SysCmdStopClr(statusReg); - *stateMachine = FSM_STOP; - } -} - -/** - * @brief Check over current status. - * @param statusReg System status. - * @param stateMachine Motor Control Status. - * @retval None. - */ -static void CheckOverCurrentState(SysStatusReg *statusReg, FsmState *stateMachine) -{ - MCS_ASSERT_PARAM(statusReg != NULL); - MCS_ASSERT_PARAM(stateMachine != NULL); - /* check systerm error status */ - if (SysIsError(statusReg) == false) { - *stateMachine = FSM_IDLE; - } -} - -/** - * @brief Check bootstrap capacitor charge time. - * @param mtrCtrl The motor control handle. - * @param stateMachine Motor Control Status. - * @retval None. - */ -static void CheckBootstrpCapChargeTime(MTRCTRL_Handle *mtrCtrl, FsmState *stateMachine) -{ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - MCS_ASSERT_PARAM(stateMachine != NULL); - mtrCtrl->sysTickCnt++; - /* check bootstrap capacitor charge time */ - if (mtrCtrl->sysTickCnt == mtrCtrl->capChargeTickNum) { - *stateMachine = FSM_CLEAR; - } -} - -/** - * @brief Check systerm cmd start signal. - * @param mtrCtrl The motor control handle. - * @param aptAddr Three-phase APT address pointer. - * @param statusReg System status. - * @param stateMachine Motor Control Status. - * @retval None. - */ -static void CheckSysCmdStart(MTRCTRL_Handle *mtrCtrl, - APT_RegStruct **aptAddr, - SysStatusReg *statusReg, - FsmState *stateMachine) -{ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - MCS_ASSERT_PARAM(aptAddr != NULL); - MCS_ASSERT_PARAM(statusReg != NULL); - MCS_ASSERT_PARAM(stateMachine != NULL); - /* check start cmd */ - if (SysGetCmdStart(statusReg)) { - SysRunningSet(statusReg); - SysCmdStartClr(statusReg); - mtrCtrl->sysTickCnt = 0; - *stateMachine = FSM_CAP_CHARGE; - /* Preparation for charging the bootstrap capacitor. */ - AptTurnOnLowSidePwm(aptAddr, mtrCtrl->aptMaxcntCmp); - /* Out put pwm */ - MotorPwmOutputEnable(aptAddr); - } -} - -/** - * @brief System timer tick task. - * @param mtrCtrl The motor control handle. - * @param aptAddr Three-phase APT address pointer. - * @retval None. - */ -static void TSK_SystickIsr(MTRCTRL_Handle *mtrCtrl, APT_RegStruct **aptAddr) -{ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - MCS_ASSERT_PARAM(aptAddr != NULL); - SysStatusReg *statusReg = &mtrCtrl->statusReg; - FsmState *stateMachine = &mtrCtrl->stateMachine; - mtrCtrl->msTickCnt++; - /* Pre-processing of motor status. */ - MotorStatePerProc(statusReg, stateMachine); - /* statemachine */ - switch (*stateMachine) { - case FSM_IDLE: - /* Set smo estimate speed before motor start-up */ - g_mc.smo.spdEst = 0.0f; - CheckSysCmdStart(mtrCtrl, aptAddr, statusReg, stateMachine); - break; - case FSM_CAP_CHARGE: - /* Bootstrap Capacitor Charging Timing */ - CheckBootstrpCapChargeTime(mtrCtrl, stateMachine); - break; - /* Clear parameter before start */ - case FSM_CLEAR: - ClearBeforeStartup(mtrCtrl); - *stateMachine = FSM_STARTUP; - break; - case FSM_STARTUP: - MCS_StartupSwitch(mtrCtrl); - break; - case FSM_RUN: - /* Speed ramp control */ - mtrCtrl->spdRefHz = RMG_Exec(&mtrCtrl->spdRmg, mtrCtrl->spdCmdHz); - /* Speed loop control */ - mtrCtrl->idqRef.q = SPDCTRL_Exec(&mtrCtrl->spdCtrl, mtrCtrl->spdRefHz, mtrCtrl->smo.spdEst); - break; - case FSM_STOP: - mtrCtrl->spdRefHz = 0.0f; - MotorPwmOutputDisable(aptAddr); - SysRunningClr(statusReg); - *stateMachine = FSM_IDLE; - break; - case FSM_FAULT: /* Overcurrent state */ - CheckOverCurrentState(statusReg, stateMachine); - break; - default: - break; - } -} - -/** - * @brief Read the ADC initialize bias trim value. - * @param mtrCtrl The motor control handle. - * @retval None. - */ -static void TrimInitAdcShiftValue(MTRCTRL_Handle *mtrCtrl) -{ - float adc0SampleTemp = 0.0f; /* Current bias value for temp store */ - float adc1SampleTemp = 0.0f; - float adc0TempSum = 0.0f; - float adc1TempSum = 0.0f; /* Current bias sum value for 20 times */ - float adcSampleTimes = 0.0f; /* ADC sample times */ - for (int i = 0; i < ADC_READINIT_TIMES; i++) { - adc0SampleTemp = (float)HAL_ADC_GetConvResult(&ADCU_HANDLE, ADCUSOCNUM); - adc1SampleTemp = (float)HAL_ADC_GetConvResult(&ADCW_HANDLE, ADCWSOCNUM); - BASE_FUNC_DELAY_US(200); /* 200 is delay count, delay 200us triger adc sampling */ - if (adc0SampleTemp > 1000.0f && adc1SampleTemp > 1000.0f) { - adcSampleTimes++; - adc0TempSum += adc0SampleTemp; - adc1TempSum += adc1SampleTemp; - } - } - if (adcSampleTimes < 1.0f) { - adcSampleTimes = 1.0f; /* Prevent divide-by-zero errors */ - } - adc0SampleTemp = adc0TempSum / adcSampleTimes; - adc1SampleTemp = adc1TempSum / adcSampleTimes; - /* Force convert to float */ - mtrCtrl->adc0Compensate = (float) adc0SampleTemp; - mtrCtrl->adc1Compensate = (float) adc1SampleTemp; - /* The normal value scope: 1800 < adc0Compensate < 2200 */ - if(g_mc.adc0Compensate < ADC_TRIMVALUE_MIN || g_mc.adc0Compensate > ADC_TRIMVALUE_MAX \ - || g_mc.adc1Compensate < ADC_TRIMVALUE_MIN || g_mc.adc1Compensate > ADC_TRIMVALUE_MAX) { - DBG_PRINTF("ADC trim value error,please reset!"); - HAL_GPIO_SetValue(&LED2_HANDLE, LED2_PIN, GPIO_LOW_LEVEL); - } - adcSampleTimes = 0; - adc0TempSum = 0; - adc1TempSum = 0; -} - -/** - * @brief Read the ADC current sampling value. - * @param CurrUvw Three-phase current. - * @retval None. - */ -static void ReadCurrUvw(UvwAxis *CurrUvw) -{ - MCS_ASSERT_PARAM(CurrUvw != NULL); - float adc0 = (float)HAL_ADC_GetConvResult(&ADCU_HANDLE, ADCUSOCNUM); - float adc1 = (float)HAL_ADC_GetConvResult(&ADCW_HANDLE, ADCWSOCNUM); - /* Convert adc sample value to current value */ - CurrUvw->u = -(adc0 - g_mc.adc0Compensate) * g_mc.adcCurrCofe; - CurrUvw->w = -(adc1 - g_mc.adc1Compensate) * g_mc.adcCurrCofe; - CurrUvw->v = -CurrUvw->u - CurrUvw->w; -} - -/** - * @brief Setting the APT Output Duty Cycle. - * @param aptx APT register base address. - * @param leftDuty Left duty cycle. - * @param rightDuty Right duty cycle. - * @retval None. - */ -static void SetPwmDuty(APT_Handle *aptx, float leftDuty, float rightDuty) -{ - MCS_ASSERT_PARAM(aptx != NULL); - MCS_ASSERT_PARAM(leftDuty > 0); - MCS_ASSERT_PARAM(rightDuty > 0); - unsigned short maxPeriodCnt = aptx->waveform.timerPeriod; - unsigned short cntCmpLeftEdge = (unsigned short)(leftDuty * maxPeriodCnt); - unsigned short cntCmpRightEdge = (unsigned short)(rightDuty * maxPeriodCnt); - /* avoid overflowing */ - cntCmpLeftEdge = (cntCmpLeftEdge > maxPeriodCnt) ? maxPeriodCnt : cntCmpLeftEdge; - cntCmpRightEdge = (cntCmpRightEdge > maxPeriodCnt) ? maxPeriodCnt : cntCmpRightEdge; - HAL_APT_SetPWMDuty(aptx, cntCmpLeftEdge, cntCmpRightEdge); -} - -/** - * @brief Duty Cycle Setting. - * @param dutyUvwLeft Three-phase left duty cycle. - * @param dutyUvwRight Three-phase right duty cycle. - * @retval None. - */ -static void SetPwmDutyCp(UvwAxis *dutyUvwLeft, UvwAxis *dutyUvwRight) -{ - MCS_ASSERT_PARAM(dutyUvwLeft != NULL); - MCS_ASSERT_PARAM(dutyUvwRight != NULL); - /* Setting the Three-Phase Duty Cycle */ - SetPwmDuty(&g_apt0, dutyUvwLeft->u, dutyUvwRight->u); - SetPwmDuty(&g_apt1, dutyUvwLeft->v, dutyUvwRight->v); - SetPwmDuty(&g_apt2, dutyUvwLeft->w, dutyUvwRight->w); -} - -/** - * @brief To set the ADC sampling trigger comparison value. - * @param cntCmpSOCA Soca Compare Count Value. - * @param cntCmpSOCB Socb Compare Count Value. - * @retval None. - */ -static void SetADCTriggerTime(unsigned short cntCmpSOCA, unsigned short cntCmpSOCB) -{ - MCS_SetAdcCompareR1(g_apt[PHASE_U], cntCmpSOCA, cntCmpSOCB, g_mc.aptMaxcntCmp); -} - -/** - * @brief Temprature table, the temprature detect range is 15 ~ 60 degree. - * @param tempResisValue Temperature sensor resistance. - * @retval None. - */ -static float TempTable(float tempResisValue) -{ - float boardTemp = 0.0f; - /* Temperatures between 15 and 30. */ - if (tempResisValue > TEMP_RES_30 && tempResisValue <= TEMP_RES_15) { - boardTemp = TEMP_15 + (TEMP_30 - TEMP_15) * (TEMP_RES_15 - tempResisValue) / (TEMP_RES_15 - TEMP_RES_30); - } else if (tempResisValue > TEMP_RES_45 && tempResisValue <= TEMP_RES_30) { /* Temperature between 30 and 45. */ - boardTemp = TEMP_30 + (TEMP_45 - TEMP_30) * (TEMP_RES_30 - tempResisValue) / (TEMP_RES_30 - TEMP_RES_45); - } else if (tempResisValue > TEMP_RES_60 && tempResisValue <= TEMP_RES_45) { /* Temperature between 45 and 50. */ - boardTemp = TEMP_45 + (TEMP_60 - TEMP_45) * (TEMP_RES_45 - tempResisValue) / (TEMP_RES_45 - TEMP_RES_60); - } else if (tempResisValue <= TEMP_RES_60) { - boardTemp = TEMP_60; /* If temperature is over 60, set temperature as 60. */ - } else if (tempResisValue >= TEMP_RES_15) { - boardTemp = TEMP_15; /* If temperature is lower 15, set temperature as 15. */ - } - return boardTemp; -} - -/** - * @brief Read power board temperature and udc. - * @retval None. - */ -static void ReadBoardTempAndUdc(void) -{ - HAL_ADC_SoftTrigSample(&ADCRESIS_HANDLE, ADCRESISSOCNUM); - HAL_ADC_SoftTrigSample(&ADCUDC_HANDLE, ADCUDCSOCNUM); - BASE_FUNC_DELAY_US(CNT_10); /* Delay 10 us. */ - /* Force convert to float type. */ - float resisAdcValue = (float)HAL_ADC_GetConvResult(&ADCRESIS_HANDLE, ADCRESISSOCNUM); - /* 10 / (x + 10) * 3.3 = resisAdcValue / 4096 * 3.3, x is resisValue, 10kohm is resistor divider value. */ - float resisValue = (4096.0f * 10.0f - 10.0f * resisAdcValue) / resisAdcValue; - g_mc.powerBoardTemp = TempTable(resisValue); - g_mc.udc = ((float)HAL_ADC_GetConvResult(&ADCUDC_HANDLE, ADCUDCSOCNUM)) * ADC_UDC_COFFI; -} - -/** - * @brief Execut abnormal feedback speed protect motion. - * @retval None. - */ -static void SpdFbkErrorProt_Exec(void) -{ - if (g_mc.prot.motorErrStatus.Bit.motorStalling == 0 && - g_mc.prot.motorErrStatus.Bit.overVoltErr == 0 && - g_mc.prot.motorErrStatus.Bit.lowerVoltErr == 0 && - g_mc.prot.motorErrStatus.Bit.overIpmTempErr == 0 && - g_mc.prot.motorErrStatus.Bit.overCurrErr == 0) { - g_mc.prot.motorErrStatus.Bit.revRotErr = 1; - /* If revRotErr, execute protect motion. */ - ProtSpo_Exec(g_apt); - } -} - -/** - * @brief Execut nan data protect motion. - * @retval None. - */ -static void NanDataDetect(void) -{ - static short errorSpdStatus = 0; - /* Detect the nan observer speed or current value. */ - if (isnan(g_mc.smo.spdEst) || isnan(g_mc.idqRef.q)) { - errorSpdStatus++; - } else { - errorSpdStatus = 0; - } - /* If the data is nan & continuous counting value is over 500 times, execute protect motion. - the detect time is 500 * 500us = 250ms. */ - if (errorSpdStatus >= 500) { - errorSpdStatus = 0; - SpdFbkErrorProt_Exec(); - } -} - -/** - * @brief Check abnormal feedback speed. - * @retval None. - */ -static void CheckSpdFbkStatus(void) -{ - static short errorCurrStatus = 0; - static short errorDeltaSpdStatus = 0; - NanDataDetect(); - if (g_mc.stateMachine == FSM_RUN) { - /* Detect the abnormal idq feedback current. */ - if (Abs(g_mc.idqRef.q - g_mc.idqFbk.q) >= CTRL_IF_CURR_AMP_A) { - errorCurrStatus++; - } else { - errorCurrStatus = 0; - } - /* Detect the abnormal feedback speed, the normal speed is > 0, if smo.spdEst < -10 && - delta speed error > USER_MIN_SPD_HZ + 10.0f at FSM_RUN stage, set the motor motion as error */ - if (g_mc.smo.spdEst < -10.0f && (g_mc.spdRefHz - g_mc.smo.spdEst > USER_MIN_SPD_HZ + 10.0f)) { - errorDeltaSpdStatus++; - } - } - /* Execute protect motion if count over 500 times, this error status caused by abnormal speed - or cabnormal urrent feedback, the detect time is 500 * 500us = 250ms. */ - if (errorCurrStatus >= 500) { - errorCurrStatus = 0; - SpdFbkErrorProt_Exec(); - } - /* This error statu caused by motor stalling, the detect time is 2 * 500us = 1ms. */ - if (errorDeltaSpdStatus >= 2) { - errorDeltaSpdStatus = 0; - g_mc.prot.motorErrStatus.Bit.motorStalling = 1; - } -} - -/** - * @brief Check Potentiometer Value callback function. - * @param param The TIMER_Handle. - * @retval None. - */ -void CheckPotentiometerValueCallback(void *param) -{ - MCS_ASSERT_PARAM(param != NULL); - BASE_FUNC_UNUSED(param); - static float potentiomitorAdcValue = 0.0f; - static float spdCmdHz = 0; - static float spdCmdHzLast = USER_MIN_SPD_HZ; /* 35.0 is spdCmdHzLast init value */ - HAL_ADC_SoftTrigSample(&ADCPTT_HANDLE, ADCPTTSOCNUM); - BASE_FUNC_DELAY_US(10); /* Delay 10 us. */ - potentiomitorAdcValue = (float)HAL_ADC_GetConvResult(&ADCPTT_HANDLE, ADCPTTSOCNUM); - /* 4045.0 is adc sample max value of potentiomitor, convert max spd to 180.25Hz */ - spdCmdHz = potentiomitorAdcValue / 4045.0f * USER_MAX_SPD_HZ; - if (Abs(spdCmdHzLast - spdCmdHz) < 1.0f) { /* Ignore changes less than 1. */ - return; - } - spdCmdHzLast = spdCmdHz; - if (spdCmdHz < USER_MIN_SPD_HZ) { /* 35.0 is spdCmdHz lower limit */ - spdCmdHz = USER_MIN_SPD_HZ; /* 35.0 is spdCmdHz lower limit */ - } - if (spdCmdHz > g_mc.mtrParam.maxElecSpd) { /* spdCmdHz upper limit */ - spdCmdHz = g_mc.mtrParam.maxElecSpd; /* spdCmdHz upper limit */ - } - if (g_mc.spdAdjustMode == CUST_SPEED_ADJUST) { - g_mc.spdCmdHz = spdCmdHz; - } -} - -/** - * @brief System timer ISR for Motor Statemachine CallBack function. - * @param param The systick timer handle. - * @retval None. - */ -void MotorStatemachineCallBack(void *param) -{ - /* Verify Parameters */ - MCS_ASSERT_PARAM(param != NULL); - BASE_FUNC_UNUSED(param); - /* Read power board temprature and voltage. */ - ReadBoardTempAndUdc(); - /* Motor speed loop state machine. */ - TSK_SystickIsr(&g_mc, g_apt); - - /* Motor error speed feedback check. */ - CheckSpdFbkStatus(); - /* Motor stalling detect. */ - STP_Det_ByCurrSpd(&g_mc.prot.stall, &g_mc.prot.motorErrStatus, g_mc.smo.spdEst, g_mc.idqFbk); - STP_Exec(&g_mc.prot.motorErrStatus, g_apt); - - /* Motor over voltage detect. */ - OVP_Det(&g_mc.prot.ovp, &g_mc.prot.motorErrStatus, g_mc.udc); - OVP_Exec(&g_mc.prot.ovp, &g_mc.spdRefHz, g_apt); - OVP_Recy(&g_mc.prot.ovp, &g_mc.prot.motorErrStatus, g_mc.udc); - /* Motor lower voltage detect. */ - LVP_Det(&g_mc.prot.lvp, &g_mc.prot.motorErrStatus, g_mc.udc); - LVP_Exec(&g_mc.prot.lvp, &g_mc.spdRefHz, g_apt); - LVP_Recy(&g_mc.prot.lvp, &g_mc.prot.motorErrStatus, g_mc.udc); - /* Power board over temperature detect. */ - OTP_Det(&g_mc.prot.otp, &g_mc.prot.motorErrStatus, OTP_IPM_ERR_BIT, g_mc.powerBoardTemp); - OTP_Exec(&g_mc.prot.otp, &g_mc.spdRefHz, g_apt); - OTP_Recy(&g_mc.prot.otp, &g_mc.prot.motorErrStatus, OTP_IPM_ERR_BIT, g_mc.powerBoardTemp); - - /* If protect level == 4, set motor state as stop. */ - if (g_mc.prot.ovp.protLevel == LEVEL_4 || g_mc.prot.lvp.protLevel == LEVEL_4 \ - || g_mc.prot.otp.protLevel == LEVEL_4) { - SysCmdStopSet(&g_mc.statusReg); - } -} - -/** - * @brief The carrier ISR wrapper function. - * @param aptHandle The APT handle. - * @retval None. - */ -void MotorCarrierProcessCallback(void *aptHandle) -{ - MCS_ASSERT_PARAM(aptHandle != NULL); - BASE_FUNC_UNUSED(aptHandle); - /* the carrierprocess of motor */ - MCS_CarrierProcess(&g_mc); - /* Over current protect */ - if (g_mc.stateMachine == FSM_RUN || g_mc.stateMachine == FSM_STARTUP) { - OCP_Det(&g_mc.prot.ocp, &g_mc.prot.motorErrStatus, g_mc.idqFbk); - OCP_Exec(&g_mc.prot.ocp, &g_mc.idqFbk, g_apt); /* Execute over current protect motion */ - if (g_mc.prot.ocp.protLevel < LEVEL_4) { - OCP_Recy(&g_mc.prot.ocp, &g_mc.prot.motorErrStatus); - } - } -} - -/** - * @brief Event interrupt callback function of APT module. - * @param para APT module handle. - * @retval None. - */ -void MotorSysErrCallback(void *para) -{ - MCS_ASSERT_PARAM(para != NULL); - APT_Handle *handle = (APT_Handle *)para; - /* The IPM overcurrent triggers and disables the three-phase PWM output. */ - MotorPwmOutputDisable(g_apt); - DCL_APT_ClearOutCtrlEventFlag((APT_RegStruct *)g_apt[PHASE_U], APT_OC_COMBINE_EVENT_A1); - DCL_APT_ClearOutCtrlEventFlag((APT_RegStruct *)g_apt[PHASE_V], APT_OC_COMBINE_EVENT_A1); - DCL_APT_ClearOutCtrlEventFlag((APT_RegStruct *)g_apt[PHASE_W], APT_OC_COMBINE_EVENT_A1); - /* Status setting error */ - SysErrorSet(&g_mc.statusReg); - DBG_PRINTF("APT error! \r\n"); - HAL_GPIO_SetValue(&LED2_HANDLE, LED2_PIN, GPIO_LOW_LEVEL); - BASE_FUNC_UNUSED(handle); -} - -/** - * @brief Init motor controller's data structure. - * @retval None. - */ -static void InitSoftware(void) -{ - /* Initializing motor control param */ - TSK_Init(); - /* Read phase-uvw current */ - g_mc.readCurrUvwCb = ReadCurrUvw; - g_mc.setPwmDutyCb = SetPwmDutyCp; - g_mc.setADCTriggerTimeCb = SetADCTriggerTime; -} - -/** - * @brief Config the KEY func. - * @param handle The GPIO handle. - * @retval None. - */ -static KEY_State Key_StateRead(GPIO_Handle *handle) -{ - if (HAL_GPIO_GetPinValue(handle, handle->pins) == 0) { - BASE_FUNC_DELAY_MS(30); /* delay 30ms for deshake */ - if (HAL_GPIO_GetPinValue(handle, handle->pins) == 0) { - while (HAL_GPIO_GetPinValue(handle, handle->pins) == 0) { - } - return KEY_DOWN; - } - } - return KEY_UP; -} - -/** - * @brief Control motor start and stop state by key func. - * @param param The GPIO handle. - * @retval None. - */ -void MotorStartStopKeyCallback(void *param) -{ - GPIO_Handle *handle = (GPIO_Handle *)param; - if (Key_StateRead(handle) == KEY_DOWN) { - if (g_mc.motorStateFlag == 0) { /* start motor */ - g_mc.motorStateFlag = 1; - SysCmdStartSet(&g_mc.statusReg); - } else if (g_mc.motorStateFlag == 1) { /* stop motor */ - g_mc.motorStateFlag = 0; - SysCmdStopSet(&g_mc.statusReg); - } - } -} - -/** - * @brief User application main entry function. - * @retval BSP_OK. - */ -int MotorMainProcess(void) -{ - unsigned int tickNum1Ms = 2; /* 1ms tick */ - static unsigned int tickCnt1Ms = 0; - unsigned int tickNum500Ms = 1000; /* 500ms tick */ - static unsigned int tickCnt500Ms = 0; - SystemInit(); - HMI_Init(); /* Init uart interrupt */ - HAL_TIMER_Start(&g_timer0); - HAL_TIMER_Start(&g_timer1); - - /* Disable PWM output before startup. */ - MotorPwmOutputDisable(g_apt); - /* Software initialization. */ - InitSoftware(); - /* Start the PWM clock. */ - HAL_APT_StartModule(RUN_APT0 | RUN_APT1 | RUN_APT2); - /* System Timer clock. */ - BASE_FUNC_DELAY_MS(ADC_READINIT_DELAY); - TrimInitAdcShiftValue(&g_mc); - BASE_FUNC_DELAY_MS(MOTOR_START_DELAY); - while (1) { - /* Cycling send data to host */ - HMI_Process_Tx(&g_mc); - if (g_mc.msTickCnt - tickCnt1Ms >= tickNum1Ms) { - tickCnt1Ms = g_mc.msTickCnt; - /* User Code 1ms Event */ - HMI_Process_Rx(&g_mc); - /* User Code 1ms Event */ - } - - if (g_mc.msTickCnt - tickCnt500Ms >= tickNum500Ms) { - if (SysIsError(&g_mc.statusReg) != true) { - /* LED toggle in normal status. */ - HAL_GPIO_TogglePin(&LED1_HANDLE, LED1_PIN); - } - tickCnt500Ms = g_mc.msTickCnt; - } - } - return 0; -} diff --git a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/user_interface/cust_process.c b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/user_interface/cust_process.c deleted file mode 100644 index 7c4f7525..00000000 --- a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/user_interface/cust_process.c +++ /dev/null @@ -1,718 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file cust_process.c - * @author MCU Algorithm Team - * @brief This file provides functions declaration of cust process interface. - */ - -#include "cust_process.h" -#include "mcs_ctlmode_config.h" -#include "mcs_math_const.h" -#include "mcs_user_config.h" -#include "mcs_assert.h" -#include "main.h" -#include "uart_module.h" -#include "mcs_mtr_param.h" -/* Macro definitions --------------------------------------------------------------------------- */ -/* Constant value. */ -#define CONST_VALUE_60 60.0f /* Constant value 60. */ -#define CONST_VALUE_DIV_1000 0.001f /* Constant value 1/1000. */ - -/* Data array index. */ -#define DATA_SEGMENT_ONE 0 /* First element of the data segment */ -#define DATA_SEGMENT_TWO 1 /* Second element of the data segment */ -#define DATA_SEGMENT_THREE 2 /* Third element of the data segment */ -#define DATA_SEGMENT_FOUR 3 /* Fourth element of the data segment */ -#define CUSTACKCODELEN 10 /* Ack code length */ - -/* Command code. */ -#define SET_PID_KP 0x01 /* Set Pid Kp Command Params */ -#define SET_PID_KI 0x02 /* Set Pid Ki Command Params */ -#define SET_PID_LIMIT 0x03 /* Set Pid limit Command Params */ - -#define SET_SMO1TH_PLL_BDW 0x01 /* Set Smo1th Pll BandWidth Command Params */ -#define SET_SMO1TH_SPDFLITER_FC 0x02 /* Set Smo1th Fc Command Params */ -#define SET_SMO1TH_FILCOMPANGLE 0x03 /* Set Smo1th FillComp Command Params */ - -#define SET_SMO4TH_KD 0x01 /* Set Smo4th Kd Command Params */ -#define SET_SMO4TH_KP 0x02 /* Set Smo4th Kq Command Params */ - -#define SET_SPD_COMMAND_HZ 0x01 /* Set Target Speed Command Params */ -#define SET_SPD_RMG_SLOPE 0x02 /* Set Speed Slope Command Params */ - -#define SET_MAX_ELEC_SPD 0x01 /* Set Max Motor Speed Command Params */ -#define SET_MOTOR_NUM_OF_PAIRS 0x02 /* Set Motor Command Params */ - -#define SET_MOTOR_RES_OF_STATOR 0x01 /* Set Motor Res Command Params */ -#define SET_MOTOR_DAXIS_INDUCTANCE 0x02 /* Set Motor Daxis Inductance Command Params */ -#define SET_MOTOR_QAXIS_INDUCTANCE 0x03 /* Set Motor Qaxis Inductance Command Params */ - -#define SET_SVPWM_VOLTAGE_PER_UNIT 0x01 /* Set Svpwm Voltage Params */ -#define SET_ADC_CURRENT_SAMPLE_COFE 0x02 /* Set Adc coffe Params */ -#define SET_CURRENT_LOOP_CONTROL_PERIOD 0x03 /* Set Current loop period Command Params */ - -#define SET_IF_TARGET_CURRENT_VALUE 0x01 /* Set If Target Params */ -#define SET_INCREMENT_OF_IF_CURRENT 0x02 /* Set If Step Command Params */ -#define SET_SPEED_RING_BEGIN_SPEED 0x03 /* Set If to Smo Start Speed Command Params */ - -static unsigned char ackCode = 0; -static unsigned char g_uartTxBuf[CUSTACKCODELEN] = {0}; - -/** - * @brief Set observer type. - * @param mtrCtrl The motor control handle. - * @param rxData Receive buffer - */ -static void CMDCODE_SetObserverType(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) -{ - /* Get function code. */ - int funcCode = (int)(rxData->data[DATA_SEGMENT_ONE].typeF); - if (funcCode == FOC_OBSERVERTYPE_SMO1TH) { - ackCode = 0X01; - mtrCtrl->obserType = FOC_OBSERVERTYPE_SMO1TH; - CUST_AckCode(g_uartTxBuf, ackCode, mtrCtrl->obserType); - } else if (funcCode == FOC_OBSERVERTYPE_SMO4TH) { - ackCode = 0X02; - mtrCtrl->obserType = FOC_OBSERVERTYPE_SMO4TH; - CUST_AckCode(g_uartTxBuf, ackCode, mtrCtrl->obserType); - } -} - -/** - * @brief Set pid parameter ack code. - * @param funcCode Received data funccode. - */ -static unsigned char SetPidAckCode(int funcCode) -{ - switch (funcCode) { - /* Set current loop D-Axis PID parameter ack code. */ - case FOC_CURDAXISPID_PARAMS: - ackCode = 0xE0; - break; - /* Set current loop Q-Axis PID parameter ack code. */ - case FOC_CURQAXISPID_PARAMS: - ackCode = 0xE3; - break; - /* Set speed loop PID parameter ack code. */ - case FOC_SPDPID_PARAMS: - ackCode = 0xE6; - break; - default: - break; - } - return ackCode; -} - -/** - * @brief Set pid parameters. - * @param pidHandle The pid control handle. - * @param rxData Receive buffer - */ -static void SetPidParams(PID_Handle *pidHandle, CUSTDATATYPE_DEF *rxData) -{ - /* Get function code. */ - int funcCode = (int)(rxData->data[DATA_SEGMENT_ONE].typeF); - /* Get command code. */ - int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); - switch (cmdCode) { - case SET_PID_KP: /* Set the P parameter. */ - PID_SetKp(pidHandle, rxData->data[DATA_SEGMENT_THREE].typeF); - ackCode = SetPidAckCode(funcCode); - CUST_AckCode(g_uartTxBuf, (unsigned char)(ackCode + SET_PID_KP), rxData->data[DATA_SEGMENT_THREE].typeF); - break; - case SET_PID_KI: /* Set the I parameter. */ - PID_SetKi(pidHandle, rxData->data[DATA_SEGMENT_THREE].typeF); - ackCode = SetPidAckCode(funcCode); - CUST_AckCode(g_uartTxBuf, (unsigned char)(ackCode + SET_PID_KI), rxData->data[DATA_SEGMENT_THREE].typeF); - break; - case SET_PID_LIMIT: /* Set the pid limit. */ - PID_SetLimit(pidHandle, rxData->data[DATA_SEGMENT_THREE].typeF); - ackCode = SetPidAckCode(funcCode); - CUST_AckCode(g_uartTxBuf, (unsigned char)(ackCode + SET_PID_LIMIT), rxData->data[DATA_SEGMENT_THREE].typeF); - break; - default: - ackCode = 0X77; - CUST_AckCode(g_uartTxBuf, ackCode, 0); - break; - } -} - -/** - * @brief Set motor pid parameters. - * @param mtrCtrl The motor control handle. - * @param rxData Receive buffer - */ -static void CMDCODE_SetMotorPidParams(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) -{ - /* Get function code. */ - int funcCode = (int)(rxData->data[DATA_SEGMENT_ONE].typeF); - - if (funcCode == FOC_CURDAXISPID_PARAMS) { - SetPidParams(&mtrCtrl->currCtrl.dAxisPi, rxData); /* Set Curr loop Daxis pid params */ - } else if (funcCode == FOC_CURQAXISPID_PARAMS) { - SetPidParams(&mtrCtrl->currCtrl.qAxisPi, rxData); /* Set Curr loop Qaxis pid params */ - mtrCtrl->currCtrl.dAxisPi.upperLimit = mtrCtrl->currCtrl.qAxisPi.upperLimit; - mtrCtrl->currCtrl.dAxisPi.lowerLimit = mtrCtrl->currCtrl.qAxisPi.lowerLimit; - } else if (funcCode == FOC_SPDPID_PARAMS) { - SetPidParams(&mtrCtrl->spdCtrl.spdPi, rxData); /* Set Speed loop params */ - } -} - -/** - * @brief Set first order sliding mode observer parameters. - * @param smoHandle The observer control handle. - * @param rxData Receive buffer - */ -static void SetObserverSmo1thParams(FOSMO_Handle *smoHandle, CUSTDATATYPE_DEF *rxData) -{ - smoHandle->kSmo = rxData->data[DATA_SEGMENT_TWO].typeF; - ackCode = 0X09; - CUST_AckCode(g_uartTxBuf, ackCode, smoHandle->kSmo); -} - -/** - * @brief Set first order sliding mode observer's phase-locked loop parameters. - * @param smoHandle The observer control handle. - * @param rxData Receive buffer - */ -static void SetObserverSmo1thPLLParams(FOSMO_Handle *smoHandle, CUSTDATATYPE_DEF *rxData) -{ - /* Get command code. */ - int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); - float pllBdw = 0.0f; - switch (cmdCode) { - case SET_SMO1TH_PLL_BDW: /* Set the bandwidth. */ - pllBdw = rxData->data[DATA_SEGMENT_THREE].typeF; - smoHandle->pll.pi.kp = 2.0f * pllBdw; /* kp = 2.0f * pllBdw */ - smoHandle->pll.pi.ki = pllBdw * pllBdw; /* ki = pllBdw * pllBdw */ - ackCode = 0X0A; - CUST_AckCode(g_uartTxBuf, ackCode, pllBdw); - break; - case SET_SMO1TH_SPDFLITER_FC: /* Set the cutoff frequency. */ - smoHandle->spdFilter.fc = rxData->data[DATA_SEGMENT_THREE].typeF; - smoHandle->spdFilter.a1 = 1.0f / (1.0f + DOUBLE_PI * smoHandle->spdFilter.fc * CTRL_CURR_PERIOD); - smoHandle->spdFilter.b1 = 1.0f - smoHandle->spdFilter.a1; - ackCode = 0X0B; - CUST_AckCode(g_uartTxBuf, ackCode, smoHandle->spdFilter.fc); - break; - case SET_SMO1TH_FILCOMPANGLE: /* Set the compensation angle. */ - smoHandle->filCompAngle = rxData->data[DATA_SEGMENT_THREE].typeF; - ackCode = 0X0C; - CUST_AckCode(g_uartTxBuf, ackCode, smoHandle->filCompAngle); - break; - default: - ackCode = 0X77; - CUST_AckCode(g_uartTxBuf, ackCode, 0); - break; - } -} - -/** - * @brief Set fourth order sliding mode observer parameters. - * @param smo4thHandle The observer control handle. - * @param rxData Receive buffer - */ -static void SetObserverSmo4thParams(SMO4TH_Handle *smo4thHandle, CUSTDATATYPE_DEF *rxData) -{ - /* Get command code. */ - int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); - - switch (cmdCode) { - case SET_SMO4TH_KD: /* Set d axis gain. */ - smo4thHandle->kd = rxData->data[DATA_SEGMENT_THREE].typeF; - ackCode = 0X0D; - CUST_AckCode(g_uartTxBuf, ackCode, smo4thHandle->kd); - break; - case SET_SMO4TH_KP: /* Set q axis gain. */ - smo4thHandle->kq = rxData->data[DATA_SEGMENT_THREE].typeF; - ackCode = 0X0E; - CUST_AckCode(g_uartTxBuf, ackCode, smo4thHandle->kq); - break; - default: - break; - } -} - -/** - * @brief Set fourth order sliding mode observer's phase-locked loop parameters. - * @param smo4thHandle The observer control handle. - * @param rxData Receive buffer - */ -static void SetObserverSmo4thPLLParams(SMO4TH_Handle *smo4thHandle, CUSTDATATYPE_DEF *rxData) -{ - float pllBdw = rxData->data[DATA_SEGMENT_TWO].typeF; - smo4thHandle->pll.pi.kp = (2.0f) * pllBdw; /* kp = 2.0f * pllBdw */ - smo4thHandle->pll.pi.ki = pllBdw * pllBdw; - ackCode = 0X11; - CUST_AckCode(g_uartTxBuf, ackCode, pllBdw); -} - -/** - * @brief Set observer parameters. - * @param mtrCtrl The motor control handle. - * @param rxData Receive buffer - */ -static void CMDCODE_SetObserverParams(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) -{ - /* Get function code. */ - int funcCode = (int)(rxData->data[DATA_SEGMENT_ONE].typeF); - - if (funcCode == FOC_OBSERVERTYPE_SMO1TH) { - SetObserverSmo1thParams(&mtrCtrl->smo, rxData); - } else if (funcCode == FOC_OBSERVERTYPE_SMO1TH_PLL) { - SetObserverSmo1thPLLParams(&mtrCtrl->smo, rxData); - } else if (funcCode == FOC_OBSERVERTYPE_SMO4TH) { - SetObserverSmo4thParams(&mtrCtrl->smo4th, rxData); - } else if (funcCode == FOC_OBSERVERTYPE_SMO4TH_PLL) { - SetObserverSmo4thPLLParams(&mtrCtrl->smo4th, rxData); - } -} - -/** - * @brief Set motor speed and speed slope. - * @param mtrCtrl The motor control handle. - * @param rxData Receive buffer - */ -static void CMDCODE_SetMotorSpdAndSlope(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) -{ - /* Get command code. */ - int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); - - switch (cmdCode) { - case SET_SPD_COMMAND_HZ: /* Set target speed(hz). */ - mtrCtrl->spdCmdHz = rxData->data[DATA_SEGMENT_THREE].typeF * mtrCtrl->mtrParam.mtrNp / CONST_VALUE_60; - /* Judgement the value > 0.00001, make sure denominator != 0. */ - if (rxData->data[DATA_SEGMENT_FOUR].typeF > 0.00001f) { - mtrCtrl->spdRmg.delta = mtrCtrl->spdCmdHz / rxData->data[DATA_SEGMENT_FOUR].typeF * CTRL_SYSTICK_PERIOD; - } - ackCode = 0X16; - CUST_AckCode(g_uartTxBuf, ackCode, mtrCtrl->spdCmdHz); - break; - case SET_SPD_RMG_SLOPE: /* Set speed slope. */ - mtrCtrl->spdRmg.delta = mtrCtrl->spdCmdHz / rxData->data[DATA_SEGMENT_THREE].typeF * CTRL_SYSTICK_PERIOD; - ackCode = 0X17; - CUST_AckCode(g_uartTxBuf, ackCode, rxData->data[DATA_SEGMENT_THREE].typeF); - break; - default: - break; - } -} - -/** - * @brief Set motor base parameters. - * @param mtrParamHandle The motor parameters handle. - * @param rxData Receive buffer - */ -static void SetMotorBaseParams(MOTOR_Param *mtrParamHandle, CUSTDATATYPE_DEF *rxData) -{ - /* Get command code. */ - int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); - - switch (cmdCode) { - case SET_MAX_ELEC_SPD: /* Set max electric speed. */ - mtrParamHandle->maxElecSpd = rxData->data[DATA_SEGMENT_THREE].typeF / - CONST_VALUE_60 * mtrParamHandle->mtrNp; - ackCode = 0X18; - CUST_AckCode(g_uartTxBuf, ackCode, rxData->data[DATA_SEGMENT_THREE].typeF); - break; - case SET_MOTOR_NUM_OF_PAIRS: /* Set the number of motor pole pairs. */ - mtrParamHandle->mtrNp = (unsigned short)(rxData->data[DATA_SEGMENT_THREE].typeF); - ackCode = 0X19; - CUST_AckCode(g_uartTxBuf, ackCode, (float)mtrParamHandle->mtrNp); - break; - default: - break; - } -} - -/** - * @brief Set motor special parameters. - * @param mtrParamHandle The motor parameters handle. - * @param rxData Receive buffer - */ -static void SetMotorSpecialParams(MOTOR_Param *mtrParamHandle, CUSTDATATYPE_DEF *rxData) -{ - /* Get command code. */ - int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); - - switch (cmdCode) { - case SET_MOTOR_RES_OF_STATOR: /* Set the resistor of stator. */ - mtrParamHandle->mtrRs = rxData->data[DATA_SEGMENT_THREE].typeF; - ackCode = 0X1A; - CUST_AckCode(g_uartTxBuf, ackCode, mtrParamHandle->mtrRs); - break; - case SET_MOTOR_DAXIS_INDUCTANCE: /* Set the d axis inductance. */ - mtrParamHandle->mtrLd = rxData->data[DATA_SEGMENT_THREE].typeF; - ackCode = 0X1B; - CUST_AckCode(g_uartTxBuf, ackCode, mtrParamHandle->mtrLd); - break; - case SET_MOTOR_QAXIS_INDUCTANCE: /* Set the q axis inductance. */ - mtrParamHandle->mtrLq = rxData->data[DATA_SEGMENT_THREE].typeF; - ackCode = 0X1C; - CUST_AckCode(g_uartTxBuf, ackCode, mtrParamHandle->mtrLq); - break; - default: - ackCode = 0X77; - CUST_AckCode(g_uartTxBuf, ackCode, 0); - break; - } -} - -/** - * @brief Set motor board parameters. - * @param mtrCtrl The motor control handle. - * @param rxData Receive buffer - */ -static void SetMotorBoardParams(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) -{ - /* Get command code. */ - int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); - - switch (cmdCode) { - case SET_SVPWM_VOLTAGE_PER_UNIT: /* Set svpwm voltage per unit. */ - mtrCtrl->sv.voltPu = rxData->data[DATA_SEGMENT_THREE].typeF * ONE_DIV_SQRT3; - mtrCtrl->currCtrl.outLimit = mtrCtrl->sv.voltPu * ONE_DIV_SQRT3; - ackCode = 0X1D; - CUST_AckCode(g_uartTxBuf, ackCode, rxData->data[DATA_SEGMENT_THREE].typeF); - break; - case SET_ADC_CURRENT_SAMPLE_COFE: /* Set adc current sample cofeature. */ - mtrCtrl->adcCurrCofe = rxData->data[DATA_SEGMENT_THREE].typeF; - ackCode = 0X1E; - CUST_AckCode(g_uartTxBuf, ackCode, mtrCtrl->adcCurrCofe); - break; - case SET_CURRENT_LOOP_CONTROL_PERIOD: /* Set current loop control period. */ - mtrCtrl->currCtrlPeriod = 1 / rxData->data[DATA_SEGMENT_THREE].typeF * CONST_VALUE_DIV_1000; - ackCode = 0X1F; - CUST_AckCode(g_uartTxBuf, ackCode, rxData->data[DATA_SEGMENT_THREE].typeF); - break; - default: - break; - } -} - -/** - * @brief Set motor parameters. - * @param mtrCtrl The motor control handle. - * @param rxData Receive buffer - */ -static void CMDCODE_SetMotorParams(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) -{ - /* Get function code. */ - int funcCode = (int)(rxData->data[DATA_SEGMENT_ONE].typeF); - - if (funcCode == MOTOR_PARAMS_BASE) { - SetMotorBaseParams(&mtrCtrl->mtrParam, rxData); - } else if (funcCode == MOTOR_PARAMS_SPECIAL) { - SetMotorSpecialParams(&mtrCtrl->mtrParam, rxData); - } else if (funcCode == MOTOR_PARAMS_BOARD) { - SetMotorBoardParams(mtrCtrl, rxData); - } -} - -/** - * @brief Motor start. - * @param mtrCtrl The motor control handle. - */ -static void CMDCODE_MotorStart(MTRCTRL_Handle *mtrCtrl) -{ - if (mtrCtrl->stateMachine != FSM_RUN) { - SysCmdStartSet(&mtrCtrl->statusReg); /* start motor. */ - mtrCtrl->motorStateFlag = 1; - ackCode = 0X24; /* send ackcode to host. */ - CUST_AckCode(g_uartTxBuf, ackCode, 1); - } -} - -/** - * @brief Motor stop. - * @param mtrCtrl The motor control handle. - */ -static void CMDCODE_MotorStop(MTRCTRL_Handle *mtrCtrl) -{ - SysCmdStopSet(&mtrCtrl->statusReg); - mtrCtrl->motorStateFlag = 0; - ackCode = 0X25; - CUST_AckCode(g_uartTxBuf, ackCode, 0); -} - -/** - * @brief Motor state reset. - * @param mtrCtrl The motor control handle. - */ -static void CMDCODE_MotorReset(MTRCTRL_Handle *mtrCtrl) -{ - BASE_FUNC_UNUSED(mtrCtrl); - BASE_FUNC_SoftReset(); -} - -/** - * @brief Set IF-Startup parameters. - * @param mtrCtrl The motor control handle. - * @param rxData Receive buffer - */ -static void SetStartupIFParams(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) -{ - /* Get command code. */ - int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); - - switch (cmdCode) { - case SET_IF_TARGET_CURRENT_VALUE: /* Set I/F start up target current value. */ - mtrCtrl->ifCtrl.targetAmp = rxData->data[DATA_SEGMENT_THREE].typeF; - ackCode = 0X26; - CUST_AckCode(g_uartTxBuf, ackCode, mtrCtrl->ifCtrl.targetAmp); - break; - case SET_INCREMENT_OF_IF_CURRENT: /* Set increment of I/F start up current. */ - mtrCtrl->ifCtrl.stepAmp = mtrCtrl->ifCtrl.targetAmp / rxData->data[DATA_SEGMENT_THREE].typeF * - CTRL_SYSTICK_PERIOD; - ackCode = 0X27; - CUST_AckCode(g_uartTxBuf, ackCode, mtrCtrl->ifCtrl.stepAmp); - break; - case SET_SPEED_RING_BEGIN_SPEED: /* Set speed ring begin speed. */ - mtrCtrl->startup.spdBegin = rxData->data[DATA_SEGMENT_THREE].typeF / - CONST_VALUE_60 * mtrCtrl->mtrParam.mtrNp; - ackCode = 0X28; - CUST_AckCode(g_uartTxBuf, ackCode, rxData->data[DATA_SEGMENT_THREE].typeF); - break; - default: - break; - } -} - -/** - * @brief Set start up parameters. - * @param mtrCtrl The motor control handle. - * @param rxData Receive buffer - */ -static void CMDCODE_SetStartupParams(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) -{ - /* Get function code. */ - int funcCode = (int)(rxData->data[DATA_SEGMENT_ONE].typeF); - - if (funcCode == FOC_STARTUP_IF) { - SetStartupIFParams(mtrCtrl, rxData); - } -} - -/** - * @brief Set adjust speed mode. - * @param mtrCtrl The motor control handle. - * @param rxData Receive buffer - */ -static void CMDCODE_SetAdjustSpdMode(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) -{ - /* Get function code. */ - int funcCode = (int)(rxData->data[DATA_SEGMENT_ONE].typeF); - /* Get commond code. */ - int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); - if (funcCode == HOST_SPEED_ADJUST) { - mtrCtrl->spdAdjustMode = HOST_SPEED_ADJUST; - /* Uart connect success. */ - mtrCtrl->uartConnectFlag = CONNECTING; - ackCode = 0X2A; - CUST_AckCode(g_uartTxBuf, ackCode, (float)mtrCtrl->spdAdjustMode); - } else if (funcCode == CUST_SPEED_ADJUST) { - if (cmdCode == 1) { /* If uart connection disconnected & stop motor commond. */ - SysCmdStopSet(&mtrCtrl->statusReg); - mtrCtrl->motorStateFlag = 0; - } - mtrCtrl->spdAdjustMode = CUST_SPEED_ADJUST; - ackCode = 0X2B; - CUST_AckCode(g_uartTxBuf, ackCode, (float)mtrCtrl->spdAdjustMode); - /* Uart disconnect. */ - mtrCtrl->uartConnectFlag = DISCONNECT; - } -} - -/** - * @brief Check uart connect. - * @param mtrCtrl The motor control handle. - * @param rxData Receive buffer - */ -static void CMDCODE_UartHandShake(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) -{ - CMDCODE_SetAdjustSpdMode(mtrCtrl, rxData); -} - -/** - * @brief Set Motor Initial Status Parameters. - * @param mtrCtrl The motor control handle. - * @param rxData Receive buffer - * @param code Instruction code. - */ -static void CMDCODE_EXE_SetMotorInitParams(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData, unsigned char code) -{ - switch (code) { - case CMDCODE_SET_OBSERVER_TYPE: { /* Set observer type. */ - CMDCODE_SetObserverType(mtrCtrl, rxData); - } - break; - case CMDCODE_SET_PID_PARAMS: { /* Set motor pid parameters. */ - CMDCODE_SetMotorPidParams(mtrCtrl, rxData); - } - break; - case CMDCODE_SET_OBSERVER_PARAMS: { /* Set observer parameters. */ - CMDCODE_SetObserverParams(mtrCtrl, rxData); - } - break; - case CMDCODE_SET_MOTOR_TARGETSPD: { /* Set motor speed and speed slope. */ - CMDCODE_SetMotorSpdAndSlope(mtrCtrl, rxData); - } - break; - case CMDCODE_SET_MOTOR_PARAMS: { /* Set motor parameters. */ - CMDCODE_SetMotorParams(mtrCtrl, rxData); - } - break; - case CMDCODE_SET_ADJUSTSPD_MODE: /* Set adjust speed mode. */ - CMDCODE_SetAdjustSpdMode(mtrCtrl, rxData); - break; - default: - break; - } -} - -/** - * @brief Set Motor Control System Status. - * @param mtrCtrl The motor control handle. - * @param rxData Receive buffer - * @param code Instruction code. - */ -static void CMDCODE_EXE_SetMotorState(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData, unsigned char code) -{ - BASE_FUNC_UNUSED(rxData); - switch (code) { - case CMDCODE_MOTOR_START: { /* Motor start command */ - CMDCODE_MotorStart(mtrCtrl); - } - break; - case CMDCODE_MOTOR_STOP: { /* Motor stop command */ - CMDCODE_MotorStop(mtrCtrl); - } - break; - case CMDCODE_MOTORSTATE_RESET: { /* Motor reset command */ - CMDCODE_MotorReset(mtrCtrl); - } - break; - default: - break; - } -} - -/** - * @brief Set Startup and Uart Link Handshake Flag Parameters. - * @param mtrCtrl The motor control handle. - * @param rxData Receive buffer - * @param code Instruction code. - */ -static void CMDCODE_EXE_SetOtherParams(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData, unsigned char code) -{ - switch (code) { - case CMDCODE_SET_STARTUP_PARAMS: /* Set start up parameters. */ - CMDCODE_SetStartupParams(mtrCtrl, rxData); - break; - case CMDCODE_UART_HANDSHAKE: { /* Check uart hand shake. */ - CMDCODE_UartHandShake(mtrCtrl, rxData); - } - break; - case CMDCODE_UART_HEARTDETECT: { /* Check uart hand shake. */ - mtrCtrl->uartHeartDetCnt++; - } - break; - default: - break; - } -} -/** - * @brief Instruction code executor. - * @param mtrCtrl The motor control handle. - * @param rxData Receive buffer - * @param code Instruction code. - */ -static void CMDCODE_EXE_Process(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData, unsigned char code) -{ - CMDCODE_EXE_SetMotorInitParams(mtrCtrl, rxData, code); - CMDCODE_EXE_SetMotorState(mtrCtrl, rxData, code); - CMDCODE_EXE_SetOtherParams(mtrCtrl, rxData, code); -} - -/** - * @brief Host data download callback and data parsing. - * @param mtrCtrl The motor control handle. - * @param rxBuf Receive buffer - */ -void CUST_UartDataProcess(MTRCTRL_Handle *mtrCtrl, unsigned char *rxBuf) -{ - /* Verify Parameters */ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - MCS_ASSERT_PARAM(rxBuf != NULL); - - /* Uart data storage struct */ - CUSTDATATYPE_DEF data; - volatile unsigned char *ptrCnt = &rxBuf[FRAME_CHECK_BEGIN + 1]; - volatile unsigned char *strCnt = &data.data[0].typeCh[0]; - for (unsigned int j = 0; j < FRAME_RECV_DATA_LENTH * FRAME_ONE_DATA_LENTH; j++) { - *strCnt++ = *ptrCnt++; - } - /* Message function code. */ - data.code = rxBuf[FRAME_CHECK_BEGIN]; - CMDCODE_EXE_Process(mtrCtrl, &data, data.code); -} - -/** - * @brief The host computer displays data transmission. - * @param mtrCtrl The motor control handle. - * @param txData Message content. - * @param stage Message status function code. - */ -void CUST_SetTxMsg(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *txData) -{ - /* Verify Parameters */ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - MCS_ASSERT_PARAM(txData != NULL); - if (mtrCtrl->stateMachine == FSM_IDLE) { - mtrCtrl->smo.spdEst = 0.0f; - } - /* Data send to host. */ - txData->data[CURRDQ_Q].typeF = mtrCtrl->idqFbk.q; - txData->data[CURRDQ_D].typeF = mtrCtrl->idqFbk.d; - txData->data[CURRREFDQ_Q].typeF = mtrCtrl->idqRef.q; - txData->data[CURRREFDQ_D].typeF = mtrCtrl->idqRef.d; - /* Motor current speed. */ - txData->data[CURRSPD].typeF = mtrCtrl->smo.spdEst * CONST_VALUE_60 / mtrCtrl->mtrParam.mtrNp; - /* Motor commond speed. */ - txData->data[SPDCMDHZ].typeF = mtrCtrl->spdCmdHz * CONST_VALUE_60 / mtrCtrl->mtrParam.mtrNp; - /* Bus voltage. */ - txData->data[UDC].typeF = mtrCtrl->udc; - /* Power board temprature. */ - txData->data[POWERBOARDTEMP].typeF = mtrCtrl->powerBoardTemp; - /* Motor protection status flag. */ - txData->data[CUST_ERR_CODE].typeI = mtrCtrl->prot.motorErrStatus.all; - /* Three phase current. */ - txData->data[CURRUVW_U].typeF = mtrCtrl->currUvw.u; - txData->data[CURRUVW_V].typeF = mtrCtrl->currUvw.v; - txData->data[CURRUVW_W].typeF = mtrCtrl->currUvw.w; - /* Three phase pwm duty. */ - txData->data[PWMDUTYUVW_U].typeF = mtrCtrl->dutyUvw.u; - txData->data[PWMDUTYUVW_V].typeF = mtrCtrl->dutyUvw.v; - txData->data[PWMDUTYUVW_W].typeF = mtrCtrl->dutyUvw.w; - /* Motor electric angle. */ - txData->data[AXISANGLE].typeF = mtrCtrl->axisAngle; - txData->data[VDQ_Q].typeF = mtrCtrl->vdqRef.q; - txData->data[VDQ_D].typeF = mtrCtrl->vdqRef.d; - txData->data[SPDREFHZ].typeF = mtrCtrl->spdRefHz * CONST_VALUE_60 / mtrCtrl->mtrParam.mtrNp; - txData->data[SENDTIMESTAMP].typeF = mtrCtrl->uartTimeStamp; -} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/user_interface/cust_process.h b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/user_interface/cust_process.h deleted file mode 100644 index 60fe69bb..00000000 --- a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/user_interface/cust_process.h +++ /dev/null @@ -1,36 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file cust_process.h - * @author MCU Algorithm Team - * @brief This file provides functions declaration of system status. - */ - -/* Define to prevent recursive inclusion ------------------------------------------------------- */ -#ifndef McsMagicTag_HMI_MOUDLE_H -#define McsMagicTag_HMI_MOUDLE_H - -/* Includes ------------------------------------------------------------------------------------ */ -#include "protocol.h" -/* Typedef definitions ------------------------------------------------------------------------- */ - - -void CUST_UartDataProcess(MTRCTRL_Handle *mtrCtrl, unsigned char *rxBuf); - -void CUST_SetTxMsg(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *txData); - -#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/user_interface/hmi_module.c b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/user_interface/hmi_module.c deleted file mode 100644 index 58bf15a7..00000000 --- a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/user_interface/hmi_module.c +++ /dev/null @@ -1,57 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file hmi_module.c - * @author MCU Algorithm Team - * @brief This file provides functions declaration of human-machine interface. - */ - -#include "hmi_module.h" -#include "mcs_assert.h" - -/** - * @brief HMI Initializatio. - * @retval None. - */ -void HMI_Init(void) -{ - UartRecvInit(); -} - -/** - * @brief HMI processing. - * @param mtrCtrl The motor control handle.. - * @retval None. - */ -void HMI_Process_Rx(MTRCTRL_Handle *mtrCtrl) -{ - /* Verify Parameters */ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - UartModuleProcess_Rx(mtrCtrl); -} - -/** - * @brief HMI processing. - * @param mtrCtrl The motor control handle.. - * @retval None. - */ -void HMI_Process_Tx(MTRCTRL_Handle *mtrCtrl) -{ - /* Verify Parameters */ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - UartModuleProcess_Tx(mtrCtrl); -} diff --git a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/user_interface/hmi_module.h b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/user_interface/hmi_module.h deleted file mode 100644 index 056f8d73..00000000 --- a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/user_interface/hmi_module.h +++ /dev/null @@ -1,37 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file hmi_module.h - * @author MCU Algorithm Team - * @brief This file provides functions declaration of system status. - */ - -/* Define to prevent recursive inclusion ------------------------------------------------------- */ -#ifndef McsMagicTag_HMI_MOUDLE_H -#define McsMagicTag_HMI_MOUDLE_H - -/* Includes ------------------------------------------------------------------------------------ */ -#include "uart_module.h" -/* Typedef definitions ------------------------------------------------------------------------- */ - - -void HMI_Init(void); - -void HMI_Process_Rx(MTRCTRL_Handle *mtrCtrl); -void HMI_Process_Tx(MTRCTRL_Handle *mtrCtrl); - -#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/user_interface/protocol.c b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/user_interface/protocol.c deleted file mode 100644 index 56f0c0bb..00000000 --- a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/user_interface/protocol.c +++ /dev/null @@ -1,174 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file protocol.c - * @author MCU Algorithm Team - * @brief This file provides functions declaration of port communication. - */ - -#include "protocol.h" -#include "apt.h" -#include "typedefs.h" -#include "main.h" -#include "mcs_assert.h" -#include "cust_process.h" - -/** - * @brief Callback function for receiving data analysis and processing. - * @param rxBuf Receive buffer. - */ -__weak void CUST_UartDataProcess(MTRCTRL_Handle *mtrCtrl, unsigned char *rxBuf) -{ - BASE_FUNC_UNUSED(mtrCtrl); - BASE_FUNC_UNUSED(rxBuf); -} -/** - * @brief User-defined protocol message sending function (weak function). - * @param rxData Sending Messages.. - */ -__weak void CUST_SetTxMsg(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *txData) -{ - BASE_FUNC_UNUSED(mtrCtrl); - BASE_FUNC_UNUSED(*txData); -} - -static void (*g_ptrDataProcess)(MTRCTRL_Handle *mtrCtrl, unsigned char *rxBuf) = CUST_UartDataProcess; - -/** - * @brief Frame checksum. - * @param ptr Pointer to the data to be checked - * @param num Number of bytes - * @retval unsigned char Checksum - */ -static unsigned char CheckSum(unsigned char *ptr, unsigned char num) -{ - unsigned char sum = 0; - unsigned char *p = ptr; - /* Calculate the sum of received data. */ - for (unsigned char i = 0; i < num; i++) { - sum += (unsigned char)*p; - p++; - } - return sum; -} - -/** - * @brief Transmitting Data Frames. - * @param mtrCtrl The motor control handle. - * @param txBuf Sending Messages. - */ -unsigned int CUST_TransmitData(MTRCTRL_Handle *mtrCtrl, unsigned char *txBuf) -{ - /* Verify Parameters */ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - MCS_ASSERT_PARAM(txBuf != NULL); - unsigned int dataLen = FRAME_ONE_DATA_LENTH * SEND_FRAME_DATA_NUM; - unsigned char i = 0; - CUSTDATATYPE_DEF txData = {0}; - CUST_SetTxMsg(mtrCtrl, &txData); - txBuf[i++] = FRAME_START; - /* Message function code */ - txBuf[i++] = FRAME_SENT; - /* Message data */ - for (unsigned char x = 0; x < SEND_FRAME_DATA_NUM; x++) { - unsigned char floatIndex = 0; - unsigned char byteOffset = i; - txBuf[x * FRAME_ONE_DATA_LENTH + byteOffset++] = txData.data[x].typeCh[floatIndex++]; - txBuf[x * FRAME_ONE_DATA_LENTH + byteOffset++] = txData.data[x].typeCh[floatIndex++]; - txBuf[x * FRAME_ONE_DATA_LENTH + byteOffset++] = txData.data[x].typeCh[floatIndex++]; - txBuf[x * FRAME_ONE_DATA_LENTH + byteOffset++] = txData.data[x].typeCh[floatIndex++]; - } - /* Message verification domain */ - txBuf[dataLen + i++] = CheckSum((unsigned char*)&txBuf[FRAME_CHECK_BEGIN], dataLen + 1); - /* End of Message */ - txBuf[dataLen + i++] = FRAME_END; - return dataLen + i; -} - -/** - * @brief Transmitting Data Frames. - * @param txBuf Sending Cust Ack Code. - * @param ackCode Ack Code. - * @param varParams Host set parameter. - */ -void CUST_AckCode(unsigned char *txBuf, unsigned char ackCode, float varParams) -{ - /* Verify Parameters */ - MCS_ASSERT_PARAM(txBuf != NULL); - CUSTDATATYPE_DEF txData = {0}; - int dataIndex = 0; - unsigned int i = 0; - unsigned int txLen = 0; - unsigned char dataLen = FRAME_ONE_CHAR_LENTH + FRAME_ONE_DATA_LENTH; - - txData.data[0].typeF = varParams; - txBuf[i++] = FRAME_START; - /* Message function code */ - txBuf[i++] = FRAME_CUSTACK; - /* Message ack code */ - txBuf[i++] = ackCode; - /* Message data */ - txBuf[i++] = txData.data[0].typeCh[dataIndex++]; - txBuf[i++] = txData.data[0].typeCh[dataIndex++]; - txBuf[i++] = txData.data[0].typeCh[dataIndex++]; - txBuf[i++] = txData.data[0].typeCh[dataIndex++]; - - /* Message verification domain */ - txBuf[FRAME_ONE_CHAR_LENTH + i++] = CheckSum((unsigned char*)&txBuf[FRAME_CHECK_BEGIN], dataLen + 1); - /* End of Message */ - txBuf[FRAME_ONE_CHAR_LENTH + i++] = FRAME_END; - txLen = FRAME_ONE_CHAR_LENTH + i++; - HAL_UART_WriteDMA(&g_uart0, txBuf, txLen); -} - -/** - * @brief Cust receive data process. - * @param mtrCtrl The motor control handle. - * @param rxBuf Receive buffer - */ -void CUST_DataReceProcss(MTRCTRL_Handle *mtrCtrl, unsigned char *rxBuf) -{ - /* Verify Parameters */ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - MCS_ASSERT_PARAM(rxBuf != NULL); - unsigned char g_uartTxBuf[10] = {0}; - unsigned char ackCode = 0; - /* Frame header check */ - if (rxBuf[0] != FRAME_START) { - ackCode = 0X78; - CUST_AckCode(g_uartTxBuf, ackCode, 0); - return; - } - /* Frame trailer check */ - if (rxBuf[FRAME_LENTH - 1] != FRAME_END) { - ackCode = 0X79; - CUST_AckCode(g_uartTxBuf, ackCode, 0); - return; - } - /* Checksum */ - if (CheckSum((unsigned char*)&rxBuf[FRAME_CHECK_BEGIN], FRAME_CHECK_NUM) != rxBuf[FRAME_CHECKSUM]) { - ackCode = 0X7A; - CUST_AckCode(g_uartTxBuf, ackCode, 0); - return; - } else { - if (g_ptrDataProcess == NULL) { - return; - } else { - g_ptrDataProcess(mtrCtrl, rxBuf); - } - } -} diff --git a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/user_interface/protocol.h b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/user_interface/protocol.h deleted file mode 100644 index 900a00d8..00000000 --- a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/user_interface/protocol.h +++ /dev/null @@ -1,121 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file protocol.h - * @author MCU Algorithm Team - * @brief This file provides functions declaration of port communication. - */ -#ifndef McsMagicTag_PORTOCOL_H -#define McsMagicTag_PORTOCOL_H - -#include "uart.h" -#include "mcs_carrier.h" - -#define RS485_SEND_ENABLE GPIO6->GPIO_DATA[GPIO_PIN_7].reg = GPIO_PIN_7 -#define RS485_SEND_DISABLE GPIO6->GPIO_DATA[GPIO_PIN_7].reg = BASE_CFG_UNSET - -#define RX_BUF_LEN (16) -#define SEND_FRAME_DATA_NUM (CUSTDATA_MAX) -/* Service Uart0 Communication Deal */ -#define FRAME_ONE_DATA_LENTH 4 -#define FRAME_ONE_CHAR_LENTH 1 -#define FRAME_RECV_DATA_LENTH 4 -#define FRAME_LENTH 20 /* Data length */ -#define FRAME_SENT 0X8F -#define FRAME_CUSTACK 0X8A -#define FRAME_START 0x0F /* Start frame */ -#define FRAME_END '/' /* StOP frame */ -#define FRAME_CHECK_BEGIN 1 /* Check frame */ -#define FRAME_CHECKSUM 18 /* Check sum */ -#define FRAME_CHECK_NUM 17 -#define CMDCODE_IDLE_FRAME 0x55 /* Fill frame */ -#define CMDCODE_GET_MOTOR_PARAMS 0x01 -#define CMDCODE_SEND_MOTOR_PARAMS 0x02 -#define CMDCODE_SET_MOTOR_CTLMODE 0x03 -#define CMDCODE_SET_OBSERVER_TYPE 0x04 -#define CMDCODE_SET_STARTUP_MODE 0x05 -#define CMDCODE_SET_PID_PARAMS 0x06 -#define CMDCODE_SET_STARTUP_PARAMS 0x07 -#define CMDCODE_SET_OBSERVER_PARAMS 0x08 -#define CMDCODE_SET_MOTOR_TARGETSPD 0x09 -#define CMDCODE_SET_MOTOR_PARAMS 0x0A -#define CMDCODE_MOTOR_START 0x0B -#define CMDCODE_MOTOR_STOP 0x0C -#define CMDCODE_MOTORSTATE_RESET 0x0D -#define CMDCODE_SEND_FIRMVERSION 0x0E -#define CMDCODE_SET_ADJUSTSPD_MODE 0x11 -#define CMDCODE_UART_HANDSHAKE 0x12 -#define CMDCODE_UART_HEARTDETECT 0x13 - -typedef union { - unsigned char typeCh[4]; - float typeF; - int typeI; -} UNIONDATATYPE_DEF; - -typedef enum { - OFFLINE_RES = 0, - OFFLINE_LD, - OFFLINE_LQ, - OFFLINE_PSIF, - OFFLINE_JS, - OFFLINE_NP, - OFFLINE_B, - OFFLINE_KPD, - OFFLINE_KID, - OFFLINE_KPQ, - OFFLINE_KIQ, - OFFLINE_KPS, - OFFLINE_KIS, - OFFLINE_SPEED, - OFLINE_MAX -} OFFLINE_IDEN_TYPE; - -typedef enum { - CURRDQ_Q = 0, - CURRDQ_D, - CURRREFDQ_Q, - CURRREFDQ_D, - CURRSPD, - SPDCMDHZ, - UDC, - POWERBOARDTEMP, - CUST_ERR_CODE, - CURRUVW_U, - CURRUVW_V, - CURRUVW_W, - PWMDUTYUVW_U, - PWMDUTYUVW_V, - PWMDUTYUVW_W, - AXISANGLE, - VDQ_Q, - VDQ_D, - SPDREFHZ, - SENDTIMESTAMP, - CUSTDATA_MAX -} SENDTOHOSTPARAMS; - -typedef struct { - volatile unsigned char code; - volatile UNIONDATATYPE_DEF data[SEND_FRAME_DATA_NUM]; -} CUSTDATATYPE_DEF; - - -void CUST_DataReceProcss(MTRCTRL_Handle *mtrCtrl, unsigned char *rxBuf); -unsigned int CUST_TransmitData(MTRCTRL_Handle *mtrCtrl, unsigned char *txBuf); -void CUST_AckCode(unsigned char *txBuf, unsigned char ackCode, float varParams); -#endif /* McsMagicTag_PORTOCOL_H */ diff --git a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/user_interface/uart_module.c b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/user_interface/uart_module.c deleted file mode 100644 index df5e9186..00000000 --- a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/user_interface/uart_module.c +++ /dev/null @@ -1,184 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the - * following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file uart_module.c - * @author MCU Algorithm Team - * @brief This file provides functions declaration of Serial port communication. - */ -#include "uart_module.h" -#include "debug.h" -#include "main.h" -#include "baseinc.h" - -/* Buffer size */ -#define UI_TX_BUF_LEN (96) -#define UI_RX_BUF_LEN (96) - -/* Receiving Timeout Interval */ -#define UART_TIME_OUT_MS (100) - -/* Start sending data to host delay after uart connect success */ -#define UART_UPDATA_DELAY_TIME_MS (50) - -/* Data buffer */ -unsigned char g_uartRxBuf[UI_RX_BUF_LEN] = {0}; -unsigned char g_uartTxBuf[UI_TX_BUF_LEN] = {0}; -static unsigned int getdeltaSystickCnt = 0; -static FRAME_Handle g_uartFrame; -/** - * @brief Receive Data Clear. - * @param uartFrame Receice Data. - * @retval None. - */ -static void FrameRecvClear(FRAME_Handle *uartFrame) -{ - /* Clear buffer lenth. */ - uartFrame->buffLen = 0; - uartFrame->timeOutCnt = 0; - uartFrame->frameFlag = 0; - /* Clear received data lenth. */ - uartFrame->rxLen = 0; - /* Clear received flag. */ - uartFrame->rxFlag = 0; - uartFrame->upDataCnt = 0; - uartFrame->rxAckFlag = 0; -} - -/** - * @brief Set Dma status. - * @param mtrCtrl The motor control handle. - * @retval None. - */ -static void SetUartDmaStatus(MTRCTRL_Handle *mtrCtrl) -{ - /* Delay 50ms start uart Tx DMA . */ - if (mtrCtrl->uartConnectFlag == CONNECTING && g_uartFrame.upDataDelayCnt++ > UART_UPDATA_DELAY_TIME_MS) { - g_uartFrame.txFlag = 1; /* Start send data flag. */ - mtrCtrl->uartConnectFlag = CONNECTED; - g_uartFrame.upDataDelayCnt = 0; - } - if (mtrCtrl->uartConnectFlag == DISCONNECT) { - g_uartFrame.txFlag = 0; /* Stop send data flag. */ - mtrCtrl->uartTimeStamp = 0; - } -} - -/** - * @brief Uart Dma interupt callback func. - * @param handle Uart handle. - * @retval None. - */ -void UART0_TXDMACallback(void *handle) -{ - BASE_FUNC_UNUSED(handle); - unsigned int getCurSystickCnt = 0; - static unsigned int getlastSystickCnt = 0; - /* USER CODE BEGIN UART0_WRITE_DMA_FINISH */ - g_uartFrame.txFlag = 1; - /* Received information answered, information update reserved. */ - if (g_uartFrame.rxAckFlag == 1) { - g_uartFrame.uartItTxFlag = 1; - g_uartFrame.rxAckFlag = 0; - } - getCurSystickCnt = DCL_SYSTICK_GetTick(); - if (getlastSystickCnt != 0) { - /* Calculate unit frame data send time */ - getdeltaSystickCnt = getCurSystickCnt - getlastSystickCnt; - } - getlastSystickCnt = getCurSystickCnt; - /* USER CODE END UART0_WRITE_DMA_FINISH */ -} - -/** - * @brief Uart0 Interruput Read CallBack Function. - * @param handle Uart handle. - * @retval None. - */ -void UART0ReadInterruptCallback(void *handle) -{ - BASE_FUNC_UNUSED(handle); - /* USER CODE BEGIN UART0_READ_IT_FINISH */ - if (g_uartFrame.rxLen >= UI_RX_BUF_LEN - 1) { - g_uartFrame.rxLen = 0; - } - HAL_UART_ReadIT(handle, &g_uartFrame.rxData, 1); - g_uartRxBuf[g_uartFrame.rxLen] = g_uartFrame.rxData; - g_uartFrame.rxLen++; - g_uartFrame.rxFlag = 1; - g_uartFrame.uartItTxFlag = 0; - return; - /* USER CODE END UART0_READ_IT_FINISH */ -} - -/** - * @brief Uart Read Data Init Function. - * @param void. - * @retval None. - */ -void UartRecvInit(void) -{ - /* Uart reception initialization */ - FrameRecvClear(&g_uartFrame); - HAL_UART_ReadIT(&g_uart0, &g_uartFrame.rxData, 1); -} - -/** - * @brief Uart Read Data Process Function. - * @param mtrCtrl The motor control handle. - * @retval None. - */ -void UartModuleProcess_Rx(MTRCTRL_Handle *mtrCtrl) -{ - /* Verify Parameters */ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - SetUartDmaStatus(mtrCtrl); - if (g_uartFrame.rxFlag == 1) { /* Receive data flag. */ - if (g_uartFrame.timeOutCnt++ > UART_TIME_OUT_MS) { - /* Received data from the host. */ - g_uartFrame.frameFlag = 1; - g_uartFrame.rxFlag = 0; - g_uartFrame.timeOutCnt = 0; - /* Execute data process. */ - CUST_DataReceProcss(mtrCtrl, g_uartRxBuf); - g_uartFrame.rxAckFlag = 1; - g_uartFrame.rxLen = 0; - } - } - g_uartFrame.frameFlag = 0; -} - -/** - * @brief Uart Write Data Process Function. - * @param mtrCtrl The motor control handle. - * @retval None. - */ -void UartModuleProcess_Tx(MTRCTRL_Handle *mtrCtrl) -{ - /* Verify Parameters */ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - if (g_uartFrame.txFlag == 1) { /* Send data flag. */ - mtrCtrl->uartTimeStamp = (float)getdeltaSystickCnt; /* Unit data time stamp */ - g_uartFrame.upDataCnt = 0; - g_uartFrame.txFlag = 0; - /* Send data to host. */ - unsigned int txLen = CUST_TransmitData(mtrCtrl, g_uartTxBuf); - /* If txIT mode send data finish, convert to DMA mode */ - if (g_uartFrame.uartItTxFlag == 1) { - HAL_UART_WriteDMA(&g_uart0, g_uartTxBuf, txLen); - } - } -} diff --git a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/user_interface/uart_module.h b/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/user_interface/uart_module.h deleted file mode 100644 index f1f8f899..00000000 --- a/vendor/yibaina_3061M/demo/10025_Six_Step_Wave/user/pmsm_sensorless_2shunt_foc/user_interface/uart_module.h +++ /dev/null @@ -1,47 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file uart_module.h - * @author MCU Algorithm Team - * @brief This file provides functions declaration of Serial port communication. - */ -#ifndef McsMagicTag_UART_MODULE_H -#define McsMagicTag_UART_MODULE_H - -#include "protocol.h" -#include "mcs_ctlmode_config.h" - -typedef struct { - unsigned int buffLen; - unsigned int timeOutCnt; - unsigned char frameFlag; - unsigned int rxLen; - unsigned char rxFlag; - unsigned char txFlag; - unsigned char rxData; - unsigned int upDataCnt; - unsigned int upDataDelayCnt; - unsigned char uartItTxFlag; - unsigned char rxAckFlag; -} FRAME_Handle; - - -void UartRecvInit(void); -void UartModuleProcess_Rx(MTRCTRL_Handle *mtrCtrl); -void UartModuleProcess_Tx(MTRCTRL_Handle *mtrCtrl); - -#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/E7050_FOC/readme.md b/vendor/yibaina_3061M/demo/E7050_FOC/readme.md deleted file mode 100644 index 55601995..00000000 --- a/vendor/yibaina_3061M/demo/E7050_FOC/readme.md +++ /dev/null @@ -1,20 +0,0 @@ -# E7050_foc - -**【功能描述】** - -- 基于ECMCU105H/ECBMCU201MPC单板的单电机双电阻采样的无感Foc应用 - -**【环境要求】** - -- 所有单板电源改制为演示用的24V低压,电机选用E7050 -- 电机(黄绿蓝)相线分别对应功率板上U/V/W通道接口 - -**【配置方法】** - -- 将user文件夹替换原工程user文件夹即可 - -【操作说明】 - -- 点击开发板按键启动电机 - -- 开发板蓝色电位器,可以调节电机转动的快慢。(5-29HZ) \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/E7050_FOC/user/generatecode/feature.h b/vendor/yibaina_3061M/demo/E7050_FOC/user/generatecode/feature.h deleted file mode 100644 index 715371be..00000000 --- a/vendor/yibaina_3061M/demo/E7050_FOC/user/generatecode/feature.h +++ /dev/null @@ -1,94 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file feature.h - * @author MCU Driver Team - * @brief This file contains macro configurations related to the project. This file is generated by the IDE tool. - * @date 2025-04-14 14:05:10 - */ - -#ifndef McuMagicTag_FEATURE_H -#define McuMagicTag_FEATURE_H - -/* Macro definitions --------------------------------------------------------- */ -#define CHIP_3061MNPICA MACRO_ENABLE - -#define MACRO_ENABLE 1 -#define MACRO_DISABLE 0 - -/* Macro switch */ -#define BASE_DEFINE_USE_ASSERT MACRO_ENABLE -#ifndef FLASH_CRC_CONFIG -#define FLASH_CRC_CONFIG -#endif /* #ifndef FLASH_CRC_CONFIG */ -#define BASE_MATH_SINCOS_MIDDLE_TABLE MACRO_ENABLE /**< This macro is used to control the table type when the - BASE_MATH_GetSinCos() queries the table. When the value of - this macro is MACRO_ENABLE, the error value obtained by the - BASE_MATH_GetSinCos() is relatively small, and the return - value of the function may be greater than or less than the - actual value. When the value of this macro is MACRO_DISABLE, - the error value obtained by the BASE_MATH_GetSinCos() is - relatively large. However, in the range [0°, 180°) and - [180°, 360°), the return value of the function is either - greater than or less than the actual value. */ - -/* Peripheral module macro switch--------------------------------------------- */ -#define BOARD_DIM_NUM 1 /**< Number of dimming handle arrays. */ - -#define BOARD_KEY_NUM 10 /**< Number of key handle arrays. */ -#define BOARD_KEY_PRESS_ON GPIO_HIGH_LEVEL /**< GPIO status corresponding to long press valid. */ -#define BOARD_KEY_PRESS_OFF GPIO_LOW_LEVEL /**< GPIO status corresponding to short press valid. */ - -#define BOARD_LED_SEG_NUM 4 /**< Number of segments. */ -#define BOARD_LED_SEGMENT_ON GPIO_HIGH_LEVEL /**< GPIO level status corresponding to valid segments. */ -#define BOARD_LED_SEGMENT_OFF GPIO_LOW_LEVEL /**< GPIO level status corresponding to invalid segments. */ - -#define BOARD_MKEY_SCHEME_NUMBER BOARD_MKEY_SCHEME_NUMBER_ONE /**< Define the scheme to be adopted. */ -#define BOARD_MKEY_OUT_NUM 4 /**< Number of GPIO pins used as output during scanning. */ -#define BOARD_MKEY_IN_NUM 4 /**< Number of GPIO pins used as input during scanning. */ -#define BOARD_MKEY_OUT_PIN_VALID GPIO_LOW_LEVEL /**< GPIO level status corresponding to the valid \ - status of the output GPIO in the key matrix. */ -#define BOARD_MKEY_OUT_PIN_INVALID GPIO_HIGH_LEVEL /**< GPIO level status corresponding to the \ - invalid status of the output GPIO in the key matrix. */ -#define BOARD_MKEY_IN_PIN_VALID GPIO_LOW_LEVEL /**< Indicates the GPIO level corresponding to the \ - valid status of the input GPIO in the key matrix. */ -#define BOARD_MKEY_IN_PIN_INVALID GPIO_HIGH_LEVEL /**< Indicates the GPIO level corresponding to the \ - invalid status of the input GPIO in the key matrix. */ - -#define BOARD_PULSES_NUM 2 /**< Number of pulse handles. */ - -#define BASE_DEFINE_SLIPAVERAGE_NUM 2 /**< Sliding average array length. */ - -#define LISTNODE_MAX 20 - -#define BASE_DEFINE_DMA_QUICKSTART - -#define XTRAIL_FREQ 30000000U - -#define DBG_USE_NO_PRINTF 0U -#define DBG_USE_UART_PRINTF 1U - -#define DBG_PRINTF_USE DBG_USE_UART_PRINTF -#if (DBG_PRINTF_USE == DBG_USE_UART_PRINTF) -#define DBG_PRINTF_UART_PORT UART0 -#endif - -/* USER CODE BEGIN 0 */ -/* USER CODE 区域内代码不会被覆盖,区域外会被生成的默认代码覆盖(其余USER CODE 区域同理) */ -/* USER CODE END 0 */ - -#endif /* McuMagicTag_FEATURE_H */ \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/E7050_FOC/user/generatecode/main.h b/vendor/yibaina_3061M/demo/E7050_FOC/user/generatecode/main.h deleted file mode 100644 index 40d65b1d..00000000 --- a/vendor/yibaina_3061M/demo/E7050_FOC/user/generatecode/main.h +++ /dev/null @@ -1,98 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file main.h - * @author MCU Driver Team - * @brief This file contains driver init functions. - * @date 2025-04-14 14:05:10 - */ - -/* Define to prevent recursive inclusion ------------------------------------- */ -#ifndef McuMagicTag_SYSTEM_INIT_H -#define McuMagicTag_SYSTEM_INIT_H - -#include "adc.h" -#include "adc_ex.h" -#include "acmp.h" -#include "acmp_ex.h" -#include "apt.h" -#include "uart.h" -#include "uart_ex.h" -#include "gpio.h" -#include "timer.h" -#include "timer_ex.h" -#include "pga.h" -#include "crg.h" -#include "dma.h" -#include "dma_ex.h" -#include "iocmg.h" - -#define IO_SPEED_FAST 0x00U -#define IO_SPEED_SLOW 0x01U - -#define IO_DRV_LEVEL4 0x00U -#define IO_DRV_LEVEL3 0x01U -#define IO_DRV_LEVEL2 0x02U -#define IO_DRV_LEVEL1 0x03U - -#define XTAL_DRV_LEVEL4 0x03U -#define XTAL_DRV_LEVEL3 0x02U -#define XTAL_DRV_LEVEL2 0x01U -#define XTAL_DRV_LEVEL1 0x00U - -#define GpioStartStop_PIN GPIO_PIN_4 -#define GpioStartStop_HANDLE g_gpio2 - -extern ACMP_Handle g_acmp0; -extern PGA_Handle g_pga0; -extern PGA_Handle g_pga1; -extern TIMER_Handle g_timer0; -extern TIMER_Handle g_timer1; -extern UART_Handle g_uart0; -extern APT_Handle g_apt0; -extern APT_Handle g_apt1; -extern APT_Handle g_apt2; -extern ADC_Handle g_adc0; - -extern DMA_Handle g_dmac; - -extern GPIO_Handle g_gpio2; -extern GPIO_Handle g_gpio1; - -BASE_StatusType CRG_Config(CRG_CoreClkSelect *coreClkSelect); -void SystemInit(void); - -void UART0WriteInterruptCallback(void *handle); -void UART0ReadInterruptCallback(void *handle); - -void UART0InterruptErrorCallback(void *handle); -void MotorStatemachineCallBack(void *handle); -void TIMER1_DMAOverFlow_InterruptProcess(void *handle); -void CheckPotentiometerValueCallback(void *handle); -void TIMER0_DMAOverFlow_InterruptProcess(void *handle); -void MotorCarrierProcessCallback(void *aptHandle); -void MotorSysErrCallback(void *aptHandle); - -void UART0_TXDMACallback(void *handle); - -void MotorStartStopKeyCallback(void *param); - -/* USER CODE BEGIN 0 */ -/* USER CODE 区域内代码不会被覆盖,区域外会被生成的默认代码覆盖(其余USER CODE 区域同理) */ -/* USER CODE END 0 */ - -#endif /* McuMagicTag_SYSTEM_INIT_H */ \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/E7050_FOC/user/generatecode/system_init.c b/vendor/yibaina_3061M/demo/E7050_FOC/user/generatecode/system_init.c deleted file mode 100644 index d49cd860..00000000 --- a/vendor/yibaina_3061M/demo/E7050_FOC/user/generatecode/system_init.c +++ /dev/null @@ -1,677 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file system_init.c - * @author MCU Driver Team - * @brief This file contains driver init functions. - * @date 2025-04-14 14:05:10 - */ - -#include "main.h" -#include "ioconfig.h" -#include "iocmg_ip.h" - -#define UART0_BAUD_RATE 1843200 - -BASE_StatusType CRG_Config(CRG_CoreClkSelect *coreClkSelect) -{ - CRG_Handle crg; - crg.baseAddress = CRG; - crg.pllRefClkSelect = CRG_PLL_REF_CLK_SELECT_HOSC; - crg.pllPreDiv = CRG_PLL_PREDIV_4; - crg.pllFbDiv = 48; /* PLL Multiplier 48 */ - crg.pllPostDiv = CRG_PLL_POSTDIV_2; - crg.coreClkSelect = CRG_CORE_CLK_SELECT_PLL; - crg.handleEx.pllPostDiv2 = CRG_PLL_POSTDIV2_3; - crg.handleEx.clk1MSelect = CRG_1M_CLK_SELECT_HOSC; - crg.handleEx.clk1MDiv = (25 - 1); /* The 1 MHz freq is equal to the input clock frequency / (clk_1m_div + 1). 25 is the div of the clk_1m in CLOCK. */ - - if (HAL_CRG_Init(&crg) != BASE_STATUS_OK) { - return BASE_STATUS_ERROR; - } - *coreClkSelect = crg.coreClkSelect; - return BASE_STATUS_OK; -} - -static void DMA_Channel0Init(void *handle) -{ - DMA_ChannelParam dma_param; - dma_param.direction = DMA_MEMORY_TO_PERIPH_BY_DMAC; - dma_param.srcAddrInc = DMA_ADDR_INCREASE; - dma_param.destAddrInc = DMA_ADDR_UNALTERED; - dma_param.srcPeriph = DMA_REQUEST_MEM; - dma_param.destPeriph = DMA_REQUEST_UART0_TX; - dma_param.srcWidth = DMA_TRANSWIDTH_BYTE; - dma_param.destWidth = DMA_TRANSWIDTH_BYTE; - dma_param.srcBurst = DMA_BURST_LENGTH_1; - dma_param.destBurst = DMA_BURST_LENGTH_1; - dma_param.pHandle = handle; - HAL_DMA_InitChannel(&g_dmac, &dma_param, DMA_CHANNEL_ZERO); -} - -static void DMA_Init(void) -{ - HAL_CRG_IpEnableSet(DMA_BASE, IP_CLK_ENABLE); - g_dmac.baseAddress = DMA; - IRQ_Register(IRQ_DMA_TC, HAL_DMA_IrqHandlerTc, &g_dmac); - IRQ_Register(IRQ_DMA_ERR, HAL_DMA_IrqHandlerError, &g_dmac); - IRQ_EnableN(IRQ_DMA_TC); - IRQ_EnableN(IRQ_DMA_ERR); - HAL_DMA_Init(&g_dmac); - DMA_Channel0Init((void *)(&g_uart0)); - HAL_DMA_SetChannelPriorityEx(&g_dmac, DMA_CHANNEL_ZERO, DMA_PRIORITY_HIGHEST); -} - -static void ACMP0_Init(void) -{ - HAL_CRG_IpEnableSet(ACMP0_BASE, IP_CLK_ENABLE); /* ACMP clock bit reset. */ - g_acmp0.baseAddress = ACMP0_BASE; - g_acmp0.inOutConfig.inputNNum = ACMP_INPUT_N_SELECT2; - g_acmp0.inOutConfig.inputPNum = ACMP_INPUT_P_SELECT2; - g_acmp0.inOutConfig.polarity = ACMP_OUT_NOT_INVERT; - g_acmp0.filterCtrl.filterMode = ACMP_FILTER_NONE; - g_acmp0.hysteresisVol = ACMP_HYS_VOL_30MV; - g_acmp0.interruptEn = BASE_CFG_UNSET; - HAL_ACMP_Init(&g_acmp0); -} - -static void ADC0_Init(void) -{ - HAL_CRG_IpEnableSet(ADC0_BASE, IP_CLK_ENABLE); - HAL_CRG_IpClkSelectSet(ADC0_BASE, CRG_ADC_CLK_ASYN_PLL_DIV); - HAL_CRG_IpClkDivSet(ADC0_BASE, CRG_ADC_DIV_1); - - g_adc0.baseAddress = ADC0; - g_adc0.socPriority = ADC_PRIMODE_ALL_ROUND; - - HAL_ADC_Init(&g_adc0); - - SOC_Param socParam = {0}; - socParam.adcInput = ADC_CH_ADCINA0; /* PGA0_OUT(ADC AIN0) */ - socParam.sampleTotalTime = ADC_SOCSAMPLE_22CLK; /* adc sample total time 22 adc_clk */ - socParam.trigSource = ADC_TRIGSOC_APT0_SOCA; - socParam.continueMode = BASE_CFG_DISABLE; - socParam.finishMode = ADC_SOCFINISH_NONE; - HAL_ADC_ConfigureSoc(&g_adc0, ADC_SOC_NUM1, &socParam); - - socParam.adcInput = ADC_CH_ADCINA1; /* PGA1_OUT(ADC AIN1) */ - socParam.sampleTotalTime = ADC_SOCSAMPLE_22CLK; /* adc sample total time 22 adc_clk */ - socParam.trigSource = ADC_TRIGSOC_APT0_SOCA; - socParam.continueMode = BASE_CFG_DISABLE; - socParam.finishMode = ADC_SOCFINISH_NONE; - HAL_ADC_ConfigureSoc(&g_adc0, ADC_SOC_NUM6, &socParam); - - socParam.adcInput = ADC_CH_ADCINA9; /* PIN7(ADC AIN9) */ - socParam.sampleTotalTime = ADC_SOCSAMPLE_22CLK; /* adc sample total time 22 adc_clk */ - socParam.trigSource = ADC_TRIGSOC_SOFT; - socParam.continueMode = BASE_CFG_DISABLE; - socParam.finishMode = ADC_SOCFINISH_NONE; - HAL_ADC_ConfigureSoc(&g_adc0, ADC_SOC_NUM0, &socParam); - - socParam.adcInput = ADC_CH_ADCINA5; /* PIN2(ADC AIN5) */ - socParam.sampleTotalTime = ADC_SOCSAMPLE_22CLK; /* adc sample total time 22 adc_clk */ - socParam.trigSource = ADC_TRIGSOC_SOFT; - socParam.continueMode = BASE_CFG_DISABLE; - socParam.finishMode = ADC_SOCFINISH_NONE; - HAL_ADC_ConfigureSoc(&g_adc0, ADC_SOC_NUM3, &socParam); - - socParam.adcInput = ADC_CH_ADCINA15; /* PIN14(ADC AIN15) */ - socParam.sampleTotalTime = ADC_SOCSAMPLE_22CLK; /* adc sample total time 22 adc_clk */ - socParam.trigSource = ADC_TRIGSOC_SOFT; - socParam.continueMode = BASE_CFG_DISABLE; - socParam.finishMode = ADC_SOCFINISH_NONE; - HAL_ADC_ConfigureSoc(&g_adc0, ADC_SOC_NUM4, &socParam); -} - -__weak void MotorSysErrCallback(void *aptHandle) -{ - BASE_FUNC_UNUSED(aptHandle); - /* USER CODE BEGIN APT0_EVENT_INTERRUPT */ - /* USER CODE END APT0_EVENT_INTERRUPT */ -} - -__weak void MotorCarrierProcessCallback(void *aptHandle) -{ - BASE_FUNC_UNUSED(aptHandle); - /* USER CODE BEGIN APT0_TIMER_INTERRUPT */ - /* USER CODE END APT0_TIMER_INTERRUPT */ -} - -static void APT0_ProtectInit(void) -{ - APT_OutCtrlProtectEx protectApt = {0}; - protectApt.ocEventEnEx = BASE_CFG_ENABLE; - protectApt.ocEventModeEx = APT_OUT_CTRL_ONE_SHOT; - protectApt.ocActionEx = APT_OUT_CTRL_ACTION_LOW; - protectApt.ocActionBEx = APT_OUT_CTRL_ACTION_LOW; - protectApt.ocEvtInterruptEnEx = BASE_CFG_ENABLE; - protectApt.ocSysEvent = APT_SYS_EVT_DEBUG | APT_SYS_EVT_CLK | APT_SYS_EVT_MEM; - protectApt.originalEvtEx = APT_EM_ORIGINAL_SRC_ACMP0; - protectApt.evtPolarityMaskEx = APT_EM_ACMP0_INVERT_BIT; - protectApt.filterCycleNumEx = 0; - HAL_APT_ProtectInitEx(&g_apt0, &protectApt); -} - -static void APT0_Init(void) -{ - HAL_CRG_IpEnableSet(APT0_BASE, IP_CLK_ENABLE); - - g_apt0.baseAddress = APT0; - - /* Clock Settings */ - g_apt0.waveform.dividerFactor = 1 - 1; - /* Timer Settings */ - g_apt0.waveform.timerPeriod = 7500; /* 7500 is count period of APT time-base timer */ - g_apt0.waveform.cntMode = APT_COUNT_MODE_UP_DOWN; - - /* Wave Form */ - g_apt0.waveform.basicType = APT_PWM_BASIC_A_HIGH_B_LOW; - g_apt0.waveform.chAOutType = APT_PWM_OUT_BASIC_TYPE; - g_apt0.waveform.chBOutType = APT_PWM_OUT_BASIC_TYPE; - g_apt0.waveform.divInitVal = 0; - g_apt0.waveform.cntInitVal = 0; - g_apt0.waveform.cntCmpLeftEdge = 500; /* 500 is count compare point of the left edge of PWM waveform */ - g_apt0.waveform.cntCmpRightEdge = 4000; /* 4000 is count compare point of the right edge of PWM waveform */ - g_apt0.waveform.cntCmpLoadMode = APT_BUFFER_INDEPENDENT_LOAD; - g_apt0.waveform.cntCmpLoadEvt = APT_COMPARE_LOAD_EVENT_ZERO; - g_apt0.waveform.deadBandCnt = 225; /* 225 is dead-band value */ - - /* ADC Trigger SOCA */ - g_apt0.adcTrg.trgEnSOCA = BASE_CFG_ENABLE; - g_apt0.adcTrg.cntCmpSOCA = 375; /* 375 is count compare point of ADC trigger source SOCA when using CMPA */ - g_apt0.adcTrg.trgSrcSOCA = APT_CS_SRC_CNTR_CMPA_DOWN; - g_apt0.adcTrg.trgScaleSOCA = 1; - - /* ADC Trigger SOCB */ - g_apt0.adcTrg.trgEnSOCB = BASE_CFG_ENABLE; - g_apt0.adcTrg.cntCmpSOCB = 1; - g_apt0.adcTrg.trgSrcSOCB = APT_CS_SRC_CNTR_CMPB_DOWN; - g_apt0.adcTrg.trgScaleSOCB = 1; - - g_apt0.adcTrg.cntCmpLoadMode = APT_BUFFER_INDEPENDENT_LOAD; - g_apt0.adcTrg.cntCmpLoadEvt = APT_COMPARE_LOAD_EVENT_ZERO; - - /* Timer Trigger */ - g_apt0.tmrInterrupt.tmrInterruptEn = BASE_CFG_ENABLE; - g_apt0.tmrInterrupt.tmrInterruptSrc = APT_INT_SRC_CNTR_ZERO; - g_apt0.tmrInterrupt.tmrInterruptScale = 1; - - APT0_ProtectInit(); - - HAL_APT_PWMInit(&g_apt0); - HAL_APT_RegisterCallBack(&g_apt0, APT_EVENT_INTERRUPT, MotorSysErrCallback); - IRQ_SetPriority(IRQ_APT0_EVT, 7); /* 7 is priority value */ - IRQ_Register(IRQ_APT0_EVT, HAL_APT_EventIrqHandler, &g_apt0); - IRQ_EnableN(IRQ_APT0_EVT); - HAL_APT_RegisterCallBack(&g_apt0, APT_TIMER_INTERRUPT, MotorCarrierProcessCallback); - IRQ_SetPriority(IRQ_APT0_TMR, 5); /* 5 is priority value */ - IRQ_Register(IRQ_APT0_TMR, HAL_APT_TimerIrqHandler, &g_apt0); - IRQ_EnableN(IRQ_APT0_TMR); -} - -static void APT1_ProtectInit(void) -{ - APT_OutCtrlProtectEx protectApt = {0}; - protectApt.ocEventEnEx = BASE_CFG_ENABLE; - protectApt.ocEventModeEx = APT_OUT_CTRL_ONE_SHOT; - protectApt.ocActionEx = APT_OUT_CTRL_ACTION_LOW; - protectApt.ocActionBEx = APT_OUT_CTRL_ACTION_LOW; - protectApt.ocEvtInterruptEnEx = BASE_CFG_DISABLE; - protectApt.ocSysEvent = APT_SYS_EVT_DEBUG | APT_SYS_EVT_CLK | APT_SYS_EVT_MEM; - protectApt.originalEvtEx = APT_EM_ORIGINAL_SRC_ACMP0; - protectApt.evtPolarityMaskEx = APT_EM_ACMP0_INVERT_BIT; - protectApt.filterCycleNumEx = 0; - HAL_APT_ProtectInitEx(&g_apt1, &protectApt); -} - -static void APT1_Init(void) -{ - HAL_CRG_IpEnableSet(APT1_BASE, IP_CLK_ENABLE); - - g_apt1.baseAddress = APT1; - - /* Clock Settings */ - g_apt1.waveform.dividerFactor = 1 - 1; - /* Timer Settings */ - g_apt1.waveform.timerPeriod = 7500; /* 7500 is count period of APT time-base timer */ - g_apt1.waveform.cntMode = APT_COUNT_MODE_UP_DOWN; - - /* Wave Form */ - g_apt1.waveform.basicType = APT_PWM_BASIC_A_HIGH_B_LOW; - g_apt1.waveform.chAOutType = APT_PWM_OUT_BASIC_TYPE; - g_apt1.waveform.chBOutType = APT_PWM_OUT_BASIC_TYPE; - g_apt1.waveform.divInitVal = 0; - g_apt1.waveform.cntInitVal = 0; - g_apt1.waveform.cntCmpLeftEdge = 500; /* 500 is count compare point of the left edge of PWM waveform */ - g_apt1.waveform.cntCmpRightEdge = 4000; /* 4000 is count compare point of the right edge of PWM waveform */ - g_apt1.waveform.cntCmpLoadMode = APT_BUFFER_INDEPENDENT_LOAD; - g_apt1.waveform.cntCmpLoadEvt = APT_COMPARE_LOAD_EVENT_ZERO; - g_apt1.waveform.deadBandCnt = 225; /* 225 is dead-band value */ - - APT1_ProtectInit(); - - HAL_APT_PWMInit(&g_apt1); -} - -static void APT2_ProtectInit(void) -{ - APT_OutCtrlProtectEx protectApt = {0}; - protectApt.ocEventEnEx = BASE_CFG_ENABLE; - protectApt.ocEventModeEx = APT_OUT_CTRL_ONE_SHOT; - protectApt.ocActionEx = APT_OUT_CTRL_ACTION_LOW; - protectApt.ocActionBEx = APT_OUT_CTRL_ACTION_LOW; - protectApt.ocEvtInterruptEnEx = BASE_CFG_DISABLE; - protectApt.ocSysEvent = APT_SYS_EVT_DEBUG | APT_SYS_EVT_CLK | APT_SYS_EVT_MEM; - protectApt.originalEvtEx = APT_EM_ORIGINAL_SRC_ACMP0; - protectApt.evtPolarityMaskEx = APT_EM_ACMP0_INVERT_BIT; - protectApt.filterCycleNumEx = 0; - HAL_APT_ProtectInitEx(&g_apt2, &protectApt); -} - -static void APT2_Init(void) -{ - HAL_CRG_IpEnableSet(APT2_BASE, IP_CLK_ENABLE); - - g_apt2.baseAddress = APT2; - - /* Clock Settings */ - g_apt2.waveform.dividerFactor = 1 - 1; - /* Timer Settings */ - g_apt2.waveform.timerPeriod = 7500; /* 7500 is count period of APT time-base timer */ - g_apt2.waveform.cntMode = APT_COUNT_MODE_UP_DOWN; - - /* Wave Form */ - g_apt2.waveform.basicType = APT_PWM_BASIC_A_HIGH_B_LOW; - g_apt2.waveform.chAOutType = APT_PWM_OUT_BASIC_TYPE; - g_apt2.waveform.chBOutType = APT_PWM_OUT_BASIC_TYPE; - g_apt2.waveform.divInitVal = 0; - g_apt2.waveform.cntInitVal = 0; - g_apt2.waveform.cntCmpLeftEdge = 500; /* 500 is count compare point of the left edge of PWM waveform */ - g_apt2.waveform.cntCmpRightEdge = 4000; /* 4000 is count compare point of the right edge of PWM waveform */ - g_apt2.waveform.cntCmpLoadMode = APT_BUFFER_INDEPENDENT_LOAD; - g_apt2.waveform.cntCmpLoadEvt = APT_COMPARE_LOAD_EVENT_ZERO; - g_apt2.waveform.deadBandCnt = 225; /* 225 is dead-band value */ - - APT2_ProtectInit(); - - HAL_APT_PWMInit(&g_apt2); -} - -__weak void MotorStartStopKeyCallback(void *param) -{ - GPIO_Handle *handle = (GPIO_Handle *)param; - BASE_FUNC_UNUSED(handle); -} - -static void GPIO_Init(void) -{ - HAL_CRG_IpEnableSet(GPIO2_BASE, IP_CLK_ENABLE); - g_gpio2.baseAddress = GPIO2; - - g_gpio2.pins = GPIO_PIN_3; - HAL_GPIO_Init(&g_gpio2); - HAL_GPIO_SetDirection(&g_gpio2, g_gpio2.pins, GPIO_OUTPUT_MODE); - HAL_GPIO_SetValue(&g_gpio2, g_gpio2.pins, GPIO_HIGH_LEVEL); - HAL_GPIO_SetIrqType(&g_gpio2, g_gpio2.pins, GPIO_INT_TYPE_NONE); - - g_gpio2.pins = GPIO_PIN_4; - HAL_GPIO_Init(&g_gpio2); - HAL_GPIO_SetDirection(&g_gpio2, g_gpio2.pins, GPIO_INPUT_MODE); - HAL_GPIO_SetValue(&g_gpio2, g_gpio2.pins, GPIO_HIGH_LEVEL); - HAL_GPIO_SetIrqType(&g_gpio2, g_gpio2.pins, GPIO_INT_TYPE_LOW_LEVEL); - - HAL_CRG_IpEnableSet(GPIO1_BASE, IP_CLK_ENABLE); - g_gpio1.baseAddress = GPIO1; - - g_gpio1.pins = GPIO_PIN_0; - HAL_GPIO_Init(&g_gpio1); - HAL_GPIO_SetDirection(&g_gpio1, g_gpio1.pins, GPIO_OUTPUT_MODE); - HAL_GPIO_SetValue(&g_gpio1, g_gpio1.pins, GPIO_HIGH_LEVEL); - HAL_GPIO_SetIrqType(&g_gpio1, g_gpio1.pins, GPIO_INT_TYPE_NONE); - - HAL_GPIO_RegisterCallBack(&g_gpio2, GPIO_PIN_4, MotorStartStopKeyCallback); - IRQ_Register(IRQ_GPIO2, HAL_GPIO_IrqHandler, &g_gpio2); - IRQ_SetPriority(IRQ_GPIO2, 1); /* set gpio1 interrupt priority to 1, 1~15. 1 is priority value */ - IRQ_EnableN(IRQ_GPIO2); /* gpio interrupt enable */ - - return; -} - -static void PGA0_Init(void) -{ - HAL_CRG_IpEnableSet(PGA0_BASE, IP_CLK_ENABLE); - - g_pga0.baseAddress = PGA0_BASE; - g_pga0.externalResistorMode = BASE_CFG_ENABLE; - g_pga0.handleEx.extCapCompensation = PGA_EXT_COMPENSATION_2X; - HAL_PGA_Init(&g_pga0); -} - -static void PGA1_Init(void) -{ - HAL_CRG_IpEnableSet(PGA1_BASE, IP_CLK_ENABLE); - - g_pga1.baseAddress = PGA1_BASE; - g_pga1.externalResistorMode = BASE_CFG_ENABLE; - g_pga1.handleEx.extCapCompensation = PGA_EXT_COMPENSATION_2X; - HAL_PGA_Init(&g_pga1); -} - -__weak void CheckPotentiometerValueCallback(void *handle) -{ - BASE_FUNC_UNUSED(handle); - /* USER CODE BEGIN CheckPotentiometerValueCallback */ - /* USER CODE END CheckPotentiometerValueCallback */ -} - -static void TIMER0_Init(void) -{ - HAL_CRG_IpEnableSet(TIMER0_BASE, IP_CLK_ENABLE); /* TIMER0 clock enable. */ - unsigned int load = ((float)HAL_CRG_GetIpFreq((void *)TIMER0) / (1u << (TIMERPRESCALER_NO_DIV * 4)) / 1000000u) * 1000000; - - g_timer0.baseAddress = TIMER0; - g_timer0.load = (load < 1) ? 0 : (load - 1); /* Set timer value immediately */ - g_timer0.bgLoad = (load < 1) ? 0 : (load - 1); /* Set timer value */ - g_timer0.mode = TIMER_MODE_RUN_PERIODIC; /* Run in period mode */ - g_timer0.prescaler = TIMERPRESCALER_NO_DIV; /* Don't frequency division */ - g_timer0.size = TIMER_SIZE_32BIT; /* 1 for 32bit, 0 for 16bit */ - g_timer0.interruptEn = BASE_CFG_ENABLE; - g_timer0.adcSocReqEnable = BASE_CFG_DISABLE; - g_timer0.dmaReqEnable = BASE_CFG_DISABLE; - HAL_TIMER_Init(&g_timer0); - IRQ_Register(IRQ_TIMER0, HAL_TIMER_IrqHandler, &g_timer0); - - HAL_TIMER_RegisterCallback(&g_timer0, TIMER_PERIOD_FIN, CheckPotentiometerValueCallback); - IRQ_SetPriority(IRQ_TIMER0, 1); /* 1 is priority value */ - IRQ_EnableN(IRQ_TIMER0); -} - -__weak void MotorStatemachineCallBack(void *handle) -{ - BASE_FUNC_UNUSED(handle); - /* USER CODE BEGIN MotorStatemachineCallBack */ - /* USER CODE END MotorStatemachineCallBack */ -} - -static void TIMER1_Init(void) -{ - HAL_CRG_IpEnableSet(TIMER1_BASE, IP_CLK_ENABLE); /* TIMER1 clock enable. */ - unsigned int load = ((float)HAL_CRG_GetIpFreq((void *)TIMER1) / (1u << (TIMERPRESCALER_NO_DIV * 4)) / 1000000u) * 500; - - g_timer1.baseAddress = TIMER1; - g_timer1.load = (load < 1) ? 0 : (load - 1); /* Set timer value immediately */ - g_timer1.bgLoad = (load < 1) ? 0 : (load - 1); /* Set timer value */ - g_timer1.mode = TIMER_MODE_RUN_PERIODIC; /* Run in period mode */ - g_timer1.prescaler = TIMERPRESCALER_NO_DIV; /* Don't frequency division */ - g_timer1.size = TIMER_SIZE_32BIT; /* 1 for 32bit, 0 for 16bit */ - g_timer1.interruptEn = BASE_CFG_ENABLE; - g_timer1.adcSocReqEnable = BASE_CFG_DISABLE; - g_timer1.dmaReqEnable = BASE_CFG_DISABLE; - HAL_TIMER_Init(&g_timer1); - IRQ_Register(IRQ_TIMER1, HAL_TIMER_IrqHandler, &g_timer1); - - HAL_TIMER_RegisterCallback(&g_timer1, TIMER_PERIOD_FIN, MotorStatemachineCallBack); - IRQ_SetPriority(IRQ_TIMER1, 1); /* 1 is priority value */ - IRQ_EnableN(IRQ_TIMER1); -} - -__weak void UART0InterruptErrorCallback(void *handle) -{ - BASE_FUNC_UNUSED(handle); - /* USER CODE BEGIN UART0_TRNS_IT_ERROR */ - /* USER CODE END UART0_TRNS_IT_ERROR */ -} - -__weak void UART0_TXDMACallback(void *handle) -{ - BASE_FUNC_UNUSED(handle); - /* USER CODE BEGIN UART0_WRITE_DMA_FINISH */ - /* USER CODE END UART0_WRITE_DMA_FINISH */ -} - -__weak void UART0WriteInterruptCallback(void *handle) -{ - BASE_FUNC_UNUSED(handle); - /* USER CODE BEGIN UART0_WRITE_IT_FINISH */ - /* USER CODE END UART0_WRITE_IT_FINISH */ -} - -__weak void UART0ReadInterruptCallback(void *handle) -{ - BASE_FUNC_UNUSED(handle); - /* USER CODE BEGIN UART0_READ_IT_FINISH */ - /* USER CODE END UART0_READ_IT_FINISH */ -} - -static void UART0_Init(void) -{ - HAL_CRG_IpEnableSet(UART0_BASE, IP_CLK_ENABLE); /* UART0 clock enable. */ - g_uart0.baseAddress = UART0; - - g_uart0.baudRate = UART0_BAUD_RATE; - g_uart0.dataLength = UART_DATALENGTH_8BIT; - g_uart0.stopBits = UART_STOPBITS_ONE; - g_uart0.parity = UART_PARITY_NONE; - g_uart0.txMode = UART_MODE_DMA; - g_uart0.rxMode = UART_MODE_INTERRUPT; - g_uart0.fifoMode = BASE_CFG_ENABLE; - g_uart0.fifoTxThr = UART_FIFODEPTH_SIZE8; - g_uart0.fifoRxThr = UART_FIFODEPTH_SIZE8; - g_uart0.hwFlowCtr = BASE_CFG_DISABLE; - g_uart0.handleEx.overSampleMultiple = UART_OVERSAMPLING_16X; - g_uart0.handleEx.msbFirst = BASE_CFG_DISABLE; - HAL_UART_Init(&g_uart0); - HAL_UART_RegisterCallBack(&g_uart0, UART_TRNS_IT_ERROR, (UART_CallbackType)UART0InterruptErrorCallback); - HAL_UART_RegisterCallBack(&g_uart0, UART_READ_IT_FINISH, (UART_CallbackType)UART0ReadInterruptCallback); - IRQ_Register(IRQ_UART0, HAL_UART_IrqHandler, &g_uart0); - IRQ_SetPriority(IRQ_UART0, 6); /* 6 is priority value */ - IRQ_EnableN(IRQ_UART0); - g_uart0.dmaHandle = &g_dmac; - g_uart0.uartDmaTxChn = 0; - HAL_UART_RegisterCallBack(&g_uart0, UART_WRITE_DMA_FINISH, (UART_CallbackType)UART0_TXDMACallback); - HAL_UART_RegisterCallBack(&g_uart0, UART_WRITE_IT_FINISH, (UART_CallbackType)UART0WriteInterruptCallback); -} - -static void IOConfig(void) -{ - SYSCTRL0->SC_SYS_STAT.BIT.update_mode = 0; - SYSCTRL0->SC_SYS_STAT.BIT.update_mode_clear = 1; - /* Config PIN39 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO0_3_AS_UART0_TXD); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO0_3_AS_UART0_TXD, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO0_3_AS_UART0_TXD, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO0_3_AS_UART0_TXD, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO0_3_AS_UART0_TXD, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN40 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO0_4_AS_UART0_RXD); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO0_4_AS_UART0_RXD, PULL_UP); /* Pull-up and Pull-down, UART RX recommend PULL_UP */ - HAL_IOCMG_SetPinSchmidtMode(GPIO0_4_AS_UART0_RXD, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO0_4_AS_UART0_RXD, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO0_4_AS_UART0_RXD, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN1 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO0_7_AS_ACMP0_OUT); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO0_7_AS_ACMP0_OUT, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO0_7_AS_ACMP0_OUT, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO0_7_AS_ACMP0_OUT, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO0_7_AS_ACMP0_OUT, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN48 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO0_6_AS_ACMP_N2); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO0_6_AS_ACMP_N2, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO0_6_AS_ACMP_N2, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO0_6_AS_ACMP_N2, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO0_6_AS_ACMP_N2, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN47 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO0_5_AS_ACMP_P2); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO0_5_AS_ACMP_P2, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO0_5_AS_ACMP_P2, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO0_5_AS_ACMP_P2, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO0_5_AS_ACMP_P2, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN4 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO2_6_AS_PGA0_N0); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO2_6_AS_PGA0_N0, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO2_6_AS_PGA0_N0, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO2_6_AS_PGA0_N0, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO2_6_AS_PGA0_N0, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN5 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO2_5_AS_PGA0_P0); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO2_5_AS_PGA0_P0, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO2_5_AS_PGA0_P0, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO2_5_AS_PGA0_P0, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO2_5_AS_PGA0_P0, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN3 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO2_7_AS_PGA0_OUT); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO2_7_AS_PGA0_OUT, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO2_7_AS_PGA0_OUT, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO2_7_AS_PGA0_OUT, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO2_7_AS_PGA0_OUT, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN7 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO3_7_AS_ADC_AIN9); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO3_7_AS_ADC_AIN9, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO3_7_AS_ADC_AIN9, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO3_7_AS_ADC_AIN9, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO3_7_AS_ADC_AIN9, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN2 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO5_1_AS_ADC_AIN5); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO5_1_AS_ADC_AIN5, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO5_1_AS_ADC_AIN5, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO5_1_AS_ADC_AIN5, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO5_1_AS_ADC_AIN5, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN14 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO4_7_AS_ADC_AIN15); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO4_7_AS_ADC_AIN15, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO4_7_AS_ADC_AIN15, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO4_7_AS_ADC_AIN15, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO4_7_AS_ADC_AIN15, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN11 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO1_5_AS_PGA1_P0); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO1_5_AS_PGA1_P0, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO1_5_AS_PGA1_P0, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO1_5_AS_PGA1_P0, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO1_5_AS_PGA1_P0, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN12 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO1_6_AS_PGA1_N0); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO1_6_AS_PGA1_N0, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO1_6_AS_PGA1_N0, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO1_6_AS_PGA1_N0, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO1_6_AS_PGA1_N0, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN13 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO1_7_AS_PGA1_OUT); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO1_7_AS_PGA1_OUT, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO1_7_AS_PGA1_OUT, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO1_7_AS_PGA1_OUT, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO1_7_AS_PGA1_OUT, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN35 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO2_3_AS_GPIO2_3); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO2_3_AS_GPIO2_3, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO2_3_AS_GPIO2_3, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO2_3_AS_GPIO2_3, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO2_3_AS_GPIO2_3, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN27 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO1_0_AS_GPIO1_0); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO1_0_AS_GPIO1_0, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO1_0_AS_GPIO1_0, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO1_0_AS_GPIO1_0, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO1_0_AS_GPIO1_0, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN41 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO2_4_AS_GPIO2_4); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO2_4_AS_GPIO2_4, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO2_4_AS_GPIO2_4, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO2_4_AS_GPIO2_4, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO2_4_AS_GPIO2_4, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN19 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO3_0_AS_APT0_PWMA); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO3_0_AS_APT0_PWMA, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO3_0_AS_APT0_PWMA, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO3_0_AS_APT0_PWMA, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO3_0_AS_APT0_PWMA, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN23 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO4_0_AS_APT0_PWMB); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO4_0_AS_APT0_PWMB, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO4_0_AS_APT0_PWMB, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO4_0_AS_APT0_PWMB, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO4_0_AS_APT0_PWMB, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN20 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO3_1_AS_APT1_PWMA); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO3_1_AS_APT1_PWMA, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO3_1_AS_APT1_PWMA, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO3_1_AS_APT1_PWMA, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO3_1_AS_APT1_PWMA, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN24 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO4_1_AS_APT1_PWMB); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO4_1_AS_APT1_PWMB, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO4_1_AS_APT1_PWMB, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO4_1_AS_APT1_PWMB, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO4_1_AS_APT1_PWMB, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN21 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO3_2_AS_APT2_PWMA); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO3_2_AS_APT2_PWMA, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO3_2_AS_APT2_PWMA, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO3_2_AS_APT2_PWMA, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO3_2_AS_APT2_PWMA, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN25 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO4_2_AS_APT2_PWMB); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO4_2_AS_APT2_PWMB, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO4_2_AS_APT2_PWMB, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO4_2_AS_APT2_PWMB, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO4_2_AS_APT2_PWMB, DRIVER_RATE_2); /* Output signal edge fast/slow */ -} - -static void APT_SyncMasterInit(void) -{ - HAL_APT_MasterSyncInit(&g_apt0, APT_SYNC_OUT_ON_CNTR_ZERO); -} - -static void APT_SyncSlaveInit(void) -{ - APT_SlaveSyncIn aptSlave; - - aptSlave.cntPhase = 0; /* counter phase value */ - aptSlave.syncCntMode = APT_COUNT_MODE_AFTER_SYNC_UP; - aptSlave.syncInSrc = APT_SYNCIN_SRC_APT0_SYNCOUT; /* sync source selection */ - aptSlave.cntrSyncSrc = APT_CNTR_SYNC_SRC_SYNCIN; - HAL_APT_SlaveSyncInit(&g_apt1, &aptSlave); - - aptSlave.cntPhase = 0; /* counter phase value */ - aptSlave.syncCntMode = APT_COUNT_MODE_AFTER_SYNC_UP; - aptSlave.syncInSrc = APT_SYNCIN_SRC_APT0_SYNCOUT; /* sync source selection */ - aptSlave.cntrSyncSrc = APT_CNTR_SYNC_SRC_SYNCIN; - HAL_APT_SlaveSyncInit(&g_apt2, &aptSlave); - -} - -void SystemInit(void) -{ - IOConfig(); - DMA_Init(); - UART0_Init(); - ACMP0_Init(); - APT0_Init(); - APT1_Init(); - APT2_Init(); - ADC0_Init(); - PGA0_Init(); - PGA1_Init(); - TIMER0_Init(); - TIMER1_Init(); - GPIO_Init(); - - APT_SyncMasterInit(); - APT_SyncSlaveInit(); - - /* USER CODE BEGIN system_init */ - /* USER CODE END system_init */ -} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/E7050_FOC/user/main.c b/vendor/yibaina_3061M/demo/E7050_FOC/user/main.c deleted file mode 100644 index d6ab767c..00000000 --- a/vendor/yibaina_3061M/demo/E7050_FOC/user/main.c +++ /dev/null @@ -1,71 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file main.c - * @author MCU Driver Team - * @brief Main program body. - * @date 2025-04-14 14:05:10 - */ - -#include "typedefs.h" -#include "feature.h" -#include "mcs_motor_process.h" -#include "main.h" -/* USER CODE BEGIN 0 */ -/* USER CODE 区域内代码不会被覆盖,区域外会被生成的默认代码覆盖(其余USER CODE 区域同理) */ -/* 建议用户放置头文件 */ -/* USER CODE END 0 */ -ACMP_Handle g_acmp0; -PGA_Handle g_pga0; -PGA_Handle g_pga1; -TIMER_Handle g_timer0; -TIMER_Handle g_timer1; -UART_Handle g_uart0; -APT_Handle g_apt0; -APT_Handle g_apt1; -APT_Handle g_apt2; -ADC_Handle g_adc0; -DMA_Handle g_dmac; -GPIO_Handle g_gpio2; -GPIO_Handle g_gpio1; -/* USER CODE BEGIN 1 */ -/* 建议用户定义全局变量、结构体、宏定义或函数声明等 */ -/* USER CODE END 1 */ - -int main(void) -{ - /* USER CODE BEGIN 2 */ - /* 建议用户放置初始化代码或启动代码等 */ - /* USER CODE END 2 */ - MotorMainProcess(); - /* USER CODE BEGIN 3 */ - /* 建议用户放置初始配置代码 */ - /* USER CODE END 3 */ - while (1) { - /* USER CODE BEGIN 4 */ - /* 建议用户放置周期性执行代码 */ - /* USER CODE END 4 */ - } - /* USER CODE BEGIN 5 */ - /* 建议用户放置代码流程 */ - /* USER CODE END 5 */ - return BASE_STATUS_OK; -} - -/* USER CODE BEGIN 6 */ -/* 建议用户放置自定义函数 */ -/* USER CODE END 6 */ \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_carrier.h b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_carrier.h deleted file mode 100644 index dea3ad66..00000000 --- a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_carrier.h +++ /dev/null @@ -1,142 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_carrier.h - * @author MCU Algorithm Team - * @brief This file provides functions declaration for carrier interrupt processing function. - */ -#ifndef McuMagicTag_MCS_CARRIER_H -#define McuMagicTag_MCS_CARRIER_H - -#include "mcs_status.h" -#include "mcs_mtr_param.h" -#include "mcs_svpwm.h" -#include "mcs_curr_ctrl.h" -#include "mcs_if_ctrl.h" -#include "mcs_ramp_mgmt.h" -#include "mcs_spd_ctrl.h" -#include "mcs_fosmo.h" -#include "mcs_smo_4th.h" -#include "mcs_pll.h" -#include "mcs_startup.h" -#include "mcs_r1_svpwm.h" -#include "mcs_fw_ctrl.h" -#include "mcs_prot_user.h" - -typedef void (*MCS_ReadCurrUvwCb)(UvwAxis *CurrUvw); -typedef void (*MCS_SetPwmDutyCb)(UvwAxis *dutyUvwLeft, UvwAxis *dutyUvwRight); -typedef void (*MCS_SetADCTriggerTimeCb)(unsigned short cntCmpSOCA, unsigned short cntCmpSOCB); - -/** - * @brief motor control FSM state define. - * @details motor control FSM state type: - * + FSM_IDLE -- IDLE state, system startup default. - * + FSM_OFFSET_CALIB -- Self calibrate, for ADC init. - * + FSM_CAP_CHARGE -- IPM cap charge. - * + FSM_CLEAR -- Clear before every run. - * + FSM_LOCATE -- Rotor position pre-locate. - * + FSM_STARTUP -- Start up. - * + FSM_SWITCH -- Transition state, control from open to closed loop. - * + FSM_RUN -- Normal running state. - * + FSM_WAIT_STOP -- Wait stopping. - * + FSM_STOP -- Normal stop. - * + FSM_FAULT -- Fault state, waiting for user process. - */ -typedef enum { - FSM_IDLE = 0, - FSM_OFFSET_CALIB, - FSM_CAP_CHARGE, - FSM_CLEAR, - FSM_LOCATE, - FSM_STARTUP, - FSM_SWITCH, - FSM_RUN, - FSM_WAIT_STOP, - FSM_STOP, - FSM_FAULT -} FsmState; - -/** - * @brief Sampling mode. - */ -typedef enum { - DUAL_RESISTORS = 0, - SINGLE_RESISTOR = 1 -} SampleMode; - -/** - * @brief Motor control data structure - */ -typedef struct { - unsigned char motorStateFlag; /**< Motor start/stop status flag. */ - float spdCmdHz; /**< External input speed command value. */ - float axisAngle; /**< Angle of the synchronous coordinate system. */ - float spdRefHz; /**< Command value after speed ramp management. */ - float currCtrlPeriod; /**< current loop control period. */ - float adc0Compensate; /**< ADC0 softwaretrim compensate value. */ - float adc1Compensate; /**< ADC1 softwaretrim compensate value. */ - float udc; /**< Bus voltage. */ - float powerBoardTemp; /**< Power boart surface temperature. */ - unsigned short aptMaxcntCmp; /**< Apt Maximum Comparison Count. */ - float adcCurrCofe; /**< Adc current sampling cofeature. */ - - unsigned short sysTickCnt; /**< System Timer Tick Count. */ - unsigned short capChargeTickNum; /**< Bootstrap Capacitor Charge Tick Count. */ - volatile unsigned int msTickCnt; /**< Millisecond-level counter, which can be used in 1-ms and 5-ms tasks. */ - unsigned short msTickNum; /**< Number of ticks corresponding to 1 ms. */ - char obserType; /**< Set Observer Type. */ - char controlMode; /**< Set foc control or sixstep bldc control mode or others. */ - char spdAdjustMode; /**< Set speed adjust mode. */ - char uartConnectFlag; /**< Uart connect success flag. */ - short uartHeartDetCnt; /**< Uart connect heart detect count. */ - float uartTimeStamp; /**< Uart data time stamp. */ - SysStatusReg statusReg; /**< System status. */ - FsmState stateMachine; /**< Motor Control State Machine. */ - - SampleMode sampleMode; /**< Current sampling mode setting: single resistor, double resistor. */ - MOTOR_Param mtrParam; /**< Motor parameters. */ - FOSMO_Handle smo; /**< SMO observer handle. */ - SMO4TH_Handle smo4th; /**< SMO 4th observer handle. */ - IF_Handle ifCtrl; /**< I/F control handle. */ - SVPWM_Handle sv; /**< Dual-resistance/three-resistance sampling SVPWM Handle. */ - R1SVPWM_Handle r1Sv; /**< Single-resistance phase-shifted SVPWM handle. */ - RMG_Handle spdRmg; /**< Ramp management struct for the speed controller input reference. */ - SPDCTRL_Handle spdCtrl; /**< Speed loop Control Handle. */ - CURRCTRL_Handle currCtrl; /**< Current loop control handle. */ - STARTUP_Handle startup; /**< Startup Switch Handle. */ - FW_Handle fw; /**< Flux-Weakening Handle. */ - - DqAxis idqRef; /**< Command value of the dq axis current. */ - UvwAxis currUvw; /**< Three-phase current sampling value. */ - AlbeAxis iabFbk; /**< αβ-axis current feedback value. */ - DqAxis idqFbk; /**< Current feedback value of the dq axis. */ - DqAxis vdqRef; /**< Current loop output dq voltage. */ - AlbeAxis vabRef; /**< Current loop output voltage αβ. */ - UvwAxis dutyUvw; /**< UVW three-phase duty cycle. */ - UvwAxis dutyUvwLeft; /**< Single Resistor UVW Three-Phase Left Duty Cycle. */ - UvwAxis dutyUvwRight; /**< Single Resistor UVW Three-Phase Right Duty Cycle. */ - - MCS_ReadCurrUvwCb readCurrUvwCb; /**< Read current callback function. */ - MCS_SetPwmDutyCb setPwmDutyCb; /**< Set the duty cycle callback function. */ - MCS_SetADCTriggerTimeCb setADCTriggerTimeCb; /**< Sets the ADC trigger point callback function. */ - - MotorProtStatus_Handle prot; /**< Motor protect status detection flag bit handle. */ -} MTRCTRL_Handle; - -void MCS_CarrierProcess(MTRCTRL_Handle *mtrCtrl); - -#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_chip_config.h b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_chip_config.h deleted file mode 100644 index 38c00b5d..00000000 --- a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_chip_config.h +++ /dev/null @@ -1,76 +0,0 @@ -/** - * @ Copyright (c) HiSilicon (Shanghai) Technologies Co., Ltd. 2022-2023. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_chip_config.h - * @author MCU Algorithm Team - * @brief This file provides config macros for ECMCU105H/ECBMCU201MPC app. - */ - -#ifndef McuMagicTag_MCS_CHIPCONFIG_H -#define McuMagicTag_MCS_CHIPCONFIG_H - -#include "feature.h" - -#if defined (CHIP_3061MNPICA) || defined (CHIP_3061MNPIKA) || defined (CHIP_3061MNNICA) || \ - defined (CHIP_3061MNNIKA) || defined (CHIP_3061MNPIC8) || defined(CHIP_3061MNNIC8) || \ - defined (CHIP_3061MNPIK8) || defined (CHIP_3061MNNIK8) - - #define ADCPTT_HANDLE g_adc0 - #define ADCRESIS_HANDLE g_adc0 - #define ADCUDC_HANDLE g_adc0 - #define ADCU_HANDLE g_adc0 - #define ADCW_HANDLE g_adc0 - #define LED1_HANDLE g_gpio2 - #define LED2_HANDLE g_gpio1 - #define LED2_PIN GPIO_PIN_0 - #define LED1_PIN GPIO_PIN_3 - #define ADCPTTSOCNUM ADC_SOC_NUM0 - #define ADCUSOCNUM ADC_SOC_NUM1 - #define ADCRESISSOCNUM ADC_SOC_NUM3 - #define ADCUDCSOCNUM ADC_SOC_NUM4 - #define ADCWSOCNUM ADC_SOC_NUM6 - - #define ADC0COMPENSATE 2037.0f - #define ADC1COMPENSATE 2027.0f - -#endif - -#if defined (CHIP_3065HRPIRZ) || defined (CHIP_3065ARPIRZ) || defined (CHIP_3066MNPIRH) || \ - defined (CHIP_3065PNPIRH) || defined (CHIP_3065PNPIRE) || defined (CHIP_3065PNPIRA) - - #define ADCU_HANDLE g_adc0 - #define ADCW_HANDLE g_adc1 - #define ADCRESIS_HANDLE g_adc2 - #define ADCUDC_HANDLE g_adc2 - #define ADCPTT_HANDLE g_adc2 - #define LED1_HANDLE g_gpio0 - #define LED2_HANDLE g_gpio0 - #define LED2_PIN GPIO_PIN_6 - #define LED1_PIN GPIO_PIN_7 - #define ADCPTTSOCNUM ADC_SOC_NUM0 - #define ADCRESISSOCNUM ADC_SOC_NUM1 - #define ADCUSOCNUM ADC_SOC_NUM8 - #define ADCWSOCNUM ADC_SOC_NUM8 - #define ADCUDCSOCNUM ADC_SOC_NUM14 - - #define ADC0COMPENSATE 2033.0f - #define ADC1COMPENSATE 2070.0f - -#endif - - -#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_ctlmode_config.h b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_ctlmode_config.h deleted file mode 100644 index 2a6f38cc..00000000 --- a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_ctlmode_config.h +++ /dev/null @@ -1,73 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_ctlmode_config.h - * @author MCU Algorithm Team - * @brief This file provides config macros for ECMCU105H app. - */ - - /* Define to prevent recursive inclusion ------------------------------------------------------- */ -#ifndef McuMagicTag_MCS_CTLMODECONFIG_H -#define McuMagicTag_MCS_CTLMODECONFIG_H - -#include "debug.h" -#include "typedefs.h" - -typedef enum { - FOC_STARTUP_IF = 0, - FOC_STARTUP_VF, - FOC_STARTUP_HFI -} MOTOR_STARTUPMODE_CONFIG; - -typedef enum { - FOC_OBSERVERTYPE_SMO1TH = 0, - FOC_OBSERVERTYPE_SMO1TH_PLL, - FOC_OBSERVERTYPE_SMO4TH, - FOC_OBSERVERTYPE_SMO4TH_PLL, - FOC_OBSERVERTYPE_LUNBORG, - FOC_OBSERVERTYPE_FLUX -} MOTOR_OBSERVERTYPE_CONFIG; - -typedef enum { - FOC_CONTROLMODE_SPEED = 0, - FOC_CONTROLMODE_TORQUE -} MOTOR_CONTROLMODE_CONFIG; - -typedef enum { - FOC_CURQAXISPID_PARAMS = 0, - FOC_CURDAXISPID_PARAMS, - FOC_SPDPID_PARAMS -} MOTOR_PID_SET; - -typedef enum { - MOTOR_PARAMS_BASE = 0, - MOTOR_PARAMS_SPECIAL, - MOTOR_PARAMS_BOARD -} MOTOR_PARAMS_SET; - -typedef enum { - CUST_SPEED_ADJUST = 0, - HOST_SPEED_ADJUST -} MODE_ADSPEED_CONFIG; - -typedef enum { - CONNECTING = 0, - CONNECTED, - DISCONNECT -} UART_STATUS; - -#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_motor_process.h b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_motor_process.h deleted file mode 100644 index 783bf214..00000000 --- a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_motor_process.h +++ /dev/null @@ -1,45 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_motor_process.h - * @author MCU Algorithm Team - * @brief This file provides motor sample functions declaration for ECMCU105H board. - */ -#ifndef McuMagicTag_MCS_MOTOR_PROCESS_H -#define McuMagicTag_MCS_MOTOR_PROCESS_H - -/** - * @brief phase sequence. - */ -typedef enum { - PHASE_U = 0, - PHASE_V, - PHASE_W, - PHASE_MAX_NUM -} MCS_PhaseEnum; - -/** - * @brief key state. - */ -typedef enum { - KEY_DOWN = 0, - KEY_UP = 1, -} KEY_State; - -int MotorMainProcess(void); - -#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_status.h b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_status.h deleted file mode 100644 index 19510c78..00000000 --- a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_status.h +++ /dev/null @@ -1,188 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_status.h - * @author MCU Algorithm Team - * @brief This file provides functions declaration of system status. - */ - -/* Define to prevent recursive inclusion ------------------------------------------------------- */ -#ifndef McuMagicTag_MCS_STATUS_H -#define McuMagicTag_MCS_STATUS_H - -/* Includes ------------------------------------------------------------------------------------ */ -#include "typedefs.h" -#include "mcs_assert.h" - -/* Typedef definitions ------------------------------------------------------------------------- */ -/** - * @brief System status define - */ -typedef union { - unsigned short all; - struct { - unsigned short cmdStart : 1; /**< Indicates that a start system command has been received. */ - unsigned short cmdStop : 1; /**< Indicates that a stop system command has been received. */ - unsigned short isRunning : 1; /**< Indicates that the system is running (enable signal) */ - unsigned short sysError : 1; /**< Indicates that the system reports an error. */ - unsigned short poweron : 1; /**< Indicates that the power-on initialization phase is complete. */ - unsigned short capcharge : 1; /**< Indicates that the bootstrap capacitor charging phase is complete. */ - unsigned short adczero : 1; /**< The current sampling point is reset to zero after power-on. */ - } Bit; -} SysStatusReg; - -/** - * @brief Get status of Bit cmdStart. - * @param sysStatus System status register handle. - * @retval Status of Bit cmdStart. - */ -static inline bool SysGetCmdStart(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - bool ret; - ret = (sysStatus->Bit.cmdStart == 1) ? true : false; - return ret; -} - -/** - * @brief Set Bit cmdStart. - * @param sysStatus System status register handle. - * @retval None. - */ -static inline void SysCmdStartSet(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - sysStatus->Bit.cmdStart = 1; -} - -/** - * @brief Clear Bit cmdStart. - * @param handle System status register handle. - * @retval None. - */ -static inline void SysCmdStartClr(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - sysStatus->Bit.cmdStart = 0; -} - -/** - * @brief Get status of Bit cmdStop. - * @param sysStatus System status register handle. - * @retval Status of Bit cmdStart. - */ -static inline bool SysGetCmdStop(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - bool ret; - ret = (sysStatus->Bit.cmdStop == 1) ? true : false; - return ret; -} - -/** - * @brief Set Bit cmdStop. - * @param sysStatus System status register handle. - * @retval None. - */ -static inline void SysCmdStopSet(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - sysStatus->Bit.cmdStop = 1; -} - -/** - * @brief Clear Bit cmdStop. - * @param sysStatus System status register handle. - * @retval None. - */ -static inline void SysCmdStopClr(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - sysStatus->Bit.cmdStop = 0; -} - -/** - * @brief Get status of Bit isRunning. - * @param sysStatus System status register handle. - * @retval Status of Bit isRunning. - */ -static inline bool SysIsRunning(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - bool ret; - ret = (sysStatus->Bit.isRunning == 1) ? true : false; - return ret; -} - -/** - * @brief Set Bit isRuning. - * @param sysStatus System status register handle. - * @retval None. - */ -static inline void SysRunningSet(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - sysStatus->Bit.isRunning = 1; -} - -/** - * @brief Clear Bit isRuning. - * @param sysStatus System status register handle. - * @retval None. - */ -static inline void SysRunningClr(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - sysStatus->Bit.isRunning = 0; -} - -/** - * @brief Get status of Bit sysError. - * @param sysStatus System status register handle. - * @retval Status of Bit sysError. - */ -static inline bool SysIsError(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - bool ret; - ret = (sysStatus->Bit.sysError == 1) ? true : false; - return ret; -} - -/** - * @brief Set Bit sysError. - * @param sysStatus System status register handle. - * @retval None. - */ -static inline void SysErrorSet(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - sysStatus->Bit.sysError = 1; -} - -/** - * @brief Clear Bit sysError. - * @param sysStatus System status register handle. - * @retval None. - */ -static inline void SysErrorClr(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - sysStatus->Bit.sysError = 0; -} - -#endif diff --git a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_user_config.h b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_user_config.h deleted file mode 100644 index 8d4cb216..00000000 --- a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_user_config.h +++ /dev/null @@ -1,108 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_user_config.h - * @author MCU Algorithm Team - * @brief This file provides functions declaration of user config parameters. - */ -#ifndef McuMagicTag_MCS_CONFIG_H -#define McuMagicTag_MCS_CONFIG_H - -#include "debug.h" -#include "typedefs.h" - -#define SMO4TH - -#define SYSTICK_PERIOD_US 500u /* systick period. */ - -#define INV_CAP_CHARGE_MS 3u /* Capacitor charging time. */ - -#define INV_VOLTAGE_BUS 24.0f /* Bus voltage, V. */ - -#define CTRL_CURR_PERIOD 0.0001f /* carrier ISR period, 100us. */ -#define CTRL_SYSTICK_PERIOD 0.0005f /* systick control period, 500us. */ - -/* Duty of sample window, the real time is 0.06*50us = 3us. */ -#define SAMPLE_WINDOW_DUTY 0.06f - -/* Duty of sample point shift as flip point, the real time is 0.008*50us = 0.4us. */ -#define SAMPLE_POINT_SHIFT 0.008f - -/* Sampling resistance 200mOhm 0.0013295 */ -#define ADC_CURR_COFFI 0.0013295f /* 3.3/4096/3.03/0.2 pga: 3.03, 200mohm. */ - -#define APT_U APT0_BASE /* Base address of U phase APT module. */ -#define APT_V APT1_BASE /* Base address of V phase APT module. */ -#define APT_W APT2_BASE /* Base address of W phase APT module. */ - -/* FOSMO */ -#define FOSMO_GAIN 4.0f /* SMO gain */ -#define FOSMO_LAMBDA 2.0f /* SMO coefficient of cut-off frequency, its value = lambda * we. */ -#define FOSMO_EMF_CUTOFF_FREQ 2.0f /* SMO back emf cutoff frequency. */ -#define SPEED_FILTER_CUTOFF_FREQUENCY 40.0f /* SMO speed cutoff frequency of speed filter. */ -#define FOSMO_PLL_BDW 30.0f /* SMO PLL bandwidth. */ - -/* SMO4TH */ -#define SMO4TH_PLL_BDW 30.0f /* SMO4TH PLL Bandwidth. */ -#define SMO4TH_KD 300.0f /* SMO4TH parameters KD. */ -#define SMO4TH_KQ 600.0f /* SMO4TH parameters KQ. */ -#define SMO4TH_SPD_FILTER_CUTOFF_FREQ 40.0f /* SMO4TH speed cutoff frequency of speed filter. */ - -/* User_Commond */ -#define CTRL_IF_CURR_AMP_A 0.7f /* IF control current amplitude. */ -#define USER_TARGET_SPD_HZ 100.0f /* User-defined target speed value. */ -#define USER_SWITCH_SPDBEGIN_HZ 30.0f /* Start of handover interval. */ -#define USER_SWITCH_SPDEND_HZ (USER_SWITCH_SPDBEGIN_HZ + 3.0f) /* End of handover period. */ -#define USER_MAX_SPD_HZ 29.0f /* User-defined maximum speed value. */ -#define USER_MIN_SPD_HZ 5.0f /* User-defined minimum speed value. */ -#define USER_SPD_SLOPE 50.0f /* slope of velocity change. */ -#define USER_CURR_SLOPE (CTRL_IF_CURR_AMP_A * 10.0f) /* Current change slope. */ - -/* PID PARAMS */ -#define CURRQAXIS_KP 5.023202f /* Current loop Q axis Kp. */ -#define CURRQAXIS_KI 20612.84f /* Current loop Q axis Ki. */ -#define CURRDAXIS_KP 3.477114f /* Current loop D axis Kp. */ -#define CURRDAXIS_KI 20612.84f /* Current loop D axis Ki. */ -/* Current loop PID output lower limit. */ -#define CURR_LOWERLIM (-INV_VOLTAGE_BUS * ONE_DIV_SQRT3 * 0.98f) -/* Current loop PID output upper limit. */ -#define CURR_UPPERLIM (INV_VOLTAGE_BUS * ONE_DIV_SQRT3 * 0.98f) - -#define SPD_KP 0.00505f /* Speed loop Kp. */ -#define SPD_KI 0.012f /* Speed loop Ki. */ -#define SPD_LOWERLIM (-0.105f) /* Speed loop PID output lower limit. */ -#define SPD_UPPERLIM 0.105f /* Speed loop PID output upper limit. */ - -/* MOTOR PARAMS */ -/* Np, Rs, Ld, Lq, Psif, J, Nmax, Currmax, PPMR, zShift. */ -/* mtrPsif & mtrJ parameter is not used in this project, temporarily set to 0. */ -#define MOTORPARAM_DEFAULTS { \ - .mtrNp = 14, \ - .mtrRs = 10.1f, \ - .mtrLd = 0.010f, \ - .mtrLq = 0.010f, \ - .mtrPsif = 0.0f, \ - .mtrJ = 0.0f, \ - .maxElecSpd = 29.0f, \ - .maxCurr = 1.0f, \ - .busVolt = INV_VOLTAGE_BUS, \ -} - -/* Bus voltage sampling conversion factor. */ -#define ADC_UDC_COFFI 0.01289f /* 0.01289 = 3.3/4096*192/12. */ - -#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_cmm.c b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_cmm.c deleted file mode 100644 index 99766bdf..00000000 --- a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_cmm.c +++ /dev/null @@ -1,43 +0,0 @@ -/** - * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_prot_cmm.c - * @author MCU Algorithm Team - * @brief This file contains protection common api definition. - */ - -/* Includes ------------------------------------------------------------------------------------ */ -#include "mcs_prot_cmm.h" -#include "apt.h" -#include "mcs_assert.h" - -/** - * @brief Safty-pulse-off function execution to turn off all the power devices. - * @param aptAddr Three-phase APT address pointer. - * @retval None. - */ -void ProtSpo_Exec(APT_RegStruct **aptAddr) -{ - MCS_ASSERT_PARAM(aptAddr != NULL); - /**< Turn off all the six power devices of the inverter. */ - for (unsigned int i = 0; i < MOTOR_PHASE_NUMBER; i++) { - APT_RegStruct *aptx = (APT_RegStruct *)(aptAddr[i]); - DCL_APT_EnableSwContPWMAction(aptx, APT_PWM_CHANNEL_A); - DCL_APT_EnableSwContPWMAction(aptx, APT_PWM_CHANNEL_B); - DCL_APT_ForcePWMOutputLow(aptx); - } -} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_cmm.h b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_cmm.h deleted file mode 100644 index 104a5dec..00000000 --- a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_cmm.h +++ /dev/null @@ -1,136 +0,0 @@ -/** - * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_prot_cmm.h - * @author MCU Algorithm Team - * @brief This file contains protection function common data struct and api declaration. - */ - -/* Define to prevent recursive inclusion ------------------------------------------------------- */ -#ifndef McuMagicTag_MCS_PROT_CMM_H -#define McuMagicTag_MCS_PROT_CMM_H - -#include "typedefs.h" -#include "apt_ip.h" - -/* Macro definitions --------------------------------------------------------------------------- */ -#define MOTOR_PHASE_NUMBER (3) - -#define PROT_VAL_THRESHOLD_NUMS (4) -#define PROT_VAL_THRESHOLD_0 (0) -#define PROT_VAL_THRESHOLD_1 (1) -#define PROT_VAL_THRESHOLD_2 (2) -#define PROT_VAL_THRESHOLD_3 (3) - -#define PROT_LIMIT_TIME_NUMS (3) -#define PROT_LIMIT_TIME_0 (0) -#define PROT_LIMIT_TIME_1 (1) -#define PROT_LIMIT_TIME_2 (2) - -#define MOTOR_PHASE_NUMBER (3) - -/**< Motor error status definition. */ -typedef union { - int all; - struct { - unsigned short overCurrErr : 1; /**< Indicates that phase current(s) is over protected value. */ - unsigned short overVoltErr : 1; /**< Indicates that dc-link voltage is over protected value. */ - unsigned short lowerVoltErr : 1; /**< Indicates that dc-link voltage is lower than protected value */ - unsigned short overIpmTempErr : 1; /**< Indicates that IPM temperature is over protected value. */ - unsigned short revRotErr : 1; /**< Indicates that motor negtive direction. */ - unsigned short motorStalling : 1; /**< Indicates that rotor is stalling. */ - unsigned short overMotorTempErr : 1; /**< Indicates that three phase currents is out-of-balance. */ - unsigned short posSnsrCommsErr : 1; /**< Indicates that position sensor communication is lost with MCU. */ - unsigned short posSnsrFuncErr : 1; /**< Indicates that position sensor reports function error. */ - unsigned short posSnsrCalibrErr : 1; /**< Indicates that position sensor fails to calibrate itself. */ - unsigned short currOutOfBalance : 1; /**< Indicates that the rotor is reverse rotation.*/ - unsigned short phsOpenErr : 1; /**< Indicates that phase winding(s) is open. */ - unsigned short phsU : 1; /**< Indicates that u phase fails when phsOpenErr occurs. */ - unsigned short phsV : 1; /**< Indicates that v phase fails when phsOpenErr occurs. */ - unsigned short phsW : 1; /**< Indicates that w phase fails when phsOpenErr occurs. */ - unsigned short multiPhs : 1; /**< Indicates that multi-phases fail when phsOpenErr occurs.*/ - } Bit; -} MotorErrStatusReg; - -/**< Protection Status Bit Definition */ -typedef enum { - OCP_ERR_BIT, - OVP_ERR_BIT, - LVP_ERR_BIT, - OTP_IPM_ERR_BIT, - OTP_MOTOR_ERR_BIT, - STALLING_ERR_BIT, - CURR_OUT_BALANCE_ERR_BIT, - POS_COMMS_ERR_BIT, - POS_FUNC_ERR_BIT, - POS_CALIB_ERR_BIT, - REV_ROT_ERR_BIT, - PHS_OPEN_ERR_BIT, - PHS_U_ERR_BIT, - PHS_V_ERR_BIT, - PHS_W_ERR_BIT, - PHS_MULTI_ERR_BIT, -} PROT_ErrBit; - -/**< Motor error protection level. */ -typedef enum { - PROT_LEVEL_0 = 0, - PROT_LEVEL_1, - PROT_LEVEL_2, - PROT_LEVEL_3, - PROT_LEVEL_4 /**< The greater level number, the severe error is. */ -} PROT_Level; - -/** - * @brief Obtains the status of a bit of data. - * @param data data. - * @param bits Number of digits. - * @retval Bit status. - */ -static inline bool GetBit(int data, unsigned short bit) -{ - bool ret; - ret = ((data >> bit) & 1); - return ret; -} - -/** - * @brief Sets the status of a bit of data. - * @param data data. - * @param bit The setted bit. - * @retval None. - */ -static inline void SetBit(int *data, unsigned char bit) -{ - *data |= (1 << bit); -} - -/** - * @brief Clear the status of a bit of data. - * @param data data. - * @param bit The Clear bit. - * @retval None. - */ -static inline void ClearBit(int *data, unsigned char bit) -{ - *data &= ~(1 << bit); -} - -/**< Protection action. */ -void ProtSpo_Exec(APT_RegStruct **aptAddr); - -#endif diff --git a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user.c b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user.c deleted file mode 100644 index e97876b7..00000000 --- a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user.c +++ /dev/null @@ -1,137 +0,0 @@ -/** - * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_prot_user.c - * @author MCU Algorithm Team - * @brief This file contains user protection data struct, inquiry api and initialization api definition. - */ - -/* Includes ------------------------------------------------------------------------------------ */ -#include "mcs_prot_user.h" -#include "mcs_prot_user_config.h" -#include "mcs_assert.h" - -/** - * @brief Get motor over current error status. - * @param motorErrStatus Motor error status. - * @retval Error status. - */ -bool IsMotorOverCurrErr(MotorErrStatusReg motorErrStatus) -{ - if (motorErrStatus.Bit.overCurrErr) { - return true; - } else { - return false; - } -} - -/** - * @brief Get motor over dc-link voltage error status. - * @param motorErrStatus Motor error status. - * @retval Error status. - */ -bool IsDcLinkOverVoltErr(MotorErrStatusReg motorErrStatus) -{ - if (motorErrStatus.Bit.overVoltErr) { - return true; - } else { - return false; - } -} - -/** - * @brief Get motor lower dc-link voltage error status. - * @param motorErrStatus Motor error status. - * @retval Error status. - */ -bool IsDcLinkLowerVoltErr(MotorErrStatusReg motorErrStatus) -{ - if (motorErrStatus.Bit.lowerVoltErr) { - return true; - } else { - return false; - } -} - -/** - * @brief Get motor over Ipm temperature error status. - * @param motorErrStatus Motor error status. - * @retval Error status. - */ -bool IsIpmOverTempErr(MotorErrStatusReg motorErrStatus) -{ - if (motorErrStatus.Bit.overIpmTempErr) { - return true; - } else { - return false; - } -} - -/** - * @brief Get motor over Motor temperature error status. - * @param motorErrStatus Motor error status. - * @retval Error status. - */ -bool IsMotorOverTempErr(MotorErrStatusReg motorErrStatus) -{ - if (motorErrStatus.Bit.overMotorTempErr) { - return true; - } else { - return false; - } -} - -/** - * @brief Get motor stalling error status. - * @param motorErrStatus Motor error status. - * @retval Error status. - */ -bool IsMotorStallingErr(MotorErrStatusReg motorErrStatus) -{ - if (motorErrStatus.Bit.motorStalling) { - return true; - } else { - return false; - } -} - -/** - * @brief Clear the motor error status. - * @param motorProt Motor protection handle. - * @retval Error status. - */ -void ClearMotorErrStatus(MotorProtStatus_Handle *motorProt) -{ - MCS_ASSERT_PARAM(motorProt != NULL); - /* Clear the motor error status. */ - motorProt->motorErrStatus.all = 0x00; - OCP_Clear(&motorProt->ocp); - OVP_Clear(&motorProt->ovp); - LVP_Clear(&motorProt->lvp); - OTP_Clear(&motorProt->otp); -} - -/** - * @brief Motor protection function initialization. - * @param motorProt Motor protection handle. - * @retval Error status. - */ -void MotorProt_Init(MotorProtStatus_Handle *motorProt) -{ - MCS_ASSERT_PARAM(motorProt != NULL); - motorProt->motorErrStatus.all = 0x00; -} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user.h b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user.h deleted file mode 100644 index 365110e1..00000000 --- a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user.h +++ /dev/null @@ -1,54 +0,0 @@ -/** - * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_prot_user.h - * @author MCU Algorithm Team - * @brief This file contains user protection data struct, inquiry api and initialization api declaration. - */ - -/* Define to prevent recursive inclusion ------------------------------------------------------- */ -#ifndef McuMagicTag_MCS_PROT_USER_H -#define McuMagicTag_MCS_PROT_USER_H - -/* Includes ------------------------------------------------------------------------------------ */ -#include "mcs_prot_cmm.h" -#include "mcs_curr_prot.h" -#include "mcs_dc_volt_prot.h" -#include "mcs_temp_prot.h" -#include "mcs_motor_stalling.h" -#include "typedefs.h" - -typedef struct { - MotorErrStatusReg motorErrStatus; /**< Motor error status. */ - OCP_Handle ocp; /**< Over current protection. */ - OVP_Handle ovp; /**< Over dc-link voltage protection. */ - LVP_Handle lvp; /**< Lower dc-link voltage protection. */ - OTP_Handle otp; /**< Over IPM temperature protection. */ - STP_Handle stall; /**< Motor stalling protection. */ -} MotorProtStatus_Handle; - -void MotorProt_Init(MotorProtStatus_Handle *motorProt); - -/**< Inquiry motor error status */ -bool IsMotorOverCurrErr(MotorErrStatusReg motorErrStatus); -bool IsDcLinkOverVoltErr(MotorErrStatusReg motorErrStatus); -bool IsDcLinkLowerVoltErr(MotorErrStatusReg motorErrStatus); -bool IsIpmOverTempErr(MotorErrStatusReg motorErrStatus); -bool IsMotorOverTempErr(MotorErrStatusReg motorErrStatus); -bool IsMotorStallingErr(MotorErrStatusReg motorErrStatus); -void ClearMotorErrStatus(MotorProtStatus_Handle *motorProt); -#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user_config.h b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user_config.h deleted file mode 100644 index 832cd1be..00000000 --- a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user_config.h +++ /dev/null @@ -1,125 +0,0 @@ -/** - * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_prot_user_config.h - * @author MCU Algorithm Team - * @brief This file contans user macro definition of the protection function. - */ - -/* Define to prevent recursive inclusion ------------------------------------------------------- */ -#ifndef McuMagicTag_MCS_PROT_USER_CONFIG_H -#define McuMagicTag_MCS_PROT_USER_CONFIG_H - -/* Macro definitions --------------------------------------------------------------------------- */ -/**< COMMON */ -/**< Peak phase current (A) of the motor or IPM under continuous operations. */ -#define PROT_MOTOR_RATED_CURR (1.0f) -/**< Only several continuous fault detection can trigger error status. */ -#define PROT_CNT_LIMIT (100) -/**< Only several contunuous none fault dectection can trigger elimination of error status. */ -#define RECY_CNT_LIMIT (10000) -/**< Only several contunuous none fault dectection can trigger elimination of error status. */ -#define OVER_VOLT_RECY_CNT_LIMIT (100) -/**< Only several contunuous none fault dectection can trigger elimination of error status. */ -#define LOWER_VOLT_RECY_CNT_LIMIT (100) - -/**< Over current protection */ -/**< Over current trigger value (A) when in level 1. */ -#define PROT_OVER_CURR_POW_DN1 (2.0f * PROT_MOTOR_RATED_CURR) -/**< Over current trigger value (A) when in level 2. */ -#define PROT_OVER_CURR_POW_DN2 (2.0f * PROT_MOTOR_RATED_CURR) -/**< Over current trigger value (A) when in level 3. */ -#define PROT_OVER_CURR_POW_DN3 (2.0f * PROT_MOTOR_RATED_CURR) -/**< Over current trigger value (A) when in level 4. */ -#define PROT_OVER_CURR_POW_OFF (2.0f * PROT_MOTOR_RATED_CURR) -#define PROT_OVER_CURR_RECY_DELTA (0.0035f) /**< Current gap (A) when recovers from protection status. */ -#define PROT_OVER_CURR_LIMIT1_TIME_SEC (30.0f) /**< 20% overload can last maximum time: 30 sec. */ -#define PROT_OVER_CURR_LIMIT2_TIME_SEC (10.0f) /**< 20% overload can last maximum time: 10 sec. */ -#define PROT_OVER_CURR_LIMIT3_TIME_SEC (2.0f) /**< 20% overload can last maximum time: 2 sec. */ - -/**< Over voltage protection */ -#define PROT_OVER_VOLT_BRK_ON1 (26.0f) /**<处于1级时,直流链路电压触发值(V)过高*/ -#define PROT_OVER_VOLT_BRK_ON2 (27.0f) /**< Over dc-link voltage trigger value (V) when in level 2. */ -#define PROT_OVER_VOLT_BRK_ON3 (28.0f) /**< Over dc-link voltage trigger value (V) when in level 3. */ -#define PROT_OVER_VOLT_BRK_ALL (30.0f) /**< Over dc-link voltage trigger value (V) when in level 4. */ -#define PROT_OVER_VOLT_RECY_DELTA (0.5f) /**<从保护状态恢复时的电压间隙(V)*/ -#define PROT_OVER_VOLT_LIMIT1_TIME_SEC (5.0f) /** 1; One-cycle mode: < 0.5 */ -#define POS_SNSR_IPD_INJ_PERIOD (4) - -#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_curr_prot.c b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_curr_prot.c deleted file mode 100644 index da4e56f8..00000000 --- a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_curr_prot.c +++ /dev/null @@ -1,267 +0,0 @@ -/** - * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_curr_prot.c - * @author MCU Algorithm Team - * @brief This file contains current protecion api definition. - */ - -/* Includes ------------------------------------------------------------------------------------ */ -#include "mcs_curr_prot.h" -#include "mcs_math.h" -#include "mcs_prot_user_config.h" -#include "mcs_assert.h" - -/** - * @brief Initilization over current protection function. - * @param ocp Over current protection handle. - * @param ts Ctrl period (s). - * @retval None. - */ -void OCP_Init(OCP_Handle *ocp, float ts) -{ - MCS_ASSERT_PARAM(ocp != NULL); - MCS_ASSERT_PARAM(ts > 0.0f); - ocp->ts = ts; - OCP_Clear(ocp); - - ocp->protCntLimit = PROT_CNT_LIMIT; - ocp->recyCntLimit = RECY_CNT_LIMIT; - /* Configuring four levels of current protection thresholds. */ - ocp->protValThr[PROT_VAL_THRESHOLD_0] = PROT_OVER_CURR_POW_DN1; - ocp->protValThr[PROT_VAL_THRESHOLD_1] = PROT_OVER_CURR_POW_DN2; - ocp->protValThr[PROT_VAL_THRESHOLD_2] = PROT_OVER_CURR_POW_DN3; - ocp->protValThr[PROT_VAL_THRESHOLD_3] = PROT_OVER_CURR_POW_OFF; - /* Configure the protection limit time. */ - ocp->protLimitTime[PROT_LIMIT_TIME_0] = PROT_OVER_CURR_LIMIT1_TIME_SEC; - ocp->protLimitTime[PROT_LIMIT_TIME_1] = PROT_OVER_CURR_LIMIT2_TIME_SEC; - ocp->protLimitTime[PROT_LIMIT_TIME_2] = PROT_OVER_CURR_LIMIT3_TIME_SEC; - ocp->recyDelta = PROT_OVER_CURR_RECY_DELTA; -} - -/** - * @brief Over current protection detection. - * @param ocp Over current protection handle. - * @param motorErrStatus Motor error status. - * @param idq DQ-axis feedback currents. - * @retval None. - */ -void OCP_Det(OCP_Handle *ocp, MotorErrStatusReg *motorErrStatus, DqAxis idq) -{ - MCS_ASSERT_PARAM(ocp != NULL); - MCS_ASSERT_PARAM(motorErrStatus != NULL); - /* Calculate current amplitude. */ - ocp->currAmp = Sqrt(idq.d * idq.d + idq.q * idq.q); - - /* Check if value goes over threshold for continuous cycles. */ - if (ocp->currAmp < ocp->protValThr[PROT_VAL_THRESHOLD_0]) { - ocp->protCnt = 0; - return; - } - - if (ocp->protCnt < ocp->protCntLimit) { - ocp->protCnt++; - return; - } - - /* Check which protection level should be activated. */ - /* Once enter error status, error status can only be changed by recover api. */ - /* protect level: level 4. */ - if (ocp->currAmp >= ocp->protValThr[PROT_VAL_THRESHOLD_3] && ocp->protLevel < PROT_LEVEL_4) { - ocp->protLevel = PROT_LEVEL_4; - motorErrStatus->Bit.overCurrErr = 1; - ocp->protCnt = 0; - return; - } - - /* Protect level: level 3. */ - if (ocp->currAmp >= ocp->protValThr[PROT_VAL_THRESHOLD_2] && ocp->protLevel < PROT_LEVEL_3) { - ocp->protLevel = PROT_LEVEL_3; - motorErrStatus->Bit.overCurrErr = 1; - ocp->protCnt = 0; - ocp->timer = 0.0f; - return; - } - - /* Protect level: level 2. */ - if (ocp->currAmp >= ocp->protValThr[PROT_VAL_THRESHOLD_1] && ocp->protLevel < PROT_LEVEL_2) { - ocp->protLevel = PROT_LEVEL_2; - motorErrStatus->Bit.overCurrErr = 1; - ocp->protCnt = 0; - ocp->timer = 0.0f; - return; - } - - /* Protect level: level 1. */ - if (ocp->currAmp >= ocp->protValThr[PROT_VAL_THRESHOLD_0] && ocp->protLevel < PROT_LEVEL_1) { - ocp->protLevel = PROT_LEVEL_1; - motorErrStatus->Bit.overCurrErr = 1; - ocp->protCnt = 0; - ocp->timer = 0.0f; - return; - } -} - -/** - * @brief Over current protection execution. - * @param ocp Over current protection handle. - * @param idqRef DQ-axis current references. - * @param aptAddr Three-phase APT address pointer. - * @retval None. - */ -void OCP_Exec(OCP_Handle *ocp, DqAxis *idqRef, APT_RegStruct **aptAddr) -{ - MCS_ASSERT_PARAM(ocp != NULL); - MCS_ASSERT_PARAM(idqRef != NULL); - MCS_ASSERT_PARAM(aptAddr != NULL); - - float id = idqRef->d; - float iq = idqRef->q; - float idqAmp = ocp->currAmp; - /* According to protect level, take corresponding action. */ - switch (ocp->protLevel) { - /* level 4: disable all PWM output. */ - case PROT_LEVEL_4: - /* Disable three-phase pwm output. */ - ProtSpo_Exec(aptAddr); - idqRef->d = 0.0f; - idqRef->q = 0.0f; - break; - - /* level 3: derate speed reference. */ - case PROT_LEVEL_3: - ocp->timer += ocp->ts; - if (ocp->timer > ocp->protLimitTime[PROT_LIMIT_TIME_2]) { - idqRef->d = id / idqAmp * PROT_MOTOR_RATED_CURR; - idqRef->q = iq / idqAmp * PROT_MOTOR_RATED_CURR; - } - break; - - /* level 2: derate speed reference. */ - case PROT_LEVEL_2: - ocp->timer += ocp->ts; - if (ocp->timer > ocp->protLimitTime[PROT_LIMIT_TIME_1]) { - idqRef->d = id / idqAmp * PROT_MOTOR_RATED_CURR; - idqRef->q = iq / idqAmp * PROT_MOTOR_RATED_CURR; - } - break; - - /* level 1: derate speed reference. */ - case PROT_LEVEL_1: - ocp->timer += ocp->ts; - if (ocp->timer > ocp->protLimitTime[PROT_LIMIT_TIME_0]) { - idqRef->d = id / idqAmp * PROT_MOTOR_RATED_CURR; - idqRef->q = iq / idqAmp * PROT_MOTOR_RATED_CURR; - } - break; - - /* level 0: take no protection action. */ - case PROT_LEVEL_0: - break; - - default: - break; - } -} - -/** - * @brief Over current protection recovery. - * @param ocp Over current protection handle. - * @param motorErrStatus Motor error status. - * @retval None. - */ -void OCP_Recy(OCP_Handle *ocp, MotorErrStatusReg *motorErrStatus) -{ - MCS_ASSERT_PARAM(ocp != NULL); - /* If not under error state, just return without any operation. */ - if (!motorErrStatus->Bit.overCurrErr) { - return; - } - - /* Calculate current amplitude. */ - float currAmp = ocp->currAmp; - - /* According to protection level, take corresponding recovery action. */ - switch (ocp->protLevel) { - /* level 4 */ - case PROT_LEVEL_4: - if (currAmp < ocp->protValThr[PROT_VAL_THRESHOLD_3] - ocp->recyDelta) { - ocp->recyCnt++; - if (ocp->recyCnt > ocp->recyCntLimit) { - ocp->protLevel = PROT_LEVEL_3; - ocp->recyCnt = 0; - } - } - break; - - /* level 3 */ - case PROT_LEVEL_3: - if (currAmp < ocp->protValThr[PROT_VAL_THRESHOLD_2] - ocp->recyDelta) { - ocp->recyCnt++; - if (ocp->recyCnt > ocp->recyCntLimit) { - ocp->protLevel = PROT_LEVEL_2; - ocp->recyCnt = 0; - } - } - break; - - /* level 2 */ - case PROT_LEVEL_2: - if (currAmp < ocp->protValThr[PROT_VAL_THRESHOLD_1] - ocp->recyDelta) { - ocp->recyCnt++; - if (ocp->recyCnt > ocp->recyCntLimit) { - ocp->protLevel = PROT_LEVEL_1; - ocp->recyCnt = 0; - } - } - break; - - /* level 1 */ - case PROT_LEVEL_1: - if (currAmp < ocp->protValThr[PROT_VAL_THRESHOLD_0] - ocp->recyDelta) { - ocp->recyCnt++; - if (ocp->recyCnt > ocp->recyCntLimit) { - ocp->protLevel = PROT_LEVEL_0; - ocp->recyCnt = 0; - } - } - break; - - /* level 0 */ - case PROT_LEVEL_0: - motorErrStatus->Bit.overCurrErr = 0; - break; - - default: - break; - } -} - -/** - * @brief Over current protection error status clear. - * @param ocp Over current protection handle. - * @retval None. - */ -void OCP_Clear(OCP_Handle *ocp) -{ - MCS_ASSERT_PARAM(ocp != NULL); - /* Clear the history value. */ - ocp->protCnt = 0; - ocp->protLevel = PROT_LEVEL_0; - ocp->recyCnt = 0; - ocp->timer = 0.0f; -} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_curr_prot.h b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_curr_prot.h deleted file mode 100644 index e9916fe4..00000000 --- a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_curr_prot.h +++ /dev/null @@ -1,52 +0,0 @@ -/** - * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_curr_prot.h - * @author MCU Algorithm Team - * @brief This file contains current protection data struct and api declaration. - */ - -/* Define to prevent recursive inclusion ------------------------------------- */ -#ifndef McuMagicTag_MCS_CURR_PROT_H -#define McuMagicTag_MCS_CURR_PROT_H - -/* Includes ------------------------------------------------------------------------------------ */ -#include "mcs_prot_cmm.h" -#include "mcs_typedef.h" -#include "apt_ip.h" - -typedef struct { - unsigned int protCnt; - unsigned int recyCnt; - unsigned int protCntLimit; - unsigned int recyCntLimit; - float currAmp; - float protValThr[PROT_VAL_THRESHOLD_NUMS]; /* from low current level to high. */ - float protLimitTime[PROT_LIMIT_TIME_NUMS]; /* from level 3 to level 1. */ - float recyDelta; - float timer; - float ts; - PROT_Level protLevel; -} OCP_Handle; - -void OCP_Init(OCP_Handle *ocp, float ts); -void OCP_Det(OCP_Handle *ocp, MotorErrStatusReg *motorErrStatus, DqAxis idq); -void OCP_Exec(OCP_Handle *ocp, DqAxis *idqRef, APT_RegStruct **aptAddr); -void OCP_Recy(OCP_Handle *ocp, MotorErrStatusReg *motorErrStatus); -void OCP_Clear(OCP_Handle *ocp); - -#endif diff --git a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_dc_volt_prot.c b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_dc_volt_prot.c deleted file mode 100644 index b0541a2e..00000000 --- a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_dc_volt_prot.c +++ /dev/null @@ -1,495 +0,0 @@ -/** - * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_dc_volt_prot.c - * @author MCU Algorithm Team - * @brief This file contains dc-link voltage protection api declaration. - */ - -/* Includes ------------------------------------------------------------------------------------ */ -#include "mcs_dc_volt_prot.h" -#include "mcs_prot_user_config.h" -#include "mcs_assert.h" - -/** - * @brief Initilization over dc-link voltage protection function. - * @param ovp Over dc-link voltage protection handle. - * @param ts Ctrl period (s). - * @retval None. - */ -void OVP_Init(OVP_Handle *ovp, float ts) -{ - MCS_ASSERT_PARAM(ovp != NULL); - MCS_ASSERT_PARAM(ts > 0.0f); - ovp->ts = ts; - OVP_Clear(ovp); - ovp->protCntLimit = PROT_CNT_LIMIT; - ovp->recyCntLimit = OVER_VOLT_RECY_CNT_LIMIT; - /* Configuring four levels of overvoltage protection thresholds. */ - ovp->protValThr[PROT_VAL_THRESHOLD_0] = PROT_OVER_VOLT_BRK_ON1; - ovp->protValThr[PROT_VAL_THRESHOLD_1] = PROT_OVER_VOLT_BRK_ON2; - ovp->protValThr[PROT_VAL_THRESHOLD_2] = PROT_OVER_VOLT_BRK_ON3; - ovp->protValThr[PROT_VAL_THRESHOLD_3] = PROT_OVER_VOLT_BRK_ALL; - /* Configure the protection limit time. */ - ovp->protLimitTime[PROT_LIMIT_TIME_0] = PROT_OVER_VOLT_LIMIT1_TIME_SEC; - ovp->protLimitTime[PROT_LIMIT_TIME_1] = PROT_OVER_VOLT_LIMIT2_TIME_SEC; - ovp->protLimitTime[PROT_LIMIT_TIME_2] = PROT_OVER_VOLT_LIMIT3_TIME_SEC; - ovp->recyDelta = PROT_OVER_VOLT_RECY_DELTA; -} - -/** - * @brief Initilization lower dc-link voltage protection function. - * @param lvp Lower dc-link voltage protection handle. - * @param ts Ctrl period (s). - * @retval None. - */ -void LVP_Init(LVP_Handle *lvp, float ts) -{ - MCS_ASSERT_PARAM(lvp != NULL); - MCS_ASSERT_PARAM(ts > 0.0f); - - lvp->ts = ts; - LVP_Clear(lvp); - - lvp->protCntLimit = PROT_CNT_LIMIT; - lvp->recyCntLimit = LOWER_VOLT_RECY_CNT_LIMIT; - /* Configuring four levels of lower voltage protection thresholds. */ - lvp->protValThr[PROT_VAL_THRESHOLD_0] = PROT_LOWER_VOLT_POW_DN1; - lvp->protValThr[PROT_VAL_THRESHOLD_1] = PROT_LOWER_VOLT_POW_DN2; - lvp->protValThr[PROT_VAL_THRESHOLD_2] = PROT_LOWER_VOLT_POW_DN3; - lvp->protValThr[PROT_VAL_THRESHOLD_3] = PROT_LOWER_VOLT_POW_OFF; - /* Configure the protection limit time. */ - lvp->protLimitTime[PROT_LIMIT_TIME_0] = PROT_LOWER_VOLT_LIMIT1_TIME_SEC; - lvp->protLimitTime[PROT_LIMIT_TIME_1] = PROT_LOWER_VOLT_LIMIT2_TIME_SEC; - lvp->protLimitTime[PROT_LIMIT_TIME_2] = PROT_LOWER_VOLT_LIMIT3_TIME_SEC; - lvp->recyDelta = PROT_OVER_VOLT_RECY_DELTA; -} - -/** - * @brief Over dc-link voltage protection detection. - * @param ovp Over dc-link voltage protection handle. - * @param motorErrStatus Motor error status. - * @param udc DC-link voltage feedback (V). - * @retval None. - */ -void OVP_Det(OVP_Handle *ovp, MotorErrStatusReg *motorErrStatus, float udc) -{ - MCS_ASSERT_PARAM(ovp != NULL); - MCS_ASSERT_PARAM(motorErrStatus != NULL); - MCS_ASSERT_PARAM(udc > 0.0f); - /* Check if value goes over threshold for continuous cycles. */ - if (udc < ovp->protValThr[PROT_VAL_THRESHOLD_0]) { - ovp->protCnt = 0; - return; - } - - if (ovp->protCnt < ovp->protCntLimit) { - ovp->protCnt++; - return; - } - - /* Check which protection level should be activated. */ - /* Once enter error status, error status can only be changed by recover api. */ - /* protect level: level 4. */ - if (udc >= ovp->protValThr[PROT_VAL_THRESHOLD_3] && ovp->protLevel < PROT_LEVEL_4) { - ovp->protLevel = PROT_LEVEL_4; - motorErrStatus->Bit.overVoltErr = 1; - ovp->protCnt = 0; - return; - } - - /* Protect level: level 3. */ - if (udc >= ovp->protValThr[PROT_VAL_THRESHOLD_2] && ovp->protLevel < PROT_LEVEL_3) { - ovp->protLevel = PROT_LEVEL_3; - motorErrStatus->Bit.overVoltErr = 1; - ovp->protCnt = 0; - ovp->timer = 0.0f; - return; - } - - /* Protect level: level 2. */ - if (udc >= ovp->protValThr[PROT_VAL_THRESHOLD_1] && ovp->protLevel < PROT_LEVEL_2) { - ovp->protLevel = PROT_LEVEL_2; - motorErrStatus->Bit.overVoltErr = 1; - ovp->protCnt = 0; - ovp->timer = 0.0f; - return; - } - - /* Protect level: level 1. */ - if (udc >= ovp->protValThr[PROT_VAL_THRESHOLD_0] && ovp->protLevel < PROT_LEVEL_1) { - ovp->protLevel = PROT_LEVEL_1; - motorErrStatus->Bit.overVoltErr = 1; - ovp->protCnt = 0; - ovp->timer = 0.0f; - return; - } -} - -/** - * @brief Lower dc-link voltage protection detection. - * @param lvp Lower dc-link voltage protection handle. - * @param motorErrStatus Motor error status. - * @param udc DC-link voltage feedback (V). - * @retval None. - */ -void LVP_Det(LVP_Handle *lvp, MotorErrStatusReg *motorErrStatus, float udc) -{ - MCS_ASSERT_PARAM(lvp != NULL); - MCS_ASSERT_PARAM(motorErrStatus != NULL); - MCS_ASSERT_PARAM(udc > 0.0f); - /* Check if value goes over threshold for continuous cycles. */ - if (udc > lvp->protValThr[PROT_VAL_THRESHOLD_0]) { - lvp->protCnt = 0; - return; - } - - if (lvp->protCnt < lvp->protCntLimit) { - lvp->protCnt++; - return; - } - - /* Check which protection level should be activated. */ - /* Once enter error status, error status can only be changed by recover api. */ - /* protect level: level 4. */ - if (udc <= lvp->protValThr[PROT_VAL_THRESHOLD_3] && lvp->protLevel < PROT_LEVEL_4) { - lvp->protLevel = PROT_LEVEL_4; - motorErrStatus->Bit.lowerVoltErr = 1; - lvp->protCnt = 0; - return; - } - - /* Protect level: level 3. */ - if (udc <= lvp->protValThr[PROT_VAL_THRESHOLD_2] && lvp->protLevel < PROT_LEVEL_3) { - lvp->protLevel = PROT_LEVEL_3; - motorErrStatus->Bit.lowerVoltErr = 1; - lvp->protCnt = 0; - lvp->timer = 0.0f; - return; - } - - /* Protect level: level 2. */ - if (udc <= lvp->protValThr[PROT_VAL_THRESHOLD_1] && lvp->protLevel < PROT_LEVEL_2) { - lvp->protLevel = PROT_LEVEL_2; - motorErrStatus->Bit.lowerVoltErr = 1; - lvp->protCnt = 0; - lvp->timer = 0.0f; - return; - } - - /* Protect level: level 1. */ - if (udc <= lvp->protValThr[PROT_VAL_THRESHOLD_0] && lvp->protLevel < PROT_LEVEL_1) { - lvp->protLevel = PROT_LEVEL_1; - motorErrStatus->Bit.lowerVoltErr = 1; - lvp->protCnt = 0; - lvp->timer = 0.0f; - return; - } -} - -/** - * @brief Over dc-link voltage protection execution. - * @param ovp Over dc-link voltage protection handle. - * @param duty Brake loop output duty (0-1). - * @param aptAddr Three-phase APT address pointer. - * @retval None. - */ -void OVP_Exec(OVP_Handle *ovp, float *duty, APT_RegStruct **aptAddr) -{ - MCS_ASSERT_PARAM(ovp != NULL); - MCS_ASSERT_PARAM(duty != NULL); - MCS_ASSERT_PARAM(aptAddr != NULL); - /* According to protect level, take corresponding action. */ - switch (ovp->protLevel) { - /* level 4: brake loop duty maximum. */ - case PROT_LEVEL_4: - *duty = PROT_OVER_VOLT_BRK_DUTY4; - /* Disable three-phase pwm output. */ - ProtSpo_Exec(aptAddr); - break; - - /* level 3: brake loop duty level 3. */ - case PROT_LEVEL_3: - *duty = PROT_OVER_VOLT_BRK_DUTY2; - ovp->timer += ovp->ts; - if (ovp->timer > ovp->protLimitTime[PROT_LIMIT_TIME_2]) { - *duty = PROT_OVER_VOLT_BRK_DUTY3; - } - break; - - /* level 2: brake loop duty level 2. */ - case PROT_LEVEL_2: - *duty = PROT_OVER_VOLT_BRK_DUTY1; - ovp->timer += ovp->ts; - if (ovp->timer > ovp->protLimitTime[PROT_LIMIT_TIME_1]) { - *duty = PROT_OVER_VOLT_BRK_DUTY2; - } - break; - - /* level 1: brake loop duty level 1. */ - case PROT_LEVEL_1: - ovp->timer += ovp->ts; - if (ovp->timer > ovp->protLimitTime[PROT_LIMIT_TIME_0]) { - *duty = PROT_OVER_VOLT_BRK_DUTY1; - } - break; - - /* level 0: take no protection action. */ - case PROT_LEVEL_0: - break; - - default: - break; - } - return; -} - -/** - * @brief Lower dc-link voltage protection execution. - * @param lvp Lower dc-link voltage protection handle. - * @param spdRef Speed Reference (Hz). - * @param aptAddr Three-phase APT address pointer. - * @retval None. - */ -void LVP_Exec(LVP_Handle *lvp, float *spdRef, APT_RegStruct **aptAddr) -{ - MCS_ASSERT_PARAM(lvp != NULL); - MCS_ASSERT_PARAM(spdRef != NULL); - MCS_ASSERT_PARAM(aptAddr != NULL); - /* According to protect level, take corresponding action. */ - switch (lvp->protLevel) { - /* level 4: disable all PWM output. */ - case PROT_LEVEL_4: - /* Disable three-phase pwm output. */ - ProtSpo_Exec(aptAddr); - *spdRef *= 0.0f; - break; - - /* level 3: derate speed reference. */ - case PROT_LEVEL_3: - *spdRef *= PROT_POW_DN2_PCT; - lvp->timer += lvp->ts; - if (lvp->timer > lvp->protLimitTime[PROT_LIMIT_TIME_2]) { - *spdRef *= PROT_POW_DN3_PCT; - } - break; - - /* level 2: derate speed reference. */ - case PROT_LEVEL_2: - *spdRef *= PROT_POW_DN1_PCT; - lvp->timer += lvp->ts; - if (lvp->timer > lvp->protLimitTime[PROT_LIMIT_TIME_1]) { - *spdRef *= PROT_POW_DN2_PCT; - } - break; - - /* level 1: derate speed reference. */ - case PROT_LEVEL_1: - lvp->timer += lvp->ts; - if (lvp->timer > lvp->protLimitTime[PROT_LIMIT_TIME_0]) { - *spdRef *= PROT_POW_DN1_PCT; - } - break; - - /* level 0: take no protection action. */ - case PROT_LEVEL_0: - break; - - default: - break; - } - return; -} - -/** - * @brief Over dc-link voltage protection recovery. - * @param ovp Over dc-link voltage protection handle. - * @param motorErrStatus Motor error status. - * @param udc DC-link voltage (V). - * @retval None. - */ -void OVP_Recy(OVP_Handle *ovp, MotorErrStatusReg *motorErrStatus, float udc) -{ - MCS_ASSERT_PARAM(ovp != NULL); - /* If not under error state, just return without any operation. */ - if (!motorErrStatus->Bit.overVoltErr) { - return; - } - - /* According to protection level, take corresponding recovery action. */ - switch (ovp->protLevel) { - /* level 4 */ - case PROT_LEVEL_4: - if (udc < ovp->protValThr[PROT_VAL_THRESHOLD_3] - ovp->recyDelta) { - ovp->recyCnt++; - if (ovp->recyCnt > ovp->recyCntLimit) { - ovp->protLevel = PROT_LEVEL_3; - ovp->recyCnt = 0; - } - } - break; - - /* level 3 */ - case PROT_LEVEL_3: - /* If the dc-link voltage is less than threshold 2, level-2 protection is restored. */ - if (udc < ovp->protValThr[PROT_VAL_THRESHOLD_2] - ovp->recyDelta) { - ovp->recyCnt++; - if (ovp->recyCnt > ovp->recyCntLimit) { - ovp->protLevel = PROT_LEVEL_2; - ovp->recyCnt = 0; - } - } - break; - - /* level 2 */ - case PROT_LEVEL_2: - /* If the dc-link voltage is less than threshold 1, level-1 protection is restored. */ - if (udc < ovp->protValThr[PROT_VAL_THRESHOLD_1] - ovp->recyDelta) { - ovp->recyCnt++; - if (ovp->recyCnt > ovp->recyCntLimit) { - ovp->protLevel = PROT_LEVEL_1; - ovp->recyCnt = 0; - } - } - break; - - /* level 1 */ - case PROT_LEVEL_1: - /* If the dc-link voltage is less than threshold 0, level-0 protection is restored. */ - if (udc < ovp->protValThr[PROT_VAL_THRESHOLD_0] - ovp->recyDelta) { - ovp->recyCnt++; - if (ovp->recyCnt > ovp->recyCntLimit) { - ovp->protLevel = PROT_LEVEL_0; - ovp->recyCnt = 0; - } - } - break; - /* level 0 Fault-free state. */ - case PROT_LEVEL_0: - motorErrStatus->Bit.overVoltErr = 0; - break; - - default: - break; - } - return; -} - -/** - * @brief Lower dc-link voltage protection recovery. - * @param lvp Lower dc-link voltage protection handle. - * @param motorErrStatus Motor error status. - * @param udc DC-link voltage (V). - * @retval None. - */ -void LVP_Recy(LVP_Handle *lvp, MotorErrStatusReg *motorErrStatus, float udc) -{ - MCS_ASSERT_PARAM(lvp != NULL); - /* If not under error state, just return without any operation. */ - if (!motorErrStatus->Bit.lowerVoltErr) { - return; - } - - /* According to protection level, take corresponding recovery action. */ - switch (lvp->protLevel) { - /* level 4 */ - case PROT_LEVEL_4: - /* If the dc-link voltage is greater than threshold 3, level-3 protection is restored. */ - if (udc > lvp->protValThr[PROT_VAL_THRESHOLD_3] + lvp->recyDelta) { - lvp->recyCnt++; - if (lvp->recyCnt > lvp->recyCntLimit) { - lvp->protLevel = PROT_LEVEL_3; - lvp->recyCnt = 0; - } - } - break; - - /* level 3 */ - case PROT_LEVEL_3: - /* If the dc-link voltage is greater than threshold 2, level-2 protection is restored. */ - if (udc > lvp->protValThr[PROT_VAL_THRESHOLD_2] + lvp->recyDelta) { - lvp->recyCnt++; - if (lvp->recyCnt > lvp->recyCntLimit) { - lvp->protLevel = PROT_LEVEL_2; - lvp->recyCnt = 0; - } - } - break; - - /* level 2 */ - case PROT_LEVEL_2: - /* If the dc-link voltage is greater than threshold 1, level-1 protection is restored. */ - if (udc > lvp->protValThr[PROT_VAL_THRESHOLD_1] + lvp->recyDelta) { - lvp->recyCnt++; - if (lvp->recyCnt > lvp->recyCntLimit) { - lvp->protLevel = PROT_LEVEL_1; - lvp->recyCnt = 0; - } - } - break; - - /* level 1 */ - case PROT_LEVEL_1: - /* If the dc-link voltage is greater than threshold 0, level-0 protection is restored. */ - if (udc > lvp->protValThr[PROT_VAL_THRESHOLD_0] + lvp->recyDelta) { - lvp->recyCnt++; - if (lvp->recyCnt > lvp->recyCntLimit) { - lvp->protLevel = PROT_LEVEL_0; - lvp->recyCnt = 0; - } - } - break; - /* level 0 Fault-free state. */ - case PROT_LEVEL_0: - motorErrStatus->Bit.lowerVoltErr = 0; - break; - - default: - break; - } - return; -} - -/** - * @brief Over dc-link voltage protection error status clear. - * @param ovp Over voltage protection handle. - * @retval None. - */ -void OVP_Clear(OVP_Handle *ovp) -{ - MCS_ASSERT_PARAM(ovp != NULL); - /* Clear the history value. */ - ovp->protCnt = 0; - ovp->protLevel = PROT_LEVEL_0; - ovp->recyCnt = 0; - ovp->timer = 0.0f; -} - -/** - * @brief Lower dc-link voltage protection error status clear. - * @param lvp Lower voltage protection handle. - * @retval None. - */ -void LVP_Clear(LVP_Handle *lvp) -{ - MCS_ASSERT_PARAM(lvp != NULL); - /* Clear the history value. */ - lvp->protCnt = 0; - lvp->protLevel = PROT_LEVEL_0; - lvp->recyCnt = 0; - lvp->timer = 0.0f; -} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_dc_volt_prot.h b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_dc_volt_prot.h deleted file mode 100644 index 0435156a..00000000 --- a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_dc_volt_prot.h +++ /dev/null @@ -1,69 +0,0 @@ -/** - * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_dc_volt_prot.h - * @author MCU Algorithm Team - * @brief This file contains dc-link voltage protection data struct and api declaration. - */ - -/* Define to prevent recursive inclusion ------------------------------------------------------- */ -#ifndef McuMagicTag_MCS_DC_VOLT_PROT_H -#define McuMagicTag_MCS_DC_VOLT_PROT_H - -/* Includes ------------------------------------------------------------------------------------ */ -#include "mcs_prot_cmm.h" -#include "apt_ip.h" - -typedef struct { - unsigned int protCnt; - unsigned int recyCnt; - unsigned int protCntLimit; - unsigned int recyCntLimit; - float protValThr[PROT_VAL_THRESHOLD_NUMS]; /* from low voltage level to high. */ - float protLimitTime[PROT_LIMIT_TIME_NUMS]; /* from level 3 to level 1. */ - float recyDelta; - float timer; - float ts; - PROT_Level protLevel; -} OVP_Handle; - -typedef struct { - unsigned int protCnt; - unsigned int recyCnt; - unsigned int protCntLimit; - unsigned int recyCntLimit; - float protValThr[PROT_VAL_THRESHOLD_NUMS]; /* from high voltage level to low. */ - float protLimitTime[PROT_LIMIT_TIME_NUMS]; /* from level 3 to level 1. */ - float recyDelta; - float timer; - float ts; - PROT_Level protLevel; -} LVP_Handle; - -void OVP_Init(OVP_Handle *ovp, float ts); -void OVP_Det(OVP_Handle *ovp, MotorErrStatusReg *motorErrStatus, float udc); -void OVP_Exec(OVP_Handle *ovp, float *duty, APT_RegStruct **aptAddr); -void OVP_Recy(OVP_Handle *ovp, MotorErrStatusReg *motorErrStatus, float udc); -void OVP_Clear(OVP_Handle *ovp); - -void LVP_Init(LVP_Handle *lvp, float ts); -void LVP_Det(LVP_Handle *lvp, MotorErrStatusReg *motorErrStatus, float udc); -void LVP_Exec(LVP_Handle *lvp, float *spdRef, APT_RegStruct **aptAddr); -void LVP_Recy(LVP_Handle *lvp, MotorErrStatusReg *motorErrStatus, float udc); -void LVP_Clear(LVP_Handle *lvp); - -#endif diff --git a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_motor_stalling.c b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_motor_stalling.c deleted file mode 100644 index 9dee4d2d..00000000 --- a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_motor_stalling.c +++ /dev/null @@ -1,112 +0,0 @@ -/** - * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_motor_stalling.c - * @author MCU Algorithm Team - * @brief This file contains motor stalling protection data struct and api definition. - */ - -/* Includes ------------------------------------------------------------------------------------ */ -#include "mcs_motor_stalling.h" -#include "mcs_prot_user_config.h" -#include "mcs_math.h" -#include "mcs_math_const.h" -#include "mcs_assert.h" - -/** - * @brief Initilization motor stalling protection function. - * @param stall Motor stalling handle. - * @param ts Ctrl period (s). - * @param currLimit The current amplitude that triggers fault. (A). - * @param spdLimit The speed amplitude that triggers fault. (Hz). - * @param timeLimit The threshold time that current amplitude over the limit (s). - * @retval None. - */ -void STP_Init(STP_Handle *stall, float ts, float currLimit, float spdLimit, float timeLimit) -{ - MCS_ASSERT_PARAM(stall != NULL); - MCS_ASSERT_PARAM(ts > 0.0f); - MCS_ASSERT_PARAM(currLimit > 0.0f); - MCS_ASSERT_PARAM(spdLimit > 0.0f); - MCS_ASSERT_PARAM(timeLimit > 0.0f); - /* Configuring parameters for stalling detection. */ - stall->ts = ts; - /* Current threshold and speed threshold for stalling fault. */ - stall->currAmpLimit = currLimit; - stall->spdLimit = spdLimit; - stall->timeLimit = timeLimit; - stall->timer = 0.0f; -} - -/** - * @brief Motor stalling detection. - * @param stall Motor stalling handle. - * @param motorErrStatus Motor error status. - * @param spd Speed feedback (Hz). - * @param idq Dq-axis current feedback (A). - * @retval None. - */ -void STP_Det_ByCurrSpd(STP_Handle *stall, MotorErrStatusReg *motorErrStatus, float spd, DqAxis idq) -{ - MCS_ASSERT_PARAM(stall != NULL); - MCS_ASSERT_PARAM(motorErrStatus != NULL); - MCS_ASSERT_PARAM(Abs(spd) >= 0.0f); - /* Calculate current amplitude. */ - float currAmp = Sqrt(idq.d * idq.d + idq.q * idq.q); - float spdAbs = Abs(spd); - /* Check if value goes over threshold for continuous cycles. */ - if (currAmp < stall->currAmpLimit || spdAbs > stall->spdLimit) { - stall->timer = 0.0f; - return; - } - /* Time accumulation. */ - if (stall->timer < stall->timeLimit) { - stall->timer += stall->ts; - return; - } - motorErrStatus->Bit.motorStalling = 1; -} - -/** - * @brief Motor stalling protection execution. - * @param motorErrStatus Motor error status. - * @param aptAddr Three-phase APT address pointer. - * @retval None. - */ -void STP_Exec(MotorErrStatusReg *motorErrStatus, APT_RegStruct **aptAddr) -{ - MCS_ASSERT_PARAM(motorErrStatus != NULL); - MCS_ASSERT_PARAM(aptAddr != NULL); - if (motorErrStatus->Bit.motorStalling == 0) { - return; - } - - /* Disable three-phase pwm output. */ - ProtSpo_Exec(aptAddr); - return; -} - -/** - * @brief Motor stalling protection error status clear. - * @param stall Motor stalling handle. - * @retval None. - */ -void STP_Clear(STP_Handle *stall) -{ - MCS_ASSERT_PARAM(stall != NULL); - stall->timer = 0.0f; -} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_motor_stalling.h b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_motor_stalling.h deleted file mode 100644 index a1d1e4be..00000000 --- a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_motor_stalling.h +++ /dev/null @@ -1,45 +0,0 @@ -/** - * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_motor_stalling.h - * @author MCU Algorithm Team - * @brief This file contains motor stalling protection data struct and api declaration. - */ - -/* Define to prevent recursive inclusion ------------------------------------- */ -#ifndef McuMagicTag_MCS_MOTOR_STALLING_PROT_H -#define McuMagicTag_MCS_MOTOR_STALLING_PROT_H - -/* Includes ------------------------------------------------------------------------------------ */ -#include "mcs_prot_cmm.h" -#include "apt_ip.h" -#include "mcs_typedef.h" - -typedef struct { - float currAmpLimit; /**< Feedback current higher than this value triggers fault. (A). */ - float spdLimit; /**< Feedback speed lower than this value triggers fault (Hz). */ - float timeLimit; /**< The threshold time that current and speed feedback over ranges (s). */ - float timer; /**< Timer to get speed and current over range time. */ - float ts; /**< Ctrl period (s). */ -} STP_Handle; - -void STP_Init(STP_Handle *stall, float ts, float currLimit, float spdLimit, float timeLimit); -void STP_Det_ByCurrSpd(STP_Handle *stall, MotorErrStatusReg *motorErrStatus, float spd, DqAxis idq); -void STP_Exec(MotorErrStatusReg *motorErrStatus, APT_RegStruct **aptAddr); -void STP_Clear(STP_Handle *stall); - -#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_temp_prot.c b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_temp_prot.c deleted file mode 100644 index 184ba4a3..00000000 --- a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_temp_prot.c +++ /dev/null @@ -1,262 +0,0 @@ -/** - * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_temp_prot.c - * @author MCU Algorithm Team - * @brief This file contains over temperature protection api definition. - */ - -/* Includes ------------------------------------------------------------------------------------ */ -#include "mcs_temp_prot.h" -#include "mcs_prot_user_config.h" -#include "mcs_assert.h" - -/** - * @brief Initilization over temperation protection function. - * @param otp Over temperature protection handle. - * @param ts Ctrl period. - * @retval None. - */ -void OTP_Init(OTP_Handle *otp, float ts) -{ - MCS_ASSERT_PARAM(otp != NULL); - MCS_ASSERT_PARAM(ts > 0.0f); - otp->ts = ts; - otp->protCntLimit = PROT_CNT_LIMIT; - otp->recyCntLimit = RECY_CNT_LIMIT; - /* Configuring the temperature protection threshold. */ - otp->protValThr[PROT_VAL_THRESHOLD_0] = PROT_OVER_IPM_TEMP_POW_DN1; - otp->protValThr[PROT_VAL_THRESHOLD_1] = PROT_OVER_IPM_TEMP_POW_DN2; - otp->protValThr[PROT_VAL_THRESHOLD_2] = PROT_OVER_IPM_TEMP_POW_DN3; - otp->protValThr[PROT_VAL_THRESHOLD_3] = PROT_OVER_IPM_TEMP_POW_OFF; - /* Configuring the protection limiting time. */ - otp->protLimitTime[PROT_LIMIT_TIME_0] = PROT_OVER_TEMP_LIMIT1_TIME_SEC; - otp->protLimitTime[PROT_LIMIT_TIME_1] = PROT_OVER_TEMP_LIMIT2_TIME_SEC; - otp->protLimitTime[PROT_LIMIT_TIME_2] = PROT_OVER_TEMP_LIMIT3_TIME_SEC; - otp->recyDelta = PROT_OVER_IPM_TEMP_RECY_DELTA; - OTP_Clear(otp); -} - -/** - * @brief Over temperatre protection detection. - * @param otp Over temperature protection handle. - * @param tempErrBit Temperature error status bit. - * @param idq DQ-axis feedback currents. - * @retval None. - */ -void OTP_Det(OTP_Handle *otp, MotorErrStatusReg *motorErrStatus, PROT_ErrBit protBit, float temp) -{ - MCS_ASSERT_PARAM(otp != NULL); - MCS_ASSERT_PARAM(motorErrStatus != NULL); - MCS_ASSERT_PARAM(temp > 0.0f); - /* Check if value goes over threshold for continuous cycles. */ - if (temp < otp->protValThr[PROT_VAL_THRESHOLD_0]) { - otp->protCnt = 0; - ClearBit(&motorErrStatus->all, protBit); - return; - } - - if (otp->protCnt < otp->protCntLimit) { - otp->protCnt++; - return; - } - - /* Check which protection level should be activated. */ - /* Once enter error status, error status can only be changed by recover api. */ - /* protect level: level 4. */ - if (temp >= otp->protValThr[PROT_VAL_THRESHOLD_3] && otp->protLevel < PROT_LEVEL_4) { - otp->protLevel = PROT_LEVEL_4; - SetBit(&motorErrStatus->all, protBit); - otp->protCnt = 0; - return; - } - - /* Protect level: level 3. */ - if (temp >= otp->protValThr[PROT_VAL_THRESHOLD_2] && otp->protLevel < PROT_LEVEL_3) { - otp->protLevel = PROT_LEVEL_3; - SetBit(&motorErrStatus->all, protBit); - otp->protCnt = 0; - otp->timer = 0.0f; - return; - } - - /* Protect level: level 2. */ - if (temp >= otp->protValThr[PROT_VAL_THRESHOLD_1] && otp->protLevel < PROT_LEVEL_2) { - otp->protLevel = PROT_LEVEL_2; - SetBit(&motorErrStatus->all, protBit); - otp->protCnt = 0; - otp->timer = 0.0f; - return; - } - - /* Protect level: level 1. */ - if (temp >= otp->protValThr[PROT_VAL_THRESHOLD_0] && otp->protLevel < PROT_LEVEL_1) { - otp->protLevel = PROT_LEVEL_1; - SetBit(&motorErrStatus->all, protBit); - otp->protCnt = 0; - otp->timer = 0.0f; - return; - } -} - -/** - * @brief Over temperature protection execution. - * @param otp Over temperature protection handle. - * @param spdRef Speed reference (Hz). - * @param aptAddr Three-phase APT address pointer. - * @retval None. - */ -void OTP_Exec(OTP_Handle *otp, float *spdRef, APT_RegStruct **aptAddr) -{ - MCS_ASSERT_PARAM(otp != NULL); - MCS_ASSERT_PARAM(spdRef != NULL); - MCS_ASSERT_PARAM(aptAddr != NULL); - /* According to protect level, take corresponding action. */ - switch (otp->protLevel) { - /* level 4: disable all PWM output. */ - case PROT_LEVEL_4: - /* Disable three-phase pwm output. */ - ProtSpo_Exec(aptAddr); - *spdRef *= 0.0f; - break; - - /* level 3: derate speed reference. */ - case PROT_LEVEL_3: - *spdRef *= PROT_POW_DN2_PCT; - otp->timer += otp->ts; - if (otp->timer > otp->protLimitTime[PROT_LIMIT_TIME_2]) { - *spdRef *= PROT_POW_DN3_PCT; - } - break; - - /* level 2: derate speed reference. */ - case PROT_LEVEL_2: - /* Reducte motor speed to level 2 */ - *spdRef *= PROT_POW_DN1_PCT; - otp->timer += otp->ts; - if (otp->timer > otp->protLimitTime[PROT_LIMIT_TIME_1]) { - *spdRef *= PROT_POW_DN2_PCT; - } - break; - - /* level 0: take no protection action. */ - case PROT_LEVEL_0: - break; - - case PROT_LEVEL_1: - /* Reducte motor speed to level 0 */ - otp->timer += otp->ts; - if (otp->timer > otp->protLimitTime[PROT_LIMIT_TIME_0]) { - /* level 1: derate speed reference. */ - *spdRef *= PROT_POW_DN1_PCT; - } - break; - - default: - break; - } - return; -} - -/** - * @brief Over temperature protection recovery. - * @param otp Over temperature protection handle. - * @param tempErrBit Temperature error status bit. - * @param temp Temperature (celsius). - * @retval None. - */ -void OTP_Recy(OTP_Handle *otp, MotorErrStatusReg *motorErrStatus, PROT_ErrBit protBit, float temp) -{ - MCS_ASSERT_PARAM(otp != NULL); - MCS_ASSERT_PARAM(motorErrStatus != NULL); - MCS_ASSERT_PARAM(temp > 0.0f); - /* If not under error state, just return without any operation. */ - if (otp->protLevel == PROT_LEVEL_0) { - motorErrStatus->all &= (~(1 >> protBit)); - return; - } - - /* According to protection level, take corresponding recovery action. */ - switch (otp->protLevel) { - /* level 4 */ - case PROT_LEVEL_4: - /* If the temperature is less than threshold 3, level-3 protection is restored. */ - if (temp < otp->protValThr[PROT_VAL_THRESHOLD_3] - otp->recyDelta) { - otp->recyCnt++; - if (otp->recyCnt > otp->recyCntLimit) { - otp->protLevel = PROT_LEVEL_3; - otp->recyCnt = 0; - } - } - break; - - /* level 3 */ - case PROT_LEVEL_3: - /* If the temperature is less than threshold 2, level-2 protection is restored. */ - if (temp < otp->protValThr[PROT_VAL_THRESHOLD_2] - otp->recyDelta) { - otp->recyCnt++; - if (otp->recyCnt > otp->recyCntLimit) { - otp->protLevel = PROT_LEVEL_2; - otp->recyCnt = 0; - } - } - break; - - /* level 2 */ - case PROT_LEVEL_2: - /* If the temperature is less than threshold 1, level-1 protection is restored. */ - if (temp < otp->protValThr[PROT_VAL_THRESHOLD_1] - otp->recyDelta) { - otp->recyCnt++; - if (otp->recyCnt > otp->recyCntLimit) { - otp->protLevel = PROT_LEVEL_1; - otp->recyCnt = 0; - } - } - break; - - /* level 1 */ - case PROT_LEVEL_1: - /* If the temperature is less than threshold 0, level-0 protection is restored. */ - if (temp < otp->protValThr[PROT_VAL_THRESHOLD_0] - otp->recyDelta) { - otp->recyCnt++; - if (otp->recyCnt > otp->recyCntLimit) { - otp->protLevel = PROT_LEVEL_0; - otp->recyCnt = 0; - } - } - break; - - default: - break; - } - return; -} - -/** - * @brief Over temperature protection error status clear. - * @param otp Over temperature protection handle. - * @retval None. - */ -void OTP_Clear(OTP_Handle *otp) -{ - MCS_ASSERT_PARAM(otp != NULL); - /* Clear the history value. */ - otp->protCnt = 0; - otp->protLevel = PROT_LEVEL_0; - otp->recyCnt = 0; - otp->timer = 0.0f; -} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_temp_prot.h b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_temp_prot.h deleted file mode 100644 index 2b9d99a7..00000000 --- a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_temp_prot.h +++ /dev/null @@ -1,49 +0,0 @@ -/** - * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_temp_prot.h - * @author MCU Algorithm Team - * @brief his file contains over temperature protection api declaration. - */ - -/* Define to prevent recursive inclusion ------------------------------------- */ -#ifndef McuMagicTag_MCS_TEMP_PROT_H -#define McuMagicTag_MCS_TEMP_PROT_H - -/* Includes ------------------------------------------------------------------------------------ */ -#include "mcs_prot_cmm.h" -#include "apt_ip.h" - -typedef struct { - unsigned int protCnt; - unsigned int recyCnt; - unsigned int protCntLimit; - unsigned int recyCntLimit; - float protValThr[PROT_VAL_THRESHOLD_NUMS]; /* from low current level to high. */ - float protLimitTime[PROT_LIMIT_TIME_NUMS]; /* from level 3 to level 1. */ - float recyDelta; - float ts; - float timer; - PROT_Level protLevel; -} OTP_Handle; - -void OTP_Init(OTP_Handle *otp, float ts); -void OTP_Det(OTP_Handle *otp, MotorErrStatusReg *motorErrStatus, PROT_ErrBit protBit, float temp); -void OTP_Exec(OTP_Handle *otp, float *spdRef, APT_RegStruct **aptAddr); -void OTP_Recy(OTP_Handle *otp, MotorErrStatusReg *motorErrStatus, PROT_ErrBit protBit, float temp); -void OTP_Clear(OTP_Handle *otp); -#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/readme.md b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/readme.md deleted file mode 100644 index 98b2a068..00000000 --- a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/readme.md +++ /dev/null @@ -1,10 +0,0 @@ -# pmsm_sensorless_2shunt_foc - -**【功能描述】** -+ 基于ECMCU105H/ECBMCU201MPC单板的单电机双电阻采样Foc应用 - -**【环境要求】** -+ 所有单板电源改制为演示用的12V低压,电机选用Gimbal GBM2804H-100T - -**【IDE配置方法】** -+ chipConfig中的Sample栏目里面选中pmsm sensorless 2shunt foc示例,然后点击生成代码即可 diff --git a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/src/mcs_carrier.c b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/src/mcs_carrier.c deleted file mode 100644 index 9fc8afac..00000000 --- a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/src/mcs_carrier.c +++ /dev/null @@ -1,144 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_carrier.c - * @author MCU Algorithm Team - * @brief This file provides carrier interrupt application for motor control. - */ - -#include "mcs_carrier.h" -#include "mcs_math.h" -#include "typedefs.h" -#include "mcs_assert.h" -#include "mcs_user_config.h" -#include "mcs_ctlmode_config.h" - -/** - * @brief Synchronous rotation coordinate system angle. - * @param mtrCtrl The motor control handle. - * @retval None. - */ -static void MCS_SyncCoorAngle(MTRCTRL_Handle *mtrCtrl) -{ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - /* Synchronous rotation coordinate system angle. */ - switch (mtrCtrl->stateMachine) { - case FSM_STARTUP: - /* Current ramp angle is 0. */ - if (mtrCtrl->startup.stage == STARTUP_STAGE_CURR) { - mtrCtrl->axisAngle = 0; - } else if (mtrCtrl->startup.stage == STARTUP_STAGE_SPD) { /* IF control phase angle self-addition. */ - mtrCtrl->axisAngle = IF_CurrAngleCalc(&mtrCtrl->ifCtrl, mtrCtrl->spdRefHz); - } else if (mtrCtrl->startup.stage == STARTUP_STAGE_SWITCH) { /* Switch Angle */ - mtrCtrl->axisAngle = mtrCtrl->smo.elecAngle; - } - break; - - case FSM_RUN: - mtrCtrl->axisAngle = mtrCtrl->smo.elecAngle; - break; - - default: - mtrCtrl->axisAngle = 0; - break; - } -} - -/** - * @brief PWM waveform setting and sampling point setting for a single resistors and dual resistors. - * @param mtrCtrl The motor control handle. - * @retval None. - */ -static void MCS_PwmAdcSet(MTRCTRL_Handle *mtrCtrl) -{ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - SampleMode sampleMode = mtrCtrl->sampleMode; - /* Set the duty cycle according to the sampling mode. */ - if (sampleMode == DUAL_RESISTORS) { - SVPWM_Exec(&mtrCtrl->sv, &mtrCtrl->vabRef, &mtrCtrl->dutyUvw); - mtrCtrl->setPwmDutyCb(&mtrCtrl->dutyUvw, &mtrCtrl->dutyUvw); - } else if (sampleMode == SINGLE_RESISTOR) { - R1SVPWM_Exec(&mtrCtrl->r1Sv, &mtrCtrl->vabRef, &mtrCtrl->dutyUvwLeft, &mtrCtrl->dutyUvwRight); - mtrCtrl->setPwmDutyCb(&mtrCtrl->dutyUvwLeft, &mtrCtrl->dutyUvwRight); - /* The ADC sampling point position needs to be set based on the phase shift of a single resistors. */ - mtrCtrl->setADCTriggerTimeCb(mtrCtrl->r1Sv.samplePoint[0] * mtrCtrl->aptMaxcntCmp, \ - mtrCtrl->r1Sv.samplePoint[1] * mtrCtrl->aptMaxcntCmp); - } -} - -/** - * @brief Carrier interrupt function. - * @param mtrCtrl The motor control handle. - * @retval None. - */ -void MCS_CarrierProcess(MTRCTRL_Handle *mtrCtrl) -{ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - UvwAxis *currUvw = &mtrCtrl->currUvw; - AlbeAxis *currAlbe = &mtrCtrl->iabFbk; - AlbeAxis *vab = &mtrCtrl->vabRef; - SampleMode sampleMode = mtrCtrl->sampleMode; - /* sample mode verify */ - if (sampleMode > SINGLE_RESISTOR || mtrCtrl->setADCTriggerTimeCb == NULL) { - return; - } - /* param verify */ - if (mtrCtrl->setPwmDutyCb == NULL || mtrCtrl->readCurrUvwCb == NULL) { - return; - } - /* Read the three-phase current value. */ - mtrCtrl->readCurrUvwCb(currUvw); - /* Clark Calc */ - ClarkeCalc(currUvw, currAlbe); - /* Smo observation */ - if (mtrCtrl->obserType == FOC_OBSERVERTYPE_SMO1TH) { - /* Smo observation */ - FOSMO_Exec(&mtrCtrl->smo, currAlbe, vab, mtrCtrl->spdRefHz); - } else if (mtrCtrl->obserType == FOC_OBSERVERTYPE_SMO4TH) { - /* Smo4th observation */ - SMO4TH_Exec(&mtrCtrl->smo4th, currAlbe, vab); - mtrCtrl->smo.spdEst = mtrCtrl->smo4th.spdEst; - mtrCtrl->smo.elecAngle = mtrCtrl->smo4th.elecAngle; - } - /* Synchronization angle */ - MCS_SyncCoorAngle(mtrCtrl); - - /* Park transformation */ - ParkCalc(currAlbe, mtrCtrl->axisAngle, &mtrCtrl->idqFbk); - - /* statemachine */ - switch (mtrCtrl->stateMachine) { - case FSM_STARTUP: - case FSM_RUN: - CURRCTRL_Exec(&mtrCtrl->currCtrl, &mtrCtrl->vdqRef, mtrCtrl->smo.spdEst, 0); - InvParkCalc(&mtrCtrl->vdqRef, mtrCtrl->axisAngle, vab); - MCS_PwmAdcSet(mtrCtrl); - break; - - case FSM_CAP_CHARGE: - case FSM_CLEAR: - case FSM_IDLE: - mtrCtrl->smo4th.spdEst = 0.0f; - break; - - default: - vab->alpha = 0.0f; - vab->beta = 0.0f; - MCS_PwmAdcSet(mtrCtrl); - break; - } -} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/src/mcs_motor_process.c b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/src/mcs_motor_process.c deleted file mode 100644 index 79bc3949..00000000 --- a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/src/mcs_motor_process.c +++ /dev/null @@ -1,950 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_motor_process.c - * @author MCU Algorithm Team - * @brief This file provides motor application. - * @details Single FOC application based on the ECMCU105H/ECBMCU201MPC board - * 1) Motor model is Gimbal GBM2804H-100T. - * 2) Select the pmsm sensorless 2shunt foc example in the sample column - of chipConfig and click Generate Code. - * 3) It's power supply must be changed to 12V. - */ -#include "main.h" -#include "mcs_user_config.h" -#include "mcs_math.h" -#include "hmi_module.h" -#include "mcs_ctlmode_config.h" -#include "mcs_prot_user.h" -#include "mcs_prot_user_config.h" -#include "mcs_math_const.h" -#include "mcs_motor_process.h" -#include "mcs_chip_config.h" -#include - -/*------------------------------- Macro Definition -----------------------------------------------*/ -#define US_PER_MS 1000 -#define ANGLE_RANGE_ABS 65536 -#define ANGLE_360_F 65536.0f /* 0 - 65536 indicates 0 to 360. */ -#define APT_FULL_DUTY 1.0f -#define TEMP_3 3.0f -#define TEMP_15 15.0f -#define TEMP_30 30.0f -#define TEMP_45 45.0f -#define TEMP_60 60.0f -#define TEMP_RES_15 78.327f -#define TEMP_RES_30 36.776f -#define TEMP_RES_45 18.301f -#define TEMP_RES_60 9.607f -#define CNT_10 10 -#define CNT_5000 5000 -#define LEVEL_4 4 -#define MOTOR_START_DELAY 2 -#define ADC_READINIT_DELAY 1 -#define ADC_READINIT_TIMES 20 -#define ADC_TRIMVALUE_MIN 1800.0f -#define ADC_TRIMVALUE_MAX 2200.0f -/*------------------------------- Param Definition -----------------------------------------------*/ -/* Motor parameters. */ -/* Np, Rs, Ld, Lq, Psif, J, Nmax, Currmax, PPMR, zShift */ -static MOTOR_Param g_motorParam = MOTORPARAM_DEFAULTS; -static APT_RegStruct* g_apt[PHASE_MAX_NUM] = {APT_U, APT_V, APT_W}; -/* Motor control handle */ -static MTRCTRL_Handle g_mc = {0}; - -/* Motor speed loop PI param. */ -static void SPDCTRL_InitWrapper(SPDCTRL_Handle *spdHandle, float ts) -{ - /* Speed loop param assignment. */ - PI_Param spdPi = { - .kp = SPD_KP, - .ki = SPD_KI, - .lowerLim = SPD_LOWERLIM, - .upperLim = SPD_UPPERLIM, - }; - /* Speed loop param init. */ - SPDCTRL_Init(spdHandle, &g_motorParam, spdPi, ts); -} - -/* Motor current Loop PI param. */ -static void CURRCTRL_InitWrapper(CURRCTRL_Handle *currHandle, DqAxis *idqRef, DqAxis *idqFbk, float ts) -{ - /* Axis-D current loop param assignment. */ - PI_Param dCurrPi = { - .kp = CURRDAXIS_KP, - .ki = CURRDAXIS_KI, - .lowerLim = CURR_LOWERLIM, - .upperLim = CURR_UPPERLIM, - }; - /* Axis-Q current loop param assignment. */ - PI_Param qCurrPi = { - .kp = CURRQAXIS_KP, - .ki = CURRQAXIS_KI, - .lowerLim = CURR_LOWERLIM, - .upperLim = CURR_UPPERLIM, - }; - /* Current loop param init. */ - CURRCTRL_Init(currHandle, &g_motorParam, idqRef, idqFbk, dCurrPi, qCurrPi, ts); -} - -/* First order smo param. */ -static void FOSMO_InitWrapper(FOSMO_Handle *fosmo, float ts) -{ - /* Smo param assignment. */ - FOSMO_Param fosmoParam = { - .gain = FOSMO_GAIN, - .lambda = FOSMO_LAMBDA, - .fcEmf = FOSMO_EMF_CUTOFF_FREQ, - .fcLpf = SPEED_FILTER_CUTOFF_FREQUENCY, - .pllBdw = FOSMO_PLL_BDW, - }; - /* Init smo param. */ - FOSMO_Init(fosmo, fosmoParam, &g_motorParam, ts); -} - -/* Smo4th param. */ -static void SMO4TH_InitWrapper(SMO4TH_Handle *smo4TH) -{ - /* Smo4th param assignment. */ - SMO4TH_Param smo4thParam = { - .kd = SMO4TH_KD, - .kq = SMO4TH_KQ, - .pllBdw = SMO4TH_PLL_BDW, - .fcLpf = SMO4TH_SPD_FILTER_CUTOFF_FREQ, - }; - /* Init smo param. */ - SMO4TH_Init(smo4TH, smo4thParam, &g_motorParam, CTRL_CURR_PERIOD); -} - -/*------------------------------- Function Definition -----------------------------------------------*/ -/** - * @brief Initialzer of system tick. - * @param mtrCtrl Motor control struct handle. - * @retval None. - */ -static void TimerTickInit(MTRCTRL_Handle *mtrCtrl) -{ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - mtrCtrl->sysTickCnt = 0; - mtrCtrl->msTickNum = US_PER_MS / SYSTICK_PERIOD_US; - mtrCtrl->capChargeTickNum = (INV_CAP_CHARGE_MS * US_PER_MS / SYSTICK_PERIOD_US); -} - -/** - * @brief Init motor control task. - * @retval None. - */ -static void TSK_Init(void) -{ - g_mc.motorStateFlag = 0; - g_mc.uartHeartDetCnt = 0; - g_mc.uartTimeStamp = 0; - g_mc.stateMachine = FSM_IDLE; - g_mc.currCtrlPeriod = CTRL_CURR_PERIOD; /* Init current controller */ - g_mc.aptMaxcntCmp = g_apt0.waveform.timerPeriod; - g_mc.sampleMode = DUAL_RESISTORS; - g_mc.obserType = FOC_OBSERVERTYPE_SMO4TH; /* Init foc observe mode */ - g_mc.controlMode = FOC_CONTROLMODE_SPEED; /* Init motor control mode */ - g_mc.adcCurrCofe = ADC_CURR_COFFI; - g_mc.spdAdjustMode = CUST_SPEED_ADJUST; - g_mc.uartConnectFlag = DISCONNECT; - g_mc.spdCmdHz = USER_MIN_SPD_HZ; /* Motor initialization speed */ - - g_mc.adc0Compensate = ADC0COMPENSATE; /* Phase-u current init adc shift trim value */ - g_mc.adc1Compensate = ADC1COMPENSATE; /* Phase-w current init adc shift trim value */ - - IF_Init(&g_mc.ifCtrl, CTRL_IF_CURR_AMP_A, USER_CURR_SLOPE, CTRL_SYSTICK_PERIOD, CTRL_CURR_PERIOD); - RMG_Init(&g_mc.spdRmg, CTRL_SYSTICK_PERIOD, USER_SPD_SLOPE); /* Init speed slope */ - MtrParamInit(&g_mc.mtrParam, g_motorParam); - - TimerTickInit(&g_mc); - if (g_mc.sampleMode == DUAL_RESISTORS) { - SVPWM_Init(&g_mc.sv, INV_VOLTAGE_BUS * ONE_DIV_SQRT3); /* Dual resistors SVPWM init. */ - } else if (g_mc.sampleMode == SINGLE_RESISTOR) { - /* Single resistor SVPWM init. */ - R1SVPWM_Init(&g_mc.r1Sv, INV_VOLTAGE_BUS * ONE_DIV_SQRT3, SAMPLE_POINT_SHIFT, SAMPLE_WINDOW_DUTY); - } - - SPDCTRL_InitWrapper(&g_mc.spdCtrl, CTRL_SYSTICK_PERIOD); - CURRCTRL_InitWrapper(&g_mc.currCtrl, &g_mc.idqRef, &g_mc.idqFbk, CTRL_CURR_PERIOD); - FOSMO_InitWrapper(&g_mc.smo, CTRL_CURR_PERIOD); - SMO4TH_InitWrapper(&g_mc.smo4th); - - STARTUP_Init(&g_mc.startup, USER_SWITCH_SPDBEGIN_HZ, USER_SWITCH_SPDBEGIN_HZ + TEMP_3); - - MotorProt_Init(&g_mc.prot); /* Init protect state comond */ - OCP_Init(&g_mc.prot.ocp, CTRL_CURR_PERIOD); - OVP_Init(&g_mc.prot.ovp, CTRL_SYSTICK_PERIOD); - LVP_Init(&g_mc.prot.lvp, CTRL_SYSTICK_PERIOD); - OTP_Init(&g_mc.prot.otp, CTRL_SYSTICK_PERIOD); - STP_Init(&g_mc.prot.stall, CTRL_SYSTICK_PERIOD, PROT_STALLING_CURR_AMP_LIMIT, - PROT_STALLING_SPD_LIMIT, PROT_STALLING_TIME_LIMIT); -} - -/** - * @brief Clear historical values of all controller before start-up. - * @param mtrCtrl The motor control handle. - * @retval None. - */ -static void ClearBeforeStartup(MTRCTRL_Handle *mtrCtrl) -{ - /* Verify Parameters */ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - /* The initial angle is 0. */ - mtrCtrl->axisAngle = 0; - - mtrCtrl->spdRefHz = 0.0f; - /* The initial dq-axis reference current is 0. */ - mtrCtrl->idqRef.d = 0.0f; - mtrCtrl->idqRef.q = 0.0f; - - mtrCtrl->vdqRef.d = 0.0f; - mtrCtrl->vdqRef.q = 0.0f; - /* Clear Duty Cycle Value. The initial duty cycle is 0.5. */ - mtrCtrl->dutyUvwLeft.u = 0.5f; - mtrCtrl->dutyUvwLeft.v = 0.5f; - mtrCtrl->dutyUvwLeft.w = 0.5f; - mtrCtrl->dutyUvwRight.u = 0.5f; - mtrCtrl->dutyUvwRight.v = 0.5f; - mtrCtrl->dutyUvwRight.w = 0.5f; - - mtrCtrl->prot.motorErrStatus.all = 0x00; - - RMG_Clear(&mtrCtrl->spdRmg); /* Clear the history value of speed slope control */ - CURRCTRL_Clear(&mtrCtrl->currCtrl); - IF_Clear(&mtrCtrl->ifCtrl); - SPDCTRL_Clear(&mtrCtrl->spdCtrl); - FOSMO_Clear(&mtrCtrl->smo); - SMO4TH_Clear(&mtrCtrl->smo4th); - STARTUP_Clear(&mtrCtrl->startup); - R1SVPWM_Clear(&mtrCtrl->r1Sv); - - OTP_Clear(&mtrCtrl->prot.otp); - OCP_Clear(&mtrCtrl->prot.ocp); - OVP_Clear(&mtrCtrl->prot.ovp); - LVP_Clear(&mtrCtrl->prot.lvp); -} - -/** - * @brief To set the comparison value of the IGBT single-resistance ADC sampling trigger position. - * @param aptx The APT register struct handle. - * @param cntCmpA A Count compare reference of time-base counter. - * @param cntCmpB B Count compare reference of time-base counter. - * @param maxCntCmp Maximum Comparison Value - * @retval None. - */ -static void MCS_SetAdcCompareR1(APT_RegStruct *aptx, unsigned short cntCmpA, - unsigned short cntCmpB, unsigned short maxCntCmp) -{ - unsigned short tmp; - /* Sets the A Count compare reference of time-base counter. */ - tmp = (unsigned short)Clamp((float)(cntCmpA), (float)(maxCntCmp - 1), 1.0f); - DCL_APT_SetCounterCompare(aptx, APT_COMPARE_REFERENCE_A, tmp); - /* Sets the B Count compare reference of time-base counter. */ - tmp = (unsigned short)Clamp((float)(cntCmpB), (float)(maxCntCmp - 1), 1.0f); - DCL_APT_SetCounterCompare(aptx, APT_COMPARE_REFERENCE_B, tmp); -} - -/** - * @brief Open the three-phase lower pipe. - * @param aptAddr Three-phase APT address pointer. - * @param maxDutyCnt Max duty count. - * @retval None. - */ -static void AptTurnOnLowSidePwm(APT_RegStruct **aptAddr, unsigned short maxDutyCnt) -{ - MCS_ASSERT_PARAM(aptAddr != NULL); - MCS_ASSERT_PARAM(maxDutyCnt != 0); - unsigned short dutyCnt; - dutyCnt = (unsigned short)(maxDutyCnt * APT_FULL_DUTY); - /* Open the three-phase lower pipe */ - for (unsigned int i = 0; i < PHASE_MAX_NUM; i++) { - APT_RegStruct *aptx = (APT_RegStruct *)(aptAddr[i]); - DCL_APT_SetCounterCompare(aptx, APT_COMPARE_REFERENCE_C, dutyCnt); - DCL_APT_SetCounterCompare(aptx, APT_COMPARE_REFERENCE_D, dutyCnt); - } -} - -/** - * @brief Enable three-phase pwm output. - * @param aptAddr Three-phase APT address pointer. - * @retval None. - */ -static void MotorPwmOutputEnable(APT_RegStruct **aptAddr) -{ - MCS_ASSERT_PARAM(aptAddr != NULL); - /* Enable three-phase pwm output */ - for (unsigned int i = 0; i < PHASE_MAX_NUM; i++) { - APT_RegStruct *aptx = (APT_RegStruct *)(aptAddr[i]); - aptx->PG_OUT_FRC.BIT.rg_pga_frc_en = BASE_CFG_UNSET; - aptx->PG_OUT_FRC.BIT.rg_pgb_frc_en = BASE_CFG_UNSET; - } -} - -/** - * @brief Disable three-phase pwm output. - * @param aptAddr Three-phase APT address pointer. - * @retval None. - */ -static void MotorPwmOutputDisable(APT_RegStruct **aptAddr) -{ - MCS_ASSERT_PARAM(aptAddr != NULL); - /* Disable three-phase pwm output. */ - for (unsigned int i = 0; i < PHASE_MAX_NUM; i++) { - APT_RegStruct *aptx = (APT_RegStruct *)(aptAddr[i]); - aptx->PG_OUT_FRC.BIT.rg_pga_frc_en = BASE_CFG_SET; - aptx->PG_OUT_FRC.BIT.rg_pgb_frc_en = BASE_CFG_SET; - DCL_APT_ForcePWMOutputLow(aptx); - } -} - -/** - * @brief Smo IF angle difference calculation. - * @param smoElecAngle Smo electrical angle. - * @param ifCtrlAngle IF control angle. - * @retval signed short angle difference. - */ -static float SmoIfAngleDiffCalc(float smoElecAngle, float ifCtrlAngle) -{ - float diff = AngleSub(smoElecAngle, ifCtrlAngle); - /* Smo IF angle difference calculation */ - return diff; -} - -/** - * @brief Construct a new mcs startupswitch object. - * @param mtrCtrl The motor control handle. - * @retval None. - */ -static void MCS_StartupSwitch(MTRCTRL_Handle *mtrCtrl) -{ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - STARTUP_Handle *startup = &mtrCtrl->startup; - DqAxis *idqRef = &mtrCtrl->idqRef; - float iftargetAmp = mtrCtrl->ifCtrl.targetAmp; - float spdRefHz = mtrCtrl->spdRefHz; - - switch (startup->stage) { - case STARTUP_STAGE_CURR: - if (mtrCtrl->ifCtrl.curAmp >= iftargetAmp) { - /* Stage change */ - idqRef->q = iftargetAmp; - startup->stage = STARTUP_STAGE_SPD; - } else { - /* current amplitude increase */ - idqRef->q = IF_CurrAmpCalc(&mtrCtrl->ifCtrl); - spdRefHz = 0.0f; - } - break; - case STARTUP_STAGE_SPD: - /* current frequency increase */ - if (Abs(spdRefHz) >= startup->spdBegin) { - /* Stage change */ - startup->stage = STARTUP_STAGE_SWITCH; - TrigVal localTrigVal; - TrigCalc(&localTrigVal, SmoIfAngleDiffCalc(mtrCtrl->smo.elecAngle, mtrCtrl->ifCtrl.angle)); - idqRef->d = 0.0f; - mtrCtrl->spdCtrl.spdPi.integral = iftargetAmp * localTrigVal.cos; - } else { - /* Speed rmg */ - spdRefHz = RMG_Exec(&mtrCtrl->spdRmg, mtrCtrl->spdCmdHz); - } - break; - - case STARTUP_STAGE_SWITCH: - /* Switch from IF to SMO */ - spdRefHz = RMG_Exec(&mtrCtrl->spdRmg, mtrCtrl->spdCmdHz); - idqRef->q = SPDCTRL_Exec(&mtrCtrl->spdCtrl, mtrCtrl->spdRefHz, mtrCtrl->smo.spdEst); - /* Transitional stage, if current reference speed > critical speed, change to next stage */ - if (spdRefHz >= startup->spdBegin + TEMP_3) { - /* Stage change */ - mtrCtrl->stateMachine = FSM_RUN; - } - break; - - default: - break; - } - mtrCtrl->spdRefHz = spdRefHz; -} - -/** - * @brief Pre-processing of motor status. - * @param statusReg System status. - * @param stateMachine Motor Control Status. - * @retval None. - */ -static void MotorStatePerProc(SysStatusReg *statusReg, volatile FsmState *stateMachine) -{ - MCS_ASSERT_PARAM(statusReg != NULL); - MCS_ASSERT_PARAM(stateMachine != NULL); - /* Get system status */ - if (SysIsError(statusReg)) { - *stateMachine = FSM_FAULT; - } - if (SysGetCmdStop(statusReg)) { - SysCmdStopClr(statusReg); - *stateMachine = FSM_STOP; - } -} - -/** - * @brief Check over current status. - * @param statusReg System status. - * @param stateMachine Motor Control Status. - * @retval None. - */ -static void CheckOverCurrentState(SysStatusReg *statusReg, FsmState *stateMachine) -{ - MCS_ASSERT_PARAM(statusReg != NULL); - MCS_ASSERT_PARAM(stateMachine != NULL); - /* check systerm error status */ - if (SysIsError(statusReg) == false) { - *stateMachine = FSM_IDLE; - } -} - -/** - * @brief Check bootstrap capacitor charge time. - * @param mtrCtrl The motor control handle. - * @param stateMachine Motor Control Status. - * @retval None. - */ -static void CheckBootstrpCapChargeTime(MTRCTRL_Handle *mtrCtrl, FsmState *stateMachine) -{ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - MCS_ASSERT_PARAM(stateMachine != NULL); - mtrCtrl->sysTickCnt++; - /* check bootstrap capacitor charge time */ - if (mtrCtrl->sysTickCnt == mtrCtrl->capChargeTickNum) { - *stateMachine = FSM_CLEAR; - } -} - -/** - * @brief Check systerm cmd start signal. - * @param mtrCtrl The motor control handle. - * @param aptAddr Three-phase APT address pointer. - * @param statusReg System status. - * @param stateMachine Motor Control Status. - * @retval None. - */ -static void CheckSysCmdStart(MTRCTRL_Handle *mtrCtrl, - APT_RegStruct **aptAddr, - SysStatusReg *statusReg, - FsmState *stateMachine) -{ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - MCS_ASSERT_PARAM(aptAddr != NULL); - MCS_ASSERT_PARAM(statusReg != NULL); - MCS_ASSERT_PARAM(stateMachine != NULL); - /* check start cmd */ - if (SysGetCmdStart(statusReg)) { - SysRunningSet(statusReg); - SysCmdStartClr(statusReg); - mtrCtrl->sysTickCnt = 0; - *stateMachine = FSM_CAP_CHARGE; - /* Preparation for charging the bootstrap capacitor. */ - AptTurnOnLowSidePwm(aptAddr, mtrCtrl->aptMaxcntCmp); - /* Out put pwm */ - MotorPwmOutputEnable(aptAddr); - } -} - -/** - * @brief System timer tick task. - * @param mtrCtrl The motor control handle. - * @param aptAddr Three-phase APT address pointer. - * @retval None. - */ -static void TSK_SystickIsr(MTRCTRL_Handle *mtrCtrl, APT_RegStruct **aptAddr) -{ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - MCS_ASSERT_PARAM(aptAddr != NULL); - SysStatusReg *statusReg = &mtrCtrl->statusReg; - FsmState *stateMachine = &mtrCtrl->stateMachine; - mtrCtrl->msTickCnt++; - /* Pre-processing of motor status. */ - MotorStatePerProc(statusReg, stateMachine); - /* statemachine */ - switch (*stateMachine) { - case FSM_IDLE: - /* Set smo estimate speed before motor start-up */ - g_mc.smo.spdEst = 0.0f; - CheckSysCmdStart(mtrCtrl, aptAddr, statusReg, stateMachine); - break; - case FSM_CAP_CHARGE: - /* Bootstrap Capacitor Charging Timing */ - CheckBootstrpCapChargeTime(mtrCtrl, stateMachine); - break; - /* Clear parameter before start */ - case FSM_CLEAR: - ClearBeforeStartup(mtrCtrl); - *stateMachine = FSM_STARTUP; - break; - case FSM_STARTUP: - MCS_StartupSwitch(mtrCtrl); - break; - case FSM_RUN: - /* Speed ramp control */ - mtrCtrl->spdRefHz = RMG_Exec(&mtrCtrl->spdRmg, mtrCtrl->spdCmdHz); - /* Speed loop control */ - mtrCtrl->idqRef.q = SPDCTRL_Exec(&mtrCtrl->spdCtrl, mtrCtrl->spdRefHz, mtrCtrl->smo.spdEst); - break; - case FSM_STOP: - mtrCtrl->spdRefHz = 0.0f; - MotorPwmOutputDisable(aptAddr); - SysRunningClr(statusReg); - *stateMachine = FSM_IDLE; - break; - case FSM_FAULT: /* Overcurrent state */ - CheckOverCurrentState(statusReg, stateMachine); - break; - default: - break; - } -} - -/** - * @brief Read the ADC initialize bias trim value. - * @param mtrCtrl The motor control handle. - * @retval None. - */ -static void TrimInitAdcShiftValue(MTRCTRL_Handle *mtrCtrl) -{ - float adc0SampleTemp = 0.0f; /* Current bias value for temp store */ - float adc1SampleTemp = 0.0f; - float adc0TempSum = 0.0f; - float adc1TempSum = 0.0f; /* Current bias sum value for 20 times */ - float adcSampleTimes = 0.0f; /* ADC sample times */ - for (int i = 0; i < ADC_READINIT_TIMES; i++) { - adc0SampleTemp = (float)HAL_ADC_GetConvResult(&ADCU_HANDLE, ADCUSOCNUM); - adc1SampleTemp = (float)HAL_ADC_GetConvResult(&ADCW_HANDLE, ADCWSOCNUM); - BASE_FUNC_DELAY_US(200); /* 200 is delay count, delay 200us triger adc sampling */ - if (adc0SampleTemp > 1000.0f && adc1SampleTemp > 1000.0f) { - adcSampleTimes++; - adc0TempSum += adc0SampleTemp; - adc1TempSum += adc1SampleTemp; - } - } - if (adcSampleTimes < 1.0f) { - adcSampleTimes = 1.0f; /* Prevent divide-by-zero errors */ - } - adc0SampleTemp = adc0TempSum / adcSampleTimes; - adc1SampleTemp = adc1TempSum / adcSampleTimes; - /* Force convert to float */ - mtrCtrl->adc0Compensate = (float) adc0SampleTemp; - mtrCtrl->adc1Compensate = (float) adc1SampleTemp; - /* The normal value scope: 1800 < adc0Compensate < 2200 */ - if(g_mc.adc0Compensate < ADC_TRIMVALUE_MIN || g_mc.adc0Compensate > ADC_TRIMVALUE_MAX \ - || g_mc.adc1Compensate < ADC_TRIMVALUE_MIN || g_mc.adc1Compensate > ADC_TRIMVALUE_MAX) { - DBG_PRINTF("ADC trim value error,please reset!"); - HAL_GPIO_SetValue(&LED2_HANDLE, LED2_PIN, GPIO_LOW_LEVEL); - } - adcSampleTimes = 0; - adc0TempSum = 0; - adc1TempSum = 0; -} - -/** - * @brief Read the ADC current sampling value. - * @param CurrUvw Three-phase current. - * @retval None. - */ -static void ReadCurrUvw(UvwAxis *CurrUvw) -{ - MCS_ASSERT_PARAM(CurrUvw != NULL); - float adc0 = (float)HAL_ADC_GetConvResult(&ADCU_HANDLE, ADCUSOCNUM); - float adc1 = (float)HAL_ADC_GetConvResult(&ADCW_HANDLE, ADCWSOCNUM); - /* Convert adc sample value to current value */ - CurrUvw->u = -(adc0 - g_mc.adc0Compensate) * g_mc.adcCurrCofe; - CurrUvw->w = -(adc1 - g_mc.adc1Compensate) * g_mc.adcCurrCofe; - CurrUvw->v = -CurrUvw->u - CurrUvw->w; -} - -/** - * @brief Setting the APT Output Duty Cycle. - * @param aptx APT register base address. - * @param leftDuty Left duty cycle. - * @param rightDuty Right duty cycle. - * @retval None. - */ -static void SetPwmDuty(APT_Handle *aptx, float leftDuty, float rightDuty) -{ - MCS_ASSERT_PARAM(aptx != NULL); - MCS_ASSERT_PARAM(leftDuty > 0); - MCS_ASSERT_PARAM(rightDuty > 0); - unsigned short maxPeriodCnt = aptx->waveform.timerPeriod; - unsigned short cntCmpLeftEdge = (unsigned short)(leftDuty * maxPeriodCnt); - unsigned short cntCmpRightEdge = (unsigned short)(rightDuty * maxPeriodCnt); - /* avoid overflowing */ - cntCmpLeftEdge = (cntCmpLeftEdge > maxPeriodCnt) ? maxPeriodCnt : cntCmpLeftEdge; - cntCmpRightEdge = (cntCmpRightEdge > maxPeriodCnt) ? maxPeriodCnt : cntCmpRightEdge; - HAL_APT_SetPWMDuty(aptx, cntCmpLeftEdge, cntCmpRightEdge); -} - -/** - * @brief Duty Cycle Setting. - * @param dutyUvwLeft Three-phase left duty cycle. - * @param dutyUvwRight Three-phase right duty cycle. - * @retval None. - */ -static void SetPwmDutyCp(UvwAxis *dutyUvwLeft, UvwAxis *dutyUvwRight) -{ - MCS_ASSERT_PARAM(dutyUvwLeft != NULL); - MCS_ASSERT_PARAM(dutyUvwRight != NULL); - /* Setting the Three-Phase Duty Cycle */ - SetPwmDuty(&g_apt0, dutyUvwLeft->u, dutyUvwRight->u); - SetPwmDuty(&g_apt1, dutyUvwLeft->v, dutyUvwRight->v); - SetPwmDuty(&g_apt2, dutyUvwLeft->w, dutyUvwRight->w); -} - -/** - * @brief To set the ADC sampling trigger comparison value. - * @param cntCmpSOCA Soca Compare Count Value. - * @param cntCmpSOCB Socb Compare Count Value. - * @retval None. - */ -static void SetADCTriggerTime(unsigned short cntCmpSOCA, unsigned short cntCmpSOCB) -{ - MCS_SetAdcCompareR1(g_apt[PHASE_U], cntCmpSOCA, cntCmpSOCB, g_mc.aptMaxcntCmp); -} - -/** - * @brief Temprature table, the temprature detect range is 15 ~ 60 degree. - * @param tempResisValue Temperature sensor resistance. - * @retval None. - */ -static float TempTable(float tempResisValue) -{ - float boardTemp = 0.0f; - /* Temperatures between 15 and 30. */ - if (tempResisValue > TEMP_RES_30 && tempResisValue <= TEMP_RES_15) { - boardTemp = TEMP_15 + (TEMP_30 - TEMP_15) * (TEMP_RES_15 - tempResisValue) / (TEMP_RES_15 - TEMP_RES_30); - } else if (tempResisValue > TEMP_RES_45 && tempResisValue <= TEMP_RES_30) { /* Temperature between 30 and 45. */ - boardTemp = TEMP_30 + (TEMP_45 - TEMP_30) * (TEMP_RES_30 - tempResisValue) / (TEMP_RES_30 - TEMP_RES_45); - } else if (tempResisValue > TEMP_RES_60 && tempResisValue <= TEMP_RES_45) { /* Temperature between 45 and 50. */ - boardTemp = TEMP_45 + (TEMP_60 - TEMP_45) * (TEMP_RES_45 - tempResisValue) / (TEMP_RES_45 - TEMP_RES_60); - } else if (tempResisValue <= TEMP_RES_60) { - boardTemp = TEMP_60; /* If temperature is over 60, set temperature as 60. */ - } else if (tempResisValue >= TEMP_RES_15) { - boardTemp = TEMP_15; /* If temperature is lower 15, set temperature as 15. */ - } - return boardTemp; -} - -/** - * @brief Read power board temperature and udc. - * @retval None. - */ -static void ReadBoardTempAndUdc(void) -{ - HAL_ADC_SoftTrigSample(&ADCRESIS_HANDLE, ADCRESISSOCNUM); - HAL_ADC_SoftTrigSample(&ADCUDC_HANDLE, ADCUDCSOCNUM); - BASE_FUNC_DELAY_US(CNT_10); /* Delay 10 us. */ - /* Force convert to float type. */ - float resisAdcValue = (float)HAL_ADC_GetConvResult(&ADCRESIS_HANDLE, ADCRESISSOCNUM); - /* 10 / (x + 10) * 3.3 = resisAdcValue / 4096 * 3.3, x is resisValue, 10kohm is resistor divider value. */ - float resisValue = (4096.0f * 10.0f - 10.0f * resisAdcValue) / resisAdcValue; - g_mc.powerBoardTemp = TempTable(resisValue); - g_mc.udc = ((float)HAL_ADC_GetConvResult(&ADCUDC_HANDLE, ADCUDCSOCNUM)) * ADC_UDC_COFFI; -} - -/** - * @brief Execut abnormal feedback speed protect motion. - * @retval None. - */ -static void SpdFbkErrorProt_Exec(void) -{ - if (g_mc.prot.motorErrStatus.Bit.motorStalling == 0 && - g_mc.prot.motorErrStatus.Bit.overVoltErr == 0 && - g_mc.prot.motorErrStatus.Bit.lowerVoltErr == 0 && - g_mc.prot.motorErrStatus.Bit.overIpmTempErr == 0 && - g_mc.prot.motorErrStatus.Bit.overCurrErr == 0) { - g_mc.prot.motorErrStatus.Bit.revRotErr = 1; - /* If revRotErr, execute protect motion. */ - ProtSpo_Exec(g_apt); - } -} - -/** - * @brief Execut nan data protect motion. - * @retval None. - */ -static void NanDataDetect(void) -{ - static short errorSpdStatus = 0; - /* Detect the nan observer speed or current value. */ - if (isnan(g_mc.smo.spdEst) || isnan(g_mc.idqRef.q)) { - errorSpdStatus++; - } else { - errorSpdStatus = 0; - } - /* If the data is nan & continuous counting value is over 500 times, execute protect motion. - the detect time is 500 * 500us = 250ms. */ - if (errorSpdStatus >= 500) { - errorSpdStatus = 0; - SpdFbkErrorProt_Exec(); - } -} - -/** - * @brief Check abnormal feedback speed. - * @retval None. - */ -static void CheckSpdFbkStatus(void) -{ - static short errorCurrStatus = 0; - static short errorDeltaSpdStatus = 0; - NanDataDetect(); - if (g_mc.stateMachine == FSM_RUN) { - /* Detect the abnormal idq feedback current. */ - if (Abs(g_mc.idqRef.q - g_mc.idqFbk.q) >= CTRL_IF_CURR_AMP_A) { - errorCurrStatus++; - } else { - errorCurrStatus = 0; - } - /* Detect the abnormal feedback speed, the normal speed is > 0, if smo.spdEst < -10 && - delta speed error > USER_MIN_SPD_HZ + 10.0f at FSM_RUN stage, set the motor motion as error */ - if (g_mc.smo.spdEst < -10.0f && (g_mc.spdRefHz - g_mc.smo.spdEst > USER_MIN_SPD_HZ + 10.0f)) { - errorDeltaSpdStatus++; - } - } - /* Execute protect motion if count over 500 times, this error status caused by abnormal speed - or cabnormal urrent feedback, the detect time is 500 * 500us = 250ms. */ - if (errorCurrStatus >= 500) { - errorCurrStatus = 0; - SpdFbkErrorProt_Exec(); - } - /* This error statu caused by motor stalling, the detect time is 2 * 500us = 1ms. */ - if (errorDeltaSpdStatus >= 2) { - errorDeltaSpdStatus = 0; - g_mc.prot.motorErrStatus.Bit.motorStalling = 1; - } -} - -/** - * @brief Check Potentiometer Value callback function. - * @param param The TIMER_Handle. - * @retval None. - */ -void CheckPotentiometerValueCallback(void *param) -{ - MCS_ASSERT_PARAM(param != NULL); - BASE_FUNC_UNUSED(param); - static float potentiomitorAdcValue = 0.0f; - static float spdCmdHz = 0; - static float spdCmdHzLast = USER_MIN_SPD_HZ; /* 35.0 is spdCmdHzLast init value */ - HAL_ADC_SoftTrigSample(&ADCPTT_HANDLE, ADCPTTSOCNUM); - BASE_FUNC_DELAY_US(10); /* Delay 10 us. */ - potentiomitorAdcValue = (float)HAL_ADC_GetConvResult(&ADCPTT_HANDLE, ADCPTTSOCNUM); - /* 4045.0 is adc sample max value of potentiomitor, convert max spd to 180.25Hz */ - spdCmdHz = potentiomitorAdcValue / 4045.0f * USER_MAX_SPD_HZ; - if (Abs(spdCmdHzLast - spdCmdHz) < 1.0f) { /* Ignore changes less than 1. */ - return; - } - spdCmdHzLast = spdCmdHz; - if (spdCmdHz < USER_MIN_SPD_HZ) { /* 35.0 is spdCmdHz lower limit */ - spdCmdHz = USER_MIN_SPD_HZ; /* 35.0 is spdCmdHz lower limit */ - } - if (spdCmdHz > g_mc.mtrParam.maxElecSpd) { /* spdCmdHz upper limit */ - spdCmdHz = g_mc.mtrParam.maxElecSpd; /* spdCmdHz upper limit */ - } - if (g_mc.spdAdjustMode == CUST_SPEED_ADJUST) { - g_mc.spdCmdHz = spdCmdHz; - } -} - -/** - * @brief System timer ISR for Motor Statemachine CallBack function. - * @param param The systick timer handle. - * @retval None. - */ -void MotorStatemachineCallBack(void *param) -{ - /* Verify Parameters */ - MCS_ASSERT_PARAM(param != NULL); - BASE_FUNC_UNUSED(param); - /* Read power board temprature and voltage. */ - ReadBoardTempAndUdc(); - /* Motor speed loop state machine. */ - TSK_SystickIsr(&g_mc, g_apt); - - /* Motor error speed feedback check. */ - CheckSpdFbkStatus(); - /* Motor stalling detect. */ - STP_Det_ByCurrSpd(&g_mc.prot.stall, &g_mc.prot.motorErrStatus, g_mc.smo.spdEst, g_mc.idqFbk); - STP_Exec(&g_mc.prot.motorErrStatus, g_apt); - - /* Motor over voltage detect. */ - OVP_Det(&g_mc.prot.ovp, &g_mc.prot.motorErrStatus, g_mc.udc); - OVP_Exec(&g_mc.prot.ovp, &g_mc.spdRefHz, g_apt); - OVP_Recy(&g_mc.prot.ovp, &g_mc.prot.motorErrStatus, g_mc.udc); - /* Motor lower voltage detect. */ - LVP_Det(&g_mc.prot.lvp, &g_mc.prot.motorErrStatus, g_mc.udc); - LVP_Exec(&g_mc.prot.lvp, &g_mc.spdRefHz, g_apt); - LVP_Recy(&g_mc.prot.lvp, &g_mc.prot.motorErrStatus, g_mc.udc); - /* Power board over temperature detect. */ - OTP_Det(&g_mc.prot.otp, &g_mc.prot.motorErrStatus, OTP_IPM_ERR_BIT, g_mc.powerBoardTemp); - OTP_Exec(&g_mc.prot.otp, &g_mc.spdRefHz, g_apt); - OTP_Recy(&g_mc.prot.otp, &g_mc.prot.motorErrStatus, OTP_IPM_ERR_BIT, g_mc.powerBoardTemp); - - /* If protect level == 4, set motor state as stop. */ - if (g_mc.prot.ovp.protLevel == LEVEL_4 || g_mc.prot.lvp.protLevel == LEVEL_4 \ - || g_mc.prot.otp.protLevel == LEVEL_4) { - SysCmdStopSet(&g_mc.statusReg); - } -} - -/** - * @brief The carrier ISR wrapper function. - * @param aptHandle The APT handle. - * @retval None. - */ -void MotorCarrierProcessCallback(void *aptHandle) -{ - MCS_ASSERT_PARAM(aptHandle != NULL); - BASE_FUNC_UNUSED(aptHandle); - /* the carrierprocess of motor */ - MCS_CarrierProcess(&g_mc); - /* Over current protect */ - if (g_mc.stateMachine == FSM_RUN || g_mc.stateMachine == FSM_STARTUP) { - OCP_Det(&g_mc.prot.ocp, &g_mc.prot.motorErrStatus, g_mc.idqFbk); - OCP_Exec(&g_mc.prot.ocp, &g_mc.idqFbk, g_apt); /* Execute over current protect motion */ - if (g_mc.prot.ocp.protLevel < LEVEL_4) { - OCP_Recy(&g_mc.prot.ocp, &g_mc.prot.motorErrStatus); - } - } -} - -/** - * @brief Event interrupt callback function of APT module. - * @param para APT module handle. - * @retval None. - */ -void MotorSysErrCallback(void *para) -{ - MCS_ASSERT_PARAM(para != NULL); - APT_Handle *handle = (APT_Handle *)para; - /* The IPM overcurrent triggers and disables the three-phase PWM output. */ - MotorPwmOutputDisable(g_apt); - DCL_APT_ClearOutCtrlEventFlag((APT_RegStruct *)g_apt[PHASE_U], APT_OC_COMBINE_EVENT_A1); - DCL_APT_ClearOutCtrlEventFlag((APT_RegStruct *)g_apt[PHASE_V], APT_OC_COMBINE_EVENT_A1); - DCL_APT_ClearOutCtrlEventFlag((APT_RegStruct *)g_apt[PHASE_W], APT_OC_COMBINE_EVENT_A1); - /* Status setting error */ - SysErrorSet(&g_mc.statusReg); - DBG_PRINTF("APT error! \r\n"); - HAL_GPIO_SetValue(&LED2_HANDLE, LED2_PIN, GPIO_LOW_LEVEL); - BASE_FUNC_UNUSED(handle); -} - -/** - * @brief Init motor controller's data structure. - * @retval None. - */ -static void InitSoftware(void) -{ - /* Initializing motor control param */ - TSK_Init(); - /* Read phase-uvw current */ - g_mc.readCurrUvwCb = ReadCurrUvw; - g_mc.setPwmDutyCb = SetPwmDutyCp; - g_mc.setADCTriggerTimeCb = SetADCTriggerTime; -} - -/** - * @brief Config the KEY func. - * @param handle The GPIO handle. - * @retval None. - */ -static KEY_State Key_StateRead(GPIO_Handle *handle) -{ - if (HAL_GPIO_GetPinValue(handle, handle->pins) == 0) { - BASE_FUNC_DELAY_MS(30); /* delay 30ms for deshake */ - if (HAL_GPIO_GetPinValue(handle, handle->pins) == 0) { - while (HAL_GPIO_GetPinValue(handle, handle->pins) == 0) { - } - return KEY_DOWN; - } - } - return KEY_UP; -} - -/** - * @brief Control motor start and stop state by key func. - * @param param The GPIO handle. - * @retval None. - */ -void MotorStartStopKeyCallback(void *param) -{ - GPIO_Handle *handle = (GPIO_Handle *)param; - if (Key_StateRead(handle) == KEY_DOWN) { - if (g_mc.motorStateFlag == 0) { /* start motor */ - g_mc.motorStateFlag = 1; - SysCmdStartSet(&g_mc.statusReg); - } else if (g_mc.motorStateFlag == 1) { /* stop motor */ - g_mc.motorStateFlag = 0; - SysCmdStopSet(&g_mc.statusReg); - } - } -} - -/** - * @brief User application main entry function. - * @retval BSP_OK. - */ -int MotorMainProcess(void) -{ - unsigned int tickNum1Ms = 2; /* 1ms tick */ - static unsigned int tickCnt1Ms = 0; - unsigned int tickNum500Ms = 1000; /* 500ms tick */ - static unsigned int tickCnt500Ms = 0; - SystemInit(); - HMI_Init(); /* Init uart interrupt */ - HAL_TIMER_Start(&g_timer0); - HAL_TIMER_Start(&g_timer1); - - /* Disable PWM output before startup. */ - MotorPwmOutputDisable(g_apt); - /* Software initialization. */ - InitSoftware(); - /* Start the PWM clock. */ - HAL_APT_StartModule(RUN_APT0 | RUN_APT1 | RUN_APT2); - /* System Timer clock. */ - BASE_FUNC_DELAY_MS(ADC_READINIT_DELAY); - TrimInitAdcShiftValue(&g_mc); - BASE_FUNC_DELAY_MS(MOTOR_START_DELAY); - while (1) { - /* Cycling send data to host */ - HMI_Process_Tx(&g_mc); - if (g_mc.msTickCnt - tickCnt1Ms >= tickNum1Ms) { - tickCnt1Ms = g_mc.msTickCnt; - /* User Code 1ms Event */ - HMI_Process_Rx(&g_mc); - /* User Code 1ms Event */ - } - - if (g_mc.msTickCnt - tickCnt500Ms >= tickNum500Ms) { - if (SysIsError(&g_mc.statusReg) != true) { - /* LED toggle in normal status. */ - HAL_GPIO_TogglePin(&LED1_HANDLE, LED1_PIN); - } - tickCnt500Ms = g_mc.msTickCnt; - } - } - return 0; -} diff --git a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/cust_process.c b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/cust_process.c deleted file mode 100644 index 7c4f7525..00000000 --- a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/cust_process.c +++ /dev/null @@ -1,718 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file cust_process.c - * @author MCU Algorithm Team - * @brief This file provides functions declaration of cust process interface. - */ - -#include "cust_process.h" -#include "mcs_ctlmode_config.h" -#include "mcs_math_const.h" -#include "mcs_user_config.h" -#include "mcs_assert.h" -#include "main.h" -#include "uart_module.h" -#include "mcs_mtr_param.h" -/* Macro definitions --------------------------------------------------------------------------- */ -/* Constant value. */ -#define CONST_VALUE_60 60.0f /* Constant value 60. */ -#define CONST_VALUE_DIV_1000 0.001f /* Constant value 1/1000. */ - -/* Data array index. */ -#define DATA_SEGMENT_ONE 0 /* First element of the data segment */ -#define DATA_SEGMENT_TWO 1 /* Second element of the data segment */ -#define DATA_SEGMENT_THREE 2 /* Third element of the data segment */ -#define DATA_SEGMENT_FOUR 3 /* Fourth element of the data segment */ -#define CUSTACKCODELEN 10 /* Ack code length */ - -/* Command code. */ -#define SET_PID_KP 0x01 /* Set Pid Kp Command Params */ -#define SET_PID_KI 0x02 /* Set Pid Ki Command Params */ -#define SET_PID_LIMIT 0x03 /* Set Pid limit Command Params */ - -#define SET_SMO1TH_PLL_BDW 0x01 /* Set Smo1th Pll BandWidth Command Params */ -#define SET_SMO1TH_SPDFLITER_FC 0x02 /* Set Smo1th Fc Command Params */ -#define SET_SMO1TH_FILCOMPANGLE 0x03 /* Set Smo1th FillComp Command Params */ - -#define SET_SMO4TH_KD 0x01 /* Set Smo4th Kd Command Params */ -#define SET_SMO4TH_KP 0x02 /* Set Smo4th Kq Command Params */ - -#define SET_SPD_COMMAND_HZ 0x01 /* Set Target Speed Command Params */ -#define SET_SPD_RMG_SLOPE 0x02 /* Set Speed Slope Command Params */ - -#define SET_MAX_ELEC_SPD 0x01 /* Set Max Motor Speed Command Params */ -#define SET_MOTOR_NUM_OF_PAIRS 0x02 /* Set Motor Command Params */ - -#define SET_MOTOR_RES_OF_STATOR 0x01 /* Set Motor Res Command Params */ -#define SET_MOTOR_DAXIS_INDUCTANCE 0x02 /* Set Motor Daxis Inductance Command Params */ -#define SET_MOTOR_QAXIS_INDUCTANCE 0x03 /* Set Motor Qaxis Inductance Command Params */ - -#define SET_SVPWM_VOLTAGE_PER_UNIT 0x01 /* Set Svpwm Voltage Params */ -#define SET_ADC_CURRENT_SAMPLE_COFE 0x02 /* Set Adc coffe Params */ -#define SET_CURRENT_LOOP_CONTROL_PERIOD 0x03 /* Set Current loop period Command Params */ - -#define SET_IF_TARGET_CURRENT_VALUE 0x01 /* Set If Target Params */ -#define SET_INCREMENT_OF_IF_CURRENT 0x02 /* Set If Step Command Params */ -#define SET_SPEED_RING_BEGIN_SPEED 0x03 /* Set If to Smo Start Speed Command Params */ - -static unsigned char ackCode = 0; -static unsigned char g_uartTxBuf[CUSTACKCODELEN] = {0}; - -/** - * @brief Set observer type. - * @param mtrCtrl The motor control handle. - * @param rxData Receive buffer - */ -static void CMDCODE_SetObserverType(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) -{ - /* Get function code. */ - int funcCode = (int)(rxData->data[DATA_SEGMENT_ONE].typeF); - if (funcCode == FOC_OBSERVERTYPE_SMO1TH) { - ackCode = 0X01; - mtrCtrl->obserType = FOC_OBSERVERTYPE_SMO1TH; - CUST_AckCode(g_uartTxBuf, ackCode, mtrCtrl->obserType); - } else if (funcCode == FOC_OBSERVERTYPE_SMO4TH) { - ackCode = 0X02; - mtrCtrl->obserType = FOC_OBSERVERTYPE_SMO4TH; - CUST_AckCode(g_uartTxBuf, ackCode, mtrCtrl->obserType); - } -} - -/** - * @brief Set pid parameter ack code. - * @param funcCode Received data funccode. - */ -static unsigned char SetPidAckCode(int funcCode) -{ - switch (funcCode) { - /* Set current loop D-Axis PID parameter ack code. */ - case FOC_CURDAXISPID_PARAMS: - ackCode = 0xE0; - break; - /* Set current loop Q-Axis PID parameter ack code. */ - case FOC_CURQAXISPID_PARAMS: - ackCode = 0xE3; - break; - /* Set speed loop PID parameter ack code. */ - case FOC_SPDPID_PARAMS: - ackCode = 0xE6; - break; - default: - break; - } - return ackCode; -} - -/** - * @brief Set pid parameters. - * @param pidHandle The pid control handle. - * @param rxData Receive buffer - */ -static void SetPidParams(PID_Handle *pidHandle, CUSTDATATYPE_DEF *rxData) -{ - /* Get function code. */ - int funcCode = (int)(rxData->data[DATA_SEGMENT_ONE].typeF); - /* Get command code. */ - int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); - switch (cmdCode) { - case SET_PID_KP: /* Set the P parameter. */ - PID_SetKp(pidHandle, rxData->data[DATA_SEGMENT_THREE].typeF); - ackCode = SetPidAckCode(funcCode); - CUST_AckCode(g_uartTxBuf, (unsigned char)(ackCode + SET_PID_KP), rxData->data[DATA_SEGMENT_THREE].typeF); - break; - case SET_PID_KI: /* Set the I parameter. */ - PID_SetKi(pidHandle, rxData->data[DATA_SEGMENT_THREE].typeF); - ackCode = SetPidAckCode(funcCode); - CUST_AckCode(g_uartTxBuf, (unsigned char)(ackCode + SET_PID_KI), rxData->data[DATA_SEGMENT_THREE].typeF); - break; - case SET_PID_LIMIT: /* Set the pid limit. */ - PID_SetLimit(pidHandle, rxData->data[DATA_SEGMENT_THREE].typeF); - ackCode = SetPidAckCode(funcCode); - CUST_AckCode(g_uartTxBuf, (unsigned char)(ackCode + SET_PID_LIMIT), rxData->data[DATA_SEGMENT_THREE].typeF); - break; - default: - ackCode = 0X77; - CUST_AckCode(g_uartTxBuf, ackCode, 0); - break; - } -} - -/** - * @brief Set motor pid parameters. - * @param mtrCtrl The motor control handle. - * @param rxData Receive buffer - */ -static void CMDCODE_SetMotorPidParams(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) -{ - /* Get function code. */ - int funcCode = (int)(rxData->data[DATA_SEGMENT_ONE].typeF); - - if (funcCode == FOC_CURDAXISPID_PARAMS) { - SetPidParams(&mtrCtrl->currCtrl.dAxisPi, rxData); /* Set Curr loop Daxis pid params */ - } else if (funcCode == FOC_CURQAXISPID_PARAMS) { - SetPidParams(&mtrCtrl->currCtrl.qAxisPi, rxData); /* Set Curr loop Qaxis pid params */ - mtrCtrl->currCtrl.dAxisPi.upperLimit = mtrCtrl->currCtrl.qAxisPi.upperLimit; - mtrCtrl->currCtrl.dAxisPi.lowerLimit = mtrCtrl->currCtrl.qAxisPi.lowerLimit; - } else if (funcCode == FOC_SPDPID_PARAMS) { - SetPidParams(&mtrCtrl->spdCtrl.spdPi, rxData); /* Set Speed loop params */ - } -} - -/** - * @brief Set first order sliding mode observer parameters. - * @param smoHandle The observer control handle. - * @param rxData Receive buffer - */ -static void SetObserverSmo1thParams(FOSMO_Handle *smoHandle, CUSTDATATYPE_DEF *rxData) -{ - smoHandle->kSmo = rxData->data[DATA_SEGMENT_TWO].typeF; - ackCode = 0X09; - CUST_AckCode(g_uartTxBuf, ackCode, smoHandle->kSmo); -} - -/** - * @brief Set first order sliding mode observer's phase-locked loop parameters. - * @param smoHandle The observer control handle. - * @param rxData Receive buffer - */ -static void SetObserverSmo1thPLLParams(FOSMO_Handle *smoHandle, CUSTDATATYPE_DEF *rxData) -{ - /* Get command code. */ - int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); - float pllBdw = 0.0f; - switch (cmdCode) { - case SET_SMO1TH_PLL_BDW: /* Set the bandwidth. */ - pllBdw = rxData->data[DATA_SEGMENT_THREE].typeF; - smoHandle->pll.pi.kp = 2.0f * pllBdw; /* kp = 2.0f * pllBdw */ - smoHandle->pll.pi.ki = pllBdw * pllBdw; /* ki = pllBdw * pllBdw */ - ackCode = 0X0A; - CUST_AckCode(g_uartTxBuf, ackCode, pllBdw); - break; - case SET_SMO1TH_SPDFLITER_FC: /* Set the cutoff frequency. */ - smoHandle->spdFilter.fc = rxData->data[DATA_SEGMENT_THREE].typeF; - smoHandle->spdFilter.a1 = 1.0f / (1.0f + DOUBLE_PI * smoHandle->spdFilter.fc * CTRL_CURR_PERIOD); - smoHandle->spdFilter.b1 = 1.0f - smoHandle->spdFilter.a1; - ackCode = 0X0B; - CUST_AckCode(g_uartTxBuf, ackCode, smoHandle->spdFilter.fc); - break; - case SET_SMO1TH_FILCOMPANGLE: /* Set the compensation angle. */ - smoHandle->filCompAngle = rxData->data[DATA_SEGMENT_THREE].typeF; - ackCode = 0X0C; - CUST_AckCode(g_uartTxBuf, ackCode, smoHandle->filCompAngle); - break; - default: - ackCode = 0X77; - CUST_AckCode(g_uartTxBuf, ackCode, 0); - break; - } -} - -/** - * @brief Set fourth order sliding mode observer parameters. - * @param smo4thHandle The observer control handle. - * @param rxData Receive buffer - */ -static void SetObserverSmo4thParams(SMO4TH_Handle *smo4thHandle, CUSTDATATYPE_DEF *rxData) -{ - /* Get command code. */ - int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); - - switch (cmdCode) { - case SET_SMO4TH_KD: /* Set d axis gain. */ - smo4thHandle->kd = rxData->data[DATA_SEGMENT_THREE].typeF; - ackCode = 0X0D; - CUST_AckCode(g_uartTxBuf, ackCode, smo4thHandle->kd); - break; - case SET_SMO4TH_KP: /* Set q axis gain. */ - smo4thHandle->kq = rxData->data[DATA_SEGMENT_THREE].typeF; - ackCode = 0X0E; - CUST_AckCode(g_uartTxBuf, ackCode, smo4thHandle->kq); - break; - default: - break; - } -} - -/** - * @brief Set fourth order sliding mode observer's phase-locked loop parameters. - * @param smo4thHandle The observer control handle. - * @param rxData Receive buffer - */ -static void SetObserverSmo4thPLLParams(SMO4TH_Handle *smo4thHandle, CUSTDATATYPE_DEF *rxData) -{ - float pllBdw = rxData->data[DATA_SEGMENT_TWO].typeF; - smo4thHandle->pll.pi.kp = (2.0f) * pllBdw; /* kp = 2.0f * pllBdw */ - smo4thHandle->pll.pi.ki = pllBdw * pllBdw; - ackCode = 0X11; - CUST_AckCode(g_uartTxBuf, ackCode, pllBdw); -} - -/** - * @brief Set observer parameters. - * @param mtrCtrl The motor control handle. - * @param rxData Receive buffer - */ -static void CMDCODE_SetObserverParams(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) -{ - /* Get function code. */ - int funcCode = (int)(rxData->data[DATA_SEGMENT_ONE].typeF); - - if (funcCode == FOC_OBSERVERTYPE_SMO1TH) { - SetObserverSmo1thParams(&mtrCtrl->smo, rxData); - } else if (funcCode == FOC_OBSERVERTYPE_SMO1TH_PLL) { - SetObserverSmo1thPLLParams(&mtrCtrl->smo, rxData); - } else if (funcCode == FOC_OBSERVERTYPE_SMO4TH) { - SetObserverSmo4thParams(&mtrCtrl->smo4th, rxData); - } else if (funcCode == FOC_OBSERVERTYPE_SMO4TH_PLL) { - SetObserverSmo4thPLLParams(&mtrCtrl->smo4th, rxData); - } -} - -/** - * @brief Set motor speed and speed slope. - * @param mtrCtrl The motor control handle. - * @param rxData Receive buffer - */ -static void CMDCODE_SetMotorSpdAndSlope(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) -{ - /* Get command code. */ - int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); - - switch (cmdCode) { - case SET_SPD_COMMAND_HZ: /* Set target speed(hz). */ - mtrCtrl->spdCmdHz = rxData->data[DATA_SEGMENT_THREE].typeF * mtrCtrl->mtrParam.mtrNp / CONST_VALUE_60; - /* Judgement the value > 0.00001, make sure denominator != 0. */ - if (rxData->data[DATA_SEGMENT_FOUR].typeF > 0.00001f) { - mtrCtrl->spdRmg.delta = mtrCtrl->spdCmdHz / rxData->data[DATA_SEGMENT_FOUR].typeF * CTRL_SYSTICK_PERIOD; - } - ackCode = 0X16; - CUST_AckCode(g_uartTxBuf, ackCode, mtrCtrl->spdCmdHz); - break; - case SET_SPD_RMG_SLOPE: /* Set speed slope. */ - mtrCtrl->spdRmg.delta = mtrCtrl->spdCmdHz / rxData->data[DATA_SEGMENT_THREE].typeF * CTRL_SYSTICK_PERIOD; - ackCode = 0X17; - CUST_AckCode(g_uartTxBuf, ackCode, rxData->data[DATA_SEGMENT_THREE].typeF); - break; - default: - break; - } -} - -/** - * @brief Set motor base parameters. - * @param mtrParamHandle The motor parameters handle. - * @param rxData Receive buffer - */ -static void SetMotorBaseParams(MOTOR_Param *mtrParamHandle, CUSTDATATYPE_DEF *rxData) -{ - /* Get command code. */ - int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); - - switch (cmdCode) { - case SET_MAX_ELEC_SPD: /* Set max electric speed. */ - mtrParamHandle->maxElecSpd = rxData->data[DATA_SEGMENT_THREE].typeF / - CONST_VALUE_60 * mtrParamHandle->mtrNp; - ackCode = 0X18; - CUST_AckCode(g_uartTxBuf, ackCode, rxData->data[DATA_SEGMENT_THREE].typeF); - break; - case SET_MOTOR_NUM_OF_PAIRS: /* Set the number of motor pole pairs. */ - mtrParamHandle->mtrNp = (unsigned short)(rxData->data[DATA_SEGMENT_THREE].typeF); - ackCode = 0X19; - CUST_AckCode(g_uartTxBuf, ackCode, (float)mtrParamHandle->mtrNp); - break; - default: - break; - } -} - -/** - * @brief Set motor special parameters. - * @param mtrParamHandle The motor parameters handle. - * @param rxData Receive buffer - */ -static void SetMotorSpecialParams(MOTOR_Param *mtrParamHandle, CUSTDATATYPE_DEF *rxData) -{ - /* Get command code. */ - int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); - - switch (cmdCode) { - case SET_MOTOR_RES_OF_STATOR: /* Set the resistor of stator. */ - mtrParamHandle->mtrRs = rxData->data[DATA_SEGMENT_THREE].typeF; - ackCode = 0X1A; - CUST_AckCode(g_uartTxBuf, ackCode, mtrParamHandle->mtrRs); - break; - case SET_MOTOR_DAXIS_INDUCTANCE: /* Set the d axis inductance. */ - mtrParamHandle->mtrLd = rxData->data[DATA_SEGMENT_THREE].typeF; - ackCode = 0X1B; - CUST_AckCode(g_uartTxBuf, ackCode, mtrParamHandle->mtrLd); - break; - case SET_MOTOR_QAXIS_INDUCTANCE: /* Set the q axis inductance. */ - mtrParamHandle->mtrLq = rxData->data[DATA_SEGMENT_THREE].typeF; - ackCode = 0X1C; - CUST_AckCode(g_uartTxBuf, ackCode, mtrParamHandle->mtrLq); - break; - default: - ackCode = 0X77; - CUST_AckCode(g_uartTxBuf, ackCode, 0); - break; - } -} - -/** - * @brief Set motor board parameters. - * @param mtrCtrl The motor control handle. - * @param rxData Receive buffer - */ -static void SetMotorBoardParams(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) -{ - /* Get command code. */ - int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); - - switch (cmdCode) { - case SET_SVPWM_VOLTAGE_PER_UNIT: /* Set svpwm voltage per unit. */ - mtrCtrl->sv.voltPu = rxData->data[DATA_SEGMENT_THREE].typeF * ONE_DIV_SQRT3; - mtrCtrl->currCtrl.outLimit = mtrCtrl->sv.voltPu * ONE_DIV_SQRT3; - ackCode = 0X1D; - CUST_AckCode(g_uartTxBuf, ackCode, rxData->data[DATA_SEGMENT_THREE].typeF); - break; - case SET_ADC_CURRENT_SAMPLE_COFE: /* Set adc current sample cofeature. */ - mtrCtrl->adcCurrCofe = rxData->data[DATA_SEGMENT_THREE].typeF; - ackCode = 0X1E; - CUST_AckCode(g_uartTxBuf, ackCode, mtrCtrl->adcCurrCofe); - break; - case SET_CURRENT_LOOP_CONTROL_PERIOD: /* Set current loop control period. */ - mtrCtrl->currCtrlPeriod = 1 / rxData->data[DATA_SEGMENT_THREE].typeF * CONST_VALUE_DIV_1000; - ackCode = 0X1F; - CUST_AckCode(g_uartTxBuf, ackCode, rxData->data[DATA_SEGMENT_THREE].typeF); - break; - default: - break; - } -} - -/** - * @brief Set motor parameters. - * @param mtrCtrl The motor control handle. - * @param rxData Receive buffer - */ -static void CMDCODE_SetMotorParams(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) -{ - /* Get function code. */ - int funcCode = (int)(rxData->data[DATA_SEGMENT_ONE].typeF); - - if (funcCode == MOTOR_PARAMS_BASE) { - SetMotorBaseParams(&mtrCtrl->mtrParam, rxData); - } else if (funcCode == MOTOR_PARAMS_SPECIAL) { - SetMotorSpecialParams(&mtrCtrl->mtrParam, rxData); - } else if (funcCode == MOTOR_PARAMS_BOARD) { - SetMotorBoardParams(mtrCtrl, rxData); - } -} - -/** - * @brief Motor start. - * @param mtrCtrl The motor control handle. - */ -static void CMDCODE_MotorStart(MTRCTRL_Handle *mtrCtrl) -{ - if (mtrCtrl->stateMachine != FSM_RUN) { - SysCmdStartSet(&mtrCtrl->statusReg); /* start motor. */ - mtrCtrl->motorStateFlag = 1; - ackCode = 0X24; /* send ackcode to host. */ - CUST_AckCode(g_uartTxBuf, ackCode, 1); - } -} - -/** - * @brief Motor stop. - * @param mtrCtrl The motor control handle. - */ -static void CMDCODE_MotorStop(MTRCTRL_Handle *mtrCtrl) -{ - SysCmdStopSet(&mtrCtrl->statusReg); - mtrCtrl->motorStateFlag = 0; - ackCode = 0X25; - CUST_AckCode(g_uartTxBuf, ackCode, 0); -} - -/** - * @brief Motor state reset. - * @param mtrCtrl The motor control handle. - */ -static void CMDCODE_MotorReset(MTRCTRL_Handle *mtrCtrl) -{ - BASE_FUNC_UNUSED(mtrCtrl); - BASE_FUNC_SoftReset(); -} - -/** - * @brief Set IF-Startup parameters. - * @param mtrCtrl The motor control handle. - * @param rxData Receive buffer - */ -static void SetStartupIFParams(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) -{ - /* Get command code. */ - int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); - - switch (cmdCode) { - case SET_IF_TARGET_CURRENT_VALUE: /* Set I/F start up target current value. */ - mtrCtrl->ifCtrl.targetAmp = rxData->data[DATA_SEGMENT_THREE].typeF; - ackCode = 0X26; - CUST_AckCode(g_uartTxBuf, ackCode, mtrCtrl->ifCtrl.targetAmp); - break; - case SET_INCREMENT_OF_IF_CURRENT: /* Set increment of I/F start up current. */ - mtrCtrl->ifCtrl.stepAmp = mtrCtrl->ifCtrl.targetAmp / rxData->data[DATA_SEGMENT_THREE].typeF * - CTRL_SYSTICK_PERIOD; - ackCode = 0X27; - CUST_AckCode(g_uartTxBuf, ackCode, mtrCtrl->ifCtrl.stepAmp); - break; - case SET_SPEED_RING_BEGIN_SPEED: /* Set speed ring begin speed. */ - mtrCtrl->startup.spdBegin = rxData->data[DATA_SEGMENT_THREE].typeF / - CONST_VALUE_60 * mtrCtrl->mtrParam.mtrNp; - ackCode = 0X28; - CUST_AckCode(g_uartTxBuf, ackCode, rxData->data[DATA_SEGMENT_THREE].typeF); - break; - default: - break; - } -} - -/** - * @brief Set start up parameters. - * @param mtrCtrl The motor control handle. - * @param rxData Receive buffer - */ -static void CMDCODE_SetStartupParams(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) -{ - /* Get function code. */ - int funcCode = (int)(rxData->data[DATA_SEGMENT_ONE].typeF); - - if (funcCode == FOC_STARTUP_IF) { - SetStartupIFParams(mtrCtrl, rxData); - } -} - -/** - * @brief Set adjust speed mode. - * @param mtrCtrl The motor control handle. - * @param rxData Receive buffer - */ -static void CMDCODE_SetAdjustSpdMode(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) -{ - /* Get function code. */ - int funcCode = (int)(rxData->data[DATA_SEGMENT_ONE].typeF); - /* Get commond code. */ - int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); - if (funcCode == HOST_SPEED_ADJUST) { - mtrCtrl->spdAdjustMode = HOST_SPEED_ADJUST; - /* Uart connect success. */ - mtrCtrl->uartConnectFlag = CONNECTING; - ackCode = 0X2A; - CUST_AckCode(g_uartTxBuf, ackCode, (float)mtrCtrl->spdAdjustMode); - } else if (funcCode == CUST_SPEED_ADJUST) { - if (cmdCode == 1) { /* If uart connection disconnected & stop motor commond. */ - SysCmdStopSet(&mtrCtrl->statusReg); - mtrCtrl->motorStateFlag = 0; - } - mtrCtrl->spdAdjustMode = CUST_SPEED_ADJUST; - ackCode = 0X2B; - CUST_AckCode(g_uartTxBuf, ackCode, (float)mtrCtrl->spdAdjustMode); - /* Uart disconnect. */ - mtrCtrl->uartConnectFlag = DISCONNECT; - } -} - -/** - * @brief Check uart connect. - * @param mtrCtrl The motor control handle. - * @param rxData Receive buffer - */ -static void CMDCODE_UartHandShake(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) -{ - CMDCODE_SetAdjustSpdMode(mtrCtrl, rxData); -} - -/** - * @brief Set Motor Initial Status Parameters. - * @param mtrCtrl The motor control handle. - * @param rxData Receive buffer - * @param code Instruction code. - */ -static void CMDCODE_EXE_SetMotorInitParams(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData, unsigned char code) -{ - switch (code) { - case CMDCODE_SET_OBSERVER_TYPE: { /* Set observer type. */ - CMDCODE_SetObserverType(mtrCtrl, rxData); - } - break; - case CMDCODE_SET_PID_PARAMS: { /* Set motor pid parameters. */ - CMDCODE_SetMotorPidParams(mtrCtrl, rxData); - } - break; - case CMDCODE_SET_OBSERVER_PARAMS: { /* Set observer parameters. */ - CMDCODE_SetObserverParams(mtrCtrl, rxData); - } - break; - case CMDCODE_SET_MOTOR_TARGETSPD: { /* Set motor speed and speed slope. */ - CMDCODE_SetMotorSpdAndSlope(mtrCtrl, rxData); - } - break; - case CMDCODE_SET_MOTOR_PARAMS: { /* Set motor parameters. */ - CMDCODE_SetMotorParams(mtrCtrl, rxData); - } - break; - case CMDCODE_SET_ADJUSTSPD_MODE: /* Set adjust speed mode. */ - CMDCODE_SetAdjustSpdMode(mtrCtrl, rxData); - break; - default: - break; - } -} - -/** - * @brief Set Motor Control System Status. - * @param mtrCtrl The motor control handle. - * @param rxData Receive buffer - * @param code Instruction code. - */ -static void CMDCODE_EXE_SetMotorState(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData, unsigned char code) -{ - BASE_FUNC_UNUSED(rxData); - switch (code) { - case CMDCODE_MOTOR_START: { /* Motor start command */ - CMDCODE_MotorStart(mtrCtrl); - } - break; - case CMDCODE_MOTOR_STOP: { /* Motor stop command */ - CMDCODE_MotorStop(mtrCtrl); - } - break; - case CMDCODE_MOTORSTATE_RESET: { /* Motor reset command */ - CMDCODE_MotorReset(mtrCtrl); - } - break; - default: - break; - } -} - -/** - * @brief Set Startup and Uart Link Handshake Flag Parameters. - * @param mtrCtrl The motor control handle. - * @param rxData Receive buffer - * @param code Instruction code. - */ -static void CMDCODE_EXE_SetOtherParams(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData, unsigned char code) -{ - switch (code) { - case CMDCODE_SET_STARTUP_PARAMS: /* Set start up parameters. */ - CMDCODE_SetStartupParams(mtrCtrl, rxData); - break; - case CMDCODE_UART_HANDSHAKE: { /* Check uart hand shake. */ - CMDCODE_UartHandShake(mtrCtrl, rxData); - } - break; - case CMDCODE_UART_HEARTDETECT: { /* Check uart hand shake. */ - mtrCtrl->uartHeartDetCnt++; - } - break; - default: - break; - } -} -/** - * @brief Instruction code executor. - * @param mtrCtrl The motor control handle. - * @param rxData Receive buffer - * @param code Instruction code. - */ -static void CMDCODE_EXE_Process(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData, unsigned char code) -{ - CMDCODE_EXE_SetMotorInitParams(mtrCtrl, rxData, code); - CMDCODE_EXE_SetMotorState(mtrCtrl, rxData, code); - CMDCODE_EXE_SetOtherParams(mtrCtrl, rxData, code); -} - -/** - * @brief Host data download callback and data parsing. - * @param mtrCtrl The motor control handle. - * @param rxBuf Receive buffer - */ -void CUST_UartDataProcess(MTRCTRL_Handle *mtrCtrl, unsigned char *rxBuf) -{ - /* Verify Parameters */ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - MCS_ASSERT_PARAM(rxBuf != NULL); - - /* Uart data storage struct */ - CUSTDATATYPE_DEF data; - volatile unsigned char *ptrCnt = &rxBuf[FRAME_CHECK_BEGIN + 1]; - volatile unsigned char *strCnt = &data.data[0].typeCh[0]; - for (unsigned int j = 0; j < FRAME_RECV_DATA_LENTH * FRAME_ONE_DATA_LENTH; j++) { - *strCnt++ = *ptrCnt++; - } - /* Message function code. */ - data.code = rxBuf[FRAME_CHECK_BEGIN]; - CMDCODE_EXE_Process(mtrCtrl, &data, data.code); -} - -/** - * @brief The host computer displays data transmission. - * @param mtrCtrl The motor control handle. - * @param txData Message content. - * @param stage Message status function code. - */ -void CUST_SetTxMsg(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *txData) -{ - /* Verify Parameters */ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - MCS_ASSERT_PARAM(txData != NULL); - if (mtrCtrl->stateMachine == FSM_IDLE) { - mtrCtrl->smo.spdEst = 0.0f; - } - /* Data send to host. */ - txData->data[CURRDQ_Q].typeF = mtrCtrl->idqFbk.q; - txData->data[CURRDQ_D].typeF = mtrCtrl->idqFbk.d; - txData->data[CURRREFDQ_Q].typeF = mtrCtrl->idqRef.q; - txData->data[CURRREFDQ_D].typeF = mtrCtrl->idqRef.d; - /* Motor current speed. */ - txData->data[CURRSPD].typeF = mtrCtrl->smo.spdEst * CONST_VALUE_60 / mtrCtrl->mtrParam.mtrNp; - /* Motor commond speed. */ - txData->data[SPDCMDHZ].typeF = mtrCtrl->spdCmdHz * CONST_VALUE_60 / mtrCtrl->mtrParam.mtrNp; - /* Bus voltage. */ - txData->data[UDC].typeF = mtrCtrl->udc; - /* Power board temprature. */ - txData->data[POWERBOARDTEMP].typeF = mtrCtrl->powerBoardTemp; - /* Motor protection status flag. */ - txData->data[CUST_ERR_CODE].typeI = mtrCtrl->prot.motorErrStatus.all; - /* Three phase current. */ - txData->data[CURRUVW_U].typeF = mtrCtrl->currUvw.u; - txData->data[CURRUVW_V].typeF = mtrCtrl->currUvw.v; - txData->data[CURRUVW_W].typeF = mtrCtrl->currUvw.w; - /* Three phase pwm duty. */ - txData->data[PWMDUTYUVW_U].typeF = mtrCtrl->dutyUvw.u; - txData->data[PWMDUTYUVW_V].typeF = mtrCtrl->dutyUvw.v; - txData->data[PWMDUTYUVW_W].typeF = mtrCtrl->dutyUvw.w; - /* Motor electric angle. */ - txData->data[AXISANGLE].typeF = mtrCtrl->axisAngle; - txData->data[VDQ_Q].typeF = mtrCtrl->vdqRef.q; - txData->data[VDQ_D].typeF = mtrCtrl->vdqRef.d; - txData->data[SPDREFHZ].typeF = mtrCtrl->spdRefHz * CONST_VALUE_60 / mtrCtrl->mtrParam.mtrNp; - txData->data[SENDTIMESTAMP].typeF = mtrCtrl->uartTimeStamp; -} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/cust_process.h b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/cust_process.h deleted file mode 100644 index 60fe69bb..00000000 --- a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/cust_process.h +++ /dev/null @@ -1,36 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file cust_process.h - * @author MCU Algorithm Team - * @brief This file provides functions declaration of system status. - */ - -/* Define to prevent recursive inclusion ------------------------------------------------------- */ -#ifndef McsMagicTag_HMI_MOUDLE_H -#define McsMagicTag_HMI_MOUDLE_H - -/* Includes ------------------------------------------------------------------------------------ */ -#include "protocol.h" -/* Typedef definitions ------------------------------------------------------------------------- */ - - -void CUST_UartDataProcess(MTRCTRL_Handle *mtrCtrl, unsigned char *rxBuf); - -void CUST_SetTxMsg(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *txData); - -#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/hmi_module.c b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/hmi_module.c deleted file mode 100644 index 58bf15a7..00000000 --- a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/hmi_module.c +++ /dev/null @@ -1,57 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file hmi_module.c - * @author MCU Algorithm Team - * @brief This file provides functions declaration of human-machine interface. - */ - -#include "hmi_module.h" -#include "mcs_assert.h" - -/** - * @brief HMI Initializatio. - * @retval None. - */ -void HMI_Init(void) -{ - UartRecvInit(); -} - -/** - * @brief HMI processing. - * @param mtrCtrl The motor control handle.. - * @retval None. - */ -void HMI_Process_Rx(MTRCTRL_Handle *mtrCtrl) -{ - /* Verify Parameters */ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - UartModuleProcess_Rx(mtrCtrl); -} - -/** - * @brief HMI processing. - * @param mtrCtrl The motor control handle.. - * @retval None. - */ -void HMI_Process_Tx(MTRCTRL_Handle *mtrCtrl) -{ - /* Verify Parameters */ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - UartModuleProcess_Tx(mtrCtrl); -} diff --git a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/hmi_module.h b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/hmi_module.h deleted file mode 100644 index 056f8d73..00000000 --- a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/hmi_module.h +++ /dev/null @@ -1,37 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file hmi_module.h - * @author MCU Algorithm Team - * @brief This file provides functions declaration of system status. - */ - -/* Define to prevent recursive inclusion ------------------------------------------------------- */ -#ifndef McsMagicTag_HMI_MOUDLE_H -#define McsMagicTag_HMI_MOUDLE_H - -/* Includes ------------------------------------------------------------------------------------ */ -#include "uart_module.h" -/* Typedef definitions ------------------------------------------------------------------------- */ - - -void HMI_Init(void); - -void HMI_Process_Rx(MTRCTRL_Handle *mtrCtrl); -void HMI_Process_Tx(MTRCTRL_Handle *mtrCtrl); - -#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/protocol.c b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/protocol.c deleted file mode 100644 index 56f0c0bb..00000000 --- a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/protocol.c +++ /dev/null @@ -1,174 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file protocol.c - * @author MCU Algorithm Team - * @brief This file provides functions declaration of port communication. - */ - -#include "protocol.h" -#include "apt.h" -#include "typedefs.h" -#include "main.h" -#include "mcs_assert.h" -#include "cust_process.h" - -/** - * @brief Callback function for receiving data analysis and processing. - * @param rxBuf Receive buffer. - */ -__weak void CUST_UartDataProcess(MTRCTRL_Handle *mtrCtrl, unsigned char *rxBuf) -{ - BASE_FUNC_UNUSED(mtrCtrl); - BASE_FUNC_UNUSED(rxBuf); -} -/** - * @brief User-defined protocol message sending function (weak function). - * @param rxData Sending Messages.. - */ -__weak void CUST_SetTxMsg(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *txData) -{ - BASE_FUNC_UNUSED(mtrCtrl); - BASE_FUNC_UNUSED(*txData); -} - -static void (*g_ptrDataProcess)(MTRCTRL_Handle *mtrCtrl, unsigned char *rxBuf) = CUST_UartDataProcess; - -/** - * @brief Frame checksum. - * @param ptr Pointer to the data to be checked - * @param num Number of bytes - * @retval unsigned char Checksum - */ -static unsigned char CheckSum(unsigned char *ptr, unsigned char num) -{ - unsigned char sum = 0; - unsigned char *p = ptr; - /* Calculate the sum of received data. */ - for (unsigned char i = 0; i < num; i++) { - sum += (unsigned char)*p; - p++; - } - return sum; -} - -/** - * @brief Transmitting Data Frames. - * @param mtrCtrl The motor control handle. - * @param txBuf Sending Messages. - */ -unsigned int CUST_TransmitData(MTRCTRL_Handle *mtrCtrl, unsigned char *txBuf) -{ - /* Verify Parameters */ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - MCS_ASSERT_PARAM(txBuf != NULL); - unsigned int dataLen = FRAME_ONE_DATA_LENTH * SEND_FRAME_DATA_NUM; - unsigned char i = 0; - CUSTDATATYPE_DEF txData = {0}; - CUST_SetTxMsg(mtrCtrl, &txData); - txBuf[i++] = FRAME_START; - /* Message function code */ - txBuf[i++] = FRAME_SENT; - /* Message data */ - for (unsigned char x = 0; x < SEND_FRAME_DATA_NUM; x++) { - unsigned char floatIndex = 0; - unsigned char byteOffset = i; - txBuf[x * FRAME_ONE_DATA_LENTH + byteOffset++] = txData.data[x].typeCh[floatIndex++]; - txBuf[x * FRAME_ONE_DATA_LENTH + byteOffset++] = txData.data[x].typeCh[floatIndex++]; - txBuf[x * FRAME_ONE_DATA_LENTH + byteOffset++] = txData.data[x].typeCh[floatIndex++]; - txBuf[x * FRAME_ONE_DATA_LENTH + byteOffset++] = txData.data[x].typeCh[floatIndex++]; - } - /* Message verification domain */ - txBuf[dataLen + i++] = CheckSum((unsigned char*)&txBuf[FRAME_CHECK_BEGIN], dataLen + 1); - /* End of Message */ - txBuf[dataLen + i++] = FRAME_END; - return dataLen + i; -} - -/** - * @brief Transmitting Data Frames. - * @param txBuf Sending Cust Ack Code. - * @param ackCode Ack Code. - * @param varParams Host set parameter. - */ -void CUST_AckCode(unsigned char *txBuf, unsigned char ackCode, float varParams) -{ - /* Verify Parameters */ - MCS_ASSERT_PARAM(txBuf != NULL); - CUSTDATATYPE_DEF txData = {0}; - int dataIndex = 0; - unsigned int i = 0; - unsigned int txLen = 0; - unsigned char dataLen = FRAME_ONE_CHAR_LENTH + FRAME_ONE_DATA_LENTH; - - txData.data[0].typeF = varParams; - txBuf[i++] = FRAME_START; - /* Message function code */ - txBuf[i++] = FRAME_CUSTACK; - /* Message ack code */ - txBuf[i++] = ackCode; - /* Message data */ - txBuf[i++] = txData.data[0].typeCh[dataIndex++]; - txBuf[i++] = txData.data[0].typeCh[dataIndex++]; - txBuf[i++] = txData.data[0].typeCh[dataIndex++]; - txBuf[i++] = txData.data[0].typeCh[dataIndex++]; - - /* Message verification domain */ - txBuf[FRAME_ONE_CHAR_LENTH + i++] = CheckSum((unsigned char*)&txBuf[FRAME_CHECK_BEGIN], dataLen + 1); - /* End of Message */ - txBuf[FRAME_ONE_CHAR_LENTH + i++] = FRAME_END; - txLen = FRAME_ONE_CHAR_LENTH + i++; - HAL_UART_WriteDMA(&g_uart0, txBuf, txLen); -} - -/** - * @brief Cust receive data process. - * @param mtrCtrl The motor control handle. - * @param rxBuf Receive buffer - */ -void CUST_DataReceProcss(MTRCTRL_Handle *mtrCtrl, unsigned char *rxBuf) -{ - /* Verify Parameters */ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - MCS_ASSERT_PARAM(rxBuf != NULL); - unsigned char g_uartTxBuf[10] = {0}; - unsigned char ackCode = 0; - /* Frame header check */ - if (rxBuf[0] != FRAME_START) { - ackCode = 0X78; - CUST_AckCode(g_uartTxBuf, ackCode, 0); - return; - } - /* Frame trailer check */ - if (rxBuf[FRAME_LENTH - 1] != FRAME_END) { - ackCode = 0X79; - CUST_AckCode(g_uartTxBuf, ackCode, 0); - return; - } - /* Checksum */ - if (CheckSum((unsigned char*)&rxBuf[FRAME_CHECK_BEGIN], FRAME_CHECK_NUM) != rxBuf[FRAME_CHECKSUM]) { - ackCode = 0X7A; - CUST_AckCode(g_uartTxBuf, ackCode, 0); - return; - } else { - if (g_ptrDataProcess == NULL) { - return; - } else { - g_ptrDataProcess(mtrCtrl, rxBuf); - } - } -} diff --git a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/protocol.h b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/protocol.h deleted file mode 100644 index 900a00d8..00000000 --- a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/protocol.h +++ /dev/null @@ -1,121 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file protocol.h - * @author MCU Algorithm Team - * @brief This file provides functions declaration of port communication. - */ -#ifndef McsMagicTag_PORTOCOL_H -#define McsMagicTag_PORTOCOL_H - -#include "uart.h" -#include "mcs_carrier.h" - -#define RS485_SEND_ENABLE GPIO6->GPIO_DATA[GPIO_PIN_7].reg = GPIO_PIN_7 -#define RS485_SEND_DISABLE GPIO6->GPIO_DATA[GPIO_PIN_7].reg = BASE_CFG_UNSET - -#define RX_BUF_LEN (16) -#define SEND_FRAME_DATA_NUM (CUSTDATA_MAX) -/* Service Uart0 Communication Deal */ -#define FRAME_ONE_DATA_LENTH 4 -#define FRAME_ONE_CHAR_LENTH 1 -#define FRAME_RECV_DATA_LENTH 4 -#define FRAME_LENTH 20 /* Data length */ -#define FRAME_SENT 0X8F -#define FRAME_CUSTACK 0X8A -#define FRAME_START 0x0F /* Start frame */ -#define FRAME_END '/' /* StOP frame */ -#define FRAME_CHECK_BEGIN 1 /* Check frame */ -#define FRAME_CHECKSUM 18 /* Check sum */ -#define FRAME_CHECK_NUM 17 -#define CMDCODE_IDLE_FRAME 0x55 /* Fill frame */ -#define CMDCODE_GET_MOTOR_PARAMS 0x01 -#define CMDCODE_SEND_MOTOR_PARAMS 0x02 -#define CMDCODE_SET_MOTOR_CTLMODE 0x03 -#define CMDCODE_SET_OBSERVER_TYPE 0x04 -#define CMDCODE_SET_STARTUP_MODE 0x05 -#define CMDCODE_SET_PID_PARAMS 0x06 -#define CMDCODE_SET_STARTUP_PARAMS 0x07 -#define CMDCODE_SET_OBSERVER_PARAMS 0x08 -#define CMDCODE_SET_MOTOR_TARGETSPD 0x09 -#define CMDCODE_SET_MOTOR_PARAMS 0x0A -#define CMDCODE_MOTOR_START 0x0B -#define CMDCODE_MOTOR_STOP 0x0C -#define CMDCODE_MOTORSTATE_RESET 0x0D -#define CMDCODE_SEND_FIRMVERSION 0x0E -#define CMDCODE_SET_ADJUSTSPD_MODE 0x11 -#define CMDCODE_UART_HANDSHAKE 0x12 -#define CMDCODE_UART_HEARTDETECT 0x13 - -typedef union { - unsigned char typeCh[4]; - float typeF; - int typeI; -} UNIONDATATYPE_DEF; - -typedef enum { - OFFLINE_RES = 0, - OFFLINE_LD, - OFFLINE_LQ, - OFFLINE_PSIF, - OFFLINE_JS, - OFFLINE_NP, - OFFLINE_B, - OFFLINE_KPD, - OFFLINE_KID, - OFFLINE_KPQ, - OFFLINE_KIQ, - OFFLINE_KPS, - OFFLINE_KIS, - OFFLINE_SPEED, - OFLINE_MAX -} OFFLINE_IDEN_TYPE; - -typedef enum { - CURRDQ_Q = 0, - CURRDQ_D, - CURRREFDQ_Q, - CURRREFDQ_D, - CURRSPD, - SPDCMDHZ, - UDC, - POWERBOARDTEMP, - CUST_ERR_CODE, - CURRUVW_U, - CURRUVW_V, - CURRUVW_W, - PWMDUTYUVW_U, - PWMDUTYUVW_V, - PWMDUTYUVW_W, - AXISANGLE, - VDQ_Q, - VDQ_D, - SPDREFHZ, - SENDTIMESTAMP, - CUSTDATA_MAX -} SENDTOHOSTPARAMS; - -typedef struct { - volatile unsigned char code; - volatile UNIONDATATYPE_DEF data[SEND_FRAME_DATA_NUM]; -} CUSTDATATYPE_DEF; - - -void CUST_DataReceProcss(MTRCTRL_Handle *mtrCtrl, unsigned char *rxBuf); -unsigned int CUST_TransmitData(MTRCTRL_Handle *mtrCtrl, unsigned char *txBuf); -void CUST_AckCode(unsigned char *txBuf, unsigned char ackCode, float varParams); -#endif /* McsMagicTag_PORTOCOL_H */ diff --git a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/uart_module.c b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/uart_module.c deleted file mode 100644 index df5e9186..00000000 --- a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/uart_module.c +++ /dev/null @@ -1,184 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the - * following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file uart_module.c - * @author MCU Algorithm Team - * @brief This file provides functions declaration of Serial port communication. - */ -#include "uart_module.h" -#include "debug.h" -#include "main.h" -#include "baseinc.h" - -/* Buffer size */ -#define UI_TX_BUF_LEN (96) -#define UI_RX_BUF_LEN (96) - -/* Receiving Timeout Interval */ -#define UART_TIME_OUT_MS (100) - -/* Start sending data to host delay after uart connect success */ -#define UART_UPDATA_DELAY_TIME_MS (50) - -/* Data buffer */ -unsigned char g_uartRxBuf[UI_RX_BUF_LEN] = {0}; -unsigned char g_uartTxBuf[UI_TX_BUF_LEN] = {0}; -static unsigned int getdeltaSystickCnt = 0; -static FRAME_Handle g_uartFrame; -/** - * @brief Receive Data Clear. - * @param uartFrame Receice Data. - * @retval None. - */ -static void FrameRecvClear(FRAME_Handle *uartFrame) -{ - /* Clear buffer lenth. */ - uartFrame->buffLen = 0; - uartFrame->timeOutCnt = 0; - uartFrame->frameFlag = 0; - /* Clear received data lenth. */ - uartFrame->rxLen = 0; - /* Clear received flag. */ - uartFrame->rxFlag = 0; - uartFrame->upDataCnt = 0; - uartFrame->rxAckFlag = 0; -} - -/** - * @brief Set Dma status. - * @param mtrCtrl The motor control handle. - * @retval None. - */ -static void SetUartDmaStatus(MTRCTRL_Handle *mtrCtrl) -{ - /* Delay 50ms start uart Tx DMA . */ - if (mtrCtrl->uartConnectFlag == CONNECTING && g_uartFrame.upDataDelayCnt++ > UART_UPDATA_DELAY_TIME_MS) { - g_uartFrame.txFlag = 1; /* Start send data flag. */ - mtrCtrl->uartConnectFlag = CONNECTED; - g_uartFrame.upDataDelayCnt = 0; - } - if (mtrCtrl->uartConnectFlag == DISCONNECT) { - g_uartFrame.txFlag = 0; /* Stop send data flag. */ - mtrCtrl->uartTimeStamp = 0; - } -} - -/** - * @brief Uart Dma interupt callback func. - * @param handle Uart handle. - * @retval None. - */ -void UART0_TXDMACallback(void *handle) -{ - BASE_FUNC_UNUSED(handle); - unsigned int getCurSystickCnt = 0; - static unsigned int getlastSystickCnt = 0; - /* USER CODE BEGIN UART0_WRITE_DMA_FINISH */ - g_uartFrame.txFlag = 1; - /* Received information answered, information update reserved. */ - if (g_uartFrame.rxAckFlag == 1) { - g_uartFrame.uartItTxFlag = 1; - g_uartFrame.rxAckFlag = 0; - } - getCurSystickCnt = DCL_SYSTICK_GetTick(); - if (getlastSystickCnt != 0) { - /* Calculate unit frame data send time */ - getdeltaSystickCnt = getCurSystickCnt - getlastSystickCnt; - } - getlastSystickCnt = getCurSystickCnt; - /* USER CODE END UART0_WRITE_DMA_FINISH */ -} - -/** - * @brief Uart0 Interruput Read CallBack Function. - * @param handle Uart handle. - * @retval None. - */ -void UART0ReadInterruptCallback(void *handle) -{ - BASE_FUNC_UNUSED(handle); - /* USER CODE BEGIN UART0_READ_IT_FINISH */ - if (g_uartFrame.rxLen >= UI_RX_BUF_LEN - 1) { - g_uartFrame.rxLen = 0; - } - HAL_UART_ReadIT(handle, &g_uartFrame.rxData, 1); - g_uartRxBuf[g_uartFrame.rxLen] = g_uartFrame.rxData; - g_uartFrame.rxLen++; - g_uartFrame.rxFlag = 1; - g_uartFrame.uartItTxFlag = 0; - return; - /* USER CODE END UART0_READ_IT_FINISH */ -} - -/** - * @brief Uart Read Data Init Function. - * @param void. - * @retval None. - */ -void UartRecvInit(void) -{ - /* Uart reception initialization */ - FrameRecvClear(&g_uartFrame); - HAL_UART_ReadIT(&g_uart0, &g_uartFrame.rxData, 1); -} - -/** - * @brief Uart Read Data Process Function. - * @param mtrCtrl The motor control handle. - * @retval None. - */ -void UartModuleProcess_Rx(MTRCTRL_Handle *mtrCtrl) -{ - /* Verify Parameters */ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - SetUartDmaStatus(mtrCtrl); - if (g_uartFrame.rxFlag == 1) { /* Receive data flag. */ - if (g_uartFrame.timeOutCnt++ > UART_TIME_OUT_MS) { - /* Received data from the host. */ - g_uartFrame.frameFlag = 1; - g_uartFrame.rxFlag = 0; - g_uartFrame.timeOutCnt = 0; - /* Execute data process. */ - CUST_DataReceProcss(mtrCtrl, g_uartRxBuf); - g_uartFrame.rxAckFlag = 1; - g_uartFrame.rxLen = 0; - } - } - g_uartFrame.frameFlag = 0; -} - -/** - * @brief Uart Write Data Process Function. - * @param mtrCtrl The motor control handle. - * @retval None. - */ -void UartModuleProcess_Tx(MTRCTRL_Handle *mtrCtrl) -{ - /* Verify Parameters */ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - if (g_uartFrame.txFlag == 1) { /* Send data flag. */ - mtrCtrl->uartTimeStamp = (float)getdeltaSystickCnt; /* Unit data time stamp */ - g_uartFrame.upDataCnt = 0; - g_uartFrame.txFlag = 0; - /* Send data to host. */ - unsigned int txLen = CUST_TransmitData(mtrCtrl, g_uartTxBuf); - /* If txIT mode send data finish, convert to DMA mode */ - if (g_uartFrame.uartItTxFlag == 1) { - HAL_UART_WriteDMA(&g_uart0, g_uartTxBuf, txLen); - } - } -} diff --git a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/uart_module.h b/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/uart_module.h deleted file mode 100644 index f1f8f899..00000000 --- a/vendor/yibaina_3061M/demo/E7050_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/uart_module.h +++ /dev/null @@ -1,47 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file uart_module.h - * @author MCU Algorithm Team - * @brief This file provides functions declaration of Serial port communication. - */ -#ifndef McsMagicTag_UART_MODULE_H -#define McsMagicTag_UART_MODULE_H - -#include "protocol.h" -#include "mcs_ctlmode_config.h" - -typedef struct { - unsigned int buffLen; - unsigned int timeOutCnt; - unsigned char frameFlag; - unsigned int rxLen; - unsigned char rxFlag; - unsigned char txFlag; - unsigned char rxData; - unsigned int upDataCnt; - unsigned int upDataDelayCnt; - unsigned char uartItTxFlag; - unsigned char rxAckFlag; -} FRAME_Handle; - - -void UartRecvInit(void); -void UartModuleProcess_Rx(MTRCTRL_Handle *mtrCtrl); -void UartModuleProcess_Tx(MTRCTRL_Handle *mtrCtrl); - -#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/readme.md b/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/readme.md deleted file mode 100644 index 0414d5ef..00000000 --- a/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/readme.md +++ /dev/null @@ -1,20 +0,0 @@ -# E7050_Six_Step_Wave - -**【功能描述】** - -- 基于ECMCU105H/ECBMCU201MPC单板的反电动势采样的无感电机六步方波调速系统应用 - -**【环境要求】** - -- 所有单板电源改制为演示用的24V低压,电机选用E7050 -- 电机(黄绿蓝)相线分别对应功率板上U/V/W通道接口 - -**【配置方法】** - -- 将user文件夹替换原工程user文件夹即可 - -【操作说明】 - -- 点击开发板按键启动电机 - -- 开发板蓝色电位器,可以调节电机转动的快慢。(15-58.8HZ) \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/generatecode/feature.h b/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/generatecode/feature.h deleted file mode 100644 index 13108bb3..00000000 --- a/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/generatecode/feature.h +++ /dev/null @@ -1,94 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file feature.h - * @author MCU Driver Team - * @brief This file contains macro configurations related to the project. This file is generated by the IDE tool. - * @date 2025-04-23 14:18:03 - */ - -#ifndef McuMagicTag_FEATURE_H -#define McuMagicTag_FEATURE_H - -/* Macro definitions --------------------------------------------------------- */ -#define CHIP_3061MNPICA MACRO_ENABLE - -#define MACRO_ENABLE 1 -#define MACRO_DISABLE 0 - -/* Macro switch */ -#define BASE_DEFINE_USE_ASSERT MACRO_ENABLE -#ifndef FLASH_CRC_CONFIG -#define FLASH_CRC_CONFIG -#endif /* #ifndef FLASH_CRC_CONFIG */ -#define BASE_MATH_SINCOS_MIDDLE_TABLE MACRO_ENABLE /**< This macro is used to control the table type when the - BASE_MATH_GetSinCos() queries the table. When the value of - this macro is MACRO_ENABLE, the error value obtained by the - BASE_MATH_GetSinCos() is relatively small, and the return - value of the function may be greater than or less than the - actual value. When the value of this macro is MACRO_DISABLE, - the error value obtained by the BASE_MATH_GetSinCos() is - relatively large. However, in the range [0°, 180°) and - [180°, 360°), the return value of the function is either - greater than or less than the actual value. */ - -/* Peripheral module macro switch--------------------------------------------- */ -#define BOARD_DIM_NUM 1 /**< Number of dimming handle arrays. */ - -#define BOARD_KEY_NUM 10 /**< Number of key handle arrays. */ -#define BOARD_KEY_PRESS_ON GPIO_HIGH_LEVEL /**< GPIO status corresponding to long press valid. */ -#define BOARD_KEY_PRESS_OFF GPIO_LOW_LEVEL /**< GPIO status corresponding to short press valid. */ - -#define BOARD_LED_SEG_NUM 4 /**< Number of segments. */ -#define BOARD_LED_SEGMENT_ON GPIO_HIGH_LEVEL /**< GPIO level status corresponding to valid segments. */ -#define BOARD_LED_SEGMENT_OFF GPIO_LOW_LEVEL /**< GPIO level status corresponding to invalid segments. */ - -#define BOARD_MKEY_SCHEME_NUMBER BOARD_MKEY_SCHEME_NUMBER_ONE /**< Define the scheme to be adopted. */ -#define BOARD_MKEY_OUT_NUM 4 /**< Number of GPIO pins used as output during scanning. */ -#define BOARD_MKEY_IN_NUM 4 /**< Number of GPIO pins used as input during scanning. */ -#define BOARD_MKEY_OUT_PIN_VALID GPIO_LOW_LEVEL /**< GPIO level status corresponding to the valid \ - status of the output GPIO in the key matrix. */ -#define BOARD_MKEY_OUT_PIN_INVALID GPIO_HIGH_LEVEL /**< GPIO level status corresponding to the \ - invalid status of the output GPIO in the key matrix. */ -#define BOARD_MKEY_IN_PIN_VALID GPIO_LOW_LEVEL /**< Indicates the GPIO level corresponding to the \ - valid status of the input GPIO in the key matrix. */ -#define BOARD_MKEY_IN_PIN_INVALID GPIO_HIGH_LEVEL /**< Indicates the GPIO level corresponding to the \ - invalid status of the input GPIO in the key matrix. */ - -#define BOARD_PULSES_NUM 2 /**< Number of pulse handles. */ - -#define BASE_DEFINE_SLIPAVERAGE_NUM 2 /**< Sliding average array length. */ - -#define LISTNODE_MAX 20 - -#define BASE_DEFINE_DMA_QUICKSTART - -#define XTRAIL_FREQ 30000000U - -#define DBG_USE_NO_PRINTF 0U -#define DBG_USE_UART_PRINTF 1U - -#define DBG_PRINTF_USE DBG_USE_UART_PRINTF -#if (DBG_PRINTF_USE == DBG_USE_UART_PRINTF) -#define DBG_PRINTF_UART_PORT UART0 -#endif - -/* USER CODE BEGIN 0 */ -/* USER CODE 区域内代码不会被覆盖,区域外会被生成的默认代码覆盖(其余USER CODE 区域同理) */ -/* USER CODE END 0 */ - -#endif /* McuMagicTag_FEATURE_H */ \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/generatecode/main.h b/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/generatecode/main.h deleted file mode 100644 index 55b93f82..00000000 --- a/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/generatecode/main.h +++ /dev/null @@ -1,84 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file main.h - * @author MCU Driver Team - * @brief This file contains driver init functions. - * @date 2025-04-23 14:18:03 - */ - -/* Define to prevent recursive inclusion ------------------------------------- */ -#ifndef McuMagicTag_SYSTEM_INIT_H -#define McuMagicTag_SYSTEM_INIT_H - -#include "adc.h" -#include "adc_ex.h" -#include "acmp.h" -#include "acmp_ex.h" -#include "apt.h" -#include "uart.h" -#include "uart_ex.h" -#include "gpio.h" -#include "timer.h" -#include "timer_ex.h" -#include "crg.h" -#include "iocmg.h" - -#define IO_SPEED_FAST 0x00U -#define IO_SPEED_SLOW 0x01U - -#define IO_DRV_LEVEL4 0x00U -#define IO_DRV_LEVEL3 0x01U -#define IO_DRV_LEVEL2 0x02U -#define IO_DRV_LEVEL1 0x03U - -#define XTAL_DRV_LEVEL4 0x03U -#define XTAL_DRV_LEVEL3 0x02U -#define XTAL_DRV_LEVEL2 0x01U -#define XTAL_DRV_LEVEL1 0x00U - -#define SYS_LED_PIN GPIO_PIN_3 -#define SYS_LED_HANDLE g_gpio2 -#define GpioStartStop_PIN GPIO_PIN_4 -#define GpioStartStop_HANDLE g_gpio2 - -extern ACMP_Handle g_acmp0; -extern TIMER_Handle g_timer1; -extern UART_Handle g_uart0; -extern APT_Handle g_apt0; -extern APT_Handle g_apt1; -extern APT_Handle g_apt2; -extern ADC_Handle g_adc0; - -extern GPIO_Handle g_gpio1; -extern GPIO_Handle g_gpio2; - -BASE_StatusType CRG_Config(CRG_CoreClkSelect *coreClkSelect); -void SystemInit(void); - -void MotorStatemachineCallBack(void *handle); -void TIMER1_DMAOverFlow_InterruptProcess(void *handle); -void MotorCarrierProcessCallback(void *aptHandle); -void MotorSysErrCallback(void *aptHandle); - -void MotorStartStopKeyCallback(void *param); - -/* USER CODE BEGIN 0 */ -/* USER CODE 区域内代码不会被覆盖,区域外会被生成的默认代码覆盖(其余USER CODE 区域同理) */ -/* USER CODE END 0 */ - -#endif /* McuMagicTag_SYSTEM_INIT_H */ \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/generatecode/system_init.c b/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/generatecode/system_init.c deleted file mode 100644 index 76ec601a..00000000 --- a/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/generatecode/system_init.c +++ /dev/null @@ -1,530 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file system_init.c - * @author MCU Driver Team - * @brief This file contains driver init functions. - * @date 2025-04-23 14:18:03 - */ - -#include "main.h" -#include "ioconfig.h" -#include "iocmg_ip.h" - -#define UART0_BAUD_RATE 115200 - -BASE_StatusType CRG_Config(CRG_CoreClkSelect *coreClkSelect) -{ - CRG_Handle crg; - crg.baseAddress = CRG; - crg.pllRefClkSelect = CRG_PLL_REF_CLK_SELECT_HOSC; - crg.pllPreDiv = CRG_PLL_PREDIV_4; - crg.pllFbDiv = 48; /* PLL Multiplier 48 */ - crg.pllPostDiv = CRG_PLL_POSTDIV_2; - crg.coreClkSelect = CRG_CORE_CLK_SELECT_PLL; - crg.handleEx.pllPostDiv2 = CRG_PLL_POSTDIV2_3; - crg.handleEx.clk1MSelect = CRG_1M_CLK_SELECT_HOSC; - crg.handleEx.clk1MDiv = (25 - 1); /* The 1 MHz freq is equal to the input clock frequency / (clk_1m_div + 1). 25 is the div of the clk_1m in CLOCK. */ - - if (HAL_CRG_Init(&crg) != BASE_STATUS_OK) { - return BASE_STATUS_ERROR; - } - *coreClkSelect = crg.coreClkSelect; - return BASE_STATUS_OK; -} - -static void ACMP0_Init(void) -{ - HAL_CRG_IpEnableSet(ACMP0_BASE, IP_CLK_ENABLE); /* ACMP clock bit reset. */ - g_acmp0.baseAddress = ACMP0_BASE; - g_acmp0.inOutConfig.inputNNum = ACMP_INPUT_N_SELECT2; - g_acmp0.inOutConfig.inputPNum = ACMP_INPUT_P_SELECT2; - g_acmp0.inOutConfig.polarity = ACMP_OUT_NOT_INVERT; - g_acmp0.filterCtrl.filterMode = ACMP_FILTER_NONE; - g_acmp0.hysteresisVol = ACMP_HYS_VOL_30MV; - g_acmp0.interruptEn = BASE_CFG_UNSET; - HAL_ACMP_Init(&g_acmp0); -} - -static void ADC0_Init(void) -{ - HAL_CRG_IpEnableSet(ADC0_BASE, IP_CLK_ENABLE); - HAL_CRG_IpClkSelectSet(ADC0_BASE, CRG_ADC_CLK_ASYN_PLL_DIV); - HAL_CRG_IpClkDivSet(ADC0_BASE, CRG_ADC_DIV_1); - - g_adc0.baseAddress = ADC0; - g_adc0.socPriority = ADC_PRIMODE_ALL_ROUND; - - HAL_ADC_Init(&g_adc0); - - SOC_Param socParam = {0}; - socParam.adcInput = ADC_CH_ADCINA11; /* PIN9(ADC AIN11) */ - socParam.sampleTotalTime = ADC_SOCSAMPLE_5CLK; /* adc sample total time 5 adc_clk */ - socParam.trigSource = ADC_TRIGSOC_APT0_SOCA; - socParam.continueMode = BASE_CFG_DISABLE; - socParam.finishMode = ADC_SOCFINISH_NONE; - HAL_ADC_ConfigureSoc(&g_adc0, ADC_SOC_NUM5, &socParam); - - socParam.adcInput = ADC_CH_ADCINA12; /* PIN10(ADC AIN12) */ - socParam.sampleTotalTime = ADC_SOCSAMPLE_5CLK; /* adc sample total time 5 adc_clk */ - socParam.trigSource = ADC_TRIGSOC_APT0_SOCA; - socParam.continueMode = BASE_CFG_DISABLE; - socParam.finishMode = ADC_SOCFINISH_NONE; - HAL_ADC_ConfigureSoc(&g_adc0, ADC_SOC_NUM2, &socParam); - - socParam.adcInput = ADC_CH_ADCINA10; /* PIN8(ADC AIN10) */ - socParam.sampleTotalTime = ADC_SOCSAMPLE_5CLK; /* adc sample total time 5 adc_clk */ - socParam.trigSource = ADC_TRIGSOC_APT0_SOCA; - socParam.continueMode = BASE_CFG_DISABLE; - socParam.finishMode = ADC_SOCFINISH_NONE; - HAL_ADC_ConfigureSoc(&g_adc0, ADC_SOC_NUM6, &socParam); - - socParam.adcInput = ADC_CH_ADCINA9; /* PIN7(ADC AIN9) */ - socParam.sampleTotalTime = ADC_SOCSAMPLE_5CLK; /* adc sample total time 5 adc_clk */ - socParam.trigSource = ADC_TRIGSOC_SOFT; - socParam.continueMode = BASE_CFG_DISABLE; - socParam.finishMode = ADC_SOCFINISH_NONE; - HAL_ADC_ConfigureSoc(&g_adc0, ADC_SOC_NUM9, &socParam); -} - -__weak void MotorSysErrCallback(void *aptHandle) -{ - BASE_FUNC_UNUSED(aptHandle); - /* USER CODE BEGIN APT0_EVENT_INTERRUPT */ - /* USER CODE END APT0_EVENT_INTERRUPT */ -} - -__weak void MotorCarrierProcessCallback(void *aptHandle) -{ - BASE_FUNC_UNUSED(aptHandle); - /* USER CODE BEGIN APT0_TIMER_INTERRUPT */ - /* USER CODE END APT0_TIMER_INTERRUPT */ -} - -static void APT0_ProtectInit(void) -{ - APT_OutCtrlProtectEx protectApt = {0}; - protectApt.ocEventEnEx = BASE_CFG_ENABLE; - protectApt.ocEventModeEx = APT_OUT_CTRL_ONE_SHOT; - protectApt.ocActionEx = APT_OUT_CTRL_ACTION_LOW; - protectApt.ocActionBEx = APT_OUT_CTRL_ACTION_LOW; - protectApt.ocEvtInterruptEnEx = BASE_CFG_ENABLE; - protectApt.ocSysEvent = APT_SYS_EVT_DEBUG | APT_SYS_EVT_CLK | APT_SYS_EVT_MEM; - protectApt.originalEvtEx = APT_EM_ORIGINAL_SRC_ACMP0; - protectApt.evtPolarityMaskEx = APT_EM_ACMP0_INVERT_BIT; - protectApt.filterCycleNumEx = 0; - HAL_APT_ProtectInitEx(&g_apt0, &protectApt); -} - -static void APT0_Init(void) -{ - HAL_CRG_IpEnableSet(APT0_BASE, IP_CLK_ENABLE); - - g_apt0.baseAddress = APT0; - - /* Clock Settings */ - g_apt0.waveform.dividerFactor = 1 - 1; - /* Timer Settings */ - g_apt0.waveform.timerPeriod = 6250; /* 6250 is count period of APT time-base timer */ - g_apt0.waveform.cntMode = APT_COUNT_MODE_UP_DOWN; - - /* Wave Form */ - g_apt0.waveform.basicType = APT_PWM_BASIC_A_HIGH_B_LOW; - g_apt0.waveform.chAOutType = APT_PWM_OUT_BASIC_TYPE; - g_apt0.waveform.chBOutType = APT_PWM_OUT_BASIC_TYPE; - g_apt0.waveform.divInitVal = 0; - g_apt0.waveform.cntInitVal = 0; - g_apt0.waveform.cntCmpLeftEdge = 3125; /* 3125 is count compare point of the left edge of PWM waveform */ - g_apt0.waveform.cntCmpRightEdge = 3125; /* 3125 is count compare point of the right edge of PWM waveform */ - g_apt0.waveform.cntCmpLoadMode = APT_BUFFER_INDEPENDENT_LOAD; - g_apt0.waveform.cntCmpLoadEvt = APT_COMPARE_LOAD_EVENT_ZERO; - g_apt0.waveform.deadBandCnt = 150; /* 150 is dead-band value */ - - /* ADC Trigger SOCA */ - g_apt0.adcTrg.trgEnSOCA = BASE_CFG_ENABLE; - g_apt0.adcTrg.cntCmpSOCA = 6000; /* 6000 is count compare point of ADC trigger source SOCA when using CMPA */ - g_apt0.adcTrg.trgSrcSOCA = APT_CS_SRC_CNTR_CMPA_DOWN; - g_apt0.adcTrg.trgScaleSOCA = 1; - - /* ADC Trigger SOCB */ - g_apt0.adcTrg.trgEnSOCB = BASE_CFG_ENABLE; - g_apt0.adcTrg.cntCmpSOCB = 1; - g_apt0.adcTrg.trgSrcSOCB = APT_CS_SRC_CNTR_CMPA_DOWN; - g_apt0.adcTrg.trgScaleSOCB = 1; - - g_apt0.adcTrg.cntCmpLoadMode = APT_BUFFER_INDEPENDENT_LOAD; - g_apt0.adcTrg.cntCmpLoadEvt = APT_COMPARE_LOAD_EVENT_ZERO; - - /* Timer Trigger */ - g_apt0.tmrInterrupt.tmrInterruptEn = BASE_CFG_ENABLE; - g_apt0.tmrInterrupt.tmrInterruptSrc = APT_INT_SRC_CNTR_CMPC_UP; - g_apt0.tmrInterrupt.tmrInterruptScale = 1; - - APT0_ProtectInit(); - - HAL_APT_PWMInit(&g_apt0); - HAL_APT_RegisterCallBack(&g_apt0, APT_EVENT_INTERRUPT, MotorSysErrCallback); - IRQ_SetPriority(IRQ_APT0_EVT, 7); /* 7 is priority value */ - IRQ_Register(IRQ_APT0_EVT, HAL_APT_EventIrqHandler, &g_apt0); - IRQ_EnableN(IRQ_APT0_EVT); - HAL_APT_RegisterCallBack(&g_apt0, APT_TIMER_INTERRUPT, MotorCarrierProcessCallback); - IRQ_SetPriority(IRQ_APT0_TMR, 6); /* 6 is priority value */ - IRQ_Register(IRQ_APT0_TMR, HAL_APT_TimerIrqHandler, &g_apt0); - IRQ_EnableN(IRQ_APT0_TMR); -} - -static void APT1_ProtectInit(void) -{ - APT_OutCtrlProtectEx protectApt = {0}; - protectApt.ocEventEnEx = BASE_CFG_ENABLE; - protectApt.ocEventModeEx = APT_OUT_CTRL_ONE_SHOT; - protectApt.ocActionEx = APT_OUT_CTRL_ACTION_LOW; - protectApt.ocActionBEx = APT_OUT_CTRL_ACTION_LOW; - protectApt.ocEvtInterruptEnEx = BASE_CFG_DISABLE; - protectApt.ocSysEvent = APT_SYS_EVT_DEBUG | APT_SYS_EVT_CLK | APT_SYS_EVT_MEM; - protectApt.originalEvtEx = APT_EM_ORIGINAL_SRC_ACMP0; - protectApt.evtPolarityMaskEx = APT_EM_ACMP0_INVERT_BIT; - protectApt.filterCycleNumEx = 0; - HAL_APT_ProtectInitEx(&g_apt1, &protectApt); -} - -static void APT1_Init(void) -{ - HAL_CRG_IpEnableSet(APT1_BASE, IP_CLK_ENABLE); - - g_apt1.baseAddress = APT1; - - /* Clock Settings */ - g_apt1.waveform.dividerFactor = 1 - 1; - /* Timer Settings */ - g_apt1.waveform.timerPeriod = 6250; /* 6250 is count period of APT time-base timer */ - g_apt1.waveform.cntMode = APT_COUNT_MODE_UP_DOWN; - - /* Wave Form */ - g_apt1.waveform.basicType = APT_PWM_BASIC_A_HIGH_B_LOW; - g_apt1.waveform.chAOutType = APT_PWM_OUT_BASIC_TYPE; - g_apt1.waveform.chBOutType = APT_PWM_OUT_BASIC_TYPE; - g_apt1.waveform.divInitVal = 0; - g_apt1.waveform.cntInitVal = 0; - g_apt1.waveform.cntCmpLeftEdge = 3125; /* 3125 is count compare point of the left edge of PWM waveform */ - g_apt1.waveform.cntCmpRightEdge = 3125; /* 3125 is count compare point of the right edge of PWM waveform */ - g_apt1.waveform.cntCmpLoadMode = APT_BUFFER_INDEPENDENT_LOAD; - g_apt1.waveform.cntCmpLoadEvt = APT_COMPARE_LOAD_EVENT_ZERO; - g_apt1.waveform.deadBandCnt = 150; /* 150 is dead-band value */ - - /* ADC Trigger SOCA */ - g_apt1.adcTrg.trgEnSOCA = BASE_CFG_ENABLE; - g_apt1.adcTrg.cntCmpSOCA = 1; /* 1 is count compare point of ADC trigger source SOCA when using CMPA */ - g_apt1.adcTrg.trgSrcSOCA = APT_CS_SRC_CNTR_ZERO; - g_apt1.adcTrg.trgScaleSOCA = 1; - - g_apt1.adcTrg.cntCmpLoadMode = APT_BUFFER_INDEPENDENT_LOAD; - g_apt1.adcTrg.cntCmpLoadEvt = APT_COMPARE_LOAD_EVENT_ZERO; - - APT1_ProtectInit(); - - HAL_APT_PWMInit(&g_apt1); -} - -static void APT2_ProtectInit(void) -{ - APT_OutCtrlProtectEx protectApt = {0}; - protectApt.ocEventEnEx = BASE_CFG_ENABLE; - protectApt.ocEventModeEx = APT_OUT_CTRL_ONE_SHOT; - protectApt.ocActionEx = APT_OUT_CTRL_ACTION_LOW; - protectApt.ocActionBEx = APT_OUT_CTRL_ACTION_LOW; - protectApt.ocEvtInterruptEnEx = BASE_CFG_DISABLE; - protectApt.ocSysEvent = APT_SYS_EVT_DEBUG | APT_SYS_EVT_CLK | APT_SYS_EVT_MEM; - protectApt.originalEvtEx = APT_EM_ORIGINAL_SRC_ACMP0; - protectApt.evtPolarityMaskEx = APT_EM_ACMP0_INVERT_BIT; - protectApt.filterCycleNumEx = 0; - HAL_APT_ProtectInitEx(&g_apt2, &protectApt); -} - -static void APT2_Init(void) -{ - HAL_CRG_IpEnableSet(APT2_BASE, IP_CLK_ENABLE); - - g_apt2.baseAddress = APT2; - - /* Clock Settings */ - g_apt2.waveform.dividerFactor = 1 - 1; - /* Timer Settings */ - g_apt2.waveform.timerPeriod = 6250; /* 6250 is count period of APT time-base timer */ - g_apt2.waveform.cntMode = APT_COUNT_MODE_UP_DOWN; - - /* Wave Form */ - g_apt2.waveform.basicType = APT_PWM_BASIC_A_HIGH_B_LOW; - g_apt2.waveform.chAOutType = APT_PWM_OUT_BASIC_TYPE; - g_apt2.waveform.chBOutType = APT_PWM_OUT_BASIC_TYPE; - g_apt2.waveform.divInitVal = 0; - g_apt2.waveform.cntInitVal = 0; - g_apt2.waveform.cntCmpLeftEdge = 3125; /* 3125 is count compare point of the left edge of PWM waveform */ - g_apt2.waveform.cntCmpRightEdge = 3125; /* 3125 is count compare point of the right edge of PWM waveform */ - g_apt2.waveform.cntCmpLoadMode = APT_BUFFER_INDEPENDENT_LOAD; - g_apt2.waveform.cntCmpLoadEvt = APT_COMPARE_LOAD_EVENT_ZERO; - g_apt2.waveform.deadBandCnt = 150; /* 150 is dead-band value */ - - APT2_ProtectInit(); - - HAL_APT_PWMInit(&g_apt2); -} - -__weak void MotorStartStopKeyCallback(void *param) -{ - GPIO_Handle *handle = (GPIO_Handle *)param; - BASE_FUNC_UNUSED(handle); -} - -static void GPIO_Init(void) -{ - HAL_CRG_IpEnableSet(GPIO1_BASE, IP_CLK_ENABLE); - g_gpio1.baseAddress = GPIO1; - - g_gpio1.pins = GPIO_PIN_0; - HAL_GPIO_Init(&g_gpio1); - HAL_GPIO_SetDirection(&g_gpio1, g_gpio1.pins, GPIO_OUTPUT_MODE); - HAL_GPIO_SetValue(&g_gpio1, g_gpio1.pins, GPIO_HIGH_LEVEL); - HAL_GPIO_SetIrqType(&g_gpio1, g_gpio1.pins, GPIO_INT_TYPE_NONE); - - HAL_CRG_IpEnableSet(GPIO2_BASE, IP_CLK_ENABLE); - g_gpio2.baseAddress = GPIO2; - - g_gpio2.pins = GPIO_PIN_3; - HAL_GPIO_Init(&g_gpio2); - HAL_GPIO_SetDirection(&g_gpio2, g_gpio2.pins, GPIO_OUTPUT_MODE); - HAL_GPIO_SetValue(&g_gpio2, g_gpio2.pins, GPIO_HIGH_LEVEL); - HAL_GPIO_SetIrqType(&g_gpio2, g_gpio2.pins, GPIO_INT_TYPE_NONE); - - g_gpio2.pins = GPIO_PIN_4; - HAL_GPIO_Init(&g_gpio2); - HAL_GPIO_SetDirection(&g_gpio2, g_gpio2.pins, GPIO_INPUT_MODE); - HAL_GPIO_SetValue(&g_gpio2, g_gpio2.pins, GPIO_LOW_LEVEL); - HAL_GPIO_SetIrqType(&g_gpio2, g_gpio2.pins, GPIO_INT_TYPE_FALL_EDGE); - - HAL_GPIO_RegisterCallBack(&g_gpio2, GPIO_PIN_4, MotorStartStopKeyCallback); - IRQ_Register(IRQ_GPIO2, HAL_GPIO_IrqHandler, &g_gpio2); - IRQ_SetPriority(IRQ_GPIO2, 1); /* set gpio1 interrupt priority to 1, 1~15. 1 is priority value */ - IRQ_EnableN(IRQ_GPIO2); /* gpio interrupt enable */ - - return; -} - -__weak void MotorStatemachineCallBack(void *handle) -{ - BASE_FUNC_UNUSED(handle); - /* USER CODE BEGIN MotorStatemachineCallBack */ - /* USER CODE END MotorStatemachineCallBack */ -} - -static void TIMER1_Init(void) -{ - HAL_CRG_IpEnableSet(TIMER1_BASE, IP_CLK_ENABLE); /* TIMER1 clock enable. */ - unsigned int load = ((float)HAL_CRG_GetIpFreq((void *)TIMER1) / (1u << (TIMERPRESCALER_NO_DIV * 4)) / 1000000u) * 500; - - g_timer1.baseAddress = TIMER1; - g_timer1.load = (load < 1) ? 0 : (load - 1); /* Set timer value immediately */ - g_timer1.bgLoad = (load < 1) ? 0 : (load - 1); /* Set timer value */ - g_timer1.mode = TIMER_MODE_RUN_PERIODIC; /* Run in period mode */ - g_timer1.prescaler = TIMERPRESCALER_NO_DIV; /* Don't frequency division */ - g_timer1.size = TIMER_SIZE_32BIT; /* 1 for 32bit, 0 for 16bit */ - g_timer1.interruptEn = BASE_CFG_ENABLE; - g_timer1.adcSocReqEnable = BASE_CFG_DISABLE; - g_timer1.dmaReqEnable = BASE_CFG_DISABLE; - HAL_TIMER_Init(&g_timer1); - IRQ_Register(IRQ_TIMER1, HAL_TIMER_IrqHandler, &g_timer1); - - HAL_TIMER_RegisterCallback(&g_timer1, TIMER_PERIOD_FIN, MotorStatemachineCallBack); - IRQ_SetPriority(IRQ_TIMER1, 2); /* 2 is priority value */ - IRQ_EnableN(IRQ_TIMER1); -} - -static void UART0_Init(void) -{ - HAL_CRG_IpEnableSet(UART0_BASE, IP_CLK_ENABLE); /* UART0 clock enable. */ - g_uart0.baseAddress = UART0; - - g_uart0.baudRate = UART0_BAUD_RATE; - g_uart0.dataLength = UART_DATALENGTH_8BIT; - g_uart0.stopBits = UART_STOPBITS_ONE; - g_uart0.parity = UART_PARITY_NONE; - g_uart0.txMode = UART_MODE_BLOCKING; - g_uart0.rxMode = UART_MODE_BLOCKING; - g_uart0.fifoMode = BASE_CFG_ENABLE; - g_uart0.fifoTxThr = UART_FIFODEPTH_SIZE8; - g_uart0.fifoRxThr = UART_FIFODEPTH_SIZE8; - g_uart0.hwFlowCtr = BASE_CFG_DISABLE; - g_uart0.handleEx.overSampleMultiple = UART_OVERSAMPLING_16X; - g_uart0.handleEx.msbFirst = BASE_CFG_DISABLE; - HAL_UART_Init(&g_uart0); -} - -static void IOConfig(void) -{ - SYSCTRL0->SC_SYS_STAT.BIT.update_mode = 0; - SYSCTRL0->SC_SYS_STAT.BIT.update_mode_clear = 1; - /* Config PIN9 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO3_5_AS_ADC_AIN11); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO3_5_AS_ADC_AIN11, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO3_5_AS_ADC_AIN11, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO3_5_AS_ADC_AIN11, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO3_5_AS_ADC_AIN11, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN10 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO5_3_AS_ADC_AIN12); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO5_3_AS_ADC_AIN12, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO5_3_AS_ADC_AIN12, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO5_3_AS_ADC_AIN12, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO5_3_AS_ADC_AIN12, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN8 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO3_6_AS_ADC_AIN10); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO3_6_AS_ADC_AIN10, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO3_6_AS_ADC_AIN10, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO3_6_AS_ADC_AIN10, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO3_6_AS_ADC_AIN10, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN7 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO3_7_AS_ADC_AIN9); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO3_7_AS_ADC_AIN9, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO3_7_AS_ADC_AIN9, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO3_7_AS_ADC_AIN9, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO3_7_AS_ADC_AIN9, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN48 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO0_6_AS_ACMP_N2); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO0_6_AS_ACMP_N2, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO0_6_AS_ACMP_N2, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO0_6_AS_ACMP_N2, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO0_6_AS_ACMP_N2, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN47 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO0_5_AS_ACMP_P2); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO0_5_AS_ACMP_P2, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO0_5_AS_ACMP_P2, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO0_5_AS_ACMP_P2, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO0_5_AS_ACMP_P2, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN1 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO0_7_AS_ACMP0_OUT); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO0_7_AS_ACMP0_OUT, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO0_7_AS_ACMP0_OUT, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO0_7_AS_ACMP0_OUT, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO0_7_AS_ACMP0_OUT, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN27 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO1_0_AS_GPIO1_0); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO1_0_AS_GPIO1_0, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO1_0_AS_GPIO1_0, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO1_0_AS_GPIO1_0, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO1_0_AS_GPIO1_0, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN35 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO2_3_AS_GPIO2_3); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO2_3_AS_GPIO2_3, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO2_3_AS_GPIO2_3, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO2_3_AS_GPIO2_3, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO2_3_AS_GPIO2_3, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN41 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO2_4_AS_GPIO2_4); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO2_4_AS_GPIO2_4, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO2_4_AS_GPIO2_4, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO2_4_AS_GPIO2_4, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO2_4_AS_GPIO2_4, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN39 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO0_3_AS_UART0_TXD); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO0_3_AS_UART0_TXD, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO0_3_AS_UART0_TXD, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO0_3_AS_UART0_TXD, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO0_3_AS_UART0_TXD, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN40 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO0_4_AS_UART0_RXD); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO0_4_AS_UART0_RXD, PULL_UP); /* Pull-up and Pull-down, UART RX recommend PULL_UP */ - HAL_IOCMG_SetPinSchmidtMode(GPIO0_4_AS_UART0_RXD, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO0_4_AS_UART0_RXD, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO0_4_AS_UART0_RXD, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN19 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO3_0_AS_APT0_PWMA); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO3_0_AS_APT0_PWMA, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO3_0_AS_APT0_PWMA, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO3_0_AS_APT0_PWMA, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO3_0_AS_APT0_PWMA, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN23 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO4_0_AS_APT0_PWMB); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO4_0_AS_APT0_PWMB, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO4_0_AS_APT0_PWMB, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO4_0_AS_APT0_PWMB, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO4_0_AS_APT0_PWMB, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN20 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO3_1_AS_APT1_PWMA); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO3_1_AS_APT1_PWMA, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO3_1_AS_APT1_PWMA, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO3_1_AS_APT1_PWMA, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO3_1_AS_APT1_PWMA, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN24 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO4_1_AS_APT1_PWMB); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO4_1_AS_APT1_PWMB, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO4_1_AS_APT1_PWMB, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO4_1_AS_APT1_PWMB, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO4_1_AS_APT1_PWMB, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN21 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO3_2_AS_APT2_PWMA); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO3_2_AS_APT2_PWMA, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO3_2_AS_APT2_PWMA, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO3_2_AS_APT2_PWMA, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO3_2_AS_APT2_PWMA, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN25 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO4_2_AS_APT2_PWMB); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO4_2_AS_APT2_PWMB, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO4_2_AS_APT2_PWMB, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO4_2_AS_APT2_PWMB, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO4_2_AS_APT2_PWMB, DRIVER_RATE_2); /* Output signal edge fast/slow */ -} - -static void APT_SyncMasterInit(void) -{ - HAL_APT_MasterSyncInit(&g_apt0, APT_SYNC_OUT_ON_CNTR_ZERO); -} - -static void APT_SyncSlaveInit(void) -{ - APT_SlaveSyncIn aptSlave; - - aptSlave.cntPhase = 0; /* counter phase value */ - aptSlave.syncCntMode = APT_COUNT_MODE_AFTER_SYNC_UP; - aptSlave.syncInSrc = APT_SYNCIN_SRC_APT0_SYNCOUT; /* sync source selection */ - aptSlave.cntrSyncSrc = APT_CNTR_SYNC_SRC_SYNCIN; - HAL_APT_SlaveSyncInit(&g_apt1, &aptSlave); - - aptSlave.cntPhase = 0; /* counter phase value */ - aptSlave.syncCntMode = APT_COUNT_MODE_AFTER_SYNC_UP; - aptSlave.syncInSrc = APT_SYNCIN_SRC_APT0_SYNCOUT; /* sync source selection */ - aptSlave.cntrSyncSrc = APT_CNTR_SYNC_SRC_SYNCIN; - HAL_APT_SlaveSyncInit(&g_apt2, &aptSlave); - -} - -void SystemInit(void) -{ - IOConfig(); - UART0_Init(); - ACMP0_Init(); - APT0_Init(); - APT1_Init(); - APT2_Init(); - ADC0_Init(); - TIMER1_Init(); - GPIO_Init(); - - APT_SyncMasterInit(); - APT_SyncSlaveInit(); - - /* USER CODE BEGIN system_init */ - /* USER CODE END system_init */ -} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/main.c b/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/main.c deleted file mode 100644 index b8775e1a..00000000 --- a/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/main.c +++ /dev/null @@ -1,67 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file main.c - * @author MCU Driver Team - * @brief Main program body. - * @date 2025-04-23 14:18:03 - */ - -#include "typedefs.h" -#include "feature.h" -#include "mcs_motor_process.h" -#include "main.h" -/* USER CODE BEGIN 0 */ -/* USER CODE 区域内代码不会被覆盖,区域外会被生成的默认代码覆盖(其余USER CODE 区域同理) */ -/* 建议用户放置头文件 */ -/* USER CODE END 0 */ -ACMP_Handle g_acmp0; -TIMER_Handle g_timer1; -UART_Handle g_uart0; -APT_Handle g_apt0; -APT_Handle g_apt1; -APT_Handle g_apt2; -ADC_Handle g_adc0; -GPIO_Handle g_gpio1; -GPIO_Handle g_gpio2; -/* USER CODE BEGIN 1 */ -/* 建议用户定义全局变量、结构体、宏定义或函数声明等 */ -/* USER CODE END 1 */ - -int main(void) -{ - /* USER CODE BEGIN 2 */ - /* 建议用户放置初始化代码或启动代码等 */ - /* USER CODE END 2 */ - MotorMainProcess(); - /* USER CODE BEGIN 3 */ - /* 建议用户放置初始配置代码 */ - /* USER CODE END 3 */ - while (1) { - /* USER CODE BEGIN 4 */ - /* 建议用户放置周期性执行代码 */ - /* USER CODE END 4 */ - } - /* USER CODE BEGIN 5 */ - /* 建议用户放置代码流程 */ - /* USER CODE END 5 */ - return BASE_STATUS_OK; -} - -/* USER CODE BEGIN 6 */ -/* 建议用户放置自定义函数 */ -/* USER CODE END 6 */ \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/application/mcs_motor_process.c b/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/application/mcs_motor_process.c deleted file mode 100644 index 94898dd5..00000000 --- a/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/application/mcs_motor_process.c +++ /dev/null @@ -1,562 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_motor_process.c - * @author MCU Algorithm Team - * @brief This file provides motor application for ECBMCU201MPC board. - * @details BLDC six-step application based on the ECBMCU201MPC board - * 1) Motor model is Gimbal GBM2804H-100T. - * 2) Select the bldc six-step sensorless example in the sample column - of chipConfig and click Generate Code. - * 3) It's power supply must be changed to 12V. - */ - -#include "debug.h" -#include "mcs_assert.h" -#include "mcs_user_config.h" -#include "mcs_status.h" -#include "mcs_carrier.h" -#include "mcs_motor_process.h" - -/*------------------------------- Macro Definition -----------------------------------------------*/ -#define US_PER_MS 1000 -#define APT_FULL_DUTY 1.0f - -/* Motor control handle for bldc */ -static MtrCtrlHandle g_mc; - -static APT_RegStruct* g_aptCp[PHASE_MAX_NUM] = {BRIDGE_CTR_APT_U, BRIDGE_CTR_APT_V, BRIDGE_CTR_APT_W}; - -/** - * @brief Initialzer of system tick. - * @param mtrCtrl Motor control struct handle. - * @retval None. - */ -static void TimerTickInit(MtrCtrlHandle *mtrCtrl) -{ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - mtrCtrl->sysTickCnt = 0; - mtrCtrl->msTickNum = US_PER_MS / SYSTICK_PERIOD_US; - mtrCtrl->capChargeTickNum = (INV_CAP_CHARGE_MS * US_PER_MS / SYSTICK_PERIOD_US); -} - -/** - * @brief Read the ADC current sampling value of the compressor. - * @param CurrUvw Three-phase current. - * @retval None. - */ -static void ReadBemfUVWMotor(UVWBemf *bemfUVW) -{ - MCS_ASSERT_PARAM(bemfUVW != NULL); - bemfUVW->u = (int)(HAL_ADC_GetConvResult(&ADC_HANDLE, ADC_U_SOC_NUM)&0xFFF); - bemfUVW->v = (int)(HAL_ADC_GetConvResult(&ADC_HANDLE, ADC_V_SOC_NUM)&0xFFF); - bemfUVW->w = (int)(HAL_ADC_GetConvResult(&ADC_HANDLE, ADC_W_SOC_NUM)&0xFFF); -} - -/** - * @brief Init motor control task. - * @retval None. - */ -static void TSK_InitMotor(void) -{ - /* Initialize target speed. */ - g_mc.spdCmdHz = SDP_TARGET_VALUE; - /* zeroPoint = IN_VOLTAGE_BUS / 2.0; 4095/3.3 :ADC value corresponding to 1 V */ - g_mc.zeroPoint = ((((float)IN_VOLTAGE_BUS / 2.0) * VOL_DIVIDER_COEFFICIENT) * 4095 / 3.3); - g_mc.pwmDuty = FORCE_DRAG_MINDUTY; - - /* Sets the number of sample filtering times for zero-crossing sampling. */ - g_mc.sysVar.bemfFilterCnt = FILTER_COUNT; - g_mc.sysVar.dragChangePhaseTime = DRAG_START_INTERVAL; - - g_mc.stateMachine = FSM_IDLE; - g_mc.aptMaxcntCmp = g_apt0.waveform.timerPeriod; - - /* Speed management initialization. */ - RMG_Init(&g_mc.spdRmg, CTRL_SYSTICK_PERIOD, USER_SPD_SLOPE); /* Init speed slope */ - TimerTickInit(&g_mc); - - /* Pid control parameter initialization. */ - g_mc.spdPi.kp = SPD_PID_KP; - g_mc.spdPi.ki = SPD_PID_KI; - g_mc.spdPi.ts = SPD_PID_TS; - g_mc.spdPi.upperLimit = APT_DUTYLIMIT_MAX; - g_mc.spdPi.lowerLimit = APT_DUTYLIMIT_MIN; - - /* BLDC six-step control initialization. */ - g_mc.stepCtrl.phaseStep = STEP1; - g_mc.stepCtrl.controlApt.u = &g_apt0; - g_mc.stepCtrl.controlApt.v = &g_apt1; - g_mc.stepCtrl.controlApt.w = &g_apt2; -} - - -/** - * @brief Software Initialization. - * @retval None. - */ -static void InitSoftware(void) -{ - TSK_InitMotor(); - - g_mc.readBemfUVW = ReadBemfUVWMotor; -} - -/** - * @brief Enable three-phase pwm output. - * @param aptAddr Three-phase APT address pointer. - * @retval None. - */ -static void MotorPwmOutputEnable(APT_RegStruct **aptAddr) -{ - MCS_ASSERT_PARAM(aptAddr != NULL); - /* Enable three-phase pwm output */ - for (unsigned int i = 0; i < PHASE_MAX_NUM; i++) { - APT_RegStruct *aptx = aptAddr[i]; - aptx->PG_OUT_FRC.BIT.rg_pga_frc_en = BASE_CFG_UNSET; - aptx->PG_OUT_FRC.BIT.rg_pgb_frc_en = BASE_CFG_UNSET; - } -} - -/** - * @brief Disable three-phase pwm output. - * @param aptAddr Three-phase APT address pointer. - * @retval None. - */ -static void MotorPwmOutputDisable(APT_RegStruct **aptAddr) -{ - MCS_ASSERT_PARAM(aptAddr != NULL); - /* Disable three-phase pwm output. */ - for (unsigned int i = 0; i < PHASE_MAX_NUM; i++) { - APT_RegStruct *aptx = aptAddr[i]; - aptx->PG_OUT_FRC.BIT.rg_pga_frc_en = BASE_CFG_SET; - aptx->PG_OUT_FRC.BIT.rg_pgb_frc_en = BASE_CFG_SET; - DCL_APT_ForcePWMOutputLow(aptx); - } -} - -/** - * @brief 强制转子对准原位。 - * @param mtrCtrl The motor control handle. - * @retval None. - */ -static void ForceAlign(MtrCtrlHandle *mtrCtrl) -{ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - mtrCtrl->pwmDuty = FORCE_DRAG_MINDUTY; - mtrCtrl->sysVar.dragChangePhaseTime = DRAG_START_INTERVAL; - MCS_SetCtrAptDuty(mtrCtrl, mtrCtrl->pwmDuty); - MotorPwmOutputEnable(g_aptCp); - mtrCtrl->stepCtrl.phaseStep = STEP6; - SixStepPwm(&mtrCtrl->stepCtrl); - BASE_FUNC_DELAY_MS(100); /* Delay 100 ms waiting for rotor alignment. */ - mtrCtrl->stepCtrl.phaseStep = STEP1; - SixStepPwm(&mtrCtrl->stepCtrl); - BASE_FUNC_DELAY_MS(100); /* Delay 100 ms waiting for rotor alignment. */ - mtrCtrl->sysVar.lastZeroPoint = DCL_SYSTICK_GetTick(); -} - -/** - * @brief Pre-processing of motor status. - * @param statusReg System status. - * @param stateMachine Motor Control Status. - * @retval None. - */ -static void MotorStatePerProc(SysStatusReg *statusReg, volatile FsmState *stateMachine) -{ - MCS_ASSERT_PARAM(statusReg != NULL); - MCS_ASSERT_PARAM(stateMachine != NULL); - /* Get system status */ - if (SysIsError(statusReg)) { - *stateMachine = FSM_FAULT; - } - if (SysGetCmdStop(statusReg)) { - SysCmdStopClr(statusReg); - *stateMachine = FSM_WAIT_STOP; - } -} - -/** - * @brief Open the three-phase lower pipe. - * @param aptAddr Three-phase APT address pointer. - * @param maxDutyCnt Max duty count. - * @retval None. - */ -static void AptTurnOnLowSidePwm(APT_RegStruct **aptAddr, unsigned int maxDutyCnt) -{ - MCS_ASSERT_PARAM(aptAddr != NULL); - MCS_ASSERT_PARAM(maxDutyCnt != 0); - unsigned int dutyCnt; - dutyCnt = maxDutyCnt * APT_FULL_DUTY; - /* Open the three-phase lower pipe */ - for (unsigned int i = 0; i < PHASE_MAX_NUM; i++) { - APT_RegStruct *aptx = aptAddr[i]; - aptx->TC_REFC.BIT.rg_cnt_refc = dutyCnt; - aptx->TC_REFD.BIT.rg_cnt_refd = dutyCnt; - } -} - -/** - * @brief Check systerm cmd start signal. - * @param mtrCtrl The motor control handle. - * @param aptAddr Three-phase APT address pointer. - * @param statusReg System status. - * @param stateMachine Motor Control Status. - * @retval None. - */ -static void CheckSysCmdStart(MtrCtrlHandle *mtrCtrl, APT_RegStruct **aptAddr, SysStatusReg *statusReg, - FsmState *stateMachine) -{ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - MCS_ASSERT_PARAM(aptAddr != NULL); - MCS_ASSERT_PARAM(statusReg != NULL); - MCS_ASSERT_PARAM(stateMachine != NULL); - /* check start cmd */ - if (SysGetCmdStart(statusReg)) { - SysRunningSet(statusReg); - SysCmdStartClr(statusReg); - mtrCtrl->sysTickCnt = 0; - *stateMachine = FSM_CAP_CHARGE; - /* Preparation for charging the bootstrap capacitor. */ - AptTurnOnLowSidePwm(aptAddr, mtrCtrl->aptMaxcntCmp); - /* Out put pwm */ - MotorPwmOutputEnable(aptAddr); - } -} - -/** - * @brief Clear historical values of all controller before start-up. - * @param mtrCtrl The motor control handle. - * @retval None. - */ -static void ClearBeforeStartup(MtrCtrlHandle *mtrCtrl) -{ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - mtrCtrl->aptMaxcntCmp = g_apt0.waveform.timerPeriod; - - /* Clear bemf parameters. */ - mtrCtrl->bemf.u = 0; - mtrCtrl->bemf.v = 0; - mtrCtrl->bemf.w = 0; - - /* Clear tickcnt. */ - mtrCtrl->msTickCnt = 0; - - mtrCtrl->pwmDuty = FORCE_DRAG_MINDUTY; - - /* Clear spd parameters. */ - mtrCtrl->spdEstHz = 0; - mtrCtrl->spdRefHz = 0; - - mtrCtrl->stepCtrl.phaseStep = STEP1; - - mtrCtrl->sysTickCnt = 0; - - mtrCtrl->sysVar.dragChangePhaseTime = DRAG_START_INTERVAL; - mtrCtrl->sysVar.changePhaseFlag = 0; - mtrCtrl->sysVar.firstEventFilterFlag = 0; - mtrCtrl->sysVar.stepTimeNum = 0; - mtrCtrl->sysVar.stepTimeFilterEnable = 0; - for (int i = 0; i < STEP_MAX_NUM; i++) { - mtrCtrl->sysVar.stepTime[i] = 0; - } - /* RMG CLEAR */ - RMG_Clear(&mtrCtrl->spdRmg); /* Clear the history value of speed slope control */ - /* SPDCTRL CLEAR */ - PID_Clear(&mtrCtrl->spdPi); -} - -/** - * @brief Check bootstrap capacitor charge time. - * @param mtrCtrl The motor control handle. - * @param stateMachine Motor Control Status. - * @retval None. - */ -static void CheckBootstrpCapChargeTime(MtrCtrlHandle *mtrCtrl, FsmState *stateMachine) -{ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - MCS_ASSERT_PARAM(stateMachine != NULL); - mtrCtrl->sysTickCnt++; - /* check bootstrap capacitor charge time */ - if (mtrCtrl->sysTickCnt == mtrCtrl->capChargeTickNum) { - /* Update Status. */ - *stateMachine = FSM_CLEAR; - } -} - -/** - * @brief Check over current status. - * @param statusReg System status. - * @param stateMachine Motor Control Status. - * @retval None. - */ -static void CheckOverCurrentState(SysStatusReg *statusReg, FsmState *stateMachine) -{ - MCS_ASSERT_PARAM(statusReg != NULL); - MCS_ASSERT_PARAM(stateMachine != NULL); - /* check systerm error status */ - if (SysIsError(statusReg) == false) { - *stateMachine = FSM_IDLE; - } -} - -/** - * @brief Deceleration control phase before stop. - * @param mtrCtrl The motor control handle. - * @param statusReg Motor Control Status. - * @param targetSpd Deceleration target value. - * @param switchSpd Switch to stop state speed. - * @retval None. - */ -static void DecelerateSpeed(MtrCtrlHandle *mtrCtrl, FsmState *stateMachine, float targetSpd, float switchSpd) -{ - /* Reduce speed before stop. */ - mtrCtrl->spdRefHz = RMG_Exec(&mtrCtrl->spdRmg, targetSpd); - mtrCtrl->spdPi.error = mtrCtrl->spdRefHz - mtrCtrl->spdEstHz; - /* Speed loop control */ - mtrCtrl->pwmDuty = PI_Exec(&mtrCtrl->spdPi); - if (mtrCtrl->spdRefHz <= switchSpd) { /* Maximum speed for switching to the FSM_STOP state */ - *stateMachine = FSM_STOP; - } -} - -/** - * @brief System timer tick task. - * @param mtrCtrl The motor control handle. - * @param aptAddr Three-phase APT address pointer. - * @retval None. - */ -static void TSK_SystickIsr(MtrCtrlHandle *mtrCtrl, APT_RegStruct **aptAddr) -{ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - MCS_ASSERT_PARAM(aptAddr != NULL); - SysStatusReg *statusReg = &mtrCtrl->statusReg; - volatile FsmState *stateMachine = &mtrCtrl->stateMachine; - mtrCtrl->msTickCnt++; - /* Pre-processing of motor status. */ - MotorStatePerProc(statusReg, stateMachine); - /* statemachine */ - switch (*stateMachine) { - case FSM_IDLE: - CheckSysCmdStart(mtrCtrl, aptAddr, statusReg, (FsmState *)stateMachine); - break; - case FSM_CAP_CHARGE: - /* Bootstrap Capacitor Charging Timing */ - CheckBootstrpCapChargeTime(mtrCtrl, (FsmState *)stateMachine); - break; - case FSM_CLEAR: - /* Clearing control parameters. */ - ClearBeforeStartup(mtrCtrl); - /* Rotor alignment. */ - ForceAlign(mtrCtrl); - *stateMachine = FSM_STARTUP; - break; - case FSM_STARTUP: - /* Forced drag. */ - break; - case FSM_RUN: - /* Speed ramp control */ - mtrCtrl->spdRefHz = RMG_Exec(&mtrCtrl->spdRmg, mtrCtrl->spdCmdHz); - mtrCtrl->spdPi.error = mtrCtrl->spdRefHz - mtrCtrl->spdEstHz; - /* Speed loop control */ - mtrCtrl->pwmDuty = PI_Exec(&mtrCtrl->spdPi); - break; - case FSM_WAIT_STOP: - /* 5.0f : maximum speed for switching to the FSM_STOP state */ - DecelerateSpeed(mtrCtrl, (FsmState *)stateMachine, 0.0f, 5.0f); - break; - case FSM_STOP: - MotorPwmOutputDisable(aptAddr); - SysRunningClr(statusReg); - *stateMachine = FSM_IDLE; - break; - case FSM_FAULT: - /* Overcurrent state */ - CheckOverCurrentState(statusReg, (FsmState *)stateMachine); - break; - default: - break; - } -} - -/** - * @brief Overcurrent protection. - * @param aptHandle The apt Handle. - * @retval None. - */ -void MotorSysErrCallback(void *aptHandle) -{ - /* Overcurrent protection callback function. */ - BASE_FUNC_UNUSED(aptHandle); - MotorPwmOutputDisable(g_aptCp); - DCL_APT_ClearOutCtrlEventFlag((APT_RegStruct *)g_aptCp[PHASE_U], APT_OC_COMBINE_EVENT_A1); - DCL_APT_ClearOutCtrlEventFlag((APT_RegStruct *)g_aptCp[PHASE_V], APT_OC_COMBINE_EVENT_A1); - DCL_APT_ClearOutCtrlEventFlag((APT_RegStruct *)g_aptCp[PHASE_W], APT_OC_COMBINE_EVENT_A1); - /* Status setting error */ - SysErrorSet(&g_mc.statusReg); - DBG_PRINTF("APT error! \r\n"); - HAL_GPIO_SetValue(&g_gpio2, GPIO_PIN_3, GPIO_LOW_LEVEL); -} - -/** - * @brief Motor blockage protection function. - * @retval None. - */ -static void MotorBlockageProtect(void) -{ - unsigned int currentTick = DCL_SYSTICK_GetTick(); /* Get the current tick value. */ - unsigned int intervalTick = (currentTick >= g_mc.sysVar.lastZeroPoint) - ? currentTick - g_mc.sysVar.lastZeroPoint - : SYSTICK_MAX_VALUE - g_mc.sysVar.lastZeroPoint + currentTick + 1; - if (intervalTick > SYSTICK_GetCRGHZ()) { - MotorPwmOutputDisable(g_aptCp); - SysErrorSet(&g_mc.statusReg); - g_mc.spdEstHz = 0; - return; - } -} - -/** - * @brief Carrier Interruption. - * @param aptHandle The apt Handle. - * @retval None. - */ -void MotorCarrierProcessCallback(void *aptHandle) -{ - BASE_FUNC_UNUSED(aptHandle); - /* USER CODE BEGIN APT0_TIMER_INTERRUPT */ - MCS_CarrierProcess(&g_mc); - if (g_mc.stateMachine == FSM_RUN || g_mc.stateMachine == FSM_WAIT_STOP) { - MotorBlockageProtect(); - } - /* USER CODE END APT0_TIMER_INTERRUPT */ -} - -/** - * @brief Change phase delay callback function. - * @param handle The TIMER1 Handle. - * @retval None. - */ -void MotorStatemachineCallBack(void *handle) -{ - /* TIMER1CallbackFunction */ - BASE_FUNC_UNUSED(handle); - TSK_SystickIsr(&g_mc, g_aptCp); -} - -/** - * @brief Check Potentiometer Value and adjust speed. - * @param None. - * @retval None. - */ -static void AdjustSpeedFunction(void) -{ - static unsigned int potentiomitorAdcValue = 0; - static float spdCmdHz = 0; - static float spdCmdHzLast = SDP_MAX_VALUE; - HAL_ADC_SoftTrigSample(&ADC_HANDLE, ADC_SOC_NUM9); /* Get the speed adjustment resistance. */ - potentiomitorAdcValue = HAL_ADC_GetConvResult(&ADC_HANDLE, ADC_SOC_NUM9); - /* 4045.0 is adc sample max value of potentiomitor */ - spdCmdHz = (float)potentiomitorAdcValue / 4045.0 * SDP_MAX_VALUE; - if (spdCmdHz < SDP_MIN_VALUE) { /* Speed protection. */ - spdCmdHz = SDP_MIN_VALUE; - } - if (spdCmdHz > SDP_MAX_VALUE) { - spdCmdHz = SDP_MAX_VALUE; - } - float delta = spdCmdHzLast > spdCmdHz ? (spdCmdHzLast - spdCmdHz) : (spdCmdHz - spdCmdHzLast); - /* 1.0 : If the speed fluctuation is less than 1.0Hz, no change is made. */ - if (delta < 1.0) { - return; - } - spdCmdHzLast = spdCmdHz; - g_mc.spdCmdHz = spdCmdHz; -} - -/** - * @brief Config the KEY func. - * @param handle The GPIO handle. - * @retval None. - */ -static KEY_State Key_StateRead(GPIO_Handle *handle) -{ - if (HAL_GPIO_GetPinValue(handle, handle->pins) == 0) { - BASE_FUNC_DELAY_MS(30); /* delay 30ms for deshake */ - if (HAL_GPIO_GetPinValue(handle, handle->pins) == 0) { - while (HAL_GPIO_GetPinValue(handle, handle->pins) == 0) { - } - return KEY_DOWN; - } - } - return KEY_UP; -} - -/** - * @brief Control motor start and stop state by key func. - * @param param The GPIO handle. - * @retval None. - */ -void MotorStartStopKeyCallback(void *param) -{ - GPIO_Handle *handle = (GPIO_Handle *)param; - if (Key_StateRead(handle) == KEY_DOWN) { - if (SysIsRunning(&g_mc.statusReg)) { /* stop apt output, motor is off status */ - SysCmdStopSet(&g_mc.statusReg); - } else { /* start apt output, motor is on status */ - HAL_APT_StartModule(RUN_APT0 | RUN_APT1 | RUN_APT2); - SysCmdStartSet(&g_mc.statusReg); - } - } -} - -/** - * @brief User application entry. - * @retval BSP_OK. - */ -int MotorMainProcess(void) -{ - SystemInit(); - /* System Initialization. */ - unsigned int tickNum100Ms = 200; /* 100ms tick */ - static unsigned int tickCnt100Ms = 0; - unsigned int tickNum500Ms = 1000; /* 500ms tick */ - static unsigned int tickCnt500Ms = 0; - - HAL_TIMER_Start(&g_timer1); - /* Disable PWM output before startup. */ - MotorPwmOutputDisable(g_aptCp); - InitSoftware(); - /* Start the PWM clock. */ - HAL_APT_StartModule(RUN_APT0 | RUN_APT1 | RUN_APT2); - - while (true) { - if (g_mc.msTickCnt - tickCnt100Ms >= tickNum100Ms) { - tickCnt100Ms = g_mc.msTickCnt; - /* User Code 100ms Event */ - AdjustSpeedFunction(); - } - if (g_mc.msTickCnt - tickCnt500Ms >= tickNum500Ms) { - if (SysIsError(&g_mc.statusReg) == true) { - break; - } - /* The LED blinks when no status is not error. */ - HAL_GPIO_TogglePin(&SYS_LED_HANDLE, SYS_LED_PIN); - tickCnt500Ms = g_mc.msTickCnt; - } - } - return 0; -} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/application/mcs_motor_process.h b/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/application/mcs_motor_process.h deleted file mode 100644 index a2fdeb2c..00000000 --- a/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/application/mcs_motor_process.h +++ /dev/null @@ -1,47 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_motor_process.h - * @author MCU Algorithm Team - * @brief This file provides motor sample functions declaration for ECBMCU201MPC board. - */ -#ifndef McuMagicTag_MCS_MOTOR_PROCESS_H -#define McuMagicTag_MCS_MOTOR_PROCESS_H - -#include "main.h" - -/** - * @brief phase sequence. - */ -typedef enum { - PHASE_U = 0, - PHASE_V, - PHASE_W, - PHASE_MAX_NUM -} MCS_PhaseEnum; - -/** - * @brief key state. - */ -typedef enum { - KEY_DOWN = 0, - KEY_UP = 1, -} KEY_State; - -int MotorMainProcess(void); - -#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/application/mcs_user_config.h b/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/application/mcs_user_config.h deleted file mode 100644 index f193532a..00000000 --- a/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/application/mcs_user_config.h +++ /dev/null @@ -1,94 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_config.h - * @author MCU Algorithm Team - * @brief This file provides config macros for ECBMCU201MPC app. - */ -#ifndef McuMagicTag_MCS_CONFIG_H -#define McuMagicTag_MCS_CONFIG_H - -#if defined(CHIP_3061MNPICA) || defined(CHIP_3061MNNICA) || defined(CHIP_3061MNPIC8) || defined(CHIP_3061MNNIC8) -#define ADC_U_SOC_NUM ADC_SOC_NUM2 -#define ADC_V_SOC_NUM ADC_SOC_NUM5 -#define ADC_W_SOC_NUM ADC_SOC_NUM6 -#define ADC_SPD_ADJ_SOC_NUM ADC_SOC_NUM9 -#define ADC_HANDLE g_adc0 -#endif - -#if defined (CHIP_3066MNPIRH) || defined (CHIP_3065PNPIRH) || defined (CHIP_3065PNPIRE) || defined (CHIP_3065PNPIRA) -#define ADC_U_SOC_NUM ADC_SOC_NUM3 -#define ADC_V_SOC_NUM ADC_SOC_NUM5 -#define ADC_W_SOC_NUM ADC_SOC_NUM7 -#define ADC_SPD_ADJ_SOC_NUM ADC_SOC_NUM9 - -#define ADC_HANDLE g_adc2 -#endif - -#define APT_PWM_FREQ 12000 /* PWM frequency 12K Hz. */ -#define CTRL_CURR_PERIOD (1.0f / APT_PWM_FREQ) /* carrier ISR period (S) */ -#define CTRL_CURE_PERIOD_US 83 - -#define SYSTICK_PERIOD_US 500u /* systick period. unit : us */ - -#define INV_CAP_CHARGE_MS 3u /* Bootstrap capacitor charge time. unit : ms */ - -#define CTRL_SYSTICK_PERIOD 0.0005f /* systick control period, 500us */ - -#define USER_SPD_SLOPE 60.0f /* slope of velocity change */ - -#define IN_VOLTAGE_BUS 24 /* Motor input voltage */ -#define POLES 14 /* 电机磁极对数 */ -#define MATH_PI 3.14 /* Pi */ -#define MOTOR_K 1.62 /* 转矩系数 */ - -#define RAMP_STP 60 /* Slope of forced draging acceleration */ -#define RAMP_DUTY_PWM 0.5 /* 1.0% */ - -#define FORCE_DRAG_MINDUTY 15 /* APT minimum duty cycle during forced drag */ -#define FORCE_DRAG_MAXDUTY 20 /* APT maximum duty cycle during forced drag */ - -#define DRAG_START_INTERVAL 1000 /* Force drag change phase time */ -#define DRAG_STOP_INTERVAL 400 /* Force drag change phase time */ - -/* Parameters of the motor in the RUN */ -#define FILTER_COUNT 3 /* Filter Times */ -#define PHASE_OFFSET 9000 /* Phase delay due to filtering */ - -#define APT_DUTYLIMIT_MAX 99.9 /* 输出APT的最大占空比 */ -#define APT_DUTYLIMIT_MIN 8.0 /* Minimum duty cycle of the output APT */ - -#define BRIDGE_CTR_APT_U APT0 /* APT address that controls the U phase */ -#define BRIDGE_CTR_APT_V APT1 /* APT address that controls the V phase */ -#define BRIDGE_CTR_APT_W APT2 /* APT address that controls the W phase */ - -#define SDP_MAX_VALUE 58.8 /* 最大相位变化频率 */ -#define SDP_MIN_VALUE 15.0 /* Minimum change phase frequency */ -#define SDP_TARGET_VALUE 58.8 /* 目标相位变化频率 */ - -/* SPD PID Param */ -#define SPD_PID_KP 0.9 /* P parameter of PID control */ -#define SPD_PID_KI 40 /* I parameter of PID control */ -#define SPD_PID_TS 0.0005 /* TS parameter of PID control cycle */ - -#define OP_TO_CL_INTERGRAL 8.0 /* Open-loop switching closed-loop integral term to prevent sudden current change. */ - -#define VOL_DIVIDER_COEFFICIENT 0.09091f /* Division coefficient of the zero-crossing detection sampling circuit */ - -#define SPD_FILTER_FC 30 - -#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/common/mcs_carrier.c b/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/common/mcs_carrier.c deleted file mode 100644 index 7779fbef..00000000 --- a/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/common/mcs_carrier.c +++ /dev/null @@ -1,224 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_carrier.c - * @author MCU Algorithm Team - * @brief This file provides carrier interrupt application for motor control. - */ - -#include "mcs_carrier.h" -#include "mcs_user_config.h" -#include "mcs_assert.h" -#include "debug.h" - -#define S_TO_US 1000000 -#define S_TO_SYSTICK 100000000 - -/** - * @brief Sets the duty cycle of the H-bridge APT. - * @param aptHandle APT module handle. - * @param duty PWM duty. Range: 0.1 ~ 99.9. - * @retval None. - */ -void MCS_SetCtrAptDuty(MtrCtrlHandle *mtrCtrl, unsigned int duty) -{ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - MCS_ASSERT_PARAM(duty > 0); - /* Set pwm duty of uvw pahse */ - HAL_APT_SetPWMDutyByNumber(mtrCtrl->stepCtrl.controlApt.u, duty); - HAL_APT_SetPWMDutyByNumber(mtrCtrl->stepCtrl.controlApt.v, duty); - HAL_APT_SetPWMDutyByNumber(mtrCtrl->stepCtrl.controlApt.w, duty); -} - -/** - * @brief Strong drag start. - * @param mtrCtrl The motor control handle. - * @retval None. - */ -static void ForceDragAcc(MtrCtrlHandle *mtrCtrl) -{ - /* Verifying Parameters. */ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - unsigned int voltageDValue; - unsigned int dragChangeFreq; - unsigned int timerperiod; - unsigned int freqLast; - unsigned int aptCountsOneUs; - - mtrCtrl->sysVar.accTimeCnt++; - if (mtrCtrl->sysVar.accTimeCnt < mtrCtrl->sysVar.dragChangePhaseTime) { - return; - } - - mtrCtrl->sysVar.accTimeCnt = 0; - /* Step 1: Calculate the voltage difference. */ - voltageDValue = IN_VOLTAGE_BUS * RAMP_DUTY_PWM; - /* Step 2: Calculate the commutation frequency. */ - freqLast = S_TO_US / (mtrCtrl->sysVar.dragChangePhaseTime * CTRL_CURE_PERIOD_US); - dragChangeFreq = voltageDValue * STEP_MAX_NUM * POLES / (MOTOR_K * 2 * MATH_PI) + freqLast; /* 2*PI = 360° */ - /* Step 3: Convert the commutation frequency to the commutation count value. */ - timerperiod = mtrCtrl->stepCtrl.controlApt.u->waveform.timerPeriod * CTRL_CURE_PERIOD_US; - mtrCtrl->sysVar.dragChangePhaseTime = S_TO_US / (timerperiod * dragChangeFreq); - - /* Determine whether the change phase speed has reached the speed of stopping forced drag. */ - if (mtrCtrl->sysVar.dragChangePhaseTime < DRAG_STOP_INTERVAL) { - mtrCtrl->sysVar.dragChangePhaseTime = DRAG_STOP_INTERVAL; - mtrCtrl->sysVar.accTimeCnt = 0; - mtrCtrl->sysVar.bemfFilterCnt = 0; - mtrCtrl->stateMachine = FSM_RUN; - mtrCtrl->sysVar.lastZeroPoint = DCL_SYSTICK_GetTick(); - mtrCtrl->spdPi.integral = OP_TO_CL_INTERGRAL; - } - - /* Shorten the time interval of forced drag change phase. */ - mtrCtrl->pwmDuty += RAMP_DUTY_PWM; - aptCountsOneUs = HAL_CRG_GetIpFreq(APT0_BASE) / S_TO_US; - mtrCtrl->sysVar.waitTime = (mtrCtrl->sysVar.dragChangePhaseTime * CTRL_CURE_PERIOD_US * aptCountsOneUs) >> 1; - /* Change phase time is 2 the waiting time. */ - mtrCtrl->spdRefHz = (float)(HAL_CRG_GetIpFreq(SYSTICK_BASE) / (mtrCtrl->sysVar.waitTime * 2)) / STEP_MAX_NUM; - mtrCtrl->spdEstHz = mtrCtrl->spdRefHz; - mtrCtrl->spdRmg.yLast = mtrCtrl->spdRefHz; - mtrCtrl->stepCtrl.phaseStep = (mtrCtrl->stepCtrl.phaseStep + 1) % STEP_MAX_NUM; - SixStepPwm(&mtrCtrl->stepCtrl); - /* Limiting the PWM Duty Cycle Range. */ - if (mtrCtrl->pwmDuty > FORCE_DRAG_MAXDUTY) { - mtrCtrl->pwmDuty = FORCE_DRAG_MAXDUTY; - } - if (mtrCtrl->pwmDuty < FORCE_DRAG_MINDUTY) { - mtrCtrl->pwmDuty = FORCE_DRAG_MINDUTY; - } - MCS_SetCtrAptDuty(mtrCtrl, mtrCtrl->pwmDuty); -} - -/** - * @brief Zero-crossing filter. - * @param mtrCtrl The motor control handle. - * @retval None. - */ -static void BemfZeroSampleFilter(MtrCtrlHandle *mtrCtrl) -{ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - mtrCtrl->sysVar.bemfFilterCnt++; - /* Filter the number of samples. */ - if (mtrCtrl->sysVar.bemfFilterCnt >= FILTER_COUNT) { - mtrCtrl->sysVar.bemfFilterCnt = 0; - unsigned int currentTick = DCL_SYSTICK_GetTick(); - if (mtrCtrl->sysVar.firstEventFilterFlag == 0) { - mtrCtrl->sysVar.lastZeroPoint = currentTick; - mtrCtrl->sysVar.changePhaseFlag = 1; - mtrCtrl->sysVar.firstEventFilterFlag = 1; - return; - } - /* Calculate the time interval between the last zero crossing. */ - mtrCtrl->sysVar.stepTime[mtrCtrl->stepCtrl.phaseStep] = currentTick >= mtrCtrl->sysVar.lastZeroPoint - ? currentTick - mtrCtrl->sysVar.lastZeroPoint - : SYSTICK_MAX_VALUE - mtrCtrl->sysVar.lastZeroPoint + currentTick + 1; - mtrCtrl->sysVar.stepTimeNum++; - /* Wait time is half the interval. */ - mtrCtrl->sysVar.waitTime = mtrCtrl->sysVar.stepTime[mtrCtrl->stepCtrl.phaseStep] >> 1; - mtrCtrl->sysVar.waitTime -= PHASE_OFFSET; - mtrCtrl->sysVar.lastZeroPoint = currentTick; - /* Update flag. */ - mtrCtrl->sysVar.changePhaseFlag = 1; - } -} - -/** - * @brief Back EMF zero crossing check. - * @param mtrCtrl The motor control handle. - * @retval None. - */ -static void BemfZeroCheck(MtrCtrlHandle *mtrCtrl) -{ - /* The step corresponds to different conditional judgments. */ - unsigned int bemfCheckTable[STEP_MAX_NUM] = {mtrCtrl->bemf.w < mtrCtrl->zeroPoint ? 1 : 0, - mtrCtrl->bemf.v > mtrCtrl->zeroPoint ? 1 : 0, - mtrCtrl->bemf.u < mtrCtrl->zeroPoint ? 1 : 0, - mtrCtrl->bemf.w > mtrCtrl->zeroPoint ? 1 : 0, - mtrCtrl->bemf.v < mtrCtrl->zeroPoint ? 1 : 0, - mtrCtrl->bemf.u > mtrCtrl->zeroPoint ? 1 : 0}; - - if (bemfCheckTable[mtrCtrl->stepCtrl.phaseStep] == 1) { - BemfZeroSampleFilter(mtrCtrl); - } -} - -/** - * @brief Change phase and speed estimation. - * @param mtrCtrl The motor control handle. - * @retval None. - */ -static void ChangePhase(MtrCtrlHandle *mtrCtrl) -{ - unsigned int currentTick = DCL_SYSTICK_GetTick(); /* Get the current tick value. */ - unsigned int intervalTick = (currentTick >= mtrCtrl->sysVar.lastZeroPoint) - ? currentTick - mtrCtrl->sysVar.lastZeroPoint - : SYSTICK_MAX_VALUE - mtrCtrl->sysVar.lastZeroPoint + currentTick + 1; - /* Start average filtering when the number of recorded data reaches six. */ - if (mtrCtrl->sysVar.stepTimeNum >= STEP_MAX_NUM) { - mtrCtrl->sysVar.stepTimeNum = 0; - mtrCtrl->sysVar.stepTimeFilterEnable = 1; - } - /* If the accumulated time is greater than the waiting commutation time, commutation is performed. */ - if (intervalTick > mtrCtrl->sysVar.waitTime) { - if (mtrCtrl->sysVar.stepTimeFilterEnable) { /* 6-step commutation time averaging if filtering is enabled. */ - unsigned int totalTime = mtrCtrl->sysVar.stepTime[STEP1] + mtrCtrl->sysVar.stepTime[STEP2] - + mtrCtrl->sysVar.stepTime[STEP3] + mtrCtrl->sysVar.stepTime[STEP4] - + mtrCtrl->sysVar.stepTime[STEP5] + mtrCtrl->sysVar.stepTime[STEP6]; - mtrCtrl->spdEstHz = (float)HAL_CRG_GetIpFreq(SYSTICK_BASE) / totalTime; - } else { - mtrCtrl->spdEstHz = (float)HAL_CRG_GetIpFreq(SYSTICK_BASE) / (mtrCtrl->sysVar.waitTime << 1) / STEP_MAX_NUM; - } - /* Change phase. */ - mtrCtrl->stepCtrl.phaseStep = (mtrCtrl->stepCtrl.phaseStep + 1) % STEP_MAX_NUM; - SixStepPwm(&mtrCtrl->stepCtrl); - mtrCtrl->sysVar.changePhaseFlag = 0; - } -} - -/** - * @brief Zero-crossing detection and change phase - * @param mtrCtrl The motor control handle. - * @retval None. - */ -void MCS_CarrierProcess(MtrCtrlHandle *mtrCtrl) -{ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - /* Offset value of the calibration value of the three-phase current */ - - switch (mtrCtrl->stateMachine) { - case FSM_STARTUP: - ForceDragAcc(mtrCtrl); /* Forced drag. */ - break; - - case FSM_RUN: - case FSM_WAIT_STOP: - mtrCtrl->readBemfUVW(&mtrCtrl->bemf); - MCS_SetCtrAptDuty(mtrCtrl, mtrCtrl->pwmDuty); - if (mtrCtrl->sysVar.changePhaseFlag) { - /* Change phase process. */ - ChangePhase(mtrCtrl); - } else { - /* Zero-crossing detection procedure. */ - BemfZeroCheck(mtrCtrl); - } - break; - - default: - break; - } -} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/common/mcs_carrier.h b/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/common/mcs_carrier.h deleted file mode 100644 index 05fc2666..00000000 --- a/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/common/mcs_carrier.h +++ /dev/null @@ -1,91 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_carrier.h - * @author MCU Algorithm Team - * @brief This file provides functions declaration for carrier interrupt processing function. - */ -#ifndef McuMagicTag_MCS_CARRIER_H -#define McuMagicTag_MCS_CARRIER_H - -#include "mcs_status.h" -#include "mcs_six_step.h" -#include "mcs_ramp_mgmt.h" -#include "mcs_spd_ctrl.h" -#include "mcs_fsm.h" - -/** - * @brief Three-phase static coordinate frame variable. - */ -typedef struct { - unsigned int u; /**< Component u of the three-phase static coordinate frame variable. */ - unsigned int v; /**< Component v of the three-phase static coordinate frame variable. */ - unsigned int w; /**< Component w of the three-phase static coordinate frame variable. */ -} UVWBemf; - -typedef void (*ReadBemf)(UVWBemf *bemfUVW); - -/** - * @brief The definition of the systematic global variables - */ -typedef struct { - unsigned char changePhaseFlag; /**< Flag indicating that delay change phase is required */ - unsigned char firstEventFilterFlag; /**< First zero-crossing event filtering flag. */ - unsigned int dragChangePhaseTime; /**< Interval for forced drag acceleration */ - unsigned int accTimeCnt; /**< Acceleration time count */ - - unsigned int bemfFilterCnt; /**< Number of sample filtering times for zero-crossing detection. */ - - unsigned int lastZeroPoint; /**< Time of the last zero point */ - unsigned int waitTime; /**< Time to wait for change phase */ - unsigned int stepTime[STEP_MAX_NUM]; /**< Record the time spent on each step */ - unsigned int stepTimeNum; /**< Number of data records in the steptime array. */ - unsigned int stepTimeFilterEnable; -} SysVariable; - -typedef struct { - volatile float spdCmdHz; /**< Set target change phase frequency */ - float spdRefHz; /**< Command values after speed ramp management */ - float spdEstHz; /**< Actual change phase frequency of feedback */ - float pwmDuty; /**< APT duty cycle */ - unsigned int zeroPoint; /**< Adc value of zero point */ - - unsigned short aptMaxcntCmp; /**< Apt Maximum Comparison Count */ - - unsigned short sysTickCnt; /**< System Timer Tick Count */ - unsigned short capChargeTickNum; /**< Bootstrap Capacitor Charge Tick Count */ - unsigned short msTickNum; /**< Number of ticks corresponding to 1 ms */ - volatile unsigned int msTickCnt; /**< Millisecond-level counter, which can be used in 1-ms and 5-ms tasks. */ - - RMG_Handle spdRmg; /**< Ramp management struct for the speed controller input reference */ - PID_Handle spdPi; /**< PI controller struct in the speed controller. */ - - UVWBemf bemf; /**< Three-phase back electromotive force */ - SixStepHandle stepCtrl; /**< Control structure of six-step square wave */ - - SysVariable sysVar; /**< System Variables */ - SysStatusReg statusReg; /**< System Status */ - FsmState stateMachine; /**< BLDC Motor Control State Machine */ - - ReadBemf readBemfUVW; /**< Function interface for obtaining the three-phase electromotive force */ -} MtrCtrlHandle; - -void MCS_CarrierProcess(MtrCtrlHandle *mtrCtrl); - -void MCS_SetCtrAptDuty(MtrCtrlHandle *mtrCtrl, unsigned int duty); - -#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/common/mcs_fsm.h b/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/common/mcs_fsm.h deleted file mode 100644 index b3c79ea2..00000000 --- a/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/common/mcs_fsm.h +++ /dev/null @@ -1,55 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_fsm.h - * @author MCU Algorithm Team - * @brief This file provides the definition of finite statemachine (FSM). - */ - -#ifndef McuMagicTag_MCS_FSM_H -#define McuMagicTag_MCS_FSM_H - -/** - * @brief motor control FSM state define. - * @details motor control FSM state type: - * + FSM_IDLE -- IDLE state, system startup default. - * + FSM_OFFSET_CALIB -- Self calibrate, for ADC init. - * + FSM_CAP_CHARGE -- IPM cap charge. - * + FSM_CLEAR -- Clear before every run. - * + FSM_LOCATE -- Rotor position pre-locate. - * + FSM_STARTUP -- Start up. - * + FSM_SWITCH -- Transition state, control from open to closed loop. - * + FSM_RUN -- Normal running state. - * + FSM_WAIT_STOP -- Wait stopping. - * + FSM_STOP -- Normal stop. - * + FSM_FAULT -- Fault state, waiting for user process. - */ -typedef enum { - FSM_IDLE = 0, - FSM_OFFSET_CALIB, - FSM_CAP_CHARGE, - FSM_CLEAR, - FSM_LOCATE, - FSM_STARTUP, - FSM_SWITCH, - FSM_RUN, - FSM_WAIT_STOP, - FSM_STOP, - FSM_FAULT -} FsmState; - -#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/func/mcs_six_step.c b/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/func/mcs_six_step.c deleted file mode 100644 index c86e609c..00000000 --- a/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/func/mcs_six_step.c +++ /dev/null @@ -1,126 +0,0 @@ -/** - * @ Copyright (c) HiSilicon (Shanghai) Technologies Co., Ltd. 2022-2023. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_six_step.c - * @author MCU Algorithm Team - * @brief This file provides functions to control the H bridge. - */ - -#include "mcs_assert.h" -#include "mcs_six_step.h" - -/* Mode for disabling or enabling software-based forced output */ -#define APT_PWM_MODE 0 -#define APT_FRC_MODE 1 - -#define PWM_SIGNAL 1 - -#define FRC_LOW APT_OUT_CTRL_ACTION_LOW -#define FRC_HIGH APT_OUT_CTRL_ACTION_HIGH - -/** - * @brief Force PWM output by APT - * @param APTx The APT to use - * @param PwmAout Force to be high or low - * @param PwmAenble PWMA signal software force mode - * @param PwmBout Force to be high or low - * @param PwmBenble PWMB signal software force mode - */ -static void APT_ForcePwmByApt(APT_RegStruct *aptx, APT_Act actMode) -{ - MCS_ASSERT_PARAM(aptx != NULL); - switch (actMode) { - case APT_CHA_PWM_CHB_LOW: - /* Channel A: 0 means not force output enable, channel A output PWM. */ - DCL_APT_DisableSwContPWMAction(aptx, APT_PWM_CHANNEL_A); - DCL_APT_SetSwContPWMAction(aptx, APT_PWM_CHANNEL_A, APT_PWM_CONTINUOUS_ACTION_LOW); - /* Channel B: 1 means force output enable. */ - DCL_APT_EnableSwContPWMAction(aptx, APT_PWM_CHANNEL_B); - /* Channel B: 2 means channel B force output LOW due to the A_H_B_L invert. */ - DCL_APT_SetSwContPWMAction(aptx, APT_PWM_CHANNEL_B, APT_PWM_CONTINUOUS_ACTION_HIGH); - break; - case APT_CHA_LOW_CHB_HIGH: - /* Channel A: 1 means force output enable. */ - /* Channel A: 1 means channel A force output LOW. */ - DCL_APT_EnableSwContPWMAction(aptx, APT_PWM_CHANNEL_A); - DCL_APT_SetSwContPWMAction(aptx, APT_PWM_CHANNEL_A, APT_PWM_CONTINUOUS_ACTION_LOW); - /* Channel B: 1 means force output enable. */ - /* Channel B: 2 means channel B force output HIGH. */ - DCL_APT_EnableSwContPWMAction(aptx, APT_PWM_CHANNEL_B); - DCL_APT_SetSwContPWMAction(aptx, APT_PWM_CHANNEL_B, APT_PWM_CONTINUOUS_ACTION_HIGH); - break; - case APT_CHA_LOW_CHB_LOW: - /* Channel A: 1 means force output enable. */ - /* Channel A: 1 means channel A force output LOW. */ - DCL_APT_EnableSwContPWMAction(aptx, APT_PWM_CHANNEL_A); - DCL_APT_SetSwContPWMAction(aptx, APT_PWM_CHANNEL_A, APT_PWM_CONTINUOUS_ACTION_LOW); - /* Channel B: 1 means force output enable. */ - /* Channel B: 1 means channel A force output HIGH due to the A_H_B_L invert. */ - DCL_APT_EnableSwContPWMAction(aptx, APT_PWM_CHANNEL_B); - DCL_APT_SetSwContPWMAction(aptx, APT_PWM_CHANNEL_B, APT_PWM_CONTINUOUS_ACTION_LOW); - break; - default: - break; - } -} - -/** - * @brief Output six-step square wave. - * @param handle The SixStepHandle. - * @retval None. - */ -void SixStepPwm(const SixStepHandle *handle) -{ - MCS_ASSERT_PARAM(handle != NULL); - APT_RegStruct *aptU = handle->controlApt.u->baseAddress; /* Get apt u\v\w baseaddress. */ - APT_RegStruct *aptV = handle->controlApt.v->baseAddress; - APT_RegStruct *aptW = handle->controlApt.w->baseAddress; - switch (handle->phaseStep) { - case STEP1: /* U+, V- */ - APT_ForcePwmByApt(aptU, APT_CHA_PWM_CHB_LOW); - APT_ForcePwmByApt(aptV, APT_CHA_LOW_CHB_LOW); - APT_ForcePwmByApt(aptW, APT_CHA_LOW_CHB_HIGH); - break; - case STEP2: /* U+, W- */ - APT_ForcePwmByApt(aptU, APT_CHA_PWM_CHB_LOW); - APT_ForcePwmByApt(aptV, APT_CHA_LOW_CHB_HIGH); - APT_ForcePwmByApt(aptW, APT_CHA_LOW_CHB_LOW); - break; - case STEP3: /* V+, W- */ - APT_ForcePwmByApt(aptU, APT_CHA_LOW_CHB_HIGH); - APT_ForcePwmByApt(aptV, APT_CHA_PWM_CHB_LOW); - APT_ForcePwmByApt(aptW, APT_CHA_LOW_CHB_LOW); - break; - case STEP4: /* V+, U- */ - APT_ForcePwmByApt(aptU, APT_CHA_LOW_CHB_LOW); - APT_ForcePwmByApt(aptV, APT_CHA_PWM_CHB_LOW); - APT_ForcePwmByApt(aptW, APT_CHA_LOW_CHB_HIGH); - break; - case STEP5: /* W+, U- */ - APT_ForcePwmByApt(aptU, APT_CHA_LOW_CHB_LOW); - APT_ForcePwmByApt(aptV, APT_CHA_LOW_CHB_HIGH); - APT_ForcePwmByApt(aptW, APT_CHA_PWM_CHB_LOW); - break; - case STEP6: /* W+, V- */ - APT_ForcePwmByApt(aptU, APT_CHA_LOW_CHB_HIGH); - APT_ForcePwmByApt(aptV, APT_CHA_LOW_CHB_LOW); - APT_ForcePwmByApt(aptW, APT_CHA_PWM_CHB_LOW); - break; - default: - break; - } -} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/func/mcs_six_step.h b/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/func/mcs_six_step.h deleted file mode 100644 index 08198c4c..00000000 --- a/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/func/mcs_six_step.h +++ /dev/null @@ -1,79 +0,0 @@ -/** - * @ Copyright (c) HiSilicon (Shanghai) Technologies Co., Ltd. 2022-2023. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_six_step.c - * @author MCU Algorithm Team - * @brief The header file contains the following declaration: - * + StepEnum enum definition. - * + PhaseEnum enum definition. - * + UVW_AptReg structure definition. - * + SixStepHandle handle structure definition. - * + AptOutCombination structure definition. - * + Six Step Pwm Function And Six Step Pwm Overlap Function. - */ - -#ifndef MCS_SIX_STEP_H -#define MCS_SIX_STEP_H - -#include "apt.h" - -typedef enum { - STEP1 = 0, - STEP2, - STEP3, - STEP4, - STEP5, - STEP6, - STEP_MAX_NUM -} StepEnum; - -typedef enum { - U = 0, - V, - W, - PHASE_MAX_NUMS -} PhaseEnum; - -typedef enum { - APT_CHA_PWM_CHB_LOW, - APT_CHA_LOW_CHB_HIGH, - APT_CHA_LOW_CHB_LOW -} APT_Act; - -/** - * @brief Three-phase static coordinate frame variable. - */ -typedef struct { - APT_Handle *u; /**< Apt handle for controlling phase U. */ - APT_Handle *v; /**< Apt handle for controlling phase V. */ - APT_Handle *w; /**< Apt handle for controlling phase W. */ -} UVW_AptReg; - -typedef struct { - unsigned char phaseStep; /* Current step. */ - UVW_AptReg controlApt; /* Apt handles for controlling H bridge. */ -} SixStepHandle; - -typedef struct { - unsigned int upperout; /* High tube level */ - unsigned int upperstate; /* High tube state */ - unsigned int lowerout; /* Low tube level */ - unsigned int lowerstate; /* Low tube state */ -} AptOutCombination; - -void SixStepPwm(const SixStepHandle *handle); -#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/func/mcs_status.h b/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/func/mcs_status.h deleted file mode 100644 index d92ed312..00000000 --- a/vendor/yibaina_3061M/demo/E7050_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/func/mcs_status.h +++ /dev/null @@ -1,188 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_status.h - * @author MCU Algorithm Team - * @brief This file provides functions declaration of system status. - */ - -/* Define to prevent recursive inclusion ------------------------------------------------------- */ -#ifndef McuMagicTag_MCS_STATUS_H -#define McuMagicTag_MCS_STATUS_H - -/* Includes ------------------------------------------------------------------------------------ */ -#include "typedefs.h" -#include "mcs_assert.h" - -/* Typedef definitions ------------------------------------------------------------------------- */ -/** - * @brief System status define - */ -typedef union { - unsigned short all; - struct { - unsigned short cmdStart : 1; /**< Indicates that a start system command has been received. */ - unsigned short cmdStop : 1; /**< Indicates that a stop system command has been received. */ - unsigned short isRunning : 1; /**< Indicates that the system is running (enable signal) */ - unsigned short sysError : 1; /**< Indicates that the system reports an error. */ - unsigned short poweron : 1; /**< Indicates that the power-on initialization phase is complete. */ - unsigned short capcharge : 1; /**< Indicates that the bootstrap capacitor charging phase is complete. */ - unsigned short adczero : 1; /**< The current sampling point is reset to zero after power-on. */ - } BIT; -} SysStatusReg; - -/** - * @brief Get status of BIT cmdStart. - * @param sysStatus System status register handle. - * @retval Status of BIT cmdStart. - */ -static inline bool SysGetCmdStart(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - bool ret; - ret = (sysStatus->BIT.cmdStart == 1) ? true : false; - return ret; -} - -/** - * @brief Set BIT cmdStart. - * @param sysStatus System status register handle. - * @retval None. - */ -static inline void SysCmdStartSet(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - sysStatus->BIT.cmdStart = 1; -} - -/** - * @brief Clear BIT cmdStart. - * @param handle System status register handle. - * @retval None. - */ -static inline void SysCmdStartClr(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - sysStatus->BIT.cmdStart = 0; -} - -/** - * @brief Get status of BIT cmdStop. - * @param sysStatus System status register handle. - * @retval Status of BIT cmdStart. - */ -static inline bool SysGetCmdStop(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - bool ret; - ret = (sysStatus->BIT.cmdStop == 1) ? true : false; - return ret; -} - -/** - * @brief Set BIT cmdStop. - * @param sysStatus System status register handle. - * @retval None. - */ -static inline void SysCmdStopSet(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - sysStatus->BIT.cmdStop = 1; -} - -/** - * @brief Clear BIT cmdStop. - * @param sysStatus System status register handle. - * @retval None. - */ -static inline void SysCmdStopClr(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - sysStatus->BIT.cmdStop = 0; -} - -/** - * @brief Get status of BIT isRunning. - * @param sysStatus System status register handle. - * @retval Status of BIT isRunning. - */ -static inline bool SysIsRunning(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - bool ret; - ret = (sysStatus->BIT.isRunning == 1) ? true : false; - return ret; -} - -/** - * @brief Set BIT isRuning. - * @param sysStatus System status register handle. - * @retval None. - */ -static inline void SysRunningSet(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - sysStatus->BIT.isRunning = 1; -} - -/** - * @brief Clear BIT isRuning. - * @param sysStatus System status register handle. - * @retval None. - */ -static inline void SysRunningClr(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - sysStatus->BIT.isRunning = 0; -} - -/** - * @brief Get status of BIT sysError. - * @param sysStatus System status register handle. - * @retval Status of BIT sysError. - */ -static inline bool SysIsError(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - bool ret; - ret = (sysStatus->BIT.sysError == 1) ? true : false; - return ret; -} - -/** - * @brief Set BIT sysError. - * @param sysStatus System status register handle. - * @retval None. - */ -static inline void SysErrorSet(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - sysStatus->BIT.sysError = 1; -} - -/** - * @brief Clear BIT sysError. - * @param sysStatus System status register handle. - * @retval None. - */ -static inline void SysErrorClr(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - sysStatus->BIT.sysError = 0; -} - -#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/readme.md b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/readme.md deleted file mode 100644 index d692ed23..00000000 --- a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/readme.md +++ /dev/null @@ -1,20 +0,0 @@ -# GIM4310-10_FOC - -**【功能描述】** - -- 基于ECMCU105H/ECBMCU201MPC单板的单电机双电阻采样的无感Foc应用 - -**【环境要求】** - -- 所有单板电源改制为演示用的12V低压,电机选用GIM4310-10 -- 电机(黄绿蓝)相线分别对应功率板上U/V/W通道接口 - -**【配置方法】** - -- 将user文件夹替换原工程user文件夹即可 - -【操作说明】 - -- 点击开发板按键启动电机 - -- 开发板蓝色电位器,可以调节电机转动的快慢。(1-24HZ) \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/generatecode/feature.h b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/generatecode/feature.h deleted file mode 100644 index fe14fc27..00000000 --- a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/generatecode/feature.h +++ /dev/null @@ -1,94 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file feature.h - * @author MCU Driver Team - * @brief This file contains macro configurations related to the project. This file is generated by the IDE tool. - * @date 2025-04-17 15:09:50 - */ - -#ifndef McuMagicTag_FEATURE_H -#define McuMagicTag_FEATURE_H - -/* Macro definitions --------------------------------------------------------- */ -#define CHIP_3061MNPICA MACRO_ENABLE - -#define MACRO_ENABLE 1 -#define MACRO_DISABLE 0 - -/* Macro switch */ -#define BASE_DEFINE_USE_ASSERT MACRO_ENABLE -#ifndef FLASH_CRC_CONFIG -#define FLASH_CRC_CONFIG -#endif /* #ifndef FLASH_CRC_CONFIG */ -#define BASE_MATH_SINCOS_MIDDLE_TABLE MACRO_ENABLE /**< This macro is used to control the table type when the - BASE_MATH_GetSinCos() queries the table. When the value of - this macro is MACRO_ENABLE, the error value obtained by the - BASE_MATH_GetSinCos() is relatively small, and the return - value of the function may be greater than or less than the - actual value. When the value of this macro is MACRO_DISABLE, - the error value obtained by the BASE_MATH_GetSinCos() is - relatively large. However, in the range [0°, 180°) and - [180°, 360°), the return value of the function is either - greater than or less than the actual value. */ - -/* Peripheral module macro switch--------------------------------------------- */ -#define BOARD_DIM_NUM 1 /**< Number of dimming handle arrays. */ - -#define BOARD_KEY_NUM 10 /**< Number of key handle arrays. */ -#define BOARD_KEY_PRESS_ON GPIO_HIGH_LEVEL /**< GPIO status corresponding to long press valid. */ -#define BOARD_KEY_PRESS_OFF GPIO_LOW_LEVEL /**< GPIO status corresponding to short press valid. */ - -#define BOARD_LED_SEG_NUM 4 /**< Number of segments. */ -#define BOARD_LED_SEGMENT_ON GPIO_HIGH_LEVEL /**< GPIO level status corresponding to valid segments. */ -#define BOARD_LED_SEGMENT_OFF GPIO_LOW_LEVEL /**< GPIO level status corresponding to invalid segments. */ - -#define BOARD_MKEY_SCHEME_NUMBER BOARD_MKEY_SCHEME_NUMBER_ONE /**< Define the scheme to be adopted. */ -#define BOARD_MKEY_OUT_NUM 4 /**< Number of GPIO pins used as output during scanning. */ -#define BOARD_MKEY_IN_NUM 4 /**< Number of GPIO pins used as input during scanning. */ -#define BOARD_MKEY_OUT_PIN_VALID GPIO_LOW_LEVEL /**< GPIO level status corresponding to the valid \ - status of the output GPIO in the key matrix. */ -#define BOARD_MKEY_OUT_PIN_INVALID GPIO_HIGH_LEVEL /**< GPIO level status corresponding to the \ - invalid status of the output GPIO in the key matrix. */ -#define BOARD_MKEY_IN_PIN_VALID GPIO_LOW_LEVEL /**< Indicates the GPIO level corresponding to the \ - valid status of the input GPIO in the key matrix. */ -#define BOARD_MKEY_IN_PIN_INVALID GPIO_HIGH_LEVEL /**< Indicates the GPIO level corresponding to the \ - invalid status of the input GPIO in the key matrix. */ - -#define BOARD_PULSES_NUM 2 /**< Number of pulse handles. */ - -#define BASE_DEFINE_SLIPAVERAGE_NUM 2 /**< Sliding average array length. */ - -#define LISTNODE_MAX 20 - -#define BASE_DEFINE_DMA_QUICKSTART - -#define XTRAIL_FREQ 30000000U - -#define DBG_USE_NO_PRINTF 0U -#define DBG_USE_UART_PRINTF 1U - -#define DBG_PRINTF_USE DBG_USE_UART_PRINTF -#if (DBG_PRINTF_USE == DBG_USE_UART_PRINTF) -#define DBG_PRINTF_UART_PORT UART0 -#endif - -/* USER CODE BEGIN 0 */ -/* USER CODE 区域内代码不会被覆盖,区域外会被生成的默认代码覆盖(其余USER CODE 区域同理) */ -/* USER CODE END 0 */ - -#endif /* McuMagicTag_FEATURE_H */ \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/generatecode/main.h b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/generatecode/main.h deleted file mode 100644 index 27f92d07..00000000 --- a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/generatecode/main.h +++ /dev/null @@ -1,98 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file main.h - * @author MCU Driver Team - * @brief This file contains driver init functions. - * @date 2025-04-17 15:09:50 - */ - -/* Define to prevent recursive inclusion ------------------------------------- */ -#ifndef McuMagicTag_SYSTEM_INIT_H -#define McuMagicTag_SYSTEM_INIT_H - -#include "adc.h" -#include "adc_ex.h" -#include "acmp.h" -#include "acmp_ex.h" -#include "apt.h" -#include "uart.h" -#include "uart_ex.h" -#include "gpio.h" -#include "timer.h" -#include "timer_ex.h" -#include "pga.h" -#include "crg.h" -#include "dma.h" -#include "dma_ex.h" -#include "iocmg.h" - -#define IO_SPEED_FAST 0x00U -#define IO_SPEED_SLOW 0x01U - -#define IO_DRV_LEVEL4 0x00U -#define IO_DRV_LEVEL3 0x01U -#define IO_DRV_LEVEL2 0x02U -#define IO_DRV_LEVEL1 0x03U - -#define XTAL_DRV_LEVEL4 0x03U -#define XTAL_DRV_LEVEL3 0x02U -#define XTAL_DRV_LEVEL2 0x01U -#define XTAL_DRV_LEVEL1 0x00U - -#define GpioStartStop_PIN GPIO_PIN_4 -#define GpioStartStop_HANDLE g_gpio2 - -extern ACMP_Handle g_acmp0; -extern PGA_Handle g_pga0; -extern PGA_Handle g_pga1; -extern TIMER_Handle g_timer0; -extern TIMER_Handle g_timer1; -extern UART_Handle g_uart0; -extern APT_Handle g_apt0; -extern APT_Handle g_apt1; -extern APT_Handle g_apt2; -extern ADC_Handle g_adc0; - -extern DMA_Handle g_dmac; - -extern GPIO_Handle g_gpio2; -extern GPIO_Handle g_gpio1; - -BASE_StatusType CRG_Config(CRG_CoreClkSelect *coreClkSelect); -void SystemInit(void); - -void UART0WriteInterruptCallback(void *handle); -void UART0ReadInterruptCallback(void *handle); - -void UART0InterruptErrorCallback(void *handle); -void MotorStatemachineCallBack(void *handle); -void TIMER1_DMAOverFlow_InterruptProcess(void *handle); -void CheckPotentiometerValueCallback(void *handle); -void TIMER0_DMAOverFlow_InterruptProcess(void *handle); -void MotorCarrierProcessCallback(void *aptHandle); -void MotorSysErrCallback(void *aptHandle); - -void UART0_TXDMACallback(void *handle); - -void MotorStartStopKeyCallback(void *param); - -/* USER CODE BEGIN 0 */ -/* USER CODE 区域内代码不会被覆盖,区域外会被生成的默认代码覆盖(其余USER CODE 区域同理) */ -/* USER CODE END 0 */ - -#endif /* McuMagicTag_SYSTEM_INIT_H */ \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/generatecode/system_init.c b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/generatecode/system_init.c deleted file mode 100644 index 4ebd5b96..00000000 --- a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/generatecode/system_init.c +++ /dev/null @@ -1,677 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file system_init.c - * @author MCU Driver Team - * @brief This file contains driver init functions. - * @date 2025-04-17 15:09:50 - */ - -#include "main.h" -#include "ioconfig.h" -#include "iocmg_ip.h" - -#define UART0_BAUD_RATE 1843200 - -BASE_StatusType CRG_Config(CRG_CoreClkSelect *coreClkSelect) -{ - CRG_Handle crg; - crg.baseAddress = CRG; - crg.pllRefClkSelect = CRG_PLL_REF_CLK_SELECT_HOSC; - crg.pllPreDiv = CRG_PLL_PREDIV_4; - crg.pllFbDiv = 48; /* PLL Multiplier 48 */ - crg.pllPostDiv = CRG_PLL_POSTDIV_2; - crg.coreClkSelect = CRG_CORE_CLK_SELECT_PLL; - crg.handleEx.pllPostDiv2 = CRG_PLL_POSTDIV2_3; - crg.handleEx.clk1MSelect = CRG_1M_CLK_SELECT_HOSC; - crg.handleEx.clk1MDiv = (25 - 1); /* The 1 MHz freq is equal to the input clock frequency / (clk_1m_div + 1). 25 is the div of the clk_1m in CLOCK. */ - - if (HAL_CRG_Init(&crg) != BASE_STATUS_OK) { - return BASE_STATUS_ERROR; - } - *coreClkSelect = crg.coreClkSelect; - return BASE_STATUS_OK; -} - -static void DMA_Channel0Init(void *handle) -{ - DMA_ChannelParam dma_param; - dma_param.direction = DMA_MEMORY_TO_PERIPH_BY_DMAC; - dma_param.srcAddrInc = DMA_ADDR_INCREASE; - dma_param.destAddrInc = DMA_ADDR_UNALTERED; - dma_param.srcPeriph = DMA_REQUEST_MEM; - dma_param.destPeriph = DMA_REQUEST_UART0_TX; - dma_param.srcWidth = DMA_TRANSWIDTH_BYTE; - dma_param.destWidth = DMA_TRANSWIDTH_BYTE; - dma_param.srcBurst = DMA_BURST_LENGTH_1; - dma_param.destBurst = DMA_BURST_LENGTH_1; - dma_param.pHandle = handle; - HAL_DMA_InitChannel(&g_dmac, &dma_param, DMA_CHANNEL_ZERO); -} - -static void DMA_Init(void) -{ - HAL_CRG_IpEnableSet(DMA_BASE, IP_CLK_ENABLE); - g_dmac.baseAddress = DMA; - IRQ_Register(IRQ_DMA_TC, HAL_DMA_IrqHandlerTc, &g_dmac); - IRQ_Register(IRQ_DMA_ERR, HAL_DMA_IrqHandlerError, &g_dmac); - IRQ_EnableN(IRQ_DMA_TC); - IRQ_EnableN(IRQ_DMA_ERR); - HAL_DMA_Init(&g_dmac); - DMA_Channel0Init((void *)(&g_uart0)); - HAL_DMA_SetChannelPriorityEx(&g_dmac, DMA_CHANNEL_ZERO, DMA_PRIORITY_HIGHEST); -} - -static void ACMP0_Init(void) -{ - HAL_CRG_IpEnableSet(ACMP0_BASE, IP_CLK_ENABLE); /* ACMP clock bit reset. */ - g_acmp0.baseAddress = ACMP0_BASE; - g_acmp0.inOutConfig.inputNNum = ACMP_INPUT_N_SELECT2; - g_acmp0.inOutConfig.inputPNum = ACMP_INPUT_P_SELECT2; - g_acmp0.inOutConfig.polarity = ACMP_OUT_NOT_INVERT; - g_acmp0.filterCtrl.filterMode = ACMP_FILTER_NONE; - g_acmp0.hysteresisVol = ACMP_HYS_VOL_30MV; - g_acmp0.interruptEn = BASE_CFG_UNSET; - HAL_ACMP_Init(&g_acmp0); -} - -static void ADC0_Init(void) -{ - HAL_CRG_IpEnableSet(ADC0_BASE, IP_CLK_ENABLE); - HAL_CRG_IpClkSelectSet(ADC0_BASE, CRG_ADC_CLK_ASYN_PLL_DIV); - HAL_CRG_IpClkDivSet(ADC0_BASE, CRG_ADC_DIV_1); - - g_adc0.baseAddress = ADC0; - g_adc0.socPriority = ADC_PRIMODE_ALL_ROUND; - - HAL_ADC_Init(&g_adc0); - - SOC_Param socParam = {0}; - socParam.adcInput = ADC_CH_ADCINA0; /* PGA0_OUT(ADC AIN0) */ - socParam.sampleTotalTime = ADC_SOCSAMPLE_22CLK; /* adc sample total time 22 adc_clk */ - socParam.trigSource = ADC_TRIGSOC_APT0_SOCA; - socParam.continueMode = BASE_CFG_DISABLE; - socParam.finishMode = ADC_SOCFINISH_NONE; - HAL_ADC_ConfigureSoc(&g_adc0, ADC_SOC_NUM1, &socParam); - - socParam.adcInput = ADC_CH_ADCINA1; /* PGA1_OUT(ADC AIN1) */ - socParam.sampleTotalTime = ADC_SOCSAMPLE_22CLK; /* adc sample total time 22 adc_clk */ - socParam.trigSource = ADC_TRIGSOC_APT0_SOCA; - socParam.continueMode = BASE_CFG_DISABLE; - socParam.finishMode = ADC_SOCFINISH_NONE; - HAL_ADC_ConfigureSoc(&g_adc0, ADC_SOC_NUM6, &socParam); - - socParam.adcInput = ADC_CH_ADCINA9; /* PIN7(ADC AIN9) */ - socParam.sampleTotalTime = ADC_SOCSAMPLE_22CLK; /* adc sample total time 22 adc_clk */ - socParam.trigSource = ADC_TRIGSOC_SOFT; - socParam.continueMode = BASE_CFG_DISABLE; - socParam.finishMode = ADC_SOCFINISH_NONE; - HAL_ADC_ConfigureSoc(&g_adc0, ADC_SOC_NUM0, &socParam); - - socParam.adcInput = ADC_CH_ADCINA5; /* PIN2(ADC AIN5) */ - socParam.sampleTotalTime = ADC_SOCSAMPLE_22CLK; /* adc sample total time 22 adc_clk */ - socParam.trigSource = ADC_TRIGSOC_SOFT; - socParam.continueMode = BASE_CFG_DISABLE; - socParam.finishMode = ADC_SOCFINISH_NONE; - HAL_ADC_ConfigureSoc(&g_adc0, ADC_SOC_NUM3, &socParam); - - socParam.adcInput = ADC_CH_ADCINA15; /* PIN14(ADC AIN15) */ - socParam.sampleTotalTime = ADC_SOCSAMPLE_22CLK; /* adc sample total time 22 adc_clk */ - socParam.trigSource = ADC_TRIGSOC_SOFT; - socParam.continueMode = BASE_CFG_DISABLE; - socParam.finishMode = ADC_SOCFINISH_NONE; - HAL_ADC_ConfigureSoc(&g_adc0, ADC_SOC_NUM4, &socParam); -} - -__weak void MotorSysErrCallback(void *aptHandle) -{ - BASE_FUNC_UNUSED(aptHandle); - /* USER CODE BEGIN APT0_EVENT_INTERRUPT */ - /* USER CODE END APT0_EVENT_INTERRUPT */ -} - -__weak void MotorCarrierProcessCallback(void *aptHandle) -{ - BASE_FUNC_UNUSED(aptHandle); - /* USER CODE BEGIN APT0_TIMER_INTERRUPT */ - /* USER CODE END APT0_TIMER_INTERRUPT */ -} - -static void APT0_ProtectInit(void) -{ - APT_OutCtrlProtectEx protectApt = {0}; - protectApt.ocEventEnEx = BASE_CFG_ENABLE; - protectApt.ocEventModeEx = APT_OUT_CTRL_ONE_SHOT; - protectApt.ocActionEx = APT_OUT_CTRL_ACTION_LOW; - protectApt.ocActionBEx = APT_OUT_CTRL_ACTION_LOW; - protectApt.ocEvtInterruptEnEx = BASE_CFG_ENABLE; - protectApt.ocSysEvent = APT_SYS_EVT_DEBUG | APT_SYS_EVT_CLK | APT_SYS_EVT_MEM; - protectApt.originalEvtEx = APT_EM_ORIGINAL_SRC_ACMP0; - protectApt.evtPolarityMaskEx = APT_EM_ACMP0_INVERT_BIT; - protectApt.filterCycleNumEx = 0; - HAL_APT_ProtectInitEx(&g_apt0, &protectApt); -} - -static void APT0_Init(void) -{ - HAL_CRG_IpEnableSet(APT0_BASE, IP_CLK_ENABLE); - - g_apt0.baseAddress = APT0; - - /* Clock Settings */ - g_apt0.waveform.dividerFactor = 1 - 1; - /* Timer Settings */ - g_apt0.waveform.timerPeriod = 7500; /* 7500 is count period of APT time-base timer */ - g_apt0.waveform.cntMode = APT_COUNT_MODE_UP_DOWN; - - /* Wave Form */ - g_apt0.waveform.basicType = APT_PWM_BASIC_A_HIGH_B_LOW; - g_apt0.waveform.chAOutType = APT_PWM_OUT_BASIC_TYPE; - g_apt0.waveform.chBOutType = APT_PWM_OUT_BASIC_TYPE; - g_apt0.waveform.divInitVal = 0; - g_apt0.waveform.cntInitVal = 0; - g_apt0.waveform.cntCmpLeftEdge = 500; /* 500 is count compare point of the left edge of PWM waveform */ - g_apt0.waveform.cntCmpRightEdge = 4000; /* 4000 is count compare point of the right edge of PWM waveform */ - g_apt0.waveform.cntCmpLoadMode = APT_BUFFER_INDEPENDENT_LOAD; - g_apt0.waveform.cntCmpLoadEvt = APT_COMPARE_LOAD_EVENT_ZERO; - g_apt0.waveform.deadBandCnt = 225; /* 225 is dead-band value */ - - /* ADC Trigger SOCA */ - g_apt0.adcTrg.trgEnSOCA = BASE_CFG_ENABLE; - g_apt0.adcTrg.cntCmpSOCA = 375; /* 375 is count compare point of ADC trigger source SOCA when using CMPA */ - g_apt0.adcTrg.trgSrcSOCA = APT_CS_SRC_CNTR_CMPA_DOWN; - g_apt0.adcTrg.trgScaleSOCA = 1; - - /* ADC Trigger SOCB */ - g_apt0.adcTrg.trgEnSOCB = BASE_CFG_ENABLE; - g_apt0.adcTrg.cntCmpSOCB = 1; - g_apt0.adcTrg.trgSrcSOCB = APT_CS_SRC_CNTR_CMPB_DOWN; - g_apt0.adcTrg.trgScaleSOCB = 1; - - g_apt0.adcTrg.cntCmpLoadMode = APT_BUFFER_INDEPENDENT_LOAD; - g_apt0.adcTrg.cntCmpLoadEvt = APT_COMPARE_LOAD_EVENT_ZERO; - - /* Timer Trigger */ - g_apt0.tmrInterrupt.tmrInterruptEn = BASE_CFG_ENABLE; - g_apt0.tmrInterrupt.tmrInterruptSrc = APT_INT_SRC_CNTR_ZERO; - g_apt0.tmrInterrupt.tmrInterruptScale = 1; - - APT0_ProtectInit(); - - HAL_APT_PWMInit(&g_apt0); - HAL_APT_RegisterCallBack(&g_apt0, APT_EVENT_INTERRUPT, MotorSysErrCallback); - IRQ_SetPriority(IRQ_APT0_EVT, 7); /* 7 is priority value */ - IRQ_Register(IRQ_APT0_EVT, HAL_APT_EventIrqHandler, &g_apt0); - IRQ_EnableN(IRQ_APT0_EVT); - HAL_APT_RegisterCallBack(&g_apt0, APT_TIMER_INTERRUPT, MotorCarrierProcessCallback); - IRQ_SetPriority(IRQ_APT0_TMR, 5); /* 5 is priority value */ - IRQ_Register(IRQ_APT0_TMR, HAL_APT_TimerIrqHandler, &g_apt0); - IRQ_EnableN(IRQ_APT0_TMR); -} - -static void APT1_ProtectInit(void) -{ - APT_OutCtrlProtectEx protectApt = {0}; - protectApt.ocEventEnEx = BASE_CFG_ENABLE; - protectApt.ocEventModeEx = APT_OUT_CTRL_ONE_SHOT; - protectApt.ocActionEx = APT_OUT_CTRL_ACTION_LOW; - protectApt.ocActionBEx = APT_OUT_CTRL_ACTION_LOW; - protectApt.ocEvtInterruptEnEx = BASE_CFG_DISABLE; - protectApt.ocSysEvent = APT_SYS_EVT_DEBUG | APT_SYS_EVT_CLK | APT_SYS_EVT_MEM; - protectApt.originalEvtEx = APT_EM_ORIGINAL_SRC_ACMP0; - protectApt.evtPolarityMaskEx = APT_EM_ACMP0_INVERT_BIT; - protectApt.filterCycleNumEx = 0; - HAL_APT_ProtectInitEx(&g_apt1, &protectApt); -} - -static void APT1_Init(void) -{ - HAL_CRG_IpEnableSet(APT1_BASE, IP_CLK_ENABLE); - - g_apt1.baseAddress = APT1; - - /* Clock Settings */ - g_apt1.waveform.dividerFactor = 1 - 1; - /* Timer Settings */ - g_apt1.waveform.timerPeriod = 7500; /* 7500 is count period of APT time-base timer */ - g_apt1.waveform.cntMode = APT_COUNT_MODE_UP_DOWN; - - /* Wave Form */ - g_apt1.waveform.basicType = APT_PWM_BASIC_A_HIGH_B_LOW; - g_apt1.waveform.chAOutType = APT_PWM_OUT_BASIC_TYPE; - g_apt1.waveform.chBOutType = APT_PWM_OUT_BASIC_TYPE; - g_apt1.waveform.divInitVal = 0; - g_apt1.waveform.cntInitVal = 0; - g_apt1.waveform.cntCmpLeftEdge = 500; /* 500 is count compare point of the left edge of PWM waveform */ - g_apt1.waveform.cntCmpRightEdge = 4000; /* 4000 is count compare point of the right edge of PWM waveform */ - g_apt1.waveform.cntCmpLoadMode = APT_BUFFER_INDEPENDENT_LOAD; - g_apt1.waveform.cntCmpLoadEvt = APT_COMPARE_LOAD_EVENT_ZERO; - g_apt1.waveform.deadBandCnt = 225; /* 225 is dead-band value */ - - APT1_ProtectInit(); - - HAL_APT_PWMInit(&g_apt1); -} - -static void APT2_ProtectInit(void) -{ - APT_OutCtrlProtectEx protectApt = {0}; - protectApt.ocEventEnEx = BASE_CFG_ENABLE; - protectApt.ocEventModeEx = APT_OUT_CTRL_ONE_SHOT; - protectApt.ocActionEx = APT_OUT_CTRL_ACTION_LOW; - protectApt.ocActionBEx = APT_OUT_CTRL_ACTION_LOW; - protectApt.ocEvtInterruptEnEx = BASE_CFG_DISABLE; - protectApt.ocSysEvent = APT_SYS_EVT_DEBUG | APT_SYS_EVT_CLK | APT_SYS_EVT_MEM; - protectApt.originalEvtEx = APT_EM_ORIGINAL_SRC_ACMP0; - protectApt.evtPolarityMaskEx = APT_EM_ACMP0_INVERT_BIT; - protectApt.filterCycleNumEx = 0; - HAL_APT_ProtectInitEx(&g_apt2, &protectApt); -} - -static void APT2_Init(void) -{ - HAL_CRG_IpEnableSet(APT2_BASE, IP_CLK_ENABLE); - - g_apt2.baseAddress = APT2; - - /* Clock Settings */ - g_apt2.waveform.dividerFactor = 1 - 1; - /* Timer Settings */ - g_apt2.waveform.timerPeriod = 7500; /* 7500 is count period of APT time-base timer */ - g_apt2.waveform.cntMode = APT_COUNT_MODE_UP_DOWN; - - /* Wave Form */ - g_apt2.waveform.basicType = APT_PWM_BASIC_A_HIGH_B_LOW; - g_apt2.waveform.chAOutType = APT_PWM_OUT_BASIC_TYPE; - g_apt2.waveform.chBOutType = APT_PWM_OUT_BASIC_TYPE; - g_apt2.waveform.divInitVal = 0; - g_apt2.waveform.cntInitVal = 0; - g_apt2.waveform.cntCmpLeftEdge = 500; /* 500 is count compare point of the left edge of PWM waveform */ - g_apt2.waveform.cntCmpRightEdge = 4000; /* 4000 is count compare point of the right edge of PWM waveform */ - g_apt2.waveform.cntCmpLoadMode = APT_BUFFER_INDEPENDENT_LOAD; - g_apt2.waveform.cntCmpLoadEvt = APT_COMPARE_LOAD_EVENT_ZERO; - g_apt2.waveform.deadBandCnt = 225; /* 225 is dead-band value */ - - APT2_ProtectInit(); - - HAL_APT_PWMInit(&g_apt2); -} - -__weak void MotorStartStopKeyCallback(void *param) -{ - GPIO_Handle *handle = (GPIO_Handle *)param; - BASE_FUNC_UNUSED(handle); -} - -static void GPIO_Init(void) -{ - HAL_CRG_IpEnableSet(GPIO2_BASE, IP_CLK_ENABLE); - g_gpio2.baseAddress = GPIO2; - - g_gpio2.pins = GPIO_PIN_3; - HAL_GPIO_Init(&g_gpio2); - HAL_GPIO_SetDirection(&g_gpio2, g_gpio2.pins, GPIO_OUTPUT_MODE); - HAL_GPIO_SetValue(&g_gpio2, g_gpio2.pins, GPIO_HIGH_LEVEL); - HAL_GPIO_SetIrqType(&g_gpio2, g_gpio2.pins, GPIO_INT_TYPE_NONE); - - g_gpio2.pins = GPIO_PIN_4; - HAL_GPIO_Init(&g_gpio2); - HAL_GPIO_SetDirection(&g_gpio2, g_gpio2.pins, GPIO_INPUT_MODE); - HAL_GPIO_SetValue(&g_gpio2, g_gpio2.pins, GPIO_HIGH_LEVEL); - HAL_GPIO_SetIrqType(&g_gpio2, g_gpio2.pins, GPIO_INT_TYPE_LOW_LEVEL); - - HAL_CRG_IpEnableSet(GPIO1_BASE, IP_CLK_ENABLE); - g_gpio1.baseAddress = GPIO1; - - g_gpio1.pins = GPIO_PIN_0; - HAL_GPIO_Init(&g_gpio1); - HAL_GPIO_SetDirection(&g_gpio1, g_gpio1.pins, GPIO_OUTPUT_MODE); - HAL_GPIO_SetValue(&g_gpio1, g_gpio1.pins, GPIO_HIGH_LEVEL); - HAL_GPIO_SetIrqType(&g_gpio1, g_gpio1.pins, GPIO_INT_TYPE_NONE); - - HAL_GPIO_RegisterCallBack(&g_gpio2, GPIO_PIN_4, MotorStartStopKeyCallback); - IRQ_Register(IRQ_GPIO2, HAL_GPIO_IrqHandler, &g_gpio2); - IRQ_SetPriority(IRQ_GPIO2, 1); /* set gpio1 interrupt priority to 1, 1~15. 1 is priority value */ - IRQ_EnableN(IRQ_GPIO2); /* gpio interrupt enable */ - - return; -} - -static void PGA0_Init(void) -{ - HAL_CRG_IpEnableSet(PGA0_BASE, IP_CLK_ENABLE); - - g_pga0.baseAddress = PGA0_BASE; - g_pga0.externalResistorMode = BASE_CFG_ENABLE; - g_pga0.handleEx.extCapCompensation = PGA_EXT_COMPENSATION_2X; - HAL_PGA_Init(&g_pga0); -} - -static void PGA1_Init(void) -{ - HAL_CRG_IpEnableSet(PGA1_BASE, IP_CLK_ENABLE); - - g_pga1.baseAddress = PGA1_BASE; - g_pga1.externalResistorMode = BASE_CFG_ENABLE; - g_pga1.handleEx.extCapCompensation = PGA_EXT_COMPENSATION_2X; - HAL_PGA_Init(&g_pga1); -} - -__weak void CheckPotentiometerValueCallback(void *handle) -{ - BASE_FUNC_UNUSED(handle); - /* USER CODE BEGIN CheckPotentiometerValueCallback */ - /* USER CODE END CheckPotentiometerValueCallback */ -} - -static void TIMER0_Init(void) -{ - HAL_CRG_IpEnableSet(TIMER0_BASE, IP_CLK_ENABLE); /* TIMER0 clock enable. */ - unsigned int load = ((float)HAL_CRG_GetIpFreq((void *)TIMER0) / (1u << (TIMERPRESCALER_NO_DIV * 4)) / 1000000u) * 1000000; - - g_timer0.baseAddress = TIMER0; - g_timer0.load = (load < 1) ? 0 : (load - 1); /* Set timer value immediately */ - g_timer0.bgLoad = (load < 1) ? 0 : (load - 1); /* Set timer value */ - g_timer0.mode = TIMER_MODE_RUN_PERIODIC; /* Run in period mode */ - g_timer0.prescaler = TIMERPRESCALER_NO_DIV; /* Don't frequency division */ - g_timer0.size = TIMER_SIZE_32BIT; /* 1 for 32bit, 0 for 16bit */ - g_timer0.interruptEn = BASE_CFG_ENABLE; - g_timer0.adcSocReqEnable = BASE_CFG_DISABLE; - g_timer0.dmaReqEnable = BASE_CFG_DISABLE; - HAL_TIMER_Init(&g_timer0); - IRQ_Register(IRQ_TIMER0, HAL_TIMER_IrqHandler, &g_timer0); - - HAL_TIMER_RegisterCallback(&g_timer0, TIMER_PERIOD_FIN, CheckPotentiometerValueCallback); - IRQ_SetPriority(IRQ_TIMER0, 1); /* 1 is priority value */ - IRQ_EnableN(IRQ_TIMER0); -} - -__weak void MotorStatemachineCallBack(void *handle) -{ - BASE_FUNC_UNUSED(handle); - /* USER CODE BEGIN MotorStatemachineCallBack */ - /* USER CODE END MotorStatemachineCallBack */ -} - -static void TIMER1_Init(void) -{ - HAL_CRG_IpEnableSet(TIMER1_BASE, IP_CLK_ENABLE); /* TIMER1 clock enable. */ - unsigned int load = ((float)HAL_CRG_GetIpFreq((void *)TIMER1) / (1u << (TIMERPRESCALER_NO_DIV * 4)) / 1000000u) * 500; - - g_timer1.baseAddress = TIMER1; - g_timer1.load = (load < 1) ? 0 : (load - 1); /* Set timer value immediately */ - g_timer1.bgLoad = (load < 1) ? 0 : (load - 1); /* Set timer value */ - g_timer1.mode = TIMER_MODE_RUN_PERIODIC; /* Run in period mode */ - g_timer1.prescaler = TIMERPRESCALER_NO_DIV; /* Don't frequency division */ - g_timer1.size = TIMER_SIZE_32BIT; /* 1 for 32bit, 0 for 16bit */ - g_timer1.interruptEn = BASE_CFG_ENABLE; - g_timer1.adcSocReqEnable = BASE_CFG_DISABLE; - g_timer1.dmaReqEnable = BASE_CFG_DISABLE; - HAL_TIMER_Init(&g_timer1); - IRQ_Register(IRQ_TIMER1, HAL_TIMER_IrqHandler, &g_timer1); - - HAL_TIMER_RegisterCallback(&g_timer1, TIMER_PERIOD_FIN, MotorStatemachineCallBack); - IRQ_SetPriority(IRQ_TIMER1, 1); /* 1 is priority value */ - IRQ_EnableN(IRQ_TIMER1); -} - -__weak void UART0InterruptErrorCallback(void *handle) -{ - BASE_FUNC_UNUSED(handle); - /* USER CODE BEGIN UART0_TRNS_IT_ERROR */ - /* USER CODE END UART0_TRNS_IT_ERROR */ -} - -__weak void UART0_TXDMACallback(void *handle) -{ - BASE_FUNC_UNUSED(handle); - /* USER CODE BEGIN UART0_WRITE_DMA_FINISH */ - /* USER CODE END UART0_WRITE_DMA_FINISH */ -} - -__weak void UART0WriteInterruptCallback(void *handle) -{ - BASE_FUNC_UNUSED(handle); - /* USER CODE BEGIN UART0_WRITE_IT_FINISH */ - /* USER CODE END UART0_WRITE_IT_FINISH */ -} - -__weak void UART0ReadInterruptCallback(void *handle) -{ - BASE_FUNC_UNUSED(handle); - /* USER CODE BEGIN UART0_READ_IT_FINISH */ - /* USER CODE END UART0_READ_IT_FINISH */ -} - -static void UART0_Init(void) -{ - HAL_CRG_IpEnableSet(UART0_BASE, IP_CLK_ENABLE); /* UART0 clock enable. */ - g_uart0.baseAddress = UART0; - - g_uart0.baudRate = UART0_BAUD_RATE; - g_uart0.dataLength = UART_DATALENGTH_8BIT; - g_uart0.stopBits = UART_STOPBITS_ONE; - g_uart0.parity = UART_PARITY_NONE; - g_uart0.txMode = UART_MODE_DMA; - g_uart0.rxMode = UART_MODE_INTERRUPT; - g_uart0.fifoMode = BASE_CFG_ENABLE; - g_uart0.fifoTxThr = UART_FIFODEPTH_SIZE8; - g_uart0.fifoRxThr = UART_FIFODEPTH_SIZE8; - g_uart0.hwFlowCtr = BASE_CFG_DISABLE; - g_uart0.handleEx.overSampleMultiple = UART_OVERSAMPLING_16X; - g_uart0.handleEx.msbFirst = BASE_CFG_DISABLE; - HAL_UART_Init(&g_uart0); - HAL_UART_RegisterCallBack(&g_uart0, UART_TRNS_IT_ERROR, (UART_CallbackType)UART0InterruptErrorCallback); - HAL_UART_RegisterCallBack(&g_uart0, UART_READ_IT_FINISH, (UART_CallbackType)UART0ReadInterruptCallback); - IRQ_Register(IRQ_UART0, HAL_UART_IrqHandler, &g_uart0); - IRQ_SetPriority(IRQ_UART0, 6); /* 6 is priority value */ - IRQ_EnableN(IRQ_UART0); - g_uart0.dmaHandle = &g_dmac; - g_uart0.uartDmaTxChn = 0; - HAL_UART_RegisterCallBack(&g_uart0, UART_WRITE_DMA_FINISH, (UART_CallbackType)UART0_TXDMACallback); - HAL_UART_RegisterCallBack(&g_uart0, UART_WRITE_IT_FINISH, (UART_CallbackType)UART0WriteInterruptCallback); -} - -static void IOConfig(void) -{ - SYSCTRL0->SC_SYS_STAT.BIT.update_mode = 0; - SYSCTRL0->SC_SYS_STAT.BIT.update_mode_clear = 1; - /* Config PIN39 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO0_3_AS_UART0_TXD); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO0_3_AS_UART0_TXD, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO0_3_AS_UART0_TXD, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO0_3_AS_UART0_TXD, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO0_3_AS_UART0_TXD, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN40 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO0_4_AS_UART0_RXD); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO0_4_AS_UART0_RXD, PULL_UP); /* Pull-up and Pull-down, UART RX recommend PULL_UP */ - HAL_IOCMG_SetPinSchmidtMode(GPIO0_4_AS_UART0_RXD, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO0_4_AS_UART0_RXD, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO0_4_AS_UART0_RXD, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN1 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO0_7_AS_ACMP0_OUT); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO0_7_AS_ACMP0_OUT, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO0_7_AS_ACMP0_OUT, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO0_7_AS_ACMP0_OUT, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO0_7_AS_ACMP0_OUT, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN48 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO0_6_AS_ACMP_N2); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO0_6_AS_ACMP_N2, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO0_6_AS_ACMP_N2, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO0_6_AS_ACMP_N2, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO0_6_AS_ACMP_N2, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN47 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO0_5_AS_ACMP_P2); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO0_5_AS_ACMP_P2, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO0_5_AS_ACMP_P2, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO0_5_AS_ACMP_P2, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO0_5_AS_ACMP_P2, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN4 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO2_6_AS_PGA0_N0); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO2_6_AS_PGA0_N0, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO2_6_AS_PGA0_N0, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO2_6_AS_PGA0_N0, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO2_6_AS_PGA0_N0, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN5 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO2_5_AS_PGA0_P0); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO2_5_AS_PGA0_P0, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO2_5_AS_PGA0_P0, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO2_5_AS_PGA0_P0, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO2_5_AS_PGA0_P0, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN3 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO2_7_AS_PGA0_OUT); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO2_7_AS_PGA0_OUT, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO2_7_AS_PGA0_OUT, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO2_7_AS_PGA0_OUT, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO2_7_AS_PGA0_OUT, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN7 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO3_7_AS_ADC_AIN9); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO3_7_AS_ADC_AIN9, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO3_7_AS_ADC_AIN9, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO3_7_AS_ADC_AIN9, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO3_7_AS_ADC_AIN9, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN2 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO5_1_AS_ADC_AIN5); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO5_1_AS_ADC_AIN5, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO5_1_AS_ADC_AIN5, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO5_1_AS_ADC_AIN5, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO5_1_AS_ADC_AIN5, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN14 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO4_7_AS_ADC_AIN15); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO4_7_AS_ADC_AIN15, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO4_7_AS_ADC_AIN15, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO4_7_AS_ADC_AIN15, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO4_7_AS_ADC_AIN15, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN11 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO1_5_AS_PGA1_P0); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO1_5_AS_PGA1_P0, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO1_5_AS_PGA1_P0, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO1_5_AS_PGA1_P0, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO1_5_AS_PGA1_P0, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN12 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO1_6_AS_PGA1_N0); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO1_6_AS_PGA1_N0, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO1_6_AS_PGA1_N0, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO1_6_AS_PGA1_N0, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO1_6_AS_PGA1_N0, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN13 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO1_7_AS_PGA1_OUT); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO1_7_AS_PGA1_OUT, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO1_7_AS_PGA1_OUT, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO1_7_AS_PGA1_OUT, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO1_7_AS_PGA1_OUT, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN35 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO2_3_AS_GPIO2_3); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO2_3_AS_GPIO2_3, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO2_3_AS_GPIO2_3, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO2_3_AS_GPIO2_3, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO2_3_AS_GPIO2_3, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN27 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO1_0_AS_GPIO1_0); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO1_0_AS_GPIO1_0, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO1_0_AS_GPIO1_0, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO1_0_AS_GPIO1_0, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO1_0_AS_GPIO1_0, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN41 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO2_4_AS_GPIO2_4); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO2_4_AS_GPIO2_4, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO2_4_AS_GPIO2_4, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO2_4_AS_GPIO2_4, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO2_4_AS_GPIO2_4, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN19 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO3_0_AS_APT0_PWMA); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO3_0_AS_APT0_PWMA, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO3_0_AS_APT0_PWMA, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO3_0_AS_APT0_PWMA, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO3_0_AS_APT0_PWMA, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN23 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO4_0_AS_APT0_PWMB); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO4_0_AS_APT0_PWMB, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO4_0_AS_APT0_PWMB, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO4_0_AS_APT0_PWMB, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO4_0_AS_APT0_PWMB, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN20 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO3_1_AS_APT1_PWMA); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO3_1_AS_APT1_PWMA, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO3_1_AS_APT1_PWMA, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO3_1_AS_APT1_PWMA, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO3_1_AS_APT1_PWMA, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN24 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO4_1_AS_APT1_PWMB); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO4_1_AS_APT1_PWMB, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO4_1_AS_APT1_PWMB, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO4_1_AS_APT1_PWMB, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO4_1_AS_APT1_PWMB, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN21 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO3_2_AS_APT2_PWMA); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO3_2_AS_APT2_PWMA, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO3_2_AS_APT2_PWMA, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO3_2_AS_APT2_PWMA, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO3_2_AS_APT2_PWMA, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN25 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO4_2_AS_APT2_PWMB); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO4_2_AS_APT2_PWMB, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO4_2_AS_APT2_PWMB, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO4_2_AS_APT2_PWMB, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO4_2_AS_APT2_PWMB, DRIVER_RATE_2); /* Output signal edge fast/slow */ -} - -static void APT_SyncMasterInit(void) -{ - HAL_APT_MasterSyncInit(&g_apt0, APT_SYNC_OUT_ON_CNTR_ZERO); -} - -static void APT_SyncSlaveInit(void) -{ - APT_SlaveSyncIn aptSlave; - - aptSlave.cntPhase = 0; /* counter phase value */ - aptSlave.syncCntMode = APT_COUNT_MODE_AFTER_SYNC_UP; - aptSlave.syncInSrc = APT_SYNCIN_SRC_APT0_SYNCOUT; /* sync source selection */ - aptSlave.cntrSyncSrc = APT_CNTR_SYNC_SRC_SYNCIN; - HAL_APT_SlaveSyncInit(&g_apt1, &aptSlave); - - aptSlave.cntPhase = 0; /* counter phase value */ - aptSlave.syncCntMode = APT_COUNT_MODE_AFTER_SYNC_UP; - aptSlave.syncInSrc = APT_SYNCIN_SRC_APT0_SYNCOUT; /* sync source selection */ - aptSlave.cntrSyncSrc = APT_CNTR_SYNC_SRC_SYNCIN; - HAL_APT_SlaveSyncInit(&g_apt2, &aptSlave); - -} - -void SystemInit(void) -{ - IOConfig(); - DMA_Init(); - UART0_Init(); - ACMP0_Init(); - APT0_Init(); - APT1_Init(); - APT2_Init(); - ADC0_Init(); - PGA0_Init(); - PGA1_Init(); - TIMER0_Init(); - TIMER1_Init(); - GPIO_Init(); - - APT_SyncMasterInit(); - APT_SyncSlaveInit(); - - /* USER CODE BEGIN system_init */ - /* USER CODE END system_init */ -} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/main.c b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/main.c deleted file mode 100644 index f16312ae..00000000 --- a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/main.c +++ /dev/null @@ -1,71 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file main.c - * @author MCU Driver Team - * @brief Main program body. - * @date 2025-04-17 15:09:50 - */ - -#include "typedefs.h" -#include "feature.h" -#include "mcs_motor_process.h" -#include "main.h" -/* USER CODE BEGIN 0 */ -/* USER CODE 区域内代码不会被覆盖,区域外会被生成的默认代码覆盖(其余USER CODE 区域同理) */ -/* 建议用户放置头文件 */ -/* USER CODE END 0 */ -ACMP_Handle g_acmp0; -PGA_Handle g_pga0; -PGA_Handle g_pga1; -TIMER_Handle g_timer0; -TIMER_Handle g_timer1; -UART_Handle g_uart0; -APT_Handle g_apt0; -APT_Handle g_apt1; -APT_Handle g_apt2; -ADC_Handle g_adc0; -DMA_Handle g_dmac; -GPIO_Handle g_gpio2; -GPIO_Handle g_gpio1; -/* USER CODE BEGIN 1 */ -/* 建议用户定义全局变量、结构体、宏定义或函数声明等 */ -/* USER CODE END 1 */ - -int main(void) -{ - /* USER CODE BEGIN 2 */ - /* 建议用户放置初始化代码或启动代码等 */ - /* USER CODE END 2 */ - MotorMainProcess(); - /* USER CODE BEGIN 3 */ - /* 建议用户放置初始配置代码 */ - /* USER CODE END 3 */ - while (1) { - /* USER CODE BEGIN 4 */ - /* 建议用户放置周期性执行代码 */ - /* USER CODE END 4 */ - } - /* USER CODE BEGIN 5 */ - /* 建议用户放置代码流程 */ - /* USER CODE END 5 */ - return BASE_STATUS_OK; -} - -/* USER CODE BEGIN 6 */ -/* 建议用户放置自定义函数 */ -/* USER CODE END 6 */ \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_carrier.h b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_carrier.h deleted file mode 100644 index dea3ad66..00000000 --- a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_carrier.h +++ /dev/null @@ -1,142 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_carrier.h - * @author MCU Algorithm Team - * @brief This file provides functions declaration for carrier interrupt processing function. - */ -#ifndef McuMagicTag_MCS_CARRIER_H -#define McuMagicTag_MCS_CARRIER_H - -#include "mcs_status.h" -#include "mcs_mtr_param.h" -#include "mcs_svpwm.h" -#include "mcs_curr_ctrl.h" -#include "mcs_if_ctrl.h" -#include "mcs_ramp_mgmt.h" -#include "mcs_spd_ctrl.h" -#include "mcs_fosmo.h" -#include "mcs_smo_4th.h" -#include "mcs_pll.h" -#include "mcs_startup.h" -#include "mcs_r1_svpwm.h" -#include "mcs_fw_ctrl.h" -#include "mcs_prot_user.h" - -typedef void (*MCS_ReadCurrUvwCb)(UvwAxis *CurrUvw); -typedef void (*MCS_SetPwmDutyCb)(UvwAxis *dutyUvwLeft, UvwAxis *dutyUvwRight); -typedef void (*MCS_SetADCTriggerTimeCb)(unsigned short cntCmpSOCA, unsigned short cntCmpSOCB); - -/** - * @brief motor control FSM state define. - * @details motor control FSM state type: - * + FSM_IDLE -- IDLE state, system startup default. - * + FSM_OFFSET_CALIB -- Self calibrate, for ADC init. - * + FSM_CAP_CHARGE -- IPM cap charge. - * + FSM_CLEAR -- Clear before every run. - * + FSM_LOCATE -- Rotor position pre-locate. - * + FSM_STARTUP -- Start up. - * + FSM_SWITCH -- Transition state, control from open to closed loop. - * + FSM_RUN -- Normal running state. - * + FSM_WAIT_STOP -- Wait stopping. - * + FSM_STOP -- Normal stop. - * + FSM_FAULT -- Fault state, waiting for user process. - */ -typedef enum { - FSM_IDLE = 0, - FSM_OFFSET_CALIB, - FSM_CAP_CHARGE, - FSM_CLEAR, - FSM_LOCATE, - FSM_STARTUP, - FSM_SWITCH, - FSM_RUN, - FSM_WAIT_STOP, - FSM_STOP, - FSM_FAULT -} FsmState; - -/** - * @brief Sampling mode. - */ -typedef enum { - DUAL_RESISTORS = 0, - SINGLE_RESISTOR = 1 -} SampleMode; - -/** - * @brief Motor control data structure - */ -typedef struct { - unsigned char motorStateFlag; /**< Motor start/stop status flag. */ - float spdCmdHz; /**< External input speed command value. */ - float axisAngle; /**< Angle of the synchronous coordinate system. */ - float spdRefHz; /**< Command value after speed ramp management. */ - float currCtrlPeriod; /**< current loop control period. */ - float adc0Compensate; /**< ADC0 softwaretrim compensate value. */ - float adc1Compensate; /**< ADC1 softwaretrim compensate value. */ - float udc; /**< Bus voltage. */ - float powerBoardTemp; /**< Power boart surface temperature. */ - unsigned short aptMaxcntCmp; /**< Apt Maximum Comparison Count. */ - float adcCurrCofe; /**< Adc current sampling cofeature. */ - - unsigned short sysTickCnt; /**< System Timer Tick Count. */ - unsigned short capChargeTickNum; /**< Bootstrap Capacitor Charge Tick Count. */ - volatile unsigned int msTickCnt; /**< Millisecond-level counter, which can be used in 1-ms and 5-ms tasks. */ - unsigned short msTickNum; /**< Number of ticks corresponding to 1 ms. */ - char obserType; /**< Set Observer Type. */ - char controlMode; /**< Set foc control or sixstep bldc control mode or others. */ - char spdAdjustMode; /**< Set speed adjust mode. */ - char uartConnectFlag; /**< Uart connect success flag. */ - short uartHeartDetCnt; /**< Uart connect heart detect count. */ - float uartTimeStamp; /**< Uart data time stamp. */ - SysStatusReg statusReg; /**< System status. */ - FsmState stateMachine; /**< Motor Control State Machine. */ - - SampleMode sampleMode; /**< Current sampling mode setting: single resistor, double resistor. */ - MOTOR_Param mtrParam; /**< Motor parameters. */ - FOSMO_Handle smo; /**< SMO observer handle. */ - SMO4TH_Handle smo4th; /**< SMO 4th observer handle. */ - IF_Handle ifCtrl; /**< I/F control handle. */ - SVPWM_Handle sv; /**< Dual-resistance/three-resistance sampling SVPWM Handle. */ - R1SVPWM_Handle r1Sv; /**< Single-resistance phase-shifted SVPWM handle. */ - RMG_Handle spdRmg; /**< Ramp management struct for the speed controller input reference. */ - SPDCTRL_Handle spdCtrl; /**< Speed loop Control Handle. */ - CURRCTRL_Handle currCtrl; /**< Current loop control handle. */ - STARTUP_Handle startup; /**< Startup Switch Handle. */ - FW_Handle fw; /**< Flux-Weakening Handle. */ - - DqAxis idqRef; /**< Command value of the dq axis current. */ - UvwAxis currUvw; /**< Three-phase current sampling value. */ - AlbeAxis iabFbk; /**< αβ-axis current feedback value. */ - DqAxis idqFbk; /**< Current feedback value of the dq axis. */ - DqAxis vdqRef; /**< Current loop output dq voltage. */ - AlbeAxis vabRef; /**< Current loop output voltage αβ. */ - UvwAxis dutyUvw; /**< UVW three-phase duty cycle. */ - UvwAxis dutyUvwLeft; /**< Single Resistor UVW Three-Phase Left Duty Cycle. */ - UvwAxis dutyUvwRight; /**< Single Resistor UVW Three-Phase Right Duty Cycle. */ - - MCS_ReadCurrUvwCb readCurrUvwCb; /**< Read current callback function. */ - MCS_SetPwmDutyCb setPwmDutyCb; /**< Set the duty cycle callback function. */ - MCS_SetADCTriggerTimeCb setADCTriggerTimeCb; /**< Sets the ADC trigger point callback function. */ - - MotorProtStatus_Handle prot; /**< Motor protect status detection flag bit handle. */ -} MTRCTRL_Handle; - -void MCS_CarrierProcess(MTRCTRL_Handle *mtrCtrl); - -#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_chip_config.h b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_chip_config.h deleted file mode 100644 index 38c00b5d..00000000 --- a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_chip_config.h +++ /dev/null @@ -1,76 +0,0 @@ -/** - * @ Copyright (c) HiSilicon (Shanghai) Technologies Co., Ltd. 2022-2023. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_chip_config.h - * @author MCU Algorithm Team - * @brief This file provides config macros for ECMCU105H/ECBMCU201MPC app. - */ - -#ifndef McuMagicTag_MCS_CHIPCONFIG_H -#define McuMagicTag_MCS_CHIPCONFIG_H - -#include "feature.h" - -#if defined (CHIP_3061MNPICA) || defined (CHIP_3061MNPIKA) || defined (CHIP_3061MNNICA) || \ - defined (CHIP_3061MNNIKA) || defined (CHIP_3061MNPIC8) || defined(CHIP_3061MNNIC8) || \ - defined (CHIP_3061MNPIK8) || defined (CHIP_3061MNNIK8) - - #define ADCPTT_HANDLE g_adc0 - #define ADCRESIS_HANDLE g_adc0 - #define ADCUDC_HANDLE g_adc0 - #define ADCU_HANDLE g_adc0 - #define ADCW_HANDLE g_adc0 - #define LED1_HANDLE g_gpio2 - #define LED2_HANDLE g_gpio1 - #define LED2_PIN GPIO_PIN_0 - #define LED1_PIN GPIO_PIN_3 - #define ADCPTTSOCNUM ADC_SOC_NUM0 - #define ADCUSOCNUM ADC_SOC_NUM1 - #define ADCRESISSOCNUM ADC_SOC_NUM3 - #define ADCUDCSOCNUM ADC_SOC_NUM4 - #define ADCWSOCNUM ADC_SOC_NUM6 - - #define ADC0COMPENSATE 2037.0f - #define ADC1COMPENSATE 2027.0f - -#endif - -#if defined (CHIP_3065HRPIRZ) || defined (CHIP_3065ARPIRZ) || defined (CHIP_3066MNPIRH) || \ - defined (CHIP_3065PNPIRH) || defined (CHIP_3065PNPIRE) || defined (CHIP_3065PNPIRA) - - #define ADCU_HANDLE g_adc0 - #define ADCW_HANDLE g_adc1 - #define ADCRESIS_HANDLE g_adc2 - #define ADCUDC_HANDLE g_adc2 - #define ADCPTT_HANDLE g_adc2 - #define LED1_HANDLE g_gpio0 - #define LED2_HANDLE g_gpio0 - #define LED2_PIN GPIO_PIN_6 - #define LED1_PIN GPIO_PIN_7 - #define ADCPTTSOCNUM ADC_SOC_NUM0 - #define ADCRESISSOCNUM ADC_SOC_NUM1 - #define ADCUSOCNUM ADC_SOC_NUM8 - #define ADCWSOCNUM ADC_SOC_NUM8 - #define ADCUDCSOCNUM ADC_SOC_NUM14 - - #define ADC0COMPENSATE 2033.0f - #define ADC1COMPENSATE 2070.0f - -#endif - - -#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_ctlmode_config.h b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_ctlmode_config.h deleted file mode 100644 index 2a6f38cc..00000000 --- a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_ctlmode_config.h +++ /dev/null @@ -1,73 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_ctlmode_config.h - * @author MCU Algorithm Team - * @brief This file provides config macros for ECMCU105H app. - */ - - /* Define to prevent recursive inclusion ------------------------------------------------------- */ -#ifndef McuMagicTag_MCS_CTLMODECONFIG_H -#define McuMagicTag_MCS_CTLMODECONFIG_H - -#include "debug.h" -#include "typedefs.h" - -typedef enum { - FOC_STARTUP_IF = 0, - FOC_STARTUP_VF, - FOC_STARTUP_HFI -} MOTOR_STARTUPMODE_CONFIG; - -typedef enum { - FOC_OBSERVERTYPE_SMO1TH = 0, - FOC_OBSERVERTYPE_SMO1TH_PLL, - FOC_OBSERVERTYPE_SMO4TH, - FOC_OBSERVERTYPE_SMO4TH_PLL, - FOC_OBSERVERTYPE_LUNBORG, - FOC_OBSERVERTYPE_FLUX -} MOTOR_OBSERVERTYPE_CONFIG; - -typedef enum { - FOC_CONTROLMODE_SPEED = 0, - FOC_CONTROLMODE_TORQUE -} MOTOR_CONTROLMODE_CONFIG; - -typedef enum { - FOC_CURQAXISPID_PARAMS = 0, - FOC_CURDAXISPID_PARAMS, - FOC_SPDPID_PARAMS -} MOTOR_PID_SET; - -typedef enum { - MOTOR_PARAMS_BASE = 0, - MOTOR_PARAMS_SPECIAL, - MOTOR_PARAMS_BOARD -} MOTOR_PARAMS_SET; - -typedef enum { - CUST_SPEED_ADJUST = 0, - HOST_SPEED_ADJUST -} MODE_ADSPEED_CONFIG; - -typedef enum { - CONNECTING = 0, - CONNECTED, - DISCONNECT -} UART_STATUS; - -#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_motor_process.h b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_motor_process.h deleted file mode 100644 index 783bf214..00000000 --- a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_motor_process.h +++ /dev/null @@ -1,45 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_motor_process.h - * @author MCU Algorithm Team - * @brief This file provides motor sample functions declaration for ECMCU105H board. - */ -#ifndef McuMagicTag_MCS_MOTOR_PROCESS_H -#define McuMagicTag_MCS_MOTOR_PROCESS_H - -/** - * @brief phase sequence. - */ -typedef enum { - PHASE_U = 0, - PHASE_V, - PHASE_W, - PHASE_MAX_NUM -} MCS_PhaseEnum; - -/** - * @brief key state. - */ -typedef enum { - KEY_DOWN = 0, - KEY_UP = 1, -} KEY_State; - -int MotorMainProcess(void); - -#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_status.h b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_status.h deleted file mode 100644 index 19510c78..00000000 --- a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_status.h +++ /dev/null @@ -1,188 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_status.h - * @author MCU Algorithm Team - * @brief This file provides functions declaration of system status. - */ - -/* Define to prevent recursive inclusion ------------------------------------------------------- */ -#ifndef McuMagicTag_MCS_STATUS_H -#define McuMagicTag_MCS_STATUS_H - -/* Includes ------------------------------------------------------------------------------------ */ -#include "typedefs.h" -#include "mcs_assert.h" - -/* Typedef definitions ------------------------------------------------------------------------- */ -/** - * @brief System status define - */ -typedef union { - unsigned short all; - struct { - unsigned short cmdStart : 1; /**< Indicates that a start system command has been received. */ - unsigned short cmdStop : 1; /**< Indicates that a stop system command has been received. */ - unsigned short isRunning : 1; /**< Indicates that the system is running (enable signal) */ - unsigned short sysError : 1; /**< Indicates that the system reports an error. */ - unsigned short poweron : 1; /**< Indicates that the power-on initialization phase is complete. */ - unsigned short capcharge : 1; /**< Indicates that the bootstrap capacitor charging phase is complete. */ - unsigned short adczero : 1; /**< The current sampling point is reset to zero after power-on. */ - } Bit; -} SysStatusReg; - -/** - * @brief Get status of Bit cmdStart. - * @param sysStatus System status register handle. - * @retval Status of Bit cmdStart. - */ -static inline bool SysGetCmdStart(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - bool ret; - ret = (sysStatus->Bit.cmdStart == 1) ? true : false; - return ret; -} - -/** - * @brief Set Bit cmdStart. - * @param sysStatus System status register handle. - * @retval None. - */ -static inline void SysCmdStartSet(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - sysStatus->Bit.cmdStart = 1; -} - -/** - * @brief Clear Bit cmdStart. - * @param handle System status register handle. - * @retval None. - */ -static inline void SysCmdStartClr(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - sysStatus->Bit.cmdStart = 0; -} - -/** - * @brief Get status of Bit cmdStop. - * @param sysStatus System status register handle. - * @retval Status of Bit cmdStart. - */ -static inline bool SysGetCmdStop(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - bool ret; - ret = (sysStatus->Bit.cmdStop == 1) ? true : false; - return ret; -} - -/** - * @brief Set Bit cmdStop. - * @param sysStatus System status register handle. - * @retval None. - */ -static inline void SysCmdStopSet(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - sysStatus->Bit.cmdStop = 1; -} - -/** - * @brief Clear Bit cmdStop. - * @param sysStatus System status register handle. - * @retval None. - */ -static inline void SysCmdStopClr(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - sysStatus->Bit.cmdStop = 0; -} - -/** - * @brief Get status of Bit isRunning. - * @param sysStatus System status register handle. - * @retval Status of Bit isRunning. - */ -static inline bool SysIsRunning(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - bool ret; - ret = (sysStatus->Bit.isRunning == 1) ? true : false; - return ret; -} - -/** - * @brief Set Bit isRuning. - * @param sysStatus System status register handle. - * @retval None. - */ -static inline void SysRunningSet(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - sysStatus->Bit.isRunning = 1; -} - -/** - * @brief Clear Bit isRuning. - * @param sysStatus System status register handle. - * @retval None. - */ -static inline void SysRunningClr(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - sysStatus->Bit.isRunning = 0; -} - -/** - * @brief Get status of Bit sysError. - * @param sysStatus System status register handle. - * @retval Status of Bit sysError. - */ -static inline bool SysIsError(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - bool ret; - ret = (sysStatus->Bit.sysError == 1) ? true : false; - return ret; -} - -/** - * @brief Set Bit sysError. - * @param sysStatus System status register handle. - * @retval None. - */ -static inline void SysErrorSet(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - sysStatus->Bit.sysError = 1; -} - -/** - * @brief Clear Bit sysError. - * @param sysStatus System status register handle. - * @retval None. - */ -static inline void SysErrorClr(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - sysStatus->Bit.sysError = 0; -} - -#endif diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_user_config.h b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_user_config.h deleted file mode 100644 index 19e1f89f..00000000 --- a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_user_config.h +++ /dev/null @@ -1,120 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_user_config.h - * @author MCU Algorithm Team - * @brief This file provides functions declaration of user config parameters. - */ -#ifndef McuMagicTag_MCS_CONFIG_H -#define McuMagicTag_MCS_CONFIG_H - -#include "debug.h" -#include "typedefs.h" - -#define SMO4TH - -#define SYSTICK_PERIOD_US 500u /* systick period. */ - -#define INV_CAP_CHARGE_MS 3u /* Capacitor charging time. */ - -#define INV_VOLTAGE_BUS 24.0f /* Bus voltage, V. */ - -#define CTRL_CURR_PERIOD 0.0001f /* carrier ISR period, 100us. */ -#define CTRL_SYSTICK_PERIOD 0.0005f /* systick control period, 500us. */ - -/* Duty of sample window, the real time is 0.06*50us = 3us. */ -#define SAMPLE_WINDOW_DUTY 0.06f - -/* Duty of sample point shift as flip point, the real time is 0.008*50us = 0.4us. */ -#define SAMPLE_POINT_SHIFT 0.008f - -/* Sampling resistance 200mOhm 0.0013295 */ -#define ADC_CURR_COFFI 0.0013295f /* 3.3/4096/3.03/0.2 pga: 3.03, 200mohm. */ - -#define APT_U APT0_BASE /* Base address of U phase APT module. */ -#define APT_V APT1_BASE /* Base address of V phase APT module. */ -#define APT_W APT2_BASE /* Base address of W phase APT module. */ - -/* FOSMO */ -#define FOSMO_GAIN 4.0f /* SMO gain */ -#define FOSMO_LAMBDA 2.0f /* SMO coefficient of cut-off frequency, its value = lambda * we. */ -#define FOSMO_EMF_CUTOFF_FREQ 2.0f /* SMO back emf cutoff frequency. */ -#define SPEED_FILTER_CUTOFF_FREQUENCY 40.0f /* SMO speed cutoff frequency of speed filter. */ -#define FOSMO_PLL_BDW 30.0f /* SMO PLL bandwidth. */ - -/* SMO4TH */ -#define SMO4TH_PLL_BDW 30.0f /* SMO4TH PLL Bandwidth. */ -#define SMO4TH_KD 300.0f /* SMO4TH parameters KD. */ -#define SMO4TH_KQ 600.0f /* SMO4TH parameters KQ. */ -#define SMO4TH_SPD_FILTER_CUTOFF_FREQ 40.0f /* SMO4TH speed cutoff frequency of speed filter. */ - -/* User_Commond */ -#define CTRL_IF_CURR_AMP_A 0.8f /* IF control current amplitude. */ -#define USER_TARGET_SPD_HZ 30.0f /* User-defined target speed value. */ -#define USER_SWITCH_SPDBEGIN_HZ 30.0f /* Start of handover interval. */ -#define USER_SWITCH_SPDEND_HZ (USER_SWITCH_SPDBEGIN_HZ + 3.0f) /* End of handover period. */ -#define USER_MAX_SPD_HZ 24.0f /* User-defined maximum speed value. */ -#define USER_MIN_SPD_HZ 5.0f /* User-defined minimum speed value. */ -#define USER_SPD_SLOPE 50.0f /* slope of velocity change. */ -#define USER_CURR_SLOPE (CTRL_IF_CURR_AMP_A * 10.0f) /* Current change slope. */ - -/* PID PARAMS */ -#define CURRQAXIS_KP 5.023202f /* Current loop Q axis Kp. */ -#define CURRQAXIS_KI 20612.84f /* Current loop Q axis Ki. */ -#define CURRDAXIS_KP 3.477114f /* Current loop D axis Kp. */ -#define CURRDAXIS_KI 20612.84f /* Current loop D axis Ki. */ -/* Current loop PID output lower limit. */ -#define CURR_LOWERLIM (-INV_VOLTAGE_BUS * ONE_DIV_SQRT3 * 0.98f) -/* Current loop PID output upper limit. */ -#define CURR_UPPERLIM (INV_VOLTAGE_BUS * ONE_DIV_SQRT3 * 0.98f) - -#define SPD_KP 0.00505f /* Speed loop Kp. */ -#define SPD_KI 0.012f /* Speed loop Ki. */ -#define SPD_LOWERLIM (-0.105f) /* Speed loop PID output lower limit. */ -#define SPD_UPPERLIM 0.105f /* Speed loop PID output upper limit. */ - -/* MOTOR PARAMS */ -/* Np, Rs, Ld, Lq, Psif, J, Nmax, Currmax, PPMR, zShift. */ -/* mtrPsif & mtrJ parameter is not used in this project, temporarily set to 0. */ -// #define MOTORPARAM_DEFAULTS { \ -// .mtrNp = 7, \ -// .mtrRs = 5.1f, \ -// .mtrLd = 0.00133f, \ -// .mtrLq = 0.00133f, \ -// .mtrPsif = 0.0f, \ -// .mtrJ = 0.0f, \ -// .maxElecSpd = 180.25f, \ -// .maxCurr = 0.105f, \ -// .busVolt = INV_VOLTAGE_BUS, \ -// } - -// // GIM4310-10 -#define MOTORPARAM_DEFAULTS { \ - .mtrNp = 14, \ - .mtrRs = 1.046f, \ - .mtrLd = 0.000344f, \ - .mtrLq = 0.000344f, \ - .mtrPsif = 0.0f, \ - .mtrJ = 0.0f, \ - .maxElecSpd = 24.0f, \ - .maxCurr = 0.8f, \ - .busVolt = INV_VOLTAGE_BUS, \ -} -/* Bus voltage sampling conversion factor. */ -#define ADC_UDC_COFFI 0.01289f /* 0.01289 = 3.3/4096*192/12. */ - -#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_cmm.c b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_cmm.c deleted file mode 100644 index 99766bdf..00000000 --- a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_cmm.c +++ /dev/null @@ -1,43 +0,0 @@ -/** - * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_prot_cmm.c - * @author MCU Algorithm Team - * @brief This file contains protection common api definition. - */ - -/* Includes ------------------------------------------------------------------------------------ */ -#include "mcs_prot_cmm.h" -#include "apt.h" -#include "mcs_assert.h" - -/** - * @brief Safty-pulse-off function execution to turn off all the power devices. - * @param aptAddr Three-phase APT address pointer. - * @retval None. - */ -void ProtSpo_Exec(APT_RegStruct **aptAddr) -{ - MCS_ASSERT_PARAM(aptAddr != NULL); - /**< Turn off all the six power devices of the inverter. */ - for (unsigned int i = 0; i < MOTOR_PHASE_NUMBER; i++) { - APT_RegStruct *aptx = (APT_RegStruct *)(aptAddr[i]); - DCL_APT_EnableSwContPWMAction(aptx, APT_PWM_CHANNEL_A); - DCL_APT_EnableSwContPWMAction(aptx, APT_PWM_CHANNEL_B); - DCL_APT_ForcePWMOutputLow(aptx); - } -} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_cmm.h b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_cmm.h deleted file mode 100644 index 104a5dec..00000000 --- a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_cmm.h +++ /dev/null @@ -1,136 +0,0 @@ -/** - * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_prot_cmm.h - * @author MCU Algorithm Team - * @brief This file contains protection function common data struct and api declaration. - */ - -/* Define to prevent recursive inclusion ------------------------------------------------------- */ -#ifndef McuMagicTag_MCS_PROT_CMM_H -#define McuMagicTag_MCS_PROT_CMM_H - -#include "typedefs.h" -#include "apt_ip.h" - -/* Macro definitions --------------------------------------------------------------------------- */ -#define MOTOR_PHASE_NUMBER (3) - -#define PROT_VAL_THRESHOLD_NUMS (4) -#define PROT_VAL_THRESHOLD_0 (0) -#define PROT_VAL_THRESHOLD_1 (1) -#define PROT_VAL_THRESHOLD_2 (2) -#define PROT_VAL_THRESHOLD_3 (3) - -#define PROT_LIMIT_TIME_NUMS (3) -#define PROT_LIMIT_TIME_0 (0) -#define PROT_LIMIT_TIME_1 (1) -#define PROT_LIMIT_TIME_2 (2) - -#define MOTOR_PHASE_NUMBER (3) - -/**< Motor error status definition. */ -typedef union { - int all; - struct { - unsigned short overCurrErr : 1; /**< Indicates that phase current(s) is over protected value. */ - unsigned short overVoltErr : 1; /**< Indicates that dc-link voltage is over protected value. */ - unsigned short lowerVoltErr : 1; /**< Indicates that dc-link voltage is lower than protected value */ - unsigned short overIpmTempErr : 1; /**< Indicates that IPM temperature is over protected value. */ - unsigned short revRotErr : 1; /**< Indicates that motor negtive direction. */ - unsigned short motorStalling : 1; /**< Indicates that rotor is stalling. */ - unsigned short overMotorTempErr : 1; /**< Indicates that three phase currents is out-of-balance. */ - unsigned short posSnsrCommsErr : 1; /**< Indicates that position sensor communication is lost with MCU. */ - unsigned short posSnsrFuncErr : 1; /**< Indicates that position sensor reports function error. */ - unsigned short posSnsrCalibrErr : 1; /**< Indicates that position sensor fails to calibrate itself. */ - unsigned short currOutOfBalance : 1; /**< Indicates that the rotor is reverse rotation.*/ - unsigned short phsOpenErr : 1; /**< Indicates that phase winding(s) is open. */ - unsigned short phsU : 1; /**< Indicates that u phase fails when phsOpenErr occurs. */ - unsigned short phsV : 1; /**< Indicates that v phase fails when phsOpenErr occurs. */ - unsigned short phsW : 1; /**< Indicates that w phase fails when phsOpenErr occurs. */ - unsigned short multiPhs : 1; /**< Indicates that multi-phases fail when phsOpenErr occurs.*/ - } Bit; -} MotorErrStatusReg; - -/**< Protection Status Bit Definition */ -typedef enum { - OCP_ERR_BIT, - OVP_ERR_BIT, - LVP_ERR_BIT, - OTP_IPM_ERR_BIT, - OTP_MOTOR_ERR_BIT, - STALLING_ERR_BIT, - CURR_OUT_BALANCE_ERR_BIT, - POS_COMMS_ERR_BIT, - POS_FUNC_ERR_BIT, - POS_CALIB_ERR_BIT, - REV_ROT_ERR_BIT, - PHS_OPEN_ERR_BIT, - PHS_U_ERR_BIT, - PHS_V_ERR_BIT, - PHS_W_ERR_BIT, - PHS_MULTI_ERR_BIT, -} PROT_ErrBit; - -/**< Motor error protection level. */ -typedef enum { - PROT_LEVEL_0 = 0, - PROT_LEVEL_1, - PROT_LEVEL_2, - PROT_LEVEL_3, - PROT_LEVEL_4 /**< The greater level number, the severe error is. */ -} PROT_Level; - -/** - * @brief Obtains the status of a bit of data. - * @param data data. - * @param bits Number of digits. - * @retval Bit status. - */ -static inline bool GetBit(int data, unsigned short bit) -{ - bool ret; - ret = ((data >> bit) & 1); - return ret; -} - -/** - * @brief Sets the status of a bit of data. - * @param data data. - * @param bit The setted bit. - * @retval None. - */ -static inline void SetBit(int *data, unsigned char bit) -{ - *data |= (1 << bit); -} - -/** - * @brief Clear the status of a bit of data. - * @param data data. - * @param bit The Clear bit. - * @retval None. - */ -static inline void ClearBit(int *data, unsigned char bit) -{ - *data &= ~(1 << bit); -} - -/**< Protection action. */ -void ProtSpo_Exec(APT_RegStruct **aptAddr); - -#endif diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user.c b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user.c deleted file mode 100644 index e97876b7..00000000 --- a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user.c +++ /dev/null @@ -1,137 +0,0 @@ -/** - * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_prot_user.c - * @author MCU Algorithm Team - * @brief This file contains user protection data struct, inquiry api and initialization api definition. - */ - -/* Includes ------------------------------------------------------------------------------------ */ -#include "mcs_prot_user.h" -#include "mcs_prot_user_config.h" -#include "mcs_assert.h" - -/** - * @brief Get motor over current error status. - * @param motorErrStatus Motor error status. - * @retval Error status. - */ -bool IsMotorOverCurrErr(MotorErrStatusReg motorErrStatus) -{ - if (motorErrStatus.Bit.overCurrErr) { - return true; - } else { - return false; - } -} - -/** - * @brief Get motor over dc-link voltage error status. - * @param motorErrStatus Motor error status. - * @retval Error status. - */ -bool IsDcLinkOverVoltErr(MotorErrStatusReg motorErrStatus) -{ - if (motorErrStatus.Bit.overVoltErr) { - return true; - } else { - return false; - } -} - -/** - * @brief Get motor lower dc-link voltage error status. - * @param motorErrStatus Motor error status. - * @retval Error status. - */ -bool IsDcLinkLowerVoltErr(MotorErrStatusReg motorErrStatus) -{ - if (motorErrStatus.Bit.lowerVoltErr) { - return true; - } else { - return false; - } -} - -/** - * @brief Get motor over Ipm temperature error status. - * @param motorErrStatus Motor error status. - * @retval Error status. - */ -bool IsIpmOverTempErr(MotorErrStatusReg motorErrStatus) -{ - if (motorErrStatus.Bit.overIpmTempErr) { - return true; - } else { - return false; - } -} - -/** - * @brief Get motor over Motor temperature error status. - * @param motorErrStatus Motor error status. - * @retval Error status. - */ -bool IsMotorOverTempErr(MotorErrStatusReg motorErrStatus) -{ - if (motorErrStatus.Bit.overMotorTempErr) { - return true; - } else { - return false; - } -} - -/** - * @brief Get motor stalling error status. - * @param motorErrStatus Motor error status. - * @retval Error status. - */ -bool IsMotorStallingErr(MotorErrStatusReg motorErrStatus) -{ - if (motorErrStatus.Bit.motorStalling) { - return true; - } else { - return false; - } -} - -/** - * @brief Clear the motor error status. - * @param motorProt Motor protection handle. - * @retval Error status. - */ -void ClearMotorErrStatus(MotorProtStatus_Handle *motorProt) -{ - MCS_ASSERT_PARAM(motorProt != NULL); - /* Clear the motor error status. */ - motorProt->motorErrStatus.all = 0x00; - OCP_Clear(&motorProt->ocp); - OVP_Clear(&motorProt->ovp); - LVP_Clear(&motorProt->lvp); - OTP_Clear(&motorProt->otp); -} - -/** - * @brief Motor protection function initialization. - * @param motorProt Motor protection handle. - * @retval Error status. - */ -void MotorProt_Init(MotorProtStatus_Handle *motorProt) -{ - MCS_ASSERT_PARAM(motorProt != NULL); - motorProt->motorErrStatus.all = 0x00; -} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user.h b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user.h deleted file mode 100644 index 365110e1..00000000 --- a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user.h +++ /dev/null @@ -1,54 +0,0 @@ -/** - * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_prot_user.h - * @author MCU Algorithm Team - * @brief This file contains user protection data struct, inquiry api and initialization api declaration. - */ - -/* Define to prevent recursive inclusion ------------------------------------------------------- */ -#ifndef McuMagicTag_MCS_PROT_USER_H -#define McuMagicTag_MCS_PROT_USER_H - -/* Includes ------------------------------------------------------------------------------------ */ -#include "mcs_prot_cmm.h" -#include "mcs_curr_prot.h" -#include "mcs_dc_volt_prot.h" -#include "mcs_temp_prot.h" -#include "mcs_motor_stalling.h" -#include "typedefs.h" - -typedef struct { - MotorErrStatusReg motorErrStatus; /**< Motor error status. */ - OCP_Handle ocp; /**< Over current protection. */ - OVP_Handle ovp; /**< Over dc-link voltage protection. */ - LVP_Handle lvp; /**< Lower dc-link voltage protection. */ - OTP_Handle otp; /**< Over IPM temperature protection. */ - STP_Handle stall; /**< Motor stalling protection. */ -} MotorProtStatus_Handle; - -void MotorProt_Init(MotorProtStatus_Handle *motorProt); - -/**< Inquiry motor error status */ -bool IsMotorOverCurrErr(MotorErrStatusReg motorErrStatus); -bool IsDcLinkOverVoltErr(MotorErrStatusReg motorErrStatus); -bool IsDcLinkLowerVoltErr(MotorErrStatusReg motorErrStatus); -bool IsIpmOverTempErr(MotorErrStatusReg motorErrStatus); -bool IsMotorOverTempErr(MotorErrStatusReg motorErrStatus); -bool IsMotorStallingErr(MotorErrStatusReg motorErrStatus); -void ClearMotorErrStatus(MotorProtStatus_Handle *motorProt); -#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user_config.h b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user_config.h deleted file mode 100644 index c0564a22..00000000 --- a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user_config.h +++ /dev/null @@ -1,125 +0,0 @@ -/** - * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_prot_user_config.h - * @author MCU Algorithm Team - * @brief This file contans user macro definition of the protection function. - */ - -/* Define to prevent recursive inclusion ------------------------------------------------------- */ -#ifndef McuMagicTag_MCS_PROT_USER_CONFIG_H -#define McuMagicTag_MCS_PROT_USER_CONFIG_H - -/* Macro definitions --------------------------------------------------------------------------- */ -/**< COMMON */ -/**< Peak phase current (A) of the motor or IPM under continuous operations. */ -#define PROT_MOTOR_RATED_CURR (1.5f) -/**< Only several continuous fault detection can trigger error status. */ -#define PROT_CNT_LIMIT (100) -/**< Only several contunuous none fault dectection can trigger elimination of error status. */ -#define RECY_CNT_LIMIT (10000) -/**< Only several contunuous none fault dectection can trigger elimination of error status. */ -#define OVER_VOLT_RECY_CNT_LIMIT (100) -/**< Only several contunuous none fault dectection can trigger elimination of error status. */ -#define LOWER_VOLT_RECY_CNT_LIMIT (100) - -/**< Over current protection */ -/**< Over current trigger value (A) when in level 1. */ -#define PROT_OVER_CURR_POW_DN1 (2.0f * PROT_MOTOR_RATED_CURR) -/**< Over current trigger value (A) when in level 2. */ -#define PROT_OVER_CURR_POW_DN2 (2.0f * PROT_MOTOR_RATED_CURR) -/**< Over current trigger value (A) when in level 3. */ -#define PROT_OVER_CURR_POW_DN3 (2.0f * PROT_MOTOR_RATED_CURR) -/**< Over current trigger value (A) when in level 4. */ -#define PROT_OVER_CURR_POW_OFF (2.0f * PROT_MOTOR_RATED_CURR) -#define PROT_OVER_CURR_RECY_DELTA (0.0035f) /**< Current gap (A) when recovers from protection status. */ -#define PROT_OVER_CURR_LIMIT1_TIME_SEC (30.0f) /**< 20% overload can last maximum time: 30 sec. */ -#define PROT_OVER_CURR_LIMIT2_TIME_SEC (10.0f) /**< 20% overload can last maximum time: 10 sec. */ -#define PROT_OVER_CURR_LIMIT3_TIME_SEC (2.0f) /**< 20% overload can last maximum time: 2 sec. */ - -/**< Over voltage protection */ -#define PROT_OVER_VOLT_BRK_ON1 (26.0f) /**<处于1级时,直流链路电压触发值(V)过高*/ -#define PROT_OVER_VOLT_BRK_ON2 (27.0f) /**< Over dc-link voltage trigger value (V) when in level 2. */ -#define PROT_OVER_VOLT_BRK_ON3 (28.0f) /**< Over dc-link voltage trigger value (V) when in level 3. */ -#define PROT_OVER_VOLT_BRK_ALL (30.0f) /**< Over dc-link voltage trigger value (V) when in level 4. */ -#define PROT_OVER_VOLT_RECY_DELTA (0.5f) /**<从保护状态恢复时的电压间隙(V)*/ -#define PROT_OVER_VOLT_LIMIT1_TIME_SEC (5.0f) /** 1; One-cycle mode: < 0.5 */ -#define POS_SNSR_IPD_INJ_PERIOD (4) - -#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_curr_prot.c b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_curr_prot.c deleted file mode 100644 index da4e56f8..00000000 --- a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_curr_prot.c +++ /dev/null @@ -1,267 +0,0 @@ -/** - * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_curr_prot.c - * @author MCU Algorithm Team - * @brief This file contains current protecion api definition. - */ - -/* Includes ------------------------------------------------------------------------------------ */ -#include "mcs_curr_prot.h" -#include "mcs_math.h" -#include "mcs_prot_user_config.h" -#include "mcs_assert.h" - -/** - * @brief Initilization over current protection function. - * @param ocp Over current protection handle. - * @param ts Ctrl period (s). - * @retval None. - */ -void OCP_Init(OCP_Handle *ocp, float ts) -{ - MCS_ASSERT_PARAM(ocp != NULL); - MCS_ASSERT_PARAM(ts > 0.0f); - ocp->ts = ts; - OCP_Clear(ocp); - - ocp->protCntLimit = PROT_CNT_LIMIT; - ocp->recyCntLimit = RECY_CNT_LIMIT; - /* Configuring four levels of current protection thresholds. */ - ocp->protValThr[PROT_VAL_THRESHOLD_0] = PROT_OVER_CURR_POW_DN1; - ocp->protValThr[PROT_VAL_THRESHOLD_1] = PROT_OVER_CURR_POW_DN2; - ocp->protValThr[PROT_VAL_THRESHOLD_2] = PROT_OVER_CURR_POW_DN3; - ocp->protValThr[PROT_VAL_THRESHOLD_3] = PROT_OVER_CURR_POW_OFF; - /* Configure the protection limit time. */ - ocp->protLimitTime[PROT_LIMIT_TIME_0] = PROT_OVER_CURR_LIMIT1_TIME_SEC; - ocp->protLimitTime[PROT_LIMIT_TIME_1] = PROT_OVER_CURR_LIMIT2_TIME_SEC; - ocp->protLimitTime[PROT_LIMIT_TIME_2] = PROT_OVER_CURR_LIMIT3_TIME_SEC; - ocp->recyDelta = PROT_OVER_CURR_RECY_DELTA; -} - -/** - * @brief Over current protection detection. - * @param ocp Over current protection handle. - * @param motorErrStatus Motor error status. - * @param idq DQ-axis feedback currents. - * @retval None. - */ -void OCP_Det(OCP_Handle *ocp, MotorErrStatusReg *motorErrStatus, DqAxis idq) -{ - MCS_ASSERT_PARAM(ocp != NULL); - MCS_ASSERT_PARAM(motorErrStatus != NULL); - /* Calculate current amplitude. */ - ocp->currAmp = Sqrt(idq.d * idq.d + idq.q * idq.q); - - /* Check if value goes over threshold for continuous cycles. */ - if (ocp->currAmp < ocp->protValThr[PROT_VAL_THRESHOLD_0]) { - ocp->protCnt = 0; - return; - } - - if (ocp->protCnt < ocp->protCntLimit) { - ocp->protCnt++; - return; - } - - /* Check which protection level should be activated. */ - /* Once enter error status, error status can only be changed by recover api. */ - /* protect level: level 4. */ - if (ocp->currAmp >= ocp->protValThr[PROT_VAL_THRESHOLD_3] && ocp->protLevel < PROT_LEVEL_4) { - ocp->protLevel = PROT_LEVEL_4; - motorErrStatus->Bit.overCurrErr = 1; - ocp->protCnt = 0; - return; - } - - /* Protect level: level 3. */ - if (ocp->currAmp >= ocp->protValThr[PROT_VAL_THRESHOLD_2] && ocp->protLevel < PROT_LEVEL_3) { - ocp->protLevel = PROT_LEVEL_3; - motorErrStatus->Bit.overCurrErr = 1; - ocp->protCnt = 0; - ocp->timer = 0.0f; - return; - } - - /* Protect level: level 2. */ - if (ocp->currAmp >= ocp->protValThr[PROT_VAL_THRESHOLD_1] && ocp->protLevel < PROT_LEVEL_2) { - ocp->protLevel = PROT_LEVEL_2; - motorErrStatus->Bit.overCurrErr = 1; - ocp->protCnt = 0; - ocp->timer = 0.0f; - return; - } - - /* Protect level: level 1. */ - if (ocp->currAmp >= ocp->protValThr[PROT_VAL_THRESHOLD_0] && ocp->protLevel < PROT_LEVEL_1) { - ocp->protLevel = PROT_LEVEL_1; - motorErrStatus->Bit.overCurrErr = 1; - ocp->protCnt = 0; - ocp->timer = 0.0f; - return; - } -} - -/** - * @brief Over current protection execution. - * @param ocp Over current protection handle. - * @param idqRef DQ-axis current references. - * @param aptAddr Three-phase APT address pointer. - * @retval None. - */ -void OCP_Exec(OCP_Handle *ocp, DqAxis *idqRef, APT_RegStruct **aptAddr) -{ - MCS_ASSERT_PARAM(ocp != NULL); - MCS_ASSERT_PARAM(idqRef != NULL); - MCS_ASSERT_PARAM(aptAddr != NULL); - - float id = idqRef->d; - float iq = idqRef->q; - float idqAmp = ocp->currAmp; - /* According to protect level, take corresponding action. */ - switch (ocp->protLevel) { - /* level 4: disable all PWM output. */ - case PROT_LEVEL_4: - /* Disable three-phase pwm output. */ - ProtSpo_Exec(aptAddr); - idqRef->d = 0.0f; - idqRef->q = 0.0f; - break; - - /* level 3: derate speed reference. */ - case PROT_LEVEL_3: - ocp->timer += ocp->ts; - if (ocp->timer > ocp->protLimitTime[PROT_LIMIT_TIME_2]) { - idqRef->d = id / idqAmp * PROT_MOTOR_RATED_CURR; - idqRef->q = iq / idqAmp * PROT_MOTOR_RATED_CURR; - } - break; - - /* level 2: derate speed reference. */ - case PROT_LEVEL_2: - ocp->timer += ocp->ts; - if (ocp->timer > ocp->protLimitTime[PROT_LIMIT_TIME_1]) { - idqRef->d = id / idqAmp * PROT_MOTOR_RATED_CURR; - idqRef->q = iq / idqAmp * PROT_MOTOR_RATED_CURR; - } - break; - - /* level 1: derate speed reference. */ - case PROT_LEVEL_1: - ocp->timer += ocp->ts; - if (ocp->timer > ocp->protLimitTime[PROT_LIMIT_TIME_0]) { - idqRef->d = id / idqAmp * PROT_MOTOR_RATED_CURR; - idqRef->q = iq / idqAmp * PROT_MOTOR_RATED_CURR; - } - break; - - /* level 0: take no protection action. */ - case PROT_LEVEL_0: - break; - - default: - break; - } -} - -/** - * @brief Over current protection recovery. - * @param ocp Over current protection handle. - * @param motorErrStatus Motor error status. - * @retval None. - */ -void OCP_Recy(OCP_Handle *ocp, MotorErrStatusReg *motorErrStatus) -{ - MCS_ASSERT_PARAM(ocp != NULL); - /* If not under error state, just return without any operation. */ - if (!motorErrStatus->Bit.overCurrErr) { - return; - } - - /* Calculate current amplitude. */ - float currAmp = ocp->currAmp; - - /* According to protection level, take corresponding recovery action. */ - switch (ocp->protLevel) { - /* level 4 */ - case PROT_LEVEL_4: - if (currAmp < ocp->protValThr[PROT_VAL_THRESHOLD_3] - ocp->recyDelta) { - ocp->recyCnt++; - if (ocp->recyCnt > ocp->recyCntLimit) { - ocp->protLevel = PROT_LEVEL_3; - ocp->recyCnt = 0; - } - } - break; - - /* level 3 */ - case PROT_LEVEL_3: - if (currAmp < ocp->protValThr[PROT_VAL_THRESHOLD_2] - ocp->recyDelta) { - ocp->recyCnt++; - if (ocp->recyCnt > ocp->recyCntLimit) { - ocp->protLevel = PROT_LEVEL_2; - ocp->recyCnt = 0; - } - } - break; - - /* level 2 */ - case PROT_LEVEL_2: - if (currAmp < ocp->protValThr[PROT_VAL_THRESHOLD_1] - ocp->recyDelta) { - ocp->recyCnt++; - if (ocp->recyCnt > ocp->recyCntLimit) { - ocp->protLevel = PROT_LEVEL_1; - ocp->recyCnt = 0; - } - } - break; - - /* level 1 */ - case PROT_LEVEL_1: - if (currAmp < ocp->protValThr[PROT_VAL_THRESHOLD_0] - ocp->recyDelta) { - ocp->recyCnt++; - if (ocp->recyCnt > ocp->recyCntLimit) { - ocp->protLevel = PROT_LEVEL_0; - ocp->recyCnt = 0; - } - } - break; - - /* level 0 */ - case PROT_LEVEL_0: - motorErrStatus->Bit.overCurrErr = 0; - break; - - default: - break; - } -} - -/** - * @brief Over current protection error status clear. - * @param ocp Over current protection handle. - * @retval None. - */ -void OCP_Clear(OCP_Handle *ocp) -{ - MCS_ASSERT_PARAM(ocp != NULL); - /* Clear the history value. */ - ocp->protCnt = 0; - ocp->protLevel = PROT_LEVEL_0; - ocp->recyCnt = 0; - ocp->timer = 0.0f; -} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_curr_prot.h b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_curr_prot.h deleted file mode 100644 index e9916fe4..00000000 --- a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_curr_prot.h +++ /dev/null @@ -1,52 +0,0 @@ -/** - * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_curr_prot.h - * @author MCU Algorithm Team - * @brief This file contains current protection data struct and api declaration. - */ - -/* Define to prevent recursive inclusion ------------------------------------- */ -#ifndef McuMagicTag_MCS_CURR_PROT_H -#define McuMagicTag_MCS_CURR_PROT_H - -/* Includes ------------------------------------------------------------------------------------ */ -#include "mcs_prot_cmm.h" -#include "mcs_typedef.h" -#include "apt_ip.h" - -typedef struct { - unsigned int protCnt; - unsigned int recyCnt; - unsigned int protCntLimit; - unsigned int recyCntLimit; - float currAmp; - float protValThr[PROT_VAL_THRESHOLD_NUMS]; /* from low current level to high. */ - float protLimitTime[PROT_LIMIT_TIME_NUMS]; /* from level 3 to level 1. */ - float recyDelta; - float timer; - float ts; - PROT_Level protLevel; -} OCP_Handle; - -void OCP_Init(OCP_Handle *ocp, float ts); -void OCP_Det(OCP_Handle *ocp, MotorErrStatusReg *motorErrStatus, DqAxis idq); -void OCP_Exec(OCP_Handle *ocp, DqAxis *idqRef, APT_RegStruct **aptAddr); -void OCP_Recy(OCP_Handle *ocp, MotorErrStatusReg *motorErrStatus); -void OCP_Clear(OCP_Handle *ocp); - -#endif diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_dc_volt_prot.c b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_dc_volt_prot.c deleted file mode 100644 index b0541a2e..00000000 --- a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_dc_volt_prot.c +++ /dev/null @@ -1,495 +0,0 @@ -/** - * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_dc_volt_prot.c - * @author MCU Algorithm Team - * @brief This file contains dc-link voltage protection api declaration. - */ - -/* Includes ------------------------------------------------------------------------------------ */ -#include "mcs_dc_volt_prot.h" -#include "mcs_prot_user_config.h" -#include "mcs_assert.h" - -/** - * @brief Initilization over dc-link voltage protection function. - * @param ovp Over dc-link voltage protection handle. - * @param ts Ctrl period (s). - * @retval None. - */ -void OVP_Init(OVP_Handle *ovp, float ts) -{ - MCS_ASSERT_PARAM(ovp != NULL); - MCS_ASSERT_PARAM(ts > 0.0f); - ovp->ts = ts; - OVP_Clear(ovp); - ovp->protCntLimit = PROT_CNT_LIMIT; - ovp->recyCntLimit = OVER_VOLT_RECY_CNT_LIMIT; - /* Configuring four levels of overvoltage protection thresholds. */ - ovp->protValThr[PROT_VAL_THRESHOLD_0] = PROT_OVER_VOLT_BRK_ON1; - ovp->protValThr[PROT_VAL_THRESHOLD_1] = PROT_OVER_VOLT_BRK_ON2; - ovp->protValThr[PROT_VAL_THRESHOLD_2] = PROT_OVER_VOLT_BRK_ON3; - ovp->protValThr[PROT_VAL_THRESHOLD_3] = PROT_OVER_VOLT_BRK_ALL; - /* Configure the protection limit time. */ - ovp->protLimitTime[PROT_LIMIT_TIME_0] = PROT_OVER_VOLT_LIMIT1_TIME_SEC; - ovp->protLimitTime[PROT_LIMIT_TIME_1] = PROT_OVER_VOLT_LIMIT2_TIME_SEC; - ovp->protLimitTime[PROT_LIMIT_TIME_2] = PROT_OVER_VOLT_LIMIT3_TIME_SEC; - ovp->recyDelta = PROT_OVER_VOLT_RECY_DELTA; -} - -/** - * @brief Initilization lower dc-link voltage protection function. - * @param lvp Lower dc-link voltage protection handle. - * @param ts Ctrl period (s). - * @retval None. - */ -void LVP_Init(LVP_Handle *lvp, float ts) -{ - MCS_ASSERT_PARAM(lvp != NULL); - MCS_ASSERT_PARAM(ts > 0.0f); - - lvp->ts = ts; - LVP_Clear(lvp); - - lvp->protCntLimit = PROT_CNT_LIMIT; - lvp->recyCntLimit = LOWER_VOLT_RECY_CNT_LIMIT; - /* Configuring four levels of lower voltage protection thresholds. */ - lvp->protValThr[PROT_VAL_THRESHOLD_0] = PROT_LOWER_VOLT_POW_DN1; - lvp->protValThr[PROT_VAL_THRESHOLD_1] = PROT_LOWER_VOLT_POW_DN2; - lvp->protValThr[PROT_VAL_THRESHOLD_2] = PROT_LOWER_VOLT_POW_DN3; - lvp->protValThr[PROT_VAL_THRESHOLD_3] = PROT_LOWER_VOLT_POW_OFF; - /* Configure the protection limit time. */ - lvp->protLimitTime[PROT_LIMIT_TIME_0] = PROT_LOWER_VOLT_LIMIT1_TIME_SEC; - lvp->protLimitTime[PROT_LIMIT_TIME_1] = PROT_LOWER_VOLT_LIMIT2_TIME_SEC; - lvp->protLimitTime[PROT_LIMIT_TIME_2] = PROT_LOWER_VOLT_LIMIT3_TIME_SEC; - lvp->recyDelta = PROT_OVER_VOLT_RECY_DELTA; -} - -/** - * @brief Over dc-link voltage protection detection. - * @param ovp Over dc-link voltage protection handle. - * @param motorErrStatus Motor error status. - * @param udc DC-link voltage feedback (V). - * @retval None. - */ -void OVP_Det(OVP_Handle *ovp, MotorErrStatusReg *motorErrStatus, float udc) -{ - MCS_ASSERT_PARAM(ovp != NULL); - MCS_ASSERT_PARAM(motorErrStatus != NULL); - MCS_ASSERT_PARAM(udc > 0.0f); - /* Check if value goes over threshold for continuous cycles. */ - if (udc < ovp->protValThr[PROT_VAL_THRESHOLD_0]) { - ovp->protCnt = 0; - return; - } - - if (ovp->protCnt < ovp->protCntLimit) { - ovp->protCnt++; - return; - } - - /* Check which protection level should be activated. */ - /* Once enter error status, error status can only be changed by recover api. */ - /* protect level: level 4. */ - if (udc >= ovp->protValThr[PROT_VAL_THRESHOLD_3] && ovp->protLevel < PROT_LEVEL_4) { - ovp->protLevel = PROT_LEVEL_4; - motorErrStatus->Bit.overVoltErr = 1; - ovp->protCnt = 0; - return; - } - - /* Protect level: level 3. */ - if (udc >= ovp->protValThr[PROT_VAL_THRESHOLD_2] && ovp->protLevel < PROT_LEVEL_3) { - ovp->protLevel = PROT_LEVEL_3; - motorErrStatus->Bit.overVoltErr = 1; - ovp->protCnt = 0; - ovp->timer = 0.0f; - return; - } - - /* Protect level: level 2. */ - if (udc >= ovp->protValThr[PROT_VAL_THRESHOLD_1] && ovp->protLevel < PROT_LEVEL_2) { - ovp->protLevel = PROT_LEVEL_2; - motorErrStatus->Bit.overVoltErr = 1; - ovp->protCnt = 0; - ovp->timer = 0.0f; - return; - } - - /* Protect level: level 1. */ - if (udc >= ovp->protValThr[PROT_VAL_THRESHOLD_0] && ovp->protLevel < PROT_LEVEL_1) { - ovp->protLevel = PROT_LEVEL_1; - motorErrStatus->Bit.overVoltErr = 1; - ovp->protCnt = 0; - ovp->timer = 0.0f; - return; - } -} - -/** - * @brief Lower dc-link voltage protection detection. - * @param lvp Lower dc-link voltage protection handle. - * @param motorErrStatus Motor error status. - * @param udc DC-link voltage feedback (V). - * @retval None. - */ -void LVP_Det(LVP_Handle *lvp, MotorErrStatusReg *motorErrStatus, float udc) -{ - MCS_ASSERT_PARAM(lvp != NULL); - MCS_ASSERT_PARAM(motorErrStatus != NULL); - MCS_ASSERT_PARAM(udc > 0.0f); - /* Check if value goes over threshold for continuous cycles. */ - if (udc > lvp->protValThr[PROT_VAL_THRESHOLD_0]) { - lvp->protCnt = 0; - return; - } - - if (lvp->protCnt < lvp->protCntLimit) { - lvp->protCnt++; - return; - } - - /* Check which protection level should be activated. */ - /* Once enter error status, error status can only be changed by recover api. */ - /* protect level: level 4. */ - if (udc <= lvp->protValThr[PROT_VAL_THRESHOLD_3] && lvp->protLevel < PROT_LEVEL_4) { - lvp->protLevel = PROT_LEVEL_4; - motorErrStatus->Bit.lowerVoltErr = 1; - lvp->protCnt = 0; - return; - } - - /* Protect level: level 3. */ - if (udc <= lvp->protValThr[PROT_VAL_THRESHOLD_2] && lvp->protLevel < PROT_LEVEL_3) { - lvp->protLevel = PROT_LEVEL_3; - motorErrStatus->Bit.lowerVoltErr = 1; - lvp->protCnt = 0; - lvp->timer = 0.0f; - return; - } - - /* Protect level: level 2. */ - if (udc <= lvp->protValThr[PROT_VAL_THRESHOLD_1] && lvp->protLevel < PROT_LEVEL_2) { - lvp->protLevel = PROT_LEVEL_2; - motorErrStatus->Bit.lowerVoltErr = 1; - lvp->protCnt = 0; - lvp->timer = 0.0f; - return; - } - - /* Protect level: level 1. */ - if (udc <= lvp->protValThr[PROT_VAL_THRESHOLD_0] && lvp->protLevel < PROT_LEVEL_1) { - lvp->protLevel = PROT_LEVEL_1; - motorErrStatus->Bit.lowerVoltErr = 1; - lvp->protCnt = 0; - lvp->timer = 0.0f; - return; - } -} - -/** - * @brief Over dc-link voltage protection execution. - * @param ovp Over dc-link voltage protection handle. - * @param duty Brake loop output duty (0-1). - * @param aptAddr Three-phase APT address pointer. - * @retval None. - */ -void OVP_Exec(OVP_Handle *ovp, float *duty, APT_RegStruct **aptAddr) -{ - MCS_ASSERT_PARAM(ovp != NULL); - MCS_ASSERT_PARAM(duty != NULL); - MCS_ASSERT_PARAM(aptAddr != NULL); - /* According to protect level, take corresponding action. */ - switch (ovp->protLevel) { - /* level 4: brake loop duty maximum. */ - case PROT_LEVEL_4: - *duty = PROT_OVER_VOLT_BRK_DUTY4; - /* Disable three-phase pwm output. */ - ProtSpo_Exec(aptAddr); - break; - - /* level 3: brake loop duty level 3. */ - case PROT_LEVEL_3: - *duty = PROT_OVER_VOLT_BRK_DUTY2; - ovp->timer += ovp->ts; - if (ovp->timer > ovp->protLimitTime[PROT_LIMIT_TIME_2]) { - *duty = PROT_OVER_VOLT_BRK_DUTY3; - } - break; - - /* level 2: brake loop duty level 2. */ - case PROT_LEVEL_2: - *duty = PROT_OVER_VOLT_BRK_DUTY1; - ovp->timer += ovp->ts; - if (ovp->timer > ovp->protLimitTime[PROT_LIMIT_TIME_1]) { - *duty = PROT_OVER_VOLT_BRK_DUTY2; - } - break; - - /* level 1: brake loop duty level 1. */ - case PROT_LEVEL_1: - ovp->timer += ovp->ts; - if (ovp->timer > ovp->protLimitTime[PROT_LIMIT_TIME_0]) { - *duty = PROT_OVER_VOLT_BRK_DUTY1; - } - break; - - /* level 0: take no protection action. */ - case PROT_LEVEL_0: - break; - - default: - break; - } - return; -} - -/** - * @brief Lower dc-link voltage protection execution. - * @param lvp Lower dc-link voltage protection handle. - * @param spdRef Speed Reference (Hz). - * @param aptAddr Three-phase APT address pointer. - * @retval None. - */ -void LVP_Exec(LVP_Handle *lvp, float *spdRef, APT_RegStruct **aptAddr) -{ - MCS_ASSERT_PARAM(lvp != NULL); - MCS_ASSERT_PARAM(spdRef != NULL); - MCS_ASSERT_PARAM(aptAddr != NULL); - /* According to protect level, take corresponding action. */ - switch (lvp->protLevel) { - /* level 4: disable all PWM output. */ - case PROT_LEVEL_4: - /* Disable three-phase pwm output. */ - ProtSpo_Exec(aptAddr); - *spdRef *= 0.0f; - break; - - /* level 3: derate speed reference. */ - case PROT_LEVEL_3: - *spdRef *= PROT_POW_DN2_PCT; - lvp->timer += lvp->ts; - if (lvp->timer > lvp->protLimitTime[PROT_LIMIT_TIME_2]) { - *spdRef *= PROT_POW_DN3_PCT; - } - break; - - /* level 2: derate speed reference. */ - case PROT_LEVEL_2: - *spdRef *= PROT_POW_DN1_PCT; - lvp->timer += lvp->ts; - if (lvp->timer > lvp->protLimitTime[PROT_LIMIT_TIME_1]) { - *spdRef *= PROT_POW_DN2_PCT; - } - break; - - /* level 1: derate speed reference. */ - case PROT_LEVEL_1: - lvp->timer += lvp->ts; - if (lvp->timer > lvp->protLimitTime[PROT_LIMIT_TIME_0]) { - *spdRef *= PROT_POW_DN1_PCT; - } - break; - - /* level 0: take no protection action. */ - case PROT_LEVEL_0: - break; - - default: - break; - } - return; -} - -/** - * @brief Over dc-link voltage protection recovery. - * @param ovp Over dc-link voltage protection handle. - * @param motorErrStatus Motor error status. - * @param udc DC-link voltage (V). - * @retval None. - */ -void OVP_Recy(OVP_Handle *ovp, MotorErrStatusReg *motorErrStatus, float udc) -{ - MCS_ASSERT_PARAM(ovp != NULL); - /* If not under error state, just return without any operation. */ - if (!motorErrStatus->Bit.overVoltErr) { - return; - } - - /* According to protection level, take corresponding recovery action. */ - switch (ovp->protLevel) { - /* level 4 */ - case PROT_LEVEL_4: - if (udc < ovp->protValThr[PROT_VAL_THRESHOLD_3] - ovp->recyDelta) { - ovp->recyCnt++; - if (ovp->recyCnt > ovp->recyCntLimit) { - ovp->protLevel = PROT_LEVEL_3; - ovp->recyCnt = 0; - } - } - break; - - /* level 3 */ - case PROT_LEVEL_3: - /* If the dc-link voltage is less than threshold 2, level-2 protection is restored. */ - if (udc < ovp->protValThr[PROT_VAL_THRESHOLD_2] - ovp->recyDelta) { - ovp->recyCnt++; - if (ovp->recyCnt > ovp->recyCntLimit) { - ovp->protLevel = PROT_LEVEL_2; - ovp->recyCnt = 0; - } - } - break; - - /* level 2 */ - case PROT_LEVEL_2: - /* If the dc-link voltage is less than threshold 1, level-1 protection is restored. */ - if (udc < ovp->protValThr[PROT_VAL_THRESHOLD_1] - ovp->recyDelta) { - ovp->recyCnt++; - if (ovp->recyCnt > ovp->recyCntLimit) { - ovp->protLevel = PROT_LEVEL_1; - ovp->recyCnt = 0; - } - } - break; - - /* level 1 */ - case PROT_LEVEL_1: - /* If the dc-link voltage is less than threshold 0, level-0 protection is restored. */ - if (udc < ovp->protValThr[PROT_VAL_THRESHOLD_0] - ovp->recyDelta) { - ovp->recyCnt++; - if (ovp->recyCnt > ovp->recyCntLimit) { - ovp->protLevel = PROT_LEVEL_0; - ovp->recyCnt = 0; - } - } - break; - /* level 0 Fault-free state. */ - case PROT_LEVEL_0: - motorErrStatus->Bit.overVoltErr = 0; - break; - - default: - break; - } - return; -} - -/** - * @brief Lower dc-link voltage protection recovery. - * @param lvp Lower dc-link voltage protection handle. - * @param motorErrStatus Motor error status. - * @param udc DC-link voltage (V). - * @retval None. - */ -void LVP_Recy(LVP_Handle *lvp, MotorErrStatusReg *motorErrStatus, float udc) -{ - MCS_ASSERT_PARAM(lvp != NULL); - /* If not under error state, just return without any operation. */ - if (!motorErrStatus->Bit.lowerVoltErr) { - return; - } - - /* According to protection level, take corresponding recovery action. */ - switch (lvp->protLevel) { - /* level 4 */ - case PROT_LEVEL_4: - /* If the dc-link voltage is greater than threshold 3, level-3 protection is restored. */ - if (udc > lvp->protValThr[PROT_VAL_THRESHOLD_3] + lvp->recyDelta) { - lvp->recyCnt++; - if (lvp->recyCnt > lvp->recyCntLimit) { - lvp->protLevel = PROT_LEVEL_3; - lvp->recyCnt = 0; - } - } - break; - - /* level 3 */ - case PROT_LEVEL_3: - /* If the dc-link voltage is greater than threshold 2, level-2 protection is restored. */ - if (udc > lvp->protValThr[PROT_VAL_THRESHOLD_2] + lvp->recyDelta) { - lvp->recyCnt++; - if (lvp->recyCnt > lvp->recyCntLimit) { - lvp->protLevel = PROT_LEVEL_2; - lvp->recyCnt = 0; - } - } - break; - - /* level 2 */ - case PROT_LEVEL_2: - /* If the dc-link voltage is greater than threshold 1, level-1 protection is restored. */ - if (udc > lvp->protValThr[PROT_VAL_THRESHOLD_1] + lvp->recyDelta) { - lvp->recyCnt++; - if (lvp->recyCnt > lvp->recyCntLimit) { - lvp->protLevel = PROT_LEVEL_1; - lvp->recyCnt = 0; - } - } - break; - - /* level 1 */ - case PROT_LEVEL_1: - /* If the dc-link voltage is greater than threshold 0, level-0 protection is restored. */ - if (udc > lvp->protValThr[PROT_VAL_THRESHOLD_0] + lvp->recyDelta) { - lvp->recyCnt++; - if (lvp->recyCnt > lvp->recyCntLimit) { - lvp->protLevel = PROT_LEVEL_0; - lvp->recyCnt = 0; - } - } - break; - /* level 0 Fault-free state. */ - case PROT_LEVEL_0: - motorErrStatus->Bit.lowerVoltErr = 0; - break; - - default: - break; - } - return; -} - -/** - * @brief Over dc-link voltage protection error status clear. - * @param ovp Over voltage protection handle. - * @retval None. - */ -void OVP_Clear(OVP_Handle *ovp) -{ - MCS_ASSERT_PARAM(ovp != NULL); - /* Clear the history value. */ - ovp->protCnt = 0; - ovp->protLevel = PROT_LEVEL_0; - ovp->recyCnt = 0; - ovp->timer = 0.0f; -} - -/** - * @brief Lower dc-link voltage protection error status clear. - * @param lvp Lower voltage protection handle. - * @retval None. - */ -void LVP_Clear(LVP_Handle *lvp) -{ - MCS_ASSERT_PARAM(lvp != NULL); - /* Clear the history value. */ - lvp->protCnt = 0; - lvp->protLevel = PROT_LEVEL_0; - lvp->recyCnt = 0; - lvp->timer = 0.0f; -} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_dc_volt_prot.h b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_dc_volt_prot.h deleted file mode 100644 index 0435156a..00000000 --- a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_dc_volt_prot.h +++ /dev/null @@ -1,69 +0,0 @@ -/** - * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_dc_volt_prot.h - * @author MCU Algorithm Team - * @brief This file contains dc-link voltage protection data struct and api declaration. - */ - -/* Define to prevent recursive inclusion ------------------------------------------------------- */ -#ifndef McuMagicTag_MCS_DC_VOLT_PROT_H -#define McuMagicTag_MCS_DC_VOLT_PROT_H - -/* Includes ------------------------------------------------------------------------------------ */ -#include "mcs_prot_cmm.h" -#include "apt_ip.h" - -typedef struct { - unsigned int protCnt; - unsigned int recyCnt; - unsigned int protCntLimit; - unsigned int recyCntLimit; - float protValThr[PROT_VAL_THRESHOLD_NUMS]; /* from low voltage level to high. */ - float protLimitTime[PROT_LIMIT_TIME_NUMS]; /* from level 3 to level 1. */ - float recyDelta; - float timer; - float ts; - PROT_Level protLevel; -} OVP_Handle; - -typedef struct { - unsigned int protCnt; - unsigned int recyCnt; - unsigned int protCntLimit; - unsigned int recyCntLimit; - float protValThr[PROT_VAL_THRESHOLD_NUMS]; /* from high voltage level to low. */ - float protLimitTime[PROT_LIMIT_TIME_NUMS]; /* from level 3 to level 1. */ - float recyDelta; - float timer; - float ts; - PROT_Level protLevel; -} LVP_Handle; - -void OVP_Init(OVP_Handle *ovp, float ts); -void OVP_Det(OVP_Handle *ovp, MotorErrStatusReg *motorErrStatus, float udc); -void OVP_Exec(OVP_Handle *ovp, float *duty, APT_RegStruct **aptAddr); -void OVP_Recy(OVP_Handle *ovp, MotorErrStatusReg *motorErrStatus, float udc); -void OVP_Clear(OVP_Handle *ovp); - -void LVP_Init(LVP_Handle *lvp, float ts); -void LVP_Det(LVP_Handle *lvp, MotorErrStatusReg *motorErrStatus, float udc); -void LVP_Exec(LVP_Handle *lvp, float *spdRef, APT_RegStruct **aptAddr); -void LVP_Recy(LVP_Handle *lvp, MotorErrStatusReg *motorErrStatus, float udc); -void LVP_Clear(LVP_Handle *lvp); - -#endif diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_motor_stalling.c b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_motor_stalling.c deleted file mode 100644 index 9dee4d2d..00000000 --- a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_motor_stalling.c +++ /dev/null @@ -1,112 +0,0 @@ -/** - * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_motor_stalling.c - * @author MCU Algorithm Team - * @brief This file contains motor stalling protection data struct and api definition. - */ - -/* Includes ------------------------------------------------------------------------------------ */ -#include "mcs_motor_stalling.h" -#include "mcs_prot_user_config.h" -#include "mcs_math.h" -#include "mcs_math_const.h" -#include "mcs_assert.h" - -/** - * @brief Initilization motor stalling protection function. - * @param stall Motor stalling handle. - * @param ts Ctrl period (s). - * @param currLimit The current amplitude that triggers fault. (A). - * @param spdLimit The speed amplitude that triggers fault. (Hz). - * @param timeLimit The threshold time that current amplitude over the limit (s). - * @retval None. - */ -void STP_Init(STP_Handle *stall, float ts, float currLimit, float spdLimit, float timeLimit) -{ - MCS_ASSERT_PARAM(stall != NULL); - MCS_ASSERT_PARAM(ts > 0.0f); - MCS_ASSERT_PARAM(currLimit > 0.0f); - MCS_ASSERT_PARAM(spdLimit > 0.0f); - MCS_ASSERT_PARAM(timeLimit > 0.0f); - /* Configuring parameters for stalling detection. */ - stall->ts = ts; - /* Current threshold and speed threshold for stalling fault. */ - stall->currAmpLimit = currLimit; - stall->spdLimit = spdLimit; - stall->timeLimit = timeLimit; - stall->timer = 0.0f; -} - -/** - * @brief Motor stalling detection. - * @param stall Motor stalling handle. - * @param motorErrStatus Motor error status. - * @param spd Speed feedback (Hz). - * @param idq Dq-axis current feedback (A). - * @retval None. - */ -void STP_Det_ByCurrSpd(STP_Handle *stall, MotorErrStatusReg *motorErrStatus, float spd, DqAxis idq) -{ - MCS_ASSERT_PARAM(stall != NULL); - MCS_ASSERT_PARAM(motorErrStatus != NULL); - MCS_ASSERT_PARAM(Abs(spd) >= 0.0f); - /* Calculate current amplitude. */ - float currAmp = Sqrt(idq.d * idq.d + idq.q * idq.q); - float spdAbs = Abs(spd); - /* Check if value goes over threshold for continuous cycles. */ - if (currAmp < stall->currAmpLimit || spdAbs > stall->spdLimit) { - stall->timer = 0.0f; - return; - } - /* Time accumulation. */ - if (stall->timer < stall->timeLimit) { - stall->timer += stall->ts; - return; - } - motorErrStatus->Bit.motorStalling = 1; -} - -/** - * @brief Motor stalling protection execution. - * @param motorErrStatus Motor error status. - * @param aptAddr Three-phase APT address pointer. - * @retval None. - */ -void STP_Exec(MotorErrStatusReg *motorErrStatus, APT_RegStruct **aptAddr) -{ - MCS_ASSERT_PARAM(motorErrStatus != NULL); - MCS_ASSERT_PARAM(aptAddr != NULL); - if (motorErrStatus->Bit.motorStalling == 0) { - return; - } - - /* Disable three-phase pwm output. */ - ProtSpo_Exec(aptAddr); - return; -} - -/** - * @brief Motor stalling protection error status clear. - * @param stall Motor stalling handle. - * @retval None. - */ -void STP_Clear(STP_Handle *stall) -{ - MCS_ASSERT_PARAM(stall != NULL); - stall->timer = 0.0f; -} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_motor_stalling.h b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_motor_stalling.h deleted file mode 100644 index a1d1e4be..00000000 --- a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_motor_stalling.h +++ /dev/null @@ -1,45 +0,0 @@ -/** - * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_motor_stalling.h - * @author MCU Algorithm Team - * @brief This file contains motor stalling protection data struct and api declaration. - */ - -/* Define to prevent recursive inclusion ------------------------------------- */ -#ifndef McuMagicTag_MCS_MOTOR_STALLING_PROT_H -#define McuMagicTag_MCS_MOTOR_STALLING_PROT_H - -/* Includes ------------------------------------------------------------------------------------ */ -#include "mcs_prot_cmm.h" -#include "apt_ip.h" -#include "mcs_typedef.h" - -typedef struct { - float currAmpLimit; /**< Feedback current higher than this value triggers fault. (A). */ - float spdLimit; /**< Feedback speed lower than this value triggers fault (Hz). */ - float timeLimit; /**< The threshold time that current and speed feedback over ranges (s). */ - float timer; /**< Timer to get speed and current over range time. */ - float ts; /**< Ctrl period (s). */ -} STP_Handle; - -void STP_Init(STP_Handle *stall, float ts, float currLimit, float spdLimit, float timeLimit); -void STP_Det_ByCurrSpd(STP_Handle *stall, MotorErrStatusReg *motorErrStatus, float spd, DqAxis idq); -void STP_Exec(MotorErrStatusReg *motorErrStatus, APT_RegStruct **aptAddr); -void STP_Clear(STP_Handle *stall); - -#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_temp_prot.c b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_temp_prot.c deleted file mode 100644 index 184ba4a3..00000000 --- a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_temp_prot.c +++ /dev/null @@ -1,262 +0,0 @@ -/** - * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_temp_prot.c - * @author MCU Algorithm Team - * @brief This file contains over temperature protection api definition. - */ - -/* Includes ------------------------------------------------------------------------------------ */ -#include "mcs_temp_prot.h" -#include "mcs_prot_user_config.h" -#include "mcs_assert.h" - -/** - * @brief Initilization over temperation protection function. - * @param otp Over temperature protection handle. - * @param ts Ctrl period. - * @retval None. - */ -void OTP_Init(OTP_Handle *otp, float ts) -{ - MCS_ASSERT_PARAM(otp != NULL); - MCS_ASSERT_PARAM(ts > 0.0f); - otp->ts = ts; - otp->protCntLimit = PROT_CNT_LIMIT; - otp->recyCntLimit = RECY_CNT_LIMIT; - /* Configuring the temperature protection threshold. */ - otp->protValThr[PROT_VAL_THRESHOLD_0] = PROT_OVER_IPM_TEMP_POW_DN1; - otp->protValThr[PROT_VAL_THRESHOLD_1] = PROT_OVER_IPM_TEMP_POW_DN2; - otp->protValThr[PROT_VAL_THRESHOLD_2] = PROT_OVER_IPM_TEMP_POW_DN3; - otp->protValThr[PROT_VAL_THRESHOLD_3] = PROT_OVER_IPM_TEMP_POW_OFF; - /* Configuring the protection limiting time. */ - otp->protLimitTime[PROT_LIMIT_TIME_0] = PROT_OVER_TEMP_LIMIT1_TIME_SEC; - otp->protLimitTime[PROT_LIMIT_TIME_1] = PROT_OVER_TEMP_LIMIT2_TIME_SEC; - otp->protLimitTime[PROT_LIMIT_TIME_2] = PROT_OVER_TEMP_LIMIT3_TIME_SEC; - otp->recyDelta = PROT_OVER_IPM_TEMP_RECY_DELTA; - OTP_Clear(otp); -} - -/** - * @brief Over temperatre protection detection. - * @param otp Over temperature protection handle. - * @param tempErrBit Temperature error status bit. - * @param idq DQ-axis feedback currents. - * @retval None. - */ -void OTP_Det(OTP_Handle *otp, MotorErrStatusReg *motorErrStatus, PROT_ErrBit protBit, float temp) -{ - MCS_ASSERT_PARAM(otp != NULL); - MCS_ASSERT_PARAM(motorErrStatus != NULL); - MCS_ASSERT_PARAM(temp > 0.0f); - /* Check if value goes over threshold for continuous cycles. */ - if (temp < otp->protValThr[PROT_VAL_THRESHOLD_0]) { - otp->protCnt = 0; - ClearBit(&motorErrStatus->all, protBit); - return; - } - - if (otp->protCnt < otp->protCntLimit) { - otp->protCnt++; - return; - } - - /* Check which protection level should be activated. */ - /* Once enter error status, error status can only be changed by recover api. */ - /* protect level: level 4. */ - if (temp >= otp->protValThr[PROT_VAL_THRESHOLD_3] && otp->protLevel < PROT_LEVEL_4) { - otp->protLevel = PROT_LEVEL_4; - SetBit(&motorErrStatus->all, protBit); - otp->protCnt = 0; - return; - } - - /* Protect level: level 3. */ - if (temp >= otp->protValThr[PROT_VAL_THRESHOLD_2] && otp->protLevel < PROT_LEVEL_3) { - otp->protLevel = PROT_LEVEL_3; - SetBit(&motorErrStatus->all, protBit); - otp->protCnt = 0; - otp->timer = 0.0f; - return; - } - - /* Protect level: level 2. */ - if (temp >= otp->protValThr[PROT_VAL_THRESHOLD_1] && otp->protLevel < PROT_LEVEL_2) { - otp->protLevel = PROT_LEVEL_2; - SetBit(&motorErrStatus->all, protBit); - otp->protCnt = 0; - otp->timer = 0.0f; - return; - } - - /* Protect level: level 1. */ - if (temp >= otp->protValThr[PROT_VAL_THRESHOLD_0] && otp->protLevel < PROT_LEVEL_1) { - otp->protLevel = PROT_LEVEL_1; - SetBit(&motorErrStatus->all, protBit); - otp->protCnt = 0; - otp->timer = 0.0f; - return; - } -} - -/** - * @brief Over temperature protection execution. - * @param otp Over temperature protection handle. - * @param spdRef Speed reference (Hz). - * @param aptAddr Three-phase APT address pointer. - * @retval None. - */ -void OTP_Exec(OTP_Handle *otp, float *spdRef, APT_RegStruct **aptAddr) -{ - MCS_ASSERT_PARAM(otp != NULL); - MCS_ASSERT_PARAM(spdRef != NULL); - MCS_ASSERT_PARAM(aptAddr != NULL); - /* According to protect level, take corresponding action. */ - switch (otp->protLevel) { - /* level 4: disable all PWM output. */ - case PROT_LEVEL_4: - /* Disable three-phase pwm output. */ - ProtSpo_Exec(aptAddr); - *spdRef *= 0.0f; - break; - - /* level 3: derate speed reference. */ - case PROT_LEVEL_3: - *spdRef *= PROT_POW_DN2_PCT; - otp->timer += otp->ts; - if (otp->timer > otp->protLimitTime[PROT_LIMIT_TIME_2]) { - *spdRef *= PROT_POW_DN3_PCT; - } - break; - - /* level 2: derate speed reference. */ - case PROT_LEVEL_2: - /* Reducte motor speed to level 2 */ - *spdRef *= PROT_POW_DN1_PCT; - otp->timer += otp->ts; - if (otp->timer > otp->protLimitTime[PROT_LIMIT_TIME_1]) { - *spdRef *= PROT_POW_DN2_PCT; - } - break; - - /* level 0: take no protection action. */ - case PROT_LEVEL_0: - break; - - case PROT_LEVEL_1: - /* Reducte motor speed to level 0 */ - otp->timer += otp->ts; - if (otp->timer > otp->protLimitTime[PROT_LIMIT_TIME_0]) { - /* level 1: derate speed reference. */ - *spdRef *= PROT_POW_DN1_PCT; - } - break; - - default: - break; - } - return; -} - -/** - * @brief Over temperature protection recovery. - * @param otp Over temperature protection handle. - * @param tempErrBit Temperature error status bit. - * @param temp Temperature (celsius). - * @retval None. - */ -void OTP_Recy(OTP_Handle *otp, MotorErrStatusReg *motorErrStatus, PROT_ErrBit protBit, float temp) -{ - MCS_ASSERT_PARAM(otp != NULL); - MCS_ASSERT_PARAM(motorErrStatus != NULL); - MCS_ASSERT_PARAM(temp > 0.0f); - /* If not under error state, just return without any operation. */ - if (otp->protLevel == PROT_LEVEL_0) { - motorErrStatus->all &= (~(1 >> protBit)); - return; - } - - /* According to protection level, take corresponding recovery action. */ - switch (otp->protLevel) { - /* level 4 */ - case PROT_LEVEL_4: - /* If the temperature is less than threshold 3, level-3 protection is restored. */ - if (temp < otp->protValThr[PROT_VAL_THRESHOLD_3] - otp->recyDelta) { - otp->recyCnt++; - if (otp->recyCnt > otp->recyCntLimit) { - otp->protLevel = PROT_LEVEL_3; - otp->recyCnt = 0; - } - } - break; - - /* level 3 */ - case PROT_LEVEL_3: - /* If the temperature is less than threshold 2, level-2 protection is restored. */ - if (temp < otp->protValThr[PROT_VAL_THRESHOLD_2] - otp->recyDelta) { - otp->recyCnt++; - if (otp->recyCnt > otp->recyCntLimit) { - otp->protLevel = PROT_LEVEL_2; - otp->recyCnt = 0; - } - } - break; - - /* level 2 */ - case PROT_LEVEL_2: - /* If the temperature is less than threshold 1, level-1 protection is restored. */ - if (temp < otp->protValThr[PROT_VAL_THRESHOLD_1] - otp->recyDelta) { - otp->recyCnt++; - if (otp->recyCnt > otp->recyCntLimit) { - otp->protLevel = PROT_LEVEL_1; - otp->recyCnt = 0; - } - } - break; - - /* level 1 */ - case PROT_LEVEL_1: - /* If the temperature is less than threshold 0, level-0 protection is restored. */ - if (temp < otp->protValThr[PROT_VAL_THRESHOLD_0] - otp->recyDelta) { - otp->recyCnt++; - if (otp->recyCnt > otp->recyCntLimit) { - otp->protLevel = PROT_LEVEL_0; - otp->recyCnt = 0; - } - } - break; - - default: - break; - } - return; -} - -/** - * @brief Over temperature protection error status clear. - * @param otp Over temperature protection handle. - * @retval None. - */ -void OTP_Clear(OTP_Handle *otp) -{ - MCS_ASSERT_PARAM(otp != NULL); - /* Clear the history value. */ - otp->protCnt = 0; - otp->protLevel = PROT_LEVEL_0; - otp->recyCnt = 0; - otp->timer = 0.0f; -} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_temp_prot.h b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_temp_prot.h deleted file mode 100644 index 2b9d99a7..00000000 --- a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_temp_prot.h +++ /dev/null @@ -1,49 +0,0 @@ -/** - * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_temp_prot.h - * @author MCU Algorithm Team - * @brief his file contains over temperature protection api declaration. - */ - -/* Define to prevent recursive inclusion ------------------------------------- */ -#ifndef McuMagicTag_MCS_TEMP_PROT_H -#define McuMagicTag_MCS_TEMP_PROT_H - -/* Includes ------------------------------------------------------------------------------------ */ -#include "mcs_prot_cmm.h" -#include "apt_ip.h" - -typedef struct { - unsigned int protCnt; - unsigned int recyCnt; - unsigned int protCntLimit; - unsigned int recyCntLimit; - float protValThr[PROT_VAL_THRESHOLD_NUMS]; /* from low current level to high. */ - float protLimitTime[PROT_LIMIT_TIME_NUMS]; /* from level 3 to level 1. */ - float recyDelta; - float ts; - float timer; - PROT_Level protLevel; -} OTP_Handle; - -void OTP_Init(OTP_Handle *otp, float ts); -void OTP_Det(OTP_Handle *otp, MotorErrStatusReg *motorErrStatus, PROT_ErrBit protBit, float temp); -void OTP_Exec(OTP_Handle *otp, float *spdRef, APT_RegStruct **aptAddr); -void OTP_Recy(OTP_Handle *otp, MotorErrStatusReg *motorErrStatus, PROT_ErrBit protBit, float temp); -void OTP_Clear(OTP_Handle *otp); -#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/readme.md b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/readme.md deleted file mode 100644 index 98b2a068..00000000 --- a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/readme.md +++ /dev/null @@ -1,10 +0,0 @@ -# pmsm_sensorless_2shunt_foc - -**【功能描述】** -+ 基于ECMCU105H/ECBMCU201MPC单板的单电机双电阻采样Foc应用 - -**【环境要求】** -+ 所有单板电源改制为演示用的12V低压,电机选用Gimbal GBM2804H-100T - -**【IDE配置方法】** -+ chipConfig中的Sample栏目里面选中pmsm sensorless 2shunt foc示例,然后点击生成代码即可 diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/src/mcs_carrier.c b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/src/mcs_carrier.c deleted file mode 100644 index 9fc8afac..00000000 --- a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/src/mcs_carrier.c +++ /dev/null @@ -1,144 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_carrier.c - * @author MCU Algorithm Team - * @brief This file provides carrier interrupt application for motor control. - */ - -#include "mcs_carrier.h" -#include "mcs_math.h" -#include "typedefs.h" -#include "mcs_assert.h" -#include "mcs_user_config.h" -#include "mcs_ctlmode_config.h" - -/** - * @brief Synchronous rotation coordinate system angle. - * @param mtrCtrl The motor control handle. - * @retval None. - */ -static void MCS_SyncCoorAngle(MTRCTRL_Handle *mtrCtrl) -{ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - /* Synchronous rotation coordinate system angle. */ - switch (mtrCtrl->stateMachine) { - case FSM_STARTUP: - /* Current ramp angle is 0. */ - if (mtrCtrl->startup.stage == STARTUP_STAGE_CURR) { - mtrCtrl->axisAngle = 0; - } else if (mtrCtrl->startup.stage == STARTUP_STAGE_SPD) { /* IF control phase angle self-addition. */ - mtrCtrl->axisAngle = IF_CurrAngleCalc(&mtrCtrl->ifCtrl, mtrCtrl->spdRefHz); - } else if (mtrCtrl->startup.stage == STARTUP_STAGE_SWITCH) { /* Switch Angle */ - mtrCtrl->axisAngle = mtrCtrl->smo.elecAngle; - } - break; - - case FSM_RUN: - mtrCtrl->axisAngle = mtrCtrl->smo.elecAngle; - break; - - default: - mtrCtrl->axisAngle = 0; - break; - } -} - -/** - * @brief PWM waveform setting and sampling point setting for a single resistors and dual resistors. - * @param mtrCtrl The motor control handle. - * @retval None. - */ -static void MCS_PwmAdcSet(MTRCTRL_Handle *mtrCtrl) -{ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - SampleMode sampleMode = mtrCtrl->sampleMode; - /* Set the duty cycle according to the sampling mode. */ - if (sampleMode == DUAL_RESISTORS) { - SVPWM_Exec(&mtrCtrl->sv, &mtrCtrl->vabRef, &mtrCtrl->dutyUvw); - mtrCtrl->setPwmDutyCb(&mtrCtrl->dutyUvw, &mtrCtrl->dutyUvw); - } else if (sampleMode == SINGLE_RESISTOR) { - R1SVPWM_Exec(&mtrCtrl->r1Sv, &mtrCtrl->vabRef, &mtrCtrl->dutyUvwLeft, &mtrCtrl->dutyUvwRight); - mtrCtrl->setPwmDutyCb(&mtrCtrl->dutyUvwLeft, &mtrCtrl->dutyUvwRight); - /* The ADC sampling point position needs to be set based on the phase shift of a single resistors. */ - mtrCtrl->setADCTriggerTimeCb(mtrCtrl->r1Sv.samplePoint[0] * mtrCtrl->aptMaxcntCmp, \ - mtrCtrl->r1Sv.samplePoint[1] * mtrCtrl->aptMaxcntCmp); - } -} - -/** - * @brief Carrier interrupt function. - * @param mtrCtrl The motor control handle. - * @retval None. - */ -void MCS_CarrierProcess(MTRCTRL_Handle *mtrCtrl) -{ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - UvwAxis *currUvw = &mtrCtrl->currUvw; - AlbeAxis *currAlbe = &mtrCtrl->iabFbk; - AlbeAxis *vab = &mtrCtrl->vabRef; - SampleMode sampleMode = mtrCtrl->sampleMode; - /* sample mode verify */ - if (sampleMode > SINGLE_RESISTOR || mtrCtrl->setADCTriggerTimeCb == NULL) { - return; - } - /* param verify */ - if (mtrCtrl->setPwmDutyCb == NULL || mtrCtrl->readCurrUvwCb == NULL) { - return; - } - /* Read the three-phase current value. */ - mtrCtrl->readCurrUvwCb(currUvw); - /* Clark Calc */ - ClarkeCalc(currUvw, currAlbe); - /* Smo observation */ - if (mtrCtrl->obserType == FOC_OBSERVERTYPE_SMO1TH) { - /* Smo observation */ - FOSMO_Exec(&mtrCtrl->smo, currAlbe, vab, mtrCtrl->spdRefHz); - } else if (mtrCtrl->obserType == FOC_OBSERVERTYPE_SMO4TH) { - /* Smo4th observation */ - SMO4TH_Exec(&mtrCtrl->smo4th, currAlbe, vab); - mtrCtrl->smo.spdEst = mtrCtrl->smo4th.spdEst; - mtrCtrl->smo.elecAngle = mtrCtrl->smo4th.elecAngle; - } - /* Synchronization angle */ - MCS_SyncCoorAngle(mtrCtrl); - - /* Park transformation */ - ParkCalc(currAlbe, mtrCtrl->axisAngle, &mtrCtrl->idqFbk); - - /* statemachine */ - switch (mtrCtrl->stateMachine) { - case FSM_STARTUP: - case FSM_RUN: - CURRCTRL_Exec(&mtrCtrl->currCtrl, &mtrCtrl->vdqRef, mtrCtrl->smo.spdEst, 0); - InvParkCalc(&mtrCtrl->vdqRef, mtrCtrl->axisAngle, vab); - MCS_PwmAdcSet(mtrCtrl); - break; - - case FSM_CAP_CHARGE: - case FSM_CLEAR: - case FSM_IDLE: - mtrCtrl->smo4th.spdEst = 0.0f; - break; - - default: - vab->alpha = 0.0f; - vab->beta = 0.0f; - MCS_PwmAdcSet(mtrCtrl); - break; - } -} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/src/mcs_motor_process.c b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/src/mcs_motor_process.c deleted file mode 100644 index f754d001..00000000 --- a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/src/mcs_motor_process.c +++ /dev/null @@ -1,951 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_motor_process.c - * @author MCU Algorithm Team - * @brief This file provides motor application. - * @details Single FOC application based on the ECMCU105H/ECBMCU201MPC board - * 1) Motor model is Gimbal GBM2804H-100T. - * 2) Select the pmsm sensorless 2shunt foc example in the sample column - of chipConfig and click Generate Code. - * 3) It's power supply must be changed to 12V. - */ -#include "main.h" -#include "mcs_user_config.h" -#include "mcs_math.h" -#include "hmi_module.h" -#include "mcs_ctlmode_config.h" -#include "mcs_prot_user.h" -#include "mcs_prot_user_config.h" -#include "mcs_math_const.h" -#include "mcs_motor_process.h" -#include "mcs_chip_config.h" -#include - -/*------------------------------- Macro Definition -----------------------------------------------*/ -#define US_PER_MS 1000 -#define ANGLE_RANGE_ABS 65536 -#define ANGLE_360_F 65536.0f /* 0 - 65536 indicates 0 to 360. */ -#define APT_FULL_DUTY 1.0f -#define TEMP_3 3.0f -#define TEMP_15 15.0f -#define TEMP_30 30.0f -#define TEMP_45 45.0f -#define TEMP_60 60.0f -#define TEMP_RES_15 78.327f -#define TEMP_RES_30 36.776f -#define TEMP_RES_45 18.301f -#define TEMP_RES_60 9.607f -#define CNT_10 10 -#define CNT_5000 5000 -#define LEVEL_4 4 -#define MOTOR_START_DELAY 2 -#define ADC_READINIT_DELAY 1 -#define ADC_READINIT_TIMES 20 -#define ADC_TRIMVALUE_MIN 1800.0f -#define ADC_TRIMVALUE_MAX 2200.0f -/*------------------------------- Param Definition -----------------------------------------------*/ -/* Motor parameters. */ -/* Np, Rs, Ld, Lq, Psif, J, Nmax, Currmax, PPMR, zShift */ -static MOTOR_Param g_motorParam = MOTORPARAM_DEFAULTS; -static APT_RegStruct* g_apt[PHASE_MAX_NUM] = {APT_U, APT_V, APT_W}; -/* Motor control handle */ -static MTRCTRL_Handle g_mc = {0}; - -/* Motor speed loop PI param. */ -static void SPDCTRL_InitWrapper(SPDCTRL_Handle *spdHandle, float ts) -{ - /* Speed loop param assignment. */ - PI_Param spdPi = { - .kp = SPD_KP, - .ki = SPD_KI, - .lowerLim = SPD_LOWERLIM, - .upperLim = SPD_UPPERLIM, - }; - /* Speed loop param init. */ - SPDCTRL_Init(spdHandle, &g_motorParam, spdPi, ts); -} - -/* Motor current Loop PI param. */ -static void CURRCTRL_InitWrapper(CURRCTRL_Handle *currHandle, DqAxis *idqRef, DqAxis *idqFbk, float ts) -{ - /* Axis-D current loop param assignment. */ - PI_Param dCurrPi = { - .kp = CURRDAXIS_KP, - .ki = CURRDAXIS_KI, - .lowerLim = CURR_LOWERLIM, - .upperLim = CURR_UPPERLIM, - }; - /* Axis-Q current loop param assignment. */ - PI_Param qCurrPi = { - .kp = CURRQAXIS_KP, - .ki = CURRQAXIS_KI, - .lowerLim = CURR_LOWERLIM, - .upperLim = CURR_UPPERLIM, - }; - /* Current loop param init. */ - CURRCTRL_Init(currHandle, &g_motorParam, idqRef, idqFbk, dCurrPi, qCurrPi, ts); -} - -/* First order smo param. */ -static void FOSMO_InitWrapper(FOSMO_Handle *fosmo, float ts) -{ - /* Smo param assignment. */ - FOSMO_Param fosmoParam = { - .gain = FOSMO_GAIN, - .lambda = FOSMO_LAMBDA, - .fcEmf = FOSMO_EMF_CUTOFF_FREQ, - .fcLpf = SPEED_FILTER_CUTOFF_FREQUENCY, - .pllBdw = FOSMO_PLL_BDW, - }; - /* Init smo param. */ - FOSMO_Init(fosmo, fosmoParam, &g_motorParam, ts); -} - -/* Smo4th param. */ -static void SMO4TH_InitWrapper(SMO4TH_Handle *smo4TH) -{ - /* Smo4th param assignment. */ - SMO4TH_Param smo4thParam = { - .kd = SMO4TH_KD, - .kq = SMO4TH_KQ, - .pllBdw = SMO4TH_PLL_BDW, - .fcLpf = SMO4TH_SPD_FILTER_CUTOFF_FREQ, - }; - /* Init smo param. */ - SMO4TH_Init(smo4TH, smo4thParam, &g_motorParam, CTRL_CURR_PERIOD); -} - -/*------------------------------- Function Definition -----------------------------------------------*/ -/** - * @brief Initialzer of system tick. - * @param mtrCtrl Motor control struct handle. - * @retval None. - */ -static void TimerTickInit(MTRCTRL_Handle *mtrCtrl) -{ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - mtrCtrl->sysTickCnt = 0; - mtrCtrl->msTickNum = US_PER_MS / SYSTICK_PERIOD_US; - mtrCtrl->capChargeTickNum = (INV_CAP_CHARGE_MS * US_PER_MS / SYSTICK_PERIOD_US); -} - -/** - * @brief Init motor control task. - * @retval None. - */ -static void TSK_Init(void) -{ - g_mc.motorStateFlag = 0; - g_mc.uartHeartDetCnt = 0; - g_mc.uartTimeStamp = 0; - g_mc.stateMachine = FSM_IDLE; - g_mc.currCtrlPeriod = CTRL_CURR_PERIOD; /* Init current controller */ - g_mc.aptMaxcntCmp = g_apt0.waveform.timerPeriod; - g_mc.sampleMode = DUAL_RESISTORS; - g_mc.obserType = FOC_OBSERVERTYPE_SMO4TH; /* Init foc observe mode */ - g_mc.controlMode = FOC_CONTROLMODE_SPEED; /* Init motor control mode */ - g_mc.adcCurrCofe = ADC_CURR_COFFI; - g_mc.spdAdjustMode = CUST_SPEED_ADJUST; - g_mc.uartConnectFlag = DISCONNECT; - g_mc.spdCmdHz = USER_MIN_SPD_HZ; /* Motor initialization speed */ - - g_mc.adc0Compensate = ADC0COMPENSATE; /* Phase-u current init adc shift trim value */ - g_mc.adc1Compensate = ADC1COMPENSATE; /* Phase-w current init adc shift trim value */ - - IF_Init(&g_mc.ifCtrl, CTRL_IF_CURR_AMP_A, USER_CURR_SLOPE, CTRL_SYSTICK_PERIOD, CTRL_CURR_PERIOD); - RMG_Init(&g_mc.spdRmg, CTRL_SYSTICK_PERIOD, USER_SPD_SLOPE); /* Init speed slope */ - MtrParamInit(&g_mc.mtrParam, g_motorParam); - - TimerTickInit(&g_mc); - if (g_mc.sampleMode == DUAL_RESISTORS) { - SVPWM_Init(&g_mc.sv, INV_VOLTAGE_BUS * ONE_DIV_SQRT3); /* Dual resistors SVPWM init. */ - } else if (g_mc.sampleMode == SINGLE_RESISTOR) { - /* Single resistor SVPWM init. */ - R1SVPWM_Init(&g_mc.r1Sv, INV_VOLTAGE_BUS * ONE_DIV_SQRT3, SAMPLE_POINT_SHIFT, SAMPLE_WINDOW_DUTY); - } - - SPDCTRL_InitWrapper(&g_mc.spdCtrl, CTRL_SYSTICK_PERIOD); - CURRCTRL_InitWrapper(&g_mc.currCtrl, &g_mc.idqRef, &g_mc.idqFbk, CTRL_CURR_PERIOD); - FOSMO_InitWrapper(&g_mc.smo, CTRL_CURR_PERIOD); - SMO4TH_InitWrapper(&g_mc.smo4th); - - STARTUP_Init(&g_mc.startup, USER_SWITCH_SPDBEGIN_HZ, USER_SWITCH_SPDBEGIN_HZ + TEMP_3); - - MotorProt_Init(&g_mc.prot); /* Init protect state comond */ - OCP_Init(&g_mc.prot.ocp, CTRL_CURR_PERIOD); - OVP_Init(&g_mc.prot.ovp, CTRL_SYSTICK_PERIOD); - LVP_Init(&g_mc.prot.lvp, CTRL_SYSTICK_PERIOD); - OTP_Init(&g_mc.prot.otp, CTRL_SYSTICK_PERIOD); - STP_Init(&g_mc.prot.stall, CTRL_SYSTICK_PERIOD, PROT_STALLING_CURR_AMP_LIMIT, - PROT_STALLING_SPD_LIMIT, PROT_STALLING_TIME_LIMIT); -} - -/** - * @brief Clear historical values of all controller before start-up. - * @param mtrCtrl The motor control handle. - * @retval None. - */ -static void ClearBeforeStartup(MTRCTRL_Handle *mtrCtrl) -{ - /* Verify Parameters */ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - /* The initial angle is 0. */ - mtrCtrl->axisAngle = 0; - - mtrCtrl->spdRefHz = 0.0f; - /* The initial dq-axis reference current is 0. */ - mtrCtrl->idqRef.d = 0.0f; - mtrCtrl->idqRef.q = 0.0f; - - mtrCtrl->vdqRef.d = 0.0f; - mtrCtrl->vdqRef.q = 0.0f; - /* Clear Duty Cycle Value. The initial duty cycle is 0.5. */ - mtrCtrl->dutyUvwLeft.u = 0.5f; - mtrCtrl->dutyUvwLeft.v = 0.5f; - mtrCtrl->dutyUvwLeft.w = 0.5f; - mtrCtrl->dutyUvwRight.u = 0.5f; - mtrCtrl->dutyUvwRight.v = 0.5f; - mtrCtrl->dutyUvwRight.w = 0.5f; - - mtrCtrl->prot.motorErrStatus.all = 0x00; - - RMG_Clear(&mtrCtrl->spdRmg); /* Clear the history value of speed slope control */ - CURRCTRL_Clear(&mtrCtrl->currCtrl); - IF_Clear(&mtrCtrl->ifCtrl); - SPDCTRL_Clear(&mtrCtrl->spdCtrl); - FOSMO_Clear(&mtrCtrl->smo); - SMO4TH_Clear(&mtrCtrl->smo4th); - STARTUP_Clear(&mtrCtrl->startup); - R1SVPWM_Clear(&mtrCtrl->r1Sv); - - OTP_Clear(&mtrCtrl->prot.otp); - OCP_Clear(&mtrCtrl->prot.ocp); - OVP_Clear(&mtrCtrl->prot.ovp); - LVP_Clear(&mtrCtrl->prot.lvp); -} - -/** - * @brief To set the comparison value of the IGBT single-resistance ADC sampling trigger position. - * @param aptx The APT register struct handle. - * @param cntCmpA A Count compare reference of time-base counter. - * @param cntCmpB B Count compare reference of time-base counter. - * @param maxCntCmp Maximum Comparison Value - * @retval None. - */ -static void MCS_SetAdcCompareR1(APT_RegStruct *aptx, unsigned short cntCmpA, - unsigned short cntCmpB, unsigned short maxCntCmp) -{ - unsigned short tmp; - /* Sets the A Count compare reference of time-base counter. */ - tmp = (unsigned short)Clamp((float)(cntCmpA), (float)(maxCntCmp - 1), 1.0f); - DCL_APT_SetCounterCompare(aptx, APT_COMPARE_REFERENCE_A, tmp); - /* Sets the B Count compare reference of time-base counter. */ - tmp = (unsigned short)Clamp((float)(cntCmpB), (float)(maxCntCmp - 1), 1.0f); - DCL_APT_SetCounterCompare(aptx, APT_COMPARE_REFERENCE_B, tmp); -} - -/** - * @brief Open the three-phase lower pipe. - * @param aptAddr Three-phase APT address pointer. - * @param maxDutyCnt Max duty count. - * @retval None. - */ -static void AptTurnOnLowSidePwm(APT_RegStruct **aptAddr, unsigned short maxDutyCnt) -{ - MCS_ASSERT_PARAM(aptAddr != NULL); - MCS_ASSERT_PARAM(maxDutyCnt != 0); - unsigned short dutyCnt; - dutyCnt = (unsigned short)(maxDutyCnt * APT_FULL_DUTY); - /* Open the three-phase lower pipe */ - for (unsigned int i = 0; i < PHASE_MAX_NUM; i++) { - APT_RegStruct *aptx = (APT_RegStruct *)(aptAddr[i]); - DCL_APT_SetCounterCompare(aptx, APT_COMPARE_REFERENCE_C, dutyCnt); - DCL_APT_SetCounterCompare(aptx, APT_COMPARE_REFERENCE_D, dutyCnt); - } -} - -/** - * @brief Enable three-phase pwm output. - * @param aptAddr Three-phase APT address pointer. - * @retval None. - */ -static void MotorPwmOutputEnable(APT_RegStruct **aptAddr) -{ - MCS_ASSERT_PARAM(aptAddr != NULL); - /* Enable three-phase pwm output */ - for (unsigned int i = 0; i < PHASE_MAX_NUM; i++) { - APT_RegStruct *aptx = (APT_RegStruct *)(aptAddr[i]); - aptx->PG_OUT_FRC.BIT.rg_pga_frc_en = BASE_CFG_UNSET; - aptx->PG_OUT_FRC.BIT.rg_pgb_frc_en = BASE_CFG_UNSET; - } -} - -/** - * @brief Disable three-phase pwm output. - * @param aptAddr Three-phase APT address pointer. - * @retval None. - */ -static void MotorPwmOutputDisable(APT_RegStruct **aptAddr) -{ - MCS_ASSERT_PARAM(aptAddr != NULL); - /* Disable three-phase pwm output. */ - for (unsigned int i = 0; i < PHASE_MAX_NUM; i++) { - APT_RegStruct *aptx = (APT_RegStruct *)(aptAddr[i]); - aptx->PG_OUT_FRC.BIT.rg_pga_frc_en = BASE_CFG_SET; - aptx->PG_OUT_FRC.BIT.rg_pgb_frc_en = BASE_CFG_SET; - DCL_APT_ForcePWMOutputLow(aptx); - } -} - -/** - * @brief Smo IF angle difference calculation. - * @param smoElecAngle Smo electrical angle. - * @param ifCtrlAngle IF control angle. - * @retval signed short angle difference. - */ -static float SmoIfAngleDiffCalc(float smoElecAngle, float ifCtrlAngle) -{ - float diff = AngleSub(smoElecAngle, ifCtrlAngle); - /* Smo IF angle difference calculation */ - return diff; -} - -/** - * @brief Construct a new mcs startupswitch object. - * @param mtrCtrl The motor control handle. - * @retval None. - */ -static void MCS_StartupSwitch(MTRCTRL_Handle *mtrCtrl) -{ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - STARTUP_Handle *startup = &mtrCtrl->startup; - DqAxis *idqRef = &mtrCtrl->idqRef; - float iftargetAmp = mtrCtrl->ifCtrl.targetAmp; - float spdRefHz = mtrCtrl->spdRefHz; - - switch (startup->stage) { - case STARTUP_STAGE_CURR: - if (mtrCtrl->ifCtrl.curAmp >= iftargetAmp) { - /* Stage change */ - idqRef->q = iftargetAmp; - startup->stage = STARTUP_STAGE_SPD; - } else { - /* current amplitude increase */ - idqRef->q = IF_CurrAmpCalc(&mtrCtrl->ifCtrl); - spdRefHz = 0.0f; - } - break; - case STARTUP_STAGE_SPD: - /* current frequency increase */ - if (Abs(spdRefHz) >= startup->spdBegin) { - /* Stage change */ - startup->stage = STARTUP_STAGE_SWITCH; - TrigVal localTrigVal; - TrigCalc(&localTrigVal, SmoIfAngleDiffCalc(mtrCtrl->smo.elecAngle, mtrCtrl->ifCtrl.angle)); - idqRef->d = 0.0f; - mtrCtrl->spdCtrl.spdPi.integral = iftargetAmp * localTrigVal.cos; - } else { - /* Speed rmg */ - spdRefHz = RMG_Exec(&mtrCtrl->spdRmg, mtrCtrl->spdCmdHz); - } - break; - - case STARTUP_STAGE_SWITCH: - /* Switch from IF to SMO */ - spdRefHz = RMG_Exec(&mtrCtrl->spdRmg, mtrCtrl->spdCmdHz); - idqRef->q = SPDCTRL_Exec(&mtrCtrl->spdCtrl, mtrCtrl->spdRefHz, mtrCtrl->smo.spdEst); - /* Transitional stage, if current reference speed > critical speed, change to next stage */ - if (spdRefHz >= startup->spdBegin + TEMP_3) { - /* Stage change */ - mtrCtrl->stateMachine = FSM_RUN; - } - break; - - default: - break; - } - mtrCtrl->spdRefHz = spdRefHz; -} - -/** - * @brief Pre-processing of motor status. - * @param statusReg System status. - * @param stateMachine Motor Control Status. - * @retval None. - */ -static void MotorStatePerProc(SysStatusReg *statusReg, volatile FsmState *stateMachine) -{ - MCS_ASSERT_PARAM(statusReg != NULL); - MCS_ASSERT_PARAM(stateMachine != NULL); - /* Get system status */ - if (SysIsError(statusReg)) { - *stateMachine = FSM_FAULT; - } - if (SysGetCmdStop(statusReg)) { - SysCmdStopClr(statusReg); - *stateMachine = FSM_STOP; - } -} - -/** - * @brief Check over current status. - * @param statusReg System status. - * @param stateMachine Motor Control Status. - * @retval None. - */ -static void CheckOverCurrentState(SysStatusReg *statusReg, FsmState *stateMachine) -{ - MCS_ASSERT_PARAM(statusReg != NULL); - MCS_ASSERT_PARAM(stateMachine != NULL); - /* check systerm error status */ - if (SysIsError(statusReg) == false) { - *stateMachine = FSM_IDLE; - } -} - -/** - * @brief Check bootstrap capacitor charge time. - * @param mtrCtrl The motor control handle. - * @param stateMachine Motor Control Status. - * @retval None. - */ -static void CheckBootstrpCapChargeTime(MTRCTRL_Handle *mtrCtrl, FsmState *stateMachine) -{ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - MCS_ASSERT_PARAM(stateMachine != NULL); - mtrCtrl->sysTickCnt++; - /* check bootstrap capacitor charge time */ - if (mtrCtrl->sysTickCnt == mtrCtrl->capChargeTickNum) { - *stateMachine = FSM_CLEAR; - } -} - -/** - * @brief Check systerm cmd start signal. - * @param mtrCtrl The motor control handle. - * @param aptAddr Three-phase APT address pointer. - * @param statusReg System status. - * @param stateMachine Motor Control Status. - * @retval None. - */ -static void CheckSysCmdStart(MTRCTRL_Handle *mtrCtrl, - APT_RegStruct **aptAddr, - SysStatusReg *statusReg, - FsmState *stateMachine) -{ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - MCS_ASSERT_PARAM(aptAddr != NULL); - MCS_ASSERT_PARAM(statusReg != NULL); - MCS_ASSERT_PARAM(stateMachine != NULL); - /* check start cmd */ - if (SysGetCmdStart(statusReg)) { - SysRunningSet(statusReg); - SysCmdStartClr(statusReg); - mtrCtrl->sysTickCnt = 0; - *stateMachine = FSM_CAP_CHARGE; - /* Preparation for charging the bootstrap capacitor. */ - AptTurnOnLowSidePwm(aptAddr, mtrCtrl->aptMaxcntCmp); - /* Out put pwm */ - MotorPwmOutputEnable(aptAddr); - } -} - -/** - * @brief System timer tick task. - * @param mtrCtrl The motor control handle. - * @param aptAddr Three-phase APT address pointer. - * @retval None. - */ -static void TSK_SystickIsr(MTRCTRL_Handle *mtrCtrl, APT_RegStruct **aptAddr) -{ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - MCS_ASSERT_PARAM(aptAddr != NULL); - SysStatusReg *statusReg = &mtrCtrl->statusReg; - FsmState *stateMachine = &mtrCtrl->stateMachine; - mtrCtrl->msTickCnt++; - /* Pre-processing of motor status. */ - MotorStatePerProc(statusReg, stateMachine); - /* statemachine */ - switch (*stateMachine) { - case FSM_IDLE: - /* Set smo estimate speed before motor start-up */ - g_mc.smo.spdEst = 0.0f; - CheckSysCmdStart(mtrCtrl, aptAddr, statusReg, stateMachine); - break; - case FSM_CAP_CHARGE: - /* Bootstrap Capacitor Charging Timing */ - CheckBootstrpCapChargeTime(mtrCtrl, stateMachine); - break; - /* Clear parameter before start */ - case FSM_CLEAR: - ClearBeforeStartup(mtrCtrl); - *stateMachine = FSM_STARTUP; - break; - case FSM_STARTUP: - MCS_StartupSwitch(mtrCtrl); - break; - case FSM_RUN: - /* Speed ramp control */ - mtrCtrl->spdRefHz = RMG_Exec(&mtrCtrl->spdRmg, mtrCtrl->spdCmdHz); - /* Speed loop control */ - mtrCtrl->idqRef.q = SPDCTRL_Exec(&mtrCtrl->spdCtrl, mtrCtrl->spdRefHz, mtrCtrl->smo.spdEst); - break; - case FSM_STOP: - mtrCtrl->spdRefHz = 0.0f; - MotorPwmOutputDisable(aptAddr); - SysRunningClr(statusReg); - *stateMachine = FSM_IDLE; - break; - case FSM_FAULT: /* Overcurrent state */ - CheckOverCurrentState(statusReg, stateMachine); - break; - default: - break; - } -} - -/** - * @brief Read the ADC initialize bias trim value. - * @param mtrCtrl The motor control handle. - * @retval None. - */ -static void TrimInitAdcShiftValue(MTRCTRL_Handle *mtrCtrl) -{ - float adc0SampleTemp = 0.0f; /* Current bias value for temp store */ - float adc1SampleTemp = 0.0f; - float adc0TempSum = 0.0f; - float adc1TempSum = 0.0f; /* Current bias sum value for 20 times */ - float adcSampleTimes = 0.0f; /* ADC sample times */ - for (int i = 0; i < ADC_READINIT_TIMES; i++) { - adc0SampleTemp = (float)HAL_ADC_GetConvResult(&ADCU_HANDLE, ADCUSOCNUM); - adc1SampleTemp = (float)HAL_ADC_GetConvResult(&ADCW_HANDLE, ADCWSOCNUM); - BASE_FUNC_DELAY_US(200); /* 200 is delay count, delay 200us triger adc sampling */ - if (adc0SampleTemp > 1000.0f && adc1SampleTemp > 1000.0f) { - adcSampleTimes++; - adc0TempSum += adc0SampleTemp; - adc1TempSum += adc1SampleTemp; - } - } - if (adcSampleTimes < 1.0f) { - adcSampleTimes = 1.0f; /* Prevent divide-by-zero errors */ - } - adc0SampleTemp = adc0TempSum / adcSampleTimes; - adc1SampleTemp = adc1TempSum / adcSampleTimes; - /* Force convert to float */ - mtrCtrl->adc0Compensate = (float) adc0SampleTemp; - mtrCtrl->adc1Compensate = (float) adc1SampleTemp; - /* The normal value scope: 1800 < adc0Compensate < 2200 */ - if(g_mc.adc0Compensate < ADC_TRIMVALUE_MIN || g_mc.adc0Compensate > ADC_TRIMVALUE_MAX \ - || g_mc.adc1Compensate < ADC_TRIMVALUE_MIN || g_mc.adc1Compensate > ADC_TRIMVALUE_MAX) { - DBG_PRINTF("ADC trim value error,please reset!"); - HAL_GPIO_SetValue(&LED2_HANDLE, LED2_PIN, GPIO_LOW_LEVEL); - } - adcSampleTimes = 0; - adc0TempSum = 0; - adc1TempSum = 0; -} - -/** - * @brief Read the ADC current sampling value. - * @param CurrUvw Three-phase current. - * @retval None. - */ -static void ReadCurrUvw(UvwAxis *CurrUvw) -{ - MCS_ASSERT_PARAM(CurrUvw != NULL); - float adc0 = (float)HAL_ADC_GetConvResult(&ADCU_HANDLE, ADCUSOCNUM); - float adc1 = (float)HAL_ADC_GetConvResult(&ADCW_HANDLE, ADCWSOCNUM); - /* Convert adc sample value to current value */ - CurrUvw->u = -(adc0 - g_mc.adc0Compensate) * g_mc.adcCurrCofe; - CurrUvw->w = -(adc1 - g_mc.adc1Compensate) * g_mc.adcCurrCofe; - CurrUvw->v = -CurrUvw->u - CurrUvw->w; -} - -/** - * @brief Setting the APT Output Duty Cycle. - * @param aptx APT register base address. - * @param leftDuty Left duty cycle. - * @param rightDuty Right duty cycle. - * @retval None. - */ -static void SetPwmDuty(APT_Handle *aptx, float leftDuty, float rightDuty) -{ - MCS_ASSERT_PARAM(aptx != NULL); - MCS_ASSERT_PARAM(leftDuty > 0); - MCS_ASSERT_PARAM(rightDuty > 0); - unsigned short maxPeriodCnt = aptx->waveform.timerPeriod; - unsigned short cntCmpLeftEdge = (unsigned short)(leftDuty * maxPeriodCnt); - unsigned short cntCmpRightEdge = (unsigned short)(rightDuty * maxPeriodCnt); - /* avoid overflowing */ - cntCmpLeftEdge = (cntCmpLeftEdge > maxPeriodCnt) ? maxPeriodCnt : cntCmpLeftEdge; - cntCmpRightEdge = (cntCmpRightEdge > maxPeriodCnt) ? maxPeriodCnt : cntCmpRightEdge; - HAL_APT_SetPWMDuty(aptx, cntCmpLeftEdge, cntCmpRightEdge); -} - -/** - * @brief Duty Cycle Setting. - * @param dutyUvwLeft Three-phase left duty cycle. - * @param dutyUvwRight Three-phase right duty cycle. - * @retval None. - */ -static void SetPwmDutyCp(UvwAxis *dutyUvwLeft, UvwAxis *dutyUvwRight) -{ - MCS_ASSERT_PARAM(dutyUvwLeft != NULL); - MCS_ASSERT_PARAM(dutyUvwRight != NULL); - /* Setting the Three-Phase Duty Cycle */ - SetPwmDuty(&g_apt0, dutyUvwLeft->u, dutyUvwRight->u); - SetPwmDuty(&g_apt1, dutyUvwLeft->v, dutyUvwRight->v); - SetPwmDuty(&g_apt2, dutyUvwLeft->w, dutyUvwRight->w); -} - -/** - * @brief To set the ADC sampling trigger comparison value. - * @param cntCmpSOCA Soca Compare Count Value. - * @param cntCmpSOCB Socb Compare Count Value. - * @retval None. - */ -static void SetADCTriggerTime(unsigned short cntCmpSOCA, unsigned short cntCmpSOCB) -{ - MCS_SetAdcCompareR1(g_apt[PHASE_U], cntCmpSOCA, cntCmpSOCB, g_mc.aptMaxcntCmp); -} - -/** - * @brief Temprature table, the temprature detect range is 15 ~ 60 degree. - * @param tempResisValue Temperature sensor resistance. - * @retval None. - */ -static float TempTable(float tempResisValue) -{ - float boardTemp = 0.0f; - /* Temperatures between 15 and 30. */ - if (tempResisValue > TEMP_RES_30 && tempResisValue <= TEMP_RES_15) { - boardTemp = TEMP_15 + (TEMP_30 - TEMP_15) * (TEMP_RES_15 - tempResisValue) / (TEMP_RES_15 - TEMP_RES_30); - } else if (tempResisValue > TEMP_RES_45 && tempResisValue <= TEMP_RES_30) { /* Temperature between 30 and 45. */ - boardTemp = TEMP_30 + (TEMP_45 - TEMP_30) * (TEMP_RES_30 - tempResisValue) / (TEMP_RES_30 - TEMP_RES_45); - } else if (tempResisValue > TEMP_RES_60 && tempResisValue <= TEMP_RES_45) { /* Temperature between 45 and 50. */ - boardTemp = TEMP_45 + (TEMP_60 - TEMP_45) * (TEMP_RES_45 - tempResisValue) / (TEMP_RES_45 - TEMP_RES_60); - } else if (tempResisValue <= TEMP_RES_60) { - boardTemp = TEMP_60; /* If temperature is over 60, set temperature as 60. */ - } else if (tempResisValue >= TEMP_RES_15) { - boardTemp = TEMP_15; /* If temperature is lower 15, set temperature as 15. */ - } - return boardTemp; -} - -/** - * @brief Read power board temperature and udc. - * @retval None. - */ -static void ReadBoardTempAndUdc(void) -{ - HAL_ADC_SoftTrigSample(&ADCRESIS_HANDLE, ADCRESISSOCNUM); - HAL_ADC_SoftTrigSample(&ADCUDC_HANDLE, ADCUDCSOCNUM); - BASE_FUNC_DELAY_US(CNT_10); /* Delay 10 us. */ - /* Force convert to float type. */ - float resisAdcValue = (float)HAL_ADC_GetConvResult(&ADCRESIS_HANDLE, ADCRESISSOCNUM); - /* 10 / (x + 10) * 3.3 = resisAdcValue / 4096 * 3.3, x is resisValue, 10kohm is resistor divider value. */ - float resisValue = (4096.0f * 10.0f - 10.0f * resisAdcValue) / resisAdcValue; - g_mc.powerBoardTemp = TempTable(resisValue); - g_mc.udc = ((float)HAL_ADC_GetConvResult(&ADCUDC_HANDLE, ADCUDCSOCNUM)) * ADC_UDC_COFFI; -} - -/** - * @brief Execut abnormal feedback speed protect motion. - * @retval None. - */ -static void SpdFbkErrorProt_Exec(void) -{ - if (g_mc.prot.motorErrStatus.Bit.motorStalling == 0 && - g_mc.prot.motorErrStatus.Bit.overVoltErr == 0 && - g_mc.prot.motorErrStatus.Bit.lowerVoltErr == 0 && - g_mc.prot.motorErrStatus.Bit.overIpmTempErr == 0 && - g_mc.prot.motorErrStatus.Bit.overCurrErr == 0) { - g_mc.prot.motorErrStatus.Bit.revRotErr = 1; - /* If revRotErr, execute protect motion. */ - ProtSpo_Exec(g_apt); - } -} - -/** - * @brief Execut nan data protect motion. - * @retval None. - */ -static void NanDataDetect(void) -{ - static short errorSpdStatus = 0; - /* Detect the nan observer speed or current value. */ - if (isnan(g_mc.smo.spdEst) || isnan(g_mc.idqRef.q)) { - errorSpdStatus++; - } else { - errorSpdStatus = 0; - } - /* If the data is nan & continuous counting value is over 500 times, execute protect motion. - the detect time is 500 * 500us = 250ms. */ - if (errorSpdStatus >= 500) { - errorSpdStatus = 0; - SpdFbkErrorProt_Exec(); - } -} - -/** - * @brief Check abnormal feedback speed. - * @retval None. - */ -static void CheckSpdFbkStatus(void) -{ - static short errorCurrStatus = 0; - static short errorDeltaSpdStatus = 0; - NanDataDetect(); - if (g_mc.stateMachine == FSM_RUN) { - /* Detect the abnormal idq feedback current. */ - if (Abs(g_mc.idqRef.q - g_mc.idqFbk.q) >= CTRL_IF_CURR_AMP_A) { - errorCurrStatus++; - } else { - errorCurrStatus = 0; - } - /* Detect the abnormal feedback speed, the normal speed is > 0, if smo.spdEst < -10 && - delta speed error > USER_MIN_SPD_HZ + 10.0f at FSM_RUN stage, set the motor motion as error */ - if (g_mc.smo.spdEst < -10.0f && (g_mc.spdRefHz - g_mc.smo.spdEst > USER_MIN_SPD_HZ + 10.0f)) { - errorDeltaSpdStatus++; - } - } - /* Execute protect motion if count over 500 times, this error status caused by abnormal speed - or cabnormal urrent feedback, the detect time is 500 * 500us = 250ms. */ - if (errorCurrStatus >= 500) { - errorCurrStatus = 0; - SpdFbkErrorProt_Exec(); - } - /* This error statu caused by motor stalling, the detect time is 2 * 500us = 1ms. */ - if (errorDeltaSpdStatus >= 2) { - errorDeltaSpdStatus = 0; - g_mc.prot.motorErrStatus.Bit.motorStalling = 1; - } -} - -/** - * @brief Check Potentiometer Value callback function. - * @param param The TIMER_Handle. - * @retval None. - */ -void CheckPotentiometerValueCallback(void *param) -{ - MCS_ASSERT_PARAM(param != NULL); - BASE_FUNC_UNUSED(param); - static float potentiomitorAdcValue = 0.0f; - static float spdCmdHz = 0; - static float spdCmdHzLast = USER_MIN_SPD_HZ; /* 35.0 is spdCmdHzLast init value */ - HAL_ADC_SoftTrigSample(&ADCPTT_HANDLE, ADCPTTSOCNUM); - BASE_FUNC_DELAY_US(10); /* Delay 10 us. */ - potentiomitorAdcValue = (float)HAL_ADC_GetConvResult(&ADCPTT_HANDLE, ADCPTTSOCNUM); - /* 4045.0 is adc sample max value of potentiomitor, convert max spd to 180.25Hz */ - spdCmdHz = potentiomitorAdcValue / 4045.0f * USER_MAX_SPD_HZ; - if (Abs(spdCmdHzLast - spdCmdHz) < 1.0f) { /* Ignore changes less than 1. */ - return; - } - spdCmdHzLast = spdCmdHz; - if (spdCmdHz < USER_MIN_SPD_HZ) { /* 35.0 is spdCmdHz lower limit */ - spdCmdHz = USER_MIN_SPD_HZ; /* 35.0 is spdCmdHz lower limit */ - } - if (spdCmdHz > g_mc.mtrParam.maxElecSpd) { /* spdCmdHz upper limit */ - spdCmdHz = g_mc.mtrParam.maxElecSpd; /* spdCmdHz upper limit */ - } - if (g_mc.spdAdjustMode == CUST_SPEED_ADJUST) { - g_mc.spdCmdHz = spdCmdHz; - } -} - -/** - * @brief System timer ISR for Motor Statemachine CallBack function. - * @param param The systick timer handle. - * @retval None. - */ -void MotorStatemachineCallBack(void *param) -{ - /* Verify Parameters */ - MCS_ASSERT_PARAM(param != NULL); - BASE_FUNC_UNUSED(param); - /* Read power board temprature and voltage. */ - ReadBoardTempAndUdc(); - /* Motor speed loop state machine. */ - TSK_SystickIsr(&g_mc, g_apt); - - /* Motor error speed feedback check. */ - CheckSpdFbkStatus(); - /* Motor stalling detect. */ - STP_Det_ByCurrSpd(&g_mc.prot.stall, &g_mc.prot.motorErrStatus, g_mc.smo.spdEst, g_mc.idqFbk); - STP_Exec(&g_mc.prot.motorErrStatus, g_apt); - - /* Motor over voltage detect. */ - OVP_Det(&g_mc.prot.ovp, &g_mc.prot.motorErrStatus, g_mc.udc); - OVP_Exec(&g_mc.prot.ovp, &g_mc.spdRefHz, g_apt); - OVP_Recy(&g_mc.prot.ovp, &g_mc.prot.motorErrStatus, g_mc.udc); - /* Motor lower voltage detect. */ - LVP_Det(&g_mc.prot.lvp, &g_mc.prot.motorErrStatus, g_mc.udc); - LVP_Exec(&g_mc.prot.lvp, &g_mc.spdRefHz, g_apt); - LVP_Recy(&g_mc.prot.lvp, &g_mc.prot.motorErrStatus, g_mc.udc); - /* Power board over temperature detect. */ - OTP_Det(&g_mc.prot.otp, &g_mc.prot.motorErrStatus, OTP_IPM_ERR_BIT, g_mc.powerBoardTemp); - OTP_Exec(&g_mc.prot.otp, &g_mc.spdRefHz, g_apt); - OTP_Recy(&g_mc.prot.otp, &g_mc.prot.motorErrStatus, OTP_IPM_ERR_BIT, g_mc.powerBoardTemp); - - /* If protect level == 4, set motor state as stop. */ - if (g_mc.prot.ovp.protLevel == LEVEL_4 || g_mc.prot.lvp.protLevel == LEVEL_4 \ - || g_mc.prot.otp.protLevel == LEVEL_4) { - SysCmdStopSet(&g_mc.statusReg); - } -} - -/** - * @brief The carrier ISR wrapper function. - * @param aptHandle The APT handle. - * @retval None. - */ -void MotorCarrierProcessCallback(void *aptHandle) -{ - MCS_ASSERT_PARAM(aptHandle != NULL); - BASE_FUNC_UNUSED(aptHandle); - /* the carrierprocess of motor */ - MCS_CarrierProcess(&g_mc); - /* Over current protect */ - if (g_mc.stateMachine == FSM_RUN || g_mc.stateMachine == FSM_STARTUP) { - OCP_Det(&g_mc.prot.ocp, &g_mc.prot.motorErrStatus, g_mc.idqFbk); - OCP_Exec(&g_mc.prot.ocp, &g_mc.idqFbk, g_apt); /* Execute over current protect motion */ - if (g_mc.prot.ocp.protLevel < LEVEL_4) { - OCP_Recy(&g_mc.prot.ocp, &g_mc.prot.motorErrStatus); - } - } -} - -/** - * @brief Event interrupt callback function of APT module. - * @param para APT module handle. - * @retval None. - */ -void MotorSysErrCallback(void *para) -{ - MCS_ASSERT_PARAM(para != NULL); - APT_Handle *handle = (APT_Handle *)para; - /* The IPM overcurrent triggers and disables the three-phase PWM output. */ - MotorPwmOutputDisable(g_apt); - DCL_APT_ClearOutCtrlEventFlag((APT_RegStruct *)g_apt[PHASE_U], APT_OC_COMBINE_EVENT_A1); - DCL_APT_ClearOutCtrlEventFlag((APT_RegStruct *)g_apt[PHASE_V], APT_OC_COMBINE_EVENT_A1); - DCL_APT_ClearOutCtrlEventFlag((APT_RegStruct *)g_apt[PHASE_W], APT_OC_COMBINE_EVENT_A1); - /* Status setting error */ - SysErrorSet(&g_mc.statusReg); - DBG_PRINTF("APT error! \r\n"); - HAL_GPIO_SetValue(&LED2_HANDLE, LED2_PIN, GPIO_LOW_LEVEL); - BASE_FUNC_UNUSED(handle); -} - -/** - * @brief Init motor controller's data structure. - * @retval None. - */ -static void InitSoftware(void) -{ - /* Initializing motor control param */ - TSK_Init(); - /* Read phase-uvw current */ - g_mc.readCurrUvwCb = ReadCurrUvw; - g_mc.setPwmDutyCb = SetPwmDutyCp; - g_mc.setADCTriggerTimeCb = SetADCTriggerTime; -} - -/** - * @brief Config the KEY func. - * @param handle The GPIO handle. - * @retval None. - */ -static KEY_State Key_StateRead(GPIO_Handle *handle) -{ - if (HAL_GPIO_GetPinValue(handle, handle->pins) == 0) { - BASE_FUNC_DELAY_MS(30); /* delay 30ms for deshake */ - if (HAL_GPIO_GetPinValue(handle, handle->pins) == 0) { - while (HAL_GPIO_GetPinValue(handle, handle->pins) == 0) { - } - return KEY_DOWN; - } - } - return KEY_UP; -} - -/** - * @brief Control motor start and stop state by key func. - * @param param The GPIO handle. - * @retval None. - */ -void MotorStartStopKeyCallback(void *param) -{ - GPIO_Handle *handle = (GPIO_Handle *)param; - if (Key_StateRead(handle) == KEY_DOWN) { - if (g_mc.motorStateFlag == 0) { /* start motor */ - g_mc.motorStateFlag = 1; - SysCmdStartSet(&g_mc.statusReg); - } else if (g_mc.motorStateFlag == 1) { /* stop motor */ - g_mc.motorStateFlag = 0; - SysCmdStopSet(&g_mc.statusReg); - } - } -} - -/** - * @brief User application main entry function. - * @retval BSP_OK. - */ -int MotorMainProcess(void) -{ - unsigned int tickNum1Ms = 2; /* 1ms tick */ - static unsigned int tickCnt1Ms = 0; - unsigned int tickNum500Ms = 1000; /* 500ms tick */ - static unsigned int tickCnt500Ms = 0; - SystemInit(); - HMI_Init(); /* Init uart interrupt */ - HAL_TIMER_Start(&g_timer0); - HAL_TIMER_Start(&g_timer1); - - /* Disable PWM output before startup. */ - - MotorPwmOutputDisable(g_apt); - /* Software initialization. */ - InitSoftware(); - /* Start the PWM clock. */ - HAL_APT_StartModule(RUN_APT0 | RUN_APT1 | RUN_APT2); - /* System Timer clock. */ - BASE_FUNC_DELAY_MS(ADC_READINIT_DELAY); - TrimInitAdcShiftValue(&g_mc); - BASE_FUNC_DELAY_MS(MOTOR_START_DELAY); - while (1) { - /* Cycling send data to host */ - HMI_Process_Tx(&g_mc); - if (g_mc.msTickCnt - tickCnt1Ms >= tickNum1Ms) { - tickCnt1Ms = g_mc.msTickCnt; - /* User Code 1ms Event */ - HMI_Process_Rx(&g_mc); - /* User Code 1ms Event */ - } - - if (g_mc.msTickCnt - tickCnt500Ms >= tickNum500Ms) { - if (SysIsError(&g_mc.statusReg) != true) { - /* LED toggle in normal status. */ - HAL_GPIO_TogglePin(&LED1_HANDLE, LED1_PIN); - } - tickCnt500Ms = g_mc.msTickCnt; - } - } - return 0; -} diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/cust_process.c b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/cust_process.c deleted file mode 100644 index 7c4f7525..00000000 --- a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/cust_process.c +++ /dev/null @@ -1,718 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file cust_process.c - * @author MCU Algorithm Team - * @brief This file provides functions declaration of cust process interface. - */ - -#include "cust_process.h" -#include "mcs_ctlmode_config.h" -#include "mcs_math_const.h" -#include "mcs_user_config.h" -#include "mcs_assert.h" -#include "main.h" -#include "uart_module.h" -#include "mcs_mtr_param.h" -/* Macro definitions --------------------------------------------------------------------------- */ -/* Constant value. */ -#define CONST_VALUE_60 60.0f /* Constant value 60. */ -#define CONST_VALUE_DIV_1000 0.001f /* Constant value 1/1000. */ - -/* Data array index. */ -#define DATA_SEGMENT_ONE 0 /* First element of the data segment */ -#define DATA_SEGMENT_TWO 1 /* Second element of the data segment */ -#define DATA_SEGMENT_THREE 2 /* Third element of the data segment */ -#define DATA_SEGMENT_FOUR 3 /* Fourth element of the data segment */ -#define CUSTACKCODELEN 10 /* Ack code length */ - -/* Command code. */ -#define SET_PID_KP 0x01 /* Set Pid Kp Command Params */ -#define SET_PID_KI 0x02 /* Set Pid Ki Command Params */ -#define SET_PID_LIMIT 0x03 /* Set Pid limit Command Params */ - -#define SET_SMO1TH_PLL_BDW 0x01 /* Set Smo1th Pll BandWidth Command Params */ -#define SET_SMO1TH_SPDFLITER_FC 0x02 /* Set Smo1th Fc Command Params */ -#define SET_SMO1TH_FILCOMPANGLE 0x03 /* Set Smo1th FillComp Command Params */ - -#define SET_SMO4TH_KD 0x01 /* Set Smo4th Kd Command Params */ -#define SET_SMO4TH_KP 0x02 /* Set Smo4th Kq Command Params */ - -#define SET_SPD_COMMAND_HZ 0x01 /* Set Target Speed Command Params */ -#define SET_SPD_RMG_SLOPE 0x02 /* Set Speed Slope Command Params */ - -#define SET_MAX_ELEC_SPD 0x01 /* Set Max Motor Speed Command Params */ -#define SET_MOTOR_NUM_OF_PAIRS 0x02 /* Set Motor Command Params */ - -#define SET_MOTOR_RES_OF_STATOR 0x01 /* Set Motor Res Command Params */ -#define SET_MOTOR_DAXIS_INDUCTANCE 0x02 /* Set Motor Daxis Inductance Command Params */ -#define SET_MOTOR_QAXIS_INDUCTANCE 0x03 /* Set Motor Qaxis Inductance Command Params */ - -#define SET_SVPWM_VOLTAGE_PER_UNIT 0x01 /* Set Svpwm Voltage Params */ -#define SET_ADC_CURRENT_SAMPLE_COFE 0x02 /* Set Adc coffe Params */ -#define SET_CURRENT_LOOP_CONTROL_PERIOD 0x03 /* Set Current loop period Command Params */ - -#define SET_IF_TARGET_CURRENT_VALUE 0x01 /* Set If Target Params */ -#define SET_INCREMENT_OF_IF_CURRENT 0x02 /* Set If Step Command Params */ -#define SET_SPEED_RING_BEGIN_SPEED 0x03 /* Set If to Smo Start Speed Command Params */ - -static unsigned char ackCode = 0; -static unsigned char g_uartTxBuf[CUSTACKCODELEN] = {0}; - -/** - * @brief Set observer type. - * @param mtrCtrl The motor control handle. - * @param rxData Receive buffer - */ -static void CMDCODE_SetObserverType(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) -{ - /* Get function code. */ - int funcCode = (int)(rxData->data[DATA_SEGMENT_ONE].typeF); - if (funcCode == FOC_OBSERVERTYPE_SMO1TH) { - ackCode = 0X01; - mtrCtrl->obserType = FOC_OBSERVERTYPE_SMO1TH; - CUST_AckCode(g_uartTxBuf, ackCode, mtrCtrl->obserType); - } else if (funcCode == FOC_OBSERVERTYPE_SMO4TH) { - ackCode = 0X02; - mtrCtrl->obserType = FOC_OBSERVERTYPE_SMO4TH; - CUST_AckCode(g_uartTxBuf, ackCode, mtrCtrl->obserType); - } -} - -/** - * @brief Set pid parameter ack code. - * @param funcCode Received data funccode. - */ -static unsigned char SetPidAckCode(int funcCode) -{ - switch (funcCode) { - /* Set current loop D-Axis PID parameter ack code. */ - case FOC_CURDAXISPID_PARAMS: - ackCode = 0xE0; - break; - /* Set current loop Q-Axis PID parameter ack code. */ - case FOC_CURQAXISPID_PARAMS: - ackCode = 0xE3; - break; - /* Set speed loop PID parameter ack code. */ - case FOC_SPDPID_PARAMS: - ackCode = 0xE6; - break; - default: - break; - } - return ackCode; -} - -/** - * @brief Set pid parameters. - * @param pidHandle The pid control handle. - * @param rxData Receive buffer - */ -static void SetPidParams(PID_Handle *pidHandle, CUSTDATATYPE_DEF *rxData) -{ - /* Get function code. */ - int funcCode = (int)(rxData->data[DATA_SEGMENT_ONE].typeF); - /* Get command code. */ - int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); - switch (cmdCode) { - case SET_PID_KP: /* Set the P parameter. */ - PID_SetKp(pidHandle, rxData->data[DATA_SEGMENT_THREE].typeF); - ackCode = SetPidAckCode(funcCode); - CUST_AckCode(g_uartTxBuf, (unsigned char)(ackCode + SET_PID_KP), rxData->data[DATA_SEGMENT_THREE].typeF); - break; - case SET_PID_KI: /* Set the I parameter. */ - PID_SetKi(pidHandle, rxData->data[DATA_SEGMENT_THREE].typeF); - ackCode = SetPidAckCode(funcCode); - CUST_AckCode(g_uartTxBuf, (unsigned char)(ackCode + SET_PID_KI), rxData->data[DATA_SEGMENT_THREE].typeF); - break; - case SET_PID_LIMIT: /* Set the pid limit. */ - PID_SetLimit(pidHandle, rxData->data[DATA_SEGMENT_THREE].typeF); - ackCode = SetPidAckCode(funcCode); - CUST_AckCode(g_uartTxBuf, (unsigned char)(ackCode + SET_PID_LIMIT), rxData->data[DATA_SEGMENT_THREE].typeF); - break; - default: - ackCode = 0X77; - CUST_AckCode(g_uartTxBuf, ackCode, 0); - break; - } -} - -/** - * @brief Set motor pid parameters. - * @param mtrCtrl The motor control handle. - * @param rxData Receive buffer - */ -static void CMDCODE_SetMotorPidParams(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) -{ - /* Get function code. */ - int funcCode = (int)(rxData->data[DATA_SEGMENT_ONE].typeF); - - if (funcCode == FOC_CURDAXISPID_PARAMS) { - SetPidParams(&mtrCtrl->currCtrl.dAxisPi, rxData); /* Set Curr loop Daxis pid params */ - } else if (funcCode == FOC_CURQAXISPID_PARAMS) { - SetPidParams(&mtrCtrl->currCtrl.qAxisPi, rxData); /* Set Curr loop Qaxis pid params */ - mtrCtrl->currCtrl.dAxisPi.upperLimit = mtrCtrl->currCtrl.qAxisPi.upperLimit; - mtrCtrl->currCtrl.dAxisPi.lowerLimit = mtrCtrl->currCtrl.qAxisPi.lowerLimit; - } else if (funcCode == FOC_SPDPID_PARAMS) { - SetPidParams(&mtrCtrl->spdCtrl.spdPi, rxData); /* Set Speed loop params */ - } -} - -/** - * @brief Set first order sliding mode observer parameters. - * @param smoHandle The observer control handle. - * @param rxData Receive buffer - */ -static void SetObserverSmo1thParams(FOSMO_Handle *smoHandle, CUSTDATATYPE_DEF *rxData) -{ - smoHandle->kSmo = rxData->data[DATA_SEGMENT_TWO].typeF; - ackCode = 0X09; - CUST_AckCode(g_uartTxBuf, ackCode, smoHandle->kSmo); -} - -/** - * @brief Set first order sliding mode observer's phase-locked loop parameters. - * @param smoHandle The observer control handle. - * @param rxData Receive buffer - */ -static void SetObserverSmo1thPLLParams(FOSMO_Handle *smoHandle, CUSTDATATYPE_DEF *rxData) -{ - /* Get command code. */ - int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); - float pllBdw = 0.0f; - switch (cmdCode) { - case SET_SMO1TH_PLL_BDW: /* Set the bandwidth. */ - pllBdw = rxData->data[DATA_SEGMENT_THREE].typeF; - smoHandle->pll.pi.kp = 2.0f * pllBdw; /* kp = 2.0f * pllBdw */ - smoHandle->pll.pi.ki = pllBdw * pllBdw; /* ki = pllBdw * pllBdw */ - ackCode = 0X0A; - CUST_AckCode(g_uartTxBuf, ackCode, pllBdw); - break; - case SET_SMO1TH_SPDFLITER_FC: /* Set the cutoff frequency. */ - smoHandle->spdFilter.fc = rxData->data[DATA_SEGMENT_THREE].typeF; - smoHandle->spdFilter.a1 = 1.0f / (1.0f + DOUBLE_PI * smoHandle->spdFilter.fc * CTRL_CURR_PERIOD); - smoHandle->spdFilter.b1 = 1.0f - smoHandle->spdFilter.a1; - ackCode = 0X0B; - CUST_AckCode(g_uartTxBuf, ackCode, smoHandle->spdFilter.fc); - break; - case SET_SMO1TH_FILCOMPANGLE: /* Set the compensation angle. */ - smoHandle->filCompAngle = rxData->data[DATA_SEGMENT_THREE].typeF; - ackCode = 0X0C; - CUST_AckCode(g_uartTxBuf, ackCode, smoHandle->filCompAngle); - break; - default: - ackCode = 0X77; - CUST_AckCode(g_uartTxBuf, ackCode, 0); - break; - } -} - -/** - * @brief Set fourth order sliding mode observer parameters. - * @param smo4thHandle The observer control handle. - * @param rxData Receive buffer - */ -static void SetObserverSmo4thParams(SMO4TH_Handle *smo4thHandle, CUSTDATATYPE_DEF *rxData) -{ - /* Get command code. */ - int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); - - switch (cmdCode) { - case SET_SMO4TH_KD: /* Set d axis gain. */ - smo4thHandle->kd = rxData->data[DATA_SEGMENT_THREE].typeF; - ackCode = 0X0D; - CUST_AckCode(g_uartTxBuf, ackCode, smo4thHandle->kd); - break; - case SET_SMO4TH_KP: /* Set q axis gain. */ - smo4thHandle->kq = rxData->data[DATA_SEGMENT_THREE].typeF; - ackCode = 0X0E; - CUST_AckCode(g_uartTxBuf, ackCode, smo4thHandle->kq); - break; - default: - break; - } -} - -/** - * @brief Set fourth order sliding mode observer's phase-locked loop parameters. - * @param smo4thHandle The observer control handle. - * @param rxData Receive buffer - */ -static void SetObserverSmo4thPLLParams(SMO4TH_Handle *smo4thHandle, CUSTDATATYPE_DEF *rxData) -{ - float pllBdw = rxData->data[DATA_SEGMENT_TWO].typeF; - smo4thHandle->pll.pi.kp = (2.0f) * pllBdw; /* kp = 2.0f * pllBdw */ - smo4thHandle->pll.pi.ki = pllBdw * pllBdw; - ackCode = 0X11; - CUST_AckCode(g_uartTxBuf, ackCode, pllBdw); -} - -/** - * @brief Set observer parameters. - * @param mtrCtrl The motor control handle. - * @param rxData Receive buffer - */ -static void CMDCODE_SetObserverParams(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) -{ - /* Get function code. */ - int funcCode = (int)(rxData->data[DATA_SEGMENT_ONE].typeF); - - if (funcCode == FOC_OBSERVERTYPE_SMO1TH) { - SetObserverSmo1thParams(&mtrCtrl->smo, rxData); - } else if (funcCode == FOC_OBSERVERTYPE_SMO1TH_PLL) { - SetObserverSmo1thPLLParams(&mtrCtrl->smo, rxData); - } else if (funcCode == FOC_OBSERVERTYPE_SMO4TH) { - SetObserverSmo4thParams(&mtrCtrl->smo4th, rxData); - } else if (funcCode == FOC_OBSERVERTYPE_SMO4TH_PLL) { - SetObserverSmo4thPLLParams(&mtrCtrl->smo4th, rxData); - } -} - -/** - * @brief Set motor speed and speed slope. - * @param mtrCtrl The motor control handle. - * @param rxData Receive buffer - */ -static void CMDCODE_SetMotorSpdAndSlope(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) -{ - /* Get command code. */ - int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); - - switch (cmdCode) { - case SET_SPD_COMMAND_HZ: /* Set target speed(hz). */ - mtrCtrl->spdCmdHz = rxData->data[DATA_SEGMENT_THREE].typeF * mtrCtrl->mtrParam.mtrNp / CONST_VALUE_60; - /* Judgement the value > 0.00001, make sure denominator != 0. */ - if (rxData->data[DATA_SEGMENT_FOUR].typeF > 0.00001f) { - mtrCtrl->spdRmg.delta = mtrCtrl->spdCmdHz / rxData->data[DATA_SEGMENT_FOUR].typeF * CTRL_SYSTICK_PERIOD; - } - ackCode = 0X16; - CUST_AckCode(g_uartTxBuf, ackCode, mtrCtrl->spdCmdHz); - break; - case SET_SPD_RMG_SLOPE: /* Set speed slope. */ - mtrCtrl->spdRmg.delta = mtrCtrl->spdCmdHz / rxData->data[DATA_SEGMENT_THREE].typeF * CTRL_SYSTICK_PERIOD; - ackCode = 0X17; - CUST_AckCode(g_uartTxBuf, ackCode, rxData->data[DATA_SEGMENT_THREE].typeF); - break; - default: - break; - } -} - -/** - * @brief Set motor base parameters. - * @param mtrParamHandle The motor parameters handle. - * @param rxData Receive buffer - */ -static void SetMotorBaseParams(MOTOR_Param *mtrParamHandle, CUSTDATATYPE_DEF *rxData) -{ - /* Get command code. */ - int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); - - switch (cmdCode) { - case SET_MAX_ELEC_SPD: /* Set max electric speed. */ - mtrParamHandle->maxElecSpd = rxData->data[DATA_SEGMENT_THREE].typeF / - CONST_VALUE_60 * mtrParamHandle->mtrNp; - ackCode = 0X18; - CUST_AckCode(g_uartTxBuf, ackCode, rxData->data[DATA_SEGMENT_THREE].typeF); - break; - case SET_MOTOR_NUM_OF_PAIRS: /* Set the number of motor pole pairs. */ - mtrParamHandle->mtrNp = (unsigned short)(rxData->data[DATA_SEGMENT_THREE].typeF); - ackCode = 0X19; - CUST_AckCode(g_uartTxBuf, ackCode, (float)mtrParamHandle->mtrNp); - break; - default: - break; - } -} - -/** - * @brief Set motor special parameters. - * @param mtrParamHandle The motor parameters handle. - * @param rxData Receive buffer - */ -static void SetMotorSpecialParams(MOTOR_Param *mtrParamHandle, CUSTDATATYPE_DEF *rxData) -{ - /* Get command code. */ - int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); - - switch (cmdCode) { - case SET_MOTOR_RES_OF_STATOR: /* Set the resistor of stator. */ - mtrParamHandle->mtrRs = rxData->data[DATA_SEGMENT_THREE].typeF; - ackCode = 0X1A; - CUST_AckCode(g_uartTxBuf, ackCode, mtrParamHandle->mtrRs); - break; - case SET_MOTOR_DAXIS_INDUCTANCE: /* Set the d axis inductance. */ - mtrParamHandle->mtrLd = rxData->data[DATA_SEGMENT_THREE].typeF; - ackCode = 0X1B; - CUST_AckCode(g_uartTxBuf, ackCode, mtrParamHandle->mtrLd); - break; - case SET_MOTOR_QAXIS_INDUCTANCE: /* Set the q axis inductance. */ - mtrParamHandle->mtrLq = rxData->data[DATA_SEGMENT_THREE].typeF; - ackCode = 0X1C; - CUST_AckCode(g_uartTxBuf, ackCode, mtrParamHandle->mtrLq); - break; - default: - ackCode = 0X77; - CUST_AckCode(g_uartTxBuf, ackCode, 0); - break; - } -} - -/** - * @brief Set motor board parameters. - * @param mtrCtrl The motor control handle. - * @param rxData Receive buffer - */ -static void SetMotorBoardParams(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) -{ - /* Get command code. */ - int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); - - switch (cmdCode) { - case SET_SVPWM_VOLTAGE_PER_UNIT: /* Set svpwm voltage per unit. */ - mtrCtrl->sv.voltPu = rxData->data[DATA_SEGMENT_THREE].typeF * ONE_DIV_SQRT3; - mtrCtrl->currCtrl.outLimit = mtrCtrl->sv.voltPu * ONE_DIV_SQRT3; - ackCode = 0X1D; - CUST_AckCode(g_uartTxBuf, ackCode, rxData->data[DATA_SEGMENT_THREE].typeF); - break; - case SET_ADC_CURRENT_SAMPLE_COFE: /* Set adc current sample cofeature. */ - mtrCtrl->adcCurrCofe = rxData->data[DATA_SEGMENT_THREE].typeF; - ackCode = 0X1E; - CUST_AckCode(g_uartTxBuf, ackCode, mtrCtrl->adcCurrCofe); - break; - case SET_CURRENT_LOOP_CONTROL_PERIOD: /* Set current loop control period. */ - mtrCtrl->currCtrlPeriod = 1 / rxData->data[DATA_SEGMENT_THREE].typeF * CONST_VALUE_DIV_1000; - ackCode = 0X1F; - CUST_AckCode(g_uartTxBuf, ackCode, rxData->data[DATA_SEGMENT_THREE].typeF); - break; - default: - break; - } -} - -/** - * @brief Set motor parameters. - * @param mtrCtrl The motor control handle. - * @param rxData Receive buffer - */ -static void CMDCODE_SetMotorParams(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) -{ - /* Get function code. */ - int funcCode = (int)(rxData->data[DATA_SEGMENT_ONE].typeF); - - if (funcCode == MOTOR_PARAMS_BASE) { - SetMotorBaseParams(&mtrCtrl->mtrParam, rxData); - } else if (funcCode == MOTOR_PARAMS_SPECIAL) { - SetMotorSpecialParams(&mtrCtrl->mtrParam, rxData); - } else if (funcCode == MOTOR_PARAMS_BOARD) { - SetMotorBoardParams(mtrCtrl, rxData); - } -} - -/** - * @brief Motor start. - * @param mtrCtrl The motor control handle. - */ -static void CMDCODE_MotorStart(MTRCTRL_Handle *mtrCtrl) -{ - if (mtrCtrl->stateMachine != FSM_RUN) { - SysCmdStartSet(&mtrCtrl->statusReg); /* start motor. */ - mtrCtrl->motorStateFlag = 1; - ackCode = 0X24; /* send ackcode to host. */ - CUST_AckCode(g_uartTxBuf, ackCode, 1); - } -} - -/** - * @brief Motor stop. - * @param mtrCtrl The motor control handle. - */ -static void CMDCODE_MotorStop(MTRCTRL_Handle *mtrCtrl) -{ - SysCmdStopSet(&mtrCtrl->statusReg); - mtrCtrl->motorStateFlag = 0; - ackCode = 0X25; - CUST_AckCode(g_uartTxBuf, ackCode, 0); -} - -/** - * @brief Motor state reset. - * @param mtrCtrl The motor control handle. - */ -static void CMDCODE_MotorReset(MTRCTRL_Handle *mtrCtrl) -{ - BASE_FUNC_UNUSED(mtrCtrl); - BASE_FUNC_SoftReset(); -} - -/** - * @brief Set IF-Startup parameters. - * @param mtrCtrl The motor control handle. - * @param rxData Receive buffer - */ -static void SetStartupIFParams(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) -{ - /* Get command code. */ - int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); - - switch (cmdCode) { - case SET_IF_TARGET_CURRENT_VALUE: /* Set I/F start up target current value. */ - mtrCtrl->ifCtrl.targetAmp = rxData->data[DATA_SEGMENT_THREE].typeF; - ackCode = 0X26; - CUST_AckCode(g_uartTxBuf, ackCode, mtrCtrl->ifCtrl.targetAmp); - break; - case SET_INCREMENT_OF_IF_CURRENT: /* Set increment of I/F start up current. */ - mtrCtrl->ifCtrl.stepAmp = mtrCtrl->ifCtrl.targetAmp / rxData->data[DATA_SEGMENT_THREE].typeF * - CTRL_SYSTICK_PERIOD; - ackCode = 0X27; - CUST_AckCode(g_uartTxBuf, ackCode, mtrCtrl->ifCtrl.stepAmp); - break; - case SET_SPEED_RING_BEGIN_SPEED: /* Set speed ring begin speed. */ - mtrCtrl->startup.spdBegin = rxData->data[DATA_SEGMENT_THREE].typeF / - CONST_VALUE_60 * mtrCtrl->mtrParam.mtrNp; - ackCode = 0X28; - CUST_AckCode(g_uartTxBuf, ackCode, rxData->data[DATA_SEGMENT_THREE].typeF); - break; - default: - break; - } -} - -/** - * @brief Set start up parameters. - * @param mtrCtrl The motor control handle. - * @param rxData Receive buffer - */ -static void CMDCODE_SetStartupParams(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) -{ - /* Get function code. */ - int funcCode = (int)(rxData->data[DATA_SEGMENT_ONE].typeF); - - if (funcCode == FOC_STARTUP_IF) { - SetStartupIFParams(mtrCtrl, rxData); - } -} - -/** - * @brief Set adjust speed mode. - * @param mtrCtrl The motor control handle. - * @param rxData Receive buffer - */ -static void CMDCODE_SetAdjustSpdMode(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) -{ - /* Get function code. */ - int funcCode = (int)(rxData->data[DATA_SEGMENT_ONE].typeF); - /* Get commond code. */ - int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); - if (funcCode == HOST_SPEED_ADJUST) { - mtrCtrl->spdAdjustMode = HOST_SPEED_ADJUST; - /* Uart connect success. */ - mtrCtrl->uartConnectFlag = CONNECTING; - ackCode = 0X2A; - CUST_AckCode(g_uartTxBuf, ackCode, (float)mtrCtrl->spdAdjustMode); - } else if (funcCode == CUST_SPEED_ADJUST) { - if (cmdCode == 1) { /* If uart connection disconnected & stop motor commond. */ - SysCmdStopSet(&mtrCtrl->statusReg); - mtrCtrl->motorStateFlag = 0; - } - mtrCtrl->spdAdjustMode = CUST_SPEED_ADJUST; - ackCode = 0X2B; - CUST_AckCode(g_uartTxBuf, ackCode, (float)mtrCtrl->spdAdjustMode); - /* Uart disconnect. */ - mtrCtrl->uartConnectFlag = DISCONNECT; - } -} - -/** - * @brief Check uart connect. - * @param mtrCtrl The motor control handle. - * @param rxData Receive buffer - */ -static void CMDCODE_UartHandShake(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) -{ - CMDCODE_SetAdjustSpdMode(mtrCtrl, rxData); -} - -/** - * @brief Set Motor Initial Status Parameters. - * @param mtrCtrl The motor control handle. - * @param rxData Receive buffer - * @param code Instruction code. - */ -static void CMDCODE_EXE_SetMotorInitParams(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData, unsigned char code) -{ - switch (code) { - case CMDCODE_SET_OBSERVER_TYPE: { /* Set observer type. */ - CMDCODE_SetObserverType(mtrCtrl, rxData); - } - break; - case CMDCODE_SET_PID_PARAMS: { /* Set motor pid parameters. */ - CMDCODE_SetMotorPidParams(mtrCtrl, rxData); - } - break; - case CMDCODE_SET_OBSERVER_PARAMS: { /* Set observer parameters. */ - CMDCODE_SetObserverParams(mtrCtrl, rxData); - } - break; - case CMDCODE_SET_MOTOR_TARGETSPD: { /* Set motor speed and speed slope. */ - CMDCODE_SetMotorSpdAndSlope(mtrCtrl, rxData); - } - break; - case CMDCODE_SET_MOTOR_PARAMS: { /* Set motor parameters. */ - CMDCODE_SetMotorParams(mtrCtrl, rxData); - } - break; - case CMDCODE_SET_ADJUSTSPD_MODE: /* Set adjust speed mode. */ - CMDCODE_SetAdjustSpdMode(mtrCtrl, rxData); - break; - default: - break; - } -} - -/** - * @brief Set Motor Control System Status. - * @param mtrCtrl The motor control handle. - * @param rxData Receive buffer - * @param code Instruction code. - */ -static void CMDCODE_EXE_SetMotorState(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData, unsigned char code) -{ - BASE_FUNC_UNUSED(rxData); - switch (code) { - case CMDCODE_MOTOR_START: { /* Motor start command */ - CMDCODE_MotorStart(mtrCtrl); - } - break; - case CMDCODE_MOTOR_STOP: { /* Motor stop command */ - CMDCODE_MotorStop(mtrCtrl); - } - break; - case CMDCODE_MOTORSTATE_RESET: { /* Motor reset command */ - CMDCODE_MotorReset(mtrCtrl); - } - break; - default: - break; - } -} - -/** - * @brief Set Startup and Uart Link Handshake Flag Parameters. - * @param mtrCtrl The motor control handle. - * @param rxData Receive buffer - * @param code Instruction code. - */ -static void CMDCODE_EXE_SetOtherParams(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData, unsigned char code) -{ - switch (code) { - case CMDCODE_SET_STARTUP_PARAMS: /* Set start up parameters. */ - CMDCODE_SetStartupParams(mtrCtrl, rxData); - break; - case CMDCODE_UART_HANDSHAKE: { /* Check uart hand shake. */ - CMDCODE_UartHandShake(mtrCtrl, rxData); - } - break; - case CMDCODE_UART_HEARTDETECT: { /* Check uart hand shake. */ - mtrCtrl->uartHeartDetCnt++; - } - break; - default: - break; - } -} -/** - * @brief Instruction code executor. - * @param mtrCtrl The motor control handle. - * @param rxData Receive buffer - * @param code Instruction code. - */ -static void CMDCODE_EXE_Process(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData, unsigned char code) -{ - CMDCODE_EXE_SetMotorInitParams(mtrCtrl, rxData, code); - CMDCODE_EXE_SetMotorState(mtrCtrl, rxData, code); - CMDCODE_EXE_SetOtherParams(mtrCtrl, rxData, code); -} - -/** - * @brief Host data download callback and data parsing. - * @param mtrCtrl The motor control handle. - * @param rxBuf Receive buffer - */ -void CUST_UartDataProcess(MTRCTRL_Handle *mtrCtrl, unsigned char *rxBuf) -{ - /* Verify Parameters */ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - MCS_ASSERT_PARAM(rxBuf != NULL); - - /* Uart data storage struct */ - CUSTDATATYPE_DEF data; - volatile unsigned char *ptrCnt = &rxBuf[FRAME_CHECK_BEGIN + 1]; - volatile unsigned char *strCnt = &data.data[0].typeCh[0]; - for (unsigned int j = 0; j < FRAME_RECV_DATA_LENTH * FRAME_ONE_DATA_LENTH; j++) { - *strCnt++ = *ptrCnt++; - } - /* Message function code. */ - data.code = rxBuf[FRAME_CHECK_BEGIN]; - CMDCODE_EXE_Process(mtrCtrl, &data, data.code); -} - -/** - * @brief The host computer displays data transmission. - * @param mtrCtrl The motor control handle. - * @param txData Message content. - * @param stage Message status function code. - */ -void CUST_SetTxMsg(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *txData) -{ - /* Verify Parameters */ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - MCS_ASSERT_PARAM(txData != NULL); - if (mtrCtrl->stateMachine == FSM_IDLE) { - mtrCtrl->smo.spdEst = 0.0f; - } - /* Data send to host. */ - txData->data[CURRDQ_Q].typeF = mtrCtrl->idqFbk.q; - txData->data[CURRDQ_D].typeF = mtrCtrl->idqFbk.d; - txData->data[CURRREFDQ_Q].typeF = mtrCtrl->idqRef.q; - txData->data[CURRREFDQ_D].typeF = mtrCtrl->idqRef.d; - /* Motor current speed. */ - txData->data[CURRSPD].typeF = mtrCtrl->smo.spdEst * CONST_VALUE_60 / mtrCtrl->mtrParam.mtrNp; - /* Motor commond speed. */ - txData->data[SPDCMDHZ].typeF = mtrCtrl->spdCmdHz * CONST_VALUE_60 / mtrCtrl->mtrParam.mtrNp; - /* Bus voltage. */ - txData->data[UDC].typeF = mtrCtrl->udc; - /* Power board temprature. */ - txData->data[POWERBOARDTEMP].typeF = mtrCtrl->powerBoardTemp; - /* Motor protection status flag. */ - txData->data[CUST_ERR_CODE].typeI = mtrCtrl->prot.motorErrStatus.all; - /* Three phase current. */ - txData->data[CURRUVW_U].typeF = mtrCtrl->currUvw.u; - txData->data[CURRUVW_V].typeF = mtrCtrl->currUvw.v; - txData->data[CURRUVW_W].typeF = mtrCtrl->currUvw.w; - /* Three phase pwm duty. */ - txData->data[PWMDUTYUVW_U].typeF = mtrCtrl->dutyUvw.u; - txData->data[PWMDUTYUVW_V].typeF = mtrCtrl->dutyUvw.v; - txData->data[PWMDUTYUVW_W].typeF = mtrCtrl->dutyUvw.w; - /* Motor electric angle. */ - txData->data[AXISANGLE].typeF = mtrCtrl->axisAngle; - txData->data[VDQ_Q].typeF = mtrCtrl->vdqRef.q; - txData->data[VDQ_D].typeF = mtrCtrl->vdqRef.d; - txData->data[SPDREFHZ].typeF = mtrCtrl->spdRefHz * CONST_VALUE_60 / mtrCtrl->mtrParam.mtrNp; - txData->data[SENDTIMESTAMP].typeF = mtrCtrl->uartTimeStamp; -} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/cust_process.h b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/cust_process.h deleted file mode 100644 index 60fe69bb..00000000 --- a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/cust_process.h +++ /dev/null @@ -1,36 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file cust_process.h - * @author MCU Algorithm Team - * @brief This file provides functions declaration of system status. - */ - -/* Define to prevent recursive inclusion ------------------------------------------------------- */ -#ifndef McsMagicTag_HMI_MOUDLE_H -#define McsMagicTag_HMI_MOUDLE_H - -/* Includes ------------------------------------------------------------------------------------ */ -#include "protocol.h" -/* Typedef definitions ------------------------------------------------------------------------- */ - - -void CUST_UartDataProcess(MTRCTRL_Handle *mtrCtrl, unsigned char *rxBuf); - -void CUST_SetTxMsg(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *txData); - -#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/hmi_module.c b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/hmi_module.c deleted file mode 100644 index 58bf15a7..00000000 --- a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/hmi_module.c +++ /dev/null @@ -1,57 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file hmi_module.c - * @author MCU Algorithm Team - * @brief This file provides functions declaration of human-machine interface. - */ - -#include "hmi_module.h" -#include "mcs_assert.h" - -/** - * @brief HMI Initializatio. - * @retval None. - */ -void HMI_Init(void) -{ - UartRecvInit(); -} - -/** - * @brief HMI processing. - * @param mtrCtrl The motor control handle.. - * @retval None. - */ -void HMI_Process_Rx(MTRCTRL_Handle *mtrCtrl) -{ - /* Verify Parameters */ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - UartModuleProcess_Rx(mtrCtrl); -} - -/** - * @brief HMI processing. - * @param mtrCtrl The motor control handle.. - * @retval None. - */ -void HMI_Process_Tx(MTRCTRL_Handle *mtrCtrl) -{ - /* Verify Parameters */ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - UartModuleProcess_Tx(mtrCtrl); -} diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/hmi_module.h b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/hmi_module.h deleted file mode 100644 index 056f8d73..00000000 --- a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/hmi_module.h +++ /dev/null @@ -1,37 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file hmi_module.h - * @author MCU Algorithm Team - * @brief This file provides functions declaration of system status. - */ - -/* Define to prevent recursive inclusion ------------------------------------------------------- */ -#ifndef McsMagicTag_HMI_MOUDLE_H -#define McsMagicTag_HMI_MOUDLE_H - -/* Includes ------------------------------------------------------------------------------------ */ -#include "uart_module.h" -/* Typedef definitions ------------------------------------------------------------------------- */ - - -void HMI_Init(void); - -void HMI_Process_Rx(MTRCTRL_Handle *mtrCtrl); -void HMI_Process_Tx(MTRCTRL_Handle *mtrCtrl); - -#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/protocol.c b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/protocol.c deleted file mode 100644 index 56f0c0bb..00000000 --- a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/protocol.c +++ /dev/null @@ -1,174 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file protocol.c - * @author MCU Algorithm Team - * @brief This file provides functions declaration of port communication. - */ - -#include "protocol.h" -#include "apt.h" -#include "typedefs.h" -#include "main.h" -#include "mcs_assert.h" -#include "cust_process.h" - -/** - * @brief Callback function for receiving data analysis and processing. - * @param rxBuf Receive buffer. - */ -__weak void CUST_UartDataProcess(MTRCTRL_Handle *mtrCtrl, unsigned char *rxBuf) -{ - BASE_FUNC_UNUSED(mtrCtrl); - BASE_FUNC_UNUSED(rxBuf); -} -/** - * @brief User-defined protocol message sending function (weak function). - * @param rxData Sending Messages.. - */ -__weak void CUST_SetTxMsg(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *txData) -{ - BASE_FUNC_UNUSED(mtrCtrl); - BASE_FUNC_UNUSED(*txData); -} - -static void (*g_ptrDataProcess)(MTRCTRL_Handle *mtrCtrl, unsigned char *rxBuf) = CUST_UartDataProcess; - -/** - * @brief Frame checksum. - * @param ptr Pointer to the data to be checked - * @param num Number of bytes - * @retval unsigned char Checksum - */ -static unsigned char CheckSum(unsigned char *ptr, unsigned char num) -{ - unsigned char sum = 0; - unsigned char *p = ptr; - /* Calculate the sum of received data. */ - for (unsigned char i = 0; i < num; i++) { - sum += (unsigned char)*p; - p++; - } - return sum; -} - -/** - * @brief Transmitting Data Frames. - * @param mtrCtrl The motor control handle. - * @param txBuf Sending Messages. - */ -unsigned int CUST_TransmitData(MTRCTRL_Handle *mtrCtrl, unsigned char *txBuf) -{ - /* Verify Parameters */ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - MCS_ASSERT_PARAM(txBuf != NULL); - unsigned int dataLen = FRAME_ONE_DATA_LENTH * SEND_FRAME_DATA_NUM; - unsigned char i = 0; - CUSTDATATYPE_DEF txData = {0}; - CUST_SetTxMsg(mtrCtrl, &txData); - txBuf[i++] = FRAME_START; - /* Message function code */ - txBuf[i++] = FRAME_SENT; - /* Message data */ - for (unsigned char x = 0; x < SEND_FRAME_DATA_NUM; x++) { - unsigned char floatIndex = 0; - unsigned char byteOffset = i; - txBuf[x * FRAME_ONE_DATA_LENTH + byteOffset++] = txData.data[x].typeCh[floatIndex++]; - txBuf[x * FRAME_ONE_DATA_LENTH + byteOffset++] = txData.data[x].typeCh[floatIndex++]; - txBuf[x * FRAME_ONE_DATA_LENTH + byteOffset++] = txData.data[x].typeCh[floatIndex++]; - txBuf[x * FRAME_ONE_DATA_LENTH + byteOffset++] = txData.data[x].typeCh[floatIndex++]; - } - /* Message verification domain */ - txBuf[dataLen + i++] = CheckSum((unsigned char*)&txBuf[FRAME_CHECK_BEGIN], dataLen + 1); - /* End of Message */ - txBuf[dataLen + i++] = FRAME_END; - return dataLen + i; -} - -/** - * @brief Transmitting Data Frames. - * @param txBuf Sending Cust Ack Code. - * @param ackCode Ack Code. - * @param varParams Host set parameter. - */ -void CUST_AckCode(unsigned char *txBuf, unsigned char ackCode, float varParams) -{ - /* Verify Parameters */ - MCS_ASSERT_PARAM(txBuf != NULL); - CUSTDATATYPE_DEF txData = {0}; - int dataIndex = 0; - unsigned int i = 0; - unsigned int txLen = 0; - unsigned char dataLen = FRAME_ONE_CHAR_LENTH + FRAME_ONE_DATA_LENTH; - - txData.data[0].typeF = varParams; - txBuf[i++] = FRAME_START; - /* Message function code */ - txBuf[i++] = FRAME_CUSTACK; - /* Message ack code */ - txBuf[i++] = ackCode; - /* Message data */ - txBuf[i++] = txData.data[0].typeCh[dataIndex++]; - txBuf[i++] = txData.data[0].typeCh[dataIndex++]; - txBuf[i++] = txData.data[0].typeCh[dataIndex++]; - txBuf[i++] = txData.data[0].typeCh[dataIndex++]; - - /* Message verification domain */ - txBuf[FRAME_ONE_CHAR_LENTH + i++] = CheckSum((unsigned char*)&txBuf[FRAME_CHECK_BEGIN], dataLen + 1); - /* End of Message */ - txBuf[FRAME_ONE_CHAR_LENTH + i++] = FRAME_END; - txLen = FRAME_ONE_CHAR_LENTH + i++; - HAL_UART_WriteDMA(&g_uart0, txBuf, txLen); -} - -/** - * @brief Cust receive data process. - * @param mtrCtrl The motor control handle. - * @param rxBuf Receive buffer - */ -void CUST_DataReceProcss(MTRCTRL_Handle *mtrCtrl, unsigned char *rxBuf) -{ - /* Verify Parameters */ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - MCS_ASSERT_PARAM(rxBuf != NULL); - unsigned char g_uartTxBuf[10] = {0}; - unsigned char ackCode = 0; - /* Frame header check */ - if (rxBuf[0] != FRAME_START) { - ackCode = 0X78; - CUST_AckCode(g_uartTxBuf, ackCode, 0); - return; - } - /* Frame trailer check */ - if (rxBuf[FRAME_LENTH - 1] != FRAME_END) { - ackCode = 0X79; - CUST_AckCode(g_uartTxBuf, ackCode, 0); - return; - } - /* Checksum */ - if (CheckSum((unsigned char*)&rxBuf[FRAME_CHECK_BEGIN], FRAME_CHECK_NUM) != rxBuf[FRAME_CHECKSUM]) { - ackCode = 0X7A; - CUST_AckCode(g_uartTxBuf, ackCode, 0); - return; - } else { - if (g_ptrDataProcess == NULL) { - return; - } else { - g_ptrDataProcess(mtrCtrl, rxBuf); - } - } -} diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/protocol.h b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/protocol.h deleted file mode 100644 index 900a00d8..00000000 --- a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/protocol.h +++ /dev/null @@ -1,121 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file protocol.h - * @author MCU Algorithm Team - * @brief This file provides functions declaration of port communication. - */ -#ifndef McsMagicTag_PORTOCOL_H -#define McsMagicTag_PORTOCOL_H - -#include "uart.h" -#include "mcs_carrier.h" - -#define RS485_SEND_ENABLE GPIO6->GPIO_DATA[GPIO_PIN_7].reg = GPIO_PIN_7 -#define RS485_SEND_DISABLE GPIO6->GPIO_DATA[GPIO_PIN_7].reg = BASE_CFG_UNSET - -#define RX_BUF_LEN (16) -#define SEND_FRAME_DATA_NUM (CUSTDATA_MAX) -/* Service Uart0 Communication Deal */ -#define FRAME_ONE_DATA_LENTH 4 -#define FRAME_ONE_CHAR_LENTH 1 -#define FRAME_RECV_DATA_LENTH 4 -#define FRAME_LENTH 20 /* Data length */ -#define FRAME_SENT 0X8F -#define FRAME_CUSTACK 0X8A -#define FRAME_START 0x0F /* Start frame */ -#define FRAME_END '/' /* StOP frame */ -#define FRAME_CHECK_BEGIN 1 /* Check frame */ -#define FRAME_CHECKSUM 18 /* Check sum */ -#define FRAME_CHECK_NUM 17 -#define CMDCODE_IDLE_FRAME 0x55 /* Fill frame */ -#define CMDCODE_GET_MOTOR_PARAMS 0x01 -#define CMDCODE_SEND_MOTOR_PARAMS 0x02 -#define CMDCODE_SET_MOTOR_CTLMODE 0x03 -#define CMDCODE_SET_OBSERVER_TYPE 0x04 -#define CMDCODE_SET_STARTUP_MODE 0x05 -#define CMDCODE_SET_PID_PARAMS 0x06 -#define CMDCODE_SET_STARTUP_PARAMS 0x07 -#define CMDCODE_SET_OBSERVER_PARAMS 0x08 -#define CMDCODE_SET_MOTOR_TARGETSPD 0x09 -#define CMDCODE_SET_MOTOR_PARAMS 0x0A -#define CMDCODE_MOTOR_START 0x0B -#define CMDCODE_MOTOR_STOP 0x0C -#define CMDCODE_MOTORSTATE_RESET 0x0D -#define CMDCODE_SEND_FIRMVERSION 0x0E -#define CMDCODE_SET_ADJUSTSPD_MODE 0x11 -#define CMDCODE_UART_HANDSHAKE 0x12 -#define CMDCODE_UART_HEARTDETECT 0x13 - -typedef union { - unsigned char typeCh[4]; - float typeF; - int typeI; -} UNIONDATATYPE_DEF; - -typedef enum { - OFFLINE_RES = 0, - OFFLINE_LD, - OFFLINE_LQ, - OFFLINE_PSIF, - OFFLINE_JS, - OFFLINE_NP, - OFFLINE_B, - OFFLINE_KPD, - OFFLINE_KID, - OFFLINE_KPQ, - OFFLINE_KIQ, - OFFLINE_KPS, - OFFLINE_KIS, - OFFLINE_SPEED, - OFLINE_MAX -} OFFLINE_IDEN_TYPE; - -typedef enum { - CURRDQ_Q = 0, - CURRDQ_D, - CURRREFDQ_Q, - CURRREFDQ_D, - CURRSPD, - SPDCMDHZ, - UDC, - POWERBOARDTEMP, - CUST_ERR_CODE, - CURRUVW_U, - CURRUVW_V, - CURRUVW_W, - PWMDUTYUVW_U, - PWMDUTYUVW_V, - PWMDUTYUVW_W, - AXISANGLE, - VDQ_Q, - VDQ_D, - SPDREFHZ, - SENDTIMESTAMP, - CUSTDATA_MAX -} SENDTOHOSTPARAMS; - -typedef struct { - volatile unsigned char code; - volatile UNIONDATATYPE_DEF data[SEND_FRAME_DATA_NUM]; -} CUSTDATATYPE_DEF; - - -void CUST_DataReceProcss(MTRCTRL_Handle *mtrCtrl, unsigned char *rxBuf); -unsigned int CUST_TransmitData(MTRCTRL_Handle *mtrCtrl, unsigned char *txBuf); -void CUST_AckCode(unsigned char *txBuf, unsigned char ackCode, float varParams); -#endif /* McsMagicTag_PORTOCOL_H */ diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/uart_module.c b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/uart_module.c deleted file mode 100644 index df5e9186..00000000 --- a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/uart_module.c +++ /dev/null @@ -1,184 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the - * following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file uart_module.c - * @author MCU Algorithm Team - * @brief This file provides functions declaration of Serial port communication. - */ -#include "uart_module.h" -#include "debug.h" -#include "main.h" -#include "baseinc.h" - -/* Buffer size */ -#define UI_TX_BUF_LEN (96) -#define UI_RX_BUF_LEN (96) - -/* Receiving Timeout Interval */ -#define UART_TIME_OUT_MS (100) - -/* Start sending data to host delay after uart connect success */ -#define UART_UPDATA_DELAY_TIME_MS (50) - -/* Data buffer */ -unsigned char g_uartRxBuf[UI_RX_BUF_LEN] = {0}; -unsigned char g_uartTxBuf[UI_TX_BUF_LEN] = {0}; -static unsigned int getdeltaSystickCnt = 0; -static FRAME_Handle g_uartFrame; -/** - * @brief Receive Data Clear. - * @param uartFrame Receice Data. - * @retval None. - */ -static void FrameRecvClear(FRAME_Handle *uartFrame) -{ - /* Clear buffer lenth. */ - uartFrame->buffLen = 0; - uartFrame->timeOutCnt = 0; - uartFrame->frameFlag = 0; - /* Clear received data lenth. */ - uartFrame->rxLen = 0; - /* Clear received flag. */ - uartFrame->rxFlag = 0; - uartFrame->upDataCnt = 0; - uartFrame->rxAckFlag = 0; -} - -/** - * @brief Set Dma status. - * @param mtrCtrl The motor control handle. - * @retval None. - */ -static void SetUartDmaStatus(MTRCTRL_Handle *mtrCtrl) -{ - /* Delay 50ms start uart Tx DMA . */ - if (mtrCtrl->uartConnectFlag == CONNECTING && g_uartFrame.upDataDelayCnt++ > UART_UPDATA_DELAY_TIME_MS) { - g_uartFrame.txFlag = 1; /* Start send data flag. */ - mtrCtrl->uartConnectFlag = CONNECTED; - g_uartFrame.upDataDelayCnt = 0; - } - if (mtrCtrl->uartConnectFlag == DISCONNECT) { - g_uartFrame.txFlag = 0; /* Stop send data flag. */ - mtrCtrl->uartTimeStamp = 0; - } -} - -/** - * @brief Uart Dma interupt callback func. - * @param handle Uart handle. - * @retval None. - */ -void UART0_TXDMACallback(void *handle) -{ - BASE_FUNC_UNUSED(handle); - unsigned int getCurSystickCnt = 0; - static unsigned int getlastSystickCnt = 0; - /* USER CODE BEGIN UART0_WRITE_DMA_FINISH */ - g_uartFrame.txFlag = 1; - /* Received information answered, information update reserved. */ - if (g_uartFrame.rxAckFlag == 1) { - g_uartFrame.uartItTxFlag = 1; - g_uartFrame.rxAckFlag = 0; - } - getCurSystickCnt = DCL_SYSTICK_GetTick(); - if (getlastSystickCnt != 0) { - /* Calculate unit frame data send time */ - getdeltaSystickCnt = getCurSystickCnt - getlastSystickCnt; - } - getlastSystickCnt = getCurSystickCnt; - /* USER CODE END UART0_WRITE_DMA_FINISH */ -} - -/** - * @brief Uart0 Interruput Read CallBack Function. - * @param handle Uart handle. - * @retval None. - */ -void UART0ReadInterruptCallback(void *handle) -{ - BASE_FUNC_UNUSED(handle); - /* USER CODE BEGIN UART0_READ_IT_FINISH */ - if (g_uartFrame.rxLen >= UI_RX_BUF_LEN - 1) { - g_uartFrame.rxLen = 0; - } - HAL_UART_ReadIT(handle, &g_uartFrame.rxData, 1); - g_uartRxBuf[g_uartFrame.rxLen] = g_uartFrame.rxData; - g_uartFrame.rxLen++; - g_uartFrame.rxFlag = 1; - g_uartFrame.uartItTxFlag = 0; - return; - /* USER CODE END UART0_READ_IT_FINISH */ -} - -/** - * @brief Uart Read Data Init Function. - * @param void. - * @retval None. - */ -void UartRecvInit(void) -{ - /* Uart reception initialization */ - FrameRecvClear(&g_uartFrame); - HAL_UART_ReadIT(&g_uart0, &g_uartFrame.rxData, 1); -} - -/** - * @brief Uart Read Data Process Function. - * @param mtrCtrl The motor control handle. - * @retval None. - */ -void UartModuleProcess_Rx(MTRCTRL_Handle *mtrCtrl) -{ - /* Verify Parameters */ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - SetUartDmaStatus(mtrCtrl); - if (g_uartFrame.rxFlag == 1) { /* Receive data flag. */ - if (g_uartFrame.timeOutCnt++ > UART_TIME_OUT_MS) { - /* Received data from the host. */ - g_uartFrame.frameFlag = 1; - g_uartFrame.rxFlag = 0; - g_uartFrame.timeOutCnt = 0; - /* Execute data process. */ - CUST_DataReceProcss(mtrCtrl, g_uartRxBuf); - g_uartFrame.rxAckFlag = 1; - g_uartFrame.rxLen = 0; - } - } - g_uartFrame.frameFlag = 0; -} - -/** - * @brief Uart Write Data Process Function. - * @param mtrCtrl The motor control handle. - * @retval None. - */ -void UartModuleProcess_Tx(MTRCTRL_Handle *mtrCtrl) -{ - /* Verify Parameters */ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - if (g_uartFrame.txFlag == 1) { /* Send data flag. */ - mtrCtrl->uartTimeStamp = (float)getdeltaSystickCnt; /* Unit data time stamp */ - g_uartFrame.upDataCnt = 0; - g_uartFrame.txFlag = 0; - /* Send data to host. */ - unsigned int txLen = CUST_TransmitData(mtrCtrl, g_uartTxBuf); - /* If txIT mode send data finish, convert to DMA mode */ - if (g_uartFrame.uartItTxFlag == 1) { - HAL_UART_WriteDMA(&g_uart0, g_uartTxBuf, txLen); - } - } -} diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/uart_module.h b/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/uart_module.h deleted file mode 100644 index f1f8f899..00000000 --- a/vendor/yibaina_3061M/demo/GIM4310-10_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/uart_module.h +++ /dev/null @@ -1,47 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file uart_module.h - * @author MCU Algorithm Team - * @brief This file provides functions declaration of Serial port communication. - */ -#ifndef McsMagicTag_UART_MODULE_H -#define McsMagicTag_UART_MODULE_H - -#include "protocol.h" -#include "mcs_ctlmode_config.h" - -typedef struct { - unsigned int buffLen; - unsigned int timeOutCnt; - unsigned char frameFlag; - unsigned int rxLen; - unsigned char rxFlag; - unsigned char txFlag; - unsigned char rxData; - unsigned int upDataCnt; - unsigned int upDataDelayCnt; - unsigned char uartItTxFlag; - unsigned char rxAckFlag; -} FRAME_Handle; - - -void UartRecvInit(void); -void UartModuleProcess_Rx(MTRCTRL_Handle *mtrCtrl); -void UartModuleProcess_Tx(MTRCTRL_Handle *mtrCtrl); - -#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/readme.md b/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/readme.md deleted file mode 100644 index 6af5e381..00000000 --- a/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/readme.md +++ /dev/null @@ -1,20 +0,0 @@ -# GIM4310-10_Six_Step_Wave - -**【功能描述】** - -- 基于ECMCU105H/ECBMCU201MPC单板的反电动势采样的无感电机六步方波调速系统应用 - -**【环境要求】** - -- 所有单板电源改制为演示用的24V低压,电机选用GIM4310-10 -- 电机(黄绿蓝)相线分别对应功率板上U/V/W通道接口 - -**【配置方法】** - -- 将user文件夹替换原工程user文件夹即可 - -【操作说明】 - -- 点击开发板按键启动电机 - -- 开发板蓝色电位器,可以调节电机转动的快慢。(12-77HZ) \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/generatecode/feature.h b/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/generatecode/feature.h deleted file mode 100644 index d89f618c..00000000 --- a/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/generatecode/feature.h +++ /dev/null @@ -1,94 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file feature.h - * @author MCU Driver Team - * @brief This file contains macro configurations related to the project. This file is generated by the IDE tool. - * @date 2025-04-23 15:45:47 - */ - -#ifndef McuMagicTag_FEATURE_H -#define McuMagicTag_FEATURE_H - -/* Macro definitions --------------------------------------------------------- */ -#define CHIP_3061MNPICA MACRO_ENABLE - -#define MACRO_ENABLE 1 -#define MACRO_DISABLE 0 - -/* Macro switch */ -#define BASE_DEFINE_USE_ASSERT MACRO_ENABLE -#ifndef FLASH_CRC_CONFIG -#define FLASH_CRC_CONFIG -#endif /* #ifndef FLASH_CRC_CONFIG */ -#define BASE_MATH_SINCOS_MIDDLE_TABLE MACRO_ENABLE /**< This macro is used to control the table type when the - BASE_MATH_GetSinCos() queries the table. When the value of - this macro is MACRO_ENABLE, the error value obtained by the - BASE_MATH_GetSinCos() is relatively small, and the return - value of the function may be greater than or less than the - actual value. When the value of this macro is MACRO_DISABLE, - the error value obtained by the BASE_MATH_GetSinCos() is - relatively large. However, in the range [0°, 180°) and - [180°, 360°), the return value of the function is either - greater than or less than the actual value. */ - -/* Peripheral module macro switch--------------------------------------------- */ -#define BOARD_DIM_NUM 1 /**< Number of dimming handle arrays. */ - -#define BOARD_KEY_NUM 10 /**< Number of key handle arrays. */ -#define BOARD_KEY_PRESS_ON GPIO_HIGH_LEVEL /**< GPIO status corresponding to long press valid. */ -#define BOARD_KEY_PRESS_OFF GPIO_LOW_LEVEL /**< GPIO status corresponding to short press valid. */ - -#define BOARD_LED_SEG_NUM 4 /**< Number of segments. */ -#define BOARD_LED_SEGMENT_ON GPIO_HIGH_LEVEL /**< GPIO level status corresponding to valid segments. */ -#define BOARD_LED_SEGMENT_OFF GPIO_LOW_LEVEL /**< GPIO level status corresponding to invalid segments. */ - -#define BOARD_MKEY_SCHEME_NUMBER BOARD_MKEY_SCHEME_NUMBER_ONE /**< Define the scheme to be adopted. */ -#define BOARD_MKEY_OUT_NUM 4 /**< Number of GPIO pins used as output during scanning. */ -#define BOARD_MKEY_IN_NUM 4 /**< Number of GPIO pins used as input during scanning. */ -#define BOARD_MKEY_OUT_PIN_VALID GPIO_LOW_LEVEL /**< GPIO level status corresponding to the valid \ - status of the output GPIO in the key matrix. */ -#define BOARD_MKEY_OUT_PIN_INVALID GPIO_HIGH_LEVEL /**< GPIO level status corresponding to the \ - invalid status of the output GPIO in the key matrix. */ -#define BOARD_MKEY_IN_PIN_VALID GPIO_LOW_LEVEL /**< Indicates the GPIO level corresponding to the \ - valid status of the input GPIO in the key matrix. */ -#define BOARD_MKEY_IN_PIN_INVALID GPIO_HIGH_LEVEL /**< Indicates the GPIO level corresponding to the \ - invalid status of the input GPIO in the key matrix. */ - -#define BOARD_PULSES_NUM 2 /**< Number of pulse handles. */ - -#define BASE_DEFINE_SLIPAVERAGE_NUM 2 /**< Sliding average array length. */ - -#define LISTNODE_MAX 20 - -#define BASE_DEFINE_DMA_QUICKSTART - -#define XTRAIL_FREQ 30000000U - -#define DBG_USE_NO_PRINTF 0U -#define DBG_USE_UART_PRINTF 1U - -#define DBG_PRINTF_USE DBG_USE_UART_PRINTF -#if (DBG_PRINTF_USE == DBG_USE_UART_PRINTF) -#define DBG_PRINTF_UART_PORT UART0 -#endif - -/* USER CODE BEGIN 0 */ -/* USER CODE 区域内代码不会被覆盖,区域外会被生成的默认代码覆盖(其余USER CODE 区域同理) */ -/* USER CODE END 0 */ - -#endif /* McuMagicTag_FEATURE_H */ \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/generatecode/main.h b/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/generatecode/main.h deleted file mode 100644 index 8c5ceec4..00000000 --- a/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/generatecode/main.h +++ /dev/null @@ -1,84 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file main.h - * @author MCU Driver Team - * @brief This file contains driver init functions. - * @date 2025-04-23 15:45:47 - */ - -/* Define to prevent recursive inclusion ------------------------------------- */ -#ifndef McuMagicTag_SYSTEM_INIT_H -#define McuMagicTag_SYSTEM_INIT_H - -#include "adc.h" -#include "adc_ex.h" -#include "acmp.h" -#include "acmp_ex.h" -#include "apt.h" -#include "uart.h" -#include "uart_ex.h" -#include "gpio.h" -#include "timer.h" -#include "timer_ex.h" -#include "crg.h" -#include "iocmg.h" - -#define IO_SPEED_FAST 0x00U -#define IO_SPEED_SLOW 0x01U - -#define IO_DRV_LEVEL4 0x00U -#define IO_DRV_LEVEL3 0x01U -#define IO_DRV_LEVEL2 0x02U -#define IO_DRV_LEVEL1 0x03U - -#define XTAL_DRV_LEVEL4 0x03U -#define XTAL_DRV_LEVEL3 0x02U -#define XTAL_DRV_LEVEL2 0x01U -#define XTAL_DRV_LEVEL1 0x00U - -#define SYS_LED_PIN GPIO_PIN_3 -#define SYS_LED_HANDLE g_gpio2 -#define GpioStartStop_PIN GPIO_PIN_4 -#define GpioStartStop_HANDLE g_gpio2 - -extern ACMP_Handle g_acmp0; -extern TIMER_Handle g_timer1; -extern UART_Handle g_uart0; -extern APT_Handle g_apt0; -extern APT_Handle g_apt1; -extern APT_Handle g_apt2; -extern ADC_Handle g_adc0; - -extern GPIO_Handle g_gpio1; -extern GPIO_Handle g_gpio2; - -BASE_StatusType CRG_Config(CRG_CoreClkSelect *coreClkSelect); -void SystemInit(void); - -void MotorStatemachineCallBack(void *handle); -void TIMER1_DMAOverFlow_InterruptProcess(void *handle); -void MotorCarrierProcessCallback(void *aptHandle); -void MotorSysErrCallback(void *aptHandle); - -void MotorStartStopKeyCallback(void *param); - -/* USER CODE BEGIN 0 */ -/* USER CODE 区域内代码不会被覆盖,区域外会被生成的默认代码覆盖(其余USER CODE 区域同理) */ -/* USER CODE END 0 */ - -#endif /* McuMagicTag_SYSTEM_INIT_H */ \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/generatecode/system_init.c b/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/generatecode/system_init.c deleted file mode 100644 index c737cc23..00000000 --- a/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/generatecode/system_init.c +++ /dev/null @@ -1,530 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file system_init.c - * @author MCU Driver Team - * @brief This file contains driver init functions. - * @date 2025-04-23 15:45:47 - */ - -#include "main.h" -#include "ioconfig.h" -#include "iocmg_ip.h" - -#define UART0_BAUD_RATE 115200 - -BASE_StatusType CRG_Config(CRG_CoreClkSelect *coreClkSelect) -{ - CRG_Handle crg; - crg.baseAddress = CRG; - crg.pllRefClkSelect = CRG_PLL_REF_CLK_SELECT_HOSC; - crg.pllPreDiv = CRG_PLL_PREDIV_4; - crg.pllFbDiv = 48; /* PLL Multiplier 48 */ - crg.pllPostDiv = CRG_PLL_POSTDIV_2; - crg.coreClkSelect = CRG_CORE_CLK_SELECT_PLL; - crg.handleEx.pllPostDiv2 = CRG_PLL_POSTDIV2_3; - crg.handleEx.clk1MSelect = CRG_1M_CLK_SELECT_HOSC; - crg.handleEx.clk1MDiv = (25 - 1); /* The 1 MHz freq is equal to the input clock frequency / (clk_1m_div + 1). 25 is the div of the clk_1m in CLOCK. */ - - if (HAL_CRG_Init(&crg) != BASE_STATUS_OK) { - return BASE_STATUS_ERROR; - } - *coreClkSelect = crg.coreClkSelect; - return BASE_STATUS_OK; -} - -static void ACMP0_Init(void) -{ - HAL_CRG_IpEnableSet(ACMP0_BASE, IP_CLK_ENABLE); /* ACMP clock bit reset. */ - g_acmp0.baseAddress = ACMP0_BASE; - g_acmp0.inOutConfig.inputNNum = ACMP_INPUT_N_SELECT2; - g_acmp0.inOutConfig.inputPNum = ACMP_INPUT_P_SELECT2; - g_acmp0.inOutConfig.polarity = ACMP_OUT_NOT_INVERT; - g_acmp0.filterCtrl.filterMode = ACMP_FILTER_NONE; - g_acmp0.hysteresisVol = ACMP_HYS_VOL_30MV; - g_acmp0.interruptEn = BASE_CFG_UNSET; - HAL_ACMP_Init(&g_acmp0); -} - -static void ADC0_Init(void) -{ - HAL_CRG_IpEnableSet(ADC0_BASE, IP_CLK_ENABLE); - HAL_CRG_IpClkSelectSet(ADC0_BASE, CRG_ADC_CLK_ASYN_PLL_DIV); - HAL_CRG_IpClkDivSet(ADC0_BASE, CRG_ADC_DIV_1); - - g_adc0.baseAddress = ADC0; - g_adc0.socPriority = ADC_PRIMODE_ALL_ROUND; - - HAL_ADC_Init(&g_adc0); - - SOC_Param socParam = {0}; - socParam.adcInput = ADC_CH_ADCINA11; /* PIN9(ADC AIN11) */ - socParam.sampleTotalTime = ADC_SOCSAMPLE_5CLK; /* adc sample total time 5 adc_clk */ - socParam.trigSource = ADC_TRIGSOC_APT0_SOCA; - socParam.continueMode = BASE_CFG_DISABLE; - socParam.finishMode = ADC_SOCFINISH_NONE; - HAL_ADC_ConfigureSoc(&g_adc0, ADC_SOC_NUM5, &socParam); - - socParam.adcInput = ADC_CH_ADCINA12; /* PIN10(ADC AIN12) */ - socParam.sampleTotalTime = ADC_SOCSAMPLE_5CLK; /* adc sample total time 5 adc_clk */ - socParam.trigSource = ADC_TRIGSOC_APT0_SOCA; - socParam.continueMode = BASE_CFG_DISABLE; - socParam.finishMode = ADC_SOCFINISH_NONE; - HAL_ADC_ConfigureSoc(&g_adc0, ADC_SOC_NUM2, &socParam); - - socParam.adcInput = ADC_CH_ADCINA10; /* PIN8(ADC AIN10) */ - socParam.sampleTotalTime = ADC_SOCSAMPLE_5CLK; /* adc sample total time 5 adc_clk */ - socParam.trigSource = ADC_TRIGSOC_APT0_SOCA; - socParam.continueMode = BASE_CFG_DISABLE; - socParam.finishMode = ADC_SOCFINISH_NONE; - HAL_ADC_ConfigureSoc(&g_adc0, ADC_SOC_NUM6, &socParam); - - socParam.adcInput = ADC_CH_ADCINA9; /* PIN7(ADC AIN9) */ - socParam.sampleTotalTime = ADC_SOCSAMPLE_5CLK; /* adc sample total time 5 adc_clk */ - socParam.trigSource = ADC_TRIGSOC_SOFT; - socParam.continueMode = BASE_CFG_DISABLE; - socParam.finishMode = ADC_SOCFINISH_NONE; - HAL_ADC_ConfigureSoc(&g_adc0, ADC_SOC_NUM9, &socParam); -} - -__weak void MotorSysErrCallback(void *aptHandle) -{ - BASE_FUNC_UNUSED(aptHandle); - /* USER CODE BEGIN APT0_EVENT_INTERRUPT */ - /* USER CODE END APT0_EVENT_INTERRUPT */ -} - -__weak void MotorCarrierProcessCallback(void *aptHandle) -{ - BASE_FUNC_UNUSED(aptHandle); - /* USER CODE BEGIN APT0_TIMER_INTERRUPT */ - /* USER CODE END APT0_TIMER_INTERRUPT */ -} - -static void APT0_ProtectInit(void) -{ - APT_OutCtrlProtectEx protectApt = {0}; - protectApt.ocEventEnEx = BASE_CFG_ENABLE; - protectApt.ocEventModeEx = APT_OUT_CTRL_ONE_SHOT; - protectApt.ocActionEx = APT_OUT_CTRL_ACTION_LOW; - protectApt.ocActionBEx = APT_OUT_CTRL_ACTION_LOW; - protectApt.ocEvtInterruptEnEx = BASE_CFG_ENABLE; - protectApt.ocSysEvent = APT_SYS_EVT_DEBUG | APT_SYS_EVT_CLK | APT_SYS_EVT_MEM; - protectApt.originalEvtEx = APT_EM_ORIGINAL_SRC_ACMP0; - protectApt.evtPolarityMaskEx = APT_EM_ACMP0_INVERT_BIT; - protectApt.filterCycleNumEx = 0; - HAL_APT_ProtectInitEx(&g_apt0, &protectApt); -} - -static void APT0_Init(void) -{ - HAL_CRG_IpEnableSet(APT0_BASE, IP_CLK_ENABLE); - - g_apt0.baseAddress = APT0; - - /* Clock Settings */ - g_apt0.waveform.dividerFactor = 1 - 1; - /* Timer Settings */ - g_apt0.waveform.timerPeriod = 6250; /* 6250 is count period of APT time-base timer */ - g_apt0.waveform.cntMode = APT_COUNT_MODE_UP_DOWN; - - /* Wave Form */ - g_apt0.waveform.basicType = APT_PWM_BASIC_A_HIGH_B_LOW; - g_apt0.waveform.chAOutType = APT_PWM_OUT_BASIC_TYPE; - g_apt0.waveform.chBOutType = APT_PWM_OUT_BASIC_TYPE; - g_apt0.waveform.divInitVal = 0; - g_apt0.waveform.cntInitVal = 0; - g_apt0.waveform.cntCmpLeftEdge = 3125; /* 3125 is count compare point of the left edge of PWM waveform */ - g_apt0.waveform.cntCmpRightEdge = 3125; /* 3125 is count compare point of the right edge of PWM waveform */ - g_apt0.waveform.cntCmpLoadMode = APT_BUFFER_INDEPENDENT_LOAD; - g_apt0.waveform.cntCmpLoadEvt = APT_COMPARE_LOAD_EVENT_ZERO; - g_apt0.waveform.deadBandCnt = 150; /* 150 is dead-band value */ - - /* ADC Trigger SOCA */ - g_apt0.adcTrg.trgEnSOCA = BASE_CFG_ENABLE; - g_apt0.adcTrg.cntCmpSOCA = 6000; /* 6000 is count compare point of ADC trigger source SOCA when using CMPA */ - g_apt0.adcTrg.trgSrcSOCA = APT_CS_SRC_CNTR_CMPA_DOWN; - g_apt0.adcTrg.trgScaleSOCA = 1; - - /* ADC Trigger SOCB */ - g_apt0.adcTrg.trgEnSOCB = BASE_CFG_ENABLE; - g_apt0.adcTrg.cntCmpSOCB = 1; - g_apt0.adcTrg.trgSrcSOCB = APT_CS_SRC_CNTR_CMPA_DOWN; - g_apt0.adcTrg.trgScaleSOCB = 1; - - g_apt0.adcTrg.cntCmpLoadMode = APT_BUFFER_INDEPENDENT_LOAD; - g_apt0.adcTrg.cntCmpLoadEvt = APT_COMPARE_LOAD_EVENT_ZERO; - - /* Timer Trigger */ - g_apt0.tmrInterrupt.tmrInterruptEn = BASE_CFG_ENABLE; - g_apt0.tmrInterrupt.tmrInterruptSrc = APT_INT_SRC_CNTR_CMPC_UP; - g_apt0.tmrInterrupt.tmrInterruptScale = 1; - - APT0_ProtectInit(); - - HAL_APT_PWMInit(&g_apt0); - HAL_APT_RegisterCallBack(&g_apt0, APT_EVENT_INTERRUPT, MotorSysErrCallback); - IRQ_SetPriority(IRQ_APT0_EVT, 7); /* 7 is priority value */ - IRQ_Register(IRQ_APT0_EVT, HAL_APT_EventIrqHandler, &g_apt0); - IRQ_EnableN(IRQ_APT0_EVT); - HAL_APT_RegisterCallBack(&g_apt0, APT_TIMER_INTERRUPT, MotorCarrierProcessCallback); - IRQ_SetPriority(IRQ_APT0_TMR, 6); /* 6 is priority value */ - IRQ_Register(IRQ_APT0_TMR, HAL_APT_TimerIrqHandler, &g_apt0); - IRQ_EnableN(IRQ_APT0_TMR); -} - -static void APT1_ProtectInit(void) -{ - APT_OutCtrlProtectEx protectApt = {0}; - protectApt.ocEventEnEx = BASE_CFG_ENABLE; - protectApt.ocEventModeEx = APT_OUT_CTRL_ONE_SHOT; - protectApt.ocActionEx = APT_OUT_CTRL_ACTION_LOW; - protectApt.ocActionBEx = APT_OUT_CTRL_ACTION_LOW; - protectApt.ocEvtInterruptEnEx = BASE_CFG_DISABLE; - protectApt.ocSysEvent = APT_SYS_EVT_DEBUG | APT_SYS_EVT_CLK | APT_SYS_EVT_MEM; - protectApt.originalEvtEx = APT_EM_ORIGINAL_SRC_ACMP0; - protectApt.evtPolarityMaskEx = APT_EM_ACMP0_INVERT_BIT; - protectApt.filterCycleNumEx = 0; - HAL_APT_ProtectInitEx(&g_apt1, &protectApt); -} - -static void APT1_Init(void) -{ - HAL_CRG_IpEnableSet(APT1_BASE, IP_CLK_ENABLE); - - g_apt1.baseAddress = APT1; - - /* Clock Settings */ - g_apt1.waveform.dividerFactor = 1 - 1; - /* Timer Settings */ - g_apt1.waveform.timerPeriod = 6250; /* 6250 is count period of APT time-base timer */ - g_apt1.waveform.cntMode = APT_COUNT_MODE_UP_DOWN; - - /* Wave Form */ - g_apt1.waveform.basicType = APT_PWM_BASIC_A_HIGH_B_LOW; - g_apt1.waveform.chAOutType = APT_PWM_OUT_BASIC_TYPE; - g_apt1.waveform.chBOutType = APT_PWM_OUT_BASIC_TYPE; - g_apt1.waveform.divInitVal = 0; - g_apt1.waveform.cntInitVal = 0; - g_apt1.waveform.cntCmpLeftEdge = 3125; /* 3125 is count compare point of the left edge of PWM waveform */ - g_apt1.waveform.cntCmpRightEdge = 3125; /* 3125 is count compare point of the right edge of PWM waveform */ - g_apt1.waveform.cntCmpLoadMode = APT_BUFFER_INDEPENDENT_LOAD; - g_apt1.waveform.cntCmpLoadEvt = APT_COMPARE_LOAD_EVENT_ZERO; - g_apt1.waveform.deadBandCnt = 150; /* 150 is dead-band value */ - - /* ADC Trigger SOCA */ - g_apt1.adcTrg.trgEnSOCA = BASE_CFG_ENABLE; - g_apt1.adcTrg.cntCmpSOCA = 1; /* 1 is count compare point of ADC trigger source SOCA when using CMPA */ - g_apt1.adcTrg.trgSrcSOCA = APT_CS_SRC_CNTR_ZERO; - g_apt1.adcTrg.trgScaleSOCA = 1; - - g_apt1.adcTrg.cntCmpLoadMode = APT_BUFFER_INDEPENDENT_LOAD; - g_apt1.adcTrg.cntCmpLoadEvt = APT_COMPARE_LOAD_EVENT_ZERO; - - APT1_ProtectInit(); - - HAL_APT_PWMInit(&g_apt1); -} - -static void APT2_ProtectInit(void) -{ - APT_OutCtrlProtectEx protectApt = {0}; - protectApt.ocEventEnEx = BASE_CFG_ENABLE; - protectApt.ocEventModeEx = APT_OUT_CTRL_ONE_SHOT; - protectApt.ocActionEx = APT_OUT_CTRL_ACTION_LOW; - protectApt.ocActionBEx = APT_OUT_CTRL_ACTION_LOW; - protectApt.ocEvtInterruptEnEx = BASE_CFG_DISABLE; - protectApt.ocSysEvent = APT_SYS_EVT_DEBUG | APT_SYS_EVT_CLK | APT_SYS_EVT_MEM; - protectApt.originalEvtEx = APT_EM_ORIGINAL_SRC_ACMP0; - protectApt.evtPolarityMaskEx = APT_EM_ACMP0_INVERT_BIT; - protectApt.filterCycleNumEx = 0; - HAL_APT_ProtectInitEx(&g_apt2, &protectApt); -} - -static void APT2_Init(void) -{ - HAL_CRG_IpEnableSet(APT2_BASE, IP_CLK_ENABLE); - - g_apt2.baseAddress = APT2; - - /* Clock Settings */ - g_apt2.waveform.dividerFactor = 1 - 1; - /* Timer Settings */ - g_apt2.waveform.timerPeriod = 6250; /* 6250 is count period of APT time-base timer */ - g_apt2.waveform.cntMode = APT_COUNT_MODE_UP_DOWN; - - /* Wave Form */ - g_apt2.waveform.basicType = APT_PWM_BASIC_A_HIGH_B_LOW; - g_apt2.waveform.chAOutType = APT_PWM_OUT_BASIC_TYPE; - g_apt2.waveform.chBOutType = APT_PWM_OUT_BASIC_TYPE; - g_apt2.waveform.divInitVal = 0; - g_apt2.waveform.cntInitVal = 0; - g_apt2.waveform.cntCmpLeftEdge = 3125; /* 3125 is count compare point of the left edge of PWM waveform */ - g_apt2.waveform.cntCmpRightEdge = 3125; /* 3125 is count compare point of the right edge of PWM waveform */ - g_apt2.waveform.cntCmpLoadMode = APT_BUFFER_INDEPENDENT_LOAD; - g_apt2.waveform.cntCmpLoadEvt = APT_COMPARE_LOAD_EVENT_ZERO; - g_apt2.waveform.deadBandCnt = 150; /* 150 is dead-band value */ - - APT2_ProtectInit(); - - HAL_APT_PWMInit(&g_apt2); -} - -__weak void MotorStartStopKeyCallback(void *param) -{ - GPIO_Handle *handle = (GPIO_Handle *)param; - BASE_FUNC_UNUSED(handle); -} - -static void GPIO_Init(void) -{ - HAL_CRG_IpEnableSet(GPIO1_BASE, IP_CLK_ENABLE); - g_gpio1.baseAddress = GPIO1; - - g_gpio1.pins = GPIO_PIN_0; - HAL_GPIO_Init(&g_gpio1); - HAL_GPIO_SetDirection(&g_gpio1, g_gpio1.pins, GPIO_OUTPUT_MODE); - HAL_GPIO_SetValue(&g_gpio1, g_gpio1.pins, GPIO_HIGH_LEVEL); - HAL_GPIO_SetIrqType(&g_gpio1, g_gpio1.pins, GPIO_INT_TYPE_NONE); - - HAL_CRG_IpEnableSet(GPIO2_BASE, IP_CLK_ENABLE); - g_gpio2.baseAddress = GPIO2; - - g_gpio2.pins = GPIO_PIN_3; - HAL_GPIO_Init(&g_gpio2); - HAL_GPIO_SetDirection(&g_gpio2, g_gpio2.pins, GPIO_OUTPUT_MODE); - HAL_GPIO_SetValue(&g_gpio2, g_gpio2.pins, GPIO_HIGH_LEVEL); - HAL_GPIO_SetIrqType(&g_gpio2, g_gpio2.pins, GPIO_INT_TYPE_NONE); - - g_gpio2.pins = GPIO_PIN_4; - HAL_GPIO_Init(&g_gpio2); - HAL_GPIO_SetDirection(&g_gpio2, g_gpio2.pins, GPIO_INPUT_MODE); - HAL_GPIO_SetValue(&g_gpio2, g_gpio2.pins, GPIO_LOW_LEVEL); - HAL_GPIO_SetIrqType(&g_gpio2, g_gpio2.pins, GPIO_INT_TYPE_FALL_EDGE); - - HAL_GPIO_RegisterCallBack(&g_gpio2, GPIO_PIN_4, MotorStartStopKeyCallback); - IRQ_Register(IRQ_GPIO2, HAL_GPIO_IrqHandler, &g_gpio2); - IRQ_SetPriority(IRQ_GPIO2, 1); /* set gpio1 interrupt priority to 1, 1~15. 1 is priority value */ - IRQ_EnableN(IRQ_GPIO2); /* gpio interrupt enable */ - - return; -} - -__weak void MotorStatemachineCallBack(void *handle) -{ - BASE_FUNC_UNUSED(handle); - /* USER CODE BEGIN MotorStatemachineCallBack */ - /* USER CODE END MotorStatemachineCallBack */ -} - -static void TIMER1_Init(void) -{ - HAL_CRG_IpEnableSet(TIMER1_BASE, IP_CLK_ENABLE); /* TIMER1 clock enable. */ - unsigned int load = ((float)HAL_CRG_GetIpFreq((void *)TIMER1) / (1u << (TIMERPRESCALER_NO_DIV * 4)) / 1000000u) * 500; - - g_timer1.baseAddress = TIMER1; - g_timer1.load = (load < 1) ? 0 : (load - 1); /* Set timer value immediately */ - g_timer1.bgLoad = (load < 1) ? 0 : (load - 1); /* Set timer value */ - g_timer1.mode = TIMER_MODE_RUN_PERIODIC; /* Run in period mode */ - g_timer1.prescaler = TIMERPRESCALER_NO_DIV; /* Don't frequency division */ - g_timer1.size = TIMER_SIZE_32BIT; /* 1 for 32bit, 0 for 16bit */ - g_timer1.interruptEn = BASE_CFG_ENABLE; - g_timer1.adcSocReqEnable = BASE_CFG_DISABLE; - g_timer1.dmaReqEnable = BASE_CFG_DISABLE; - HAL_TIMER_Init(&g_timer1); - IRQ_Register(IRQ_TIMER1, HAL_TIMER_IrqHandler, &g_timer1); - - HAL_TIMER_RegisterCallback(&g_timer1, TIMER_PERIOD_FIN, MotorStatemachineCallBack); - IRQ_SetPriority(IRQ_TIMER1, 2); /* 2 is priority value */ - IRQ_EnableN(IRQ_TIMER1); -} - -static void UART0_Init(void) -{ - HAL_CRG_IpEnableSet(UART0_BASE, IP_CLK_ENABLE); /* UART0 clock enable. */ - g_uart0.baseAddress = UART0; - - g_uart0.baudRate = UART0_BAUD_RATE; - g_uart0.dataLength = UART_DATALENGTH_8BIT; - g_uart0.stopBits = UART_STOPBITS_ONE; - g_uart0.parity = UART_PARITY_NONE; - g_uart0.txMode = UART_MODE_BLOCKING; - g_uart0.rxMode = UART_MODE_BLOCKING; - g_uart0.fifoMode = BASE_CFG_ENABLE; - g_uart0.fifoTxThr = UART_FIFODEPTH_SIZE8; - g_uart0.fifoRxThr = UART_FIFODEPTH_SIZE8; - g_uart0.hwFlowCtr = BASE_CFG_DISABLE; - g_uart0.handleEx.overSampleMultiple = UART_OVERSAMPLING_16X; - g_uart0.handleEx.msbFirst = BASE_CFG_DISABLE; - HAL_UART_Init(&g_uart0); -} - -static void IOConfig(void) -{ - SYSCTRL0->SC_SYS_STAT.BIT.update_mode = 0; - SYSCTRL0->SC_SYS_STAT.BIT.update_mode_clear = 1; - /* Config PIN9 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO3_5_AS_ADC_AIN11); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO3_5_AS_ADC_AIN11, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO3_5_AS_ADC_AIN11, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO3_5_AS_ADC_AIN11, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO3_5_AS_ADC_AIN11, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN10 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO5_3_AS_ADC_AIN12); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO5_3_AS_ADC_AIN12, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO5_3_AS_ADC_AIN12, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO5_3_AS_ADC_AIN12, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO5_3_AS_ADC_AIN12, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN8 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO3_6_AS_ADC_AIN10); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO3_6_AS_ADC_AIN10, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO3_6_AS_ADC_AIN10, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO3_6_AS_ADC_AIN10, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO3_6_AS_ADC_AIN10, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN7 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO3_7_AS_ADC_AIN9); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO3_7_AS_ADC_AIN9, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO3_7_AS_ADC_AIN9, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO3_7_AS_ADC_AIN9, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO3_7_AS_ADC_AIN9, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN48 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO0_6_AS_ACMP_N2); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO0_6_AS_ACMP_N2, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO0_6_AS_ACMP_N2, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO0_6_AS_ACMP_N2, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO0_6_AS_ACMP_N2, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN47 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO0_5_AS_ACMP_P2); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO0_5_AS_ACMP_P2, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO0_5_AS_ACMP_P2, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO0_5_AS_ACMP_P2, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO0_5_AS_ACMP_P2, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN1 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO0_7_AS_ACMP0_OUT); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO0_7_AS_ACMP0_OUT, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO0_7_AS_ACMP0_OUT, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO0_7_AS_ACMP0_OUT, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO0_7_AS_ACMP0_OUT, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN27 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO1_0_AS_GPIO1_0); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO1_0_AS_GPIO1_0, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO1_0_AS_GPIO1_0, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO1_0_AS_GPIO1_0, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO1_0_AS_GPIO1_0, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN35 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO2_3_AS_GPIO2_3); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO2_3_AS_GPIO2_3, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO2_3_AS_GPIO2_3, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO2_3_AS_GPIO2_3, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO2_3_AS_GPIO2_3, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN41 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO2_4_AS_GPIO2_4); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO2_4_AS_GPIO2_4, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO2_4_AS_GPIO2_4, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO2_4_AS_GPIO2_4, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO2_4_AS_GPIO2_4, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN39 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO0_3_AS_UART0_TXD); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO0_3_AS_UART0_TXD, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO0_3_AS_UART0_TXD, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO0_3_AS_UART0_TXD, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO0_3_AS_UART0_TXD, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN40 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO0_4_AS_UART0_RXD); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO0_4_AS_UART0_RXD, PULL_UP); /* Pull-up and Pull-down, UART RX recommend PULL_UP */ - HAL_IOCMG_SetPinSchmidtMode(GPIO0_4_AS_UART0_RXD, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO0_4_AS_UART0_RXD, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO0_4_AS_UART0_RXD, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN19 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO3_0_AS_APT0_PWMA); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO3_0_AS_APT0_PWMA, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO3_0_AS_APT0_PWMA, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO3_0_AS_APT0_PWMA, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO3_0_AS_APT0_PWMA, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN23 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO4_0_AS_APT0_PWMB); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO4_0_AS_APT0_PWMB, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO4_0_AS_APT0_PWMB, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO4_0_AS_APT0_PWMB, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO4_0_AS_APT0_PWMB, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN20 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO3_1_AS_APT1_PWMA); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO3_1_AS_APT1_PWMA, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO3_1_AS_APT1_PWMA, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO3_1_AS_APT1_PWMA, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO3_1_AS_APT1_PWMA, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN24 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO4_1_AS_APT1_PWMB); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO4_1_AS_APT1_PWMB, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO4_1_AS_APT1_PWMB, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO4_1_AS_APT1_PWMB, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO4_1_AS_APT1_PWMB, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN21 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO3_2_AS_APT2_PWMA); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO3_2_AS_APT2_PWMA, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO3_2_AS_APT2_PWMA, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO3_2_AS_APT2_PWMA, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO3_2_AS_APT2_PWMA, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN25 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO4_2_AS_APT2_PWMB); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO4_2_AS_APT2_PWMB, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO4_2_AS_APT2_PWMB, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO4_2_AS_APT2_PWMB, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO4_2_AS_APT2_PWMB, DRIVER_RATE_2); /* Output signal edge fast/slow */ -} - -static void APT_SyncMasterInit(void) -{ - HAL_APT_MasterSyncInit(&g_apt0, APT_SYNC_OUT_ON_CNTR_ZERO); -} - -static void APT_SyncSlaveInit(void) -{ - APT_SlaveSyncIn aptSlave; - - aptSlave.cntPhase = 0; /* counter phase value */ - aptSlave.syncCntMode = APT_COUNT_MODE_AFTER_SYNC_UP; - aptSlave.syncInSrc = APT_SYNCIN_SRC_APT0_SYNCOUT; /* sync source selection */ - aptSlave.cntrSyncSrc = APT_CNTR_SYNC_SRC_SYNCIN; - HAL_APT_SlaveSyncInit(&g_apt1, &aptSlave); - - aptSlave.cntPhase = 0; /* counter phase value */ - aptSlave.syncCntMode = APT_COUNT_MODE_AFTER_SYNC_UP; - aptSlave.syncInSrc = APT_SYNCIN_SRC_APT0_SYNCOUT; /* sync source selection */ - aptSlave.cntrSyncSrc = APT_CNTR_SYNC_SRC_SYNCIN; - HAL_APT_SlaveSyncInit(&g_apt2, &aptSlave); - -} - -void SystemInit(void) -{ - IOConfig(); - UART0_Init(); - ACMP0_Init(); - APT0_Init(); - APT1_Init(); - APT2_Init(); - ADC0_Init(); - TIMER1_Init(); - GPIO_Init(); - - APT_SyncMasterInit(); - APT_SyncSlaveInit(); - - /* USER CODE BEGIN system_init */ - /* USER CODE END system_init */ -} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/main.c b/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/main.c deleted file mode 100644 index 83b0b133..00000000 --- a/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/main.c +++ /dev/null @@ -1,67 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file main.c - * @author MCU Driver Team - * @brief Main program body. - * @date 2025-04-23 15:45:47 - */ - -#include "typedefs.h" -#include "feature.h" -#include "mcs_motor_process.h" -#include "main.h" -/* USER CODE BEGIN 0 */ -/* USER CODE 区域内代码不会被覆盖,区域外会被生成的默认代码覆盖(其余USER CODE 区域同理) */ -/* 建议用户放置头文件 */ -/* USER CODE END 0 */ -ACMP_Handle g_acmp0; -TIMER_Handle g_timer1; -UART_Handle g_uart0; -APT_Handle g_apt0; -APT_Handle g_apt1; -APT_Handle g_apt2; -ADC_Handle g_adc0; -GPIO_Handle g_gpio1; -GPIO_Handle g_gpio2; -/* USER CODE BEGIN 1 */ -/* 建议用户定义全局变量、结构体、宏定义或函数声明等 */ -/* USER CODE END 1 */ - -int main(void) -{ - /* USER CODE BEGIN 2 */ - /* 建议用户放置初始化代码或启动代码等 */ - /* USER CODE END 2 */ - MotorMainProcess(); - /* USER CODE BEGIN 3 */ - /* 建议用户放置初始配置代码 */ - /* USER CODE END 3 */ - while (1) { - /* USER CODE BEGIN 4 */ - /* 建议用户放置周期性执行代码 */ - /* USER CODE END 4 */ - } - /* USER CODE BEGIN 5 */ - /* 建议用户放置代码流程 */ - /* USER CODE END 5 */ - return BASE_STATUS_OK; -} - -/* USER CODE BEGIN 6 */ -/* 建议用户放置自定义函数 */ -/* USER CODE END 6 */ \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/application/mcs_motor_process.c b/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/application/mcs_motor_process.c deleted file mode 100644 index d53b28d7..00000000 --- a/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/application/mcs_motor_process.c +++ /dev/null @@ -1,562 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_motor_process.c - * @author MCU Algorithm Team - * @brief This file provides motor application for ECBMCU201MPC board. - * @details BLDC six-step application based on the ECBMCU201MPC board - * 1) Motor model is Gimbal GBM2804H-100T. - * 2) Select the bldc six-step sensorless example in the sample column - of chipConfig and click Generate Code. - * 3) It's power supply must be changed to 12V. - */ - -#include "debug.h" -#include "mcs_assert.h" -#include "mcs_user_config.h" -#include "mcs_status.h" -#include "mcs_carrier.h" -#include "mcs_motor_process.h" - -/*------------------------------- Macro Definition -----------------------------------------------*/ -#define US_PER_MS 1000 -#define APT_FULL_DUTY 1.0f - -/* Motor control handle for bldc */ -static MtrCtrlHandle g_mc; - -static APT_RegStruct* g_aptCp[PHASE_MAX_NUM] = {BRIDGE_CTR_APT_U, BRIDGE_CTR_APT_V, BRIDGE_CTR_APT_W}; - -/** - * @brief Initialzer of system tick. - * @param mtrCtrl Motor control struct handle. - * @retval None. - */ -static void TimerTickInit(MtrCtrlHandle *mtrCtrl) -{ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - mtrCtrl->sysTickCnt = 0; - mtrCtrl->msTickNum = US_PER_MS / SYSTICK_PERIOD_US; - mtrCtrl->capChargeTickNum = (INV_CAP_CHARGE_MS * US_PER_MS / SYSTICK_PERIOD_US); -} - -/** - * @brief Read the ADC current sampling value of the compressor. - * @param CurrUvw Three-phase current. - * @retval None. - */ -static void ReadBemfUVWMotor(UVWBemf *bemfUVW) -{ - MCS_ASSERT_PARAM(bemfUVW != NULL); - bemfUVW->u = (int)(HAL_ADC_GetConvResult(&ADC_HANDLE, ADC_U_SOC_NUM)&0xFFF); - bemfUVW->v = (int)(HAL_ADC_GetConvResult(&ADC_HANDLE, ADC_V_SOC_NUM)&0xFFF); - bemfUVW->w = (int)(HAL_ADC_GetConvResult(&ADC_HANDLE, ADC_W_SOC_NUM)&0xFFF); -} - -/** - * @brief Init motor control task. - * @retval None. - */ -static void TSK_InitMotor(void) -{ - /* Initialize target speed. */ - g_mc.spdCmdHz = SDP_TARGET_VALUE; - /* zeroPoint = IN_VOLTAGE_BUS / 2.0; 4095/3.3 :ADC value corresponding to 1 V */ - g_mc.zeroPoint = ((((float)IN_VOLTAGE_BUS / 2.0) * VOL_DIVIDER_COEFFICIENT) * 4095 / 3.3); - g_mc.pwmDuty = FORCE_DRAG_MINDUTY; - - /* Sets the number of sample filtering times for zero-crossing sampling. */ - g_mc.sysVar.bemfFilterCnt = FILTER_COUNT; - g_mc.sysVar.dragChangePhaseTime = DRAG_START_INTERVAL; - - g_mc.stateMachine = FSM_IDLE; - g_mc.aptMaxcntCmp = g_apt0.waveform.timerPeriod; - - /* Speed management initialization. */ - RMG_Init(&g_mc.spdRmg, CTRL_SYSTICK_PERIOD, USER_SPD_SLOPE); /* Init speed slope */ - TimerTickInit(&g_mc); - - /* Pid control parameter initialization. */ - g_mc.spdPi.kp = SPD_PID_KP; - g_mc.spdPi.ki = SPD_PID_KI; - g_mc.spdPi.ts = SPD_PID_TS; - g_mc.spdPi.upperLimit = APT_DUTYLIMIT_MAX; - g_mc.spdPi.lowerLimit = APT_DUTYLIMIT_MIN; - - /* BLDC six-step control initialization. */ - g_mc.stepCtrl.phaseStep = STEP1; - g_mc.stepCtrl.controlApt.u = &g_apt0; - g_mc.stepCtrl.controlApt.v = &g_apt1; - g_mc.stepCtrl.controlApt.w = &g_apt2; -} - - -/** - * @brief Software Initialization. - * @retval None. - */ -static void InitSoftware(void) -{ - TSK_InitMotor(); - - g_mc.readBemfUVW = ReadBemfUVWMotor; -} - -/** - * @brief Enable three-phase pwm output. - * @param aptAddr Three-phase APT address pointer. - * @retval None. - */ -static void MotorPwmOutputEnable(APT_RegStruct **aptAddr) -{ - MCS_ASSERT_PARAM(aptAddr != NULL); - /* Enable three-phase pwm output */ - for (unsigned int i = 0; i < PHASE_MAX_NUM; i++) { - APT_RegStruct *aptx = aptAddr[i]; - aptx->PG_OUT_FRC.BIT.rg_pga_frc_en = BASE_CFG_UNSET; - aptx->PG_OUT_FRC.BIT.rg_pgb_frc_en = BASE_CFG_UNSET; - } -} - -/** - * @brief Disable three-phase pwm output. - * @param aptAddr Three-phase APT address pointer. - * @retval None. - */ -static void MotorPwmOutputDisable(APT_RegStruct **aptAddr) -{ - MCS_ASSERT_PARAM(aptAddr != NULL); - /* Disable three-phase pwm output. */ - for (unsigned int i = 0; i < PHASE_MAX_NUM; i++) { - APT_RegStruct *aptx = aptAddr[i]; - aptx->PG_OUT_FRC.BIT.rg_pga_frc_en = BASE_CFG_SET; - aptx->PG_OUT_FRC.BIT.rg_pgb_frc_en = BASE_CFG_SET; - DCL_APT_ForcePWMOutputLow(aptx); - } -} - -/** - * @brief Force rotor alignment to home position. - * @param mtrCtrl The motor control handle. - * @retval None. - */ -static void ForceAlign(MtrCtrlHandle *mtrCtrl) -{ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - mtrCtrl->pwmDuty = FORCE_DRAG_MINDUTY; - mtrCtrl->sysVar.dragChangePhaseTime = DRAG_START_INTERVAL; - MCS_SetCtrAptDuty(mtrCtrl, mtrCtrl->pwmDuty); - MotorPwmOutputEnable(g_aptCp); - mtrCtrl->stepCtrl.phaseStep = STEP6; - SixStepPwm(&mtrCtrl->stepCtrl); - BASE_FUNC_DELAY_MS(100); /* Delay 100 ms waiting for rotor alignment. */ - mtrCtrl->stepCtrl.phaseStep = STEP1; - SixStepPwm(&mtrCtrl->stepCtrl); - BASE_FUNC_DELAY_MS(100); /* Delay 100 ms waiting for rotor alignment. */ - mtrCtrl->sysVar.lastZeroPoint = DCL_SYSTICK_GetTick(); -} - -/** - * @brief Pre-processing of motor status. - * @param statusReg System status. - * @param stateMachine Motor Control Status. - * @retval None. - */ -static void MotorStatePerProc(SysStatusReg *statusReg, volatile FsmState *stateMachine) -{ - MCS_ASSERT_PARAM(statusReg != NULL); - MCS_ASSERT_PARAM(stateMachine != NULL); - /* Get system status */ - if (SysIsError(statusReg)) { - *stateMachine = FSM_FAULT; - } - if (SysGetCmdStop(statusReg)) { - SysCmdStopClr(statusReg); - *stateMachine = FSM_WAIT_STOP; - } -} - -/** - * @brief Open the three-phase lower pipe. - * @param aptAddr Three-phase APT address pointer. - * @param maxDutyCnt Max duty count. - * @retval None. - */ -static void AptTurnOnLowSidePwm(APT_RegStruct **aptAddr, unsigned int maxDutyCnt) -{ - MCS_ASSERT_PARAM(aptAddr != NULL); - MCS_ASSERT_PARAM(maxDutyCnt != 0); - unsigned int dutyCnt; - dutyCnt = maxDutyCnt * APT_FULL_DUTY; - /* Open the three-phase lower pipe */ - for (unsigned int i = 0; i < PHASE_MAX_NUM; i++) { - APT_RegStruct *aptx = aptAddr[i]; - aptx->TC_REFC.BIT.rg_cnt_refc = dutyCnt; - aptx->TC_REFD.BIT.rg_cnt_refd = dutyCnt; - } -} - -/** - * @brief Check systerm cmd start signal. - * @param mtrCtrl The motor control handle. - * @param aptAddr Three-phase APT address pointer. - * @param statusReg System status. - * @param stateMachine Motor Control Status. - * @retval None. - */ -static void CheckSysCmdStart(MtrCtrlHandle *mtrCtrl, APT_RegStruct **aptAddr, SysStatusReg *statusReg, - FsmState *stateMachine) -{ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - MCS_ASSERT_PARAM(aptAddr != NULL); - MCS_ASSERT_PARAM(statusReg != NULL); - MCS_ASSERT_PARAM(stateMachine != NULL); - /* check start cmd */ - if (SysGetCmdStart(statusReg)) { - SysRunningSet(statusReg); - SysCmdStartClr(statusReg); - mtrCtrl->sysTickCnt = 0; - *stateMachine = FSM_CAP_CHARGE; - /* Preparation for charging the bootstrap capacitor. */ - AptTurnOnLowSidePwm(aptAddr, mtrCtrl->aptMaxcntCmp); - /* Out put pwm */ - MotorPwmOutputEnable(aptAddr); - } -} - -/** - * @brief Clear historical values of all controller before start-up. - * @param mtrCtrl The motor control handle. - * @retval None. - */ -static void ClearBeforeStartup(MtrCtrlHandle *mtrCtrl) -{ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - mtrCtrl->aptMaxcntCmp = g_apt0.waveform.timerPeriod; - - /* Clear bemf parameters. */ - mtrCtrl->bemf.u = 0; - mtrCtrl->bemf.v = 0; - mtrCtrl->bemf.w = 0; - - /* Clear tickcnt. */ - mtrCtrl->msTickCnt = 0; - - mtrCtrl->pwmDuty = FORCE_DRAG_MINDUTY; - - /* Clear spd parameters. */ - mtrCtrl->spdEstHz = 0; - mtrCtrl->spdRefHz = 0; - - mtrCtrl->stepCtrl.phaseStep = STEP1; - - mtrCtrl->sysTickCnt = 0; - - mtrCtrl->sysVar.dragChangePhaseTime = DRAG_START_INTERVAL; - mtrCtrl->sysVar.changePhaseFlag = 0; - mtrCtrl->sysVar.firstEventFilterFlag = 0; - mtrCtrl->sysVar.stepTimeNum = 0; - mtrCtrl->sysVar.stepTimeFilterEnable = 0; - for (int i = 0; i < STEP_MAX_NUM; i++) { - mtrCtrl->sysVar.stepTime[i] = 0; - } - /* RMG CLEAR */ - RMG_Clear(&mtrCtrl->spdRmg); /* Clear the history value of speed slope control */ - /* SPDCTRL CLEAR */ - PID_Clear(&mtrCtrl->spdPi); -} - -/** - * @brief Check bootstrap capacitor charge time. - * @param mtrCtrl The motor control handle. - * @param stateMachine Motor Control Status. - * @retval None. - */ -static void CheckBootstrpCapChargeTime(MtrCtrlHandle *mtrCtrl, FsmState *stateMachine) -{ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - MCS_ASSERT_PARAM(stateMachine != NULL); - mtrCtrl->sysTickCnt++; - /* check bootstrap capacitor charge time */ - if (mtrCtrl->sysTickCnt == mtrCtrl->capChargeTickNum) { - /* Update Status. */ - *stateMachine = FSM_CLEAR; - } -} - -/** - * @brief Check over current status. - * @param statusReg System status. - * @param stateMachine Motor Control Status. - * @retval None. - */ -static void CheckOverCurrentState(SysStatusReg *statusReg, FsmState *stateMachine) -{ - MCS_ASSERT_PARAM(statusReg != NULL); - MCS_ASSERT_PARAM(stateMachine != NULL); - /* check systerm error status */ - if (SysIsError(statusReg) == false) { - *stateMachine = FSM_IDLE; - } -} - -/** - * @brief Deceleration control phase before stop. - * @param mtrCtrl The motor control handle. - * @param statusReg Motor Control Status. - * @param targetSpd Deceleration target value. - * @param switchSpd Switch to stop state speed. - * @retval None. - */ -static void DecelerateSpeed(MtrCtrlHandle *mtrCtrl, FsmState *stateMachine, float targetSpd, float switchSpd) -{ - /* Reduce speed before stop. */ - mtrCtrl->spdRefHz = RMG_Exec(&mtrCtrl->spdRmg, targetSpd); - mtrCtrl->spdPi.error = mtrCtrl->spdRefHz - mtrCtrl->spdEstHz; - /* Speed loop control */ - mtrCtrl->pwmDuty = PI_Exec(&mtrCtrl->spdPi); - if (mtrCtrl->spdRefHz <= switchSpd) { /* Maximum speed for switching to the FSM_STOP state */ - *stateMachine = FSM_STOP; - } -} - -/** - * @brief System timer tick task. - * @param mtrCtrl The motor control handle. - * @param aptAddr Three-phase APT address pointer. - * @retval None. - */ -static void TSK_SystickIsr(MtrCtrlHandle *mtrCtrl, APT_RegStruct **aptAddr) -{ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - MCS_ASSERT_PARAM(aptAddr != NULL); - SysStatusReg *statusReg = &mtrCtrl->statusReg; - volatile FsmState *stateMachine = &mtrCtrl->stateMachine; - mtrCtrl->msTickCnt++; - /* Pre-processing of motor status. */ - MotorStatePerProc(statusReg, stateMachine); - /* statemachine */ - switch (*stateMachine) { - case FSM_IDLE: - CheckSysCmdStart(mtrCtrl, aptAddr, statusReg, (FsmState *)stateMachine); - break; - case FSM_CAP_CHARGE: - /* Bootstrap Capacitor Charging Timing */ - CheckBootstrpCapChargeTime(mtrCtrl, (FsmState *)stateMachine); - break; - case FSM_CLEAR: - /* Clearing control parameters. */ - ClearBeforeStartup(mtrCtrl); - /* Rotor alignment. */ - ForceAlign(mtrCtrl); - *stateMachine = FSM_STARTUP; - break; - case FSM_STARTUP: - /* Forced drag. */ - break; - case FSM_RUN: - /* Speed ramp control */ - mtrCtrl->spdRefHz = RMG_Exec(&mtrCtrl->spdRmg, mtrCtrl->spdCmdHz); - mtrCtrl->spdPi.error = mtrCtrl->spdRefHz - mtrCtrl->spdEstHz; - /* Speed loop control */ - mtrCtrl->pwmDuty = PI_Exec(&mtrCtrl->spdPi); - break; - case FSM_WAIT_STOP: - /* 5.0f : maximum speed for switching to the FSM_STOP state */ - DecelerateSpeed(mtrCtrl, (FsmState *)stateMachine, 0.0f, 5.0f); - break; - case FSM_STOP: - MotorPwmOutputDisable(aptAddr); - SysRunningClr(statusReg); - *stateMachine = FSM_IDLE; - break; - case FSM_FAULT: - /* Overcurrent state */ - CheckOverCurrentState(statusReg, (FsmState *)stateMachine); - break; - default: - break; - } -} - -/** - * @brief Overcurrent protection. - * @param aptHandle The apt Handle. - * @retval None. - */ -void MotorSysErrCallback(void *aptHandle) -{ - /* Overcurrent protection callback function. */ - BASE_FUNC_UNUSED(aptHandle); - MotorPwmOutputDisable(g_aptCp); - DCL_APT_ClearOutCtrlEventFlag((APT_RegStruct *)g_aptCp[PHASE_U], APT_OC_COMBINE_EVENT_A1); - DCL_APT_ClearOutCtrlEventFlag((APT_RegStruct *)g_aptCp[PHASE_V], APT_OC_COMBINE_EVENT_A1); - DCL_APT_ClearOutCtrlEventFlag((APT_RegStruct *)g_aptCp[PHASE_W], APT_OC_COMBINE_EVENT_A1); - /* Status setting error */ - SysErrorSet(&g_mc.statusReg); - DBG_PRINTF("APT error! \r\n"); - HAL_GPIO_SetValue(&g_gpio2, GPIO_PIN_3, GPIO_LOW_LEVEL); -} - -/** - * @brief Motor blockage protection function. - * @retval None. - */ -static void MotorBlockageProtect(void) -{ - unsigned int currentTick = DCL_SYSTICK_GetTick(); /* Get the current tick value. */ - unsigned int intervalTick = (currentTick >= g_mc.sysVar.lastZeroPoint) - ? currentTick - g_mc.sysVar.lastZeroPoint - : SYSTICK_MAX_VALUE - g_mc.sysVar.lastZeroPoint + currentTick + 1; - if (intervalTick > SYSTICK_GetCRGHZ()) { - MotorPwmOutputDisable(g_aptCp); - SysErrorSet(&g_mc.statusReg); - g_mc.spdEstHz = 0; - return; - } -} - -/** - * @brief Carrier Interruption. - * @param aptHandle The apt Handle. - * @retval None. - */ -void MotorCarrierProcessCallback(void *aptHandle) -{ - BASE_FUNC_UNUSED(aptHandle); - /* USER CODE BEGIN APT0_TIMER_INTERRUPT */ - MCS_CarrierProcess(&g_mc); - if (g_mc.stateMachine == FSM_RUN || g_mc.stateMachine == FSM_WAIT_STOP) { - MotorBlockageProtect(); - } - /* USER CODE END APT0_TIMER_INTERRUPT */ -} - -/** - * @brief Change phase delay callback function. - * @param handle The TIMER1 Handle. - * @retval None. - */ -void MotorStatemachineCallBack(void *handle) -{ - /* TIMER1CallbackFunction */ - BASE_FUNC_UNUSED(handle); - TSK_SystickIsr(&g_mc, g_aptCp); -} - -/** - * @brief Check Potentiometer Value and adjust speed. - * @param None. - * @retval None. - */ -static void AdjustSpeedFunction(void) -{ - static unsigned int potentiomitorAdcValue = 0; - static float spdCmdHz = 0; - static float spdCmdHzLast = SDP_MAX_VALUE; - HAL_ADC_SoftTrigSample(&ADC_HANDLE, ADC_SOC_NUM9); /* Get the speed adjustment resistance. */ - potentiomitorAdcValue = HAL_ADC_GetConvResult(&ADC_HANDLE, ADC_SOC_NUM9); - /* 4045.0 is adc sample max value of potentiomitor */ - spdCmdHz = (float)potentiomitorAdcValue / 4045.0 * SDP_MAX_VALUE; - if (spdCmdHz < SDP_MIN_VALUE) { /* Speed protection. */ - spdCmdHz = SDP_MIN_VALUE; - } - if (spdCmdHz > SDP_MAX_VALUE) { - spdCmdHz = SDP_MAX_VALUE; - } - float delta = spdCmdHzLast > spdCmdHz ? (spdCmdHzLast - spdCmdHz) : (spdCmdHz - spdCmdHzLast); - /* 1.0 : If the speed fluctuation is less than 1.0Hz, no change is made. */ - if (delta < 1.0) { - return; - } - spdCmdHzLast = spdCmdHz; - g_mc.spdCmdHz = spdCmdHz; -} - -/** - * @brief Config the KEY func. - * @param handle The GPIO handle. - * @retval None. - */ -static KEY_State Key_StateRead(GPIO_Handle *handle) -{ - if (HAL_GPIO_GetPinValue(handle, handle->pins) == 0) { - BASE_FUNC_DELAY_MS(30); /* delay 30ms for deshake */ - if (HAL_GPIO_GetPinValue(handle, handle->pins) == 0) { - while (HAL_GPIO_GetPinValue(handle, handle->pins) == 0) { - } - return KEY_DOWN; - } - } - return KEY_UP; -} - -/** - * @brief Control motor start and stop state by key func. - * @param param The GPIO handle. - * @retval None. - */ -void MotorStartStopKeyCallback(void *param) -{ - GPIO_Handle *handle = (GPIO_Handle *)param; - if (Key_StateRead(handle) == KEY_DOWN) { - if (SysIsRunning(&g_mc.statusReg)) { /* stop apt output, motor is off status */ - SysCmdStopSet(&g_mc.statusReg); - } else { /* start apt output, motor is on status */ - HAL_APT_StartModule(RUN_APT0 | RUN_APT1 | RUN_APT2); - SysCmdStartSet(&g_mc.statusReg); - } - } -} - -/** - * @brief User application entry. - * @retval BSP_OK. - */ -int MotorMainProcess(void) -{ - SystemInit(); - /* System Initialization. */ - unsigned int tickNum100Ms = 200; /* 100ms tick */ - static unsigned int tickCnt100Ms = 0; - unsigned int tickNum500Ms = 1000; /* 500ms tick */ - static unsigned int tickCnt500Ms = 0; - - HAL_TIMER_Start(&g_timer1); - /* Disable PWM output before startup. */ - MotorPwmOutputDisable(g_aptCp); - InitSoftware(); - /* Start the PWM clock. */ - HAL_APT_StartModule(RUN_APT0 | RUN_APT1 | RUN_APT2); - - while (true) { - if (g_mc.msTickCnt - tickCnt100Ms >= tickNum100Ms) { - tickCnt100Ms = g_mc.msTickCnt; - /* User Code 100ms Event */ - AdjustSpeedFunction(); - } - if (g_mc.msTickCnt - tickCnt500Ms >= tickNum500Ms) { - if (SysIsError(&g_mc.statusReg) == true) { - break; - } - /* The LED blinks when no status is not error. */ - HAL_GPIO_TogglePin(&SYS_LED_HANDLE, SYS_LED_PIN); - tickCnt500Ms = g_mc.msTickCnt; - } - } - return 0; -} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/application/mcs_motor_process.h b/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/application/mcs_motor_process.h deleted file mode 100644 index a2fdeb2c..00000000 --- a/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/application/mcs_motor_process.h +++ /dev/null @@ -1,47 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_motor_process.h - * @author MCU Algorithm Team - * @brief This file provides motor sample functions declaration for ECBMCU201MPC board. - */ -#ifndef McuMagicTag_MCS_MOTOR_PROCESS_H -#define McuMagicTag_MCS_MOTOR_PROCESS_H - -#include "main.h" - -/** - * @brief phase sequence. - */ -typedef enum { - PHASE_U = 0, - PHASE_V, - PHASE_W, - PHASE_MAX_NUM -} MCS_PhaseEnum; - -/** - * @brief key state. - */ -typedef enum { - KEY_DOWN = 0, - KEY_UP = 1, -} KEY_State; - -int MotorMainProcess(void); - -#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/application/mcs_user_config.h b/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/application/mcs_user_config.h deleted file mode 100644 index 6f88c4c4..00000000 --- a/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/application/mcs_user_config.h +++ /dev/null @@ -1,94 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_config.h - * @author MCU Algorithm Team - * @brief This file provides config macros for ECBMCU201MPC app. - */ -#ifndef McuMagicTag_MCS_CONFIG_H -#define McuMagicTag_MCS_CONFIG_H - -#if defined(CHIP_3061MNPICA) || defined(CHIP_3061MNNICA) || defined(CHIP_3061MNPIC8) || defined(CHIP_3061MNNIC8) -#define ADC_U_SOC_NUM ADC_SOC_NUM2 -#define ADC_V_SOC_NUM ADC_SOC_NUM5 -#define ADC_W_SOC_NUM ADC_SOC_NUM6 -#define ADC_SPD_ADJ_SOC_NUM ADC_SOC_NUM9 -#define ADC_HANDLE g_adc0 -#endif - -#if defined (CHIP_3066MNPIRH) || defined (CHIP_3065PNPIRH) || defined (CHIP_3065PNPIRE) || defined (CHIP_3065PNPIRA) -#define ADC_U_SOC_NUM ADC_SOC_NUM3 -#define ADC_V_SOC_NUM ADC_SOC_NUM5 -#define ADC_W_SOC_NUM ADC_SOC_NUM7 -#define ADC_SPD_ADJ_SOC_NUM ADC_SOC_NUM9 - -#define ADC_HANDLE g_adc2 -#endif - -#define APT_PWM_FREQ 12000 /* PWM frequency 12K Hz. */ -#define CTRL_CURR_PERIOD (1.0f / APT_PWM_FREQ) /* carrier ISR period (S) */ -#define CTRL_CURE_PERIOD_US 83 - -#define SYSTICK_PERIOD_US 500u /* systick period. unit : us */ - -#define INV_CAP_CHARGE_MS 3u /* Bootstrap capacitor charge time. unit : ms */ - -#define CTRL_SYSTICK_PERIOD 0.0005f /* systick control period, 500us */ - -#define USER_SPD_SLOPE 60.0f /* slope of velocity change */ - -#define IN_VOLTAGE_BUS 24 /* Motor input voltage */ -#define POLES 14 /* logarithm of motor pole */ -#define MATH_PI 3.14 /* Pi */ -#define MOTOR_K 0.66 /* Torque Coefficient */ - -#define RAMP_STP 60 /* Slope of forced draging acceleration */ -#define RAMP_DUTY_PWM 0.5 /* 1.0% */ - -#define FORCE_DRAG_MINDUTY 10 /* APT minimum duty cycle during forced drag */ -#define FORCE_DRAG_MAXDUTY 15 /* APT maximum duty cycle during forced drag */ - -#define DRAG_START_INTERVAL 1000 /* Force drag change phase time */ -#define DRAG_STOP_INTERVAL 400 /* Force drag change phase time */ - -/* Parameters of the motor in the RUN */ -#define FILTER_COUNT 3 /* Filter Times */ -#define PHASE_OFFSET 9000 /* Phase delay due to filtering */ - -#define APT_DUTYLIMIT_MAX 99.9 /* Maximum duty cycle of the output APT */ -#define APT_DUTYLIMIT_MIN 8.0 /* Minimum duty cycle of the output APT */ - -#define BRIDGE_CTR_APT_U APT0 /* APT address that controls the U phase */ -#define BRIDGE_CTR_APT_V APT1 /* APT address that controls the V phase */ -#define BRIDGE_CTR_APT_W APT2 /* APT address that controls the W phase */ - -#define SDP_MAX_VALUE 77.00 /* Maximum change phase frequency */ -#define SDP_MIN_VALUE 12.0 /* Minimum change phase frequency */ -#define SDP_TARGET_VALUE 77.00 /* Target change phase frequency */ - -/* SPD PID Param */ -#define SPD_PID_KP 0.05 /* P parameter of PID control */ -#define SPD_PID_KI 6 /* I parameter of PID control */ -#define SPD_PID_TS 0.0005 /* TS parameter of PID control cycle */ - -#define OP_TO_CL_INTERGRAL 8.0 /* Open-loop switching closed-loop integral term to prevent sudden current change. */ - -#define VOL_DIVIDER_COEFFICIENT 0.09091f /* Division coefficient of the zero-crossing detection sampling circuit */ - -#define SPD_FILTER_FC 30 - -#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/common/mcs_carrier.c b/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/common/mcs_carrier.c deleted file mode 100644 index 7779fbef..00000000 --- a/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/common/mcs_carrier.c +++ /dev/null @@ -1,224 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_carrier.c - * @author MCU Algorithm Team - * @brief This file provides carrier interrupt application for motor control. - */ - -#include "mcs_carrier.h" -#include "mcs_user_config.h" -#include "mcs_assert.h" -#include "debug.h" - -#define S_TO_US 1000000 -#define S_TO_SYSTICK 100000000 - -/** - * @brief Sets the duty cycle of the H-bridge APT. - * @param aptHandle APT module handle. - * @param duty PWM duty. Range: 0.1 ~ 99.9. - * @retval None. - */ -void MCS_SetCtrAptDuty(MtrCtrlHandle *mtrCtrl, unsigned int duty) -{ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - MCS_ASSERT_PARAM(duty > 0); - /* Set pwm duty of uvw pahse */ - HAL_APT_SetPWMDutyByNumber(mtrCtrl->stepCtrl.controlApt.u, duty); - HAL_APT_SetPWMDutyByNumber(mtrCtrl->stepCtrl.controlApt.v, duty); - HAL_APT_SetPWMDutyByNumber(mtrCtrl->stepCtrl.controlApt.w, duty); -} - -/** - * @brief Strong drag start. - * @param mtrCtrl The motor control handle. - * @retval None. - */ -static void ForceDragAcc(MtrCtrlHandle *mtrCtrl) -{ - /* Verifying Parameters. */ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - unsigned int voltageDValue; - unsigned int dragChangeFreq; - unsigned int timerperiod; - unsigned int freqLast; - unsigned int aptCountsOneUs; - - mtrCtrl->sysVar.accTimeCnt++; - if (mtrCtrl->sysVar.accTimeCnt < mtrCtrl->sysVar.dragChangePhaseTime) { - return; - } - - mtrCtrl->sysVar.accTimeCnt = 0; - /* Step 1: Calculate the voltage difference. */ - voltageDValue = IN_VOLTAGE_BUS * RAMP_DUTY_PWM; - /* Step 2: Calculate the commutation frequency. */ - freqLast = S_TO_US / (mtrCtrl->sysVar.dragChangePhaseTime * CTRL_CURE_PERIOD_US); - dragChangeFreq = voltageDValue * STEP_MAX_NUM * POLES / (MOTOR_K * 2 * MATH_PI) + freqLast; /* 2*PI = 360° */ - /* Step 3: Convert the commutation frequency to the commutation count value. */ - timerperiod = mtrCtrl->stepCtrl.controlApt.u->waveform.timerPeriod * CTRL_CURE_PERIOD_US; - mtrCtrl->sysVar.dragChangePhaseTime = S_TO_US / (timerperiod * dragChangeFreq); - - /* Determine whether the change phase speed has reached the speed of stopping forced drag. */ - if (mtrCtrl->sysVar.dragChangePhaseTime < DRAG_STOP_INTERVAL) { - mtrCtrl->sysVar.dragChangePhaseTime = DRAG_STOP_INTERVAL; - mtrCtrl->sysVar.accTimeCnt = 0; - mtrCtrl->sysVar.bemfFilterCnt = 0; - mtrCtrl->stateMachine = FSM_RUN; - mtrCtrl->sysVar.lastZeroPoint = DCL_SYSTICK_GetTick(); - mtrCtrl->spdPi.integral = OP_TO_CL_INTERGRAL; - } - - /* Shorten the time interval of forced drag change phase. */ - mtrCtrl->pwmDuty += RAMP_DUTY_PWM; - aptCountsOneUs = HAL_CRG_GetIpFreq(APT0_BASE) / S_TO_US; - mtrCtrl->sysVar.waitTime = (mtrCtrl->sysVar.dragChangePhaseTime * CTRL_CURE_PERIOD_US * aptCountsOneUs) >> 1; - /* Change phase time is 2 the waiting time. */ - mtrCtrl->spdRefHz = (float)(HAL_CRG_GetIpFreq(SYSTICK_BASE) / (mtrCtrl->sysVar.waitTime * 2)) / STEP_MAX_NUM; - mtrCtrl->spdEstHz = mtrCtrl->spdRefHz; - mtrCtrl->spdRmg.yLast = mtrCtrl->spdRefHz; - mtrCtrl->stepCtrl.phaseStep = (mtrCtrl->stepCtrl.phaseStep + 1) % STEP_MAX_NUM; - SixStepPwm(&mtrCtrl->stepCtrl); - /* Limiting the PWM Duty Cycle Range. */ - if (mtrCtrl->pwmDuty > FORCE_DRAG_MAXDUTY) { - mtrCtrl->pwmDuty = FORCE_DRAG_MAXDUTY; - } - if (mtrCtrl->pwmDuty < FORCE_DRAG_MINDUTY) { - mtrCtrl->pwmDuty = FORCE_DRAG_MINDUTY; - } - MCS_SetCtrAptDuty(mtrCtrl, mtrCtrl->pwmDuty); -} - -/** - * @brief Zero-crossing filter. - * @param mtrCtrl The motor control handle. - * @retval None. - */ -static void BemfZeroSampleFilter(MtrCtrlHandle *mtrCtrl) -{ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - mtrCtrl->sysVar.bemfFilterCnt++; - /* Filter the number of samples. */ - if (mtrCtrl->sysVar.bemfFilterCnt >= FILTER_COUNT) { - mtrCtrl->sysVar.bemfFilterCnt = 0; - unsigned int currentTick = DCL_SYSTICK_GetTick(); - if (mtrCtrl->sysVar.firstEventFilterFlag == 0) { - mtrCtrl->sysVar.lastZeroPoint = currentTick; - mtrCtrl->sysVar.changePhaseFlag = 1; - mtrCtrl->sysVar.firstEventFilterFlag = 1; - return; - } - /* Calculate the time interval between the last zero crossing. */ - mtrCtrl->sysVar.stepTime[mtrCtrl->stepCtrl.phaseStep] = currentTick >= mtrCtrl->sysVar.lastZeroPoint - ? currentTick - mtrCtrl->sysVar.lastZeroPoint - : SYSTICK_MAX_VALUE - mtrCtrl->sysVar.lastZeroPoint + currentTick + 1; - mtrCtrl->sysVar.stepTimeNum++; - /* Wait time is half the interval. */ - mtrCtrl->sysVar.waitTime = mtrCtrl->sysVar.stepTime[mtrCtrl->stepCtrl.phaseStep] >> 1; - mtrCtrl->sysVar.waitTime -= PHASE_OFFSET; - mtrCtrl->sysVar.lastZeroPoint = currentTick; - /* Update flag. */ - mtrCtrl->sysVar.changePhaseFlag = 1; - } -} - -/** - * @brief Back EMF zero crossing check. - * @param mtrCtrl The motor control handle. - * @retval None. - */ -static void BemfZeroCheck(MtrCtrlHandle *mtrCtrl) -{ - /* The step corresponds to different conditional judgments. */ - unsigned int bemfCheckTable[STEP_MAX_NUM] = {mtrCtrl->bemf.w < mtrCtrl->zeroPoint ? 1 : 0, - mtrCtrl->bemf.v > mtrCtrl->zeroPoint ? 1 : 0, - mtrCtrl->bemf.u < mtrCtrl->zeroPoint ? 1 : 0, - mtrCtrl->bemf.w > mtrCtrl->zeroPoint ? 1 : 0, - mtrCtrl->bemf.v < mtrCtrl->zeroPoint ? 1 : 0, - mtrCtrl->bemf.u > mtrCtrl->zeroPoint ? 1 : 0}; - - if (bemfCheckTable[mtrCtrl->stepCtrl.phaseStep] == 1) { - BemfZeroSampleFilter(mtrCtrl); - } -} - -/** - * @brief Change phase and speed estimation. - * @param mtrCtrl The motor control handle. - * @retval None. - */ -static void ChangePhase(MtrCtrlHandle *mtrCtrl) -{ - unsigned int currentTick = DCL_SYSTICK_GetTick(); /* Get the current tick value. */ - unsigned int intervalTick = (currentTick >= mtrCtrl->sysVar.lastZeroPoint) - ? currentTick - mtrCtrl->sysVar.lastZeroPoint - : SYSTICK_MAX_VALUE - mtrCtrl->sysVar.lastZeroPoint + currentTick + 1; - /* Start average filtering when the number of recorded data reaches six. */ - if (mtrCtrl->sysVar.stepTimeNum >= STEP_MAX_NUM) { - mtrCtrl->sysVar.stepTimeNum = 0; - mtrCtrl->sysVar.stepTimeFilterEnable = 1; - } - /* If the accumulated time is greater than the waiting commutation time, commutation is performed. */ - if (intervalTick > mtrCtrl->sysVar.waitTime) { - if (mtrCtrl->sysVar.stepTimeFilterEnable) { /* 6-step commutation time averaging if filtering is enabled. */ - unsigned int totalTime = mtrCtrl->sysVar.stepTime[STEP1] + mtrCtrl->sysVar.stepTime[STEP2] - + mtrCtrl->sysVar.stepTime[STEP3] + mtrCtrl->sysVar.stepTime[STEP4] - + mtrCtrl->sysVar.stepTime[STEP5] + mtrCtrl->sysVar.stepTime[STEP6]; - mtrCtrl->spdEstHz = (float)HAL_CRG_GetIpFreq(SYSTICK_BASE) / totalTime; - } else { - mtrCtrl->spdEstHz = (float)HAL_CRG_GetIpFreq(SYSTICK_BASE) / (mtrCtrl->sysVar.waitTime << 1) / STEP_MAX_NUM; - } - /* Change phase. */ - mtrCtrl->stepCtrl.phaseStep = (mtrCtrl->stepCtrl.phaseStep + 1) % STEP_MAX_NUM; - SixStepPwm(&mtrCtrl->stepCtrl); - mtrCtrl->sysVar.changePhaseFlag = 0; - } -} - -/** - * @brief Zero-crossing detection and change phase - * @param mtrCtrl The motor control handle. - * @retval None. - */ -void MCS_CarrierProcess(MtrCtrlHandle *mtrCtrl) -{ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - /* Offset value of the calibration value of the three-phase current */ - - switch (mtrCtrl->stateMachine) { - case FSM_STARTUP: - ForceDragAcc(mtrCtrl); /* Forced drag. */ - break; - - case FSM_RUN: - case FSM_WAIT_STOP: - mtrCtrl->readBemfUVW(&mtrCtrl->bemf); - MCS_SetCtrAptDuty(mtrCtrl, mtrCtrl->pwmDuty); - if (mtrCtrl->sysVar.changePhaseFlag) { - /* Change phase process. */ - ChangePhase(mtrCtrl); - } else { - /* Zero-crossing detection procedure. */ - BemfZeroCheck(mtrCtrl); - } - break; - - default: - break; - } -} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/common/mcs_carrier.h b/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/common/mcs_carrier.h deleted file mode 100644 index 05fc2666..00000000 --- a/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/common/mcs_carrier.h +++ /dev/null @@ -1,91 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_carrier.h - * @author MCU Algorithm Team - * @brief This file provides functions declaration for carrier interrupt processing function. - */ -#ifndef McuMagicTag_MCS_CARRIER_H -#define McuMagicTag_MCS_CARRIER_H - -#include "mcs_status.h" -#include "mcs_six_step.h" -#include "mcs_ramp_mgmt.h" -#include "mcs_spd_ctrl.h" -#include "mcs_fsm.h" - -/** - * @brief Three-phase static coordinate frame variable. - */ -typedef struct { - unsigned int u; /**< Component u of the three-phase static coordinate frame variable. */ - unsigned int v; /**< Component v of the three-phase static coordinate frame variable. */ - unsigned int w; /**< Component w of the three-phase static coordinate frame variable. */ -} UVWBemf; - -typedef void (*ReadBemf)(UVWBemf *bemfUVW); - -/** - * @brief The definition of the systematic global variables - */ -typedef struct { - unsigned char changePhaseFlag; /**< Flag indicating that delay change phase is required */ - unsigned char firstEventFilterFlag; /**< First zero-crossing event filtering flag. */ - unsigned int dragChangePhaseTime; /**< Interval for forced drag acceleration */ - unsigned int accTimeCnt; /**< Acceleration time count */ - - unsigned int bemfFilterCnt; /**< Number of sample filtering times for zero-crossing detection. */ - - unsigned int lastZeroPoint; /**< Time of the last zero point */ - unsigned int waitTime; /**< Time to wait for change phase */ - unsigned int stepTime[STEP_MAX_NUM]; /**< Record the time spent on each step */ - unsigned int stepTimeNum; /**< Number of data records in the steptime array. */ - unsigned int stepTimeFilterEnable; -} SysVariable; - -typedef struct { - volatile float spdCmdHz; /**< Set target change phase frequency */ - float spdRefHz; /**< Command values after speed ramp management */ - float spdEstHz; /**< Actual change phase frequency of feedback */ - float pwmDuty; /**< APT duty cycle */ - unsigned int zeroPoint; /**< Adc value of zero point */ - - unsigned short aptMaxcntCmp; /**< Apt Maximum Comparison Count */ - - unsigned short sysTickCnt; /**< System Timer Tick Count */ - unsigned short capChargeTickNum; /**< Bootstrap Capacitor Charge Tick Count */ - unsigned short msTickNum; /**< Number of ticks corresponding to 1 ms */ - volatile unsigned int msTickCnt; /**< Millisecond-level counter, which can be used in 1-ms and 5-ms tasks. */ - - RMG_Handle spdRmg; /**< Ramp management struct for the speed controller input reference */ - PID_Handle spdPi; /**< PI controller struct in the speed controller. */ - - UVWBemf bemf; /**< Three-phase back electromotive force */ - SixStepHandle stepCtrl; /**< Control structure of six-step square wave */ - - SysVariable sysVar; /**< System Variables */ - SysStatusReg statusReg; /**< System Status */ - FsmState stateMachine; /**< BLDC Motor Control State Machine */ - - ReadBemf readBemfUVW; /**< Function interface for obtaining the three-phase electromotive force */ -} MtrCtrlHandle; - -void MCS_CarrierProcess(MtrCtrlHandle *mtrCtrl); - -void MCS_SetCtrAptDuty(MtrCtrlHandle *mtrCtrl, unsigned int duty); - -#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/common/mcs_fsm.h b/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/common/mcs_fsm.h deleted file mode 100644 index b3c79ea2..00000000 --- a/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/common/mcs_fsm.h +++ /dev/null @@ -1,55 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_fsm.h - * @author MCU Algorithm Team - * @brief This file provides the definition of finite statemachine (FSM). - */ - -#ifndef McuMagicTag_MCS_FSM_H -#define McuMagicTag_MCS_FSM_H - -/** - * @brief motor control FSM state define. - * @details motor control FSM state type: - * + FSM_IDLE -- IDLE state, system startup default. - * + FSM_OFFSET_CALIB -- Self calibrate, for ADC init. - * + FSM_CAP_CHARGE -- IPM cap charge. - * + FSM_CLEAR -- Clear before every run. - * + FSM_LOCATE -- Rotor position pre-locate. - * + FSM_STARTUP -- Start up. - * + FSM_SWITCH -- Transition state, control from open to closed loop. - * + FSM_RUN -- Normal running state. - * + FSM_WAIT_STOP -- Wait stopping. - * + FSM_STOP -- Normal stop. - * + FSM_FAULT -- Fault state, waiting for user process. - */ -typedef enum { - FSM_IDLE = 0, - FSM_OFFSET_CALIB, - FSM_CAP_CHARGE, - FSM_CLEAR, - FSM_LOCATE, - FSM_STARTUP, - FSM_SWITCH, - FSM_RUN, - FSM_WAIT_STOP, - FSM_STOP, - FSM_FAULT -} FsmState; - -#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/func/mcs_six_step.c b/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/func/mcs_six_step.c deleted file mode 100644 index c86e609c..00000000 --- a/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/func/mcs_six_step.c +++ /dev/null @@ -1,126 +0,0 @@ -/** - * @ Copyright (c) HiSilicon (Shanghai) Technologies Co., Ltd. 2022-2023. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_six_step.c - * @author MCU Algorithm Team - * @brief This file provides functions to control the H bridge. - */ - -#include "mcs_assert.h" -#include "mcs_six_step.h" - -/* Mode for disabling or enabling software-based forced output */ -#define APT_PWM_MODE 0 -#define APT_FRC_MODE 1 - -#define PWM_SIGNAL 1 - -#define FRC_LOW APT_OUT_CTRL_ACTION_LOW -#define FRC_HIGH APT_OUT_CTRL_ACTION_HIGH - -/** - * @brief Force PWM output by APT - * @param APTx The APT to use - * @param PwmAout Force to be high or low - * @param PwmAenble PWMA signal software force mode - * @param PwmBout Force to be high or low - * @param PwmBenble PWMB signal software force mode - */ -static void APT_ForcePwmByApt(APT_RegStruct *aptx, APT_Act actMode) -{ - MCS_ASSERT_PARAM(aptx != NULL); - switch (actMode) { - case APT_CHA_PWM_CHB_LOW: - /* Channel A: 0 means not force output enable, channel A output PWM. */ - DCL_APT_DisableSwContPWMAction(aptx, APT_PWM_CHANNEL_A); - DCL_APT_SetSwContPWMAction(aptx, APT_PWM_CHANNEL_A, APT_PWM_CONTINUOUS_ACTION_LOW); - /* Channel B: 1 means force output enable. */ - DCL_APT_EnableSwContPWMAction(aptx, APT_PWM_CHANNEL_B); - /* Channel B: 2 means channel B force output LOW due to the A_H_B_L invert. */ - DCL_APT_SetSwContPWMAction(aptx, APT_PWM_CHANNEL_B, APT_PWM_CONTINUOUS_ACTION_HIGH); - break; - case APT_CHA_LOW_CHB_HIGH: - /* Channel A: 1 means force output enable. */ - /* Channel A: 1 means channel A force output LOW. */ - DCL_APT_EnableSwContPWMAction(aptx, APT_PWM_CHANNEL_A); - DCL_APT_SetSwContPWMAction(aptx, APT_PWM_CHANNEL_A, APT_PWM_CONTINUOUS_ACTION_LOW); - /* Channel B: 1 means force output enable. */ - /* Channel B: 2 means channel B force output HIGH. */ - DCL_APT_EnableSwContPWMAction(aptx, APT_PWM_CHANNEL_B); - DCL_APT_SetSwContPWMAction(aptx, APT_PWM_CHANNEL_B, APT_PWM_CONTINUOUS_ACTION_HIGH); - break; - case APT_CHA_LOW_CHB_LOW: - /* Channel A: 1 means force output enable. */ - /* Channel A: 1 means channel A force output LOW. */ - DCL_APT_EnableSwContPWMAction(aptx, APT_PWM_CHANNEL_A); - DCL_APT_SetSwContPWMAction(aptx, APT_PWM_CHANNEL_A, APT_PWM_CONTINUOUS_ACTION_LOW); - /* Channel B: 1 means force output enable. */ - /* Channel B: 1 means channel A force output HIGH due to the A_H_B_L invert. */ - DCL_APT_EnableSwContPWMAction(aptx, APT_PWM_CHANNEL_B); - DCL_APT_SetSwContPWMAction(aptx, APT_PWM_CHANNEL_B, APT_PWM_CONTINUOUS_ACTION_LOW); - break; - default: - break; - } -} - -/** - * @brief Output six-step square wave. - * @param handle The SixStepHandle. - * @retval None. - */ -void SixStepPwm(const SixStepHandle *handle) -{ - MCS_ASSERT_PARAM(handle != NULL); - APT_RegStruct *aptU = handle->controlApt.u->baseAddress; /* Get apt u\v\w baseaddress. */ - APT_RegStruct *aptV = handle->controlApt.v->baseAddress; - APT_RegStruct *aptW = handle->controlApt.w->baseAddress; - switch (handle->phaseStep) { - case STEP1: /* U+, V- */ - APT_ForcePwmByApt(aptU, APT_CHA_PWM_CHB_LOW); - APT_ForcePwmByApt(aptV, APT_CHA_LOW_CHB_LOW); - APT_ForcePwmByApt(aptW, APT_CHA_LOW_CHB_HIGH); - break; - case STEP2: /* U+, W- */ - APT_ForcePwmByApt(aptU, APT_CHA_PWM_CHB_LOW); - APT_ForcePwmByApt(aptV, APT_CHA_LOW_CHB_HIGH); - APT_ForcePwmByApt(aptW, APT_CHA_LOW_CHB_LOW); - break; - case STEP3: /* V+, W- */ - APT_ForcePwmByApt(aptU, APT_CHA_LOW_CHB_HIGH); - APT_ForcePwmByApt(aptV, APT_CHA_PWM_CHB_LOW); - APT_ForcePwmByApt(aptW, APT_CHA_LOW_CHB_LOW); - break; - case STEP4: /* V+, U- */ - APT_ForcePwmByApt(aptU, APT_CHA_LOW_CHB_LOW); - APT_ForcePwmByApt(aptV, APT_CHA_PWM_CHB_LOW); - APT_ForcePwmByApt(aptW, APT_CHA_LOW_CHB_HIGH); - break; - case STEP5: /* W+, U- */ - APT_ForcePwmByApt(aptU, APT_CHA_LOW_CHB_LOW); - APT_ForcePwmByApt(aptV, APT_CHA_LOW_CHB_HIGH); - APT_ForcePwmByApt(aptW, APT_CHA_PWM_CHB_LOW); - break; - case STEP6: /* W+, V- */ - APT_ForcePwmByApt(aptU, APT_CHA_LOW_CHB_HIGH); - APT_ForcePwmByApt(aptV, APT_CHA_LOW_CHB_LOW); - APT_ForcePwmByApt(aptW, APT_CHA_PWM_CHB_LOW); - break; - default: - break; - } -} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/func/mcs_six_step.h b/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/func/mcs_six_step.h deleted file mode 100644 index 08198c4c..00000000 --- a/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/func/mcs_six_step.h +++ /dev/null @@ -1,79 +0,0 @@ -/** - * @ Copyright (c) HiSilicon (Shanghai) Technologies Co., Ltd. 2022-2023. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_six_step.c - * @author MCU Algorithm Team - * @brief The header file contains the following declaration: - * + StepEnum enum definition. - * + PhaseEnum enum definition. - * + UVW_AptReg structure definition. - * + SixStepHandle handle structure definition. - * + AptOutCombination structure definition. - * + Six Step Pwm Function And Six Step Pwm Overlap Function. - */ - -#ifndef MCS_SIX_STEP_H -#define MCS_SIX_STEP_H - -#include "apt.h" - -typedef enum { - STEP1 = 0, - STEP2, - STEP3, - STEP4, - STEP5, - STEP6, - STEP_MAX_NUM -} StepEnum; - -typedef enum { - U = 0, - V, - W, - PHASE_MAX_NUMS -} PhaseEnum; - -typedef enum { - APT_CHA_PWM_CHB_LOW, - APT_CHA_LOW_CHB_HIGH, - APT_CHA_LOW_CHB_LOW -} APT_Act; - -/** - * @brief Three-phase static coordinate frame variable. - */ -typedef struct { - APT_Handle *u; /**< Apt handle for controlling phase U. */ - APT_Handle *v; /**< Apt handle for controlling phase V. */ - APT_Handle *w; /**< Apt handle for controlling phase W. */ -} UVW_AptReg; - -typedef struct { - unsigned char phaseStep; /* Current step. */ - UVW_AptReg controlApt; /* Apt handles for controlling H bridge. */ -} SixStepHandle; - -typedef struct { - unsigned int upperout; /* High tube level */ - unsigned int upperstate; /* High tube state */ - unsigned int lowerout; /* Low tube level */ - unsigned int lowerstate; /* Low tube state */ -} AptOutCombination; - -void SixStepPwm(const SixStepHandle *handle); -#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/func/mcs_status.h b/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/func/mcs_status.h deleted file mode 100644 index d92ed312..00000000 --- a/vendor/yibaina_3061M/demo/GIM4310-10_Six_Step_Wave/user/mcs_sensorless_bldc_six_step_wave/func/mcs_status.h +++ /dev/null @@ -1,188 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_status.h - * @author MCU Algorithm Team - * @brief This file provides functions declaration of system status. - */ - -/* Define to prevent recursive inclusion ------------------------------------------------------- */ -#ifndef McuMagicTag_MCS_STATUS_H -#define McuMagicTag_MCS_STATUS_H - -/* Includes ------------------------------------------------------------------------------------ */ -#include "typedefs.h" -#include "mcs_assert.h" - -/* Typedef definitions ------------------------------------------------------------------------- */ -/** - * @brief System status define - */ -typedef union { - unsigned short all; - struct { - unsigned short cmdStart : 1; /**< Indicates that a start system command has been received. */ - unsigned short cmdStop : 1; /**< Indicates that a stop system command has been received. */ - unsigned short isRunning : 1; /**< Indicates that the system is running (enable signal) */ - unsigned short sysError : 1; /**< Indicates that the system reports an error. */ - unsigned short poweron : 1; /**< Indicates that the power-on initialization phase is complete. */ - unsigned short capcharge : 1; /**< Indicates that the bootstrap capacitor charging phase is complete. */ - unsigned short adczero : 1; /**< The current sampling point is reset to zero after power-on. */ - } BIT; -} SysStatusReg; - -/** - * @brief Get status of BIT cmdStart. - * @param sysStatus System status register handle. - * @retval Status of BIT cmdStart. - */ -static inline bool SysGetCmdStart(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - bool ret; - ret = (sysStatus->BIT.cmdStart == 1) ? true : false; - return ret; -} - -/** - * @brief Set BIT cmdStart. - * @param sysStatus System status register handle. - * @retval None. - */ -static inline void SysCmdStartSet(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - sysStatus->BIT.cmdStart = 1; -} - -/** - * @brief Clear BIT cmdStart. - * @param handle System status register handle. - * @retval None. - */ -static inline void SysCmdStartClr(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - sysStatus->BIT.cmdStart = 0; -} - -/** - * @brief Get status of BIT cmdStop. - * @param sysStatus System status register handle. - * @retval Status of BIT cmdStart. - */ -static inline bool SysGetCmdStop(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - bool ret; - ret = (sysStatus->BIT.cmdStop == 1) ? true : false; - return ret; -} - -/** - * @brief Set BIT cmdStop. - * @param sysStatus System status register handle. - * @retval None. - */ -static inline void SysCmdStopSet(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - sysStatus->BIT.cmdStop = 1; -} - -/** - * @brief Clear BIT cmdStop. - * @param sysStatus System status register handle. - * @retval None. - */ -static inline void SysCmdStopClr(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - sysStatus->BIT.cmdStop = 0; -} - -/** - * @brief Get status of BIT isRunning. - * @param sysStatus System status register handle. - * @retval Status of BIT isRunning. - */ -static inline bool SysIsRunning(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - bool ret; - ret = (sysStatus->BIT.isRunning == 1) ? true : false; - return ret; -} - -/** - * @brief Set BIT isRuning. - * @param sysStatus System status register handle. - * @retval None. - */ -static inline void SysRunningSet(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - sysStatus->BIT.isRunning = 1; -} - -/** - * @brief Clear BIT isRuning. - * @param sysStatus System status register handle. - * @retval None. - */ -static inline void SysRunningClr(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - sysStatus->BIT.isRunning = 0; -} - -/** - * @brief Get status of BIT sysError. - * @param sysStatus System status register handle. - * @retval Status of BIT sysError. - */ -static inline bool SysIsError(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - bool ret; - ret = (sysStatus->BIT.sysError == 1) ? true : false; - return ret; -} - -/** - * @brief Set BIT sysError. - * @param sysStatus System status register handle. - * @retval None. - */ -static inline void SysErrorSet(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - sysStatus->BIT.sysError = 1; -} - -/** - * @brief Clear BIT sysError. - * @param sysStatus System status register handle. - * @retval None. - */ -static inline void SysErrorClr(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - sysStatus->BIT.sysError = 0; -} - -#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/readme.md b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/readme.md deleted file mode 100644 index 8350bef0..00000000 --- a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/readme.md +++ /dev/null @@ -1,22 +0,0 @@ -# pmsm_pos_qdm_foc - -**【功能描述】** - -- 基于ECMCU105H/ECBMCU201MPC单板的单电机双电阻采样的位置-速度-电流闭环Foc应用 - -**【环境要求】** - -- 所有单板电源改制为演示用的24V低压,电机选用42JSF630AS-1000 -- 电机ABZ编码器线序分别对应功率板上ABZ通道接口 -- 电机(黄绿蓝)相线分别对应功率板上U/V/W通道接口 - -**【配置方法】** - -- 将user文件夹替换原工程user文件夹即可 - -【操作说明】 - -- 点击开发板按键启动电机 - -- 打开串口小助手,输入期望角度(弧度值),电机会转到相应角度,6.28为一圈 -- 开发板蓝色电位器,可以调节电机转动的快慢 \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/generatecode/feature.h b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/generatecode/feature.h deleted file mode 100644 index a1f9eb27..00000000 --- a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/generatecode/feature.h +++ /dev/null @@ -1,93 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file feature.h - * @author MCU Driver Team - * @brief This file contains macro configurations related to the project. This file is generated by the IDE tool. - */ - -#ifndef McuMagicTag_FEATURE_H -#define McuMagicTag_FEATURE_H - -/* Macro definitions --------------------------------------------------------- */ -#define CHIP_3061MNPICA MACRO_ENABLE - -#define MACRO_ENABLE 1 -#define MACRO_DISABLE 0 - -/* Macro switch */ -#define BASE_DEFINE_USE_ASSERT MACRO_ENABLE -#ifndef FLASH_CRC_CONFIG -#define FLASH_CRC_CONFIG -#endif /* #ifndef FLASH_CRC_CONFIG */ -#define BASE_MATH_SINCOS_MIDDLE_TABLE MACRO_ENABLE /**< This macro is used to control the table type when the - BASE_MATH_GetSinCos() queries the table. When the value of - this macro is MACRO_ENABLE, the error value obtained by the - BASE_MATH_GetSinCos() is relatively small, and the return - value of the function may be greater than or less than the - actual value. When the value of this macro is MACRO_DISABLE, - the error value obtained by the BASE_MATH_GetSinCos() is - relatively large. However, in the range [0°, 180°) and - [180°, 360°), the return value of the function is either - greater than or less than the actual value. */ - -/* Peripheral module macro switch--------------------------------------------- */ -#define BOARD_DIM_NUM 1 /**< Number of dimming handle arrays. */ - -#define BOARD_KEY_NUM 10 /**< Number of key handle arrays. */ -#define BOARD_KEY_PRESS_ON GPIO_HIGH_LEVEL /**< GPIO status corresponding to long press valid. */ -#define BOARD_KEY_PRESS_OFF GPIO_LOW_LEVEL /**< GPIO status corresponding to short press valid. */ - -#define BOARD_LED_SEG_NUM 4 /**< Number of segments. */ -#define BOARD_LED_SEGMENT_ON GPIO_HIGH_LEVEL /**< GPIO level status corresponding to valid segments. */ -#define BOARD_LED_SEGMENT_OFF GPIO_LOW_LEVEL /**< GPIO level status corresponding to invalid segments. */ - -#define BOARD_MKEY_SCHEME_NUMBER BOARD_MKEY_SCHEME_NUMBER_ONE /**< Define the scheme to be adopted. */ -#define BOARD_MKEY_OUT_NUM 4 /**< Number of GPIO pins used as output during scanning. */ -#define BOARD_MKEY_IN_NUM 4 /**< Number of GPIO pins used as input during scanning. */ -#define BOARD_MKEY_OUT_PIN_VALID GPIO_LOW_LEVEL /**< GPIO level status corresponding to the valid \ - status of the output GPIO in the key matrix. */ -#define BOARD_MKEY_OUT_PIN_INVALID GPIO_HIGH_LEVEL /**< GPIO level status corresponding to the \ - invalid status of the output GPIO in the key matrix. */ -#define BOARD_MKEY_IN_PIN_VALID GPIO_LOW_LEVEL /**< Indicates the GPIO level corresponding to the \ - valid status of the input GPIO in the key matrix. */ -#define BOARD_MKEY_IN_PIN_INVALID GPIO_HIGH_LEVEL /**< Indicates the GPIO level corresponding to the \ - invalid status of the input GPIO in the key matrix. */ - -#define BOARD_PULSES_NUM 2 /**< Number of pulse handles. */ - -#define BASE_DEFINE_SLIPAVERAGE_NUM 2 /**< Sliding average array length. */ - -#define LISTNODE_MAX 20 - -#define BASE_DEFINE_DMA_QUICKSTART - -#define XTRAIL_FREQ 30000000U - -#define DBG_USE_NO_PRINTF 0U -#define DBG_USE_UART_PRINTF 1U - -#define DBG_PRINTF_USE DBG_USE_UART_PRINTF -#if (DBG_PRINTF_USE == DBG_USE_UART_PRINTF) -#define DBG_PRINTF_UART_PORT UART0 -#endif - -/* USER CODE BEGIN 0 */ -/* USER CODE 区域内代码不会被覆盖,区域外会被生成的默认代码覆盖(其余USER CODE 区域同理) */ -/* USER CODE END 0 */ - -#endif /* McuMagicTag_FEATURE_H */ \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/generatecode/main.h b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/generatecode/main.h deleted file mode 100644 index 0750df8a..00000000 --- a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/generatecode/main.h +++ /dev/null @@ -1,101 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file main.h - * @author MCU Driver Team - * @brief This file contains driver init functions. - */ - -/* Define to prevent recursive inclusion ------------------------------------- */ -#ifndef McuMagicTag_SYSTEM_INIT_H -#define McuMagicTag_SYSTEM_INIT_H - -#include "adc.h" -#include "adc_ex.h" -#include "acmp.h" -#include "acmp_ex.h" -#include "apt.h" -#include "uart.h" -#include "uart_ex.h" -#include "qdm.h" -#include "gpio.h" -#include "timer.h" -#include "timer_ex.h" -#include "pga.h" -#include "crg.h" -#include "dma.h" -#include "dma_ex.h" -#include "iocmg.h" -#include "i2c.h" - -#define IO_SPEED_FAST 0x00U -#define IO_SPEED_SLOW 0x01U - -#define IO_DRV_LEVEL4 0x00U -#define IO_DRV_LEVEL3 0x01U -#define IO_DRV_LEVEL2 0x02U -#define IO_DRV_LEVEL1 0x03U - -#define XTAL_DRV_LEVEL4 0x03U -#define XTAL_DRV_LEVEL3 0x02U -#define XTAL_DRV_LEVEL2 0x01U -#define XTAL_DRV_LEVEL1 0x00U - -#define GpioStartStop_PIN GPIO_PIN_4 -#define GpioStartStop_HANDLE g_gpio2 - -extern ACMP_Handle g_acmp0; -extern QDM_Handle g_qdm1; -extern PGA_Handle g_pga0; -extern PGA_Handle g_pga1; -extern TIMER_Handle g_timer0; -extern TIMER_Handle g_timer1; -extern UART_Handle g_uart0; -extern APT_Handle g_apt0; -extern APT_Handle g_apt1; -extern APT_Handle g_apt2; -extern ADC_Handle g_adc0; - -extern DMA_Handle g_dmac; - -extern GPIO_Handle g_gpio2; -extern GPIO_Handle g_gpio1; -extern I2C_Handle g_i2c0; -extern TIMER_Handle g_timer2; -BASE_StatusType CRG_Config(CRG_CoreClkSelect *coreClkSelect); -void SystemInit(void); - -void UART0WriteInterruptCallback(void *handle); -void UART0ReadInterruptCallback(void *handle); - -void UART0InterruptErrorCallback(void *handle); -void MotorStatemachineCallBack(void *handle); -void TIMER1_DMAOverFlow_InterruptProcess(void *handle); -void CheckPotentiometerValueCallback(void *handle); -void TIMER0_DMAOverFlow_InterruptProcess(void *handle); -void MotorCarrierProcessCallback(void *aptHandle); -void MotorSysErrCallback(void *aptHandle); - -void UART0_TXDMACallback(void *handle); - -void MotorStartStopKeyCallback(void *param); - -/* USER CODE BEGIN 0 */ -/* USER CODE 区域内代码不会被覆盖,区域外会被生成的默认代码覆盖(其余USER CODE 区域同理) */ -/* USER CODE END 0 */ - -#endif /* McuMagicTag_SYSTEM_INIT_H */ \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/generatecode/system_init.c b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/generatecode/system_init.c deleted file mode 100644 index 188e0a86..00000000 --- a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/generatecode/system_init.c +++ /dev/null @@ -1,775 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file system_init.c - * @author MCU Driver Team - * @brief This file contains driver init functions. - */ - -#include "main.h" -#include "ioconfig.h" -#include "iocmg_ip.h" - -#define UART0_BAND_RATE 115200 - -BASE_StatusType CRG_Config(CRG_CoreClkSelect *coreClkSelect) -{ - CRG_Handle crg; - crg.baseAddress = CRG; - crg.pllRefClkSelect = CRG_PLL_REF_CLK_SELECT_HOSC; - crg.pllPreDiv = CRG_PLL_PREDIV_4; - crg.pllFbDiv = 48; /* PLL Multiplier 48 */ - crg.pllPostDiv = CRG_PLL_POSTDIV_2; - crg.coreClkSelect = CRG_CORE_CLK_SELECT_PLL; - crg.handleEx.pllPostDiv2 = CRG_PLL_POSTDIV2_3; - crg.handleEx.clk1MSelect = CRG_1M_CLK_SELECT_HOSC; - crg.handleEx.clk1MDiv = (25 - 1); /* The 1 MHz freq is equal to the input clock frequency / (clk_1m_div + 1). 25 is the div of the clk_1m in CLOCK. */ - - if (HAL_CRG_Init(&crg) != BASE_STATUS_OK) { - return BASE_STATUS_ERROR; - } - *coreClkSelect = crg.coreClkSelect; - return BASE_STATUS_OK; -} - -static void DMA_Channel0Init(void *handle) -{ - DMA_ChannelParam dma_param; - dma_param.direction = DMA_MEMORY_TO_PERIPH_BY_DMAC; - dma_param.srcAddrInc = DMA_ADDR_INCREASE; - dma_param.destAddrInc = DMA_ADDR_UNALTERED; - dma_param.srcPeriph = DMA_REQUEST_MEM; - dma_param.destPeriph = DMA_REQUEST_UART0_TX; - dma_param.srcWidth = DMA_TRANSWIDTH_BYTE; - dma_param.destWidth = DMA_TRANSWIDTH_BYTE; - dma_param.srcBurst = DMA_BURST_LENGTH_1; - dma_param.destBurst = DMA_BURST_LENGTH_1; - dma_param.pHandle = handle; - HAL_DMA_InitChannel(&g_dmac, &dma_param, DMA_CHANNEL_ZERO); -} - -static void DMA_Init(void) -{ - HAL_CRG_IpEnableSet(DMA_BASE, IP_CLK_ENABLE); - g_dmac.baseAddress = DMA; - IRQ_Register(IRQ_DMA_TC, HAL_DMA_IrqHandlerTc, &g_dmac); - IRQ_Register(IRQ_DMA_ERR, HAL_DMA_IrqHandlerError, &g_dmac); - IRQ_EnableN(IRQ_DMA_TC); - IRQ_EnableN(IRQ_DMA_ERR); - HAL_DMA_Init(&g_dmac); - DMA_Channel0Init((void *)(&g_uart0)); - HAL_DMA_SetChannelPriorityEx(&g_dmac, DMA_CHANNEL_ZERO, DMA_PRIORITY_HIGHEST); -} - -static void ACMP0_Init(void) -{ - HAL_CRG_IpEnableSet(ACMP0_BASE, IP_CLK_ENABLE); /* ACMP clock bit reset. */ - g_acmp0.baseAddress = ACMP0_BASE; - g_acmp0.inOutConfig.inputNNum = ACMP_INPUT_N_SELECT2; - g_acmp0.inOutConfig.inputPNum = ACMP_INPUT_P_SELECT2; - g_acmp0.inOutConfig.polarity = ACMP_OUT_NOT_INVERT; - g_acmp0.filterCtrl.filterMode = ACMP_FILTER_NONE; - g_acmp0.hysteresisVol = ACMP_HYS_VOL_30MV; - g_acmp0.interruptEn = BASE_CFG_UNSET; - HAL_ACMP_Init(&g_acmp0); -} - -static void ADC0_Init(void) -{ - HAL_CRG_IpEnableSet(ADC0_BASE, IP_CLK_ENABLE); - HAL_CRG_IpClkSelectSet(ADC0_BASE, CRG_ADC_CLK_ASYN_PLL_DIV); - HAL_CRG_IpClkDivSet(ADC0_BASE, CRG_ADC_DIV_1); - - g_adc0.baseAddress = ADC0; - g_adc0.socPriority = ADC_PRIMODE_ALL_ROUND; - - HAL_ADC_Init(&g_adc0); - - SOC_Param socParam = {0}; - socParam.adcInput = ADC_CH_ADCINA0; /* PGA0_OUT(ADC AIN0) */ - socParam.sampleTotalTime = ADC_SOCSAMPLE_22CLK; /* adc sample total time 22 adc_clk */ - socParam.trigSource = ADC_TRIGSOC_APT0_SOCA; - socParam.continueMode = BASE_CFG_DISABLE; - socParam.finishMode = ADC_SOCFINISH_NONE; - HAL_ADC_ConfigureSoc(&g_adc0, ADC_SOC_NUM1, &socParam); - - socParam.adcInput = ADC_CH_ADCINA1; /* PGA1_OUT(ADC AIN1) */ - socParam.sampleTotalTime = ADC_SOCSAMPLE_22CLK; /* adc sample total time 22 adc_clk */ - socParam.trigSource = ADC_TRIGSOC_APT0_SOCA; - socParam.continueMode = BASE_CFG_DISABLE; - socParam.finishMode = ADC_SOCFINISH_NONE; - HAL_ADC_ConfigureSoc(&g_adc0, ADC_SOC_NUM6, &socParam); - - socParam.adcInput = ADC_CH_ADCINA9; /* PIN7(ADC AIN9) */ - socParam.sampleTotalTime = ADC_SOCSAMPLE_22CLK; /* adc sample total time 22 adc_clk */ - socParam.trigSource = ADC_TRIGSOC_SOFT; - socParam.continueMode = BASE_CFG_DISABLE; - socParam.finishMode = ADC_SOCFINISH_NONE; - HAL_ADC_ConfigureSoc(&g_adc0, ADC_SOC_NUM0, &socParam); - - socParam.adcInput = ADC_CH_ADCINA5; /* PIN2(ADC AIN5) */ - socParam.sampleTotalTime = ADC_SOCSAMPLE_22CLK; /* adc sample total time 22 adc_clk */ - socParam.trigSource = ADC_TRIGSOC_SOFT; - socParam.continueMode = BASE_CFG_DISABLE; - socParam.finishMode = ADC_SOCFINISH_NONE; - HAL_ADC_ConfigureSoc(&g_adc0, ADC_SOC_NUM3, &socParam); - - socParam.adcInput = ADC_CH_ADCINA15; /* PIN14(ADC AIN15) */ - socParam.sampleTotalTime = ADC_SOCSAMPLE_22CLK; /* adc sample total time 22 adc_clk */ - socParam.trigSource = ADC_TRIGSOC_SOFT; - socParam.continueMode = BASE_CFG_DISABLE; - socParam.finishMode = ADC_SOCFINISH_NONE; - HAL_ADC_ConfigureSoc(&g_adc0, ADC_SOC_NUM4, &socParam); -} - -__weak void MotorSysErrCallback(void *aptHandle) -{ - BASE_FUNC_UNUSED(aptHandle); - /* USER CODE BEGIN APT0_EVENT_INTERRUPT */ - /* USER CODE END APT0_EVENT_INTERRUPT */ -} - -__weak void MotorCarrierProcessCallback(void *aptHandle) -{ - BASE_FUNC_UNUSED(aptHandle); - /* USER CODE BEGIN APT0_TIMER_INTERRUPT */ - /* USER CODE END APT0_TIMER_INTERRUPT */ -} - -static void APT0_ProtectInit(void) -{ - APT_OutCtrlProtectEx protectApt = {0}; - protectApt.ocEventEnEx = BASE_CFG_ENABLE; - protectApt.ocEventModeEx = APT_OUT_CTRL_ONE_SHOT; - protectApt.ocActionEx = APT_OUT_CTRL_ACTION_LOW; - protectApt.ocActionBEx = APT_OUT_CTRL_ACTION_LOW; - protectApt.ocEvtInterruptEnEx = BASE_CFG_ENABLE; - protectApt.ocSysEvent = APT_SYS_EVT_DEBUG | APT_SYS_EVT_CLK | APT_SYS_EVT_MEM; - protectApt.originalEvtEx = APT_EM_ORIGINAL_SRC_ACMP0; - protectApt.evtPolarityMaskEx = APT_EM_ACMP0_INVERT_BIT; - protectApt.filterCycleNumEx = 0; - HAL_APT_ProtectInitEx(&g_apt0, &protectApt); -} - -static void APT0_Init(void) -{ - HAL_CRG_IpEnableSet(APT0_BASE, IP_CLK_ENABLE); - - g_apt0.baseAddress = APT0; - - /* Clock Settings */ - g_apt0.waveform.dividerFactor = 1 - 1; - /* Timer Settings */ - g_apt0.waveform.timerPeriod = 7500; /* 7500 is count period of APT time-base timer */ - g_apt0.waveform.cntMode = APT_COUNT_MODE_UP_DOWN; - - /* Wave Form */ - g_apt0.waveform.basicType = APT_PWM_BASIC_A_HIGH_B_LOW; - g_apt0.waveform.chAOutType = APT_PWM_OUT_BASIC_TYPE; - g_apt0.waveform.chBOutType = APT_PWM_OUT_BASIC_TYPE; - g_apt0.waveform.divInitVal = 0; - g_apt0.waveform.cntInitVal = 0; - g_apt0.waveform.cntCmpLeftEdge = 500; /* 500 is count compare point of the left edge of PWM waveform */ - g_apt0.waveform.cntCmpRightEdge = 4000; /* 4000 is count compare point of the right edge of PWM waveform */ - g_apt0.waveform.cntCmpLoadMode = APT_BUFFER_INDEPENDENT_LOAD; - g_apt0.waveform.cntCmpLoadEvt = APT_COMPARE_LOAD_EVENT_ZERO; - g_apt0.waveform.deadBandCnt = 225; /* 225 is dead-band value */ - - /* ADC Trigger SOCA */ - g_apt0.adcTrg.trgEnSOCA = BASE_CFG_ENABLE; - g_apt0.adcTrg.cntCmpSOCA = 375; /* 375 is count compare point of ADC trigger source SOCA when using CMPA */ - g_apt0.adcTrg.trgSrcSOCA = APT_CS_SRC_CNTR_CMPA_DOWN; - g_apt0.adcTrg.trgScaleSOCA = 1; - - /* ADC Trigger SOCB */ - g_apt0.adcTrg.trgEnSOCB = BASE_CFG_ENABLE; - g_apt0.adcTrg.cntCmpSOCB = 1; - g_apt0.adcTrg.trgSrcSOCB = APT_CS_SRC_CNTR_CMPB_DOWN; - g_apt0.adcTrg.trgScaleSOCB = 1; - - g_apt0.adcTrg.cntCmpLoadMode = APT_BUFFER_INDEPENDENT_LOAD; - g_apt0.adcTrg.cntCmpLoadEvt = APT_COMPARE_LOAD_EVENT_ZERO; - - /* Timer Trigger */ - g_apt0.tmrInterrupt.tmrInterruptEn = BASE_CFG_ENABLE; - g_apt0.tmrInterrupt.tmrInterruptSrc = APT_INT_SRC_CNTR_ZERO; - g_apt0.tmrInterrupt.tmrInterruptScale = 1; - - APT0_ProtectInit(); - - HAL_APT_PWMInit(&g_apt0); - HAL_APT_RegisterCallBack(&g_apt0, APT_EVENT_INTERRUPT, MotorSysErrCallback); - IRQ_SetPriority(IRQ_APT0_EVT, 7); /* 7 is priority value */ - IRQ_Register(IRQ_APT0_EVT, HAL_APT_EventIrqHandler, &g_apt0); - IRQ_EnableN(IRQ_APT0_EVT); - HAL_APT_RegisterCallBack(&g_apt0, APT_TIMER_INTERRUPT, MotorCarrierProcessCallback); - IRQ_SetPriority(IRQ_APT0_TMR, 5); /* 5 is priority value */ - IRQ_Register(IRQ_APT0_TMR, HAL_APT_TimerIrqHandler, &g_apt0); - IRQ_EnableN(IRQ_APT0_TMR); -} - -static void APT1_ProtectInit(void) -{ - APT_OutCtrlProtectEx protectApt = {0}; - protectApt.ocEventEnEx = BASE_CFG_ENABLE; - protectApt.ocEventModeEx = APT_OUT_CTRL_ONE_SHOT; - protectApt.ocActionEx = APT_OUT_CTRL_ACTION_LOW; - protectApt.ocActionBEx = APT_OUT_CTRL_ACTION_LOW; - protectApt.ocEvtInterruptEnEx = BASE_CFG_DISABLE; - protectApt.ocSysEvent = APT_SYS_EVT_DEBUG | APT_SYS_EVT_CLK | APT_SYS_EVT_MEM; - protectApt.originalEvtEx = APT_EM_ORIGINAL_SRC_ACMP0; - protectApt.evtPolarityMaskEx = APT_EM_ACMP0_INVERT_BIT; - protectApt.filterCycleNumEx = 0; - HAL_APT_ProtectInitEx(&g_apt1, &protectApt); -} - -static void APT1_Init(void) -{ - HAL_CRG_IpEnableSet(APT1_BASE, IP_CLK_ENABLE); - - g_apt1.baseAddress = APT1; - - /* Clock Settings */ - g_apt1.waveform.dividerFactor = 1 - 1; - /* Timer Settings */ - g_apt1.waveform.timerPeriod = 7500; /* 7500 is count period of APT time-base timer */ - g_apt1.waveform.cntMode = APT_COUNT_MODE_UP_DOWN; - - /* Wave Form */ - g_apt1.waveform.basicType = APT_PWM_BASIC_A_HIGH_B_LOW; - g_apt1.waveform.chAOutType = APT_PWM_OUT_BASIC_TYPE; - g_apt1.waveform.chBOutType = APT_PWM_OUT_BASIC_TYPE; - g_apt1.waveform.divInitVal = 0; - g_apt1.waveform.cntInitVal = 0; - g_apt1.waveform.cntCmpLeftEdge = 500; /* 500 is count compare point of the left edge of PWM waveform */ - g_apt1.waveform.cntCmpRightEdge = 4000; /* 4000 is count compare point of the right edge of PWM waveform */ - g_apt1.waveform.cntCmpLoadMode = APT_BUFFER_INDEPENDENT_LOAD; - g_apt1.waveform.cntCmpLoadEvt = APT_COMPARE_LOAD_EVENT_ZERO; - g_apt1.waveform.deadBandCnt = 225; /* 225 is dead-band value */ - - APT1_ProtectInit(); - - HAL_APT_PWMInit(&g_apt1); -} - -static void APT2_ProtectInit(void) -{ - APT_OutCtrlProtectEx protectApt = {0}; - protectApt.ocEventEnEx = BASE_CFG_ENABLE; - protectApt.ocEventModeEx = APT_OUT_CTRL_ONE_SHOT; - protectApt.ocActionEx = APT_OUT_CTRL_ACTION_LOW; - protectApt.ocActionBEx = APT_OUT_CTRL_ACTION_LOW; - protectApt.ocEvtInterruptEnEx = BASE_CFG_DISABLE; - protectApt.ocSysEvent = APT_SYS_EVT_DEBUG | APT_SYS_EVT_CLK | APT_SYS_EVT_MEM; - protectApt.originalEvtEx = APT_EM_ORIGINAL_SRC_ACMP0; - protectApt.evtPolarityMaskEx = APT_EM_ACMP0_INVERT_BIT; - protectApt.filterCycleNumEx = 0; - HAL_APT_ProtectInitEx(&g_apt2, &protectApt); -} - -static void APT2_Init(void) -{ - HAL_CRG_IpEnableSet(APT2_BASE, IP_CLK_ENABLE); - - g_apt2.baseAddress = APT2; - - /* Clock Settings */ - g_apt2.waveform.dividerFactor = 1 - 1; - /* Timer Settings */ - g_apt2.waveform.timerPeriod = 7500; /* 7500 is count period of APT time-base timer */ - g_apt2.waveform.cntMode = APT_COUNT_MODE_UP_DOWN; - - /* Wave Form */ - g_apt2.waveform.basicType = APT_PWM_BASIC_A_HIGH_B_LOW; - g_apt2.waveform.chAOutType = APT_PWM_OUT_BASIC_TYPE; - g_apt2.waveform.chBOutType = APT_PWM_OUT_BASIC_TYPE; - g_apt2.waveform.divInitVal = 0; - g_apt2.waveform.cntInitVal = 0; - g_apt2.waveform.cntCmpLeftEdge = 500; /* 500 is count compare point of the left edge of PWM waveform */ - g_apt2.waveform.cntCmpRightEdge = 4000; /* 4000 is count compare point of the right edge of PWM waveform */ - g_apt2.waveform.cntCmpLoadMode = APT_BUFFER_INDEPENDENT_LOAD; - g_apt2.waveform.cntCmpLoadEvt = APT_COMPARE_LOAD_EVENT_ZERO; - g_apt2.waveform.deadBandCnt = 225; /* 225 is dead-band value */ - - APT2_ProtectInit(); - - HAL_APT_PWMInit(&g_apt2); -} - -__weak void MotorStartStopKeyCallback(void *param) -{ - GPIO_Handle *handle = (GPIO_Handle *)param; - BASE_FUNC_UNUSED(handle); -} - -static void GPIO_Init(void) -{ - HAL_CRG_IpEnableSet(GPIO2_BASE, IP_CLK_ENABLE); - g_gpio2.baseAddress = GPIO2; - - g_gpio2.pins = GPIO_PIN_3; - HAL_GPIO_Init(&g_gpio2); - HAL_GPIO_SetDirection(&g_gpio2, g_gpio2.pins, GPIO_OUTPUT_MODE); - HAL_GPIO_SetValue(&g_gpio2, g_gpio2.pins, GPIO_HIGH_LEVEL); - HAL_GPIO_SetIrqType(&g_gpio2, g_gpio2.pins, GPIO_INT_TYPE_NONE); - - g_gpio2.pins = GPIO_PIN_4; - HAL_GPIO_Init(&g_gpio2); - HAL_GPIO_SetDirection(&g_gpio2, g_gpio2.pins, GPIO_INPUT_MODE); - HAL_GPIO_SetValue(&g_gpio2, g_gpio2.pins, GPIO_HIGH_LEVEL); - HAL_GPIO_SetIrqType(&g_gpio2, g_gpio2.pins, GPIO_INT_TYPE_LOW_LEVEL); - - HAL_CRG_IpEnableSet(GPIO1_BASE, IP_CLK_ENABLE); - g_gpio1.baseAddress = GPIO1; - - g_gpio1.pins = GPIO_PIN_0; - HAL_GPIO_Init(&g_gpio1); - HAL_GPIO_SetDirection(&g_gpio1, g_gpio1.pins, GPIO_OUTPUT_MODE); - HAL_GPIO_SetValue(&g_gpio1, g_gpio1.pins, GPIO_HIGH_LEVEL); - HAL_GPIO_SetIrqType(&g_gpio1, g_gpio1.pins, GPIO_INT_TYPE_NONE); - - HAL_GPIO_RegisterCallBack(&g_gpio2, GPIO_PIN_4, MotorStartStopKeyCallback); - IRQ_Register(IRQ_GPIO2, HAL_GPIO_IrqHandler, &g_gpio2); - IRQ_SetPriority(IRQ_GPIO2, 1); /* set gpio1 interrupt priority to 1, 1~15. 1 is priority value */ - IRQ_EnableN(IRQ_GPIO2); /* gpio interrupt enable */ - - return; -} - -static void PGA0_Init(void) -{ - HAL_CRG_IpEnableSet(PGA0_BASE, IP_CLK_ENABLE); - - g_pga0.baseAddress = PGA0_BASE; - g_pga0.externalResistorMode = BASE_CFG_ENABLE; - g_pga0.handleEx.extCapCompensation = PGA_EXT_COMPENSATION_2X; - HAL_PGA_Init(&g_pga0); -} - -static void PGA1_Init(void) -{ - HAL_CRG_IpEnableSet(PGA1_BASE, IP_CLK_ENABLE); - - g_pga1.baseAddress = PGA1_BASE; - g_pga1.externalResistorMode = BASE_CFG_ENABLE; - g_pga1.handleEx.extCapCompensation = PGA_EXT_COMPENSATION_2X; - HAL_PGA_Init(&g_pga1); -} - - -static void I2C0_Init(void) -{ - HAL_CRG_IpEnableSet(I2C0_BASE, IP_CLK_ENABLE); /* I2C0 clock enable. */ - g_i2c0.baseAddress = I2C0; - - g_i2c0.functionMode = I2C_MODE_SELECT_MASTER_ONLY; - g_i2c0.addrMode = I2C_7_BITS; - g_i2c0.sdaHoldTime = 10; /* 10 is sda Hold Time */ - g_i2c0.freq = 100000; /* freqence is 400000 */ - g_i2c0.transferBuff = NULL; - g_i2c0.ignoreAckFlag = BASE_CFG_DISABLE; - g_i2c0.handleEx.spikeFilterTime = 0; - g_i2c0.handleEx.sdaDelayTime = 0; - g_i2c0.timeout = 10000; /* 10000 is time out */ - g_i2c0.state = I2C_STATE_RESET; - HAL_I2C_Init(&g_i2c0); -} - -static void QDM1_Init(void) -{ - HAL_CRG_IpEnableSet(QDM1_BASE, IP_CLK_ENABLE); - g_qdm1.baseAddress = QDM1_BASE; - - /* emulation config */ - g_qdm1.emuMode = QDM_EMULATION_MODE_RUN_FREE; - /* input config */ - g_qdm1.ctrlConfig.decoderMode = QDM_QUADRATURE_COUNT; - - g_qdm1.motorLineNum = 16384; - - g_qdm1.inputFilter.qdmAFilterLevel = 100; - g_qdm1.inputFilter.qdmBFilterLevel = 100; - g_qdm1.inputFilter.qdmZFilterLevel = 100; - g_qdm1.ctrlConfig.polarity = 0; - g_qdm1.ctrlConfig.swap = QDM_SWAP_DISABLE; - g_qdm1.ctrlConfig.resolution = QDM_4X_RESOLUTION; - - g_qdm1.pcntMode = QDM_PCNT_MODE_BY_DIR; - g_qdm1.pcntRstMode = QDM_PCNT_RST_OVF; - g_qdm1.posInit = 0; - g_qdm1.pcntIdxInitMode = QDM_IDX_INIT_DISABLE; - g_qdm1.lock_mode = QDM_LOCK_RISING_INDEX; - g_qdm1.posMax = 4294967295; - - g_qdm1.tsuPrescaler = 0; - g_qdm1.cevtPrescaler = QDM_CEVT_PRESCALER_DIVI1; - g_qdm1.qcMax = 65535; - - g_qdm1.ctrlConfig.ptuMode = QDM_PTU_MODE_CYCLE; - g_qdm1.period = 1; - g_qdm1.ctrlConfig.trgLockMode = QDM_TRG_BY_READ; - g_qdm1.subModeEn = true; - - HAL_QDM_Init(&g_qdm1); -} - -__weak void CheckPotentiometerValueCallback(void *handle) -{ - BASE_FUNC_UNUSED(handle); - /* USER CODE BEGIN CheckPotentiometerValueCallback */ - /* USER CODE END CheckPotentiometerValueCallback */ -} - -static void TIMER0_Init(void) -{ - HAL_CRG_IpEnableSet(TIMER0_BASE, IP_CLK_ENABLE); /* TIMER0 clock enable. */ - unsigned int load = (float)(HAL_CRG_GetIpFreq((void *)TIMER0) / (1u << (TIMERPRESCALER_NO_DIV * 4)) / 1000000u) * 1000000; - - g_timer0.baseAddress = TIMER0; - g_timer0.load = (load < 1) ? 0 : (load - 1); /* Set timer value immediately */ - g_timer0.bgLoad = (load < 1) ? 0 : (load - 1); /* Set timer value */ - g_timer0.mode = TIMER_MODE_RUN_PERIODIC; /* Run in period mode */ - g_timer0.prescaler = TIMERPRESCALER_NO_DIV; /* Don't frequency division */ - g_timer0.size = TIMER_SIZE_32BIT; /* 1 for 32bit, 0 for 16bit */ - g_timer0.interruptEn = BASE_CFG_ENABLE; - g_timer0.adcSocReqEnable = BASE_CFG_DISABLE; - g_timer0.dmaReqEnable = BASE_CFG_DISABLE; - HAL_TIMER_Init(&g_timer0); - IRQ_Register(IRQ_TIMER0, HAL_TIMER_IrqHandler, &g_timer0); - - HAL_TIMER_RegisterCallback(&g_timer0, TIMER_PERIOD_FIN, CheckPotentiometerValueCallback); - IRQ_SetPriority(IRQ_TIMER0, 1); /* 1 is priority value */ - IRQ_EnableN(IRQ_TIMER0); -} - -__weak void MotorStatemachineCallBack(void *handle) -{ - BASE_FUNC_UNUSED(handle); - /* USER CODE BEGIN MotorStatemachineCallBack */ - /* USER CODE END MotorStatemachineCallBack */ -} - -static void TIMER1_Init(void) -{ - HAL_CRG_IpEnableSet(TIMER1_BASE, IP_CLK_ENABLE); /* TIMER1 clock enable. */ - unsigned int load = (float)(HAL_CRG_GetIpFreq((void *)TIMER1) / (1u << (TIMERPRESCALER_NO_DIV * 4)) / 1000000u) * 500; - - g_timer1.baseAddress = TIMER1; - g_timer1.load = (load < 1) ? 0 : (load - 1); /* Set timer value immediately */ - g_timer1.bgLoad = (load < 1) ? 0 : (load - 1); /* Set timer value */ - g_timer1.mode = TIMER_MODE_RUN_PERIODIC; /* Run in period mode */ - g_timer1.prescaler = TIMERPRESCALER_NO_DIV; /* Don't frequency division */ - g_timer1.size = TIMER_SIZE_32BIT; /* 1 for 32bit, 0 for 16bit */ - g_timer1.interruptEn = BASE_CFG_ENABLE; - g_timer1.adcSocReqEnable = BASE_CFG_DISABLE; - g_timer1.dmaReqEnable = BASE_CFG_DISABLE; - HAL_TIMER_Init(&g_timer1); - IRQ_Register(IRQ_TIMER1, HAL_TIMER_IrqHandler, &g_timer1); - - HAL_TIMER_RegisterCallback(&g_timer1, TIMER_PERIOD_FIN, MotorStatemachineCallBack); - IRQ_SetPriority(IRQ_TIMER1, 1); /* 1 is priority value */ - IRQ_EnableN(IRQ_TIMER1); -} - -__weak void TIMER2_InterruptProcess(void *handle) -{ - BASE_FUNC_UNUSED(handle); - /* USER CODE BEGIN TIMER2_InterruptProcess */ - /* USER CODE END TIMER2_InterruptProcess */ -} - - -__weak void UART0InterruptErrorCallback(void *handle) -{ - BASE_FUNC_UNUSED(handle); - /* USER CODE BEGIN UART0_TRNS_IT_ERROR */ - /* USER CODE END UART0_TRNS_IT_ERROR */ -} - -__weak void UART0_TXDMACallback(void *handle) -{ - BASE_FUNC_UNUSED(handle); - /* USER CODE BEGIN UART0_WRITE_DMA_FINISH */ - /* USER CODE END UART0_WRITE_DMA_FINISH */ -} - -__weak void UART0WriteInterruptCallback(void *handle) -{ - BASE_FUNC_UNUSED(handle); - /* USER CODE BEGIN UART0_WRITE_IT_FINISH */ - /* USER CODE END UART0_WRITE_IT_FINISH */ -} - -__weak void UART0ReadInterruptCallback(void *handle) -{ - BASE_FUNC_UNUSED(handle); - /* USER CODE BEGIN UART0_READ_IT_FINISH */ - /* USER CODE END UART0_READ_IT_FINISH */ -} - -static void UART0_Init(void) -{ - HAL_CRG_IpEnableSet(UART0_BASE, IP_CLK_ENABLE); /* UART0 clock enable. */ - g_uart0.baseAddress = UART0; - - g_uart0.baudRate = UART0_BAND_RATE; - g_uart0.dataLength = UART_DATALENGTH_8BIT; - g_uart0.stopBits = UART_STOPBITS_ONE; - g_uart0.parity = UART_PARITY_NONE; - g_uart0.txMode = UART_MODE_DMA; - g_uart0.rxMode = UART_MODE_INTERRUPT; - g_uart0.fifoMode = BASE_CFG_ENABLE; - g_uart0.fifoTxThr = UART_FIFODEPTH_SIZE8; - g_uart0.fifoRxThr = UART_FIFODEPTH_SIZE8; - g_uart0.hwFlowCtr = BASE_CFG_DISABLE; - g_uart0.handleEx.overSampleMultiple = UART_OVERSAMPLING_16X; - g_uart0.handleEx.msbFirst = BASE_CFG_DISABLE; - HAL_UART_Init(&g_uart0); - HAL_UART_RegisterCallBack(&g_uart0, UART_TRNS_IT_ERROR, (UART_CallbackType)UART0InterruptErrorCallback); - HAL_UART_RegisterCallBack(&g_uart0, UART_READ_IT_FINISH, (UART_CallbackType)UART0ReadInterruptCallback); - IRQ_Register(IRQ_UART0, HAL_UART_IrqHandler, &g_uart0); - IRQ_SetPriority(IRQ_UART0, 6); /* 6 is priority value */ - IRQ_EnableN(IRQ_UART0); - g_uart0.dmaHandle = &g_dmac; - g_uart0.uartDmaTxChn = 0; - HAL_UART_RegisterCallBack(&g_uart0, UART_WRITE_DMA_FINISH, (UART_CallbackType)UART0_TXDMACallback); - HAL_UART_RegisterCallBack(&g_uart0, UART_WRITE_IT_FINISH, (UART_CallbackType)UART0WriteInterruptCallback); -} - -static void IOConfig(void) -{ - SYSCTRL0->SC_SYS_STAT.BIT.update_mode = 0; - SYSCTRL0->SC_SYS_STAT.BIT.update_mode_clear = 1; - /* Config PIN39 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO0_3_AS_UART0_TXD); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO0_3_AS_UART0_TXD, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO0_3_AS_UART0_TXD, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO0_3_AS_UART0_TXD, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO0_3_AS_UART0_TXD, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN40 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO0_4_AS_UART0_RXD); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO0_4_AS_UART0_RXD, PULL_UP); /* Pull-up and Pull-down, UART RX recommend PULL_UP */ - HAL_IOCMG_SetPinSchmidtMode(GPIO0_4_AS_UART0_RXD, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO0_4_AS_UART0_RXD, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO0_4_AS_UART0_RXD, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN1 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO0_7_AS_ACMP0_OUT); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO0_7_AS_ACMP0_OUT, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO0_7_AS_ACMP0_OUT, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO0_7_AS_ACMP0_OUT, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO0_7_AS_ACMP0_OUT, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN48 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO0_6_AS_ACMP_N2); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO0_6_AS_ACMP_N2, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO0_6_AS_ACMP_N2, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO0_6_AS_ACMP_N2, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO0_6_AS_ACMP_N2, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN47 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO0_5_AS_ACMP_P2); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO0_5_AS_ACMP_P2, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO0_5_AS_ACMP_P2, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO0_5_AS_ACMP_P2, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO0_5_AS_ACMP_P2, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN4 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO2_6_AS_PGA0_N0); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO2_6_AS_PGA0_N0, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO2_6_AS_PGA0_N0, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO2_6_AS_PGA0_N0, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO2_6_AS_PGA0_N0, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN5 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO2_5_AS_PGA0_P0); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO2_5_AS_PGA0_P0, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO2_5_AS_PGA0_P0, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO2_5_AS_PGA0_P0, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO2_5_AS_PGA0_P0, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN3 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO2_7_AS_PGA0_OUT); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO2_7_AS_PGA0_OUT, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO2_7_AS_PGA0_OUT, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO2_7_AS_PGA0_OUT, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO2_7_AS_PGA0_OUT, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN7 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO3_7_AS_ADC_AIN9); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO3_7_AS_ADC_AIN9, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO3_7_AS_ADC_AIN9, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO3_7_AS_ADC_AIN9, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO3_7_AS_ADC_AIN9, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN2 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO5_1_AS_ADC_AIN5); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO5_1_AS_ADC_AIN5, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO5_1_AS_ADC_AIN5, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO5_1_AS_ADC_AIN5, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO5_1_AS_ADC_AIN5, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN14 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO4_7_AS_ADC_AIN15); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO4_7_AS_ADC_AIN15, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO4_7_AS_ADC_AIN15, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO4_7_AS_ADC_AIN15, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO4_7_AS_ADC_AIN15, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN11 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO1_5_AS_PGA1_P0); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO1_5_AS_PGA1_P0, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO1_5_AS_PGA1_P0, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO1_5_AS_PGA1_P0, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO1_5_AS_PGA1_P0, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN12 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO1_6_AS_PGA1_N0); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO1_6_AS_PGA1_N0, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO1_6_AS_PGA1_N0, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO1_6_AS_PGA1_N0, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO1_6_AS_PGA1_N0, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN13 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO1_7_AS_PGA1_OUT); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO1_7_AS_PGA1_OUT, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO1_7_AS_PGA1_OUT, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO1_7_AS_PGA1_OUT, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO1_7_AS_PGA1_OUT, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN35 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO2_3_AS_GPIO2_3); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO2_3_AS_GPIO2_3, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO2_3_AS_GPIO2_3, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO2_3_AS_GPIO2_3, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO2_3_AS_GPIO2_3, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN27 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO1_0_AS_GPIO1_0); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO1_0_AS_GPIO1_0, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO1_0_AS_GPIO1_0, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO1_0_AS_GPIO1_0, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO1_0_AS_GPIO1_0, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN41 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO2_4_AS_GPIO2_4); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO2_4_AS_GPIO2_4, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO2_4_AS_GPIO2_4, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO2_4_AS_GPIO2_4, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO2_4_AS_GPIO2_4, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN31 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO2_0_AS_QDM1_A); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO2_0_AS_QDM1_A, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO2_0_AS_QDM1_A, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO2_0_AS_QDM1_A, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO2_0_AS_QDM1_A, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN32 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO2_1_AS_QDM1_B); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO2_1_AS_QDM1_B, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO2_1_AS_QDM1_B, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO2_1_AS_QDM1_B, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO2_1_AS_QDM1_B, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN33 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO5_0_AS_QDM1_INDEX); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO5_0_AS_QDM1_INDEX, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO5_0_AS_QDM1_INDEX, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO5_0_AS_QDM1_INDEX, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO5_0_AS_QDM1_INDEX, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN19 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO3_0_AS_APT0_PWMA); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO3_0_AS_APT0_PWMA, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO3_0_AS_APT0_PWMA, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO3_0_AS_APT0_PWMA, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO3_0_AS_APT0_PWMA, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN23 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO4_0_AS_APT0_PWMB); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO4_0_AS_APT0_PWMB, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO4_0_AS_APT0_PWMB, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO4_0_AS_APT0_PWMB, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO4_0_AS_APT0_PWMB, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN20 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO3_1_AS_APT1_PWMA); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO3_1_AS_APT1_PWMA, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO3_1_AS_APT1_PWMA, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO3_1_AS_APT1_PWMA, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO3_1_AS_APT1_PWMA, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN24 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO4_1_AS_APT1_PWMB); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO4_1_AS_APT1_PWMB, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO4_1_AS_APT1_PWMB, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO4_1_AS_APT1_PWMB, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO4_1_AS_APT1_PWMB, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN21 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO3_2_AS_APT2_PWMA); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO3_2_AS_APT2_PWMA, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO3_2_AS_APT2_PWMA, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO3_2_AS_APT2_PWMA, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO3_2_AS_APT2_PWMA, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN25 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO4_2_AS_APT2_PWMB); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO4_2_AS_APT2_PWMB, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO4_2_AS_APT2_PWMB, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO4_2_AS_APT2_PWMB, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO4_2_AS_APT2_PWMB, DRIVER_RATE_2); /* Output signal edge fast/slow */ - - - /* Config PIN15 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO4_5_AS_I2C0_SCL); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO4_5_AS_I2C0_SCL, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO4_5_AS_I2C0_SCL, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO4_5_AS_I2C0_SCL, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO4_5_AS_I2C0_SCL, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN16 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO4_3_AS_I2C0_SDA); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO4_3_AS_I2C0_SDA, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO4_3_AS_I2C0_SDA, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO4_3_AS_I2C0_SDA, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO4_3_AS_I2C0_SDA, DRIVER_RATE_2); /* Output signal edge fast/slow */ - -} - -static void APT_SyncMasterInit(void) -{ - HAL_APT_MasterSyncInit(&g_apt0, APT_SYNC_OUT_ON_CNTR_ZERO); -} - -static void APT_SyncSlaveInit(void) -{ - APT_SlaveSyncIn aptSlave; - - aptSlave.cntPhase = 0; /* counter phase value */ - aptSlave.syncCntMode = APT_COUNT_MODE_AFTER_SYNC_UP; - aptSlave.syncInSrc = APT_SYNCIN_SRC_APT0_SYNCOUT; /* sync source selection */ - aptSlave.cntrSyncSrc = APT_CNTR_SYNC_SRC_SYNCIN; - HAL_APT_SlaveSyncInit(&g_apt1, &aptSlave); - - aptSlave.cntPhase = 0; /* counter phase value */ - aptSlave.syncCntMode = APT_COUNT_MODE_AFTER_SYNC_UP; - aptSlave.syncInSrc = APT_SYNCIN_SRC_APT0_SYNCOUT; /* sync source selection */ - aptSlave.cntrSyncSrc = APT_CNTR_SYNC_SRC_SYNCIN; - HAL_APT_SlaveSyncInit(&g_apt2, &aptSlave); - -} - -void SystemInit(void) -{ - IOConfig(); - DMA_Init(); - UART0_Init(); - ACMP0_Init(); - APT0_Init(); - APT1_Init(); - APT2_Init(); - ADC0_Init(); - PGA0_Init(); - PGA1_Init(); - TIMER0_Init(); - TIMER1_Init(); - QDM1_Init(); - GPIO_Init(); - I2C0_Init(); - APT_SyncMasterInit(); - APT_SyncSlaveInit(); - - /* USER CODE BEGIN system_init */ - /* USER CODE END system_init */ -} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/main.c b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/main.c deleted file mode 100644 index b77983aa..00000000 --- a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/main.c +++ /dev/null @@ -1,81 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file main.c - * @author MCU Driver Team - * @brief Main program body. - */ - -#include "typedefs.h" -#include "feature.h" -#include "mcs_motor_process.h" -#include "main.h" -/* USER CODE BEGIN 0 */ -/* USER CODE 区域内代码不会被覆盖,区域外会被生成的默认代码覆盖(其余USER CODE 区域同理) */ -/* 建议用户放置头文件 */ - -#include "debug.h" -/* USER CODE END 0 */ -QDM_Handle g_qdm1; -ACMP_Handle g_acmp0; -PGA_Handle g_pga0; -PGA_Handle g_pga1; -TIMER_Handle g_timer0; -TIMER_Handle g_timer1; -TIMER_Handle g_timer2; -UART_Handle g_uart0; -APT_Handle g_apt0; -APT_Handle g_apt1; -APT_Handle g_apt2; -ADC_Handle g_adc0; -DMA_Handle g_dmac; -GPIO_Handle g_gpio2; -GPIO_Handle g_gpio1; -I2C_Handle g_i2c0; -/* USER CODE BEGIN 1 */ -/* 建议用户定义全局变量、结构体、宏定义或函数声明等 */ - - -/* USER CODE END 1 */ - -int main(void) -{ - /* USER CODE BEGIN 2 */ - /* 建议用户放置初始化代码或启动代码等 */ - /* USER CODE END 2 */ - MotorMainProcess(); - - // SystemInit(); /* 系统初始化 */ - /* USER CODE BEGIN 3 */ - /* 建议用户放置初始配置代码 */ - /* USER CODE END 3 */ - while (1) { - /* USER CODE BEGIN 4 */ - /* 建议用户放置周期性执行代码 */ - - BASE_FUNC_DELAY_MS(100); - /* USER CODE END 4 */ - } - /* USER CODE BEGIN 5 */ - /* 建议用户放置代码流程 */ - /* USER CODE END 5 */ - return BASE_STATUS_OK; -} - -/* USER CODE BEGIN 6 */ -/* 建议用户放置自定义函数 */ -/* USER CODE END 6 */ \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/inc/mcs_carrier.h b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/inc/mcs_carrier.h deleted file mode 100644 index e09442d2..00000000 --- a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/inc/mcs_carrier.h +++ /dev/null @@ -1,153 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_carrier.h - * @author MCU Algorithm Team - * @brief This file provides functions declaration for carrier interrupt processing function. - */ -#ifndef McuMagicTag_MCS_CARRIER_H -#define McuMagicTag_MCS_CARRIER_H - -#include "mcs_status.h" -#include "mcs_mtr_param.h" -#include "mcs_svpwm.h" -#include "mcs_curr_ctrl.h" -#include "mcs_if_ctrl.h" -#include "mcs_ramp_mgmt.h" -#include "mcs_spd_ctrl.h" -#include "mcs_pos_ctrl.h" -#include "mcs_fosmo.h" -#include "mcs_smo_4th.h" -#include "mcs_pll.h" -#include "mcs_startup.h" -#include "mcs_r1_svpwm.h" -#include "mcs_fw_ctrl.h" -#include "mcs_prot_user.h" -#include "mcs_inc_enc.h" - -typedef void (*MCS_ReadCurrUvwCb)(UvwAxis *CurrUvw); -typedef void (*MCS_SetPwmDutyCb)(UvwAxis *dutyUvwLeft, UvwAxis *dutyUvwRight); -typedef void (*MCS_SetADCTriggerTimeCb)(unsigned short cntCmpSOCA, unsigned short cntCmpSOCB); -typedef void (*MCS_GetEncAngSpd)(float *speed, float *angle); - -/** - * @brief motor control FSM state define. - * @details motor control FSM state type: - * + FSM_IDLE -- IDLE state, system startup default. - * + FSM_OFFSET_CALIB -- Self calibrate, for ADC init. - * + FSM_CAP_CHARGE -- IPM cap charge. - * + FSM_CLEAR -- Clear before every run. - * + FSM_LOCATE -- Rotor position pre-locate. - * + FSM_STARTUP -- Start up. - * + FSM_SWITCH -- Transition state, control from open to closed loop. - * + FSM_RUN -- Normal running state. - * + FSM_WAIT_STOP -- Wait stopping. - * + FSM_STOP -- Normal stop. - * + FSM_FAULT -- Fault state, waiting for user process. - */ -typedef enum { - FSM_IDLE = 0, - FSM_OFFSET_CALIB, - FSM_CAP_CHARGE, - FSM_CLEAR, - FSM_LOCATE, - FSM_STARTUP, - FSM_SWITCH, - FSM_RUN, - FSM_WAIT_STOP, - FSM_STOP, - FSM_FAULT -} FsmState; - -/** - * @brief Sampling mode. - */ -typedef enum { - DUAL_RESISTORS = 0, - SINGLE_RESISTOR = 1 -} SampleMode; - -/** - * @brief Motor control data structure - */ -typedef struct { - unsigned char motorStateFlag; - float spdCmdHz; /**< External input speed command value */ - float axisAngle; /**< Angle of the synchronous coordinate system */ - short motorSpinPos; /**< IF启动模式下电机旋转位置*/ - float spdRefHz; /**< Command value after speed ramp management */ - float currCtrlPeriod; /**< current loop control period */ - float adc0Compensate; /**< ADC0 softwaretrim compensate value */ - float adc1Compensate; /**< ADC1 softwaretrim compensate value */ - float udc; /**< Bus voltage */ - float powerBoardTemp; /**< Power boart surface temperature */ - unsigned short aptMaxcntCmp; /**< Apt Maximum Comparison Count */ - float adcCurrCofe; /**< Adc current sampling cofeature */ - - unsigned short sysTickCnt; /**< System Timer Tick Count */ - unsigned short capChargeTickNum; /**< Bootstrap Capacitor Charge Tick Count */ - volatile unsigned int msTickCnt; /**< Millisecond-level counter, which can be used in 1-ms and 5-ms tasks. */ - unsigned short msTickNum; /**< Number of ticks corresponding to 1 ms */ - char obserType; /**< Set Observer Type */ - char controlMode; /**< Set foc control or sixstep bldc control mode or others */ - char spdAdjustMode; /**< Set speed adjust mode */ - char uartConnectFlag; /**< Uart connect success flag */ - short uartHeartDetCnt; /**< Uart connect heart detect count */ - float uartTimeStamp; /**< Uart data time stamp */ - SysStatusReg statusReg; /**< System status */ - FsmState stateMachine; /**< Motor Control State Machine */ - - SampleMode sampleMode; /**< sample mode */ - MOTOR_Param mtrParam; /**< Motor parameters */ - FOSMO_Handle smo; /**< SMO observer handle */ - SMO4TH_Handle smo4th; /**< SMO 4th observer handle */ - EncoderHandle *encHandle; /**< Encoder parameter handle */ - IF_Handle ifCtrl; /**< I/F control handle */ - SVPWM_Handle sv; /**< SVPWM Handle */ - R1SVPWM_Handle r1Sv; /**< Single-resistance phase-shifted SVPWM handld */ - RMG_Handle spdRmg; /**< Ramp management struct for the speed controller input reference */ - SPDCTRL_Handle spdCtrl; /**< Speed loop Control Handle */ - POSCTRL_Handle posCtrl; /**< Position controller handle. */ - CURRCTRL_Handle currCtrl; /**< Current loop control handle */ - STARTUP_Handle startup; /**< Startup Switch Handle */ - FW_Handle fw; /**< Flux-Weakening Handle */ - - DqAxis idqRef; /**< Command value of the dq axis current */ - UvwAxis currUvw; /**< Three-phase current sampling value */ - AlbeAxis iabFbk; /**< αβ-axis current feedback value */ - DqAxis idqFbk; /**< Current feedback value of the dq axis */ - DqAxis vdqRef; /**< Current loop output dq voltage */ - AlbeAxis vabRef; /**< Current loop output voltage αβ */ - UvwAxis dutyUvw; /**< UVW three-phase duty cycle */ - UvwAxis dutyUvwLeft; /**< Single Resistor UVW Three-Phase Left Duty Cycle */ - UvwAxis dutyUvwRight; /**< Single Resistor UVW Three-Phase Right Duty Cycle*/ - - MCS_ReadCurrUvwCb readCurrUvwCb; /**< Read current callback function */ - MCS_SetPwmDutyCb setPwmDutyCb; /**< Set the duty cycle callback function. */ - MCS_SetADCTriggerTimeCb setADCTriggerTimeCb; /**< Sets the ADC trigger point callback function. */ - - MotorProtStatus_Handle prot; /**< Protection handle. */ - - MCS_GetEncAngSpd getEncAngSpd; /**< Get the angle and speed of the encoder. */ - short encReady; - float encSpeed; - float encAxisAngle; -} MTRCTRL_Handle; - -void MCS_CarrierProcess(MTRCTRL_Handle *mtrCtrl); - -#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/inc/mcs_chip_config.h b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/inc/mcs_chip_config.h deleted file mode 100644 index 2fc50e46..00000000 --- a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/inc/mcs_chip_config.h +++ /dev/null @@ -1,81 +0,0 @@ -/** - * @ Copyright (c) HiSilicon (Shanghai) Technologies Co., Ltd. 2022-2023. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_chip_config.h - * @author MCU Algorithm Team - * @brief This file provides config macros for ECMCU105H/ECBMCU201MPC app. - */ - -#ifndef McuMagicTag_MCS_CHIPCONFIG_H -#define McuMagicTag_MCS_CHIPCONFIG_H - -#include "feature.h" - -#ifdef CHIP_3061MNPICA - - #define ADCPTT_HANDLE g_adc0 - #define ADCRESIS_HANDLE g_adc0 - #define ADCUDC_HANDLE g_adc0 - #define ADCU_HANDLE g_adc0 - #define ADCW_HANDLE g_adc0 - #define LED1_HANDLE g_gpio2 - #define LED2_HANDLE g_gpio1 - #define LED2_PIN GPIO_PIN_0 - #define LED1_PIN GPIO_PIN_3 - #define ADCPTTSOCNUM ADC_SOC_NUM0 - #define ADCUSOCNUM ADC_SOC_NUM1 - #define ADCRESISSOCNUM ADC_SOC_NUM3 - #define ADCUDCSOCNUM ADC_SOC_NUM4 - #define ADCWSOCNUM ADC_SOC_NUM6 - - #define ADC0COMPENSATE 2037.0f - #define ADC1COMPENSATE 2027.0f - - #define QDMNUM QDM1 - #define QDMIRQNUM IRQ_QDM1 - #define QDMBASEADDR QDM1_BASE - -#endif - -#if defined (CHIP_3065HRPIRZ) || defined (CHIP_3065ARPIRZ) - - #define ADCU_HANDLE g_adc0 - #define ADCW_HANDLE g_adc1 - #define ADCRESIS_HANDLE g_adc2 - #define ADCUDC_HANDLE g_adc2 - #define ADCPTT_HANDLE g_adc2 - #define LED1_HANDLE g_gpio0 - #define LED2_HANDLE g_gpio0 - #define LED2_PIN GPIO_PIN_6 - #define LED1_PIN GPIO_PIN_7 - #define ADCPTTSOCNUM ADC_SOC_NUM0 - #define ADCRESISSOCNUM ADC_SOC_NUM1 - #define ADCUSOCNUM ADC_SOC_NUM8 - #define ADCWSOCNUM ADC_SOC_NUM8 - #define ADCUDCSOCNUM ADC_SOC_NUM14 - - #define ADC0COMPENSATE 2033.0f - #define ADC1COMPENSATE 2070.0f - - #define QDMNUM QDM0 - #define QDMIRQNUM IRQ_QDM0 - #define QDMBASEADDR QDM0_BASE - -#endif - - -#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/inc/mcs_ctlmode_config.h b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/inc/mcs_ctlmode_config.h deleted file mode 100644 index 0c158026..00000000 --- a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/inc/mcs_ctlmode_config.h +++ /dev/null @@ -1,75 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_ctlmode_config.h - * @author MCU Algorithm Team - * @brief This file provides config macros for ECMCU105H app. - */ - - /* Define to prevent recursive inclusion ------------------------------------------------------- */ -#ifndef McuMagicTag_MCS_CTLMODECONFIG_H -#define McuMagicTag_MCS_CTLMODECONFIG_H - -#include "debug.h" -#include "typedefs.h" - -typedef enum { - FOC_STARTUP_IF = 0, - FOC_STARTUP_VF, - FOC_STARTUP_HFI -} MOTOR_STARTUPMODE_CONFIG; - -typedef enum { - FOC_OBSERVERTYPE_SMO1TH = 0, - FOC_OBSERVERTYPE_SMO1TH_PLL, - FOC_OBSERVERTYPE_ENC, - FOC_OBSERVERTYPE_SMO4TH_PLL, - FOC_OBSERVERTYPE_LUNBORG, - FOC_OBSERVERTYPE_FLUX, - FOC_OBSERVERTYPE_SMO4TH -} MOTOR_OBSERVERTYPE_CONFIG; - -typedef enum { - FOC_CONTROLMODE_SPEED = 0, - FOC_CONTROLMODE_TORQUE, - FOC_CONTROLMODE_POS -} MOTOR_CONTROLMODE_CONFIG; - -typedef enum { - FOC_CURQAXISPID_PARAMS = 0, - FOC_CURDAXISPID_PARAMS, - FOC_SPDPID_PARAMS -} MOTOR_PID_SET; - -typedef enum { - MOTOR_PARAMS_BASE = 0, - MOTOR_PARAMS_SPECIAL, - MOTOR_PARAMS_BOARD -} MOTOR_PARAMS_SET; - -typedef enum { - CUST_SPEED_ADJUST = 0, - HOST_SPEED_ADJUST -} MODE_ADSPEED_CONFIG; - -typedef enum { - CONNECTING = 0, - CONNECTED, - DISCONNECT -} UART_STATUS; - -#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/inc/mcs_ex_common.h b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/inc/mcs_ex_common.h deleted file mode 100644 index fe7117d9..00000000 --- a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/inc/mcs_ex_common.h +++ /dev/null @@ -1,78 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_ex_common.h - * @author MCU Algorithm Team - * @brief - */ - -/* Define to prevent recursive inclusion ------------------------------------------------------- */ -#ifndef MCU_MAGICTAG_MCS_EX_COMMON_H -#define MCU_MAGICTAG_MCS_EX_COMMON_H - -/* Includes ------------------------------------------------------------------------------------ */ -#include "interrupt_ip.h" -#include "typedefs.h" - -#define CRG_ENABLE_VALUE 3 - -#define CPU_FRQ_HZ 200000000u /* CPU frequency */ -#define CPU_MHZ 1000000u -#define TIMER_FRQ_DIV 2 - -/** - * @defgroup EXTEND EXTEND - * @brief Extend common module. - * @{ - */ - -/** - * @defgroup EXTEND_Common EXTEND Common - * @brief EXTEND common external module. - * @{ - */ - -/* Typedef definitions ------------------------------------------------------------------------- */ - -/** - * @defgroup EXTEND_Param_Def EXTEND Parameters Definition - * @brief Definition of EXTEND parameters. - * @{ - */ - -typedef struct { - IRQ_ID irqNum; /* Interrupt Number */ - void *baseAddr; /* Peripherals baseAddr */ -} NvicHandle; - - -/** - * @} - */ - -/** - * @} - */ - -/** - * @} - */ - -/** - * @} - */ -#endif diff --git a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/inc/mcs_inc_enc.h b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/inc/mcs_inc_enc.h deleted file mode 100644 index 97fbda72..00000000 --- a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/inc/mcs_inc_enc.h +++ /dev/null @@ -1,126 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_incremental_enc.h - * @author MCU Algorithm Team - * @brief This file provides function of QDM module and encoder speed angle calculation. - */ - -/* Define to prevent recursive inclusion ------------------------------------------------------- */ -#ifndef MCU_MAGICTAG_MCS_INC_ENC_H -#define MCU_MAGICTAG_MCS_INC_ENC_H - -/* Includes ------------------------------------------------------------------------------------ */ -#include "qdm_ip.h" -#include "mcs_ex_common.h" - -/* Macro definitions --------------------------------------------------------------------------- */ -#define SPDBUF_MAXSIZE 20 - -/** - * @brief QDM Peripheral management. - */ -typedef struct { - void *qdmAddr; /* QDM peripheral address */ - NvicHandle zPulsesNvic; -} MCS_QdmHandle; - -/** - * @brief QDM initialization structure. - */ -typedef struct { - void *qdmAddr; /**< QDM peripheral address */ - NvicHandle *zPlusesNvic; /**< z Pulse Interrupt Configuration */ - unsigned int zPlusesIrqPrio; /**< Interrupt priority */ - IRQ_PROC_FUNC zPlusesIrqFunc; /**< Interrupt function */ -} MCS_QdmInitStru; - -/** - * @brief Encoder motor parameter structure. - */ -typedef struct { - signed int mtrPPMR; /**< pulse per mechanical round */ - unsigned int zShift; /**< pulse Z shift */ - unsigned int mtrNp; /**< numbers of pole pairs */ - float ctrlPeriod; /**< The encoder calculates the control period. */ - /**< the times of compute the angle, if reach the specified num, it will be reset to 0 again */ - unsigned int timeNum; -} MCS_EncInitStru; - -/** - * @brief encoder control data structure. - */ -typedef struct { - signed int pulsePerMechRound; /**< pulses of each mechanical round */ - signed int pulsePerElecRound; /**< pulses of each eletricity period */ - signed short zShift; /**< Z-pulse cheap compensation */ - float elecAngle; /**< electricity angle */ - float mechAngle; /**< motor mechine angle */ - unsigned short cntNow; /**< counter for now */ - unsigned short cntPre; /**< counter of last record */ - signed short pulsePos; /**< Number of pulses corresponding to mechanical - position [-32768, 32767] */ - float elecSpeed; /**< elec speed, in HZ */ - signed short speedBuf[SPDBUF_MAXSIZE]; /**< speed buffer */ - signed short speedBufSize; /**< speed buffer size */ - signed short speedBufIndex; /**< speed bufer index */ - float pulseToHzPu; /**< pulse to HZ transition value */ - - unsigned short timeCnt; /**< times of compute the angle, the carrier ISR call times */ - /**< the times of compute the angle, if reach the specified num, it will be reset to 0 again */ - unsigned short timeNum; - - unsigned short pulZCnt; /**< counter of Z pulse */ - unsigned short pulseAngle; /**< Number of pulses corresponding to electrical angle. - [0, pulsePerElecRound-1] */ - float pulseToElecAngle; /**< pulse to electricity angle transition */ - unsigned short testAngle; -} EncoderHandle; -/** - * @} - */ - -/** - * @defgroup MCS_INC_ENC_Declaration incremental encoder. - * @{ - */ -void MCS_QdmInit(MCS_QdmInitStru *qdmInit); -void MCS_GetEncoderCnt(EncoderHandle *handle, QDM_RegStruct *qdm); -void MCS_GetElecAngleByEnc(EncoderHandle *handle); -void MCS_GetElecSpeedByEnc(EncoderHandle *handle); -void MCS_EncoderClear(EncoderHandle *handle); -void MCS_EncoderInit(EncoderHandle *handle, MCS_EncInitStru *encParam); -/** - * @brief Get the QDM position counter. - * @retval unsigned short QDM SCNT. - */ -static inline unsigned short MCS_GetQdmPosCnt(QDM_RegStruct *qdm) -{ - return (unsigned short)(qdm->QPOSCNT); -} -/** - * @} - */ - -/** - * @} - */ - -/** - * @} - */ -#endif /* McuMagicTag_MCS_INC_ENC_H */ \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/inc/mcs_motor_process.h b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/inc/mcs_motor_process.h deleted file mode 100644 index 783bf214..00000000 --- a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/inc/mcs_motor_process.h +++ /dev/null @@ -1,45 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_motor_process.h - * @author MCU Algorithm Team - * @brief This file provides motor sample functions declaration for ECMCU105H board. - */ -#ifndef McuMagicTag_MCS_MOTOR_PROCESS_H -#define McuMagicTag_MCS_MOTOR_PROCESS_H - -/** - * @brief phase sequence. - */ -typedef enum { - PHASE_U = 0, - PHASE_V, - PHASE_W, - PHASE_MAX_NUM -} MCS_PhaseEnum; - -/** - * @brief key state. - */ -typedef enum { - KEY_DOWN = 0, - KEY_UP = 1, -} KEY_State; - -int MotorMainProcess(void); - -#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/inc/mcs_status.h b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/inc/mcs_status.h deleted file mode 100644 index 19510c78..00000000 --- a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/inc/mcs_status.h +++ /dev/null @@ -1,188 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_status.h - * @author MCU Algorithm Team - * @brief This file provides functions declaration of system status. - */ - -/* Define to prevent recursive inclusion ------------------------------------------------------- */ -#ifndef McuMagicTag_MCS_STATUS_H -#define McuMagicTag_MCS_STATUS_H - -/* Includes ------------------------------------------------------------------------------------ */ -#include "typedefs.h" -#include "mcs_assert.h" - -/* Typedef definitions ------------------------------------------------------------------------- */ -/** - * @brief System status define - */ -typedef union { - unsigned short all; - struct { - unsigned short cmdStart : 1; /**< Indicates that a start system command has been received. */ - unsigned short cmdStop : 1; /**< Indicates that a stop system command has been received. */ - unsigned short isRunning : 1; /**< Indicates that the system is running (enable signal) */ - unsigned short sysError : 1; /**< Indicates that the system reports an error. */ - unsigned short poweron : 1; /**< Indicates that the power-on initialization phase is complete. */ - unsigned short capcharge : 1; /**< Indicates that the bootstrap capacitor charging phase is complete. */ - unsigned short adczero : 1; /**< The current sampling point is reset to zero after power-on. */ - } Bit; -} SysStatusReg; - -/** - * @brief Get status of Bit cmdStart. - * @param sysStatus System status register handle. - * @retval Status of Bit cmdStart. - */ -static inline bool SysGetCmdStart(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - bool ret; - ret = (sysStatus->Bit.cmdStart == 1) ? true : false; - return ret; -} - -/** - * @brief Set Bit cmdStart. - * @param sysStatus System status register handle. - * @retval None. - */ -static inline void SysCmdStartSet(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - sysStatus->Bit.cmdStart = 1; -} - -/** - * @brief Clear Bit cmdStart. - * @param handle System status register handle. - * @retval None. - */ -static inline void SysCmdStartClr(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - sysStatus->Bit.cmdStart = 0; -} - -/** - * @brief Get status of Bit cmdStop. - * @param sysStatus System status register handle. - * @retval Status of Bit cmdStart. - */ -static inline bool SysGetCmdStop(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - bool ret; - ret = (sysStatus->Bit.cmdStop == 1) ? true : false; - return ret; -} - -/** - * @brief Set Bit cmdStop. - * @param sysStatus System status register handle. - * @retval None. - */ -static inline void SysCmdStopSet(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - sysStatus->Bit.cmdStop = 1; -} - -/** - * @brief Clear Bit cmdStop. - * @param sysStatus System status register handle. - * @retval None. - */ -static inline void SysCmdStopClr(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - sysStatus->Bit.cmdStop = 0; -} - -/** - * @brief Get status of Bit isRunning. - * @param sysStatus System status register handle. - * @retval Status of Bit isRunning. - */ -static inline bool SysIsRunning(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - bool ret; - ret = (sysStatus->Bit.isRunning == 1) ? true : false; - return ret; -} - -/** - * @brief Set Bit isRuning. - * @param sysStatus System status register handle. - * @retval None. - */ -static inline void SysRunningSet(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - sysStatus->Bit.isRunning = 1; -} - -/** - * @brief Clear Bit isRuning. - * @param sysStatus System status register handle. - * @retval None. - */ -static inline void SysRunningClr(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - sysStatus->Bit.isRunning = 0; -} - -/** - * @brief Get status of Bit sysError. - * @param sysStatus System status register handle. - * @retval Status of Bit sysError. - */ -static inline bool SysIsError(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - bool ret; - ret = (sysStatus->Bit.sysError == 1) ? true : false; - return ret; -} - -/** - * @brief Set Bit sysError. - * @param sysStatus System status register handle. - * @retval None. - */ -static inline void SysErrorSet(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - sysStatus->Bit.sysError = 1; -} - -/** - * @brief Clear Bit sysError. - * @param sysStatus System status register handle. - * @retval None. - */ -static inline void SysErrorClr(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - sysStatus->Bit.sysError = 0; -} - -#endif diff --git a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/inc/mcs_user_config.h b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/inc/mcs_user_config.h deleted file mode 100644 index 95a8aa17..00000000 --- a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/inc/mcs_user_config.h +++ /dev/null @@ -1,143 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_user_config.h - * @author MCU Algorithm Team - * @brief This file provides functions declaration of user config parameters. - */ -#ifndef McuMagicTag_MCS_CONFIG_H -#define McuMagicTag_MCS_CONFIG_H - -#include "debug.h" -#include "typedefs.h" - -#define SYSTICK_PERIOD_US 500u /* Systick定时器的周期,单位为微秒 */ - -#define INV_CAP_CHARGE_MS 3u /* 反向电容充电时间,单位为毫秒 */ - -#define INV_VOLTAGE_BUS 24.0f // 总线电压,单位为伏特 - -#define CTRL_CURR_PERIOD 0.0001f /* 载波中断服务例程的周期,100微秒 */ -#define CTRL_SYSTICK_PERIOD 0.0005f /* Systick控制周期,500微秒 */ - -/* 采样窗口的占空比,实际时间为0.06*50us = 3us。 */ -#define SAMPLE_WINDOW_DUTY 0.06f - -/* 采样点移位作为翻转点的占空比,实际时间为0.008*50us = 0.4us。 */ -#define SAMPLE_POINT_SHIFT 0.008f - -/* 采样电阻200毫欧姆,0.0013295 */ -#define ADC_CURR_COFFI 0.0013295f /* 3.3/4096/3.03/0.2 pga: 3.03, 200mohm */ -/* APT */ -#define APT_SYNC_IN_SRC APT_SYNCIN_SRC_APT0_SYNCOUT /* APT同步输入源 */ - -#define APT_U APT0_BASE /* U相APT模块的基地址 */ -#define APT_V APT1_BASE /* V相APT模块的基地址 */ -#define APT_W APT2_BASE /* W相APT模块的基地址 */ - -/* User_Commond */ -#define CTRL_IF_CURR_AMP_A 1.0f /* IF控制电流幅值 */ -#define USER_TARGET_SPD_HZ 40.0f /* 用户目标速度,单位为赫兹,括号用于输入负指令 */ - -#define USER_MIN_SPD_HZ 5.0f /* 电机最小速度限制,单位为赫兹 */ -#define USER_MAX_SPD_HZ 200.0f /* 电机最大速度限制,单位为赫兹 */ -#define USER_SPD_SLOPE 30.0f /* 速度变化斜率 */ -#define USER_CURR_SLOPE (CTRL_IF_CURR_AMP_A * 5.0f) /* 电流变化斜率 */ - -/* PID PARAMS */ -#define CURRQAXIS_KP 0.7414f /* Q轴电流PID控制器的比例系数 */ -#define CURRQAXIS_KI 1256.0f /* Q轴电流PID控制器的积分系数 */ -#define CURRDAXIS_KP 0.7414f /* D轴电流PID控制器的比例系数 */ -#define CURRDAXIS_KI 1256.0f /* D轴电流PID控制器的积分系数 */ -#define CURR_LOWERLIM (-INV_VOLTAGE_BUS * ONE_DIV_SQRT3 * 0.95f) /* 电流下限 */ -#define CURR_UPPERLIM (INV_VOLTAGE_BUS * ONE_DIV_SQRT3 * 0.95f) /* 电流上限 */ - -#define SPD_KP 0.01f /* 速度PID控制器的比例系数 */ -#define SPD_KI 0.1f /* 速度PID控制器的积分系数 */ -#define SPD_LOWERLIM -1.0f /* 速度下限 */ -#define SPD_UPPERLIM 1.0f /* 速度上限 */ - -#define POS_KP 5.0f /* 位置PID控制器的比例系数 */ -#define POS_KI 0.3f /* 位置PID控制器的积分系数 */ -#define POS_KD 0.01f /* 位置PID控制器的微分系数 */ -#define POS_NS 10.0f /* 位置环Ns参数 */ -#define POS_LOWERLIM -200.0f /* 位置下限 */ -#define POS_UPPERLIM 200.0f /* 位置上限 */ - -/* MOTOR PARAMS */ -/* Np, Rs, Ld, Lq, Psif, J, Nmax, Currmax, PPMR, zShift */ -/* mtrPsif & mtrJ parameter is not used in this project, temporarily set to 0 */ - -/* 电机参数 */ -/* Np, Rs, Ld, Lq, Psif, J, Nmax, Currmax, PPMR, zShift */ -/* mtrPsif & mtrJ 参数在这个项目中没有使用,暂时设置为0 */ -// 电机的极对数 -/* 电机的电阻,单位为欧姆 */ -/* 电机的直轴电感,单位为亨利 */ -/* 电机的交轴电感,单位为亨利 */ -/* 电机的磁链,本项目未使用 */ -/* 电机的转动惯量,本项目未使用 */ -/* 电机的最大电气转速,单位为赫兹 */ -/* 电机的最大电流,单位为安培 */ -/* 电机的每转脉冲数 */ -/* 电机的零位偏移 */ - -#define MOTORPARAM_DEFAULTS { \ - .mtrNp = 4, \ - .mtrRs = 0.5f, \ - .mtrLd = 0.000295f, \ - .mtrLq = 0.000295f, \ - .mtrPsif = 0.0f, \ - .mtrJ = 0.0f, \ - .maxElecSpd = 200.0f, \ - .maxCurr = 1.5f, \ - .mtrPPMR = 4000, \ - .zShift = 410, \ -} - -// // //1025电机 -// #define MOTORPARAM_DEFAULTS { \ -// .mtrNp = 21, \ -// .mtrRs = 2.5f, \ -// .mtrLd = 0.066f, \ -// .mtrLq = 0.066f, \ -// .mtrPsif = 0.0f, \ -// .mtrJ = 0.0f, \ -// .maxElecSpd = 200.0f, \ -// .maxCurr = 1.5f, \ -// .mtrPPMR = 4000, \ -// .zShift = 410, \ -// } - - -// // GIM4310-10 -// #define MOTORPARAM_DEFAULTS { \ -// .mtrNp = 14, \ -// .mtrRs = 1.046f, \ -// .mtrLd = 0.000344f, \ -// .mtrLq = 0.000344f, \ -// .mtrPsif = 0.0f, \ -// .mtrJ = 0.0f, \ -// .maxElecSpd = 200.0f, \ -// .maxCurr = 1.5f, \ -// .mtrPPMR = 4000, \ -// .zShift = 410, \ -// } - -#define ADC_UDC_COFFI 0.01289f /* 0.01289 = 3.3/4096*192/12 */ -// ADC和UDC的系数,用于将ADC值转换为实际电压值 -#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/common/mcs_prot_cmm.c b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/common/mcs_prot_cmm.c deleted file mode 100644 index 99766bdf..00000000 --- a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/common/mcs_prot_cmm.c +++ /dev/null @@ -1,43 +0,0 @@ -/** - * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_prot_cmm.c - * @author MCU Algorithm Team - * @brief This file contains protection common api definition. - */ - -/* Includes ------------------------------------------------------------------------------------ */ -#include "mcs_prot_cmm.h" -#include "apt.h" -#include "mcs_assert.h" - -/** - * @brief Safty-pulse-off function execution to turn off all the power devices. - * @param aptAddr Three-phase APT address pointer. - * @retval None. - */ -void ProtSpo_Exec(APT_RegStruct **aptAddr) -{ - MCS_ASSERT_PARAM(aptAddr != NULL); - /**< Turn off all the six power devices of the inverter. */ - for (unsigned int i = 0; i < MOTOR_PHASE_NUMBER; i++) { - APT_RegStruct *aptx = (APT_RegStruct *)(aptAddr[i]); - DCL_APT_EnableSwContPWMAction(aptx, APT_PWM_CHANNEL_A); - DCL_APT_EnableSwContPWMAction(aptx, APT_PWM_CHANNEL_B); - DCL_APT_ForcePWMOutputLow(aptx); - } -} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/common/mcs_prot_cmm.h b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/common/mcs_prot_cmm.h deleted file mode 100644 index 65e5632f..00000000 --- a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/common/mcs_prot_cmm.h +++ /dev/null @@ -1,136 +0,0 @@ -/** - * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_prot_cmm.h - * @author MCU Algorithm Team - * @brief This file contains protection function common data struct and api declaration. - */ - -/* Define to prevent recursive inclusion ------------------------------------------------------- */ -#ifndef McuMagicTag_MCS_PROT_CMM_H -#define McuMagicTag_MCS_PROT_CMM_H - -#include "typedefs.h" -#include "apt_ip.h" - -/* Macro definitions --------------------------------------------------------------------------- */ -#define MOTOR_PHASE_NUMBER (3) - -#define PROT_VAL_THRESHOLD_NUMS (4) -#define PROT_VAL_THRESHOLD_0 (0) -#define PROT_VAL_THRESHOLD_1 (1) -#define PROT_VAL_THRESHOLD_2 (2) -#define PROT_VAL_THRESHOLD_3 (3) - -#define PROT_LIMIT_TIME_NUMS (3) -#define PROT_LIMIT_TIME_0 (0) -#define PROT_LIMIT_TIME_1 (1) -#define PROT_LIMIT_TIME_2 (2) - -#define MOTOR_PHASE_NUMBER (3) - -/**< Motor error status definition. */ -typedef union { - int all; - struct { - unsigned short overCurrErr : 1; /**<表示相电流超过保护值*/ - unsigned short overVoltErr : 1; /**<表示直流链路电压超过保护值*/ - unsigned short lowerVoltErr : 1; /**<表示直流链路电压低于保护值*/ - unsigned short overIpmTempErr : 1; /**<表示IPM温度超过保护值*/ - unsigned short revRotErr : 1; /**<表示电机为负方向*/ - unsigned short motorStalling : 1; /**<表示转子失速*/ - unsigned short overMotorTempErr : 1; /**<表示三相电流不平衡*/ - unsigned short posSnsrCommsErr : 1; /**<表示位置传感器与MCU的通信中断*/ - unsigned short posSnsrFuncErr : 1; /**<表示位置传感器报告功能错误*/ - unsigned short posSnsrCalibrErr : 1; /**<表示位置传感器无法自行校准*/ - unsigned short currOutOfBalance : 1; /**<表示转子反向旋转*/ - unsigned short phsOpenErr : 1; /**<表示相绕组断开*/ - unsigned short phsU : 1; /**<表示发生phsOpenErr时u阶段失败*/ - unsigned short phsV : 1; /**<表示发生phsOpenErr时v阶段失败*/ - unsigned short phsW : 1; /**<表示发生phsOpenErr时w阶段失败*/ - unsigned short multiPhs : 1; /**<表示发生phsOpenErr时多阶段失败*/ - } Bit; -} MotorErrStatusReg; - -/**<保护状态位定义*/ -typedef enum { - OCP_ERR_BIT, - OVP_ERR_BIT, - LVP_ERR_BIT, - OTP_IPM_ERR_BIT, - OTP_MOTOR_ERR_BIT, - STALLING_ERR_BIT, - CURR_OUT_BALANCE_ERR_BIT, - POS_COMMS_ERR_BIT, - POS_FUNC_ERR_BIT, - POS_CALIB_ERR_BIT, - REV_ROT_ERR_BIT, - PHS_OPEN_ERR_BIT, - PHS_U_ERR_BIT, - PHS_V_ERR_BIT, - PHS_W_ERR_BIT, - PHS_MULTI_ERR_BIT, -} PROT_ErrBit; - -/**< Motor error protection level. */ -typedef enum { - PROT_LEVEL_0 = 0, - PROT_LEVEL_1, - PROT_LEVEL_2, - PROT_LEVEL_3, - PROT_LEVEL_4 /**< The greater level number, the severe error is. */ -} PROT_Level; - -/** - * @brief Obtains the status of a bit of data. - * @param data data. - * @param bits Number of digits. - * @retval Bit status. - */ -static inline bool GetBit(int data, unsigned short bit) -{ - bool ret; - ret = ((data >> bit) & 1); - return ret; -} - -/** - * @brief Sets the status of a bit of data. - * @param data data. - * @param bit The setted bit. - * @retval None. - */ -static inline void SetBit(int *data, unsigned char bit) -{ - *data |= (1 << bit); -} - -/** - * @brief Clear the status of a bit of data. - * @param data data. - * @param bit The Clear bit. - * @retval None. - */ -static inline void ClearBit(int *data, unsigned char bit) -{ - *data &= ~(1 << bit); -} - -/**< Protection action. */ -void ProtSpo_Exec(APT_RegStruct **aptAddr); - -#endif diff --git a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/common/mcs_prot_user.c b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/common/mcs_prot_user.c deleted file mode 100644 index e97876b7..00000000 --- a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/common/mcs_prot_user.c +++ /dev/null @@ -1,137 +0,0 @@ -/** - * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_prot_user.c - * @author MCU Algorithm Team - * @brief This file contains user protection data struct, inquiry api and initialization api definition. - */ - -/* Includes ------------------------------------------------------------------------------------ */ -#include "mcs_prot_user.h" -#include "mcs_prot_user_config.h" -#include "mcs_assert.h" - -/** - * @brief Get motor over current error status. - * @param motorErrStatus Motor error status. - * @retval Error status. - */ -bool IsMotorOverCurrErr(MotorErrStatusReg motorErrStatus) -{ - if (motorErrStatus.Bit.overCurrErr) { - return true; - } else { - return false; - } -} - -/** - * @brief Get motor over dc-link voltage error status. - * @param motorErrStatus Motor error status. - * @retval Error status. - */ -bool IsDcLinkOverVoltErr(MotorErrStatusReg motorErrStatus) -{ - if (motorErrStatus.Bit.overVoltErr) { - return true; - } else { - return false; - } -} - -/** - * @brief Get motor lower dc-link voltage error status. - * @param motorErrStatus Motor error status. - * @retval Error status. - */ -bool IsDcLinkLowerVoltErr(MotorErrStatusReg motorErrStatus) -{ - if (motorErrStatus.Bit.lowerVoltErr) { - return true; - } else { - return false; - } -} - -/** - * @brief Get motor over Ipm temperature error status. - * @param motorErrStatus Motor error status. - * @retval Error status. - */ -bool IsIpmOverTempErr(MotorErrStatusReg motorErrStatus) -{ - if (motorErrStatus.Bit.overIpmTempErr) { - return true; - } else { - return false; - } -} - -/** - * @brief Get motor over Motor temperature error status. - * @param motorErrStatus Motor error status. - * @retval Error status. - */ -bool IsMotorOverTempErr(MotorErrStatusReg motorErrStatus) -{ - if (motorErrStatus.Bit.overMotorTempErr) { - return true; - } else { - return false; - } -} - -/** - * @brief Get motor stalling error status. - * @param motorErrStatus Motor error status. - * @retval Error status. - */ -bool IsMotorStallingErr(MotorErrStatusReg motorErrStatus) -{ - if (motorErrStatus.Bit.motorStalling) { - return true; - } else { - return false; - } -} - -/** - * @brief Clear the motor error status. - * @param motorProt Motor protection handle. - * @retval Error status. - */ -void ClearMotorErrStatus(MotorProtStatus_Handle *motorProt) -{ - MCS_ASSERT_PARAM(motorProt != NULL); - /* Clear the motor error status. */ - motorProt->motorErrStatus.all = 0x00; - OCP_Clear(&motorProt->ocp); - OVP_Clear(&motorProt->ovp); - LVP_Clear(&motorProt->lvp); - OTP_Clear(&motorProt->otp); -} - -/** - * @brief Motor protection function initialization. - * @param motorProt Motor protection handle. - * @retval Error status. - */ -void MotorProt_Init(MotorProtStatus_Handle *motorProt) -{ - MCS_ASSERT_PARAM(motorProt != NULL); - motorProt->motorErrStatus.all = 0x00; -} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/common/mcs_prot_user.h b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/common/mcs_prot_user.h deleted file mode 100644 index 365110e1..00000000 --- a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/common/mcs_prot_user.h +++ /dev/null @@ -1,54 +0,0 @@ -/** - * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_prot_user.h - * @author MCU Algorithm Team - * @brief This file contains user protection data struct, inquiry api and initialization api declaration. - */ - -/* Define to prevent recursive inclusion ------------------------------------------------------- */ -#ifndef McuMagicTag_MCS_PROT_USER_H -#define McuMagicTag_MCS_PROT_USER_H - -/* Includes ------------------------------------------------------------------------------------ */ -#include "mcs_prot_cmm.h" -#include "mcs_curr_prot.h" -#include "mcs_dc_volt_prot.h" -#include "mcs_temp_prot.h" -#include "mcs_motor_stalling.h" -#include "typedefs.h" - -typedef struct { - MotorErrStatusReg motorErrStatus; /**< Motor error status. */ - OCP_Handle ocp; /**< Over current protection. */ - OVP_Handle ovp; /**< Over dc-link voltage protection. */ - LVP_Handle lvp; /**< Lower dc-link voltage protection. */ - OTP_Handle otp; /**< Over IPM temperature protection. */ - STP_Handle stall; /**< Motor stalling protection. */ -} MotorProtStatus_Handle; - -void MotorProt_Init(MotorProtStatus_Handle *motorProt); - -/**< Inquiry motor error status */ -bool IsMotorOverCurrErr(MotorErrStatusReg motorErrStatus); -bool IsDcLinkOverVoltErr(MotorErrStatusReg motorErrStatus); -bool IsDcLinkLowerVoltErr(MotorErrStatusReg motorErrStatus); -bool IsIpmOverTempErr(MotorErrStatusReg motorErrStatus); -bool IsMotorOverTempErr(MotorErrStatusReg motorErrStatus); -bool IsMotorStallingErr(MotorErrStatusReg motorErrStatus); -void ClearMotorErrStatus(MotorProtStatus_Handle *motorProt); -#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/common/mcs_prot_user_config.h b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/common/mcs_prot_user_config.h deleted file mode 100644 index 988c519d..00000000 --- a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/common/mcs_prot_user_config.h +++ /dev/null @@ -1,130 +0,0 @@ -/** - * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_prot_user_config.h - * @author MCU Algorithm Team - * @brief This file contans user macro definition of the protection function. - */ - -/* Define to prevent recursive inclusion ------------------------------------------------------- */ -#ifndef McuMagicTag_MCS_PROT_USER_CONFIG_H -#define McuMagicTag_MCS_PROT_USER_CONFIG_H - -/* Macro definitions --------------------------------------------------------------------------- */ - -/**< COMMON */ -/**<连续运行时电机或IPM的峰值相电流(A)*/ -#define PROT_MOTOR_RATED_CURR (1.0f) -/**<只有几次连续的故障检测才能触发错误状态*/ -#define PROT_CNT_LIMIT (100) -/**<只有几个连续的无故障检测才能触发错误状态的消除*/ -#define RECY_CNT_LIMIT (10000) -/**<只有几个连续的无故障检测才能触发错误状态的消除*/ -#define OVER_VOLT_RECY_CNT_LIMIT (100) -/**<只有几个连续的无故障检测才能触发错误状态的消除*/ -#define LOWER_VOLT_RECY_CNT_LIMIT (100) - -/**<过电流保护*/ -/**<处于1级时的过电流触发值(A)*/ -#define PROT_OVER_CURR_POW_DN1 (2.0f * PROT_MOTOR_RATED_CURR) -/**<处于2级时的过电流触发值(A)*/ -#define PROT_OVER_CURR_POW_DN2 (2.0f * PROT_MOTOR_RATED_CURR) -/**<处于3级时的过电流触发值(A)*/ -#define PROT_OVER_CURR_POW_DN3 (2.0f * PROT_MOTOR_RATED_CURR) -/**<处于4级时的过电流触发值(A)*/ -#define PROT_OVER_CURR_POW_OFF (2.0f * PROT_MOTOR_RATED_CURR) -#define PROT_OVER_CURR_RECY_DELTA (0.0035f) /*<从保护状态恢复时的电流间隙(A)*/ -#define PROT_OVER_CURR_LIMIT1_TIME_SEC (30.0f) /**<20%过载的最长持续时间:30秒*/ -#define PROT_OVER_CURR_LIMIT2_TIME_SEC (10.0f) /**<20%过载的最长持续时间:10秒*/ -#define PROT_OVER_CURR_LIMIT3_TIME_SEC (2.0f) /**<20%过载的最长持续时间:2秒*/ - -/**< Over voltage protection */ -#define PROT_OVER_VOLT_BRK_ON1 (26.0f) /**<处于1级时,直流链路电压触发值(V)过高*/ -#define PROT_OVER_VOLT_BRK_ON2 (27.0f) /**< Over dc-link voltage trigger value (V) when in level 2. */ -#define PROT_OVER_VOLT_BRK_ON3 (28.0f) /**< Over dc-link voltage trigger value (V) when in level 3. */ -#define PROT_OVER_VOLT_BRK_ALL (30.0f) /**< Over dc-link voltage trigger value (V) when in level 4. */ -#define PROT_OVER_VOLT_RECY_DELTA (0.5f) /**<从保护状态恢复时的电压间隙(V)*/ -#define PROT_OVER_VOLT_LIMIT1_TIME_SEC (5.0f) /**1;单循环模式:<0.5*/ -#define POS_SNSR_IPD_INJ_PERIOD (4) - -#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/mcs_curr_prot.c b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/mcs_curr_prot.c deleted file mode 100644 index da4e56f8..00000000 --- a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/mcs_curr_prot.c +++ /dev/null @@ -1,267 +0,0 @@ -/** - * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_curr_prot.c - * @author MCU Algorithm Team - * @brief This file contains current protecion api definition. - */ - -/* Includes ------------------------------------------------------------------------------------ */ -#include "mcs_curr_prot.h" -#include "mcs_math.h" -#include "mcs_prot_user_config.h" -#include "mcs_assert.h" - -/** - * @brief Initilization over current protection function. - * @param ocp Over current protection handle. - * @param ts Ctrl period (s). - * @retval None. - */ -void OCP_Init(OCP_Handle *ocp, float ts) -{ - MCS_ASSERT_PARAM(ocp != NULL); - MCS_ASSERT_PARAM(ts > 0.0f); - ocp->ts = ts; - OCP_Clear(ocp); - - ocp->protCntLimit = PROT_CNT_LIMIT; - ocp->recyCntLimit = RECY_CNT_LIMIT; - /* Configuring four levels of current protection thresholds. */ - ocp->protValThr[PROT_VAL_THRESHOLD_0] = PROT_OVER_CURR_POW_DN1; - ocp->protValThr[PROT_VAL_THRESHOLD_1] = PROT_OVER_CURR_POW_DN2; - ocp->protValThr[PROT_VAL_THRESHOLD_2] = PROT_OVER_CURR_POW_DN3; - ocp->protValThr[PROT_VAL_THRESHOLD_3] = PROT_OVER_CURR_POW_OFF; - /* Configure the protection limit time. */ - ocp->protLimitTime[PROT_LIMIT_TIME_0] = PROT_OVER_CURR_LIMIT1_TIME_SEC; - ocp->protLimitTime[PROT_LIMIT_TIME_1] = PROT_OVER_CURR_LIMIT2_TIME_SEC; - ocp->protLimitTime[PROT_LIMIT_TIME_2] = PROT_OVER_CURR_LIMIT3_TIME_SEC; - ocp->recyDelta = PROT_OVER_CURR_RECY_DELTA; -} - -/** - * @brief Over current protection detection. - * @param ocp Over current protection handle. - * @param motorErrStatus Motor error status. - * @param idq DQ-axis feedback currents. - * @retval None. - */ -void OCP_Det(OCP_Handle *ocp, MotorErrStatusReg *motorErrStatus, DqAxis idq) -{ - MCS_ASSERT_PARAM(ocp != NULL); - MCS_ASSERT_PARAM(motorErrStatus != NULL); - /* Calculate current amplitude. */ - ocp->currAmp = Sqrt(idq.d * idq.d + idq.q * idq.q); - - /* Check if value goes over threshold for continuous cycles. */ - if (ocp->currAmp < ocp->protValThr[PROT_VAL_THRESHOLD_0]) { - ocp->protCnt = 0; - return; - } - - if (ocp->protCnt < ocp->protCntLimit) { - ocp->protCnt++; - return; - } - - /* Check which protection level should be activated. */ - /* Once enter error status, error status can only be changed by recover api. */ - /* protect level: level 4. */ - if (ocp->currAmp >= ocp->protValThr[PROT_VAL_THRESHOLD_3] && ocp->protLevel < PROT_LEVEL_4) { - ocp->protLevel = PROT_LEVEL_4; - motorErrStatus->Bit.overCurrErr = 1; - ocp->protCnt = 0; - return; - } - - /* Protect level: level 3. */ - if (ocp->currAmp >= ocp->protValThr[PROT_VAL_THRESHOLD_2] && ocp->protLevel < PROT_LEVEL_3) { - ocp->protLevel = PROT_LEVEL_3; - motorErrStatus->Bit.overCurrErr = 1; - ocp->protCnt = 0; - ocp->timer = 0.0f; - return; - } - - /* Protect level: level 2. */ - if (ocp->currAmp >= ocp->protValThr[PROT_VAL_THRESHOLD_1] && ocp->protLevel < PROT_LEVEL_2) { - ocp->protLevel = PROT_LEVEL_2; - motorErrStatus->Bit.overCurrErr = 1; - ocp->protCnt = 0; - ocp->timer = 0.0f; - return; - } - - /* Protect level: level 1. */ - if (ocp->currAmp >= ocp->protValThr[PROT_VAL_THRESHOLD_0] && ocp->protLevel < PROT_LEVEL_1) { - ocp->protLevel = PROT_LEVEL_1; - motorErrStatus->Bit.overCurrErr = 1; - ocp->protCnt = 0; - ocp->timer = 0.0f; - return; - } -} - -/** - * @brief Over current protection execution. - * @param ocp Over current protection handle. - * @param idqRef DQ-axis current references. - * @param aptAddr Three-phase APT address pointer. - * @retval None. - */ -void OCP_Exec(OCP_Handle *ocp, DqAxis *idqRef, APT_RegStruct **aptAddr) -{ - MCS_ASSERT_PARAM(ocp != NULL); - MCS_ASSERT_PARAM(idqRef != NULL); - MCS_ASSERT_PARAM(aptAddr != NULL); - - float id = idqRef->d; - float iq = idqRef->q; - float idqAmp = ocp->currAmp; - /* According to protect level, take corresponding action. */ - switch (ocp->protLevel) { - /* level 4: disable all PWM output. */ - case PROT_LEVEL_4: - /* Disable three-phase pwm output. */ - ProtSpo_Exec(aptAddr); - idqRef->d = 0.0f; - idqRef->q = 0.0f; - break; - - /* level 3: derate speed reference. */ - case PROT_LEVEL_3: - ocp->timer += ocp->ts; - if (ocp->timer > ocp->protLimitTime[PROT_LIMIT_TIME_2]) { - idqRef->d = id / idqAmp * PROT_MOTOR_RATED_CURR; - idqRef->q = iq / idqAmp * PROT_MOTOR_RATED_CURR; - } - break; - - /* level 2: derate speed reference. */ - case PROT_LEVEL_2: - ocp->timer += ocp->ts; - if (ocp->timer > ocp->protLimitTime[PROT_LIMIT_TIME_1]) { - idqRef->d = id / idqAmp * PROT_MOTOR_RATED_CURR; - idqRef->q = iq / idqAmp * PROT_MOTOR_RATED_CURR; - } - break; - - /* level 1: derate speed reference. */ - case PROT_LEVEL_1: - ocp->timer += ocp->ts; - if (ocp->timer > ocp->protLimitTime[PROT_LIMIT_TIME_0]) { - idqRef->d = id / idqAmp * PROT_MOTOR_RATED_CURR; - idqRef->q = iq / idqAmp * PROT_MOTOR_RATED_CURR; - } - break; - - /* level 0: take no protection action. */ - case PROT_LEVEL_0: - break; - - default: - break; - } -} - -/** - * @brief Over current protection recovery. - * @param ocp Over current protection handle. - * @param motorErrStatus Motor error status. - * @retval None. - */ -void OCP_Recy(OCP_Handle *ocp, MotorErrStatusReg *motorErrStatus) -{ - MCS_ASSERT_PARAM(ocp != NULL); - /* If not under error state, just return without any operation. */ - if (!motorErrStatus->Bit.overCurrErr) { - return; - } - - /* Calculate current amplitude. */ - float currAmp = ocp->currAmp; - - /* According to protection level, take corresponding recovery action. */ - switch (ocp->protLevel) { - /* level 4 */ - case PROT_LEVEL_4: - if (currAmp < ocp->protValThr[PROT_VAL_THRESHOLD_3] - ocp->recyDelta) { - ocp->recyCnt++; - if (ocp->recyCnt > ocp->recyCntLimit) { - ocp->protLevel = PROT_LEVEL_3; - ocp->recyCnt = 0; - } - } - break; - - /* level 3 */ - case PROT_LEVEL_3: - if (currAmp < ocp->protValThr[PROT_VAL_THRESHOLD_2] - ocp->recyDelta) { - ocp->recyCnt++; - if (ocp->recyCnt > ocp->recyCntLimit) { - ocp->protLevel = PROT_LEVEL_2; - ocp->recyCnt = 0; - } - } - break; - - /* level 2 */ - case PROT_LEVEL_2: - if (currAmp < ocp->protValThr[PROT_VAL_THRESHOLD_1] - ocp->recyDelta) { - ocp->recyCnt++; - if (ocp->recyCnt > ocp->recyCntLimit) { - ocp->protLevel = PROT_LEVEL_1; - ocp->recyCnt = 0; - } - } - break; - - /* level 1 */ - case PROT_LEVEL_1: - if (currAmp < ocp->protValThr[PROT_VAL_THRESHOLD_0] - ocp->recyDelta) { - ocp->recyCnt++; - if (ocp->recyCnt > ocp->recyCntLimit) { - ocp->protLevel = PROT_LEVEL_0; - ocp->recyCnt = 0; - } - } - break; - - /* level 0 */ - case PROT_LEVEL_0: - motorErrStatus->Bit.overCurrErr = 0; - break; - - default: - break; - } -} - -/** - * @brief Over current protection error status clear. - * @param ocp Over current protection handle. - * @retval None. - */ -void OCP_Clear(OCP_Handle *ocp) -{ - MCS_ASSERT_PARAM(ocp != NULL); - /* Clear the history value. */ - ocp->protCnt = 0; - ocp->protLevel = PROT_LEVEL_0; - ocp->recyCnt = 0; - ocp->timer = 0.0f; -} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/mcs_curr_prot.h b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/mcs_curr_prot.h deleted file mode 100644 index e9916fe4..00000000 --- a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/mcs_curr_prot.h +++ /dev/null @@ -1,52 +0,0 @@ -/** - * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_curr_prot.h - * @author MCU Algorithm Team - * @brief This file contains current protection data struct and api declaration. - */ - -/* Define to prevent recursive inclusion ------------------------------------- */ -#ifndef McuMagicTag_MCS_CURR_PROT_H -#define McuMagicTag_MCS_CURR_PROT_H - -/* Includes ------------------------------------------------------------------------------------ */ -#include "mcs_prot_cmm.h" -#include "mcs_typedef.h" -#include "apt_ip.h" - -typedef struct { - unsigned int protCnt; - unsigned int recyCnt; - unsigned int protCntLimit; - unsigned int recyCntLimit; - float currAmp; - float protValThr[PROT_VAL_THRESHOLD_NUMS]; /* from low current level to high. */ - float protLimitTime[PROT_LIMIT_TIME_NUMS]; /* from level 3 to level 1. */ - float recyDelta; - float timer; - float ts; - PROT_Level protLevel; -} OCP_Handle; - -void OCP_Init(OCP_Handle *ocp, float ts); -void OCP_Det(OCP_Handle *ocp, MotorErrStatusReg *motorErrStatus, DqAxis idq); -void OCP_Exec(OCP_Handle *ocp, DqAxis *idqRef, APT_RegStruct **aptAddr); -void OCP_Recy(OCP_Handle *ocp, MotorErrStatusReg *motorErrStatus); -void OCP_Clear(OCP_Handle *ocp); - -#endif diff --git a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/mcs_dc_volt_prot.c b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/mcs_dc_volt_prot.c deleted file mode 100644 index b0541a2e..00000000 --- a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/mcs_dc_volt_prot.c +++ /dev/null @@ -1,495 +0,0 @@ -/** - * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_dc_volt_prot.c - * @author MCU Algorithm Team - * @brief This file contains dc-link voltage protection api declaration. - */ - -/* Includes ------------------------------------------------------------------------------------ */ -#include "mcs_dc_volt_prot.h" -#include "mcs_prot_user_config.h" -#include "mcs_assert.h" - -/** - * @brief Initilization over dc-link voltage protection function. - * @param ovp Over dc-link voltage protection handle. - * @param ts Ctrl period (s). - * @retval None. - */ -void OVP_Init(OVP_Handle *ovp, float ts) -{ - MCS_ASSERT_PARAM(ovp != NULL); - MCS_ASSERT_PARAM(ts > 0.0f); - ovp->ts = ts; - OVP_Clear(ovp); - ovp->protCntLimit = PROT_CNT_LIMIT; - ovp->recyCntLimit = OVER_VOLT_RECY_CNT_LIMIT; - /* Configuring four levels of overvoltage protection thresholds. */ - ovp->protValThr[PROT_VAL_THRESHOLD_0] = PROT_OVER_VOLT_BRK_ON1; - ovp->protValThr[PROT_VAL_THRESHOLD_1] = PROT_OVER_VOLT_BRK_ON2; - ovp->protValThr[PROT_VAL_THRESHOLD_2] = PROT_OVER_VOLT_BRK_ON3; - ovp->protValThr[PROT_VAL_THRESHOLD_3] = PROT_OVER_VOLT_BRK_ALL; - /* Configure the protection limit time. */ - ovp->protLimitTime[PROT_LIMIT_TIME_0] = PROT_OVER_VOLT_LIMIT1_TIME_SEC; - ovp->protLimitTime[PROT_LIMIT_TIME_1] = PROT_OVER_VOLT_LIMIT2_TIME_SEC; - ovp->protLimitTime[PROT_LIMIT_TIME_2] = PROT_OVER_VOLT_LIMIT3_TIME_SEC; - ovp->recyDelta = PROT_OVER_VOLT_RECY_DELTA; -} - -/** - * @brief Initilization lower dc-link voltage protection function. - * @param lvp Lower dc-link voltage protection handle. - * @param ts Ctrl period (s). - * @retval None. - */ -void LVP_Init(LVP_Handle *lvp, float ts) -{ - MCS_ASSERT_PARAM(lvp != NULL); - MCS_ASSERT_PARAM(ts > 0.0f); - - lvp->ts = ts; - LVP_Clear(lvp); - - lvp->protCntLimit = PROT_CNT_LIMIT; - lvp->recyCntLimit = LOWER_VOLT_RECY_CNT_LIMIT; - /* Configuring four levels of lower voltage protection thresholds. */ - lvp->protValThr[PROT_VAL_THRESHOLD_0] = PROT_LOWER_VOLT_POW_DN1; - lvp->protValThr[PROT_VAL_THRESHOLD_1] = PROT_LOWER_VOLT_POW_DN2; - lvp->protValThr[PROT_VAL_THRESHOLD_2] = PROT_LOWER_VOLT_POW_DN3; - lvp->protValThr[PROT_VAL_THRESHOLD_3] = PROT_LOWER_VOLT_POW_OFF; - /* Configure the protection limit time. */ - lvp->protLimitTime[PROT_LIMIT_TIME_0] = PROT_LOWER_VOLT_LIMIT1_TIME_SEC; - lvp->protLimitTime[PROT_LIMIT_TIME_1] = PROT_LOWER_VOLT_LIMIT2_TIME_SEC; - lvp->protLimitTime[PROT_LIMIT_TIME_2] = PROT_LOWER_VOLT_LIMIT3_TIME_SEC; - lvp->recyDelta = PROT_OVER_VOLT_RECY_DELTA; -} - -/** - * @brief Over dc-link voltage protection detection. - * @param ovp Over dc-link voltage protection handle. - * @param motorErrStatus Motor error status. - * @param udc DC-link voltage feedback (V). - * @retval None. - */ -void OVP_Det(OVP_Handle *ovp, MotorErrStatusReg *motorErrStatus, float udc) -{ - MCS_ASSERT_PARAM(ovp != NULL); - MCS_ASSERT_PARAM(motorErrStatus != NULL); - MCS_ASSERT_PARAM(udc > 0.0f); - /* Check if value goes over threshold for continuous cycles. */ - if (udc < ovp->protValThr[PROT_VAL_THRESHOLD_0]) { - ovp->protCnt = 0; - return; - } - - if (ovp->protCnt < ovp->protCntLimit) { - ovp->protCnt++; - return; - } - - /* Check which protection level should be activated. */ - /* Once enter error status, error status can only be changed by recover api. */ - /* protect level: level 4. */ - if (udc >= ovp->protValThr[PROT_VAL_THRESHOLD_3] && ovp->protLevel < PROT_LEVEL_4) { - ovp->protLevel = PROT_LEVEL_4; - motorErrStatus->Bit.overVoltErr = 1; - ovp->protCnt = 0; - return; - } - - /* Protect level: level 3. */ - if (udc >= ovp->protValThr[PROT_VAL_THRESHOLD_2] && ovp->protLevel < PROT_LEVEL_3) { - ovp->protLevel = PROT_LEVEL_3; - motorErrStatus->Bit.overVoltErr = 1; - ovp->protCnt = 0; - ovp->timer = 0.0f; - return; - } - - /* Protect level: level 2. */ - if (udc >= ovp->protValThr[PROT_VAL_THRESHOLD_1] && ovp->protLevel < PROT_LEVEL_2) { - ovp->protLevel = PROT_LEVEL_2; - motorErrStatus->Bit.overVoltErr = 1; - ovp->protCnt = 0; - ovp->timer = 0.0f; - return; - } - - /* Protect level: level 1. */ - if (udc >= ovp->protValThr[PROT_VAL_THRESHOLD_0] && ovp->protLevel < PROT_LEVEL_1) { - ovp->protLevel = PROT_LEVEL_1; - motorErrStatus->Bit.overVoltErr = 1; - ovp->protCnt = 0; - ovp->timer = 0.0f; - return; - } -} - -/** - * @brief Lower dc-link voltage protection detection. - * @param lvp Lower dc-link voltage protection handle. - * @param motorErrStatus Motor error status. - * @param udc DC-link voltage feedback (V). - * @retval None. - */ -void LVP_Det(LVP_Handle *lvp, MotorErrStatusReg *motorErrStatus, float udc) -{ - MCS_ASSERT_PARAM(lvp != NULL); - MCS_ASSERT_PARAM(motorErrStatus != NULL); - MCS_ASSERT_PARAM(udc > 0.0f); - /* Check if value goes over threshold for continuous cycles. */ - if (udc > lvp->protValThr[PROT_VAL_THRESHOLD_0]) { - lvp->protCnt = 0; - return; - } - - if (lvp->protCnt < lvp->protCntLimit) { - lvp->protCnt++; - return; - } - - /* Check which protection level should be activated. */ - /* Once enter error status, error status can only be changed by recover api. */ - /* protect level: level 4. */ - if (udc <= lvp->protValThr[PROT_VAL_THRESHOLD_3] && lvp->protLevel < PROT_LEVEL_4) { - lvp->protLevel = PROT_LEVEL_4; - motorErrStatus->Bit.lowerVoltErr = 1; - lvp->protCnt = 0; - return; - } - - /* Protect level: level 3. */ - if (udc <= lvp->protValThr[PROT_VAL_THRESHOLD_2] && lvp->protLevel < PROT_LEVEL_3) { - lvp->protLevel = PROT_LEVEL_3; - motorErrStatus->Bit.lowerVoltErr = 1; - lvp->protCnt = 0; - lvp->timer = 0.0f; - return; - } - - /* Protect level: level 2. */ - if (udc <= lvp->protValThr[PROT_VAL_THRESHOLD_1] && lvp->protLevel < PROT_LEVEL_2) { - lvp->protLevel = PROT_LEVEL_2; - motorErrStatus->Bit.lowerVoltErr = 1; - lvp->protCnt = 0; - lvp->timer = 0.0f; - return; - } - - /* Protect level: level 1. */ - if (udc <= lvp->protValThr[PROT_VAL_THRESHOLD_0] && lvp->protLevel < PROT_LEVEL_1) { - lvp->protLevel = PROT_LEVEL_1; - motorErrStatus->Bit.lowerVoltErr = 1; - lvp->protCnt = 0; - lvp->timer = 0.0f; - return; - } -} - -/** - * @brief Over dc-link voltage protection execution. - * @param ovp Over dc-link voltage protection handle. - * @param duty Brake loop output duty (0-1). - * @param aptAddr Three-phase APT address pointer. - * @retval None. - */ -void OVP_Exec(OVP_Handle *ovp, float *duty, APT_RegStruct **aptAddr) -{ - MCS_ASSERT_PARAM(ovp != NULL); - MCS_ASSERT_PARAM(duty != NULL); - MCS_ASSERT_PARAM(aptAddr != NULL); - /* According to protect level, take corresponding action. */ - switch (ovp->protLevel) { - /* level 4: brake loop duty maximum. */ - case PROT_LEVEL_4: - *duty = PROT_OVER_VOLT_BRK_DUTY4; - /* Disable three-phase pwm output. */ - ProtSpo_Exec(aptAddr); - break; - - /* level 3: brake loop duty level 3. */ - case PROT_LEVEL_3: - *duty = PROT_OVER_VOLT_BRK_DUTY2; - ovp->timer += ovp->ts; - if (ovp->timer > ovp->protLimitTime[PROT_LIMIT_TIME_2]) { - *duty = PROT_OVER_VOLT_BRK_DUTY3; - } - break; - - /* level 2: brake loop duty level 2. */ - case PROT_LEVEL_2: - *duty = PROT_OVER_VOLT_BRK_DUTY1; - ovp->timer += ovp->ts; - if (ovp->timer > ovp->protLimitTime[PROT_LIMIT_TIME_1]) { - *duty = PROT_OVER_VOLT_BRK_DUTY2; - } - break; - - /* level 1: brake loop duty level 1. */ - case PROT_LEVEL_1: - ovp->timer += ovp->ts; - if (ovp->timer > ovp->protLimitTime[PROT_LIMIT_TIME_0]) { - *duty = PROT_OVER_VOLT_BRK_DUTY1; - } - break; - - /* level 0: take no protection action. */ - case PROT_LEVEL_0: - break; - - default: - break; - } - return; -} - -/** - * @brief Lower dc-link voltage protection execution. - * @param lvp Lower dc-link voltage protection handle. - * @param spdRef Speed Reference (Hz). - * @param aptAddr Three-phase APT address pointer. - * @retval None. - */ -void LVP_Exec(LVP_Handle *lvp, float *spdRef, APT_RegStruct **aptAddr) -{ - MCS_ASSERT_PARAM(lvp != NULL); - MCS_ASSERT_PARAM(spdRef != NULL); - MCS_ASSERT_PARAM(aptAddr != NULL); - /* According to protect level, take corresponding action. */ - switch (lvp->protLevel) { - /* level 4: disable all PWM output. */ - case PROT_LEVEL_4: - /* Disable three-phase pwm output. */ - ProtSpo_Exec(aptAddr); - *spdRef *= 0.0f; - break; - - /* level 3: derate speed reference. */ - case PROT_LEVEL_3: - *spdRef *= PROT_POW_DN2_PCT; - lvp->timer += lvp->ts; - if (lvp->timer > lvp->protLimitTime[PROT_LIMIT_TIME_2]) { - *spdRef *= PROT_POW_DN3_PCT; - } - break; - - /* level 2: derate speed reference. */ - case PROT_LEVEL_2: - *spdRef *= PROT_POW_DN1_PCT; - lvp->timer += lvp->ts; - if (lvp->timer > lvp->protLimitTime[PROT_LIMIT_TIME_1]) { - *spdRef *= PROT_POW_DN2_PCT; - } - break; - - /* level 1: derate speed reference. */ - case PROT_LEVEL_1: - lvp->timer += lvp->ts; - if (lvp->timer > lvp->protLimitTime[PROT_LIMIT_TIME_0]) { - *spdRef *= PROT_POW_DN1_PCT; - } - break; - - /* level 0: take no protection action. */ - case PROT_LEVEL_0: - break; - - default: - break; - } - return; -} - -/** - * @brief Over dc-link voltage protection recovery. - * @param ovp Over dc-link voltage protection handle. - * @param motorErrStatus Motor error status. - * @param udc DC-link voltage (V). - * @retval None. - */ -void OVP_Recy(OVP_Handle *ovp, MotorErrStatusReg *motorErrStatus, float udc) -{ - MCS_ASSERT_PARAM(ovp != NULL); - /* If not under error state, just return without any operation. */ - if (!motorErrStatus->Bit.overVoltErr) { - return; - } - - /* According to protection level, take corresponding recovery action. */ - switch (ovp->protLevel) { - /* level 4 */ - case PROT_LEVEL_4: - if (udc < ovp->protValThr[PROT_VAL_THRESHOLD_3] - ovp->recyDelta) { - ovp->recyCnt++; - if (ovp->recyCnt > ovp->recyCntLimit) { - ovp->protLevel = PROT_LEVEL_3; - ovp->recyCnt = 0; - } - } - break; - - /* level 3 */ - case PROT_LEVEL_3: - /* If the dc-link voltage is less than threshold 2, level-2 protection is restored. */ - if (udc < ovp->protValThr[PROT_VAL_THRESHOLD_2] - ovp->recyDelta) { - ovp->recyCnt++; - if (ovp->recyCnt > ovp->recyCntLimit) { - ovp->protLevel = PROT_LEVEL_2; - ovp->recyCnt = 0; - } - } - break; - - /* level 2 */ - case PROT_LEVEL_2: - /* If the dc-link voltage is less than threshold 1, level-1 protection is restored. */ - if (udc < ovp->protValThr[PROT_VAL_THRESHOLD_1] - ovp->recyDelta) { - ovp->recyCnt++; - if (ovp->recyCnt > ovp->recyCntLimit) { - ovp->protLevel = PROT_LEVEL_1; - ovp->recyCnt = 0; - } - } - break; - - /* level 1 */ - case PROT_LEVEL_1: - /* If the dc-link voltage is less than threshold 0, level-0 protection is restored. */ - if (udc < ovp->protValThr[PROT_VAL_THRESHOLD_0] - ovp->recyDelta) { - ovp->recyCnt++; - if (ovp->recyCnt > ovp->recyCntLimit) { - ovp->protLevel = PROT_LEVEL_0; - ovp->recyCnt = 0; - } - } - break; - /* level 0 Fault-free state. */ - case PROT_LEVEL_0: - motorErrStatus->Bit.overVoltErr = 0; - break; - - default: - break; - } - return; -} - -/** - * @brief Lower dc-link voltage protection recovery. - * @param lvp Lower dc-link voltage protection handle. - * @param motorErrStatus Motor error status. - * @param udc DC-link voltage (V). - * @retval None. - */ -void LVP_Recy(LVP_Handle *lvp, MotorErrStatusReg *motorErrStatus, float udc) -{ - MCS_ASSERT_PARAM(lvp != NULL); - /* If not under error state, just return without any operation. */ - if (!motorErrStatus->Bit.lowerVoltErr) { - return; - } - - /* According to protection level, take corresponding recovery action. */ - switch (lvp->protLevel) { - /* level 4 */ - case PROT_LEVEL_4: - /* If the dc-link voltage is greater than threshold 3, level-3 protection is restored. */ - if (udc > lvp->protValThr[PROT_VAL_THRESHOLD_3] + lvp->recyDelta) { - lvp->recyCnt++; - if (lvp->recyCnt > lvp->recyCntLimit) { - lvp->protLevel = PROT_LEVEL_3; - lvp->recyCnt = 0; - } - } - break; - - /* level 3 */ - case PROT_LEVEL_3: - /* If the dc-link voltage is greater than threshold 2, level-2 protection is restored. */ - if (udc > lvp->protValThr[PROT_VAL_THRESHOLD_2] + lvp->recyDelta) { - lvp->recyCnt++; - if (lvp->recyCnt > lvp->recyCntLimit) { - lvp->protLevel = PROT_LEVEL_2; - lvp->recyCnt = 0; - } - } - break; - - /* level 2 */ - case PROT_LEVEL_2: - /* If the dc-link voltage is greater than threshold 1, level-1 protection is restored. */ - if (udc > lvp->protValThr[PROT_VAL_THRESHOLD_1] + lvp->recyDelta) { - lvp->recyCnt++; - if (lvp->recyCnt > lvp->recyCntLimit) { - lvp->protLevel = PROT_LEVEL_1; - lvp->recyCnt = 0; - } - } - break; - - /* level 1 */ - case PROT_LEVEL_1: - /* If the dc-link voltage is greater than threshold 0, level-0 protection is restored. */ - if (udc > lvp->protValThr[PROT_VAL_THRESHOLD_0] + lvp->recyDelta) { - lvp->recyCnt++; - if (lvp->recyCnt > lvp->recyCntLimit) { - lvp->protLevel = PROT_LEVEL_0; - lvp->recyCnt = 0; - } - } - break; - /* level 0 Fault-free state. */ - case PROT_LEVEL_0: - motorErrStatus->Bit.lowerVoltErr = 0; - break; - - default: - break; - } - return; -} - -/** - * @brief Over dc-link voltage protection error status clear. - * @param ovp Over voltage protection handle. - * @retval None. - */ -void OVP_Clear(OVP_Handle *ovp) -{ - MCS_ASSERT_PARAM(ovp != NULL); - /* Clear the history value. */ - ovp->protCnt = 0; - ovp->protLevel = PROT_LEVEL_0; - ovp->recyCnt = 0; - ovp->timer = 0.0f; -} - -/** - * @brief Lower dc-link voltage protection error status clear. - * @param lvp Lower voltage protection handle. - * @retval None. - */ -void LVP_Clear(LVP_Handle *lvp) -{ - MCS_ASSERT_PARAM(lvp != NULL); - /* Clear the history value. */ - lvp->protCnt = 0; - lvp->protLevel = PROT_LEVEL_0; - lvp->recyCnt = 0; - lvp->timer = 0.0f; -} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/mcs_dc_volt_prot.h b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/mcs_dc_volt_prot.h deleted file mode 100644 index 0435156a..00000000 --- a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/mcs_dc_volt_prot.h +++ /dev/null @@ -1,69 +0,0 @@ -/** - * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_dc_volt_prot.h - * @author MCU Algorithm Team - * @brief This file contains dc-link voltage protection data struct and api declaration. - */ - -/* Define to prevent recursive inclusion ------------------------------------------------------- */ -#ifndef McuMagicTag_MCS_DC_VOLT_PROT_H -#define McuMagicTag_MCS_DC_VOLT_PROT_H - -/* Includes ------------------------------------------------------------------------------------ */ -#include "mcs_prot_cmm.h" -#include "apt_ip.h" - -typedef struct { - unsigned int protCnt; - unsigned int recyCnt; - unsigned int protCntLimit; - unsigned int recyCntLimit; - float protValThr[PROT_VAL_THRESHOLD_NUMS]; /* from low voltage level to high. */ - float protLimitTime[PROT_LIMIT_TIME_NUMS]; /* from level 3 to level 1. */ - float recyDelta; - float timer; - float ts; - PROT_Level protLevel; -} OVP_Handle; - -typedef struct { - unsigned int protCnt; - unsigned int recyCnt; - unsigned int protCntLimit; - unsigned int recyCntLimit; - float protValThr[PROT_VAL_THRESHOLD_NUMS]; /* from high voltage level to low. */ - float protLimitTime[PROT_LIMIT_TIME_NUMS]; /* from level 3 to level 1. */ - float recyDelta; - float timer; - float ts; - PROT_Level protLevel; -} LVP_Handle; - -void OVP_Init(OVP_Handle *ovp, float ts); -void OVP_Det(OVP_Handle *ovp, MotorErrStatusReg *motorErrStatus, float udc); -void OVP_Exec(OVP_Handle *ovp, float *duty, APT_RegStruct **aptAddr); -void OVP_Recy(OVP_Handle *ovp, MotorErrStatusReg *motorErrStatus, float udc); -void OVP_Clear(OVP_Handle *ovp); - -void LVP_Init(LVP_Handle *lvp, float ts); -void LVP_Det(LVP_Handle *lvp, MotorErrStatusReg *motorErrStatus, float udc); -void LVP_Exec(LVP_Handle *lvp, float *spdRef, APT_RegStruct **aptAddr); -void LVP_Recy(LVP_Handle *lvp, MotorErrStatusReg *motorErrStatus, float udc); -void LVP_Clear(LVP_Handle *lvp); - -#endif diff --git a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/mcs_motor_stalling.c b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/mcs_motor_stalling.c deleted file mode 100644 index 9dee4d2d..00000000 --- a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/mcs_motor_stalling.c +++ /dev/null @@ -1,112 +0,0 @@ -/** - * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_motor_stalling.c - * @author MCU Algorithm Team - * @brief This file contains motor stalling protection data struct and api definition. - */ - -/* Includes ------------------------------------------------------------------------------------ */ -#include "mcs_motor_stalling.h" -#include "mcs_prot_user_config.h" -#include "mcs_math.h" -#include "mcs_math_const.h" -#include "mcs_assert.h" - -/** - * @brief Initilization motor stalling protection function. - * @param stall Motor stalling handle. - * @param ts Ctrl period (s). - * @param currLimit The current amplitude that triggers fault. (A). - * @param spdLimit The speed amplitude that triggers fault. (Hz). - * @param timeLimit The threshold time that current amplitude over the limit (s). - * @retval None. - */ -void STP_Init(STP_Handle *stall, float ts, float currLimit, float spdLimit, float timeLimit) -{ - MCS_ASSERT_PARAM(stall != NULL); - MCS_ASSERT_PARAM(ts > 0.0f); - MCS_ASSERT_PARAM(currLimit > 0.0f); - MCS_ASSERT_PARAM(spdLimit > 0.0f); - MCS_ASSERT_PARAM(timeLimit > 0.0f); - /* Configuring parameters for stalling detection. */ - stall->ts = ts; - /* Current threshold and speed threshold for stalling fault. */ - stall->currAmpLimit = currLimit; - stall->spdLimit = spdLimit; - stall->timeLimit = timeLimit; - stall->timer = 0.0f; -} - -/** - * @brief Motor stalling detection. - * @param stall Motor stalling handle. - * @param motorErrStatus Motor error status. - * @param spd Speed feedback (Hz). - * @param idq Dq-axis current feedback (A). - * @retval None. - */ -void STP_Det_ByCurrSpd(STP_Handle *stall, MotorErrStatusReg *motorErrStatus, float spd, DqAxis idq) -{ - MCS_ASSERT_PARAM(stall != NULL); - MCS_ASSERT_PARAM(motorErrStatus != NULL); - MCS_ASSERT_PARAM(Abs(spd) >= 0.0f); - /* Calculate current amplitude. */ - float currAmp = Sqrt(idq.d * idq.d + idq.q * idq.q); - float spdAbs = Abs(spd); - /* Check if value goes over threshold for continuous cycles. */ - if (currAmp < stall->currAmpLimit || spdAbs > stall->spdLimit) { - stall->timer = 0.0f; - return; - } - /* Time accumulation. */ - if (stall->timer < stall->timeLimit) { - stall->timer += stall->ts; - return; - } - motorErrStatus->Bit.motorStalling = 1; -} - -/** - * @brief Motor stalling protection execution. - * @param motorErrStatus Motor error status. - * @param aptAddr Three-phase APT address pointer. - * @retval None. - */ -void STP_Exec(MotorErrStatusReg *motorErrStatus, APT_RegStruct **aptAddr) -{ - MCS_ASSERT_PARAM(motorErrStatus != NULL); - MCS_ASSERT_PARAM(aptAddr != NULL); - if (motorErrStatus->Bit.motorStalling == 0) { - return; - } - - /* Disable three-phase pwm output. */ - ProtSpo_Exec(aptAddr); - return; -} - -/** - * @brief Motor stalling protection error status clear. - * @param stall Motor stalling handle. - * @retval None. - */ -void STP_Clear(STP_Handle *stall) -{ - MCS_ASSERT_PARAM(stall != NULL); - stall->timer = 0.0f; -} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/mcs_motor_stalling.h b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/mcs_motor_stalling.h deleted file mode 100644 index a1d1e4be..00000000 --- a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/mcs_motor_stalling.h +++ /dev/null @@ -1,45 +0,0 @@ -/** - * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_motor_stalling.h - * @author MCU Algorithm Team - * @brief This file contains motor stalling protection data struct and api declaration. - */ - -/* Define to prevent recursive inclusion ------------------------------------- */ -#ifndef McuMagicTag_MCS_MOTOR_STALLING_PROT_H -#define McuMagicTag_MCS_MOTOR_STALLING_PROT_H - -/* Includes ------------------------------------------------------------------------------------ */ -#include "mcs_prot_cmm.h" -#include "apt_ip.h" -#include "mcs_typedef.h" - -typedef struct { - float currAmpLimit; /**< Feedback current higher than this value triggers fault. (A). */ - float spdLimit; /**< Feedback speed lower than this value triggers fault (Hz). */ - float timeLimit; /**< The threshold time that current and speed feedback over ranges (s). */ - float timer; /**< Timer to get speed and current over range time. */ - float ts; /**< Ctrl period (s). */ -} STP_Handle; - -void STP_Init(STP_Handle *stall, float ts, float currLimit, float spdLimit, float timeLimit); -void STP_Det_ByCurrSpd(STP_Handle *stall, MotorErrStatusReg *motorErrStatus, float spd, DqAxis idq); -void STP_Exec(MotorErrStatusReg *motorErrStatus, APT_RegStruct **aptAddr); -void STP_Clear(STP_Handle *stall); - -#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/mcs_temp_prot.c b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/mcs_temp_prot.c deleted file mode 100644 index 184ba4a3..00000000 --- a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/mcs_temp_prot.c +++ /dev/null @@ -1,262 +0,0 @@ -/** - * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_temp_prot.c - * @author MCU Algorithm Team - * @brief This file contains over temperature protection api definition. - */ - -/* Includes ------------------------------------------------------------------------------------ */ -#include "mcs_temp_prot.h" -#include "mcs_prot_user_config.h" -#include "mcs_assert.h" - -/** - * @brief Initilization over temperation protection function. - * @param otp Over temperature protection handle. - * @param ts Ctrl period. - * @retval None. - */ -void OTP_Init(OTP_Handle *otp, float ts) -{ - MCS_ASSERT_PARAM(otp != NULL); - MCS_ASSERT_PARAM(ts > 0.0f); - otp->ts = ts; - otp->protCntLimit = PROT_CNT_LIMIT; - otp->recyCntLimit = RECY_CNT_LIMIT; - /* Configuring the temperature protection threshold. */ - otp->protValThr[PROT_VAL_THRESHOLD_0] = PROT_OVER_IPM_TEMP_POW_DN1; - otp->protValThr[PROT_VAL_THRESHOLD_1] = PROT_OVER_IPM_TEMP_POW_DN2; - otp->protValThr[PROT_VAL_THRESHOLD_2] = PROT_OVER_IPM_TEMP_POW_DN3; - otp->protValThr[PROT_VAL_THRESHOLD_3] = PROT_OVER_IPM_TEMP_POW_OFF; - /* Configuring the protection limiting time. */ - otp->protLimitTime[PROT_LIMIT_TIME_0] = PROT_OVER_TEMP_LIMIT1_TIME_SEC; - otp->protLimitTime[PROT_LIMIT_TIME_1] = PROT_OVER_TEMP_LIMIT2_TIME_SEC; - otp->protLimitTime[PROT_LIMIT_TIME_2] = PROT_OVER_TEMP_LIMIT3_TIME_SEC; - otp->recyDelta = PROT_OVER_IPM_TEMP_RECY_DELTA; - OTP_Clear(otp); -} - -/** - * @brief Over temperatre protection detection. - * @param otp Over temperature protection handle. - * @param tempErrBit Temperature error status bit. - * @param idq DQ-axis feedback currents. - * @retval None. - */ -void OTP_Det(OTP_Handle *otp, MotorErrStatusReg *motorErrStatus, PROT_ErrBit protBit, float temp) -{ - MCS_ASSERT_PARAM(otp != NULL); - MCS_ASSERT_PARAM(motorErrStatus != NULL); - MCS_ASSERT_PARAM(temp > 0.0f); - /* Check if value goes over threshold for continuous cycles. */ - if (temp < otp->protValThr[PROT_VAL_THRESHOLD_0]) { - otp->protCnt = 0; - ClearBit(&motorErrStatus->all, protBit); - return; - } - - if (otp->protCnt < otp->protCntLimit) { - otp->protCnt++; - return; - } - - /* Check which protection level should be activated. */ - /* Once enter error status, error status can only be changed by recover api. */ - /* protect level: level 4. */ - if (temp >= otp->protValThr[PROT_VAL_THRESHOLD_3] && otp->protLevel < PROT_LEVEL_4) { - otp->protLevel = PROT_LEVEL_4; - SetBit(&motorErrStatus->all, protBit); - otp->protCnt = 0; - return; - } - - /* Protect level: level 3. */ - if (temp >= otp->protValThr[PROT_VAL_THRESHOLD_2] && otp->protLevel < PROT_LEVEL_3) { - otp->protLevel = PROT_LEVEL_3; - SetBit(&motorErrStatus->all, protBit); - otp->protCnt = 0; - otp->timer = 0.0f; - return; - } - - /* Protect level: level 2. */ - if (temp >= otp->protValThr[PROT_VAL_THRESHOLD_1] && otp->protLevel < PROT_LEVEL_2) { - otp->protLevel = PROT_LEVEL_2; - SetBit(&motorErrStatus->all, protBit); - otp->protCnt = 0; - otp->timer = 0.0f; - return; - } - - /* Protect level: level 1. */ - if (temp >= otp->protValThr[PROT_VAL_THRESHOLD_0] && otp->protLevel < PROT_LEVEL_1) { - otp->protLevel = PROT_LEVEL_1; - SetBit(&motorErrStatus->all, protBit); - otp->protCnt = 0; - otp->timer = 0.0f; - return; - } -} - -/** - * @brief Over temperature protection execution. - * @param otp Over temperature protection handle. - * @param spdRef Speed reference (Hz). - * @param aptAddr Three-phase APT address pointer. - * @retval None. - */ -void OTP_Exec(OTP_Handle *otp, float *spdRef, APT_RegStruct **aptAddr) -{ - MCS_ASSERT_PARAM(otp != NULL); - MCS_ASSERT_PARAM(spdRef != NULL); - MCS_ASSERT_PARAM(aptAddr != NULL); - /* According to protect level, take corresponding action. */ - switch (otp->protLevel) { - /* level 4: disable all PWM output. */ - case PROT_LEVEL_4: - /* Disable three-phase pwm output. */ - ProtSpo_Exec(aptAddr); - *spdRef *= 0.0f; - break; - - /* level 3: derate speed reference. */ - case PROT_LEVEL_3: - *spdRef *= PROT_POW_DN2_PCT; - otp->timer += otp->ts; - if (otp->timer > otp->protLimitTime[PROT_LIMIT_TIME_2]) { - *spdRef *= PROT_POW_DN3_PCT; - } - break; - - /* level 2: derate speed reference. */ - case PROT_LEVEL_2: - /* Reducte motor speed to level 2 */ - *spdRef *= PROT_POW_DN1_PCT; - otp->timer += otp->ts; - if (otp->timer > otp->protLimitTime[PROT_LIMIT_TIME_1]) { - *spdRef *= PROT_POW_DN2_PCT; - } - break; - - /* level 0: take no protection action. */ - case PROT_LEVEL_0: - break; - - case PROT_LEVEL_1: - /* Reducte motor speed to level 0 */ - otp->timer += otp->ts; - if (otp->timer > otp->protLimitTime[PROT_LIMIT_TIME_0]) { - /* level 1: derate speed reference. */ - *spdRef *= PROT_POW_DN1_PCT; - } - break; - - default: - break; - } - return; -} - -/** - * @brief Over temperature protection recovery. - * @param otp Over temperature protection handle. - * @param tempErrBit Temperature error status bit. - * @param temp Temperature (celsius). - * @retval None. - */ -void OTP_Recy(OTP_Handle *otp, MotorErrStatusReg *motorErrStatus, PROT_ErrBit protBit, float temp) -{ - MCS_ASSERT_PARAM(otp != NULL); - MCS_ASSERT_PARAM(motorErrStatus != NULL); - MCS_ASSERT_PARAM(temp > 0.0f); - /* If not under error state, just return without any operation. */ - if (otp->protLevel == PROT_LEVEL_0) { - motorErrStatus->all &= (~(1 >> protBit)); - return; - } - - /* According to protection level, take corresponding recovery action. */ - switch (otp->protLevel) { - /* level 4 */ - case PROT_LEVEL_4: - /* If the temperature is less than threshold 3, level-3 protection is restored. */ - if (temp < otp->protValThr[PROT_VAL_THRESHOLD_3] - otp->recyDelta) { - otp->recyCnt++; - if (otp->recyCnt > otp->recyCntLimit) { - otp->protLevel = PROT_LEVEL_3; - otp->recyCnt = 0; - } - } - break; - - /* level 3 */ - case PROT_LEVEL_3: - /* If the temperature is less than threshold 2, level-2 protection is restored. */ - if (temp < otp->protValThr[PROT_VAL_THRESHOLD_2] - otp->recyDelta) { - otp->recyCnt++; - if (otp->recyCnt > otp->recyCntLimit) { - otp->protLevel = PROT_LEVEL_2; - otp->recyCnt = 0; - } - } - break; - - /* level 2 */ - case PROT_LEVEL_2: - /* If the temperature is less than threshold 1, level-1 protection is restored. */ - if (temp < otp->protValThr[PROT_VAL_THRESHOLD_1] - otp->recyDelta) { - otp->recyCnt++; - if (otp->recyCnt > otp->recyCntLimit) { - otp->protLevel = PROT_LEVEL_1; - otp->recyCnt = 0; - } - } - break; - - /* level 1 */ - case PROT_LEVEL_1: - /* If the temperature is less than threshold 0, level-0 protection is restored. */ - if (temp < otp->protValThr[PROT_VAL_THRESHOLD_0] - otp->recyDelta) { - otp->recyCnt++; - if (otp->recyCnt > otp->recyCntLimit) { - otp->protLevel = PROT_LEVEL_0; - otp->recyCnt = 0; - } - } - break; - - default: - break; - } - return; -} - -/** - * @brief Over temperature protection error status clear. - * @param otp Over temperature protection handle. - * @retval None. - */ -void OTP_Clear(OTP_Handle *otp) -{ - MCS_ASSERT_PARAM(otp != NULL); - /* Clear the history value. */ - otp->protCnt = 0; - otp->protLevel = PROT_LEVEL_0; - otp->recyCnt = 0; - otp->timer = 0.0f; -} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/mcs_temp_prot.h b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/mcs_temp_prot.h deleted file mode 100644 index 2b9d99a7..00000000 --- a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/protection/mcs_temp_prot.h +++ /dev/null @@ -1,49 +0,0 @@ -/** - * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_temp_prot.h - * @author MCU Algorithm Team - * @brief his file contains over temperature protection api declaration. - */ - -/* Define to prevent recursive inclusion ------------------------------------- */ -#ifndef McuMagicTag_MCS_TEMP_PROT_H -#define McuMagicTag_MCS_TEMP_PROT_H - -/* Includes ------------------------------------------------------------------------------------ */ -#include "mcs_prot_cmm.h" -#include "apt_ip.h" - -typedef struct { - unsigned int protCnt; - unsigned int recyCnt; - unsigned int protCntLimit; - unsigned int recyCntLimit; - float protValThr[PROT_VAL_THRESHOLD_NUMS]; /* from low current level to high. */ - float protLimitTime[PROT_LIMIT_TIME_NUMS]; /* from level 3 to level 1. */ - float recyDelta; - float ts; - float timer; - PROT_Level protLevel; -} OTP_Handle; - -void OTP_Init(OTP_Handle *otp, float ts); -void OTP_Det(OTP_Handle *otp, MotorErrStatusReg *motorErrStatus, PROT_ErrBit protBit, float temp); -void OTP_Exec(OTP_Handle *otp, float *spdRef, APT_RegStruct **aptAddr); -void OTP_Recy(OTP_Handle *otp, MotorErrStatusReg *motorErrStatus, PROT_ErrBit protBit, float temp); -void OTP_Clear(OTP_Handle *otp); -#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/readme.md b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/readme.md deleted file mode 100644 index 592524f0..00000000 --- a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/readme.md +++ /dev/null @@ -1,12 +0,0 @@ -# pmsm_encode_qdm_2shunt_foc - -**【功能描述】** -+ 基于ECMCU105H/ECBMCU201MPC单板的单电机双电阻采样的有感Foc应用 - -**【环境要求】** -+ 所有单板电源改制为演示用的24V低压,电机选用42JSF630AS-1000 -+ 电机ABZ编码器线序分别对应功率板上ABZ通道接口 -+ 电机(绿蓝黄)相线分别对应功率板上U/V/W通道接口 - -**【IDE配置方法】** -+ chipConfig中的Sample栏目里面选中pmsm encode qdm 2shunt foc示例,然后点击生成代码即可 diff --git a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/src/mcs_carrier.c b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/src/mcs_carrier.c deleted file mode 100644 index 4f8c9d37..00000000 --- a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/src/mcs_carrier.c +++ /dev/null @@ -1,136 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_carrier.c - * @author MCU Algorithm Team - * @brief This file provides carrier interrupt application for motor control. - */ - -#include "mcs_carrier.h" -#include "mcs_math.h" -#include "typedefs.h" -#include "mcs_assert.h" -#include "mcs_user_config.h" -#include "mcs_ctlmode_config.h" - -/** - * @brief 同步旋转坐标系角度。 - * @param mtrCtrl The motor control handle. - * @retval None. - */ -static void MCS_SyncCoorAngle(MTRCTRL_Handle *mtrCtrl) -{ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - /* Synchronous rotation coordinate system angle. */ - switch (mtrCtrl->stateMachine) { - case FSM_STARTUP: - /* 当前斜坡角度为0。 */ - if (mtrCtrl->startup.stage == STARTUP_STAGE_CURR) { - mtrCtrl->axisAngle = 0; - } else if (mtrCtrl->startup.stage == STARTUP_STAGE_SPD) { /* 中频控制相位角自加。 */ - mtrCtrl->axisAngle = IF_CurrAngleCalc(&mtrCtrl->ifCtrl, mtrCtrl->spdRefHz); - } - break; - - case FSM_RUN: - mtrCtrl->axisAngle = mtrCtrl->encAxisAngle; - break; - - default: - mtrCtrl->axisAngle = 0; - break; - } -} - -/** - * @brief 单电阻器和双电阻器的PWM波形设置和采样点设置。 - * @param mtrCtrl The motor control handle. - * @retval None. - */ -static void MCS_PwmAdcSet(MTRCTRL_Handle *mtrCtrl) -{ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - SampleMode sampleMode = mtrCtrl->sampleMode; - /* 根据采样模式设置占空比. */ - if (sampleMode == DUAL_RESISTORS) { - //三相上开关PWM波的占空比为在两相静止坐标系(albe)中计算。 - SVPWM_Exec(&mtrCtrl->sv, &mtrCtrl->vabRef, &mtrCtrl->dutyUvw); - mtrCtrl->setPwmDutyCb(&mtrCtrl->dutyUvw, &mtrCtrl->dutyUvw); - } else if (sampleMode == SINGLE_RESISTOR) { - R1SVPWM_Exec(&mtrCtrl->r1Sv, &mtrCtrl->vabRef, &mtrCtrl->dutyUvwLeft, &mtrCtrl->dutyUvwRight); - mtrCtrl->setPwmDutyCb(&mtrCtrl->dutyUvwLeft, &mtrCtrl->dutyUvwRight); - /* ADC采样点位置需要根据单个电阻器的相移进行设置。*/ - mtrCtrl->setADCTriggerTimeCb(mtrCtrl->r1Sv.samplePoint[0] * mtrCtrl->aptMaxcntCmp, \ - mtrCtrl->r1Sv.samplePoint[1] * mtrCtrl->aptMaxcntCmp); - } -} - -/** - * @brief 载波中断功能。 - * @param mtrCtrl The motor control handle. - * @retval None. - * 电流环 - */ -void MCS_CarrierProcess(MTRCTRL_Handle *mtrCtrl) -{ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - UvwAxis *currUvw = &mtrCtrl->currUvw; - AlbeAxis *currAlbe = &mtrCtrl->iabFbk; - AlbeAxis *vab = &mtrCtrl->vabRef; - SampleMode sampleMode = mtrCtrl->sampleMode; - /* 采样模式验证 */ - if (sampleMode > SINGLE_RESISTOR || mtrCtrl->setADCTriggerTimeCb == NULL) { - return; - } - /* 参数验证 */ - if (mtrCtrl->setPwmDutyCb == NULL || mtrCtrl->readCurrUvwCb == NULL) { - return; - } - /* 读取三相电流值。 */ - mtrCtrl->readCurrUvwCb(currUvw); - mtrCtrl->getEncAngSpd(&mtrCtrl->encSpeed, &mtrCtrl->encAxisAngle);//读取反馈速度与角度 - /* 同步角度 */ - MCS_SyncCoorAngle(mtrCtrl); - - /* 克拉克变化 UVW->A、B */ - ClarkeCalc(currUvw, currAlbe); - /* 帕克变化 A、B +角度->q、d */ - ParkCalc(currAlbe, mtrCtrl->axisAngle, &mtrCtrl->idqFbk); - - /* 状态机 */ - switch (mtrCtrl->stateMachine) { - case FSM_STARTUP: - case FSM_RUN: - CURRCTRL_Exec(&mtrCtrl->currCtrl, &mtrCtrl->vdqRef, mtrCtrl->smo.spdEst, 0); - //帕克逆变化 ,将d,q轴电流+角度逆变换为α,β。 - InvParkCalc(&mtrCtrl->vdqRef, mtrCtrl->axisAngle, vab); - //单电阻器和双电阻器的PWM波形设置和采样点设置。 - MCS_PwmAdcSet(mtrCtrl); - break; - - case FSM_CAP_CHARGE: - case FSM_CLEAR: - case FSM_IDLE: - break; - - default: - vab->alpha = 0.0f; - vab->beta = 0.0f; - MCS_PwmAdcSet(mtrCtrl); - break; - } -} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/src/mcs_inc_enc.c b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/src/mcs_inc_enc.c deleted file mode 100644 index 9769c9b5..00000000 --- a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/src/mcs_inc_enc.c +++ /dev/null @@ -1,236 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_incremental_enc.c - * @author MCU Algorithm Team - * @brief This file provides function of QDM module and encoder speed angle calculation. - */ -#include "mcs_inc_enc.h" -#include "crg.h" -#include "debug.h" -#include "mcs_math_const.h" - -#define FILTER_TIME_A 100 /* Unit: clock cycles */ -#define FILTER_TIME_B 100 /* Unit: clock cycles */ -#define FILTER_TIME_Z 100 /* Unit: clock cycles */ -#define ENC_SPEED_BUF_SIZE 10 -#define ENC_HALF_PPR 5000u -#define ENC_MAX_POS 65536u -/** - * @brief QMD Initialization. - * @param qdmInit MCS_QdmInitStru. - * @retval None. - */ -void MCS_QdmInit(MCS_QdmInitStru *qdmInit) -{ - QDM_RegStruct *qdm = qdmInit->qdmAddr; - - HAL_CRG_IpEnableSet(qdmInit->qdmAddr, CRG_ENABLE_VALUE); - - /* Internal counters are not affected by emulator pause CPU */ - qdm->QEMUMODE.BIT.emu_mode = QDM_EMULATION_MODE_RUN_FREE; - - /* Quadrature input decoding mode. Inputs are quadrature encoder A, B, Z phase signals. */ - qdm->QCTRL.BIT.qdu_mode = QDM_QUADRATURE_COUNT; - -/** - * This parameter is valid only when the quadrature count mode is - * used. In other modes, the frequency is multiplied by 2x. - */ - qdm->QCTRL.BIT.qdu_xclk = QDM_4X_RESOLUTION; - - /* xCLK-xDIRCount mode. */ - qdm->QPPUCTRL.BIT.pcnt_mode = QDM_PPU_COUNT_MODE_CLK_DIR; - - /* QDM position counter reset select: overflow reset (upflow, underflow) */ - qdm->QPPUCTRL.BIT.pcnt_rst_mode = QDM_POSITION_RESET_MAX_POS; - - /* QDM initialization position counter mode: disabled */ - qdm->QPPUCTRL.BIT.pcnt_idx_init_mode = QDM_POSITION_INIT_DO_NOTHING; - - /* QDM Index latch mode select: latch at the rising edge of index */ - qdm->QPPUCTRL.BIT.pcnt_idx_lock_mode = QDM_LOCK_RISING_INDEX; - - qdm->QPOSMAX = 0xFFFF; /* QDM Position Count Max */ - - /* ABZ filtering time */ - qdm->QDMAFT.BIT.qdma_ft_level = FILTER_TIME_A; - qdm->QDMBFT.BIT.qdmb_ft_level = FILTER_TIME_B; - qdm->QDMIFT.BIT.qdmi_ft_level = FILTER_TIME_Z; - -/** - * QDM Position count value. When ppu_en is disabled or counting stops after the emulator is - * connected, software can write QPOSCNT. - */ - qdm->QPOSCNT = 0; - - /* QDM Position processing unit PPU enable 1: PPU position counter starts counting */ - qdm->QCTRL.BIT.ppu_en = BASE_CFG_ENABLE; - - /* Enable z-pulse interrupt */ - if (qdmInit->zPlusesIrqFunc != NULL && qdmInit->zPlusesNvic->baseAddr != NULL) { - IRQ_SetPriority(qdmInit->zPlusesNvic->irqNum, qdmInit->zPlusesIrqPrio); - IRQ_Register(qdmInit->zPlusesNvic->irqNum, qdmInit->zPlusesIrqFunc, qdmInit->zPlusesNvic); - DCL_QDM_EnableInterrupt(qdm, QDM_INT_INDEX_EVNT_LATCH); - IRQ_EnableN(qdmInit->zPlusesNvic->irqNum); - } -} - -/** - * @brief Get the Encoder Cnt object. - * @param enc encoder handle. - * @param qdm QDM_RegStruct. - * @retval None. - */ -void MCS_GetEncoderCnt(EncoderHandle *enc, QDM_RegStruct *qdm) -{ - signed short tmpS16; - - enc->cntNow = qdm->QPOSCNT; - /* Get pulse conut in unit period */ - tmpS16 = (signed short)(enc->cntNow - enc->cntPre); - - enc->pulsePos += tmpS16; /* Total number of pulses since power-on. */ - enc->pulseAngle = (unsigned short)qdm->QPOSCNT; - - enc->cntPre = enc->cntNow; -} - -/** - * @brief Get the Elec Angle By Enc object. - * @param enc Encoder handle. - * @retval signed short elecAngle Angle in electronic, S16degree. - */ -void MCS_GetElecAngleByEnc(EncoderHandle *enc) -{ - signed int tmpS32; - - tmpS32 = enc->pulseAngle - enc->pulZCnt; - /* Align electric angle & enc angle */ - tmpS32 += enc->zShift; - /* Limit max value */ - if (tmpS32 > INT16_MAX) { - tmpS32 -= ENC_MAX_POS; - } - /* Limit min value */ - if (tmpS32 < -INT16_MAX) { - tmpS32 += ENC_MAX_POS; - } - while (tmpS32 >= enc->pulsePerElecRound) { - tmpS32 -= enc->pulsePerElecRound; - } - while (tmpS32 < 0) { - tmpS32 += enc->pulsePerElecRound; - } - float tempElecAngle = (signed short)(tmpS32 * enc->pulseToElecAngle); - /* Convert short to float angle type */ - enc->elecAngle = tempElecAngle * DIGITAL_TO_RAD; -} - -/** - * @brief Get the Elec Speed By Enc object. - * @param enc Encoder handle. - * @retval None. - */ -void MCS_GetElecSpeedByEnc(EncoderHandle *enc) -{ - signed int tmpS32; - signed short halfPPR = ENC_HALF_PPR; - unsigned short index = enc->speedBufIndex; - - enc->timeCnt++; - if (enc->timeCnt >= enc->timeNum) { - enc->timeCnt = 0; - } else { - return; - } - - /* Get pulse conut in unit period */ - tmpS32 = enc->pulsePos - enc->speedBuf[index]; - /* Limit max value */ - if (tmpS32 > halfPPR) { - tmpS32 -= ENC_MAX_POS; - } - /* Limit min value */ - if (tmpS32 < -halfPPR) { - tmpS32 += ENC_MAX_POS; - } - /* Convert unit pulse count to Hz */ - enc->elecSpeed = tmpS32 * enc->pulseToHzPu; - enc->speedBuf[index] = enc->pulsePos; - index++; - /* Store current speed for calcule the average */ - if (index >= enc->speedBufSize) { - enc->speedBufIndex = 0; - } else { - enc->speedBufIndex = index; - } -} - -/** - * @brief Clear the historical cache and time count. - * @param enc Encoder handle. - * @retval None. - */ -void MCS_EncoderClear(EncoderHandle *enc) -{ - enc->speedBufIndex = 0; - for (unsigned short i = 0; i < enc->speedBufSize; i++) { - enc->speedBuf[i] = enc->pulsePos; - } - - enc->timeCnt = 0; -} - -/** - * @brief Initialzer of encoder struct handle. - * @param enc Encoder handle. - * @param encParam Encoder and Motor parameter. - * @retval None. - */ -void MCS_EncoderInit(EncoderHandle *enc, MCS_EncInitStru *encParam) -{ - if (encParam->mtrPPMR == 0 || encParam->mtrNp == 0) { - return; - } - - enc->pulsePerMechRound = encParam->mtrPPMR; - enc->pulsePerElecRound = encParam->mtrPPMR / encParam->mtrNp; - enc->zShift = encParam->zShift; - /* Clear count value */ - enc->cntNow = 0; - enc->cntPre = 0; - - enc->pulsePos = 0; - enc->pulseAngle = 0; - enc->elecAngle = 0; - /* Initial speed buffer */ - enc->speedBufSize = ENC_SPEED_BUF_SIZE; /* Max = 10 */ - enc->speedBufIndex = 0; - for (unsigned short i = 0; i < enc->speedBufSize; i++) { - enc->speedBuf[i] = enc->pulsePos; - } - /* Clear time count value */ - enc->timeCnt = 0; - enc->timeNum = encParam->timeNum; - enc->elecSpeed = 0.0f; /* Hz */ - /* Convert unit pulse count to Hz */ - enc->pulseToHzPu = (float)(1.0f / - (enc->pulsePerElecRound * enc->timeNum * enc->speedBufSize * encParam->ctrlPeriod)); - /* Convert unit pulse count to electric angle */ - enc->pulseToElecAngle = (float)(65536.0f / enc->pulsePerElecRound); -} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/src/mcs_motor_process.c b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/src/mcs_motor_process.c deleted file mode 100644 index 6626f9a4..00000000 --- a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/src/mcs_motor_process.c +++ /dev/null @@ -1,1047 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_motor_process.c - * @author MCU Algorithm Team - * @brief This file provides motor application. - * @details Single FOC application based on the ECMCU105H/ECBMCU201MPC board - * 1) Motor model is 42JSF630AS-1000. - * 2) Select the pmsm encode qdm 2shunt foc example in the sample column - of chipConfig and click Generate Code. - * 3) It's power supply must be changed to 24V. - */ -#include "main.h" -#include "mcs_user_config.h" -#include "mcs_math.h" -#include "hmi_module.h" -#include "mcs_ctlmode_config.h" -#include "mcs_prot_user.h" -#include "mcs_prot_user_config.h" -#include "mcs_math_const.h" -#include "mcs_motor_process.h" -#include "mcs_chip_config.h" -#include "mcs_inc_enc.h" -#include -#include "debug.h" - - -/*------------------------------- Macro Definition -----------------------------------------------*/ -#define US_PER_MS 1000 -#define ANGLE_RANGE_ABS 65536 -#define ANGLE_360_F 65536.0f /* 0 - 65536 indicates 0 to 360. */ -#define APT_FULL_DUTY 1.0f -#define TEMP_3 3.0f -#define TEMP_15 15.0f -#define TEMP_30 30.0f -#define TEMP_45 45.0f -#define TEMP_60 60.0f -#define TEMP_RES_15 78.327f -#define TEMP_RES_30 36.776f -#define TEMP_RES_45 18.301f -#define TEMP_RES_60 9.607f -#define CNT_10 10 -#define CNT_5000 5000 -#define LEVEL_4 4 -#define MOTOR_START_DELAY 2 -#define ADC_READINIT_DELAY 1 -#define ADC_READINIT_TIMES 20 -#define ADC_TRIMVALUE_MIN 1800.0f -#define ADC_TRIMVALUE_MAX 2200.0f -#define IRQ_QDM0_PRIORITY 7 /* the QDM encoder IRQ priority, highest */ -#define ENC_TIMES_NUM 5 -/*------------------------------- Param Definition -----------------------------------------------*/ -/* Motor parameters. */ -/* Np, Rs, Ld, Lq, Psif, J, Nmax, Currmax, PPMR, zShift */ -static MOTOR_Param g_motorParam = MOTORPARAM_DEFAULTS; -static APT_RegStruct* g_apt[PHASE_MAX_NUM] = {APT_U, APT_V, APT_W}; - -extern __UINT16_TYPE__ last_raw_Angle; -/* QDM */ -typedef struct { - MCS_QdmHandle qdm; -} PeriphHandle; -static PeriphHandle g_periph = {.qdm = {.qdmAddr = QDMBASEADDR, - .zPulsesNvic = {.irqNum = QDMIRQNUM, - .baseAddr = QDMBASEADDR}}}; -/* Motor control handle */ -static MTRCTRL_Handle g_mc = {0}; -/* QDM control handle */ -static EncoderHandle g_enc = {0}; - - - - -/** - * @brief 获取编码器的角度和速度。 - * @param speed 速度指针。 - * @param angle 角度指针。 - * @retval 无。 - */ - -static void GetEncAngSpd(float* speed, float* angle) -{ - MCS_GetEncoderCnt(&g_enc, QDMNUM);// 获取编码器计数值 - // /* 计算电机电角度 -π ~ π */ - MCS_GetElecAngleByEnc(&g_enc); // 根据编码器数据计算电机电角度 - // /* 计算电机速度 */ - MCS_GetElecSpeedByEnc(&g_enc); // 根据编码器数据计算电机速度 - *speed = g_enc.elecSpeed; // 将计算得到的电机速度赋值给传入的速度指针 - *angle = g_enc.elecAngle; // 将计算得到的电机电角度赋值给传入的角度指针 - - - - -} - - -static void ISR_QdmzPulses(void* args) -{ - NvicHandle* qdmHandle = (NvicHandle*)args; // 将传入的参数转换为NvicHandle指针 - /* 清除QDM中断标志 */ - DCL_QDM_ClearInterrupt((QDM_RegStruct*)qdmHandle->baseAddr, QDM_INT_INDEX_EVNT_LATCH); // 清除QDM事件捕获中断标志 - IRQ_ClearN(qdmHandle->irqNum); // 清除中断请求 - - g_enc.pulZCnt = MCS_GetQdmPosCnt(qdmHandle->baseAddr); - g_mc.motorSpinPos++; // 增加电机旋转位置计数 - if (g_mc.encReady == 0) { - /* Z脉冲标志用于确定在初始启动期间IF预设位是否成功使用。 */ - g_mc.encReady = 1; // 如果编码器尚未准备好,设置编码器准备好标志 - } - -} - - - -/* Motor POS loop PID param. */ -static void POSCTRL_InitWrapper(POSCTRL_Handle *posHandle, float ts) -{ - /* Position loop param assignment. */ - PID_Param posPi = { - .kp = POS_KP, - .ki = POS_KI, - .kd = POS_KD, - .ns = POS_NS, - .lowerLim = POS_LOWERLIM, - .upperLim = POS_UPPERLIM, - }; - /* Position loop param init. */ - POSCTRL_Init(posHandle, &posPi, ts); -} - -/* Motor speed loop PI param. */ -static void SPDCTRL_InitWrapper(SPDCTRL_Handle *spdHandle, float ts) -{ - /* Speed loop param assignment. */ - PI_Param spdPi = { - .kp = SPD_KP, - .ki = SPD_KI, - .lowerLim = SPD_LOWERLIM, - .upperLim = SPD_UPPERLIM, - }; - /* Speed loop param init. */ - SPDCTRL_Init(spdHandle, &g_motorParam, spdPi, ts); -} - -/* Motor current Loop PI param. */ -static void CURRCTRL_InitWrapper(CURRCTRL_Handle *currHandle, DqAxis *idqRef, DqAxis *idqFbk, float ts) -{ - /* Axis-D current loop param assignment. */ - PI_Param dCurrPi = { - .kp = CURRDAXIS_KP, - .ki = CURRDAXIS_KI, - .lowerLim = CURR_LOWERLIM, - .upperLim = CURR_UPPERLIM, - }; - /* Axis-Q current loop param assignment. */ - PI_Param qCurrPi = { - .kp = CURRQAXIS_KP, - .ki = CURRQAXIS_KI, - .lowerLim = CURR_LOWERLIM, - .upperLim = CURR_UPPERLIM, - }; - /* Current loop param init. */ - CURRCTRL_Init(currHandle, &g_motorParam, idqRef, idqFbk, dCurrPi, qCurrPi, ts); -} - -/*------------------------------- Function Definition -----------------------------------------------*/ -/** - * @brief 系统标记的初始化器。 - * @param mtrCtrl Motor control struct handle. - * @retval None. - */ -static void TimerTickInit(MTRCTRL_Handle *mtrCtrl) -{ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - mtrCtrl->sysTickCnt = 0; - mtrCtrl->msTickNum = US_PER_MS / SYSTICK_PERIOD_US; - mtrCtrl->capChargeTickNum = (INV_CAP_CHARGE_MS * US_PER_MS / SYSTICK_PERIOD_US); -} - -/** - * @brief Init motor control task. - * @retval None. - */ -static void TSK_Init(void) -{ - g_mc.motorStateFlag = 0; - g_mc.uartHeartDetCnt = 0; - g_mc.uartTimeStamp = 0; - g_mc.stateMachine = FSM_IDLE; - g_mc.currCtrlPeriod = CTRL_CURR_PERIOD; /* 初始化电流控制器 */ - g_mc.aptMaxcntCmp = g_apt0.waveform.timerPeriod; - g_mc.sampleMode = DUAL_RESISTORS; - g_mc.obserType = FOC_OBSERVERTYPE_ENC; /* 启动foc观察模式*/ - g_mc.controlMode = FOC_CONTROLMODE_POS; /* 启动电机控制模式 */ - g_mc.adcCurrCofe = ADC_CURR_COFFI; - g_mc.spdAdjustMode = CUST_SPEED_ADJUST; - g_mc.uartConnectFlag = DISCONNECT; - g_mc.spdCmdHz = 35.0f; /* Motor initialization speed */ - - g_mc.adc0Compensate = ADC0COMPENSATE; /* u相电流初始化adc移位微调值 */ - g_mc.adc1Compensate = ADC1COMPENSATE; /* w相电流初始化adc移位微调值*/ - - IF_Init(&g_mc.ifCtrl, CTRL_IF_CURR_AMP_A, USER_CURR_SLOPE, CTRL_SYSTICK_PERIOD, CTRL_CURR_PERIOD);//IF初始化电流 - RMG_Init(&g_mc.spdRmg, CTRL_SYSTICK_PERIOD, USER_SPD_SLOPE); /* 初始速度斜率*/ - MtrParamInit(&g_mc.mtrParam, g_motorParam); //电机参数初始化 - - TimerTickInit(&g_mc); - SVPWM_Init(&g_mc.sv, INV_VOLTAGE_BUS * ONE_DIV_SQRT3); - R1SVPWM_Init(&g_mc.r1Sv, INV_VOLTAGE_BUS * ONE_DIV_SQRT3, SAMPLE_POINT_SHIFT, SAMPLE_WINDOW_DUTY); - //电机速度回路PI参数。 - SPDCTRL_InitWrapper(&g_mc.spdCtrl, CTRL_SYSTICK_PERIOD); - //电机位置回路PI参数。 - POSCTRL_InitWrapper(&g_mc.posCtrl, CTRL_SYSTICK_PERIOD * 5.0f); /* Position loop control period */ - //电机电流回路PI参数。 - CURRCTRL_InitWrapper(&g_mc.currCtrl, &g_mc.idqRef, &g_mc.idqFbk, CTRL_CURR_PERIOD); - - MotorProt_Init(&g_mc.prot); /* 初始化保护状态通用 */ - OCP_Init(&g_mc.prot.ocp, CTRL_CURR_PERIOD); // 启动过电流保护功能。 - OVP_Init(&g_mc.prot.ovp, CTRL_SYSTICK_PERIOD); //启动直流链路电压保护功能。 - LVP_Init(&g_mc.prot.lvp, CTRL_SYSTICK_PERIOD); //启动直流链路电压保护功能。 - OTP_Init(&g_mc.prot.otp, CTRL_SYSTICK_PERIOD); //初始化超温保护功能。 - STP_Init(&g_mc.prot.stall, CTRL_SYSTICK_PERIOD, PROT_STALLING_CURR_AMP_LIMIT, - PROT_STALLING_SPD_LIMIT, PROT_STALLING_TIME_LIMIT);//启动电机失速保护功能。 -} - -/** - * @brief Clear historical values of all controller before start-up. - * @param mtrCtrl The motor control handle. - * @retval None. - */ -static void ClearBeforeStartup(MTRCTRL_Handle *mtrCtrl) -{ - /* Verify Parameters */ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - /* The initial angle is 0. */ - mtrCtrl->axisAngle = 0.0f; - mtrCtrl->spdRefHz = 0.0f; - mtrCtrl->motorSpinPos = 0; - /* The initial dq-axis reference current is 0. */ - mtrCtrl->idqRef.d = 0.0f; - mtrCtrl->idqRef.q = 0.0f; - - mtrCtrl->vdqRef.d = 0.0f; - mtrCtrl->vdqRef.q = 0.0f; - /* Clear Duty Cycle Value. The initial duty cycle is 0.5. */ - mtrCtrl->dutyUvwLeft.u = 0.5f; - mtrCtrl->dutyUvwLeft.v = 0.5f; - mtrCtrl->dutyUvwLeft.w = 0.5f; - mtrCtrl->dutyUvwRight.u = 0.5f; - mtrCtrl->dutyUvwRight.v = 0.5f; - mtrCtrl->dutyUvwRight.w = 0.5f; - - mtrCtrl->prot.motorErrStatus.all = 0x00; - - RMG_Clear(&mtrCtrl->spdRmg); /* Clear the history value of speed slope control */ - CURRCTRL_Clear(&mtrCtrl->currCtrl); - IF_Clear(&mtrCtrl->ifCtrl); - SPDCTRL_Clear(&mtrCtrl->spdCtrl); - STARTUP_Clear(&mtrCtrl->startup); - R1SVPWM_Clear(&mtrCtrl->r1Sv); - POSCTRL_Clear(&mtrCtrl->posCtrl); - MCS_EncoderClear(&g_enc); - - OTP_Clear(&mtrCtrl->prot.otp); - OCP_Clear(&mtrCtrl->prot.ocp); - OVP_Clear(&mtrCtrl->prot.ovp); - LVP_Clear(&mtrCtrl->prot.lvp); -} - -/** - * @brief To set the comparison value of the IGBT single-resistance ADC sampling trigger position. - * @param aptx The APT register struct handle. - * @param cntCmpA A Count compare reference of time-base counter. - * @param cntCmpB B Count compare reference of time-base counter. - * @param maxCntCmp Maximum Comparison Value - * @retval None. - */ -static void MCS_SetAdcCompareR1(APT_RegStruct *aptx, unsigned short cntCmpA, - unsigned short cntCmpB, unsigned short maxCntCmp) -{ - unsigned short tmp; - /* Sets the A Count compare reference of time-base counter. */ - tmp = (unsigned short)Clamp((float)(cntCmpA), (float)(maxCntCmp - 1), 1.0f); - DCL_APT_SetCounterCompare(aptx, APT_COMPARE_REFERENCE_A, tmp); - /* Sets the B Count compare reference of time-base counter. */ - tmp = (unsigned short)Clamp((float)(cntCmpB), (float)(maxCntCmp - 1), 1.0f); - DCL_APT_SetCounterCompare(aptx, APT_COMPARE_REFERENCE_B, tmp); -} - -/** - * @brief Open the three-phase lower pipe. - * @param aptAddr Three-phase APT address pointer. - * @param maxDutyCnt Max duty count. - * @retval None. - */ -static void AptTurnOnLowSidePwm(APT_RegStruct **aptAddr, unsigned short maxDutyCnt) -{ - MCS_ASSERT_PARAM(aptAddr != NULL); - MCS_ASSERT_PARAM(maxDutyCnt != 0); - unsigned short dutyCnt; - dutyCnt = (unsigned short)(maxDutyCnt * APT_FULL_DUTY); - /* Open the three-phase lower pipe */ - for (unsigned int i = 0; i < PHASE_MAX_NUM; i++) { - APT_RegStruct *aptx = (APT_RegStruct *)(aptAddr[i]); - DCL_APT_SetCounterCompare(aptx, APT_COMPARE_REFERENCE_C, dutyCnt); - DCL_APT_SetCounterCompare(aptx, APT_COMPARE_REFERENCE_D, dutyCnt); - } -} - -/** - * @brief Enable three-phase pwm output. - * @param aptAddr Three-phase APT address pointer. - * @retval None. - */ -static void MotorPwmOutputEnable(APT_RegStruct **aptAddr) -{ - MCS_ASSERT_PARAM(aptAddr != NULL); - /* Enable three-phase pwm output */ - for (unsigned int i = 0; i < PHASE_MAX_NUM; i++) { - APT_RegStruct *aptx = (APT_RegStruct *)(aptAddr[i]); - aptx->PG_OUT_FRC.BIT.rg_pga_frc_en = BASE_CFG_UNSET; - aptx->PG_OUT_FRC.BIT.rg_pgb_frc_en = BASE_CFG_UNSET; - } -} - -/** - * @brief Disable three-phase pwm output. - * @param aptAddr Three-phase APT address pointer. - * @retval None. - */ -static void MotorPwmOutputDisable(APT_RegStruct **aptAddr) -{ - MCS_ASSERT_PARAM(aptAddr != NULL); - /* Disable three-phase pwm output. */ - for (unsigned int i = 0; i < PHASE_MAX_NUM; i++) { - APT_RegStruct *aptx = (APT_RegStruct *)(aptAddr[i]); - aptx->PG_OUT_FRC.BIT.rg_pga_frc_en = BASE_CFG_SET; - aptx->PG_OUT_FRC.BIT.rg_pgb_frc_en = BASE_CFG_SET; - DCL_APT_ForcePWMOutputLow(aptx); - } -} - -/** - * @brief 构造一个新的mcs启动开关对象。 - * @param mtrCtrl The motor control handle. - * @retval None. - */ -static void MCS_StartupSwitch(MTRCTRL_Handle *mtrCtrl) -{ - // 确保传递的电机控制句柄不是NULL - MCS_ASSERT_PARAM(mtrCtrl != NULL); - // 获取启动阶段处理结构体的指针 - STARTUP_Handle *startup = &mtrCtrl->startup; - // 获取dq轴参考值结构体的指针 - DqAxis *idqRef = &mtrCtrl->idqRef; - // 获取目标电流幅值 - float iftargetAmp = mtrCtrl->ifCtrl.targetAmp; - // 获取速度参考值(Hz) - float spdRefHz = mtrCtrl->spdRefHz; - // 根据当前启动阶段执行相应操作 - switch (startup->stage) { - case STARTUP_STAGE_CURR: - if (mtrCtrl->ifCtrl.curAmp >= iftargetAmp) { //IF采样电流大于IF目标电流时。 - /* 阶段转换 */ - // 设置q轴参考电流为目标电流值 - idqRef->q = iftargetAmp; - // 更改启动阶段至速度阶段 - startup->stage = STARTUP_STAGE_SPD; - mtrCtrl->encReady = 0; /* 启动阶段清除Z信号干扰错误 */ - - } else { - /* 增加电流幅值 */ - // 计算并设置新的q轴参考电流 - idqRef->q = IF_CurrAmpCalc(&mtrCtrl->ifCtrl); - // 在此阶段保持速度参考为0 - spdRefHz = 0.0f; - } - break; - - case STARTUP_STAGE_SPD: // 当前处于速度启动阶段 - /* 提高电流频率 */ - // 判断电机是否已经旋转了足够圈数 - if (mtrCtrl->motorSpinPos > 3) { /* 3是If模式下的电机转数 */ - /* 阶段转换 */ - // 转换状态机至运行状态 - mtrCtrl->stateMachine = FSM_RUN; - } else { - /* 速度斜坡生成 */ - // 执行速度斜坡生成函数,并设定一个固定的拖动速度 - spdRefHz = RMG_Exec(&mtrCtrl->spdRmg, 5.0f); /*5.0f为If模式力拖动速度*/ - } - break; - - default: - // 默认情况,不执行任何操作 - break; - } - // 更新电机控制结构中的速度参考值 - mtrCtrl->spdRefHz = spdRefHz; -} - -/** - * @brief Pre-processing of motor status. - * @param statusReg System status. - * @param stateMachine Motor Control Status. - * @retval None. - */ -static void MotorStatePerProc(SysStatusReg *statusReg, volatile FsmState *stateMachine) -{ - MCS_ASSERT_PARAM(statusReg != NULL); - MCS_ASSERT_PARAM(stateMachine != NULL); - /* Get system status */ - if (SysIsError(statusReg)) { - *stateMachine = FSM_FAULT; - } - if (SysGetCmdStop(statusReg)) { - SysCmdStopClr(statusReg); - *stateMachine = FSM_STOP; - } -} - -/** - * @brief Check over current status. - * @param statusReg System status. - * @param stateMachine Motor Control Status. - * @retval None. - */ -static void CheckOverCurrentState(SysStatusReg *statusReg, FsmState *stateMachine) -{ - MCS_ASSERT_PARAM(statusReg != NULL); - MCS_ASSERT_PARAM(stateMachine != NULL); - /* check systerm error status */ - if (SysIsError(statusReg) == false) { - *stateMachine = FSM_IDLE; - } -} - -/** - * @brief Check bootstrap capacitor charge time. - * @param mtrCtrl The motor control handle. - * @param stateMachine Motor Control Status. - * @retval None. - */ -static void CheckBootstrpCapChargeTime(MTRCTRL_Handle *mtrCtrl, FsmState *stateMachine) -{ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - MCS_ASSERT_PARAM(stateMachine != NULL); - mtrCtrl->sysTickCnt++; - /* check bootstrap capacitor charge time */ - if (mtrCtrl->sysTickCnt == mtrCtrl->capChargeTickNum) { - *stateMachine = FSM_CLEAR; - } -} - - - -/** - * @brief Check systerm cmd start signal. - * @param mtrCtrl The motor control handle. - * @param aptAddr Three-phase APT address pointer. - * @param statusReg System status. - * @param stateMachine Motor Control Status. - * @retval None. - */ -static void CheckSysCmdStart(MTRCTRL_Handle *mtrCtrl, - APT_RegStruct **aptAddr, - SysStatusReg *statusReg, - FsmState *stateMachine) -{ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - MCS_ASSERT_PARAM(aptAddr != NULL); - MCS_ASSERT_PARAM(statusReg != NULL); - MCS_ASSERT_PARAM(stateMachine != NULL); - /* check start cmd */ - if (SysGetCmdStart(statusReg)) { - SysRunningSet(statusReg); - SysCmdStartClr(statusReg); - mtrCtrl->sysTickCnt = 0; - *stateMachine = FSM_CAP_CHARGE; - /* 为自举电容器充电做准备。 */ - AptTurnOnLowSidePwm(aptAddr, mtrCtrl->aptMaxcntCmp); - /* Out put pwm */ - MotorPwmOutputEnable(aptAddr); - } -} - -/***************************************** - * @brief 系统计时器勾选任务。 - * @param mtrCtrl The motor control handle. - * @param aptAddr Three-phase APT address pointer. - * @retval None. - * 速度环 - * - * - ****************************************/ -static void TSK_SystickIsr(MTRCTRL_Handle *mtrCtrl, APT_RegStruct **aptAddr) -{ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - MCS_ASSERT_PARAM(aptAddr != NULL); - SysStatusReg *statusReg = &mtrCtrl->statusReg; - FsmState *stateMachine = &mtrCtrl->stateMachine; - mtrCtrl->msTickCnt++; - /* 电机状态的预处理。 */ - MotorStatePerProc(statusReg, stateMachine); - /* statemachine */ - switch (*stateMachine) { - case FSM_IDLE: - /*在电机启动前设置smo估计速度*/ - CheckSysCmdStart(mtrCtrl, aptAddr, statusReg, stateMachine); - break; - case FSM_CAP_CHARGE: - /* Bootstrap电容充电定时 */ - CheckBootstrpCapChargeTime(mtrCtrl, stateMachine); - break; - /* 启动前清除参数 */ - case FSM_CLEAR: - ClearBeforeStartup(mtrCtrl); - *stateMachine = FSM_STARTUP; - break; - case FSM_STARTUP: - // 构造一个新的mcs启动开关对象。 - MCS_StartupSwitch(mtrCtrl); - break; - case FSM_RUN: - if (mtrCtrl->controlMode == FOC_CONTROLMODE_SPEED) { /* Speed control mode */ - /* 速度斜坡控制 */ - mtrCtrl->spdRefHz = RMG_Exec(&mtrCtrl->spdRmg, mtrCtrl->spdCmdHz); - } else if (mtrCtrl->controlMode == FOC_CONTROLMODE_POS) { /* Position control mode */ - mtrCtrl->sysTickCnt++; - POSCTRL_SetSlope(&mtrCtrl->posCtrl, mtrCtrl->spdCmdHz);//设置位置斜坡 - /* 200.0是目标位置,用户可以重新定义 */ - // POSCTRL_SetTarget(&mtrCtrl->posCtrl, 0.5 *DOUBLE_PI * g_motorParam.mtrNp);//定位环目标位置设置。 - - float posFbk = POSCTRL_AngleExpand(&mtrCtrl->posCtrl, mtrCtrl->axisAngle); - - if (mtrCtrl->sysTickCnt % 5 == 0) { /* 5是位置环划分系数。*/ - mtrCtrl->spdRefHz = POSCTRL_Exec(&mtrCtrl->posCtrl, mtrCtrl->posCtrl.posTarget, posFbk); - } - } - /* 速度环路控制 */ - mtrCtrl->idqRef.q = SPDCTRL_Exec(&mtrCtrl->spdCtrl, mtrCtrl->spdRefHz, mtrCtrl->encSpeed);//用速度偏差值,求出所需Q轴电流 - - break; - case FSM_STOP: - mtrCtrl->spdRefHz = 0.0f; - MotorPwmOutputDisable(aptAddr); - SysRunningClr(statusReg); - *stateMachine = FSM_IDLE; - break; - case FSM_FAULT: /* Overcurrent state */ - CheckOverCurrentState(statusReg, stateMachine); - break; - default: - break; - } -} - -/** - * @brief Read the ADC initialize bias trim value. - * @param mtrCtrl The motor control handle. - * @retval None. - */ -static void TrimInitAdcShiftValue(MTRCTRL_Handle *mtrCtrl) -{ - float adc0SampleTemp = 0.0f; /* Current bias value for temp store */ - float adc1SampleTemp = 0.0f; - float adc0TempSum = 0.0f; - float adc1TempSum = 0.0f; /* Current bias sum value for 20 times */ - float adcSampleTimes = 0.0f; /* ADC sample times */ - for (int i = 0; i < ADC_READINIT_TIMES; i++) { - adc0SampleTemp = (float)HAL_ADC_GetConvResult(&ADCU_HANDLE, ADCUSOCNUM); - adc1SampleTemp = (float)HAL_ADC_GetConvResult(&ADCW_HANDLE, ADCWSOCNUM); - BASE_FUNC_DELAY_US(200); /* 200 is delay count, delay 200us triger adc sampling */ - if (adc0SampleTemp > 1000.0f && adc1SampleTemp > 1000.0f) { - adcSampleTimes++; - adc0TempSum += adc0SampleTemp; - adc1TempSum += adc1SampleTemp; - } - } - adc0SampleTemp = adc0TempSum / adcSampleTimes; - adc1SampleTemp = adc1TempSum / adcSampleTimes; - /* Force convert to float */ - mtrCtrl->adc0Compensate = (float) adc0SampleTemp; - mtrCtrl->adc1Compensate = (float) adc1SampleTemp; - /* The normal value scope: 1800 < adc0Compensate < 2200 */ - if(g_mc.adc0Compensate < ADC_TRIMVALUE_MIN || g_mc.adc0Compensate > ADC_TRIMVALUE_MAX \ - || g_mc.adc1Compensate < ADC_TRIMVALUE_MIN || g_mc.adc1Compensate > ADC_TRIMVALUE_MAX) { - DBG_PRINTF("ADC trim value error,please reset!"); - HAL_GPIO_SetValue(&LED2_HANDLE, LED2_PIN, GPIO_LOW_LEVEL); - } - adcSampleTimes = 0; - adc0TempSum = 0; - adc1TempSum = 0; -} - -/** - * @brief Read the ADC current sampling value. - * @param CurrUvw Three-phase current. - * @retval None. - */ -static void ReadCurrUvw(UvwAxis *CurrUvw) -{ - MCS_ASSERT_PARAM(CurrUvw != NULL); - float adc0 = (float)HAL_ADC_GetConvResult(&ADCU_HANDLE, ADCUSOCNUM); - float adc1 = (float)HAL_ADC_GetConvResult(&ADCW_HANDLE, ADCWSOCNUM); - /* Convert adc sample value to current value */ - CurrUvw->u = -(adc0 - g_mc.adc0Compensate) * g_mc.adcCurrCofe; - CurrUvw->w = -(adc1 - g_mc.adc1Compensate) * g_mc.adcCurrCofe; - CurrUvw->v = -CurrUvw->u - CurrUvw->w; -} - -/** - * @brief Setting the APT Output Duty Cycle. - * @param aptx APT register base address. - * @param leftDuty Left duty cycle. - * @param rightDuty Right duty cycle. - * @retval None. - */ -static void SetPwmDuty(APT_Handle *aptx, float leftDuty, float rightDuty) -{ - MCS_ASSERT_PARAM(aptx != NULL); - MCS_ASSERT_PARAM(leftDuty > 0); - MCS_ASSERT_PARAM(rightDuty > 0); - unsigned short maxPeriodCnt = aptx->waveform.timerPeriod; - unsigned short cntCmpLeftEdge = (unsigned short)(leftDuty * maxPeriodCnt); - unsigned short cntCmpRightEdge = (unsigned short)(rightDuty * maxPeriodCnt); - /* avoid overflowing */ - cntCmpLeftEdge = (cntCmpLeftEdge > maxPeriodCnt) ? maxPeriodCnt : cntCmpLeftEdge; - cntCmpRightEdge = (cntCmpRightEdge > maxPeriodCnt) ? maxPeriodCnt : cntCmpRightEdge; - HAL_APT_SetPWMDuty(aptx, cntCmpLeftEdge, cntCmpRightEdge); -} - -/** - * @brief Duty Cycle Setting. - * @param dutyUvwLeft Three-phase left duty cycle. - * @param dutyUvwRight Three-phase right duty cycle. - * @retval None. - */ -static void SetPwmDutyCp(UvwAxis *dutyUvwLeft, UvwAxis *dutyUvwRight) -{ - MCS_ASSERT_PARAM(dutyUvwLeft != NULL); - MCS_ASSERT_PARAM(dutyUvwRight != NULL); - /* Setting the Three-Phase Duty Cycle */ - SetPwmDuty(&g_apt0, dutyUvwLeft->u, dutyUvwRight->u); - SetPwmDuty(&g_apt1, dutyUvwLeft->v, dutyUvwRight->v); - SetPwmDuty(&g_apt2, dutyUvwLeft->w, dutyUvwRight->w); -} - -/** - * @brief To set the ADC sampling trigger comparison value. - * @param cntCmpSOCA Soca Compare Count Value. - * @param cntCmpSOCB Socb Compare Count Value. - * @retval None. - */ -static void SetADCTriggerTime(unsigned short cntCmpSOCA, unsigned short cntCmpSOCB) -{ - MCS_SetAdcCompareR1(g_apt[PHASE_U], cntCmpSOCA, cntCmpSOCB, g_mc.aptMaxcntCmp); -} - -/** - * @brief Temprature table. - * @param tempResisValue Temperature sensor resistance. - * @retval None. - */ -static float TempTable(float tempResisValue) -{ - float boardTemp = 0.0f; - /* Temperatures between 15 and 30. */ - if (tempResisValue > TEMP_RES_30 && tempResisValue <= TEMP_RES_15) { - boardTemp = TEMP_15 + (TEMP_30 - TEMP_15) * (TEMP_RES_15 - tempResisValue) / (TEMP_RES_15 - TEMP_RES_30); - } else if (tempResisValue > TEMP_RES_45 && tempResisValue <= TEMP_RES_30) { /* Temperature between 30 and 45. */ - boardTemp = TEMP_30 + (TEMP_45 - TEMP_30) * (TEMP_RES_30 - tempResisValue) / (TEMP_RES_30 - TEMP_RES_45); - } else if (tempResisValue > TEMP_RES_60 && tempResisValue <= TEMP_RES_45) { /* Temperature between 45 and 50. */ - boardTemp = TEMP_45 + (TEMP_60 - TEMP_45) * (TEMP_RES_45 - tempResisValue) / (TEMP_RES_45 - TEMP_RES_60); - } else if (tempResisValue <= TEMP_RES_60) { - boardTemp = TEMP_60; /* If temperature is over 60, set temperature as 60. */ - } else if (tempResisValue >= TEMP_RES_15) { - boardTemp = TEMP_15; /* If temperature is lower 15, set temperature as 15. */ - } - return boardTemp; -} - -/** - * @brief Read power board temperature and udc. - * @retval None. - */ -static void ReadBoardTempAndUdc(void) -{ - HAL_ADC_SoftTrigSample(&ADCRESIS_HANDLE, ADCRESISSOCNUM); - HAL_ADC_SoftTrigSample(&ADCUDC_HANDLE, ADCUDCSOCNUM); - BASE_FUNC_DELAY_US(CNT_10); /* Delay 10 us. */ - /* Force convert to float type. */ - float resisAdcValue = (float)HAL_ADC_GetConvResult(&ADCRESIS_HANDLE, ADCRESISSOCNUM); - /* 10 / (x + 10) * 3.3 = resisAdcValue / 4096 * 3.3, x is resisValue, 10kohm is resistor divider value. */ - float resisValue = (4096.0f * 10.0f - 10.0f * resisAdcValue) / resisAdcValue; - g_mc.powerBoardTemp = TempTable(resisValue); - g_mc.udc = ((float)HAL_ADC_GetConvResult(&ADCUDC_HANDLE, ADCUDCSOCNUM)) * ADC_UDC_COFFI; -} - -/** - * @brief 执行异常反馈速度保护动作。 - * @retval None. - */ -static void SpdFbkErrorProt_Exec(void) -{ - if (g_mc.prot.motorErrStatus.Bit.motorStalling == 0 && - g_mc.prot.motorErrStatus.Bit.overVoltErr == 0 && - g_mc.prot.motorErrStatus.Bit.lowerVoltErr == 0 && - g_mc.prot.motorErrStatus.Bit.overIpmTempErr == 0 && - g_mc.prot.motorErrStatus.Bit.overCurrErr == 0) { - g_mc.prot.motorErrStatus.Bit.revRotErr = 1; - /* If revRotErr, execute protect motion. */ - ProtSpo_Exec(g_apt); - } -} -/** - * @brief Check abnormal feedback speed. - * @retval None. - */ -static void CheckSpdFbkStatus(void) -{ - static short errorSpdStatus = 0; - static short errorDeltaSpdStatus = 0; - /* Detect the nan value. */ - if (isnan(g_mc.encSpeed) || isnan(g_mc.idqRef.q)) { - errorSpdStatus++; - } else { - errorSpdStatus = 0; - } - if (g_mc.stateMachine == FSM_RUN) { - /* 检测反馈速度异常。定义10为速度误差值,0.5为当前误差值*/ - if (Abs(g_mc.spdRefHz - g_mc.encSpeed) >= CNT_10 && g_mc.idqRef.q <= 0.5f) { - errorDeltaSpdStatus++; - } - } - /*如果计数超过5000次,则执行保护动作. */ - if (errorSpdStatus >= CNT_5000 || errorDeltaSpdStatus >= CNT_5000) { - errorSpdStatus = 0; - errorDeltaSpdStatus = 0; - SpdFbkErrorProt_Exec(); - } -} -/** - * @brief Check Potentiometer Value callback function. - * @param param The TIMER_Handle. - * @retval None. - */ -void CheckPotentiometerValueCallback(void *param) -{ - MCS_ASSERT_PARAM(param != NULL); - BASE_FUNC_UNUSED(param); - static float potentiomitorAdcValue = 0.0f; - static float spdCmdHz = 0; - static float spdCmdHzLast = 35.0f; /* 35.0 is spdCmdHzLast init value */ - HAL_ADC_SoftTrigSample(&ADCPTT_HANDLE, ADCPTTSOCNUM); - BASE_FUNC_DELAY_US(10); /* Delay 10 us. */ - potentiomitorAdcValue = (float)HAL_ADC_GetConvResult(&ADCPTT_HANDLE, ADCPTTSOCNUM); - /* 4045.0 is adc sample max value of potentiomitor, convert max spd to 200.0Hz */ - spdCmdHz = potentiomitorAdcValue / 4045.0f * USER_MAX_SPD_HZ;//电位器控制速度 - // spdCmdHz=uart_spdcmd;//串口控制速度 - - POSCTRL_SetTarget(&g_mc.posCtrl,uart_poscmd * g_motorParam.mtrNp);//串口控制位置转动 - - // POSCTRL_SetTarget(&g_mc.posCtrl,potentiomitorAdcValue / 4045.0f * 6.28 * g_motorParam.mtrNp);//电位器控制电机位置转动 - - - if (Abs(spdCmdHzLast - spdCmdHz) < 1.0f) { /* Ignore changes less than 1. */ - return; - } - spdCmdHzLast = spdCmdHz; - if (spdCmdHz < USER_MIN_SPD_HZ) { /* USER_MIN_SPD_HZ is spdCmdHz lower limit */ - spdCmdHz = USER_MIN_SPD_HZ; /* USER_MIN_SPD_HZ is spdCmdHz lower limit */ - } - if (spdCmdHz > g_mc.mtrParam.maxElecSpd) { /* spdCmdHz upper limit */ - spdCmdHz = g_mc.mtrParam.maxElecSpd; /* spdCmdHz upper limit */ - } - if (g_mc.spdAdjustMode == CUST_SPEED_ADJUST) { - g_mc.spdCmdHz = spdCmdHz; - } -} - -/** - * @brief 用于电机状态机回叫功能的系统定时器ISR。 - * @param param The systick timer handle. - * @retval None. - * - */ -void MotorStatemachineCallBack(void *param) -{ - /* Verify Parameters */ - MCS_ASSERT_PARAM(param != NULL); - BASE_FUNC_UNUSED(param); - /* 读取电源板温度和电压。*/ - ReadBoardTempAndUdc(); - /* 电机错误状态检查. */ - TSK_SystickIsr(&g_mc, g_apt); - if (g_mc.prot.motorErrStatus.all == 0) { - /* 电机错误状态检查 */ - CheckSpdFbkStatus(); - /* 电机失速检测。 */ - STP_Det_ByCurrSpd(&g_mc.prot.stall, &g_mc.prot.motorErrStatus, g_mc.encSpeed, g_mc.idqFbk); - STP_Exec(&g_mc.prot.motorErrStatus, g_apt); - } - /* 电机过电压检测。 */ - OVP_Det(&g_mc.prot.ovp, &g_mc.prot.motorErrStatus, g_mc.udc); - OVP_Exec(&g_mc.prot.ovp, &g_mc.spdRefHz, g_apt); - OVP_Recy(&g_mc.prot.ovp, &g_mc.prot.motorErrStatus, g_mc.udc); - /* 电机低电压检测. */ - LVP_Det(&g_mc.prot.lvp, &g_mc.prot.motorErrStatus, g_mc.udc); - LVP_Exec(&g_mc.prot.lvp, &g_mc.spdRefHz, g_apt); - LVP_Recy(&g_mc.prot.lvp, &g_mc.prot.motorErrStatus, g_mc.udc); - /* 电源板超温检测. */ - OTP_Det(&g_mc.prot.otp, &g_mc.prot.motorErrStatus, OTP_IPM_ERR_BIT, g_mc.powerBoardTemp); - OTP_Exec(&g_mc.prot.otp, &g_mc.spdRefHz, g_apt); - OTP_Recy(&g_mc.prot.otp, &g_mc.prot.motorErrStatus, OTP_IPM_ERR_BIT, g_mc.powerBoardTemp); - - /* 如果保护级别==4,则将电机状态设置为停止。 */ - if (g_mc.prot.ovp.protLevel == LEVEL_4 || g_mc.prot.lvp.protLevel == LEVEL_4 \ - || g_mc.prot.otp.protLevel == LEVEL_4) { - SysCmdStopSet(&g_mc.statusReg); - } -} - -/** - * @brief The carrier ISR wrapper function. - * @param aptHandle The APT handle. - * @retval None. - * - * 载波中断函数, - */ -void MotorCarrierProcessCallback(void *aptHandle) -{ - MCS_ASSERT_PARAM(aptHandle != NULL); - BASE_FUNC_UNUSED(aptHandle); - /* the carrierprocess of motor */ - MCS_CarrierProcess(&g_mc); - /* Over current protect */ - if (g_mc.stateMachine == FSM_RUN || g_mc.stateMachine == FSM_STARTUP) { - OCP_Det(&g_mc.prot.ocp, &g_mc.prot.motorErrStatus, g_mc.idqFbk); - OCP_Exec(&g_mc.prot.ocp, &g_mc.idqFbk, g_apt); /* Execute over current protect motion */ - if (g_mc.prot.ocp.protLevel < LEVEL_4) { - OCP_Recy(&g_mc.prot.ocp, &g_mc.prot.motorErrStatus); - } - } -} - -/** - * @brief Event interrupt callback function of APT module. - * @param para APT module handle. - * @retval None. - */ -void MotorSysErrCallback(void *para) -{ - MCS_ASSERT_PARAM(para != NULL); - APT_Handle *handle = (APT_Handle *)para; - /* The IPM overcurrent triggers and disables the three-phase PWM output. */ - MotorPwmOutputDisable(g_apt); - DCL_APT_ClearOutCtrlEventFlag((APT_RegStruct *)g_apt[PHASE_U], APT_OC_COMBINE_EVENT_A1); - DCL_APT_ClearOutCtrlEventFlag((APT_RegStruct *)g_apt[PHASE_V], APT_OC_COMBINE_EVENT_A1); - DCL_APT_ClearOutCtrlEventFlag((APT_RegStruct *)g_apt[PHASE_W], APT_OC_COMBINE_EVENT_A1); - /* Status setting error */ - SysErrorSet(&g_mc.statusReg); - DBG_PRINTF("APT error! \r\n"); - HAL_GPIO_SetValue(&LED2_HANDLE, LED2_PIN, GPIO_LOW_LEVEL); - BASE_FUNC_UNUSED(handle); -} - -/** - * @brief 初始化电机控制器的数据结构。 - * @retval None. - */ -static void InitSoftware(void) -{ - /* 正在初始化电机控制参数 */ - TSK_Init(); - /* Read phase-uvw current */ - g_mc.readCurrUvwCb = ReadCurrUvw; - g_mc.setPwmDutyCb = SetPwmDutyCp; - g_mc.setADCTriggerTimeCb = SetADCTriggerTime; - g_mc.encHandle = &g_enc; - - MCS_EncInitStru encMotorParam; - EncoderHandle* enc = &g_enc; - encMotorParam.mtrNp = g_motorParam.mtrNp; - encMotorParam.mtrPPMR = g_motorParam.mtrPPMR; - encMotorParam.zShift = g_motorParam.zShift; - encMotorParam.ctrlPeriod = CTRL_CURR_PERIOD; - encMotorParam.timeNum = ENC_TIMES_NUM; - MCS_EncoderInit(enc, &encMotorParam); /* encoder Initializing Parameter Configurations. */ - - /* MCU peripheral configuration function used for initial motor control. */ - g_mc.getEncAngSpd = GetEncAngSpd; /* Callback function for obtaining the encoder speed angle. */ -} - -/** - * @brief Config the master APT. - * @param aptx The master APT handle. - * @retval None. - */ -static void AptMasterSet(APT_Handle *aptx) -{ - MCS_ASSERT_PARAM(aptx != NULL); - /* Config the master APT. */ - HAL_APT_MasterSyncInit(aptx, APT_SYNC_OUT_ON_CNTR_ZERO); -} - -/** - * @brief Config the slave APT. - * @param aptx The slave APT handle. - * @retval None. - */ -static void AptSalveSet(APT_Handle *aptx) -{ - MCS_ASSERT_PARAM(aptx != NULL); - APT_SlaveSyncIn slave; - /* Config the slave APT. */ - slave.divPhase = 0; - slave.cntPhase = 0; - slave.syncCntMode = APT_COUNT_MODE_AFTER_SYNC_UP; - slave.syncInSrc = APT_SYNC_IN_SRC; - slave.cntrSyncSrc = APT_CNTR_SYNC_SRC_SYNCIN; - HAL_APT_SlaveSyncInit(aptx, &slave); -} -/** - * @brief Configuring Master and Slave APTs. - * @retval None. - */ -static void AptMasterSalveSet(void) -{ - /* motor fan APT master/slave synchronization */ - AptMasterSet(&g_apt0); - AptSalveSet(&g_apt1); - AptSalveSet(&g_apt2); -} - -/** - * @brief Config the KEY func. - * @param handle The GPIO handle. - * @retval None. - */ -static KEY_State Key_StateRead(GPIO_Handle *handle) -{ - if (HAL_GPIO_GetPinValue(handle, handle->pins) == 0) { - BASE_FUNC_DELAY_MS(30); /* delay 30ms for deshake */ - if (HAL_GPIO_GetPinValue(handle, handle->pins) == 0) { - while (HAL_GPIO_GetPinValue(handle, handle->pins) == 0) { - } - return KEY_DOWN; - } - } - return KEY_UP; -} - - -/** - * @brief Control motor start and stop state by key func. - * @param param The GPIO handle. - * @retval None. - */ -void MotorStartStopKeyCallback(void *param) -{ - GPIO_Handle *handle = (GPIO_Handle *)param; - if (Key_StateRead(handle) == KEY_DOWN) { - if (g_mc.motorStateFlag == 0) { /* start motor */ - g_mc.motorStateFlag = 1; - SysCmdStartSet(&g_mc.statusReg); - } else if (g_mc.motorStateFlag == 1) { /* stop motor */ - g_mc.motorStateFlag = 0; - SysCmdStopSet(&g_mc.statusReg); - } - } -} - -/** - * @brief 用户应用程序的主入口函数。 - * @retval BSP_OK 返回状态。 - */ -int MotorMainProcess(void) -{ - unsigned int tickNum1Ms = 2; /* 定义1毫秒的计时器滴答数 */ - static unsigned int tickCnt1Ms = 0; /* 静态变量,用于记录1毫秒滴答计数 */ - unsigned int tickNum500Ms = 1000; /* 定义500毫秒的计时器滴答数 */ - static unsigned int tickCnt500Ms = 0; /* 静态变量,用于记录500毫秒滴答计数 */ - - SystemInit(); /* 系统初始化 */ - HMI_Init(); /* 初始化串口中断,用于人机交互接口 */ - HAL_TIMER_Start(&g_timer0); /* 启动定时器0 */ - HAL_TIMER_Start(&g_timer1); /* 启动定时器1 */ - - - AptMasterSalveSet(); /* 设置主从设备 */ - /* 在启动前禁用PWM输出。 */ - MotorPwmOutputDisable(g_apt); /* 禁用PWM输出 */ - /* 软件初始化。 */ - InitSoftware(); /* 初始化软件相关设置 电机参数初始化*/ - - /* QDM使用算法驱动的速度和角度采集 */ - static MCS_QdmInitStru qdmInit; /* QDM初始化结构体 */ - qdmInit.qdmAddr = g_periph.qdm.qdmAddr; /* 设置QDM地址 */ - - qdmInit.zPlusesIrqFunc = ISR_QdmzPulses; /* 设置QDM零脉冲中断服务函数 */ - - qdmInit.zPlusesNvic = &g_periph.qdm.zPulsesNvic; /* 设置QDM零脉冲中断控制器 */ - qdmInit.zPlusesIrqPrio = IRQ_QDM0_PRIORITY; /* 设置QDM零脉冲中断优先级 */ - MCS_QdmInit(&qdmInit); /* 初始化QDM,必须在使能载波中断之前执行。 */ - - /* 启动PWM时钟。 */ - HAL_APT_StartModule(RUN_APT0 | RUN_APT1 | RUN_APT2); /* 启动APT模块 */ - /* 系统定时器时钟延迟。 */ - BASE_FUNC_DELAY_MS(ADC_READINIT_DELAY); /* 延迟,等待ADC初始化 */ - TrimInitAdcShiftValue(&g_mc); /* 初始化ADC移位值 */ - BASE_FUNC_DELAY_MS(MOTOR_START_DELAY); /* 延迟,等待电机启动 */ - while (1) { - - /* 循环发送数据到主机 */ - HMI_Process_Tx(&g_mc); /* 处理人机交互接口的发送数据 */ - if (g_mc.msTickCnt - tickCnt1Ms >= tickNum1Ms) { /* 检查是否达到1毫秒的计时 */ - tickCnt1Ms = g_mc.msTickCnt; /* 更新1毫秒滴答计数 */ - /* 用户代码1毫秒事件 */ - HMI_Process_Rx(&g_mc); /* 处理人机交互接口的接收数据 */ - /* 用户代码1毫秒事件 */ - } - - if (g_mc.msTickCnt - tickCnt500Ms >= tickNum500Ms) { /* 检查是否达到500毫秒的计时 */ - if (SysIsError(&g_mc.statusReg) != true) { /* 检查系统是否出现错误 */ - /* 如果系统状态正常,则切换LED灯的状态。 */ - HAL_GPIO_TogglePin(&LED1_HANDLE, LED1_PIN); /* 切换LED灯的状态 */ - } - tickCnt500Ms = g_mc.msTickCnt; /* 更新500毫秒滴答计数 */ - } - } - return 0; -} diff --git a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/user_interface/cust_process.c b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/user_interface/cust_process.c deleted file mode 100644 index 69f95195..00000000 --- a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/user_interface/cust_process.c +++ /dev/null @@ -1,639 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file cust_process.c - * @author MCU Algorithm Team - * @brief This file provides functions declaration of cust process interface. - */ - -#include "cust_process.h" -#include "mcs_ctlmode_config.h" -#include "mcs_math_const.h" -#include "mcs_user_config.h" -#include "mcs_assert.h" -#include "main.h" -#include "uart_module.h" -#include "mcs_mtr_param.h" -#include "mcs_inc_enc.h" - -/* Macro definitions --------------------------------------------------------------------------- */ -/* Constant value. */ -#define CONST_VALUE_60 60.0f /* Constant value 60. */ -#define CONST_VALUE_DIV_1000 0.001f /* Constant value 1/1000. */ - -/* Data array index. */ -#define DATA_SEGMENT_ONE 0 /* First element of the data segment */ -#define DATA_SEGMENT_TWO 1 /* 数据段的第二个元素 */ -#define DATA_SEGMENT_THREE 2 /* Third element of the data segment */ -#define DATA_SEGMENT_FOUR 3 /* Fourth element of the data segment */ -#define CUSTACKCODELEN 10 /* Ack code length */ - -/* Command code. */ -#define SET_PID_KP 0x01 /* Set Pid Kp Command Params */ -#define SET_PID_KI 0x02 /* Set Pid Ki Command Params */ -#define SET_PID_LIMIT 0x03 /* Set Pid limit Command Params */ - -#define SET_ENC_MTRPPMR 0x01 /* Set encoder lines */ -#define SET_ENC_ZSHIFT 0x02 /* Set encoder zero point shift */ - -#define SET_SPD_COMMAND_HZ 0x01 /* Set Target Speed Command Params */ -#define SET_SPD_RMG_SLOPE 0x02 /* Set Speed Slope Command Params */ - -#define SET_MAX_ELEC_SPD 0x01 /* Set Max Motor Speed Command Params */ -#define SET_MOTOR_NUM_OF_PAIRS 0x02 /* Set Motor Command Params */ - -#define SET_MOTOR_RES_OF_STATOR 0x01 /* Set Motor Res Command Params */ -#define SET_MOTOR_DAXIS_INDUCTANCE 0x02 /* Set Motor Daxis Inductance Command Params */ -#define SET_MOTOR_QAXIS_INDUCTANCE 0x03 /* Set Motor Qaxis Inductance Command Params */ - -#define SET_SVPWM_VOLTAGE_PER_UNIT 0x01 /* Set Svpwm Voltage Params */ -#define SET_ADC_CURRENT_SAMPLE_COFE 0x02 /* Set Adc coffe Params */ -#define SET_CURRENT_LOOP_CONTROL_PERIOD 0x03 /* Set Current loop period Command Params */ - -#define SET_IF_TARGET_CURRENT_VALUE 0x01 /* Set If Target Params */ -#define SET_INCREMENT_OF_IF_CURRENT 0x02 /* Set If Step Command Params */ -#define SET_SPEED_RING_BEGIN_SPEED 0x03 /* Set If to Smo Start Speed Command Params */ - -static unsigned char ackCode = 0; -static unsigned char g_uartTxBuf[CUSTACKCODELEN] = {0}; - -/** - * @brief Set observer type. - * @param mtrCtrl The motor control handle. - * @param rxData Receive buffer - */ -static void CMDCODE_SetObserverType(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) -{ - /* Get function code. */ - int funcCode = (int)(rxData->data[DATA_SEGMENT_ONE].typeF); - if (funcCode == FOC_OBSERVERTYPE_ENC) { - ackCode = 0X03; - mtrCtrl->obserType = FOC_OBSERVERTYPE_ENC; - CUST_AckCode(g_uartTxBuf, ackCode, mtrCtrl->obserType); - } -} - -/** - * @brief Set pid parameter ack code. - * @param funcCode Received data funccode. - */ -static unsigned char SetPidAckCode(int funcCode) -{ - switch (funcCode) { - /* Set current loop D-Axis PID parameter ack code. */ - case FOC_CURDAXISPID_PARAMS: - ackCode = 0xE0; - break; - /* Set current loop Q-Axis PID parameter ack code. */ - case FOC_CURQAXISPID_PARAMS: - ackCode = 0xE3; - break; - /* Set speed loop PID parameter ack code. */ - case FOC_SPDPID_PARAMS: - ackCode = 0xE6; - break; - default: - break; - } - return ackCode; -} - -/** - * @brief Set pid parameters. - * @param pidHandle The pid control handle. - * @param rxData Receive buffer - */ -static void SetPidParams(PID_Handle *pidHandle, CUSTDATATYPE_DEF *rxData) -{ - /* Get function code. */ - int funcCode = (int)(rxData->data[DATA_SEGMENT_ONE].typeF); - /* Get command code. */ - int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); - switch (cmdCode) { - case SET_PID_KP: /* Set the P parameter. */ - PID_SetKp(pidHandle, rxData->data[DATA_SEGMENT_THREE].typeF); - ackCode = SetPidAckCode(funcCode); - CUST_AckCode(g_uartTxBuf, ackCode + SET_PID_KP, rxData->data[DATA_SEGMENT_THREE].typeF); - break; - case SET_PID_KI: /* Set the I parameter. */ - PID_SetKi(pidHandle, rxData->data[DATA_SEGMENT_THREE].typeF); - ackCode = SetPidAckCode(funcCode); - CUST_AckCode(g_uartTxBuf, ackCode + SET_PID_KI, rxData->data[DATA_SEGMENT_THREE].typeF); - break; - case SET_PID_LIMIT: /* Set the pid limit. */ - PID_SetLimit(pidHandle, rxData->data[DATA_SEGMENT_THREE].typeF); - ackCode = SetPidAckCode(funcCode); - CUST_AckCode(g_uartTxBuf, ackCode + SET_PID_LIMIT, rxData->data[DATA_SEGMENT_THREE].typeF); - break; - default: - ackCode = 0X77; - CUST_AckCode(g_uartTxBuf, ackCode, 0); - break; - } -} - -/** - * @brief Set motor pid parameters. - * @param mtrCtrl The motor control handle. - * @param rxData Receive buffer - */ -static void CMDCODE_SetMotorPidParams(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) -{ - /* Get function code. */ - int funcCode = (int)(rxData->data[DATA_SEGMENT_ONE].typeF); - - if (funcCode == FOC_CURDAXISPID_PARAMS) { - SetPidParams(&mtrCtrl->currCtrl.dAxisPi, rxData); /* Set Curr loop Daxis pid params */ - } else if (funcCode == FOC_CURQAXISPID_PARAMS) { - SetPidParams(&mtrCtrl->currCtrl.qAxisPi, rxData); /* Set Curr loop Qaxis pid params */ - mtrCtrl->currCtrl.dAxisPi.upperLimit = mtrCtrl->currCtrl.qAxisPi.upperLimit; - mtrCtrl->currCtrl.dAxisPi.lowerLimit = mtrCtrl->currCtrl.qAxisPi.lowerLimit; - } else if (funcCode == FOC_SPDPID_PARAMS) { - SetPidParams(&mtrCtrl->spdCtrl.spdPi, rxData); /* Set speed loop params */ - } -} - -/** - * @brief Set encoder observer parameters. - * @param encHandle The observer control handle. - * @param rxData Receive buffer - */ -static void SetObserverEncParams(EncoderHandle *encHandle, CUSTDATATYPE_DEF *rxData) -{ - /* Get command code. */ - int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); - switch (cmdCode) { - case SET_ENC_MTRPPMR: /* Set encoder MTRPPMR. */ - encHandle->pulsePerMechRound = rxData->data[DATA_SEGMENT_THREE].typeF; - ackCode = 0X0D; - CUST_AckCode(g_uartTxBuf, ackCode, encHandle->pulsePerMechRound); - break; - case SET_ENC_ZSHIFT: /* Set encoder zero shift. */ - encHandle->zShift = rxData->data[DATA_SEGMENT_THREE].typeF; - ackCode = 0X0E; - CUST_AckCode(g_uartTxBuf, ackCode, encHandle->zShift); - break; - default: - break; - } -} - -/** - * @brief 设置观察者参数。 - * @param mtrCtrl The motor control handle. - * @param rxData Receive buffer - */ -static void CMDCODE_SetObserverParams(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) -{ - /* Get function code. */ - int funcCode = (int)(rxData->data[DATA_SEGMENT_ONE].typeF); - if (funcCode == FOC_OBSERVERTYPE_ENC) { - SetObserverEncParams(mtrCtrl->encHandle, rxData); - mtrCtrl->encHandle->pulsePerElecRound = mtrCtrl->encHandle->pulsePerMechRound / mtrCtrl->mtrParam.mtrNp; - } -} - -/** - * @brief 设置电机速度和速度斜率。 - * @param mtrCtrl The motor control handle. - * @param rxData Receive buffer - */ -static void CMDCODE_SetMotorSpdAndSlope(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) -{ - /* Get command code. */ - int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); - - switch (cmdCode) { - case SET_SPD_COMMAND_HZ: /* Set target speed(hz). */ - mtrCtrl->spdCmdHz = rxData->data[DATA_SEGMENT_THREE].typeF * mtrCtrl->mtrParam.mtrNp / CONST_VALUE_60; - /* Judgement the value > 0.00001, make sure denominator != 0 */ - if (rxData->data[DATA_SEGMENT_FOUR].typeF > 0.00001f) { - mtrCtrl->spdRmg.delta = mtrCtrl->spdCmdHz / rxData->data[DATA_SEGMENT_FOUR].typeF * CTRL_SYSTICK_PERIOD; - } - ackCode = 0X16; - CUST_AckCode(g_uartTxBuf, ackCode, mtrCtrl->spdCmdHz); - break; - case SET_SPD_RMG_SLOPE: /* Set speed slope. */ - mtrCtrl->spdRmg.delta = mtrCtrl->spdCmdHz / rxData->data[DATA_SEGMENT_THREE].typeF * CTRL_SYSTICK_PERIOD; - ackCode = 0X17; - CUST_AckCode(g_uartTxBuf, ackCode, rxData->data[DATA_SEGMENT_THREE].typeF); - break; - default: - break; - } -} - -/** - * @brief Set motor base parameters. - * @param mtrParamHandle The motor parameters handle. - * @param rxData Receive buffer - */ -static void SetMotorBaseParams(MOTOR_Param *mtrParamHandle, CUSTDATATYPE_DEF *rxData) -{ - /* Get command code. */ - int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); - - switch (cmdCode) { - case SET_MAX_ELEC_SPD: /* Set max electric speed. */ - mtrParamHandle->maxElecSpd = rxData->data[DATA_SEGMENT_THREE].typeF / - CONST_VALUE_60 * mtrParamHandle->mtrNp; - ackCode = 0X18; - CUST_AckCode(g_uartTxBuf, ackCode, rxData->data[DATA_SEGMENT_THREE].typeF); - break; - case SET_MOTOR_NUM_OF_PAIRS: /* Set the number of motor pole pairs. */ - mtrParamHandle->mtrNp = (unsigned short)(rxData->data[DATA_SEGMENT_THREE].typeF); - ackCode = 0X19; - CUST_AckCode(g_uartTxBuf, ackCode, (float)mtrParamHandle->mtrNp); - break; - default: - break; - } -} - -/** - * @brief Set motor special parameters. - * @param mtrParamHandle The motor parameters handle. - * @param rxData Receive buffer - */ -static void SetMotorSpecialParams(MOTOR_Param *mtrParamHandle, CUSTDATATYPE_DEF *rxData) -{ - /* Get command code. */ - int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); - - switch (cmdCode) { - case SET_MOTOR_RES_OF_STATOR: /* Set the resistor of stator. */ - mtrParamHandle->mtrRs = rxData->data[DATA_SEGMENT_THREE].typeF; - ackCode = 0X1A; - CUST_AckCode(g_uartTxBuf, ackCode, mtrParamHandle->mtrRs); - break; - case SET_MOTOR_DAXIS_INDUCTANCE: /* Set the d axis inductance. */ - mtrParamHandle->mtrLd = rxData->data[DATA_SEGMENT_THREE].typeF; - ackCode = 0X1B; - CUST_AckCode(g_uartTxBuf, ackCode, mtrParamHandle->mtrLd); - break; - case SET_MOTOR_QAXIS_INDUCTANCE: /* Set the q axis inductance. */ - mtrParamHandle->mtrLq = rxData->data[DATA_SEGMENT_THREE].typeF; - ackCode = 0X1C; - CUST_AckCode(g_uartTxBuf, ackCode, mtrParamHandle->mtrLq); - break; - default: - ackCode = 0X77; - CUST_AckCode(g_uartTxBuf, ackCode, 0); - break; - } -} - -/** - * @brief Set motor board parameters. - * @param mtrCtrl The motor control handle. - * @param rxData Receive buffer - */ -static void SetMotorBoardParams(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) -{ - /* Get command code. */ - int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); - - switch (cmdCode) { - case SET_SVPWM_VOLTAGE_PER_UNIT: /* Set svpwm voltage per unit. */ - mtrCtrl->sv.voltPu = rxData->data[DATA_SEGMENT_THREE].typeF * ONE_DIV_SQRT3; - mtrCtrl->currCtrl.outLimit = mtrCtrl->sv.voltPu * ONE_DIV_SQRT3; - ackCode = 0X1D; - CUST_AckCode(g_uartTxBuf, ackCode, rxData->data[DATA_SEGMENT_THREE].typeF); - break; - case SET_ADC_CURRENT_SAMPLE_COFE: /* Set adc current sample cofeature. */ - mtrCtrl->adcCurrCofe = rxData->data[DATA_SEGMENT_THREE].typeF; - ackCode = 0X1E; - CUST_AckCode(g_uartTxBuf, ackCode, mtrCtrl->adcCurrCofe); - break; - case SET_CURRENT_LOOP_CONTROL_PERIOD: /* Set current loop control period. */ - mtrCtrl->currCtrlPeriod = 1 / rxData->data[DATA_SEGMENT_THREE].typeF * CONST_VALUE_DIV_1000; - ackCode = 0X1F; - CUST_AckCode(g_uartTxBuf, ackCode, rxData->data[DATA_SEGMENT_THREE].typeF); - break; - default: - break; - } -} - -/** - * @brief Set motor parameters. - * @param mtrCtrl The motor control handle. - * @param rxData Receive buffer - */ -static void CMDCODE_SetMotorParams(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) -{ - /* Get function code. */ - int funcCode = (int)(rxData->data[DATA_SEGMENT_ONE].typeF); - - if (funcCode == MOTOR_PARAMS_BASE) { - SetMotorBaseParams(&mtrCtrl->mtrParam, rxData); - } else if (funcCode == MOTOR_PARAMS_SPECIAL) { - SetMotorSpecialParams(&mtrCtrl->mtrParam, rxData); - } else if (funcCode == MOTOR_PARAMS_BOARD) { - SetMotorBoardParams(mtrCtrl, rxData); - } -} - -/** - * @brief Motor start. - * @param mtrCtrl The motor control handle. - */ -static void CMDCODE_MotorStart(MTRCTRL_Handle *mtrCtrl) -{ - if (mtrCtrl->stateMachine != FSM_RUN) { - SysCmdStartSet(&mtrCtrl->statusReg); /* start motor. */ - mtrCtrl->motorStateFlag = 1; - ackCode = 0X24; /* send ackcode to host. */ - CUST_AckCode(g_uartTxBuf, ackCode, 1); - } -} - -/** - * @brief Motor stop. - * @param mtrCtrl The motor control handle. - */ -static void CMDCODE_MotorStop(MTRCTRL_Handle *mtrCtrl) -{ - SysCmdStopSet(&mtrCtrl->statusReg); - mtrCtrl->motorStateFlag = 0; - ackCode = 0X25; - CUST_AckCode(g_uartTxBuf, ackCode, 0); -} - -/** - * @brief Motor state reset. - * @param mtrCtrl The motor control handle. - */ -static void CMDCODE_MotorReset(MTRCTRL_Handle *mtrCtrl) -{ - BASE_FUNC_UNUSED(mtrCtrl); - BASE_FUNC_SoftReset(); -} - -/** - * @brief Set IF-Startup parameters. - * @param mtrCtrl The motor control handle. - * @param rxData Receive buffer - */ -static void SetStartupIFParams(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) -{ - /* Get command code. */ - int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); - - switch (cmdCode) { - case SET_IF_TARGET_CURRENT_VALUE: /* Set I/F start up target current value. */ - mtrCtrl->ifCtrl.targetAmp = rxData->data[DATA_SEGMENT_THREE].typeF; - ackCode = 0X26; - CUST_AckCode(g_uartTxBuf, ackCode, mtrCtrl->ifCtrl.targetAmp); - break; - case SET_INCREMENT_OF_IF_CURRENT: /* Set increment of I/F start up current. */ - mtrCtrl->ifCtrl.stepAmp = mtrCtrl->ifCtrl.targetAmp / rxData->data[DATA_SEGMENT_THREE].typeF * - CTRL_SYSTICK_PERIOD; - ackCode = 0X27; - CUST_AckCode(g_uartTxBuf, ackCode, mtrCtrl->ifCtrl.stepAmp); - break; - case SET_SPEED_RING_BEGIN_SPEED: /* Set speed ring begin speed. */ - mtrCtrl->startup.spdBegin = rxData->data[DATA_SEGMENT_THREE].typeF / - CONST_VALUE_60 * mtrCtrl->mtrParam.mtrNp; - ackCode = 0X28; - CUST_AckCode(g_uartTxBuf, ackCode, rxData->data[DATA_SEGMENT_THREE].typeF); - break; - default: - break; - } -} - -/** - * @brief Set start up parameters. - * @param mtrCtrl The motor control handle. - * @param rxData Receive buffer - */ -static void CMDCODE_SetStartupParams(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) -{ - /* Get function code. */ - int funcCode = (int)(rxData->data[DATA_SEGMENT_ONE].typeF); - - if (funcCode == FOC_STARTUP_IF) { - SetStartupIFParams(mtrCtrl, rxData); - } -} - -/** - * @brief Set adjust speed mode. - * @param mtrCtrl The motor control handle. - * @param rxData Receive buffer - */ -static void CMDCODE_SetAdjustSpdMode(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) -{ - /* Get function code. */ - int funcCode = (int)(rxData->data[DATA_SEGMENT_ONE].typeF); - /* Get commond code. */ - int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); - if (funcCode == HOST_SPEED_ADJUST) { - mtrCtrl->spdAdjustMode = HOST_SPEED_ADJUST; - /* Uart connect success. */ - mtrCtrl->uartConnectFlag = CONNECTING; - ackCode = 0X2A; - CUST_AckCode(g_uartTxBuf, ackCode, (float)mtrCtrl->spdAdjustMode); - } else if (funcCode == CUST_SPEED_ADJUST) { - if (cmdCode == 1) { /* If uart connection disconnected & stop motor commond. */ - SysCmdStopSet(&mtrCtrl->statusReg); - mtrCtrl->motorStateFlag = 0; - } - mtrCtrl->spdAdjustMode = CUST_SPEED_ADJUST; - ackCode = 0X2B; - CUST_AckCode(g_uartTxBuf, ackCode, (float)mtrCtrl->spdAdjustMode); - /* Uart disconnect. */ - mtrCtrl->uartConnectFlag = DISCONNECT; - } -} - -/** - * @brief Check uart connect. - * @param mtrCtrl The motor control handle. - * @param rxData Receive buffer - */ -static void CMDCODE_UartHandShake(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) -{ - CMDCODE_SetAdjustSpdMode(mtrCtrl, rxData); -} - -/** - * @brief Set Motor Initial Status Parameters. - * @param mtrCtrl The motor control handle. - * @param rxData Receive buffer - * @param code Instruction code. - */ -static void CMDCODE_EXE_SetMotorInitParams(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData, unsigned char code) -{ - switch (code) { - case CMDCODE_SET_OBSERVER_TYPE: { /* Set observer type. */ - CMDCODE_SetObserverType(mtrCtrl, rxData); - } - break; - case CMDCODE_SET_PID_PARAMS: { /* Set motor pid parameters. */ - CMDCODE_SetMotorPidParams(mtrCtrl, rxData); - } - break; - case CMDCODE_SET_OBSERVER_PARAMS: { /* Set observer parameters. */ - CMDCODE_SetObserverParams(mtrCtrl, rxData); - } - break; - case CMDCODE_SET_MOTOR_TARGETSPD: { /* Set motor speed and speed slope. */ - CMDCODE_SetMotorSpdAndSlope(mtrCtrl, rxData); - } - break; - case CMDCODE_SET_MOTOR_PARAMS: { /* Set motor parameters. */ - CMDCODE_SetMotorParams(mtrCtrl, rxData); - } - break; - case CMDCODE_SET_ADJUSTSPD_MODE: /* Set adjust speed mode. */ - CMDCODE_SetAdjustSpdMode(mtrCtrl, rxData); - break; - default: - break; - } -} - -/** - * @brief Set Motor Control System Status. - * @param mtrCtrl The motor control handle. - * @param rxData Receive buffer - * @param code Instruction code. - */ -static void CMDCODE_EXE_SetMotorState(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData, unsigned char code) -{ - BASE_FUNC_UNUSED(rxData); - switch (code) { - case CMDCODE_MOTOR_START: { /* Motor start command */ - CMDCODE_MotorStart(mtrCtrl); - } - break; - case CMDCODE_MOTOR_STOP: { /* Motor stop command */ - CMDCODE_MotorStop(mtrCtrl); - } - break; - case CMDCODE_MOTORSTATE_RESET: { /* Motor reset command */ - CMDCODE_MotorReset(mtrCtrl); - } - break; - default: - break; - } -} - -/** - * @brief Set Startup and Uart Link Handshake Flag Parameters. - * @param mtrCtrl The motor control handle. - * @param rxData Receive buffer - * @param code Instruction code. - */ -static void CMDCODE_EXE_SetOtherParams(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData, unsigned char code) -{ - switch (code) { - case CMDCODE_SET_STARTUP_PARAMS: /* Set start up parameters. */ - CMDCODE_SetStartupParams(mtrCtrl, rxData); - break; - case CMDCODE_UART_HANDSHAKE: { /* Check uart hand shake. */ - CMDCODE_UartHandShake(mtrCtrl, rxData); - } - break; - case CMDCODE_UART_HEARTDETECT: { /* Check uart hand shake. */ - mtrCtrl->uartHeartDetCnt++; - } - break; - default: - break; - } -} -/** - * @brief Instruction code executor. - * @param mtrCtrl The motor control handle. - * @param rxData Receive buffer - * @param code Instruction code. - */ -static void CMDCODE_EXE_Process(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData, unsigned char code) -{ - CMDCODE_EXE_SetMotorInitParams(mtrCtrl, rxData, code); - CMDCODE_EXE_SetMotorState(mtrCtrl, rxData, code); - CMDCODE_EXE_SetOtherParams(mtrCtrl, rxData, code); -} - -/** - * @brief Host data download callback and data parsing. - * @param mtrCtrl The motor control handle. - * @param rxBuf Receive buffer - */ -void CUST_UartDataProcess(MTRCTRL_Handle *mtrCtrl, unsigned char *rxBuf) -{ - /* Verify Parameters */ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - MCS_ASSERT_PARAM(rxBuf != NULL); - - /* Uart data storage struct */ - CUSTDATATYPE_DEF data; - volatile unsigned char *ptrCnt = &rxBuf[FRAME_CHECK_BEGIN + 1]; - volatile char *strCnt = &data.data[0].typeCh[0]; - for (unsigned int j = 0; j < FRAME_RECV_DATA_LENTH * FRAME_ONE_DATA_LENTH; j++) { - *strCnt++ = *ptrCnt++; - } - /* Message function code. */ - data.code = rxBuf[FRAME_CHECK_BEGIN]; - CMDCODE_EXE_Process(mtrCtrl, &data, data.code); -} - -/** - * @brief The host computer displays data transmission. - * @param mtrCtrl The motor control handle. - * @param txData Message content. - * @param stage Message status function code. - */ -void CUST_SetTxMsg(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *txData) -{ - /* Verify Parameters */ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - MCS_ASSERT_PARAM(txData != NULL); - /* Data send to host. */ - txData->data[CURRDQ_Q].typeF = mtrCtrl->idqFbk.q; - txData->data[CURRDQ_D].typeF = mtrCtrl->idqFbk.d; - txData->data[CURRREFDQ_Q].typeF = mtrCtrl->idqRef.q; - txData->data[CURRREFDQ_D].typeF = mtrCtrl->idqRef.d; - /* Motor current speed. */ - txData->data[CURRSPD].typeF = mtrCtrl->encSpeed * CONST_VALUE_60 / mtrCtrl->mtrParam.mtrNp; - /* Motor commond speed. */ - txData->data[SPDCMDHZ].typeF = mtrCtrl->spdCmdHz * CONST_VALUE_60 / mtrCtrl->mtrParam.mtrNp; - /* Bus voltage. */ - txData->data[UDC].typeF = mtrCtrl->udc; - /* Power board temprature. */ - txData->data[POWERBOARDTEMP].typeF = mtrCtrl->powerBoardTemp; - /* Motor protection status flag. */ - txData->data[CUST_ERR_CODE].typeI = mtrCtrl->prot.motorErrStatus.all; - /* Three phase current. */ - txData->data[CURRUVW_U].typeF = mtrCtrl->currUvw.u; - txData->data[CURRUVW_V].typeF = mtrCtrl->currUvw.v; - txData->data[CURRUVW_W].typeF = mtrCtrl->currUvw.w; - /* Three phase pwm duty. */ - txData->data[PWMDUTYUVW_U].typeF = mtrCtrl->dutyUvw.u; - txData->data[PWMDUTYUVW_V].typeF = mtrCtrl->dutyUvw.v; - txData->data[PWMDUTYUVW_W].typeF = mtrCtrl->dutyUvw.w; - /* Motor electric angle. */ - txData->data[AXISANGLE].typeF = mtrCtrl->axisAngle; - txData->data[VDQ_Q].typeF = mtrCtrl->vdqRef.q; - txData->data[VDQ_D].typeF = mtrCtrl->vdqRef.d; - txData->data[SPDREFHZ].typeF = mtrCtrl->spdRefHz * CONST_VALUE_60 / mtrCtrl->mtrParam.mtrNp; - txData->data[SENDTIMESTAMP].typeF = mtrCtrl->uartTimeStamp; -} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/user_interface/cust_process.h b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/user_interface/cust_process.h deleted file mode 100644 index 60fe69bb..00000000 --- a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/user_interface/cust_process.h +++ /dev/null @@ -1,36 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file cust_process.h - * @author MCU Algorithm Team - * @brief This file provides functions declaration of system status. - */ - -/* Define to prevent recursive inclusion ------------------------------------------------------- */ -#ifndef McsMagicTag_HMI_MOUDLE_H -#define McsMagicTag_HMI_MOUDLE_H - -/* Includes ------------------------------------------------------------------------------------ */ -#include "protocol.h" -/* Typedef definitions ------------------------------------------------------------------------- */ - - -void CUST_UartDataProcess(MTRCTRL_Handle *mtrCtrl, unsigned char *rxBuf); - -void CUST_SetTxMsg(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *txData); - -#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/user_interface/hmi_module.c b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/user_interface/hmi_module.c deleted file mode 100644 index f6f762ad..00000000 --- a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/user_interface/hmi_module.c +++ /dev/null @@ -1,58 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file hmi_module.c - * @author MCU Algorithm Team - * @brief This file provides functions declaration of human-machine interface. - */ - -#include "hmi_module.h" -#include "mcs_assert.h" - -/** - * @brief HMI Initializatio. - * @retval None. - */ -void HMI_Init(void) -{ - UartRecvInit(); - -} - -/** - * @brief HMI processing. - * @param mtrCtrl The motor control handle.. - * @retval None. - */ -void HMI_Process_Rx(MTRCTRL_Handle *mtrCtrl) -{ - /* Verify Parameters */ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - UartModuleProcess_Rx(mtrCtrl); -} - -/** - * @brief HMI processing. - * @param mtrCtrl The motor control handle.. - * @retval None. - */ -void HMI_Process_Tx(MTRCTRL_Handle *mtrCtrl) -{ - /* Verify Parameters */ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - UartModuleProcess_Tx(mtrCtrl); -} diff --git a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/user_interface/hmi_module.h b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/user_interface/hmi_module.h deleted file mode 100644 index 056f8d73..00000000 --- a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/user_interface/hmi_module.h +++ /dev/null @@ -1,37 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file hmi_module.h - * @author MCU Algorithm Team - * @brief This file provides functions declaration of system status. - */ - -/* Define to prevent recursive inclusion ------------------------------------------------------- */ -#ifndef McsMagicTag_HMI_MOUDLE_H -#define McsMagicTag_HMI_MOUDLE_H - -/* Includes ------------------------------------------------------------------------------------ */ -#include "uart_module.h" -/* Typedef definitions ------------------------------------------------------------------------- */ - - -void HMI_Init(void); - -void HMI_Process_Rx(MTRCTRL_Handle *mtrCtrl); -void HMI_Process_Tx(MTRCTRL_Handle *mtrCtrl); - -#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/user_interface/protocol.c b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/user_interface/protocol.c deleted file mode 100644 index 987b4c5a..00000000 --- a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/user_interface/protocol.c +++ /dev/null @@ -1,174 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file protocol.c - * @author MCU Algorithm Team - * @brief This file provides functions declaration of port communication. - */ - -#include "protocol.h" -#include "apt.h" -#include "typedefs.h" -#include "main.h" -#include "mcs_assert.h" -#include "cust_process.h" - -/** - * @brief Callback function for receiving data analysis and processing. - * @param rxBuf Receive buffer. - */ -__weak void CUST_UartDataProcess(MTRCTRL_Handle *mtrCtrl, unsigned char *rxBuf) -{ - BASE_FUNC_UNUSED(mtrCtrl); - BASE_FUNC_UNUSED(rxBuf); -} -/** - * @brief User-defined protocol message sending function (weak function). - * @param rxData Sending Messages.. - */ -__weak void CUST_SetTxMsg(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *txData) -{ - BASE_FUNC_UNUSED(mtrCtrl); - BASE_FUNC_UNUSED(*txData); -} - -static void (*g_ptrDataProcess)(MTRCTRL_Handle *mtrCtrl, unsigned char *rxBuf) = CUST_UartDataProcess; - -/** - * @brief Frame checksum. - * @param ptr Pointer to the data to be checked - * @param num Number of bytes - * @retval unsigned char Checksum - */ -static unsigned char CheckSum(unsigned char *ptr, unsigned char num) -{ - unsigned char sum = 0; - unsigned char *p = ptr; - /* Calculate the sum of received data. */ - for (int i = 0; i < num; i++) { - sum += *p; - p++; - } - return sum; -} - -/** - * @brief Transmitting Data Frames. - * @param mtrCtrl The motor control handle. - * @param txBuf Sending Messages. - */ -unsigned int CUST_TransmitData(MTRCTRL_Handle *mtrCtrl, unsigned char *txBuf) -{ - /* Verify Parameters */ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - MCS_ASSERT_PARAM(txBuf != NULL); - unsigned int dataLen = FRAME_ONE_DATA_LENTH * SEND_FRAME_DATA_NUM; - unsigned int i = 0; - CUSTDATATYPE_DEF txData = {0}; - CUST_SetTxMsg(mtrCtrl, &txData); - txBuf[i++] = FRAME_START; - /* Message function code */ - txBuf[i++] = FRAME_SENT; - /* Message data */ - for (unsigned char x = 0; x < SEND_FRAME_DATA_NUM; x++) { - int floatIndex = 0; - int byteOffset = i; - txBuf[x * FRAME_ONE_DATA_LENTH + byteOffset++] = txData.data[x].typeCh[floatIndex++]; - txBuf[x * FRAME_ONE_DATA_LENTH + byteOffset++] = txData.data[x].typeCh[floatIndex++]; - txBuf[x * FRAME_ONE_DATA_LENTH + byteOffset++] = txData.data[x].typeCh[floatIndex++]; - txBuf[x * FRAME_ONE_DATA_LENTH + byteOffset++] = txData.data[x].typeCh[floatIndex++]; - } - /* Message verification domain */ - txBuf[dataLen + i++] = CheckSum((unsigned char*)&txBuf[FRAME_CHECK_BEGIN], dataLen + 1); - /* End of Message */ - txBuf[dataLen + i++] = FRAME_END; - return dataLen + i; -} - -/** - * @brief Transmitting Data Frames. - * @param txBuf Sending Cust Ack Code. - * @param ackCode Ack Code. - * @param varParams Host set parameter. - */ -void CUST_AckCode(unsigned char *txBuf, unsigned char ackCode, float varParams) -{ - /* Verify Parameters */ - MCS_ASSERT_PARAM(txBuf != NULL); - CUSTDATATYPE_DEF txData = {0}; - int dataIndex = 0; - unsigned int i = 0; - unsigned int txLen = 0; - unsigned int dataLen = FRAME_ONE_CHAR_LENTH + FRAME_ONE_DATA_LENTH; - - txData.data[0].typeF = varParams; - txBuf[i++] = FRAME_START; - /* Message function code */ - txBuf[i++] = FRAME_CUSTACK; - /* Message ack code */ - txBuf[i++] = ackCode; - /* Message data */ - txBuf[i++] = txData.data[0].typeCh[dataIndex++]; - txBuf[i++] = txData.data[0].typeCh[dataIndex++]; - txBuf[i++] = txData.data[0].typeCh[dataIndex++]; - txBuf[i++] = txData.data[0].typeCh[dataIndex++]; - - /* Message verification domain */ - txBuf[FRAME_ONE_CHAR_LENTH + i++] = CheckSum((unsigned char*)&txBuf[FRAME_CHECK_BEGIN], dataLen + 1); - /* End of Message */ - txBuf[FRAME_ONE_CHAR_LENTH + i++] = FRAME_END; - txLen = FRAME_ONE_CHAR_LENTH + i++; - HAL_UART_WriteIT(&g_uart0, txBuf, txLen); -} - -/** - * @brief Cust receive data process. - * @param mtrCtrl The motor control handle. - * @param rxBuf Receive buffer - */ -void CUST_DataReceProcss(MTRCTRL_Handle *mtrCtrl, unsigned char *rxBuf) -{ - /* Verify Parameters */ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - MCS_ASSERT_PARAM(rxBuf != NULL); - unsigned char g_uartTxBuf[10] = {0}; - unsigned char ackCode = 0; - /* Frame header check */ - if (rxBuf[0] != FRAME_START) { - ackCode = 0X78; - CUST_AckCode(g_uartTxBuf, ackCode, 0); - return; - } - /* Frame trailer check */ - if (rxBuf[FRAME_LENTH - 1] != FRAME_END) { - ackCode = 0X79; - CUST_AckCode(g_uartTxBuf, ackCode, 0); - return; - } - /* Checksum */ - if (CheckSum((unsigned char*)&rxBuf[FRAME_CHECK_BEGIN], FRAME_CHECK_NUM) != rxBuf[FRAME_CHECKSUM]) { - ackCode = 0X7A; - CUST_AckCode(g_uartTxBuf, ackCode, 0); - return; - } else { - if (g_ptrDataProcess == NULL) { - return; - } else { - g_ptrDataProcess(mtrCtrl, rxBuf); - } - } -} diff --git a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/user_interface/protocol.h b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/user_interface/protocol.h deleted file mode 100644 index 33f0f49c..00000000 --- a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/user_interface/protocol.h +++ /dev/null @@ -1,121 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file protocol.h - * @author MCU Algorithm Team - * @brief This file provides functions declaration of port communication. - */ -#ifndef McsMagicTag_PORTOCOL_H -#define McsMagicTag_PORTOCOL_H - -#include "uart.h" -#include "mcs_carrier.h" - -#define RS485_SEND_ENABLE GPIO6->GPIO_DATA[GPIO_PIN_7].reg = GPIO_PIN_7 -#define RS485_SEND_DISABLE GPIO6->GPIO_DATA[GPIO_PIN_7].reg = BASE_CFG_UNSET - -#define RX_BUF_LEN (16) -#define SEND_FRAME_DATA_NUM (CUSTDATA_MAX) -/* Service Uart0 Communication Deal */ -#define FRAME_ONE_DATA_LENTH 4 -#define FRAME_ONE_CHAR_LENTH 1 -#define FRAME_RECV_DATA_LENTH 4 -#define FRAME_LENTH 20 /* Data length */ -#define FRAME_SENT 0X8F -#define FRAME_CUSTACK 0X8A -#define FRAME_START 0x0F /* Start frame */ -#define FRAME_END '/' /* StOP frame */ -#define FRAME_CHECK_BEGIN 1 /* Check frame */ -#define FRAME_CHECKSUM 18 /* Check sum */ -#define FRAME_CHECK_NUM 17 -#define CMDCODE_IDLE_FRAME 0x55 /* Fill frame */ -#define CMDCODE_GET_MOTOR_PARAMS 0x01 -#define CMDCODE_SEND_MOTOR_PARAMS 0x02 -#define CMDCODE_SET_MOTOR_CTLMODE 0x03 -#define CMDCODE_SET_OBSERVER_TYPE 0x04 -#define CMDCODE_SET_STARTUP_MODE 0x05 -#define CMDCODE_SET_PID_PARAMS 0x06 -#define CMDCODE_SET_STARTUP_PARAMS 0x07 -#define CMDCODE_SET_OBSERVER_PARAMS 0x08 -#define CMDCODE_SET_MOTOR_TARGETSPD 0x09 -#define CMDCODE_SET_MOTOR_PARAMS 0x0A -#define CMDCODE_MOTOR_START 0x0B -#define CMDCODE_MOTOR_STOP 0x0C -#define CMDCODE_MOTORSTATE_RESET 0x0D -#define CMDCODE_SEND_FIRMVERSION 0x0E -#define CMDCODE_SET_ADJUSTSPD_MODE 0x11 -#define CMDCODE_UART_HANDSHAKE 0x12 -#define CMDCODE_UART_HEARTDETECT 0x13 - -typedef union { - char typeCh[4]; - float typeF; - int typeI; -} UNIONDATATYPE_DEF; - -typedef enum { - OFFLINE_RES = 0, - OFFLINE_LD, - OFFLINE_LQ, - OFFLINE_PSIF, - OFFLINE_JS, - OFFLINE_NP, - OFFLINE_B, - OFFLINE_KPD, - OFFLINE_KID, - OFFLINE_KPQ, - OFFLINE_KIQ, - OFFLINE_KPS, - OFFLINE_KIS, - OFFLINE_SPEED, - OFLINE_MAX -} OFFLINE_IDEN_TYPE; - -typedef enum { - CURRDQ_Q = 0, - CURRDQ_D, - CURRREFDQ_Q, - CURRREFDQ_D, - CURRSPD, - SPDCMDHZ, - UDC, - POWERBOARDTEMP, - CUST_ERR_CODE, - CURRUVW_U, - CURRUVW_V, - CURRUVW_W, - PWMDUTYUVW_U, - PWMDUTYUVW_V, - PWMDUTYUVW_W, - AXISANGLE, - VDQ_Q, - VDQ_D, - SPDREFHZ, - SENDTIMESTAMP, - CUSTDATA_MAX -} SENDTOHOSTPARAMS; - -typedef struct { - volatile unsigned char code; - volatile UNIONDATATYPE_DEF data[SEND_FRAME_DATA_NUM]; -} CUSTDATATYPE_DEF; - - -void CUST_DataReceProcss(MTRCTRL_Handle *mtrCtrl, unsigned char *rxBuf); -unsigned int CUST_TransmitData(MTRCTRL_Handle *mtrCtrl, unsigned char *txBuf); -void CUST_AckCode(unsigned char *txBuf, unsigned char ackCode, float varParams); -#endif /* McsMagicTag_PORTOCOL_H */ diff --git a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/user_interface/uart_module.c b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/user_interface/uart_module.c deleted file mode 100644 index 8ded8180..00000000 --- a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/user_interface/uart_module.c +++ /dev/null @@ -1,234 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the - * following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file uart_module.c - * @author MCU Algorithm Team - * @brief This file provides functions declaration of Serial port communication. - */ -#include "uart_module.h" -#include "debug.h" -#include "main.h" -#include "baseinc.h" -#include -/* Buffer size */ -#define UI_TX_BUF_LEN (96) -#define UI_RX_BUF_LEN (96) - -/* Receiving Timeout Interval */ -#define UART_TIME_OUT_MS (100) - -/* Start sending data to host delay after uart connect success */ -#define UART_UPDATA_DELAY_TIME_MS (50) - -/* Uart baudrate */ -#define UART0BAUDRATE (115200) - -/* Data buffer */ -unsigned char g_uartRxBuf[UI_RX_BUF_LEN] = {0}; -unsigned char g_uartTxBuf[UI_TX_BUF_LEN] = {0}; -static unsigned int getdeltaSystickCnt = 0; -static FRAME_Handle g_uartFrame; -__UINT32_TYPE__ uart_spdcmd;//串口速度命令 -double uart_poscmd; //串口位置命令 - -/** - * @brief Receive Data Clear. - * @param uartFrame Receice Data. - */ -static void FrameRecvClear(FRAME_Handle *uartFrame) -{ - /* Clear buffer lenth. */ - uartFrame->buffLen = 0; - uartFrame->timeOutCnt = 0; - uartFrame->frameFlag = 0; - /* Clear received data lenth. */ - uartFrame->rxLen = 0; - /* Clear received flag. */ - uartFrame->rxFlag = 0; - uartFrame->upDataCnt = 0; -} - -/** - * @brief Set Dma status. - * @param mtrCtrl The motor control handle. - */ -static void SetUartDmaStatus(MTRCTRL_Handle *mtrCtrl) -{ - /* Delay 50ms start uart Tx DMA . */ - if (mtrCtrl->uartConnectFlag == CONNECTING && g_uartFrame.upDataDelayCnt++ > UART_UPDATA_DELAY_TIME_MS) { - g_uartFrame.txFlag = 1; /* Start send data flag. */ - mtrCtrl->uartConnectFlag = CONNECTED; - g_uartFrame.upDataDelayCnt = 0; - } - if (mtrCtrl->uartConnectFlag == DISCONNECT) { - g_uartFrame.txFlag = 0; /* Stop send data flag. */ - mtrCtrl->uartTimeStamp = 0; - } -} - -/** - * @brief Set uart baudRate. - * @param baudrate Uart baudRate. - */ -static void SetUartBaudRate(unsigned int baudrate) -{ - /* Re_Write uart0 baudrate. */ - g_uart0.baudRate = baudrate; - HAL_UART_Init(&g_uart0); -} - -/** - * @brief Uart Dma interupt callback func. - * @param null. - */ -void UART0_TXDMACallback(void *handle) -{ - BASE_FUNC_UNUSED(handle); - unsigned int getCurSystickCnt = 0; - static unsigned int getlastSystickCnt = 0; - /* USER CODE BEGIN UART0_WRITE_DMA_FINISH */ - g_uartFrame.txFlag = 1; - getCurSystickCnt = DCL_SYSTICK_GetTick(); - if (getlastSystickCnt != 0) { - /* Calculate unit frame data send time */ - getdeltaSystickCnt = getCurSystickCnt - getlastSystickCnt; - } - getlastSystickCnt = getCurSystickCnt; - /* USER CODE END UART0_WRITE_DMA_FINISH */ -} - -/** - * @brief Uart0 interruput Write CallBack Function. - * @param handle Uart handle. - */ -void UART0WriteInterruptCallback(void *handle) -{ - BASE_FUNC_UNUSED(handle); - /* USER CODE BEGIN UART0_WRITE_IT_FINISH */ - g_uartFrame.uartItTxFlag = 1; - g_uartFrame.txFlag = 1; - /* USER CODE END UART0_WRITE_IT_FINISH */ -} - -__UINT32_TYPE__ ascii_to_int_manual(const __UINT8_TYPE__ *buf, __UINT8_TYPE__ len) { - __UINT32_TYPE__ result = 0; - bool is_negative = false; - __UINT8_TYPE__ i = 0; - - // 检查符号位 - if (len > 0 && buf[0] == '-') { - is_negative = true; - i = 1; // 跳过符号位 - } - - // 逐字符转换 - for (; i < len; i++) { - if (buf[i] < '0' || buf[i] > '9') { - break; // 遇到非数字字符停止 - } - - // 检查溢出 - // if (result > (INT32_MAX - (buf[i] - '0')) / 10) { - // return is_negative ? INT32_MIN : INT32_MAX; - // } - - result = result * 10 + (buf[i] - '0'); - } - - return is_negative ? -result : result; -} - - - -/** - * @brief Uart0 Interruput Read CallBack Function. - * @param handle Uart handle. - */ -void UART0ReadInterruptCallback(void *handle) -{ - BASE_FUNC_UNUSED(handle); - /* USER CODE BEGIN UART0_READ_IT_FINISH */ - if (g_uartFrame.rxLen >= UI_RX_BUF_LEN - 1) { - g_uartFrame.rxLen = 0; - } - HAL_UART_ReadIT(handle, &g_uartFrame.rxData, 1); - g_uartRxBuf[g_uartFrame.rxLen] = g_uartFrame.rxData; - - uart_spdcmd=ascii_to_int_manual(g_uartRxBuf, 3);//字符串转整型 - uart_poscmd=atof(g_uartRxBuf);//字符串转浮点 - g_uartFrame.rxLen++; - g_uartFrame.rxFlag = 1; - g_uartFrame.uartItTxFlag = 0; - return; - /* USER CODE END UART0_READ_IT_FINISH */ -} - -/** - * @brief Uart Read Data Init Function. - * @param void. - */ -void UartRecvInit(void) -{ - /* Uart reception initialization */ - FrameRecvClear(&g_uartFrame); - SetUartBaudRate(UART0BAUDRATE); - HAL_UART_ReadIT(&g_uart0, &g_uartFrame.rxData, 1); -} - -/** - * @brief Uart Read Data Process Function. - * @param mtrCtrl The motor control handle. - */ -void UartModuleProcess_Rx(MTRCTRL_Handle *mtrCtrl) -{ - /* Verify Parameters */ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - SetUartDmaStatus(mtrCtrl); - if (g_uartFrame.rxFlag == 1) { /* Receive data flag. */ - if (g_uartFrame.timeOutCnt++ > UART_TIME_OUT_MS) { - /* Received data from the host. */ - g_uartFrame.frameFlag = 1; - g_uartFrame.rxFlag = 0; - g_uartFrame.timeOutCnt = 0; - /* Execute data process. */ - CUST_DataReceProcss(mtrCtrl, g_uartRxBuf); - g_uartFrame.rxLen = 0; - } - } - g_uartFrame.frameFlag = 0; -} - -/** - * @brief Uart Write Data Process Function. - * @param mtrCtrl The motor control handle. - */ -void UartModuleProcess_Tx(MTRCTRL_Handle *mtrCtrl) -{ - /* Verify Parameters */ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - if (g_uartFrame.txFlag == 1) { /* Send data flag. */ - mtrCtrl->uartTimeStamp = (float)getdeltaSystickCnt; /* Unit data time stamp */ - g_uartFrame.upDataCnt = 0; - g_uartFrame.txFlag = 0; - /* Send data to host. */ - unsigned int txLen = CUST_TransmitData(mtrCtrl, g_uartTxBuf); - /* If txIT mode send data finish, convert to DMA mode */ - if (g_uartFrame.uartItTxFlag == 1) { - HAL_UART_WriteDMA(&g_uart0, g_uartTxBuf, txLen); - } - } -} diff --git a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/user_interface/uart_module.h b/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/user_interface/uart_module.h deleted file mode 100644 index fce60120..00000000 --- a/vendor/yibaina_3061M/demo/pmsm_pos_qdm_foc/user/pmsm_encode_qdm_2shunt_foc/user_interface/uart_module.h +++ /dev/null @@ -1,48 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file uart_module.h - * @author MCU Algorithm Team - * @brief This file provides functions declaration of Serial port communication. - */ -#ifndef McsMagicTag_UART_MODULE_H -#define McsMagicTag_UART_MODULE_H - -#include "protocol.h" -#include "mcs_ctlmode_config.h" - -extern __UINT32_TYPE__ uart_spdcmd; -extern double uart_poscmd; -typedef struct { - unsigned int buffLen; - unsigned int timeOutCnt; - unsigned char frameFlag; - unsigned int rxLen; - unsigned char rxFlag; - unsigned char txFlag; - unsigned char rxData; - unsigned int upDataCnt; - unsigned int upDataDelayCnt; - unsigned char uartItTxFlag; -} FRAME_Handle; - - -void UartRecvInit(void); -void UartModuleProcess_Rx(MTRCTRL_Handle *mtrCtrl); -void UartModuleProcess_Tx(MTRCTRL_Handle *mtrCtrl); - -#endif \ No newline at end of file -- Gitee From 621063a439714481cb0980e26c16db13d3700335 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B8=A9=E8=80=81=E5=B8=88?= <1161625498@qq.com> Date: Tue, 3 Jun 2025 16:22:11 +0800 Subject: [PATCH 3/5] update readme.md --- README.md | 20 +- vendor/yibaina_3061M/demo/10025_FOC/readme.md | 20 - .../10025_FOC/user/generatecode/system_init.c | 677 ------------- .../inc/mcs_carrier.h | 142 --- .../inc/mcs_chip_config.h | 76 -- .../inc/mcs_ctlmode_config.h | 73 -- .../inc/mcs_status.h | 188 ---- .../inc/mcs_user_config.h | 107 -- .../protection/common/mcs_prot_cmm.c | 43 - .../protection/common/mcs_prot_cmm.h | 136 --- .../protection/common/mcs_prot_user.c | 137 --- .../protection/common/mcs_prot_user.h | 54 - .../protection/common/mcs_prot_user_config.h | 125 --- .../protection/mcs_curr_prot.c | 267 ----- .../protection/mcs_curr_prot.h | 52 - .../protection/mcs_dc_volt_prot.c | 495 --------- .../protection/mcs_dc_volt_prot.h | 69 -- .../protection/mcs_motor_stalling.c | 112 --- .../protection/mcs_motor_stalling.h | 45 - .../protection/mcs_temp_prot.c | 262 ----- .../protection/mcs_temp_prot.h | 49 - .../src/mcs_carrier.c | 144 --- .../src/mcs_motor_process.c | 950 ------------------ .../user_interface/cust_process.c | 718 ------------- .../user_interface/hmi_module.c | 57 -- .../user_interface/hmi_module.h | 37 - .../user_interface/protocol.c | 174 ---- .../user_interface/protocol.h | 121 --- .../user_interface/uart_module.c | 184 ---- .../user_interface/uart_module.h | 47 - .../generatecode/feature.h | 1 - .../generatecode/main.h | 41 +- .../generatecode/system_init.c | 124 +++ .../gpio_key_sample.c} | 53 +- .../gpio_key_sample.h} | 33 +- .../user => sample_gpio_key}/main.c | 16 +- .../demo/sample_gpio_key/readme.md | 78 ++ 37 files changed, 265 insertions(+), 5662 deletions(-) delete mode 100644 vendor/yibaina_3061M/demo/10025_FOC/readme.md delete mode 100644 vendor/yibaina_3061M/demo/10025_FOC/user/generatecode/system_init.c delete mode 100644 vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_carrier.h delete mode 100644 vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_chip_config.h delete mode 100644 vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_ctlmode_config.h delete mode 100644 vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_status.h delete mode 100644 vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_user_config.h delete mode 100644 vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_cmm.c delete mode 100644 vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_cmm.h delete mode 100644 vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user.c delete mode 100644 vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user.h delete mode 100644 vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user_config.h delete mode 100644 vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_curr_prot.c delete mode 100644 vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_curr_prot.h delete mode 100644 vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_dc_volt_prot.c delete mode 100644 vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_dc_volt_prot.h delete mode 100644 vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_motor_stalling.c delete mode 100644 vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_motor_stalling.h delete mode 100644 vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_temp_prot.c delete mode 100644 vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_temp_prot.h delete mode 100644 vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/src/mcs_carrier.c delete mode 100644 vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/src/mcs_motor_process.c delete mode 100644 vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/cust_process.c delete mode 100644 vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/hmi_module.c delete mode 100644 vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/hmi_module.h delete mode 100644 vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/protocol.c delete mode 100644 vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/protocol.h delete mode 100644 vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/uart_module.c delete mode 100644 vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/uart_module.h rename vendor/yibaina_3061M/demo/{10025_FOC/user => sample_gpio_key}/generatecode/feature.h (99%) rename vendor/yibaina_3061M/demo/{10025_FOC/user => sample_gpio_key}/generatecode/main.h (70%) create mode 100644 vendor/yibaina_3061M/demo/sample_gpio_key/generatecode/system_init.c rename vendor/yibaina_3061M/demo/{10025_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/cust_process.h => sample_gpio_key/gpio_key_sample.c} (55%) rename vendor/yibaina_3061M/demo/{10025_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_motor_process.h => sample_gpio_key/gpio_key_sample.h} (76%) rename vendor/yibaina_3061M/demo/{10025_FOC/user => sample_gpio_key}/main.c (89%) create mode 100644 vendor/yibaina_3061M/demo/sample_gpio_key/readme.md diff --git a/README.md b/README.md index 7a39b4b1..697e525b 100644 --- a/README.md +++ b/README.md @@ -6,17 +6,17 @@ ## 购买渠道 -| 名称 | 硬件资料 | 介绍 | +| 名称 | 购买链接 | 介绍 | | :------------: | ------------------------------------------------------------ | ------------------------------- | -| V5A20002NOOJDB | [购买链接](https://www.ickey.cn/detail/1003001016603884/V5A20002NOOJDB.html#3a190c21-a8e5-40b7-8e88-b7a4b6a9d0b1) | 3061M系列解决方案 128KB QFN48 | -| V5A20002NOOJLB | [购买链接](https://www.ickey.cn/detail/1003001016603886/V5A20002NOOJLB.html#208f8e9e-5cdf-4353-988d-d1df20bbb33f) | 3061M系列解决方案 128KB QFN32 | -| V5A20002NOQJLB | [购买链接](https://www.ickey.cn/detail/1003001016603885/V5A20002NOQJLB.html#208f8e9e-5cdf-4353-988d-d1df20bbb33f) | 3061M系列解决方案 128KB QFP32 | -| V5A20002NOOJDB | [购买链接](https://www.ickey.cn/detail/1003001016603884/V5A20002NOOJDB.html#208f8e9e-5cdf-4353-988d-d1df20bbb33f) | 3061M系列解决方案 128KB QFN48 | -| V5A20002NOOJD9 | [购买链接](https://www.ickey.cn/detail/1003001016603888/V5A20002NOOJD9.html#d9fd2c53-3b07-4b62-ba26-cb59ae94234f) | 3061M系列解决方案 64KB QFN48 | -| V5A20002NOQJD9 | [购买链接](https://www.ickey.cn/detail/1003001016603887/V5A20002NOQJD9.html#208f8e9e-5cdf-4353-988d-d1df20bbb33f) | 3061M系列解决方案 64KB QFP48 | -| V5A20002NOQJL9 | [购买链接](https://www.ickey.cn/detail/1003001016603889/V5A20002NOQJL9.html#208f8e9e-5cdf-4353-988d-d1df20bbb33f) | 3061M系列解决方案 64KB QFP32 | -| V5A20002NOOJL9 | [购买链接](https://www.ickey.cn/detail/1003001016603890/V5A20002NOOJL9.html#208f8e9e-5cdf-4353-988d-d1df20bbb33f) | 3061M系列解决方案 64KB QFN32 | -| V5A20001ISQJSA | [购买链接](https://www.ickey.cn/detail/1003001016603882/V5A20001ISQJSA.html#) | 3065H系列解决方案 160KB LQFP64 | +| V5A20002NOQJDB | [批量购买]([V5A20002NOQJDB价格_V5A20002NOQJDB库存_V5A20002NOQJDB采购信息-云汉芯城 ICkey.cn](https://search.ickey.cn/?keyword=V5A20002NOQJDB&bom_ab=null)) | 3061M系列解决方案 128KB QFP48 | +| V5A20002NOOJLB | [批量购买](https://www.ickey.cn/detail/1003001016603886/V5A20002NOOJLB.html#208f8e9e-5cdf-4353-988d-d1df20bbb33f) [少量购买](https://www.ickey.cn/detail/1003001021937018/V5A20002NOOJLB.html#a48f53a5-b3cf-4d3e-8254-e91505f16fae) | 3061M系列解决方案 128KB QFN32 | +| V5A20002NOQJLB | [批量购买](https://www.ickey.cn/detail/1003001016603885/V5A20002NOQJLB.html#208f8e9e-5cdf-4353-988d-d1df20bbb33f) [少量购买](https://www.ickey.cn/detail/1003001017510002/V5A20002NOQJLB.html#a48f53a5-b3cf-4d3e-8254-e91505f16fae) | 3061M系列解决方案 128KB QFP32 | +| V5A20002NOOJDB | [批量购买](https://www.ickey.cn/detail/1003001016603884/V5A20002NOOJDB.html#208f8e9e-5cdf-4353-988d-d1df20bbb33f) | 3061M系列解决方案 128KB QFN48 | +| V5A20002NOOJD9 | [批量购买](https://www.ickey.cn/detail/1003001016603888/V5A20002NOOJD9.html#d9fd2c53-3b07-4b62-ba26-cb59ae94234f) [少量购买](https://www.ickey.cn/detail/1003001021937055/V5A20002NOOJD9.html#a48f53a5-b3cf-4d3e-8254-e91505f16fae) | 3061M系列解决方案 64KB QFN48 | +| V5A20002NOQJD9 | [批量购买](https://www.ickey.cn/detail/1003001016603887/V5A20002NOQJD9.html#208f8e9e-5cdf-4353-988d-d1df20bbb33f) | 3061M系列解决方案 64KB QFP48 | +| V5A20002NOQJL9 | [批量购买](https://www.ickey.cn/detail/1003001016603889/V5A20002NOQJL9.html#208f8e9e-5cdf-4353-988d-d1df20bbb33f) [少量购买](https://www.ickey.cn/detail/1003001021936978/V5A20002NOQJL9.html#a48f53a5-b3cf-4d3e-8254-e91505f16fae) | 3061M系列解决方案 64KB QFP32 | +| V5A20002NOOJL9 | [批量购买](https://www.ickey.cn/detail/1003001016603890/V5A20002NOOJL9.html#208f8e9e-5cdf-4353-988d-d1df20bbb33f) [少量购买](https://www.ickey.cn/detail/1003001021936989/V5A20002NOOJL9.html#a48f53a5-b3cf-4d3e-8254-e91505f16fae) | 3061M系列解决方案 64KB QFN32 | +| V5A20001ISQJSA | [批量购买](https://www.ickey.cn/detail/1003001016603882/V5A20001ISQJSA.html#) | 3065H系列解决方案 160KB LQFP64 | diff --git a/vendor/yibaina_3061M/demo/10025_FOC/readme.md b/vendor/yibaina_3061M/demo/10025_FOC/readme.md deleted file mode 100644 index 0085a9e2..00000000 --- a/vendor/yibaina_3061M/demo/10025_FOC/readme.md +++ /dev/null @@ -1,20 +0,0 @@ -# 10025_foc - -**【功能描述】** - -- 基于ECMCU105H/ECBMCU201MPC单板的单电机双电阻采样的无感Foc应用 - -**【环境要求】** - -- 所有单板电源改制为演示用的24V低压,电机选用PM120(10025) -- 电机(黄绿蓝)相线分别对应功率板上U/V/W通道接口 - -**【配置方法】** - -- 将user文件夹替换原工程user文件夹即可 - -【操作说明】 - -- 点击开发板按键启动电机 - -- 开发板蓝色电位器,可以调节电机转动的快慢。(1-29HZ) \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_FOC/user/generatecode/system_init.c b/vendor/yibaina_3061M/demo/10025_FOC/user/generatecode/system_init.c deleted file mode 100644 index 3c5e593c..00000000 --- a/vendor/yibaina_3061M/demo/10025_FOC/user/generatecode/system_init.c +++ /dev/null @@ -1,677 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file system_init.c - * @author MCU Driver Team - * @brief This file contains driver init functions. - * @date 2025-04-17 14:22:06 - */ - -#include "main.h" -#include "ioconfig.h" -#include "iocmg_ip.h" - -#define UART0_BAUD_RATE 1843200 - -BASE_StatusType CRG_Config(CRG_CoreClkSelect *coreClkSelect) -{ - CRG_Handle crg; - crg.baseAddress = CRG; - crg.pllRefClkSelect = CRG_PLL_REF_CLK_SELECT_HOSC; - crg.pllPreDiv = CRG_PLL_PREDIV_4; - crg.pllFbDiv = 48; /* PLL Multiplier 48 */ - crg.pllPostDiv = CRG_PLL_POSTDIV_2; - crg.coreClkSelect = CRG_CORE_CLK_SELECT_PLL; - crg.handleEx.pllPostDiv2 = CRG_PLL_POSTDIV2_3; - crg.handleEx.clk1MSelect = CRG_1M_CLK_SELECT_HOSC; - crg.handleEx.clk1MDiv = (25 - 1); /* The 1 MHz freq is equal to the input clock frequency / (clk_1m_div + 1). 25 is the div of the clk_1m in CLOCK. */ - - if (HAL_CRG_Init(&crg) != BASE_STATUS_OK) { - return BASE_STATUS_ERROR; - } - *coreClkSelect = crg.coreClkSelect; - return BASE_STATUS_OK; -} - -static void DMA_Channel0Init(void *handle) -{ - DMA_ChannelParam dma_param; - dma_param.direction = DMA_MEMORY_TO_PERIPH_BY_DMAC; - dma_param.srcAddrInc = DMA_ADDR_INCREASE; - dma_param.destAddrInc = DMA_ADDR_UNALTERED; - dma_param.srcPeriph = DMA_REQUEST_MEM; - dma_param.destPeriph = DMA_REQUEST_UART0_TX; - dma_param.srcWidth = DMA_TRANSWIDTH_BYTE; - dma_param.destWidth = DMA_TRANSWIDTH_BYTE; - dma_param.srcBurst = DMA_BURST_LENGTH_1; - dma_param.destBurst = DMA_BURST_LENGTH_1; - dma_param.pHandle = handle; - HAL_DMA_InitChannel(&g_dmac, &dma_param, DMA_CHANNEL_ZERO); -} - -static void DMA_Init(void) -{ - HAL_CRG_IpEnableSet(DMA_BASE, IP_CLK_ENABLE); - g_dmac.baseAddress = DMA; - IRQ_Register(IRQ_DMA_TC, HAL_DMA_IrqHandlerTc, &g_dmac); - IRQ_Register(IRQ_DMA_ERR, HAL_DMA_IrqHandlerError, &g_dmac); - IRQ_EnableN(IRQ_DMA_TC); - IRQ_EnableN(IRQ_DMA_ERR); - HAL_DMA_Init(&g_dmac); - DMA_Channel0Init((void *)(&g_uart0)); - HAL_DMA_SetChannelPriorityEx(&g_dmac, DMA_CHANNEL_ZERO, DMA_PRIORITY_HIGHEST); -} - -static void ACMP0_Init(void) -{ - HAL_CRG_IpEnableSet(ACMP0_BASE, IP_CLK_ENABLE); /* ACMP clock bit reset. */ - g_acmp0.baseAddress = ACMP0_BASE; - g_acmp0.inOutConfig.inputNNum = ACMP_INPUT_N_SELECT2; - g_acmp0.inOutConfig.inputPNum = ACMP_INPUT_P_SELECT2; - g_acmp0.inOutConfig.polarity = ACMP_OUT_NOT_INVERT; - g_acmp0.filterCtrl.filterMode = ACMP_FILTER_NONE; - g_acmp0.hysteresisVol = ACMP_HYS_VOL_30MV; - g_acmp0.interruptEn = BASE_CFG_UNSET; - HAL_ACMP_Init(&g_acmp0); -} - -static void ADC0_Init(void) -{ - HAL_CRG_IpEnableSet(ADC0_BASE, IP_CLK_ENABLE); - HAL_CRG_IpClkSelectSet(ADC0_BASE, CRG_ADC_CLK_ASYN_PLL_DIV); - HAL_CRG_IpClkDivSet(ADC0_BASE, CRG_ADC_DIV_1); - - g_adc0.baseAddress = ADC0; - g_adc0.socPriority = ADC_PRIMODE_ALL_ROUND; - - HAL_ADC_Init(&g_adc0); - - SOC_Param socParam = {0}; - socParam.adcInput = ADC_CH_ADCINA0; /* PGA0_OUT(ADC AIN0) */ - socParam.sampleTotalTime = ADC_SOCSAMPLE_22CLK; /* adc sample total time 22 adc_clk */ - socParam.trigSource = ADC_TRIGSOC_APT0_SOCA; - socParam.continueMode = BASE_CFG_DISABLE; - socParam.finishMode = ADC_SOCFINISH_NONE; - HAL_ADC_ConfigureSoc(&g_adc0, ADC_SOC_NUM1, &socParam); - - socParam.adcInput = ADC_CH_ADCINA1; /* PGA1_OUT(ADC AIN1) */ - socParam.sampleTotalTime = ADC_SOCSAMPLE_22CLK; /* adc sample total time 22 adc_clk */ - socParam.trigSource = ADC_TRIGSOC_APT0_SOCA; - socParam.continueMode = BASE_CFG_DISABLE; - socParam.finishMode = ADC_SOCFINISH_NONE; - HAL_ADC_ConfigureSoc(&g_adc0, ADC_SOC_NUM6, &socParam); - - socParam.adcInput = ADC_CH_ADCINA9; /* PIN7(ADC AIN9) */ - socParam.sampleTotalTime = ADC_SOCSAMPLE_22CLK; /* adc sample total time 22 adc_clk */ - socParam.trigSource = ADC_TRIGSOC_SOFT; - socParam.continueMode = BASE_CFG_DISABLE; - socParam.finishMode = ADC_SOCFINISH_NONE; - HAL_ADC_ConfigureSoc(&g_adc0, ADC_SOC_NUM0, &socParam); - - socParam.adcInput = ADC_CH_ADCINA5; /* PIN2(ADC AIN5) */ - socParam.sampleTotalTime = ADC_SOCSAMPLE_22CLK; /* adc sample total time 22 adc_clk */ - socParam.trigSource = ADC_TRIGSOC_SOFT; - socParam.continueMode = BASE_CFG_DISABLE; - socParam.finishMode = ADC_SOCFINISH_NONE; - HAL_ADC_ConfigureSoc(&g_adc0, ADC_SOC_NUM3, &socParam); - - socParam.adcInput = ADC_CH_ADCINA15; /* PIN14(ADC AIN15) */ - socParam.sampleTotalTime = ADC_SOCSAMPLE_22CLK; /* adc sample total time 22 adc_clk */ - socParam.trigSource = ADC_TRIGSOC_SOFT; - socParam.continueMode = BASE_CFG_DISABLE; - socParam.finishMode = ADC_SOCFINISH_NONE; - HAL_ADC_ConfigureSoc(&g_adc0, ADC_SOC_NUM4, &socParam); -} - -__weak void MotorSysErrCallback(void *aptHandle) -{ - BASE_FUNC_UNUSED(aptHandle); - /* USER CODE BEGIN APT0_EVENT_INTERRUPT */ - /* USER CODE END APT0_EVENT_INTERRUPT */ -} - -__weak void MotorCarrierProcessCallback(void *aptHandle) -{ - BASE_FUNC_UNUSED(aptHandle); - /* USER CODE BEGIN APT0_TIMER_INTERRUPT */ - /* USER CODE END APT0_TIMER_INTERRUPT */ -} - -static void APT0_ProtectInit(void) -{ - APT_OutCtrlProtectEx protectApt = {0}; - protectApt.ocEventEnEx = BASE_CFG_ENABLE; - protectApt.ocEventModeEx = APT_OUT_CTRL_ONE_SHOT; - protectApt.ocActionEx = APT_OUT_CTRL_ACTION_LOW; - protectApt.ocActionBEx = APT_OUT_CTRL_ACTION_LOW; - protectApt.ocEvtInterruptEnEx = BASE_CFG_ENABLE; - protectApt.ocSysEvent = APT_SYS_EVT_DEBUG | APT_SYS_EVT_CLK | APT_SYS_EVT_MEM; - protectApt.originalEvtEx = APT_EM_ORIGINAL_SRC_ACMP0; - protectApt.evtPolarityMaskEx = APT_EM_ACMP0_INVERT_BIT; - protectApt.filterCycleNumEx = 0; - HAL_APT_ProtectInitEx(&g_apt0, &protectApt); -} - -static void APT0_Init(void) -{ - HAL_CRG_IpEnableSet(APT0_BASE, IP_CLK_ENABLE); - - g_apt0.baseAddress = APT0; - - /* Clock Settings */ - g_apt0.waveform.dividerFactor = 1 - 1; - /* Timer Settings */ - g_apt0.waveform.timerPeriod = 7500; /* 7500 is count period of APT time-base timer */ - g_apt0.waveform.cntMode = APT_COUNT_MODE_UP_DOWN; - - /* Wave Form */ - g_apt0.waveform.basicType = APT_PWM_BASIC_A_HIGH_B_LOW; - g_apt0.waveform.chAOutType = APT_PWM_OUT_BASIC_TYPE; - g_apt0.waveform.chBOutType = APT_PWM_OUT_BASIC_TYPE; - g_apt0.waveform.divInitVal = 0; - g_apt0.waveform.cntInitVal = 0; - g_apt0.waveform.cntCmpLeftEdge = 500; /* 500 is count compare point of the left edge of PWM waveform */ - g_apt0.waveform.cntCmpRightEdge = 4000; /* 4000 is count compare point of the right edge of PWM waveform */ - g_apt0.waveform.cntCmpLoadMode = APT_BUFFER_INDEPENDENT_LOAD; - g_apt0.waveform.cntCmpLoadEvt = APT_COMPARE_LOAD_EVENT_ZERO; - g_apt0.waveform.deadBandCnt = 225; /* 225 is dead-band value */ - - /* ADC Trigger SOCA */ - g_apt0.adcTrg.trgEnSOCA = BASE_CFG_ENABLE; - g_apt0.adcTrg.cntCmpSOCA = 375; /* 375 is count compare point of ADC trigger source SOCA when using CMPA */ - g_apt0.adcTrg.trgSrcSOCA = APT_CS_SRC_CNTR_CMPA_DOWN; - g_apt0.adcTrg.trgScaleSOCA = 1; - - /* ADC Trigger SOCB */ - g_apt0.adcTrg.trgEnSOCB = BASE_CFG_ENABLE; - g_apt0.adcTrg.cntCmpSOCB = 1; - g_apt0.adcTrg.trgSrcSOCB = APT_CS_SRC_CNTR_CMPB_DOWN; - g_apt0.adcTrg.trgScaleSOCB = 1; - - g_apt0.adcTrg.cntCmpLoadMode = APT_BUFFER_INDEPENDENT_LOAD; - g_apt0.adcTrg.cntCmpLoadEvt = APT_COMPARE_LOAD_EVENT_ZERO; - - /* Timer Trigger */ - g_apt0.tmrInterrupt.tmrInterruptEn = BASE_CFG_ENABLE; - g_apt0.tmrInterrupt.tmrInterruptSrc = APT_INT_SRC_CNTR_ZERO; - g_apt0.tmrInterrupt.tmrInterruptScale = 1; - - APT0_ProtectInit(); - - HAL_APT_PWMInit(&g_apt0); - HAL_APT_RegisterCallBack(&g_apt0, APT_EVENT_INTERRUPT, MotorSysErrCallback); - IRQ_SetPriority(IRQ_APT0_EVT, 7); /* 7 is priority value */ - IRQ_Register(IRQ_APT0_EVT, HAL_APT_EventIrqHandler, &g_apt0); - IRQ_EnableN(IRQ_APT0_EVT); - HAL_APT_RegisterCallBack(&g_apt0, APT_TIMER_INTERRUPT, MotorCarrierProcessCallback); - IRQ_SetPriority(IRQ_APT0_TMR, 5); /* 5 is priority value */ - IRQ_Register(IRQ_APT0_TMR, HAL_APT_TimerIrqHandler, &g_apt0); - IRQ_EnableN(IRQ_APT0_TMR); -} - -static void APT1_ProtectInit(void) -{ - APT_OutCtrlProtectEx protectApt = {0}; - protectApt.ocEventEnEx = BASE_CFG_ENABLE; - protectApt.ocEventModeEx = APT_OUT_CTRL_ONE_SHOT; - protectApt.ocActionEx = APT_OUT_CTRL_ACTION_LOW; - protectApt.ocActionBEx = APT_OUT_CTRL_ACTION_LOW; - protectApt.ocEvtInterruptEnEx = BASE_CFG_DISABLE; - protectApt.ocSysEvent = APT_SYS_EVT_DEBUG | APT_SYS_EVT_CLK | APT_SYS_EVT_MEM; - protectApt.originalEvtEx = APT_EM_ORIGINAL_SRC_ACMP0; - protectApt.evtPolarityMaskEx = APT_EM_ACMP0_INVERT_BIT; - protectApt.filterCycleNumEx = 0; - HAL_APT_ProtectInitEx(&g_apt1, &protectApt); -} - -static void APT1_Init(void) -{ - HAL_CRG_IpEnableSet(APT1_BASE, IP_CLK_ENABLE); - - g_apt1.baseAddress = APT1; - - /* Clock Settings */ - g_apt1.waveform.dividerFactor = 1 - 1; - /* Timer Settings */ - g_apt1.waveform.timerPeriod = 7500; /* 7500 is count period of APT time-base timer */ - g_apt1.waveform.cntMode = APT_COUNT_MODE_UP_DOWN; - - /* Wave Form */ - g_apt1.waveform.basicType = APT_PWM_BASIC_A_HIGH_B_LOW; - g_apt1.waveform.chAOutType = APT_PWM_OUT_BASIC_TYPE; - g_apt1.waveform.chBOutType = APT_PWM_OUT_BASIC_TYPE; - g_apt1.waveform.divInitVal = 0; - g_apt1.waveform.cntInitVal = 0; - g_apt1.waveform.cntCmpLeftEdge = 500; /* 500 is count compare point of the left edge of PWM waveform */ - g_apt1.waveform.cntCmpRightEdge = 4000; /* 4000 is count compare point of the right edge of PWM waveform */ - g_apt1.waveform.cntCmpLoadMode = APT_BUFFER_INDEPENDENT_LOAD; - g_apt1.waveform.cntCmpLoadEvt = APT_COMPARE_LOAD_EVENT_ZERO; - g_apt1.waveform.deadBandCnt = 225; /* 225 is dead-band value */ - - APT1_ProtectInit(); - - HAL_APT_PWMInit(&g_apt1); -} - -static void APT2_ProtectInit(void) -{ - APT_OutCtrlProtectEx protectApt = {0}; - protectApt.ocEventEnEx = BASE_CFG_ENABLE; - protectApt.ocEventModeEx = APT_OUT_CTRL_ONE_SHOT; - protectApt.ocActionEx = APT_OUT_CTRL_ACTION_LOW; - protectApt.ocActionBEx = APT_OUT_CTRL_ACTION_LOW; - protectApt.ocEvtInterruptEnEx = BASE_CFG_DISABLE; - protectApt.ocSysEvent = APT_SYS_EVT_DEBUG | APT_SYS_EVT_CLK | APT_SYS_EVT_MEM; - protectApt.originalEvtEx = APT_EM_ORIGINAL_SRC_ACMP0; - protectApt.evtPolarityMaskEx = APT_EM_ACMP0_INVERT_BIT; - protectApt.filterCycleNumEx = 0; - HAL_APT_ProtectInitEx(&g_apt2, &protectApt); -} - -static void APT2_Init(void) -{ - HAL_CRG_IpEnableSet(APT2_BASE, IP_CLK_ENABLE); - - g_apt2.baseAddress = APT2; - - /* Clock Settings */ - g_apt2.waveform.dividerFactor = 1 - 1; - /* Timer Settings */ - g_apt2.waveform.timerPeriod = 7500; /* 7500 is count period of APT time-base timer */ - g_apt2.waveform.cntMode = APT_COUNT_MODE_UP_DOWN; - - /* Wave Form */ - g_apt2.waveform.basicType = APT_PWM_BASIC_A_HIGH_B_LOW; - g_apt2.waveform.chAOutType = APT_PWM_OUT_BASIC_TYPE; - g_apt2.waveform.chBOutType = APT_PWM_OUT_BASIC_TYPE; - g_apt2.waveform.divInitVal = 0; - g_apt2.waveform.cntInitVal = 0; - g_apt2.waveform.cntCmpLeftEdge = 500; /* 500 is count compare point of the left edge of PWM waveform */ - g_apt2.waveform.cntCmpRightEdge = 4000; /* 4000 is count compare point of the right edge of PWM waveform */ - g_apt2.waveform.cntCmpLoadMode = APT_BUFFER_INDEPENDENT_LOAD; - g_apt2.waveform.cntCmpLoadEvt = APT_COMPARE_LOAD_EVENT_ZERO; - g_apt2.waveform.deadBandCnt = 225; /* 225 is dead-band value */ - - APT2_ProtectInit(); - - HAL_APT_PWMInit(&g_apt2); -} - -__weak void MotorStartStopKeyCallback(void *param) -{ - GPIO_Handle *handle = (GPIO_Handle *)param; - BASE_FUNC_UNUSED(handle); -} - -static void GPIO_Init(void) -{ - HAL_CRG_IpEnableSet(GPIO2_BASE, IP_CLK_ENABLE); - g_gpio2.baseAddress = GPIO2; - - g_gpio2.pins = GPIO_PIN_3; - HAL_GPIO_Init(&g_gpio2); - HAL_GPIO_SetDirection(&g_gpio2, g_gpio2.pins, GPIO_OUTPUT_MODE); - HAL_GPIO_SetValue(&g_gpio2, g_gpio2.pins, GPIO_HIGH_LEVEL); - HAL_GPIO_SetIrqType(&g_gpio2, g_gpio2.pins, GPIO_INT_TYPE_NONE); - - g_gpio2.pins = GPIO_PIN_4; - HAL_GPIO_Init(&g_gpio2); - HAL_GPIO_SetDirection(&g_gpio2, g_gpio2.pins, GPIO_INPUT_MODE); - HAL_GPIO_SetValue(&g_gpio2, g_gpio2.pins, GPIO_HIGH_LEVEL); - HAL_GPIO_SetIrqType(&g_gpio2, g_gpio2.pins, GPIO_INT_TYPE_LOW_LEVEL); - - HAL_CRG_IpEnableSet(GPIO1_BASE, IP_CLK_ENABLE); - g_gpio1.baseAddress = GPIO1; - - g_gpio1.pins = GPIO_PIN_0; - HAL_GPIO_Init(&g_gpio1); - HAL_GPIO_SetDirection(&g_gpio1, g_gpio1.pins, GPIO_OUTPUT_MODE); - HAL_GPIO_SetValue(&g_gpio1, g_gpio1.pins, GPIO_HIGH_LEVEL); - HAL_GPIO_SetIrqType(&g_gpio1, g_gpio1.pins, GPIO_INT_TYPE_NONE); - - HAL_GPIO_RegisterCallBack(&g_gpio2, GPIO_PIN_4, MotorStartStopKeyCallback); - IRQ_Register(IRQ_GPIO2, HAL_GPIO_IrqHandler, &g_gpio2); - IRQ_SetPriority(IRQ_GPIO2, 1); /* set gpio1 interrupt priority to 1, 1~15. 1 is priority value */ - IRQ_EnableN(IRQ_GPIO2); /* gpio interrupt enable */ - - return; -} - -static void PGA0_Init(void) -{ - HAL_CRG_IpEnableSet(PGA0_BASE, IP_CLK_ENABLE); - - g_pga0.baseAddress = PGA0_BASE; - g_pga0.externalResistorMode = BASE_CFG_ENABLE; - g_pga0.handleEx.extCapCompensation = PGA_EXT_COMPENSATION_2X; - HAL_PGA_Init(&g_pga0); -} - -static void PGA1_Init(void) -{ - HAL_CRG_IpEnableSet(PGA1_BASE, IP_CLK_ENABLE); - - g_pga1.baseAddress = PGA1_BASE; - g_pga1.externalResistorMode = BASE_CFG_ENABLE; - g_pga1.handleEx.extCapCompensation = PGA_EXT_COMPENSATION_2X; - HAL_PGA_Init(&g_pga1); -} - -__weak void CheckPotentiometerValueCallback(void *handle) -{ - BASE_FUNC_UNUSED(handle); - /* USER CODE BEGIN CheckPotentiometerValueCallback */ - /* USER CODE END CheckPotentiometerValueCallback */ -} - -static void TIMER0_Init(void) -{ - HAL_CRG_IpEnableSet(TIMER0_BASE, IP_CLK_ENABLE); /* TIMER0 clock enable. */ - unsigned int load = ((float)HAL_CRG_GetIpFreq((void *)TIMER0) / (1u << (TIMERPRESCALER_NO_DIV * 4)) / 1000000u) * 1000000; - - g_timer0.baseAddress = TIMER0; - g_timer0.load = (load < 1) ? 0 : (load - 1); /* Set timer value immediately */ - g_timer0.bgLoad = (load < 1) ? 0 : (load - 1); /* Set timer value */ - g_timer0.mode = TIMER_MODE_RUN_PERIODIC; /* Run in period mode */ - g_timer0.prescaler = TIMERPRESCALER_NO_DIV; /* Don't frequency division */ - g_timer0.size = TIMER_SIZE_32BIT; /* 1 for 32bit, 0 for 16bit */ - g_timer0.interruptEn = BASE_CFG_ENABLE; - g_timer0.adcSocReqEnable = BASE_CFG_DISABLE; - g_timer0.dmaReqEnable = BASE_CFG_DISABLE; - HAL_TIMER_Init(&g_timer0); - IRQ_Register(IRQ_TIMER0, HAL_TIMER_IrqHandler, &g_timer0); - - HAL_TIMER_RegisterCallback(&g_timer0, TIMER_PERIOD_FIN, CheckPotentiometerValueCallback); - IRQ_SetPriority(IRQ_TIMER0, 1); /* 1 is priority value */ - IRQ_EnableN(IRQ_TIMER0); -} - -__weak void MotorStatemachineCallBack(void *handle) -{ - BASE_FUNC_UNUSED(handle); - /* USER CODE BEGIN MotorStatemachineCallBack */ - /* USER CODE END MotorStatemachineCallBack */ -} - -static void TIMER1_Init(void) -{ - HAL_CRG_IpEnableSet(TIMER1_BASE, IP_CLK_ENABLE); /* TIMER1 clock enable. */ - unsigned int load = ((float)HAL_CRG_GetIpFreq((void *)TIMER1) / (1u << (TIMERPRESCALER_NO_DIV * 4)) / 1000000u) * 500; - - g_timer1.baseAddress = TIMER1; - g_timer1.load = (load < 1) ? 0 : (load - 1); /* Set timer value immediately */ - g_timer1.bgLoad = (load < 1) ? 0 : (load - 1); /* Set timer value */ - g_timer1.mode = TIMER_MODE_RUN_PERIODIC; /* Run in period mode */ - g_timer1.prescaler = TIMERPRESCALER_NO_DIV; /* Don't frequency division */ - g_timer1.size = TIMER_SIZE_32BIT; /* 1 for 32bit, 0 for 16bit */ - g_timer1.interruptEn = BASE_CFG_ENABLE; - g_timer1.adcSocReqEnable = BASE_CFG_DISABLE; - g_timer1.dmaReqEnable = BASE_CFG_DISABLE; - HAL_TIMER_Init(&g_timer1); - IRQ_Register(IRQ_TIMER1, HAL_TIMER_IrqHandler, &g_timer1); - - HAL_TIMER_RegisterCallback(&g_timer1, TIMER_PERIOD_FIN, MotorStatemachineCallBack); - IRQ_SetPriority(IRQ_TIMER1, 1); /* 1 is priority value */ - IRQ_EnableN(IRQ_TIMER1); -} - -__weak void UART0InterruptErrorCallback(void *handle) -{ - BASE_FUNC_UNUSED(handle); - /* USER CODE BEGIN UART0_TRNS_IT_ERROR */ - /* USER CODE END UART0_TRNS_IT_ERROR */ -} - -__weak void UART0_TXDMACallback(void *handle) -{ - BASE_FUNC_UNUSED(handle); - /* USER CODE BEGIN UART0_WRITE_DMA_FINISH */ - /* USER CODE END UART0_WRITE_DMA_FINISH */ -} - -__weak void UART0WriteInterruptCallback(void *handle) -{ - BASE_FUNC_UNUSED(handle); - /* USER CODE BEGIN UART0_WRITE_IT_FINISH */ - /* USER CODE END UART0_WRITE_IT_FINISH */ -} - -__weak void UART0ReadInterruptCallback(void *handle) -{ - BASE_FUNC_UNUSED(handle); - /* USER CODE BEGIN UART0_READ_IT_FINISH */ - /* USER CODE END UART0_READ_IT_FINISH */ -} - -static void UART0_Init(void) -{ - HAL_CRG_IpEnableSet(UART0_BASE, IP_CLK_ENABLE); /* UART0 clock enable. */ - g_uart0.baseAddress = UART0; - - g_uart0.baudRate = UART0_BAUD_RATE; - g_uart0.dataLength = UART_DATALENGTH_8BIT; - g_uart0.stopBits = UART_STOPBITS_ONE; - g_uart0.parity = UART_PARITY_NONE; - g_uart0.txMode = UART_MODE_DMA; - g_uart0.rxMode = UART_MODE_INTERRUPT; - g_uart0.fifoMode = BASE_CFG_ENABLE; - g_uart0.fifoTxThr = UART_FIFODEPTH_SIZE8; - g_uart0.fifoRxThr = UART_FIFODEPTH_SIZE8; - g_uart0.hwFlowCtr = BASE_CFG_DISABLE; - g_uart0.handleEx.overSampleMultiple = UART_OVERSAMPLING_16X; - g_uart0.handleEx.msbFirst = BASE_CFG_DISABLE; - HAL_UART_Init(&g_uart0); - HAL_UART_RegisterCallBack(&g_uart0, UART_TRNS_IT_ERROR, (UART_CallbackType)UART0InterruptErrorCallback); - HAL_UART_RegisterCallBack(&g_uart0, UART_READ_IT_FINISH, (UART_CallbackType)UART0ReadInterruptCallback); - IRQ_Register(IRQ_UART0, HAL_UART_IrqHandler, &g_uart0); - IRQ_SetPriority(IRQ_UART0, 6); /* 6 is priority value */ - IRQ_EnableN(IRQ_UART0); - g_uart0.dmaHandle = &g_dmac; - g_uart0.uartDmaTxChn = 0; - HAL_UART_RegisterCallBack(&g_uart0, UART_WRITE_DMA_FINISH, (UART_CallbackType)UART0_TXDMACallback); - HAL_UART_RegisterCallBack(&g_uart0, UART_WRITE_IT_FINISH, (UART_CallbackType)UART0WriteInterruptCallback); -} - -static void IOConfig(void) -{ - SYSCTRL0->SC_SYS_STAT.BIT.update_mode = 0; - SYSCTRL0->SC_SYS_STAT.BIT.update_mode_clear = 1; - /* Config PIN39 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO0_3_AS_UART0_TXD); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO0_3_AS_UART0_TXD, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO0_3_AS_UART0_TXD, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO0_3_AS_UART0_TXD, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO0_3_AS_UART0_TXD, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN40 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO0_4_AS_UART0_RXD); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO0_4_AS_UART0_RXD, PULL_UP); /* Pull-up and Pull-down, UART RX recommend PULL_UP */ - HAL_IOCMG_SetPinSchmidtMode(GPIO0_4_AS_UART0_RXD, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO0_4_AS_UART0_RXD, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO0_4_AS_UART0_RXD, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN1 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO0_7_AS_ACMP0_OUT); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO0_7_AS_ACMP0_OUT, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO0_7_AS_ACMP0_OUT, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO0_7_AS_ACMP0_OUT, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO0_7_AS_ACMP0_OUT, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN48 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO0_6_AS_ACMP_N2); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO0_6_AS_ACMP_N2, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO0_6_AS_ACMP_N2, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO0_6_AS_ACMP_N2, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO0_6_AS_ACMP_N2, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN47 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO0_5_AS_ACMP_P2); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO0_5_AS_ACMP_P2, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO0_5_AS_ACMP_P2, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO0_5_AS_ACMP_P2, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO0_5_AS_ACMP_P2, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN4 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO2_6_AS_PGA0_N0); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO2_6_AS_PGA0_N0, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO2_6_AS_PGA0_N0, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO2_6_AS_PGA0_N0, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO2_6_AS_PGA0_N0, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN5 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO2_5_AS_PGA0_P0); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO2_5_AS_PGA0_P0, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO2_5_AS_PGA0_P0, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO2_5_AS_PGA0_P0, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO2_5_AS_PGA0_P0, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN3 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO2_7_AS_PGA0_OUT); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO2_7_AS_PGA0_OUT, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO2_7_AS_PGA0_OUT, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO2_7_AS_PGA0_OUT, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO2_7_AS_PGA0_OUT, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN7 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO3_7_AS_ADC_AIN9); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO3_7_AS_ADC_AIN9, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO3_7_AS_ADC_AIN9, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO3_7_AS_ADC_AIN9, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO3_7_AS_ADC_AIN9, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN2 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO5_1_AS_ADC_AIN5); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO5_1_AS_ADC_AIN5, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO5_1_AS_ADC_AIN5, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO5_1_AS_ADC_AIN5, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO5_1_AS_ADC_AIN5, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN14 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO4_7_AS_ADC_AIN15); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO4_7_AS_ADC_AIN15, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO4_7_AS_ADC_AIN15, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO4_7_AS_ADC_AIN15, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO4_7_AS_ADC_AIN15, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN11 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO1_5_AS_PGA1_P0); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO1_5_AS_PGA1_P0, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO1_5_AS_PGA1_P0, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO1_5_AS_PGA1_P0, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO1_5_AS_PGA1_P0, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN12 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO1_6_AS_PGA1_N0); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO1_6_AS_PGA1_N0, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO1_6_AS_PGA1_N0, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO1_6_AS_PGA1_N0, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO1_6_AS_PGA1_N0, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN13 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO1_7_AS_PGA1_OUT); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO1_7_AS_PGA1_OUT, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO1_7_AS_PGA1_OUT, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO1_7_AS_PGA1_OUT, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO1_7_AS_PGA1_OUT, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN35 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO2_3_AS_GPIO2_3); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO2_3_AS_GPIO2_3, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO2_3_AS_GPIO2_3, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO2_3_AS_GPIO2_3, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO2_3_AS_GPIO2_3, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN27 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO1_0_AS_GPIO1_0); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO1_0_AS_GPIO1_0, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO1_0_AS_GPIO1_0, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO1_0_AS_GPIO1_0, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO1_0_AS_GPIO1_0, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN41 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO2_4_AS_GPIO2_4); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO2_4_AS_GPIO2_4, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO2_4_AS_GPIO2_4, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO2_4_AS_GPIO2_4, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO2_4_AS_GPIO2_4, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN19 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO3_0_AS_APT0_PWMA); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO3_0_AS_APT0_PWMA, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO3_0_AS_APT0_PWMA, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO3_0_AS_APT0_PWMA, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO3_0_AS_APT0_PWMA, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN23 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO4_0_AS_APT0_PWMB); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO4_0_AS_APT0_PWMB, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO4_0_AS_APT0_PWMB, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO4_0_AS_APT0_PWMB, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO4_0_AS_APT0_PWMB, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN20 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO3_1_AS_APT1_PWMA); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO3_1_AS_APT1_PWMA, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO3_1_AS_APT1_PWMA, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO3_1_AS_APT1_PWMA, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO3_1_AS_APT1_PWMA, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN24 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO4_1_AS_APT1_PWMB); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO4_1_AS_APT1_PWMB, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO4_1_AS_APT1_PWMB, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO4_1_AS_APT1_PWMB, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO4_1_AS_APT1_PWMB, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN21 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO3_2_AS_APT2_PWMA); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO3_2_AS_APT2_PWMA, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO3_2_AS_APT2_PWMA, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO3_2_AS_APT2_PWMA, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO3_2_AS_APT2_PWMA, DRIVER_RATE_2); /* Output signal edge fast/slow */ - /* Config PIN25 */ - HAL_IOCMG_SetPinAltFuncMode(GPIO4_2_AS_APT2_PWMB); /* Check function selection */ - HAL_IOCMG_SetPinPullMode(GPIO4_2_AS_APT2_PWMB, PULL_NONE); /* Pull-up and Pull-down */ - HAL_IOCMG_SetPinSchmidtMode(GPIO4_2_AS_APT2_PWMB, SCHMIDT_DISABLE); /* Schmitt input on/off */ - HAL_IOCMG_SetPinLevelShiftRate(GPIO4_2_AS_APT2_PWMB, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ - HAL_IOCMG_SetPinDriveRate(GPIO4_2_AS_APT2_PWMB, DRIVER_RATE_2); /* Output signal edge fast/slow */ -} - -static void APT_SyncMasterInit(void) -{ - HAL_APT_MasterSyncInit(&g_apt0, APT_SYNC_OUT_ON_CNTR_ZERO); -} - -static void APT_SyncSlaveInit(void) -{ - APT_SlaveSyncIn aptSlave; - - aptSlave.cntPhase = 0; /* counter phase value */ - aptSlave.syncCntMode = APT_COUNT_MODE_AFTER_SYNC_UP; - aptSlave.syncInSrc = APT_SYNCIN_SRC_APT0_SYNCOUT; /* sync source selection */ - aptSlave.cntrSyncSrc = APT_CNTR_SYNC_SRC_SYNCIN; - HAL_APT_SlaveSyncInit(&g_apt1, &aptSlave); - - aptSlave.cntPhase = 0; /* counter phase value */ - aptSlave.syncCntMode = APT_COUNT_MODE_AFTER_SYNC_UP; - aptSlave.syncInSrc = APT_SYNCIN_SRC_APT0_SYNCOUT; /* sync source selection */ - aptSlave.cntrSyncSrc = APT_CNTR_SYNC_SRC_SYNCIN; - HAL_APT_SlaveSyncInit(&g_apt2, &aptSlave); - -} - -void SystemInit(void) -{ - IOConfig(); - DMA_Init(); - UART0_Init(); - ACMP0_Init(); - APT0_Init(); - APT1_Init(); - APT2_Init(); - ADC0_Init(); - PGA0_Init(); - PGA1_Init(); - TIMER0_Init(); - TIMER1_Init(); - GPIO_Init(); - - APT_SyncMasterInit(); - APT_SyncSlaveInit(); - - /* USER CODE BEGIN system_init */ - /* USER CODE END system_init */ -} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_carrier.h b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_carrier.h deleted file mode 100644 index dea3ad66..00000000 --- a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_carrier.h +++ /dev/null @@ -1,142 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_carrier.h - * @author MCU Algorithm Team - * @brief This file provides functions declaration for carrier interrupt processing function. - */ -#ifndef McuMagicTag_MCS_CARRIER_H -#define McuMagicTag_MCS_CARRIER_H - -#include "mcs_status.h" -#include "mcs_mtr_param.h" -#include "mcs_svpwm.h" -#include "mcs_curr_ctrl.h" -#include "mcs_if_ctrl.h" -#include "mcs_ramp_mgmt.h" -#include "mcs_spd_ctrl.h" -#include "mcs_fosmo.h" -#include "mcs_smo_4th.h" -#include "mcs_pll.h" -#include "mcs_startup.h" -#include "mcs_r1_svpwm.h" -#include "mcs_fw_ctrl.h" -#include "mcs_prot_user.h" - -typedef void (*MCS_ReadCurrUvwCb)(UvwAxis *CurrUvw); -typedef void (*MCS_SetPwmDutyCb)(UvwAxis *dutyUvwLeft, UvwAxis *dutyUvwRight); -typedef void (*MCS_SetADCTriggerTimeCb)(unsigned short cntCmpSOCA, unsigned short cntCmpSOCB); - -/** - * @brief motor control FSM state define. - * @details motor control FSM state type: - * + FSM_IDLE -- IDLE state, system startup default. - * + FSM_OFFSET_CALIB -- Self calibrate, for ADC init. - * + FSM_CAP_CHARGE -- IPM cap charge. - * + FSM_CLEAR -- Clear before every run. - * + FSM_LOCATE -- Rotor position pre-locate. - * + FSM_STARTUP -- Start up. - * + FSM_SWITCH -- Transition state, control from open to closed loop. - * + FSM_RUN -- Normal running state. - * + FSM_WAIT_STOP -- Wait stopping. - * + FSM_STOP -- Normal stop. - * + FSM_FAULT -- Fault state, waiting for user process. - */ -typedef enum { - FSM_IDLE = 0, - FSM_OFFSET_CALIB, - FSM_CAP_CHARGE, - FSM_CLEAR, - FSM_LOCATE, - FSM_STARTUP, - FSM_SWITCH, - FSM_RUN, - FSM_WAIT_STOP, - FSM_STOP, - FSM_FAULT -} FsmState; - -/** - * @brief Sampling mode. - */ -typedef enum { - DUAL_RESISTORS = 0, - SINGLE_RESISTOR = 1 -} SampleMode; - -/** - * @brief Motor control data structure - */ -typedef struct { - unsigned char motorStateFlag; /**< Motor start/stop status flag. */ - float spdCmdHz; /**< External input speed command value. */ - float axisAngle; /**< Angle of the synchronous coordinate system. */ - float spdRefHz; /**< Command value after speed ramp management. */ - float currCtrlPeriod; /**< current loop control period. */ - float adc0Compensate; /**< ADC0 softwaretrim compensate value. */ - float adc1Compensate; /**< ADC1 softwaretrim compensate value. */ - float udc; /**< Bus voltage. */ - float powerBoardTemp; /**< Power boart surface temperature. */ - unsigned short aptMaxcntCmp; /**< Apt Maximum Comparison Count. */ - float adcCurrCofe; /**< Adc current sampling cofeature. */ - - unsigned short sysTickCnt; /**< System Timer Tick Count. */ - unsigned short capChargeTickNum; /**< Bootstrap Capacitor Charge Tick Count. */ - volatile unsigned int msTickCnt; /**< Millisecond-level counter, which can be used in 1-ms and 5-ms tasks. */ - unsigned short msTickNum; /**< Number of ticks corresponding to 1 ms. */ - char obserType; /**< Set Observer Type. */ - char controlMode; /**< Set foc control or sixstep bldc control mode or others. */ - char spdAdjustMode; /**< Set speed adjust mode. */ - char uartConnectFlag; /**< Uart connect success flag. */ - short uartHeartDetCnt; /**< Uart connect heart detect count. */ - float uartTimeStamp; /**< Uart data time stamp. */ - SysStatusReg statusReg; /**< System status. */ - FsmState stateMachine; /**< Motor Control State Machine. */ - - SampleMode sampleMode; /**< Current sampling mode setting: single resistor, double resistor. */ - MOTOR_Param mtrParam; /**< Motor parameters. */ - FOSMO_Handle smo; /**< SMO observer handle. */ - SMO4TH_Handle smo4th; /**< SMO 4th observer handle. */ - IF_Handle ifCtrl; /**< I/F control handle. */ - SVPWM_Handle sv; /**< Dual-resistance/three-resistance sampling SVPWM Handle. */ - R1SVPWM_Handle r1Sv; /**< Single-resistance phase-shifted SVPWM handle. */ - RMG_Handle spdRmg; /**< Ramp management struct for the speed controller input reference. */ - SPDCTRL_Handle spdCtrl; /**< Speed loop Control Handle. */ - CURRCTRL_Handle currCtrl; /**< Current loop control handle. */ - STARTUP_Handle startup; /**< Startup Switch Handle. */ - FW_Handle fw; /**< Flux-Weakening Handle. */ - - DqAxis idqRef; /**< Command value of the dq axis current. */ - UvwAxis currUvw; /**< Three-phase current sampling value. */ - AlbeAxis iabFbk; /**< αβ-axis current feedback value. */ - DqAxis idqFbk; /**< Current feedback value of the dq axis. */ - DqAxis vdqRef; /**< Current loop output dq voltage. */ - AlbeAxis vabRef; /**< Current loop output voltage αβ. */ - UvwAxis dutyUvw; /**< UVW three-phase duty cycle. */ - UvwAxis dutyUvwLeft; /**< Single Resistor UVW Three-Phase Left Duty Cycle. */ - UvwAxis dutyUvwRight; /**< Single Resistor UVW Three-Phase Right Duty Cycle. */ - - MCS_ReadCurrUvwCb readCurrUvwCb; /**< Read current callback function. */ - MCS_SetPwmDutyCb setPwmDutyCb; /**< Set the duty cycle callback function. */ - MCS_SetADCTriggerTimeCb setADCTriggerTimeCb; /**< Sets the ADC trigger point callback function. */ - - MotorProtStatus_Handle prot; /**< Motor protect status detection flag bit handle. */ -} MTRCTRL_Handle; - -void MCS_CarrierProcess(MTRCTRL_Handle *mtrCtrl); - -#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_chip_config.h b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_chip_config.h deleted file mode 100644 index 38c00b5d..00000000 --- a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_chip_config.h +++ /dev/null @@ -1,76 +0,0 @@ -/** - * @ Copyright (c) HiSilicon (Shanghai) Technologies Co., Ltd. 2022-2023. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_chip_config.h - * @author MCU Algorithm Team - * @brief This file provides config macros for ECMCU105H/ECBMCU201MPC app. - */ - -#ifndef McuMagicTag_MCS_CHIPCONFIG_H -#define McuMagicTag_MCS_CHIPCONFIG_H - -#include "feature.h" - -#if defined (CHIP_3061MNPICA) || defined (CHIP_3061MNPIKA) || defined (CHIP_3061MNNICA) || \ - defined (CHIP_3061MNNIKA) || defined (CHIP_3061MNPIC8) || defined(CHIP_3061MNNIC8) || \ - defined (CHIP_3061MNPIK8) || defined (CHIP_3061MNNIK8) - - #define ADCPTT_HANDLE g_adc0 - #define ADCRESIS_HANDLE g_adc0 - #define ADCUDC_HANDLE g_adc0 - #define ADCU_HANDLE g_adc0 - #define ADCW_HANDLE g_adc0 - #define LED1_HANDLE g_gpio2 - #define LED2_HANDLE g_gpio1 - #define LED2_PIN GPIO_PIN_0 - #define LED1_PIN GPIO_PIN_3 - #define ADCPTTSOCNUM ADC_SOC_NUM0 - #define ADCUSOCNUM ADC_SOC_NUM1 - #define ADCRESISSOCNUM ADC_SOC_NUM3 - #define ADCUDCSOCNUM ADC_SOC_NUM4 - #define ADCWSOCNUM ADC_SOC_NUM6 - - #define ADC0COMPENSATE 2037.0f - #define ADC1COMPENSATE 2027.0f - -#endif - -#if defined (CHIP_3065HRPIRZ) || defined (CHIP_3065ARPIRZ) || defined (CHIP_3066MNPIRH) || \ - defined (CHIP_3065PNPIRH) || defined (CHIP_3065PNPIRE) || defined (CHIP_3065PNPIRA) - - #define ADCU_HANDLE g_adc0 - #define ADCW_HANDLE g_adc1 - #define ADCRESIS_HANDLE g_adc2 - #define ADCUDC_HANDLE g_adc2 - #define ADCPTT_HANDLE g_adc2 - #define LED1_HANDLE g_gpio0 - #define LED2_HANDLE g_gpio0 - #define LED2_PIN GPIO_PIN_6 - #define LED1_PIN GPIO_PIN_7 - #define ADCPTTSOCNUM ADC_SOC_NUM0 - #define ADCRESISSOCNUM ADC_SOC_NUM1 - #define ADCUSOCNUM ADC_SOC_NUM8 - #define ADCWSOCNUM ADC_SOC_NUM8 - #define ADCUDCSOCNUM ADC_SOC_NUM14 - - #define ADC0COMPENSATE 2033.0f - #define ADC1COMPENSATE 2070.0f - -#endif - - -#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_ctlmode_config.h b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_ctlmode_config.h deleted file mode 100644 index 2a6f38cc..00000000 --- a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_ctlmode_config.h +++ /dev/null @@ -1,73 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_ctlmode_config.h - * @author MCU Algorithm Team - * @brief This file provides config macros for ECMCU105H app. - */ - - /* Define to prevent recursive inclusion ------------------------------------------------------- */ -#ifndef McuMagicTag_MCS_CTLMODECONFIG_H -#define McuMagicTag_MCS_CTLMODECONFIG_H - -#include "debug.h" -#include "typedefs.h" - -typedef enum { - FOC_STARTUP_IF = 0, - FOC_STARTUP_VF, - FOC_STARTUP_HFI -} MOTOR_STARTUPMODE_CONFIG; - -typedef enum { - FOC_OBSERVERTYPE_SMO1TH = 0, - FOC_OBSERVERTYPE_SMO1TH_PLL, - FOC_OBSERVERTYPE_SMO4TH, - FOC_OBSERVERTYPE_SMO4TH_PLL, - FOC_OBSERVERTYPE_LUNBORG, - FOC_OBSERVERTYPE_FLUX -} MOTOR_OBSERVERTYPE_CONFIG; - -typedef enum { - FOC_CONTROLMODE_SPEED = 0, - FOC_CONTROLMODE_TORQUE -} MOTOR_CONTROLMODE_CONFIG; - -typedef enum { - FOC_CURQAXISPID_PARAMS = 0, - FOC_CURDAXISPID_PARAMS, - FOC_SPDPID_PARAMS -} MOTOR_PID_SET; - -typedef enum { - MOTOR_PARAMS_BASE = 0, - MOTOR_PARAMS_SPECIAL, - MOTOR_PARAMS_BOARD -} MOTOR_PARAMS_SET; - -typedef enum { - CUST_SPEED_ADJUST = 0, - HOST_SPEED_ADJUST -} MODE_ADSPEED_CONFIG; - -typedef enum { - CONNECTING = 0, - CONNECTED, - DISCONNECT -} UART_STATUS; - -#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_status.h b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_status.h deleted file mode 100644 index 19510c78..00000000 --- a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_status.h +++ /dev/null @@ -1,188 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_status.h - * @author MCU Algorithm Team - * @brief This file provides functions declaration of system status. - */ - -/* Define to prevent recursive inclusion ------------------------------------------------------- */ -#ifndef McuMagicTag_MCS_STATUS_H -#define McuMagicTag_MCS_STATUS_H - -/* Includes ------------------------------------------------------------------------------------ */ -#include "typedefs.h" -#include "mcs_assert.h" - -/* Typedef definitions ------------------------------------------------------------------------- */ -/** - * @brief System status define - */ -typedef union { - unsigned short all; - struct { - unsigned short cmdStart : 1; /**< Indicates that a start system command has been received. */ - unsigned short cmdStop : 1; /**< Indicates that a stop system command has been received. */ - unsigned short isRunning : 1; /**< Indicates that the system is running (enable signal) */ - unsigned short sysError : 1; /**< Indicates that the system reports an error. */ - unsigned short poweron : 1; /**< Indicates that the power-on initialization phase is complete. */ - unsigned short capcharge : 1; /**< Indicates that the bootstrap capacitor charging phase is complete. */ - unsigned short adczero : 1; /**< The current sampling point is reset to zero after power-on. */ - } Bit; -} SysStatusReg; - -/** - * @brief Get status of Bit cmdStart. - * @param sysStatus System status register handle. - * @retval Status of Bit cmdStart. - */ -static inline bool SysGetCmdStart(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - bool ret; - ret = (sysStatus->Bit.cmdStart == 1) ? true : false; - return ret; -} - -/** - * @brief Set Bit cmdStart. - * @param sysStatus System status register handle. - * @retval None. - */ -static inline void SysCmdStartSet(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - sysStatus->Bit.cmdStart = 1; -} - -/** - * @brief Clear Bit cmdStart. - * @param handle System status register handle. - * @retval None. - */ -static inline void SysCmdStartClr(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - sysStatus->Bit.cmdStart = 0; -} - -/** - * @brief Get status of Bit cmdStop. - * @param sysStatus System status register handle. - * @retval Status of Bit cmdStart. - */ -static inline bool SysGetCmdStop(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - bool ret; - ret = (sysStatus->Bit.cmdStop == 1) ? true : false; - return ret; -} - -/** - * @brief Set Bit cmdStop. - * @param sysStatus System status register handle. - * @retval None. - */ -static inline void SysCmdStopSet(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - sysStatus->Bit.cmdStop = 1; -} - -/** - * @brief Clear Bit cmdStop. - * @param sysStatus System status register handle. - * @retval None. - */ -static inline void SysCmdStopClr(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - sysStatus->Bit.cmdStop = 0; -} - -/** - * @brief Get status of Bit isRunning. - * @param sysStatus System status register handle. - * @retval Status of Bit isRunning. - */ -static inline bool SysIsRunning(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - bool ret; - ret = (sysStatus->Bit.isRunning == 1) ? true : false; - return ret; -} - -/** - * @brief Set Bit isRuning. - * @param sysStatus System status register handle. - * @retval None. - */ -static inline void SysRunningSet(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - sysStatus->Bit.isRunning = 1; -} - -/** - * @brief Clear Bit isRuning. - * @param sysStatus System status register handle. - * @retval None. - */ -static inline void SysRunningClr(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - sysStatus->Bit.isRunning = 0; -} - -/** - * @brief Get status of Bit sysError. - * @param sysStatus System status register handle. - * @retval Status of Bit sysError. - */ -static inline bool SysIsError(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - bool ret; - ret = (sysStatus->Bit.sysError == 1) ? true : false; - return ret; -} - -/** - * @brief Set Bit sysError. - * @param sysStatus System status register handle. - * @retval None. - */ -static inline void SysErrorSet(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - sysStatus->Bit.sysError = 1; -} - -/** - * @brief Clear Bit sysError. - * @param sysStatus System status register handle. - * @retval None. - */ -static inline void SysErrorClr(SysStatusReg *sysStatus) -{ - MCS_ASSERT_PARAM(sysStatus != NULL); - sysStatus->Bit.sysError = 0; -} - -#endif diff --git a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_user_config.h b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_user_config.h deleted file mode 100644 index 4a8bfaa8..00000000 --- a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_user_config.h +++ /dev/null @@ -1,107 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_user_config.h - * @author MCU Algorithm Team - * @brief This file provides functions declaration of user config parameters. - */ -#ifndef McuMagicTag_MCS_CONFIG_H -#define McuMagicTag_MCS_CONFIG_H - -#include "debug.h" -#include "typedefs.h" - -#define SMO4TH - -#define SYSTICK_PERIOD_US 500u /* systick period. */ - -#define INV_CAP_CHARGE_MS 3u /* Capacitor charging time. */ - -#define INV_VOLTAGE_BUS 24.0f /* Bus voltage, V. */ - -#define CTRL_CURR_PERIOD 0.0001f /* carrier ISR period, 100us. */ -#define CTRL_SYSTICK_PERIOD 0.0005f /* systick control period, 500us. */ - -/* Duty of sample window, the real time is 0.06*50us = 3us. */ -#define SAMPLE_WINDOW_DUTY 0.06f - -/* Duty of sample point shift as flip point, the real time is 0.008*50us = 0.4us. */ -#define SAMPLE_POINT_SHIFT 0.008f - -/* Sampling resistance 200mOhm 0.0013295 */ -#define ADC_CURR_COFFI 0.0013295f /* 3.3/4096/3.03/0.2 pga: 3.03, 200mohm. */ - -#define APT_U APT0_BASE /* Base address of U phase APT module. */ -#define APT_V APT1_BASE /* Base address of V phase APT module. */ -#define APT_W APT2_BASE /* Base address of W phase APT module. */ - -/* FOSMO */ -#define FOSMO_GAIN 4.0f /* SMO gain */ -#define FOSMO_LAMBDA 2.0f /* SMO coefficient of cut-off frequency, its value = lambda * we. */ -#define FOSMO_EMF_CUTOFF_FREQ 2.0f /* SMO back emf cutoff frequency. */ -#define SPEED_FILTER_CUTOFF_FREQUENCY 40.0f /* SMO speed cutoff frequency of speed filter. */ -#define FOSMO_PLL_BDW 30.0f /* SMO PLL bandwidth. */ - -/* SMO4TH */ -#define SMO4TH_PLL_BDW 30.0f /* SMO4TH PLL Bandwidth. */ -#define SMO4TH_KD 300.0f /* SMO4TH parameters KD. */ -#define SMO4TH_KQ 600.0f /* SMO4TH parameters KQ. */ -#define SMO4TH_SPD_FILTER_CUTOFF_FREQ 40.0f /* SMO4TH speed cutoff frequency of speed filter. */ - -/* User_Commond */ -#define CTRL_IF_CURR_AMP_A 0.8f /* IF control current amplitude. */ -#define USER_TARGET_SPD_HZ 100.0f /* User-defined target speed value. */ -#define USER_SWITCH_SPDBEGIN_HZ 30.0f /* Start of handover interval. */ -#define USER_SWITCH_SPDEND_HZ (USER_SWITCH_SPDBEGIN_HZ + 3.0f) /* End of handover period. */ -#define USER_MAX_SPD_HZ 29.0f /* User-defined maximum speed value. */ -#define USER_MIN_SPD_HZ 1.0f /* User-defined minimum speed value. */ -#define USER_SPD_SLOPE 50.0f /* slope of velocity change. */ -#define USER_CURR_SLOPE (CTRL_IF_CURR_AMP_A * 10.0f) /* Current change slope. */ - -/* PID PARAMS */ -#define CURRQAXIS_KP 5.023202f /* Current loop Q axis Kp. */ -#define CURRQAXIS_KI 20612.84f /* Current loop Q axis Ki. */ -#define CURRDAXIS_KP 3.477114f /* Current loop D axis Kp. */ -#define CURRDAXIS_KI 20612.84f /* Current loop D axis Ki. */ -/* Current loop PID output lower limit. */ -#define CURR_LOWERLIM (-INV_VOLTAGE_BUS * ONE_DIV_SQRT3 * 0.98f) -/* Current loop PID output upper limit. */ -#define CURR_UPPERLIM (INV_VOLTAGE_BUS * ONE_DIV_SQRT3 * 0.98f) - -#define SPD_KP 0.00505f /* Speed loop Kp. */ -#define SPD_KI 0.012f /* Speed loop Ki. */ -#define SPD_LOWERLIM (-0.105f) /* Speed loop PID output lower limit. */ -#define SPD_UPPERLIM 0.105f /* Speed loop PID output upper limit. */ - - -#define MOTORPARAM_DEFAULTS { \ - .mtrNp = 21, \ - .mtrRs = 2.5f, \ - .mtrLd = 0.066f, \ - .mtrLq = 0.066f, \ - .mtrPsif = 0.0f, \ - .mtrJ = 0.0f, \ - .maxElecSpd = 49.0f, \ - .maxCurr = 0.8f, \ - .busVolt = INV_VOLTAGE_BUS, \ -} - - -/* Bus voltage sampling conversion factor. */ -#define ADC_UDC_COFFI 0.01289f /* 0.01289 = 3.3/4096*192/12. */ - -#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_cmm.c b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_cmm.c deleted file mode 100644 index 99766bdf..00000000 --- a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_cmm.c +++ /dev/null @@ -1,43 +0,0 @@ -/** - * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_prot_cmm.c - * @author MCU Algorithm Team - * @brief This file contains protection common api definition. - */ - -/* Includes ------------------------------------------------------------------------------------ */ -#include "mcs_prot_cmm.h" -#include "apt.h" -#include "mcs_assert.h" - -/** - * @brief Safty-pulse-off function execution to turn off all the power devices. - * @param aptAddr Three-phase APT address pointer. - * @retval None. - */ -void ProtSpo_Exec(APT_RegStruct **aptAddr) -{ - MCS_ASSERT_PARAM(aptAddr != NULL); - /**< Turn off all the six power devices of the inverter. */ - for (unsigned int i = 0; i < MOTOR_PHASE_NUMBER; i++) { - APT_RegStruct *aptx = (APT_RegStruct *)(aptAddr[i]); - DCL_APT_EnableSwContPWMAction(aptx, APT_PWM_CHANNEL_A); - DCL_APT_EnableSwContPWMAction(aptx, APT_PWM_CHANNEL_B); - DCL_APT_ForcePWMOutputLow(aptx); - } -} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_cmm.h b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_cmm.h deleted file mode 100644 index 104a5dec..00000000 --- a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_cmm.h +++ /dev/null @@ -1,136 +0,0 @@ -/** - * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_prot_cmm.h - * @author MCU Algorithm Team - * @brief This file contains protection function common data struct and api declaration. - */ - -/* Define to prevent recursive inclusion ------------------------------------------------------- */ -#ifndef McuMagicTag_MCS_PROT_CMM_H -#define McuMagicTag_MCS_PROT_CMM_H - -#include "typedefs.h" -#include "apt_ip.h" - -/* Macro definitions --------------------------------------------------------------------------- */ -#define MOTOR_PHASE_NUMBER (3) - -#define PROT_VAL_THRESHOLD_NUMS (4) -#define PROT_VAL_THRESHOLD_0 (0) -#define PROT_VAL_THRESHOLD_1 (1) -#define PROT_VAL_THRESHOLD_2 (2) -#define PROT_VAL_THRESHOLD_3 (3) - -#define PROT_LIMIT_TIME_NUMS (3) -#define PROT_LIMIT_TIME_0 (0) -#define PROT_LIMIT_TIME_1 (1) -#define PROT_LIMIT_TIME_2 (2) - -#define MOTOR_PHASE_NUMBER (3) - -/**< Motor error status definition. */ -typedef union { - int all; - struct { - unsigned short overCurrErr : 1; /**< Indicates that phase current(s) is over protected value. */ - unsigned short overVoltErr : 1; /**< Indicates that dc-link voltage is over protected value. */ - unsigned short lowerVoltErr : 1; /**< Indicates that dc-link voltage is lower than protected value */ - unsigned short overIpmTempErr : 1; /**< Indicates that IPM temperature is over protected value. */ - unsigned short revRotErr : 1; /**< Indicates that motor negtive direction. */ - unsigned short motorStalling : 1; /**< Indicates that rotor is stalling. */ - unsigned short overMotorTempErr : 1; /**< Indicates that three phase currents is out-of-balance. */ - unsigned short posSnsrCommsErr : 1; /**< Indicates that position sensor communication is lost with MCU. */ - unsigned short posSnsrFuncErr : 1; /**< Indicates that position sensor reports function error. */ - unsigned short posSnsrCalibrErr : 1; /**< Indicates that position sensor fails to calibrate itself. */ - unsigned short currOutOfBalance : 1; /**< Indicates that the rotor is reverse rotation.*/ - unsigned short phsOpenErr : 1; /**< Indicates that phase winding(s) is open. */ - unsigned short phsU : 1; /**< Indicates that u phase fails when phsOpenErr occurs. */ - unsigned short phsV : 1; /**< Indicates that v phase fails when phsOpenErr occurs. */ - unsigned short phsW : 1; /**< Indicates that w phase fails when phsOpenErr occurs. */ - unsigned short multiPhs : 1; /**< Indicates that multi-phases fail when phsOpenErr occurs.*/ - } Bit; -} MotorErrStatusReg; - -/**< Protection Status Bit Definition */ -typedef enum { - OCP_ERR_BIT, - OVP_ERR_BIT, - LVP_ERR_BIT, - OTP_IPM_ERR_BIT, - OTP_MOTOR_ERR_BIT, - STALLING_ERR_BIT, - CURR_OUT_BALANCE_ERR_BIT, - POS_COMMS_ERR_BIT, - POS_FUNC_ERR_BIT, - POS_CALIB_ERR_BIT, - REV_ROT_ERR_BIT, - PHS_OPEN_ERR_BIT, - PHS_U_ERR_BIT, - PHS_V_ERR_BIT, - PHS_W_ERR_BIT, - PHS_MULTI_ERR_BIT, -} PROT_ErrBit; - -/**< Motor error protection level. */ -typedef enum { - PROT_LEVEL_0 = 0, - PROT_LEVEL_1, - PROT_LEVEL_2, - PROT_LEVEL_3, - PROT_LEVEL_4 /**< The greater level number, the severe error is. */ -} PROT_Level; - -/** - * @brief Obtains the status of a bit of data. - * @param data data. - * @param bits Number of digits. - * @retval Bit status. - */ -static inline bool GetBit(int data, unsigned short bit) -{ - bool ret; - ret = ((data >> bit) & 1); - return ret; -} - -/** - * @brief Sets the status of a bit of data. - * @param data data. - * @param bit The setted bit. - * @retval None. - */ -static inline void SetBit(int *data, unsigned char bit) -{ - *data |= (1 << bit); -} - -/** - * @brief Clear the status of a bit of data. - * @param data data. - * @param bit The Clear bit. - * @retval None. - */ -static inline void ClearBit(int *data, unsigned char bit) -{ - *data &= ~(1 << bit); -} - -/**< Protection action. */ -void ProtSpo_Exec(APT_RegStruct **aptAddr); - -#endif diff --git a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user.c b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user.c deleted file mode 100644 index e97876b7..00000000 --- a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user.c +++ /dev/null @@ -1,137 +0,0 @@ -/** - * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_prot_user.c - * @author MCU Algorithm Team - * @brief This file contains user protection data struct, inquiry api and initialization api definition. - */ - -/* Includes ------------------------------------------------------------------------------------ */ -#include "mcs_prot_user.h" -#include "mcs_prot_user_config.h" -#include "mcs_assert.h" - -/** - * @brief Get motor over current error status. - * @param motorErrStatus Motor error status. - * @retval Error status. - */ -bool IsMotorOverCurrErr(MotorErrStatusReg motorErrStatus) -{ - if (motorErrStatus.Bit.overCurrErr) { - return true; - } else { - return false; - } -} - -/** - * @brief Get motor over dc-link voltage error status. - * @param motorErrStatus Motor error status. - * @retval Error status. - */ -bool IsDcLinkOverVoltErr(MotorErrStatusReg motorErrStatus) -{ - if (motorErrStatus.Bit.overVoltErr) { - return true; - } else { - return false; - } -} - -/** - * @brief Get motor lower dc-link voltage error status. - * @param motorErrStatus Motor error status. - * @retval Error status. - */ -bool IsDcLinkLowerVoltErr(MotorErrStatusReg motorErrStatus) -{ - if (motorErrStatus.Bit.lowerVoltErr) { - return true; - } else { - return false; - } -} - -/** - * @brief Get motor over Ipm temperature error status. - * @param motorErrStatus Motor error status. - * @retval Error status. - */ -bool IsIpmOverTempErr(MotorErrStatusReg motorErrStatus) -{ - if (motorErrStatus.Bit.overIpmTempErr) { - return true; - } else { - return false; - } -} - -/** - * @brief Get motor over Motor temperature error status. - * @param motorErrStatus Motor error status. - * @retval Error status. - */ -bool IsMotorOverTempErr(MotorErrStatusReg motorErrStatus) -{ - if (motorErrStatus.Bit.overMotorTempErr) { - return true; - } else { - return false; - } -} - -/** - * @brief Get motor stalling error status. - * @param motorErrStatus Motor error status. - * @retval Error status. - */ -bool IsMotorStallingErr(MotorErrStatusReg motorErrStatus) -{ - if (motorErrStatus.Bit.motorStalling) { - return true; - } else { - return false; - } -} - -/** - * @brief Clear the motor error status. - * @param motorProt Motor protection handle. - * @retval Error status. - */ -void ClearMotorErrStatus(MotorProtStatus_Handle *motorProt) -{ - MCS_ASSERT_PARAM(motorProt != NULL); - /* Clear the motor error status. */ - motorProt->motorErrStatus.all = 0x00; - OCP_Clear(&motorProt->ocp); - OVP_Clear(&motorProt->ovp); - LVP_Clear(&motorProt->lvp); - OTP_Clear(&motorProt->otp); -} - -/** - * @brief Motor protection function initialization. - * @param motorProt Motor protection handle. - * @retval Error status. - */ -void MotorProt_Init(MotorProtStatus_Handle *motorProt) -{ - MCS_ASSERT_PARAM(motorProt != NULL); - motorProt->motorErrStatus.all = 0x00; -} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user.h b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user.h deleted file mode 100644 index 365110e1..00000000 --- a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user.h +++ /dev/null @@ -1,54 +0,0 @@ -/** - * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_prot_user.h - * @author MCU Algorithm Team - * @brief This file contains user protection data struct, inquiry api and initialization api declaration. - */ - -/* Define to prevent recursive inclusion ------------------------------------------------------- */ -#ifndef McuMagicTag_MCS_PROT_USER_H -#define McuMagicTag_MCS_PROT_USER_H - -/* Includes ------------------------------------------------------------------------------------ */ -#include "mcs_prot_cmm.h" -#include "mcs_curr_prot.h" -#include "mcs_dc_volt_prot.h" -#include "mcs_temp_prot.h" -#include "mcs_motor_stalling.h" -#include "typedefs.h" - -typedef struct { - MotorErrStatusReg motorErrStatus; /**< Motor error status. */ - OCP_Handle ocp; /**< Over current protection. */ - OVP_Handle ovp; /**< Over dc-link voltage protection. */ - LVP_Handle lvp; /**< Lower dc-link voltage protection. */ - OTP_Handle otp; /**< Over IPM temperature protection. */ - STP_Handle stall; /**< Motor stalling protection. */ -} MotorProtStatus_Handle; - -void MotorProt_Init(MotorProtStatus_Handle *motorProt); - -/**< Inquiry motor error status */ -bool IsMotorOverCurrErr(MotorErrStatusReg motorErrStatus); -bool IsDcLinkOverVoltErr(MotorErrStatusReg motorErrStatus); -bool IsDcLinkLowerVoltErr(MotorErrStatusReg motorErrStatus); -bool IsIpmOverTempErr(MotorErrStatusReg motorErrStatus); -bool IsMotorOverTempErr(MotorErrStatusReg motorErrStatus); -bool IsMotorStallingErr(MotorErrStatusReg motorErrStatus); -void ClearMotorErrStatus(MotorProtStatus_Handle *motorProt); -#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user_config.h b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user_config.h deleted file mode 100644 index 832cd1be..00000000 --- a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/common/mcs_prot_user_config.h +++ /dev/null @@ -1,125 +0,0 @@ -/** - * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_prot_user_config.h - * @author MCU Algorithm Team - * @brief This file contans user macro definition of the protection function. - */ - -/* Define to prevent recursive inclusion ------------------------------------------------------- */ -#ifndef McuMagicTag_MCS_PROT_USER_CONFIG_H -#define McuMagicTag_MCS_PROT_USER_CONFIG_H - -/* Macro definitions --------------------------------------------------------------------------- */ -/**< COMMON */ -/**< Peak phase current (A) of the motor or IPM under continuous operations. */ -#define PROT_MOTOR_RATED_CURR (1.0f) -/**< Only several continuous fault detection can trigger error status. */ -#define PROT_CNT_LIMIT (100) -/**< Only several contunuous none fault dectection can trigger elimination of error status. */ -#define RECY_CNT_LIMIT (10000) -/**< Only several contunuous none fault dectection can trigger elimination of error status. */ -#define OVER_VOLT_RECY_CNT_LIMIT (100) -/**< Only several contunuous none fault dectection can trigger elimination of error status. */ -#define LOWER_VOLT_RECY_CNT_LIMIT (100) - -/**< Over current protection */ -/**< Over current trigger value (A) when in level 1. */ -#define PROT_OVER_CURR_POW_DN1 (2.0f * PROT_MOTOR_RATED_CURR) -/**< Over current trigger value (A) when in level 2. */ -#define PROT_OVER_CURR_POW_DN2 (2.0f * PROT_MOTOR_RATED_CURR) -/**< Over current trigger value (A) when in level 3. */ -#define PROT_OVER_CURR_POW_DN3 (2.0f * PROT_MOTOR_RATED_CURR) -/**< Over current trigger value (A) when in level 4. */ -#define PROT_OVER_CURR_POW_OFF (2.0f * PROT_MOTOR_RATED_CURR) -#define PROT_OVER_CURR_RECY_DELTA (0.0035f) /**< Current gap (A) when recovers from protection status. */ -#define PROT_OVER_CURR_LIMIT1_TIME_SEC (30.0f) /**< 20% overload can last maximum time: 30 sec. */ -#define PROT_OVER_CURR_LIMIT2_TIME_SEC (10.0f) /**< 20% overload can last maximum time: 10 sec. */ -#define PROT_OVER_CURR_LIMIT3_TIME_SEC (2.0f) /**< 20% overload can last maximum time: 2 sec. */ - -/**< Over voltage protection */ -#define PROT_OVER_VOLT_BRK_ON1 (26.0f) /**<处于1级时,直流链路电压触发值(V)过高*/ -#define PROT_OVER_VOLT_BRK_ON2 (27.0f) /**< Over dc-link voltage trigger value (V) when in level 2. */ -#define PROT_OVER_VOLT_BRK_ON3 (28.0f) /**< Over dc-link voltage trigger value (V) when in level 3. */ -#define PROT_OVER_VOLT_BRK_ALL (30.0f) /**< Over dc-link voltage trigger value (V) when in level 4. */ -#define PROT_OVER_VOLT_RECY_DELTA (0.5f) /**<从保护状态恢复时的电压间隙(V)*/ -#define PROT_OVER_VOLT_LIMIT1_TIME_SEC (5.0f) /** 1; One-cycle mode: < 0.5 */ -#define POS_SNSR_IPD_INJ_PERIOD (4) - -#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_curr_prot.c b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_curr_prot.c deleted file mode 100644 index da4e56f8..00000000 --- a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_curr_prot.c +++ /dev/null @@ -1,267 +0,0 @@ -/** - * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_curr_prot.c - * @author MCU Algorithm Team - * @brief This file contains current protecion api definition. - */ - -/* Includes ------------------------------------------------------------------------------------ */ -#include "mcs_curr_prot.h" -#include "mcs_math.h" -#include "mcs_prot_user_config.h" -#include "mcs_assert.h" - -/** - * @brief Initilization over current protection function. - * @param ocp Over current protection handle. - * @param ts Ctrl period (s). - * @retval None. - */ -void OCP_Init(OCP_Handle *ocp, float ts) -{ - MCS_ASSERT_PARAM(ocp != NULL); - MCS_ASSERT_PARAM(ts > 0.0f); - ocp->ts = ts; - OCP_Clear(ocp); - - ocp->protCntLimit = PROT_CNT_LIMIT; - ocp->recyCntLimit = RECY_CNT_LIMIT; - /* Configuring four levels of current protection thresholds. */ - ocp->protValThr[PROT_VAL_THRESHOLD_0] = PROT_OVER_CURR_POW_DN1; - ocp->protValThr[PROT_VAL_THRESHOLD_1] = PROT_OVER_CURR_POW_DN2; - ocp->protValThr[PROT_VAL_THRESHOLD_2] = PROT_OVER_CURR_POW_DN3; - ocp->protValThr[PROT_VAL_THRESHOLD_3] = PROT_OVER_CURR_POW_OFF; - /* Configure the protection limit time. */ - ocp->protLimitTime[PROT_LIMIT_TIME_0] = PROT_OVER_CURR_LIMIT1_TIME_SEC; - ocp->protLimitTime[PROT_LIMIT_TIME_1] = PROT_OVER_CURR_LIMIT2_TIME_SEC; - ocp->protLimitTime[PROT_LIMIT_TIME_2] = PROT_OVER_CURR_LIMIT3_TIME_SEC; - ocp->recyDelta = PROT_OVER_CURR_RECY_DELTA; -} - -/** - * @brief Over current protection detection. - * @param ocp Over current protection handle. - * @param motorErrStatus Motor error status. - * @param idq DQ-axis feedback currents. - * @retval None. - */ -void OCP_Det(OCP_Handle *ocp, MotorErrStatusReg *motorErrStatus, DqAxis idq) -{ - MCS_ASSERT_PARAM(ocp != NULL); - MCS_ASSERT_PARAM(motorErrStatus != NULL); - /* Calculate current amplitude. */ - ocp->currAmp = Sqrt(idq.d * idq.d + idq.q * idq.q); - - /* Check if value goes over threshold for continuous cycles. */ - if (ocp->currAmp < ocp->protValThr[PROT_VAL_THRESHOLD_0]) { - ocp->protCnt = 0; - return; - } - - if (ocp->protCnt < ocp->protCntLimit) { - ocp->protCnt++; - return; - } - - /* Check which protection level should be activated. */ - /* Once enter error status, error status can only be changed by recover api. */ - /* protect level: level 4. */ - if (ocp->currAmp >= ocp->protValThr[PROT_VAL_THRESHOLD_3] && ocp->protLevel < PROT_LEVEL_4) { - ocp->protLevel = PROT_LEVEL_4; - motorErrStatus->Bit.overCurrErr = 1; - ocp->protCnt = 0; - return; - } - - /* Protect level: level 3. */ - if (ocp->currAmp >= ocp->protValThr[PROT_VAL_THRESHOLD_2] && ocp->protLevel < PROT_LEVEL_3) { - ocp->protLevel = PROT_LEVEL_3; - motorErrStatus->Bit.overCurrErr = 1; - ocp->protCnt = 0; - ocp->timer = 0.0f; - return; - } - - /* Protect level: level 2. */ - if (ocp->currAmp >= ocp->protValThr[PROT_VAL_THRESHOLD_1] && ocp->protLevel < PROT_LEVEL_2) { - ocp->protLevel = PROT_LEVEL_2; - motorErrStatus->Bit.overCurrErr = 1; - ocp->protCnt = 0; - ocp->timer = 0.0f; - return; - } - - /* Protect level: level 1. */ - if (ocp->currAmp >= ocp->protValThr[PROT_VAL_THRESHOLD_0] && ocp->protLevel < PROT_LEVEL_1) { - ocp->protLevel = PROT_LEVEL_1; - motorErrStatus->Bit.overCurrErr = 1; - ocp->protCnt = 0; - ocp->timer = 0.0f; - return; - } -} - -/** - * @brief Over current protection execution. - * @param ocp Over current protection handle. - * @param idqRef DQ-axis current references. - * @param aptAddr Three-phase APT address pointer. - * @retval None. - */ -void OCP_Exec(OCP_Handle *ocp, DqAxis *idqRef, APT_RegStruct **aptAddr) -{ - MCS_ASSERT_PARAM(ocp != NULL); - MCS_ASSERT_PARAM(idqRef != NULL); - MCS_ASSERT_PARAM(aptAddr != NULL); - - float id = idqRef->d; - float iq = idqRef->q; - float idqAmp = ocp->currAmp; - /* According to protect level, take corresponding action. */ - switch (ocp->protLevel) { - /* level 4: disable all PWM output. */ - case PROT_LEVEL_4: - /* Disable three-phase pwm output. */ - ProtSpo_Exec(aptAddr); - idqRef->d = 0.0f; - idqRef->q = 0.0f; - break; - - /* level 3: derate speed reference. */ - case PROT_LEVEL_3: - ocp->timer += ocp->ts; - if (ocp->timer > ocp->protLimitTime[PROT_LIMIT_TIME_2]) { - idqRef->d = id / idqAmp * PROT_MOTOR_RATED_CURR; - idqRef->q = iq / idqAmp * PROT_MOTOR_RATED_CURR; - } - break; - - /* level 2: derate speed reference. */ - case PROT_LEVEL_2: - ocp->timer += ocp->ts; - if (ocp->timer > ocp->protLimitTime[PROT_LIMIT_TIME_1]) { - idqRef->d = id / idqAmp * PROT_MOTOR_RATED_CURR; - idqRef->q = iq / idqAmp * PROT_MOTOR_RATED_CURR; - } - break; - - /* level 1: derate speed reference. */ - case PROT_LEVEL_1: - ocp->timer += ocp->ts; - if (ocp->timer > ocp->protLimitTime[PROT_LIMIT_TIME_0]) { - idqRef->d = id / idqAmp * PROT_MOTOR_RATED_CURR; - idqRef->q = iq / idqAmp * PROT_MOTOR_RATED_CURR; - } - break; - - /* level 0: take no protection action. */ - case PROT_LEVEL_0: - break; - - default: - break; - } -} - -/** - * @brief Over current protection recovery. - * @param ocp Over current protection handle. - * @param motorErrStatus Motor error status. - * @retval None. - */ -void OCP_Recy(OCP_Handle *ocp, MotorErrStatusReg *motorErrStatus) -{ - MCS_ASSERT_PARAM(ocp != NULL); - /* If not under error state, just return without any operation. */ - if (!motorErrStatus->Bit.overCurrErr) { - return; - } - - /* Calculate current amplitude. */ - float currAmp = ocp->currAmp; - - /* According to protection level, take corresponding recovery action. */ - switch (ocp->protLevel) { - /* level 4 */ - case PROT_LEVEL_4: - if (currAmp < ocp->protValThr[PROT_VAL_THRESHOLD_3] - ocp->recyDelta) { - ocp->recyCnt++; - if (ocp->recyCnt > ocp->recyCntLimit) { - ocp->protLevel = PROT_LEVEL_3; - ocp->recyCnt = 0; - } - } - break; - - /* level 3 */ - case PROT_LEVEL_3: - if (currAmp < ocp->protValThr[PROT_VAL_THRESHOLD_2] - ocp->recyDelta) { - ocp->recyCnt++; - if (ocp->recyCnt > ocp->recyCntLimit) { - ocp->protLevel = PROT_LEVEL_2; - ocp->recyCnt = 0; - } - } - break; - - /* level 2 */ - case PROT_LEVEL_2: - if (currAmp < ocp->protValThr[PROT_VAL_THRESHOLD_1] - ocp->recyDelta) { - ocp->recyCnt++; - if (ocp->recyCnt > ocp->recyCntLimit) { - ocp->protLevel = PROT_LEVEL_1; - ocp->recyCnt = 0; - } - } - break; - - /* level 1 */ - case PROT_LEVEL_1: - if (currAmp < ocp->protValThr[PROT_VAL_THRESHOLD_0] - ocp->recyDelta) { - ocp->recyCnt++; - if (ocp->recyCnt > ocp->recyCntLimit) { - ocp->protLevel = PROT_LEVEL_0; - ocp->recyCnt = 0; - } - } - break; - - /* level 0 */ - case PROT_LEVEL_0: - motorErrStatus->Bit.overCurrErr = 0; - break; - - default: - break; - } -} - -/** - * @brief Over current protection error status clear. - * @param ocp Over current protection handle. - * @retval None. - */ -void OCP_Clear(OCP_Handle *ocp) -{ - MCS_ASSERT_PARAM(ocp != NULL); - /* Clear the history value. */ - ocp->protCnt = 0; - ocp->protLevel = PROT_LEVEL_0; - ocp->recyCnt = 0; - ocp->timer = 0.0f; -} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_curr_prot.h b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_curr_prot.h deleted file mode 100644 index e9916fe4..00000000 --- a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_curr_prot.h +++ /dev/null @@ -1,52 +0,0 @@ -/** - * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_curr_prot.h - * @author MCU Algorithm Team - * @brief This file contains current protection data struct and api declaration. - */ - -/* Define to prevent recursive inclusion ------------------------------------- */ -#ifndef McuMagicTag_MCS_CURR_PROT_H -#define McuMagicTag_MCS_CURR_PROT_H - -/* Includes ------------------------------------------------------------------------------------ */ -#include "mcs_prot_cmm.h" -#include "mcs_typedef.h" -#include "apt_ip.h" - -typedef struct { - unsigned int protCnt; - unsigned int recyCnt; - unsigned int protCntLimit; - unsigned int recyCntLimit; - float currAmp; - float protValThr[PROT_VAL_THRESHOLD_NUMS]; /* from low current level to high. */ - float protLimitTime[PROT_LIMIT_TIME_NUMS]; /* from level 3 to level 1. */ - float recyDelta; - float timer; - float ts; - PROT_Level protLevel; -} OCP_Handle; - -void OCP_Init(OCP_Handle *ocp, float ts); -void OCP_Det(OCP_Handle *ocp, MotorErrStatusReg *motorErrStatus, DqAxis idq); -void OCP_Exec(OCP_Handle *ocp, DqAxis *idqRef, APT_RegStruct **aptAddr); -void OCP_Recy(OCP_Handle *ocp, MotorErrStatusReg *motorErrStatus); -void OCP_Clear(OCP_Handle *ocp); - -#endif diff --git a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_dc_volt_prot.c b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_dc_volt_prot.c deleted file mode 100644 index b0541a2e..00000000 --- a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_dc_volt_prot.c +++ /dev/null @@ -1,495 +0,0 @@ -/** - * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_dc_volt_prot.c - * @author MCU Algorithm Team - * @brief This file contains dc-link voltage protection api declaration. - */ - -/* Includes ------------------------------------------------------------------------------------ */ -#include "mcs_dc_volt_prot.h" -#include "mcs_prot_user_config.h" -#include "mcs_assert.h" - -/** - * @brief Initilization over dc-link voltage protection function. - * @param ovp Over dc-link voltage protection handle. - * @param ts Ctrl period (s). - * @retval None. - */ -void OVP_Init(OVP_Handle *ovp, float ts) -{ - MCS_ASSERT_PARAM(ovp != NULL); - MCS_ASSERT_PARAM(ts > 0.0f); - ovp->ts = ts; - OVP_Clear(ovp); - ovp->protCntLimit = PROT_CNT_LIMIT; - ovp->recyCntLimit = OVER_VOLT_RECY_CNT_LIMIT; - /* Configuring four levels of overvoltage protection thresholds. */ - ovp->protValThr[PROT_VAL_THRESHOLD_0] = PROT_OVER_VOLT_BRK_ON1; - ovp->protValThr[PROT_VAL_THRESHOLD_1] = PROT_OVER_VOLT_BRK_ON2; - ovp->protValThr[PROT_VAL_THRESHOLD_2] = PROT_OVER_VOLT_BRK_ON3; - ovp->protValThr[PROT_VAL_THRESHOLD_3] = PROT_OVER_VOLT_BRK_ALL; - /* Configure the protection limit time. */ - ovp->protLimitTime[PROT_LIMIT_TIME_0] = PROT_OVER_VOLT_LIMIT1_TIME_SEC; - ovp->protLimitTime[PROT_LIMIT_TIME_1] = PROT_OVER_VOLT_LIMIT2_TIME_SEC; - ovp->protLimitTime[PROT_LIMIT_TIME_2] = PROT_OVER_VOLT_LIMIT3_TIME_SEC; - ovp->recyDelta = PROT_OVER_VOLT_RECY_DELTA; -} - -/** - * @brief Initilization lower dc-link voltage protection function. - * @param lvp Lower dc-link voltage protection handle. - * @param ts Ctrl period (s). - * @retval None. - */ -void LVP_Init(LVP_Handle *lvp, float ts) -{ - MCS_ASSERT_PARAM(lvp != NULL); - MCS_ASSERT_PARAM(ts > 0.0f); - - lvp->ts = ts; - LVP_Clear(lvp); - - lvp->protCntLimit = PROT_CNT_LIMIT; - lvp->recyCntLimit = LOWER_VOLT_RECY_CNT_LIMIT; - /* Configuring four levels of lower voltage protection thresholds. */ - lvp->protValThr[PROT_VAL_THRESHOLD_0] = PROT_LOWER_VOLT_POW_DN1; - lvp->protValThr[PROT_VAL_THRESHOLD_1] = PROT_LOWER_VOLT_POW_DN2; - lvp->protValThr[PROT_VAL_THRESHOLD_2] = PROT_LOWER_VOLT_POW_DN3; - lvp->protValThr[PROT_VAL_THRESHOLD_3] = PROT_LOWER_VOLT_POW_OFF; - /* Configure the protection limit time. */ - lvp->protLimitTime[PROT_LIMIT_TIME_0] = PROT_LOWER_VOLT_LIMIT1_TIME_SEC; - lvp->protLimitTime[PROT_LIMIT_TIME_1] = PROT_LOWER_VOLT_LIMIT2_TIME_SEC; - lvp->protLimitTime[PROT_LIMIT_TIME_2] = PROT_LOWER_VOLT_LIMIT3_TIME_SEC; - lvp->recyDelta = PROT_OVER_VOLT_RECY_DELTA; -} - -/** - * @brief Over dc-link voltage protection detection. - * @param ovp Over dc-link voltage protection handle. - * @param motorErrStatus Motor error status. - * @param udc DC-link voltage feedback (V). - * @retval None. - */ -void OVP_Det(OVP_Handle *ovp, MotorErrStatusReg *motorErrStatus, float udc) -{ - MCS_ASSERT_PARAM(ovp != NULL); - MCS_ASSERT_PARAM(motorErrStatus != NULL); - MCS_ASSERT_PARAM(udc > 0.0f); - /* Check if value goes over threshold for continuous cycles. */ - if (udc < ovp->protValThr[PROT_VAL_THRESHOLD_0]) { - ovp->protCnt = 0; - return; - } - - if (ovp->protCnt < ovp->protCntLimit) { - ovp->protCnt++; - return; - } - - /* Check which protection level should be activated. */ - /* Once enter error status, error status can only be changed by recover api. */ - /* protect level: level 4. */ - if (udc >= ovp->protValThr[PROT_VAL_THRESHOLD_3] && ovp->protLevel < PROT_LEVEL_4) { - ovp->protLevel = PROT_LEVEL_4; - motorErrStatus->Bit.overVoltErr = 1; - ovp->protCnt = 0; - return; - } - - /* Protect level: level 3. */ - if (udc >= ovp->protValThr[PROT_VAL_THRESHOLD_2] && ovp->protLevel < PROT_LEVEL_3) { - ovp->protLevel = PROT_LEVEL_3; - motorErrStatus->Bit.overVoltErr = 1; - ovp->protCnt = 0; - ovp->timer = 0.0f; - return; - } - - /* Protect level: level 2. */ - if (udc >= ovp->protValThr[PROT_VAL_THRESHOLD_1] && ovp->protLevel < PROT_LEVEL_2) { - ovp->protLevel = PROT_LEVEL_2; - motorErrStatus->Bit.overVoltErr = 1; - ovp->protCnt = 0; - ovp->timer = 0.0f; - return; - } - - /* Protect level: level 1. */ - if (udc >= ovp->protValThr[PROT_VAL_THRESHOLD_0] && ovp->protLevel < PROT_LEVEL_1) { - ovp->protLevel = PROT_LEVEL_1; - motorErrStatus->Bit.overVoltErr = 1; - ovp->protCnt = 0; - ovp->timer = 0.0f; - return; - } -} - -/** - * @brief Lower dc-link voltage protection detection. - * @param lvp Lower dc-link voltage protection handle. - * @param motorErrStatus Motor error status. - * @param udc DC-link voltage feedback (V). - * @retval None. - */ -void LVP_Det(LVP_Handle *lvp, MotorErrStatusReg *motorErrStatus, float udc) -{ - MCS_ASSERT_PARAM(lvp != NULL); - MCS_ASSERT_PARAM(motorErrStatus != NULL); - MCS_ASSERT_PARAM(udc > 0.0f); - /* Check if value goes over threshold for continuous cycles. */ - if (udc > lvp->protValThr[PROT_VAL_THRESHOLD_0]) { - lvp->protCnt = 0; - return; - } - - if (lvp->protCnt < lvp->protCntLimit) { - lvp->protCnt++; - return; - } - - /* Check which protection level should be activated. */ - /* Once enter error status, error status can only be changed by recover api. */ - /* protect level: level 4. */ - if (udc <= lvp->protValThr[PROT_VAL_THRESHOLD_3] && lvp->protLevel < PROT_LEVEL_4) { - lvp->protLevel = PROT_LEVEL_4; - motorErrStatus->Bit.lowerVoltErr = 1; - lvp->protCnt = 0; - return; - } - - /* Protect level: level 3. */ - if (udc <= lvp->protValThr[PROT_VAL_THRESHOLD_2] && lvp->protLevel < PROT_LEVEL_3) { - lvp->protLevel = PROT_LEVEL_3; - motorErrStatus->Bit.lowerVoltErr = 1; - lvp->protCnt = 0; - lvp->timer = 0.0f; - return; - } - - /* Protect level: level 2. */ - if (udc <= lvp->protValThr[PROT_VAL_THRESHOLD_1] && lvp->protLevel < PROT_LEVEL_2) { - lvp->protLevel = PROT_LEVEL_2; - motorErrStatus->Bit.lowerVoltErr = 1; - lvp->protCnt = 0; - lvp->timer = 0.0f; - return; - } - - /* Protect level: level 1. */ - if (udc <= lvp->protValThr[PROT_VAL_THRESHOLD_0] && lvp->protLevel < PROT_LEVEL_1) { - lvp->protLevel = PROT_LEVEL_1; - motorErrStatus->Bit.lowerVoltErr = 1; - lvp->protCnt = 0; - lvp->timer = 0.0f; - return; - } -} - -/** - * @brief Over dc-link voltage protection execution. - * @param ovp Over dc-link voltage protection handle. - * @param duty Brake loop output duty (0-1). - * @param aptAddr Three-phase APT address pointer. - * @retval None. - */ -void OVP_Exec(OVP_Handle *ovp, float *duty, APT_RegStruct **aptAddr) -{ - MCS_ASSERT_PARAM(ovp != NULL); - MCS_ASSERT_PARAM(duty != NULL); - MCS_ASSERT_PARAM(aptAddr != NULL); - /* According to protect level, take corresponding action. */ - switch (ovp->protLevel) { - /* level 4: brake loop duty maximum. */ - case PROT_LEVEL_4: - *duty = PROT_OVER_VOLT_BRK_DUTY4; - /* Disable three-phase pwm output. */ - ProtSpo_Exec(aptAddr); - break; - - /* level 3: brake loop duty level 3. */ - case PROT_LEVEL_3: - *duty = PROT_OVER_VOLT_BRK_DUTY2; - ovp->timer += ovp->ts; - if (ovp->timer > ovp->protLimitTime[PROT_LIMIT_TIME_2]) { - *duty = PROT_OVER_VOLT_BRK_DUTY3; - } - break; - - /* level 2: brake loop duty level 2. */ - case PROT_LEVEL_2: - *duty = PROT_OVER_VOLT_BRK_DUTY1; - ovp->timer += ovp->ts; - if (ovp->timer > ovp->protLimitTime[PROT_LIMIT_TIME_1]) { - *duty = PROT_OVER_VOLT_BRK_DUTY2; - } - break; - - /* level 1: brake loop duty level 1. */ - case PROT_LEVEL_1: - ovp->timer += ovp->ts; - if (ovp->timer > ovp->protLimitTime[PROT_LIMIT_TIME_0]) { - *duty = PROT_OVER_VOLT_BRK_DUTY1; - } - break; - - /* level 0: take no protection action. */ - case PROT_LEVEL_0: - break; - - default: - break; - } - return; -} - -/** - * @brief Lower dc-link voltage protection execution. - * @param lvp Lower dc-link voltage protection handle. - * @param spdRef Speed Reference (Hz). - * @param aptAddr Three-phase APT address pointer. - * @retval None. - */ -void LVP_Exec(LVP_Handle *lvp, float *spdRef, APT_RegStruct **aptAddr) -{ - MCS_ASSERT_PARAM(lvp != NULL); - MCS_ASSERT_PARAM(spdRef != NULL); - MCS_ASSERT_PARAM(aptAddr != NULL); - /* According to protect level, take corresponding action. */ - switch (lvp->protLevel) { - /* level 4: disable all PWM output. */ - case PROT_LEVEL_4: - /* Disable three-phase pwm output. */ - ProtSpo_Exec(aptAddr); - *spdRef *= 0.0f; - break; - - /* level 3: derate speed reference. */ - case PROT_LEVEL_3: - *spdRef *= PROT_POW_DN2_PCT; - lvp->timer += lvp->ts; - if (lvp->timer > lvp->protLimitTime[PROT_LIMIT_TIME_2]) { - *spdRef *= PROT_POW_DN3_PCT; - } - break; - - /* level 2: derate speed reference. */ - case PROT_LEVEL_2: - *spdRef *= PROT_POW_DN1_PCT; - lvp->timer += lvp->ts; - if (lvp->timer > lvp->protLimitTime[PROT_LIMIT_TIME_1]) { - *spdRef *= PROT_POW_DN2_PCT; - } - break; - - /* level 1: derate speed reference. */ - case PROT_LEVEL_1: - lvp->timer += lvp->ts; - if (lvp->timer > lvp->protLimitTime[PROT_LIMIT_TIME_0]) { - *spdRef *= PROT_POW_DN1_PCT; - } - break; - - /* level 0: take no protection action. */ - case PROT_LEVEL_0: - break; - - default: - break; - } - return; -} - -/** - * @brief Over dc-link voltage protection recovery. - * @param ovp Over dc-link voltage protection handle. - * @param motorErrStatus Motor error status. - * @param udc DC-link voltage (V). - * @retval None. - */ -void OVP_Recy(OVP_Handle *ovp, MotorErrStatusReg *motorErrStatus, float udc) -{ - MCS_ASSERT_PARAM(ovp != NULL); - /* If not under error state, just return without any operation. */ - if (!motorErrStatus->Bit.overVoltErr) { - return; - } - - /* According to protection level, take corresponding recovery action. */ - switch (ovp->protLevel) { - /* level 4 */ - case PROT_LEVEL_4: - if (udc < ovp->protValThr[PROT_VAL_THRESHOLD_3] - ovp->recyDelta) { - ovp->recyCnt++; - if (ovp->recyCnt > ovp->recyCntLimit) { - ovp->protLevel = PROT_LEVEL_3; - ovp->recyCnt = 0; - } - } - break; - - /* level 3 */ - case PROT_LEVEL_3: - /* If the dc-link voltage is less than threshold 2, level-2 protection is restored. */ - if (udc < ovp->protValThr[PROT_VAL_THRESHOLD_2] - ovp->recyDelta) { - ovp->recyCnt++; - if (ovp->recyCnt > ovp->recyCntLimit) { - ovp->protLevel = PROT_LEVEL_2; - ovp->recyCnt = 0; - } - } - break; - - /* level 2 */ - case PROT_LEVEL_2: - /* If the dc-link voltage is less than threshold 1, level-1 protection is restored. */ - if (udc < ovp->protValThr[PROT_VAL_THRESHOLD_1] - ovp->recyDelta) { - ovp->recyCnt++; - if (ovp->recyCnt > ovp->recyCntLimit) { - ovp->protLevel = PROT_LEVEL_1; - ovp->recyCnt = 0; - } - } - break; - - /* level 1 */ - case PROT_LEVEL_1: - /* If the dc-link voltage is less than threshold 0, level-0 protection is restored. */ - if (udc < ovp->protValThr[PROT_VAL_THRESHOLD_0] - ovp->recyDelta) { - ovp->recyCnt++; - if (ovp->recyCnt > ovp->recyCntLimit) { - ovp->protLevel = PROT_LEVEL_0; - ovp->recyCnt = 0; - } - } - break; - /* level 0 Fault-free state. */ - case PROT_LEVEL_0: - motorErrStatus->Bit.overVoltErr = 0; - break; - - default: - break; - } - return; -} - -/** - * @brief Lower dc-link voltage protection recovery. - * @param lvp Lower dc-link voltage protection handle. - * @param motorErrStatus Motor error status. - * @param udc DC-link voltage (V). - * @retval None. - */ -void LVP_Recy(LVP_Handle *lvp, MotorErrStatusReg *motorErrStatus, float udc) -{ - MCS_ASSERT_PARAM(lvp != NULL); - /* If not under error state, just return without any operation. */ - if (!motorErrStatus->Bit.lowerVoltErr) { - return; - } - - /* According to protection level, take corresponding recovery action. */ - switch (lvp->protLevel) { - /* level 4 */ - case PROT_LEVEL_4: - /* If the dc-link voltage is greater than threshold 3, level-3 protection is restored. */ - if (udc > lvp->protValThr[PROT_VAL_THRESHOLD_3] + lvp->recyDelta) { - lvp->recyCnt++; - if (lvp->recyCnt > lvp->recyCntLimit) { - lvp->protLevel = PROT_LEVEL_3; - lvp->recyCnt = 0; - } - } - break; - - /* level 3 */ - case PROT_LEVEL_3: - /* If the dc-link voltage is greater than threshold 2, level-2 protection is restored. */ - if (udc > lvp->protValThr[PROT_VAL_THRESHOLD_2] + lvp->recyDelta) { - lvp->recyCnt++; - if (lvp->recyCnt > lvp->recyCntLimit) { - lvp->protLevel = PROT_LEVEL_2; - lvp->recyCnt = 0; - } - } - break; - - /* level 2 */ - case PROT_LEVEL_2: - /* If the dc-link voltage is greater than threshold 1, level-1 protection is restored. */ - if (udc > lvp->protValThr[PROT_VAL_THRESHOLD_1] + lvp->recyDelta) { - lvp->recyCnt++; - if (lvp->recyCnt > lvp->recyCntLimit) { - lvp->protLevel = PROT_LEVEL_1; - lvp->recyCnt = 0; - } - } - break; - - /* level 1 */ - case PROT_LEVEL_1: - /* If the dc-link voltage is greater than threshold 0, level-0 protection is restored. */ - if (udc > lvp->protValThr[PROT_VAL_THRESHOLD_0] + lvp->recyDelta) { - lvp->recyCnt++; - if (lvp->recyCnt > lvp->recyCntLimit) { - lvp->protLevel = PROT_LEVEL_0; - lvp->recyCnt = 0; - } - } - break; - /* level 0 Fault-free state. */ - case PROT_LEVEL_0: - motorErrStatus->Bit.lowerVoltErr = 0; - break; - - default: - break; - } - return; -} - -/** - * @brief Over dc-link voltage protection error status clear. - * @param ovp Over voltage protection handle. - * @retval None. - */ -void OVP_Clear(OVP_Handle *ovp) -{ - MCS_ASSERT_PARAM(ovp != NULL); - /* Clear the history value. */ - ovp->protCnt = 0; - ovp->protLevel = PROT_LEVEL_0; - ovp->recyCnt = 0; - ovp->timer = 0.0f; -} - -/** - * @brief Lower dc-link voltage protection error status clear. - * @param lvp Lower voltage protection handle. - * @retval None. - */ -void LVP_Clear(LVP_Handle *lvp) -{ - MCS_ASSERT_PARAM(lvp != NULL); - /* Clear the history value. */ - lvp->protCnt = 0; - lvp->protLevel = PROT_LEVEL_0; - lvp->recyCnt = 0; - lvp->timer = 0.0f; -} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_dc_volt_prot.h b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_dc_volt_prot.h deleted file mode 100644 index 0435156a..00000000 --- a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_dc_volt_prot.h +++ /dev/null @@ -1,69 +0,0 @@ -/** - * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_dc_volt_prot.h - * @author MCU Algorithm Team - * @brief This file contains dc-link voltage protection data struct and api declaration. - */ - -/* Define to prevent recursive inclusion ------------------------------------------------------- */ -#ifndef McuMagicTag_MCS_DC_VOLT_PROT_H -#define McuMagicTag_MCS_DC_VOLT_PROT_H - -/* Includes ------------------------------------------------------------------------------------ */ -#include "mcs_prot_cmm.h" -#include "apt_ip.h" - -typedef struct { - unsigned int protCnt; - unsigned int recyCnt; - unsigned int protCntLimit; - unsigned int recyCntLimit; - float protValThr[PROT_VAL_THRESHOLD_NUMS]; /* from low voltage level to high. */ - float protLimitTime[PROT_LIMIT_TIME_NUMS]; /* from level 3 to level 1. */ - float recyDelta; - float timer; - float ts; - PROT_Level protLevel; -} OVP_Handle; - -typedef struct { - unsigned int protCnt; - unsigned int recyCnt; - unsigned int protCntLimit; - unsigned int recyCntLimit; - float protValThr[PROT_VAL_THRESHOLD_NUMS]; /* from high voltage level to low. */ - float protLimitTime[PROT_LIMIT_TIME_NUMS]; /* from level 3 to level 1. */ - float recyDelta; - float timer; - float ts; - PROT_Level protLevel; -} LVP_Handle; - -void OVP_Init(OVP_Handle *ovp, float ts); -void OVP_Det(OVP_Handle *ovp, MotorErrStatusReg *motorErrStatus, float udc); -void OVP_Exec(OVP_Handle *ovp, float *duty, APT_RegStruct **aptAddr); -void OVP_Recy(OVP_Handle *ovp, MotorErrStatusReg *motorErrStatus, float udc); -void OVP_Clear(OVP_Handle *ovp); - -void LVP_Init(LVP_Handle *lvp, float ts); -void LVP_Det(LVP_Handle *lvp, MotorErrStatusReg *motorErrStatus, float udc); -void LVP_Exec(LVP_Handle *lvp, float *spdRef, APT_RegStruct **aptAddr); -void LVP_Recy(LVP_Handle *lvp, MotorErrStatusReg *motorErrStatus, float udc); -void LVP_Clear(LVP_Handle *lvp); - -#endif diff --git a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_motor_stalling.c b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_motor_stalling.c deleted file mode 100644 index 9dee4d2d..00000000 --- a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_motor_stalling.c +++ /dev/null @@ -1,112 +0,0 @@ -/** - * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_motor_stalling.c - * @author MCU Algorithm Team - * @brief This file contains motor stalling protection data struct and api definition. - */ - -/* Includes ------------------------------------------------------------------------------------ */ -#include "mcs_motor_stalling.h" -#include "mcs_prot_user_config.h" -#include "mcs_math.h" -#include "mcs_math_const.h" -#include "mcs_assert.h" - -/** - * @brief Initilization motor stalling protection function. - * @param stall Motor stalling handle. - * @param ts Ctrl period (s). - * @param currLimit The current amplitude that triggers fault. (A). - * @param spdLimit The speed amplitude that triggers fault. (Hz). - * @param timeLimit The threshold time that current amplitude over the limit (s). - * @retval None. - */ -void STP_Init(STP_Handle *stall, float ts, float currLimit, float spdLimit, float timeLimit) -{ - MCS_ASSERT_PARAM(stall != NULL); - MCS_ASSERT_PARAM(ts > 0.0f); - MCS_ASSERT_PARAM(currLimit > 0.0f); - MCS_ASSERT_PARAM(spdLimit > 0.0f); - MCS_ASSERT_PARAM(timeLimit > 0.0f); - /* Configuring parameters for stalling detection. */ - stall->ts = ts; - /* Current threshold and speed threshold for stalling fault. */ - stall->currAmpLimit = currLimit; - stall->spdLimit = spdLimit; - stall->timeLimit = timeLimit; - stall->timer = 0.0f; -} - -/** - * @brief Motor stalling detection. - * @param stall Motor stalling handle. - * @param motorErrStatus Motor error status. - * @param spd Speed feedback (Hz). - * @param idq Dq-axis current feedback (A). - * @retval None. - */ -void STP_Det_ByCurrSpd(STP_Handle *stall, MotorErrStatusReg *motorErrStatus, float spd, DqAxis idq) -{ - MCS_ASSERT_PARAM(stall != NULL); - MCS_ASSERT_PARAM(motorErrStatus != NULL); - MCS_ASSERT_PARAM(Abs(spd) >= 0.0f); - /* Calculate current amplitude. */ - float currAmp = Sqrt(idq.d * idq.d + idq.q * idq.q); - float spdAbs = Abs(spd); - /* Check if value goes over threshold for continuous cycles. */ - if (currAmp < stall->currAmpLimit || spdAbs > stall->spdLimit) { - stall->timer = 0.0f; - return; - } - /* Time accumulation. */ - if (stall->timer < stall->timeLimit) { - stall->timer += stall->ts; - return; - } - motorErrStatus->Bit.motorStalling = 1; -} - -/** - * @brief Motor stalling protection execution. - * @param motorErrStatus Motor error status. - * @param aptAddr Three-phase APT address pointer. - * @retval None. - */ -void STP_Exec(MotorErrStatusReg *motorErrStatus, APT_RegStruct **aptAddr) -{ - MCS_ASSERT_PARAM(motorErrStatus != NULL); - MCS_ASSERT_PARAM(aptAddr != NULL); - if (motorErrStatus->Bit.motorStalling == 0) { - return; - } - - /* Disable three-phase pwm output. */ - ProtSpo_Exec(aptAddr); - return; -} - -/** - * @brief Motor stalling protection error status clear. - * @param stall Motor stalling handle. - * @retval None. - */ -void STP_Clear(STP_Handle *stall) -{ - MCS_ASSERT_PARAM(stall != NULL); - stall->timer = 0.0f; -} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_motor_stalling.h b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_motor_stalling.h deleted file mode 100644 index a1d1e4be..00000000 --- a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_motor_stalling.h +++ /dev/null @@ -1,45 +0,0 @@ -/** - * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_motor_stalling.h - * @author MCU Algorithm Team - * @brief This file contains motor stalling protection data struct and api declaration. - */ - -/* Define to prevent recursive inclusion ------------------------------------- */ -#ifndef McuMagicTag_MCS_MOTOR_STALLING_PROT_H -#define McuMagicTag_MCS_MOTOR_STALLING_PROT_H - -/* Includes ------------------------------------------------------------------------------------ */ -#include "mcs_prot_cmm.h" -#include "apt_ip.h" -#include "mcs_typedef.h" - -typedef struct { - float currAmpLimit; /**< Feedback current higher than this value triggers fault. (A). */ - float spdLimit; /**< Feedback speed lower than this value triggers fault (Hz). */ - float timeLimit; /**< The threshold time that current and speed feedback over ranges (s). */ - float timer; /**< Timer to get speed and current over range time. */ - float ts; /**< Ctrl period (s). */ -} STP_Handle; - -void STP_Init(STP_Handle *stall, float ts, float currLimit, float spdLimit, float timeLimit); -void STP_Det_ByCurrSpd(STP_Handle *stall, MotorErrStatusReg *motorErrStatus, float spd, DqAxis idq); -void STP_Exec(MotorErrStatusReg *motorErrStatus, APT_RegStruct **aptAddr); -void STP_Clear(STP_Handle *stall); - -#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_temp_prot.c b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_temp_prot.c deleted file mode 100644 index 184ba4a3..00000000 --- a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_temp_prot.c +++ /dev/null @@ -1,262 +0,0 @@ -/** - * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_temp_prot.c - * @author MCU Algorithm Team - * @brief This file contains over temperature protection api definition. - */ - -/* Includes ------------------------------------------------------------------------------------ */ -#include "mcs_temp_prot.h" -#include "mcs_prot_user_config.h" -#include "mcs_assert.h" - -/** - * @brief Initilization over temperation protection function. - * @param otp Over temperature protection handle. - * @param ts Ctrl period. - * @retval None. - */ -void OTP_Init(OTP_Handle *otp, float ts) -{ - MCS_ASSERT_PARAM(otp != NULL); - MCS_ASSERT_PARAM(ts > 0.0f); - otp->ts = ts; - otp->protCntLimit = PROT_CNT_LIMIT; - otp->recyCntLimit = RECY_CNT_LIMIT; - /* Configuring the temperature protection threshold. */ - otp->protValThr[PROT_VAL_THRESHOLD_0] = PROT_OVER_IPM_TEMP_POW_DN1; - otp->protValThr[PROT_VAL_THRESHOLD_1] = PROT_OVER_IPM_TEMP_POW_DN2; - otp->protValThr[PROT_VAL_THRESHOLD_2] = PROT_OVER_IPM_TEMP_POW_DN3; - otp->protValThr[PROT_VAL_THRESHOLD_3] = PROT_OVER_IPM_TEMP_POW_OFF; - /* Configuring the protection limiting time. */ - otp->protLimitTime[PROT_LIMIT_TIME_0] = PROT_OVER_TEMP_LIMIT1_TIME_SEC; - otp->protLimitTime[PROT_LIMIT_TIME_1] = PROT_OVER_TEMP_LIMIT2_TIME_SEC; - otp->protLimitTime[PROT_LIMIT_TIME_2] = PROT_OVER_TEMP_LIMIT3_TIME_SEC; - otp->recyDelta = PROT_OVER_IPM_TEMP_RECY_DELTA; - OTP_Clear(otp); -} - -/** - * @brief Over temperatre protection detection. - * @param otp Over temperature protection handle. - * @param tempErrBit Temperature error status bit. - * @param idq DQ-axis feedback currents. - * @retval None. - */ -void OTP_Det(OTP_Handle *otp, MotorErrStatusReg *motorErrStatus, PROT_ErrBit protBit, float temp) -{ - MCS_ASSERT_PARAM(otp != NULL); - MCS_ASSERT_PARAM(motorErrStatus != NULL); - MCS_ASSERT_PARAM(temp > 0.0f); - /* Check if value goes over threshold for continuous cycles. */ - if (temp < otp->protValThr[PROT_VAL_THRESHOLD_0]) { - otp->protCnt = 0; - ClearBit(&motorErrStatus->all, protBit); - return; - } - - if (otp->protCnt < otp->protCntLimit) { - otp->protCnt++; - return; - } - - /* Check which protection level should be activated. */ - /* Once enter error status, error status can only be changed by recover api. */ - /* protect level: level 4. */ - if (temp >= otp->protValThr[PROT_VAL_THRESHOLD_3] && otp->protLevel < PROT_LEVEL_4) { - otp->protLevel = PROT_LEVEL_4; - SetBit(&motorErrStatus->all, protBit); - otp->protCnt = 0; - return; - } - - /* Protect level: level 3. */ - if (temp >= otp->protValThr[PROT_VAL_THRESHOLD_2] && otp->protLevel < PROT_LEVEL_3) { - otp->protLevel = PROT_LEVEL_3; - SetBit(&motorErrStatus->all, protBit); - otp->protCnt = 0; - otp->timer = 0.0f; - return; - } - - /* Protect level: level 2. */ - if (temp >= otp->protValThr[PROT_VAL_THRESHOLD_1] && otp->protLevel < PROT_LEVEL_2) { - otp->protLevel = PROT_LEVEL_2; - SetBit(&motorErrStatus->all, protBit); - otp->protCnt = 0; - otp->timer = 0.0f; - return; - } - - /* Protect level: level 1. */ - if (temp >= otp->protValThr[PROT_VAL_THRESHOLD_0] && otp->protLevel < PROT_LEVEL_1) { - otp->protLevel = PROT_LEVEL_1; - SetBit(&motorErrStatus->all, protBit); - otp->protCnt = 0; - otp->timer = 0.0f; - return; - } -} - -/** - * @brief Over temperature protection execution. - * @param otp Over temperature protection handle. - * @param spdRef Speed reference (Hz). - * @param aptAddr Three-phase APT address pointer. - * @retval None. - */ -void OTP_Exec(OTP_Handle *otp, float *spdRef, APT_RegStruct **aptAddr) -{ - MCS_ASSERT_PARAM(otp != NULL); - MCS_ASSERT_PARAM(spdRef != NULL); - MCS_ASSERT_PARAM(aptAddr != NULL); - /* According to protect level, take corresponding action. */ - switch (otp->protLevel) { - /* level 4: disable all PWM output. */ - case PROT_LEVEL_4: - /* Disable three-phase pwm output. */ - ProtSpo_Exec(aptAddr); - *spdRef *= 0.0f; - break; - - /* level 3: derate speed reference. */ - case PROT_LEVEL_3: - *spdRef *= PROT_POW_DN2_PCT; - otp->timer += otp->ts; - if (otp->timer > otp->protLimitTime[PROT_LIMIT_TIME_2]) { - *spdRef *= PROT_POW_DN3_PCT; - } - break; - - /* level 2: derate speed reference. */ - case PROT_LEVEL_2: - /* Reducte motor speed to level 2 */ - *spdRef *= PROT_POW_DN1_PCT; - otp->timer += otp->ts; - if (otp->timer > otp->protLimitTime[PROT_LIMIT_TIME_1]) { - *spdRef *= PROT_POW_DN2_PCT; - } - break; - - /* level 0: take no protection action. */ - case PROT_LEVEL_0: - break; - - case PROT_LEVEL_1: - /* Reducte motor speed to level 0 */ - otp->timer += otp->ts; - if (otp->timer > otp->protLimitTime[PROT_LIMIT_TIME_0]) { - /* level 1: derate speed reference. */ - *spdRef *= PROT_POW_DN1_PCT; - } - break; - - default: - break; - } - return; -} - -/** - * @brief Over temperature protection recovery. - * @param otp Over temperature protection handle. - * @param tempErrBit Temperature error status bit. - * @param temp Temperature (celsius). - * @retval None. - */ -void OTP_Recy(OTP_Handle *otp, MotorErrStatusReg *motorErrStatus, PROT_ErrBit protBit, float temp) -{ - MCS_ASSERT_PARAM(otp != NULL); - MCS_ASSERT_PARAM(motorErrStatus != NULL); - MCS_ASSERT_PARAM(temp > 0.0f); - /* If not under error state, just return without any operation. */ - if (otp->protLevel == PROT_LEVEL_0) { - motorErrStatus->all &= (~(1 >> protBit)); - return; - } - - /* According to protection level, take corresponding recovery action. */ - switch (otp->protLevel) { - /* level 4 */ - case PROT_LEVEL_4: - /* If the temperature is less than threshold 3, level-3 protection is restored. */ - if (temp < otp->protValThr[PROT_VAL_THRESHOLD_3] - otp->recyDelta) { - otp->recyCnt++; - if (otp->recyCnt > otp->recyCntLimit) { - otp->protLevel = PROT_LEVEL_3; - otp->recyCnt = 0; - } - } - break; - - /* level 3 */ - case PROT_LEVEL_3: - /* If the temperature is less than threshold 2, level-2 protection is restored. */ - if (temp < otp->protValThr[PROT_VAL_THRESHOLD_2] - otp->recyDelta) { - otp->recyCnt++; - if (otp->recyCnt > otp->recyCntLimit) { - otp->protLevel = PROT_LEVEL_2; - otp->recyCnt = 0; - } - } - break; - - /* level 2 */ - case PROT_LEVEL_2: - /* If the temperature is less than threshold 1, level-1 protection is restored. */ - if (temp < otp->protValThr[PROT_VAL_THRESHOLD_1] - otp->recyDelta) { - otp->recyCnt++; - if (otp->recyCnt > otp->recyCntLimit) { - otp->protLevel = PROT_LEVEL_1; - otp->recyCnt = 0; - } - } - break; - - /* level 1 */ - case PROT_LEVEL_1: - /* If the temperature is less than threshold 0, level-0 protection is restored. */ - if (temp < otp->protValThr[PROT_VAL_THRESHOLD_0] - otp->recyDelta) { - otp->recyCnt++; - if (otp->recyCnt > otp->recyCntLimit) { - otp->protLevel = PROT_LEVEL_0; - otp->recyCnt = 0; - } - } - break; - - default: - break; - } - return; -} - -/** - * @brief Over temperature protection error status clear. - * @param otp Over temperature protection handle. - * @retval None. - */ -void OTP_Clear(OTP_Handle *otp) -{ - MCS_ASSERT_PARAM(otp != NULL); - /* Clear the history value. */ - otp->protCnt = 0; - otp->protLevel = PROT_LEVEL_0; - otp->recyCnt = 0; - otp->timer = 0.0f; -} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_temp_prot.h b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_temp_prot.h deleted file mode 100644 index 2b9d99a7..00000000 --- a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/protection/mcs_temp_prot.h +++ /dev/null @@ -1,49 +0,0 @@ -/** - * @copyright Copyright (c) 2023, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_temp_prot.h - * @author MCU Algorithm Team - * @brief his file contains over temperature protection api declaration. - */ - -/* Define to prevent recursive inclusion ------------------------------------- */ -#ifndef McuMagicTag_MCS_TEMP_PROT_H -#define McuMagicTag_MCS_TEMP_PROT_H - -/* Includes ------------------------------------------------------------------------------------ */ -#include "mcs_prot_cmm.h" -#include "apt_ip.h" - -typedef struct { - unsigned int protCnt; - unsigned int recyCnt; - unsigned int protCntLimit; - unsigned int recyCntLimit; - float protValThr[PROT_VAL_THRESHOLD_NUMS]; /* from low current level to high. */ - float protLimitTime[PROT_LIMIT_TIME_NUMS]; /* from level 3 to level 1. */ - float recyDelta; - float ts; - float timer; - PROT_Level protLevel; -} OTP_Handle; - -void OTP_Init(OTP_Handle *otp, float ts); -void OTP_Det(OTP_Handle *otp, MotorErrStatusReg *motorErrStatus, PROT_ErrBit protBit, float temp); -void OTP_Exec(OTP_Handle *otp, float *spdRef, APT_RegStruct **aptAddr); -void OTP_Recy(OTP_Handle *otp, MotorErrStatusReg *motorErrStatus, PROT_ErrBit protBit, float temp); -void OTP_Clear(OTP_Handle *otp); -#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/src/mcs_carrier.c b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/src/mcs_carrier.c deleted file mode 100644 index 9fc8afac..00000000 --- a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/src/mcs_carrier.c +++ /dev/null @@ -1,144 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_carrier.c - * @author MCU Algorithm Team - * @brief This file provides carrier interrupt application for motor control. - */ - -#include "mcs_carrier.h" -#include "mcs_math.h" -#include "typedefs.h" -#include "mcs_assert.h" -#include "mcs_user_config.h" -#include "mcs_ctlmode_config.h" - -/** - * @brief Synchronous rotation coordinate system angle. - * @param mtrCtrl The motor control handle. - * @retval None. - */ -static void MCS_SyncCoorAngle(MTRCTRL_Handle *mtrCtrl) -{ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - /* Synchronous rotation coordinate system angle. */ - switch (mtrCtrl->stateMachine) { - case FSM_STARTUP: - /* Current ramp angle is 0. */ - if (mtrCtrl->startup.stage == STARTUP_STAGE_CURR) { - mtrCtrl->axisAngle = 0; - } else if (mtrCtrl->startup.stage == STARTUP_STAGE_SPD) { /* IF control phase angle self-addition. */ - mtrCtrl->axisAngle = IF_CurrAngleCalc(&mtrCtrl->ifCtrl, mtrCtrl->spdRefHz); - } else if (mtrCtrl->startup.stage == STARTUP_STAGE_SWITCH) { /* Switch Angle */ - mtrCtrl->axisAngle = mtrCtrl->smo.elecAngle; - } - break; - - case FSM_RUN: - mtrCtrl->axisAngle = mtrCtrl->smo.elecAngle; - break; - - default: - mtrCtrl->axisAngle = 0; - break; - } -} - -/** - * @brief PWM waveform setting and sampling point setting for a single resistors and dual resistors. - * @param mtrCtrl The motor control handle. - * @retval None. - */ -static void MCS_PwmAdcSet(MTRCTRL_Handle *mtrCtrl) -{ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - SampleMode sampleMode = mtrCtrl->sampleMode; - /* Set the duty cycle according to the sampling mode. */ - if (sampleMode == DUAL_RESISTORS) { - SVPWM_Exec(&mtrCtrl->sv, &mtrCtrl->vabRef, &mtrCtrl->dutyUvw); - mtrCtrl->setPwmDutyCb(&mtrCtrl->dutyUvw, &mtrCtrl->dutyUvw); - } else if (sampleMode == SINGLE_RESISTOR) { - R1SVPWM_Exec(&mtrCtrl->r1Sv, &mtrCtrl->vabRef, &mtrCtrl->dutyUvwLeft, &mtrCtrl->dutyUvwRight); - mtrCtrl->setPwmDutyCb(&mtrCtrl->dutyUvwLeft, &mtrCtrl->dutyUvwRight); - /* The ADC sampling point position needs to be set based on the phase shift of a single resistors. */ - mtrCtrl->setADCTriggerTimeCb(mtrCtrl->r1Sv.samplePoint[0] * mtrCtrl->aptMaxcntCmp, \ - mtrCtrl->r1Sv.samplePoint[1] * mtrCtrl->aptMaxcntCmp); - } -} - -/** - * @brief Carrier interrupt function. - * @param mtrCtrl The motor control handle. - * @retval None. - */ -void MCS_CarrierProcess(MTRCTRL_Handle *mtrCtrl) -{ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - UvwAxis *currUvw = &mtrCtrl->currUvw; - AlbeAxis *currAlbe = &mtrCtrl->iabFbk; - AlbeAxis *vab = &mtrCtrl->vabRef; - SampleMode sampleMode = mtrCtrl->sampleMode; - /* sample mode verify */ - if (sampleMode > SINGLE_RESISTOR || mtrCtrl->setADCTriggerTimeCb == NULL) { - return; - } - /* param verify */ - if (mtrCtrl->setPwmDutyCb == NULL || mtrCtrl->readCurrUvwCb == NULL) { - return; - } - /* Read the three-phase current value. */ - mtrCtrl->readCurrUvwCb(currUvw); - /* Clark Calc */ - ClarkeCalc(currUvw, currAlbe); - /* Smo observation */ - if (mtrCtrl->obserType == FOC_OBSERVERTYPE_SMO1TH) { - /* Smo observation */ - FOSMO_Exec(&mtrCtrl->smo, currAlbe, vab, mtrCtrl->spdRefHz); - } else if (mtrCtrl->obserType == FOC_OBSERVERTYPE_SMO4TH) { - /* Smo4th observation */ - SMO4TH_Exec(&mtrCtrl->smo4th, currAlbe, vab); - mtrCtrl->smo.spdEst = mtrCtrl->smo4th.spdEst; - mtrCtrl->smo.elecAngle = mtrCtrl->smo4th.elecAngle; - } - /* Synchronization angle */ - MCS_SyncCoorAngle(mtrCtrl); - - /* Park transformation */ - ParkCalc(currAlbe, mtrCtrl->axisAngle, &mtrCtrl->idqFbk); - - /* statemachine */ - switch (mtrCtrl->stateMachine) { - case FSM_STARTUP: - case FSM_RUN: - CURRCTRL_Exec(&mtrCtrl->currCtrl, &mtrCtrl->vdqRef, mtrCtrl->smo.spdEst, 0); - InvParkCalc(&mtrCtrl->vdqRef, mtrCtrl->axisAngle, vab); - MCS_PwmAdcSet(mtrCtrl); - break; - - case FSM_CAP_CHARGE: - case FSM_CLEAR: - case FSM_IDLE: - mtrCtrl->smo4th.spdEst = 0.0f; - break; - - default: - vab->alpha = 0.0f; - vab->beta = 0.0f; - MCS_PwmAdcSet(mtrCtrl); - break; - } -} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/src/mcs_motor_process.c b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/src/mcs_motor_process.c deleted file mode 100644 index 79bc3949..00000000 --- a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/src/mcs_motor_process.c +++ /dev/null @@ -1,950 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_motor_process.c - * @author MCU Algorithm Team - * @brief This file provides motor application. - * @details Single FOC application based on the ECMCU105H/ECBMCU201MPC board - * 1) Motor model is Gimbal GBM2804H-100T. - * 2) Select the pmsm sensorless 2shunt foc example in the sample column - of chipConfig and click Generate Code. - * 3) It's power supply must be changed to 12V. - */ -#include "main.h" -#include "mcs_user_config.h" -#include "mcs_math.h" -#include "hmi_module.h" -#include "mcs_ctlmode_config.h" -#include "mcs_prot_user.h" -#include "mcs_prot_user_config.h" -#include "mcs_math_const.h" -#include "mcs_motor_process.h" -#include "mcs_chip_config.h" -#include - -/*------------------------------- Macro Definition -----------------------------------------------*/ -#define US_PER_MS 1000 -#define ANGLE_RANGE_ABS 65536 -#define ANGLE_360_F 65536.0f /* 0 - 65536 indicates 0 to 360. */ -#define APT_FULL_DUTY 1.0f -#define TEMP_3 3.0f -#define TEMP_15 15.0f -#define TEMP_30 30.0f -#define TEMP_45 45.0f -#define TEMP_60 60.0f -#define TEMP_RES_15 78.327f -#define TEMP_RES_30 36.776f -#define TEMP_RES_45 18.301f -#define TEMP_RES_60 9.607f -#define CNT_10 10 -#define CNT_5000 5000 -#define LEVEL_4 4 -#define MOTOR_START_DELAY 2 -#define ADC_READINIT_DELAY 1 -#define ADC_READINIT_TIMES 20 -#define ADC_TRIMVALUE_MIN 1800.0f -#define ADC_TRIMVALUE_MAX 2200.0f -/*------------------------------- Param Definition -----------------------------------------------*/ -/* Motor parameters. */ -/* Np, Rs, Ld, Lq, Psif, J, Nmax, Currmax, PPMR, zShift */ -static MOTOR_Param g_motorParam = MOTORPARAM_DEFAULTS; -static APT_RegStruct* g_apt[PHASE_MAX_NUM] = {APT_U, APT_V, APT_W}; -/* Motor control handle */ -static MTRCTRL_Handle g_mc = {0}; - -/* Motor speed loop PI param. */ -static void SPDCTRL_InitWrapper(SPDCTRL_Handle *spdHandle, float ts) -{ - /* Speed loop param assignment. */ - PI_Param spdPi = { - .kp = SPD_KP, - .ki = SPD_KI, - .lowerLim = SPD_LOWERLIM, - .upperLim = SPD_UPPERLIM, - }; - /* Speed loop param init. */ - SPDCTRL_Init(spdHandle, &g_motorParam, spdPi, ts); -} - -/* Motor current Loop PI param. */ -static void CURRCTRL_InitWrapper(CURRCTRL_Handle *currHandle, DqAxis *idqRef, DqAxis *idqFbk, float ts) -{ - /* Axis-D current loop param assignment. */ - PI_Param dCurrPi = { - .kp = CURRDAXIS_KP, - .ki = CURRDAXIS_KI, - .lowerLim = CURR_LOWERLIM, - .upperLim = CURR_UPPERLIM, - }; - /* Axis-Q current loop param assignment. */ - PI_Param qCurrPi = { - .kp = CURRQAXIS_KP, - .ki = CURRQAXIS_KI, - .lowerLim = CURR_LOWERLIM, - .upperLim = CURR_UPPERLIM, - }; - /* Current loop param init. */ - CURRCTRL_Init(currHandle, &g_motorParam, idqRef, idqFbk, dCurrPi, qCurrPi, ts); -} - -/* First order smo param. */ -static void FOSMO_InitWrapper(FOSMO_Handle *fosmo, float ts) -{ - /* Smo param assignment. */ - FOSMO_Param fosmoParam = { - .gain = FOSMO_GAIN, - .lambda = FOSMO_LAMBDA, - .fcEmf = FOSMO_EMF_CUTOFF_FREQ, - .fcLpf = SPEED_FILTER_CUTOFF_FREQUENCY, - .pllBdw = FOSMO_PLL_BDW, - }; - /* Init smo param. */ - FOSMO_Init(fosmo, fosmoParam, &g_motorParam, ts); -} - -/* Smo4th param. */ -static void SMO4TH_InitWrapper(SMO4TH_Handle *smo4TH) -{ - /* Smo4th param assignment. */ - SMO4TH_Param smo4thParam = { - .kd = SMO4TH_KD, - .kq = SMO4TH_KQ, - .pllBdw = SMO4TH_PLL_BDW, - .fcLpf = SMO4TH_SPD_FILTER_CUTOFF_FREQ, - }; - /* Init smo param. */ - SMO4TH_Init(smo4TH, smo4thParam, &g_motorParam, CTRL_CURR_PERIOD); -} - -/*------------------------------- Function Definition -----------------------------------------------*/ -/** - * @brief Initialzer of system tick. - * @param mtrCtrl Motor control struct handle. - * @retval None. - */ -static void TimerTickInit(MTRCTRL_Handle *mtrCtrl) -{ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - mtrCtrl->sysTickCnt = 0; - mtrCtrl->msTickNum = US_PER_MS / SYSTICK_PERIOD_US; - mtrCtrl->capChargeTickNum = (INV_CAP_CHARGE_MS * US_PER_MS / SYSTICK_PERIOD_US); -} - -/** - * @brief Init motor control task. - * @retval None. - */ -static void TSK_Init(void) -{ - g_mc.motorStateFlag = 0; - g_mc.uartHeartDetCnt = 0; - g_mc.uartTimeStamp = 0; - g_mc.stateMachine = FSM_IDLE; - g_mc.currCtrlPeriod = CTRL_CURR_PERIOD; /* Init current controller */ - g_mc.aptMaxcntCmp = g_apt0.waveform.timerPeriod; - g_mc.sampleMode = DUAL_RESISTORS; - g_mc.obserType = FOC_OBSERVERTYPE_SMO4TH; /* Init foc observe mode */ - g_mc.controlMode = FOC_CONTROLMODE_SPEED; /* Init motor control mode */ - g_mc.adcCurrCofe = ADC_CURR_COFFI; - g_mc.spdAdjustMode = CUST_SPEED_ADJUST; - g_mc.uartConnectFlag = DISCONNECT; - g_mc.spdCmdHz = USER_MIN_SPD_HZ; /* Motor initialization speed */ - - g_mc.adc0Compensate = ADC0COMPENSATE; /* Phase-u current init adc shift trim value */ - g_mc.adc1Compensate = ADC1COMPENSATE; /* Phase-w current init adc shift trim value */ - - IF_Init(&g_mc.ifCtrl, CTRL_IF_CURR_AMP_A, USER_CURR_SLOPE, CTRL_SYSTICK_PERIOD, CTRL_CURR_PERIOD); - RMG_Init(&g_mc.spdRmg, CTRL_SYSTICK_PERIOD, USER_SPD_SLOPE); /* Init speed slope */ - MtrParamInit(&g_mc.mtrParam, g_motorParam); - - TimerTickInit(&g_mc); - if (g_mc.sampleMode == DUAL_RESISTORS) { - SVPWM_Init(&g_mc.sv, INV_VOLTAGE_BUS * ONE_DIV_SQRT3); /* Dual resistors SVPWM init. */ - } else if (g_mc.sampleMode == SINGLE_RESISTOR) { - /* Single resistor SVPWM init. */ - R1SVPWM_Init(&g_mc.r1Sv, INV_VOLTAGE_BUS * ONE_DIV_SQRT3, SAMPLE_POINT_SHIFT, SAMPLE_WINDOW_DUTY); - } - - SPDCTRL_InitWrapper(&g_mc.spdCtrl, CTRL_SYSTICK_PERIOD); - CURRCTRL_InitWrapper(&g_mc.currCtrl, &g_mc.idqRef, &g_mc.idqFbk, CTRL_CURR_PERIOD); - FOSMO_InitWrapper(&g_mc.smo, CTRL_CURR_PERIOD); - SMO4TH_InitWrapper(&g_mc.smo4th); - - STARTUP_Init(&g_mc.startup, USER_SWITCH_SPDBEGIN_HZ, USER_SWITCH_SPDBEGIN_HZ + TEMP_3); - - MotorProt_Init(&g_mc.prot); /* Init protect state comond */ - OCP_Init(&g_mc.prot.ocp, CTRL_CURR_PERIOD); - OVP_Init(&g_mc.prot.ovp, CTRL_SYSTICK_PERIOD); - LVP_Init(&g_mc.prot.lvp, CTRL_SYSTICK_PERIOD); - OTP_Init(&g_mc.prot.otp, CTRL_SYSTICK_PERIOD); - STP_Init(&g_mc.prot.stall, CTRL_SYSTICK_PERIOD, PROT_STALLING_CURR_AMP_LIMIT, - PROT_STALLING_SPD_LIMIT, PROT_STALLING_TIME_LIMIT); -} - -/** - * @brief Clear historical values of all controller before start-up. - * @param mtrCtrl The motor control handle. - * @retval None. - */ -static void ClearBeforeStartup(MTRCTRL_Handle *mtrCtrl) -{ - /* Verify Parameters */ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - /* The initial angle is 0. */ - mtrCtrl->axisAngle = 0; - - mtrCtrl->spdRefHz = 0.0f; - /* The initial dq-axis reference current is 0. */ - mtrCtrl->idqRef.d = 0.0f; - mtrCtrl->idqRef.q = 0.0f; - - mtrCtrl->vdqRef.d = 0.0f; - mtrCtrl->vdqRef.q = 0.0f; - /* Clear Duty Cycle Value. The initial duty cycle is 0.5. */ - mtrCtrl->dutyUvwLeft.u = 0.5f; - mtrCtrl->dutyUvwLeft.v = 0.5f; - mtrCtrl->dutyUvwLeft.w = 0.5f; - mtrCtrl->dutyUvwRight.u = 0.5f; - mtrCtrl->dutyUvwRight.v = 0.5f; - mtrCtrl->dutyUvwRight.w = 0.5f; - - mtrCtrl->prot.motorErrStatus.all = 0x00; - - RMG_Clear(&mtrCtrl->spdRmg); /* Clear the history value of speed slope control */ - CURRCTRL_Clear(&mtrCtrl->currCtrl); - IF_Clear(&mtrCtrl->ifCtrl); - SPDCTRL_Clear(&mtrCtrl->spdCtrl); - FOSMO_Clear(&mtrCtrl->smo); - SMO4TH_Clear(&mtrCtrl->smo4th); - STARTUP_Clear(&mtrCtrl->startup); - R1SVPWM_Clear(&mtrCtrl->r1Sv); - - OTP_Clear(&mtrCtrl->prot.otp); - OCP_Clear(&mtrCtrl->prot.ocp); - OVP_Clear(&mtrCtrl->prot.ovp); - LVP_Clear(&mtrCtrl->prot.lvp); -} - -/** - * @brief To set the comparison value of the IGBT single-resistance ADC sampling trigger position. - * @param aptx The APT register struct handle. - * @param cntCmpA A Count compare reference of time-base counter. - * @param cntCmpB B Count compare reference of time-base counter. - * @param maxCntCmp Maximum Comparison Value - * @retval None. - */ -static void MCS_SetAdcCompareR1(APT_RegStruct *aptx, unsigned short cntCmpA, - unsigned short cntCmpB, unsigned short maxCntCmp) -{ - unsigned short tmp; - /* Sets the A Count compare reference of time-base counter. */ - tmp = (unsigned short)Clamp((float)(cntCmpA), (float)(maxCntCmp - 1), 1.0f); - DCL_APT_SetCounterCompare(aptx, APT_COMPARE_REFERENCE_A, tmp); - /* Sets the B Count compare reference of time-base counter. */ - tmp = (unsigned short)Clamp((float)(cntCmpB), (float)(maxCntCmp - 1), 1.0f); - DCL_APT_SetCounterCompare(aptx, APT_COMPARE_REFERENCE_B, tmp); -} - -/** - * @brief Open the three-phase lower pipe. - * @param aptAddr Three-phase APT address pointer. - * @param maxDutyCnt Max duty count. - * @retval None. - */ -static void AptTurnOnLowSidePwm(APT_RegStruct **aptAddr, unsigned short maxDutyCnt) -{ - MCS_ASSERT_PARAM(aptAddr != NULL); - MCS_ASSERT_PARAM(maxDutyCnt != 0); - unsigned short dutyCnt; - dutyCnt = (unsigned short)(maxDutyCnt * APT_FULL_DUTY); - /* Open the three-phase lower pipe */ - for (unsigned int i = 0; i < PHASE_MAX_NUM; i++) { - APT_RegStruct *aptx = (APT_RegStruct *)(aptAddr[i]); - DCL_APT_SetCounterCompare(aptx, APT_COMPARE_REFERENCE_C, dutyCnt); - DCL_APT_SetCounterCompare(aptx, APT_COMPARE_REFERENCE_D, dutyCnt); - } -} - -/** - * @brief Enable three-phase pwm output. - * @param aptAddr Three-phase APT address pointer. - * @retval None. - */ -static void MotorPwmOutputEnable(APT_RegStruct **aptAddr) -{ - MCS_ASSERT_PARAM(aptAddr != NULL); - /* Enable three-phase pwm output */ - for (unsigned int i = 0; i < PHASE_MAX_NUM; i++) { - APT_RegStruct *aptx = (APT_RegStruct *)(aptAddr[i]); - aptx->PG_OUT_FRC.BIT.rg_pga_frc_en = BASE_CFG_UNSET; - aptx->PG_OUT_FRC.BIT.rg_pgb_frc_en = BASE_CFG_UNSET; - } -} - -/** - * @brief Disable three-phase pwm output. - * @param aptAddr Three-phase APT address pointer. - * @retval None. - */ -static void MotorPwmOutputDisable(APT_RegStruct **aptAddr) -{ - MCS_ASSERT_PARAM(aptAddr != NULL); - /* Disable three-phase pwm output. */ - for (unsigned int i = 0; i < PHASE_MAX_NUM; i++) { - APT_RegStruct *aptx = (APT_RegStruct *)(aptAddr[i]); - aptx->PG_OUT_FRC.BIT.rg_pga_frc_en = BASE_CFG_SET; - aptx->PG_OUT_FRC.BIT.rg_pgb_frc_en = BASE_CFG_SET; - DCL_APT_ForcePWMOutputLow(aptx); - } -} - -/** - * @brief Smo IF angle difference calculation. - * @param smoElecAngle Smo electrical angle. - * @param ifCtrlAngle IF control angle. - * @retval signed short angle difference. - */ -static float SmoIfAngleDiffCalc(float smoElecAngle, float ifCtrlAngle) -{ - float diff = AngleSub(smoElecAngle, ifCtrlAngle); - /* Smo IF angle difference calculation */ - return diff; -} - -/** - * @brief Construct a new mcs startupswitch object. - * @param mtrCtrl The motor control handle. - * @retval None. - */ -static void MCS_StartupSwitch(MTRCTRL_Handle *mtrCtrl) -{ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - STARTUP_Handle *startup = &mtrCtrl->startup; - DqAxis *idqRef = &mtrCtrl->idqRef; - float iftargetAmp = mtrCtrl->ifCtrl.targetAmp; - float spdRefHz = mtrCtrl->spdRefHz; - - switch (startup->stage) { - case STARTUP_STAGE_CURR: - if (mtrCtrl->ifCtrl.curAmp >= iftargetAmp) { - /* Stage change */ - idqRef->q = iftargetAmp; - startup->stage = STARTUP_STAGE_SPD; - } else { - /* current amplitude increase */ - idqRef->q = IF_CurrAmpCalc(&mtrCtrl->ifCtrl); - spdRefHz = 0.0f; - } - break; - case STARTUP_STAGE_SPD: - /* current frequency increase */ - if (Abs(spdRefHz) >= startup->spdBegin) { - /* Stage change */ - startup->stage = STARTUP_STAGE_SWITCH; - TrigVal localTrigVal; - TrigCalc(&localTrigVal, SmoIfAngleDiffCalc(mtrCtrl->smo.elecAngle, mtrCtrl->ifCtrl.angle)); - idqRef->d = 0.0f; - mtrCtrl->spdCtrl.spdPi.integral = iftargetAmp * localTrigVal.cos; - } else { - /* Speed rmg */ - spdRefHz = RMG_Exec(&mtrCtrl->spdRmg, mtrCtrl->spdCmdHz); - } - break; - - case STARTUP_STAGE_SWITCH: - /* Switch from IF to SMO */ - spdRefHz = RMG_Exec(&mtrCtrl->spdRmg, mtrCtrl->spdCmdHz); - idqRef->q = SPDCTRL_Exec(&mtrCtrl->spdCtrl, mtrCtrl->spdRefHz, mtrCtrl->smo.spdEst); - /* Transitional stage, if current reference speed > critical speed, change to next stage */ - if (spdRefHz >= startup->spdBegin + TEMP_3) { - /* Stage change */ - mtrCtrl->stateMachine = FSM_RUN; - } - break; - - default: - break; - } - mtrCtrl->spdRefHz = spdRefHz; -} - -/** - * @brief Pre-processing of motor status. - * @param statusReg System status. - * @param stateMachine Motor Control Status. - * @retval None. - */ -static void MotorStatePerProc(SysStatusReg *statusReg, volatile FsmState *stateMachine) -{ - MCS_ASSERT_PARAM(statusReg != NULL); - MCS_ASSERT_PARAM(stateMachine != NULL); - /* Get system status */ - if (SysIsError(statusReg)) { - *stateMachine = FSM_FAULT; - } - if (SysGetCmdStop(statusReg)) { - SysCmdStopClr(statusReg); - *stateMachine = FSM_STOP; - } -} - -/** - * @brief Check over current status. - * @param statusReg System status. - * @param stateMachine Motor Control Status. - * @retval None. - */ -static void CheckOverCurrentState(SysStatusReg *statusReg, FsmState *stateMachine) -{ - MCS_ASSERT_PARAM(statusReg != NULL); - MCS_ASSERT_PARAM(stateMachine != NULL); - /* check systerm error status */ - if (SysIsError(statusReg) == false) { - *stateMachine = FSM_IDLE; - } -} - -/** - * @brief Check bootstrap capacitor charge time. - * @param mtrCtrl The motor control handle. - * @param stateMachine Motor Control Status. - * @retval None. - */ -static void CheckBootstrpCapChargeTime(MTRCTRL_Handle *mtrCtrl, FsmState *stateMachine) -{ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - MCS_ASSERT_PARAM(stateMachine != NULL); - mtrCtrl->sysTickCnt++; - /* check bootstrap capacitor charge time */ - if (mtrCtrl->sysTickCnt == mtrCtrl->capChargeTickNum) { - *stateMachine = FSM_CLEAR; - } -} - -/** - * @brief Check systerm cmd start signal. - * @param mtrCtrl The motor control handle. - * @param aptAddr Three-phase APT address pointer. - * @param statusReg System status. - * @param stateMachine Motor Control Status. - * @retval None. - */ -static void CheckSysCmdStart(MTRCTRL_Handle *mtrCtrl, - APT_RegStruct **aptAddr, - SysStatusReg *statusReg, - FsmState *stateMachine) -{ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - MCS_ASSERT_PARAM(aptAddr != NULL); - MCS_ASSERT_PARAM(statusReg != NULL); - MCS_ASSERT_PARAM(stateMachine != NULL); - /* check start cmd */ - if (SysGetCmdStart(statusReg)) { - SysRunningSet(statusReg); - SysCmdStartClr(statusReg); - mtrCtrl->sysTickCnt = 0; - *stateMachine = FSM_CAP_CHARGE; - /* Preparation for charging the bootstrap capacitor. */ - AptTurnOnLowSidePwm(aptAddr, mtrCtrl->aptMaxcntCmp); - /* Out put pwm */ - MotorPwmOutputEnable(aptAddr); - } -} - -/** - * @brief System timer tick task. - * @param mtrCtrl The motor control handle. - * @param aptAddr Three-phase APT address pointer. - * @retval None. - */ -static void TSK_SystickIsr(MTRCTRL_Handle *mtrCtrl, APT_RegStruct **aptAddr) -{ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - MCS_ASSERT_PARAM(aptAddr != NULL); - SysStatusReg *statusReg = &mtrCtrl->statusReg; - FsmState *stateMachine = &mtrCtrl->stateMachine; - mtrCtrl->msTickCnt++; - /* Pre-processing of motor status. */ - MotorStatePerProc(statusReg, stateMachine); - /* statemachine */ - switch (*stateMachine) { - case FSM_IDLE: - /* Set smo estimate speed before motor start-up */ - g_mc.smo.spdEst = 0.0f; - CheckSysCmdStart(mtrCtrl, aptAddr, statusReg, stateMachine); - break; - case FSM_CAP_CHARGE: - /* Bootstrap Capacitor Charging Timing */ - CheckBootstrpCapChargeTime(mtrCtrl, stateMachine); - break; - /* Clear parameter before start */ - case FSM_CLEAR: - ClearBeforeStartup(mtrCtrl); - *stateMachine = FSM_STARTUP; - break; - case FSM_STARTUP: - MCS_StartupSwitch(mtrCtrl); - break; - case FSM_RUN: - /* Speed ramp control */ - mtrCtrl->spdRefHz = RMG_Exec(&mtrCtrl->spdRmg, mtrCtrl->spdCmdHz); - /* Speed loop control */ - mtrCtrl->idqRef.q = SPDCTRL_Exec(&mtrCtrl->spdCtrl, mtrCtrl->spdRefHz, mtrCtrl->smo.spdEst); - break; - case FSM_STOP: - mtrCtrl->spdRefHz = 0.0f; - MotorPwmOutputDisable(aptAddr); - SysRunningClr(statusReg); - *stateMachine = FSM_IDLE; - break; - case FSM_FAULT: /* Overcurrent state */ - CheckOverCurrentState(statusReg, stateMachine); - break; - default: - break; - } -} - -/** - * @brief Read the ADC initialize bias trim value. - * @param mtrCtrl The motor control handle. - * @retval None. - */ -static void TrimInitAdcShiftValue(MTRCTRL_Handle *mtrCtrl) -{ - float adc0SampleTemp = 0.0f; /* Current bias value for temp store */ - float adc1SampleTemp = 0.0f; - float adc0TempSum = 0.0f; - float adc1TempSum = 0.0f; /* Current bias sum value for 20 times */ - float adcSampleTimes = 0.0f; /* ADC sample times */ - for (int i = 0; i < ADC_READINIT_TIMES; i++) { - adc0SampleTemp = (float)HAL_ADC_GetConvResult(&ADCU_HANDLE, ADCUSOCNUM); - adc1SampleTemp = (float)HAL_ADC_GetConvResult(&ADCW_HANDLE, ADCWSOCNUM); - BASE_FUNC_DELAY_US(200); /* 200 is delay count, delay 200us triger adc sampling */ - if (adc0SampleTemp > 1000.0f && adc1SampleTemp > 1000.0f) { - adcSampleTimes++; - adc0TempSum += adc0SampleTemp; - adc1TempSum += adc1SampleTemp; - } - } - if (adcSampleTimes < 1.0f) { - adcSampleTimes = 1.0f; /* Prevent divide-by-zero errors */ - } - adc0SampleTemp = adc0TempSum / adcSampleTimes; - adc1SampleTemp = adc1TempSum / adcSampleTimes; - /* Force convert to float */ - mtrCtrl->adc0Compensate = (float) adc0SampleTemp; - mtrCtrl->adc1Compensate = (float) adc1SampleTemp; - /* The normal value scope: 1800 < adc0Compensate < 2200 */ - if(g_mc.adc0Compensate < ADC_TRIMVALUE_MIN || g_mc.adc0Compensate > ADC_TRIMVALUE_MAX \ - || g_mc.adc1Compensate < ADC_TRIMVALUE_MIN || g_mc.adc1Compensate > ADC_TRIMVALUE_MAX) { - DBG_PRINTF("ADC trim value error,please reset!"); - HAL_GPIO_SetValue(&LED2_HANDLE, LED2_PIN, GPIO_LOW_LEVEL); - } - adcSampleTimes = 0; - adc0TempSum = 0; - adc1TempSum = 0; -} - -/** - * @brief Read the ADC current sampling value. - * @param CurrUvw Three-phase current. - * @retval None. - */ -static void ReadCurrUvw(UvwAxis *CurrUvw) -{ - MCS_ASSERT_PARAM(CurrUvw != NULL); - float adc0 = (float)HAL_ADC_GetConvResult(&ADCU_HANDLE, ADCUSOCNUM); - float adc1 = (float)HAL_ADC_GetConvResult(&ADCW_HANDLE, ADCWSOCNUM); - /* Convert adc sample value to current value */ - CurrUvw->u = -(adc0 - g_mc.adc0Compensate) * g_mc.adcCurrCofe; - CurrUvw->w = -(adc1 - g_mc.adc1Compensate) * g_mc.adcCurrCofe; - CurrUvw->v = -CurrUvw->u - CurrUvw->w; -} - -/** - * @brief Setting the APT Output Duty Cycle. - * @param aptx APT register base address. - * @param leftDuty Left duty cycle. - * @param rightDuty Right duty cycle. - * @retval None. - */ -static void SetPwmDuty(APT_Handle *aptx, float leftDuty, float rightDuty) -{ - MCS_ASSERT_PARAM(aptx != NULL); - MCS_ASSERT_PARAM(leftDuty > 0); - MCS_ASSERT_PARAM(rightDuty > 0); - unsigned short maxPeriodCnt = aptx->waveform.timerPeriod; - unsigned short cntCmpLeftEdge = (unsigned short)(leftDuty * maxPeriodCnt); - unsigned short cntCmpRightEdge = (unsigned short)(rightDuty * maxPeriodCnt); - /* avoid overflowing */ - cntCmpLeftEdge = (cntCmpLeftEdge > maxPeriodCnt) ? maxPeriodCnt : cntCmpLeftEdge; - cntCmpRightEdge = (cntCmpRightEdge > maxPeriodCnt) ? maxPeriodCnt : cntCmpRightEdge; - HAL_APT_SetPWMDuty(aptx, cntCmpLeftEdge, cntCmpRightEdge); -} - -/** - * @brief Duty Cycle Setting. - * @param dutyUvwLeft Three-phase left duty cycle. - * @param dutyUvwRight Three-phase right duty cycle. - * @retval None. - */ -static void SetPwmDutyCp(UvwAxis *dutyUvwLeft, UvwAxis *dutyUvwRight) -{ - MCS_ASSERT_PARAM(dutyUvwLeft != NULL); - MCS_ASSERT_PARAM(dutyUvwRight != NULL); - /* Setting the Three-Phase Duty Cycle */ - SetPwmDuty(&g_apt0, dutyUvwLeft->u, dutyUvwRight->u); - SetPwmDuty(&g_apt1, dutyUvwLeft->v, dutyUvwRight->v); - SetPwmDuty(&g_apt2, dutyUvwLeft->w, dutyUvwRight->w); -} - -/** - * @brief To set the ADC sampling trigger comparison value. - * @param cntCmpSOCA Soca Compare Count Value. - * @param cntCmpSOCB Socb Compare Count Value. - * @retval None. - */ -static void SetADCTriggerTime(unsigned short cntCmpSOCA, unsigned short cntCmpSOCB) -{ - MCS_SetAdcCompareR1(g_apt[PHASE_U], cntCmpSOCA, cntCmpSOCB, g_mc.aptMaxcntCmp); -} - -/** - * @brief Temprature table, the temprature detect range is 15 ~ 60 degree. - * @param tempResisValue Temperature sensor resistance. - * @retval None. - */ -static float TempTable(float tempResisValue) -{ - float boardTemp = 0.0f; - /* Temperatures between 15 and 30. */ - if (tempResisValue > TEMP_RES_30 && tempResisValue <= TEMP_RES_15) { - boardTemp = TEMP_15 + (TEMP_30 - TEMP_15) * (TEMP_RES_15 - tempResisValue) / (TEMP_RES_15 - TEMP_RES_30); - } else if (tempResisValue > TEMP_RES_45 && tempResisValue <= TEMP_RES_30) { /* Temperature between 30 and 45. */ - boardTemp = TEMP_30 + (TEMP_45 - TEMP_30) * (TEMP_RES_30 - tempResisValue) / (TEMP_RES_30 - TEMP_RES_45); - } else if (tempResisValue > TEMP_RES_60 && tempResisValue <= TEMP_RES_45) { /* Temperature between 45 and 50. */ - boardTemp = TEMP_45 + (TEMP_60 - TEMP_45) * (TEMP_RES_45 - tempResisValue) / (TEMP_RES_45 - TEMP_RES_60); - } else if (tempResisValue <= TEMP_RES_60) { - boardTemp = TEMP_60; /* If temperature is over 60, set temperature as 60. */ - } else if (tempResisValue >= TEMP_RES_15) { - boardTemp = TEMP_15; /* If temperature is lower 15, set temperature as 15. */ - } - return boardTemp; -} - -/** - * @brief Read power board temperature and udc. - * @retval None. - */ -static void ReadBoardTempAndUdc(void) -{ - HAL_ADC_SoftTrigSample(&ADCRESIS_HANDLE, ADCRESISSOCNUM); - HAL_ADC_SoftTrigSample(&ADCUDC_HANDLE, ADCUDCSOCNUM); - BASE_FUNC_DELAY_US(CNT_10); /* Delay 10 us. */ - /* Force convert to float type. */ - float resisAdcValue = (float)HAL_ADC_GetConvResult(&ADCRESIS_HANDLE, ADCRESISSOCNUM); - /* 10 / (x + 10) * 3.3 = resisAdcValue / 4096 * 3.3, x is resisValue, 10kohm is resistor divider value. */ - float resisValue = (4096.0f * 10.0f - 10.0f * resisAdcValue) / resisAdcValue; - g_mc.powerBoardTemp = TempTable(resisValue); - g_mc.udc = ((float)HAL_ADC_GetConvResult(&ADCUDC_HANDLE, ADCUDCSOCNUM)) * ADC_UDC_COFFI; -} - -/** - * @brief Execut abnormal feedback speed protect motion. - * @retval None. - */ -static void SpdFbkErrorProt_Exec(void) -{ - if (g_mc.prot.motorErrStatus.Bit.motorStalling == 0 && - g_mc.prot.motorErrStatus.Bit.overVoltErr == 0 && - g_mc.prot.motorErrStatus.Bit.lowerVoltErr == 0 && - g_mc.prot.motorErrStatus.Bit.overIpmTempErr == 0 && - g_mc.prot.motorErrStatus.Bit.overCurrErr == 0) { - g_mc.prot.motorErrStatus.Bit.revRotErr = 1; - /* If revRotErr, execute protect motion. */ - ProtSpo_Exec(g_apt); - } -} - -/** - * @brief Execut nan data protect motion. - * @retval None. - */ -static void NanDataDetect(void) -{ - static short errorSpdStatus = 0; - /* Detect the nan observer speed or current value. */ - if (isnan(g_mc.smo.spdEst) || isnan(g_mc.idqRef.q)) { - errorSpdStatus++; - } else { - errorSpdStatus = 0; - } - /* If the data is nan & continuous counting value is over 500 times, execute protect motion. - the detect time is 500 * 500us = 250ms. */ - if (errorSpdStatus >= 500) { - errorSpdStatus = 0; - SpdFbkErrorProt_Exec(); - } -} - -/** - * @brief Check abnormal feedback speed. - * @retval None. - */ -static void CheckSpdFbkStatus(void) -{ - static short errorCurrStatus = 0; - static short errorDeltaSpdStatus = 0; - NanDataDetect(); - if (g_mc.stateMachine == FSM_RUN) { - /* Detect the abnormal idq feedback current. */ - if (Abs(g_mc.idqRef.q - g_mc.idqFbk.q) >= CTRL_IF_CURR_AMP_A) { - errorCurrStatus++; - } else { - errorCurrStatus = 0; - } - /* Detect the abnormal feedback speed, the normal speed is > 0, if smo.spdEst < -10 && - delta speed error > USER_MIN_SPD_HZ + 10.0f at FSM_RUN stage, set the motor motion as error */ - if (g_mc.smo.spdEst < -10.0f && (g_mc.spdRefHz - g_mc.smo.spdEst > USER_MIN_SPD_HZ + 10.0f)) { - errorDeltaSpdStatus++; - } - } - /* Execute protect motion if count over 500 times, this error status caused by abnormal speed - or cabnormal urrent feedback, the detect time is 500 * 500us = 250ms. */ - if (errorCurrStatus >= 500) { - errorCurrStatus = 0; - SpdFbkErrorProt_Exec(); - } - /* This error statu caused by motor stalling, the detect time is 2 * 500us = 1ms. */ - if (errorDeltaSpdStatus >= 2) { - errorDeltaSpdStatus = 0; - g_mc.prot.motorErrStatus.Bit.motorStalling = 1; - } -} - -/** - * @brief Check Potentiometer Value callback function. - * @param param The TIMER_Handle. - * @retval None. - */ -void CheckPotentiometerValueCallback(void *param) -{ - MCS_ASSERT_PARAM(param != NULL); - BASE_FUNC_UNUSED(param); - static float potentiomitorAdcValue = 0.0f; - static float spdCmdHz = 0; - static float spdCmdHzLast = USER_MIN_SPD_HZ; /* 35.0 is spdCmdHzLast init value */ - HAL_ADC_SoftTrigSample(&ADCPTT_HANDLE, ADCPTTSOCNUM); - BASE_FUNC_DELAY_US(10); /* Delay 10 us. */ - potentiomitorAdcValue = (float)HAL_ADC_GetConvResult(&ADCPTT_HANDLE, ADCPTTSOCNUM); - /* 4045.0 is adc sample max value of potentiomitor, convert max spd to 180.25Hz */ - spdCmdHz = potentiomitorAdcValue / 4045.0f * USER_MAX_SPD_HZ; - if (Abs(spdCmdHzLast - spdCmdHz) < 1.0f) { /* Ignore changes less than 1. */ - return; - } - spdCmdHzLast = spdCmdHz; - if (spdCmdHz < USER_MIN_SPD_HZ) { /* 35.0 is spdCmdHz lower limit */ - spdCmdHz = USER_MIN_SPD_HZ; /* 35.0 is spdCmdHz lower limit */ - } - if (spdCmdHz > g_mc.mtrParam.maxElecSpd) { /* spdCmdHz upper limit */ - spdCmdHz = g_mc.mtrParam.maxElecSpd; /* spdCmdHz upper limit */ - } - if (g_mc.spdAdjustMode == CUST_SPEED_ADJUST) { - g_mc.spdCmdHz = spdCmdHz; - } -} - -/** - * @brief System timer ISR for Motor Statemachine CallBack function. - * @param param The systick timer handle. - * @retval None. - */ -void MotorStatemachineCallBack(void *param) -{ - /* Verify Parameters */ - MCS_ASSERT_PARAM(param != NULL); - BASE_FUNC_UNUSED(param); - /* Read power board temprature and voltage. */ - ReadBoardTempAndUdc(); - /* Motor speed loop state machine. */ - TSK_SystickIsr(&g_mc, g_apt); - - /* Motor error speed feedback check. */ - CheckSpdFbkStatus(); - /* Motor stalling detect. */ - STP_Det_ByCurrSpd(&g_mc.prot.stall, &g_mc.prot.motorErrStatus, g_mc.smo.spdEst, g_mc.idqFbk); - STP_Exec(&g_mc.prot.motorErrStatus, g_apt); - - /* Motor over voltage detect. */ - OVP_Det(&g_mc.prot.ovp, &g_mc.prot.motorErrStatus, g_mc.udc); - OVP_Exec(&g_mc.prot.ovp, &g_mc.spdRefHz, g_apt); - OVP_Recy(&g_mc.prot.ovp, &g_mc.prot.motorErrStatus, g_mc.udc); - /* Motor lower voltage detect. */ - LVP_Det(&g_mc.prot.lvp, &g_mc.prot.motorErrStatus, g_mc.udc); - LVP_Exec(&g_mc.prot.lvp, &g_mc.spdRefHz, g_apt); - LVP_Recy(&g_mc.prot.lvp, &g_mc.prot.motorErrStatus, g_mc.udc); - /* Power board over temperature detect. */ - OTP_Det(&g_mc.prot.otp, &g_mc.prot.motorErrStatus, OTP_IPM_ERR_BIT, g_mc.powerBoardTemp); - OTP_Exec(&g_mc.prot.otp, &g_mc.spdRefHz, g_apt); - OTP_Recy(&g_mc.prot.otp, &g_mc.prot.motorErrStatus, OTP_IPM_ERR_BIT, g_mc.powerBoardTemp); - - /* If protect level == 4, set motor state as stop. */ - if (g_mc.prot.ovp.protLevel == LEVEL_4 || g_mc.prot.lvp.protLevel == LEVEL_4 \ - || g_mc.prot.otp.protLevel == LEVEL_4) { - SysCmdStopSet(&g_mc.statusReg); - } -} - -/** - * @brief The carrier ISR wrapper function. - * @param aptHandle The APT handle. - * @retval None. - */ -void MotorCarrierProcessCallback(void *aptHandle) -{ - MCS_ASSERT_PARAM(aptHandle != NULL); - BASE_FUNC_UNUSED(aptHandle); - /* the carrierprocess of motor */ - MCS_CarrierProcess(&g_mc); - /* Over current protect */ - if (g_mc.stateMachine == FSM_RUN || g_mc.stateMachine == FSM_STARTUP) { - OCP_Det(&g_mc.prot.ocp, &g_mc.prot.motorErrStatus, g_mc.idqFbk); - OCP_Exec(&g_mc.prot.ocp, &g_mc.idqFbk, g_apt); /* Execute over current protect motion */ - if (g_mc.prot.ocp.protLevel < LEVEL_4) { - OCP_Recy(&g_mc.prot.ocp, &g_mc.prot.motorErrStatus); - } - } -} - -/** - * @brief Event interrupt callback function of APT module. - * @param para APT module handle. - * @retval None. - */ -void MotorSysErrCallback(void *para) -{ - MCS_ASSERT_PARAM(para != NULL); - APT_Handle *handle = (APT_Handle *)para; - /* The IPM overcurrent triggers and disables the three-phase PWM output. */ - MotorPwmOutputDisable(g_apt); - DCL_APT_ClearOutCtrlEventFlag((APT_RegStruct *)g_apt[PHASE_U], APT_OC_COMBINE_EVENT_A1); - DCL_APT_ClearOutCtrlEventFlag((APT_RegStruct *)g_apt[PHASE_V], APT_OC_COMBINE_EVENT_A1); - DCL_APT_ClearOutCtrlEventFlag((APT_RegStruct *)g_apt[PHASE_W], APT_OC_COMBINE_EVENT_A1); - /* Status setting error */ - SysErrorSet(&g_mc.statusReg); - DBG_PRINTF("APT error! \r\n"); - HAL_GPIO_SetValue(&LED2_HANDLE, LED2_PIN, GPIO_LOW_LEVEL); - BASE_FUNC_UNUSED(handle); -} - -/** - * @brief Init motor controller's data structure. - * @retval None. - */ -static void InitSoftware(void) -{ - /* Initializing motor control param */ - TSK_Init(); - /* Read phase-uvw current */ - g_mc.readCurrUvwCb = ReadCurrUvw; - g_mc.setPwmDutyCb = SetPwmDutyCp; - g_mc.setADCTriggerTimeCb = SetADCTriggerTime; -} - -/** - * @brief Config the KEY func. - * @param handle The GPIO handle. - * @retval None. - */ -static KEY_State Key_StateRead(GPIO_Handle *handle) -{ - if (HAL_GPIO_GetPinValue(handle, handle->pins) == 0) { - BASE_FUNC_DELAY_MS(30); /* delay 30ms for deshake */ - if (HAL_GPIO_GetPinValue(handle, handle->pins) == 0) { - while (HAL_GPIO_GetPinValue(handle, handle->pins) == 0) { - } - return KEY_DOWN; - } - } - return KEY_UP; -} - -/** - * @brief Control motor start and stop state by key func. - * @param param The GPIO handle. - * @retval None. - */ -void MotorStartStopKeyCallback(void *param) -{ - GPIO_Handle *handle = (GPIO_Handle *)param; - if (Key_StateRead(handle) == KEY_DOWN) { - if (g_mc.motorStateFlag == 0) { /* start motor */ - g_mc.motorStateFlag = 1; - SysCmdStartSet(&g_mc.statusReg); - } else if (g_mc.motorStateFlag == 1) { /* stop motor */ - g_mc.motorStateFlag = 0; - SysCmdStopSet(&g_mc.statusReg); - } - } -} - -/** - * @brief User application main entry function. - * @retval BSP_OK. - */ -int MotorMainProcess(void) -{ - unsigned int tickNum1Ms = 2; /* 1ms tick */ - static unsigned int tickCnt1Ms = 0; - unsigned int tickNum500Ms = 1000; /* 500ms tick */ - static unsigned int tickCnt500Ms = 0; - SystemInit(); - HMI_Init(); /* Init uart interrupt */ - HAL_TIMER_Start(&g_timer0); - HAL_TIMER_Start(&g_timer1); - - /* Disable PWM output before startup. */ - MotorPwmOutputDisable(g_apt); - /* Software initialization. */ - InitSoftware(); - /* Start the PWM clock. */ - HAL_APT_StartModule(RUN_APT0 | RUN_APT1 | RUN_APT2); - /* System Timer clock. */ - BASE_FUNC_DELAY_MS(ADC_READINIT_DELAY); - TrimInitAdcShiftValue(&g_mc); - BASE_FUNC_DELAY_MS(MOTOR_START_DELAY); - while (1) { - /* Cycling send data to host */ - HMI_Process_Tx(&g_mc); - if (g_mc.msTickCnt - tickCnt1Ms >= tickNum1Ms) { - tickCnt1Ms = g_mc.msTickCnt; - /* User Code 1ms Event */ - HMI_Process_Rx(&g_mc); - /* User Code 1ms Event */ - } - - if (g_mc.msTickCnt - tickCnt500Ms >= tickNum500Ms) { - if (SysIsError(&g_mc.statusReg) != true) { - /* LED toggle in normal status. */ - HAL_GPIO_TogglePin(&LED1_HANDLE, LED1_PIN); - } - tickCnt500Ms = g_mc.msTickCnt; - } - } - return 0; -} diff --git a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/cust_process.c b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/cust_process.c deleted file mode 100644 index 7c4f7525..00000000 --- a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/cust_process.c +++ /dev/null @@ -1,718 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file cust_process.c - * @author MCU Algorithm Team - * @brief This file provides functions declaration of cust process interface. - */ - -#include "cust_process.h" -#include "mcs_ctlmode_config.h" -#include "mcs_math_const.h" -#include "mcs_user_config.h" -#include "mcs_assert.h" -#include "main.h" -#include "uart_module.h" -#include "mcs_mtr_param.h" -/* Macro definitions --------------------------------------------------------------------------- */ -/* Constant value. */ -#define CONST_VALUE_60 60.0f /* Constant value 60. */ -#define CONST_VALUE_DIV_1000 0.001f /* Constant value 1/1000. */ - -/* Data array index. */ -#define DATA_SEGMENT_ONE 0 /* First element of the data segment */ -#define DATA_SEGMENT_TWO 1 /* Second element of the data segment */ -#define DATA_SEGMENT_THREE 2 /* Third element of the data segment */ -#define DATA_SEGMENT_FOUR 3 /* Fourth element of the data segment */ -#define CUSTACKCODELEN 10 /* Ack code length */ - -/* Command code. */ -#define SET_PID_KP 0x01 /* Set Pid Kp Command Params */ -#define SET_PID_KI 0x02 /* Set Pid Ki Command Params */ -#define SET_PID_LIMIT 0x03 /* Set Pid limit Command Params */ - -#define SET_SMO1TH_PLL_BDW 0x01 /* Set Smo1th Pll BandWidth Command Params */ -#define SET_SMO1TH_SPDFLITER_FC 0x02 /* Set Smo1th Fc Command Params */ -#define SET_SMO1TH_FILCOMPANGLE 0x03 /* Set Smo1th FillComp Command Params */ - -#define SET_SMO4TH_KD 0x01 /* Set Smo4th Kd Command Params */ -#define SET_SMO4TH_KP 0x02 /* Set Smo4th Kq Command Params */ - -#define SET_SPD_COMMAND_HZ 0x01 /* Set Target Speed Command Params */ -#define SET_SPD_RMG_SLOPE 0x02 /* Set Speed Slope Command Params */ - -#define SET_MAX_ELEC_SPD 0x01 /* Set Max Motor Speed Command Params */ -#define SET_MOTOR_NUM_OF_PAIRS 0x02 /* Set Motor Command Params */ - -#define SET_MOTOR_RES_OF_STATOR 0x01 /* Set Motor Res Command Params */ -#define SET_MOTOR_DAXIS_INDUCTANCE 0x02 /* Set Motor Daxis Inductance Command Params */ -#define SET_MOTOR_QAXIS_INDUCTANCE 0x03 /* Set Motor Qaxis Inductance Command Params */ - -#define SET_SVPWM_VOLTAGE_PER_UNIT 0x01 /* Set Svpwm Voltage Params */ -#define SET_ADC_CURRENT_SAMPLE_COFE 0x02 /* Set Adc coffe Params */ -#define SET_CURRENT_LOOP_CONTROL_PERIOD 0x03 /* Set Current loop period Command Params */ - -#define SET_IF_TARGET_CURRENT_VALUE 0x01 /* Set If Target Params */ -#define SET_INCREMENT_OF_IF_CURRENT 0x02 /* Set If Step Command Params */ -#define SET_SPEED_RING_BEGIN_SPEED 0x03 /* Set If to Smo Start Speed Command Params */ - -static unsigned char ackCode = 0; -static unsigned char g_uartTxBuf[CUSTACKCODELEN] = {0}; - -/** - * @brief Set observer type. - * @param mtrCtrl The motor control handle. - * @param rxData Receive buffer - */ -static void CMDCODE_SetObserverType(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) -{ - /* Get function code. */ - int funcCode = (int)(rxData->data[DATA_SEGMENT_ONE].typeF); - if (funcCode == FOC_OBSERVERTYPE_SMO1TH) { - ackCode = 0X01; - mtrCtrl->obserType = FOC_OBSERVERTYPE_SMO1TH; - CUST_AckCode(g_uartTxBuf, ackCode, mtrCtrl->obserType); - } else if (funcCode == FOC_OBSERVERTYPE_SMO4TH) { - ackCode = 0X02; - mtrCtrl->obserType = FOC_OBSERVERTYPE_SMO4TH; - CUST_AckCode(g_uartTxBuf, ackCode, mtrCtrl->obserType); - } -} - -/** - * @brief Set pid parameter ack code. - * @param funcCode Received data funccode. - */ -static unsigned char SetPidAckCode(int funcCode) -{ - switch (funcCode) { - /* Set current loop D-Axis PID parameter ack code. */ - case FOC_CURDAXISPID_PARAMS: - ackCode = 0xE0; - break; - /* Set current loop Q-Axis PID parameter ack code. */ - case FOC_CURQAXISPID_PARAMS: - ackCode = 0xE3; - break; - /* Set speed loop PID parameter ack code. */ - case FOC_SPDPID_PARAMS: - ackCode = 0xE6; - break; - default: - break; - } - return ackCode; -} - -/** - * @brief Set pid parameters. - * @param pidHandle The pid control handle. - * @param rxData Receive buffer - */ -static void SetPidParams(PID_Handle *pidHandle, CUSTDATATYPE_DEF *rxData) -{ - /* Get function code. */ - int funcCode = (int)(rxData->data[DATA_SEGMENT_ONE].typeF); - /* Get command code. */ - int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); - switch (cmdCode) { - case SET_PID_KP: /* Set the P parameter. */ - PID_SetKp(pidHandle, rxData->data[DATA_SEGMENT_THREE].typeF); - ackCode = SetPidAckCode(funcCode); - CUST_AckCode(g_uartTxBuf, (unsigned char)(ackCode + SET_PID_KP), rxData->data[DATA_SEGMENT_THREE].typeF); - break; - case SET_PID_KI: /* Set the I parameter. */ - PID_SetKi(pidHandle, rxData->data[DATA_SEGMENT_THREE].typeF); - ackCode = SetPidAckCode(funcCode); - CUST_AckCode(g_uartTxBuf, (unsigned char)(ackCode + SET_PID_KI), rxData->data[DATA_SEGMENT_THREE].typeF); - break; - case SET_PID_LIMIT: /* Set the pid limit. */ - PID_SetLimit(pidHandle, rxData->data[DATA_SEGMENT_THREE].typeF); - ackCode = SetPidAckCode(funcCode); - CUST_AckCode(g_uartTxBuf, (unsigned char)(ackCode + SET_PID_LIMIT), rxData->data[DATA_SEGMENT_THREE].typeF); - break; - default: - ackCode = 0X77; - CUST_AckCode(g_uartTxBuf, ackCode, 0); - break; - } -} - -/** - * @brief Set motor pid parameters. - * @param mtrCtrl The motor control handle. - * @param rxData Receive buffer - */ -static void CMDCODE_SetMotorPidParams(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) -{ - /* Get function code. */ - int funcCode = (int)(rxData->data[DATA_SEGMENT_ONE].typeF); - - if (funcCode == FOC_CURDAXISPID_PARAMS) { - SetPidParams(&mtrCtrl->currCtrl.dAxisPi, rxData); /* Set Curr loop Daxis pid params */ - } else if (funcCode == FOC_CURQAXISPID_PARAMS) { - SetPidParams(&mtrCtrl->currCtrl.qAxisPi, rxData); /* Set Curr loop Qaxis pid params */ - mtrCtrl->currCtrl.dAxisPi.upperLimit = mtrCtrl->currCtrl.qAxisPi.upperLimit; - mtrCtrl->currCtrl.dAxisPi.lowerLimit = mtrCtrl->currCtrl.qAxisPi.lowerLimit; - } else if (funcCode == FOC_SPDPID_PARAMS) { - SetPidParams(&mtrCtrl->spdCtrl.spdPi, rxData); /* Set Speed loop params */ - } -} - -/** - * @brief Set first order sliding mode observer parameters. - * @param smoHandle The observer control handle. - * @param rxData Receive buffer - */ -static void SetObserverSmo1thParams(FOSMO_Handle *smoHandle, CUSTDATATYPE_DEF *rxData) -{ - smoHandle->kSmo = rxData->data[DATA_SEGMENT_TWO].typeF; - ackCode = 0X09; - CUST_AckCode(g_uartTxBuf, ackCode, smoHandle->kSmo); -} - -/** - * @brief Set first order sliding mode observer's phase-locked loop parameters. - * @param smoHandle The observer control handle. - * @param rxData Receive buffer - */ -static void SetObserverSmo1thPLLParams(FOSMO_Handle *smoHandle, CUSTDATATYPE_DEF *rxData) -{ - /* Get command code. */ - int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); - float pllBdw = 0.0f; - switch (cmdCode) { - case SET_SMO1TH_PLL_BDW: /* Set the bandwidth. */ - pllBdw = rxData->data[DATA_SEGMENT_THREE].typeF; - smoHandle->pll.pi.kp = 2.0f * pllBdw; /* kp = 2.0f * pllBdw */ - smoHandle->pll.pi.ki = pllBdw * pllBdw; /* ki = pllBdw * pllBdw */ - ackCode = 0X0A; - CUST_AckCode(g_uartTxBuf, ackCode, pllBdw); - break; - case SET_SMO1TH_SPDFLITER_FC: /* Set the cutoff frequency. */ - smoHandle->spdFilter.fc = rxData->data[DATA_SEGMENT_THREE].typeF; - smoHandle->spdFilter.a1 = 1.0f / (1.0f + DOUBLE_PI * smoHandle->spdFilter.fc * CTRL_CURR_PERIOD); - smoHandle->spdFilter.b1 = 1.0f - smoHandle->spdFilter.a1; - ackCode = 0X0B; - CUST_AckCode(g_uartTxBuf, ackCode, smoHandle->spdFilter.fc); - break; - case SET_SMO1TH_FILCOMPANGLE: /* Set the compensation angle. */ - smoHandle->filCompAngle = rxData->data[DATA_SEGMENT_THREE].typeF; - ackCode = 0X0C; - CUST_AckCode(g_uartTxBuf, ackCode, smoHandle->filCompAngle); - break; - default: - ackCode = 0X77; - CUST_AckCode(g_uartTxBuf, ackCode, 0); - break; - } -} - -/** - * @brief Set fourth order sliding mode observer parameters. - * @param smo4thHandle The observer control handle. - * @param rxData Receive buffer - */ -static void SetObserverSmo4thParams(SMO4TH_Handle *smo4thHandle, CUSTDATATYPE_DEF *rxData) -{ - /* Get command code. */ - int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); - - switch (cmdCode) { - case SET_SMO4TH_KD: /* Set d axis gain. */ - smo4thHandle->kd = rxData->data[DATA_SEGMENT_THREE].typeF; - ackCode = 0X0D; - CUST_AckCode(g_uartTxBuf, ackCode, smo4thHandle->kd); - break; - case SET_SMO4TH_KP: /* Set q axis gain. */ - smo4thHandle->kq = rxData->data[DATA_SEGMENT_THREE].typeF; - ackCode = 0X0E; - CUST_AckCode(g_uartTxBuf, ackCode, smo4thHandle->kq); - break; - default: - break; - } -} - -/** - * @brief Set fourth order sliding mode observer's phase-locked loop parameters. - * @param smo4thHandle The observer control handle. - * @param rxData Receive buffer - */ -static void SetObserverSmo4thPLLParams(SMO4TH_Handle *smo4thHandle, CUSTDATATYPE_DEF *rxData) -{ - float pllBdw = rxData->data[DATA_SEGMENT_TWO].typeF; - smo4thHandle->pll.pi.kp = (2.0f) * pllBdw; /* kp = 2.0f * pllBdw */ - smo4thHandle->pll.pi.ki = pllBdw * pllBdw; - ackCode = 0X11; - CUST_AckCode(g_uartTxBuf, ackCode, pllBdw); -} - -/** - * @brief Set observer parameters. - * @param mtrCtrl The motor control handle. - * @param rxData Receive buffer - */ -static void CMDCODE_SetObserverParams(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) -{ - /* Get function code. */ - int funcCode = (int)(rxData->data[DATA_SEGMENT_ONE].typeF); - - if (funcCode == FOC_OBSERVERTYPE_SMO1TH) { - SetObserverSmo1thParams(&mtrCtrl->smo, rxData); - } else if (funcCode == FOC_OBSERVERTYPE_SMO1TH_PLL) { - SetObserverSmo1thPLLParams(&mtrCtrl->smo, rxData); - } else if (funcCode == FOC_OBSERVERTYPE_SMO4TH) { - SetObserverSmo4thParams(&mtrCtrl->smo4th, rxData); - } else if (funcCode == FOC_OBSERVERTYPE_SMO4TH_PLL) { - SetObserverSmo4thPLLParams(&mtrCtrl->smo4th, rxData); - } -} - -/** - * @brief Set motor speed and speed slope. - * @param mtrCtrl The motor control handle. - * @param rxData Receive buffer - */ -static void CMDCODE_SetMotorSpdAndSlope(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) -{ - /* Get command code. */ - int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); - - switch (cmdCode) { - case SET_SPD_COMMAND_HZ: /* Set target speed(hz). */ - mtrCtrl->spdCmdHz = rxData->data[DATA_SEGMENT_THREE].typeF * mtrCtrl->mtrParam.mtrNp / CONST_VALUE_60; - /* Judgement the value > 0.00001, make sure denominator != 0. */ - if (rxData->data[DATA_SEGMENT_FOUR].typeF > 0.00001f) { - mtrCtrl->spdRmg.delta = mtrCtrl->spdCmdHz / rxData->data[DATA_SEGMENT_FOUR].typeF * CTRL_SYSTICK_PERIOD; - } - ackCode = 0X16; - CUST_AckCode(g_uartTxBuf, ackCode, mtrCtrl->spdCmdHz); - break; - case SET_SPD_RMG_SLOPE: /* Set speed slope. */ - mtrCtrl->spdRmg.delta = mtrCtrl->spdCmdHz / rxData->data[DATA_SEGMENT_THREE].typeF * CTRL_SYSTICK_PERIOD; - ackCode = 0X17; - CUST_AckCode(g_uartTxBuf, ackCode, rxData->data[DATA_SEGMENT_THREE].typeF); - break; - default: - break; - } -} - -/** - * @brief Set motor base parameters. - * @param mtrParamHandle The motor parameters handle. - * @param rxData Receive buffer - */ -static void SetMotorBaseParams(MOTOR_Param *mtrParamHandle, CUSTDATATYPE_DEF *rxData) -{ - /* Get command code. */ - int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); - - switch (cmdCode) { - case SET_MAX_ELEC_SPD: /* Set max electric speed. */ - mtrParamHandle->maxElecSpd = rxData->data[DATA_SEGMENT_THREE].typeF / - CONST_VALUE_60 * mtrParamHandle->mtrNp; - ackCode = 0X18; - CUST_AckCode(g_uartTxBuf, ackCode, rxData->data[DATA_SEGMENT_THREE].typeF); - break; - case SET_MOTOR_NUM_OF_PAIRS: /* Set the number of motor pole pairs. */ - mtrParamHandle->mtrNp = (unsigned short)(rxData->data[DATA_SEGMENT_THREE].typeF); - ackCode = 0X19; - CUST_AckCode(g_uartTxBuf, ackCode, (float)mtrParamHandle->mtrNp); - break; - default: - break; - } -} - -/** - * @brief Set motor special parameters. - * @param mtrParamHandle The motor parameters handle. - * @param rxData Receive buffer - */ -static void SetMotorSpecialParams(MOTOR_Param *mtrParamHandle, CUSTDATATYPE_DEF *rxData) -{ - /* Get command code. */ - int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); - - switch (cmdCode) { - case SET_MOTOR_RES_OF_STATOR: /* Set the resistor of stator. */ - mtrParamHandle->mtrRs = rxData->data[DATA_SEGMENT_THREE].typeF; - ackCode = 0X1A; - CUST_AckCode(g_uartTxBuf, ackCode, mtrParamHandle->mtrRs); - break; - case SET_MOTOR_DAXIS_INDUCTANCE: /* Set the d axis inductance. */ - mtrParamHandle->mtrLd = rxData->data[DATA_SEGMENT_THREE].typeF; - ackCode = 0X1B; - CUST_AckCode(g_uartTxBuf, ackCode, mtrParamHandle->mtrLd); - break; - case SET_MOTOR_QAXIS_INDUCTANCE: /* Set the q axis inductance. */ - mtrParamHandle->mtrLq = rxData->data[DATA_SEGMENT_THREE].typeF; - ackCode = 0X1C; - CUST_AckCode(g_uartTxBuf, ackCode, mtrParamHandle->mtrLq); - break; - default: - ackCode = 0X77; - CUST_AckCode(g_uartTxBuf, ackCode, 0); - break; - } -} - -/** - * @brief Set motor board parameters. - * @param mtrCtrl The motor control handle. - * @param rxData Receive buffer - */ -static void SetMotorBoardParams(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) -{ - /* Get command code. */ - int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); - - switch (cmdCode) { - case SET_SVPWM_VOLTAGE_PER_UNIT: /* Set svpwm voltage per unit. */ - mtrCtrl->sv.voltPu = rxData->data[DATA_SEGMENT_THREE].typeF * ONE_DIV_SQRT3; - mtrCtrl->currCtrl.outLimit = mtrCtrl->sv.voltPu * ONE_DIV_SQRT3; - ackCode = 0X1D; - CUST_AckCode(g_uartTxBuf, ackCode, rxData->data[DATA_SEGMENT_THREE].typeF); - break; - case SET_ADC_CURRENT_SAMPLE_COFE: /* Set adc current sample cofeature. */ - mtrCtrl->adcCurrCofe = rxData->data[DATA_SEGMENT_THREE].typeF; - ackCode = 0X1E; - CUST_AckCode(g_uartTxBuf, ackCode, mtrCtrl->adcCurrCofe); - break; - case SET_CURRENT_LOOP_CONTROL_PERIOD: /* Set current loop control period. */ - mtrCtrl->currCtrlPeriod = 1 / rxData->data[DATA_SEGMENT_THREE].typeF * CONST_VALUE_DIV_1000; - ackCode = 0X1F; - CUST_AckCode(g_uartTxBuf, ackCode, rxData->data[DATA_SEGMENT_THREE].typeF); - break; - default: - break; - } -} - -/** - * @brief Set motor parameters. - * @param mtrCtrl The motor control handle. - * @param rxData Receive buffer - */ -static void CMDCODE_SetMotorParams(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) -{ - /* Get function code. */ - int funcCode = (int)(rxData->data[DATA_SEGMENT_ONE].typeF); - - if (funcCode == MOTOR_PARAMS_BASE) { - SetMotorBaseParams(&mtrCtrl->mtrParam, rxData); - } else if (funcCode == MOTOR_PARAMS_SPECIAL) { - SetMotorSpecialParams(&mtrCtrl->mtrParam, rxData); - } else if (funcCode == MOTOR_PARAMS_BOARD) { - SetMotorBoardParams(mtrCtrl, rxData); - } -} - -/** - * @brief Motor start. - * @param mtrCtrl The motor control handle. - */ -static void CMDCODE_MotorStart(MTRCTRL_Handle *mtrCtrl) -{ - if (mtrCtrl->stateMachine != FSM_RUN) { - SysCmdStartSet(&mtrCtrl->statusReg); /* start motor. */ - mtrCtrl->motorStateFlag = 1; - ackCode = 0X24; /* send ackcode to host. */ - CUST_AckCode(g_uartTxBuf, ackCode, 1); - } -} - -/** - * @brief Motor stop. - * @param mtrCtrl The motor control handle. - */ -static void CMDCODE_MotorStop(MTRCTRL_Handle *mtrCtrl) -{ - SysCmdStopSet(&mtrCtrl->statusReg); - mtrCtrl->motorStateFlag = 0; - ackCode = 0X25; - CUST_AckCode(g_uartTxBuf, ackCode, 0); -} - -/** - * @brief Motor state reset. - * @param mtrCtrl The motor control handle. - */ -static void CMDCODE_MotorReset(MTRCTRL_Handle *mtrCtrl) -{ - BASE_FUNC_UNUSED(mtrCtrl); - BASE_FUNC_SoftReset(); -} - -/** - * @brief Set IF-Startup parameters. - * @param mtrCtrl The motor control handle. - * @param rxData Receive buffer - */ -static void SetStartupIFParams(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) -{ - /* Get command code. */ - int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); - - switch (cmdCode) { - case SET_IF_TARGET_CURRENT_VALUE: /* Set I/F start up target current value. */ - mtrCtrl->ifCtrl.targetAmp = rxData->data[DATA_SEGMENT_THREE].typeF; - ackCode = 0X26; - CUST_AckCode(g_uartTxBuf, ackCode, mtrCtrl->ifCtrl.targetAmp); - break; - case SET_INCREMENT_OF_IF_CURRENT: /* Set increment of I/F start up current. */ - mtrCtrl->ifCtrl.stepAmp = mtrCtrl->ifCtrl.targetAmp / rxData->data[DATA_SEGMENT_THREE].typeF * - CTRL_SYSTICK_PERIOD; - ackCode = 0X27; - CUST_AckCode(g_uartTxBuf, ackCode, mtrCtrl->ifCtrl.stepAmp); - break; - case SET_SPEED_RING_BEGIN_SPEED: /* Set speed ring begin speed. */ - mtrCtrl->startup.spdBegin = rxData->data[DATA_SEGMENT_THREE].typeF / - CONST_VALUE_60 * mtrCtrl->mtrParam.mtrNp; - ackCode = 0X28; - CUST_AckCode(g_uartTxBuf, ackCode, rxData->data[DATA_SEGMENT_THREE].typeF); - break; - default: - break; - } -} - -/** - * @brief Set start up parameters. - * @param mtrCtrl The motor control handle. - * @param rxData Receive buffer - */ -static void CMDCODE_SetStartupParams(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) -{ - /* Get function code. */ - int funcCode = (int)(rxData->data[DATA_SEGMENT_ONE].typeF); - - if (funcCode == FOC_STARTUP_IF) { - SetStartupIFParams(mtrCtrl, rxData); - } -} - -/** - * @brief Set adjust speed mode. - * @param mtrCtrl The motor control handle. - * @param rxData Receive buffer - */ -static void CMDCODE_SetAdjustSpdMode(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) -{ - /* Get function code. */ - int funcCode = (int)(rxData->data[DATA_SEGMENT_ONE].typeF); - /* Get commond code. */ - int cmdCode = (int)(rxData->data[DATA_SEGMENT_TWO].typeF); - if (funcCode == HOST_SPEED_ADJUST) { - mtrCtrl->spdAdjustMode = HOST_SPEED_ADJUST; - /* Uart connect success. */ - mtrCtrl->uartConnectFlag = CONNECTING; - ackCode = 0X2A; - CUST_AckCode(g_uartTxBuf, ackCode, (float)mtrCtrl->spdAdjustMode); - } else if (funcCode == CUST_SPEED_ADJUST) { - if (cmdCode == 1) { /* If uart connection disconnected & stop motor commond. */ - SysCmdStopSet(&mtrCtrl->statusReg); - mtrCtrl->motorStateFlag = 0; - } - mtrCtrl->spdAdjustMode = CUST_SPEED_ADJUST; - ackCode = 0X2B; - CUST_AckCode(g_uartTxBuf, ackCode, (float)mtrCtrl->spdAdjustMode); - /* Uart disconnect. */ - mtrCtrl->uartConnectFlag = DISCONNECT; - } -} - -/** - * @brief Check uart connect. - * @param mtrCtrl The motor control handle. - * @param rxData Receive buffer - */ -static void CMDCODE_UartHandShake(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData) -{ - CMDCODE_SetAdjustSpdMode(mtrCtrl, rxData); -} - -/** - * @brief Set Motor Initial Status Parameters. - * @param mtrCtrl The motor control handle. - * @param rxData Receive buffer - * @param code Instruction code. - */ -static void CMDCODE_EXE_SetMotorInitParams(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData, unsigned char code) -{ - switch (code) { - case CMDCODE_SET_OBSERVER_TYPE: { /* Set observer type. */ - CMDCODE_SetObserverType(mtrCtrl, rxData); - } - break; - case CMDCODE_SET_PID_PARAMS: { /* Set motor pid parameters. */ - CMDCODE_SetMotorPidParams(mtrCtrl, rxData); - } - break; - case CMDCODE_SET_OBSERVER_PARAMS: { /* Set observer parameters. */ - CMDCODE_SetObserverParams(mtrCtrl, rxData); - } - break; - case CMDCODE_SET_MOTOR_TARGETSPD: { /* Set motor speed and speed slope. */ - CMDCODE_SetMotorSpdAndSlope(mtrCtrl, rxData); - } - break; - case CMDCODE_SET_MOTOR_PARAMS: { /* Set motor parameters. */ - CMDCODE_SetMotorParams(mtrCtrl, rxData); - } - break; - case CMDCODE_SET_ADJUSTSPD_MODE: /* Set adjust speed mode. */ - CMDCODE_SetAdjustSpdMode(mtrCtrl, rxData); - break; - default: - break; - } -} - -/** - * @brief Set Motor Control System Status. - * @param mtrCtrl The motor control handle. - * @param rxData Receive buffer - * @param code Instruction code. - */ -static void CMDCODE_EXE_SetMotorState(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData, unsigned char code) -{ - BASE_FUNC_UNUSED(rxData); - switch (code) { - case CMDCODE_MOTOR_START: { /* Motor start command */ - CMDCODE_MotorStart(mtrCtrl); - } - break; - case CMDCODE_MOTOR_STOP: { /* Motor stop command */ - CMDCODE_MotorStop(mtrCtrl); - } - break; - case CMDCODE_MOTORSTATE_RESET: { /* Motor reset command */ - CMDCODE_MotorReset(mtrCtrl); - } - break; - default: - break; - } -} - -/** - * @brief Set Startup and Uart Link Handshake Flag Parameters. - * @param mtrCtrl The motor control handle. - * @param rxData Receive buffer - * @param code Instruction code. - */ -static void CMDCODE_EXE_SetOtherParams(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData, unsigned char code) -{ - switch (code) { - case CMDCODE_SET_STARTUP_PARAMS: /* Set start up parameters. */ - CMDCODE_SetStartupParams(mtrCtrl, rxData); - break; - case CMDCODE_UART_HANDSHAKE: { /* Check uart hand shake. */ - CMDCODE_UartHandShake(mtrCtrl, rxData); - } - break; - case CMDCODE_UART_HEARTDETECT: { /* Check uart hand shake. */ - mtrCtrl->uartHeartDetCnt++; - } - break; - default: - break; - } -} -/** - * @brief Instruction code executor. - * @param mtrCtrl The motor control handle. - * @param rxData Receive buffer - * @param code Instruction code. - */ -static void CMDCODE_EXE_Process(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *rxData, unsigned char code) -{ - CMDCODE_EXE_SetMotorInitParams(mtrCtrl, rxData, code); - CMDCODE_EXE_SetMotorState(mtrCtrl, rxData, code); - CMDCODE_EXE_SetOtherParams(mtrCtrl, rxData, code); -} - -/** - * @brief Host data download callback and data parsing. - * @param mtrCtrl The motor control handle. - * @param rxBuf Receive buffer - */ -void CUST_UartDataProcess(MTRCTRL_Handle *mtrCtrl, unsigned char *rxBuf) -{ - /* Verify Parameters */ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - MCS_ASSERT_PARAM(rxBuf != NULL); - - /* Uart data storage struct */ - CUSTDATATYPE_DEF data; - volatile unsigned char *ptrCnt = &rxBuf[FRAME_CHECK_BEGIN + 1]; - volatile unsigned char *strCnt = &data.data[0].typeCh[0]; - for (unsigned int j = 0; j < FRAME_RECV_DATA_LENTH * FRAME_ONE_DATA_LENTH; j++) { - *strCnt++ = *ptrCnt++; - } - /* Message function code. */ - data.code = rxBuf[FRAME_CHECK_BEGIN]; - CMDCODE_EXE_Process(mtrCtrl, &data, data.code); -} - -/** - * @brief The host computer displays data transmission. - * @param mtrCtrl The motor control handle. - * @param txData Message content. - * @param stage Message status function code. - */ -void CUST_SetTxMsg(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *txData) -{ - /* Verify Parameters */ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - MCS_ASSERT_PARAM(txData != NULL); - if (mtrCtrl->stateMachine == FSM_IDLE) { - mtrCtrl->smo.spdEst = 0.0f; - } - /* Data send to host. */ - txData->data[CURRDQ_Q].typeF = mtrCtrl->idqFbk.q; - txData->data[CURRDQ_D].typeF = mtrCtrl->idqFbk.d; - txData->data[CURRREFDQ_Q].typeF = mtrCtrl->idqRef.q; - txData->data[CURRREFDQ_D].typeF = mtrCtrl->idqRef.d; - /* Motor current speed. */ - txData->data[CURRSPD].typeF = mtrCtrl->smo.spdEst * CONST_VALUE_60 / mtrCtrl->mtrParam.mtrNp; - /* Motor commond speed. */ - txData->data[SPDCMDHZ].typeF = mtrCtrl->spdCmdHz * CONST_VALUE_60 / mtrCtrl->mtrParam.mtrNp; - /* Bus voltage. */ - txData->data[UDC].typeF = mtrCtrl->udc; - /* Power board temprature. */ - txData->data[POWERBOARDTEMP].typeF = mtrCtrl->powerBoardTemp; - /* Motor protection status flag. */ - txData->data[CUST_ERR_CODE].typeI = mtrCtrl->prot.motorErrStatus.all; - /* Three phase current. */ - txData->data[CURRUVW_U].typeF = mtrCtrl->currUvw.u; - txData->data[CURRUVW_V].typeF = mtrCtrl->currUvw.v; - txData->data[CURRUVW_W].typeF = mtrCtrl->currUvw.w; - /* Three phase pwm duty. */ - txData->data[PWMDUTYUVW_U].typeF = mtrCtrl->dutyUvw.u; - txData->data[PWMDUTYUVW_V].typeF = mtrCtrl->dutyUvw.v; - txData->data[PWMDUTYUVW_W].typeF = mtrCtrl->dutyUvw.w; - /* Motor electric angle. */ - txData->data[AXISANGLE].typeF = mtrCtrl->axisAngle; - txData->data[VDQ_Q].typeF = mtrCtrl->vdqRef.q; - txData->data[VDQ_D].typeF = mtrCtrl->vdqRef.d; - txData->data[SPDREFHZ].typeF = mtrCtrl->spdRefHz * CONST_VALUE_60 / mtrCtrl->mtrParam.mtrNp; - txData->data[SENDTIMESTAMP].typeF = mtrCtrl->uartTimeStamp; -} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/hmi_module.c b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/hmi_module.c deleted file mode 100644 index 58bf15a7..00000000 --- a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/hmi_module.c +++ /dev/null @@ -1,57 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file hmi_module.c - * @author MCU Algorithm Team - * @brief This file provides functions declaration of human-machine interface. - */ - -#include "hmi_module.h" -#include "mcs_assert.h" - -/** - * @brief HMI Initializatio. - * @retval None. - */ -void HMI_Init(void) -{ - UartRecvInit(); -} - -/** - * @brief HMI processing. - * @param mtrCtrl The motor control handle.. - * @retval None. - */ -void HMI_Process_Rx(MTRCTRL_Handle *mtrCtrl) -{ - /* Verify Parameters */ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - UartModuleProcess_Rx(mtrCtrl); -} - -/** - * @brief HMI processing. - * @param mtrCtrl The motor control handle.. - * @retval None. - */ -void HMI_Process_Tx(MTRCTRL_Handle *mtrCtrl) -{ - /* Verify Parameters */ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - UartModuleProcess_Tx(mtrCtrl); -} diff --git a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/hmi_module.h b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/hmi_module.h deleted file mode 100644 index 056f8d73..00000000 --- a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/hmi_module.h +++ /dev/null @@ -1,37 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file hmi_module.h - * @author MCU Algorithm Team - * @brief This file provides functions declaration of system status. - */ - -/* Define to prevent recursive inclusion ------------------------------------------------------- */ -#ifndef McsMagicTag_HMI_MOUDLE_H -#define McsMagicTag_HMI_MOUDLE_H - -/* Includes ------------------------------------------------------------------------------------ */ -#include "uart_module.h" -/* Typedef definitions ------------------------------------------------------------------------- */ - - -void HMI_Init(void); - -void HMI_Process_Rx(MTRCTRL_Handle *mtrCtrl); -void HMI_Process_Tx(MTRCTRL_Handle *mtrCtrl); - -#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/protocol.c b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/protocol.c deleted file mode 100644 index 56f0c0bb..00000000 --- a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/protocol.c +++ /dev/null @@ -1,174 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file protocol.c - * @author MCU Algorithm Team - * @brief This file provides functions declaration of port communication. - */ - -#include "protocol.h" -#include "apt.h" -#include "typedefs.h" -#include "main.h" -#include "mcs_assert.h" -#include "cust_process.h" - -/** - * @brief Callback function for receiving data analysis and processing. - * @param rxBuf Receive buffer. - */ -__weak void CUST_UartDataProcess(MTRCTRL_Handle *mtrCtrl, unsigned char *rxBuf) -{ - BASE_FUNC_UNUSED(mtrCtrl); - BASE_FUNC_UNUSED(rxBuf); -} -/** - * @brief User-defined protocol message sending function (weak function). - * @param rxData Sending Messages.. - */ -__weak void CUST_SetTxMsg(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *txData) -{ - BASE_FUNC_UNUSED(mtrCtrl); - BASE_FUNC_UNUSED(*txData); -} - -static void (*g_ptrDataProcess)(MTRCTRL_Handle *mtrCtrl, unsigned char *rxBuf) = CUST_UartDataProcess; - -/** - * @brief Frame checksum. - * @param ptr Pointer to the data to be checked - * @param num Number of bytes - * @retval unsigned char Checksum - */ -static unsigned char CheckSum(unsigned char *ptr, unsigned char num) -{ - unsigned char sum = 0; - unsigned char *p = ptr; - /* Calculate the sum of received data. */ - for (unsigned char i = 0; i < num; i++) { - sum += (unsigned char)*p; - p++; - } - return sum; -} - -/** - * @brief Transmitting Data Frames. - * @param mtrCtrl The motor control handle. - * @param txBuf Sending Messages. - */ -unsigned int CUST_TransmitData(MTRCTRL_Handle *mtrCtrl, unsigned char *txBuf) -{ - /* Verify Parameters */ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - MCS_ASSERT_PARAM(txBuf != NULL); - unsigned int dataLen = FRAME_ONE_DATA_LENTH * SEND_FRAME_DATA_NUM; - unsigned char i = 0; - CUSTDATATYPE_DEF txData = {0}; - CUST_SetTxMsg(mtrCtrl, &txData); - txBuf[i++] = FRAME_START; - /* Message function code */ - txBuf[i++] = FRAME_SENT; - /* Message data */ - for (unsigned char x = 0; x < SEND_FRAME_DATA_NUM; x++) { - unsigned char floatIndex = 0; - unsigned char byteOffset = i; - txBuf[x * FRAME_ONE_DATA_LENTH + byteOffset++] = txData.data[x].typeCh[floatIndex++]; - txBuf[x * FRAME_ONE_DATA_LENTH + byteOffset++] = txData.data[x].typeCh[floatIndex++]; - txBuf[x * FRAME_ONE_DATA_LENTH + byteOffset++] = txData.data[x].typeCh[floatIndex++]; - txBuf[x * FRAME_ONE_DATA_LENTH + byteOffset++] = txData.data[x].typeCh[floatIndex++]; - } - /* Message verification domain */ - txBuf[dataLen + i++] = CheckSum((unsigned char*)&txBuf[FRAME_CHECK_BEGIN], dataLen + 1); - /* End of Message */ - txBuf[dataLen + i++] = FRAME_END; - return dataLen + i; -} - -/** - * @brief Transmitting Data Frames. - * @param txBuf Sending Cust Ack Code. - * @param ackCode Ack Code. - * @param varParams Host set parameter. - */ -void CUST_AckCode(unsigned char *txBuf, unsigned char ackCode, float varParams) -{ - /* Verify Parameters */ - MCS_ASSERT_PARAM(txBuf != NULL); - CUSTDATATYPE_DEF txData = {0}; - int dataIndex = 0; - unsigned int i = 0; - unsigned int txLen = 0; - unsigned char dataLen = FRAME_ONE_CHAR_LENTH + FRAME_ONE_DATA_LENTH; - - txData.data[0].typeF = varParams; - txBuf[i++] = FRAME_START; - /* Message function code */ - txBuf[i++] = FRAME_CUSTACK; - /* Message ack code */ - txBuf[i++] = ackCode; - /* Message data */ - txBuf[i++] = txData.data[0].typeCh[dataIndex++]; - txBuf[i++] = txData.data[0].typeCh[dataIndex++]; - txBuf[i++] = txData.data[0].typeCh[dataIndex++]; - txBuf[i++] = txData.data[0].typeCh[dataIndex++]; - - /* Message verification domain */ - txBuf[FRAME_ONE_CHAR_LENTH + i++] = CheckSum((unsigned char*)&txBuf[FRAME_CHECK_BEGIN], dataLen + 1); - /* End of Message */ - txBuf[FRAME_ONE_CHAR_LENTH + i++] = FRAME_END; - txLen = FRAME_ONE_CHAR_LENTH + i++; - HAL_UART_WriteDMA(&g_uart0, txBuf, txLen); -} - -/** - * @brief Cust receive data process. - * @param mtrCtrl The motor control handle. - * @param rxBuf Receive buffer - */ -void CUST_DataReceProcss(MTRCTRL_Handle *mtrCtrl, unsigned char *rxBuf) -{ - /* Verify Parameters */ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - MCS_ASSERT_PARAM(rxBuf != NULL); - unsigned char g_uartTxBuf[10] = {0}; - unsigned char ackCode = 0; - /* Frame header check */ - if (rxBuf[0] != FRAME_START) { - ackCode = 0X78; - CUST_AckCode(g_uartTxBuf, ackCode, 0); - return; - } - /* Frame trailer check */ - if (rxBuf[FRAME_LENTH - 1] != FRAME_END) { - ackCode = 0X79; - CUST_AckCode(g_uartTxBuf, ackCode, 0); - return; - } - /* Checksum */ - if (CheckSum((unsigned char*)&rxBuf[FRAME_CHECK_BEGIN], FRAME_CHECK_NUM) != rxBuf[FRAME_CHECKSUM]) { - ackCode = 0X7A; - CUST_AckCode(g_uartTxBuf, ackCode, 0); - return; - } else { - if (g_ptrDataProcess == NULL) { - return; - } else { - g_ptrDataProcess(mtrCtrl, rxBuf); - } - } -} diff --git a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/protocol.h b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/protocol.h deleted file mode 100644 index 900a00d8..00000000 --- a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/protocol.h +++ /dev/null @@ -1,121 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file protocol.h - * @author MCU Algorithm Team - * @brief This file provides functions declaration of port communication. - */ -#ifndef McsMagicTag_PORTOCOL_H -#define McsMagicTag_PORTOCOL_H - -#include "uart.h" -#include "mcs_carrier.h" - -#define RS485_SEND_ENABLE GPIO6->GPIO_DATA[GPIO_PIN_7].reg = GPIO_PIN_7 -#define RS485_SEND_DISABLE GPIO6->GPIO_DATA[GPIO_PIN_7].reg = BASE_CFG_UNSET - -#define RX_BUF_LEN (16) -#define SEND_FRAME_DATA_NUM (CUSTDATA_MAX) -/* Service Uart0 Communication Deal */ -#define FRAME_ONE_DATA_LENTH 4 -#define FRAME_ONE_CHAR_LENTH 1 -#define FRAME_RECV_DATA_LENTH 4 -#define FRAME_LENTH 20 /* Data length */ -#define FRAME_SENT 0X8F -#define FRAME_CUSTACK 0X8A -#define FRAME_START 0x0F /* Start frame */ -#define FRAME_END '/' /* StOP frame */ -#define FRAME_CHECK_BEGIN 1 /* Check frame */ -#define FRAME_CHECKSUM 18 /* Check sum */ -#define FRAME_CHECK_NUM 17 -#define CMDCODE_IDLE_FRAME 0x55 /* Fill frame */ -#define CMDCODE_GET_MOTOR_PARAMS 0x01 -#define CMDCODE_SEND_MOTOR_PARAMS 0x02 -#define CMDCODE_SET_MOTOR_CTLMODE 0x03 -#define CMDCODE_SET_OBSERVER_TYPE 0x04 -#define CMDCODE_SET_STARTUP_MODE 0x05 -#define CMDCODE_SET_PID_PARAMS 0x06 -#define CMDCODE_SET_STARTUP_PARAMS 0x07 -#define CMDCODE_SET_OBSERVER_PARAMS 0x08 -#define CMDCODE_SET_MOTOR_TARGETSPD 0x09 -#define CMDCODE_SET_MOTOR_PARAMS 0x0A -#define CMDCODE_MOTOR_START 0x0B -#define CMDCODE_MOTOR_STOP 0x0C -#define CMDCODE_MOTORSTATE_RESET 0x0D -#define CMDCODE_SEND_FIRMVERSION 0x0E -#define CMDCODE_SET_ADJUSTSPD_MODE 0x11 -#define CMDCODE_UART_HANDSHAKE 0x12 -#define CMDCODE_UART_HEARTDETECT 0x13 - -typedef union { - unsigned char typeCh[4]; - float typeF; - int typeI; -} UNIONDATATYPE_DEF; - -typedef enum { - OFFLINE_RES = 0, - OFFLINE_LD, - OFFLINE_LQ, - OFFLINE_PSIF, - OFFLINE_JS, - OFFLINE_NP, - OFFLINE_B, - OFFLINE_KPD, - OFFLINE_KID, - OFFLINE_KPQ, - OFFLINE_KIQ, - OFFLINE_KPS, - OFFLINE_KIS, - OFFLINE_SPEED, - OFLINE_MAX -} OFFLINE_IDEN_TYPE; - -typedef enum { - CURRDQ_Q = 0, - CURRDQ_D, - CURRREFDQ_Q, - CURRREFDQ_D, - CURRSPD, - SPDCMDHZ, - UDC, - POWERBOARDTEMP, - CUST_ERR_CODE, - CURRUVW_U, - CURRUVW_V, - CURRUVW_W, - PWMDUTYUVW_U, - PWMDUTYUVW_V, - PWMDUTYUVW_W, - AXISANGLE, - VDQ_Q, - VDQ_D, - SPDREFHZ, - SENDTIMESTAMP, - CUSTDATA_MAX -} SENDTOHOSTPARAMS; - -typedef struct { - volatile unsigned char code; - volatile UNIONDATATYPE_DEF data[SEND_FRAME_DATA_NUM]; -} CUSTDATATYPE_DEF; - - -void CUST_DataReceProcss(MTRCTRL_Handle *mtrCtrl, unsigned char *rxBuf); -unsigned int CUST_TransmitData(MTRCTRL_Handle *mtrCtrl, unsigned char *txBuf); -void CUST_AckCode(unsigned char *txBuf, unsigned char ackCode, float varParams); -#endif /* McsMagicTag_PORTOCOL_H */ diff --git a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/uart_module.c b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/uart_module.c deleted file mode 100644 index df5e9186..00000000 --- a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/uart_module.c +++ /dev/null @@ -1,184 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the - * following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file uart_module.c - * @author MCU Algorithm Team - * @brief This file provides functions declaration of Serial port communication. - */ -#include "uart_module.h" -#include "debug.h" -#include "main.h" -#include "baseinc.h" - -/* Buffer size */ -#define UI_TX_BUF_LEN (96) -#define UI_RX_BUF_LEN (96) - -/* Receiving Timeout Interval */ -#define UART_TIME_OUT_MS (100) - -/* Start sending data to host delay after uart connect success */ -#define UART_UPDATA_DELAY_TIME_MS (50) - -/* Data buffer */ -unsigned char g_uartRxBuf[UI_RX_BUF_LEN] = {0}; -unsigned char g_uartTxBuf[UI_TX_BUF_LEN] = {0}; -static unsigned int getdeltaSystickCnt = 0; -static FRAME_Handle g_uartFrame; -/** - * @brief Receive Data Clear. - * @param uartFrame Receice Data. - * @retval None. - */ -static void FrameRecvClear(FRAME_Handle *uartFrame) -{ - /* Clear buffer lenth. */ - uartFrame->buffLen = 0; - uartFrame->timeOutCnt = 0; - uartFrame->frameFlag = 0; - /* Clear received data lenth. */ - uartFrame->rxLen = 0; - /* Clear received flag. */ - uartFrame->rxFlag = 0; - uartFrame->upDataCnt = 0; - uartFrame->rxAckFlag = 0; -} - -/** - * @brief Set Dma status. - * @param mtrCtrl The motor control handle. - * @retval None. - */ -static void SetUartDmaStatus(MTRCTRL_Handle *mtrCtrl) -{ - /* Delay 50ms start uart Tx DMA . */ - if (mtrCtrl->uartConnectFlag == CONNECTING && g_uartFrame.upDataDelayCnt++ > UART_UPDATA_DELAY_TIME_MS) { - g_uartFrame.txFlag = 1; /* Start send data flag. */ - mtrCtrl->uartConnectFlag = CONNECTED; - g_uartFrame.upDataDelayCnt = 0; - } - if (mtrCtrl->uartConnectFlag == DISCONNECT) { - g_uartFrame.txFlag = 0; /* Stop send data flag. */ - mtrCtrl->uartTimeStamp = 0; - } -} - -/** - * @brief Uart Dma interupt callback func. - * @param handle Uart handle. - * @retval None. - */ -void UART0_TXDMACallback(void *handle) -{ - BASE_FUNC_UNUSED(handle); - unsigned int getCurSystickCnt = 0; - static unsigned int getlastSystickCnt = 0; - /* USER CODE BEGIN UART0_WRITE_DMA_FINISH */ - g_uartFrame.txFlag = 1; - /* Received information answered, information update reserved. */ - if (g_uartFrame.rxAckFlag == 1) { - g_uartFrame.uartItTxFlag = 1; - g_uartFrame.rxAckFlag = 0; - } - getCurSystickCnt = DCL_SYSTICK_GetTick(); - if (getlastSystickCnt != 0) { - /* Calculate unit frame data send time */ - getdeltaSystickCnt = getCurSystickCnt - getlastSystickCnt; - } - getlastSystickCnt = getCurSystickCnt; - /* USER CODE END UART0_WRITE_DMA_FINISH */ -} - -/** - * @brief Uart0 Interruput Read CallBack Function. - * @param handle Uart handle. - * @retval None. - */ -void UART0ReadInterruptCallback(void *handle) -{ - BASE_FUNC_UNUSED(handle); - /* USER CODE BEGIN UART0_READ_IT_FINISH */ - if (g_uartFrame.rxLen >= UI_RX_BUF_LEN - 1) { - g_uartFrame.rxLen = 0; - } - HAL_UART_ReadIT(handle, &g_uartFrame.rxData, 1); - g_uartRxBuf[g_uartFrame.rxLen] = g_uartFrame.rxData; - g_uartFrame.rxLen++; - g_uartFrame.rxFlag = 1; - g_uartFrame.uartItTxFlag = 0; - return; - /* USER CODE END UART0_READ_IT_FINISH */ -} - -/** - * @brief Uart Read Data Init Function. - * @param void. - * @retval None. - */ -void UartRecvInit(void) -{ - /* Uart reception initialization */ - FrameRecvClear(&g_uartFrame); - HAL_UART_ReadIT(&g_uart0, &g_uartFrame.rxData, 1); -} - -/** - * @brief Uart Read Data Process Function. - * @param mtrCtrl The motor control handle. - * @retval None. - */ -void UartModuleProcess_Rx(MTRCTRL_Handle *mtrCtrl) -{ - /* Verify Parameters */ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - SetUartDmaStatus(mtrCtrl); - if (g_uartFrame.rxFlag == 1) { /* Receive data flag. */ - if (g_uartFrame.timeOutCnt++ > UART_TIME_OUT_MS) { - /* Received data from the host. */ - g_uartFrame.frameFlag = 1; - g_uartFrame.rxFlag = 0; - g_uartFrame.timeOutCnt = 0; - /* Execute data process. */ - CUST_DataReceProcss(mtrCtrl, g_uartRxBuf); - g_uartFrame.rxAckFlag = 1; - g_uartFrame.rxLen = 0; - } - } - g_uartFrame.frameFlag = 0; -} - -/** - * @brief Uart Write Data Process Function. - * @param mtrCtrl The motor control handle. - * @retval None. - */ -void UartModuleProcess_Tx(MTRCTRL_Handle *mtrCtrl) -{ - /* Verify Parameters */ - MCS_ASSERT_PARAM(mtrCtrl != NULL); - if (g_uartFrame.txFlag == 1) { /* Send data flag. */ - mtrCtrl->uartTimeStamp = (float)getdeltaSystickCnt; /* Unit data time stamp */ - g_uartFrame.upDataCnt = 0; - g_uartFrame.txFlag = 0; - /* Send data to host. */ - unsigned int txLen = CUST_TransmitData(mtrCtrl, g_uartTxBuf); - /* If txIT mode send data finish, convert to DMA mode */ - if (g_uartFrame.uartItTxFlag == 1) { - HAL_UART_WriteDMA(&g_uart0, g_uartTxBuf, txLen); - } - } -} diff --git a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/uart_module.h b/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/uart_module.h deleted file mode 100644 index f1f8f899..00000000 --- a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/uart_module.h +++ /dev/null @@ -1,47 +0,0 @@ -/** - * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. - * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the - * following conditions are met: - * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following - * disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the - * following disclaimer in the documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote - * products derived from this software without specific prior written permission. - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE - * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file uart_module.h - * @author MCU Algorithm Team - * @brief This file provides functions declaration of Serial port communication. - */ -#ifndef McsMagicTag_UART_MODULE_H -#define McsMagicTag_UART_MODULE_H - -#include "protocol.h" -#include "mcs_ctlmode_config.h" - -typedef struct { - unsigned int buffLen; - unsigned int timeOutCnt; - unsigned char frameFlag; - unsigned int rxLen; - unsigned char rxFlag; - unsigned char txFlag; - unsigned char rxData; - unsigned int upDataCnt; - unsigned int upDataDelayCnt; - unsigned char uartItTxFlag; - unsigned char rxAckFlag; -} FRAME_Handle; - - -void UartRecvInit(void); -void UartModuleProcess_Rx(MTRCTRL_Handle *mtrCtrl); -void UartModuleProcess_Tx(MTRCTRL_Handle *mtrCtrl); - -#endif \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_FOC/user/generatecode/feature.h b/vendor/yibaina_3061M/demo/sample_gpio_key/generatecode/feature.h similarity index 99% rename from vendor/yibaina_3061M/demo/10025_FOC/user/generatecode/feature.h rename to vendor/yibaina_3061M/demo/sample_gpio_key/generatecode/feature.h index a18d8c21..a1f9eb27 100644 --- a/vendor/yibaina_3061M/demo/10025_FOC/user/generatecode/feature.h +++ b/vendor/yibaina_3061M/demo/sample_gpio_key/generatecode/feature.h @@ -18,7 +18,6 @@ * @file feature.h * @author MCU Driver Team * @brief This file contains macro configurations related to the project. This file is generated by the IDE tool. - * @date 2025-04-17 14:22:06 */ #ifndef McuMagicTag_FEATURE_H diff --git a/vendor/yibaina_3061M/demo/10025_FOC/user/generatecode/main.h b/vendor/yibaina_3061M/demo/sample_gpio_key/generatecode/main.h similarity index 70% rename from vendor/yibaina_3061M/demo/10025_FOC/user/generatecode/main.h rename to vendor/yibaina_3061M/demo/sample_gpio_key/generatecode/main.h index 1c8caefb..c77e0f2e 100644 --- a/vendor/yibaina_3061M/demo/10025_FOC/user/generatecode/main.h +++ b/vendor/yibaina_3061M/demo/sample_gpio_key/generatecode/main.h @@ -18,27 +18,16 @@ * @file main.h * @author MCU Driver Team * @brief This file contains driver init functions. - * @date 2025-04-17 14:22:06 */ /* Define to prevent recursive inclusion ------------------------------------- */ #ifndef McuMagicTag_SYSTEM_INIT_H #define McuMagicTag_SYSTEM_INIT_H -#include "adc.h" -#include "adc_ex.h" -#include "acmp.h" -#include "acmp_ex.h" -#include "apt.h" #include "uart.h" #include "uart_ex.h" #include "gpio.h" -#include "timer.h" -#include "timer_ex.h" -#include "pga.h" #include "crg.h" -#include "dma.h" -#include "dma_ex.h" #include "iocmg.h" #define IO_SPEED_FAST 0x00U @@ -54,42 +43,14 @@ #define XTAL_DRV_LEVEL2 0x01U #define XTAL_DRV_LEVEL1 0x00U -#define GpioStartStop_PIN GPIO_PIN_4 -#define GpioStartStop_HANDLE g_gpio2 - -extern ACMP_Handle g_acmp0; -extern PGA_Handle g_pga0; -extern PGA_Handle g_pga1; -extern TIMER_Handle g_timer0; -extern TIMER_Handle g_timer1; extern UART_Handle g_uart0; -extern APT_Handle g_apt0; -extern APT_Handle g_apt1; -extern APT_Handle g_apt2; -extern ADC_Handle g_adc0; - -extern DMA_Handle g_dmac; extern GPIO_Handle g_gpio2; -extern GPIO_Handle g_gpio1; BASE_StatusType CRG_Config(CRG_CoreClkSelect *coreClkSelect); void SystemInit(void); -void UART0WriteInterruptCallback(void *handle); -void UART0ReadInterruptCallback(void *handle); - -void UART0InterruptErrorCallback(void *handle); -void MotorStatemachineCallBack(void *handle); -void TIMER1_DMAOverFlow_InterruptProcess(void *handle); -void CheckPotentiometerValueCallback(void *handle); -void TIMER0_DMAOverFlow_InterruptProcess(void *handle); -void MotorCarrierProcessCallback(void *aptHandle); -void MotorSysErrCallback(void *aptHandle); - -void UART0_TXDMACallback(void *handle); - -void MotorStartStopKeyCallback(void *param); +void GPIO_CallBackFunc(void *param); /* USER CODE BEGIN 0 */ /* USER CODE 区域内代码不会被覆盖,区域外会被生成的默认代码覆盖(其余USER CODE 区域同理) */ diff --git a/vendor/yibaina_3061M/demo/sample_gpio_key/generatecode/system_init.c b/vendor/yibaina_3061M/demo/sample_gpio_key/generatecode/system_init.c new file mode 100644 index 00000000..24a14c9c --- /dev/null +++ b/vendor/yibaina_3061M/demo/sample_gpio_key/generatecode/system_init.c @@ -0,0 +1,124 @@ +/** + * @copyright Copyright (c) 2022, HiSilicon (Shanghai) Technologies Co., Ltd. All rights reserved. + * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the + * following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the + * following disclaimer in the documentation and/or other materials provided with the distribution. + * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written permission. + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE + * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * @file system_init.c + * @author MCU Driver Team + * @brief This file contains driver init functions. + */ + +#include "main.h" +#include "ioconfig.h" +#include "iocmg_ip.h" + +#define UART0_BAND_RATE 115200 + +BASE_StatusType CRG_Config(CRG_CoreClkSelect *coreClkSelect) +{ + CRG_Handle crg; + crg.baseAddress = CRG; + crg.pllRefClkSelect = CRG_PLL_REF_CLK_SELECT_HOSC; + crg.pllPreDiv = CRG_PLL_PREDIV_4; + crg.pllFbDiv = 48; /* PLL Multiplier 48 */ + crg.pllPostDiv = CRG_PLL_POSTDIV_2; + crg.coreClkSelect = CRG_CORE_CLK_SELECT_PLL; + crg.handleEx.pllPostDiv2 = CRG_PLL_POSTDIV2_3; + crg.handleEx.clk1MSelect = CRG_1M_CLK_SELECT_HOSC; + crg.handleEx.clk1MDiv = (25 - 1); /* The 1 MHz freq is equal to the input clock frequency / (clk_1m_div + 1). 25 is the div of the clk_1m in CLOCK. */ + + if (HAL_CRG_Init(&crg) != BASE_STATUS_OK) { + return BASE_STATUS_ERROR; + } + *coreClkSelect = crg.coreClkSelect; + return BASE_STATUS_OK; +} + +__weak void GPIO_CallBackFunc(void *param) +{ + GPIO_Handle *handle = (GPIO_Handle *)param; + BASE_FUNC_UNUSED(handle); +} + +static void GPIO_Init(void) +{ + HAL_CRG_IpEnableSet(GPIO2_BASE, IP_CLK_ENABLE); + g_gpio2.baseAddress = GPIO2; + + g_gpio2.pins = GPIO_PIN_4; + HAL_GPIO_Init(&g_gpio2); + HAL_GPIO_SetDirection(&g_gpio2, g_gpio2.pins, GPIO_INPUT_MODE); + HAL_GPIO_SetValue(&g_gpio2, g_gpio2.pins, GPIO_LOW_LEVEL); + HAL_GPIO_SetIrqType(&g_gpio2, g_gpio2.pins, GPIO_INT_TYPE_RISE_EDGE); + + HAL_GPIO_RegisterCallBack(&g_gpio2, GPIO_PIN_4, GPIO_CallBackFunc); + IRQ_Register(IRQ_GPIO2, HAL_GPIO_IrqHandler, &g_gpio2); + IRQ_SetPriority(IRQ_GPIO2, 1); /* set gpio1 interrupt priority to 1, 1~15. 1 is priority value */ + IRQ_EnableN(IRQ_GPIO2); /* gpio interrupt enable */ + + return; +} + +static void UART0_Init(void) +{ + HAL_CRG_IpEnableSet(UART0_BASE, IP_CLK_ENABLE); /* UART0 clock enable. */ + g_uart0.baseAddress = UART0; + + g_uart0.baudRate = UART0_BAND_RATE; + g_uart0.dataLength = UART_DATALENGTH_8BIT; + g_uart0.stopBits = UART_STOPBITS_ONE; + g_uart0.parity = UART_PARITY_NONE; + g_uart0.txMode = UART_MODE_BLOCKING; + g_uart0.rxMode = UART_MODE_BLOCKING; + g_uart0.fifoMode = BASE_CFG_ENABLE; + g_uart0.fifoTxThr = UART_FIFODEPTH_SIZE8; + g_uart0.fifoRxThr = UART_FIFODEPTH_SIZE8; + g_uart0.hwFlowCtr = BASE_CFG_DISABLE; + g_uart0.handleEx.overSampleMultiple = UART_OVERSAMPLING_16X; + g_uart0.handleEx.msbFirst = BASE_CFG_DISABLE; + HAL_UART_Init(&g_uart0); +} + +static void IOConfig(void) +{ + /* Config PIN21 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO2_4_AS_GPIO2_4); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO2_4_AS_GPIO2_4, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO2_4_AS_GPIO2_4, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO2_4_AS_GPIO2_4, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO2_4_AS_GPIO2_4, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN39 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO0_3_AS_UART0_TXD); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO0_3_AS_UART0_TXD, PULL_NONE); /* Pull-up and Pull-down */ + HAL_IOCMG_SetPinSchmidtMode(GPIO0_3_AS_UART0_TXD, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO0_3_AS_UART0_TXD, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO0_3_AS_UART0_TXD, DRIVER_RATE_2); /* Output signal edge fast/slow */ + /* Config PIN40 */ + HAL_IOCMG_SetPinAltFuncMode(GPIO0_4_AS_UART0_RXD); /* Check function selection */ + HAL_IOCMG_SetPinPullMode(GPIO0_4_AS_UART0_RXD, PULL_UP); /* Pull-up and Pull-down, UART RX recommend PULL_UP */ + HAL_IOCMG_SetPinSchmidtMode(GPIO0_4_AS_UART0_RXD, SCHMIDT_DISABLE); /* Schmitt input on/off */ + HAL_IOCMG_SetPinLevelShiftRate(GPIO0_4_AS_UART0_RXD, LEVEL_SHIFT_RATE_SLOW); /* Output drive capability */ + HAL_IOCMG_SetPinDriveRate(GPIO0_4_AS_UART0_RXD, DRIVER_RATE_2); /* Output signal edge fast/slow */ +} + +void SystemInit(void) +{ + IOConfig(); + UART0_Init(); + GPIO_Init(); + + /* USER CODE BEGIN system_init */ + /* USER CODE END system_init */ +} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/cust_process.h b/vendor/yibaina_3061M/demo/sample_gpio_key/gpio_key_sample.c similarity index 55% rename from vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/cust_process.h rename to vendor/yibaina_3061M/demo/sample_gpio_key/gpio_key_sample.c index 60fe69bb..6ec5c721 100644 --- a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/user_interface/cust_process.h +++ b/vendor/yibaina_3061M/demo/sample_gpio_key/gpio_key_sample.c @@ -15,22 +15,51 @@ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file cust_process.h - * @author MCU Algorithm Team - * @brief This file provides functions declaration of system status. + * @file gpio_key_sample.c + * @author MCU Driver Team + * @brief GPIO module realize a key function sample + * @details GPIO Capturing Key Triggered Interrupt Service Function. If the hardware environment does \ + * not support this function, you need to set up an environment for verification. */ -/* Define to prevent recursive inclusion ------------------------------------------------------- */ -#ifndef McsMagicTag_HMI_MOUDLE_H -#define McsMagicTag_HMI_MOUDLE_H +/* Includes ------------------------------------------------------------------*/ +#include "debug.h" +#include "gpio.h" +#include "main.h" +#include "gpio_key_sample.h" -/* Includes ------------------------------------------------------------------------------------ */ -#include "protocol.h" -/* Typedef definitions ------------------------------------------------------------------------- */ +#define PREVENT_SWIPE_SCREEN_TIME 50 +#define CYCLE_INTERVAL_TIME 500 +/* prototype functions -------------------------------------------------------*/ +void GPIO_CallBackFunc(void *param); -void CUST_UartDataProcess(MTRCTRL_Handle *mtrCtrl, unsigned char *rxBuf); +/* ---------------------------------- Sample Parameters ---------------------- */ +/** + * @brief GPIO key test sample. + * @param None + * @retval Value of @ref BASE_StatusType. + */ +BASE_StatusType GPIO_KeySample(void) +{ + SystemInit(); + /* Waiting for the key to come. */ + while (1) { + DBG_PRINTF("Wait key \r\n"); + BASE_FUNC_DELAY_MS(CYCLE_INTERVAL_TIME); + } -void CUST_SetTxMsg(MTRCTRL_Handle *mtrCtrl, CUSTDATATYPE_DEF *txData); + return BASE_STATUS_OK; +} -#endif \ No newline at end of file +/** + * @brief GPIO register interrupt callback function. + * @param param Value of @ref GPIO_Handle. + * @retval None + */ +void GPIO_CallBackFunc(void *param) +{ + BASE_FUNC_UNUSED(param); + DBG_PRINTF("in GPIO Key Handler \r\n"); + BASE_FUNC_DELAY_MS(PREVENT_SWIPE_SCREEN_TIME); +} \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_motor_process.h b/vendor/yibaina_3061M/demo/sample_gpio_key/gpio_key_sample.h similarity index 76% rename from vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_motor_process.h rename to vendor/yibaina_3061M/demo/sample_gpio_key/gpio_key_sample.h index 783bf214..e490c9de 100644 --- a/vendor/yibaina_3061M/demo/10025_FOC/user/pmsm_sensorless_2shunt_foc/inc/mcs_motor_process.h +++ b/vendor/yibaina_3061M/demo/sample_gpio_key/gpio_key_sample.h @@ -15,31 +15,16 @@ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * @file mcs_motor_process.h - * @author MCU Algorithm Team - * @brief This file provides motor sample functions declaration for ECMCU105H board. + * @file gpio_key_sample.h + * @author MCU Driver Team + * @brief GPIO module sample + * @details This file provides sample code for users to help use + * the trigger an interrupt based on the key connected to the GPIO. */ -#ifndef McuMagicTag_MCS_MOTOR_PROCESS_H -#define McuMagicTag_MCS_MOTOR_PROCESS_H -/** - * @brief phase sequence. - */ -typedef enum { - PHASE_U = 0, - PHASE_V, - PHASE_W, - PHASE_MAX_NUM -} MCS_PhaseEnum; - -/** - * @brief key state. - */ -typedef enum { - KEY_DOWN = 0, - KEY_UP = 1, -} KEY_State; +#ifndef GPIO_KEY_SAMPLE_H +#define GPIO_KEY_SAMPLE_H -int MotorMainProcess(void); +BASE_StatusType GPIO_KeySample(void); -#endif \ No newline at end of file +#endif /* GPIO_KEY_SAMPLE_H */ \ No newline at end of file diff --git a/vendor/yibaina_3061M/demo/10025_FOC/user/main.c b/vendor/yibaina_3061M/demo/sample_gpio_key/main.c similarity index 89% rename from vendor/yibaina_3061M/demo/10025_FOC/user/main.c rename to vendor/yibaina_3061M/demo/sample_gpio_key/main.c index 1dfa2fc3..10f82fbe 100644 --- a/vendor/yibaina_3061M/demo/10025_FOC/user/main.c +++ b/vendor/yibaina_3061M/demo/sample_gpio_key/main.c @@ -18,30 +18,18 @@ * @file main.c * @author MCU Driver Team * @brief Main program body. - * @date 2025-04-17 14:22:06 */ #include "typedefs.h" #include "feature.h" -#include "mcs_motor_process.h" +#include "gpio_key_sample.h" #include "main.h" /* USER CODE BEGIN 0 */ /* USER CODE 区域内代码不会被覆盖,区域外会被生成的默认代码覆盖(其余USER CODE 区域同理) */ /* 建议用户放置头文件 */ /* USER CODE END 0 */ -ACMP_Handle g_acmp0; -PGA_Handle g_pga0; -PGA_Handle g_pga1; -TIMER_Handle g_timer0; -TIMER_Handle g_timer1; UART_Handle g_uart0; -APT_Handle g_apt0; -APT_Handle g_apt1; -APT_Handle g_apt2; -ADC_Handle g_adc0; -DMA_Handle g_dmac; GPIO_Handle g_gpio2; -GPIO_Handle g_gpio1; /* USER CODE BEGIN 1 */ /* 建议用户定义全局变量、结构体、宏定义或函数声明等 */ /* USER CODE END 1 */ @@ -51,7 +39,7 @@ int main(void) /* USER CODE BEGIN 2 */ /* 建议用户放置初始化代码或启动代码等 */ /* USER CODE END 2 */ - MotorMainProcess(); + GPIO_KeySample(); /* USER CODE BEGIN 3 */ /* 建议用户放置初始配置代码 */ /* USER CODE END 3 */ diff --git a/vendor/yibaina_3061M/demo/sample_gpio_key/readme.md b/vendor/yibaina_3061M/demo/sample_gpio_key/readme.md new file mode 100644 index 00000000..839d2230 --- /dev/null +++ b/vendor/yibaina_3061M/demo/sample_gpio_key/readme.md @@ -0,0 +1,78 @@ +# sample_gpio_key + +## 1.1 介绍 + +**功能介绍:** 基于HAL接口完成时钟、GPIO控制器初始化和功能配置。在示例代码中通过中断的方式实现对S2按键的检测。 + +**软件概述:** 使用中断方式实现按键功能。 + +**硬件概述:** 核心板。通过板端丝印可以看出S2按键的GPIO对应GPIO2_4。硬件搭建要求如图所示: + + + +![image-20240829165616356](../../../../docs/pic/tools/image-20240829165616356.png) + +## 1.2 约束与限制 + +### 1.2.1 支持应用运行的芯片和开发板 + + 本示例支持开发板:3061M + +### 1.2.2 支持API版本、SDK版本 + + 本示例支持版本号:SolarA2_1.0.1.2 + +### 1.2.3 支持IDE版本 + + 本示例支持IDE版本号:1.0.0.6; + +## 1.3 效果预览 + + 当用户烧录编译后的示例代码后,初始化和配置完成后,示例代码中在按键没有按下时Debug串口会一直打印等待按键的log信息,当连接的按键按下时功能正常时Debug串口打印成功log信息;功能异常时按下按键时Debug串口还是会一直打印等待按键的log信息。 + +![image-20240829172606786](../../../../docs/pic/sample_gpio_key/image-20240829172606786.png) + +## 1.4 接口介绍 + +#### 1.4.1 HAL_GPIO_Init() + +| **定义:** | void HAL_GPIO_Init(GPIO_Handle *handle); | +| ------------ | ----------------------------------------- | +| **功能:** | 初始化GPIO寄存器值 | +| **参数:** | handle:GPIO_Handle的值 | +| **返回值:** | None | +| **依赖:** | drivers\gpio\common\inc\gpio.h | + +## 1.5 具体实现 + + 步骤一:GPIO管脚选择:示例代码中选择GPIO管脚用于通过中断方式进行按键检测。也可以选择其他GPIO管脚用于按键检测功能测试,在"GPIO_Init()"接口中的"g_gpiox.baseAddress"可以配置其它GPIOX,g_gpiox.pins可以配置“GPIO_PIN_0-GPIO_PIN_7”中的任意一个。 + + 步骤二:GPIO管脚初始化:调用接口"HAL_GPIO_Init()”完成对示例代码中GPIO管脚的方向、电平、中断模式配置。 + +## 1.6 实验流程 + +- 步骤一:在xxx\vendor\yibaina_3061M\demo\sample_gpio_key/文件夹里面内容复制替换到xxx/user/目录下。 + + ![image-20240829174750105](../../../../docs/pic/sample_gpio_key/image-20240829174750105.png) + + +- 步骤二:点击编译按钮![image-20240528173107958](../../../../docs/pic/sample_gpio_key/image-20240528173107958-17249242848691.png),编译成功后终端窗口输出如下图所示。 + ![image-20240624165330284](../../../../docs/pic/tools/image-20240624165330284.png) + + + +- 步骤三:进入工程配置界面。打开要烧录的工程后,单击顶部![image-20240624154427635](../../../../docs/pic/sample_gpio_key/image-20240624154427635-17249243742892.png)的图标,进入工程配置界面->进入程序加载。选择传输方式为“swd”,并配置其他参数。(具体[参考tools目录README搭建环境](https://gitee.com/HiSpark/open_mcu/tree/master/tools)) + + ![image-20240829174913995](../../../../docs/pic/sample_gpio_key/image-20240829174913995.png) + +- 步骤四:单击“烧录”![image-20240624162016190](../../../../docs/pic/tools/image-20240624162016190.png) 按钮,开始烧录。烧录成功后终端窗口输出如下图所示。 + + ![image-20240624162037834](../../../../docs/pic/sample_gpio_key/image-20240624162037834-17249245112874.png) + +* 步骤五 : 当用户烧录编译后的示例代码后,初始化和配置完成后,示例代码中在按键没有按下时Debug串口会一直打印等待按键的log信息,当连接的按键按下时功能正常时Debug串口打印成功log信息;功能异常时按下按键时Debug串口还是会一直打印等待按键的log信息。 + + ![image-20240829172606786](../../../../docs/pic/sample_gpio_key/image-20240829172606786.png) + + + + \ No newline at end of file -- Gitee From b01030e74decdb6bf28458168ceeea16a110a213 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B8=A9=E8=80=81=E5=B8=88?= <1161625498@qq.com> Date: Tue, 3 Jun 2025 16:27:37 +0800 Subject: [PATCH 4/5] update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 697e525b..b43eda6a 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ | 名称 | 购买链接 | 介绍 | | :------------: | ------------------------------------------------------------ | ------------------------------- | -| V5A20002NOQJDB | [批量购买]([V5A20002NOQJDB价格_V5A20002NOQJDB库存_V5A20002NOQJDB采购信息-云汉芯城 ICkey.cn](https://search.ickey.cn/?keyword=V5A20002NOQJDB&bom_ab=null)) | 3061M系列解决方案 128KB QFP48 | +| V5A20002NOQJDB | [批量购买](https://search.ickey.cn/?keyword=V5A20002NOQJDB&bom_ab=null) | 3061M系列解决方案 128KB QFP48 | | V5A20002NOOJLB | [批量购买](https://www.ickey.cn/detail/1003001016603886/V5A20002NOOJLB.html#208f8e9e-5cdf-4353-988d-d1df20bbb33f) [少量购买](https://www.ickey.cn/detail/1003001021937018/V5A20002NOOJLB.html#a48f53a5-b3cf-4d3e-8254-e91505f16fae) | 3061M系列解决方案 128KB QFN32 | | V5A20002NOQJLB | [批量购买](https://www.ickey.cn/detail/1003001016603885/V5A20002NOQJLB.html#208f8e9e-5cdf-4353-988d-d1df20bbb33f) [少量购买](https://www.ickey.cn/detail/1003001017510002/V5A20002NOQJLB.html#a48f53a5-b3cf-4d3e-8254-e91505f16fae) | 3061M系列解决方案 128KB QFP32 | | V5A20002NOOJDB | [批量购买](https://www.ickey.cn/detail/1003001016603884/V5A20002NOOJDB.html#208f8e9e-5cdf-4353-988d-d1df20bbb33f) | 3061M系列解决方案 128KB QFN48 | -- Gitee From 3d567cfad20f8b6cd36ef5878f10081895a90b52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B8=A9=E8=80=81=E5=B8=88?= <1161625498@qq.com> Date: Tue, 3 Jun 2025 17:04:32 +0800 Subject: [PATCH 5/5] update README.md --- README.md | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index b43eda6a..b205e38e 100644 --- a/README.md +++ b/README.md @@ -6,17 +6,17 @@ ## 购买渠道 -| 名称 | 购买链接 | 介绍 | -| :------------: | ------------------------------------------------------------ | ------------------------------- | -| V5A20002NOQJDB | [批量购买](https://search.ickey.cn/?keyword=V5A20002NOQJDB&bom_ab=null) | 3061M系列解决方案 128KB QFP48 | -| V5A20002NOOJLB | [批量购买](https://www.ickey.cn/detail/1003001016603886/V5A20002NOOJLB.html#208f8e9e-5cdf-4353-988d-d1df20bbb33f) [少量购买](https://www.ickey.cn/detail/1003001021937018/V5A20002NOOJLB.html#a48f53a5-b3cf-4d3e-8254-e91505f16fae) | 3061M系列解决方案 128KB QFN32 | -| V5A20002NOQJLB | [批量购买](https://www.ickey.cn/detail/1003001016603885/V5A20002NOQJLB.html#208f8e9e-5cdf-4353-988d-d1df20bbb33f) [少量购买](https://www.ickey.cn/detail/1003001017510002/V5A20002NOQJLB.html#a48f53a5-b3cf-4d3e-8254-e91505f16fae) | 3061M系列解决方案 128KB QFP32 | -| V5A20002NOOJDB | [批量购买](https://www.ickey.cn/detail/1003001016603884/V5A20002NOOJDB.html#208f8e9e-5cdf-4353-988d-d1df20bbb33f) | 3061M系列解决方案 128KB QFN48 | -| V5A20002NOOJD9 | [批量购买](https://www.ickey.cn/detail/1003001016603888/V5A20002NOOJD9.html#d9fd2c53-3b07-4b62-ba26-cb59ae94234f) [少量购买](https://www.ickey.cn/detail/1003001021937055/V5A20002NOOJD9.html#a48f53a5-b3cf-4d3e-8254-e91505f16fae) | 3061M系列解决方案 64KB QFN48 | -| V5A20002NOQJD9 | [批量购买](https://www.ickey.cn/detail/1003001016603887/V5A20002NOQJD9.html#208f8e9e-5cdf-4353-988d-d1df20bbb33f) | 3061M系列解决方案 64KB QFP48 | -| V5A20002NOQJL9 | [批量购买](https://www.ickey.cn/detail/1003001016603889/V5A20002NOQJL9.html#208f8e9e-5cdf-4353-988d-d1df20bbb33f) [少量购买](https://www.ickey.cn/detail/1003001021936978/V5A20002NOQJL9.html#a48f53a5-b3cf-4d3e-8254-e91505f16fae) | 3061M系列解决方案 64KB QFP32 | -| V5A20002NOOJL9 | [批量购买](https://www.ickey.cn/detail/1003001016603890/V5A20002NOOJL9.html#208f8e9e-5cdf-4353-988d-d1df20bbb33f) [少量购买](https://www.ickey.cn/detail/1003001021936989/V5A20002NOOJL9.html#a48f53a5-b3cf-4d3e-8254-e91505f16fae) | 3061M系列解决方案 64KB QFN32 | -| V5A20001ISQJSA | [批量购买](https://www.ickey.cn/detail/1003001016603882/V5A20001ISQJSA.html#) | 3065H系列解决方案 160KB LQFP64 | +| 名称 | 批量购买 | 少量购买 | 介绍 | +| :------------: | ------------------------------------------------------------ | ------------------------------------------------------------ | ------------------------------- | +| V5A20002NOQJDB | [购买链接](https://search.ickey.cn/?keyword=V5A20002NOQJDB&bom_ab=null) | 暂无 | 3061M系列解决方案 128KB QFP48 | +| V5A20002NOOJLB | [购买链接](https://www.ickey.cn/detail/1003001016603886/V5A20002NOOJLB.html#208f8e9e-5cdf-4353-988d-d1df20bbb33f) | [购买链接](https://www.ickey.cn/detail/1003001021937018/V5A20002NOOJLB.html#a48f53a5-b3cf-4d3e-8254-e91505f16fae) | 3061M系列解决方案 128KB QFN32 | +| V5A20002NOQJLB | [购买链接](https://www.ickey.cn/detail/1003001016603885/V5A20002NOQJLB.html#208f8e9e-5cdf-4353-988d-d1df20bbb33f) | [购买链接](https://www.ickey.cn/detail/1003001017510002/V5A20002NOQJLB.html#a48f53a5-b3cf-4d3e-8254-e91505f16fae) | 3061M系列解决方案 128KB QFP32 | +| V5A20002NOOJDB | [购买链接](https://www.ickey.cn/detail/1003001016603884/V5A20002NOOJDB.html#208f8e9e-5cdf-4353-988d-d1df20bbb33f) | 暂无 | 3061M系列解决方案 128KB QFN48 | +| V5A20002NOOJD9 | [购买链接](https://www.ickey.cn/detail/1003001016603888/V5A20002NOOJD9.html#d9fd2c53-3b07-4b62-ba26-cb59ae94234f) | [购买链接](https://www.ickey.cn/detail/1003001021937055/V5A20002NOOJD9.html#a48f53a5-b3cf-4d3e-8254-e91505f16fae) | 3061M系列解决方案 64KB QFN48 | +| V5A20002NOQJD9 | [购买链接](https://www.ickey.cn/detail/1003001016603887/V5A20002NOQJD9.html#208f8e9e-5cdf-4353-988d-d1df20bbb33f) | 暂无 | 3061M系列解决方案 64KB QFP48 | +| V5A20002NOQJL9 | [购买链接](https://www.ickey.cn/detail/1003001016603889/V5A20002NOQJL9.html#208f8e9e-5cdf-4353-988d-d1df20bbb33f) | [购买链接](https://www.ickey.cn/detail/1003001021936978/V5A20002NOQJL9.html#a48f53a5-b3cf-4d3e-8254-e91505f16fae) | 3061M系列解决方案 64KB QFP32 | +| V5A20002NOOJL9 | [购买链接](https://www.ickey.cn/detail/1003001016603890/V5A20002NOOJL9.html#208f8e9e-5cdf-4353-988d-d1df20bbb33f) | [购买链接](https://www.ickey.cn/detail/1003001021936989/V5A20002NOOJL9.html#a48f53a5-b3cf-4d3e-8254-e91505f16fae) | 3061M系列解决方案 64KB QFN32 | +| V5A20001ISQJSA | [购买链接](https://www.ickey.cn/detail/1003001016603882/V5A20001ISQJSA.html#) | 暂无 | 3065H系列解决方案 160KB LQFP64 | -- Gitee