file_path
stringlengths
20
207
content
stringlengths
5
3.85M
size
int64
5
3.85M
lang
stringclasses
9 values
avg_line_length
float64
1.33
100
max_line_length
int64
4
993
alphanum_fraction
float64
0.26
0.93
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/Middlewares/ST/AI/Inc/lite_conv2d_dqnn.h
/** ****************************************************************************** * @file lite_conv2d_dqnn.h * @author AIS * @brief header file of AI platform lite conv kernel datatypes ****************************************************************************** * @attention * * Copyright (c) 2021 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** @verbatim @endverbatim ****************************************************************************** */ #ifndef LITE_CONV2D_DQNN_H #define LITE_CONV2D_DQNN_H #pragma once #include "ai_lite_interface.h" # define AI_16_OVERFLOW_CHECK(val_) (val_ <= 32767) /******************************************************************************/ /* Forward Functions Section */ /******************************************************************************/ AI_API_DECLARE_BEGIN /*! * @brief Handles 2D convolution with binary input, binary output and * binary weights - with 0 padding (QKeras like) - Lite I/F * @ingroup lite_conv2d_dqnn */ LITE_API_ENTRY void forward_lite_conv2d_is1os1ws1_bn_pad0(const ai_u32 *pDataIn_init, ai_u32 *pDataOut_init, const ai_u32 *pWeights_init, ai_float *pScratch_32, const ai_u32 n_channel_in, const ai_u32 n_channel_out, const ai_i32 width_in, const ai_i32 height_in, const ai_i32 width_out, const ai_i32 height_out, const ai_i32 filt_width, const ai_i32 filt_height, const ai_i32 filt_pad_x, const ai_i32 filt_pad_y, const ai_i32 filt_stride_x, const ai_i32 filt_stride_y, const ai_i32 *pThreshold); /*! * @brief Handles 2D convolution with binary input, binary output and * binary weights - with 0 padding (QKeras like) - Lite I/F * - Optimized thanks to Optim0 assumptions * @ingroup lite_conv2d_dqnn */ LITE_API_ENTRY void forward_lite_conv2d_is1os1ws1_bn_pad0_optim0(const ai_u32 *pDataIn_init, ai_u32 *pDataOut_init, const ai_u32 *pWeights_init, ai_float *pScratch_32, const ai_u32 n_channel_in, const ai_u32 n_channel_out, const ai_i32 width_in, const ai_i32 height_in, const ai_i32 width_out, const ai_i32 height_out, const ai_i32 filt_width, const ai_i32 filt_height, const ai_i32 filt_pad_x, const ai_i32 filt_pad_y, const ai_i32 filt_stride_x, const ai_i32 filt_stride_y, const ai_i32 *pThreshold); /*! * @brief Handles 2D convolution with binary input, 8-bits output and * binary weights - with 0 padding (QKeras like) - Lite I/F * @ingroup lite_conv2d_dqnn */ LITE_API_ENTRY void forward_lite_conv2d_is1os8ws1_bn_pad0(const ai_u32 *pDataIn_init, ai_i8 *pDataOut_init, const ai_u32 *pWeights_init, ai_float *pScratch_32, const ai_u32 n_channel_in, const ai_u32 n_channel_out, const ai_i32 width_in, const ai_i32 height_in, const ai_i32 width_out, const ai_i32 height_out, const ai_i32 filt_width, const ai_i32 filt_height, const ai_i32 filt_pad_x, const ai_i32 filt_pad_y, const ai_i32 filt_stride_x, const ai_i32 filt_stride_y, const ai_float *pScale, const ai_float *pOffset); /*! * @brief Handles 2D convolution with binary input, binary output and * binary weights - with +1/-1 padding (Larq like) - Lite I/F * @ingroup lite_conv2d_dqnn */ LITE_API_ENTRY void forward_lite_conv2d_is1os1ws1_bn_pad1(const ai_u32 *pDataIn_init, ai_u32 *pDataOut_init, const ai_u32 *pWeights_init, ai_float *pScratch_32, const ai_u32 n_channel_in, const ai_u32 n_channel_out, const ai_i32 width_in, const ai_i32 height_in, const ai_i32 width_out, const ai_i32 height_out, const ai_i32 filt_width, const ai_i32 filt_height, const ai_i32 filt_pad_x, const ai_i32 filt_pad_y, const ai_i32 filt_stride_x, const ai_i32 filt_stride_y, const ai_i32 *pThreshold, const ai_i32 pad_value); /*! * @brief Handles 2D convolution with binary input, binary output and * binary weights - with +1/-1 padding (Larq like) - Lite I/F * - Optimized thanks to Optim2 assumptions * @ingroup lite_conv2d_dqnn */ LITE_API_ENTRY void forward_lite_conv2d_is1os1ws1_bn_pad1_optim2(const ai_u32 *pDataIn_init, ai_u32 *pDataOut_init, const ai_u32 *pWeights_init, ai_float *pScratch_32, const ai_u32 n_channel_in, const ai_u32 n_channel_out, const ai_i32 width_in, const ai_i32 height_in, const ai_i32 width_out, const ai_i32 height_out, const ai_i32 filt_width, const ai_i32 filt_height, const ai_i32 filt_pad_x, const ai_i32 filt_pad_y, const ai_i32 filt_stride_x, const ai_i32 filt_stride_y, const ai_i32 *pThreshold, const ai_i32 pad_value); /*! * @brief Handles 2D convolution with binary input, 8-bits output and * binary weights - with +1/-1 padding (Larq like) - Lite I/F * @ingroup lite_conv2d_dqnn */ LITE_API_ENTRY void forward_lite_conv2d_is1os8ws1_bn_pad1(const ai_u32 *pDataIn_init, ai_i8 *pDataOut_init, const ai_u32 *pWeights_init, ai_float *pScratch_32, const ai_u32 n_channel_in, const ai_u32 n_channel_out, const ai_i32 width_in, const ai_i32 height_in, const ai_i32 width_out, const ai_i32 height_out, const ai_i32 filt_width, const ai_i32 filt_height, const ai_i32 filt_pad_x, const ai_i32 filt_pad_y, const ai_i32 filt_stride_x, const ai_i32 filt_stride_y, const ai_float *pScale, const ai_float *pOffset, const ai_i32 pad_value); /*! * @brief Handles 2D convolution with binary input, 8-bits output and * binary weights - with +1/-1 padding (Larq like) - Lite I/F * - Optimized thanks to Optim1 assumptions * @ingroup lite_conv2d_dqnn */ LITE_API_ENTRY void forward_lite_conv2d_is1os8ws1_bn_pad1_optim1(const ai_u32 *pDataIn_init, ai_i8 *pDataOut_init, const ai_u32 *pWeights_init, ai_float *pScratch_32, const ai_u32 n_channel_in, const ai_u32 n_channel_out, const ai_i32 width_in, const ai_i32 height_in, const ai_i32 width_out, const ai_i32 height_out, const ai_i32 filt_width, const ai_i32 filt_height, const ai_i32 filt_pad_x, const ai_i32 filt_pad_y, const ai_i32 filt_stride_x, const ai_i32 filt_stride_y, const ai_float *pScale, const ai_float *pOffset, const ai_i32 pad_value); /** * @brief Handles 2D convolution with binary input, fixed point 16-bits output and * binary weights - with 0 padding (QKeras like) - Lite I/F * @ingroup lite_conv2d_dqnn */ LITE_API_ENTRY void forward_lite_conv2d_is1os16ws1_bn_pad0_fxp(const ai_u32 *pDataIn_init, ai_i16 *pDataOut_init, const ai_u32 *pWeights_init, ai_float *pScratch_32, const ai_u32 n_channel_in, const ai_u32 n_channel_out, const ai_i32 width_in, const ai_i32 height_in, const ai_i32 width_out, const ai_i32 height_out, const ai_i32 filt_width, const ai_i32 filt_height, const ai_i32 filt_pad_x, const ai_i32 filt_pad_y, const ai_i32 filt_stride_x, const ai_i32 filt_stride_y, const ai_float *pScale_init, const ai_float *pOffset_init); /*! * @brief Handles 2D convolution with binary input, fixed point 16-bits output and * binary weights - with +1/-1 padding (Larq like) - Lite I/F * * @ingroup lite_conv2d_dqnn */ LITE_API_ENTRY void forward_lite_conv2d_is1os16ws1_bn_pad1_fxp(const ai_u32 *pDataIn_init, ai_i16 *pDataOut_init, const ai_u32 *pWeights_init, ai_float *pScratch_32, const ai_u32 n_channel_in, const ai_u32 n_channel_out, const ai_i32 width_in, const ai_i32 height_in, const ai_i32 width_out, const ai_i32 height_out, const ai_i32 filt_width, const ai_i32 filt_height, const ai_i32 filt_pad_x, const ai_i32 filt_pad_y, const ai_i32 filt_stride_x, const ai_i32 filt_stride_y, const ai_float *pScale_init, const ai_float *pOffset_init, const ai_i32 pad_value); /*! * @brief Handles 2D convolution with binary input, fixed point 16-bits output and * binary weights - with +1/-1 padding (Larq like) - Lite I/F * - Optimized thanks to Optim1 assumptions * * @ingroup lite_conv2d_dqnn */ LITE_API_ENTRY void forward_lite_conv2d_is1os16ws1_bn_pad1_optim1_fxp(const ai_u32 *pDataIn_init, ai_i16 *pDataOut_init, const ai_u32 *pWeights_init, ai_float *pScratch_32, const ai_u32 n_channel_in, const ai_u32 n_channel_out, const ai_i32 width_in, const ai_i32 height_in, const ai_i32 width_out, const ai_i32 height_out, const ai_i32 filt_width, const ai_i32 filt_height, const ai_i32 filt_pad_x, const ai_i32 filt_pad_y, const ai_i32 filt_stride_x, const ai_i32 filt_stride_y, const ai_float *pScale_init, const ai_float *pOffset_init, const ai_i32 pad_value); /** * @brief Handles 2D convolution with binary input, fixed point 16-bits unsigned output and * binary weights - with 0 padding (QKeras like) - Lite I/F * * @ingroup lite_conv2d_dqnn */ LITE_API_ENTRY void forward_lite_conv2d_is1ou16ws1_bn_pad1_fxp(const ai_u32 *pDataIn_init, ai_u16 *pDataOut_init, const ai_u32 *pWeights_init, ai_float *pScratch_32, const ai_u32 n_channel_in, const ai_u32 n_channel_out, const ai_i32 width_in, const ai_i32 height_in, const ai_i32 width_out, const ai_i32 height_out, const ai_i32 filt_width, const ai_i32 filt_height, const ai_i32 filt_pad_x, const ai_i32 filt_pad_y, const ai_i32 filt_stride_x, const ai_i32 filt_stride_y, const ai_float *pScale_init, const ai_float *pOffset_init, const ai_i32 pad_value); /*! * @brief Handles 2D convolution with binary input, fixed point 16-bits unsigned output and * binary weights - with +1/-1 padding (Larq like) - Lite I/F * * @ingroup lite_conv2d_dqnn */ LITE_API_ENTRY void forward_lite_conv2d_is1ou16ws1_bn_pad0_fxp(const ai_u32 *pDataIn_init, ai_u16 *pDataOut_init, const ai_u32 *pWeights_init, ai_float *pScratch_32, const ai_u32 n_channel_in, const ai_u32 n_channel_out, const ai_i32 width_in, const ai_i32 height_in, const ai_i32 width_out, const ai_i32 height_out, const ai_i32 filt_width, const ai_i32 filt_height, const ai_i32 filt_pad_x, const ai_i32 filt_pad_y, const ai_i32 filt_stride_x, const ai_i32 filt_stride_y, const ai_float *pScale_init, const ai_float *pOffset_init); /*! * @brief Handles 2D convolution with binary input, fixed point 16-bits unsigned output and * binary weights - with +1/-1 padding (Larq like) - Lite I/F. * - Optimized thanks to Optim1 assumptions * * @ingroup lite_conv2d_dqnn */ LITE_API_ENTRY void forward_lite_conv2d_is1ou16ws1_bn_pad1_optim1_fxp(const ai_u32 *pDataIn_init, ai_u16 *pDataOut_init, const ai_u32 *pWeights_init, ai_float *pScratch_32, const ai_u32 n_channel_in, const ai_u32 n_channel_out, const ai_i32 width_in, const ai_i32 height_in, const ai_i32 width_out, const ai_i32 height_out, const ai_i32 filt_width, const ai_i32 filt_height, const ai_i32 filt_pad_x, const ai_i32 filt_pad_y, const ai_i32 filt_stride_x, const ai_i32 filt_stride_y, const ai_float *pScale_init, const ai_float *pOffset_init, const ai_i32 pad_value); /*! * @brief Handles 2D convolution with 8-bits quantized Input and weights and * binary output - Lite I/F * @ingroup lite_conv2d_dqnn * @param layer conv2d_dqnn layer */ LITE_API_ENTRY void forward_lite_conv2d_is8os1ws8(const ai_i8 *pDataIn_init, ai_u32 *pDataOut_init, const ai_i8 *pWeights_init, ai_float *pScratch_32, const ai_u32 n_channel_in, const ai_u32 n_channel_out, const ai_i32 width_in, const ai_i32 height_in, const ai_i32 width_out, const ai_i32 height_out, const ai_i32 filt_width, const ai_i32 filt_height, const ai_i32 filt_pad_x, const ai_i32 filt_pad_y, const ai_i32 filt_stride_x, const ai_i32 filt_stride_y, const ai_i32 *pThreshold, const ai_i8 in_zeropoint); /*! * @brief Handles 2D convolution with 8-bits quantized Input and weights and * binary output - Lite I/F - Optimized thanks to Optim2 assumptions * @ingroup lite_conv2d_dqnn * @param layer conv2d_dqnn layer */ LITE_API_ENTRY void forward_lite_conv2d_is8os1ws8_optim2(const ai_i8 *pDataIn_init, ai_u32 *pDataOut_init, const ai_i8 *pWeights_init, ai_float *pScratch_32, const ai_u32 n_channel_in, const ai_u32 n_channel_out, const ai_i32 width_in, const ai_i32 height_in, const ai_i32 width_out, const ai_i32 height_out, const ai_i32 filt_width, const ai_i32 filt_height, const ai_i32 filt_pad_x, const ai_i32 filt_pad_y, const ai_i32 filt_stride_x, const ai_i32 filt_stride_y, const ai_i32 *pThreshold, const ai_i8 in_zeropoint); /*! * @brief Handles 2D convolution with 8-bits quantized Input and weights and * binary output - quantized with DoReFa SotA quantizer, lite I/F * @ingroup lite_conv2d_dqnn */ LITE_API_ENTRY void forward_lite_conv2d_dorefa_is8os1ws8(const ai_i8 *pDataIn_init, ai_u32 *pDataOut_init, const ai_u8 *pWeights_init, ai_float *pScratch_32, const ai_u32 n_channel_in, const ai_u32 n_channel_out, const ai_i32 width_in, const ai_i32 height_in, const ai_i32 width_out, const ai_i32 height_out, const ai_i32 filt_width, const ai_i32 filt_height, const ai_i32 filt_pad_x, const ai_i32 filt_pad_y, const ai_i32 filt_stride_x, const ai_i32 filt_stride_y, const ai_i32 *pThreshold, const ai_i8 in_zeropoint); /*! * @brief Handles 2D convolution with 8-bits quantized input, output and weights * - quantized with with different quantization for channel * @ingroup lite_conv2d_dqnn */ LITE_API_ENTRY void forward_lite_conv2d_is8os8ws8_sssa_ch(const ai_i8 *pData_in, ai_i8 *pData_out, const ai_i8 *pWeights, const ai_i32 *pBias, ai_u16 *pBuffer_a, const ai_size width_in, const ai_size height_in, const ai_size width_out, const ai_size height_out, const ai_u16 n_channel_in, const ai_u16 n_channel_out, const ai_size filt_width, const ai_size filt_height, const ai_u16 filt_pad_x, const ai_u16 filt_pad_y, const ai_u16 filt_stride_x, const ai_u16 filt_stride_y, const ai_u16 dilation_x, const ai_u16 dilation_y, const ai_float in_scale, const ai_float out_scale, const ai_float *pWt_scale, const ai_i8 in_zeropoint, const ai_i8 out_zeropoint, const ai_i32 scratch_size); /*! * @brief Handles 2D convolution with 16-bits quantized inputs, binary outputs and binary weights - Lite I/F. * Vanilla version. * @ingroup lite_conv2d_dqnn * @param layer conv2d_dqnn layer */ LITE_API_ENTRY void forward_lite_conv2d_is16os1ws1_bn_fxp(const ai_i16 *pIn, ai_u32 *pOut_32, const ai_u32 *pWeights, const ai_i32 *pThreshold, ai_i8 *pBufferA, const ai_i32 dim_kernel, const ai_i16 dim_im_in_x, const ai_i16 dim_im_in_y, const ai_i16 dim_im_out_x, const ai_i16 dim_im_out_y, const ai_i16 ch_im_in, const ai_i16 ch_im_out, const ai_i16 dim_kernel_x, const ai_i16 dim_kernel_y, const ai_i16 padding_x, const ai_i16 padding_y, const ai_i16 stride_x, const ai_i16 stride_y, const ai_i16 dilation_x, const ai_i16 dilation_y, const ai_i16 in_zeropoint); /** * @brief Handles 2D convolution with 16-bits quantized inputs, 16-bits quantized outputs and binary weights - Lite I/F * * @ingroup lite_conv2d_dqnn */ LITE_API_ENTRY void forward_lite_conv2d_is16os16ws1_fxp(const ai_i16 *pIn, ai_i16 *pOut, const ai_u32 *pWeights, ai_i8 *pBufferA, const ai_i16 dim_im_in_x, const ai_i16 dim_im_in_y, const ai_i16 dim_im_out_x, const ai_i16 dim_im_out_y, const ai_i16 ch_im_in, const ai_i16 ch_im_out, const ai_u32 dim_kernel, const ai_i16 dim_kernel_x, const ai_i16 dim_kernel_y, const ai_i16 padding_x, const ai_i16 padding_y, const ai_i16 stride_x, const ai_i16 stride_y, const ai_i16 dilation_x, const ai_i16 dilation_y, const ai_i16 in_zeropoint); AI_API_DECLARE_END #endif /*LITE_CONV2D_DQNN_H*/
30,870
C
55.333942
119
0.351085
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/Middlewares/ST/AI/Inc/core_private.h
/** ****************************************************************************** * @file core_private.h * @author AST Embedded Analytics Research Platform * @brief private header file of common private core module defines ****************************************************************************** * @attention * * Copyright (c) 2019 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** @verbatim @endverbatim ****************************************************************************** */ #ifndef CORE_PRIVATE_H #define CORE_PRIVATE_H #pragma once #include "ai_math_helpers.h" #include "ai_datatypes_internal.h" #include "core_log.h" /*! * @defgroup core_private Core Library Private macros and datatypes * @brief Common macros, datatypes and routines for core private rounites * @details This module contains the definitons and implementations of some * internal routines and datatypes that are supposed to not be exposed as * public headers. So usually this file should be include only on .c files or * headers that are private as well */ /*** Foreground Colors ****************************************************/ #define CORE_COLOR_BLACK "\x1b[30m" #define CORE_COLOR_RED "\x1b[31m" #define CORE_COLOR_GREEN "\x1b[32m" #define CORE_COLOR_YELLOW "\x1b[33m" #define CORE_COLOR_BLUE "\x1b[94m" #define CORE_COLOR_MAGENTA "\x1b[35m" #define CORE_COLOR_CYAN "\x1b[36m" #define CORE_COLOR_WHYTE "\x1b[37m" #define CORE_COLOR_DEFAULT "\x1b[39m" #define CORE_COLOR_LGRAY "\x1b[90m" #define CORE_COLOR_LRED "\x1b[91m" #define CORE_COLOR_LGREEN "\x1b[92m" #define CORE_COLOR_LYELLOW "\x1b[93m" #define CORE_COLOR_LBLUE "\x1b[94m" #define CORE_COLOR_LMAGENTA "\x1b[95m" #define CORE_COLOR_LCYAN "\x1b[96m" #define CORE_COLOR_LWHITE "\x1b[97m" /*** Text Attributes Colors *********************************************/ #define CORE_COLOR_OFF "\x1b[0m" #define CORE_COLOR_BOLD "\x1b[1m" #define CORE_COLOR_UNDERLINE "\x1b[4m" #define CORE_COLOR_BLINK "\x1b[5m" #define CORE_COLOR_BOLD_OFF "\x1b[21m" #define CORE_COLOR_UNDERLINE_OFF "\x1b[24m" #define CORE_COLOR_BLINK_OFF "\x1b[25m" /*** Background Colors ****************************************************/ #define CORE_COLOR_BG_BLACK "\x1b[40m" #define CORE_COLOR_BG_RED "\x1b[41m" #define CORE_COLOR_BG_GREEN "\x1b[42m" #define CORE_COLOR_BG_YELLOW "\x1b[43m" #define CORE_COLOR_BG_BLUE "\x1b[44m" #define CORE_COLOR_BG_MAGENTA "\x1b[45m" #define CORE_COLOR_BG_CYAN "\x1b[46m" #define CORE_COLOR_BG_WHITE "\x1b[47m" #define CORE_COLOR_BG_DEFAULT "\x1b[49m" #define CORE_COLOR_BG_LGRAY "\x1b[100m" #define CORE_COLOR_BG_LRED "\x1b[101m" #define CORE_COLOR_BG_LGREEN "\x1b[102m" #define CORE_COLOR_BG_LYELLOW "\x1b[103m" #define CORE_COLOR_BG_LBLUE "\x1b[104m" #define CORE_COLOR_BG_LMAGENTA "\x1b[105m" #define CORE_COLOR_BG_LCYAN "\x1b[106m" #define CORE_COLOR_BG_LWHITE "\x1b[107m" /*****************************************************************************/ #define CORE_ADDRESS_RANGE_INIT(start_, end_) \ core_address_range_init(start_, end_) #define CORE_GET_BUFFER_META_INFO(meta_info_, tensor_ptr_) \ core_get_buffer_meta_info(meta_info_, tensor_ptr_) #define CORE_ADDRESS_RANGE_END(range_) \ ( (ai_ptr)(((range_)->start)+((range_)->size)) ) #define CORE_ADDRESS_RANGE_OVERLAP(overlap_) \ ( ((overlap_)->start) && (((overlap_)->size)>0) ) #define CORE_ADDRESS_RANGE_OVERLAP_PARTIAL(overlap_, ref_) \ ( ((overlap_)->start) && (((overlap_)->size)<((ref_)->size)) ) #define CORE_MEMORY_OVERLAP_INIT(partial_, range_, chain_id_, tensor_id_) { \ .partial = (partial_), .range = AI_PACK(range_), \ .chain_id = (chain_id_), .tensor_id = (tensor_id_) \ } #define CORE_OFFSET(offset_, max_) \ ((ai_i32)(((offset_)<0) ? AI_MAX((max_) - (offset_), 0) : AI_MIN(offset_, max_))) /*****************************************************************************/ /** Network Context Handlers **/ /*****************************************************************************/ /*****************************************************************************/ /** Network Tensors Handlers **/ /*****************************************************************************/ #define AI_TENSOR_HAS_INTQ_INFO \ AI_BUFFER_META_HAS_INTQ_INFO #define CORE_TENSOR_GET_SHAPE_SIZE(tensor_) \ ai_shape_get_size(AI_TENSOR_SHAPE(tensor_)) #define CORE_ASSERT_SHAPE_MATCH(x, y) \ do { \ AI_ASSERT(AI_SHAPE_H(y) == 1 || AI_SHAPE_H(x)==1 || AI_SHAPE_H(y)==AI_SHAPE_H(x)) \ AI_ASSERT(AI_SHAPE_W(y) == 1 || AI_SHAPE_W(x)==1 || AI_SHAPE_W(y)==AI_SHAPE_W(x)) \ AI_ASSERT(AI_SHAPE_D(y) == 1 || AI_SHAPE_D(x)==1 || AI_SHAPE_D(y)==AI_SHAPE_D(x)) \ AI_ASSERT(AI_SHAPE_E(y) == 1 || AI_SHAPE_E(x)==1 || AI_SHAPE_E(y)==AI_SHAPE_E(x)) \ AI_ASSERT(AI_SHAPE_CH(y) == 1 || AI_SHAPE_CH(x)==1|| AI_SHAPE_CH(y)==AI_SHAPE_CH(x)) \ AI_ASSERT(AI_SHAPE_IN_CH(y) == 1 || AI_SHAPE_IN_CH(x)==1|| AI_SHAPE_IN_CH(y)==AI_SHAPE_IN_CH(x)) \ } while(0); #define AI_TENSOR_ARRAY_BYTE_SIZE(t_) \ AI_ARRAY_OBJ_BYTE_SIZE(AI_ARRAY_OBJ(t_->data)) #define AI_TENSOR_ARRAY_GET_DATA_ADDR(t_) \ AI_HANDLE_PTR(AI_ARRAY_OBJ_DATA_START(t_->data, void)) #define AI_TENSOR_ARRAY_UPDATE_DATA_ADDR(t_, addr_) \ { ai_array *arr_ = AI_ARRAY_OBJ(t_->data); \ const uintptr_t off_ = (uintptr_t)arr_->data - (uintptr_t)arr_->data_start; \ arr_->data_start = AI_PTR(addr_); \ arr_->data = AI_PTR((uintptr_t)addr_ + off_); \ } #define AI_TENSOR_INTEGER_GET_SIZE(t_) \ ((t_->klass) ? (AI_KLASS_GET_INTQ_INFO_LIST(t_))->size : 0) #define AI_TENSOR_INTEGER_GET_SCALE(t_, idx_) \ AI_INTQ_INFO_LIST_SCALE(AI_KLASS_GET_INTQ_INFO_LIST(t_), ai_float, idx_) #define AI_TENSOR_INTEGER_GET_ZEROPOINT_I8(t_, idx_) \ AI_INTQ_INFO_LIST_ZEROPOINT(AI_KLASS_GET_INTQ_INFO_LIST(t_), ai_i8, idx_) #define AI_TENSOR_INTEGER_GET_ZEROPOINT_U8(t_, idx_) \ AI_INTQ_INFO_LIST_ZEROPOINT(AI_KLASS_GET_INTQ_INFO_LIST(t_), ai_u8, idx_) #define AI_TENSOR_FMT_GET_SIGN(t_) \ AI_BUFFER_FMT_GET_SIGN(AI_ARRAY_OBJ(t_->data)->format) #define AI_TENSOR_FMT_GET_BITS(t_) \ AI_BUFFER_FMT_GET_BITS(AI_ARRAY_OBJ(t_->data)->format) #define AI_TENSOR_FMT_GET_FBITS(t_) \ AI_BUFFER_FMT_GET_FBITS(AI_ARRAY_OBJ(t_->data)->format) #define AI_TENSOR_FMT_GET_TYPE(t_) \ AI_BUFFER_FMT_GET_TYPE(AI_ARRAY_OBJ(t_->data)->format) #define AI_TENSOR_GET_FMT(t_) \ (AI_ARRAY_OBJ(t_->data)->format) /*****************************************************************************/ /** Network Buffers Handlers **/ /*****************************************************************************/ #define AI_FOR_EACH_BUFFER_ARRAY_ITEM(buffer_ptr_, buffer_array_ptr_, start_pos_, end_pos_) \ ai_buffer* buffer_ptr_ = AI_BUFFER_ARRAY_ITEM(buffer_array_ptr_, \ CORE_OFFSET(end_pos_, AI_BUFFER_ARRAY_SIZE(buffer_array_ptr_))); \ for ( ; buffer_ptr_ && AI_BUFFER_ARRAY_SIZE(buffer_array_ptr_) && \ (buffer_ptr_>=AI_BUFFER_ARRAY_ITEM(buffer_array_ptr_, \ CORE_OFFSET(start_pos_, AI_BUFFER_ARRAY_SIZE(buffer_array_ptr_)))); buffer_ptr_--) /*****************************************************************************/ /** Network Arrays Handlers **/ /*****************************************************************************/ #define AI_ARRAY_OBJ_FMT(array_) \ AI_CAST(ai_array_format, AI_ARRAY_OBJ(array_)->format) #define AI_ARRAY_OBJ_SIZE(array_) \ (AI_ARRAY_OBJ(array_)->size) #define AI_ARRAY_OBJ_BYTE_SIZE(array_) \ AI_SIZE(AI_ARRAY_GET_BYTE_SIZE(AI_ARRAY_OBJ_FMT(array_), \ AI_ARRAY_OBJ_SIZE(array_))) #define AI_ARRAY_OBJ_DATA_SIZE(array_) \ AI_ARRAY_GET_DATA_BYTE_SIZE(AI_ARRAY_OBJ_FMT(array_), \ AI_ARRAY_OBJ_SIZE(array_)) #define AI_ARRAY_OBJ_DATA(array_, type_) \ AI_CAST(type_*, AI_ARRAY_OBJ(array_)->data) #define AI_ARRAY_OBJ_DATA_START(array_, type_) \ AI_CAST(type_*, AI_ARRAY_OBJ(array_)->data_start) #define AI_ARRAY_OBJ_ELEM(array_, type_, pos_) \ AI_ARRAY_OBJ_DATA(array_, type_)[(pos_)] /*****************************************************************************/ /** Network Tensors Chains / Lists Handlers **/ /*****************************************************************************/ #define SET_TENSOR_IN(chain_, pos_) \ (GET_TENSOR_LIST_IN(chain_)->tensor[(pos_)]) #define SET_TENSOR_OUT(chain_, pos_) \ (GET_TENSOR_LIST_OUT(chain_)->tensor[(pos_)]) #define AI_NODE_IO_GET(node_, in_, out_) \ ASSERT_NODE_SANITY(node_) \ ai_tensor* in_ = GET_TENSOR_IN((node_)->tensors, 0); \ ai_tensor* out_ = GET_TENSOR_OUT((node_)->tensors, 0); \ ASSERT_TENSOR_SANITY(in_) \ ASSERT_TENSOR_SANITY(out_) /*****************************************************************************/ #define AI_BITS_TO_BYTES(bits_) \ (((bits_)+0x7) >> 3) #define AI_BYTES_TO_BITS(bytes_) \ ((bytes_) << 3) /*****************************************************************************/ /** Network Nodes Handlers **/ /*****************************************************************************/ #define AI_NODE_IS_FIRST(node) \ (AI_NODE_OBJ(node)==AI_NODE_OBJ(AI_NODE_OBJ(node)->network->input_node)) #define AI_NODE_IS_LAST(node_) \ ((AI_NODE_OBJ(node_)==AI_NODE_OBJ(node_)->next) || \ (AI_NODE_OBJ(node_)->next==NULL)) #define AI_FOR_EACH_NODE_DO(node_, nodes_) \ for (ai_node* node_ = AI_NODE_OBJ(nodes_); (node_); \ node_ = ((AI_NODE_IS_LAST(node_)) ? NULL : (node_)->next)) /*****************************************************************************/ typedef struct { ai_ptr start; ai_size size; } ai_address_range; typedef struct { ai_address_range range; ai_u16 chain_id; ai_u16 tensor_id; ai_bool partial; } ai_memory_overlap; /*****************************************************************************/ AI_DECLARE_STATIC ai_address_range core_address_range_init( const ai_handle start, const ai_handle end) { ai_address_range r; r.start = (start<end) ? start : end; r.size = (ai_size) ((start<end) ? ((ai_uptr)end-(ai_uptr)start) : ((ai_uptr)start-(ai_uptr)end)); return r; } AI_DECLARE_STATIC ai_buffer_meta_info* core_get_buffer_meta_info( ai_buffer_meta_info* meta, const ai_tensor* t) { if (!meta) return NULL; AI_ASSERT(t && t->data) ai_bool ok; meta->flags = 0x0; meta->intq_info = AI_KLASS_GET_INTQ_INFO_LIST(t); ok = (meta->intq_info && (meta->intq_info->size>0)); meta->flags |= (ok) ? AI_BUFFER_META_HAS_INTQ_INFO : 0x0; return (ok) ? meta : NULL; } #if 0 #include <stdio.h> #include <stdarg.h> AI_DECLARE_STATIC void _dump_file_print( const char* fname, const char* fmt, ...) { static FILE* fp = NULL; if (fname) { if (!fp) { fp = fopen(fname, "a"); } } if (fp) { va_list args; va_start(args, fmt); vfprintf(fp, fmt, args); va_end(args); fflush(fp); } } AI_DECLARE_STATIC void _dump_bytearray( const char* fname, const ai_handle src, const ai_size src_size, const ai_u8 src_id, const char* name) { static FILE* fp = NULL; if (fname && src && (src_size>0)) { if (!fp) { fp = fopen(fname, "a"); } } if (fp) { switch (src_id) { case 1: { const ai_float* src_value = (const ai_float*)src; fprintf(fp, "ai_float %s[%u] = {%f", name, src_size, src_value[0]); for (ai_size i=1; i<src_size; i++) { fprintf(fp, ", %f", src_value[i]); } } break; case 2: { const ai_i8* src_value = (const ai_i8*)src; fprintf(fp, "ai_i8 %s[%u] = {%d", name, src_size, src_value[0]); for (ai_size i=1; i<src_size; i++) { fprintf(fp, ", %d", src_value[i]); } } break; case 3: { const ai_u8* src_value = (const ai_u8*)src; fprintf(fp, "ai_u8 %s[%u] = {%u", name, src_size, src_value[0]); for (ai_size i=1; i<src_size; i++) { fprintf(fp, ", %u", src_value[i]); } } break; default: fprintf(fp, "format not supported: %u {", src_id); break; } fprintf(fp, "};\n"); fflush(fp); } } #endif #endif /* CORE_PRIVATE_H */
13,110
C
34.822404
114
0.510297
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/Middlewares/ST/AI/Inc/lite_operators.h
#ifndef LITE_OPERATORS_H #define LITE_OPERATORS_H #pragma once #include "lite_bn_f32.h" #include "lite_bn_integer.h" #include "lite_conv2d.h" #include "lite_conv2d_dqnn.h" #include "lite_convert_dqnn.h" #include "lite_dense_if32.h" #include "lite_dense_is1.h" #include "lite_dense_is1ws1.h" #include "lite_dense_ws1.h" #include "lite_gru_f32.h" #include "lite_dw_dqnn.h" #include "lite_pw_dqnn.h" #include "lite_dense_is8os8ws8.h" #include "lite_generic_float.h" #include "lite_pool_f32.h" #include "lite_maxpool_dqnn.h" #include "lite_nl_generic_float.h" #include "lite_nl_generic_integer.h" #include "lite_pad_generic.h" #include "lite_pad_dqnn.h" #include "lite_upsample_generic.h" #endif /* LITE_OPERATORS_H */
718
C
23.793103
36
0.727019
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/Middlewares/ST/AI/Inc/layers_norm.h
/** ****************************************************************************** * @file layers_norm.h * @author AST Embedded Analytics Research Platform * @brief header file of AI platform normalization layers datatypes ****************************************************************************** * @attention * * Copyright (c) 2018 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** @verbatim @endverbatim ****************************************************************************** */ #ifndef LAYERS_NORM_H #define LAYERS_NORM_H #pragma once #include "layers_common.h" /*! * @defgroup layers_norm Normalization Layers Definitions * @brief definition * */ AI_API_DECLARE_BEGIN /*! * @struct ai_layer_bn * @ingroup layers_norm * @brief Batch normalization (scale with bias) layer */ typedef ai_layer_base ai_layer_bn; /*! * @struct ai_layer_lrn * @ingroup layers_norm * @brief Local Response Normalization layer * * Divides each element by a scale factor computed */ typedef AI_ALIGNED_TYPE(struct, 4) ai_layer_lrn_ { AI_LAYER_COMMON_FIELDS_DECLARE ai_u32 local_size; /*!< size of the normalization window */ ai_float k; /*!< bias term */ ai_float alpha; /*!< input scale */ ai_float beta; /*!< scale exponent */ } ai_layer_lrn; /*! * @enum ai_norm_type_e * @ingroup layers_norm * @brief store the type of normalization algorithm to apply */ typedef enum ai_norm_type_ { NONE = 0, L1 = 1, L2 = 2, MAX = 3, } ai_norm_type_e; /*! * @struct ai_layer_norm * @ingroup layers_norm * @brief Lp Normalization layer * * Normalizes the tensor along the 'axis' direction using the Lp norm. * Optionally divides the result by the number of the elements. */ typedef AI_ALIGNED_TYPE(struct, 4) ai_layer_norm_ { AI_LAYER_COMMON_FIELDS_DECLARE ai_shape_idx axis; /*! normalization axis */ ai_float exponent; /*!< normalization exponent p */ ai_bool scale; /*!< multiplies by the pth root of the number of elements */ ai_norm_type_e norm_type; } ai_layer_norm; /*! * @brief Local response normalization computed on a float array * @ingroup layers_norm * @param out opaque handler to float output channel * @param in opaque handler to float input channel * @param pad amount of padding for the channels */ AI_INTERNAL_API void func_lrn_array_f32(ai_handle out, const ai_handle in, const ai_size in_size, const ai_size channel_size, const ai_i32 pad, const ai_float k, const ai_float alpha, const ai_float beta); /*! * @brief Lp normalization computed on a float array * @ingroup layers_norm * @param out opaque handler to float output channel * @param in opaque handler to float input channel * @param exponent p exponent for the Lp normalization * @param axis_stride stride (in array elements) of the normalization axis * @param axis_size size of the normalization axis * @param outer_size number of tensor slices (including the normalization axis) * on which compute the normalization */ AI_INTERNAL_API void func_norm_array_f32(ai_handle out, const ai_handle in, const ai_float exponent, const ai_float norm, const ai_size axis_stride, const ai_size axis_size, const ai_size outer_size); /*! * @brief Max normalization computed on float array * @ingroup layers_norm * @param out opaque handler to float output channel * @param in opaque handler to float input channel * @param axis_stride stride (in array elements) of the normalization axis * @param axis_size size of the normalization axis * @param outer_size number of tensor slices (including the normalization axis) */ AI_INTERNAL_API void func_norm_max_array_f32(ai_handle out, const ai_handle in, const ai_float norm, const ai_size axis_size, const ai_size n_el); /*! * @brief Fast L2 normalization computed on a float array * @ingroup layers_norm * @param out opaque handler to float output channel * @param in opaque handler to float input channel * @param axis_size size of the normalization axis * @param n_el total number of elements in the tensor */ AI_INTERNAL_API void func_norm_l2_fast_array_f32(ai_handle out, const ai_handle in, const ai_float norm, const ai_size axis_size, const ai_size outer_size); /*! * @brief Fast L1 normalization computed on a float array * @ingroup layers_norm * @param out opaque handler to float output channel * @param in opaque handler to float input channel * @param axis_size size of the normalization axis * @param n_el total number of elements in the tensor */ AI_INTERNAL_API void func_norm_l1_fast_array_f32(ai_handle out, const ai_handle in, const ai_float norm, const ai_size axis_size, const ai_size n_el); /******************************************************************************/ /* Forward Functions Section */ /******************************************************************************/ /*! * @brief Computes the activations of a batchnorm (scale + bias) layer. * @ingroup layers_norm * @param layer the batch normalization (bn) layer */ AI_INTERNAL_API void forward_bn(ai_layer* layer); /*! * @brief Computes the activations of a batchnorm (scale + bias) layer with * integer format * @ingroup layers_norm * @param layer the batch normalization (bn) layer */ AI_INTERNAL_API void forward_bn_integer(ai_layer* layer); /*! * @brief Computes the activations of a Local Response Normalization Layer. * @ingroup layers_norm * @param layer the local response normalization (lrn) layer */ AI_INTERNAL_API void forward_lrn(ai_layer* layer); /*! * @brief Computes the activations of a normalization layer. * @ingroup layers_norm * @param layer the normalization (norm) layer */ AI_INTERNAL_API void forward_norm(ai_layer* layer); /*! * @brief Batch Normalization with 16-bit input, 16-bit threshold and binary output. * It is implemented using a threshold, and this is possible because the output is binary. * @param layer the batch normalization layer */ AI_INTERNAL_API void forward_bn_is16os1ws16(ai_layer *pLayer); AI_API_DECLARE_END #endif /*LAYERS_NORM_H*/
6,910
C
31.909524
97
0.612156
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/Middlewares/ST/AI/Inc/layers_conv2d_dqnn.h
/** ****************************************************************************** * @file layers_conv2d_dqnn.h * @author AIS * @brief header file of AI platform DQNN conv datatypes ****************************************************************************** * @attention * * Copyright (c) 2021 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** @verbatim @endverbatim ****************************************************************************** */ #ifndef LAYERS_CONV2D_DQNN_H #define LAYERS_CONV2D_DQNN_H #pragma once #include "layers_common.h" #include "layers_conv2d.h" /*! * @defgroup layers_conv2d_dqnn Layers Definitions * @brief definition * */ AI_API_DECLARE_BEGIN #define AI_DQNN_PAD_1_KEY (1) #define AI_DQNN_PAD_M1_KEY (-1) #define AI_DQNN_PAD_0_KEY (0) #define AI_DQNN_PAD_1_VALUE (0x0) #define AI_DQNN_PAD_M1_VALUE (0xFFFFFFFF) #define AI_DQNN_PAD_0_VALUE (0x2) /*! * @struct ai_layer_conv2d_dqnn * @ingroup layers_conv2d_dqnn * @brief conv2d_dqnn layer * * @ref forward_conv2d_is1os1ws1 */ typedef AI_ALIGNED_TYPE(struct, 4) ai_layer_conv2d_dqnn_ { AI_LAYER_CONV2D_FIELDS_DECLARE ai_i32 pad_value; } ai_layer_conv2d_dqnn; /******************************************************************************/ /* Forward Functions Section */ /******************************************************************************/ /*! * @brief Handles point wise convolution with binary input, binary output and * binary weights * @ingroup layers_conv2d_dqnn * @param layer conv2d_dqnn layer */ AI_INTERNAL_API void forward_pw_is1os1ws1_bn(ai_layer *pLayer); /*! * @brief Handles point wise convolution with binary input, binary output and * binary weights - Optimized thanks to Optim2 assumptions * @ingroup layers_conv2d_dqnn * @param layer conv2d_dqnn layer */ AI_INTERNAL_API void forward_pw_is1os1ws1_bn_optim2(ai_layer *pLayer); /*! * @brief Handles point wise convolution with binary input, 8-bits output and * binary weights * @ingroup layers_conv2d_dqnn * @param layer conv2d_dqnn layer */ AI_INTERNAL_API void forward_pw_is1os8ws1_bn(ai_layer *pLayer); /*! * @brief Handles point wise convolution with binary input, 8-bits output and * binary weights - Optimized thanks to Optim1 assumptions * @ingroup layers_conv2d_dqnn * @param layer conv2d_dqnn layer */ AI_INTERNAL_API void forward_pw_is1os8ws1_bn_optim1(ai_layer *pLayer); /*! * @brief Handles point-wise convolution with binary input, float32 output * and binary weights * @ingroup layers_conv2d_dqnn * @param layer conv2d_dqnn layer */ AI_INTERNAL_API void forward_pw_is1of32ws1_bn(ai_layer *pLayer); /*! * @brief Handles point-wise convolution with binary input, float32 output * and binary weights - Optimized thanks to Optim1 assumptions * @ingroup layers_conv2d_dqnn * @param layer conv2d_dqnn layer */ AI_INTERNAL_API void forward_pw_is1of32ws1_bn_optim1(ai_layer *pLayer); /*! * @brief Handles 2D convolution with binary input, binary output and * binary weights * @ingroup layers_conv2d_dqnn * @param layer conv2d_dqnn layer */ AI_INTERNAL_API void forward_conv2d_is1os1ws1_bn(ai_layer *pLayer); /*! * @brief Handles 2D convolution with binary input, binary output and * binary weights - Optimized thanks to Optim2 assumptions * @ingroup layers_conv2d_dqnn * @param layer conv2d_dqnn layer */ AI_INTERNAL_API void forward_conv2d_is1os1ws1_bn_optim2(ai_layer *pLayer); /*! * @brief Handles 2D convolution with binary input, 8-bits output and * binary weights * @ingroup layers_conv2d_dqnn * @param layer conv2d_dqnn layer */ AI_INTERNAL_API void forward_conv2d_is1os8ws1_bn(ai_layer *pLayer); /*! * @brief Handles 2D convolution with binary input, 8-bits output and * binary weights - Optimized thanks to Optim1 assumptions * @ingroup layers_conv2d_dqnn * @param layer conv2d_dqnn layer */ AI_INTERNAL_API void forward_conv2d_is1os8ws1_bn_optim1(ai_layer *pLayer); /*! * @brief Handles 2D convolution with binary input, binary output and * binary weights - with 0 padding (QKeras like) * @ingroup layers_conv2d_dqnn * @param layer conv2d_dqnn layer */ AI_INTERNAL_API void forward_conv2d_is1os1ws1_bn_pad0(ai_layer *pLayer); /*! * @brief Handles 2D convolution with binary input, binary output and * binary weights - with 0 padding (QKeras like) - Optimized thanks to * Optim0 assumptions * @ingroup layers_conv2d_dqnn * @param layer conv2d_dqnn layer */ AI_INTERNAL_API void forward_conv2d_is1os1ws1_bn_pad0_optim0(ai_layer *pLayer); /*! * @brief Handles 2D convolution with binary input, 8-bits output and * binary weights - with 0 padding (QKeras like) * @ingroup layers_conv2d_dqnn * @param layer conv2d_dqnn layer */ AI_INTERNAL_API void forward_conv2d_is1os8ws1_bn_pad0(ai_layer *pLayer); /*! * @brief Handles 2D convolution with binary input, binary output and * binary weights - with +1/-1 padding (Larq like) * @ingroup layers_conv2d_dqnn * @param layer conv2d_dqnn layer */ AI_INTERNAL_API void forward_conv2d_is1os1ws1_bn_pad1(ai_layer *pLayer); /*! * @brief Handles 2D convolution with binary input, binary output and * binary weights - with +1/-1 padding (Larq like) - Optimized thanks * to Optim2 assumptions * @ingroup layers_conv2d_dqnn * @param layer conv2d_dqnn layer */ AI_INTERNAL_API void forward_conv2d_is1os1ws1_bn_pad1_optim2(ai_layer *pLayer); /*! * @brief Handles 2D convolution with binary input, 8-bits output and * binary weights - with +1/-1 padding (Larq like) * @ingroup layers_conv2d_dqnn * @param layer conv2d_dqnn layer */ AI_INTERNAL_API void forward_conv2d_is1os8ws1_bn_pad1(ai_layer *pLayer); /*! * @brief Handles 2D convolution with binary input, 8-bits output and * binary weights - with +1/-1 padding (Larq like) - Optimized thanks * to Optim1 assumptions * @ingroup layers_conv2d_dqnn * @param layer conv2d_dqnn layer */ AI_INTERNAL_API void forward_conv2d_is1os8ws1_bn_pad1_optim1(ai_layer *pLayer); /*! * @brief Handles 2D convolution with 8-bits quantized Input and weights and * binary output * @ingroup layers_conv2d_dqnn * @param layer conv2d_dqnn layer */ AI_INTERNAL_API void forward_conv2d_is8os1ws8(ai_layer *pLayer); /*! * @brief Handles 2D convolution with 8-bits quantized Input and weights and * binary output - Optimized thanks to Optim2 assumptions * @ingroup layers_conv2d_dqnn * @param layer conv2d_dqnn layer */ AI_INTERNAL_API void forward_conv2d_is8os1ws8_optim2(ai_layer *pLayer); /*! * @brief Handles 2D convolution with 8-bits quantized Input and weights and * binary output - quantized with DoReFa SotA quantizer * @ingroup layers_conv2d_dqnn * @param layer conv2d_dqnn layer */ AI_INTERNAL_API void forward_conv2d_dorefa_is8os1ws8(ai_layer *pLayer); /*! * @brief Handles 2D convolution with 16-bits quantized input, binary weights and binary output * @ingroup layers_conv2d_dqnn * @param layer conv2d_dqnn layer */ AI_INTERNAL_API void forward_conv2d_is16os1ws1_bn_fxp(ai_layer *pLayer); /*! * @brief Handles 2D convolution with 16-bits quantized input, binary weights and 16-bits quantized output * @ingroup layers_conv2d_dqnn * @param layer conv2d_dqnn layer */ AI_INTERNAL_API void forward_conv2d_is16os16ws1_fxp(ai_layer *pLayer); /*! * @brief Handles depth-wise convolution with binary input, binary output and * binary weights * @ingroup layers_conv2d_dqnn * @param layer conv2d_dqnn layer */ AI_INTERNAL_API void forward_dw_is1os1ws1_bn(ai_layer *pLayer); /*! * @brief Handles depth-wise convolution with binary input, binary output and * binary weights - Optimized thanks to Optim3 assumptions * @ingroup layers_conv2d_dqnn * @param layer conv2d_dqnn layer */ AI_INTERNAL_API void forward_dw_is1os1ws1_bn_optim3(ai_layer *pLayer); /*! * @brief Handles depth-wise convolution with binary input, binary output and * binary weights - with 0 padding (QKeras like) * @ingroup layers_conv2d_dqnn * @param layer conv2d_dqnn layer */ AI_INTERNAL_API void forward_dw_is1os1ws1_bn_pad0(ai_layer *pLayer); /*! * @brief Handles depth-wise convolution with binary input, binary output and * binary weights - with 0 padding (QKeras like) - Optimized thanks to * Optim3 assumptions * @ingroup layers_conv2d_dqnn * @param layer conv2d_dqnn layer */ AI_INTERNAL_API void forward_dw_is1os1ws1_bn_pad0_optim3(ai_layer *pLayer); /*! * @brief Handles depth-wise convolution with binary input, binary output and * binary weights - with +1/-1 padding (Larq like) * @ingroup layers_conv2d_dqnn * @param layer conv2d_dqnn layer */ AI_INTERNAL_API void forward_dw_is1os1ws1_bn_pad1(ai_layer *pLayer); /*! * @brief Handles depth-wise convolution with binary input, binary output and * binary weights - with +1/-1 padding (Larq like) - Optimized thanks to * Optim3 assumptions * @ingroup layers_conv2d_dqnn * @param layer conv2d_dqnn layer */ AI_INTERNAL_API void forward_dw_is1os1ws1_bn_pad1_optim3(ai_layer *pLayer); /*! * @brief Handles 2D convolution with 8-bits quantized Input and output and * binary weights * @ingroup layers_conv2d_dqnn * @param layer conv2d_dqnn layer */ AI_INTERNAL_API void forward_conv2d_is8os8ws1(ai_layer *pLayer); /** * @brief Handles 2D convolution with binary input, fixed point 16-bits output and * binary weights - with 0 padding (QKeras like) - Lite I/F * @ingroup layers_conv2d * @param layer the convolutional (conv) layer */ AI_INTERNAL_API void forward_conv2d_is1os16ws1_bn_pad0_fxp(ai_layer *pLayer); /*! * @brief Handles 2D convolution with binary input, fixed point 16-bits output and * binary weights - with +1/-1 padding (Larq like) - Lite I/F * @ingroup layers_conv2d * @param layer the convolutional (conv) layer */ AI_INTERNAL_API void forward_conv2d_is1os16ws1_bn_pad1_fxp(ai_layer *pLayer); /*! * @brief Handles 2D convolution with binary input, fixed point 16-bits output and * binary weights - with +1/-1 padding (Larq like) - Lite I/F * - Optimized thanks to Optim1 assumptions * @ingroup layers_conv2d * @param layer the convolutional (conv) layer */ AI_INTERNAL_API void forward_conv2d_is1os16ws1_bn_pad1_optim1_fxp(ai_layer *pLayer); /*! * @brief Handles 2D convolution with binary input, fixed point 16-bits unsigned output and * binary weights - with 0 padding (QKeras like) - Lite I/F * @ingroup layers_conv2d * @param layer the convolutional (conv) layer */ AI_INTERNAL_API void forward_conv2d_is1ou16ws1_bn_pad0_fxp(ai_layer *pLayer); /*! * @brief Handles 2D convolution with binary input, fixed point 16-bits unsigned output and * binary weights - with +1/-1 padding (Larq like) - Lite I/F * @ingroup lite_conv2d_dqnn * @param layer the convolutional (conv) layer */ AI_INTERNAL_API void forward_conv2d_is1ou16ws1_bn_pad1_fxp(ai_layer *pLayer); /*! * @brief Handles 2D convolution with binary input, fixed point 16-bits unsiged output and * binary weights - with +1/-1 padding (Larq like) - Lite I/F * - Optimized thanks to Optim1 assumptions * @ingroup lite_conv2d_dqnn * @param layer the convolutional (conv) layer */ AI_INTERNAL_API void forward_conv2d_is1ou16ws1_bn_pad1_optim1_fxp(ai_layer *pLayer); /*! * @brief Computes the activations of a integer quantized 2D convolutional layer * for SSSA per channel quantized RGB scheme using n_channel_in = 3 * @ingroup layers_conv2d * @param layer the convolutional (conv) layer */ AI_INTERNAL_API void forward_conv2d_is8os8ws8_sssa_ch_rgb(const ai_i8 *pData_in, ai_i8 *pData_out, const ai_i8 *pWeights, const ai_i32 *pBias, ai_u16 *pBuffer_a, const ai_size width_in, const ai_size height_in, const ai_size width_out, const ai_size height_out, const ai_u16 n_channel_in, const ai_u16 n_channel_out, const ai_size filt_width, const ai_size filt_height, const ai_u16 filt_pad_x, const ai_u16 filt_pad_y, const ai_u16 filt_stride_x, const ai_u16 filt_stride_y, const ai_float in_scale, const ai_float out_scale, const ai_float *pWt_scale, const ai_i8 in_zeropoint, const ai_i8 out_zeropoint, const ai_bool out_ch_format, ai_i16 *p_out_r_shift, ai_i32 *p_out_factor); /*! * @brief Computes the activations of a point-wise integer quantized convolution for SSSA per channel quantized scheme * @ingroup layers_conv2d * @param layer the convolutional (conv) layer */ AI_INTERNAL_API void forward_pw_is8os8ws8_sssa_ch(const ai_i8 *pData_in, ai_i8 *pData_out, const ai_i8 *pWeights, const ai_i32 *pBias, ai_u16 *pBuffer_a, const ai_size width_in, const ai_size height_in, const ai_size width_out, const ai_size height_out, const ai_u16 n_channel_in, const ai_u16 n_channel_out, const ai_size filt_width, const ai_size filt_height, const ai_u16 filt_pad_x, const ai_u16 filt_pad_y, const ai_u16 filt_stride_x, const ai_u16 filt_stride_y, const ai_u16 dilation_x, const ai_u16 dilation_y, const ai_float in_scale, const ai_float out_scale, const ai_float *pWt_scale, const ai_i8 in_zeropoint, const ai_i8 out_zeropoint, ai_i16 *p_out_r_shift, ai_i32 *p_out_factor, ai_i32 AI_PWOverlay, ai_i16 *bufferA, ai_i32 scratch_size); // st_nn_context_t context); /*! * @brief Computes the activations of a depth-wise integer quantized convolution for SSSA per channel quantized scheme * @ingroup layers_conv2d * @param layer the convolutional (conv) layer */ AI_INTERNAL_API void forward_dw_is8os8ws8_sssa_ch(const ai_i8 *pData_in, ai_i8 *pData_out, const ai_i8 *pWeights, const ai_i32 *pBias, ai_u16 *pBuffer_a, const ai_size width_in, const ai_size height_in, const ai_size width_out, const ai_size height_out, const ai_u16 n_channel_in, const ai_u16 n_channel_out, const ai_size filt_width, const ai_size filt_height, const ai_u16 filt_pad_x, const ai_u16 filt_pad_y, const ai_u16 filt_stride_x, const ai_u16 filt_stride_y, const ai_u16 dilation_x, const ai_u16 dilation_y, const ai_float in_scale, const ai_float out_scale, const ai_float *pWt_scale, const ai_i8 in_zeropoint, const ai_i8 out_zeropoint, ai_i16 *p_out_r_shift, ai_i32 *p_out_factor); AI_API_DECLARE_END #endif /*LAYERS_CONV2D_DQNN_H*/
17,573
C
34.647059
91
0.579981
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/Middlewares/ST/AI/Inc/layers_upsample_generic.h
/** ****************************************************************************** * @file layers_upsample_generic.h * @author Cyril Enault * @brief header file of AI platform padding generic datatypes ****************************************************************************** * @attention * * Copyright (c) 2022 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** @verbatim @endverbatim ****************************************************************************** */ #ifndef LAYERS_UPSAMPLE_H #define LAYERS_UPSAMPLE_H #pragma once #include "layers_generic.h" /*! * @defgroup layers_pad_generic Layers Definitions * @brief definition * */ AI_API_DECLARE_BEGIN /******************************************************************************/ /* Forward Functions Section */ /******************************************************************************/ /*! * @brief Handles generic upsmapling in nearest mode * @ingroup layers_generic * @param layer upsample layer */ AI_INTERNAL_API void forward_upsample_nearest(ai_layer *pLayer); /*! * @brief Handles generic upsmapling in zeros mode * @ingroup layers_generic * @param layer upsample layer */ AI_INTERNAL_API void forward_upsample_zeros(ai_layer *pLayer); /*! * @brief Handles generic upsmapling in bilinear mode * @ingroup layers_generic * @param layer upsample layer */ AI_INTERNAL_API void forward_upsample_bilinear(ai_layer *pLayer); AI_API_DECLARE_END #endif /*LAYERS_PAD_GENERIC_H*/
1,845
C
26.552238
80
0.509485
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/Middlewares/ST/AI/Inc/lite_bn_f32.h
#ifndef LITE_BN_F32_H #define LITE_BN_F32_H #pragma once #include "ai_lite_interface.h" /*! * @brief Forward function for a batch normalization (BN) layer with * signed float input, signed float output, and float parameters. * @ingroup lite_bn_f32 * @param output The pointer to output buffer. * @param input The pointer to input buffer. * @param scale The pointer to BN scale param. * @param bias The pointer to bias. * @param n_elements The number of elements in the input tensor. * @param n_channel_in The number of channel in the input tensor. */ LITE_API_ENTRY void forward_lite_bn_if32of32wf32( ai_float* output, const ai_float* input, const ai_float* scale, const ai_float* bias, const ai_u32 n_elements, const ai_u32 n_channel_in); #endif /* LITE_BN_F32_H */
791
C
29.461537
69
0.718078
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/Middlewares/ST/AI/Inc/lite_dense_is1ws1.h
#ifndef _LITE_DENSE_IS1WS1_H #define _LITE_DENSE_IS1WS1_H #pragma once #include "ai_lite_interface.h" /*! * @brief Forward function for a dense layer with signed binary input, * signed binary output, and signed binary weights. * @ingroup lite_dense_is1ws1 * @param output The pointer to output buffer. * @param input The pointer to input buffer. * @param weights The pointer to weights. * @param bias The pointer to bias (NULL if not available). * @param scratch The pointer to the scratch buffer. * @param n_channel_in The number of channels of the input. * @param n_channel_ouy The number of channels of the output, i.e., * the number of dense hidden neurons. */ LITE_API_ENTRY void forward_lite_dense_is1os1ws1( ai_pbits *output, const ai_pbits *input, const ai_pbits *weights, const ai_pbits *bias, ai_i32 *scratch, const ai_u32 n_channel_in, const ai_u32 n_channel_out ); /*! * @brief Forward function for a dense layer with signed binary input, * signed binary output, and signed binary weights. * The BN is fused, i.e., the layer requires weights, scale, and offset, where * weights are those of the dense layer, scale is that of the BN, and the offset * corresponds to dense bias * bn scale + bn offset. If the parameters do not * agree with such convention, the behavior is undefined. * @ingroup lite_dense_is1ws1 * @param output The pointer to output buffer. * @param input The pointer to input buffer. * @param weights The pointer to weights. * @param scale The pointer to scale. * @param offset The pointer to offset. * @param scratch The pointer to the scratch buffer. * @param n_channel_in The number of channels of the input. * @param n_channel_ouy The number of channels of the output, i.e., * the number of dense hidden neurons. */ LITE_API_ENTRY void forward_lite_dense_is1os1ws1_bn( ai_pbits *output, const ai_pbits *input, const ai_pbits *weights, const ai_float *scale, const ai_float *offset, ai_i32 *scratch, const ai_u32 n_channel_in, const ai_u32 n_channel_out ); /*! * @brief Forward function for a dense layer with signed binary input, * signed binary output, and signed 16bit weights. * @ingroup lite_dense_is1ws1 * @param output The pointer to output buffer. * @param input The pointer to input buffer. * @param weights The pointer to weights. * @param bias The pointer to bias (NULL if not available). * @param scratch The pointer to the scratch buffer (signed 32bit). * @param n_channel_in The number of channels of the input. * @param n_channel_ouy The number of channels of the output, i.e., * the number of dense hidden neurons. */ LITE_API_ENTRY void forward_lite_dense_is1os16ws1( ai_i16 *output, const ai_pbits *input, const ai_pbits *weights, const ai_pbits *bias, ai_i32 *scratch, const ai_u32 n_channel_in, const ai_u32 n_channel_out); /*! * @brief Forward function for a dense layer with signed binary input, * signed binary output, and signed 16bit weights. * The BN is fused, i.e., the layer requires weights, scale, and offset, where * weights are those of the dense layer, scale is that of the BN, and the offset * corresponds to dense bias * bn scale + bn offset. If the parameters do not * agree with such convention, the behavior is undefined. * @ingroup lite_dense_is1ws1 * @param output The pointer to output buffer. * @param input The pointer to input buffer. * @param weights The pointer to weights. * @param bias The pointer to bias (NULL if not available). * @param scratch The pointer to the scratch buffer (signed 32bit). * @param n_channel_in The number of channels of the input. * @param n_channel_ouy The number of channels of the output, i.e., * the number of dense hidden neurons. */ LITE_API_ENTRY void forward_lite_dense_is1os16ws1_bn( ai_i16 *output, const ai_pbits *input, const ai_pbits *weights, const ai_float *scale, const ai_float *offset, ai_i32 *scratch, const ai_u32 n_channel_in, const ai_u32 n_channel_out); /*! * @brief Forward function for a dense layer with signed binary input, * signed float output, and signed binary weights. * @ingroup lite_dense_is1ws1 * @param output The pointer to output buffer. * @param input The pointer to input buffer. * @param weights The pointer to weights. * @param bias The pointer to bias (NULL if not available). * @param scratch The pointer to the scratch buffer (unused). * @param n_channel_in The number of channels of the input. * @param n_channel_ouy The number of channels of the output, i.e., * the number of dense hidden neurons. */ LITE_API_ENTRY void forward_lite_dense_is1of32ws1( ai_float *output, const ai_pbits *input, const ai_pbits *weights, const ai_pbits *bias, ai_i32 *scratch, const ai_u32 n_channel_in, const ai_u32 n_channel_out ); /*! * @brief Forward function for a dense layer with signed binary input, * signed float output, and signed binary weights. * The BN is fused, i.e., the layer requires weights, scale, and offset, where * weights are those of the dense layer, scale is that of the BN, and the offset * corresponds to dense bias * bn scale + bn offset. If the parameters do not * agree with such convention, the behavior is undefined. * @ingroup lite_dense_is1ws1 * @param output The pointer to output buffer. * @param input The pointer to input buffer. * @param weights The pointer to weights. * @param scale The pointer to scale. * @param offset The pointer to offset. * @param scratch The pointer to the scratch buffer (unused). * @param n_channel_in The number of channels of the input. * @param n_channel_out The number of channels of the output, i.e., * the number of dense hidden neurons. */ LITE_API_ENTRY void forward_lite_dense_is1of32ws1_bn( ai_float *output, const ai_pbits *input, const ai_pbits *weights, const ai_float *scale, const ai_float *offset, ai_i32 *scratch, const ai_u32 n_channel_in, const ai_u32 n_channel_out ); #endif /*_LITE_DENSE_IS1WS1_H*/
5,999
C
40.666666
80
0.724287
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/Middlewares/ST/AI/Inc/layers_custom.h
/** ****************************************************************************** * @file layers_custom.h * @author Marco Lattuada * @brief header file of AI platform custom layers datatype ****************************************************************************** * @attention * * Copyright (c) 2020 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** @verbatim @endverbatim ****************************************************************************** */ #ifndef LAYERS_CUSTOM_H #define LAYERS_CUSTOM_H #pragma once #include "layers_common.h" /*! * @defgroup layers_custom Custom layer definitions * @brief Definition of structures custom layers */ AI_API_DECLARE_BEGIN /*! * @struct ai_layer_custom * @ingroup layers_custom * @brief Custom layer wrapper * * The custom layer wrapper */ typedef ai_layer_stateful ai_layer_custom; AI_API_DECLARE_END #endif /*LAYERS_CUSTOM_H*/
1,217
C
24.914893
80
0.518488
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/Middlewares/ST/AI/Inc/lite_conv2d.h
/** ****************************************************************************** * @file lite_conv2d.h * @author AIS * @brief header file of AI platform lite conv2d kernel datatypes ****************************************************************************** * @attention * * Copyright (c) 2021 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** @verbatim @endverbatim ****************************************************************************** */ #ifndef LITE_CONV2D_H #define LITE_CONV2D_H #pragma once #include "ai_lite_interface.h" /******************************************************************************/ /* Forward Functions Section */ /******************************************************************************/ /*! * @brief Handles 2D convolution with float input, float output and * float weights * @ingroup lite_conv2d */ LITE_API_ENTRY void forward_lite_conv2d_if32of32wf32(const ai_float *pDataIn_init, ai_float *pDataOut_init, const ai_ptr_const pWeights_init, const ai_ptr_const pBias_init, const ai_size n_channel_in, const ai_size n_channel_out, const ai_size width_in, const ai_size height_in, const ai_size width_out, const ai_size height_out, const ai_size filt_width, const ai_size filt_height, const ai_u16 filt_pad_x, const ai_u16 filt_pad_y, const ai_u16 filt_stride_x, const ai_u16 filt_stride_y, const ai_size filt_height_dilated, const ai_size filt_width_dilated, const ai_u16 dilation_x, const ai_u16 dilation_y, const ai_size n_groups); /*! * @brief Handles 2D depthwise convolution with float input, float output and * float weights * @ingroup lite_conv2d */ LITE_API_ENTRY void forward_lite_dw_if32of32wf32(const ai_float *pDataIn_init, ai_float *pDataOut_init, const ai_ptr_const pWeights_init, const ai_ptr_const pBias_init, const ai_size n_channel_in, const ai_size n_channel_out, const ai_size width_in, const ai_size height_in, const ai_size width_out, const ai_size height_out, const ai_size filt_width, const ai_size filt_height, const ai_u16 filt_pad_x, const ai_u16 filt_pad_y, const ai_u16 filt_stride_x, const ai_u16 filt_stride_y, const ai_size filt_height_dilated, const ai_size filt_width_dilated, const ai_u16 dilation_x, const ai_u16 dilation_y, const ai_size n_groups); /*! * @brief Handles 2D grouped convolution with float input, float output and * float weights * @ingroup lite_conv2d */ LITE_API_ENTRY void forward_lite_conv2d_if32of32wf32_group(const ai_float *pDataIn_init, ai_float *pDataOut_init, const ai_ptr_const pWeights_init, const ai_ptr_const pBias_init, const ai_size n_channel_in, const ai_size n_channel_out, const ai_size width_in, const ai_size height_in, const ai_size width_out, const ai_size height_out, const ai_size filt_width, const ai_size filt_height, const ai_u16 filt_pad_x, const ai_u16 filt_pad_y, const ai_u16 filt_stride_x, const ai_u16 filt_stride_y, const ai_size filt_height_dilated, const ai_size filt_width_dilated, const ai_u16 dilation_x, const ai_u16 dilation_y, const ai_size n_groups); /*! * @brief Handles dilated conv2d convolutions (valid padding) * @ingroup lite_conv2d */ LITE_API_ENTRY void forward_lite_conv2d_dilated_sssa8_ch(const ai_i8 *pData_in, const ai_u16 dim_im_in_x, const ai_u16 dim_im_in_y, const ai_u16 n_channel_in, const ai_i8 *pWeights, const ai_u16 n_channel_out, const ai_u16 dim_kernel_x, const ai_u16 dim_kernel_y, const ai_u16 stride_x, const ai_u16 stride_y, const ai_u16 dilation_x, const ai_u16 dilation_y, const ai_i32 *pBias, const ai_i8 in_zeropoint, const ai_i8 out_zeropoint, const ai_layer_format_type out_ch_format, ai_i8 *pData_out, const ai_u16 dim_im_out_x, const ai_u16 dim_im_out_y, ai_u32 height_loop_cnt, const ai_u16 weights_prefetch_enabled, ai_i32 scratch_size, ai_i16 *pBuffer_a); /*! * @brief Handles conv2d convolutions (valid padding) with number of channels >= 8 * @ingroup lite_conv2d */ LITE_API_ENTRY void forward_lite_conv2d_deep_sssa8_ch(const ai_i8 *pData_in, const ai_u16 dim_im_in_x, const ai_u16 dim_im_in_y, const ai_u16 n_channel_in, const ai_i8 *pWeights, const ai_u16 n_channel_out, const ai_u16 dim_kernel_x, const ai_u16 dim_kernel_y, const ai_u16 stride_x, const ai_u16 stride_y, const ai_i32 *pBias, const ai_i8 in_zeropoint, const ai_i8 out_zeropoint, const ai_layer_format_type out_ch_format, ai_i8 *pData_out, const ai_u16 dim_im_out_x, const ai_u16 dim_im_out_y, ai_u32 height_loop_cnt, const ai_u16 weights_prefetch_enabled, ai_i32 scratch_size, ai_i16 *pBuffer_a); /*! * @brief Handles conv2d convolutions (valid padding) with number of channels >= 8 * Special forward function for 3x3 kernels and Stride = 1 * @ingroup lite_conv2d */ LITE_API_ENTRY void forward_lite_conv2d_deep_3x3_sssa8_ch(const ai_i8 *pData_in, const ai_u16 dim_im_in_x, const ai_u16 dim_im_in_y, const ai_u16 n_channel_in, const ai_i8 *pWeights, const ai_u16 n_channel_out, const ai_i32 *pBias, const ai_i8 in_zeropoint, const ai_i8 out_zeropoint, const ai_layer_format_type out_ch_format, ai_i8 *pData_out, const ai_u16 dim_im_out_x, const ai_u16 dim_im_out_y, ai_u32 height_loop_cnt, ai_i32 scratch_size, ai_i16 *pBuffer_a); /*! * @brief Handles conv2d convolutions with same padding or with number of channels < 8 * @ingroup lite_conv2d */ LITE_API_ENTRY void forward_lite_conv2d_sssa8_ch(const ai_i8 *pData_in, const ai_u16 dim_im_in_x, const ai_u16 dim_im_in_y, const ai_u16 n_channel_in, const ai_i8 *pWeights, const ai_u16 n_channel_out, const ai_u16 dim_kernel_x, const ai_u16 dim_kernel_y, const ai_u16 stride_x, const ai_u16 stride_y, const ai_u16 padding_x, const ai_u16 padding_y, const ai_i32 *pBias, const ai_i8 in_zeropoint, const ai_i8 out_zeropoint, const ai_layer_format_type out_ch_format, ai_i8 *pData_out, const ai_u16 dim_im_out_x, const ai_u16 dim_im_out_y, const ai_u16 weights_prefetch_enabled, ai_i32 scratch_size, ai_i16 *pBuffer_a); /*! * @brief Handles rgb conv2d convolutions * @ingroup lite_conv2d */ LITE_API_ENTRY void forward_lite_conv2d_rgb_sssa8_ch(const ai_i8 *pData_in, const ai_u16 dim_im_in, const ai_i8 *pWeights, const ai_u16 n_channel_out, const ai_u16 dim_kernel, const ai_u16 padding, const ai_u16 stride, const ai_i32 *pBias, const ai_i8 in_zeropoint, const ai_i8 out_zeropoint, const ai_layer_format_type out_ch_format, ai_i8 *pData_out, const ai_u16 dim_im_out, ai_i32 scratch_size, ai_i16 *pBuffer_a); /*! * @brief Handles 2D convolution with float input, float output and * float weights with pool fused * @ingroup lite_conv2d */ LITE_API_ENTRY void forward_lite_conv2d_if32of32wf32_pool(const ai_float *pDataIn_init, ai_float *pDataOut_init, const ai_float * pWeights_init, const ai_float *pBias_init, ai_float *pScratch_init, const ai_short_size n_channel_in, const ai_short_size n_channel_out, const ai_short_size width_in, const ai_short_size height_in, const ai_short_size width_out, const ai_short_size height_out, const ai_short_size filt_width, const ai_short_size filt_height, const ai_u16 filt_pad_x, const ai_u16 filt_pad_y, const ai_u16 filt_stride_x, const ai_u16 filt_stride_y, const ai_short_size filt_height_dilated, const ai_short_size filt_width_dilated, const ai_u16 dilation_x, const ai_u16 dilation_y, const ai_short_size n_groups, const ai_short_size width_conv_out, const ai_short_size height_conv_out, ai_handle pool_func, const ai_short_size pool_width, const ai_short_size pool_height, const ai_short_size pool_stride_x, const ai_short_size pool_stride_y, const ai_short_size pool_pad_x, const ai_short_size pool_pad_y); /*! * @brief Handles 2D depthwise convolution with float input, float output and * float weights with pool fused * @ingroup lite_conv2d */ LITE_API_ENTRY void forward_lite_dw_if32of32wf32_pool(const ai_float *pDataIn_init, ai_float *pDataOut_init, const ai_float *pWeights_init, const ai_float *pBias_init, ai_float *pScratch_init, const ai_short_size n_channel_in, const ai_short_size n_channel_out, const ai_short_size width_in, const ai_short_size height_in, const ai_short_size width_out, const ai_short_size height_out, const ai_short_size filt_width, const ai_short_size filt_height, const ai_u16 filt_pad_x, const ai_u16 filt_pad_y, const ai_u16 filt_stride_x, const ai_u16 filt_stride_y, const ai_short_size filt_height_dilated, const ai_short_size filt_width_dilated, const ai_u16 dilation_x, const ai_u16 dilation_y, const ai_short_size n_groups, const ai_short_size width_conv_out, const ai_short_size height_conv_out, ai_handle pool_func, const ai_short_size pool_width, const ai_short_size pool_height, const ai_short_size pool_stride_x, const ai_short_size pool_stride_y, const ai_short_size pool_pad_x, const ai_short_size pool_pad_y); /*! * @brief Handles 2D grouped convolution with float input, float output and * float weights with pool fused * @ingroup lite_conv2d */ LITE_API_ENTRY void forward_lite_conv2d_if32of32wf32_group_pool(const ai_float *pDataIn_init, ai_float *pDataOut_init, const ai_float *pWeights_init, const ai_float *pBias_init, ai_float *pScratch_init, const ai_short_size n_channel_in, const ai_short_size n_channel_out, const ai_short_size width_in, const ai_short_size height_in, const ai_short_size width_out, const ai_short_size height_out, const ai_short_size filt_width, const ai_short_size filt_height, const ai_u16 filt_pad_x, const ai_u16 filt_pad_y, const ai_u16 filt_stride_x, const ai_u16 filt_stride_y, const ai_short_size filt_height_dilated, const ai_short_size filt_width_dilated, const ai_u16 dilation_x, const ai_u16 dilation_y, const ai_short_size n_groups, const ai_short_size width_conv_out, const ai_short_size height_conv_out, ai_handle pool_func, const ai_short_size pool_width, const ai_short_size pool_height, const ai_short_size pool_stride_x, const ai_short_size pool_stride_y, const ai_short_size pool_pad_x, const ai_short_size pool_pad_y); #endif /*LITE_CONV2D_H*/
18,200
C
49.418282
86
0.398407
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/Middlewares/ST/AI/Inc/layers_formats_converters.h
/** ****************************************************************************** * @file layers_formats_converters.h * @author AST Embedded Analytics Research Platform * @brief header file of formats converters layers ****************************************************************************** * @attention * * Copyright (c) 2021 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** @verbatim @endverbatim ****************************************************************************** */ #ifndef LAYERS_FORMATS_CONVERTERS_H #define LAYERS_FORMATS_CONVERTERS_H #pragma once #include "layers_common.h" /*! * @defgroup layers_formats_converters Formats Converters Layers Definition * @brief this group implements formats converter layers (cast, etc.) * */ AI_API_DECLARE_BEGIN /*! * @struct ai_layer_cast * @ingroup layers_formats_converters * @brief C Implementation of cast layer */ typedef AI_ALIGNED_TYPE(struct, 4) ai_layer_cast_ { AI_LAYER_COMMON_FIELDS_DECLARE ai_array_format to_format; /*!< cast output format */ } ai_layer_cast; /*****************************************************************************/ /* Forward Functions Section */ /*****************************************************************************/ /*! * @brief forward function for cast layer. * @ingroup layers_ * @param layer template layer as an opaque pointer */ AI_INTERNAL_API void forward_cast(ai_layer* layer); AI_API_DECLARE_END #endif /*LAYERS_FORMATS_CONVERTERS_H*/
1,862
C
29.048387
80
0.50913
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/Middlewares/ST/AI/Inc/layers_pool.h
/** ****************************************************************************** * @file layers_pool.h * @author AST Embedded Analytics Research Platform * @brief header file of AI platform pooling layers datatypes ****************************************************************************** * @attention * * Copyright (c) 2018 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** @verbatim @endverbatim ****************************************************************************** */ #ifndef LAYERS_POOL_H #define LAYERS_POOL_H #pragma once #include "layers_common.h" #include "lite_maxpool_dqnn.h" #include "lite_pool_f32.h" /*! * @defgroup layers_pool Pooling Layers Definitions * @brief definition * */ AI_API_DECLARE_BEGIN /*! * @struct ai_layer_pool * @ingroup layers_pool * @brief Pooling layer * * The type of pooling function is handled by the specific forward function * @ref forward_pool */ typedef AI_ALIGNED_TYPE(struct, 4) ai_layer_pool_ { AI_LAYER_COMMON_FIELDS_DECLARE ai_shape_2d pool_size; /*!< pooling size */ ai_shape_2d pool_stride; /*!< pooling stride */ ai_shape pool_pad; /*!< pooling pad, y,x border sizes */ ai_u8 count_include_pad; /*!< include pad flag */ } ai_layer_pool; /*! * @brief Max Pooling on a 8/16 bits fixed point data array * @ingroup layers_pool * @param in opaque handler to input data to process * @param dim_im_in_x input feature map width * @param dim_im_in_y input feature map height * @param ch_im_in number of input channels * @param dim_kernel_x kernel width * @param dim_kernel_y kernel height * @param padding_x right padding value * @param padding_y top padding value * @param stride_x stride value on x dimension * @param stride_y stride value on y dimension * @param dim_im_out_x output feature map width * @param dim_im_out_y output feature map height * @param out opaque handler to output data */ AI_INTERNAL_API void pool_func_mp_array_fixed(ai_handle in, const ai_u16 dim_im_in_x, const ai_u16 dim_im_in_y, const ai_u16 ch_im_in, const ai_u16 dim_kernel_x, const ai_u16 dim_kernel_y, const ai_u16 padding_x, const ai_u16 padding_y, const ai_u16 stride_x, const ai_u16 stride_y, const ai_u16 dim_im_out_x, const ai_u16 dim_im_out_y, ai_handle out); /*! * @brief Max Pooling on a 8-bits integer quantized data array * @ingroup layers_pool * @param in opaque handler to input data to process * @param dim_im_in_x input feature map width * @param dim_im_in_y input feature map height * @param ch_im_in number of input channels * @param dim_kernel_x kernel width * @param dim_kernel_y kernel height * @param padding_x right padding value * @param padding_y top padding value * @param stride_x stride value on x dimension * @param stride_y stride value on y dimension * @param dim_im_out_x output feature map width * @param dim_im_out_y output feature map height * @param out opaque handler to output data */ AI_INTERNAL_API void pool_func_mp_array_integer(ai_handle in, const ai_u16 dim_im_in_x, const ai_u16 dim_im_in_y, const ai_u16 ch_im_in, const ai_u16 dim_kernel_x, const ai_u16 dim_kernel_y, const ai_u16 padding_x, const ai_u16 padding_y, const ai_u16 stride_x, const ai_u16 stride_y, const ai_u16 dim_im_out_x, const ai_u16 dim_im_out_y, ai_handle out); /*! * @brief Max Pooling on a signed 8-bits integer quantized data array * @ingroup layers_pool * @param in opaque handler to input data to process * @param dim_im_in_x input feature map width * @param dim_im_in_y input feature map height * @param ch_im_in number of input channels * @param dim_kernel_x kernel width * @param dim_kernel_y kernel height * @param padding_x right padding value * @param padding_y top padding value * @param stride_x stride value on x dimension * @param stride_y stride value on y dimension * @param dim_im_out_x output feature map width * @param dim_im_out_y output feature map height * @param out opaque handler to output data */ AI_INTERNAL_API void pool_func_mp_array_integer_INT8(ai_handle in, const ai_u16 dim_im_in_x, const ai_u16 dim_im_in_y, const ai_u16 ch_im_in, const ai_u16 dim_kernel_x, const ai_u16 dim_kernel_y, const ai_u16 padding_x, const ai_u16 padding_y, const ai_u16 stride_x, const ai_u16 stride_y, const ai_u16 dim_im_out_x, const ai_u16 dim_im_out_y, ai_handle out); /*! * @brief Max Pooling on a unsigned 8-bits integer quantized data array * @ingroup layers_pool * @param in opaque handler to input data to process * @param dim_im_in_x input feature map width * @param dim_im_in_y input feature map height * @param ch_im_in number of input channels * @param dim_kernel_x kernel width * @param dim_kernel_y kernel height * @param padding_x right padding value * @param padding_y top padding value * @param stride_x stride value on x dimension * @param stride_y stride value on y dimension * @param dim_im_out_x output feature map width * @param dim_im_out_y output feature map height * @param out opaque handler to output data */ AI_INTERNAL_API void pool_func_mp_array_integer_UINT8(ai_handle in, const ai_u16 dim_im_in_x, const ai_u16 dim_im_in_y, const ai_u16 ch_im_in, const ai_u16 dim_kernel_x, const ai_u16 dim_kernel_y, const ai_u16 padding_x, const ai_u16 padding_y, const ai_u16 stride_x, const ai_u16 stride_y, const ai_u16 dim_im_out_x, const ai_u16 dim_im_out_y, ai_handle out); /*! * @brief Average Pooling on a 8/16 bits fixed point data array * @ingroup layers_pool * @param in opaque handler to input data to process * @param dim_im_in_x input feature map width * @param dim_im_in_y input feature map height * @param ch_im_in number of input channels * @param dim_kernel_x kernel width * @param dim_kernel_y kernel height * @param padding_x right padding value * @param padding_y top padding value * @param stride_x stride value on x dimension * @param stride_y stride value on y dimension * @param dim_im_out_x output feature map width * @param dim_im_out_y output feature map height * @param out opaque handler to scratch memory */ AI_INTERNAL_API void pool_func_ap_array_fixed(ai_handle in, const ai_u16 dim_im_in_x, const ai_u16 dim_im_in_y, const ai_u16 ch_im_in, const ai_u16 dim_kernel_x, const ai_u16 dim_kernel_y, const ai_u16 padding_x, const ai_u16 padding_y, const ai_u16 stride_x, const ai_u16 stride_y, const ai_u16 dim_im_out_x, const ai_u16 dim_im_out_y, ai_handle out); /*! * @brief Average Pooling on a 8-bits integer quantized data array * @ingroup layers_pool * @param in opaque handler to input data to process * @param dim_im_in_x input feature map width * @param dim_im_in_y input feature map height * @param ch_im_in number of input channels * @param dim_kernel_x kernel width * @param dim_kernel_y kernel height * @param padding_x right padding value * @param padding_y top padding value * @param stride_x stride value on x dimension * @param stride_y stride value on y dimension * @param dim_im_out_x output feature map width * @param dim_im_out_y output feature map height * @param out opaque handler to scratch memory */ AI_INTERNAL_API void pool_func_ap_array_integer(ai_handle in, const ai_u16 dim_im_in_x, const ai_u16 dim_im_in_y, const ai_u16 ch_im_in, const ai_u16 dim_kernel_x, const ai_u16 dim_kernel_y, const ai_u16 padding_x, const ai_u16 padding_y, const ai_u16 stride_x, const ai_u16 stride_y, const ai_u16 dim_im_out_x, const ai_u16 dim_im_out_y, ai_handle out); /*! * @brief Average Pooling on a signed 8-bits integer quantized data array * @ingroup layers_pool * @param in opaque handler to input data to process * @param dim_im_in_x input feature map width * @param dim_im_in_y input feature map height * @param ch_im_in number of input channels * @param dim_kernel_x kernel width * @param dim_kernel_y kernel height * @param padding_x right padding value * @param padding_y top padding value * @param stride_x stride value on x dimension * @param stride_y stride value on y dimension * @param dim_im_out_x output feature map width * @param dim_im_out_y output feature map height * @param out opaque handler to scratch memory */ AI_INTERNAL_API void pool_func_ap_array_integer_INT8(ai_handle in, const ai_u16 dim_im_in_x, const ai_u16 dim_im_in_y, const ai_u16 ch_im_in, const ai_u16 dim_kernel_x, const ai_u16 dim_kernel_y, const ai_u16 padding_x, const ai_u16 padding_y, const ai_u16 stride_x, const ai_u16 stride_y, const ai_u16 dim_im_out_x, const ai_u16 dim_im_out_y, ai_handle out); /*! * @brief Average Pooling on a unsigned 8-bits integer quantized data array * @ingroup layers_pool * @param in opaque handler to input data to process * @param dim_im_in_x input feature map width * @param dim_im_in_y input feature map height * @param ch_im_in number of input channels * @param dim_kernel_x kernel width * @param dim_kernel_y kernel height * @param padding_x right padding value * @param padding_y top padding value * @param stride_x stride value on x dimension * @param stride_y stride value on y dimension * @param dim_im_out_x output feature map width * @param dim_im_out_y output feature map height * @param out opaque handler to scratch memory */ AI_INTERNAL_API void pool_func_ap_array_integer_UINT8(ai_handle in, const ai_u16 dim_im_in_x, const ai_u16 dim_im_in_y, const ai_u16 ch_im_in, const ai_u16 dim_kernel_x, const ai_u16 dim_kernel_y, const ai_u16 padding_x, const ai_u16 padding_y, const ai_u16 stride_x, const ai_u16 stride_y, const ai_u16 dim_im_out_x, const ai_u16 dim_im_out_y, ai_handle out); /******************************************************************************/ /* Forward Functions Section */ /******************************************************************************/ /*! * @brief Computes the activations of a max pooling layer. * @ingroup layers_pool * @param layer the pooling (pool) layer */ AI_INTERNAL_API void forward_mp(ai_layer* layer); /*! * @brief Computes the activations of a fixed point max pooling layer. * @ingroup layers_pool * @param layer the pooling (pool) layer */ AI_INTERNAL_API void forward_mp_fixed(ai_layer *pLayer); /*! * @brief Computes the activations of an integer-quantized max pooling layer. * @ingroup layers_pool * @param layer the pooling (pool) layer */ AI_INTERNAL_API void forward_mp_integer(ai_layer *pLayer); /*! * @brief Computes the activations of an integer-quantized max pooling layer * with int8 I/O * @ingroup layers_pool * @param layer the pooling (pool) layer */ AI_INTERNAL_API void forward_mp_integer_INT8(ai_layer *pLayer); /*! * @brief Computes the activations of an integer-quantized max pooling layer * with uint8 I/O * @ingroup layers_pool * @param layer the pooling (pool) layer */ AI_INTERNAL_API void forward_mp_integer_UINT8(ai_layer *pLayer); /*! * @brief Computes the activations of an integer-quantized max pooling layer * with int16 I/O * @ingroup layers_pool * @param layer the pooling (pool) layer */ AI_INTERNAL_API void forward_mp_integer_INT16(ai_layer *pLayer); /*! * @brief Computes the activations of an integer-quantized max pooling layer * with uint16 I/O * @ingroup layers_pool * @param layer the pooling (pool) layer */ AI_INTERNAL_API void forward_mp_integer_UINT16(ai_layer *pLayer); /*! * @brief Computes the activations of an average pooling layer. * @ingroup layers_pool * @param layer the pooling (pool) layer */ AI_INTERNAL_API void forward_ap(ai_layer* layer); /*! * @brief Computes the activations of a fixed point average pooling layer. * @ingroup layers_pool * @param layer the pooling (pool) layer */ AI_INTERNAL_API void forward_ap_fixed(ai_layer *pLayer); /*! * @brief Computes the activations of an integer-quantized average pooling layer. * @ingroup layers_pool * @param layer the pooling (pool) layer */ AI_INTERNAL_API void forward_ap_integer(ai_layer *pLayer); /*! * @brief Computes the activations of an integer-quantized average pooling layer * with int8 I/O * @ingroup layers_pool * @param layer the pooling (pool) layer */ AI_INTERNAL_API void forward_ap_integer_INT8(ai_layer *pLayer); /*! * @brief Computes the activations of an integer-quantized average pooling layer * with uint8 I/O * @ingroup layers_pool * @param layer the pooling (pool) layer */ AI_INTERNAL_API void forward_ap_integer_UINT8(ai_layer *pLayer); AI_API_DECLARE_END #endif /*LAYERS_POOL_H*/
14,171
C
36.294737
81
0.624656
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/Middlewares/ST/AI/Inc/layers_pad_generic.h
/** ****************************************************************************** * @file layers_pad_generic.h * @author Marco Forleo * @brief header file of AI platform padding generic datatypes ****************************************************************************** * @attention * * Copyright (c) 2022 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** @verbatim @endverbatim ****************************************************************************** */ #ifndef LAYERS_PADDING_DQNN_H #define LAYERS_PADDING_DQNN_H #pragma once #include "layers_generic.h" /*! * @defgroup layers_pad_generic Layers Definitions * @brief definition * */ AI_API_DECLARE_BEGIN /******************************************************************************/ /* Forward Functions Section */ /******************************************************************************/ /*! * @brief Handles generic padding in constant mode * @ingroup layers_generic_dqnn * @param layer pad layer */ AI_INTERNAL_API void forward_pad_constant(ai_layer *pLayer); /*! * @brief Handles generic padding in edge mode * @ingroup layers_generic_dqnn * @param layer pad layer */ AI_INTERNAL_API void forward_pad_edge(ai_layer *pLayer); /*! * @brief Handles generic padding in reflect mode * @ingroup layers_generic_dqnn * @param layer pad layer */ AI_INTERNAL_API void forward_pad_reflect(ai_layer *pLayer); /*! * @brief Handles generic padding in constant mode Channel 1st 8bit * @ingroup layers_generic_dqnn * @param layer pad layer */ AI_INTERNAL_API void forward_pad_8bit_ch1st_3x3_constant(ai_layer* pLayer); AI_API_DECLARE_END #endif /*LAYERS_PAD_GENERIC_H*/
2,034
C
25.776315
80
0.525074
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/Middlewares/ST/AI/Inc/layers_ml_treeensembleclassifier.h
/** ****************************************************************************** * @file layers_ml_treeensembleclassifier.h * @author AIS * @brief header file of AI platform TreeEnsembleClassifier datatypes ****************************************************************************** * @attention * * Copyright (c) 2021-2022 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** @verbatim @endverbatim ****************************************************************************** */ #ifndef LAYERS_TREE_ENSEMBLE_CLASSIFIER_H #define LAYERS_TREE_ENSEMBLE_CLASSIFIER_H #pragma once #include "layers_common.h" #include "layers_nl.h" /*! * @defgroup layers_ml_treensembleclassifier Layers Definitions * @brief definition * */ AI_API_DECLARE_BEGIN /* Error return codes */ #define AI_TREE_ENSEMBLE_CLASSIFIER_ERROR_NO 0 #define AI_TREE_ENSEMBLE_CLASSIFIER_ERROR_WRONG_IDX_FMT -1 #define AI_TREE_ENSEMBLE_CLASSIFIER_ERROR_UNFOUND_LEAF -2 #define AI_TREE_ENSEMBLE_CLASSIFIER_ERROR_UNSUPPORTED_BRANCH -3 #define AI_TREE_ENSEMBLE_CLASSIFIER_ERROR_UNSUPPORTED_FEATURE -4 #define AI_TREE_ENSEMBLE_CLASSIFIER_DEPTH_MAX 10000 /* Type of condition in the TreeEnsembleClassifier*/ typedef enum { AI_TREE_ENSEMBLE_CLASSIFIER_BRANCH_LT_IDX = 0, AI_TREE_ENSEMBLE_CLASSIFIER_BRANCH_LEQ_IDX, AI_TREE_ENSEMBLE_CLASSIFIER_BRANCH_EQ_IDX, AI_TREE_ENSEMBLE_CLASSIFIER_BRANCH_END, } ai_tree_ensenble_classifier_branch_e; typedef AI_ALIGNED_TYPE(struct, 4) ai_layer_tree_ensemble_classifier_ { AI_LAYER_COMMON_FIELDS_DECLARE func_nl nl_func; uint8_t all_weights_are_positive; ai_float nodes_values_scale; ai_float nodes_values_offset; ai_float class_weights_scale; ai_float class_weights_offset; } ai_layer_tree_ensemble_classifier; /******************************************************************************/ /* Forward Functions Section */ /******************************************************************************/ /*! * @brief Decodes the TreeEnsembleClassifier ML operator. * @ingroup layers_svmreg * @param layer tree ensemble classifier layer */ AI_INTERNAL_API void forward_tree_ensemble_classifier(ai_layer *pLayer); AI_INTERNAL_API ai_i32 decodeEstimator_LEQ_8Bits(const ai_float *pDataIn, ai_float *pOutDataScores, const ai_u8 *pFeatureIdxForEstimator, const ai_float *pValuesForEstimator, const ai_u8 *pTrueIdxForEstimator, const ai_u8 *pFalseIdxForEstimator, const ai_handle pClassWeightsForEstimator, const ai_array_format classWeightsFormat, const ai_u8 *pClassNodeIdsForEstimator, const ai_u16 nbClassWithCurrentEstimator, const ai_u8 *pClassIdsForEstimator); AI_INTERNAL_API ai_i32 decodeEstimator_LEQ_16Bits(const ai_float *pDataIn, ai_float *pOutDataScores, const ai_u8 *pFeatureIdxForEstimator, const ai_float *pValuesForEstimator, const ai_u16 *pTrueIdxForEstimator, const ai_u16 *pFalseIdxForEstimator, ai_handle pClassWeightsForEstimator, const ai_array_format classWeightsFormat, const ai_u16 *pClassNodeIdsForEstimator, const ai_u16 nbClassWithCurrentEstimator, const ai_u16 *pClassIdsForEstimator); AI_API_DECLARE_END #endif /*LAYERS_TREE_ENSEMBLE_CLASSIFIER_H*/
4,238
C
36.513274
80
0.542237
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/Middlewares/ST/AI/Inc/lite_dense_is8os8ws8.h
/** ****************************************************************************** * @file lite_dense_is8os8ws8.h * @author Marco Forleo * @brief header file of AI platform lite dense kernel datatypes ****************************************************************************** * @attention * * Copyright (c) 2022 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** @verbatim @endverbatim ****************************************************************************** */ #ifndef LITE_DENSE_IS8OS8WS8_H #define LITE_DENSE_IS8OS8WS8_H #pragma once #include "ai_lite_interface.h" /******************************************************************************/ /* Forward Functions Section */ /******************************************************************************/ /*! * @brief Forward function for a dense layer with signed input, * signed output and signed weights all at 8 bits. * @ingroup lite_dense_is8os8ws8 * @param input The pointer to input buffer. * @param output The pointer to output buffer. * @param weights The pointer to weights. * @param bias The pointer to bias (NULL if not available). * @param in_zeropoint The value of the zero point of the input. * @param out_zeropoint TThe value of the zero point of the output. * @param n_channel_in The number of channels of the input. * @param n_channel_out The number of channels of the output, i.e., * the number of dense hidden neurons. * @param n_pixels Total number of pixels. */ LITE_API_ENTRY void forward_lite_dense_is8os8ws8(ai_i8 * pDataOut, const ai_i8 *pDataIn, const ai_i8 *pWeights, const ai_i32 *pBias, const ai_i8 in_zeropoint, const ai_i8 out_zeropoint, const ai_u16 n_channel_in, const ai_u16 n_channel_out, const ai_size n_pixels, const ai_float in_scale, const ai_float out_scale, const ai_float Wt_scale, ai_i16 *pBuffer_a); void forward_lite_dense_is8os8ws8_ch(ai_i8 * pDataOut, const ai_i8 *pDataIn, const ai_i8 *pWeights, const ai_i32 *pBias, const ai_i8 in_zeropoint, const ai_i8 out_zeropoint, const ai_u16 n_channel_in, const ai_u16 n_channel_out, const ai_size n_pixels, const ai_float in_scale, const ai_float out_scale, const ai_float *pWt_scale, ai_i16 *pBuffer_a); #endif /*LITE_DENSE_IS8OS8WS8_H*/
3,465
C
44.605263
80
0.43088
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/Middlewares/ST/AI/Inc/ai_platform_interface.h
/** ****************************************************************************** * @file ai_platform_interface.h * @author AST Embedded Analytics Research Platform * @brief Definitions of AI platform interface APIs types ****************************************************************************** * @attention * * Copyright (c) 2018 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** @verbatim @endverbatim ****************************************************************************** */ #ifndef AI_PLATFORM_INTERFACE_H #define AI_PLATFORM_INTERFACE_H #pragma once #include "ai_platform.h" #include "datatypes_network.h" #include "ai_datatypes.h" #include "ai_datatypes_format.h" /*! * @defgroup datatypes_interface Interface Datatypes * @brief Data structures and defines used to implement neural networks */ /******************************************************************************/ #define AI_ERROR_TRAP(net_, type_, code_) \ ai_platform_network_set_error((net_), AI_CONCAT(AI_ERROR_,type_), \ AI_CONCAT(AI_ERROR_CODE_,code_)) /*! AI_PTR HANDLERS SECTION ************************************/ #define AI_PTR(ptr_) AI_CAST(ai_ptr, ptr_) #define AI_PTR_CONST(ptr_) AI_CAST(ai_ptr_const, ptr_) /*! STATIC ARRAYS ALLOCATOR SECTION ************************************/ #define AI_PACK_STORAGE_ARRAY(type_, dim_, ...) \ (type_[dim_]) { AI_PACK(__VA_ARGS__) } /*! AI_STORAGE_KLASS SECTION ************************************/ #define AI_STORAGE_KLASS_PACK(type_, dim_, ...) \ AI_PACK_STORAGE_ARRAY(type_, dim_, __VA_ARGS__) #define AI_STORAGE_KLASS_INIT(type_, size_, data_) \ { \ .type = (type_), \ .size = (size_), \ .data = (ai_handle)(data_), \ } /*! * @enum ai_storage_klass_type * @ingroup ai_platform_interface * @brief @ref ai_storage_class types enum */ typedef enum { AI_STORAGE_KLASS_NONE = 0x00, AI_STORAGE_KLASS_SHAPE = 0x01, AI_STORAGE_KLASS_STRIDE = 0x02, } ai_storage_klass_type; /*! * @struct ai_storage_klass * @ingroup ai_platform_interface * @brief Generic "Template" klass for generic storage arrays containers * from this klass several typed containers are derived (see e.g. @ref ai_shape) */ AI_PACKED_STRUCT_START typedef AI_ALIGNED_TYPE(struct, 4) AI_PACKED ai_storage_klass_s { ai_u32 type : 8; ai_u32 size : 24; ai_handle data; } ai_storage_klass; AI_PACKED_STRUCT_END /*! AI_SHAPES SECTION ************************************/ #define AI_SHAPE_MAX_DIMENSION (6) #define AI_SHAPE_2D_INIT(w_, h_) \ { .data = { (w_), (h_) } } #define AI_SHAPE_INIT(dim_, ...) \ AI_STORAGE_KLASS_INIT( \ AI_STORAGE_KLASS_SHAPE, \ dim_, \ AI_STORAGE_KLASS_PACK(ai_shape_dimension, dim_, ## __VA_ARGS__)) #define AI_SHAPE_INIT_FROM_BUFFER(dim_, buffer_) \ AI_STORAGE_KLASS_INIT( \ AI_STORAGE_KLASS_SHAPE, \ dim_, \ buffer_) #define AI_SHAPE_ALLOCATE_STATIC(num_dim_) \ AI_SHAPE_INIT((num_dim_), 0) typedef ai_u8 ai_shape_idx; /*! * @struct ai_shape * @ingroup ai_platform_interface * @brief Dimensions for generic 4D tensors */ typedef ai_storage_klass ai_shape; /*! AI_STRIDES HANDLERS SECTION ************************************/ #define AI_STRIDE_INIT(dim_, ...) \ AI_STORAGE_KLASS_INIT( \ AI_STORAGE_KLASS_STRIDE, \ dim_, \ AI_STORAGE_KLASS_PACK(ai_stride_dimension, dim_, ## __VA_ARGS__)) #define AI_STRIDE_INIT_FROM_BUFFER(dim_, buffer_) \ AI_STORAGE_KLASS_INIT( \ AI_STORAGE_KLASS_STRIDE, \ dim_, \ buffer_) #define AI_STRIDE_ALLOCATE_STATIC(num_dims_) \ AI_STRIDE_INIT((num_dims_), 0) /*! * @struct ai_stride * @ingroup ai_platform_interface * @brief Stride dimensions for generic 4D tensors (in number of elements) */ typedef ai_storage_klass ai_stride; /*! BASIC_TYPES HANDLERS SECTION ************************************/ #define AI_SIZE(value_) \ AI_CAST(ai_size, value_) /*! AI_KLASS_OBJ HANDLERS SECTION ************************************/ #define AI_KLASS_OBJ(obj_) \ AI_CAST(ai_klass_obj, obj_) /*! GENERIC HANDLERS SECTION ************************************/ #define AI_OBJ_DATA(obj_, type_) \ AI_CAST(type_, (obj_)->data) /*! AI_BUFFER HANDLERS SECTION ************************************/ #define AI_BUFFER_OBJ(ptr_) \ AI_CAST(ai_buffer*, ptr_) /*! AI_ARRAY HANDLERS SECTION ************************************/ #define AI_ARRAY_OBJ(ptr_) \ AI_CAST(ai_array*, ptr_) #define AI_ARRAY_OBJ_INIT_STATIC(type_, format_, size_, ...) { \ .format = AI_FMT_OBJ(format_), \ .size = (ai_array_size)(size_), \ .data = (ai_ptr)((type_[]){ __VA_ARGS__ }), \ .data_start = AI_PTR(0), \ } #define AI_ARRAY_OBJ_INIT(format_, data_, data_start_, size_) { \ .format = AI_FMT_OBJ(format_), \ .size = AI_CAST(ai_array_size, size_), \ .data = AI_PTR(data_), \ .data_start = AI_PTR(data_start_) } #define AI_ARRAY_OBJ_DECLARE_STATIC(name_, type_, format_, attr_, size_, ...) \ AI_ALIGNED(4) \ attr_ ai_array name_ = AI_ARRAY_OBJ_INIT_STATIC(type_, format_, size_, __VA_ARGS__); #define AI_ARRAY_OBJ_DECLARE(name_, format_, data_, data_start_, size_, attr_) \ AI_ALIGNED(4) \ attr_ ai_array name_ = AI_ARRAY_OBJ_INIT(format_, data_, data_start_, size_); /********************************* ai_array macros ***************************/ #define AI_PACK_ARRAYS(...) \ (ai_array[]) { AI_PACK(__VA_ARGS__) } #define AI_ARRAY_LIST_OBJ_INIT(arrays_ptr_) \ ((ai_array*)(arrays_ptr_)) #define AI_ARRAY_LIST_FLAGS(list_) \ ((list_) ? (list_)->flags : 0x0) #define AI_ARRAY_LIST_SIZE(list_) \ ((list_) ? (list_)->size : 0) #define AI_ARRAY_LIST_DATA(list_, pos_) \ ((list_) ? &((list_)->data[pos_]) : NULL) /********************************* ai_tensor macros **************************/ #define AI_TENSOR_OBJ(obj_) \ AI_CAST(ai_tensor*, obj_) #define AI_TENSOR_INFO_OBJ_INIT(id_, flags_, data_size_) { \ .id = (id_), \ .flags = (flags_), \ .data_size = (data_size_) \ } #define AI_TENSOR_OBJ_INIT(id_, flags_, shape_, stride_, arrays_size_, arrays_ptr_, klass_obj_) { \ .klass = (ai_klass_obj)(klass_obj_), \ .info = AI_TENSOR_INFO_OBJ_INIT(id_, flags_, arrays_size_), \ .shape = shape_, \ .stride = stride_, \ .data = AI_ARRAY_LIST_OBJ_INIT(AI_PACK(arrays_ptr_)), \ } #define AI_TENSOR_OBJ_DECLARE(name_, attr_, id_, flags_, shape_, stride_, \ arrays_size_, arrays_ptr_, klass_obj_) \ AI_ALIGNED(4) \ attr_ ai_tensor name_ = AI_TENSOR_OBJ_INIT(id_, flags_, AI_PACK(shape_), AI_PACK(stride_), \ arrays_size_, AI_PACK(arrays_ptr_), AI_PACK(klass_obj_)); /********************************* TENSOR STATE MACROS ***********************/ #define AI_TENSOR_STATE_OBJ_INIT(end_ptr_ , curr_ptr_, stride_, size_) \ { (end_ptr_), (curr_ptr_), (stride_), (size_) } /********************************* TENSOR LIST MACROS ************************/ #if (AI_TOOLS_API_VERSION <= AI_TOOLS_API_VERSION_1_3) #pragma message ("Including deprecated AI_TENSOR_LIST_ENTRY, AI_TENSOR_LIST_EMPTY, AI_TENSOR_LIST_IO_ENTRY") AI_DEPRECATED #define AI_TENSOR_LIST_EMPTY \ AI_TENSOR_LIST_OBJ_EMPTY AI_DEPRECATED #define AI_TENSOR_LIST_ENTRY(...) \ AI_TENSOR_LIST_OBJ_INIT(AI_FLAG_NONE, AI_NUMARGS(__VA_ARGS__), __VA_ARGS__) AI_DEPRECATED #define AI_TENSOR_LIST_IO_ENTRY(flags_, size_, ...) \ AI_TENSOR_LIST_IO_OBJ_INIT(flags_, size_, __VA_ARGS__) #endif /* AI_TOOLS_API_VERSION_1_3 */ #define AI_TENSOR_LIST_OBJ_INIT(flags_, size_, ...) \ { .size = (size_), .flags = (flags_), \ .tensor = (ai_tensor*[]) { __VA_ARGS__ }, .info = NULL \ } #define AI_TENSOR_LIST_OBJ_EMPTY \ { .size = 0, .flags = AI_FLAG_NONE, \ .tensor = (ai_tensor*[]) { NULL }, .info = NULL \ } #define AI_TENSOR_LIST_OBJ_DECLARE(name_, attr_, flags_, size_, ...) \ AI_ALIGNED(4) \ attr_ ai_tensor_list name_ = AI_TENSOR_LIST_OBJ_INIT( \ flags_, size_, __VA_ARGS__); /********************************* TENSOR LIST I/O MACROS ********************/ #define AI_TENSOR_LIST_IO_OBJ_INIT(flags_, size_, ...) \ { .size = (size_), .flags = (flags_), \ .tensor = (ai_tensor*[]) { __VA_ARGS__ }, \ .info = (ai_tensor_list_info[1]) { { \ .buffer = (ai_buffer[size_]){AI_STRUCT_INIT}, \ .state = (ai_tensor_state[size_]){AI_STRUCT_INIT}, \ .meta = (ai_buffer_meta_info[size_]){AI_STRUCT_INIT} \ } } \ } /********************************* TENSOR CHAIN MACROS ***********************/ #define AI_TENSOR_CHAIN_OBJ_INIT(flags_, size_, ...) \ { .size = (size_), .flags = (flags_), \ .chain = (ai_tensor_list[]){ __VA_ARGS__ } } #define AI_TENSOR_CHAIN_OBJ_DECLARE(name_, attr_, size_, ...) \ AI_ALIGNED(4) \ attr_ ai_tensor_chain name_ = \ AI_TENSOR_CHAIN_OBJ_INIT(AI_FLAG_NONE, size_, __VA_ARGS__); /********************************* TENSOR CHAIN I/O MACROS *******************/ #define AI_TENSOR_CHAIN_IO_OBJ_INIT(flags_, in_tensor_list_, out_tensor_list_) \ { .chain = (ai_tensor_list[]){ in_tensor_list_, out_tensor_list_ }, \ .size = 2, .flags = (flags_) } #define AI_TENSOR_CHAIN_IO_OBJ_DECLARE( \ name_, attr_, flags_, in_tensor_list_, out_tensor_list_) \ AI_ALIGNED(4) \ attr_ ai_tensor_chain_io name_ = \ AI_TENSOR_CHAIN_IO_OBJ_INIT(flags_, in_tensor_list_, out_tensor_list_); /******************************* NETWORK SECTION ****************************/ #define AI_NETWORK_OBJ(obj_) \ ((ai_network*)(obj_)) #if (AI_TOOLS_API_VERSION < AI_TOOLS_API_VERSION_1_5) AI_DEPRECATED #define AI_NETWORK_OBJ_INIT( \ weights_buffer_, activations_buffer_, \ in_tensor_list_ptr_, out_tensor_list_ptr_, \ in_node_ptr_, signature_, klass_obj_) { \ .magic = 0x0, \ .signature = signature_, \ .klass = AI_KLASS_OBJ(klass_obj_), \ .flags = AI_FLAG_NONE, \ .error = AI_ERROR_INIT(NONE, NONE), \ .n_batches = 0, \ .batch_id = 0, \ .buffers = AI_NETWORK_BUFFERS_INIT( \ AI_BUFFER_ARRAY_OBJ_INIT_STATIC(AI_FLAG_NONE, 1, AI_PACK(weights_buffer_)), \ AI_BUFFER_ARRAY_OBJ_INIT_STATIC(AI_FLAG_NONE, 1, AI_PACK(activations_buffer_))), \ .tensors = AI_TENSOR_CHAIN_IO_OBJ_INIT(AI_FLAG_NONE, \ AI_PACK(in_tensor_list_ptr_), \ AI_PACK(out_tensor_list_ptr_)), \ .input_node = AI_NODE_OBJ(in_node_ptr_), \ .current_node = AI_NODE_OBJ(NULL), \ .on_node_exec = NULL, \ .data_exec = NULL, \ .lite_cb = NULL, \ } #else #define AI_NETWORK_OBJ_INIT( \ weights_buffer_, activations_buffer_, \ in_tensor_list_ptr_, out_tensor_list_ptr_, \ in_node_ptr_, signature_, klass_obj_) { \ .magic = 0x0, \ .signature = signature_, \ .klass = AI_KLASS_OBJ(klass_obj_), \ .flags = AI_FLAG_NONE, \ .error = AI_ERROR_INIT(NONE, NONE), \ .n_batches = 0, \ .batch_id = 0, \ .buffers = AI_NETWORK_BUFFERS_INIT(AI_PACK(weights_buffer_), \ AI_PACK(activations_buffer_)), \ .tensors = AI_TENSOR_CHAIN_IO_OBJ_INIT(AI_FLAG_NONE, \ AI_PACK(in_tensor_list_ptr_), \ AI_PACK(out_tensor_list_ptr_)), \ .input_node = AI_NODE_OBJ(in_node_ptr_), \ .current_node = AI_NODE_OBJ(NULL), \ .on_node_exec = NULL, \ .data_exec = NULL, \ .lite_cb = NULL, \ } #endif // AI_TOOLS_API_VERSION #define AI_NETWORK_OBJ_DECLARE( \ name_, attr_, \ weights_buffer_, activations_buffer_, \ in_tensor_list_ptr_, out_tensor_list_ptr_, \ in_node_ptr_, signature_, klass_obj_) \ AI_ALIGNED(4) \ attr_ ai_network name_ = AI_NETWORK_OBJ_INIT( \ AI_PACK(weights_buffer_), \ AI_PACK(activations_buffer_), \ AI_PACK(in_tensor_list_ptr_), \ AI_PACK(out_tensor_list_ptr_), \ (in_node_ptr_), (signature_), (klass_obj_)); #define AI_NETWORK_ACQUIRE_CTX(handle_) \ AI_NETWORK_OBJ(ai_platform_context_acquire(handle_)) /******************************************************************************/ AI_API_DECLARE_BEGIN /*! * @typedef ai_version * @ingroup ai_platform_interface * @brief Packed representation for @ref ai_platform_version */ typedef uint32_t ai_version; /*! * @typedef ai_klass_obj * @ingroup ai_platform_interface * @brief handler to (private) generic subclass derivatives implementation */ typedef void* ai_klass_obj; /*! * @typedef ai_ptr * @ingroup ai_platform_interface * @brief Byte pointer data addressing */ typedef uint8_t* ai_ptr; /*! * @typedef ai_ptr_const * @ingroup ai_platform_interface * @brief Constant byte pointer data addressing */ typedef const uint8_t* ai_ptr_const; /*! * @typedef ai_ptr_offset * @ingroup ai_platform_interface * @brief byte offset for computing strides */ typedef int32_t ai_ptr_offset; /*! * @typedef ai_magic * @ingroup ai_platform_interface * @brief magic field to mark internal datatstructures */ typedef uint32_t ai_magic; /*! * @typedef ai_any_ptr * @ingroup ai_platform_interface * @brief union for defining any pointer */ typedef union { ai_handle handle; ai_ptr ptr; ai_float* float32; ai_double* float64; ai_u8* u8; ai_i8* s8; ai_u16* u16; ai_i16* s16; ai_u32* u32; ai_i32* s32; ai_u64* u64; ai_i64* s64; } ai_any_ptr; #define AI_ANY_PTR_INIT(ptr_) \ { .handle = (ai_handle)(ptr_) } #define AI_CONTEXT_FIELDS \ ai_magic magic; /*!< magic word to mark valid contexts datastructs*/ \ ai_signature signature; /*!< 32bit signature for network consistency checks */ #define AI_CONTEXT_OBJ(obj) ((ai_context*)(obj)) /*! * @typedef ai_context * @ingroup ai_platform_interface * @brief Abstract internal context header exposed to codegen interface */ AI_PACKED_STRUCT_START typedef AI_ALIGNED_TYPE(struct, 4) AI_PACKED ai_context_ { AI_CONTEXT_FIELDS } ai_context; AI_PACKED_STRUCT_END /*! * @enum ai_shape_2d_type * @ingroup ai_platform_interface * @brief Codes for the 2D tensor dimensions */ typedef enum { AI_SHAPE_2D_MAX_DIMENSION = 0x2, AI_SHAPE_2D_HEIGHT = 0x1, AI_SHAPE_2D_WIDTH = 0x0, } ai_shape_2d_type; /*! * @struct ai_shape_2d * @ingroup ai_platform_interface * @brief Dimensions for generic 2D tensors */ AI_PACKED_STRUCT_START typedef AI_ALIGNED_TYPE(struct, 4) AI_PACKED ai_shape_2d_s { ai_shape_dimension data[AI_SHAPE_2D_MAX_DIMENSION]; /*!< 2D tensor dimensions */ } ai_shape_2d; AI_PACKED_STRUCT_END /*! * @struct ai_array * @ingroup ai_platform_interface * @brief Generic flattened array with size * and (byte) stride of each item */ AI_PACKED_STRUCT_START typedef AI_ALIGNED_TYPE(struct, 4) AI_PACKED ai_array_s { ai_array_format format; /*!< array format (see @ref ai_array_format) */ ai_array_size size; /*!< number of elements in the array (NOT number of bytes!). The size of the array could be determine using @ref AI_ARRAY_GET_BYTE_SIZE macro */ ai_ptr data; /*!< pointer to data */ ai_ptr data_start; /*!< pointer to parent's data start address */ } ai_array; AI_PACKED_STRUCT_END /*! * @struct ai_tensor_info * @ingroup ai_platform_interface * @brief ai_tensor_info info structure for storing size of the array list, * tensor dimensionality, etc. * */ AI_PACKED_STRUCT_START typedef AI_ALIGNED_TYPE(struct, 4) AI_PACKED ai_tensor_info_s { ai_u16 id; ai_u8 flags; ai_u8 data_size; } ai_tensor_info; AI_PACKED_STRUCT_END /*! * @struct ai_tensor * @ingroup ai_platform_interface * @brief Generic tensor structure for storing parameters and activations * * The data is stored in a flattened array with an implicit order given by the * reverse order in @ref ai_shape_dimension: * in_channels, channels, width, height. */ AI_PACKED_STRUCT_START typedef AI_ALIGNED_TYPE(struct, 4) AI_PACKED ai_tensor_s { ai_klass_obj klass; /*!< opaque pointer to klass context */ ai_tensor_info info; /*!< tensor info metadata see @ref ai_tensor_info)*/ ai_shape shape; /*!< tensor shape see @ref ai_shape */ ai_stride stride; /*!< tensor stride see @ref ai_stride */ ai_array* data; /*!< flattened array pointer to tensor data */ } ai_tensor; AI_PACKED_STRUCT_END /*! * @struct ai_tensor_state * @ingroup ai_platform_interface * @brief state context for tensor management (used for I/O network tensors) */ AI_PACKED_STRUCT_START typedef AI_ALIGNED_TYPE(struct, 4) AI_PACKED ai_tensor_state_s { ai_ptr end_ptr; /*!< end address of the I/O tensor data buffer */ ai_ptr curr_ptr; /*!< current address of the I/O tensor data buffer (for batching) */ ai_ptr_offset stride; /*!< single batch buffer size (in bytes) */ ai_size size; /*!< total size in bytes of the I/O tensor buffer */ } ai_tensor_state; AI_PACKED_STRUCT_END /*! * @struct ai_tensor_list_info * @ingroup ai_platform_interface * @brief info metadata for tensor list management (used for I/O network tensors) */ AI_PACKED_STRUCT_START typedef AI_ALIGNED_TYPE(struct, 4) AI_PACKED ai_tensor_list_info_s { ai_tensor_state* state; /*!< I/O buffer internal pointers state */ ai_buffer* buffer; /*!< I/O buffer pointer */ ai_buffer_meta_info* meta; /*!< I/O buffer meta informations */ } ai_tensor_list_info; AI_PACKED_STRUCT_END /********************************* INTEGER QUANTIZATION DATATYPES ************/ #define AI_INTQ_INFO_OBJ_INIT(flags_, scale_ , zeropoint_) { \ .scale = (scale_), \ .zeropoint = (ai_handle)(zeropoint_), \ .flags = (flags_), \ } #define AI_PACK_INTQ_INFO_LIST(...) \ (ai_intq_info_list[]) { AI_PACK(__VA_ARGS__) } #define AI_PACK_INTQ_INFO(scale_, zp_) \ (INTQ_CONST ai_intq_info[1]) { { \ .scale = (INTQ_CONST ai_float*) AI_PACK(scale_), \ .zeropoint = (ai_handle) AI_PACK(zp_) \ } } #define AI_PACK_INTQ_SCALE(...) \ (INTQ_CONST ai_float[]) { AI_PACK(__VA_ARGS__) } #define AI_PACK_INTQ_ZP(...) \ (INTQ_CONST ai_i8[]) { AI_PACK(__VA_ARGS__) } #define AI_PACK_UINTQ_ZP(...) \ (INTQ_CONST ai_u8[]) { AI_PACK(__VA_ARGS__) } #define AI_PACK_INTQ_ZP16(...) \ (INTQ_CONST ai_i16[]) { AI_PACK(__VA_ARGS__) } #define AI_PACK_UINTQ_ZP16(...) \ (INTQ_CONST ai_u16[]) { AI_PACK(__VA_ARGS__) } #define AI_INTQ_INFO_LIST_OBJ_INIT(flags_, size_, info_) \ { \ .flags = (flags_), \ .size = (size_), \ .info = (info_), \ } #define AI_INTQ_INFO_LIST_OBJ_EMPTY { 0 } #define AI_INTQ_INFO_LIST_OBJ_DECLARE(name_, attr_, flags_, size_, info_) \ AI_ALIGNED(4) \ attr_ ai_intq_info_list name_ = \ AI_INTQ_INFO_LIST_OBJ_INIT(flags_, size_, AI_PACK(info_)); #define AI_INTQ_INFO_LIST_OBJ_DECLARE_EMPTY(name_, attr_) \ AI_ALIGNED(4) \ attr_ ai_intq_info_list name_ = AI_INTQ_INFO_LIST_OBJ_EMPTY; /********************************* TENSOR CHAINS DATATYPES *******************/ /*! * @enum ai_tensor_chain_type * @ingroup ai_platform_interface * @brief Enum for the different tensor chains supported in the library */ typedef enum { AI_TENSOR_CHAIN_INPUT = 0x0, AI_TENSOR_CHAIN_OUTPUT = 0x1, AI_TENSOR_CHAIN_WEIGHTS = 0x2, AI_TENSOR_CHAIN_SCRATCH = 0x3, AI_TENSOR_CHAIN_SIZE } ai_tensor_chain_type; /*! * @struct ai_tensor_list * @ingroup ai_platform_interface * @brief list (in form of arrays) of internal nodes tensor pointers */ AI_PACKED_STRUCT_START typedef AI_ALIGNED_TYPE(struct, 4) AI_PACKED ai_tensor_list_s { ai_u16 size; /*!< number of elements in the the tensor list */ ai_u16 flags; /*!< optional flags to store tensor list attributes */ ai_tensor** tensor; /*!< array of linked tensor pointer */ ai_tensor_list_info* info; /*!< pointer to an array of metainfo associated to the tensors */ } ai_tensor_list; AI_PACKED_STRUCT_END /*! * @struct ai_tensor_chain * @ingroup ai_platform_interface * @brief tensor chain datastruct for internal network nodes */ AI_PACKED_STRUCT_START typedef AI_ALIGNED_TYPE(struct, 4) AI_PACKED ai_tensor_chain_s { ai_u16 size; ai_u16 flags; ai_tensor_list* chain; /*!< pointer to a 4 sized array see @ref ai_tensor_chain_type */ } ai_tensor_chain; AI_PACKED_STRUCT_END /************************************** LAYER DATATYPES *******************/ /*! * @struct ai_layer * @ingroup ai_platform_interface * @brief Structure encoding a generic opaque layer in the network * */ typedef void ai_layer; /************************************** OBSERVER DATATYPES *******************/ /* forward function */ struct ai_node_s; /*! * @struct ai_observer_node * @ingroup ai_observer_interface * @brief observer node data struct for internal network nodes */ AI_PACKED_STRUCT_START typedef AI_ALIGNED_TYPE(struct, 4) AI_PACKED ai_observer_node_s { ai_u16 c_idx; /*!< node index (position in the execution list) */ ai_u16 type; /*!< node type info @see ai_node datastruct */ ai_u16 id; /*!< node id assigned by codegen tool to identify the model layer*/ ai_u16 unused; /*!< unused field for alignment */ const ai_tensor_chain* inner_tensors; /*!< pointer to the inner tensor if available */ const ai_tensor_chain* tensors; /*!< pointer to a 4 sized array see @ref ai_tensor_chain_type */ } ai_observer_node; AI_PACKED_STRUCT_END #define AI_OBSERVER_NONE_EVT (0) /*!< No event */ #define AI_OBSERVER_INIT_EVT (1 << 0) /*!< called at the end of the init function */ #define AI_OBSERVER_PRE_EVT (1 << 1) /*!< before c-node execution */ #define AI_OBSERVER_POST_EVT (1 << 2) /*!< after c-node execution */ #define AI_OBSERVER_FIRST_EVT (1 << 8) /*!< indicate the first c-node */ #define AI_OBSERVER_LAST_EVT (1 << 9) /*!< indicate the last c-node */ #define AI_OBSERVER_REGISTERED (1 << 24) /*!< internal flag */ #define AI_OBSERVER_MASK_EVT (0xFF) /*!< mask for requested user event */ /* Client callback definition */ typedef ai_u32 (*ai_observer_node_cb)( const ai_handle cookie, const ai_u32 flags, const ai_observer_node *node); AI_PACKED_STRUCT_START typedef AI_ALIGNED_TYPE(struct, 4) AI_PACKED ai_observer_exec_ctx_s { ai_observer_node_cb on_node; /*!< registered user observer call-back function */ ai_handle cookie; /*!< reference of the user context */ ai_u32 flags; /*!< flags definition */ ai_u16 c_idx; /*!< store/indicate the index of the current c_node */ ai_u16 n_nodes; /*!< total number of c_node */ struct ai_node_s *cur; /*!< pointer of the current node (pre or post) */ } ai_observer_exec_ctx; AI_PACKED_STRUCT_END typedef enum { AI_NODE_EXEC_INIT = 0x0, AI_NODE_EXEC_START = 0x1, AI_NODE_EXEC_PRE = 0x2, AI_NODE_EXEC_POST = 0x3, } ai_node_exec_state; /* Internal/private definition of node execution callback */ typedef ai_u32 (*ai_node_exec_cb)( const ai_node_exec_state state, struct ai_node_s *cur, const ai_handle ctx); /********************************* NETWORK DATATYPES *************************/ /*! * @struct ai_network * @ingroup layers * @brief Structure encoding a sequential neural network */ AI_PACKED_STRUCT_START typedef AI_ALIGNED_TYPE(struct, 4) AI_PACKED ai_network_s { AI_CONTEXT_FIELDS ai_klass_obj klass; /*!< opaque handler to specific network implementations */ ai_flags flags; /*!< bitflags mask to track some network state info */ ai_error error; /*!< track 1st error code in the network */ ai_u16 n_batches; /*!< number of batches to process */ ai_u16 batch_id; /*!< current batch to to process btw [0, n_batches)*/ // New 6.1 context storing explicitly network buffers. This allow also management of network persistent state now ai_network_buffers buffers; /*!< network buffers datastruct */ ai_tensor_chain tensors; /*!< I/O tensor chain list see @ref ai_tensor_list */ struct ai_node_s* input_node; /*!< first node to execute */ struct ai_node_s* current_node; /*!< current node to execute */ ai_node_exec_cb on_node_exec; /*!< registered call-back function called when a node/operator is scheduled */ ai_handle data_exec; /*!< private reference for the runtime context */ ai_handle lite_cb; /*!< registered opaque call-back handler for lite APIs */ ai_version tool_api_version; /*! Tools Codegen API version */ } ai_network; AI_PACKED_STRUCT_END /*! * @brief Get platform runtime lib revision version as string. * @ingroup ai_platform_interface * @return a string containing the revision of the runtime library */ AI_INTERFACE_TYPE const char* ai_platform_runtime_get_revision(void); /*! * @brief Get platform runtime lib version as datastruct. * @ingroup ai_platform_interface * @return a datastruct containing the version of the runtime library */ AI_INTERFACE_TYPE ai_platform_version ai_platform_runtime_get_version(void); /*! * @brief Get platform public APIs version as datastruct. * @ingroup ai_platform_interface * @return a datastruct containing the version of the public APIs */ AI_INTERFACE_TYPE ai_platform_version ai_platform_api_get_version(void); /*! * @brief Get platform interface private APIs version as datastruct. * @ingroup ai_platform_interface * @return a datastruct containing the version of the interface private APIs */ AI_INTERFACE_TYPE ai_platform_version ai_platform_interface_api_get_version(void); /**************************************************************************** ** Context APIs ****************************************************************************/ /*! * @brief Get platform context. * @ingroup ai_platform_interface * @return a valid context handle or NULL otherwise */ AI_INTERFACE_TYPE ai_context* ai_platform_context_acquire(const ai_handle handle); /*! * @brief Release platform context. * @ingroup ai_platform_interface * @return an opaque handle to the released object */ AI_INTERFACE_TYPE ai_handle ai_platform_context_release(ai_context* ctx); /**************************************************************************** ** Platform Network Params APIs ****************************************************************************/ /*! * @brief get the weights map from user provided network params info * @ingroup ai_platform_interface * @param params a pointer to ai_network_params struct * @param map table pointer to the table map to initialize * @param map_size the number of entries of the table to initialize * @return true if initialization succeeded, false otherwise */ AI_INTERFACE_TYPE ai_bool ai_platform_get_weights_map( ai_ptr* map, const ai_size map_size, const ai_network_params* params); /*! * @brief get the activations map from user provided network params info * @ingroup ai_platform_interface * @param params a pointer to ai_network_params struct * @param map table pointer to the table map to initialize * @param map_size the number of entries of the table to initialize * @return true if initialization succeeded, false otherwise */ AI_INTERFACE_TYPE ai_bool ai_platform_get_activations_map( ai_ptr* map, const ai_size map_size, const ai_network_params* params); /*! * @brief bind code generated weights and activations map arrays to ai_netwoek_params * @ingroup ai_platform_interface * @param[out] params the network params struct reporting binded params * @param[in] map_weights pointer to the codegened weights map array to be bound * @param[in] map_activations pointer to the codegened activation map array to be bound * @return true if network parameters binding succeed, false otherwise */ AI_INTERFACE_TYPE ai_bool ai_platform_bind_network_params( ai_network_params* params, const ai_buffer_array* map_weights, const ai_buffer_array* map_activations); /**************************************************************************** ** Platform Network APIs ****************************************************************************/ /*! * @brief get **first** error tracked when using the network * @ingroup ai_platform_interface * @param network an opaque handler to the network context * @return ai_error the FIRST error generated during network processing */ AI_INTERFACE_TYPE ai_error ai_platform_network_get_error(ai_handle network); /*! * @brief Set specific error code of the network. if an error is already present * keep it * @ingroup ai_platform_interface * @param net_ctx a pointer to the network context * @param type error type as defined in @ref ai_error_type * @param code error code as defined in @ref ai_error_code * @return true if no previous errors where recorded, false if a previous error * is present or context is invalid */ AI_INTERFACE_TYPE ai_bool ai_platform_network_set_error( ai_network* net_ctx, const ai_error_type type, const ai_error_code code); /*! * @brief Finalize network report datastruct with I/O buffer infos * @ingroup ai_platform_interface * @return bool if the report has been finalized correctly. false otherwise */ AI_INTERFACE_TYPE ai_bool ai_platform_api_get_network_report( ai_handle network, ai_network_report* r); /*! * @brief Get network inputs array pointer as a ai_buffer array pointer. * @ingroup network * @param network an opaque handler to the network context * @param n_buffer optional parameter to return the number of inputs * @return a ai_buffer pointer to the inputs arrays */ AI_INTERFACE_TYPE ai_buffer* ai_platform_inputs_get(ai_handle network, ai_u16 *n_buffer); /*! * @brief Get network outputs array pointer as a ai_buffer array pointer. * @ingroup network * @param network an opaque handler to the network context * @param n_buffer optional parameter to return the number of outputs * @return a ai_buffer pointer to the inputs arrays */ AI_INTERFACE_TYPE ai_buffer* ai_platform_outputs_get(ai_handle network, ai_u16 *n_buffer); /*! * @brief create a network context with some error check * @ingroup ai_platform_interface * @param a pointer to an opaque handle of the network context * @param an (optional) pointer to the network config buffer info * @param net_ctx a pointer to the network context structure to initialize * @param tool_major major version id of the tool used to generate the network * @param tool_minor minor version id of the tool used to generate the network * @param tool_micro micro version id of the tool used to generate the network * @return the error during network creation or error none if ok */ AI_INTERFACE_TYPE ai_error ai_platform_network_create( ai_handle* network, const ai_buffer* network_config, ai_network* net_ctx, const ai_u8 tool_major, const ai_u8 tool_minor, const ai_u8 tool_micro); /*! * @brief destroy a network context * @ingroup ai_platform_interface * @param network a pointer to an opaque handle of the network context * @return AI_HANDLE_NULL if deallocation OK, same network handle if failed */ AI_INTERFACE_TYPE ai_handle ai_platform_network_destroy(ai_handle network); /*! * @brief initialize the network context * @ingroup ai_platform_interface * @param network a pointer to an opaque handle of the network context * @return a valid network context, NULL if initialization failed */ AI_INTERFACE_TYPE ai_network* ai_platform_network_init( ai_handle network, const ai_network_params* params); /*! * @brief post-initialize of the network context. * @ingroup ai_platform_interface * @param network a pointer to an opaque handle of the network context * @return a valid network context, NULL if initialization failed */ AI_INTERFACE_TYPE ai_bool ai_platform_network_post_init(ai_handle network); /*! * @brief main platform runtime execute of a network * @ingroup ai_platform_interface * @param network an opaque handler to the network context * @param input a pointer to the input buffer data to process * @param output a pointer to the output buffer * @return the number of batches processed from the input. A result <=0 in case * of error */ AI_INTERFACE_TYPE ai_i32 ai_platform_network_process( ai_handle network, const ai_buffer* input, ai_buffer* output); /**************************************************************************** ** Observer APIs ****************************************************************************/ /*! * @brief Return the info of a requested c-node (defined by the * c_idx field). Should be called after the initialization phase. * @ingroup ai_platform_observer * @param network a pointer to an opaque handle of the network context * @param node_info a pointer to a reference of the node description * @return true if the node_info->c_idx designates a valid index else * false (network error is updated). */ AI_INTERFACE_TYPE ai_bool ai_platform_observer_node_info( ai_handle network, ai_observer_node *node_info); /*! * @brief Register an observer context. Allows to register a client CB which * will be called before or/and after the execution of a c-node with * the references of the used tensors (see @ref ai_observer_node). * @ingroup ai_platform_observer * @param network a pointer to an opaque handle of the network context * @param cb reference of the user callback function * @param cookie reference of a user object/ctx * @param flags indicate expected events (see AI_OBSERVER_XX_EVT flag definition) * @return false if the registration has failed (network error is updated) else true * of error. */ AI_INTERFACE_TYPE ai_bool ai_platform_observer_register( ai_handle network, ai_observer_node_cb cb, ai_handle cookie, ai_u32 flags); AI_INTERFACE_TYPE ai_bool ai_platform_observer_register_s(ai_handle network, ai_observer_exec_ctx *ctx); /*! * @brief un-register the observer context. * @ingroup ai_platform_observer * @param network a pointer to an opaque handle of the network context * @param ctx a pointer to a reference of the registered platform observer context * @param cb reference of the registered user callback function * @param cookie reference of the registered user object/ctx * @return false if the un-registration has failed (network error is updated) else true * of error. */ AI_INTERFACE_TYPE ai_bool ai_platform_observer_unregister(ai_handle network, ai_observer_node_cb cb, ai_handle cookie); AI_INTERFACE_TYPE ai_bool ai_platform_observer_unregister_s(ai_handle network, ai_observer_exec_ctx *ctx); AI_API_DECLARE_END #endif /*AI_PLATFORM_INTERFACE_H*/
35,318
C
33.091699
115
0.615012
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/Middlewares/ST/AI/Inc/core_common.h
/** ****************************************************************************** * @file core_common.h * @author AST Embedded Analytics Research Platform * @brief header file of common core datatypes ****************************************************************************** * @attention * * Copyright (c) 2018 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** @verbatim @endverbatim ****************************************************************************** */ #ifndef CORE_COMMON_H #define CORE_COMMON_H #pragma once #include "ai_platform.h" #include "ai_platform_interface.h" #include "core_datatypes.h" // #include "core_log.h" /*! * @defgroup core_common Common Core Library Routines * @brief Common macros, datatypes and routines of core common module * @details This module contains the definitons and handling of the @ref ai_node * datastructures. An ai_node is a generic abstraction for a network node that * could be either a fixed function layer or an operator. Ideally the platform * interface defined in api module should handle an process generic nodes in the * network, not relying on the fact that they are layers or operators datastructs * Specific implementative details should be kept inside layers and operators * modules. The core module implements additionally common routines used in the * layers and operators modules. */ /******************************************************************************/ #ifdef HAS_AI_ASSERT #define ASSERT_ARRAY_SANITY(a_) \ AI_ASSERT((a_) && (a_)->size>0) #define ASSERT_ARRAY_DATA_SANITY(a_) \ ASSERT_ARRAY_SANITY(a_) \ AI_ASSERT((a_)->data && (a_)->data_start) #define ASSERT_TENSOR_SANITY(t_) \ AI_ASSERT((t_) && (t_)->data) \ AI_ASSERT(CORE_TENSOR_GET_SHAPE_SIZE(t_)>0) \ ASSERT_ARRAY_SANITY((t_)->data) #define ASSERT_TENSOR_LIST_SANITY(tlist_) \ AI_ASSERT((tlist_) && (GET_TENSOR_LIST_SIZE(tlist_)>0)) \ #define ASSERT_TENSOR_DATA_SANITY(t_) \ ASSERT_TENSOR_SANITY(t_) \ ASSERT_ARRAY_DATA_SANITY((t_)->data) #define ASSERT_NODE_SANITY(node_) \ do { \ AI_ASSERT(AI_NODE_OBJ(node_)->tensors && AI_NODE_OBJ(node_)->tensors->chain) \ ASSERT_TENSOR_SANITY(GET_TENSOR_IN(AI_NODE_OBJ(node_)->tensors, 0)) \ ASSERT_TENSOR_SANITY(GET_TENSOR_OUT(AI_NODE_OBJ(node_)->tensors, 0)) \ } while (0); #else #define ASSERT_ARRAY_SANITY(a_) /* ASSERT_ARRAY_SANITY */ #define ASSERT_ARRAY_DATA_SANITY(a_) /* ASSERT_ARRAY_DATA_SANITY */ #define ASSERT_TENSOR_SANITY(t_) /* ASSERT_TENSOR_SANITY */ #define ASSERT_TENSOR_LIST_SANITY(tlist_) /* ASSERT_TENSOR_LIST_SANITY */ #define ASSERT_TENSOR_DATA_SANITY(t_) /* ASSERT_TENSOR_DATA_SANITY */ #define ASSERT_NODE_SANITY(node_) /* ASSERT_NODE_SANITY */ #endif /*HAS_AI_ASSERT*/ #if defined(__GNUC__) || defined(__clang__) /* Suppress unused function warnings */ #define AI_UNUSED_FUNCTION __attribute__((unused)) /* Manage false positives in address sanitizer */ #define AI_NO_SANITIZE_ADDRESS __attribute__((no_sanitize_address)) #else #define AI_UNUSED_FUNCTION /* AI_UNUSED_FUNCTION */ #define AI_NO_SANITIZE_ADDRESS /* AI_NO_SANITIZE_ADDRESS */ #endif /******************************************************************************/ #define AI_NODE_TYPE(type_) \ ((ai_node_type)((ai_u32)(type_)&0xFFFF)) #define AI_NODE_OBJ(obj_) \ ((ai_node*)(obj_)) #define AI_NODE_FUNC(func_) \ ((node_func)(func_)) #define AI_NODE_COMMON_FIELDS_DECLARE \ ai_node_type type; /*!< node type id (see @ref ai_node_type) */ \ ai_id_obj id; /*!< node object instance id (see @ref ai_id_obj) */ \ ai_flags flags; /*!< node object flags */ \ ai_klass_obj klass; /*!< opaque handler to specific layer implementations */ \ struct ai_network_s* network; /*!< handle to global network context */ \ struct ai_node_s* next; /*!< the next node object in the sequence */ \ node_func forward; /*!< forward function for the node */ \ AI_CONST ai_tensor_chain* tensors; /*!< pointer to node tensor chain */ #define AI_NODE_STATEFUL_FIELDS_DECLARE \ AI_NODE_COMMON_FIELDS_DECLARE \ ai_handle state; \ node_func init; \ node_func update; \ node_func destroy; #define AI_NODE_COMMON_INIT(type_, id_, flags_, klass_, network_, next_, forward_) \ .type = AI_NODE_TYPE(type_), \ .id = AI_ID_OBJ(id_), \ .flags = (flags_), \ .klass = AI_KLASS_OBJ(klass_), \ .network = AI_NETWORK_OBJ(network_), \ .next = AI_NODE_OBJ(next_), \ .forward = AI_NODE_FUNC(forward_) /*****************************************************************************/ /** Network Tensors Chains / Lists Handlers **/ /*****************************************************************************/ #define AI_FOR_EACH_TENSOR_CHAIN_DO(tlist_ptr_, chain_) \ ai_tensor_list* tlist_ptr_ = (chain_)->chain; \ for (; tlist_ptr_<(((chain_)->chain)+((chain_)->size)); tlist_ptr_++) #define AI_FOR_EACH_TENSOR_LIST_DO(idx_, t_ptr_, tlist_ptr_) \ ai_tensor* t_ptr_ = NULL; \ for (ai_size idx_ = 0; (idx_ < GET_TENSOR_LIST_SIZE(tlist_ptr_)) && \ ((t_ptr_ = GET_TENSOR_LIST_ITEM(tlist_ptr_, idx_)) != NULL); ++idx_) #define GET_TENSOR_LIST_INFO(list_) \ ((list_)->info) #define GET_TENSOR_LIST_META(list_, pos_) \ (&(GET_TENSOR_LIST_INFO(list_)->meta[pos_])) #define GET_TENSOR_LIST_STATE(list_, pos_) \ (&(GET_TENSOR_LIST_INFO(list_)->state[pos_])) #define GET_TENSOR_LIST_BUFFER(list_, pos_) \ (&(GET_TENSOR_LIST_INFO(list_)->buffer[pos_])) #define GET_TENSOR_LIST_ITEM(list_, pos_) \ ((NULL!=GET_TENSOR_LIST_ITEMS(list_)) \ ? GET_TENSOR_LIST_ITEMS(list_)[(pos_)] : NULL) #define GET_TENSOR_LIST_ITEMS(list_) \ ((list_)->tensor) #define GET_TENSOR_LIST_SIZE(list_) \ ((NULL!=(list_)) ? (list_)->size : 0) #define GET_TENSOR_CHAIN_SIZE(chain_) \ ((NULL!=(chain_)) ? (chain_)->size : 0) #define GET_TENSOR_LIST(chain_, type_) \ ((AI_CONCAT(AI_TENSOR_CHAIN_, type_)<(chain_)->size) \ ? &(chain_)->chain[AI_CONCAT(AI_TENSOR_CHAIN_, type_)] : NULL) #define GET_TENSOR_LIST_IN(chain_) \ (GET_TENSOR_LIST(chain_, INPUT)) #define GET_TENSOR_LIST_OUT(chain_) \ (GET_TENSOR_LIST(chain_, OUTPUT)) #define GET_TENSOR_LIST_WEIGTHS(chain_) \ (GET_TENSOR_LIST(chain_, WEIGHTS)) #define GET_TENSOR_LIST_SCRATCH(chain_) \ (GET_TENSOR_LIST(chain_, SCRATCH)) #define GET_TENSOR_IN(chain_, pos_) \ (GET_TENSOR_LIST_ITEM(GET_TENSOR_LIST_IN(chain_), (pos_))) #define GET_TENSOR_OUT(chain_, pos_) \ (GET_TENSOR_LIST_ITEM(GET_TENSOR_LIST_OUT(chain_), (pos_))) #define GET_TENSOR_WEIGHTS(chain_, pos_) \ (GET_TENSOR_LIST_ITEM(GET_TENSOR_LIST_WEIGTHS(chain_), (pos_))) #define GET_TENSOR_SCRATCH(chain_, pos_) \ (GET_TENSOR_LIST_ITEM(GET_TENSOR_LIST_SCRATCH(chain_), (pos_))) /******************************************************************************/ #if 1 #define SECTION_SERIAL(expr) expr #define SECTION_PARALLEL(expr) #else #define SECTION_SERIAL(expr) #define SECTION_PARALLEL(expr) expr #endif AI_API_DECLARE_BEGIN /*! * @struct ai_node_type * @ingroup core_common * @brief generic network node numeric type ID * */ typedef uint16_t ai_node_type; /*! * @typedef void (*node_func)(struct ai_node_s* node) * @ingroup core_common * @brief Callback signatures for all forward functions */ typedef void (*node_func)(struct ai_node_s* node); /*! * @typedef ai_float (*func_nl_el)(const ai_float x) * @ingroup core_common * @brief Fuction pointer for generic elementwise transforms * * This function pointer abstracts a generic nonlinear function applied to a * single element. See @ref ai_math_sqrt in @ref math_helpers as examples. */ typedef ai_float (*func_nl_el)(const ai_float x); /*! * @struct ai_node * @ingroup core_common * @brief Structure encoding a generic node of the network * * The node struct includes information about the network it belong to, the * next node in a sequential network and the forward function. The forward * functions are implemented in the @ref layers module. */ typedef AI_ALIGNED_TYPE(struct, 4) ai_node_s { AI_NODE_COMMON_FIELDS_DECLARE } ai_node; /*! * @struct ai_node_stateful * @ingroup core_common * @brief Structure encoding a stateful node of the network * * The node struct includes information about the network it belong to, the * next node in a sequential network and the init, update and forward functions. * The node functions are implemented in the @ref layers module. */ typedef AI_ALIGNED_TYPE(struct, 4) ai_node_stateful_s { AI_NODE_STATEFUL_FIELDS_DECLARE } ai_node_stateful; /*! * @brief initialize core module * @ingroup core_common * @return false if initialization fails, false otherwise */ AI_INTERNAL_API ai_bool core_init(void); /*! * @brief get 1st error raised during processing * @ingroup core_common * @param[out] error the @ref ai_error recorded during processing * @return the 1st error generated during processing. If no errors AI_ERROR_NONE */ AI_INTERNAL_API ai_error core_get_error(ai_error* error); /*! * @brief set error recorded during processing * @ingroup core_common * @param[out] error the @ref ai_error to set * @param[in] type the specific error type to set * @param[in] code the specific error code to set * @return true if the error is set, false in case a precedent error was already */ AI_INTERNAL_API ai_bool core_set_error( ai_error* error, const ai_error_type type, const ai_error_code code); AI_API_DECLARE_END #endif /*CORE_COMMON_H*/
9,995
C
33.588235
92
0.615408
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/Middlewares/ST/AI/Inc/layers.h
/** ****************************************************************************** * @file layers.h * @author AST Embedded Analytics Research Platform * @brief header file of AI platform layers datatypes ****************************************************************************** * @attention * * Copyright (c) 2017 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** @verbatim @endverbatim ****************************************************************************** */ #ifndef LAYERS_H #define LAYERS_H #pragma once #include "layers_common.h" #include "layers_conv2d.h" #include "layers_custom.h" #include "layers_dense.h" #include "layers_formats_converters.h" #include "layers_generic.h" #include "layers_lite_graph.h" #include "layers_nl.h" #include "layers_norm.h" #include "layers_pad_dqnn.h" #include "layers_pad_generic.h" #include "layers_pool.h" #include "layers_rnn.h" #include "layers_upsample_generic.h" #include "layers_sm.h" #include "layers_ml.h" #include "layers_ml_iforest.h" #include "layers_ml_svc.h" #include "layers_ml.h" #include "layers_ml_linearclassifier.h" #include "layers_ml_treeensembleclassifier.h" #include "layers_ml_treeensembleregressor.h" #include "layers_ml_svmregressor.h" #include "layers_conv2d_dqnn.h" #include "layers_dense_dqnn.h" #include "layers_pool_dqnn.h" #include "layers_generic_dqnn.h" #include "layers_upsample_generic.h" // #include "layers_template.h" AI_API_DECLARE_BEGIN /*! * @struct ai_any_layer_ptr * @ingroup layers * @brief Generic union for typed layers pointers */ typedef struct { ai_layer_type type; /*!< layer type id (see @ref ai_layer_type) */ union { #define LAYER_ENTRY(type_, id_, struct_, forward_func_, init_func_, destroy_func_) \ AI_CONCAT(ai_layer_, struct_)* struct_; #include "layers_list.h" }; } ai_any_layer_ptr; AI_API_DECLARE_END #endif /*LAYERS_H*/
2,190
C
27.089743
84
0.603653
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/Middlewares/ST/AI/Inc/core_net_inspect_interface.h
/** ****************************************************************************** * @file core_net_inspect_interface.h * @author AST Embedded Analytics Research Platform * @brief header file of core network inspection interface APIs ****************************************************************************** * @attention * * Copyright (c) 2018 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** @verbatim @endverbatim ****************************************************************************** */ #ifndef __CORE_NET_INSPECT_INTERFACE_H_ #define __CORE_NET_INSPECT_INTERFACE_H_ #pragma once #include "ai_platform.h" AI_API_DECLARE_BEGIN /*! * @defgroup core_validation Validation Core * @brief Implementation of the validation network interface headers */ /*! * @struct ai_inspect_node_info * @brief network node inspection context: there is one of this datastruct * for each node of the network */ typedef struct ai_inspect_node_info_s { ai_u16 type; /*!< node type info @see ai_node datastruct */ ai_u16 id; /*!< node id assigned by codegen tool to identify the specific node instance */ ai_u16 batch_id; /*!< current node batch processed */ ai_u16 n_batches; /*!< total number of node batches to process */ ai_float elapsed_ms; /*!< node performance analysys: time in milliseconds to execute the node forward function */ ai_u16 in_size; /*!< number of node's input activation buffers */ ai_u16 out_size; /*!< number of node's output activation buffers */ ai_buffer* in; /*!< input node activation buffer see @ref ai_buffer */ ai_buffer* out; /*!< output node activation buffer see @ref ai_buffer */ } ai_inspect_node_info; /*! * @struct ai_inspect_net_report * @brief network inspection report context */ typedef struct ai_inspect_net_report_s { ai_u32 id; /*!< id of the report */ ai_signature signature; /*!< network identification checksum */ ai_u32 num_inferences; /*!< total number of inferences processed during the inspection */ ai_u32 n_nodes; /*!< number of nodes in the network */ ai_float elapsed_ms; /*!< network total time (in ms) for processing num_inferences inferences */ ai_inspect_node_info* node; /*!< pointer to the array of size n_nodes where a single node report is reported. see @ref ai_inspect_node_info datastruct */ } ai_inspect_net_report; /*! * @enum net inspector inspection mode * @brief configuration flags to set net inspection mode */ typedef enum { VALIDATION_INSPECT = (0x1<<0), /**< Network validation inspection mode */ STORE_ALL_IO_ACTIVATIONS = (0x1<<7), /**< Store all I/O activations on snapshot datastruct */ } ai_inspect_mode; typedef enum { AI_NODE_EXEC_PRE_FORWARD_STAGE = 0x0, AI_NODE_EXEC_POST_FORWARD_STAGE = 0x1, } ai_node_exec_stage; /*! * @brief function pointer to callback report */ typedef void (*ai_inspect_report_cb_func)( const ai_handle cookie, const ai_inspect_net_report* report); /*! * @brief function pointer to node execute */ typedef void (*ai_inspect_exec_node_cb_func)( const ai_handle cookie, const ai_inspect_node_info* node_info, const ai_node_exec_stage stage); /*! * @struct ai_inspect_config * @brief inspection config datastruct */ typedef struct ai_inspect_config_s { ai_u8 validation_mode; /*!< validation mode flags see @ref ai_inspect_mode */ ai_u8 log_level; /*!< log class level see @ref LOG_SUDO */ ai_bool log_quiet; /*!< log class quiet mode */ ai_inspect_report_cb_func on_report_destroy; /*!< callback function called when a report datastruct is released from memory */ ai_inspect_exec_node_cb_func on_exec_node; /*!< callback function called when a node is executed (pre & post) */ ai_handle cookie; } ai_inspect_config; AI_API_DECLARE_END #endif /*__CORE_NET_INSPECT_INTERFACE_H_*/
4,734
C
37.495935
98
0.562315
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/Middlewares/ST/AI/Inc/layers_rnn.h
/** ****************************************************************************** * @file layers_rnn.h * @author AST Embedded Analytics Research Platform * @brief header file of RNN layers ****************************************************************************** * @attention * * Copyright (c) 2018 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** @verbatim @endverbatim ****************************************************************************** */ #ifndef LAYERS_RNN_H #define LAYERS_RNN_H #pragma once #include "layers_common.h" #include "layers_nl.h" AI_API_DECLARE_BEGIN /*! * @struct ai_layer_lstm * @ingroup layers * @brief LSTM layer with generic nonlinearities and peephole connections */ typedef AI_ALIGNED_TYPE(struct, 4) ai_layer_lstm_ { AI_LAYER_STATEFUL_FIELDS_DECLARE ai_size n_units; /**< size of the hidden RNN state */ func_nl activation_nl; /**< activation nonlinearity (input to cell) */ func_nl recurrent_nl; /**< recurrent nonlinearity (hidden to cell) */ func_nl out_nl; /**< output nonlinearity (cell to hidden) */ ai_bool go_backwards; /**< process reversed input */ ai_bool return_state; /**< return state */ ai_bool reverse_seq; /**< reverse output sequence */ ai_float cell_clip; /**< cell clip value */ } ai_layer_lstm; /*! * @struct ai_layer_gru * @ingroup layers * @brief Gated Recurrent Unit (GRU) layer with generic nonlinearities */ typedef AI_ALIGNED_TYPE(struct, 4) ai_layer_gru_ { AI_LAYER_STATEFUL_FIELDS_DECLARE ai_size n_units; /**< size of the hidden RNN state */ func_nl activation_nl; /**< activation nonlinearity (input to cell) */ func_nl recurrent_nl; /**< recurrent nonlinearity (hidden to cell) */ ai_bool reset_after; ai_bool return_state; ai_bool go_backwards; /**< process reversed input */ ai_bool reverse_seq; /**< reverse output sequence */ } ai_layer_gru; /*! * @struct ai_layer_rnn * @ingroup layers * @brief Simple Recurrent Neural Network (RNN) layer */ typedef AI_ALIGNED_TYPE(struct, 4) ai_layer_rnn_ { AI_LAYER_COMMON_FIELDS_DECLARE ai_size n_units; /**< size of the hidden RNN state */ func_nl activation_nl; /**< activation nonlinearity (input to hidden) */ ai_bool go_backwards; /**< process reversed input */ ai_bool reverse_seq; /**< reverse output sequence */ ai_bool return_state; } ai_layer_rnn; /*! * @brief Initialize a Long-Short Term Memory (LSTM) layer. * @ingroup layers * * Function used to initialize lstm internal state */ AI_INTERNAL_API void init_lstm(ai_layer * layer); /*! * @brief Destroy a Long-Short Term Memory (LSTM) layer state. * @ingroup layers * * Function used to destroy lstm internal state */ AI_INTERNAL_API void destroy_lstm(ai_layer * layer); /*! * @brief Computes the activations of a Long-Short Term Memory (LSTM) layer. * @ingroup layers * * Implements a Long-Short Term Layer with peephole connections: * \f{eqnarray*}{ * i_t &=& \sigma_a(x_t W_{xi} + h_{t-1} W_{hi} * + w_{ci} \odot c_{t-1} + b_i)\\ * f_t &=& \sigma_a(x_t W_{xf} + h_{t-1} W_{hf} * + w_{cf} \odot c_{t-1} + b_f)\\ * c_t &=& f_t \odot c_{t - 1} * + i_t \odot \sigma_r(x_t W_{xc} + h_{t-1} W_{hc} + b_c)\\ * o_t &=& \sigma_a(x_t W_{xo} + h_{t-1} W_{ho} + w_{co} \odot c_t + b_o)\\ * h_t &=& o_t \odot \sigma_o(c_t) * \f} * where \f$\sigma_a\f$ is the activation nonlinearity, \f$\sigma_r\f$ is the * recurrent nonlinearity and \f$\sigma_o\f$ is the out nonlinearity. The * \f$W_x\f$, \f$W_h\f$ and \f$W_c\f$ weights are sliced from the kernel, * recurrent and peephole weights. * * @param layer the LSTM layer */ AI_INTERNAL_API void forward_lstm(ai_layer * layer); /*! * @brief Initialize a Gated Recurrent Unit (GRU) layer. * @ingroup layers * * Function used to initialize gru internal state */ AI_INTERNAL_API void init_gru(ai_layer * layer); /*! * @brief Destroy a Gated Recurrent Unit (GRU) layer state. * @ingroup layers * * Function used to destroy gru internal state */ AI_INTERNAL_API void destroy_gru(ai_layer * layer); /*! * @brief Computes the activations of a Gated Recurrent Unit (GRU) layer. * @ingroup layers * * Implements a Gated Recurrent Unit with the formula: * \f{eqnarray*}{ * r_t &=& \sigma_a(x_t W_{xr} + h_{t - 1} W_{hr} + b_r) \\ * z_t &=& \sigma_a(x_t W_{xz} + h_{t - 1} W_{hz} + b_z) \\ * c_t &=& \sigma_r(x_t W_{xc} + r_t \odot (h_{t - 1} W_{hc} + b_{hc}) + b_c) * \qquad \textnormal{when reset after is true} \\ * c_t &=& \sigma_r(x_t W_{xc} + (r_t \odot h_{t - 1}) W_{hc} + b_{hc} + b_c) * \qquad \textnormal{when reset after is false (default)} \\ * h_t &=& (1 - z_t) \odot h_{t - 1} + z_t \odot c_t * \f} * where \f$\sigma_a\f$ is the activation nonlinearity and \f$\sigma_r\f$ is * the recurrent nonlinearity. The weights are sliced from the kernel and * recurrent weights. * * @param layer the GRU layer */ AI_INTERNAL_API void forward_gru(ai_layer * layer); /*! * @brief Computes the activations of a Recurrent Neural Network (RNN) layer. * @ingroup layers * * Implements a recurrent layer with the formula: * \f{eqnarray*}{ * h_t &=& \sigma_a(x_t W_{xr} + h_{t - 1} W_{hr} + b_r) * \f} * where \f$\sigma_a\f$ is the activation nonlinearity. The weights are sliced * from the kernel and recurrent weights. * * @param layer the RNN layer */ AI_INTERNAL_API void forward_rnn(ai_layer * layer); AI_API_DECLARE_END #endif /* LAYERS_RNN_H */
5,866
C
30.713513
80
0.596147
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/Middlewares/ST/AI/Inc/layers_lite_graph.h
/** ****************************************************************************** * @file layers_lite_graph.h * @author AST Embedded Analytics Research Platform * @brief header file of AI platform lite graph layers wrapper interface ****************************************************************************** * @attention * * Copyright (c) 2021 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** @verbatim @endverbatim ****************************************************************************** */ #ifndef LAYERS_LITE_GRAPH_H #define LAYERS_LITE_GRAPH_H #pragma once #include "core_common.h" /*! * @defgroup layers_lite_graph Lite Graph Wrapper Definitions * @brief definition * */ AI_API_DECLARE_BEGIN /*! * @struct ai_layer_lite_graph * @ingroup layers_lite_graph * @brief Generic Lite Graph Layer Wrapper * * The type of lite graph is handled by the specific forward lite graph function. */ typedef AI_ALIGNED_TYPE(struct, 4) ai_layer_lite_graph_ { AI_NODE_COMMON_FIELDS_DECLARE ai_handle* activations_map; /*!< array of pointers to shared activations memory pools */ ai_handle* weights_map; /*!< array of pointers to shared weights memory pools */ } ai_layer_lite_graph; AI_API_DECLARE_END #endif /*LAYERS_LITE_GRAPH_H*/
1,598
C
29.169811
98
0.558824
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/Middlewares/ST/AI/Inc/core_datatypes.h
/** ****************************************************************************** * @file core_datatypes.h * @author AST Embedded Analytics Research Platform * @brief header file of core module private defines and datatypes * to public nor codegen tool ****************************************************************************** * @attention * * Copyright (c) 2018 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** @verbatim @endverbatim ****************************************************************************** */ #ifndef AI_CORE_DATATYPES_H #define AI_CORE_DATATYPES_H #pragma once #include <stdint.h> /*! * @defgroup Core Module Datatypes * @brief Data structures and defines used by core module */ /*! * @brief platform runtime core library version */ #define AI_PLATFORM_RUNTIME_MAJOR 8 #define AI_PLATFORM_RUNTIME_MINOR 1 #define AI_PLATFORM_RUNTIME_MICRO 0 #define AI_PLATFORM_RUNTIME_BUILD A1-SNAPSHOT #define AI_MAGIC_CONTEXT_TOKEN (0xA1C00100) /*!< AI Cool! Magic Token */ #define AI_MAGIC_INSPECTOR_TOKEN (0xA1C00101) /*!< AI Cool! Magic Token */ #define AI_ID_OBJ(id) \ ((ai_id_obj)(id)) #define AI_C_ARRAY_COUNT(array_) \ ( sizeof(array_) / sizeof((array_)[0]) ) #define AI_C_ARRAY_BYTE_SIZE(array_) \ ( sizeof(array_) ) /*! * @typedef ai_id_obj * @ingroup core_datatypes * @brief numeric identifier for generic object instances (e.g. layers, * operators, etc.) It is used by codegen tool to keep tracks of specific * instances created */ typedef uint16_t ai_id_obj; #endif /*AI_CORE_DATATYPES_H*/
1,901
C
27.818181
80
0.570752
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/Middlewares/ST/AI/Inc/layers_ml_svmregressor.h
/** ****************************************************************************** * @file layers_svmregressor.h * @author AIS * @brief header file of AI platform SVM Regressor datatypes ****************************************************************************** * @attention * * Copyright (c) 2021 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** @verbatim @endverbatim ****************************************************************************** */ #ifndef LAYERS_SVMREGRESSOR_H #define LAYERS_SVMREGRESSOR_H #pragma once #include "layers_common.h" /*! * @defgroup layers_svmreg Layers Definitions * @brief definition * */ AI_API_DECLARE_BEGIN /* SVM regressor kernel types */ typedef enum ai_svm_kernel_e_ { AI_SVMREG_KERNEL_LINEAR = 0, AI_SVMREG_KERNEL_POLYNOMIAL, AI_SVMREG_KERNEL_RBF, AI_SVMREG_KERNEL_SIGMOID, AI_SVMREG_KERNEL_UNSUPPORTED, } ai_svm_kernel_e; /*! * @struct ai_layer_svmreg * @ingroup layers_svmreg * @brief SVM Regressor layer * * The type of svmreg function is handled by the specific forward function * @ref forward_svm_regressor */ typedef AI_ALIGNED_TYPE(struct, 4) ai_layer_svmreg_ { AI_LAYER_COMMON_FIELDS_DECLARE ai_float intercept; /*!< constant used in the decision function */ ai_float gamma; /*!< kernel coefficient for rbf, polynomial and sigmoid functions */ ai_float coef0; /*!< term in polynomial and sigmoid functions */ ai_u32 degree; /*!< polynomial function degree */ ai_svm_kernel_e kernel_type; /*!< kernel type : see ai_svm_kernel_e */ } ai_layer_svmreg; /******************************************************************************/ /* Forward Functions Section */ /******************************************************************************/ /*! * @brief Decodes the SVM Regressor ML operator. * @ingroup layers_svmreg * @param layer svm regressor layer */ AI_INTERNAL_API void forward_svm_regressor(ai_layer *pLayer); AI_API_DECLARE_END #endif /*LAYERS_SVMREGRESSOR_H*/
2,397
C
27.891566
96
0.53567
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/Middlewares/ST/AI/Inc/lite_bn_integer.h
#ifndef LITE_BN_INTEGER_H #define LITE_BN_INTEGER_H #pragma once #include "ai_lite_interface.h" /** * @brief Batch Normalization with 16-bit input, 16-bit threshold and binary output. * It is implemented using a threshold, and this is possible because the output is binary. * * @param[in] pIn Input data pointer * @param[out] pOut_32 Output data pointer * @param[in] pThreshold Thresholds pointer (one per channel) * @param[in] dim_x X dimension * @param[in] dim_y Y dimension * @param[in] channels_num Channels number */ LITE_API_ENTRY void forward_lite_bn_is16os1ws16(const ai_i16 *pIn, ai_u32 *pOut_32, const ai_i16 *pThreshold, const ai_i16 dim_x, const ai_i16 dim_y, const ai_i16 channels_num); #endif /* LITE_BN_INTEGER_H */
913
C
34.153845
90
0.591457
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/Middlewares/ST/AI/Inc/ai_datatypes_internal.h
/** ****************************************************************************** * @file ai_datatypes_internal.h * @author AST Embedded Analytics Research Platform * @brief Definitions of AI platform private APIs types ****************************************************************************** * @attention * * Copyright (c) 2017 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** @verbatim @endverbatim ****************************************************************************** */ #ifndef AI_DATATYPES_INTERNAL_H #define AI_DATATYPES_INTERNAL_H #pragma once #include "ai_datatypes.h" #include "ai_datatypes_defines.h" /*! * @defgroup datatypes_internal Internal Datatypes * @brief Data structures used internally to implement neural networks * * The layers are defined as structs; a generic layer type defines the basic * layer parameters and type-specific parameters are handled by specializations * implemented as a C union. The layers keep also a pointer to the parent * network and the next layer in the network. * The input, output and parameters are tensor with an hard-coded maximum * dimension of 4. Tensors are floating point arrays with a notion of size. * The network is a linked list of layers, and thus it stores only the pointer * to the first layer. */ /*! * @section Offsets * @ingroup datatypes_internal * Macros to handle (byte) stride addressing on tensors. The `AI_PTR` macro * is used to always cast a pointer to byte array. The macros `AI_OFFSET_X` are * used to compute (byte) offsets of respectively adjacents row elements, col * elements, channel elements and `channel_in` elements. * @{ */ /*! AI_STORAGE_KLASS SECTION ************************************/ #define AI_STORAGE_KLASS_TYPE(s_) \ ( (s_)->type ) #define AI_STORAGE_KLASS_SIZE(s_) \ ( (s_)->size ) #define AI_STORAGE_KLASS_DATA(s_, type_) \ ( (type_*)((s_)->data) ) #define AI_STORAGE_KLASS_COPY(dst_, dst_type_, src_, src_type_) \ { \ AI_ASSERT(AI_STORAGE_KLASS_SIZE(src_)>=AI_STORAGE_KLASS_SIZE(dst_)) \ AI_STORAGE_KLASS_SIZE(dst_) = AI_STORAGE_KLASS_SIZE(src_); \ for (ai_size i=0; i<AI_STORAGE_KLASS_SIZE(dst_); i++ ) { \ AI_STORAGE_KLASS_DATA(dst_, dst_type_)[i] = \ AI_STORAGE_KLASS_DATA(src_, src_type_)[i]; \ } \ } #define AI_STORAGE_KLASS_DUMP(s_, pfx_, post_, fmt_, type_) \ { \ AI_ASSERT(s_) \ AI_DEBUG_PRINT(pfx_, AI_STORAGE_KLASS_SIZE(s_)) \ for ( ai_u32 i=0; i<AI_STORAGE_KLASS_SIZE(s_); i++ ) { \ if ( (i % 8)==0 ) { AI_DEBUG_PRINT("\n ") } \ AI_DEBUG_PRINT(fmt_, AI_STORAGE_KLASS_DATA(s_, type_)[i]) \ } \ AI_DEBUG_PRINT(post_) \ } /*! AI_SHAPES SECTION ************************************/ #define AI_SHAPE_2D_H(shape_) \ AI_SHAPE_ELEM(shape_, AI_SHAPE_2D_HEIGHT) #define AI_SHAPE_2D_W(shape_) \ AI_SHAPE_ELEM(shape_, AI_SHAPE_2D_WIDTH) #define AI_SHAPE_ELEM(shape_, pos_) \ AI_STORAGE_KLASS_DATA(shape_, ai_shape_dimension)[pos_] #define AI_SHAPE_GET_ELEM(shape_, pos_) \ (((pos_) < AI_SHAPE_SIZE(shape_)) ? AI_SHAPE_ELEM(shape_, pos_) : 1) #define AI_SHAPE_SET_ELEM(shape_, pos_, val_) \ if ((pos_) < AI_SHAPE_SIZE(shape_)) { AI_SHAPE_ELEM(shape_, pos_) = (val_); } #define AI_SHAPE_TYPE(shape_) \ AI_STORAGE_KLASS_TYPE(shape_) #define AI_SHAPE_SIZE(shape_) \ AI_STORAGE_KLASS_SIZE(shape_) #define AI_SHAPE_CLONE(dst_, src_) \ AI_STORAGE_KLASS_COPY(dst_, ai_shape_dimension, src_, ai_shape_dimension) #define AI_SHAPE_BCAST_CLONE(dst_, src_) \ { \ for (ai_size i = 0; i < AI_SHAPE_SIZE(dst_); i++) { \ AI_SHAPE_SET_ELEM(dst_, i, AI_SHAPE_GET_ELEM(src_, i)); \ } \ } //#define AI_SHAPE_BATCH(shape_) AI_SHAPE_ELEM((shape_), AI_SHAPE_BATCH_CHANNEL) #define AI_SHAPE_H(shape_) AI_SHAPE_ELEM((shape_), AI_SHAPE_HEIGHT) #define AI_SHAPE_W(shape_) AI_SHAPE_ELEM((shape_), AI_SHAPE_WIDTH) #define AI_SHAPE_CH(shape_) AI_SHAPE_ELEM((shape_), AI_SHAPE_CHANNEL) #define AI_SHAPE_IN_CH(shape_) AI_SHAPE_ELEM((shape_), AI_SHAPE_IN_CHANNEL) #define AI_SHAPE_D(shape_) ((AI_SHAPE_SIZE((shape_)) > AI_SHAPE_DEPTH) \ ? AI_SHAPE_ELEM((shape_), AI_SHAPE_DEPTH) : 1) #define AI_SHAPE_E(shape_) ((AI_SHAPE_SIZE((shape_)) > AI_SHAPE_EXTENSION) \ ? AI_SHAPE_ELEM((shape_), AI_SHAPE_EXTENSION) : 1) #define AI_SHAPE_T(shape_) AI_SHAPE_ELEM((shape_), AI_SHAPE_TIME) #define AI_CONV_SHAPE_H AI_SHAPE_W #define AI_CONV_SHAPE_W AI_SHAPE_CH #define AI_CONV_SHAPE_CH AI_SHAPE_H #define AI_CONV_SHAPE_IN_CH AI_SHAPE_IN_CH /*! AI_STRIDES SECTION ***********************************/ #define AI_STRIDE_2D_H(stride_) \ AI_STRIDE_ELEM((stride_), AI_SHAPE_2D_HEIGHT) #define AI_STRIDE_2D_W(stride_) \ AI_STRIDE_ELEM((stride_), AI_SHAPE_2D_WIDTH) #define AI_STRIDE_ELEM(stride_, pos_) \ AI_STORAGE_KLASS_DATA(stride_, ai_stride_dimension)[pos_] #define AI_STRIDE_GET_ELEM(stride_, pos_) \ (((pos_) < AI_STRIDE_SIZE(stride_)) ? AI_STRIDE_ELEM(stride_, pos_) : 0) #define AI_STRIDE_SET_ELEM(stride_, pos_, val_) \ if ((pos_) < AI_STRIDE_SIZE(stride_)) AI_STRIDE_ELEM(stride_, pos_) = (val_); #define AI_STRIDE_TYPE(stride_) \ AI_STORAGE_KLASS_TYPE(stride_) #define AI_STRIDE_SIZE(stride_) \ AI_STORAGE_KLASS_SIZE(stride_) #define AI_STRIDE_CLONE(dst_, src_) \ AI_STORAGE_KLASS_COPY(dst_, ai_stride_dimension, src_, ai_stride_dimension) #define AI_STRIDE_BCAST_CLONE(dst_, src_) \ { \ for (ai_size i=0; i<AI_STRIDE_SIZE(dst_); i++) { \ AI_STRIDE_SET_ELEM(dst_, i, AI_STRIDE_GET_ELEM(src_, i)); \ } \ } //#define AI_STRIDE_BATCH(stride) AI_STRIDE_ELEM((stride), AI_SHAPE_BATCH_CHANNEL) #define AI_STRIDE_H(stride) AI_STRIDE_ELEM((stride), AI_SHAPE_HEIGHT) #define AI_STRIDE_W(stride) AI_STRIDE_ELEM((stride), AI_SHAPE_WIDTH) #define AI_STRIDE_CH(stride) AI_STRIDE_ELEM((stride), AI_SHAPE_CHANNEL) #define AI_STRIDE_IN_CH(stride) AI_STRIDE_ELEM((stride), AI_SHAPE_IN_CHANNEL) #define AI_STRIDE_D(stride) ((AI_STRIDE_SIZE((stride)) >= 5) ? AI_STRIDE_ELEM((stride), AI_SHAPE_DEPTH) : 0) #define AI_STRIDE_E(stride) ((AI_STRIDE_SIZE((stride)) == 6) ? AI_STRIDE_ELEM((stride), AI_SHAPE_EXTENSION) : 0) #define AI_STRIDE_T(stride) AI_STRIDE_ELEM((stride), AI_SHAPE_TIME) #define AI_STRIDE_SET_H(stride, val) AI_STRIDE_SET_ELEM((stride), AI_SHAPE_HEIGHT, val) #define AI_STRIDE_SET_W(stride, val) AI_STRIDE_SET_ELEM((stride), AI_SHAPE_WIDTH, val) #define AI_STRIDE_SET_CH(stride, val) AI_STRIDE_SET_ELEM((stride), AI_SHAPE_CHANNEL, val) #define AI_STRIDE_SET_IN_CH(stride, val) AI_STRIDE_SET_ELEM((stride), AI_SHAPE_IN_CHANNEL, val) #define AI_STRIDE_SET_D(stride, val) if (AI_STRIDE_SIZE((stride)) >= 5) AI_STRIDE_SET_ELEM((stride), AI_SHAPE_DEPTH, val) #define AI_STRIDE_SET_E(stride, val) if (AI_STRIDE_SIZE((stride)) == 6) AI_STRIDE_SET_ELEM((stride), AI_SHAPE_EXTENSION, val) /*! AI_TENSORS SECTION ***********************************/ #define AI_TENSOR_KLASS(tensor_) \ ((tensor_) ? (tensor_)->klass : NULL) #define AI_TENSOR_SHAPE(tensor_) \ (&((tensor_)->shape)) #define AI_TENSOR_STRIDE(tensor_) \ (&((tensor_)->stride)) #define AI_TENSOR_INFO(tensor_) \ (&((tensor_)->info)) #define AI_TENSOR_ARRAY(tensor_) \ ((tensor_) ? (tensor_)->data : NULL) #define AI_TENSOR_ID(tensor_) \ ((tensor_) ? AI_TENSOR_INFO(tensor_)->id : 0) #define AI_TENSOR_FLAGS(tensor_) \ ((tensor_) ? AI_TENSOR_INFO(tensor_)->flags : 0) #define AI_TENSOR_DATA_SIZE(tensor_) \ ((tensor_) ? AI_TENSOR_INFO(tensor_)->data_size : 0) /*! AI_OFFSETS SECTION ***********************************/ //#define AI_OFFSET_BATCH(b, stride) ((ai_ptr_offset)(b) * AI_STRIDE_BATCH(stride)) #define AI_OFFSET_H(y, stride) ((ai_ptr_offset)(y) * AI_STRIDE_H(stride)) #define AI_OFFSET_W(x, stride) ((ai_ptr_offset)(x) * AI_STRIDE_W(stride)) #define AI_OFFSET_CH(ch, stride) ((ai_ptr_offset)(ch) * AI_STRIDE_CH(stride)) #define AI_OFFSET_IN_CH(in_ch, stride) ((ai_ptr_offset)(in_ch) * \ AI_STRIDE_IN_CH(stride)) #define AI_OFFSET_D(d, stride) ((ai_ptr_offset)(d) * AI_STRIDE_D(stride)) #define AI_OFFSET_E(e, stride) ((ai_ptr_offset)(e) * AI_STRIDE_E(stride)) #define AI_OFFSET_5D(y, x, d, e, ch, stride) ( \ AI_OFFSET_H((y), (stride)) + AI_OFFSET_W((x), (stride)) + \ AI_OFFSET_D((d), (stride)) + AI_OFFSET_E((e), (stride)) + \ AI_OFFSET_CH((ch), (stride)) ) #define AI_OFFSET(y, x, ch, z, stride) ( \ AI_OFFSET_H((y), (stride)) + AI_OFFSET_W((x), (stride)) + \ AI_OFFSET_CH((ch), (stride)) + \ ((AI_STRIDE_SIZE((stride)) == 4) ? AI_OFFSET_IN_CH((z), (stride)) : AI_OFFSET_D((z), (stride))) ) /*! @} */ #define AI_GET_CONV_OUT_SIZE(in_size, filt_size, pad_l, pad_r, filt_stride) \ ((((in_size) - (filt_size) + (pad_l) + (pad_r)) / (filt_stride)) + 1) /** Tensors datatypes defines handlers ****************************************/ #define AI_TENSOR_SIZE(tensor_) \ get_tensor_size(tensor_, true) #define AI_TENSOR_SIZE_UNPAD(tensor_) \ get_tensor_size(tensor_, false) #define AI_TENSOR_BYTE_SIZE(tensor_) \ get_tensor_byte_size(tensor_) /******************************************************************************/ #define AI_PLATFORM_VERSION_INIT(major_, minor_, micro_) \ { .major = (major_), .minor = (minor_), .micro = (micro_), .reserved = 0x0 } /** Integer tensor info extraction ********************************************/ #define AI_INTQ_INFO_LIST_SCALE_ARRAY(list_, type_) \ ( ((list_) && (list_)->info) \ ? ((type_*)((list_)->info->scale)) : NULL ) #define AI_INTQ_INFO_LIST_ZEROPOINT_ARRAY(list_, type_) \ ( ((list_) && (list_)->info) \ ? ((type_*)((list_)->info->zeropoint)) : NULL ) #define AI_KLASS_GET_INTQ_INFO_LIST(tensor_) \ ((ai_intq_info_list*)((tensor_)->klass)) AI_API_DECLARE_BEGIN /*! * @brief Check whether 2 shapes have identical dimensions. * @ingroup datatypes_internal * @param shape0 the 1st tensor shape to compare * @param shape1 the 2nd tensor shape to compare * @return true if shape0 and shape1 have same dimensions. false otherwise */ AI_DECLARE_STATIC ai_bool ai_shape_is_same( const ai_shape* shape0, const ai_shape* shape1) { AI_ASSERT(shape0 && shape1) if (AI_SHAPE_SIZE(shape0) != AI_SHAPE_SIZE(shape1)) return false; ai_size dim = AI_SHAPE_SIZE(shape0); while ( dim>0 ) { dim--; if ( AI_SHAPE_ELEM(shape0, dim)!=AI_SHAPE_ELEM(shape1, dim) ) return false; } return true; } /*! * @brief Check whether the shapes is 1*1*1... for a scalar value content. * @ingroup datatypes_internal * @param shape the tensor shape to evaluate * @return true if shape0 is scalar false otherwise */ AI_DECLARE_STATIC ai_bool ai_shape_is_scalar( const ai_shape* shape0) { ai_size dim = AI_SHAPE_SIZE(shape0); while (dim>0) { dim--; if (AI_SHAPE_ELEM(shape0, dim) != 1) return false; } return true; } /*! * @brief Check if shape0 is a subshape of shape1 * @ingroup datatypes_internal * @param shape0 the 1st tensor shape to compare * @param shape1 the 2nd tensor shape to compare * @return true if shape0 is a subshape of shape1 (all shape0 dimensions are * smallers or equal of the shape1 ones). false otherwise */ AI_DECLARE_STATIC ai_bool ai_shape_is_subshape( const ai_shape* shape0, const ai_shape* shape1) { AI_ASSERT(shape0 && shape1) AI_ASSERT(AI_SHAPE_SIZE(shape0)==AI_SHAPE_SIZE(shape1)) ai_size dim = AI_SHAPE_SIZE(shape0); while (dim) { dim--; if ( AI_SHAPE_ELEM(shape0, dim)>AI_SHAPE_ELEM(shape1, dim) ) return false; } return true; } /*! * @brief Computes the total size of a tensor given its dimensions. * @ingroup datatypes_internal * @param shape the tensor shape */ AI_DECLARE_STATIC ai_size ai_shape_get_size(const ai_shape* shape) { AI_ASSERT(shape) ai_size dim = AI_SHAPE_SIZE(shape); ai_size size = 1; while (dim>0) { dim--; size *= AI_SHAPE_ELEM(shape, dim); } return size; } /*! * @brief Computes the size of the input image discarding the channels. * @ingroup datatypes_internal * @param shape the tensor shape */ AI_DECLARE_STATIC ai_size ai_shape_get_npixels(const ai_shape* shape) { AI_ASSERT(shape) const ai_size npixels = AI_SHAPE_W(shape) * AI_SHAPE_H(shape); return npixels; } /** APIs Section *************************************************************/ /*! * @brief Get packed version from major, minor, micro representaion. * @ingroup datatypes_internal * @param major major version value * @param minor minor version value * @param micro micro version value * @return a packed version info obtained serializing input values */ AI_INTERNAL_API ai_version ai_version_get(const ai_u8 major, const ai_u8 minor, const ai_u8 micro); /*! * @brief Get un-packed version from packed version representaion. * @ingroup datatypes_internal * @param version a packed varsion info * @return struct with de-serialized major, minor, micro values */ AI_INTERNAL_API ai_platform_version ai_platform_version_get(const ai_version version); /*! * @brief Map from ai_buffer data struct to ai_array data struct. * @ingroup datatypes_internal * @param buf a pointer to the ai_buffer to be mapped to ai_array * @return an initialized @ref ai_array struct representing same data */ AI_INTERNAL_API ai_array ai_from_buffer_to_array(const ai_buffer* buf); /*! * @brief Map from ai_array data struct to ai_buffer data struct. * @ingroup datatypes_internal * @param array a pointer to the ai_array to be mapped to ai_buffer * @return an initialized @ref ai_buffer struct representing same data */ AI_INTERNAL_API ai_buffer ai_from_array_to_buffer(const ai_array* array); /*! * @brief get the total number of elements of a n-dimensional tensor. * @ingroup datatypes_internal * @param t a pointer to an @ref ai_tensor * @param with_padding when true it considers also padded elements * @return the number of elements of the tensor (with/without padded ones) */ AI_INTERNAL_API ai_size get_tensor_size(const ai_tensor* t, const ai_bool with_padding); /*! * @brief get the total size in bytes of elements of a n-dimensional tensor (excluding padded ones). * @ingroup datatypes_internal * @param t a pointer to an @ref ai_tensor * @return the total size in bytes of elements of the tensor (excluding padded ones) */ AI_INTERNAL_API ai_size get_tensor_byte_size(const ai_tensor* t); AI_API_DECLARE_END #endif /*AI_DATATYPES_INTERNAL_H*/
14,911
C
34.336493
133
0.62303
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/Middlewares/ST/AI/Inc/lite_dw_dqnn.h
/** ****************************************************************************** * @file lite_dw_dqnn.h * @author AIS * @brief header file of AI platform lite dw kernel datatypes ****************************************************************************** * @attention * * Copyright (c) 2021 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** @verbatim @endverbatim ****************************************************************************** */ #ifndef LITE_DW_DQNN_H #define LITE_DW_DQNN_H #pragma once #include "ai_lite_interface.h" /******************************************************************************/ /* Forward Functions Section */ /******************************************************************************/ /*! * @brief Handles 2D DW convolution with binary input, binary output and * binary weights - with 0 padding (QKeras like) - Lite I/F * @ingroup lite_conv2d_dqnn */ LITE_API_ENTRY void forward_lite_dw_is1os1ws1_bn_pad0(const ai_u32 *pDataIn_init, ai_u32 * pDataOut_init, const ai_u32 *pWeights_init, ai_float *pScratch_32, const ai_u32 n_channel_in, const ai_u32 n_channel_out, const ai_i32 width_in, const ai_i32 height_in, const ai_i32 width_out, const ai_i32 height_out, const ai_i32 filt_width, const ai_i32 filt_height, const ai_i32 filt_pad_x, const ai_i32 filt_pad_y, const ai_i32 filt_stride_x, const ai_i32 filt_stride_y, const ai_i32 *pThreshold); /*! * @brief Handles 2D DW convolution with binary input, binary output and * binary weights - with 0 padding (QKeras like) - Lite I/F * - Optimized thanks to Optim3 assumptions * @ingroup lite_conv2d_dqnn */ LITE_API_ENTRY void forward_lite_dw_is1os1ws1_bn_pad0_optim3(const ai_u32 *pDataIn_init, ai_u32 * pDataOut_init, const ai_u32 *pWeights_init, ai_float *pScratch_32, const ai_u32 n_channel_in, const ai_u32 n_channel_out, const ai_i32 width_in, const ai_i32 height_in, const ai_i32 width_out, const ai_i32 height_out, const ai_i32 filt_width, const ai_i32 filt_height, const ai_i32 filt_pad_x, const ai_i32 filt_pad_y, const ai_i32 filt_stride_x, const ai_i32 filt_stride_y, const ai_i32 *pThreshold); /*! * @brief Handles 2D convolution with binary input, binary output and * binary weights - with +1/-1 padding (Larq like) - Lite I/F * @ingroup lite_conv2d_dqnn */ LITE_API_ENTRY void forward_lite_dw_is1os1ws1_bn_pad1(const ai_u32 *pDataIn_init, ai_u32 * pDataOut_init, const ai_u32 *pWeights_init, ai_float *pScratch_32, const ai_u32 n_channel_in, const ai_u32 n_channel_out, const ai_i32 width_in, const ai_i32 height_in, const ai_i32 width_out, const ai_i32 height_out, const ai_i32 filt_width, const ai_i32 filt_height, const ai_i32 filt_pad_x, const ai_i32 filt_pad_y, const ai_i32 filt_stride_x, const ai_i32 filt_stride_y, const ai_i32 *pThreshold, const ai_i32 pad_value); /*! * @brief Handles 2D convolution with binary input, binary output and * binary weights - with +1/-1 padding (Larq like) - Lite I/F * - Optimized thanks to Optim3 assumptions * @ingroup lite_conv2d_dqnn */ LITE_API_ENTRY void forward_lite_dw_is1os1ws1_bn_pad1_optim3(const ai_u32 *pDataIn_init, ai_u32 * pDataOut_init, const ai_u32 *pWeights_init, ai_float *pScratch_32, const ai_u32 n_channel_in, const ai_u32 n_channel_out, const ai_i32 width_in, const ai_i32 height_in, const ai_i32 width_out, const ai_i32 height_out, const ai_i32 filt_width, const ai_i32 filt_height, const ai_i32 filt_pad_x, const ai_i32 filt_pad_y, const ai_i32 filt_stride_x, const ai_i32 filt_stride_y, const ai_i32 *pThreshold, const ai_i32 pad_value); #endif /*LITE_DW_DQNN_H*/
6,834
C
49.629629
80
0.362891
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/Middlewares/ST/AI/Inc/lite_upsample_generic.h
/** ****************************************************************************** * @file lite_upsample.h * @author AIS * @brief header file of AI platform lite pw kernel datatypes ****************************************************************************** * @attention * * Copyright (c) 2021 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** @verbatim @endverbatim ****************************************************************************** */ #ifndef LITE_UPSAMPLE_GENERIC_H #define LITE_UPSAMPLE_GENERIC_H #pragma once #include "ai_lite_interface.h" void forward_lite_upsample_generic_nearest(const ai_u8* in_data, ai_u8* out_data, const ai_size width_in, const ai_size width_out, const ai_float width_scale, const ai_size height_out, const ai_float height_scale, const ai_u32 output_tensor_w_stride, const ai_float offset_round_coeff); void forward_lite_upsample_nearest(ai_ptr in_data, ai_ptr out_data, const ai_size width_in, const ai_float width_scale, const ai_float height_scale, const ai_size width_out, const ai_size height_out, const ai_ptr_offset stride_w, const ai_float offset_round_coeff); void forward_lite_upsample_zeros( ai_ptr in_data, ai_ptr out_data, const ai_size width_in, const ai_size height_in, const ai_float width_scale, const ai_float height_scale, const ai_size width_out, const ai_size height_out, const ai_ptr_offset stride_ch, const ai_ptr_offset stride_w, const ai_handle p_zero_value); #endif /*LITE_UPSAMPLE_GENERIC_H*/
2,756
C
44.196721
80
0.394049
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/Middlewares/ST/AI/Inc/ai_lite_interface.h
/** ****************************************************************************** * @file ai_lite_interface.h * @author AST Embedded Analytics Research Platform * @brief Definitions and implementations of runtime-lite codegen APIs ****************************************************************************** * @attention * * Copyright (c) 2022 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** @verbatim @endverbatim ****************************************************************************** */ #ifndef AI_LITE_INTERFACE_H #define AI_LITE_INTERFACE_H #pragma once #include "ai_platform.h" #include "ai_lite.h" /*****************************************************************************/ /* Generic Codegen Section */ // #ifdef HAS_LOG #if 0 #include "core_log.h" #define LITE_GRAPH_START(_graph_name) \ AI_LOG_DEBUG("[LITE GRAPH START] : " _graph_name) #define LITE_GRAPH_END(_graph_name) \ AI_LOG_DEBUG("[LITE GRAPH END] : " _graph_name) #else #define LITE_GRAPH_START(_graph_name) \ /* LITE_GRAPH_START() */ #define LITE_GRAPH_END(_graph_name) \ /* LITE_GRAPH_END() */ #endif /* HAS_LOG */ #ifdef HAS_AI_ASSERT #include <assert.h> #define LITE_ASSERT(_cond) \ { assert(_cond); } #else #define LITE_ASSERT(_cond) \ do { /* LITE_ASSERT() */ } while (0); #endif /*HAS_AI_ASSERT*/ /*****************************************************************************/ #if defined(_MSC_VER) #define LITE_DECLARE_STATIC static __inline #define LITE_HINT_INLINE static __inline #define LITE_FORCE_INLINE static __inline #elif defined(__ICCARM__) || defined (__IAR_SYSTEMS_ICC__) #define LITE_DECLARE_STATIC static inline #define LITE_HINT_INLINE static inline #define LITE_FORCE_INLINE static inline #elif defined(__GNUC__) #define LITE_DECLARE_STATIC static __inline #define LITE_HINT_INLINE static __inline #define LITE_FORCE_INLINE static __inline #else #define LITE_DECLARE_STATIC static __inline #define LITE_HINT_INLINE static __inline #define LITE_FORCE_INLINE static __inline #endif /* _MSC_VER */ #define LITE_API_ENTRY /* LITE_API_ENTRY */ #define LITE_PACK(...) \ __VA_ARGS__ #define LITE_UNUSED(_elem) \ ((void)(_elem)); #define LITE_KERNEL_SECTION(_code_block) \ { LITE_PACK(_code_block) } /*****************************************************************************/ /* Arrays Section */ #define LITE_ARRAY_VALUES(...) \ { LITE_PACK(__VA_ARGS__) } #define LITE_ARRAY_DATA(_array, _type) \ ((_type*)(_array)->data) #define LITE_ARRAY_DATA_START(_array, _type) \ ((_type*)(_array)->data_start) /*****************************************************************************/ /* Tensors Section */ #define LITE_TENSOR_ARRAY(_tensor, _pos) \ (((_tensor)->data) + (_pos)) /*****************************************************************************/ /* Tensors List Section */ #define LITE_TENSOR_LIST(_chain, _pos) \ (&(_chain)->chain[_pos]) #define LITE_TENSOR_IN(_chain, _pos) \ (LITE_TENSOR_LIST(_chain, 0)->tensor[_pos]) #define LITE_TENSOR_OUT(_chain, _pos) \ (LITE_TENSOR_LIST(_chain, 1)->tensor[_pos]) #define LITE_TENSOR_WEIGHTS(_chain, _pos) \ (LITE_TENSOR_LIST(_chain, 2)->tensor[_pos]) #define LITE_TENSOR_SCRATCHS(_chain, _pos) \ (LITE_TENSOR_LIST(_chain, 3)->tensor[_pos]) /*****************************************************************************/ #define LITE_LAYER_ACQUIRE(name_, cast_type_, ptr_) \ LITE_ASSERT(ptr_) \ AI_CONCAT(ai_layer_, cast_type_)* name_ = \ (AI_CONCAT(ai_layer_, cast_type_)*)(ptr_); #define LITE_LAYER_RELEASE(name_, cast_type_) \ /* LITE_LAYER_RELEASE() */ /*****************************************************************************/ AI_API_DECLARE_BEGIN AI_API_DECLARE_END #endif /* AI_LITE_INTERFACE_H */
4,226
C
28.767605
80
0.503076
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/Middlewares/ST/AI/Inc/layers_sm.h
/** ****************************************************************************** * @file layers_sm.h * @author AST Embedded Analytics Research Platform * @brief header file of AI platform non softmax layer datatype ****************************************************************************** * @attention * * Copyright (c) 2018 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** @verbatim @endverbatim ****************************************************************************** */ #ifndef LAYERS_SM_H #define LAYERS_SM_H #pragma once #include "layers_common.h" /*! * @defgroup layers SoftMax Layer Definitions * @brief definition * */ AI_API_DECLARE_BEGIN /*! * @brief Softmax normalization computed on an array of fixed point channels * @ingroup layers_sm * @param out opaque handler to output channel array * @param in opaque handler to input channel array * @param in_size total size (number of elements) to process on the input * @param channel_size number of elements of the input channel * @param in_channel_step number of elements to move to next input element * @param out_channel_step number of elements to move to next output element */ AI_INTERNAL_API void sm_func_sm_array_fixed(ai_handle out, const ai_handle in, const ai_size in_size, const ai_size channel_size, const ai_size in_channel_step, const ai_size out_channel_step); /*! * @brief Computes the activations of a fixed point softmax nonlinear layer. * @ingroup layers_sm * @param layer the softmax (sm) layer */ AI_INTERNAL_API void forward_sm_fixed(ai_layer *pLayer); AI_API_DECLARE_END #endif /*LAYERS_SM_H*/
2,051
C
30.56923
80
0.570453
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/Middlewares/ST/AI/Inc/datatypes_network.h
/** ****************************************************************************** * @file datatypes_network.h * @author AST Embedded Analytics Research Platform * @brief Definitions of code generated network types ****************************************************************************** * @attention * * Copyright (c) 2017 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** @verbatim @endverbatim ****************************************************************************** */ #ifndef DATATYPES_NETWORK_H #define DATATYPES_NETWORK_H #pragma once /* * Header to be overriden by the generated version * by including with <> the include directories are searched in the order * specified in the compiler * To enable the override, put the generated path before the API path */ #include "ai_platform.h" AI_API_DECLARE_BEGIN #ifdef AI_OVERRIDE_CUSTOM_TYPES #warning "Warning: Custom Types have been already defined!\n" #endif #define AI_CUSTOM_TYPES_COUNT (3) #define AI_CUSTOM_TYPES_SIGNATURE_DECLARE(name) \ const ai_custom_type_signature name[AI_CUSTOM_TYPES_COUNT+1] = { \ AI_CUSTOM_TYPES_COUNT, \ AI_CUSTOM_SIZE(ai_shape_dimension), \ AI_CUSTOM_SIZE(ai_stride_dimension), \ AI_CUSTOM_SIZE(ai_array_size), \ }; typedef ai_i32 ai_stride_dimension; typedef ai_u32 ai_array_size; AI_API_DECLARE_END #endif /*DATATYPES_NETWORK_H*/
1,694
C
27.728813
80
0.579103
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/Middlewares/ST/AI/Inc/ai_layer_custom_interface.h
/** ****************************************************************************** * @file ai_layer_custom_interface.h * @author AST Embedded Analytics Research Platform * @brief Definitions of AI platform custom layers interface APIs ****************************************************************************** * @attention * * Copyright (c) 2021 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** @verbatim @endverbatim ****************************************************************************** */ #ifndef AI_LAYER_CUSTOM_INTERFACE_H #define AI_LAYER_CUSTOM_INTERFACE_H #pragma once #include "ai_platform.h" #include "ai_platform_interface.h" #include "layers_custom.h" #define INTQ_SCALE_FLOAT (AI_BUFFER_META_FLAG_SCALE_FLOAT) #define INTQ_ZEROPOINT_U8 (AI_BUFFER_META_FLAG_ZEROPOINT_U8) #define INTQ_ZEROPOINT_S8 (AI_BUFFER_META_FLAG_ZEROPOINT_S8) #define INTQ_ZEROPOINT_U16 (AI_BUFFER_META_FLAG_ZEROPOINT_U16) #define INTQ_ZEROPOINT_S16 (AI_BUFFER_META_FLAG_ZEROPOINT_S16) #define AI_TENSOR_HEIGHT (3) #define AI_TENSOR_WIDTH (2) #define AI_TENSOR_CHANNEL (1) #define AI_TENSOR_IN_CHANNEL (0) AI_API_DECLARE_BEGIN typedef enum { TYPE_NONE = 0x0, TYPE_FLOAT, TYPE_BOOL, TYPE_INTEGER, TYPE_SIGNED, TYPE_UNSIGNED, } ai_tensor_type; typedef struct { ai_tensor_type type; ai_i8 bits; ai_i8 fbits; } ai_tensor_format; typedef struct { ai_u16 flags; /*!< optional flags to store intq info attributes */ ai_u16 size; /*!< number of elements in the the intq_info list */ ai_float* scale; /*!< array of scales factors */ union { ai_u8* zeropoint_u8; /*!< array of zeropoints as unsigned */ ai_i8* zeropoint_s8; /*!< array of zeropoints as signed */ }; } ai_tensor_intq_info; /**************************************************************************** ** Layer Custom Interface APIs ****************************************************************************/ /*! * @brief acquire the custom layer from its handle * @ingroup ai_layer_custom_interface * @param layer an opaque handler to the custom layer * @return a pointer to ai_layer_custom if found and valid, else NULL */ AI_INTERFACE_TYPE ai_layer_custom* ai_layer_custom_get( ai_layer* layer); /*! * @brief release the custom layer provided its handle * @ingroup ai_layer_custom_interface * @param layer an opaque handler to the custom layer to release */ AI_INTERFACE_TYPE void ai_layer_custom_release( ai_layer* layer); /*! * @brief get the number of inputs tensors of a custom layer * @ingroup ai_layer_custom_interface * @param layer an opaque handler to the custom layer * @return the number of input tensors of the layer. 0 if no input tensors or error */ AI_INTERFACE_TYPE ai_size ai_layer_get_tensor_in_size( const ai_layer* layer); /*! * @brief get the number of outputs tensors of a custom layer * @ingroup ai_layer_custom_interface * @param layer an opaque handler to the custom layer * @return the number of outputs tensors of the layer. 0 if no outputs tensors or error */ AI_INTERFACE_TYPE ai_size ai_layer_get_tensor_out_size( const ai_layer* layer); /*! * @brief get the number of weights tensors of a custom layer * @ingroup ai_layer_custom_interface * @param layer an opaque handler to the custom layer * @return the number of weights tensors of the layer. 0 if no weights tensors or error */ AI_INTERFACE_TYPE ai_size ai_layer_get_tensor_weights_size( const ai_layer* layer); /*! * @brief get the n-th (at index pos) input tensor pointer from a layer * @ingroup ai_layer_custom_interface * @param layer an opaque handler to the layer * @param pos the index position in the tensor list * @return a pointer to a tensor if found, else, if invalid or out-of-range NULL */ AI_INTERFACE_TYPE ai_tensor* ai_layer_get_tensor_in( const ai_layer* layer, const ai_u16 pos); /*! * @brief get the n-th (at index pos) output tensor pointer from a layer * @ingroup ai_layer_custom_interface * @param layer an opaque handler to the layer * @param pos the index position in the tensor list * @return a pointer to a tensor if found, else, if invalid or out-of-range NULL */ AI_INTERFACE_TYPE ai_tensor* ai_layer_get_tensor_out( const ai_layer* layer, const ai_u16 pos); /*! * @brief get the n-th (at index pos) weight tensor pointer from a layer * @ingroup ai_layer_custom_interface * @param layer an opaque handler to the layer * @param pos the index position in the tensor list * @return a pointer to a tensor if found, else, if invalid or out-of-range NULL */ AI_INTERFACE_TYPE ai_tensor* ai_layer_get_tensor_weights( const ai_layer* layer, const ai_u16 pos); /**** Layer Tensors APIs ***************************************************/ /*! * @brief check if the tensor has integer quantization informations @ref ai_tensor_intq_info * @ingroup ai_layer_custom_interface * @param tensor a pointer to the tensor * @return true if tensot has integer quantization informations, false otherwise */ AI_INTERFACE_TYPE ai_bool ai_tensor_has_intq( const ai_tensor* t); /*! * @brief get the tensor integer quantization informations @ref ai_tensor_intq_info * @ingroup ai_layer_custom_interface * @param tensor a pointer to the tensor * @return the integer quantization informations as a struct @ref ai_tensor_intq_info */ AI_INTERFACE_TYPE ai_tensor_intq_info ai_tensor_get_intq( const ai_tensor* t); /*! * @brief get the format of the tensor see @ref ai_tensor_format * @ingroup ai_layer_custom_interface * @param tensor a pointer to the tensor * @return the tensor format */ AI_INTERFACE_TYPE ai_tensor_format ai_tensor_get_format( const ai_tensor* t); /**** Shapes Getters ****/ /*! * @brief get the dimensionality of the tensor shapes * @ingroup ai_layer_custom_interface * @param tensor a pointer to the tensor * @return the dimensionality of the tensor shape */ AI_INTERFACE_TYPE ai_size ai_tensor_get_shape_size( const ai_tensor* t); /*! * @brief get the value of the shape dimensionality pos * @ingroup ai_layer_custom_interface * @param tensor a pointer to the tensor * @return the value of the shape dimensionality at pos of the tensor */ AI_INTERFACE_TYPE ai_shape_dimension ai_tensor_get_shape( const ai_tensor* t, const ai_u16 pos); /**** Strides Getters ****/ /*! * @brief get the dimensionality of the tensor strides * @ingroup ai_layer_custom_interface * @param tensor a pointer to the tensor * @return the dimensionality of the tensor strides @ref ai_stride */ AI_INTERFACE_TYPE ai_size ai_tensor_get_stride_size( const ai_tensor* t); /*! * @brief get the value of the stride dimensionality pos * @ingroup ai_layer_custom_interface * @param tensor a pointer to the tensor * @return the value of the stride dimensionality at pos of the tensor */ AI_INTERFACE_TYPE ai_stride_dimension ai_tensor_get_stride( const ai_tensor* t, const ai_u16 pos); /**** Data Storage Getters ****/ /*! * @brief get tensor storage data buffer pointer * @ingroup ai_layer_custom_interface * @param tensor a pointer to the tensor * @return a pointer to the tensor data buffer, set to NULL if error */ AI_INTERFACE_TYPE ai_any_ptr ai_tensor_get_data( const ai_tensor* t); /*! * @brief get number of tensor elements * @ingroup ai_layer_custom_interface * @param tensor a pointer to the tensor * @return the number of tensor elements or 0 if error */ AI_INTERFACE_TYPE ai_size ai_tensor_get_data_size( const ai_tensor* t); /*! * @brief get the size in bytes of the tensor data buffer * @ingroup ai_layer_custom_interface * @param tensor a pointer to the tensor * @return the size in bytes of the tensor data buffer. 0 if error */ AI_INTERFACE_TYPE ai_size ai_tensor_get_data_byte_size( const ai_tensor* t); AI_API_DECLARE_END #endif /*AI_LAYER_CUSTOM_INTERFACE_H*/
8,272
C
29.985019
92
0.66308
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/Middlewares/ST/AI/Inc/layers_pool_dqnn.h
/** ****************************************************************************** * @file layers_conv2d_dqnn.h * @author AIS * @brief header file of AI platform DQNN pool datatypes ****************************************************************************** * @attention * * Copyright (c) 2021 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** @verbatim @endverbatim ****************************************************************************** */ #ifndef LAYERS_POOL_DQNN_H #define LAYERS_POOL_DQNN_H #pragma once #include "layers_common.h" #include "layers_pool.h" /*! * @defgroup layers_pool_dqnn Layers Definitions * @brief definition * */ AI_API_DECLARE_BEGIN /*! * @struct ai_layer_pool_dqnn * @ingroup layers_pool_dqnn * @brief pool_dqnn layer * * @ref forward_maxpool_is1os1 */ typedef AI_ALIGNED_TYPE(struct, 4) ai_layer_pool_dqnn_ { AI_LAYER_COMMON_FIELDS_DECLARE ai_shape_2d pool_size; /*!< pooling size */ ai_shape_2d pool_stride; /*!< pooling stride */ ai_shape pool_pad; /*!< pooling pad, y,x border sizes */ // ai_u32 pad_value; /*!< pooling pad value */ } ai_layer_pool_dqnn; /******************************************************************************/ /* Forward Functions Section */ /******************************************************************************/ /*! * @brief Handles max pooling with binary input and binary output * @ingroup layers_pool_dqnn * @param layer conv2d_pool layer */ AI_INTERNAL_API void forward_maxpool_is1os1(ai_layer *pLayer); AI_API_DECLARE_END #endif /*LAYERS_POOL_DQNN_H*/
1,996
C
26.356164
80
0.482966
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/Middlewares/ST/AI/Inc/lite_dw.h
/** ****************************************************************************** * @file lite_dw.h * @author AIS * @brief header file of AI platform lite dw kernel datatypes ****************************************************************************** * @attention * * Copyright (c) 2021 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** @verbatim @endverbatim ****************************************************************************** */ #ifndef LITE_DW_H #define LITE_DW_H #pragma once #include "ai_lite_interface.h" /******************************************************************************/ /* Forward Functions Section */ /******************************************************************************/ /*! * @brief Handles dw convolutions generic case (supports depth multiplier >= 1) * @ingroup lite_dw */ LITE_API_ENTRY void forward_lite_dw_dm_sssa8_ch(const ai_i8 *Im_in, const ai_u16 dim_im_in_x, const ai_u16 dim_im_in_y, const ai_u16 ch_im_in, const ai_i8 *wt, const ai_u16 ch_im_out, const ai_u16 dim_kernel_x, const ai_u16 dim_kernel_y, const ai_u16 padding_x, const ai_u16 padding_y, const ai_u16 stride_x, const ai_u16 stride_y, const ai_i32 *bias, const ai_i8 In_ZeroPoint, const ai_i8 Out_ZeroPoint, ai_i8 *Im_out, const ai_u16 dim_im_out_x, const ai_u16 dim_im_out_y, const ai_i32 nl_pool_fused, ai_i16 *bufferA); /*! * @brief Handles dw convolutions with depth multiplier = 1 only * @ingroup lite_dw */ LITE_API_ENTRY void forward_lite_dw_sssa8_ch(const ai_i8 *Im_in, const ai_u16 dim_im_in_x, const ai_u16 dim_im_in_y, const ai_u16 ch_im_in, const ai_i8 *wt, const ai_u16 dim_kernel_x, const ai_u16 dim_kernel_y, const ai_u16 padding_x, const ai_u16 padding_y, const ai_u16 stride_x, const ai_u16 stride_y, const ai_i32 *bias, const ai_i8 In_ZeroPoint, const ai_i8 Out_ZeroPoint, ai_i8 *Im_out, const ai_u16 dim_im_out_x, const ai_u16 dim_im_out_y, const ai_i32 nl_pool_fused, ai_i16 *bufferA); /*! * @brief Handles dw convolutions with depth multiplier = 1, valid padding * and 3*3 kernel size * @ingroup lite_dw */ LITE_API_ENTRY void forward_lite_dw_3x3_sssa8_ch(const ai_i8 *Im_in, const ai_u16 dim_im_in_x, const ai_u16 dim_im_in_y, const ai_u16 ch_im_in, const ai_i8 *wt, const ai_u16 stride_x, const ai_u16 stride_y, const ai_i32 *bias, const ai_i8 In_ZeroPoint, const ai_i8 Out_ZeroPoint, ai_i8 *Im_out, const ai_u16 dim_im_out_x, const ai_u16 dim_im_out_y, const ai_i32 nl_pool_fused, ai_i16 *bufferA); /*! * @brief Handles dw convolutions with depth multiplier = 1, valid padding, * 3*3 kernel size, stride_x = 1 and weights/input are channel first * @ingroup lite_dw */ LITE_API_ENTRY void forward_lite_dw_3x3_ch1st_sssa8_ch(const ai_i8 *Im_in, const ai_u16 dim_im_in_x, const ai_u16 dim_im_in_y, const ai_u16 ch_im_in, const ai_i8 *wt, const ai_u16 stride_x, const ai_u16 stride_y, const ai_i32 *bias, const ai_i8 In_ZeroPoint, const ai_i8 Out_ZeroPoint, ai_i8 *Im_out, const ai_u16 dim_im_out_x, const ai_u16 dim_im_out_y, const ai_i32 nl_pool_fused, ai_i16 *bufferA); #endif /*LITE_DW_H*/
5,348
C
39.218045
80
0.382199
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/Middlewares/ST/AI/Inc/layers_ml.h
/** ****************************************************************************** * @file layers_ml.h * @author AST Embedded Analytics Research Platform * @brief header file of AI platform ml layers datatypes ****************************************************************************** * @attention * * Copyright (c) 2021 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** @verbatim @endverbatim ****************************************************************************** */ #ifndef LAYERS_ML_H #define LAYERS_ML_H #pragma once #include "layers_common.h" /*! * @defgroup layers_generic ML Layers Definitions * @brief definition * */ AI_API_DECLARE_BEGIN /*! * @struct ai_layer_ArrayFeatureExtractor * @ingroup layers_ml * @brief ai_layer_ArrayFeatureExtractor layer definition * * This layer select elements of the input tensor based on the indices passed. It is intended to be used * by his associated forward function @ref forward_arrayfeatureextractor */ typedef AI_ALIGNED_TYPE(struct, 4) ai_layer_arrayfeatureextractor_ { AI_LAYER_COMMON_FIELDS_DECLARE ai_tensor* indices; /*!< Indices of corrisponding axis in axes*/ } ai_layer_arrayfeatureextractor; /*! * @struct ai_layer_ZipMap * @ingroup layers_ml * @brief ai_layer_ZipMap layer definition * * This layer creates a map from the input and the attributes. * The values are provided by the input tensor, while the keys are specified by the attributes. * The user must provide keys in either classlabels_strings or classlabels_int64s (but not both). * The columns of the tensor correspond one-by-one to the keys specified by the attributes. * There must be as many columns as keys. * It is intended to be used by his associated forward function @ref forward_zipmap. */ typedef AI_ALIGNED_TYPE(struct, 4) ai_layer_zipmap_ { AI_LAYER_COMMON_FIELDS_DECLARE ai_bool has_classlabels_int; } ai_layer_zipmap; /******************************************************************************/ /* Forward Functions Section */ /******************************************************************************/ /*! * @brief select elements of the input tensor based on the indices passed. * @ingroup layers_ml * @param layer array feture extractor */ AI_INTERNAL_API void forward_arrayfeatureextractor(ai_layer* layer); /*! * @brief creates a map from the inputs and the attributes * @ingroup layers_ml * @param layer zipmap */ AI_INTERNAL_API void forward_zipmap(ai_layer* layer); AI_API_DECLARE_END #endif /*LAYERS_ML_H*/
2,899
C
28.896907
104
0.595378
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/Middlewares/ST/AI/Inc/lite_dense_is8os1ws1.h
/** ****************************************************************************** * @file lite_dense_is8os1ws1.h * @author Marco Forleo * @brief header file of AI platform lite dense kernel datatypes ****************************************************************************** * @attention * * Copyright (c) 2023 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** @verbatim @endverbatim ****************************************************************************** */ #ifndef LITE_DENSE_IS8OS1WS1_H #define LITE_DENSE_IS8OS1WS1_H #pragma once #include "ai_lite_interface.h" /******************************************************************************/ /* Forward Functions Section */ /******************************************************************************/ /*! * @brief Forward function for a dense layer with signed 8 bits input, * binary weights and binary output. * @ingroup lite_dense_is8os1ws1 * @param out_ptr The pointer to output buffer. *@param data_in_init_ptr The pointer to input buffer. * @param weights_ptr The pointer to weights. * @param scratch_ptr The pointer to scratch buffer. * @param scratch_size The value of scratch tensor size. * @param n_channel_out The number of channels of the output, i.e., * the number of dense hidden neurons. * @param n_channel_in The number of channels of the input. * @param scale_ptr The pointer to scale buffer of BN. * @param offset_ptr The pointer to offset buffer of BN. */ LITE_API_ENTRY void forward_lite_dense_is8os1ws1_bn_fxp(ai_pbits *out_ptr, const ai_i8 *data_in_init_ptr, const ai_pbits *weights_ptr, ai_i32 *scratch_ptr, const ai_u32 scratch_size, const ai_u32 n_channel_out, const ai_u32 n_channel_in, const ai_i32 *threshold_ptr); #endif /*LITE_DENSE_IS8OS1WS1_H*/
2,452
C
41.293103
80
0.472268
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/Middlewares/ST/AI/Inc/layers_list.h
/** ****************************************************************************** * @file layers_list.h * @author AST Embedded Analytics Research Platform * @brief header file of AI platform layers datatypes ****************************************************************************** * @attention * * Copyright (c) 2018 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** @verbatim @endverbatim ****************************************************************************** */ /* No sentry. This is deliberate!! */ /* Template: LAYER_ENTRY(type_, id_, struct_, forward_func_, init_func_, destroy_func_) * Where: * - type_ is the (enum) type name of the layer. to have the complete enum * value you should use the macro @ref AI_LAYER_TYPE_ENTRY(type_) that adds * the specific prefix and postfix tokens to the type_ * - id_ is the numeric id of the layer * - struct_ is the name of the datastruct of the layer without the ai_layer_ * prefix * - forward_func_ is the forward function name of the routine implementing * actual layer processing * - init_func_ is the init function name of the routine implementing * actual layer initialization * - destroy_func_ is the destroy function name of the routine implementing * actual layer de-initialization */ /* Layer IDs for stateless layers (bit 8 set) */ #define LAYER_ID(id_) \ (0x100 + (id_)) /* Layer IDs for stateful layers (bits 7 and 8 set) */ #define LAYER_STATEFUL_ID(id_) \ (0x180 + (id_)) /*!< Base layer */ LAYER_ENTRY(BASE, LAYER_ID(0), base, NULL, NULL, NULL) /*!< Elementwise addition layer */ LAYER_ENTRY(ADD, LAYER_ID(1), add, forward_add, NULL, NULL) /*!< Batch normalization layer */ LAYER_ENTRY(BN, LAYER_ID(2), bn, forward_bn, NULL, NULL) /*!< 2D Convolutional layer */ LAYER_ENTRY(CONV2D, LAYER_ID(3), conv2d, forward_conv2d, NULL, NULL) /*!< Dense layer */ LAYER_ENTRY(DENSE, LAYER_ID(4), dense, forward_dense, NULL, NULL) /*!< Local Response Normalization layer */ LAYER_ENTRY(LRN, LAYER_ID(6), lrn, forward_lrn, NULL, NULL) /*!< Nonlinearity layer */ LAYER_ENTRY(NL, LAYER_ID(7), nl, NULL, NULL, NULL) /*!< Normalization layer */ LAYER_ENTRY(NORM, LAYER_ID(8), norm, forward_norm, NULL, NULL) /*!< Merged Conv2d / Pool layer */ LAYER_ENTRY(OPTIMIZED_CONV2D, LAYER_ID(9), conv2d_nl_pool, forward_conv2d_nl_pool, NULL, NULL) /*!< Transpose Tensor layer */ LAYER_ENTRY(TRANSPOSE, LAYER_ID(10), transpose, forward_transpose, NULL, NULL) /*!< Pooling layer */ LAYER_ENTRY(POOL, LAYER_ID(11), pool, forward_pool, NULL, NULL) /*!< Softmax layer */ LAYER_ENTRY(SM, LAYER_ID(12), sm, forward_sm, NULL, NULL) /*!< Split layer */ LAYER_ENTRY(SPLIT, LAYER_ID(13), split, forward_split, NULL, NULL) /*!< TimeDelay layer */ LAYER_ENTRY(TIME_DELAY, LAYER_ID(14), time_delay, forward_time_delay, NULL, NULL) /*!< TimeDistributed layer */ LAYER_ENTRY(TIME_DISTRIBUTED, LAYER_ID(15), time_distributed, forward_time_distributed, NULL, NULL) /*!< Concat Tensor layer */ LAYER_ENTRY(CONCAT, LAYER_ID(16), concat, forward_concat, NULL, NULL) /*!< GEMM layer */ LAYER_ENTRY(GEMM, LAYER_ID(17), gemm, forward_gemm, NULL, NULL) /*!< Upsample layer */ LAYER_ENTRY(UPSAMPLE, LAYER_ID(18), upsample, forward_upsample, NULL, NULL) /*!< Container layer for eltwise operations */ LAYER_ENTRY(ELTWISE, LAYER_ID(19), eltwise, forward_eltwise, NULL, NULL) /*!< Container layer for eltwise integer operations */ LAYER_ENTRY(ELTWISE_INTEGER, LAYER_ID(20), eltwise_integer, NULL, NULL, NULL) /*!< InstanceNormalization layer */ LAYER_ENTRY(INSTANCENORMALIZATION, LAYER_ID(21), instanceNormalization, forward_instanceNormalization, NULL, NULL) /*!< Pad layer */ LAYER_ENTRY(PAD, LAYER_ID(22), pad, forward_pad, NULL, NULL) /*!< Slice layer */ LAYER_ENTRY(SLICE, LAYER_ID(23), slice, forward_slice, NULL, NULL) /*!< Tile layer */ LAYER_ENTRY(TILE, LAYER_ID(24), tile, forward_tile, NULL, NULL) /*!< Container layer for reduce operations */ LAYER_ENTRY(REDUCE, LAYER_ID(25), reduce, forward_reduce, NULL, NULL) /*!< Recurrent Neural Network layer */ LAYER_ENTRY(RNN, LAYER_ID(26), rnn, forward_rnn, NULL, NULL) /*!< Resize layer */ LAYER_ENTRY(RESIZE, LAYER_ID(27), resize, forward_resize, NULL, NULL) /*!< Gather layer */ LAYER_ENTRY(GATHER, LAYER_ID(28), gather, forward_gather, NULL, NULL) /*!< Pack layer */ LAYER_ENTRY(PACK, LAYER_ID(29), pack, forward_pack, NULL, NULL) /*!< Unpack layer */ LAYER_ENTRY(UNPACK, LAYER_ID(30), unpack, forward_unpack, NULL, NULL) /*!< ArgMax layer */ LAYER_ENTRY(ARGMAX, LAYER_ID(31), argmax, forward_argmax, NULL, NULL) /*!< ArgMin layer */ LAYER_ENTRY(ARGMIN, LAYER_ID(32), argmin, forward_argmin, NULL, NULL) /*!< Cast Neural Network Layer */ LAYER_ENTRY(CAST, LAYER_ID(33), cast, forward_cast, NULL, NULL) /*!< iForest layer */ LAYER_ENTRY(IFOREST, LAYER_ID(34), iforest, forward_iforest, NULL, NULL) /*!< SVM Regressor layer */ LAYER_ENTRY(SVMREG, LAYER_ID(35), svmreg, forward_svm_regressor, NULL, NULL) /*!< ArrayFeatureExtractor layer */ LAYER_ENTRY(ARRAYFEATUREEXTRACTOR, LAYER_ID(36), arrayfeatureextractor, forward_arrayfeatureextractor, NULL, NULL) /*!< SVM Classifier (SVC) layer */ LAYER_ENTRY(SVC, LAYER_ID(37), svc, forward_svc, NULL, NULL) /*!< ZipMap layer */ LAYER_ENTRY(ZIPMAP, LAYER_ID(38), zipmap, forward_zipmap, NULL, NULL) /*!< Where layer */ LAYER_ENTRY(WHERE, LAYER_ID(39), where, forward_where, NULL, NULL) /*!< LinearClassifier layer */ LAYER_ENTRY(LINEARCLASSIFIER, LAYER_ID(42), linearclassifier, forward_linearclassifier, NULL, NULL) /*!< TreeEnsembleClassifier layer */ LAYER_ENTRY(TREE_ENSEMBLE_CLASSIFIER, LAYER_ID(43), tree_ensemble_classifier, forward_tree_ensemble_classifier, NULL, NULL) /*!< TopK layer */ LAYER_ENTRY(TOPK, LAYER_ID(45), topK, forward_topK, NULL, NULL) /*!< ReduceLogSumExp layer */ LAYER_ENTRY(REDUCE_LOG_SUM_EXP, LAYER_ID(51), reduce_log_sum_exp, forward_reduce_log_sum_exp, NULL, NULL) /*!< ReduceL1 layer */ LAYER_ENTRY(REDUCE_L1, LAYER_ID(52), reduce_l1, forward_reduce_l1, NULL, NULL) /*!< Runtime Lite Graph Wrapper layer */ LAYER_ENTRY(LITE_GRAPH, LAYER_ID(63), lite_graph, NULL, NULL, NULL) /*!< TreeEnsembleRegressor layer */ LAYER_ENTRY(TREE_ENSEMBLE_REGRESSOR, LAYER_ID(66), tree_ensemble_regressor, forward_tree_ensemble_regressor, NULL, NULL) /*!< Deeply Quantized Dense Layers */ LAYER_ENTRY(CONV2D_DQNN, LAYER_ID(40), conv2d_dqnn, forward_pw_is1os1ws1_bn, NULL, NULL) LAYER_ENTRY(POOL_DQNN, LAYER_ID(41), pool_dqnn, forward_maxpool_is1os1, NULL, NULL) LAYER_ENTRY(DENSE_DQNN, LAYER_ID(44), dense_dqnn, forward_dense_is1os1ws1, NULL, NULL) /*!< Reverse layer */ LAYER_ENTRY(REVERSE, LAYER_ID(50), reverse, forward_reverse, NULL, NULL) /*****************************************************************************/ /*!< Base Stateful Layer type */ LAYER_ENTRY(STATEFUL, LAYER_STATEFUL_ID(0), stateful, NULL, NULL, NULL) /*!< Long Short Time Memory layer */ LAYER_ENTRY(LSTM, LAYER_STATEFUL_ID(1), lstm, forward_lstm, init_lstm, destroy_lstm) /*!< Custom layer */ LAYER_ENTRY(CUSTOM, LAYER_STATEFUL_ID(2), custom, NULL, NULL, NULL) /*!< Gated Recurrent Unit layer */ LAYER_ENTRY(GRU, LAYER_STATEFUL_ID(3), gru, forward_gru, init_gru, destroy_gru) /*!< Stateless Template layer declaration */ /* LAYER_ENTRY(TEMPLATE, LAYER_ID(XX), template, forward_template, NULL, NULL) */ /*!< Stateful Template layer declaration */ /* LAYER_ENTRY(TEMPLATE, LAYER_STATEFUL_ID(XX), template, forward_template, init_template, destroy_template) */ #undef LAYER_ENTRY #undef LAYER_ID #undef LAYER_STATEFUL_ID
7,821
C
45.838323
123
0.671781
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/Middlewares/ST/AI/Inc/lite_pad_generic.h
/** ****************************************************************************** * @file lite_pad_generic.h * @author AIS * @brief header file of AI platform lite padding kernel datatypes ****************************************************************************** * @attention * * Copyright (c) 2022 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** @verbatim @endverbatim ****************************************************************************** */ #ifndef LITE_PADDING_DQNN_H #define LITE_PADDING_DQNN_H #pragma once #include "ai_lite_interface.h" /******************************************************************************/ /* Forward Functions Section */ /******************************************************************************/ /*! * @brief Handles padding with 8 bits input/output in constant mode - Lite I/F * Channel 1st Format Input and Output * @ingroup lite_padding_dqnn */ LITE_API_ENTRY void forward_lite_pad_8bit_ch1st_3x3_constant(ai_ptr_const in_data_tensor, ai_ptr out_data_tensor, const ai_handle fill_value, const ai_i32 height_in, const ai_i32 channel_in, const ai_ptr_offset ch_stride_in, const ai_ptr_offset h_stride_in, const ai_ptr_offset h_stride_pad); /*! * @brief Handles padding with 8 bits input/output in constant mode - Lite I/F * @ingroup lite_padding_dqnn */ LITE_API_ENTRY void forward_lite_pad_constant(ai_ptr_const in_data, ai_ptr out_data, const ai_handle fill_value, const ai_i16 in_bits, const ai_i32 height_in, const ai_ptr_offset ch_stride_in, const ai_ptr_offset h_stride_in, const ai_ptr_offset h_stride_pad, const ai_ptr_offset h_stride_pad_b, const ai_ptr_offset w_stride_pad, const ai_ptr_offset w_stride_pad_r); /*! * @brief Handles padding with 8 bits input/output in edge mode - Lite I/F * @ingroup lite_padding_dqnn */ void forward_lite_pad_edge(ai_ptr_const in_data_tensor, ai_ptr out_data, const ai_i32 height_in, const ai_i16 pads_y, const ai_i16 pads_x_r, const ai_ptr_offset h_stride_in, const ai_ptr_offset w_stride_in, const ai_ptr_offset h_stride_out, const ai_ptr_offset h_stride_pad, const ai_ptr_offset w_stride_pad, const ai_ptr_offset h_stride_pad_b); /*! * @brief Handles padding with 8 bits input/output in reflect mode - Lite I/F * @ingroup lite_padding_dqnn */ void forward_lite_pad_reflect(ai_ptr_const in_data, ai_ptr out_data, const ai_i32 depth, const ai_i32 height_in, const ai_i32 width_in, const ai_i32 height_out, const ai_i32 width_out, const ai_ptr_offset h_stride_in, const ai_ptr_offset w_stride_in, const ai_ptr_offset h_stride_out, const ai_ptr_offset w_stride_out, const ai_i16 pads_x, const ai_i16 pads_y, const ai_i16 pads_y_b, const ai_ptr_offset h_stride_pad, const ai_ptr_offset w_stride_pad, const ai_ptr_offset w_stride_pad_r); #endif /*LITE_PADDING_GENERIC_H*/
4,546
C
43.145631
80
0.421909
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/Middlewares/ST/AI/Inc/ai_datatypes_format.h
/** ****************************************************************************** * @file ai_datatypes_format.h * @author AST Embedded Analytics Research Platform * @brief Definitions of AI platform private format handling routines ****************************************************************************** * @attention * * Copyright (c) 2017 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** @verbatim @endverbatim ****************************************************************************** */ #ifndef AI_DATATYPES_FORMAT_H #define AI_DATATYPES_FORMAT_H #pragma once #include "ai_platform.h" #include "ai_datatypes_defines.h" // #include "core_datatypes.h" /*! * @defgroup ai_datatypes_format Definiton and Macro of array and buffer formats * @brief Type definition and implementation of internal @ref ai_array and * @ref ai_buffer formats. * @details The library handles 2 different kind of formats: an internal format * that is part of the @ref ai_array struct that is a packed 32bit representation * of the format attributes, and a public format (used in public APIs) associated * with @ref ai_buffer struct , defined as enum in @ref ai_platform.h, * that is just an enum type. Converters are provided in this header file to * convert from one format representation to another. * Some MSB bits are reserved in both formats to code some bit flag useful to * declare some special attribute. Three flags are actually implemented in both * formats: the @ref AI_BUFFER_FMT_FLAG_CONST and @ref AI_FMT_FLAG_CONST used * to tag read-only memory buffers, @ref AI_BUFFER_FMT_FLAG_STATIC and * @ref AI_FMT_FLAG_STATIC to mark statically allocated memory buffers and * @ref AI_FMT_FLAG_SCRATCH_BUFFER to tag temporary scratch buffers. * All the formats are declared in a proper tuple organize table header named * @ref format_lists.h that enumerates all the formats available for the library. * A new format could be added easily by adding a new FMY_ENTRY() as required. * The preprocessor automatically generates the code for the handling of the * format according to this tuples entry. A rational for the methodology could * be found here: * - https://codecraft.co/2012/10/29/how-enums-spread-disease-and-how-to-cure-it/ * * The 32bits internal format fields are organized as follows: * * MSB LSB * 31 25 24 23 21 17 14 7 0 * /---------------------------------------------------------------------------/ * / ATTR. FLAGS | FLOAT | SIGN | LDIV | TYPE | PMASK | BITS | FBITS / * /---------------------------------------------------------------------------/ * Where: * - FLAGS: is the reserved bits to store additional format attributes (e.g. * I/O / STATIC flags. etc.) * - FLOAT: 1 bit mark the format as floating point type * - SIGN : 1 bit mark the format as signed type * - LDIV : 2 bits is a log2 value that is used to compute elements size * with some special format such as the compressed ones. It is a shift * factor usually set to zero * - TYPE : 4 bits mark the format "family" type. Actually 5 families are coded, * @ref AI_FMT_FLOAT (float types) * @ref AI_FMT_Q (fixed-point types in Qm.n format) * @ref AI_FMT_BOOL (boolean type) * @ref AI_FMT_LUT4 (compressed lookup 16 formats) * @ref AI_FMT_LUT8 (compressed lookup 256 formats) * - PMASK 3 bits padding mask used to set the optional dimension for padding * to handle special aligned formats/ E.g. a 1 bit format * Usually this is set to 0x0 * - BITS 7 bits set the total number of bits of the element, padding bits * excluded. The bits are thus = sign bit + fractional bits + integer bits * The number of integer bits could thus be known using the @ref * AI_FMT_GET_IBITS() macro. * - FBITS 7 bits set the number of fractional bits in the format * * * A reference code snippet for usage is the test unit that uses this header: * * \include test/test_lcut_formats.cpp * */ /*! * Format bitfields definition. NOTE: 7 MSB are masked off * for (optional) atributes setting using flags. see @ref AI_FMT_FLAG_CONST that * is used for marking a data as constant readonly */ /* 1 bit field to identify floating point values*/ #define _FMT_FLOAT_MASK (0x1) #define _FMT_FLOAT_BITS (24) /*! 1 bit sign info */ #define _FMT_SIGN_MASK (0x1) #define _FMT_SIGN_BITS (23) /*! fractional bits field (i.e. for Q formats see @ref AI_FMT_Q) */ #define _FMT_FBITS_MASK (0x7F) #define _FMT_FBITS_BITS (0) #define _FMT_FBITS_BIAS ((_FMT_FBITS_MASK+1) >> 1) /*! TOTAL number of bits (fractional+integer+sign) (excluded padding ones) */ #define _FMT_BITS_MASK (0x7F) #define _FMT_BITS_BITS (7) #define _FMT_BITS_BIAS (0) /*! Padding bits for handling formats not aligned to multiples of 8 bits */ #define _FMT_PMASK_MASK (0x7) #define _FMT_PMASK_BITS (14) /*! bits reserved for identifying the family format, e.g. float, fixed-point..*/ #define _FMT_TYPE_MASK (0xF) #define _FMT_TYPE_BITS (17) #define _FMT_LDIV_MASK (0x3) #define _FMT_LDIV_BITS (21) /******************************************************************************/ #define AI_FMT_OBJ(fmt_) ((ai_array_format)(fmt_)) /*! * Only 25 LSB bits are used for storing actual format bits. 7 bits are reserved * for format attributes, see @ref AI_FMT_FLAG_CONST flag */ #define AI_FMT_FLAG_BITS (25) #define AI_FMT_MASK ((0x1<<AI_FMT_FLAG_BITS)-1) #define AI_FMT_FLAG_CONST (0x1<<30) #define AI_FMT_FLAG_STATIC (0x1<<29) #define AI_FMT_FLAG_SCRATCH_BUFFER (0x1<<28) #define AI_FMT_FLAG_IS_IO (0x1<<27) #define AI_FMT_FLAG_VISITED (0x1<<26) /******************************************************************************/ /*! * Format "Class" type : this identify the family of the format: * float, integer, fixed point (i.e. Q format), compressed via lookup table */ #define AI_FMT_NONE (0x0) #define AI_FMT_FLOAT (0x1) #define AI_FMT_Q (0x2) #define AI_FMT_BOOL (0x3) #define AI_FMT_LUT4 (0x4) #define AI_FMT_LUT8 (0x8) #define AI_FMT_QMASK \ ( (_FMT_FBITS_MASK<<_FMT_FBITS_BITS) | \ (_FMT_BITS_MASK<<_FMT_BITS_BITS) | \ (_FMT_PMASK_MASK<<_FMT_PMASK_BITS) ) #define AI_FMT_BINARY_MASK \ (AI_FMT_MASK & (~(_FMT_SIGN_MASK<<_FMT_SIGN_BITS))) #define AI_FMT_IS_BINARY(val_) \ (((val_) & AI_FMT_BINARY_MASK) == AI_ARRAY_FORMAT_U1) #define AI_FMT_GET(val_) \ ( (AI_FMT_OBJ(val_)) & AI_FMT_MASK ) #define AI_FMT_MASK_Q(val_) \ ( AI_FMT_OBJ(val_) & (~(AI_FMT_QMASK)) ) #define AI_FMT_GET_Q(val_) \ ( AI_FMT_MASK_Q(val_) | AI_FMT_SET_BITS(0) | AI_FMT_SET_FBITS(0) ) #define AI_FMT_GET_FLAGS(val_) \ ( ((AI_FMT_OBJ(val_)) & (~AI_FMT_MASK)) >> AI_FMT_FLAG_BITS ) #define AI_FMT_SAME(fmt1_, fmt2_) \ ( AI_FMT_GET(fmt1_) == AI_FMT_GET(fmt2_) ) #define _FMT_SET(val, mask, bits) AI_FMT_OBJ(((val)&(mask))<<(bits)) #define _FMT_GET(fmt, mask, bits) ((AI_FMT_OBJ(fmt)>>(bits))&(mask)) #define AI_FMT_SET_FLOAT(val) _FMT_SET(val, _FMT_FLOAT_MASK, _FMT_FLOAT_BITS) #define AI_FMT_GET_FLOAT(fmt) _FMT_GET(fmt, _FMT_FLOAT_MASK, _FMT_FLOAT_BITS) #define AI_FMT_SET_SIGN(val) _FMT_SET(val, _FMT_SIGN_MASK, _FMT_SIGN_BITS) #define AI_FMT_GET_SIGN(fmt) _FMT_GET(fmt, _FMT_SIGN_MASK, _FMT_SIGN_BITS) #define AI_FMT_SET_PMASK(val) _FMT_SET(val, _FMT_PMASK_MASK, _FMT_PMASK_BITS) #define AI_FMT_GET_PMASK(fmt) _FMT_GET(fmt, _FMT_PMASK_MASK, _FMT_PMASK_BITS) #define AI_FMT_SET_TYPE(val) _FMT_SET(val, _FMT_TYPE_MASK, _FMT_TYPE_BITS) #define AI_FMT_GET_TYPE(fmt) _FMT_GET(fmt, _FMT_TYPE_MASK, _FMT_TYPE_BITS) #define AI_FMT_SET_LDIV(val) _FMT_SET(val, _FMT_LDIV_MASK, _FMT_LDIV_BITS) #define AI_FMT_GET_LDIV(fmt) _FMT_GET(fmt, _FMT_LDIV_MASK, _FMT_LDIV_BITS) #define AI_FMT_SET_BITS(val) \ _FMT_SET((val) + _FMT_BITS_BIAS, _FMT_BITS_MASK, _FMT_BITS_BITS) #define AI_FMT_GET_BITS(fmt) \ ((ai_i8)_FMT_GET(fmt, _FMT_BITS_MASK, _FMT_BITS_BITS) - _FMT_BITS_BIAS) #define AI_FMT_SET_FBITS(val) \ _FMT_SET((val) + _FMT_FBITS_BIAS, _FMT_FBITS_MASK, _FMT_FBITS_BITS) #define AI_FMT_GET_FBITS(fmt) \ ((ai_i8)_FMT_GET(fmt, _FMT_FBITS_MASK, _FMT_FBITS_BITS) - _FMT_FBITS_BIAS) /*! * The total number of bits for a given format is supposed to be the sum of the * bits + padding bits. This means that the number of integer bits is derived * as follow: int_bits = bits - fbits (fractional bits) - 1 (for the sign) */ #define AI_FMT_GET_BITS_SIZE(fmt_) \ AI_FMT_GET_BITS(fmt_) /*! Macro used to compute the integer bits for a format */ #define AI_FMT_GET_IBITS(fmt_) \ ((ai_i16)AI_FMT_GET_BITS(fmt_)-AI_FMT_GET_FBITS(fmt_)-AI_FMT_GET_SIGN(fmt_)) /*! ai_buffer format handlers section *****************************************/ #define AI_BUFFER_FMT_MASK_Q(fmt_) \ ( AI_BUFFER_FMT_OBJ(fmt_) & 0xFFFFC000 ) #define AI_BUFFER_FMT_GET_Q(fmt_) \ ( AI_BUFFER_FMT_MASK_Q(fmt_) | AI_BUFFER_FMT_SET_FBITS(0) | \ AI_BUFFER_FMT_SET_FBITS(0) ) #define AI_BUFFER_FMT_SET_Q(bits_, fbits_) \ AI_BUFFER_FMT_SET(AI_BUFFER_FMT_TYPE_Q, 1, 0, bits_, fbits_) #define AI_BUFFER_FMT_IS_Q(fmt_) \ ( (AI_BUFFER_FMT_TYPE_Q==AI_BUFFER_FMT_GET_TYPE(fmt_)) && \ (1==AI_BUFFER_FMT_GET_SIGN(fmt_)) ) #define AI_BUFFER_FMT_SET_UQ(bits_, fbits_) \ AI_BUFFER_FMT_SET(AI_BUFFER_FMT_TYPE_Q, 0, 0, bits_, fbits_) #define AI_BUFFER_FMT_IS_UQ(fmt_) \ ( (AI_BUFFER_FMT_TYPE_Q==AI_BUFFER_FMT_GET_TYPE(fmt_)) && \ (0==AI_BUFFER_FMT_GET_SIGN(fmt_)) ) /*! Q ai_array format handlers ************************************************/ #define AI_ARRAY_FMT_Q(bits_, fbits_) \ ( AI_FMT_MASK_Q(AI_ARRAY_FORMAT_Q) | AI_FMT_SET_BITS(bits_) | AI_FMT_SET_FBITS(fbits_) ) #define AI_ARRAY_FMT_SET_Q(bits_, fbits_) \ AI_ARRAY_FMT_Q(bits_, fbits_) #define AI_ARRAY_FMT_IS_Q(fmt_) \ ( AI_FMT_GET(AI_FMT_MASK_Q(AI_ARRAY_FORMAT_Q))==AI_FMT_GET(AI_FMT_MASK_Q(fmt_)) ) #define AI_ARRAY_FMT_UQ(bits_, fbits_) \ ( AI_FMT_MASK_Q(AI_ARRAY_FORMAT_UQ) | AI_FMT_SET_BITS(bits_) | AI_FMT_SET_FBITS(fbits_) ) #define AI_ARRAY_FMT_SET_UQ(bits_, fbits_) \ AI_ARRAY_FMT_UQ(bits_, fbits_) #define AI_ARRAY_FMT_IS_UQ(fmt_) \ ( AI_FMT_GET(AI_FMT_MASK_Q(AI_ARRAY_FORMAT_UQ))==AI_FMT_GET(AI_FMT_MASK_Q(fmt_)) ) AI_DEPRECATED /* Alias for AI_ARRAY_FMT_SET_Q */ #define AI_ARRAY_FMT_SET_SQ(bits_, fbits_) \ AI_ARRAY_FMT_SET_Q(bits_, fbits_) AI_DEPRECATED /* Alias for AI_ARRAY_FMT_IS_Q */ #define AI_ARRAY_FMT_IS_SQ(fmt_) \ AI_ARRAY_FMT_IS_Q(fmt_) /*! ai_array section **********************************************************/ #define AI_ARRAY_FMT_ENTRY(name_) \ AI_CONCAT(AI_ARRAY_FORMAT_, name_) #define AI_ARRAY_FMT_NAME(fmt_) \ ai_array_fmt_name(fmt_) #define AI_ARRAY_FMT_VALID(fmt_) \ ai_array_fmt_valid(fmt_) #define AI_ARRAY_FMT_EXPORTED(fmt_) \ ai_array_fmt_exported(fmt_) #define AI_ARRAY_FMT_GET_FORMATS(formats_) \ ai_array_fmt_get_formats(formats_) #define AI_ARRAY_TO_BUFFER_FMT(fmt_) \ ai_array_to_buffer_fmt(fmt_) #define AI_ARRAY_GET_BYTE_SIZE(fmt_, count_) \ ai_array_get_byte_size(fmt_, count_) #define AI_ARRAY_GET_DATA_BYTE_SIZE(fmt_, count_) \ ai_array_get_data_byte_size(fmt_, count_) #define AI_ARRAY_GET_ELEMS_FROM_SIZE(fmt_, size_) \ ai_array_get_elems_from_size(fmt_, size_) AI_API_DECLARE_BEGIN /*! * @typedef ai_array_format * @ingroup ai_datatypes_format * @brief Generic Data Format Specifier for @ref ai_array (32bits packed info) */ typedef int32_t ai_array_format; /*! * @enum internal data format enums * @ingroup ai_datatypes_format * @brief Generic Data Format Specifier (32bits packed info) */ typedef enum { #define FMT_ENTRY(exp_, name_, type_id_, sign_bit_, float_bit_, \ pmask_, bits_, fbits_, ldiv_bits_) \ AI_ARRAY_FMT_ENTRY(name_) = (AI_FMT_SET_FLOAT(float_bit_) | \ AI_FMT_SET_SIGN(sign_bit_) | \ AI_FMT_SET_BITS(bits_) | \ AI_FMT_SET_FBITS(fbits_) | \ AI_FMT_SET_PMASK(pmask_) | \ AI_FMT_SET_TYPE(type_id_) | \ AI_FMT_SET_LDIV(ldiv_bits_)), #include "formats_list.h" } ai_array_format_entry; /*! * @brief Get a human readable string from the format ID value * @ingroup ai_datatypes_format * @param[in] type the @ref ai_array_format to print out * @return a string with a human readable name of the format */ AI_INTERNAL_API const char* ai_array_fmt_name(const ai_array_format type); /*! * @brief Check if @ref ai_array_format is a exportable to an @ref ai_buffer_format * @ingroup ai_datatypes_format * @param[in] type the ai_array_format to check * @return true if the format is exported, false otherwise */ AI_INTERNAL_API ai_bool ai_array_fmt_exported(const ai_array_format type); /*! * @brief Check if @ref ai_array_format is a valid format present in the list of * supported formats * @ingroup ai_datatypes_format * @param[in] type the ai_array_format to check * @return true if the format is valid, false otherwise */ AI_INTERNAL_API ai_bool ai_array_fmt_valid(const ai_array_format type); /*! * @brief Get the complete list of supported @ref ai_array_format formats * @ingroup ai_datatypes_format * @param[out] formats a pointer to an array withj all supported formats listed * @return the number of supported formats */ AI_INTERNAL_API ai_size ai_array_fmt_get_formats(const ai_array_format** formats); /*! ai_buffer section ********************************************************* * Only 25 LSB bits are used for storing actual format bits. 7 bits are reserved * for format atrtributes, see @ref AI_FMT_FLAG_CONST flag */ #define AI_BUFFER_FMT_ENTRY(name_) \ AI_CONCAT(AI_BUFFER_FORMAT_, name_) #define AI_BUFFER_FMT_NAME(type_) \ ai_buffer_fmt_name(type_) #define AI_BUFFER_FMT_VALID(type_) \ ai_buffer_fmt_valid(type_) #define AI_BUFFER_FMT_GET_FORMATS(formats_) \ ai_buffer_fmt_get_formats(formats_) #define AI_BUFFER_TO_ARRAY_FMT(fmt_) \ ai_buffer_to_array_fmt(fmt_) #define AI_BUFFER_GET_BITS_SIZE(fmt) \ AI_ARRAY_GET_BITS_SIZE(AI_BUFFER_TO_ARRAY_FMT(fmt)) /*! * @brief Get a human readable string from the format ID value * @ingroup ai_datatypes_format * @param[in] type the @ref ai_buffer_format to print out * @return a string with a human readable name of the format */ AI_INTERNAL_API const char* ai_buffer_fmt_name( const ai_buffer_format type); /*! * @brief Check if @ref ai_buffer_format is a valid format present in the list * of supported formats * @ingroup ai_datatypes_format * @param[in] type the @ref ai_buffer_format to check * @return true if the format is valid, false otherwise */ AI_INTERNAL_API ai_bool ai_buffer_fmt_valid( const ai_buffer_format type); /*! * @brief Get the complete list of supported @ref ai_buffer_format formats * @ingroup ai_datatypes_format * @param[out] formats a pointer to an array with all supported formats listed * @return the number of supported formats */ AI_INTERNAL_API ai_size ai_buffer_fmt_get_formats( const ai_buffer_format** formats); /*! Conversions section *******************************************************/ /*! * @brief Convert from ai_array_format to ai_buffer_format. * @ingroup ai_datatypes_format * @param fmt the input ai_array_format to convert * @return the converted format as a ai_buffer_format */ AI_INTERNAL_API ai_buffer_format ai_array_to_buffer_fmt( const ai_array_format fmt); /*! * @brief Convert from ai_buffer_format to ai_array_format. * @ingroup ai_datatypes_format * @param fmt the input ai_buffer_format to convert * @return the converted format as a ai_array_format */ AI_INTERNAL_API ai_array_format ai_buffer_to_array_fmt( const ai_buffer_format fmt); /** helpers section ***********************************************************/ /*! * @brief Computes the size in bytes given an ai_array_format and number of * array elements. * @details This routine computes from the number of elements of the array its * size in bytes. If the array is referred by a tensor structure, it is the task * of the latter to handle per-dimension padding (e.g. to align odd rows in a * 4-bit matrix. At array level the padding elements MUST be included in the * number of elements. * @ingroup ai_datatypes_format * @param[in] fmt the input array format as an ai_array_format * @param[in] count the number of elements stored in the data array * @return the size in bytes of the array given the specific format and number * of elements (including padding elements) */ AI_INTERNAL_API ai_size ai_array_get_byte_size( const ai_array_format fmt, const ai_size count); /*! * @brief Computes the size in bytes given an ai_array_format and number of * array elements of the data fields (e.g. LUT table size excluded). * @details This routine computes from the number of elements of the array its * size in bytes. If the array is referred by a tensor structure, it is the task * of the latter to handle per-dimension padding (e.g. to align odd rows in a * 4-bit matrix. At array level the padding elements MUST be included in the * number of elements. * @ingroup ai_datatypes_format * @param[in] fmt the input array format as an ai_array_format * @param[in] count the number of elements stored in the data array * @return the size in bytes of the array given the specific format and number * of elements (including padding elements) */ AI_INTERNAL_API ai_size ai_array_get_data_byte_size( const ai_array_format fmt, const ai_size count); /*! * @brief Computes the number of elements from ai_array_format and * the size in byte of the array. * @ingroup ai_datatypes_format * @param fmt the input array format as an ai_array_format * @param size the size in bytes of the array * @return the number of elements that could be stored given the format */ AI_INTERNAL_API ai_size ai_array_get_elems_from_size( const ai_array_format fmt, const ai_size byte_size); AI_API_DECLARE_END #endif /*AI_DATATYPES_FORMAT_H*/
18,640
C
36.965377
91
0.635569
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/Middlewares/ST/AI/Inc/core_net_inspect.h
/** ****************************************************************************** * @file core_net_inspect.h * @author AST Embedded Analytics Research Platform * @brief header file of core network inspection APIs ****************************************************************************** * @attention * * Copyright (c) 2018 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** @verbatim @endverbatim ****************************************************************************** */ #ifndef __CORE_NET_INSPECT_H_ #define __CORE_NET_INSPECT_H_ #pragma once #include "core_net_inspect_interface.h" #include "core_common.h" #include "layers_common.h" /*! * @defgroup core_net_inspect Core Network Inspection routines * @brief Implementation of core network inspection routines that allows to * inspect on a node basis a generated network model * @details A network context @ref ai_network basically contains a chained list * of nodes @ref ai_node that have an associated forward function. * Each ai)network context and ai_node datastructs have as a required member * field an opaque handler (i.e. a void pointer) to a klass object. * This handler is intended to be used as a platform specific node context * that implements specific target platform routines. * The inspector module basically acts as a plugin that exploiting these features * by temporary creating an hidden inspection context (see * @ref ai_core_inspect_net_klass) associated to the network and * linking it by re-routing the klass field to this inspection context. The * inspection context saves as part of its state (by a stack push operation), the * internal state of the network (all node / network klass pointers and actual * forward functions). * Thus, for each node it re-routes all node's forward functions to a dedicated * inspection forward function (see @ref _forward_inspect_validate() routine) * This routine is the core of the mechanism and it allows to inspect a network * node by node. Some additional inspection could thus be done inside the * _forward_inspect_validate() routine before and after the actual node * forward function is called; * */ AI_API_DECLARE_BEGIN /*! * @defgroup core_net_inspect Network Inspection Core * @brief Implementation of the validation network routines */ /*! * @brief Initialize the network inspection context on a given network * @ingroup core net inspect * @param network opaque handler to the network instance * @param cfg a pointer to the inspector configuration we want to use * @return true if execution of the API is fine, false otherwise */ AI_API_ENTRY ai_bool ai_network_inspect_init( ai_handle network, const ai_inspect_config* cfg); /*! * @brief Get a summary report from the inspected network * @ingroup core net inspect * @param network opaque handler to the network instance * @param report a pointer to the report provided back by the inspection * @return true if execution of the API is fine, false otherwise */ AI_API_ENTRY ai_bool ai_network_inspect_get_report( ai_handle network, ai_inspect_net_report* report); /*! * @brief Destroy the network inspection context on a given network * @ingroup core net inspect * @param network opaque handler to the network instance * @return true if execution of the API is fine, false otherwise */ AI_API_ENTRY ai_bool ai_network_inspect_destroy(ai_handle network); AI_API_DECLARE_END #endif /*__CORE_NET_INSPECT_H_*/
3,780
C
37.581632
81
0.682804
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/Middlewares/ST/AI/Inc/lite_nl_generic_integer.h
#ifndef LITE_NL_GENERIC_INTEGER_H #define LITE_NL_GENERIC_INTEGER_H #pragma once #include "ai_lite_interface.h" /** * @brief forward lite function for a s8 softmax non-linearity where the softmax is applied per channel. * @ingroup lite_nl_generic_integer * @param output The pointer to output buffer (s8). * @param input The pointer to input buffer (s8). * @param in_size. The size of the input (including channels). * @param ch_size The nsize of each channel. * @param in_ch_step The step between consecutive elements (inputs) * @param out_ch_step The step between consecutive elements (outputs) * @param mult * @param shift * @param min_diff */ LITE_API_ENTRY void forward_lite_nl_softmax_is8os8( ai_i8* out_ptr, const ai_i8* in_ptr, const ai_size in_size, const ai_size ch_size, const ai_i32 in_ch_step, const ai_i32 out_ch_step, const ai_i32 mult, const ai_i32 shift, const ai_i32 min_diff, ai_i32* scratch); /** * @brief forward lite function for a u8 softmax non-linearity where the softmax is applied per channel. * @ingroup lite_nl_generic_integer * @param output The pointer to output buffer (s8). * @param input The pointer to input buffer (s8). * @param in_size. The size of the input (including channels). * @param ch_size The nsize of each channel. * @param in_ch_step The step between consecutive elements (inputs) * @param out_ch_step The step between consecutive elements (outputs) * @param mult * @param shift * @param min_diff */ LITE_API_ENTRY void forward_lite_nl_softmax_iu8ou8( ai_u8* out_ptr, const ai_u8* in_ptr, const ai_size in_size, const ai_size ch_size, const ai_i32 in_ch_step, const ai_i32 out_ch_step, const ai_i32 mult, const ai_i32 shift, const ai_i32 min_diff, ai_i32* scratch); #endif /* LITE_NL_GENERIC_INTEGER_H */
1,815
C
34.607842
104
0.713499
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/Middlewares/ST/AI/Inc/ai_datatypes.h
/** ****************************************************************************** * @file ai_datatypes.h * @author AST Embedded Analytics Research Platform * @brief Definitions of AI platform private APIs types ****************************************************************************** * @attention * * Copyright (c) 2017 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** @verbatim @endverbatim ****************************************************************************** */ #ifndef AI_DATATYPES_H #define AI_DATATYPES_H #pragma once #include <string.h> #include "ai_platform.h" #include "ai_platform_interface.h" /*! * @defgroup datatypes Platform Interface Datatypes * @brief Data structures used by AI platform to implement neural networks * */ /** Count Variable Number of Arguments (up to 64 elements) *******************/ #define AI_NUMARGS(...) \ PP_NARG_(__VA_ARGS__,PP_RSEQ_N()) #define PP_NARG_(...) \ PP_ARG_N(__VA_ARGS__) #define PP_ARG_N( \ _1, _2, _3, _4, _5, _6, _7, _8, _9,_10, \ _11,_12,_13,_14,_15,_16,_17,_18,_19,_20, \ _21,_22,_23,_24,_25,_26,_27,_28,_29,_30, \ _31,_32,_33,_34,_35,_36,_37,_38,_39,_40, \ _41,_42,_43,_44,_45,_46,_47,_48,_49,_50, \ _51,_52,_53,_54,_55,_56,_57,_58,_59,_60, \ _61,_62,_63,N,...) N #define PP_RSEQ_N() \ 63,62,61,60, \ 59,58,57,56,55,54,53,52,51,50, \ 49,48,47,46,45,44,43,42,41,40, \ 39,38,37,36,35,34,33,32,31,30, \ 29,28,27,26,25,24,23,22,21,20, \ 19,18,17,16,15,14,13,12,11,10, \ 9,8,7,6,5,4,3,2,1,0 /*****************************************************************************/ #define AI_PTR_ALIGN(ptr, alignment) \ ((((ai_uptr)(ptr))+((ai_uptr)(alignment)-1))&(~((ai_uptr)(alignment)-1))) /*! * @typedef ai_offset * @ingroup ai_datatypes_internal * @brief Generic index offset type */ typedef int32_t ai_offset; AI_API_DECLARE_BEGIN AI_API_DECLARE_END #endif /* AI_DATATYPES_H */
2,349
C
29.51948
80
0.473819
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/Middlewares/ST/AI/Inc/ai_common_config.h
/** ****************************************************************************** * @file ai_common_config.h * @author AST Embedded Analytics Research Platform * @brief header file of AI platform common compile configuration defines ****************************************************************************** * @attention * * Copyright (c) 2018 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** @verbatim @endverbatim ****************************************************************************** */ #ifndef AI_COMMON_CONFIG_H #define AI_COMMON_CONFIG_H #pragma once /*! * @defgroup layers Layers Compilation Config Definitions * @brief definition * */ #define HAS_PROFILE_FLOAT #define HAS_PROFILE_FIXED #endif /*AI_COMMON_CONFIG_H*/
1,070
C
28.749999
80
0.495327
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/Middlewares/ST/AI/Inc/lite_pw_dqnn.h
/** ****************************************************************************** * @file lite_pw_dqnn.h * @author AIS * @brief header file of AI platform lite pw kernel datatypes ****************************************************************************** * @attention * * Copyright (c) 2021 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** @verbatim @endverbatim ****************************************************************************** */ #ifndef LITE_PW_DQNN_H #define LITE_PW_DQNN_H #pragma once #include "ai_lite_interface.h" /******************************************************************************/ /* Forward Functions Section */ /******************************************************************************/ /*! * @brief Handles point wise convolution with binary input, binary output and * binary weights - Lite API version * @ingroup lite_pw_dqnn */ LITE_API_ENTRY void forward_lite_pw_is1os1ws1_bn(const ai_u32 *pDataIn_init, ai_u32 *pDataOut_init, const ai_u32 *pWeights_init, const ai_u32 n_channel_in, const ai_u32 n_channel_out, const ai_i32 width_out, const ai_i32 height_out, const ai_i32 *pThreshold); /*! * @brief Handles point wise convolution with binary input, binary output and * binary weights - Lite API version - Optimized thanks to Optim2 * assumptions * @ingroup lite_pw_dqnn */ LITE_API_ENTRY void forward_lite_pw_is1os1ws1_bn_optim2(const ai_u32 *pDataIn_init, ai_u32 *pDataOut_init, const ai_u32 *pWeights_init, const ai_u32 n_channel_in, const ai_u32 n_channel_out, const ai_i32 width_out, const ai_i32 height_out, const ai_i32 *pThreshold); /*! * @brief Handles point wise convolution with binary input, 8-bits output and * binary weights - Lite API version * @ingroup lite_pw_dqnn */ LITE_API_ENTRY void forward_lite_pw_is1os8ws1_bn(const ai_u32 *pDataIn_init, ai_i8 *pDataOut_init, const ai_u32 *pWeights_init, const ai_u32 n_channel_in, const ai_u32 n_channel_out, const ai_i32 width_out, const ai_i32 height_out, const ai_float *pScale, const ai_float *pOffset); /*! * @brief Handles point wise convolution with binary input, 8-bits output and * binary weights - Lite API version - Optimized thanks to Optim1 * assumptions * @ingroup lite_pw_dqnn */ LITE_API_ENTRY void forward_lite_pw_is1os8ws1_bn_optim1(const ai_u32 *pDataIn_init, ai_i8 *pDataOut_init, const ai_u32 *pWeights_init, const ai_u32 n_channel_in, const ai_u32 n_channel_out, const ai_i32 width_out, const ai_i32 height_out, const ai_float *pScale, const ai_float *pOffset); /*! * @brief Handles point-wise convolution with binary input, float32 output * and binary weights - Lite API version * @ingroup lite_pw_dqnn */ LITE_API_ENTRY void forward_lite_pw_is1of32ws1_bn(const ai_u32 *pDataIn_init, ai_float *pDataOut_init, const ai_u32 *pWeights_init, const ai_u32 n_channel_in, const ai_u32 n_channel_out, const ai_i32 width_out, const ai_i32 height_out, const ai_float *pScale, const ai_float *pOffset); /*! * @brief Handles point-wise convolution with binary input, float32 output * and binary weights - Lite API version - Optimized thanks to Optim1 * assumptions * @ingroup lite_pw_dqnn */ LITE_API_ENTRY void forward_lite_pw_is1of32ws1_bn_optim1(const ai_u32 *pDataIn_init, ai_float *pDataOut_init, const ai_u32 *pWeights_init, const ai_u32 n_channel_in, const ai_u32 n_channel_out, const ai_i32 width_out, const ai_i32 height_out, const ai_float *pScale, const ai_float *pOffset); #endif /*LITE_PW_DQNN_H*/
5,632
C
42
80
0.430753
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/Middlewares/ST/AI/Inc/ai_platform.h
/** ****************************************************************************** * @file ai_platform.h * @author AST Embedded Analytics Research Platform * @brief Definitions of AI platform public APIs types ****************************************************************************** * @attention * * Copyright (c) 2017 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** @verbatim @endverbatim ****************************************************************************** */ #ifndef AI_PLATFORM_H #define AI_PLATFORM_H #pragma once #include <stdint.h> #include <stddef.h> #include <inttypes.h> #ifndef AI_PLATFORM_API_MAJOR #define AI_PLATFORM_API_MAJOR (1) #endif #ifndef AI_PLATFORM_API_MINOR #define AI_PLATFORM_API_MINOR (2) #endif #ifndef AI_PLATFORM_API_MICRO #define AI_PLATFORM_API_MICRO (0) #endif #define AI_PLATFORM_API_VERSION \ AI_VERSION(AI_PLATFORM_API_MAJOR, \ AI_PLATFORM_API_MINOR, \ AI_PLATFORM_API_MICRO) #ifndef AI_TOOLS_API_VERSION_MAJOR #define AI_TOOLS_API_VERSION_MAJOR (1) #endif #ifndef AI_TOOLS_API_VERSION_MINOR #define AI_TOOLS_API_VERSION_MINOR (5) #endif #ifndef AI_TOOLS_API_VERSION_MICRO #define AI_TOOLS_API_VERSION_MICRO (0) #endif /*****************************************************************************/ #define AI_TOOLS_API_VERSION \ AI_VERSION(AI_TOOLS_API_VERSION_MAJOR, \ AI_TOOLS_API_VERSION_MINOR, \ AI_TOOLS_API_VERSION_MICRO) #define AI_TOOLS_API_VERSION_1_3 \ AI_VERSION(1, 3, 0) #define AI_TOOLS_API_VERSION_1_4 \ AI_VERSION(1, 4, 0) #define AI_TOOLS_API_VERSION_1_5 \ AI_VERSION(1, 5, 0) /*****************************************************************************/ #ifdef __cplusplus #define AI_API_DECLARE_BEGIN extern "C" { #define AI_API_DECLARE_END } #else #include <stdbool.h> #define AI_API_DECLARE_BEGIN /* AI_API_DECLARE_BEGIN */ #define AI_API_DECLARE_END /* AI_API_DECLARE_END */ #endif /*****************************************************************************/ #define AI_FLAG_NONE (0x0) /*****************************************************************************/ AI_API_DECLARE_BEGIN /*! * @typedef ai_flags * @ingroup ai_platform * @brief bitmask for flags management */ typedef uint32_t ai_flags; /*****************************************************************************/ #define AI_CONCAT_ARG(a, b) a ## b #define AI_CONCAT(a, b) AI_CONCAT_ARG(a, b) /*! AI_CAST SECTION ***********************************/ #define AI_CAST(type_, expr_) ((type_)(expr_)) /*****************************************************************************/ #define AI_MAGIC_SIGNATURE \ (0xa1facade) #define AI_PACK(...) \ __VA_ARGS__ /*****************************************************************************/ #define AI_SHAPE_BCWH (0x01u) /*! * @typedef ai_shape_dimension * @ingroup ai_platform * @brief shape dimension type to be used in shape related structs @ref ai_buffer_shape */ typedef uint32_t ai_shape_dimension; /*****************************************************************************/ #if defined(_MSC_VER) #define AI_API_ENTRY __declspec(dllexport) #define AI_ALIGNED(x) /* AI_ALIGNED(x) */ #elif defined(__ICCARM__) || defined (__IAR_SYSTEMS_ICC__) #define AI_API_ENTRY /* AI_API_ENTRY */ #define AI_ALIGNED(x) AI_CONCAT(AI_ALIGNED_,x) #define AI_ALIGNED_1 _Pragma("data_alignment = 1") #define AI_ALIGNED_2 _Pragma("data_alignment = 2") #define AI_ALIGNED_4 _Pragma("data_alignment = 4") #define AI_ALIGNED_8 _Pragma("data_alignment = 8") #define AI_ALIGNED_16 _Pragma("data_alignment = 16") #define AI_ALIGNED_32 _Pragma("data_alignment = 32") #elif defined(__CC_ARM) #define AI_API_ENTRY __attribute__((visibility("default"))) #define AI_ALIGNED(x) __attribute__((aligned (x))) /* Keil disallows anonymous union initialization by default */ #pragma anon_unions #elif defined(__GNUC__) //#define AI_API_ENTRY __attribute__((visibility("default"))) #define AI_API_ENTRY /* AI_API_ENTRY */ #define AI_ALIGNED(x) __attribute__((aligned(x))) #else /* Dynamic libraries are not supported by the compiler */ #define AI_API_ENTRY /* AI_API_ENTRY */ #define AI_ALIGNED(x) /* AI_ALIGNED(x) */ #endif #define AI_HANDLE_PTR(ptr_) ((ai_handle)(ptr_)) #define AI_HANDLE_NULL AI_HANDLE_PTR(NULL) #define AI_HANDLE_FUNC_PTR(func) ((ai_handle_func)(func)) #define AI_UNUSED(x) (void)(x); #define AI_DEPRECATED /* AI_DEPRECATED */ #define AI_LEGACY /* AI_LEGACY */ #define AI_MAGIC_MARKER (0xA1FACADE) #if defined(__cplusplus) #define AI_STRUCT_INIT {} #define AI_C_ARRAY_INIT {} #else #define AI_STRUCT_INIT {0} #define AI_C_ARRAY_INIT {0} #endif #define AI_ERROR_FMT AIU32_FMT #define AI_IS_UNSIGNED(type) \ ((((type)0) - 1) > 0) #define AI_CUSTOM_SIZE(type) \ (ai_custom_type_signature)((AI_IS_UNSIGNED(type)) \ ? (0x80|(sizeof(type)&0x7f)) : (sizeof(type)&0x7f)) /*! network buffers struct handlers *******************************************/ #ifdef __cplusplus #define AI_NETWORK_PARAMS_INIT(params_, activations_) \ { \ {{ params_, activations_ }} \ } #define AI_NETWORK_BUFFERS_INIT(weights_buffers_, activations_buffers_) \ { \ AI_MAGIC_SIGNATURE, AI_PACK(weights_buffers_), AI_PACK(activations_buffers_) \ } #else #define AI_NETWORK_PARAMS_INIT(params_, activations_) \ { \ .params = params_, \ .activations = activations_ \ } #define AI_NETWORK_BUFFERS_INIT(weights_buffers_, activations_buffers_) \ { \ .map_signature = AI_MAGIC_SIGNATURE, \ .map_weights = AI_PACK(weights_buffers_), \ .map_activations = AI_PACK(activations_buffers_) \ } #endif // __cplusplus /*! binary padded bits macro helpers *****************************************/ #define AI_PBITS_MASK \ (0x1F) #define AI_PBITS_SHIFTS \ (5) #define AI_PBITS_PADDED_BYTES_COUNT(bits_) \ (((ai_u32)(bits_) + 7) >> 3) #define AI_PBITS_PADDED_WORDS_COUNT(bits_) \ (((ai_size)(bits_) + AI_PBITS_MASK) >> AI_PBITS_SHIFTS) #define AI_PBITS_GET_WORD(word_ptr_, bits_) \ (((ai_pbits*)(word_ptr_)) + ((bits_) >> AI_PBITS_SHIFTS)) #define AI_PAD_CHANNELS(format_, channels_) \ ((AI_BUFFER_FMT_GET_BITS(format_)==1) ? (AI_PBITS_PADDED_WORDS_COUNT(channels_) << AI_PBITS_SHIFTS) : (channels_)) /*! ai_intq_info struct handlers *********************************************/ #define INTQ_CONST const // #define INTQ_CONST #define AI_INTQ_INFO_LIST(list_) \ ((list_)->info) #define AI_INTQ_INFO_LIST_FLAGS(list_) \ ((list_) ? (list_)->flags : 0) #define AI_INTQ_INFO_LIST_SIZE(list_) \ ((list_) ? (list_)->size : 0) #define AI_HAS_INTQ_INFO_LIST(list_) \ ((list_) ? (((list_)->info) && ((list_)->size>0)) : false) #define AI_INTQ_INFO_LIST_SCALE(list_, type_, pos_) \ (((list_) && (list_)->info && ((pos_)<(list_)->size)) \ ? ((type_*)((list_)->info->scale))[(pos_)] : 0) #define AI_INTQ_INFO_LIST_ZEROPOINT(list_, type_, pos_) \ (((list_) && (list_)->info && ((pos_)<(list_)->size)) \ ? ((type_*)((list_)->info->zeropoint))[(pos_)] : 0) /*! ai_buffer format handlers ************************************************/ /*! * @enum buffer format definition * @ingroup ai_platform * * 32 bit signed format list. */ typedef int32_t ai_buffer_format; /*! ai_buffer_meta flags & macros ********************************************/ #define AI_BUFFER_META_HAS_INTQ_INFO (0x1U << 0) #define AI_BUFFER_META_FLAG_SCALE_FLOAT (0x1U << 0) #define AI_BUFFER_META_FLAG_ZEROPOINT_U8 (0x1U << 1) #define AI_BUFFER_META_FLAG_ZEROPOINT_S8 (0x1U << 2) #define AI_BUFFER_META_FLAG_ZEROPOINT_U16 (0x1U << 3) #define AI_BUFFER_META_FLAG_ZEROPOINT_S16 (0x1U << 4) /*! ai_buffer format variable flags & macros *********************************/ #define AI_BUFFER_FMT_TYPE_NONE (0x0) #define AI_BUFFER_FMT_TYPE_FLOAT (0x1) #define AI_BUFFER_FMT_TYPE_Q (0x2) #define AI_BUFFER_FMT_TYPE_BOOL (0x3) #define AI_BUFFER_FMT_FLAG_CONST (0x1U<<30) #define AI_BUFFER_FMT_FLAG_STATIC (0x1U<<29) #define AI_BUFFER_FMT_FLAG_IS_IO (0x1U<<27) #define AI_BUFFER_FMT_FLAG_PERSISTENT (0x1U<<29) #define AI_BUFFER_FMT_PACK(value_, mask_, bits_) \ ( ((value_) & (mask_)) << (bits_) ) #define AI_BUFFER_FMT_UNPACK(fmt_, mask_, bits_) \ ( (AI_BUFFER_FMT_OBJ(fmt_) >> (bits_)) & (mask_) ) #define AI_BUFFER_FMT_OBJ(fmt_) \ ((ai_buffer_format)(fmt_)) #define AI_BUFFER_FMT_GET_FLOAT(fmt_) \ AI_BUFFER_FMT_UNPACK(fmt_, 0x1, 24) #define AI_BUFFER_FMT_GET_SIGN(fmt_) \ AI_BUFFER_FMT_UNPACK(fmt_, 0x1, 23) #define AI_BUFFER_FMT_GET_TYPE(fmt_) \ AI_BUFFER_FMT_UNPACK(fmt_, 0xF, 17) #define AI_BUFFER_FMT_GET_BITS(fmt_) \ AI_BUFFER_FMT_UNPACK(fmt_, 0x7F, 7) #define AI_BUFFER_FMT_SET_BITS(bits_) \ AI_BUFFER_FMT_PACK((bits_), 0x7F, 7) #define AI_BUFFER_FMT_GET_FBITS(fmt_) \ ( (ai_i8)AI_BUFFER_FMT_UNPACK(fmt_, 0x7F, 0) - 64 ) #define AI_BUFFER_FMT_SET_FBITS(fbits_) \ AI_BUFFER_FMT_PACK((fbits_)+64, 0x7F, 0) #define AI_BUFFER_FMT_SET(type_id_, sign_bit_, float_bit_, bits_, fbits_) \ AI_BUFFER_FMT_OBJ( \ AI_BUFFER_FMT_PACK(float_bit_, 0x1, 24) | \ AI_BUFFER_FMT_PACK(sign_bit_, 0x1, 23) | \ AI_BUFFER_FMT_PACK(0, 0x3, 21) | \ AI_BUFFER_FMT_PACK(type_id_, 0xF, 17) | \ AI_BUFFER_FMT_PACK(0, 0x7, 14) | \ AI_BUFFER_FMT_SET_BITS(bits_) | \ AI_BUFFER_FMT_SET_FBITS(fbits_) \ ) #define AI_BUFFER_FMT_SAME(fmt1_, fmt2_) \ ( AI_BUFFER_FMT_GET(fmt1_) == AI_BUFFER_FMT_GET(fmt2_) ) #define AI_BUFFER_FMT_GET(fmt_) \ (AI_BUFFER_FMT_OBJ(fmt_) & 0x01FFFFFF) #define AI_BUFFER_FORMAT(buf_) \ AI_BUFFER_FMT_GET((buf_)->format) /*! * @define shape type index * @ingroup ai_platform * @brief positional ID for generic shapes C structs */ #define AI_SHAPE_EXTENSION (0x5) #define AI_SHAPE_DEPTH (0x4) #define AI_SHAPE_HEIGHT (0x3) #define AI_SHAPE_WIDTH (0x2) #define AI_SHAPE_CHANNEL (0x1) #define AI_SHAPE_IN_CHANNEL (0x0) #define AI_SHAPE_BATCH (0x0) #define AI_SHAPE_TIME (0x0) AI_DEPRECATED #define AI_BUFFER_WIDTH(buf_) \ ((buf_)->shape.data[AI_SHAPE_WIDTH]) AI_DEPRECATED #define AI_BUFFER_HEIGHT(buf_) \ ((buf_)->shape.data[AI_SHAPE_HEIGHT]) AI_DEPRECATED #define AI_BUFFER_CHANNELS(buf_) \ ((buf_)->shape.data[AI_SHAPE_CHANNEL]) AI_DEPRECATED #define AI_BUFFER_N_BATCHES(buf_) \ ((buf_)->shape.data[AI_SHAPE_BATCH]) #define AI_BUFFER_DATA(buf_, type_) \ ((type_*)((buf_)->data)) #define AI_BUFFER_META_INFO(buf_) \ ((buf_)->meta_info) #define AI_BUFFER_META_INFO_INTQ(meta_) \ ((meta_) && ((meta_)->flags & AI_BUFFER_META_HAS_INTQ_INFO)) \ ? ((meta_)->intq_info) : NULL #define AI_BUFFER_META_INFO_INTQ_GET_SIZE(meta_) \ ( (AI_BUFFER_META_INFO_INTQ(meta_)) \ ? AI_INTQ_INFO_LIST_SIZE(AI_BUFFER_META_INFO_INTQ(meta_)) \ : 0 ) #define AI_BUFFER_META_INFO_INTQ_GET_SCALE(meta_, pos_) \ ( (AI_BUFFER_META_INFO_INTQ(meta_)) \ ? AI_INTQ_INFO_LIST_SCALE(AI_BUFFER_META_INFO_INTQ(meta_), ai_float, pos_) \ : 0 ) #define AI_BUFFER_META_INFO_INTQ_GET_ZEROPOINT(meta_, pos_) \ ( (AI_BUFFER_META_INFO_INTQ(meta_)) \ ? ((AI_INTQ_INFO_LIST_FLAGS(AI_BUFFER_META_INFO_INTQ(meta_))&AI_BUFFER_META_FLAG_ZEROPOINT_U8) \ ? AI_INTQ_INFO_LIST_ZEROPOINT(AI_BUFFER_META_INFO_INTQ(meta_), ai_u8, pos_) \ : AI_INTQ_INFO_LIST_ZEROPOINT(AI_BUFFER_META_INFO_INTQ(meta_), ai_i8, pos_) ) \ : 0 ) #define AI_BUFFER_META_INFO_INIT(flags_, intq_info_) { \ .flags = (flags_), \ .intq_info = AI_PACK(intq_info_) \ } #define AI_BUFFER_SIZE(buf_) \ ai_buffer_get_size(buf_, true) #define AI_BUFFER_SIZE_UNPAD(buf_) \ ai_buffer_get_size(buf_, false) #define AI_BUFFER_BYTE_SIZE(count_, fmt_) \ ai_buffer_get_byte_size(count_, fmt_) #define AI_BUFFER_FLAGS(buf_) \ ((buf_) ? (buf_)->flags : 0x0) #define AI_BUFFER_SHAPE_INIT(type_, size_, ...) \ { \ .type = (type_), \ .size = (size_), \ .data = (ai_shape_dimension[]){ __VA_ARGS__ } \ } #define AI_BUFFER_SHAPE_INIT_FROM_ARRAY(type_, size_, array_ptr_) \ { \ .type = (type_), \ .size = (size_), \ .data = (ai_shape_dimension*)(array_ptr_) \ } #define AI_BUFFER_SHAPE_SIZE(buf_) \ ((buf_) ? (buf_)->shape.size : 0) #define AI_BUFFER_SHAPE_TYPE(buf_) \ ((buf_) ? (buf_)->shape.type : 0) #if defined(HAS_AI_ASSERT) && defined(AI_ASSERT) #define AI_BUFFER_SET_SHAPE_ELEM(buf_, pos_, value_) { \ AI_ASSERT(buf_) \ (buf_)->shape.data[pos_] = (value_); \ } #define AI_BUFFER_SHAPE_ELEM(buf_, pos_) \ (((pos_)<AI_BUFFER_SHAPE_SIZE(buf_)) ? (buf_)->shape.data[pos_] : 0) #else #define AI_BUFFER_SET_SHAPE_ELEM(buf_, pos_, value_) { \ (buf_)->shape.data[pos_] = (value_); \ } #define AI_BUFFER_SHAPE_ELEM(buf_, pos_) \ (buf_)->shape.data[pos_] #endif AI_DEPRECATED #define AI_BUFFER_OBJ_INIT(format_, h_, w_, ch_, n_batches_, data_) \ { .format = (ai_buffer_format)(format_), \ .data = (ai_handle)(data_), \ .meta_info = NULL, \ .flags = AI_FLAG_NONE, \ .size = (h_) * (w_) * AI_PAD_CHANNELS(format_, ch_), \ .shape = AI_BUFFER_SHAPE_INIT(AI_SHAPE_BCWH, 4, (n_batches_), (ch_), (w_), (h_)), \ } AI_DEPRECATED #define AI_BUFFER_OBJ_INIT_STATIC(type_, format_, h_, w_, ch_, n_batches_, ...) \ { .format = (ai_buffer_format)(format_), \ .data = (ai_handle)((type_[]){__VA_ARGS__}), \ .meta_info = NULL, \ .flags = AI_FLAG_NONE, \ .size = (h_) * (w_) * AI_PAD_CHANNELS(format_, ch_), \ .shape = AI_BUFFER_SHAPE_INIT(AI_SHAPE_BCWH, 4, (n_batches_), (ch_), (w_), (h_)) \ } /* 7.1 new macro API */ #define AI_BUFFER_INIT(flags_, format_, shape_, size_, meta_info_, data_) \ { .format = (ai_buffer_format)(format_), \ .data = (ai_handle)(data_), \ .meta_info = (meta_info_), \ .flags = (flags_), \ .size = (size_), \ .shape = AI_PACK(shape_) \ } /* 7.1 new macro API */ #define AI_BUFFER_INIT_STATIC(type_, flags_, format_, shape_, size_, meta_info_, ...) \ { .format = (ai_buffer_format)(format_), \ .data = (ai_handle)((type_[]){__VA_ARGS__}), \ .meta_info = (meta_info_), \ .flags = (flags_), \ .size = (size_), \ .shape = AI_PACK(shape_) \ } /*****************************************************************************/ #define AI_NETWORK_BUFFERS_FIELD_DECLARE \ ai_signature map_signature; /*! structure signature (required!) */ \ ai_buffer_array map_weights; /*! info about weights array buffers (required!) */ \ ai_buffer_array map_activations; /*! info about activations array buffers (required!) */ #define AI_NETWORK_PARAMS_FIELDS_DECLARE \ union { \ struct { \ ai_buffer params; /*! info about params buffer(required!) */ \ ai_buffer activations; /*! info about activations buffer (required!) */ \ }; \ struct { \ AI_NETWORK_BUFFERS_FIELD_DECLARE \ }; \ }; /*****************************************************************************/ #define AI_BUFFER_ARRAY_OBJ_INIT(flags_, size_, buffer_array_) \ { \ .flags = (ai_u16)(flags_), \ .size = (ai_u16)(size_), \ .buffer = (ai_buffer*)(buffer_array_) \ } #define AI_BUFFER_ARRAY_OBJ_INIT_STATIC(flags_, size_, ...) \ { \ .flags = (ai_u16)(flags_), \ .size = (ai_u16)(size_), \ .buffer = (ai_buffer*)((ai_buffer[]){__VA_ARGS__}) \ } #define AI_BUFFER_ARRAY_SANE(buf_array_) \ ai_buffer_array_sane(buf_array_) #define AI_BUFFER_ARRAY_FLAGS(buf_array_) \ ((AI_BUFFER_ARRAY_SANE(buf_array_)) ? (buf_array_)->flags : AI_FLAG_NONE) #define AI_BUFFER_ARRAY_SIZE(buf_array_) \ ((AI_BUFFER_ARRAY_SANE(buf_array_)) ? (buf_array_)->size : 0) #define AI_BUFFER_ARRAY_ITEM(buf_array_, pos_) \ ((AI_BUFFER_ARRAY_SANE(buf_array_)) ? ((buf_array_)->buffer + (pos_)) : NULL) #define AI_BUFFER_ARRAY_ITEM_SET_ADDRESS(buf_array_, pos_, address_) \ ai_buffer_array_item_set_address(buf_array_, pos_, address_) /*! * @enum buffer formats enum list * @ingroup ai_platform * * List of supported ai_buffer format types. */ enum { AI_BUFFER_FORMAT_NONE = AI_BUFFER_FMT_SET(AI_BUFFER_FMT_TYPE_NONE, 0, 0, 0, 0), AI_BUFFER_FORMAT_FLOAT = AI_BUFFER_FMT_SET(AI_BUFFER_FMT_TYPE_FLOAT, 1, 1, 32, 0), AI_BUFFER_FORMAT_U1 = AI_BUFFER_FMT_SET(AI_BUFFER_FMT_TYPE_Q, 0, 0, 1, 0), AI_BUFFER_FORMAT_U8 = AI_BUFFER_FMT_SET(AI_BUFFER_FMT_TYPE_Q, 0, 0, 8, 0), AI_BUFFER_FORMAT_U16 = AI_BUFFER_FMT_SET(AI_BUFFER_FMT_TYPE_Q, 0, 0, 16, 0), AI_BUFFER_FORMAT_U32 = AI_BUFFER_FMT_SET(AI_BUFFER_FMT_TYPE_Q, 0, 0, 32, 0), AI_BUFFER_FORMAT_S1 = AI_BUFFER_FMT_SET(AI_BUFFER_FMT_TYPE_Q, 1, 0, 1, 0), AI_BUFFER_FORMAT_S8 = AI_BUFFER_FMT_SET(AI_BUFFER_FMT_TYPE_Q, 1, 0, 8, 0), AI_BUFFER_FORMAT_S16 = AI_BUFFER_FMT_SET(AI_BUFFER_FMT_TYPE_Q, 1, 0, 16, 0), AI_BUFFER_FORMAT_S32 = AI_BUFFER_FMT_SET(AI_BUFFER_FMT_TYPE_Q, 1, 0, 32, 0), AI_BUFFER_FORMAT_Q = AI_BUFFER_FMT_SET(AI_BUFFER_FMT_TYPE_Q, 1, 0, 0, 0), AI_BUFFER_FORMAT_Q7 = AI_BUFFER_FMT_SET(AI_BUFFER_FMT_TYPE_Q, 1, 0, 8, 7), AI_BUFFER_FORMAT_Q15 = AI_BUFFER_FMT_SET(AI_BUFFER_FMT_TYPE_Q, 1, 0, 16, 15), AI_BUFFER_FORMAT_UQ = AI_BUFFER_FMT_SET(AI_BUFFER_FMT_TYPE_Q, 0, 0, 0, 0), AI_BUFFER_FORMAT_UQ7 = AI_BUFFER_FMT_SET(AI_BUFFER_FMT_TYPE_Q, 0, 0, 8, 7), AI_BUFFER_FORMAT_UQ15 = AI_BUFFER_FMT_SET(AI_BUFFER_FMT_TYPE_Q, 0, 0, 16, 15), AI_BUFFER_FORMAT_BOOL = AI_BUFFER_FMT_SET(AI_BUFFER_FMT_TYPE_BOOL, 0, 0, 8, 0), }; /*****************************************************************************/ #define AI_ERROR_INIT(type_, code_) { \ .type = AI_ERROR_##type_, \ .code = AI_ERROR_CODE_##code_ \ } /* printf formats */ #define SSIZET_FMT "%" PRIu32 #define AII32_FMT "%" PRId32 #define AIU32_FMT "%" PRIu32 #define AII64_FMT "%" PRId64 #define AIU64_FMT "%" PRIu64 #define AI_VERSION(major_, minor_, micro_) \ (((major_)<<24) | ((minor_)<<16) | ((micro_)<<8)) typedef uint8_t ai_custom_type_signature; typedef void* ai_handle; typedef const void* ai_handle_const; typedef float ai_float; typedef double ai_double; typedef bool ai_bool; typedef char ai_char; typedef uint32_t ai_size; typedef int16_t ai_short_size; typedef uintptr_t ai_uptr; typedef unsigned int ai_uint; typedef uint8_t ai_u8; typedef uint16_t ai_u16; typedef uint32_t ai_u32; typedef uint64_t ai_u64; typedef int ai_int; typedef int8_t ai_i8; typedef int16_t ai_i16; typedef int32_t ai_i32; typedef int64_t ai_i64; typedef uint64_t ai_macc; typedef int32_t ai_pbits; typedef uint32_t ai_signature; typedef void (*ai_handle_func)(ai_handle); /*****************************************************************************/ /*! * @struct ai_error * @ingroup ai_platform * @brief Structure encoding details about the last error. */ typedef struct ai_error_ { ai_u32 type : 8; /*!< Error type represented by @ref ai_error_type */ ai_u32 code : 24; /*!< Error code represented by @ref ai_error_code */ } ai_error; /*****************************************************************************/ /*! * @struct ai_intq_info * @ingroup ai_platform * @brief an element of the ai_intq_info_list entry. It reports an array for the * scale and zeropoint values for each buffer. Optional flags are also present */ typedef struct ai_intq_info_ { INTQ_CONST ai_float* scale; INTQ_CONST ai_handle zeropoint; } ai_intq_info; /*! * @struct ai_intq_info_list * @ingroup ai_platform * @brief list reporting meta info for quantized networks integer support * when size > 1 it means a per channel out quantization */ typedef struct ai_intq_info_list_ { ai_u16 flags; /*!< optional flags to store intq info attributes */ ai_u16 size; /*!< number of elements in the the intq_info list */ INTQ_CONST ai_intq_info* info; /*!< pointer to an array of metainfo * associated to the intq_info list */ } ai_intq_info_list; /*****************************************************************************/ /*! * @struct ai_buffer_meta_info * @ingroup ai_platform * @brief Optional meta attributes associated with the I/O buffer. * This datastruct is used also for network querying, where the data field may * may be NULL. */ typedef struct ai_buffer_meta_info_ { ai_u32 flags; /*!< meta info flags */ ai_intq_info_list* intq_info; /*!< meta info related to integer format */ } ai_buffer_meta_info; /*! * @struct ai_buffer_shape * @ingroup ai_platform * @brief Memory buffer shape datatype definition. */ typedef struct ai_buffer_shape_ { ai_u32 type : 8; /*!< shape type: reserved for compatibility */ ai_u32 size : 24; /*!< size: shape cardinality */ ai_shape_dimension* data; /*!< pointer to shape tuple array */ } ai_buffer_shape; /*! * @struct ai_buffer * @ingroup ai_platform * @brief Memory buffer storing data (optional) with a shape, size and type. * This datastruct is used also for network querying, where the data field may * may be NULL. */ typedef struct ai_buffer_ { ai_buffer_format format; /*!< buffer format */ ai_handle data; /*!< pointer to buffer data */ ai_buffer_meta_info* meta_info; /*!< pointer to buffer metadata info */ /* New 7.1 fields */ ai_flags flags; /*!< shape optional flags */ ai_size size; /*!< number of elements of the buffer (including optional padding) */ ai_buffer_shape shape; /*!< n-dimensional shape info */ } ai_buffer; /*! * @struct ai_buffer_array * @ingroup ai_platform * @brief Array of @ref ai_buffer. */ typedef struct ai_buffer_array_ { ai_u16 flags; /*!< buffer array flags */ ai_u16 size; /*!< buffer array size */ ai_buffer* buffer; /*!< buffer array buffers pointer */ } ai_buffer_array; /* enums section */ /*! * @enum ai_error_type * @ingroup ai_platform * * Generic enum to list network error types. */ typedef enum { AI_ERROR_NONE = 0x00, /*!< No error */ AI_ERROR_TOOL_PLATFORM_API_MISMATCH = 0x01, AI_ERROR_TYPES_MISMATCH = 0x02, AI_ERROR_INVALID_HANDLE = 0x10, AI_ERROR_INVALID_STATE = 0x11, AI_ERROR_INVALID_INPUT = 0x12, AI_ERROR_INVALID_OUTPUT = 0x13, AI_ERROR_INVALID_PARAM = 0x14, AI_ERROR_INVALID_SIGNATURE = 0x15, AI_ERROR_INVALID_SIZE = 0x16, AI_ERROR_INVALID_VALUE = 0x17, AI_ERROR_INIT_FAILED = 0x30, AI_ERROR_ALLOCATION_FAILED = 0x31, AI_ERROR_DEALLOCATION_FAILED = 0x32, AI_ERROR_CREATE_FAILED = 0x33, } ai_error_type; /*! * @enum ai_error_code * @ingroup ai_platform * * Generic enum to list network error codes. */ typedef enum { AI_ERROR_CODE_NONE = 0x0000, /*!< No error */ AI_ERROR_CODE_NETWORK = 0x0010, AI_ERROR_CODE_NETWORK_PARAMS = 0x0011, AI_ERROR_CODE_NETWORK_WEIGHTS = 0x0012, AI_ERROR_CODE_NETWORK_ACTIVATIONS = 0x0013, AI_ERROR_CODE_LAYER = 0x0014, AI_ERROR_CODE_TENSOR = 0x0015, AI_ERROR_CODE_ARRAY = 0x0016, AI_ERROR_CODE_INVALID_PTR = 0x0017, AI_ERROR_CODE_INVALID_SIZE = 0x0018, AI_ERROR_CODE_INVALID_FORMAT = 0x0019, AI_ERROR_CODE_OUT_OF_RANGE = 0x0020, AI_ERROR_CODE_INVALID_BATCH = 0x0021, AI_ERROR_CODE_MISSED_INIT = 0x0030, AI_ERROR_CODE_IN_USE = 0x0040, AI_ERROR_CODE_LOCK = 0x0041, } ai_error_code; /*! * @struct ai_platform_version * @ingroup ai_platform * @brief Datastruct storing platform version info */ typedef struct ai_platform_version_ { ai_u8 major; ai_u8 minor; ai_u8 micro; ai_u8 reserved; } ai_platform_version; /*! * @struct ai_network_params * @ingroup ai_platform * * Datastructure to pass parameters during network initialization. */ typedef struct ai_network_params_ { AI_NETWORK_PARAMS_FIELDS_DECLARE } ai_network_params; /*! * @struct ai_network_buffers * @ingroup ai_platform * * Datastructure to pass network buffers during network initialization. */ typedef struct ai_network_buffers_ { AI_NETWORK_BUFFERS_FIELD_DECLARE } ai_network_buffers; /*! * @struct ai_network_report * @ingroup ai_platform * * Datastructure to query a network report with some relevant network detail. */ typedef struct ai_network_report_ { const char* model_name; const char* model_signature; const char* model_datetime; const char* compile_datetime; const char* runtime_revision; ai_platform_version runtime_version; const char* tool_revision; ai_platform_version tool_version; ai_platform_version tool_api_version; ai_platform_version api_version; ai_platform_version interface_api_version; ai_macc n_macc; ai_u16 n_inputs; ai_u16 n_outputs; ai_buffer* inputs; ai_buffer* outputs; AI_NETWORK_PARAMS_FIELDS_DECLARE ai_u32 n_nodes; ai_signature signature; } ai_network_report; /*! * @enum ai_upsample_mode * @ingroup ai_platform * @brief allowed mode in upsample layer */ typedef enum { AI_UPSAMPLE_ZEROS = 0x0, AI_UPSAMPLE_NEAREST, AI_UPSAMPLE_BILINEAR, AI_UPSAMPLE_TRILINEAR } ai_upsample_mode; /*! * @enum ai_resize_mode * @ingroup ai_platform * @brief allowed mode in resize layer */ typedef enum { AI_RESIZE_ZEROS = 0x0, AI_RESIZE_NEAREST, AI_RESIZE_LINEAR, AI_RESIZE_CUBIC } ai_resize_mode; /*! * @enum ai_coord_transf_mode * @ingroup ai_platform * @brief coordinate_transformation_mode in resize layer */ typedef enum { AI_HALF_PIXEL = 0x0, AI_PYTORCH_HALF_PIXEL, AI_ALIGN_CORNERS, AI_ASYMMETRIC, AI_TF_HALF_PIXEL_FOR_NN, AI_TF_CROP_AND_RESIZE } ai_coord_transf_mode; typedef enum { AI_ROUND_PREFER_FLOOR = 0x0, AI_ROUND_PREFER_CEIL, AI_ROUND_FLOOR, AI_ROUND_CEIL } ai_nearest_mode; typedef enum { AI_PAD_CONSTANT = 0x0, AI_PAD_REFLECT, AI_PAD_EDGE, AI_PAD_8BIT_CH1ST_CONSTANT, } ai_pad_mode; #define OUTPUT_PADDING_FLAG (1 << 0) #define CHANNEL_FIRST_FLAG (1 << 1) /* Carefull when changing those definitions bit0 shall always select output padding (Valid vs Same) bit1 shall always select Channel first /channel lst format */ typedef enum { AI_LAYER_FORMAT_CHANNEL_LAST_VALID = 0x0, AI_LAYER_FORMAT_CHANNEL_LAST_SAME = 0x1, AI_LAYER_FORMAT_CHANNEL_FIRST_VALID = 0x2, AI_LAYER_FORMAT_CHANNEL_FIRST_SAME = 0x3, } ai_layer_format_type; /*! ai_platform public APIs **************************************************/ /*! * @brief get the total number of elements of an ai_buffer. * @ingroup ai_platform * @param buffer a pointer to an @ref ai_buffer * @param with_padding when true it considers also padded elements * @return the number of elements of the buffer (with/without padded ones) */ AI_API_ENTRY ai_size ai_buffer_get_size(const ai_buffer* buffer, const ai_bool with_padding); /*! * @brief get the size in bytes of an ai_buffer (given the number of elements and format). * @ingroup ai_platform * @param count the number of elements composing the buffer * @param fmt the format of the ai_buffer * @return the size in bytes of the buffer */ AI_API_ENTRY ai_size ai_buffer_get_byte_size(const ai_size count, const ai_buffer_format fmt); /*! * @brief get total size in bytes of a buffer array. * @ingroup ai_platform * @param barray a pointer to the buffer array * @return the total size in bytes of all the buffer arrays */ AI_API_ENTRY ai_bool ai_buffer_array_is_empty(const ai_buffer_array* barray); /*! * @brief get total size in bytes of a buffer array. * @ingroup ai_platform * @param barray a pointer to the buffer array * @return the total size in bytes of all the buffer arrays */ AI_API_ENTRY ai_bool ai_buffer_array_is_valid(const ai_buffer_array* barray); /*! * @brief check if a buffer array is valid - i.e. not empty. * @ingroup ai_platform * @param barray a pointer to the buffer array * @return true if the array is consistent and not empty, false otherwise */ AI_API_ENTRY ai_bool ai_buffer_array_sane(const ai_buffer_array* barray); /*! * @brief get total size in bytes of a buffer array. * @ingroup ai_platform * @param barray a pointer to the buffer array * @return the total size in bytes of all the buffer arrays */ AI_API_ENTRY ai_size ai_buffer_array_get_byte_size(const ai_buffer_array* barray); /*! * @brief set the address of buffer array item @pos * @ingroup ai_platform * @param barray a pointer to the buffer array * @param pos the index of the element in the array * @param address the address to set * @return true if successful, false otherwise */ AI_API_ENTRY ai_bool ai_buffer_array_item_set_address( ai_buffer_array* barray, const ai_u32 pos, ai_handle address); AI_API_DECLARE_END #endif /*AI_PLATFORM_H*/
30,193
C
30.159959
116
0.58189
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/Middlewares/ST/AI/Inc/layers_ml_iforest.h
/** ****************************************************************************** * @file layers_iforest.h * @author AIS * @brief header file of AI platform iForest layers datatypes ****************************************************************************** * @attention * * Copyright (c) 2021 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** @verbatim @endverbatim ****************************************************************************** */ #ifndef LAYERS_IFOREST_H #define LAYERS_IFOREST_H #pragma once #include "layers_common.h" /*! * @defgroup layers_ml Layers Definitions * @brief definition * */ AI_API_DECLARE_BEGIN /* Allowed tests branch in the iTrees */ typedef enum { AI_IFOREST_BRANCH_LT_IDX = 0, AI_IFOREST_BRANCH_LEQ_IDX, AI_IFOREST_BRANCH_EQ_IDX, AI_IFOREST_BRANCH_END, } ai_iforest_branch_e; /*! * @struct ai_layer_iforest * @ingroup layers_iforest * @brief iForest layer * * The type of iforest function is handled by the specific forward function * @ref forward_iforest */ typedef AI_ALIGNED_TYPE(struct, 4) ai_layer_iforest_ { AI_LAYER_COMMON_FIELDS_DECLARE ai_float global_average_path_length; /*!< global average path length used to normalized average path length*/ ai_float score_threshold; /*!< score threshold used to center the score around 0 */ } ai_layer_iforest; /******************************************************************************/ /* Forward Functions Section */ /******************************************************************************/ /*! * @brief Decodes the iforest ML algorithm. * @ingroup layers_iforest * @param layer iforest layer */ AI_INTERNAL_API void forward_iforest(ai_layer *pLayer); AI_API_DECLARE_END #endif /*LAYERS_IFOREST_H*/
2,134
C
25.6875
112
0.524367
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/Middlewares/ST/AI/Inc/layers_ml_svc.h
/** ****************************************************************************** * @file layers_svc.h * @author AST Embedded Analytics Research Platform * @brief header file of AI platform SVM Classifier (SVC) datatypes ****************************************************************************** * @attention * * Copyright (c) 2021 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** @verbatim @endverbatim ****************************************************************************** */ #ifndef LAYERS_SVC_H #define LAYERS_SVC_H #pragma once #include "layers_common.h" /*! * @defgroup layers_svc Layers Definitions * @brief definition * */ AI_API_DECLARE_BEGIN /* SVM classifier (SVC) kernel types */ typedef enum ai_svc_kernel_e_ { AI_SVC_KERNEL_LINEAR = 0, AI_SVC_KERNEL_POLYNOMIAL, AI_SVC_KERNEL_RBF, AI_SVC_KERNEL_SIGMOID, AI_SVC_KERNEL_UNSUPPORTED } ai_svc_kernel_e; /*! * @struct ai_layer_svc * @ingroup layers_svc * @brief SVM Classifier (SVC) layer * * The type of svc function is handled by the specific forward function * @ref forward_svc */ typedef AI_ALIGNED_TYPE(struct, 4) ai_layer_svc_ { AI_LAYER_COMMON_FIELDS_DECLARE ai_float gamma; /*!< kernel coefficient for rbf, polynomial and sigmoid functions */ ai_float coef0; /*!< term in polynomial and sigmoid functions */ ai_u32 degree; /*!< polynomial function degree */ ai_svc_kernel_e kernel_type; /*!< kernel type : see ai_svm_kernel_e */ ai_bool proba_support; /*!< whether or not use the parameters learned in Platt scaling */ ai_bool has_classlabels_int; /*!< if True, SVC returns classlabels int, else classlabels string */ } ai_layer_svc; /******************************************************************************/ /* Forward Functions Section */ /******************************************************************************/ /*! * @brief Decodes the SVM Classifier ML operator. * @ingroup layers_svc * @param layer svm classifier layer */ AI_INTERNAL_API void forward_svc(ai_layer *pLayer); AI_API_DECLARE_END #endif /*LAYERS_SVC_H*/
2,548
C
30.085365
110
0.523155
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/Middlewares/ST/AI/Inc/core_log.h
/** ****************************************************************************** * @file core_log.h * @author AST Embedded Analytics Research Platform * @brief header file of core log interfaces ****************************************************************************** * @attention * * Copyright (c) 2018 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** @verbatim @endverbatim ****************************************************************************** */ #ifndef CORE_LOG_H #define CORE_LOG_H #pragma once #include "ai_platform.h" #include "ai_datatypes_defines.h" /*! * @defgroup core_log Logger core routines wrapper interface * @brief Common macros, datatypes and routines of ai logger module * @details This header defines the wrapping macros interfaces to handle the * global logger module. These macro are defined when the macro HAS_LOG is * defined, otherwise they are all set to NOP routines and no logger code is * compiled at all. When the macro HAS_LOG is defined, only the log messages * having an enum id >= the value of the macro are compiled. Thus to include in * compilation only log messages up to the error level the value of HAS_LOG must * be equal the the enum value of LOG_ERROR macro (i.e. 3). a value of 6 means * to include all log messages up to the lower LOG_TRACE level. */ #if defined HAS_LOG && (HAS_LOG>=0) #include "ai_log.h" #define AI_LOG_SECTION(...) \ { __VA_ARGS__ } #define AI_LOG_ACQUIRE() \ ai_log_acquire() #define AI_LOG_SET_LEVEL(level_) \ AI_WRAP_FUNC(ai_log_set_level(level_);) #define AI_LOG_SET_QUIET(onoff_) \ AI_WRAP_FUNC(ai_log_set_quiet(onoff_);) #define AI_LOG_SET_LOCK_FN(fn_, udata_) \ AI_WRAP_FUNC(ai_log_set_lock(fn_, udata_);) #define AI_LOG_CHANNEL_PUSH(level_, fn_, udata_) \ AI_WRAP_FUNC(ai_log_channel_push(level_, fn_, udata_);) #define AI_LOG_CHANNEL_POP(fn_, udata_) \ AI_WRAP_FUNC(ai_log_channel_pop(fn_, udata_);) #ifdef LOG_USE_FILE #define AI_LOG_SET_FILE_POINTER(fp_) \ AI_WRAP_FUNC(ai_log_set_fp(fp_);) #else #define AI_LOG_SET_FILE_POINTER(fp_) \ AI_WRAP_FUNC(/*AI_LOG_SET_FILE_POINTER()*/) #endif #else #define AI_LOG_SECTION(...) AI_WRAP_FUNC(/*AI_LOG_SECTION()*/) #define AI_LOG_ACQUIRE() (NULL) #define AI_LOG_SET_LEVEL(level_) AI_WRAP_FUNC(/*AI_LOG_SET_LEVEL()*/) #define AI_LOG_SET_QUIET(onoff_) AI_WRAP_FUNC(/*AI_LOG_SET_QUIET()*/) #define AI_LOG_SET_LOCK_FN(fn_, udata_) AI_WRAP_FUNC(/*AI_LOG_SET_LOCK_FN()*/) #define AI_LOG_CHANNEL_PUSH(level_, fn_, udata_) AI_WRAP_FUNC(/*AI_LOG_CHANNEL_PUSH()*/) #define AI_LOG_CHANNEL_POP(fn_, udata_) AI_WRAP_FUNC(/*AI_LOG_CHANNEL_POP()*/) #define AI_LOG_SET_FILE_POINTER(fp_) AI_WRAP_FUNC(/*AI_LOG_SET_FILE_POINTER()*/) #endif #if defined HAS_LOG #define AI_LOG_PRINT(level, fmt, ...) \ AI_WRAP_FUNC(ai_log_print(level, fmt, ##__VA_ARGS__);) #else #define AI_LOG_PRINT(level, fmt, ...) \ AI_WRAP_FUNC(/*AI_LOG_PRINT(...)*/) #endif #if defined HAS_LOG && (HAS_LOG>=LOG_SUDO) #define AI_LOG_SUDO(fmt, ...) \ AI_WRAP_FUNC(ai_log_log(LOG_SUDO, __FILE__, __LINE__, fmt LOG_CR, ##__VA_ARGS__);) #else #define AI_LOG_SUDO(fmt, ...) AI_WRAP_FUNC(/*AI_LOG_SUDO()*/) #endif #if defined HAS_LOG && (HAS_LOG>=LOG_TRACE) #define AI_LOG_TRACE(fmt, ...) \ AI_WRAP_FUNC(ai_log_log(LOG_TRACE, __FILE__, __LINE__, fmt LOG_CR, ##__VA_ARGS__);) #else #define AI_LOG_TRACE(fmt, ...) AI_WRAP_FUNC(/*AI_LOG_TRACE()*/) #endif #if defined HAS_LOG && (HAS_LOG>=LOG_DEBUG) #define AI_LOG_DEBUG(fmt, ...) \ AI_WRAP_FUNC(ai_log_log(LOG_DEBUG, __FILE__, __LINE__, fmt LOG_CR, ##__VA_ARGS__);) #else #define AI_LOG_DEBUG(fmt, ...) AI_WRAP_FUNC(/*AI_LOG_DEBUG()*/) #endif #if defined HAS_LOG && (HAS_LOG>=LOG_INFO) #define AI_LOG_INFO(fmt, ...) \ AI_WRAP_FUNC(ai_log_log(LOG_INFO, __FILE__, __LINE__, fmt LOG_CR, ##__VA_ARGS__);) #else #define AI_LOG_INFO(fmt, ...) AI_WRAP_FUNC(/*AI_LOG_INFO()*/) #endif #if defined HAS_LOG && (HAS_LOG>=LOG_WARN) #define AI_LOG_WARN(fmt, ...) \ AI_WRAP_FUNC(ai_log_log(LOG_WARN, __FILE__, __LINE__, fmt LOG_CR, ##__VA_ARGS__);) #else #define AI_LOG_WARN(fmt, ...) AI_WRAP_FUNC(/*AI_LOG_WARN()*/) #endif #if defined HAS_LOG && (HAS_LOG>=LOG_ERROR) #define AI_LOG_ERROR(fmt, ...) \ AI_WRAP_FUNC(ai_log_log(LOG_ERROR, __FILE__, __LINE__, fmt LOG_CR, ##__VA_ARGS__);) #else #define AI_LOG_ERROR(fmt, ...) AI_WRAP_FUNC(/*AI_LOG_ERROR()*/) #endif #if defined HAS_LOG && (HAS_LOG>=LOG_FATAL) #define AI_LOG_FATAL(fmt, ...) \ AI_WRAP_FUNC(ai_log_log(LOG_FATAL, __FILE__, __LINE__, fmt LOG_CR, ##__VA_ARGS__);) #else #define AI_LOG_FATAL(fmt, ...) AI_WRAP_FUNC(/*AI_LOG_FATAL()*/) #endif #endif /*CORE_LOG_H*/
5,222
C
37.404411
97
0.572769
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/Middlewares/ST/AI/Inc/lite_dense_if32.h
#ifndef _LITE_DENSE_IF32_H #define _LITE_DENSE_IF32_H #pragma once #include "ai_lite_interface.h" /*! * @brief Forward function for a dense layer with signed float input, * signed float output, and float weights. * @ingroup lite_dense_if32 * @param output The pointer to output buffer. * @param input The pointer to input buffer. * @param weights The pointer to weights. * @param bias The pointer to bias (NULL if not available). * @param n_channel_in The number of channels of the input. * @param n_channel_out The number of channels of the output, i.e., * the number of dense hidden neurons. */ LITE_API_ENTRY void forward_lite_dense_if32of32wf32( ai_float* output, const ai_float* input, const ai_float* weights, const ai_float* bias, const ai_u32 n_channel_in, const ai_u32 n_channel_out); #endif /*_LITE_DENSE_IF32_H*/
855
C
30.703703
69
0.71462
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/Middlewares/ST/AI/Inc/layers_dense.h
/** ****************************************************************************** * @file layers_dense.h * @author AST Embedded Analytics Research Platform * @brief header file of AI platform dense layers datatypes ****************************************************************************** * @attention * * Copyright (c) 2018 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** @verbatim @endverbatim ****************************************************************************** */ #ifndef LAYERS_DENSE_H #define LAYERS_DENSE_H #pragma once #include "layers_common.h" /*! * @defgroup layers Normalization Layers Definitions * @brief definition * */ AI_API_DECLARE_BEGIN /*! * @brief Computes the activations of a fixed point dense (fully connected) layer. * @ingroup layers_dense * @param layer the dense layer */ AI_INTERNAL_API void forward_dense_fixed(ai_layer *pLayer); AI_API_DECLARE_END #endif /*LAYERS_DENSE_H*/
1,264
C
24.3
82
0.524525
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/Middlewares/ST/AI/Inc/layers_generic_dqnn.h
/** ****************************************************************************** * @file layers_generic_dqnn.h * @author AIS * @brief header file of AI platform DQNN generic datatypes ****************************************************************************** * @attention * * Copyright (c) 2021 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** @verbatim @endverbatim ****************************************************************************** */ #ifndef LAYERS_GENERIC_DQNN_H #define LAYERS_GENERIC_DQNN_H #pragma once #include "layers_common.h" #include "layers_generic.h" /*! * @defgroup layers_generic_dqnn Layers Definitions * @brief definition * */ AI_API_DECLARE_BEGIN /******************************************************************************/ /* Forward Functions Section */ /******************************************************************************/ /*! * @brief Handles concat with binary input, binary output and * binary weights * @ingroup layers_generic_dqnn * @param layer concat layer */ AI_INTERNAL_API void forward_concat_is1os1(ai_layer *pLayer); AI_API_DECLARE_END #endif /*LAYERS_GENERIC_DQNN_H*/
1,537
C
26.464285
80
0.454782
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/Middlewares/ST/AI/Inc/lite_generic_float.h
/** ****************************************************************************** * @file lite_conv2d_dqnn.h * @author AIS * @brief header file of AI platform lite conv kernel datatypes ****************************************************************************** * @attention * * Copyright (c) 2021 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** @verbatim @endverbatim ****************************************************************************** */ #ifndef LITE_GENERIC_FLOAT_H #define LITE_GENERIC_FLOAT_H #pragma once #include "ai_lite_interface.h" /******************************************************************************/ /* Forward Functions Section */ /******************************************************************************/ /*! * @brief Handles 2D convolution with binary input, binary output and * binary weights - with 0 padding (QKeras like) - Lite I/F * @ingroup lite_conv2d_dqnn */ LITE_API_ENTRY void forward_lite_topK_axis_0_if32of32(const ai_float *pDataIn_init, ai_float *pDataOut_values_init, ai_i32 *pDataOut_index_init, const ai_size height_in, const ai_size width_in, const ai_size n_channel_in, const ai_size k, ai_i16 largest, void (*f)(const ai_float* inputs, ai_float* values, ai_i32* indices, ai_size k, ai_size n_elements, ai_i32 stride, ai_i16 largest) ); /*! * @brief Handles 2D convolution with binary input, binary output and * binary weights - with 0 padding (QKeras like) - Lite I/F * - Optimized thanks to Optim0 assumptions * @ingroup lite_conv2d_dqnn */ LITE_API_ENTRY void forward_lite_topK_axis_1_if32of32(const ai_float *pDataIn_init, ai_float *pDataOut_values_init, ai_i32 *pDataOut_index_init, const ai_size height_in, const ai_size width_in, const ai_size n_channel_in, const ai_size k, ai_i16 largest, void (*f)(const ai_float* inputs, ai_float* values, ai_i32* indices, ai_size k, ai_size n_elements, ai_i32 stride, ai_i16 largest) ); /*! * @brief Handles 2D convolution with binary input, 8-bits output and * binary weights - with 0 padding (QKeras like) - Lite I/F * @ingroup lite_conv2d_dqnn */ LITE_API_ENTRY void forward_lite_topK_axis_2_if32of32(const ai_float *pDataIn_init, ai_float *pDataOut_values_init, ai_i32 *pDataOut_index_init, const ai_size height_in, const ai_size width_in, const ai_size n_channel_in, const ai_size k, ai_i16 largest, void (*f)(const ai_float* inputs, ai_float* values, ai_i32* indices, ai_size k, ai_size n_elements, ai_i32 stride, ai_i16 largest) ); LITE_API_ENTRY void forward_lite_func_reduce_l1_if32of32( ai_float* out_ptr, const ai_float* in_ptr, const ai_size out_size, const ai_size in_step, const ai_size axis_size, const ai_size axis_step); LITE_API_ENTRY void forward_lite_func_reduce_l2_if32of32( ai_float* out_ptr, const ai_float* in_ptr, const ai_size out_size, const ai_size in_step, const ai_size axis_size, const ai_size axis_step); #endif /*LITE_GENERIC_FLOAT_H*/
4,287
C
42.755102
148
0.463028
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/Middlewares/ST/AI/Inc/layers_nl.h
/** ****************************************************************************** * @file layers_nl.h * @author AST Embedded Analytics Research Platform * @brief header file of AI platform nonlinearity layers datatypes ****************************************************************************** * @attention * * Copyright (c) 2018 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** @verbatim @endverbatim ****************************************************************************** */ #ifndef LAYERS_NL_H #define LAYERS_NL_H #pragma once #include "layers_common.h" /*! * @defgroup layers_nl Normalization Layers Definitions * @brief definition * */ AI_API_DECLARE_BEGIN /*! * @struct ai_layer_nl * @ingroup layers_nl * @brief Generic Nonlinearity layer * * The type of nonlinearity is handled by the specific forward function. * It is a sequential layer. see @ref ai_layer */ typedef AI_ALIGNED_TYPE(struct, 4) ai_layer_nl_ { AI_LAYER_COMMON_FIELDS_DECLARE AI_CONST ai_array* nl_params; /*!< associated parameters array */ } ai_layer_nl; /*! * @struct ai_layer_sm * @ingroup layers_nl * @brief Softmax Nonlinearity layer * * It is a sequential layer. see @ref ai_layer */ typedef ai_layer_nl ai_layer_sm; /*! * @typedef (*func_nl) * @ingroup layers_nl * @brief Fuction pointer for generic non linear transform * this function pointer abstracts a generic non linear layer. * see @ref nl_func_tanh_array_f32 and similar as examples. */ //typedef void (*func_nl)(ai_array *out, const ai_array *in, // const ai_size size, const ai_handle params); typedef void (*func_nl)(ai_tensor *out, const ai_tensor *in, const ai_size size, const ai_handle params); /*! * @brief Softmax pooling computed on a single float channel * @ingroup layers_nl * @param out opaque handler to float output channel * @param in opaque handler to float input channel * @param channel_size number of elements of the input channel * @param params opaque handler to optional nl parameters */ AI_INTERNAL_API void nl_func_sm_channel_f32(ai_tensor *out, const ai_tensor *in, const ai_size channel_size, const ai_handle params); /*! * @brief Softmax normalization computed on an array of float channels * @ingroup layers_nl * @param out opaque handler to float output channel array * @param in opaque handler to float input channel array * @param in_size total size (number of elements) to process on the input * @param channel_size number of elements of the input channel * @param in_channel_step number of elements to move to next input element * @param out_channel_step number of elements to move to next output element */ AI_INTERNAL_API void nl_func_sm_array_f32(ai_tensor *out, ai_tensor *in, const ai_size in_size, const ai_size channel_size, const ai_size in_channel_step, const ai_size out_channel_step); /*! * @brief Softmax zero pooling computed on a single float channel * @ingroup layers_nl * @param out opaque handler to float output channel * @param in opaque handler to float input channel * @param channel_size number of elements of the input channel * @param params opaque handler to optional nl parameters */ AI_INTERNAL_API void nl_func_sm_zero_channel_f32(ai_tensor *out, const ai_tensor *in, const ai_size channel_size, const ai_handle params); /*! * @brief Probit non linearity * @ingroup layers_nl * @param out opaque handler to float output channel * @param in opaque handler to float input channel * @param channel_size number of elements of the input channel * @param params opaque handler to optional nl parameters */ AI_INTERNAL_API void nl_func_probit_f32(ai_tensor *out, const ai_tensor *in, const ai_size channel_size, const ai_handle params); /*! * @brief Computes the tanh function on a float data array * @ingroup layers_nl * @param in opaque handler to float, size should be 1 * @param out opaque handler to float output elem * @param size number of elements in the input buffer * @param params opaque handler to optional nl parameters */ AI_INTERNAL_API void nl_func_tanh_array_f32(ai_tensor *out, const ai_tensor *in, const ai_size size, const ai_handle params); /*! * @brief Computes the tanh function on a fixed point data array * @ingroup layers_nl * @param in opaque handler to input elements to process * @param out opaque handler to output elements * @param size total size (number of elements) to process on the input * @param params opaque handler to optional nl parameters */ AI_INTERNAL_API void nl_func_tanh_array_fixed(ai_tensor *out, const ai_tensor *in, const ai_size size, const ai_handle params); /*! * @brief Computes the sigmoid function on a float data array * @ingroup layers_nl * @param in opaque handler to float, size should be 1 * @param out opaque handler to float output elem * @param size number of elements in the input buffer * @param params opaque handler to optional nl parameters */ AI_INTERNAL_API void nl_func_sigmoid_array_f32(ai_tensor *out, const ai_tensor *in, const ai_size size, const ai_handle params); /*! * @brief Computes the sigmoid function on a fixed point data array * @ingroup layers_nl * @param in opaque handler to input elements to process * @param out opaque handler to output elements * @param size total size (number of elements) to process on the input * @param params opaque handler to optional nl parameters */ AI_INTERNAL_API void nl_func_sigmoid_array_fixed(ai_tensor *out, const ai_tensor *in, const ai_size size, const ai_handle params); /*! * @brief Computes the hard sigmoid function on a float data array * @ingroup layers_nl * @param in opaque handler to float, size should be 1 * @param out opaque handler to float output elem * @param size number of elements in the input buffer * @param params opaque handler to optional nl parameters */ AI_INTERNAL_API void nl_func_hard_sigmoid_array_f32(ai_tensor *out, const ai_tensor *in, const ai_size size, const ai_handle params); /*! * @brief Computes the logistic function on a float data array * @ingroup layers_nl * @param in opaque handler to float, size should be 1 * @param out opaque handler to float output elem * @param size number of elements in the input buffer * @param params opaque handler to optional nl parameters */ AI_INTERNAL_API void nl_func_logistic_array_f32(ai_tensor *out, const ai_tensor *in, const ai_size size, const ai_handle params); /*! * @brief Computes the swish function on a float data array * @ingroup layers_nl * @param in opaque handler to float, size should be 1 * @param out opaque handler to float output elem * @param size number of elements in the input buffer * @param params opaque handler to optional nl parameters */ AI_INTERNAL_API void nl_func_swish_array_f32(ai_tensor *out, const ai_tensor *in, const ai_size size, const ai_handle params); /*! * @brief Computes the hard swish function on a float data array * @ingroup layers_nl * @param in opaque handler to float, size should be 1 * @param out opaque handler to float output elem * @param size number of elements in the input buffer * @param params opaque handler to optional nl parameters */ AI_INTERNAL_API void nl_func_hard_swish_array_f32(ai_tensor *out, const ai_tensor *in, const ai_size size, const ai_handle params); /*! * @brief Computes the absolute value function on a float data array * @ingroup layers_nl * @param in opaque handler to float, size should be 1 * @param out opaque handler to float output elem * @param size number of elements in the input buffer * @param params opaque handler to optional nl parameters */ AI_INTERNAL_API void nl_func_abs_array_f32(ai_tensor *out, const ai_tensor *in, const ai_size size, const ai_handle params); /*! * @brief Computes the cosine function on a float data array * @ingroup layers_nl * @param in opaque handler to float, size should be 1 * @param out opaque handler to float output elem * @param size number of elements in the input buffer * @param params opaque handler to optional nl parameters */ AI_INTERNAL_API void nl_func_cos_array_f32(ai_tensor *out, const ai_tensor *in, const ai_size size, const ai_handle params); /*! * @brief Computes the inverse cosine function on a float data array * @ingroup layers_nl * @param in opaque handler to float, size should be 1 * @param out opaque handler to float output elem * @param size number of elements in the input buffer * @param params opaque handler to optional nl parameters */ AI_INTERNAL_API void nl_func_acos_array_f32(ai_tensor *out, const ai_tensor *in, const ai_size size, const ai_handle params); /*! * @brief Computes the hyperbolic cosine function on a float data array * @ingroup layers_nl * @param in opaque handler to float, size should be 1 * @param out opaque handler to float output elem * @param size number of elements in the input buffer * @param params opaque handler to optional nl parameters */ AI_INTERNAL_API void nl_func_cosh_array_f32(ai_tensor *out, const ai_tensor *in, const ai_size size, const ai_handle params); /*! * @brief Computes the inverse hyperbolic cosine function on a float data array * @ingroup layers_nl * @param in opaque handler to float, size should be 1 * @param out opaque handler to float output elem * @param size number of elements in the input buffer * @param params opaque handler to optional nl parameters */ AI_INTERNAL_API void nl_func_acosh_array_f32(ai_tensor *out, const ai_tensor *in, const ai_size size, const ai_handle params); /*! * @brief Computes the sine function on a float data array * @ingroup layers_nl * @param in opaque handler to float, size should be 1 * @param out opaque handler to float output elem * @param size number of elements in the input buffer * @param params opaque handler to optional nl parameters */ AI_INTERNAL_API void nl_func_sin_array_f32(ai_tensor *out, const ai_tensor *in, const ai_size size, const ai_handle params); /*! * @brief Computes the inverse sine function on a float data array * @ingroup layers_nl * @param in opaque handler to float, size should be 1 * @param out opaque handler to float output elem * @param size number of elements in the input buffer * @param params opaque handler to optional nl parameters */ AI_INTERNAL_API void nl_func_asin_array_f32(ai_tensor *out, const ai_tensor *in, const ai_size size, const ai_handle params); /*! * @brief Computes the hyperbolic sine function on a float data array * @ingroup layers_nl * @param in opaque handler to float, size should be 1 * @param out opaque handler to float output elem * @param size number of elements in the input buffer * @param params opaque handler to optional nl parameters */ AI_INTERNAL_API void nl_func_sinh_array_f32(ai_tensor *out, const ai_tensor *in, const ai_size size, const ai_handle params); /*! * @brief Computes the inverse hyperbolic sine function on a float data array * @ingroup layers_nl * @param in opaque handler to float, size should be 1 * @param out opaque handler to float output elem * @param size number of elements in the input buffer * @param params opaque handler to optional nl parameters */ AI_INTERNAL_API void nl_func_asinh_array_f32(ai_tensor *out, const ai_tensor *in, const ai_size size, const ai_handle params); /*! * @brief Computes the tangent function on a float data array * @ingroup layers_nl * @param in opaque handler to float, size should be 1 * @param out opaque handler to float output elem * @param size number of elements in the input buffer * @param params opaque handler to optional nl parameters */ AI_INTERNAL_API void nl_func_tan_array_f32(ai_tensor *out, const ai_tensor *in, const ai_size size, const ai_handle params); /*! * @brief Computes the inverse tangent function on a float data array * @ingroup layers_nl * @param in opaque handler to float, size should be 1 * @param out opaque handler to float output elem * @param size number of elements in the input buffer * @param params opaque handler to optional nl parameters */ AI_INTERNAL_API void nl_func_atan_array_f32(ai_tensor *out, const ai_tensor *in, const ai_size size, const ai_handle params); /*! * @brief Computes the inverse hyperbolic tangent function on a float data array * @ingroup layers_nl * @param in opaque handler to float, size should be 1 * @param out opaque handler to float output elem * @param size number of elements in the input buffer * @param params opaque handler to optional nl parameters */ AI_INTERNAL_API void nl_func_atanh_array_f32(ai_tensor *out, const ai_tensor *in, const ai_size size, const ai_handle params); /*! * @brief Computes the error function on a float data array * @ingroup layers_nl * @param in opaque handler to float, size should be 1 * @param out opaque handler to float output elem * @param size number of elements in the input buffer * @param params opaque handler to optional nl parameters */ AI_INTERNAL_API void nl_func_erf_array_f32(ai_tensor *out, const ai_tensor *in, const ai_size size, const ai_handle params); /*! * @brief Computes the natural logarithm function on a float data array * @ingroup layers_nl * @param in opaque handler to float, size should be 1 * @param out opaque handler to float output elem * @param size number of elements in the input buffer * @param params opaque handler to optional nl parameters */ AI_INTERNAL_API void nl_func_log_array_f32(ai_tensor *out, const ai_tensor *in, const ai_size size, const ai_handle params); /*! * @brief Computes the reciprocal square root function on a float data array * @ingroup layers_nl * @param in opaque handler to float, size should be 1 * @param out opaque handler to float output elem * @param size number of elements in the input buffer * @param params opaque handler to optional nl parameters */ AI_INTERNAL_API void nl_func_rsqrt_array_f32(ai_tensor *out, const ai_tensor *in, const ai_size size, const ai_handle params); /*! * @brief Computes the squarefunction on a float data array * @ingroup layers_nl * @param in opaque handler to float, size should be 1 * @param out opaque handler to float output elem * @param size number of elements in the input buffer * @param params opaque handler to optional nl parameters */ AI_INTERNAL_API void nl_func_square_array_f32(ai_tensor *out, const ai_tensor *in, const ai_size size, const ai_handle params); /*! * @brief Computes the floor function on a float data array * @ingroup layers_nl * @param in opaque handler to float, size should be 1 * @param out opaque handler to float output elem * @param size number of elements in the input buffer * @param params opaque handler to optional nl parameters */ AI_INTERNAL_API void nl_func_floor_array_f32(ai_tensor *out, const ai_tensor *in, const ai_size size, const ai_handle params); /*! * @brief Computes the ceil function on a float data array * @ingroup layers_nl * @param in opaque handler to float, size should be 1 * @param out opaque handler to float output elem * @param size number of elements in the input buffer * @param params opaque handler to optional nl parameters */ AI_INTERNAL_API void nl_func_ceil_array_f32(ai_tensor *out, const ai_tensor *in, const ai_size size, const ai_handle params); /*! * @brief Computes the rounding function on a float data array * @ingroup layers_nl * @param in opaque handler to float, size should be 1 * @param out opaque handler to float output elem * @param size number of elements in the input buffer * @param params opaque handler to optional nl parameters */ AI_INTERNAL_API void nl_func_round_array_f32(ai_tensor *out, const ai_tensor *in, const ai_size size, const ai_handle params); /*! * @brief Computes the exponential function on a float data array * @ingroup layers_nl * @param in opaque handler to float, size should be 1 * @param out opaque handler to float output elem * @param size number of elements in the input buffer * @param params opaque handler to optional nl parameters */ AI_INTERNAL_API void nl_func_exp_array_f32(ai_tensor *out, const ai_tensor *in, const ai_size size, const ai_handle params); /*! * @brief Computes the sign negation function on a float data array * @ingroup layers_nl * @param in opaque handler to float, size should be 1 * @param out opaque handler to float output elem * @param size number of elements in the input buffer * @param params opaque handler to optional nl parameters */ AI_INTERNAL_API void nl_func_neg_array_f32(ai_tensor *out, const ai_tensor *in, const ai_size size, const ai_handle params); /*! * @brief Computes the sign negation function on a float data array * @ingroup layers_nl * @param in opaque handler to float, size should be 1 * @param out opaque handler to float output elem * @param size number of elements in the input buffer * @param params opaque handler to optional nl parameters */ AI_INTERNAL_API void nl_func_not_array_bool(ai_tensor *out, const ai_tensor *in, const ai_size size, const ai_handle params); /*! * @brief Computes the reciprocal function on a float data array * @ingroup layers_nl * @param in opaque handler to float, size should be 1 * @param out opaque handler to float output elem * @param size number of elements in the input buffer * @param params opaque handler to optional nl parameters */ AI_INTERNAL_API void nl_func_reciprocal_array_f32(ai_tensor *out, const ai_tensor *in, const ai_size size, const ai_handle params); /*! * @brief Computes the square root function on a float data array * @ingroup layers_nl * @param in opaque handler to float, size should be 1 * @param out opaque handler to float output elem * @param size number of elements in the input buffer * @param params opaque handler to optional nl parameters */ AI_INTERNAL_API void nl_func_sqrt_array_f32(ai_tensor *out, const ai_tensor *in, const ai_size size, const ai_handle params); /*! * @brief Computes the soft plus function on a float data array * @ingroup layers_nl * @param in opaque handler to float, size should be 1 * @param out opaque handler to float output elem * @param size number of elements in the input buffer */ AI_INTERNAL_API void nl_func_soft_plus_array_f32(ai_tensor *out, const ai_tensor *in, const ai_size size, const ai_handle params); /*! * @brief Computes the soft sign function on a float data array * @ingroup layers_nl * @param in opaque handler to float, size should be 1 * @param out opaque handler to float output elem * @param size number of elements in the input buffer * @param params opaque handler to optional nl parameters */ AI_INTERNAL_API void nl_func_soft_sign_array_f32(ai_tensor *out, const ai_tensor *in, const ai_size size, const ai_handle params); /*! * @brief Computes the sign function on a single float element. * @ingroup layers_nl * @param in opaque handler to float, size should be 1 * @param out opaque handler to float output elem * @param size number of elements in the input buffer */ AI_INTERNAL_API void nl_func_sign_array_f32(ai_tensor *out, const ai_tensor *in, const ai_size size, const ai_handle params); /*! * @brief Computes the clip function on a float data array * @ingroup layers_nl * @param in opaque handler to float, size should be 1 * @param out opaque handler to float output elem * @param size number of elements in the input buffer * @param params opaque handler to optional nl parameters */ AI_INTERNAL_API void nl_func_clip_array_f32(ai_tensor *out, const ai_tensor *in, const ai_size size, const ai_handle params); /*! * @brief Computes the hardmax function on a float data array * @ingroup layers_nl * @param in opaque handler to float, size should be 1 * @param out opaque handler to float output elem * @param axis direction of the max index to be searched */ AI_INTERNAL_API void nl_func_hardmax_array_f32(ai_tensor *out, const ai_tensor *in, const ai_shape *shape, const ai_handle params); /*! * @brief Computes the generic relu function on a float data array * @ingroup layers_nl * @param in opaque handler to float, size should be 1 * @param out opaque handler to float output elem * @param size number of elements in the input buffer * @param params opaque handler to optional nl parameters */ AI_INTERNAL_API void nl_func_relu_generic_array_f32(ai_tensor *out, const ai_tensor *in, const ai_size size, const ai_handle params); /*! * @brief Computes the thresholded relu function on a float data array * @ingroup layers_nl * @param in opaque handler to float, size should be 1 * @param out opaque handler to float output elem * @param size number of elements in the input buffer * @param params opaque handler to optional nl parameters */ AI_INTERNAL_API void nl_func_relu_thresholded_array_f32(ai_tensor *out, const ai_tensor *in, const ai_size size, const ai_handle params); /*! * @brief Computes the relu function on a float data array * @ingroup layers_nl * @param in opaque handler to float, size should be 1 * @param out opaque handler to float output elem * @param size number of elements in the input buffer * @param params opaque handler to optional nl parameters */ AI_INTERNAL_API void nl_func_relu_array_f32(ai_tensor *out, const ai_tensor *in, const ai_size size, const ai_handle params); /*! * @brief Computes the relu function on a fixed point data array * @ingroup layers_nl * @param in opaque handler to input elements to process * @param out opaque handler to output elements * @param size total size (number of elements) to process on the input * @param params opaque handler to optional nl parameters */ AI_INTERNAL_API void nl_func_relu_array_fixed(ai_tensor *out, const ai_tensor *in, const ai_size size, const ai_handle params); /*! * @brief Computes the relu function on an integer-quantized data array * @ingroup layers_nl * @param in opaque handler to input elements to process * @param out opaque handler to output elements * @param size total size (number of elements) to process on the input * @param params opaque handler to optional nl parameters */ void nl_func_relu_array_integer(ai_tensor *out, const ai_tensor *in, const ai_size size, const ai_handle params); /*! * @brief Computes the clip function on an integer-quantized data array * @ingroup layers_nl * @param in opaque handler to input elements to process * @param out opaque handler to output elements * @param size total size (number of elements) to process on the input * @param params opaque handler to optional nl parameters */ void nl_func_clip_array_integer(ai_tensor *out, const ai_tensor *in, const ai_size size, const ai_handle params); /*! * @brief Computes the activation function on an integer-quantized data array * @ingroup layers_nl * @param in opaque handler to input elements to process * @param out opaque handler to output elements * @param size total size (number of elements) to process on the input * @param params opaque handler to generated and used LUT */ void nl_func_array_integer(ai_tensor *out, const ai_tensor *in, const ai_size size, const ai_handle params); /*! * @brief Computes the elu function on a float data array * @ingroup layers_nl * @param in opaque handler to float, size should be 1 * @param out opaque handler to float output elem * @param size number of elements in the input buffer * @param params opaque handler to optional nl parameters */ AI_INTERNAL_API void nl_func_elu_array_f32(ai_tensor *out, const ai_tensor *in, const ai_size size, const ai_handle params); /*! * @brief Computes the max relu function on a fixed point data array * @ingroup layers_nl * @param in opaque handler to input elements to process * @param out opaque handler to output elements * @param size total size (number of elements) to process on the input * @param params opaque handler to optional nl parameters */ AI_INTERNAL_API void nl_func_relu_max_array_fixed(ai_tensor *out, const ai_tensor *in, const ai_size size, const ai_handle params); /*! * @brief Computes the selu function on a float data array * @ingroup layers_nl * @param in opaque handler to float, size should be 1 * @param out opaque handler to float output elem * @param size number of elements in the input buffer * @param params opaque handler to optional nl parameters */ AI_INTERNAL_API void nl_func_selu_array_f32(ai_tensor *out, const ai_tensor *in, const ai_size size, const ai_handle params); /*! * @brief Computes the prelu function on a float data array * @ingroup layers_nl * @param in opaque handler to float, size should be 1 * @param slope opaque handler to float, size should be 1 * @param out opaque handler to float output elem * @param size size of the input data in bytes * @param params opaque handler to optional nl parameters */ AI_INTERNAL_API void nl_func_prelu_array_f32(ai_tensor *out, const ai_tensor *in, const ai_size size, const ai_handle params); /*! * @brief Computes the prelu function on an integer-quantized data array * @ingroup layers_nl * @param in opaque handler to input elements to process * @param out opaque handler to output elements * @param size total size (number of elements) to process on the input * @param params opaque handler to optional nl parameters */ void nl_func_prelu_array_integer(ai_tensor *out, const ai_tensor *in, const ai_size size, const ai_handle params); /******************************************************************************/ /** Forward Functions Section **/ /******************************************************************************/ /*! * @brief Computes the activations of a ReLU nonlinear layer. * @ingroup layers_nl * @param layer the nonlinear (nl) layer */ AI_INTERNAL_API void forward_relu(ai_layer* layer); /*! * @brief Computes the activations of a fixed point ReLU nonlinear layer. * @ingroup layers_nl * @param layer the nonlinear (nl) layer */ AI_INTERNAL_API void forward_relu_fixed(ai_layer *pLayer); /*! * @brief Computes the activations of a integer-quantized ReLU nonlinear layer. * @ingroup layers_nl * @param layer the nonlinear (nl) layer */ AI_INTERNAL_API void forward_relu_integer(ai_layer *pLayer); /*! * @brief Computes the activations of a clip integer-quantized nonlinear layer. * @ingroup layers_nl * @param pLayer the nonlinear (nl) layer */ AI_INTERNAL_API void forward_clip_integer(ai_layer *pLayer); /*! * @brief Computes the activations of a ReLU6 nonlinear layer. * @ingroup layers_nl * @param layer the nonlinear (nl) layer */ AI_INTERNAL_API void forward_relu_thresholded(ai_layer* layer); /*! * @brief Computes the activations of a fixed point max ReLU layer. * @ingroup layers_nl * @param layer the nonlinear (nl) layer */ AI_INTERNAL_API void forward_relu_max_fixed(ai_layer *pLayer); /*! * @brief Computes the activations of a ELU nonlinear layer. * @ingroup layers_nl * @param layer the nonlinear (nl) layer */ AI_INTERNAL_API void forward_elu(ai_layer* layer); /*! * @brief Computes the activations of a SELU nonlinear layer. * @ingroup layers_nl * @param layer the nonlinear (nl) layer */ AI_INTERNAL_API void forward_selu(ai_layer* layer); /*! * @brief Computes the activations of a PRELU nonlinear layer. * @ingroup layers_nl * @param layer the nonlinear (nl) layer */ AI_INTERNAL_API void forward_prelu(ai_layer* layer); /*! * @brief Computes the activations of a binary tanh (sign) nonlinear layer. * @ingroup layers * @param layer the nonlinear (nl) layer */ AI_INTERNAL_API void forward_sign(ai_layer* layer); /*! * @brief Computes the activations of a clip nonlinear layer. * @ingroup layers * @param layer the nonlinear (nl) layer */ AI_INTERNAL_API void forward_clip(ai_layer* layer); /*! * @brief Computes the activations of a sigmoid nonlinear layer. * @ingroup layers_nl * @param layer the nonlinear (nl) layer */ AI_INTERNAL_API void forward_sigmoid(ai_layer* layer); /*! * @brief Computes the activations of a fixed point sigmoid nonlinear layer. * @ingroup layers_nl * @param layer the nonlinear (nl) layer */ AI_INTERNAL_API void forward_sigmoid_fixed(ai_layer *pLayer); /*! * @brief Computes the activations of a hard sigmoid nonlinear layer. * @ingroup layers_nl * @param layer the nonlinear (nl) layer */ AI_INTERNAL_API void forward_hard_sigmoid(ai_layer* layer); /*! * @brief Computes the activations of a swish nonlinear layer. * @ingroup layers_nl * @param layer the nonlinear (nl) layer */ AI_INTERNAL_API void forward_swish(ai_layer* layer); /*! * @brief Computes the activations of a hard swish nonlinear layer. * @ingroup layers_nl * @param layer the nonlinear (nl) layer */ AI_INTERNAL_API void forward_hard_swish(ai_layer* layer); /*! * @brief Computes the activations of an exponential nonlinear layer. * @ingroup layers_nl * @param layer the nonlinear (nl) layer */ AI_INTERNAL_API void forward_exp(ai_layer* layer); /*! * @brief Computes the activations of an square root nonlinear layer. * @ingroup layers_nl * @param layer the nonlinear (nl) layer */ AI_INTERNAL_API void forward_sqrt(ai_layer* layer); /*! * @brief Computes the activations of a soft plus nonlinear layer. * @ingroup layers_nl * @param layer the nonlinear (nl) layer */ AI_INTERNAL_API void forward_soft_plus(ai_layer* layer); /*! * @brief Computes the activations of a soft sign nonlinear layer. * @ingroup layers_nl * @param layer the nonlinear (nl) layer */ AI_INTERNAL_API void forward_soft_sign(ai_layer* layer); /*! * @brief Computes the activations of a cosine (cos) layer. * @ingroup layers_nl * @param layer the nonlinear (nl) layer */ AI_INTERNAL_API void forward_cos(ai_layer* layer); /*! * @brief Computes the activations of a inverse cosine (acos) layer. * @ingroup layers_nl * @param layer the nonlinear (nl) layer */ AI_INTERNAL_API void forward_acos(ai_layer* layer); /*! * @brief Computes the activations of a hyperbolic cosine (cosh) layer. * @ingroup layers_nl * @param layer the nonlinear (nl) layer */ AI_INTERNAL_API void forward_cosh(ai_layer* layer); /*! * @brief Computes the activations of a inverse hyperbolic cosine (acosh) layer. * @ingroup layers_nl * @param layer the nonlinear (nl) layer */ AI_INTERNAL_API void forward_acosh(ai_layer* layer); /*! * @brief Computes the activations of a sine (sin) layer. * @ingroup layers_nl * @param layer the nonlinear (nl) layer */ AI_INTERNAL_API void forward_sin(ai_layer* layer); /*! * @brief Computes the activations of a inverse sine (asin) layer. * @ingroup layers_nl * @param layer the nonlinear (nl) layer */ AI_INTERNAL_API void forward_asin(ai_layer* layer); /*! * @brief Computes the activations of a hyperbolic sine (sinh) layer. * @ingroup layers_nl * @param layer the nonlinear (nl) layer */ AI_INTERNAL_API void forward_sinh(ai_layer* layer); /*! * @brief Computes the activations of a inverse hyperbolic sine (asinh) layer. * @ingroup layers_nl * @param layer the nonlinear (nl) layer */ AI_INTERNAL_API void forward_asinh(ai_layer* layer); /*! * @brief Computes the activations of a tangent (tan) layer. * @ingroup layers_nl * @param layer the nonlinear (nl) layer */ AI_INTERNAL_API void forward_tan(ai_layer* layer); /*! * @brief Computes the activations of a inverse tangent (atan) layer. * @ingroup layers_nl * @param layer the nonlinear (nl) layer */ AI_INTERNAL_API void forward_atan(ai_layer* layer); /*! * @brief Computes the activations of a hyperbolic tangent (tanh) layer. * @ingroup layers_nl * @param layer the nonlinear (nl) layer */ AI_INTERNAL_API void forward_tanh(ai_layer* layer); /*! * @brief Computes the activations of a inverse hyperbolic tangent (atanh) layer. * @ingroup layers_nl * @param layer the nonlinear (nl) layer */ AI_INTERNAL_API void forward_atanh(ai_layer* layer); /*! * @brief Computes the activations of a fixed point tanh nonlinear layer. * @ingroup layers_nl * @param layer the nonlinear (nl) layer */ AI_INTERNAL_API void forward_tanh_fixed(ai_layer *pLayer); /*! * @brief Computes the activations of a error function (erf) layer. * @ingroup layers_nl * @param layer the nonlinear (nl) layer */ AI_INTERNAL_API void forward_erf(ai_layer* layer); /*! * @brief Computes the activations of a natural logarithm (log) layer. * @ingroup layers_nl * @param layer the nonlinear (nl) layer */ AI_INTERNAL_API void forward_log(ai_layer* layer); /*! * @brief Computes the activations of a reciprocal square root (rsqrt) layer. * @ingroup layers_nl * @param layer the nonlinear (nl) layer */ AI_INTERNAL_API void forward_rsqrt(ai_layer* layer); /*! * @brief Computes the activations of a square layer. * @ingroup layers_nl * @param layer the nonlinear (nl) layer */ AI_INTERNAL_API void forward_square(ai_layer* layer); /*! * @brief Computes the activations of an absolute value (abs) layer. * @ingroup layers_nl * @param layer the nonlinear (nl) layer */ AI_INTERNAL_API void forward_abs(ai_layer* layer); /*! * @brief Computes the activations of a ceil layer. * @ingroup layers_nl * @param layer the nonlinear (nl) layer */ AI_INTERNAL_API void forward_ceil(ai_layer* layer); /*! * @brief Computes the activations of a floor layer. * @ingroup layers_nl * @param layer the nonlinear (nl) layer */ AI_INTERNAL_API void forward_floor(ai_layer* layer); /*! * @brief Computes the activations of a rounding layer. * @ingroup layers_nl * @param layer the nonlinear (nl) layer */ AI_INTERNAL_API void forward_round(ai_layer* layer); /*! * @brief Computes the activations of a sign negation (neg) layer. * @ingroup layers_nl * @param layer the nonlinear (nl) layer */ AI_INTERNAL_API void forward_neg(ai_layer* layer); /*! * @brief Computes the activations of a sign negation (not) layer. * @ingroup layers_nl * @param layer the nonlinear (nl) layer */ AI_INTERNAL_API void forward_not(ai_layer* layer); /*! * @brief Computes the activations of a reciprocal layer. * @ingroup layers_nl * @param layer the nonlinear (nl) layer */ AI_INTERNAL_API void forward_reciprocal(ai_layer* layer); /*! * @brief Hardmax on an input tensors * @ingroup layers_generic * @param layer the hardmax layer */ AI_INTERNAL_API void forward_hardmax(ai_layer* layer); /*! * @brief Computes the activations of a softmax nonlinear layer. * @ingroup layers_nl * @param layer the softmax (sm) layer */ AI_INTERNAL_API void forward_sm(ai_layer* layer); /*! * @brief Computes the activations of a softmax nonlinear layer (integer version). * @ingroup layers_nl * @param layer the softmax (sm) layer */ AI_INTERNAL_API void forward_sm_integer(ai_layer* layer); /*! * @brief Computes the activations of an integer quantized nonlinear layer. * Non linear operation is function of used LUT defined through * (pLayer->nl_params->data) * @ingroup layers_nl * @param layer the nonlinear (nl) layer */ AI_INTERNAL_API void forward_nl_integer(ai_layer *pLayer); /*! * @brief Computes the activations of an integer quantized PReLu. * Slope params are located like weights, not params because they are * quantized * @ingroup layers_nl * @param layer the nonlinear (nl) layer */ AI_INTERNAL_API void forward_prelu_integer(ai_layer *pLayer); AI_API_DECLARE_END #endif /*LAYERS_NL_H*/
37,339
C
32.63964
84
0.688663
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/Middlewares/ST/AI/Inc/lite_convert_dqnn.h
/** ****************************************************************************** * @file lite_convert_dqnn.h * @author AIS * @brief header file of AI platform lite convert kernel datatypes ****************************************************************************** * @attention * * Copyright (c) 2021 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** @verbatim @endverbatim ****************************************************************************** */ #ifndef LITE_CONVERT_DQNN_H #define LITE_CONVERT_DQNN_H #pragma once #include "ai_lite_interface.h" /******************************************************************************/ /* Forward Functions Section */ /******************************************************************************/ LITE_API_ENTRY void forward_lite_node_convert_is1os8( const ai_pbits *p_in, ai_i8 *p_out, const ai_i32 n_channels, const ai_i32 n_pixels, const ai_i8 *n_values); LITE_API_ENTRY void forward_lite_node_convert_is1os16( const ai_pbits *p_in, ai_i16 *p_out, const ai_i32 n_channels, const ai_i32 n_pixels, const ai_i16 *n_values); LITE_API_ENTRY void forward_lite_node_convert_is1of32( const ai_pbits *p_in, ai_float *p_out, const ai_i32 n_channels, const ai_i32 n_pixels, const ai_float *n_values); /*! * @brief Handles data conversion from 8-bits signed input to signed binary * outputs - Lite API version * @ingroup lite_pw_dqnn */ LITE_API_ENTRY void forward_lite_node_convert_is8os1( const ai_i8 *p_in, ai_pbits *p_out, const ai_i32 n_channels, const ai_i32 n_pixels, const ai_i8 zp, const ai_i8 pad); LITE_API_ENTRY void forward_lite_node_convert_is16os1( const ai_i16 *p_in, ai_pbits *p_out, const ai_i32 n_channels, const ai_i32 n_pixels, const ai_i8 zp, const ai_i8 pad); LITE_API_ENTRY void forward_lite_node_convert_if32os1( const ai_float *p_in, ai_pbits *p_out, const ai_i32 n_channels, const ai_i32 n_pixels, const ai_i8 zp, const ai_i8 pad); LITE_API_ENTRY void forward_lite_node_convert_integer_if32os8( const ai_float *p_in, ai_i8 *p_out, const ai_u32 size, const ai_float out_scale, const ai_i8 out_zeropoint); LITE_API_ENTRY void forward_lite_node_convert_integer_if32ou8( const ai_float *p_in, ai_u8 *p_out, const ai_u32 size, const ai_float out_scale, const ai_u8 out_zeropoint); LITE_API_ENTRY void forward_lite_node_convert_integer_is8of32( const ai_i8 *p_in, ai_float *p_out, const ai_u32 size, const ai_float in_scale, const ai_i8 in_zeropoint); LITE_API_ENTRY void forward_lite_node_convert_integer_iu8of32( const ai_u8 *p_in, ai_float *p_out, const ai_u32 size, const ai_float in_scale, const ai_u8 in_zeropoint); LITE_API_ENTRY void forward_lite_node_convert_if32os16( const ai_float *p_in, ai_i16 *p_out, const ai_u32 size, const ai_float out_scale, const ai_i16 out_zeropoint); LITE_API_ENTRY void forward_lite_node_convert_if32ou16( const ai_float *p_in, ai_u16 *p_out, const ai_u32 size, const ai_float out_scale, const ai_u16 out_zeropoint); LITE_API_ENTRY void forward_lite_node_convert_is16of32( const ai_i16 *p_in, ai_float *p_out, const ai_u32 size, const ai_float in_scale, const ai_i16 in_zeropoint); LITE_API_ENTRY void forward_lite_node_convert_iu16of32( const ai_u16 *p_in, ai_float *p_out, const ai_u32 size, const ai_float in_scale, const ai_u16 in_zeropoint); LITE_API_ENTRY void forward_lite_node_convert_integer_iu8ou8( const ai_u8 *p_in, ai_u8 *p_out, const ai_i32 n_elems, const ai_float scale_ratio, const ai_u8 in_zp, const ai_u8 out_zp); LITE_API_ENTRY void forward_lite_node_convert_integer_iu8os8( const ai_u8 *p_in, ai_i8 *p_out, const ai_i32 n_elems, const ai_float scale_ratio, const ai_u8 in_zp, const ai_i8 out_zp); LITE_API_ENTRY void forward_lite_node_convert_integer_iu8os8_fast( const ai_u8 *p_in, ai_i8 *p_out, const ai_i32 n_elems, const ai_float scale_ratio, const ai_u8 in_zp, const ai_i8 out_zp); LITE_API_ENTRY void forward_lite_node_convert_integer_is8ou8( const ai_i8 *p_in, ai_u8 *p_out, const ai_i32 n_elems, const ai_float scale_ratio, const ai_i8 in_zp, const ai_u8 out_zp); LITE_API_ENTRY void forward_lite_node_convert_integer_is8ou8_fast( const ai_i8 *p_in, ai_u8 *p_out, const ai_i32 n_elems, const ai_float scale_ratio, const ai_i8 in_zp, const ai_u8 out_zp); LITE_API_ENTRY void forward_lite_node_convert_is16ou16( const ai_i16 *p_in, ai_u16 *p_out, const ai_i32 n_elems, const ai_float scale_ratio, const ai_i16 in_zp, const ai_u16 out_zp); #endif /*LITE_CONVERT_DQNN_H*/
5,069
C
21.533333
80
0.616098
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/Middlewares/ST/AI/Inc/lite_dense_ws1.h
#ifndef LITE_DENSE_WS1_H #define LITE_DENSE_WS1_H #pragma once #include "ai_lite_interface.h" /*! * @brief Forward function for a dense layer with signed 16bit input, * signed 16bit output, binary weights and binary bias. * @ingroup lite_dense_ws1 * @param output The pointer to output buffer. * @param input The pointer to input buffer. * @param weights The pointer to weights. * @param bias The pointer to bias. * @param scratch The pointer to the scratch buffer (unused). * @param n_channel_in The number of channels of the input. * @param n_channel_out The number of channels of the output, i.e., * the number of dense hidden neurons. */ LITE_API_ENTRY void forward_lite_dense_is16os16ws1( ai_i16* output, const ai_i16* input, const ai_pbits* weights, const ai_pbits* bias, ai_i32* scratch, const ai_u32 n_channel_in, const ai_u32 n_channel_out); /*! * @brief Forward function for a dense layer with signed 16bit input, * signed 16bit output, binary weights and binary bias. * The BN is fused, i.e., the layer requires weights, scale, and offset, where * weights are those of the dense layer, scale is that of the BN, and the offset * corresponds to dense bias * bn scale + bn offset. If the parameters do not * agree with such convention, the behavior is undefined. * @ingroup lite_dense_ws1 * @param output The pointer to output buffer. * @param input The pointer to input buffer. * @param weights The pointer to weights. * @param scale The pointer to scale. * @param offset The pointer to offset. * @param scratch The pointer to the scratch buffer (unused). * @param n_channel_in The number of channels of the input. * @param n_channel_out The number of channels of the output, i.e., * the number of dense hidden neurons. */ LITE_API_ENTRY void forward_lite_dense_is16os16ws1_bn( ai_i16* output, const ai_i16* input, const ai_pbits* weights, const ai_float *scale, const ai_float *offset, ai_i32* scratch, const ai_u32 n_channel_in, const ai_u32 n_channel_out); /*! * @brief Forward function for a dense layer with signed f32 input, * f32 output, binary weights and binary bias. * @ingroup lite_dense_ws1 * @param output The pointer to output buffer. * @param input The pointer to input buffer. * @param weights The pointer to weights. * @param bias The pointer to bias. * @param scratch The pointer to the scratch buffer (unused). * @param n_channel_in The number of channels of the input. * @param n_channel_out The number of channels of the output, i.e., * the number of dense hidden neurons. */ LITE_API_ENTRY void forward_lite_dense_if32os1ws1( ai_pbits *output, const ai_float *input, const ai_pbits *weights, const ai_float *bias, ai_float *scratch, const ai_u32 n_channel_in, const ai_u32 n_channel_out); /*! * @brief Forward function for a dense layer with signed f32 input, * f32 output, binary weights. * The BN is fused, i.e., the layer requires weights, scale, and offset, where * weights are those of the dense layer, scale is that of the BN, and the offset * corresponds to dense bias * bn scale + bn offset. If the parameters do not * agree with such convention, the behavior is undefined. * @ingroup lite_dense_ws1 * @param output The pointer to output buffer. * @param input The pointer to input buffer. * @param weights The pointer to weights. * @param scale The pointer to scale. * @param offset The pointer to offset. * @param scratch The pointer to the scratch buffer (unused). * @param n_channel_in The number of channels of the input. * @param n_channel_out The number of channels of the output, i.e., * the number of dense hidden neurons. */ LITE_API_ENTRY void forward_lite_dense_if32os1ws1_bn( ai_pbits *output, const ai_float *input, const ai_pbits *weights, const ai_float *scale, const ai_float *offset, ai_float *scratch, const ai_u32 n_channel_in, const ai_u32 n_channel_out); /*! * @brief Forward function for a dense layer with signed f32 input, * f32 output, and binary weights. * @ingroup lite_dense_ws1 * @param output The pointer to output buffer. * @param input The pointer to input buffer. * @param weights The pointer to weights. * @param bias The pointer to binary bias. * @param scratch The pointer to the scratch buffer (unused). * @param n_channel_in The number of channels of the input. * @param n_channel_out The number of channels of the output, i.e., * the number of dense hidden neurons. */ LITE_API_ENTRY void forward_lite_dense_if32of32ws1( ai_float* output, const ai_float* input, const ai_pbits* weights, const ai_pbits* bias, ai_float* scratch, const ai_u32 n_channel_in, const ai_u32 n_channel_out); /*! * @brief Forward function for a dense layer with signed f32 input, * f32 output, and binary weights. * The BN is fused, i.e., the layer requires weights, scale, and offset, where * weights are those of the dense layer, scale is that of the BN, and the offset * corresponds to dense bias * bn scale + bn offset. If the parameters do not * agree with such convention, the behavior is undefined. * @ingroup lite_dense_ws1 * @param output The pointer to output buffer. * @param input The pointer to input buffer. * @param weights The pointer to weights. * @param scale The pointer to scale. * @param offset The pointer to offset. * @param scratch The pointer to the scratch buffer (unused). * @param n_channel_in The number of channels of the input. * @param n_channel_out The number of channels of the output, i.e., * the number of dense hidden neurons. */ LITE_API_ENTRY void forward_lite_dense_if32of32ws1_bn( ai_float *output, const ai_float *input, const ai_pbits *weights, const ai_float *scale, const ai_float *offset, ai_float *scratch, const ai_u32 n_channel_in, const ai_u32 n_channel_out); #endif /* LITE_DENSE_IS1WS1_H */
5,884
C
39.586207
80
0.720768
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/Middlewares/ST/AI/Inc/layers_pad_dqnn.h
/** ****************************************************************************** * @file layers_pad_dqnn.h * @author AIS * @brief header file of AI platform DQNN padding datatypes ****************************************************************************** * @attention * * Copyright (c) 2021 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** @verbatim @endverbatim ****************************************************************************** */ #ifndef LAYERS_PADDING_DQNN_H #define LAYERS_PADDING_DQNN_H #pragma once #include "layers_common.h" #include "layers_generic.h" /*! * @defgroup layers_generic_dqnn Layers Definitions * @brief definition * */ AI_API_DECLARE_BEGIN /******************************************************************************/ /* Forward Functions Section */ /******************************************************************************/ /*! * @brief Handles padding with binary input and binary output * @ingroup layers_generic_dqnn * @param layer pad layer */ AI_INTERNAL_API void forward_pad_is1os1(ai_layer *pLayer); AI_API_DECLARE_END #endif /*LAYERS_PADDING_DQNN_H*/
1,499
C
26.777777
80
0.451634
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/Middlewares/ST/AI/Inc/lite_upsample.h
/** ****************************************************************************** * @file lite_upsample.h * @author AIS * @brief header file of AI platform lite pw kernel datatypes ****************************************************************************** * @attention * * Copyright (c) 2021 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** @verbatim @endverbatim ****************************************************************************** */ #ifndef LITE_UPSAMPLE_H #define LITE_UPSAMPLE_H #pragma once #include "ai_lite_interface.h" void forward_lite_upsample_bilinear_if32of32(const ai_float* in_data, ai_float* out_data, const ai_size width_in, const ai_size height_in, const ai_float width_scale, const ai_float height_scale, const ai_size width_out, const ai_size height_out, const ai_bool center, const ai_size n_channel); void forward_lite_upsample_bilinear_is8os8(const ai_i8* in_data, ai_i8* out_data, const ai_size width_in, const ai_size height_in, const ai_float width_scale, const ai_float height_scale, const ai_size width_out, const ai_size height_out, const ai_bool center, const ai_size n_channel); void forward_lite_upsample_bilinear_iu8ou8(const ai_u8* in_data, ai_u8* out_data, const ai_size width_in, const ai_size height_in, const ai_float width_scale, const ai_float height_scale, const ai_size width_out, const ai_size height_out, const ai_bool center, const ai_size n_channel); void forward_lite_upsample_bilinear_is16os16(const ai_i16* in_data, ai_i16* out_data, const ai_size width_in, const ai_size height_in, const ai_float width_scale, const ai_float height_scale, const ai_size width_out, const ai_size height_out, const ai_bool center, const ai_size n_channel); void forward_lite_upsample_bilinear_iu16ou16(const ai_u16* in_data, ai_u16* out_data, const ai_size width_in, const ai_size height_in, const ai_float width_scale, const ai_float height_scale, const ai_size width_out, const ai_size height_out, const ai_bool center, const ai_size n_channel); #endif /*LITE_UPSAMPLE__H*/
3,970
C
48.024691
80
0.380605
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/Middlewares/ST/AI/Inc/layers_generic.h
/** ****************************************************************************** * @file layers_generic.h * @author AST Embedded Analytics Research Platform * @brief header file of AI platform generic layers datatypes ****************************************************************************** * @attention * * Copyright (c) 2018 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** @verbatim @endverbatim ****************************************************************************** */ #ifndef LAYERS_GENERIC_H #define LAYERS_GENERIC_H #pragma once #include "layers_common.h" typedef enum { KTfLiteNone = 0, KTfLiteActRelu, KTfLiteActRelu1, KTfLiteActRelu6, KTfLiteActTanh, KTfLiteActSignBit, KTfLiteActSigmoid } ai_tflitefused_activation; /*! * @defgroup layers_generic Generic Layers Definitions * @brief definition * */ AI_API_DECLARE_BEGIN /*! * @struct ai_layer_time_delay * @ingroup layers_generic * @brief TimeDelay layer with sparse kernel */ typedef AI_ALIGNED_TYPE(struct, 4) ai_layer_time_delay_ { AI_LAYER_COMMON_FIELDS_DECLARE AI_CONST ai_array* mask; /*!< sparse filter mask */ } ai_layer_time_delay; /*! * @struct ai_layer_split * @ingroup layers_generic * @brief Split layer definition * * This layer defines the params of a splitting layer. It is intended to be used * by his associated forward function @ref forward_split */ //typedef AI_ALIGNED_TYPE(struct, 4) ai_layer_split_ { // AI_LAYER_COMMON_FIELDS_DECLARE // ai_u16 out_layers_count; /*!< number of output layers to split*/ // ai_u16 out_layer_curr; /*!< current layer to split */ // ai_layer** out_layers; /*!< output layers list */ // ai_tensor** out_tensors; /*!< output tensors list */ // ai_tensor* in_tensor; /*!< input tensor */ // func_copy_tensor copy_to_out_tensor; /*!< pointer to copy tensor func // (NULL = no copy) */ //} ai_layer_split; typedef AI_ALIGNED_TYPE(struct, 4) ai_layer_split_ { AI_LAYER_COMMON_FIELDS_DECLARE ai_shape_dimension axis; //ai_tensor* num_or_size_splits; } ai_layer_split; /*! * @struct ai_layer_topK * @ingroup layers_generic * @brief topK layer definition */ typedef AI_ALIGNED_TYPE(struct, 4) ai_layer_topK_{ AI_LAYER_COMMON_FIELDS_DECLARE ai_i16 axis; ai_i16 largest; } ai_layer_topK; typedef AI_ALIGNED_TYPE(struct,4)ai_layer_svdf_{ AI_LAYER_COMMON_FIELDS_DECLARE ai_size rank; ai_tflitefused_activation activation; } ai_layer_svdf; /*! * @struct ai_layer_slice * @ingroup layers_generic * @brief Slice layer definition * * This layer defines the params of a slicing layer. It is intended to be used * by his associated forward function @ref forward_slice */ typedef AI_ALIGNED_TYPE(struct, 4) ai_layer_slice_ { AI_LAYER_COMMON_FIELDS_DECLARE AI_CONST ai_array* axes; /*!< Axes that 'starts' and 'ends' apply to. It's optional*/ AI_CONST ai_array* starts; /*!< Starting indices of corrisponding axis in axes*/ AI_CONST ai_array* ends; /*!< Ending indices (exclusive) of corrisponding axis in axes*/ } ai_layer_slice; /*! * @struct ai_layer_gather * @ingroup layers_generic * @brief Gather layer definition * * This layer defines the params of a gathering layer. It is intended to be used * by his associated forward function @ref forward_gather */ typedef AI_ALIGNED_TYPE(struct, 4) ai_layer_gather_ { AI_LAYER_COMMON_FIELDS_DECLARE ai_i16 axis; /*!< Which axis to gather on It's optional*/ ai_tensor* indices; /*!< Indices of corrisponding axis in axes*/ } ai_layer_gather; /*! * @struct ai_layer_tile * @ingroup layers generic * @brief Tile layer definition * * This layer defines the param of an tile layer. It constructs a tensor by tiling a * given tensor. It is intended to be used by its associated forward function * @ref forward_upsample */ typedef AI_ALIGNED_TYPE(struct, 4) ai_layer_tile_{ AI_LAYER_COMMON_FIELDS_DECLARE AI_CONST ai_array* repeats; /*!< numbers of repeated copies along each dimension */ } ai_layer_tile; /*! * @struct ai_layer_shape * @ingroup layers generic * @brief Shape layer definition * * This layer defines the param of a shape layer. It returns the shape of the * input tensor. It is intended to be used by its associated forward function * @ref forward_shape */ typedef AI_ALIGNED_TYPE(struct, 4) ai_layer_shape_{ AI_LAYER_COMMON_FIELDS_DECLARE } ai_layer_shape; /*! * @struct ai_layer_upsample * @ingroup layers generic * @brief Upsample layer definition * * This layer defines the param of an upsampling layer. It overloads its params * to allow zeros upsampling, helpful traspose convolutions, for instance. * It is intended to be used by its associated forward function @ref forward_upsample */ typedef AI_ALIGNED_TYPE(struct, 4) ai_layer_upsample_{ AI_LAYER_COMMON_FIELDS_DECLARE ai_upsample_mode mode; /*!< upsample mode */ ai_bool center; /*!< center pixels */ AI_CONST ai_array* scales; /*!< scale array along each dimension */ ai_nearest_mode nearest_mode; /*!< used in nearest mode */ } ai_layer_upsample; /*! * @struct ai_layer_resize * @ingroup layers generic * @brief Resize layer definition * * This layer defines the param of a resize layer. * It is intended to be used by its associated forward function @ref forward_resize */ typedef AI_ALIGNED_TYPE(struct, 4) ai_layer_resize_{ AI_LAYER_COMMON_FIELDS_DECLARE ai_coord_transf_mode coord_transf_mode; /*!< coordinate tranformation mode */ ai_float cubic_coeff_a; /*!< the coefficient 'a' used in cubic interpolation */ ai_bool exclude_outside; /*!< exclude outside pixels flag */ ai_float extrapol_val; /*!< used in tf_crop_and_resize cas */ ai_resize_mode mode; /*!< resize mode */ ai_nearest_mode nearest_mode; /*!< used in nearest mode */ AI_CONST ai_array* scales; /*!< scale array along each dimension */ AI_CONST ai_array* roi; /*!< roi array, used in tf_crop_and_resize case */ } ai_layer_resize; /*! * @struct ai_layer_instanceNormalization * @ingroup layers generic * @brief instance normalization layer definition * * This layer defines the params of an instance normalization layer. * It is intended to be used by its associated forward function @ref forward_instanceNormalization */ typedef AI_ALIGNED_TYPE(struct, 4) ai_layer_instanceNormaization_{ AI_LAYER_COMMON_FIELDS_DECLARE ai_float eps; /*!< epsilon value, to avoid by zero division */ } ai_layer_instanceNormalization; /*! * @struct ai_layer_mode * @ingroup layers generic * @brief Pad layer definition * * This layer defines the param of an pad layer. It pad a tensor. * It is intended to be used by its associated forward function @ref forward_pad */ typedef AI_ALIGNED_TYPE(struct, 4) ai_layer_pad_{ AI_LAYER_COMMON_FIELDS_DECLARE ai_pad_mode mode; /*!< pad mode */ ai_shape pads; /*!< Number of padding to add or remove at the beginning and end of each axis */ const ai_array* value; /*!< Indicates the value to be filled */ } ai_layer_pad; /*! * @struct ai_layer_mode * @ingroup layers generic * @brief ConstantOfShape layer definition * * This layer defines the param of an constantofshape layer. It constantofshape a tensor. * It is intended to be used by its associated forward function @ref forward_constantofshape */ typedef AI_ALIGNED_TYPE(struct, 4) ai_layer_constantofshape_{ AI_LAYER_COMMON_FIELDS_DECLARE const ai_array* value; /*!< Indicates the value to be filled */ } ai_layer_constantofshape; /*! * @struct ai_layer_add * @ingroup layers_generic * @brief Add layer definition * * This layer defines the params of an add layer. */ typedef AI_ALIGNED_TYPE(struct, 4) ai_layer_add_ { AI_LAYER_COMMON_FIELDS_DECLARE ai_u16 in_layers_count; /*!< number of input layers to concat */ ai_u16 in_layer_curr; /*!< current layer to concat */ ai_tensor** in_tensors; /*!< input tensors list (if NULL==no copy) */ ai_tensor* out_tensor; /*!< output tensor (if NULL==no copy) */ func_copy_tensor copy_to_out_tensor; /*!< pointer to copy tensor func (NULL = no copy) */ ai_layer_base* split_layer; /*!< pointer to associated split layer */ ai_layer_base* next_layer; /*!< pointer to next layer to process */ } ai_layer_add; typedef AI_ALIGNED_TYPE(struct, 4) ai_layer_argmax_ { AI_LAYER_COMMON_FIELDS_DECLARE ai_i16 axis; ai_i16 select_last_index; } ai_layer_argmax; typedef AI_ALIGNED_TYPE(struct, 4) ai_layer_argmin_ { AI_LAYER_COMMON_FIELDS_DECLARE ai_i16 axis; ai_i16 select_last_index; } ai_layer_argmin; // TODO: REMOVE This legacy typedef ai_layer_argmax ai_layer_ArgMax; typedef ai_layer_argmin ai_layer_ArgMin; /*! * @struct ai_layer_transpose * @ingroup layers_generic * @brief Transpose layer datastruct declaration. This defines the params of a * transpose layer. It is intended to be used by his associated forward function * @ref forward_transpose */ typedef AI_ALIGNED_TYPE(struct, 4) ai_layer_transpose_ { AI_LAYER_COMMON_FIELDS_DECLARE ai_shape out_mapping; /*!< transpose output mapping order. I.e. tt is a permutation of the input tensor shape */ } ai_layer_transpose; /*! * @struct ai_layer_transpose_batch * @ingroup layers_generic * @brief Transpose batch layer datastruct declaration. This defines the params of a * transpose layer. It is intended to be used by his associated forward function * @ref forward_transpose_batch */ typedef ai_layer_base ai_layer_transpose_batch; #define AI_TIME_DISTRIBUTED_AXIS (AI_SHAPE_HEIGHT) /*! * @struct ai_layer_time_distributed * @ingroup layers_generic * @brief Time distributed layer datastruct declaration. This defines the params * of a time distributed layer. It is intended to be used by his associated * forward function @ref forward_time_distributed */ typedef AI_ALIGNED_TYPE(struct, 4) ai_layer_time_distributed_ { AI_LAYER_COMMON_FIELDS_DECLARE ai_layer_base* inner_layer; /*!< inner layer to process */ } ai_layer_time_distributed; /*! * @struct ai_layer_concat * @ingroup layers_generic * @brief Concatenation layer * * Concat Layer. * It is a sequential layer. see @ref ai_layer_sequential */ typedef AI_ALIGNED_TYPE(struct, 4) ai_layer_concat_ { AI_LAYER_COMMON_FIELDS_DECLARE ai_shape_dimension axis; /*!< which axis to concatenate on */ } ai_layer_concat; /*! * @struct ai_layer_pack * @ingroup layers_generic * @brief pack layer * * Pack Layer. * It is a sequential layer. see @ref ai_layer_sequential */ typedef AI_ALIGNED_TYPE(struct, 4) ai_layer_pack_ { AI_LAYER_COMMON_FIELDS_DECLARE ai_shape_dimension axis; /*!< which axis to concatenate on */ } ai_layer_pack; /*! * @struct ai_layer_unpack * @ingroup layers_generic * @brief unpack layer * * Unpack Layer. * It is a sequential layer. see @ref ai_layer_sequential */ typedef AI_ALIGNED_TYPE(struct, 4) ai_layer_unpack_ { AI_LAYER_COMMON_FIELDS_DECLARE ai_shape_dimension axis; /*!< which axis to concatenate on */ } ai_layer_unpack; typedef void (*func_binary)(ai_handle out,const ai_handle a, const ai_handle b); typedef void (*func_buffer_binary)(ai_handle out,const ai_handle a, const ai_handle b, const ai_size loop); typedef void (*func_buffer_binary_integer)(ai_handle out,const ai_handle a, const ai_handle b, const ai_size loop, const ai_handle scale1, const ai_handle zp1, const ai_handle scale2, const ai_handle zp2, const ai_handle scaleout, const ai_handle zpout, const ai_i32 scalar_op); /*! * @struct ai_layer_eltwise * @ingroup layers_generic * @brief General element-wise transformation layer * * Elementwise Layer. * It is a sequential layer. see @ref ai_layer_sequential */ typedef AI_ALIGNED_TYPE(struct, 4) ai_layer_eltwise_ { AI_LAYER_COMMON_FIELDS_DECLARE func_binary operation; /*!< operation to apply elementwise */ func_buffer_binary buffer_operation; /*!< operation to apply elementwise */ } ai_layer_eltwise; /*! * @struct ai_layer_eltwise_integer * @ingroup layers_generic * @brief General element-wise transformation layer for integer data * * Elementwise Layer. * It is a sequential layer. see @ref ai_layer_sequential */ typedef AI_ALIGNED_TYPE(struct, 4) ai_layer_eltwise_integer_ { AI_LAYER_COMMON_FIELDS_DECLARE func_binary operation; /*!< operation to apply elementwise */ func_buffer_binary_integer buffer_operation; /*!< operation to apply elementwise */ } ai_layer_eltwise_integer; /*! * @struct ai_layer_reduce * @ingroup layers_generic * @brief General dimension reduction layer * * reduction Layer. * It is a sequential layer. see @ref ai_layer_sequential */ typedef AI_ALIGNED_TYPE(struct, 4) ai_layer_reduce_ { AI_LAYER_COMMON_FIELDS_DECLARE const ai_array* neutral_value; /*!< Initialization value for operation */ func_binary operation; /*!< operation to apply elementwise */ } ai_layer_reduce; /*! * @struct ai_layer_reduce_log_sum_exp * @ingroup layers_generic * @brief General dimension reduction layer * * reduction Layer. * It is a sequential layer. see @ref ai_layer_sequential */ typedef AI_ALIGNED_TYPE(struct, 4) ai_layer_reduce_log_sum_exp_ { AI_LAYER_COMMON_FIELDS_DECLARE ai_shape_dimension axis; } ai_layer_reduce_log_sum_exp; /*! * @struct ai_layer_reduce l1 * @ingroup layers_generic * @brief General dimension reduction layer * * reduction Layer. * It is a sequential layer. see @ref ai_layer_sequential */ typedef AI_ALIGNED_TYPE(struct, 4) ai_layer_reduce_l1_ { AI_LAYER_COMMON_FIELDS_DECLARE AI_CONST ai_array* axes; } ai_layer_reduce_l1; /*! * @struct ai_layer_reduce l2 * @ingroup layers_generic * @brief General dimension reduction layer * * reduction Layer. * It is a sequential layer. see @ref ai_layer_sequential */ typedef AI_ALIGNED_TYPE(struct, 4) ai_layer_reduce_l2_ { AI_LAYER_COMMON_FIELDS_DECLARE AI_CONST ai_array* axes; } ai_layer_reduce_l2; /*! * @struct ai_layer_where * @ingroup layers generic * @brief Where layer definition * * This layer operates on 3 input tensors: condition, X and Y. * It return elements, either from X or Y, depending on condition * (with Numpy-style broadcasting support). * @ref forward_where */ typedef AI_ALIGNED_TYPE(struct, 4) ai_layer_where_ { AI_LAYER_COMMON_FIELDS_DECLARE const ai_array *shapes_len; ai_bool channel_first; } ai_layer_where; /*! * @struct ai_layer_reverse * @ingroup layers_reverse * @brief Reverse layer * * The type of reverse function is handled by the specific forward function * @ref forward_svm_regressor */ typedef AI_ALIGNED_TYPE(struct, 4) ai_layer_reverse_ { AI_LAYER_COMMON_FIELDS_DECLARE ai_i32 axis; /*!< selected axis to perform the operation */ } ai_layer_reverse; /******************************************************************************/ /* Forward Functions Section */ /******************************************************************************/ /*! * @brief Dummy forward routine with no processing. * @ingroup layers_generic * @param generic layer handle */ AI_INTERNAL_API void forward_nop(ai_layer* layer); /*! * @brief Computes the activations of a TimeDelay layer. * @ingroup layers_generic * @param layer the time delay layer */ AI_INTERNAL_API void forward_time_delay(ai_layer* layer); /*! * @brief Split network computation in N parallel branches. * @ingroup layers_generic * @param layer the split layer */ AI_INTERNAL_API void forward_split(ai_layer* layer); /*! * @brief Add network computation from N parallel branches. * @ingroup layers_generic * @param layer the add layer */ AI_INTERNAL_API void forward_add(ai_layer* layer); /*! * @brief Compute the indices of the max elements of the input tensor's element along the provided axis. * @ingroup layers_generic * @param layer argmax layer */ AI_INTERNAL_API void forward_argmax(ai_layer* layer); /*! * @brief Compute the indices of the min elements of the input tensor's element along the provided axis. * @ingroup layers_generic * @param layer argmin layer */ AI_INTERNAL_API void forward_argmin(ai_layer* layer); /*! * @brief Svdf layer. * @ingroup layers_generic * @param layer svdf layer */ AI_INTERNAL_API void forward_svdf(ai_layer* layer); /*! * @brief Transpose a tensor along a pivot and save transposed values into an output * tensor * @ingroup layers_generic * @param layer the transpose layer */ AI_INTERNAL_API void forward_transpose(ai_layer* layer); /*! * @brief Transpose batch and save transposed values of a determinate batch into an output * tensor * @ingroup layers_generic * @param layer the transpose batch layer */ AI_INTERNAL_API void forward_transpose_batch(ai_layer* layer); /*! * @brief TimeDistrubuted forward layer function. This forward function * implements the timedistributed layer. * @ingroup layers_generic * @param layer the time distributed layer */ AI_INTERNAL_API void forward_time_distributed(ai_layer* layer); /*! * @brief Packing a list of tensors in a single tensor * @ingroup layers generic * @param layer the packing layer */ AI_INTERNAL_API void forward_pack(ai_layer* layer); /*! * @brief Unpacking a single of tensors in a list tensor * @ingroup layers generic * @param layer the unpacking layer */ AI_INTERNAL_API void forward_unpack(ai_layer* layer); /*! * @brief Concatenates a list of tensors into a single tensor. * @ingroup layers_generic * @param layer the concatenation layer */ AI_INTERNAL_API void forward_concat(ai_layer* layer); /*! * @brief Gather an input tensor * @ingroup layers_generic * @param layer the gathered layer */ AI_INTERNAL_API void forward_gather(ai_layer* layer); /*! * @brief Slice an input tensors * @ingroup layers_generic * @param layer the sliced layer */ AI_INTERNAL_API void forward_slice(ai_layer* layer); /*! * @brief Tile an input tensors * @ingroup layers_generic * @param layer the tiled layer */ AI_INTERNAL_API void forward_tile(ai_layer* layer); /*! * @brief Returns the shape of an input tensors * @ingroup layers_generic * @param layer the Shape layer */ AI_INTERNAL_API void forward_shape(ai_layer* layer); /*! * @brief TopK an input tensors * @ingroup layers_generic * @param layer the Topked layer */ AI_INTERNAL_API void forward_topK(ai_layer* layer); /*! * @brief Pad an input tensors * @ingroup layers_generic * @param layer the pad layer */ AI_INTERNAL_API void forward_pad(ai_layer* layer); /*! * @brief ConstantofShape an input tensors * @ingroup layers_generic * @param layer the constantofshape layer */ AI_INTERNAL_API void forward_constantofshape(ai_layer* layer); /*! * @brief Upsample an input tensors * @ingroup layers_generic * @param layer the upsampled layer */ AI_INTERNAL_API void forward_upsample(ai_layer* layer); /*! * @brief Resize an input tensors * @ingroup layers_generic * @param layer the resized layer */ AI_INTERNAL_API void forward_resize(ai_layer* layer); /*! * @brief Instance Normalization on an input tensors * @ingroup layers_generic * @param layer the instance normalization layer */ AI_INTERNAL_API void forward_instanceNormalization(ai_layer* layer); /*! * @brief Apply an elementwise transformation to the input tensors * @ingroup layers_generic * @param layer the elementwise layer */ AI_INTERNAL_API void forward_eltwise(ai_layer* layer); /*! * @brief Apply an elementwise transformation to the integer input tensors * @ingroup layers_generic * @param layer the elementwise layer */ AI_INTERNAL_API void forward_eltwise_integer(ai_layer* layer); /*! * @brief Apply an elementwise transformation to the signed integer input tensors * @ingroup layers_generic * @param layer the elementwise layer */ AI_INTERNAL_API void forward_eltwise_integer_INT8(ai_layer* layer); /*! * @brief Apply an elementwise transformation to the unsigned integer input tensors * @ingroup layers_generic * @param layer the elementwise layer */ AI_INTERNAL_API void forward_eltwise_integer_UINT8(ai_layer* layer); /*! * @brief Apply a reduce transformation to the input tensors * @ingroup layers_generic * @param layer the reduce layer */ AI_INTERNAL_API void forward_reduce(ai_layer* layer); /*! * @brief Apply a reduce transformation to the input tensors * @ingroup layers_generic * @param layer the reduce layer */ AI_INTERNAL_API void forward_reduce_log_sum_exp(ai_layer* layer); /*! * @brief Apply a reduce transformation to the input tensors * @ingroup layers_generic * @param layer the reduce layer */ AI_INTERNAL_API void forward_reduce_l1(ai_layer* layer); /*! * @brief Apply a reduce transformation to the input tensors * @ingroup layers_generic * @param layer the reduce layer */ AI_INTERNAL_API void forward_reduce_l2(ai_layer* layer); /*! * @brief Behave like numpy.where with Numpy-style broadcasting support * @ingroup layers_generic * @param layer the where layer */ AI_INTERNAL_API void forward_where(ai_layer* layer); /*! * @brief Apply an elementwise addition to the input tensors * @ingroup layers_generic * @param layer the elementwise layer */ AI_INTERNAL_API void forward_add_integer(ai_layer* layer); /*! * @brief Apply an elementwise addition to the input tensors * with int8 I/O * @ingroup layers_generic * @param layer the elementwise layer */ AI_INTERNAL_API void forward_add_integer_INT8(ai_layer* layer); /*! * @brief Apply an elementwise addition to the input tensors * with uint8 I/O * @ingroup layers_generic * @param layer the elementwise layer */ AI_INTERNAL_API void forward_add_integer_UINT8(ai_layer* layer); /*! * @brief Reverse layer. * @ingroup layers_generic * @param layer reverse layer */ AI_INTERNAL_API void forward_reverse(ai_layer *pLayer); /*! * @brief Upsample an input tensors with unsigned 8-bit integer input,. * It is to be used also for other formats, since the function only * performs memory copy. * @ingroup layers_generic * @param layer the upsampled layer */ AI_INTERNAL_API void forward_upsample_generic(ai_layer* layer); AI_API_DECLARE_END #endif /*LAYERS_GENERIC_H*/
22,964
C
28.292092
130
0.683505
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/Middlewares/ST/AI/Inc/ai_datatypes_defines.h
/** ****************************************************************************** * @file ai_datatypes_defines.h * @author AST Embedded Analytics Research Platform * @brief Definitions of AI platform private APIs types ****************************************************************************** * @attention * * Copyright (c) 2017 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** @verbatim @endverbatim ****************************************************************************** */ #ifndef AI_DATATYPES_DEFINES_H #define AI_DATATYPES_DEFINES_H #pragma once #include "ai_platform.h" /*! * @defgroup datatypes_defines Internal Datatypes Defines Header * @brief Data structures used internally to implement neural networks * */ /* define to track datatypes used by codegen */ #define AI_INTERFACE_TYPE /* AI_INTERFACE_TYPE */ #define AI_INTERNAL_API /* AI_INTERNAL_API */ #define AI_CONST const #define AI_STATIC static #define AI_STATIC_CONST static const /******************************************************************************/ /* NOP operation used by codegen */ #define AI_NOP /* NOP */ #define AI_WRAP_FUNC(fn_) do { fn_ } while (0); #define AI_CAT(a, ...) AI_PRIMITIVE_CAT(a, __VA_ARGS__) #define AI_PRIMITIVE_CAT(a, ...) a ## __VA_ARGS__ /******************************************************************************/ #ifdef HAS_AI_ASSERT #include <assert.h> #define AI_ASSERT(cond) \ { assert(cond); } #else #define AI_ASSERT(cond) \ AI_WRAP_FUNC(/*AI_ASSERT*/) #endif /*HAS_AI_ASSERT*/ /******************************************************************************/ #define AI_NO_PACKED_STRUCTS /* Macro for defining packed structures (compiler dependent). * This just reduces memory requirements, but is not required. */ #if defined(AI_NO_PACKED_STRUCTS) /* Disable struct packing */ #define AI_PACKED_STRUCT_START /* AI_PACKED_STRUCT_START */ #define AI_PACKED_STRUCT_END /* AI_PACKED_STRUCT_END */ #define AI_PACKED /* AI_PACKED */ #elif defined(__GNUC__) || defined(__clang__) /* For GCC and clang */ #define AI_PACKED_STRUCT_START /* AI_PACKED_STRUCT_START */ #define AI_PACKED_STRUCT_END /* AI_PACKED_STRUCT_END */ #define AI_PACKED __attribute__((packed)) #elif defined(__ICCARM__) || defined (__IAR_SYSTEMS_ICC__) || defined(__CC_ARM) /* For IAR ARM and Keil MDK-ARM compilers */ #define AI_PACKED_STRUCT_START _Pragma("pack(push, 1)") #define AI_PACKED_STRUCT_END _Pragma("pack(pop)") #define AI_PACKED /* AI_PACKED */ #elif defined(_MSC_VER) && (_MSC_VER >= 1500) /* For Microsoft Visual C++ */ #define AI_PACKED_STRUCT_START __pragma(pack(push, 1)) #define AI_PACKED_STRUCT_END __pragma(pack(pop)) #define AI_PACKED /* AI_PACKED */ #else /* Unknown compiler */ #define AI_PACKED_STRUCT_START /* AI_PACKED_STRUCT_START */ #define AI_PACKED_STRUCT_END /* AI_PACKED_STRUCT_END */ #define AI_PACKED /* AI_PACKED */ #endif /* AI_NO_PACKED_STRUCTS */ /******************************************************************************/ #define AI_STRINGIFY_ARG(contents) # contents #define AI_STRINGIFY(macro_or_string) AI_STRINGIFY_ARG (macro_or_string) /******************************************************************************/ #if defined(_MSC_VER) #define AI_DECLARE_STATIC static __inline // #define AI_FORCE_INLINE static __forceinline #define AI_FORCE_INLINE static __inline #define AI_HINT_INLINE static __inline #define AI_ALIGNED_TYPE(type, x) type __declspec(align(x)) #define AI_INTERFACE_ENTRY __declspec(dllexport) #elif defined(__ICCARM__) || defined (__IAR_SYSTEMS_ICC__) #define AI_DECLARE_STATIC static inline // #define AI_FORCE_INLINE static _Pragma("inline=forced") // TODO: check this definition! #define AI_FORCE_INLINE static inline #define AI_HINT_INLINE static inline #define AI_ALIGNED_TYPE(type, x) type #define AI_INTERFACE_ENTRY /* AI_INTERFACE_ENTRY */ #elif defined(__GNUC__) #define AI_DECLARE_STATIC static __inline #define AI_FORCE_INLINE static __inline #define AI_HINT_INLINE static __inline #define AI_ALIGNED_TYPE(type, x) type __attribute__ ((aligned(x))) #define AI_INTERFACE_ENTRY /* AI_INTERFACE_ENTRY */ #else /* _MSC_VER */ #define AI_DECLARE_STATIC static __inline // #define AI_FORCE_INLINE static __forceinline #define AI_FORCE_INLINE static __inline #define AI_HINT_INLINE static __inline #define AI_ALIGNED_TYPE(type, x) type __attribute__ ((aligned(x))) #define AI_INTERFACE_ENTRY __attribute__((visibility("default"))) #endif /* _MSC_VER */ /******************************************************************************/ #define AI_ALIGN_MASKED(value, mask) ( ((value)+(mask))&(~(mask)) ) #define AI_GET_VERSION_STRING(major, minor, micro) \ AI_STRINGIFY_ARG(major) "." \ AI_STRINGIFY_ARG(minor) "." \ AI_STRINGIFY_ARG(micro) \ #define AI_PACK_TENSORS_PTR(...) \ AI_PACK(__VA_ARGS__) #define AI_PACK_INFO(size_) (ai_tensor_info[1]) { { \ .buffer = (ai_buffer[size_])AI_STRUCT_INIT, \ .state = (ai_tensor_state[size_])AI_STRUCT_INIT, \ } } #define AI_CR "\r\n" #if (defined HAS_AI_DEBUG || defined HAS_DEBUG_LIB) #include <stdio.h> #define AI_DEBUG(...) __VA_ARGS__ #define AI_DEBUG_PRINT(fmt, ...) { printf(fmt, ##__VA_ARGS__); } #else #define AI_DEBUG(...) AI_WRAP_FUNC(/*AI_DEBUG*/) #define AI_DEBUG_PRINT(fmt, ...) AI_WRAP_FUNC(/*AI_DEBUG_PRINT*/) #endif #define AI_FLAG_SET(mask, flag) (mask) |= (flag) #define AI_FLAG_UNSET(mask, flag) (mask) &= (~(flag)) #define AI_FLAG_IS_SET(mask, flag) ((flag)==((mask)&(flag))) #endif /*AI_DATATYPES_DEFINES_H*/
6,551
C
39.444444
105
0.526637
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/Middlewares/ST/AI/Inc/layers_dense_dqnn.h
/** ****************************************************************************** * @file layers_dense_dqnn.h * @author AST Embedded Analytics Research Platform * @brief header file of deeply quantized dense layers. ****************************************************************************** * @attention * * Copyright (c) 2021 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** @verbatim @endverbatim ****************************************************************************** */ #ifndef LAYERS_DENSE_DQNN_H #define LAYERS_DENSE_DQNN_H #pragma once #include "layers_common.h" /*! * @defgroup layers_dense_dqnn Quantized Dense Layers definition. * @brief Implements the kernels and the forward functions to implement * dense layers with quantized inputs, weights, or outputs. */ AI_API_DECLARE_BEGIN /*! * @struct ai_layer_dense_dqnn * @ingroup layers_dense_dqnn * @brief Specific instance of deeply quantized dense layers. */ typedef ai_layer_base ai_layer_dense_dqnn; /*****************************************************************************/ /* Forward Functions Section */ /*****************************************************************************/ /*! * @brief Forward function for a dense layer with signed binary input, * signed binary output, and signed binary weights. * @ingroup layers_dense_dqnn * @param layer template layer as an opaque pointer */ AI_INTERNAL_API void forward_dense_is1os1ws1(ai_layer* layer); /*! * @brief Forward function for a dense layer with signed binary input, * signed binary output, and signed binary weights. * The BN is fused, i.e., the layer requires weights, scale, and offset, where * weights are those of the dense layer, scale is that of the BN, and the offset * corresponds to dense bias * bn scale + bn offset. If the parameters do not * agree with such convention, the behavior is undefined. * @ingroup layers_dense_dqnn * @param layer template layer as an opaque pointer */ AI_INTERNAL_API void forward_dense_is1os1ws1_bn(ai_layer* layer); /*! * @brief Forward function for a dense layer with signed binary input, * 8-bit signed output, and signed binary weights. * @ingroup layers_dense_dqnn * @param layer template layer as an opaque pointer */ AI_INTERNAL_API void forward_dense_is1os8ws1(ai_layer* layer); /*! * @brief Forward function for a dense layer with signed binary input, * 8-bit signed output, and signed binary weights. * @ingroup layers_dense_dqnn * @param layer template layer as an opaque pointer */ AI_INTERNAL_API void forward_dense_is1os16ws1(ai_layer* layer); /*! * @brief Forward function for a dense layer with signed binary input, * 32-bit floating point output, and signed binary weights. * @ingroup layers_dense_dqnn * @param layer template layer as an opaque pointer */ AI_INTERNAL_API void forward_dense_is1of32ws1(ai_layer* layer); /*! * @brief Forward function for a dense layer with signed binary input, * 32-bit floating point output, and signed binary weights. * The BN is fused, i.e., the layer requires weights, scale, and offset, where * weights are those of the dense layer, scale is that of the BN, and the offset * corresponds to dense bias * bn scale + bn offset. If the parameters do not * agree with such convention, the behavior is undefined. * @ingroup layers_dense_dqnn * @param layer template layer as an opaque pointer */ AI_INTERNAL_API void forward_dense_is1of32ws1_bn(ai_layer* layer); /*! * @brief Forward function for a dense layer with signed binary input, * 32-bit floating point output, and 32-bit floating point weights. * @ingroup layers_dense_dqnn * @param layer template layer as an opaque pointer */ AI_INTERNAL_API void forward_dense_is1of32wf32(ai_layer* layer); /*! * @brief Forward function for a dense layer with signed binary input, * 32-bit floating point output, and 32-bit floating point weights. * The BN is fused, i.e., the layer requires weights, scale, and offset, where * weights are those of the dense layer, scale is that of the BN, and the offset * corresponds to dense bias * bn scale + bn offset. If the parameters do not * agree with such convention, the behavior is undefined. * @ingroup layers_dense_dqnn * @param layer template layer as an opaque pointer */ AI_INTERNAL_API void forward_dense_is1of32wf32_bn(ai_layer* layer); /*! * @brief Forward function for a dense layer with signed binary input, * 32-bit floating point output, and 8-bit signed weights. * @ingroup layers_dense_dqnn * @param layer template layer as an opaque pointer */ AI_INTERNAL_API void forward_dense_is1of32ws8(ai_layer* layer); /*! * @brief Forward function for a dense layer with signed binary input, * 32-bit floating point output, and 8-bit signed weights. * The BN is fused, i.e., the layer requires weights, scale, and offset, where * weights are those of the dense layer, scale is that of the BN, and the offset * corresponds to dense bias * bn scale + bn offset. If the parameters do not * agree with such convention, the behavior is undefined. * @ingroup layers_dense_dqnn * @param layer template layer as an opaque pointer */ AI_INTERNAL_API void forward_dense_is1of32ws8_bn(ai_layer* layer); /*! * @brief Forward function for a dense layer with signed binary input, * binary output, and 8-bit signed weights. * @ingroup layers_dense_dqnn * @param layer template layer as an opaque pointer */ AI_INTERNAL_API void forward_dense_is1os1ws8(ai_layer* layer); /*! * @brief Forward function for a dense layer with signed binary input, * binary output, and 8-bit signed weights. * The BN is fused, i.e., the layer requires weights, scale, and offset, where * weights are those of the dense layer, scale is that of the BN, and the offset * corresponds to dense bias * bn scale + bn offset. If the parameters do not * agree with such convention, the behavior is undefined. * @ingroup layers_dense_dqnn * @param layer template layer as an opaque pointer */ AI_INTERNAL_API void forward_dense_is1os1ws8_bn(ai_layer* layer); /*! * @brief Forward function for a dense layer with signed binary input, * 8-bit signed output, and 8-bit signed weights. * @ingroup layers_dense_dqnn * @param layer template layer as an opaque pointer */ AI_INTERNAL_API void forward_dense_is1os8ws8(ai_layer* layer); /*! * @brief Forward function for a dense layer with signed binary input, * 16-bit signed output, and 8-bit signed weights. * @ingroup layers_dense_dqnn * @param layer template layer as an opaque pointer */ AI_INTERNAL_API void forward_dense_is1os16ws8(ai_layer* layer); /*! * @brief Forward function for a dense layer with signed 8-bit input, * float output, and binary weights. * @ingroup layers_dense_dqnn * @param layer template layer as an opaque pointer */ AI_INTERNAL_API void forward_dense_is8of32ws1(ai_layer* layer); /*! * @brief Forward function for a dense layer with signed 8-bit input, * float output, and binary weights. * The BN is fused, i.e., the layer requires weights, scale, and offset, where * weights are those of the dense layer, scale is that of the BN, and the offset * corresponds to dense bias * bn scale + bn offset. If the parameters do not * agree with such convention, the behavior is undefined. * @ingroup layers_dense_dqnn * @param layer template layer as an opaque pointer */ AI_INTERNAL_API void forward_dense_is8of32ws1_bn(ai_layer* layer); /*! * @brief Forward function for a dense layer with signed 8-bit input, * 1-bit signed output, and binary weights. * @ingroup layers_dense_dqnn * @param layer template layer as an opaque pointer */ AI_INTERNAL_API void forward_dense_is8os1ws1(ai_layer* layer); /*! * @brief Forward function for a dense layer with signed 8-bit input, * 1-bit signed output, and binary weights. * The BN is fused, i.e., the layer requires weights, scale, and offset, where * weights are those of the dense layer, scale is that of the BN, and the offset * corresponds to dense bias * bn scale + bn offset. If the parameters do not * agree with such convention, the behavior is undefined. * @ingroup layers_dense_dqnn * @param layer template layer as an opaque pointer */ AI_INTERNAL_API void forward_dense_is8os1ws1_bn(ai_layer* layer); /*! * @brief Forward function for a dense layer with signed 8-bit input, * binary weights and binary output. * The BN is fused, i.e., the layer requires weights, scale, and offset, where * weights are those of the dense layer, scale is that of the BN, and the offset * corresponds to dense bias * bn scale + bn offset. If the parameters do not * agree with such convention, the behavior is undefined. * @ingroup layers_dense_dqnn * @param layer template layer as an opaque pointer */ AI_INTERNAL_API void forward_dense_is8os1ws1_bn_fxp(ai_layer* layer); /*! * @brief Forward function for a dense layer with signed 8-bit input, * 8-bit signed output, and binary weights. * @ingroup layers_dense_dqnn * @param layer template layer as an opaque pointer */ AI_INTERNAL_API void forward_dense_is8os8ws1(ai_layer* layer); /*! * @brief Forward function for a dense layer with signed 8-bit input, * 16-bit signed output, and binary weights. * @ingroup layers_dense_dqnn * @param layer template layer as an opaque pointer */ AI_INTERNAL_API void forward_dense_is8os16ws1(ai_layer* layer); /*! * @brief Forward function for a dense layer with signed 16-bit input, * 1-bit signed output, and binary weights. * @ingroup layers_dense_dqnn * @param layer template layer as an opaque pointer */ AI_INTERNAL_API void forward_dense_is16os1ws1(ai_layer* layer); /*! * @brief Forward function for a dense layer with signed 16-bit input, * 1-bit signed output, and binary weights. * The BN is fused, i.e., the layer requires weights, scale, and offset, where * weights are those of the dense layer, scale is that of the BN, and the offset * corresponds to dense bias * bn scale + bn offset. If the parameters do not * agree with such convention, the behavior is undefined. * @ingroup layers_dense_dqnn * @param layer template layer as an opaque pointer */ AI_INTERNAL_API void forward_dense_is16os1ws1_bn(ai_layer* layer); /*! * @brief Forward function for a dense layer with signed 16-bit input, * 8-bit signed output, and binary weights. * @ingroup layers_dense_dqnn * @param layer template layer as an opaque pointer */ AI_INTERNAL_API void forward_dense_is16os8ws1(ai_layer* layer); /*! * @brief Forward function for a dense layer with signed 16-bit input, * 16-bit signed output, and binary weights. * @ingroup layers_dense_dqnn * @param layer template layer as an opaque pointer */ AI_INTERNAL_API void forward_dense_is16os16ws1(ai_layer* layer); /*! * @brief Forward function for a dense layer with signed 16-bit input, * f32 output, and binary weights. * @ingroup layers_dense_dqnn * @param layer template layer as an opaque pointer */ AI_INTERNAL_API void forward_dense_is16of32ws1(ai_layer* layer); /*! * @brief Forward function for a dense layer with signed 16-bit input, * f32 output, and binary weights. * The BN is fused, i.e., the layer requires weights, scale, and offset, where * weights are those of the dense layer, scale is that of the BN, and the offset * corresponds to dense bias * bn scale + bn offset. If the parameters do not * agree with such convention, the behavior is undefined. * @ingroup layers_dense_dqnn * @param layer template layer as an opaque pointer */ AI_INTERNAL_API void forward_dense_is16of32ws1_bn(ai_layer* layer); /*! * @brief Forward function for a dense layer with signed f32 input, * 1-bit signed output, and binary weights. * @ingroup layers_dense_dqnn * @param layer template layer as an opaque pointer */ AI_INTERNAL_API void forward_dense_if32os1ws1(ai_layer* layer); /*! * @brief Forward function for a dense layer with signed f32 input, * 1-bit signed output, and binary weights. * The BN is fused, i.e., the layer requires weights, scale, and offset, where * weights are those of the dense layer, scale is that of the BN, and the offset * corresponds to dense bias * bn scale + bn offset. If the parameters do not * agree with such convention, the behavior is undefined. * @ingroup layers_dense_dqnn * @param layer template layer as an opaque pointer */ AI_INTERNAL_API void forward_dense_if32os1ws1_bn(ai_layer* layer); /*! * @brief Forward function for a dense layer with signed f32 input, * 8-bit signed output, and binary weights. * @ingroup layers_dense_dqnn * @param layer template layer as an opaque pointer */ AI_INTERNAL_API void forward_dense_if32os8ws1(ai_layer* layer); /*! * @brief Forward function for a dense layer with signed f32 input, * 16-bit signed output, and binary weights. * @ingroup layers_dense_dqnn * @param layer template layer as an opaque pointer */ AI_INTERNAL_API void forward_dense_if32os16ws1(ai_layer* layer); /*! * @brief Forward function for a dense layer with signed f32 input, * f32 output, and binary weights. * @ingroup layers_dense_dqnn * @param layer template layer as an opaque pointer */ AI_INTERNAL_API void forward_dense_if32of32ws1(ai_layer* layer); /*! * @brief Forward function for a dense layer with signed f32 input, * f32 output, and binary weights. * The BN is fused, i.e., the layer requires weights, scale, and offset, where * weights are those of the dense layer, scale is that of the BN, and the offset * corresponds to dense bias * bn scale + bn offset. If the parameters do not * agree with such convention, the behavior is undefined. * @ingroup layers_dense_dqnn * @param layer template layer as an opaque pointer */ AI_INTERNAL_API void forward_dense_if32of32ws1_bn(ai_layer* layer); AI_API_DECLARE_END #endif /*LAYERS_DENSE_DQNN_H*/
14,182
C
34.546366
80
0.709632
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/Middlewares/ST/AI/Inc/ai_lite.h
/** ****************************************************************************** * @file ai_lite.h * @author AST Embedded Analytics Research Platform * @brief Definitions and implementations of runtime-lite public APIs ****************************************************************************** * @attention * * Copyright (c) 2022 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** @verbatim @endverbatim ****************************************************************************** */ #ifndef AI_LITE_H #define AI_LITE_H #pragma once #include "ai_platform.h" #include "ai_lite_inspect.h" #define LITE_API_ENTRY \ /* LITE_API_ENTRY */ #define LITE_GRAPH_INIT(_inputs, _outputs, _activations, _weights, _cb, _cb_cookie) { \ .inputs = (_inputs), \ .outputs = (_outputs), \ .activations = (_activations), \ .weights = (const ai_handle*)(_weights), \ .cb = ((ai_lite_inspect_cb)(_cb)), \ .cb_cookie = ((ai_handle)(_cb_cookie)), \ } AI_API_DECLARE_BEGIN typedef enum { LITE_OK = 0, LITE_KO_INPUTS, LITE_KO_OUTPUTS, LITE_KO_WEIGHTS, LITE_KO_ACTIVATIONS, LITE_KO_GRAPH, } lite_result; typedef struct { ai_handle* inputs; ai_handle* outputs; ai_handle* activations; const ai_handle* weights; ai_lite_inspect_cb cb; ai_handle cb_cookie; } lite_graph; AI_API_DECLARE_END #endif /* AI_LITE_H */
1,699
C
25.5625
87
0.521483
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/Middlewares/ST/AI/Inc/layers_ml_linearclassifier.h
/** ****************************************************************************** * @file layers_ml_linearclassifier.h * @author SRA * @brief header file of AI platform LinearClassifier datatypes ****************************************************************************** * @attention * * Copyright (c) 2021 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** @verbatim @endverbatim ****************************************************************************** */ #ifndef LAYERS_LINEARCLASSIFIER_H #define LAYERS_LINEARCLASSIFIER_H #pragma once #include "layers_common.h" #include "layers_nl.h" /*! * @defgroup layers_linearclassifier Layers Definitions * @brief definition * */ AI_API_DECLARE_BEGIN /*! * @struct ai_layer_linearclassifier * @ingroup layers_linearclassifier * @brief Linearclassifier layer * * The type of svmreg function is handled by the specific forward function * @ref forward_linearclassifier */ typedef AI_ALIGNED_TYPE(struct, 4) ai_layer_linearclassifier_ { AI_LAYER_COMMON_FIELDS_DECLARE func_nl nl_func; /*!< function pointer to non linear transform */ \ ai_bool multi_class; /*!< Indicates whether to do OvR or multinomial */ ai_bool has_classlabels_int; /*!< if True, LinearClassifier returns classlabels int, else classlabels string */ } ai_layer_linearclassifier; /******************************************************************************/ /* Forward Functions Section */ /******************************************************************************/ /*! * @brief Decodes the LinearClassifier ML operator. * @ingroup layers_linaerclassifier * @param layer linear classifier layer */ AI_INTERNAL_API void forward_linearclassifier(ai_layer *pLayer); AI_API_DECLARE_END #endif /*LAYERS_LINEARCLASSIFIER_H*/
2,176
C
29.661971
118
0.542279
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/Middlewares/ST/AI/Inc/lite_nl_list.h
/** ****************************************************************************** * @file lite_nl_list.h * @author AST Embedded Analytics Research Platform * @brief header file of lite supported non-linearities routines ****************************************************************************** * @attention * * Copyright (c) 2022 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** @verbatim @endverbatim ****************************************************************************** */ // #define LITE_NL_ENTRY(nl_id_, nl_name_, nl_op_, nl_op_args_) /* No sentry. This is deliberate!! */ LITE_NL_ENTRY(1, abs, AI_ABS, 1) LITE_NL_ENTRY(2, acos, AI_MATH_ACOS, 1) LITE_NL_ENTRY(3, acosh, AI_MATH_ACOSH, 1) LITE_NL_ENTRY(4, asin, AI_MATH_ASIN, 1) LITE_NL_ENTRY(5, asinh, AI_MATH_ASINH, 1) LITE_NL_ENTRY(6, atan, AI_MATH_ATAN, 1) LITE_NL_ENTRY(7, atanh, AI_MATH_ATANH, 1) LITE_NL_ENTRY(8, ceil, AI_CEIL, 1) LITE_NL_ENTRY(9, cos, AI_MATH_COS, 1) LITE_NL_ENTRY(10, cosh, AI_MATH_COSH, 1) LITE_NL_ENTRY(11, erf, AI_MATH_ERF, 1) LITE_NL_ENTRY(12, exp, AI_MATH_EXP, 1) LITE_NL_ENTRY(13, floor, AI_FLOOR, 1) LITE_NL_ENTRY(14, hardmax, /**/, 0) LITE_NL_ENTRY(15, log, AI_MATH_LOG, 1) LITE_NL_ENTRY(16, logistic, AI_MATH_LOGISTIC, 1) LITE_NL_ENTRY(17, neg, AI_NEG, 1) LITE_NL_ENTRY(18, rsqrt, AI_MATH_RSQRT, 1) LITE_NL_ENTRY(19, sin, AI_MATH_SIN, 1) LITE_NL_ENTRY(20, sinh, AI_MATH_SINH, 1) LITE_NL_ENTRY(21, tan, AI_MATH_TAN, 1) LITE_NL_ENTRY(22, square, AI_MATH_SQUARE, 1) LITE_NL_ENTRY(23, reciprocal, AI_RECIPROCAL, 1) LITE_NL_ENTRY(24, round, AI_ROUND, 1) LITE_NL_ENTRY(25, sigmoid, AI_MATH_SIGMOID, 1) LITE_NL_ENTRY(26, swish, AI_MATH_SWISH, 1) LITE_NL_ENTRY(27, hard_swish, AI_MATH_HARD_SWISH, 1) LITE_NL_ENTRY(28, sign, AI_SIGN, 1) LITE_NL_ENTRY(29, sqrt, AI_MATH_SQRT, 1) // LITE_NL_ENTRY(30, softmax, /**/, 0) // for future changes // LITE_NL_ENTRY(31, softmax_zero_channel, /**/, 0) // for future changes LITE_NL_ENTRY(32, soft_plus, AI_MATH_SOFT_PLUS, 1) LITE_NL_ENTRY(33, soft_sign, AI_MATH_SOFT_SIGN, 1) LITE_NL_ENTRY(34, tanh, AI_MATH_TANH, 1) LITE_NL_ENTRY(35, prelu, /**/, 0) LITE_NL_ENTRY(36, relu, AI_MATH_RELU, 1) LITE_NL_ENTRY(37, relu_generic, /**/, 0) LITE_NL_ENTRY(101, elu, AI_MATH_ELU, 2) LITE_NL_ENTRY(102, relu_thresholded, AI_MATH_RELU_THRESHOLDED, 2) LITE_NL_ENTRY(201, clip, AI_CLAMP, 3) LITE_NL_ENTRY(202, hard_sigmoid, AI_MATH_HARD_SIGMOID, 3) LITE_NL_ENTRY(203, selu, AI_MATH_SELU, 3) #undef LITE_NL_ENTRY #undef LITE_NL_IIF_0 #undef LITE_NL_IIF_1 #undef LITE_NL_IIF_2 #undef LITE_NL_IIF_3
2,844
C
35.474359
80
0.60443
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/Middlewares/ST/AI/Inc/ai_math_helpers.h
/** ****************************************************************************** * @file ai_math_helpers.h * @author AST Embedded Analytics Research Platform * @brief Math helpers routines header file. ****************************************************************************** * @attention * * Copyright (c) 2017 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** @verbatim @endverbatim ****************************************************************************** */ #ifndef AI_MATH_HELPERS_H #define AI_MATH_HELPERS_H #include "ai_lite_math_helpers.h" //#if defined(HAS_X86) || defined(__CC_ARM) || defined(CM4) || defined(CM7) #define _AI_CONV_2D_LOOP_UNROLLING_OPTIM //#endif #define STM32_DOT_INLINE_OPTIM /* Modes for element wise integer optimized implementation */ #define AI_ELTWISE_NO_SCALAR (0) #define AI_ELTWISE_SCALAR1 (1) #define AI_ELTWISE_SCALAR2 (2) #define AI_ELTWISE_SCALAR_CH1 (3) #define AI_ELTWISE_SCALAR_CH2 (4) AI_API_DECLARE_BEGIN /*! * @typedef ai_vec4_float * @ingroup ai_datatypes_internal * @brief 32bit X 4 float (optimization for embedded MCU) */ typedef struct _ai_vec4_float { ai_float a1; ai_float a2; ai_float a3; ai_float a4; } ai_vec4_float; #define AI_VEC4_FLOAT(ptr_) \ _get_vec4_float((ai_handle)(ptr_)) AI_DECLARE_STATIC ai_vec4_float _get_vec4_float(const ai_handle fptr) { return *((const ai_vec4_float*)fptr); } #if defined(STM32_DOT_INLINE_OPTIM) AI_DECLARE_STATIC void __ai_math_dot_array( ai_float* out, const ai_float* data0, const ai_float* data1, ai_size data_size) { register ai_float sum = 0.0f; /* Temporary result storage */ /* Run the below code for Cortex-M4 and Cortex-M3 */ #if defined(_AI_CONV_2D_LOOP_UNROLLING_OPTIM) /* First part of the processing with loop unrolling. Compute 16 outputs at a time. ** a second loop below computes the remaining 1 to 15 samples. */ while (data_size >= 16u) { register ai_vec4_float ch_in_f = AI_VEC4_FLOAT(data1); register ai_vec4_float weights_in_f = AI_VEC4_FLOAT(data0); sum += weights_in_f.a1 * ch_in_f.a1; sum += weights_in_f.a2 * ch_in_f.a2; sum += weights_in_f.a3 * ch_in_f.a3; sum += weights_in_f.a4 * ch_in_f.a4; data1 += 4; data0 += 4; ch_in_f = AI_VEC4_FLOAT(data1); weights_in_f = AI_VEC4_FLOAT(data0); sum += weights_in_f.a1 * ch_in_f.a1; sum += weights_in_f.a2 * ch_in_f.a2; sum += weights_in_f.a3 * ch_in_f.a3; sum += weights_in_f.a4 * ch_in_f.a4; data1 += 4; data0 += 4; ch_in_f = AI_VEC4_FLOAT(data1); weights_in_f = AI_VEC4_FLOAT(data0); sum += weights_in_f.a1 * ch_in_f.a1; sum += weights_in_f.a2 * ch_in_f.a2; sum += weights_in_f.a3 * ch_in_f.a3; sum += weights_in_f.a4 * ch_in_f.a4; data1 += 4; data0 += 4; ch_in_f = AI_VEC4_FLOAT(data1); weights_in_f = AI_VEC4_FLOAT(data0); sum += weights_in_f.a1 * ch_in_f.a1; sum += weights_in_f.a2 * ch_in_f.a2; sum += weights_in_f.a3 * ch_in_f.a3; sum += weights_in_f.a4 * ch_in_f.a4; data1 += 4; data0 += 4; data_size -= 16u; } #else /* First part of the processing with loop unrolling. Compute 4 outputs at a time. ** a second loop below computes the remaining 1 to 3 samples. */ while (data_size >= 4u) { /* C = A[0]* B[0] + A[1]* B[1] + A[2]* B[2] + .....+ A[blockSize-1]* B[blockSize-1] */ /* Calculate dot product and then store the result in a temporary buffer */ sum += (*data0++) * (*data1++); sum += (*data0++) * (*data1++); sum += (*data0++) * (*data1++); sum += (*data0++) * (*data1++); /* Decrement the loop counter */ data_size -= 4u; } #endif while (data_size > 0u) { /* C = A[0]* B[0] + A[1]* B[1] + A[2]* B[2] + .....+ A[blockSize-1]* B[blockSize-1] */ /* Calculate dot product and then store the result in a temporary buffer. */ sum += (*data0++) * (*data1++); /* Decrement the loop counter */ data_size--; } /* Directly accumulate the result back in the destination buffer */ *out += sum; } #undef AI_MATH_DOT_ARRAY #define AI_MATH_DOT_ARRAY(dst, src0, src1, size) \ { __ai_math_dot_array(dst, src0, src1, size); } #else /* STM32_DOT_INLINE_OPTIM */ #undef AI_MATH_DOT_ARRAY #define AI_MATH_DOT_ARRAY(dst, src0, src1, size) \ { ai_math_dot_array(dst, src0, src1, size); } #endif /*! * @defgroup math_helpers Math helpers * @brief Common math functions * * Math functions are mapped to the underlying platform through those utility * functions. On x86 and ARM v7 they are mapped to the float math functions in * the C99 standard library; on MCUs they are mapped to the ARM DSP functions. */ /*! * @brief platform optimized dot product of float vectors * * Computes the dot product between vectors and adds the result to out. * @ingroup math_helpers * @param out scalar result of the dot product * @param data0 the first float vector * @param data1 the second float vector * @param data_size the size of both vectors */ AI_INTERFACE_ENTRY void ai_math_dot_array( ai_float* out, const ai_float* data0, const ai_float* data1, const ai_size data_size); /*! * @brief ErfInv a float value * @ingroup math_helpers * @param x input value * @return square root of the value */ AI_INTERFACE_ENTRY ai_float ai_math_erfinv(const ai_float x); /*! * @brief platform optimized exponential on a float value * @ingroup math_helpers * @param x input value * @return exponential of the value */ AI_INTERFACE_ENTRY ai_float ai_math_exp(const ai_float x); /*! * @brief platform logical not * @ingroup math_helpers * @param x input value * @return not of the value */ AI_INTERFACE_ENTRY ai_bool ai_logical_not(const ai_bool x); /*! * @brief platform optimized pow on a float value * @ingroup math_helpers * @param x input value * @param e input value * @return pow of the value ^ e */ AI_INTERFACE_ENTRY ai_float ai_math_pow(const ai_float x, const ai_float e); /*! * @brief platform optimized tangent on a float value * @ingroup math_helpers * @param x input value * @return hyperbolic tangent of the value */ AI_INTERFACE_ENTRY ai_float ai_math_tanh(const ai_float x); /*! * @brief platform optimized relu on a float value * @ingroup math_helpers * @param x input value * @return relu of the value ( x if x>0 else 0) */ AI_INTERFACE_ENTRY ai_float ai_math_relu(const ai_float x); /*! * @brief platform optimized parametric relu on a float value * @ingroup math_helpers * @param x input value * @param slope input value * @return parametric relu of the value */ AI_INTERFACE_ENTRY ai_float ai_math_prelu(const ai_float x, const ai_float slope); /*! * @brief platform optimized parametric sigmoid on a float value * @ingroup math_helpers * @param x input value * @return sigmoid of the value */ AI_INTERFACE_ENTRY ai_float ai_math_sigmoid(const ai_float x); /*! * @brief platform optimized parametric hard sigmoid on a float value * @ingroup math_helpers * @param x input value * @return hard sigmoid of the value */ AI_INTERFACE_ENTRY ai_float ai_math_hard_sigmoid(const ai_float x); // const ai_float alpha, const ai_float beta); /*! * @brief platform optimized parametric swish on a float value * @ingroup math_helpers * @param x input value * @return swish of the value */ AI_INTERFACE_ENTRY ai_float ai_math_swish(const ai_float x); /*! * @brief platform optimized parametric hard_swish on a float value * @ingroup math_helpers * @param x input value * @return hard_swish of the value */ AI_INTERFACE_ENTRY ai_float ai_math_hard_swish(const ai_float x); /*! * @brief platform optimized parametric sign function on a float value * @ingroup math_helpers * @param x input value * @return sign of the value */ AI_INTERFACE_ENTRY ai_float ai_math_sign(const ai_float x); /*! * @brief optimized parametric rectified linear unit on a float value * @ingroup math_helpers * @param x input value * @param slope parameter value * @return x if x is positive and x*slope otherwise */ AI_INTERFACE_ENTRY ai_float ai_fast_prelu(const ai_float x, const ai_float slope); AI_INTERFACE_ENTRY void ai_div(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_div_buffer(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_div_f32(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_div_buffer_f32(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_div_s32(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_div_buffer_s32(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_div_s16(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_div_buffer_s16(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_div_s8(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_div_buffer_s8(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_div_u32(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_div_buffer_u32(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_div_u16(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_div_buffer_u16(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_div_u8(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_div_buffer_u8(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_div_buffer_INT8(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop, const ai_handle pScale1, const ai_handle pZp1, const ai_handle pScale2, const ai_handle pZp2, const ai_handle pScaleout, const ai_handle pZpout, const ai_i32 scalar_op); AI_INTERFACE_ENTRY void ai_div_buffer_UINT8(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop, const ai_handle pScale1, const ai_handle pZp1, const ai_handle pScale2, const ai_handle pZp2, const ai_handle pScaleout, const ai_handle pZpout, const ai_i32 scalar_op); AI_INTERFACE_ENTRY void ai_bitshift_right(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_bitshift_right_buffer(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_bitshift_right_u32(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_bitshift_right_buffer_u32(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_bitshift_right_u16(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_bitshift_right_buffer_u16(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_bitshift_right_u8(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_bitshift_right_buffer_u8(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_bitshift_left(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_bitshift_left_buffer(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_bitshift_left_u32(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_bitshift_left_buffer_u32(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_bitshift_left_u16(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_bitshift_left_buffer_u16(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_bitshift_left_u8(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_bitshift_left_buffer_u8(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_floor_div(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_floor_div_buffer(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_floor_mod(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_floor_mod_buffer(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_max(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_max_buffer(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_max_f32(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_max_buffer_f32(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_max_s32(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_max_buffer_s32(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_max_s16(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_max_buffer_s16(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_max_s8(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_max_buffer_s8(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_max_u32(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_max_buffer_u32(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_max_u16(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_max_buffer_u16(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_max_u8(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_max_buffer_u8(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_max_buffer_INT8(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop, const ai_handle pScale1, const ai_handle pZp1, const ai_handle pScale2, const ai_handle pZp2, const ai_handle pScaleout, const ai_handle pZpout, const ai_i32 scalar_op); AI_INTERFACE_ENTRY void ai_max_buffer_UINT8(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop, const ai_handle pScale1, const ai_handle pZp1, const ai_handle pScale2, const ai_handle pZp2, const ai_handle pScaleout, const ai_handle pZpout, const ai_i32 scalar_op); AI_INTERFACE_ENTRY void ai_min(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_min_buffer(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_min_f32(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_min_buffer_f32(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_min_s32(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_min_buffer_s32(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_min_s16(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_min_buffer_s16(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_min_s8(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_min_buffer_s8(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_min_u32(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_min_buffer_u32(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_min_u16(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_min_buffer_u16(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_min_u8(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_min_buffer_u8(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_min_buffer_INT8(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop, const ai_handle pScale1, const ai_handle pZp1, const ai_handle pScale2, const ai_handle pZp2, const ai_handle pScaleout, const ai_handle pZpout, const ai_i32 scalar_op); AI_INTERFACE_ENTRY void ai_min_buffer_UINT8(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop, const ai_handle pScale1, const ai_handle pZp1, const ai_handle pScale2, const ai_handle pZp2, const ai_handle pScaleout, const ai_handle pZpout, const ai_i32 scalar_op); AI_INTERFACE_ENTRY void ai_mul(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_mul_buffer(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_mul_f32(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_mul_buffer_f32(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_mul_s32(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_mul_buffer_s32(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_mul_s16(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_mul_buffer_s16(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_mul_s8(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_mul_buffer_s8(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_mul_u32(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_mul_buffer_u32(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_mul_u16(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_mul_buffer_u16(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_mul_u8(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_mul_buffer_u8(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_mul_buffer_INT8(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop, const ai_handle pScale1, const ai_handle pZp1, const ai_handle pScale2, const ai_handle pZp2, const ai_handle pScaleout, const ai_handle pZpout, const ai_i32 scalar_op); AI_INTERFACE_ENTRY void ai_mul_buffer_UINT8(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop, const ai_handle pScale1, const ai_handle pZp1, const ai_handle pScale2, const ai_handle pZp2, const ai_handle pScaleout, const ai_handle pZpout, const ai_i32 scalar_op); AI_INTERFACE_ENTRY void ai_pow(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_pow_buffer(ai_handle out, const ai_handle b, const ai_handle e, const ai_size loop); AI_INTERFACE_ENTRY void ai_sub(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_sub_buffer(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_sub_f32(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_sub_buffer_f32(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_sub_s32(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_sub_buffer_s32(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_sub_s16(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_sub_buffer_s16(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_sub_s8(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_sub_buffer_s8(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_sub_u32(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_sub_buffer_u32(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_sub_u16(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_sub_buffer_u16(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_sub_u8(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_sub_buffer_u8(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_sub_buffer_INT8(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop, const ai_handle pScale1, const ai_handle pZp1, const ai_handle pScale2, const ai_handle pZp2, const ai_handle pScaleout, const ai_handle pZpout, const ai_i32 scalar_op); AI_INTERFACE_ENTRY void ai_sub_buffer_UINT8(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop, const ai_handle pScale1, const ai_handle pZp1, const ai_handle pScale2, const ai_handle pZp2, const ai_handle pScaleout, const ai_handle pZpout, const ai_i32 scalar_op); AI_INTERFACE_ENTRY void ai_sum(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_sum_buffer(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_sum_f32(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_sum_buffer_f32(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_sum_s32(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_sum_buffer_s32(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_sum_s16(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_sum_buffer_s16(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_sum_s8(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_sum_buffer_s8(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_sum_u32(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_sum_buffer_u32(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_sum_u16(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_sum_buffer_u16(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_sum_u8(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_sum_buffer_u8(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_sum_buffer_INT8(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop, const ai_handle pScale1, const ai_handle pZp1, const ai_handle pScale2, const ai_handle pZp2, const ai_handle pScaleout, const ai_handle pZpout, const ai_i32 scalar_op); AI_INTERFACE_ENTRY void ai_sum_buffer_UINT8(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop, const ai_handle pScale1, const ai_handle pZp1, const ai_handle pScale2, const ai_handle pZp2, const ai_handle pScaleout, const ai_handle pZpout, const ai_i32 scalar_op); AI_INTERFACE_ENTRY void ai_and(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_and_buffer(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_or(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_or_buffer(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_xor(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_xor_buffer(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_greater(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_greater_buffer(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_greater_f32(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_greater_buffer_f32(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_greater_s32(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_greater_buffer_s32(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_greater_s16(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_greater_buffer_s16(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_greater_s8(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_greater_buffer_s8(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_greater_u32(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_greater_buffer_u32(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_greater_u16(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_greater_buffer_u16(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_greater_u8(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_greater_buffer_u8(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_greater_or_equal(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_greater_or_equal_buffer(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_greater_or_equal_f32(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_greater_or_equal_buffer_f32(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_greater_or_equal_s32(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_greater_or_equal_buffer_s32(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_greater_or_equal_s16(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_greater_or_equal_buffer_s16(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_greater_or_equal_s8(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_greater_or_equal_buffer_s8(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_greater_or_equal_u32(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_greater_or_equal_buffer_u32(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_greater_or_equal_u16(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_greater_or_equal_buffer_u16(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_greater_or_equal_u8(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_greater_or_equal_buffer_u8(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_less(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_less_buffer(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_less_f32(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_less_buffer_f32(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_less_s32(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_less_buffer_s32(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_less_s16(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_less_buffer_s16(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_less_s8(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_less_buffer_s8(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_less_u32(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_less_buffer_u32(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_less_u16(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_less_buffer_u16(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_less_u8(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_less_buffer_u8(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_less_or_equal(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_less_or_equal_buffer(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_less_or_equal_f32(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_less_or_equal_buffer_f32(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_less_or_equal_s32(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_less_or_equal_buffer_s32(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_less_or_equal_s16(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_less_or_equal_buffer_s16(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_less_or_equal_s8(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_less_or_equal_buffer_s8(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_less_or_equal_u32(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_less_or_equal_buffer_u32(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_less_or_equal_u16(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_less_or_equal_buffer_u16(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_less_or_equal_u8(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_less_or_equal_buffer_u8(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_equal(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_equal_buffer(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_equal_f32(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_equal_buffer_f32(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_equal_s32(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_equal_buffer_s32(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_equal_s16(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_equal_buffer_s16(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_equal_s8(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_equal_buffer_s8(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_equal_u32(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_equal_buffer_u32(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_equal_u16(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_equal_buffer_u16(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_equal_u8(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_equal_buffer_u8(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_INTERFACE_ENTRY void ai_squared_diff(ai_handle out, const ai_handle a, const ai_handle b); AI_INTERFACE_ENTRY void ai_squared_diff_buffer(ai_handle out, const ai_handle a, const ai_handle b, const ai_size loop); AI_API_DECLARE_END #endif /* AI_MATH_HELPERS_H */
34,676
C
61.820652
137
0.706252
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/Middlewares/ST/AI/Inc/layers_ml_treeensembleregressor.h
/** ****************************************************************************** * @file layers_svmregressor.h * @author AIS * @brief header file of AI platform SVM Regressor datatypes ****************************************************************************** * @attention * * Copyright (c) 2021 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** @verbatim @endverbatim ****************************************************************************** */ #ifndef LAYERS_TREE_ENSEMBLE_REGRESSOR_H #define LAYERS_TREE_ENSEMBLE_REGRESSOR_H #pragma once #include "layers_common.h" #include "layers_ml_treeensembleclassifier.h" #include "layers_nl.h" /*! * @defgroup layers_svmreg Layers Definitions * @brief definition * */ AI_API_DECLARE_BEGIN typedef AI_ALIGNED_TYPE(struct, 4) ai_layer_tree_ensemble_regressor_ { AI_LAYER_COMMON_FIELDS_DECLARE func_nl nl_func; uint8_t all_weights_are_positive; ai_float nodes_values_offset; ai_float nodes_values_scale; ai_float target_weights_offset; ai_float target_weights_scale; } ai_layer_tree_ensemble_regressor; /******************************************************************************/ /* Forward Functions Section */ /******************************************************************************/ /*! * @brief Decodes the TreeEnsembleRegressor ML operator. * @ingroup layers_svmreg * @param layer tree ensemble regressor layer */ AI_INTERNAL_API void forward_tree_ensemble_regressor(ai_layer *pLayer); AI_API_DECLARE_END #endif /*LAYERS_SVMREGRESSOR_H*/
1,923
C
29.0625
80
0.520021
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/Middlewares/ST/AI/Inc/lite_pool_f32.h
/** ****************************************************************************** * @file lite_maxpool_dqnn.h * @author AIS * @brief header file of AI platform lite maxpool kernel datatypes ****************************************************************************** * @attention * * Copyright (c) 2021 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** @verbatim @endverbatim ****************************************************************************** */ #ifndef LITE_POOL_F32_H #define LITE_POOL_F32_H #include "ai_lite_interface.h" #define FUNC_POOL(handle) \ ((func_pool)(handle)) /*! * @typedef (*func_pool) * @ingroup layers_pool * @brief Fuction pointer for generic pooling transform * this function pointer abstracts a generic pooling layer. * see @ref pool_func_ap_array_f32 as examples */ typedef void (*func_pool)(ai_float* in, const ai_u16 dim_im_in_x, const ai_u16 dim_im_in_y, const ai_u16 ch_im_in, const ai_u16 dim_kernel_x, const ai_u16 dim_kernel_y, const ai_u16 padding_x, const ai_u16 padding_y, const ai_u16 stride_x, const ai_u16 stride_y, const ai_u16 dim_im_out_x, const ai_u16 dim_im_out_y, ai_float* out); /******************************************************************************/ /** Conv2d Functions Section **/ /******************************************************************************/ AI_INTERNAL_API void pool_func_mp_array_f32(ai_float* pData_in, const ai_u16 dim_im_in_x, const ai_u16 dim_im_in_y, const ai_u16 ch_im_in, const ai_u16 dim_kernel_x, const ai_u16 dim_kernel_y, const ai_u16 padding_x, const ai_u16 padding_y, const ai_u16 stride_x, const ai_u16 stride_y, const ai_u16 dim_im_out_x, const ai_u16 dim_im_out_y, ai_float* pData_out); AI_INTERNAL_API void pool_func_ap_array_f32(ai_float *pData_in, const ai_u16 dim_im_in_x, const ai_u16 dim_im_in_y, const ai_u16 ch_im_in, const ai_u16 dim_kernel_x, const ai_u16 dim_kernel_y, const ai_u16 padding_x, const ai_u16 padding_y, const ai_u16 stride_x, const ai_u16 stride_y, const ai_u16 dim_im_out_x, const ai_u16 dim_im_out_y, ai_float *pData_out); #endif // LITE_POOL_F32_H_
2,936
C
39.232876
80
0.466962
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/Middlewares/ST/AI/Inc/lite_gru_f32.h
#ifndef LITE_GRU_F32_H #define LITE_GRU_F32_H #pragma once #include "ai_lite_interface.h" /*! * @brief Forward function for a stateless GRU (gate recurrent unit) layer with * signed float input, signed float output, and float parameters. * @ingroup lite_gru_f32 * @param output The pointer to output buffer. * @param input The pointer to input buffer. * @param gru_kernel The pointer to gru kernel param. * @param gru_recurrent The pointer to gru recurrent param. * @param gru_bias The pointer to bias. * @param gru_scratch The pointer to GRU scratch. * @param n_units The number of GRU cells (dimensionality of output space). * @param n_timesteps The number of timesteps of the input sequence. * @param n_features The number of features of the input sequence. * @param activation_nl The activation function used to update memory state. * @param recurrent_nl The activation function to use for the recurrent step. * @param return_seq If True, returns the full output sequence, else only the last output. * @param go_backwards If True, process the input sequence backwards. * @param reverse_seq If True, reverse the input sequence * @param reset_after Whether to apply reset gate after (True) or before (False) matmul. * @param activation_param The parameters for activation_nl (can be NULL) * @param recurrent_param The parameters for recurrent_nl (can be NULL) */ LITE_API_ENTRY void forward_lite_gru_if32of32wf32( ai_float* output, const ai_float* input, const ai_float* gru_kernel, const ai_float* gru_recurrent, const ai_float* gru_bias, ai_float* gru_scratch, const ai_u32 n_units, const ai_size n_timesteps, const ai_size n_features, ai_handle activation_nl, ai_handle recurrent_nl, ai_bool return_seq, ai_bool go_backwards, ai_bool reverse_seq, ai_bool reset_after, const ai_float* activation_param, const ai_float* recurrent_param); #endif /* LITE_GRU_F32_H */
1,910
C
46.774999
90
0.746597
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/Middlewares/ST/AI/Inc/lite_dense_is1.h
#ifndef _LITE_DENSE_IS1_H #define _LITE_DENSE_IS1_H #pragma once #include "ai_lite_interface.h" /*! * @brief Forward function for a dense layer with signed binary input, * signed float output, and float weights. * @ingroup lite_dense_is1 * @param output The pointer to output buffer. * @param input The pointer to input buffer. * @param weights The pointer to weights. * @param bias The pointer to bias (NULL if not available). * @param scratch The pointer to the scratch buffer (unused). * @param n_channel_in The number of channels of the input. * @param n_channel_ouy The number of channels of the output, i.e., * the number of dense hidden neurons. */ LITE_API_ENTRY void forward_lite_dense_is1of32wf32( ai_float *output, const ai_pbits *input, const ai_float *weights, const ai_float *bias, ai_float *scratch, const ai_u32 n_channel_in, const ai_u32 n_channel_out ); /*! * @brief Forward function for a dense layer with signed binary input, * signed float output, and float weights. * The BN is fused, i.e., the layer requires weights, scale, and offset, where * weights are those of the dense layer, scale is that of the BN, and the offset * corresponds to dense bias * bn scale + bn offset. If the parameters do not * agree with such convention, the behavior is undefined. * @ingroup lite_dense_is1 * @param output The pointer to output buffer. * @param input The pointer to input buffer. * @param weights The pointer to weights. * @param scale The pointer to scale. * @param offset The pointer to offset. * @param scratch The pointer to the scratch buffer (unused). * @param n_channel_in The number of channels of the input. * @param n_channel_ouy The number of channels of the output, i.e., * the number of dense hidden neurons. */ LITE_API_ENTRY void forward_lite_dense_is1of32wf32_bn( ai_float *output, const ai_pbits *input, const ai_float *weights, const ai_float *scale, const ai_float *offset, ai_float *scratch, const ai_u32 n_channel_in, const ai_u32 n_channel_out ); #endif /*_LITE_DENSE_IS1_H*/
2,078
C
36.799999
80
0.720404
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/Middlewares/ST/AI/Inc/lite_nl_generic_float.h
#ifndef LITE_NL_GENERIC_FLOAT_H #define LITE_NL_GENERIC_FLOAT_H #pragma once #include "ai_lite_interface.h" #define LITE_NL_ENTRY(nl_id_, nl_name_, nl_op_, nl_op_args_) \ /** \ * @brief lite function for a templated non-linearity nl_op_. \ * @ingroup lite_nl_generic_float \ * @param out_ptr The pointer to output buffer. \ * @param in_ptr The pointer to input buffer. \ * @param in_size. The size of the input. \ * @param params opaque handler to optional NL params (not used). \ */ \ LITE_API_ENTRY \ void forward_lite_nl_ ## nl_name_ ## _if32of32( \ ai_handle out_ptr, const ai_handle in_ptr, const ai_i32 in_size, const ai_handle params); #include "lite_nl_list.h" /** * @brief lite function for a float softmax non-linearity where the softmax is applied per channel. * @ingroup lite_nl_generic_float * @param output The pointer to output buffer. * @param input The pointer to input buffer. * @param in_size. The size of the input. * @param channel_size The nsize of each channel. * @param in_channel_step * @param out_channel_step */ LITE_API_ENTRY void forward_lite_nl_softmax_if32of32( ai_handle out_ptr, const ai_handle in_ptr, const ai_i32 in_size, const ai_size ch_size, const ai_i32 in_ch_step, const ai_i32 out_ch_step); /** * @brief lite function for a float softmax zero channel non-linearity where the softmax is applied per channel. * @ingroup lite_nl_generic_float * @param output The pointer to output buffer. * @param input The pointer to input buffer. * @param in_size. The size of the input. * @param channel_size The nsize of each channel. * @param in_channel_step * @param out_channel_step */ LITE_API_ENTRY void forward_lite_nl_softmax_zero_channel_if32of32( ai_handle out_ptr, const ai_handle in_ptr, const ai_i32 in_size, const ai_size ch_size, const ai_i32 in_ch_step, const ai_i32 out_ch_step); #endif /* LITE_NL_GENERIC_FLOAT_H */
1,907
C
33.071428
112
0.708967
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/Middlewares/ST/AI/Inc/lite_pw.h
/** ****************************************************************************** * @file lite_pw.h * @author AIS * @brief header file of AI platform lite pw kernel datatypes ****************************************************************************** * @attention * * Copyright (c) 2021 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** @verbatim @endverbatim ****************************************************************************** */ #ifndef LITE_PW_H #define LITE_PW_H #pragma once #include "ai_lite_interface.h" /******************************************************************************/ /* Forward Functions Section */ /******************************************************************************/ /*! * @brief Handles pw convolutions generic case * @ingroup lite_pw */ LITE_API_ENTRY void forward_lite_pw_sssa8_ch(const ai_i8 *pData_in, const ai_u16 width_in, const ai_u16 height_in, const ai_u16 n_channel_in, const ai_i8 *pWeights, const ai_u16 n_channel_out, const ai_i32 *pBias, const ai_i8 in_zeropoint, const ai_i8 out_zeropoint, const ai_layer_format_type out_ch_format, ai_i8 *pData_out, ai_u32 height_loop_cnt, ai_u16 weights_prefetch_enabled, ai_i32 scratch_size, ai_i16 *pBuffer_a); #endif /*LITE_PW_H*/
1,973
C
34.249999
80
0.387228
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/Middlewares/ST/AI/Inc/ai_lite_inspect.h
/** ****************************************************************************** * @file ai_lite_inspect.h * @author AST Embedded Analytics Research Platform * @brief Definitions and implementations of runtime-lite inspection routines ****************************************************************************** * @attention * * Copyright (c) 2021 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** @verbatim @endverbatim ****************************************************************************** */ #ifndef AI_LITE_INSPECT_H #define AI_LITE_INSPECT_H #pragma once #include "ai_platform.h" //#define HAS_LITE_INSPECT AI_API_DECLARE_BEGIN /* Types needed by inspect callback signature */ typedef ai_i32 ai_data_format; typedef ai_i32 ai_data_id; /* Lite inspect callback definition */ typedef void (*ai_lite_inspect_cb)( const ai_handle cookie, const ai_data_id node_id, const ai_handle data, const ai_size data_size, const ai_data_format data_fmt, const ai_data_id data_id); #ifdef HAS_LITE_INSPECT #define LITE_INSPECT_CB(_node_id, _data, _data_size, _data_fmt, _data_id) { \ if (graph->cb) { \ graph->cb(graph->cb_cookie, \ (ai_data_id)(_node_id), (ai_handle)(_data), (ai_size)(_data_size), \ (ai_data_format)(_data_fmt), (ai_data_id)(_data_id)); \ } \ } #else #define LITE_INSPECT_CB(_node_id, _data, _data_size, _data_fmt, _data_id) { \ do { /* LITE_INSPECT_CB() */ } while (0); \ } #endif /* HAS_LITE_INSPECT */ AI_API_DECLARE_END #endif /* AI_LITE_INSPECT_H */
1,858
C
28.507936
82
0.545748
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/Middlewares/ST/AI/Inc/lite_maxpool_dqnn.h
/** ****************************************************************************** * @file lite_maxpool_dqnn.h * @author AIS * @brief header file of AI platform lite maxpool kernel datatypes ****************************************************************************** * @attention * * Copyright (c) 2021 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** @verbatim @endverbatim ****************************************************************************** */ #ifndef LITE_MAXPOOL_DQNN_H #define LITE_MAXPOOL_DQNN_H #pragma once #include "ai_lite_interface.h" /******************************************************************************/ /* Forward Functions Section */ /******************************************************************************/ /*! * @brief Handles maxpool with binary input and binary output - Lite I/F * @ingroup lite_maxpool_dqnn */ LITE_API_ENTRY void forward_lite_maxpool_is1os1(const ai_u32 *pDataIn_init, ai_u32 *pDataOut_init, const ai_i32 width_in, const ai_i32 width_out, const ai_i32 height_in, const ai_i32 height_out, const ai_u32 n_channel_in, const ai_u32 n_channel_out, const ai_i32 pool_width, const ai_i32 pool_height, const ai_i32 pool_pad_x, const ai_i32 pool_pad_y, const ai_i32 pool_stride_x, const ai_i32 pool_stride_y, const ai_u32 pool_pad_value, ai_float *pScratch_32); /*! * @brief Handles maxpool with 8 bits signed input and output with a positive scale of the input- Lite I/F * @ingroup lite_maxpool_dqnn */ LITE_API_ENTRY void forward_lite_maxpool_is8os8_scalepos(const ai_i8 *pDataIn, ai_i8 *pDataOut, const ai_u16 dim_im_in_x, const ai_u16 dim_im_in_y, const ai_u16 ch_im_in, const ai_u16 dim_kernel_x, const ai_u16 dim_kernel_y, const ai_u16 padding_x, const ai_u16 padding_y, const ai_u16 stride_x, const ai_u16 stride_y, const ai_u16 dim_im_out_x, const ai_u16 dim_im_out_y, const ai_float InOut_ScaleRatio, const ai_i8 In_ZeroPoint, const ai_i8 Out_ZeroPoint); /*! * @brief Handles maxpool with 8 bits signed input and output with a negative scale of the input- Lite I/F * @ingroup lite_maxpool_dqnn */ LITE_API_ENTRY void forward_lite_maxpool_is8os8_scaleneg(const ai_i8 *pDataIn, ai_i8 *pDataOut, const ai_u16 dim_im_in_x, const ai_u16 dim_im_in_y, const ai_u16 ch_im_in, const ai_u16 dim_kernel_x, const ai_u16 dim_kernel_y, const ai_u16 padding_x, const ai_u16 padding_y, const ai_u16 stride_x, const ai_u16 stride_y, const ai_u16 dim_im_out_x, const ai_u16 dim_im_out_y, const ai_float InOut_ScaleRatio, const ai_i8 In_ZeroPoint, const ai_i8 Out_ZeroPoint); /*! * @brief Handles maxpool with 8 bits unsigned input and output with a positive scale of the input- Lite I/F * @ingroup lite_maxpool_dqnn */ LITE_API_ENTRY void forward_lite_maxpool_iu8ou8_scalepos(const ai_u8 *pDataIn, ai_u8 *pDataOut, const ai_u16 dim_im_in_x, const ai_u16 dim_im_in_y, const ai_u16 ch_im_in, const ai_u16 dim_kernel_x, const ai_u16 dim_kernel_y, const ai_u16 padding_x, const ai_u16 padding_y, const ai_u16 stride_x, const ai_u16 stride_y, const ai_u16 dim_im_out_x, const ai_u16 dim_im_out_y, const ai_float InOut_ScaleRatio, const ai_u8 In_ZeroPoint, const ai_u8 Out_ZeroPoint); /*! * @brief Handles maxpool with 8 bits unsigned input and output with a negative scale of the input- Lite I/F * @ingroup lite_maxpool_dqnn */ LITE_API_ENTRY void forward_lite_maxpool_iu8ou8_scaleneg(const ai_u8 *pDataIn, ai_u8 *pDataOut, const ai_u16 dim_im_in_x, const ai_u16 dim_im_in_y, const ai_u16 ch_im_in, const ai_u16 dim_kernel_x, const ai_u16 dim_kernel_y, const ai_u16 padding_x, const ai_u16 padding_y, const ai_u16 stride_x, const ai_u16 stride_y, const ai_u16 dim_im_out_x, const ai_u16 dim_im_out_y, const ai_float InOut_ScaleRatio, const ai_u8 In_ZeroPoint, const ai_u8 Out_ZeroPoint); /*! * @brief Handles maxpool with 16 bits signed input and output with a positive scale of the input- Lite I/F * @ingroup lite_maxpool_dqnn */ LITE_API_ENTRY void forward_lite_maxpool_is16os16_scalepos(const ai_i16 *pApInput, ai_i16 *pApOutput, const ai_u16 dim_im_in_x, const ai_u16 dim_im_in_y, const ai_u16 ch_im_in, const ai_u16 dim_kernel_x, const ai_u16 dim_kernel_y, const ai_u16 padding_x, const ai_u16 padding_y, const ai_u16 stride_x, const ai_u16 stride_y, const ai_u16 dim_im_out_x, const ai_u16 dim_im_out_y, const ai_float InOut_ScaleRatio, const ai_i16 In_ZeroPoint, const ai_i16 Out_ZeroPoint); /*! * @brief Handles maxpool with 16 bits unsigned input and output with a positive scale of the input- Lite I/F * @ingroup lite_maxpool_dqnn */ LITE_API_ENTRY void forward_lite_maxpool_iu16ou16_scalepos(const ai_u16 *pApInput, ai_u16 *pApOutput, const ai_u16 dim_im_in_x, const ai_u16 dim_im_in_y, const ai_u16 ch_im_in, const ai_u16 dim_kernel_x, const ai_u16 dim_kernel_y, const ai_u16 padding_x, const ai_u16 padding_y, const ai_u16 stride_x, const ai_u16 stride_y, const ai_u16 dim_im_out_x, const ai_u16 dim_im_out_y, const ai_float InOut_ScaleRatio, const ai_u16 In_ZeroPoint, const ai_u16 Out_ZeroPoint); #endif /*LITE_MAXPOOL_DQNN_H*/
8,459
C
51.546584
109
0.422154
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/Middlewares/ST/AI/Inc/layers_common.h
/** ****************************************************************************** * @file layers_common.h * @author AST Embedded Analytics Research Platform * @brief header file of AI platform layers datatypes ****************************************************************************** * @attention * * Copyright (c) 2017 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** @verbatim @endverbatim ****************************************************************************** */ #ifndef LAYERS_COMMON_H #define LAYERS_COMMON_H #pragma once // #include <stdlib.h> #ifdef USE_CYCLE_MEASUREMENTS #include "layers_cycles_estimation.h" #endif #include "ai_platform.h" #include "ai_common_config.h" #include "core_common.h" /* optimizations */ #define AI_OPTIM_DICT8_DOT_ARRAY_F32 (1) #define AI_OPTIM_DICT8_DTCM (1) #define AI_OPTIM_FUNC_MP_ARRAY_F32 (0) #define AI_LAYER_OBJ(obj_) \ ((ai_layer_base*)(obj_)) #define AI_LAYER_FUNC(func_) \ ((layer_func)(func_)) #define AI_LAYER_TYPE(type_) \ ( (ai_layer_type)((ai_u32)(type_)&0xFFFF) ) #define AI_LAYER_TYPE_ENTRY(type_) \ AI_CONCAT(AI_CONCAT(AI_LAYER_, type_), _TYPE) #define AI_LAYER_TYPE_NAME(type_) \ ai_layer_type_name(AI_LAYER_TYPE(type_)) #if (AI_TOOLS_API_VERSION <= AI_TOOLS_API_VERSION_1_3) #pragma message ("Including deprecated AI_LAYER_OBJ_INIT, AI_LAYER_OBJ_DECLARE") AI_DEPRECATED #define AI_LAYER_OBJ_INIT(type_, id_, network_, \ next_, forward_, ...) \ { \ AI_NODE_COMMON_INIT(AI_CONCAT(AI_LAYER_, type_), id_, 0x0, \ NULL, network_, next_, forward_), \ ## __VA_ARGS__ \ } AI_DEPRECATED #define AI_LAYER_OBJ_DECLARE(varname_, id_, type_, struct_, forward_func_, \ network_, next_, attr_, ...) \ AI_ALIGNED(4) \ attr_ AI_CONCAT(ai_layer_, struct_) varname_ = \ AI_LAYER_OBJ_INIT(type_, id_, network_, \ next_, forward_func_, \ ## __VA_ARGS__); #else #define AI_LAYER_OBJ_INIT(type_, id_, flags_, klass_, network_, \ next_, forward_, tensors_, ...) \ { \ AI_NODE_COMMON_INIT(AI_CONCAT(AI_LAYER_, type_), id_, flags_, \ klass_, network_, next_, forward_), \ .tensors = (tensors_), \ ## __VA_ARGS__ \ } #define AI_LAYER_OBJ_DECLARE( \ varname_, id_, \ type_, flags_, klass_obj_, \ struct_, forward_func_, \ tensors_chain_, \ network_, next_, attr_, ...) \ AI_ALIGNED(4) \ attr_ AI_CONCAT(ai_layer_, struct_) varname_ = \ AI_LAYER_OBJ_INIT(type_, id_, flags_, klass_obj_, network_, \ next_, forward_func_, tensors_chain_, ## __VA_ARGS__); #endif /* AI_TOOLS_API_VERSION_1_3 */ #ifdef HAS_AI_ASSERT #define AI_LAYER_IO_GET(layer_, in_, out_) \ ASSERT_LAYER_SANITY(layer_) \ const ai_tensor* in_ = GET_TENSOR_IN((layer_)->tensors, 0); \ ai_tensor* out_ = GET_TENSOR_OUT((layer_)->tensors, 0); \ ASSERT_TENSOR_DATA_SANITY(in_) \ ASSERT_TENSOR_DATA_SANITY(out_) #define AI_LAYER_TENSOR_LIST_IO_GET(layer_, tlist_in_, tlist_out_) \ ASSERT_LAYER_SANITY(layer_) \ const ai_tensor_list* tlist_in_ = GET_TENSOR_LIST_IN((layer_)->tensors); \ ai_tensor_list* tlist_out_ = GET_TENSOR_LIST_OUT((layer_)->tensors); \ ASSERT_TENSOR_LIST_SANITY(tlist_in_) \ ASSERT_TENSOR_LIST_SANITY(tlist_out_) #define AI_LAYER_WEIGHTS_GET(layer_, weights_, bias_) \ const ai_tensor* weights_ = GET_TENSOR_WEIGHTS((layer_)->tensors, 0); \ const ai_tensor* bias_ = (GET_TENSOR_LIST_SIZE(GET_TENSOR_LIST_WEIGTHS((layer_)->tensors))>1) \ ? GET_TENSOR_WEIGHTS((layer_)->tensors, 1) \ : NULL; \ ASSERT_TENSOR_DATA_SANITY(weights_) \ if (bias_) { ASSERT_TENSOR_DATA_SANITY(bias_) } #else #define AI_LAYER_IO_GET(layer_, in_, out_) \ const ai_tensor* in_ = GET_TENSOR_IN((layer_)->tensors, 0); \ ai_tensor* out_ = GET_TENSOR_OUT((layer_)->tensors, 0); #define AI_LAYER_TENSOR_LIST_IO_GET(layer_, tlist_in_, tlist_out_) \ const ai_tensor_list* tlist_in_ = GET_TENSOR_LIST_IN((layer_)->tensors); \ ai_tensor_list* tlist_out_ = GET_TENSOR_LIST_OUT((layer_)->tensors); #define AI_LAYER_WEIGHTS_GET(layer_, weights_, bias_) \ const ai_tensor* weights_ = GET_TENSOR_WEIGHTS((layer_)->tensors, 0); \ const ai_tensor* bias_ = (GET_TENSOR_LIST_SIZE(GET_TENSOR_LIST_WEIGTHS((layer_)->tensors))>1) \ ? GET_TENSOR_WEIGHTS((layer_)->tensors, 1) \ : NULL; \ #endif /*HAS_AI_ASSERT*/ AI_API_DECLARE_BEGIN /*! * @defgroup layers_common Layers Common * @brief Implementation of the common layers datastructures * This header enumerates the layers specific definition implemented in the * library toghether with the macros and datatypes used to manipulate them. */ /*! * @typedef (*func_copy_tensor) * @ingroup layers_common * @brief Fuction pointer for generic tensor copy routines * this function pointer abstracts a generic tensor copy routine. */ typedef ai_bool (*func_copy_tensor)(ai_tensor* dst, const ai_tensor* src); /*! * @enum ai_layer_type * @ingroup layers_common * @brief ai_tools supported layers type id */ typedef enum { #define LAYER_ENTRY(type_, id_, struct_, forward_func_, init_func_, destroy_func_) \ AI_LAYER_TYPE_ENTRY(type_) = id_, #include "layers_list.h" } ai_layer_type; #define AI_LAYER_COMMON_FIELDS_DECLARE \ AI_NODE_COMMON_FIELDS_DECLARE #define AI_LAYER_STATEFUL_FIELDS_DECLARE \ AI_NODE_STATEFUL_FIELDS_DECLARE /*! * @typedef void (*layer_func)(struct ai_layer_* layer) * @ingroup layers_common * @brief Callback signatures for all layers forward functions */ typedef node_func layer_func; /*! * @struct ai_layer_base * @ingroup layers_common * @brief Structure encoding a base layer in the network * */ typedef ai_node ai_layer_base; /*! * @struct ai_layer_stateful * @ingroup layers_common * @brief Structure encoding a stateful layer in the network * */ typedef ai_node_stateful ai_layer_stateful; /*! * @brief Check the custom network types against the internally compiled ones * Helper function to check if the private APIs where compiled with a different * `datatypes_network.h` than the one provided to the caller. * @ingroup layers_common * @param signatures list of type sizes signatures (first element is the number of types) * @return false if there is a type size mismatch */ AI_INTERNAL_API ai_bool ai_check_custom_types(const ai_custom_type_signature* signatures); /*! * @brief Helper API to retrieve a human readable layer type from enum * @ingroup layers_common * @param type in type of layer * @return string defining the type of the layer */ AI_INTERNAL_API const char* ai_layer_type_name(const ai_layer_type type); /*! * @brief Helper API to check if a node is a valid layer type * @ingroup layers_common * @param type in type of layer * @return true if the layer is one of the ones listed in the enum, * false otherwise */ AI_INTERNAL_API ai_bool ai_layer_type_is_valid(const ai_layer_type type); #ifdef HAS_AI_ASSERT /*! * @brief chack scratch size computed with actual scratch buffer size * @ingroup layers * @param layer_type the layer type * @param fmt buffers format * @param filt_width filter width (when relevant) * @param filt_height filter height (when relevant) * @param n_channel_in the number of channels in * @param n_channel_out the number of channels out * @param is_pointwise is pointwise convulation (conv2d) * @param is_rgb is rgb convolution (conv2d) * @param is depthwise is depthwise convolution (conv2d) * @param is_ch_wise has weights per channel * @param is_sssa is signed * @param p_tensor_scratch the scratch tensor * @param p_function_name the name of the function * @param line_nb the the line of the function */ AI_INTERNAL_API ai_size ai_layer_get_scratch_size( ai_layer_type layer_type, ai_array_format fmt, ai_size filt_width, ai_size filt_height, ai_u16 n_channel_in, ai_u16 n_channel_out, ai_bool is_pointwise, ai_bool is_rgb, ai_bool is_depthwise, ai_bool is_ch1st, ai_bool is_ch_wise, ai_bool is_sss); /*! * @brief chack scratch size computed with actual scratch buffer size * @ingroup layers * @param layer_type the layer type * @param fmt buffers format * @param filt_width filter width (when relevant) * @param filt_height filter height (when relevant) * @param n_channel_in the number of channels in * @param n_channel_out the number of channels out * @param is_pointwise is pointwise convulation (conv2d) * @param is_rgb is rgb convolution (conv2d) * @param is depthwise is depthwise convolution (conv2d) * @param is_ch_wise has weights per channel * @param is_sssa is signed * @param p_tensor_scratch the scratch tensor * @param p_function_name the name of the function * @param line_nb the the line of the function */ AI_INTERNAL_API void ai_layer_check_scratch_size( ai_layer_type layer_type, ai_array_format fmt, ai_size filt_width, ai_size filt_height, ai_u16 n_channel_in, ai_u16 n_channel_out, ai_bool is_pointwise, ai_bool is_rgb, ai_bool is_depthwise, ai_bool is_ch1st, ai_bool is_ch_wise, ai_bool is_sssa, ai_tensor *p_tensor_scratch, const char *p_function_name, int line_nb); #define CHECK_SCRATCH_BUFFER_SIZE( layer_type, fmt, \ filt_width, filt_height, \ n_channel_in, n_channel_out, \ is_pointwise, is_rgb, \ is_depthwise, is_ch1st, is_ch_wise, \ is_sssa_ch, p_tensor_scratch) \ ai_layer_check_scratch_size(layer_type, fmt, \ filt_width, filt_height, \ n_channel_in, n_channel_out, \ is_pointwise, is_rgb, \ is_depthwise, is_ch1st, is_ch_wise, \ is_sssa_ch, p_tensor_scratch,\ __FUNCTION__, __LINE__); #endif AI_API_DECLARE_END #endif /*LAYERS_COMMON_H*/
10,739
C
34.681063
99
0.607133
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/MCSDK_v6.2.0-Full/MotorControl/MCSDK/MCLib/Any/Src/potentiometer.c
/** ****************************************************************************** * @file potentiometer.c * @author Motor Control SDK Team, ST Microelectronics * @brief This file provides the functions that implement the potentiometer * component of the Motor Control SDK. * ****************************************************************************** * @attention * * <h2><center>&copy; Copyright (c) 2023 STMicroelectronics. * All rights reserved.</center></h2> * * This software component is licensed by ST under Ultimate Liberty license * SLA0044, the "License"; You may not use this file except in compliance with * the License. You may obtain a copy of the License at: * www.st.com/SLA0044 * ****************************************************************************** * @ingroup Potentiometer */ /* Includes ------------------------------------------------------------------*/ #include "potentiometer.h" /** @addtogroup MCSDK * @{ */ /** @defgroup Potentiometer Linear Potentiometer * @brief Linear Potentiometer reading component of the Motor Control SDK * * The Potentiometer component aims at measuring the voltage set on a linear * potentiometer. It uses the services of the @ref RCM component to get measures * from an ADC channel connected to the potentiometer. * * The component is designed to take potentiometer measures periodically. It * computes a moving average on a number of these measures in order to reduce * the reading noise. This moving average is the potentiometer value produced * by the component. It is retrieved by calling the POT_GetValue() function. * * The measures are taken by the POT_TakeMeasurement() function. This * function must then be called periodically. * * At startup or after a call to the POT_Clear() function, a valid average * potentiometer value is not immediately available. Enough measures need to be * taken so that the moving average can be computed. The POT_ValidValueAvailable() * function can be used to check whether a valid potentiometer value is returned * when calling the POT_GetValue() function. * * The state of a Potentiometer component is maintained in a Potentiometer_Handle_t * structure. To use the Potentiometer component, a Potentiometer_Handle_t structure * needs to be instanciated and initialized. The initialization is performed thanks * to the POT_Init() function. Prior to calling this function, some of the fields of * this structure need to be given a value. See the Potentiometer_Handle_t and below * for more details on this. * * The Potentiometer component accumulates a number of measures on which it * computes a moving average. For performance reasons, this number must be a * power of two. This is set in the Potentiometer_Handle_t::LPFilterBandwidthPOW2 * field of the potentiometer handle structure. * * @note In the current version of the Potentiometer component, the periodic ADC * measures **must** be performed on the Medium Frequency Task. This can be done by using * the MC_APP_PostMediumFrequencyHook_M1() function of the @ref MCAppHooks service * for instance. * * @{ */ /* Public functions ----------------------------------------------------------*/ /** * @brief Initializes a Potentiometer component * * This function must be called once before starting to use the component. * * @param pHandle Handle on the Potentiometer component to initialize */ void POT_Init(Potentiometer_Handle_t *pHandle) { #ifdef NULL_PTR_CHECK_POT if (MC_NULL == pHandle) { /* Nothing to do */ } else { #endif /* NULL_PTR_CHECK_POT */ /* Potentiometer component init */ pHandle->LPFilterBandwidth = (uint16_t)(1 << pHandle->LPFilterBandwidthPOW2); POT_Clear(pHandle); #ifdef NULL_PTR_CHECK_POT } #endif /* NULL_PTR_CHECK_POT */ } /** * @brief Clears the state of a Potentiometer component * * After this function has been called, the potentiometer value * becomes invalid. See the @ref Potentiometer documentation for more * details. * * @param pHandle Handle on the Potentiometer component */ void POT_Clear(Potentiometer_Handle_t *pHandle) { #ifdef NULL_PTR_CHECK_POT if (MC_NULL == pHandle) { /* Nothing to do */ } else { #endif /* NULL_PTR_CHECK_POT */ for (uint8_t i = 0; i < pHandle->LPFilterBandwidth; i++) { pHandle->PotMeasArray[i] = (uint16_t) 0; } pHandle->PotValueAccumulator = (uint32_t)0; pHandle->Index = (uint16_t)0; pHandle->Valid = (bool) false; #ifdef NULL_PTR_CHECK_POT } #endif /* NULL_PTR_CHECK_POT */ } /** * @brief Measures the voltage of the potentiometer of a Potentiometer component * * This function needs to be called periodically. See the @ref Potentiometer * documentation for more details. * * @param pHandle Handle on the Potentiometer component */ void POT_TakeMeasurement(Potentiometer_Handle_t *pHandle, uint16_t rawValue) { #ifdef NULL_PTR_CHECK_POT if (MC_NULL == pHandle) { /* Nothing to do */ } else { #endif /* NULL_PTR_CHECK_POT */ /* Update the Accumulator */ pHandle->PotValueAccumulator -= (uint32_t) pHandle->PotMeasArray[pHandle->Index]; pHandle->PotMeasArray[pHandle->Index] = rawValue; pHandle->PotValueAccumulator += (uint32_t) pHandle->PotMeasArray[pHandle->Index]; /* Update the Index */ pHandle->Index++; if (pHandle->Index == pHandle->LPFilterBandwidth) { pHandle->Index = 0; /* The Index has reached the end of the measurement array. * So, the accumulator is only made of actual measure. * Hence, its value is considered valid. */ pHandle->Valid = true; } else { /* Nothing to do */ } #ifdef NULL_PTR_CHECK_POT } #endif /* NULL_PTR_CHECK_POT */ } /** * @brief Returns the current value of a Potentiometer component * * @param pHandle Handle on the Potentiometer component */ uint16_t POT_GetValue(Potentiometer_Handle_t *pHandle) { #ifdef NULL_PTR_CHECK_POT return ((NULL == pHandle) ? 0U : (uint16_t)(pHandle->PotValueAccumulator >> pHandle->LPFilterBandwidthPOW2)); #else /* NULL_PTR_CHECK_POT */ return ((uint16_t)(pHandle->PotValueAccumulator >> pHandle->LPFilterBandwidthPOW2)); #endif /* NULL_PTR_CHECK_POT */ } /** * @brief Returns true if the current value of a Potentiometer component is valid * and false otherwise * * @param pHandle Handle on the Potentiometer component */ bool POT_ValidValueAvailable(Potentiometer_Handle_t *pHandle) { #ifdef NULL_PTR_CHECK_POT return ((NULL == pHandle) ? 0U : pHandle->Valid); #else /* NULL_PTR_CHECK_POT */ return (pHandle->Valid); #endif /* NULL_PTR_CHECK_POT */ } /** * @} */ /** * @} */ /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
6,915
C
32.090909
113
0.652639
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/MCSDK_v6.2.0-Full/MotorControl/MCSDK/MCLib/Any/Src/gap_gate_driver_ctrl.c
/** ****************************************************************************** * @file gap_gate_driver_ctrl.c * @author Motor Control SDK Team, ST Microelectronics * @brief This file provides firmware functions that implement the features * of the GAP component of the Motor Control SDK that provides support * the STGAPxx galvanically isolated gate drivers family. * ****************************************************************************** * @attention * * <h2><center>&copy; Copyright (c) 2023 STMicroelectronics. * All rights reserved.</center></h2> * * This software component is licensed by ST under Ultimate Liberty license * SLA0044, the "License"; You may not use this file except in compliance with * the License. You may obtain a copy of the License at: * www.st.com/SLA0044 * ****************************************************************************** * @ingroup GAP_GATE_DRIVER_CTRL */ /* Includes ------------------------------------------------------------------*/ #include "gap_gate_driver_ctrl.h" #include "mc_type.h" /** * @addtogroup MCSDK * @{ */ /** * @defgroup GAP_GATE_DRIVER_CTRL STGAP1x controller * @brief A component to interface with the configuration and diagnostic * features of STGAP1x gate drivers through SPI * * The STGAP1x gate drivers family offers galvanically isolated gate drivers * for high-power MOSFET and IGBT applications. These devices provide advanced * configuration and diagnostic features that are accessible through their SPI * interface. Several STGAP1x devices can be daisy-chained on an SPI bus. * * The STGAP1x controller component allows for configuring any number of STGAP1x * devices daisy-chained on an SPI bus and for accessing their diagnostic * registers. * * The GAP driver configuration is performed at the first steps of the MCboot(). * In a MCSDK project up to seven gate drivers might be configured: * * 1 brake STGAP1AS_BRAKE * * 3 high and low side PWM signals STGAP1AS_UH, STGAP1AS_UL, STGAP1AS_VH, STGAP1AS_VL, STGAP1AS_WH, STGAP1AS_WL. * * More information on STGAP1x can be find on st.com: [Isolated Gate Drivers](https://www.st.com/en/motor-drivers/isolated-gate-drivers.html) * * @{ */ /* Private defines -----------------------------------------------------------*/ #define GAP_STARTCONFIG 0x2A /**< @brief CRC data computation value to start GAP driver configuration. */ #define GAP_STOPCONFIG 0x3A /**< @brief CRC data computation value to stop GAP driver configuration. */ #define GAP_WRITEREG 0x80 /**< @brief CRC data computation value for writing register. */ #define GAP_READREG 0xA0 /**< @brief CRC data computation value for reading register. */ #define GAP_RESETREG 0xC0 /**< @brief CRC data computation value to reset a register. */ #define GAP_RESETSTATUS 0xD0 /**< @brief CRC data computation value to reset a status register. */ #define GAP_GLOBALRESET 0xEA /**< @brief CRC data computation value to reset GAP driver. */ #define GAP_SLPEEP 0xF5 /**< @brief CRC data computation value to set GAP driver in sleep mode. */ #define GAP_NOP 0x00 /**< @brief CRC data computation value for no operation. */ #define GAP_ERROR_CODE_FROM_DEVICE_MASK (uint32_t)(0xFF000000) /**< @brief ERROR code mask */ #define WAITTIME 5000 /**< @brief 860u wait time duration. */ #define WAITTRCHK 50 /**< @brief 8.6u wait time duration. */ #define WAITTSENSECHK 50 /**< @brief 8.6u wait time duration. */ #define WAITTGCHK 50 /**< @brief 8.6u wait time duration. */ #define WAITTDESATCHK 50 /**< @brief 8.6u wait time duration. */ /* Global variables ----------------------------------------------------------*/ static void GAP_SD_Deactivate(GAP_Handle_t *pHandle_t); static void GAP_SD_Activate(GAP_Handle_t *pHandle_t); static void GAP_CS_Deactivate(GAP_Handle_t *pHandle_t); static void GAP_CS_Activate(GAP_Handle_t *pHandle_t); static uint16_t GAP_SPI_Send(GAP_Handle_t *pHandle_t, uint16_t value); /** * @brief Checks errors of GAP devices * @param pHandle Handle of the GAP component GAP_Handle_t. * @param errorNow Buffer of returned bitfields containing error flags * coming from GAP device that are currently active.\n * The buffer have to be provided from the caller. * @param errorOccurred Buffer of returned bitfields containing error flags * coming from GAP device that are over.\n * The buffer have to be provided from the caller. * @retval bool It returns false if an error occurs, otherwise return true. */ __weak bool GAP_CheckErrors(GAP_Handle_t *pHandle, uint32_t *error_now, uint32_t *error_occurred) { bool ret_val = false; uint32_t errorFromDevices[MAX_DEVICES_NUMBER]; if ((error_now) && (error_occurred)) { uint8_t index1, index2; uint8_t ret_read[MAX_DEVICES_NUMBER]; // /* If current error is device not programmable try to re-configure before // read the status registers */ // if ((pDVars->wGAP_ErrorsNow[0] & GAP_ERROR_CODE_DEVICES_NOT_PROGRAMMABLE) || // (pDVars->wGAP_ErrorsNow[0] & GAP_ERROR_CODE_SPI_CRC)) // { // if (GAP_Configuration(pHandle)) // { // pDVars->wGAP_ErrorsNow[0] &= ~GAP_ERROR_CODE_DEVICES_NOT_PROGRAMMABLE; // pDVars->wGAP_ErrorsNow[0] &= ~GAP_ERROR_CODE_SPI_CRC; // } // } index2 = pHandle->DeviceNum; ret_val = GAP_ReadRegs(pHandle, ret_read, STATUS1); for (index1 = 0; index1 < index2; index1 ++) { errorFromDevices[index1] = (ret_read[index1] << 16); } ret_val = GAP_ReadRegs(pHandle, ret_read, STATUS2); for (index1 = 0; index1 < index2; index1 ++) { /* Clear GATE bit from STATUS2 - no error if 1 */ ret_read[index1] &= 0xFE; errorFromDevices[index1] |= (ret_read[index1] << 8); } ret_val = GAP_ReadRegs(pHandle, ret_read, STATUS3); for (index1 = 0; index1 < index2; index1 ++) { errorFromDevices[index1] |= ret_read[index1]; } for (index1 = 0; index1 < index2; index1 ++) { pHandle->GAP_ErrorsNow[index1] &= GAP_ERROR_CODE_FROM_DEVICE_MASK; pHandle->GAP_ErrorsNow[index1] |= errorFromDevices[index1]; pHandle->GAP_ErrorsOccurred[index1] |= pHandle->GAP_ErrorsNow[index1]; error_now[index1] = pHandle->GAP_ErrorsNow[index1]; error_occurred[index1] = pHandle->GAP_ErrorsOccurred[index1]; } } return ret_val; } /** * @brief Clears the fault state of GAP devices. * @param pHandle Handle of the GAP component GAP_Handle_t. * @retval none. */ __weak void GAP_FaultAck(GAP_Handle_t *pHandle) { uint8_t index1, index2; uint16_t value; GAP_SD_Activate(pHandle); value = GAP_CRCCalculate(GAP_RESETSTATUS, 0xFF); GAP_CS_Activate(pHandle); index2 = pHandle->DeviceNum; for (index1 = 0; index1 < index2; index1 ++) { GAP_SPI_Send(pHandle, value); } GAP_CS_Deactivate(pHandle); GAP_SD_Deactivate(pHandle); wait(WAITTIME); for (index1 = 0; index1 < index2; index1 ++) { pHandle->GAP_ErrorsOccurred[index1] = GAP_ERROR_CLEAR; } } /** * @brief Programs the GAP devices with the settled parameters. * @param pHandle Handle of the GAP component GAP_Handle_t. * @retval bool It returns false if at least one device is not programmable, * otherwise return true. */ __weak bool GAP_Configuration(GAP_Handle_t *pHandle) { bool ret_val; LL_SPI_Enable(pHandle->SPIx); /* Configure devices with settled parameters */ GAP_DevicesConfiguration(pHandle); /* Verify if device has been programmed */ ret_val = GAP_IsDevicesProgrammed(pHandle); if (!ret_val) { /* At least one device is not programmable */ pHandle->GAP_ErrorsNow[0] |= GAP_ERROR_CODE_DEVICES_NOT_PROGRAMMABLE; pHandle->GAP_ErrorsOccurred[0] |= GAP_ERROR_CODE_DEVICES_NOT_PROGRAMMABLE; } return ret_val; } /** * @brief Checks if the GAP devices are programmed with the settled parameters. * @param pHandle Handle of the GAP component GAP_Handle_t. * @retval True if the GAP devices are already programmed with the settled * parameters. */ __weak bool GAP_IsDevicesProgrammed(GAP_Handle_t *pHandle) { bool ret_val = true; uint8_t index1, index2 = pHandle->DeviceNum; uint8_t read_reg_values[MAX_DEVICES_NUMBER]; GAP_ReadRegs(pHandle, read_reg_values, CFG1); for (index1 = 0; index1 < index2; index1 ++) { if ((pHandle->DeviceParams[index1].CFG1 & CFG1_REG_MASK) != read_reg_values[index1]) { ret_val = false; break; } } if (ret_val) { GAP_ReadRegs(pHandle, read_reg_values, CFG2); for (index1 = 0; index1 < index2; index1 ++) { if ((pHandle->DeviceParams[index1].CFG2 & CFG2_REG_MASK) != read_reg_values[index1]) { ret_val = false; break; } } } if (ret_val) { GAP_ReadRegs(pHandle, read_reg_values, CFG3); for (index1 = 0; index1 < index2; index1 ++) { if ((pHandle->DeviceParams[index1].CFG3 & CFG3_REG_MASK) != read_reg_values[index1]) { ret_val = false; break; } } } if (ret_val) { GAP_ReadRegs(pHandle, read_reg_values, CFG4); for (index1 = 0; index1 < index2; index1 ++) { if ((pHandle->DeviceParams[index1].CFG4 & CFG4_REG_MASK) != read_reg_values[index1]) { ret_val = false; break; } } } if (ret_val) { GAP_ReadRegs(pHandle, read_reg_values, CFG5); for (index1 = 0; index1 < index2; index1 ++) { if ((pHandle->DeviceParams[index1].CFG5 & CFG5_REG_MASK) != read_reg_values[index1]) { ret_val = false; break; } } } if (ret_val) { GAP_ReadRegs(pHandle, read_reg_values, DIAG1); for (index1 = 0; index1 < index2; index1 ++) { if ((pHandle->DeviceParams[index1].DIAG1 & DIAG1_REG_MASK) != read_reg_values[index1]) { ret_val = false; break; } } } if (ret_val) { GAP_ReadRegs(pHandle, read_reg_values, DIAG2); for (index1 = 0; index1 < index2; index1 ++) { if ((pHandle->DeviceParams[index1].DIAG2 & DIAG2_REG_MASK) != read_reg_values[index1]) { ret_val = false; break; } } } return ret_val; } /** * @brief Programs the GAP devices with the settled parameters. * @param pHandle Handle of the GAP component GAP_Handle_t. * @retval bool It returns false if an error occurs, otherwise return true. */ __weak bool GAP_DevicesConfiguration(GAP_Handle_t *pHandle) { bool ret_val = true; uint8_t index1, DeviceNum = pHandle->DeviceNum; uint8_t write_reg_values[MAX_DEVICES_NUMBER]; GAP_StartConfig(pHandle); /* Global Reset before programming */ GAP_GlobalReset(pHandle); for (index1 = 0; index1 < DeviceNum; index1 ++) { write_reg_values[index1] = pHandle->DeviceParams[index1].CFG1; } ret_val = GAP_WriteRegs(pHandle, write_reg_values, CFG1); if (ret_val) { for (index1 = 0; index1 < DeviceNum; index1 ++) { write_reg_values[index1] = pHandle->DeviceParams[index1].CFG2; } ret_val = GAP_WriteRegs(pHandle, write_reg_values, CFG2); } if (ret_val) { for (index1 = 0; index1 < DeviceNum; index1 ++) { write_reg_values[index1] = pHandle->DeviceParams[index1].CFG3; } ret_val = GAP_WriteRegs(pHandle, write_reg_values, CFG3); } if (ret_val) { for (index1 = 0; index1 < DeviceNum; index1 ++) { write_reg_values[index1] = pHandle->DeviceParams[index1].CFG4; } ret_val = GAP_WriteRegs(pHandle, write_reg_values, CFG4); } if (ret_val) { for (index1 = 0; index1 < DeviceNum; index1 ++) { write_reg_values[index1] = pHandle->DeviceParams[index1].CFG5; } ret_val = GAP_WriteRegs(pHandle, write_reg_values, CFG5); } if (ret_val) { for (index1 = 0; index1 < DeviceNum; index1 ++) { write_reg_values[index1] = pHandle->DeviceParams[index1].DIAG1; } ret_val = GAP_WriteRegs(pHandle, write_reg_values, DIAG1); } if (ret_val) { for (index1 = 0; index1 < DeviceNum; index1 ++) { write_reg_values[index1] = pHandle->DeviceParams[index1].DIAG2; } ret_val = GAP_WriteRegs(pHandle, write_reg_values, DIAG2); } GAP_StopConfig(pHandle); /* Fault reset */ GAP_FaultAck(pHandle); return ret_val; } /** * @brief Calculates CRC from data and creates 16bit value with data as MSB and * CRC as LSB. * @param data 8bit value used to calculate CRC. * @retval uint16_t It returns the 16bit value with data as MSB and * CRC as LSB. */ __weak uint16_t GAP_CRCCalculate(uint8_t data, uint8_t crc_initial_value) { uint8_t crc = crc_initial_value; uint8_t poly = 0x07; uint8_t crc_temp; uint8_t crctab[8]; uint8_t index1, index2; uint16_t value; value = data; value <<= 8; for (index2 = 0; index2 < 8; index2 ++) { crctab[index2] = (crc >> index2) & 0x1; } for (index2 = 0; index2 < 8; index2 ++) { crc_temp = (crctab[7] << 7) + (crctab[6] << 6) + (crctab[5] << 5) + (crctab[4] << 4) + (crctab[3] << 3) + (crctab[2] << 2) + (crctab[1] << 1) + (crctab[0]); crctab[0] = ((data >> (7 - index2)) & 0x1) ^ crctab[7]; for (index1 = 1; index1 < 8; index1 ++) { crctab[index1] = (crctab[0] & ((poly >> index1) & 0x1)) ^ ((crc_temp >> (index1 - 1)) & 0x1); } } crc = (crctab[7] << 7) + (crctab[6] << 6) + (crctab[5] << 5) + (crctab[4] << 4) + (crctab[3] << 3) + (crctab[2] << 2) + (crctab[1] << 1) + (crctab[0] << 0); crc ^= 0xFF; value |= crc; return value; } /** * @brief Verifies the CRC from dataIn and extracts the 8bit data value (out). * @param out Reference for the extracted 8bit data value. * @param dataIn 16bit value with data as MSB and CRC as LSB. * @retval bool It returns true if CRC is correct, false otherwise. */ __weak bool GAP_CRCCheck(uint8_t *out, uint16_t data_in) { bool ret_val = false; uint8_t data = (uint8_t)(data_in >> 8); uint8_t received_crc = (uint8_t)(data_in); uint8_t crc = (uint8_t)(GAP_CRCCalculate(data, 0xFF)) ^ 0xFF; if (crc == received_crc) { *out = data; ret_val = true; } return ret_val; } /** * @brief Waits a time interval proportional to count. * @param count Number of count to be waited. * @retval none */ __weak void wait(uint16_t count) { volatile uint16_t wait_cnt; for (wait_cnt = 0; wait_cnt < count; wait_cnt ++) { /* Nothing to do */ } } /** * @brief Returns the register mask starting from it address. * @param reg Register address GAP_Registers_Handle_t. * @retval uint8_t Mask to be and-ed bit wise to data to filter it. */ __weak uint8_t GAP_RegMask(GAP_Registers_Handle_t reg) { uint8_t ret_val; switch (reg) { case CFG1: { ret_val = CFG1_REG_MASK; } break; case CFG2: { ret_val = CFG2_REG_MASK; } break; case CFG3: { ret_val = CFG3_REG_MASK; } break; case CFG4: { ret_val = CFG4_REG_MASK; } break; case CFG5: { ret_val = CFG5_REG_MASK; } break; case STATUS1: { ret_val = STATUS1_REG_MASK; } break; case STATUS2: { ret_val = STATUS2_REG_MASK; } break; case STATUS3: { ret_val = STATUS3_REG_MASK; } break; case TEST1: { ret_val = TEST1_REG_MASK; } break; case DIAG1: { ret_val = DIAG1_REG_MASK; } break; case DIAG2: { ret_val = DIAG2_REG_MASK; } break; default: { ret_val = 0x00; } break; } return ret_val; } /** * @brief Reads all data in the daisy chain related to register reg. * @param pHandle Handle of the GAP component GAP_Handle_t. * @param pDataRead Pointer to the buffer in which will be stored the readed * data. The buffer have to be provided from the caller. * @param reg Register to be read. It must be one of the register defined in * GAP_Registers_Handle_t. * @retval bool It returns false if an error occurs, otherwise return true. */ __weak bool GAP_ReadRegs(GAP_Handle_t *pHandle, uint8_t *pDataRead, GAP_Registers_Handle_t reg) { bool ret_val = false; uint8_t index1; uint8_t device; uint8_t data; uint16_t value; if (pDataRead) { value = GAP_CRCCalculate(GAP_READREG | reg, 0xFF); GAP_CS_Activate(pHandle); for (index1 = 0; index1 < pHandle->DeviceNum; index1 ++) { GAP_SPI_Send(pHandle, value); } GAP_CS_Deactivate(pHandle); wait(WAITTIME); value = GAP_CRCCalculate(GAP_NOP, 0xFF); GAP_CS_Activate(pHandle); ret_val = true; for (index1 = 0; index1 < pHandle->DeviceNum; index1 ++) { device = pHandle->DeviceNum - index1 - 1; if (pHandle->DeviceParams[device].CFG1 & GAP_CFG1_CRC_SPI) { if (GAP_CRCCheck(&data, GAP_SPI_Send(pHandle, value))) { pDataRead[device] = data & GAP_RegMask(reg); } else { pDataRead[device] = 0x00; pHandle->GAP_ErrorsNow[0] |= GAP_ERROR_CODE_SPI_CRC; pHandle->GAP_ErrorsOccurred[0] |= GAP_ERROR_CODE_SPI_CRC; ret_val = false; } } else { pDataRead[device] = (uint8_t)(GAP_SPI_Send(pHandle, value) >> 8) & GAP_RegMask(reg); } } GAP_CS_Deactivate(pHandle); } return ret_val; } /** * @brief Switches the device to the configuration mode allowing writing configuration values in configuration registers. * @param pHandle Handle of the GAP component GAP_Handle_t. * @retval none. */ __weak void GAP_StartConfig(GAP_Handle_t *pHandle) { uint8_t index; uint16_t value; GAP_SD_Activate(pHandle); value = GAP_CRCCalculate(GAP_STARTCONFIG, 0xFF); GAP_CS_Activate(pHandle); for (index = 0; index < pHandle->DeviceNum; index ++) { GAP_SPI_Send(pHandle, value); } GAP_CS_Deactivate(pHandle); wait(WAITTIME); } /** * @brief Quits the configuration mode and make all changes effective. * @param pHandle Handle of the GAP component GAP_Handle_t. * @retval none. */ __weak void GAP_StopConfig(GAP_Handle_t *pHandle) { uint8_t index; uint16_t value; value = GAP_CRCCalculate(GAP_STOPCONFIG, 0xFF); GAP_CS_Activate(pHandle); for (index = 0; index < pHandle->DeviceNum; index ++) { GAP_SPI_Send(pHandle, value); } GAP_CS_Deactivate(pHandle); wait(WAITTIME); GAP_SD_Deactivate(pHandle); } /** * @brief Writes data in the daisy chain into the register reg. * @param pHandle Handle of the GAP component GAP_Handle_t. * @param pDataWrite Pointer to the buffer in which are stored the data * to be written in the register reg. The buffer have to be provided * from the caller. * @param reg Register to be write. It must be one of the register defined in * GAP_Registers_Handle_t. * @retval bool It returns false if an error occurs, otherwise return true. */ __weak bool GAP_WriteRegs(GAP_Handle_t *pHandle, uint8_t *pDataWrite, GAP_Registers_Handle_t reg) { bool ret_val = false; if (pDataWrite) { uint8_t crc; uint8_t index; uint16_t value; value = GAP_CRCCalculate(GAP_WRITEREG | reg, 0xFF); crc = (uint8_t)(value); GAP_CS_Activate(pHandle); for (index = 0; index < pHandle->DeviceNum; index ++) { GAP_SPI_Send(pHandle, value); } GAP_CS_Deactivate(pHandle); wait(WAITTIME); GAP_CS_Activate(pHandle); ret_val = true; for (index = 0; index < pHandle->DeviceNum; index ++) { value = GAP_CRCCalculate(pDataWrite[index], crc ^ 0xFF); GAP_SPI_Send(pHandle, value); } GAP_CS_Deactivate(pHandle); wait(WAITTIME); } return ret_val; } /** * @brief Resets all the registers to the default and releases all the failure flag. * @param pHandle Handle of the GAP component GAP_Handle_t. * @retval none. */ __weak void GAP_GlobalReset(GAP_Handle_t *pHandle) { uint8_t index; uint16_t value; value = GAP_CRCCalculate(GAP_GLOBALRESET, 0xFF); GAP_CS_Activate(pHandle); for (index = 0; index < pHandle->DeviceNum; index ++) { GAP_SPI_Send(pHandle, value); } GAP_CS_Deactivate(pHandle); wait(WAITTIME); } /** * @brief Resets selected status register. * @param pHandle Handle of the GAP component GAP_Handle_t. * @param reg Register to be reset. It must be one of the STATUS register * defined in GAP_Registers_Handle_t. * @retval bool It returns false if an error occurs, otherwise return true. */ __weak bool GAP_ResetStatus(GAP_Handle_t *pHandle, GAP_Registers_Handle_t reg) { bool ret_val = false; uint8_t index; uint16_t value; GAP_SD_Activate(pHandle); value = GAP_CRCCalculate(GAP_RESETREG | reg, 0xFF); GAP_CS_Activate(pHandle); for (index = 0; index < pHandle->DeviceNum; index ++) { GAP_SPI_Send(pHandle, value); } GAP_CS_Deactivate(pHandle); GAP_SD_Deactivate(pHandle); return ret_val; } /** * @brief It performs the selected test on each GAP devices. * @attention pHandle function should be called just in IDLE state. * @param pHandle Handle of the GAP component GAP_Handle_t. * @param testMode Test mode to be executed. It shall be one of the * test modes present in the GAP_TestMode_t. * @retval bool It returns true if an error occurs, otherwise return false. */ __weak bool GAP_Test(GAP_Handle_t *pHandle, GAP_TestMode_t testMode) { bool ret_val = false; bool invertResult = false; uint8_t testModeData; uint8_t clr[MAX_DEVICES_NUMBER]; uint8_t data[MAX_DEVICES_NUMBER]; uint8_t index1, index2 = pHandle->DeviceNum; uint8_t statusMask; uint16_t wait_cnt; switch (testMode) { case GOFF_CHK: { testModeData = GAP_TEST1_GOFFCHK; wait_cnt = WAITTGCHK; statusMask = GAP_STATUS1_DESAT; } break; case GON_CHK: { testModeData = GAP_TEST1_GONCHK; wait_cnt = WAITTGCHK; statusMask = GAP_STATUS1_TSD; } break; case GAP_TEST1_DESCHK: { testModeData = DESAT_CHK; wait_cnt = WAITTDESATCHK; statusMask = GAP_STATUS1_DESAT; invertResult = true; } break; case SENSE_RESISTOR_CHK: { testModeData = GAP_TEST1_RCHK; wait_cnt = WAITTRCHK; statusMask = GAP_STATUS1_SENSE; } break; case SENSE_COMPARATOR_CHK: { testModeData = GAP_TEST1_SNSCHK; wait_cnt = WAITTSENSECHK; statusMask = GAP_STATUS1_SENSE; invertResult = true; } break; default: { ret_val = true; } break; } for (index1 = 0; index1 < index2; index1 ++) { data[index1] = testModeData; clr[index1] = 0x00; } GAP_WriteRegs(pHandle, data, TEST1); wait(wait_cnt); GAP_ReadRegs(pHandle, data, STATUS1); /* Clear TEST1 regs */ GAP_WriteRegs(pHandle, clr, TEST1); /* Clear STATUS1 regs */ GAP_ResetStatus(pHandle, STATUS1); for (index1 = 0; index1 < index2; index1 ++) { if (invertResult) { if ((data[index1] & statusMask) == 0) { ret_val = true; } } else { if ((data[index1] & statusMask) != 0) { ret_val = true; } } } return ret_val; } /** * @brief This function sends a 16bit value through the configured SPI and * returns the 16-bit value received during communication. * @param pHandle_t Handle of the GAP component GAP_Handle_t. * @param value Value to be sent through SPI. * @retval uint16_t Received 16bit value. */ uint16_t GAP_SPI_Send(GAP_Handle_t *pHandle_t, uint16_t value) { SPI_TypeDef *SPIx = pHandle_t->SPIx; /* Wait for SPI Tx buffer empty */ while (LL_SPI_IsActiveFlag_TXE(SPIx) == 0); /* Send SPI data */ LL_SPI_TransmitData16(SPIx, value); /* Wait for SPIz data reception */ while (LL_SPI_IsActiveFlag_RXNE(SPIx) == 0); /* Read SPIz received data */ return LL_SPI_ReceiveData16(SPIx); } /** * @brief Deactivates SD pin. * @param pHandle_t Handle of the GAP component GAP_Handle_t. * @retval none. */ void GAP_SD_Deactivate(GAP_Handle_t *pHandle_t) { LL_GPIO_SetOutputPin(pHandle_t->NSDPort, pHandle_t->NSDPin); } /** * @brief Activates SD pin. * @param pHandle_t Handle of the GAP component GAP_Handle_t. * @retval none. */ void GAP_SD_Activate(GAP_Handle_t *pHandle_t) { LL_GPIO_ResetOutputPin(pHandle_t->NSDPort, pHandle_t->NSDPin); } /** * @brief Deactivates CS pin. * @param pHandle_t Handle of the GAP component GAP_Handle_t. * @retval none. */ void GAP_CS_Deactivate(GAP_Handle_t *pHandle_t) { LL_GPIO_SetOutputPin(pHandle_t->NCSPort, pHandle_t->NCSPin); } /** * @brief Activates CS pin. * @param pHandle_t Handle of the GAP component GAP_Handle_t. * @retval none. */ void GAP_CS_Activate(GAP_Handle_t *pHandle_t) { LL_GPIO_ResetOutputPin(pHandle_t->NCSPort, pHandle_t->NCSPin); } /** * @} */ /** * @} */ /************************ (C) COPYRIGHT 2023 STMicroelectronics *****END OF FILE****/
25,537
C
26.971522
142
0.611309
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/MCSDK_v6.2.0-Full/MotorControl/MCSDK/MCLib/Any/Src/pidregdqx_current.c
/** ****************************************************************************** * @file pidregdqx_current.c * @author Motor Control SDK Team, ST Microelectronics * @brief This file provides firmware functions that implement the features * of the PID current regulator component of the Motor Control SDK * ****************************************************************************** * @attention * * <h2><center>&copy; Copyright (c) 2023 STMicroelectronics. * All rights reserved.</center></h2> * * This software component is licensed by ST under Ultimate Liberty license * SLA0044, the "License"; You may not use this file except in compliance with * the License. You may obtain a copy of the License at: * www.st.com/SLA0044 * ****************************************************************************** * @ingroup PIDRegdqx */ #include "pidregdqx_current.h" /** @addtogroup MCSDK * @{ */ /** * @defgroup PIDRegdqx PID current regulator * * @brief PID current regulator component of the Motor Control SDK * * The PID current regulator component implements the following control functions: * * * A proportional-integral controller, implemented by the PIDREGDQX_CURRENT_run() function: * * A circle limitation feature that limits the output duty vector according to the maximum * modulation value configured by the user thanks to the function PIDREGDQX_CURRENT_setMaxModulation_squared(). * * A DC bus compensation to mitigate DC bus ripple. * * ![](pidreg_current.png) * * * Input: * * `I_dq_Ref`: currents vector references * * `I_dq` : currents vector measured * * Output: * * `Duty_dq:` Duty vector * * If needed the PID current regulator can apply a cross-coupling compensation feature to increase stability at high speed. * * ![](pidreg_crosscomp_current.png) * * * Input: * * `I_dq_Ref`: currents vector references * * `I_dq` : currents vector measured * * `?`: Electrical speed from delta angle (low pass filtered) * * Output: * * `Duty_dq`: Duty vector * * Each of the gain parameters, can be set, at run time and independently, via the PIDREGDQX_CURRENT_setKp_si(), * PIDREGDQX_CURRENT_setWi_si(). * * A PID Current Regulator component needs to be initialized before it can be used. This is done with the PIDREGDQX_CURRENT_init() * function that sets the intergral term to 0 and initializes the component data structure. * * To keep the computed values within limits, the component features the possibility to constrain the integral term * within a range of values bounded by the PIDREGDQX_CURRENT_setOutputLimitsD() and PIDREGDQX_CURRENT_setOutputLimitsQ() functions. * * Handling a process with a PID Controller may require some adjustment to cope with specific situations. To that end, the * PID speed regulator component provides functions to set the integral term (PIDREGDQX_CURRENT_setUiD_pu() and PIDREGDQX_CURRENT_setUiQ_pu()). * @{ */ #define PU_FMT (30) /*!< @brief Per unit format, external format */ #define SUM_FTM (24) /*!< @brief Summing format, internal format */ /** * @brief Initializes PID current regulator component. * It Should be called during Motor control middleware initialization * @param pHandle PID current regulator handler * @param current_scale current scaling factor * @param voltage_scale voltage scaling factor * @param pid_freq_hz PID regulator execution frequency * @param freq_scale_hz frequency scaling factor * @param duty_limit duty cycle limit */ void PIDREGDQX_CURRENT_init( PIDREGDQX_CURRENT_s * pHandle, const float current_scale, const float voltage_scale, const float pid_freq_hz, const float freq_scale_hz, const float duty_limit ) { pHandle->crosscompON = false; /* only needed ON when stability at highest speeds is an issue */ pHandle->Kp_fps.value = 0; pHandle->Kp_fps.fixpFmt = 30; pHandle->Wi_fps.value = 0; pHandle->Wi_fps.fixpFmt = 30; pHandle->maxModulation_squared = FIXP((double)(duty_limit * duty_limit)); pHandle->MaxD = FIXP24(0.7f); pHandle->MinD = FIXP24(-0.7f); pHandle->UpD = FIXP24(0.0f); pHandle->UiD = FIXP24(0.0f); pHandle->OutD = FIXP30(0.0f); pHandle->MaxQ = FIXP24(0.7f); pHandle->MinQ = FIXP24(-0.7f); pHandle->UpQ = FIXP24(0.0f); pHandle->UiD = FIXP24(0.0f); pHandle->compensation = FIXP24(1.0f); pHandle->current_scale = current_scale; pHandle->voltage_scale = voltage_scale; pHandle->freq_scale_hz = freq_scale_hz; pHandle->pid_freq_hz = pid_freq_hz; pHandle->wfsT = FIXP((MATH_TWO_PI * (double)freq_scale_hz / (double)pid_freq_hz)); } #if defined (CCMRAM) #if defined (__ICCARM__) #pragma location = ".ccmram" #elif defined (__CC_ARM) || defined(__GNUC__) __attribute__((section (".ccmram"))) #endif #endif /** * @brief Computes the output of a PID current regulator component, sum of its proportional * and integral terms * @param pHandle PID current regulator handler * @param errD D current error * @param errQ Q current error * @param felec_pu motor electrical frequency */ void PIDREGDQX_CURRENT_run( PIDREGDQX_CURRENT_s* pHandle, const fixp30_t errD, const fixp30_t errQ, const fixp30_t felec_pu) { fixp24_t maxD = pHandle->MaxD; fixp24_t minD = pHandle->MinD; fixp24_t maxQ = pHandle->MaxQ; fixp24_t minQ = pHandle->MinQ; fixp24_t uiD = pHandle->UiD; fixp24_t uiQ = pHandle->UiQ; fixp24_t welecT = FIXP(0.0f); if(pHandle->crosscompON) { welecT = FIXP30_mpy(felec_pu, pHandle->wfsT); } /* Error */ pHandle->ErrD = (errD >> (PU_FMT-SUM_FTM)); pHandle->ErrQ = (errQ >> (PU_FMT-SUM_FTM)); /* Proportional term D*/ fixp24_t upD = FIXP_mpyFIXPscaled(pHandle->ErrD, &pHandle->Kp_fps); upD = FIXP_rsmpy(upD, pHandle->compensation); /* Proportional term Q*/ fixp24_t upQ = FIXP_mpyFIXPscaled(pHandle->ErrQ, &pHandle->Kp_fps); upQ = FIXP_rsmpy(upQ, pHandle->compensation); /* Integral term including cross-term D*/ uiD += FIXP_mpyFIXPscaled(upD, &pHandle->Wi_fps) - FIXP_mpy(upQ,welecT); pHandle->clippedD = ((uiD <= minD) || (uiD >= maxD)); uiD = FIXP_sat(uiD, maxD, minD); pHandle->UiD = uiD; /* Summing term D*/ fixp24_t sumD =FIXP_sat(upD + uiD, maxD, minD); // Circle limitation based on sumD fixp24_t d_duty_squared = FIXP_mpy(sumD, sumD); fixp24_t q_duty_squared = pHandle->maxModulation_squared - d_duty_squared; maxQ = FIXP24_sqrt(q_duty_squared); minQ = -maxQ; pHandle->MaxQ = maxQ; pHandle->MinQ = minQ; /* Integral term Q*/ uiQ += FIXP_mpyFIXPscaled(upQ, &pHandle->Wi_fps) + FIXP_mpy(upD,welecT); pHandle->clippedQ = ((uiQ <= minQ) || (uiQ >= maxQ)); uiQ = FIXP_sat(uiQ, maxQ, minQ); pHandle->UiQ = uiQ; /* Summing term Q*/ fixp24_t sumQ =FIXP_sat(upQ + uiQ, maxQ, minQ); /*back to 30 */ fixp30_t outD = (sumD << (PU_FMT-SUM_FTM)); fixp30_t outQ = (sumQ << (PU_FMT-SUM_FTM)); /* Store DQ values for monitoring */ pHandle->UpD = upD; pHandle->OutD = outD; pHandle->UpQ = upQ; pHandle->OutQ = outQ; pHandle->clipped = pHandle->clippedD || pHandle->clippedQ; } /** * @brief Gets overall clipping status * @param pHandle PID current regulator handler * @retval true if clipped false otherwise */ bool PIDREGDQX_CURRENT_getClipped( PIDREGDQX_CURRENT_s* pHandle ) { return pHandle->clipped; } /** * @brief Gets Kp gain in SI unit * @param pHandle PID current regulator handler * @retval Kp in float format */ float PIDREGDQX_CURRENT_getKp_si( PIDREGDQX_CURRENT_s* pHandle ) { float Kp_pu = FIXPSCALED_FIXPscaledToFloat(&pHandle->Kp_fps); float Kp_si = Kp_pu / pHandle->current_scale * pHandle->voltage_scale; return Kp_si; } #if defined (CCMRAM) #if defined (__ICCARM__) #pragma location = ".ccmram" #elif defined (__CC_ARM) || defined(__GNUC__) __attribute__((section (".ccmram"))) #endif #endif /** * @brief Gets the PID output on D axis * @param pHandle PID current regulator handler * @retval outD signal */ fixp_t PIDREGDQX_CURRENT_getOutD(PIDREGDQX_CURRENT_s* pHandle) { return pHandle->OutD; } #if defined (CCMRAM) #if defined (__ICCARM__) #pragma location = ".ccmram" #elif defined (__CC_ARM) || defined(__GNUC__) __attribute__((section (".ccmram"))) #endif #endif /** * @brief Gets the PID output on Q axis * @param pHandle PID current regulator handler * @retval outD signal */ fixp_t PIDREGDQX_CURRENT_getOutQ(PIDREGDQX_CURRENT_s* pHandle) { return pHandle->OutQ; } /** * @brief Gets Ki gain in SI unit * @param pHandle PID current regulator handler * @retval Ki gain */ float PIDREGDQX_CURRENT_getWi_si( PIDREGDQX_CURRENT_s* pHandle ) { float Wi_pu = FIXPSCALED_FIXPscaledToFloat(&pHandle->Wi_fps); float Wi_si = Wi_pu * pHandle->pid_freq_hz; return Wi_si; } /** * @brief Sets Kp gain expressed in SI unit * @param pHandle PID current regulator handler */ void PIDREGDQX_CURRENT_setKp_si( PIDREGDQX_CURRENT_s* pHandle, const float Kp) { // Parameter Kp is in V/A (or Ohm) /* Convert to per unit, in full scale voltage per full scale current */ float Kp_pu = Kp * pHandle->current_scale / pHandle->voltage_scale; /* Convert Kp_pu to scaled value, and store */ FIXPSCALED_floatToFIXPscaled(Kp_pu, &pHandle->Kp_fps); } /** * @brief Sets Kp and Ki gain according to Rs and Ls value of the motor * @param pHandle PID current regulator handler * @param Rsi motor resistance expressed in Ohm * @param Lsi motor inductance expressed in Henry * @param margin margin to apply for Kp computation */ void PIDREGDQX_CURRENT_setKpWiRLmargin_si( PIDREGDQX_CURRENT_s* pHandle, const float Rsi, const float Lsi, const float margin) { float kp_idq = (2.0f / margin) * Lsi * pHandle->pid_freq_hz; /* using duty_scale=2 unit V/A take gain-margin (margin about 5) */ float wi_idq = (Rsi / Lsi); /* unit rad/s */ pHandle->Kp = kp_idq; PIDREGDQX_CURRENT_setKp_si(pHandle, kp_idq); PIDREGDQX_CURRENT_setWi_si(pHandle, wi_idq); } /** * @brief Sets Ki gain in SI unit * @param pHandle PID current regulator handler * @param Wi Ki gain expressed in rad/s */ void PIDREGDQX_CURRENT_setWi_si( PIDREGDQX_CURRENT_s* pHandle, const float Wi) { // Parameter Wi is in 1/s (or rad/s) // Wi unit is frequency rad/s in series with Kp, dimension of Kp*Wi = V/As (voltage per charge, being 1/Farad) /* Convert to unit per interrupt frequency */ float Wi_pu = Wi / pHandle->pid_freq_hz; FIXPSCALED_floatToFIXPscaled(Wi_pu, &pHandle->Wi_fps); } #if defined (CCMRAM) #if defined (__ICCARM__) #pragma location = ".ccmram" #elif defined (__CC_ARM) || defined(__GNUC__) __attribute__((section (".ccmram"))) #endif #endif /** * @brief Sets D integral term * @param pHandle PID current regulator handler * @param Ui integral term value */ void PIDREGDQX_CURRENT_setUiD_pu( PIDREGDQX_CURRENT_s* pHandle, const fixp30_t Ui) { // Parameter Ui is in the same unit as the output, per unit duty // Internally the Ui is stored in a different format pHandle->UiD = (Ui >> (PU_FMT-SUM_FTM)); } #if defined (CCMRAM) #if defined (__ICCARM__) #pragma location = ".ccmram" #elif defined (__CC_ARM) || defined(__GNUC__) __attribute__((section (".ccmram"))) #endif #endif /** * @brief Sets Q integral term * @param pHandle PID current regulator handler * @param Ui integral term value */ void PIDREGDQX_CURRENT_setUiQ_pu( PIDREGDQX_CURRENT_s* pHandle, const fixp30_t Ui) { // Parameter Ui is in the same unit as the output, per unit duty // Internally the Ui is stored in a different format pHandle->UiQ = (Ui >> (PU_FMT-SUM_FTM)); } #if defined (CCMRAM) #if defined (__ICCARM__) #pragma location = ".ccmram" #elif defined (__CC_ARM) || defined(__GNUC__) __attribute__((section (".ccmram"))) #endif #endif /** * @brief Sets bus voltage compensation factor * @param pHandle PID current regulator handler * @param compensation bus voltage compensation factor */ void PIDREGDQX_CURRENT_setCompensation( PIDREGDQX_CURRENT_s* pHandle, const fixp24_t compensation) { pHandle->compensation = compensation; } /** * @brief Sets the D upper and lower output limit of a PID component * @param pHandle PID current regulator handler * @param max_pu upper limit in per-unit * @param mix_pu lower limit in per-unit */ void PIDREGDQX_CURRENT_setOutputLimitsD( PIDREGDQX_CURRENT_s* pHandle, const fixp30_t max_pu, const fixp30_t min_pu) { pHandle->MaxD = (max_pu >> (PU_FMT-SUM_FTM)); pHandle->MinD = (min_pu >> (PU_FMT-SUM_FTM)); } /** * @brief Sets the Q upper and lower output limit of a PID component * @param pHandle PID current regulator handler * @param max_pu upper limit in per-unit * @param mix_pu lower limit in per-unit */ void PIDREGDQX_CURRENT_setOutputLimitsQ( PIDREGDQX_CURRENT_s* pHandle, const fixp30_t max_pu, const fixp30_t min_pu) { pHandle->MaxQ = (max_pu >> (PU_FMT-SUM_FTM)); pHandle->MinQ = (min_pu >> (PU_FMT-SUM_FTM)); } /** * @brief Sets squared maximum modulation value * @param pHandle PID current regulator handler * @param duty_limit maximum modulation */ void PIDREGDQX_CURRENT_setMaxModulation_squared( PIDREGDQX_CURRENT_s* pHandle, const float duty_limit) { pHandle->maxModulation_squared = FIXP((double)(duty_limit * duty_limit)); } /** * @} */ /** * @} */ /************************ (C) COPYRIGHT 2023 STMicroelectronics *****END OF FILE****/
13,442
C
31.314904
144
0.672221
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/MCSDK_v6.2.0-Full/MotorControl/MCSDK/MCLib/Any/Src/speed_potentiometer.c
/** ****************************************************************************** * @file speed_potentiometer.c * @author Motor Control SDK Team, ST Microelectronics * @brief This file provides firmware functions that implement the features * of the Speed Potentiometer component of the Motor Control SDK. ****************************************************************************** * @attention * * <h2><center>&copy; Copyright (c) 20232 STMicroelectronics. * All rights reserved.</center></h2> * * This software component is licensed by ST under Ultimate Liberty license * SLA0044, the "License"; You may not use this file except in compliance with * the License. You may obtain a copy of the License at: * www.st.com/SLA0044 * ****************************************************************************** * @ingroup SpeedPotentiometer */ /* Includes ------------------------------------------------------------------*/ #include "speed_potentiometer.h" /** @addtogroup MCSDK * @{ */ /** @addtogroup Potentiometer * @{ */ /** @defgroup SpeedPotentiometer Speed Potentiometer * @brief Potentiometer reading component that sets the motor * speed reference from the potentiometer value * * The Speed Potentiometer component uses the services of the * @ref Potentiometer component to read a potentiometer. Values read * from this potentiometer are used to set the rotation speed reference * of a motor. * * The state of a Speed Potentiometer component is maintained in a * SpeedPotentiometer_Handle_t structure. To use the Speed Potentiometer component, * a SpeedPotentiometer_Handle_t structure needs to be instanciated and initialized. * The initialization is performed thanks to the SPDPOT_Init() function. Prior to * calling this function, some of the fields of this structure need to be given a * value. See the Potentiometer_Handle_t and below for more details on this. * * A Speed Potentiometer component sets speed references by executing speed ramps * thanks to the STC_ExecRamp() function. Prior to doing so, the component checks * if the motor is started (that is: if its state machine has reached the #RUN state) * and if its control modality is [Speed](@ref MCM_SPEED_MODE). If either of these * conditions is not met, no speed ramp is executed. * * In addition, a speed ramp is executed only if the value of the potentiometer at * that time differs from the one of the previous ramp that the component filed by * a configurable amount. This amount is gieven by the * @ref SpeedPotentiometer_Handle_t::SpeedAdjustmentRange "SpeedAdjustmentRange" field * of the Handle structure. * * The speed range accessible through the potentiometer is bounded by a minimum speed * and a maximum speed. the minimum speed is stated at compile time in the * @ref SpeedPotentiometer_Handle_t::MinimumSpeed "MinimumSpeed" field of the Handle * structure. The maximum speed is deduced from the * @ref SpeedPotentiometer_Handle_t::ConversionFactor "ConversionFactor" field thanks to * the following formula: * * $$ * MaximumSpeed = \frac{(65536 - MinimumSpeed \times ConversionFactor )}{ConversionFactor} * $$ * * where 65536 is the maximum value that the potentiometer (the ADC actually) can produce. * * The @ref SpeedPotentiometer_Handle_t::MinimumSpeed "MinimumSpeed" can be set so that when * the potentiometer is set to its minimum value, the motor stil spins. This is useful when * the Motor Control application uses a sensorless speed and position sensing algorithm that * cannot work below a given speed. * * The duration of speed ramps is controlled with the @ref SpeedPotentiometer_Handle_t::RampSlope "RampSlope" * field of the Handle. This field actually sets the acceleration used to change from one speed * to another. * * A potentiometer provides absolute values. A Speed Potentiometer component turns these * values into either positive or negative speed references depending on the actual speed * of the motor. So, if a motor is started with a negative speed, the references set by the * Speed Potentiometer component targetting it will also be negative. * * Values measured from the potentiometer are expressed in "u16digits": these are 16-bit values directly * read from an ADC. They need to be converted to the [speed unit](#SPEED_UNIT) used by the API * of the motor control library in order to generatethe speed references for the ramps. * * @note In the current version of the Speed Potentiometer component, the periodic ADC * measures **must** be performed on the Medium Frequency Task. This can be done by using * the MC_APP_PostMediumFrequencyHook_M1() function of the @ref MCAppHooks service * for instance. * * @{ */ /* Public functions ----------------------------------------------------------*/ /** * @brief Initializes a Speed Potentiometer component * * This function must be called once before the component can be used. * * @param pHandle Handle on the Speed Potentiometer component to initialize */ void SPDPOT_Init(SpeedPotentiometer_Handle_t *pHandle) { #ifdef NULL_PTR_CHECK_SPD_POT if (MC_NULL == pHandle) { /* Nothing to do */ } else { #endif POT_Init(&pHandle->Pot); SPDPOT_Clear(pHandle); #ifdef NULL_PTR_CHECK_SPD_POT } #endif /* NULL_PTR_CHECK_SPD_POT */ } /** * @brief Clears the state of a Speed Potentiometer component * * The state of the @ref Potentiometer component instance used by the * Speed Potentiometer component is also cleared. * * @param pHandle Handle on the Speed Potentiometer component */ void SPDPOT_Clear( SpeedPotentiometer_Handle_t * pHandle ) { #ifdef NULL_PTR_CHECK_SPD_POT if (MC_NULL == pHandle) { /* Nothing to do */ } else { #endif POT_Clear((Potentiometer_Handle_t *)pHandle); pHandle->LastSpeedRefSet = (uint16_t)0; pHandle->IsRunning = false; #ifdef NULL_PTR_CHECK_SPD_POT } #endif /* NULL_PTR_CHECK_SPD_POT */ } /** * @brief Reads the value of the potentiometer of a Speed Potentiometer component and sets * the rotation speed reference of the motor it targets acordingly * * The potentiometer handled by the Speed Potentiometer component is read. If its value * differs from the one that was last used to set the speed reference of the motor by more * than @ref SpeedPotentiometer_Handle_t::SpeedAdjustmentRange "SpeedAdjustmentRange", then * this new value is used to set a new speed reference. * * If the motor does not spin (it is not in the #RUN state) or if the current motor control * modality is not speed (#MCM_SPEED_MODE), nothing is done. * * This function needs to be called periodically. See the @ref Potentiometer * documentation for more details. * @param pHandle Handle on the Speed Potentiometer component */ bool SPDPOT_Run( SpeedPotentiometer_Handle_t *pHandle, uint16_t rawValue) { bool retVal = false; #ifdef NULL_PTR_CHECK_SPD_POT if (MC_NULL == pHandle) { /* Nothing to do */ } else { #endif SpeednTorqCtrl_Handle_t *pSTC = pHandle->pMCI->pSTC; POT_TakeMeasurement((Potentiometer_Handle_t *)pHandle, rawValue); if (MCM_SPEED_MODE == STC_GetControlMode(pSTC)) { if (RUN == MCI_GetSTMState(pHandle->pMCI)) { if (false == pHandle->IsRunning) { /* Making sure the potentiometer value is going to be considered */ pHandle->LastSpeedRefSet ^= 65535; /* Remember that the motor is running */ pHandle->IsRunning = true; } else { /* Nothing to do */ } if (POT_ValidValueAvailable((Potentiometer_Handle_t *) pHandle)) { uint16_t potValue = POT_GetValue((Potentiometer_Handle_t *)pHandle); if ((potValue <= (pHandle->LastSpeedRefSet - pHandle->SpeedAdjustmentRange)) || (potValue >= pHandle->LastSpeedRefSet + pHandle->SpeedAdjustmentRange)) { SpeednPosFdbk_Handle_t *speedHandle; uint32_t rampDuration; int16_t currentSpeed; int16_t requestedSpeed; int16_t deltaSpeed; speedHandle = STC_GetSpeedSensor(pSTC); currentSpeed = SPD_GetAvrgMecSpeedUnit(speedHandle); requestedSpeed = (int16_t)((potValue / pHandle->ConversionFactor) + pHandle->MinimumSpeed); deltaSpeed = (int16_t)requestedSpeed - ((currentSpeed >= 0) ? currentSpeed : -currentSpeed); if (deltaSpeed < 0) { deltaSpeed = - deltaSpeed; } else { /* Nothing to do */ } rampDuration = ((uint32_t)deltaSpeed * 1000U) / pHandle->RampSlope; if (currentSpeed < 0) { requestedSpeed = - requestedSpeed; } else { /* Nothing to do */ } STC_ExecRamp(pSTC, requestedSpeed, rampDuration); pHandle->LastSpeedRefSet = potValue; retVal = true; } } else { /* Nothing to do */ } } else { pHandle->IsRunning = false; } } else { pHandle->IsRunning = false; } #ifdef NULL_PTR_CHECK_SPD_POT } #endif /* NULL_PTR_CHECK_SPD_POT */ return (retVal); } /** * @} */ /** * @} */ /** * @} */ /************************ (C) COPYRIGHT 2023 STMicroelectronics *****END OF FILE****/
9,735
C
33.524823
111
0.635747
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/MCSDK_v6.2.0-Full/MotorControl/MCSDK/MCLib/Any/Src/pqd_motor_power_measurement.c
/** ****************************************************************************** * @file pqd_motor_power_measurement.c * @author Motor Control SDK Team, ST Microelectronics * @brief This file provides the functions that implement the features of the * PQD Motor Power Measurement component of the Motor Control SDK. * ****************************************************************************** * @attention * * <h2><center>&copy; Copyright (c) 2023 STMicroelectronics. * All rights reserved.</center></h2> * * This software component is licensed by ST under Ultimate Liberty license * SLA0044, the "License"; You may not use this file except in compliance with * the License. You may obtain a copy of the License at: * www.st.com/SLA0044 * ****************************************************************************** * @ingroup pqd_motorpowermeasurement */ /* Includes ------------------------------------------------------------------*/ #include "pqd_motor_power_measurement.h" #include "mc_type.h" /** @addtogroup MCSDK * @{ */ /** @defgroup pqd_motorpowermeasurement PQD Motor Power Measurement * @brief Motor Power Measurement component using DQ-frame current and voltage values * * The PQD Motor Power Measurement component uses @f$I_d@f$, @f$I_q@f$, @f$V_d@f$ and @f$V_q@f$ * to compute the electrical power flowing through the motor. * * These values are periodically sampled from the current regulation loop and used to compute * instantaneous power values. The instantaneous values are then used to compute an average * power value that is stored. These computations are done with integer operations and the * average value is store as an integer, in s16 digit format (s16A x s16V unit). * * The PQD Motor Power Measurement component provides an interface, PQD_GetAvrgElMotorPowerW() * that converts the int16_t digit average power into a floating point value expressed in * Watts. * * @{ */ /** * @brief Updates the average electrical motor power measure with the latest values * of the DQ currents and voltages. * * This method should be called with Medium Frequency Task periodicity. It computes an * instantaneous power value using the latest @f$I_{qd}@f$ and @f$V_{qd}@f$ data available * and uses it to update the average motor power value. * * Computations are done on s16A and s16V integer values defined in * [Current measurement unit](measurement_units.md). The average motor power value is * computed as an int16_t value. * * @param pHandle Handle on the related PQD Motor Power Measurement component instance. */ __weak void PQD_CalcElMotorPower(PQD_MotorPowMeas_Handle_t *pHandle) { #ifdef NULL_PTR_CHECK_PQD_MOT_POW_MEAS if (MC_NULL == pHandle) { /* Nothing to do */ } else { #endif int32_t wAux; qd_t Iqd = pHandle->pFOCVars->Iqd; qd_t Vqd = pHandle->pFOCVars->Vqd; wAux = ((int32_t)Iqd.q * (int32_t)Vqd.q) + ((int32_t)Iqd.d * (int32_t)Vqd.d); wAux /= 65536; /* pHandle->hAvrgElMotorPower += (wAux - pHandle->hAvrgElMotorPower) >> 4 */ pHandle->hAvrgElMotorPower += (int16_t)((wAux - (int32_t)pHandle->hAvrgElMotorPower) / 16); #ifdef NULL_PTR_CHECK_PQD_MOT_POW_MEAS } #endif } /** * @brief Clears the int16_t digit average motor power value stored in the handle. * * This function should be called before each motor start. * * @param pHandle Handle on the related PQD Motor Power Measurement component instance. */ __weak void PQD_Clear(PQD_MotorPowMeas_Handle_t *pHandle) { #ifdef NULL_PTR_CHECK_MOT_POW_MES if (MC_NULL == pHandle) { /* nothing to do */ } else { #endif pHandle->hAvrgElMotorPower = 0; #ifdef NULL_PTR_CHECK_MOT_POW_MES } #endif } /** * @brief Returns an average value of the measured motor power expressed in Watts * * This function converts the int16_t digit average motor power value stored in the handle * in a floating point value in Watts. * * @param pHandle pointer on the related component instance. * @retval float_t The average measured motor power expressed in Watts. */ __weak float_t PQD_GetAvrgElMotorPowerW(const PQD_MotorPowMeas_Handle_t *pHandle) { float_t PowerW = 0.0f; #ifdef NULL_PTR_CHECK_PQD_MOT_POW_MEAS if (MC_NULL == pHandle) { /* Nothing to do */ } else { #endif /* First perform an integer multiplication, then a float one. */ PowerW = ((float_t)pHandle->hAvrgElMotorPower * (float_t)VBS_GetAvBusVoltage_V(pHandle->pVBS)) * pHandle->ConvFact; #ifdef NULL_PTR_CHECK_PQD_MOT_POW_MEAS } #endif return (PowerW); } /** * @} */ /** * @} */ /************************ (C) COPYRIGHT 2023 STMicroelectronics *****END OF FILE****/
4,818
C
30.292208
117
0.636779
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/MCSDK_v6.2.0-Full/MotorControl/MCSDK/MCLib/Any/Src/digital_output.c
/** ****************************************************************************** * @file digital_output.c * @author Motor Control SDK Team, ST Microelectronics * @brief This file provides firmware functions that implement the Digital * Output component of the Motor Control SDK: * ****************************************************************************** * @attention * * <h2><center>&copy; Copyright (c) 2023 STMicroelectronics. * All rights reserved.</center></h2> * * This software component is licensed by ST under Ultimate Liberty license * SLA0044, the "License"; You may not use this file except in compliance with * the License. You may obtain a copy of the License at: * www.st.com/SLA0044 * ****************************************************************************** * @ingroup DigitalOutput */ /* Includes ------------------------------------------------------------------*/ #include "digital_output.h" #include "mc_type.h" /** @addtogroup MCSDK * @{ */ /** @defgroup DigitalOutput Digital Output * @brief Digital output component of the Motor Control SDK * * @{ */ /** * @brief Accordingly with selected polarity, it sets to active or inactive the * digital output * @param pHandle handler address of the digital output component. * @param State New requested state * */ __weak void DOUT_SetOutputState(DOUT_handle_t *pHandle, DOutputState_t State) { #ifdef NULL_PTR_CHECK_DIG_OUT if (MC_NULL == pHandle) { /* Nothing to do */ } else { #endif if (ACTIVE == State) { if (pHandle->bDOutputPolarity == DOutputActiveHigh) { LL_GPIO_SetOutputPin(pHandle->hDOutputPort, pHandle->hDOutputPin); } else { LL_GPIO_ResetOutputPin(pHandle->hDOutputPort, pHandle->hDOutputPin); } } else if (pHandle->bDOutputPolarity == DOutputActiveHigh) { LL_GPIO_ResetOutputPin(pHandle->hDOutputPort, pHandle->hDOutputPin); } else { LL_GPIO_SetOutputPin(pHandle->hDOutputPort, pHandle->hDOutputPin); } pHandle->OutputState = State; #ifdef NULL_PTR_CHECK_DIG_OUT } #endif } /** * @brief It returns the state of the digital output * @param pHandle pointer on related component instance * @retval Digital output state (ACTIVE or INACTIVE) */ __weak DOutputState_t DOUT_GetOutputState(DOUT_handle_t *pHandle) //cstat !MISRAC2012-Rule-8.13 { #ifdef NULL_PTR_CHECK_DIG_OUT DOutputState_t temp_outputState; if (MC_NULL == pHandle) { temp_outputState = INACTIVE; } else { temp_outputState = pHandle->OutputState; } return (temp_outputState); #else return (pHandle->OutputState); #endif } /** * @} */ /** * @} */ /************************ (C) COPYRIGHT 2023 STMicroelectronics *****END OF FILE****/
2,871
C
24.415929
95
0.575409
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/MCSDK_v6.2.0-Full/MotorControl/MCSDK/MCLib/Any/Src/pwm_common_sixstep.c
/** ****************************************************************************** * @file pwm_common_sixstep.c * @author Motor Control SDK Team, ST Microelectronics * @brief This file provides firmware functions that implement the following features * of the six-step PWM component of the Motor Control SDK: * * * ADC triggering for sensorless bemf acquisition * * translation of the electrical angle in step sequence * ****************************************************************************** * @attention * * <h2><center>&copy; Copyright (c) 2023 STMicroelectronics. * All rights reserved.</center></h2> * * This software component is licensed by ST under Ultimate Liberty license * SLA0044, the "License"; You may not use this file except in compliance with * the License. You may obtain a copy of the License at: * www.st.com/SLA0044 * ****************************************************************************** * @ingroup pwm_curr_fdbk_6s */ /* Includes ------------------------------------------------------------------*/ #include "pwm_common_sixstep.h" /** @addtogroup MCSDK * @{ */ /** @addtogroup pwm_curr_fdbk * @{ */ /** @defgroup pwm_curr_fdbk_6s Six-Step PWM generation * * @brief PWM components for Six Step drive * * @todo Complete documentation for the pwm_curr_fdbk_6s module * * @{ */ /** * @brief It is used to clear the variable in CPWMC. * @param this related object of class CPWMC * @retval none */ void PWMC_Clear(PWMC_Handle_t *pHandle) { } /** * @brief Switches PWM generation off, inactivating the outputs. * @param pHandle Handle on the target instance of the PWMC component */ __weak void PWMC_SwitchOffPWM( PWMC_Handle_t * pHandle ) { pHandle->pFctSwitchOffPwm( pHandle ); } /** * @brief Switches PWM generation on * @param pHandle Handle on the target instance of the PWMC component */ __weak void PWMC_SwitchOnPWM( PWMC_Handle_t * pHandle ) { pHandle->pFctSwitchOnPwm( pHandle ); } /** * @brief Set the ADC trigger point for bemf acquisition. * @param pHandle Handle on the target instance of the PWMC component * @param SamplingPoint pulse value of the timer channel used for ADC triggering */ __weak void PWMC_SetADCTriggerChannel( PWMC_Handle_t * pHandle, uint16_t SamplingPoint ) { pHandle->pFctSetADCTriggerChannel( pHandle, SamplingPoint ); } /** * @brief Switches power stage Low Sides transistors on. * * This function is meant for charging boot capacitors of the driving * section. It has to be called on each motor start-up when using high * voltage drivers. * * @param pHandle: handle on the target instance of the PWMC component */ __weak void PWMC_TurnOnLowSides( PWMC_Handle_t * pHandle, uint32_t ticks) { pHandle->pFctTurnOnLowSides(pHandle, ticks); } /** * @brief It is used to retrieve the satus of TurnOnLowSides action. * @param pHandle: handler of the current instance of the PWMC component * @retval bool It returns the state of TurnOnLowSides action: * true if TurnOnLowSides action is active, false otherwise. */ /** @brief Returns the status of the "TurnOnLowSide" action on the power stage * controlled by the @p pHandle PWMC component: true if it * is active, false otherwise*/ __weak bool PWMC_GetTurnOnLowSidesAction( PWMC_Handle_t * pHandle ) { return pHandle->TurnOnLowSidesAction; } /** * @brief It is used to set the align motor flag. * @param this related object of class CPWMC * @param flag to be applied in uint8_t, 1: motor is in align stage, 2: motor is not in align stage * @retval none */ void PWMC_SetAlignFlag(PWMC_Handle_t *pHandle, int16_t flag) { pHandle->AlignFlag = flag; } /** * @brief Sets the Callback that the PWMC component shall invoke to switch PWM * generation off. * @param pCallBack pointer on the callback * @param pHandle pointer on the handle structure of the PWMC instance * */ __weak void PWMC_RegisterSwitchOffPwmCallBack( PWMC_Generic_Cb_t pCallBack, PWMC_Handle_t * pHandle ) { pHandle->pFctSwitchOffPwm = pCallBack; } /** * @brief Sets the Callback that the PWMC component shall invoke to switch PWM * generation on. * @param pCallBack pointer on the callback * @param pHandle pointer on the handle structure of the PWMC instance * */ __weak void PWMC_RegisterSwitchonPwmCallBack( PWMC_Generic_Cb_t pCallBack, PWMC_Handle_t * pHandle ) { pHandle->pFctSwitchOnPwm = pCallBack; } /** * @brief Sets the Callback that the PWMC component shall invoke to turn low sides on. * @param pCallBack pointer on the callback * @param pHandle pointer on the handle structure of the PWMC instance * */ __weak void PWMC_RegisterTurnOnLowSidesCallBack( PWMC_TurnOnLowSides_Cb_t pCallBack, PWMC_Handle_t * pHandle ) { pHandle->pFctTurnOnLowSides = pCallBack; } /** * @brief Sets the Callback that the PWMC component shall invoke to the overcurrent status * @param pCallBack pointer on the callback * @param pHandle pointer on the handle structure of the PWMC instance * */ __weak void PWMC_RegisterIsOverCurrentOccurredCallBack( PWMC_OverCurr_Cb_t pCallBack, PWMC_Handle_t * pHandle ) { pHandle->pFctIsOverCurrentOccurred = pCallBack; } /** * @brief It forces the Fast Demag interval to the passed value * @param pHandle: handler of the current instance of the PWM component * @param uint16_t: period where the fast demagnetization is applied * @retval none */ void PWMC_ForceFastDemagTime(PWMC_Handle_t * pHandle, uint16_t constFastDemagTime ) { pHandle->DemagCounterThreshold = constFastDemagTime; } /** * @brief It enables/disables the Fast Demag feature at next step change * @param pHandle: handler of the current instance of the PWM component * @param uint8_t: 0=disable, 1=enable * @retval none */ void PWMC_SetFastDemagState(PWMC_Handle_t * pHandle, uint8_t State ) { if (State == 1) { pHandle->ModUpdateReq = ENABLE_FAST_DEMAG; } else { pHandle->ModUpdateReq = DISABLE_FAST_DEMAG; } } /** * @brief It enables/disables the Qusi Synch feature at next step change * @param pHandle: handler of the current instance of the PWM component * @param uint8_t: 0=disable, 1=enable * @retval none */ void PWMC_SetQuasiSynchState(PWMC_Handle_t * pHandle, uint8_t State ) { if (State == 1) { pHandle->ModUpdateReq = ENABLE_QUASI_SYNCH; } else { pHandle->ModUpdateReq = DISABLE_QUASI_SYNCH; } } /** * @brief It returns the Fast Demag feature status * @param pHandle: handler of the current instance of the PWM component * @retval uint8_t: 0=disabled, 1=enabled */ uint8_t PWMC_GetFastDemagState(PWMC_Handle_t * pHandle ) { return ((MC_NULL == pHandle->pGetFastDemagFlag) ? 0 : pHandle->pGetFastDemagFlag(pHandle)); } /** * @brief It returns the Quasi Synch feature status * @param pHandle: handler of the current instance of the PWM component * @retval uint8_t: 0=disabled, 1=enabled */ uint8_t PWMC_GetQuasiSynchState(PWMC_Handle_t * pHandle ) { return ((MC_NULL == pHandle->pGetQuasiSynchFlag) ? 0 : pHandle->pGetQuasiSynchFlag(pHandle)); } /** * @brief Converts the motor electrical angle to the corresponding step in the six-step sequence * @param pHandle pointer on the handle structure of the PWMC instance * @retval calculated step */ __weak uint8_t PWMC_ElAngleToStep( PWMC_Handle_t * pHandle ) { uint8_t Step; if ((pHandle->hElAngle >= (int16_t)( S16_60_PHASE_SHIFT / 2)) && (pHandle->hElAngle < (int16_t)( S16_60_PHASE_SHIFT + S16_60_PHASE_SHIFT / 2))) Step = STEP_1; else if ((pHandle->hElAngle >= (int16_t)( S16_60_PHASE_SHIFT + S16_60_PHASE_SHIFT / 2)) && (pHandle->hElAngle < (int16_t)( S16_120_PHASE_SHIFT + S16_60_PHASE_SHIFT / 2))) Step = STEP_2; else if ((pHandle->hElAngle >= (int16_t)( S16_120_PHASE_SHIFT + S16_60_PHASE_SHIFT / 2)) || (pHandle->hElAngle < (int16_t)( - S16_120_PHASE_SHIFT - S16_60_PHASE_SHIFT / 2))) Step = STEP_3; else if ((pHandle->hElAngle >= (int16_t)( - S16_120_PHASE_SHIFT - S16_60_PHASE_SHIFT / 2)) && (pHandle->hElAngle < (int16_t)( - S16_60_PHASE_SHIFT - S16_60_PHASE_SHIFT / 2))) Step = STEP_4; else if ((pHandle->hElAngle >= (int16_t)( - S16_60_PHASE_SHIFT - S16_60_PHASE_SHIFT / 2)) && (pHandle->hElAngle < (int16_t)( - S16_60_PHASE_SHIFT / 2))) Step = STEP_5; else if ((pHandle->hElAngle >= (int16_t)( - S16_60_PHASE_SHIFT / 2)) && (pHandle->hElAngle < (int16_t)( S16_60_PHASE_SHIFT / 2))) Step = STEP_6; else {} return Step; } /* * @brief Checks if an overcurrent occurred since last call. * * @param pHdl: Handler of the current instance of the PWM component. * @retval uint16_t Returns #MC_OVER_CURR if an overcurrent has been * detected since last method call, #MC_NO_FAULTS otherwise. */ __weak uint16_t PWMC_IsFaultOccurred(PWMC_Handle_t *pHandle) { uint16_t retVal = MC_NO_FAULTS; if (true == pHandle->OverVoltageFlag) { retVal = MC_OVER_VOLT; pHandle->OverVoltageFlag = false; } else { /* Nothing to do */ } if (true == pHandle->OverCurrentFlag) { retVal |= MC_OVER_CURR; pHandle->OverCurrentFlag = false; } else { /* Nothing to do */ } if (true == pHandle->driverProtectionFlag) { retVal |= MC_DP_FAULT; pHandle->driverProtectionFlag = false; } else { /* Nothing to do */ } return (retVal); } /** * @} */ /** * @} */ /** * @} */ /************************ (C) COPYRIGHT 2023 STMicroelectronics *****END OF FILE****/
9,715
C
29.844444
191
0.652702
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/MCSDK_v6.2.0-Full/MotorControl/MCSDK/MCLib/Any/Src/virtual_speed_sensor.c
/** ****************************************************************************** * @file virtual_speed_sensor.c * @author Motor Control SDK Team, ST Microelectronics * @brief This file provides firmware functions that implement the features * of the Virtual Speed Sensor component of the Motor Control SDK. * ****************************************************************************** * @attention * * <h2><center>&copy; Copyright (c) 2023 STMicroelectronics. * All rights reserved.</center></h2> * * This software component is licensed by ST under Ultimate Liberty license * SLA0044, the "License"; You may not use this file except in compliance with * the License. You may obtain a copy of the License at: * www.st.com/SLA0044 * ****************************************************************************** * @ingroup VirtualSpeedSensor */ /* Includes ------------------------------------------------------------------*/ #include "virtual_speed_sensor.h" /** @addtogroup MCSDK * @{ */ /** @addtogroup SpeednPosFdbk * @{ */ /** @defgroup VirtualSpeedSensor Virtual Speed & Position Feedback * @brief Virtual Speed Speed & Position Feedback implementation * * This component provides a "virtual" implementation of the speed and position feedback features. * This implementation provides a theoretical estimation of the speed and position of the rotor of * the motor based on a mechanical acceleration and an initial angle set by the application. * * This component is used during the @ref RevUpCtrl "Rev-Up Control" phases of the motor or in an * @ref OpenLoop "Open Loop Control" configuration in a sensorless subsystem. * * * @{ */ /** * @brief Software initialization of VirtualSpeedSensor component. * @param pHandle: handler of the current instance of the VirtualSpeedSensor component. * @retval none * * - Calls VSS_Clear. * - Called at initialization of the whole MC core. */ __weak void VSS_Init(VirtualSpeedSensor_Handle_t *pHandle) { VSS_Clear(pHandle); } /** * @brief Software initialization of VSS object to be performed at each restart * of the motor. * @param pHandle: handler of the current instance of the VirtualSpeedSensor component. * @retval none */ __weak void VSS_Clear(VirtualSpeedSensor_Handle_t *pHandle) { #ifdef NULL_PTR_CHECK_VIR_SPD_SEN if (MC_NULL == pHandle) { /* Nothing to do */ } else { #endif pHandle->_Super.bSpeedErrorNumber = 0U; pHandle->_Super.hElAngle = 0; pHandle->_Super.hMecAngle = 0; pHandle->_Super.hAvrMecSpeedUnit = 0; pHandle->_Super.hElSpeedDpp = 0; pHandle->_Super.hMecAccelUnitP = 0; pHandle->_Super.bSpeedErrorNumber = 0U; pHandle->wElAccDppP32 = 0; pHandle->wElSpeedDpp32 = 0; pHandle->hRemainingStep = 0U; pHandle->hElAngleAccu = 0; pHandle->bTransitionStarted = false; pHandle->bTransitionEnded = false; pHandle->hTransitionRemainingSteps = pHandle->hTransitionSteps; pHandle->bTransitionLocked = false; pHandle->bCopyObserver = false; #ifdef NULL_PTR_CHECK_VIR_SPD_SEN } #endif } #if defined (CCMRAM) #if defined (__ICCARM__) #pragma location = ".ccmram" #elif defined (__CC_ARM) || defined(__GNUC__) __attribute__((section(".ccmram"))) #endif #endif /** * @brief Updates the rotor electrical angle integrating the last settled * instantaneous electrical speed express in [dpp](measurement_units.md). * @param pHandle: handler of the current instance of the VirtualSpeedSensor component. * @retval int16_t Measured electrical angle in s16degree format. * * - Systematically called after #SPD_GetElAngle that retrieves last computed rotor electrical angle. */ //cstat !MISRAC2012-Rule-8.13 __weak int16_t VSS_CalcElAngle(VirtualSpeedSensor_Handle_t *pHandle, int16_t *pInputVars_str) { int16_t hRetAngle; #ifdef NULL_PTR_CHECK_VIR_SPD_SEN if ((MC_NULL == pHandle) || (MC_NULL == pInputVars_str)) { hRetAngle = 0; } else { #endif int16_t hAngleDiff; int32_t wAux; int16_t hAngleCorr; int16_t hSignCorr = 1; if (true == pHandle->bCopyObserver) { hRetAngle = *(int16_t *)pInputVars_str; } else { pHandle->hElAngleAccu += pHandle->_Super.hElSpeedDpp; pHandle->_Super.hMecAngle += (pHandle->_Super.hElSpeedDpp / (int16_t)pHandle->_Super.bElToMecRatio); if (true == pHandle->bTransitionStarted) { if (0 == pHandle->hTransitionRemainingSteps) { hRetAngle = *(int16_t *)pInputVars_str; pHandle->bTransitionEnded = true; pHandle->_Super.bSpeedErrorNumber = 0U; } else { pHandle->hTransitionRemainingSteps--; if (pHandle->_Super.hElSpeedDpp >= 0) { hAngleDiff = *(int16_t *)pInputVars_str - pHandle->hElAngleAccu; } else { hAngleDiff = pHandle->hElAngleAccu - *(int16_t *)pInputVars_str; hSignCorr = -1; } wAux = (int32_t)hAngleDiff * pHandle->hTransitionRemainingSteps; hAngleCorr = (int16_t)(wAux / pHandle->hTransitionSteps); hAngleCorr *= hSignCorr; if (hAngleDiff >= 0) { pHandle->bTransitionLocked = true; hRetAngle = *(int16_t *)pInputVars_str - hAngleCorr; } else { if (false == pHandle->bTransitionLocked) { hRetAngle = pHandle->hElAngleAccu; } else { hRetAngle = *(int16_t *)pInputVars_str + hAngleCorr; } } } } else { hRetAngle = pHandle->hElAngleAccu; } } pHandle->_Super.hElAngle = hRetAngle; #ifdef NULL_PTR_CHECK_VIR_SPD_SEN } #endif return (hRetAngle); } /** * @brief Computes and stores rotor instantaneous electrical speed (express * in [dpp](measurement_units.md) considering the measurement frequency) in order to provide it * to #SPD_GetElAngle. * @param pHandle: handler of the current instance of the VirtualSpeedSensor component. * @param hMecSpeedUnit pointer to int16_t, used to return the rotor average * mechanical speed [SPEED_UNIT](measurement_units.md). * @retval bool true = sensor information is reliable and false = sensor information is not reliable. * * - Stores and returns through parameter hMecSpeedUnit the rotor average mechanical speed, * expressed in the unit defined by [SPEED_UNIT](measurement_units.md). * - Returns the reliability state of the sensor (always true). * - Called with the same periodicity on which speed control is executed, precisely during START and SWITCH_OVER states * of the MC tasks state machine or in its RUM state in @ref OpenLoop "Open Loop Control" configuration into * TSK_MediumFrequencyTask. */ __weak bool VSS_CalcAvrgMecSpeedUnit(VirtualSpeedSensor_Handle_t *pHandle, int16_t *hMecSpeedUnit) { bool SpeedSensorReliability; #ifdef NULL_PTR_CHECK_VIR_SPD_SEN if ((MC_NULL == pHandle) || (MC_NULL == hMecSpeedUnit)) { SpeedSensorReliability = false; } else { #endif if (pHandle->hRemainingStep > 1u) { pHandle->wElSpeedDpp32 += pHandle->wElAccDppP32; #ifndef FULL_MISRA_C_COMPLIANCY_VIRT_SPD_SENS //cstat !MISRAC2012-Rule-1.3_n !ATH-shift-neg !MISRAC2012-Rule-10.1_R6 pHandle->_Super.hElSpeedDpp = (int16_t)(pHandle->wElSpeedDpp32 >> 16); #else pHandle->_Super.hElSpeedDpp = (int16_t)(pHandle->wElSpeedDpp32 / 65536); #endif /* Convert dpp into MecUnit */ *hMecSpeedUnit = (int16_t)((((int32_t)pHandle->_Super.hElSpeedDpp) * ((int32_t )pHandle->_Super.hMeasurementFrequency) * SPEED_UNIT) / (((int32_t)pHandle->_Super.DPPConvFactor) * ((int32_t)pHandle->_Super.bElToMecRatio))); pHandle->_Super.hAvrMecSpeedUnit = *hMecSpeedUnit; pHandle->hRemainingStep--; } else if (1U == pHandle->hRemainingStep) { *hMecSpeedUnit = pHandle->hFinalMecSpeedUnit; pHandle->_Super.hAvrMecSpeedUnit = *hMecSpeedUnit; pHandle->_Super.hElSpeedDpp = (int16_t)((((int32_t)*hMecSpeedUnit) * ((int32_t)pHandle->_Super.DPPConvFactor)) / (((int32_t)SPEED_UNIT) * ((int32_t)pHandle->_Super.hMeasurementFrequency))); pHandle->_Super.hElSpeedDpp *= ((int16_t)pHandle->_Super.bElToMecRatio); pHandle->hRemainingStep = 0U; } else { *hMecSpeedUnit = pHandle->_Super.hAvrMecSpeedUnit; } /* If the transition is not done yet, we already know that speed is not reliable */ if (false == pHandle->bTransitionEnded) { pHandle->_Super.bSpeedErrorNumber = pHandle->_Super.bMaximumSpeedErrorsNumber; SpeedSensorReliability = false; } else { SpeedSensorReliability = SPD_IsMecSpeedReliable(&pHandle->_Super, hMecSpeedUnit); } #ifdef NULL_PTR_CHECK_VIR_SPD_SEN } #endif return (SpeedSensorReliability); } /** * @brief Sets instantaneous information on VSS mechanical and electrical angle. * @param pHandle: handler of the current instance of the VirtualSpeedSensor component. * @param hMecAngle: instantaneous measure of rotor mechanical angle. * @retval none * * - Called during @ref RevUpCtrl "Rev-Up Control" and * @ref EncAlignCtrl "Encoder Alignment Controller procedure" initialization. */ __weak void VSS_SetMecAngle(VirtualSpeedSensor_Handle_t *pHandle, int16_t hMecAngle) { #ifdef NULL_PTR_CHECK_VIR_SPD_SEN if (MC_NULL == pHandle) { /* Nothing to do */ } else { #endif pHandle->hElAngleAccu = hMecAngle; pHandle->_Super.hMecAngle = pHandle->hElAngleAccu / ((int16_t)pHandle->_Super.bElToMecRatio); pHandle->_Super.hElAngle = hMecAngle; #ifdef NULL_PTR_CHECK_VIR_SPD_SEN } #endif } /** * @brief Sets the mechanical acceleration of virtual speed sensor. * @param pHandle: handler of the current instance of the VirtualSpeedSensor component. * @param hFinalMecSpeedUnit mechanical speed assumed by * the virtual speed sensor at the end of the duration. Expressed in the unit defined * by [SPEED_UNIT](measurement_units.md). * @param hDurationms: Duration expressed in ms. It can be 0 to apply * instantaneous the final speed. * @retval none * * - This acceleration is defined starting from current mechanical speed, final mechanical * speed expressed in [SPEED_UNIT](measurement_units.md) and duration expressed in milliseconds. * - Called during @ref RevUpCtrl "Rev-Up Control" and * @ref EncAlignCtrl "Encoder Alignment Controller procedure" initialization. */ __weak void VSS_SetMecAcceleration(VirtualSpeedSensor_Handle_t *pHandle, int16_t hFinalMecSpeedUnit, uint16_t hDurationms) { #ifdef NULL_PTR_CHECK_VIR_SPD_SEN if (MC_NULL == pHandle) { /* Nothing to do */ } else { #endif int32_t wMecAccDppP32; uint16_t hNbrStep; int16_t hCurrentMecSpeedDpp; int16_t hFinalMecSpeedDpp; if (false == pHandle->bTransitionStarted) { if (0U == hDurationms) { pHandle->_Super.hAvrMecSpeedUnit = hFinalMecSpeedUnit; pHandle->_Super.hElSpeedDpp = (int16_t)((((int32_t)hFinalMecSpeedUnit) * ((int32_t)pHandle->_Super.DPPConvFactor)) / (((int32_t)SPEED_UNIT) * ((int32_t)pHandle->_Super.hMeasurementFrequency))); pHandle->_Super.hElSpeedDpp *= ((int16_t)pHandle->_Super.bElToMecRatio); pHandle->hRemainingStep = 0U; pHandle->hFinalMecSpeedUnit = hFinalMecSpeedUnit; } else { hNbrStep = (uint16_t)((((uint32_t)hDurationms) * ((uint32_t)pHandle->hSpeedSamplingFreqHz)) / 1000U); hNbrStep++; pHandle->hRemainingStep = hNbrStep; hCurrentMecSpeedDpp = pHandle->_Super.hElSpeedDpp / ((int16_t)pHandle->_Super.bElToMecRatio); hFinalMecSpeedDpp = (int16_t)((((int32_t )hFinalMecSpeedUnit) * ((int32_t)pHandle->_Super.DPPConvFactor)) / (((int32_t )SPEED_UNIT) * ((int32_t)pHandle->_Super.hMeasurementFrequency))); if (0U == hNbrStep) { /* Nothing to do */ } else { wMecAccDppP32 = ((((int32_t)hFinalMecSpeedDpp) - ((int32_t)hCurrentMecSpeedDpp)) * ((int32_t)65536)) / ((int32_t )hNbrStep); pHandle->wElAccDppP32 = wMecAccDppP32 * ((int16_t)pHandle->_Super.bElToMecRatio); } pHandle->hFinalMecSpeedUnit = hFinalMecSpeedUnit; pHandle->wElSpeedDpp32 = ((int32_t)pHandle->_Super.hElSpeedDpp) * ((int32_t)65536); } } else { /* Nothing to do */ } #ifdef NULL_PTR_CHECK_VIR_SPD_SEN } #endif } /** * @brief Checks if the ramp executed after a #VSS_SetMecAcceleration command * has been completed by checking zero value of the Number of steps remaining to reach the final * speed. * @param pHandle: handler of the current instance of the VirtualSpeedSensor component. * @retval bool: true if the ramp is completed, otherwise false. * * - Not used into current implementation. */ //cstat !MISRAC2012-Rule-8.13 __weak bool VSS_RampCompleted(VirtualSpeedSensor_Handle_t *pHandle) { bool retVal = false; #ifdef NULL_PTR_CHECK_VIR_SPD_SEN if (MC_NULL == pHandle) { /* nothing to do */ } else { #endif if (0U == pHandle->hRemainingStep) { retVal = true; } else { /* Nothing to do */ } #ifdef NULL_PTR_CHECK_VIR_SPD_SEN } #endif return (retVal); } /** * @brief Gets the final speed of last settled ramp of virtual speed sensor expressed in [SPEED_UNIT](measurement_units.md). * @param pHandle: handler of the current instance of the VirtualSpeedSensor component. * @retval none * * - Will be call for future dual motor implementation into START state of MC tasks state machine into TSK_MediumFrequencyTask. */ //cstat !MISRAC2012-Rule-8.13 __weak int16_t VSS_GetLastRampFinalSpeed(VirtualSpeedSensor_Handle_t *pHandle) { #ifdef NULL_PTR_CHECK_VIR_SPD_SEN return ((MC_NULL == pHandle) ? 0 : pHandle->hFinalMecSpeedUnit); #else return (pHandle->hFinalMecSpeedUnit); #endif } /** * @brief Sets the command to Start the transition phase from Virtual Speed Sensor * to other Speed Sensor. * @param pHandle: handler of the current instance of the VirtualSpeedSensor component. * @param bool: true to Start the transition phase, false has no effect. * @retval bool: true if Transition phase is enabled (started or not), false if * transition has been triggered but it's actually disabled * (parameter #hTransitionSteps = 0). * * - Transition is to be considered ended when Sensor information is * declared 'Reliable' or if function returned value is false. * - Called into START state of MC tasks state machine into TSK_MediumFrequencyTask. */ __weak bool VSS_SetStartTransition(VirtualSpeedSensor_Handle_t *pHandle, bool bCommand) { bool bAux = true; #ifdef NULL_PTR_CHECK_VIR_SPD_SEN if (MC_NULL == pHandle) { /* nothing to do */ } else { #endif if (true == bCommand) { pHandle->bTransitionStarted = true; if (0 == pHandle->hTransitionSteps) { pHandle->bTransitionEnded = true; pHandle->_Super.bSpeedErrorNumber = 0U; bAux = false; } else { /* Nothing to do */ } } else { /* Nothing to do */ } #ifdef NULL_PTR_CHECK_VIR_SPD_SEN } #endif return (bAux); } /** * @brief Returns the status of the transition phase by checking the status of the two parameters * @ref VirtualSpeedSensor_Handle_t::bTransitionStarted "bTransitionStarted" and * @ref VirtualSpeedSensor_Handle_t::bTransitionEnded "bTransitionEnded". * @param pHandle: handler of the current instance of the VirtualSpeedSensor components * @retval bool: true if Transition phase is ongoing, false otherwise. * * - Not used into current implementation. */ //cstat !MISRAC2012-Rule-8.13 __weak bool VSS_IsTransitionOngoing(VirtualSpeedSensor_Handle_t *pHandle) { bool retVal = false; #ifdef NULL_PTR_CHECK_VIR_SPD_SEN if (MC_NULL == pHandle) { /* nothing to do */ } else { #endif uint16_t hTS = 0U; uint16_t hTE = 0U; uint16_t hAux; if (true == pHandle->bTransitionStarted) { hTS = 1U; } else { /* Nothing to do */ } if (true == pHandle->bTransitionEnded) { hTE = 1U; } else { /* Nothing to do */ } hAux = hTS ^ hTE; if (hAux != 0U) { retVal = true; } else { /* Nothing to do */ } #ifdef NULL_PTR_CHECK_VIR_SPD_SEN } #endif return (retVal); } /** * @brief Returns the ending status of the transition phase by checking the parameter * @ref VirtualSpeedSensor_Handle_t::bTransitionEnded "bTransitionEnded". * @param pHandle: handler of the current instance of the VirtualSpeedSensor components * @retval bool: true if Transition phase ended, false otherwise. * * - Called into SWITCH_OVER state of MC tasks state machine into TSK_MediumFrequencyTask. */ //cstat !MISRAC2012-Rule-8.13 __weak bool VSS_TransitionEnded(VirtualSpeedSensor_Handle_t *pHandle) { #ifdef NULL_PTR_CHECK_VIR_SPD_SEN return ((MC_NULL == pHandle) ? false : pHandle->bTransitionEnded); #else return (pHandle->bTransitionEnded); #endif } /** * @brief Sets instantaneous information on rotor electrical angle same as copied by state observer. * @param pHandle: handler of the current instance of the VirtualSpeedSensor component. * @retval none * * - Not used into current implementation. */ __weak void VSS_SetCopyObserver(VirtualSpeedSensor_Handle_t *pHandle) { #ifdef NULL_PTR_CHECK_VIR_SPD_SEN if (MC_NULL == pHandle) { /* Nothing to do */ } else { #endif pHandle->bCopyObserver = true; #ifdef NULL_PTR_CHECK_VIR_SPD_SEN } #endif } /** * @brief Sets instantaneous information on rotor electrical angle. * @param pHandle: handler of the current instance of the VirtualSpeedSensor component. * @param hElAngle instantaneous measure of rotor electrical angle in [s16degrees](measurement_units.md). * @retval none * * - Not used into current implementation. */ __weak void VSS_SetElAngle(VirtualSpeedSensor_Handle_t *pHandle, int16_t hElAngle) { #ifdef NULL_PTR_CHECK_VIR_SPD_SEN if (MC_NULL == pHandle) { /* Nothing to do */ } else { #endif pHandle->hElAngleAccu = hElAngle; pHandle->_Super.hElAngle = hElAngle; #ifdef NULL_PTR_CHECK_VIR_SPD_SEN } #endif } /** * @} */ /** * @} */ /** * @} */ /************************ (C) COPYRIGHT 2023 STMicroelectronics *****END OF FILE****/
19,123
C
29.845161
128
0.638707
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/MCSDK_v6.2.0-Full/MotorControl/MCSDK/MCLib/Any/Src/profiler.c
/** ****************************************************************************** * @file profiler.c * @author Motor Control SDK Team, ST Microelectronics * @brief This file provides firmware functions that implement * the profiler component of the Motor Control SDK. * ****************************************************************************** * @attention * * <h2><center>&copy; Copyright (c) 2023 STMicroelectronics. * All rights reserved.</center></h2> * * This software component is licensed by ST under Ultimate Liberty license * SLA0044, the "License"; You may not use this file except in compliance with * the License. You may obtain a copy of the License at: * www.st.com/SLA0044 * ****************************************************************************** * @ingroup Profiler */ /* Includes ------------------------------------------------------------------*/ #include "profiler.h" #include "drive_parameters.h" #include "parameters_conversion.h" /** @addtogroup MCSDK * @{ */ /** * @defgroup Profiler * * @brief Profiler component of the Motor Control SDK * * This procedure can be used to identify any PMSM. * * Once the profiler complete successfully the motor parameters @f$R_s,\ L_s@f$ and motor flux @f$\psi_{mag} @f$ are provided.\n * The estimated @f$R_s@f$ and @f$L_s@f$ are then used to define the PI settings (@f$K_p,\ K_i@f$) for current controllers. * * [Profiler](Profiler.md) * * @{ */ void PROFILER_stateMachine(PROFILER_Handle handle, MOTOR_Handle motor); /** * @brief Initializes Profiler component. * @param handle Profiler handler * @param pParams pointer on profiler parameters * @param flashParams pointer on flash parameters handler * */ void PROFILER_init(PROFILER_Handle handle, PROFILER_Params *pParams, FLASH_Params_t const *flashParams) { PROFILER_DCAC_init(&handle->dcacObj); PROFILER_FLUXESTIM_init(&handle->fluxestimObj); pParams->fullScaleFreq_Hz = flashParams->scale.frequency; pParams->fullScaleCurrent_A = flashParams->scale.current; pParams->fullScaleVoltage_V = flashParams->scale.voltage; pParams->PolePairs = flashParams->motor.polePairs; handle->PolePairs = pParams->PolePairs; handle->PowerDC_goal_W = pParams->dcac_PowerDC_goal_W; handle->fullScaleFreq_Hz = pParams->fullScaleFreq_Hz; handle->fullScaleVoltage_V = pParams->fullScaleVoltage_V; PROFILER_DCAC_setParams(&handle->dcacObj, pParams); PROFILER_FLUXESTIM_setParams(&handle->fluxestimObj, pParams); } /** * Resets Profiler to restart new measure * param pHandle Profiler handler * param motor handler * */ void PROFILER_reset(PROFILER_Handle handle, MOTOR_Handle motor) { handle->state = PROFILER_STATE_Idle; handle->command = PROFILER_COMMAND_None; handle->error = PROFILER_ERROR_None; PROFILER_DCAC_reset(&handle->dcacObj, motor); PROFILER_FLUXESTIM_reset(&handle->fluxestimObj, motor); /* Clear result variables */ PROFILER_resetEstimates(handle); } /** * @brief Executes profiler procedure * @param handle Profiler handler * @param motor motor handler * */ void PROFILER_run(PROFILER_Handle handle, MOTOR_Handle motor) { /* Called from the motor control interrupt */ /* Required because some data can only be calculated in the interrupt (or at least synchronous to isr) * Slow/demanding calculations are not allowed */ switch (handle->state) { case PROFILER_STATE_DCandACcheck: PROFILER_DCAC_run(&handle->dcacObj, motor); break; case PROFILER_STATE_FluxEstim: PROFILER_FLUXESTIM_run(&handle->fluxestimObj, motor); break; case PROFILER_STATE_Idle: /* No break */ case PROFILER_STATE_Complete: /* No break */ case PROFILER_STATE_Error: case PROFILER_STATE_DCAC_Error: case PROFILER_STATE_FluxEstim_Error: /* Nothing to do in these states */ break; } } /** * @brief Executes profiling in background task. * @param handle Profiler handler * @param motor parameters * */ void PROFILER_runBackground(PROFILER_Handle handle, MOTOR_Handle motor) { /* Called from slow control loop, outside of ISR */ /* Does nothing when profiler is not active */ if ((handle->state == PROFILER_STATE_Idle || handle->state == PROFILER_STATE_Complete) && (handle->command != PROFILER_COMMAND_Start) && handle->command != PROFILER_COMMAND_Reset) return; /* Run the profiler top-level state machine */ PROFILER_stateMachine(handle, motor); /* Run the lower level profilers */ switch(handle->state) { case PROFILER_STATE_DCandACcheck: PROFILER_DCAC_runBackground(&handle->dcacObj, motor); break; case PROFILER_STATE_FluxEstim: PROFILER_FLUXESTIM_runBackground(&handle->fluxestimObj, motor); break; case PROFILER_STATE_Complete: case PROFILER_STATE_Error: case PROFILER_STATE_DCAC_Error: case PROFILER_STATE_FluxEstim_Error: motor->pSPD->zestControl.injectFreq_kHz = FIXP30(80.0f / 1000.0f); // switch to 80Hz injection frequency after profiling; motor->pSPD->zestControl.injectId_A_pu = 0; // switch to 0 injection after profiling; motor->pSPD->zestControl.feedbackGainD = 0; // reset gainD after profiling motor->pSPD->zestControl.feedbackGainQ = 0; // reset gainQ after profiling motor->pCurrCtrl->Ddq_ref_pu.D = 0 ; break; case PROFILER_STATE_Idle: /* No break */ /* Nothing to do in these states */ break; } } /** * @brief Set PWM off and disable current controller * @param handle Profiler handler * @param motor parameters * */ void PROFILER_setMotorToIdle(PROFILER_Handle handle, MOTOR_Handle motor) { motor->pCurrCtrl->forceZeroPwm = true; motor->pCurrCtrl->currentControlEnabled = false; } /** * @brief Check profiler state before to start profiling procedure * @param handle Profiler handler * @param motor parameters * */ PROFILER_Error_e PROFILER_isReadyToStart(PROFILER_Handle handle, MOTOR_Handle motor) { // ToDo: Check PWM is active (Requires reference to hardware layer from motorHandle) /* Must be in control mode 'None' */ // ToDo: Allow Profiler start in more controlModes if (motor->pFocVars->controlMode != MCM_PROFILING_MODE) return PROFILER_ERROR_NotReady; // ToDo: Check offset measurement performed (or known-good from EEPROM) if (motor->pCurrCtrl->forceZeroPwm == false) return PROFILER_ERROR_NotReady; /* Must be in ForceZeroPwm mode */ return PROFILER_ERROR_None; } /** * @brief Profiler top-level state machine * @param handle Profiler handler * @param motor parameters * */ void PROFILER_stateMachine(PROFILER_Handle handle, MOTOR_Handle motor) { PROFILER_State_e state = handle->state; PROFILER_State_e newState = state; PROFILER_Command_e command = handle->command; handle->command = PROFILER_COMMAND_None; switch (state) { case PROFILER_STATE_Idle: if (command == PROFILER_COMMAND_Start) { PROFILER_Error_e isReadyResult = PROFILER_isReadyToStart(handle, motor); if (isReadyResult == PROFILER_ERROR_None) { handle->error = PROFILER_ERROR_None; /* Reset sub-profilers */ PROFILER_DCAC_reset(&handle->dcacObj, motor); PROFILER_FLUXESTIM_reset(&handle->fluxestimObj, motor); PROFILER_resetEstimates(handle); /* Set up for DCAC measurement */ handle->dcacObj.PowerDC_goal_W = handle->PowerDC_goal_W; motor->pFocVars->controlMode = MCM_PROFILING_MODE; newState = PROFILER_STATE_DCandACcheck; } else { /* Cannot start profiling now */ handle->error = isReadyResult; newState = PROFILER_STATE_Error; } } break; case PROFILER_STATE_DCandACcheck: if ((handle->dcacObj.state == PROFILER_DCAC_STATE_Complete) || (handle->dcacObj.state == PROFILER_DCAC_STATE_Error)) { /* Fetch measured Rs and Ls */ handle->PowerDC_W = handle->dcacObj.PowerDC_W; handle->dutyDC = FIXP30_toF(handle->dcacObj.dc_duty_pu); handle->Idc_A = FIXP30_toF(handle->dcacObj.Id_dc_ref_pu) * handle->dcacObj.fullScaleCurrent_A; handle->Rs_dc = handle->dcacObj.Rs_dc; handle->Rs_ac = handle->dcacObj.Rs_inject; handle->Ld_H = handle->dcacObj.Ls_inject; handle->Lq_H = 0.0f; //todo handle->R_factor = handle->dcacObj.Rs_inject / handle->dcacObj.Rs_dc; handle->injectFreq_Hz = FIXP30_toF(handle->dcacObj.ac_freqkHz_pu) * 1000.0f; /* Copy value from DCAC test */ handle->fluxestimObj.Id_ref_pu = handle->dcacObj.Id_dc_ref_pu; handle->fluxestimObj.ramp_rate_A_pu_per_cycle = FIXP30_mpy(handle->fluxestimObj.Id_ref_pu, handle->fluxestimObj.CurToRamp_sf_pu); if (handle->dcacObj.state == PROFILER_DCAC_STATE_Error) { handle->error = PROFILER_ERROR_DCAC; newState = PROFILER_STATE_DCAC_Error; /* Clear profiling mode */ motor->pFocVars->controlMode = MCM_PROFILING_MODE; } else { newState = PROFILER_STATE_FluxEstim; } } break; case PROFILER_STATE_FluxEstim: if ((handle->fluxestimObj.state == PROFILER_FLUXESTIM_STATE_Complete) || (handle->fluxestimObj.state == PROFILER_FLUXESTIM_STATE_Error)) { /* Fetch measured Flux */ handle->freqEst_Hz = FIXP30_toF(handle->fluxestimObj.freqEst_pu) * handle->fullScaleFreq_Hz; handle->freqHSO_Hz = FIXP30_toF(handle->fluxestimObj.freqHSO_pu) * handle->fullScaleFreq_Hz; if (handle->fluxestimObj.state == PROFILER_FLUXESTIM_STATE_Complete) { handle->Flux_Wb = FIXP30_toF(handle->fluxestimObj.flux_Wb); handle->debug_Flux_VpHz = M_TWOPI * handle->Flux_Wb; /* Flux V/Hz, just for check*/ handle->debug_kV = 30.0f / (1.0000f * handle->PolePairs * handle->debug_Flux_VpHz); //rpm per Volt DC (drone motors) handle->KT_Nm_A = 1.5f * handle->PolePairs * handle->Flux_Wb; /* Torque per Amp (Nm/Apeak) */ handle->Isc_A = handle->Flux_Wb / handle->Ld_H; /* machine's short-circuit current */ /* Update PolePulse parameters */ POLPULSE_setCurrentGoal(motor->pPolpulse, 0.25f*handle->Isc_A); POLPULSE_setLsd(motor->pPolpulse, handle->Ld_H); handle->CritFreq_Hz = (handle->Rs_dc * handle->Idc_A) / handle->debug_Flux_VpHz; /* freq where Rs voltage drop would equal emf at used current level */ // suggested Kp_mech = 5 * rated current / rated speed: rated current is about float FullScaleFlux = handle->fullScaleVoltage_V * (float)(1.0f/TF_REGULATION_RATE); float RatedFlux_pu = (M_TWOPI * handle->Flux_Wb) / FullScaleFlux; float oneOverRatedFlux_pu = 1.0f / RatedFlux_pu; FIXPSCALED_floatToFIXPscaled(oneOverRatedFlux_pu, &motor->pSPD->oneOverFlux_pu_fps); motor->pFocVars->Kt = (1.0f / (1.5f * handle->PolePairs * handle->Flux_Wb)); motor->pSPD->flagDynamicQgain = false; newState = PROFILER_STATE_Complete; } else /* PROFILER_FLUXESTIM_STATE_Error */ { handle->Flux_Wb = 0.0; handle->debug_Flux_VpHz = 0.0; handle->debug_kV = 0.0; handle->KT_Nm_A = 0.0; handle->Isc_A = 0.0; handle->CritFreq_Hz = 0.0; handle->error = PROFILER_ERROR_FluxEstim; newState = PROFILER_STATE_FluxEstim_Error; } /* PWM off and related settings */ PROFILER_setMotorToIdle(handle, motor); /* Next state */ motor->pFocVars->controlMode = MCM_PROFILING_MODE; } break; case PROFILER_STATE_Complete: { PROFILER_DCAC_reset(&handle->dcacObj, motor); PROFILER_FLUXESTIM_reset(&handle->fluxestimObj, motor); /* Allow starting from Complete state */ if (command == PROFILER_COMMAND_Start) { newState = PROFILER_STATE_Idle; } } break; case PROFILER_STATE_Error: case PROFILER_STATE_DCAC_Error: case PROFILER_STATE_FluxEstim_Error: /* PWM off and related settings */ PROFILER_setMotorToIdle(handle, motor); break; } /* Reset is allowed in any state */ if (command == PROFILER_COMMAND_Reset) { PROFILER_reset(handle, motor); newState = PROFILER_STATE_Idle; } if (newState != state) { handle->state = newState; } } /* Accessors */ float_t PROFILER_getDcAcMeasurementTime(PROFILER_Handle handle) { return PROFILER_DCAC_getMeasurementTime(&handle->dcacObj); } /** * @brief Returns measured flux amplitude (in Weber) * @param pHandle Handle on the Profiler component */ float_t PROFILER_getFlux_Wb(const PROFILER_Handle handle) { return handle->Flux_Wb; } /** * @brief Returns estimated flux amplitude (in Hz) * @param pHandle Handle on the Profiler component */ float_t PROFILER_getFluxEstFreq_Hz(const PROFILER_Handle handle) { return PROFILER_FLUXESTIM_getFluxEstFreq_Hz(&handle->fluxestimObj); } /** * @brief Returns measurement time allowed to estimate the flux * @param pHandle Handle on the Profiler component */ float_t PROFILER_getFluxEstMeasurementTime(PROFILER_Handle handle) { return PROFILER_FLUXESTIM_getMeasurementTime(&handle->fluxestimObj); } /** * @brief Returns estimated inductance (in Henry) * @param pHandle Handle on the Profiler component */ float_t PROFILER_getLd_H(const PROFILER_Handle handle) { return handle->Ld_H; } /** * @brief Returns power goal used for DCAC measurement * @param pHandle Handle on the Profiler component */ float_t PROFILER_getPowerGoal_W(const PROFILER_Handle handle) { return handle->PowerDC_goal_W; } /** * @brief Returns estimated resistance Rs_ac from AC-duty injection * @param pHandle Handle on the Profiler component */ float_t PROFILER_getRs_ac(const PROFILER_Handle handle) { return handle->Rs_ac; } /** * @brief Returns estimated resistance Rs_dc once current ramp-up complete * @param pHandle Handle on the Profiler component */ float_t PROFILER_getRs_dc(const PROFILER_Handle handle) { return handle->Rs_dc; } /** * @brief Sets user command to start or reset the profiler * @param pHandle Handle on the Profiler component * @param command User command: Start or Reset */ void PROFILER_setCommand(PROFILER_Handle handle, const PROFILER_Command_e command) { handle->command = command; } /** * @brief Sets measurement time allowed to DCAC steps * @param pHandle Handle on the Profiler component * @param time_seconds time in seconds allowed to execute each one of steps: DC, AC measuring */ void PROFILER_setDcAcMeasurementTime(PROFILER_Handle handle, const float_t time_seconds) { PROFILER_DCAC_setMeasurementTime(&handle->dcacObj, time_seconds); } /** * @brief Sets flux estimation frequency * @param pHandle Handle on the Profiler component * @param fluxEstFreq_Hz Flux estimate frequency (speed in Hz) */ void PROFILER_setFluxEstFreq_Hz(PROFILER_Handle handle, const float_t fluxEstFreq_Hz) { PROFILER_FLUXESTIM_setFluxEstFreq_Hz(&handle->fluxestimObj, fluxEstFreq_Hz); } /** * @brief Sets measurement time allowed for flux estimate step * @param pHandle Handle on the Profiler component * @param time_seconds time in seconds allowed to execute the flux measurement step */ void PROFILER_setFluxEstMeasurementTime(PROFILER_Handle handle, const float_t time_seconds) { PROFILER_FLUXESTIM_setMeasurementTime(&handle->fluxestimObj, time_seconds); } /** * @brief Sets the number of polepairs * @param pHandle Handle on the Profiler component * @param polepairs number of polepairs */ void PROFILER_setPolePairs(PROFILER_Handle handle, const float_t polepairs) { handle->PolePairs = polepairs; } /** * @brief Sets the power goal for DCAC measurement * @param pHandle Handle on the Profiler component * @param powerGoal_W Level of power allowed to identify the motor */ void PROFILER_setPowerGoal_W(PROFILER_Handle handle, const float_t powerGoal_W) { if (handle->state == PROFILER_STATE_Idle || handle->state == PROFILER_STATE_Complete || handle->state == PROFILER_STATE_Error) { handle->PowerDC_goal_W = powerGoal_W; } } /** * @brief Clears result variables before each profiling * @param pHandle Handle on the Profiler component */ void PROFILER_resetEstimates(PROFILER_Handle handle) { handle->PowerDC_W = 0.0; handle->dutyDC = 0.0; handle->Idc_A = 0.0; handle->Rs_dc = 0.0; handle->Rs_ac = 0.0; handle->Ld_H = 0.0; handle->Lq_H = 0.0f; handle->freqEst_Hz = 0.0; handle->freqHSO_Hz = 0.0; handle->Flux_Wb = 0.0; handle->debug_Flux_VpHz = 0.0; handle->debug_kV = 0.0; handle->KT_Nm_A = 0.0; handle->Isc_A = 0.0; handle->CritFreq_Hz = 0.0; } /** * @} */ /** * @} */ /************************ (C) COPYRIGHT 2023 STMicroelectronics *****END OF FILE****/
16,941
C
30.316081
159
0.665132
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/MCSDK_v6.2.0-Full/MotorControl/MCSDK/MCLib/Any/Src/ramp_ext_mngr.c
/** ****************************************************************************** * @file ramp_ext_mngr.c * @author Motor Control SDK Team, ST Microelectronics * @brief This file provides firmware functions that implement the features * of the Ramp Extended Manager component of the Motor Control SDK: * ****************************************************************************** * @attention * * <h2><center>&copy; Copyright (c) 2023 STMicroelectronics. * All rights reserved.</center></h2> * * This software component is licensed by ST under Ultimate Liberty license * SLA0044, the "License"; You may not use this file except in compliance with * the License. You may obtain a copy of the License at: * www.st.com/SLA0044 * ****************************************************************************** */ /* Includes ------------------------------------------------------------------*/ #include "ramp_ext_mngr.h" /** @addtogroup MCSDK * @{ */ /** @defgroup RampExtMngr Ramp Manager * @brief Ramp Extended Manager component of the Motor Control SDK * * @todo Document the Ramp Extended Manager "module". * * @{ */ /* Private function prototypes -----------------------------------------------*/ uint32_t getScalingFactor(int32_t Target); /** * @brief It reset the state variable to zero. * @param pHandle related Handle of struct RampMngr_Handle_t * @retval none. */ void REMNG_Init(RampExtMngr_Handle_t *pHandle) { #ifdef NULL_PTR_CHECK_RMP_EXT_MNG if (MC_NULL == pHandle) { /* Nothing to do */ } else { #endif pHandle->Ext = 0; pHandle->TargetFinal = 0; pHandle->RampRemainingStep = 0U; pHandle->IncDecAmount = 0; pHandle->ScalingFactor = 1U; #ifdef NULL_PTR_CHECK_RMP_EXT_MNG } #endif } #if defined (CCMRAM) #if defined (__ICCARM__) #pragma location = ".ccmram" #elif defined (__CC_ARM) || defined(__GNUC__) __attribute__((section(".ccmram"))) #endif #endif /** * @brief Exec the ramp calculations and returns the current value of the state variable. It must be called at fixed interval defined in the hExecFreq. * @param pHandle related Handle of struct RampMngr_Handle_t * @retval int32_t value of the state variable */ __weak int32_t REMNG_Calc(RampExtMngr_Handle_t *pHandle) { int32_t ret_val; #ifdef NULL_PTR_CHECK_RMP_EXT_MNG if (MC_NULL == pHandle) { ret_val = 0; } else { #endif int32_t current_ref; current_ref = pHandle->Ext; /* Update the variable and terminates the ramp if needed */ if (pHandle->RampRemainingStep > 1U) { /* Increment/decrement the reference value */ current_ref += pHandle->IncDecAmount; /* Decrement the number of remaining steps */ pHandle->RampRemainingStep --; } else if (1U == pHandle->RampRemainingStep) { /* Set the backup value of TargetFinal */ current_ref = pHandle->TargetFinal * ((int32_t)pHandle->ScalingFactor); pHandle->RampRemainingStep = 0U; } else { /* Do nothing */ } pHandle->Ext = current_ref; ret_val = pHandle->Ext / ((int32_t)pHandle->ScalingFactor); #ifdef NULL_PTR_CHECK_RMP_EXT_MNG } #endif return (ret_val); } /** * @brief Setup the ramp to be executed * @param pHandle related Handle of struct RampMngr_Handle_t * @param hTargetFinal (signed 32bit) final value of state variable at the end * of the ramp. * @param hDurationms (unsigned 32bit) the duration of the ramp expressed in * milliseconds. It is possible to set 0 to perform an instantaneous * change in the value. * @retval bool It returns true is command is valid, false otherwise */ __weak bool REMNG_ExecRamp(RampExtMngr_Handle_t *pHandle, int32_t TargetFinal, uint32_t Durationms) { bool retVal = true; #ifdef NULL_PTR_CHECK_RMP_EXT_MNG if (MC_NULL == pHandle) { retVal = false; } else { #endif uint32_t aux; int32_t aux1; int32_t current_ref; /* Get current state */ current_ref = pHandle->Ext / ((int32_t)pHandle->ScalingFactor); if (0U == Durationms) { pHandle->ScalingFactor = getScalingFactor(TargetFinal); pHandle->Ext = TargetFinal * ((int32_t)pHandle->ScalingFactor); pHandle->RampRemainingStep = 0U; pHandle->IncDecAmount = 0; } else { uint32_t wScalingFactor = getScalingFactor(TargetFinal - current_ref); uint32_t wScalingFactor2 = getScalingFactor(current_ref); uint32_t wScalingFactor3 = getScalingFactor(TargetFinal); uint32_t wScalingFactorMin; if (wScalingFactor < wScalingFactor2) { if (wScalingFactor < wScalingFactor3) { wScalingFactorMin = wScalingFactor; } else { wScalingFactorMin = wScalingFactor3; } } else { if (wScalingFactor2 < wScalingFactor3) { wScalingFactorMin = wScalingFactor2; } else { wScalingFactorMin = wScalingFactor3; } } pHandle->ScalingFactor = wScalingFactorMin; pHandle->Ext = current_ref * ((int32_t)pHandle->ScalingFactor); /* Store the TargetFinal to be applied in the last step */ pHandle->TargetFinal = TargetFinal; /* Compute the (wRampRemainingStep) number of steps remaining to complete the ramp */ aux = Durationms * ((uint32_t)pHandle->FrequencyHz); /* Check for overflow and use prescaler */ aux /= 1000U; pHandle->RampRemainingStep = aux; pHandle->RampRemainingStep++; /* Compute the increment/decrement amount (wIncDecAmount) to be applied to the reference value at each CalcTorqueReference */ aux1 = (TargetFinal - current_ref) * ((int32_t)pHandle->ScalingFactor); aux1 /= ((int32_t)pHandle->RampRemainingStep); pHandle->IncDecAmount = aux1; } #ifdef NULL_PTR_CHECK_RMP_EXT_MNG } #endif return (retVal); } /** * @brief Returns the current value of the state variable. * @param pHandle related Handle of struct RampMngr_Handle_t * @retval int32_t value of the state variable */ __weak int32_t REMNG_GetValue(const RampExtMngr_Handle_t *pHandle) { #ifdef NULL_PTR_CHECK_RMP_EXT_MNG return ((MC_NULL == pHandle) ? 0 : (pHandle->Ext / ((int32_t)pHandle->ScalingFactor))); #else return (pHandle->Ext / ((int32_t)pHandle->ScalingFactor)); #endif } #if defined (CCMRAM) #if defined (__ICCARM__) #pragma location = ".ccmram" #elif defined (__CC_ARM) || defined(__GNUC__) __attribute__((section(".ccmram"))) #endif #endif /** * @brief Check if the settled ramp has been completed. * @param pHandle related Handle of struct RampMngr_Handle_t. * @retval bool It returns true if the ramp is completed, false otherwise. */ __weak bool REMNG_RampCompleted(const RampExtMngr_Handle_t *pHandle) { bool retVal = false; #ifdef NULL_PTR_CHECK_RMP_EXT_MNG if (MC_NULL == pHandle) { /* Nothing to do */ } else { #endif if (0U == pHandle->RampRemainingStep) { retVal = true; } else { /* nothing to do */ } #ifdef NULL_PTR_CHECK_RMP_EXT_MNG } #endif return (retVal); } /** * @brief Stop the execution of the ramp keeping the last reached value. * @param pHandle related Handle of struct RampMngr_Handle_t. * @retval none */ __weak void REMNG_StopRamp(RampExtMngr_Handle_t *pHandle) { #ifdef NULL_PTR_CHECK_RMP_EXT_MNG if (MC_NULL == pHandle) { /* Nothing to do */ } else { #endif pHandle->RampRemainingStep = 0U; pHandle->IncDecAmount = 0; #ifdef NULL_PTR_CHECK_RMP_EXT_MNG } #endif } /** * @brief Calculating the scaling factor to maximixe the resolution. It * perform the 2^int(31-log2(Target)) with an iterative approach. * It allows to keep Target * Scaling factor inside int32_t type. * @param Target Input data. * @retval uint32_t It returns the optimized scaling factor. */ __weak uint32_t getScalingFactor(int32_t Target) { uint32_t TargetAbs; int32_t aux; uint8_t i; if (Target < 0) { aux = -Target; TargetAbs = (uint32_t)aux; } else { TargetAbs = (uint32_t)Target; } for (i = 1U; i < 32U; i++) { uint32_t limit = (((uint32_t)1) << (31U - i)); if (TargetAbs >= limit) { break; } else { /* Nothing to do */ } } return (((uint32_t)1) << (i - 1U)); } /** * @} */ /** * @} */ /************************ (C) COPYRIGHT 2023 STMicroelectronics *****END OF FILE****/
8,605
C
25
101
0.607438
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/MCSDK_v6.2.0-Full/MotorControl/MCSDK/MCLib/Any/Src/virtual_bus_voltage_sensor.c
/** ****************************************************************************** * @file virtual_bus_voltage_sensor.c * @author Motor Control SDK Team, ST Microelectronics * @brief This file provides firmware functions that implement the features * of the Virtual Bus Voltage Sensor component of the Motor Control SDK. * ****************************************************************************** * @attention * * <h2><center>&copy; Copyright (c) 2023 STMicroelectronics. * All rights reserved.</center></h2> * * This software component is licensed by ST under Ultimate Liberty license * SLA0044, the "License"; You may not use this file except in compliance with * the License. You may obtain a copy of the License at: * www.st.com/SLA0044 * ****************************************************************************** * @ingroup VirtualBusVoltageSensor */ /* Includes ------------------------------------------------------------------*/ #include "virtual_bus_voltage_sensor.h" /** @addtogroup MCSDK * @{ */ /** @addtogroup BusVoltageSensor * @{ */ /** @defgroup VirtualBusVoltageSensor Virtual Bus Voltage Sensor * @brief Virtual Bus Voltage Sensor implementation. * * @{ */ /** * @brief It initializes bus voltage conversion for virtual bus voltage sensor * (latest value and averaged value) with expected VBus value * @param pHandle related Handle of VirtualBusVoltageSensor_Handle_t * @retval none */ __weak void VVBS_Init(VirtualBusVoltageSensor_Handle_t *pHandle) { pHandle->_Super.FaultState = MC_NO_ERROR; pHandle->_Super.LatestConv = pHandle->ExpectedVbus_d; pHandle->_Super.AvBusVoltage_d = pHandle->ExpectedVbus_d; } /** * @brief Empty function (no process and no returned value) * @param pHandle related Handle of VirtualBusVoltageSensor_Handle_t * @retval none */ __weak void VVBS_Clear(VirtualBusVoltageSensor_Handle_t *pHandle) { return; } /** * @brief It returns #MC_NO_ERROR * @param pHandle related Handle of VirtualBusVoltageSensor_Handle_t * @retval uint16_t Fault code error: #MC_NO_ERROR */ __weak uint16_t VVBS_NoErrors(VirtualBusVoltageSensor_Handle_t *pHandle) { return (MC_NO_ERROR); } /** * @} */ /** * @} */ /** @} */ /************************ (C) COPYRIGHT 2023 STMicroelectronics *****END OF FILE****/
2,410
C
27.034883
85
0.582988
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/MCSDK_v6.2.0-Full/MotorControl/MCSDK/MCLib/Any/Src/revup_ctrl_sixstep.c
/** ****************************************************************************** * @file revup_ctrl_sixstep.c * @author Motor Control SDK Team, ST Microelectronics * @brief This file provides the functions that implement the features * of the Rev-Up Control component for Six-Step drives of the Motor * Control SDK. * ****************************************************************************** * @attention * * <h2><center>&copy; Copyright (c) 2023 STMicroelectronics International N.V. * All rights reserved.</center></h2> * * Redistribution and use in source and binary forms, with or without * modification, are permitted, provided that the following conditions are met: * * 1. Redistribution of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of STMicroelectronics nor the names of other * contributors to this software may be used to endorse or promote products * derived from this software without specific written permission. * 4. This software, including modifications and/or derivative works of this * software, must execute solely and exclusively on microcontroller or * microprocessor devices manufactured by or for STMicroelectronics. * 5. Redistribution and use of this software other than as permitted under * this license is void and will automatically terminate your rights under * this license. * * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * ****************************************************************************** * @ingroup RevUpCtrl6S */ /* Includes ------------------------------------------------------------------*/ #include "revup_ctrl_sixstep.h" /** @addtogroup MCSDK * @{ */ /** @addtogroup RevUpCtrl * * @{ */ /** @defgroup RevUpCtrl6S Six-Step Rev-Up Control component * @brief Rev-Up Control component used to start motor driven with the Six-Step technique * * @{ */ /* Private defines -----------------------------------------------------------*/ /** * @brief Timeout used to reset integral term of PLL. * It is expressed in ms. * */ #define RUC_OTF_PLL_RESET_TIMEOUT 100u /* Private functions ----------------------------------------------------------*/ /* Returns the mechanical speed of a selected phase */ static int16_t RUC_GetPhaseFinalMecSpeed01Hz(RevUpCtrl_Handle_t *pHandle, uint8_t bPhase) { int16_t hRetVal = 0; if (MC_NULL == pHandle) { /* Nothing to do */ } else { hRetVal = pHandle->ParamsData[bPhase].hFinalMecSpeedUnit; } return (hRetVal); } /** * @brief Initialize and configure the 6-Step RevUpCtrl Component * @param pHandle: Pointer on Handle structure of RevUp controller. * @param pSTC: Pointer on speed and torque controller structure. * @param pVSS: Pointer on virtual speed sensor structure. */ __weak void RUC_Init( RevUpCtrl_Handle_t *pHandle, SpeednTorqCtrl_Handle_t *pSTC, VirtualSpeedSensor_Handle_t *pVSS) { if (MC_NULL == pHandle) { /* Nothing to do */ } else { RevUpCtrl_PhaseParams_t *pRUCPhaseParams = &pHandle->ParamsData[0]; uint8_t bPhase = 0U; pHandle->pSTC = pSTC; pHandle->pVSS = pVSS; while ((pRUCPhaseParams != MC_NULL) && (bPhase < RUC_MAX_PHASE_NUMBER)) { /* Dump HF data for now HF data are forced to 16 bits*/ pRUCPhaseParams = (RevUpCtrl_PhaseParams_t * )pRUCPhaseParams->pNext; //cstat !MISRAC2012-Rule-11.5 bPhase++; } if (0U == bPhase) { /* nothing to do error */ } else { pHandle->ParamsData[bPhase - 1u].pNext = MC_NULL; pHandle->bPhaseNbr = bPhase; } } } /* * @brief Initialize internal 6-Step RevUp controller state. * @param pHandle: Pointer on Handle structure of RevUp controller. * @param hMotorDirection: Rotor rotation direction. * This parameter must be -1 or +1. */ __weak void RUC_Clear(RevUpCtrl_Handle_t *pHandle, int16_t hMotorDirection) { if (MC_NULL == pHandle) { /* Nothing to do */ } else { VirtualSpeedSensor_Handle_t *pVSS = pHandle->pVSS; SpeednTorqCtrl_Handle_t *pSTC = pHandle->pSTC; RevUpCtrl_PhaseParams_t *pPhaseParams = pHandle->ParamsData; pHandle->hDirection = hMotorDirection; /*Initializes the rev up stages counter.*/ pHandle->bStageCnt = 0U; /* Calls the clear method of VSS.*/ VSS_Clear(pVSS); /* Sets the STC in torque mode.*/ STC_SetControlMode(pSTC, MCM_TORQUE_MODE); /* Sets the mechanical starting angle of VSS.*/ VSS_SetMecAngle(pVSS, pHandle->hStartingMecAngle * hMotorDirection); /* Sets to zero the starting torque of STC */ (void)STC_ExecRamp(pSTC, STC_GetDutyCycleRef(pSTC), 0U); /* Gives the first command to STC and VSS.*/ (void)STC_ExecRamp(pSTC, pPhaseParams->hFinalPulse, (uint32_t)(pPhaseParams->hDurationms)); VSS_SetMecAcceleration(pVSS, pPhaseParams->hFinalMecSpeedUnit * hMotorDirection, pPhaseParams->hDurationms ); /* Compute hPhaseRemainingTicks.*/ pHandle->hPhaseRemainingTicks = (uint16_t)((((uint32_t)pPhaseParams->hDurationms) * ((uint32_t)pHandle->hRUCFrequencyHz)) / 1000U ); pHandle->hPhaseRemainingTicks++; /*Set the next phases parameter pointer.*/ pHandle->pCurrentPhaseParams = (RevUpCtrl_PhaseParams_t * )pPhaseParams->pNext; //cstat !MISRAC2012-Rule-11.5 pHandle->EnteredZone1 = false; } } /** * @brief Update rev-up duty cycle relative to actual Vbus value to be applied * @param pHandle: Pointer on Handle structure of RevUp controller. * @param BusVHandle: pointer to the bus voltage sensor */ __weak void RUC_UpdatePulse(RevUpCtrl_Handle_t *pHandle, BusVoltageSensor_Handle_t *BusVHandle) { if (MC_NULL == pHandle) { /* Nothing to do */ } else { uint16_t tPulseUpdateFactor = 10 * NOMINAL_BUS_VOLTAGE_V / VBS_GetAvBusVoltage_V(BusVHandle); pHandle->PulseUpdateFactor = tPulseUpdateFactor; } } /* * @brief 6-Step Main revup controller procedure executing overall programmed phases. * @param pHandle: Pointer on Handle structure of RevUp controller. * @retval Boolean set to true when entire revup phases have been completed. */ __weak bool RUC_Exec(RevUpCtrl_Handle_t *pHandle) { bool retVal = true; if (MC_NULL == pHandle) { retVal = false; } else { if (pHandle->hPhaseRemainingTicks > 0U) { /* Decrease the hPhaseRemainingTicks.*/ pHandle->hPhaseRemainingTicks--; } /* hPhaseRemainingTicks > 0 */ if (0U == pHandle->hPhaseRemainingTicks) { if (pHandle->pCurrentPhaseParams != MC_NULL) { /* If it becomes zero the current phase has been completed.*/ /* Gives the next command to STC and VSS.*/ uint16_t hPulse = pHandle->pCurrentPhaseParams->hFinalPulse * pHandle->PulseUpdateFactor / 10; (void)STC_ExecRamp(pHandle->pSTC, hPulse, (uint32_t)(pHandle->pCurrentPhaseParams->hDurationms)); VSS_SetMecAcceleration(pHandle->pVSS, pHandle->pCurrentPhaseParams->hFinalMecSpeedUnit * pHandle->hDirection, pHandle->pCurrentPhaseParams->hDurationms); /* Compute hPhaseRemainingTicks.*/ pHandle->hPhaseRemainingTicks = (uint16_t)((((uint32_t)pHandle->pCurrentPhaseParams->hDurationms) * ((uint32_t)pHandle->hRUCFrequencyHz)) / 1000U ); pHandle->hPhaseRemainingTicks++; /*Set the next phases parameter pointer.*/ pHandle->pCurrentPhaseParams = pHandle->pCurrentPhaseParams->pNext; //cstat !MISRAC2012-Rule-11.5 /*Increases the rev up stages counter.*/ pHandle->bStageCnt++; } else { retVal = false; } } } return (retVal); } /* * @brief It is used to check if this stage is used for align motor. * @param this related object of class CRUC. * @retval Returns 1 if the alignment is correct otherwise it returns 0 */ uint8_t RUC_IsAlignStageNow(RevUpCtrl_Handle_t *pHandle) { uint8_t align_flag = 0; if (MC_NULL == pHandle) { /* Nothing to do */ } else { int16_t speed; speed = RUC_GetPhaseFinalMecSpeed01Hz(pHandle, pHandle->bStageCnt); if (0 == speed) { align_flag = 1; } } return (align_flag); } /* * @brief Provide current state of revup controller procedure. * @param pHandle: Pointer on Handle structure of RevUp controller. * @retval Boolean set to true when entire revup phases have been completed. */ __weak bool RUC_Completed(RevUpCtrl_Handle_t *pHandle) { bool retVal = false; if (MC_NULL == pHandle) { /* Nothing to do */ } else { if (MC_NULL == pHandle->pCurrentPhaseParams) { retVal = true; } } return (retVal); } /* * @brief Allow to exit from RevUp process at the current rotor speed. * @param pHandle: Pointer on Handle structure of RevUp controller. */ __weak void RUC_Stop(RevUpCtrl_Handle_t *pHandle) { if (MC_NULL == pHandle) { /* Nothing to do */ } else { VirtualSpeedSensor_Handle_t *pVSS = pHandle->pVSS; pHandle->pCurrentPhaseParams = MC_NULL; pHandle->hPhaseRemainingTicks = 0U; VSS_SetMecAcceleration(pVSS, SPD_GetAvrgMecSpeedUnit(&pVSS->_Super), 0U); } } #if defined (CCMRAM) #if defined (__ICCARM__) #pragma location = ".ccmram" #elif defined (__CC_ARM) || defined(__GNUC__) __attribute__( ( section ( ".ccmram" ) ) ) #endif #endif /* * @brief Check that alignment and first acceleration stage are completed. * @param pHandle: Pointer on Handle structure of RevUp controller. * @retval Boolean set to true when first acceleration stage has been reached. */ __weak bool RUC_FirstAccelerationStageReached( RevUpCtrl_Handle_t * pHandle) { bool retVal = false; if (MC_NULL == pHandle) { /* Nothing to do */ } else { if (pHandle->bStageCnt >= pHandle->bFirstAccelerationStage) { retVal = true; } } return (retVal); } /* * @brief Check that minimum rotor speed has been reached. * @param pHandle: Pointer on Handle structure of RevUp controller. * @retval Boolean set to true when speed has been reached. */ __weak bool RUC_ObserverSpeedReached( RevUpCtrl_Handle_t * pHandle) { bool retVal = false; VirtualSpeedSensor_Handle_t *pVSS = pHandle->pVSS; int16_t hSpeed; hSpeed = SPD_GetAvrgMecSpeedUnit(&pVSS->_Super); if (hSpeed < 0) hSpeed = -hSpeed; if (MC_NULL == pHandle) { /* Nothing to do */ } else { if ((false == pHandle->EnteredZone1) && (hSpeed >= pHandle->hMinStartUpValidSpeed)) { pHandle->EnteredZone1 = true; retVal = true; } } return (retVal); } /* * @brief Allow to modify duration of a selected phase. * @param pHandle: Pointer on Handle structure of RevUp controller. * @param bPhase: RevUp phase where new duration shall be modified. * This parameter must be a number between 0 and 6. * @param hDurationms: new duration value required for associated phase. * This parameter must be set in millisecond. */ __weak void RUC_SetPhaseDurationms(RevUpCtrl_Handle_t *pHandle, uint8_t bPhase, uint16_t hDurationms) { if (MC_NULL == pHandle) { /* Nothing to do */ } else { pHandle->ParamsData[bPhase].hDurationms = hDurationms; } } /* * @brief Allow to modify targeted mechanical speed of a selected phase. * @param pHandle: Pointer on Handle structure of RevUp controller. * @param bPhase: RevUp phase where new mechanical speed shall be modified. * This parameter must be a number between 0 and 6. * @param hFinalMecSpeedUnit: new targeted mechanical speed. * This parameter must be expressed in 0.1Hz. */ __weak void RUC_SetPhaseFinalMecSpeedUnit(RevUpCtrl_Handle_t *pHandle, uint8_t bPhase, int16_t hFinalMecSpeedUnit) { if (MC_NULL == pHandle) { /* Nothing to do */ } else { pHandle->ParamsData[bPhase].hFinalMecSpeedUnit = hFinalMecSpeedUnit; } } /** * @brief Allow to modify targeted PWM counter pulse of a selected phase. * @param pHandle: Pointer on Handle structure of RevUp controller. * @param bPhase: RevUp phase where new the motor torque shall be modified. * This parameter must be a number between 0 and 6. * @param hFinalPulse: new targeted motor torque. */ __weak void RUC_SetPhaseFinalPulse(RevUpCtrl_Handle_t *pHandle, uint8_t bPhase, uint16_t hFinalPulse) { if (MC_NULL == pHandle) { /* Nothing to do */ } else { pHandle->ParamsData[bPhase].hFinalPulse = hFinalPulse; } } /* * @brief Allow to configure a revUp phase. * @param pHandle: Pointer on Handle structure of RevUp controller. * @retval Returns boolean set to true */ __weak bool RUC_SetPhase(RevUpCtrl_Handle_t *pHandle, uint8_t phaseNumber, RevUpCtrl_PhaseParams_t *phaseData) { bool retValue = true; if ((MC_NULL == pHandle) || (MC_NULL == phaseData)) { retValue = false; } else { pHandle->ParamsData[phaseNumber].hFinalPulse = phaseData->hFinalPulse; pHandle->ParamsData[phaseNumber].hFinalMecSpeedUnit = phaseData->hFinalMecSpeedUnit; pHandle->ParamsData[phaseNumber].hDurationms = phaseData->hDurationms; } return (retValue); } /* * @brief Allow to read duration set in selected phase. * @param pHandle: Pointer on Handle structure of RevUp controller. * @param bPhase: RevUp phase from where duration is read. * This parameter must be a number between 0 and 6. * @retval Returns duration used in selected phase. */ __weak uint16_t RUC_GetPhaseDurationms(RevUpCtrl_Handle_t *pHandle, uint8_t bPhase) { return ((MC_NULL == pHandle) ? 0U : (uint16_t)pHandle->ParamsData[bPhase].hDurationms); } /* * @brief Allow to read targeted rotor speed set in selected phase. * @param pHandle: Pointer on Handle structure of RevUp controller. * @param bPhase: RevUp phase from where targeted rotor speed is read. * This parameter must be a number between 0 and 6. * @retval Returns targeted rotor speed set in selected phase. */ __weak int16_t RUC_GetPhaseFinalMecSpeedUnit(RevUpCtrl_Handle_t *pHandle, uint8_t bPhase) { return ((MC_NULL == pHandle) ? 0 : (int16_t)pHandle->ParamsData[bPhase].hFinalMecSpeedUnit); } /** * @brief Allow to read targeted PWM counter pulse set in selected phase. * @param pHandle: Pointer on Handle structure of RevUp controller. * @param bPhase: RevUp phase from where targeted motor torque is read. * This parameter must be a number between 0 and 6. * @retval Returns targeted motor torque set in selected phase. */ __weak int16_t RUC_GetPhaseFinalPulse(RevUpCtrl_Handle_t *pHandle, uint8_t bPhase) { return ((MC_NULL == pHandle) ? 0 : (int16_t)pHandle->ParamsData[bPhase].hFinalPulse); } /* * @brief Allow to read total number of programmed phases. * @param pHandle: Pointer on Handle structure of RevUp controller. * @retval Returns number of phases relative to the programmed revup. */ __weak uint8_t RUC_GetNumberOfPhases(RevUpCtrl_Handle_t *pHandle) { return ((MC_NULL == pHandle) ? 0U : (uint8_t)pHandle->bPhaseNbr); } /* * @brief Allow to read a programmed phase. * @param pHandle: Pointer on Handle structure of RevUp controller. * @retval Returns number of phases relative to the programmed revup. */ __weak bool RUC_GetPhase(RevUpCtrl_Handle_t *pHandle, uint8_t phaseNumber, RevUpCtrl_PhaseParams_t *phaseData) { bool retValue = true; if ((MC_NULL == pHandle) || (MC_NULL == phaseData)) { retValue = false; } else { phaseData->hFinalPulse = (int16_t)pHandle->ParamsData[phaseNumber].hFinalPulse; phaseData->hFinalMecSpeedUnit = (int16_t)pHandle->ParamsData[phaseNumber].hFinalMecSpeedUnit; phaseData->hDurationms = (uint16_t)pHandle->ParamsData[phaseNumber].hDurationms; } return (retValue); } /** * @brief Allow to read spinning direction of the motor * @param pHandle: Pointer on Handle structure of RevUp controller. * @retval Returns direction of the motor. */ __weak int16_t RUC_GetDirection(RevUpCtrl_Handle_t *pHandle) { return ((MC_NULL == pHandle) ? 0U : (int16_t)pHandle->hDirection); } /** * @} */ /** * @} */ /** * @} */ /************************ (C) COPYRIGHT 2023 STMicroelectronics *****END OF FILE****/
17,712
C
30.184859
114
0.656786
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/MCSDK_v6.2.0-Full/MotorControl/MCSDK/MCLib/Any/Src/data_scope.c
/** ****************************************************************************** * @file data_scope.c * @author Piak Electronic Design B.V. * @brief This file is part of the Motor Control SDK. ****************************************************************************** * @attention * * <h2><center>&copy; Copyright (c) 2023 Piak Electronic Design B.V. * All rights reserved.</center></h2> * * This software component is licensed by ST under Ultimate Liberty license * SLA0044, the "License"; You may not use this file except in compliance with * the License. You may obtain a copy of the License at: * www.st.com/SLA0044 * ****************************************************************************** */ /* data_scope.c */ #include "data_scope.h" #include <string.h> // memset /* ToDo: * Conversion and scaling; * Source is IQ24, IQ15, IQ30 etc. * Current scaling * IQ_PU_A to 1V/A, 0.1V/A, 0.01V/A etc. * User sets preference in some scale that makes sense * 1V/Nm, 1V/VpHz * */ /* Responsibilities: * Offset, scale. Compensate for DAC board deviations * (This module was first developed for DAC output, and this functionality is left in) * Signal selection * Whole group select, 4 channels at once * Individual channel selection * Scale selection * 1V/Nm, 0.1V/Nm etc for torques * 1V/VpHz for flux * Scale-up, scale-down impulse, like encoder knob on oscilloscope * Natural scale, not per unit * Per unit to A, 1V/A etc. * (1V/<unit> terminology is a legacy of the DAC output) * Excluded: * Meta-data * Is defined in another module, which is application-specific. DATA_SCOPE is a more general module. */ /* Implementation: * _run function * performs scaling using current offset, scale factors * uses current input values, pointer to struct parameter input * does not do floats (?) (maybe FPU on only?) * choice: * Should this lookup data in memory? (internal selection? * Must know data then? * Or location, type etc of data * Should this be given data from outside (outside selection) * Channel select is external then [So No] * * Option: * Channel has * Source location (void*) * Numerical type (uint16, int32 etc) * Data type (A_PU, FLUX_PU, V_PU etc) * Scaling from internal Per Unit to usable units) * Data scale (iqFmt) * Scope scale (1V/U, 10V/U, 0.1V/U etc) * 1x, 2x, 5x, 10x, 20x, 50x, 100x, 0.5x, 0.2x, 0.1x etc * Ranges? * */ /* Internal function declarations */ /* Initialization functions */ DATA_SCOPE_Handle DATA_SCOPE_init(void* pMemory, const size_t numWords) { DATA_SCOPE_Handle handle = (DATA_SCOPE_Handle) 0; if (numWords >= sizeof(DATA_SCOPE_Obj)) { handle = (DATA_SCOPE_Handle) pMemory; memset(pMemory, 0, numWords); } return handle; } /* end of DATA_SCOPE_init() function */ void DATA_SCOPE_setup(DATA_SCOPE_Handle handle) { DATA_SCOPE_Obj* obj = (DATA_SCOPE_Obj*) handle; DATA_SCOPE_CHANNEL_s* pChannel = 0; int i; for (i = 0; i < DATA_SCOPE_NUMCHANNELS; i++) { pChannel = &obj->channel[i]; /* Disable channel */ pChannel->enabled = false; /* Set scale and offset to defaults */ pChannel->dac_scale = FIXP16(0.1); pChannel->dac_offset = FIXP16(0.5); pChannel->data_scale_f = 0.0f; DATA_SCOPE_setChannelDataScale(handle, i, 1.0f); } return; } /* end of DATA_SCOPE_setup() function */ /* Functional */ void DATA_SCOPE_run(DATA_SCOPE_Handle handle) { DATA_SCOPE_Obj* obj = (DATA_SCOPE_Obj*) handle; DATA_SCOPE_CHANNEL_s* pChannel = 0; int i; for (i = 0; i < DATA_SCOPE_NUMCHANNELS; i++) { pChannel = &obj->channel[i]; if (pChannel->enabled) { // Read data from RAM uint16_t dac_value = 0; switch (pChannel->datatype) { case DATA_DATATYPE_Int32: { /* Read value, in native scale unit */ fixp_t value = *((fixp_t*)(pChannel->pData)); /* Multiply by data_scale, which is in FIXP(V/unit) */ value = FIXP_mpyFIXPscaled(value, &pChannel->data_scale_fixps); // data_qFmt is taken into account in setChannelDataScale pChannel->data_fixp24_volt = value; /* Saturate to +/- 5V */ value = FIXP_sat(value, FIXP(5.0), FIXP(-5.0)); /* Scale to DAC scaling */ value = FIXP_mpy(value, pChannel->dac_scale); /* Adjust for offset */ value -= pChannel->dac_offset; /* Limit to full scale */ // value = FIXP_sat(value, 0xFFFF, 0); #warning Issue here is likely that value is signed, and 0xFFFF is unsigned? dac_value = (uint16_t) value; } break; } // Store pChannel->dac_value = dac_value; } } return; } /* end of DATA_SCOPE_run() function */ uint16_t DATA_SCOPE_getSingleChannelValue(DATA_SCOPE_Handle handle, uint16_t channel) { DATA_SCOPE_Obj* obj = (DATA_SCOPE_Obj*) handle; DATA_SCOPE_CHANNEL_s* pChannel = &obj->channel[channel]; uint16_t value = pChannel->dac_offset; if (pChannel->enabled) value = pChannel->dac_value; return (value); } /* end of DATA_SCOPE_getSingleChannelValue() function */ void DATA_SCOPE_setChannelDacScaleAndOffset(DATA_SCOPE_Handle handle, const uint16_t channel, const fixp15_t scale, const fixp15_t offset) { DATA_SCOPE_Obj* obj = (DATA_SCOPE_Obj*) handle; DATA_SCOPE_CHANNEL_s* pChannel = &obj->channel[channel]; pChannel->dac_scale = scale; pChannel->dac_offset = offset; return; } /* end of DATA_SCOPE_setChannelDacScaleAndOffset() function */ void DATA_SCOPE_setChannelDataScale(DATA_SCOPE_Handle handle, const uint16_t channel, const float dataScale) { DATA_SCOPE_Obj* obj = (DATA_SCOPE_Obj*) handle; DATA_SCOPE_CHANNEL_s* pChannel = &obj->channel[channel]; /* Only update if the data scale has actually changed */ if (pChannel->data_scale_f == dataScale) return; /* Remember the new data scale */ pChannel->data_scale_f = dataScale; float ds = 1.0f / dataScale; FIXPSCALED_calculateScaleFactor(pChannel->data_qFmt, 24, pChannel->data_fullscale, 1.0f, ds, &pChannel->data_scale_fixps); return; } /* Internal functions */ /* end of data_scope.c */ /************************ (C) COPYRIGHT 2023 Piak Electronic Design B.V. *****END OF FILE****/
6,952
C
28.841202
138
0.567319
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/MCSDK_v6.2.0-Full/MotorControl/MCSDK/MCLib/Any/Src/pid_regulator.c
/** ****************************************************************************** * @file pid_regulator.c * @author Motor Control SDK Team, ST Microelectronics * @brief This file provides firmware functions that implement the features * of the PID regulator component of the Motor Control SDK. * * The PID regulator component provides the functions needed to implement * a proportional–integral–derivative controller. * * See @link PIDRegulator @endlink for more details. * ****************************************************************************** * @attention * * <h2><center>&copy; Copyright (c) 2023 STMicroelectronics. * All rights reserved.</center></h2> * * This software component is licensed by ST under Ultimate Liberty license * SLA0044, the "License"; You may not use this file except in compliance with * the License. You may obtain a copy of the License at: * www.st.com/SLA0044 * ****************************************************************************** * @ingroup PIDRegulator */ /* Includes ------------------------------------------------------------------*/ #include "pid_regulator.h" #include "mc_type.h" /** @addtogroup MCSDK * @{ */ /** * @defgroup PIDRegulator PID Regulator * @brief PID Regulator component of the Motor Control SDK * * The PID regulator component implements the following two control functions: * * * A simple proportional-integral controller, implemented by the PI_Controller() function: * @f[ * r(t_k) = K_p \times \epsilon(t_k) + K_i \times \sum_{j=0}^k\epsilon(t_j) * @f] * * A complete proportional–integral–derivative controller, implemented by the PID_Controller() function: * @f[ * r(t_k) = K_p \times \epsilon(t_k) + K_i \times \sum_{j=0}^k\epsilon(t_j) + K_d \times (\epsilon(t_k) - \epsilon(t_{k-1})) * @f] * * The proportional, integral and derivative gains are expressed as rational numbers, with a gain (numerator) * and a divisor (denominator) parameters: * * * Proportional gain: @f$ K_{p} = K_{pg} / K_{pd} @f$ * * Integral gain: @f$ K_{i} = K_{ig} / K_{id} @f$ * * Derivative gain: @f$ K_{d} = K_{dg} / K_{dd} @f$ * * Each of the gain numerator and divisor parameters, @f$K_{{p}g}@f$, @f$K_{{i}g}@f$, @f$K_{{d}g}@f$, @f$K_{{p}d}@f$, * @f$K_{id}@f$, @f$K_{dd}@f$, can be set, at run time and independently, via the PID_SetKP(), PID_SetKI(), PID_SetKD(), * PID_SetKPDivisorPOW2(), PID_SetKIDivisorPOW2() and PID_SetKDDivisorPOW2() functions, respectively. * * A PID Regulator component needs to be initialized before it can be used. This is done with the PID_HandleInit() * function that sets the intergral term and the derivative term base (the previous value of the process error input) * to 0 and that also resets the numerators of the proportional, integral and derivative gains to their default values. * These default values are literally written in the code of the application, so they are set at compile time. They * can be retrieved with the PID_GetDefaultKP(), PID_GetDefaultKI() and PID_GetDefaultKD() functions. * * The controller functions implemented by the PI_Controller() and the PID_Controller() functions are based on 16-bit * integer arithmetics: the gains are expressed as fractional numbers, with 16-bit numerators and denominators. And the * controller output values returned by these functions are also 16-bit integers. This makes it possible to use this * component efficiently on all STM2 MCUs. * * To keep the computed values within limits, the component features the possibility to constrain the integral term * within a range of values bounded by the PID_SetLowerIntegralTermLimit() and PID_SetUpperIntegralTermLimit() functions. * * The output valud of the controller can also be bounded between a high and a low limit thanks to the * PID_SetLowerOutputLimit() and PID_SetUpperOutputLimit() functions. * * Hanlding a process with a PID Controller may require some adjustment to cope with specific situations. To that end, the * PID regulator component provides functions to set the integral term (PID_SetIntegralTerm()) or to set the value of the * previous process error (PID_SetPrevError()). * * See the [PID chapter of the User Manual](PID_regulator_theoretical_background.md) for more details on the theoretical * background of this regulator. * @{ */ /** * @brief Initializes the handle of a PID component * @param pHandle A Handle on the PID component to initialize * * The integral term and the derivative base of the PID component are * set to zero. * * The numerators of the proportional, integral and derivative gains * are set to their default values. These default values are the ones * set to the PID_Handle_t::hDefKpGain, PID_Handle_t::hDefKiGain and * PID_Handle_t::hDefKdGain fields of the PID_Handle_t structure. */ __weak void PID_HandleInit(PID_Handle_t *pHandle) { #ifdef NULL_PTR_CHECK_PID_REG if (MC_NULL == pHandle) { /* Nothing to do */ } else { #endif pHandle->hKpGain = pHandle->hDefKpGain; pHandle->hKiGain = pHandle->hDefKiGain; pHandle->hKdGain = pHandle->hDefKdGain; pHandle->wIntegralTerm = 0; pHandle->wPrevProcessVarError = 0; #ifdef NULL_PTR_CHECK_PID_REG } #endif } /** * @brief Sets @f$K_{pg}@f$, the numerator of the proportional gain of a PID component * @param pHandle Handle on the PID component * @param hKpGain New @f$K_{pg}@f$ value */ __weak void PID_SetKP(PID_Handle_t *pHandle, int16_t hKpGain) { #ifdef NULL_PTR_CHECK_PID_REG if (MC_NULL == pHandle) { /* Nothing to do */ } else { #endif pHandle->hKpGain = hKpGain; #ifdef NULL_PTR_CHECK_PID_REG } #endif } /** * @brief Sets the @f$K_{ig}@f$, the numrerator of the integral gain of a PID component * @param pHandle Handle on the PID component * @param hKiGain new @f$K_{ig}@f$ value */ __weak void PID_SetKI(PID_Handle_t *pHandle, int16_t hKiGain) { #ifdef NULL_PTR_CHECK_PID_REG if (MC_NULL == pHandle) { /* Nothing to do */ } else { #endif pHandle->hKiGain = hKiGain; #ifdef NULL_PTR_CHECK_PID_REG } #endif } /** * @brief Returns @f$K_{pg}@f$, the numerator of the proportional gain of a PID component * @param pHandle Handle on the PID component */ __weak int16_t PID_GetKP(PID_Handle_t *pHandle) //cstat !MISRAC2012-Rule-8.13 { #ifdef NULL_PTR_CHECK_PID_REG return ((MC_NULL == pHandle) ? 0 : pHandle->hKpGain); #else return (pHandle->hKpGain); #endif } /** * @brief Returns @f$K_{ig}@f$, the numrerator of the integral gain of a PID component * @param pHandle Handle on the PID component */ __weak int16_t PID_GetKI(PID_Handle_t *pHandle) //cstat !MISRAC2012-Rule-8.13 { #ifdef NULL_PTR_CHECK_PID_REG return ((MC_NULL == pHandle) ? 0 : pHandle->hKiGain); #else return (pHandle->hKiGain); #endif } /** * @brief Returns the default @f$K_{pg}@f$ value, the numerator of the proportional * gain of a PID component * @param pHandle Handle on the PID component * * The default @f$K_{pg}@f$ value is the one that is being used at startup time, * when the MCU is reset or when the PID_HandleInit() function gets called. When * any of the last two event occurs, any proportional gain numerator that may * have been previously set with the PID_SetKP() function is replaced by this * default value. */ __weak int16_t PID_GetDefaultKP(PID_Handle_t *pHandle) //cstat !MISRAC2012-Rule-8.13 { #ifdef NULL_PTR_CHECK_PID_REG return ((MC_NULL == pHandle) ? 0 : pHandle->hDefKpGain); #else return (pHandle->hDefKpGain); #endif } /** * @brief Returns the default @f$K_{ig}@f$ value, the numerator of the integral * gain of a PID component * @param pHandle Handle on the PID component * * The default @f$K_{ig}@f$ value is the one that is being used at startup time, * when the MCU is reset or when the PID_HandleInit() function gets called. When * any of the last two event occurs, any gain that may have been previously set * with the PID_SetKI() function is replaced by this default value. */ __weak int16_t PID_GetDefaultKI(PID_Handle_t *pHandle) //cstat !MISRAC2012-Rule-8.13 { #ifdef NULL_PTR_CHECK_PID_REG return ((MC_NULL == pHandle) ? 0 : pHandle->hDefKiGain); #else return (pHandle->hDefKiGain); #endif } #if defined (CCMRAM) #if defined (__ICCARM__) #pragma location = ".ccmram" #elif defined (__CC_ARM) || defined(__GNUC__) __attribute__((section(".ccmram"))) #endif #endif /** * @brief Sets the value of the integral term of a PID component * @param pHandle Handle on the PID component * @param wIntegralTermValue new integral term value multiplied by the divisor * of the integral gain * * If @f$T_{i}@f$ is the target integral term, the @p wIntegralTermValue term is stored * before the division by @f$K_{id}@f$ to maximize the available dynamics. * * @attention @p wIntegralTermValue divided by @f$K_{id}@f$ must fit in a 16-bit signed * integer value. */ __weak void PID_SetIntegralTerm(PID_Handle_t *pHandle, int32_t wIntegralTermValue) { #ifdef NULL_PTR_CHECK_PID_REG if (MC_NULL == pHandle) { /* Nothing to do */ } else { #endif pHandle->wIntegralTerm = wIntegralTermValue; #ifdef NULL_PTR_CHECK_PID_REG } #endif return; } /** * @brief Returns @f$K_{pd}@f$, the divisor of the proportional gain of a PID component * @param pHandle Handle on the PID component * * The divisors that make the gains of the @ref PIDRegulator "PID regulator component" are * powers of two. * * @sa PID_GetKPDivisorPOW2() */ __weak uint16_t PID_GetKPDivisor(PID_Handle_t *pHandle) //cstat !MISRAC2012-Rule-8.13 { #ifdef NULL_PTR_CHECK_PID_REG return ((MC_NULL == pHandle) ? 0U : pHandle->hKpDivisor); #else return (pHandle->hKpDivisor); #endif } /** * @brief Returns the power of two that makes @f$K_{pd}@f$, the divisor of the proportional * gain of a PID component * @param pHandle Handle on the PID component * * The divisors that make the gains of the @ref PIDRegulator "PID regulator component" are * powers of two. * * @sa PID_GetKPDivisor() */ __weak uint16_t PID_GetKPDivisorPOW2(PID_Handle_t *pHandle) //cstat !MISRAC2012-Rule-8.13 { #ifdef NULL_PTR_CHECK_PID_REG return ((MC_NULL == pHandle) ? 0U : pHandle->hKpDivisorPOW2); #else return (pHandle->hKpDivisorPOW2); #endif } /** * @brief Sets the power of two that makes @f$K_{pd}@f$, the divisor of the proportional gain * of a PID component * @param pHandle Handle on the PID component * @param hKpDivisorPOW2 new @f$K_{pd}@f$ divisor value, expressed as power of 2 * * The divisors that make the gains of the @ref PIDRegulator "PID regulator component" are * powers of two. * * This function sets @f$K_{pd}@f$ to 2 to the power of @p hKpDivisorPOW2. */ __weak void PID_SetKPDivisorPOW2(PID_Handle_t *pHandle, uint16_t hKpDivisorPOW2) { #ifdef NULL_PTR_CHECK_PID_REG if (MC_NULL == pHandle) { /* Nothing to do */ } else { #endif pHandle->hKpDivisorPOW2 = hKpDivisorPOW2; pHandle->hKpDivisor = (((uint16_t)1) << hKpDivisorPOW2); #ifdef NULL_PTR_CHECK_PID_REG } #endif } /** * @brief Returns @f$K_{id}@f$, the divisor of the integral gain of a PID component * @param pHandle Handle on the PID component * * The divisors that make the gains of the @ref PIDRegulator "PID regulator component" are * powers of two. * * @sa PID_GetKIDivisorPOW2() */ __weak uint16_t PID_GetKIDivisor(PID_Handle_t *pHandle) //cstat !MISRAC2012-Rule-8.13 { #ifdef NULL_PTR_CHECK_PID_REG return ((MC_NULL == pHandle) ? 0U : pHandle->hKiDivisor); #else return (pHandle->hKiDivisor); #endif } /** * @brief Returns the power of two that makes @f$K_{id}@f$, the divisor of the integral * gain of a PID component * @param pHandle Handle on the PID component * * The divisors that make the gains of the @ref PIDRegulator "PID regulator component" are * powers of two. * * @sa PID_GetKIDivisor() */ __weak uint16_t PID_GetKIDivisorPOW2(PID_Handle_t *pHandle) //cstat !MISRAC2012-Rule-8.13 { #ifdef NULL_PTR_CHECK_PID_REG return ((MC_NULL == pHandle) ? 0U : pHandle->hKiDivisorPOW2); #else return (pHandle->hKiDivisorPOW2); #endif } /** * @brief Sets the power of two that makes @f$K_{id}@f$, the divisor of the integral gain * of a PID component * @param pHandle Handle on the PID component * @param hKiDivisorPOW2 new @f$K_{id}@f$ divisor value, expressed as power of 2 * * The divisors that make the gains of the @ref PIDRegulator "PID regulator component" are * powers of two. * * This function sets @f$K_{id}@f$ to 2 to the power of @p hKiDivisorPOW2. * * Note that the upper and lower limits of the integral term are also updated to * accept any 16-bit value. If the limits of the integral term need to be different * use the PID_SetUpperIntegralTermLimit() and PID_SetLowerIntegralTermLimit() functions * after this one. */ __weak void PID_SetKIDivisorPOW2(PID_Handle_t *pHandle, uint16_t hKiDivisorPOW2) { #ifdef NULL_PTR_CHECK_PID_REG if (MC_NULL == pHandle) { /* Nothing to do */ } else { #endif uint32_t wKiDiv = (((uint32_t)1) << hKiDivisorPOW2); pHandle->hKiDivisorPOW2 = hKiDivisorPOW2; pHandle->hKiDivisor = (uint16_t)wKiDiv; PID_SetUpperIntegralTermLimit(pHandle, (int32_t)INT16_MAX * (int32_t)wKiDiv); PID_SetLowerIntegralTermLimit(pHandle, (int32_t)(-INT16_MAX) * (int32_t)wKiDiv); #ifdef NULL_PTR_CHECK_PID_REG } #endif } /** * @brief Sets the lower limit of the integral term of a PID component * * @param pHandle Handle on the PID component * @param wLowerLimit new lower integral term limit multiplied by the divisor * of the integral gain * * If @f$T_{iL}@f$ is the target lower limit, the @p wLowerLimit parameter must * be set to @f$T_{iL}\times K_{id}@f$. This is because the limit is checked before * applying the divisor in the PI_Controller() and PID_Controller() controller * functions. * * When the PI or PID controller is executed, the value of the integral term is floored * to this value. * * @attention @p wLowerLimit divided by @f$K_{id}@f$ must fit in a 16-bit signed * integer value. */ __weak void PID_SetLowerIntegralTermLimit(PID_Handle_t *pHandle, int32_t wLowerLimit) { #ifdef NULL_PTR_CHECK_PID_REG if (MC_NULL == pHandle) { /* Nothing to do */ } else { #endif pHandle->wLowerIntegralLimit = wLowerLimit; #ifdef NULL_PTR_CHECK_PID_REG } #endif } /** * @brief Sets the upper limit of the integral term of a PID component * * @param pHandle Handle on the PID component * @param wUpperLimit new upper integral term limit multiplied by the divisor * of the integral gain * * If @f$T_{iU}@f$ is the target upper limit, the @p wUpperLimit parameter must * be set to @f$T_{iU}\times K_{id}@f$. This is because the limit is checked before * applying the divisor in the PI_Controller() and PID_Controller() controller * functions. * * When the controller is executed, the value of the integral term is capped to * this value. * * @attention @p wUpperLimit divided by @f$K_{id}@f$ must fit in a 16-bit signed * integer value. */ __weak void PID_SetUpperIntegralTermLimit(PID_Handle_t *pHandle, int32_t wUpperLimit) { #ifdef NULL_PTR_CHECK_PID_REG if (MC_NULL == pHandle) { /* Nothing to do */ } else { #endif pHandle->wUpperIntegralLimit = wUpperLimit; #ifdef NULL_PTR_CHECK_PID_REG } #endif } /** * @brief Sets the lower output limit of a PID component * * @param pHandle Handle on the PID component * @param hLowerLimit new lower limit of the output value * * When the controller is executed, the value of its output is floored to this value. */ __weak void PID_SetLowerOutputLimit(PID_Handle_t *pHandle, int16_t hLowerLimit) { #ifdef NULL_PTR_CHECK_PID_REG if (MC_NULL == pHandle) { /* Nothing to do */ } else { #endif pHandle->hLowerOutputLimit = hLowerLimit; #ifdef NULL_PTR_CHECK_PID_REG } #endif } /** * @brief Sets the upper output limit of a PID component * * @param pHandle Handle on the PID component * @param hUpperLimit: new upper limit of the output value * * When the controller is executed, the value of its output is capped to this value. */ __weak void PID_SetUpperOutputLimit(PID_Handle_t *pHandle, int16_t hUpperLimit) { #ifdef NULL_PTR_CHECK_PID_REG if (MC_NULL == pHandle) { /* Nothing to do */ } else { #endif pHandle->hUpperOutputLimit = hUpperLimit; #ifdef NULL_PTR_CHECK_PID_REG } #endif } /** * @brief Sets the value of the previous process error of a PID component * * @param pHandle Handle on the PID component * @param wPrevProcessVarError new value of the previous error variable */ __weak void PID_SetPrevError(PID_Handle_t *pHandle, int32_t wPrevProcessVarError) { #ifdef NULL_PTR_CHECK_PID_REG if (MC_NULL == pHandle) { /* Nothing to do */ } else { #endif pHandle->wPrevProcessVarError = wPrevProcessVarError; #ifdef NULL_PTR_CHECK_PID_REG } #endif return; } /** * @brief Sets @f$K_{dg}@f$, the numerator of the derivative gain of a PID component * @param pHandle Handle on the PID component * @param hKpGain New @f$K_{dg}@f$ value */ __weak void PID_SetKD(PID_Handle_t *pHandle, int16_t hKdGain) { #ifdef NULL_PTR_CHECK_PID_REG if (MC_NULL == pHandle) { /* Nothing to do */ } else { #endif pHandle->hKdGain = hKdGain; #ifdef NULL_PTR_CHECK_PID_REG } #endif } /** * @brief Returns @f$K_{dg}@f$, the numerator of the derivative gain of a PID component * @param pHandle Handle on the PID component */ __weak int16_t PID_GetKD(PID_Handle_t *pHandle) //cstat !MISRAC2012-Rule-8.13 { #ifdef NULL_PTR_CHECK_PID_REG return ((MC_NULL == pHandle) ? 0 : pHandle->hKdGain); #else return (pHandle->hKdGain); #endif } /** * @brief Returns @f$K_{dd}@f$, the divisor of the derivative gain of a PID component * @param pHandle Handle on the PID component * * The divisors that make the gains of the @ref PIDRegulator "PID regulator component" are * powers of two. * * @sa PID_GetKDDivisorPOW2() */ __weak uint16_t PID_GetKDDivisor(PID_Handle_t *pHandle) //cstat !MISRAC2012-Rule-8.13 { #ifdef NULL_PTR_CHECK_PID_REG return ((MC_NULL == pHandle) ? 0U : pHandle->hKdDivisor); #else return (pHandle->hKdDivisor); #endif } /** * @brief Returns the power of two that makes @f$K_{dd}@f$, the divisor of the derivative * gain of a PID component * @param pHandle Handle on the PID component * * The divisors that make the gains of the @ref PIDRegulator "PID regulator component" are * powers of two. * * @sa PID_GetKDDivisor() */ __weak uint16_t PID_GetKDDivisorPOW2(PID_Handle_t *pHandle) //cstat !MISRAC2012-Rule-8.13 { #ifdef NULL_PTR_CHECK_PID_REG return ((MC_NULL == pHandle) ? 0U : pHandle->hKdDivisorPOW2); #else return (pHandle->hKdDivisorPOW2); #endif } /** * @brief Sets the power of two that makes @f$K_{dd}@f$, the divisor of the derivative gain * of a PID component * @param pHandle Handle on the PID component * @param hKdDivisorPOW2 new @f$K_{dd}@f$ divisor value, expressed as power of 2 * * The divisors that make the gains of the @ref PIDRegulator "PID regulator component" are * powers of two. * * This function sets @f$K_{dd}@f$ to 2 to the power of @p hKdDivisorPOW2. */ __weak void PID_SetKDDivisorPOW2(PID_Handle_t *pHandle, uint16_t hKdDivisorPOW2) { #ifdef NULL_PTR_CHECK_PID_REG if (MC_NULL == pHandle) { /* Nothing to do */ } else { #endif pHandle->hKdDivisorPOW2 = hKdDivisorPOW2; pHandle->hKdDivisor = (((uint16_t)1) << hKdDivisorPOW2); #ifdef NULL_PTR_CHECK_PID_REG } #endif } #if defined (CCMRAM) #if defined (__ICCARM__) #pragma location = ".ccmram" #elif defined (__CC_ARM) || defined(__GNUC__) __attribute__((section(".ccmram"))) #endif #endif /** * @brief Computes the output of a PI Regulator component, sum of its proportional and * integral terms * * @param pHandle Handle on the PID component * @param wProcessVarError current process variable error (the reference value minus the * present process variable value) * @retval computed PI controller output * * This function implements the proportional-integral controller function described by the * @ref PIDRegulator "PID regulator component". The integral term is saturated by the upper * and lower intergral term limit values before it is added to the proportional term. * * The resulting value is then saturated by the upper and lower output limit values before * being returned. */ __weak int16_t PI_Controller(PID_Handle_t *pHandle, int32_t wProcessVarError) { int16_t returnValue; #ifdef NULL_PTR_CHECK_PID_REG if (MC_NULL == pHandle) { returnValue = 0; } else { #endif int32_t wProportional_Term; int32_t wIntegral_Term; int32_t wOutput_32; int32_t wIntegral_sum_temp; int32_t wDischarge = 0; int16_t hUpperOutputLimit = pHandle->hUpperOutputLimit; int16_t hLowerOutputLimit = pHandle->hLowerOutputLimit; /* Proportional term computation*/ wProportional_Term = pHandle->hKpGain * wProcessVarError; /* Integral term computation */ if (0 == pHandle->hKiGain) { pHandle->wIntegralTerm = 0; } else { wIntegral_Term = pHandle->hKiGain * wProcessVarError; wIntegral_sum_temp = pHandle->wIntegralTerm + wIntegral_Term; if (wIntegral_sum_temp < 0) { if (pHandle->wIntegralTerm > 0) { if (wIntegral_Term > 0) { wIntegral_sum_temp = INT32_MAX; } else { /* Nothing to do */ } } else { /* Nothing to do */ } } else { if (pHandle->wIntegralTerm < 0) { if (wIntegral_Term < 0) { wIntegral_sum_temp = -INT32_MAX; } else { /* Nothing to do */ } } else { /* Nothing to do */ } } if (wIntegral_sum_temp > pHandle->wUpperIntegralLimit) { pHandle->wIntegralTerm = pHandle->wUpperIntegralLimit; } else if (wIntegral_sum_temp < pHandle->wLowerIntegralLimit) { pHandle->wIntegralTerm = pHandle->wLowerIntegralLimit; } else { pHandle->wIntegralTerm = wIntegral_sum_temp; } } #ifndef FULL_MISRA_C_COMPLIANCY_PID_REGULATOR /* WARNING: the below instruction is not MISRA compliant, user should verify that Cortex-M3 assembly instruction ASR (arithmetic shift right) is used by the compiler to perform the shifts (instead of LSR logical shift right)*/ //cstat !MISRAC2012-Rule-1.3_n !ATH-shift-neg !MISRAC2012-Rule-10.1_R6 wOutput_32 = (wProportional_Term >> pHandle->hKpDivisorPOW2) + (pHandle->wIntegralTerm >> pHandle->hKiDivisorPOW2); #else wOutput_32 = (wProportional_Term / (int32_t)pHandle->hKpDivisor) + (pHandle->wIntegralTerm / (int32_t)pHandle->hKiDivisor); #endif if (wOutput_32 > hUpperOutputLimit) { wDischarge = hUpperOutputLimit - wOutput_32; wOutput_32 = hUpperOutputLimit; } else if (wOutput_32 < hLowerOutputLimit) { wDischarge = hLowerOutputLimit - wOutput_32; wOutput_32 = hLowerOutputLimit; } else { /* Nothing to do here */ } pHandle->wIntegralTerm += wDischarge; returnValue = (int16_t)wOutput_32; #ifdef NULL_PTR_CHECK_PID_REG } #endif return (returnValue); } #if defined (CCMRAM) #if defined (__ICCARM__) #pragma location = ".ccmram" #elif defined (__CC_ARM) || defined(__GNUC__) __attribute__((section(".ccmram"))) #endif #endif /** * @brief Computes the output of a PID Regulator component, sum of its proportional, * integral and derivative terms * * @param pHandle Handle on the PID component * @param wProcessVarError current process variable error (the reference value minus the * present process variable value) * @retval computed PID controller output * * This function implements the proportional-integral-derivative controller function described * by the @ref PIDRegulator "PID regulator component". The integral term is saturated by * the upper and lower intergral term limit values before it is added to the proportional term * and derivative terms. * * The resulting value is then saturated by the upper and lower output limit values before * being returned. */ __weak int16_t PID_Controller(PID_Handle_t *pHandle, int32_t wProcessVarError) { int16_t returnValue; #ifdef NULL_PTR_CHECK_PID_REG if (MC_NULL == pHandle) { returnValue = 0; } else { #endif int32_t wDifferential_Term; int32_t wDeltaError; int32_t wTemp_output; if (0 == pHandle->hKdGain) /* derivative terms not used */ { wTemp_output = PI_Controller(pHandle, wProcessVarError); } else { wDeltaError = wProcessVarError - pHandle->wPrevProcessVarError; wDifferential_Term = pHandle->hKdGain * wDeltaError; #ifndef FULL_MISRA_C_COMPLIANCY_PID_REGULATOR /* WARNING: the below instruction is not MISRA compliant, user should verify that Cortex-M3 assembly instruction ASR (arithmetic shift right) is used by the compiler to perform the shifts (instead of LSR logical shift right)*/ //cstat !MISRAC2012-Rule-1.3_n !ATH-shift-neg !MISRAC2012-Rule-10.1_R6 wDifferential_Term >>= pHandle->hKdDivisorPOW2; #else wDifferential_Term /= ((int32_t)pHandle->hKdDivisor); #endif pHandle->wPrevProcessVarError = wProcessVarError; wTemp_output = PI_Controller(pHandle, wProcessVarError) + wDifferential_Term; if (wTemp_output > pHandle->hUpperOutputLimit) { wTemp_output = pHandle->hUpperOutputLimit; } else if (wTemp_output < pHandle->hLowerOutputLimit) { wTemp_output = pHandle->hLowerOutputLimit; } else { /* Nothing to do */ } } returnValue = (int16_t) wTemp_output; #ifdef NULL_PTR_CHECK_PID_REG } #endif return (returnValue); } /** * @} */ /** * @} */ /************************ (C) COPYRIGHT 2023 STMicroelectronics *****END OF FILE****/
26,857
C
29.835821
125
0.657743
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/MCSDK_v6.2.0-Full/MotorControl/MCSDK/MCLib/Any/Src/mcptl.c
/** ****************************************************************************** * @file mcptl.c * @author Motor Control SDK Team, ST Microelectronics * @brief * * ****************************************************************************** * @attention * * <h2><center>&copy; Copyright (c) 2023 STMicroelectronics. * All rights reserved.</center></h2> * * This software component is licensed by ST under Ultimate Liberty license * SLA0044, the "License"; You may not use this file except in compliance with * the License. You may obtain a copy of the License at: * www.st.com/SLA0044 * ****************************************************************************** */ #include "mcptl.h" bool MCTL_decodeCRCData(MCTL_Handle_t *pHandle) { return (true); } /******************* (C) COPYRIGHT 2023 STMicroelectronics *****END OF FILE****/
919
C
28.677418
80
0.452666
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/MCSDK_v6.2.0-Full/MotorControl/MCSDK/MCLib/Any/Src/profiler_fluxestim.c
/** ****************************************************************************** * @file profiler_fluxestim.c * @author Motor Control SDK Team, ST Microelectronics * @brief This file provides firmware functions of profiler * component * ****************************************************************************** * @attention * * <h2><center>&copy; Copyright (c) 2023 STMicroelectronics. * All rights reserved.</center></h2> * * This software component is licensed by ST under Ultimate Liberty license * SLA0044, the "License"; You may not use this file except in compliance with * the License. You may obtain a copy of the License at: * www.st.com/SLA0044 * ****************************************************************************** * */ /* Includes ------------------------------------------------------------------*/ #include "profiler_fluxestim.h" void PROFILER_FLUXESTIM_stateMachine(PROFILER_FLUXESTIM_Handle handle, MOTOR_Handle motor); /** * @brief todo */ void PROFILER_FLUXESTIM_init(PROFILER_FLUXESTIM_Handle handle) { handle->state = PROFILER_FLUXESTIM_STATE_Idle; } /** * @brief todo */ void PROFILER_FLUXESTIM_setParams(PROFILER_FLUXESTIM_Handle handle, PROFILER_Params* pParams) { /* Scaling */ handle->fullScaleVoltage_V = pParams->fullScaleVoltage_V; handle->fullScaleCurrent_A = pParams->fullScaleCurrent_A; handle->fullScaleFreq_Hz = pParams->fullScaleFreq_Hz; /* Number of medium frequency task cycles in the dc measurement time */ handle->background_rate_hz = pParams->background_rate_hz; PROFILER_FLUXESTIM_setMeasurementTime(handle, pParams->fluxestim_measurement_time_s); handle->CurToRamp_sf_pu = FIXP30(1.0f / pParams->fluxestim_current_ramping_time_s / pParams->background_rate_hz); handle->FreqToRamp_sf_pu = FIXP30(1.0f / pParams->fluxestim_speed_ramping_time_s / pParams->background_rate_hz); /* Current setpoint */ handle->Id_ref_pu = FIXP30(pParams->fluxestim_Idref_A / pParams->fullScaleCurrent_A); /* Flux estimation frequency */ PROFILER_FLUXESTIM_setFluxEstFreq_Hz(handle, pParams->fluxestim_speed_Hz); /* Ramp rate */ handle->ramp_rate_freq_pu_per_cycle = FIXP30_mpy(handle->FluxEstFreq_pu, handle->FreqToRamp_sf_pu); handle->ramp_rate_A_pu_per_cycle = FIXP30_mpy(handle->Id_ref_pu, handle->CurToRamp_sf_pu); handle->voltagefilterpole_rps = pParams->voltagefilterpole_rps; handle->glue_dcac_fluxestim_on = pParams->glue_dcac_fluxestim_on; /* tune fluxEstimPole to 1/7 of measurement time to get 0.999 expectation ratio */ handle->fluxEstimPoleTs = FIXP30(7.0f /(pParams->fluxestim_measurement_time_s * pParams->background_rate_hz)); } /** * @brief todo */ void PROFILER_FLUXESTIM_run(PROFILER_FLUXESTIM_Handle handle, MOTOR_Handle motor) { /* This function is called from the high frequency task / motor control interrupt */ /* After Sensorless and CurrentControl, so the DQ-values are available */ } /** * @brief todo */ void PROFILER_FLUXESTIM_runBackground(PROFILER_FLUXESTIM_Handle handle, MOTOR_Handle motor) { /* This function is called from the medium frequency task */ /* Counter counts down */ if (handle->counter > 0) handle->counter--; switch (handle->state) { case PROFILER_FLUXESTIM_STATE_Idle: handle->flux_Wb = FIXP30(0.0f); /* clear flux filter before new estimate */ break; /* Current ramping */ case PROFILER_FLUXESTIM_STATE_RampUpCur: { fixp30_t ramp_rate = handle->ramp_rate_A_pu_per_cycle; fixp30_t delta_pu = handle->Id_ref_pu - motor->pSTC->Idq_ref_pu.D; /* delta = target - current */ motor->pSTC->Idq_ref_pu.D += FIXP_sat(delta_pu, ramp_rate, -ramp_rate); } break; case PROFILER_FLUXESTIM_STATE_RampUpFreq: { fixp30_t ramp_rate = handle->ramp_rate_freq_pu_per_cycle; fixp30_t delta_pu = handle->FluxEstFreq_pu - motor->pSTC->speed_ref_pu; /* delta = target - current */ motor->pSTC->speed_ref_pu += FIXP_sat(delta_pu, ramp_rate, -ramp_rate); } break; case PROFILER_FLUXESTIM_STATE_FluxRefAuto: { /* read and filter actual flux amplitude and copy to reference value: equilibrium will be found, use 7tau for accuracy */ handle->flux_Wb += FIXP30_mpy((HSO_getFluxAmpl_Wb(motor->pSPD->pHSO) - handle->flux_Wb), handle->fluxEstimPoleTs); HSO_setFluxRef_Wb(motor->pSPD->pHSO, handle->flux_Wb); /* Update user visible rated flux */ motor->pFocVars->Flux_Rated_VpHz = FIXP30_mpy(handle->flux_Wb, FIXP(M_TWOPI)); } break; case PROFILER_FLUXESTIM_STATE_RampDownFreq: { fixp30_t ramp_rate = handle->ramp_rate_freq_pu_per_cycle; fixp30_t delta_pu = 0 - motor->pSTC->speed_ref_pu; /* delta = target - present */ motor->pSTC->speed_ref_pu += FIXP_sat(delta_pu, ramp_rate, -ramp_rate); } break; case PROFILER_FLUXESTIM_STATE_RampDownCur: { fixp30_t ramp_rate = handle->ramp_rate_A_pu_per_cycle; fixp30_t delta_pu = 0 - motor->pSTC->Idq_ref_pu.D; /* delta = target - current */ motor->pSTC->Idq_ref_pu.D += FIXP_sat(delta_pu, ramp_rate, -ramp_rate); } break; case PROFILER_FLUXESTIM_STATE_Error: break; case PROFILER_FLUXESTIM_STATE_Complete: break; } PROFILER_FLUXESTIM_stateMachine(handle, motor); } /** * @brief todo */ void PROFILER_FLUXESTIM_reset(PROFILER_FLUXESTIM_Handle handle, MOTOR_Handle motor) { handle->state = PROFILER_FLUXESTIM_STATE_Idle; handle->error = PROFILER_FLUXESTIM_ERROR_None; motor->pSTC->Idq_ref_pu.D = 0; motor->pSTC->Idq_ref_pu.Q = 0; motor->pSTC->speed_ref_pu = 0; } /** * @brief todo */ void PROFILER_FLUXESTIM_stateMachine(PROFILER_FLUXESTIM_Handle handle, MOTOR_Handle motor) { PROFILER_FLUXESTIM_State_e state = handle->state; PROFILER_FLUXESTIM_State_e newState = state; switch (state) { case PROFILER_FLUXESTIM_STATE_Idle: /* Only called when profiler active */ /* Set motor to required state */ { /* Open loop current mode */ motor->pSTC->SpeedControlEnabled = false; motor->pCurrCtrl->currentControlEnabled = true; motor->pSPD->closedLoopAngleEnabled = false; motor->pCurrCtrl->forceZeroPwm = false; if (handle->glue_dcac_fluxestim_on == false) { motor->pCurrCtrl->Ddq_ref_pu.D = 0; } motor->pCurrCtrl->Ddq_ref_pu.Q = 0; motor->pSTC->speedref_source = FOC_SPEED_SOURCE_Default; motor->pSTC->speed_ref_active_pu = 0; motor->pSTC->speed_ref_ramped_pu = 0; motor->pSTC->speed_ref_pu = FIXP30(0.0f); motor->pSPD->zestControl.enableControlUpdate = false; /* Disable MC_ZEST_Control_Update() */ motor->pSPD->flagHsoAngleEqualsOpenLoopAngle = false; /* enable hso to follow EMF */ ZEST_setInjectMode(motor->pSPD->pZeST, ZEST_INJECTMODE_None); if (handle->glue_dcac_fluxestim_on) { motor->pSTC->Idq_ref_pu.D = motor->pCurrCtrl->Idq_in_pu.D; /* Copy present current to reference */ PIDREGDQX_CURRENT_setUiD_pu(&motor->pCurrCtrl->pid_IdIqX_obj,motor->pCurrCtrl->Ddq_ref_pu.D); /* Copy present duty to current controller integrator */ } } { float_t myfreq = 100.0f; /* Set Flux observer cross-over frequency */ HSO_setMinCrossOver_Hz(motor->pSPD->pHSO, myfreq); HSO_setMaxCrossOver_Hz(motor->pSPD->pHSO, myfreq); handle->flux_Wb = FIXP30(0.0f); /* Set Ref-flux to zero before speed rampup */ HSO_setFluxRef_Wb(motor->pSPD->pHSO, handle->flux_Wb); } newState = PROFILER_FLUXESTIM_STATE_RampUpCur; break; case PROFILER_FLUXESTIM_STATE_RampUpCur: /* open-loop current mode */ if (motor->pSTC->Idq_ref_pu.D == handle->Id_ref_pu) { motor->pSTC->I_max_pu = FIXP24_mpy(handle->Id_ref_pu, FIXP24(2.0f)); newState = PROFILER_FLUXESTIM_STATE_RampUpFreq; } break; case PROFILER_FLUXESTIM_STATE_RampUpFreq: { Voltages_Uab_t Uab = motor->pPWM->Uab_in_pu; fixp30_t Umag = FIXP_mag(Uab.A, Uab.B); /* ramp speed up till set frequency or 50% of available duty */ if ((motor->pSTC->speed_ref_pu == handle->FluxEstFreq_pu) || (Umag > (motor->pVBus->Udcbus_in_pu >> 2))) { /* Prepare the Flux measurement */ handle->freqEst_pu = motor->pSTC->speed_ref_pu; //copy actual OL speed /* set new Flux observer cross-over frequency was 100Hz up to now*/ float_t myfreq = 2 * FIXP30_toF(handle->freqEst_pu) * handle->fullScaleFreq_Hz; HSO_setMinCrossOver_Hz(motor->pSPD->pHSO, myfreq); HSO_setMaxCrossOver_Hz(motor->pSPD->pHSO, myfreq); handle->counter = handle->measurement_counts; newState = PROFILER_FLUXESTIM_STATE_FluxRefAuto; } } break; case PROFILER_FLUXESTIM_STATE_FluxRefAuto: if (handle->counter == 0) { handle->freqHSO_pu = HSO_getSpeedLP_pu(motor->pSPD->pHSO); //copy actual HSO speed fixp30_t freqHSO = FIXP_abs(handle->freqHSO_pu); fixp30_t freqRef = FIXP_abs(handle->freqEst_pu); if ((freqHSO < FIXP_mpy(freqRef, FIXP(0.60))) || (freqHSO > FIXP_mpy(freqRef, FIXP(1.50)))) { handle->error = PROFILER_FLUXESTIM_ERROR_FluxNotValid; } newState = PROFILER_FLUXESTIM_STATE_RampDownFreq; } break; case PROFILER_FLUXESTIM_STATE_RampDownFreq: if (motor->pSTC->speed_ref_pu == FIXP30(0.0f)) { HSO_setMinCrossOver_Hz(motor->pSPD->pHSO, 20.0f); /* return to previous settings */ HSO_setMaxCrossOver_Hz(motor->pSPD->pHSO, 500.0f); //set Kp_speed to Imax / speed:(voltage/flux) newState = PROFILER_FLUXESTIM_STATE_RampDownCur; } break; case PROFILER_FLUXESTIM_STATE_RampDownCur: // Reduce current reference by ramp rate if (motor->pSTC->Idq_ref_pu.D == FIXP30(0.0f)) { /* Done, set motor control back to normal functioning */ ZEST_setInjectMode(motor->pSPD->pZeST, ZEST_INJECTMODE_Auto); motor->pSPD->flagDisableImpedanceCorrection = false; /* Enable Impedance Correction */ motor->pSPD->zestControl.enableControlUpdate = true; /* Back to default situation */ if (handle->error != PROFILER_FLUXESTIM_ERROR_None) { newState = PROFILER_FLUXESTIM_STATE_Error; } else { newState = PROFILER_FLUXESTIM_STATE_Complete; } } break; case PROFILER_FLUXESTIM_STATE_Complete: /* No action */ break; case PROFILER_FLUXESTIM_STATE_Error: /* No action */ break; } if (state != newState) { handle->state = newState; } } /* Accessors */ /** * @brief todo */ float_t PROFILER_FLUXESTIM_getFluxEstFreq_Hz(const PROFILER_FLUXESTIM_Handle handle) { return FIXP30_toF(handle->FluxEstFreq_pu) * handle->fullScaleFreq_Hz; } /** * @brief todo */ float_t PROFILER_FLUXESTIM_getMeasurementTime(PROFILER_FLUXESTIM_Handle handle) { return handle->measurement_time_s; } /** * @brief todo */ void PROFILER_FLUXESTIM_setFluxEstFreq_Hz(PROFILER_FLUXESTIM_Handle handle, const float_t fluxEstFreq_Hz) { handle->FluxEstFreq_pu = FIXP30(fluxEstFreq_Hz / handle->fullScaleFreq_Hz); } /** * @brief todo */ void PROFILER_FLUXESTIM_setMeasurementTime(PROFILER_FLUXESTIM_Handle handle, const float_t time_seconds) { handle->measurement_time_s = time_seconds; handle->measurement_counts = (uint32_t) (time_seconds * handle->background_rate_hz); } /************************ (C) COPYRIGHT 2023 STMicroelectronics *****END OF FILE****/
11,111
C
32.071428
154
0.671857
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/MCSDK_v6.2.0-Full/MotorControl/MCSDK/MCLib/Any/Src/sto_cordic_speed_pos_fdbk.c
/** ****************************************************************************** * @file sto_cordic_speed_pos_fdbk.c * @author Motor Control SDK Team, ST Microelectronics * @brief This file provides firmware functions that implement the features * of the State Observer + CORDIC Speed & Position Feedback component of the * Motor Control SDK. * ****************************************************************************** * @attention * * <h2><center>&copy; Copyright (c) 2023 STMicroelectronics International N.V. * All rights reserved.</center></h2> * * Redistribution and use in source and binary forms, with or without * modification, are permitted, provided that the following conditions are met: * * 1. Redistribution of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of STMicroelectronics nor the names of other * contributors to this software may be used to endorse or promote products * derived from this software without specific written permission. * 4. This software, including modifications and/or derivative works of this * software, must execute solely and exclusively on microcontroller or * microprocessor devices manufactured by or for STMicroelectronics. * 5. Redistribution and use of this software other than as permitted under * this license is void and will automatically terminate your rights under * this license. * * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * ****************************************************************************** */ /* Includes ------------------------------------------------------------------*/ #include "sto_cordic_speed_pos_fdbk.h" #include "mc_math.h" #include "mc_type.h" /** @addtogroup MCSDK * @{ */ /** @addtogroup SpeednPosFdbk * @{ */ /** @defgroup STO_CORDIC_SpeednPosFdbk State Observer CORDIC Speed & Position Feedback * @brief State Observer with CORDIC Speed & Position Feedback implementation * * This component uses a State Observer coupled with a CORDIC (COordinate Rotation DIgital * Computer) to provide an estimation of the speed and the position of the rotor of the motor. * * @todo Document the State Observer + CORDIC Speed & Position Feedback "module". * * @{ */ /* Private defines -----------------------------------------------------------*/ #define C6_COMP_CONST1 (int32_t)1043038 #define C6_COMP_CONST2 (int32_t)10430 /* Private functions prototypes ----------------------------------------------*/ static void STO_CR_Store_Rotor_Speed(STO_CR_Handle_t *pHandle, int16_t hRotor_Speed, int16_t hOrRotor_Speed); static void STO_CR_InitSpeedBuffer(STO_CR_Handle_t *pHandle); /** * @brief It initializes the state observer object * @param pHandle: handler of the current instance of the STO CORDIC component * @retval none */ __weak void STO_CR_Init(STO_CR_Handle_t *pHandle) { #ifdef NULL_PTR_CHECK_STO_COR_SPD_POS_FDB if (NULL == pHandle) { /* Nothing to do */ } else { #endif int32_t wAux; int16_t htempk; pHandle->ConsistencyCounter = pHandle->StartUpConsistThreshold; pHandle->EnableDualCheck = true; wAux = (int32_t)1; pHandle->F3POW2 = 0u; htempk = (int16_t)(C6_COMP_CONST1 / (pHandle->hF2)); while (htempk != 0) { htempk /= (int16_t)2; wAux *= (int32_t)2; pHandle->F3POW2++; } pHandle->hF3 = (int16_t)wAux; wAux = (int32_t)(pHandle->hF2) * pHandle->hF3; pHandle->hC6 = (int16_t)(wAux / C6_COMP_CONST2); STO_CR_Clear(pHandle); /* Acceleration measurement set to zero */ pHandle->_Super.hMecAccelUnitP = 0; #ifdef NULL_PTR_CHECK_STO_COR_SPD_POS_FDB } #endif } #if defined (CCMRAM) #if defined (__ICCARM__) #pragma location = ".ccmram" #elif defined (__CC_ARM) || defined(__GNUC__) __attribute__((section(".ccmram"))) #endif #endif /** * @brief This method executes Luenberger state observer equations and calls * CORDIC with the purpose of computing a new speed estimation and * updating the estimated electrical angle. * @param pHandle: handler of the current instance of the STO CORDIC component. * pInputs pointer to the observer inputs structure. * @retval int16_t rotor electrical angle (s16Degrees) */ //cstat !MISRAC2012-Rule-8.13 __weak int16_t STO_CR_CalcElAngle(STO_CR_Handle_t *pHandle, Observer_Inputs_t *pInputs) { int16_t returnvalue; #ifdef NULL_PTR_CHECK_STO_COR_SPD_POS_FDB if ((NULL == pHandle) || (NULL == pInputs)) { returnvalue = 0; } else { #endif int32_t wAux; int32_t wDirection; int32_t wAux_Alpha; int32_t wAux_Beta; int32_t wIalfa_est_Next; int32_t wIbeta_est_Next; int32_t wBemf_alfa_est_Next; int32_t wBemf_beta_est_Next; int16_t hAux; int16_t hAux_Alfa; int16_t hAux_Beta; int16_t hIalfa_err; int16_t hIbeta_err; int16_t hRotor_Speed; int16_t hOrRotor_Speed; int16_t hRotor_Acceleration; int16_t hRotor_Angle; int16_t hValfa; int16_t hVbeta; int16_t hPrev_Rotor_Angle = pHandle->_Super.hElAngle; int16_t hPrev_Rotor_Speed = pHandle->_Super.hElSpeedDpp; int16_t hMax_Instant_Accel = pHandle->MaxInstantElAcceleration; if (pHandle->wBemf_alfa_est > ((int32_t)pHandle->hF2 * INT16_MAX)) { pHandle->wBemf_alfa_est = INT16_MAX * (int32_t)(pHandle->hF2); } else if (pHandle->wBemf_alfa_est <= (-INT16_MAX * (int32_t)(pHandle->hF2))) { pHandle->wBemf_alfa_est = -INT16_MAX * (int32_t)(pHandle->hF2); } else { /* Nothing to do */ } #ifndef FULL_MISRA_C_COMPLIANCY_STO_CORDIC //cstat !MISRAC2012-Rule-1.3_n !ATH-shift-neg !MISRAC2012-Rule-10.1_R6 hAux_Alfa = (int16_t)(pHandle->wBemf_alfa_est >> pHandle->F2LOG); #else hAux_Alfa = (int16_t)(pHandle->wBemf_alfa_est / pHandle->hF2); #endif if (pHandle->wBemf_beta_est > (INT16_MAX * (int32_t)(pHandle->hF2))) { pHandle->wBemf_beta_est = INT16_MAX * (int32_t)(pHandle->hF2); } else if (pHandle->wBemf_beta_est <= (-INT16_MAX * (int32_t)(pHandle->hF2))) { pHandle->wBemf_beta_est = -INT16_MAX * (int32_t)(pHandle->hF2); } else { /* Nothing to do */ } #ifndef FULL_MISRA_C_COMPLIANCY_STO_CORDIC //cstat !MISRAC2012-Rule-1.3_n !ATH-shift-neg !MISRAC2012-Rule-10.1_R6 hAux_Beta = (int16_t)(pHandle->wBemf_beta_est >> pHandle->F2LOG); #else hAux_Beta = (int16_t)(pHandle->wBemf_beta_est / pHandle->hF2); #endif if (pHandle->Ialfa_est > (INT16_MAX * (int32_t)(pHandle->hF1))) { pHandle->Ialfa_est = INT16_MAX * (int32_t)(pHandle->hF1); } else if (pHandle->Ialfa_est <= (-INT16_MAX * (int32_t)(pHandle->hF1))) { pHandle->Ialfa_est = -INT16_MAX * (int32_t)(pHandle->hF1); } else { /* Nothing to do */ } if (pHandle->Ibeta_est > (INT16_MAX * (int32_t)(pHandle->hF1))) { pHandle->Ibeta_est = INT16_MAX * (int32_t)(pHandle->hF1); } else if (pHandle->Ibeta_est <= (-INT16_MAX * (int32_t)(pHandle->hF1))) { pHandle->Ibeta_est = -INT16_MAX * (int32_t)(pHandle->hF1); } else { /* Nothing to do */ } #ifndef FULL_MISRA_C_COMPLIANCY_STO_CORDIC //cstat !MISRAC2012-Rule-1.3_n !ATH-shift-neg !MISRAC2012-Rule-10.1_R6 hIalfa_err = (int16_t)(pHandle->Ialfa_est >> pHandle->F1LOG); #else hIalfa_err = (int16_t)(pHandle->Ialfa_est / pHandle->hF1); #endif hIalfa_err = hIalfa_err - pInputs->Ialfa_beta.alpha; #ifndef FULL_MISRA_C_COMPLIANCY_STO_CORDIC //cstat !MISRAC2012-Rule-1.3_n !ATH-shift-neg !MISRAC2012-Rule-10.1_R6 hIbeta_err = (int16_t)(pHandle->Ibeta_est >> pHandle->F1LOG); #else hIbeta_err = (int16_t)(pHandle->Ibeta_est / pHandle->hF1); #endif hIbeta_err = hIbeta_err - pInputs->Ialfa_beta.beta; wAux = (int32_t)(pInputs->Vbus) * pInputs->Valfa_beta.alpha; #ifndef FULL_MISRA_C_COMPLIANCY_STO_CORDIC hValfa = (int16_t)(wAux >> 16); //cstat !MISRAC2012-Rule-1.3_n !ATH-shift-neg !MISRAC2012-Rule-10.1_R6 #else hValfa = (int16_t)(wAux / 65536); #endif wAux = (int32_t)(pInputs->Vbus) * pInputs->Valfa_beta.beta; #ifndef FULL_MISRA_C_COMPLIANCY_STO_CORDIC hVbeta = (int16_t)(wAux >> 16); //cstat !MISRAC2012-Rule-1.3_n !ATH-shift-neg !MISRAC2012-Rule-10.1_R6 #else hVbeta = (int16_t)(wAux / 65536); #endif /* Alfa axes observer */ #ifndef FULL_MISRA_C_COMPLIANCY_STO_CORDIC //cstat !MISRAC2012-Rule-1.3_n !ATH-shift-neg !MISRAC2012-Rule-10.1_R6 hAux = (int16_t)(pHandle->Ialfa_est >> pHandle->F1LOG); #else hAux = (int16_t)(pHandle->Ialfa_est / pHandle->hF1); #endif wAux = (int32_t)(pHandle->hC1) * hAux; wIalfa_est_Next = pHandle->Ialfa_est - wAux; wAux = (int32_t)(pHandle->hC2) * hIalfa_err; wIalfa_est_Next += wAux; wAux = (int32_t)(pHandle->hC5) * hValfa; wIalfa_est_Next += wAux; wAux = (int32_t)(pHandle->hC3) * hAux_Alfa; wIalfa_est_Next -= wAux; wAux = (int32_t)(pHandle->hC4) * hIalfa_err; wBemf_alfa_est_Next = pHandle->wBemf_alfa_est + wAux; #ifndef FULL_MISRA_C_COMPLIANCY_STO_CORDIC //cstat !MISRAC2012-Rule-1.3_n !ATH-shift-neg !MISRAC2012-Rule-10.1_R6 wAux = (int32_t) hAux_Beta >> pHandle->F3POW2; #else wAux = (int32_t)hAux_Beta / pHandle->hF3; #endif wAux = wAux * pHandle->hC6; wAux = hPrev_Rotor_Speed * wAux; wBemf_alfa_est_Next += wAux; /* Beta axes observer */ #ifndef FULL_MISRA_C_COMPLIANCY_STO_CORDIC //cstat !MISRAC2012-Rule-1.3_n !ATH-shift-neg !MISRAC2012-Rule-10.1_R6 hAux = (int16_t)(pHandle->Ibeta_est >> pHandle->F1LOG); #else hAux = (int16_t)(pHandle->Ibeta_est / pHandle->hF1); #endif wAux = (int32_t)(pHandle->hC1) * hAux; wIbeta_est_Next = pHandle->Ibeta_est - wAux; wAux = (int32_t)(pHandle->hC2) * hIbeta_err; wIbeta_est_Next += wAux; wAux = (int32_t)(pHandle->hC5) * hVbeta; wIbeta_est_Next += wAux; wAux = (int32_t)(pHandle->hC3) * hAux_Beta; wIbeta_est_Next -= wAux; wAux = (int32_t)(pHandle->hC4) * hIbeta_err; wBemf_beta_est_Next = pHandle->wBemf_beta_est + wAux; #ifndef FULL_MISRA_C_COMPLIANCY_STO_CORDIC //cstat !MISRAC2012-Rule-1.3_n !ATH-shift-neg !MISRAC2012-Rule-10.1_R6 wAux = (int32_t) hAux_Alfa >> pHandle->F3POW2; #else wAux = (int32_t)hAux_Alfa / pHandle->hF3; #endif wAux = wAux * pHandle->hC6; wAux = hPrev_Rotor_Speed * wAux; wBemf_beta_est_Next -= wAux; if (pHandle->Orig_ElSpeedDpp >= 0) { wDirection = 1; } else { wDirection = -1; } /* Stores observed b-emfs */ pHandle->hBemf_alfa_est = hAux_Alfa; pHandle->hBemf_beta_est = hAux_Beta; /* Calls the CORDIC blockset */ wAux_Alpha = pHandle->wBemf_alfa_est * wDirection; wAux_Beta = pHandle->wBemf_beta_est * wDirection; hRotor_Angle = MCM_PhaseComputation(wAux_Alpha, -wAux_Beta); hOrRotor_Speed = (int16_t)(hRotor_Angle - hPrev_Rotor_Angle); hRotor_Acceleration = hOrRotor_Speed - hPrev_Rotor_Speed; hRotor_Speed = hOrRotor_Speed; if (wDirection == 1) { if (hRotor_Speed < 0) { hRotor_Speed = 0; } else { if (hRotor_Acceleration > hMax_Instant_Accel) { hRotor_Speed = hPrev_Rotor_Speed + hMax_Instant_Accel; pHandle->_Super.hElAngle = hPrev_Rotor_Angle + hRotor_Speed; } else { pHandle->_Super.hElAngle = hRotor_Angle; } } } else { if (hRotor_Speed > 0) { hRotor_Speed = 0; } else { if (hRotor_Acceleration < (-hMax_Instant_Accel)) { hRotor_Speed = hPrev_Rotor_Speed - hMax_Instant_Accel; pHandle->_Super.hElAngle = hPrev_Rotor_Angle + hRotor_Speed; } else { pHandle->_Super.hElAngle = hRotor_Angle; } } } if (hRotor_Acceleration > hMax_Instant_Accel) { hOrRotor_Speed = hPrev_Rotor_Speed + hMax_Instant_Accel; } else if (hRotor_Acceleration < (-hMax_Instant_Accel)) { hOrRotor_Speed = hPrev_Rotor_Speed - hMax_Instant_Accel; } else { /* Nothing to do */ } STO_CR_Store_Rotor_Speed(pHandle, hRotor_Speed, hOrRotor_Speed); /* Storing previous values of currents and bemfs */ pHandle->Ialfa_est = wIalfa_est_Next; pHandle->wBemf_alfa_est = wBemf_alfa_est_Next; pHandle->Ibeta_est = wIbeta_est_Next; pHandle->wBemf_beta_est = wBemf_beta_est_Next; returnvalue = pHandle->_Super.hElAngle; #ifdef NULL_PTR_CHECK_STO_COR_SPD_POS_FDB } #endif return (returnvalue); } /** * @brief Computes the rotor average mechanical speed in the unit defined by * #SPEED_UNIT and writes it in pMecSpeedUnit * * This method must be called - at least - with the same periodicity * on which the speed control is executed. It computes and returns - through * parameter pMecSpeedUnit - the rotor average mechanical speed, expressed in * the unit defined by #SPEED_UNIT. Average is computed considering a FIFO depth * equal to STO_CR_Handle_t::SpeedBufferSizeUnit. * * Moreover it also computes and returns the reliability state of the sensor, * measured with reference to parameters STO_CR_Handle_t::Reliability_hysteresys, * STO_CR_Handle_t::VariancePercentage and STO_CR_Handle_t::SpeedBufferSizeUnit of * the STO_CR_Handle_t handle. * * @param pHandle handler of the current instance of the STO CORDIC component * @param pMecSpeedUnit pointer to int16_t, used to return the rotor average * mechanical speed * @retval true if the sensor information is reliable, false otherwise */ __weak bool STO_CR_CalcAvrgMecSpeedUnit(STO_CR_Handle_t *pHandle, int16_t *pMecSpeedUnit) { bool bAux = false; #ifdef NULL_PTR_CHECK_STO_COR_SPD_POS_FDB if ((NULL == pHandle) || (NULL == pMecSpeedUnit)) { /* Nothing to do */ } else { #endif int32_t wAvrSpeed_dpp = (int32_t)0; int32_t wError; int32_t wAux; int32_t wAvrSquareSpeed; int32_t wAvrQuadraticError = 0; int32_t wObsBemf, wEstBemf; int32_t wObsBemfSq = 0; int32_t wEstBemfSq = 0; int32_t wEstBemfSqLo; uint8_t i; uint8_t bSpeedBufferSizeUnit = pHandle->SpeedBufferSizeUnit; bool bIs_Speed_Reliable = false; bool bIs_Bemf_Consistent = false; for (i = 0U; i < bSpeedBufferSizeUnit; i++) { wAvrSpeed_dpp += (int32_t)(pHandle->Speed_Buffer[i]); } if ((uint8_t)0 == bSpeedBufferSizeUnit) { /* Nothing to do */ } else { wAvrSpeed_dpp = wAvrSpeed_dpp / (int16_t)bSpeedBufferSizeUnit; } /* Value is written during init and computed by MC Workbench and always >= 1 */ //cstat -MISRAC2012-Rule-1.3_d for (i = 0U; i < bSpeedBufferSizeUnit; i++) { wError = (int32_t)(pHandle->Speed_Buffer[i]) - wAvrSpeed_dpp; wError = (wError * wError); wAvrQuadraticError += wError; } /* It computes the measurement variance */ wAvrQuadraticError = wAvrQuadraticError / (int16_t)bSpeedBufferSizeUnit; //cstat +MISRAC2012-Rule-1.3_d /* The maximum variance acceptable is here calculated as a function of average speed */ wAvrSquareSpeed = wAvrSpeed_dpp * wAvrSpeed_dpp; int64_t lAvrSquareSpeed = (int64_t)(wAvrSquareSpeed) * (int64_t)pHandle->VariancePercentage; wAvrSquareSpeed = (int32_t)(lAvrSquareSpeed / (int64_t)128); if (wAvrQuadraticError < wAvrSquareSpeed) { bIs_Speed_Reliable = true; } else { /* Nothing to do */ } /* Computation of Mechanical speed unit */ wAux = wAvrSpeed_dpp * (int32_t)(pHandle->_Super.hMeasurementFrequency); wAux = wAux * (int32_t) (pHandle->_Super.SpeedUnit); wAux = wAux / (int32_t)(pHandle->_Super.DPPConvFactor); wAux = wAux / (int16_t)(pHandle->_Super.bElToMecRatio); *pMecSpeedUnit = (int16_t)wAux; pHandle->_Super.hAvrMecSpeedUnit = (int16_t)wAux; pHandle->IsSpeedReliable = bIs_Speed_Reliable; /* Bemf Consistency Check algorithm */ if (true == pHandle->EnableDualCheck) /*do algorithm if it's enabled*/ { /* wAux abs value */ /* False positive */ wAux = ((wAux < 0) ? (-wAux) : (wAux)); //cstat !MISRAC2012-Rule-14.3_b !RED-cond-never !RED-cmp-never if (wAux < (int32_t)(pHandle->MaxAppPositiveMecSpeedUnit)) { /* Computation of Observed back-emf */ wObsBemf = (int32_t)(pHandle->hBemf_alfa_est); wObsBemfSq = wObsBemf * wObsBemf; wObsBemf = (int32_t)(pHandle->hBemf_beta_est); wObsBemfSq += wObsBemf * wObsBemf; /* Computation of Estimated back-emf */ wEstBemf = (wAux * 32767) / (int16_t)(pHandle->_Super.hMaxReliableMecSpeedUnit); wEstBemfSq = (wEstBemf * (int32_t)(pHandle->BemfConsistencyGain)) / 64; wEstBemfSq *= wEstBemf; /* Computation of threshold */ wEstBemfSqLo = wEstBemfSq - ((wEstBemfSq / 64) * (int32_t)(pHandle->BemfConsistencyCheck)); /* Check */ if (wObsBemfSq > wEstBemfSqLo) { bIs_Bemf_Consistent = true; } else { /* Nothing to do */ } } else { /* Nothing to do */ } pHandle->IsBemfConsistent = bIs_Bemf_Consistent; pHandle->Obs_Bemf_Level = wObsBemfSq; pHandle->Est_Bemf_Level = wEstBemfSq; } else { bIs_Bemf_Consistent = true; } /* Decision making */ if (pHandle->IsAlgorithmConverged == false) { bAux = SPD_IsMecSpeedReliable(&pHandle->_Super, pMecSpeedUnit); } else { if ((pHandle->IsSpeedReliable == false) || (bIs_Bemf_Consistent == false)) { pHandle->ReliabilityCounter++; if (pHandle->ReliabilityCounter >= pHandle->Reliability_hysteresys) { pHandle->ReliabilityCounter = 0U; pHandle->_Super.bSpeedErrorNumber = pHandle->_Super.bMaximumSpeedErrorsNumber; bAux = false; } else { bAux = SPD_IsMecSpeedReliable (&pHandle->_Super, pMecSpeedUnit); } } else { pHandle->ReliabilityCounter = 0U; bAux = SPD_IsMecSpeedReliable (&pHandle->_Super, pMecSpeedUnit); } } #ifdef NULL_PTR_CHECK_STO_COR_SPD_POS_FDB } #endif return (bAux); } /** * @brief It clears state observer object by re-initializing private variables * @param pHandle pointer on the component instance to work on. */ __weak void STO_CR_Clear(STO_CR_Handle_t *pHandle) { #ifdef NULL_PTR_CHECK_STO_COR_SPD_POS_FDB if (NULL == pHandle) { /* Nothing to do */ } else { #endif pHandle->Ialfa_est = (int32_t)0; pHandle->Ibeta_est = (int32_t)0; pHandle->wBemf_alfa_est = (int32_t)0; pHandle->wBemf_beta_est = (int32_t)0; pHandle->_Super.hElAngle = (int16_t)0; pHandle->_Super.hElSpeedDpp = (int16_t)0; pHandle->Orig_ElSpeedDpp = (int16_t)0; pHandle->ConsistencyCounter = 0u; pHandle->ReliabilityCounter = 0u; pHandle->IsAlgorithmConverged = false; pHandle->IsBemfConsistent = false; pHandle->Obs_Bemf_Level = (int32_t)0; pHandle->Est_Bemf_Level = (int32_t)0; pHandle->DppBufferSum = (int32_t)0; pHandle->DppOrigBufferSum = (int32_t)0; pHandle->ForceConvergency = false; pHandle->ForceConvergency2 = false; STO_CR_InitSpeedBuffer(pHandle); #ifdef NULL_PTR_CHECK_STO_COR_SPD_POS_FDB } #endif } /** * @brief It stores in estimated speed FIFO latest calculated value of motor * speed * @param pHandle: handler of the current instance of the STO CORDIC component * @retval none */ inline static void STO_CR_Store_Rotor_Speed(STO_CR_Handle_t *pHandle, int16_t hRotor_Speed, int16_t hOrRotor_Speed) { uint8_t bBuffer_index = pHandle->Speed_Buffer_Index; bBuffer_index++; if (bBuffer_index == pHandle->SpeedBufferSizeUnit) { bBuffer_index = 0U; } else { /* Nothing too do */ } pHandle->SpeedBufferOldestEl = pHandle->Speed_Buffer[bBuffer_index]; pHandle->OrigSpeedBufferOldestEl = pHandle->Orig_Speed_Buffer[bBuffer_index]; pHandle->Speed_Buffer[bBuffer_index] = hRotor_Speed; pHandle->Orig_Speed_Buffer[bBuffer_index] = hOrRotor_Speed; pHandle->Speed_Buffer_Index = bBuffer_index; } /** * @brief It clears the estimated speed buffer * @param pHandle: handler of the current instance of the STO CORDIC component * @retval none */ static void STO_CR_InitSpeedBuffer(STO_CR_Handle_t *pHandle) { uint8_t b_i; uint8_t bSpeedBufferSizeUnit = pHandle->SpeedBufferSizeUnit; /*init speed buffer*/ for (b_i = 0U; b_i < bSpeedBufferSizeUnit; b_i++) { pHandle->Speed_Buffer[b_i] = (int16_t)0; pHandle->Orig_Speed_Buffer[b_i] = (int16_t)0; } pHandle->Speed_Buffer_Index = 0u; pHandle->SpeedBufferOldestEl = (int16_t)0; pHandle->OrigSpeedBufferOldestEl = (int16_t)0; } /** * @brief Returns true if the Observer has converged or false otherwise. * * Internally performs a set of checks necessary to state whether * the state observer algorithm has converged or not. * * This function is to be periodically called during the motor rev-up procedure * at the same frequency as the *_CalcElAngle() functions. * * It returns true if the estimated angle and speed can be considered reliable, * false otherwise. * * @param pHandle pointer on the component instance * @param hForcedMecSpeedUnit Mechanical speed as forced by VSS, in the unit defined by #SPEED_UNIT */ __weak bool STO_CR_IsObserverConverged(STO_CR_Handle_t *pHandle, int16_t hForcedMecSpeedUnit) { bool bAux = false; #ifdef NULL_PTR_CHECK_STO_COR_SPD_POS_FDB if (NULL == pHandle) { /* Nothing to do */ } else { #endif int32_t wAux; int32_t wtemp; int16_t hEstimatedSpeedUnit; int16_t hUpperThreshold; int16_t hLowerThreshold; int16_t lForcedMecSpeedUnit; if (pHandle->ForceConvergency2 == true) { lForcedMecSpeedUnit = pHandle->_Super.hAvrMecSpeedUnit; } else { lForcedMecSpeedUnit = hForcedMecSpeedUnit; } if (pHandle->ForceConvergency == true) { bAux = true; pHandle->IsAlgorithmConverged = true; pHandle->_Super.bSpeedErrorNumber = 0U; } else { hEstimatedSpeedUnit = pHandle->_Super.hAvrMecSpeedUnit; wtemp = (int32_t)hEstimatedSpeedUnit * (int32_t)lForcedMecSpeedUnit; if (wtemp > 0) { if (hEstimatedSpeedUnit < 0) { hEstimatedSpeedUnit = -hEstimatedSpeedUnit; } else { /* Nothing to do */ } if (lForcedMecSpeedUnit < 0) { lForcedMecSpeedUnit = -lForcedMecSpeedUnit; } else { /* Nothing to do */ } wAux = (int32_t) (lForcedMecSpeedUnit) * (int16_t)pHandle->SpeedValidationBand_H; hUpperThreshold = (int16_t)(wAux / (int32_t)16); wAux = (int32_t) (lForcedMecSpeedUnit) * (int16_t)pHandle->SpeedValidationBand_L; hLowerThreshold = (int16_t)(wAux / (int32_t)16); /* If the variance of the estimated speed is low enough... */ if (true == pHandle->IsSpeedReliable) { if ((uint16_t)hEstimatedSpeedUnit > pHandle->MinStartUpValidSpeed) { /* ...and the estimated value is quite close to the expected value... */ if (hEstimatedSpeedUnit >= hLowerThreshold) { if (hEstimatedSpeedUnit <= hUpperThreshold) { pHandle->ConsistencyCounter++; /* ...for hConsistencyThreshold consecutive times... */ if (pHandle->ConsistencyCounter >= pHandle->StartUpConsistThreshold) { /* The algorithm converged */ bAux = true; pHandle->IsAlgorithmConverged = true; pHandle->_Super.bSpeedErrorNumber = 0U; } } else { pHandle->ConsistencyCounter = 0U; } } else { pHandle->ConsistencyCounter = 0U; } } else { pHandle->ConsistencyCounter = 0U; } } else { pHandle->ConsistencyCounter = 0U; } } } #ifdef NULL_PTR_CHECK_STO_COR_SPD_POS_FDB } #endif return (bAux); } /* false positive */ //cstat -MISRAC2012-Rule-2.2_b /** * @brief It exports estimated Bemf alpha-beta in qd_t format * @param pHandle: handler of the current instance of the STO CORDIC component * @retval alphabeta_t Bemf alpha-beta */ //cstat !MISRAC2012-Rule-8.13 __weak alphabeta_t STO_CR_GetEstimatedBemf(STO_CR_Handle_t *pHandle) { alphabeta_t Vaux; #ifdef NULL_PTR_CHECK_STO_COR_SPD_POS_FDB if (NULL == pHandle) { Vaux.alpha = 0; Vaux.beta = 0; } else { #endif Vaux.alpha = pHandle->hBemf_alfa_est; Vaux.beta = pHandle->hBemf_beta_est; #ifdef NULL_PTR_CHECK_STO_COR_SPD_POS_FDB } #endif return (Vaux); } /** * @brief It exports the stator current alpha-beta as estimated by state * observer * @param pHandle: handler of the current instance of the STO CORDIC component * @retval alphabeta_t State observer estimated stator current Ialpha-beta */ //cstat !MISRAC2012-Rule-8.13 __weak alphabeta_t STO_CR_GetEstimatedCurrent(STO_CR_Handle_t *pHandle) { alphabeta_t Iaux; #ifdef NULL_PTR_CHECK_STO_COR_SPD_POS_FDB if (NULL == pHandle) { Iaux.alpha = 0; Iaux.beta = 0; } else { #endif #ifndef FULL_MISRA_C_COMPLIANCY_STO_CORDIC //cstat !MISRAC2012-Rule-1.3_n !ATH-shift-neg !MISRAC2012-Rule-10.1_R6 Iaux.alpha = (int16_t)(pHandle->Ialfa_est >> pHandle->F1LOG); #else Iaux.alpha = (int16_t)(pHandle->Ialfa_est / (pHandle->hF1)); #endif #ifndef FULL_MISRA_C_COMPLIANCY_STO_CORDIC //cstat !MISRAC2012-Rule-1.3_n !ATH-shift-neg !MISRAC2012-Rule-10.1_R6 Iaux.beta = (int16_t)(pHandle->Ibeta_est >> pHandle->F1LOG); #else Iaux.beta = (int16_t)(pHandle->Ibeta_est / (pHandle->hF1)); #endif #ifdef NULL_PTR_CHECK_STO_COR_SPD_POS_FDB } #endif return (Iaux); } //cstat +MISRAC2012-Rule-2.2_b /** * @brief It exports current observer gains through parameters hhC2 and hhC4 * @param pHandle: handler of the current instance of the STO CORDIC component * @param phC2 pointer to int16_t used to return parameters hhC2 * @param phC4 pointer to int16_t used to return parameters hhC4 * @retval none */ __weak void STO_CR_GetObserverGains(STO_CR_Handle_t *pHandle, int16_t *phC2, int16_t *phC4) { #ifdef NULL_PTR_CHECK_STO_COR_SPD_POS_FDB if ((NULL == pHandle) || (NULL == phC2) || (NULL == phC4)) { /* Nothing to do */ } else { #endif *phC2 = pHandle->hC2; *phC4 = pHandle->hC4; #ifdef NULL_PTR_CHECK_STO_COR_SPD_POS_FDB } #endif } /** * @brief It allows setting new values for observer gains * @param pHandle: handler of the current instance of the STO CORDIC component * @param wK1 new value for observer gain hhC1 * @param wK2 new value for observer gain hhC2 * @retval none */ __weak void STO_CR_SetObserverGains(STO_CR_Handle_t *pHandle, int16_t hhC1, int16_t hhC2) { #ifdef NULL_PTR_CHECK_STO_COR_SPD_POS_FDB if (NULL == pHandle) { /* Nothing to do */ } else { #endif pHandle->hC2 = hhC1; pHandle->hC4 = hhC2; #ifdef NULL_PTR_CHECK_STO_COR_SPD_POS_FDB } #endif } #if defined (CCMRAM) #if defined (__ICCARM__) #pragma location = ".ccmram" #elif defined (__CC_ARM) || defined(__GNUC__) __attribute__((section(".ccmram"))) #endif #endif /** * @brief This method must be called - at least - with the same periodicity * on which speed control is executed. It computes and update object * variable hElSpeedDpp that is estimated average electrical speed * expressed in dpp used for instance in observer equations. * Average is computed considering a FIFO depth equal to * bSpeedBufferSizedpp. * @param pHandle: handler of the current instance of the STO CORDIC component * @retval none */ __weak void STO_CR_CalcAvrgElSpeedDpp(STO_CR_Handle_t *pHandle) { #ifdef NULL_PTR_CHECK_STO_COR_SPD_POS_FDB if (NULL == pHandle) { /* Nothing to do */ } else { #endif int32_t wSum = pHandle->DppBufferSum; int32_t wSumOrig = pHandle->DppOrigBufferSum; int32_t wAvrSpeed_dpp; int16_t hSpeedBufferSizedpp = (int16_t)(pHandle->SpeedBufferSizedpp); int16_t hSpeedBufferSizeUnit = (int16_t)(pHandle->SpeedBufferSizeUnit); int16_t hBufferSizeDiff; int16_t hIndexNew = (int16_t)pHandle->Speed_Buffer_Index; int16_t hIndexOld; int16_t hIndexOldTemp; hBufferSizeDiff = hSpeedBufferSizeUnit - hSpeedBufferSizedpp; if (0 == hBufferSizeDiff) { wSum = wSum + pHandle->Speed_Buffer[hIndexNew] - pHandle->SpeedBufferOldestEl; wSumOrig = wSumOrig + pHandle->Orig_Speed_Buffer[hIndexNew] - pHandle->OrigSpeedBufferOldestEl; } else { hIndexOldTemp = hIndexNew + hBufferSizeDiff; if (hIndexOldTemp >= hSpeedBufferSizeUnit) { hIndexOld = hIndexOldTemp - hSpeedBufferSizeUnit; } else { hIndexOld = hIndexOldTemp; } wSum = wSum + pHandle->Speed_Buffer[hIndexNew] - pHandle->Speed_Buffer[hIndexOld]; wSumOrig = wSumOrig + pHandle->Orig_Speed_Buffer[hIndexNew] - pHandle->Orig_Speed_Buffer[hIndexOld]; } #ifndef FULL_MISRA_C_COMPLIANCY_STO_CORDIC //cstat !MISRAC2012-Rule-1.3_n !ATH-shift-neg !MISRAC2012-Rule-10.1_R6 wAvrSpeed_dpp = (int32_t)(wSum >> pHandle->SpeedBufferSizedppLOG); pHandle->_Super.hElSpeedDpp = (int16_t)wAvrSpeed_dpp; //cstat !MISRAC2012-Rule-1.3_n !ATH-shift-neg !MISRAC2012-Rule-10.1_R6 wAvrSpeed_dpp = (int32_t)(wSumOrig >> pHandle->SpeedBufferSizedppLOG); #else if (hSpeedBufferSizedpp != 0) { wAvrSpeed_dpp = wSum / hSpeedBufferSizedpp; pHandle->_Super.hElSpeedDpp = (int16_t)wAvrSpeed_dpp; wAvrSpeed_dpp = wSumOrig / hSpeedBufferSizedpp; } else { wAvrSpeed_dpp = (int32_t)0; } #endif pHandle->Orig_ElSpeedDpp = (int16_t)wAvrSpeed_dpp; pHandle->DppBufferSum = wSum; pHandle->DppOrigBufferSum = wSumOrig; #ifdef NULL_PTR_CHECK_STO_COR_SPD_POS_FDB } #endif } /** * @brief It exports estimated Bemf squared level * @param pHandle: handler of the current instance of the STO CORDIC component * @retval int32_t */ __weak int32_t STO_CR_GetEstimatedBemfLevel(const STO_CR_Handle_t *pHandle) { #ifdef NULL_PTR_CHECK_STO_COR_SPD_POS_FDB return ((NULL == pHandle) ? 0 : pHandle->Est_Bemf_Level); #else return (pHandle->Est_Bemf_Level); #endif } /** * @brief It exports observed Bemf squared level * @param pHandle: handler of the current instance of the STO CORDIC component * @retval int32_t */ __weak int32_t STO_CR_GetObservedBemfLevel(const STO_CR_Handle_t *pHandle) { #ifdef NULL_PTR_CHECK_STO_COR_SPD_POS_FDB return ((NULL == pHandle) ? 0 : pHandle->Obs_Bemf_Level); #else return (pHandle->Obs_Bemf_Level); #endif } /** * @brief It enables/disables the bemf consistency check * @param pHandle: handler of the current instance of the STO CORDIC component * @param bSel boolean; true enables check; false disables check */ __weak void STO_CR_BemfConsistencyCheckSwitch(STO_CR_Handle_t *pHandle, bool bSel) { #ifdef NULL_PTR_CHECK_STO_COR_SPD_POS_FDB if (NULL == pHandle) { /* Noything to do */ } else { #endif pHandle->EnableDualCheck = bSel; #ifdef NULL_PTR_CHECK_STO_COR_SPD_POS_FDB } #endif } /** * @brief It returns the result of the Bemf consistency check * @param pHandle: handler of the current instance of the STO CORDIC component * @retval bool Bemf consistency state */ __weak bool STO_CR_IsBemfConsistent(const STO_CR_Handle_t *pHandle) { #ifdef NULL_PTR_CHECK_STO_COR_SPD_POS_FDB return ((NULL == pHandle) ? false : pHandle->IsBemfConsistent); #else return (pHandle->IsBemfConsistent); #endif } /** * @brief This method returns the reliability of the speed sensor * @param pHandle: handler of the current instance of the STO CORDIC component * @retval bool speed sensor reliability, measured with reference to parameters * bReliability_hysteresys, hVariancePercentage and bSpeedBufferSize * true = sensor information is reliable * false = sensor information is not reliable */ __weak bool STO_CR_IsSpeedReliable(const STO_Handle_t *pHandle) { bool returnbool; #ifdef NULL_PTR_CHECK_STO_COR_SPD_POS_FDB if (NULL == pHandle) { returnbool = false; } else { #endif const STO_CR_Handle_t *pHdl = (STO_CR_Handle_t *)pHandle->_Super; //cstat !MISRAC2012-Rule-11.3 returnbool = pHdl->IsSpeedReliable; #ifdef NULL_PTR_CHECK_STO_COR_SPD_POS_FDB } #endif return (returnbool); } /* @brief It forces the state-observer to declare converged * @param pHandle: handler of the current instance of the STO CORDIC component */ __weak void STO_CR_ForceConvergency1(STO_Handle_t *pHandle) { #ifdef NULL_PTR_CHECK_STO_COR_SPD_POS_FDB if (NULL == pHandle) { /* Nothing to do */ } else { #endif STO_CR_Handle_t *pHdl = (STO_CR_Handle_t *)pHandle->_Super; //cstat !MISRAC2012-Rule-11.3 pHdl->ForceConvergency = true; #ifdef NULL_PTR_CHECK_STO_COR_SPD_POS_FDB } #endif } /* @brief It forces the state-observer to declare converged * @param pHandle: handler of the current instance of the STO CORDIC component */ __weak void STO_CR_ForceConvergency2(STO_Handle_t *pHandle) { #ifdef NULL_PTR_CHECK_STO_COR_SPD_POS_FDB if (NULL == pHandle) { /* Nothing to do */ } else { #endif STO_CR_Handle_t *pHdl = (STO_CR_Handle_t *)pHandle->_Super; //cstat !MISRAC2012-Rule-11.3 pHdl->ForceConvergency2 = true; #ifdef NULL_PTR_CHECK_STO_COR_SPD_POS_FDB } #endif } /** * @} */ /** * @} */ /** @} */ /************************ (C) COPYRIGHT 2023 STMicroelectronics *****END OF FILE****/
35,354
C
28.810287
115
0.639136
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/MCSDK_v6.2.0-Full/MotorControl/MCSDK/MCLib/Any/Src/speed_ctrl.c
/* ****************************************************************************** * @file speed_ctrl.c * @author Motor Control SDK Team, ST Microelectronics * @brief This file provides firmware functions that implement the following features * of the Speed Control component of the Motor Control SDK. * ****************************************************************************** * @attention * * <h2><center>&copy; Copyright (c) 2023 STMicroelectronics. * All rights reserved.</center></h2> * * This software component is licensed by ST under Ultimate Liberty license * SLA0044, the "License"; You may not use this file except in compliance with * the License. You may obtain a copy of the License at: * www.st.com/SLA0044 * ****************************************************************************** */ /* Includes ------------------------------------------------------------------*/ #include "speed_ctrl.h" #include "speed_pos_fdbk.h" #include "mc_type.h" #define CHECK_BOUNDARY /** @addtogroup MCSDK * @{ */ /** @defgroup SpeednTorqCtrl Speed Control * @brief Speed Control component of the Motor Control SDK * * @todo Document the Speed Control "module". * * @{ */ /** * @brief Initializes all the object variables, usually it has to be called * once right after object creation. * @param pHandle: handler of the current instance of the SpeednTorqCtrl component * @param pPI the PI object used as controller for the speed regulation. * @param SPD_Handle the speed sensor used to perform the speed regulation. * @retval none. */ __weak void STC_Init( SpeednTorqCtrl_Handle_t * pHandle, PID_Handle_t * pPI, SpeednPosFdbk_Handle_t * SPD_Handle ) { if (MC_NULL == pHandle) { /* Nothing to do */ } else { pHandle->PISpeed = pPI; pHandle->SPD = SPD_Handle; pHandle->Mode = pHandle->ModeDefault; pHandle->SpeedRefUnitExt = ((int32_t)pHandle->MecSpeedRefUnitDefault) * 65536; pHandle->DutyCycleRef = ((uint32_t)pHandle->DutyCycleRefDefault) * 65536; pHandle->TargetFinal = 0; pHandle->RampRemainingStep = 0U; pHandle->IncDecAmount = 0; } } /** * @brief It sets in real time the speed sensor utilized by the STC. * @param pHandle: handler of the current instance of the SpeednTorqCtrl component * @param SPD_Handle Speed sensor component to be set. * @retval none */ __weak void STC_SetSpeedSensor( SpeednTorqCtrl_Handle_t * pHandle, SpeednPosFdbk_Handle_t * SPD_Handle ) { if (MC_NULL == pHandle) { /* Nothing to do */ } else { pHandle->SPD = SPD_Handle; } } /** * @brief It returns the speed sensor utilized by the FOC. * @param pHandle: handler of the current instance of the SpeednTorqCtrl component * @retval SpeednPosFdbk_Handle_t speed sensor utilized by the FOC. */ __weak SpeednPosFdbk_Handle_t * STC_GetSpeedSensor( SpeednTorqCtrl_Handle_t * pHandle ) { return ((MC_NULL == pHandle) ? MC_NULL : pHandle->SPD); } /** * @brief It should be called before each motor restart. If STC is set in speed mode, this method resets the integral term of speed regulator. * @param pHandle: handler of the current instance of the SpeednTorqCtrl component * @retval none. */ __weak void STC_Clear( SpeednTorqCtrl_Handle_t * pHandle ) { if (MC_NULL == pHandle) { /* Nothing to do */ } else { if (MCM_SPEED_MODE == pHandle->Mode) { PID_SetIntegralTerm(pHandle->PISpeed, 0); } pHandle->DutyCycleRef = ((uint32_t)pHandle->DutyCycleRefDefault) * 65536; } } /** * @brief Get the current mechanical rotor speed reference. * Expressed in the unit defined by SPEED_UNIT. * @param pHandle: handler of the current instance of the SpeednTorqCtrl component * @retval int16_t current mechanical rotor speed reference. * Expressed in the unit defined by SPEED_UNIT. */ __weak int16_t STC_GetMecSpeedRefUnit( SpeednTorqCtrl_Handle_t * pHandle ) { #ifdef NO_FULL_MISRA_C_COMPLIANCY_SPD_TORQ_CTRL //cstat !MISRAC2012-Rule-1.3_n !ATH-shift-neg !MISRAC2012-Rule-10.1_R6 return ((MC_NULL == pHandle) ? 0 : (int16_t)(pHandle->SpeedRefUnitExt >> 16)); #else return ((MC_NULL == pHandle) ? 0 : (int16_t)(pHandle->SpeedRefUnitExt / 65536)); #endif } /** * @brief Get the current motor dutycycle reference. This value represents * actually the dutycycle reference expressed in digit. * @param pHandle: handler of the current instance of the SpeednTorqCtrl component * @retval uint16_t current dutycycle reference. This value is actually expressed in digit. */ __weak uint16_t STC_GetDutyCycleRef(SpeednTorqCtrl_Handle_t *pHandle) { #ifdef NO_FULL_MISRA_C_COMPLIANCY_SPD_TORQ_CTRL //cstat !MISRAC2012-Rule-1.3_n !ATH-shift-neg !MISRAC2012-Rule-10.1_R6 return ((MC_NULL == pHandle) ? 0 : (uint16_t)(pHandle->DutyCycleRef >> 16)); #else return ((MC_NULL == pHandle) ? 0 : (uint16_t)(pHandle->DutyCycleRef / 65536)); #endif } /** * @brief Set the modality of the speed controller. Two modality * are available Torque mode and Speed mode. * In Torque mode is possible to set directly the dutycycle * reference or execute a motor dutycycle ramp. This value represents * actually the reference expressed in digit. * In Speed mode is possible to set the mechanical rotor speed * reference or execute a speed ramp. The required motor dutycycle is * automatically calculated by the STC. * This command interrupts the execution of any previous ramp command * maintaining the last value of dutycycle. * @param pHandle: handler of the current instance of the SpeednTorqCtrl component * @param bMode modality of STC. It can be one of these two settings: * MCM_TORQUE_MODE to enable the Torque mode or MCM_SPEED_MODE to * enable the Speed mode. * @retval none */ __weak void STC_SetControlMode( SpeednTorqCtrl_Handle_t * pHandle, MC_ControlMode_t bMode ) { if (MC_NULL == pHandle) { /* Nothing to do */ } else { pHandle->Mode = bMode; pHandle->RampRemainingStep = 0u; /* Interrupts previous ramp. */ } } /** * @brief Get the modality of the speed controller. * @param pHandle: handler of the current instance of the SpeednTorqCtrl component * @retval MC_ControlMode_t It returns the modality of STC. It can be one of * these two values: MCM_TORQUE_MODE or MCM_SPEED_MODE. */ __weak MC_ControlMode_t STC_GetControlMode( SpeednTorqCtrl_Handle_t * pHandle ) { return ((MC_NULL == pHandle) ? MCM_TORQUE_MODE : pHandle->Mode); } /** * @brief Starts the execution of a ramp using new target and duration. This * command interrupts the execution of any previous ramp command. * The generated ramp will be in the modality previously set by * STC_SetControlMode method. * @param pHandle: handler of the current instance of the SpeednTorqCtrl component * @param hTargetFinal final value of command. This is different accordingly * the STC modality. * hTargetFinal is the value of mechanical rotor speed reference at the end * of the ramp.Expressed in the unit defined by SPEED_UNIT * @param hDurationms the duration of the ramp expressed in milliseconds. It * is possible to set 0 to perform an instantaneous change in the value. * @retval bool It return false if the absolute value of hTargetFinal is out of * the boundary of the application (Above max application speed or below min * application speed in this case the command is ignored and the * previous ramp is not interrupted, otherwise it returns true. */ __weak bool STC_ExecRamp(SpeednTorqCtrl_Handle_t *pHandle, int16_t hTargetFinal, uint32_t hDurationms) { bool allowedRange = true; if (MC_NULL == pHandle) { allowedRange = false; } else { uint32_t wAux; int32_t wAux1; int16_t hCurrentReference; /* Check if the hTargetFinal is out of the bound of application. */ if (MCM_TORQUE_MODE == pHandle->Mode) { hCurrentReference = STC_GetDutyCycleRef(pHandle); #ifdef CHECK_BOUNDARY if ((int32_t)hTargetFinal > (int32_t)pHandle->MaxPositiveDutyCycle) { allowedRange = false; } #endif } else { #ifdef NO_FULL_MISRA_C_COMPLIANCY_SPD_TORQ_CTRL //cstat !MISRAC2012-Rule-1.3_n !ATH-shift-neg !MISRAC2012-Rule-10.1_R6 hCurrentReference = (int16_t)(pHandle->SpeedRefUnitExt >> 16); #else hCurrentReference = (int16_t)(pHandle->SpeedRefUnitExt / 65536); #endif #ifdef CHECK_BOUNDARY if ((int32_t)hTargetFinal > (int32_t)pHandle->MaxAppPositiveMecSpeedUnit) { allowedRange = false; } else if (hTargetFinal < pHandle->MinAppNegativeMecSpeedUnit) { allowedRange = false; } else if ((int32_t)hTargetFinal < (int32_t)pHandle->MinAppPositiveMecSpeedUnit) { if (hTargetFinal > pHandle->MaxAppNegativeMecSpeedUnit) { allowedRange = false; } } else { /* Nothing to do */ } #endif } if (true == allowedRange) { /* Interrupts the execution of any previous ramp command */ if (0U == hDurationms) { if (MCM_SPEED_MODE == pHandle->Mode) { pHandle->SpeedRefUnitExt = ((int32_t)hTargetFinal) * 65536; } else { pHandle->DutyCycleRef = ((int32_t)hTargetFinal) * 65536; } pHandle->RampRemainingStep = 0U; pHandle->IncDecAmount = 0; } else { /* Store the hTargetFinal to be applied in the last step */ pHandle->TargetFinal = hTargetFinal; /* Compute the (wRampRemainingStep) number of steps remaining to complete the ramp. */ wAux = ((uint32_t)hDurationms) * ((uint32_t)pHandle->STCFrequencyHz); wAux /= 1000U; pHandle->RampRemainingStep = wAux; pHandle->RampRemainingStep++; /* Compute the increment/decrement amount (wIncDecAmount) to be applied to the reference value at each CalcSpeedReference. */ wAux1 = (((int32_t)hTargetFinal) - ((int32_t)hCurrentReference)) * 65536; wAux1 /= ((int32_t)pHandle->RampRemainingStep); pHandle->IncDecAmount = wAux1; } } } return (allowedRange); } /** * @brief This command interrupts the execution of any previous ramp command. * The last value of mechanical rotor speed reference is maintained. * @param pHandle: handler of the current instance of the SpeednTorqCtrl component * @retval none */ __weak void STC_StopRamp(SpeednTorqCtrl_Handle_t *pHandle) { if (MC_NULL == pHandle) { /* Nothing to do */ } else { pHandle->RampRemainingStep = 0U; pHandle->IncDecAmount = 0; } } /** * @brief It is used to compute the new value of motor speed reference. It * must be called at fixed time equal to hSTCFrequencyHz. It is called * passing as parameter the speed sensor used to perform the speed * regulation. * @param pHandle: handler of the current instance of the SpeednTorqCtrl component * @retval int16_t motor dutycycle reference. This value represents actually the * dutycycle expressed in digit. */ __weak uint16_t STC_CalcSpeedReference(SpeednTorqCtrl_Handle_t *pHandle) { uint16_t hDutyCycleReference; if (MC_NULL == pHandle) { hDutyCycleReference = 0; } else { int32_t wCurrentReference; int16_t hMeasuredSpeed; int16_t hTargetSpeed; int16_t hError; if (MCM_TORQUE_MODE == pHandle->Mode) { wCurrentReference = pHandle->DutyCycleRef; } else { wCurrentReference = pHandle->SpeedRefUnitExt; } /* Update the speed reference or the torque reference according to the mode and terminates the ramp if needed. */ if (pHandle->RampRemainingStep > 1U) { /* Increment/decrement the reference value. */ wCurrentReference += pHandle->IncDecAmount; /* Decrement the number of remaining steps */ pHandle->RampRemainingStep--; } else if (1U == pHandle->RampRemainingStep) { /* Set the backup value of hTargetFinal. */ wCurrentReference = ((int32_t)pHandle->TargetFinal) * 65536; pHandle->RampRemainingStep = 0U; } else { /* Do nothing. */ } if (MCM_SPEED_MODE == pHandle->Mode) { /* Run the speed control loop */ /* Compute speed error */ #ifdef NO_FULL_MISRA_C_COMPLIANCY_SPD_TORQ_CTRL //cstat !MISRAC2012-Rule-1.3_n !ATH-shift-neg !MISRAC2012-Rule-10.1_R6 hTargetSpeed = (int16_t)(wCurrentReference >> 16); #else hTargetSpeed = (int16_t)(wCurrentReference / 65536); #endif hMeasuredSpeed = SPD_GetAvrgMecSpeedUnit(pHandle->SPD); if (hTargetSpeed < 0) hError = hMeasuredSpeed - hTargetSpeed; else hError = hTargetSpeed - hMeasuredSpeed; hDutyCycleReference = PI_Controller(pHandle->PISpeed, (int32_t)hError); pHandle->SpeedRefUnitExt = wCurrentReference; pHandle->DutyCycleRef = ((int32_t)hDutyCycleReference) * 65536; } else { pHandle->DutyCycleRef = wCurrentReference; #ifdef NO_FULL_MISRA_C_COMPLIANCY_SPD_TORQ_CTRL //cstat !MISRAC2012-Rule-1.3_n !ATH-shift-neg !MISRAC2012-Rule-10.1_R6 hDutyCycleReference = (int16_t)(wCurrentReference >> 16); #else hDutyCycleReference = (int16_t)(wCurrentReference / 65536); #endif } } return (hDutyCycleReference); } /** * @brief Get the Default mechanical rotor speed reference. * Expressed in the unit defined by #SPEED_UNIT. * @param pHandle: handler of the current instance of the SpeednTorqCtrl component * @retval int16_t It returns the Default mechanical rotor speed. * Expressed in the unit defined by #SPEED_UNIT */ __weak int16_t STC_GetMecSpeedRefUnitDefault(SpeednTorqCtrl_Handle_t *pHandle) { return ((MC_NULL == pHandle) ? 0 : pHandle->MecSpeedRefUnitDefault); } /** * @brief Returns the Application maximum positive value of rotor speed. Expressed in the unit defined by #SPEED_UNIT. * @param pHandle: handler of the current instance of the SpeednTorqCtrl component */ __weak uint16_t STC_GetMaxAppPositiveMecSpeedUnit(SpeednTorqCtrl_Handle_t *pHandle) { return ((MC_NULL == pHandle) ? 0U : pHandle->MaxAppPositiveMecSpeedUnit); } /** * @brief Returns the Application minimum negative value of rotor speed. Expressed in the unit defined by #SPEED_UNIT. * @param pHandle: handler of the current instance of the SpeednTorqCtrl component */ __weak int16_t STC_GetMinAppNegativeMecSpeedUnit(SpeednTorqCtrl_Handle_t *pHandle) { return ((MC_NULL == pHandle) ? 0 : pHandle->MinAppNegativeMecSpeedUnit); } /** * @brief Check if the settled speed ramp has been completed. * @param pHandle: handler of the current instance of the SpeednTorqCtrl component * @retval bool It returns true if the ramp is completed, false otherwise. */ __weak bool STC_RampCompleted(SpeednTorqCtrl_Handle_t *pHandle) { bool retVal = false; if (MC_NULL == pHandle) { /* Nothing to do */ } else { if (0U == pHandle->RampRemainingStep) { retVal = true; } } return (retVal); } /** * @brief Stop the execution of speed ramp. * @param pHandle: handler of the current instance of the SpeednTorqCtrl component * @retval bool It returns true if the command is executed, false otherwise. */ __weak bool STC_StopSpeedRamp(SpeednTorqCtrl_Handle_t *pHandle) { bool retVal = false; if (MC_NULL == pHandle) { /* Nothing to do */ } else { if (MCM_SPEED_MODE == pHandle->Mode) { pHandle->RampRemainingStep = 0u; retVal = true; } } return (retVal); } /** * @brief Force the speed reference to the curren speed. It is used * at the START_RUN state to initialize the speed reference. * @param pHandle: handler of the current instance of the SpeednTorqCtrl component * @retval none */ __weak void STC_ForceSpeedReferenceToCurrentSpeed(SpeednTorqCtrl_Handle_t *pHandle) { if (MC_NULL == pHandle) { /* Nothing to do */ } else { pHandle->SpeedRefUnitExt = ((int32_t)SPD_GetAvrgMecSpeedUnit(pHandle->SPD)) * (int32_t)65536; } } /** * @} */ /** * @} */ /************************ (C) COPYRIGHT 2023 STMicroelectronics *****END OF FILE****/
16,565
C
31.042553
120
0.652883
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/MCSDK_v6.2.0-Full/MotorControl/MCSDK/MCLib/Any/Src/revup_ctrl.c
/** ****************************************************************************** * @file revup_ctrl.c * @author Motor Control SDK Team, ST Microelectronics * @brief This file provides firmware functions that implement the features * of the Rev-Up Control component of the Motor Control SDK: * * * Main Rev-Up procedure to execute programmed phases * * On the Fly (OTF) * ****************************************************************************** * @attention * * <h2><center>&copy; Copyright (c) 2023 STMicroelectronics International N.V. * All rights reserved.</center></h2> * * Redistribution and use in source and binary forms, with or without * modification, are permitted, provided that the following conditions are met: * * 1. Redistribution of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * 3. Neither the name of STMicroelectronics nor the names of other * contributors to this software may be used to endorse or promote products * derived from this software without specific written permission. * 4. This software, including modifications and/or derivative works of this * software, must execute solely and exclusively on microcontroller or * microprocessor devices manufactured by or for STMicroelectronics. * 5. Redistribution and use of this software other than as permitted under * this license is void and will automatically terminate your rights under * this license. * * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * ****************************************************************************** * @ingroup RevUpCtrlFOC */ /* Includes ------------------------------------------------------------------*/ #include "revup_ctrl.h" /** @addtogroup MCSDK * @{ */ /** @defgroup RevUpCtrl Rev-Up Control * @brief Rev-Up Control component of the Motor Control SDK * * Used to start up the motor with a set of pre-programmed phases. * * The number of phases of the Rev-Up procedure range is from 0 to 5. * The Rev-Up controller must be called at speed loop frequency. * * @{ */ /** @defgroup RevUpCtrlFOC FOC Rev-Up Control component * @brief Rev-Up control component used to start motor driven with * the Field Oriented Control technique * * @{ */ /* Private defines -----------------------------------------------------------*/ /** * @brief Timeout used to reset integral term of PLL. * It is expressed in ms. * */ #define RUC_OTF_PLL_RESET_TIMEOUT 100u /* Private functions ----------------------------------------------------------*/ /* Returns the mechanical speed of a selected phase */ //cstat !MISRAC2012-Rule-8.13 static int16_t RUC_GetPhaseFinalMecSpeed01Hz(RevUpCtrl_Handle_t *pHandle, uint8_t bPhase) { return (pHandle->ParamsData[bPhase].hFinalMecSpeedUnit); } /** * @brief Initialize and configure the FOC RevUpCtrl Component * @param pHandle: Pointer on Handle structure of FOC RevUp controller. * @param pSTC: Pointer on speed and torque controller structure. * @param pVSS: Pointer on virtual speed sensor structure. * @param pSNSL: Pointer on sensorless state observer structure. * @param pPWM: Pointer on the PWM structure. */ __weak void RUC_Init(RevUpCtrl_Handle_t *pHandle, SpeednTorqCtrl_Handle_t *pSTC, VirtualSpeedSensor_Handle_t *pVSS, STO_Handle_t *pSNSL, PWMC_Handle_t *pPWM) { #ifdef NULL_PTR_CHECK_REV_UP_CTL if (MC_NULL == pHandle) { /* Nothing to do */ } else { #endif RevUpCtrl_PhaseParams_t *pRUCPhaseParams = &pHandle->ParamsData[0]; uint8_t bPhase = 0U; pHandle->pSTC = pSTC; pHandle->pVSS = pVSS; pHandle->pSNSL = pSNSL; pHandle->pPWM = pPWM; pHandle->OTFSCLowside = false; pHandle->EnteredZone1 = false; while ((pRUCPhaseParams != MC_NULL) && (bPhase < RUC_MAX_PHASE_NUMBER)) { /* Dump HF data for now HF data are forced to 16 bits */ pRUCPhaseParams = (RevUpCtrl_PhaseParams_t *)pRUCPhaseParams->pNext; //cstat !MISRAC2012-Rule-11.5 bPhase++; } if (0U == bPhase) { /* nothing to do error */ } else { pHandle->ParamsData[bPhase - 1u].pNext = MC_NULL; pHandle->bPhaseNbr = bPhase; pHandle->bResetPLLTh = (uint8_t)((RUC_OTF_PLL_RESET_TIMEOUT * pHandle->hRUCFrequencyHz) / 1000U); } #ifdef NULL_PTR_CHECK_REV_UP_CTL } #endif } /** * @brief Initialize internal FOC RevUp controller state. * @param pHandle: Pointer on Handle structure of RevUp controller. * @param hMotorDirection: Rotor rotation direction. * This parameter must be -1 or +1. */ __weak void RUC_Clear(RevUpCtrl_Handle_t *pHandle, int16_t hMotorDirection) { #ifdef NULL_PTR_CHECK_REV_UP_CTL if (MC_NULL == pHandle) { /* Nothing to do */ } else { #endif VirtualSpeedSensor_Handle_t *pVSS = pHandle->pVSS; SpeednTorqCtrl_Handle_t *pSTC = pHandle->pSTC; RevUpCtrl_PhaseParams_t *pPhaseParams = pHandle->ParamsData; pHandle->hDirection = hMotorDirection; pHandle->EnteredZone1 = false; /* Initializes the rev up stages counter */ pHandle->bStageCnt = 0U; pHandle->bOTFRelCounter = 0U; pHandle->OTFSCLowside = false; /* Calls the clear method of VSS */ VSS_Clear(pVSS); /* Sets the STC in torque mode */ STC_SetControlMode(pSTC, MCM_TORQUE_MODE); /* Sets the mechanical starting angle of VSS */ VSS_SetMecAngle(pVSS, pHandle->hStartingMecAngle * hMotorDirection); /* Sets to zero the starting torque of STC */ (void)STC_ExecRamp(pSTC, 0, 0U); /* Gives the first command to STC and VSS */ (void)STC_ExecRamp(pSTC, pPhaseParams->hFinalTorque * hMotorDirection, (uint32_t)(pPhaseParams->hDurationms)); VSS_SetMecAcceleration(pVSS, pPhaseParams->hFinalMecSpeedUnit * hMotorDirection, pPhaseParams->hDurationms); /* Compute hPhaseRemainingTicks */ pHandle->hPhaseRemainingTicks = (uint16_t)((((uint32_t)pPhaseParams->hDurationms) * ((uint32_t)pHandle->hRUCFrequencyHz)) / 1000U ); pHandle->hPhaseRemainingTicks++; /* Set the next phases parameter pointer */ pHandle->pCurrentPhaseParams = (RevUpCtrl_PhaseParams_t *)pPhaseParams->pNext; //cstat !MISRAC2012-Rule-11.5 /* Timeout counter for PLL reset during OTF */ pHandle->bResetPLLCnt = 0U; #ifdef NULL_PTR_CHECK_REV_UP_CTL } #endif } /** * @brief FOC Main Rev-Up controller procedure executing overall programmed phases and * on-the-fly startup handling. * @param pHandle: Pointer on Handle structure of RevUp controller. * @retval Boolean set to false when entire Rev-Up phases have been completed. */ __weak bool RUC_OTF_Exec(RevUpCtrl_Handle_t *pHandle) { bool retVal = true; #ifdef NULL_PTR_CHECK_REV_UP_CTL if (MC_NULL == pHandle) { /* Nothing to do */ } else { #endif bool IsSpeedReliable; bool condition = false; if (pHandle->hPhaseRemainingTicks > 0u) { /* Decrease the hPhaseRemainingTicks */ pHandle->hPhaseRemainingTicks--; /* OTF start-up */ if (0U == pHandle->bStageCnt) { if (false == pHandle->EnteredZone1) { if (pHandle->pSNSL->pFctStoOtfResetPLL != MC_NULL) { pHandle->bResetPLLCnt++; if (pHandle->bResetPLLCnt > pHandle->bResetPLLTh) { pHandle->pSNSL->pFctStoOtfResetPLL(pHandle->pSNSL); pHandle->bOTFRelCounter = 0U; pHandle->bResetPLLCnt = 0U; } else { /* Nothing to do */ } } else { /* Nothing to do */ } IsSpeedReliable = pHandle->pSNSL->pFctSTO_SpeedReliabilityCheck(pHandle->pSNSL); if (IsSpeedReliable) { if (pHandle->bOTFRelCounter < 127U) { pHandle->bOTFRelCounter++; } else { /* Nothing to do */ } } else { pHandle->bOTFRelCounter = 0U; } if (pHandle->pSNSL->pFctStoOtfResetPLL != MC_NULL) { if (pHandle->bOTFRelCounter == (pHandle->bResetPLLTh >> 1)) { condition = true; } else { /* Nothing to do */ } } else { if (127U == pHandle->bOTFRelCounter) { condition = true; } else { /* Nothing to do */ } } if (true == condition) { bool bCollinearSpeed = false; int16_t hObsSpeedUnit = SPD_GetAvrgMecSpeedUnit(pHandle->pSNSL->_Super); int16_t hObsSpeedUnitAbsValue = ((hObsSpeedUnit < 0) ? (-hObsSpeedUnit) : (hObsSpeedUnit)); /* hObsSpeedUnit absolute value */ if (pHandle->hDirection > 0) { if (hObsSpeedUnit > 0) { bCollinearSpeed = true; /* Actual and reference speed are collinear */ } else { /* Nothing to do */ } } else { if (hObsSpeedUnit < 0) { bCollinearSpeed = true; /* Actual and reference speed are collinear */ } else { /* Nothing to do */ } } if (false == bCollinearSpeed) { /*reverse speed management*/ pHandle->bOTFRelCounter = 0U; } else /* Speeds are collinear */ { if ((uint16_t)(hObsSpeedUnitAbsValue) > pHandle->hMinStartUpValidSpeed) { /* Startup end, go to run */ pHandle->pSNSL->pFctForceConvergency1(pHandle->pSNSL); pHandle->EnteredZone1 = true; } else if ((uint16_t)(hObsSpeedUnitAbsValue) > pHandle->hMinStartUpFlySpeed) { /* Synch with startup */ /* Nearest phase search */ int16_t hOldFinalMecSpeedUnit = 0; int16_t hOldFinalTorque = 0; int32_t wDeltaSpeedRevUp; int32_t wDeltaTorqueRevUp; bool bError = false; VSS_SetCopyObserver(pHandle->pVSS); pHandle->pSNSL->pFctForceConvergency2(pHandle->pSNSL); if (MC_NULL == pHandle->pCurrentPhaseParams) { bError = true; pHandle->hPhaseRemainingTicks = 0U; } else { while (pHandle->pCurrentPhaseParams->hFinalMecSpeedUnit < hObsSpeedUnitAbsValue) { if (pHandle->pCurrentPhaseParams->pNext == MC_NULL) { /* Sets for Revup fail error */ bError = true; pHandle->hPhaseRemainingTicks = 0U; break; } else { /* Skips this phase */ hOldFinalMecSpeedUnit = pHandle->pCurrentPhaseParams->hFinalMecSpeedUnit; hOldFinalTorque = pHandle->pCurrentPhaseParams->hFinalTorque; //cstat !MISRAC2012-Rule-11.5 pHandle->pCurrentPhaseParams = (RevUpCtrl_PhaseParams_t *)pHandle->pCurrentPhaseParams->pNext; pHandle->bStageCnt++; } } } if (false == bError) { /* Calculation of the transition phase from OTF to standard revup */ int16_t hTorqueReference; wDeltaSpeedRevUp = ((int32_t)(pHandle->pCurrentPhaseParams->hFinalMecSpeedUnit)) - ((int32_t)(hOldFinalMecSpeedUnit)); wDeltaTorqueRevUp = ((int32_t)(pHandle->pCurrentPhaseParams->hFinalTorque)) - ((int32_t)(hOldFinalTorque)); if ((int32_t)0 == wDeltaSpeedRevUp) { /* Nothing to do */ } else { hTorqueReference = (int16_t)((((int32_t)hObsSpeedUnit) * wDeltaTorqueRevUp) / wDeltaSpeedRevUp) + hOldFinalTorque; (void)STC_ExecRamp(pHandle->pSTC, pHandle->hDirection * hTorqueReference, 0U); } pHandle->hPhaseRemainingTicks = 1U; pHandle->pCurrentPhaseParams = &pHandle->OTFPhaseParams; pHandle->bStageCnt = 6U; } /* No MC_NULL error */ } /* Speed > MinStartupFly */ else { /* Nothing to do */ } } /* Speeds are collinear */ } /* Speed is reliable */ }/* EnteredZone1 1 is false */ else { pHandle->pSNSL->pFctForceConvergency1(pHandle->pSNSL); } } /* Stage 0 */ } /* hPhaseRemainingTicks > 0 */ if (0U == pHandle->hPhaseRemainingTicks) { if (pHandle->pCurrentPhaseParams != MC_NULL) { if (0U == pHandle->bStageCnt) { /* End of OTF */ PWMC_SwitchOffPWM(pHandle->pPWM); pHandle->OTFSCLowside = true; PWMC_TurnOnLowSides(pHandle->pPWM, 0u); pHandle->bOTFRelCounter = 0U; } else if (1U == pHandle->bStageCnt) { PWMC_SwitchOnPWM(pHandle->pPWM); pHandle->OTFSCLowside = false; } else { /* Nothing to do */ } /* If it becomes zero the current phase has been completed */ /* Gives the next command to STC and VSS */ (void)STC_ExecRamp(pHandle->pSTC, pHandle->pCurrentPhaseParams->hFinalTorque * pHandle->hDirection, (uint32_t)(pHandle->pCurrentPhaseParams->hDurationms)); VSS_SetMecAcceleration(pHandle->pVSS, pHandle->pCurrentPhaseParams->hFinalMecSpeedUnit * pHandle->hDirection, pHandle->pCurrentPhaseParams->hDurationms); /* Compute hPhaseRemainingTicks */ pHandle->hPhaseRemainingTicks = (uint16_t)((((uint32_t)pHandle->pCurrentPhaseParams->hDurationms) * (uint32_t)pHandle->hRUCFrequencyHz) / 1000U); pHandle->hPhaseRemainingTicks++; /* Set the next phases parameter pointer */ pHandle->pCurrentPhaseParams = pHandle->pCurrentPhaseParams->pNext; //cstat !MISRAC2012-Rule-11.5 /* Increases the rev up stages counter */ pHandle->bStageCnt++; } else { if (pHandle->bStageCnt == (pHandle->bPhaseNbr - (uint8_t)1)) /* End of user programmed revup */ { retVal = false; } else if (7U == pHandle->bStageCnt) /* End of first OTF runs */ { pHandle->bStageCnt = 0U; /* Breaking state */ pHandle->hPhaseRemainingTicks = 0U; } else { /* Nothing to do */ } } } #ifdef NULL_PTR_CHECK_REV_UP_CTL } #endif return (retVal); } /** * @brief FOC Main Rev-Up controller procedure executing overall programmed phases. * @param pHandle: Pointer on Handle structure of RevUp controller. * @retval Boolean set to false when entire Rev-Up phases have been completed. */ __weak bool RUC_Exec(RevUpCtrl_Handle_t *pHandle) { bool retVal = true; #ifdef NULL_PTR_CHECK_REV_UP_CTL if (MC_NULL == pHandle) { retVal = false; } else { #endif if (pHandle->hPhaseRemainingTicks > 0U) { /* Decrease the hPhaseRemainingTicks */ pHandle->hPhaseRemainingTicks--; } /* hPhaseRemainingTicks > 0 */ else { /* Nothing to do */ } if (0U == pHandle->hPhaseRemainingTicks) { if (pHandle->pCurrentPhaseParams != MC_NULL) { /* If it becomes zero the current phase has been completed */ /* Gives the next command to STC and VSS */ (void)STC_ExecRamp(pHandle->pSTC, pHandle->pCurrentPhaseParams->hFinalTorque * pHandle->hDirection, (uint32_t)(pHandle->pCurrentPhaseParams->hDurationms)); VSS_SetMecAcceleration(pHandle->pVSS, pHandle->pCurrentPhaseParams->hFinalMecSpeedUnit * pHandle->hDirection, pHandle->pCurrentPhaseParams->hDurationms); /* Compute hPhaseRemainingTicks */ pHandle->hPhaseRemainingTicks = (uint16_t)((((uint32_t)pHandle->pCurrentPhaseParams->hDurationms) * ((uint32_t)pHandle->hRUCFrequencyHz)) / 1000U ); pHandle->hPhaseRemainingTicks++; /* Set the next phases parameter pointer */ pHandle->pCurrentPhaseParams = pHandle->pCurrentPhaseParams->pNext; //cstat !MISRAC2012-Rule-11.5 /* Increases the rev up stages counter */ pHandle->bStageCnt++; } else { retVal = false; } } else { /* Nothing to do */ } #ifdef NULL_PTR_CHECK_REV_UP_CTL } #endif return (retVal); } /** * @brief It checks if this stage is used for align motor. * @param pHandle: Pointer on Handle structure of RevUp controller. * @retval Returns 1 if the alignment is correct otherwise it returns 0 */ //cstat !MISRAC2012-Rule-8.13 uint8_t RUC_IsAlignStageNow(RevUpCtrl_Handle_t *pHandle) { uint8_t align_flag = 0; #ifdef NULL_PTR_CHECK_REV_UP_CTL if (MC_NULL == pHandle) { /* Nothing to do */ } else { #endif int16_t speed; speed = RUC_GetPhaseFinalMecSpeed01Hz(pHandle, pHandle->bStageCnt); if (0 == speed) { align_flag = 1; } else { /* Nothing to do */ } #ifdef NULL_PTR_CHECK_REV_UP_CTL } #endif return (align_flag); } /** * @brief Provide current state of Rev-Up controller procedure. * @param pHandle: Pointer on Handle structure of RevUp controller. * @retval Boolean set to true when entire Rev-Up phases have been completed. */ //cstat !MISRAC2012-Rule-8.13 __weak bool RUC_Completed(RevUpCtrl_Handle_t *pHandle) { bool retVal = false; #ifdef NULL_PTR_CHECK_REV_UP_CTL if (MC_NULL == pHandle) { /* Nothing to do */ } else { #endif if (MC_NULL == pHandle->pCurrentPhaseParams) { retVal = true; } else { /* Nothing to do */ } #ifdef NULL_PTR_CHECK_REV_UP_CTL } #endif return (retVal); } /** * @brief Allow to exit from Rev-Up process at the current rotor speed. * @param pHandle: Pointer on Handle structure of RevUp controller. */ __weak void RUC_Stop(RevUpCtrl_Handle_t *pHandle) { #ifdef NULL_PTR_CHECK_REV_UP_CTL if (MC_NULL == pHandle) { /* Nothing to do */ } else { #endif VirtualSpeedSensor_Handle_t *pVSS = pHandle->pVSS; pHandle->pCurrentPhaseParams = MC_NULL; pHandle->hPhaseRemainingTicks = 0U; VSS_SetMecAcceleration(pVSS, SPD_GetAvrgMecSpeedUnit(&pVSS->_Super), 0U); #ifdef NULL_PTR_CHECK_REV_UP_CTL } #endif } #if defined (CCMRAM) #if defined (__ICCARM__) #pragma location = ".ccmram" #elif defined (__CC_ARM) || defined(__GNUC__) __attribute__((section(".ccmram"))) #endif #endif /** * @brief Check that alignment and first acceleration stage are completed. * @param pHandle: Pointer on Handle structure of RevUp controller. * @retval Boolean set to true when first acceleration stage has been reached. */ //cstat !MISRAC2012-Rule-8.13 __weak bool RUC_FirstAccelerationStageReached(RevUpCtrl_Handle_t *pHandle) { bool retVal = false; #ifdef NULL_PTR_CHECK_REV_UP_CTL if (MC_NULL == pHandle) { /* Nothing to do */ } else { #endif if (pHandle->bStageCnt >= pHandle->bFirstAccelerationStage) { retVal = true; } else { /* Nothing to do */ } #ifdef NULL_PTR_CHECK_REV_UP_CTL } #endif return (retVal); } /** * @brief Allow to modify duration of a selected phase. * @param pHandle: Pointer on Handle structure of RevUp controller. * @param bPhase: RevUp phase where new duration shall be modified. * This parameter must be a number between 0 and 6. * @param hDurationms: new duration value required for associated phase. * This parameter must be set in millisecond. */ __weak void RUC_SetPhaseDurationms(RevUpCtrl_Handle_t *pHandle, uint8_t bPhase, uint16_t hDurationms) { #ifdef NULL_PTR_CHECK_REV_UP_CTL if (MC_NULL == pHandle) { /* Nothing to do */ } else { #endif pHandle->ParamsData[bPhase].hDurationms = hDurationms; #ifdef NULL_PTR_CHECK_REV_UP_CTL } #endif } /** * @brief Allow to modify targeted mechanical speed of a selected phase. * @param pHandle: Pointer on Handle structure of RevUp controller. * @param bPhase: RevUp phase where new mechanical speed shall be modified. * This parameter must be a number between 0 and 6. * @param hFinalMecSpeedUnit: new targeted mechanical speed. * This parameter must be expressed in 0.1Hz. */ __weak void RUC_SetPhaseFinalMecSpeedUnit(RevUpCtrl_Handle_t *pHandle, uint8_t bPhase, int16_t hFinalMecSpeedUnit) { #ifdef NULL_PTR_CHECK_REV_UP_CTL if (MC_NULL == pHandle) { /* Nothing to do */ } else { #endif pHandle->ParamsData[bPhase].hFinalMecSpeedUnit = hFinalMecSpeedUnit; #ifdef NULL_PTR_CHECK_REV_UP_CTL } #endif } /** * @brief Allow to modify targeted the motor torque of a selected phase. * @param pHandle: Pointer on Handle structure of RevUp controller. * @param bPhase: RevUp phase where new the motor torque shall be modified. * This parameter must be a number between 0 and 6. * @param hFinalTorque: new targeted motor torque. */ __weak void RUC_SetPhaseFinalTorque(RevUpCtrl_Handle_t *pHandle, uint8_t bPhase, int16_t hFinalTorque) { #ifdef NULL_PTR_CHECK_REV_UP_CTL if (MC_NULL == pHandle) { /* Nothing to do */ } else { #endif pHandle->ParamsData[bPhase].hFinalTorque = hFinalTorque; #ifdef NULL_PTR_CHECK_REV_UP_CTL } #endif } /** * @brief Allow to configure a Rev-Up phase. * @param pHandle: Pointer on Handle structure of Rev-Up controller. * @param phaseNumber : number of phases relative to the programmed Rev-Up * @param phaseData: * - RevUp phase where new motor torque shall be modified. * This parameter must be a number between 0 and 6. * - RevUp phase where new mechanical speed shall be modified. * This parameter must be a number between 0 and 6. * - New duration value required for associated phase in ms unit. * @retval Boolean set to true */ __weak bool RUC_SetPhase(RevUpCtrl_Handle_t *pHandle, uint8_t phaseNumber, RevUpCtrl_PhaseParams_t *phaseData) { bool retValue = true; #ifdef NULL_PTR_CHECK_REV_UP_CTL if ((MC_NULL == pHandle) || (MC_NULL == phaseData)) { retValue = false; } else { #endif pHandle->ParamsData[phaseNumber].hFinalTorque = phaseData->hFinalTorque; pHandle->ParamsData[phaseNumber].hFinalMecSpeedUnit = phaseData->hFinalMecSpeedUnit; pHandle->ParamsData[phaseNumber].hDurationms = phaseData->hDurationms; #ifdef NULL_PTR_CHECK_REV_UP_CTL } #endif return (retValue); } /** * @brief Allow to read duration set in selected phase. * @param pHandle: Pointer on Handle structure of RevUp controller. * @param bPhase: RevUp phase from where duration is read. * This parameter must be a number between 0 and 6. * @retval Returns duration used in selected phase in ms unit. */ //cstat !MISRAC2012-Rule-8.13 __weak uint16_t RUC_GetPhaseDurationms(RevUpCtrl_Handle_t *pHandle, uint8_t bPhase) { #ifdef NULL_PTR_CHECK_REV_UP_CTL return ((MC_NULL == pHandle) ? 0U : (uint16_t)pHandle->ParamsData[bPhase].hDurationms); #else return ((uint16_t)pHandle->ParamsData[bPhase].hDurationms); #endif } /** * @brief Allow to read targeted rotor speed set in selected phase. * @param pHandle: Pointer on Handle structure of RevUp controller. * @param bPhase: RevUp phase from where targeted rotor speed is read. * This parameter must be a number between 0 and 6. * @retval Returns targeted rotor speed set for a selected phase. */ //cstat !MISRAC2012-Rule-8.13 __weak int16_t RUC_GetPhaseFinalMecSpeedUnit(RevUpCtrl_Handle_t *pHandle, uint8_t bPhase) { #ifdef NULL_PTR_CHECK_REV_UP_CTL return ((MC_NULL == pHandle) ? 0 : (int16_t)pHandle->ParamsData[bPhase].hFinalMecSpeedUnit); #else return ((int16_t)pHandle->ParamsData[bPhase].hFinalMecSpeedUnit); #endif } /** * @brief Allow to read targeted motor torque set in selected phase. * @param pHandle: Pointer on Handle structure of RevUp controller. * @param bPhase: RevUp phase from where targeted motor torque is read. * This parameter must be a number between 0 and 6. * @retval Returns targeted motor torque set for a selected phase. */ //cstat !MISRAC2012-Rule-8.13 __weak int16_t RUC_GetPhaseFinalTorque(RevUpCtrl_Handle_t *pHandle, uint8_t bPhase) { #ifdef NULL_PTR_CHECK_REV_UP_CTL return ((MC_NULL == pHandle) ? 0 : (int16_t)pHandle->ParamsData[bPhase].hFinalTorque); #else return ((int16_t)pHandle->ParamsData[bPhase].hFinalTorque); #endif } /** * @brief Allow to read total number of programmed phases. * @param pHandle: Pointer on Handle structure of RevUp controller. * @retval Returns number of phases relative to the programmed Rev-Up. * */ //cstat !MISRAC2012-Rule-8.13 __weak uint8_t RUC_GetNumberOfPhases(RevUpCtrl_Handle_t *pHandle) { #ifdef NULL_PTR_CHECK_REV_UP_CTL return ((MC_NULL == pHandle) ? 0U : (uint8_t)pHandle->bPhaseNbr); #else return ((uint8_t)pHandle->bPhaseNbr); #endif } /** * @brief Allow to read a programmed phase. * @param pHandle: Pointer on Handle structure of RevUp controller. * @param phaseNumber : number of phases relative to the programmed Rev-Up * @param phaseData: * - RevUp phase from where targeted rotor speed is read. * This parameter must be a number between 0 and 6. * - RevUp phase from where targeted motor torque is read. * This parameter must be a number between 0 and 6. * - Duration set in selected phase in ms unit. * @retval Returns Boolean set to true value. */ //cstat !MISRAC2012-Rule-8.13 __weak bool RUC_GetPhase(RevUpCtrl_Handle_t *pHandle, uint8_t phaseNumber, RevUpCtrl_PhaseParams_t *phaseData) { bool retValue = true; #ifdef NULL_PTR_CHECK_REV_UP_CTL if ((MC_NULL == pHandle) || (MC_NULL == phaseData)) { retValue = false; } else { #endif phaseData->hFinalTorque = (int16_t)pHandle->ParamsData[phaseNumber].hFinalTorque; phaseData->hFinalMecSpeedUnit = (int16_t)pHandle->ParamsData[phaseNumber].hFinalMecSpeedUnit; phaseData->hDurationms = (uint16_t)pHandle->ParamsData[phaseNumber].hDurationms; #ifdef NULL_PTR_CHECK_REV_UP_CTL } #endif return (retValue); } /** * @brief Allow to read status of On The Fly (OTF) feature. * @param pHandle: Pointer on Handle structure of RevUp controller. * @retval Boolean set to true at the end of OTF processing. */ //cstat !MISRAC2012-Rule-8.13 __weak bool RUC_Get_SCLowsideOTF_Status(RevUpCtrl_Handle_t *pHandle) { #ifdef NULL_PTR_CHECK_REV_UP_CTL return ((MC_NULL == pHandle) ? false : pHandle->OTFSCLowside); #else return (pHandle->OTFSCLowside); #endif } /** * @} */ /** * @} */ /** * @} */ /************************ (C) COPYRIGHT 2023 STMicroelectronics *****END OF FILE****/
29,303
C
30.68
116
0.601884
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/MCSDK_v6.2.0-Full/MotorControl/MCSDK/MCLib/Any/Src/cos_sin.c
/** ****************************************************************************** * @file cos_sin.c * @author Piak Electronic Design B.V. * @brief This file is part of the Motor Control SDK. ****************************************************************************** * @attention * * <h2><center>&copy; Copyright (c) 2023 Piak Electronic Design B.V. * All rights reserved.</center></h2> * * This software component is licensed by ST under Ultimate Liberty license * SLA0044, the "License"; You may not use this file except in compliance with * the License. You may obtain a copy of the License at: * www.st.com/SLA0044 * ****************************************************************************** */ // #include "math_defs.h" // Needed for printf(), etc. // #include <stdio.h> // // Assumes a normalized angle: 0 <= th/(2*pi) <= 1. // Fast poly z = cos(th) + j*sin(th) int32 precision, // for any value of th using 10th order polynomial // approximations, absolute error = |e| < 10^-5. See: // Abramowitz and Stegun, "Handbook of Mathematical // Functions", p. 76. Polynomial coeffs. in cordicx.h // // cos_sin_poly_L(&zi); // void cos_sin_poly_L(DQL zi) { // // Cos, Sin coeffs. static long long ccsi[_CSSN], csni[_CSSN]; static long long pi2, pi, hpi, one; static int ob, n = 0; // Misc. variables. int b, bb, hth, i, quad; long long th, th2, cs, sn; // Number of bits. b = zi->b; // Convert coefficients? if (b != ob) { // Difference bits. bb = _MNB - b; ob = b; // Move & scale cos,sin coeffs. for (i = 0; i < _CSSN; i++) { // Scale coeffs. ccsi[i] = _ccsl[i] >> bb; csni[i] = _csnl[i] >> bb; // Both coefs. zero? if (ccsi[i] == 0LL && csni[i] == 0LL) break; } // Last non-zero coefficients. n = i; // Constants. pi2 = _PII2 >> bb; pi = _PII >> bb; hpi = _HPII >> bb; one = _ONEI >> bb; } // th = thr/(2*pi). th = (zi->thr*pi) >> (zi->b - 1); // |th| > pi? if (th > pi) th -= pi2; else if (th < -pi) th += pi2; // Get quadrant. if (th > _ZERO) { if (th > hpi) { th -= hpi; quad = 1; } else quad = 0; } else { if (th < -hpi) { th += hpi; quad = 2; } else quad = 3; } // 1/2 Angle avoids overflow. hth = (int) (llabs(th) > one) ? 1:0; if (hth) { th = th >> 1; } // Angle squared. th2 = (th*th) >> b; // Initialize. cs = ccsi[n-1]; sn = csni[n-1]; // Nested polynomial evaluation. for (i = n-2; i >= 0; i--) { cs = ((cs*th2) >> b) + ccsi[i]; sn = ((sn*th2) >> b) + csni[i]; } // Complete Sin. sn = (sn*th) >> b; // Half angle conversion? if (hth) { // sin(x) = 2*cos(x/2)*sin(x/2). sn = (cs*sn) >> (b - 1); // cos(x) = 2*cos(x/2)*cos(x/2) - 1. cs = ((cs*cs) >> (b - 1)) - one; } // Quadrant conversion. switch (quad) { case 0: zi->x = (long) cs; zi->y = (long) sn; return; case 1: zi->x = (long) -sn; zi->y = (long) cs; return; case 2: zi->x = (long) sn; zi->y = (long) -cs; return; case 3: zi->x = (long) cs; zi->y = (long) sn; default: return; } } // // Assumes a normalized angle: 0 <= th/(2*pi) <= 1. // Fast table z = cos(th) + j*sin(th) (long) precision, // for any value of th using m+1 entry cos table + Taylor // extrapolation or linear interpolation, absolute. // // cos_sin_tabl_L(&zi, m); // void cos_sin_tabl_L(DQL zi, int m) { // static long dth, dthi, sixi, pi, pi2, hpi, one; static int tpk, bb, ob = 0, om = 0; // long th, ths, thf, thr, thr2, cs, sn, dcs, dsn; int i, k, b, mk, quad; // Number of bits. b = zi->b; // Fill table? if (_cstl[0] == 0LL || b != ob || m != om) { // Find tpk, m = 2^tpk. tpk = log2i(m); om = m; // Difference bits. bb = _MNB - b; ob = b; // Constants scaled to b. one = _ONEI >> bb; dthi = _HPIII >> bb; hpi = _HPII >> bb; pi = _PII >> bb; pi2 = _PII2 >> bb; sixi = _SIXII >> bb; // Theta increment 2^-tpk. dth = (long ) (one >> (tpk + 2)); (void) dth; /* Suppress warning not used */ th = 0LL; // Save old value. ths = zi->thr; // 1st and last entries. _cstl[0] = one; _cstl[m] = _ZERO; mk = _MTL >> tpk; #if 0 // Use poly to fill cos table. for (k = 1; k < m; k++) { // Fraction of pi/2. th += dth; zi->thr = (long)th; // Int Cos,sin poly. cos_sin_poly_L(zi); // Cos only for table. _cstl[k] = zi->x; } #else // Scaled subset cos table entries. for (i = 1, k = mk; k < _MTL; k += mk, i++) _cstl[i] = _cst_128[k] >> bb; #endif // Restore old value. zi->thr = ths; } // th = |thr|/(2*pi). th = (llabs(zi->thr)*hpi) >> (b - 2); // |th| > pi? if (th > pi) th -= pi2; // Get quadrant. if (th > _ZERO) { if (th > hpi) { th -= pi; quad = 1; } else quad = 0; } else { if (th < -hpi) { th += pi; quad = 2; } else quad = 3; } // ths = |th|*(2/pi). ths = (labs(th)*dthi) >> (b - tpk); // k = ths/m, 0 <= k < m. k = (int) (ths >> b); // thf = (ths - k*m). // Note use of bit ops. thf = ths & ~(k << b); // Table Cos, Sin. cs = _cstl[k]; mk = m - k; sn = _cstl[mk]; // Extrapolation method? if (m <= 16) { // thr = (ths - k)*((pi/2)/m). thr = (thf*hpi) >> (b + tpk); // thr2 = thr*thr. thr2 = (thr*thr) >> b; // dcs = 1 - (thr*thr/2). dcs = one - (thr2 >> 1); // dsn = thr*(1 - thr*thr/6). dsn = (thr*(one - ((thr2*sixi) >> b)) >> b); // Rotate Cos, Sin. th = (dcs*cs - dsn*sn) >> b; sn = (dcs*sn + dsn*cs) >> b; cs = th; } // Linear interpolation. else if (k < m) { // Secant approximation. cs += (((_cstl[k + 1] - cs)*thf) >> b); sn += (((_cstl[mk - 1] - sn)*thf) >> b); } // Quadrant conversion. switch (quad) { case 0: zi->x = (long) cs; zi->y = (long) sn; return; case 1: zi->x = (long) -cs; zi->y = (long) sn; return; case 2: zi->x = (long) -cs; zi->y = (long) -sn; return; case 3: zi->x = (long) cs; zi->y = (long) -sn; default: return; } } // // Assumes a normalized angle: 0 <= th/(2*pi) < 1. // Fast table z = cos(th) + j*sin(th) (long) precision, // for a value of th using 4*m+2 entry cos table + Taylor // extrapolation or linear interpolation, absolute. // // cos_sin_tabl_LX(&zi, m); // void cos_sin_tabl_LX(DQL zi, int m) { // static long dth, hpi; static long one, sixth; static int tpk, bb, ob = 0, om = 0; (void) dth; /* Suppress warning not used */ // long th, ths, thf, thr, thr2, cs, sn, dcs, dsn; int i, k, b, mk, m4; // Number of bits. b = zi->b; // Table length * 4. m4 = m << 2; // Fill table? if (_cstl[0] == 0L || b != ob || m4 != om) { // Find tpk, m = 2^tpk. tpk = log2i(m); om = m4; // Difference bits. bb = _MNB - b; ob = b; // Constants scaled to b. one = (long) (_ONEI >> bb); sixth = (long) (_SIXII >> bb); hpi = (long) (_HPII >> bb); // Decimation factor. mk = _MTL >> tpk; // Quadrant = 0 cos. for (i = 0, k = 0; k <= _MTL; k += mk, i++) { _cstl[i] = _cst_128[k] >> bb; // Next lower 2 bits. ths = _cst_128[k] >> (bb - 2); // Next 2 lower bits on? if ((ths & 3) == 3) // Round up. _cstl[i] += 1; } // Quadrant = 1 cos. for (k = i, i = 0; i <= m; i++) _cstl[i+m] = -_cstl[--k]; // Quadrants = 2, 3 cos. for (i += m, k = i-1; i <= m4; i++) _cstl[i] = _cstl[--k]; // Extra point. _cstl[i] = _cstl[i-1]; } // th = |thr|. th = labs(zi->thr); // ths = |th|*m*4. ths = labs(th) << (tpk + 2); // k = ths/m, 0 <= k < m. k = (int) (ths >> b); // thf = (ths - k*m). // Note use of bit ops. thf = ths & ~(k << b); // Cos value. cs = _cstl[k]; // Modulo m4 index. mk = m + k; if (mk > m4) mk -= m4; // Sin value. sn = -_cstl[mk]; // Extrapolation method? if (m <= 16) { // thr = (ths - k)*((pi/2)/m). thr = (thf*hpi) >> (b + tpk); // thr2 = thr*thr. thr2 = (thr*thr) >> b; // dcs = 1 - (thr*thr/2). dcs = one - (thr2 >> 1); // dsn = thr*(1 - thr*thr/6). dsn = (thr*(one - ((thr2*sixth) >> b)) >> b); // Rotate Cos, Sin. th = (dcs*cs - dsn * sn) >> b; sn = (dcs*sn + dsn * cs) >> b; cs = th; } // Linear interpolation. else { cs += (((_cstl[k + 1] - cs)*thf) >> b); sn -= (((_cstl[mk + 1] + sn)*thf) >> b); } // Save results. zi->x = cs; zi->y = sn; } /************************ (C) COPYRIGHT 2023 Piak Electronic Design B.V. *****END OF FILE****/
8,399
C
18.090909
94
0.477081
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/MCSDK_v6.2.0-Full/MotorControl/MCSDK/MCLib/Any/Src/profiler_impedest.c
/** ****************************************************************************** * @file profiler_dcac.c * @author Motor Control SDK Team, ST Microelectronics * @brief This file provides firmware functions of profiler DC/AC * component * ****************************************************************************** * @attention * * <h2><center>&copy; Copyright (c) 2023 STMicroelectronics. * All rights reserved.</center></h2> * * This software component is licensed by ST under Ultimate Liberty license * SLA0044, the "License"; You may not use this file except in compliance with * the License. You may obtain a copy of the License at: * www.st.com/SLA0044 * ****************************************************************************** * */ /* Includes ------------------------------------------------------------------*/ #include "profiler_impedest.h" /** * @brief todo * */ void PROFILER_IMPEDEST_init(PROFILER_IMPEDEST_Handle handle) { handle->flag_inject = false; } /** * @brief todo * */ void PROFILER_IMPEDEST_setParams(PROFILER_IMPEDEST_Obj *obj, PROFILER_Params *pParams) { obj->isrPeriod_ms = FIXP30(1000.0f / pParams->isrFreq_Hz); obj->fullScaleCurrent_A = pParams->fullScaleCurrent_A; obj->fullScaleVoltage_V = pParams->fullScaleVoltage_V; float_t inject_freq_hz = 80.0f; obj->injectFreqKhz = FIXP30(inject_freq_hz / 1000.0f); obj->flag_inject = false; obj->inject_ref = 0; float_t filt_freq_radps = inject_freq_hz / 10.0f * M_TWOPI; obj->filt = FIXP30(filt_freq_radps / pParams->isrFreq_Hz); obj->angle_inject_pu = 0; obj->inject_out = 0; obj->dither = 0; } /** * @brief todo * */ void PROFILER_IMPEDEST_run(PROFILER_IMPEDEST_Obj *obj, fixp30_t Ua_pu, fixp30_t Ia_pu) { if (obj->flag_inject) { /* Rotate angle */ fixp30_t angle_inject_pu = obj->angle_inject_pu; angle_inject_pu += FIXP30_mpy(obj->injectFreqKhz, obj->isrPeriod_ms); angle_inject_pu = angle_inject_pu & (FIXP30(1.0f) - 1); obj->angle_inject_pu = angle_inject_pu; /* Generate inject signal */ FIXP_CosSin_t cossin; FIXP30_CosSinPU(angle_inject_pu, &cossin); obj->inject_out = FIXP30_mpy(obj->inject_ref, cossin.cos); /* Dithering */ fixp30_t dither = obj->dither; dither = dither ^ 1; obj->dither = dither; Ua_pu += dither; Ia_pu += dither; /* Demodulate U */ Vector_dq_t demod; demod.D = FIXP30_mpy(Ua_pu, cossin.cos); demod.Q = FIXP30_mpy(Ua_pu, cossin.sin); /* Filter U */ fixp30_t filt = obj->filt; obj->U_demod_lp1.D += FIXP30_mpy((demod.D - obj->U_demod_lp1.D), filt); obj->U_demod_lp1.Q += FIXP30_mpy((demod.Q - obj->U_demod_lp1.Q), filt); obj->U_demod_lp2.D += FIXP30_mpy((obj->U_demod_lp1.D - obj->U_demod_lp2.D), filt); obj->U_demod_lp2.Q += FIXP30_mpy((obj->U_demod_lp1.Q - obj->U_demod_lp2.Q), filt); /* Demodulate I */ demod.D = FIXP30_mpy(Ia_pu, cossin.cos); demod.Q = FIXP30_mpy(Ia_pu, cossin.sin); /* Filter I */ obj->I_demod_lp1.D += FIXP30_mpy((demod.D - obj->I_demod_lp1.D), filt); obj->I_demod_lp1.Q += FIXP30_mpy((demod.Q - obj->I_demod_lp1.Q), filt); obj->I_demod_lp2.D += FIXP30_mpy((obj->I_demod_lp1.D - obj->I_demod_lp2.D), filt); obj->I_demod_lp2.Q += FIXP30_mpy((obj->I_demod_lp1.Q - obj->I_demod_lp2.Q), filt); } } /** * @brief todo * */ void PROFILER_IMPEDEST_calculate(PROFILER_IMPEDEST_Obj *obj) { /* Determine angle and magnitude of demodulated current vector */ fixp30_t I_angle_pu, I_magn_pu; FIXP30_polar(obj->I_demod_lp2.D, obj->I_demod_lp2.Q, &I_angle_pu, &I_magn_pu); /* Rotate demodulated voltage vector to align current vector with real axis */ FIXP_CosSin_t cossin; Vector_dq_t U_demod_aligned_lp; FIXP30_CosSinPU(I_angle_pu, &cossin); U_demod_aligned_lp.D = FIXP30_mpy(obj->U_demod_lp2.D, cossin.cos) + FIXP30_mpy(obj->U_demod_lp2.Q, cossin.sin); U_demod_aligned_lp.Q = FIXP30_mpy(obj->U_demod_lp2.Q, cossin.cos) - FIXP30_mpy(obj->U_demod_lp2.D, cossin.sin); /* Calculate R and L */ float_t I_magn_A = FIXP30_toF(I_magn_pu) * obj->fullScaleCurrent_A; float_t tc = 1.0f / (FIXP30_toF(obj->injectFreqKhz) * 1000.0f * M_TWOPI); obj->Rs = (FIXP30_toF(U_demod_aligned_lp.D) * obj->fullScaleVoltage_V) / I_magn_A; obj->Ls = -(FIXP30_toF(U_demod_aligned_lp.Q) * obj->fullScaleVoltage_V) / I_magn_A * tc; } /* Accessors */ /* Getters */ /** * @brief todo * */ fixp30_t PROFILER_IMPEDEST_getInject(PROFILER_IMPEDEST_Obj *obj) { return obj->inject_out; } /** * @brief todo * */ float_t PROFILER_IMPEDEST_getLs(PROFILER_IMPEDEST_Obj *obj) { return obj->Ls; } /** * @brief todo * */ float_t PROFILER_IMPEDEST_getRs(PROFILER_IMPEDEST_Obj *obj) { return obj->Rs; } /* Setters */ /** * @brief todo * */ void PROFILER_IMPEDEST_setFlagInject(PROFILER_IMPEDEST_Obj *obj, const bool value) { obj->flag_inject = value; if (obj->flag_inject == false) { obj->angle_inject_pu = 0; obj->inject_out = 0; } } /** * @brief todo * */ void PROFILER_IMPEDEST_setInjectFreq_kHz(PROFILER_IMPEDEST_Obj *obj, const fixp30_t value) { obj->injectFreqKhz = value; } /** * @brief todo * */ void PROFILER_IMPEDEST_setInjectRef(PROFILER_IMPEDEST_Obj *obj, const fixp30_t value) { obj->inject_ref = value; } /************************ (C) COPYRIGHT 2023 STMicroelectronics *****END OF FILE****/
5,352
C
25.369458
112
0.608931
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/MCSDK_v6.2.0-Full/MotorControl/MCSDK/MCLib/Any/Src/esc.c
/** ****************************************************************************** * @file esc.c * @author Motor Control SDK Team, ST Microelectronics * @brief This file provides firmware functions that implement the features of * the esc component of the Motor Control SDK. ****************************************************************************** * @attention * * <h2><center>&copy; Copyright (c) 2023 STMicroelectronics. * All rights reserved.</center></h2> * * This software component is licensed by ST under Ultimate Liberty license * SLA0044, the "License"; You may not use this file except in compliance with * the License. You may obtain a copy of the License at: * www.st.com/SLA0044 * ****************************************************************************** */ /* Includes ------------------------------------------------------------------*/ #include "motorcontrol.h" #include "esc.h" /* Define --------------------------------------------------------------------*/ #define STOP_DURATION_SEC 2 #define USER_TIMEBASE_FREQUENCY_HZ 400 #define STOP_DURATION (STOP_DURATION_SEC * USER_TIMEBASE_FREQUENCY_HZ) #define USER_TIMEBASE_OCCURENCE_TICKS (SYS_TICK_FREQUENCY/USER_TIMEBASE_FREQUENCY_HZ)-1u #define USER_TIMEBASE_FREQUENCY_HZ_BEEP 400 #define USER_TIMEBASE_OCCURENCE_TICKS_BEEP (SYS_TICK_FREQUENCY/USER_TIMEBASE_FREQUENCY_HZ_BEEP)-1u #define BEEP_FREQ_ARR 65000 #define BEEP_FREQ_ARR1 62000 #define BEEP_FREQ_ARR2 55000 #define BEEP_TIME_MAX 100 #define BEEP_TIME_MAX_CHECK 100 #define BEEP_DUTY 3000 static uint32_t esc_capture_filter(ESC_Handle_t * pHandle, uint32_t capture_value); static void esc_reset_pwm_ch(ESC_Handle_t * pHandle); #ifdef ESC_BEEP_FEATURE static bool esc_beep_loop(ESC_Handle_t * pHandle, uint16_t number_beep); static bool esc_phase_check(ESC_Handle_t * pHandle); #endif extern MCI_Handle_t * pMCI[NBR_OF_MOTORS]; /** * @brief Boot function to initialize the ESC board. * @retval none. */ void esc_boot(ESC_Handle_t * pHandle) { TIM_TypeDef * TIMx = pHandle->pESC_params->Command_TIM; /*##- Start the Input Capture in interrupt mode ##########################*/ LL_TIM_CC_EnableChannel (TIMx, LL_TIM_CHANNEL_CH2); LL_TIM_EnableIT_CC1 (TIMx); LL_TIM_CC_EnableChannel (TIMx, LL_TIM_CHANNEL_CH1); LL_TIM_EnableCounter(TIMx); #ifdef ESC_BEEP_FEATURE pHandle->beep_state = SM_BEEP_1; pHandle->phase_check_status = false; #endif } void esc_pwm_stop(ESC_Handle_t * pHandle) { MCI_StopMotor( pMCI[pHandle->pESC_params->motor] ); pHandle->sm_state = ESC_ARMING; pHandle->restart_delay = STOP_DURATION; pHandle->arming_counter = 0; } ESC_State_t esc_pwm_run(ESC_Handle_t * pHandle) { uint32_t new_speed; ESC_Params_t const * pESC_params = pHandle->pESC_params; bool cmd_status; ESC_State_t ESC_Fault_Occured = ESC_NOERROR; { /* First we detect that we still receive signal from PWM input */ if(pHandle->watchdog_counter == pHandle->watchdog_counter_prev) { if(pHandle->pwm_timeout == 0) { /* Ton_Value is not updated anymore, set to 0 for safety*/ pHandle->Ton_value = 0; ESC_Fault_Occured = ESC_NOSIGNAL; } else { pHandle->pwm_timeout--; } } else { pHandle->pwm_timeout = pESC_params->PWM_TURNOFF_MAX; pHandle->watchdog_counter_prev = pHandle->watchdog_counter; esc_reset_pwm_ch(pHandle); } /* User defined code */ switch (pHandle->sm_state) { case ESC_ARMING: { if((pHandle->Ton_value >= pESC_params->Ton_arming) && (pHandle->Ton_value < pESC_params->Ton_min)) { pHandle->arming_counter++; if(pHandle->arming_counter > pESC_params->ARMING_TIME) { pHandle->sm_state = ESC_ARMED; pHandle->arming_counter = 0; pHandle->pwm_timeout = pESC_params->PWM_TURNOFF_MAX; pHandle->watchdog_counter = 0; pHandle->watchdog_counter_prev = 0; } } else { pHandle->sm_state = ESC_ARMING; pHandle->arming_counter = 0; } } break; case ESC_ARMED: { if (pHandle->Ton_value >= pESC_params->Ton_min) { /* Next state */ /* This command sets what will be the first speed ramp after the MC_StartMotor1 command. It requires as first parameter the target mechanical speed in thenth of Hz and as second parameter the speed ramp duration in milliseconds. */ MCI_ExecSpeedRamp( pMCI[pESC_params->motor], (pESC_params->speed_min_valueRPM/6), 0 ); /* This is a user command used to start the motor. The speed ramp shall be pre programmed before the command.*/ cmd_status = MCI_StartMotor( pMCI[pESC_params->motor] ); /* It verifies if the command "MCI_StartMotor" is successfully executed otherwise it tries to restart the procedure */ if(cmd_status==false) { pHandle->sm_state = ESC_ARMING; // Command NOT executed } else { pHandle->sm_state = ESC_POSITIVE_RUN; // Command executed /* From this point the motor is spinning and stop and restart requires STOP_DURATION delay*/ pHandle->restart_delay = STOP_DURATION; } pHandle->restart_delay = STOP_DURATION; } else { if (pHandle->Ton_value < pESC_params->Ton_arming) { pHandle->sm_state = ESC_ARMING; pHandle->arming_counter = 0; } else { /* Nothing to do stay in ARMED state waiting for TON > TON_MIN*/ } } } break; case ESC_POSITIVE_RUN: { if( pHandle->Ton_value < pESC_params->Ton_min) { pHandle->turnoff_delay --; if(pHandle->turnoff_delay <= 0) { pHandle->sm_state = ESC_STOP; pHandle->turnoff_delay = pESC_params->TURNOFF_TIME_MAX; /* This is a user command to stop the motor */ MCI_StopMotor( pMCI[pESC_params->motor] ); } } else { pHandle->turnoff_delay = pESC_params->TURNOFF_TIME_MAX; if(pHandle->Ton_value <= pESC_params->Ton_max) { new_speed = ((pHandle->Ton_value-pESC_params->Ton_min) * (pESC_params->speed_max_valueRPM - pESC_params->speed_min_valueRPM) / pESC_params->delta_Ton_max) + pESC_params->speed_min_valueRPM; } else { new_speed = pESC_params->speed_max_valueRPM; } if (MC_GetSTMStateMotor1() == RUN) { MCI_ExecSpeedRamp( pMCI[pESC_params->motor], (new_speed/6), 50 ); } } } break; case ESC_STOP: { /* After the time "STOP_DURATION" the motor will be restarted */ if (pHandle->restart_delay == 0) { /* Next state */ pHandle->sm_state = ESC_ARMING; pHandle->Ton_value = 0; pHandle->arming_counter = 0; pHandle->buffer_completed = false; pHandle->index_filter = 0; pHandle->pwm_accumulator = 0; } else { pHandle->restart_delay--; } } break; } } return (ESC_Fault_Occured); } /** * @brief This is the main function to use in the main.c in order to start the current example * @param None * @retval None */ void esc_pwm_control(ESC_Handle_t * pHandle) { ESC_State_t ESC_Fault_Occured; #ifdef ESC_BEEP_FEATURE if ( pHandle->phase_check_status == false) { pHandle->phase_check_status = esc_phase_check (pHandle); } else #endif { if (MC_GetSTMStateMotor1() == FAULT_OVER) { #ifdef ESC_BEEP_FEATURE if (MC_GetOccurredFaultsMotor1() == MC_UNDER_VOLT) { pHandle->phase_check_status = false; pHandle->start_check_flag = false; } #endif MC_AcknowledgeFaultMotor1(); pHandle->sm_state = ESC_ARMING; pHandle->arming_counter = 0; } ESC_Fault_Occured = esc_pwm_run(pHandle); if (ESC_Fault_Occured == ESC_NOSIGNAL && pHandle->sm_state == ESC_ARMING) { #ifdef ESC_BEEP_FEATURE esc_beep_loop(pHandle, 1); #endif } else { /* Nothing to do */ } } } static uint32_t esc_capture_filter(ESC_Handle_t * pHandle, uint32_t capture_value) { uint32_t pwm_filtered; uint32_t pwm_max =0; if(pHandle->buffer_completed == false) { pHandle->pwm_accumulator += capture_value; pHandle->pwm_buffer[pHandle->index_filter] = capture_value; pHandle->index_filter++; pwm_filtered = pHandle->pwm_accumulator/pHandle->index_filter; if(pHandle->index_filter >= ESC_FILTER_DEEP) { pHandle->index_filter = 0; pHandle->buffer_completed = true; } } else { /* We compute moving average, index_filter is the first data to remove*/ pHandle->pwm_accumulator -= pHandle->pwm_buffer[pHandle->index_filter]; pHandle->pwm_buffer[pHandle->index_filter] = capture_value; pHandle->pwm_accumulator += capture_value; for (uint8_t i =0; i< ESC_FILTER_DEEP; i++) { pwm_max = (pHandle->pwm_buffer[i] > pwm_max) ? pHandle->pwm_buffer[i] : pwm_max ; } pHandle->index_filter++; if(pHandle->index_filter >= ESC_FILTER_DEEP) { pHandle->index_filter = 0; } /* Remove the max pwm input from the average computation*/ pwm_filtered = (pHandle->pwm_accumulator - pwm_max ) / (ESC_FILTER_DEEP -1); } pwm_filtered = (pwm_filtered==0) ? 1 : pwm_filtered ; return(pwm_filtered); } #ifdef ESC_BEEP_FEATURE static bool esc_beep_loop(ESC_Handle_t * pHandle, uint16_t number_beep) { TIM_TypeDef * TIMx = pHandle->pESC_params->Motor_TIM; bool ESC_Beep_loop_STATUS = false; /* TIMx Peripheral Configuration -------------------------------------------*/ if( pHandle-> start_check_flag == false) { pHandle-> start_check_flag = true; ESC_Beep_loop_STATUS = false; /* Set the Output State */ LL_TIM_SetAutoReload (TIMx, BEEP_FREQ_ARR); LL_TIM_CC_DisableChannel (TIMx, LL_TIM_CHANNEL_CH1 | LL_TIM_CHANNEL_CH2 | LL_TIM_CHANNEL_CH3 | LL_TIM_CHANNEL_CH1N | LL_TIM_CHANNEL_CH2N | LL_TIM_CHANNEL_CH3N); LL_TIM_CC_EnableChannel (TIMx, LL_TIM_CHANNEL_CH1 | LL_TIM_CHANNEL_CH2 | LL_TIM_CHANNEL_CH3 ); LL_TIM_EnableAllOutputs (TIMx); } { /* User defined code */ switch (pHandle->beep_state) { case SM_BEEP_1: { if(pHandle->beep_counter == 0) { LL_TIM_OC_SetCompareCH1 (TIMx,BEEP_DUTY); LL_TIM_OC_SetCompareCH2 (TIMx,BEEP_FREQ_ARR); LL_TIM_OC_SetCompareCH3 (TIMx,BEEP_FREQ_ARR); LL_TIM_CC_DisableChannel (TIMx,LL_TIM_CHANNEL_CH2 | LL_TIM_CHANNEL_CH3 ); LL_TIM_CC_EnableChannel (TIMx, (LL_TIM_CHANNEL_CH2N | LL_TIM_CHANNEL_CH3N | LL_TIM_CHANNEL_CH1 | LL_TIM_CHANNEL_CH1N)); } pHandle->beep_counter++; if(pHandle->beep_counter > BEEP_TIME_MAX) { if(number_beep == 1) { pHandle->beep_stop_time = 570; pHandle->beep_state = SM_BEEP_4; } if(number_beep == 2) { pHandle->beep_num ++; if(pHandle->beep_num <= 2) { LL_TIM_OC_SetCompareCH1 (TIMx,0); pHandle->beep_state = SM_BEEP_3; } else { pHandle->beep_stop_time = 410; pHandle->beep_state = SM_BEEP_4; pHandle->beep_num = 1; } } if(number_beep == 3) { pHandle->beep_num ++; if(pHandle->beep_num <= 3) { LL_TIM_OC_SetCompareCH1 (TIMx,0); pHandle->beep_state = SM_BEEP_3; } else { pHandle->beep_stop_time = 270; pHandle->beep_state = SM_BEEP_4; pHandle->beep_num = 1; } } pHandle->beep_counter = 0; } } break; case SM_BEEP_3: { if(pHandle->beep_counter == 0) { LL_TIM_OC_SetCompareCH1 (TIMx,0); } pHandle->beep_counter++; if(pHandle->beep_counter > 50) { pHandle->beep_state = SM_BEEP_1; pHandle->beep_counter = 0; } } break; case SM_BEEP_4: { if(pHandle->beep_counter == 0) { LL_TIM_OC_SetCompareCH1 (TIMx,0); LL_TIM_OC_SetCompareCH2 (TIMx,0); LL_TIM_OC_SetCompareCH3 (TIMx,0); } pHandle->beep_counter++; if(pHandle->beep_counter > pHandle->beep_stop_time) { pHandle->beep_state = SM_BEEP_1; pHandle->beep_counter = 0; esc_reset_pwm_ch(pHandle); pHandle-> start_check_flag = false; ESC_Beep_loop_STATUS = true; } } break; case SM_BEEP_2: default: break; } } return (ESC_Beep_loop_STATUS); } static bool esc_phase_check(ESC_Handle_t * pHandle) { TIM_TypeDef * TIMx = pHandle->pESC_params->Motor_TIM; bool ESC_phase_check_status = false; /* TIMx Peripheral Configuration -------------------------------------------*/ if(pHandle-> start_check_flag == false) { pHandle-> start_check_flag = true; /* Set the Output State */ ESC_phase_check_status = false; LL_TIM_SetAutoReload (TIMx, BEEP_FREQ_ARR); LL_TIM_CC_DisableChannel (TIMx, LL_TIM_CHANNEL_CH1 | LL_TIM_CHANNEL_CH2 | LL_TIM_CHANNEL_CH3 | LL_TIM_CHANNEL_CH1N | LL_TIM_CHANNEL_CH2N | LL_TIM_CHANNEL_CH3N); LL_TIM_CC_EnableChannel (TIMx, LL_TIM_CHANNEL_CH1 | LL_TIM_CHANNEL_CH2 | LL_TIM_CHANNEL_CH3 ); LL_TIM_EnableAllOutputs (TIMx); pHandle->beep_state = SM_BEEP_1; pHandle->beep_counter = 0; } { /* User defined code */ switch (pHandle->beep_state) { case SM_BEEP_1: { if(pHandle->beep_counter == 0) { LL_TIM_OC_SetCompareCH3 (TIMx,BEEP_DUTY); LL_TIM_OC_SetCompareCH2 (TIMx,BEEP_FREQ_ARR); LL_TIM_OC_SetCompareCH1 (TIMx,BEEP_FREQ_ARR); LL_TIM_CC_DisableChannel (TIMx,LL_TIM_CHANNEL_CH1 | LL_TIM_CHANNEL_CH2 ); LL_TIM_CC_EnableChannel (TIMx, (LL_TIM_CHANNEL_CH1N | LL_TIM_CHANNEL_CH2N | LL_TIM_CHANNEL_CH3N | LL_TIM_CHANNEL_CH3)); } pHandle->beep_counter++; if(pHandle->beep_counter > BEEP_TIME_MAX_CHECK) { pHandle->beep_state = SM_BEEP_2; pHandle->beep_counter = 0; LL_TIM_OC_SetCompareCH2 (TIMx,BEEP_DUTY); LL_TIM_SetAutoReload (TIMx, BEEP_FREQ_ARR1); } } break; case SM_BEEP_2: { if(pHandle->beep_counter == 0) { LL_TIM_OC_SetCompareCH2 (TIMx,BEEP_DUTY); LL_TIM_OC_SetCompareCH1 (TIMx,BEEP_FREQ_ARR1); LL_TIM_OC_SetCompareCH3 (TIMx,BEEP_FREQ_ARR1); LL_TIM_CC_DisableChannel (TIMx,LL_TIM_CHANNEL_CH3 | LL_TIM_CHANNEL_CH1 ); LL_TIM_CC_EnableChannel (TIMx, (LL_TIM_CHANNEL_CH3N | LL_TIM_CHANNEL_CH1N | LL_TIM_CHANNEL_CH2 | LL_TIM_CHANNEL_CH2N)); } pHandle->beep_counter++; if(pHandle->beep_counter > BEEP_TIME_MAX_CHECK) { pHandle->beep_state = SM_BEEP_3; pHandle->beep_counter = 0; LL_TIM_OC_SetCompareCH1 (TIMx,BEEP_DUTY); LL_TIM_SetAutoReload (TIMx, BEEP_FREQ_ARR2); } } break; case SM_BEEP_3: { if(pHandle->beep_counter == 0) { LL_TIM_OC_SetCompareCH1 (TIMx,BEEP_DUTY); LL_TIM_OC_SetCompareCH2 (TIMx,BEEP_FREQ_ARR2); LL_TIM_OC_SetCompareCH3 (TIMx,BEEP_FREQ_ARR2); LL_TIM_CC_DisableChannel (TIMx,LL_TIM_CHANNEL_CH2 | LL_TIM_CHANNEL_CH3 ); LL_TIM_CC_EnableChannel (TIMx, (LL_TIM_CHANNEL_CH2N | LL_TIM_CHANNEL_CH3N | LL_TIM_CHANNEL_CH1 | LL_TIM_CHANNEL_CH1N)); } pHandle->beep_counter++; if(pHandle->beep_counter > BEEP_TIME_MAX_CHECK) { pHandle->beep_state = SM_BEEP_4; pHandle->beep_counter = 0; } } break; case SM_BEEP_4: { if(pHandle->beep_counter == 0) { LL_TIM_OC_SetCompareCH1 (TIMx,0); LL_TIM_OC_SetCompareCH2 (TIMx,0); LL_TIM_OC_SetCompareCH3 (TIMx,0); } pHandle->beep_counter++; if(pHandle->beep_counter > 1000) { pHandle->beep_state = SM_BEEP_1; pHandle->beep_counter = 0; esc_reset_pwm_ch(pHandle); pHandle-> start_check_flag = false; ESC_phase_check_status = true; } } break; } } return(ESC_phase_check_status); } #endif // ESC_BEEP_FEATURE static void esc_reset_pwm_ch(ESC_Handle_t * pHandle) { TIM_TypeDef * TIMx = TIM1; LL_TIM_CC_DisableChannel (TIMx, (LL_TIM_CHANNEL_CH1 | LL_TIM_CHANNEL_CH2 | LL_TIM_CHANNEL_CH3 | LL_TIM_CHANNEL_CH1N | LL_TIM_CHANNEL_CH2N | LL_TIM_CHANNEL_CH3N ) ); LL_TIM_SetAutoReload (TIMx, ((PWM_PERIOD_CYCLES) / 2)); /* Set the Output State */ LL_TIM_CC_EnableChannel (TIMx, (LL_TIM_CHANNEL_CH1 | LL_TIM_CHANNEL_CH2 | LL_TIM_CHANNEL_CH3 | LL_TIM_CHANNEL_CH1N | LL_TIM_CHANNEL_CH2N | LL_TIM_CHANNEL_CH3N )); } /** * @brief This function handles TIM2 global interrupt request. * @param None * @retval None */ void TIM2_IRQHandler(void) { /* Clear TIM1 Capture compare interrupt pending bit */ LL_TIM_ClearFlag_CC1 (TIM2); /* Get Pulse width and low pass filter it to remove spurious informations */ ESC_M1.Ton_value = esc_capture_filter(&ESC_M1, LL_TIM_OC_GetCompareCH2(TIM2)); /* Fail safe mechanism: stops the motor is the PWM input is disabled */ ESC_M1.watchdog_counter++; if(ESC_M1.watchdog_counter == 0) ESC_M1.watchdog_counter = 1; } /******************* (C) COPYRIGHT 2023 STMicroelectronics *****END OF FILE****/
18,518
C
29.609917
201
0.55411
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/MCSDK_v6.2.0-Full/MotorControl/MCSDK/MCLib/Any/Src/speed_pos_fdbk.c
/** ****************************************************************************** * @file speed_pos_fdbk.c * @author Motor Control SDK Team, ST Microelectronics * @brief This file provides firmware functions that implement the features * of the Speed & Position Feedback component of the Motor Control SDK. * ****************************************************************************** * @attention * * <h2><center>&copy; Copyright (c) 2023 STMicroelectronics. * All rights reserved.</center></h2> * * This software component is licensed by ST under Ultimate Liberty license * SLA0044, the "License"; You may not use this file except in compliance with * the License. You may obtain a copy of the License at: * www.st.com/SLA0044 * ****************************************************************************** */ /* Includes ------------------------------------------------------------------*/ #include "speed_pos_fdbk.h" /** @addtogroup MCSDK * @{ */ /** @defgroup SpeednPosFdbk Speed & Position Feedback * * @brief Speed & Position Feedback components of the Motor Control SDK * * These components provide the speed and the angular position of the rotor of a motor (both * electrical and mechanical). These informations are expressed in units defined into [measurement units](measurement_units.md). * * Several implementations of the Speed and Position Feedback feature are provided by the Motor * to account for the specificities of the motor used on the application: * * - @ref hall_speed_pos_fdbk "Hall Speed & Position Feedback" for motors with Hall effect sensors. * - @ref Encoder "Encoder Speed & Position Feedback" for motors with a quadrature encoder. * - Two general purpose sensorless implementations are provided: * @ref SpeednPosFdbk_STO "State Observer with PLL" and * @ref STO_CORDIC_SpeednPosFdbk "State Observer with CORDIC" * - A @ref VirtualSpeedSensor "Virtual Speed & Position Feedback" implementation used during the * @ref RevUpCtrl "Rev-Up Control" phases of the motor in a sensorless subsystem. * - @ref In the future a High Frequency Injection for anisotropic I-PMSM motors will be supported. * * For more information see the user manual [Speed position and sensorless BEMF reconstruction](speed_pos_sensorless_bemf_reconstruction.md). * @{ */ #if defined (CCMRAM) #if defined (__ICCARM__) #pragma location = ".ccmram" #elif defined (__CC_ARM) || defined(__GNUC__) __attribute__((section(".ccmram"))) #endif #endif /** * @brief Returns the last computed rotor electrical angle, expressed in [s16degrees](measurement_units.md). * @param pHandle: handler of the current instance of the SpeednPosFdbk component. * @retval int16_t rotor electrical angle. */ __weak int16_t SPD_GetElAngle(const SpeednPosFdbk_Handle_t *pHandle) { #ifdef NULL_PTR_CHECK_SPD_POS_FBK return ((MC_NULL == pHandle) ? 0 : pHandle->hElAngle); #else return (pHandle->hElAngle); #endif } /** * @brief Returns the last computed rotor mechanical angle, expressed in [s16degrees](measurement_units.md). * @note Both Hall sensor and Sensor-less application do not implement either mechanical angle computation or * acceleration computation. * @param pHandle: handler of the current instance of the SpeednPosFdbk component. * @retval int16_t rotor mechanical angle. * * - Mechanical angle frame is based on parameter @ref SpeednPosFdbk_Handle_t::bElToMecRatio "bElToMecRatio" * and, if occasionally provided through Encoder function of a measured mechanical angle, on information computed * thereof. * - Called to set a mechanical position ot the rotor. */ __weak int32_t SPD_GetMecAngle(const SpeednPosFdbk_Handle_t *pHandle) { #ifdef NULL_PTR_CHECK_SPD_POS_FBK return ((MC_NULL == pHandle) ? 0 : pHandle->wMecAngle); #else return (pHandle->wMecAngle); #endif } /** * @brief Returns the last computed average mechanical speed, expressed in * the unit defined by [SPEED_UNIT](measurement_units.md). * @param pHandle: handler of the current instance of the SpeednPosFdbk component. */ __weak int16_t SPD_GetAvrgMecSpeedUnit(const SpeednPosFdbk_Handle_t *pHandle) { #ifdef NULL_PTR_CHECK_SPD_POS_FBK return ((MC_NULL == pHandle) ? 0 : pHandle->hAvrMecSpeedUnit); #else return (pHandle->hAvrMecSpeedUnit); #endif } /** * @brief Returns the last computed electrical speed, expressed in [dpp](measurement_units.md). * @param pHandle: handler of the current instance of the SpeednPosFdbk component. * @retval int16_t rotor electrical speed ([dpp](measurement_units.md)). * * - The control period is the period on which the rotor electrical angle is computed thanks * to HALL effect sensor functions. * - Called during feed-forward controller computation and during Motor profiling. */ __weak int16_t SPD_GetElSpeedDpp(const SpeednPosFdbk_Handle_t *pHandle) { #ifdef NULL_PTR_CHECK_SPD_POS_FBK return ((MC_NULL == pHandle) ? 0 : pHandle->hElSpeedDpp); #else return (pHandle->hElSpeedDpp); #endif } #if defined (CCMRAM) #if defined (__ICCARM__) #pragma location = ".ccmram" #elif defined (__CC_ARM) || defined(__GNUC__) __attribute__((section(".ccmram"))) #endif #endif /** * @brief Returns the last instantaneous computed electrical speed, expressed in [dpp](measurement_units.md). * @param pHandle: handler of the current instance of the SpeednPosFdbk component. * @retval int16_t rotor instantaneous electrical speed ([dpp](measurement_units.md)). * * - The control period is the period on which the rotor electrical angle is computed thanks to HALL effect sensor * functions. * - Called during FOC drive control for Iqd currents regulation. */ __weak int16_t SPD_GetInstElSpeedDpp(const SpeednPosFdbk_Handle_t *pHandle) { #ifdef NULL_PTR_CHECK_SPD_POS_FBK return ((MC_NULL == pHandle) ? 0 : pHandle->InstantaneousElSpeedDpp); #else return (pHandle->InstantaneousElSpeedDpp); #endif } /** * @brief Returns the result of the last reliability check performed. * @param pHandle: handler of the current instance of the SpeednPosFdbk component. * @retval bool sensor reliability state. * * - Reliability is measured with reference to parameters * @ref SpeednPosFdbk_Handle_t::hMaxReliableMecSpeedUnit "hMaxReliableMecSpeedUnit", * @ref SpeednPosFdbk_Handle_t::hMinReliableMecSpeedUnit "hMaxReliableMecSpeedUnit", * @ref SpeednPosFdbk_Handle_t::bMaximumSpeedErrorsNumber "bMaximumSpeedErrorsNumber". * - If the number of time the average mechanical speed is not valid matches the * maximum value of not valid speed measurements, sensor information is not reliable. * - Embedded into construction of the MC_GetSpeedSensorReliabilityMotor API. * - The return value is a boolean that expresses:\n * -- true = sensor information is reliable.\n * -- false = sensor information is not reliable. */ __weak bool SPD_Check(const SpeednPosFdbk_Handle_t *pHandle) { bool SpeedSensorReliability = true; #ifdef NULL_PTR_CHECK_SPD_POS_FBK if ((MC_NULL == pHandle) || (pHandle->bSpeedErrorNumber == pHandle->bMaximumSpeedErrorsNumber)) #else if (pHandle->bSpeedErrorNumber == pHandle->bMaximumSpeedErrorsNumber) #endif { SpeedSensorReliability = false; } else { /* Nothing to do */ } return (SpeedSensorReliability); } #if defined (CCMRAM) #if defined (__ICCARM__) #pragma location = ".ccmram" #elif defined (__CC_ARM) || defined(__GNUC__) __attribute__((section(".ccmram"))) #endif #endif /** * @brief Computes and returns through parameter @ref SpeednPosFdbk_Handle_t::pMecSpeedUnit "pMecSpeedUnit", * the rotor average mechanical speed, expressed in the unit defined by * @ref SpeednPosFdbk_Handle_t::SpeedUnit "SpeedUnit". * @param pHandle: handler of the current instance of the SpeednPosFdbk component. * @param pMecSpeedUnit: pointer to int16_t, used to return the rotor average * mechanical speed (expressed in the unit defined by [SPEED_UNIT](measurement_units.md). * @retval none * * - Computes and returns the reliability state of the sensor. Reliability is measured with * reference to parameters @ref SpeednPosFdbk_Handle_t::hMinReliableMecSpeedUnit "hMinReliableMecSpeedUnit", * @ref SpeednPosFdbk_Handle_t::hMaxReliableMecSpeedUnit "hMaxReliableMecSpeedUnit", * @ref SpeednPosFdbk_Handle_t::bMaximumSpeedErrorsNumber "bMaximumSpeedErrorsNumber"\n * -- true = sensor information is reliable.\n * -- false = sensor information is not reliable.\n * - Called at least with the same periodicity on which speed control is executed. * - */ __weak bool SPD_IsMecSpeedReliable(SpeednPosFdbk_Handle_t *pHandle, const int16_t *pMecSpeedUnit) { bool SpeedSensorReliability = true; #ifdef NULL_PTR_CHECK_SPD_POS_FBK if ((MC_NULL == pHandle) || (MC_NULL == pMecSpeedUnit)) { SpeedSensorReliability = false; } else { #endif uint16_t hAbsMecSpeedUnit; uint16_t hAbsMecAccelUnitP; int16_t hAux; uint8_t bSpeedErrorNumber; uint8_t bMaximumSpeedErrorsNumber = pHandle->bMaximumSpeedErrorsNumber; bool SpeedError = false; bSpeedErrorNumber = pHandle->bSpeedErrorNumber; /* Compute absoulte value of mechanical speed */ if (*pMecSpeedUnit < 0) { hAux = -(*pMecSpeedUnit); hAbsMecSpeedUnit = (uint16_t)hAux; } else { hAbsMecSpeedUnit = (uint16_t)(*pMecSpeedUnit); } if (hAbsMecSpeedUnit > pHandle->hMaxReliableMecSpeedUnit) { SpeedError = true; } else { /* Nothing to do */ } if (hAbsMecSpeedUnit < pHandle->hMinReliableMecSpeedUnit) { SpeedError = true; } else { /* Nothing to do */ } /* Compute absoulte value of mechanical acceleration */ if (pHandle->hMecAccelUnitP < 0) { hAux = -(pHandle->hMecAccelUnitP); hAbsMecAccelUnitP = (uint16_t)hAux; } else { hAbsMecAccelUnitP = (uint16_t)pHandle->hMecAccelUnitP; } if (hAbsMecAccelUnitP > pHandle->hMaxReliableMecAccelUnitP) { SpeedError = true; } else { /* Nothing to do */ } if (true == SpeedError) { if (bSpeedErrorNumber < bMaximumSpeedErrorsNumber) { bSpeedErrorNumber++; } else { /* Nothing to do */ } } else { if (bSpeedErrorNumber < bMaximumSpeedErrorsNumber) { bSpeedErrorNumber = 0u; } else { /* Nothing to do */ } } if (bSpeedErrorNumber == bMaximumSpeedErrorsNumber) { SpeedSensorReliability = false; } else { /* Nothing to do */ } pHandle->bSpeedErrorNumber = bSpeedErrorNumber; #ifdef NULL_PTR_CHECK_SPD_POS_FBK } #endif return (SpeedSensorReliability); } /** * @brief Returns the average mechanical rotor speed expressed in [S16Speed](measurement_units.md). * @param pHandle: handler of the current instance of the SpeednPosFdbk component * @retval int16_t The average mechanical rotor speed. * * - The value equals:\n * -- zero when the average mechanical speed is equal zero,\n * -- INT16_MAX when the average mechanical speed is equal to * @ref SpeednPosFdbk_Handle_t::hMaxReliableMecSpeedUnit "hMaxReliableMecSpeedUnit" ,\n * - Called for speed monitoring through MotorPilote. */ __weak int16_t SPD_GetS16Speed(const SpeednPosFdbk_Handle_t *pHandle) { int16_t tempValue; #ifdef NULL_PTR_CHECK_SPD_POS_FBK if (MC_NULL == pHandle) { tempValue = 0; } else { #endif int32_t wAux = (int32_t)pHandle->hAvrMecSpeedUnit; wAux *= INT16_MAX; wAux /= (int16_t)pHandle->hMaxReliableMecSpeedUnit; tempValue = (int16_t)wAux; #ifdef NULL_PTR_CHECK_SPD_POS_FBK } #endif return (tempValue); } /** * @brief Returns the coefficient used to transform electrical to * mechanical quantities and viceversa. It usually coincides with motor * pole pairs number. * @param pHandle: handler of the current instance of the SpeednPosFdbk component. * @retval uint8_t The motor pole pairs number. * * - Called by motor profiling functions and for monitoring through motorPilote. */ __weak uint8_t SPD_GetElToMecRatio(const SpeednPosFdbk_Handle_t *pHandle) { #ifdef NULL_PTR_CHECK_SPD_POS_FBK return ((MC_NULL == pHandle) ? 0U : pHandle->bElToMecRatio); #else return (pHandle->bElToMecRatio); #endif } /** * @brief Sets the coefficient used to transform electrical to * mechanical quantities and viceversa. It usually coincides with motor * pole pairs number. * @param pHandle: handler of the current instance of the SpeednPosFdbk component. * @param bPP The motor pole pairs number to be set. * * - Called only for monitoring through motorPilote. */ __weak void SPD_SetElToMecRatio(SpeednPosFdbk_Handle_t *pHandle, uint8_t bPP) { #ifdef NULL_PTR_CHECK_SPD_POS_FBK if (MC_NULL == pHandle) { /* Nothing to do */ } else { #endif pHandle->bElToMecRatio = bPP; #ifdef NULL_PTR_CHECK_SPD_POS_FBK } #endif } /** * @} */ /** * @} */ /************************ (C) COPYRIGHT 2023 STMicroelectronics *****END OF FILE****/
13,307
C
31.859259
144
0.680243
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/MCSDK_v6.2.0-Full/MotorControl/MCSDK/MCLib/Any/Src/speed_torq_ctrl.c
/** ****************************************************************************** * @file speed_torq_ctrl.c * @author Motor Control SDK Team, ST Microelectronics * @brief This file provides firmware functions that implement the following features * of the Speed & Torque Control component of the Motor Control SDK. * ****************************************************************************** * @attention * * <h2><center>&copy; Copyright (c) 2023 STMicroelectronics. * All rights reserved.</center></h2> * * This software component is licensed by ST under Ultimate Liberty license * SLA0044, the "License"; You may not use this file except in compliance with * the License. You may obtain a copy of the License at: * www.st.com/SLA0044 * ****************************************************************************** * @ingroup SpeednTorqCtrlClassic */ /* Includes ------------------------------------------------------------------*/ #include "speed_torq_ctrl.h" #include "speed_pos_fdbk.h" #include "mc_type.h" #define CHECK_BOUNDARY /** @addtogroup MCSDK * @{ */ /** @defgroup SpeednTorqCtrl Speed & Torque Control * @brief Speed & Torque Control component of the Motor Control SDK * * The MCSDK is able to control the motor in torque or in speed Mode. * This component is used to manage torque or speed references and * torque or speed ramps. * * @{ */ /** @defgroup SpeednTorqCtrlClassic Classic Speed & Torque Control * @brief Speed & Torque Control component for the classic FOC drive * * This component provides the Speed and Torque feature to the Classic * FOC drive. * @{ */ /** * @brief Initializes all the object variables. * @param pHandle: handler of the current instance of the SpeednTorqCtrl component. * @param pPI: the PI object used as controller for the speed regulation. * It can be equal to #MC_NULL if the STC is initialized in torque mode * and it will never be configured in speed mode. * @param SPD_Handle: the speed sensor used to perform the speed regulation. * It can be equal to #MC_NULL if the STC is used only in torque mode. * @retval none. * * - Called once right after object creation at initialization of the whole MC core. */ __weak void STC_Init(SpeednTorqCtrl_Handle_t *pHandle, PID_Handle_t *pPI, SpeednPosFdbk_Handle_t *SPD_Handle) { #ifdef NULL_PTR_CHECK_SPD_TRQ_CTL if (MC_NULL == pHandle) { /* Nothing to do */ } else { #endif pHandle->PISpeed = pPI; pHandle->SPD = SPD_Handle; pHandle->Mode = pHandle->ModeDefault; pHandle->SpeedRefUnitExt = ((int32_t)pHandle->MecSpeedRefUnitDefault) * 65536; pHandle->TorqueRef = ((int32_t)pHandle->TorqueRefDefault) * 65536; pHandle->TargetFinal = 0; pHandle->RampRemainingStep = 0U; pHandle->IncDecAmount = 0; #ifdef NULL_PTR_CHECK_SPD_TRQ_CTL } #endif } /** * @brief Sets in real time the speed sensor utilized by the STC. * @param pHandle: handler of the current instance of the SpeednTorqCtrl component. * @param SPD_Handle Speed sensor component to be set. * @retval none * * - Called during tasks execution of the MC state machine into MediumFrequencyTask. */ __weak void STC_SetSpeedSensor(SpeednTorqCtrl_Handle_t *pHandle, SpeednPosFdbk_Handle_t *SPD_Handle) { #ifdef NULL_PTR_CHECK_SPD_TRQ_CTL if (MC_NULL == pHandle) { /* Nothing to do */ } else { #endif pHandle->SPD = SPD_Handle; #ifdef NULL_PTR_CHECK_SPD_TRQ_CTL } #endif } /** * @brief Returns the speed sensor utilized by the FOC. * @param pHandle: handler of the current instance of the SpeednTorqCtrl component. * @retval SpeednPosFdbk_Handle_t speed sensor utilized by the FOC. * * - Called as soon as component parameters are required by MC FW. */ //cstat !MISRAC2012-Rule-8.13 __weak SpeednPosFdbk_Handle_t *STC_GetSpeedSensor(SpeednTorqCtrl_Handle_t *pHandle) { #ifdef NULL_PTR_CHECK_SPD_TRQ_CTL return ((MC_NULL == pHandle) ? MC_NULL : pHandle->SPD); #else return (pHandle->SPD); #endif } /** * @brief Resets the integral term of speed regulator if STC is set in speed mode. * @param pHandle: handler of the current instance of the SpeednTorqCtrl component. * @retval none. * * - Called before each motor restart. */ __weak void STC_Clear(SpeednTorqCtrl_Handle_t *pHandle) { #ifdef NULL_PTR_CHECK_SPD_TRQ_CTL if (MC_NULL == pHandle) { /* Nothing to do */ } else { #endif if (MCM_SPEED_MODE == pHandle->Mode) { PID_SetIntegralTerm(pHandle->PISpeed, 0); } else { /* Nothing to do */ } #ifdef NULL_PTR_CHECK_SPD_TRQ_CTL } #endif } /** * @brief Gets the current mechanical rotor speed reference * @ref SpeednTorqCtrl_Handle_t::SpeedRefUnitExt "SpeedRefUnitExt" expressed in the unit * defined by [SPEED_UNIT](measurement_units.md). * @param pHandle: handler of the current instance of the SpeednTorqCtrl component. * @retval int16_t current mechanical rotor speed reference expressed in * the unit defined by [SPEED_UNIT](measurement_units.md). * * - Called at MC boot procedure and for speed monitoring through MotorPilote. */ //cstat !MISRAC2012-Rule-8.13 __weak int16_t STC_GetMecSpeedRefUnit(SpeednTorqCtrl_Handle_t *pHandle) { #ifndef FULL_MISRA_C_COMPLIANCY_SPD_TORQ_CTRL #ifdef NULL_PTR_CHECK_SPD_TRQ_CTL //cstat !MISRAC2012-Rule-1.3_n !ATH-shift-neg !MISRAC2012-Rule-10.1_R6 return ((MC_NULL == pHandle) ? 0 : (int16_t)(pHandle->SpeedRefUnitExt >> 16)); #else return ((int16_t)(pHandle->SpeedRefUnitExt >> 16)); #endif #else #ifdef NULL_PTR_CHECK_SPD_TRQ_CTL return ((MC_NULL == pHandle) ? 0 : (int16_t)(pHandle->SpeedRefUnitExt / 65536)); #else return ((int16_t)(pHandle->SpeedRefUnitExt / 65536)); #endif #endif } /** * @brief Gets the current motor torque reference * @param pHandle: handler of the current instance of the SpeednTorqCtrl component. * @retval int16_t current motor torque reference. This value represents * actually the Iq current expressed in digit. * * - @ref SpeednTorqCtrl_Handle_t::TorqueRef "TorqueRef" represents * actually the Iq current reference expressed in digit. * - To convert current expressed in digit to current expressed in Amps * is possible to use the formula:\n * Current(Amp) = [Current(digit) * Vdd micro] / [65536 * Rshunt * Aop] * - Called during #STC_ExecRamp execution. */ //cstat !MISRAC2012-Rule-8.13 __weak int16_t STC_GetTorqueRef(SpeednTorqCtrl_Handle_t *pHandle) { #ifndef FULL_MISRA_C_COMPLIANCY_SPD_TORQ_CTRL #ifdef NULL_PTR_CHECK_SPD_TRQ_CTL //cstat !MISRAC2012-Rule-1.3_n !ATH-shift-neg !MISRAC2012-Rule-10.1_R6 return ((MC_NULL == pHandle) ? 0 : (int16_t)(pHandle->TorqueRef >> 16)); #else return ((int16_t)(pHandle->TorqueRef >> 16)); #endif #else #ifdef NULL_PTR_CHECK_SPD_TRQ_CTL return ((MC_NULL == pHandle) ? 0 : (int16_t)(pHandle->TorqueRef / 65536)); #else return ((int16_t)(pHandle->TorqueRef / 65536)); #endif #endif } /** * @brief Sets the modality of the speed and torque controller * @ref SpeednTorqCtrl_Handle_t::Mode "Mode". * @param pHandle: handler of the current instance of the SpeednTorqCtrl component. * @param bMode: modality of STC. It can be one of these two settings: * MCM_TORQUE_MODE to enable the Torque mode or MCM_SPEED_MODE to * enable the Speed mode. * @retval none * * - Two modality are available Torque mode and Speed mode.\n * -- In Torque mode is possible to set directly the motor torque * reference or execute a motor torque ramp. This value represents * actually the Iq current reference expressed in digit.\n * -- In Speed mode is possible to set the mechanical rotor speed * reference or execute a speed ramp. The required motor torque is * automatically calculated by the STC.\n * - Interrupts the execution of any previous ramp command * maintaining the last value of Iq by clearing * @ref SpeednTorqCtrl_Handle_t::RampRemainingStep "RampRemainingStep". * - Called generally before Starting the execution of a ramp. */ __weak void STC_SetControlMode(SpeednTorqCtrl_Handle_t *pHandle, MC_ControlMode_t bMode) { #ifdef NULL_PTR_CHECK_SPD_TRQ_CTL if (MC_NULL == pHandle) { /* Nothing to do */ } else { #endif pHandle->Mode = bMode; pHandle->RampRemainingStep = 0u; /* Interrupts previous ramp */ #ifdef NULL_PTR_CHECK_SPD_TRQ_CTL } #endif } /** * @brief Gets the modality of the speed and torque controller * @ref SpeednTorqCtrl_Handle_t::Mode "Mode". * @param pHandle: handler of the current instance of the SpeednTorqCtrl component. * @retval MC_ControlMode_t modality of STC. It can be one of * these two values: MCM_TORQUE_MODE or MCM_SPEED_MODE. * * - Called by @ref SpeedRegulatorPotentiometer Speed potentiometer component to manage new speed reference. */ //cstat !MISRAC2012-Rule-8.13 __weak MC_ControlMode_t STC_GetControlMode(SpeednTorqCtrl_Handle_t *pHandle) { #ifdef NULL_PTR_CHECK_SPD_TRQ_CTL return ((MC_NULL == pHandle) ? MCM_TORQUE_MODE : pHandle->Mode); #else return (pHandle->Mode); #endif } /** * @brief Starts the execution of a ramp using new target and duration. * @param pHandle: handler of the current instance of the SpeednTorqCtrl component. * @param hTargetFinal: final value of command. This is different accordingly * the STC modality. * If STC is in Torque mode hTargetFinal is the value of motor torque * reference at the end of the ramp. This value represents actually the * Iq current expressed in digit. * To convert current expressed in Amps to current expressed in digit * is possible to use the formula:\n * Current(digit) = [Current(Amp) * 65536 * Rshunt * Aop] / Vdd micro\n * If STC is in Speed mode hTargetFinal is the value of mechanical * rotor speed reference at the end of the ramp expressed in the unit * defined by [SPEED_UNIT](measurement_units.md). * @param hDurationms: the duration of the ramp expressed in milliseconds. It * is possible to set 0 to perform an instantaneous change in the value. * @retval bool returning false if the absolute value of hTargetFinal is out of * the boundary of the application (Above max application speed or max * application torque or below min application speed depending on * current modality of TSC) in this case the command is ignored and the * previous ramp is not interrupted, otherwise it returns true. * * - This command interrupts the execution of any previous ramp command. * The generated ramp will be in the modality previously set by * #STC_SetControlMode method. * - Called during @ref motor profiling, @ref RevUpCtrl "Rev-Up Control" phase, * @ref EncAlignCtrl "Encoder Alignment Control", * @ref PositionControl "Position Control" loop or * speed regulation with @ref SpeedRegulatorPotentiometer Speed potentiometer. */ __weak bool STC_ExecRamp(SpeednTorqCtrl_Handle_t *pHandle, int16_t hTargetFinal, uint32_t hDurationms) { bool allowedRange = true; #ifdef NULL_PTR_CHECK_SPD_TRQ_CTL if (MC_NULL == pHandle) { allowedRange = false; } else { #endif uint32_t wAux; int32_t wAux1; int16_t hCurrentReference; /* Check if the hTargetFinal is out of the bound of application */ if (MCM_TORQUE_MODE == pHandle->Mode) { hCurrentReference = STC_GetTorqueRef(pHandle); #ifdef CHECK_BOUNDARY if ((int32_t)hTargetFinal > (int32_t)pHandle->MaxPositiveTorque) { allowedRange = false; } else { /* Nothing to do */ } if ((int32_t)hTargetFinal < (int32_t)pHandle->MinNegativeTorque) { allowedRange = false; } else { /* Nothing to do */ } #endif } else { #ifndef FULL_MISRA_C_COMPLIANCY_SPD_TORQ_CTRL //cstat !MISRAC2012-Rule-1.3_n !ATH-shift-neg !MISRAC2012-Rule-10.1_R6 hCurrentReference = (int16_t)(pHandle->SpeedRefUnitExt >> 16); #else hCurrentReference = (int16_t)(pHandle->SpeedRefUnitExt / 65536); #endif #ifdef CHECK_BOUNDARY if ((int32_t)hTargetFinal > (int32_t)pHandle->MaxAppPositiveMecSpeedUnit) { allowedRange = false; } else if (hTargetFinal < pHandle->MinAppNegativeMecSpeedUnit) { allowedRange = false; } else if ((int32_t)hTargetFinal < (int32_t)pHandle->MinAppPositiveMecSpeedUnit) { if (hTargetFinal > pHandle->MaxAppNegativeMecSpeedUnit) { allowedRange = false; } } else { /* Nothing to do */ } #endif } if (true == allowedRange) { /* Interrupts the execution of any previous ramp command */ if (0U == hDurationms) { if (MCM_SPEED_MODE == pHandle->Mode) { pHandle->SpeedRefUnitExt = ((int32_t)hTargetFinal) * 65536; } else { pHandle->TorqueRef = ((int32_t)hTargetFinal) * 65536; } pHandle->RampRemainingStep = 0U; pHandle->IncDecAmount = 0; } else { /* Store the hTargetFinal to be applied in the last step */ pHandle->TargetFinal = hTargetFinal; /* Compute the (wRampRemainingStep) number of steps remaining to complete the ramp */ wAux = ((uint32_t)hDurationms) * ((uint32_t)pHandle->STCFrequencyHz); wAux /= 1000U; pHandle->RampRemainingStep = wAux; pHandle->RampRemainingStep++; /* Compute the increment/decrement amount (wIncDecAmount) to be applied to the reference value at each CalcTorqueReference */ wAux1 = (((int32_t)hTargetFinal) - ((int32_t)hCurrentReference)) * 65536; wAux1 /= ((int32_t)pHandle->RampRemainingStep); pHandle->IncDecAmount = wAux1; } } #ifdef NULL_PTR_CHECK_SPD_TRQ_CTL } #endif return (allowedRange); } /** * @brief Interrupts the execution of any previous ramp command in particular by clearing * the number of steps remaining to complete the ramp * @ref SpeednTorqCtrl_Handle_t::RampRemainingStep "RampRemainingStep". * @param pHandle: handler of the current instance of the SpeednTorqCtrl component. * @retval none * * - If STC has been set in Torque mode the last value of Iq is maintained.\n * - If STC has been set in Speed mode the last value of mechanical * rotor speed reference is maintained. * - Called by MCI_StopSpeedRamp execution command. */ __weak void STC_StopRamp(SpeednTorqCtrl_Handle_t *pHandle) { #ifdef NULL_PTR_CHECK_SPD_TRQ_CTL if (MC_NULL == pHandle) { /* Nothing to do */ } else { #endif pHandle->RampRemainingStep = 0U; pHandle->IncDecAmount = 0; #ifdef NULL_PTR_CHECK_SPD_TRQ_CTL } #endif } /** * @brief Computes the new value of motor torque reference. * @param pHandle: handler of the current instance of the SpeednTorqCtrl component. * @retval int16_t motor torque reference. This value represents actually the * Iq current expressed in digit. * To convert current expressed in Amps to current expressed in digit * is possible to use the formula:\n * Current(digit) = [Current(Amp) * 65536 * Rshunt * Aop] / Vdd micro * * - Must be called at fixed time equal to hSTCFrequencyHz. It is called * passing as parameter the speed sensor used to perform the speed regulation. * - Called during START and ALIGNEMENT states of the MC state machine into MediumFrequencyTask. */ __weak int16_t STC_CalcTorqueReference(SpeednTorqCtrl_Handle_t *pHandle) { int16_t hTorqueReference; #ifdef NULL_PTR_CHECK_SPD_TRQ_CTL if (MC_NULL == pHandle) { hTorqueReference = 0; } else { #endif int32_t wCurrentReference; int16_t hMeasuredSpeed; int16_t hTargetSpeed; int16_t hError; if (MCM_TORQUE_MODE == pHandle->Mode) { wCurrentReference = pHandle->TorqueRef; } else { wCurrentReference = pHandle->SpeedRefUnitExt; } /* Update the speed reference or the torque reference according to the mode and terminates the ramp if needed */ if (pHandle->RampRemainingStep > 1U) { /* Increment/decrement the reference value */ wCurrentReference += pHandle->IncDecAmount; /* Decrement the number of remaining steps */ pHandle->RampRemainingStep--; } else if (1U == pHandle->RampRemainingStep) { /* Set the backup value of hTargetFinal */ wCurrentReference = ((int32_t)pHandle->TargetFinal) * 65536; pHandle->RampRemainingStep = 0U; } else { /* Do nothing */ } if (MCM_SPEED_MODE == pHandle->Mode) { /* Run the speed control loop */ /* Compute speed error */ #ifndef FULL_MISRA_C_COMPLIANCY_SPD_TORQ_CTRL //cstat !MISRAC2012-Rule-1.3_n !ATH-shift-neg !MISRAC2012-Rule-10.1_R6 hTargetSpeed = (int16_t)(wCurrentReference >> 16); #else hTargetSpeed = (int16_t)(wCurrentReference / 65536); #endif hMeasuredSpeed = SPD_GetAvrgMecSpeedUnit(pHandle->SPD); hError = hTargetSpeed - hMeasuredSpeed; hTorqueReference = PI_Controller(pHandle->PISpeed, (int32_t)hError); pHandle->SpeedRefUnitExt = wCurrentReference; pHandle->TorqueRef = ((int32_t)hTorqueReference) * 65536; } else { pHandle->TorqueRef = wCurrentReference; #ifndef FULL_MISRA_C_COMPLIANCY_SPD_TORQ_CTRL //cstat !MISRAC2012-Rule-1.3_n !ATH-shift-neg !MISRAC2012-Rule-10.1_R6 hTorqueReference = (int16_t)(wCurrentReference >> 16); #else hTorqueReference = (int16_t)(wCurrentReference / 65536); #endif } #ifdef NULL_PTR_CHECK_SPD_TRQ_CTL } #endif return (hTorqueReference); } /** * @brief Gets the Default mechanical rotor speed reference * @ref SpeednTorqCtrl_Handle_t::MecSpeedRefUnitDefault "MecSpeedRefUnitDefault" expressed in * the unit defined by [SPEED_UNIT](measurement_units.md). * @param pHandle: handler of the current instance of the SpeednTorqCtrl component. * @retval int16_t Default mechanical rotor speed. * * - It is the first command to STC after the start of speed ramp execution. * - Called during the boot phase of the MC process. */ //cstat !MISRAC2012-Rule-8.13 __weak int16_t STC_GetMecSpeedRefUnitDefault(SpeednTorqCtrl_Handle_t *pHandle) { #ifdef NULL_PTR_CHECK_SPD_TRQ_CTL return ((MC_NULL == pHandle) ? 0 : pHandle->MecSpeedRefUnitDefault); #else return (pHandle->MecSpeedRefUnitDefault); #endif } /** * @brief Returns the Application maximum positive value of rotor speed * @ref SpeednTorqCtrl_Handle_t::MaxAppPositiveMecSpeedUnit "MaxAppPositiveMecSpeedUnit". * Expressed in the unit defined by [SPEED_UNIT](measurement_units.md). * @param pHandle: handler of the current instance of the SpeednTorqCtrl component. * * - Not used into current implementation. */ //cstat !MISRAC2012-Rule-8.13 __weak uint16_t STC_GetMaxAppPositiveMecSpeedUnit(SpeednTorqCtrl_Handle_t *pHandle) { #ifdef NULL_PTR_CHECK_SPD_TRQ_CTL return ((MC_NULL == pHandle) ? 0U : pHandle->MaxAppPositiveMecSpeedUnit); #else return (pHandle->MaxAppPositiveMecSpeedUnit); #endif } /** * @brief Returns the Application minimum negative value of rotor speed * @ref SpeednTorqCtrl_Handle_t::MinAppNegativeMecSpeedUnit "MinAppNegativeMecSpeedUnit". * Expressed in the unit defined by [SPEED_UNIT](measurement_units.md). * @param pHandle: handler of the current instance of the SpeednTorqCtrl component. * * - Not used into current implementation. */ //cstat !MISRAC2012-Rule-8.13 __weak int16_t STC_GetMinAppNegativeMecSpeedUnit(SpeednTorqCtrl_Handle_t *pHandle) { #ifdef NULL_PTR_CHECK_SPD_TRQ_CTL return ((MC_NULL == pHandle) ? 0 : pHandle->MinAppNegativeMecSpeedUnit); #else return (pHandle->MinAppNegativeMecSpeedUnit); #endif } /** * @brief Checks if the settled speed or torque ramp has been completed by checking zero value of * @ref SpeednTorqCtrl_Handle_t::RampRemainingStep "RampRemainingStep". * @param pHandle: handler of the current instance of the SpeednTorqCtrl component. * @retval bool returning true if the ramp is completed, false otherwise. * * - Called during motor profiler tuning of HALL sensor. */ //cstat !MISRAC2012-Rule-8.13 __weak bool STC_RampCompleted(SpeednTorqCtrl_Handle_t *pHandle) { bool retVal = false; #ifdef NULL_PTR_CHECK_SPD_TRQ_CTL if (MC_NULL == pHandle) { /* Nothing to do */ } else { #endif if (0U == pHandle->RampRemainingStep) { retVal = true; } #ifdef NULL_PTR_CHECK_SPD_TRQ_CTL } #endif return (retVal); } /** * @brief Stops the execution of speed ramp by clearing the number of steps remaining to complete the ramp. * @param pHandle: handler of the current instance of the SpeednTorqCtrl component. * @retval bool returning true if the command is executed, false otherwise. * * - Not used into current implementation. */ __weak bool STC_StopSpeedRamp(SpeednTorqCtrl_Handle_t *pHandle) { bool retVal = false; #ifdef NULL_PTR_CHECK_SPD_TRQ_CTL if (MC_NULL == pHandle) { /* Nothing to do */ } else { #endif if (MCM_SPEED_MODE == pHandle->Mode) { pHandle->RampRemainingStep = 0u; retVal = true; } #ifdef NULL_PTR_CHECK_SPD_TRQ_CTL } #endif return (retVal); } /** * @brief Returns the default values of Iqdref. * @param pHandle: handler of the current instance of the SpeednTorqCtrl component. * @retval default values of Iqdref. * * - Called during the boot phase of the MC process. */ //cstat !MISRAC2012-Rule-8.13 __weak qd_t STC_GetDefaultIqdref(SpeednTorqCtrl_Handle_t *pHandle) { qd_t IqdRefDefault; #ifdef NULL_PTR_CHECK_SPD_TRQ_CTL if (MC_NULL == pHandle) { IqdRefDefault.q = 0; IqdRefDefault.d = 0; } else { #endif IqdRefDefault.q = pHandle->TorqueRefDefault; IqdRefDefault.d = pHandle->IdrefDefault; #ifdef NULL_PTR_CHECK_SPD_TRQ_CTL } #endif return (IqdRefDefault); } /** * @brief Changes the nominal current by setting new values of * @ref SpeednTorqCtrl_Handle_t::MaxPositiveTorque "MaxPositiveTorque" and * @ref SpeednTorqCtrl_Handle_t::MinNegativeTorque "MinNegativeTorque". * @param pHandle: handler of the current instance of the SpeednTorqCtrl component. * @param hNominalCurrent: represents actually the maximum Iq current expressed in digit. * @retval none * * - Not used into current implementation. */ __weak void STC_SetNominalCurrent(SpeednTorqCtrl_Handle_t *pHandle, uint16_t hNominalCurrent) { #ifdef NULL_PTR_CHECK_SPD_TRQ_CTL if (MC_NULL == pHandle) { /* Nothing to do */ } else { #endif pHandle->MaxPositiveTorque = hNominalCurrent; pHandle->MinNegativeTorque = -(int16_t)hNominalCurrent; #ifdef NULL_PTR_CHECK_SPD_TRQ_CTL } #endif } /** * @brief Forces the speed reference * @ref SpeednTorqCtrl_Handle_t::SpeedRefUnitExt "SpeedRefUnitExt" to the current speed. * @param pHandle: handler of the current instance of the SpeednTorqCtrl component. * @retval none * * - Called during the CHARGE_BOOT_CAP, SWITCH_OVER and WAIT_STOP_MOTOR states of the MC state machine * into MediumFrequencyTask to initialize the speed reference. */ __weak void STC_ForceSpeedReferenceToCurrentSpeed(SpeednTorqCtrl_Handle_t *pHandle) { #ifdef NULL_PTR_CHECK_SPD_TRQ_CTL if (MC_NULL == pHandle) { /* Nothing to do */ } else { #endif pHandle->SpeedRefUnitExt = ((int32_t)SPD_GetAvrgMecSpeedUnit(pHandle->SPD)) * (int32_t)65536; #ifdef NULL_PTR_CHECK_SPD_TRQ_CTL } #endif } /** * @} */ /** * @} */ /** * @} */ /************************ (C) COPYRIGHT 2023 STMicroelectronics *****END OF FILE****/
24,016
C
31.151272
109
0.671386
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/MCSDK_v6.2.0-Full/MotorControl/MCSDK/MCLib/Any/Src/pidreg_speed.c
/** ****************************************************************************** * @file pidreg_speed.c * @author Motor Control SDK Team, ST Microelectronics * @brief This file provides firmware functions that implement the features * of the PID speed regulator component of the Motor Control SDK * ****************************************************************************** * @attention * * <h2><center>&copy; Copyright (c) 2023 STMicroelectronics. * All rights reserved.</center></h2> * * This software component is licensed by ST under Ultimate Liberty license * SLA0044, the "License"; You may not use this file except in compliance with * the License. You may obtain a copy of the License at: * www.st.com/SLA0044 * ****************************************************************************** * @ingroup PIDRegSpeed */ #include "pidreg_speed.h" /** @addtogroup MCSDK * @{ */ /** * @defgroup PIDRegSpeed PID speed regulator * * @brief PID speed regulator component of the Motor Control SDK * * The PID speed regulator component implements the following control functions: * * * A proportional-integral controller, implemented by the PIDREG_SPEED2_run() function: * * ![](pidreg_speed.png) * * * Input: * * `Speed_user_ref`: Electrical reference speed (in Hz) from user * * `?`: Electrical speed from delta angle (low pass filtered) * * `?Emf`: Electrical speed from angular speed minus correction signal (low pass filtered) * * Output: * * `I_q_ref`: Iq current reference * * Note: * Input `err` represents the speed error without zest-correction and is used for proportional gain Kp only. Without zest-correction in the feedback signal, higher proportional gains can be used while keeping the system stable. * Input `errIncSpd` is the discrete derivative of the estimated angle that also includes the zest-correction. While standing still, zest will do something to compensate for any system imperfections and offsets, hence `err` will * contain a non-zero signal that represents a non-zero speed. Hence a corrected angle is required for integral speed (position) control, otherwise position-drift will occur. * In case a Ki is commanded, the shaft should act as a mechanical torsional spring. Standstill should result in a fixed position without drift at any constant load. Hence `errIncSpd` is used for the integration part. * * Each of the gain parameters, can be set, at run time and independently, via the PIDREG_SPEED_setKp_si(), * PIDREG_SPEED_setKp_si(). * * A PID Speed Regulator component needs to be initialized before it can be used. This is done with the PIDREG_SPEED_init() * function that sets the intergral term to 0 and initializes the component data structure. * * To keep the computed values within limits, the component features the possibility to constrain the integral term * within a range of values bounded by the PIDREG_SPEED_setOutputLimits() function. * * Handling a process with a PID Controller may require some adjustment to cope with specific situations. To that end, the * PID speed regulator component provides functions to set the integral term (PIDREG_SPEED_setUi_pu()). * * * @{ */ #define PU_FMT (30) /*!< @brief Per unit format, external format */ #define SUM_FTM (24) /*!< @brief Summing format, internal format */ #define HALF_BIT ((1L << (PU_FMT-SUM_FTM-1))-1) /*!< @brief equal to 2^(PU_FMT-SUM_FTM -1) -1 = 32-1 = 31 in standard case*/ /** * @brief Initializes PID speed regulator component. * * It Should be called during Motor control middleware initialization. * @param pHandle PID current regulator handler * @param current_scale current scaling factor * @param frequency_scale frequency scaling factor * @param pid_freq_PID regulator execution frequency */ void PIDREG_SPEED_init( PIDREG_SPEED_s* pHandle, const float current_scale, const float frequency_scale, const float pid_freq_hz ) { pHandle->Kp_fps.value = 0; pHandle->Kp_fps.fixpFmt = 30; pHandle->Ki_fps.value = 0; pHandle->Ki_fps.fixpFmt = 30; pHandle->Max = FIXP24(0.7f); pHandle->Min = FIXP24(-0.7f); pHandle->Up = FIXP24(0.0f); pHandle->Ui = FIXP24(0.0f); pHandle->Out = FIXP30(0.0f); pHandle->dither = 0; pHandle->current_scale = current_scale; pHandle->frequency_scale = frequency_scale; pHandle->pid_freq_hz = pid_freq_hz; } /** * @brief Computes the output of a PID speed regulator component, sum of its proportional * and integral terms * @param pHandle PID current regulator handler * @param err speed error */ fixp30_t PIDREG_SPEED_run( PIDREG_SPEED_s* pHandle, const fixp30_t err ) { fixp24_t max = pHandle->Max; fixp24_t min = pHandle->Min; fixp24_t ui = pHandle->Ui; /* Error */ fixp24_t err_24 = (err >> (PU_FMT-SUM_FTM)); /* Proportional term */ fixp24_t up = FIXP_mpyFIXPscaled(err_24, &pHandle->Kp_fps); /* Integral term */ ui += FIXP_mpyFIXPscaled(err_24, &pHandle->Ki_fps); ui = FIXP_sat(ui, max, min); pHandle->Ui = ui; fixp24_t sum = FIXP_sat(up + ui, max, min); fixp30_t out = (sum << (PU_FMT-SUM_FTM)); /* Store values for monitoring */ pHandle->Err = err_24; pHandle->Up = up; pHandle->Out = out; return (out); } #if defined (CCMRAM) #if defined (__ICCARM__) #pragma location = ".ccmram" #elif defined (__CC_ARM) || defined(__GNUC__) __attribute__((section (".ccmram"))) #endif #endif /** * @brief Computes the output of a PID speed regulator component, sum of its proportional * and integral terms * @param pHandle PID current regulator handler * @param err speed error * @param err speed error */ fixp30_t PIDREG_SPEED2_run( PIDREG_SPEED_s* pHandle, const fixp30_t err, const fixp30_t errIncSpd) { fixp24_t max = pHandle->Max; fixp24_t min = pHandle->Min; fixp24_t ui = pHandle->Ui; /* Error */ fixp24_t err_24 = ((err + HALF_BIT) >> (PU_FMT-SUM_FTM)); fixp24_t errIncSpd_24 = ((errIncSpd + HALF_BIT) >> (PU_FMT-SUM_FTM)); /* Proportional term */ fixp24_t up = FIXP_mpyFIXPscaled(err_24, &pHandle->Kp_fps); /* Integral term */ pHandle->dither = (pHandle->dither + 1) & 1; ui += FIXP_mpyFIXPscaled(errIncSpd_24, &pHandle->Ki_fps) + pHandle->dither; pHandle->clipped = ((ui <= min) || (ui >= max)); ui = FIXP_sat(ui, max, min); pHandle->Ui = ui; fixp24_t sum = FIXP_sat(up + ui, max, min); fixp30_t out = (sum << (PU_FMT-SUM_FTM)); /* Store values for monitoring */ pHandle->Err = err_24; pHandle->Up = up; pHandle->Out = out; return (out); } /** * @brief Gets overall clipping status * @param pHandle PID current regulator handler * @retval bool clipping status */ bool PIDREG_SPEED_getClipped( PIDREG_SPEED_s* pHandle ) { return pHandle->clipped; } /* end of PIDREG_SPEED_getClipped( PIDREG_SPEED_s* pHandle )*/ /** * @brief Gets Kp gain in SI unit * @param pHandle PID speed regulator handler * @retval float Kp gain in SI unit */ float PIDREG_SPEED_getKp_si( PIDREG_SPEED_s* pHandle ) { float Kp_pu = FIXPSCALED_FIXPscaledToFloat(&pHandle->Kp_fps); float Kp_si = Kp_pu / pHandle->frequency_scale * pHandle->current_scale; return Kp_si; } /** * @brief Gets Ki gain in SI unit * @param pHandle PID speed regulator handler * @retval float Ki gain in SI unit */ float PIDREG_SPEED_getKi_si( PIDREG_SPEED_s* pHandle ) { float Ki_pu = FIXPSCALED_FIXPscaledToFloat(&pHandle->Ki_fps); float Ki_si = Ki_pu / pHandle->frequency_scale * pHandle->current_scale * pHandle->pid_freq_hz; return Ki_si; } /** * @brief Sets Kp gain in SI unit * @param pHandle PID current regulator handler * @param Kp Kp gain */ void PIDREG_SPEED_setKp_si( PIDREG_SPEED_s* pHandle, const float Kp) { // Parameter Kp is in current per speed, A/Hz (electrical) // User may want to set using different scale, like A/rad*s^-1, that is done external to this function /* Convert to per unit, in full scale current per full scale frequency */ float Kp_pu = Kp * pHandle->frequency_scale / pHandle->current_scale; /* Convert Kp_pu to scaled value, and store */ FIXPSCALED_floatToFIXPscaled(Kp_pu, &pHandle->Kp_fps); } /** * @brief Sets Ki gain in SI unit * @param pHandle PID current regulator handler * @param Ki Ki gain */ void PIDREG_SPEED_setKi_si( PIDREG_SPEED_s* pHandle, const float Ki) { /* Parameter Ki is in current per angle, A/rad; */ /* Convert to per unit, in full scale voltage per full scale current per pid_freq_hz */ float Ki_pu = Ki * pHandle->frequency_scale / pHandle->current_scale / pHandle->pid_freq_hz; FIXPSCALED_floatToFIXPscaled(Ki_pu, &pHandle->Ki_fps); } /** * @brief Sets integral term * @param pHandle PID current regulator handler * @param Ui integral term */ void PIDREG_SPEED_setUi_pu( PIDREG_SPEED_s* pHandle, const fixp30_t Ui) { // Parameter Ui is in the same unit as the output, per unit duty // Internally the Ui is stored in a different format pHandle->Ui = (Ui >> (PU_FMT-SUM_FTM)); } /** * @brief Sets PID regulator output limits * @param pHandle PID current regulator handler * @param max_pu upper limit in per-unit * @param mix_pu lower limit in per unit */ void PIDREG_SPEED_setOutputLimits(PIDREG_SPEED_s* pHandle, const fixp30_t max_pu, const fixp30_t min_pu) { pHandle->Max = (max_pu >> (PU_FMT-SUM_FTM)); pHandle->Min = (min_pu >> (PU_FMT-SUM_FTM)); } /** * @brief Gets PID regulator maximum output limits * @param pHandle PID current regulator handler * @retval fixp30_t maximum output limits */ fixp30_t PIDREG_SPEED_getOutputLimitMax(PIDREG_SPEED_s* pHandle) { return ((pHandle->Max) << (PU_FMT-SUM_FTM)); } /** * @brief Gets PID regulator minimum output limits * @param pHandle PID current regulator handler * @retval fixp30_t minimum output limits */ fixp30_t PIDREG_SPEED_getOutputLimitMin(PIDREG_SPEED_s* pHandle) { return ((pHandle->Min) << (PU_FMT-SUM_FTM)); } /** * @} */ /** * @} */ /************************ (C) COPYRIGHT 2023 STMicroelectronics *****END OF FILE****/
10,194
C
31.78135
229
0.662841
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/MCSDK_v6.2.0-Full/MotorControl/MCSDK/MCLib/Any/Src/flux_weakening_ctrl.c
/** ****************************************************************************** * @file flux_weakening_ctrl.c * @author Motor Control SDK Team, ST Microelectronics * @brief This file provides firmware functions that implements the Flux Weakening * Control component of the Motor Control SDK. * ****************************************************************************** * @attention * * <h2><center>&copy; Copyright (c) 2023 STMicroelectronics. * All rights reserved.</center></h2> * * This software component is licensed by ST under Ultimate Liberty license * SLA0044, the "License"; You may not use this file except in compliance with * the License. You may obtain a copy of the License at: * www.st.com/SLA0044 * ****************************************************************************** * @ingroup FluxWeakeningCtrl */ /* Includes ------------------------------------------------------------------*/ #include "flux_weakening_ctrl.h" #include "mc_math.h" #include "mc_type.h" #include "pid_regulator.h" /** @addtogroup MCSDK * @{ */ /** @defgroup FluxWeakeningCtrl Flux Weakening Control * @brief Flux Weakening (FW) Control component of the Motor Control SDK * * The Flux Weakening Control component modifies Idq reference to reach a speed higher than rated one. * To do so it uses its own PID controller to control the current reference and acts also on the PID speed controller. * * Flux Weakening Control component needs to be initialized before it can be used. This is done with the FW_Init() * function. Ensure that PID speed has been correctly initialized prior to use flux weakiening component. * * The controller functions implemented by the FW_CalcCurrRef() functions is based on 16-bit integer arithmetics * The controller output values returned by this functions is also 16-bit integers. This makes it possible to use this * component efficiently on all STM2 MCUs. * * For more information, please refer to [Flux Weakening documentation](flux_weakening_control.md) * * @{ */ /** * @brief Initializes flux weakening component handler, it should be called * once during Motor Control initialization. * @param pHandle pointer to flux weakening component handler. * @param pPIDSpeed pointer to speed PID strutcture. * @param PIDFluxWeakeningHandle pointer to FW PID strutcture. */ __weak void FW_Init(FW_Handle_t *pHandle, PID_Handle_t *pPIDSpeed, PID_Handle_t *pPIDFluxWeakeningHandle) { #ifdef NULL_PTR_CHECK_FLUX_WEAK if (NULL == pHandle) { /* Mothing to do */ } else { #endif pHandle->hFW_V_Ref = pHandle->hDefaultFW_V_Ref; pHandle->pFluxWeakeningPID = pPIDFluxWeakeningHandle; pHandle->pSpeedPID = pPIDSpeed; #ifdef NULL_PTR_CHECK_FLUX_WEAK } #endif } /** * @brief Clears the Flux weakening internal variables except the target * voltage (hFW_V_Ref). It should be called before each motor restart * @param pHandle pointer to flux weakening component handler. */ __weak void FW_Clear(FW_Handle_t *pHandle) { qd_t V_null = {(int16_t)0, (int16_t)0}; #ifdef NULL_PTR_CHECK_FLUX_WEAK if (NULL == pHandle) { /* Mothing to do */ } else { #endif PID_SetIntegralTerm(pHandle->pFluxWeakeningPID, (int32_t)0); pHandle->AvVolt_qd = V_null; pHandle->AvVoltAmpl = (int16_t)0; pHandle->hIdRefOffset = (int16_t)0; #ifdef NULL_PTR_CHECK_FLUX_WEAK } #endif } /** * @brief Computes Iqdref according to the flux weakening algorithm. * As soon as the speed increases beyond the nominal one, flux weakening * algorithm takes place and handles Idref value. Finally, accordingly * with new Idref, a new Iqref saturation value is also computed and * put into speed PI. this routine should be called during background task. * @param pHandle pointer to flux weakening component handler. * @param Iqdref current reference that will be * modified, if needed, by the flux weakening algorithm. * @retval qd_t Computed Iqdref. */ __weak qd_t FW_CalcCurrRef(FW_Handle_t *pHandle, qd_t Iqdref) { qd_t IqdrefRet; #ifdef NULL_PTR_CHECK_FLUX_WEAK if (NULL == pHandle) { IqdrefRet.d = 0; IqdrefRet.q = 0; } else { #endif int32_t wIdRef; int32_t wIqSatSq; int32_t wIqSat; int32_t wAux1; int16_t Aux2; uint32_t wVoltLimit_Ref; int16_t hId_fw; /* Computation of the Id contribution coming from flux weakening algorithm */ wVoltLimit_Ref = ((uint32_t)(pHandle->hFW_V_Ref) * pHandle->hMaxModule) / 1000U; Aux2 = MCM_Modulus( pHandle->AvVolt_qd.q, pHandle->AvVolt_qd.d ); pHandle->AvVoltAmpl = Aux2; hId_fw = PI_Controller(pHandle->pFluxWeakeningPID, (int32_t)wVoltLimit_Ref - (int32_t)Aux2); /* If the Id coming from flux weakening algorithm (Id_fw) is positive, keep unchanged Idref, otherwise sum it to last Idref available when Id_fw was zero */ if (hId_fw >= (int16_t)0) { pHandle->hIdRefOffset = Iqdref.d; wIdRef = (int32_t)Iqdref.d; } else { wIdRef = (int32_t)pHandle->hIdRefOffset + hId_fw; } /* Saturate new Idref to prevent the rotor from being demagnetized */ if (wIdRef < pHandle->hDemagCurrent) { wIdRef = pHandle->hDemagCurrent; } else { /* Nothing to do */ } IqdrefRet.d = (int16_t)wIdRef; /* New saturation for Iqref */ wIqSatSq = pHandle->wNominalSqCurr - (wIdRef * wIdRef); wIqSat = MCM_Sqrt(wIqSatSq); /* Iqref saturation value used for updating integral term limitations of speed PI */ wAux1 = wIqSat * (int32_t)PID_GetKIDivisor(pHandle->pSpeedPID); PID_SetLowerIntegralTermLimit(pHandle->pSpeedPID, -wAux1); PID_SetUpperIntegralTermLimit(pHandle->pSpeedPID, wAux1); /* Iqref saturation value used for updating integral term limitations of speed PI */ if (Iqdref.q > wIqSat) { IqdrefRet.q = (int16_t)wIqSat; } else if (Iqdref.q < -wIqSat) { IqdrefRet.q = -(int16_t)wIqSat; } else { IqdrefRet.q = Iqdref.q; } #ifdef NULL_PTR_CHECK_FLUX_WEAK } #endif return (IqdrefRet); } //cstat #ATH-shift-bounds #if defined (CCMRAM) #if defined (__ICCARM__) #pragma location = ".ccmram" #elif defined (__CC_ARM) || defined(__GNUC__) __attribute__((section(".ccmram"))) #endif #endif /** * @brief Applies a low-pass filter on both Vqd voltage components. Filter * bandwidth depends on hVqdLowPassFilterBW parameter. It shall * be called during current controller task. * @param pHandle pointer to flux weakening component handler. * @param Vqd Voltage componets to be averaged. */ __weak void FW_DataProcess(FW_Handle_t *pHandle, qd_t Vqd) { #ifdef NULL_PTR_CHECK_FLUX_WEAK if (NULL == pHandle) { /* Nothing to do */ } else { #endif int32_t wAux; int32_t lowPassFilterBW = (int32_t)(pHandle->hVqdLowPassFilterBW) - (int32_t)1 ; #ifndef FULL_MISRA_C_COMPLIANCY_FLUX_WEAK wAux = (int32_t)(pHandle->AvVolt_qd.q) * lowPassFilterBW; wAux += Vqd.q; //cstat !MISRAC2012-Rule-1.3_n !ATH-shift-neg !MISRAC2012-Rule-10.1_R6 pHandle->AvVolt_qd.q = (int16_t)(wAux >> pHandle->hVqdLowPassFilterBWLOG); wAux = (int32_t)(pHandle->AvVolt_qd.d) * lowPassFilterBW; wAux += Vqd.d; //cstat !MISRAC2012-Rule-1.3_n !ATH-shift-neg !MISRAC2012-Rule-10.1_R6 pHandle->AvVolt_qd.d = (int16_t)(wAux >> pHandle->hVqdLowPassFilterBWLOG); #else wAux = (int32_t)(pHandle->AvVolt_qd.q) * lowPassFilterBW; wAux += Vqd.q; pHandle->AvVolt_qd.q = (int16_t)(wAux / (int32_t)(pHandle->hVqdLowPassFilterBW)); wAux = (int32_t)(pHandle->AvVolt_qd.d) * lowPassFilterBW; wAux += Vqd.d; pHandle->AvVolt_qd.d = (int16_t)(wAux / (int32_t)pHandle->hVqdLowPassFilterBW); #endif #ifdef NULL_PTR_CHECK_FLUX_WEAK } #endif } /** * @brief Sets a new value for the voltage reference used by * flux weakening algorithm. * @param pHandle pointer to flux weakening component handler. * @param hNewVref New target voltage value, expressed in tenth of percentage * points of available voltage. */ __weak void FW_SetVref(FW_Handle_t *pHandle, uint16_t hNewVref) { #ifdef NULL_PTR_CHECK_FLUX_WEAK if (NULL == pHandle) { /* Nothing to do */ } else { #endif pHandle->hFW_V_Ref = hNewVref; #ifdef NULL_PTR_CHECK_FLUX_WEAK } #endif } /** * @brief Returns the present value of target voltage used by flux * weakening algorihtm. * @param pHandle pointer to flux weakening component handler. * @retval int16_t Present target voltage value expressed in tenth of * percentage points of available voltage. */ __weak uint16_t FW_GetVref(FW_Handle_t *pHandle) //cstat !MISRAC2012-Rule-8.13 { #ifdef NULL_PTR_CHECK_FLUX_WEAK return ((NULL == pHandle) ? 0U : pHandle->hFW_V_Ref); #else return (pHandle->hFW_V_Ref); #endif } /** * @brief Returns the present value of voltage actually used by flux * weakening algorihtm. * @param pHandle pointer to flux weakening component handler. * @retval int16_t Present averaged phase stator voltage value, expressed * in s16V (0-to-peak), where * PhaseVoltage(V) = [PhaseVoltage(s16A) * Vbus(V)] /[sqrt(3) *32767]. */ __weak int16_t FW_GetAvVAmplitude(FW_Handle_t *pHandle) //cstat !MISRAC2012-Rule-8.13 { #ifdef NULL_PTR_CHECK_FLUX_WEAK return ((NULL == pHandle) ? 0 : pHandle->AvVoltAmpl); #else return (pHandle->AvVoltAmpl); #endif } /** * @brief Returns the present voltage actually used by flux * weakening algorihtm as percentage of available voltage. * @param pHandle pointer to flux weakening component handler. * @retval uint16_t Present averaged phase stator voltage value, expressed in * tenth of percentage points of available voltage. */ __weak uint16_t FW_GetAvVPercentage(FW_Handle_t *pHandle) //cstat !MISRAC2012-Rule-8.13 { #ifdef NULL_PTR_CHECK_FLUX_WEAK return ((NULL == pHandle) ? 0U : (uint16_t)((uint32_t)(pHandle->AvVoltAmpl) * 1000U / (uint32_t)(pHandle->hMaxModule))); #else return ((uint16_t)((uint32_t)(pHandle->AvVoltAmpl) * 1000U / (uint32_t)(pHandle->hMaxModule))); #endif } /** * @} */ /** * @} */ /************************ (C) COPYRIGHT 2023 STMicroelectronics *****END OF FILE****/
10,436
C
30.627273
121
0.650537
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/MCSDK_v6.2.0-Full/MotorControl/MCSDK/MCLib/Any/Src/pwm_common.c
/** ****************************************************************************** * @file pwm_common.c * @author Motor Control SDK Team, ST Microelectronics * @brief This file provides firmware functions that implement common features * of the PWM & Current Feedback component of the Motor Control SDK: * * * start timers (main and auxiliary) synchronously * ****************************************************************************** * @attention * * <h2><center>&copy; Copyright (c) 2023 STMicroelectronics. * All rights reserved.</center></h2> * * This software component is licensed by ST under Ultimate Liberty license * SLA0044, the "License"; You may not use this file except in compliance with * the License. You may obtain a copy of the License at: * www.st.com/SLA0044 * ****************************************************************************** * @ingroup pwm_curr_fdbk */ /* Includes ------------------------------------------------------------------*/ #include "pwm_common.h" /** @addtogroup MCSDK * @{ */ /** @addtogroup pwm_curr_fdbk * @{ */ #ifdef TIM2 /** * @brief Performs the start of all the timers required by the control. * * Uses TIM2 as a temporary timer to achieve synchronization between PWM signals. * When this function is called, TIM1 and/or TIM8 must be in a frozen state * with CNT, ARR, REP RATE and trigger correctly set (these settings are * usually performed in the Init method accordingly with the configuration) */ __weak void startTimers(void) { uint32_t isTIM2ClockOn; uint32_t trigOut; isTIM2ClockOn = LL_APB1_GRP1_IsEnabledClock(LL_APB1_GRP1_PERIPH_TIM2); if ((uint32_t)0 == isTIM2ClockOn) { /* Temporary Enable TIM2 clock if not already on */ LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM2); LL_TIM_GenerateEvent_UPDATE(TIM2); LL_APB1_GRP1_DisableClock(LL_APB1_GRP1_PERIPH_TIM2); } else { trigOut = LL_TIM_ReadReg(TIM2, CR2) & TIM_CR2_MMS; LL_TIM_SetTriggerOutput(TIM2, LL_TIM_TRGO_UPDATE); LL_TIM_GenerateEvent_UPDATE(TIM2); LL_TIM_SetTriggerOutput(TIM2, trigOut); } } #endif /** * @brief Waits for the end of the polarization. * * If the polarization exceeds the number of needed PWM cycles, it reports an error. * * @param TIMx Timer used to generate PWM. * @param SWerror Variable used to report a SW error. * @param repCnt Repetition counter value. * @param cnt Polarization counter value. */ //cstat !MISRAC2012-Rule-8.13 __weak void waitForPolarizationEnd(TIM_TypeDef *TIMx, uint16_t *SWerror, uint8_t repCnt, volatile uint8_t *cnt) { #ifdef NULL_PTR_CHECK_POW_COM if ((MC_NULL == cnt) || (MC_NULL == SWerror)) { /* Nothing to do */ } else { #endif uint16_t hCalibrationPeriodCounter; uint16_t hMaxPeriodsNumber; hMaxPeriodsNumber = ((uint16_t)2 * NB_CONVERSIONS) * (((uint16_t)repCnt + 1U) >> 1); /* Wait for NB_CONVERSIONS to be executed */ LL_TIM_ClearFlag_CC1(TIMx); hCalibrationPeriodCounter = 0u; while (*cnt < NB_CONVERSIONS) { if ((uint32_t)ERROR == LL_TIM_IsActiveFlag_CC1(TIMx)) { LL_TIM_ClearFlag_CC1(TIMx); hCalibrationPeriodCounter++; if (hCalibrationPeriodCounter >= hMaxPeriodsNumber) { if (*cnt < NB_CONVERSIONS) { *SWerror = 1u; break; } else { /* Nothing to do */ } } else { /* Nothing to do */ } } else { /* Nothing to do */ } } #ifdef NULL_PTR_CHECK_POW_COM } #endif } /** * @} */ /** * @} */ /************************ (C) COPYRIGHT 2023 STMicroelectronics *****END OF FILE****/
3,844
C
26.66187
112
0.567378
Tbarkin121/GuardDog/stm32/MotorDrive/LegDay/MCSDK_v6.2.0-Full/MotorControl/MCSDK/MCLib/Any/Src/trajectory_ctrl.c
/** ****************************************************************************** * @file trajectory_ctrl.c * @author Motor Control SDK Team, ST Microelectronics * @brief This file provides firmware functions that implements the Position Control * component of the Motor Control SDK. * ****************************************************************************** * @attention * * <h2><center>&copy; Copyright (c) 2023 STMicroelectronics. * All rights reserved.</center></h2> * * This software component is licensed by ST under Ultimate Liberty license * SLA0044, the "License"; You may not use this file except in compliance with * the License. You may obtain a copy of the License at: * www.st.com/SLA0044 * ****************************************************************************** * @ingroup PositionControl */ /* Includes ------------------------------------------------------------------*/ #include "trajectory_ctrl.h" #include "speed_pos_fdbk.h" /** @addtogroup MCSDK * @{ */ /** * @defgroup PositionControl Position Control * * @brief Position Control component of the Motor Control SDK * The Position Control component allows to control the movement of the motor in two different ways: * * * Trajectory control mode, implemented by the TC_MoveCommand() function: allows to move the motor to a specified * target mechanical position in a settled time (duration) following a programmed trajectory composed of three phases: * 1- acceleration, 2- rotation at constant speed and 3- deceleration. * * Follow mode, implemented by the TC_FollowCommand() function: This mode is for instance useful when the trajectory is * computed by an external controller, or by an algorithm defined by the user. * The user can send at fixed rate, different target positions according to a required trajectory and the position control * algorithm computes the intermediate points to reach (follow) the target with a smooth movement. * * The position controller uses a PID (with a proportional, integral and derivative action) to regulate the angular position. * * @{ */ /** * @brief Initializes the handle of position control. * @param pHandle handler of the current instance of the Position Control component. * @param pPIDPosReg pointer on the handler of the current instance of PID used for the position regulation. * @param pSTC pointer on the handler of the current instance of the SpeednTorqCtrl component. * @param pENC handler of the current instance of the EncAlignCtrl component. */ void TC_Init(PosCtrl_Handle_t *pHandle, PID_Handle_t *pPIDPosReg, SpeednTorqCtrl_Handle_t *pSTC, ENCODER_Handle_t *pENC) { pHandle->MovementDuration = 0.0f; pHandle->AngleStep = 0.0f; pHandle->SubStep[0] = 0.0f; pHandle->SubStep[1] = 0.0f; pHandle->SubStep[2] = 0.0f; pHandle->SubStep[3] = 0.0f; pHandle->SubStep[4] = 0.0f; pHandle->SubStep[5] = 0.0f; pHandle->SubStepDuration = 0; pHandle->Jerk = 0.0f; pHandle->CruiseSpeed = 0.0f; pHandle->Acceleration = 0.0f; pHandle->Omega = 0.0f; pHandle->OmegaPrev = 0.0f; pHandle->Theta = 0.0f; pHandle->ThetaPrev = 0.0f; pHandle->ReceivedTh = 0.0f; pHandle->TcTick = 0; pHandle->ElapseTime = 0.0f; pHandle->PositionControlRegulation = DISABLE; pHandle->PositionCtrlStatus = TC_READY_FOR_COMMAND; pHandle->pENC = pENC; pHandle->pSTC = pSTC; pHandle->PIDPosRegulator = pPIDPosReg; pHandle->MecAngleOffset = 0; } /** * @brief Configures the trapezoidal speed trajectory. * @param pHandle handler of the current instance of the Position Control component. * @param startingAngle Current mechanical position. * @param angleStep Target mechanical position. * @param movementDuration Duration to reach the final position (in seconds). * @retval ConfigurationStatus set to true when Trajectory command is programmed * otherwise not yet ready for a new trajectory configuration. * * This function implements the Trajectory Control mode. When fDuration is different from 0, * the trajectory of the movement, and therefore its acceleration and speed, are computed. * */ bool TC_MoveCommand(PosCtrl_Handle_t *pHandle, float startingAngle, float angleStep, float movementDuration) { bool RetConfigStatus = false; float fMinimumStepDuration; if ((pHandle->PositionCtrlStatus == TC_FOLLOWING_ON_GOING) && (movementDuration > 0)) { /* Back to Move command as the movement duration is different from 0 */ pHandle->PositionCtrlStatus = TC_READY_FOR_COMMAND; } else { /* Nothing to do */ } if ((pHandle->PositionCtrlStatus == TC_READY_FOR_COMMAND) && (movementDuration > 0)) { pHandle->PositionControlRegulation = ENABLE; fMinimumStepDuration = (9.0f * pHandle->SamplingTime); /* WARNING: Movement duration value is rounded to the nearest valid value [(DeltaT/9) / SamplingTime]: shall be an integer value */ pHandle->MovementDuration = (float)((int)(movementDuration / fMinimumStepDuration)) * fMinimumStepDuration; pHandle->StartingAngle = startingAngle; pHandle->AngleStep = angleStep; pHandle->FinalAngle = startingAngle + angleStep; /* SubStep duration = DeltaT/9 (DeltaT represents the total duration of the programmed movement) */ pHandle->SubStepDuration = pHandle->MovementDuration / 9.0f; /* Sub step of acceleration phase */ pHandle->SubStep[0] = 1 * pHandle->SubStepDuration; /* Sub-step 1 of acceleration phase */ pHandle->SubStep[1] = 2 * pHandle->SubStepDuration; /* Sub-step 2 of acceleration phase */ pHandle->SubStep[2] = 3 * pHandle->SubStepDuration; /* Sub-step 3 of acceleration phase */ /* Sub step of deceleration Phase */ pHandle->SubStep[3] = 6 * pHandle->SubStepDuration; /* Sub-step 1 of deceleration phase */ pHandle->SubStep[4] = 7 * pHandle->SubStepDuration; /* Sub-step 2 of deceleration phase */ pHandle->SubStep[5] = 8 * pHandle->SubStepDuration; /* Sub-step 3 of deceleration phase */ /* Jerk (J) to be used by the trajectory calculator to integrate (step by step) the target position. J = DeltaTheta/(12 * A * A * A) => DeltaTheta = final position and A = Sub-Step duration */ pHandle->Jerk = pHandle->AngleStep / (12 * pHandle->SubStepDuration * pHandle->SubStepDuration * pHandle->SubStepDuration); /* Speed cruiser = 2*J*A*A) */ pHandle->CruiseSpeed = 2 * pHandle->Jerk * pHandle->SubStepDuration * pHandle->SubStepDuration; pHandle->ElapseTime = 0.0f; pHandle->Omega = 0.0f; pHandle->Acceleration = 0.0f; pHandle->Theta = startingAngle; pHandle->PositionCtrlStatus = TC_MOVEMENT_ON_GOING; /* new trajectory has been programmed */ RetConfigStatus = true; } else { /* Nothing to do */ } return (RetConfigStatus); } /** * @brief Follows an angular position command. * @param pHandle handler of the current instance of the Position Control component. * @param Angle Target mechanical position. * * This function implements the Follow mode. When the duration is set to zero, the user can send at * fixed rate different target positions according to a required trajectory and the position control * algorithm computes the intermediate points to reach (follow) the target with a smooth movement. * This mode is for instance useful when the trajectory is computed by an external controller, or by * an algorithm defined by the user and executed by the same microcontroller. * */ void TC_FollowCommand(PosCtrl_Handle_t *pHandle, float Angle) { float omega = 0, acceleration = 0, dt = 0; /* Estimate speed */ if (pHandle->ReceivedTh > 0) { /* Calculate dt */ dt = pHandle->TcTick * pHandle->SysTickPeriod; pHandle->TcTick = 0; if (dt > 0) { omega = (Angle - pHandle->ThetaPrev) / dt; } else { /* Nothing to do */ } } else { /* Nothing to do */ } /* Estimated acceleration */ if (pHandle->ReceivedTh > 1) { if (dt > 0) { acceleration = (omega - pHandle->OmegaPrev) / dt; } else { /* nothing to do */ } } else { /* Nothing to do */ } /* Update state variable */ pHandle->ThetaPrev = Angle; pHandle->OmegaPrev = omega; if (pHandle->ReceivedTh < 2) { pHandle->ReceivedTh++; } else { /* Nothing to do */ } pHandle->Acceleration = acceleration; pHandle->Omega = omega; pHandle->Theta = Angle; pHandle->PositionCtrlStatus = TC_FOLLOWING_ON_GOING; /* follow mode has been programmed */ pHandle->MovementDuration = 0; } /** * @brief Proceeds on the position control loop. * @param pHandle: handler of the current instance of the Position Control component. */ void TC_PositionRegulation(PosCtrl_Handle_t *pHandle) { int32_t wMecAngleRef; int32_t wMecAngle; int32_t wError; int32_t hTorqueRef_Pos; if (pHandle->PositionCtrlStatus == TC_MOVEMENT_ON_GOING) { TC_MoveExecution(pHandle); } else { /* Nothing to do */ } if (pHandle->PositionCtrlStatus == TC_FOLLOWING_ON_GOING) { TC_FollowExecution(pHandle); } else { /* Nothing to do */ } if (pHandle->PositionControlRegulation == ENABLE) { wMecAngleRef = (int32_t)(pHandle->Theta * RADTOS16); wMecAngle = SPD_GetMecAngle(STC_GetSpeedSensor(pHandle->pSTC)); wError = wMecAngleRef - wMecAngle; hTorqueRef_Pos = PID_Controller(pHandle->PIDPosRegulator, wError); STC_SetControlMode(pHandle->pSTC, MCM_TORQUE_MODE); STC_ExecRamp(pHandle->pSTC, hTorqueRef_Pos, 0); } else { /* Nothing to do */ } } /** * @brief Executes the programmed trajectory movement. * @param pHandle handler of the current instance of the Position Control component. */ void TC_MoveExecution(PosCtrl_Handle_t *pHandle) { float jerkApplied = 0; if (pHandle->ElapseTime < pHandle->SubStep[0]) /* 1st Sub-Step interval time of acceleration phase */ { jerkApplied = pHandle->Jerk; } else if (pHandle->ElapseTime < pHandle->SubStep[1]) /* 2nd Sub-Step interval time of acceleration phase */ { } else if (pHandle->ElapseTime < pHandle->SubStep[2]) /* 3rd Sub-Step interval time of acceleration phase */ { jerkApplied = -(pHandle->Jerk); } else if (pHandle->ElapseTime < pHandle->SubStep[3]) /* Speed Cruise phase (after acceleration and before deceleration phases) */ { pHandle->Acceleration = 0.0f; pHandle->Omega = pHandle->CruiseSpeed; } else if (pHandle->ElapseTime < pHandle->SubStep[4]) /* 1st Sub-Step interval time of deceleration phase */ { jerkApplied = -(pHandle->Jerk); } else if (pHandle->ElapseTime < pHandle->SubStep[5]) /* 2nd Sub-Step interval time of deceleration phase */ { } else if (pHandle->ElapseTime < pHandle->MovementDuration) /* 3rd Sub-Step interval time of deceleration phase */ { jerkApplied = pHandle->Jerk; } else { pHandle->Theta = pHandle->FinalAngle; pHandle->PositionCtrlStatus = TC_TARGET_POSITION_REACHED; } if (TC_MOVEMENT_ON_GOING == pHandle->PositionCtrlStatus) { pHandle->Acceleration += jerkApplied * pHandle->SamplingTime; pHandle->Omega += pHandle->Acceleration * pHandle->SamplingTime; pHandle->Theta += pHandle->Omega * pHandle->SamplingTime; } else { /* Nothing to do */ } pHandle->ElapseTime += pHandle->SamplingTime; if (TC_RampCompleted(pHandle)) { if (TC_ZERO_ALIGNMENT_START == pHandle->AlignmentStatus) { /* Ramp is used to search the zero index, if completed there is no z signal */ pHandle->AlignmentStatus = TC_ALIGNMENT_ERROR; } else { /* Nothing to do */ } pHandle->ElapseTime = 0; pHandle->PositionCtrlStatus = TC_READY_FOR_COMMAND; } else { /* Nothing to do */ } } /** * @brief Updates the angular position. * @param pHandle handler of the current instance of the Position Control component. */ void TC_FollowExecution(PosCtrl_Handle_t *pHandle) { pHandle->Omega += pHandle->Acceleration * pHandle->SamplingTime; pHandle->Theta += pHandle->Omega * pHandle->SamplingTime; } /** * @brief Handles the alignment phase at starting before any position commands. * @param pHandle: handler of the current instance of the Position Control component. */ void TC_EncAlignmentCommand(PosCtrl_Handle_t *pHandle) { int32_t wMecAngleRef; if (TC_ALIGNMENT_COMPLETED == pHandle->AlignmentStatus) { pHandle->PositionCtrlStatus = TC_READY_FOR_COMMAND; /* Do nothing - EncAlignment must be done only one time after the power on */ } else { if (pHandle->AlignmentCfg == TC_ABSOLUTE_ALIGNMENT_SUPPORTED) { /* If index is supported start the search of the zero */ pHandle->EncoderAbsoluteAligned = false; wMecAngleRef = SPD_GetMecAngle(STC_GetSpeedSensor(pHandle->pSTC)); TC_MoveCommand(pHandle, (float)(wMecAngleRef) / RADTOS16, Z_ALIGNMENT_NB_ROTATION, Z_ALIGNMENT_DURATION); pHandle->AlignmentStatus = TC_ZERO_ALIGNMENT_START; } else { /* If index is not supprted set the alignment angle as zero reference */ pHandle->pENC->_Super.wMecAngle = 0; pHandle->AlignmentStatus = TC_ALIGNMENT_COMPLETED; pHandle->PositionCtrlStatus = TC_READY_FOR_COMMAND; pHandle->PositionControlRegulation = ENABLE; } } } /** * @brief It controls if time allowed for movement is completed. * @param pHandle: handler of the current instance of the Position Control component. * @retval Status return true when the programmed trajectory movement is completed * false when the trajectory movement execution is still ongoing. */ bool TC_RampCompleted(PosCtrl_Handle_t *pHandle) { bool retVal = false; /* Check that entire sequence (Acceleration - Cruise - Deceleration) is completed */ if (pHandle->ElapseTime > pHandle->MovementDuration + pHandle->SamplingTime) { retVal = true; } else { /* Nothing to do */ } return (retVal); } /** * @brief Set the absolute zero mechanical position. * @param pHandle: handler of the current instance of the Position Control component. */ void TC_EncoderReset(PosCtrl_Handle_t *pHandle) { if ((!pHandle->EncoderAbsoluteAligned) && (pHandle->AlignmentStatus == TC_ZERO_ALIGNMENT_START)) { pHandle->MecAngleOffset = pHandle->pENC->_Super.hMecAngle; pHandle->pENC->_Super.wMecAngle = 0; pHandle->EncoderAbsoluteAligned = true; pHandle->AlignmentStatus = TC_ALIGNMENT_COMPLETED; pHandle->PositionCtrlStatus = TC_READY_FOR_COMMAND; pHandle->Theta = 0.0f; ENC_SetMecAngle(pHandle->pENC, pHandle->MecAngleOffset); } else { /* Nothing to do */ } } /** * @brief Returns the current rotor mechanical angle, expressed in radiant. * @param pHandle: handler of the current instance of the Position Control component. * @retval current mechanical position */ float TC_GetCurrentPosition(PosCtrl_Handle_t *pHandle) { return ((float)((SPD_GetMecAngle(STC_GetSpeedSensor(pHandle->pSTC))) / RADTOS16)); } /** * @brief Returns the target rotor mechanical angle, expressed in radiant. * @param pHandle: handler of the current instance of the Position Control component. * @retval Target mechanical position */ float TC_GetTargetPosition(PosCtrl_Handle_t *pHandle) { return (pHandle->FinalAngle); } /** * @brief Returns the duration used to execute the movement, expressed in seconds. * @param pHandle handler of the current instance of the Position Control component. * @retval Duration of programmed movement */ float TC_GetMoveDuration(PosCtrl_Handle_t *pHandle) { return (pHandle->MovementDuration); } /** * @brief Returns the status of the position control execution. * @param pHandle: handler of the current instance of the Position Control component. * @retval Position Control Status */ PosCtrlStatus_t TC_GetControlPositionStatus(PosCtrl_Handle_t *pHandle) { return (pHandle->PositionCtrlStatus); } /** * @brief Returns the status after the rotor alignment phase. * @param pHandle handler of the current instance of the Position Control component. */ AlignStatus_t TC_GetAlignmentStatus(PosCtrl_Handle_t *pHandle) { return (pHandle->AlignmentStatus); } /** * @brief Increments Tick counter used in follow mode. * @param pHandle handler of the current instance of the Position Control component. */ void TC_IncTick(PosCtrl_Handle_t *pHandle) { pHandle->TcTick++; } /** * @} */ /** * @} */ /************************ (C) COPYRIGHT 2023 STMicroelectronics *****END OF FILE****/
16,855
C
30.803774
127
0.670958